1*b9c1b51eSKate Stone //===-- OperatingSystemPython.cpp --------------------------------*- C++
2*b9c1b51eSKate Stone //-*-===//
3b3e77600SGreg Clayton //
4b3e77600SGreg Clayton //                     The LLVM Compiler Infrastructure
5b3e77600SGreg Clayton //
6b3e77600SGreg Clayton // This file is distributed under the University of Illinois Open Source
7b3e77600SGreg Clayton // License. See LICENSE.TXT for details.
8b3e77600SGreg Clayton //
9b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
1093a64300SDaniel Malea 
11b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
12b3e77600SGreg Clayton 
13b3e77600SGreg Clayton #include "OperatingSystemPython.h"
14b3e77600SGreg Clayton // C Includes
15b3e77600SGreg Clayton // C++ Includes
16b3e77600SGreg Clayton // Other libraries and framework includes
17*b9c1b51eSKate Stone #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
18*b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h"
19*b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h"
20*b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h"
21b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h"
22b3e77600SGreg Clayton #include "lldb/Core/DataBufferHeap.h"
235790759aSEnrico Granata #include "lldb/Core/Debugger.h"
24b3e77600SGreg Clayton #include "lldb/Core/Module.h"
25b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
26b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
2762f80036SGreg Clayton #include "lldb/Core/StreamString.h"
280641ca1aSZachary Turner #include "lldb/Core/StructuredData.h"
29b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
305790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
310641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h"
32b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
33b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
34b3e77600SGreg Clayton #include "lldb/Target/Process.h"
35b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
36b3e77600SGreg Clayton #include "lldb/Target/Target.h"
37b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
38*b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h"
39b3e77600SGreg Clayton 
40b3e77600SGreg Clayton using namespace lldb;
41b3e77600SGreg Clayton using namespace lldb_private;
42b3e77600SGreg Clayton 
43*b9c1b51eSKate Stone void OperatingSystemPython::Initialize() {
44*b9c1b51eSKate Stone   PluginManager::RegisterPlugin(GetPluginNameStatic(),
45*b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
46*b9c1b51eSKate Stone                                 nullptr);
47b3e77600SGreg Clayton }
48b3e77600SGreg Clayton 
49*b9c1b51eSKate Stone void OperatingSystemPython::Terminate() {
50b3e77600SGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
51b3e77600SGreg Clayton }
52b3e77600SGreg Clayton 
53*b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
54*b9c1b51eSKate Stone                                                        bool force) {
55*b9c1b51eSKate Stone   // Python OperatingSystem plug-ins must be requested by name, so force must be
56*b9c1b51eSKate Stone   // true
57c9d645d3SGreg Clayton   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
58*b9c1b51eSKate Stone   if (python_os_plugin_spec && python_os_plugin_spec.Exists()) {
59*b9c1b51eSKate Stone     std::unique_ptr<OperatingSystemPython> os_ap(
60*b9c1b51eSKate Stone         new OperatingSystemPython(process, python_os_plugin_spec));
61c9d645d3SGreg Clayton     if (os_ap.get() && os_ap->IsValid())
62c9d645d3SGreg Clayton       return os_ap.release();
63c9d645d3SGreg Clayton   }
64b3e77600SGreg Clayton   return NULL;
65b3e77600SGreg Clayton }
66b3e77600SGreg Clayton 
67*b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginNameStatic() {
6857abc5d6SGreg Clayton   static ConstString g_name("python");
6957abc5d6SGreg Clayton   return g_name;
70b3e77600SGreg Clayton }
71b3e77600SGreg Clayton 
72*b9c1b51eSKate Stone const char *OperatingSystemPython::GetPluginDescriptionStatic() {
73*b9c1b51eSKate Stone   return "Operating system plug-in that gathers OS information from a python "
74*b9c1b51eSKate Stone          "class that implements the necessary OperatingSystem functionality.";
75b3e77600SGreg Clayton }
76b3e77600SGreg Clayton 
77*b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
78*b9c1b51eSKate Stone                                              const FileSpec &python_module_path)
79*b9c1b51eSKate Stone     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_ap(),
80*b9c1b51eSKate Stone       m_interpreter(NULL), m_python_object_sp() {
815790759aSEnrico Granata   if (!process)
825790759aSEnrico Granata     return;
83a4d8747dSGreg Clayton   TargetSP target_sp = process->CalculateTarget();
845790759aSEnrico Granata   if (!target_sp)
855790759aSEnrico Granata     return;
86*b9c1b51eSKate Stone   m_interpreter =
87*b9c1b51eSKate Stone       target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
88*b9c1b51eSKate Stone   if (m_interpreter) {
892443cbd7SGreg Clayton 
90*b9c1b51eSKate Stone     std::string os_plugin_class_name(
91*b9c1b51eSKate Stone         python_module_path.GetFilename().AsCString(""));
92*b9c1b51eSKate Stone     if (!os_plugin_class_name.empty()) {
93c9d645d3SGreg Clayton       const bool init_session = false;
94c9d645d3SGreg Clayton       const bool allow_reload = true;
95c9d645d3SGreg Clayton       char python_module_path_cstr[PATH_MAX];
96*b9c1b51eSKate Stone       python_module_path.GetPath(python_module_path_cstr,
97*b9c1b51eSKate Stone                                  sizeof(python_module_path_cstr));
98c9d645d3SGreg Clayton       Error error;
99*b9c1b51eSKate Stone       if (m_interpreter->LoadScriptingModule(
100*b9c1b51eSKate Stone               python_module_path_cstr, allow_reload, init_session, error)) {
101c9d645d3SGreg Clayton         // Strip the ".py" extension if there is one
102c9d645d3SGreg Clayton         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
103c9d645d3SGreg Clayton         if (py_extension_pos != std::string::npos)
104c9d645d3SGreg Clayton           os_plugin_class_name.erase(py_extension_pos);
105*b9c1b51eSKate Stone         // Add ".OperatingSystemPlugIn" to the module name to get a string like
106*b9c1b51eSKate Stone         // "modulename.OperatingSystemPlugIn"
107c9d645d3SGreg Clayton         os_plugin_class_name += ".OperatingSystemPlugIn";
1080641ca1aSZachary Turner         StructuredData::ObjectSP object_sp =
109*b9c1b51eSKate Stone             m_interpreter->OSPlugin_CreatePluginObject(
110*b9c1b51eSKate Stone                 os_plugin_class_name.c_str(), process->CalculateProcess());
1110641ca1aSZachary Turner         if (object_sp && object_sp->IsValid())
112a4d8747dSGreg Clayton           m_python_object_sp = object_sp;
113c9d645d3SGreg Clayton       }
1142443cbd7SGreg Clayton     }
1155790759aSEnrico Granata   }
116b3e77600SGreg Clayton }
117b3e77600SGreg Clayton 
118*b9c1b51eSKate Stone OperatingSystemPython::~OperatingSystemPython() {}
119b3e77600SGreg Clayton 
120*b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
121*b9c1b51eSKate Stone   if (m_register_info_ap.get() == NULL) {
122a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
1235790759aSEnrico Granata       return NULL;
12462f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1251c22be69SGreg Clayton 
1261c22be69SGreg Clayton     if (log)
127*b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::GetDynamicRegisterInfo() fetching "
128*b9c1b51eSKate Stone                   "thread register definitions from python for pid %" PRIu64,
129*b9c1b51eSKate Stone                   m_process->GetID());
1301c22be69SGreg Clayton 
131*b9c1b51eSKate Stone     StructuredData::DictionarySP dictionary =
132*b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1335790759aSEnrico Granata     if (!dictionary)
1345790759aSEnrico Granata       return NULL;
135b3e77600SGreg Clayton 
136*b9c1b51eSKate Stone     m_register_info_ap.reset(new DynamicRegisterInfo(
137*b9c1b51eSKate Stone         *dictionary, m_process->GetTarget().GetArchitecture()));
1382443cbd7SGreg Clayton     assert(m_register_info_ap->GetNumRegisters() > 0);
1392443cbd7SGreg Clayton     assert(m_register_info_ap->GetNumRegisterSets() > 0);
140b3e77600SGreg Clayton   }
141b3e77600SGreg Clayton   return m_register_info_ap.get();
142b3e77600SGreg Clayton }
143b3e77600SGreg Clayton 
144b3e77600SGreg Clayton //------------------------------------------------------------------
145b3e77600SGreg Clayton // PluginInterface protocol
146b3e77600SGreg Clayton //------------------------------------------------------------------
147*b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginName() {
148b3e77600SGreg Clayton   return GetPluginNameStatic();
149b3e77600SGreg Clayton }
150b3e77600SGreg Clayton 
151*b9c1b51eSKate Stone uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
152b3e77600SGreg Clayton 
153*b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
154ba4e61d3SAndrew Kaylor                                              ThreadList &core_thread_list,
155*b9c1b51eSKate Stone                                              ThreadList &new_thread_list) {
156a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp)
157a85e6b6cSDaniel Malea     return false;
1581c22be69SGreg Clayton 
15962f80036SGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1601c22be69SGreg Clayton 
161ab745c2aSGreg Clayton   // First thing we have to do is to try to get the API lock, and the run lock.
162ab745c2aSGreg Clayton   // We're going to change the thread content of the process, and we're going
163ab745c2aSGreg Clayton   // to use python, which requires the API lock to do it.
164ab745c2aSGreg Clayton   //
165ab745c2aSGreg Clayton   // If someone already has the API lock, that is ok, we just want to avoid
166ab745c2aSGreg Clayton   // external code from making new API calls while this call is happening.
167ab745c2aSGreg Clayton   //
168ab745c2aSGreg Clayton   // This is a recursive lock so we can grant it to any Python code called on
169ab745c2aSGreg Clayton   // the stack below us.
17085e276b8SJim Ingham   Target &target = m_process->GetTarget();
171*b9c1b51eSKate Stone   std::unique_lock<std::recursive_mutex> lock(target.GetAPIMutex(),
172*b9c1b51eSKate Stone                                               std::defer_lock);
173bb19a13cSSaleem Abdulrasool   lock.try_lock();
17485e276b8SJim Ingham 
1751c22be69SGreg Clayton   if (log)
176*b9c1b51eSKate Stone     log->Printf("OperatingSystemPython::UpdateThreadList() fetching thread "
177*b9c1b51eSKate Stone                 "data from python for pid %" PRIu64,
178*b9c1b51eSKate Stone                 m_process->GetID());
1791c22be69SGreg Clayton 
180*b9c1b51eSKate Stone   // The threads that are in "new_thread_list" upon entry are the threads from
181*b9c1b51eSKate Stone   // the
182b3ae8761SGreg Clayton   // lldb_private::Process subclass, no memory threads will be in this list.
183b3ae8761SGreg Clayton 
184*b9c1b51eSKate Stone   auto interpreter_lock =
185*b9c1b51eSKate Stone       m_interpreter
186*b9c1b51eSKate Stone           ->AcquireInterpreterLock(); // to make sure threads_list stays alive
187*b9c1b51eSKate Stone   StructuredData::ArraySP threads_list =
188*b9c1b51eSKate Stone       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
189e98008ccSGreg Clayton 
190e98008ccSGreg Clayton   const uint32_t num_cores = core_thread_list.GetSize(false);
191e98008ccSGreg Clayton 
192e98008ccSGreg Clayton   // Make a map so we can keep track of which cores were used from the
193e98008ccSGreg Clayton   // core_thread list. Any real threads/cores that weren't used should
194e98008ccSGreg Clayton   // later be put back into the "new_thread_list".
195e98008ccSGreg Clayton   std::vector<bool> core_used_map(num_cores, false);
196*b9c1b51eSKate Stone   if (threads_list) {
197*b9c1b51eSKate Stone     if (log) {
19862f80036SGreg Clayton       StreamString strm;
1990641ca1aSZachary Turner       threads_list->Dump(strm);
20062f80036SGreg Clayton       log->Printf("threads_list = %s", strm.GetString().c_str());
20162f80036SGreg Clayton     }
2020641ca1aSZachary Turner 
2030641ca1aSZachary Turner     const uint32_t num_threads = threads_list->GetSize();
204*b9c1b51eSKate Stone     for (uint32_t i = 0; i < num_threads; ++i) {
205*b9c1b51eSKate Stone       StructuredData::ObjectSP thread_dict_obj =
206*b9c1b51eSKate Stone           threads_list->GetItemAtIndex(i);
207*b9c1b51eSKate Stone       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
208*b9c1b51eSKate Stone         ThreadSP thread_sp(
209*b9c1b51eSKate Stone             CreateThreadFromThreadInfo(*thread_dict, core_thread_list,
210*b9c1b51eSKate Stone                                        old_thread_list, core_used_map, NULL));
211a4d8747dSGreg Clayton         if (thread_sp)
212435ce139SGreg Clayton           new_thread_list.AddThread(thread_sp);
213435ce139SGreg Clayton       }
214435ce139SGreg Clayton     }
215435ce139SGreg Clayton   }
2166e0ff1a3SGreg Clayton 
217e98008ccSGreg Clayton   // Any real core threads that didn't end up backing a memory thread should
218*b9c1b51eSKate Stone   // still be in the main thread list, and they should be inserted at the
219*b9c1b51eSKate Stone   // beginning
220e98008ccSGreg Clayton   // of the list
221e98008ccSGreg Clayton   uint32_t insert_idx = 0;
222*b9c1b51eSKate Stone   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
223*b9c1b51eSKate Stone     if (core_used_map[core_idx] == false) {
224*b9c1b51eSKate Stone       new_thread_list.InsertThread(
225*b9c1b51eSKate Stone           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
226e98008ccSGreg Clayton       ++insert_idx;
227e98008ccSGreg Clayton     }
228e98008ccSGreg Clayton   }
2296e0ff1a3SGreg Clayton 
230b3e77600SGreg Clayton   return new_thread_list.GetSize(false) > 0;
231b3e77600SGreg Clayton }
232b3e77600SGreg Clayton 
233*b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
234*b9c1b51eSKate Stone     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
235*b9c1b51eSKate Stone     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
236*b9c1b51eSKate Stone     bool *did_create_ptr) {
237a4d8747dSGreg Clayton   ThreadSP thread_sp;
2380641ca1aSZachary Turner   tid_t tid = LLDB_INVALID_THREAD_ID;
2390641ca1aSZachary Turner   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2400641ca1aSZachary Turner     return ThreadSP();
241a4d8747dSGreg Clayton 
2420641ca1aSZachary Turner   uint32_t core_number;
2430641ca1aSZachary Turner   addr_t reg_data_addr;
2440641ca1aSZachary Turner   std::string name;
2450641ca1aSZachary Turner   std::string queue;
2460641ca1aSZachary Turner 
2470641ca1aSZachary Turner   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
248*b9c1b51eSKate Stone   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
249*b9c1b51eSKate Stone                                       LLDB_INVALID_ADDRESS);
2500641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("name", name);
2510641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("queue", queue);
252a4d8747dSGreg Clayton 
253160c9d81SGreg Clayton   // See if a thread already exists for "tid"
254b3ae8761SGreg Clayton   thread_sp = old_thread_list.FindThreadByID(tid, false);
255*b9c1b51eSKate Stone   if (thread_sp) {
256*b9c1b51eSKate Stone     // A thread already does exist for "tid", make sure it was an operating
257*b9c1b51eSKate Stone     // system
258160c9d81SGreg Clayton     // plug-in generated thread.
259*b9c1b51eSKate Stone     if (!IsOperatingSystemPluginThread(thread_sp)) {
260160c9d81SGreg Clayton       // We have thread ID overlap between the protocol threads and the
261160c9d81SGreg Clayton       // operating system threads, clear the thread so we create an
262160c9d81SGreg Clayton       // operating system thread for this.
263160c9d81SGreg Clayton       thread_sp.reset();
264160c9d81SGreg Clayton     }
265160c9d81SGreg Clayton   }
266160c9d81SGreg Clayton 
267*b9c1b51eSKate Stone   if (!thread_sp) {
268a4d8747dSGreg Clayton     if (did_create_ptr)
269a4d8747dSGreg Clayton       *did_create_ptr = true;
270*b9c1b51eSKate Stone     thread_sp.reset(new ThreadMemory(*m_process, tid, name.c_str(),
271*b9c1b51eSKate Stone                                      queue.c_str(), reg_data_addr));
272b3ae8761SGreg Clayton   }
273b3ae8761SGreg Clayton 
274*b9c1b51eSKate Stone   if (core_number < core_thread_list.GetSize(false)) {
275*b9c1b51eSKate Stone     ThreadSP core_thread_sp(
276*b9c1b51eSKate Stone         core_thread_list.GetThreadAtIndex(core_number, false));
277*b9c1b51eSKate Stone     if (core_thread_sp) {
278*b9c1b51eSKate Stone       // Keep track of which cores were set as the backing thread for memory
279*b9c1b51eSKate Stone       // threads...
280e98008ccSGreg Clayton       if (core_number < core_used_map.size())
281e98008ccSGreg Clayton         core_used_map[core_number] = true;
282e98008ccSGreg Clayton 
283160c9d81SGreg Clayton       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
284*b9c1b51eSKate Stone       if (backing_core_thread_sp) {
285160c9d81SGreg Clayton         thread_sp->SetBackingThread(backing_core_thread_sp);
286*b9c1b51eSKate Stone       } else {
287160c9d81SGreg Clayton         thread_sp->SetBackingThread(core_thread_sp);
288160c9d81SGreg Clayton       }
289160c9d81SGreg Clayton     }
290a4d8747dSGreg Clayton   }
291a4d8747dSGreg Clayton   return thread_sp;
292a4d8747dSGreg Clayton }
293a4d8747dSGreg Clayton 
294*b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
295b3e77600SGreg Clayton 
296b3e77600SGreg Clayton RegisterContextSP
297*b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
298*b9c1b51eSKate Stone                                                       addr_t reg_data_addr) {
299435ce139SGreg Clayton   RegisterContextSP reg_ctx_sp;
300a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp || !thread)
301160c9d81SGreg Clayton     return reg_ctx_sp;
302160c9d81SGreg Clayton 
303160c9d81SGreg Clayton   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
304160c9d81SGreg Clayton     return reg_ctx_sp;
3051c22be69SGreg Clayton 
306*b9c1b51eSKate Stone   // First thing we have to do is get the API lock, and the run lock.  We're
307*b9c1b51eSKate Stone   // going to change the thread
308*b9c1b51eSKate Stone   // content of the process, and we're going to use python, which requires the
309*b9c1b51eSKate Stone   // API lock to do it.
310*b9c1b51eSKate Stone   // So get & hold that.  This is a recursive lock so we can grant it to any
311*b9c1b51eSKate Stone   // Python code called on the stack below us.
31285e276b8SJim Ingham   Target &target = m_process->GetTarget();
313bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
31485e276b8SJim Ingham 
3155160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3161c22be69SGreg Clayton 
317*b9c1b51eSKate Stone   auto lock =
318*b9c1b51eSKate Stone       m_interpreter
319*b9c1b51eSKate Stone           ->AcquireInterpreterLock(); // to make sure python objects stays alive
320*b9c1b51eSKate Stone   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
321ead45e01SGreg Clayton     // The registers data is in contiguous memory, just create the register
322ead45e01SGreg Clayton     // context using the address provided
323ead45e01SGreg Clayton     if (log)
324*b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
325*b9c1b51eSKate Stone                   "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
326*b9c1b51eSKate Stone                   ") creating memory register context",
327*b9c1b51eSKate Stone                   thread->GetID(), thread->GetProtocolID(), reg_data_addr);
328*b9c1b51eSKate Stone     reg_ctx_sp.reset(new RegisterContextMemory(
329*b9c1b51eSKate Stone         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr));
330*b9c1b51eSKate Stone   } else {
331ead45e01SGreg Clayton     // No register data address is provided, query the python plug-in to let
332ead45e01SGreg Clayton     // it make up the data as it sees fit
3331c22be69SGreg Clayton     if (log)
334*b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
335*b9c1b51eSKate Stone                   "= 0x%" PRIx64 ", 0x%" PRIx64
336*b9c1b51eSKate Stone                   ") fetching register data from python",
337*b9c1b51eSKate Stone                   thread->GetID(), thread->GetProtocolID());
3381c22be69SGreg Clayton 
339*b9c1b51eSKate Stone     StructuredData::StringSP reg_context_data =
340*b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
341*b9c1b51eSKate Stone                                                     thread->GetID());
342*b9c1b51eSKate Stone     if (reg_context_data) {
3430641ca1aSZachary Turner       std::string value = reg_context_data->GetValue();
3440641ca1aSZachary Turner       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
345*b9c1b51eSKate Stone       if (data_sp->GetByteSize()) {
346*b9c1b51eSKate Stone         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
347*b9c1b51eSKate Stone             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
348*b9c1b51eSKate Stone         if (reg_ctx_memory) {
349435ce139SGreg Clayton           reg_ctx_sp.reset(reg_ctx_memory);
350435ce139SGreg Clayton           reg_ctx_memory->SetAllRegisterData(data_sp);
351435ce139SGreg Clayton         }
352435ce139SGreg Clayton       }
353435ce139SGreg Clayton     }
354ead45e01SGreg Clayton   }
355*b9c1b51eSKate Stone   // if we still have no register data, fallback on a dummy context to avoid
356*b9c1b51eSKate Stone   // crashing
357*b9c1b51eSKate Stone   if (!reg_ctx_sp) {
358cbd79b6cSEnrico Granata     if (log)
359*b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
360*b9c1b51eSKate Stone                   "= 0x%" PRIx64 ") forcing a dummy register context",
361*b9c1b51eSKate Stone                   thread->GetID());
362*b9c1b51eSKate Stone     reg_ctx_sp.reset(new RegisterContextDummy(
363*b9c1b51eSKate Stone         *thread, 0, target.GetArchitecture().GetAddressByteSize()));
364cbd79b6cSEnrico Granata   }
365b3e77600SGreg Clayton   return reg_ctx_sp;
366b3e77600SGreg Clayton }
367b3e77600SGreg Clayton 
368b3e77600SGreg Clayton StopInfoSP
369*b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
370b3e77600SGreg Clayton   // We should have gotten the thread stop info from the dictionary of data for
371b3e77600SGreg Clayton   // the thread in the initial call to get_thread_info(), this should have been
372b3e77600SGreg Clayton   // cached so we can return it here
373*b9c1b51eSKate Stone   StopInfoSP
374*b9c1b51eSKate Stone       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
375b3e77600SGreg Clayton   return stop_info_sp;
376b3e77600SGreg Clayton }
377b3e77600SGreg Clayton 
378*b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
379*b9c1b51eSKate Stone                                                    addr_t context) {
3805160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
381a4d8747dSGreg Clayton 
382a4d8747dSGreg Clayton   if (log)
383*b9c1b51eSKate Stone     log->Printf("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
384*b9c1b51eSKate Stone                 ", context = 0x%" PRIx64 ") fetching register data from python",
385*b9c1b51eSKate Stone                 tid, context);
386a4d8747dSGreg Clayton 
387*b9c1b51eSKate Stone   if (m_interpreter && m_python_object_sp) {
388*b9c1b51eSKate Stone     // First thing we have to do is get the API lock, and the run lock.  We're
389*b9c1b51eSKate Stone     // going to change the thread
390*b9c1b51eSKate Stone     // content of the process, and we're going to use python, which requires the
391*b9c1b51eSKate Stone     // API lock to do it.
392*b9c1b51eSKate Stone     // So get & hold that.  This is a recursive lock so we can grant it to any
393*b9c1b51eSKate Stone     // Python code called on the stack below us.
394a4d8747dSGreg Clayton     Target &target = m_process->GetTarget();
395bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex());
396a4d8747dSGreg Clayton 
397*b9c1b51eSKate Stone     auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure
398*b9c1b51eSKate Stone                                                          // thread_info_dict
399*b9c1b51eSKate Stone                                                          // stays alive
400*b9c1b51eSKate Stone     StructuredData::DictionarySP thread_info_dict =
401*b9c1b51eSKate Stone         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
402e98008ccSGreg Clayton     std::vector<bool> core_used_map;
403*b9c1b51eSKate Stone     if (thread_info_dict) {
404b3ae8761SGreg Clayton       ThreadList core_threads(m_process);
405a4d8747dSGreg Clayton       ThreadList &thread_list = m_process->GetThreadList();
406a4d8747dSGreg Clayton       bool did_create = false;
407*b9c1b51eSKate Stone       ThreadSP thread_sp(
408*b9c1b51eSKate Stone           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
409*b9c1b51eSKate Stone                                      thread_list, core_used_map, &did_create));
410a4d8747dSGreg Clayton       if (did_create)
411a4d8747dSGreg Clayton         thread_list.AddThread(thread_sp);
412a4d8747dSGreg Clayton       return thread_sp;
413a4d8747dSGreg Clayton     }
414a4d8747dSGreg Clayton   }
415a4d8747dSGreg Clayton   return ThreadSP();
416a4d8747dSGreg Clayton }
417a4d8747dSGreg Clayton 
418b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
419