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