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