1 //===-- ScriptInterpreterPython.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifdef LLDB_DISABLE_PYTHON
12 // Python is disabled in this build
16 #include "lldb-python.h"
17 #include "ScriptInterpreterPython.h"
18 #include "PythonDataObjects.h"
19 #include "PythonExceptionState.h"
27 #include "lldb/API/SBValue.h"
28 #include "lldb/Breakpoint/BreakpointLocation.h"
29 #include "lldb/Breakpoint/StoppointCallbackContext.h"
30 #include "lldb/Breakpoint/WatchpointOptions.h"
31 #include "lldb/Core/Communication.h"
32 #include "lldb/Core/Debugger.h"
33 #include "lldb/Core/PluginManager.h"
34 #include "lldb/Core/Timer.h"
35 #include "lldb/Core/ValueObject.h"
36 #include "lldb/DataFormatters/TypeSummary.h"
37 #include "lldb/Host/ConnectionFileDescriptor.h"
38 #include "lldb/Host/FileSystem.h"
39 #include "lldb/Host/HostInfo.h"
40 #include "lldb/Host/Pipe.h"
41 #include "lldb/Interpreter/CommandInterpreter.h"
42 #include "lldb/Interpreter/CommandReturnObject.h"
43 #include "lldb/Target/Thread.h"
44 #include "lldb/Target/ThreadPlan.h"
47 #include "lldb/Host/windows/ConnectionGenericFileWindows.h"
50 #include "llvm/ADT/StringRef.h"
51 #include "llvm/ADT/STLExtras.h"
54 using namespace lldb_private;
56 static ScriptInterpreterPython::SWIGInitCallback g_swig_init_callback = nullptr;
57 static ScriptInterpreterPython::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = nullptr;
58 static ScriptInterpreterPython::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = nullptr;
59 static ScriptInterpreterPython::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = nullptr;
60 static ScriptInterpreterPython::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = nullptr;
61 static ScriptInterpreterPython::SWIGPythonCreateCommandObject g_swig_create_cmd = nullptr;
62 static ScriptInterpreterPython::SWIGPythonCalculateNumChildren g_swig_calc_children = nullptr;
63 static ScriptInterpreterPython::SWIGPythonGetChildAtIndex g_swig_get_child_index = nullptr;
64 static ScriptInterpreterPython::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = nullptr;
65 static ScriptInterpreterPython::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = nullptr;
66 static ScriptInterpreterPython::SWIGPythonGetValueObjectSPFromSBValue g_swig_get_valobj_sp_from_sbvalue = nullptr;
67 static ScriptInterpreterPython::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = nullptr;
68 static ScriptInterpreterPython::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = nullptr;
69 static ScriptInterpreterPython::SWIGPythonGetValueSynthProviderInstance g_swig_getvalue_provider = nullptr;
70 static ScriptInterpreterPython::SWIGPythonCallCommand g_swig_call_command = nullptr;
71 static ScriptInterpreterPython::SWIGPythonCallCommandObject g_swig_call_command_object = nullptr;
72 static ScriptInterpreterPython::SWIGPythonCallModuleInit g_swig_call_module_init = nullptr;
73 static ScriptInterpreterPython::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = nullptr;
74 static ScriptInterpreterPython::SWIGPythonScriptKeyword_Process g_swig_run_script_keyword_process = nullptr;
75 static ScriptInterpreterPython::SWIGPythonScriptKeyword_Thread g_swig_run_script_keyword_thread = nullptr;
76 static ScriptInterpreterPython::SWIGPythonScriptKeyword_Target g_swig_run_script_keyword_target = nullptr;
77 static ScriptInterpreterPython::SWIGPythonScriptKeyword_Frame g_swig_run_script_keyword_frame = nullptr;
78 static ScriptInterpreterPython::SWIGPythonScriptKeyword_Value g_swig_run_script_keyword_value = nullptr;
79 static ScriptInterpreterPython::SWIGPython_GetDynamicSetting g_swig_plugin_get = nullptr;
80 static ScriptInterpreterPython::SWIGPythonCreateScriptedThreadPlan g_swig_thread_plan_script = nullptr;
81 static ScriptInterpreterPython::SWIGPythonCallThreadPlan g_swig_call_thread_plan = nullptr;
83 static bool g_initialized = false;
88 // Initializing Python is not a straightforward process. We cannot control what
89 // external code may have done before getting to this point in LLDB, including
90 // potentially having already initialized Python, so we need to do a lot of work
91 // to ensure that the existing state of the system is maintained across our
92 // initialization. We do this by using an RAII pattern where we save off initial
93 // state at the beginning, and restore it at the end
94 struct InitializePythonRAII
97 InitializePythonRAII() :
98 m_gil_state(PyGILState_UNLOCKED),
99 m_was_already_initialized(false)
101 // Python will muck with STDIN terminal state, so save off any current TTY
102 // settings so we can restore them.
103 m_stdin_tty_state.Save(STDIN_FILENO, false);
105 InitializePythonHome();
107 // Python < 3.2 and Python >= 3.2 reversed the ordering requirements for
108 // calling `Py_Initialize` and `PyEval_InitThreads`. < 3.2 requires that you
109 // call `PyEval_InitThreads` first, and >= 3.2 requires that you call it last.
110 #if (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 2) || (PY_MAJOR_VERSION > 3)
112 InitializeThreadsPrivate();
114 InitializeThreadsPrivate();
119 ~InitializePythonRAII()
121 if (m_was_already_initialized)
123 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
127 log->Printf("Releasing PyGILState. Returning to state = %slocked\n",
128 m_was_already_initialized == PyGILState_UNLOCKED ? "un" : "");
130 PyGILState_Release(m_gil_state);
134 // We initialized the threads in this function, just unlock the GIL.
138 m_stdin_tty_state.Restore();
142 void InitializePythonHome()
144 #if defined(LLDB_PYTHON_HOME)
145 #if PY_MAJOR_VERSION >= 3
147 static wchar_t *g_python_home = Py_DecodeLocale(LLDB_PYTHON_HOME, &size);
149 static char *g_python_home = LLDB_PYTHON_HOME;
151 Py_SetPythonHome(g_python_home);
155 void InitializeThreadsPrivate()
157 if (PyEval_ThreadsInitialized())
159 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
161 m_was_already_initialized = true;
162 m_gil_state = PyGILState_Ensure();
165 log->Printf("Ensured PyGILState. Previous state = %slocked\n",
166 m_gil_state == PyGILState_UNLOCKED ? "un" : "");
171 // InitThreads acquires the GIL if it hasn't been called before.
172 PyEval_InitThreads();
175 TerminalState m_stdin_tty_state;
176 PyGILState_STATE m_gil_state;
177 bool m_was_already_initialized;
182 ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter,
188 ScriptInterpreterLocker (),
189 m_teardown_session( (on_leave & TearDownSession) == TearDownSession ),
190 m_python_interpreter(py_interpreter)
193 if ((on_entry & InitSession) == InitSession)
195 if (DoInitSession(on_entry, in, out, err) == false)
197 // Don't teardown the session if we didn't init it.
198 m_teardown_session = false;
204 ScriptInterpreterPython::Locker::DoAcquireLock()
206 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
207 m_GILState = PyGILState_Ensure();
209 log->Printf("Ensured PyGILState. Previous state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
211 // we need to save the thread state when we first start the command
212 // because we might decide to interrupt it while some action is taking
213 // place outside of Python (e.g. printing to screen, waiting for the network, ...)
214 // in that case, _PyThreadState_Current will be NULL - and we would be unable
215 // to set the asynchronous exception - not a desirable situation
216 m_python_interpreter->SetThreadState(PyThreadState_Get());
217 m_python_interpreter->IncrementLockCount();
222 ScriptInterpreterPython::Locker::DoInitSession(uint16_t on_entry_flags, FILE *in, FILE *out, FILE *err)
224 if (!m_python_interpreter)
226 return m_python_interpreter->EnterSession (on_entry_flags, in, out, err);
230 ScriptInterpreterPython::Locker::DoFreeLock()
232 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
234 log->Printf("Releasing PyGILState. Returning to state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
235 PyGILState_Release(m_GILState);
236 m_python_interpreter->DecrementLockCount();
241 ScriptInterpreterPython::Locker::DoTearDownSession()
243 if (!m_python_interpreter)
245 m_python_interpreter->LeaveSession ();
249 ScriptInterpreterPython::Locker::~Locker()
251 if (m_teardown_session)
256 ScriptInterpreterPython::ScriptInterpreterPython(CommandInterpreter &interpreter) :
257 ScriptInterpreter(interpreter, eScriptLanguagePython),
258 IOHandlerDelegateMultiline("DONE"),
264 m_session_dict(PyInitialValue::Invalid),
265 m_sys_module_dict(PyInitialValue::Invalid),
266 m_run_one_line_function(),
267 m_run_one_line_str_global(),
268 m_dictionary_name(interpreter.GetDebugger().GetInstanceName().AsCString()),
270 m_active_io_handler(eIOHandlerNone),
271 m_session_is_active(false),
272 m_pty_slave_is_open(false),
273 m_valid_session(true),
275 m_command_thread_state(nullptr)
277 assert(g_initialized && "ScriptInterpreterPython created but InitializePrivate has not been called!");
279 m_dictionary_name.append("_dict");
280 StreamString run_string;
281 run_string.Printf ("%s = dict()", m_dictionary_name.c_str());
284 ScriptInterpreterPython::Locker::AcquireLock,
285 ScriptInterpreterPython::Locker::FreeAcquiredLock);
286 PyRun_SimpleString (run_string.GetData());
289 run_string.Printf ("run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')", m_dictionary_name.c_str());
290 PyRun_SimpleString (run_string.GetData());
292 // Reloading modules requires a different syntax in Python 2 and Python 3. This provides
293 // a consistent syntax no matter what version of Python.
295 run_string.Printf("run_one_line (%s, 'from six.moves import reload_module')", m_dictionary_name.c_str());
296 PyRun_SimpleString(run_string.GetData());
298 // WARNING: temporary code that loads Cocoa formatters - this should be done on a per-platform basis rather than loading the whole set
299 // and letting the individual formatter classes exploit APIs to check whether they can/cannot do their task
301 run_string.Printf ("run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp, pydoc')", m_dictionary_name.c_str());
302 PyRun_SimpleString (run_string.GetData());
305 run_string.Printf ("run_one_line (%s, 'import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line')", m_dictionary_name.c_str());
306 PyRun_SimpleString (run_string.GetData());
309 run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", m_dictionary_name.c_str(),
310 interpreter.GetDebugger().GetID());
311 PyRun_SimpleString (run_string.GetData());
314 ScriptInterpreterPython::~ScriptInterpreterPython ()
316 // the session dictionary may hold objects with complex state
317 // which means that they may need to be torn down with some level of smarts
318 // and that, in turn, requires a valid thread state
319 // force Python to procure itself such a thread state, nuke the session dictionary
320 // and then release it for others to use and proceed with the rest of the shutdown
321 auto gil_state = PyGILState_Ensure();
322 m_session_dict.Reset();
323 PyGILState_Release(gil_state);
327 ScriptInterpreterPython::Initialize()
329 static std::once_flag g_once_flag;
331 std::call_once(g_once_flag, []()
335 PluginManager::RegisterPlugin(GetPluginNameStatic(),
336 GetPluginDescriptionStatic(),
337 lldb::eScriptLanguagePython,
343 ScriptInterpreterPython::Terminate()
348 lldb::ScriptInterpreterSP
349 ScriptInterpreterPython::CreateInstance(CommandInterpreter &interpreter)
351 return std::make_shared<ScriptInterpreterPython>(interpreter);
354 lldb_private::ConstString
355 ScriptInterpreterPython::GetPluginNameStatic()
357 static ConstString g_name("script-python");
362 ScriptInterpreterPython::GetPluginDescriptionStatic()
364 return "Embedded Python interpreter";
367 lldb_private::ConstString
368 ScriptInterpreterPython::GetPluginName()
370 return GetPluginNameStatic();
374 ScriptInterpreterPython::GetPluginVersion()
380 ScriptInterpreterPython::IOHandlerActivated (IOHandler &io_handler)
382 const char *instructions = nullptr;
384 switch (m_active_io_handler)
388 case eIOHandlerBreakpoint:
389 instructions = R"(Enter your Python command(s). Type 'DONE' to end.
390 def function (frame, bp_loc, internal_dict):
391 """frame: the lldb.SBFrame for the location at which you stopped
392 bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
393 internal_dict: an LLDB support object not to be used"""
396 case eIOHandlerWatchpoint:
397 instructions = "Enter your Python command(s). Type 'DONE' to end.\n";
403 StreamFileSP output_sp(io_handler.GetOutputStreamFile());
406 output_sp->PutCString(instructions);
413 ScriptInterpreterPython::IOHandlerInputComplete (IOHandler &io_handler, std::string &data)
415 io_handler.SetIsDone(true);
416 bool batch_mode = m_interpreter.GetBatchCommandMode();
418 switch (m_active_io_handler)
422 case eIOHandlerBreakpoint:
424 std::vector<BreakpointOptions *> *bp_options_vec = (std::vector<BreakpointOptions *> *)io_handler.GetUserData();
425 for (auto bp_options : *bp_options_vec)
430 std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
433 data_ap->user_source.SplitIntoLines(data);
435 if (GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source).Success())
437 BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
438 bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
440 else if (!batch_mode)
442 StreamFileSP error_sp = io_handler.GetErrorStreamFile();
445 error_sp->Printf ("Warning: No command attached to breakpoint.\n");
451 m_active_io_handler = eIOHandlerNone;
454 case eIOHandlerWatchpoint:
456 WatchpointOptions *wp_options = (WatchpointOptions *)io_handler.GetUserData();
457 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
460 data_ap->user_source.SplitIntoLines(data);
462 if (GenerateWatchpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
464 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
465 wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
467 else if (!batch_mode)
469 StreamFileSP error_sp = io_handler.GetErrorStreamFile();
472 error_sp->Printf ("Warning: No command attached to breakpoint.\n");
477 m_active_io_handler = eIOHandlerNone;
485 ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh)
490 ScriptInterpreterPython::SaveTerminalState (int fd)
492 // Python mucks with the terminal state of STDIN. If we can possibly avoid
493 // this by setting the file handles up correctly prior to entering the
494 // interpreter we should. For now we save and restore the terminal state
495 // on the input file handle.
496 m_terminal_state.Save (fd, false);
500 ScriptInterpreterPython::RestoreTerminalState ()
502 // Python mucks with the terminal state of STDIN. If we can possibly avoid
503 // this by setting the file handles up correctly prior to entering the
504 // interpreter we should. For now we save and restore the terminal state
505 // on the input file handle.
506 m_terminal_state.Restore();
510 ScriptInterpreterPython::LeaveSession ()
512 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
514 log->PutCString("ScriptInterpreterPython::LeaveSession()");
516 // checking that we have a valid thread state - since we use our own threading and locking
517 // in some (rare) cases during cleanup Python may end up believing we have no thread state
518 // and PyImport_AddModule will crash if that is the case - since that seems to only happen
519 // when destroying the SBDebugger, we can make do without clearing up stdout and stderr
521 // rdar://problem/11292882
522 // When the current thread state is NULL, PyThreadState_Get() issues a fatal error.
523 if (PyThreadState_GetDict())
525 PythonDictionary &sys_module_dict = GetSysModuleDictionary ();
526 if (sys_module_dict.IsValid())
528 if (m_saved_stdin.IsValid())
530 sys_module_dict.SetItemForKey(PythonString("stdin"), m_saved_stdin);
531 m_saved_stdin.Reset ();
533 if (m_saved_stdout.IsValid())
535 sys_module_dict.SetItemForKey(PythonString("stdout"), m_saved_stdout);
536 m_saved_stdout.Reset ();
538 if (m_saved_stderr.IsValid())
540 sys_module_dict.SetItemForKey(PythonString("stderr"), m_saved_stderr);
541 m_saved_stderr.Reset ();
546 m_session_is_active = false;
550 ScriptInterpreterPython::EnterSession (uint16_t on_entry_flags,
555 // If we have already entered the session, without having officially 'left' it, then there is no need to
557 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
558 if (m_session_is_active)
561 log->Printf("ScriptInterpreterPython::EnterSession(on_entry_flags=0x%" PRIx16 ") session is already active, returning without doing anything", on_entry_flags);
566 log->Printf("ScriptInterpreterPython::EnterSession(on_entry_flags=0x%" PRIx16 ")", on_entry_flags);
569 m_session_is_active = true;
571 StreamString run_string;
573 if (on_entry_flags & Locker::InitGlobals)
575 run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
576 run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
577 run_string.PutCString ("; lldb.target = lldb.debugger.GetSelectedTarget()");
578 run_string.PutCString ("; lldb.process = lldb.target.GetProcess()");
579 run_string.PutCString ("; lldb.thread = lldb.process.GetSelectedThread ()");
580 run_string.PutCString ("; lldb.frame = lldb.thread.GetSelectedFrame ()");
581 run_string.PutCString ("')");
585 // If we aren't initing the globals, we should still always set the debugger (since that is always unique.)
586 run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
587 run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
588 run_string.PutCString ("')");
591 PyRun_SimpleString (run_string.GetData());
594 PythonDictionary &sys_module_dict = GetSysModuleDictionary ();
595 if (sys_module_dict.IsValid())
597 File in_file(in, false);
598 File out_file(out, false);
599 File err_file(err, false);
601 lldb::StreamFileSP in_sp;
602 lldb::StreamFileSP out_sp;
603 lldb::StreamFileSP err_sp;
604 if (!in_file.IsValid() || !out_file.IsValid() || !err_file.IsValid())
605 m_interpreter.GetDebugger().AdoptTopIOHandlerFilesIfInvalid (in_sp, out_sp, err_sp);
607 m_saved_stdin.Reset();
609 if ((on_entry_flags & Locker::NoSTDIN) == 0)
612 if (!in_file.IsValid() && in_sp)
613 in_file = in_sp->GetFile();
614 if (in_file.IsValid())
616 // Flush the file before giving it to python to avoid interleaved output.
619 m_saved_stdin = sys_module_dict.GetItemForKey(PythonString("stdin")).AsType<PythonFile>();
620 // This call can deadlock your process if the file is locked
621 PythonFile new_file(in_file, "r");
622 sys_module_dict.SetItemForKey (PythonString("stdin"), new_file);
626 if (!out_file.IsValid() && out_sp)
627 out_file = out_sp->GetFile();
628 if (out_file.IsValid())
630 // Flush the file before giving it to python to avoid interleaved output.
633 m_saved_stdout = sys_module_dict.GetItemForKey(PythonString("stdout")).AsType<PythonFile>();
635 PythonFile new_file(out_file, "w");
636 sys_module_dict.SetItemForKey (PythonString("stdout"), new_file);
639 m_saved_stdout.Reset();
641 if (!err_file.IsValid() && err_sp)
642 err_file = err_sp->GetFile();
643 if (err_file.IsValid())
645 // Flush the file before giving it to python to avoid interleaved output.
648 m_saved_stderr = sys_module_dict.GetItemForKey(PythonString("stderr")).AsType<PythonFile>();
650 PythonFile new_file(err_file, "w");
651 sys_module_dict.SetItemForKey (PythonString("stderr"), new_file);
654 m_saved_stderr.Reset();
657 if (PyErr_Occurred())
664 ScriptInterpreterPython::GetMainModule()
666 if (!m_main_module.IsValid())
667 m_main_module.Reset(PyRefType::Borrowed, PyImport_AddModule("__main__"));
668 return m_main_module;
672 ScriptInterpreterPython::GetSessionDictionary ()
674 if (m_session_dict.IsValid())
675 return m_session_dict;
677 PythonObject &main_module = GetMainModule();
678 if (!main_module.IsValid())
679 return m_session_dict;
681 PythonDictionary main_dict(PyRefType::Borrowed, PyModule_GetDict(main_module.get()));
682 if (!main_dict.IsValid())
683 return m_session_dict;
685 PythonObject item = main_dict.GetItemForKey(PythonString(m_dictionary_name));
686 m_session_dict.Reset(PyRefType::Borrowed, item.get());
687 return m_session_dict;
691 ScriptInterpreterPython::GetSysModuleDictionary ()
693 if (m_sys_module_dict.IsValid())
694 return m_sys_module_dict;
696 PythonObject sys_module(PyRefType::Borrowed, PyImport_AddModule("sys"));
697 if (sys_module.IsValid())
698 m_sys_module_dict.Reset(PyRefType::Borrowed, PyModule_GetDict(sys_module.get()));
699 return m_sys_module_dict;
703 GenerateUniqueName (const char* base_name_wanted,
704 uint32_t& functions_counter,
705 const void* name_token = nullptr)
709 if (!base_name_wanted)
710 return std::string();
713 sstr.Printf ("%s_%d", base_name_wanted, functions_counter++);
715 sstr.Printf ("%s_%p", base_name_wanted, name_token);
717 return sstr.GetString();
721 ScriptInterpreterPython::GetEmbeddedInterpreterModuleObjects ()
723 if (m_run_one_line_function.IsValid())
726 PythonObject module(PyRefType::Borrowed, PyImport_AddModule ("lldb.embedded_interpreter"));
727 if (!module.IsValid())
730 PythonDictionary module_dict(PyRefType::Borrowed, PyModule_GetDict(module.get()));
731 if (!module_dict.IsValid())
734 m_run_one_line_function = module_dict.GetItemForKey(PythonString("run_one_line"));
735 m_run_one_line_str_global = module_dict.GetItemForKey(PythonString("g_run_one_line_str"));
736 return m_run_one_line_function.IsValid();
740 ReadThreadBytesReceived(void *baton, const void *src, size_t src_len)
744 Stream *strm = (Stream *)baton;
745 strm->Write(src, src_len);
751 ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options)
753 if (!m_valid_session)
756 if (command && command[0])
758 // We want to call run_one_line, passing in the dictionary and the command string. We cannot do this through
759 // PyRun_SimpleString here because the command string may contain escaped characters, and putting it inside
760 // another string to pass to PyRun_SimpleString messes up the escaping. So we use the following more complicated
761 // method to pass the command string directly down to Python.
762 Debugger &debugger = m_interpreter.GetDebugger();
764 StreamFileSP input_file_sp;
765 StreamFileSP output_file_sp;
766 StreamFileSP error_file_sp;
767 Communication output_comm ("lldb.ScriptInterpreterPython.ExecuteOneLine.comm");
768 bool join_read_thread = false;
769 if (options.GetEnableIO())
773 input_file_sp = debugger.GetInputFile();
774 // Set output to a temporary file so we can forward the results on to the result object
777 Error pipe_result = pipe.CreateNew(false);
778 if (pipe_result.Success())
781 lldb::file_t read_file = pipe.GetReadNativeHandle();
782 pipe.ReleaseReadFileDescriptor();
783 std::unique_ptr<ConnectionGenericFile> conn_ap(new ConnectionGenericFile(read_file, true));
785 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor(pipe.ReleaseReadFileDescriptor(), true));
787 if (conn_ap->IsConnected())
789 output_comm.SetConnection(conn_ap.release());
790 output_comm.SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived, &result->GetOutputStream());
791 output_comm.StartReadThread();
792 join_read_thread = true;
793 FILE *outfile_handle = fdopen (pipe.ReleaseWriteFileDescriptor(), "w");
794 output_file_sp.reset(new StreamFile(outfile_handle, true));
795 error_file_sp = output_file_sp;
797 ::setbuf (outfile_handle, nullptr);
799 result->SetImmediateOutputFile(debugger.GetOutputFile()->GetFile().GetStream());
800 result->SetImmediateErrorFile(debugger.GetErrorFile()->GetFile().GetStream());
804 if (!input_file_sp || !output_file_sp || !error_file_sp)
805 debugger.AdoptTopIOHandlerFilesIfInvalid(input_file_sp, output_file_sp, error_file_sp);
809 input_file_sp.reset (new StreamFile ());
810 input_file_sp->GetFile().Open(FileSystem::DEV_NULL, File::eOpenOptionRead);
811 output_file_sp.reset (new StreamFile ());
812 output_file_sp->GetFile().Open(FileSystem::DEV_NULL, File::eOpenOptionWrite);
813 error_file_sp = output_file_sp;
816 FILE *in_file = input_file_sp->GetFile().GetStream();
817 FILE *out_file = output_file_sp->GetFile().GetStream();
818 FILE *err_file = error_file_sp->GetFile().GetStream();
819 bool success = false;
821 // WARNING! It's imperative that this RAII scope be as tight as possible. In particular, the
822 // scope must end *before* we try to join the read thread. The reason for this is that a
823 // pre-requisite for joining the read thread is that we close the write handle (to break the
824 // pipe and cause it to wake up and exit). But acquiring the GIL as below will redirect Python's
825 // stdio to use this same handle. If we close the handle while Python is still using it, bad
826 // things will happen.
828 ScriptInterpreterPython::Locker::AcquireLock |
829 ScriptInterpreterPython::Locker::InitSession |
830 (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) |
831 ((result && result->GetInteractive()) ? 0: Locker::NoSTDIN),
832 ScriptInterpreterPython::Locker::FreeAcquiredLock |
833 ScriptInterpreterPython::Locker::TearDownSession,
838 // Find the correct script interpreter dictionary in the main module.
839 PythonDictionary &session_dict = GetSessionDictionary ();
840 if (session_dict.IsValid())
842 if (GetEmbeddedInterpreterModuleObjects ())
844 if (PyCallable_Check(m_run_one_line_function.get()))
846 PythonObject pargs(PyRefType::Owned, Py_BuildValue("(Os)", session_dict.get(), command));
849 PythonObject return_value(PyRefType::Owned,
850 PyObject_CallObject(m_run_one_line_function.get(), pargs.get()));
851 if (return_value.IsValid())
853 else if (options.GetMaskoutErrors() && PyErr_Occurred ())
863 // Flush our output and error file handles
865 if (out_file != err_file)
869 if (join_read_thread)
871 // Close the write end of the pipe since we are done with our
872 // one line script. This should cause the read thread that
873 // output_comm is using to exit
874 output_file_sp->GetFile().Close();
875 // The close above should cause this thread to exit when it gets
876 // to the end of file, so let it get all its data
877 output_comm.JoinReadThread();
878 // Now we can close the read end of the pipe
879 output_comm.Disconnect();
886 // The one-liner failed. Append the error message.
888 result->AppendErrorWithFormat ("python failed attempting to evaluate '%s'\n", command);
893 result->AppendError ("empty command passed to python\n");
898 class IOHandlerPythonInterpreter :
903 IOHandlerPythonInterpreter (Debugger &debugger,
904 ScriptInterpreterPython *python) :
905 IOHandler (debugger, IOHandler::Type::PythonInterpreter),
911 ~IOHandlerPythonInterpreter() override
917 GetControlSequence (char ch) override
920 return ConstString("quit()\n");
921 return ConstString();
929 int stdin_fd = GetInputFD();
932 Terminal terminal(stdin_fd);
933 TerminalState terminal_state;
934 const bool is_a_tty = terminal.IsATerminal();
938 terminal_state.Save (stdin_fd, false);
939 terminal.SetCanonical(false);
940 terminal.SetEcho(true);
943 ScriptInterpreterPython::Locker locker (m_python,
944 ScriptInterpreterPython::Locker::AcquireLock |
945 ScriptInterpreterPython::Locker::InitSession |
946 ScriptInterpreterPython::Locker::InitGlobals,
947 ScriptInterpreterPython::Locker::FreeAcquiredLock |
948 ScriptInterpreterPython::Locker::TearDownSession);
950 // The following call drops into the embedded interpreter loop and stays there until the
951 // user chooses to exit from the Python interpreter.
952 // This embedded interpreter will, as any Python code that performs I/O, unlock the GIL before
953 // a system call that can hang, and lock it when the syscall has returned.
955 // We need to surround the call to the embedded interpreter with calls to PyGILState_Ensure and
956 // PyGILState_Release (using the Locker above). This is because Python has a global lock which must be held whenever we want
957 // to touch any Python objects. Otherwise, if the user calls Python code, the interpreter state will be off,
958 // and things could hang (it's happened before).
960 StreamString run_string;
961 run_string.Printf ("run_python_interpreter (%s)", m_python->GetDictionaryName ());
962 PyRun_SimpleString (run_string.GetData());
965 terminal_state.Restore();
978 Interrupt () override
980 return m_python->Interrupt();
989 ScriptInterpreterPython *m_python;
994 ScriptInterpreterPython::ExecuteInterpreterLoop ()
996 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
998 Debugger &debugger = GetCommandInterpreter().GetDebugger();
1000 // At the moment, the only time the debugger does not have an input file handle is when this is called
1001 // directly from Python, in which case it is both dangerous and unnecessary (not to mention confusing) to
1002 // try to embed a running interpreter loop inside the already running Python interpreter loop, so we won't
1005 if (!debugger.GetInputFile()->GetFile().IsValid())
1008 IOHandlerSP io_handler_sp (new IOHandlerPythonInterpreter (debugger, this));
1011 debugger.PushIOHandler(io_handler_sp);
1016 ScriptInterpreterPython::Interrupt()
1018 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
1020 if (IsExecutingPython())
1022 PyThreadState *state = PyThreadState_Get();
1024 state = GetThreadState();
1027 long tid = state->thread_id;
1028 PyThreadState_Swap(state);
1029 int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);
1031 log->Printf("ScriptInterpreterPython::Interrupt() sending PyExc_KeyboardInterrupt (tid = %li, num_threads = %i)...", tid, num_threads);
1036 log->Printf("ScriptInterpreterPython::Interrupt() python code not running, can't interrupt");
1041 ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
1042 ScriptInterpreter::ScriptReturnType return_type,
1044 const ExecuteScriptOptions &options)
1048 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN,
1049 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1051 PythonObject py_return;
1052 PythonObject &main_module = GetMainModule();
1053 PythonDictionary globals(PyRefType::Borrowed, PyModule_GetDict(main_module.get()));
1054 PythonObject py_error;
1055 bool ret_success = false;
1058 PythonDictionary locals = GetSessionDictionary ();
1060 if (!locals.IsValid())
1062 locals.Reset(PyRefType::Owned, PyObject_GetAttrString(globals.get(), m_dictionary_name.c_str()));
1065 if (!locals.IsValid())
1068 py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
1069 if (py_error.IsValid())
1072 if (in_string != nullptr)
1074 { // scope for PythonInputReaderManager
1075 //PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
1076 py_return.Reset(PyRefType::Owned, PyRun_String(in_string, Py_eval_input, globals.get(), locals.get()));
1077 if (!py_return.IsValid())
1079 py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
1080 if (py_error.IsValid())
1083 py_return.Reset(PyRefType::Owned, PyRun_String(in_string, Py_single_input, globals.get(), locals.get()));
1087 if (py_return.IsValid())
1089 switch (return_type)
1091 case eScriptReturnTypeCharPtr: // "char *"
1093 const char format[3] = "s#";
1094 success = PyArg_Parse (py_return.get(), format, (char **) ret_value);
1097 case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return == Py_None
1099 const char format[3] = "z";
1100 success = PyArg_Parse (py_return.get(), format, (char **) ret_value);
1103 case eScriptReturnTypeBool:
1105 const char format[2] = "b";
1106 success = PyArg_Parse (py_return.get(), format, (bool *) ret_value);
1109 case eScriptReturnTypeShortInt:
1111 const char format[2] = "h";
1112 success = PyArg_Parse (py_return.get(), format, (short *) ret_value);
1115 case eScriptReturnTypeShortIntUnsigned:
1117 const char format[2] = "H";
1118 success = PyArg_Parse (py_return.get(), format, (unsigned short *) ret_value);
1121 case eScriptReturnTypeInt:
1123 const char format[2] = "i";
1124 success = PyArg_Parse (py_return.get(), format, (int *) ret_value);
1127 case eScriptReturnTypeIntUnsigned:
1129 const char format[2] = "I";
1130 success = PyArg_Parse (py_return.get(), format, (unsigned int *) ret_value);
1133 case eScriptReturnTypeLongInt:
1135 const char format[2] = "l";
1136 success = PyArg_Parse (py_return.get(), format, (long *) ret_value);
1139 case eScriptReturnTypeLongIntUnsigned:
1141 const char format[2] = "k";
1142 success = PyArg_Parse (py_return.get(), format, (unsigned long *) ret_value);
1145 case eScriptReturnTypeLongLong:
1147 const char format[2] = "L";
1148 success = PyArg_Parse (py_return.get(), format, (long long *) ret_value);
1151 case eScriptReturnTypeLongLongUnsigned:
1153 const char format[2] = "K";
1154 success = PyArg_Parse (py_return.get(), format, (unsigned long long *) ret_value);
1157 case eScriptReturnTypeFloat:
1159 const char format[2] = "f";
1160 success = PyArg_Parse (py_return.get(), format, (float *) ret_value);
1163 case eScriptReturnTypeDouble:
1165 const char format[2] = "d";
1166 success = PyArg_Parse (py_return.get(), format, (double *) ret_value);
1169 case eScriptReturnTypeChar:
1171 const char format[2] = "c";
1172 success = PyArg_Parse (py_return.get(), format, (char *) ret_value);
1175 case eScriptReturnTypeOpaqueObject:
1178 PyObject *saved_value = py_return.get();
1179 Py_XINCREF(saved_value);
1180 *((PyObject **)ret_value) = saved_value;
1188 ret_success = false;
1192 py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
1193 if (py_error.IsValid())
1195 ret_success = false;
1196 if (options.GetMaskoutErrors())
1198 if (PyErr_GivenExceptionMatches (py_error.get(), PyExc_SyntaxError))
1208 ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, const ExecuteScriptOptions &options)
1213 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0) | Locker::NoSTDIN,
1214 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1216 PythonObject return_value;
1217 PythonObject &main_module = GetMainModule();
1218 PythonDictionary globals(PyRefType::Borrowed, PyModule_GetDict(main_module.get()));
1219 PythonObject py_error;
1221 PythonDictionary locals = GetSessionDictionary();
1223 if (!locals.IsValid())
1224 locals.Reset(PyRefType::Owned, PyObject_GetAttrString(globals.get(), m_dictionary_name.c_str()));
1226 if (!locals.IsValid())
1229 py_error.Reset(PyRefType::Borrowed, PyErr_Occurred());
1230 if (py_error.IsValid())
1233 if (in_string != nullptr)
1235 PythonObject code_object;
1236 code_object.Reset(PyRefType::Owned, Py_CompileString(in_string, "temp.py", Py_file_input));
1238 if (code_object.IsValid())
1240 // In Python 2.x, PyEval_EvalCode takes a PyCodeObject, but in Python 3.x, it takes
1241 // a PyObject. They are convertible (hence the function PyCode_Check(PyObject*), so
1242 // we have to do the cast for Python 2.x
1243 #if PY_MAJOR_VERSION >= 3
1244 PyObject *py_code_obj = code_object.get();
1246 PyCodeObject *py_code_obj = reinterpret_cast<PyCodeObject *>(code_object.get());
1248 return_value.Reset(PyRefType::Owned, PyEval_EvalCode(py_code_obj, globals.get(), locals.get()));
1252 PythonExceptionState exception_state(!options.GetMaskoutErrors());
1253 if (exception_state.IsError())
1254 error.SetErrorString(exception_state.Format().c_str());
1261 ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (std::vector<BreakpointOptions *> &bp_options_vec,
1262 CommandReturnObject &result)
1264 m_active_io_handler = eIOHandlerBreakpoint;
1265 m_interpreter.GetPythonCommandsFromIOHandler (" ", *this, true, &bp_options_vec);
1269 ScriptInterpreterPython::CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
1270 CommandReturnObject &result)
1272 m_active_io_handler = eIOHandlerWatchpoint;
1273 m_interpreter.GetPythonCommandsFromIOHandler (" ", *this, true, wp_options);
1277 ScriptInterpreterPython::SetBreakpointCommandCallbackFunction (BreakpointOptions *bp_options,
1278 const char *function_name)
1280 // For now just cons up a oneliner that calls the provided function.
1281 std::string oneliner("return ");
1282 oneliner += function_name;
1283 oneliner += "(frame, bp_loc, internal_dict)";
1284 m_interpreter.GetScriptInterpreter()->SetBreakpointCommandCallback (bp_options,
1288 // Set a Python one-liner as the callback for the breakpoint.
1290 ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
1291 const char *command_body_text)
1293 std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1295 // Split the command_body_text into lines, and pass that to GenerateBreakpointCommandCallbackData. That will
1296 // wrap the body in an auto-generated function, and return the function name in script_source. That is what
1297 // the callback will actually invoke.
1299 data_ap->user_source.SplitIntoLines(command_body_text);
1300 Error error = GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source);
1301 if (error.Success())
1303 BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
1304 bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
1311 // Set a Python one-liner as the callback for the watchpoint.
1313 ScriptInterpreterPython::SetWatchpointCommandCallback (WatchpointOptions *wp_options,
1314 const char *oneliner)
1316 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1318 // It's necessary to set both user_source and script_source to the oneliner.
1319 // The former is used to generate callback description (as in watchpoint command list)
1320 // while the latter is used for Python to interpret during the actual callback.
1322 data_ap->user_source.AppendString (oneliner);
1323 data_ap->script_source.assign (oneliner);
1325 if (GenerateWatchpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
1327 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
1328 wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
1335 ScriptInterpreterPython::ExportFunctionDefinitionToInterpreter (StringList &function_def)
1337 // Convert StringList to one long, newline delimited, const char *.
1338 std::string function_def_string(function_def.CopyList());
1340 Error error = ExecuteMultipleLines (function_def_string.c_str(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false));
1345 ScriptInterpreterPython::GenerateFunction(const char *signature, const StringList &input)
1348 int num_lines = input.GetSize ();
1351 error.SetErrorString ("No input data.");
1355 if (!signature || *signature == 0)
1357 error.SetErrorString("No output function name.");
1362 StringList auto_generated_function;
1363 auto_generated_function.AppendString (signature);
1364 auto_generated_function.AppendString (" global_dict = globals()"); // Grab the global dictionary
1365 auto_generated_function.AppendString (" new_keys = internal_dict.keys()"); // Make a list of keys in the session dict
1366 auto_generated_function.AppendString (" old_keys = global_dict.keys()"); // Save list of keys in global dict
1367 auto_generated_function.AppendString (" global_dict.update (internal_dict)"); // Add the session dictionary to the
1368 // global dictionary.
1370 // Wrap everything up inside the function, increasing the indentation.
1372 auto_generated_function.AppendString(" if True:");
1373 for (int i = 0; i < num_lines; ++i)
1376 sstr.Printf (" %s", input.GetStringAtIndex (i));
1377 auto_generated_function.AppendString (sstr.GetData());
1379 auto_generated_function.AppendString (" for key in new_keys:"); // Iterate over all the keys from session dict
1380 auto_generated_function.AppendString (" internal_dict[key] = global_dict[key]"); // Update session dict values
1381 auto_generated_function.AppendString (" if key not in old_keys:"); // If key was not originally in global dict
1382 auto_generated_function.AppendString (" del global_dict[key]"); // ...then remove key/value from global dict
1384 // Verify that the results are valid Python.
1386 error = ExportFunctionDefinitionToInterpreter (auto_generated_function);
1392 ScriptInterpreterPython::GenerateTypeScriptFunction (StringList &user_input, std::string& output, const void* name_token)
1394 static uint32_t num_created_functions = 0;
1395 user_input.RemoveBlankLines ();
1398 // Check to see if we have any data; if not, just return.
1399 if (user_input.GetSize() == 0)
1402 // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the
1403 // ValueObject as parameter to the function.
1405 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_type_print_func", num_created_functions, name_token));
1406 sstr.Printf ("def %s (valobj, internal_dict):", auto_generated_function_name.c_str());
1408 if (!GenerateFunction(sstr.GetData(), user_input).Success())
1411 // Store the name of the auto-generated function to be called.
1412 output.assign(auto_generated_function_name);
1417 ScriptInterpreterPython::GenerateScriptAliasFunction (StringList &user_input, std::string &output)
1419 static uint32_t num_created_functions = 0;
1420 user_input.RemoveBlankLines ();
1423 // Check to see if we have any data; if not, just return.
1424 if (user_input.GetSize() == 0)
1427 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_cmd_alias_func", num_created_functions));
1429 sstr.Printf ("def %s (debugger, args, result, internal_dict):", auto_generated_function_name.c_str());
1431 if (!GenerateFunction(sstr.GetData(),user_input).Success())
1434 // Store the name of the auto-generated function to be called.
1435 output.assign(auto_generated_function_name);
1441 ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, std::string &output, const void* name_token)
1443 static uint32_t num_created_classes = 0;
1444 user_input.RemoveBlankLines ();
1445 int num_lines = user_input.GetSize ();
1448 // Check to see if we have any data; if not, just return.
1449 if (user_input.GetSize() == 0)
1452 // Wrap all user input into a Python class
1454 std::string auto_generated_class_name(GenerateUniqueName("lldb_autogen_python_type_synth_class",num_created_classes,name_token));
1456 StringList auto_generated_class;
1458 // Create the function name & definition string.
1460 sstr.Printf ("class %s:", auto_generated_class_name.c_str());
1461 auto_generated_class.AppendString (sstr.GetData());
1463 // Wrap everything up inside the class, increasing the indentation.
1464 // we don't need to play any fancy indentation tricks here because there is no
1465 // surrounding code whose indentation we need to honor
1466 for (int i = 0; i < num_lines; ++i)
1469 sstr.Printf (" %s", user_input.GetStringAtIndex (i));
1470 auto_generated_class.AppendString (sstr.GetData());
1474 // Verify that the results are valid Python.
1475 // (even though the method is ExportFunctionDefinitionToInterpreter, a class will actually be exported)
1476 // (TODO: rename that method to ExportDefinitionToInterpreter)
1477 if (!ExportFunctionDefinitionToInterpreter (auto_generated_class).Success())
1480 // Store the name of the auto-generated class
1482 output.assign(auto_generated_class_name);
1486 StructuredData::GenericSP
1487 ScriptInterpreterPython::OSPlugin_CreatePluginObject(const char *class_name, lldb::ProcessSP process_sp)
1489 if (class_name == nullptr || class_name[0] == '\0')
1490 return StructuredData::GenericSP();
1493 return StructuredData::GenericSP();
1498 Locker py_lock (this,
1499 Locker::AcquireLock | Locker::NoSTDIN,
1501 ret_val = g_swig_create_os_plugin (class_name,
1502 m_dictionary_name.c_str(),
1506 return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
1509 StructuredData::DictionarySP
1510 ScriptInterpreterPython::OSPlugin_RegisterInfo(StructuredData::ObjectSP os_plugin_object_sp)
1512 Locker py_lock(this,
1513 Locker::AcquireLock | Locker::NoSTDIN,
1516 static char callee_name[] = "get_register_info";
1518 if (!os_plugin_object_sp)
1519 return StructuredData::DictionarySP();
1521 StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1525 PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue());
1527 if (!implementor.IsAllocated())
1528 return StructuredData::DictionarySP();
1530 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
1532 if (PyErr_Occurred())
1535 if (!pmeth.IsAllocated())
1536 return StructuredData::DictionarySP();
1538 if (PyCallable_Check(pmeth.get()) == 0)
1540 if (PyErr_Occurred())
1543 return StructuredData::DictionarySP();
1546 if (PyErr_Occurred())
1549 // right now we know this function exists and is callable..
1550 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
1552 // if it fails, print the error but otherwise go on
1553 if (PyErr_Occurred())
1558 assert(PythonDictionary::Check(py_return.get()) && "get_register_info returned unknown object type!");
1560 PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
1561 return result_dict.CreateStructuredDictionary();
1564 StructuredData::ArraySP
1565 ScriptInterpreterPython::OSPlugin_ThreadsInfo(StructuredData::ObjectSP os_plugin_object_sp)
1567 Locker py_lock (this,
1568 Locker::AcquireLock | Locker::NoSTDIN,
1571 static char callee_name[] = "get_thread_info";
1573 if (!os_plugin_object_sp)
1574 return StructuredData::ArraySP();
1576 StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1580 PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue());
1582 if (!implementor.IsAllocated())
1583 return StructuredData::ArraySP();
1585 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
1587 if (PyErr_Occurred())
1590 if (!pmeth.IsAllocated())
1591 return StructuredData::ArraySP();
1593 if (PyCallable_Check(pmeth.get()) == 0)
1595 if (PyErr_Occurred())
1598 return StructuredData::ArraySP();
1601 if (PyErr_Occurred())
1604 // right now we know this function exists and is callable..
1605 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
1607 // if it fails, print the error but otherwise go on
1608 if (PyErr_Occurred())
1614 assert(PythonList::Check(py_return.get()) && "get_thread_info returned unknown object type!");
1616 PythonList result_list(PyRefType::Borrowed, py_return.get());
1617 return result_list.CreateStructuredArray();
1620 // GetPythonValueFormatString provides a system independent type safe way to
1621 // convert a variable's type into a python value format. Python value formats
1622 // are defined in terms of builtin C types and could change from system to
1623 // as the underlying typedef for uint* types, size_t, off_t and other values
1626 template <typename T>
1627 const char *GetPythonValueFormatString(T t)
1629 assert(!"Unhandled type passed to GetPythonValueFormatString(T), make a specialization of GetPythonValueFormatString() to support this type.");
1632 template <> const char *GetPythonValueFormatString (char *) { return "s"; }
1633 template <> const char *GetPythonValueFormatString (char) { return "b"; }
1634 template <> const char *GetPythonValueFormatString (unsigned char) { return "B"; }
1635 template <> const char *GetPythonValueFormatString (short) { return "h"; }
1636 template <> const char *GetPythonValueFormatString (unsigned short) { return "H"; }
1637 template <> const char *GetPythonValueFormatString (int) { return "i"; }
1638 template <> const char *GetPythonValueFormatString (unsigned int) { return "I"; }
1639 template <> const char *GetPythonValueFormatString (long) { return "l"; }
1640 template <> const char *GetPythonValueFormatString (unsigned long) { return "k"; }
1641 template <> const char *GetPythonValueFormatString (long long) { return "L"; }
1642 template <> const char *GetPythonValueFormatString (unsigned long long) { return "K"; }
1643 template <> const char *GetPythonValueFormatString (float t) { return "f"; }
1644 template <> const char *GetPythonValueFormatString (double t) { return "d"; }
1646 StructuredData::StringSP
1647 ScriptInterpreterPython::OSPlugin_RegisterContextData(StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid)
1649 Locker py_lock (this,
1650 Locker::AcquireLock | Locker::NoSTDIN,
1653 static char callee_name[] = "get_register_data";
1654 static char *param_format = const_cast<char *>(GetPythonValueFormatString(tid));
1656 if (!os_plugin_object_sp)
1657 return StructuredData::StringSP();
1659 StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1662 PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue());
1664 if (!implementor.IsAllocated())
1665 return StructuredData::StringSP();
1667 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
1669 if (PyErr_Occurred())
1672 if (!pmeth.IsAllocated())
1673 return StructuredData::StringSP();
1675 if (PyCallable_Check(pmeth.get()) == 0)
1677 if (PyErr_Occurred())
1679 return StructuredData::StringSP();
1682 if (PyErr_Occurred())
1685 // right now we know this function exists and is callable..
1686 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, param_format, tid));
1688 // if it fails, print the error but otherwise go on
1689 if (PyErr_Occurred())
1695 assert(PythonBytes::Check(py_return.get()) && "get_register_data returned unknown object type!");
1697 PythonBytes result(PyRefType::Borrowed, py_return.get());
1698 return result.CreateStructuredString();
1701 StructuredData::DictionarySP
1702 ScriptInterpreterPython::OSPlugin_CreateThread(StructuredData::ObjectSP os_plugin_object_sp, lldb::tid_t tid, lldb::addr_t context)
1704 Locker py_lock(this,
1705 Locker::AcquireLock | Locker::NoSTDIN,
1708 static char callee_name[] = "create_thread";
1709 std::string param_format;
1710 param_format += GetPythonValueFormatString(tid);
1711 param_format += GetPythonValueFormatString(context);
1713 if (!os_plugin_object_sp)
1714 return StructuredData::DictionarySP();
1716 StructuredData::Generic *generic = os_plugin_object_sp->GetAsGeneric();
1720 PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue());
1722 if (!implementor.IsAllocated())
1723 return StructuredData::DictionarySP();
1725 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
1727 if (PyErr_Occurred())
1730 if (!pmeth.IsAllocated())
1731 return StructuredData::DictionarySP();
1733 if (PyCallable_Check(pmeth.get()) == 0)
1735 if (PyErr_Occurred())
1737 return StructuredData::DictionarySP();
1740 if (PyErr_Occurred())
1743 // right now we know this function exists and is callable..
1744 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, ¶m_format[0], tid, context));
1746 // if it fails, print the error but otherwise go on
1747 if (PyErr_Occurred())
1753 assert(PythonDictionary::Check(py_return.get()) && "create_thread returned unknown object type!");
1755 PythonDictionary result_dict(PyRefType::Borrowed, py_return.get());
1756 return result_dict.CreateStructuredDictionary();
1759 StructuredData::ObjectSP
1760 ScriptInterpreterPython::CreateScriptedThreadPlan(const char *class_name, lldb::ThreadPlanSP thread_plan_sp)
1762 if (class_name == nullptr || class_name[0] == '\0')
1763 return StructuredData::ObjectSP();
1765 if (!thread_plan_sp.get())
1766 return StructuredData::ObjectSP();
1768 Debugger &debugger = thread_plan_sp->GetTarget().GetDebugger();
1769 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1770 ScriptInterpreterPython *python_interpreter = static_cast<ScriptInterpreterPython *>(script_interpreter);
1772 if (!script_interpreter)
1773 return StructuredData::ObjectSP();
1778 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1780 ret_val = g_swig_thread_plan_script (class_name,
1781 python_interpreter->m_dictionary_name.c_str(),
1785 return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
1789 ScriptInterpreterPython::ScriptedThreadPlanExplainsStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
1791 bool explains_stop = true;
1792 StructuredData::Generic *generic = nullptr;
1794 generic = implementor_sp->GetAsGeneric();
1797 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1798 explains_stop = g_swig_call_thread_plan(generic->GetValue(), "explains_stop", event, script_error);
1802 return explains_stop;
1806 ScriptInterpreterPython::ScriptedThreadPlanShouldStop(StructuredData::ObjectSP implementor_sp, Event *event, bool &script_error)
1808 bool should_stop = true;
1809 StructuredData::Generic *generic = nullptr;
1811 generic = implementor_sp->GetAsGeneric();
1814 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1815 should_stop = g_swig_call_thread_plan(generic->GetValue(), "should_stop", event, script_error);
1823 ScriptInterpreterPython::ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error)
1825 bool should_step = false;
1826 StructuredData::Generic *generic = nullptr;
1828 generic = implementor_sp->GetAsGeneric();
1831 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1832 should_step = g_swig_call_thread_plan(generic->GetValue(), "should_step", NULL, script_error);
1837 return lldb::eStateStepping;
1839 return lldb::eStateRunning;
1842 StructuredData::ObjectSP
1843 ScriptInterpreterPython::LoadPluginModule(const FileSpec &file_spec, lldb_private::Error &error)
1845 if (!file_spec.Exists())
1847 error.SetErrorString("no such file");
1848 return StructuredData::ObjectSP();
1851 StructuredData::ObjectSP module_sp;
1853 if (LoadScriptingModule(file_spec.GetPath().c_str(),true,true,error,&module_sp))
1856 return StructuredData::ObjectSP();
1859 StructuredData::DictionarySP
1860 ScriptInterpreterPython::GetDynamicSettings(StructuredData::ObjectSP plugin_module_sp, Target *target, const char *setting_name,
1861 lldb_private::Error &error)
1863 if (!plugin_module_sp || !target || !setting_name || !setting_name[0] || !g_swig_plugin_get)
1864 return StructuredData::DictionarySP();
1865 StructuredData::Generic *generic = plugin_module_sp->GetAsGeneric();
1867 return StructuredData::DictionarySP();
1869 PythonObject reply_pyobj;
1871 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1872 TargetSP target_sp(target->shared_from_this());
1873 reply_pyobj.Reset(PyRefType::Owned,
1874 (PyObject *)g_swig_plugin_get(generic->GetValue(), setting_name, target_sp));
1877 PythonDictionary py_dict(PyRefType::Borrowed, reply_pyobj.get());
1878 return py_dict.CreateStructuredDictionary();
1881 StructuredData::ObjectSP
1882 ScriptInterpreterPython::CreateSyntheticScriptedProvider(const char *class_name, lldb::ValueObjectSP valobj)
1884 if (class_name == nullptr || class_name[0] == '\0')
1885 return StructuredData::ObjectSP();
1888 return StructuredData::ObjectSP();
1890 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
1891 Target *target = exe_ctx.GetTargetPtr();
1894 return StructuredData::ObjectSP();
1896 Debugger &debugger = target->GetDebugger();
1897 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
1898 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
1900 if (!script_interpreter)
1901 return StructuredData::ObjectSP();
1903 void *ret_val = nullptr;
1906 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1907 ret_val = g_swig_synthetic_script (class_name,
1908 python_interpreter->m_dictionary_name.c_str(),
1912 return StructuredData::ObjectSP(new StructuredPythonObject(ret_val));
1915 StructuredData::GenericSP
1916 ScriptInterpreterPython::CreateScriptCommandObject (const char *class_name)
1918 DebuggerSP debugger_sp(GetCommandInterpreter().GetDebugger().shared_from_this());
1920 if (class_name == nullptr || class_name[0] == '\0')
1921 return StructuredData::GenericSP();
1923 if (!debugger_sp.get())
1924 return StructuredData::GenericSP();
1929 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
1930 ret_val = g_swig_create_cmd (class_name,
1931 m_dictionary_name.c_str(),
1935 return StructuredData::GenericSP(new StructuredPythonObject(ret_val));
1939 ScriptInterpreterPython::GenerateTypeScriptFunction (const char* oneliner, std::string& output, const void* name_token)
1942 input.SplitIntoLines(oneliner, strlen(oneliner));
1943 return GenerateTypeScriptFunction(input, output, name_token);
1947 ScriptInterpreterPython::GenerateTypeSynthClass (const char* oneliner, std::string& output, const void* name_token)
1950 input.SplitIntoLines(oneliner, strlen(oneliner));
1951 return GenerateTypeSynthClass(input, output, name_token);
1956 ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, std::string& output)
1958 static uint32_t num_created_functions = 0;
1959 user_input.RemoveBlankLines ();
1962 if (user_input.GetSize() == 0)
1964 error.SetErrorString("No input data.");
1968 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_bp_callback_func_",num_created_functions));
1969 sstr.Printf ("def %s (frame, bp_loc, internal_dict):", auto_generated_function_name.c_str());
1971 error = GenerateFunction(sstr.GetData(), user_input);
1972 if (!error.Success())
1975 // Store the name of the auto-generated function to be called.
1976 output.assign(auto_generated_function_name);
1981 ScriptInterpreterPython::GenerateWatchpointCommandCallbackData (StringList &user_input, std::string& output)
1983 static uint32_t num_created_functions = 0;
1984 user_input.RemoveBlankLines ();
1987 if (user_input.GetSize() == 0)
1990 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_wp_callback_func_",num_created_functions));
1991 sstr.Printf ("def %s (frame, wp, internal_dict):", auto_generated_function_name.c_str());
1993 if (!GenerateFunction(sstr.GetData(), user_input).Success())
1996 // Store the name of the auto-generated function to be called.
1997 output.assign(auto_generated_function_name);
2002 ScriptInterpreterPython::GetScriptedSummary(const char *python_function_name, lldb::ValueObjectSP valobj,
2003 StructuredData::ObjectSP &callee_wrapper_sp, const TypeSummaryOptions &options,
2004 std::string &retval)
2007 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
2011 retval.assign("<no object>");
2015 void *old_callee = nullptr;
2016 StructuredData::Generic *generic = nullptr;
2017 if (callee_wrapper_sp)
2019 generic = callee_wrapper_sp->GetAsGeneric();
2021 old_callee = generic->GetValue();
2023 void* new_callee = old_callee;
2026 if (python_function_name && *python_function_name)
2029 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2031 TypeSummaryOptionsSP options_sp(new TypeSummaryOptions(options));
2033 Timer scoped_timer ("g_swig_typescript_callback","g_swig_typescript_callback");
2034 ret_val = g_swig_typescript_callback (python_function_name,
2035 GetSessionDictionary().get(),
2045 retval.assign("<no function name>");
2049 if (new_callee && old_callee != new_callee)
2050 callee_wrapper_sp.reset(new StructuredPythonObject(new_callee));
2056 ScriptInterpreterPython::Clear ()
2058 // Release any global variables that might have strong references to
2059 // LLDB objects when clearing the python script interpreter.
2061 ScriptInterpreterPython::Locker::AcquireLock,
2062 ScriptInterpreterPython::Locker::FreeAcquiredLock);
2064 // This may be called as part of Py_Finalize. In that case the modules are destroyed in random
2065 // order and we can't guarantee that we can access these.
2066 if (Py_IsInitialized())
2067 PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process = None; lldb.thread = None; lldb.frame = None");
2071 ScriptInterpreterPython::BreakpointCallbackFunction
2074 StoppointCallbackContext *context,
2076 user_id_t break_loc_id
2079 BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton;
2080 const char *python_function_name = bp_option_data->script_source.c_str();
2085 ExecutionContext exe_ctx (context->exe_ctx_ref);
2086 Target *target = exe_ctx.GetTargetPtr();
2091 Debugger &debugger = target->GetDebugger();
2092 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2093 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2095 if (!script_interpreter)
2098 if (python_function_name && python_function_name[0])
2100 const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2101 BreakpointSP breakpoint_sp = target->GetBreakpointByID (break_id);
2104 const BreakpointLocationSP bp_loc_sp (breakpoint_sp->FindLocationByID (break_loc_id));
2106 if (stop_frame_sp && bp_loc_sp)
2108 bool ret_val = true;
2110 Locker py_lock(python_interpreter, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2111 ret_val = g_swig_breakpoint_callback (python_function_name,
2112 python_interpreter->m_dictionary_name.c_str(),
2120 // We currently always true so we stop in case anything goes wrong when
2121 // trying to call the script function
2126 ScriptInterpreterPython::WatchpointCallbackFunction
2129 StoppointCallbackContext *context,
2133 WatchpointOptions::CommandData *wp_option_data = (WatchpointOptions::CommandData *) baton;
2134 const char *python_function_name = wp_option_data->script_source.c_str();
2139 ExecutionContext exe_ctx (context->exe_ctx_ref);
2140 Target *target = exe_ctx.GetTargetPtr();
2145 Debugger &debugger = target->GetDebugger();
2146 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2147 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2149 if (!script_interpreter)
2152 if (python_function_name && python_function_name[0])
2154 const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2155 WatchpointSP wp_sp = target->GetWatchpointList().FindByID (watch_id);
2158 if (stop_frame_sp && wp_sp)
2160 bool ret_val = true;
2162 Locker py_lock(python_interpreter, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2163 ret_val = g_swig_watchpoint_callback (python_function_name,
2164 python_interpreter->m_dictionary_name.c_str(),
2172 // We currently always true so we stop in case anything goes wrong when
2173 // trying to call the script function
2178 ScriptInterpreterPython::CalculateNumChildren(const StructuredData::ObjectSP &implementor_sp, uint32_t max)
2180 if (!implementor_sp)
2182 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2185 void *implementor = generic->GetValue();
2189 if (!g_swig_calc_children)
2195 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2196 ret_val = g_swig_calc_children (implementor, max);
2203 ScriptInterpreterPython::GetChildAtIndex(const StructuredData::ObjectSP &implementor_sp, uint32_t idx)
2205 if (!implementor_sp)
2206 return lldb::ValueObjectSP();
2208 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2210 return lldb::ValueObjectSP();
2211 void *implementor = generic->GetValue();
2213 return lldb::ValueObjectSP();
2215 if (!g_swig_get_child_index || !g_swig_cast_to_sbvalue)
2216 return lldb::ValueObjectSP();
2218 lldb::ValueObjectSP ret_val;
2221 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2222 void* child_ptr = g_swig_get_child_index (implementor,idx);
2223 if (child_ptr != nullptr && child_ptr != Py_None)
2225 lldb::SBValue* sb_value_ptr = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr);
2226 if (sb_value_ptr == nullptr)
2227 Py_XDECREF(child_ptr);
2229 ret_val = g_swig_get_valobj_sp_from_sbvalue (sb_value_ptr);
2233 Py_XDECREF(child_ptr);
2241 ScriptInterpreterPython::GetIndexOfChildWithName(const StructuredData::ObjectSP &implementor_sp, const char *child_name)
2243 if (!implementor_sp)
2246 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2249 void *implementor = generic->GetValue();
2253 if (!g_swig_get_index_child)
2256 int ret_val = UINT32_MAX;
2259 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2260 ret_val = g_swig_get_index_child (implementor, child_name);
2267 ScriptInterpreterPython::UpdateSynthProviderInstance(const StructuredData::ObjectSP &implementor_sp)
2269 bool ret_val = false;
2271 if (!implementor_sp)
2274 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2277 void *implementor = generic->GetValue();
2281 if (!g_swig_update_provider)
2285 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2286 ret_val = g_swig_update_provider (implementor);
2293 ScriptInterpreterPython::MightHaveChildrenSynthProviderInstance(const StructuredData::ObjectSP &implementor_sp)
2295 bool ret_val = false;
2297 if (!implementor_sp)
2300 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2303 void *implementor = generic->GetValue();
2307 if (!g_swig_mighthavechildren_provider)
2311 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2312 ret_val = g_swig_mighthavechildren_provider (implementor);
2319 ScriptInterpreterPython::GetSyntheticValue(const StructuredData::ObjectSP &implementor_sp)
2321 lldb::ValueObjectSP ret_val(nullptr);
2323 if (!implementor_sp)
2326 StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2329 void *implementor = generic->GetValue();
2333 if (!g_swig_getvalue_provider || !g_swig_cast_to_sbvalue || !g_swig_get_valobj_sp_from_sbvalue)
2337 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2338 void* child_ptr = g_swig_getvalue_provider (implementor);
2339 if (child_ptr != nullptr && child_ptr != Py_None)
2341 lldb::SBValue* sb_value_ptr = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr);
2342 if (sb_value_ptr == nullptr)
2343 Py_XDECREF(child_ptr);
2345 ret_val = g_swig_get_valobj_sp_from_sbvalue (sb_value_ptr);
2349 Py_XDECREF(child_ptr);
2357 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2359 std::string& output,
2365 error.SetErrorString("no process");
2368 if (!impl_function || !impl_function[0])
2370 error.SetErrorString("no function to execute");
2373 if (!g_swig_run_script_keyword_process)
2375 error.SetErrorString("internal helper function missing");
2379 ProcessSP process_sp(process->shared_from_this());
2380 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2381 ret_val = g_swig_run_script_keyword_process (impl_function, m_dictionary_name.c_str(), process_sp, output);
2383 error.SetErrorString("python script evaluation failed");
2389 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2391 std::string& output,
2397 error.SetErrorString("no thread");
2400 if (!impl_function || !impl_function[0])
2402 error.SetErrorString("no function to execute");
2405 if (!g_swig_run_script_keyword_thread)
2407 error.SetErrorString("internal helper function missing");
2411 ThreadSP thread_sp(thread->shared_from_this());
2412 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2413 ret_val = g_swig_run_script_keyword_thread (impl_function, m_dictionary_name.c_str(), thread_sp, output);
2415 error.SetErrorString("python script evaluation failed");
2421 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2423 std::string& output,
2429 error.SetErrorString("no thread");
2432 if (!impl_function || !impl_function[0])
2434 error.SetErrorString("no function to execute");
2437 if (!g_swig_run_script_keyword_target)
2439 error.SetErrorString("internal helper function missing");
2443 TargetSP target_sp(target->shared_from_this());
2444 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2445 ret_val = g_swig_run_script_keyword_target (impl_function, m_dictionary_name.c_str(), target_sp, output);
2447 error.SetErrorString("python script evaluation failed");
2453 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2455 std::string& output,
2461 error.SetErrorString("no frame");
2464 if (!impl_function || !impl_function[0])
2466 error.SetErrorString("no function to execute");
2469 if (!g_swig_run_script_keyword_frame)
2471 error.SetErrorString("internal helper function missing");
2475 StackFrameSP frame_sp(frame->shared_from_this());
2476 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2477 ret_val = g_swig_run_script_keyword_frame (impl_function, m_dictionary_name.c_str(), frame_sp, output);
2479 error.SetErrorString("python script evaluation failed");
2485 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2487 std::string& output,
2493 error.SetErrorString("no value");
2496 if (!impl_function || !impl_function[0])
2498 error.SetErrorString("no function to execute");
2501 if (!g_swig_run_script_keyword_value)
2503 error.SetErrorString("internal helper function missing");
2507 ValueObjectSP value_sp(value->GetSP());
2508 Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2509 ret_val = g_swig_run_script_keyword_value (impl_function, m_dictionary_name.c_str(), value_sp, output);
2511 error.SetErrorString("python script evaluation failed");
2516 uint64_t replace_all(std::string& str, const std::string& oldStr, const std::string& newStr)
2519 uint64_t matches = 0;
2520 while((pos = str.find(oldStr, pos)) != std::string::npos)
2523 str.replace(pos, oldStr.length(), newStr);
2524 pos += newStr.length();
2530 ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_reload, bool init_session, lldb_private::Error &error,
2531 StructuredData::ObjectSP *module_sp)
2533 if (!pathname || !pathname[0])
2535 error.SetErrorString("invalid pathname");
2539 if (!g_swig_call_module_init)
2541 error.SetErrorString("internal helper function missing");
2545 lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2548 FileSpec target_file(pathname, true);
2549 std::string basename(target_file.GetFilename().GetCString());
2551 StreamString command_stream;
2553 // Before executing Pyton code, lock the GIL.
2554 Locker py_lock (this,
2555 Locker::AcquireLock | (init_session ? Locker::InitSession : 0) | Locker::NoSTDIN,
2556 Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
2558 if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
2559 target_file.GetFileType() == FileSpec::eFileTypeUnknown)
2561 // if not a valid file of any sort, check if it might be a filename still
2562 // dot can't be used but / and \ can, and if either is found, reject
2563 if (strchr(pathname,'\\') || strchr(pathname,'/'))
2565 error.SetErrorString("invalid pathname");
2568 basename = pathname; // not a filename, probably a package of some sort, let it go through
2570 else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory ||
2571 target_file.GetFileType() == FileSpec::eFileTypeRegular ||
2572 target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink)
2574 std::string directory(target_file.GetDirectory().GetCString());
2575 replace_all(directory,"'","\\'");
2577 // now make sure that Python has "directory" in the search path
2578 StreamString command_stream;
2579 command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n",
2582 bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)).Success();
2583 if (!syspath_retval)
2585 error.SetErrorString("Python sys.path handling failed");
2589 // strip .py or .pyc extension
2590 ConstString extension = target_file.GetFileNameExtension();
2593 if (::strcmp(extension.GetCString(), "py") == 0)
2594 basename.resize(basename.length()-3);
2595 else if(::strcmp(extension.GetCString(), "pyc") == 0)
2596 basename.resize(basename.length()-4);
2601 error.SetErrorString("no known way to import this module specification");
2605 // check if the module is already import-ed
2606 command_stream.Clear();
2607 command_stream.Printf("sys.modules.__contains__('%s')",basename.c_str());
2608 bool does_contain = false;
2609 // this call will succeed if the module was ever imported in any Debugger in the lifetime of the process
2610 // in which this LLDB framework is living
2611 bool was_imported_globally = (ExecuteOneLineWithReturn(command_stream.GetData(),
2612 ScriptInterpreterPython::eScriptReturnTypeBool,
2614 ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && does_contain);
2615 // this call will fail if the module was not imported in this Debugger before
2616 command_stream.Clear();
2617 command_stream.Printf("sys.getrefcount(%s)",basename.c_str());
2618 bool was_imported_locally = GetSessionDictionary().GetItemForKey(PythonString(basename)).IsAllocated();
2620 bool was_imported = (was_imported_globally || was_imported_locally);
2622 if (was_imported == true && can_reload == false)
2624 error.SetErrorString("module already imported");
2628 // now actually do the import
2629 command_stream.Clear();
2633 if (!was_imported_locally)
2634 command_stream.Printf("import %s ; reload_module(%s)",basename.c_str(),basename.c_str());
2636 command_stream.Printf("reload_module(%s)",basename.c_str());
2639 command_stream.Printf("import %s",basename.c_str());
2641 error = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
2645 // if we are here, everything worked
2646 // call __lldb_init_module(debugger,dict)
2647 if (!g_swig_call_module_init (basename.c_str(),
2648 m_dictionary_name.c_str(),
2651 error.SetErrorString("calling __lldb_init_module failed");
2657 // everything went just great, now set the module object
2658 command_stream.Clear();
2659 command_stream.Printf("%s",basename.c_str());
2660 void* module_pyobj = nullptr;
2661 if (ExecuteOneLineWithReturn(command_stream.GetData(),ScriptInterpreter::eScriptReturnTypeOpaqueObject,&module_pyobj) && module_pyobj)
2662 module_sp->reset(new StructuredPythonObject(module_pyobj));
2670 ScriptInterpreterPython::IsReservedWord (const char* word)
2672 if (!word || !word[0])
2675 llvm::StringRef word_sr(word);
2677 // filter out a few characters that would just confuse us
2678 // and that are clearly not keyword material anyway
2679 if (word_sr.find_first_of("'\"") != llvm::StringRef::npos)
2682 StreamString command_stream;
2683 command_stream.Printf("keyword.iskeyword('%s')", word);
2685 ExecuteScriptOptions options;
2686 options.SetEnableIO(false);
2687 options.SetMaskoutErrors(true);
2688 options.SetSetLLDBGlobals(false);
2689 if (ExecuteOneLineWithReturn(command_stream.GetData(), ScriptInterpreter::eScriptReturnTypeBool, &result, options))
2694 ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp,
2695 ScriptedCommandSynchronicity synchro) :
2696 m_debugger_sp(debugger_sp),
2697 m_synch_wanted(synchro),
2698 m_old_asynch(debugger_sp->GetAsyncExecution())
2700 if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2701 m_debugger_sp->SetAsyncExecution(false);
2702 else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2703 m_debugger_sp->SetAsyncExecution(true);
2706 ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler()
2708 if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2709 m_debugger_sp->SetAsyncExecution(m_old_asynch);
2713 ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
2715 ScriptedCommandSynchronicity synchronicity,
2716 lldb_private::CommandReturnObject& cmd_retobj,
2718 const lldb_private::ExecutionContext& exe_ctx)
2722 error.SetErrorString("no function to execute");
2726 if (!g_swig_call_command)
2728 error.SetErrorString("no helper function to run scripted commands");
2732 lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2733 lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2735 if (!debugger_sp.get())
2737 error.SetErrorString("invalid Debugger pointer");
2741 bool ret_val = false;
2743 std::string err_msg;
2746 Locker py_lock(this,
2747 Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2748 Locker::FreeLock | Locker::TearDownSession);
2750 SynchronicityHandler synch_handler(debugger_sp,
2753 ret_val = g_swig_call_command (impl_function,
2754 m_dictionary_name.c_str(),
2762 error.SetErrorString("unable to execute script function");
2770 ScriptInterpreterPython::RunScriptBasedCommand (StructuredData::GenericSP impl_obj_sp,
2772 ScriptedCommandSynchronicity synchronicity,
2773 lldb_private::CommandReturnObject& cmd_retobj,
2775 const lldb_private::ExecutionContext& exe_ctx)
2777 if (!impl_obj_sp || !impl_obj_sp->IsValid())
2779 error.SetErrorString("no function to execute");
2783 if (!g_swig_call_command_object)
2785 error.SetErrorString("no helper function to run scripted commands");
2789 lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2790 lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2792 if (!debugger_sp.get())
2794 error.SetErrorString("invalid Debugger pointer");
2798 bool ret_val = false;
2800 std::string err_msg;
2803 Locker py_lock(this,
2804 Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2805 Locker::FreeLock | Locker::TearDownSession);
2807 SynchronicityHandler synch_handler(debugger_sp,
2810 ret_val = g_swig_call_command_object (impl_obj_sp->GetValue(),
2818 error.SetErrorString("unable to execute script function");
2825 // in Python, a special attribute __doc__ contains the docstring
2826 // for an object (function, method, class, ...) if any is defined
2827 // Otherwise, the attribute's value is None
2829 ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& dest)
2832 if (!item || !*item)
2834 std::string command(item);
2835 command += ".__doc__";
2837 char* result_ptr = nullptr; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully
2839 if (ExecuteOneLineWithReturn (command.c_str(),
2840 ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
2842 ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false)))
2845 dest.assign(result_ptr);
2850 StreamString str_stream;
2851 str_stream.Printf("Function %s was not found. Containing module might be missing.",item);
2852 dest.assign(str_stream.GetData());
2858 ScriptInterpreterPython::GetShortHelpForCommandObject (StructuredData::GenericSP cmd_obj_sp,
2861 bool got_string = false;
2864 Locker py_lock (this,
2865 Locker::AcquireLock | Locker::NoSTDIN,
2868 static char callee_name[] = "get_short_help";
2873 PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
2875 if (!implementor.IsAllocated())
2878 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2880 if (PyErr_Occurred())
2883 if (!pmeth.IsAllocated())
2886 if (PyCallable_Check(pmeth.get()) == 0)
2888 if (PyErr_Occurred())
2893 if (PyErr_Occurred())
2896 // right now we know this function exists and is callable..
2897 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
2899 // if it fails, print the error but otherwise go on
2900 if (PyErr_Occurred())
2906 if (py_return.IsAllocated() && PythonString::Check(py_return.get()))
2908 PythonString py_string(PyRefType::Borrowed, py_return.get());
2909 llvm::StringRef return_data(py_string.GetString());
2910 dest.assign(return_data.data(), return_data.size());
2917 ScriptInterpreterPython::GetFlagsForCommandObject (StructuredData::GenericSP cmd_obj_sp)
2919 uint32_t result = 0;
2921 Locker py_lock (this,
2922 Locker::AcquireLock | Locker::NoSTDIN,
2925 static char callee_name[] = "get_flags";
2930 PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
2932 if (!implementor.IsAllocated())
2935 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2937 if (PyErr_Occurred())
2940 if (!pmeth.IsAllocated())
2943 if (PyCallable_Check(pmeth.get()) == 0)
2945 if (PyErr_Occurred())
2950 if (PyErr_Occurred())
2953 // right now we know this function exists and is callable..
2954 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
2956 // if it fails, print the error but otherwise go on
2957 if (PyErr_Occurred())
2963 if (py_return.IsAllocated() && PythonInteger::Check(py_return.get()))
2965 PythonInteger int_value(PyRefType::Borrowed, py_return.get());
2966 result = int_value.GetInteger();
2973 ScriptInterpreterPython::GetLongHelpForCommandObject (StructuredData::GenericSP cmd_obj_sp,
2976 bool got_string = false;
2979 Locker py_lock (this,
2980 Locker::AcquireLock | Locker::NoSTDIN,
2983 static char callee_name[] = "get_long_help";
2988 PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
2990 if (!implementor.IsAllocated())
2993 PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2995 if (PyErr_Occurred())
2998 if (!pmeth.IsAllocated())
3001 if (PyCallable_Check(pmeth.get()) == 0)
3003 if (PyErr_Occurred())
3009 if (PyErr_Occurred())
3012 // right now we know this function exists and is callable..
3013 PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3015 // if it fails, print the error but otherwise go on
3016 if (PyErr_Occurred())
3022 if (py_return.IsAllocated() && PythonString::Check(py_return.get()))
3024 PythonString str(PyRefType::Borrowed, py_return.get());
3025 llvm::StringRef str_data(str.GetString());
3026 dest.assign(str_data.data(), str_data.size());
3033 std::unique_ptr<ScriptInterpreterLocker>
3034 ScriptInterpreterPython::AcquireInterpreterLock ()
3036 std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(this,
3037 Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN,
3038 Locker::FreeLock | Locker::TearDownSession));
3043 ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback swig_init_callback,
3044 SWIGBreakpointCallbackFunction swig_breakpoint_callback,
3045 SWIGWatchpointCallbackFunction swig_watchpoint_callback,
3046 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback,
3047 SWIGPythonCreateSyntheticProvider swig_synthetic_script,
3048 SWIGPythonCreateCommandObject swig_create_cmd,
3049 SWIGPythonCalculateNumChildren swig_calc_children,
3050 SWIGPythonGetChildAtIndex swig_get_child_index,
3051 SWIGPythonGetIndexOfChildWithName swig_get_index_child,
3052 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue ,
3053 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue,
3054 SWIGPythonUpdateSynthProviderInstance swig_update_provider,
3055 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider,
3056 SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider,
3057 SWIGPythonCallCommand swig_call_command,
3058 SWIGPythonCallCommandObject swig_call_command_object,
3059 SWIGPythonCallModuleInit swig_call_module_init,
3060 SWIGPythonCreateOSPlugin swig_create_os_plugin,
3061 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process,
3062 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread,
3063 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target,
3064 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame,
3065 SWIGPythonScriptKeyword_Value swig_run_script_keyword_value,
3066 SWIGPython_GetDynamicSetting swig_plugin_get,
3067 SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script,
3068 SWIGPythonCallThreadPlan swig_call_thread_plan)
3070 g_swig_init_callback = swig_init_callback;
3071 g_swig_breakpoint_callback = swig_breakpoint_callback;
3072 g_swig_watchpoint_callback = swig_watchpoint_callback;
3073 g_swig_typescript_callback = swig_typescript_callback;
3074 g_swig_synthetic_script = swig_synthetic_script;
3075 g_swig_create_cmd = swig_create_cmd;
3076 g_swig_calc_children = swig_calc_children;
3077 g_swig_get_child_index = swig_get_child_index;
3078 g_swig_get_index_child = swig_get_index_child;
3079 g_swig_cast_to_sbvalue = swig_cast_to_sbvalue;
3080 g_swig_get_valobj_sp_from_sbvalue = swig_get_valobj_sp_from_sbvalue;
3081 g_swig_update_provider = swig_update_provider;
3082 g_swig_mighthavechildren_provider = swig_mighthavechildren_provider;
3083 g_swig_getvalue_provider = swig_getvalue_provider;
3084 g_swig_call_command = swig_call_command;
3085 g_swig_call_command_object = swig_call_command_object;
3086 g_swig_call_module_init = swig_call_module_init;
3087 g_swig_create_os_plugin = swig_create_os_plugin;
3088 g_swig_run_script_keyword_process = swig_run_script_keyword_process;
3089 g_swig_run_script_keyword_thread = swig_run_script_keyword_thread;
3090 g_swig_run_script_keyword_target = swig_run_script_keyword_target;
3091 g_swig_run_script_keyword_frame = swig_run_script_keyword_frame;
3092 g_swig_run_script_keyword_value = swig_run_script_keyword_value;
3093 g_swig_plugin_get = swig_plugin_get;
3094 g_swig_thread_plan_script = swig_thread_plan_script;
3095 g_swig_call_thread_plan = swig_call_thread_plan;
3099 ScriptInterpreterPython::InitializePrivate ()
3101 assert(!g_initialized && "ScriptInterpreterPython::InitializePrivate() called more than once!");
3102 g_initialized = true;
3104 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
3106 // RAII-based initialization which correctly handles multiple-initialization, version-
3107 // specific differences among Python 2 and Python 3, and saving and restoring various
3108 // other pieces of state that can get mucked with during initialization.
3109 InitializePythonRAII initialize_guard;
3111 if (g_swig_init_callback)
3112 g_swig_init_callback ();
3114 // Update the path python uses to search for modules to include the current directory.
3116 PyRun_SimpleString ("import sys");
3117 AddToSysPath(AddLocation::End, ".");
3120 // Don't denormalize paths when calling file_spec.GetPath(). On platforms that use
3121 // a backslash as the path separator, this will result in executing python code containing
3122 // paths with unescaped backslashes. But Python also accepts forward slashes, so to make
3123 // life easier we just use that.
3124 if (HostInfo::GetLLDBPath(ePathTypePythonDir, file_spec))
3125 AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3126 if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, file_spec))
3127 AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3129 PyRun_SimpleString ("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line");
3133 ScriptInterpreterPython::AddToSysPath(AddLocation location, std::string path)
3135 std::string path_copy;
3137 std::string statement;
3138 if (location == AddLocation::Beginning)
3140 statement.assign("sys.path.insert(0,\"");
3141 statement.append (path);
3142 statement.append ("\")");
3146 statement.assign("sys.path.append(\"");
3147 statement.append(path);
3148 statement.append("\")");
3150 PyRun_SimpleString (statement.c_str());
3155 //ScriptInterpreterPython::Terminate ()
3157 // // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it). Calling
3158 // // Py_Finalize here causes test suite runs to seg fault: The test suite runs in Python. It registers
3159 // // SBDebugger::Terminate to be called 'at_exit'. When the test suite Python harness finishes up, it calls
3160 // // Py_Finalize, which calls all the 'at_exit' registered functions. SBDebugger::Terminate calls Debugger::Terminate,
3161 // // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls
3162 // // ScriptInterpreterPython::Terminate. So if we call Py_Finalize here, we end up with Py_Finalize being called from
3163 // // within Py_Finalize, which results in a seg fault.
3165 // // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't
3166 // // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the
3167 // // process exits).
3169 //// Py_Finalize ();
3172 #endif // #ifdef LLDB_DISABLE_PYTHON