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 // In order to guarantee correct working with Python, Python.h *MUST* be
11 // the *FIRST* header file included here.
12 #ifdef LLDB_DISABLE_PYTHON
14 // Python is disabled in this build
18 #if defined (__APPLE__)
19 #include <Python/Python.h>
24 #include "lldb/Interpreter/ScriptInterpreterPython.h"
31 #include "lldb/API/SBValue.h"
32 #include "lldb/Breakpoint/BreakpointLocation.h"
33 #include "lldb/Breakpoint/StoppointCallbackContext.h"
34 #include "lldb/Breakpoint/WatchpointOptions.h"
35 #include "lldb/Core/Debugger.h"
36 #include "lldb/Core/Timer.h"
37 #include "lldb/Host/Host.h"
38 #include "lldb/Interpreter/CommandInterpreter.h"
39 #include "lldb/Interpreter/CommandReturnObject.h"
40 #include "lldb/Target/Thread.h"
43 using namespace lldb_private;
46 static ScriptInterpreter::SWIGInitCallback g_swig_init_callback = NULL;
47 static ScriptInterpreter::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = NULL;
48 static ScriptInterpreter::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = NULL;
49 static ScriptInterpreter::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = NULL;
50 static ScriptInterpreter::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = NULL;
51 static ScriptInterpreter::SWIGPythonCalculateNumChildren g_swig_calc_children = NULL;
52 static ScriptInterpreter::SWIGPythonGetChildAtIndex g_swig_get_child_index = NULL;
53 static ScriptInterpreter::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = NULL;
54 static ScriptInterpreter::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = NULL;
55 static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = NULL;
56 static ScriptInterpreter::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = NULL;
57 static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = NULL;
58 static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = NULL;
59 static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = NULL;
60 static ScriptInterpreter::SWIGPythonScriptKeyword_Process g_swig_run_script_keyword_process = NULL;
61 static ScriptInterpreter::SWIGPythonScriptKeyword_Thread g_swig_run_script_keyword_thread = NULL;
62 static ScriptInterpreter::SWIGPythonScriptKeyword_Target g_swig_run_script_keyword_target = NULL;
63 static ScriptInterpreter::SWIGPythonScriptKeyword_Frame g_swig_run_script_keyword_frame = NULL;
65 // these are the Pythonic implementations of the required callbacks
66 // these are scripting-language specific, which is why they belong here
67 // we still need to use function pointers to them instead of relying
68 // on linkage-time resolution because the SWIG stuff and this file
69 // get built at different times
71 LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
72 const char *session_dictionary_name,
73 const lldb::StackFrameSP& sb_frame,
74 const lldb::BreakpointLocationSP& sb_bp_loc);
77 LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
78 const char *session_dictionary_name,
79 const lldb::StackFrameSP& sb_frame,
80 const lldb::WatchpointSP& sb_wp);
83 LLDBSwigPythonCallTypeScript (const char *python_function_name,
84 void *session_dictionary,
85 const lldb::ValueObjectSP& valobj_sp,
86 void** pyfunct_wrapper,
90 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
91 const char *session_dictionary_name,
92 const lldb::ValueObjectSP& valobj_sp);
96 LLDBSwigPython_CalculateNumChildren (void *implementor);
99 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
102 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
105 LLDBSWIGPython_CastPyObjectToSBValue (void* data);
108 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
111 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
114 LLDBSwigPythonCallCommand (const char *python_function_name,
115 const char *session_dictionary_name,
116 lldb::DebuggerSP& debugger,
118 lldb_private::CommandReturnObject &cmd_retobj);
121 LLDBSwigPythonCallModuleInit (const char *python_module_name,
122 const char *session_dictionary_name,
123 lldb::DebuggerSP& debugger);
126 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
127 const char *session_dictionary_name,
128 const lldb::ProcessSP& process_sp);
131 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
132 const char* session_dictionary_name,
133 lldb::ProcessSP& process,
134 std::string& output);
137 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
138 const char* session_dictionary_name,
139 lldb::ThreadSP& thread,
140 std::string& output);
143 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
144 const char* session_dictionary_name,
145 lldb::TargetSP& target,
146 std::string& output);
149 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
150 const char* session_dictionary_name,
151 lldb::StackFrameSP& frame,
152 std::string& output);
155 _check_and_flush (FILE *stream)
157 int prev_fail = ferror (stream);
158 return fflush (stream) || prev_fail ? EOF : 0;
161 ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter,
164 FILE* wait_msg_handle) :
165 ScriptInterpreterLocker (),
166 m_teardown_session( (on_leave & TearDownSession) == TearDownSession ),
167 m_python_interpreter(py_interpreter),
168 m_tmp_fh(wait_msg_handle)
170 if (m_python_interpreter && !m_tmp_fh)
171 m_tmp_fh = (m_python_interpreter->m_dbg_stdout ? m_python_interpreter->m_dbg_stdout : stdout);
174 if ((on_entry & InitSession) == InitSession)
176 if (DoInitSession((on_entry & InitGlobals) == InitGlobals) == false)
178 // Don't teardown the session if we didn't init it.
179 m_teardown_session = false;
185 ScriptInterpreterPython::Locker::DoAcquireLock()
187 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
188 m_GILState = PyGILState_Ensure();
190 log->Printf("Ensured PyGILState. Previous state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
195 ScriptInterpreterPython::Locker::DoInitSession(bool init_lldb_globals)
197 if (!m_python_interpreter)
199 return m_python_interpreter->EnterSession (init_lldb_globals);
203 ScriptInterpreterPython::Locker::DoFreeLock()
205 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
207 log->Printf("Releasing PyGILState. Returning to state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
208 PyGILState_Release(m_GILState);
213 ScriptInterpreterPython::Locker::DoTearDownSession()
215 if (!m_python_interpreter)
217 m_python_interpreter->LeaveSession ();
221 ScriptInterpreterPython::Locker::~Locker()
223 if (m_teardown_session)
228 ScriptInterpreterPython::PythonInputReaderManager::PythonInputReaderManager (ScriptInterpreterPython *interpreter) :
229 m_interpreter(interpreter),
234 if (m_interpreter == NULL)
240 m_debugger_sp = m_interpreter->GetCommandInterpreter().GetDebugger().shared_from_this();
248 m_reader_sp = InputReaderSP(new InputReader(*m_debugger_sp.get()));
256 Error error (m_reader_sp->Initialize (ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback,
257 m_interpreter, // baton
258 eInputReaderGranularityLine, // token size, to pass to callback function
261 true)); // echo input
266 m_debugger_sp->PushInputReader (m_reader_sp);
267 m_interpreter->m_embedded_thread_input_reader_sp = m_reader_sp;
271 ScriptInterpreterPython::PythonInputReaderManager::~PythonInputReaderManager()
273 // Nothing to do if either m_interpreter or m_reader_sp is invalid.
274 if (!m_interpreter || !m_reader_sp)
277 m_reader_sp->SetIsDone (true);
279 m_debugger_sp->PopInputReader(m_reader_sp);
281 // Only mess with m_interpreter's counterpart if, indeed, they are the same object.
282 if (m_reader_sp.get() == m_interpreter->m_embedded_thread_input_reader_sp.get())
284 m_interpreter->m_embedded_thread_pty.CloseSlaveFileDescriptor();
285 m_interpreter->m_embedded_thread_input_reader_sp.reset();
290 ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback (void *baton,
292 InputReaderAction notification,
296 lldb::thread_t embedded_interpreter_thread;
297 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
302 ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
304 if (script_interpreter->m_script_lang != eScriptLanguagePython)
307 switch (notification)
309 case eInputReaderActivate:
311 // Save terminal settings if we can
312 int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor();
313 if (input_fd == File::kInvalidDescriptor)
314 input_fd = STDIN_FILENO;
316 script_interpreter->SaveTerminalState(input_fd);
318 char error_str[1024];
319 if (script_interpreter->m_embedded_thread_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str,
323 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in opening master pty (fd = %d).",
324 script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor());
326 StreamString run_string;
327 char error_str[1024];
328 const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
329 if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
331 ScriptInterpreterPython::Locker locker(script_interpreter,
332 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
333 ScriptInterpreterPython::Locker::FreeAcquiredLock);
334 run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str());
335 PyRun_SimpleString (run_string.GetData());
338 run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str());
339 PyRun_SimpleString (run_string.GetData());
342 run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str());
343 PyRun_SimpleString (run_string.GetData());
346 run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(),
348 PyRun_SimpleString (run_string.GetData());
352 embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.noninteractive-python>",
353 ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader,
354 script_interpreter, NULL);
355 if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread))
358 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread);
360 Host::ThreadDetach (embedded_interpreter_thread, &detach_error);
365 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed in creating thread");
366 reader.SetIsDone (true);
372 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed to open master pty ");
373 reader.SetIsDone (true);
378 case eInputReaderDeactivate:
379 // When another input reader is pushed, don't leave the session...
380 //script_interpreter->LeaveSession ();
383 case eInputReaderReactivate:
385 // ScriptInterpreterPython::Locker locker(script_interpreter,
386 // ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession,
387 // ScriptInterpreterPython::Locker::FreeAcquiredLock);
391 case eInputReaderAsynchronousOutputWritten:
394 case eInputReaderInterrupt:
396 PyThreadState* state = _PyThreadState_Current;
398 state = script_interpreter->m_command_thread_state;
401 long tid = state->thread_id;
402 _PyThreadState_Current = state;
403 int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);
405 log->Printf("ScriptInterpreterPython::NonInteractiveInputReaderCallback, eInputReaderInterrupt, tid = %ld, num_threads = %d, state = %p",
406 tid,num_threads,state);
409 log->Printf("ScriptInterpreterPython::NonInteractiveInputReaderCallback, eInputReaderInterrupt, state = NULL");
413 case eInputReaderEndOfFile:
414 reader.SetIsDone(true);
417 case eInputReaderGotToken:
418 if (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor() != -1)
421 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes,
423 if (bytes && bytes_len)
424 ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), bytes, bytes_len);
425 ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), "\n", 1);
430 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.",
433 reader.SetIsDone (true);
437 case eInputReaderDone:
439 StreamString run_string;
440 char error_str[1024];
441 const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
442 if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
444 ScriptInterpreterPython::Locker locker(script_interpreter,
445 ScriptInterpreterPython::Locker::AcquireLock,
446 ScriptInterpreterPython::Locker::FreeAcquiredLock);
447 run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin; sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str());
448 PyRun_SimpleString (run_string.GetData());
451 // Restore terminal settings if they were validly saved
453 log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Done, closing down input reader.");
455 script_interpreter->RestoreTerminalState ();
457 script_interpreter->m_embedded_thread_pty.CloseMasterFileDescriptor();
465 ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) :
466 ScriptInterpreter (interpreter, eScriptLanguagePython),
467 m_embedded_thread_pty (),
468 m_embedded_python_pty (),
469 m_embedded_thread_input_reader_sp (),
470 m_embedded_python_input_reader_sp (),
471 m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()),
475 m_run_one_line (NULL),
476 m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()),
478 m_session_is_active (false),
479 m_valid_session (true),
480 m_command_thread_state (NULL)
483 static int g_initialized = false;
487 g_initialized = true;
488 ScriptInterpreterPython::InitializePrivate ();
491 m_dictionary_name.append("_dict");
492 StreamString run_string;
493 run_string.Printf ("%s = dict()", m_dictionary_name.c_str());
496 ScriptInterpreterPython::Locker::AcquireLock,
497 ScriptInterpreterPython::Locker::FreeAcquiredLock);
498 PyRun_SimpleString (run_string.GetData());
502 // Importing 'lldb' module calls SBDebugger::Initialize, which calls Debugger::Initialize, which increments a
503 // global debugger ref-count; therefore we need to check the ref-count before and after importing lldb, and if the
504 // ref-count increased we need to call Debugger::Terminate here to decrement the ref-count so that when the final
505 // call to Debugger::Terminate is made, the ref-count has the correct value.
507 // Bonus question: Why doesn't the ref-count always increase? Because sometimes lldb has already been imported, in
508 // which case the code inside it, including the call to SBDebugger::Initialize(), does not get executed.
510 int old_count = Debugger::TestDebuggerRefCount();
512 run_string.Printf ("run_one_line (%s, 'import copy, os, re, sys, uuid, lldb')", m_dictionary_name.c_str());
513 PyRun_SimpleString (run_string.GetData());
515 // WARNING: temporary code that loads Cocoa formatters - this should be done on a per-platform basis rather than loading the whole set
516 // and letting the individual formatter classes exploit APIs to check whether they can/cannot do their task
518 run_string.Printf ("run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp, pydoc')", m_dictionary_name.c_str());
519 PyRun_SimpleString (run_string.GetData());
521 int new_count = Debugger::TestDebuggerRefCount();
523 if (new_count > old_count)
524 Debugger::Terminate();
527 run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", m_dictionary_name.c_str(),
528 interpreter.GetDebugger().GetID());
529 PyRun_SimpleString (run_string.GetData());
531 if (m_dbg_stdout != NULL)
533 m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush);
536 // get the output file handle from the debugger (if any)
537 File& out_file = interpreter.GetDebugger().GetOutputFile();
538 if (out_file.IsValid())
539 ResetOutputFileHandle(out_file.GetStream());
542 ScriptInterpreterPython::~ScriptInterpreterPython ()
544 Debugger &debugger = GetCommandInterpreter().GetDebugger();
546 if (m_embedded_thread_input_reader_sp.get() != NULL)
548 m_embedded_thread_input_reader_sp->SetIsDone (true);
549 m_embedded_thread_pty.CloseSlaveFileDescriptor();
550 const InputReaderSP reader_sp = m_embedded_thread_input_reader_sp;
551 debugger.PopInputReader (reader_sp);
552 m_embedded_thread_input_reader_sp.reset();
555 if (m_embedded_python_input_reader_sp.get() != NULL)
557 m_embedded_python_input_reader_sp->SetIsDone (true);
558 m_embedded_python_pty.CloseSlaveFileDescriptor();
559 const InputReaderSP reader_sp = m_embedded_python_input_reader_sp;
560 debugger.PopInputReader (reader_sp);
561 m_embedded_python_input_reader_sp.reset();
567 ScriptInterpreterPython::Locker::AcquireLock,
568 ScriptInterpreterPython::Locker::FreeLock);
569 Py_XDECREF ((PyObject*)m_new_sysout);
574 ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh)
582 ScriptInterpreterPython::Locker::AcquireLock,
583 ScriptInterpreterPython::Locker::FreeAcquiredLock);
585 m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush);
589 ScriptInterpreterPython::SaveTerminalState (int fd)
591 // Python mucks with the terminal state of STDIN. If we can possibly avoid
592 // this by setting the file handles up correctly prior to entering the
593 // interpreter we should. For now we save and restore the terminal state
594 // on the input file handle.
595 m_terminal_state.Save (fd, false);
599 ScriptInterpreterPython::RestoreTerminalState ()
601 // Python mucks with the terminal state of STDIN. If we can possibly avoid
602 // this by setting the file handles up correctly prior to entering the
603 // interpreter we should. For now we save and restore the terminal state
604 // on the input file handle.
605 m_terminal_state.Restore();
609 ScriptInterpreterPython::LeaveSession ()
611 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
613 log->PutCString("ScriptInterpreterPython::LeaveSession()");
615 // checking that we have a valid thread state - since we use our own threading and locking
616 // in some (rare) cases during cleanup Python may end up believing we have no thread state
617 // and PyImport_AddModule will crash if that is the case - since that seems to only happen
618 // when destroying the SBDebugger, we can make do without clearing up stdout and stderr
620 // rdar://problem/11292882
621 // When the current thread state is NULL, PyThreadState_Get() issues a fatal error.
622 if (PyThreadState_GetDict())
624 PyObject *sysmod = PyImport_AddModule ("sys");
625 PyObject *sysdict = PyModule_GetDict (sysmod);
627 if (m_new_sysout && sysmod && sysdict)
630 PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout);
632 PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_syserr);
636 m_session_is_active = false;
640 ScriptInterpreterPython::EnterSession (bool init_lldb_globals)
642 // If we have already entered the session, without having officially 'left' it, then there is no need to
644 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
645 if (m_session_is_active)
648 log->Printf("ScriptInterpreterPython::EnterSession(init_lldb_globals=%i) session is already active, returning without doing anything", init_lldb_globals);
653 log->Printf("ScriptInterpreterPython::EnterSession(init_lldb_globals=%i)", init_lldb_globals);
656 m_session_is_active = true;
658 StreamString run_string;
660 if (init_lldb_globals)
662 run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
663 run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
664 run_string.PutCString ("; lldb.target = lldb.debugger.GetSelectedTarget()");
665 run_string.PutCString ("; lldb.process = lldb.target.GetProcess()");
666 run_string.PutCString ("; lldb.thread = lldb.process.GetSelectedThread ()");
667 run_string.PutCString ("; lldb.frame = lldb.thread.GetSelectedFrame ()");
668 run_string.PutCString ("')");
672 // If we aren't initing the globals, we should still always set the debugger (since that is always unique.)
673 run_string.Printf ( "run_one_line (%s, \"lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
674 run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
675 run_string.PutCString ("\")");
678 PyRun_SimpleString (run_string.GetData());
681 PyObject *sysmod = PyImport_AddModule ("sys");
682 PyObject *sysdict = PyModule_GetDict (sysmod);
684 if (m_new_sysout && sysmod && sysdict)
686 m_old_sysout = PyDict_GetItemString(sysdict, "stdout");
687 m_old_syserr = PyDict_GetItemString(sysdict, "stderr");
690 PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_new_sysout);
691 PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_new_sysout);
695 if (PyErr_Occurred())
702 FindSessionDictionary (const char* dict_name)
704 static std::map<ConstString,PyObject*> g_dict_map;
706 ConstString dict(dict_name);
708 std::map<ConstString,PyObject*>::iterator iter = g_dict_map.find(dict);
710 if (iter != g_dict_map.end())
713 PyObject *main_mod = PyImport_AddModule ("__main__");
714 if (main_mod != NULL)
716 PyObject *main_dict = PyModule_GetDict (main_mod);
717 if ((main_dict != NULL)
718 && PyDict_Check (main_dict))
720 // Go through the main dictionary looking for the correct python script interpreter dictionary
721 PyObject *key, *value;
724 while (PyDict_Next (main_dict, &pos, &key, &value))
726 // We have stolen references to the key and value objects in the dictionary; we need to increment
727 // them now so that Python's garbage collector doesn't collect them out from under us.
730 if (strcmp (PyString_AsString (key), dict_name) == 0)
732 g_dict_map[dict] = value;
742 GenerateUniqueName (const char* base_name_wanted,
743 uint32_t& functions_counter,
744 void* name_token = NULL)
748 if (!base_name_wanted)
749 return std::string();
752 sstr.Printf ("%s_%d", base_name_wanted, functions_counter++);
754 sstr.Printf ("%s_%p", base_name_wanted, name_token);
756 return sstr.GetString();
760 ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options)
762 if (!m_valid_session)
765 // We want to call run_one_line, passing in the dictionary and the command string. We cannot do this through
766 // PyRun_SimpleString here because the command string may contain escaped characters, and putting it inside
767 // another string to pass to PyRun_SimpleString messes up the escaping. So we use the following more complicated
768 // method to pass the command string directly down to Python.
771 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
772 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
774 bool success = false;
778 // Find the correct script interpreter dictionary in the main module.
779 PyObject *script_interpreter_dict = FindSessionDictionary(m_dictionary_name.c_str());
780 if (script_interpreter_dict != NULL)
782 PyObject *pfunc = (PyObject*)m_run_one_line;
783 PyObject *pmod = PyImport_AddModule ("lldb.embedded_interpreter");
786 PyObject *pmod_dict = PyModule_GetDict (pmod);
787 if ((pmod_dict != NULL)
788 && PyDict_Check (pmod_dict))
792 PyObject *key, *value;
795 while (PyDict_Next (pmod_dict, &pos, &key, &value))
799 if (strcmp (PyString_AsString (key), "run_one_line") == 0)
805 m_run_one_line = pfunc;
808 if (pfunc && PyCallable_Check (pfunc))
810 PyObject *pargs = Py_BuildValue("(Os)",script_interpreter_dict,command);
813 PyObject *pvalue = NULL;
814 { // scope for PythonInputReaderManager
815 PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
816 pvalue = PyObject_CallObject (pfunc, pargs);
824 else if (options.GetMaskoutErrors() && PyErr_Occurred ())
833 Py_INCREF (script_interpreter_dict);
839 // The one-liner failed. Append the error message.
841 result->AppendErrorWithFormat ("python failed attempting to evaluate '%s'\n", command);
846 result->AppendError ("empty command passed to python\n");
851 ScriptInterpreterPython::InputReaderCallback
855 InputReaderAction notification,
860 lldb::thread_t embedded_interpreter_thread;
861 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
866 ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
868 if (script_interpreter->m_script_lang != eScriptLanguagePython)
871 switch (notification)
873 case eInputReaderActivate:
875 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
876 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
879 out_stream->Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n");
883 // Save terminal settings if we can
884 int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor();
885 if (input_fd == File::kInvalidDescriptor)
886 input_fd = STDIN_FILENO;
888 script_interpreter->SaveTerminalState(input_fd);
891 ScriptInterpreterPython::Locker locker(script_interpreter,
892 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
893 ScriptInterpreterPython::Locker::FreeAcquiredLock);
896 char error_str[1024];
897 if (script_interpreter->m_embedded_python_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str,
901 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in opening master pty (fd = %d).",
902 script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor());
903 embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.embedded-python-loop>",
904 ScriptInterpreterPython::RunEmbeddedPythonInterpreter,
905 script_interpreter, NULL);
906 if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread))
909 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread);
911 Host::ThreadDetach (embedded_interpreter_thread, &detach_error);
916 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed in creating thread");
917 reader.SetIsDone (true);
923 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed to open master pty ");
924 reader.SetIsDone (true);
929 case eInputReaderDeactivate:
930 // When another input reader is pushed, don't leave the session...
931 //script_interpreter->LeaveSession ();
934 case eInputReaderReactivate:
936 ScriptInterpreterPython::Locker locker (script_interpreter,
937 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession,
938 ScriptInterpreterPython::Locker::FreeAcquiredLock);
942 case eInputReaderAsynchronousOutputWritten:
945 case eInputReaderInterrupt:
946 ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "raise KeyboardInterrupt\n", 24);
949 case eInputReaderEndOfFile:
950 ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()\n", 7);
953 case eInputReaderGotToken:
954 if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1)
957 log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes,
959 if (bytes && bytes_len)
961 if ((int) bytes[0] == 4)
962 ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()", 6);
964 ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), bytes, bytes_len);
966 ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "\n", 1);
971 log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.",
974 reader.SetIsDone (true);
979 case eInputReaderDone:
981 Locker locker(script_interpreter,
982 ScriptInterpreterPython::Locker::AcquireLock,
983 ScriptInterpreterPython::Locker::FreeAcquiredLock);
984 script_interpreter->LeaveSession ();
987 // Restore terminal settings if they were validly saved
989 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Done, closing down input reader.");
991 script_interpreter->RestoreTerminalState ();
993 script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor();
1002 ScriptInterpreterPython::ExecuteInterpreterLoop ()
1004 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1006 Debugger &debugger = GetCommandInterpreter().GetDebugger();
1008 // At the moment, the only time the debugger does not have an input file handle is when this is called
1009 // directly from Python, in which case it is both dangerous and unnecessary (not to mention confusing) to
1010 // try to embed a running interpreter loop inside the already running Python interpreter loop, so we won't
1013 if (!debugger.GetInputFile().IsValid())
1016 InputReaderSP reader_sp (new InputReader(debugger));
1019 Error error (reader_sp->Initialize (ScriptInterpreterPython::InputReaderCallback,
1021 eInputReaderGranularityLine, // token size, to pass to callback function
1024 true)); // echo input
1026 if (error.Success())
1028 debugger.PushInputReader (reader_sp);
1029 m_embedded_python_input_reader_sp = reader_sp;
1035 ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
1036 ScriptInterpreter::ScriptReturnType return_type,
1038 const ExecuteScriptOptions &options)
1042 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
1043 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1045 PyObject *py_return = NULL;
1046 PyObject *mainmod = PyImport_AddModule ("__main__");
1047 PyObject *globals = PyModule_GetDict (mainmod);
1048 PyObject *locals = NULL;
1049 PyObject *py_error = NULL;
1050 bool ret_success = false;
1051 bool should_decrement_locals = false;
1054 locals = FindSessionDictionary(m_dictionary_name.c_str());
1058 locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str());
1059 should_decrement_locals = true;
1065 should_decrement_locals = false;
1068 py_error = PyErr_Occurred();
1069 if (py_error != NULL)
1072 if (in_string != NULL)
1074 { // scope for PythonInputReaderManager
1075 PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
1076 py_return = PyRun_String (in_string, Py_eval_input, globals, locals);
1077 if (py_return == NULL)
1079 py_error = PyErr_Occurred ();
1080 if (py_error != NULL)
1083 py_return = PyRun_String (in_string, Py_single_input, globals, locals);
1088 && should_decrement_locals)
1089 Py_XDECREF (locals);
1091 if (py_return != NULL)
1093 switch (return_type)
1095 case eScriptReturnTypeCharPtr: // "char *"
1097 const char format[3] = "s#";
1098 success = PyArg_Parse (py_return, format, (char **) ret_value);
1101 case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return == Py_None
1103 const char format[3] = "z";
1104 success = PyArg_Parse (py_return, format, (char **) ret_value);
1107 case eScriptReturnTypeBool:
1109 const char format[2] = "b";
1110 success = PyArg_Parse (py_return, format, (bool *) ret_value);
1113 case eScriptReturnTypeShortInt:
1115 const char format[2] = "h";
1116 success = PyArg_Parse (py_return, format, (short *) ret_value);
1119 case eScriptReturnTypeShortIntUnsigned:
1121 const char format[2] = "H";
1122 success = PyArg_Parse (py_return, format, (unsigned short *) ret_value);
1125 case eScriptReturnTypeInt:
1127 const char format[2] = "i";
1128 success = PyArg_Parse (py_return, format, (int *) ret_value);
1131 case eScriptReturnTypeIntUnsigned:
1133 const char format[2] = "I";
1134 success = PyArg_Parse (py_return, format, (unsigned int *) ret_value);
1137 case eScriptReturnTypeLongInt:
1139 const char format[2] = "l";
1140 success = PyArg_Parse (py_return, format, (long *) ret_value);
1143 case eScriptReturnTypeLongIntUnsigned:
1145 const char format[2] = "k";
1146 success = PyArg_Parse (py_return, format, (unsigned long *) ret_value);
1149 case eScriptReturnTypeLongLong:
1151 const char format[2] = "L";
1152 success = PyArg_Parse (py_return, format, (long long *) ret_value);
1155 case eScriptReturnTypeLongLongUnsigned:
1157 const char format[2] = "K";
1158 success = PyArg_Parse (py_return, format, (unsigned long long *) ret_value);
1161 case eScriptReturnTypeFloat:
1163 const char format[2] = "f";
1164 success = PyArg_Parse (py_return, format, (float *) ret_value);
1167 case eScriptReturnTypeDouble:
1169 const char format[2] = "d";
1170 success = PyArg_Parse (py_return, format, (double *) ret_value);
1173 case eScriptReturnTypeChar:
1175 const char format[2] = "c";
1176 success = PyArg_Parse (py_return, format, (char *) ret_value);
1180 Py_XDECREF (py_return);
1184 ret_success = false;
1188 py_error = PyErr_Occurred();
1189 if (py_error != NULL)
1191 ret_success = false;
1192 if (options.GetMaskoutErrors())
1194 if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
1204 ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, const ExecuteScriptOptions &options)
1209 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
1210 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1212 bool success = false;
1213 PyObject *py_return = NULL;
1214 PyObject *mainmod = PyImport_AddModule ("__main__");
1215 PyObject *globals = PyModule_GetDict (mainmod);
1216 PyObject *locals = NULL;
1217 PyObject *py_error = NULL;
1218 bool should_decrement_locals = false;
1220 locals = FindSessionDictionary(m_dictionary_name.c_str());
1224 locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str());
1225 should_decrement_locals = true;
1231 should_decrement_locals = false;
1234 py_error = PyErr_Occurred();
1235 if (py_error != NULL)
1238 if (in_string != NULL)
1240 struct _node *compiled_node = PyParser_SimpleParseString (in_string, Py_file_input);
1243 PyCodeObject *compiled_code = PyNode_Compile (compiled_node, "temp.py");
1246 { // scope for PythonInputReaderManager
1247 PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
1248 py_return = PyEval_EvalCode (compiled_code, globals, locals);
1250 if (py_return != NULL)
1253 Py_XDECREF (py_return);
1255 if (locals && should_decrement_locals)
1256 Py_XDECREF (locals);
1261 py_error = PyErr_Occurred ();
1262 if (py_error != NULL)
1265 if (options.GetMaskoutErrors())
1267 if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
1276 static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.";
1278 static const char *g_bkpt_command_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.\n"
1279 "def function(frame,bp_loc,internal_dict):\n"
1280 " \"\"\"frame: the SBFrame for the location at which you stopped\n"
1281 " bp_loc: an SBBreakpointLocation for the breakpoint location information\n"
1282 " internal_dict: an LLDB support object not to be used\"\"\"";
1285 ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback
1288 InputReader &reader,
1289 InputReaderAction notification,
1294 static StringList commands_in_progress;
1296 switch (notification)
1298 case eInputReaderActivate:
1301 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1302 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1303 commands_in_progress.Clear();
1306 out_stream->Printf ("%s\n", g_bkpt_command_reader_instructions);
1307 if (reader.GetPrompt())
1308 out_stream->Printf ("%s", reader.GetPrompt());
1309 out_stream->Flush ();
1314 case eInputReaderDeactivate:
1317 case eInputReaderReactivate:
1319 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1320 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1321 if (reader.GetPrompt() && !batch_mode)
1323 out_stream->Printf ("%s", reader.GetPrompt());
1324 out_stream->Flush ();
1329 case eInputReaderAsynchronousOutputWritten:
1332 case eInputReaderGotToken:
1334 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1335 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1336 std::string temp_string (bytes, bytes_len);
1337 commands_in_progress.AppendString (temp_string.c_str());
1338 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1340 out_stream->Printf ("%s", reader.GetPrompt());
1341 out_stream->Flush ();
1346 case eInputReaderEndOfFile:
1347 case eInputReaderInterrupt:
1348 // Control-c (SIGINT) & control-d both mean finish & exit.
1349 reader.SetIsDone(true);
1351 // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command.
1352 if (notification == eInputReaderInterrupt)
1353 commands_in_progress.Clear();
1355 // Fall through here...
1357 case eInputReaderDone:
1359 bool batch_mode = notification == eInputReaderDone ?
1360 reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode() :
1362 BreakpointOptions *bp_options = (BreakpointOptions *)baton;
1363 std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1364 data_ap->user_source.AppendList (commands_in_progress);
1367 ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1370 if (interpreter->GenerateBreakpointCommandCallbackData (data_ap->user_source,
1371 data_ap->script_source))
1373 BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
1374 bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
1376 else if (!batch_mode)
1378 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1379 out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1380 out_stream->Flush();
1387 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1388 out_stream->Printf ("Warning: Unable to find script intepreter; no command attached to breakpoint.\n");
1389 out_stream->Flush();
1402 ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback
1405 InputReader &reader,
1406 InputReaderAction notification,
1411 static StringList commands_in_progress;
1413 switch (notification)
1415 case eInputReaderActivate:
1417 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1418 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1420 commands_in_progress.Clear();
1423 out_stream->Printf ("%s\n", g_reader_instructions);
1424 if (reader.GetPrompt())
1425 out_stream->Printf ("%s", reader.GetPrompt());
1426 out_stream->Flush ();
1431 case eInputReaderDeactivate:
1434 case eInputReaderReactivate:
1436 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1437 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1438 if (reader.GetPrompt() && !batch_mode)
1440 out_stream->Printf ("%s", reader.GetPrompt());
1441 out_stream->Flush ();
1446 case eInputReaderAsynchronousOutputWritten:
1449 case eInputReaderGotToken:
1451 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1452 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1453 std::string temp_string (bytes, bytes_len);
1454 commands_in_progress.AppendString (temp_string.c_str());
1455 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1457 out_stream->Printf ("%s", reader.GetPrompt());
1458 out_stream->Flush ();
1463 case eInputReaderEndOfFile:
1464 case eInputReaderInterrupt:
1465 // Control-c (SIGINT) & control-d both mean finish & exit.
1466 reader.SetIsDone(true);
1468 // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command.
1469 if (notification == eInputReaderInterrupt)
1470 commands_in_progress.Clear();
1472 // Fall through here...
1474 case eInputReaderDone:
1476 bool batch_mode = notification == eInputReaderDone ?
1477 reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode() :
1479 WatchpointOptions *wp_options = (WatchpointOptions *)baton;
1480 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1481 data_ap->user_source.AppendList (commands_in_progress);
1484 ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1487 if (interpreter->GenerateWatchpointCommandCallbackData (data_ap->user_source,
1488 data_ap->script_source))
1490 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
1491 wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
1493 else if (!batch_mode)
1495 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1496 out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1497 out_stream->Flush();
1504 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1505 out_stream->Printf ("Warning: Unable to find script intepreter; no command attached to breakpoint.\n");
1506 out_stream->Flush();
1519 ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
1520 CommandReturnObject &result)
1522 Debugger &debugger = GetCommandInterpreter().GetDebugger();
1524 InputReaderSP reader_sp (new InputReader (debugger));
1528 Error err = reader_sp->Initialize (
1529 ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback,
1530 bp_options, // baton
1531 eInputReaderGranularityLine, // token size, for feeding data to callback function
1532 "DONE", // end token
1534 true); // echo input
1537 debugger.PushInputReader (reader_sp);
1540 result.AppendError (err.AsCString());
1541 result.SetStatus (eReturnStatusFailed);
1546 result.AppendError("out of memory");
1547 result.SetStatus (eReturnStatusFailed);
1552 ScriptInterpreterPython::CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
1553 CommandReturnObject &result)
1555 Debugger &debugger = GetCommandInterpreter().GetDebugger();
1557 InputReaderSP reader_sp (new InputReader (debugger));
1561 Error err = reader_sp->Initialize (
1562 ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback,
1563 wp_options, // baton
1564 eInputReaderGranularityLine, // token size, for feeding data to callback function
1565 "DONE", // end token
1567 true); // echo input
1570 debugger.PushInputReader (reader_sp);
1573 result.AppendError (err.AsCString());
1574 result.SetStatus (eReturnStatusFailed);
1579 result.AppendError("out of memory");
1580 result.SetStatus (eReturnStatusFailed);
1584 // Set a Python one-liner as the callback for the breakpoint.
1586 ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
1587 const char *oneliner)
1589 std::unique_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1591 // It's necessary to set both user_source and script_source to the oneliner.
1592 // The former is used to generate callback description (as in breakpoint command list)
1593 // while the latter is used for Python to interpret during the actual callback.
1595 data_ap->user_source.AppendString (oneliner);
1596 data_ap->script_source.assign (oneliner);
1598 if (GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
1600 BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
1601 bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
1607 // Set a Python one-liner as the callback for the watchpoint.
1609 ScriptInterpreterPython::SetWatchpointCommandCallback (WatchpointOptions *wp_options,
1610 const char *oneliner)
1612 std::unique_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1614 // It's necessary to set both user_source and script_source to the oneliner.
1615 // The former is used to generate callback description (as in watchpoint command list)
1616 // while the latter is used for Python to interpret during the actual callback.
1618 data_ap->user_source.AppendString (oneliner);
1619 data_ap->script_source.assign (oneliner);
1621 if (GenerateWatchpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
1623 BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
1624 wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
1631 ScriptInterpreterPython::ExportFunctionDefinitionToInterpreter (StringList &function_def)
1633 // Convert StringList to one long, newline delimited, const char *.
1634 std::string function_def_string(function_def.CopyList());
1636 return ExecuteMultipleLines (function_def_string.c_str(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false));
1640 ScriptInterpreterPython::GenerateFunction(const char *signature, const StringList &input)
1642 int num_lines = input.GetSize ();
1646 if (!signature || *signature == 0)
1650 StringList auto_generated_function;
1651 auto_generated_function.AppendString (signature);
1652 auto_generated_function.AppendString (" global_dict = globals()"); // Grab the global dictionary
1653 auto_generated_function.AppendString (" new_keys = internal_dict.keys()"); // Make a list of keys in the session dict
1654 auto_generated_function.AppendString (" old_keys = global_dict.keys()"); // Save list of keys in global dict
1655 auto_generated_function.AppendString (" global_dict.update (internal_dict)"); // Add the session dictionary to the
1656 // global dictionary.
1658 // Wrap everything up inside the function, increasing the indentation.
1660 auto_generated_function.AppendString(" if True:");
1661 for (int i = 0; i < num_lines; ++i)
1664 sstr.Printf (" %s", input.GetStringAtIndex (i));
1665 auto_generated_function.AppendString (sstr.GetData());
1667 auto_generated_function.AppendString (" for key in new_keys:"); // Iterate over all the keys from session dict
1668 auto_generated_function.AppendString (" internal_dict[key] = global_dict[key]"); // Update session dict values
1669 auto_generated_function.AppendString (" if key not in old_keys:"); // If key was not originally in global dict
1670 auto_generated_function.AppendString (" del global_dict[key]"); // ...then remove key/value from global dict
1672 // Verify that the results are valid Python.
1674 if (!ExportFunctionDefinitionToInterpreter (auto_generated_function))
1682 ScriptInterpreterPython::GenerateTypeScriptFunction (StringList &user_input, std::string& output, void* name_token)
1684 static uint32_t num_created_functions = 0;
1685 user_input.RemoveBlankLines ();
1688 // Check to see if we have any data; if not, just return.
1689 if (user_input.GetSize() == 0)
1692 // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the
1693 // ValueObject as parameter to the function.
1695 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_type_print_func", num_created_functions, name_token));
1696 sstr.Printf ("def %s (valobj, internal_dict):", auto_generated_function_name.c_str());
1698 if (!GenerateFunction(sstr.GetData(), user_input))
1701 // Store the name of the auto-generated function to be called.
1702 output.assign(auto_generated_function_name);
1707 ScriptInterpreterPython::GenerateScriptAliasFunction (StringList &user_input, std::string &output)
1709 static uint32_t num_created_functions = 0;
1710 user_input.RemoveBlankLines ();
1713 // Check to see if we have any data; if not, just return.
1714 if (user_input.GetSize() == 0)
1717 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_cmd_alias_func", num_created_functions));
1719 sstr.Printf ("def %s (debugger, args, result, internal_dict):", auto_generated_function_name.c_str());
1721 if (!GenerateFunction(sstr.GetData(),user_input))
1724 // Store the name of the auto-generated function to be called.
1725 output.assign(auto_generated_function_name);
1731 ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, std::string &output, void* name_token)
1733 static uint32_t num_created_classes = 0;
1734 user_input.RemoveBlankLines ();
1735 int num_lines = user_input.GetSize ();
1738 // Check to see if we have any data; if not, just return.
1739 if (user_input.GetSize() == 0)
1742 // Wrap all user input into a Python class
1744 std::string auto_generated_class_name(GenerateUniqueName("lldb_autogen_python_type_synth_class",num_created_classes,name_token));
1746 StringList auto_generated_class;
1748 // Create the function name & definition string.
1750 sstr.Printf ("class %s:", auto_generated_class_name.c_str());
1751 auto_generated_class.AppendString (sstr.GetData());
1753 // Wrap everything up inside the class, increasing the indentation.
1754 // we don't need to play any fancy indentation tricks here because there is no
1755 // surrounding code whose indentation we need to honor
1756 for (int i = 0; i < num_lines; ++i)
1759 sstr.Printf (" %s", user_input.GetStringAtIndex (i));
1760 auto_generated_class.AppendString (sstr.GetData());
1764 // Verify that the results are valid Python.
1765 // (even though the method is ExportFunctionDefinitionToInterpreter, a class will actually be exported)
1766 // (TODO: rename that method to ExportDefinitionToInterpreter)
1767 if (!ExportFunctionDefinitionToInterpreter (auto_generated_class))
1770 // Store the name of the auto-generated class
1772 output.assign(auto_generated_class_name);
1776 lldb::ScriptInterpreterObjectSP
1777 ScriptInterpreterPython::OSPlugin_CreatePluginObject (const char *class_name, lldb::ProcessSP process_sp)
1779 if (class_name == NULL || class_name[0] == '\0')
1780 return lldb::ScriptInterpreterObjectSP();
1783 return lldb::ScriptInterpreterObjectSP();
1788 Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1789 ret_val = g_swig_create_os_plugin (class_name,
1790 m_dictionary_name.c_str(),
1794 return MakeScriptObject(ret_val);
1797 lldb::ScriptInterpreterObjectSP
1798 ScriptInterpreterPython::OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
1800 Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1802 static char callee_name[] = "get_register_info";
1804 if (!os_plugin_object_sp)
1805 return lldb::ScriptInterpreterObjectSP();
1807 PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1809 if (implementor == NULL || implementor == Py_None)
1810 return lldb::ScriptInterpreterObjectSP();
1812 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
1814 if (PyErr_Occurred())
1819 if (pmeth == NULL || pmeth == Py_None)
1822 return lldb::ScriptInterpreterObjectSP();
1825 if (PyCallable_Check(pmeth) == 0)
1827 if (PyErr_Occurred())
1833 return lldb::ScriptInterpreterObjectSP();
1836 if (PyErr_Occurred())
1843 // right now we know this function exists and is callable..
1844 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
1846 // if it fails, print the error but otherwise go on
1847 if (PyErr_Occurred())
1853 return MakeScriptObject(py_return);
1856 lldb::ScriptInterpreterObjectSP
1857 ScriptInterpreterPython::OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
1859 Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1861 static char callee_name[] = "get_thread_info";
1863 if (!os_plugin_object_sp)
1864 return lldb::ScriptInterpreterObjectSP();
1866 PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1868 if (implementor == NULL || implementor == Py_None)
1869 return lldb::ScriptInterpreterObjectSP();
1871 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
1873 if (PyErr_Occurred())
1878 if (pmeth == NULL || pmeth == Py_None)
1881 return lldb::ScriptInterpreterObjectSP();
1884 if (PyCallable_Check(pmeth) == 0)
1886 if (PyErr_Occurred())
1892 return lldb::ScriptInterpreterObjectSP();
1895 if (PyErr_Occurred())
1902 // right now we know this function exists and is callable..
1903 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
1905 // if it fails, print the error but otherwise go on
1906 if (PyErr_Occurred())
1912 return MakeScriptObject(py_return);
1915 // GetPythonValueFormatString provides a system independent type safe way to
1916 // convert a variable's type into a python value format. Python value formats
1917 // are defined in terms of builtin C types and could change from system to
1918 // as the underlying typedef for uint* types, size_t, off_t and other values
1921 template <typename T>
1922 const char *GetPythonValueFormatString(T t)
1924 assert(!"Unhandled type passed to GetPythonValueFormatString(T), make a specialization of GetPythonValueFormatString() to support this type.");
1927 template <> const char *GetPythonValueFormatString (char *) { return "s"; }
1928 template <> const char *GetPythonValueFormatString (char) { return "b"; }
1929 template <> const char *GetPythonValueFormatString (unsigned char) { return "B"; }
1930 template <> const char *GetPythonValueFormatString (short) { return "h"; }
1931 template <> const char *GetPythonValueFormatString (unsigned short) { return "H"; }
1932 template <> const char *GetPythonValueFormatString (int) { return "i"; }
1933 template <> const char *GetPythonValueFormatString (unsigned int) { return "I"; }
1934 template <> const char *GetPythonValueFormatString (long) { return "l"; }
1935 template <> const char *GetPythonValueFormatString (unsigned long) { return "k"; }
1936 template <> const char *GetPythonValueFormatString (long long) { return "L"; }
1937 template <> const char *GetPythonValueFormatString (unsigned long long) { return "K"; }
1938 template <> const char *GetPythonValueFormatString (float t) { return "f"; }
1939 template <> const char *GetPythonValueFormatString (double t) { return "d"; }
1941 lldb::ScriptInterpreterObjectSP
1942 ScriptInterpreterPython::OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
1945 Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1947 static char callee_name[] = "get_register_data";
1948 static char *param_format = const_cast<char *>(GetPythonValueFormatString(tid));
1950 if (!os_plugin_object_sp)
1951 return lldb::ScriptInterpreterObjectSP();
1953 PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1955 if (implementor == NULL || implementor == Py_None)
1956 return lldb::ScriptInterpreterObjectSP();
1958 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
1960 if (PyErr_Occurred())
1965 if (pmeth == NULL || pmeth == Py_None)
1968 return lldb::ScriptInterpreterObjectSP();
1971 if (PyCallable_Check(pmeth) == 0)
1973 if (PyErr_Occurred())
1979 return lldb::ScriptInterpreterObjectSP();
1982 if (PyErr_Occurred())
1989 // right now we know this function exists and is callable..
1990 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, tid);
1992 // if it fails, print the error but otherwise go on
1993 if (PyErr_Occurred())
1999 return MakeScriptObject(py_return);
2002 lldb::ScriptInterpreterObjectSP
2003 ScriptInterpreterPython::OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
2005 lldb::addr_t context)
2007 Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
2009 static char callee_name[] = "create_thread";
2010 std::string param_format;
2011 param_format += GetPythonValueFormatString(tid);
2012 param_format += GetPythonValueFormatString(context);
2014 if (!os_plugin_object_sp)
2015 return lldb::ScriptInterpreterObjectSP();
2017 PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
2019 if (implementor == NULL || implementor == Py_None)
2020 return lldb::ScriptInterpreterObjectSP();
2022 PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name);
2024 if (PyErr_Occurred())
2029 if (pmeth == NULL || pmeth == Py_None)
2032 return lldb::ScriptInterpreterObjectSP();
2035 if (PyCallable_Check(pmeth) == 0)
2037 if (PyErr_Occurred())
2043 return lldb::ScriptInterpreterObjectSP();
2046 if (PyErr_Occurred())
2053 // right now we know this function exists and is callable..
2054 PyObject* py_return = PyObject_CallMethod(implementor, callee_name, ¶m_format[0], tid, context);
2056 // if it fails, print the error but otherwise go on
2057 if (PyErr_Occurred())
2063 return MakeScriptObject(py_return);
2066 lldb::ScriptInterpreterObjectSP
2067 ScriptInterpreterPython::CreateSyntheticScriptedProvider (const char *class_name,
2068 lldb::ValueObjectSP valobj)
2070 if (class_name == NULL || class_name[0] == '\0')
2071 return lldb::ScriptInterpreterObjectSP();
2074 return lldb::ScriptInterpreterObjectSP();
2076 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
2077 Target *target = exe_ctx.GetTargetPtr();
2080 return lldb::ScriptInterpreterObjectSP();
2082 Debugger &debugger = target->GetDebugger();
2083 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2084 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2086 if (!script_interpreter)
2087 return lldb::ScriptInterpreterObjectSP();
2092 Locker py_lock(this);
2093 ret_val = g_swig_synthetic_script (class_name,
2094 python_interpreter->m_dictionary_name.c_str(),
2098 return MakeScriptObject(ret_val);
2102 ScriptInterpreterPython::GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token)
2105 input.SplitIntoLines(oneliner, strlen(oneliner));
2106 return GenerateTypeScriptFunction(input, output, name_token);
2110 ScriptInterpreterPython::GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token)
2113 input.SplitIntoLines(oneliner, strlen(oneliner));
2114 return GenerateTypeSynthClass(input, output, name_token);
2119 ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, std::string& output)
2121 static uint32_t num_created_functions = 0;
2122 user_input.RemoveBlankLines ();
2125 if (user_input.GetSize() == 0)
2128 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_bp_callback_func_",num_created_functions));
2129 sstr.Printf ("def %s (frame, bp_loc, internal_dict):", auto_generated_function_name.c_str());
2131 if (!GenerateFunction(sstr.GetData(), user_input))
2134 // Store the name of the auto-generated function to be called.
2135 output.assign(auto_generated_function_name);
2140 ScriptInterpreterPython::GenerateWatchpointCommandCallbackData (StringList &user_input, std::string& output)
2142 static uint32_t num_created_functions = 0;
2143 user_input.RemoveBlankLines ();
2146 if (user_input.GetSize() == 0)
2149 std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_wp_callback_func_",num_created_functions));
2150 sstr.Printf ("def %s (frame, wp, internal_dict):", auto_generated_function_name.c_str());
2152 if (!GenerateFunction(sstr.GetData(), user_input))
2155 // Store the name of the auto-generated function to be called.
2156 output.assign(auto_generated_function_name);
2161 ScriptInterpreterPython::GetScriptedSummary (const char *python_function_name,
2162 lldb::ValueObjectSP valobj,
2163 lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
2164 std::string& retval)
2167 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
2171 retval.assign("<no object>");
2175 void* old_callee = (callee_wrapper_sp ? callee_wrapper_sp->GetObject() : NULL);
2176 void* new_callee = old_callee;
2179 if (python_function_name
2180 && *python_function_name)
2183 Locker py_lock(this);
2185 Timer scoped_timer ("g_swig_typescript_callback","g_swig_typescript_callback");
2186 ret_val = g_swig_typescript_callback (python_function_name,
2187 FindSessionDictionary(m_dictionary_name.c_str()),
2196 retval.assign("<no function name>");
2200 if (new_callee && old_callee != new_callee)
2201 callee_wrapper_sp = MakeScriptObject(new_callee);
2208 ScriptInterpreterPython::BreakpointCallbackFunction
2211 StoppointCallbackContext *context,
2213 user_id_t break_loc_id
2216 BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton;
2217 const char *python_function_name = bp_option_data->script_source.c_str();
2222 ExecutionContext exe_ctx (context->exe_ctx_ref);
2223 Target *target = exe_ctx.GetTargetPtr();
2228 Debugger &debugger = target->GetDebugger();
2229 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2230 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2232 if (!script_interpreter)
2235 if (python_function_name != NULL
2236 && python_function_name[0] != '\0')
2238 const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2239 BreakpointSP breakpoint_sp = target->GetBreakpointByID (break_id);
2242 const BreakpointLocationSP bp_loc_sp (breakpoint_sp->FindLocationByID (break_loc_id));
2244 if (stop_frame_sp && bp_loc_sp)
2246 bool ret_val = true;
2248 Locker py_lock(python_interpreter);
2249 ret_val = g_swig_breakpoint_callback (python_function_name,
2250 python_interpreter->m_dictionary_name.c_str(),
2258 // We currently always true so we stop in case anything goes wrong when
2259 // trying to call the script function
2264 ScriptInterpreterPython::WatchpointCallbackFunction
2267 StoppointCallbackContext *context,
2271 WatchpointOptions::CommandData *wp_option_data = (WatchpointOptions::CommandData *) baton;
2272 const char *python_function_name = wp_option_data->script_source.c_str();
2277 ExecutionContext exe_ctx (context->exe_ctx_ref);
2278 Target *target = exe_ctx.GetTargetPtr();
2283 Debugger &debugger = target->GetDebugger();
2284 ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2285 ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2287 if (!script_interpreter)
2290 if (python_function_name != NULL
2291 && python_function_name[0] != '\0')
2293 const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2294 WatchpointSP wp_sp = target->GetWatchpointList().FindByID (watch_id);
2297 if (stop_frame_sp && wp_sp)
2299 bool ret_val = true;
2301 Locker py_lock(python_interpreter);
2302 ret_val = g_swig_watchpoint_callback (python_function_name,
2303 python_interpreter->m_dictionary_name.c_str(),
2311 // We currently always true so we stop in case anything goes wrong when
2312 // trying to call the script function
2316 lldb::thread_result_t
2317 ScriptInterpreterPython::RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton)
2319 ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
2321 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
2324 log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread starting...", baton);
2326 char error_str[1024];
2327 const char *pty_slave_name = script_interpreter->m_embedded_python_pty.GetSlaveName (error_str, sizeof (error_str));
2329 if (pty_slave_name != NULL)
2331 StreamString run_string;
2333 // Ensure we have the GIL before running any Python code.
2334 // Since we're only running a few one-liners and then dropping to the interpreter (which will release the GIL when needed),
2335 // we can just release the GIL after finishing our work.
2336 // If finer-grained locking is desirable, we can lock and unlock the GIL only when calling a python function.
2337 Locker locker(script_interpreter,
2338 ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
2339 ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
2341 run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str());
2342 PyRun_SimpleString (run_string.GetData());
2343 run_string.Clear ();
2345 run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str());
2346 PyRun_SimpleString (run_string.GetData());
2347 run_string.Clear ();
2349 run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str());
2350 PyRun_SimpleString (run_string.GetData());
2351 run_string.Clear ();
2353 run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(),
2355 PyRun_SimpleString (run_string.GetData());
2356 run_string.Clear ();
2358 // The following call drops into the embedded interpreter loop and stays there until the
2359 // user chooses to exit from the Python interpreter.
2360 // This embedded interpreter will, as any Python code that performs I/O, unlock the GIL before
2361 // a system call that can hang, and lock it when the syscall has returned.
2363 // We need to surround the call to the embedded interpreter with calls to PyGILState_Ensure and
2364 // PyGILState_Release (using the Locker above). This is because Python has a global lock which must be held whenever we want
2365 // to touch any Python objects. Otherwise, if the user calls Python code, the interpreter state will be off,
2366 // and things could hang (it's happened before).
2368 run_string.Printf ("run_python_interpreter (%s)", script_interpreter->m_dictionary_name.c_str());
2369 PyRun_SimpleString (run_string.GetData());
2370 run_string.Clear ();
2372 run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str());
2373 PyRun_SimpleString (run_string.GetData());
2376 run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str());
2377 PyRun_SimpleString (run_string.GetData());
2381 if (script_interpreter->m_embedded_python_input_reader_sp)
2382 script_interpreter->m_embedded_python_input_reader_sp->SetIsDone (true);
2384 script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor();
2386 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT);
2388 log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread exiting...", baton);
2391 // Clean up the input reader and make the debugger pop it off the stack.
2392 Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger();
2393 const InputReaderSP reader_sp = script_interpreter->m_embedded_python_input_reader_sp;
2396 debugger.PopInputReader (reader_sp);
2397 script_interpreter->m_embedded_python_input_reader_sp.reset();
2403 lldb::thread_result_t
2404 ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader (lldb::thread_arg_t baton)
2406 ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
2408 const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp;
2411 reader_sp->WaitOnReaderIsDone();
2417 ScriptInterpreterPython::CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2419 if (!implementor_sp)
2422 void* implementor = implementor_sp->GetObject();
2427 if (!g_swig_calc_children)
2430 uint32_t ret_val = 0;
2433 Locker py_lock(this);
2434 ret_val = g_swig_calc_children (implementor);
2441 ScriptInterpreterPython::GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor_sp, uint32_t idx)
2443 if (!implementor_sp)
2444 return lldb::ValueObjectSP();
2446 void* implementor = implementor_sp->GetObject();
2449 return lldb::ValueObjectSP();
2451 if (!g_swig_get_child_index || !g_swig_cast_to_sbvalue)
2452 return lldb::ValueObjectSP();
2454 void* child_ptr = NULL;
2455 lldb::SBValue* value_sb = NULL;
2456 lldb::ValueObjectSP ret_val;
2459 Locker py_lock(this);
2460 child_ptr = g_swig_get_child_index (implementor,idx);
2461 if (child_ptr != NULL && child_ptr != Py_None)
2463 value_sb = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr);
2464 if (value_sb == NULL)
2465 Py_XDECREF(child_ptr);
2467 ret_val = value_sb->GetSP();
2471 Py_XDECREF(child_ptr);
2479 ScriptInterpreterPython::GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor_sp, const char* child_name)
2481 if (!implementor_sp)
2484 void* implementor = implementor_sp->GetObject();
2489 if (!g_swig_get_index_child)
2492 int ret_val = UINT32_MAX;
2495 Locker py_lock(this);
2496 ret_val = g_swig_get_index_child (implementor, child_name);
2503 ScriptInterpreterPython::UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2505 bool ret_val = false;
2507 if (!implementor_sp)
2510 void* implementor = implementor_sp->GetObject();
2515 if (!g_swig_update_provider)
2519 Locker py_lock(this);
2520 ret_val = g_swig_update_provider (implementor);
2527 ScriptInterpreterPython::MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2529 bool ret_val = false;
2531 if (!implementor_sp)
2534 void* implementor = implementor_sp->GetObject();
2539 if (!g_swig_mighthavechildren_provider)
2543 Locker py_lock(this);
2544 ret_val = g_swig_mighthavechildren_provider (implementor);
2551 ReadPythonBacktrace (PyObject* py_backtrace)
2553 PyObject* traceback_module = NULL,
2554 *stringIO_module = NULL,
2555 *stringIO_builder = NULL,
2556 *stringIO_buffer = NULL,
2558 *printTB_args = NULL,
2559 *printTB_result = NULL,
2560 *stringIO_getvalue = NULL,
2561 *printTB_string = NULL;
2563 std::string retval("backtrace unavailable");
2565 if (py_backtrace && py_backtrace != Py_None)
2567 traceback_module = PyImport_ImportModule("traceback");
2568 stringIO_module = PyImport_ImportModule("StringIO");
2570 if (traceback_module && traceback_module != Py_None && stringIO_module && stringIO_module != Py_None)
2572 stringIO_builder = PyObject_GetAttrString(stringIO_module, "StringIO");
2573 if (stringIO_builder && stringIO_builder != Py_None)
2575 stringIO_buffer = PyObject_CallObject(stringIO_builder, NULL);
2576 if (stringIO_buffer && stringIO_buffer != Py_None)
2578 printTB = PyObject_GetAttrString(traceback_module, "print_tb");
2579 if (printTB && printTB != Py_None)
2581 printTB_args = Py_BuildValue("OOO",py_backtrace,Py_None,stringIO_buffer);
2582 printTB_result = PyObject_CallObject(printTB, printTB_args);
2583 stringIO_getvalue = PyObject_GetAttrString(stringIO_buffer, "getvalue");
2584 if (stringIO_getvalue && stringIO_getvalue != Py_None)
2586 printTB_string = PyObject_CallObject (stringIO_getvalue,NULL);
2587 if (printTB_string && printTB_string != Py_None && PyString_Check(printTB_string))
2588 retval.assign(PyString_AsString(printTB_string));
2595 Py_XDECREF(traceback_module);
2596 Py_XDECREF(stringIO_module);
2597 Py_XDECREF(stringIO_builder);
2598 Py_XDECREF(stringIO_buffer);
2599 Py_XDECREF(printTB);
2600 Py_XDECREF(printTB_args);
2601 Py_XDECREF(printTB_result);
2602 Py_XDECREF(stringIO_getvalue);
2603 Py_XDECREF(printTB_string);
2608 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2610 std::string& output,
2616 error.SetErrorString("no process");
2619 if (!impl_function || !impl_function[0])
2621 error.SetErrorString("no function to execute");
2624 if (!g_swig_run_script_keyword_process)
2626 error.SetErrorString("internal helper function missing");
2630 ProcessSP process_sp(process->shared_from_this());
2631 Locker py_lock(this);
2632 ret_val = g_swig_run_script_keyword_process (impl_function, m_dictionary_name.c_str(), process_sp, output);
2634 error.SetErrorString("python script evaluation failed");
2640 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2642 std::string& output,
2648 error.SetErrorString("no thread");
2651 if (!impl_function || !impl_function[0])
2653 error.SetErrorString("no function to execute");
2656 if (!g_swig_run_script_keyword_thread)
2658 error.SetErrorString("internal helper function missing");
2662 ThreadSP thread_sp(thread->shared_from_this());
2663 Locker py_lock(this);
2664 ret_val = g_swig_run_script_keyword_thread (impl_function, m_dictionary_name.c_str(), thread_sp, output);
2666 error.SetErrorString("python script evaluation failed");
2672 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2674 std::string& output,
2680 error.SetErrorString("no thread");
2683 if (!impl_function || !impl_function[0])
2685 error.SetErrorString("no function to execute");
2688 if (!g_swig_run_script_keyword_target)
2690 error.SetErrorString("internal helper function missing");
2694 TargetSP target_sp(target->shared_from_this());
2695 Locker py_lock(this);
2696 ret_val = g_swig_run_script_keyword_target (impl_function, m_dictionary_name.c_str(), target_sp, output);
2698 error.SetErrorString("python script evaluation failed");
2704 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2706 std::string& output,
2712 error.SetErrorString("no frame");
2715 if (!impl_function || !impl_function[0])
2717 error.SetErrorString("no function to execute");
2720 if (!g_swig_run_script_keyword_frame)
2722 error.SetErrorString("internal helper function missing");
2726 StackFrameSP frame_sp(frame->shared_from_this());
2727 Locker py_lock(this);
2728 ret_val = g_swig_run_script_keyword_frame (impl_function, m_dictionary_name.c_str(), frame_sp, output);
2730 error.SetErrorString("python script evaluation failed");
2735 uint64_t replace_all(std::string& str, const std::string& oldStr, const std::string& newStr)
2738 uint64_t matches = 0;
2739 while((pos = str.find(oldStr, pos)) != std::string::npos)
2742 str.replace(pos, oldStr.length(), newStr);
2743 pos += newStr.length();
2749 ScriptInterpreterPython::LoadScriptingModule (const char* pathname,
2752 lldb_private::Error& error)
2754 if (!pathname || !pathname[0])
2756 error.SetErrorString("invalid pathname");
2760 if (!g_swig_call_module_init)
2762 error.SetErrorString("internal helper function missing");
2766 lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2769 FileSpec target_file(pathname, true);
2770 std::string basename(target_file.GetFilename().GetCString());
2772 StreamString command_stream;
2774 // Before executing Pyton code, lock the GIL.
2775 Locker py_lock (this,
2776 Locker::AcquireLock | (init_session ? Locker::InitSession : 0),
2777 Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
2779 if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
2780 target_file.GetFileType() == FileSpec::eFileTypeUnknown)
2782 // if not a valid file of any sort, check if it might be a filename still
2783 // dot can't be used but / and \ can, and if either is found, reject
2784 if (strchr(pathname,'\\') || strchr(pathname,'/'))
2786 error.SetErrorString("invalid pathname");
2789 basename = pathname; // not a filename, probably a package of some sort, let it go through
2791 else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory ||
2792 target_file.GetFileType() == FileSpec::eFileTypeRegular ||
2793 target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink)
2795 std::string directory(target_file.GetDirectory().GetCString());
2796 replace_all(directory,"'","\\'");
2798 // now make sure that Python has "directory" in the search path
2799 StreamString command_stream;
2800 command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n",
2803 bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
2804 if (!syspath_retval)
2806 error.SetErrorString("Python sys.path handling failed");
2810 // strip .py or .pyc extension
2811 ConstString extension = target_file.GetFileNameExtension();
2814 if (::strcmp(extension.GetCString(), "py") == 0)
2815 basename.resize(basename.length()-3);
2816 else if(::strcmp(extension.GetCString(), "pyc") == 0)
2817 basename.resize(basename.length()-4);
2822 error.SetErrorString("no known way to import this module specification");
2826 // check if the module is already import-ed
2827 command_stream.Clear();
2828 command_stream.Printf("sys.modules.__contains__('%s')",basename.c_str());
2829 bool does_contain = false;
2831 // this call will succeed if the module was ever imported in any Debugger in the lifetime of the process
2832 // in which this LLDB framework is living
2833 bool was_imported_globally = (ExecuteOneLineWithReturn(command_stream.GetData(),
2834 ScriptInterpreterPython::eScriptReturnTypeBool,
2836 ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && does_contain);
2837 // this call will fail if the module was not imported in this Debugger before
2838 command_stream.Clear();
2839 command_stream.Printf("sys.getrefcount(%s)",basename.c_str());
2840 bool was_imported_locally = (ExecuteOneLineWithReturn(command_stream.GetData(),
2841 ScriptInterpreterPython::eScriptReturnTypeInt,
2843 ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && refcount > 0);
2845 bool was_imported = (was_imported_globally || was_imported_locally);
2847 if (was_imported == true && can_reload == false)
2849 error.SetErrorString("module already imported");
2853 // now actually do the import
2854 command_stream.Clear();
2858 if (!was_imported_locally)
2859 command_stream.Printf("import %s ; reload(%s)",basename.c_str(),basename.c_str());
2861 command_stream.Printf("reload(%s)",basename.c_str());
2864 command_stream.Printf("import %s",basename.c_str());
2866 bool import_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false).SetMaskoutErrors(false));
2867 PyObject* py_error = PyErr_Occurred(); // per Python docs: "you do not need to Py_DECREF()" the return of this function
2869 if (py_error || !import_retval) // check for failure of the import
2871 if (py_error) // if we have a Python error..
2873 PyObject *type = NULL,*value = NULL,*traceback = NULL;
2874 PyErr_Fetch (&type,&value,&traceback);
2876 if (PyErr_GivenExceptionMatches (py_error, PyExc_ImportError)) // and it is an ImportError
2878 if (value && value != Py_None)
2879 error.SetErrorString(PyString_AsString(PyObject_Str(value)));
2881 error.SetErrorString("ImportError raised by imported module");
2883 else // any other error
2885 // get the backtrace
2886 std::string bt = ReadPythonBacktrace(traceback);
2888 if (value && value != Py_None)
2889 error.SetErrorStringWithFormat("Python error raised while importing module: %s - traceback: %s", PyString_AsString(PyObject_Str(value)),bt.c_str());
2891 error.SetErrorStringWithFormat("Python raised an error while importing module - traceback: %s",bt.c_str());
2896 Py_XDECREF(traceback);
2898 else // we failed but have no error to explain why
2900 error.SetErrorString("unknown error while importing module");
2903 // anyway, clear the error indicator and return false
2908 // if we are here, everything worked
2909 // call __lldb_init_module(debugger,dict)
2910 if (!g_swig_call_module_init (basename.c_str(),
2911 m_dictionary_name.c_str(),
2914 error.SetErrorString("calling __lldb_init_module failed");
2921 lldb::ScriptInterpreterObjectSP
2922 ScriptInterpreterPython::MakeScriptObject (void* object)
2924 return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterPythonObject(object));
2927 ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp,
2928 ScriptedCommandSynchronicity synchro) :
2929 m_debugger_sp(debugger_sp),
2930 m_synch_wanted(synchro),
2931 m_old_asynch(debugger_sp->GetAsyncExecution())
2933 if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2934 m_debugger_sp->SetAsyncExecution(false);
2935 else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2936 m_debugger_sp->SetAsyncExecution(true);
2939 ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler()
2941 if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2942 m_debugger_sp->SetAsyncExecution(m_old_asynch);
2946 ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
2948 ScriptedCommandSynchronicity synchronicity,
2949 lldb_private::CommandReturnObject& cmd_retobj,
2954 error.SetErrorString("no function to execute");
2958 if (!g_swig_call_command)
2960 error.SetErrorString("no helper function to run scripted commands");
2964 lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2966 if (!debugger_sp.get())
2968 error.SetErrorString("invalid Debugger pointer");
2972 bool ret_val = false;
2974 std::string err_msg;
2977 Locker py_lock(this,
2978 Locker::AcquireLock | Locker::InitSession,
2979 Locker::FreeLock | Locker::TearDownSession);
2981 SynchronicityHandler synch_handler(debugger_sp,
2984 // we need to save the thread state when we first start the command
2985 // because we might decide to interrupt it while some action is taking
2986 // place outside of Python (e.g. printing to screen, waiting for the network, ...)
2987 // in that case, _PyThreadState_Current will be NULL - and we would be unable
2988 // to set the asynchronous exception - not a desirable situation
2989 m_command_thread_state = _PyThreadState_Current;
2991 PythonInputReaderManager py_input(this);
2993 ret_val = g_swig_call_command (impl_function,
2994 m_dictionary_name.c_str(),
3001 error.SetErrorString("unable to execute script function");
3008 // in Python, a special attribute __doc__ contains the docstring
3009 // for an object (function, method, class, ...) if any is defined
3010 // Otherwise, the attribute's value is None
3012 ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& dest)
3015 if (!item || !*item)
3017 std::string command(item);
3018 command += ".__doc__";
3020 char* result_ptr = NULL; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully
3022 if (ExecuteOneLineWithReturn (command.c_str(),
3023 ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
3025 ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false)))
3028 dest.assign(result_ptr);
3033 StreamString str_stream;
3034 str_stream.Printf("Function %s was not found. Containing module might be missing.",item);
3035 dest.assign(str_stream.GetData());
3040 std::unique_ptr<ScriptInterpreterLocker>
3041 ScriptInterpreterPython::AcquireInterpreterLock ()
3043 std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(this,
3044 Locker::AcquireLock | Locker::InitSession,
3045 Locker::FreeLock | Locker::TearDownSession));
3050 ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_init_callback)
3052 g_swig_init_callback = python_swig_init_callback;
3053 g_swig_breakpoint_callback = LLDBSwigPythonBreakpointCallbackFunction;
3054 g_swig_watchpoint_callback = LLDBSwigPythonWatchpointCallbackFunction;
3055 g_swig_typescript_callback = LLDBSwigPythonCallTypeScript;
3056 g_swig_synthetic_script = LLDBSwigPythonCreateSyntheticProvider;
3057 g_swig_calc_children = LLDBSwigPython_CalculateNumChildren;
3058 g_swig_get_child_index = LLDBSwigPython_GetChildAtIndex;
3059 g_swig_get_index_child = LLDBSwigPython_GetIndexOfChildWithName;
3060 g_swig_cast_to_sbvalue = LLDBSWIGPython_CastPyObjectToSBValue;
3061 g_swig_update_provider = LLDBSwigPython_UpdateSynthProviderInstance;
3062 g_swig_mighthavechildren_provider = LLDBSwigPython_MightHaveChildrenSynthProviderInstance;
3063 g_swig_call_command = LLDBSwigPythonCallCommand;
3064 g_swig_call_module_init = LLDBSwigPythonCallModuleInit;
3065 g_swig_create_os_plugin = LLDBSWIGPythonCreateOSPlugin;
3066 g_swig_run_script_keyword_process = LLDBSWIGPythonRunScriptKeywordProcess;
3067 g_swig_run_script_keyword_thread = LLDBSWIGPythonRunScriptKeywordThread;
3068 g_swig_run_script_keyword_target = LLDBSWIGPythonRunScriptKeywordTarget;
3069 g_swig_run_script_keyword_frame = LLDBSWIGPythonRunScriptKeywordFrame;
3073 ScriptInterpreterPython::InitializePrivate ()
3075 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
3077 // Python will muck with STDIN terminal state, so save off any current TTY
3078 // settings so we can restore them.
3079 TerminalState stdin_tty_state;
3080 stdin_tty_state.Save(STDIN_FILENO, false);
3082 PyGILState_STATE gstate;
3083 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
3084 bool threads_already_initialized = false;
3085 if (PyEval_ThreadsInitialized ()) {
3086 gstate = PyGILState_Ensure ();
3088 log->Printf("Ensured PyGILState. Previous state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : "");
3089 threads_already_initialized = true;
3091 // InitThreads acquires the GIL if it hasn't been called before.
3092 PyEval_InitThreads ();
3094 Py_InitializeEx (0);
3096 // Initialize SWIG after setting up python
3097 assert (g_swig_init_callback != NULL);
3098 g_swig_init_callback ();
3100 // Update the path python uses to search for modules to include the current directory.
3102 PyRun_SimpleString ("import sys");
3103 PyRun_SimpleString ("sys.path.append ('.')");
3105 // Find the module that owns this code and use that path we get to
3106 // set the sys.path appropriately.
3109 char python_dir_path[PATH_MAX];
3110 if (Host::GetLLDBPath (ePathTypePythonDir, file_spec))
3112 std::string python_path("sys.path.insert(0,\"");
3113 size_t orig_len = python_path.length();
3114 if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
3116 python_path.append (python_dir_path);
3117 python_path.append ("\")");
3118 PyRun_SimpleString (python_path.c_str());
3119 python_path.resize (orig_len);
3122 if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, file_spec))
3124 if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
3126 python_path.append (python_dir_path);
3127 python_path.append ("\")");
3128 PyRun_SimpleString (python_path.c_str());
3129 python_path.resize (orig_len);
3134 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; from termios import *");
3136 if (threads_already_initialized) {
3138 log->Printf("Releasing PyGILState. Returning to state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : "");
3139 PyGILState_Release (gstate);
3141 // We initialized the threads in this function, just unlock the GIL.
3142 PyEval_SaveThread();
3145 stdin_tty_state.Restore();
3149 //ScriptInterpreterPython::Terminate ()
3151 // // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it). Calling
3152 // // Py_Finalize here causes test suite runs to seg fault: The test suite runs in Python. It registers
3153 // // SBDebugger::Terminate to be called 'at_exit'. When the test suite Python harness finishes up, it calls
3154 // // Py_Finalize, which calls all the 'at_exit' registered functions. SBDebugger::Terminate calls Debugger::Terminate,
3155 // // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls
3156 // // ScriptInterpreterPython::Terminate. So if we call Py_Finalize here, we end up with Py_Finalize being called from
3157 // // within Py_Finalize, which results in a seg fault.
3159 // // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't
3160 // // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the
3161 // // process exits).
3163 //// Py_Finalize ();
3166 #endif // #ifdef LLDB_DISABLE_PYTHON