Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ScriptInterpreterPython.cpp -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // In order to guarantee correct working with Python, Python.h *MUST* be |
Benjamin Kramer | c28bbdb | 2011-10-23 16:49:03 +0000 | [diff] [blame] | 11 | // the *FIRST* header file included here. |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 12 | #ifdef LLDB_DISABLE_PYTHON |
| 13 | |
| 14 | // Python is disabled in this build |
| 15 | |
| 16 | #else |
Benjamin Kramer | c28bbdb | 2011-10-23 16:49:03 +0000 | [diff] [blame] | 17 | |
| 18 | #if defined (__APPLE__) |
| 19 | #include <Python/Python.h> |
| 20 | #else |
| 21 | #include <Python.h> |
| 22 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | #include "lldb/Interpreter/ScriptInterpreterPython.h" |
| 25 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #include <string> |
| 30 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 31 | #include "lldb/API/SBValue.h" |
Greg Clayton | 987c7eb | 2011-09-17 08:33:22 +0000 | [diff] [blame] | 32 | #include "lldb/Breakpoint/BreakpointLocation.h" |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 33 | #include "lldb/Breakpoint/StoppointCallbackContext.h" |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 34 | #include "lldb/Breakpoint/WatchpointOptions.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | #include "lldb/Core/Debugger.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "lldb/Core/Timer.h" |
| 37 | #include "lldb/Host/Host.h" |
| 38 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 39 | #include "lldb/Interpreter/CommandReturnObject.h" |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 40 | #include "lldb/Target/Thread.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | using namespace lldb; |
| 43 | using namespace lldb_private; |
| 44 | |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 45 | |
| 46 | static ScriptInterpreter::SWIGInitCallback g_swig_init_callback = NULL; |
| 47 | static ScriptInterpreter::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = NULL; |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 48 | static ScriptInterpreter::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = NULL; |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 49 | static ScriptInterpreter::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = NULL; |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 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; |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 55 | static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = NULL; |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 56 | static ScriptInterpreter::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = NULL; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 57 | static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = NULL; |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 58 | static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = NULL; |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 59 | static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 61 | // these are the Pythonic implementations of the required callbacks |
| 62 | // these are scripting-language specific, which is why they belong here |
| 63 | // we still need to use function pointers to them instead of relying |
| 64 | // on linkage-time resolution because the SWIG stuff and this file |
| 65 | // get built at different times |
| 66 | extern "C" bool |
| 67 | LLDBSwigPythonBreakpointCallbackFunction |
| 68 | ( |
| 69 | const char *python_function_name, |
| 70 | const char *session_dictionary_name, |
| 71 | const lldb::StackFrameSP& sb_frame, |
| 72 | const lldb::BreakpointLocationSP& sb_bp_loc |
| 73 | ); |
| 74 | |
| 75 | extern "C" bool |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 76 | LLDBSwigPythonWatchpointCallbackFunction |
| 77 | ( |
| 78 | const char *python_function_name, |
| 79 | const char *session_dictionary_name, |
| 80 | const lldb::StackFrameSP& sb_frame, |
| 81 | const lldb::WatchpointSP& sb_wp |
| 82 | ); |
| 83 | |
| 84 | extern "C" bool |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 85 | LLDBSwigPythonCallTypeScript |
| 86 | ( |
| 87 | const char *python_function_name, |
| 88 | void *session_dictionary, |
| 89 | const lldb::ValueObjectSP& valobj_sp, |
| 90 | void** pyfunct_wrapper, |
| 91 | std::string& retval |
| 92 | ); |
| 93 | |
| 94 | extern "C" void* |
| 95 | LLDBSwigPythonCreateSyntheticProvider |
| 96 | ( |
| 97 | const std::string python_class_name, |
| 98 | const char *session_dictionary_name, |
| 99 | const lldb::ValueObjectSP& valobj_sp |
| 100 | ); |
| 101 | |
| 102 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 103 | extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor); |
| 104 | extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx); |
| 105 | extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name); |
| 106 | extern "C" void* LLDBSWIGPython_CastPyObjectToSBValue (void* data); |
| 107 | extern "C" bool LLDBSwigPython_UpdateSynthProviderInstance (void* implementor); |
| 108 | extern "C" bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor); |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 109 | |
| 110 | extern "C" bool LLDBSwigPythonCallCommand |
| 111 | ( |
| 112 | const char *python_function_name, |
| 113 | const char *session_dictionary_name, |
| 114 | lldb::DebuggerSP& debugger, |
| 115 | const char* args, |
| 116 | std::string& err_msg, |
| 117 | lldb_private::CommandReturnObject& cmd_retobj |
| 118 | ); |
| 119 | |
| 120 | extern "C" bool LLDBSwigPythonCallModuleInit |
| 121 | ( |
| 122 | const std::string python_module_name, |
| 123 | const char *session_dictionary_name, |
| 124 | lldb::DebuggerSP& debugger |
| 125 | ); |
| 126 | |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 127 | extern "C" void* LLDBSWIGPythonCreateOSPlugin |
| 128 | ( |
| 129 | const std::string python_class_name, |
| 130 | const char *session_dictionary_name, |
| 131 | const lldb::ProcessSP& process_sp |
| 132 | ); |
| 133 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | static int |
| 135 | _check_and_flush (FILE *stream) |
| 136 | { |
| 137 | int prev_fail = ferror (stream); |
| 138 | return fflush (stream) || prev_fail ? EOF : 0; |
| 139 | } |
| 140 | |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 141 | ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter, |
| 142 | uint16_t on_entry, |
| 143 | uint16_t on_leave, |
| 144 | FILE* wait_msg_handle) : |
| 145 | m_need_session( (on_leave & TearDownSession) == TearDownSession ), |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 146 | m_python_interpreter(py_interpreter), |
| 147 | m_tmp_fh(wait_msg_handle) |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 148 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 149 | if (m_python_interpreter && !m_tmp_fh) |
| 150 | m_tmp_fh = (m_python_interpreter->m_dbg_stdout ? m_python_interpreter->m_dbg_stdout : stdout); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 151 | |
| 152 | DoAcquireLock(); |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 153 | if ((on_entry & InitSession) == InitSession) |
| 154 | DoInitSession((on_entry & InitGlobals) == InitGlobals); |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | bool |
| 158 | ScriptInterpreterPython::Locker::DoAcquireLock() |
| 159 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 160 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE)); |
| 161 | m_GILState = PyGILState_Ensure(); |
| 162 | if (log) |
| 163 | log->Printf("Ensured PyGILState. Previous state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : ""); |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | bool |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 168 | ScriptInterpreterPython::Locker::DoInitSession(bool init_lldb_globals) |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 169 | { |
| 170 | if (!m_python_interpreter) |
| 171 | return false; |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 172 | m_python_interpreter->EnterSession (init_lldb_globals); |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool |
| 177 | ScriptInterpreterPython::Locker::DoFreeLock() |
| 178 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 179 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE)); |
| 180 | if (log) |
| 181 | log->Printf("Releasing PyGILState. Returning to state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : ""); |
| 182 | PyGILState_Release(m_GILState); |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool |
| 187 | ScriptInterpreterPython::Locker::DoTearDownSession() |
| 188 | { |
| 189 | if (!m_python_interpreter) |
| 190 | return false; |
| 191 | m_python_interpreter->LeaveSession (); |
| 192 | return true; |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | ScriptInterpreterPython::Locker::~Locker() |
| 196 | { |
| 197 | if (m_need_session) |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 198 | DoTearDownSession(); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 199 | DoFreeLock(); |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 202 | ScriptInterpreterPython::PythonInputReaderManager::PythonInputReaderManager (ScriptInterpreterPython *interpreter) : |
| 203 | m_interpreter(interpreter), |
| 204 | m_debugger_sp(), |
| 205 | m_reader_sp(), |
| 206 | m_error(false) |
| 207 | { |
| 208 | if (m_interpreter == NULL) |
| 209 | { |
| 210 | m_error = true; |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | m_debugger_sp = m_interpreter->GetCommandInterpreter().GetDebugger().shared_from_this(); |
| 215 | |
| 216 | if (!m_debugger_sp) |
| 217 | { |
| 218 | m_error = true; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | m_reader_sp = InputReaderSP(new InputReader(*m_debugger_sp.get())); |
| 223 | |
| 224 | if (!m_reader_sp) |
| 225 | { |
| 226 | m_error = true; |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | Error error (m_reader_sp->Initialize (ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback, |
| 231 | m_interpreter, // baton |
| 232 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 233 | NULL, // end token |
| 234 | NULL, // prompt |
| 235 | true)); // echo input |
| 236 | if (error.Fail()) |
| 237 | m_error = true; |
| 238 | else |
| 239 | { |
| 240 | m_debugger_sp->PushInputReader (m_reader_sp); |
| 241 | m_interpreter->m_embedded_thread_input_reader_sp = m_reader_sp; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | ScriptInterpreterPython::PythonInputReaderManager::~PythonInputReaderManager() |
| 246 | { |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 247 | // Nothing to do if either m_interpreter or m_reader_sp is invalid. |
| 248 | if (!m_interpreter || !m_reader_sp) |
| 249 | return; |
| 250 | |
| 251 | m_reader_sp->SetIsDone (true); |
| 252 | if (m_debugger_sp) |
| 253 | m_debugger_sp->PopInputReader(m_reader_sp); |
| 254 | |
| 255 | // Only mess with m_interpreter's counterpart if, indeed, they are the same object. |
| 256 | if (m_reader_sp.get() == m_interpreter->m_embedded_thread_input_reader_sp.get()) |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 257 | { |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 258 | m_interpreter->m_embedded_thread_pty.CloseSlaveFileDescriptor(); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 259 | m_interpreter->m_embedded_thread_input_reader_sp.reset(); |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 260 | } |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | size_t |
| 264 | ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback |
| 265 | ( |
| 266 | void *baton, |
| 267 | InputReader &reader, |
| 268 | InputReaderAction notification, |
| 269 | const char *bytes, |
| 270 | size_t bytes_len |
| 271 | ) |
| 272 | { |
| 273 | lldb::thread_t embedded_interpreter_thread; |
| 274 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT)); |
| 275 | |
| 276 | if (baton == NULL) |
| 277 | return 0; |
| 278 | |
| 279 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
| 280 | |
| 281 | if (script_interpreter->m_script_lang != eScriptLanguagePython) |
| 282 | return 0; |
| 283 | |
| 284 | StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream(); |
| 285 | |
| 286 | switch (notification) |
| 287 | { |
| 288 | case eInputReaderActivate: |
| 289 | { |
| 290 | // Save terminal settings if we can |
| 291 | int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor(); |
| 292 | if (input_fd == File::kInvalidDescriptor) |
| 293 | input_fd = STDIN_FILENO; |
| 294 | |
| 295 | script_interpreter->SaveTerminalState(input_fd); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 296 | |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 297 | char error_str[1024]; |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 298 | if (script_interpreter->m_embedded_thread_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 299 | sizeof(error_str))) |
| 300 | { |
| 301 | if (log) |
| 302 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in opening master pty (fd = %d).", |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 303 | script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor()); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 304 | { |
| 305 | StreamString run_string; |
| 306 | char error_str[1024]; |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 307 | const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str)); |
Enrico Granata | ca2c707 | 2012-07-31 16:58:12 +0000 | [diff] [blame] | 308 | if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL) |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 309 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 310 | ScriptInterpreterPython::Locker locker(script_interpreter, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 311 | ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals, |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 312 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 313 | run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 314 | PyRun_SimpleString (run_string.GetData()); |
| 315 | run_string.Clear (); |
| 316 | |
| 317 | run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str()); |
| 318 | PyRun_SimpleString (run_string.GetData()); |
| 319 | run_string.Clear (); |
| 320 | |
| 321 | run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 322 | PyRun_SimpleString (run_string.GetData()); |
| 323 | run_string.Clear (); |
| 324 | |
| 325 | run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(), |
| 326 | pty_slave_name); |
| 327 | PyRun_SimpleString (run_string.GetData()); |
| 328 | run_string.Clear (); |
| 329 | } |
| 330 | } |
| 331 | embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.noninteractive-python>", |
| 332 | ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader, |
| 333 | script_interpreter, NULL); |
| 334 | if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread)) |
| 335 | { |
| 336 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 337 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 338 | Error detach_error; |
| 339 | Host::ThreadDetach (embedded_interpreter_thread, &detach_error); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | if (log) |
| 344 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed in creating thread"); |
| 345 | reader.SetIsDone (true); |
| 346 | } |
| 347 | } |
| 348 | else |
| 349 | { |
| 350 | if (log) |
| 351 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed to open master pty "); |
| 352 | reader.SetIsDone (true); |
| 353 | } |
| 354 | } |
| 355 | break; |
| 356 | |
| 357 | case eInputReaderDeactivate: |
| 358 | // When another input reader is pushed, don't leave the session... |
| 359 | //script_interpreter->LeaveSession (); |
| 360 | break; |
| 361 | |
| 362 | case eInputReaderReactivate: |
Greg Clayton | 3b1afc6 | 2012-09-01 00:38:36 +0000 | [diff] [blame] | 363 | // { |
| 364 | // ScriptInterpreterPython::Locker locker(script_interpreter, |
| 365 | // ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession, |
| 366 | // ScriptInterpreterPython::Locker::FreeAcquiredLock); |
| 367 | // } |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | case eInputReaderAsynchronousOutputWritten: |
| 371 | break; |
| 372 | |
| 373 | case eInputReaderInterrupt: |
| 374 | reader.SetIsDone(true); |
| 375 | break; |
| 376 | |
| 377 | case eInputReaderEndOfFile: |
| 378 | reader.SetIsDone(true); |
| 379 | break; |
| 380 | |
| 381 | case eInputReaderGotToken: |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 382 | if (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor() != -1) |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 383 | { |
| 384 | if (log) |
| 385 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes, |
| 386 | bytes_len); |
| 387 | if (bytes && bytes_len) |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 388 | ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), bytes, bytes_len); |
| 389 | ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), "\n", 1); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 390 | } |
| 391 | else |
| 392 | { |
| 393 | if (log) |
| 394 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.", |
| 395 | bytes, |
| 396 | bytes_len); |
| 397 | reader.SetIsDone (true); |
| 398 | } |
| 399 | |
| 400 | break; |
| 401 | |
| 402 | case eInputReaderDone: |
| 403 | { |
| 404 | StreamString run_string; |
| 405 | char error_str[1024]; |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 406 | const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str)); |
Enrico Granata | ca2c707 | 2012-07-31 16:58:12 +0000 | [diff] [blame] | 407 | if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL) |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 408 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 409 | ScriptInterpreterPython::Locker locker(script_interpreter, |
| 410 | ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession, |
| 411 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 412 | run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 413 | PyRun_SimpleString (run_string.GetData()); |
| 414 | run_string.Clear(); |
| 415 | |
| 416 | run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 417 | PyRun_SimpleString (run_string.GetData()); |
| 418 | run_string.Clear(); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // Restore terminal settings if they were validly saved |
| 423 | if (log) |
| 424 | log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Done, closing down input reader."); |
| 425 | |
| 426 | script_interpreter->RestoreTerminalState (); |
| 427 | |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 428 | script_interpreter->m_embedded_thread_pty.CloseMasterFileDescriptor(); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | |
| 432 | return bytes_len; |
| 433 | } |
| 434 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 435 | ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 436 | ScriptInterpreter (interpreter, eScriptLanguagePython), |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 437 | m_embedded_thread_pty (), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 438 | m_embedded_python_pty (), |
| 439 | m_embedded_thread_input_reader_sp (), |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 440 | m_embedded_python_input_reader_sp (), |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 441 | m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 442 | m_new_sysout (NULL), |
Johnny Chen | c65046d | 2012-03-08 20:53:04 +0000 | [diff] [blame] | 443 | m_old_sysout (NULL), |
| 444 | m_old_syserr (NULL), |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 445 | m_run_one_line (NULL), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 446 | m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()), |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 447 | m_terminal_state (), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 448 | m_session_is_active (false), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 449 | m_valid_session (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 450 | { |
| 451 | |
Greg Clayton | 7c330d6 | 2011-01-27 01:01:10 +0000 | [diff] [blame] | 452 | static int g_initialized = false; |
| 453 | |
| 454 | if (!g_initialized) |
| 455 | { |
| 456 | g_initialized = true; |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 457 | ScriptInterpreterPython::InitializePrivate (); |
Greg Clayton | 7c330d6 | 2011-01-27 01:01:10 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 460 | m_dictionary_name.append("_dict"); |
| 461 | StreamString run_string; |
| 462 | run_string.Printf ("%s = dict()", m_dictionary_name.c_str()); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 463 | |
| 464 | Locker locker(this, |
| 465 | ScriptInterpreterPython::Locker::AcquireLock, |
| 466 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 467 | PyRun_SimpleString (run_string.GetData()); |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 468 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 469 | run_string.Clear(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 470 | |
| 471 | // Importing 'lldb' module calls SBDebugger::Initialize, which calls Debugger::Initialize, which increments a |
| 472 | // global debugger ref-count; therefore we need to check the ref-count before and after importing lldb, and if the |
| 473 | // ref-count increased we need to call Debugger::Terminate here to decrement the ref-count so that when the final |
| 474 | // call to Debugger::Terminate is made, the ref-count has the correct value. |
| 475 | // |
| 476 | // Bonus question: Why doesn't the ref-count always increase? Because sometimes lldb has already been imported, in |
| 477 | // which case the code inside it, including the call to SBDebugger::Initialize(), does not get executed. |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 478 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 479 | int old_count = Debugger::TestDebuggerRefCount(); |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 480 | |
Greg Clayton | 4e651b1 | 2012-04-25 00:58:03 +0000 | [diff] [blame] | 481 | run_string.Printf ("run_one_line (%s, 'import copy, os, re, sys, uuid, lldb')", m_dictionary_name.c_str()); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 482 | PyRun_SimpleString (run_string.GetData()); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 483 | |
Enrico Granata | 8f84cfb | 2012-02-23 23:10:03 +0000 | [diff] [blame] | 484 | // WARNING: temporary code that loads Cocoa formatters - this should be done on a per-platform basis rather than loading the whole set |
| 485 | // and letting the individual formatter classes exploit APIs to check whether they can/cannot do their task |
| 486 | run_string.Clear(); |
Enrico Granata | 14cf90e | 2012-10-22 19:09:28 +0000 | [diff] [blame] | 487 | run_string.Printf ("run_one_line (%s, 'import lldb.runtime.objc, lldb.formatters, lldb.formatters.objc, lldb.formatters.cpp, pydoc')", m_dictionary_name.c_str()); |
Enrico Granata | 8f84cfb | 2012-02-23 23:10:03 +0000 | [diff] [blame] | 488 | PyRun_SimpleString (run_string.GetData()); |
Greg Clayton | b302dff | 2012-02-01 08:09:32 +0000 | [diff] [blame] | 489 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 490 | int new_count = Debugger::TestDebuggerRefCount(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 491 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 492 | if (new_count > old_count) |
| 493 | Debugger::Terminate(); |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 494 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 495 | run_string.Clear(); |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 496 | run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", m_dictionary_name.c_str(), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 497 | interpreter.GetDebugger().GetID()); |
| 498 | PyRun_SimpleString (run_string.GetData()); |
| 499 | |
| 500 | if (m_dbg_stdout != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 501 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 502 | m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush); |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 503 | } |
Enrico Granata | db05491 | 2012-10-29 21:18:03 +0000 | [diff] [blame] | 504 | |
| 505 | // get the output file handle from the debugger (if any) |
| 506 | File& out_file = interpreter.GetDebugger().GetOutputFile(); |
| 507 | if (out_file.IsValid()) |
| 508 | ResetOutputFileHandle(out_file.GetStream()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | ScriptInterpreterPython::~ScriptInterpreterPython () |
| 512 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 513 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 514 | |
| 515 | if (m_embedded_thread_input_reader_sp.get() != NULL) |
| 516 | { |
| 517 | m_embedded_thread_input_reader_sp->SetIsDone (true); |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 518 | m_embedded_thread_pty.CloseSlaveFileDescriptor(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 519 | const InputReaderSP reader_sp = m_embedded_thread_input_reader_sp; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 520 | debugger.PopInputReader (reader_sp); |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 521 | m_embedded_thread_input_reader_sp.reset(); |
| 522 | } |
| 523 | |
| 524 | if (m_embedded_python_input_reader_sp.get() != NULL) |
| 525 | { |
| 526 | m_embedded_python_input_reader_sp->SetIsDone (true); |
| 527 | m_embedded_python_pty.CloseSlaveFileDescriptor(); |
| 528 | const InputReaderSP reader_sp = m_embedded_python_input_reader_sp; |
| 529 | debugger.PopInputReader (reader_sp); |
| 530 | m_embedded_python_input_reader_sp.reset(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | if (m_new_sysout) |
| 534 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 535 | Locker locker(this, |
| 536 | ScriptInterpreterPython::Locker::AcquireLock, |
| 537 | ScriptInterpreterPython::Locker::FreeLock); |
| 538 | Py_DECREF ((PyObject*)m_new_sysout); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 539 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 542 | void |
| 543 | ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh) |
| 544 | { |
| 545 | if (fh == NULL) |
| 546 | return; |
| 547 | |
| 548 | m_dbg_stdout = fh; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 549 | |
Johnny Chen | c65046d | 2012-03-08 20:53:04 +0000 | [diff] [blame] | 550 | Locker locker(this, |
| 551 | ScriptInterpreterPython::Locker::AcquireLock, |
| 552 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
| 553 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 554 | m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | void |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 558 | ScriptInterpreterPython::SaveTerminalState (int fd) |
| 559 | { |
| 560 | // Python mucks with the terminal state of STDIN. If we can possibly avoid |
| 561 | // this by setting the file handles up correctly prior to entering the |
| 562 | // interpreter we should. For now we save and restore the terminal state |
| 563 | // on the input file handle. |
| 564 | m_terminal_state.Save (fd, false); |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | ScriptInterpreterPython::RestoreTerminalState () |
| 569 | { |
| 570 | // Python mucks with the terminal state of STDIN. If we can possibly avoid |
| 571 | // this by setting the file handles up correctly prior to entering the |
| 572 | // interpreter we should. For now we save and restore the terminal state |
| 573 | // on the input file handle. |
| 574 | m_terminal_state.Restore(); |
| 575 | } |
| 576 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 577 | void |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 578 | ScriptInterpreterPython::LeaveSession () |
| 579 | { |
Enrico Granata | 7aa754c | 2012-04-04 17:31:29 +0000 | [diff] [blame] | 580 | // checking that we have a valid thread state - since we use our own threading and locking |
| 581 | // in some (rare) cases during cleanup Python may end up believing we have no thread state |
| 582 | // and PyImport_AddModule will crash if that is the case - since that seems to only happen |
| 583 | // when destroying the SBDebugger, we can make do without clearing up stdout and stderr |
Johnny Chen | 2b53695 | 2012-05-04 20:37:11 +0000 | [diff] [blame] | 584 | |
| 585 | // rdar://problem/11292882 |
| 586 | // When the current thread state is NULL, PyThreadState_Get() issues a fatal error. |
| 587 | if (PyThreadState_GetDict()) |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 588 | { |
Enrico Granata | 7aa754c | 2012-04-04 17:31:29 +0000 | [diff] [blame] | 589 | PyObject *sysmod = PyImport_AddModule ("sys"); |
| 590 | PyObject *sysdict = PyModule_GetDict (sysmod); |
| 591 | |
| 592 | if (m_new_sysout && sysmod && sysdict) |
| 593 | { |
| 594 | if (m_old_sysout) |
| 595 | PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout); |
| 596 | if (m_old_syserr) |
| 597 | PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_syserr); |
| 598 | } |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 601 | m_session_is_active = false; |
| 602 | } |
| 603 | |
| 604 | void |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 605 | ScriptInterpreterPython::EnterSession (bool init_lldb_globals) |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 606 | { |
| 607 | // If we have already entered the session, without having officially 'left' it, then there is no need to |
| 608 | // 'enter' it again. |
| 609 | |
| 610 | if (m_session_is_active) |
| 611 | return; |
| 612 | |
| 613 | m_session_is_active = true; |
| 614 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 615 | StreamString run_string; |
| 616 | |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 617 | if (init_lldb_globals) |
| 618 | { |
| 619 | run_string.Printf ( "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID()); |
| 620 | run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID()); |
| 621 | run_string.PutCString ("; lldb.target = lldb.debugger.GetSelectedTarget()"); |
| 622 | run_string.PutCString ("; lldb.process = lldb.target.GetProcess()"); |
| 623 | run_string.PutCString ("; lldb.thread = lldb.process.GetSelectedThread ()"); |
| 624 | run_string.PutCString ("; lldb.frame = lldb.thread.GetSelectedFrame ()"); |
| 625 | run_string.PutCString ("')"); |
| 626 | } |
Jim Ingham | 1dba077 | 2012-12-08 02:02:04 +0000 | [diff] [blame] | 627 | else |
| 628 | { |
| 629 | // If we aren't initing the globals, we should still always set the debugger (since that is always unique.) |
| 630 | run_string.Printf ( "run_one_line (%s, \"lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID()); |
| 631 | run_string.Printf ( "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID()); |
| 632 | run_string.PutCString ("\")"); |
| 633 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 634 | |
Caroline Tice | 6af65cb | 2011-05-03 21:21:50 +0000 | [diff] [blame] | 635 | PyRun_SimpleString (run_string.GetData()); |
| 636 | run_string.Clear(); |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 637 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 638 | PyObject *sysmod = PyImport_AddModule ("sys"); |
| 639 | PyObject *sysdict = PyModule_GetDict (sysmod); |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 640 | |
Greg Clayton | 2fecc45 | 2012-01-28 02:11:02 +0000 | [diff] [blame] | 641 | if (m_new_sysout && sysmod && sysdict) |
| 642 | { |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 643 | m_old_sysout = PyDict_GetItemString(sysdict, "stdout"); |
| 644 | m_old_syserr = PyDict_GetItemString(sysdict, "stderr"); |
Johnny Chen | c65046d | 2012-03-08 20:53:04 +0000 | [diff] [blame] | 645 | if (m_new_sysout) |
| 646 | { |
| 647 | PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_new_sysout); |
| 648 | PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_new_sysout); |
| 649 | } |
Greg Clayton | 2fecc45 | 2012-01-28 02:11:02 +0000 | [diff] [blame] | 650 | } |
Johnny Chen | 41641f9 | 2012-02-29 01:52:13 +0000 | [diff] [blame] | 651 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 652 | if (PyErr_Occurred()) |
| 653 | PyErr_Clear (); |
Greg Clayton | 2fecc45 | 2012-01-28 02:11:02 +0000 | [diff] [blame] | 654 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 655 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 656 | static PyObject* |
| 657 | FindSessionDictionary (const char* dict_name) |
| 658 | { |
| 659 | static std::map<ConstString,PyObject*> g_dict_map; |
| 660 | |
| 661 | ConstString dict(dict_name); |
| 662 | |
| 663 | std::map<ConstString,PyObject*>::iterator iter = g_dict_map.find(dict); |
| 664 | |
| 665 | if (iter != g_dict_map.end()) |
| 666 | return iter->second; |
| 667 | |
| 668 | PyObject *main_mod = PyImport_AddModule ("__main__"); |
| 669 | if (main_mod != NULL) |
| 670 | { |
| 671 | PyObject *main_dict = PyModule_GetDict (main_mod); |
| 672 | if ((main_dict != NULL) |
| 673 | && PyDict_Check (main_dict)) |
| 674 | { |
| 675 | // Go through the main dictionary looking for the correct python script interpreter dictionary |
| 676 | PyObject *key, *value; |
| 677 | Py_ssize_t pos = 0; |
| 678 | |
| 679 | while (PyDict_Next (main_dict, &pos, &key, &value)) |
| 680 | { |
| 681 | // We have stolen references to the key and value objects in the dictionary; we need to increment |
| 682 | // them now so that Python's garbage collector doesn't collect them out from under us. |
| 683 | Py_INCREF (key); |
| 684 | Py_INCREF (value); |
| 685 | if (strcmp (PyString_AsString (key), dict_name) == 0) |
| 686 | { |
| 687 | g_dict_map[dict] = value; |
| 688 | return value; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
| 695 | |
| 696 | static std::string |
| 697 | GenerateUniqueName (const char* base_name_wanted, |
| 698 | uint32_t& functions_counter, |
| 699 | void* name_token = NULL) |
| 700 | { |
| 701 | StreamString sstr; |
| 702 | |
| 703 | if (!base_name_wanted) |
| 704 | return std::string(); |
| 705 | |
| 706 | if (!name_token) |
| 707 | sstr.Printf ("%s_%d", base_name_wanted, functions_counter++); |
| 708 | else |
| 709 | sstr.Printf ("%s_%p", base_name_wanted, name_token); |
| 710 | |
| 711 | return sstr.GetString(); |
| 712 | } |
| 713 | |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 714 | bool |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 715 | ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 716 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 717 | if (!m_valid_session) |
| 718 | return false; |
| 719 | |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 720 | // We want to call run_one_line, passing in the dictionary and the command string. We cannot do this through |
| 721 | // PyRun_SimpleString here because the command string may contain escaped characters, and putting it inside |
| 722 | // another string to pass to PyRun_SimpleString messes up the escaping. So we use the following more complicated |
| 723 | // method to pass the command string directly down to Python. |
| 724 | |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 725 | Locker locker(this, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 726 | ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0), |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 727 | ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0)); |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 728 | |
| 729 | bool success = false; |
| 730 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 731 | if (command) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | { |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 733 | // Find the correct script interpreter dictionary in the main module. |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 734 | PyObject *script_interpreter_dict = FindSessionDictionary(m_dictionary_name.c_str()); |
| 735 | if (script_interpreter_dict != NULL) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 736 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 737 | PyObject *pfunc = (PyObject*)m_run_one_line; |
Greg Clayton | 6f2f0ab | 2012-04-25 01:49:50 +0000 | [diff] [blame] | 738 | PyObject *pmod = PyImport_AddModule ("lldb.embedded_interpreter"); |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 739 | if (pmod != NULL) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 740 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 741 | PyObject *pmod_dict = PyModule_GetDict (pmod); |
| 742 | if ((pmod_dict != NULL) |
| 743 | && PyDict_Check (pmod_dict)) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 744 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 745 | if (!pfunc) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 746 | { |
| 747 | PyObject *key, *value; |
| 748 | Py_ssize_t pos = 0; |
| 749 | |
| 750 | while (PyDict_Next (pmod_dict, &pos, &key, &value)) |
| 751 | { |
| 752 | Py_INCREF (key); |
| 753 | Py_INCREF (value); |
| 754 | if (strcmp (PyString_AsString (key), "run_one_line") == 0) |
| 755 | { |
| 756 | pfunc = value; |
| 757 | break; |
| 758 | } |
| 759 | } |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 760 | m_run_one_line = pfunc; |
| 761 | } |
| 762 | |
| 763 | if (pfunc && PyCallable_Check (pfunc)) |
| 764 | { |
| 765 | PyObject *pargs = Py_BuildValue("(Os)",script_interpreter_dict,command); |
| 766 | if (pargs != NULL) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 767 | { |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 768 | PyObject *pvalue = NULL; |
| 769 | { // scope for PythonInputReaderManager |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 770 | PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 771 | pvalue = PyObject_CallObject (pfunc, pargs); |
| 772 | } |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 773 | Py_DECREF (pargs); |
| 774 | if (pvalue != NULL) |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 775 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 776 | Py_DECREF (pvalue); |
| 777 | success = true; |
| 778 | } |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 779 | else if (options.GetMaskoutErrors() && PyErr_Occurred ()) |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 780 | { |
| 781 | PyErr_Print(); |
| 782 | PyErr_Clear(); |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | } |
| 786 | } |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 787 | } |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 788 | Py_INCREF (script_interpreter_dict); |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 789 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 790 | |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 791 | if (success) |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 792 | return true; |
| 793 | |
| 794 | // The one-liner failed. Append the error message. |
| 795 | if (result) |
| 796 | result->AppendErrorWithFormat ("python failed attempting to evaluate '%s'\n", command); |
| 797 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | } |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 799 | |
| 800 | if (result) |
| 801 | result->AppendError ("empty command passed to python\n"); |
| 802 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 805 | size_t |
| 806 | ScriptInterpreterPython::InputReaderCallback |
| 807 | ( |
| 808 | void *baton, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 809 | InputReader &reader, |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 810 | InputReaderAction notification, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 811 | const char *bytes, |
| 812 | size_t bytes_len |
| 813 | ) |
| 814 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 815 | lldb::thread_t embedded_interpreter_thread; |
| 816 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT)); |
| 817 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 818 | if (baton == NULL) |
| 819 | return 0; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 820 | |
| 821 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 822 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 823 | if (script_interpreter->m_script_lang != eScriptLanguagePython) |
| 824 | return 0; |
| 825 | |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 826 | StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream(); |
| 827 | bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode(); |
| 828 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 829 | switch (notification) |
| 830 | { |
| 831 | case eInputReaderActivate: |
| 832 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 833 | if (!batch_mode) |
| 834 | { |
| 835 | out_stream->Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n"); |
| 836 | out_stream->Flush(); |
| 837 | } |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 838 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 839 | // Save terminal settings if we can |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 840 | int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor(); |
| 841 | if (input_fd == File::kInvalidDescriptor) |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 842 | input_fd = STDIN_FILENO; |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 843 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 844 | script_interpreter->SaveTerminalState(input_fd); |
Greg Clayton | 9920858 | 2011-02-07 19:04:58 +0000 | [diff] [blame] | 845 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 846 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 847 | ScriptInterpreterPython::Locker locker(script_interpreter, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 848 | ScriptInterpreterPython::Locker::AcquireLock |
| 849 | | ScriptInterpreterPython::Locker::InitSession |
| 850 | | ScriptInterpreterPython::Locker::InitGlobals, |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 851 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 852 | } |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 853 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 854 | char error_str[1024]; |
| 855 | if (script_interpreter->m_embedded_python_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, |
| 856 | sizeof(error_str))) |
| 857 | { |
| 858 | if (log) |
| 859 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in opening master pty (fd = %d).", |
| 860 | script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor()); |
| 861 | embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.embedded-python-loop>", |
| 862 | ScriptInterpreterPython::RunEmbeddedPythonInterpreter, |
| 863 | script_interpreter, NULL); |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 864 | if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread)) |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 865 | { |
| 866 | if (log) |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 867 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 868 | Error detach_error; |
| 869 | Host::ThreadDetach (embedded_interpreter_thread, &detach_error); |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | if (log) |
| 874 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed in creating thread"); |
| 875 | reader.SetIsDone (true); |
| 876 | } |
| 877 | } |
| 878 | else |
| 879 | { |
| 880 | if (log) |
| 881 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed to open master pty "); |
| 882 | reader.SetIsDone (true); |
| 883 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 884 | } |
| 885 | break; |
| 886 | |
| 887 | case eInputReaderDeactivate: |
Greg Clayton | a1cec24 | 2012-01-06 00:47:38 +0000 | [diff] [blame] | 888 | // When another input reader is pushed, don't leave the session... |
| 889 | //script_interpreter->LeaveSession (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 890 | break; |
| 891 | |
| 892 | case eInputReaderReactivate: |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 893 | { |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 894 | ScriptInterpreterPython::Locker locker(script_interpreter, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 895 | ScriptInterpreterPython::Locker::AcquireLock |
| 896 | | ScriptInterpreterPython::Locker::InitSession |
| 897 | | ScriptInterpreterPython::Locker::InitGlobals, |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 898 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 899 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 900 | break; |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 901 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 902 | case eInputReaderAsynchronousOutputWritten: |
| 903 | break; |
| 904 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 905 | case eInputReaderInterrupt: |
| 906 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "raise KeyboardInterrupt\n", 24); |
| 907 | break; |
| 908 | |
| 909 | case eInputReaderEndOfFile: |
| 910 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()\n", 7); |
| 911 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 912 | |
| 913 | case eInputReaderGotToken: |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 914 | if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 915 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 916 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 917 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes, |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 918 | bytes_len); |
| 919 | if (bytes && bytes_len) |
| 920 | { |
| 921 | if ((int) bytes[0] == 4) |
| 922 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()", 6); |
| 923 | else |
| 924 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), bytes, bytes_len); |
| 925 | } |
| 926 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "\n", 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 927 | } |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 928 | else |
| 929 | { |
| 930 | if (log) |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 931 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.", |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 932 | bytes, |
| 933 | bytes_len); |
| 934 | reader.SetIsDone (true); |
| 935 | } |
| 936 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 937 | break; |
| 938 | |
| 939 | case eInputReaderDone: |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 940 | { |
| 941 | Locker locker(script_interpreter, |
| 942 | ScriptInterpreterPython::Locker::AcquireLock, |
| 943 | ScriptInterpreterPython::Locker::FreeAcquiredLock); |
| 944 | script_interpreter->LeaveSession (); |
| 945 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 946 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 947 | // Restore terminal settings if they were validly saved |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 948 | if (log) |
| 949 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Done, closing down input reader."); |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 950 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 951 | script_interpreter->RestoreTerminalState (); |
| 952 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 953 | script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 954 | break; |
| 955 | } |
| 956 | |
| 957 | return bytes_len; |
| 958 | } |
| 959 | |
| 960 | |
| 961 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 962 | ScriptInterpreterPython::ExecuteInterpreterLoop () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 963 | { |
| 964 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 965 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 966 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 967 | |
| 968 | // At the moment, the only time the debugger does not have an input file handle is when this is called |
| 969 | // directly from Python, in which case it is both dangerous and unnecessary (not to mention confusing) to |
| 970 | // try to embed a running interpreter loop inside the already running Python interpreter loop, so we won't |
| 971 | // do it. |
| 972 | |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 973 | if (!debugger.GetInputFile().IsValid()) |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 974 | return; |
| 975 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 976 | InputReaderSP reader_sp (new InputReader(debugger)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 977 | if (reader_sp) |
| 978 | { |
| 979 | Error error (reader_sp->Initialize (ScriptInterpreterPython::InputReaderCallback, |
| 980 | this, // baton |
| 981 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 982 | NULL, // end token |
| 983 | NULL, // prompt |
| 984 | true)); // echo input |
| 985 | |
| 986 | if (error.Success()) |
| 987 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 988 | debugger.PushInputReader (reader_sp); |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 989 | m_embedded_python_input_reader_sp = reader_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | bool |
| 995 | ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string, |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 996 | ScriptInterpreter::ScriptReturnType return_type, |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 997 | void *ret_value, |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 998 | const ExecuteScriptOptions &options) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 999 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1000 | |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 1001 | Locker locker(this, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 1002 | ScriptInterpreterPython::Locker::AcquireLock |
| 1003 | | ScriptInterpreterPython::Locker::InitSession |
| 1004 | | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0), |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1005 | ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0)); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1007 | PyObject *py_return = NULL; |
| 1008 | PyObject *mainmod = PyImport_AddModule ("__main__"); |
| 1009 | PyObject *globals = PyModule_GetDict (mainmod); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1010 | PyObject *locals = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1011 | PyObject *py_error = NULL; |
Johnny Chen | 60a7df5 | 2011-08-11 19:17:45 +0000 | [diff] [blame] | 1012 | bool ret_success = false; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1013 | bool should_decrement_locals = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1014 | int success; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1015 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1016 | locals = FindSessionDictionary(m_dictionary_name.c_str()); |
| 1017 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1018 | if (locals == NULL) |
| 1019 | { |
| 1020 | locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str()); |
| 1021 | should_decrement_locals = true; |
| 1022 | } |
| 1023 | |
| 1024 | if (locals == NULL) |
| 1025 | { |
| 1026 | locals = globals; |
| 1027 | should_decrement_locals = false; |
| 1028 | } |
| 1029 | |
| 1030 | py_error = PyErr_Occurred(); |
| 1031 | if (py_error != NULL) |
| 1032 | PyErr_Clear(); |
| 1033 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1034 | if (in_string != NULL) |
| 1035 | { |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 1036 | { // scope for PythonInputReaderManager |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1037 | PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 1038 | py_return = PyRun_String (in_string, Py_eval_input, globals, locals); |
| 1039 | if (py_return == NULL) |
| 1040 | { |
| 1041 | py_error = PyErr_Occurred (); |
| 1042 | if (py_error != NULL) |
| 1043 | PyErr_Clear (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1044 | |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 1045 | py_return = PyRun_String (in_string, Py_single_input, globals, locals); |
| 1046 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1049 | if (locals != NULL |
| 1050 | && should_decrement_locals) |
| 1051 | Py_DECREF (locals); |
| 1052 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1053 | if (py_return != NULL) |
| 1054 | { |
| 1055 | switch (return_type) |
| 1056 | { |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1057 | case eScriptReturnTypeCharPtr: // "char *" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1058 | { |
| 1059 | const char format[3] = "s#"; |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 1060 | success = PyArg_Parse (py_return, format, (char **) ret_value); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1063 | case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return == Py_None |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1064 | { |
| 1065 | const char format[3] = "z"; |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 1066 | success = PyArg_Parse (py_return, format, (char **) ret_value); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1069 | case eScriptReturnTypeBool: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | { |
| 1071 | const char format[2] = "b"; |
| 1072 | success = PyArg_Parse (py_return, format, (bool *) ret_value); |
| 1073 | break; |
| 1074 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1075 | case eScriptReturnTypeShortInt: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | { |
| 1077 | const char format[2] = "h"; |
| 1078 | success = PyArg_Parse (py_return, format, (short *) ret_value); |
| 1079 | break; |
| 1080 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1081 | case eScriptReturnTypeShortIntUnsigned: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | { |
| 1083 | const char format[2] = "H"; |
| 1084 | success = PyArg_Parse (py_return, format, (unsigned short *) ret_value); |
| 1085 | break; |
| 1086 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1087 | case eScriptReturnTypeInt: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1088 | { |
| 1089 | const char format[2] = "i"; |
| 1090 | success = PyArg_Parse (py_return, format, (int *) ret_value); |
| 1091 | break; |
| 1092 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1093 | case eScriptReturnTypeIntUnsigned: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1094 | { |
| 1095 | const char format[2] = "I"; |
| 1096 | success = PyArg_Parse (py_return, format, (unsigned int *) ret_value); |
| 1097 | break; |
| 1098 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1099 | case eScriptReturnTypeLongInt: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1100 | { |
| 1101 | const char format[2] = "l"; |
| 1102 | success = PyArg_Parse (py_return, format, (long *) ret_value); |
| 1103 | break; |
| 1104 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1105 | case eScriptReturnTypeLongIntUnsigned: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1106 | { |
| 1107 | const char format[2] = "k"; |
| 1108 | success = PyArg_Parse (py_return, format, (unsigned long *) ret_value); |
| 1109 | break; |
| 1110 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1111 | case eScriptReturnTypeLongLong: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1112 | { |
| 1113 | const char format[2] = "L"; |
| 1114 | success = PyArg_Parse (py_return, format, (long long *) ret_value); |
| 1115 | break; |
| 1116 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1117 | case eScriptReturnTypeLongLongUnsigned: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1118 | { |
| 1119 | const char format[2] = "K"; |
| 1120 | success = PyArg_Parse (py_return, format, (unsigned long long *) ret_value); |
| 1121 | break; |
| 1122 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1123 | case eScriptReturnTypeFloat: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1124 | { |
| 1125 | const char format[2] = "f"; |
| 1126 | success = PyArg_Parse (py_return, format, (float *) ret_value); |
| 1127 | break; |
| 1128 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1129 | case eScriptReturnTypeDouble: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1130 | { |
| 1131 | const char format[2] = "d"; |
| 1132 | success = PyArg_Parse (py_return, format, (double *) ret_value); |
| 1133 | break; |
| 1134 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 1135 | case eScriptReturnTypeChar: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1136 | { |
| 1137 | const char format[2] = "c"; |
| 1138 | success = PyArg_Parse (py_return, format, (char *) ret_value); |
| 1139 | break; |
| 1140 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1141 | } |
| 1142 | Py_DECREF (py_return); |
| 1143 | if (success) |
| 1144 | ret_success = true; |
| 1145 | else |
| 1146 | ret_success = false; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | py_error = PyErr_Occurred(); |
| 1151 | if (py_error != NULL) |
| 1152 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | ret_success = false; |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1154 | if (options.GetMaskoutErrors()) |
| 1155 | { |
| 1156 | if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError)) |
| 1157 | PyErr_Print (); |
| 1158 | PyErr_Clear(); |
| 1159 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1160 | } |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1161 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1162 | return ret_success; |
| 1163 | } |
| 1164 | |
| 1165 | bool |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1166 | ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, const ExecuteScriptOptions &options) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1167 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 1168 | |
| 1169 | |
| 1170 | Locker locker(this, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 1171 | ScriptInterpreterPython::Locker::AcquireLock |
| 1172 | | ScriptInterpreterPython::Locker::InitSession |
| 1173 | | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0), |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1174 | ScriptInterpreterPython::Locker::FreeAcquiredLock | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::TearDownSession : 0)); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1176 | bool success = false; |
| 1177 | PyObject *py_return = NULL; |
| 1178 | PyObject *mainmod = PyImport_AddModule ("__main__"); |
| 1179 | PyObject *globals = PyModule_GetDict (mainmod); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1180 | PyObject *locals = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1181 | PyObject *py_error = NULL; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1182 | bool should_decrement_locals = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1183 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1184 | locals = FindSessionDictionary(m_dictionary_name.c_str()); |
| 1185 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1186 | if (locals == NULL) |
| 1187 | { |
| 1188 | locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str()); |
| 1189 | should_decrement_locals = true; |
| 1190 | } |
| 1191 | |
| 1192 | if (locals == NULL) |
| 1193 | { |
| 1194 | locals = globals; |
| 1195 | should_decrement_locals = false; |
| 1196 | } |
| 1197 | |
| 1198 | py_error = PyErr_Occurred(); |
| 1199 | if (py_error != NULL) |
| 1200 | PyErr_Clear(); |
| 1201 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1202 | if (in_string != NULL) |
| 1203 | { |
| 1204 | struct _node *compiled_node = PyParser_SimpleParseString (in_string, Py_file_input); |
| 1205 | if (compiled_node) |
| 1206 | { |
| 1207 | PyCodeObject *compiled_code = PyNode_Compile (compiled_node, "temp.py"); |
| 1208 | if (compiled_code) |
| 1209 | { |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 1210 | { // scope for PythonInputReaderManager |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1211 | PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL); |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 1212 | py_return = PyEval_EvalCode (compiled_code, globals, locals); |
| 1213 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1214 | if (py_return != NULL) |
| 1215 | { |
| 1216 | success = true; |
| 1217 | Py_DECREF (py_return); |
| 1218 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1219 | if (locals && should_decrement_locals) |
| 1220 | Py_DECREF (locals); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | py_error = PyErr_Occurred (); |
| 1226 | if (py_error != NULL) |
| 1227 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1228 | success = false; |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1229 | if (options.GetMaskoutErrors()) |
| 1230 | { |
| 1231 | if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError)) |
| 1232 | PyErr_Print (); |
| 1233 | PyErr_Clear(); |
| 1234 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | return success; |
| 1238 | } |
| 1239 | |
| 1240 | static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end."; |
| 1241 | |
| 1242 | size_t |
| 1243 | ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback |
| 1244 | ( |
| 1245 | void *baton, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1246 | InputReader &reader, |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1247 | InputReaderAction notification, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1248 | const char *bytes, |
| 1249 | size_t bytes_len |
| 1250 | ) |
| 1251 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1252 | static StringList commands_in_progress; |
| 1253 | |
| 1254 | StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream(); |
| 1255 | bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode(); |
| 1256 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1257 | switch (notification) |
| 1258 | { |
| 1259 | case eInputReaderActivate: |
| 1260 | { |
| 1261 | commands_in_progress.Clear(); |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1262 | if (!batch_mode) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1263 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1264 | out_stream->Printf ("%s\n", g_reader_instructions); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1265 | if (reader.GetPrompt()) |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1266 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1267 | out_stream->Flush (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | break; |
| 1271 | |
| 1272 | case eInputReaderDeactivate: |
| 1273 | break; |
| 1274 | |
| 1275 | case eInputReaderReactivate: |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1276 | if (reader.GetPrompt() && !batch_mode) |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1277 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1278 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1279 | out_stream->Flush (); |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1280 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1283 | case eInputReaderAsynchronousOutputWritten: |
| 1284 | break; |
| 1285 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1286 | case eInputReaderGotToken: |
| 1287 | { |
| 1288 | std::string temp_string (bytes, bytes_len); |
| 1289 | commands_in_progress.AppendString (temp_string.c_str()); |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1290 | if (!reader.IsDone() && reader.GetPrompt() && !batch_mode) |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1291 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1292 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1293 | out_stream->Flush (); |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1294 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1295 | } |
| 1296 | break; |
| 1297 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1298 | case eInputReaderEndOfFile: |
| 1299 | case eInputReaderInterrupt: |
| 1300 | // Control-c (SIGINT) & control-d both mean finish & exit. |
| 1301 | reader.SetIsDone(true); |
| 1302 | |
| 1303 | // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command. |
| 1304 | if (notification == eInputReaderInterrupt) |
| 1305 | commands_in_progress.Clear(); |
| 1306 | |
| 1307 | // Fall through here... |
| 1308 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1309 | case eInputReaderDone: |
| 1310 | { |
| 1311 | BreakpointOptions *bp_options = (BreakpointOptions *)baton; |
| 1312 | std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData()); |
| 1313 | data_ap->user_source.AppendList (commands_in_progress); |
| 1314 | if (data_ap.get()) |
| 1315 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1316 | ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1317 | if (interpreter) |
| 1318 | { |
| 1319 | if (interpreter->GenerateBreakpointCommandCallbackData (data_ap->user_source, |
| 1320 | data_ap->script_source)) |
| 1321 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1322 | BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release())); |
| 1323 | bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1324 | } |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1325 | else if (!batch_mode) |
| 1326 | { |
| 1327 | out_stream->Printf ("Warning: No command attached to breakpoint.\n"); |
| 1328 | out_stream->Flush(); |
| 1329 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1330 | } |
| 1331 | else |
| 1332 | { |
Caroline Tice | 892fadd | 2011-06-16 16:27:19 +0000 | [diff] [blame] | 1333 | if (!batch_mode) |
| 1334 | { |
| 1335 | out_stream->Printf ("Warning: Unable to find script intepreter; no command attached to breakpoint.\n"); |
| 1336 | out_stream->Flush(); |
| 1337 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | break; |
| 1342 | |
| 1343 | } |
| 1344 | |
| 1345 | return bytes_len; |
| 1346 | } |
| 1347 | |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1348 | size_t |
| 1349 | ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback |
| 1350 | ( |
| 1351 | void *baton, |
| 1352 | InputReader &reader, |
| 1353 | InputReaderAction notification, |
| 1354 | const char *bytes, |
| 1355 | size_t bytes_len |
| 1356 | ) |
| 1357 | { |
| 1358 | static StringList commands_in_progress; |
| 1359 | |
| 1360 | StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream(); |
| 1361 | bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode(); |
| 1362 | |
| 1363 | switch (notification) |
| 1364 | { |
| 1365 | case eInputReaderActivate: |
| 1366 | { |
| 1367 | commands_in_progress.Clear(); |
| 1368 | if (!batch_mode) |
| 1369 | { |
| 1370 | out_stream->Printf ("%s\n", g_reader_instructions); |
| 1371 | if (reader.GetPrompt()) |
| 1372 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1373 | out_stream->Flush (); |
| 1374 | } |
| 1375 | } |
| 1376 | break; |
| 1377 | |
| 1378 | case eInputReaderDeactivate: |
| 1379 | break; |
| 1380 | |
| 1381 | case eInputReaderReactivate: |
| 1382 | if (reader.GetPrompt() && !batch_mode) |
| 1383 | { |
| 1384 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1385 | out_stream->Flush (); |
| 1386 | } |
| 1387 | break; |
| 1388 | |
| 1389 | case eInputReaderAsynchronousOutputWritten: |
| 1390 | break; |
| 1391 | |
| 1392 | case eInputReaderGotToken: |
| 1393 | { |
| 1394 | std::string temp_string (bytes, bytes_len); |
| 1395 | commands_in_progress.AppendString (temp_string.c_str()); |
| 1396 | if (!reader.IsDone() && reader.GetPrompt() && !batch_mode) |
| 1397 | { |
| 1398 | out_stream->Printf ("%s", reader.GetPrompt()); |
| 1399 | out_stream->Flush (); |
| 1400 | } |
| 1401 | } |
| 1402 | break; |
| 1403 | |
| 1404 | case eInputReaderEndOfFile: |
| 1405 | case eInputReaderInterrupt: |
| 1406 | // Control-c (SIGINT) & control-d both mean finish & exit. |
| 1407 | reader.SetIsDone(true); |
| 1408 | |
| 1409 | // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command. |
| 1410 | if (notification == eInputReaderInterrupt) |
| 1411 | commands_in_progress.Clear(); |
| 1412 | |
| 1413 | // Fall through here... |
| 1414 | |
| 1415 | case eInputReaderDone: |
| 1416 | { |
| 1417 | WatchpointOptions *wp_options = (WatchpointOptions *)baton; |
| 1418 | std::auto_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); |
| 1419 | data_ap->user_source.AppendList (commands_in_progress); |
| 1420 | if (data_ap.get()) |
| 1421 | { |
| 1422 | ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
| 1423 | if (interpreter) |
| 1424 | { |
| 1425 | if (interpreter->GenerateWatchpointCommandCallbackData (data_ap->user_source, |
| 1426 | data_ap->script_source)) |
| 1427 | { |
| 1428 | BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); |
| 1429 | wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp); |
| 1430 | } |
| 1431 | else if (!batch_mode) |
| 1432 | { |
| 1433 | out_stream->Printf ("Warning: No command attached to breakpoint.\n"); |
| 1434 | out_stream->Flush(); |
| 1435 | } |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | if (!batch_mode) |
| 1440 | { |
| 1441 | out_stream->Printf ("Warning: Unable to find script intepreter; no command attached to breakpoint.\n"); |
| 1442 | out_stream->Flush(); |
| 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | break; |
| 1448 | |
| 1449 | } |
| 1450 | |
| 1451 | return bytes_len; |
| 1452 | } |
| 1453 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1454 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1455 | ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1456 | CommandReturnObject &result) |
| 1457 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1458 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 1459 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1460 | InputReaderSP reader_sp (new InputReader (debugger)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1461 | |
| 1462 | if (reader_sp) |
| 1463 | { |
| 1464 | Error err = reader_sp->Initialize ( |
| 1465 | ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback, |
| 1466 | bp_options, // baton |
| 1467 | eInputReaderGranularityLine, // token size, for feeding data to callback function |
| 1468 | "DONE", // end token |
| 1469 | "> ", // prompt |
| 1470 | true); // echo input |
| 1471 | |
| 1472 | if (err.Success()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1473 | debugger.PushInputReader (reader_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1474 | else |
| 1475 | { |
| 1476 | result.AppendError (err.AsCString()); |
| 1477 | result.SetStatus (eReturnStatusFailed); |
| 1478 | } |
| 1479 | } |
| 1480 | else |
| 1481 | { |
| 1482 | result.AppendError("out of memory"); |
| 1483 | result.SetStatus (eReturnStatusFailed); |
| 1484 | } |
| 1485 | } |
| 1486 | |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1487 | void |
| 1488 | ScriptInterpreterPython::CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options, |
| 1489 | CommandReturnObject &result) |
| 1490 | { |
| 1491 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 1492 | |
| 1493 | InputReaderSP reader_sp (new InputReader (debugger)); |
| 1494 | |
| 1495 | if (reader_sp) |
| 1496 | { |
| 1497 | Error err = reader_sp->Initialize ( |
| 1498 | ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback, |
| 1499 | wp_options, // baton |
| 1500 | eInputReaderGranularityLine, // token size, for feeding data to callback function |
| 1501 | "DONE", // end token |
| 1502 | "> ", // prompt |
| 1503 | true); // echo input |
| 1504 | |
| 1505 | if (err.Success()) |
| 1506 | debugger.PushInputReader (reader_sp); |
| 1507 | else |
| 1508 | { |
| 1509 | result.AppendError (err.AsCString()); |
| 1510 | result.SetStatus (eReturnStatusFailed); |
| 1511 | } |
| 1512 | } |
| 1513 | else |
| 1514 | { |
| 1515 | result.AppendError("out of memory"); |
| 1516 | result.SetStatus (eReturnStatusFailed); |
| 1517 | } |
| 1518 | } |
| 1519 | |
Johnny Chen | 3e0571b | 2010-09-11 00:23:59 +0000 | [diff] [blame] | 1520 | // Set a Python one-liner as the callback for the breakpoint. |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1521 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1522 | ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options, |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1523 | const char *oneliner) |
| 1524 | { |
| 1525 | std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData()); |
| 1526 | |
| 1527 | // It's necessary to set both user_source and script_source to the oneliner. |
| 1528 | // The former is used to generate callback description (as in breakpoint command list) |
| 1529 | // while the latter is used for Python to interpret during the actual callback. |
Caroline Tice | 5136f94 | 2010-09-27 21:35:15 +0000 | [diff] [blame] | 1530 | |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1531 | data_ap->user_source.AppendString (oneliner); |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1532 | data_ap->script_source.assign (oneliner); |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1533 | |
Caroline Tice | 5136f94 | 2010-09-27 21:35:15 +0000 | [diff] [blame] | 1534 | if (GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source)) |
| 1535 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1536 | BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release())); |
| 1537 | bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp); |
Caroline Tice | 5136f94 | 2010-09-27 21:35:15 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
| 1542 | |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1543 | // Set a Python one-liner as the callback for the watchpoint. |
| 1544 | void |
| 1545 | ScriptInterpreterPython::SetWatchpointCommandCallback (WatchpointOptions *wp_options, |
| 1546 | const char *oneliner) |
| 1547 | { |
| 1548 | std::auto_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData()); |
| 1549 | |
| 1550 | // It's necessary to set both user_source and script_source to the oneliner. |
| 1551 | // The former is used to generate callback description (as in watchpoint command list) |
| 1552 | // while the latter is used for Python to interpret during the actual callback. |
| 1553 | |
| 1554 | data_ap->user_source.AppendString (oneliner); |
| 1555 | data_ap->script_source.assign (oneliner); |
| 1556 | |
| 1557 | if (GenerateWatchpointCommandCallbackData (data_ap->user_source, data_ap->script_source)) |
| 1558 | { |
| 1559 | BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release())); |
| 1560 | wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp); |
| 1561 | } |
| 1562 | |
| 1563 | return; |
| 1564 | } |
| 1565 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1566 | bool |
| 1567 | ScriptInterpreterPython::ExportFunctionDefinitionToInterpreter (StringList &function_def) |
| 1568 | { |
| 1569 | // Convert StringList to one long, newline delimited, const char *. |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1570 | std::string function_def_string(function_def.CopyList()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1571 | |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 1572 | return ExecuteMultipleLines (function_def_string.c_str(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1575 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1576 | ScriptInterpreterPython::GenerateFunction(const char *signature, const StringList &input) |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1577 | { |
| 1578 | int num_lines = input.GetSize (); |
| 1579 | if (num_lines == 0) |
| 1580 | return false; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1581 | |
| 1582 | if (!signature || *signature == 0) |
| 1583 | return false; |
| 1584 | |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1585 | StreamString sstr; |
| 1586 | StringList auto_generated_function; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1587 | auto_generated_function.AppendString (signature); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1588 | auto_generated_function.AppendString (" global_dict = globals()"); // Grab the global dictionary |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1589 | auto_generated_function.AppendString (" new_keys = internal_dict.keys()"); // Make a list of keys in the session dict |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1590 | auto_generated_function.AppendString (" old_keys = global_dict.keys()"); // Save list of keys in global dict |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1591 | auto_generated_function.AppendString (" global_dict.update (internal_dict)"); // Add the session dictionary to the |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1592 | // global dictionary. |
| 1593 | |
| 1594 | // Wrap everything up inside the function, increasing the indentation. |
| 1595 | |
| 1596 | for (int i = 0; i < num_lines; ++i) |
| 1597 | { |
| 1598 | sstr.Clear (); |
| 1599 | sstr.Printf (" %s", input.GetStringAtIndex (i)); |
| 1600 | auto_generated_function.AppendString (sstr.GetData()); |
| 1601 | } |
| 1602 | auto_generated_function.AppendString (" for key in new_keys:"); // Iterate over all the keys from session dict |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1603 | auto_generated_function.AppendString (" internal_dict[key] = global_dict[key]"); // Update session dict values |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1604 | auto_generated_function.AppendString (" if key not in old_keys:"); // If key was not originally in global dict |
| 1605 | auto_generated_function.AppendString (" del global_dict[key]"); // ...then remove key/value from global dict |
| 1606 | |
| 1607 | // Verify that the results are valid Python. |
| 1608 | |
| 1609 | if (!ExportFunctionDefinitionToInterpreter (auto_generated_function)) |
| 1610 | return false; |
| 1611 | |
| 1612 | return true; |
| 1613 | |
| 1614 | } |
| 1615 | |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1616 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1617 | ScriptInterpreterPython::GenerateTypeScriptFunction (StringList &user_input, std::string& output, void* name_token) |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1618 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1619 | static uint32_t num_created_functions = 0; |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1620 | user_input.RemoveBlankLines (); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1621 | StreamString sstr; |
| 1622 | |
| 1623 | // Check to see if we have any data; if not, just return. |
| 1624 | if (user_input.GetSize() == 0) |
| 1625 | return false; |
| 1626 | |
| 1627 | // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the |
| 1628 | // ValueObject as parameter to the function. |
| 1629 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1630 | std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_type_print_func", num_created_functions, name_token)); |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1631 | sstr.Printf ("def %s (valobj, internal_dict):", auto_generated_function_name.c_str()); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1632 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1633 | if (!GenerateFunction(sstr.GetData(), user_input)) |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1634 | return false; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1635 | |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1636 | // Store the name of the auto-generated function to be called. |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1637 | output.assign(auto_generated_function_name); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1638 | return true; |
| 1639 | } |
| 1640 | |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1641 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1642 | ScriptInterpreterPython::GenerateScriptAliasFunction (StringList &user_input, std::string &output) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1643 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1644 | static uint32_t num_created_functions = 0; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1645 | user_input.RemoveBlankLines (); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1646 | StreamString sstr; |
| 1647 | |
| 1648 | // Check to see if we have any data; if not, just return. |
| 1649 | if (user_input.GetSize() == 0) |
| 1650 | return false; |
| 1651 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1652 | std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_cmd_alias_func", num_created_functions)); |
| 1653 | |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1654 | sstr.Printf ("def %s (debugger, args, result, internal_dict):", auto_generated_function_name.c_str()); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1655 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1656 | if (!GenerateFunction(sstr.GetData(),user_input)) |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1657 | return false; |
| 1658 | |
| 1659 | // Store the name of the auto-generated function to be called. |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1660 | output.assign(auto_generated_function_name); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 1661 | return true; |
| 1662 | } |
| 1663 | |
| 1664 | |
| 1665 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1666 | ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, std::string &output, void* name_token) |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1667 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1668 | static uint32_t num_created_classes = 0; |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1669 | user_input.RemoveBlankLines (); |
| 1670 | int num_lines = user_input.GetSize (); |
| 1671 | StreamString sstr; |
| 1672 | |
| 1673 | // Check to see if we have any data; if not, just return. |
| 1674 | if (user_input.GetSize() == 0) |
| 1675 | return false; |
| 1676 | |
| 1677 | // Wrap all user input into a Python class |
| 1678 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1679 | std::string auto_generated_class_name(GenerateUniqueName("lldb_autogen_python_type_synth_class",num_created_classes,name_token)); |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1680 | |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1681 | StringList auto_generated_class; |
| 1682 | |
| 1683 | // Create the function name & definition string. |
| 1684 | |
| 1685 | sstr.Printf ("class %s:", auto_generated_class_name.c_str()); |
| 1686 | auto_generated_class.AppendString (sstr.GetData()); |
| 1687 | |
| 1688 | // Wrap everything up inside the class, increasing the indentation. |
| 1689 | |
| 1690 | for (int i = 0; i < num_lines; ++i) |
| 1691 | { |
| 1692 | sstr.Clear (); |
| 1693 | sstr.Printf (" %s", user_input.GetStringAtIndex (i)); |
| 1694 | auto_generated_class.AppendString (sstr.GetData()); |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | // Verify that the results are valid Python. |
| 1699 | // (even though the method is ExportFunctionDefinitionToInterpreter, a class will actually be exported) |
| 1700 | // (TODO: rename that method to ExportDefinitionToInterpreter) |
| 1701 | if (!ExportFunctionDefinitionToInterpreter (auto_generated_class)) |
| 1702 | return false; |
| 1703 | |
| 1704 | // Store the name of the auto-generated class |
| 1705 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1706 | output.assign(auto_generated_class_name); |
Enrico Granata | e89ab7b | 2011-07-25 16:59:05 +0000 | [diff] [blame] | 1707 | return true; |
| 1708 | } |
| 1709 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1710 | lldb::ScriptInterpreterObjectSP |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 1711 | ScriptInterpreterPython::CreateOSPlugin (std::string class_name, |
| 1712 | lldb::ProcessSP process_sp) |
| 1713 | { |
| 1714 | if (class_name.empty()) |
| 1715 | return lldb::ScriptInterpreterObjectSP(); |
| 1716 | |
| 1717 | if (!process_sp) |
| 1718 | return lldb::ScriptInterpreterObjectSP(); |
| 1719 | |
| 1720 | void* ret_val; |
| 1721 | |
| 1722 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 1723 | Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock); |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 1724 | ret_val = g_swig_create_os_plugin (class_name, |
| 1725 | m_dictionary_name.c_str(), |
| 1726 | process_sp); |
| 1727 | } |
| 1728 | |
| 1729 | return MakeScriptObject(ret_val); |
| 1730 | } |
| 1731 | |
| 1732 | lldb::ScriptInterpreterObjectSP |
| 1733 | ScriptInterpreterPython::OSPlugin_QueryForRegisterInfo (lldb::ScriptInterpreterObjectSP object) |
| 1734 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 1735 | Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock); |
| 1736 | |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 1737 | static char callee_name[] = "get_register_info"; |
| 1738 | |
| 1739 | if (!object) |
| 1740 | return lldb::ScriptInterpreterObjectSP(); |
| 1741 | |
| 1742 | PyObject* implementor = (PyObject*)object->GetObject(); |
| 1743 | |
| 1744 | if (implementor == NULL || implementor == Py_None) |
| 1745 | return lldb::ScriptInterpreterObjectSP(); |
| 1746 | |
| 1747 | PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); |
| 1748 | |
| 1749 | if (PyErr_Occurred()) |
| 1750 | { |
| 1751 | PyErr_Clear(); |
| 1752 | } |
| 1753 | |
| 1754 | if (pmeth == NULL || pmeth == Py_None) |
| 1755 | { |
| 1756 | Py_XDECREF(pmeth); |
| 1757 | return lldb::ScriptInterpreterObjectSP(); |
| 1758 | } |
| 1759 | |
| 1760 | if (PyCallable_Check(pmeth) == 0) |
| 1761 | { |
| 1762 | if (PyErr_Occurred()) |
| 1763 | { |
| 1764 | PyErr_Clear(); |
| 1765 | } |
| 1766 | |
| 1767 | Py_XDECREF(pmeth); |
| 1768 | return lldb::ScriptInterpreterObjectSP(); |
| 1769 | } |
| 1770 | |
| 1771 | if (PyErr_Occurred()) |
| 1772 | { |
| 1773 | PyErr_Clear(); |
| 1774 | } |
| 1775 | |
| 1776 | Py_XDECREF(pmeth); |
| 1777 | |
| 1778 | // right now we know this function exists and is callable.. |
| 1779 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL); |
| 1780 | |
| 1781 | // if it fails, print the error but otherwise go on |
| 1782 | if (PyErr_Occurred()) |
| 1783 | { |
| 1784 | PyErr_Print(); |
| 1785 | PyErr_Clear(); |
| 1786 | } |
| 1787 | |
| 1788 | return MakeScriptObject(py_return); |
| 1789 | } |
| 1790 | |
| 1791 | lldb::ScriptInterpreterObjectSP |
Enrico Granata | 3e85b63 | 2012-08-24 00:51:29 +0000 | [diff] [blame] | 1792 | ScriptInterpreterPython::OSPlugin_QueryForThreadsInfo (lldb::ScriptInterpreterObjectSP object) |
| 1793 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 1794 | Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock); |
| 1795 | |
Enrico Granata | 3e85b63 | 2012-08-24 00:51:29 +0000 | [diff] [blame] | 1796 | static char callee_name[] = "get_thread_info"; |
| 1797 | |
| 1798 | if (!object) |
| 1799 | return lldb::ScriptInterpreterObjectSP(); |
| 1800 | |
| 1801 | PyObject* implementor = (PyObject*)object->GetObject(); |
| 1802 | |
| 1803 | if (implementor == NULL || implementor == Py_None) |
| 1804 | return lldb::ScriptInterpreterObjectSP(); |
| 1805 | |
| 1806 | PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); |
| 1807 | |
| 1808 | if (PyErr_Occurred()) |
| 1809 | { |
| 1810 | PyErr_Clear(); |
| 1811 | } |
| 1812 | |
| 1813 | if (pmeth == NULL || pmeth == Py_None) |
| 1814 | { |
| 1815 | Py_XDECREF(pmeth); |
| 1816 | return lldb::ScriptInterpreterObjectSP(); |
| 1817 | } |
| 1818 | |
| 1819 | if (PyCallable_Check(pmeth) == 0) |
| 1820 | { |
| 1821 | if (PyErr_Occurred()) |
| 1822 | { |
| 1823 | PyErr_Clear(); |
| 1824 | } |
| 1825 | |
| 1826 | Py_XDECREF(pmeth); |
| 1827 | return lldb::ScriptInterpreterObjectSP(); |
| 1828 | } |
| 1829 | |
| 1830 | if (PyErr_Occurred()) |
| 1831 | { |
| 1832 | PyErr_Clear(); |
| 1833 | } |
| 1834 | |
| 1835 | Py_XDECREF(pmeth); |
| 1836 | |
| 1837 | // right now we know this function exists and is callable.. |
| 1838 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL); |
| 1839 | |
| 1840 | // if it fails, print the error but otherwise go on |
| 1841 | if (PyErr_Occurred()) |
| 1842 | { |
| 1843 | PyErr_Print(); |
| 1844 | PyErr_Clear(); |
| 1845 | } |
| 1846 | |
| 1847 | return MakeScriptObject(py_return); |
| 1848 | } |
| 1849 | |
| 1850 | lldb::ScriptInterpreterObjectSP |
Greg Clayton | b8f126a | 2012-08-24 05:45:15 +0000 | [diff] [blame] | 1851 | ScriptInterpreterPython::OSPlugin_QueryForRegisterContextData (lldb::ScriptInterpreterObjectSP object, |
| 1852 | lldb::tid_t thread_id) |
Enrico Granata | 3e85b63 | 2012-08-24 00:51:29 +0000 | [diff] [blame] | 1853 | { |
Enrico Granata | cec963a | 2012-08-24 01:34:39 +0000 | [diff] [blame] | 1854 | Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock); |
| 1855 | |
Enrico Granata | 3e85b63 | 2012-08-24 00:51:29 +0000 | [diff] [blame] | 1856 | static char callee_name[] = "get_register_data"; |
| 1857 | static char param_format[] = "l"; |
| 1858 | |
| 1859 | if (!object) |
| 1860 | return lldb::ScriptInterpreterObjectSP(); |
| 1861 | |
| 1862 | PyObject* implementor = (PyObject*)object->GetObject(); |
| 1863 | |
| 1864 | if (implementor == NULL || implementor == Py_None) |
| 1865 | return lldb::ScriptInterpreterObjectSP(); |
| 1866 | |
| 1867 | PyObject* pmeth = PyObject_GetAttrString(implementor, callee_name); |
| 1868 | |
| 1869 | if (PyErr_Occurred()) |
| 1870 | { |
| 1871 | PyErr_Clear(); |
| 1872 | } |
| 1873 | |
| 1874 | if (pmeth == NULL || pmeth == Py_None) |
| 1875 | { |
| 1876 | Py_XDECREF(pmeth); |
| 1877 | return lldb::ScriptInterpreterObjectSP(); |
| 1878 | } |
| 1879 | |
| 1880 | if (PyCallable_Check(pmeth) == 0) |
| 1881 | { |
| 1882 | if (PyErr_Occurred()) |
| 1883 | { |
| 1884 | PyErr_Clear(); |
| 1885 | } |
| 1886 | |
| 1887 | Py_XDECREF(pmeth); |
| 1888 | return lldb::ScriptInterpreterObjectSP(); |
| 1889 | } |
| 1890 | |
| 1891 | if (PyErr_Occurred()) |
| 1892 | { |
| 1893 | PyErr_Clear(); |
| 1894 | } |
| 1895 | |
| 1896 | Py_XDECREF(pmeth); |
| 1897 | |
| 1898 | // right now we know this function exists and is callable.. |
| 1899 | PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, thread_id); |
| 1900 | |
| 1901 | // if it fails, print the error but otherwise go on |
| 1902 | if (PyErr_Occurred()) |
| 1903 | { |
| 1904 | PyErr_Print(); |
| 1905 | PyErr_Clear(); |
| 1906 | } |
| 1907 | |
| 1908 | return MakeScriptObject(py_return); |
| 1909 | } |
| 1910 | |
| 1911 | lldb::ScriptInterpreterObjectSP |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1912 | ScriptInterpreterPython::CreateSyntheticScriptedProvider (std::string class_name, |
| 1913 | lldb::ValueObjectSP valobj) |
| 1914 | { |
| 1915 | if (class_name.empty()) |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1916 | return lldb::ScriptInterpreterObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1917 | |
| 1918 | if (!valobj.get()) |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1919 | return lldb::ScriptInterpreterObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1920 | |
Greg Clayton | b4d7fc0 | 2012-02-17 07:49:44 +0000 | [diff] [blame] | 1921 | ExecutionContext exe_ctx (valobj->GetExecutionContextRef()); |
| 1922 | Target *target = exe_ctx.GetTargetPtr(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1923 | |
| 1924 | if (!target) |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1925 | return lldb::ScriptInterpreterObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1926 | |
| 1927 | Debugger &debugger = target->GetDebugger(); |
| 1928 | ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); |
| 1929 | ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter; |
| 1930 | |
| 1931 | if (!script_interpreter) |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1932 | return lldb::ScriptInterpreterObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1933 | |
| 1934 | void* ret_val; |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 1935 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1936 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 1937 | Locker py_lock(this); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 1938 | ret_val = g_swig_synthetic_script (class_name, |
| 1939 | python_interpreter->m_dictionary_name.c_str(), |
| 1940 | valobj); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1943 | return MakeScriptObject(ret_val); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1946 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1947 | ScriptInterpreterPython::GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token) |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1948 | { |
Enrico Granata | 16376ed | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 1949 | StringList input; |
| 1950 | input.SplitIntoLines(oneliner, strlen(oneliner)); |
| 1951 | return GenerateTypeScriptFunction(input, output, name_token); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1954 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1955 | ScriptInterpreterPython::GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token) |
Enrico Granata | 16376ed | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 1956 | { |
| 1957 | StringList input; |
| 1958 | input.SplitIntoLines(oneliner, strlen(oneliner)); |
| 1959 | return GenerateTypeSynthClass(input, output, name_token); |
| 1960 | } |
| 1961 | |
| 1962 | |
| 1963 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1964 | ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, std::string& output) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1965 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1966 | static uint32_t num_created_functions = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1967 | user_input.RemoveBlankLines (); |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1968 | StreamString sstr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1969 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1970 | if (user_input.GetSize() == 0) |
| 1971 | return false; |
| 1972 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1973 | std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_bp_callback_func_",num_created_functions)); |
Enrico Granata | c1ca9dc | 2012-08-08 02:06:30 +0000 | [diff] [blame] | 1974 | sstr.Printf ("def %s (frame, bp_loc, internal_dict):", auto_generated_function_name.c_str()); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1975 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1976 | if (!GenerateFunction(sstr.GetData(), user_input)) |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1977 | return false; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1978 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1979 | // Store the name of the auto-generated function to be called. |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 1980 | output.assign(auto_generated_function_name); |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1981 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 1984 | bool |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 1985 | ScriptInterpreterPython::GenerateWatchpointCommandCallbackData (StringList &user_input, std::string& output) |
| 1986 | { |
| 1987 | static uint32_t num_created_functions = 0; |
| 1988 | user_input.RemoveBlankLines (); |
| 1989 | StreamString sstr; |
| 1990 | |
| 1991 | if (user_input.GetSize() == 0) |
| 1992 | return false; |
| 1993 | |
| 1994 | std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_wp_callback_func_",num_created_functions)); |
| 1995 | sstr.Printf ("def %s (frame, wp, internal_dict):", auto_generated_function_name.c_str()); |
| 1996 | |
| 1997 | if (!GenerateFunction(sstr.GetData(), user_input)) |
| 1998 | return false; |
| 1999 | |
| 2000 | // Store the name of the auto-generated function to be called. |
| 2001 | output.assign(auto_generated_function_name); |
| 2002 | return true; |
| 2003 | } |
| 2004 | |
| 2005 | bool |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2006 | ScriptInterpreterPython::GetScriptedSummary (const char *python_function_name, |
| 2007 | lldb::ValueObjectSP valobj, |
| 2008 | lldb::ScriptInterpreterObjectSP& callee_wrapper_sp, |
| 2009 | std::string& retval) |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2010 | { |
| 2011 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2012 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2013 | |
| 2014 | if (!valobj.get()) |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2015 | { |
| 2016 | retval.assign("<no object>"); |
| 2017 | return false; |
| 2018 | } |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2019 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2020 | void* old_callee = (callee_wrapper_sp ? callee_wrapper_sp->GetObject() : NULL); |
| 2021 | void* new_callee = old_callee; |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2022 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2023 | bool ret_val; |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2024 | if (python_function_name |
| 2025 | && *python_function_name) |
| 2026 | { |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2027 | { |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2028 | Locker py_lock(this); |
| 2029 | { |
| 2030 | Timer scoped_timer ("g_swig_typescript_callback","g_swig_typescript_callback"); |
| 2031 | ret_val = g_swig_typescript_callback (python_function_name, |
| 2032 | FindSessionDictionary(m_dictionary_name.c_str()), |
| 2033 | valobj, |
| 2034 | &new_callee, |
| 2035 | retval); |
| 2036 | } |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2037 | } |
| 2038 | } |
| 2039 | else |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2040 | { |
| 2041 | retval.assign("<no function name>"); |
| 2042 | return false; |
| 2043 | } |
| 2044 | |
| 2045 | if (new_callee && old_callee != new_callee) |
| 2046 | callee_wrapper_sp = MakeScriptObject(new_callee); |
Enrico Granata | f7a9b14 | 2011-07-15 02:26:42 +0000 | [diff] [blame] | 2047 | |
| 2048 | return ret_val; |
| 2049 | |
| 2050 | } |
| 2051 | |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 2052 | bool |
| 2053 | ScriptInterpreterPython::BreakpointCallbackFunction |
| 2054 | ( |
| 2055 | void *baton, |
| 2056 | StoppointCallbackContext *context, |
| 2057 | user_id_t break_id, |
| 2058 | user_id_t break_loc_id |
| 2059 | ) |
| 2060 | { |
| 2061 | BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2062 | const char *python_function_name = bp_option_data->script_source.c_str(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2063 | |
| 2064 | if (!context) |
| 2065 | return true; |
| 2066 | |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 2067 | ExecutionContext exe_ctx (context->exe_ctx_ref); |
| 2068 | Target *target = exe_ctx.GetTargetPtr(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (!target) |
| 2071 | return true; |
| 2072 | |
| 2073 | Debugger &debugger = target->GetDebugger(); |
| 2074 | ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); |
| 2075 | ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter; |
| 2076 | |
| 2077 | if (!script_interpreter) |
| 2078 | return true; |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 2079 | |
| 2080 | if (python_function_name != NULL |
| 2081 | && python_function_name[0] != '\0') |
| 2082 | { |
Greg Clayton | f4124de | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 2083 | const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP()); |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 2084 | BreakpointSP breakpoint_sp = target->GetBreakpointByID (break_id); |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2085 | if (breakpoint_sp) |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2086 | { |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2087 | const BreakpointLocationSP bp_loc_sp (breakpoint_sp->FindLocationByID (break_loc_id)); |
| 2088 | |
| 2089 | if (stop_frame_sp && bp_loc_sp) |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 2090 | { |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2091 | bool ret_val = true; |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2092 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2093 | Locker py_lock(python_interpreter); |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2094 | ret_val = g_swig_breakpoint_callback (python_function_name, |
| 2095 | python_interpreter->m_dictionary_name.c_str(), |
| 2096 | stop_frame_sp, |
| 2097 | bp_loc_sp); |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2098 | } |
| 2099 | return ret_val; |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 2100 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2101 | } |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 2102 | } |
| 2103 | // We currently always true so we stop in case anything goes wrong when |
| 2104 | // trying to call the script function |
| 2105 | return true; |
| 2106 | } |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2107 | |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 2108 | bool |
| 2109 | ScriptInterpreterPython::WatchpointCallbackFunction |
| 2110 | ( |
| 2111 | void *baton, |
| 2112 | StoppointCallbackContext *context, |
| 2113 | user_id_t watch_id |
| 2114 | ) |
| 2115 | { |
| 2116 | WatchpointOptions::CommandData *wp_option_data = (WatchpointOptions::CommandData *) baton; |
| 2117 | const char *python_function_name = wp_option_data->script_source.c_str(); |
| 2118 | |
| 2119 | if (!context) |
| 2120 | return true; |
| 2121 | |
| 2122 | ExecutionContext exe_ctx (context->exe_ctx_ref); |
| 2123 | Target *target = exe_ctx.GetTargetPtr(); |
| 2124 | |
| 2125 | if (!target) |
| 2126 | return true; |
| 2127 | |
| 2128 | Debugger &debugger = target->GetDebugger(); |
| 2129 | ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); |
| 2130 | ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter; |
| 2131 | |
| 2132 | if (!script_interpreter) |
| 2133 | return true; |
| 2134 | |
| 2135 | if (python_function_name != NULL |
| 2136 | && python_function_name[0] != '\0') |
| 2137 | { |
| 2138 | const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP()); |
| 2139 | WatchpointSP wp_sp = target->GetWatchpointList().FindByID (watch_id); |
| 2140 | if (wp_sp) |
| 2141 | { |
| 2142 | if (stop_frame_sp && wp_sp) |
| 2143 | { |
| 2144 | bool ret_val = true; |
| 2145 | { |
| 2146 | Locker py_lock(python_interpreter); |
| 2147 | ret_val = g_swig_watchpoint_callback (python_function_name, |
| 2148 | python_interpreter->m_dictionary_name.c_str(), |
| 2149 | stop_frame_sp, |
| 2150 | wp_sp); |
| 2151 | } |
| 2152 | return ret_val; |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | // We currently always true so we stop in case anything goes wrong when |
| 2157 | // trying to call the script function |
| 2158 | return true; |
| 2159 | } |
| 2160 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2161 | lldb::thread_result_t |
| 2162 | ScriptInterpreterPython::RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton) |
| 2163 | { |
| 2164 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
| 2165 | |
| 2166 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT)); |
| 2167 | |
| 2168 | if (log) |
| 2169 | log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread starting...", baton); |
| 2170 | |
| 2171 | char error_str[1024]; |
| 2172 | const char *pty_slave_name = script_interpreter->m_embedded_python_pty.GetSlaveName (error_str, sizeof (error_str)); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2173 | |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2174 | if (pty_slave_name != NULL) |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 2175 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2176 | StreamString run_string; |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2177 | |
| 2178 | // Ensure we have the GIL before running any Python code. |
| 2179 | // Since we're only running a few one-liners and then dropping to the interpreter (which will release the GIL when needed), |
| 2180 | // we can just release the GIL after finishing our work. |
| 2181 | // If finer-grained locking is desirable, we can lock and unlock the GIL only when calling a python function. |
| 2182 | Locker locker(script_interpreter, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 2183 | ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals, |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2184 | ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession); |
| 2185 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2186 | run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 2187 | PyRun_SimpleString (run_string.GetData()); |
| 2188 | run_string.Clear (); |
| 2189 | |
| 2190 | run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str()); |
| 2191 | PyRun_SimpleString (run_string.GetData()); |
| 2192 | run_string.Clear (); |
| 2193 | |
| 2194 | run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 2195 | PyRun_SimpleString (run_string.GetData()); |
| 2196 | run_string.Clear (); |
| 2197 | |
| 2198 | run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(), |
| 2199 | pty_slave_name); |
| 2200 | PyRun_SimpleString (run_string.GetData()); |
| 2201 | run_string.Clear (); |
| 2202 | |
Johnny Chen | 8054ba3 | 2011-03-11 00:28:50 +0000 | [diff] [blame] | 2203 | // The following call drops into the embedded interpreter loop and stays there until the |
| 2204 | // user chooses to exit from the Python interpreter. |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2205 | // This embedded interpreter will, as any Python code that performs I/O, unlock the GIL before |
| 2206 | // a system call that can hang, and lock it when the syscall has returned. |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2207 | |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2208 | // We need to surround the call to the embedded interpreter with calls to PyGILState_Ensure and |
| 2209 | // PyGILState_Release (using the Locker above). This is because Python has a global lock which must be held whenever we want |
| 2210 | // to touch any Python objects. Otherwise, if the user calls Python code, the interpreter state will be off, |
| 2211 | // and things could hang (it's happened before). |
Caroline Tice | ce207c1 | 2011-03-11 00:21:55 +0000 | [diff] [blame] | 2212 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2213 | run_string.Printf ("run_python_interpreter (%s)", script_interpreter->m_dictionary_name.c_str()); |
| 2214 | PyRun_SimpleString (run_string.GetData()); |
| 2215 | run_string.Clear (); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2216 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2217 | run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 2218 | PyRun_SimpleString (run_string.GetData()); |
| 2219 | run_string.Clear(); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2220 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2221 | run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 2222 | PyRun_SimpleString (run_string.GetData()); |
| 2223 | run_string.Clear(); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 2226 | if (script_interpreter->m_embedded_python_input_reader_sp) |
| 2227 | script_interpreter->m_embedded_python_input_reader_sp->SetIsDone (true); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2228 | |
| 2229 | script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2230 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2231 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT); |
| 2232 | if (log) |
| 2233 | log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread exiting...", baton); |
| 2234 | |
| 2235 | |
Johnny Chen | 8054ba3 | 2011-03-11 00:28:50 +0000 | [diff] [blame] | 2236 | // Clean up the input reader and make the debugger pop it off the stack. |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2237 | Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger(); |
Johnny Chen | 8121d7a | 2012-08-17 23:44:35 +0000 | [diff] [blame] | 2238 | const InputReaderSP reader_sp = script_interpreter->m_embedded_python_input_reader_sp; |
| 2239 | if (reader_sp) |
| 2240 | { |
| 2241 | debugger.PopInputReader (reader_sp); |
| 2242 | script_interpreter->m_embedded_python_input_reader_sp.reset(); |
| 2243 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2244 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2245 | return NULL; |
| 2246 | } |
| 2247 | |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 2248 | lldb::thread_result_t |
| 2249 | ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader (lldb::thread_arg_t baton) |
| 2250 | { |
| 2251 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
| 2252 | |
| 2253 | const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp; |
| 2254 | |
| 2255 | if (reader_sp) |
| 2256 | reader_sp->WaitOnReaderIsDone(); |
| 2257 | |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2261 | uint32_t |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2262 | ScriptInterpreterPython::CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor_sp) |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2263 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2264 | if (!implementor_sp) |
| 2265 | return 0; |
| 2266 | |
| 2267 | void* implementor = implementor_sp->GetObject(); |
| 2268 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2269 | if (!implementor) |
| 2270 | return 0; |
| 2271 | |
| 2272 | if (!g_swig_calc_children) |
| 2273 | return 0; |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2274 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2275 | uint32_t ret_val = 0; |
| 2276 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2277 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2278 | Locker py_lock(this); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 2279 | ret_val = g_swig_calc_children (implementor); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | return ret_val; |
| 2283 | } |
| 2284 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2285 | lldb::ValueObjectSP |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2286 | ScriptInterpreterPython::GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor_sp, uint32_t idx) |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2287 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2288 | if (!implementor_sp) |
| 2289 | return lldb::ValueObjectSP(); |
| 2290 | |
| 2291 | void* implementor = implementor_sp->GetObject(); |
| 2292 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2293 | if (!implementor) |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2294 | return lldb::ValueObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2295 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2296 | if (!g_swig_get_child_index || !g_swig_cast_to_sbvalue) |
| 2297 | return lldb::ValueObjectSP(); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2298 | |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2299 | void* child_ptr = NULL; |
| 2300 | lldb::SBValue* value_sb = NULL; |
| 2301 | lldb::ValueObjectSP ret_val; |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2302 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2303 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2304 | Locker py_lock(this); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 2305 | child_ptr = g_swig_get_child_index (implementor,idx); |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2306 | if (child_ptr != NULL && child_ptr != Py_None) |
| 2307 | { |
| 2308 | value_sb = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr); |
| 2309 | if (value_sb == NULL) |
| 2310 | Py_XDECREF(child_ptr); |
| 2311 | else |
Greg Clayton | eeabe81 | 2012-11-01 21:35:16 +0000 | [diff] [blame] | 2312 | ret_val = value_sb->GetSP(); |
Enrico Granata | 9154480 | 2011-09-06 19:20:51 +0000 | [diff] [blame] | 2313 | } |
| 2314 | else |
| 2315 | { |
| 2316 | Py_XDECREF(child_ptr); |
| 2317 | } |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | return ret_val; |
| 2321 | } |
| 2322 | |
| 2323 | int |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2324 | ScriptInterpreterPython::GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor_sp, const char* child_name) |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2325 | { |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2326 | if (!implementor_sp) |
| 2327 | return UINT32_MAX; |
| 2328 | |
| 2329 | void* implementor = implementor_sp->GetObject(); |
| 2330 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2331 | if (!implementor) |
| 2332 | return UINT32_MAX; |
| 2333 | |
| 2334 | if (!g_swig_get_index_child) |
| 2335 | return UINT32_MAX; |
| 2336 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2337 | int ret_val = UINT32_MAX; |
| 2338 | |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2339 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2340 | Locker py_lock(this); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 2341 | ret_val = g_swig_get_index_child (implementor, child_name); |
Enrico Granata | 9ae7cef | 2011-07-24 00:14:56 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
| 2344 | return ret_val; |
| 2345 | } |
| 2346 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2347 | bool |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2348 | ScriptInterpreterPython::UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp) |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2349 | { |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2350 | bool ret_val = false; |
| 2351 | |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2352 | if (!implementor_sp) |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2353 | return ret_val; |
Enrico Granata | 400105d | 2012-03-06 23:42:15 +0000 | [diff] [blame] | 2354 | |
| 2355 | void* implementor = implementor_sp->GetObject(); |
| 2356 | |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2357 | if (!implementor) |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2358 | return ret_val; |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2359 | |
| 2360 | if (!g_swig_update_provider) |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2361 | return ret_val; |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2362 | |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2363 | { |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2364 | Locker py_lock(this); |
Enrico Granata | 3d656c7 | 2012-10-22 18:18:36 +0000 | [diff] [blame] | 2365 | ret_val = g_swig_update_provider (implementor); |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Enrico Granata | cf09f88 | 2012-03-19 22:58:49 +0000 | [diff] [blame] | 2368 | return ret_val; |
Enrico Granata | 979e20d | 2011-07-29 19:53:35 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2371 | bool |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 2372 | ScriptInterpreterPython::MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp) |
| 2373 | { |
| 2374 | bool ret_val = false; |
| 2375 | |
| 2376 | if (!implementor_sp) |
| 2377 | return ret_val; |
| 2378 | |
| 2379 | void* implementor = implementor_sp->GetObject(); |
| 2380 | |
| 2381 | if (!implementor) |
| 2382 | return ret_val; |
| 2383 | |
| 2384 | if (!g_swig_mighthavechildren_provider) |
| 2385 | return ret_val; |
| 2386 | |
| 2387 | { |
| 2388 | Locker py_lock(this); |
| 2389 | ret_val = g_swig_mighthavechildren_provider (implementor); |
| 2390 | } |
| 2391 | |
| 2392 | return ret_val; |
| 2393 | } |
| 2394 | |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2395 | static std::string |
| 2396 | ReadPythonBacktrace (PyObject* py_backtrace) |
| 2397 | { |
| 2398 | PyObject* traceback_module = NULL, |
| 2399 | *stringIO_module = NULL, |
| 2400 | *stringIO_builder = NULL, |
| 2401 | *stringIO_buffer = NULL, |
| 2402 | *printTB = NULL, |
| 2403 | *printTB_args = NULL, |
| 2404 | *printTB_result = NULL, |
| 2405 | *stringIO_getvalue = NULL, |
| 2406 | *printTB_string = NULL; |
| 2407 | |
| 2408 | std::string retval("backtrace unavailable"); |
| 2409 | |
| 2410 | if (py_backtrace && py_backtrace != Py_None) |
| 2411 | { |
| 2412 | traceback_module = PyImport_ImportModule("traceback"); |
| 2413 | stringIO_module = PyImport_ImportModule("StringIO"); |
| 2414 | |
| 2415 | if (traceback_module && traceback_module != Py_None && stringIO_module && stringIO_module != Py_None) |
| 2416 | { |
| 2417 | stringIO_builder = PyObject_GetAttrString(stringIO_module, "StringIO"); |
| 2418 | if (stringIO_builder && stringIO_builder != Py_None) |
| 2419 | { |
| 2420 | stringIO_buffer = PyObject_CallObject(stringIO_builder, NULL); |
| 2421 | if (stringIO_buffer && stringIO_buffer != Py_None) |
| 2422 | { |
| 2423 | printTB = PyObject_GetAttrString(traceback_module, "print_tb"); |
| 2424 | if (printTB && printTB != Py_None) |
| 2425 | { |
| 2426 | printTB_args = Py_BuildValue("OOO",py_backtrace,Py_None,stringIO_buffer); |
| 2427 | printTB_result = PyObject_CallObject(printTB, printTB_args); |
| 2428 | stringIO_getvalue = PyObject_GetAttrString(stringIO_buffer, "getvalue"); |
| 2429 | if (stringIO_getvalue && stringIO_getvalue != Py_None) |
| 2430 | { |
| 2431 | printTB_string = PyObject_CallObject (stringIO_getvalue,NULL); |
| 2432 | if (printTB_string && printTB_string != Py_None && PyString_Check(printTB_string)) |
| 2433 | retval.assign(PyString_AsString(printTB_string)); |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | } |
| 2438 | } |
| 2439 | } |
| 2440 | Py_XDECREF(traceback_module); |
| 2441 | Py_XDECREF(stringIO_module); |
| 2442 | Py_XDECREF(stringIO_builder); |
| 2443 | Py_XDECREF(stringIO_buffer); |
| 2444 | Py_XDECREF(printTB); |
| 2445 | Py_XDECREF(printTB_args); |
| 2446 | Py_XDECREF(printTB_result); |
| 2447 | Py_XDECREF(stringIO_getvalue); |
| 2448 | Py_XDECREF(printTB_string); |
| 2449 | return retval; |
| 2450 | } |
| 2451 | |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 2452 | bool |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2453 | ScriptInterpreterPython::LoadScriptingModule (const char* pathname, |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2454 | bool can_reload, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 2455 | bool init_session, |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2456 | lldb_private::Error& error) |
| 2457 | { |
| 2458 | if (!pathname || !pathname[0]) |
| 2459 | { |
| 2460 | error.SetErrorString("invalid pathname"); |
| 2461 | return false; |
| 2462 | } |
| 2463 | |
| 2464 | if (!g_swig_call_module_init) |
| 2465 | { |
| 2466 | error.SetErrorString("internal helper function missing"); |
| 2467 | return false; |
| 2468 | } |
| 2469 | |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2470 | lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this(); |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2471 | |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2472 | { |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2473 | FileSpec target_file(pathname, true); |
| 2474 | |
| 2475 | // TODO: would we want to reject any other value? |
| 2476 | if (target_file.GetFileType() == FileSpec::eFileTypeInvalid || |
| 2477 | target_file.GetFileType() == FileSpec::eFileTypeUnknown) |
| 2478 | { |
| 2479 | error.SetErrorString("invalid pathname"); |
| 2480 | return false; |
| 2481 | } |
| 2482 | |
| 2483 | const char* directory = target_file.GetDirectory().GetCString(); |
| 2484 | std::string basename(target_file.GetFilename().GetCString()); |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2485 | |
| 2486 | // Before executing Pyton code, lock the GIL. |
Greg Clayton | 2e7f313 | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 2487 | Locker py_lock (this, |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 2488 | Locker::AcquireLock | (init_session ? Locker::InitSession : 0), |
| 2489 | Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0)); |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2490 | |
| 2491 | // now make sure that Python has "directory" in the search path |
| 2492 | StreamString command_stream; |
Enrico Granata | 6f58bbd | 2012-11-08 02:44:10 +0000 | [diff] [blame] | 2493 | command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n", |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2494 | directory, |
| 2495 | directory); |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2496 | bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)); |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2497 | if (!syspath_retval) |
| 2498 | { |
| 2499 | error.SetErrorString("Python sys.path handling failed"); |
| 2500 | return false; |
| 2501 | } |
| 2502 | |
| 2503 | // strip .py or .pyc extension |
| 2504 | ConstString extension = target_file.GetFileNameExtension(); |
Sean Callanan | 3e1d95d | 2012-12-03 21:28:37 +0000 | [diff] [blame] | 2505 | if (extension) |
| 2506 | { |
| 2507 | if (::strcmp(extension.GetCString(), "py") == 0) |
| 2508 | basename.resize(basename.length()-3); |
| 2509 | else if(::strcmp(extension.GetCString(), "pyc") == 0) |
| 2510 | basename.resize(basename.length()-4); |
| 2511 | } |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2512 | |
| 2513 | // check if the module is already import-ed |
| 2514 | command_stream.Clear(); |
| 2515 | command_stream.Printf("sys.getrefcount(%s)",basename.c_str()); |
| 2516 | int refcount = 0; |
| 2517 | // this call will fail if the module does not exist (because the parameter to it is not a string |
| 2518 | // but an actual Python module object, which is non-existant if the module was not imported before) |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2519 | bool was_imported = (ExecuteOneLineWithReturn(command_stream.GetData(), |
Greg Clayton | 2e7f313 | 2012-10-18 22:40:37 +0000 | [diff] [blame] | 2520 | ScriptInterpreterPython::eScriptReturnTypeInt, |
| 2521 | &refcount, |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2522 | ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && refcount > 0); |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2523 | if (was_imported == true && can_reload == false) |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2524 | { |
| 2525 | error.SetErrorString("module already imported"); |
| 2526 | return false; |
| 2527 | } |
| 2528 | |
| 2529 | // now actually do the import |
| 2530 | command_stream.Clear(); |
| 2531 | command_stream.Printf("import %s",basename.c_str()); |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2532 | bool import_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false).SetMaskoutErrors(false)); |
| 2533 | PyObject* py_error = PyErr_Occurred(); // per Python docs: "you do not need to Py_DECREF()" the return of this function |
| 2534 | |
| 2535 | if (py_error || !import_retval) // check for failure of the import |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2536 | { |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2537 | if (py_error) // if we have a Python error.. |
| 2538 | { |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2539 | PyObject *type = NULL,*value = NULL,*traceback = NULL; |
| 2540 | PyErr_Fetch (&type,&value,&traceback); |
| 2541 | |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2542 | if (PyErr_GivenExceptionMatches (py_error, PyExc_ImportError)) // and it is an ImportError |
| 2543 | { |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2544 | if (value && value != Py_None) |
| 2545 | error.SetErrorString(PyString_AsString(PyObject_Str(value))); |
| 2546 | else |
| 2547 | error.SetErrorString("ImportError raised by imported module"); |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2548 | } |
| 2549 | else // any other error |
| 2550 | { |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2551 | // get the backtrace |
| 2552 | std::string bt = ReadPythonBacktrace(traceback); |
Enrico Granata | 6cc99d2 | 2012-11-13 02:57:43 +0000 | [diff] [blame] | 2553 | |
| 2554 | if (value && value != Py_None) |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2555 | error.SetErrorStringWithFormat("Python error raised while importing module: %s - traceback: %s", PyString_AsString(PyObject_Str(value)),bt.c_str()); |
Enrico Granata | 6cc99d2 | 2012-11-13 02:57:43 +0000 | [diff] [blame] | 2556 | else |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2557 | error.SetErrorStringWithFormat("Python raised an error while importing module - traceback: %s",bt.c_str()); |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2558 | } |
Enrico Granata | 9ac7ce3 | 2012-11-30 20:15:16 +0000 | [diff] [blame] | 2559 | |
| 2560 | Py_XDECREF(type); |
| 2561 | Py_XDECREF(value); |
| 2562 | Py_XDECREF(traceback); |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2563 | } |
| 2564 | else // we failed but have no error to explain why |
| 2565 | { |
| 2566 | error.SetErrorString("unknown error while importing module"); |
| 2567 | } |
| 2568 | |
| 2569 | // anyway, clear the error indicator and return false |
| 2570 | PyErr_Clear(); |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2571 | return false; |
| 2572 | } |
| 2573 | |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2574 | // if we are here, everything worked |
Enrico Granata | 16376ed | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 2575 | // call __lldb_init_module(debugger,dict) |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2576 | if (!g_swig_call_module_init (basename, |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2577 | m_dictionary_name.c_str(), |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2578 | debugger_sp)) |
| 2579 | { |
Enrico Granata | 16376ed | 2012-02-15 02:34:21 +0000 | [diff] [blame] | 2580 | error.SetErrorString("calling __lldb_init_module failed"); |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2581 | return false; |
| 2582 | } |
| 2583 | return true; |
| 2584 | } |
| 2585 | } |
| 2586 | |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2587 | lldb::ScriptInterpreterObjectSP |
| 2588 | ScriptInterpreterPython::MakeScriptObject (void* object) |
| 2589 | { |
| 2590 | return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterPythonObject(object)); |
| 2591 | } |
| 2592 | |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2593 | ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp, |
| 2594 | ScriptedCommandSynchronicity synchro) : |
| 2595 | m_debugger_sp(debugger_sp), |
| 2596 | m_synch_wanted(synchro), |
| 2597 | m_old_asynch(debugger_sp->GetAsyncExecution()) |
| 2598 | { |
| 2599 | if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous) |
| 2600 | m_debugger_sp->SetAsyncExecution(false); |
| 2601 | else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous) |
| 2602 | m_debugger_sp->SetAsyncExecution(true); |
| 2603 | } |
| 2604 | |
| 2605 | ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler() |
| 2606 | { |
| 2607 | if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue) |
| 2608 | m_debugger_sp->SetAsyncExecution(m_old_asynch); |
| 2609 | } |
| 2610 | |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2611 | bool |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2612 | ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function, |
| 2613 | const char* args, |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2614 | ScriptedCommandSynchronicity synchronicity, |
Enrico Granata | 6b1596d | 2011-08-16 23:24:13 +0000 | [diff] [blame] | 2615 | lldb_private::CommandReturnObject& cmd_retobj, |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2616 | Error& error) |
| 2617 | { |
| 2618 | if (!impl_function) |
| 2619 | { |
| 2620 | error.SetErrorString("no function to execute"); |
| 2621 | return false; |
| 2622 | } |
| 2623 | |
| 2624 | if (!g_swig_call_command) |
| 2625 | { |
| 2626 | error.SetErrorString("no helper function to run scripted commands"); |
| 2627 | return false; |
| 2628 | } |
| 2629 | |
Greg Clayton | 13d24fb | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 2630 | lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this(); |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2631 | |
| 2632 | if (!debugger_sp.get()) |
| 2633 | { |
| 2634 | error.SetErrorString("invalid Debugger pointer"); |
| 2635 | return false; |
| 2636 | } |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2637 | |
| 2638 | bool ret_val; |
| 2639 | |
| 2640 | std::string err_msg; |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2641 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2642 | { |
Jim Ingham | 14e71ec | 2012-12-07 17:43:38 +0000 | [diff] [blame] | 2643 | Locker py_lock(this, |
| 2644 | Locker::AcquireLock | Locker::InitSession | Locker::InitGlobals, |
| 2645 | Locker::FreeLock | Locker::TearDownSession); |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2646 | SynchronicityHandler synch_handler(debugger_sp, |
| 2647 | synchronicity); |
| 2648 | |
Enrico Granata | a1ba314 | 2012-06-07 00:17:18 +0000 | [diff] [blame] | 2649 | PythonInputReaderManager py_input(this); |
| 2650 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2651 | ret_val = g_swig_call_command (impl_function, |
Enrico Granata | fa1f617 | 2011-10-24 17:22:21 +0000 | [diff] [blame] | 2652 | m_dictionary_name.c_str(), |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2653 | debugger_sp, |
| 2654 | args, |
| 2655 | err_msg, |
Enrico Granata | 3370f0c | 2011-08-19 23:56:34 +0000 | [diff] [blame] | 2656 | cmd_retobj); |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2657 | } |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2658 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2659 | if (!ret_val) |
| 2660 | error.SetErrorString(err_msg.c_str()); |
| 2661 | else |
| 2662 | error.Clear(); |
Enrico Granata | 6010ace | 2011-11-07 22:57:04 +0000 | [diff] [blame] | 2663 | |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2664 | return ret_val; |
Enrico Granata | c2a2825 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2667 | // in Python, a special attribute __doc__ contains the docstring |
| 2668 | // for an object (function, method, class, ...) if any is defined |
| 2669 | // Otherwise, the attribute's value is None |
Enrico Granata | 5b9f777 | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 2670 | bool |
| 2671 | ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& dest) |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2672 | { |
Enrico Granata | 5b9f777 | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 2673 | dest.clear(); |
| 2674 | if (!item || !*item) |
| 2675 | return false; |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2676 | std::string command(item); |
| 2677 | command += ".__doc__"; |
| 2678 | |
| 2679 | char* result_ptr = NULL; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully |
| 2680 | |
| 2681 | if (ExecuteOneLineWithReturn (command.c_str(), |
Enrico Granata | 59df36f | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 2682 | ScriptInterpreter::eScriptReturnTypeCharStrOrNone, |
Enrico Granata | fd670c2 | 2012-10-31 00:01:26 +0000 | [diff] [blame] | 2683 | &result_ptr, ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false) /*.SetSetLLDBGlobals(false)*/)) |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2684 | { |
Enrico Granata | b2c0a12 | 2012-09-20 23:23:55 +0000 | [diff] [blame] | 2685 | if (result_ptr) |
| 2686 | dest.assign(result_ptr); |
Enrico Granata | 5b9f777 | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 2687 | return true; |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2688 | } |
| 2689 | else |
Enrico Granata | 5b9f777 | 2012-09-18 21:53:02 +0000 | [diff] [blame] | 2690 | { |
| 2691 | StreamString str_stream; |
| 2692 | str_stream.Printf("Function %s was not found. Containing module might be missing.",item); |
| 2693 | dest.assign(str_stream.GetData()); |
| 2694 | return false; |
| 2695 | } |
Enrico Granata | e5e34cb | 2011-08-17 01:30:04 +0000 | [diff] [blame] | 2696 | } |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 2697 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2698 | void |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2699 | ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_init_callback) |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2700 | { |
| 2701 | g_swig_init_callback = python_swig_init_callback; |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2702 | g_swig_breakpoint_callback = LLDBSwigPythonBreakpointCallbackFunction; |
Johnny Chen | f3ec461 | 2012-08-09 23:09:42 +0000 | [diff] [blame] | 2703 | g_swig_watchpoint_callback = LLDBSwigPythonWatchpointCallbackFunction; |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2704 | g_swig_typescript_callback = LLDBSwigPythonCallTypeScript; |
| 2705 | g_swig_synthetic_script = LLDBSwigPythonCreateSyntheticProvider; |
| 2706 | g_swig_calc_children = LLDBSwigPython_CalculateNumChildren; |
| 2707 | g_swig_get_child_index = LLDBSwigPython_GetChildAtIndex; |
| 2708 | g_swig_get_index_child = LLDBSwigPython_GetIndexOfChildWithName; |
| 2709 | g_swig_cast_to_sbvalue = LLDBSWIGPython_CastPyObjectToSBValue; |
| 2710 | g_swig_update_provider = LLDBSwigPython_UpdateSynthProviderInstance; |
Enrico Granata | 800332c | 2012-10-23 19:54:09 +0000 | [diff] [blame] | 2711 | g_swig_mighthavechildren_provider = LLDBSwigPython_MightHaveChildrenSynthProviderInstance; |
Enrico Granata | 1328b14 | 2012-02-29 03:28:49 +0000 | [diff] [blame] | 2712 | g_swig_call_command = LLDBSwigPythonCallCommand; |
| 2713 | g_swig_call_module_init = LLDBSwigPythonCallModuleInit; |
Enrico Granata | 155ee91 | 2012-08-24 00:30:47 +0000 | [diff] [blame] | 2714 | g_swig_create_os_plugin = LLDBSWIGPythonCreateOSPlugin; |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | void |
| 2718 | ScriptInterpreterPython::InitializePrivate () |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2719 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2720 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 2721 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 2722 | // Python will muck with STDIN terminal state, so save off any current TTY |
| 2723 | // settings so we can restore them. |
| 2724 | TerminalState stdin_tty_state; |
| 2725 | stdin_tty_state.Save(STDIN_FILENO, false); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2726 | |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2727 | PyGILState_STATE gstate; |
| 2728 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE)); |
| 2729 | bool threads_already_initialized = false; |
| 2730 | if (PyEval_ThreadsInitialized ()) { |
| 2731 | gstate = PyGILState_Ensure (); |
| 2732 | if (log) |
| 2733 | log->Printf("Ensured PyGILState. Previous state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : ""); |
| 2734 | threads_already_initialized = true; |
| 2735 | } else { |
| 2736 | // InitThreads acquires the GIL if it hasn't been called before. |
| 2737 | PyEval_InitThreads (); |
| 2738 | } |
Caroline Tice | a54461d | 2011-06-02 22:09:43 +0000 | [diff] [blame] | 2739 | Py_InitializeEx (0); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2740 | |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2741 | // Initialize SWIG after setting up python |
| 2742 | assert (g_swig_init_callback != NULL); |
| 2743 | g_swig_init_callback (); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2744 | |
| 2745 | // Update the path python uses to search for modules to include the current directory. |
| 2746 | |
Caroline Tice | d4d9283 | 2011-06-13 21:33:00 +0000 | [diff] [blame] | 2747 | PyRun_SimpleString ("import sys"); |
| 2748 | PyRun_SimpleString ("sys.path.append ('.')"); |
Jim Ingham | 2a19ef9 | 2011-08-27 01:24:08 +0000 | [diff] [blame] | 2749 | |
| 2750 | // Find the module that owns this code and use that path we get to |
| 2751 | // set the sys.path appropriately. |
| 2752 | |
| 2753 | FileSpec file_spec; |
| 2754 | char python_dir_path[PATH_MAX]; |
| 2755 | if (Host::GetLLDBPath (ePathTypePythonDir, file_spec)) |
| 2756 | { |
| 2757 | std::string python_path("sys.path.insert(0,\""); |
| 2758 | size_t orig_len = python_path.length(); |
| 2759 | if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path))) |
| 2760 | { |
| 2761 | python_path.append (python_dir_path); |
| 2762 | python_path.append ("\")"); |
| 2763 | PyRun_SimpleString (python_path.c_str()); |
| 2764 | python_path.resize (orig_len); |
| 2765 | } |
| 2766 | |
| 2767 | if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, file_spec)) |
| 2768 | { |
| 2769 | if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path))) |
| 2770 | { |
| 2771 | python_path.append (python_dir_path); |
| 2772 | python_path.append ("\")"); |
| 2773 | PyRun_SimpleString (python_path.c_str()); |
| 2774 | python_path.resize (orig_len); |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | |
Greg Clayton | 4e651b1 | 2012-04-25 00:58:03 +0000 | [diff] [blame] | 2779 | 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 *"); |
Greg Clayton | 9920858 | 2011-02-07 19:04:58 +0000 | [diff] [blame] | 2780 | |
Johnny Chen | 8165d43 | 2012-08-18 04:14:54 +0000 | [diff] [blame] | 2781 | if (threads_already_initialized) { |
| 2782 | if (log) |
| 2783 | log->Printf("Releasing PyGILState. Returning to state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : ""); |
| 2784 | PyGILState_Release (gstate); |
| 2785 | } else { |
| 2786 | // We initialized the threads in this function, just unlock the GIL. |
| 2787 | PyEval_SaveThread(); |
| 2788 | } |
| 2789 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 2790 | stdin_tty_state.Restore(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Greg Clayton | e86cbb9 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 2793 | //void |
| 2794 | //ScriptInterpreterPython::Terminate () |
| 2795 | //{ |
| 2796 | // // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it). Calling |
| 2797 | // // Py_Finalize here causes test suite runs to seg fault: The test suite runs in Python. It registers |
| 2798 | // // SBDebugger::Terminate to be called 'at_exit'. When the test suite Python harness finishes up, it calls |
| 2799 | // // Py_Finalize, which calls all the 'at_exit' registered functions. SBDebugger::Terminate calls Debugger::Terminate, |
| 2800 | // // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls |
| 2801 | // // ScriptInterpreterPython::Terminate. So if we call Py_Finalize here, we end up with Py_Finalize being called from |
| 2802 | // // within Py_Finalize, which results in a seg fault. |
| 2803 | // // |
| 2804 | // // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't |
| 2805 | // // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the |
| 2806 | // // process exits). |
| 2807 | // // |
| 2808 | //// Py_Finalize (); |
| 2809 | //} |
Greg Clayton | 3e4238d | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 2810 | |
| 2811 | #endif // #ifdef LLDB_DISABLE_PYTHON |