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