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