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