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