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