1b3e77600SGreg Clayton //===-- OperatingSystemPython.cpp --------------------------------*- C++ -*-===//
2b3e77600SGreg Clayton //
3b3e77600SGreg Clayton //                     The LLVM Compiler Infrastructure
4b3e77600SGreg Clayton //
5b3e77600SGreg Clayton // This file is distributed under the University of Illinois Open Source
6b3e77600SGreg Clayton // License. See LICENSE.TXT for details.
7b3e77600SGreg Clayton //
8b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
993a64300SDaniel Malea 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
12b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
13b3e77600SGreg Clayton 
14b3e77600SGreg Clayton #include "OperatingSystemPython.h"
15b3e77600SGreg Clayton // C Includes
16b3e77600SGreg Clayton // C++ Includes
17b3e77600SGreg Clayton // Other libraries and framework includes
18b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h"
19b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
205790759aSEnrico Granata #include "lldb/Core/Debugger.h"
21b3e77600SGreg Clayton #include "lldb/Core/Module.h"
22b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
23b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
24b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
255790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
265790759aSEnrico Granata #include "lldb/Interpreter/PythonDataObjects.h"
27b3e77600SGreg Clayton #include "lldb/Symbol/ClangNamespaceDecl.h"
28b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
29b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
30b3e77600SGreg Clayton #include "lldb/Target/Process.h"
31b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
32b3e77600SGreg Clayton #include "lldb/Target/Target.h"
33b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h"
34b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
35b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
36b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h"
37b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h"
38b3e77600SGreg Clayton 
39b3e77600SGreg Clayton using namespace lldb;
40b3e77600SGreg Clayton using namespace lldb_private;
41b3e77600SGreg Clayton 
42b3e77600SGreg Clayton void
43b3e77600SGreg Clayton OperatingSystemPython::Initialize()
44b3e77600SGreg Clayton {
45b3e77600SGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
46b3e77600SGreg Clayton                                    GetPluginDescriptionStatic(),
47b3e77600SGreg Clayton                                    CreateInstance);
48b3e77600SGreg Clayton }
49b3e77600SGreg Clayton 
50b3e77600SGreg Clayton void
51b3e77600SGreg Clayton OperatingSystemPython::Terminate()
52b3e77600SGreg Clayton {
53b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
54b3e77600SGreg Clayton }
55b3e77600SGreg Clayton 
56b3e77600SGreg Clayton OperatingSystem *
57b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
58b3e77600SGreg Clayton {
59b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
60c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
61c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
62c9d645d3SGreg Clayton     {
63*7b0992d9SGreg Clayton         std::unique_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
64c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
65c9d645d3SGreg Clayton             return os_ap.release();
66c9d645d3SGreg Clayton     }
67b3e77600SGreg Clayton     return NULL;
68b3e77600SGreg Clayton }
69b3e77600SGreg Clayton 
70b3e77600SGreg Clayton 
71b3e77600SGreg Clayton const char *
72b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
73b3e77600SGreg Clayton {
74b3e77600SGreg Clayton     return "python";
75b3e77600SGreg Clayton }
76b3e77600SGreg Clayton 
77b3e77600SGreg Clayton const char *
78b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
79b3e77600SGreg Clayton {
80b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
81b3e77600SGreg Clayton }
82b3e77600SGreg Clayton 
83b3e77600SGreg Clayton 
84c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
85b3e77600SGreg Clayton     OperatingSystem (process),
86b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
875790759aSEnrico Granata     m_register_info_ap (),
885790759aSEnrico Granata     m_interpreter (NULL),
89a4d8747dSGreg Clayton     m_python_object_sp ()
90b3e77600SGreg Clayton {
915790759aSEnrico Granata     if (!process)
925790759aSEnrico Granata         return;
93a4d8747dSGreg Clayton     TargetSP target_sp = process->CalculateTarget();
945790759aSEnrico Granata     if (!target_sp)
955790759aSEnrico Granata         return;
965790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
975790759aSEnrico Granata     if (m_interpreter)
985790759aSEnrico Granata     {
992443cbd7SGreg Clayton 
100c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
101c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
102c9d645d3SGreg Clayton         {
103c9d645d3SGreg Clayton             const bool init_session = false;
104c9d645d3SGreg Clayton             const bool allow_reload = true;
105c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
106c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
107c9d645d3SGreg Clayton             Error error;
108c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
109c9d645d3SGreg Clayton             {
110c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
111c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
112c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
113c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
114c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
115c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
116a4d8747dSGreg Clayton                 ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess());
117a4d8747dSGreg Clayton                 if (object_sp && object_sp->GetObject())
118a4d8747dSGreg Clayton                     m_python_object_sp = object_sp;
119c9d645d3SGreg Clayton             }
1202443cbd7SGreg Clayton         }
1215790759aSEnrico Granata     }
122b3e77600SGreg Clayton }
123b3e77600SGreg Clayton 
124b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython ()
125b3e77600SGreg Clayton {
126b3e77600SGreg Clayton }
127b3e77600SGreg Clayton 
128b3e77600SGreg Clayton DynamicRegisterInfo *
129b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo ()
130b3e77600SGreg Clayton {
1312443cbd7SGreg Clayton     if (m_register_info_ap.get() == NULL)
1322443cbd7SGreg Clayton     {
133a4d8747dSGreg Clayton         if (!m_interpreter || !m_python_object_sp)
1345790759aSEnrico Granata             return NULL;
1355160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1361c22be69SGreg Clayton 
1371c22be69SGreg Clayton         if (log)
138d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
1391c22be69SGreg Clayton 
140a4d8747dSGreg Clayton         PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp));
1415790759aSEnrico Granata         if (!dictionary)
1425790759aSEnrico Granata             return NULL;
143b3e77600SGreg Clayton 
1442443cbd7SGreg Clayton         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
1452443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisters() > 0);
1462443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisterSets() > 0);
147b3e77600SGreg Clayton     }
148b3e77600SGreg Clayton     return m_register_info_ap.get();
149b3e77600SGreg Clayton }
150b3e77600SGreg Clayton 
151b3e77600SGreg Clayton //------------------------------------------------------------------
152b3e77600SGreg Clayton // PluginInterface protocol
153b3e77600SGreg Clayton //------------------------------------------------------------------
154b3e77600SGreg Clayton const char *
155b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
156b3e77600SGreg Clayton {
157b3e77600SGreg Clayton     return "OperatingSystemPython";
158b3e77600SGreg Clayton }
159b3e77600SGreg Clayton 
160b3e77600SGreg Clayton const char *
161b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName()
162b3e77600SGreg Clayton {
163b3e77600SGreg Clayton     return GetPluginNameStatic();
164b3e77600SGreg Clayton }
165b3e77600SGreg Clayton 
166b3e77600SGreg Clayton uint32_t
167b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
168b3e77600SGreg Clayton {
169b3e77600SGreg Clayton     return 1;
170b3e77600SGreg Clayton }
171b3e77600SGreg Clayton 
172b3e77600SGreg Clayton bool
173b3e77600SGreg Clayton OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list, ThreadList &new_thread_list)
174b3e77600SGreg Clayton {
175a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
176a85e6b6cSDaniel Malea         return false;
1771c22be69SGreg Clayton 
1785160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1791c22be69SGreg Clayton 
18085e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
18185e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
18285e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
18385e276b8SJim Ingham     Target &target = m_process->GetTarget();
18485e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
18585e276b8SJim Ingham 
1861c22be69SGreg Clayton     if (log)
187d01b2953SDaniel Malea         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
1881c22be69SGreg Clayton 
189b3ae8761SGreg Clayton     // The threads that are in "new_thread_list" upon entry are the threads from the
190b3ae8761SGreg Clayton     // lldb_private::Process subclass, no memory threads will be in this list.
191b3ae8761SGreg Clayton 
192198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive
193a4d8747dSGreg Clayton     PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp));
194a4d8747dSGreg Clayton     if (threads_list)
195435ce139SGreg Clayton     {
196b3ae8761SGreg Clayton         ThreadList core_thread_list(new_thread_list);
197b3ae8761SGreg Clayton 
198b3ae8761SGreg Clayton         uint32_t i;
199a4d8747dSGreg Clayton         const uint32_t num_threads = threads_list.GetSize();
200b3ae8761SGreg Clayton         for (i=0; i<num_threads; ++i)
201435ce139SGreg Clayton         {
202a4d8747dSGreg Clayton             PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
203435ce139SGreg Clayton             if (thread_dict)
204435ce139SGreg Clayton             {
205b3ae8761SGreg Clayton                 if (thread_dict.GetItemForKey("core"))
206b3ae8761SGreg Clayton                 {
207b3ae8761SGreg Clayton                     // We have some threads that are saying they are on a "core", which means
208b3ae8761SGreg Clayton                     // they map the threads that are gotten from the lldb_private::Process subclass
209b3ae8761SGreg Clayton                     // so clear the new threads list so the core threads don't show up
210b3ae8761SGreg Clayton                     new_thread_list.Clear();
211b3ae8761SGreg Clayton                     break;
212b3ae8761SGreg Clayton                 }
213b3ae8761SGreg Clayton             }
214b3ae8761SGreg Clayton         }
215b3ae8761SGreg Clayton         for (i=0; i<num_threads; ++i)
216b3ae8761SGreg Clayton         {
217b3ae8761SGreg Clayton             PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
218b3ae8761SGreg Clayton             if (thread_dict)
219b3ae8761SGreg Clayton             {
220b3ae8761SGreg Clayton                 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL));
221a4d8747dSGreg Clayton                 if (thread_sp)
222435ce139SGreg Clayton                     new_thread_list.AddThread(thread_sp);
223435ce139SGreg Clayton             }
224435ce139SGreg Clayton         }
225435ce139SGreg Clayton     }
226435ce139SGreg Clayton     else
227435ce139SGreg Clayton     {
228b3e77600SGreg Clayton         new_thread_list = old_thread_list;
229435ce139SGreg Clayton     }
230b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
231b3e77600SGreg Clayton }
232b3e77600SGreg Clayton 
233a4d8747dSGreg Clayton ThreadSP
234b3ae8761SGreg Clayton OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
235b3ae8761SGreg Clayton                                                    ThreadList &core_thread_list,
236b3ae8761SGreg Clayton                                                    ThreadList &old_thread_list,
237b3ae8761SGreg Clayton                                                    bool *did_create_ptr)
238a4d8747dSGreg Clayton {
239a4d8747dSGreg Clayton     ThreadSP thread_sp;
240a4d8747dSGreg Clayton     if (thread_dict)
241a4d8747dSGreg Clayton     {
242a4d8747dSGreg Clayton         PythonString tid_pystr("tid");
243c7bece56SGreg Clayton         const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
244c7bece56SGreg Clayton         if (tid != LLDB_INVALID_THREAD_ID)
245c7bece56SGreg Clayton         {
246b3ae8761SGreg Clayton             PythonString core_pystr("core");
247a4d8747dSGreg Clayton             PythonString name_pystr("name");
248a4d8747dSGreg Clayton             PythonString queue_pystr("queue");
249a4d8747dSGreg Clayton             PythonString state_pystr("state");
250a4d8747dSGreg Clayton             PythonString stop_reason_pystr("stop_reason");
251a4d8747dSGreg Clayton             PythonString reg_data_addr_pystr ("register_data_addr");
252a4d8747dSGreg Clayton 
253b3ae8761SGreg Clayton             const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX);
254a4d8747dSGreg Clayton             const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
255a4d8747dSGreg Clayton             const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
256a4d8747dSGreg Clayton             const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
257a4d8747dSGreg Clayton             //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
258a4d8747dSGreg Clayton             //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
259a4d8747dSGreg Clayton 
260b3ae8761SGreg Clayton             thread_sp = old_thread_list.FindThreadByID (tid, false);
261a4d8747dSGreg Clayton             if (!thread_sp)
262a4d8747dSGreg Clayton             {
263a4d8747dSGreg Clayton                 if (did_create_ptr)
264a4d8747dSGreg Clayton                     *did_create_ptr = true;
265a4d8747dSGreg Clayton                 thread_sp.reset (new ThreadMemory (*m_process,
266a4d8747dSGreg Clayton                                                    tid,
267a4d8747dSGreg Clayton                                                    name,
268a4d8747dSGreg Clayton                                                    queue,
269a4d8747dSGreg Clayton                                                    reg_data_addr));
270b3ae8761SGreg Clayton 
271b3ae8761SGreg Clayton             }
272b3ae8761SGreg Clayton 
273b3ae8761SGreg Clayton             if (core_number < core_thread_list.GetSize(false))
274b3ae8761SGreg Clayton             {
275b3ae8761SGreg Clayton                 thread_sp->SetBackingThread(core_thread_list.GetThreadAtIndex(core_number, false));
276a4d8747dSGreg Clayton             }
277a4d8747dSGreg Clayton         }
278c7bece56SGreg Clayton     }
279a4d8747dSGreg Clayton     return thread_sp;
280a4d8747dSGreg Clayton }
281a4d8747dSGreg Clayton 
282a4d8747dSGreg Clayton 
283a4d8747dSGreg Clayton 
284b3e77600SGreg Clayton void
285b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
286b3e77600SGreg Clayton {
287b3e77600SGreg Clayton }
288b3e77600SGreg Clayton 
289b3e77600SGreg Clayton RegisterContextSP
290a4d8747dSGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
291b3e77600SGreg Clayton {
292435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
293a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp || !thread)
294c44306f7SFilipe Cabecinhas         return RegisterContextSP();
2951c22be69SGreg Clayton 
29685e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
29785e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
29885e276b8SJim Ingham     // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
29985e276b8SJim Ingham     Target &target = m_process->GetTarget();
30085e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
30185e276b8SJim Ingham 
3025160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
3031c22be69SGreg Clayton 
304198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
305ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
306ead45e01SGreg Clayton     {
307ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
308ead45e01SGreg Clayton         // context using the address provided
309ead45e01SGreg Clayton         if (log)
310d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context", thread->GetID(), reg_data_addr);
311ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
312ead45e01SGreg Clayton     }
313ead45e01SGreg Clayton     else
314ead45e01SGreg Clayton     {
315ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
316ead45e01SGreg Clayton         // it make up the data as it sees fit
3171c22be69SGreg Clayton         if (log)
318d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") fetching register data from python", thread->GetID());
3191c22be69SGreg Clayton 
320a4d8747dSGreg Clayton         PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
321435ce139SGreg Clayton         if (reg_context_data)
322435ce139SGreg Clayton         {
323435ce139SGreg Clayton             DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
324435ce139SGreg Clayton                                                       reg_context_data.GetSize()));
325435ce139SGreg Clayton             if (data_sp->GetByteSize())
326435ce139SGreg Clayton             {
327435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
328435ce139SGreg Clayton                 if (reg_ctx_memory)
329435ce139SGreg Clayton                 {
330435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
331435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
332435ce139SGreg Clayton                 }
333435ce139SGreg Clayton             }
334435ce139SGreg Clayton         }
335ead45e01SGreg Clayton     }
336b3e77600SGreg Clayton     return reg_ctx_sp;
337b3e77600SGreg Clayton }
338b3e77600SGreg Clayton 
339b3e77600SGreg Clayton StopInfoSP
340b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
341b3e77600SGreg Clayton {
342b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
343b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
344b3e77600SGreg Clayton     // cached so we can return it here
345b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
346b3e77600SGreg Clayton     return stop_info_sp;
347b3e77600SGreg Clayton }
348b3e77600SGreg Clayton 
349a4d8747dSGreg Clayton lldb::ThreadSP
350a4d8747dSGreg Clayton OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
351a4d8747dSGreg Clayton {
3525160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
353a4d8747dSGreg Clayton 
354a4d8747dSGreg Clayton     if (log)
355a4d8747dSGreg Clayton         log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
356a4d8747dSGreg Clayton 
357a4d8747dSGreg Clayton     if (m_interpreter && m_python_object_sp)
358a4d8747dSGreg Clayton     {
359a4d8747dSGreg Clayton         // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
360a4d8747dSGreg Clayton         // content of the process, and we're going to use python, which requires the API lock to do it.
361a4d8747dSGreg Clayton         // So get & hold that.  This is a recursive lock so we can grant it to any Python code called on the stack below us.
362a4d8747dSGreg Clayton         Target &target = m_process->GetTarget();
363a4d8747dSGreg Clayton         Mutex::Locker api_locker (target.GetAPIMutex());
364a4d8747dSGreg Clayton 
365198125a8SEnrico Granata         auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
366a4d8747dSGreg Clayton         PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context));
367a4d8747dSGreg Clayton         if (thread_info_dict)
368a4d8747dSGreg Clayton         {
369b3ae8761SGreg Clayton             ThreadList core_threads(m_process);
370a4d8747dSGreg Clayton             ThreadList &thread_list = m_process->GetThreadList();
371a4d8747dSGreg Clayton             bool did_create = false;
372b3ae8761SGreg Clayton             ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, &did_create));
373a4d8747dSGreg Clayton             if (did_create)
374a4d8747dSGreg Clayton                 thread_list.AddThread(thread_sp);
375a4d8747dSGreg Clayton             return thread_sp;
376a4d8747dSGreg Clayton         }
377a4d8747dSGreg Clayton     }
378a4d8747dSGreg Clayton     return ThreadSP();
379a4d8747dSGreg Clayton }
380a4d8747dSGreg Clayton 
381a4d8747dSGreg Clayton 
382b3e77600SGreg Clayton 
383b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
384