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