xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1dda28197Spatrick //===-- OperatingSystemPython.cpp -----------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick 
9061da546Spatrick #include "lldb/Host/Config.h"
10061da546Spatrick 
11061da546Spatrick #if LLDB_ENABLE_PYTHON
12061da546Spatrick 
13061da546Spatrick #include "OperatingSystemPython.h"
14061da546Spatrick 
15061da546Spatrick #include "Plugins/Process/Utility/RegisterContextDummy.h"
16061da546Spatrick #include "Plugins/Process/Utility/RegisterContextMemory.h"
17061da546Spatrick #include "Plugins/Process/Utility/ThreadMemory.h"
18061da546Spatrick #include "lldb/Core/Debugger.h"
19061da546Spatrick #include "lldb/Core/Module.h"
20061da546Spatrick #include "lldb/Core/PluginManager.h"
21061da546Spatrick #include "lldb/Core/ValueObjectVariable.h"
22061da546Spatrick #include "lldb/Interpreter/CommandInterpreter.h"
23061da546Spatrick #include "lldb/Interpreter/ScriptInterpreter.h"
24061da546Spatrick #include "lldb/Symbol/ObjectFile.h"
25061da546Spatrick #include "lldb/Symbol/VariableList.h"
26061da546Spatrick #include "lldb/Target/Process.h"
27061da546Spatrick #include "lldb/Target/StopInfo.h"
28061da546Spatrick #include "lldb/Target/Target.h"
29061da546Spatrick #include "lldb/Target/Thread.h"
30061da546Spatrick #include "lldb/Target/ThreadList.h"
31061da546Spatrick #include "lldb/Utility/DataBufferHeap.h"
32*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
33061da546Spatrick #include "lldb/Utility/RegisterValue.h"
34061da546Spatrick #include "lldb/Utility/StreamString.h"
35061da546Spatrick #include "lldb/Utility/StructuredData.h"
36061da546Spatrick 
37061da546Spatrick #include <memory>
38061da546Spatrick 
39061da546Spatrick using namespace lldb;
40061da546Spatrick using namespace lldb_private;
41061da546Spatrick 
LLDB_PLUGIN_DEFINE(OperatingSystemPython)42dda28197Spatrick LLDB_PLUGIN_DEFINE(OperatingSystemPython)
43dda28197Spatrick 
44061da546Spatrick void OperatingSystemPython::Initialize() {
45061da546Spatrick   PluginManager::RegisterPlugin(GetPluginNameStatic(),
46061da546Spatrick                                 GetPluginDescriptionStatic(), CreateInstance,
47061da546Spatrick                                 nullptr);
48061da546Spatrick }
49061da546Spatrick 
Terminate()50061da546Spatrick void OperatingSystemPython::Terminate() {
51061da546Spatrick   PluginManager::UnregisterPlugin(CreateInstance);
52061da546Spatrick }
53061da546Spatrick 
CreateInstance(Process * process,bool force)54061da546Spatrick OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
55061da546Spatrick                                                        bool force) {
56061da546Spatrick   // Python OperatingSystem plug-ins must be requested by name, so force must
57061da546Spatrick   // be true
58061da546Spatrick   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
59061da546Spatrick   if (python_os_plugin_spec &&
60061da546Spatrick       FileSystem::Instance().Exists(python_os_plugin_spec)) {
61061da546Spatrick     std::unique_ptr<OperatingSystemPython> os_up(
62061da546Spatrick         new OperatingSystemPython(process, python_os_plugin_spec));
63061da546Spatrick     if (os_up.get() && os_up->IsValid())
64061da546Spatrick       return os_up.release();
65061da546Spatrick   }
66061da546Spatrick   return nullptr;
67061da546Spatrick }
68061da546Spatrick 
GetPluginDescriptionStatic()69*f6aab3d8Srobert llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
70061da546Spatrick   return "Operating system plug-in that gathers OS information from a python "
71061da546Spatrick          "class that implements the necessary OperatingSystem functionality.";
72061da546Spatrick }
73061da546Spatrick 
OperatingSystemPython(lldb_private::Process * process,const FileSpec & python_module_path)74061da546Spatrick OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
75061da546Spatrick                                              const FileSpec &python_module_path)
76061da546Spatrick     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77061da546Spatrick       m_interpreter(nullptr), m_python_object_sp() {
78061da546Spatrick   if (!process)
79061da546Spatrick     return;
80061da546Spatrick   TargetSP target_sp = process->CalculateTarget();
81061da546Spatrick   if (!target_sp)
82061da546Spatrick     return;
83061da546Spatrick   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
84061da546Spatrick   if (m_interpreter) {
85061da546Spatrick 
86061da546Spatrick     std::string os_plugin_class_name(
87061da546Spatrick         python_module_path.GetFilename().AsCString(""));
88061da546Spatrick     if (!os_plugin_class_name.empty()) {
89be691f3bSpatrick       LoadScriptOptions options;
90061da546Spatrick       char python_module_path_cstr[PATH_MAX];
91061da546Spatrick       python_module_path.GetPath(python_module_path_cstr,
92061da546Spatrick                                  sizeof(python_module_path_cstr));
93061da546Spatrick       Status error;
94be691f3bSpatrick       if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
95be691f3bSpatrick                                              error)) {
96061da546Spatrick         // Strip the ".py" extension if there is one
97061da546Spatrick         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
98061da546Spatrick         if (py_extension_pos != std::string::npos)
99061da546Spatrick           os_plugin_class_name.erase(py_extension_pos);
100061da546Spatrick         // Add ".OperatingSystemPlugIn" to the module name to get a string like
101061da546Spatrick         // "modulename.OperatingSystemPlugIn"
102061da546Spatrick         os_plugin_class_name += ".OperatingSystemPlugIn";
103061da546Spatrick         StructuredData::ObjectSP object_sp =
104061da546Spatrick             m_interpreter->OSPlugin_CreatePluginObject(
105061da546Spatrick                 os_plugin_class_name.c_str(), process->CalculateProcess());
106061da546Spatrick         if (object_sp && object_sp->IsValid())
107061da546Spatrick           m_python_object_sp = object_sp;
108061da546Spatrick       }
109061da546Spatrick     }
110061da546Spatrick   }
111061da546Spatrick }
112061da546Spatrick 
113be691f3bSpatrick OperatingSystemPython::~OperatingSystemPython() = default;
114061da546Spatrick 
GetDynamicRegisterInfo()115061da546Spatrick DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
116061da546Spatrick   if (m_register_info_up == nullptr) {
117061da546Spatrick     if (!m_interpreter || !m_python_object_sp)
118061da546Spatrick       return nullptr;
119*f6aab3d8Srobert     Log *log = GetLog(LLDBLog::OS);
120061da546Spatrick 
121061da546Spatrick     LLDB_LOGF(log,
122061da546Spatrick               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
123061da546Spatrick               "thread register definitions from python for pid %" PRIu64,
124061da546Spatrick               m_process->GetID());
125061da546Spatrick 
126061da546Spatrick     StructuredData::DictionarySP dictionary =
127061da546Spatrick         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
128061da546Spatrick     if (!dictionary)
129061da546Spatrick       return nullptr;
130061da546Spatrick 
131be691f3bSpatrick     m_register_info_up = std::make_unique<DynamicRegisterInfo>(
132be691f3bSpatrick         *dictionary, m_process->GetTarget().GetArchitecture());
133061da546Spatrick     assert(m_register_info_up->GetNumRegisters() > 0);
134061da546Spatrick     assert(m_register_info_up->GetNumRegisterSets() > 0);
135061da546Spatrick   }
136061da546Spatrick   return m_register_info_up.get();
137061da546Spatrick }
138061da546Spatrick 
UpdateThreadList(ThreadList & old_thread_list,ThreadList & core_thread_list,ThreadList & new_thread_list)139061da546Spatrick bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
140061da546Spatrick                                              ThreadList &core_thread_list,
141061da546Spatrick                                              ThreadList &new_thread_list) {
142061da546Spatrick   if (!m_interpreter || !m_python_object_sp)
143061da546Spatrick     return false;
144061da546Spatrick 
145*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::OS);
146061da546Spatrick 
147061da546Spatrick   // First thing we have to do is to try to get the API lock, and the
148061da546Spatrick   // interpreter lock. We're going to change the thread content of the process,
149061da546Spatrick   // and we're going to use python, which requires the API lock to do it. We
150061da546Spatrick   // need the interpreter lock to make sure thread_info_dict stays alive.
151061da546Spatrick   //
152061da546Spatrick   // If someone already has the API lock, that is ok, we just want to avoid
153061da546Spatrick   // external code from making new API calls while this call is happening.
154061da546Spatrick   //
155061da546Spatrick   // This is a recursive lock so we can grant it to any Python code called on
156061da546Spatrick   // the stack below us.
157061da546Spatrick   Target &target = m_process->GetTarget();
158061da546Spatrick   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
159061da546Spatrick                                                   std::defer_lock);
160061da546Spatrick   (void)api_lock.try_lock(); // See above.
161061da546Spatrick   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
162061da546Spatrick 
163061da546Spatrick   LLDB_LOGF(log,
164061da546Spatrick             "OperatingSystemPython::UpdateThreadList() fetching thread "
165061da546Spatrick             "data from python for pid %" PRIu64,
166061da546Spatrick             m_process->GetID());
167061da546Spatrick 
168061da546Spatrick   // The threads that are in "core_thread_list" upon entry are the threads from
169061da546Spatrick   // the lldb_private::Process subclass, no memory threads will be in this
170061da546Spatrick   // list.
171061da546Spatrick   StructuredData::ArraySP threads_list =
172061da546Spatrick       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
173061da546Spatrick 
174061da546Spatrick   const uint32_t num_cores = core_thread_list.GetSize(false);
175061da546Spatrick 
176061da546Spatrick   // Make a map so we can keep track of which cores were used from the
177061da546Spatrick   // core_thread list. Any real threads/cores that weren't used should later be
178061da546Spatrick   // put back into the "new_thread_list".
179061da546Spatrick   std::vector<bool> core_used_map(num_cores, false);
180061da546Spatrick   if (threads_list) {
181061da546Spatrick     if (log) {
182061da546Spatrick       StreamString strm;
183061da546Spatrick       threads_list->Dump(strm);
184061da546Spatrick       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
185061da546Spatrick     }
186061da546Spatrick 
187061da546Spatrick     const uint32_t num_threads = threads_list->GetSize();
188061da546Spatrick     for (uint32_t i = 0; i < num_threads; ++i) {
189061da546Spatrick       StructuredData::ObjectSP thread_dict_obj =
190061da546Spatrick           threads_list->GetItemAtIndex(i);
191061da546Spatrick       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
192061da546Spatrick         ThreadSP thread_sp(CreateThreadFromThreadInfo(
193061da546Spatrick             *thread_dict, core_thread_list, old_thread_list, core_used_map,
194061da546Spatrick             nullptr));
195061da546Spatrick         if (thread_sp)
196061da546Spatrick           new_thread_list.AddThread(thread_sp);
197061da546Spatrick       }
198061da546Spatrick     }
199061da546Spatrick   }
200061da546Spatrick 
201061da546Spatrick   // Any real core threads that didn't end up backing a memory thread should
202061da546Spatrick   // still be in the main thread list, and they should be inserted at the
203061da546Spatrick   // beginning of the list
204061da546Spatrick   uint32_t insert_idx = 0;
205061da546Spatrick   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
206061da546Spatrick     if (!core_used_map[core_idx]) {
207061da546Spatrick       new_thread_list.InsertThread(
208061da546Spatrick           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
209061da546Spatrick       ++insert_idx;
210061da546Spatrick     }
211061da546Spatrick   }
212061da546Spatrick 
213061da546Spatrick   return new_thread_list.GetSize(false) > 0;
214061da546Spatrick }
215061da546Spatrick 
CreateThreadFromThreadInfo(StructuredData::Dictionary & thread_dict,ThreadList & core_thread_list,ThreadList & old_thread_list,std::vector<bool> & core_used_map,bool * did_create_ptr)216061da546Spatrick ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
217061da546Spatrick     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
218061da546Spatrick     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
219061da546Spatrick     bool *did_create_ptr) {
220061da546Spatrick   ThreadSP thread_sp;
221061da546Spatrick   tid_t tid = LLDB_INVALID_THREAD_ID;
222061da546Spatrick   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
223061da546Spatrick     return ThreadSP();
224061da546Spatrick 
225061da546Spatrick   uint32_t core_number;
226061da546Spatrick   addr_t reg_data_addr;
227061da546Spatrick   llvm::StringRef name;
228061da546Spatrick   llvm::StringRef queue;
229061da546Spatrick 
230061da546Spatrick   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
231061da546Spatrick   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
232061da546Spatrick                                       LLDB_INVALID_ADDRESS);
233061da546Spatrick   thread_dict.GetValueForKeyAsString("name", name);
234061da546Spatrick   thread_dict.GetValueForKeyAsString("queue", queue);
235061da546Spatrick 
236061da546Spatrick   // See if a thread already exists for "tid"
237061da546Spatrick   thread_sp = old_thread_list.FindThreadByID(tid, false);
238061da546Spatrick   if (thread_sp) {
239061da546Spatrick     // A thread already does exist for "tid", make sure it was an operating
240061da546Spatrick     // system
241061da546Spatrick     // plug-in generated thread.
242061da546Spatrick     if (!IsOperatingSystemPluginThread(thread_sp)) {
243061da546Spatrick       // We have thread ID overlap between the protocol threads and the
244061da546Spatrick       // operating system threads, clear the thread so we create an operating
245061da546Spatrick       // system thread for this.
246061da546Spatrick       thread_sp.reset();
247061da546Spatrick     }
248061da546Spatrick   }
249061da546Spatrick 
250061da546Spatrick   if (!thread_sp) {
251061da546Spatrick     if (did_create_ptr)
252061da546Spatrick       *did_create_ptr = true;
253061da546Spatrick     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
254061da546Spatrick                                                reg_data_addr);
255061da546Spatrick   }
256061da546Spatrick 
257061da546Spatrick   if (core_number < core_thread_list.GetSize(false)) {
258061da546Spatrick     ThreadSP core_thread_sp(
259061da546Spatrick         core_thread_list.GetThreadAtIndex(core_number, false));
260061da546Spatrick     if (core_thread_sp) {
261061da546Spatrick       // Keep track of which cores were set as the backing thread for memory
262061da546Spatrick       // threads...
263061da546Spatrick       if (core_number < core_used_map.size())
264061da546Spatrick         core_used_map[core_number] = true;
265061da546Spatrick 
266061da546Spatrick       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
267061da546Spatrick       if (backing_core_thread_sp) {
268061da546Spatrick         thread_sp->SetBackingThread(backing_core_thread_sp);
269061da546Spatrick       } else {
270061da546Spatrick         thread_sp->SetBackingThread(core_thread_sp);
271061da546Spatrick       }
272061da546Spatrick     }
273061da546Spatrick   }
274061da546Spatrick   return thread_sp;
275061da546Spatrick }
276061da546Spatrick 
ThreadWasSelected(Thread * thread)277061da546Spatrick void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
278061da546Spatrick 
279061da546Spatrick RegisterContextSP
CreateRegisterContextForThread(Thread * thread,addr_t reg_data_addr)280061da546Spatrick OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
281061da546Spatrick                                                       addr_t reg_data_addr) {
282061da546Spatrick   RegisterContextSP reg_ctx_sp;
283061da546Spatrick   if (!m_interpreter || !m_python_object_sp || !thread)
284061da546Spatrick     return reg_ctx_sp;
285061da546Spatrick 
286061da546Spatrick   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
287061da546Spatrick     return reg_ctx_sp;
288061da546Spatrick 
289061da546Spatrick   // First thing we have to do is to try to get the API lock, and the
290061da546Spatrick   // interpreter lock. We're going to change the thread content of the process,
291061da546Spatrick   // and we're going to use python, which requires the API lock to do it. We
292061da546Spatrick   // need the interpreter lock to make sure thread_info_dict stays alive.
293061da546Spatrick   //
294061da546Spatrick   // If someone already has the API lock, that is ok, we just want to avoid
295061da546Spatrick   // external code from making new API calls while this call is happening.
296061da546Spatrick   //
297061da546Spatrick   // This is a recursive lock so we can grant it to any Python code called on
298061da546Spatrick   // the stack below us.
299061da546Spatrick   Target &target = m_process->GetTarget();
300061da546Spatrick   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
301061da546Spatrick                                                   std::defer_lock);
302061da546Spatrick   (void)api_lock.try_lock(); // See above.
303061da546Spatrick   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
304061da546Spatrick 
305*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
306061da546Spatrick 
307061da546Spatrick   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
308061da546Spatrick     // The registers data is in contiguous memory, just create the register
309061da546Spatrick     // context using the address provided
310061da546Spatrick     LLDB_LOGF(log,
311061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
312061da546Spatrick               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
313061da546Spatrick               ") creating memory register context",
314061da546Spatrick               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
315061da546Spatrick     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
316061da546Spatrick         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
317061da546Spatrick   } else {
318061da546Spatrick     // No register data address is provided, query the python plug-in to let it
319061da546Spatrick     // make up the data as it sees fit
320061da546Spatrick     LLDB_LOGF(log,
321061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
322061da546Spatrick               "= 0x%" PRIx64 ", 0x%" PRIx64
323061da546Spatrick               ") fetching register data from python",
324061da546Spatrick               thread->GetID(), thread->GetProtocolID());
325061da546Spatrick 
326061da546Spatrick     StructuredData::StringSP reg_context_data =
327061da546Spatrick         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
328061da546Spatrick                                                     thread->GetID());
329061da546Spatrick     if (reg_context_data) {
330dda28197Spatrick       std::string value = std::string(reg_context_data->GetValue());
331061da546Spatrick       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
332061da546Spatrick       if (data_sp->GetByteSize()) {
333061da546Spatrick         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
334061da546Spatrick             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
335061da546Spatrick         if (reg_ctx_memory) {
336061da546Spatrick           reg_ctx_sp.reset(reg_ctx_memory);
337061da546Spatrick           reg_ctx_memory->SetAllRegisterData(data_sp);
338061da546Spatrick         }
339061da546Spatrick       }
340061da546Spatrick     }
341061da546Spatrick   }
342061da546Spatrick   // if we still have no register data, fallback on a dummy context to avoid
343061da546Spatrick   // crashing
344061da546Spatrick   if (!reg_ctx_sp) {
345061da546Spatrick     LLDB_LOGF(log,
346061da546Spatrick               "OperatingSystemPython::CreateRegisterContextForThread (tid "
347061da546Spatrick               "= 0x%" PRIx64 ") forcing a dummy register context",
348061da546Spatrick               thread->GetID());
349061da546Spatrick     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
350061da546Spatrick         *thread, 0, target.GetArchitecture().GetAddressByteSize());
351061da546Spatrick   }
352061da546Spatrick   return reg_ctx_sp;
353061da546Spatrick }
354061da546Spatrick 
355061da546Spatrick StopInfoSP
CreateThreadStopReason(lldb_private::Thread * thread)356061da546Spatrick OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
357061da546Spatrick   // We should have gotten the thread stop info from the dictionary of data for
358061da546Spatrick   // the thread in the initial call to get_thread_info(), this should have been
359061da546Spatrick   // cached so we can return it here
360061da546Spatrick   StopInfoSP
361061da546Spatrick       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
362061da546Spatrick   return stop_info_sp;
363061da546Spatrick }
364061da546Spatrick 
CreateThread(lldb::tid_t tid,addr_t context)365061da546Spatrick lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
366061da546Spatrick                                                    addr_t context) {
367*f6aab3d8Srobert   Log *log = GetLog(LLDBLog::Thread);
368061da546Spatrick 
369061da546Spatrick   LLDB_LOGF(log,
370061da546Spatrick             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
371061da546Spatrick             ", context = 0x%" PRIx64 ") fetching register data from python",
372061da546Spatrick             tid, context);
373061da546Spatrick 
374061da546Spatrick   if (m_interpreter && m_python_object_sp) {
375061da546Spatrick     // First thing we have to do is to try to get the API lock, and the
376061da546Spatrick     // interpreter lock. We're going to change the thread content of the
377061da546Spatrick     // process, and we're going to use python, which requires the API lock to
378061da546Spatrick     // do it. We need the interpreter lock to make sure thread_info_dict stays
379061da546Spatrick     // alive.
380061da546Spatrick     //
381061da546Spatrick     // If someone already has the API lock, that is ok, we just want to avoid
382061da546Spatrick     // external code from making new API calls while this call is happening.
383061da546Spatrick     //
384061da546Spatrick     // This is a recursive lock so we can grant it to any Python code called on
385061da546Spatrick     // the stack below us.
386061da546Spatrick     Target &target = m_process->GetTarget();
387061da546Spatrick     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
388061da546Spatrick                                                     std::defer_lock);
389061da546Spatrick     (void)api_lock.try_lock(); // See above.
390061da546Spatrick     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
391061da546Spatrick 
392061da546Spatrick     StructuredData::DictionarySP thread_info_dict =
393061da546Spatrick         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
394061da546Spatrick     std::vector<bool> core_used_map;
395061da546Spatrick     if (thread_info_dict) {
396061da546Spatrick       ThreadList core_threads(m_process);
397061da546Spatrick       ThreadList &thread_list = m_process->GetThreadList();
398061da546Spatrick       bool did_create = false;
399061da546Spatrick       ThreadSP thread_sp(
400061da546Spatrick           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
401061da546Spatrick                                      thread_list, core_used_map, &did_create));
402061da546Spatrick       if (did_create)
403061da546Spatrick         thread_list.AddThread(thread_sp);
404061da546Spatrick       return thread_sp;
405061da546Spatrick     }
406061da546Spatrick   }
407061da546Spatrick   return ThreadSP();
408061da546Spatrick }
409061da546Spatrick 
410061da546Spatrick #endif // #if LLDB_ENABLE_PYTHON
411