1c156427dSZachary Turner //===-- OperatingSystemPython.cpp --------------------------------*- C++-*-===//
2b3e77600SGreg Clayton //
3b3e77600SGreg Clayton //                     The LLVM Compiler Infrastructure
4b3e77600SGreg Clayton //
5b3e77600SGreg Clayton // This file is distributed under the University of Illinois Open Source
6b3e77600SGreg Clayton // License. See LICENSE.TXT for details.
7b3e77600SGreg Clayton //
8b3e77600SGreg Clayton //===----------------------------------------------------------------------===//
993a64300SDaniel Malea 
10b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON
11b3e77600SGreg Clayton 
12b3e77600SGreg Clayton #include "OperatingSystemPython.h"
13b3e77600SGreg Clayton // C Includes
14b3e77600SGreg Clayton // C++ Includes
15b3e77600SGreg Clayton // Other libraries and framework includes
16b9c1b51eSKate Stone #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
17b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h"
18b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h"
19b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h"
205790759aSEnrico Granata #include "lldb/Core/Debugger.h"
21b3e77600SGreg Clayton #include "lldb/Core/Module.h"
22b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h"
23b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h"
24b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h"
255790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h"
260641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h"
27b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h"
28b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h"
29b3e77600SGreg Clayton #include "lldb/Target/Process.h"
30b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h"
31b3e77600SGreg Clayton #include "lldb/Target/Target.h"
32b3e77600SGreg Clayton #include "lldb/Target/Thread.h"
33b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h"
34666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h"
35bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h"
36f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h"
37b3e77600SGreg Clayton 
38b3e77600SGreg Clayton using namespace lldb;
39b3e77600SGreg Clayton using namespace lldb_private;
40b3e77600SGreg Clayton 
41b9c1b51eSKate Stone void OperatingSystemPython::Initialize() {
42b9c1b51eSKate Stone   PluginManager::RegisterPlugin(GetPluginNameStatic(),
43b9c1b51eSKate Stone                                 GetPluginDescriptionStatic(), CreateInstance,
44b9c1b51eSKate Stone                                 nullptr);
45b3e77600SGreg Clayton }
46b3e77600SGreg Clayton 
47b9c1b51eSKate Stone void OperatingSystemPython::Terminate() {
48b3e77600SGreg Clayton   PluginManager::UnregisterPlugin(CreateInstance);
49b3e77600SGreg Clayton }
50b3e77600SGreg Clayton 
51b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
52b9c1b51eSKate Stone                                                        bool force) {
53b9c1b51eSKate Stone   // Python OperatingSystem plug-ins must be requested by name, so force must be
54b9c1b51eSKate Stone   // true
55c9d645d3SGreg Clayton   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
56b9c1b51eSKate Stone   if (python_os_plugin_spec && python_os_plugin_spec.Exists()) {
57b9c1b51eSKate Stone     std::unique_ptr<OperatingSystemPython> os_ap(
58b9c1b51eSKate Stone         new OperatingSystemPython(process, python_os_plugin_spec));
59c9d645d3SGreg Clayton     if (os_ap.get() && os_ap->IsValid())
60c9d645d3SGreg Clayton       return os_ap.release();
61c9d645d3SGreg Clayton   }
62b3e77600SGreg Clayton   return NULL;
63b3e77600SGreg Clayton }
64b3e77600SGreg Clayton 
65b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginNameStatic() {
6657abc5d6SGreg Clayton   static ConstString g_name("python");
6757abc5d6SGreg Clayton   return g_name;
68b3e77600SGreg Clayton }
69b3e77600SGreg Clayton 
70b9c1b51eSKate Stone const char *OperatingSystemPython::GetPluginDescriptionStatic() {
71b9c1b51eSKate Stone   return "Operating system plug-in that gathers OS information from a python "
72b9c1b51eSKate Stone          "class that implements the necessary OperatingSystem functionality.";
73b3e77600SGreg Clayton }
74b3e77600SGreg Clayton 
75b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
76b9c1b51eSKate Stone                                              const FileSpec &python_module_path)
77b9c1b51eSKate Stone     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_ap(),
78b9c1b51eSKate Stone       m_interpreter(NULL), m_python_object_sp() {
795790759aSEnrico Granata   if (!process)
805790759aSEnrico Granata     return;
81a4d8747dSGreg Clayton   TargetSP target_sp = process->CalculateTarget();
825790759aSEnrico Granata   if (!target_sp)
835790759aSEnrico Granata     return;
84b9c1b51eSKate Stone   m_interpreter =
85b9c1b51eSKate Stone       target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter();
86b9c1b51eSKate Stone   if (m_interpreter) {
872443cbd7SGreg Clayton 
88b9c1b51eSKate Stone     std::string os_plugin_class_name(
89b9c1b51eSKate Stone         python_module_path.GetFilename().AsCString(""));
90b9c1b51eSKate Stone     if (!os_plugin_class_name.empty()) {
91c9d645d3SGreg Clayton       const bool init_session = false;
92c9d645d3SGreg Clayton       const bool allow_reload = true;
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;
97b9c1b51eSKate Stone       if (m_interpreter->LoadScriptingModule(
98b9c1b51eSKate Stone               python_module_path_cstr, allow_reload, 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() {
119b9c1b51eSKate Stone   if (m_register_info_ap.get() == NULL) {
120a4d8747dSGreg Clayton     if (!m_interpreter || !m_python_object_sp)
1215790759aSEnrico Granata       return NULL;
12262f80036SGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1231c22be69SGreg Clayton 
1241c22be69SGreg Clayton     if (log)
125b9c1b51eSKate Stone       log->Printf("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)
1325790759aSEnrico Granata       return NULL;
133b3e77600SGreg Clayton 
134b9c1b51eSKate Stone     m_register_info_ap.reset(new DynamicRegisterInfo(
135b9c1b51eSKate Stone         *dictionary, m_process->GetTarget().GetArchitecture()));
1362443cbd7SGreg Clayton     assert(m_register_info_ap->GetNumRegisters() > 0);
1372443cbd7SGreg Clayton     assert(m_register_info_ap->GetNumRegisterSets() > 0);
138b3e77600SGreg Clayton   }
139b3e77600SGreg Clayton   return m_register_info_ap.get();
140b3e77600SGreg Clayton }
141b3e77600SGreg Clayton 
142b3e77600SGreg Clayton //------------------------------------------------------------------
143b3e77600SGreg Clayton // PluginInterface protocol
144b3e77600SGreg Clayton //------------------------------------------------------------------
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 
159*85a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
160*85a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
161*85a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
162*85a5ec51SJonas 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();
170*85a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
171b9c1b51eSKate Stone                                                   std::defer_lock);
172*85a5ec51SJonas Devlieghere   api_lock.try_lock();
173*85a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
17485e276b8SJim Ingham 
1751c22be69SGreg Clayton   if (log)
176b9c1b51eSKate Stone     log->Printf("OperatingSystemPython::UpdateThreadList() fetching thread "
177b9c1b51eSKate Stone                 "data from python for pid %" PRIu64,
178b9c1b51eSKate Stone                 m_process->GetID());
1791c22be69SGreg Clayton 
180b9c1b51eSKate Stone   // The threads that are in "new_thread_list" upon entry are the threads from
181*85a5ec51SJonas Devlieghere   // the lldb_private::Process subclass, no memory threads will be in this
182*85a5ec51SJonas 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
189e98008ccSGreg Clayton   // core_thread list. Any real threads/cores that weren't used should
190e98008ccSGreg Clayton   // later be 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);
196c156427dSZachary Turner       log->Printf("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()) {
204b9c1b51eSKate Stone         ThreadSP thread_sp(
205b9c1b51eSKate Stone             CreateThreadFromThreadInfo(*thread_dict, core_thread_list,
206b9c1b51eSKate Stone                                        old_thread_list, core_used_map, NULL));
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
215b9c1b51eSKate Stone   // beginning
216e98008ccSGreg Clayton   // of the list
217e98008ccSGreg Clayton   uint32_t insert_idx = 0;
218b9c1b51eSKate Stone   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
219b9c1b51eSKate Stone     if (core_used_map[core_idx] == false) {
220b9c1b51eSKate Stone       new_thread_list.InsertThread(
221b9c1b51eSKate Stone           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
222e98008ccSGreg Clayton       ++insert_idx;
223e98008ccSGreg Clayton     }
224e98008ccSGreg Clayton   }
2256e0ff1a3SGreg Clayton 
226b3e77600SGreg Clayton   return new_thread_list.GetSize(false) > 0;
227b3e77600SGreg Clayton }
228b3e77600SGreg Clayton 
229b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
230b9c1b51eSKate Stone     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
231b9c1b51eSKate Stone     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
232b9c1b51eSKate Stone     bool *did_create_ptr) {
233a4d8747dSGreg Clayton   ThreadSP thread_sp;
2340641ca1aSZachary Turner   tid_t tid = LLDB_INVALID_THREAD_ID;
2350641ca1aSZachary Turner   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2360641ca1aSZachary Turner     return ThreadSP();
237a4d8747dSGreg Clayton 
2380641ca1aSZachary Turner   uint32_t core_number;
2390641ca1aSZachary Turner   addr_t reg_data_addr;
2402833321fSZachary Turner   llvm::StringRef name;
2412833321fSZachary Turner   llvm::StringRef queue;
2420641ca1aSZachary Turner 
2430641ca1aSZachary Turner   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
244b9c1b51eSKate Stone   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
245b9c1b51eSKate Stone                                       LLDB_INVALID_ADDRESS);
2460641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("name", name);
2470641ca1aSZachary Turner   thread_dict.GetValueForKeyAsString("queue", queue);
248a4d8747dSGreg Clayton 
249160c9d81SGreg Clayton   // See if a thread already exists for "tid"
250b3ae8761SGreg Clayton   thread_sp = old_thread_list.FindThreadByID(tid, false);
251b9c1b51eSKate Stone   if (thread_sp) {
252b9c1b51eSKate Stone     // A thread already does exist for "tid", make sure it was an operating
253b9c1b51eSKate Stone     // system
254160c9d81SGreg Clayton     // plug-in generated thread.
255b9c1b51eSKate Stone     if (!IsOperatingSystemPluginThread(thread_sp)) {
256160c9d81SGreg Clayton       // We have thread ID overlap between the protocol threads and the
257160c9d81SGreg Clayton       // operating system threads, clear the thread so we create an
258160c9d81SGreg Clayton       // operating system thread for this.
259160c9d81SGreg Clayton       thread_sp.reset();
260160c9d81SGreg Clayton     }
261160c9d81SGreg Clayton   }
262160c9d81SGreg Clayton 
263b9c1b51eSKate Stone   if (!thread_sp) {
264a4d8747dSGreg Clayton     if (did_create_ptr)
265a4d8747dSGreg Clayton       *did_create_ptr = true;
2662833321fSZachary Turner     thread_sp.reset(
2672833321fSZachary Turner         new ThreadMemory(*m_process, tid, name, queue, reg_data_addr));
268b3ae8761SGreg Clayton   }
269b3ae8761SGreg Clayton 
270b9c1b51eSKate Stone   if (core_number < core_thread_list.GetSize(false)) {
271b9c1b51eSKate Stone     ThreadSP core_thread_sp(
272b9c1b51eSKate Stone         core_thread_list.GetThreadAtIndex(core_number, false));
273b9c1b51eSKate Stone     if (core_thread_sp) {
274b9c1b51eSKate Stone       // Keep track of which cores were set as the backing thread for memory
275b9c1b51eSKate Stone       // threads...
276e98008ccSGreg Clayton       if (core_number < core_used_map.size())
277e98008ccSGreg Clayton         core_used_map[core_number] = true;
278e98008ccSGreg Clayton 
279160c9d81SGreg Clayton       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
280b9c1b51eSKate Stone       if (backing_core_thread_sp) {
281160c9d81SGreg Clayton         thread_sp->SetBackingThread(backing_core_thread_sp);
282b9c1b51eSKate Stone       } else {
283160c9d81SGreg Clayton         thread_sp->SetBackingThread(core_thread_sp);
284160c9d81SGreg Clayton       }
285160c9d81SGreg Clayton     }
286a4d8747dSGreg Clayton   }
287a4d8747dSGreg Clayton   return thread_sp;
288a4d8747dSGreg Clayton }
289a4d8747dSGreg Clayton 
290b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
291b3e77600SGreg Clayton 
292b3e77600SGreg Clayton RegisterContextSP
293b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
294b9c1b51eSKate Stone                                                       addr_t reg_data_addr) {
295435ce139SGreg Clayton   RegisterContextSP reg_ctx_sp;
296a4d8747dSGreg Clayton   if (!m_interpreter || !m_python_object_sp || !thread)
297160c9d81SGreg Clayton     return reg_ctx_sp;
298160c9d81SGreg Clayton 
299160c9d81SGreg Clayton   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
300160c9d81SGreg Clayton     return reg_ctx_sp;
3011c22be69SGreg Clayton 
302*85a5ec51SJonas Devlieghere   // First thing we have to do is to try to get the API lock, and the
303*85a5ec51SJonas Devlieghere   // interpreter lock. We're going to change the thread content of the process,
304*85a5ec51SJonas Devlieghere   // and we're going to use python, which requires the API lock to do it. We
305*85a5ec51SJonas Devlieghere   // need the interpreter lock to make sure thread_info_dict stays alive.
306*85a5ec51SJonas Devlieghere   //
307*85a5ec51SJonas Devlieghere   // If someone already has the API lock, that is ok, we just want to avoid
308*85a5ec51SJonas Devlieghere   // external code from making new API calls while this call is happening.
309*85a5ec51SJonas Devlieghere   //
310*85a5ec51SJonas Devlieghere   // This is a recursive lock so we can grant it to any Python code called on
311*85a5ec51SJonas Devlieghere   // the stack below us.
31285e276b8SJim Ingham   Target &target = m_process->GetTarget();
313*85a5ec51SJonas Devlieghere   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
314*85a5ec51SJonas Devlieghere                                                   std::defer_lock);
315*85a5ec51SJonas Devlieghere   api_lock.try_lock();
316*85a5ec51SJonas Devlieghere   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
31785e276b8SJim Ingham 
3185160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3191c22be69SGreg Clayton 
320b9c1b51eSKate Stone   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
321ead45e01SGreg Clayton     // The registers data is in contiguous memory, just create the register
322ead45e01SGreg Clayton     // context using the address provided
323ead45e01SGreg Clayton     if (log)
324b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
325b9c1b51eSKate Stone                   "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
326b9c1b51eSKate Stone                   ") creating memory register context",
327b9c1b51eSKate Stone                   thread->GetID(), thread->GetProtocolID(), reg_data_addr);
328b9c1b51eSKate Stone     reg_ctx_sp.reset(new RegisterContextMemory(
329b9c1b51eSKate Stone         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr));
330b9c1b51eSKate Stone   } else {
331ead45e01SGreg Clayton     // No register data address is provided, query the python plug-in to let
332ead45e01SGreg Clayton     // it make up the data as it sees fit
3331c22be69SGreg Clayton     if (log)
334b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
335b9c1b51eSKate Stone                   "= 0x%" PRIx64 ", 0x%" PRIx64
336b9c1b51eSKate Stone                   ") fetching register data from python",
337b9c1b51eSKate Stone                   thread->GetID(), thread->GetProtocolID());
3381c22be69SGreg Clayton 
339b9c1b51eSKate Stone     StructuredData::StringSP reg_context_data =
340b9c1b51eSKate Stone         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
341b9c1b51eSKate Stone                                                     thread->GetID());
342b9c1b51eSKate Stone     if (reg_context_data) {
3430641ca1aSZachary Turner       std::string value = reg_context_data->GetValue();
3440641ca1aSZachary Turner       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
345b9c1b51eSKate Stone       if (data_sp->GetByteSize()) {
346b9c1b51eSKate Stone         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
347b9c1b51eSKate Stone             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
348b9c1b51eSKate Stone         if (reg_ctx_memory) {
349435ce139SGreg Clayton           reg_ctx_sp.reset(reg_ctx_memory);
350435ce139SGreg Clayton           reg_ctx_memory->SetAllRegisterData(data_sp);
351435ce139SGreg Clayton         }
352435ce139SGreg Clayton       }
353435ce139SGreg Clayton     }
354ead45e01SGreg Clayton   }
355b9c1b51eSKate Stone   // if we still have no register data, fallback on a dummy context to avoid
356b9c1b51eSKate Stone   // crashing
357b9c1b51eSKate Stone   if (!reg_ctx_sp) {
358cbd79b6cSEnrico Granata     if (log)
359b9c1b51eSKate Stone       log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid "
360b9c1b51eSKate Stone                   "= 0x%" PRIx64 ") forcing a dummy register context",
361b9c1b51eSKate Stone                   thread->GetID());
362b9c1b51eSKate Stone     reg_ctx_sp.reset(new RegisterContextDummy(
363b9c1b51eSKate Stone         *thread, 0, target.GetArchitecture().GetAddressByteSize()));
364cbd79b6cSEnrico Granata   }
365b3e77600SGreg Clayton   return reg_ctx_sp;
366b3e77600SGreg Clayton }
367b3e77600SGreg Clayton 
368b3e77600SGreg Clayton StopInfoSP
369b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
370b3e77600SGreg Clayton   // We should have gotten the thread stop info from the dictionary of data for
371b3e77600SGreg Clayton   // the thread in the initial call to get_thread_info(), this should have been
372b3e77600SGreg Clayton   // cached so we can return it here
373b9c1b51eSKate Stone   StopInfoSP
374b9c1b51eSKate Stone       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
375b3e77600SGreg Clayton   return stop_info_sp;
376b3e77600SGreg Clayton }
377b3e77600SGreg Clayton 
378b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
379b9c1b51eSKate Stone                                                    addr_t context) {
3805160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
381a4d8747dSGreg Clayton 
382a4d8747dSGreg Clayton   if (log)
383b9c1b51eSKate Stone     log->Printf("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
384b9c1b51eSKate Stone                 ", context = 0x%" PRIx64 ") fetching register data from python",
385b9c1b51eSKate Stone                 tid, context);
386a4d8747dSGreg Clayton 
387b9c1b51eSKate Stone   if (m_interpreter && m_python_object_sp) {
388*85a5ec51SJonas Devlieghere     // First thing we have to do is to try to get the API lock, and the
389*85a5ec51SJonas Devlieghere     // interpreter lock. We're going to change the thread content of the
390*85a5ec51SJonas Devlieghere     // process, and we're going to use python, which requires the API lock to
391*85a5ec51SJonas Devlieghere     // do it. We need the interpreter lock to make sure thread_info_dict stays
392*85a5ec51SJonas Devlieghere     // alive.
393*85a5ec51SJonas Devlieghere     //
394*85a5ec51SJonas Devlieghere     // If someone already has the API lock, that is ok, we just want to avoid
395*85a5ec51SJonas Devlieghere     // external code from making new API calls while this call is happening.
396*85a5ec51SJonas Devlieghere     //
397*85a5ec51SJonas Devlieghere     // This is a recursive lock so we can grant it to any Python code called on
398*85a5ec51SJonas Devlieghere     // the stack below us.
399a4d8747dSGreg Clayton     Target &target = m_process->GetTarget();
400*85a5ec51SJonas Devlieghere     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
401*85a5ec51SJonas Devlieghere                                                     std::defer_lock);
402*85a5ec51SJonas Devlieghere     api_lock.try_lock();
403*85a5ec51SJonas Devlieghere     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
404a4d8747dSGreg Clayton 
405b9c1b51eSKate Stone     StructuredData::DictionarySP thread_info_dict =
406b9c1b51eSKate Stone         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
407e98008ccSGreg Clayton     std::vector<bool> core_used_map;
408b9c1b51eSKate Stone     if (thread_info_dict) {
409b3ae8761SGreg Clayton       ThreadList core_threads(m_process);
410a4d8747dSGreg Clayton       ThreadList &thread_list = m_process->GetThreadList();
411a4d8747dSGreg Clayton       bool did_create = false;
412b9c1b51eSKate Stone       ThreadSP thread_sp(
413b9c1b51eSKate Stone           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
414b9c1b51eSKate Stone                                      thread_list, core_used_map, &did_create));
415a4d8747dSGreg Clayton       if (did_create)
416a4d8747dSGreg Clayton         thread_list.AddThread(thread_sp);
417a4d8747dSGreg Clayton       return thread_sp;
418a4d8747dSGreg Clayton     }
419a4d8747dSGreg Clayton   }
420a4d8747dSGreg Clayton   return ThreadSP();
421a4d8747dSGreg Clayton }
422a4d8747dSGreg Clayton 
423b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON
424