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