1c156427dSZachary Turner //===-- OperatingSystemPython.cpp --------------------------------*- C++-*-===// 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 9b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON 10b3e77600SGreg Clayton 11b3e77600SGreg Clayton #include "OperatingSystemPython.h" 12796ac80bSJonas Devlieghere 13b9c1b51eSKate Stone #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 14b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h" 15b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h" 16b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h" 175790759aSEnrico Granata #include "lldb/Core/Debugger.h" 18b3e77600SGreg Clayton #include "lldb/Core/Module.h" 19b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h" 20b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 215790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 220641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h" 23b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 24b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h" 25b3e77600SGreg Clayton #include "lldb/Target/Process.h" 26b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h" 27b3e77600SGreg Clayton #include "lldb/Target/Target.h" 28b3e77600SGreg Clayton #include "lldb/Target/Thread.h" 29b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h" 30666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 31d821c997SPavel Labath #include "lldb/Utility/RegisterValue.h" 32bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 33f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h" 34b3e77600SGreg Clayton 35796ac80bSJonas Devlieghere #include <memory> 36796ac80bSJonas Devlieghere 37b3e77600SGreg Clayton using namespace lldb; 38b3e77600SGreg Clayton using namespace lldb_private; 39b3e77600SGreg Clayton 40b9c1b51eSKate Stone void OperatingSystemPython::Initialize() { 41b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 42b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 43b9c1b51eSKate Stone nullptr); 44b3e77600SGreg Clayton } 45b3e77600SGreg Clayton 46b9c1b51eSKate Stone void OperatingSystemPython::Terminate() { 47b3e77600SGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 48b3e77600SGreg Clayton } 49b3e77600SGreg Clayton 50b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process, 51b9c1b51eSKate Stone bool force) { 5205097246SAdrian Prantl // Python OperatingSystem plug-ins must be requested by name, so force must 5305097246SAdrian Prantl // be true 54c9d645d3SGreg Clayton FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath()); 55dbd7fabaSJonas Devlieghere if (python_os_plugin_spec && 56dbd7fabaSJonas Devlieghere FileSystem::Instance().Exists(python_os_plugin_spec)) { 57d5b44036SJonas Devlieghere std::unique_ptr<OperatingSystemPython> os_up( 58b9c1b51eSKate Stone new OperatingSystemPython(process, python_os_plugin_spec)); 59d5b44036SJonas Devlieghere if (os_up.get() && os_up->IsValid()) 60d5b44036SJonas Devlieghere return os_up.release(); 61c9d645d3SGreg Clayton } 62248a1305SKonrad Kleine return nullptr; 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) 77d5b44036SJonas Devlieghere : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(), 78248a1305SKonrad Kleine m_interpreter(nullptr), 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; 842b29b432SJonas Devlieghere m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 85b9c1b51eSKate Stone if (m_interpreter) { 862443cbd7SGreg Clayton 87b9c1b51eSKate Stone std::string os_plugin_class_name( 88b9c1b51eSKate Stone python_module_path.GetFilename().AsCString("")); 89b9c1b51eSKate Stone if (!os_plugin_class_name.empty()) { 90c9d645d3SGreg Clayton const bool init_session = false; 91c9d645d3SGreg Clayton const bool allow_reload = true; 92c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX]; 93b9c1b51eSKate Stone python_module_path.GetPath(python_module_path_cstr, 94b9c1b51eSKate Stone sizeof(python_module_path_cstr)); 9597206d57SZachary Turner Status error; 96b9c1b51eSKate Stone if (m_interpreter->LoadScriptingModule( 97b9c1b51eSKate Stone python_module_path_cstr, allow_reload, init_session, error)) { 98c9d645d3SGreg Clayton // Strip the ".py" extension if there is one 99c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 100c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos) 101c9d645d3SGreg Clayton os_plugin_class_name.erase(py_extension_pos); 102b9c1b51eSKate Stone // Add ".OperatingSystemPlugIn" to the module name to get a string like 103b9c1b51eSKate Stone // "modulename.OperatingSystemPlugIn" 104c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn"; 1050641ca1aSZachary Turner StructuredData::ObjectSP object_sp = 106b9c1b51eSKate Stone m_interpreter->OSPlugin_CreatePluginObject( 107b9c1b51eSKate Stone os_plugin_class_name.c_str(), process->CalculateProcess()); 1080641ca1aSZachary Turner if (object_sp && object_sp->IsValid()) 109a4d8747dSGreg Clayton m_python_object_sp = object_sp; 110c9d645d3SGreg Clayton } 1112443cbd7SGreg Clayton } 1125790759aSEnrico Granata } 113b3e77600SGreg Clayton } 114b3e77600SGreg Clayton 115b9c1b51eSKate Stone OperatingSystemPython::~OperatingSystemPython() {} 116b3e77600SGreg Clayton 117b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() { 118248a1305SKonrad Kleine if (m_register_info_up == nullptr) { 119a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 120248a1305SKonrad Kleine return nullptr; 12162f80036SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS)); 1221c22be69SGreg Clayton 123*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 124*63e5fb76SJonas Devlieghere "OperatingSystemPython::GetDynamicRegisterInfo() fetching " 125b9c1b51eSKate Stone "thread register definitions from python for pid %" PRIu64, 126b9c1b51eSKate Stone m_process->GetID()); 1271c22be69SGreg Clayton 128b9c1b51eSKate Stone StructuredData::DictionarySP dictionary = 129b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp); 1305790759aSEnrico Granata if (!dictionary) 131248a1305SKonrad Kleine return nullptr; 132b3e77600SGreg Clayton 133d5b44036SJonas Devlieghere m_register_info_up.reset(new DynamicRegisterInfo( 134b9c1b51eSKate Stone *dictionary, m_process->GetTarget().GetArchitecture())); 135d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisters() > 0); 136d5b44036SJonas Devlieghere assert(m_register_info_up->GetNumRegisterSets() > 0); 137b3e77600SGreg Clayton } 138d5b44036SJonas Devlieghere return m_register_info_up.get(); 139b3e77600SGreg Clayton } 140b3e77600SGreg Clayton 141b3e77600SGreg Clayton // PluginInterface protocol 142b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginName() { 143b3e77600SGreg Clayton return GetPluginNameStatic(); 144b3e77600SGreg Clayton } 145b3e77600SGreg Clayton 146b9c1b51eSKate Stone uint32_t OperatingSystemPython::GetPluginVersion() { return 1; } 147b3e77600SGreg Clayton 148b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list, 149ba4e61d3SAndrew Kaylor ThreadList &core_thread_list, 150b9c1b51eSKate Stone ThreadList &new_thread_list) { 151a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 152a85e6b6cSDaniel Malea return false; 1531c22be69SGreg Clayton 15462f80036SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS)); 1551c22be69SGreg Clayton 15685a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 15785a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process, 15885a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We 15985a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive. 160ab745c2aSGreg Clayton // 161ab745c2aSGreg Clayton // If someone already has the API lock, that is ok, we just want to avoid 162ab745c2aSGreg Clayton // external code from making new API calls while this call is happening. 163ab745c2aSGreg Clayton // 164ab745c2aSGreg Clayton // This is a recursive lock so we can grant it to any Python code called on 165ab745c2aSGreg Clayton // the stack below us. 16685e276b8SJim Ingham Target &target = m_process->GetTarget(); 16785a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 168b9c1b51eSKate Stone std::defer_lock); 16985a5ec51SJonas Devlieghere api_lock.try_lock(); 17085a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 17185e276b8SJim Ingham 172*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 173*63e5fb76SJonas Devlieghere "OperatingSystemPython::UpdateThreadList() fetching thread " 174b9c1b51eSKate Stone "data from python for pid %" PRIu64, 175b9c1b51eSKate Stone m_process->GetID()); 1761c22be69SGreg Clayton 177b9c1b51eSKate Stone // The threads that are in "new_thread_list" upon entry are the threads from 17885a5ec51SJonas Devlieghere // the lldb_private::Process subclass, no memory threads will be in this 17985a5ec51SJonas Devlieghere // list. 180b9c1b51eSKate Stone StructuredData::ArraySP threads_list = 181b9c1b51eSKate Stone m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp); 182e98008ccSGreg Clayton 183e98008ccSGreg Clayton const uint32_t num_cores = core_thread_list.GetSize(false); 184e98008ccSGreg Clayton 185e98008ccSGreg Clayton // Make a map so we can keep track of which cores were used from the 18605097246SAdrian Prantl // core_thread list. Any real threads/cores that weren't used should later be 18705097246SAdrian Prantl // put back into the "new_thread_list". 188e98008ccSGreg Clayton std::vector<bool> core_used_map(num_cores, false); 189b9c1b51eSKate Stone if (threads_list) { 190b9c1b51eSKate Stone if (log) { 19162f80036SGreg Clayton StreamString strm; 1920641ca1aSZachary Turner threads_list->Dump(strm); 193*63e5fb76SJonas Devlieghere LLDB_LOGF(log, "threads_list = %s", strm.GetData()); 19462f80036SGreg Clayton } 1950641ca1aSZachary Turner 1960641ca1aSZachary Turner const uint32_t num_threads = threads_list->GetSize(); 197b9c1b51eSKate Stone for (uint32_t i = 0; i < num_threads; ++i) { 198b9c1b51eSKate Stone StructuredData::ObjectSP thread_dict_obj = 199b9c1b51eSKate Stone threads_list->GetItemAtIndex(i); 200b9c1b51eSKate Stone if (auto thread_dict = thread_dict_obj->GetAsDictionary()) { 201248a1305SKonrad Kleine ThreadSP thread_sp(CreateThreadFromThreadInfo( 202248a1305SKonrad Kleine *thread_dict, core_thread_list, old_thread_list, core_used_map, 203248a1305SKonrad Kleine nullptr)); 204a4d8747dSGreg Clayton if (thread_sp) 205435ce139SGreg Clayton new_thread_list.AddThread(thread_sp); 206435ce139SGreg Clayton } 207435ce139SGreg Clayton } 208435ce139SGreg Clayton } 2096e0ff1a3SGreg Clayton 210e98008ccSGreg Clayton // Any real core threads that didn't end up backing a memory thread should 211b9c1b51eSKate Stone // still be in the main thread list, and they should be inserted at the 21205097246SAdrian Prantl // beginning of the list 213e98008ccSGreg Clayton uint32_t insert_idx = 0; 214b9c1b51eSKate Stone for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) { 215a6682a41SJonas Devlieghere if (!core_used_map[core_idx]) { 216b9c1b51eSKate Stone new_thread_list.InsertThread( 217b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx); 218e98008ccSGreg Clayton ++insert_idx; 219e98008ccSGreg Clayton } 220e98008ccSGreg Clayton } 2216e0ff1a3SGreg Clayton 222b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0; 223b3e77600SGreg Clayton } 224b3e77600SGreg Clayton 225b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo( 226b9c1b51eSKate Stone StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list, 227b9c1b51eSKate Stone ThreadList &old_thread_list, std::vector<bool> &core_used_map, 228b9c1b51eSKate Stone bool *did_create_ptr) { 229a4d8747dSGreg Clayton ThreadSP thread_sp; 2300641ca1aSZachary Turner tid_t tid = LLDB_INVALID_THREAD_ID; 2310641ca1aSZachary Turner if (!thread_dict.GetValueForKeyAsInteger("tid", tid)) 2320641ca1aSZachary Turner return ThreadSP(); 233a4d8747dSGreg Clayton 2340641ca1aSZachary Turner uint32_t core_number; 2350641ca1aSZachary Turner addr_t reg_data_addr; 2362833321fSZachary Turner llvm::StringRef name; 2372833321fSZachary Turner llvm::StringRef queue; 2380641ca1aSZachary Turner 2390641ca1aSZachary Turner thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX); 240b9c1b51eSKate Stone thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, 241b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 2420641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("name", name); 2430641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("queue", queue); 244a4d8747dSGreg Clayton 245160c9d81SGreg Clayton // See if a thread already exists for "tid" 246b3ae8761SGreg Clayton thread_sp = old_thread_list.FindThreadByID(tid, false); 247b9c1b51eSKate Stone if (thread_sp) { 248b9c1b51eSKate Stone // A thread already does exist for "tid", make sure it was an operating 249b9c1b51eSKate Stone // system 250160c9d81SGreg Clayton // plug-in generated thread. 251b9c1b51eSKate Stone if (!IsOperatingSystemPluginThread(thread_sp)) { 252160c9d81SGreg Clayton // We have thread ID overlap between the protocol threads and the 25305097246SAdrian Prantl // operating system threads, clear the thread so we create an operating 25405097246SAdrian Prantl // system thread for this. 255160c9d81SGreg Clayton thread_sp.reset(); 256160c9d81SGreg Clayton } 257160c9d81SGreg Clayton } 258160c9d81SGreg Clayton 259b9c1b51eSKate Stone if (!thread_sp) { 260a4d8747dSGreg Clayton if (did_create_ptr) 261a4d8747dSGreg Clayton *did_create_ptr = true; 262796ac80bSJonas Devlieghere thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue, 263796ac80bSJonas Devlieghere reg_data_addr); 264b3ae8761SGreg Clayton } 265b3ae8761SGreg Clayton 266b9c1b51eSKate Stone if (core_number < core_thread_list.GetSize(false)) { 267b9c1b51eSKate Stone ThreadSP core_thread_sp( 268b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_number, false)); 269b9c1b51eSKate Stone if (core_thread_sp) { 270b9c1b51eSKate Stone // Keep track of which cores were set as the backing thread for memory 271b9c1b51eSKate Stone // threads... 272e98008ccSGreg Clayton if (core_number < core_used_map.size()) 273e98008ccSGreg Clayton core_used_map[core_number] = true; 274e98008ccSGreg Clayton 275160c9d81SGreg Clayton ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread()); 276b9c1b51eSKate Stone if (backing_core_thread_sp) { 277160c9d81SGreg Clayton thread_sp->SetBackingThread(backing_core_thread_sp); 278b9c1b51eSKate Stone } else { 279160c9d81SGreg Clayton thread_sp->SetBackingThread(core_thread_sp); 280160c9d81SGreg Clayton } 281160c9d81SGreg Clayton } 282a4d8747dSGreg Clayton } 283a4d8747dSGreg Clayton return thread_sp; 284a4d8747dSGreg Clayton } 285a4d8747dSGreg Clayton 286b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {} 287b3e77600SGreg Clayton 288b3e77600SGreg Clayton RegisterContextSP 289b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread, 290b9c1b51eSKate Stone addr_t reg_data_addr) { 291435ce139SGreg Clayton RegisterContextSP reg_ctx_sp; 292a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp || !thread) 293160c9d81SGreg Clayton return reg_ctx_sp; 294160c9d81SGreg Clayton 295160c9d81SGreg Clayton if (!IsOperatingSystemPluginThread(thread->shared_from_this())) 296160c9d81SGreg Clayton return reg_ctx_sp; 2971c22be69SGreg Clayton 29885a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 29985a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the process, 30085a5ec51SJonas Devlieghere // and we're going to use python, which requires the API lock to do it. We 30185a5ec51SJonas Devlieghere // need the interpreter lock to make sure thread_info_dict stays alive. 30285a5ec51SJonas Devlieghere // 30385a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid 30485a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening. 30585a5ec51SJonas Devlieghere // 30685a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on 30785a5ec51SJonas Devlieghere // the stack below us. 30885e276b8SJim Ingham Target &target = m_process->GetTarget(); 30985a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 31085a5ec51SJonas Devlieghere std::defer_lock); 31185a5ec51SJonas Devlieghere api_lock.try_lock(); 31285a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 31385e276b8SJim Ingham 3145160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 3151c22be69SGreg Clayton 316b9c1b51eSKate Stone if (reg_data_addr != LLDB_INVALID_ADDRESS) { 317ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register 318ead45e01SGreg Clayton // context using the address provided 319*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 320*63e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 321b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 322b9c1b51eSKate Stone ") creating memory register context", 323b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID(), reg_data_addr); 324796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextMemory>( 325796ac80bSJonas Devlieghere *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr); 326b9c1b51eSKate Stone } else { 32705097246SAdrian Prantl // No register data address is provided, query the python plug-in to let it 32805097246SAdrian Prantl // make up the data as it sees fit 329*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 330*63e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 331b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 332b9c1b51eSKate Stone ") fetching register data from python", 333b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID()); 3341c22be69SGreg Clayton 335b9c1b51eSKate Stone StructuredData::StringSP reg_context_data = 336b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, 337b9c1b51eSKate Stone thread->GetID()); 338b9c1b51eSKate Stone if (reg_context_data) { 3390641ca1aSZachary Turner std::string value = reg_context_data->GetValue(); 3400641ca1aSZachary Turner DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length())); 341b9c1b51eSKate Stone if (data_sp->GetByteSize()) { 342b9c1b51eSKate Stone RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory( 343b9c1b51eSKate Stone *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); 344b9c1b51eSKate Stone if (reg_ctx_memory) { 345435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory); 346435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData(data_sp); 347435ce139SGreg Clayton } 348435ce139SGreg Clayton } 349435ce139SGreg Clayton } 350ead45e01SGreg Clayton } 351b9c1b51eSKate Stone // if we still have no register data, fallback on a dummy context to avoid 352b9c1b51eSKate Stone // crashing 353b9c1b51eSKate Stone if (!reg_ctx_sp) { 354*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 355*63e5fb76SJonas Devlieghere "OperatingSystemPython::CreateRegisterContextForThread (tid " 356b9c1b51eSKate Stone "= 0x%" PRIx64 ") forcing a dummy register context", 357b9c1b51eSKate Stone thread->GetID()); 358796ac80bSJonas Devlieghere reg_ctx_sp = std::make_shared<RegisterContextDummy>( 359796ac80bSJonas Devlieghere *thread, 0, target.GetArchitecture().GetAddressByteSize()); 360cbd79b6cSEnrico Granata } 361b3e77600SGreg Clayton return reg_ctx_sp; 362b3e77600SGreg Clayton } 363b3e77600SGreg Clayton 364b3e77600SGreg Clayton StopInfoSP 365b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) { 366b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for 367b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been 368b3e77600SGreg Clayton // cached so we can return it here 369b9c1b51eSKate Stone StopInfoSP 370b9c1b51eSKate Stone stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 371b3e77600SGreg Clayton return stop_info_sp; 372b3e77600SGreg Clayton } 373b3e77600SGreg Clayton 374b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid, 375b9c1b51eSKate Stone addr_t context) { 3765160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 377a4d8747dSGreg Clayton 378*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 379*63e5fb76SJonas Devlieghere "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 380b9c1b51eSKate Stone ", context = 0x%" PRIx64 ") fetching register data from python", 381b9c1b51eSKate Stone tid, context); 382a4d8747dSGreg Clayton 383b9c1b51eSKate Stone if (m_interpreter && m_python_object_sp) { 38485a5ec51SJonas Devlieghere // First thing we have to do is to try to get the API lock, and the 38585a5ec51SJonas Devlieghere // interpreter lock. We're going to change the thread content of the 38685a5ec51SJonas Devlieghere // process, and we're going to use python, which requires the API lock to 38785a5ec51SJonas Devlieghere // do it. We need the interpreter lock to make sure thread_info_dict stays 38885a5ec51SJonas Devlieghere // alive. 38985a5ec51SJonas Devlieghere // 39085a5ec51SJonas Devlieghere // If someone already has the API lock, that is ok, we just want to avoid 39185a5ec51SJonas Devlieghere // external code from making new API calls while this call is happening. 39285a5ec51SJonas Devlieghere // 39385a5ec51SJonas Devlieghere // This is a recursive lock so we can grant it to any Python code called on 39485a5ec51SJonas Devlieghere // the stack below us. 395a4d8747dSGreg Clayton Target &target = m_process->GetTarget(); 39685a5ec51SJonas Devlieghere std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 39785a5ec51SJonas Devlieghere std::defer_lock); 39885a5ec51SJonas Devlieghere api_lock.try_lock(); 39985a5ec51SJonas Devlieghere auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 400a4d8747dSGreg Clayton 401b9c1b51eSKate Stone StructuredData::DictionarySP thread_info_dict = 402b9c1b51eSKate Stone m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context); 403e98008ccSGreg Clayton std::vector<bool> core_used_map; 404b9c1b51eSKate Stone if (thread_info_dict) { 405b3ae8761SGreg Clayton ThreadList core_threads(m_process); 406a4d8747dSGreg Clayton ThreadList &thread_list = m_process->GetThreadList(); 407a4d8747dSGreg Clayton bool did_create = false; 408b9c1b51eSKate Stone ThreadSP thread_sp( 409b9c1b51eSKate Stone CreateThreadFromThreadInfo(*thread_info_dict, core_threads, 410b9c1b51eSKate Stone thread_list, core_used_map, &did_create)); 411a4d8747dSGreg Clayton if (did_create) 412a4d8747dSGreg Clayton thread_list.AddThread(thread_sp); 413a4d8747dSGreg Clayton return thread_sp; 414a4d8747dSGreg Clayton } 415a4d8747dSGreg Clayton } 416a4d8747dSGreg Clayton return ThreadSP(); 417a4d8747dSGreg Clayton } 418a4d8747dSGreg Clayton 419b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON 420