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 
42bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(OperatingSystemPython)
43fbb4d1e4SJonas Devlieghere 
44b9c1b51eSKate Stone void OperatingSystemPython::Initialize() {
45b9c1b51eSKate Stone   PluginManager::RegisterPlugin(GetPluginNameStatic(),
46b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
47b9c1b51eSKate Stone                                 nullptr);
48b3e77600SGreg Clayton }
49b3e77600SGreg Clayton 
50b9c1b51eSKate Stone void OperatingSystemPython::Terminate() {
51b3e77600SGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
52b3e77600SGreg Clayton }
53b3e77600SGreg Clayton 
54b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
55b9c1b51eSKate Stone                                                        bool force) {
5605097246SAdrian Prantl   // Python OperatingSystem plug-ins must be requested by name, so force must
5705097246SAdrian Prantl   // be true
58c9d645d3SGreg Clayton   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
59dbd7fabaSJonas Devlieghere   if (python_os_plugin_spec &&
60dbd7fabaSJonas Devlieghere       FileSystem::Instance().Exists(python_os_plugin_spec)) {
61d5b44036SJonas Devlieghere     std::unique_ptr<OperatingSystemPython> os_up(
62b9c1b51eSKate Stone         new OperatingSystemPython(process, python_os_plugin_spec));
63d5b44036SJonas Devlieghere     if (os_up.get() && os_up->IsValid())
64d5b44036SJonas Devlieghere       return os_up.release();
65c9d645d3SGreg Clayton   }
66248a1305SKonrad Kleine   return nullptr;
67b3e77600SGreg Clayton }
68b3e77600SGreg Clayton 
69b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginNameStatic() {
7057abc5d6SGreg Clayton   static ConstString g_name("python");
7157abc5d6SGreg Clayton   return g_name;
72b3e77600SGreg Clayton }
73b3e77600SGreg Clayton 
74b9c1b51eSKate Stone const char *OperatingSystemPython::GetPluginDescriptionStatic() {
75b9c1b51eSKate Stone   return "Operating system plug-in that gathers OS information from a python "
76b9c1b51eSKate Stone          "class that implements the necessary OperatingSystem functionality.";
77b3e77600SGreg Clayton }
78b3e77600SGreg Clayton 
79b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
80b9c1b51eSKate Stone                                              const FileSpec &python_module_path)
81d5b44036SJonas Devlieghere     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
82248a1305SKonrad Kleine       m_interpreter(nullptr), m_python_object_sp() {
835790759aSEnrico Granata   if (!process)
845790759aSEnrico Granata     return;
85a4d8747dSGreg Clayton   TargetSP target_sp = process->CalculateTarget();
865790759aSEnrico Granata   if (!target_sp)
875790759aSEnrico Granata     return;
882b29b432SJonas Devlieghere   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
89b9c1b51eSKate Stone   if (m_interpreter) {
902443cbd7SGreg Clayton 
91b9c1b51eSKate Stone     std::string os_plugin_class_name(
92b9c1b51eSKate Stone         python_module_path.GetFilename().AsCString(""));
93b9c1b51eSKate Stone     if (!os_plugin_class_name.empty()) {
94c9d645d3SGreg Clayton       const bool init_session = false;
95c9d645d3SGreg Clayton       char python_module_path_cstr[PATH_MAX];
96b9c1b51eSKate Stone       python_module_path.GetPath(python_module_path_cstr,
97b9c1b51eSKate Stone                                  sizeof(python_module_path_cstr));
9897206d57SZachary Turner       Status error;
9915625112SJonas Devlieghere       if (m_interpreter->LoadScriptingModule(python_module_path_cstr,
10015625112SJonas Devlieghere                                              init_session, error)) {
101c9d645d3SGreg Clayton         // Strip the ".py" extension if there is one
102c9d645d3SGreg Clayton         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
103c9d645d3SGreg Clayton         if (py_extension_pos != std::string::npos)
104c9d645d3SGreg Clayton           os_plugin_class_name.erase(py_extension_pos);
105b9c1b51eSKate Stone         // Add ".OperatingSystemPlugIn" to the module name to get a string like
106b9c1b51eSKate Stone         // "modulename.OperatingSystemPlugIn"
107c9d645d3SGreg Clayton         os_plugin_class_name += ".OperatingSystemPlugIn";
1080641ca1aSZachary Turner         StructuredData::ObjectSP object_sp =
109b9c1b51eSKate Stone             m_interpreter->OSPlugin_CreatePluginObject(
110b9c1b51eSKate Stone                 os_plugin_class_name.c_str(), process->CalculateProcess());
1110641ca1aSZachary Turner         if (object_sp && object_sp->IsValid())
112a4d8747dSGreg Clayton           m_python_object_sp = object_sp;
113c9d645d3SGreg Clayton       }
1142443cbd7SGreg Clayton     }
1155790759aSEnrico Granata   }
116b3e77600SGreg Clayton }
117b3e77600SGreg Clayton 
118*fd2433e1SJonas Devlieghere OperatingSystemPython::~OperatingSystemPython() = default;
119b3e77600SGreg Clayton 
120b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
121248a1305SKonrad Kleine   if (m_register_info_up == nullptr) {
122a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
123248a1305SKonrad Kleine       return nullptr;
12462f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1251c22be69SGreg Clayton 
12663e5fb76SJonas Devlieghere     LLDB_LOGF(log,
12763e5fb76SJonas Devlieghere               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
128b9c1b51eSKate Stone               "thread register definitions from python for pid %" PRIu64,
129b9c1b51eSKate Stone               m_process->GetID());
1301c22be69SGreg Clayton 
131b9c1b51eSKate Stone     StructuredData::DictionarySP dictionary =
132b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1335790759aSEnrico Granata     if (!dictionary)
134248a1305SKonrad Kleine       return nullptr;
135b3e77600SGreg Clayton 
136d2ec9184SJonas Devlieghere     m_register_info_up = std::make_unique<DynamicRegisterInfo>(
137d2ec9184SJonas Devlieghere         *dictionary, m_process->GetTarget().GetArchitecture());
138d5b44036SJonas Devlieghere     assert(m_register_info_up->GetNumRegisters() > 0);
139d5b44036SJonas Devlieghere     assert(m_register_info_up->GetNumRegisterSets() > 0);
140b3e77600SGreg Clayton   }
141d5b44036SJonas Devlieghere   return m_register_info_up.get();
142b3e77600SGreg Clayton }
143b3e77600SGreg Clayton 
144b3e77600SGreg Clayton // PluginInterface protocol
145b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginName() {
146b3e77600SGreg Clayton   return GetPluginNameStatic();
147b3e77600SGreg Clayton }
148b3e77600SGreg Clayton 
149b9c1b51eSKate Stone uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
150b3e77600SGreg Clayton 
151b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
152ba4e61d3SAndrew Kaylor                                              ThreadList &core_thread_list,
153b9c1b51eSKate Stone                                              ThreadList &new_thread_list) {
154a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp)
155a85e6b6cSDaniel Malea     return false;
1561c22be69SGreg Clayton 
15762f80036SGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1581c22be69SGreg Clayton 
15985a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
16085a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
16185a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
16285a5ec51SJonas Devlieghere   // need the interpreter lock to make sure thread_info_dict stays alive.
163ab745c2aSGreg Clayton   //
164ab745c2aSGreg Clayton   // If someone already has the API lock, that is ok, we just want to avoid
165ab745c2aSGreg Clayton   // external code from making new API calls while this call is happening.
166ab745c2aSGreg Clayton   //
167ab745c2aSGreg Clayton   // This is a recursive lock so we can grant it to any Python code called on
168ab745c2aSGreg Clayton   // the stack below us.
16985e276b8SJim Ingham   Target &target = m_process->GetTarget();
17085a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
171b9c1b51eSKate Stone                                                   std::defer_lock);
1724d23764dSReid Kleckner   (void)api_lock.try_lock(); // See above.
17385a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
17485e276b8SJim Ingham 
17563e5fb76SJonas Devlieghere   LLDB_LOGF(log,
17663e5fb76SJonas Devlieghere             "OperatingSystemPython::UpdateThreadList() fetching thread "
177b9c1b51eSKate Stone             "data from python for pid %" PRIu64,
178b9c1b51eSKate Stone             m_process->GetID());
1791c22be69SGreg Clayton 
1808240b0d7SJim Ingham   // The threads that are in "core_thread_list" upon entry are the threads from
18185a5ec51SJonas Devlieghere   // the lldb_private::Process subclass, no memory threads will be in this
18285a5ec51SJonas Devlieghere   // list.
183b9c1b51eSKate Stone   StructuredData::ArraySP threads_list =
184b9c1b51eSKate Stone       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
185e98008ccSGreg Clayton 
186e98008ccSGreg Clayton   const uint32_t num_cores = core_thread_list.GetSize(false);
187e98008ccSGreg Clayton 
188e98008ccSGreg Clayton   // Make a map so we can keep track of which cores were used from the
18905097246SAdrian Prantl   // core_thread list. Any real threads/cores that weren't used should later be
19005097246SAdrian Prantl   // put back into the "new_thread_list".
191e98008ccSGreg Clayton   std::vector<bool> core_used_map(num_cores, false);
192b9c1b51eSKate Stone   if (threads_list) {
193b9c1b51eSKate Stone     if (log) {
19462f80036SGreg Clayton       StreamString strm;
1950641ca1aSZachary Turner       threads_list->Dump(strm);
19663e5fb76SJonas Devlieghere       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
19762f80036SGreg Clayton     }
1980641ca1aSZachary Turner 
1990641ca1aSZachary Turner     const uint32_t num_threads = threads_list->GetSize();
200b9c1b51eSKate Stone     for (uint32_t i = 0; i < num_threads; ++i) {
201b9c1b51eSKate Stone       StructuredData::ObjectSP thread_dict_obj =
202b9c1b51eSKate Stone           threads_list->GetItemAtIndex(i);
203b9c1b51eSKate Stone       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
204248a1305SKonrad Kleine         ThreadSP thread_sp(CreateThreadFromThreadInfo(
205248a1305SKonrad Kleine             *thread_dict, core_thread_list, old_thread_list, core_used_map,
206248a1305SKonrad Kleine             nullptr));
207a4d8747dSGreg Clayton         if (thread_sp)
208435ce139SGreg Clayton           new_thread_list.AddThread(thread_sp);
209435ce139SGreg Clayton       }
210435ce139SGreg Clayton     }
211435ce139SGreg Clayton   }
2126e0ff1a3SGreg Clayton 
213e98008ccSGreg Clayton   // Any real core threads that didn't end up backing a memory thread should
214b9c1b51eSKate Stone   // still be in the main thread list, and they should be inserted at the
21505097246SAdrian Prantl   // beginning of the list
216e98008ccSGreg Clayton   uint32_t insert_idx = 0;
217b9c1b51eSKate Stone   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
218a6682a41SJonas Devlieghere     if (!core_used_map[core_idx]) {
219b9c1b51eSKate Stone       new_thread_list.InsertThread(
220b9c1b51eSKate Stone           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
221e98008ccSGreg Clayton       ++insert_idx;
222e98008ccSGreg Clayton     }
223e98008ccSGreg Clayton   }
2246e0ff1a3SGreg Clayton 
225b3e77600SGreg Clayton   return new_thread_list.GetSize(false) > 0;
226b3e77600SGreg Clayton }
227b3e77600SGreg Clayton 
228b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
229b9c1b51eSKate Stone     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
230b9c1b51eSKate Stone     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
231b9c1b51eSKate Stone     bool *did_create_ptr) {
232a4d8747dSGreg Clayton   ThreadSP thread_sp;
2330641ca1aSZachary Turner   tid_t tid = LLDB_INVALID_THREAD_ID;
2340641ca1aSZachary Turner   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2350641ca1aSZachary Turner     return ThreadSP();
236a4d8747dSGreg Clayton 
2370641ca1aSZachary Turner   uint32_t core_number;
2380641ca1aSZachary Turner   addr_t reg_data_addr;
2392833321fSZachary Turner   llvm::StringRef name;
2402833321fSZachary Turner   llvm::StringRef queue;
2410641ca1aSZachary Turner 
2420641ca1aSZachary Turner   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
243b9c1b51eSKate Stone   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
244b9c1b51eSKate Stone                                       LLDB_INVALID_ADDRESS);
2450641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("name", name);
2460641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("queue", queue);
247a4d8747dSGreg Clayton 
248160c9d81SGreg Clayton   // See if a thread already exists for "tid"
249b3ae8761SGreg Clayton   thread_sp = old_thread_list.FindThreadByID(tid, false);
250b9c1b51eSKate Stone   if (thread_sp) {
251b9c1b51eSKate Stone     // A thread already does exist for "tid", make sure it was an operating
252b9c1b51eSKate Stone     // system
253160c9d81SGreg Clayton     // plug-in generated thread.
254b9c1b51eSKate Stone     if (!IsOperatingSystemPluginThread(thread_sp)) {
255160c9d81SGreg Clayton       // We have thread ID overlap between the protocol threads and the
25605097246SAdrian Prantl       // operating system threads, clear the thread so we create an operating
25705097246SAdrian Prantl       // system thread for this.
258160c9d81SGreg Clayton       thread_sp.reset();
259160c9d81SGreg Clayton     }
260160c9d81SGreg Clayton   }
261160c9d81SGreg Clayton 
262b9c1b51eSKate Stone   if (!thread_sp) {
263a4d8747dSGreg Clayton     if (did_create_ptr)
264a4d8747dSGreg Clayton       *did_create_ptr = true;
265796ac80bSJonas Devlieghere     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
266796ac80bSJonas Devlieghere                                                reg_data_addr);
267b3ae8761SGreg Clayton   }
268b3ae8761SGreg Clayton 
269b9c1b51eSKate Stone   if (core_number < core_thread_list.GetSize(false)) {
270b9c1b51eSKate Stone     ThreadSP core_thread_sp(
271b9c1b51eSKate Stone         core_thread_list.GetThreadAtIndex(core_number, false));
272b9c1b51eSKate Stone     if (core_thread_sp) {
273b9c1b51eSKate Stone       // Keep track of which cores were set as the backing thread for memory
274b9c1b51eSKate Stone       // threads...
275e98008ccSGreg Clayton       if (core_number < core_used_map.size())
276e98008ccSGreg Clayton         core_used_map[core_number] = true;
277e98008ccSGreg Clayton 
278160c9d81SGreg Clayton       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
279b9c1b51eSKate Stone       if (backing_core_thread_sp) {
280160c9d81SGreg Clayton         thread_sp->SetBackingThread(backing_core_thread_sp);
281b9c1b51eSKate Stone       } else {
282160c9d81SGreg Clayton         thread_sp->SetBackingThread(core_thread_sp);
283160c9d81SGreg Clayton       }
284160c9d81SGreg Clayton     }
285a4d8747dSGreg Clayton   }
286a4d8747dSGreg Clayton   return thread_sp;
287a4d8747dSGreg Clayton }
288a4d8747dSGreg Clayton 
289b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
290b3e77600SGreg Clayton 
291b3e77600SGreg Clayton RegisterContextSP
292b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
293b9c1b51eSKate Stone                                                       addr_t reg_data_addr) {
294435ce139SGreg Clayton   RegisterContextSP reg_ctx_sp;
295a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp || !thread)
296160c9d81SGreg Clayton     return reg_ctx_sp;
297160c9d81SGreg Clayton 
298160c9d81SGreg Clayton   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
299160c9d81SGreg Clayton     return reg_ctx_sp;
3001c22be69SGreg Clayton 
30185a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
30285a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
30385a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
30485a5ec51SJonas Devlieghere   // need the interpreter lock to make sure thread_info_dict stays alive.
30585a5ec51SJonas Devlieghere   //
30685a5ec51SJonas Devlieghere   // If someone already has the API lock, that is ok, we just want to avoid
30785a5ec51SJonas Devlieghere   // external code from making new API calls while this call is happening.
30885a5ec51SJonas Devlieghere   //
30985a5ec51SJonas Devlieghere   // This is a recursive lock so we can grant it to any Python code called on
31085a5ec51SJonas Devlieghere   // the stack below us.
31185e276b8SJim Ingham   Target &target = m_process->GetTarget();
31285a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
31385a5ec51SJonas Devlieghere                                                   std::defer_lock);
3144d23764dSReid Kleckner   (void)api_lock.try_lock(); // See above.
31585a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
31685e276b8SJim Ingham 
3175160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3181c22be69SGreg Clayton 
319b9c1b51eSKate Stone   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
320ead45e01SGreg Clayton     // The registers data is in contiguous memory, just create the register
321ead45e01SGreg Clayton     // context using the address provided
32263e5fb76SJonas Devlieghere     LLDB_LOGF(log,
32363e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
324b9c1b51eSKate Stone               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
325b9c1b51eSKate Stone               ") creating memory register context",
326b9c1b51eSKate Stone               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
327796ac80bSJonas Devlieghere     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
328796ac80bSJonas Devlieghere         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
329b9c1b51eSKate Stone   } else {
33005097246SAdrian Prantl     // No register data address is provided, query the python plug-in to let it
33105097246SAdrian Prantl     // make up the data as it sees fit
33263e5fb76SJonas Devlieghere     LLDB_LOGF(log,
33363e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
334b9c1b51eSKate Stone               "= 0x%" PRIx64 ", 0x%" PRIx64
335b9c1b51eSKate Stone               ") fetching register data from python",
336b9c1b51eSKate Stone               thread->GetID(), thread->GetProtocolID());
3371c22be69SGreg Clayton 
338b9c1b51eSKate Stone     StructuredData::StringSP reg_context_data =
339b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
340b9c1b51eSKate Stone                                                     thread->GetID());
341b9c1b51eSKate Stone     if (reg_context_data) {
342adcd0268SBenjamin Kramer       std::string value = std::string(reg_context_data->GetValue());
3430641ca1aSZachary Turner       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
344b9c1b51eSKate Stone       if (data_sp->GetByteSize()) {
345b9c1b51eSKate Stone         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
346b9c1b51eSKate Stone             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
347b9c1b51eSKate Stone         if (reg_ctx_memory) {
348435ce139SGreg Clayton           reg_ctx_sp.reset(reg_ctx_memory);
349435ce139SGreg Clayton           reg_ctx_memory->SetAllRegisterData(data_sp);
350435ce139SGreg Clayton         }
351435ce139SGreg Clayton       }
352435ce139SGreg Clayton     }
353ead45e01SGreg Clayton   }
354b9c1b51eSKate Stone   // if we still have no register data, fallback on a dummy context to avoid
355b9c1b51eSKate Stone   // crashing
356b9c1b51eSKate Stone   if (!reg_ctx_sp) {
35763e5fb76SJonas Devlieghere     LLDB_LOGF(log,
35863e5fb76SJonas Devlieghere               "OperatingSystemPython::CreateRegisterContextForThread (tid "
359b9c1b51eSKate Stone               "= 0x%" PRIx64 ") forcing a dummy register context",
360b9c1b51eSKate Stone               thread->GetID());
361796ac80bSJonas Devlieghere     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
362796ac80bSJonas Devlieghere         *thread, 0, target.GetArchitecture().GetAddressByteSize());
363cbd79b6cSEnrico Granata   }
364b3e77600SGreg Clayton   return reg_ctx_sp;
365b3e77600SGreg Clayton }
366b3e77600SGreg Clayton 
367b3e77600SGreg Clayton StopInfoSP
368b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
369b3e77600SGreg Clayton   // We should have gotten the thread stop info from the dictionary of data for
370b3e77600SGreg Clayton   // the thread in the initial call to get_thread_info(), this should have been
371b3e77600SGreg Clayton   // cached so we can return it here
372b9c1b51eSKate Stone   StopInfoSP
373b9c1b51eSKate Stone       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
374b3e77600SGreg Clayton   return stop_info_sp;
375b3e77600SGreg Clayton }
376b3e77600SGreg Clayton 
377b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
378b9c1b51eSKate Stone                                                    addr_t context) {
3795160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
380a4d8747dSGreg Clayton 
38163e5fb76SJonas Devlieghere   LLDB_LOGF(log,
38263e5fb76SJonas Devlieghere             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
383b9c1b51eSKate Stone             ", context = 0x%" PRIx64 ") fetching register data from python",
384b9c1b51eSKate Stone             tid, context);
385a4d8747dSGreg Clayton 
386b9c1b51eSKate Stone   if (m_interpreter && m_python_object_sp) {
38785a5ec51SJonas Devlieghere     // First thing we have to do is to try to get the API lock, and the
38885a5ec51SJonas Devlieghere     // interpreter lock. We're going to change the thread content of the
38985a5ec51SJonas Devlieghere     // process, and we're going to use python, which requires the API lock to
39085a5ec51SJonas Devlieghere     // do it. We need the interpreter lock to make sure thread_info_dict stays
39185a5ec51SJonas Devlieghere     // alive.
39285a5ec51SJonas Devlieghere     //
39385a5ec51SJonas Devlieghere     // If someone already has the API lock, that is ok, we just want to avoid
39485a5ec51SJonas Devlieghere     // external code from making new API calls while this call is happening.
39585a5ec51SJonas Devlieghere     //
39685a5ec51SJonas Devlieghere     // This is a recursive lock so we can grant it to any Python code called on
39785a5ec51SJonas Devlieghere     // the stack below us.
398a4d8747dSGreg Clayton     Target &target = m_process->GetTarget();
39985a5ec51SJonas Devlieghere     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
40085a5ec51SJonas Devlieghere                                                     std::defer_lock);
4014d23764dSReid Kleckner     (void)api_lock.try_lock(); // See above.
40285a5ec51SJonas Devlieghere     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
403a4d8747dSGreg Clayton 
404b9c1b51eSKate Stone     StructuredData::DictionarySP thread_info_dict =
405b9c1b51eSKate Stone         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
406e98008ccSGreg Clayton     std::vector<bool> core_used_map;
407b9c1b51eSKate Stone     if (thread_info_dict) {
408b3ae8761SGreg Clayton       ThreadList core_threads(m_process);
409a4d8747dSGreg Clayton       ThreadList &thread_list = m_process->GetThreadList();
410a4d8747dSGreg Clayton       bool did_create = false;
411b9c1b51eSKate Stone       ThreadSP thread_sp(
412b9c1b51eSKate Stone           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
413b9c1b51eSKate Stone                                      thread_list, core_used_map, &did_create));
414a4d8747dSGreg Clayton       if (did_create)
415a4d8747dSGreg Clayton         thread_list.AddThread(thread_sp);
416a4d8747dSGreg Clayton       return thread_sp;
417a4d8747dSGreg Clayton     }
418a4d8747dSGreg Clayton   }
419a4d8747dSGreg Clayton   return ThreadSP();
420a4d8747dSGreg Clayton }
421a4d8747dSGreg Clayton 
4224e26cf2cSJonas Devlieghere #endif // #if LLDB_ENABLE_PYTHON
423