ScriptInterpreterPython.cpp revision 360784
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
54using namespace lldb;
55using namespace lldb_private;
56using namespace lldb_private::python;
57using llvm::Expected;
58
59// Defined in the SWIG source file
60#if PY_MAJOR_VERSION >= 3
61extern "C" PyObject *PyInit__lldb(void);
62
63#define LLDBSwigPyInit PyInit__lldb
64
65#else
66extern "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
86extern "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
97extern "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
101extern "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
106extern "C" void *
107LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
108                                      const char *session_dictionary_name,
109                                      const lldb::ValueObjectSP &valobj_sp);
110
111extern "C" void *
112LLDBSwigPythonCreateCommandObject(const char *python_class_name,
113                                  const char *session_dictionary_name,
114                                  const lldb::DebuggerSP debugger_sp);
115
116extern "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
122extern "C" bool LLDBSWIGPythonCallThreadPlan(void *implementor,
123                                             const char *method_name,
124                                             Event *event_sp, bool &got_error);
125
126extern "C" void *LLDBSwigPythonCreateScriptedBreakpointResolver(
127    const char *python_class_name, const char *session_dictionary_name,
128    lldb_private::StructuredDataImpl *args, lldb::BreakpointSP &bkpt_sp);
129
130extern "C" unsigned int
131LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
132                                     lldb_private::SymbolContext *sym_ctx);
133
134extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor,
135                                                      uint32_t max);
136
137extern "C" void *LLDBSwigPython_GetChildAtIndex(void *implementor,
138                                                uint32_t idx);
139
140extern "C" int LLDBSwigPython_GetIndexOfChildWithName(void *implementor,
141                                                      const char *child_name);
142
143extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data);
144
145extern lldb::ValueObjectSP
146LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
147
148extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance(void *implementor);
149
150extern "C" bool
151LLDBSwigPython_MightHaveChildrenSynthProviderInstance(void *implementor);
152
153extern "C" void *
154LLDBSwigPython_GetValueSynthProviderInstance(void *implementor);
155
156extern "C" bool
157LLDBSwigPythonCallCommand(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
163extern "C" bool
164LLDBSwigPythonCallCommandObject(void *implementor, lldb::DebuggerSP &debugger,
165                                const char *args,
166                                lldb_private::CommandReturnObject &cmd_retobj,
167                                lldb::ExecutionContextRefSP exe_ctx_ref_sp);
168
169extern "C" bool
170LLDBSwigPythonCallModuleInit(const char *python_module_name,
171                             const char *session_dictionary_name,
172                             lldb::DebuggerSP &debugger);
173
174extern "C" void *
175LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
176                             const char *session_dictionary_name,
177                             const lldb::ProcessSP &process_sp);
178
179extern "C" void *
180LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
181                                     const char *session_dictionary_name);
182
183extern "C" void *
184LLDBSwigPython_GetRecognizedArguments(void *implementor,
185                                      const lldb::StackFrameSP &frame_sp);
186
187extern "C" bool LLDBSWIGPythonRunScriptKeywordProcess(
188    const char *python_function_name, const char *session_dictionary_name,
189    lldb::ProcessSP &process, std::string &output);
190
191extern "C" bool LLDBSWIGPythonRunScriptKeywordThread(
192    const char *python_function_name, const char *session_dictionary_name,
193    lldb::ThreadSP &thread, std::string &output);
194
195extern "C" bool LLDBSWIGPythonRunScriptKeywordTarget(
196    const char *python_function_name, const char *session_dictionary_name,
197    lldb::TargetSP &target, std::string &output);
198
199extern "C" bool LLDBSWIGPythonRunScriptKeywordFrame(
200    const char *python_function_name, const char *session_dictionary_name,
201    lldb::StackFrameSP &frame, std::string &output);
202
203extern "C" bool LLDBSWIGPythonRunScriptKeywordValue(
204    const char *python_function_name, const char *session_dictionary_name,
205    lldb::ValueObjectSP &value, std::string &output);
206
207extern "C" void *
208LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
209                                 const lldb::TargetSP &target_sp);
210
211static bool g_initialized = false;
212
213namespace {
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
221struct InitializePythonRAII {
222public:
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
276private:
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
333void 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
349void 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
364FileSpec 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
383lldb_private::ConstString ScriptInterpreterPython::GetPluginNameStatic() {
384  static ConstString g_name("script-python");
385  return g_name;
386}
387
388const char *ScriptInterpreterPython::GetPluginDescriptionStatic() {
389  return "Embedded Python interpreter";
390}
391
392void 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
403void ScriptInterpreterPython::Terminate() {}
404
405ScriptInterpreterPythonImpl::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
420bool 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
436bool 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
444bool 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
453bool ScriptInterpreterPythonImpl::Locker::DoTearDownSession() {
454  if (!m_python_interpreter)
455    return false;
456  m_python_interpreter->LeaveSession();
457  return true;
458}
459
460ScriptInterpreterPythonImpl::Locker::~Locker() {
461  if (m_teardown_session)
462    DoTearDownSession();
463  DoFreeLock();
464}
465
466ScriptInterpreterPythonImpl::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
522ScriptInterpreterPythonImpl::~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
533lldb_private::ConstString ScriptInterpreterPythonImpl::GetPluginName() {
534  return GetPluginNameStatic();
535}
536
537uint32_t ScriptInterpreterPythonImpl::GetPluginVersion() { return 1; }
538
539void 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.
548def 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
568void 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
631lldb::ScriptInterpreterSP
632ScriptInterpreterPythonImpl::CreateInstance(Debugger &debugger) {
633  return std::make_shared<ScriptInterpreterPythonImpl>(debugger);
634}
635
636void 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
675bool 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
702bool 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
785PythonModule &ScriptInterpreterPythonImpl::GetMainModule() {
786  if (!m_main_module.IsValid())
787    m_main_module = unwrapIgnoringErrors(PythonModule::Import("__main__"));
788  return m_main_module;
789}
790
791PythonDictionary &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
809PythonDictionary &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
817llvm::Expected<unsigned>
818ScriptInterpreterPythonImpl::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
843static 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
859bool 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
880static 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
889bool 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
1050void 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
1071bool 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
1095bool 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
1206Status 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
1246void 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, &bp_options_vec);
1252}
1253
1254void ScriptInterpreterPythonImpl::CollectDataForWatchpointCommandCallback(
1255    WatchpointOptions *wp_options, CommandReturnObject &result) {
1256  m_active_io_handler = eIOHandlerWatchpoint;
1257  m_debugger.GetCommandInterpreter().GetPythonCommandsFromIOHandler(
1258      "    ", *this, wp_options);
1259}
1260
1261Status 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
1303Status 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
1320Status 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.
1326Status 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.
1351void 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
1374Status 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
1385Status 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
1442bool 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
1469bool 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
1493bool 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
1537StructuredData::GenericSP
1538ScriptInterpreterPythonImpl::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
1554lldb::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
1597StructuredData::GenericSP
1598ScriptInterpreterPythonImpl::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
1618StructuredData::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
1673StructuredData::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
1735template <typename T> const char *GetPythonValueFormatString(T t);
1736template <> const char *GetPythonValueFormatString(char *) { return "s"; }
1737template <> const char *GetPythonValueFormatString(char) { return "b"; }
1738template <> const char *GetPythonValueFormatString(unsigned char) {
1739  return "B";
1740}
1741template <> const char *GetPythonValueFormatString(short) { return "h"; }
1742template <> const char *GetPythonValueFormatString(unsigned short) {
1743  return "H";
1744}
1745template <> const char *GetPythonValueFormatString(int) { return "i"; }
1746template <> const char *GetPythonValueFormatString(unsigned int) { return "I"; }
1747template <> const char *GetPythonValueFormatString(long) { return "l"; }
1748template <> const char *GetPythonValueFormatString(unsigned long) {
1749  return "k";
1750}
1751template <> const char *GetPythonValueFormatString(long long) { return "L"; }
1752template <> const char *GetPythonValueFormatString(unsigned long long) {
1753  return "K";
1754}
1755template <> const char *GetPythonValueFormatString(float t) { return "f"; }
1756template <> const char *GetPythonValueFormatString(double t) { return "d"; }
1757
1758StructuredData::StringSP
1759ScriptInterpreterPythonImpl::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
1815StructuredData::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
1874StructuredData::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
1906bool 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
1923bool 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
1940bool 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
1957lldb::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
1977StructuredData::GenericSP
1978ScriptInterpreterPythonImpl::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
2010bool 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
2027lldb::SearchDepth
2028ScriptInterpreterPythonImpl::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
2050StructuredData::ObjectSP
2051ScriptInterpreterPythonImpl::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, error, &module_sp))
2061    return module_sp;
2062
2063  return StructuredData::ObjectSP();
2064}
2065
2066StructuredData::DictionarySP ScriptInterpreterPythonImpl::GetDynamicSettings(
2067    StructuredData::ObjectSP plugin_module_sp, Target *target,
2068    const char *setting_name, lldb_private::Status &error) {
2069  if (!plugin_module_sp || !target || !setting_name || !setting_name[0])
2070    return StructuredData::DictionarySP();
2071  StructuredData::Generic *generic = plugin_module_sp->GetAsGeneric();
2072  if (!generic)
2073    return StructuredData::DictionarySP();
2074
2075  Locker py_lock(this,
2076                 Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2077  TargetSP target_sp(target->shared_from_this());
2078
2079  auto setting = (PyObject *)LLDBSWIGPython_GetDynamicSetting(
2080      generic->GetValue(), setting_name, target_sp);
2081
2082  if (!setting)
2083    return StructuredData::DictionarySP();
2084
2085  PythonDictionary py_dict =
2086      unwrapIgnoringErrors(As<PythonDictionary>(Take<PythonObject>(setting)));
2087
2088  if (!py_dict)
2089    return StructuredData::DictionarySP();
2090
2091  return py_dict.CreateStructuredDictionary();
2092}
2093
2094StructuredData::ObjectSP
2095ScriptInterpreterPythonImpl::CreateSyntheticScriptedProvider(
2096    const char *class_name, lldb::ValueObjectSP valobj) {
2097  if (class_name == nullptr || class_name[0] == '\0')
2098    return StructuredData::ObjectSP();
2099
2100  if (!valobj.get())
2101    return StructuredData::ObjectSP();
2102
2103  ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
2104  Target *target = exe_ctx.GetTargetPtr();
2105
2106  if (!target)
2107    return StructuredData::ObjectSP();
2108
2109  Debugger &debugger = target->GetDebugger();
2110  ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
2111  ScriptInterpreterPythonImpl *python_interpreter =
2112      (ScriptInterpreterPythonImpl *)script_interpreter;
2113
2114  if (!script_interpreter)
2115    return StructuredData::ObjectSP();
2116
2117  void *ret_val = nullptr;
2118
2119  {
2120    Locker py_lock(this,
2121                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2122    ret_val = LLDBSwigPythonCreateSyntheticProvider(
2123        class_name, python_interpreter->m_dictionary_name.c_str(), valobj);
2124  }
2125
2126  return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
2127}
2128
2129StructuredData::GenericSP
2130ScriptInterpreterPythonImpl::CreateScriptCommandObject(const char *class_name) {
2131  DebuggerSP debugger_sp(m_debugger.shared_from_this());
2132
2133  if (class_name == nullptr || class_name[0] == '\0')
2134    return StructuredData::GenericSP();
2135
2136  if (!debugger_sp.get())
2137    return StructuredData::GenericSP();
2138
2139  void *ret_val;
2140
2141  {
2142    Locker py_lock(this,
2143                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2144    ret_val = LLDBSwigPythonCreateCommandObject(
2145        class_name, m_dictionary_name.c_str(), debugger_sp);
2146  }
2147
2148  return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
2149}
2150
2151bool ScriptInterpreterPythonImpl::GenerateTypeScriptFunction(
2152    const char *oneliner, std::string &output, const void *name_token) {
2153  StringList input;
2154  input.SplitIntoLines(oneliner, strlen(oneliner));
2155  return GenerateTypeScriptFunction(input, output, name_token);
2156}
2157
2158bool ScriptInterpreterPythonImpl::GenerateTypeSynthClass(
2159    const char *oneliner, std::string &output, const void *name_token) {
2160  StringList input;
2161  input.SplitIntoLines(oneliner, strlen(oneliner));
2162  return GenerateTypeSynthClass(input, output, name_token);
2163}
2164
2165Status ScriptInterpreterPythonImpl::GenerateBreakpointCommandCallbackData(
2166    StringList &user_input, std::string &output,
2167    bool has_extra_args) {
2168  static uint32_t num_created_functions = 0;
2169  user_input.RemoveBlankLines();
2170  StreamString sstr;
2171  Status error;
2172  if (user_input.GetSize() == 0) {
2173    error.SetErrorString("No input data.");
2174    return error;
2175  }
2176
2177  std::string auto_generated_function_name(GenerateUniqueName(
2178      "lldb_autogen_python_bp_callback_func_", num_created_functions));
2179  if (has_extra_args)
2180    sstr.Printf("def %s (frame, bp_loc, extra_args, internal_dict):",
2181                auto_generated_function_name.c_str());
2182  else
2183    sstr.Printf("def %s (frame, bp_loc, internal_dict):",
2184                auto_generated_function_name.c_str());
2185
2186  error = GenerateFunction(sstr.GetData(), user_input);
2187  if (!error.Success())
2188    return error;
2189
2190  // Store the name of the auto-generated function to be called.
2191  output.assign(auto_generated_function_name);
2192  return error;
2193}
2194
2195bool ScriptInterpreterPythonImpl::GenerateWatchpointCommandCallbackData(
2196    StringList &user_input, std::string &output) {
2197  static uint32_t num_created_functions = 0;
2198  user_input.RemoveBlankLines();
2199  StreamString sstr;
2200
2201  if (user_input.GetSize() == 0)
2202    return false;
2203
2204  std::string auto_generated_function_name(GenerateUniqueName(
2205      "lldb_autogen_python_wp_callback_func_", num_created_functions));
2206  sstr.Printf("def %s (frame, wp, internal_dict):",
2207              auto_generated_function_name.c_str());
2208
2209  if (!GenerateFunction(sstr.GetData(), user_input).Success())
2210    return false;
2211
2212  // Store the name of the auto-generated function to be called.
2213  output.assign(auto_generated_function_name);
2214  return true;
2215}
2216
2217bool ScriptInterpreterPythonImpl::GetScriptedSummary(
2218    const char *python_function_name, lldb::ValueObjectSP valobj,
2219    StructuredData::ObjectSP &callee_wrapper_sp,
2220    const TypeSummaryOptions &options, std::string &retval) {
2221
2222  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
2223  Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
2224
2225  if (!valobj.get()) {
2226    retval.assign("<no object>");
2227    return false;
2228  }
2229
2230  void *old_callee = nullptr;
2231  StructuredData::Generic *generic = nullptr;
2232  if (callee_wrapper_sp) {
2233    generic = callee_wrapper_sp->GetAsGeneric();
2234    if (generic)
2235      old_callee = generic->GetValue();
2236  }
2237  void *new_callee = old_callee;
2238
2239  bool ret_val;
2240  if (python_function_name && *python_function_name) {
2241    {
2242      Locker py_lock(this, Locker::AcquireLock | Locker::InitSession |
2243                               Locker::NoSTDIN);
2244      {
2245        TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options));
2246
2247        static Timer::Category func_cat("LLDBSwigPythonCallTypeScript");
2248        Timer scoped_timer(func_cat, "LLDBSwigPythonCallTypeScript");
2249        ret_val = LLDBSwigPythonCallTypeScript(
2250            python_function_name, GetSessionDictionary().get(), valobj,
2251            &new_callee, options_sp, retval);
2252      }
2253    }
2254  } else {
2255    retval.assign("<no function name>");
2256    return false;
2257  }
2258
2259  if (new_callee && old_callee != new_callee)
2260    callee_wrapper_sp = std::make_shared<StructuredPythonObject>(new_callee);
2261
2262  return ret_val;
2263}
2264
2265bool ScriptInterpreterPythonImpl::BreakpointCallbackFunction(
2266    void *baton, StoppointCallbackContext *context, user_id_t break_id,
2267    user_id_t break_loc_id) {
2268  CommandDataPython *bp_option_data = (CommandDataPython *)baton;
2269  const char *python_function_name = bp_option_data->script_source.c_str();
2270
2271  if (!context)
2272    return true;
2273
2274  ExecutionContext exe_ctx(context->exe_ctx_ref);
2275  Target *target = exe_ctx.GetTargetPtr();
2276
2277  if (!target)
2278    return true;
2279
2280  Debugger &debugger = target->GetDebugger();
2281  ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
2282  ScriptInterpreterPythonImpl *python_interpreter =
2283      (ScriptInterpreterPythonImpl *)script_interpreter;
2284
2285  if (!script_interpreter)
2286    return true;
2287
2288  if (python_function_name && python_function_name[0]) {
2289    const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
2290    BreakpointSP breakpoint_sp = target->GetBreakpointByID(break_id);
2291    if (breakpoint_sp) {
2292      const BreakpointLocationSP bp_loc_sp(
2293          breakpoint_sp->FindLocationByID(break_loc_id));
2294
2295      if (stop_frame_sp && bp_loc_sp) {
2296        bool ret_val = true;
2297        {
2298          Locker py_lock(python_interpreter, Locker::AcquireLock |
2299                                                 Locker::InitSession |
2300                                                 Locker::NoSTDIN);
2301          Expected<bool> maybe_ret_val =
2302              LLDBSwigPythonBreakpointCallbackFunction(
2303                  python_function_name,
2304                  python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
2305                  bp_loc_sp, bp_option_data->m_extra_args_up.get());
2306
2307          if (!maybe_ret_val) {
2308
2309            llvm::handleAllErrors(
2310                maybe_ret_val.takeError(),
2311                [&](PythonException &E) {
2312                  debugger.GetErrorStream() << E.ReadBacktrace();
2313                },
2314                [&](const llvm::ErrorInfoBase &E) {
2315                  debugger.GetErrorStream() << E.message();
2316                });
2317
2318          } else {
2319            ret_val = maybe_ret_val.get();
2320          }
2321        }
2322        return ret_val;
2323      }
2324    }
2325  }
2326  // We currently always true so we stop in case anything goes wrong when
2327  // trying to call the script function
2328  return true;
2329}
2330
2331bool ScriptInterpreterPythonImpl::WatchpointCallbackFunction(
2332    void *baton, StoppointCallbackContext *context, user_id_t watch_id) {
2333  WatchpointOptions::CommandData *wp_option_data =
2334      (WatchpointOptions::CommandData *)baton;
2335  const char *python_function_name = wp_option_data->script_source.c_str();
2336
2337  if (!context)
2338    return true;
2339
2340  ExecutionContext exe_ctx(context->exe_ctx_ref);
2341  Target *target = exe_ctx.GetTargetPtr();
2342
2343  if (!target)
2344    return true;
2345
2346  Debugger &debugger = target->GetDebugger();
2347  ScriptInterpreter *script_interpreter = debugger.GetScriptInterpreter();
2348  ScriptInterpreterPythonImpl *python_interpreter =
2349      (ScriptInterpreterPythonImpl *)script_interpreter;
2350
2351  if (!script_interpreter)
2352    return true;
2353
2354  if (python_function_name && python_function_name[0]) {
2355    const StackFrameSP stop_frame_sp(exe_ctx.GetFrameSP());
2356    WatchpointSP wp_sp = target->GetWatchpointList().FindByID(watch_id);
2357    if (wp_sp) {
2358      if (stop_frame_sp && wp_sp) {
2359        bool ret_val = true;
2360        {
2361          Locker py_lock(python_interpreter, Locker::AcquireLock |
2362                                                 Locker::InitSession |
2363                                                 Locker::NoSTDIN);
2364          ret_val = LLDBSwigPythonWatchpointCallbackFunction(
2365              python_function_name,
2366              python_interpreter->m_dictionary_name.c_str(), stop_frame_sp,
2367              wp_sp);
2368        }
2369        return ret_val;
2370      }
2371    }
2372  }
2373  // We currently always true so we stop in case anything goes wrong when
2374  // trying to call the script function
2375  return true;
2376}
2377
2378size_t ScriptInterpreterPythonImpl::CalculateNumChildren(
2379    const StructuredData::ObjectSP &implementor_sp, uint32_t max) {
2380  if (!implementor_sp)
2381    return 0;
2382  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2383  if (!generic)
2384    return 0;
2385  void *implementor = generic->GetValue();
2386  if (!implementor)
2387    return 0;
2388
2389  size_t ret_val = 0;
2390
2391  {
2392    Locker py_lock(this,
2393                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2394    ret_val = LLDBSwigPython_CalculateNumChildren(implementor, max);
2395  }
2396
2397  return ret_val;
2398}
2399
2400lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetChildAtIndex(
2401    const StructuredData::ObjectSP &implementor_sp, uint32_t idx) {
2402  if (!implementor_sp)
2403    return lldb::ValueObjectSP();
2404
2405  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2406  if (!generic)
2407    return lldb::ValueObjectSP();
2408  void *implementor = generic->GetValue();
2409  if (!implementor)
2410    return lldb::ValueObjectSP();
2411
2412  lldb::ValueObjectSP ret_val;
2413  {
2414    Locker py_lock(this,
2415                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2416    void *child_ptr = LLDBSwigPython_GetChildAtIndex(implementor, idx);
2417    if (child_ptr != nullptr && child_ptr != Py_None) {
2418      lldb::SBValue *sb_value_ptr =
2419          (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
2420      if (sb_value_ptr == nullptr)
2421        Py_XDECREF(child_ptr);
2422      else
2423        ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
2424    } else {
2425      Py_XDECREF(child_ptr);
2426    }
2427  }
2428
2429  return ret_val;
2430}
2431
2432int ScriptInterpreterPythonImpl::GetIndexOfChildWithName(
2433    const StructuredData::ObjectSP &implementor_sp, const char *child_name) {
2434  if (!implementor_sp)
2435    return UINT32_MAX;
2436
2437  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2438  if (!generic)
2439    return UINT32_MAX;
2440  void *implementor = generic->GetValue();
2441  if (!implementor)
2442    return UINT32_MAX;
2443
2444  int ret_val = UINT32_MAX;
2445
2446  {
2447    Locker py_lock(this,
2448                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2449    ret_val = LLDBSwigPython_GetIndexOfChildWithName(implementor, child_name);
2450  }
2451
2452  return ret_val;
2453}
2454
2455bool ScriptInterpreterPythonImpl::UpdateSynthProviderInstance(
2456    const StructuredData::ObjectSP &implementor_sp) {
2457  bool ret_val = false;
2458
2459  if (!implementor_sp)
2460    return ret_val;
2461
2462  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2463  if (!generic)
2464    return ret_val;
2465  void *implementor = generic->GetValue();
2466  if (!implementor)
2467    return ret_val;
2468
2469  {
2470    Locker py_lock(this,
2471                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2472    ret_val = LLDBSwigPython_UpdateSynthProviderInstance(implementor);
2473  }
2474
2475  return ret_val;
2476}
2477
2478bool ScriptInterpreterPythonImpl::MightHaveChildrenSynthProviderInstance(
2479    const StructuredData::ObjectSP &implementor_sp) {
2480  bool ret_val = false;
2481
2482  if (!implementor_sp)
2483    return ret_val;
2484
2485  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2486  if (!generic)
2487    return ret_val;
2488  void *implementor = generic->GetValue();
2489  if (!implementor)
2490    return ret_val;
2491
2492  {
2493    Locker py_lock(this,
2494                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2495    ret_val =
2496        LLDBSwigPython_MightHaveChildrenSynthProviderInstance(implementor);
2497  }
2498
2499  return ret_val;
2500}
2501
2502lldb::ValueObjectSP ScriptInterpreterPythonImpl::GetSyntheticValue(
2503    const StructuredData::ObjectSP &implementor_sp) {
2504  lldb::ValueObjectSP ret_val(nullptr);
2505
2506  if (!implementor_sp)
2507    return ret_val;
2508
2509  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2510  if (!generic)
2511    return ret_val;
2512  void *implementor = generic->GetValue();
2513  if (!implementor)
2514    return ret_val;
2515
2516  {
2517    Locker py_lock(this,
2518                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2519    void *child_ptr = LLDBSwigPython_GetValueSynthProviderInstance(implementor);
2520    if (child_ptr != nullptr && child_ptr != Py_None) {
2521      lldb::SBValue *sb_value_ptr =
2522          (lldb::SBValue *)LLDBSWIGPython_CastPyObjectToSBValue(child_ptr);
2523      if (sb_value_ptr == nullptr)
2524        Py_XDECREF(child_ptr);
2525      else
2526        ret_val = LLDBSWIGPython_GetValueObjectSPFromSBValue(sb_value_ptr);
2527    } else {
2528      Py_XDECREF(child_ptr);
2529    }
2530  }
2531
2532  return ret_val;
2533}
2534
2535ConstString ScriptInterpreterPythonImpl::GetSyntheticTypeName(
2536    const StructuredData::ObjectSP &implementor_sp) {
2537  Locker py_lock(this,
2538                 Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2539
2540  static char callee_name[] = "get_type_name";
2541
2542  ConstString ret_val;
2543  bool got_string = false;
2544  std::string buffer;
2545
2546  if (!implementor_sp)
2547    return ret_val;
2548
2549  StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2550  if (!generic)
2551    return ret_val;
2552  PythonObject implementor(PyRefType::Borrowed,
2553                           (PyObject *)generic->GetValue());
2554  if (!implementor.IsAllocated())
2555    return ret_val;
2556
2557  PythonObject pmeth(PyRefType::Owned,
2558                     PyObject_GetAttrString(implementor.get(), callee_name));
2559
2560  if (PyErr_Occurred())
2561    PyErr_Clear();
2562
2563  if (!pmeth.IsAllocated())
2564    return ret_val;
2565
2566  if (PyCallable_Check(pmeth.get()) == 0) {
2567    if (PyErr_Occurred())
2568      PyErr_Clear();
2569    return ret_val;
2570  }
2571
2572  if (PyErr_Occurred())
2573    PyErr_Clear();
2574
2575  // right now we know this function exists and is callable..
2576  PythonObject py_return(
2577      PyRefType::Owned,
2578      PyObject_CallMethod(implementor.get(), callee_name, nullptr));
2579
2580  // if it fails, print the error but otherwise go on
2581  if (PyErr_Occurred()) {
2582    PyErr_Print();
2583    PyErr_Clear();
2584  }
2585
2586  if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
2587    PythonString py_string(PyRefType::Borrowed, py_return.get());
2588    llvm::StringRef return_data(py_string.GetString());
2589    if (!return_data.empty()) {
2590      buffer.assign(return_data.data(), return_data.size());
2591      got_string = true;
2592    }
2593  }
2594
2595  if (got_string)
2596    ret_val.SetCStringWithLength(buffer.c_str(), buffer.size());
2597
2598  return ret_val;
2599}
2600
2601bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2602    const char *impl_function, Process *process, std::string &output,
2603    Status &error) {
2604  bool ret_val;
2605  if (!process) {
2606    error.SetErrorString("no process");
2607    return false;
2608  }
2609  if (!impl_function || !impl_function[0]) {
2610    error.SetErrorString("no function to execute");
2611    return false;
2612  }
2613
2614  {
2615    ProcessSP process_sp(process->shared_from_this());
2616    Locker py_lock(this,
2617                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2618    ret_val = LLDBSWIGPythonRunScriptKeywordProcess(
2619        impl_function, m_dictionary_name.c_str(), process_sp, output);
2620    if (!ret_val)
2621      error.SetErrorString("python script evaluation failed");
2622  }
2623  return ret_val;
2624}
2625
2626bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2627    const char *impl_function, Thread *thread, std::string &output,
2628    Status &error) {
2629  bool ret_val;
2630  if (!thread) {
2631    error.SetErrorString("no thread");
2632    return false;
2633  }
2634  if (!impl_function || !impl_function[0]) {
2635    error.SetErrorString("no function to execute");
2636    return false;
2637  }
2638
2639  {
2640    ThreadSP thread_sp(thread->shared_from_this());
2641    Locker py_lock(this,
2642                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2643    ret_val = LLDBSWIGPythonRunScriptKeywordThread(
2644        impl_function, m_dictionary_name.c_str(), thread_sp, output);
2645    if (!ret_val)
2646      error.SetErrorString("python script evaluation failed");
2647  }
2648  return ret_val;
2649}
2650
2651bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2652    const char *impl_function, Target *target, std::string &output,
2653    Status &error) {
2654  bool ret_val;
2655  if (!target) {
2656    error.SetErrorString("no thread");
2657    return false;
2658  }
2659  if (!impl_function || !impl_function[0]) {
2660    error.SetErrorString("no function to execute");
2661    return false;
2662  }
2663
2664  {
2665    TargetSP target_sp(target->shared_from_this());
2666    Locker py_lock(this,
2667                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2668    ret_val = LLDBSWIGPythonRunScriptKeywordTarget(
2669        impl_function, m_dictionary_name.c_str(), target_sp, output);
2670    if (!ret_val)
2671      error.SetErrorString("python script evaluation failed");
2672  }
2673  return ret_val;
2674}
2675
2676bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2677    const char *impl_function, StackFrame *frame, std::string &output,
2678    Status &error) {
2679  bool ret_val;
2680  if (!frame) {
2681    error.SetErrorString("no frame");
2682    return false;
2683  }
2684  if (!impl_function || !impl_function[0]) {
2685    error.SetErrorString("no function to execute");
2686    return false;
2687  }
2688
2689  {
2690    StackFrameSP frame_sp(frame->shared_from_this());
2691    Locker py_lock(this,
2692                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2693    ret_val = LLDBSWIGPythonRunScriptKeywordFrame(
2694        impl_function, m_dictionary_name.c_str(), frame_sp, output);
2695    if (!ret_val)
2696      error.SetErrorString("python script evaluation failed");
2697  }
2698  return ret_val;
2699}
2700
2701bool ScriptInterpreterPythonImpl::RunScriptFormatKeyword(
2702    const char *impl_function, ValueObject *value, std::string &output,
2703    Status &error) {
2704  bool ret_val;
2705  if (!value) {
2706    error.SetErrorString("no value");
2707    return false;
2708  }
2709  if (!impl_function || !impl_function[0]) {
2710    error.SetErrorString("no function to execute");
2711    return false;
2712  }
2713
2714  {
2715    ValueObjectSP value_sp(value->GetSP());
2716    Locker py_lock(this,
2717                   Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2718    ret_val = LLDBSWIGPythonRunScriptKeywordValue(
2719        impl_function, m_dictionary_name.c_str(), value_sp, output);
2720    if (!ret_val)
2721      error.SetErrorString("python script evaluation failed");
2722  }
2723  return ret_val;
2724}
2725
2726uint64_t replace_all(std::string &str, const std::string &oldStr,
2727                     const std::string &newStr) {
2728  size_t pos = 0;
2729  uint64_t matches = 0;
2730  while ((pos = str.find(oldStr, pos)) != std::string::npos) {
2731    matches++;
2732    str.replace(pos, oldStr.length(), newStr);
2733    pos += newStr.length();
2734  }
2735  return matches;
2736}
2737
2738bool ScriptInterpreterPythonImpl::LoadScriptingModule(
2739    const char *pathname, bool init_session, lldb_private::Status &error,
2740    StructuredData::ObjectSP *module_sp) {
2741  if (!pathname || !pathname[0]) {
2742    error.SetErrorString("invalid pathname");
2743    return false;
2744  }
2745
2746  lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
2747
2748  {
2749    FileSpec target_file(pathname);
2750    FileSystem::Instance().Resolve(target_file);
2751    std::string basename(target_file.GetFilename().GetCString());
2752
2753    StreamString command_stream;
2754
2755    // Before executing Python code, lock the GIL.
2756    Locker py_lock(this,
2757                   Locker::AcquireLock |
2758                       (init_session ? Locker::InitSession : 0) |
2759                       Locker::NoSTDIN,
2760                   Locker::FreeAcquiredLock |
2761                       (init_session ? Locker::TearDownSession : 0));
2762    namespace fs = llvm::sys::fs;
2763    fs::file_status st;
2764    std::error_code ec = status(target_file.GetPath(), st);
2765
2766    if (ec || st.type() == fs::file_type::status_error ||
2767        st.type() == fs::file_type::type_unknown ||
2768        st.type() == fs::file_type::file_not_found) {
2769      // if not a valid file of any sort, check if it might be a filename still
2770      // dot can't be used but / and \ can, and if either is found, reject
2771      if (strchr(pathname, '\\') || strchr(pathname, '/')) {
2772        error.SetErrorString("invalid pathname");
2773        return false;
2774      }
2775      basename = pathname; // not a filename, probably a package of some sort,
2776                           // let it go through
2777    } else if (is_directory(st) || is_regular_file(st)) {
2778      if (target_file.GetDirectory().IsEmpty()) {
2779        error.SetErrorString("invalid directory name");
2780        return false;
2781      }
2782
2783      std::string directory = target_file.GetDirectory().GetCString();
2784      replace_all(directory, "\\", "\\\\");
2785      replace_all(directory, "'", "\\'");
2786
2787      // now make sure that Python has "directory" in the search path
2788      StreamString command_stream;
2789      command_stream.Printf("if not (sys.path.__contains__('%s')):\n    "
2790                            "sys.path.insert(1,'%s');\n\n",
2791                            directory.c_str(), directory.c_str());
2792      bool syspath_retval =
2793          ExecuteMultipleLines(command_stream.GetData(),
2794                               ScriptInterpreter::ExecuteScriptOptions()
2795                                   .SetEnableIO(false)
2796                                   .SetSetLLDBGlobals(false))
2797              .Success();
2798      if (!syspath_retval) {
2799        error.SetErrorString("Python sys.path handling failed");
2800        return false;
2801      }
2802
2803      // strip .py or .pyc extension
2804      ConstString extension = target_file.GetFileNameExtension();
2805      if (extension) {
2806        if (llvm::StringRef(extension.GetCString()) == ".py")
2807          basename.resize(basename.length() - 3);
2808        else if (llvm::StringRef(extension.GetCString()) == ".pyc")
2809          basename.resize(basename.length() - 4);
2810      }
2811    } else {
2812      error.SetErrorString("no known way to import this module specification");
2813      return false;
2814    }
2815
2816    // check if the module is already import-ed
2817    command_stream.Clear();
2818    command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
2819    bool does_contain = false;
2820    // this call will succeed if the module was ever imported in any Debugger
2821    // in the lifetime of the process in which this LLDB framework is living
2822    bool was_imported_globally =
2823        (ExecuteOneLineWithReturn(
2824             command_stream.GetData(),
2825             ScriptInterpreterPythonImpl::eScriptReturnTypeBool, &does_contain,
2826             ScriptInterpreter::ExecuteScriptOptions()
2827                 .SetEnableIO(false)
2828                 .SetSetLLDBGlobals(false)) &&
2829         does_contain);
2830    // this call will fail if the module was not imported in this Debugger
2831    // before
2832    command_stream.Clear();
2833    command_stream.Printf("sys.getrefcount(%s)", basename.c_str());
2834    bool was_imported_locally = GetSessionDictionary()
2835                                    .GetItemForKey(PythonString(basename))
2836                                    .IsAllocated();
2837
2838    bool was_imported = (was_imported_globally || was_imported_locally);
2839
2840    // now actually do the import
2841    command_stream.Clear();
2842
2843    if (was_imported) {
2844      if (!was_imported_locally)
2845        command_stream.Printf("import %s ; reload_module(%s)", basename.c_str(),
2846                              basename.c_str());
2847      else
2848        command_stream.Printf("reload_module(%s)", basename.c_str());
2849    } else
2850      command_stream.Printf("import %s", basename.c_str());
2851
2852    error = ExecuteMultipleLines(command_stream.GetData(),
2853                                 ScriptInterpreter::ExecuteScriptOptions()
2854                                     .SetEnableIO(false)
2855                                     .SetSetLLDBGlobals(false));
2856    if (error.Fail())
2857      return false;
2858
2859    // if we are here, everything worked
2860    // call __lldb_init_module(debugger,dict)
2861    if (!LLDBSwigPythonCallModuleInit(basename.c_str(),
2862                                      m_dictionary_name.c_str(), debugger_sp)) {
2863      error.SetErrorString("calling __lldb_init_module failed");
2864      return false;
2865    }
2866
2867    if (module_sp) {
2868      // everything went just great, now set the module object
2869      command_stream.Clear();
2870      command_stream.Printf("%s", basename.c_str());
2871      void *module_pyobj = nullptr;
2872      if (ExecuteOneLineWithReturn(
2873              command_stream.GetData(),
2874              ScriptInterpreter::eScriptReturnTypeOpaqueObject,
2875              &module_pyobj) &&
2876          module_pyobj)
2877        *module_sp = std::make_shared<StructuredPythonObject>(module_pyobj);
2878    }
2879
2880    return true;
2881  }
2882}
2883
2884bool ScriptInterpreterPythonImpl::IsReservedWord(const char *word) {
2885  if (!word || !word[0])
2886    return false;
2887
2888  llvm::StringRef word_sr(word);
2889
2890  // filter out a few characters that would just confuse us and that are
2891  // clearly not keyword material anyway
2892  if (word_sr.find('"') != llvm::StringRef::npos ||
2893      word_sr.find('\'') != llvm::StringRef::npos)
2894    return false;
2895
2896  StreamString command_stream;
2897  command_stream.Printf("keyword.iskeyword('%s')", word);
2898  bool result;
2899  ExecuteScriptOptions options;
2900  options.SetEnableIO(false);
2901  options.SetMaskoutErrors(true);
2902  options.SetSetLLDBGlobals(false);
2903  if (ExecuteOneLineWithReturn(command_stream.GetData(),
2904                               ScriptInterpreter::eScriptReturnTypeBool,
2905                               &result, options))
2906    return result;
2907  return false;
2908}
2909
2910ScriptInterpreterPythonImpl::SynchronicityHandler::SynchronicityHandler(
2911    lldb::DebuggerSP debugger_sp, ScriptedCommandSynchronicity synchro)
2912    : m_debugger_sp(debugger_sp), m_synch_wanted(synchro),
2913      m_old_asynch(debugger_sp->GetAsyncExecution()) {
2914  if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2915    m_debugger_sp->SetAsyncExecution(false);
2916  else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2917    m_debugger_sp->SetAsyncExecution(true);
2918}
2919
2920ScriptInterpreterPythonImpl::SynchronicityHandler::~SynchronicityHandler() {
2921  if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2922    m_debugger_sp->SetAsyncExecution(m_old_asynch);
2923}
2924
2925bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
2926    const char *impl_function, llvm::StringRef args,
2927    ScriptedCommandSynchronicity synchronicity,
2928    lldb_private::CommandReturnObject &cmd_retobj, Status &error,
2929    const lldb_private::ExecutionContext &exe_ctx) {
2930  if (!impl_function) {
2931    error.SetErrorString("no function to execute");
2932    return false;
2933  }
2934
2935  lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
2936  lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2937
2938  if (!debugger_sp.get()) {
2939    error.SetErrorString("invalid Debugger pointer");
2940    return false;
2941  }
2942
2943  bool ret_val = false;
2944
2945  std::string err_msg;
2946
2947  {
2948    Locker py_lock(this,
2949                   Locker::AcquireLock | Locker::InitSession |
2950                       (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2951                   Locker::FreeLock | Locker::TearDownSession);
2952
2953    SynchronicityHandler synch_handler(debugger_sp, synchronicity);
2954
2955    std::string args_str = args.str();
2956    ret_val = LLDBSwigPythonCallCommand(
2957        impl_function, m_dictionary_name.c_str(), debugger_sp, args_str.c_str(),
2958        cmd_retobj, exe_ctx_ref_sp);
2959  }
2960
2961  if (!ret_val)
2962    error.SetErrorString("unable to execute script function");
2963  else
2964    error.Clear();
2965
2966  return ret_val;
2967}
2968
2969bool ScriptInterpreterPythonImpl::RunScriptBasedCommand(
2970    StructuredData::GenericSP impl_obj_sp, llvm::StringRef args,
2971    ScriptedCommandSynchronicity synchronicity,
2972    lldb_private::CommandReturnObject &cmd_retobj, Status &error,
2973    const lldb_private::ExecutionContext &exe_ctx) {
2974  if (!impl_obj_sp || !impl_obj_sp->IsValid()) {
2975    error.SetErrorString("no function to execute");
2976    return false;
2977  }
2978
2979  lldb::DebuggerSP debugger_sp = m_debugger.shared_from_this();
2980  lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2981
2982  if (!debugger_sp.get()) {
2983    error.SetErrorString("invalid Debugger pointer");
2984    return false;
2985  }
2986
2987  bool ret_val = false;
2988
2989  std::string err_msg;
2990
2991  {
2992    Locker py_lock(this,
2993                   Locker::AcquireLock | Locker::InitSession |
2994                       (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2995                   Locker::FreeLock | Locker::TearDownSession);
2996
2997    SynchronicityHandler synch_handler(debugger_sp, synchronicity);
2998
2999    std::string args_str = args.str();
3000    ret_val = LLDBSwigPythonCallCommandObject(impl_obj_sp->GetValue(),
3001                                              debugger_sp, args_str.c_str(),
3002                                              cmd_retobj, exe_ctx_ref_sp);
3003  }
3004
3005  if (!ret_val)
3006    error.SetErrorString("unable to execute script function");
3007  else
3008    error.Clear();
3009
3010  return ret_val;
3011}
3012
3013// in Python, a special attribute __doc__ contains the docstring for an object
3014// (function, method, class, ...) if any is defined Otherwise, the attribute's
3015// value is None
3016bool ScriptInterpreterPythonImpl::GetDocumentationForItem(const char *item,
3017                                                          std::string &dest) {
3018  dest.clear();
3019  if (!item || !*item)
3020    return false;
3021  std::string command(item);
3022  command += ".__doc__";
3023
3024  char *result_ptr = nullptr; // Python is going to point this to valid data if
3025                              // ExecuteOneLineWithReturn returns successfully
3026
3027  if (ExecuteOneLineWithReturn(
3028          command.c_str(), ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
3029          &result_ptr,
3030          ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false))) {
3031    if (result_ptr)
3032      dest.assign(result_ptr);
3033    return true;
3034  } else {
3035    StreamString str_stream;
3036    str_stream.Printf(
3037        "Function %s was not found. Containing module might be missing.", item);
3038    dest = str_stream.GetString();
3039    return false;
3040  }
3041}
3042
3043bool ScriptInterpreterPythonImpl::GetShortHelpForCommandObject(
3044    StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
3045  bool got_string = false;
3046  dest.clear();
3047
3048  Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
3049
3050  static char callee_name[] = "get_short_help";
3051
3052  if (!cmd_obj_sp)
3053    return false;
3054
3055  PythonObject implementor(PyRefType::Borrowed,
3056                           (PyObject *)cmd_obj_sp->GetValue());
3057
3058  if (!implementor.IsAllocated())
3059    return false;
3060
3061  PythonObject pmeth(PyRefType::Owned,
3062                     PyObject_GetAttrString(implementor.get(), callee_name));
3063
3064  if (PyErr_Occurred())
3065    PyErr_Clear();
3066
3067  if (!pmeth.IsAllocated())
3068    return false;
3069
3070  if (PyCallable_Check(pmeth.get()) == 0) {
3071    if (PyErr_Occurred())
3072      PyErr_Clear();
3073    return false;
3074  }
3075
3076  if (PyErr_Occurred())
3077    PyErr_Clear();
3078
3079  // right now we know this function exists and is callable..
3080  PythonObject py_return(
3081      PyRefType::Owned,
3082      PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3083
3084  // if it fails, print the error but otherwise go on
3085  if (PyErr_Occurred()) {
3086    PyErr_Print();
3087    PyErr_Clear();
3088  }
3089
3090  if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
3091    PythonString py_string(PyRefType::Borrowed, py_return.get());
3092    llvm::StringRef return_data(py_string.GetString());
3093    dest.assign(return_data.data(), return_data.size());
3094    got_string = true;
3095  }
3096  return got_string;
3097}
3098
3099uint32_t ScriptInterpreterPythonImpl::GetFlagsForCommandObject(
3100    StructuredData::GenericSP cmd_obj_sp) {
3101  uint32_t result = 0;
3102
3103  Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
3104
3105  static char callee_name[] = "get_flags";
3106
3107  if (!cmd_obj_sp)
3108    return result;
3109
3110  PythonObject implementor(PyRefType::Borrowed,
3111                           (PyObject *)cmd_obj_sp->GetValue());
3112
3113  if (!implementor.IsAllocated())
3114    return result;
3115
3116  PythonObject pmeth(PyRefType::Owned,
3117                     PyObject_GetAttrString(implementor.get(), callee_name));
3118
3119  if (PyErr_Occurred())
3120    PyErr_Clear();
3121
3122  if (!pmeth.IsAllocated())
3123    return result;
3124
3125  if (PyCallable_Check(pmeth.get()) == 0) {
3126    if (PyErr_Occurred())
3127      PyErr_Clear();
3128    return result;
3129  }
3130
3131  if (PyErr_Occurred())
3132    PyErr_Clear();
3133
3134  // right now we know this function exists and is callable..
3135  PythonObject py_return(
3136      PyRefType::Owned,
3137      PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3138
3139  // if it fails, print the error but otherwise go on
3140  if (PyErr_Occurred()) {
3141    PyErr_Print();
3142    PyErr_Clear();
3143  }
3144
3145  if (py_return.IsAllocated() && PythonInteger::Check(py_return.get())) {
3146    PythonInteger int_value(PyRefType::Borrowed, py_return.get());
3147    result = int_value.GetInteger();
3148  }
3149
3150  return result;
3151}
3152
3153bool ScriptInterpreterPythonImpl::GetLongHelpForCommandObject(
3154    StructuredData::GenericSP cmd_obj_sp, std::string &dest) {
3155  bool got_string = false;
3156  dest.clear();
3157
3158  Locker py_lock(this, Locker::AcquireLock | Locker::NoSTDIN, Locker::FreeLock);
3159
3160  static char callee_name[] = "get_long_help";
3161
3162  if (!cmd_obj_sp)
3163    return false;
3164
3165  PythonObject implementor(PyRefType::Borrowed,
3166                           (PyObject *)cmd_obj_sp->GetValue());
3167
3168  if (!implementor.IsAllocated())
3169    return false;
3170
3171  PythonObject pmeth(PyRefType::Owned,
3172                     PyObject_GetAttrString(implementor.get(), callee_name));
3173
3174  if (PyErr_Occurred())
3175    PyErr_Clear();
3176
3177  if (!pmeth.IsAllocated())
3178    return false;
3179
3180  if (PyCallable_Check(pmeth.get()) == 0) {
3181    if (PyErr_Occurred())
3182      PyErr_Clear();
3183
3184    return false;
3185  }
3186
3187  if (PyErr_Occurred())
3188    PyErr_Clear();
3189
3190  // right now we know this function exists and is callable..
3191  PythonObject py_return(
3192      PyRefType::Owned,
3193      PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3194
3195  // if it fails, print the error but otherwise go on
3196  if (PyErr_Occurred()) {
3197    PyErr_Print();
3198    PyErr_Clear();
3199  }
3200
3201  if (py_return.IsAllocated() && PythonString::Check(py_return.get())) {
3202    PythonString str(PyRefType::Borrowed, py_return.get());
3203    llvm::StringRef str_data(str.GetString());
3204    dest.assign(str_data.data(), str_data.size());
3205    got_string = true;
3206  }
3207
3208  return got_string;
3209}
3210
3211std::unique_ptr<ScriptInterpreterLocker>
3212ScriptInterpreterPythonImpl::AcquireInterpreterLock() {
3213  std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(
3214      this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN,
3215      Locker::FreeLock | Locker::TearDownSession));
3216  return py_lock;
3217}
3218
3219void ScriptInterpreterPythonImpl::InitializePrivate() {
3220  if (g_initialized)
3221    return;
3222
3223  g_initialized = true;
3224
3225  static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
3226  Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
3227
3228  // RAII-based initialization which correctly handles multiple-initialization,
3229  // version- specific differences among Python 2 and Python 3, and saving and
3230  // restoring various other pieces of state that can get mucked with during
3231  // initialization.
3232  InitializePythonRAII initialize_guard;
3233
3234  LLDBSwigPyInit();
3235
3236  // Update the path python uses to search for modules to include the current
3237  // directory.
3238
3239  PyRun_SimpleString("import sys");
3240  AddToSysPath(AddLocation::End, ".");
3241
3242  // Don't denormalize paths when calling file_spec.GetPath().  On platforms
3243  // that use a backslash as the path separator, this will result in executing
3244  // python code containing paths with unescaped backslashes.  But Python also
3245  // accepts forward slashes, so to make life easier we just use that.
3246  if (FileSpec file_spec = GetPythonDir())
3247    AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3248  if (FileSpec file_spec = HostInfo::GetShlibDir())
3249    AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3250
3251  PyRun_SimpleString("sys.dont_write_bytecode = 1; import "
3252                     "lldb.embedded_interpreter; from "
3253                     "lldb.embedded_interpreter import run_python_interpreter; "
3254                     "from lldb.embedded_interpreter import run_one_line");
3255}
3256
3257void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
3258                                               std::string path) {
3259  std::string path_copy;
3260
3261  std::string statement;
3262  if (location == AddLocation::Beginning) {
3263    statement.assign("sys.path.insert(0,\"");
3264    statement.append(path);
3265    statement.append("\")");
3266  } else {
3267    statement.assign("sys.path.append(\"");
3268    statement.append(path);
3269    statement.append("\")");
3270  }
3271  PyRun_SimpleString(statement.c_str());
3272}
3273
3274// We are intentionally NOT calling Py_Finalize here (this would be the logical
3275// place to call it).  Calling Py_Finalize here causes test suite runs to seg
3276// fault:  The test suite runs in Python.  It registers SBDebugger::Terminate to
3277// be called 'at_exit'.  When the test suite Python harness finishes up, it
3278// calls Py_Finalize, which calls all the 'at_exit' registered functions.
3279// SBDebugger::Terminate calls Debugger::Terminate, which calls lldb::Terminate,
3280// which calls ScriptInterpreter::Terminate, which calls
3281// ScriptInterpreterPythonImpl::Terminate.  So if we call Py_Finalize here, we
3282// end up with Py_Finalize being called from within Py_Finalize, which results
3283// in a seg fault. Since this function only gets called when lldb is shutting
3284// down and going away anyway, the fact that we don't actually call Py_Finalize
3285// should not cause any problems (everything should shut down/go away anyway
3286// when the process exits).
3287//
3288// void ScriptInterpreterPythonImpl::Terminate() { Py_Finalize (); }
3289
3290#endif
3291