xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1*061da546Spatrick //===-- OperatingSystemPython.cpp --------------------------------*- C++-*-===//
2*061da546Spatrick //
3*061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*061da546Spatrick //
7*061da546Spatrick //===----------------------------------------------------------------------===//
8*061da546Spatrick 
9*061da546Spatrick #include "lldb/Host/Config.h"
10*061da546Spatrick 
11*061da546Spatrick #if LLDB_ENABLE_PYTHON
12*061da546Spatrick 
13*061da546Spatrick #include "OperatingSystemPython.h"
14*061da546Spatrick 
15*061da546Spatrick #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
16*061da546Spatrick #include "Plugins/Process/Utility/RegisterContextDummy.h"
17*061da546Spatrick #include "Plugins/Process/Utility/RegisterContextMemory.h"
18*061da546Spatrick #include "Plugins/Process/Utility/ThreadMemory.h"
19*061da546Spatrick #include "lldb/Core/Debugger.h"
20*061da546Spatrick #include "lldb/Core/Module.h"
21*061da546Spatrick #include "lldb/Core/PluginManager.h"
22*061da546Spatrick #include "lldb/Core/ValueObjectVariable.h"
23*061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
24*061da546Spatrick #include "lldb/Interpreter/ScriptInterpreter.h"
25*061da546Spatrick #include "lldb/Symbol/ObjectFile.h"
26*061da546Spatrick #include "lldb/Symbol/VariableList.h"
27*061da546Spatrick #include "lldb/Target/Process.h"
28*061da546Spatrick #include "lldb/Target/StopInfo.h"
29*061da546Spatrick #include "lldb/Target/Target.h"
30*061da546Spatrick #include "lldb/Target/Thread.h"
31*061da546Spatrick #include "lldb/Target/ThreadList.h"
32*061da546Spatrick #include "lldb/Utility/DataBufferHeap.h"
33*061da546Spatrick #include "lldb/Utility/RegisterValue.h"
34*061da546Spatrick #include "lldb/Utility/StreamString.h"
35*061da546Spatrick #include "lldb/Utility/StructuredData.h"
36*061da546Spatrick 
37*061da546Spatrick #include <memory>
38*061da546Spatrick 
39*061da546Spatrick using namespace lldb;
40*061da546Spatrick using namespace lldb_private;
41*061da546Spatrick 
42*061da546Spatrick void OperatingSystemPython::Initialize() {
43*061da546Spatrick   PluginManager::RegisterPlugin(GetPluginNameStatic(),
44*061da546Spatrick                                 GetPluginDescriptionStatic(), CreateInstance,
45*061da546Spatrick                                 nullptr);
46*061da546Spatrick }
47*061da546Spatrick 
48*061da546Spatrick void OperatingSystemPython::Terminate() {
49*061da546Spatrick   PluginManager::UnregisterPlugin(CreateInstance);
50*061da546Spatrick }
51*061da546Spatrick 
52*061da546Spatrick OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
53*061da546Spatrick                                                        bool force) {
54*061da546Spatrick   // Python OperatingSystem plug-ins must be requested by name, so force must
55*061da546Spatrick   // be true
56*061da546Spatrick   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
57*061da546Spatrick   if (python_os_plugin_spec &&
58*061da546Spatrick       FileSystem::Instance().Exists(python_os_plugin_spec)) {
59*061da546Spatrick     std::unique_ptr<OperatingSystemPython> os_up(
60*061da546Spatrick         new OperatingSystemPython(process, python_os_plugin_spec));
61*061da546Spatrick     if (os_up.get() && os_up->IsValid())
62*061da546Spatrick       return os_up.release();
63*061da546Spatrick   }
64*061da546Spatrick   return nullptr;
65*061da546Spatrick }
66*061da546Spatrick 
67*061da546Spatrick ConstString OperatingSystemPython::GetPluginNameStatic() {
68*061da546Spatrick   static ConstString g_name("python");
69*061da546Spatrick   return g_name;
70*061da546Spatrick }
71*061da546Spatrick 
72*061da546Spatrick const char *OperatingSystemPython::GetPluginDescriptionStatic() {
73*061da546Spatrick   return "Operating system plug-in that gathers OS information from a python "
74*061da546Spatrick          "class that implements the necessary OperatingSystem functionality.";
75*061da546Spatrick }
76*061da546Spatrick 
77*061da546Spatrick OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
78*061da546Spatrick                                              const FileSpec &python_module_path)
79*061da546Spatrick     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
80*061da546Spatrick       m_interpreter(nullptr), m_python_object_sp() {
81*061da546Spatrick   if (!process)
82*061da546Spatrick     return;
83*061da546Spatrick   TargetSP target_sp = process->CalculateTarget();
84*061da546Spatrick   if (!target_sp)
85*061da546Spatrick     return;
86*061da546Spatrick   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
87*061da546Spatrick   if (m_interpreter) {
88*061da546Spatrick 
89*061da546Spatrick     std::string os_plugin_class_name(
90*061da546Spatrick         python_module_path.GetFilename().AsCString(""));
91*061da546Spatrick     if (!os_plugin_class_name.empty()) {
92*061da546Spatrick       const bool init_session = false;
93*061da546Spatrick       char python_module_path_cstr[PATH_MAX];
94*061da546Spatrick       python_module_path.GetPath(python_module_path_cstr,
95*061da546Spatrick                                  sizeof(python_module_path_cstr));
96*061da546Spatrick       Status error;
97*061da546Spatrick       if (m_interpreter->LoadScriptingModule(python_module_path_cstr,
98*061da546Spatrick                                              init_session, error)) {
99*061da546Spatrick         // Strip the ".py" extension if there is one
100*061da546Spatrick         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
101*061da546Spatrick         if (py_extension_pos != std::string::npos)
102*061da546Spatrick           os_plugin_class_name.erase(py_extension_pos);
103*061da546Spatrick         // Add ".OperatingSystemPlugIn" to the module name to get a string like
104*061da546Spatrick         // "modulename.OperatingSystemPlugIn"
105*061da546Spatrick         os_plugin_class_name += ".OperatingSystemPlugIn";
106*061da546Spatrick         StructuredData::ObjectSP object_sp =
107*061da546Spatrick             m_interpreter->OSPlugin_CreatePluginObject(
108*061da546Spatrick                 os_plugin_class_name.c_str(), process->CalculateProcess());
109*061da546Spatrick         if (object_sp && object_sp->IsValid())
110*061da546Spatrick           m_python_object_sp = object_sp;
111*061da546Spatrick       }
112*061da546Spatrick     }
113*061da546Spatrick   }
114*061da546Spatrick }
115*061da546Spatrick 
116*061da546Spatrick OperatingSystemPython::~OperatingSystemPython() {}
117*061da546Spatrick 
118*061da546Spatrick DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
119*061da546Spatrick   if (m_register_info_up == nullptr) {
120*061da546Spatrick     if (!m_interpreter || !m_python_object_sp)
121*061da546Spatrick       return nullptr;
122*061da546Spatrick     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
123*061da546Spatrick 
124*061da546Spatrick     LLDB_LOGF(log,
125*061da546Spatrick               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
126*061da546Spatrick               "thread register definitions from python for pid %" PRIu64,
127*061da546Spatrick               m_process->GetID());
128*061da546Spatrick 
129*061da546Spatrick     StructuredData::DictionarySP dictionary =
130*061da546Spatrick         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
131*061da546Spatrick     if (!dictionary)
132*061da546Spatrick       return nullptr;
133*061da546Spatrick 
134*061da546Spatrick     m_register_info_up.reset(new DynamicRegisterInfo(
135*061da546Spatrick         *dictionary, m_process->GetTarget().GetArchitecture()));
136*061da546Spatrick     assert(m_register_info_up->GetNumRegisters() > 0);
137*061da546Spatrick     assert(m_register_info_up->GetNumRegisterSets() > 0);
138*061da546Spatrick   }
139*061da546Spatrick   return m_register_info_up.get();
140*061da546Spatrick }
141*061da546Spatrick 
142*061da546Spatrick // PluginInterface protocol
143*061da546Spatrick ConstString OperatingSystemPython::GetPluginName() {
144*061da546Spatrick   return GetPluginNameStatic();
145*061da546Spatrick }
146*061da546Spatrick 
147*061da546Spatrick uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
148*061da546Spatrick 
149*061da546Spatrick bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
150*061da546Spatrick                                              ThreadList &core_thread_list,
151*061da546Spatrick                                              ThreadList &new_thread_list) {
152*061da546Spatrick   if (!m_interpreter || !m_python_object_sp)
153*061da546Spatrick     return false;
154*061da546Spatrick 
155*061da546Spatrick   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
156*061da546Spatrick 
157*061da546Spatrick   // First thing we have to do is to try to get the API lock, and the
158*061da546Spatrick   // interpreter lock. We're going to change the thread content of the process,
159*061da546Spatrick   // and we're going to use python, which requires the API lock to do it. We
160*061da546Spatrick   // need the interpreter lock to make sure thread_info_dict stays alive.
161*061da546Spatrick   //
162*061da546Spatrick   // If someone already has the API lock, that is ok, we just want to avoid
163*061da546Spatrick   // external code from making new API calls while this call is happening.
164*061da546Spatrick   //
165*061da546Spatrick   // This is a recursive lock so we can grant it to any Python code called on
166*061da546Spatrick   // the stack below us.
167*061da546Spatrick   Target &target = m_process->GetTarget();
168*061da546Spatrick   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
169*061da546Spatrick                                                   std::defer_lock);
170*061da546Spatrick   (void)api_lock.try_lock(); // See above.
171*061da546Spatrick   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
172*061da546Spatrick 
173*061da546Spatrick   LLDB_LOGF(log,
174*061da546Spatrick             "OperatingSystemPython::UpdateThreadList() fetching thread "
175*061da546Spatrick             "data from python for pid %" PRIu64,
176*061da546Spatrick             m_process->GetID());
177*061da546Spatrick 
178*061da546Spatrick   // The threads that are in "core_thread_list" upon entry are the threads from
179*061da546Spatrick   // the lldb_private::Process subclass, no memory threads will be in this
180*061da546Spatrick   // list.
181*061da546Spatrick   StructuredData::ArraySP threads_list =
182*061da546Spatrick       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
183*061da546Spatrick 
184*061da546Spatrick   const uint32_t num_cores = core_thread_list.GetSize(false);
185*061da546Spatrick 
186*061da546Spatrick   // Make a map so we can keep track of which cores were used from the
187*061da546Spatrick   // core_thread list. Any real threads/cores that weren't used should later be
188*061da546Spatrick   // put back into the "new_thread_list".
189*061da546Spatrick   std::vector<bool> core_used_map(num_cores, false);
190*061da546Spatrick   if (threads_list) {
191*061da546Spatrick     if (log) {
192*061da546Spatrick       StreamString strm;
193*061da546Spatrick       threads_list->Dump(strm);
194*061da546Spatrick       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
195*061da546Spatrick     }
196*061da546Spatrick 
197*061da546Spatrick     const uint32_t num_threads = threads_list->GetSize();
198*061da546Spatrick     for (uint32_t i = 0; i < num_threads; ++i) {
199*061da546Spatrick       StructuredData::ObjectSP thread_dict_obj =
200*061da546Spatrick           threads_list->GetItemAtIndex(i);
201*061da546Spatrick       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
202*061da546Spatrick         ThreadSP thread_sp(CreateThreadFromThreadInfo(
203*061da546Spatrick             *thread_dict, core_thread_list, old_thread_list, core_used_map,
204*061da546Spatrick             nullptr));
205*061da546Spatrick         if (thread_sp)
206*061da546Spatrick           new_thread_list.AddThread(thread_sp);
207*061da546Spatrick       }
208*061da546Spatrick     }
209*061da546Spatrick   }
210*061da546Spatrick 
211*061da546Spatrick   // Any real core threads that didn't end up backing a memory thread should
212*061da546Spatrick   // still be in the main thread list, and they should be inserted at the
213*061da546Spatrick   // beginning of the list
214*061da546Spatrick   uint32_t insert_idx = 0;
215*061da546Spatrick   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
216*061da546Spatrick     if (!core_used_map[core_idx]) {
217*061da546Spatrick       new_thread_list.InsertThread(
218*061da546Spatrick           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
219*061da546Spatrick       ++insert_idx;
220*061da546Spatrick     }
221*061da546Spatrick   }
222*061da546Spatrick 
223*061da546Spatrick   return new_thread_list.GetSize(false) > 0;
224*061da546Spatrick }
225*061da546Spatrick 
226*061da546Spatrick ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
227*061da546Spatrick     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
228*061da546Spatrick     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
229*061da546Spatrick     bool *did_create_ptr) {
230*061da546Spatrick   ThreadSP thread_sp;
231*061da546Spatrick   tid_t tid = LLDB_INVALID_THREAD_ID;
232*061da546Spatrick   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
233*061da546Spatrick     return ThreadSP();
234*061da546Spatrick 
235*061da546Spatrick   uint32_t core_number;
236*061da546Spatrick   addr_t reg_data_addr;
237*061da546Spatrick   llvm::StringRef name;
238*061da546Spatrick   llvm::StringRef queue;
239*061da546Spatrick 
240*061da546Spatrick   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
241*061da546Spatrick   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
242*061da546Spatrick                                       LLDB_INVALID_ADDRESS);
243*061da546Spatrick   thread_dict.GetValueForKeyAsString("name", name);
244*061da546Spatrick   thread_dict.GetValueForKeyAsString("queue", queue);
245*061da546Spatrick 
246*061da546Spatrick   // See if a thread already exists for "tid"
247*061da546Spatrick   thread_sp = old_thread_list.FindThreadByID(tid, false);
248*061da546Spatrick   if (thread_sp) {
249*061da546Spatrick     // A thread already does exist for "tid", make sure it was an operating
250*061da546Spatrick     // system
251*061da546Spatrick     // plug-in generated thread.
252*061da546Spatrick     if (!IsOperatingSystemPluginThread(thread_sp)) {
253*061da546Spatrick       // We have thread ID overlap between the protocol threads and the
254*061da546Spatrick       // operating system threads, clear the thread so we create an operating
255*061da546Spatrick       // system thread for this.
256*061da546Spatrick       thread_sp.reset();
257*061da546Spatrick     }
258*061da546Spatrick   }
259*061da546Spatrick 
260*061da546Spatrick   if (!thread_sp) {
261*061da546Spatrick     if (did_create_ptr)
262*061da546Spatrick       *did_create_ptr = true;
263*061da546Spatrick     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
264*061da546Spatrick                                                reg_data_addr);
265*061da546Spatrick   }
266*061da546Spatrick 
267*061da546Spatrick   if (core_number < core_thread_list.GetSize(false)) {
268*061da546Spatrick     ThreadSP core_thread_sp(
269*061da546Spatrick         core_thread_list.GetThreadAtIndex(core_number, false));
270*061da546Spatrick     if (core_thread_sp) {
271*061da546Spatrick       // Keep track of which cores were set as the backing thread for memory
272*061da546Spatrick       // threads...
273*061da546Spatrick       if (core_number < core_used_map.size())
274*061da546Spatrick         core_used_map[core_number] = true;
275*061da546Spatrick 
276*061da546Spatrick       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
277*061da546Spatrick       if (backing_core_thread_sp) {
278*061da546Spatrick         thread_sp->SetBackingThread(backing_core_thread_sp);
279*061da546Spatrick       } else {
280*061da546Spatrick         thread_sp->SetBackingThread(core_thread_sp);
281*061da546Spatrick       }
282*061da546Spatrick     }
283*061da546Spatrick   }
284*061da546Spatrick   return thread_sp;
285*061da546Spatrick }
286*061da546Spatrick 
287*061da546Spatrick void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
288*061da546Spatrick 
289*061da546Spatrick RegisterContextSP
290*061da546Spatrick OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
291*061da546Spatrick                                                       addr_t reg_data_addr) {
292*061da546Spatrick   RegisterContextSP reg_ctx_sp;
293*061da546Spatrick   if (!m_interpreter || !m_python_object_sp || !thread)
294*061da546Spatrick     return reg_ctx_sp;
295*061da546Spatrick 
296*061da546Spatrick   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
297*061da546Spatrick     return reg_ctx_sp;
298*061da546Spatrick 
299*061da546Spatrick   // First thing we have to do is to try to get the API lock, and the
300*061da546Spatrick   // interpreter lock. We're going to change the thread content of the process,
301*061da546Spatrick   // and we're going to use python, which requires the API lock to do it. We
302*061da546Spatrick   // need the interpreter lock to make sure thread_info_dict stays alive.
303*061da546Spatrick   //
304*061da546Spatrick   // If someone already has the API lock, that is ok, we just want to avoid
305*061da546Spatrick   // external code from making new API calls while this call is happening.
306*061da546Spatrick   //
307*061da546Spatrick   // This is a recursive lock so we can grant it to any Python code called on
308*061da546Spatrick   // the stack below us.
309*061da546Spatrick   Target &target = m_process->GetTarget();
310*061da546Spatrick   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
311*061da546Spatrick                                                   std::defer_lock);
312*061da546Spatrick   (void)api_lock.try_lock(); // See above.
313*061da546Spatrick   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
314*061da546Spatrick 
315*061da546Spatrick   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
316*061da546Spatrick 
317*061da546Spatrick   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
318*061da546Spatrick     // The registers data is in contiguous memory, just create the register
319*061da546Spatrick     // context using the address provided
320*061da546Spatrick     LLDB_LOGF(log,
321*061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
322*061da546Spatrick               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
323*061da546Spatrick               ") creating memory register context",
324*061da546Spatrick               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
325*061da546Spatrick     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
326*061da546Spatrick         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
327*061da546Spatrick   } else {
328*061da546Spatrick     // No register data address is provided, query the python plug-in to let it
329*061da546Spatrick     // make up the data as it sees fit
330*061da546Spatrick     LLDB_LOGF(log,
331*061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
332*061da546Spatrick               "= 0x%" PRIx64 ", 0x%" PRIx64
333*061da546Spatrick               ") fetching register data from python",
334*061da546Spatrick               thread->GetID(), thread->GetProtocolID());
335*061da546Spatrick 
336*061da546Spatrick     StructuredData::StringSP reg_context_data =
337*061da546Spatrick         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
338*061da546Spatrick                                                     thread->GetID());
339*061da546Spatrick     if (reg_context_data) {
340*061da546Spatrick       std::string value = reg_context_data->GetValue();
341*061da546Spatrick       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
342*061da546Spatrick       if (data_sp->GetByteSize()) {
343*061da546Spatrick         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
344*061da546Spatrick             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
345*061da546Spatrick         if (reg_ctx_memory) {
346*061da546Spatrick           reg_ctx_sp.reset(reg_ctx_memory);
347*061da546Spatrick           reg_ctx_memory->SetAllRegisterData(data_sp);
348*061da546Spatrick         }
349*061da546Spatrick       }
350*061da546Spatrick     }
351*061da546Spatrick   }
352*061da546Spatrick   // if we still have no register data, fallback on a dummy context to avoid
353*061da546Spatrick   // crashing
354*061da546Spatrick   if (!reg_ctx_sp) {
355*061da546Spatrick     LLDB_LOGF(log,
356*061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
357*061da546Spatrick               "= 0x%" PRIx64 ") forcing a dummy register context",
358*061da546Spatrick               thread->GetID());
359*061da546Spatrick     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
360*061da546Spatrick         *thread, 0, target.GetArchitecture().GetAddressByteSize());
361*061da546Spatrick   }
362*061da546Spatrick   return reg_ctx_sp;
363*061da546Spatrick }
364*061da546Spatrick 
365*061da546Spatrick StopInfoSP
366*061da546Spatrick OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
367*061da546Spatrick   // We should have gotten the thread stop info from the dictionary of data for
368*061da546Spatrick   // the thread in the initial call to get_thread_info(), this should have been
369*061da546Spatrick   // cached so we can return it here
370*061da546Spatrick   StopInfoSP
371*061da546Spatrick       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
372*061da546Spatrick   return stop_info_sp;
373*061da546Spatrick }
374*061da546Spatrick 
375*061da546Spatrick lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
376*061da546Spatrick                                                    addr_t context) {
377*061da546Spatrick   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
378*061da546Spatrick 
379*061da546Spatrick   LLDB_LOGF(log,
380*061da546Spatrick             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
381*061da546Spatrick             ", context = 0x%" PRIx64 ") fetching register data from python",
382*061da546Spatrick             tid, context);
383*061da546Spatrick 
384*061da546Spatrick   if (m_interpreter && m_python_object_sp) {
385*061da546Spatrick     // First thing we have to do is to try to get the API lock, and the
386*061da546Spatrick     // interpreter lock. We're going to change the thread content of the
387*061da546Spatrick     // process, and we're going to use python, which requires the API lock to
388*061da546Spatrick     // do it. We need the interpreter lock to make sure thread_info_dict stays
389*061da546Spatrick     // alive.
390*061da546Spatrick     //
391*061da546Spatrick     // If someone already has the API lock, that is ok, we just want to avoid
392*061da546Spatrick     // external code from making new API calls while this call is happening.
393*061da546Spatrick     //
394*061da546Spatrick     // This is a recursive lock so we can grant it to any Python code called on
395*061da546Spatrick     // the stack below us.
396*061da546Spatrick     Target &target = m_process->GetTarget();
397*061da546Spatrick     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
398*061da546Spatrick                                                     std::defer_lock);
399*061da546Spatrick     (void)api_lock.try_lock(); // See above.
400*061da546Spatrick     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
401*061da546Spatrick 
402*061da546Spatrick     StructuredData::DictionarySP thread_info_dict =
403*061da546Spatrick         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
404*061da546Spatrick     std::vector<bool> core_used_map;
405*061da546Spatrick     if (thread_info_dict) {
406*061da546Spatrick       ThreadList core_threads(m_process);
407*061da546Spatrick       ThreadList &thread_list = m_process->GetThreadList();
408*061da546Spatrick       bool did_create = false;
409*061da546Spatrick       ThreadSP thread_sp(
410*061da546Spatrick           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
411*061da546Spatrick                                      thread_list, core_used_map, &did_create));
412*061da546Spatrick       if (did_create)
413*061da546Spatrick         thread_list.AddThread(thread_sp);
414*061da546Spatrick       return thread_sp;
415*061da546Spatrick     }
416*061da546Spatrick   }
417*061da546Spatrick   return ThreadSP();
418*061da546Spatrick }
419*061da546Spatrick 
420*061da546Spatrick #endif // #if LLDB_ENABLE_PYTHON
421