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