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