180814287SRaphael Isemann //===-- OperatingSystemPython.cpp -----------------------------------------===//
2b3e77600SGreg Clayton //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b3e77600SGreg Clayton //
7b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
893a64300SDaniel Malea 
959998b7bSJonas Devlieghere #include "lldb/Host/Config.h"
1059998b7bSJonas Devlieghere 
114e26cf2cSJonas Devlieghere #if LLDB_ENABLE_PYTHON
12b3e77600SGreg Clayton 
13b3e77600SGreg Clayton #include "OperatingSystemPython.h"
14796ac80bSJonas Devlieghere 
15b9c1b51eSKate Stone #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
16b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h"
17b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h"
18b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h"
195790759aSEnrico Granata #include "lldb/Core/Debugger.h"
20b3e77600SGreg Clayton #include "lldb/Core/Module.h"
21b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
22b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
235790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
240641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h"
25b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
26b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
27b3e77600SGreg Clayton #include "lldb/Target/Process.h"
28b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
29b3e77600SGreg Clayton #include "lldb/Target/Target.h"
30b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
31b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h"
32666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
33d821c997SPavel Labath #include "lldb/Utility/RegisterValue.h"
34bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
35f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
36b3e77600SGreg Clayton 
37796ac80bSJonas Devlieghere #include <memory>
38796ac80bSJonas Devlieghere 
39b3e77600SGreg Clayton using namespace lldb;
40b3e77600SGreg Clayton using namespace lldb_private;
41b3e77600SGreg Clayton 
42b9c1b51eSKate Stone void OperatingSystemPython::Initialize() {
43b9c1b51eSKate Stone   PluginManager::RegisterPlugin(GetPluginNameStatic(),
44b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
45b9c1b51eSKate Stone                                 nullptr);
46b3e77600SGreg Clayton }
47b3e77600SGreg Clayton 
48b9c1b51eSKate Stone void OperatingSystemPython::Terminate() {
49b3e77600SGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
50b3e77600SGreg Clayton }
51b3e77600SGreg Clayton 
52b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
53b9c1b51eSKate Stone                                                        bool force) {
5405097246SAdrian Prantl   // Python OperatingSystem plug-ins must be requested by name, so force must
5505097246SAdrian Prantl   // be true
56c9d645d3SGreg Clayton   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
57dbd7fabaSJonas Devlieghere   if (python_os_plugin_spec &&
58dbd7fabaSJonas Devlieghere       FileSystem::Instance().Exists(python_os_plugin_spec)) {
59d5b44036SJonas Devlieghere     std::unique_ptr<OperatingSystemPython> os_up(
60b9c1b51eSKate Stone         new OperatingSystemPython(process, python_os_plugin_spec));
61d5b44036SJonas Devlieghere     if (os_up.get() && os_up->IsValid())
62d5b44036SJonas Devlieghere       return os_up.release();
63c9d645d3SGreg Clayton   }
64248a1305SKonrad Kleine   return nullptr;
65b3e77600SGreg Clayton }
66b3e77600SGreg Clayton 
67b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginNameStatic() {
6857abc5d6SGreg Clayton   static ConstString g_name("python");
6957abc5d6SGreg Clayton   return g_name;
70b3e77600SGreg Clayton }
71b3e77600SGreg Clayton 
72b9c1b51eSKate Stone const char *OperatingSystemPython::GetPluginDescriptionStatic() {
73b9c1b51eSKate Stone   return "Operating system plug-in that gathers OS information from a python "
74b9c1b51eSKate Stone          "class that implements the necessary OperatingSystem functionality.";
75b3e77600SGreg Clayton }
76b3e77600SGreg Clayton 
77b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
78b9c1b51eSKate Stone                                              const FileSpec &python_module_path)
79d5b44036SJonas Devlieghere     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
80248a1305SKonrad Kleine       m_interpreter(nullptr), m_python_object_sp() {
815790759aSEnrico Granata   if (!process)
825790759aSEnrico Granata     return;
83a4d8747dSGreg Clayton   TargetSP target_sp = process->CalculateTarget();
845790759aSEnrico Granata   if (!target_sp)
855790759aSEnrico Granata     return;
862b29b432SJonas Devlieghere   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
87b9c1b51eSKate Stone   if (m_interpreter) {
882443cbd7SGreg Clayton 
89b9c1b51eSKate Stone     std::string os_plugin_class_name(
90b9c1b51eSKate Stone         python_module_path.GetFilename().AsCString(""));
91b9c1b51eSKate Stone     if (!os_plugin_class_name.empty()) {
92c9d645d3SGreg Clayton       const bool init_session = false;
93c9d645d3SGreg Clayton       char python_module_path_cstr[PATH_MAX];
94b9c1b51eSKate Stone       python_module_path.GetPath(python_module_path_cstr,
95b9c1b51eSKate Stone                                  sizeof(python_module_path_cstr));
9697206d57SZachary Turner       Status error;
9715625112SJonas Devlieghere       if (m_interpreter->LoadScriptingModule(python_module_path_cstr,
9815625112SJonas Devlieghere                                              init_session, error)) {
99c9d645d3SGreg Clayton         // Strip the ".py" extension if there is one
100c9d645d3SGreg Clayton         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
101c9d645d3SGreg Clayton         if (py_extension_pos != std::string::npos)
102c9d645d3SGreg Clayton           os_plugin_class_name.erase(py_extension_pos);
103b9c1b51eSKate Stone         // Add ".OperatingSystemPlugIn" to the module name to get a string like
104b9c1b51eSKate Stone         // "modulename.OperatingSystemPlugIn"
105c9d645d3SGreg Clayton         os_plugin_class_name += ".OperatingSystemPlugIn";
1060641ca1aSZachary Turner         StructuredData::ObjectSP object_sp =
107b9c1b51eSKate Stone             m_interpreter->OSPlugin_CreatePluginObject(
108b9c1b51eSKate Stone                 os_plugin_class_name.c_str(), process->CalculateProcess());
1090641ca1aSZachary Turner         if (object_sp && object_sp->IsValid())
110a4d8747dSGreg Clayton           m_python_object_sp = object_sp;
111c9d645d3SGreg Clayton       }
1122443cbd7SGreg Clayton     }
1135790759aSEnrico Granata   }
114b3e77600SGreg Clayton }
115b3e77600SGreg Clayton 
116b9c1b51eSKate Stone OperatingSystemPython::~OperatingSystemPython() {}
117b3e77600SGreg Clayton 
118b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
119248a1305SKonrad Kleine   if (m_register_info_up == nullptr) {
120a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
121248a1305SKonrad Kleine       return nullptr;
12262f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1231c22be69SGreg Clayton 
12463e5fb76SJonas Devlieghere     LLDB_LOGF(log,
12563e5fb76SJonas Devlieghere               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
126b9c1b51eSKate Stone               "thread register definitions from python for pid %" PRIu64,
127b9c1b51eSKate Stone               m_process->GetID());
1281c22be69SGreg Clayton 
129b9c1b51eSKate Stone     StructuredData::DictionarySP dictionary =
130b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1315790759aSEnrico Granata     if (!dictionary)
132248a1305SKonrad Kleine       return nullptr;
133b3e77600SGreg Clayton 
134d5b44036SJonas Devlieghere     m_register_info_up.reset(new DynamicRegisterInfo(
135b9c1b51eSKate Stone         *dictionary, m_process->GetTarget().GetArchitecture()));
136d5b44036SJonas Devlieghere     assert(m_register_info_up->GetNumRegisters() > 0);
137d5b44036SJonas Devlieghere     assert(m_register_info_up->GetNumRegisterSets() > 0);
138b3e77600SGreg Clayton   }
139d5b44036SJonas Devlieghere   return m_register_info_up.get();
140b3e77600SGreg Clayton }
141b3e77600SGreg Clayton 
142b3e77600SGreg Clayton // PluginInterface protocol
143b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginName() {
144b3e77600SGreg Clayton   return GetPluginNameStatic();
145b3e77600SGreg Clayton }
146b3e77600SGreg Clayton 
147b9c1b51eSKate Stone uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
148b3e77600SGreg Clayton 
149b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
150ba4e61d3SAndrew Kaylor                                              ThreadList &core_thread_list,
151b9c1b51eSKate Stone                                              ThreadList &new_thread_list) {
152a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp)
153a85e6b6cSDaniel Malea     return false;
1541c22be69SGreg Clayton 
15562f80036SGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1561c22be69SGreg Clayton 
15785a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
15885a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
15985a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
16085a5ec51SJonas Devlieghere   // need the interpreter lock to make sure thread_info_dict stays alive.
161ab745c2aSGreg Clayton   //
162ab745c2aSGreg Clayton   // If someone already has the API lock, that is ok, we just want to avoid
163ab745c2aSGreg Clayton   // external code from making new API calls while this call is happening.
164ab745c2aSGreg Clayton   //
165ab745c2aSGreg Clayton   // This is a recursive lock so we can grant it to any Python code called on
166ab745c2aSGreg Clayton   // the stack below us.
16785e276b8SJim Ingham   Target &target = m_process->GetTarget();
16885a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
169b9c1b51eSKate Stone                                                   std::defer_lock);
1704d23764dSReid Kleckner   (void)api_lock.try_lock(); // See above.
17185a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
17285e276b8SJim Ingham 
17363e5fb76SJonas Devlieghere   LLDB_LOGF(log,
17463e5fb76SJonas Devlieghere             "OperatingSystemPython::UpdateThreadList() fetching thread "
175b9c1b51eSKate Stone             "data from python for pid %" PRIu64,
176b9c1b51eSKate Stone             m_process->GetID());
1771c22be69SGreg Clayton 
1788240b0d7SJim Ingham   // The threads that are in "core_thread_list" upon entry are the threads from
17985a5ec51SJonas Devlieghere   // the lldb_private::Process subclass, no memory threads will be in this
18085a5ec51SJonas Devlieghere   // list.
181b9c1b51eSKate Stone   StructuredData::ArraySP threads_list =
182b9c1b51eSKate Stone       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
183e98008ccSGreg Clayton 
184e98008ccSGreg Clayton   const uint32_t num_cores = core_thread_list.GetSize(false);
185e98008ccSGreg Clayton 
186e98008ccSGreg Clayton   // Make a map so we can keep track of which cores were used from the
18705097246SAdrian Prantl   // core_thread list. Any real threads/cores that weren't used should later be
18805097246SAdrian Prantl   // put back into the "new_thread_list".
189e98008ccSGreg Clayton   std::vector<bool> core_used_map(num_cores, false);
190b9c1b51eSKate Stone   if (threads_list) {
191b9c1b51eSKate Stone     if (log) {
19262f80036SGreg Clayton       StreamString strm;
1930641ca1aSZachary Turner       threads_list->Dump(strm);
19463e5fb76SJonas Devlieghere       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
19562f80036SGreg Clayton     }
1960641ca1aSZachary Turner 
1970641ca1aSZachary Turner     const uint32_t num_threads = threads_list->GetSize();
198b9c1b51eSKate Stone     for (uint32_t i = 0; i < num_threads; ++i) {
199b9c1b51eSKate Stone       StructuredData::ObjectSP thread_dict_obj =
200b9c1b51eSKate Stone           threads_list->GetItemAtIndex(i);
201b9c1b51eSKate Stone       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
202248a1305SKonrad Kleine         ThreadSP thread_sp(CreateThreadFromThreadInfo(
203248a1305SKonrad Kleine             *thread_dict, core_thread_list, old_thread_list, core_used_map,
204248a1305SKonrad Kleine             nullptr));
205a4d8747dSGreg Clayton         if (thread_sp)
206435ce139SGreg Clayton           new_thread_list.AddThread(thread_sp);
207435ce139SGreg Clayton       }
208435ce139SGreg Clayton     }
209435ce139SGreg Clayton   }
2106e0ff1a3SGreg Clayton 
211e98008ccSGreg Clayton   // Any real core threads that didn't end up backing a memory thread should
212b9c1b51eSKate Stone   // still be in the main thread list, and they should be inserted at the
21305097246SAdrian Prantl   // beginning of the list
214e98008ccSGreg Clayton   uint32_t insert_idx = 0;
215b9c1b51eSKate Stone   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
216a6682a41SJonas Devlieghere     if (!core_used_map[core_idx]) {
217b9c1b51eSKate Stone       new_thread_list.InsertThread(
218b9c1b51eSKate Stone           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
219e98008ccSGreg Clayton       ++insert_idx;
220e98008ccSGreg Clayton     }
221e98008ccSGreg Clayton   }
2226e0ff1a3SGreg Clayton 
223b3e77600SGreg Clayton   return new_thread_list.GetSize(false) > 0;
224b3e77600SGreg Clayton }
225b3e77600SGreg Clayton 
226b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
227b9c1b51eSKate Stone     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
228b9c1b51eSKate Stone     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
229b9c1b51eSKate Stone     bool *did_create_ptr) {
230a4d8747dSGreg Clayton   ThreadSP thread_sp;
2310641ca1aSZachary Turner   tid_t tid = LLDB_INVALID_THREAD_ID;
2320641ca1aSZachary Turner   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2330641ca1aSZachary Turner     return ThreadSP();
234a4d8747dSGreg Clayton 
2350641ca1aSZachary Turner   uint32_t core_number;
2360641ca1aSZachary Turner   addr_t reg_data_addr;
2372833321fSZachary Turner   llvm::StringRef name;
2382833321fSZachary Turner   llvm::StringRef queue;
2390641ca1aSZachary Turner 
2400641ca1aSZachary Turner   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
241b9c1b51eSKate Stone   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
242b9c1b51eSKate Stone                                       LLDB_INVALID_ADDRESS);
2430641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("name", name);
2440641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("queue", queue);
245a4d8747dSGreg Clayton 
246160c9d81SGreg Clayton   // See if a thread already exists for "tid"
247b3ae8761SGreg Clayton   thread_sp = old_thread_list.FindThreadByID(tid, false);
248b9c1b51eSKate Stone   if (thread_sp) {
249b9c1b51eSKate Stone     // A thread already does exist for "tid", make sure it was an operating
250b9c1b51eSKate Stone     // system
251160c9d81SGreg Clayton     // plug-in generated thread.
252b9c1b51eSKate Stone     if (!IsOperatingSystemPluginThread(thread_sp)) {
253160c9d81SGreg Clayton       // We have thread ID overlap between the protocol threads and the
25405097246SAdrian Prantl       // operating system threads, clear the thread so we create an operating
25505097246SAdrian Prantl       // system thread for this.
256160c9d81SGreg Clayton       thread_sp.reset();
257160c9d81SGreg Clayton     }
258160c9d81SGreg Clayton   }
259160c9d81SGreg Clayton 
260b9c1b51eSKate Stone   if (!thread_sp) {
261a4d8747dSGreg Clayton     if (did_create_ptr)
262a4d8747dSGreg Clayton       *did_create_ptr = true;
263796ac80bSJonas Devlieghere     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
264796ac80bSJonas Devlieghere                                                reg_data_addr);
265b3ae8761SGreg Clayton   }
266b3ae8761SGreg Clayton 
267b9c1b51eSKate Stone   if (core_number < core_thread_list.GetSize(false)) {
268b9c1b51eSKate Stone     ThreadSP core_thread_sp(
269b9c1b51eSKate Stone         core_thread_list.GetThreadAtIndex(core_number, false));
270b9c1b51eSKate Stone     if (core_thread_sp) {
271b9c1b51eSKate Stone       // Keep track of which cores were set as the backing thread for memory
272b9c1b51eSKate Stone       // threads...
273e98008ccSGreg Clayton       if (core_number < core_used_map.size())
274e98008ccSGreg Clayton         core_used_map[core_number] = true;
275e98008ccSGreg Clayton 
276160c9d81SGreg Clayton       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
277b9c1b51eSKate Stone       if (backing_core_thread_sp) {
278160c9d81SGreg Clayton         thread_sp->SetBackingThread(backing_core_thread_sp);
279b9c1b51eSKate Stone       } else {
280160c9d81SGreg Clayton         thread_sp->SetBackingThread(core_thread_sp);
281160c9d81SGreg Clayton       }
282160c9d81SGreg Clayton     }
283a4d8747dSGreg Clayton   }
284a4d8747dSGreg Clayton   return thread_sp;
285a4d8747dSGreg Clayton }
286a4d8747dSGreg Clayton 
287b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
288b3e77600SGreg Clayton 
289b3e77600SGreg Clayton RegisterContextSP
290b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
291b9c1b51eSKate Stone                                                       addr_t reg_data_addr) {
292435ce139SGreg Clayton   RegisterContextSP reg_ctx_sp;
293a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp || !thread)
294160c9d81SGreg Clayton     return reg_ctx_sp;
295160c9d81SGreg Clayton 
296160c9d81SGreg Clayton   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
297160c9d81SGreg Clayton     return reg_ctx_sp;
2981c22be69SGreg Clayton 
29985a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
30085a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
30185a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
30285a5ec51SJonas Devlieghere   // need the interpreter lock to make sure thread_info_dict stays alive.
30385a5ec51SJonas Devlieghere   //
30485a5ec51SJonas Devlieghere   // If someone already has the API lock, that is ok, we just want to avoid
30585a5ec51SJonas Devlieghere   // external code from making new API calls while this call is happening.
30685a5ec51SJonas Devlieghere   //
30785a5ec51SJonas Devlieghere   // This is a recursive lock so we can grant it to any Python code called on
30885a5ec51SJonas Devlieghere   // the stack below us.
30985e276b8SJim Ingham   Target &target = m_process->GetTarget();
31085a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
31185a5ec51SJonas Devlieghere                                                   std::defer_lock);
3124d23764dSReid Kleckner   (void)api_lock.try_lock(); // See above.
31385a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
31485e276b8SJim Ingham 
3155160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3161c22be69SGreg Clayton 
317b9c1b51eSKate Stone   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
318ead45e01SGreg Clayton     // The registers data is in contiguous memory, just create the register
319ead45e01SGreg Clayton     // context using the address provided
32063e5fb76SJonas Devlieghere     LLDB_LOGF(log,
32163e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
322b9c1b51eSKate Stone               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
323b9c1b51eSKate Stone               ") creating memory register context",
324b9c1b51eSKate Stone               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
325796ac80bSJonas Devlieghere     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
326796ac80bSJonas Devlieghere         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
327b9c1b51eSKate Stone   } else {
32805097246SAdrian Prantl     // No register data address is provided, query the python plug-in to let it
32905097246SAdrian Prantl     // make up the data as it sees fit
33063e5fb76SJonas Devlieghere     LLDB_LOGF(log,
33163e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
332b9c1b51eSKate Stone               "= 0x%" PRIx64 ", 0x%" PRIx64
333b9c1b51eSKate Stone               ") fetching register data from python",
334b9c1b51eSKate Stone               thread->GetID(), thread->GetProtocolID());
3351c22be69SGreg Clayton 
336b9c1b51eSKate Stone     StructuredData::StringSP reg_context_data =
337b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
338b9c1b51eSKate Stone                                                     thread->GetID());
339b9c1b51eSKate Stone     if (reg_context_data) {
340*adcd0268SBenjamin Kramer       std::string value = std::string(reg_context_data->GetValue());
3410641ca1aSZachary Turner       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
342b9c1b51eSKate Stone       if (data_sp->GetByteSize()) {
343b9c1b51eSKate Stone         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
344b9c1b51eSKate Stone             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
345b9c1b51eSKate Stone         if (reg_ctx_memory) {
346435ce139SGreg Clayton           reg_ctx_sp.reset(reg_ctx_memory);
347435ce139SGreg Clayton           reg_ctx_memory->SetAllRegisterData(data_sp);
348435ce139SGreg Clayton         }
349435ce139SGreg Clayton       }
350435ce139SGreg Clayton     }
351ead45e01SGreg Clayton   }
352b9c1b51eSKate Stone   // if we still have no register data, fallback on a dummy context to avoid
353b9c1b51eSKate Stone   // crashing
354b9c1b51eSKate Stone   if (!reg_ctx_sp) {
35563e5fb76SJonas Devlieghere     LLDB_LOGF(log,
35663e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
357b9c1b51eSKate Stone               "= 0x%" PRIx64 ") forcing a dummy register context",
358b9c1b51eSKate Stone               thread->GetID());
359796ac80bSJonas Devlieghere     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
360796ac80bSJonas Devlieghere         *thread, 0, target.GetArchitecture().GetAddressByteSize());
361cbd79b6cSEnrico Granata   }
362b3e77600SGreg Clayton   return reg_ctx_sp;
363b3e77600SGreg Clayton }
364b3e77600SGreg Clayton 
365b3e77600SGreg Clayton StopInfoSP
366b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
367b3e77600SGreg Clayton   // We should have gotten the thread stop info from the dictionary of data for
368b3e77600SGreg Clayton   // the thread in the initial call to get_thread_info(), this should have been
369b3e77600SGreg Clayton   // cached so we can return it here
370b9c1b51eSKate Stone   StopInfoSP
371b9c1b51eSKate Stone       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
372b3e77600SGreg Clayton   return stop_info_sp;
373b3e77600SGreg Clayton }
374b3e77600SGreg Clayton 
375b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
376b9c1b51eSKate Stone                                                    addr_t context) {
3775160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
378a4d8747dSGreg Clayton 
37963e5fb76SJonas Devlieghere   LLDB_LOGF(log,
38063e5fb76SJonas Devlieghere             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
381b9c1b51eSKate Stone             ", context = 0x%" PRIx64 ") fetching register data from python",
382b9c1b51eSKate Stone             tid, context);
383a4d8747dSGreg Clayton 
384b9c1b51eSKate Stone   if (m_interpreter && m_python_object_sp) {
38585a5ec51SJonas Devlieghere     // First thing we have to do is to try to get the API lock, and the
38685a5ec51SJonas Devlieghere     // interpreter lock. We're going to change the thread content of the
38785a5ec51SJonas Devlieghere     // process, and we're going to use python, which requires the API lock to
38885a5ec51SJonas Devlieghere     // do it. We need the interpreter lock to make sure thread_info_dict stays
38985a5ec51SJonas Devlieghere     // alive.
39085a5ec51SJonas Devlieghere     //
39185a5ec51SJonas Devlieghere     // If someone already has the API lock, that is ok, we just want to avoid
39285a5ec51SJonas Devlieghere     // external code from making new API calls while this call is happening.
39385a5ec51SJonas Devlieghere     //
39485a5ec51SJonas Devlieghere     // This is a recursive lock so we can grant it to any Python code called on
39585a5ec51SJonas Devlieghere     // the stack below us.
396a4d8747dSGreg Clayton     Target &target = m_process->GetTarget();
39785a5ec51SJonas Devlieghere     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
39885a5ec51SJonas Devlieghere                                                     std::defer_lock);
3994d23764dSReid Kleckner     (void)api_lock.try_lock(); // See above.
40085a5ec51SJonas Devlieghere     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
401a4d8747dSGreg Clayton 
402b9c1b51eSKate Stone     StructuredData::DictionarySP thread_info_dict =
403b9c1b51eSKate Stone         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
404e98008ccSGreg Clayton     std::vector<bool> core_used_map;
405b9c1b51eSKate Stone     if (thread_info_dict) {
406b3ae8761SGreg Clayton       ThreadList core_threads(m_process);
407a4d8747dSGreg Clayton       ThreadList &thread_list = m_process->GetThreadList();
408a4d8747dSGreg Clayton       bool did_create = false;
409b9c1b51eSKate Stone       ThreadSP thread_sp(
410b9c1b51eSKate Stone           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
411b9c1b51eSKate Stone                                      thread_list, core_used_map, &did_create));
412a4d8747dSGreg Clayton       if (did_create)
413a4d8747dSGreg Clayton         thread_list.AddThread(thread_sp);
414a4d8747dSGreg Clayton       return thread_sp;
415a4d8747dSGreg Clayton     }
416a4d8747dSGreg Clayton   }
417a4d8747dSGreg Clayton   return ThreadSP();
418a4d8747dSGreg Clayton }
419a4d8747dSGreg Clayton 
4204e26cf2cSJonas Devlieghere #endif // #if LLDB_ENABLE_PYTHON
421