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