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