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