ScriptInterpreterPython.cpp revision 952e9dc874944fcdbbb224f3ec4fc2c859376f64
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
11// the *FIRST* header file included here.
12#ifdef LLDB_DISABLE_PYTHON
13
14// Python is disabled in this build
15
16#else
17
18#if defined (__APPLE__)
19#include <Python/Python.h>
20#else
21#include <Python.h>
22#endif
23
24#include "lldb/Interpreter/ScriptInterpreterPython.h"
25
26#include <stdlib.h>
27#include <stdio.h>
28
29#include <string>
30
31#include "lldb/API/SBValue.h"
32#include "lldb/Breakpoint/BreakpointLocation.h"
33#include "lldb/Breakpoint/StoppointCallbackContext.h"
34#include "lldb/Breakpoint/WatchpointOptions.h"
35#include "lldb/Core/Debugger.h"
36#include "lldb/Core/Timer.h"
37#include "lldb/Host/Host.h"
38#include "lldb/Interpreter/CommandInterpreter.h"
39#include "lldb/Interpreter/CommandReturnObject.h"
40#include "lldb/Target/Thread.h"
41
42using namespace lldb;
43using namespace lldb_private;
44
45
46static ScriptInterpreter::SWIGInitCallback g_swig_init_callback = NULL;
47static ScriptInterpreter::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = NULL;
48static ScriptInterpreter::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = NULL;
49static ScriptInterpreter::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = NULL;
50static ScriptInterpreter::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = NULL;
51static ScriptInterpreter::SWIGPythonCalculateNumChildren g_swig_calc_children = NULL;
52static ScriptInterpreter::SWIGPythonGetChildAtIndex g_swig_get_child_index = NULL;
53static ScriptInterpreter::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = NULL;
54static ScriptInterpreter::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue  = NULL;
55static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = NULL;
56static ScriptInterpreter::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = NULL;
57static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = NULL;
58static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = NULL;
59static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = NULL;
60
61// these are the Pythonic implementations of the required callbacks
62// these are scripting-language specific, which is why they belong here
63// we still need to use function pointers to them instead of relying
64// on linkage-time resolution because the SWIG stuff and this file
65// get built at different times
66extern "C" bool
67LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
68                                          const char *session_dictionary_name,
69                                          const lldb::StackFrameSP& sb_frame,
70                                          const lldb::BreakpointLocationSP& sb_bp_loc);
71
72extern "C" bool
73LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
74                                          const char *session_dictionary_name,
75                                          const lldb::StackFrameSP& sb_frame,
76                                          const lldb::WatchpointSP& sb_wp);
77
78extern "C" bool
79LLDBSwigPythonCallTypeScript (const char *python_function_name,
80                              void *session_dictionary,
81                              const lldb::ValueObjectSP& valobj_sp,
82                              void** pyfunct_wrapper,
83                              std::string& retval);
84
85extern "C" void*
86LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
87                                       const char *session_dictionary_name,
88                                       const lldb::ValueObjectSP& valobj_sp);
89
90
91extern "C" uint32_t
92LLDBSwigPython_CalculateNumChildren (void *implementor);
93
94extern "C" void *
95LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
96
97extern "C" int
98LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
99
100extern "C" void *
101LLDBSWIGPython_CastPyObjectToSBValue (void* data);
102
103extern "C" bool
104LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
105
106extern "C" bool
107LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
108
109extern "C" bool
110LLDBSwigPythonCallCommand (const char *python_function_name,
111                           const char *session_dictionary_name,
112                           lldb::DebuggerSP& debugger,
113                           const char* args,
114                           std::string& err_msg,
115                           lldb_private::CommandReturnObject& cmd_retobj);
116
117extern "C" bool
118LLDBSwigPythonCallModuleInit (const char *python_module_name,
119                              const char *session_dictionary_name,
120                              lldb::DebuggerSP& debugger);
121
122extern "C" void*
123LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
124                              const char *session_dictionary_name,
125                              const lldb::ProcessSP& process_sp);
126
127static int
128_check_and_flush (FILE *stream)
129{
130  int prev_fail = ferror (stream);
131  return fflush (stream) || prev_fail ? EOF : 0;
132}
133
134ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter,
135                                         uint16_t on_entry,
136                                         uint16_t on_leave,
137                                         FILE* wait_msg_handle) :
138    ScriptInterpreterLocker (),
139    m_teardown_session( (on_leave & TearDownSession) == TearDownSession ),
140    m_python_interpreter(py_interpreter),
141    m_tmp_fh(wait_msg_handle)
142{
143    if (m_python_interpreter && !m_tmp_fh)
144        m_tmp_fh = (m_python_interpreter->m_dbg_stdout ? m_python_interpreter->m_dbg_stdout : stdout);
145
146    DoAcquireLock();
147    if ((on_entry & InitSession) == InitSession)
148    {
149        if (DoInitSession((on_entry & InitGlobals) == InitGlobals) == false)
150        {
151            // Don't teardown the session if we didn't init it.
152            m_teardown_session = false;
153        }
154    }
155}
156
157bool
158ScriptInterpreterPython::Locker::DoAcquireLock()
159{
160    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
161    m_GILState = PyGILState_Ensure();
162    if (log)
163        log->Printf("Ensured PyGILState. Previous state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
164    return true;
165}
166
167bool
168ScriptInterpreterPython::Locker::DoInitSession(bool init_lldb_globals)
169{
170    if (!m_python_interpreter)
171        return false;
172    return m_python_interpreter->EnterSession (init_lldb_globals);
173}
174
175bool
176ScriptInterpreterPython::Locker::DoFreeLock()
177{
178    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
179    if (log)
180        log->Printf("Releasing PyGILState. Returning to state = %slocked\n", m_GILState == PyGILState_UNLOCKED ? "un" : "");
181    PyGILState_Release(m_GILState);
182    return true;
183}
184
185bool
186ScriptInterpreterPython::Locker::DoTearDownSession()
187{
188    if (!m_python_interpreter)
189        return false;
190    m_python_interpreter->LeaveSession ();
191    return true;
192}
193
194ScriptInterpreterPython::Locker::~Locker()
195{
196    if (m_teardown_session)
197        DoTearDownSession();
198    DoFreeLock();
199}
200
201ScriptInterpreterPython::PythonInputReaderManager::PythonInputReaderManager (ScriptInterpreterPython *interpreter) :
202m_interpreter(interpreter),
203m_debugger_sp(),
204m_reader_sp(),
205m_error(false)
206{
207    if (m_interpreter == NULL)
208    {
209        m_error = true;
210        return;
211    }
212
213    m_debugger_sp = m_interpreter->GetCommandInterpreter().GetDebugger().shared_from_this();
214
215    if (!m_debugger_sp)
216    {
217        m_error = true;
218        return;
219    }
220
221    m_reader_sp = InputReaderSP(new InputReader(*m_debugger_sp.get()));
222
223    if (!m_reader_sp)
224    {
225        m_error = true;
226        return;
227    }
228
229    Error error (m_reader_sp->Initialize (ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback,
230                                          m_interpreter,                // baton
231                                          eInputReaderGranularityLine,  // token size, to pass to callback function
232                                          NULL,                         // end token
233                                          NULL,                         // prompt
234                                          true));                       // echo input
235    if (error.Fail())
236        m_error = true;
237    else
238    {
239        m_debugger_sp->PushInputReader (m_reader_sp);
240        m_interpreter->m_embedded_thread_input_reader_sp = m_reader_sp;
241    }
242}
243
244ScriptInterpreterPython::PythonInputReaderManager::~PythonInputReaderManager()
245{
246    // Nothing to do if either m_interpreter or m_reader_sp is invalid.
247    if (!m_interpreter || !m_reader_sp)
248        return;
249
250    m_reader_sp->SetIsDone (true);
251    if (m_debugger_sp)
252        m_debugger_sp->PopInputReader(m_reader_sp);
253
254    // Only mess with m_interpreter's counterpart if, indeed, they are the same object.
255    if (m_reader_sp.get() == m_interpreter->m_embedded_thread_input_reader_sp.get())
256    {
257        m_interpreter->m_embedded_thread_pty.CloseSlaveFileDescriptor();
258        m_interpreter->m_embedded_thread_input_reader_sp.reset();
259    }
260}
261
262size_t
263ScriptInterpreterPython::PythonInputReaderManager::InputReaderCallback (void *baton,
264                                                                        InputReader &reader,
265                                                                        InputReaderAction notification,
266                                                                        const char *bytes,
267                                                                        size_t bytes_len)
268{
269    lldb::thread_t embedded_interpreter_thread;
270    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
271
272    if (baton == NULL)
273        return 0;
274
275    ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
276
277    if (script_interpreter->m_script_lang != eScriptLanguagePython)
278        return 0;
279
280    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
281
282    switch (notification)
283    {
284        case eInputReaderActivate:
285        {
286            // Save terminal settings if we can
287            int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor();
288            if (input_fd == File::kInvalidDescriptor)
289                input_fd = STDIN_FILENO;
290
291            script_interpreter->SaveTerminalState(input_fd);
292
293            char error_str[1024];
294            if (script_interpreter->m_embedded_thread_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str,
295                                                                                    sizeof(error_str)))
296            {
297                if (log)
298                    log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in opening master pty (fd = %d).",
299                                 script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor());
300                {
301                    StreamString run_string;
302                    char error_str[1024];
303                    const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
304                    if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
305                    {
306                        ScriptInterpreterPython::Locker locker(script_interpreter,
307                                                               ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
308                                                               ScriptInterpreterPython::Locker::FreeAcquiredLock);
309                        run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str());
310                        PyRun_SimpleString (run_string.GetData());
311                        run_string.Clear ();
312
313                        run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str());
314                        PyRun_SimpleString (run_string.GetData());
315                        run_string.Clear ();
316
317                        run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str());
318                        PyRun_SimpleString (run_string.GetData());
319                        run_string.Clear ();
320
321                        run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(),
322                                           pty_slave_name);
323                        PyRun_SimpleString (run_string.GetData());
324                        run_string.Clear ();
325                    }
326                }
327                embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.noninteractive-python>",
328                                                                  ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader,
329                                                                  script_interpreter, NULL);
330                if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread))
331                {
332                    if (log)
333                        log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread);
334                    Error detach_error;
335                    Host::ThreadDetach (embedded_interpreter_thread, &detach_error);
336                }
337                else
338                {
339                    if (log)
340                        log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed in creating thread");
341                    reader.SetIsDone (true);
342                }
343            }
344            else
345            {
346                if (log)
347                    log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Activate, failed to open master pty ");
348                reader.SetIsDone (true);
349            }
350        }
351            break;
352
353        case eInputReaderDeactivate:
354			// When another input reader is pushed, don't leave the session...
355            //script_interpreter->LeaveSession ();
356            break;
357
358        case eInputReaderReactivate:
359//        {
360//            ScriptInterpreterPython::Locker locker(script_interpreter,
361//                                                   ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession,
362//                                                   ScriptInterpreterPython::Locker::FreeAcquiredLock);
363//        }
364            break;
365
366        case eInputReaderAsynchronousOutputWritten:
367            break;
368
369        case eInputReaderInterrupt:
370            {
371                PyThreadState* state = _PyThreadState_Current;
372                if (!state)
373                    state = script_interpreter->m_command_thread_state;
374                if (state)
375                {
376                    long tid = state->thread_id;
377                    _PyThreadState_Current = state;
378                    int num_threads = PyThreadState_SetAsyncExc(tid, PyExc_KeyboardInterrupt);
379                    if (log)
380                        log->Printf("ScriptInterpreterPython::NonInteractiveInputReaderCallback, eInputReaderInterrupt, tid = %ld, num_threads = %d, state = %p",
381                                    tid,num_threads,state);
382                }
383                else if (log)
384                    log->Printf("ScriptInterpreterPython::NonInteractiveInputReaderCallback, eInputReaderInterrupt, state = NULL");
385            }
386            break;
387
388        case eInputReaderEndOfFile:
389            reader.SetIsDone(true);
390            break;
391
392        case eInputReaderGotToken:
393            if (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor() != -1)
394            {
395                if (log)
396                    log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes,
397                                 bytes_len);
398                if (bytes && bytes_len)
399                    ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), bytes, bytes_len);
400                ::write (script_interpreter->m_embedded_thread_pty.GetMasterFileDescriptor(), "\n", 1);
401            }
402            else
403            {
404                if (log)
405                    log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.",
406                                 bytes,
407                                 bytes_len);
408                reader.SetIsDone (true);
409            }
410            break;
411
412        case eInputReaderDone:
413            {
414                StreamString run_string;
415                char error_str[1024];
416                const char *pty_slave_name = script_interpreter->m_embedded_thread_pty.GetSlaveName (error_str, sizeof (error_str));
417                if (pty_slave_name != NULL && PyThreadState_GetDict() != NULL)
418                {
419                    ScriptInterpreterPython::Locker locker(script_interpreter,
420                                                           ScriptInterpreterPython::Locker::AcquireLock,
421                                                           ScriptInterpreterPython::Locker::FreeAcquiredLock);
422                    run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin; sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str());
423                    PyRun_SimpleString (run_string.GetData());
424                    run_string.Clear();
425                }
426                // Restore terminal settings if they were validly saved
427                if (log)
428                    log->Printf ("ScriptInterpreterPython::NonInteractiveInputReaderCallback, Done, closing down input reader.");
429
430                script_interpreter->RestoreTerminalState ();
431
432                script_interpreter->m_embedded_thread_pty.CloseMasterFileDescriptor();
433            }
434            break;
435    }
436
437    return bytes_len;
438}
439
440ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interpreter) :
441    ScriptInterpreter (interpreter, eScriptLanguagePython),
442    m_embedded_thread_pty (),
443    m_embedded_python_pty (),
444    m_embedded_thread_input_reader_sp (),
445    m_embedded_python_input_reader_sp (),
446    m_dbg_stdout (interpreter.GetDebugger().GetOutputFile().GetStream()),
447    m_new_sysout (NULL),
448    m_old_sysout (NULL),
449    m_old_syserr (NULL),
450    m_run_one_line (NULL),
451    m_dictionary_name (interpreter.GetDebugger().GetInstanceName().AsCString()),
452    m_terminal_state (),
453    m_session_is_active (false),
454    m_valid_session (true),
455    m_command_thread_state (NULL)
456{
457
458    static int g_initialized = false;
459
460    if (!g_initialized)
461    {
462        g_initialized = true;
463        ScriptInterpreterPython::InitializePrivate ();
464    }
465
466    m_dictionary_name.append("_dict");
467    StreamString run_string;
468    run_string.Printf ("%s = dict()", m_dictionary_name.c_str());
469
470    Locker locker(this,
471                  ScriptInterpreterPython::Locker::AcquireLock,
472                  ScriptInterpreterPython::Locker::FreeAcquiredLock);
473    PyRun_SimpleString (run_string.GetData());
474
475    run_string.Clear();
476
477    // Importing 'lldb' module calls SBDebugger::Initialize, which calls Debugger::Initialize, which increments a
478    // global debugger ref-count; therefore we need to check the ref-count before and after importing lldb, and if the
479    // ref-count increased we need to call Debugger::Terminate here to decrement the ref-count so that when the final
480    // call to Debugger::Terminate is made, the ref-count has the correct value.
481    //
482    // Bonus question:  Why doesn't the ref-count always increase?  Because sometimes lldb has already been imported, in
483    // which case the code inside it, including the call to SBDebugger::Initialize(), does not get executed.
484
485    int old_count = Debugger::TestDebuggerRefCount();
486
487    run_string.Printf ("run_one_line (%s, 'import copy, os, re, sys, uuid, lldb')", m_dictionary_name.c_str());
488    PyRun_SimpleString (run_string.GetData());
489
490    // WARNING: temporary code that loads Cocoa formatters - this should be done on a per-platform basis rather than loading the whole set
491    // and letting the individual formatter classes exploit APIs to check whether they can/cannot do their task
492    run_string.Clear();
493    run_string.Printf ("run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp, pydoc')", m_dictionary_name.c_str());
494    PyRun_SimpleString (run_string.GetData());
495
496    int new_count = Debugger::TestDebuggerRefCount();
497
498    if (new_count > old_count)
499        Debugger::Terminate();
500
501    run_string.Clear();
502    run_string.Printf ("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64 "; pydoc.pager = pydoc.plainpager')", m_dictionary_name.c_str(),
503                       interpreter.GetDebugger().GetID());
504    PyRun_SimpleString (run_string.GetData());
505
506    if (m_dbg_stdout != NULL)
507    {
508        m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush);
509    }
510
511    // get the output file handle from the debugger (if any)
512    File& out_file = interpreter.GetDebugger().GetOutputFile();
513    if (out_file.IsValid())
514        ResetOutputFileHandle(out_file.GetStream());
515}
516
517ScriptInterpreterPython::~ScriptInterpreterPython ()
518{
519    Debugger &debugger = GetCommandInterpreter().GetDebugger();
520
521    if (m_embedded_thread_input_reader_sp.get() != NULL)
522    {
523        m_embedded_thread_input_reader_sp->SetIsDone (true);
524        m_embedded_thread_pty.CloseSlaveFileDescriptor();
525        const InputReaderSP reader_sp = m_embedded_thread_input_reader_sp;
526        debugger.PopInputReader (reader_sp);
527        m_embedded_thread_input_reader_sp.reset();
528    }
529
530    if (m_embedded_python_input_reader_sp.get() != NULL)
531    {
532        m_embedded_python_input_reader_sp->SetIsDone (true);
533        m_embedded_python_pty.CloseSlaveFileDescriptor();
534        const InputReaderSP reader_sp = m_embedded_python_input_reader_sp;
535        debugger.PopInputReader (reader_sp);
536        m_embedded_python_input_reader_sp.reset();
537    }
538
539    if (m_new_sysout)
540    {
541        Locker locker(this,
542                      ScriptInterpreterPython::Locker::AcquireLock,
543                      ScriptInterpreterPython::Locker::FreeLock);
544        Py_DECREF ((PyObject*)m_new_sysout);
545    }
546}
547
548void
549ScriptInterpreterPython::ResetOutputFileHandle (FILE *fh)
550{
551    if (fh == NULL)
552        return;
553
554    m_dbg_stdout = fh;
555
556    Locker locker(this,
557                  ScriptInterpreterPython::Locker::AcquireLock,
558                  ScriptInterpreterPython::Locker::FreeAcquiredLock);
559
560    m_new_sysout = PyFile_FromFile (m_dbg_stdout, (char *) "", (char *) "w", _check_and_flush);
561}
562
563void
564ScriptInterpreterPython::SaveTerminalState (int fd)
565{
566    // Python mucks with the terminal state of STDIN. If we can possibly avoid
567    // this by setting the file handles up correctly prior to entering the
568    // interpreter we should. For now we save and restore the terminal state
569    // on the input file handle.
570    m_terminal_state.Save (fd, false);
571}
572
573void
574ScriptInterpreterPython::RestoreTerminalState ()
575{
576    // Python mucks with the terminal state of STDIN. If we can possibly avoid
577    // this by setting the file handles up correctly prior to entering the
578    // interpreter we should. For now we save and restore the terminal state
579    // on the input file handle.
580    m_terminal_state.Restore();
581}
582
583void
584ScriptInterpreterPython::LeaveSession ()
585{
586    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
587    if (log)
588        log->PutCString("ScriptInterpreterPython::LeaveSession()");
589
590    // checking that we have a valid thread state - since we use our own threading and locking
591    // in some (rare) cases during cleanup Python may end up believing we have no thread state
592    // and PyImport_AddModule will crash if that is the case - since that seems to only happen
593    // when destroying the SBDebugger, we can make do without clearing up stdout and stderr
594
595    // rdar://problem/11292882
596    // When the current thread state is NULL, PyThreadState_Get() issues a fatal error.
597    if (PyThreadState_GetDict())
598    {
599        PyObject *sysmod = PyImport_AddModule ("sys");
600        PyObject *sysdict = PyModule_GetDict (sysmod);
601
602        if (m_new_sysout && sysmod && sysdict)
603        {
604            if (m_old_sysout)
605                PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_old_sysout);
606            if (m_old_syserr)
607                PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_old_syserr);
608        }
609    }
610
611    m_session_is_active = false;
612}
613
614bool
615ScriptInterpreterPython::EnterSession (bool init_lldb_globals)
616{
617    // If we have already entered the session, without having officially 'left' it, then there is no need to
618    // 'enter' it again.
619    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
620    if (m_session_is_active)
621    {
622        if (log)
623            log->Printf("ScriptInterpreterPython::EnterSession(init_lldb_globals=%i) session is already active, returning without doing anything", init_lldb_globals);
624        return false;
625    }
626
627    if (log)
628        log->Printf("ScriptInterpreterPython::EnterSession(init_lldb_globals=%i)", init_lldb_globals);
629
630
631    m_session_is_active = true;
632
633    StreamString run_string;
634
635    if (init_lldb_globals)
636    {
637        run_string.Printf (    "run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
638        run_string.Printf (    "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
639        run_string.PutCString ("; lldb.target = lldb.debugger.GetSelectedTarget()");
640        run_string.PutCString ("; lldb.process = lldb.target.GetProcess()");
641        run_string.PutCString ("; lldb.thread = lldb.process.GetSelectedThread ()");
642        run_string.PutCString ("; lldb.frame = lldb.thread.GetSelectedFrame ()");
643        run_string.PutCString ("')");
644    }
645    else
646    {
647        // If we aren't initing the globals, we should still always set the debugger (since that is always unique.)
648        run_string.Printf (    "run_one_line (%s, \"lldb.debugger_unique_id = %" PRIu64, m_dictionary_name.c_str(), GetCommandInterpreter().GetDebugger().GetID());
649        run_string.Printf (    "; lldb.debugger = lldb.SBDebugger.FindDebuggerWithID (%" PRIu64 ")", GetCommandInterpreter().GetDebugger().GetID());
650        run_string.PutCString ("\")");
651    }
652
653    PyRun_SimpleString (run_string.GetData());
654    run_string.Clear();
655
656    PyObject *sysmod = PyImport_AddModule ("sys");
657    PyObject *sysdict = PyModule_GetDict (sysmod);
658
659    if (m_new_sysout && sysmod && sysdict)
660    {
661        m_old_sysout = PyDict_GetItemString(sysdict, "stdout");
662        m_old_syserr = PyDict_GetItemString(sysdict, "stderr");
663        if (m_new_sysout)
664        {
665            PyDict_SetItemString (sysdict, "stdout", (PyObject*)m_new_sysout);
666            PyDict_SetItemString (sysdict, "stderr", (PyObject*)m_new_sysout);
667        }
668    }
669
670    if (PyErr_Occurred())
671        PyErr_Clear ();
672
673    return true;
674}
675
676static PyObject*
677FindSessionDictionary (const char* dict_name)
678{
679    static std::map<ConstString,PyObject*> g_dict_map;
680
681    ConstString dict(dict_name);
682
683    std::map<ConstString,PyObject*>::iterator iter = g_dict_map.find(dict);
684
685    if (iter != g_dict_map.end())
686        return iter->second;
687
688    PyObject *main_mod = PyImport_AddModule ("__main__");
689    if (main_mod != NULL)
690    {
691        PyObject *main_dict = PyModule_GetDict (main_mod);
692        if ((main_dict != NULL)
693            && PyDict_Check (main_dict))
694        {
695            // Go through the main dictionary looking for the correct python script interpreter dictionary
696            PyObject *key, *value;
697            Py_ssize_t pos = 0;
698
699            while (PyDict_Next (main_dict, &pos, &key, &value))
700            {
701                // We have stolen references to the key and value objects in the dictionary; we need to increment
702                // them now so that Python's garbage collector doesn't collect them out from under us.
703                Py_INCREF (key);
704                Py_INCREF (value);
705                if (strcmp (PyString_AsString (key), dict_name) == 0)
706                {
707                    g_dict_map[dict] = value;
708                    return value;
709                }
710            }
711        }
712    }
713    return NULL;
714}
715
716static std::string
717GenerateUniqueName (const char* base_name_wanted,
718                    uint32_t& functions_counter,
719                    void* name_token = NULL)
720{
721    StreamString sstr;
722
723    if (!base_name_wanted)
724        return std::string();
725
726    if (!name_token)
727        sstr.Printf ("%s_%d", base_name_wanted, functions_counter++);
728    else
729        sstr.Printf ("%s_%p", base_name_wanted, name_token);
730
731    return sstr.GetString();
732}
733
734bool
735ScriptInterpreterPython::ExecuteOneLine (const char *command, CommandReturnObject *result, const ExecuteScriptOptions &options)
736{
737    if (!m_valid_session)
738        return false;
739
740    // We want to call run_one_line, passing in the dictionary and the command string.  We cannot do this through
741    // PyRun_SimpleString here because the command string may contain escaped characters, and putting it inside
742    // another string to pass to PyRun_SimpleString messes up the escaping.  So we use the following more complicated
743    // method to pass the command string directly down to Python.
744
745    Locker locker(this,
746                  ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
747                  ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
748
749    bool success = false;
750
751    if (command)
752    {
753        // Find the correct script interpreter dictionary in the main module.
754        PyObject *script_interpreter_dict = FindSessionDictionary(m_dictionary_name.c_str());
755        if (script_interpreter_dict != NULL)
756        {
757            PyObject *pfunc = (PyObject*)m_run_one_line;
758            PyObject *pmod = PyImport_AddModule ("lldb.embedded_interpreter");
759            if (pmod != NULL)
760            {
761                PyObject *pmod_dict = PyModule_GetDict (pmod);
762                if ((pmod_dict != NULL)
763                    && PyDict_Check (pmod_dict))
764                {
765                    if (!pfunc)
766                    {
767                        PyObject *key, *value;
768                        Py_ssize_t pos = 0;
769
770                        while (PyDict_Next (pmod_dict, &pos, &key, &value))
771                        {
772                            Py_INCREF (key);
773                            Py_INCREF (value);
774                            if (strcmp (PyString_AsString (key), "run_one_line") == 0)
775                            {
776                                pfunc = value;
777                                break;
778                            }
779                        }
780                        m_run_one_line = pfunc;
781                    }
782
783                    if (pfunc && PyCallable_Check (pfunc))
784                    {
785                        PyObject *pargs = Py_BuildValue("(Os)",script_interpreter_dict,command);
786                        if (pargs != NULL)
787                        {
788                            PyObject *pvalue = NULL;
789                            { // scope for PythonInputReaderManager
790                                PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
791                                pvalue = PyObject_CallObject (pfunc, pargs);
792                            }
793                            Py_DECREF (pargs);
794                            if (pvalue != NULL)
795                            {
796                                Py_DECREF (pvalue);
797                                success = true;
798                            }
799                            else if (options.GetMaskoutErrors() && PyErr_Occurred ())
800                            {
801                                PyErr_Print();
802                                PyErr_Clear();
803                            }
804                        }
805                    }
806                }
807            }
808            Py_INCREF (script_interpreter_dict);
809        }
810
811        if (success)
812            return true;
813
814        // The one-liner failed.  Append the error message.
815        if (result)
816            result->AppendErrorWithFormat ("python failed attempting to evaluate '%s'\n", command);
817        return false;
818    }
819
820    if (result)
821        result->AppendError ("empty command passed to python\n");
822    return false;
823}
824
825size_t
826ScriptInterpreterPython::InputReaderCallback
827(
828    void *baton,
829    InputReader &reader,
830    InputReaderAction notification,
831    const char *bytes,
832    size_t bytes_len
833)
834{
835    lldb::thread_t embedded_interpreter_thread;
836    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
837
838    if (baton == NULL)
839        return 0;
840
841    ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
842
843    if (script_interpreter->m_script_lang != eScriptLanguagePython)
844        return 0;
845
846    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
847    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
848
849    switch (notification)
850    {
851    case eInputReaderActivate:
852        {
853            if (!batch_mode)
854            {
855                out_stream->Printf ("Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.\n");
856                out_stream->Flush();
857            }
858
859            // Save terminal settings if we can
860            int input_fd = reader.GetDebugger().GetInputFile().GetDescriptor();
861            if (input_fd == File::kInvalidDescriptor)
862                input_fd = STDIN_FILENO;
863
864            script_interpreter->SaveTerminalState(input_fd);
865
866            {
867                ScriptInterpreterPython::Locker locker(script_interpreter,
868                                                       ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
869                                                       ScriptInterpreterPython::Locker::FreeAcquiredLock);
870            }
871
872            char error_str[1024];
873            if (script_interpreter->m_embedded_python_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str,
874                                                                                    sizeof(error_str)))
875            {
876                if (log)
877                    log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in opening master pty (fd = %d).",
878                                  script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor());
879                embedded_interpreter_thread = Host::ThreadCreate ("<lldb.script-interpreter.embedded-python-loop>",
880                                                                  ScriptInterpreterPython::RunEmbeddedPythonInterpreter,
881                                                                  script_interpreter, NULL);
882                if (IS_VALID_LLDB_HOST_THREAD(embedded_interpreter_thread))
883                {
884                    if (log)
885                        log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, succeeded in creating thread (thread_t = %p)", (void *)embedded_interpreter_thread);
886                    Error detach_error;
887                    Host::ThreadDetach (embedded_interpreter_thread, &detach_error);
888                }
889                else
890                {
891                    if (log)
892                        log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed in creating thread");
893                    reader.SetIsDone (true);
894                }
895            }
896            else
897            {
898                if (log)
899                    log->Printf ("ScriptInterpreterPython::InputReaderCallback, Activate, failed to open master pty ");
900                reader.SetIsDone (true);
901            }
902        }
903        break;
904
905    case eInputReaderDeactivate:
906			// When another input reader is pushed, don't leave the session...
907            //script_interpreter->LeaveSession ();
908        break;
909
910    case eInputReaderReactivate:
911        {
912            ScriptInterpreterPython::Locker locker (script_interpreter,
913                                                    ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession,
914                                                    ScriptInterpreterPython::Locker::FreeAcquiredLock);
915        }
916        break;
917
918    case eInputReaderAsynchronousOutputWritten:
919        break;
920
921    case eInputReaderInterrupt:
922        ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "raise KeyboardInterrupt\n", 24);
923        break;
924
925    case eInputReaderEndOfFile:
926        ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()\n", 7);
927        break;
928
929    case eInputReaderGotToken:
930        if (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor() != -1)
931        {
932            if (log)
933                log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu", bytes,
934                             bytes_len);
935            if (bytes && bytes_len)
936            {
937                if ((int) bytes[0] == 4)
938                    ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "quit()", 6);
939                else
940                    ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), bytes, bytes_len);
941            }
942            ::write (script_interpreter->m_embedded_python_pty.GetMasterFileDescriptor(), "\n", 1);
943        }
944        else
945        {
946            if (log)
947                log->Printf ("ScriptInterpreterPython::InputReaderCallback, GotToken, bytes='%s', byte_len = %lu, Master File Descriptor is bad.",
948                             bytes,
949                             bytes_len);
950            reader.SetIsDone (true);
951        }
952
953        break;
954
955    case eInputReaderDone:
956        {
957            Locker locker(script_interpreter,
958                          ScriptInterpreterPython::Locker::AcquireLock,
959                          ScriptInterpreterPython::Locker::FreeAcquiredLock);
960            script_interpreter->LeaveSession ();
961        }
962
963        // Restore terminal settings if they were validly saved
964        if (log)
965            log->Printf ("ScriptInterpreterPython::InputReaderCallback, Done, closing down input reader.");
966
967        script_interpreter->RestoreTerminalState ();
968
969        script_interpreter->m_embedded_python_pty.CloseMasterFileDescriptor();
970        break;
971    }
972
973    return bytes_len;
974}
975
976
977void
978ScriptInterpreterPython::ExecuteInterpreterLoop ()
979{
980    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
981
982    Debugger &debugger = GetCommandInterpreter().GetDebugger();
983
984    // At the moment, the only time the debugger does not have an input file handle is when this is called
985    // directly from Python, in which case it is both dangerous and unnecessary (not to mention confusing) to
986    // try to embed a running interpreter loop inside the already running Python interpreter loop, so we won't
987    // do it.
988
989    if (!debugger.GetInputFile().IsValid())
990        return;
991
992    InputReaderSP reader_sp (new InputReader(debugger));
993    if (reader_sp)
994    {
995        Error error (reader_sp->Initialize (ScriptInterpreterPython::InputReaderCallback,
996                                            this,                         // baton
997                                            eInputReaderGranularityLine,  // token size, to pass to callback function
998                                            NULL,                         // end token
999                                            NULL,                         // prompt
1000                                            true));                       // echo input
1001
1002        if (error.Success())
1003        {
1004            debugger.PushInputReader (reader_sp);
1005            m_embedded_python_input_reader_sp = reader_sp;
1006        }
1007    }
1008}
1009
1010bool
1011ScriptInterpreterPython::ExecuteOneLineWithReturn (const char *in_string,
1012                                                   ScriptInterpreter::ScriptReturnType return_type,
1013                                                   void *ret_value,
1014                                                   const ExecuteScriptOptions &options)
1015{
1016
1017    Locker locker(this,
1018                  ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
1019                  ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1020
1021    PyObject *py_return = NULL;
1022    PyObject *mainmod = PyImport_AddModule ("__main__");
1023    PyObject *globals = PyModule_GetDict (mainmod);
1024    PyObject *locals = NULL;
1025    PyObject *py_error = NULL;
1026    bool ret_success = false;
1027    bool should_decrement_locals = false;
1028    int success;
1029
1030    locals = FindSessionDictionary(m_dictionary_name.c_str());
1031
1032    if (locals == NULL)
1033    {
1034        locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str());
1035        should_decrement_locals = true;
1036    }
1037
1038    if (locals == NULL)
1039    {
1040        locals = globals;
1041        should_decrement_locals = false;
1042    }
1043
1044    py_error = PyErr_Occurred();
1045    if (py_error != NULL)
1046        PyErr_Clear();
1047
1048    if (in_string != NULL)
1049    {
1050        { // scope for PythonInputReaderManager
1051            PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
1052            py_return = PyRun_String (in_string, Py_eval_input, globals, locals);
1053            if (py_return == NULL)
1054            {
1055                py_error = PyErr_Occurred ();
1056                if (py_error != NULL)
1057                    PyErr_Clear ();
1058
1059                py_return = PyRun_String (in_string, Py_single_input, globals, locals);
1060            }
1061        }
1062
1063        if (locals != NULL
1064            && should_decrement_locals)
1065            Py_DECREF (locals);
1066
1067        if (py_return != NULL)
1068        {
1069            switch (return_type)
1070            {
1071                case eScriptReturnTypeCharPtr: // "char *"
1072                {
1073                    const char format[3] = "s#";
1074                    success = PyArg_Parse (py_return, format, (char **) ret_value);
1075                    break;
1076                }
1077                case eScriptReturnTypeCharStrOrNone: // char* or NULL if py_return == Py_None
1078                {
1079                    const char format[3] = "z";
1080                    success = PyArg_Parse (py_return, format, (char **) ret_value);
1081                    break;
1082                }
1083                case eScriptReturnTypeBool:
1084                {
1085                    const char format[2] = "b";
1086                    success = PyArg_Parse (py_return, format, (bool *) ret_value);
1087                    break;
1088                }
1089                case eScriptReturnTypeShortInt:
1090                {
1091                    const char format[2] = "h";
1092                    success = PyArg_Parse (py_return, format, (short *) ret_value);
1093                    break;
1094                }
1095                case eScriptReturnTypeShortIntUnsigned:
1096                {
1097                    const char format[2] = "H";
1098                    success = PyArg_Parse (py_return, format, (unsigned short *) ret_value);
1099                    break;
1100                }
1101                case eScriptReturnTypeInt:
1102                {
1103                    const char format[2] = "i";
1104                    success = PyArg_Parse (py_return, format, (int *) ret_value);
1105                    break;
1106                }
1107                case eScriptReturnTypeIntUnsigned:
1108                {
1109                    const char format[2] = "I";
1110                    success = PyArg_Parse (py_return, format, (unsigned int *) ret_value);
1111                    break;
1112                }
1113                case eScriptReturnTypeLongInt:
1114                {
1115                    const char format[2] = "l";
1116                    success = PyArg_Parse (py_return, format, (long *) ret_value);
1117                    break;
1118                }
1119                case eScriptReturnTypeLongIntUnsigned:
1120                {
1121                    const char format[2] = "k";
1122                    success = PyArg_Parse (py_return, format, (unsigned long *) ret_value);
1123                    break;
1124                }
1125                case eScriptReturnTypeLongLong:
1126                {
1127                    const char format[2] = "L";
1128                    success = PyArg_Parse (py_return, format, (long long *) ret_value);
1129                    break;
1130                }
1131                case eScriptReturnTypeLongLongUnsigned:
1132                {
1133                    const char format[2] = "K";
1134                    success = PyArg_Parse (py_return, format, (unsigned long long *) ret_value);
1135                    break;
1136                }
1137                case eScriptReturnTypeFloat:
1138                {
1139                    const char format[2] = "f";
1140                    success = PyArg_Parse (py_return, format, (float *) ret_value);
1141                    break;
1142                }
1143                case eScriptReturnTypeDouble:
1144                {
1145                    const char format[2] = "d";
1146                    success = PyArg_Parse (py_return, format, (double *) ret_value);
1147                    break;
1148                }
1149                case eScriptReturnTypeChar:
1150                {
1151                    const char format[2] = "c";
1152                    success = PyArg_Parse (py_return, format, (char *) ret_value);
1153                    break;
1154                }
1155            }
1156            Py_DECREF (py_return);
1157            if (success)
1158                ret_success = true;
1159            else
1160                ret_success = false;
1161        }
1162    }
1163
1164    py_error = PyErr_Occurred();
1165    if (py_error != NULL)
1166    {
1167        ret_success = false;
1168        if (options.GetMaskoutErrors())
1169        {
1170            if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
1171                PyErr_Print ();
1172            PyErr_Clear();
1173        }
1174    }
1175
1176    return ret_success;
1177}
1178
1179bool
1180ScriptInterpreterPython::ExecuteMultipleLines (const char *in_string, const ExecuteScriptOptions &options)
1181{
1182
1183
1184    Locker locker(this,
1185                  ScriptInterpreterPython::Locker::AcquireLock      | ScriptInterpreterPython::Locker::InitSession | (options.GetSetLLDBGlobals() ? ScriptInterpreterPython::Locker::InitGlobals : 0),
1186                  ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
1187
1188    bool success = false;
1189    PyObject *py_return = NULL;
1190    PyObject *mainmod = PyImport_AddModule ("__main__");
1191    PyObject *globals = PyModule_GetDict (mainmod);
1192    PyObject *locals = NULL;
1193    PyObject *py_error = NULL;
1194    bool should_decrement_locals = false;
1195
1196    locals = FindSessionDictionary(m_dictionary_name.c_str());
1197
1198    if (locals == NULL)
1199    {
1200        locals = PyObject_GetAttrString (globals, m_dictionary_name.c_str());
1201        should_decrement_locals = true;
1202    }
1203
1204    if (locals == NULL)
1205    {
1206        locals = globals;
1207        should_decrement_locals = false;
1208    }
1209
1210    py_error = PyErr_Occurred();
1211    if (py_error != NULL)
1212        PyErr_Clear();
1213
1214    if (in_string != NULL)
1215    {
1216        struct _node *compiled_node = PyParser_SimpleParseString (in_string, Py_file_input);
1217        if (compiled_node)
1218        {
1219            PyCodeObject *compiled_code = PyNode_Compile (compiled_node, "temp.py");
1220            if (compiled_code)
1221            {
1222                { // scope for PythonInputReaderManager
1223                    PythonInputReaderManager py_input(options.GetEnableIO() ? this : NULL);
1224                    py_return = PyEval_EvalCode (compiled_code, globals, locals);
1225                }
1226                if (py_return != NULL)
1227                {
1228                    success = true;
1229                    Py_DECREF (py_return);
1230                }
1231                if (locals && should_decrement_locals)
1232                    Py_DECREF (locals);
1233            }
1234        }
1235    }
1236
1237    py_error = PyErr_Occurred ();
1238    if (py_error != NULL)
1239    {
1240        success = false;
1241        if (options.GetMaskoutErrors())
1242        {
1243            if (PyErr_GivenExceptionMatches (py_error, PyExc_SyntaxError))
1244                PyErr_Print ();
1245            PyErr_Clear();
1246        }
1247    }
1248
1249    return success;
1250}
1251
1252static const char *g_reader_instructions = "Enter your Python command(s). Type 'DONE' to end.";
1253
1254size_t
1255ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback
1256(
1257    void *baton,
1258    InputReader &reader,
1259    InputReaderAction notification,
1260    const char *bytes,
1261    size_t bytes_len
1262)
1263{
1264    static StringList commands_in_progress;
1265
1266    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1267    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1268
1269    switch (notification)
1270    {
1271    case eInputReaderActivate:
1272        {
1273            commands_in_progress.Clear();
1274            if (!batch_mode)
1275            {
1276                out_stream->Printf ("%s\n", g_reader_instructions);
1277                if (reader.GetPrompt())
1278                    out_stream->Printf ("%s", reader.GetPrompt());
1279                out_stream->Flush ();
1280            }
1281        }
1282        break;
1283
1284    case eInputReaderDeactivate:
1285        break;
1286
1287    case eInputReaderReactivate:
1288        if (reader.GetPrompt() && !batch_mode)
1289        {
1290            out_stream->Printf ("%s", reader.GetPrompt());
1291            out_stream->Flush ();
1292        }
1293        break;
1294
1295    case eInputReaderAsynchronousOutputWritten:
1296        break;
1297
1298    case eInputReaderGotToken:
1299        {
1300            std::string temp_string (bytes, bytes_len);
1301            commands_in_progress.AppendString (temp_string.c_str());
1302            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1303            {
1304                out_stream->Printf ("%s", reader.GetPrompt());
1305                out_stream->Flush ();
1306            }
1307        }
1308        break;
1309
1310    case eInputReaderEndOfFile:
1311    case eInputReaderInterrupt:
1312        // Control-c (SIGINT) & control-d both mean finish & exit.
1313        reader.SetIsDone(true);
1314
1315        // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command.
1316        if (notification == eInputReaderInterrupt)
1317            commands_in_progress.Clear();
1318
1319        // Fall through here...
1320
1321    case eInputReaderDone:
1322        {
1323            BreakpointOptions *bp_options = (BreakpointOptions *)baton;
1324            std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1325            data_ap->user_source.AppendList (commands_in_progress);
1326            if (data_ap.get())
1327            {
1328                ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1329                if (interpreter)
1330                {
1331                    if (interpreter->GenerateBreakpointCommandCallbackData (data_ap->user_source,
1332                                                                            data_ap->script_source))
1333                    {
1334                        BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
1335                        bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
1336                    }
1337                    else if (!batch_mode)
1338                    {
1339                        out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1340                        out_stream->Flush();
1341                    }
1342                }
1343                else
1344                {
1345		            if (!batch_mode)
1346                    {
1347                        out_stream->Printf ("Warning:  Unable to find script intepreter; no command attached to breakpoint.\n");
1348                        out_stream->Flush();
1349                    }
1350                }
1351            }
1352        }
1353        break;
1354
1355    }
1356
1357    return bytes_len;
1358}
1359
1360size_t
1361ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback
1362(
1363    void *baton,
1364    InputReader &reader,
1365    InputReaderAction notification,
1366    const char *bytes,
1367    size_t bytes_len
1368)
1369{
1370    static StringList commands_in_progress;
1371
1372    StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream();
1373    bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
1374
1375    switch (notification)
1376    {
1377    case eInputReaderActivate:
1378        {
1379            commands_in_progress.Clear();
1380            if (!batch_mode)
1381            {
1382                out_stream->Printf ("%s\n", g_reader_instructions);
1383                if (reader.GetPrompt())
1384                    out_stream->Printf ("%s", reader.GetPrompt());
1385                out_stream->Flush ();
1386            }
1387        }
1388        break;
1389
1390    case eInputReaderDeactivate:
1391        break;
1392
1393    case eInputReaderReactivate:
1394        if (reader.GetPrompt() && !batch_mode)
1395        {
1396            out_stream->Printf ("%s", reader.GetPrompt());
1397            out_stream->Flush ();
1398        }
1399        break;
1400
1401    case eInputReaderAsynchronousOutputWritten:
1402        break;
1403
1404    case eInputReaderGotToken:
1405        {
1406            std::string temp_string (bytes, bytes_len);
1407            commands_in_progress.AppendString (temp_string.c_str());
1408            if (!reader.IsDone() && reader.GetPrompt() && !batch_mode)
1409            {
1410                out_stream->Printf ("%s", reader.GetPrompt());
1411                out_stream->Flush ();
1412            }
1413        }
1414        break;
1415
1416    case eInputReaderEndOfFile:
1417    case eInputReaderInterrupt:
1418        // Control-c (SIGINT) & control-d both mean finish & exit.
1419        reader.SetIsDone(true);
1420
1421        // Control-c (SIGINT) ALSO means cancel; do NOT create a breakpoint command.
1422        if (notification == eInputReaderInterrupt)
1423            commands_in_progress.Clear();
1424
1425        // Fall through here...
1426
1427    case eInputReaderDone:
1428        {
1429            WatchpointOptions *wp_options = (WatchpointOptions *)baton;
1430            std::auto_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1431            data_ap->user_source.AppendList (commands_in_progress);
1432            if (data_ap.get())
1433            {
1434                ScriptInterpreter *interpreter = reader.GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
1435                if (interpreter)
1436                {
1437                    if (interpreter->GenerateWatchpointCommandCallbackData (data_ap->user_source,
1438                                                                            data_ap->script_source))
1439                    {
1440                        BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
1441                        wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
1442                    }
1443                    else if (!batch_mode)
1444                    {
1445                        out_stream->Printf ("Warning: No command attached to breakpoint.\n");
1446                        out_stream->Flush();
1447                    }
1448                }
1449                else
1450                {
1451		            if (!batch_mode)
1452                    {
1453                        out_stream->Printf ("Warning:  Unable to find script intepreter; no command attached to breakpoint.\n");
1454                        out_stream->Flush();
1455                    }
1456                }
1457            }
1458        }
1459        break;
1460
1461    }
1462
1463    return bytes_len;
1464}
1465
1466void
1467ScriptInterpreterPython::CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options,
1468                                                                  CommandReturnObject &result)
1469{
1470    Debugger &debugger = GetCommandInterpreter().GetDebugger();
1471
1472    InputReaderSP reader_sp (new InputReader (debugger));
1473
1474    if (reader_sp)
1475    {
1476        Error err = reader_sp->Initialize (
1477                ScriptInterpreterPython::GenerateBreakpointOptionsCommandCallback,
1478                bp_options,                 // baton
1479                eInputReaderGranularityLine, // token size, for feeding data to callback function
1480                "DONE",                     // end token
1481                "> ",                       // prompt
1482                true);                      // echo input
1483
1484        if (err.Success())
1485            debugger.PushInputReader (reader_sp);
1486        else
1487        {
1488            result.AppendError (err.AsCString());
1489            result.SetStatus (eReturnStatusFailed);
1490        }
1491    }
1492    else
1493    {
1494        result.AppendError("out of memory");
1495        result.SetStatus (eReturnStatusFailed);
1496    }
1497}
1498
1499void
1500ScriptInterpreterPython::CollectDataForWatchpointCommandCallback (WatchpointOptions *wp_options,
1501                                                                  CommandReturnObject &result)
1502{
1503    Debugger &debugger = GetCommandInterpreter().GetDebugger();
1504
1505    InputReaderSP reader_sp (new InputReader (debugger));
1506
1507    if (reader_sp)
1508    {
1509        Error err = reader_sp->Initialize (
1510                ScriptInterpreterPython::GenerateWatchpointOptionsCommandCallback,
1511                wp_options,                 // baton
1512                eInputReaderGranularityLine, // token size, for feeding data to callback function
1513                "DONE",                     // end token
1514                "> ",                       // prompt
1515                true);                      // echo input
1516
1517        if (err.Success())
1518            debugger.PushInputReader (reader_sp);
1519        else
1520        {
1521            result.AppendError (err.AsCString());
1522            result.SetStatus (eReturnStatusFailed);
1523        }
1524    }
1525    else
1526    {
1527        result.AppendError("out of memory");
1528        result.SetStatus (eReturnStatusFailed);
1529    }
1530}
1531
1532// Set a Python one-liner as the callback for the breakpoint.
1533void
1534ScriptInterpreterPython::SetBreakpointCommandCallback (BreakpointOptions *bp_options,
1535                                                       const char *oneliner)
1536{
1537    std::auto_ptr<BreakpointOptions::CommandData> data_ap(new BreakpointOptions::CommandData());
1538
1539    // It's necessary to set both user_source and script_source to the oneliner.
1540    // The former is used to generate callback description (as in breakpoint command list)
1541    // while the latter is used for Python to interpret during the actual callback.
1542
1543    data_ap->user_source.AppendString (oneliner);
1544    data_ap->script_source.assign (oneliner);
1545
1546    if (GenerateBreakpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
1547    {
1548        BatonSP baton_sp (new BreakpointOptions::CommandBaton (data_ap.release()));
1549        bp_options->SetCallback (ScriptInterpreterPython::BreakpointCallbackFunction, baton_sp);
1550    }
1551
1552    return;
1553}
1554
1555// Set a Python one-liner as the callback for the watchpoint.
1556void
1557ScriptInterpreterPython::SetWatchpointCommandCallback (WatchpointOptions *wp_options,
1558                                                       const char *oneliner)
1559{
1560    std::auto_ptr<WatchpointOptions::CommandData> data_ap(new WatchpointOptions::CommandData());
1561
1562    // It's necessary to set both user_source and script_source to the oneliner.
1563    // The former is used to generate callback description (as in watchpoint command list)
1564    // while the latter is used for Python to interpret during the actual callback.
1565
1566    data_ap->user_source.AppendString (oneliner);
1567    data_ap->script_source.assign (oneliner);
1568
1569    if (GenerateWatchpointCommandCallbackData (data_ap->user_source, data_ap->script_source))
1570    {
1571        BatonSP baton_sp (new WatchpointOptions::CommandBaton (data_ap.release()));
1572        wp_options->SetCallback (ScriptInterpreterPython::WatchpointCallbackFunction, baton_sp);
1573    }
1574
1575    return;
1576}
1577
1578bool
1579ScriptInterpreterPython::ExportFunctionDefinitionToInterpreter (StringList &function_def)
1580{
1581    // Convert StringList to one long, newline delimited, const char *.
1582    std::string function_def_string(function_def.CopyList());
1583
1584    return ExecuteMultipleLines (function_def_string.c_str(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false));
1585}
1586
1587bool
1588ScriptInterpreterPython::GenerateFunction(const char *signature, const StringList &input)
1589{
1590    int num_lines = input.GetSize ();
1591    if (num_lines == 0)
1592        return false;
1593
1594    if (!signature || *signature == 0)
1595        return false;
1596
1597    StreamString sstr;
1598    StringList auto_generated_function;
1599    auto_generated_function.AppendString (signature);
1600    auto_generated_function.AppendString ("     global_dict = globals()");   // Grab the global dictionary
1601    auto_generated_function.AppendString ("     new_keys = internal_dict.keys()");    // Make a list of keys in the session dict
1602    auto_generated_function.AppendString ("     old_keys = global_dict.keys()"); // Save list of keys in global dict
1603    auto_generated_function.AppendString ("     global_dict.update (internal_dict)"); // Add the session dictionary to the
1604    // global dictionary.
1605
1606    // Wrap everything up inside the function, increasing the indentation.
1607
1608    auto_generated_function.AppendString("     if True:");
1609    for (int i = 0; i < num_lines; ++i)
1610    {
1611        sstr.Clear ();
1612        sstr.Printf ("       %s", input.GetStringAtIndex (i));
1613        auto_generated_function.AppendString (sstr.GetData());
1614    }
1615    auto_generated_function.AppendString ("     for key in new_keys:");  // Iterate over all the keys from session dict
1616    auto_generated_function.AppendString ("         internal_dict[key] = global_dict[key]");  // Update session dict values
1617    auto_generated_function.AppendString ("         if key not in old_keys:");       // If key was not originally in global dict
1618    auto_generated_function.AppendString ("             del global_dict[key]");      //  ...then remove key/value from global dict
1619
1620    // Verify that the results are valid Python.
1621
1622    if (!ExportFunctionDefinitionToInterpreter (auto_generated_function))
1623        return false;
1624
1625    return true;
1626
1627}
1628
1629bool
1630ScriptInterpreterPython::GenerateTypeScriptFunction (StringList &user_input, std::string& output, void* name_token)
1631{
1632    static uint32_t num_created_functions = 0;
1633    user_input.RemoveBlankLines ();
1634    StreamString sstr;
1635
1636    // Check to see if we have any data; if not, just return.
1637    if (user_input.GetSize() == 0)
1638        return false;
1639
1640    // Take what the user wrote, wrap it all up inside one big auto-generated Python function, passing in the
1641    // ValueObject as parameter to the function.
1642
1643    std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_type_print_func", num_created_functions, name_token));
1644    sstr.Printf ("def %s (valobj, internal_dict):", auto_generated_function_name.c_str());
1645
1646    if (!GenerateFunction(sstr.GetData(), user_input))
1647        return false;
1648
1649    // Store the name of the auto-generated function to be called.
1650    output.assign(auto_generated_function_name);
1651    return true;
1652}
1653
1654bool
1655ScriptInterpreterPython::GenerateScriptAliasFunction (StringList &user_input, std::string &output)
1656{
1657    static uint32_t num_created_functions = 0;
1658    user_input.RemoveBlankLines ();
1659    StreamString sstr;
1660
1661    // Check to see if we have any data; if not, just return.
1662    if (user_input.GetSize() == 0)
1663        return false;
1664
1665    std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_cmd_alias_func", num_created_functions));
1666
1667    sstr.Printf ("def %s (debugger, args, result, internal_dict):", auto_generated_function_name.c_str());
1668
1669    if (!GenerateFunction(sstr.GetData(),user_input))
1670        return false;
1671
1672    // Store the name of the auto-generated function to be called.
1673    output.assign(auto_generated_function_name);
1674    return true;
1675}
1676
1677
1678bool
1679ScriptInterpreterPython::GenerateTypeSynthClass (StringList &user_input, std::string &output, void* name_token)
1680{
1681    static uint32_t num_created_classes = 0;
1682    user_input.RemoveBlankLines ();
1683    int num_lines = user_input.GetSize ();
1684    StreamString sstr;
1685
1686    // Check to see if we have any data; if not, just return.
1687    if (user_input.GetSize() == 0)
1688        return false;
1689
1690    // Wrap all user input into a Python class
1691
1692    std::string auto_generated_class_name(GenerateUniqueName("lldb_autogen_python_type_synth_class",num_created_classes,name_token));
1693
1694    StringList auto_generated_class;
1695
1696    // Create the function name & definition string.
1697
1698    sstr.Printf ("class %s:", auto_generated_class_name.c_str());
1699    auto_generated_class.AppendString (sstr.GetData());
1700
1701    // Wrap everything up inside the class, increasing the indentation.
1702    // we don't need to play any fancy indentation tricks here because there is no
1703    // surrounding code whose indentation we need to honor
1704    for (int i = 0; i < num_lines; ++i)
1705    {
1706        sstr.Clear ();
1707        sstr.Printf ("     %s", user_input.GetStringAtIndex (i));
1708        auto_generated_class.AppendString (sstr.GetData());
1709    }
1710
1711
1712    // Verify that the results are valid Python.
1713    // (even though the method is ExportFunctionDefinitionToInterpreter, a class will actually be exported)
1714    // (TODO: rename that method to ExportDefinitionToInterpreter)
1715    if (!ExportFunctionDefinitionToInterpreter (auto_generated_class))
1716        return false;
1717
1718    // Store the name of the auto-generated class
1719
1720    output.assign(auto_generated_class_name);
1721    return true;
1722}
1723
1724lldb::ScriptInterpreterObjectSP
1725ScriptInterpreterPython::OSPlugin_CreatePluginObject (const char *class_name, lldb::ProcessSP process_sp)
1726{
1727    if (class_name == NULL || class_name[0] == '\0')
1728        return lldb::ScriptInterpreterObjectSP();
1729
1730    if (!process_sp)
1731        return lldb::ScriptInterpreterObjectSP();
1732
1733    void* ret_val;
1734
1735    {
1736        Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1737        ret_val = g_swig_create_os_plugin    (class_name,
1738                                              m_dictionary_name.c_str(),
1739                                              process_sp);
1740    }
1741
1742    return MakeScriptObject(ret_val);
1743}
1744
1745lldb::ScriptInterpreterObjectSP
1746ScriptInterpreterPython::OSPlugin_RegisterInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
1747{
1748    Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1749
1750    static char callee_name[] = "get_register_info";
1751
1752    if (!os_plugin_object_sp)
1753        return lldb::ScriptInterpreterObjectSP();
1754
1755    PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1756
1757    if (implementor == NULL || implementor == Py_None)
1758        return lldb::ScriptInterpreterObjectSP();
1759
1760    PyObject* pmeth  = PyObject_GetAttrString(implementor, callee_name);
1761
1762    if (PyErr_Occurred())
1763    {
1764        PyErr_Clear();
1765    }
1766
1767    if (pmeth == NULL || pmeth == Py_None)
1768    {
1769        Py_XDECREF(pmeth);
1770        return lldb::ScriptInterpreterObjectSP();
1771    }
1772
1773    if (PyCallable_Check(pmeth) == 0)
1774    {
1775        if (PyErr_Occurred())
1776        {
1777            PyErr_Clear();
1778        }
1779
1780        Py_XDECREF(pmeth);
1781        return lldb::ScriptInterpreterObjectSP();
1782    }
1783
1784    if (PyErr_Occurred())
1785    {
1786        PyErr_Clear();
1787    }
1788
1789    Py_XDECREF(pmeth);
1790
1791    // right now we know this function exists and is callable..
1792    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
1793
1794    // if it fails, print the error but otherwise go on
1795    if (PyErr_Occurred())
1796    {
1797        PyErr_Print();
1798        PyErr_Clear();
1799    }
1800
1801    return MakeScriptObject(py_return);
1802}
1803
1804lldb::ScriptInterpreterObjectSP
1805ScriptInterpreterPython::OSPlugin_ThreadsInfo (lldb::ScriptInterpreterObjectSP os_plugin_object_sp)
1806{
1807    Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1808
1809    static char callee_name[] = "get_thread_info";
1810
1811    if (!os_plugin_object_sp)
1812        return lldb::ScriptInterpreterObjectSP();
1813
1814    PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1815
1816    if (implementor == NULL || implementor == Py_None)
1817        return lldb::ScriptInterpreterObjectSP();
1818
1819    PyObject* pmeth  = PyObject_GetAttrString(implementor, callee_name);
1820
1821    if (PyErr_Occurred())
1822    {
1823        PyErr_Clear();
1824    }
1825
1826    if (pmeth == NULL || pmeth == Py_None)
1827    {
1828        Py_XDECREF(pmeth);
1829        return lldb::ScriptInterpreterObjectSP();
1830    }
1831
1832    if (PyCallable_Check(pmeth) == 0)
1833    {
1834        if (PyErr_Occurred())
1835        {
1836            PyErr_Clear();
1837        }
1838
1839        Py_XDECREF(pmeth);
1840        return lldb::ScriptInterpreterObjectSP();
1841    }
1842
1843    if (PyErr_Occurred())
1844    {
1845        PyErr_Clear();
1846    }
1847
1848    Py_XDECREF(pmeth);
1849
1850    // right now we know this function exists and is callable..
1851    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, NULL);
1852
1853    // if it fails, print the error but otherwise go on
1854    if (PyErr_Occurred())
1855    {
1856        PyErr_Print();
1857        PyErr_Clear();
1858    }
1859
1860    return MakeScriptObject(py_return);
1861}
1862
1863// GetPythonValueFormatString provides a system independent type safe way to
1864// convert a variable's type into a python value format. Python value formats
1865// are defined in terms of builtin C types and could change from system to
1866// as the underlying typedef for uint* types, size_t, off_t and other values
1867// change.
1868
1869template <typename T>
1870const char *GetPythonValueFormatString(T t)
1871{
1872    assert(!"Unhandled type passed to GetPythonValueFormatString(T), make a specialization of GetPythonValueFormatString() to support this type.");
1873    return NULL;
1874}
1875template <> const char *GetPythonValueFormatString (char *)             { return "s"; }
1876template <> const char *GetPythonValueFormatString (char)               { return "b"; }
1877template <> const char *GetPythonValueFormatString (unsigned char)      { return "B"; }
1878template <> const char *GetPythonValueFormatString (short)              { return "h"; }
1879template <> const char *GetPythonValueFormatString (unsigned short)     { return "H"; }
1880template <> const char *GetPythonValueFormatString (int)                { return "i"; }
1881template <> const char *GetPythonValueFormatString (unsigned int)       { return "I"; }
1882template <> const char *GetPythonValueFormatString (long)               { return "l"; }
1883template <> const char *GetPythonValueFormatString (unsigned long)      { return "k"; }
1884template <> const char *GetPythonValueFormatString (long long)          { return "L"; }
1885template <> const char *GetPythonValueFormatString (unsigned long long) { return "K"; }
1886template <> const char *GetPythonValueFormatString (float t)            { return "f"; }
1887template <> const char *GetPythonValueFormatString (double t)           { return "d"; }
1888
1889lldb::ScriptInterpreterObjectSP
1890ScriptInterpreterPython::OSPlugin_RegisterContextData (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
1891                                                       lldb::tid_t tid)
1892{
1893    Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1894
1895    static char callee_name[] = "get_register_data";
1896    static char *param_format = const_cast<char *>(GetPythonValueFormatString(tid));
1897
1898    if (!os_plugin_object_sp)
1899        return lldb::ScriptInterpreterObjectSP();
1900
1901    PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1902
1903    if (implementor == NULL || implementor == Py_None)
1904        return lldb::ScriptInterpreterObjectSP();
1905
1906    PyObject* pmeth  = PyObject_GetAttrString(implementor, callee_name);
1907
1908    if (PyErr_Occurred())
1909    {
1910        PyErr_Clear();
1911    }
1912
1913    if (pmeth == NULL || pmeth == Py_None)
1914    {
1915        Py_XDECREF(pmeth);
1916        return lldb::ScriptInterpreterObjectSP();
1917    }
1918
1919    if (PyCallable_Check(pmeth) == 0)
1920    {
1921        if (PyErr_Occurred())
1922        {
1923            PyErr_Clear();
1924        }
1925
1926        Py_XDECREF(pmeth);
1927        return lldb::ScriptInterpreterObjectSP();
1928    }
1929
1930    if (PyErr_Occurred())
1931    {
1932        PyErr_Clear();
1933    }
1934
1935    Py_XDECREF(pmeth);
1936
1937    // right now we know this function exists and is callable..
1938    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, param_format, tid);
1939
1940    // if it fails, print the error but otherwise go on
1941    if (PyErr_Occurred())
1942    {
1943        PyErr_Print();
1944        PyErr_Clear();
1945    }
1946
1947    return MakeScriptObject(py_return);
1948}
1949
1950lldb::ScriptInterpreterObjectSP
1951ScriptInterpreterPython::OSPlugin_CreateThread (lldb::ScriptInterpreterObjectSP os_plugin_object_sp,
1952                                                lldb::tid_t tid,
1953                                                lldb::addr_t context)
1954{
1955    Locker py_lock(this,Locker::AcquireLock,Locker::FreeLock);
1956
1957    static char callee_name[] = "create_thread";
1958    std::string param_format;
1959    param_format += GetPythonValueFormatString(tid);
1960    param_format += GetPythonValueFormatString(context);
1961
1962    if (!os_plugin_object_sp)
1963        return lldb::ScriptInterpreterObjectSP();
1964
1965    PyObject* implementor = (PyObject*)os_plugin_object_sp->GetObject();
1966
1967    if (implementor == NULL || implementor == Py_None)
1968        return lldb::ScriptInterpreterObjectSP();
1969
1970    PyObject* pmeth  = PyObject_GetAttrString(implementor, callee_name);
1971
1972    if (PyErr_Occurred())
1973    {
1974        PyErr_Clear();
1975    }
1976
1977    if (pmeth == NULL || pmeth == Py_None)
1978    {
1979        Py_XDECREF(pmeth);
1980        return lldb::ScriptInterpreterObjectSP();
1981    }
1982
1983    if (PyCallable_Check(pmeth) == 0)
1984    {
1985        if (PyErr_Occurred())
1986        {
1987            PyErr_Clear();
1988        }
1989
1990        Py_XDECREF(pmeth);
1991        return lldb::ScriptInterpreterObjectSP();
1992    }
1993
1994    if (PyErr_Occurred())
1995    {
1996        PyErr_Clear();
1997    }
1998
1999    Py_XDECREF(pmeth);
2000
2001    // right now we know this function exists and is callable..
2002    PyObject* py_return = PyObject_CallMethod(implementor, callee_name, &param_format[0], tid, context);
2003
2004    // if it fails, print the error but otherwise go on
2005    if (PyErr_Occurred())
2006    {
2007        PyErr_Print();
2008        PyErr_Clear();
2009    }
2010
2011    return MakeScriptObject(py_return);
2012}
2013
2014lldb::ScriptInterpreterObjectSP
2015ScriptInterpreterPython::CreateSyntheticScriptedProvider (const char *class_name,
2016                                                          lldb::ValueObjectSP valobj)
2017{
2018    if (class_name == NULL || class_name[0] == '\0')
2019        return lldb::ScriptInterpreterObjectSP();
2020
2021    if (!valobj.get())
2022        return lldb::ScriptInterpreterObjectSP();
2023
2024    ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
2025    Target *target = exe_ctx.GetTargetPtr();
2026
2027    if (!target)
2028        return lldb::ScriptInterpreterObjectSP();
2029
2030    Debugger &debugger = target->GetDebugger();
2031    ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2032    ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2033
2034    if (!script_interpreter)
2035        return lldb::ScriptInterpreterObjectSP();
2036
2037    void* ret_val;
2038
2039    {
2040        Locker py_lock(this);
2041        ret_val = g_swig_synthetic_script (class_name,
2042                                           python_interpreter->m_dictionary_name.c_str(),
2043                                           valobj);
2044    }
2045
2046    return MakeScriptObject(ret_val);
2047}
2048
2049bool
2050ScriptInterpreterPython::GenerateTypeScriptFunction (const char* oneliner, std::string& output, void* name_token)
2051{
2052    StringList input;
2053    input.SplitIntoLines(oneliner, strlen(oneliner));
2054    return GenerateTypeScriptFunction(input, output, name_token);
2055}
2056
2057bool
2058ScriptInterpreterPython::GenerateTypeSynthClass (const char* oneliner, std::string& output, void* name_token)
2059{
2060    StringList input;
2061    input.SplitIntoLines(oneliner, strlen(oneliner));
2062    return GenerateTypeSynthClass(input, output, name_token);
2063}
2064
2065
2066bool
2067ScriptInterpreterPython::GenerateBreakpointCommandCallbackData (StringList &user_input, std::string& output)
2068{
2069    static uint32_t num_created_functions = 0;
2070    user_input.RemoveBlankLines ();
2071    StreamString sstr;
2072
2073    if (user_input.GetSize() == 0)
2074        return false;
2075
2076    std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_bp_callback_func_",num_created_functions));
2077    sstr.Printf ("def %s (frame, bp_loc, internal_dict):", auto_generated_function_name.c_str());
2078
2079    if (!GenerateFunction(sstr.GetData(), user_input))
2080        return false;
2081
2082    // Store the name of the auto-generated function to be called.
2083    output.assign(auto_generated_function_name);
2084    return true;
2085}
2086
2087bool
2088ScriptInterpreterPython::GenerateWatchpointCommandCallbackData (StringList &user_input, std::string& output)
2089{
2090    static uint32_t num_created_functions = 0;
2091    user_input.RemoveBlankLines ();
2092    StreamString sstr;
2093
2094    if (user_input.GetSize() == 0)
2095        return false;
2096
2097    std::string auto_generated_function_name(GenerateUniqueName("lldb_autogen_python_wp_callback_func_",num_created_functions));
2098    sstr.Printf ("def %s (frame, wp, internal_dict):", auto_generated_function_name.c_str());
2099
2100    if (!GenerateFunction(sstr.GetData(), user_input))
2101        return false;
2102
2103    // Store the name of the auto-generated function to be called.
2104    output.assign(auto_generated_function_name);
2105    return true;
2106}
2107
2108bool
2109ScriptInterpreterPython::GetScriptedSummary (const char *python_function_name,
2110                                             lldb::ValueObjectSP valobj,
2111                                             lldb::ScriptInterpreterObjectSP& callee_wrapper_sp,
2112                                             std::string& retval)
2113{
2114
2115    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
2116
2117    if (!valobj.get())
2118    {
2119        retval.assign("<no object>");
2120        return false;
2121    }
2122
2123    void* old_callee = (callee_wrapper_sp ? callee_wrapper_sp->GetObject() : NULL);
2124    void* new_callee = old_callee;
2125
2126    bool ret_val;
2127    if (python_function_name
2128        && *python_function_name)
2129    {
2130        {
2131            Locker py_lock(this);
2132            {
2133            Timer scoped_timer ("g_swig_typescript_callback","g_swig_typescript_callback");
2134            ret_val = g_swig_typescript_callback (python_function_name,
2135                                                  FindSessionDictionary(m_dictionary_name.c_str()),
2136                                                  valobj,
2137                                                  &new_callee,
2138                                                  retval);
2139            }
2140        }
2141    }
2142    else
2143    {
2144        retval.assign("<no function name>");
2145        return false;
2146    }
2147
2148    if (new_callee && old_callee != new_callee)
2149        callee_wrapper_sp = MakeScriptObject(new_callee);
2150
2151    return ret_val;
2152
2153}
2154
2155bool
2156ScriptInterpreterPython::BreakpointCallbackFunction
2157(
2158    void *baton,
2159    StoppointCallbackContext *context,
2160    user_id_t break_id,
2161    user_id_t break_loc_id
2162)
2163{
2164    BreakpointOptions::CommandData *bp_option_data = (BreakpointOptions::CommandData *) baton;
2165    const char *python_function_name = bp_option_data->script_source.c_str();
2166
2167    if (!context)
2168        return true;
2169
2170    ExecutionContext exe_ctx (context->exe_ctx_ref);
2171    Target *target = exe_ctx.GetTargetPtr();
2172
2173    if (!target)
2174        return true;
2175
2176    Debugger &debugger = target->GetDebugger();
2177    ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2178    ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2179
2180    if (!script_interpreter)
2181        return true;
2182
2183    if (python_function_name != NULL
2184        && python_function_name[0] != '\0')
2185    {
2186        const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2187        BreakpointSP breakpoint_sp = target->GetBreakpointByID (break_id);
2188        if (breakpoint_sp)
2189        {
2190            const BreakpointLocationSP bp_loc_sp (breakpoint_sp->FindLocationByID (break_loc_id));
2191
2192            if (stop_frame_sp && bp_loc_sp)
2193            {
2194                bool ret_val = true;
2195                {
2196                    Locker py_lock(python_interpreter);
2197                    ret_val = g_swig_breakpoint_callback (python_function_name,
2198                                                          python_interpreter->m_dictionary_name.c_str(),
2199                                                          stop_frame_sp,
2200                                                          bp_loc_sp);
2201                }
2202                return ret_val;
2203            }
2204        }
2205    }
2206    // We currently always true so we stop in case anything goes wrong when
2207    // trying to call the script function
2208    return true;
2209}
2210
2211bool
2212ScriptInterpreterPython::WatchpointCallbackFunction
2213(
2214    void *baton,
2215    StoppointCallbackContext *context,
2216    user_id_t watch_id
2217)
2218{
2219    WatchpointOptions::CommandData *wp_option_data = (WatchpointOptions::CommandData *) baton;
2220    const char *python_function_name = wp_option_data->script_source.c_str();
2221
2222    if (!context)
2223        return true;
2224
2225    ExecutionContext exe_ctx (context->exe_ctx_ref);
2226    Target *target = exe_ctx.GetTargetPtr();
2227
2228    if (!target)
2229        return true;
2230
2231    Debugger &debugger = target->GetDebugger();
2232    ScriptInterpreter *script_interpreter = debugger.GetCommandInterpreter().GetScriptInterpreter();
2233    ScriptInterpreterPython *python_interpreter = (ScriptInterpreterPython *) script_interpreter;
2234
2235    if (!script_interpreter)
2236        return true;
2237
2238    if (python_function_name != NULL
2239        && python_function_name[0] != '\0')
2240    {
2241        const StackFrameSP stop_frame_sp (exe_ctx.GetFrameSP());
2242        WatchpointSP wp_sp = target->GetWatchpointList().FindByID (watch_id);
2243        if (wp_sp)
2244        {
2245            if (stop_frame_sp && wp_sp)
2246            {
2247                bool ret_val = true;
2248                {
2249                    Locker py_lock(python_interpreter);
2250                    ret_val = g_swig_watchpoint_callback (python_function_name,
2251                                                          python_interpreter->m_dictionary_name.c_str(),
2252                                                          stop_frame_sp,
2253                                                          wp_sp);
2254                }
2255                return ret_val;
2256            }
2257        }
2258    }
2259    // We currently always true so we stop in case anything goes wrong when
2260    // trying to call the script function
2261    return true;
2262}
2263
2264lldb::thread_result_t
2265ScriptInterpreterPython::RunEmbeddedPythonInterpreter (lldb::thread_arg_t baton)
2266{
2267    ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
2268
2269    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT));
2270
2271    if (log)
2272        log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread starting...", baton);
2273
2274    char error_str[1024];
2275    const char *pty_slave_name = script_interpreter->m_embedded_python_pty.GetSlaveName (error_str, sizeof (error_str));
2276
2277    if (pty_slave_name != NULL)
2278    {
2279        StreamString run_string;
2280
2281        // Ensure we have the GIL before running any Python code.
2282        // Since we're only running a few one-liners and then dropping to the interpreter (which will release the GIL when needed),
2283        // we can just release the GIL after finishing our work.
2284        // If finer-grained locking is desirable, we can lock and unlock the GIL only when calling a python function.
2285        Locker locker(script_interpreter,
2286                      ScriptInterpreterPython::Locker::AcquireLock | ScriptInterpreterPython::Locker::InitSession | ScriptInterpreterPython::Locker::InitGlobals,
2287                      ScriptInterpreterPython::Locker::FreeAcquiredLock | ScriptInterpreterPython::Locker::TearDownSession);
2288
2289        run_string.Printf ("run_one_line (%s, 'save_stderr = sys.stderr')", script_interpreter->m_dictionary_name.c_str());
2290        PyRun_SimpleString (run_string.GetData());
2291        run_string.Clear ();
2292
2293        run_string.Printf ("run_one_line (%s, 'sys.stderr = sys.stdout')", script_interpreter->m_dictionary_name.c_str());
2294        PyRun_SimpleString (run_string.GetData());
2295        run_string.Clear ();
2296
2297        run_string.Printf ("run_one_line (%s, 'save_stdin = sys.stdin')", script_interpreter->m_dictionary_name.c_str());
2298        PyRun_SimpleString (run_string.GetData());
2299        run_string.Clear ();
2300
2301        run_string.Printf ("run_one_line (%s, \"sys.stdin = open ('%s', 'r')\")", script_interpreter->m_dictionary_name.c_str(),
2302                           pty_slave_name);
2303        PyRun_SimpleString (run_string.GetData());
2304        run_string.Clear ();
2305
2306        // The following call drops into the embedded interpreter loop and stays there until the
2307        // user chooses to exit from the Python interpreter.
2308        // This embedded interpreter will, as any Python code that performs I/O, unlock the GIL before
2309        // a system call that can hang, and lock it when the syscall has returned.
2310
2311        // We need to surround the call to the embedded interpreter with calls to PyGILState_Ensure and
2312        // PyGILState_Release (using the Locker above). This is because Python has a global lock which must be held whenever we want
2313        // to touch any Python objects. Otherwise, if the user calls Python code, the interpreter state will be off,
2314        // and things could hang (it's happened before).
2315
2316        run_string.Printf ("run_python_interpreter (%s)", script_interpreter->m_dictionary_name.c_str());
2317        PyRun_SimpleString (run_string.GetData());
2318        run_string.Clear ();
2319
2320        run_string.Printf ("run_one_line (%s, 'sys.stdin = save_stdin')", script_interpreter->m_dictionary_name.c_str());
2321        PyRun_SimpleString (run_string.GetData());
2322        run_string.Clear();
2323
2324        run_string.Printf ("run_one_line (%s, 'sys.stderr = save_stderr')", script_interpreter->m_dictionary_name.c_str());
2325        PyRun_SimpleString (run_string.GetData());
2326        run_string.Clear();
2327    }
2328
2329    if (script_interpreter->m_embedded_python_input_reader_sp)
2330        script_interpreter->m_embedded_python_input_reader_sp->SetIsDone (true);
2331
2332    script_interpreter->m_embedded_python_pty.CloseSlaveFileDescriptor();
2333
2334    log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT);
2335    if (log)
2336        log->Printf ("%p ScriptInterpreterPython::RunEmbeddedPythonInterpreter () thread exiting...", baton);
2337
2338
2339    // Clean up the input reader and make the debugger pop it off the stack.
2340    Debugger &debugger = script_interpreter->GetCommandInterpreter().GetDebugger();
2341    const InputReaderSP reader_sp = script_interpreter->m_embedded_python_input_reader_sp;
2342    if (reader_sp)
2343    {
2344        debugger.PopInputReader (reader_sp);
2345        script_interpreter->m_embedded_python_input_reader_sp.reset();
2346    }
2347
2348    return NULL;
2349}
2350
2351lldb::thread_result_t
2352ScriptInterpreterPython::PythonInputReaderManager::RunPythonInputReader (lldb::thread_arg_t baton)
2353{
2354    ScriptInterpreterPython *script_interpreter = (ScriptInterpreterPython *) baton;
2355
2356    const InputReaderSP reader_sp = script_interpreter->m_embedded_thread_input_reader_sp;
2357
2358    if (reader_sp)
2359        reader_sp->WaitOnReaderIsDone();
2360
2361    return NULL;
2362}
2363
2364size_t
2365ScriptInterpreterPython::CalculateNumChildren (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2366{
2367    if (!implementor_sp)
2368        return 0;
2369
2370    void* implementor = implementor_sp->GetObject();
2371
2372    if (!implementor)
2373        return 0;
2374
2375    if (!g_swig_calc_children)
2376        return 0;
2377
2378    uint32_t ret_val = 0;
2379
2380    {
2381        Locker py_lock(this);
2382        ret_val = g_swig_calc_children (implementor);
2383    }
2384
2385    return ret_val;
2386}
2387
2388lldb::ValueObjectSP
2389ScriptInterpreterPython::GetChildAtIndex (const lldb::ScriptInterpreterObjectSP& implementor_sp, uint32_t idx)
2390{
2391    if (!implementor_sp)
2392        return lldb::ValueObjectSP();
2393
2394    void* implementor = implementor_sp->GetObject();
2395
2396    if (!implementor)
2397        return lldb::ValueObjectSP();
2398
2399    if (!g_swig_get_child_index || !g_swig_cast_to_sbvalue)
2400        return lldb::ValueObjectSP();
2401
2402    void* child_ptr = NULL;
2403    lldb::SBValue* value_sb = NULL;
2404    lldb::ValueObjectSP ret_val;
2405
2406    {
2407        Locker py_lock(this);
2408        child_ptr = g_swig_get_child_index (implementor,idx);
2409        if (child_ptr != NULL && child_ptr != Py_None)
2410        {
2411            value_sb = (lldb::SBValue*)g_swig_cast_to_sbvalue(child_ptr);
2412            if (value_sb == NULL)
2413                Py_XDECREF(child_ptr);
2414            else
2415                ret_val = value_sb->GetSP();
2416        }
2417        else
2418        {
2419            Py_XDECREF(child_ptr);
2420        }
2421    }
2422
2423    return ret_val;
2424}
2425
2426int
2427ScriptInterpreterPython::GetIndexOfChildWithName (const lldb::ScriptInterpreterObjectSP& implementor_sp, const char* child_name)
2428{
2429    if (!implementor_sp)
2430        return UINT32_MAX;
2431
2432    void* implementor = implementor_sp->GetObject();
2433
2434    if (!implementor)
2435        return UINT32_MAX;
2436
2437    if (!g_swig_get_index_child)
2438        return UINT32_MAX;
2439
2440    int ret_val = UINT32_MAX;
2441
2442    {
2443        Locker py_lock(this);
2444        ret_val = g_swig_get_index_child (implementor, child_name);
2445    }
2446
2447    return ret_val;
2448}
2449
2450bool
2451ScriptInterpreterPython::UpdateSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2452{
2453    bool ret_val = false;
2454
2455    if (!implementor_sp)
2456        return ret_val;
2457
2458    void* implementor = implementor_sp->GetObject();
2459
2460    if (!implementor)
2461        return ret_val;
2462
2463    if (!g_swig_update_provider)
2464        return ret_val;
2465
2466    {
2467        Locker py_lock(this);
2468        ret_val = g_swig_update_provider (implementor);
2469    }
2470
2471    return ret_val;
2472}
2473
2474bool
2475ScriptInterpreterPython::MightHaveChildrenSynthProviderInstance (const lldb::ScriptInterpreterObjectSP& implementor_sp)
2476{
2477    bool ret_val = false;
2478
2479    if (!implementor_sp)
2480        return ret_val;
2481
2482    void* implementor = implementor_sp->GetObject();
2483
2484    if (!implementor)
2485        return ret_val;
2486
2487    if (!g_swig_mighthavechildren_provider)
2488        return ret_val;
2489
2490    {
2491        Locker py_lock(this);
2492        ret_val = g_swig_mighthavechildren_provider (implementor);
2493    }
2494
2495    return ret_val;
2496}
2497
2498static std::string
2499ReadPythonBacktrace (PyObject* py_backtrace)
2500{
2501    PyObject* traceback_module = NULL,
2502    *stringIO_module = NULL,
2503    *stringIO_builder = NULL,
2504    *stringIO_buffer = NULL,
2505    *printTB = NULL,
2506    *printTB_args = NULL,
2507    *printTB_result = NULL,
2508    *stringIO_getvalue = NULL,
2509    *printTB_string = NULL;
2510
2511    std::string retval("backtrace unavailable");
2512
2513    if (py_backtrace && py_backtrace != Py_None)
2514    {
2515        traceback_module = PyImport_ImportModule("traceback");
2516        stringIO_module = PyImport_ImportModule("StringIO");
2517
2518        if (traceback_module && traceback_module != Py_None && stringIO_module && stringIO_module != Py_None)
2519        {
2520            stringIO_builder = PyObject_GetAttrString(stringIO_module, "StringIO");
2521            if (stringIO_builder && stringIO_builder != Py_None)
2522            {
2523                stringIO_buffer = PyObject_CallObject(stringIO_builder, NULL);
2524                if (stringIO_buffer && stringIO_buffer != Py_None)
2525                {
2526                    printTB = PyObject_GetAttrString(traceback_module, "print_tb");
2527                    if (printTB && printTB != Py_None)
2528                    {
2529                        printTB_args = Py_BuildValue("OOO",py_backtrace,Py_None,stringIO_buffer);
2530                        printTB_result = PyObject_CallObject(printTB, printTB_args);
2531                        stringIO_getvalue = PyObject_GetAttrString(stringIO_buffer, "getvalue");
2532                        if (stringIO_getvalue && stringIO_getvalue != Py_None)
2533                        {
2534                            printTB_string = PyObject_CallObject (stringIO_getvalue,NULL);
2535                            if (printTB_string && printTB_string != Py_None && PyString_Check(printTB_string))
2536                                retval.assign(PyString_AsString(printTB_string));
2537                        }
2538                    }
2539                }
2540            }
2541        }
2542    }
2543    Py_XDECREF(traceback_module);
2544    Py_XDECREF(stringIO_module);
2545    Py_XDECREF(stringIO_builder);
2546    Py_XDECREF(stringIO_buffer);
2547    Py_XDECREF(printTB);
2548    Py_XDECREF(printTB_args);
2549    Py_XDECREF(printTB_result);
2550    Py_XDECREF(stringIO_getvalue);
2551    Py_XDECREF(printTB_string);
2552    return retval;
2553}
2554
2555bool
2556ScriptInterpreterPython::LoadScriptingModule (const char* pathname,
2557                                              bool can_reload,
2558                                              bool init_session,
2559                                              lldb_private::Error& error)
2560{
2561    if (!pathname || !pathname[0])
2562    {
2563        error.SetErrorString("invalid pathname");
2564        return false;
2565    }
2566
2567    if (!g_swig_call_module_init)
2568    {
2569        error.SetErrorString("internal helper function missing");
2570        return false;
2571    }
2572
2573    lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2574
2575    {
2576        FileSpec target_file(pathname, true);
2577
2578        // TODO: would we want to reject any other value?
2579        if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
2580            target_file.GetFileType() == FileSpec::eFileTypeUnknown)
2581        {
2582            error.SetErrorString("invalid pathname");
2583            return false;
2584        }
2585
2586        const char* directory = target_file.GetDirectory().GetCString();
2587        std::string basename(target_file.GetFilename().GetCString());
2588
2589        // Before executing Pyton code, lock the GIL.
2590        Locker py_lock (this,
2591                        Locker::AcquireLock      | (init_session ? Locker::InitSession     : 0),
2592                        Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
2593
2594        // now make sure that Python has "directory" in the search path
2595        StreamString command_stream;
2596        command_stream.Printf("if not (sys.path.__contains__('%s')):\n    sys.path.insert(1,'%s');\n\n",
2597                              directory,
2598                              directory);
2599        bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
2600        if (!syspath_retval)
2601        {
2602            error.SetErrorString("Python sys.path handling failed");
2603            return false;
2604        }
2605
2606        // strip .py or .pyc extension
2607        ConstString extension = target_file.GetFileNameExtension();
2608        if (extension)
2609        {
2610            if (::strcmp(extension.GetCString(), "py") == 0)
2611                basename.resize(basename.length()-3);
2612            else if(::strcmp(extension.GetCString(), "pyc") == 0)
2613                basename.resize(basename.length()-4);
2614        }
2615
2616        // check if the module is already import-ed
2617        command_stream.Clear();
2618        command_stream.Printf("sys.getrefcount(%s)",basename.c_str());
2619        int refcount = 0;
2620        // this call will fail if the module does not exist (because the parameter to it is not a string
2621        // but an actual Python module object, which is non-existant if the module was not imported before)
2622        bool was_imported = (ExecuteOneLineWithReturn(command_stream.GetData(),
2623                                                      ScriptInterpreterPython::eScriptReturnTypeInt,
2624                                                      &refcount,
2625                                                      ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && refcount > 0);
2626        if (was_imported == true && can_reload == false)
2627        {
2628            error.SetErrorString("module already imported");
2629            return false;
2630        }
2631
2632        // now actually do the import
2633        command_stream.Clear();
2634        command_stream.Printf("import %s",basename.c_str());
2635        bool import_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false).SetMaskoutErrors(false));
2636        PyObject* py_error = PyErr_Occurred(); // per Python docs: "you do not need to Py_DECREF()" the return of this function
2637
2638        if (py_error || !import_retval) // check for failure of the import
2639        {
2640            if (py_error) // if we have a Python error..
2641            {
2642                PyObject *type = NULL,*value = NULL,*traceback = NULL;
2643                PyErr_Fetch (&type,&value,&traceback);
2644
2645                if (PyErr_GivenExceptionMatches (py_error, PyExc_ImportError)) // and it is an ImportError
2646                {
2647                    if (value && value != Py_None)
2648                        error.SetErrorString(PyString_AsString(PyObject_Str(value)));
2649                    else
2650                        error.SetErrorString("ImportError raised by imported module");
2651                }
2652                else // any other error
2653                {
2654                    // get the backtrace
2655                    std::string bt = ReadPythonBacktrace(traceback);
2656
2657                    if (value && value != Py_None)
2658                        error.SetErrorStringWithFormat("Python error raised while importing module: %s - traceback: %s", PyString_AsString(PyObject_Str(value)),bt.c_str());
2659                    else
2660                        error.SetErrorStringWithFormat("Python raised an error while importing module - traceback: %s",bt.c_str());
2661                }
2662
2663                Py_XDECREF(type);
2664                Py_XDECREF(value);
2665                Py_XDECREF(traceback);
2666            }
2667            else // we failed but have no error to explain why
2668            {
2669                error.SetErrorString("unknown error while importing module");
2670            }
2671
2672            // anyway, clear the error indicator and return false
2673            PyErr_Clear();
2674            return false;
2675        }
2676
2677        // if we are here, everything worked
2678        // call __lldb_init_module(debugger,dict)
2679        if (!g_swig_call_module_init (basename.c_str(),
2680                                      m_dictionary_name.c_str(),
2681                                      debugger_sp))
2682        {
2683            error.SetErrorString("calling __lldb_init_module failed");
2684            return false;
2685        }
2686        return true;
2687    }
2688}
2689
2690lldb::ScriptInterpreterObjectSP
2691ScriptInterpreterPython::MakeScriptObject (void* object)
2692{
2693    return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterPythonObject(object));
2694}
2695
2696ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp,
2697                                                                     ScriptedCommandSynchronicity synchro) :
2698    m_debugger_sp(debugger_sp),
2699    m_synch_wanted(synchro),
2700    m_old_asynch(debugger_sp->GetAsyncExecution())
2701{
2702    if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2703        m_debugger_sp->SetAsyncExecution(false);
2704    else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2705        m_debugger_sp->SetAsyncExecution(true);
2706}
2707
2708ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler()
2709{
2710    if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2711        m_debugger_sp->SetAsyncExecution(m_old_asynch);
2712}
2713
2714bool
2715ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
2716                                               const char* args,
2717                                               ScriptedCommandSynchronicity synchronicity,
2718                                               lldb_private::CommandReturnObject& cmd_retobj,
2719                                               Error& error)
2720{
2721    if (!impl_function)
2722    {
2723        error.SetErrorString("no function to execute");
2724        return false;
2725    }
2726
2727    if (!g_swig_call_command)
2728    {
2729        error.SetErrorString("no helper function to run scripted commands");
2730        return false;
2731    }
2732
2733    lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2734
2735    if (!debugger_sp.get())
2736    {
2737        error.SetErrorString("invalid Debugger pointer");
2738        return false;
2739    }
2740
2741    bool ret_val;
2742
2743    std::string err_msg;
2744
2745    {
2746        Locker py_lock(this,
2747                       Locker::AcquireLock | Locker::InitSession,
2748                       Locker::FreeLock    | Locker::TearDownSession);
2749
2750        SynchronicityHandler synch_handler(debugger_sp,
2751                                           synchronicity);
2752
2753        // we need to save the thread state when we first start the command
2754        // because we might decide to interrupt it while some action is taking
2755        // place outside of Python (e.g. printing to screen, waiting for the network, ...)
2756        // in that case, _PyThreadState_Current will be NULL - and we would be unable
2757        // to set the asynchronous exception - not a desirable situation
2758        m_command_thread_state = _PyThreadState_Current;
2759
2760        PythonInputReaderManager py_input(this);
2761
2762        ret_val = g_swig_call_command       (impl_function,
2763                                             m_dictionary_name.c_str(),
2764                                             debugger_sp,
2765                                             args,
2766                                             err_msg,
2767                                             cmd_retobj);
2768    }
2769
2770    if (!ret_val)
2771        error.SetErrorString(err_msg.c_str());
2772    else
2773        error.Clear();
2774
2775    return ret_val;
2776}
2777
2778// in Python, a special attribute __doc__ contains the docstring
2779// for an object (function, method, class, ...) if any is defined
2780// Otherwise, the attribute's value is None
2781bool
2782ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& dest)
2783{
2784	dest.clear();
2785	if (!item || !*item)
2786		return false;
2787    std::string command(item);
2788    command += ".__doc__";
2789
2790    char* result_ptr = NULL; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully
2791
2792    if (ExecuteOneLineWithReturn (command.c_str(),
2793                                  ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
2794                                  &result_ptr,
2795                                  ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false)))
2796    {
2797        if (result_ptr)
2798            dest.assign(result_ptr);
2799        return true;
2800    }
2801    else
2802    {
2803        StreamString str_stream;
2804        str_stream.Printf("Function %s was not found. Containing module might be missing.",item);
2805        dest.assign(str_stream.GetData());
2806        return false;
2807    }
2808}
2809
2810std::auto_ptr<ScriptInterpreterLocker>
2811ScriptInterpreterPython::AcquireInterpreterLock ()
2812{
2813    std::auto_ptr<ScriptInterpreterLocker> py_lock(new Locker(this,
2814                                                              Locker::AcquireLock | Locker::InitSession,
2815                                                              Locker::FreeLock | Locker::TearDownSession));
2816    return py_lock;
2817}
2818
2819void
2820ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_init_callback)
2821{
2822    g_swig_init_callback = python_swig_init_callback;
2823    g_swig_breakpoint_callback = LLDBSwigPythonBreakpointCallbackFunction;
2824    g_swig_watchpoint_callback = LLDBSwigPythonWatchpointCallbackFunction;
2825    g_swig_typescript_callback = LLDBSwigPythonCallTypeScript;
2826    g_swig_synthetic_script = LLDBSwigPythonCreateSyntheticProvider;
2827    g_swig_calc_children = LLDBSwigPython_CalculateNumChildren;
2828    g_swig_get_child_index = LLDBSwigPython_GetChildAtIndex;
2829    g_swig_get_index_child = LLDBSwigPython_GetIndexOfChildWithName;
2830    g_swig_cast_to_sbvalue = LLDBSWIGPython_CastPyObjectToSBValue;
2831    g_swig_update_provider = LLDBSwigPython_UpdateSynthProviderInstance;
2832    g_swig_mighthavechildren_provider = LLDBSwigPython_MightHaveChildrenSynthProviderInstance;
2833    g_swig_call_command = LLDBSwigPythonCallCommand;
2834    g_swig_call_module_init = LLDBSwigPythonCallModuleInit;
2835    g_swig_create_os_plugin = LLDBSWIGPythonCreateOSPlugin;
2836}
2837
2838void
2839ScriptInterpreterPython::InitializePrivate ()
2840{
2841    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
2842
2843    // Python will muck with STDIN terminal state, so save off any current TTY
2844    // settings so we can restore them.
2845    TerminalState stdin_tty_state;
2846    stdin_tty_state.Save(STDIN_FILENO, false);
2847
2848    PyGILState_STATE gstate;
2849    Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_SCRIPT | LIBLLDB_LOG_VERBOSE));
2850    bool threads_already_initialized = false;
2851    if (PyEval_ThreadsInitialized ()) {
2852        gstate = PyGILState_Ensure ();
2853        if (log)
2854            log->Printf("Ensured PyGILState. Previous state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : "");
2855        threads_already_initialized = true;
2856    } else {
2857        // InitThreads acquires the GIL if it hasn't been called before.
2858        PyEval_InitThreads ();
2859    }
2860    Py_InitializeEx (0);
2861
2862    // Initialize SWIG after setting up python
2863    assert (g_swig_init_callback != NULL);
2864    g_swig_init_callback ();
2865
2866    // Update the path python uses to search for modules to include the current directory.
2867
2868    PyRun_SimpleString ("import sys");
2869    PyRun_SimpleString ("sys.path.append ('.')");
2870
2871    // Find the module that owns this code and use that path we get to
2872    // set the sys.path appropriately.
2873
2874    FileSpec file_spec;
2875    char python_dir_path[PATH_MAX];
2876    if (Host::GetLLDBPath (ePathTypePythonDir, file_spec))
2877    {
2878        std::string python_path("sys.path.insert(0,\"");
2879        size_t orig_len = python_path.length();
2880        if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
2881        {
2882            python_path.append (python_dir_path);
2883            python_path.append ("\")");
2884            PyRun_SimpleString (python_path.c_str());
2885            python_path.resize (orig_len);
2886        }
2887
2888        if (Host::GetLLDBPath (ePathTypeLLDBShlibDir, file_spec))
2889        {
2890            if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
2891            {
2892                python_path.append (python_dir_path);
2893                python_path.append ("\")");
2894                PyRun_SimpleString (python_path.c_str());
2895                python_path.resize (orig_len);
2896            }
2897        }
2898    }
2899
2900    PyRun_SimpleString ("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line; from termios import *");
2901
2902    if (threads_already_initialized) {
2903        if (log)
2904            log->Printf("Releasing PyGILState. Returning to state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : "");
2905        PyGILState_Release (gstate);
2906    } else {
2907        // We initialized the threads in this function, just unlock the GIL.
2908        PyEval_SaveThread();
2909    }
2910
2911    stdin_tty_state.Restore();
2912}
2913
2914//void
2915//ScriptInterpreterPython::Terminate ()
2916//{
2917//    // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it).  Calling
2918//    // Py_Finalize here causes test suite runs to seg fault:  The test suite runs in Python.  It registers
2919//    // SBDebugger::Terminate to be called 'at_exit'.  When the test suite Python harness finishes up, it calls
2920//    // Py_Finalize, which calls all the 'at_exit' registered functions.  SBDebugger::Terminate calls Debugger::Terminate,
2921//    // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls
2922//    // ScriptInterpreterPython::Terminate.  So if we call Py_Finalize here, we end up with Py_Finalize being called from
2923//    // within Py_Finalize, which results in a seg fault.
2924//    //
2925//    // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't
2926//    // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the
2927//    // process exits).
2928//    //
2929////    Py_Finalize ();
2930//}
2931
2932#endif // #ifdef LLDB_DISABLE_PYTHON
2933