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