xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (revision 6eec8d6c6f2c179ecea70087bf5f5dff3052dd4b)
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 ConstString
2353 ScriptInterpreterPython::GetSyntheticTypeName (const StructuredData::ObjectSP &implementor_sp)
2354 {
2355     Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2356 
2357     static char callee_name[] = "get_type_name";
2358 
2359     ConstString ret_val;
2360     bool got_string = false;
2361     std::string buffer;
2362 
2363     if (!implementor_sp)
2364         return ret_val;
2365 
2366     StructuredData::Generic *generic = implementor_sp->GetAsGeneric();
2367     if (!generic)
2368         return ret_val;
2369     PythonObject implementor(PyRefType::Borrowed, (PyObject *)generic->GetValue());
2370     if (!implementor.IsAllocated())
2371         return ret_val;
2372 
2373     PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2374 
2375     if (PyErr_Occurred())
2376         PyErr_Clear();
2377 
2378     if (!pmeth.IsAllocated())
2379         return ret_val;
2380 
2381     if (PyCallable_Check(pmeth.get()) == 0)
2382     {
2383         if (PyErr_Occurred())
2384             PyErr_Clear();
2385         return ret_val;
2386     }
2387 
2388     if (PyErr_Occurred())
2389         PyErr_Clear();
2390 
2391     // right now we know this function exists and is callable..
2392     PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
2393 
2394     // if it fails, print the error but otherwise go on
2395     if (PyErr_Occurred())
2396     {
2397         PyErr_Print();
2398         PyErr_Clear();
2399     }
2400 
2401     if (py_return.IsAllocated() && PythonString::Check(py_return.get()))
2402     {
2403         PythonString py_string(PyRefType::Borrowed, py_return.get());
2404         llvm::StringRef return_data(py_string.GetString());
2405         if (!return_data.empty())
2406         {
2407             buffer.assign(return_data.data(), return_data.size());
2408             got_string = true;
2409         }
2410     }
2411 
2412     if (got_string)
2413         ret_val.SetCStringWithLength(buffer.c_str(), buffer.size());
2414 
2415     return ret_val;
2416 }
2417 
2418 bool
2419 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2420                                                  Process* process,
2421                                                  std::string& output,
2422                                                  Error& error)
2423 {
2424     bool ret_val;
2425     if (!process)
2426     {
2427         error.SetErrorString("no process");
2428         return false;
2429     }
2430     if (!impl_function || !impl_function[0])
2431     {
2432         error.SetErrorString("no function to execute");
2433         return false;
2434     }
2435     if (!g_swig_run_script_keyword_process)
2436     {
2437         error.SetErrorString("internal helper function missing");
2438         return false;
2439     }
2440     {
2441         ProcessSP process_sp(process->shared_from_this());
2442         Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2443         ret_val = g_swig_run_script_keyword_process (impl_function, m_dictionary_name.c_str(), process_sp, output);
2444         if (!ret_val)
2445             error.SetErrorString("python script evaluation failed");
2446     }
2447     return ret_val;
2448 }
2449 
2450 bool
2451 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2452                                                  Thread* thread,
2453                                                  std::string& output,
2454                                                  Error& error)
2455 {
2456     bool ret_val;
2457     if (!thread)
2458     {
2459         error.SetErrorString("no thread");
2460         return false;
2461     }
2462     if (!impl_function || !impl_function[0])
2463     {
2464         error.SetErrorString("no function to execute");
2465         return false;
2466     }
2467     if (!g_swig_run_script_keyword_thread)
2468     {
2469         error.SetErrorString("internal helper function missing");
2470         return false;
2471     }
2472     {
2473         ThreadSP thread_sp(thread->shared_from_this());
2474         Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2475         ret_val = g_swig_run_script_keyword_thread (impl_function, m_dictionary_name.c_str(), thread_sp, output);
2476         if (!ret_val)
2477             error.SetErrorString("python script evaluation failed");
2478     }
2479     return ret_val;
2480 }
2481 
2482 bool
2483 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2484                                                  Target* target,
2485                                                  std::string& output,
2486                                                  Error& error)
2487 {
2488     bool ret_val;
2489     if (!target)
2490     {
2491         error.SetErrorString("no thread");
2492         return false;
2493     }
2494     if (!impl_function || !impl_function[0])
2495     {
2496         error.SetErrorString("no function to execute");
2497         return false;
2498     }
2499     if (!g_swig_run_script_keyword_target)
2500     {
2501         error.SetErrorString("internal helper function missing");
2502         return false;
2503     }
2504     {
2505         TargetSP target_sp(target->shared_from_this());
2506         Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2507         ret_val = g_swig_run_script_keyword_target (impl_function, m_dictionary_name.c_str(), target_sp, output);
2508         if (!ret_val)
2509             error.SetErrorString("python script evaluation failed");
2510     }
2511     return ret_val;
2512 }
2513 
2514 bool
2515 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2516                                                  StackFrame* frame,
2517                                                  std::string& output,
2518                                                  Error& error)
2519 {
2520     bool ret_val;
2521     if (!frame)
2522     {
2523         error.SetErrorString("no frame");
2524         return false;
2525     }
2526     if (!impl_function || !impl_function[0])
2527     {
2528         error.SetErrorString("no function to execute");
2529         return false;
2530     }
2531     if (!g_swig_run_script_keyword_frame)
2532     {
2533         error.SetErrorString("internal helper function missing");
2534         return false;
2535     }
2536     {
2537         StackFrameSP frame_sp(frame->shared_from_this());
2538         Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2539         ret_val = g_swig_run_script_keyword_frame (impl_function, m_dictionary_name.c_str(), frame_sp, output);
2540         if (!ret_val)
2541             error.SetErrorString("python script evaluation failed");
2542     }
2543     return ret_val;
2544 }
2545 
2546 bool
2547 ScriptInterpreterPython::RunScriptFormatKeyword (const char* impl_function,
2548                                                  ValueObject *value,
2549                                                  std::string& output,
2550                                                  Error& error)
2551 {
2552     bool ret_val;
2553     if (!value)
2554     {
2555         error.SetErrorString("no value");
2556         return false;
2557     }
2558     if (!impl_function || !impl_function[0])
2559     {
2560         error.SetErrorString("no function to execute");
2561         return false;
2562     }
2563     if (!g_swig_run_script_keyword_value)
2564     {
2565         error.SetErrorString("internal helper function missing");
2566         return false;
2567     }
2568     {
2569         ValueObjectSP value_sp(value->GetSP());
2570         Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN);
2571         ret_val = g_swig_run_script_keyword_value (impl_function, m_dictionary_name.c_str(), value_sp, output);
2572         if (!ret_val)
2573             error.SetErrorString("python script evaluation failed");
2574     }
2575     return ret_val;
2576 }
2577 
2578 uint64_t replace_all(std::string& str, const std::string& oldStr, const std::string& newStr)
2579 {
2580     size_t pos = 0;
2581     uint64_t matches = 0;
2582     while((pos = str.find(oldStr, pos)) != std::string::npos)
2583     {
2584         matches++;
2585         str.replace(pos, oldStr.length(), newStr);
2586         pos += newStr.length();
2587     }
2588     return matches;
2589 }
2590 
2591 bool
2592 ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_reload, bool init_session, lldb_private::Error &error,
2593                                              StructuredData::ObjectSP *module_sp)
2594 {
2595     if (!pathname || !pathname[0])
2596     {
2597         error.SetErrorString("invalid pathname");
2598         return false;
2599     }
2600 
2601     if (!g_swig_call_module_init)
2602     {
2603         error.SetErrorString("internal helper function missing");
2604         return false;
2605     }
2606 
2607     lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2608 
2609     {
2610         FileSpec target_file(pathname, true);
2611         std::string basename(target_file.GetFilename().GetCString());
2612 
2613         StreamString command_stream;
2614 
2615         // Before executing Python code, lock the GIL.
2616         Locker py_lock (this,
2617                         Locker::AcquireLock      | (init_session ? Locker::InitSession     : 0) | Locker::NoSTDIN,
2618                         Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
2619 
2620         if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
2621             target_file.GetFileType() == FileSpec::eFileTypeUnknown)
2622         {
2623             // if not a valid file of any sort, check if it might be a filename still
2624             // dot can't be used but / and \ can, and if either is found, reject
2625             if (strchr(pathname,'\\') || strchr(pathname,'/'))
2626             {
2627                 error.SetErrorString("invalid pathname");
2628                 return false;
2629             }
2630             basename = pathname; // not a filename, probably a package of some sort, let it go through
2631         }
2632         else if (target_file.GetFileType() == FileSpec::eFileTypeDirectory ||
2633                  target_file.GetFileType() == FileSpec::eFileTypeRegular ||
2634                  target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink)
2635         {
2636             std::string directory = target_file.GetDirectory().GetCString();
2637             replace_all(directory, "\\", "\\\\");
2638             replace_all(directory, "'", "\\'");
2639 
2640             // now make sure that Python has "directory" in the search path
2641             StreamString command_stream;
2642             command_stream.Printf("if not (sys.path.__contains__('%s')):\n    sys.path.insert(1,'%s');\n\n",
2643                                   directory.c_str(),
2644                                   directory.c_str());
2645             bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)).Success();
2646             if (!syspath_retval)
2647             {
2648                 error.SetErrorString("Python sys.path handling failed");
2649                 return false;
2650             }
2651 
2652             // strip .py or .pyc extension
2653             ConstString extension = target_file.GetFileNameExtension();
2654             if (extension)
2655             {
2656                 if (::strcmp(extension.GetCString(), "py") == 0)
2657                     basename.resize(basename.length()-3);
2658                 else if(::strcmp(extension.GetCString(), "pyc") == 0)
2659                     basename.resize(basename.length()-4);
2660             }
2661         }
2662         else
2663         {
2664             error.SetErrorString("no known way to import this module specification");
2665             return false;
2666         }
2667 
2668         // check if the module is already import-ed
2669         command_stream.Clear();
2670         command_stream.Printf("sys.modules.__contains__('%s')",basename.c_str());
2671         bool does_contain = false;
2672         // this call will succeed if the module was ever imported in any Debugger in the lifetime of the process
2673         // in which this LLDB framework is living
2674         bool was_imported_globally = (ExecuteOneLineWithReturn(command_stream.GetData(),
2675                                                                ScriptInterpreterPython::eScriptReturnTypeBool,
2676                                                                &does_contain,
2677                                                                ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)) && does_contain);
2678         // this call will fail if the module was not imported in this Debugger before
2679         command_stream.Clear();
2680         command_stream.Printf("sys.getrefcount(%s)",basename.c_str());
2681         bool was_imported_locally = GetSessionDictionary().GetItemForKey(PythonString(basename)).IsAllocated();
2682 
2683         bool was_imported = (was_imported_globally || was_imported_locally);
2684 
2685         if (was_imported == true && can_reload == false)
2686         {
2687             error.SetErrorString("module already imported");
2688             return false;
2689         }
2690 
2691         // now actually do the import
2692         command_stream.Clear();
2693 
2694         if (was_imported)
2695         {
2696             if (!was_imported_locally)
2697                 command_stream.Printf("import %s ; reload_module(%s)",basename.c_str(),basename.c_str());
2698             else
2699                 command_stream.Printf("reload_module(%s)",basename.c_str());
2700         }
2701         else
2702             command_stream.Printf("import %s", basename.c_str());
2703 
2704         error = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
2705         if (error.Fail())
2706             return false;
2707 
2708         // if we are here, everything worked
2709         // call __lldb_init_module(debugger,dict)
2710         if (!g_swig_call_module_init (basename.c_str(),
2711                                       m_dictionary_name.c_str(),
2712                                       debugger_sp))
2713         {
2714             error.SetErrorString("calling __lldb_init_module failed");
2715             return false;
2716         }
2717 
2718         if (module_sp)
2719         {
2720             // everything went just great, now set the module object
2721             command_stream.Clear();
2722             command_stream.Printf("%s",basename.c_str());
2723             void* module_pyobj = nullptr;
2724             if (ExecuteOneLineWithReturn(command_stream.GetData(),ScriptInterpreter::eScriptReturnTypeOpaqueObject,&module_pyobj) && module_pyobj)
2725                 module_sp->reset(new StructuredPythonObject(module_pyobj));
2726         }
2727 
2728         return true;
2729     }
2730 }
2731 
2732 bool
2733 ScriptInterpreterPython::IsReservedWord (const char* word)
2734 {
2735     if (!word || !word[0])
2736         return false;
2737 
2738     llvm::StringRef word_sr(word);
2739 
2740     // filter out a few characters that would just confuse us
2741     // and that are clearly not keyword material anyway
2742     if (word_sr.find_first_of("'\"") != llvm::StringRef::npos)
2743         return false;
2744 
2745     StreamString command_stream;
2746     command_stream.Printf("keyword.iskeyword('%s')", word);
2747     bool result;
2748     ExecuteScriptOptions options;
2749     options.SetEnableIO(false);
2750     options.SetMaskoutErrors(true);
2751     options.SetSetLLDBGlobals(false);
2752     if (ExecuteOneLineWithReturn(command_stream.GetData(), ScriptInterpreter::eScriptReturnTypeBool, &result, options))
2753         return result;
2754     return false;
2755 }
2756 
2757 ScriptInterpreterPython::SynchronicityHandler::SynchronicityHandler (lldb::DebuggerSP debugger_sp,
2758                                                                      ScriptedCommandSynchronicity synchro) :
2759     m_debugger_sp(debugger_sp),
2760     m_synch_wanted(synchro),
2761     m_old_asynch(debugger_sp->GetAsyncExecution())
2762 {
2763     if (m_synch_wanted == eScriptedCommandSynchronicitySynchronous)
2764         m_debugger_sp->SetAsyncExecution(false);
2765     else if (m_synch_wanted == eScriptedCommandSynchronicityAsynchronous)
2766         m_debugger_sp->SetAsyncExecution(true);
2767 }
2768 
2769 ScriptInterpreterPython::SynchronicityHandler::~SynchronicityHandler()
2770 {
2771     if (m_synch_wanted != eScriptedCommandSynchronicityCurrentValue)
2772         m_debugger_sp->SetAsyncExecution(m_old_asynch);
2773 }
2774 
2775 bool
2776 ScriptInterpreterPython::RunScriptBasedCommand(const char* impl_function,
2777                                                const char* args,
2778                                                ScriptedCommandSynchronicity synchronicity,
2779                                                lldb_private::CommandReturnObject& cmd_retobj,
2780                                                Error& error,
2781                                                const lldb_private::ExecutionContext& exe_ctx)
2782 {
2783     if (!impl_function)
2784     {
2785         error.SetErrorString("no function to execute");
2786         return false;
2787     }
2788 
2789     if (!g_swig_call_command)
2790     {
2791         error.SetErrorString("no helper function to run scripted commands");
2792         return false;
2793     }
2794 
2795     lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2796     lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2797 
2798     if (!debugger_sp.get())
2799     {
2800         error.SetErrorString("invalid Debugger pointer");
2801         return false;
2802    }
2803 
2804     bool ret_val = false;
2805 
2806     std::string err_msg;
2807 
2808     {
2809         Locker py_lock(this,
2810                        Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2811                        Locker::FreeLock    | Locker::TearDownSession);
2812 
2813         SynchronicityHandler synch_handler(debugger_sp,
2814                                            synchronicity);
2815 
2816         ret_val = g_swig_call_command       (impl_function,
2817                                              m_dictionary_name.c_str(),
2818                                              debugger_sp,
2819                                              args,
2820                                              cmd_retobj,
2821                                              exe_ctx_ref_sp);
2822     }
2823 
2824     if (!ret_val)
2825         error.SetErrorString("unable to execute script function");
2826     else
2827         error.Clear();
2828 
2829     return ret_val;
2830 }
2831 
2832 bool
2833 ScriptInterpreterPython::RunScriptBasedCommand (StructuredData::GenericSP impl_obj_sp,
2834                                                 const char* args,
2835                                                 ScriptedCommandSynchronicity synchronicity,
2836                                                 lldb_private::CommandReturnObject& cmd_retobj,
2837                                                 Error& error,
2838                                                 const lldb_private::ExecutionContext& exe_ctx)
2839 {
2840     if (!impl_obj_sp || !impl_obj_sp->IsValid())
2841     {
2842         error.SetErrorString("no function to execute");
2843         return false;
2844     }
2845 
2846     if (!g_swig_call_command_object)
2847     {
2848         error.SetErrorString("no helper function to run scripted commands");
2849         return false;
2850     }
2851 
2852     lldb::DebuggerSP debugger_sp = m_interpreter.GetDebugger().shared_from_this();
2853     lldb::ExecutionContextRefSP exe_ctx_ref_sp(new ExecutionContextRef(exe_ctx));
2854 
2855     if (!debugger_sp.get())
2856     {
2857         error.SetErrorString("invalid Debugger pointer");
2858         return false;
2859     }
2860 
2861     bool ret_val = false;
2862 
2863     std::string err_msg;
2864 
2865     {
2866         Locker py_lock(this,
2867                        Locker::AcquireLock | Locker::InitSession | (cmd_retobj.GetInteractive() ? 0 : Locker::NoSTDIN),
2868                        Locker::FreeLock    | Locker::TearDownSession);
2869 
2870         SynchronicityHandler synch_handler(debugger_sp,
2871                                            synchronicity);
2872 
2873         ret_val = g_swig_call_command_object      (impl_obj_sp->GetValue(),
2874                                                    debugger_sp,
2875                                                    args,
2876                                                    cmd_retobj,
2877                                                    exe_ctx_ref_sp);
2878     }
2879 
2880     if (!ret_val)
2881         error.SetErrorString("unable to execute script function");
2882     else
2883         error.Clear();
2884 
2885     return ret_val;
2886 }
2887 
2888 // in Python, a special attribute __doc__ contains the docstring
2889 // for an object (function, method, class, ...) if any is defined
2890 // Otherwise, the attribute's value is None
2891 bool
2892 ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& dest)
2893 {
2894 	dest.clear();
2895 	if (!item || !*item)
2896 		return false;
2897     std::string command(item);
2898     command += ".__doc__";
2899 
2900     char* result_ptr = nullptr; // Python is going to point this to valid data if ExecuteOneLineWithReturn returns successfully
2901 
2902     if (ExecuteOneLineWithReturn (command.c_str(),
2903                                   ScriptInterpreter::eScriptReturnTypeCharStrOrNone,
2904                                   &result_ptr,
2905                                   ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false)))
2906     {
2907         if (result_ptr)
2908             dest.assign(result_ptr);
2909         return true;
2910     }
2911     else
2912     {
2913         StreamString str_stream;
2914         str_stream.Printf("Function %s was not found. Containing module might be missing.",item);
2915         dest.assign(str_stream.GetData());
2916         return false;
2917     }
2918 }
2919 
2920 bool
2921 ScriptInterpreterPython::GetShortHelpForCommandObject (StructuredData::GenericSP cmd_obj_sp,
2922                                                        std::string& dest)
2923 {
2924     bool got_string = false;
2925     dest.clear();
2926 
2927     Locker py_lock (this,
2928                     Locker::AcquireLock | Locker::NoSTDIN,
2929                     Locker::FreeLock);
2930 
2931     static char callee_name[] = "get_short_help";
2932 
2933     if (!cmd_obj_sp)
2934         return false;
2935 
2936     PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
2937 
2938     if (!implementor.IsAllocated())
2939         return false;
2940 
2941     PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2942 
2943     if (PyErr_Occurred())
2944         PyErr_Clear();
2945 
2946     if (!pmeth.IsAllocated())
2947         return false;
2948 
2949     if (PyCallable_Check(pmeth.get()) == 0)
2950     {
2951         if (PyErr_Occurred())
2952             PyErr_Clear();
2953         return false;
2954     }
2955 
2956     if (PyErr_Occurred())
2957         PyErr_Clear();
2958 
2959     // right now we know this function exists and is callable..
2960     PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
2961 
2962     // if it fails, print the error but otherwise go on
2963     if (PyErr_Occurred())
2964     {
2965         PyErr_Print();
2966         PyErr_Clear();
2967     }
2968 
2969     if (py_return.IsAllocated() && PythonString::Check(py_return.get()))
2970     {
2971         PythonString py_string(PyRefType::Borrowed, py_return.get());
2972         llvm::StringRef return_data(py_string.GetString());
2973         dest.assign(return_data.data(), return_data.size());
2974         got_string = true;
2975     }
2976     return got_string;
2977 }
2978 
2979 uint32_t
2980 ScriptInterpreterPython::GetFlagsForCommandObject (StructuredData::GenericSP cmd_obj_sp)
2981 {
2982     uint32_t result = 0;
2983 
2984     Locker py_lock (this,
2985                     Locker::AcquireLock | Locker::NoSTDIN,
2986                     Locker::FreeLock);
2987 
2988     static char callee_name[] = "get_flags";
2989 
2990     if (!cmd_obj_sp)
2991         return result;
2992 
2993     PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
2994 
2995     if (!implementor.IsAllocated())
2996         return result;
2997 
2998     PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
2999 
3000     if (PyErr_Occurred())
3001         PyErr_Clear();
3002 
3003     if (!pmeth.IsAllocated())
3004         return result;
3005 
3006     if (PyCallable_Check(pmeth.get()) == 0)
3007     {
3008         if (PyErr_Occurred())
3009             PyErr_Clear();
3010         return result;
3011     }
3012 
3013     if (PyErr_Occurred())
3014         PyErr_Clear();
3015 
3016     // right now we know this function exists and is callable..
3017     PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3018 
3019     // if it fails, print the error but otherwise go on
3020     if (PyErr_Occurred())
3021     {
3022         PyErr_Print();
3023         PyErr_Clear();
3024     }
3025 
3026     if (py_return.IsAllocated() && PythonInteger::Check(py_return.get()))
3027     {
3028         PythonInteger int_value(PyRefType::Borrowed, py_return.get());
3029         result = int_value.GetInteger();
3030     }
3031 
3032     return result;
3033 }
3034 
3035 bool
3036 ScriptInterpreterPython::GetLongHelpForCommandObject (StructuredData::GenericSP cmd_obj_sp,
3037                                                       std::string& dest)
3038 {
3039     bool got_string = false;
3040     dest.clear();
3041 
3042     Locker py_lock (this,
3043                     Locker::AcquireLock | Locker::NoSTDIN,
3044                     Locker::FreeLock);
3045 
3046     static char callee_name[] = "get_long_help";
3047 
3048     if (!cmd_obj_sp)
3049         return false;
3050 
3051     PythonObject implementor(PyRefType::Borrowed, (PyObject *)cmd_obj_sp->GetValue());
3052 
3053     if (!implementor.IsAllocated())
3054         return false;
3055 
3056     PythonObject pmeth(PyRefType::Owned, PyObject_GetAttrString(implementor.get(), callee_name));
3057 
3058     if (PyErr_Occurred())
3059         PyErr_Clear();
3060 
3061     if (!pmeth.IsAllocated())
3062         return false;
3063 
3064     if (PyCallable_Check(pmeth.get()) == 0)
3065     {
3066         if (PyErr_Occurred())
3067             PyErr_Clear();
3068 
3069         return false;
3070     }
3071 
3072     if (PyErr_Occurred())
3073         PyErr_Clear();
3074 
3075     // right now we know this function exists and is callable..
3076     PythonObject py_return(PyRefType::Owned, PyObject_CallMethod(implementor.get(), callee_name, nullptr));
3077 
3078     // if it fails, print the error but otherwise go on
3079     if (PyErr_Occurred())
3080     {
3081         PyErr_Print();
3082         PyErr_Clear();
3083     }
3084 
3085     if (py_return.IsAllocated() && PythonString::Check(py_return.get()))
3086     {
3087         PythonString str(PyRefType::Borrowed, py_return.get());
3088         llvm::StringRef str_data(str.GetString());
3089         dest.assign(str_data.data(), str_data.size());
3090         got_string = true;
3091     }
3092 
3093     return got_string;
3094 }
3095 
3096 std::unique_ptr<ScriptInterpreterLocker>
3097 ScriptInterpreterPython::AcquireInterpreterLock ()
3098 {
3099     std::unique_ptr<ScriptInterpreterLocker> py_lock(new Locker(this,
3100                                                                 Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN,
3101                                                                 Locker::FreeLock | Locker::TearDownSession));
3102     return py_lock;
3103 }
3104 
3105 void
3106 ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback swig_init_callback,
3107                                                 SWIGBreakpointCallbackFunction swig_breakpoint_callback,
3108                                                 SWIGWatchpointCallbackFunction swig_watchpoint_callback,
3109                                                 SWIGPythonTypeScriptCallbackFunction swig_typescript_callback,
3110                                                 SWIGPythonCreateSyntheticProvider swig_synthetic_script,
3111                                                 SWIGPythonCreateCommandObject swig_create_cmd,
3112                                                 SWIGPythonCalculateNumChildren swig_calc_children,
3113                                                 SWIGPythonGetChildAtIndex swig_get_child_index,
3114                                                 SWIGPythonGetIndexOfChildWithName swig_get_index_child,
3115                                                 SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue ,
3116                                                 SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue,
3117                                                 SWIGPythonUpdateSynthProviderInstance swig_update_provider,
3118                                                 SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider,
3119                                                 SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider,
3120                                                 SWIGPythonCallCommand swig_call_command,
3121                                                 SWIGPythonCallCommandObject swig_call_command_object,
3122                                                 SWIGPythonCallModuleInit swig_call_module_init,
3123                                                 SWIGPythonCreateOSPlugin swig_create_os_plugin,
3124                                                 SWIGPythonScriptKeyword_Process swig_run_script_keyword_process,
3125                                                 SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread,
3126                                                 SWIGPythonScriptKeyword_Target swig_run_script_keyword_target,
3127                                                 SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame,
3128                                                 SWIGPythonScriptKeyword_Value swig_run_script_keyword_value,
3129                                                 SWIGPython_GetDynamicSetting swig_plugin_get,
3130                                                 SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script,
3131                                                 SWIGPythonCallThreadPlan swig_call_thread_plan)
3132 {
3133     g_swig_init_callback = swig_init_callback;
3134     g_swig_breakpoint_callback = swig_breakpoint_callback;
3135     g_swig_watchpoint_callback = swig_watchpoint_callback;
3136     g_swig_typescript_callback = swig_typescript_callback;
3137     g_swig_synthetic_script = swig_synthetic_script;
3138     g_swig_create_cmd = swig_create_cmd;
3139     g_swig_calc_children = swig_calc_children;
3140     g_swig_get_child_index = swig_get_child_index;
3141     g_swig_get_index_child = swig_get_index_child;
3142     g_swig_cast_to_sbvalue = swig_cast_to_sbvalue;
3143     g_swig_get_valobj_sp_from_sbvalue = swig_get_valobj_sp_from_sbvalue;
3144     g_swig_update_provider = swig_update_provider;
3145     g_swig_mighthavechildren_provider = swig_mighthavechildren_provider;
3146     g_swig_getvalue_provider = swig_getvalue_provider;
3147     g_swig_call_command = swig_call_command;
3148     g_swig_call_command_object = swig_call_command_object;
3149     g_swig_call_module_init = swig_call_module_init;
3150     g_swig_create_os_plugin = swig_create_os_plugin;
3151     g_swig_run_script_keyword_process = swig_run_script_keyword_process;
3152     g_swig_run_script_keyword_thread = swig_run_script_keyword_thread;
3153     g_swig_run_script_keyword_target = swig_run_script_keyword_target;
3154     g_swig_run_script_keyword_frame = swig_run_script_keyword_frame;
3155     g_swig_run_script_keyword_value = swig_run_script_keyword_value;
3156     g_swig_plugin_get = swig_plugin_get;
3157     g_swig_thread_plan_script = swig_thread_plan_script;
3158     g_swig_call_thread_plan = swig_call_thread_plan;
3159 }
3160 
3161 void
3162 ScriptInterpreterPython::InitializePrivate ()
3163 {
3164     if (g_initialized)
3165         return;
3166 
3167     g_initialized = true;
3168 
3169     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
3170 
3171     // RAII-based initialization which correctly handles multiple-initialization, version-
3172     // specific differences among Python 2 and Python 3, and saving and restoring various
3173     // other pieces of state that can get mucked with during initialization.
3174     InitializePythonRAII initialize_guard;
3175 
3176     if (g_swig_init_callback)
3177         g_swig_init_callback ();
3178 
3179     // Update the path python uses to search for modules to include the current directory.
3180 
3181     PyRun_SimpleString ("import sys");
3182     AddToSysPath(AddLocation::End, ".");
3183 
3184     FileSpec file_spec;
3185     // Don't denormalize paths when calling file_spec.GetPath().  On platforms that use
3186     // a backslash as the path separator, this will result in executing python code containing
3187     // paths with unescaped backslashes.  But Python also accepts forward slashes, so to make
3188     // life easier we just use that.
3189     if (HostInfo::GetLLDBPath(ePathTypePythonDir, file_spec))
3190         AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3191     if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, file_spec))
3192         AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
3193 
3194     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");
3195 }
3196 
3197 void
3198 ScriptInterpreterPython::AddToSysPath(AddLocation location, std::string path)
3199 {
3200     std::string path_copy;
3201 
3202     std::string statement;
3203     if (location == AddLocation::Beginning)
3204     {
3205         statement.assign("sys.path.insert(0,\"");
3206         statement.append (path);
3207         statement.append ("\")");
3208     }
3209     else
3210     {
3211         statement.assign("sys.path.append(\"");
3212         statement.append(path);
3213         statement.append("\")");
3214     }
3215     PyRun_SimpleString (statement.c_str());
3216 }
3217 
3218 
3219 //void
3220 //ScriptInterpreterPython::Terminate ()
3221 //{
3222 //    // We are intentionally NOT calling Py_Finalize here (this would be the logical place to call it).  Calling
3223 //    // Py_Finalize here causes test suite runs to seg fault:  The test suite runs in Python.  It registers
3224 //    // SBDebugger::Terminate to be called 'at_exit'.  When the test suite Python harness finishes up, it calls
3225 //    // Py_Finalize, which calls all the 'at_exit' registered functions.  SBDebugger::Terminate calls Debugger::Terminate,
3226 //    // which calls lldb::Terminate, which calls ScriptInterpreter::Terminate, which calls
3227 //    // ScriptInterpreterPython::Terminate.  So if we call Py_Finalize here, we end up with Py_Finalize being called from
3228 //    // within Py_Finalize, which results in a seg fault.
3229 //    //
3230 //    // Since this function only gets called when lldb is shutting down and going away anyway, the fact that we don't
3231 //    // actually call Py_Finalize should not cause any problems (everything should shut down/go away anyway when the
3232 //    // process exits).
3233 //    //
3234 ////    Py_Finalize ();
3235 //}
3236 
3237 #endif // #ifdef LLDB_DISABLE_PYTHON
3238