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