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