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 
10b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
11b3e77600SGreg Clayton 
12b3e77600SGreg Clayton #include "OperatingSystemPython.h"
13b3e77600SGreg Clayton // C Includes
14b3e77600SGreg Clayton // C++ Includes
15b3e77600SGreg Clayton // Other libraries and framework includes
16b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h"
17b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
185790759aSEnrico Granata #include "lldb/Core/Debugger.h"
19b3e77600SGreg Clayton #include "lldb/Core/Module.h"
20b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
21b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
2262f80036SGreg Clayton #include "lldb/Core/StreamString.h"
230641ca1aSZachary Turner #include "lldb/Core/StructuredData.h"
24b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
255790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
260641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h"
27b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
28b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
29b3e77600SGreg Clayton #include "lldb/Target/Process.h"
30b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
31b3e77600SGreg Clayton #include "lldb/Target/Target.h"
32b3e77600SGreg Clayton #include "lldb/Target/ThreadList.h"
33b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
34b3e77600SGreg Clayton #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
35cbd79b6cSEnrico Granata #include "Plugins/Process/Utility/RegisterContextDummy.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 {
4565d4d5c3SRyan Brown     PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, nullptr);
46b3e77600SGreg Clayton }
47b3e77600SGreg Clayton 
48b3e77600SGreg Clayton void
49b3e77600SGreg Clayton OperatingSystemPython::Terminate()
50b3e77600SGreg Clayton {
51b3e77600SGreg Clayton     PluginManager::UnregisterPlugin (CreateInstance);
52b3e77600SGreg Clayton }
53b3e77600SGreg Clayton 
54b3e77600SGreg Clayton OperatingSystem *
55b3e77600SGreg Clayton OperatingSystemPython::CreateInstance (Process *process, bool force)
56b3e77600SGreg Clayton {
57b3e77600SGreg Clayton     // Python OperatingSystem plug-ins must be requested by name, so force must be true
58c9d645d3SGreg Clayton     FileSpec python_os_plugin_spec (process->GetPythonOSPluginPath());
59c9d645d3SGreg Clayton     if (python_os_plugin_spec && python_os_plugin_spec.Exists())
60c9d645d3SGreg Clayton     {
617b0992d9SGreg Clayton         std::unique_ptr<OperatingSystemPython> os_ap (new OperatingSystemPython (process, python_os_plugin_spec));
62c9d645d3SGreg Clayton         if (os_ap.get() && os_ap->IsValid())
63c9d645d3SGreg Clayton             return os_ap.release();
64c9d645d3SGreg Clayton     }
65b3e77600SGreg Clayton     return NULL;
66b3e77600SGreg Clayton }
67b3e77600SGreg Clayton 
68b3e77600SGreg Clayton 
6957abc5d6SGreg Clayton ConstString
70b3e77600SGreg Clayton OperatingSystemPython::GetPluginNameStatic()
71b3e77600SGreg Clayton {
7257abc5d6SGreg Clayton     static ConstString g_name("python");
7357abc5d6SGreg Clayton     return g_name;
74b3e77600SGreg Clayton }
75b3e77600SGreg Clayton 
76b3e77600SGreg Clayton const char *
77b3e77600SGreg Clayton OperatingSystemPython::GetPluginDescriptionStatic()
78b3e77600SGreg Clayton {
79b3e77600SGreg Clayton     return "Operating system plug-in that gathers OS information from a python class that implements the necessary OperatingSystem functionality.";
80b3e77600SGreg Clayton }
81b3e77600SGreg Clayton 
82b3e77600SGreg Clayton 
83c9d645d3SGreg Clayton OperatingSystemPython::OperatingSystemPython (lldb_private::Process *process, const FileSpec &python_module_path) :
84b3e77600SGreg Clayton     OperatingSystem (process),
85b3e77600SGreg Clayton     m_thread_list_valobj_sp (),
865790759aSEnrico Granata     m_register_info_ap (),
875790759aSEnrico Granata     m_interpreter (NULL),
88a4d8747dSGreg Clayton     m_python_object_sp ()
89b3e77600SGreg Clayton {
905790759aSEnrico Granata     if (!process)
915790759aSEnrico Granata         return;
92a4d8747dSGreg Clayton     TargetSP target_sp = process->CalculateTarget();
935790759aSEnrico Granata     if (!target_sp)
945790759aSEnrico Granata         return;
955790759aSEnrico Granata     m_interpreter = target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
965790759aSEnrico Granata     if (m_interpreter)
975790759aSEnrico Granata     {
982443cbd7SGreg Clayton 
99c9d645d3SGreg Clayton         std::string os_plugin_class_name (python_module_path.GetFilename().AsCString(""));
100c9d645d3SGreg Clayton         if (!os_plugin_class_name.empty())
101c9d645d3SGreg Clayton         {
102c9d645d3SGreg Clayton             const bool init_session = false;
103c9d645d3SGreg Clayton             const bool allow_reload = true;
104c9d645d3SGreg Clayton             char python_module_path_cstr[PATH_MAX];
105c9d645d3SGreg Clayton             python_module_path.GetPath(python_module_path_cstr, sizeof(python_module_path_cstr));
106c9d645d3SGreg Clayton             Error error;
107c9d645d3SGreg Clayton             if (m_interpreter->LoadScriptingModule (python_module_path_cstr, allow_reload, init_session, error))
108c9d645d3SGreg Clayton             {
109c9d645d3SGreg Clayton                 // Strip the ".py" extension if there is one
110c9d645d3SGreg Clayton                 size_t py_extension_pos = os_plugin_class_name.rfind(".py");
111c9d645d3SGreg Clayton                 if (py_extension_pos != std::string::npos)
112c9d645d3SGreg Clayton                     os_plugin_class_name.erase (py_extension_pos);
113c9d645d3SGreg Clayton                 // Add ".OperatingSystemPlugIn" to the module name to get a string like "modulename.OperatingSystemPlugIn"
114c9d645d3SGreg Clayton                 os_plugin_class_name += ".OperatingSystemPlugIn";
1150641ca1aSZachary Turner                 StructuredData::ObjectSP object_sp =
1160641ca1aSZachary Turner                     m_interpreter->OSPlugin_CreatePluginObject(os_plugin_class_name.c_str(), process->CalculateProcess());
1170641ca1aSZachary Turner                 if (object_sp && object_sp->IsValid())
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;
13562f80036SGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
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 
1400641ca1aSZachary Turner         StructuredData::DictionarySP dictionary = m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1415790759aSEnrico Granata         if (!dictionary)
1425790759aSEnrico Granata             return NULL;
143b3e77600SGreg Clayton 
144d04f0edaSGreg Clayton         m_register_info_ap.reset(new DynamicRegisterInfo(*dictionary, m_process->GetTarget().GetArchitecture()));
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 //------------------------------------------------------------------
15457abc5d6SGreg Clayton ConstString
155b3e77600SGreg Clayton OperatingSystemPython::GetPluginName()
156b3e77600SGreg Clayton {
157b3e77600SGreg Clayton     return GetPluginNameStatic();
158b3e77600SGreg Clayton }
159b3e77600SGreg Clayton 
160b3e77600SGreg Clayton uint32_t
161b3e77600SGreg Clayton OperatingSystemPython::GetPluginVersion()
162b3e77600SGreg Clayton {
163b3e77600SGreg Clayton     return 1;
164b3e77600SGreg Clayton }
165b3e77600SGreg Clayton 
166b3e77600SGreg Clayton bool
167ba4e61d3SAndrew Kaylor OperatingSystemPython::UpdateThreadList (ThreadList &old_thread_list,
168ba4e61d3SAndrew Kaylor                                          ThreadList &core_thread_list,
169ba4e61d3SAndrew Kaylor                                          ThreadList &new_thread_list)
170b3e77600SGreg Clayton {
171a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
172a85e6b6cSDaniel Malea         return false;
1731c22be69SGreg Clayton 
17462f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OS));
1751c22be69SGreg Clayton 
176ab745c2aSGreg Clayton     // First thing we have to do is to try to get the API lock, and the run lock.
177ab745c2aSGreg Clayton     // We're going to change the thread content of the process, and we're going
178ab745c2aSGreg Clayton     // to use python, which requires the API lock to do it.
179ab745c2aSGreg Clayton     //
180ab745c2aSGreg Clayton     // If someone already has the API lock, that is ok, we just want to avoid
181ab745c2aSGreg Clayton     // external code from making new API calls while this call is happening.
182ab745c2aSGreg Clayton     //
183ab745c2aSGreg Clayton     // This is a recursive lock so we can grant it to any Python code called on
184ab745c2aSGreg Clayton     // the stack below us.
18585e276b8SJim Ingham     Target &target = m_process->GetTarget();
186*bb19a13cSSaleem Abdulrasool     std::unique_lock<std::recursive_mutex> lock(target.GetAPIMutex(), std::defer_lock);
187*bb19a13cSSaleem Abdulrasool     lock.try_lock();
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 
195*bb19a13cSSaleem Abdulrasool     auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); // to make sure threads_list stays alive
1960641ca1aSZachary Turner     StructuredData::ArraySP threads_list = m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
197e98008ccSGreg Clayton 
198e98008ccSGreg Clayton     const uint32_t num_cores = core_thread_list.GetSize(false);
199e98008ccSGreg Clayton 
200e98008ccSGreg Clayton     // Make a map so we can keep track of which cores were used from the
201e98008ccSGreg Clayton     // core_thread list. Any real threads/cores that weren't used should
202e98008ccSGreg Clayton     // later be put back into the "new_thread_list".
203e98008ccSGreg Clayton     std::vector<bool> core_used_map(num_cores, false);
204a4d8747dSGreg Clayton     if (threads_list)
205435ce139SGreg Clayton     {
20662f80036SGreg Clayton         if (log)
20762f80036SGreg Clayton         {
20862f80036SGreg Clayton             StreamString strm;
2090641ca1aSZachary Turner             threads_list->Dump(strm);
21062f80036SGreg Clayton             log->Printf("threads_list = %s", strm.GetString().c_str());
21162f80036SGreg Clayton         }
2120641ca1aSZachary Turner 
2130641ca1aSZachary Turner         const uint32_t num_threads = threads_list->GetSize();
2140641ca1aSZachary Turner         for (uint32_t i = 0; i < num_threads; ++i)
2156e0ff1a3SGreg Clayton         {
2160641ca1aSZachary Turner             StructuredData::ObjectSP thread_dict_obj = threads_list->GetItemAtIndex(i);
2170641ca1aSZachary Turner             if (auto thread_dict = thread_dict_obj->GetAsDictionary())
218435ce139SGreg Clayton             {
2190641ca1aSZachary Turner                 ThreadSP thread_sp(CreateThreadFromThreadInfo(*thread_dict, core_thread_list, old_thread_list, core_used_map, NULL));
220a4d8747dSGreg Clayton                 if (thread_sp)
221435ce139SGreg Clayton                     new_thread_list.AddThread(thread_sp);
222435ce139SGreg Clayton             }
223435ce139SGreg Clayton         }
224435ce139SGreg Clayton     }
2256e0ff1a3SGreg Clayton 
226e98008ccSGreg Clayton     // Any real core threads that didn't end up backing a memory thread should
227e98008ccSGreg Clayton     // still be in the main thread list, and they should be inserted at the beginning
228e98008ccSGreg Clayton     // of the list
229e98008ccSGreg Clayton     uint32_t insert_idx = 0;
230e98008ccSGreg Clayton     for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx)
231e98008ccSGreg Clayton     {
232e98008ccSGreg Clayton         if (core_used_map[core_idx] == false)
233e98008ccSGreg Clayton         {
234e98008ccSGreg Clayton             new_thread_list.InsertThread (core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
235e98008ccSGreg Clayton             ++insert_idx;
236e98008ccSGreg Clayton         }
237e98008ccSGreg Clayton     }
2386e0ff1a3SGreg Clayton 
239b3e77600SGreg Clayton     return new_thread_list.GetSize(false) > 0;
240b3e77600SGreg Clayton }
241b3e77600SGreg Clayton 
242a4d8747dSGreg Clayton ThreadSP
2430641ca1aSZachary Turner OperatingSystemPython::CreateThreadFromThreadInfo(StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
2440641ca1aSZachary Turner                                                   ThreadList &old_thread_list, std::vector<bool> &core_used_map, bool *did_create_ptr)
245a4d8747dSGreg Clayton {
246a4d8747dSGreg Clayton     ThreadSP thread_sp;
2470641ca1aSZachary Turner     tid_t tid = LLDB_INVALID_THREAD_ID;
2480641ca1aSZachary Turner     if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2490641ca1aSZachary Turner         return ThreadSP();
250a4d8747dSGreg Clayton 
2510641ca1aSZachary Turner     uint32_t core_number;
2520641ca1aSZachary Turner     addr_t reg_data_addr;
2530641ca1aSZachary Turner     std::string name;
2540641ca1aSZachary Turner     std::string queue;
2550641ca1aSZachary Turner 
2560641ca1aSZachary Turner     thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
2570641ca1aSZachary Turner     thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, LLDB_INVALID_ADDRESS);
2580641ca1aSZachary Turner     thread_dict.GetValueForKeyAsString("name", name);
2590641ca1aSZachary Turner     thread_dict.GetValueForKeyAsString("queue", queue);
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;
2800641ca1aSZachary Turner         thread_sp.reset(new ThreadMemory(*m_process, tid, name.c_str(), queue.c_str(), reg_data_addr));
281b3ae8761SGreg Clayton     }
282b3ae8761SGreg Clayton 
283b3ae8761SGreg Clayton     if (core_number < core_thread_list.GetSize(false))
284b3ae8761SGreg Clayton     {
285160c9d81SGreg Clayton         ThreadSP core_thread_sp(core_thread_list.GetThreadAtIndex(core_number, false));
286160c9d81SGreg Clayton         if (core_thread_sp)
287160c9d81SGreg Clayton         {
288e98008ccSGreg Clayton             // Keep track of which cores were set as the backing thread for memory threads...
289e98008ccSGreg Clayton             if (core_number < core_used_map.size())
290e98008ccSGreg Clayton                 core_used_map[core_number] = true;
291e98008ccSGreg Clayton 
292160c9d81SGreg Clayton             ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
293160c9d81SGreg Clayton             if (backing_core_thread_sp)
294160c9d81SGreg Clayton             {
295160c9d81SGreg Clayton                 thread_sp->SetBackingThread(backing_core_thread_sp);
296160c9d81SGreg Clayton             }
297160c9d81SGreg Clayton             else
298160c9d81SGreg Clayton             {
299160c9d81SGreg Clayton                 thread_sp->SetBackingThread(core_thread_sp);
300160c9d81SGreg Clayton             }
301160c9d81SGreg Clayton         }
302a4d8747dSGreg Clayton     }
303a4d8747dSGreg Clayton     return thread_sp;
304a4d8747dSGreg Clayton }
305a4d8747dSGreg Clayton 
306a4d8747dSGreg Clayton 
307a4d8747dSGreg Clayton 
308b3e77600SGreg Clayton void
309b3e77600SGreg Clayton OperatingSystemPython::ThreadWasSelected (Thread *thread)
310b3e77600SGreg Clayton {
311b3e77600SGreg Clayton }
312b3e77600SGreg Clayton 
313b3e77600SGreg Clayton RegisterContextSP
314a4d8747dSGreg Clayton OperatingSystemPython::CreateRegisterContextForThread (Thread *thread, addr_t reg_data_addr)
315b3e77600SGreg Clayton {
316435ce139SGreg Clayton     RegisterContextSP reg_ctx_sp;
317a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp || !thread)
318160c9d81SGreg Clayton         return reg_ctx_sp;
319160c9d81SGreg Clayton 
320160c9d81SGreg Clayton     if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
321160c9d81SGreg Clayton         return reg_ctx_sp;
3221c22be69SGreg Clayton 
32385e276b8SJim Ingham     // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
32485e276b8SJim Ingham     // content of the process, and we're going to use python, which requires the API lock to do it.
32585e276b8SJim 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.
32685e276b8SJim Ingham     Target &target = m_process->GetTarget();
327*bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
32885e276b8SJim Ingham 
3295160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
3301c22be69SGreg Clayton 
331198125a8SEnrico Granata     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure python objects stays alive
332ead45e01SGreg Clayton     if (reg_data_addr != LLDB_INVALID_ADDRESS)
333ead45e01SGreg Clayton     {
334ead45e01SGreg Clayton         // The registers data is in contiguous memory, just create the register
335ead45e01SGreg Clayton         // context using the address provided
336ead45e01SGreg Clayton         if (log)
337160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 ") creating memory register context",
338160c9d81SGreg Clayton                          thread->GetID(),
339160c9d81SGreg Clayton                          thread->GetProtocolID(),
340160c9d81SGreg Clayton                          reg_data_addr);
341ead45e01SGreg Clayton         reg_ctx_sp.reset (new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), reg_data_addr));
342ead45e01SGreg Clayton     }
343ead45e01SGreg Clayton     else
344ead45e01SGreg Clayton     {
345ead45e01SGreg Clayton         // No register data address is provided, query the python plug-in to let
346ead45e01SGreg Clayton         // it make up the data as it sees fit
3471c22be69SGreg Clayton         if (log)
348160c9d81SGreg Clayton             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ", 0x%" PRIx64 ") fetching register data from python",
349160c9d81SGreg Clayton                          thread->GetID(),
350160c9d81SGreg Clayton                          thread->GetProtocolID());
3511c22be69SGreg Clayton 
3520641ca1aSZachary Turner         StructuredData::StringSP reg_context_data = m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, thread->GetID());
353435ce139SGreg Clayton         if (reg_context_data)
354435ce139SGreg Clayton         {
3550641ca1aSZachary Turner             std::string value = reg_context_data->GetValue();
3560641ca1aSZachary Turner             DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
357435ce139SGreg Clayton             if (data_sp->GetByteSize())
358435ce139SGreg Clayton             {
359435ce139SGreg Clayton                 RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory (*thread, 0, *GetDynamicRegisterInfo (), LLDB_INVALID_ADDRESS);
360435ce139SGreg Clayton                 if (reg_ctx_memory)
361435ce139SGreg Clayton                 {
362435ce139SGreg Clayton                     reg_ctx_sp.reset(reg_ctx_memory);
363435ce139SGreg Clayton                     reg_ctx_memory->SetAllRegisterData (data_sp);
364435ce139SGreg Clayton                 }
365435ce139SGreg Clayton             }
366435ce139SGreg Clayton         }
367ead45e01SGreg Clayton     }
368cbd79b6cSEnrico Granata     // if we still have no register data, fallback on a dummy context to avoid crashing
369cbd79b6cSEnrico Granata     if (!reg_ctx_sp)
370cbd79b6cSEnrico Granata     {
371cbd79b6cSEnrico Granata         if (log)
372cbd79b6cSEnrico Granata             log->Printf ("OperatingSystemPython::CreateRegisterContextForThread (tid = 0x%" PRIx64 ") forcing a dummy register context", thread->GetID());
373cbd79b6cSEnrico Granata         reg_ctx_sp.reset(new RegisterContextDummy(*thread,0,target.GetArchitecture().GetAddressByteSize()));
374cbd79b6cSEnrico Granata     }
375b3e77600SGreg Clayton     return reg_ctx_sp;
376b3e77600SGreg Clayton }
377b3e77600SGreg Clayton 
378b3e77600SGreg Clayton StopInfoSP
379b3e77600SGreg Clayton OperatingSystemPython::CreateThreadStopReason (lldb_private::Thread *thread)
380b3e77600SGreg Clayton {
381b3e77600SGreg Clayton     // We should have gotten the thread stop info from the dictionary of data for
382b3e77600SGreg Clayton     // the thread in the initial call to get_thread_info(), this should have been
383b3e77600SGreg Clayton     // cached so we can return it here
384b3e77600SGreg Clayton     StopInfoSP stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
385b3e77600SGreg Clayton     return stop_info_sp;
386b3e77600SGreg Clayton }
387b3e77600SGreg Clayton 
388a4d8747dSGreg Clayton lldb::ThreadSP
389a4d8747dSGreg Clayton OperatingSystemPython::CreateThread (lldb::tid_t tid, addr_t context)
390a4d8747dSGreg Clayton {
3915160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
392a4d8747dSGreg Clayton 
393a4d8747dSGreg Clayton     if (log)
394a4d8747dSGreg Clayton         log->Printf ("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 ", context = 0x%" PRIx64 ") fetching register data from python", tid, context);
395a4d8747dSGreg Clayton 
396a4d8747dSGreg Clayton     if (m_interpreter && m_python_object_sp)
397a4d8747dSGreg Clayton     {
398a4d8747dSGreg Clayton         // First thing we have to do is get the API lock, and the run lock.  We're going to change the thread
399a4d8747dSGreg Clayton         // content of the process, and we're going to use python, which requires the API lock to do it.
400a4d8747dSGreg 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.
401a4d8747dSGreg Clayton         Target &target = m_process->GetTarget();
402*bb19a13cSSaleem Abdulrasool         std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
403a4d8747dSGreg Clayton 
404198125a8SEnrico Granata         auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure thread_info_dict stays alive
4050641ca1aSZachary Turner         StructuredData::DictionarySP thread_info_dict = m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
406e98008ccSGreg Clayton         std::vector<bool> core_used_map;
407a4d8747dSGreg Clayton         if (thread_info_dict)
408a4d8747dSGreg Clayton         {
409b3ae8761SGreg Clayton             ThreadList core_threads(m_process);
410a4d8747dSGreg Clayton             ThreadList &thread_list = m_process->GetThreadList();
411a4d8747dSGreg Clayton             bool did_create = false;
4120641ca1aSZachary Turner             ThreadSP thread_sp(CreateThreadFromThreadInfo(*thread_info_dict, core_threads, thread_list, core_used_map, &did_create));
413a4d8747dSGreg Clayton             if (did_create)
414a4d8747dSGreg Clayton                 thread_list.AddThread(thread_sp);
415a4d8747dSGreg Clayton             return thread_sp;
416a4d8747dSGreg Clayton         }
417a4d8747dSGreg Clayton     }
418a4d8747dSGreg Clayton     return ThreadSP();
419a4d8747dSGreg Clayton }
420a4d8747dSGreg Clayton 
421a4d8747dSGreg Clayton 
422b3e77600SGreg Clayton 
423b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
424