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