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"
36cbd79b6cSEnrico Granata #include "Plugins/Process/Utility/RegisterContextDummy.h"
37b3e77600SGreg Clayton #include "Plugins/Process/Utility/RegisterContextMemory.h"
38b3e77600SGreg Clayton #include "Plugins/Process/Utility/ThreadMemory.h"
39b3e77600SGreg Clayton 
40b3e77600SGreg Clayton using namespace lldb;
41b3e77600SGreg Clayton using namespace lldb_private;
42b3e77600SGreg Clayton 
43b3e77600SGreg Clayton void
44b3e77600SGreg Clayton OperatingSystemPython::Initialize()
45b3e77600SGreg Clayton {
46b3e77600SGreg Clayton     PluginManager::RegisterPlugin (GetPluginNameStatic(),
47b3e77600SGreg Clayton                                    GetPluginDescriptionStatic(),
48b3e77600SGreg Clayton                                    CreateInstance);
49b3e77600SGreg Clayton }
50b3e77600SGreg Clayton 
51b3e77600SGreg Clayton void
52b3e77600SGreg Clayton OperatingSystemPython::Terminate()
53b3e77600SGreg Clayton {
54b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
55b3e77600SGreg Clayton }
56b3e77600SGreg Clayton 
57b3e77600SGreg Clayton OperatingSystem *
58b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
59b3e77600SGreg Clayton {
60b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
61c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
62c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
63c9d645d3SGreg Clayton     {
647b0992d9SGreg Clayton         std::unique_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
65c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
66c9d645d3SGreg Clayton             return os_ap.release();
67c9d645d3SGreg Clayton     }
68b3e77600SGreg Clayton     return NULL;
69b3e77600SGreg Clayton }
70b3e77600SGreg Clayton 
71b3e77600SGreg Clayton 
72b3e77600SGreg Clayton const char *
73b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
74b3e77600SGreg Clayton {
75b3e77600SGreg Clayton     return "python";
76b3e77600SGreg Clayton }
77b3e77600SGreg Clayton 
78b3e77600SGreg Clayton const char *
79b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
80b3e77600SGreg Clayton {
81b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
82b3e77600SGreg Clayton }
83b3e77600SGreg Clayton 
84b3e77600SGreg Clayton 
85c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
86b3e77600SGreg Clayton     OperatingSystem (process),
87b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
885790759aSEnrico Granata     m_register_info_ap (),
895790759aSEnrico Granata     m_interpreter (NULL),
90a4d8747dSGreg Clayton     m_python_object_sp ()
91b3e77600SGreg Clayton {
925790759aSEnrico Granata     if (!process)
935790759aSEnrico Granata         return;
94a4d8747dSGreg Clayton     TargetSP target_sp = process->CalculateTarget();
955790759aSEnrico Granata     if (!target_sp)
965790759aSEnrico Granata         return;
975790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
985790759aSEnrico Granata     if (m_interpreter)
995790759aSEnrico Granata     {
1002443cbd7SGreg Clayton 
101c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
102c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
103c9d645d3SGreg Clayton         {
104c9d645d3SGreg Clayton             const bool init_session = false;
105c9d645d3SGreg Clayton             const bool allow_reload = true;
106c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
107c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
108c9d645d3SGreg Clayton             Error error;
109c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
110c9d645d3SGreg Clayton             {
111c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
112c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
113c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
114c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
115c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
116c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
117a4d8747dSGreg Clayton                 ScriptInterpreterObjectSP object_sp = m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess());
118a4d8747dSGreg Clayton                 if (object_sp && object_sp->GetObject())
119a4d8747dSGreg Clayton                     m_python_object_sp = object_sp;
120c9d645d3SGreg Clayton             }
1212443cbd7SGreg Clayton         }
1225790759aSEnrico Granata     }
123b3e77600SGreg Clayton }
124b3e77600SGreg Clayton 
125b3e77600SGreg Clayton OperatingSystemPython::~OperatingSystemPython ()
126b3e77600SGreg Clayton {
127b3e77600SGreg Clayton }
128b3e77600SGreg Clayton 
129b3e77600SGreg Clayton DynamicRegisterInfo *
130b3e77600SGreg Clayton OperatingSystemPython::GetDynamicRegisterInfo ()
131b3e77600SGreg Clayton {
1322443cbd7SGreg Clayton     if (m_register_info_ap.get() == NULL)
1332443cbd7SGreg Clayton     {
134a4d8747dSGreg Clayton         if (!m_interpreter || !m_python_object_sp)
1355790759aSEnrico Granata             return NULL;
1365160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1371c22be69SGreg Clayton 
1381c22be69SGreg Clayton         if (log)
139d01b2953SDaniel Malea             log->Printf ("OperatingSystemPython::GetDynamicRegisterInfo() fetching thread register definitions from python for pid %" PRIu64, m_process->GetID());
1401c22be69SGreg Clayton 
141a4d8747dSGreg Clayton         PythonDictionary dictionary(m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp));
1425790759aSEnrico Granata         if (!dictionary)
1435790759aSEnrico Granata             return NULL;
144b3e77600SGreg Clayton 
1452443cbd7SGreg Clayton         m_register_info_ap.reset (new DynamicRegisterInfo (dictionary));
1462443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisters() > 0);
1472443cbd7SGreg Clayton         assert (m_register_info_ap->GetNumRegisterSets() > 0);
148b3e77600SGreg Clayton     }
149b3e77600SGreg Clayton     return m_register_info_ap.get();
150b3e77600SGreg Clayton }
151b3e77600SGreg Clayton 
152b3e77600SGreg Clayton //------------------------------------------------------------------
153b3e77600SGreg Clayton // PluginInterface protocol
154b3e77600SGreg Clayton //------------------------------------------------------------------
155b3e77600SGreg Clayton const char *
156b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
157b3e77600SGreg Clayton {
158b3e77600SGreg Clayton     return "OperatingSystemPython";
159b3e77600SGreg Clayton }
160b3e77600SGreg Clayton 
161b3e77600SGreg Clayton const char *
162b3e77600SGreg Clayton OperatingSystemPython::GetShortPluginName()
163b3e77600SGreg Clayton {
164b3e77600SGreg Clayton     return GetPluginNameStatic();
165b3e77600SGreg Clayton }
166b3e77600SGreg Clayton 
167b3e77600SGreg Clayton uint32_t
168b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
169b3e77600SGreg Clayton {
170b3e77600SGreg Clayton     return 1;
171b3e77600SGreg Clayton }
172b3e77600SGreg Clayton 
173b3e77600SGreg Clayton bool
174*ba4e61d3SAndrew Kaylor OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
175*ba4e61d3SAndrew Kaylor                                          ThreadList &core_thread_list,
176*ba4e61d3SAndrew Kaylor                                          ThreadList &new_thread_list)
177b3e77600SGreg Clayton {
178a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
179a85e6b6cSDaniel Malea         return false;
1801c22be69SGreg Clayton 
1815160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PROCESS));
1821c22be69SGreg Clayton 
18385e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
18485e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
18585e276b8SJim 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.
18685e276b8SJim Ingham     Target &target = m_process->GetTarget();
18785e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
18885e276b8SJim Ingham 
1891c22be69SGreg Clayton     if (log)
190d01b2953SDaniel Malea         log->Printf ("OperatingSystemPython::UpdateThreadList() fetching thread data from python for pid %" PRIu64, m_process->GetID());
1911c22be69SGreg Clayton 
192b3ae8761SGreg Clayton     // The threads that are in "new_thread_list" upon entry are the threads from the
193b3ae8761SGreg Clayton     // lldb_private::Process subclass, no memory threads will be in this list.
194b3ae8761SGreg Clayton 
195198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive
196a4d8747dSGreg Clayton     PythonList threads_list(m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp));
197a4d8747dSGreg Clayton     if (threads_list)
198435ce139SGreg Clayton     {
199b3ae8761SGreg Clayton         uint32_t i;
200a4d8747dSGreg Clayton         const uint32_t num_threads = threads_list.GetSize();
201b3ae8761SGreg Clayton         for (i=0; i<num_threads; ++i)
202435ce139SGreg Clayton         {
203a4d8747dSGreg Clayton             PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
204435ce139SGreg Clayton             if (thread_dict)
205435ce139SGreg Clayton             {
206b3ae8761SGreg Clayton                 if (thread_dict.GetItemForKey("core"))
207b3ae8761SGreg Clayton                 {
208b3ae8761SGreg Clayton                     // We have some threads that are saying they are on a "core", which means
209b3ae8761SGreg Clayton                     // they map the threads that are gotten from the lldb_private::Process subclass
210b3ae8761SGreg Clayton                     // so clear the new threads list so the core threads don't show up
211b3ae8761SGreg Clayton                     new_thread_list.Clear();
212b3ae8761SGreg Clayton                     break;
213b3ae8761SGreg Clayton                 }
214b3ae8761SGreg Clayton             }
215b3ae8761SGreg Clayton         }
216b3ae8761SGreg Clayton         for (i=0; i<num_threads; ++i)
217b3ae8761SGreg Clayton         {
218b3ae8761SGreg Clayton             PythonDictionary thread_dict(threads_list.GetItemAtIndex(i));
219b3ae8761SGreg Clayton             if (thread_dict)
220b3ae8761SGreg Clayton             {
221b3ae8761SGreg Clayton                 ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_dict, core_thread_list, old_thread_list, NULL));
222a4d8747dSGreg Clayton                 if (thread_sp)
223435ce139SGreg Clayton                     new_thread_list.AddThread(thread_sp);
224435ce139SGreg Clayton             }
225435ce139SGreg Clayton         }
226435ce139SGreg Clayton     }
227435ce139SGreg Clayton     else
228435ce139SGreg Clayton     {
229b3e77600SGreg Clayton         new_thread_list = old_thread_list;
230435ce139SGreg Clayton     }
231b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
232b3e77600SGreg Clayton }
233b3e77600SGreg Clayton 
234a4d8747dSGreg Clayton ThreadSP
235b3ae8761SGreg Clayton OperatingSystemPython::CreateThreadFromThreadInfo (PythonDictionary &thread_dict,
236b3ae8761SGreg Clayton                                                    ThreadList &core_thread_list,
237b3ae8761SGreg Clayton                                                    ThreadList &old_thread_list,
238b3ae8761SGreg Clayton                                                    bool *did_create_ptr)
239a4d8747dSGreg Clayton {
240a4d8747dSGreg Clayton     ThreadSP thread_sp;
241a4d8747dSGreg Clayton     if (thread_dict)
242a4d8747dSGreg Clayton     {
243a4d8747dSGreg Clayton         PythonString tid_pystr("tid");
244c7bece56SGreg Clayton         const tid_t tid = thread_dict.GetItemForKeyAsInteger (tid_pystr, LLDB_INVALID_THREAD_ID);
245c7bece56SGreg Clayton         if (tid != LLDB_INVALID_THREAD_ID)
246c7bece56SGreg Clayton         {
247b3ae8761SGreg Clayton             PythonString core_pystr("core");
248a4d8747dSGreg Clayton             PythonString name_pystr("name");
249a4d8747dSGreg Clayton             PythonString queue_pystr("queue");
250160c9d81SGreg Clayton             //PythonString state_pystr("state");
251160c9d81SGreg Clayton             //PythonString stop_reason_pystr("stop_reason");
252a4d8747dSGreg Clayton             PythonString reg_data_addr_pystr ("register_data_addr");
253a4d8747dSGreg Clayton 
254b3ae8761SGreg Clayton             const uint32_t core_number = thread_dict.GetItemForKeyAsInteger (core_pystr, UINT32_MAX);
255a4d8747dSGreg Clayton             const addr_t reg_data_addr = thread_dict.GetItemForKeyAsInteger (reg_data_addr_pystr, LLDB_INVALID_ADDRESS);
256a4d8747dSGreg Clayton             const char *name = thread_dict.GetItemForKeyAsString (name_pystr);
257a4d8747dSGreg Clayton             const char *queue = thread_dict.GetItemForKeyAsString (queue_pystr);
258a4d8747dSGreg Clayton             //const char *state = thread_dict.GetItemForKeyAsString (state_pystr);
259a4d8747dSGreg Clayton             //const char *stop_reason = thread_dict.GetItemForKeyAsString (stop_reason_pystr);
260a4d8747dSGreg Clayton 
261160c9d81SGreg Clayton             // See if a thread already exists for "tid"
262b3ae8761SGreg Clayton             thread_sp = old_thread_list.FindThreadByID (tid, false);
263160c9d81SGreg Clayton             if (thread_sp)
264160c9d81SGreg Clayton             {
265160c9d81SGreg Clayton                 // A thread already does exist for "tid", make sure it was an operating system
266160c9d81SGreg Clayton                 // plug-in generated thread.
267160c9d81SGreg Clayton                 if (!IsOperatingSystemPluginThread(thread_sp))
268160c9d81SGreg Clayton                 {
269160c9d81SGreg Clayton                     // We have thread ID overlap between the protocol threads and the
270160c9d81SGreg Clayton                     // operating system threads, clear the thread so we create an
271160c9d81SGreg Clayton                     // operating system thread for this.
272160c9d81SGreg Clayton                     thread_sp.reset();
273160c9d81SGreg Clayton                 }
274160c9d81SGreg Clayton             }
275160c9d81SGreg Clayton 
276a4d8747dSGreg Clayton             if (!thread_sp)
277a4d8747dSGreg Clayton             {
278a4d8747dSGreg Clayton                 if (did_create_ptr)
279a4d8747dSGreg Clayton                     *did_create_ptr = true;
280a4d8747dSGreg Clayton                 thread_sp.reset (new ThreadMemory (*m_process,
281a4d8747dSGreg Clayton                                                    tid,
282a4d8747dSGreg Clayton                                                    name,
283a4d8747dSGreg Clayton                                                    queue,
284a4d8747dSGreg Clayton                                                    reg_data_addr));
285b3ae8761SGreg Clayton 
286b3ae8761SGreg Clayton             }
287b3ae8761SGreg Clayton 
288b3ae8761SGreg Clayton             if (core_number < core_thread_list.GetSize(false))
289b3ae8761SGreg Clayton             {
290160c9d81SGreg Clayton                 ThreadSP core_thread_sp (core_thread_list.GetThreadAtIndex(core_number, false));
291160c9d81SGreg Clayton                 if (core_thread_sp)
292160c9d81SGreg Clayton                 {
293160c9d81SGreg Clayton                     ThreadSP backing_core_thread_sp (core_thread_sp->GetBackingThread());
294160c9d81SGreg Clayton                     if (backing_core_thread_sp)
295160c9d81SGreg Clayton                     {
296160c9d81SGreg Clayton                         thread_sp->SetBackingThread(backing_core_thread_sp);
297160c9d81SGreg Clayton                     }
298160c9d81SGreg Clayton                     else
299160c9d81SGreg Clayton                     {
300160c9d81SGreg Clayton                         thread_sp->SetBackingThread(core_thread_sp);
301160c9d81SGreg Clayton                     }
302160c9d81SGreg Clayton                 }
303a4d8747dSGreg Clayton             }
304a4d8747dSGreg Clayton         }
305c7bece56SGreg Clayton     }
306a4d8747dSGreg Clayton     return thread_sp;
307a4d8747dSGreg Clayton }
308a4d8747dSGreg Clayton 
309a4d8747dSGreg Clayton 
310a4d8747dSGreg Clayton 
311b3e77600SGreg Clayton void
312b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
313b3e77600SGreg Clayton {
314b3e77600SGreg Clayton }
315b3e77600SGreg Clayton 
316b3e77600SGreg Clayton RegisterContextSP
317a4d8747dSGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
318b3e77600SGreg Clayton {
319435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
320a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp || !thread)
321160c9d81SGreg Clayton         return reg_ctx_sp;
322160c9d81SGreg Clayton 
323160c9d81SGreg Clayton     if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
324160c9d81SGreg Clayton         return reg_ctx_sp;
3251c22be69SGreg Clayton 
32685e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
32785e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
32885e276b8SJim 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.
32985e276b8SJim Ingham     Target &target = m_process->GetTarget();
33085e276b8SJim Ingham     Mutex::Locker api_locker (target.GetAPIMutex());
33185e276b8SJim Ingham 
3325160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
3331c22be69SGreg Clayton 
334198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
335ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
336ead45e01SGreg Clayton     {
337ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
338ead45e01SGreg Clayton         // context using the address provided
339ead45e01SGreg Clayton         if (log)
340160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
341160c9d81SGreg Clayton                          thread->GetID(),
342160c9d81SGreg Clayton                          thread->GetProtocolID(),
343160c9d81SGreg Clayton                          reg_data_addr);
344ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
345ead45e01SGreg Clayton     }
346ead45e01SGreg Clayton     else
347ead45e01SGreg Clayton     {
348ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
349ead45e01SGreg Clayton         // it make up the data as it sees fit
3501c22be69SGreg Clayton         if (log)
351160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
352160c9d81SGreg Clayton                          thread->GetID(),
353160c9d81SGreg Clayton                          thread->GetProtocolID());
3541c22be69SGreg Clayton 
355a4d8747dSGreg Clayton         PythonString reg_context_data(m_interpreter->OSPlugin_RegisterContextData (m_python_object_sp, thread->GetID()));
356435ce139SGreg Clayton         if (reg_context_data)
357435ce139SGreg Clayton         {
358435ce139SGreg Clayton             DataBufferSP data_sp (new DataBufferHeap (reg_context_data.GetString(),
359435ce139SGreg Clayton                                                       reg_context_data.GetSize()));
360435ce139SGreg Clayton             if (data_sp->GetByteSize())
361435ce139SGreg Clayton             {
362435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
363435ce139SGreg Clayton                 if (reg_ctx_memory)
364435ce139SGreg Clayton                 {
365435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
366435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
367435ce139SGreg Clayton                 }
368435ce139SGreg Clayton             }
369435ce139SGreg Clayton         }
370ead45e01SGreg Clayton     }
371cbd79b6cSEnrico Granata     // if we still have no register data, fallback on a dummy context to avoid crashing
372cbd79b6cSEnrico Granata     if (!reg_ctx_sp)
373cbd79b6cSEnrico Granata     {
374cbd79b6cSEnrico Granata         if (log)
375cbd79b6cSEnrico Granata             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") forcing a dummy register context", thread->GetID());
376cbd79b6cSEnrico Granata         reg_ctx_sp.reset(new RegisterContextDummy(*thread,0,target.GetArchitecture().GetAddressByteSize()));
377cbd79b6cSEnrico Granata     }
378b3e77600SGreg Clayton     return reg_ctx_sp;
379b3e77600SGreg Clayton }
380b3e77600SGreg Clayton 
381b3e77600SGreg Clayton StopInfoSP
382b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
383b3e77600SGreg Clayton {
384b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
385b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
386b3e77600SGreg Clayton     // cached so we can return it here
387b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
388b3e77600SGreg Clayton     return stop_info_sp;
389b3e77600SGreg Clayton }
390b3e77600SGreg Clayton 
391a4d8747dSGreg Clayton lldb::ThreadSP
392a4d8747dSGreg Clayton OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
393a4d8747dSGreg Clayton {
3945160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
395a4d8747dSGreg Clayton 
396a4d8747dSGreg Clayton     if (log)
397a4d8747dSGreg Clayton         log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
398a4d8747dSGreg Clayton 
399a4d8747dSGreg Clayton     if (m_interpreter && m_python_object_sp)
400a4d8747dSGreg Clayton     {
401a4d8747dSGreg Clayton         // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
402a4d8747dSGreg Clayton         // content of the process, and we're going to use python, which requires the API lock to do it.
403a4d8747dSGreg 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.
404a4d8747dSGreg Clayton         Target &target = m_process->GetTarget();
405a4d8747dSGreg Clayton         Mutex::Locker api_locker (target.GetAPIMutex());
406a4d8747dSGreg Clayton 
407198125a8SEnrico Granata         auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
408a4d8747dSGreg Clayton         PythonDictionary thread_info_dict (m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context));
409a4d8747dSGreg Clayton         if (thread_info_dict)
410a4d8747dSGreg Clayton         {
411b3ae8761SGreg Clayton             ThreadList core_threads(m_process);
412a4d8747dSGreg Clayton             ThreadList &thread_list = m_process->GetThreadList();
413a4d8747dSGreg Clayton             bool did_create = false;
414b3ae8761SGreg Clayton             ThreadSP thread_sp (CreateThreadFromThreadInfo (thread_info_dict, core_threads, thread_list, &did_create));
415a4d8747dSGreg Clayton             if (did_create)
416a4d8747dSGreg Clayton                 thread_list.AddThread(thread_sp);
417a4d8747dSGreg Clayton             return thread_sp;
418a4d8747dSGreg Clayton         }
419a4d8747dSGreg Clayton     }
420a4d8747dSGreg Clayton     return ThreadSP();
421a4d8747dSGreg Clayton }
422a4d8747dSGreg Clayton 
423a4d8747dSGreg Clayton 
424b3e77600SGreg Clayton 
425b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
426