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