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