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