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