1 //===-- ScriptInterpreterPython.cpp ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Host/Config.h"
10 #include "lldb/lldb-enumerations.h"
11 
12 #if LLDB_ENABLE_PYTHON
13 
14 // LLDB Python header must be included first
15 #include "lldb-python.h"
16 
17 #include "PythonDataObjects.h"
18 #include "PythonReadline.h"
19 #include "SWIGPythonBridge.h"
20 #include "ScriptInterpreterPythonImpl.h"
21 #include "ScriptedPlatformPythonInterface.h"
22 #include "ScriptedProcessPythonInterface.h"
23 
24 #include "lldb/API/SBError.h"
25 #include "lldb/API/SBFrame.h"
26 #include "lldb/API/SBValue.h"
27 #include "lldb/Breakpoint/StoppointCallbackContext.h"
28 #include "lldb/Breakpoint/WatchpointOptions.h"
29 #include "lldb/Core/Debugger.h"
30 #include "lldb/Core/PluginManager.h"
31 #include "lldb/Core/ThreadedCommunication.h"
32 #include "lldb/Core/ValueObject.h"
33 #include "lldb/DataFormatters/TypeSummary.h"
34 #include "lldb/Host/FileSystem.h"
35 #include "lldb/Host/HostInfo.h"
36 #include "lldb/Host/Pipe.h"
37 #include "lldb/Interpreter/CommandInterpreter.h"
38 #include "lldb/Interpreter/CommandReturnObject.h"
39 #include "lldb/Target/Thread.h"
40 #include "lldb/Target/ThreadPlan.h"
41 #include "lldb/Utility/Instrumentation.h"
42 #include "lldb/Utility/LLDBLog.h"
43 #include "lldb/Utility/Timer.h"
44 #include "llvm/ADT/STLExtras.h"
45 #include "llvm/ADT/StringRef.h"
46 #include "llvm/Support/Error.h"
47 #include "llvm/Support/FileSystem.h"
48 #include "llvm/Support/FormatAdapters.h"
49 
50 #include <cstdio>
51 #include <cstdlib>
52 #include <memory>
53 #include <mutex>
54 #include <optional>
55 #include <string>
56 
57 using namespace lldb;
58 using namespace lldb_private;
59 using namespace lldb_private::python;
60 using llvm::Expected;
61 
62 LLDB_PLUGIN_DEFINE(ScriptInterpreterPython)
63 
64 // Defined in the SWIG source file
65 extern "C" PyObject *PyInit__lldb(void);
66 
67 #define LLDBSwigPyInit PyInit__lldb
68 
69 #if defined(_WIN32)
70 // Don't mess with the signal handlers on Windows.
71 #define LLDB_USE_PYTHON_SET_INTERRUPT 0
72 #else
73 // PyErr_SetInterrupt was introduced in 3.2.
74 #define LLDB_USE_PYTHON_SET_INTERRUPT                                          \
75   (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
76 #endif
77 
78 static ScriptInterpreterPythonImpl *GetPythonInterpreter(Debugger &debugger) {
79   ScriptInterpreter *script_interpreter =
80       debugger.GetScriptInterpreter(true, lldb::eScriptLanguagePython);
81   return static_cast<ScriptInterpreterPythonImpl *>(script_interpreter);
82 }
83 
84 namespace {
85 
86 // Initializing Python is not a straightforward process.  We cannot control
87 // what external code may have done before getting to this point in LLDB,
88 // including potentially having already initialized Python, so we need to do a
89 // lot of work to ensure that the existing state of the system is maintained
90 // across our initialization.  We do this by using an RAII pattern where we
91 // save off initial state at the beginning, and restore it at the end
92 struct InitializePythonRAII {
93 public:
94   InitializePythonRAII() {
95     InitializePythonHome();
96 
97 #ifdef LLDB_USE_LIBEDIT_READLINE_COMPAT_MODULE
98     // Python's readline is incompatible with libedit being linked into lldb.
99     // Provide a patched version local to the embedded interpreter.
100     bool ReadlinePatched = false;
101     for (auto *p = PyImport_Inittab; p->name != nullptr; p++) {
102       if (strcmp(p->name, "readline") == 0) {
103         p->initfunc = initlldb_readline;
104         break;
105       }
106     }
107     if (!ReadlinePatched) {
108       PyImport_AppendInittab("readline", initlldb_readline);
109       ReadlinePatched = true;
110     }
111 #endif
112 
113     // Register _lldb as a built-in module.
114     PyImport_AppendInittab("_lldb", LLDBSwigPyInit);
115 
116 // Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
117 // calling `Py_Initialize` and `PyEval_InitThreads`.  < 3.2 requires that you
118 // call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
119 #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
120     Py_InitializeEx(0);
121     InitializeThreadsPrivate();
122 #else
123     InitializeThreadsPrivate();
124     Py_InitializeEx(0);
125 #endif
126   }
127 
128   ~InitializePythonRAII() {
129     if (m_was_already_initialized) {
130       Log *log = GetLog(LLDBLog::Script);
131       LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked",
132                 m_gil_state == PyGILState_UNLOCKED ? "un" : "");
133       PyGILState_Release(m_gil_state);
134     } else {
135       // We initialized the threads in this function, just unlock the GIL.
136       PyEval_SaveThread();
137     }
138   }
139 
140 private:
141   void InitializePythonHome() {
142 #if LLDB_EMBED_PYTHON_HOME
143     typedef wchar_t *str_type;
144     static str_type g_python_home = []() -> str_type {
145       const char *lldb_python_home = LLDB_PYTHON_HOME;
146       const char *absolute_python_home = nullptr;
147       llvm::SmallString<64> path;
148       if (llvm::sys::path::is_absolute(lldb_python_home)) {
149         absolute_python_home = lldb_python_home;
150       } else {
151         FileSpec spec = HostInfo::GetShlibDir();
152         if (!spec)
153           return nullptr;
154         spec.GetPath(path);
155         llvm::sys::path::append(path, lldb_python_home);
156         absolute_python_home = path.c_str();
157       }
158       size_t size = 0;
159       return Py_DecodeLocale(absolute_python_home, &size);
160     }();
161     if (g_python_home != nullptr) {
162       Py_SetPythonHome(g_python_home);
163     }
164 #endif
165   }
166 
167   void InitializeThreadsPrivate() {
168 // Since Python 3.7 `Py_Initialize` calls `PyEval_InitThreads` inside itself,
169 // so there is no way to determine whether the embedded interpreter
170 // was already initialized by some external code. `PyEval_ThreadsInitialized`
171 // would always return `true` and `PyGILState_Ensure/Release` flow would be
172 // executed instead of unlocking GIL with `PyEval_SaveThread`. When
173 // an another thread calls `PyGILState_Ensure` it would get stuck in deadlock.
174 #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 7) || (PY_MAJOR_VERSION > 3)
175     // The only case we should go further and acquire the GIL: it is unlocked.
176     if (PyGILState_Check())
177       return;
178 #endif
179 
180     if (PyEval_ThreadsInitialized()) {
181       Log *log = GetLog(LLDBLog::Script);
182 
183       m_was_already_initialized = true;
184       m_gil_state = PyGILState_Ensure();
185       LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked\n",
186                 m_gil_state == PyGILState_UNLOCKED ? "un" : "");
187       return;
188     }
189 
190     // InitThreads acquires the GIL if it hasn't been called before.
191     PyEval_InitThreads();
192   }
193 
194   PyGILState_STATE m_gil_state = PyGILState_UNLOCKED;
195   bool m_was_already_initialized = false;
196 };
197 
198 #if LLDB_USE_PYTHON_SET_INTERRUPT
199 /// Saves the current signal handler for the specified signal and restores
200 /// it at the end of the current scope.
201 struct RestoreSignalHandlerScope {
202   /// The signal handler.
203   struct sigaction m_prev_handler;
204   int m_signal_code;
205   RestoreSignalHandlerScope(int signal_code) : m_signal_code(signal_code) {
206     // Initialize sigaction to their default state.
207     std::memset(&m_prev_handler, 0, sizeof(m_prev_handler));
208     // Don't install a new handler, just read back the old one.
209     struct sigaction *new_handler = nullptr;
210     int signal_err = ::sigaction(m_signal_code, new_handler, &m_prev_handler);
211     lldbassert(signal_err == 0 && "sigaction failed to read handler");
212   }
213   ~RestoreSignalHandlerScope() {
214     int signal_err = ::sigaction(m_signal_code, &m_prev_handler, nullptr);
215     lldbassert(signal_err == 0 && "sigaction failed to restore old handler");
216   }
217 };
218 #endif
219 } // namespace
220 
221 void ScriptInterpreterPython::ComputePythonDirForApple(
222     llvm::SmallVectorImpl<char> &path) {
223   auto style = llvm::sys::path::Style::posix;
224 
225   llvm::StringRef path_ref(path.begin(), path.size());
226   auto rbegin = llvm::sys::path::rbegin(path_ref, style);
227   auto rend = llvm::sys::path::rend(path_ref);
228   auto framework = std::find(rbegin, rend, "LLDB.framework");
229   if (framework == rend) {
230     ComputePythonDir(path);
231     return;
232   }
233   path.resize(framework - rend);
234   llvm::sys::path::append(path, style, "LLDB.framework", "Resources", "Python");
235 }
236 
237 void ScriptInterpreterPython::ComputePythonDir(
238     llvm::SmallVectorImpl<char> &path) {
239   // Build the path by backing out of the lib dir, then building with whatever
240   // the real python interpreter uses.  (e.g. lib for most, lib64 on RHEL
241   // x86_64, or bin on Windows).
242   llvm::sys::path::remove_filename(path);
243   llvm::sys::path::append(path, LLDB_PYTHON_RELATIVE_LIBDIR);
244 
245 #if defined(_WIN32)
246   // This will be injected directly through FileSpec.SetDirectory(),
247   // so we need to normalize manually.
248   std::replace(path.begin(), path.end(), '\\', '/');
249 #endif
250 }
251 
252 FileSpec ScriptInterpreterPython::GetPythonDir() {
253   static FileSpec g_spec = []() {
254     FileSpec spec = HostInfo::GetShlibDir();
255     if (!spec)
256       return FileSpec();
257     llvm::SmallString<64> path;
258     spec.GetPath(path);
259 
260 #if defined(__APPLE__)
261     ComputePythonDirForApple(path);
262 #else
263     ComputePythonDir(path);
264 #endif
265     spec.SetDirectory(path);
266     return spec;
267   }();
268   return g_spec;
269 }
270 
271 static const char GetInterpreterInfoScript[] = R"(
272 import os
273 import sys
274 
275 def main(lldb_python_dir, python_exe_relative_path):
276   info = {
277     "lldb-pythonpath": lldb_python_dir,
278     "language": "python",
279     "prefix": sys.prefix,
280     "executable": os.path.join(sys.prefix, python_exe_relative_path)
281   }
282   return info
283 )";
284 
285 static const char python_exe_relative_path[] = LLDB_PYTHON_EXE_RELATIVE_PATH;
286 
287 StructuredData::DictionarySP ScriptInterpreterPython::GetInterpreterInfo() {
288   GIL gil;
289   FileSpec python_dir_spec = GetPythonDir();
290   if (!python_dir_spec)
291     return nullptr;
292   PythonScript get_info(GetInterpreterInfoScript);
293   auto info_json = unwrapIgnoringErrors(
294       As<PythonDictionary>(get_info(PythonString(python_dir_spec.GetPath()),
295                                     PythonString(python_exe_relative_path))));
296   if (!info_json)
297     return nullptr;
298   return info_json.CreateStructuredDictionary();
299 }
300 
301 void ScriptInterpreterPython::SharedLibraryDirectoryHelper(
302     FileSpec &this_file) {
303   // When we're loaded from python, this_file will point to the file inside the
304   // python package directory. Replace it with the one in the lib directory.
305 #ifdef _WIN32
306   // On windows, we need to manually back out of the python tree, and go into
307   // the bin directory. This is pretty much the inverse of what ComputePythonDir
308   // does.
309   if (this_file.GetFileNameExtension() == ".pyd") {
310     this_file.RemoveLastPathComponent(); // _lldb.pyd or _lldb_d.pyd
311     this_file.RemoveLastPathComponent(); // lldb
312     llvm::StringRef libdir = LLDB_PYTHON_RELATIVE_LIBDIR;
313     for (auto it = llvm::sys::path::begin(libdir),
314               end = llvm::sys::path::end(libdir);
315          it != end; ++it)
316       this_file.RemoveLastPathComponent();
317     this_file.AppendPathComponent("bin");
318     this_file.AppendPathComponent("liblldb.dll");
319   }
320 #else
321   // The python file is a symlink, so we can find the real library by resolving
322   // it. We can do this unconditionally.
323   FileSystem::Instance().ResolveSymbolicLink(this_file, this_file);
324 #endif
325 }
326 
327 llvm::StringRef ScriptInterpreterPython::GetPluginDescriptionStatic() {
328   return "Embedded Python interpreter";
329 }
330 
331 void ScriptInterpreterPython::Initialize() {
332   static llvm::once_flag g_once_flag;
333   llvm::call_once(g_once_flag, []() {
334     PluginManager::RegisterPlugin(GetPluginNameStatic(),
335                                   GetPluginDescriptionStatic(),
336                                   lldb::eScriptLanguagePython,
337                                   ScriptInterpreterPythonImpl::CreateInstance);
338     ScriptInterpreterPythonImpl::Initialize();
339   });
340 }
341 
342 void ScriptInterpreterPython::Terminate() {}
343 
344 ScriptInterpreterPythonImpl::Locker::Locker(
345     ScriptInterpreterPythonImpl *py_interpreter, uint16_t on_entry,
346     uint16_t on_leave, FileSP in, FileSP out, FileSP err)
347     : ScriptInterpreterLocker(),
348       m_teardown_session((on_leave & TearDownSession) == TearDownSession),
349       m_python_interpreter(py_interpreter) {
350   DoAcquireLock();
351   if ((on_entry & InitSession) == InitSession) {
352     if (!DoInitSession(on_entry, in, out, err)) {
353       // Don't teardown the session if we didn't init it.
354       m_teardown_session = false;
355     }
356   }
357 }
358 
359 bool ScriptInterpreterPythonImpl::Locker::DoAcquireLock() {
360   Log *log = GetLog(LLDBLog::Script);
361   m_GILState = PyGILState_Ensure();
362   LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked",
363             m_GILState == PyGILState_UNLOCKED ? "un" : "");
364 
365   // we need to save the thread state when we first start the command because
366   // we might decide to interrupt it while some action is taking place outside
367   // of Python (e.g. printing to screen, waiting for the network, ...) in that
368   // case, _PyThreadState_Current will be NULL - and we would be unable to set
369   // the asynchronous exception - not a desirable situation
370   m_python_interpreter->SetThreadState(PyThreadState_Get());
371   m_python_interpreter->IncrementLockCount();
372   return true;
373 }
374 
375 bool ScriptInterpreterPythonImpl::Locker::DoInitSession(uint16_t on_entry_flags,
376                                                         FileSP in, FileSP out,
377                                                         FileSP err) {
378   if (!m_python_interpreter)
379     return false;
380   return m_python_interpreter->EnterSession(on_entry_flags, in, out, err);
381 }
382 
383 bool ScriptInterpreterPythonImpl::Locker::DoFreeLock() {
384   Log *log = GetLog(LLDBLog::Script);
385   LLDB_LOGV(log, "Releasing PyGILState. Returning to state = {0}locked",
386             m_GILState == PyGILState_UNLOCKED ? "un" : "");
387   PyGILState_Release(m_GILState);
388   m_python_interpreter->DecrementLockCount();
389   return true;
390 }
391 
392 bool ScriptInterpreterPythonImpl::Locker::DoTearDownSession() {
393   if (!m_python_interpreter)
394     return false;
395   m_python_interpreter->LeaveSession();
396   return true;
397 }
398 
399 ScriptInterpreterPythonImpl::Locker::~Locker() {
400   if (m_teardown_session)
401     DoTearDownSession();
402   DoFreeLock();
403 }
404 
405 ScriptInterpreterPythonImpl::ScriptInterpreterPythonImpl(Debugger &debugger)
406     : ScriptInterpreterPython(debugger), m_saved_stdin(), m_saved_stdout(),
407       m_saved_stderr(), m_main_module(),
408       m_session_dict(PyInitialValue::Invalid),
409       m_sys_module_dict(PyInitialValue::Invalid), m_run_one_line_function(),
410       m_run_one_line_str_global(),
411       m_dictionary_name(m_debugger.GetInstanceName()),
412       m_active_io_handler(eIOHandlerNone), m_session_is_active(false),
413       m_pty_secondary_is_open(false), m_valid_session(true), m_lock_count(0),
414       m_command_thread_state(nullptr) {
415   m_scripted_platform_interface_up =
416       std::make_unique<ScriptedPlatformPythonInterface>(*this);
417 
418   m_dictionary_name.append("_dict");
419   StreamString run_string;
420   run_string.Printf("%s = dict()", m_dictionary_name.c_str());
421 
422   Locker locker(this, Locker::AcquireLock, Locker::FreeAcquiredLock);
423   PyRun_SimpleString(run_string.GetData());
424 
425   run_string.Clear();
426   run_string.Printf(
427       "run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')",
428       m_dictionary_name.c_str());
429   PyRun_SimpleString(run_string.GetData());
430 
431   // Reloading modules requires a different syntax in Python 2 and Python 3.
432   // This provides a consistent syntax no matter what version of Python.
433   run_string.Clear();
434   run_string.Printf("run_one_line (%s, 'from importlib import reload as reload_module')",
435                     m_dictionary_name.c_str());
436   PyRun_SimpleString(run_string.GetData());
437 
438   // WARNING: temporary code that loads Cocoa formatters - this should be done
439   // on a per-platform basis rather than loading the whole set and letting the
440   // individual formatter classes exploit APIs to check whether they can/cannot
441   // do their task
442   run_string.Clear();
443   run_string.Printf(
444       "run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp')",
445       m_dictionary_name.c_str());
446   PyRun_SimpleString(run_string.GetData());
447   run_string.Clear();
448 
449   run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from "
450                     "lldb.embedded_interpreter import run_python_interpreter; "
451                     "from lldb.embedded_interpreter import run_one_line')",
452                     m_dictionary_name.c_str());
453   PyRun_SimpleString(run_string.GetData());
454   run_string.Clear();
455 
456   run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64
457                     "')",
458                     m_dictionary_name.c_str(), m_debugger.GetID());
459   PyRun_SimpleString(run_string.GetData());
460 }
461 
462 ScriptInterpreterPythonImpl::~ScriptInterpreterPythonImpl() {
463   // the session dictionary may hold objects with complex state which means
464   // that they may need to be torn down with some level of smarts and that, in
465   // turn, requires a valid thread state force Python to procure itself such a
466   // thread state, nuke the session dictionary and then release it for others
467   // to use and proceed with the rest of the shutdown
468   auto gil_state = PyGILState_Ensure();
469   m_session_dict.Reset();
470   PyGILState_Release(gil_state);
471 }
472 
473 void ScriptInterpreterPythonImpl::IOHandlerActivated(IOHandler &io_handler,
474                                                      bool interactive) {
475   const char *instructions = nullptr;
476 
477   switch (m_active_io_handler) {
478   case eIOHandlerNone:
479     break;
480   case eIOHandlerBreakpoint:
481     instructions = R"(Enter your Python command(s). Type 'DONE' to end.
482 def function (frame, bp_loc, internal_dict):
483     """frame: the lldb.SBFrame for the location at which you stopped
484        bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
485        internal_dict: an LLDB support object not to be used"""
486 )";
487     break;
488   case eIOHandlerWatchpoint:
489     instructions = "Enter your Python command(s). Type 'DONE' to end.\n";
490     break;
491   }
492 
493   if (instructions) {
494     StreamFileSP output_sp(io_handler.GetOutputStreamFileSP());
495     if (output_sp && interactive) {
496       output_sp->PutCString(instructions);
497       output_sp->Flush();
498     }
499   }
500 }
501 
502 void ScriptInterpreterPythonImpl::IOHandlerInputComplete(IOHandler &io_handler,
503                                                          std::string &data) {
504   io_handler.SetIsDone(true);
505   bool batch_mode = m_debugger.GetCommandInterpreter().GetBatchCommandMode();
506 
507   switch (m_active_io_handler) {
508   case eIOHandlerNone:
509     break;
510   case eIOHandlerBreakpoint: {
511     std::vector<std::reference_wrapper<BreakpointOptions>> *bp_options_vec =
512         (std::vector<std::reference_wrapper<BreakpointOptions>> *)
513             io_handler.GetUserData();
514     for (BreakpointOptions &bp_options : *bp_options_vec) {
515 
516       auto data_up = std::make_unique<CommandDataPython>();
517       if (!data_up)
518         break;
519       data_up->user_source.SplitIntoLines(data);
520 
521       StructuredData::ObjectSP empty_args_sp;
522       if (GenerateBreakpointCommandCallbackData(data_up->user_source,
523                                                 data_up->script_source,
524                                                 /*has_extra_args=*/false,
525                                                 /*is_callback=*/false)
526               .Success()) {
527         auto baton_sp = std::make_shared<BreakpointOptions::CommandBaton>(
528             std::move(data_up));
529         bp_options.SetCallback(
530             ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
531       } else if (!batch_mode) {
532         StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
533         if (error_sp) {
534           error_sp->Printf("Warning: No command attached to breakpoint.\n");
535           error_sp->Flush();
536         }
537       }
538     }
539     m_active_io_handler = eIOHandlerNone;
540   } break;
541   case eIOHandlerWatchpoint: {
542     WatchpointOptions *wp_options =
543         (WatchpointOptions *)io_handler.GetUserData();
544     auto data_up = std::make_unique<WatchpointOptions::CommandData>();
545     data_up->user_source.SplitIntoLines(data);
546 
547     if (GenerateWatchpointCommandCallbackData(data_up->user_source,
548                                               data_up->script_source,
549                                               /*is_callback=*/false)) {
550       auto baton_sp =
551           std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
552       wp_options->SetCallback(
553           ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp);
554     } else if (!batch_mode) {
555       StreamFileSP error_sp = io_handler.GetErrorStreamFileSP();
556       if (error_sp) {
557         error_sp->Printf("Warning: No command attached to breakpoint.\n");
558         error_sp->Flush();
559       }
560     }
561     m_active_io_handler = eIOHandlerNone;
562   } break;
563   }
564 }
565 
566 lldb::ScriptInterpreterSP
567 ScriptInterpreterPythonImpl::CreateInstance(Debugger &debugger) {
568   return std::make_shared<ScriptInterpreterPythonImpl>(debugger);
569 }
570 
571 void ScriptInterpreterPythonImpl::LeaveSession() {
572   Log *log = GetLog(LLDBLog::Script);
573   if (log)
574     log->PutCString("ScriptInterpreterPythonImpl::LeaveSession()");
575 
576   // Unset the LLDB global variables.
577   PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
578                      "= None; lldb.thread = None; lldb.frame = None");
579 
580   // checking that we have a valid thread state - since we use our own
581   // threading and locking in some (rare) cases during cleanup Python may end
582   // up believing we have no thread state and PyImport_AddModule will crash if
583   // that is the case - since that seems to only happen when destroying the
584   // SBDebugger, we can make do without clearing up stdout and stderr
585 
586   // rdar://problem/11292882
587   // When the current thread state is NULL, PyThreadState_Get() issues a fatal
588   // error.
589   if (PyThreadState_GetDict()) {
590     PythonDictionary &sys_module_dict = GetSysModuleDictionary();
591     if (sys_module_dict.IsValid()) {
592       if (m_saved_stdin.IsValid()) {
593         sys_module_dict.SetItemForKey(PythonString("stdin"), m_saved_stdin);
594         m_saved_stdin.Reset();
595       }
596       if (m_saved_stdout.IsValid()) {
597         sys_module_dict.SetItemForKey(PythonString("stdout"), m_saved_stdout);
598         m_saved_stdout.Reset();
599       }
600       if (m_saved_stderr.IsValid()) {
601         sys_module_dict.SetItemForKey(PythonString("stderr"), m_saved_stderr);
602         m_saved_stderr.Reset();
603       }
604     }
605   }
606 
607   m_session_is_active = false;
608 }
609 
610 bool ScriptInterpreterPythonImpl::SetStdHandle(FileSP file_sp,
611                                                const char *py_name,
612                                                PythonObject &save_file,
613                                                const char *mode) {
614   if (!file_sp || !*file_sp) {
615     save_file.Reset();
616     return false;
617   }
618   File &file = *file_sp;
619 
620   // Flush the file before giving it to python to avoid interleaved output.
621   file.Flush();
622 
623   PythonDictionary &sys_module_dict = GetSysModuleDictionary();
624 
625   auto new_file = PythonFile::FromFile(file, mode);
626   if (!new_file) {
627     llvm::consumeError(new_file.takeError());
628     return false;
629   }
630 
631   save_file = sys_module_dict.GetItemForKey(PythonString(py_name));
632 
633   sys_module_dict.SetItemForKey(PythonString(py_name), new_file.get());
634   return true;
635 }
636 
637 bool ScriptInterpreterPythonImpl::EnterSession(uint16_t on_entry_flags,
638                                                FileSP in_sp, FileSP out_sp,
639                                                FileSP err_sp) {
640   // If we have already entered the session, without having officially 'left'
641   // it, then there is no need to 'enter' it again.
642   Log *log = GetLog(LLDBLog::Script);
643   if (m_session_is_active) {
644     LLDB_LOGF(
645         log,
646         "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16
647         ") session is already active, returning without doing anything",
648         on_entry_flags);
649     return false;
650   }
651 
652   LLDB_LOGF(
653       log,
654       "ScriptInterpreterPythonImpl::EnterSession(on_entry_flags=0x%" PRIx16 ")",
655       on_entry_flags);
656 
657   m_session_is_active = true;
658 
659   StreamString run_string;
660 
661   if (on_entry_flags & Locker::InitGlobals) {
662     run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64,
663                       m_dictionary_name.c_str(), m_debugger.GetID());
664     run_string.Printf(
665         "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")",
666         m_debugger.GetID());
667     run_string.PutCString("; lldb.target = lldb.debugger.GetSelectedTarget()");
668     run_string.PutCString("; lldb.process = lldb.target.GetProcess()");
669     run_string.PutCString("; lldb.thread = lldb.process.GetSelectedThread ()");
670     run_string.PutCString("; lldb.frame = lldb.thread.GetSelectedFrame ()");
671     run_string.PutCString("')");
672   } else {
673     // If we aren't initing the globals, we should still always set the
674     // debugger (since that is always unique.)
675     run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64,
676                       m_dictionary_name.c_str(), m_debugger.GetID());
677     run_string.Printf(
678         "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")",
679         m_debugger.GetID());
680     run_string.PutCString("')");
681   }
682 
683   PyRun_SimpleString(run_string.GetData());
684   run_string.Clear();
685 
686   PythonDictionary &sys_module_dict = GetSysModuleDictionary();
687   if (sys_module_dict.IsValid()) {
688     lldb::FileSP top_in_sp;
689     lldb::StreamFileSP top_out_sp, top_err_sp;
690     if (!in_sp || !out_sp || !err_sp || !*in_sp || !*out_sp || !*err_sp)
691       m_debugger.AdoptTopIOHandlerFilesIfInvalid(top_in_sp, top_out_sp,
692                                                  top_err_sp);
693 
694     if (on_entry_flags & Locker::NoSTDIN) {
695       m_saved_stdin.Reset();
696     } else {
697       if (!SetStdHandle(in_sp, "stdin", m_saved_stdin, "r")) {
698         if (top_in_sp)
699           SetStdHandle(top_in_sp, "stdin", m_saved_stdin, "r");
700       }
701     }
702 
703     if (!SetStdHandle(out_sp, "stdout", m_saved_stdout, "w")) {
704       if (top_out_sp)
705         SetStdHandle(top_out_sp->GetFileSP(), "stdout", m_saved_stdout, "w");
706     }
707 
708     if (!SetStdHandle(err_sp, "stderr", m_saved_stderr, "w")) {
709       if (top_err_sp)
710         SetStdHandle(top_err_sp->GetFileSP(), "stderr", m_saved_stderr, "w");
711     }
712   }
713 
714   if (PyErr_Occurred())
715     PyErr_Clear();
716 
717   return true;
718 }
719 
720 PythonModule &ScriptInterpreterPythonImpl::GetMainModule() {
721   if (!m_main_module.IsValid())
722     m_main_module = unwrapIgnoringErrors(PythonModule::Import("__main__"));
723   return m_main_module;
724 }
725 
726 PythonDictionary &ScriptInterpreterPythonImpl::GetSessionDictionary() {
727   if (m_session_dict.IsValid())
728     return m_session_dict;
729 
730   PythonObject &main_module = GetMainModule();
731   if (!main_module.IsValid())
732     return m_session_dict;
733 
734   PythonDictionary main_dict(PyRefType::Borrowed,
735                              PyModule_GetDict(main_module.get()));
736   if (!main_dict.IsValid())
737     return m_session_dict;
738 
739   m_session_dict = unwrapIgnoringErrors(
740       As<PythonDictionary>(main_dict.GetItem(m_dictionary_name)));
741   return m_session_dict;
742 }
743 
744 PythonDictionary &ScriptInterpreterPythonImpl::GetSysModuleDictionary() {
745   if (m_sys_module_dict.IsValid())
746     return m_sys_module_dict;
747   PythonModule sys_module = unwrapIgnoringErrors(PythonModule::Import("sys"));
748   m_sys_module_dict = sys_module.GetDictionary();
749   return m_sys_module_dict;
750 }
751 
752 llvm::Expected<unsigned>
753 ScriptInterpreterPythonImpl::GetMaxPositionalArgumentsForCallable(
754     const llvm::StringRef &callable_name) {
755   if (callable_name.empty()) {
756     return llvm::createStringError(
757         llvm::inconvertibleErrorCode(),
758         "called with empty callable name.");
759   }
760   Locker py_lock(this, Locker::AcquireLock |
761                  Locker::InitSession |
762                  Locker::NoSTDIN);
763   auto dict = PythonModule::MainModule()
764       .ResolveName<PythonDictionary>(m_dictionary_name);
765   auto pfunc = PythonObject::ResolveNameWithDictionary<PythonCallable>(
766       callable_name, dict);
767   if (!pfunc.IsAllocated()) {
768     return llvm::createStringError(
769         llvm::inconvertibleErrorCode(),
770         "can't find callable: %s", callable_name.str().c_str());
771   }
772   llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
773   if (!arg_info)
774     return arg_info.takeError();
775   return arg_info.get().max_positional_args;
776 }
777 
778 static std::string GenerateUniqueName(const char *base_name_wanted,
779                                       uint32_t &functions_counter,
780                                       const void *name_token = nullptr) {
781   StreamString sstr;
782 
783   if (!base_name_wanted)
784     return std::string();
785 
786   if (!name_token)
787     sstr.Printf("%s_%d", base_name_wanted, functions_counter++);
788   else
789     sstr.Printf("%s_%p", base_name_wanted, name_token);
790 
791   return std::string(sstr.GetString());
792 }
793 
794 bool ScriptInterpreterPythonImpl::GetEmbeddedInterpreterModuleObjects() {
795   if (m_run_one_line_function.IsValid())
796     return true;
797 
798   PythonObject module(PyRefType::Borrowed,
799                       PyImport_AddModule("lldb.embedded_interpreter"));
800   if (!module.IsValid())
801     return false;
802 
803   PythonDictionary module_dict(PyRefType::Borrowed,
804                                PyModule_GetDict(module.get()));
805   if (!module_dict.IsValid())
806     return false;
807 
808   m_run_one_line_function =
809       module_dict.GetItemForKey(PythonString("run_one_line"));
810   m_run_one_line_str_global =
811       module_dict.GetItemForKey(PythonString("g_run_one_line_str"));
812   return m_run_one_line_function.IsValid();
813 }
814 
815 bool ScriptInterpreterPythonImpl::ExecuteOneLine(
816     llvm::StringRef command, CommandReturnObject *result,
817     const ExecuteScriptOptions &options) {
818   std::string command_str = command.str();
819 
820   if (!m_valid_session)
821     return false;
822 
823   if (!command.empty()) {
824     // We want to call run_one_line, passing in the dictionary and the command
825     // string.  We cannot do this through PyRun_SimpleString here because the
826     // command string may contain escaped characters, and putting it inside
827     // another string to pass to PyRun_SimpleString messes up the escaping.  So
828     // we use the following more complicated method to pass the command string
829     // directly down to Python.
830     llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
831         io_redirect_or_error = ScriptInterpreterIORedirect::Create(
832             options.GetEnableIO(), m_debugger, result);
833     if (!io_redirect_or_error) {
834       if (result)
835         result->AppendErrorWithFormatv(
836             "failed to redirect I/O: {0}\n",
837             llvm::fmt_consume(io_redirect_or_error.takeError()));
838       else
839         llvm::consumeError(io_redirect_or_error.takeError());
840       return false;
841     }
842 
843     ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
844 
845     bool success = false;
846     {
847       // WARNING!  It's imperative that this RAII scope be as tight as
848       // possible. In particular, the scope must end *before* we try to join
849       // the read thread.  The reason for this is that a pre-requisite for
850       // joining the read thread is that we close the write handle (to break
851       // the pipe and cause it to wake up and exit).  But acquiring the GIL as
852       // below will redirect Python's stdio to use this same handle.  If we
853       // close the handle while Python is still using it, bad things will
854       // happen.
855       Locker locker(
856           this,
857           Locker::AcquireLock | Locker::InitSession |
858               (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
859               ((result && result->GetInteractive()) ? 0 : Locker::NoSTDIN),
860           Locker::FreeAcquiredLock | Locker::TearDownSession,
861           io_redirect.GetInputFile(), io_redirect.GetOutputFile(),
862           io_redirect.GetErrorFile());
863 
864       // Find the correct script interpreter dictionary in the main module.
865       PythonDictionary &session_dict = GetSessionDictionary();
866       if (session_dict.IsValid()) {
867         if (GetEmbeddedInterpreterModuleObjects()) {
868           if (PyCallable_Check(m_run_one_line_function.get())) {
869             PythonObject pargs(
870                 PyRefType::Owned,
871                 Py_BuildValue("(Os)", session_dict.get(), command_str.c_str()));
872             if (pargs.IsValid()) {
873               PythonObject return_value(
874                   PyRefType::Owned,
875                   PyObject_CallObject(m_run_one_line_function.get(),
876                                       pargs.get()));
877               if (return_value.IsValid())
878                 success = true;
879               else if (options.GetMaskoutErrors() && PyErr_Occurred()) {
880                 PyErr_Print();
881                 PyErr_Clear();
882               }
883             }
884           }
885         }
886       }
887 
888       io_redirect.Flush();
889     }
890 
891     if (success)
892       return true;
893 
894     // The one-liner failed.  Append the error message.
895     if (result) {
896       result->AppendErrorWithFormat(
897           "python failed attempting to evaluate '%s'\n", command_str.c_str());
898     }
899     return false;
900   }
901 
902   if (result)
903     result->AppendError("empty command passed to python\n");
904   return false;
905 }
906 
907 void ScriptInterpreterPythonImpl::ExecuteInterpreterLoop() {
908   LLDB_SCOPED_TIMER();
909 
910   Debugger &debugger = m_debugger;
911 
912   // At the moment, the only time the debugger does not have an input file
913   // handle is when this is called directly from Python, in which case it is
914   // both dangerous and unnecessary (not to mention confusing) to try to embed
915   // a running interpreter loop inside the already running Python interpreter
916   // loop, so we won't do it.
917 
918   if (!debugger.GetInputFile().IsValid())
919     return;
920 
921   IOHandlerSP io_handler_sp(new IOHandlerPythonInterpreter(debugger, this));
922   if (io_handler_sp) {
923     debugger.RunIOHandlerAsync(io_handler_sp);
924   }
925 }
926 
927 bool ScriptInterpreterPythonImpl::Interrupt() {
928 #if LLDB_USE_PYTHON_SET_INTERRUPT
929   // If the interpreter isn't evaluating any Python at the moment then return
930   // false to signal that this function didn't handle the interrupt and the
931   // next component should try handling it.
932   if (!IsExecutingPython())
933     return false;
934 
935   // Tell Python that it should pretend to have received a SIGINT.
936   PyErr_SetInterrupt();
937   // PyErr_SetInterrupt has no way to return an error so we can only pretend the
938   // signal got successfully handled and return true.
939   // Python 3.10 introduces PyErr_SetInterruptEx that could return an error, but
940   // the error handling is limited to checking the arguments which would be
941   // just our (hardcoded) input signal code SIGINT, so that's not useful at all.
942   return true;
943 #else
944   Log *log = GetLog(LLDBLog::Script);
945 
946   if (IsExecutingPython()) {
947     PyThreadState *state = PyThreadState_GET();
948     if (!state)
949       state = GetThreadState();
950     if (state) {
951       long tid = state->thread_id;
952       PyThreadState_Swap(state);
953       int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);
954       LLDB_LOGF(log,
955                 "ScriptInterpreterPythonImpl::Interrupt() sending "
956                 "PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...",
957                 tid, num_threads);
958       return true;
959     }
960   }
961   LLDB_LOGF(log,
962             "ScriptInterpreterPythonImpl::Interrupt() python code not running, "
963             "can't interrupt");
964   return false;
965 #endif
966 }
967 
968 bool ScriptInterpreterPythonImpl::ExecuteOneLineWithReturn(
969     llvm::StringRef in_string, ScriptInterpreter::ScriptReturnType return_type,
970     void *ret_value, const ExecuteScriptOptions &options) {
971 
972   llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
973       io_redirect_or_error = ScriptInterpreterIORedirect::Create(
974           options.GetEnableIO(), m_debugger, /*result=*/nullptr);
975 
976   if (!io_redirect_or_error) {
977     llvm::consumeError(io_redirect_or_error.takeError());
978     return false;
979   }
980 
981   ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
982 
983   Locker locker(this,
984                 Locker::AcquireLock | Locker::InitSession |
985                     (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
986                     Locker::NoSTDIN,
987                 Locker::FreeAcquiredLock | Locker::TearDownSession,
988                 io_redirect.GetInputFile(), io_redirect.GetOutputFile(),
989                 io_redirect.GetErrorFile());
990 
991   PythonModule &main_module = GetMainModule();
992   PythonDictionary globals = main_module.GetDictionary();
993 
994   PythonDictionary locals = GetSessionDictionary();
995   if (!locals.IsValid())
996     locals = unwrapIgnoringErrors(
997         As<PythonDictionary>(globals.GetAttribute(m_dictionary_name)));
998   if (!locals.IsValid())
999     locals = globals;
1000 
1001   Expected<PythonObject> maybe_py_return =
1002       runStringOneLine(in_string, globals, locals);
1003 
1004   if (!maybe_py_return) {
1005     llvm::handleAllErrors(
1006         maybe_py_return.takeError(),
1007         [&](PythonException &E) {
1008           E.Restore();
1009           if (options.GetMaskoutErrors()) {
1010             if (E.Matches(PyExc_SyntaxError)) {
1011               PyErr_Print();
1012             }
1013             PyErr_Clear();
1014           }
1015         },
1016         [](const llvm::ErrorInfoBase &E) {});
1017     return false;
1018   }
1019 
1020   PythonObject py_return = std::move(maybe_py_return.get());
1021   assert(py_return.IsValid());
1022 
1023   switch (return_type) {
1024   case eScriptReturnTypeCharPtr: // "char *"
1025   {
1026     const char format[3] = "s#";
1027     return PyArg_Parse(py_return.get(), format, (char **)ret_value);
1028   }
1029   case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return ==
1030                                        // Py_None
1031   {
1032     const char format[3] = "z";
1033     return PyArg_Parse(py_return.get(), format, (char **)ret_value);
1034   }
1035   case eScriptReturnTypeBool: {
1036     const char format[2] = "b";
1037     return PyArg_Parse(py_return.get(), format, (bool *)ret_value);
1038   }
1039   case eScriptReturnTypeShortInt: {
1040     const char format[2] = "h";
1041     return PyArg_Parse(py_return.get(), format, (short *)ret_value);
1042   }
1043   case eScriptReturnTypeShortIntUnsigned: {
1044     const char format[2] = "H";
1045     return PyArg_Parse(py_return.get(), format, (unsigned short *)ret_value);
1046   }
1047   case eScriptReturnTypeInt: {
1048     const char format[2] = "i";
1049     return PyArg_Parse(py_return.get(), format, (int *)ret_value);
1050   }
1051   case eScriptReturnTypeIntUnsigned: {
1052     const char format[2] = "I";
1053     return PyArg_Parse(py_return.get(), format, (unsigned int *)ret_value);
1054   }
1055   case eScriptReturnTypeLongInt: {
1056     const char format[2] = "l";
1057     return PyArg_Parse(py_return.get(), format, (long *)ret_value);
1058   }
1059   case eScriptReturnTypeLongIntUnsigned: {
1060     const char format[2] = "k";
1061     return PyArg_Parse(py_return.get(), format, (unsigned long *)ret_value);
1062   }
1063   case eScriptReturnTypeLongLong: {
1064     const char format[2] = "L";
1065     return PyArg_Parse(py_return.get(), format, (long long *)ret_value);
1066   }
1067   case eScriptReturnTypeLongLongUnsigned: {
1068     const char format[2] = "K";
1069     return PyArg_Parse(py_return.get(), format,
1070                        (unsigned long long *)ret_value);
1071   }
1072   case eScriptReturnTypeFloat: {
1073     const char format[2] = "f";
1074     return PyArg_Parse(py_return.get(), format, (float *)ret_value);
1075   }
1076   case eScriptReturnTypeDouble: {
1077     const char format[2] = "d";
1078     return PyArg_Parse(py_return.get(), format, (double *)ret_value);
1079   }
1080   case eScriptReturnTypeChar: {
1081     const char format[2] = "c";
1082     return PyArg_Parse(py_return.get(), format, (char *)ret_value);
1083   }
1084   case eScriptReturnTypeOpaqueObject: {
1085     *((PyObject **)ret_value) = py_return.release();
1086     return true;
1087   }
1088   }
1089   llvm_unreachable("Fully covered switch!");
1090 }
1091 
1092 Status ScriptInterpreterPythonImpl::ExecuteMultipleLines(
1093     const char *in_string, const ExecuteScriptOptions &options) {
1094 
1095   if (in_string == nullptr)
1096     return Status();
1097 
1098   llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
1099       io_redirect_or_error = ScriptInterpreterIORedirect::Create(
1100           options.GetEnableIO(), m_debugger, /*result=*/nullptr);
1101 
1102   if (!io_redirect_or_error)
1103     return Status(io_redirect_or_error.takeError());
1104 
1105   ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
1106 
1107   Locker locker(this,
1108                 Locker::AcquireLock | Locker::InitSession |
1109                     (options.GetSetLLDBGlobals() ? Locker::InitGlobals : 0) |
1110                     Locker::NoSTDIN,
1111                 Locker::FreeAcquiredLock | Locker::TearDownSession,
1112                 io_redirect.GetInputFile(), io_redirect.GetOutputFile(),
1113                 io_redirect.GetErrorFile());
1114 
1115   PythonModule &main_module = GetMainModule();
1116   PythonDictionary globals = main_module.GetDictionary();
1117 
1118   PythonDictionary locals = GetSessionDictionary();
1119   if (!locals.IsValid())
1120     locals = unwrapIgnoringErrors(
1121         As<PythonDictionary>(globals.GetAttribute(m_dictionary_name)));
1122   if (!locals.IsValid())
1123     locals = globals;
1124 
1125   Expected<PythonObject> return_value =
1126       runStringMultiLine(in_string, globals, locals);
1127 
1128   if (!return_value) {
1129     llvm::Error error =
1130         llvm::handleErrors(return_value.takeError(), [&](PythonException &E) {
1131           llvm::Error error = llvm::createStringError(
1132               llvm::inconvertibleErrorCode(), E.ReadBacktrace());
1133           if (!options.GetMaskoutErrors())
1134             E.Restore();
1135           return error;
1136         });
1137     return Status(std::move(error));
1138   }
1139 
1140   return Status();
1141 }
1142 
1143 void ScriptInterpreterPythonImpl::CollectDataForBreakpointCommandCallback(
1144     std::vector<std::reference_wrapper<BreakpointOptions>> &bp_options_vec,
1145     CommandReturnObject &result) {
1146   m_active_io_handler = eIOHandlerBreakpoint;
1147   m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler(
1148       "    ", *this, &bp_options_vec);
1149 }
1150 
1151 void ScriptInterpreterPythonImpl::CollectDataForWatchpointCommandCallback(
1152     WatchpointOptions *wp_options, CommandReturnObject &result) {
1153   m_active_io_handler = eIOHandlerWatchpoint;
1154   m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler(
1155       "    ", *this, wp_options);
1156 }
1157 
1158 Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallbackFunction(
1159     BreakpointOptions &bp_options, const char *function_name,
1160     StructuredData::ObjectSP extra_args_sp) {
1161   Status error;
1162   // For now just cons up a oneliner that calls the provided function.
1163   std::string function_signature = function_name;
1164 
1165   llvm::Expected<unsigned> maybe_args =
1166       GetMaxPositionalArgumentsForCallable(function_name);
1167   if (!maybe_args) {
1168     error.SetErrorStringWithFormat(
1169         "could not get num args: %s",
1170         llvm::toString(maybe_args.takeError()).c_str());
1171     return error;
1172   }
1173   size_t max_args = *maybe_args;
1174 
1175   bool uses_extra_args = false;
1176   if (max_args >= 4) {
1177     uses_extra_args = true;
1178     function_signature += "(frame, bp_loc, extra_args, internal_dict)";
1179   } else if (max_args >= 3) {
1180     if (extra_args_sp) {
1181       error.SetErrorString("cannot pass extra_args to a three argument callback"
1182                           );
1183       return error;
1184     }
1185     uses_extra_args = false;
1186     function_signature += "(frame, bp_loc, internal_dict)";
1187   } else {
1188     error.SetErrorStringWithFormat("expected 3 or 4 argument "
1189                                    "function, %s can only take %zu",
1190                                    function_name, max_args);
1191     return error;
1192   }
1193 
1194   SetBreakpointCommandCallback(bp_options, function_signature.c_str(),
1195                                extra_args_sp, uses_extra_args,
1196                                /*is_callback=*/true);
1197   return error;
1198 }
1199 
1200 Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback(
1201     BreakpointOptions &bp_options,
1202     std::unique_ptr<BreakpointOptions::CommandData> &cmd_data_up) {
1203   Status error;
1204   error = GenerateBreakpointCommandCallbackData(cmd_data_up->user_source,
1205                                                 cmd_data_up->script_source,
1206                                                 /*has_extra_args=*/false,
1207                                                 /*is_callback=*/false);
1208   if (error.Fail()) {
1209     return error;
1210   }
1211   auto baton_sp =
1212       std::make_shared<BreakpointOptions::CommandBaton>(std::move(cmd_data_up));
1213   bp_options.SetCallback(
1214       ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
1215   return error;
1216 }
1217 
1218 Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback(
1219     BreakpointOptions &bp_options, const char *command_body_text,
1220     bool is_callback) {
1221   return SetBreakpointCommandCallback(bp_options, command_body_text, {},
1222                                       /*uses_extra_args=*/false, is_callback);
1223 }
1224 
1225 // Set a Python one-liner as the callback for the breakpoint.
1226 Status ScriptInterpreterPythonImpl::SetBreakpointCommandCallback(
1227     BreakpointOptions &bp_options, const char *command_body_text,
1228     StructuredData::ObjectSP extra_args_sp, bool uses_extra_args,
1229     bool is_callback) {
1230   auto data_up = std::make_unique<CommandDataPython>(extra_args_sp);
1231   // Split the command_body_text into lines, and pass that to
1232   // GenerateBreakpointCommandCallbackData.  That will wrap the body in an
1233   // auto-generated function, and return the function name in script_source.
1234   // That is what the callback will actually invoke.
1235 
1236   data_up->user_source.SplitIntoLines(command_body_text);
1237   Status error = GenerateBreakpointCommandCallbackData(
1238       data_up->user_source, data_up->script_source, uses_extra_args,
1239       is_callback);
1240   if (error.Success()) {
1241     auto baton_sp =
1242         std::make_shared<BreakpointOptions::CommandBaton>(std::move(data_up));
1243     bp_options.SetCallback(
1244         ScriptInterpreterPythonImpl::BreakpointCallbackFunction, baton_sp);
1245     return error;
1246   }
1247   return error;
1248 }
1249 
1250 // Set a Python one-liner as the callback for the watchpoint.
1251 void ScriptInterpreterPythonImpl::SetWatchpointCommandCallback(
1252     WatchpointOptions *wp_options, const char *user_input,
1253     bool is_callback) {
1254   auto data_up = std::make_unique<WatchpointOptions::CommandData>();
1255 
1256   // It's necessary to set both user_source and script_source to the oneliner.
1257   // The former is used to generate callback description (as in watchpoint
1258   // command list) while the latter is used for Python to interpret during the
1259   // actual callback.
1260 
1261   data_up->user_source.AppendString(user_input);
1262   data_up->script_source.assign(user_input);
1263 
1264   if (GenerateWatchpointCommandCallbackData(
1265           data_up->user_source, data_up->script_source, is_callback)) {
1266     auto baton_sp =
1267         std::make_shared<WatchpointOptions::CommandBaton>(std::move(data_up));
1268     wp_options->SetCallback(
1269         ScriptInterpreterPythonImpl::WatchpointCallbackFunction, baton_sp);
1270   }
1271 }
1272 
1273 Status ScriptInterpreterPythonImpl::ExportFunctionDefinitionToInterpreter(
1274     StringList &function_def) {
1275   // Convert StringList to one long, newline delimited, const char *.
1276   std::string function_def_string(function_def.CopyList());
1277 
1278   Status error = ExecuteMultipleLines(
1279       function_def_string.c_str(),
1280       ExecuteScriptOptions().SetEnableIO(false));
1281   return error;
1282 }
1283 
1284 Status ScriptInterpreterPythonImpl::GenerateFunction(const char *signature,
1285                                                      const StringList &input,
1286                                                      bool is_callback) {
1287   Status error;
1288   int num_lines = input.GetSize();
1289   if (num_lines == 0) {
1290     error.SetErrorString("No input data.");
1291     return error;
1292   }
1293 
1294   if (!signature || *signature == 0) {
1295     error.SetErrorString("No output function name.");
1296     return error;
1297   }
1298 
1299   StreamString sstr;
1300   StringList auto_generated_function;
1301   auto_generated_function.AppendString(signature);
1302   auto_generated_function.AppendString(
1303       "    global_dict = globals()"); // Grab the global dictionary
1304   auto_generated_function.AppendString(
1305       "    new_keys = internal_dict.keys()"); // Make a list of keys in the
1306                                               // session dict
1307   auto_generated_function.AppendString(
1308       "    old_keys = global_dict.keys()"); // Save list of keys in global dict
1309   auto_generated_function.AppendString(
1310       "    global_dict.update(internal_dict)"); // Add the session dictionary
1311                                                 // to the global dictionary.
1312 
1313   if (is_callback) {
1314     // If the user input is a callback to a python function, make sure the input
1315     // is only 1 line, otherwise appending the user input would break the
1316     // generated wrapped function
1317     if (num_lines == 1) {
1318       sstr.Clear();
1319       sstr.Printf("    __return_val = %s", input.GetStringAtIndex(0));
1320       auto_generated_function.AppendString(sstr.GetData());
1321     } else {
1322       return Status("ScriptInterpreterPythonImpl::GenerateFunction(is_callback="
1323                     "true) = ERROR: python function is multiline.");
1324     }
1325   } else {
1326     auto_generated_function.AppendString(
1327         "    __return_val = None"); // Initialize user callback return value.
1328     auto_generated_function.AppendString(
1329         "    def __user_code():"); // Create a nested function that will wrap
1330                                    // the user input. This is necessary to
1331                                    // capture the return value of the user input
1332                                    // and prevent early returns.
1333     for (int i = 0; i < num_lines; ++i) {
1334       sstr.Clear();
1335       sstr.Printf("      %s", input.GetStringAtIndex(i));
1336       auto_generated_function.AppendString(sstr.GetData());
1337     }
1338     auto_generated_function.AppendString(
1339         "    __return_val = __user_code()"); //  Call user code and capture
1340                                              //  return value
1341   }
1342   auto_generated_function.AppendString(
1343       "    for key in new_keys:"); // Iterate over all the keys from session
1344                                    // dict
1345   auto_generated_function.AppendString(
1346       "        internal_dict[key] = global_dict[key]"); // Update session dict
1347                                                         // values
1348   auto_generated_function.AppendString(
1349       "        if key not in old_keys:"); // If key was not originally in
1350                                           // global dict
1351   auto_generated_function.AppendString(
1352       "            del global_dict[key]"); //  ...then remove key/value from
1353                                            //  global dict
1354   auto_generated_function.AppendString(
1355       "    return __return_val"); //  Return the user callback return value.
1356 
1357   // Verify that the results are valid Python.
1358   error = ExportFunctionDefinitionToInterpreter(auto_generated_function);
1359 
1360   return error;
1361 }
1362 
1363 bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
1364     StringList &user_input, std::string &output, const void *name_token) {
1365   static uint32_t num_created_functions = 0;
1366   user_input.RemoveBlankLines();
1367   StreamString sstr;
1368 
1369   // Check to see if we have any data; if not, just return.
1370   if (user_input.GetSize() == 0)
1371     return false;
1372 
1373   // Take what the user wrote, wrap it all up inside one big auto-generated
1374   // Python function, passing in the ValueObject as parameter to the function.
1375 
1376   std::string auto_generated_function_name(
1377       GenerateUniqueName("lldb_autogen_python_type_print_func",
1378                          num_created_functions, name_token));
1379   sstr.Printf("def %s (valobj, internal_dict):",
1380               auto_generated_function_name.c_str());
1381 
1382   if (!GenerateFunction(sstr.GetData(), user_input, /*is_callback=*/false)
1383            .Success())
1384     return false;
1385 
1386   // Store the name of the auto-generated function to be called.
1387   output.assign(auto_generated_function_name);
1388   return true;
1389 }
1390 
1391 bool ScriptInterpreterPythonImpl::GenerateScriptAliasFunction(
1392     StringList &user_input, std::string &output) {
1393   static uint32_t num_created_functions = 0;
1394   user_input.RemoveBlankLines();
1395   StreamString sstr;
1396 
1397   // Check to see if we have any data; if not, just return.
1398   if (user_input.GetSize() == 0)
1399     return false;
1400 
1401   std::string auto_generated_function_name(GenerateUniqueName(
1402       "lldb_autogen_python_cmd_alias_func", num_created_functions));
1403 
1404   sstr.Printf("def %s (debugger, args, exe_ctx, result, internal_dict):",
1405               auto_generated_function_name.c_str());
1406 
1407   if (!GenerateFunction(sstr.GetData(), user_input, /*is_callback=*/true)
1408            .Success())
1409     return false;
1410 
1411   // Store the name of the auto-generated function to be called.
1412   output.assign(auto_generated_function_name);
1413   return true;
1414 }
1415 
1416 bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass(
1417     StringList &user_input, std::string &output, const void *name_token) {
1418   static uint32_t num_created_classes = 0;
1419   user_input.RemoveBlankLines();
1420   int num_lines = user_input.GetSize();
1421   StreamString sstr;
1422 
1423   // Check to see if we have any data; if not, just return.
1424   if (user_input.GetSize() == 0)
1425     return false;
1426 
1427   // Wrap all user input into a Python class
1428 
1429   std::string auto_generated_class_name(GenerateUniqueName(
1430       "lldb_autogen_python_type_synth_class", num_created_classes, name_token));
1431 
1432   StringList auto_generated_class;
1433 
1434   // Create the function name & definition string.
1435 
1436   sstr.Printf("class %s:", auto_generated_class_name.c_str());
1437   auto_generated_class.AppendString(sstr.GetString());
1438 
1439   // Wrap everything up inside the class, increasing the indentation. we don't
1440   // need to play any fancy indentation tricks here because there is no
1441   // surrounding code whose indentation we need to honor
1442   for (int i = 0; i < num_lines; ++i) {
1443     sstr.Clear();
1444     sstr.Printf("     %s", user_input.GetStringAtIndex(i));
1445     auto_generated_class.AppendString(sstr.GetString());
1446   }
1447 
1448   // Verify that the results are valid Python. (even though the method is
1449   // ExportFunctionDefinitionToInterpreter, a class will actually be exported)
1450   // (TODO: rename that method to ExportDefinitionToInterpreter)
1451   if (!ExportFunctionDefinitionToInterpreter(auto_generated_class).Success())
1452     return false;
1453 
1454   // Store the name of the auto-generated class
1455 
1456   output.assign(auto_generated_class_name);
1457   return true;
1458 }
1459 
1460 StructuredData::GenericSP
1461 ScriptInterpreterPythonImpl::CreateFrameRecognizer(const char *class_name) {
1462   if (class_name == nullptr || class_name[0] == '\0')
1463     return StructuredData::GenericSP();
1464 
1465   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1466   PythonObject ret_val = SWIGBridge::LLDBSWIGPython_CreateFrameRecognizer(
1467       class_name, m_dictionary_name.c_str());
1468 
1469   return StructuredData::GenericSP(
1470       new StructuredPythonObject(std::move(ret_val)));
1471 }
1472 
1473 lldb::ValueObjectListSP ScriptInterpreterPythonImpl::GetRecognizedArguments(
1474     const StructuredData::ObjectSP &os_plugin_object_sp,
1475     lldb::StackFrameSP frame_sp) {
1476   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1477 
1478   if (!os_plugin_object_sp)
1479     return ValueObjectListSP();
1480 
1481   StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1482   if (!generic)
1483     return nullptr;
1484 
1485   PythonObject implementor(PyRefType::Borrowed,
1486                            (PyObject *)generic->GetValue());
1487 
1488   if (!implementor.IsAllocated())
1489     return ValueObjectListSP();
1490 
1491   PythonObject py_return(PyRefType::Owned,
1492                          SWIGBridge::LLDBSwigPython_GetRecognizedArguments(
1493                              implementor.get(), frame_sp));
1494 
1495   // if it fails, print the error but otherwise go on
1496   if (PyErr_Occurred()) {
1497     PyErr_Print();
1498     PyErr_Clear();
1499   }
1500   if (py_return.get()) {
1501     PythonList result_list(PyRefType::Borrowed, py_return.get());
1502     ValueObjectListSP result = ValueObjectListSP(new ValueObjectList());
1503     for (size_t i = 0; i < result_list.GetSize(); i++) {
1504       PyObject *item = result_list.GetItemAtIndex(i).get();
1505       lldb::SBValue *sb_value_ptr =
1506           (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(item);
1507       auto valobj_sp =
1508           SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
1509       if (valobj_sp)
1510         result->Append(valobj_sp);
1511     }
1512     return result;
1513   }
1514   return ValueObjectListSP();
1515 }
1516 
1517 ScriptedProcessInterfaceUP
1518 ScriptInterpreterPythonImpl::CreateScriptedProcessInterface() {
1519   return std::make_unique<ScriptedProcessPythonInterface>(*this);
1520 }
1521 
1522 StructuredData::ObjectSP
1523 ScriptInterpreterPythonImpl::CreateStructuredDataFromScriptObject(
1524     ScriptObject obj) {
1525   void *ptr = const_cast<void *>(obj.GetPointer());
1526   PythonObject py_obj(PyRefType::Borrowed, static_cast<PyObject *>(ptr));
1527   if (!py_obj.IsValid() || py_obj.IsNone())
1528     return {};
1529   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1530   return py_obj.CreateStructuredObject();
1531 }
1532 
1533 StructuredData::GenericSP
1534 ScriptInterpreterPythonImpl::OSPlugin_CreatePluginObject(
1535     const char *class_name, lldb::ProcessSP process_sp) {
1536   if (class_name == nullptr || class_name[0] == '\0')
1537     return StructuredData::GenericSP();
1538 
1539   if (!process_sp)
1540     return StructuredData::GenericSP();
1541 
1542   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1543   PythonObject ret_val = SWIGBridge::LLDBSWIGPythonCreateOSPlugin(
1544       class_name, m_dictionary_name.c_str(), process_sp);
1545 
1546   return StructuredData::GenericSP(
1547       new StructuredPythonObject(std::move(ret_val)));
1548 }
1549 
1550 StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_RegisterInfo(
1551     StructuredData::ObjectSP os_plugin_object_sp) {
1552   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1553 
1554   if (!os_plugin_object_sp)
1555     return {};
1556 
1557   StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1558   if (!generic)
1559     return {};
1560 
1561   PythonObject implementor(PyRefType::Borrowed,
1562                            (PyObject *)generic->GetValue());
1563 
1564   if (!implementor.IsAllocated())
1565     return {};
1566 
1567   llvm::Expected<PythonObject> expected_py_return =
1568       implementor.CallMethod("get_register_info");
1569 
1570   if (!expected_py_return) {
1571     llvm::consumeError(expected_py_return.takeError());
1572     return {};
1573   }
1574 
1575   PythonObject py_return = std::move(expected_py_return.get());
1576 
1577   if (py_return.get()) {
1578     PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
1579     return result_dict.CreateStructuredDictionary();
1580   }
1581   return StructuredData::DictionarySP();
1582 }
1583 
1584 StructuredData::ArraySP ScriptInterpreterPythonImpl::OSPlugin_ThreadsInfo(
1585     StructuredData::ObjectSP os_plugin_object_sp) {
1586   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1587   if (!os_plugin_object_sp)
1588     return {};
1589 
1590   StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1591   if (!generic)
1592     return {};
1593 
1594   PythonObject implementor(PyRefType::Borrowed,
1595                            (PyObject *)generic->GetValue());
1596 
1597   if (!implementor.IsAllocated())
1598     return {};
1599 
1600   llvm::Expected<PythonObject> expected_py_return =
1601       implementor.CallMethod("get_thread_info");
1602 
1603   if (!expected_py_return) {
1604     llvm::consumeError(expected_py_return.takeError());
1605     return {};
1606   }
1607 
1608   PythonObject py_return = std::move(expected_py_return.get());
1609 
1610   if (py_return.get()) {
1611     PythonList result_list(PyRefType::Borrowed, py_return.get());
1612     return result_list.CreateStructuredArray();
1613   }
1614   return StructuredData::ArraySP();
1615 }
1616 
1617 StructuredData::StringSP
1618 ScriptInterpreterPythonImpl::OSPlugin_RegisterContextData(
1619     StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid) {
1620   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1621 
1622   if (!os_plugin_object_sp)
1623     return {};
1624 
1625   StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1626   if (!generic)
1627     return {};
1628   PythonObject implementor(PyRefType::Borrowed,
1629                            (PyObject *)generic->GetValue());
1630 
1631   if (!implementor.IsAllocated())
1632     return {};
1633 
1634   llvm::Expected<PythonObject> expected_py_return =
1635       implementor.CallMethod("get_register_data", tid);
1636 
1637   if (!expected_py_return) {
1638     llvm::consumeError(expected_py_return.takeError());
1639     return {};
1640   }
1641 
1642   PythonObject py_return = std::move(expected_py_return.get());
1643 
1644   if (py_return.get()) {
1645     PythonBytes result(PyRefType::Borrowed, py_return.get());
1646     return result.CreateStructuredString();
1647   }
1648   return {};
1649 }
1650 
1651 StructuredData::DictionarySP ScriptInterpreterPythonImpl::OSPlugin_CreateThread(
1652     StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid,
1653     lldb::addr_t context) {
1654   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
1655 
1656   if (!os_plugin_object_sp)
1657     return {};
1658 
1659   StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1660   if (!generic)
1661     return {};
1662 
1663   PythonObject implementor(PyRefType::Borrowed,
1664                            (PyObject *)generic->GetValue());
1665 
1666   if (!implementor.IsAllocated())
1667     return {};
1668 
1669   llvm::Expected<PythonObject> expected_py_return =
1670       implementor.CallMethod("create_thread", tid, context);
1671 
1672   if (!expected_py_return) {
1673     llvm::consumeError(expected_py_return.takeError());
1674     return {};
1675   }
1676 
1677   PythonObject py_return = std::move(expected_py_return.get());
1678 
1679   if (py_return.get()) {
1680     PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
1681     return result_dict.CreateStructuredDictionary();
1682   }
1683   return StructuredData::DictionarySP();
1684 }
1685 
1686 StructuredData::ObjectSP ScriptInterpreterPythonImpl::CreateScriptedThreadPlan(
1687     const char *class_name, const StructuredDataImpl &args_data,
1688     std::string &error_str, lldb::ThreadPlanSP thread_plan_sp) {
1689   if (class_name == nullptr || class_name[0] == '\0')
1690     return StructuredData::ObjectSP();
1691 
1692   if (!thread_plan_sp.get())
1693     return {};
1694 
1695   Debugger &debugger = thread_plan_sp->GetTarget().GetDebugger();
1696   ScriptInterpreterPythonImpl *python_interpreter =
1697       GetPythonInterpreter(debugger);
1698 
1699   if (!python_interpreter)
1700     return {};
1701 
1702   Locker py_lock(this,
1703                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1704   PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateScriptedThreadPlan(
1705       class_name, python_interpreter->m_dictionary_name.c_str(), args_data,
1706       error_str, thread_plan_sp);
1707   if (!ret_val)
1708     return {};
1709 
1710   return StructuredData::ObjectSP(
1711       new StructuredPythonObject(std::move(ret_val)));
1712 }
1713 
1714 bool ScriptInterpreterPythonImpl::ScriptedThreadPlanExplainsStop(
1715     StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) {
1716   bool explains_stop = true;
1717   StructuredData::Generic *generic = nullptr;
1718   if (implementor_sp)
1719     generic = implementor_sp->GetAsGeneric();
1720   if (generic) {
1721     Locker py_lock(this,
1722                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1723     explains_stop = SWIGBridge::LLDBSWIGPythonCallThreadPlan(
1724         generic->GetValue(), "explains_stop", event, script_error);
1725     if (script_error)
1726       return true;
1727   }
1728   return explains_stop;
1729 }
1730 
1731 bool ScriptInterpreterPythonImpl::ScriptedThreadPlanShouldStop(
1732     StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error) {
1733   bool should_stop = true;
1734   StructuredData::Generic *generic = nullptr;
1735   if (implementor_sp)
1736     generic = implementor_sp->GetAsGeneric();
1737   if (generic) {
1738     Locker py_lock(this,
1739                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1740     should_stop = SWIGBridge::LLDBSWIGPythonCallThreadPlan(
1741         generic->GetValue(), "should_stop", event, script_error);
1742     if (script_error)
1743       return true;
1744   }
1745   return should_stop;
1746 }
1747 
1748 bool ScriptInterpreterPythonImpl::ScriptedThreadPlanIsStale(
1749     StructuredData::ObjectSP implementor_sp, bool &script_error) {
1750   bool is_stale = true;
1751   StructuredData::Generic *generic = nullptr;
1752   if (implementor_sp)
1753     generic = implementor_sp->GetAsGeneric();
1754   if (generic) {
1755     Locker py_lock(this,
1756                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1757     is_stale = SWIGBridge::LLDBSWIGPythonCallThreadPlan(
1758         generic->GetValue(), "is_stale", (Event *)nullptr, script_error);
1759     if (script_error)
1760       return true;
1761   }
1762   return is_stale;
1763 }
1764 
1765 lldb::StateType ScriptInterpreterPythonImpl::ScriptedThreadPlanGetRunState(
1766     StructuredData::ObjectSP implementor_sp, bool &script_error) {
1767   bool should_step = false;
1768   StructuredData::Generic *generic = nullptr;
1769   if (implementor_sp)
1770     generic = implementor_sp->GetAsGeneric();
1771   if (generic) {
1772     Locker py_lock(this,
1773                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1774     should_step = SWIGBridge::LLDBSWIGPythonCallThreadPlan(
1775         generic->GetValue(), "should_step", (Event *)nullptr, script_error);
1776     if (script_error)
1777       should_step = true;
1778   }
1779   if (should_step)
1780     return lldb::eStateStepping;
1781   return lldb::eStateRunning;
1782 }
1783 
1784 bool
1785 ScriptInterpreterPythonImpl::ScriptedThreadPlanGetStopDescription(
1786     StructuredData::ObjectSP implementor_sp, lldb_private::Stream *stream,
1787     bool &script_error) {
1788   StructuredData::Generic *generic = nullptr;
1789   if (implementor_sp)
1790     generic = implementor_sp->GetAsGeneric();
1791   if (!generic) {
1792     script_error = true;
1793     return false;
1794   }
1795   Locker py_lock(this,
1796                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1797   return SWIGBridge::LLDBSWIGPythonCallThreadPlan(
1798       generic->GetValue(), "stop_description", stream, script_error);
1799 }
1800 
1801 
1802 StructuredData::GenericSP
1803 ScriptInterpreterPythonImpl::CreateScriptedBreakpointResolver(
1804     const char *class_name, const StructuredDataImpl &args_data,
1805     lldb::BreakpointSP &bkpt_sp) {
1806 
1807   if (class_name == nullptr || class_name[0] == '\0')
1808     return StructuredData::GenericSP();
1809 
1810   if (!bkpt_sp.get())
1811     return StructuredData::GenericSP();
1812 
1813   Debugger &debugger = bkpt_sp->GetTarget().GetDebugger();
1814   ScriptInterpreterPythonImpl *python_interpreter =
1815       GetPythonInterpreter(debugger);
1816 
1817   if (!python_interpreter)
1818     return StructuredData::GenericSP();
1819 
1820   Locker py_lock(this,
1821                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1822 
1823   PythonObject ret_val =
1824       SWIGBridge::LLDBSwigPythonCreateScriptedBreakpointResolver(
1825           class_name, python_interpreter->m_dictionary_name.c_str(), args_data,
1826           bkpt_sp);
1827 
1828   return StructuredData::GenericSP(
1829       new StructuredPythonObject(std::move(ret_val)));
1830 }
1831 
1832 bool ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchCallback(
1833     StructuredData::GenericSP implementor_sp, SymbolContext *sym_ctx) {
1834   bool should_continue = false;
1835 
1836   if (implementor_sp) {
1837     Locker py_lock(this,
1838                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1839     should_continue = SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
1840         implementor_sp->GetValue(), "__callback__", sym_ctx);
1841     if (PyErr_Occurred()) {
1842       PyErr_Print();
1843       PyErr_Clear();
1844     }
1845   }
1846   return should_continue;
1847 }
1848 
1849 lldb::SearchDepth
1850 ScriptInterpreterPythonImpl::ScriptedBreakpointResolverSearchDepth(
1851     StructuredData::GenericSP implementor_sp) {
1852   int depth_as_int = lldb::eSearchDepthModule;
1853   if (implementor_sp) {
1854     Locker py_lock(this,
1855                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1856     depth_as_int = SWIGBridge::LLDBSwigPythonCallBreakpointResolver(
1857         implementor_sp->GetValue(), "__get_depth__", nullptr);
1858     if (PyErr_Occurred()) {
1859       PyErr_Print();
1860       PyErr_Clear();
1861     }
1862   }
1863   if (depth_as_int == lldb::eSearchDepthInvalid)
1864     return lldb::eSearchDepthModule;
1865 
1866   if (depth_as_int <= lldb::kLastSearchDepthKind)
1867     return (lldb::SearchDepth)depth_as_int;
1868   return lldb::eSearchDepthModule;
1869 }
1870 
1871 StructuredData::GenericSP ScriptInterpreterPythonImpl::CreateScriptedStopHook(
1872     TargetSP target_sp, const char *class_name,
1873     const StructuredDataImpl &args_data, Status &error) {
1874 
1875   if (!target_sp) {
1876     error.SetErrorString("No target for scripted stop-hook.");
1877     return StructuredData::GenericSP();
1878   }
1879 
1880   if (class_name == nullptr || class_name[0] == '\0') {
1881     error.SetErrorString("No class name for scripted stop-hook.");
1882     return StructuredData::GenericSP();
1883   }
1884 
1885   ScriptInterpreterPythonImpl *python_interpreter =
1886       GetPythonInterpreter(m_debugger);
1887 
1888   if (!python_interpreter) {
1889     error.SetErrorString("No script interpreter for scripted stop-hook.");
1890     return StructuredData::GenericSP();
1891   }
1892 
1893   Locker py_lock(this,
1894                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1895 
1896   PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateScriptedStopHook(
1897       target_sp, class_name, python_interpreter->m_dictionary_name.c_str(),
1898       args_data, error);
1899 
1900   return StructuredData::GenericSP(
1901       new StructuredPythonObject(std::move(ret_val)));
1902 }
1903 
1904 bool ScriptInterpreterPythonImpl::ScriptedStopHookHandleStop(
1905     StructuredData::GenericSP implementor_sp, ExecutionContext &exc_ctx,
1906     lldb::StreamSP stream_sp) {
1907   assert(implementor_sp &&
1908          "can't call a stop hook with an invalid implementor");
1909   assert(stream_sp && "can't call a stop hook with an invalid stream");
1910 
1911   Locker py_lock(this,
1912                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1913 
1914   lldb::ExecutionContextRefSP exc_ctx_ref_sp(new ExecutionContextRef(exc_ctx));
1915 
1916   bool ret_val = SWIGBridge::LLDBSwigPythonStopHookCallHandleStop(
1917       implementor_sp->GetValue(), exc_ctx_ref_sp, stream_sp);
1918   return ret_val;
1919 }
1920 
1921 StructuredData::ObjectSP
1922 ScriptInterpreterPythonImpl::LoadPluginModule(const FileSpec &file_spec,
1923                                               lldb_private::Status &error) {
1924   if (!FileSystem::Instance().Exists(file_spec)) {
1925     error.SetErrorString("no such file");
1926     return StructuredData::ObjectSP();
1927   }
1928 
1929   StructuredData::ObjectSP module_sp;
1930 
1931   LoadScriptOptions load_script_options =
1932       LoadScriptOptions().SetInitSession(true).SetSilent(false);
1933   if (LoadScriptingModule(file_spec.GetPath().c_str(), load_script_options,
1934                           error, &module_sp))
1935     return module_sp;
1936 
1937   return StructuredData::ObjectSP();
1938 }
1939 
1940 StructuredData::DictionarySP ScriptInterpreterPythonImpl::GetDynamicSettings(
1941     StructuredData::ObjectSP plugin_module_sp, Target *target,
1942     const char *setting_name, lldb_private::Status &error) {
1943   if (!plugin_module_sp || !target || !setting_name || !setting_name[0])
1944     return StructuredData::DictionarySP();
1945   StructuredData::Generic *generic = plugin_module_sp->GetAsGeneric();
1946   if (!generic)
1947     return StructuredData::DictionarySP();
1948 
1949   Locker py_lock(this,
1950                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1951   TargetSP target_sp(target->shared_from_this());
1952 
1953   auto setting = (PyObject *)SWIGBridge::LLDBSWIGPython_GetDynamicSetting(
1954       generic->GetValue(), setting_name, target_sp);
1955 
1956   if (!setting)
1957     return StructuredData::DictionarySP();
1958 
1959   PythonDictionary py_dict =
1960       unwrapIgnoringErrors(As<PythonDictionary>(Take<PythonObject>(setting)));
1961 
1962   if (!py_dict)
1963     return StructuredData::DictionarySP();
1964 
1965   return py_dict.CreateStructuredDictionary();
1966 }
1967 
1968 StructuredData::ObjectSP
1969 ScriptInterpreterPythonImpl::CreateSyntheticScriptedProvider(
1970     const char *class_name, lldb::ValueObjectSP valobj) {
1971   if (class_name == nullptr || class_name[0] == '\0')
1972     return StructuredData::ObjectSP();
1973 
1974   if (!valobj.get())
1975     return StructuredData::ObjectSP();
1976 
1977   ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
1978   Target *target = exe_ctx.GetTargetPtr();
1979 
1980   if (!target)
1981     return StructuredData::ObjectSP();
1982 
1983   Debugger &debugger = target->GetDebugger();
1984   ScriptInterpreterPythonImpl *python_interpreter =
1985       GetPythonInterpreter(debugger);
1986 
1987   if (!python_interpreter)
1988     return StructuredData::ObjectSP();
1989 
1990   Locker py_lock(this,
1991                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1992   PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateSyntheticProvider(
1993       class_name, python_interpreter->m_dictionary_name.c_str(), valobj);
1994 
1995   return StructuredData::ObjectSP(
1996       new StructuredPythonObject(std::move(ret_val)));
1997 }
1998 
1999 StructuredData::GenericSP
2000 ScriptInterpreterPythonImpl::CreateScriptCommandObject(const char *class_name) {
2001   DebuggerSP debugger_sp(m_debugger.shared_from_this());
2002 
2003   if (class_name == nullptr || class_name[0] == '\0')
2004     return StructuredData::GenericSP();
2005 
2006   if (!debugger_sp.get())
2007     return StructuredData::GenericSP();
2008 
2009   Locker py_lock(this,
2010                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2011   PythonObject ret_val = SWIGBridge::LLDBSwigPythonCreateCommandObject(
2012       class_name, m_dictionary_name.c_str(), debugger_sp);
2013 
2014   if (ret_val.IsValid())
2015     return StructuredData::GenericSP(
2016         new StructuredPythonObject(std::move(ret_val)));
2017   else
2018     return {};
2019 }
2020 
2021 bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
2022     const char *oneliner, std::string &output, const void *name_token) {
2023   StringList input;
2024   input.SplitIntoLines(oneliner, strlen(oneliner));
2025   return GenerateTypeScriptFunction(input, output, name_token);
2026 }
2027 
2028 bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass(
2029     const char *oneliner, std::string &output, const void *name_token) {
2030   StringList input;
2031   input.SplitIntoLines(oneliner, strlen(oneliner));
2032   return GenerateTypeSynthClass(input, output, name_token);
2033 }
2034 
2035 Status ScriptInterpreterPythonImpl::GenerateBreakpointCommandCallbackData(
2036     StringList &user_input, std::string &output, bool has_extra_args,
2037     bool is_callback) {
2038   static uint32_t num_created_functions = 0;
2039   user_input.RemoveBlankLines();
2040   StreamString sstr;
2041   Status error;
2042   if (user_input.GetSize() == 0) {
2043     error.SetErrorString("No input data.");
2044     return error;
2045   }
2046 
2047   std::string auto_generated_function_name(GenerateUniqueName(
2048       "lldb_autogen_python_bp_callback_func_", num_created_functions));
2049   if (has_extra_args)
2050     sstr.Printf("def %s (frame, bp_loc, extra_args, internal_dict):",
2051                 auto_generated_function_name.c_str());
2052   else
2053     sstr.Printf("def %s (frame, bp_loc, internal_dict):",
2054                 auto_generated_function_name.c_str());
2055 
2056   error = GenerateFunction(sstr.GetData(), user_input, is_callback);
2057   if (!error.Success())
2058     return error;
2059 
2060   // Store the name of the auto-generated function to be called.
2061   output.assign(auto_generated_function_name);
2062   return error;
2063 }
2064 
2065 bool ScriptInterpreterPythonImpl::GenerateWatchpointCommandCallbackData(
2066     StringList &user_input, std::string &output, bool is_callback) {
2067   static uint32_t num_created_functions = 0;
2068   user_input.RemoveBlankLines();
2069   StreamString sstr;
2070 
2071   if (user_input.GetSize() == 0)
2072     return false;
2073 
2074   std::string auto_generated_function_name(GenerateUniqueName(
2075       "lldb_autogen_python_wp_callback_func_", num_created_functions));
2076   sstr.Printf("def %s (frame, wp, internal_dict):",
2077               auto_generated_function_name.c_str());
2078 
2079   if (!GenerateFunction(sstr.GetData(), user_input, is_callback).Success())
2080     return false;
2081 
2082   // Store the name of the auto-generated function to be called.
2083   output.assign(auto_generated_function_name);
2084   return true;
2085 }
2086 
2087 bool ScriptInterpreterPythonImpl::GetScriptedSummary(
2088     const char *python_function_name, lldb::ValueObjectSP valobj,
2089     StructuredData::ObjectSP &callee_wrapper_sp,
2090     const TypeSummaryOptions &options, std::string &retval) {
2091 
2092   LLDB_SCOPED_TIMER();
2093 
2094   if (!valobj.get()) {
2095     retval.assign("<no object>");
2096     return false;
2097   }
2098 
2099   void *old_callee = nullptr;
2100   StructuredData::Generic *generic = nullptr;
2101   if (callee_wrapper_sp) {
2102     generic = callee_wrapper_sp->GetAsGeneric();
2103     if (generic)
2104       old_callee = generic->GetValue();
2105   }
2106   void *new_callee = old_callee;
2107 
2108   bool ret_val;
2109   if (python_function_name && *python_function_name) {
2110     {
2111       Locker py_lock(this, Locker::AcquireLock | Locker::InitSession |
2112                                Locker::NoSTDIN);
2113       {
2114         TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options));
2115 
2116         static Timer::Category func_cat("LLDBSwigPythonCallTypeScript");
2117         Timer scoped_timer(func_cat, "LLDBSwigPythonCallTypeScript");
2118         ret_val = SWIGBridge::LLDBSwigPythonCallTypeScript(
2119             python_function_name, GetSessionDictionary().get(), valobj,
2120             &new_callee, options_sp, retval);
2121       }
2122     }
2123   } else {
2124     retval.assign("<no function name>");
2125     return false;
2126   }
2127 
2128   if (new_callee && old_callee != new_callee) {
2129     Locker py_lock(this,
2130                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2131     callee_wrapper_sp = std::make_shared<StructuredPythonObject>(
2132         PythonObject(PyRefType::Borrowed, static_cast<PyObject *>(new_callee)));
2133   }
2134 
2135   return ret_val;
2136 }
2137 
2138 bool ScriptInterpreterPythonImpl::FormatterCallbackFunction(
2139     const char *python_function_name, TypeImplSP type_impl_sp) {
2140   Locker py_lock(this,
2141                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2142   return SWIGBridge::LLDBSwigPythonFormatterCallbackFunction(
2143       python_function_name, m_dictionary_name.c_str(), type_impl_sp);
2144 }
2145 
2146 bool ScriptInterpreterPythonImpl::BreakpointCallbackFunction(
2147     void *baton, StoppointCallbackContext *context, user_id_t break_id,
2148     user_id_t break_loc_id) {
2149   CommandDataPython *bp_option_data = (CommandDataPython *)baton;
2150   const char *python_function_name = bp_option_data->script_source.c_str();
2151 
2152   if (!context)
2153     return true;
2154 
2155   ExecutionContext exe_ctx(context->exe_ctx_ref);
2156   Target *target = exe_ctx.GetTargetPtr();
2157 
2158   if (!target)
2159     return true;
2160 
2161   Debugger &debugger = target->GetDebugger();
2162   ScriptInterpreterPythonImpl *python_interpreter =
2163       GetPythonInterpreter(debugger);
2164 
2165   if (!python_interpreter)
2166     return true;
2167 
2168   if (python_function_name && python_function_name[0]) {
2169     const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
2170     BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
2171     if (breakpoint_sp) {
2172       const BreakpointLocationSP bp_loc_sp(
2173           breakpoint_sp->FindLocationByID(break_loc_id));
2174 
2175       if (stop_frame_sp && bp_loc_sp) {
2176         bool ret_val = true;
2177         {
2178           Locker py_lock(python_interpreter, Locker::AcquireLock |
2179                                                  Locker::InitSession |
2180                                                  Locker::NoSTDIN);
2181           Expected<bool> maybe_ret_val =
2182               SWIGBridge::LLDBSwigPythonBreakpointCallbackFunction(
2183                   python_function_name,
2184                   python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
2185                   bp_loc_sp, bp_option_data->m_extra_args);
2186 
2187           if (!maybe_ret_val) {
2188 
2189             llvm::handleAllErrors(
2190                 maybe_ret_val.takeError(),
2191                 [&](PythonException &E) {
2192                   debugger.GetErrorStream() << E.ReadBacktrace();
2193                 },
2194                 [&](const llvm::ErrorInfoBase &E) {
2195                   debugger.GetErrorStream() << E.message();
2196                 });
2197 
2198           } else {
2199             ret_val = maybe_ret_val.get();
2200           }
2201         }
2202         return ret_val;
2203       }
2204     }
2205   }
2206   // We currently always true so we stop in case anything goes wrong when
2207   // trying to call the script function
2208   return true;
2209 }
2210 
2211 bool ScriptInterpreterPythonImpl::WatchpointCallbackFunction(
2212     void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
2213   WatchpointOptions::CommandData *wp_option_data =
2214       (WatchpointOptions::CommandData *)baton;
2215   const char *python_function_name = wp_option_data->script_source.c_str();
2216 
2217   if (!context)
2218     return true;
2219 
2220   ExecutionContext exe_ctx(context->exe_ctx_ref);
2221   Target *target = exe_ctx.GetTargetPtr();
2222 
2223   if (!target)
2224     return true;
2225 
2226   Debugger &debugger = target->GetDebugger();
2227   ScriptInterpreterPythonImpl *python_interpreter =
2228       GetPythonInterpreter(debugger);
2229 
2230   if (!python_interpreter)
2231     return true;
2232 
2233   if (python_function_name && python_function_name[0]) {
2234     const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
2235     WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
2236     if (wp_sp) {
2237       if (stop_frame_sp && wp_sp) {
2238         bool ret_val = true;
2239         {
2240           Locker py_lock(python_interpreter, Locker::AcquireLock |
2241                                                  Locker::InitSession |
2242                                                  Locker::NoSTDIN);
2243           ret_val = SWIGBridge::LLDBSwigPythonWatchpointCallbackFunction(
2244               python_function_name,
2245               python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
2246               wp_sp);
2247         }
2248         return ret_val;
2249       }
2250     }
2251   }
2252   // We currently always true so we stop in case anything goes wrong when
2253   // trying to call the script function
2254   return true;
2255 }
2256 
2257 size_t ScriptInterpreterPythonImpl::CalculateNumChildren(
2258     const StructuredData::ObjectSP &implementor_sp, uint32_t max) {
2259   if (!implementor_sp)
2260     return 0;
2261   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2262   if (!generic)
2263     return 0;
2264   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2265   if (!implementor)
2266     return 0;
2267 
2268   size_t ret_val = 0;
2269 
2270   {
2271     Locker py_lock(this,
2272                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2273     ret_val = SWIGBridge::LLDBSwigPython_CalculateNumChildren(implementor, max);
2274   }
2275 
2276   return ret_val;
2277 }
2278 
2279 lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetChildAtIndex(
2280     const StructuredData::ObjectSP &implementor_sp, uint32_t idx) {
2281   if (!implementor_sp)
2282     return lldb::ValueObjectSP();
2283 
2284   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2285   if (!generic)
2286     return lldb::ValueObjectSP();
2287   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2288   if (!implementor)
2289     return lldb::ValueObjectSP();
2290 
2291   lldb::ValueObjectSP ret_val;
2292   {
2293     Locker py_lock(this,
2294                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2295     PyObject *child_ptr =
2296         SWIGBridge::LLDBSwigPython_GetChildAtIndex(implementor, idx);
2297     if (child_ptr != nullptr && child_ptr != Py_None) {
2298       lldb::SBValue *sb_value_ptr =
2299           (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
2300       if (sb_value_ptr == nullptr)
2301         Py_XDECREF(child_ptr);
2302       else
2303         ret_val = SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
2304             sb_value_ptr);
2305     } else {
2306       Py_XDECREF(child_ptr);
2307     }
2308   }
2309 
2310   return ret_val;
2311 }
2312 
2313 int ScriptInterpreterPythonImpl::GetIndexOfChildWithName(
2314     const StructuredData::ObjectSP &implementor_sp, const char *child_name) {
2315   if (!implementor_sp)
2316     return UINT32_MAX;
2317 
2318   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2319   if (!generic)
2320     return UINT32_MAX;
2321   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2322   if (!implementor)
2323     return UINT32_MAX;
2324 
2325   int ret_val = UINT32_MAX;
2326 
2327   {
2328     Locker py_lock(this,
2329                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2330     ret_val = SWIGBridge::LLDBSwigPython_GetIndexOfChildWithName(implementor, child_name);
2331   }
2332 
2333   return ret_val;
2334 }
2335 
2336 bool ScriptInterpreterPythonImpl::UpdateSynthProviderInstance(
2337     const StructuredData::ObjectSP &implementor_sp) {
2338   bool ret_val = false;
2339 
2340   if (!implementor_sp)
2341     return ret_val;
2342 
2343   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2344   if (!generic)
2345     return ret_val;
2346   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2347   if (!implementor)
2348     return ret_val;
2349 
2350   {
2351     Locker py_lock(this,
2352                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2353     ret_val =
2354         SWIGBridge::LLDBSwigPython_UpdateSynthProviderInstance(implementor);
2355   }
2356 
2357   return ret_val;
2358 }
2359 
2360 bool ScriptInterpreterPythonImpl::MightHaveChildrenSynthProviderInstance(
2361     const StructuredData::ObjectSP &implementor_sp) {
2362   bool ret_val = false;
2363 
2364   if (!implementor_sp)
2365     return ret_val;
2366 
2367   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2368   if (!generic)
2369     return ret_val;
2370   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2371   if (!implementor)
2372     return ret_val;
2373 
2374   {
2375     Locker py_lock(this,
2376                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2377     ret_val = SWIGBridge::LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
2378         implementor);
2379   }
2380 
2381   return ret_val;
2382 }
2383 
2384 lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetSyntheticValue(
2385     const StructuredData::ObjectSP &implementor_sp) {
2386   lldb::ValueObjectSP ret_val(nullptr);
2387 
2388   if (!implementor_sp)
2389     return ret_val;
2390 
2391   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2392   if (!generic)
2393     return ret_val;
2394   auto *implementor = static_cast<PyObject *>(generic->GetValue());
2395   if (!implementor)
2396     return ret_val;
2397 
2398   {
2399     Locker py_lock(this,
2400                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2401     PyObject *child_ptr =
2402         SWIGBridge::LLDBSwigPython_GetValueSynthProviderInstance(implementor);
2403     if (child_ptr != nullptr && child_ptr != Py_None) {
2404       lldb::SBValue *sb_value_ptr =
2405           (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
2406       if (sb_value_ptr == nullptr)
2407         Py_XDECREF(child_ptr);
2408       else
2409         ret_val = SWIGBridge::LLDBSWIGPython_GetValueObjectSPFromSBValue(
2410             sb_value_ptr);
2411     } else {
2412       Py_XDECREF(child_ptr);
2413     }
2414   }
2415 
2416   return ret_val;
2417 }
2418 
2419 ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName(
2420     const StructuredData::ObjectSP &implementor_sp) {
2421   Locker py_lock(this,
2422                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2423 
2424   if (!implementor_sp)
2425     return {};
2426 
2427   StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2428   if (!generic)
2429     return {};
2430 
2431   PythonObject implementor(PyRefType::Borrowed,
2432                            (PyObject *)generic->GetValue());
2433   if (!implementor.IsAllocated())
2434     return {};
2435 
2436   llvm::Expected<PythonObject> expected_py_return =
2437       implementor.CallMethod("get_type_name");
2438 
2439   if (!expected_py_return) {
2440     llvm::consumeError(expected_py_return.takeError());
2441     return {};
2442   }
2443 
2444   PythonObject py_return = std::move(expected_py_return.get());
2445 
2446   ConstString ret_val;
2447   bool got_string = false;
2448   std::string buffer;
2449 
2450   if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
2451     PythonString py_string(PyRefType::Borrowed, py_return.get());
2452     llvm::StringRef return_data(py_string.GetString());
2453     if (!return_data.empty()) {
2454       buffer.assign(return_data.data(), return_data.size());
2455       got_string = true;
2456     }
2457   }
2458 
2459   if (got_string)
2460     ret_val.SetCStringWithLength(buffer.c_str(), buffer.size());
2461 
2462   return ret_val;
2463 }
2464 
2465 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2466     const char *impl_function, Process *process, std::string &output,
2467     Status &error) {
2468   bool ret_val;
2469   if (!process) {
2470     error.SetErrorString("no process");
2471     return false;
2472   }
2473   if (!impl_function || !impl_function[0]) {
2474     error.SetErrorString("no function to execute");
2475     return false;
2476   }
2477 
2478   {
2479     Locker py_lock(this,
2480                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2481     ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordProcess(
2482         impl_function, m_dictionary_name.c_str(), process->shared_from_this(),
2483         output);
2484     if (!ret_val)
2485       error.SetErrorString("python script evaluation failed");
2486   }
2487   return ret_val;
2488 }
2489 
2490 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2491     const char *impl_function, Thread *thread, std::string &output,
2492     Status &error) {
2493   if (!thread) {
2494     error.SetErrorString("no thread");
2495     return false;
2496   }
2497   if (!impl_function || !impl_function[0]) {
2498     error.SetErrorString("no function to execute");
2499     return false;
2500   }
2501 
2502   Locker py_lock(this,
2503                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2504   if (std::optional<std::string> result =
2505           SWIGBridge::LLDBSWIGPythonRunScriptKeywordThread(
2506               impl_function, m_dictionary_name.c_str(),
2507               thread->shared_from_this())) {
2508     output = std::move(*result);
2509     return true;
2510   }
2511   error.SetErrorString("python script evaluation failed");
2512   return false;
2513 }
2514 
2515 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2516     const char *impl_function, Target *target, std::string &output,
2517     Status &error) {
2518   bool ret_val;
2519   if (!target) {
2520     error.SetErrorString("no thread");
2521     return false;
2522   }
2523   if (!impl_function || !impl_function[0]) {
2524     error.SetErrorString("no function to execute");
2525     return false;
2526   }
2527 
2528   {
2529     TargetSP target_sp(target->shared_from_this());
2530     Locker py_lock(this,
2531                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2532     ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordTarget(
2533         impl_function, m_dictionary_name.c_str(), target_sp, output);
2534     if (!ret_val)
2535       error.SetErrorString("python script evaluation failed");
2536   }
2537   return ret_val;
2538 }
2539 
2540 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2541     const char *impl_function, StackFrame *frame, std::string &output,
2542     Status &error) {
2543   if (!frame) {
2544     error.SetErrorString("no frame");
2545     return false;
2546   }
2547   if (!impl_function || !impl_function[0]) {
2548     error.SetErrorString("no function to execute");
2549     return false;
2550   }
2551 
2552   Locker py_lock(this,
2553                  Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2554   if (std::optional<std::string> result =
2555           SWIGBridge::LLDBSWIGPythonRunScriptKeywordFrame(
2556               impl_function, m_dictionary_name.c_str(),
2557               frame->shared_from_this())) {
2558     output = std::move(*result);
2559     return true;
2560   }
2561   error.SetErrorString("python script evaluation failed");
2562   return false;
2563 }
2564 
2565 bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2566     const char *impl_function, ValueObject *value, std::string &output,
2567     Status &error) {
2568   bool ret_val;
2569   if (!value) {
2570     error.SetErrorString("no value");
2571     return false;
2572   }
2573   if (!impl_function || !impl_function[0]) {
2574     error.SetErrorString("no function to execute");
2575     return false;
2576   }
2577 
2578   {
2579     Locker py_lock(this,
2580                    Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2581     ret_val = SWIGBridge::LLDBSWIGPythonRunScriptKeywordValue(
2582         impl_function, m_dictionary_name.c_str(), value->GetSP(), output);
2583     if (!ret_val)
2584       error.SetErrorString("python script evaluation failed");
2585   }
2586   return ret_val;
2587 }
2588 
2589 uint64_t replace_all(std::string &str, const std::string &oldStr,
2590                      const std::string &newStr) {
2591   size_t pos = 0;
2592   uint64_t matches = 0;
2593   while ((pos = str.find(oldStr, pos)) != std::string::npos) {
2594     matches++;
2595     str.replace(pos, oldStr.length(), newStr);
2596     pos += newStr.length();
2597   }
2598   return matches;
2599 }
2600 
2601 bool ScriptInterpreterPythonImpl::LoadScriptingModule(
2602     const char *pathname, const LoadScriptOptions &options,
2603     lldb_private::Status &error, StructuredData::ObjectSP *module_sp,
2604     FileSpec extra_search_dir) {
2605   namespace fs = llvm::sys::fs;
2606   namespace path = llvm::sys::path;
2607 
2608   ExecuteScriptOptions exc_options = ExecuteScriptOptions()
2609                                          .SetEnableIO(!options.GetSilent())
2610                                          .SetSetLLDBGlobals(false);
2611 
2612   if (!pathname || !pathname[0]) {
2613     error.SetErrorString("empty path");
2614     return false;
2615   }
2616 
2617   llvm::Expected<std::unique_ptr<ScriptInterpreterIORedirect>>
2618       io_redirect_or_error = ScriptInterpreterIORedirect::Create(
2619           exc_options.GetEnableIO(), m_debugger, /*result=*/nullptr);
2620 
2621   if (!io_redirect_or_error) {
2622     error = io_redirect_or_error.takeError();
2623     return false;
2624   }
2625 
2626   ScriptInterpreterIORedirect &io_redirect = **io_redirect_or_error;
2627 
2628   // Before executing Python code, lock the GIL.
2629   Locker py_lock(this,
2630                  Locker::AcquireLock |
2631                      (options.GetInitSession() ? Locker::InitSession : 0) |
2632                      Locker::NoSTDIN,
2633                  Locker::FreeAcquiredLock |
2634                      (options.GetInitSession() ? Locker::TearDownSession : 0),
2635                  io_redirect.GetInputFile(), io_redirect.GetOutputFile(),
2636                  io_redirect.GetErrorFile());
2637 
2638   auto ExtendSysPath = [&](std::string directory) -> llvm::Error {
2639     if (directory.empty()) {
2640       return llvm::make_error<llvm::StringError>(
2641           "invalid directory name", llvm::inconvertibleErrorCode());
2642     }
2643 
2644     replace_all(directory, "\\", "\\\\");
2645     replace_all(directory, "'", "\\'");
2646 
2647     // Make sure that Python has "directory" in the search path.
2648     StreamString command_stream;
2649     command_stream.Printf("if not (sys.path.__contains__('%s')):\n    "
2650                           "sys.path.insert(1,'%s');\n\n",
2651                           directory.c_str(), directory.c_str());
2652     bool syspath_retval =
2653         ExecuteMultipleLines(command_stream.GetData(), exc_options).Success();
2654     if (!syspath_retval) {
2655       return llvm::make_error<llvm::StringError>(
2656           "Python sys.path handling failed", llvm::inconvertibleErrorCode());
2657     }
2658 
2659     return llvm::Error::success();
2660   };
2661 
2662   std::string module_name(pathname);
2663   bool possible_package = false;
2664 
2665   if (extra_search_dir) {
2666     if (llvm::Error e = ExtendSysPath(extra_search_dir.GetPath())) {
2667       error = std::move(e);
2668       return false;
2669     }
2670   } else {
2671     FileSpec module_file(pathname);
2672     FileSystem::Instance().Resolve(module_file);
2673 
2674     fs::file_status st;
2675     std::error_code ec = status(module_file.GetPath(), st);
2676 
2677     if (ec || st.type() == fs::file_type::status_error ||
2678         st.type() == fs::file_type::type_unknown ||
2679         st.type() == fs::file_type::file_not_found) {
2680       // if not a valid file of any sort, check if it might be a filename still
2681       // dot can't be used but / and \ can, and if either is found, reject
2682       if (strchr(pathname, '\\') || strchr(pathname, '/')) {
2683         error.SetErrorStringWithFormatv("invalid pathname '{0}'", pathname);
2684         return false;
2685       }
2686       // Not a filename, probably a package of some sort, let it go through.
2687       possible_package = true;
2688     } else if (is_directory(st) || is_regular_file(st)) {
2689       if (module_file.GetDirectory().IsEmpty()) {
2690         error.SetErrorStringWithFormatv("invalid directory name '{0}'", pathname);
2691         return false;
2692       }
2693       if (llvm::Error e =
2694               ExtendSysPath(module_file.GetDirectory().GetCString())) {
2695         error = std::move(e);
2696         return false;
2697       }
2698       module_name = module_file.GetFilename().GetCString();
2699     } else {
2700       error.SetErrorString("no known way to import this module specification");
2701       return false;
2702     }
2703   }
2704 
2705   // Strip .py or .pyc extension
2706   llvm::StringRef extension = llvm::sys::path::extension(module_name);
2707   if (!extension.empty()) {
2708     if (extension == ".py")
2709       module_name.resize(module_name.length() - 3);
2710     else if (extension == ".pyc")
2711       module_name.resize(module_name.length() - 4);
2712   }
2713 
2714   if (!possible_package && module_name.find('.') != llvm::StringRef::npos) {
2715     error.SetErrorStringWithFormat(
2716         "Python does not allow dots in module names: %s", module_name.c_str());
2717     return false;
2718   }
2719 
2720   if (module_name.find('-') != llvm::StringRef::npos) {
2721     error.SetErrorStringWithFormat(
2722         "Python discourages dashes in module names: %s", module_name.c_str());
2723     return false;
2724   }
2725 
2726   // Check if the module is already imported.
2727   StreamString command_stream;
2728   command_stream.Clear();
2729   command_stream.Printf("sys.modules.__contains__('%s')", module_name.c_str());
2730   bool does_contain = false;
2731   // This call will succeed if the module was ever imported in any Debugger in
2732   // the lifetime of the process in which this LLDB framework is living.
2733   const bool does_contain_executed = ExecuteOneLineWithReturn(
2734       command_stream.GetData(),
2735       ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain, exc_options);
2736 
2737   const bool was_imported_globally = does_contain_executed && does_contain;
2738   const bool was_imported_locally =
2739       GetSessionDictionary()
2740           .GetItemForKey(PythonString(module_name))
2741           .IsAllocated();
2742 
2743   // now actually do the import
2744   command_stream.Clear();
2745 
2746   if (was_imported_globally || was_imported_locally) {
2747     if (!was_imported_locally)
2748       command_stream.Printf("import %s ; reload_module(%s)",
2749                             module_name.c_str(), module_name.c_str());
2750     else
2751       command_stream.Printf("reload_module(%s)", module_name.c_str());
2752   } else
2753     command_stream.Printf("import %s", module_name.c_str());
2754 
2755   error = ExecuteMultipleLines(command_stream.GetData(), exc_options);
2756   if (error.Fail())
2757     return false;
2758 
2759   // if we are here, everything worked
2760   // call __lldb_init_module(debugger,dict)
2761   if (!SWIGBridge::LLDBSwigPythonCallModuleInit(
2762           module_name.c_str(), m_dictionary_name.c_str(),
2763           m_debugger.shared_from_this())) {
2764     error.SetErrorString("calling __lldb_init_module failed");
2765     return false;
2766   }
2767 
2768   if (module_sp) {
2769     // everything went just great, now set the module object
2770     command_stream.Clear();
2771     command_stream.Printf("%s", module_name.c_str());
2772     void *module_pyobj = nullptr;
2773     if (ExecuteOneLineWithReturn(
2774             command_stream.GetData(),
2775             ScriptInterpreter::eScriptReturnTypeOpaqueObject, &module_pyobj,
2776             exc_options) &&
2777         module_pyobj)
2778       *module_sp = std::make_shared<StructuredPythonObject>(PythonObject(
2779           PyRefType::Owned, static_cast<PyObject *>(module_pyobj)));
2780   }
2781 
2782   return true;
2783 }
2784 
2785 bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {
2786   if (!word || !word[0])
2787     return false;
2788 
2789   llvm::StringRef word_sr(word);
2790 
2791   // filter out a few characters that would just confuse us and that are
2792   // clearly not keyword material anyway
2793   if (word_sr.find('"') != llvm::StringRef::npos ||
2794       word_sr.find('\'') != llvm::StringRef::npos)
2795     return false;
2796 
2797   StreamString command_stream;
2798   command_stream.Printf("keyword.iskeyword('%s')", word);
2799   bool result;
2800   ExecuteScriptOptions options;
2801   options.SetEnableIO(false);
2802   options.SetMaskoutErrors(true);
2803   options.SetSetLLDBGlobals(false);
2804   if (ExecuteOneLineWithReturn(command_stream.GetData(),
2805                                ScriptInterpreter::eScriptReturnTypeBool,
2806                                &result, options))
2807     return result;
2808   return false;
2809 }
2810 
2811 ScriptInterpreterPythonImpl::SynchronicityHandler::SynchronicityHandler(
2812     lldb::DebuggerSP debugger_sp, ScriptedCommandSynchronicity synchro)
2813     : m_debugger_sp(debugger_sp), m_synch_wanted(synchro),
2814       m_old_asynch(debugger_sp->GetAsyncExecution()) {
2815   if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2816     m_debugger_sp->SetAsyncExecution(false);
2817   else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2818     m_debugger_sp->SetAsyncExecution(true);
2819 }
2820 
2821 ScriptInterpreterPythonImpl::SynchronicityHandler::~SynchronicityHandler() {
2822   if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2823     m_debugger_sp->SetAsyncExecution(m_old_asynch);
2824 }
2825 
2826 bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
2827     const char *impl_function, llvm::StringRef args,
2828     ScriptedCommandSynchronicity synchronicity,
2829     lldb_private::CommandReturnObject &cmd_retobj, Status &error,
2830     const lldb_private::ExecutionContext &exe_ctx) {
2831   if (!impl_function) {
2832     error.SetErrorString("no function to execute");
2833     return false;
2834   }
2835 
2836   lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
2837   lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2838 
2839   if (!debugger_sp.get()) {
2840     error.SetErrorString("invalid Debugger pointer");
2841     return false;
2842   }
2843 
2844   bool ret_val = false;
2845 
2846   std::string err_msg;
2847 
2848   {
2849     Locker py_lock(this,
2850                    Locker::AcquireLock | Locker::InitSession |
2851                        (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2852                    Locker::FreeLock | Locker::TearDownSession);
2853 
2854     SynchronicityHandler synch_handler(debugger_sp, synchronicity);
2855 
2856     std::string args_str = args.str();
2857     ret_val = SWIGBridge::LLDBSwigPythonCallCommand(
2858         impl_function, m_dictionary_name.c_str(), debugger_sp, args_str.c_str(),
2859         cmd_retobj, exe_ctx_ref_sp);
2860   }
2861 
2862   if (!ret_val)
2863     error.SetErrorString("unable to execute script function");
2864   else if (cmd_retobj.GetStatus() == eReturnStatusFailed)
2865     return false;
2866 
2867   error.Clear();
2868   return ret_val;
2869 }
2870 
2871 bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
2872     StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
2873     ScriptedCommandSynchronicity synchronicity,
2874     lldb_private::CommandReturnObject &cmd_retobj, Status &error,
2875     const lldb_private::ExecutionContext &exe_ctx) {
2876   if (!impl_obj_sp || !impl_obj_sp->IsValid()) {
2877     error.SetErrorString("no function to execute");
2878     return false;
2879   }
2880 
2881   lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
2882   lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2883 
2884   if (!debugger_sp.get()) {
2885     error.SetErrorString("invalid Debugger pointer");
2886     return false;
2887   }
2888 
2889   bool ret_val = false;
2890 
2891   std::string err_msg;
2892 
2893   {
2894     Locker py_lock(this,
2895                    Locker::AcquireLock | Locker::InitSession |
2896                        (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2897                    Locker::FreeLock | Locker::TearDownSession);
2898 
2899     SynchronicityHandler synch_handler(debugger_sp, synchronicity);
2900 
2901     std::string args_str = args.str();
2902     ret_val = SWIGBridge::LLDBSwigPythonCallCommandObject(
2903         static_cast<PyObject *>(impl_obj_sp->GetValue()), debugger_sp,
2904         args_str.c_str(), cmd_retobj, exe_ctx_ref_sp);
2905   }
2906 
2907   if (!ret_val)
2908     error.SetErrorString("unable to execute script function");
2909   else if (cmd_retobj.GetStatus() == eReturnStatusFailed)
2910     return false;
2911 
2912   error.Clear();
2913   return ret_val;
2914 }
2915 
2916 /// In Python, a special attribute __doc__ contains the docstring for an object
2917 /// (function, method, class, ...) if any is defined Otherwise, the attribute's
2918 /// value is None.
2919 bool ScriptInterpreterPythonImpl::GetDocumentationForItem(const char *item,
2920                                                           std::string &dest) {
2921   dest.clear();
2922 
2923   if (!item || !*item)
2924     return false;
2925 
2926   std::string command(item);
2927   command += ".__doc__";
2928 
2929   // Python is going to point this to valid data if ExecuteOneLineWithReturn
2930   // returns successfully.
2931   char *result_ptr = nullptr;
2932 
2933   if (ExecuteOneLineWithReturn(
2934           command, ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
2935           &result_ptr,
2936           ExecuteScriptOptions().SetEnableIO(false))) {
2937     if (result_ptr)
2938       dest.assign(result_ptr);
2939     return true;
2940   }
2941 
2942   StreamString str_stream;
2943   str_stream << "Function " << item
2944              << " was not found. Containing module might be missing.";
2945   dest = std::string(str_stream.GetString());
2946 
2947   return false;
2948 }
2949 
2950 bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject(
2951     StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
2952   dest.clear();
2953 
2954   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
2955 
2956   if (!cmd_obj_sp)
2957     return false;
2958 
2959   PythonObject implementor(PyRefType::Borrowed,
2960                            (PyObject *)cmd_obj_sp->GetValue());
2961 
2962   if (!implementor.IsAllocated())
2963     return false;
2964 
2965   llvm::Expected<PythonObject> expected_py_return =
2966       implementor.CallMethod("get_short_help");
2967 
2968   if (!expected_py_return) {
2969     llvm::consumeError(expected_py_return.takeError());
2970     return false;
2971   }
2972 
2973   PythonObject py_return = std::move(expected_py_return.get());
2974 
2975   if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
2976     PythonString py_string(PyRefType::Borrowed, py_return.get());
2977     llvm::StringRef return_data(py_string.GetString());
2978     dest.assign(return_data.data(), return_data.size());
2979     return true;
2980   }
2981 
2982   return false;
2983 }
2984 
2985 uint32_t ScriptInterpreterPythonImpl::GetFlagsForCommandObject(
2986     StructuredData::GenericSP cmd_obj_sp) {
2987   uint32_t result = 0;
2988 
2989   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
2990 
2991   static char callee_name[] = "get_flags";
2992 
2993   if (!cmd_obj_sp)
2994     return result;
2995 
2996   PythonObject implementor(PyRefType::Borrowed,
2997                            (PyObject *)cmd_obj_sp->GetValue());
2998 
2999   if (!implementor.IsAllocated())
3000     return result;
3001 
3002   PythonObject pmeth(PyRefType::Owned,
3003                      PyObject_GetAttrString(implementor.get(), callee_name));
3004 
3005   if (PyErr_Occurred())
3006     PyErr_Clear();
3007 
3008   if (!pmeth.IsAllocated())
3009     return result;
3010 
3011   if (PyCallable_Check(pmeth.get()) == 0) {
3012     if (PyErr_Occurred())
3013       PyErr_Clear();
3014     return result;
3015   }
3016 
3017   if (PyErr_Occurred())
3018     PyErr_Clear();
3019 
3020   long long py_return = unwrapOrSetPythonException(
3021       As<long long>(implementor.CallMethod(callee_name)));
3022 
3023   // if it fails, print the error but otherwise go on
3024   if (PyErr_Occurred()) {
3025     PyErr_Print();
3026     PyErr_Clear();
3027   } else {
3028     result = py_return;
3029   }
3030 
3031   return result;
3032 }
3033 
3034 bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject(
3035     StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
3036   dest.clear();
3037 
3038   Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
3039 
3040   if (!cmd_obj_sp)
3041     return false;
3042 
3043   PythonObject implementor(PyRefType::Borrowed,
3044                            (PyObject *)cmd_obj_sp->GetValue());
3045 
3046   if (!implementor.IsAllocated())
3047     return false;
3048 
3049   llvm::Expected<PythonObject> expected_py_return =
3050       implementor.CallMethod("get_long_help");
3051 
3052   if (!expected_py_return) {
3053     llvm::consumeError(expected_py_return.takeError());
3054     return false;
3055   }
3056 
3057   PythonObject py_return = std::move(expected_py_return.get());
3058 
3059   bool got_string = false;
3060   if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
3061     PythonString str(PyRefType::Borrowed, py_return.get());
3062     llvm::StringRef str_data(str.GetString());
3063     dest.assign(str_data.data(), str_data.size());
3064     got_string = true;
3065   }
3066 
3067   return got_string;
3068 }
3069 
3070 std::unique_ptr<ScriptInterpreterLocker>
3071 ScriptInterpreterPythonImpl::AcquireInterpreterLock() {
3072   std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(
3073       this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN,
3074       Locker::FreeLock | Locker::TearDownSession));
3075   return py_lock;
3076 }
3077 
3078 void ScriptInterpreterPythonImpl::Initialize() {
3079   LLDB_SCOPED_TIMER();
3080 
3081   // RAII-based initialization which correctly handles multiple-initialization,
3082   // version- specific differences among Python 2 and Python 3, and saving and
3083   // restoring various other pieces of state that can get mucked with during
3084   // initialization.
3085   InitializePythonRAII initialize_guard;
3086 
3087   LLDBSwigPyInit();
3088 
3089   // Update the path python uses to search for modules to include the current
3090   // directory.
3091 
3092   PyRun_SimpleString("import sys");
3093   AddToSysPath(AddLocation::End, ".");
3094 
3095   // Don't denormalize paths when calling file_spec.GetPath().  On platforms
3096   // that use a backslash as the path separator, this will result in executing
3097   // python code containing paths with unescaped backslashes.  But Python also
3098   // accepts forward slashes, so to make life easier we just use that.
3099   if (FileSpec file_spec = GetPythonDir())
3100     AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3101   if (FileSpec file_spec = HostInfo::GetShlibDir())
3102     AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3103 
3104   PyRun_SimpleString("sys.dont_write_bytecode = 1; import "
3105                      "lldb.embedded_interpreter; from "
3106                      "lldb.embedded_interpreter import run_python_interpreter; "
3107                      "from lldb.embedded_interpreter import run_one_line");
3108 
3109 #if LLDB_USE_PYTHON_SET_INTERRUPT
3110   // Python will not just overwrite its internal SIGINT handler but also the
3111   // one from the process. Backup the current SIGINT handler to prevent that
3112   // Python deletes it.
3113   RestoreSignalHandlerScope save_sigint(SIGINT);
3114 
3115   // Setup a default SIGINT signal handler that works the same way as the
3116   // normal Python REPL signal handler which raises a KeyboardInterrupt.
3117   // Also make sure to not pollute the user's REPL with the signal module nor
3118   // our utility function.
3119   PyRun_SimpleString("def lldb_setup_sigint_handler():\n"
3120                      "  import signal;\n"
3121                      "  def signal_handler(sig, frame):\n"
3122                      "    raise KeyboardInterrupt()\n"
3123                      "  signal.signal(signal.SIGINT, signal_handler);\n"
3124                      "lldb_setup_sigint_handler();\n"
3125                      "del lldb_setup_sigint_handler\n");
3126 #endif
3127 }
3128 
3129 void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
3130                                                std::string path) {
3131   std::string path_copy;
3132 
3133   std::string statement;
3134   if (location == AddLocation::Beginning) {
3135     statement.assign("sys.path.insert(0,\"");
3136     statement.append(path);
3137     statement.append("\")");
3138   } else {
3139     statement.assign("sys.path.append(\"");
3140     statement.append(path);
3141     statement.append("\")");
3142   }
3143   PyRun_SimpleString(statement.c_str());
3144 }
3145 
3146 // We are intentionally NOT calling Py_Finalize here (this would be the logical
3147 // place to call it).  Calling Py_Finalize here causes test suite runs to seg
3148 // fault:  The test suite runs in Python.  It registers SBDebugger::Terminate to
3149 // be called 'at_exit'.  When the test suite Python harness finishes up, it
3150 // calls Py_Finalize, which calls all the 'at_exit' registered functions.
3151 // SBDebugger::Terminate calls Debugger::Terminate, which calls lldb::Terminate,
3152 // which calls ScriptInterpreter::Terminate, which calls
3153 // ScriptInterpreterPythonImpl::Terminate.  So if we call Py_Finalize here, we
3154 // end up with Py_Finalize being called from within Py_Finalize, which results
3155 // in a seg fault. Since this function only gets called when lldb is shutting
3156 // down and going away anyway, the fact that we don't actually call Py_Finalize
3157 // should not cause any problems (everything should shut down/go away anyway
3158 // when the process exits).
3159 //
3160 // void ScriptInterpreterPythonImpl::Terminate() { Py_Finalize (); }
3161 
3162 #endif
3163