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