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