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 | |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 21 | #include "lldb/API/SBFrame.h" |
| 22 | #include "lldb/API/SBBreakpointLocation.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 | |
Greg Clayton | 121f331 | 2010-07-07 18:40:03 +0000 | [diff] [blame] | 31 | // This function is in the C++ output file generated by SWIG after it is |
| 32 | // run on all of the headers in "lldb/API/SB*.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | extern "C" void init_lldb (void); |
| 34 | |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 35 | extern "C" bool |
| 36 | LLDBSWIGPythonBreakpointCallbackFunction |
| 37 | ( |
| 38 | const char *python_function_name, |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 39 | const char *session_dictionary_name, |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 40 | lldb::SBFrame& sb_frame, |
| 41 | lldb::SBBreakpointLocation& sb_bp_loc |
| 42 | ); |
| 43 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | using namespace lldb; |
| 45 | using namespace lldb_private; |
| 46 | |
| 47 | const char embedded_interpreter_string[] = |
| 48 | "import readline\n\ |
| 49 | import code\n\ |
| 50 | import sys\n\ |
| 51 | import traceback\n\ |
| 52 | \n\ |
| 53 | class SimpleREPL(code.InteractiveConsole):\n\ |
| 54 | def __init__(self, prompt, dict):\n\ |
| 55 | code.InteractiveConsole.__init__(self,dict)\n\ |
| 56 | self.prompt = prompt\n\ |
| 57 | self.loop_exit = False\n\ |
| 58 | self.dict = dict\n\ |
| 59 | \n\ |
| 60 | def interact(self):\n\ |
| 61 | try:\n\ |
| 62 | sys.ps1\n\ |
| 63 | except AttributeError:\n\ |
| 64 | sys.ps1 = \">>> \"\n\ |
| 65 | try:\n\ |
| 66 | sys.ps2\n\ |
| 67 | except AttributeError:\n\ |
| 68 | sys.ps2 = \"... \"\n\ |
| 69 | \n\ |
| 70 | while not self.loop_exit:\n\ |
| 71 | try:\n\ |
| 72 | self.read_py_command()\n\ |
| 73 | except (SystemExit, EOFError):\n\ |
| 74 | # EOF while in Python just breaks out to top level.\n\ |
| 75 | self.write('\\n')\n\ |
| 76 | self.loop_exit = True\n\ |
| 77 | break\n\ |
| 78 | except KeyboardInterrupt:\n\ |
| 79 | self.write(\"\\nKeyboardInterrupt\\n\")\n\ |
| 80 | self.resetbuffer()\n\ |
| 81 | more = 0\n\ |
| 82 | except:\n\ |
| 83 | traceback.print_exc()\n\ |
| 84 | \n\ |
| 85 | def process_input (self, in_str):\n\ |
| 86 | # Canonicalize the format of the input string\n\ |
| 87 | temp_str = in_str\n\ |
| 88 | temp_str.strip(' \t')\n\ |
| 89 | words = temp_str.split()\n\ |
| 90 | temp_str = ('').join(words)\n\ |
| 91 | \n\ |
| 92 | # Check the input string to see if it was the quit\n\ |
| 93 | # command. If so, intercept it, so that it doesn't\n\ |
| 94 | # close stdin on us!\n\ |
Jason Molenda | a8a5e56 | 2010-06-09 21:56:00 +0000 | [diff] [blame] | 95 | if (temp_str.lower() == \"quit()\" or temp_str.lower() == \"exit()\"):\n\ |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | self.loop_exit = True\n\ |
| 97 | in_str = \"raise SystemExit \"\n\ |
| 98 | return in_str\n\ |
| 99 | \n\ |
| 100 | def my_raw_input (self, prompt):\n\ |
| 101 | stream = sys.stdout\n\ |
| 102 | stream.write (prompt)\n\ |
| 103 | stream.flush ()\n\ |
| 104 | try:\n\ |
| 105 | line = sys.stdin.readline()\n\ |
| 106 | except KeyboardInterrupt:\n\ |
| 107 | line = \" \\n\"\n\ |
| 108 | except (SystemExit, EOFError):\n\ |
| 109 | line = \"quit()\\n\"\n\ |
| 110 | if not line:\n\ |
| 111 | raise EOFError\n\ |
| 112 | if line[-1] == '\\n':\n\ |
| 113 | line = line[:-1]\n\ |
| 114 | return line\n\ |
| 115 | \n\ |
| 116 | def read_py_command(self):\n\ |
| 117 | # Read off a complete Python command.\n\ |
| 118 | more = 0\n\ |
| 119 | while 1:\n\ |
| 120 | if more:\n\ |
| 121 | prompt = sys.ps2\n\ |
| 122 | else:\n\ |
| 123 | prompt = sys.ps1\n\ |
| 124 | line = self.my_raw_input(prompt)\n\ |
| 125 | # Can be None if sys.stdin was redefined\n\ |
| 126 | encoding = getattr(sys.stdin, \"encoding\", None)\n\ |
| 127 | if encoding and not isinstance(line, unicode):\n\ |
| 128 | line = line.decode(encoding)\n\ |
| 129 | line = self.process_input (line)\n\ |
| 130 | more = self.push(line)\n\ |
| 131 | if not more:\n\ |
| 132 | break\n\ |
| 133 | \n\ |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 134 | def one_line (self, input):\n\ |
| 135 | line = self.process_input (input)\n\ |
| 136 | more = self.push(line)\n\ |
| 137 | if more:\n\ |
| 138 | self.write (\"Input not a complete line.\")\n\ |
| 139 | self.resetbuffer()\n\ |
| 140 | more = 0\n\ |
| 141 | \n\ |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | def run_python_interpreter (dict):\n\ |
| 143 | # Pass in the dictionary, for continuity from one session to the next.\n\ |
| 144 | repl = SimpleREPL('>>> ', dict)\n\ |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 145 | repl.interact()\n\ |
| 146 | \n\ |
| 147 | def run_one_line (dict, input_string):\n\ |
| 148 | repl = SimpleREPL ('', dict)\n\ |
| 149 | repl.one_line (input_string)\n"; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | |
| 151 | static int |
| 152 | _check_and_flush (FILE *stream) |
| 153 | { |
| 154 | int prev_fail = ferror (stream); |
| 155 | return fflush (stream) || prev_fail ? EOF : 0; |
| 156 | } |
| 157 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 158 | static Predicate<lldb::tid_t> & |
| 159 | PythonMutexPredicate () |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 160 | { |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 161 | static lldb_private::Predicate<lldb::tid_t> g_interpreter_is_running (LLDB_INVALID_THREAD_ID); |
| 162 | return g_interpreter_is_running; |
| 163 | } |
| 164 | |
| 165 | static bool |
| 166 | CurrentThreadHasPythonLock () |
| 167 | { |
| 168 | TimeValue timeout; |
| 169 | |
| 170 | timeout = TimeValue::Now(); // Don't wait any time. |
| 171 | |
| 172 | return PythonMutexPredicate().WaitForValueEqualTo (Host::GetCurrentThreadID(), &timeout, NULL); |
| 173 | } |
| 174 | |
| 175 | static bool |
| 176 | GetPythonLock (uint32_t seconds_to_wait) |
| 177 | { |
| 178 | |
| 179 | TimeValue timeout; |
| 180 | |
| 181 | if (seconds_to_wait != UINT32_MAX) |
| 182 | { |
| 183 | timeout = TimeValue::Now(); |
| 184 | timeout.OffsetWithSeconds (seconds_to_wait); |
| 185 | } |
| 186 | |
| 187 | return PythonMutexPredicate().WaitForValueEqualToAndSetValueTo (LLDB_INVALID_THREAD_ID, |
| 188 | Host::GetCurrentThreadID(), &timeout, NULL); |
| 189 | } |
| 190 | |
| 191 | static void |
| 192 | ReleasePythonLock () |
| 193 | { |
| 194 | PythonMutexPredicate().SetValue (LLDB_INVALID_THREAD_ID, eBroadcastAlways); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 197 | ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) : |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 198 | ScriptInterpreter (interpreter, eScriptLanguagePython), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 199 | m_embedded_python_pty (), |
| 200 | m_embedded_thread_input_reader_sp (), |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 201 | m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 202 | m_new_sysout (NULL), |
| 203 | m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()), |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 204 | m_terminal_state (), |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 205 | m_session_is_active (false), |
| 206 | m_pty_slave_is_open (false), |
| 207 | m_valid_session (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | { |
| 209 | |
Greg Clayton | 7c330d6 | 2011-01-27 01:01:10 +0000 | [diff] [blame] | 210 | static int g_initialized = false; |
| 211 | |
| 212 | if (!g_initialized) |
| 213 | { |
| 214 | g_initialized = true; |
| 215 | ScriptInterpreterPython::Initialize (); |
| 216 | } |
| 217 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 218 | bool safe_to_run = false; |
| 219 | bool need_to_release_lock = true; |
| 220 | int interval = 5; // Number of seconds to try getting the Python lock before timing out. |
| 221 | |
| 222 | // We don't dare exit this function without finishing setting up the script interpreter, so we must wait until |
| 223 | // we can get the Python lock. |
| 224 | |
| 225 | if (CurrentThreadHasPythonLock()) |
| 226 | { |
| 227 | safe_to_run = true; |
| 228 | need_to_release_lock = false; |
| 229 | } |
| 230 | |
| 231 | while (!safe_to_run) |
| 232 | { |
| 233 | safe_to_run = GetPythonLock (interval); |
| 234 | if (!safe_to_run) |
| 235 | { |
| 236 | FILE *tmp_fh = (m_dbg_stdout ? m_dbg_stdout : stdout); |
| 237 | fprintf (tmp_fh, |
| 238 | "Python interpreter is locked on another thread; " |
| 239 | "please release interpreter in order to continue.\n"); |
| 240 | interval = interval * 2; |
| 241 | } |
| 242 | } |
| 243 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 244 | m_dictionary_name.append("_dict"); |
| 245 | StreamString run_string; |
| 246 | run_string.Printf ("%s = dict()", m_dictionary_name.c_str()); |
| 247 | PyRun_SimpleString (run_string.GetData()); |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 248 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 249 | run_string.Clear(); |
| 250 | run_string.Printf ("run_one_line (%s, 'import sys')", m_dictionary_name.c_str()); |
| 251 | PyRun_SimpleString (run_string.GetData()); |
| 252 | |
| 253 | // Importing 'lldb' module calls SBDebugger::Initialize, which calls Debugger::Initialize, which increments a |
| 254 | // global debugger ref-count; therefore we need to check the ref-count before and after importing lldb, and if the |
| 255 | // ref-count increased we need to call Debugger::Terminate here to decrement the ref-count so that when the final |
| 256 | // call to Debugger::Terminate is made, the ref-count has the correct value. |
| 257 | // |
| 258 | // Bonus question: Why doesn't the ref-count always increase? Because sometimes lldb has already been imported, in |
| 259 | // 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] | 260 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 261 | int old_count = Debugger::TestDebuggerRefCount(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 263 | run_string.Clear(); |
| 264 | run_string.Printf ("run_one_line (%s, 'import lldb')", m_dictionary_name.c_str()); |
| 265 | PyRun_SimpleString (run_string.GetData()); |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 266 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 267 | int new_count = Debugger::TestDebuggerRefCount(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 268 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 269 | if (new_count > old_count) |
| 270 | Debugger::Terminate(); |
Caroline Tice | 5867f6b | 2010-10-18 18:24:17 +0000 | [diff] [blame] | 271 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 272 | run_string.Clear(); |
| 273 | run_string.Printf ("run_one_line (%s, 'import copy')", m_dictionary_name.c_str()); |
| 274 | PyRun_SimpleString (run_string.GetData()); |
| 275 | |
| 276 | run_string.Clear(); |
| 277 | run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %d')", m_dictionary_name.c_str(), |
| 278 | interpreter.GetDebugger().GetID()); |
| 279 | PyRun_SimpleString (run_string.GetData()); |
| 280 | |
| 281 | if (m_dbg_stdout != NULL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 282 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 283 | 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] | 284 | } |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 285 | |
| 286 | if (need_to_release_lock) |
| 287 | ReleasePythonLock(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | ScriptInterpreterPython::~ScriptInterpreterPython () |
| 291 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 292 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 293 | |
| 294 | if (m_embedded_thread_input_reader_sp.get() != NULL) |
| 295 | { |
| 296 | m_embedded_thread_input_reader_sp->SetIsDone (true); |
| 297 | m_embedded_python_pty.CloseSlaveFileDescriptor(); |
| 298 | m_pty_slave_is_open = false; |
| 299 | const InputReaderSP reader_sp = m_embedded_thread_input_reader_sp; |
| 300 | m_embedded_thread_input_reader_sp.reset(); |
| 301 | debugger.PopInputReader (reader_sp); |
| 302 | } |
| 303 | |
| 304 | if (m_new_sysout) |
| 305 | { |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 306 | FILE *tmp_fh = (m_dbg_stdout ? m_dbg_stdout : stdout); |
| 307 | if (!CurrentThreadHasPythonLock ()) |
| 308 | { |
| 309 | while (!GetPythonLock (1)) |
| 310 | fprintf (tmp_fh, "Python interpreter locked on another thread; waiting to acquire lock...\n"); |
| 311 | Py_DECREF (m_new_sysout); |
| 312 | ReleasePythonLock (); |
| 313 | } |
| 314 | else |
| 315 | Py_DECREF (m_new_sysout); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 316 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 319 | void |
| 320 | ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh) |
| 321 | { |
| 322 | if (fh == NULL) |
| 323 | return; |
| 324 | |
| 325 | m_dbg_stdout = fh; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 326 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 327 | FILE *tmp_fh = (m_dbg_stdout ? m_dbg_stdout : stdout); |
| 328 | if (!CurrentThreadHasPythonLock ()) |
| 329 | { |
| 330 | while (!GetPythonLock (1)) |
| 331 | fprintf (tmp_fh, "Python interpreter locked on another thread; waiting to acquire lock...\n"); |
| 332 | EnterSession (); |
| 333 | m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush); |
| 334 | LeaveSession (); |
| 335 | ReleasePythonLock (); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | EnterSession (); |
| 340 | m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush); |
| 341 | LeaveSession (); |
| 342 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 346 | ScriptInterpreterPython::SaveTerminalState (int fd) |
| 347 | { |
| 348 | // Python mucks with the terminal state of STDIN. If we can possibly avoid |
| 349 | // this by setting the file handles up correctly prior to entering the |
| 350 | // interpreter we should. For now we save and restore the terminal state |
| 351 | // on the input file handle. |
| 352 | m_terminal_state.Save (fd, false); |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | ScriptInterpreterPython::RestoreTerminalState () |
| 357 | { |
| 358 | // Python mucks with the terminal state of STDIN. If we can possibly avoid |
| 359 | // this by setting the file handles up correctly prior to entering the |
| 360 | // interpreter we should. For now we save and restore the terminal state |
| 361 | // on the input file handle. |
| 362 | m_terminal_state.Restore(); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
| 367 | void |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 368 | ScriptInterpreterPython::LeaveSession () |
| 369 | { |
| 370 | m_session_is_active = false; |
| 371 | } |
| 372 | |
| 373 | void |
| 374 | ScriptInterpreterPython::EnterSession () |
| 375 | { |
| 376 | // If we have already entered the session, without having officially 'left' it, then there is no need to |
| 377 | // 'enter' it again. |
| 378 | |
| 379 | if (m_session_is_active) |
| 380 | return; |
| 381 | |
| 382 | m_session_is_active = true; |
| 383 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 384 | StreamString run_string; |
| 385 | |
| 386 | run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %d')", m_dictionary_name.c_str(), |
| 387 | GetCommandInterpreter().GetDebugger().GetID()); |
| 388 | PyRun_SimpleString (run_string.GetData()); |
| 389 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 390 | |
| 391 | PyObject *sysmod = PyImport_AddModule ("sys"); |
| 392 | PyObject *sysdict = PyModule_GetDict (sysmod); |
| 393 | |
| 394 | if ((m_new_sysout != NULL) |
| 395 | && (sysmod != NULL) |
| 396 | && (sysdict != NULL)) |
| 397 | PyDict_SetItemString (sysdict, "stdout", m_new_sysout); |
| 398 | |
| 399 | if (PyErr_Occurred()) |
| 400 | PyErr_Clear (); |
| 401 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 402 | if (!m_pty_slave_is_open) |
| 403 | { |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 404 | run_string.Clear(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 405 | run_string.Printf ("run_one_line (%s, \"new_stdin = open('%s', 'r')\")", m_dictionary_name.c_str(), |
| 406 | m_pty_slave_name.c_str()); |
| 407 | PyRun_SimpleString (run_string.GetData()); |
| 408 | m_pty_slave_is_open = true; |
| 409 | |
| 410 | run_string.Clear(); |
| 411 | run_string.Printf ("run_one_line (%s, 'sys.stdin = new_stdin')", m_dictionary_name.c_str()); |
| 412 | PyRun_SimpleString (run_string.GetData()); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 417 | bool |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 418 | ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 419 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 420 | if (!m_valid_session) |
| 421 | return false; |
| 422 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 423 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 424 | |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 425 | // We want to call run_one_line, passing in the dictionary and the command string. We cannot do this through |
| 426 | // PyRun_SimpleString here because the command string may contain escaped characters, and putting it inside |
| 427 | // another string to pass to PyRun_SimpleString messes up the escaping. So we use the following more complicated |
| 428 | // method to pass the command string directly down to Python. |
| 429 | |
| 430 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 431 | bool need_to_release_lock = true; |
| 432 | |
| 433 | if (CurrentThreadHasPythonLock()) |
| 434 | need_to_release_lock = false; |
| 435 | else if (!GetPythonLock (1)) |
| 436 | { |
| 437 | fprintf ((m_dbg_stdout ? m_dbg_stdout : stdout), |
| 438 | "Python interpreter is currently locked by another thread; unable to process command.\n"); |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | EnterSession (); |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 443 | bool success = false; |
| 444 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 445 | if (command) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 446 | { |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 447 | // Find the correct script interpreter dictionary in the main module. |
| 448 | PyObject *main_mod = PyImport_AddModule ("__main__"); |
| 449 | PyObject *script_interpreter_dict = NULL; |
| 450 | if (main_mod != NULL) |
| 451 | { |
| 452 | PyObject *main_dict = PyModule_GetDict (main_mod); |
| 453 | if ((main_dict != NULL) |
| 454 | && PyDict_Check (main_dict)) |
| 455 | { |
| 456 | // Go through the main dictionary looking for the correct python script interpreter dictionary |
| 457 | PyObject *key, *value; |
| 458 | Py_ssize_t pos = 0; |
| 459 | |
| 460 | while (PyDict_Next (main_dict, &pos, &key, &value)) |
| 461 | { |
| 462 | // We have stolen references to the key and value objects in the dictionary; we need to increment |
| 463 | // them now so that Python's garbage collector doesn't collect them out from under us. |
| 464 | Py_INCREF (key); |
| 465 | Py_INCREF (value); |
| 466 | if (strcmp (PyString_AsString (key), m_dictionary_name.c_str()) == 0) |
| 467 | { |
| 468 | script_interpreter_dict = value; |
| 469 | break; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | if (script_interpreter_dict != NULL) |
| 475 | { |
| 476 | PyObject *pfunc = NULL; |
| 477 | PyObject *pmod = PyImport_AddModule ("embedded_interpreter"); |
| 478 | if (pmod != NULL) |
| 479 | { |
| 480 | PyObject *pmod_dict = PyModule_GetDict (pmod); |
| 481 | if ((pmod_dict != NULL) |
| 482 | && PyDict_Check (pmod_dict)) |
| 483 | { |
| 484 | PyObject *key, *value; |
| 485 | Py_ssize_t pos = 0; |
| 486 | |
| 487 | while (PyDict_Next (pmod_dict, &pos, &key, &value)) |
| 488 | { |
| 489 | Py_INCREF (key); |
| 490 | Py_INCREF (value); |
| 491 | if (strcmp (PyString_AsString (key), "run_one_line") == 0) |
| 492 | { |
| 493 | pfunc = value; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | PyObject *string_arg = PyString_FromString (command); |
| 499 | if (pfunc && string_arg && PyCallable_Check (pfunc)) |
| 500 | { |
| 501 | PyObject *pargs = PyTuple_New (2); |
| 502 | if (pargs != NULL) |
| 503 | { |
| 504 | PyTuple_SetItem (pargs, 0, script_interpreter_dict); |
| 505 | PyTuple_SetItem (pargs, 1, string_arg); |
| 506 | PyObject *pvalue = PyObject_CallObject (pfunc, pargs); |
| 507 | Py_DECREF (pargs); |
| 508 | if (pvalue != NULL) |
| 509 | { |
| 510 | Py_DECREF (pvalue); |
| 511 | success = true; |
| 512 | } |
| 513 | else if (PyErr_Occurred ()) |
| 514 | { |
| 515 | PyErr_Print(); |
| 516 | PyErr_Clear(); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | Py_INCREF (script_interpreter_dict); |
| 523 | } |
| 524 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 525 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 526 | LeaveSession (); |
| 527 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 528 | if (need_to_release_lock) |
| 529 | ReleasePythonLock(); |
| 530 | |
Caroline Tice | 4a461da | 2011-01-14 21:09:29 +0000 | [diff] [blame] | 531 | if (success) |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 532 | return true; |
| 533 | |
| 534 | // The one-liner failed. Append the error message. |
| 535 | if (result) |
| 536 | result->AppendErrorWithFormat ("python failed attempting to evaluate '%s'\n", command); |
| 537 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | } |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 539 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 540 | LeaveSession (); |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 541 | |
| 542 | if (need_to_release_lock) |
| 543 | ReleasePythonLock (); |
| 544 | |
Johnny Chen | 60dde64 | 2010-07-30 22:33:14 +0000 | [diff] [blame] | 545 | if (result) |
| 546 | result->AppendError ("empty command passed to python\n"); |
| 547 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | size_t |
| 553 | ScriptInterpreterPython::InputReaderCallback |
| 554 | ( |
| 555 | void *baton, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 556 | InputReader &reader, |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 557 | InputReaderAction notification, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 558 | const char *bytes, |
| 559 | size_t bytes_len |
| 560 | ) |
| 561 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 562 | lldb::thread_t embedded_interpreter_thread; |
| 563 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT)); |
| 564 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 565 | if (baton == NULL) |
| 566 | return 0; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 567 | |
| 568 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
| 569 | |
| 570 | if (script_interpreter->m_script_lang != eScriptLanguagePython) |
| 571 | return 0; |
| 572 | |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 573 | File &out_file = reader.GetDebugger().GetOutputFile(); |
Caroline Tice | 67637d8 | 2010-12-15 19:51:12 +0000 | [diff] [blame] | 574 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | switch (notification) |
| 576 | { |
| 577 | case eInputReaderActivate: |
| 578 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 579 | out_file.Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n"); |
| 580 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | // Save terminal settings if we can |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 582 | int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor(); |
| 583 | if (input_fd == File::kInvalidDescriptor) |
Greg Clayton | 24b48ff | 2010-10-17 22:03:32 +0000 | [diff] [blame] | 584 | input_fd = STDIN_FILENO; |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 585 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 586 | script_interpreter->SaveTerminalState(input_fd); |
Greg Clayton | 9920858 | 2011-02-07 19:04:58 +0000 | [diff] [blame] | 587 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 588 | if (!CurrentThreadHasPythonLock()) |
| 589 | { |
| 590 | while (!GetPythonLock(1)) |
| 591 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 592 | out_file.Printf ("Python interpreter locked on another thread; waiting to acquire lock...\n"); |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 593 | } |
| 594 | script_interpreter->EnterSession (); |
| 595 | ReleasePythonLock(); |
| 596 | } |
| 597 | else |
| 598 | script_interpreter->EnterSession (); |
| 599 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 600 | char error_str[1024]; |
| 601 | if (script_interpreter->m_embedded_python_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, |
| 602 | sizeof(error_str))) |
| 603 | { |
| 604 | if (log) |
| 605 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in opening master pty (fd = %d).", |
| 606 | script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor()); |
| 607 | embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.embedded-python-loop>", |
| 608 | ScriptInterpreterPython::RunEmbeddedPythonInterpreter, |
| 609 | script_interpreter, NULL); |
Greg Clayton | 09c81ef | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 610 | if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread)) |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 611 | { |
| 612 | if (log) |
| 613 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in creating thread (thread = %d)", embedded_interpreter_thread); |
| 614 | Error detach_error; |
| 615 | Host::ThreadDetach (embedded_interpreter_thread, &detach_error); |
| 616 | } |
| 617 | else |
| 618 | { |
| 619 | if (log) |
| 620 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed in creating thread"); |
| 621 | reader.SetIsDone (true); |
| 622 | } |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | if (log) |
| 627 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed to open master pty "); |
| 628 | reader.SetIsDone (true); |
| 629 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 630 | } |
| 631 | break; |
| 632 | |
| 633 | case eInputReaderDeactivate: |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 634 | script_interpreter->LeaveSession (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | break; |
| 636 | |
| 637 | case eInputReaderReactivate: |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 638 | if (!CurrentThreadHasPythonLock()) |
| 639 | { |
| 640 | while (!GetPythonLock(1)) |
| 641 | { |
| 642 | // Wait until lock is acquired. |
| 643 | } |
| 644 | script_interpreter->EnterSession (); |
| 645 | ReleasePythonLock(); |
| 646 | } |
| 647 | else |
| 648 | script_interpreter->EnterSession (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | break; |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 650 | |
| 651 | case eInputReaderInterrupt: |
| 652 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "raise KeyboardInterrupt\n", 24); |
| 653 | break; |
| 654 | |
| 655 | case eInputReaderEndOfFile: |
| 656 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()\n", 7); |
| 657 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 658 | |
| 659 | case eInputReaderGotToken: |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 660 | if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 661 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 662 | if (log) |
| 663 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %d", bytes, |
| 664 | bytes_len); |
| 665 | if (bytes && bytes_len) |
| 666 | { |
| 667 | if ((int) bytes[0] == 4) |
| 668 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()", 6); |
| 669 | else |
| 670 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), bytes, bytes_len); |
| 671 | } |
| 672 | ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "\n", 1); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 673 | } |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 674 | else |
| 675 | { |
| 676 | if (log) |
| 677 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %d, Master File Descriptor is bad.", |
| 678 | bytes, |
| 679 | bytes_len); |
| 680 | reader.SetIsDone (true); |
| 681 | } |
| 682 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case eInputReaderDone: |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 686 | script_interpreter->LeaveSession (); |
| 687 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 688 | // Restore terminal settings if they were validly saved |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 689 | if (log) |
| 690 | log->Printf ("ScriptInterpreterPython::InputReaderCallback, Done, closing down input reader."); |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 691 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 692 | script_interpreter->RestoreTerminalState (); |
| 693 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 694 | script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 695 | break; |
| 696 | } |
| 697 | |
| 698 | return bytes_len; |
| 699 | } |
| 700 | |
| 701 | |
| 702 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 703 | ScriptInterpreterPython::ExecuteInterpreterLoop () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 704 | { |
| 705 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 706 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 707 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 708 | |
| 709 | // At the moment, the only time the debugger does not have an input file handle is when this is called |
| 710 | // directly from Python, in which case it is both dangerous and unnecessary (not to mention confusing) to |
| 711 | // try to embed a running interpreter loop inside the already running Python interpreter loop, so we won't |
| 712 | // do it. |
| 713 | |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 714 | if (!debugger.GetInputFile().IsValid()) |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 715 | return; |
| 716 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 717 | InputReaderSP reader_sp (new InputReader(debugger)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 718 | if (reader_sp) |
| 719 | { |
| 720 | Error error (reader_sp->Initialize (ScriptInterpreterPython::InputReaderCallback, |
| 721 | this, // baton |
| 722 | eInputReaderGranularityLine, // token size, to pass to callback function |
| 723 | NULL, // end token |
| 724 | NULL, // prompt |
| 725 | true)); // echo input |
| 726 | |
| 727 | if (error.Success()) |
| 728 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 729 | debugger.PushInputReader (reader_sp); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 730 | m_embedded_thread_input_reader_sp = reader_sp; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | bool |
| 736 | ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string, |
| 737 | ScriptInterpreter::ReturnType return_type, |
| 738 | void *ret_value) |
| 739 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 740 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 741 | bool need_to_release_lock = true; |
| 742 | |
| 743 | if (CurrentThreadHasPythonLock()) |
| 744 | need_to_release_lock = false; |
| 745 | else if (!GetPythonLock (1)) |
| 746 | { |
| 747 | fprintf ((m_dbg_stdout ? m_dbg_stdout : stdout), |
| 748 | "Python interpreter is currently locked by another thread; unable to process command.\n"); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | EnterSession (); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 754 | PyObject *py_return = NULL; |
| 755 | PyObject *mainmod = PyImport_AddModule ("__main__"); |
| 756 | PyObject *globals = PyModule_GetDict (mainmod); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 757 | PyObject *locals = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 758 | PyObject *py_error = NULL; |
| 759 | bool ret_success; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 760 | bool should_decrement_locals = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | int success; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 762 | |
| 763 | if (PyDict_Check (globals)) |
| 764 | { |
| 765 | PyObject *key, *value; |
| 766 | Py_ssize_t pos = 0; |
| 767 | |
| 768 | int i = 0; |
| 769 | while (PyDict_Next (globals, &pos, &key, &value)) |
| 770 | { |
| 771 | // We have stolen references to the key and value objects in the dictionary; we need to increment them now |
| 772 | // so that Python's garbage collector doesn't collect them out from under us. |
| 773 | Py_INCREF (key); |
| 774 | Py_INCREF (value); |
| 775 | char *c_str = PyString_AsString (key); |
| 776 | if (strcmp (c_str, m_dictionary_name.c_str()) == 0) |
| 777 | locals = value; |
| 778 | ++i; |
| 779 | } |
| 780 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 781 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 782 | if (locals == NULL) |
| 783 | { |
| 784 | locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str()); |
| 785 | should_decrement_locals = true; |
| 786 | } |
| 787 | |
| 788 | if (locals == NULL) |
| 789 | { |
| 790 | locals = globals; |
| 791 | should_decrement_locals = false; |
| 792 | } |
| 793 | |
| 794 | py_error = PyErr_Occurred(); |
| 795 | if (py_error != NULL) |
| 796 | PyErr_Clear(); |
| 797 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | if (in_string != NULL) |
| 799 | { |
| 800 | py_return = PyRun_String (in_string, Py_eval_input, globals, locals); |
| 801 | if (py_return == NULL) |
| 802 | { |
| 803 | py_error = PyErr_Occurred (); |
| 804 | if (py_error != NULL) |
| 805 | PyErr_Clear (); |
| 806 | |
| 807 | py_return = PyRun_String (in_string, Py_single_input, globals, locals); |
| 808 | } |
| 809 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 810 | if (locals != NULL |
| 811 | && should_decrement_locals) |
| 812 | Py_DECREF (locals); |
| 813 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 814 | if (py_return != NULL) |
| 815 | { |
| 816 | switch (return_type) |
| 817 | { |
| 818 | case eCharPtr: // "char *" |
| 819 | { |
| 820 | const char format[3] = "s#"; |
| 821 | success = PyArg_Parse (py_return, format, (char **) &ret_value); |
| 822 | break; |
| 823 | } |
| 824 | case eBool: |
| 825 | { |
| 826 | const char format[2] = "b"; |
| 827 | success = PyArg_Parse (py_return, format, (bool *) ret_value); |
| 828 | break; |
| 829 | } |
| 830 | case eShortInt: |
| 831 | { |
| 832 | const char format[2] = "h"; |
| 833 | success = PyArg_Parse (py_return, format, (short *) ret_value); |
| 834 | break; |
| 835 | } |
| 836 | case eShortIntUnsigned: |
| 837 | { |
| 838 | const char format[2] = "H"; |
| 839 | success = PyArg_Parse (py_return, format, (unsigned short *) ret_value); |
| 840 | break; |
| 841 | } |
| 842 | case eInt: |
| 843 | { |
| 844 | const char format[2] = "i"; |
| 845 | success = PyArg_Parse (py_return, format, (int *) ret_value); |
| 846 | break; |
| 847 | } |
| 848 | case eIntUnsigned: |
| 849 | { |
| 850 | const char format[2] = "I"; |
| 851 | success = PyArg_Parse (py_return, format, (unsigned int *) ret_value); |
| 852 | break; |
| 853 | } |
| 854 | case eLongInt: |
| 855 | { |
| 856 | const char format[2] = "l"; |
| 857 | success = PyArg_Parse (py_return, format, (long *) ret_value); |
| 858 | break; |
| 859 | } |
| 860 | case eLongIntUnsigned: |
| 861 | { |
| 862 | const char format[2] = "k"; |
| 863 | success = PyArg_Parse (py_return, format, (unsigned long *) ret_value); |
| 864 | break; |
| 865 | } |
| 866 | case eLongLong: |
| 867 | { |
| 868 | const char format[2] = "L"; |
| 869 | success = PyArg_Parse (py_return, format, (long long *) ret_value); |
| 870 | break; |
| 871 | } |
| 872 | case eLongLongUnsigned: |
| 873 | { |
| 874 | const char format[2] = "K"; |
| 875 | success = PyArg_Parse (py_return, format, (unsigned long long *) ret_value); |
| 876 | break; |
| 877 | } |
| 878 | case eFloat: |
| 879 | { |
| 880 | const char format[2] = "f"; |
| 881 | success = PyArg_Parse (py_return, format, (float *) ret_value); |
| 882 | break; |
| 883 | } |
| 884 | case eDouble: |
| 885 | { |
| 886 | const char format[2] = "d"; |
| 887 | success = PyArg_Parse (py_return, format, (double *) ret_value); |
| 888 | break; |
| 889 | } |
| 890 | case eChar: |
| 891 | { |
| 892 | const char format[2] = "c"; |
| 893 | success = PyArg_Parse (py_return, format, (char *) ret_value); |
| 894 | break; |
| 895 | } |
| 896 | default: |
| 897 | {} |
| 898 | } |
| 899 | Py_DECREF (py_return); |
| 900 | if (success) |
| 901 | ret_success = true; |
| 902 | else |
| 903 | ret_success = false; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | py_error = PyErr_Occurred(); |
| 908 | if (py_error != NULL) |
| 909 | { |
| 910 | if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError)) |
| 911 | PyErr_Print (); |
| 912 | PyErr_Clear(); |
| 913 | ret_success = false; |
| 914 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 915 | |
| 916 | LeaveSession (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 917 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 918 | if (need_to_release_lock) |
| 919 | ReleasePythonLock(); |
| 920 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 921 | return ret_success; |
| 922 | } |
| 923 | |
| 924 | bool |
| 925 | ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string) |
| 926 | { |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 927 | FILE *tmp_fh = (m_dbg_stdout ? m_dbg_stdout : stdout); |
| 928 | bool need_to_release_lock = true; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 929 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 930 | if (CurrentThreadHasPythonLock()) |
| 931 | need_to_release_lock = false; |
| 932 | else |
| 933 | { |
| 934 | while (!GetPythonLock (1)) |
| 935 | fprintf (tmp_fh, "Python interpreter locked on another thread; waiting to acquire lock...\n"); |
| 936 | } |
| 937 | |
| 938 | EnterSession (); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 939 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 940 | bool success = false; |
| 941 | PyObject *py_return = NULL; |
| 942 | PyObject *mainmod = PyImport_AddModule ("__main__"); |
| 943 | PyObject *globals = PyModule_GetDict (mainmod); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 944 | PyObject *locals = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 945 | PyObject *py_error = NULL; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 946 | bool should_decrement_locals = false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 947 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 948 | if (PyDict_Check (globals)) |
| 949 | { |
| 950 | PyObject *key, *value; |
| 951 | Py_ssize_t pos = 0; |
| 952 | |
| 953 | while (PyDict_Next (globals, &pos, &key, &value)) |
| 954 | { |
| 955 | // We have stolen references to the key and value objects in the dictionary; we need to increment them now |
| 956 | // so that Python's garbage collector doesn't collect them out from under us. |
| 957 | Py_INCREF (key); |
| 958 | Py_INCREF (value); |
| 959 | if (strcmp (PyString_AsString (key), m_dictionary_name.c_str()) == 0) |
| 960 | locals = value; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | if (locals == NULL) |
| 965 | { |
| 966 | locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str()); |
| 967 | should_decrement_locals = true; |
| 968 | } |
| 969 | |
| 970 | if (locals == NULL) |
| 971 | { |
| 972 | locals = globals; |
| 973 | should_decrement_locals = false; |
| 974 | } |
| 975 | |
| 976 | py_error = PyErr_Occurred(); |
| 977 | if (py_error != NULL) |
| 978 | PyErr_Clear(); |
| 979 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 980 | if (in_string != NULL) |
| 981 | { |
| 982 | struct _node *compiled_node = PyParser_SimpleParseString (in_string, Py_file_input); |
| 983 | if (compiled_node) |
| 984 | { |
| 985 | PyCodeObject *compiled_code = PyNode_Compile (compiled_node, "temp.py"); |
| 986 | if (compiled_code) |
| 987 | { |
| 988 | py_return = PyEval_EvalCode (compiled_code, globals, locals); |
| 989 | if (py_return != NULL) |
| 990 | { |
| 991 | success = true; |
| 992 | Py_DECREF (py_return); |
| 993 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 994 | if (locals && should_decrement_locals) |
| 995 | Py_DECREF (locals); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | py_error = PyErr_Occurred (); |
| 1001 | if (py_error != NULL) |
| 1002 | { |
| 1003 | if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError)) |
| 1004 | PyErr_Print (); |
| 1005 | PyErr_Clear(); |
| 1006 | success = false; |
| 1007 | } |
| 1008 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1009 | LeaveSession (); |
| 1010 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1011 | if (need_to_release_lock) |
| 1012 | ReleasePythonLock(); |
| 1013 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1014 | return success; |
| 1015 | } |
| 1016 | |
| 1017 | static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end."; |
| 1018 | |
| 1019 | size_t |
| 1020 | ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback |
| 1021 | ( |
| 1022 | void *baton, |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1023 | InputReader &reader, |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1024 | InputReaderAction notification, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1025 | const char *bytes, |
| 1026 | size_t bytes_len |
| 1027 | ) |
| 1028 | { |
| 1029 | static StringList commands_in_progress; |
| 1030 | |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1031 | File &out_file = reader.GetDebugger().GetOutputFile(); |
Caroline Tice | c95c6d1 | 2010-09-14 22:49:06 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1033 | switch (notification) |
| 1034 | { |
| 1035 | case eInputReaderActivate: |
| 1036 | { |
| 1037 | commands_in_progress.Clear(); |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1038 | if (out_file.IsValid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1039 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1040 | out_file.Printf ("%s\n", g_reader_instructions); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1041 | if (reader.GetPrompt()) |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1042 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1043 | out_file.Flush (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | break; |
| 1047 | |
| 1048 | case eInputReaderDeactivate: |
| 1049 | break; |
| 1050 | |
| 1051 | case eInputReaderReactivate: |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1052 | if (reader.GetPrompt() && out_file.IsValid()) |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1053 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1054 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1055 | out_file.Flush (); |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1056 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1057 | break; |
| 1058 | |
| 1059 | case eInputReaderGotToken: |
| 1060 | { |
| 1061 | std::string temp_string (bytes, bytes_len); |
| 1062 | commands_in_progress.AppendString (temp_string.c_str()); |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1063 | if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt()) |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1064 | { |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1065 | out_file.Printf ("%s", reader.GetPrompt()); |
| 1066 | out_file.Flush (); |
Caroline Tice | f81b4c5 | 2010-10-27 18:34:42 +0000 | [diff] [blame] | 1067 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1068 | } |
| 1069 | break; |
| 1070 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1071 | case eInputReaderEndOfFile: |
| 1072 | case eInputReaderInterrupt: |
| 1073 | // Control-c (SIGINT) & control-d both mean finish & exit. |
| 1074 | reader.SetIsDone(true); |
| 1075 | |
| 1076 | // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command. |
| 1077 | if (notification == eInputReaderInterrupt) |
| 1078 | commands_in_progress.Clear(); |
| 1079 | |
| 1080 | // Fall through here... |
| 1081 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | case eInputReaderDone: |
| 1083 | { |
| 1084 | BreakpointOptions *bp_options = (BreakpointOptions *)baton; |
| 1085 | std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData()); |
| 1086 | data_ap->user_source.AppendList (commands_in_progress); |
| 1087 | if (data_ap.get()) |
| 1088 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1089 | ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1090 | if (interpreter) |
| 1091 | { |
| 1092 | if (interpreter->GenerateBreakpointCommandCallbackData (data_ap->user_source, |
| 1093 | data_ap->script_source)) |
| 1094 | { |
| 1095 | if (data_ap->script_source.GetSize() == 1) |
| 1096 | { |
| 1097 | BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release())); |
| 1098 | bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp); |
| 1099 | } |
| 1100 | } |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1101 | else |
Greg Clayton | 5892856 | 2011-02-09 01:08:52 +0000 | [diff] [blame] | 1102 | out_file.Printf ("Warning: No command attached to breakpoint.\n"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | // FIXME: Error processing. |
| 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | break; |
| 1111 | |
| 1112 | } |
| 1113 | |
| 1114 | return bytes_len; |
| 1115 | } |
| 1116 | |
| 1117 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1118 | ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1119 | CommandReturnObject &result) |
| 1120 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1121 | Debugger &debugger = GetCommandInterpreter().GetDebugger(); |
| 1122 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1123 | InputReaderSP reader_sp (new InputReader (debugger)); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1124 | |
| 1125 | if (reader_sp) |
| 1126 | { |
| 1127 | Error err = reader_sp->Initialize ( |
| 1128 | ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback, |
| 1129 | bp_options, // baton |
| 1130 | eInputReaderGranularityLine, // token size, for feeding data to callback function |
| 1131 | "DONE", // end token |
| 1132 | "> ", // prompt |
| 1133 | true); // echo input |
| 1134 | |
| 1135 | if (err.Success()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1136 | debugger.PushInputReader (reader_sp); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1137 | else |
| 1138 | { |
| 1139 | result.AppendError (err.AsCString()); |
| 1140 | result.SetStatus (eReturnStatusFailed); |
| 1141 | } |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | result.AppendError("out of memory"); |
| 1146 | result.SetStatus (eReturnStatusFailed); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Johnny Chen | 3e0571b | 2010-09-11 00:23:59 +0000 | [diff] [blame] | 1150 | // Set a Python one-liner as the callback for the breakpoint. |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1151 | void |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1152 | ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options, |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1153 | const char *oneliner) |
| 1154 | { |
| 1155 | std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData()); |
| 1156 | |
| 1157 | // It's necessary to set both user_source and script_source to the oneliner. |
| 1158 | // The former is used to generate callback description (as in breakpoint command list) |
| 1159 | // 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] | 1160 | |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1161 | data_ap->user_source.AppendString (oneliner); |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1162 | |
Caroline Tice | 5136f94 | 2010-09-27 21:35:15 +0000 | [diff] [blame] | 1163 | if (GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source)) |
| 1164 | { |
| 1165 | if (data_ap->script_source.GetSize() == 1) |
| 1166 | { |
| 1167 | BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release())); |
| 1168 | bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp); |
| 1169 | } |
| 1170 | } |
| 1171 | |
Johnny Chen | d1c2dca | 2010-09-10 18:21:10 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1175 | bool |
| 1176 | ScriptInterpreterPython::ExportFunctionDefinitionToInterpreter (StringList &function_def) |
| 1177 | { |
| 1178 | // Convert StringList to one long, newline delimited, const char *. |
| 1179 | std::string function_def_string; |
| 1180 | |
| 1181 | int num_lines = function_def.GetSize(); |
| 1182 | |
| 1183 | for (int i = 0; i < num_lines; ++i) |
| 1184 | { |
| 1185 | function_def_string.append (function_def.GetStringAtIndex(i)); |
| 1186 | if (function_def_string.at (function_def_string.length() - 1) != '\n') |
| 1187 | function_def_string.append ("\n"); |
| 1188 | |
| 1189 | } |
| 1190 | |
| 1191 | return ExecuteMultipleLines (function_def_string.c_str()); |
| 1192 | } |
| 1193 | |
| 1194 | bool |
| 1195 | ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, StringList &callback_data) |
| 1196 | { |
| 1197 | static int num_created_functions = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1198 | user_input.RemoveBlankLines (); |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1199 | int num_lines = user_input.GetSize (); |
| 1200 | StreamString sstr; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1201 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1202 | // Check to see if we have any data; if not, just return. |
| 1203 | if (user_input.GetSize() == 0) |
| 1204 | return false; |
| 1205 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1206 | // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the |
| 1207 | // frame and breakpoint location as parameters to the function. |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1208 | |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1209 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1210 | sstr.Printf ("lldb_autogen_python_bp_callback_func_%d", num_created_functions); |
| 1211 | ++num_created_functions; |
| 1212 | std::string auto_generated_function_name = sstr.GetData(); |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1213 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1214 | sstr.Clear(); |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1215 | StringList auto_generated_function; |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1216 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1217 | // Create the function name & definition string. |
| 1218 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1219 | 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] | 1220 | auto_generated_function.AppendString (sstr.GetData()); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1221 | |
| 1222 | // Pre-pend code for setting up the session dictionary. |
| 1223 | |
| 1224 | auto_generated_function.AppendString (" global_dict = globals()"); // Grab the global dictionary |
| 1225 | auto_generated_function.AppendString (" new_keys = dict.keys()"); // Make a list of keys in the session dict |
| 1226 | auto_generated_function.AppendString (" old_keys = global_dict.keys()"); // Save list of keys in global dict |
| 1227 | auto_generated_function.AppendString (" global_dict.update (dict)"); // Add the session dictionary to the |
| 1228 | // global dictionary. |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1229 | |
| 1230 | // Wrap everything up inside the function, increasing the indentation. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1231 | |
| 1232 | for (int i = 0; i < num_lines; ++i) |
| 1233 | { |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1234 | sstr.Clear (); |
| 1235 | sstr.Printf (" %s", user_input.GetStringAtIndex (i)); |
| 1236 | auto_generated_function.AppendString (sstr.GetData()); |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1237 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1238 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1239 | // Append code to clean up the global dictionary and update the session dictionary (all updates in the function |
| 1240 | // got written to the values in the global dictionary, not the session dictionary). |
| 1241 | |
| 1242 | auto_generated_function.AppendString (" for key in new_keys:"); // Iterate over all the keys from session dict |
| 1243 | auto_generated_function.AppendString (" dict[key] = global_dict[key]"); // Update session dict values |
| 1244 | auto_generated_function.AppendString (" if key not in old_keys:"); // If key was not originally in global dict |
| 1245 | auto_generated_function.AppendString (" del global_dict[key]"); // ...then remove key/value from global dict |
| 1246 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1247 | // Verify that the results are valid Python. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1248 | |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1249 | if (!ExportFunctionDefinitionToInterpreter (auto_generated_function)) |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1250 | { |
Caroline Tice | b447e84 | 2010-09-21 19:25:28 +0000 | [diff] [blame] | 1251 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1252 | } |
Caroline Tice | 59c5d5d | 2010-09-27 18:00:20 +0000 | [diff] [blame] | 1253 | |
| 1254 | // Store the name of the auto-generated function to be called. |
| 1255 | |
| 1256 | callback_data.AppendString (auto_generated_function_name.c_str()); |
| 1257 | return true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1260 | bool |
| 1261 | ScriptInterpreterPython::BreakpointCallbackFunction |
| 1262 | ( |
| 1263 | void *baton, |
| 1264 | StoppointCallbackContext *context, |
| 1265 | user_id_t break_id, |
| 1266 | user_id_t break_loc_id |
| 1267 | ) |
| 1268 | { |
| 1269 | BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton; |
| 1270 | const char *python_function_name = bp_option_data->script_source.GetStringAtIndex (0); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (!context) |
| 1273 | return true; |
| 1274 | |
| 1275 | Target *target = context->exe_ctx.target; |
| 1276 | |
| 1277 | if (!target) |
| 1278 | return true; |
| 1279 | |
| 1280 | Debugger &debugger = target->GetDebugger(); |
| 1281 | ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter(); |
| 1282 | ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter; |
| 1283 | |
| 1284 | if (!script_interpreter) |
| 1285 | return true; |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1286 | |
| 1287 | if (python_function_name != NULL |
| 1288 | && python_function_name[0] != '\0') |
| 1289 | { |
| 1290 | Thread *thread = context->exe_ctx.thread; |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1291 | const StackFrameSP stop_frame_sp = thread->GetStackFrameSPForStackFramePtr (context->exe_ctx.frame); |
| 1292 | BreakpointSP breakpoint_sp = target->GetBreakpointByID (break_id); |
| 1293 | const BreakpointLocationSP bp_loc_sp = breakpoint_sp->FindLocationByID (break_loc_id); |
| 1294 | |
| 1295 | SBFrame sb_frame (stop_frame_sp); |
| 1296 | SBBreakpointLocation sb_bp_loc (bp_loc_sp); |
| 1297 | |
| 1298 | if (sb_bp_loc.IsValid() || sb_frame.IsValid()) |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1299 | { |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1300 | bool ret_val = true; |
| 1301 | FILE *tmp_fh = (python_interpreter->m_dbg_stdout ? python_interpreter->m_dbg_stdout : stdout); |
| 1302 | if (CurrentThreadHasPythonLock()) |
| 1303 | { |
| 1304 | python_interpreter->EnterSession (); |
| 1305 | ret_val = LLDBSWIGPythonBreakpointCallbackFunction(python_function_name, |
| 1306 | python_interpreter->m_dictionary_name.c_str(), |
| 1307 | sb_frame, sb_bp_loc); |
| 1308 | python_interpreter->LeaveSession (); |
| 1309 | } |
| 1310 | else |
| 1311 | { |
| 1312 | while (!GetPythonLock (1)) |
| 1313 | fprintf (tmp_fh, |
| 1314 | "Python interpreter locked on another thread; waiting to acquire lock...\n"); |
| 1315 | python_interpreter->EnterSession (); |
| 1316 | ret_val = LLDBSWIGPythonBreakpointCallbackFunction(python_function_name, |
| 1317 | python_interpreter->m_dictionary_name.c_str(), |
| 1318 | sb_frame, sb_bp_loc); |
| 1319 | python_interpreter->LeaveSession (); |
| 1320 | ReleasePythonLock (); |
| 1321 | } |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1322 | return ret_val; |
| 1323 | } |
Greg Clayton | 5144f38 | 2010-10-07 17:14:24 +0000 | [diff] [blame] | 1324 | } |
| 1325 | // We currently always true so we stop in case anything goes wrong when |
| 1326 | // trying to call the script function |
| 1327 | return true; |
| 1328 | } |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1329 | |
| 1330 | lldb::thread_result_t |
| 1331 | ScriptInterpreterPython::RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton) |
| 1332 | { |
| 1333 | ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton; |
| 1334 | |
| 1335 | LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT)); |
| 1336 | |
| 1337 | if (log) |
| 1338 | log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread starting...", baton); |
| 1339 | |
| 1340 | char error_str[1024]; |
| 1341 | 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] | 1342 | bool need_to_release_lock = true; |
| 1343 | bool safe_to_run = false; |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1344 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1345 | if (CurrentThreadHasPythonLock()) |
| 1346 | { |
| 1347 | safe_to_run = true; |
| 1348 | need_to_release_lock = false; |
| 1349 | } |
| 1350 | else |
| 1351 | { |
| 1352 | int interval = 1; |
| 1353 | safe_to_run = GetPythonLock (interval); |
| 1354 | while (!safe_to_run) |
| 1355 | { |
| 1356 | interval = interval * 2; |
| 1357 | safe_to_run = GetPythonLock (interval); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | if (pty_slave_name != NULL && safe_to_run) |
| 1362 | { |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1363 | StreamString run_string; |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1364 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1365 | script_interpreter->EnterSession (); |
| 1366 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1367 | run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 1368 | PyRun_SimpleString (run_string.GetData()); |
| 1369 | run_string.Clear (); |
| 1370 | |
| 1371 | run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str()); |
| 1372 | PyRun_SimpleString (run_string.GetData()); |
| 1373 | run_string.Clear (); |
| 1374 | |
| 1375 | run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 1376 | PyRun_SimpleString (run_string.GetData()); |
| 1377 | run_string.Clear (); |
| 1378 | |
| 1379 | run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(), |
| 1380 | pty_slave_name); |
| 1381 | PyRun_SimpleString (run_string.GetData()); |
| 1382 | run_string.Clear (); |
| 1383 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1384 | // The following call drops into the embedded interpreter loop and stays there until the |
| 1385 | // user chooses to exit from the Python interpreter. |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1386 | |
| 1387 | run_string.Printf ("run_python_interpreter (%s)", script_interpreter->m_dictionary_name.c_str()); |
| 1388 | PyRun_SimpleString (run_string.GetData()); |
| 1389 | run_string.Clear (); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1390 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1391 | run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str()); |
| 1392 | PyRun_SimpleString (run_string.GetData()); |
| 1393 | run_string.Clear(); |
| 1394 | |
| 1395 | run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str()); |
| 1396 | PyRun_SimpleString (run_string.GetData()); |
| 1397 | run_string.Clear(); |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1398 | |
| 1399 | script_interpreter->LeaveSession (); |
| 1400 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Caroline Tice | 202f6b8 | 2011-01-17 21:55:19 +0000 | [diff] [blame] | 1403 | if (!safe_to_run) |
| 1404 | fprintf ((script_interpreter->m_dbg_stdout ? script_interpreter->m_dbg_stdout : stdout), |
| 1405 | "Python interpreter locked on another thread; unable to acquire lock.\n"); |
| 1406 | |
| 1407 | if (need_to_release_lock) |
| 1408 | ReleasePythonLock (); |
| 1409 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1410 | if (script_interpreter->m_embedded_thread_input_reader_sp) |
| 1411 | script_interpreter->m_embedded_thread_input_reader_sp->SetIsDone (true); |
| 1412 | |
| 1413 | script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1414 | |
| 1415 | script_interpreter->m_pty_slave_is_open = false; |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1416 | |
| 1417 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT); |
| 1418 | if (log) |
| 1419 | log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread exiting...", baton); |
| 1420 | |
| 1421 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1422 | // Clean up the input reader and make the debugger pop it off the stack. |
| 1423 | Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger(); |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1424 | const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp; |
| 1425 | script_interpreter->m_embedded_thread_input_reader_sp.reset(); |
| 1426 | debugger.PopInputReader (reader_sp); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1427 | |
Caroline Tice | 2ade611 | 2010-11-10 19:18:14 +0000 | [diff] [blame] | 1428 | return NULL; |
| 1429 | } |
| 1430 | |
| 1431 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1432 | void |
| 1433 | ScriptInterpreterPython::Initialize () |
| 1434 | { |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1435 | Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); |
| 1436 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 1437 | // Python will muck with STDIN terminal state, so save off any current TTY |
| 1438 | // settings so we can restore them. |
| 1439 | TerminalState stdin_tty_state; |
| 1440 | stdin_tty_state.Save(STDIN_FILENO, false); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1441 | |
| 1442 | // Find the module that owns this code and use that path we get to |
| 1443 | // set the PYTHONPATH appropriately. |
| 1444 | |
| 1445 | FileSpec file_spec; |
| 1446 | char python_dir_path[PATH_MAX]; |
| 1447 | if (Host::GetLLDBPath (ePathTypePythonDir, file_spec)) |
| 1448 | { |
| 1449 | std::string python_path; |
| 1450 | const char *curr_python_path = ::getenv ("PYTHONPATH"); |
| 1451 | if (curr_python_path) |
| 1452 | { |
| 1453 | // We have a current value for PYTHONPATH, so lets append to it |
| 1454 | python_path.append (curr_python_path); |
| 1455 | } |
| 1456 | |
| 1457 | if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path))) |
| 1458 | { |
| 1459 | if (!python_path.empty()) |
| 1460 | python_path.append (1, ':'); |
| 1461 | python_path.append (python_dir_path); |
| 1462 | } |
| 1463 | |
| 1464 | if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, file_spec)) |
| 1465 | { |
| 1466 | if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path))) |
| 1467 | { |
| 1468 | if (!python_path.empty()) |
| 1469 | python_path.append (1, ':'); |
| 1470 | python_path.append (python_dir_path); |
| 1471 | } |
| 1472 | } |
| 1473 | const char *pathon_path_env_cstr = python_path.c_str(); |
| 1474 | ::setenv ("PYTHONPATH", pathon_path_env_cstr, 1); |
| 1475 | } |
| 1476 | |
| 1477 | Py_Initialize (); |
| 1478 | |
| 1479 | PyObject *compiled_module = Py_CompileString (embedded_interpreter_string, |
| 1480 | "embedded_interpreter.py", |
| 1481 | Py_file_input); |
| 1482 | |
| 1483 | PyObject *py_error = PyErr_Occurred (); |
| 1484 | if (py_error != NULL) |
| 1485 | { |
| 1486 | PyErr_Print(); |
| 1487 | PyErr_Clear(); |
| 1488 | } |
| 1489 | |
| 1490 | |
| 1491 | // This function is in the C++ output file generated by SWIG after it is |
| 1492 | // run on all of the headers in "lldb/API/SB*.h" |
| 1493 | init_lldb (); |
| 1494 | |
| 1495 | // Update the path python uses to search for modules to include the current directory. |
| 1496 | |
| 1497 | int success = PyRun_SimpleString ("import sys"); |
| 1498 | success = PyRun_SimpleString ("sys.path.append ('.')"); |
| 1499 | |
| 1500 | PyObject *pmod = NULL; |
| 1501 | |
| 1502 | if (compiled_module) |
| 1503 | { |
| 1504 | pmod = PyImport_ExecCodeModule (const_cast<char*> ("embedded_interpreter"), |
| 1505 | compiled_module); |
| 1506 | Py_DECREF (compiled_module); |
| 1507 | } |
| 1508 | |
| 1509 | if (pmod != NULL) |
| 1510 | { |
| 1511 | PyRun_SimpleString ("from embedded_interpreter import run_python_interpreter"); |
| 1512 | PyRun_SimpleString ("from embedded_interpreter import run_one_line"); |
| 1513 | PyRun_SimpleString ("import sys"); |
| 1514 | PyRun_SimpleString ("from termios import *"); |
| 1515 | Py_DECREF (pmod); |
| 1516 | } |
Greg Clayton | 9920858 | 2011-02-07 19:04:58 +0000 | [diff] [blame] | 1517 | |
Greg Clayton | 0fdd4a0 | 2011-02-07 23:24:47 +0000 | [diff] [blame] | 1518 | stdin_tty_state.Restore(); |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | void |
| 1522 | ScriptInterpreterPython::Terminate () |
| 1523 | { |
| 1524 | // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it). Calling |
| 1525 | // Py_Finalize here causes test suite runs to seg fault: The test suite runs in Python. It registers |
| 1526 | // SBDebugger::Terminate to be called 'at_exit'. When the test suite Python harness finishes up, it calls |
| 1527 | // Py_Finalize, which calls all the 'at_exit' registered functions. SBDebugger::Terminate calls Debugger::Terminate, |
| 1528 | // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls |
| 1529 | // ScriptInterpreterPython::Terminate. So if we call Py_Finalize here, we end up with Py_Finalize being called from |
| 1530 | // within Py_Finalize, which results in a seg fault. |
| 1531 | // |
| 1532 | // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't |
| 1533 | // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the |
| 1534 | // process exits). |
| 1535 | // |
| 1536 | // Py_Finalize (); |
| 1537 | } |