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