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/RegisterContextDummy.h" 16b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h" 17b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h" 185790759aSEnrico Granata #include "lldb/Core/Debugger.h" 19b3e77600SGreg Clayton #include "lldb/Core/Module.h" 20b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h" 21b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 225790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 230641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h" 24b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 25b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h" 26b3e77600SGreg Clayton #include "lldb/Target/Process.h" 27b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h" 28b3e77600SGreg Clayton #include "lldb/Target/Target.h" 29b3e77600SGreg Clayton #include "lldb/Target/Thread.h" 30b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h" 31666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 32d821c997SPavel Labath #include "lldb/Utility/RegisterValue.h" 33bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 34f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h" 35b3e77600SGreg Clayton 36796ac80bSJonas Devlieghere #include <memory> 37796ac80bSJonas Devlieghere 38b3e77600SGreg Clayton using namespace lldb; 39b3e77600SGreg Clayton using namespace lldb_private; 40b3e77600SGreg Clayton 41bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(OperatingSystemPython) 42fbb4d1e4SJonas Devlieghere 43b9c1b51eSKate Stone void OperatingSystemPython::Initialize() { 44b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 45b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 46b9c1b51eSKate Stone nullptr); 47b3e77600SGreg Clayton } 48b3e77600SGreg Clayton 49b9c1b51eSKate Stone void OperatingSystemPython::Terminate() { 50b3e77600SGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 51b3e77600SGreg Clayton } 52b3e77600SGreg Clayton 53b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process, 54b9c1b51eSKate Stone bool force) { 5505097246SAdrian Prantl // Python OperatingSystem plug-ins must be requested by name, so force must 5605097246SAdrian Prantl // be true 57c9d645d3SGreg Clayton FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath()); 58dbd7fabaSJonas Devlieghere if (python_os_plugin_spec && 59dbd7fabaSJonas Devlieghere FileSystem::Instance().Exists(python_os_plugin_spec)) { 60d5b44036SJonas Devlieghere std::unique_ptr<OperatingSystemPython> os_up( 61b9c1b51eSKate Stone new OperatingSystemPython(process, python_os_plugin_spec)); 62d5b44036SJonas Devlieghere if (os_up.get() && os_up->IsValid()) 63d5b44036SJonas Devlieghere return os_up.release(); 64c9d645d3SGreg Clayton } 65248a1305SKonrad Kleine return nullptr; 66b3e77600SGreg Clayton } 67b3e77600SGreg Clayton 682ace1e57SPavel Labath llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() { 69b9c1b51eSKate Stone return "Operating system plug-in that gathers OS information from a python " 70b9c1b51eSKate Stone "class that implements the necessary OperatingSystem functionality."; 71b3e77600SGreg Clayton } 72b3e77600SGreg Clayton 73b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process, 74b9c1b51eSKate Stone const FileSpec &python_module_path) 75d5b44036SJonas Devlieghere : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(), 76248a1305SKonrad Kleine m_interpreter(nullptr), m_python_object_sp() { 775790759aSEnrico Granata if (!process) 785790759aSEnrico Granata return; 79a4d8747dSGreg Clayton TargetSP target_sp = process->CalculateTarget(); 805790759aSEnrico Granata if (!target_sp) 815790759aSEnrico Granata return; 822b29b432SJonas Devlieghere m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 83b9c1b51eSKate Stone if (m_interpreter) { 842443cbd7SGreg Clayton 85b9c1b51eSKate Stone std::string os_plugin_class_name( 86b9c1b51eSKate Stone python_module_path.GetFilename().AsCString("")); 87b9c1b51eSKate Stone if (!os_plugin_class_name.empty()) { 88f9517353SJonas Devlieghere LoadScriptOptions options; 89c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX]; 90b9c1b51eSKate Stone python_module_path.GetPath(python_module_path_cstr, 91b9c1b51eSKate Stone sizeof(python_module_path_cstr)); 9297206d57SZachary Turner Status error; 93f9517353SJonas Devlieghere if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options, 94f9517353SJonas Devlieghere error)) { 95c9d645d3SGreg Clayton // Strip the ".py" extension if there is one 96c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 97c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos) 98c9d645d3SGreg Clayton os_plugin_class_name.erase(py_extension_pos); 99b9c1b51eSKate Stone // Add ".OperatingSystemPlugIn" to the module name to get a string like 100b9c1b51eSKate Stone // "modulename.OperatingSystemPlugIn" 101c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn"; 1020641ca1aSZachary Turner StructuredData::ObjectSP object_sp = 103b9c1b51eSKate Stone m_interpreter->OSPlugin_CreatePluginObject( 104b9c1b51eSKate Stone os_plugin_class_name.c_str(), process->CalculateProcess()); 1050641ca1aSZachary Turner if (object_sp && object_sp->IsValid()) 106a4d8747dSGreg Clayton m_python_object_sp = object_sp; 107c9d645d3SGreg Clayton } 1082443cbd7SGreg Clayton } 1095790759aSEnrico Granata } 110b3e77600SGreg Clayton } 111b3e77600SGreg Clayton 112fd2433e1SJonas Devlieghere OperatingSystemPython::~OperatingSystemPython() = default; 113b3e77600SGreg Clayton 114b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() { 115248a1305SKonrad Kleine if (m_register_info_up == nullptr) { 116a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 117248a1305SKonrad Kleine return nullptr; 118*a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::OS); 1191c22be69SGreg Clayton 12063e5fb76SJonas Devlieghere LLDB_LOGF(log, 12163e5fb76SJonas Devlieghere "OperatingSystemPython::GetDynamicRegisterInfo() fetching " 122b9c1b51eSKate Stone "thread register definitions from python for pid %" PRIu64, 123b9c1b51eSKate Stone m_process->GetID()); 1241c22be69SGreg Clayton 125b9c1b51eSKate Stone StructuredData::DictionarySP dictionary = 126b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp); 1275790759aSEnrico Granata if (!dictionary) 128248a1305SKonrad Kleine return nullptr; 129b3e77600SGreg Clayton 130d2ec9184SJonas Devlieghere m_register_info_up = std::make_unique<DynamicRegisterInfo>( 131d2ec9184SJonas Devlieghere *dictionary, m_process->GetTarget().GetArchitecture()); 132d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisters() > 0); 133d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisterSets() > 0); 134b3e77600SGreg Clayton } 135d5b44036SJonas Devlieghere return m_register_info_up.get(); 136b3e77600SGreg Clayton } 137b3e77600SGreg Clayton 138b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list, 139ba4e61d3SAndrew Kaylor ThreadList &core_thread_list, 140b9c1b51eSKate Stone ThreadList &new_thread_list) { 141a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 142a85e6b6cSDaniel Malea return false; 1431c22be69SGreg Clayton 144*a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::OS); 1451c22be69SGreg Clayton 14685a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 14785a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process, 14885a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We 14985a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive. 150ab745c2aSGreg Clayton // 151ab745c2aSGreg Clayton // If someone already has the API lock, that is ok, we just want to avoid 152ab745c2aSGreg Clayton // external code from making new API calls while this call is happening. 153ab745c2aSGreg Clayton // 154ab745c2aSGreg Clayton // This is a recursive lock so we can grant it to any Python code called on 155ab745c2aSGreg Clayton // the stack below us. 15685e276b8SJim Ingham Target &target = m_process->GetTarget(); 15785a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 158b9c1b51eSKate Stone std::defer_lock); 1594d23764dSReid Kleckner (void)api_lock.try_lock(); // See above. 16085a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 16185e276b8SJim Ingham 16263e5fb76SJonas Devlieghere LLDB_LOGF(log, 16363e5fb76SJonas Devlieghere "OperatingSystemPython::UpdateThreadList() fetching thread " 164b9c1b51eSKate Stone "data from python for pid %" PRIu64, 165b9c1b51eSKate Stone m_process->GetID()); 1661c22be69SGreg Clayton 1678240b0d7SJim Ingham // The threads that are in "core_thread_list" upon entry are the threads from 16885a5ec51SJonas Devlieghere // the lldb_private::Process subclass, no memory threads will be in this 16985a5ec51SJonas Devlieghere // list. 170b9c1b51eSKate Stone StructuredData::ArraySP threads_list = 171b9c1b51eSKate Stone m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp); 172e98008ccSGreg Clayton 173e98008ccSGreg Clayton const uint32_t num_cores = core_thread_list.GetSize(false); 174e98008ccSGreg Clayton 175e98008ccSGreg Clayton // Make a map so we can keep track of which cores were used from the 17605097246SAdrian Prantl // core_thread list. Any real threads/cores that weren't used should later be 17705097246SAdrian Prantl // put back into the "new_thread_list". 178e98008ccSGreg Clayton std::vector<bool> core_used_map(num_cores, false); 179b9c1b51eSKate Stone if (threads_list) { 180b9c1b51eSKate Stone if (log) { 18162f80036SGreg Clayton StreamString strm; 1820641ca1aSZachary Turner threads_list->Dump(strm); 18363e5fb76SJonas Devlieghere LLDB_LOGF(log, "threads_list = %s", strm.GetData()); 18462f80036SGreg Clayton } 1850641ca1aSZachary Turner 1860641ca1aSZachary Turner const uint32_t num_threads = threads_list->GetSize(); 187b9c1b51eSKate Stone for (uint32_t i = 0; i < num_threads; ++i) { 188b9c1b51eSKate Stone StructuredData::ObjectSP thread_dict_obj = 189b9c1b51eSKate Stone threads_list->GetItemAtIndex(i); 190b9c1b51eSKate Stone if (auto thread_dict = thread_dict_obj->GetAsDictionary()) { 191248a1305SKonrad Kleine ThreadSP thread_sp(CreateThreadFromThreadInfo( 192248a1305SKonrad Kleine *thread_dict, core_thread_list, old_thread_list, core_used_map, 193248a1305SKonrad Kleine nullptr)); 194a4d8747dSGreg Clayton if (thread_sp) 195435ce139SGreg Clayton new_thread_list.AddThread(thread_sp); 196435ce139SGreg Clayton } 197435ce139SGreg Clayton } 198435ce139SGreg Clayton } 1996e0ff1a3SGreg Clayton 200e98008ccSGreg Clayton // Any real core threads that didn't end up backing a memory thread should 201b9c1b51eSKate Stone // still be in the main thread list, and they should be inserted at the 20205097246SAdrian Prantl // beginning of the list 203e98008ccSGreg Clayton uint32_t insert_idx = 0; 204b9c1b51eSKate Stone for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) { 205a6682a41SJonas Devlieghere if (!core_used_map[core_idx]) { 206b9c1b51eSKate Stone new_thread_list.InsertThread( 207b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx); 208e98008ccSGreg Clayton ++insert_idx; 209e98008ccSGreg Clayton } 210e98008ccSGreg Clayton } 2116e0ff1a3SGreg Clayton 212b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0; 213b3e77600SGreg Clayton } 214b3e77600SGreg Clayton 215b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo( 216b9c1b51eSKate Stone StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list, 217b9c1b51eSKate Stone ThreadList &old_thread_list, std::vector<bool> &core_used_map, 218b9c1b51eSKate Stone bool *did_create_ptr) { 219a4d8747dSGreg Clayton ThreadSP thread_sp; 2200641ca1aSZachary Turner tid_t tid = LLDB_INVALID_THREAD_ID; 2210641ca1aSZachary Turner if (!thread_dict.GetValueForKeyAsInteger("tid", tid)) 2220641ca1aSZachary Turner return ThreadSP(); 223a4d8747dSGreg Clayton 2240641ca1aSZachary Turner uint32_t core_number; 2250641ca1aSZachary Turner addr_t reg_data_addr; 2262833321fSZachary Turner llvm::StringRef name; 2272833321fSZachary Turner llvm::StringRef queue; 2280641ca1aSZachary Turner 2290641ca1aSZachary Turner thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX); 230b9c1b51eSKate Stone thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, 231b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 2320641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("name", name); 2330641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("queue", queue); 234a4d8747dSGreg Clayton 235160c9d81SGreg Clayton // See if a thread already exists for "tid" 236b3ae8761SGreg Clayton thread_sp = old_thread_list.FindThreadByID(tid, false); 237b9c1b51eSKate Stone if (thread_sp) { 238b9c1b51eSKate Stone // A thread already does exist for "tid", make sure it was an operating 239b9c1b51eSKate Stone // system 240160c9d81SGreg Clayton // plug-in generated thread. 241b9c1b51eSKate Stone if (!IsOperatingSystemPluginThread(thread_sp)) { 242160c9d81SGreg Clayton // We have thread ID overlap between the protocol threads and the 24305097246SAdrian Prantl // operating system threads, clear the thread so we create an operating 24405097246SAdrian Prantl // system thread for this. 245160c9d81SGreg Clayton thread_sp.reset(); 246160c9d81SGreg Clayton } 247160c9d81SGreg Clayton } 248160c9d81SGreg Clayton 249b9c1b51eSKate Stone if (!thread_sp) { 250a4d8747dSGreg Clayton if (did_create_ptr) 251a4d8747dSGreg Clayton *did_create_ptr = true; 252796ac80bSJonas Devlieghere thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue, 253796ac80bSJonas Devlieghere reg_data_addr); 254b3ae8761SGreg Clayton } 255b3ae8761SGreg Clayton 256b9c1b51eSKate Stone if (core_number < core_thread_list.GetSize(false)) { 257b9c1b51eSKate Stone ThreadSP core_thread_sp( 258b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_number, false)); 259b9c1b51eSKate Stone if (core_thread_sp) { 260b9c1b51eSKate Stone // Keep track of which cores were set as the backing thread for memory 261b9c1b51eSKate Stone // threads... 262e98008ccSGreg Clayton if (core_number < core_used_map.size()) 263e98008ccSGreg Clayton core_used_map[core_number] = true; 264e98008ccSGreg Clayton 265160c9d81SGreg Clayton ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread()); 266b9c1b51eSKate Stone if (backing_core_thread_sp) { 267160c9d81SGreg Clayton thread_sp->SetBackingThread(backing_core_thread_sp); 268b9c1b51eSKate Stone } else { 269160c9d81SGreg Clayton thread_sp->SetBackingThread(core_thread_sp); 270160c9d81SGreg Clayton } 271160c9d81SGreg Clayton } 272a4d8747dSGreg Clayton } 273a4d8747dSGreg Clayton return thread_sp; 274a4d8747dSGreg Clayton } 275a4d8747dSGreg Clayton 276b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {} 277b3e77600SGreg Clayton 278b3e77600SGreg Clayton RegisterContextSP 279b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread, 280b9c1b51eSKate Stone addr_t reg_data_addr) { 281435ce139SGreg Clayton RegisterContextSP reg_ctx_sp; 282a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp || !thread) 283160c9d81SGreg Clayton return reg_ctx_sp; 284160c9d81SGreg Clayton 285160c9d81SGreg Clayton if (!IsOperatingSystemPluginThread(thread->shared_from_this())) 286160c9d81SGreg Clayton return reg_ctx_sp; 2871c22be69SGreg Clayton 28885a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 28985a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process, 29085a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We 29185a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive. 29285a5ec51SJonas Devlieghere // 29385a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid 29485a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening. 29585a5ec51SJonas Devlieghere // 29685a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on 29785a5ec51SJonas Devlieghere // the stack below us. 29885e276b8SJim Ingham Target &target = m_process->GetTarget(); 29985a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 30085a5ec51SJonas Devlieghere std::defer_lock); 3014d23764dSReid Kleckner (void)api_lock.try_lock(); // See above. 30285a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 30385e276b8SJim Ingham 304*a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Thread); 3051c22be69SGreg Clayton 306b9c1b51eSKate Stone if (reg_data_addr != LLDB_INVALID_ADDRESS) { 307ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register 308ead45e01SGreg Clayton // context using the address provided 30963e5fb76SJonas Devlieghere LLDB_LOGF(log, 31063e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 311b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 312b9c1b51eSKate Stone ") creating memory register context", 313b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID(), reg_data_addr); 314796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextMemory>( 315796ac80bSJonas Devlieghere *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr); 316b9c1b51eSKate Stone } else { 31705097246SAdrian Prantl // No register data address is provided, query the python plug-in to let it 31805097246SAdrian Prantl // make up the data as it sees fit 31963e5fb76SJonas Devlieghere LLDB_LOGF(log, 32063e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 321b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 322b9c1b51eSKate Stone ") fetching register data from python", 323b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID()); 3241c22be69SGreg Clayton 325b9c1b51eSKate Stone StructuredData::StringSP reg_context_data = 326b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, 327b9c1b51eSKate Stone thread->GetID()); 328b9c1b51eSKate Stone if (reg_context_data) { 329adcd0268SBenjamin Kramer std::string value = std::string(reg_context_data->GetValue()); 3300641ca1aSZachary Turner DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length())); 331b9c1b51eSKate Stone if (data_sp->GetByteSize()) { 332b9c1b51eSKate Stone RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory( 333b9c1b51eSKate Stone *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); 334b9c1b51eSKate Stone if (reg_ctx_memory) { 335435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory); 336435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData(data_sp); 337435ce139SGreg Clayton } 338435ce139SGreg Clayton } 339435ce139SGreg Clayton } 340ead45e01SGreg Clayton } 341b9c1b51eSKate Stone // if we still have no register data, fallback on a dummy context to avoid 342b9c1b51eSKate Stone // crashing 343b9c1b51eSKate Stone if (!reg_ctx_sp) { 34463e5fb76SJonas Devlieghere LLDB_LOGF(log, 34563e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 346b9c1b51eSKate Stone "= 0x%" PRIx64 ") forcing a dummy register context", 347b9c1b51eSKate Stone thread->GetID()); 348796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextDummy>( 349796ac80bSJonas Devlieghere *thread, 0, target.GetArchitecture().GetAddressByteSize()); 350cbd79b6cSEnrico Granata } 351b3e77600SGreg Clayton return reg_ctx_sp; 352b3e77600SGreg Clayton } 353b3e77600SGreg Clayton 354b3e77600SGreg Clayton StopInfoSP 355b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) { 356b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for 357b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been 358b3e77600SGreg Clayton // cached so we can return it here 359b9c1b51eSKate Stone StopInfoSP 360b9c1b51eSKate Stone stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 361b3e77600SGreg Clayton return stop_info_sp; 362b3e77600SGreg Clayton } 363b3e77600SGreg Clayton 364b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid, 365b9c1b51eSKate Stone addr_t context) { 366*a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Thread); 367a4d8747dSGreg Clayton 36863e5fb76SJonas Devlieghere LLDB_LOGF(log, 36963e5fb76SJonas Devlieghere "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 370b9c1b51eSKate Stone ", context = 0x%" PRIx64 ") fetching register data from python", 371b9c1b51eSKate Stone tid, context); 372a4d8747dSGreg Clayton 373b9c1b51eSKate Stone if (m_interpreter && m_python_object_sp) { 37485a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 37585a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the 37685a5ec51SJonas Devlieghere // process, and we're going to use python, which requires the API lock to 37785a5ec51SJonas Devlieghere // do it. We need the interpreter lock to make sure thread_info_dict stays 37885a5ec51SJonas Devlieghere // alive. 37985a5ec51SJonas Devlieghere // 38085a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid 38185a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening. 38285a5ec51SJonas Devlieghere // 38385a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on 38485a5ec51SJonas Devlieghere // the stack below us. 385a4d8747dSGreg Clayton Target &target = m_process->GetTarget(); 38685a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 38785a5ec51SJonas Devlieghere std::defer_lock); 3884d23764dSReid Kleckner (void)api_lock.try_lock(); // See above. 38985a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 390a4d8747dSGreg Clayton 391b9c1b51eSKate Stone StructuredData::DictionarySP thread_info_dict = 392b9c1b51eSKate Stone m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context); 393e98008ccSGreg Clayton std::vector<bool> core_used_map; 394b9c1b51eSKate Stone if (thread_info_dict) { 395b3ae8761SGreg Clayton ThreadList core_threads(m_process); 396a4d8747dSGreg Clayton ThreadList &thread_list = m_process->GetThreadList(); 397a4d8747dSGreg Clayton bool did_create = false; 398b9c1b51eSKate Stone ThreadSP thread_sp( 399b9c1b51eSKate Stone CreateThreadFromThreadInfo(*thread_info_dict, core_threads, 400b9c1b51eSKate Stone thread_list, core_used_map, &did_create)); 401a4d8747dSGreg Clayton if (did_create) 402a4d8747dSGreg Clayton thread_list.AddThread(thread_sp); 403a4d8747dSGreg Clayton return thread_sp; 404a4d8747dSGreg Clayton } 405a4d8747dSGreg Clayton } 406a4d8747dSGreg Clayton return ThreadSP(); 407a4d8747dSGreg Clayton } 408a4d8747dSGreg Clayton 4094e26cf2cSJonas Devlieghere #endif // #if LLDB_ENABLE_PYTHON 410