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/RegisterContextDummy.h" 160b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextMemory.h" 170b57cec5SDimitry Andric #include "Plugins/Process/Utility/ThreadMemory.h" 180b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 190b57cec5SDimitry Andric #include "lldb/Core/Module.h" 200b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h" 210b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h" 220b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 230b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h" 240b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h" 250b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h" 260b57cec5SDimitry Andric #include "lldb/Target/Process.h" 270b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h" 280b57cec5SDimitry Andric #include "lldb/Target/Target.h" 290b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 300b57cec5SDimitry Andric #include "lldb/Target/ThreadList.h" 310b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h" 3281ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.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 69349cc55cSDimitry Andric llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() { 700b57cec5SDimitry Andric return "Operating system plug-in that gathers OS information from a python " 710b57cec5SDimitry Andric "class that implements the necessary OperatingSystem functionality."; 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process, 750b57cec5SDimitry Andric const FileSpec &python_module_path) 760b57cec5SDimitry Andric : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(), 770b57cec5SDimitry Andric m_interpreter(nullptr), m_python_object_sp() { 780b57cec5SDimitry Andric if (!process) 790b57cec5SDimitry Andric return; 800b57cec5SDimitry Andric TargetSP target_sp = process->CalculateTarget(); 810b57cec5SDimitry Andric if (!target_sp) 820b57cec5SDimitry Andric return; 830b57cec5SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 840b57cec5SDimitry Andric if (m_interpreter) { 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric std::string os_plugin_class_name( 870b57cec5SDimitry Andric python_module_path.GetFilename().AsCString("")); 880b57cec5SDimitry Andric if (!os_plugin_class_name.empty()) { 89fe6060f1SDimitry Andric LoadScriptOptions options; 900b57cec5SDimitry Andric char python_module_path_cstr[PATH_MAX]; 910b57cec5SDimitry Andric python_module_path.GetPath(python_module_path_cstr, 920b57cec5SDimitry Andric sizeof(python_module_path_cstr)); 930b57cec5SDimitry Andric Status error; 94fe6060f1SDimitry Andric if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options, 95fe6060f1SDimitry Andric error)) { 960b57cec5SDimitry Andric // Strip the ".py" extension if there is one 970b57cec5SDimitry Andric size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 980b57cec5SDimitry Andric if (py_extension_pos != std::string::npos) 990b57cec5SDimitry Andric os_plugin_class_name.erase(py_extension_pos); 1000b57cec5SDimitry Andric // Add ".OperatingSystemPlugIn" to the module name to get a string like 1010b57cec5SDimitry Andric // "modulename.OperatingSystemPlugIn" 1020b57cec5SDimitry Andric os_plugin_class_name += ".OperatingSystemPlugIn"; 1030b57cec5SDimitry Andric StructuredData::ObjectSP object_sp = 1040b57cec5SDimitry Andric m_interpreter->OSPlugin_CreatePluginObject( 1050b57cec5SDimitry Andric os_plugin_class_name.c_str(), process->CalculateProcess()); 1060b57cec5SDimitry Andric if (object_sp && object_sp->IsValid()) 1070b57cec5SDimitry Andric m_python_object_sp = object_sp; 1080b57cec5SDimitry Andric } 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric 113fe6060f1SDimitry Andric OperatingSystemPython::~OperatingSystemPython() = default; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() { 1160b57cec5SDimitry Andric if (m_register_info_up == nullptr) { 1170b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp) 1180b57cec5SDimitry Andric return nullptr; 11981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS); 1200b57cec5SDimitry Andric 1219dba64beSDimitry Andric LLDB_LOGF(log, 1229dba64beSDimitry Andric "OperatingSystemPython::GetDynamicRegisterInfo() fetching " 1230b57cec5SDimitry Andric "thread register definitions from python for pid %" PRIu64, 1240b57cec5SDimitry Andric m_process->GetID()); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric StructuredData::DictionarySP dictionary = 1270b57cec5SDimitry Andric m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp); 1280b57cec5SDimitry Andric if (!dictionary) 1290b57cec5SDimitry Andric return nullptr; 1300b57cec5SDimitry Andric 131*06c3fb27SDimitry Andric m_register_info_up = DynamicRegisterInfo::Create( 132e8d8bef9SDimitry Andric *dictionary, m_process->GetTarget().GetArchitecture()); 133*06c3fb27SDimitry Andric assert(m_register_info_up); 1340b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisters() > 0); 1350b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisterSets() > 0); 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric return m_register_info_up.get(); 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list, 1410b57cec5SDimitry Andric ThreadList &core_thread_list, 1420b57cec5SDimitry Andric ThreadList &new_thread_list) { 1430b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp) 1440b57cec5SDimitry Andric return false; 1450b57cec5SDimitry Andric 14681ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS); 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 1490b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the process, 1500b57cec5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We 1510b57cec5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive. 1520b57cec5SDimitry Andric // 1530b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 1540b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 1550b57cec5SDimitry Andric // 1560b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 1570b57cec5SDimitry Andric // the stack below us. 1580b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 1590b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 1600b57cec5SDimitry Andric std::defer_lock); 161480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 1620b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 1630b57cec5SDimitry Andric 1649dba64beSDimitry Andric LLDB_LOGF(log, 1659dba64beSDimitry Andric "OperatingSystemPython::UpdateThreadList() fetching thread " 1660b57cec5SDimitry Andric "data from python for pid %" PRIu64, 1670b57cec5SDimitry Andric m_process->GetID()); 1680b57cec5SDimitry Andric 1699dba64beSDimitry Andric // The threads that are in "core_thread_list" upon entry are the threads from 1700b57cec5SDimitry Andric // the lldb_private::Process subclass, no memory threads will be in this 1710b57cec5SDimitry Andric // list. 1720b57cec5SDimitry Andric StructuredData::ArraySP threads_list = 1730b57cec5SDimitry Andric m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric const uint32_t num_cores = core_thread_list.GetSize(false); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric // Make a map so we can keep track of which cores were used from the 1780b57cec5SDimitry Andric // core_thread list. Any real threads/cores that weren't used should later be 1790b57cec5SDimitry Andric // put back into the "new_thread_list". 1800b57cec5SDimitry Andric std::vector<bool> core_used_map(num_cores, false); 1810b57cec5SDimitry Andric if (threads_list) { 1820b57cec5SDimitry Andric if (log) { 1830b57cec5SDimitry Andric StreamString strm; 1840b57cec5SDimitry Andric threads_list->Dump(strm); 1859dba64beSDimitry Andric LLDB_LOGF(log, "threads_list = %s", strm.GetData()); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric const uint32_t num_threads = threads_list->GetSize(); 1890b57cec5SDimitry Andric for (uint32_t i = 0; i < num_threads; ++i) { 1900b57cec5SDimitry Andric StructuredData::ObjectSP thread_dict_obj = 1910b57cec5SDimitry Andric threads_list->GetItemAtIndex(i); 1920b57cec5SDimitry Andric if (auto thread_dict = thread_dict_obj->GetAsDictionary()) { 1930b57cec5SDimitry Andric ThreadSP thread_sp(CreateThreadFromThreadInfo( 1940b57cec5SDimitry Andric *thread_dict, core_thread_list, old_thread_list, core_used_map, 1950b57cec5SDimitry Andric nullptr)); 1960b57cec5SDimitry Andric if (thread_sp) 1970b57cec5SDimitry Andric new_thread_list.AddThread(thread_sp); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric } 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // Any real core threads that didn't end up backing a memory thread should 2030b57cec5SDimitry Andric // still be in the main thread list, and they should be inserted at the 2040b57cec5SDimitry Andric // beginning of the list 2050b57cec5SDimitry Andric uint32_t insert_idx = 0; 2060b57cec5SDimitry Andric for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) { 2070b57cec5SDimitry Andric if (!core_used_map[core_idx]) { 2080b57cec5SDimitry Andric new_thread_list.InsertThread( 2090b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx); 2100b57cec5SDimitry Andric ++insert_idx; 2110b57cec5SDimitry Andric } 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric return new_thread_list.GetSize(false) > 0; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo( 2180b57cec5SDimitry Andric StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list, 2190b57cec5SDimitry Andric ThreadList &old_thread_list, std::vector<bool> &core_used_map, 2200b57cec5SDimitry Andric bool *did_create_ptr) { 2210b57cec5SDimitry Andric ThreadSP thread_sp; 2220b57cec5SDimitry Andric tid_t tid = LLDB_INVALID_THREAD_ID; 2230b57cec5SDimitry Andric if (!thread_dict.GetValueForKeyAsInteger("tid", tid)) 2240b57cec5SDimitry Andric return ThreadSP(); 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric uint32_t core_number; 2270b57cec5SDimitry Andric addr_t reg_data_addr; 2280b57cec5SDimitry Andric llvm::StringRef name; 2290b57cec5SDimitry Andric llvm::StringRef queue; 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX); 2320b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, 2330b57cec5SDimitry Andric LLDB_INVALID_ADDRESS); 2340b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("name", name); 2350b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("queue", queue); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // See if a thread already exists for "tid" 2380b57cec5SDimitry Andric thread_sp = old_thread_list.FindThreadByID(tid, false); 2390b57cec5SDimitry Andric if (thread_sp) { 2400b57cec5SDimitry Andric // A thread already does exist for "tid", make sure it was an operating 2410b57cec5SDimitry Andric // system 2420b57cec5SDimitry Andric // plug-in generated thread. 2430b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread_sp)) { 2440b57cec5SDimitry Andric // We have thread ID overlap between the protocol threads and the 2450b57cec5SDimitry Andric // operating system threads, clear the thread so we create an operating 2460b57cec5SDimitry Andric // system thread for this. 2470b57cec5SDimitry Andric thread_sp.reset(); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric if (!thread_sp) { 2520b57cec5SDimitry Andric if (did_create_ptr) 2530b57cec5SDimitry Andric *did_create_ptr = true; 2540b57cec5SDimitry Andric thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue, 2550b57cec5SDimitry Andric reg_data_addr); 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric if (core_number < core_thread_list.GetSize(false)) { 2590b57cec5SDimitry Andric ThreadSP core_thread_sp( 2600b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_number, false)); 2610b57cec5SDimitry Andric if (core_thread_sp) { 2620b57cec5SDimitry Andric // Keep track of which cores were set as the backing thread for memory 2630b57cec5SDimitry Andric // threads... 2640b57cec5SDimitry Andric if (core_number < core_used_map.size()) 2650b57cec5SDimitry Andric core_used_map[core_number] = true; 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread()); 2680b57cec5SDimitry Andric if (backing_core_thread_sp) { 2690b57cec5SDimitry Andric thread_sp->SetBackingThread(backing_core_thread_sp); 2700b57cec5SDimitry Andric } else { 2710b57cec5SDimitry Andric thread_sp->SetBackingThread(core_thread_sp); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric return thread_sp; 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric void OperatingSystemPython::ThreadWasSelected(Thread *thread) {} 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric RegisterContextSP 2810b57cec5SDimitry Andric OperatingSystemPython::CreateRegisterContextForThread(Thread *thread, 2820b57cec5SDimitry Andric addr_t reg_data_addr) { 2830b57cec5SDimitry Andric RegisterContextSP reg_ctx_sp; 2840b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp || !thread) 2850b57cec5SDimitry Andric return reg_ctx_sp; 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread->shared_from_this())) 2880b57cec5SDimitry Andric return reg_ctx_sp; 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 2910b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the process, 2920b57cec5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We 2930b57cec5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive. 2940b57cec5SDimitry Andric // 2950b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 2960b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 2970b57cec5SDimitry Andric // 2980b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 2990b57cec5SDimitry Andric // the stack below us. 3000b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 3010b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 3020b57cec5SDimitry Andric std::defer_lock); 303480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 3040b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 3050b57cec5SDimitry Andric 30681ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric if (reg_data_addr != LLDB_INVALID_ADDRESS) { 3090b57cec5SDimitry Andric // The registers data is in contiguous memory, just create the register 3100b57cec5SDimitry Andric // context using the address provided 3119dba64beSDimitry Andric LLDB_LOGF(log, 3129dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 3130b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 3140b57cec5SDimitry Andric ") creating memory register context", 3150b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID(), reg_data_addr); 3160b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextMemory>( 3170b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr); 3180b57cec5SDimitry Andric } else { 3190b57cec5SDimitry Andric // No register data address is provided, query the python plug-in to let it 3200b57cec5SDimitry Andric // make up the data as it sees fit 3219dba64beSDimitry Andric LLDB_LOGF(log, 3229dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 3230b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 3240b57cec5SDimitry Andric ") fetching register data from python", 3250b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID()); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric StructuredData::StringSP reg_context_data = 3280b57cec5SDimitry Andric m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, 3290b57cec5SDimitry Andric thread->GetID()); 3300b57cec5SDimitry Andric if (reg_context_data) { 3315ffd83dbSDimitry Andric std::string value = std::string(reg_context_data->GetValue()); 3320b57cec5SDimitry Andric DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length())); 3330b57cec5SDimitry Andric if (data_sp->GetByteSize()) { 3340b57cec5SDimitry Andric RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory( 3350b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); 3360b57cec5SDimitry Andric if (reg_ctx_memory) { 3370b57cec5SDimitry Andric reg_ctx_sp.reset(reg_ctx_memory); 3380b57cec5SDimitry Andric reg_ctx_memory->SetAllRegisterData(data_sp); 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric // if we still have no register data, fallback on a dummy context to avoid 3440b57cec5SDimitry Andric // crashing 3450b57cec5SDimitry Andric if (!reg_ctx_sp) { 3469dba64beSDimitry Andric LLDB_LOGF(log, 3479dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 3480b57cec5SDimitry Andric "= 0x%" PRIx64 ") forcing a dummy register context", 3490b57cec5SDimitry Andric thread->GetID()); 3500b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextDummy>( 3510b57cec5SDimitry Andric *thread, 0, target.GetArchitecture().GetAddressByteSize()); 3520b57cec5SDimitry Andric } 3530b57cec5SDimitry Andric return reg_ctx_sp; 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric StopInfoSP 3570b57cec5SDimitry Andric OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) { 3580b57cec5SDimitry Andric // We should have gotten the thread stop info from the dictionary of data for 3590b57cec5SDimitry Andric // the thread in the initial call to get_thread_info(), this should have been 3600b57cec5SDimitry Andric // cached so we can return it here 3610b57cec5SDimitry Andric StopInfoSP 3620b57cec5SDimitry Andric stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 3630b57cec5SDimitry Andric return stop_info_sp; 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid, 3670b57cec5SDimitry Andric addr_t context) { 36881ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread); 3690b57cec5SDimitry Andric 3709dba64beSDimitry Andric LLDB_LOGF(log, 3719dba64beSDimitry Andric "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 3720b57cec5SDimitry Andric ", context = 0x%" PRIx64 ") fetching register data from python", 3730b57cec5SDimitry Andric tid, context); 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric if (m_interpreter && m_python_object_sp) { 3760b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 3770b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the 3780b57cec5SDimitry Andric // process, and we're going to use python, which requires the API lock to 3790b57cec5SDimitry Andric // do it. We need the interpreter lock to make sure thread_info_dict stays 3800b57cec5SDimitry Andric // alive. 3810b57cec5SDimitry Andric // 3820b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 3830b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 3840b57cec5SDimitry Andric // 3850b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 3860b57cec5SDimitry Andric // the stack below us. 3870b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 3880b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 3890b57cec5SDimitry Andric std::defer_lock); 390480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 3910b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric StructuredData::DictionarySP thread_info_dict = 3940b57cec5SDimitry Andric m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context); 3950b57cec5SDimitry Andric std::vector<bool> core_used_map; 3960b57cec5SDimitry Andric if (thread_info_dict) { 3970b57cec5SDimitry Andric ThreadList core_threads(m_process); 3980b57cec5SDimitry Andric ThreadList &thread_list = m_process->GetThreadList(); 3990b57cec5SDimitry Andric bool did_create = false; 4000b57cec5SDimitry Andric ThreadSP thread_sp( 4010b57cec5SDimitry Andric CreateThreadFromThreadInfo(*thread_info_dict, core_threads, 4020b57cec5SDimitry Andric thread_list, core_used_map, &did_create)); 4030b57cec5SDimitry Andric if (did_create) 4040b57cec5SDimitry Andric thread_list.AddThread(thread_sp); 4050b57cec5SDimitry Andric return thread_sp; 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric return ThreadSP(); 4090b57cec5SDimitry Andric } 4100b57cec5SDimitry Andric 411480093f4SDimitry Andric #endif // #if LLDB_ENABLE_PYTHON 412