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