blob: 07eeafac0c2204a944c0c1cb9c8ff0da392f3d4d [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- 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 Clayton17f5afe2011-02-05 02:56:16 +000011// the *FIRST* header file included in ScriptInterpreterPython.h, and that
12// must be the *FIRST* header file included here.
Chris Lattner24943d22010-06-08 16:52:24 +000013
14#include "lldb/Interpreter/ScriptInterpreterPython.h"
15
Chris Lattner24943d22010-06-08 16:52:24 +000016#include <stdlib.h>
17#include <stdio.h>
18
19#include <string>
20
Greg Clayton5144f382010-10-07 17:14:24 +000021#include "lldb/API/SBFrame.h"
22#include "lldb/API/SBBreakpointLocation.h"
Greg Clayton63094e02010-06-23 01:19:29 +000023#include "lldb/Breakpoint/StoppointCallbackContext.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Core/Timer.h"
26#include "lldb/Host/Host.h"
27#include "lldb/Interpreter/CommandInterpreter.h"
28#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton5144f382010-10-07 17:14:24 +000029#include "lldb/Target/Thread.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030
Greg Clayton121f3312010-07-07 18:40:03 +000031// 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 Lattner24943d22010-06-08 16:52:24 +000033extern "C" void init_lldb (void);
34
Greg Clayton5144f382010-10-07 17:14:24 +000035extern "C" bool
36LLDBSWIGPythonBreakpointCallbackFunction
37(
38 const char *python_function_name,
Caroline Tice0aa2e552011-01-14 00:29:16 +000039 const char *session_dictionary_name,
Greg Clayton5144f382010-10-07 17:14:24 +000040 lldb::SBFrame& sb_frame,
41 lldb::SBBreakpointLocation& sb_bp_loc
42);
43
Chris Lattner24943d22010-06-08 16:52:24 +000044using namespace lldb;
45using namespace lldb_private;
46
47const char embedded_interpreter_string[] =
48"import readline\n\
49import code\n\
50import sys\n\
51import traceback\n\
52\n\
53class 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 Molendaa8a5e562010-06-09 21:56:00 +000095 if (temp_str.lower() == \"quit()\" or temp_str.lower() == \"exit()\"):\n\
Chris Lattner24943d22010-06-08 16:52:24 +000096 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 Tice0aa2e552011-01-14 00:29:16 +0000134 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 Lattner24943d22010-06-08 16:52:24 +0000142def 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 Tice0aa2e552011-01-14 00:29:16 +0000145 repl.interact()\n\
146\n\
147def run_one_line (dict, input_string):\n\
148 repl = SimpleREPL ('', dict)\n\
149 repl.one_line (input_string)\n";
Chris Lattner24943d22010-06-08 16:52:24 +0000150
151static 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 Tice202f6b82011-01-17 21:55:19 +0000158static Predicate<lldb::tid_t> &
159PythonMutexPredicate ()
Caroline Tice0aa2e552011-01-14 00:29:16 +0000160{
Caroline Tice202f6b82011-01-17 21:55:19 +0000161 static lldb_private::Predicate<lldb::tid_t> g_interpreter_is_running (LLDB_INVALID_THREAD_ID);
162 return g_interpreter_is_running;
163}
164
165static bool
166CurrentThreadHasPythonLock ()
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
175static bool
176GetPythonLock (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
191static void
192ReleasePythonLock ()
193{
194 PythonMutexPredicate().SetValue (LLDB_INVALID_THREAD_ID, eBroadcastAlways);
Caroline Tice0aa2e552011-01-14 00:29:16 +0000195}
196
Greg Clayton63094e02010-06-23 01:19:29 +0000197ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) :
Greg Clayton238c0a12010-09-18 01:14:36 +0000198 ScriptInterpreter (interpreter, eScriptLanguagePython),
Caroline Tice0aa2e552011-01-14 00:29:16 +0000199 m_embedded_python_pty (),
200 m_embedded_thread_input_reader_sp (),
Greg Clayton58928562011-02-09 01:08:52 +0000201 m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()),
Caroline Tice0aa2e552011-01-14 00:29:16 +0000202 m_new_sysout (NULL),
203 m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()),
Greg Clayton0fdd4a02011-02-07 23:24:47 +0000204 m_terminal_state (),
Caroline Tice0aa2e552011-01-14 00:29:16 +0000205 m_session_is_active (false),
206 m_pty_slave_is_open (false),
207 m_valid_session (true)
Chris Lattner24943d22010-06-08 16:52:24 +0000208{
209
Greg Clayton7c330d62011-01-27 01:01:10 +0000210 static int g_initialized = false;
211
212 if (!g_initialized)
213 {
214 g_initialized = true;
215 ScriptInterpreterPython::Initialize ();
216 }
217
Caroline Tice202f6b82011-01-17 21:55:19 +0000218 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 Tice0aa2e552011-01-14 00:29:16 +0000244 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 Tice5867f6b2010-10-18 18:24:17 +0000248
Caroline Tice0aa2e552011-01-14 00:29:16 +0000249 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 Tice5867f6b2010-10-18 18:24:17 +0000260
Caroline Tice0aa2e552011-01-14 00:29:16 +0000261 int old_count = Debugger::TestDebuggerRefCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000262
Caroline Tice0aa2e552011-01-14 00:29:16 +0000263 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 Clayton24b48ff2010-10-17 22:03:32 +0000266
Caroline Tice0aa2e552011-01-14 00:29:16 +0000267 int new_count = Debugger::TestDebuggerRefCount();
Chris Lattner24943d22010-06-08 16:52:24 +0000268
Caroline Tice0aa2e552011-01-14 00:29:16 +0000269 if (new_count > old_count)
270 Debugger::Terminate();
Caroline Tice5867f6b2010-10-18 18:24:17 +0000271
Caroline Tice0aa2e552011-01-14 00:29:16 +0000272 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 Lattner24943d22010-06-08 16:52:24 +0000282 {
Caroline Tice0aa2e552011-01-14 00:29:16 +0000283 m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush);
Caroline Tice5867f6b2010-10-18 18:24:17 +0000284 }
Caroline Tice202f6b82011-01-17 21:55:19 +0000285
286 if (need_to_release_lock)
287 ReleasePythonLock();
Chris Lattner24943d22010-06-08 16:52:24 +0000288}
289
290ScriptInterpreterPython::~ScriptInterpreterPython ()
291{
Caroline Tice0aa2e552011-01-14 00:29:16 +0000292 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 Tice202f6b82011-01-17 21:55:19 +0000306 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 Tice0aa2e552011-01-14 00:29:16 +0000316 }
Chris Lattner24943d22010-06-08 16:52:24 +0000317}
318
Caroline Tice0aa2e552011-01-14 00:29:16 +0000319void
320ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh)
321{
322 if (fh == NULL)
323 return;
324
325 m_dbg_stdout = fh;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000326
Caroline Tice202f6b82011-01-17 21:55:19 +0000327 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 Tice0aa2e552011-01-14 00:29:16 +0000343}
344
345void
Greg Clayton0fdd4a02011-02-07 23:24:47 +0000346ScriptInterpreterPython::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
355void
356ScriptInterpreterPython::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
367void
Caroline Tice0aa2e552011-01-14 00:29:16 +0000368ScriptInterpreterPython::LeaveSession ()
369{
370 m_session_is_active = false;
371}
372
373void
374ScriptInterpreterPython::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 Tice202f6b82011-01-17 21:55:19 +0000384 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 Tice0aa2e552011-01-14 00:29:16 +0000390
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 Tice0aa2e552011-01-14 00:29:16 +0000402 if (!m_pty_slave_is_open)
403 {
Caroline Tice202f6b82011-01-17 21:55:19 +0000404 run_string.Clear();
Caroline Tice0aa2e552011-01-14 00:29:16 +0000405 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 Chen60dde642010-07-30 22:33:14 +0000417bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000418ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result)
Chris Lattner24943d22010-06-08 16:52:24 +0000419{
Caroline Tice0aa2e552011-01-14 00:29:16 +0000420 if (!m_valid_session)
421 return false;
422
Caroline Tice0aa2e552011-01-14 00:29:16 +0000423
Caroline Tice0aa2e552011-01-14 00:29:16 +0000424
Caroline Tice4a461da2011-01-14 21:09:29 +0000425 // 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 Tice202f6b82011-01-17 21:55:19 +0000431 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 Tice4a461da2011-01-14 21:09:29 +0000443 bool success = false;
444
Greg Clayton63094e02010-06-23 01:19:29 +0000445 if (command)
Chris Lattner24943d22010-06-08 16:52:24 +0000446 {
Caroline Tice4a461da2011-01-14 21:09:29 +0000447 // 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 Clayton63094e02010-06-23 01:19:29 +0000525
Caroline Tice0aa2e552011-01-14 00:29:16 +0000526 LeaveSession ();
527
Caroline Tice202f6b82011-01-17 21:55:19 +0000528 if (need_to_release_lock)
529 ReleasePythonLock();
530
Caroline Tice4a461da2011-01-14 21:09:29 +0000531 if (success)
Johnny Chen60dde642010-07-30 22:33:14 +0000532 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 Lattner24943d22010-06-08 16:52:24 +0000538 }
Johnny Chen60dde642010-07-30 22:33:14 +0000539
Caroline Tice0aa2e552011-01-14 00:29:16 +0000540 LeaveSession ();
Caroline Tice202f6b82011-01-17 21:55:19 +0000541
542 if (need_to_release_lock)
543 ReleasePythonLock ();
544
Johnny Chen60dde642010-07-30 22:33:14 +0000545 if (result)
546 result->AppendError ("empty command passed to python\n");
547 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000548}
549
550
551
552size_t
553ScriptInterpreterPython::InputReaderCallback
554(
555 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +0000556 InputReader &reader,
Greg Clayton5144f382010-10-07 17:14:24 +0000557 InputReaderAction notification,
Chris Lattner24943d22010-06-08 16:52:24 +0000558 const char *bytes,
559 size_t bytes_len
560)
561{
Caroline Tice2ade6112010-11-10 19:18:14 +0000562 lldb::thread_t embedded_interpreter_thread;
563 LogSP log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
564
Chris Lattner24943d22010-06-08 16:52:24 +0000565 if (baton == NULL)
566 return 0;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000567
568 ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
569
570 if (script_interpreter->m_script_lang != eScriptLanguagePython)
571 return 0;
572
Greg Clayton58928562011-02-09 01:08:52 +0000573 File &out_file = reader.GetDebugger().GetOutputFile();
Caroline Tice67637d82010-12-15 19:51:12 +0000574
Chris Lattner24943d22010-06-08 16:52:24 +0000575 switch (notification)
576 {
577 case eInputReaderActivate:
578 {
Greg Clayton58928562011-02-09 01:08:52 +0000579 out_file.Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n");
580
Chris Lattner24943d22010-06-08 16:52:24 +0000581 // Save terminal settings if we can
Greg Clayton58928562011-02-09 01:08:52 +0000582 int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor();
583 if (input_fd == File::kInvalidDescriptor)
Greg Clayton24b48ff2010-10-17 22:03:32 +0000584 input_fd = STDIN_FILENO;
Caroline Ticec95c6d12010-09-14 22:49:06 +0000585
Greg Clayton0fdd4a02011-02-07 23:24:47 +0000586 script_interpreter->SaveTerminalState(input_fd);
Greg Clayton99208582011-02-07 19:04:58 +0000587
Caroline Tice202f6b82011-01-17 21:55:19 +0000588 if (!CurrentThreadHasPythonLock())
589 {
590 while (!GetPythonLock(1))
591 {
Greg Clayton58928562011-02-09 01:08:52 +0000592 out_file.Printf ("Python interpreter locked on another thread; waiting to acquire lock...\n");
Caroline Tice202f6b82011-01-17 21:55:19 +0000593 }
594 script_interpreter->EnterSession ();
595 ReleasePythonLock();
596 }
597 else
598 script_interpreter->EnterSession ();
599
Caroline Tice2ade6112010-11-10 19:18:14 +0000600 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 Clayton09c81ef2011-02-08 01:34:25 +0000610 if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread))
Caroline Tice2ade6112010-11-10 19:18:14 +0000611 {
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 Lattner24943d22010-06-08 16:52:24 +0000630 }
631 break;
632
633 case eInputReaderDeactivate:
Caroline Tice0aa2e552011-01-14 00:29:16 +0000634 script_interpreter->LeaveSession ();
Chris Lattner24943d22010-06-08 16:52:24 +0000635 break;
636
637 case eInputReaderReactivate:
Caroline Tice202f6b82011-01-17 21:55:19 +0000638 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 Lattner24943d22010-06-08 16:52:24 +0000649 break;
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000650
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 Lattner24943d22010-06-08 16:52:24 +0000658
659 case eInputReaderGotToken:
Caroline Tice2ade6112010-11-10 19:18:14 +0000660 if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1)
Chris Lattner24943d22010-06-08 16:52:24 +0000661 {
Caroline Tice2ade6112010-11-10 19:18:14 +0000662 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 Lattner24943d22010-06-08 16:52:24 +0000673 }
Caroline Tice2ade6112010-11-10 19:18:14 +0000674 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 Lattner24943d22010-06-08 16:52:24 +0000683 break;
684
685 case eInputReaderDone:
Caroline Tice0aa2e552011-01-14 00:29:16 +0000686 script_interpreter->LeaveSession ();
687
Chris Lattner24943d22010-06-08 16:52:24 +0000688 // Restore terminal settings if they were validly saved
Caroline Tice2ade6112010-11-10 19:18:14 +0000689 if (log)
690 log->Printf ("ScriptInterpreterPython::InputReaderCallback, Done, closing down input reader.");
Caroline Ticec95c6d12010-09-14 22:49:06 +0000691
Greg Clayton0fdd4a02011-02-07 23:24:47 +0000692 script_interpreter->RestoreTerminalState ();
693
Caroline Tice2ade6112010-11-10 19:18:14 +0000694 script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor();
Chris Lattner24943d22010-06-08 16:52:24 +0000695 break;
696 }
697
698 return bytes_len;
699}
700
701
702void
Greg Clayton238c0a12010-09-18 01:14:36 +0000703ScriptInterpreterPython::ExecuteInterpreterLoop ()
Chris Lattner24943d22010-06-08 16:52:24 +0000704{
705 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
706
Caroline Tice0aa2e552011-01-14 00:29:16 +0000707 Debugger &debugger = GetCommandInterpreter().GetDebugger();
Caroline Ticec95c6d12010-09-14 22:49:06 +0000708
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 Clayton58928562011-02-09 01:08:52 +0000714 if (!debugger.GetInputFile().IsValid())
Caroline Ticec95c6d12010-09-14 22:49:06 +0000715 return;
716
Greg Clayton63094e02010-06-23 01:19:29 +0000717 InputReaderSP reader_sp (new InputReader(debugger));
Chris Lattner24943d22010-06-08 16:52:24 +0000718 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 Clayton63094e02010-06-23 01:19:29 +0000729 debugger.PushInputReader (reader_sp);
Caroline Tice2ade6112010-11-10 19:18:14 +0000730 m_embedded_thread_input_reader_sp = reader_sp;
Chris Lattner24943d22010-06-08 16:52:24 +0000731 }
732 }
733}
734
735bool
736ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
737 ScriptInterpreter::ReturnType return_type,
738 void *ret_value)
739{
Caroline Tice0aa2e552011-01-14 00:29:16 +0000740
Caroline Tice202f6b82011-01-17 21:55:19 +0000741 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 Tice0aa2e552011-01-14 00:29:16 +0000753
Chris Lattner24943d22010-06-08 16:52:24 +0000754 PyObject *py_return = NULL;
755 PyObject *mainmod = PyImport_AddModule ("__main__");
756 PyObject *globals = PyModule_GetDict (mainmod);
Caroline Tice0aa2e552011-01-14 00:29:16 +0000757 PyObject *locals = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000758 PyObject *py_error = NULL;
759 bool ret_success;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000760 bool should_decrement_locals = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000761 int success;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000762
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 Lattner24943d22010-06-08 16:52:24 +0000781
Caroline Tice0aa2e552011-01-14 00:29:16 +0000782 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 Lattner24943d22010-06-08 16:52:24 +0000798 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 Tice0aa2e552011-01-14 00:29:16 +0000810 if (locals != NULL
811 && should_decrement_locals)
812 Py_DECREF (locals);
813
Chris Lattner24943d22010-06-08 16:52:24 +0000814 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 Tice0aa2e552011-01-14 00:29:16 +0000915
916 LeaveSession ();
Chris Lattner24943d22010-06-08 16:52:24 +0000917
Caroline Tice202f6b82011-01-17 21:55:19 +0000918 if (need_to_release_lock)
919 ReleasePythonLock();
920
Chris Lattner24943d22010-06-08 16:52:24 +0000921 return ret_success;
922}
923
924bool
925ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string)
926{
Caroline Tice202f6b82011-01-17 21:55:19 +0000927 FILE *tmp_fh = (m_dbg_stdout ? m_dbg_stdout : stdout);
928 bool need_to_release_lock = true;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000929
Caroline Tice202f6b82011-01-17 21:55:19 +0000930 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 Tice0aa2e552011-01-14 00:29:16 +0000939
Chris Lattner24943d22010-06-08 16:52:24 +0000940 bool success = false;
941 PyObject *py_return = NULL;
942 PyObject *mainmod = PyImport_AddModule ("__main__");
943 PyObject *globals = PyModule_GetDict (mainmod);
Caroline Tice0aa2e552011-01-14 00:29:16 +0000944 PyObject *locals = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000945 PyObject *py_error = NULL;
Caroline Tice0aa2e552011-01-14 00:29:16 +0000946 bool should_decrement_locals = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000947
Caroline Tice0aa2e552011-01-14 00:29:16 +0000948 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 Lattner24943d22010-06-08 16:52:24 +0000980 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 Tice0aa2e552011-01-14 00:29:16 +0000994 if (locals && should_decrement_locals)
995 Py_DECREF (locals);
Chris Lattner24943d22010-06-08 16:52:24 +0000996 }
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 Tice0aa2e552011-01-14 00:29:16 +00001009 LeaveSession ();
1010
Caroline Tice202f6b82011-01-17 21:55:19 +00001011 if (need_to_release_lock)
1012 ReleasePythonLock();
1013
Chris Lattner24943d22010-06-08 16:52:24 +00001014 return success;
1015}
1016
1017static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.";
1018
1019size_t
1020ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback
1021(
1022 void *baton,
Greg Clayton63094e02010-06-23 01:19:29 +00001023 InputReader &reader,
Greg Clayton5144f382010-10-07 17:14:24 +00001024 InputReaderAction notification,
Chris Lattner24943d22010-06-08 16:52:24 +00001025 const char *bytes,
1026 size_t bytes_len
1027)
1028{
1029 static StringList commands_in_progress;
1030
Greg Clayton58928562011-02-09 01:08:52 +00001031 File &out_file = reader.GetDebugger().GetOutputFile();
Caroline Ticec95c6d12010-09-14 22:49:06 +00001032
Chris Lattner24943d22010-06-08 16:52:24 +00001033 switch (notification)
1034 {
1035 case eInputReaderActivate:
1036 {
1037 commands_in_progress.Clear();
Greg Clayton58928562011-02-09 01:08:52 +00001038 if (out_file.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +00001039 {
Greg Clayton58928562011-02-09 01:08:52 +00001040 out_file.Printf ("%s\n", g_reader_instructions);
Greg Clayton63094e02010-06-23 01:19:29 +00001041 if (reader.GetPrompt())
Greg Clayton58928562011-02-09 01:08:52 +00001042 out_file.Printf ("%s", reader.GetPrompt());
1043 out_file.Flush ();
Chris Lattner24943d22010-06-08 16:52:24 +00001044 }
1045 }
1046 break;
1047
1048 case eInputReaderDeactivate:
1049 break;
1050
1051 case eInputReaderReactivate:
Greg Clayton58928562011-02-09 01:08:52 +00001052 if (reader.GetPrompt() && out_file.IsValid())
Caroline Ticef81b4c52010-10-27 18:34:42 +00001053 {
Greg Clayton58928562011-02-09 01:08:52 +00001054 out_file.Printf ("%s", reader.GetPrompt());
1055 out_file.Flush ();
Caroline Ticef81b4c52010-10-27 18:34:42 +00001056 }
Chris Lattner24943d22010-06-08 16:52:24 +00001057 break;
1058
1059 case eInputReaderGotToken:
1060 {
1061 std::string temp_string (bytes, bytes_len);
1062 commands_in_progress.AppendString (temp_string.c_str());
Greg Clayton58928562011-02-09 01:08:52 +00001063 if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt())
Caroline Ticef81b4c52010-10-27 18:34:42 +00001064 {
Greg Clayton58928562011-02-09 01:08:52 +00001065 out_file.Printf ("%s", reader.GetPrompt());
1066 out_file.Flush ();
Caroline Ticef81b4c52010-10-27 18:34:42 +00001067 }
Chris Lattner24943d22010-06-08 16:52:24 +00001068 }
1069 break;
1070
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001071 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 Lattner24943d22010-06-08 16:52:24 +00001082 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 Clayton63094e02010-06-23 01:19:29 +00001089 ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
Chris Lattner24943d22010-06-08 16:52:24 +00001090 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 Ticeb447e842010-09-21 19:25:28 +00001101 else
Greg Clayton58928562011-02-09 01:08:52 +00001102 out_file.Printf ("Warning: No command attached to breakpoint.\n");
Chris Lattner24943d22010-06-08 16:52:24 +00001103 }
1104 else
1105 {
1106 // FIXME: Error processing.
1107 }
1108 }
1109 }
1110 break;
1111
1112 }
1113
1114 return bytes_len;
1115}
1116
1117void
Greg Clayton238c0a12010-09-18 01:14:36 +00001118ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
Chris Lattner24943d22010-06-08 16:52:24 +00001119 CommandReturnObject &result)
1120{
Caroline Tice0aa2e552011-01-14 00:29:16 +00001121 Debugger &debugger = GetCommandInterpreter().GetDebugger();
1122
Greg Clayton63094e02010-06-23 01:19:29 +00001123 InputReaderSP reader_sp (new InputReader (debugger));
Chris Lattner24943d22010-06-08 16:52:24 +00001124
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 Clayton63094e02010-06-23 01:19:29 +00001136 debugger.PushInputReader (reader_sp);
Chris Lattner24943d22010-06-08 16:52:24 +00001137 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 Chen3e0571b2010-09-11 00:23:59 +00001150// Set a Python one-liner as the callback for the breakpoint.
Johnny Chend1c2dca2010-09-10 18:21:10 +00001151void
Greg Clayton238c0a12010-09-18 01:14:36 +00001152ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
Johnny Chend1c2dca2010-09-10 18:21:10 +00001153 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 Tice5136f942010-09-27 21:35:15 +00001160
Johnny Chend1c2dca2010-09-10 18:21:10 +00001161 data_ap->user_source.AppendString (oneliner);
Johnny Chend1c2dca2010-09-10 18:21:10 +00001162
Caroline Tice5136f942010-09-27 21:35:15 +00001163 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 Chend1c2dca2010-09-10 18:21:10 +00001172 return;
1173}
1174
Chris Lattner24943d22010-06-08 16:52:24 +00001175bool
1176ScriptInterpreterPython::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
1194bool
1195ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, StringList &callback_data)
1196{
1197 static int num_created_functions = 0;
Chris Lattner24943d22010-06-08 16:52:24 +00001198 user_input.RemoveBlankLines ();
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001199 int num_lines = user_input.GetSize ();
1200 StreamString sstr;
Chris Lattner24943d22010-06-08 16:52:24 +00001201
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001202 // Check to see if we have any data; if not, just return.
1203 if (user_input.GetSize() == 0)
1204 return false;
1205
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001206 // 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 Ticeb447e842010-09-21 19:25:28 +00001208
Caroline Ticeb447e842010-09-21 19:25:28 +00001209
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001210 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 Ticeb447e842010-09-21 19:25:28 +00001213
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001214 sstr.Clear();
Caroline Ticeb447e842010-09-21 19:25:28 +00001215 StringList auto_generated_function;
Caroline Ticeb447e842010-09-21 19:25:28 +00001216
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001217 // Create the function name & definition string.
1218
Caroline Tice0aa2e552011-01-14 00:29:16 +00001219 sstr.Printf ("def %s (frame, bp_loc, dict):", auto_generated_function_name.c_str());
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001220 auto_generated_function.AppendString (sstr.GetData());
Caroline Tice0aa2e552011-01-14 00:29:16 +00001221
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 Tice59c5d5d2010-09-27 18:00:20 +00001229
1230 // Wrap everything up inside the function, increasing the indentation.
Chris Lattner24943d22010-06-08 16:52:24 +00001231
1232 for (int i = 0; i < num_lines; ++i)
1233 {
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001234 sstr.Clear ();
1235 sstr.Printf (" %s", user_input.GetStringAtIndex (i));
1236 auto_generated_function.AppendString (sstr.GetData());
Caroline Ticeb447e842010-09-21 19:25:28 +00001237 }
Chris Lattner24943d22010-06-08 16:52:24 +00001238
Caroline Tice0aa2e552011-01-14 00:29:16 +00001239 // 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 Tice59c5d5d2010-09-27 18:00:20 +00001247 // Verify that the results are valid Python.
Chris Lattner24943d22010-06-08 16:52:24 +00001248
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001249 if (!ExportFunctionDefinitionToInterpreter (auto_generated_function))
Caroline Ticeb447e842010-09-21 19:25:28 +00001250 {
Caroline Ticeb447e842010-09-21 19:25:28 +00001251 return false;
Chris Lattner24943d22010-06-08 16:52:24 +00001252 }
Caroline Tice59c5d5d2010-09-27 18:00:20 +00001253
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 Lattner24943d22010-06-08 16:52:24 +00001258}
1259
Greg Clayton5144f382010-10-07 17:14:24 +00001260bool
1261ScriptInterpreterPython::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 Tice0aa2e552011-01-14 00:29:16 +00001271
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 Clayton5144f382010-10-07 17:14:24 +00001286
1287 if (python_function_name != NULL
1288 && python_function_name[0] != '\0')
1289 {
1290 Thread *thread = context->exe_ctx.thread;
Greg Clayton5144f382010-10-07 17:14:24 +00001291 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 Tice0aa2e552011-01-14 00:29:16 +00001299 {
Caroline Tice202f6b82011-01-17 21:55:19 +00001300 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 Tice0aa2e552011-01-14 00:29:16 +00001322 return ret_val;
1323 }
Greg Clayton5144f382010-10-07 17:14:24 +00001324 }
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 Tice2ade6112010-11-10 19:18:14 +00001329
1330lldb::thread_result_t
1331ScriptInterpreterPython::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 Tice202f6b82011-01-17 21:55:19 +00001342 bool need_to_release_lock = true;
1343 bool safe_to_run = false;
Caroline Tice0aa2e552011-01-14 00:29:16 +00001344
Caroline Tice202f6b82011-01-17 21:55:19 +00001345 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 Tice2ade6112010-11-10 19:18:14 +00001363 StreamString run_string;
Caroline Tice2ade6112010-11-10 19:18:14 +00001364
Caroline Tice202f6b82011-01-17 21:55:19 +00001365 script_interpreter->EnterSession ();
1366
Caroline Tice0aa2e552011-01-14 00:29:16 +00001367 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 Tice2ade6112010-11-10 19:18:14 +00001384 // The following call drops into the embedded interpreter loop and stays there until the
1385 // user chooses to exit from the Python interpreter.
Caroline Tice0aa2e552011-01-14 00:29:16 +00001386
Caroline Ticece207c12011-03-11 00:21:55 +00001387 // When in the embedded interpreter, the user can call arbitrary system and Python stuff, which may require
1388 // the ability to run multi-threaded stuff, so we need to surround the call the the embedded interpreter with
1389 // calls to Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS.
1390
1391 // We ALSO need to surround the call to the embedded interpreter with calls to PyGILState_Ensure and
1392 // PyGILState_Release. This is because this embedded interpreter is being run on a DIFFERENT THREAD than
1393 // the thread on which the call to Py_Initialize (and PyEval_InitThreads) was called. Those initializations
1394 // called PyGILState_Ensure on *that* thread, but it also needs to be called on *this* thread. Otherwise,
1395 // if the user calls Python code that does threading stuff, the interpreter state will be off, and things could
1396 // hang (it's happened before).
1397
Caroline Tice9d352ce2011-03-07 23:24:28 +00001398 Py_BEGIN_ALLOW_THREADS
1399 PyGILState_STATE gstate = PyGILState_Ensure();
1400
Caroline Tice0aa2e552011-01-14 00:29:16 +00001401 run_string.Printf ("run_python_interpreter (%s)", script_interpreter->m_dictionary_name.c_str());
1402 PyRun_SimpleString (run_string.GetData());
1403 run_string.Clear ();
Caroline Tice2ade6112010-11-10 19:18:14 +00001404
Caroline Tice9d352ce2011-03-07 23:24:28 +00001405 PyGILState_Release (gstate);
1406 Py_END_ALLOW_THREADS
1407
Caroline Tice0aa2e552011-01-14 00:29:16 +00001408 run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str());
1409 PyRun_SimpleString (run_string.GetData());
1410 run_string.Clear();
1411
1412 run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str());
1413 PyRun_SimpleString (run_string.GetData());
1414 run_string.Clear();
Caroline Tice202f6b82011-01-17 21:55:19 +00001415
1416 script_interpreter->LeaveSession ();
1417
Caroline Tice2ade6112010-11-10 19:18:14 +00001418 }
1419
Caroline Tice202f6b82011-01-17 21:55:19 +00001420 if (!safe_to_run)
1421 fprintf ((script_interpreter->m_dbg_stdout ? script_interpreter->m_dbg_stdout : stdout),
1422 "Python interpreter locked on another thread; unable to acquire lock.\n");
1423
1424 if (need_to_release_lock)
1425 ReleasePythonLock ();
1426
Caroline Tice2ade6112010-11-10 19:18:14 +00001427 if (script_interpreter->m_embedded_thread_input_reader_sp)
1428 script_interpreter->m_embedded_thread_input_reader_sp->SetIsDone (true);
1429
1430 script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor();
Caroline Tice0aa2e552011-01-14 00:29:16 +00001431
1432 script_interpreter->m_pty_slave_is_open = false;
Caroline Tice2ade6112010-11-10 19:18:14 +00001433
1434 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT);
1435 if (log)
1436 log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread exiting...", baton);
1437
1438
Caroline Tice0aa2e552011-01-14 00:29:16 +00001439 // Clean up the input reader and make the debugger pop it off the stack.
1440 Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger();
Caroline Tice2ade6112010-11-10 19:18:14 +00001441 const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp;
1442 script_interpreter->m_embedded_thread_input_reader_sp.reset();
1443 debugger.PopInputReader (reader_sp);
Caroline Tice0aa2e552011-01-14 00:29:16 +00001444
Caroline Tice2ade6112010-11-10 19:18:14 +00001445 return NULL;
1446}
1447
1448
Caroline Tice0aa2e552011-01-14 00:29:16 +00001449void
1450ScriptInterpreterPython::Initialize ()
1451{
Caroline Tice0aa2e552011-01-14 00:29:16 +00001452 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
1453
Greg Clayton0fdd4a02011-02-07 23:24:47 +00001454 // Python will muck with STDIN terminal state, so save off any current TTY
1455 // settings so we can restore them.
1456 TerminalState stdin_tty_state;
1457 stdin_tty_state.Save(STDIN_FILENO, false);
Caroline Tice0aa2e552011-01-14 00:29:16 +00001458
1459 // Find the module that owns this code and use that path we get to
1460 // set the PYTHONPATH appropriately.
1461
1462 FileSpec file_spec;
1463 char python_dir_path[PATH_MAX];
1464 if (Host::GetLLDBPath (ePathTypePythonDir, file_spec))
1465 {
1466 std::string python_path;
1467 const char *curr_python_path = ::getenv ("PYTHONPATH");
1468 if (curr_python_path)
1469 {
1470 // We have a current value for PYTHONPATH, so lets append to it
1471 python_path.append (curr_python_path);
1472 }
1473
1474 if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
1475 {
1476 if (!python_path.empty())
1477 python_path.append (1, ':');
1478 python_path.append (python_dir_path);
1479 }
1480
1481 if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, file_spec))
1482 {
1483 if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
1484 {
1485 if (!python_path.empty())
1486 python_path.append (1, ':');
1487 python_path.append (python_dir_path);
1488 }
1489 }
1490 const char *pathon_path_env_cstr = python_path.c_str();
1491 ::setenv ("PYTHONPATH", pathon_path_env_cstr, 1);
1492 }
1493
Caroline Tice9d352ce2011-03-07 23:24:28 +00001494 PyEval_InitThreads ();
Caroline Tice0aa2e552011-01-14 00:29:16 +00001495 Py_Initialize ();
1496
1497 PyObject *compiled_module = Py_CompileString (embedded_interpreter_string,
1498 "embedded_interpreter.py",
1499 Py_file_input);
1500
1501 PyObject *py_error = PyErr_Occurred ();
1502 if (py_error != NULL)
1503 {
1504 PyErr_Print();
1505 PyErr_Clear();
1506 }
1507
1508
1509 // This function is in the C++ output file generated by SWIG after it is
1510 // run on all of the headers in "lldb/API/SB*.h"
1511 init_lldb ();
1512
1513 // Update the path python uses to search for modules to include the current directory.
1514
1515 int success = PyRun_SimpleString ("import sys");
1516 success = PyRun_SimpleString ("sys.path.append ('.')");
1517
1518 PyObject *pmod = NULL;
1519
1520 if (compiled_module)
1521 {
1522 pmod = PyImport_ExecCodeModule (const_cast<char*> ("embedded_interpreter"),
1523 compiled_module);
1524 Py_DECREF (compiled_module);
1525 }
1526
1527 if (pmod != NULL)
1528 {
1529 PyRun_SimpleString ("from embedded_interpreter import run_python_interpreter");
1530 PyRun_SimpleString ("from embedded_interpreter import run_one_line");
1531 PyRun_SimpleString ("import sys");
1532 PyRun_SimpleString ("from termios import *");
1533 Py_DECREF (pmod);
1534 }
Greg Clayton99208582011-02-07 19:04:58 +00001535
Greg Clayton0fdd4a02011-02-07 23:24:47 +00001536 stdin_tty_state.Restore();
Caroline Tice0aa2e552011-01-14 00:29:16 +00001537}
1538
1539void
1540ScriptInterpreterPython::Terminate ()
1541{
1542 // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it). Calling
1543 // Py_Finalize here causes test suite runs to seg fault: The test suite runs in Python. It registers
1544 // SBDebugger::Terminate to be called 'at_exit'. When the test suite Python harness finishes up, it calls
1545 // Py_Finalize, which calls all the 'at_exit' registered functions. SBDebugger::Terminate calls Debugger::Terminate,
1546 // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls
1547 // ScriptInterpreterPython::Terminate. So if we call Py_Finalize here, we end up with Py_Finalize being called from
1548 // within Py_Finalize, which results in a seg fault.
1549 //
1550 // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't
1551 // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the
1552 // process exits).
1553 //
1554// Py_Finalize ();
1555}