xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
15ffd83dbSDimitry Andric //===-- OperatingSystemPython.cpp -----------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9480093f4SDimitry Andric #include "lldb/Host/Config.h"
10480093f4SDimitry Andric 
11480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "OperatingSystemPython.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "Plugins/Process/Utility/DynamicRegisterInfo.h"
160b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextDummy.h"
170b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextMemory.h"
180b57cec5SDimitry Andric #include "Plugins/Process/Utility/ThreadMemory.h"
190b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
200b57cec5SDimitry Andric #include "lldb/Core/Module.h"
210b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
220b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
230b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
240b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
260b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
270b57cec5SDimitry Andric #include "lldb/Target/Process.h"
280b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
290b57cec5SDimitry Andric #include "lldb/Target/Target.h"
300b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
310b57cec5SDimitry Andric #include "lldb/Target/ThreadList.h"
320b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
330b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
340b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
350b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #include <memory>
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric using namespace lldb;
400b57cec5SDimitry Andric using namespace lldb_private;
410b57cec5SDimitry Andric 
425ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(OperatingSystemPython)
435ffd83dbSDimitry Andric 
440b57cec5SDimitry Andric void OperatingSystemPython::Initialize() {
450b57cec5SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
460b57cec5SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance,
470b57cec5SDimitry Andric                                 nullptr);
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric void OperatingSystemPython::Terminate() {
510b57cec5SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
550b57cec5SDimitry Andric                                                        bool force) {
560b57cec5SDimitry Andric   // Python OperatingSystem plug-ins must be requested by name, so force must
570b57cec5SDimitry Andric   // be true
580b57cec5SDimitry Andric   FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
590b57cec5SDimitry Andric   if (python_os_plugin_spec &&
600b57cec5SDimitry Andric       FileSystem::Instance().Exists(python_os_plugin_spec)) {
610b57cec5SDimitry Andric     std::unique_ptr<OperatingSystemPython> os_up(
620b57cec5SDimitry Andric         new OperatingSystemPython(process, python_os_plugin_spec));
630b57cec5SDimitry Andric     if (os_up.get() && os_up->IsValid())
640b57cec5SDimitry Andric       return os_up.release();
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric   return nullptr;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric ConstString OperatingSystemPython::GetPluginNameStatic() {
700b57cec5SDimitry Andric   static ConstString g_name("python");
710b57cec5SDimitry Andric   return g_name;
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric const char *OperatingSystemPython::GetPluginDescriptionStatic() {
750b57cec5SDimitry Andric   return "Operating system plug-in that gathers OS information from a python "
760b57cec5SDimitry Andric          "class that implements the necessary OperatingSystem functionality.";
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
800b57cec5SDimitry Andric                                              const FileSpec &python_module_path)
810b57cec5SDimitry Andric     : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
820b57cec5SDimitry Andric       m_interpreter(nullptr), m_python_object_sp() {
830b57cec5SDimitry Andric   if (!process)
840b57cec5SDimitry Andric     return;
850b57cec5SDimitry Andric   TargetSP target_sp = process->CalculateTarget();
860b57cec5SDimitry Andric   if (!target_sp)
870b57cec5SDimitry Andric     return;
880b57cec5SDimitry Andric   m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
890b57cec5SDimitry Andric   if (m_interpreter) {
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric     std::string os_plugin_class_name(
920b57cec5SDimitry Andric         python_module_path.GetFilename().AsCString(""));
930b57cec5SDimitry Andric     if (!os_plugin_class_name.empty()) {
94*fe6060f1SDimitry Andric       LoadScriptOptions options;
950b57cec5SDimitry Andric       char python_module_path_cstr[PATH_MAX];
960b57cec5SDimitry Andric       python_module_path.GetPath(python_module_path_cstr,
970b57cec5SDimitry Andric                                  sizeof(python_module_path_cstr));
980b57cec5SDimitry Andric       Status error;
99*fe6060f1SDimitry Andric       if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
100*fe6060f1SDimitry Andric                                              error)) {
1010b57cec5SDimitry Andric         // Strip the ".py" extension if there is one
1020b57cec5SDimitry Andric         size_t py_extension_pos = os_plugin_class_name.rfind(".py");
1030b57cec5SDimitry Andric         if (py_extension_pos != std::string::npos)
1040b57cec5SDimitry Andric           os_plugin_class_name.erase(py_extension_pos);
1050b57cec5SDimitry Andric         // Add ".OperatingSystemPlugIn" to the module name to get a string like
1060b57cec5SDimitry Andric         // "modulename.OperatingSystemPlugIn"
1070b57cec5SDimitry Andric         os_plugin_class_name += ".OperatingSystemPlugIn";
1080b57cec5SDimitry Andric         StructuredData::ObjectSP object_sp =
1090b57cec5SDimitry Andric             m_interpreter->OSPlugin_CreatePluginObject(
1100b57cec5SDimitry Andric                 os_plugin_class_name.c_str(), process->CalculateProcess());
1110b57cec5SDimitry Andric         if (object_sp && object_sp->IsValid())
1120b57cec5SDimitry Andric           m_python_object_sp = object_sp;
1130b57cec5SDimitry Andric       }
1140b57cec5SDimitry Andric     }
1150b57cec5SDimitry Andric   }
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
118*fe6060f1SDimitry Andric OperatingSystemPython::~OperatingSystemPython() = default;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
1210b57cec5SDimitry Andric   if (m_register_info_up == nullptr) {
1220b57cec5SDimitry Andric     if (!m_interpreter || !m_python_object_sp)
1230b57cec5SDimitry Andric       return nullptr;
1240b57cec5SDimitry Andric     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1250b57cec5SDimitry Andric 
1269dba64beSDimitry Andric     LLDB_LOGF(log,
1279dba64beSDimitry Andric               "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
1280b57cec5SDimitry Andric               "thread register definitions from python for pid %" PRIu64,
1290b57cec5SDimitry Andric               m_process->GetID());
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric     StructuredData::DictionarySP dictionary =
1320b57cec5SDimitry Andric         m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp);
1330b57cec5SDimitry Andric     if (!dictionary)
1340b57cec5SDimitry Andric       return nullptr;
1350b57cec5SDimitry Andric 
136e8d8bef9SDimitry Andric     m_register_info_up = std::make_unique<DynamicRegisterInfo>(
137e8d8bef9SDimitry Andric         *dictionary, m_process->GetTarget().GetArchitecture());
1380b57cec5SDimitry Andric     assert(m_register_info_up->GetNumRegisters() > 0);
1390b57cec5SDimitry Andric     assert(m_register_info_up->GetNumRegisterSets() > 0);
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric   return m_register_info_up.get();
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric // PluginInterface protocol
1450b57cec5SDimitry Andric ConstString OperatingSystemPython::GetPluginName() {
1460b57cec5SDimitry Andric   return GetPluginNameStatic();
1470b57cec5SDimitry Andric }
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric uint32_t OperatingSystemPython::GetPluginVersion() { return 1; }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
1520b57cec5SDimitry Andric                                              ThreadList &core_thread_list,
1530b57cec5SDimitry Andric                                              ThreadList &new_thread_list) {
1540b57cec5SDimitry Andric   if (!m_interpreter || !m_python_object_sp)
1550b57cec5SDimitry Andric     return false;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS));
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   // First thing we have to do is to try to get the API lock, and the
1600b57cec5SDimitry Andric   // interpreter lock. We're going to change the thread content of the process,
1610b57cec5SDimitry Andric   // and we're going to use python, which requires the API lock to do it. We
1620b57cec5SDimitry Andric   // need the interpreter lock to make sure thread_info_dict stays alive.
1630b57cec5SDimitry Andric   //
1640b57cec5SDimitry Andric   // If someone already has the API lock, that is ok, we just want to avoid
1650b57cec5SDimitry Andric   // external code from making new API calls while this call is happening.
1660b57cec5SDimitry Andric   //
1670b57cec5SDimitry Andric   // This is a recursive lock so we can grant it to any Python code called on
1680b57cec5SDimitry Andric   // the stack below us.
1690b57cec5SDimitry Andric   Target &target = m_process->GetTarget();
1700b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
1710b57cec5SDimitry Andric                                                   std::defer_lock);
172480093f4SDimitry Andric   (void)api_lock.try_lock(); // See above.
1730b57cec5SDimitry Andric   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
1740b57cec5SDimitry Andric 
1759dba64beSDimitry Andric   LLDB_LOGF(log,
1769dba64beSDimitry Andric             "OperatingSystemPython::UpdateThreadList() fetching thread "
1770b57cec5SDimitry Andric             "data from python for pid %" PRIu64,
1780b57cec5SDimitry Andric             m_process->GetID());
1790b57cec5SDimitry Andric 
1809dba64beSDimitry Andric   // The threads that are in "core_thread_list" upon entry are the threads from
1810b57cec5SDimitry Andric   // the lldb_private::Process subclass, no memory threads will be in this
1820b57cec5SDimitry Andric   // list.
1830b57cec5SDimitry Andric   StructuredData::ArraySP threads_list =
1840b57cec5SDimitry Andric       m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp);
1850b57cec5SDimitry Andric 
1860b57cec5SDimitry Andric   const uint32_t num_cores = core_thread_list.GetSize(false);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   // Make a map so we can keep track of which cores were used from the
1890b57cec5SDimitry Andric   // core_thread list. Any real threads/cores that weren't used should later be
1900b57cec5SDimitry Andric   // put back into the "new_thread_list".
1910b57cec5SDimitry Andric   std::vector<bool> core_used_map(num_cores, false);
1920b57cec5SDimitry Andric   if (threads_list) {
1930b57cec5SDimitry Andric     if (log) {
1940b57cec5SDimitry Andric       StreamString strm;
1950b57cec5SDimitry Andric       threads_list->Dump(strm);
1969dba64beSDimitry Andric       LLDB_LOGF(log, "threads_list = %s", strm.GetData());
1970b57cec5SDimitry Andric     }
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric     const uint32_t num_threads = threads_list->GetSize();
2000b57cec5SDimitry Andric     for (uint32_t i = 0; i < num_threads; ++i) {
2010b57cec5SDimitry Andric       StructuredData::ObjectSP thread_dict_obj =
2020b57cec5SDimitry Andric           threads_list->GetItemAtIndex(i);
2030b57cec5SDimitry Andric       if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
2040b57cec5SDimitry Andric         ThreadSP thread_sp(CreateThreadFromThreadInfo(
2050b57cec5SDimitry Andric             *thread_dict, core_thread_list, old_thread_list, core_used_map,
2060b57cec5SDimitry Andric             nullptr));
2070b57cec5SDimitry Andric         if (thread_sp)
2080b57cec5SDimitry Andric           new_thread_list.AddThread(thread_sp);
2090b57cec5SDimitry Andric       }
2100b57cec5SDimitry Andric     }
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // Any real core threads that didn't end up backing a memory thread should
2140b57cec5SDimitry Andric   // still be in the main thread list, and they should be inserted at the
2150b57cec5SDimitry Andric   // beginning of the list
2160b57cec5SDimitry Andric   uint32_t insert_idx = 0;
2170b57cec5SDimitry Andric   for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
2180b57cec5SDimitry Andric     if (!core_used_map[core_idx]) {
2190b57cec5SDimitry Andric       new_thread_list.InsertThread(
2200b57cec5SDimitry Andric           core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
2210b57cec5SDimitry Andric       ++insert_idx;
2220b57cec5SDimitry Andric     }
2230b57cec5SDimitry Andric   }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   return new_thread_list.GetSize(false) > 0;
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
2290b57cec5SDimitry Andric     StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
2300b57cec5SDimitry Andric     ThreadList &old_thread_list, std::vector<bool> &core_used_map,
2310b57cec5SDimitry Andric     bool *did_create_ptr) {
2320b57cec5SDimitry Andric   ThreadSP thread_sp;
2330b57cec5SDimitry Andric   tid_t tid = LLDB_INVALID_THREAD_ID;
2340b57cec5SDimitry Andric   if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2350b57cec5SDimitry Andric     return ThreadSP();
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   uint32_t core_number;
2380b57cec5SDimitry Andric   addr_t reg_data_addr;
2390b57cec5SDimitry Andric   llvm::StringRef name;
2400b57cec5SDimitry Andric   llvm::StringRef queue;
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
2430b57cec5SDimitry Andric   thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
2440b57cec5SDimitry Andric                                       LLDB_INVALID_ADDRESS);
2450b57cec5SDimitry Andric   thread_dict.GetValueForKeyAsString("name", name);
2460b57cec5SDimitry Andric   thread_dict.GetValueForKeyAsString("queue", queue);
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   // See if a thread already exists for "tid"
2490b57cec5SDimitry Andric   thread_sp = old_thread_list.FindThreadByID(tid, false);
2500b57cec5SDimitry Andric   if (thread_sp) {
2510b57cec5SDimitry Andric     // A thread already does exist for "tid", make sure it was an operating
2520b57cec5SDimitry Andric     // system
2530b57cec5SDimitry Andric     // plug-in generated thread.
2540b57cec5SDimitry Andric     if (!IsOperatingSystemPluginThread(thread_sp)) {
2550b57cec5SDimitry Andric       // We have thread ID overlap between the protocol threads and the
2560b57cec5SDimitry Andric       // operating system threads, clear the thread so we create an operating
2570b57cec5SDimitry Andric       // system thread for this.
2580b57cec5SDimitry Andric       thread_sp.reset();
2590b57cec5SDimitry Andric     }
2600b57cec5SDimitry Andric   }
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   if (!thread_sp) {
2630b57cec5SDimitry Andric     if (did_create_ptr)
2640b57cec5SDimitry Andric       *did_create_ptr = true;
2650b57cec5SDimitry Andric     thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
2660b57cec5SDimitry Andric                                                reg_data_addr);
2670b57cec5SDimitry Andric   }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric   if (core_number < core_thread_list.GetSize(false)) {
2700b57cec5SDimitry Andric     ThreadSP core_thread_sp(
2710b57cec5SDimitry Andric         core_thread_list.GetThreadAtIndex(core_number, false));
2720b57cec5SDimitry Andric     if (core_thread_sp) {
2730b57cec5SDimitry Andric       // Keep track of which cores were set as the backing thread for memory
2740b57cec5SDimitry Andric       // threads...
2750b57cec5SDimitry Andric       if (core_number < core_used_map.size())
2760b57cec5SDimitry Andric         core_used_map[core_number] = true;
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric       ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
2790b57cec5SDimitry Andric       if (backing_core_thread_sp) {
2800b57cec5SDimitry Andric         thread_sp->SetBackingThread(backing_core_thread_sp);
2810b57cec5SDimitry Andric       } else {
2820b57cec5SDimitry Andric         thread_sp->SetBackingThread(core_thread_sp);
2830b57cec5SDimitry Andric       }
2840b57cec5SDimitry Andric     }
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric   return thread_sp;
2870b57cec5SDimitry Andric }
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric RegisterContextSP
2920b57cec5SDimitry Andric OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
2930b57cec5SDimitry Andric                                                       addr_t reg_data_addr) {
2940b57cec5SDimitry Andric   RegisterContextSP reg_ctx_sp;
2950b57cec5SDimitry Andric   if (!m_interpreter || !m_python_object_sp || !thread)
2960b57cec5SDimitry Andric     return reg_ctx_sp;
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
2990b57cec5SDimitry Andric     return reg_ctx_sp;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // First thing we have to do is to try to get the API lock, and the
3020b57cec5SDimitry Andric   // interpreter lock. We're going to change the thread content of the process,
3030b57cec5SDimitry Andric   // and we're going to use python, which requires the API lock to do it. We
3040b57cec5SDimitry Andric   // need the interpreter lock to make sure thread_info_dict stays alive.
3050b57cec5SDimitry Andric   //
3060b57cec5SDimitry Andric   // If someone already has the API lock, that is ok, we just want to avoid
3070b57cec5SDimitry Andric   // external code from making new API calls while this call is happening.
3080b57cec5SDimitry Andric   //
3090b57cec5SDimitry Andric   // This is a recursive lock so we can grant it to any Python code called on
3100b57cec5SDimitry Andric   // the stack below us.
3110b57cec5SDimitry Andric   Target &target = m_process->GetTarget();
3120b57cec5SDimitry Andric   std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
3130b57cec5SDimitry Andric                                                   std::defer_lock);
314480093f4SDimitry Andric   (void)api_lock.try_lock(); // See above.
3150b57cec5SDimitry Andric   auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   if (reg_data_addr != LLDB_INVALID_ADDRESS) {
3200b57cec5SDimitry Andric     // The registers data is in contiguous memory, just create the register
3210b57cec5SDimitry Andric     // context using the address provided
3229dba64beSDimitry Andric     LLDB_LOGF(log,
3239dba64beSDimitry Andric               "OperatingSystemPython::CreateRegisterContextForThread (tid "
3240b57cec5SDimitry Andric               "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
3250b57cec5SDimitry Andric               ") creating memory register context",
3260b57cec5SDimitry Andric               thread->GetID(), thread->GetProtocolID(), reg_data_addr);
3270b57cec5SDimitry Andric     reg_ctx_sp = std::make_shared<RegisterContextMemory>(
3280b57cec5SDimitry Andric         *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
3290b57cec5SDimitry Andric   } else {
3300b57cec5SDimitry Andric     // No register data address is provided, query the python plug-in to let it
3310b57cec5SDimitry Andric     // make up the data as it sees fit
3329dba64beSDimitry Andric     LLDB_LOGF(log,
3339dba64beSDimitry Andric               "OperatingSystemPython::CreateRegisterContextForThread (tid "
3340b57cec5SDimitry Andric               "= 0x%" PRIx64 ", 0x%" PRIx64
3350b57cec5SDimitry Andric               ") fetching register data from python",
3360b57cec5SDimitry Andric               thread->GetID(), thread->GetProtocolID());
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric     StructuredData::StringSP reg_context_data =
3390b57cec5SDimitry Andric         m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp,
3400b57cec5SDimitry Andric                                                     thread->GetID());
3410b57cec5SDimitry Andric     if (reg_context_data) {
3425ffd83dbSDimitry Andric       std::string value = std::string(reg_context_data->GetValue());
3430b57cec5SDimitry Andric       DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
3440b57cec5SDimitry Andric       if (data_sp->GetByteSize()) {
3450b57cec5SDimitry Andric         RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
3460b57cec5SDimitry Andric             *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
3470b57cec5SDimitry Andric         if (reg_ctx_memory) {
3480b57cec5SDimitry Andric           reg_ctx_sp.reset(reg_ctx_memory);
3490b57cec5SDimitry Andric           reg_ctx_memory->SetAllRegisterData(data_sp);
3500b57cec5SDimitry Andric         }
3510b57cec5SDimitry Andric       }
3520b57cec5SDimitry Andric     }
3530b57cec5SDimitry Andric   }
3540b57cec5SDimitry Andric   // if we still have no register data, fallback on a dummy context to avoid
3550b57cec5SDimitry Andric   // crashing
3560b57cec5SDimitry Andric   if (!reg_ctx_sp) {
3579dba64beSDimitry Andric     LLDB_LOGF(log,
3589dba64beSDimitry Andric               "OperatingSystemPython::CreateRegisterContextForThread (tid "
3590b57cec5SDimitry Andric               "= 0x%" PRIx64 ") forcing a dummy register context",
3600b57cec5SDimitry Andric               thread->GetID());
3610b57cec5SDimitry Andric     reg_ctx_sp = std::make_shared<RegisterContextDummy>(
3620b57cec5SDimitry Andric         *thread, 0, target.GetArchitecture().GetAddressByteSize());
3630b57cec5SDimitry Andric   }
3640b57cec5SDimitry Andric   return reg_ctx_sp;
3650b57cec5SDimitry Andric }
3660b57cec5SDimitry Andric 
3670b57cec5SDimitry Andric StopInfoSP
3680b57cec5SDimitry Andric OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
3690b57cec5SDimitry Andric   // We should have gotten the thread stop info from the dictionary of data for
3700b57cec5SDimitry Andric   // the thread in the initial call to get_thread_info(), this should have been
3710b57cec5SDimitry Andric   // cached so we can return it here
3720b57cec5SDimitry Andric   StopInfoSP
3730b57cec5SDimitry Andric       stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
3740b57cec5SDimitry Andric   return stop_info_sp;
3750b57cec5SDimitry Andric }
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
3780b57cec5SDimitry Andric                                                    addr_t context) {
3790b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
3800b57cec5SDimitry Andric 
3819dba64beSDimitry Andric   LLDB_LOGF(log,
3829dba64beSDimitry Andric             "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
3830b57cec5SDimitry Andric             ", context = 0x%" PRIx64 ") fetching register data from python",
3840b57cec5SDimitry Andric             tid, context);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   if (m_interpreter && m_python_object_sp) {
3870b57cec5SDimitry Andric     // First thing we have to do is to try to get the API lock, and the
3880b57cec5SDimitry Andric     // interpreter lock. We're going to change the thread content of the
3890b57cec5SDimitry Andric     // process, and we're going to use python, which requires the API lock to
3900b57cec5SDimitry Andric     // do it. We need the interpreter lock to make sure thread_info_dict stays
3910b57cec5SDimitry Andric     // alive.
3920b57cec5SDimitry Andric     //
3930b57cec5SDimitry Andric     // If someone already has the API lock, that is ok, we just want to avoid
3940b57cec5SDimitry Andric     // external code from making new API calls while this call is happening.
3950b57cec5SDimitry Andric     //
3960b57cec5SDimitry Andric     // This is a recursive lock so we can grant it to any Python code called on
3970b57cec5SDimitry Andric     // the stack below us.
3980b57cec5SDimitry Andric     Target &target = m_process->GetTarget();
3990b57cec5SDimitry Andric     std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(),
4000b57cec5SDimitry Andric                                                     std::defer_lock);
401480093f4SDimitry Andric     (void)api_lock.try_lock(); // See above.
4020b57cec5SDimitry Andric     auto interpreter_lock = m_interpreter->AcquireInterpreterLock();
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric     StructuredData::DictionarySP thread_info_dict =
4050b57cec5SDimitry Andric         m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context);
4060b57cec5SDimitry Andric     std::vector<bool> core_used_map;
4070b57cec5SDimitry Andric     if (thread_info_dict) {
4080b57cec5SDimitry Andric       ThreadList core_threads(m_process);
4090b57cec5SDimitry Andric       ThreadList &thread_list = m_process->GetThreadList();
4100b57cec5SDimitry Andric       bool did_create = false;
4110b57cec5SDimitry Andric       ThreadSP thread_sp(
4120b57cec5SDimitry Andric           CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
4130b57cec5SDimitry Andric                                      thread_list, core_used_map, &did_create));
4140b57cec5SDimitry Andric       if (did_create)
4150b57cec5SDimitry Andric         thread_list.AddThread(thread_sp);
4160b57cec5SDimitry Andric       return thread_sp;
4170b57cec5SDimitry Andric     }
4180b57cec5SDimitry Andric   }
4190b57cec5SDimitry Andric   return ThreadSP();
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
422480093f4SDimitry Andric #endif // #if LLDB_ENABLE_PYTHON
423