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