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