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