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