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