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