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