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