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