15ffd83dbSDimitry Andric //===-- OperatingSystemPython.cpp -----------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9480093f4SDimitry Andric #include "lldb/Host/Config.h" 10480093f4SDimitry Andric 11480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "OperatingSystemPython.h" 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextDummy.h" 16*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextMemory.h" 17*0b57cec5SDimitry Andric #include "Plugins/Process/Utility/ThreadMemory.h" 18*0b57cec5SDimitry Andric #include "lldb/Core/Debugger.h" 19*0b57cec5SDimitry Andric #include "lldb/Core/Module.h" 20*0b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h" 21*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h" 22*0b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h" 23*0b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h" 24*0b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h" 25*0b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h" 26*0b57cec5SDimitry Andric #include "lldb/Target/Process.h" 27*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h" 28*0b57cec5SDimitry Andric #include "lldb/Target/Target.h" 29*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h" 30*0b57cec5SDimitry Andric #include "lldb/Target/ThreadList.h" 31*0b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h" 3281ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h" 33*0b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h" 34*0b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h" 35*0b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h" 36*0b57cec5SDimitry Andric 37*0b57cec5SDimitry Andric #include <memory> 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric using namespace lldb; 40*0b57cec5SDimitry Andric using namespace lldb_private; 41*0b57cec5SDimitry Andric 425ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(OperatingSystemPython) 435ffd83dbSDimitry Andric 44*0b57cec5SDimitry Andric void OperatingSystemPython::Initialize() { 45*0b57cec5SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(), 46*0b57cec5SDimitry Andric GetPluginDescriptionStatic(), CreateInstance, 47*0b57cec5SDimitry Andric nullptr); 48*0b57cec5SDimitry Andric } 49*0b57cec5SDimitry Andric 50*0b57cec5SDimitry Andric void OperatingSystemPython::Terminate() { 51*0b57cec5SDimitry Andric PluginManager::UnregisterPlugin(CreateInstance); 52*0b57cec5SDimitry Andric } 53*0b57cec5SDimitry Andric 54*0b57cec5SDimitry Andric OperatingSystem *OperatingSystemPython::CreateInstance(Process *process, 55*0b57cec5SDimitry Andric bool force) { 56*0b57cec5SDimitry Andric // Python OperatingSystem plug-ins must be requested by name, so force must 57*0b57cec5SDimitry Andric // be true 58*0b57cec5SDimitry Andric FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath()); 59*0b57cec5SDimitry Andric if (python_os_plugin_spec && 60*0b57cec5SDimitry Andric FileSystem::Instance().Exists(python_os_plugin_spec)) { 61*0b57cec5SDimitry Andric std::unique_ptr<OperatingSystemPython> os_up( 62*0b57cec5SDimitry Andric new OperatingSystemPython(process, python_os_plugin_spec)); 63*0b57cec5SDimitry Andric if (os_up.get() && os_up->IsValid()) 64*0b57cec5SDimitry Andric return os_up.release(); 65*0b57cec5SDimitry Andric } 66*0b57cec5SDimitry Andric return nullptr; 67*0b57cec5SDimitry Andric } 68*0b57cec5SDimitry Andric 69349cc55cSDimitry Andric llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() { 70*0b57cec5SDimitry Andric return "Operating system plug-in that gathers OS information from a python " 71*0b57cec5SDimitry Andric "class that implements the necessary OperatingSystem functionality."; 72*0b57cec5SDimitry Andric } 73*0b57cec5SDimitry Andric 74*0b57cec5SDimitry Andric OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process, 75*0b57cec5SDimitry Andric const FileSpec &python_module_path) 76*0b57cec5SDimitry Andric : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(), 77*0b57cec5SDimitry Andric m_interpreter(nullptr), m_python_object_sp() { 78*0b57cec5SDimitry Andric if (!process) 79*0b57cec5SDimitry Andric return; 80*0b57cec5SDimitry Andric TargetSP target_sp = process->CalculateTarget(); 81*0b57cec5SDimitry Andric if (!target_sp) 82*0b57cec5SDimitry Andric return; 83*0b57cec5SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter(); 84*0b57cec5SDimitry Andric if (m_interpreter) { 85*0b57cec5SDimitry Andric 86*0b57cec5SDimitry Andric std::string os_plugin_class_name( 87*0b57cec5SDimitry Andric python_module_path.GetFilename().AsCString("")); 88*0b57cec5SDimitry Andric if (!os_plugin_class_name.empty()) { 89fe6060f1SDimitry Andric LoadScriptOptions options; 90*0b57cec5SDimitry Andric char python_module_path_cstr[PATH_MAX]; 91*0b57cec5SDimitry Andric python_module_path.GetPath(python_module_path_cstr, 92*0b57cec5SDimitry Andric sizeof(python_module_path_cstr)); 93*0b57cec5SDimitry Andric Status error; 94fe6060f1SDimitry Andric if (m_interpreter->LoadScriptingModule(python_module_path_cstr, options, 95fe6060f1SDimitry Andric error)) { 96*0b57cec5SDimitry Andric // Strip the ".py" extension if there is one 97*0b57cec5SDimitry Andric size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 98*0b57cec5SDimitry Andric if (py_extension_pos != std::string::npos) 99*0b57cec5SDimitry Andric os_plugin_class_name.erase(py_extension_pos); 100*0b57cec5SDimitry Andric // Add ".OperatingSystemPlugIn" to the module name to get a string like 101*0b57cec5SDimitry Andric // "modulename.OperatingSystemPlugIn" 102*0b57cec5SDimitry Andric os_plugin_class_name += ".OperatingSystemPlugIn"; 103*0b57cec5SDimitry Andric StructuredData::ObjectSP object_sp = 104*0b57cec5SDimitry Andric m_interpreter->OSPlugin_CreatePluginObject( 105*0b57cec5SDimitry Andric os_plugin_class_name.c_str(), process->CalculateProcess()); 106*0b57cec5SDimitry Andric if (object_sp && object_sp->IsValid()) 107*0b57cec5SDimitry Andric m_python_object_sp = object_sp; 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric } 110*0b57cec5SDimitry Andric } 111*0b57cec5SDimitry Andric } 112*0b57cec5SDimitry Andric 113fe6060f1SDimitry Andric OperatingSystemPython::~OperatingSystemPython() = default; 114*0b57cec5SDimitry Andric 115*0b57cec5SDimitry Andric DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() { 116*0b57cec5SDimitry Andric if (m_register_info_up == nullptr) { 117*0b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp) 118*0b57cec5SDimitry Andric return nullptr; 11981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS); 120*0b57cec5SDimitry Andric 1219dba64beSDimitry Andric LLDB_LOGF(log, 1229dba64beSDimitry Andric "OperatingSystemPython::GetDynamicRegisterInfo() fetching " 123*0b57cec5SDimitry Andric "thread register definitions from python for pid %" PRIu64, 124*0b57cec5SDimitry Andric m_process->GetID()); 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric StructuredData::DictionarySP dictionary = 127*0b57cec5SDimitry Andric m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp); 128*0b57cec5SDimitry Andric if (!dictionary) 129*0b57cec5SDimitry Andric return nullptr; 130*0b57cec5SDimitry Andric 131e8d8bef9SDimitry Andric m_register_info_up = std::make_unique<DynamicRegisterInfo>( 132e8d8bef9SDimitry Andric *dictionary, m_process->GetTarget().GetArchitecture()); 133*0b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisters() > 0); 134*0b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisterSets() > 0); 135*0b57cec5SDimitry Andric } 136*0b57cec5SDimitry Andric return m_register_info_up.get(); 137*0b57cec5SDimitry Andric } 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list, 140*0b57cec5SDimitry Andric ThreadList &core_thread_list, 141*0b57cec5SDimitry Andric ThreadList &new_thread_list) { 142*0b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp) 143*0b57cec5SDimitry Andric return false; 144*0b57cec5SDimitry Andric 14581ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS); 146*0b57cec5SDimitry Andric 147*0b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 148*0b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the process, 149*0b57cec5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We 150*0b57cec5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive. 151*0b57cec5SDimitry Andric // 152*0b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 153*0b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 154*0b57cec5SDimitry Andric // 155*0b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 156*0b57cec5SDimitry Andric // the stack below us. 157*0b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 158*0b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 159*0b57cec5SDimitry Andric std::defer_lock); 160480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 161*0b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 162*0b57cec5SDimitry Andric 1639dba64beSDimitry Andric LLDB_LOGF(log, 1649dba64beSDimitry Andric "OperatingSystemPython::UpdateThreadList() fetching thread " 165*0b57cec5SDimitry Andric "data from python for pid %" PRIu64, 166*0b57cec5SDimitry Andric m_process->GetID()); 167*0b57cec5SDimitry Andric 1689dba64beSDimitry Andric // The threads that are in "core_thread_list" upon entry are the threads from 169*0b57cec5SDimitry Andric // the lldb_private::Process subclass, no memory threads will be in this 170*0b57cec5SDimitry Andric // list. 171*0b57cec5SDimitry Andric StructuredData::ArraySP threads_list = 172*0b57cec5SDimitry Andric m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp); 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric const uint32_t num_cores = core_thread_list.GetSize(false); 175*0b57cec5SDimitry Andric 176*0b57cec5SDimitry Andric // Make a map so we can keep track of which cores were used from the 177*0b57cec5SDimitry Andric // core_thread list. Any real threads/cores that weren't used should later be 178*0b57cec5SDimitry Andric // put back into the "new_thread_list". 179*0b57cec5SDimitry Andric std::vector<bool> core_used_map(num_cores, false); 180*0b57cec5SDimitry Andric if (threads_list) { 181*0b57cec5SDimitry Andric if (log) { 182*0b57cec5SDimitry Andric StreamString strm; 183*0b57cec5SDimitry Andric threads_list->Dump(strm); 1849dba64beSDimitry Andric LLDB_LOGF(log, "threads_list = %s", strm.GetData()); 185*0b57cec5SDimitry Andric } 186*0b57cec5SDimitry Andric 187*0b57cec5SDimitry Andric const uint32_t num_threads = threads_list->GetSize(); 188*0b57cec5SDimitry Andric for (uint32_t i = 0; i < num_threads; ++i) { 189*0b57cec5SDimitry Andric StructuredData::ObjectSP thread_dict_obj = 190*0b57cec5SDimitry Andric threads_list->GetItemAtIndex(i); 191*0b57cec5SDimitry Andric if (auto thread_dict = thread_dict_obj->GetAsDictionary()) { 192*0b57cec5SDimitry Andric ThreadSP thread_sp(CreateThreadFromThreadInfo( 193*0b57cec5SDimitry Andric *thread_dict, core_thread_list, old_thread_list, core_used_map, 194*0b57cec5SDimitry Andric nullptr)); 195*0b57cec5SDimitry Andric if (thread_sp) 196*0b57cec5SDimitry Andric new_thread_list.AddThread(thread_sp); 197*0b57cec5SDimitry Andric } 198*0b57cec5SDimitry Andric } 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric // Any real core threads that didn't end up backing a memory thread should 202*0b57cec5SDimitry Andric // still be in the main thread list, and they should be inserted at the 203*0b57cec5SDimitry Andric // beginning of the list 204*0b57cec5SDimitry Andric uint32_t insert_idx = 0; 205*0b57cec5SDimitry Andric for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) { 206*0b57cec5SDimitry Andric if (!core_used_map[core_idx]) { 207*0b57cec5SDimitry Andric new_thread_list.InsertThread( 208*0b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx); 209*0b57cec5SDimitry Andric ++insert_idx; 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric } 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric return new_thread_list.GetSize(false) > 0; 214*0b57cec5SDimitry Andric } 215*0b57cec5SDimitry Andric 216*0b57cec5SDimitry Andric ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo( 217*0b57cec5SDimitry Andric StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list, 218*0b57cec5SDimitry Andric ThreadList &old_thread_list, std::vector<bool> &core_used_map, 219*0b57cec5SDimitry Andric bool *did_create_ptr) { 220*0b57cec5SDimitry Andric ThreadSP thread_sp; 221*0b57cec5SDimitry Andric tid_t tid = LLDB_INVALID_THREAD_ID; 222*0b57cec5SDimitry Andric if (!thread_dict.GetValueForKeyAsInteger("tid", tid)) 223*0b57cec5SDimitry Andric return ThreadSP(); 224*0b57cec5SDimitry Andric 225*0b57cec5SDimitry Andric uint32_t core_number; 226*0b57cec5SDimitry Andric addr_t reg_data_addr; 227*0b57cec5SDimitry Andric llvm::StringRef name; 228*0b57cec5SDimitry Andric llvm::StringRef queue; 229*0b57cec5SDimitry Andric 230*0b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX); 231*0b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, 232*0b57cec5SDimitry Andric LLDB_INVALID_ADDRESS); 233*0b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("name", name); 234*0b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("queue", queue); 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric // See if a thread already exists for "tid" 237*0b57cec5SDimitry Andric thread_sp = old_thread_list.FindThreadByID(tid, false); 238*0b57cec5SDimitry Andric if (thread_sp) { 239*0b57cec5SDimitry Andric // A thread already does exist for "tid", make sure it was an operating 240*0b57cec5SDimitry Andric // system 241*0b57cec5SDimitry Andric // plug-in generated thread. 242*0b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread_sp)) { 243*0b57cec5SDimitry Andric // We have thread ID overlap between the protocol threads and the 244*0b57cec5SDimitry Andric // operating system threads, clear the thread so we create an operating 245*0b57cec5SDimitry Andric // system thread for this. 246*0b57cec5SDimitry Andric thread_sp.reset(); 247*0b57cec5SDimitry Andric } 248*0b57cec5SDimitry Andric } 249*0b57cec5SDimitry Andric 250*0b57cec5SDimitry Andric if (!thread_sp) { 251*0b57cec5SDimitry Andric if (did_create_ptr) 252*0b57cec5SDimitry Andric *did_create_ptr = true; 253*0b57cec5SDimitry Andric thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue, 254*0b57cec5SDimitry Andric reg_data_addr); 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric if (core_number < core_thread_list.GetSize(false)) { 258*0b57cec5SDimitry Andric ThreadSP core_thread_sp( 259*0b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_number, false)); 260*0b57cec5SDimitry Andric if (core_thread_sp) { 261*0b57cec5SDimitry Andric // Keep track of which cores were set as the backing thread for memory 262*0b57cec5SDimitry Andric // threads... 263*0b57cec5SDimitry Andric if (core_number < core_used_map.size()) 264*0b57cec5SDimitry Andric core_used_map[core_number] = true; 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread()); 267*0b57cec5SDimitry Andric if (backing_core_thread_sp) { 268*0b57cec5SDimitry Andric thread_sp->SetBackingThread(backing_core_thread_sp); 269*0b57cec5SDimitry Andric } else { 270*0b57cec5SDimitry Andric thread_sp->SetBackingThread(core_thread_sp); 271*0b57cec5SDimitry Andric } 272*0b57cec5SDimitry Andric } 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric return thread_sp; 275*0b57cec5SDimitry Andric } 276*0b57cec5SDimitry Andric 277*0b57cec5SDimitry Andric void OperatingSystemPython::ThreadWasSelected(Thread *thread) {} 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric RegisterContextSP 280*0b57cec5SDimitry Andric OperatingSystemPython::CreateRegisterContextForThread(Thread *thread, 281*0b57cec5SDimitry Andric addr_t reg_data_addr) { 282*0b57cec5SDimitry Andric RegisterContextSP reg_ctx_sp; 283*0b57cec5SDimitry Andric if (!m_interpreter || !m_python_object_sp || !thread) 284*0b57cec5SDimitry Andric return reg_ctx_sp; 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread->shared_from_this())) 287*0b57cec5SDimitry Andric return reg_ctx_sp; 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 290*0b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the process, 291*0b57cec5SDimitry Andric // and we're going to use python, which requires the API lock to do it. We 292*0b57cec5SDimitry Andric // need the interpreter lock to make sure thread_info_dict stays alive. 293*0b57cec5SDimitry Andric // 294*0b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 295*0b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 296*0b57cec5SDimitry Andric // 297*0b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 298*0b57cec5SDimitry Andric // the stack below us. 299*0b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 300*0b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 301*0b57cec5SDimitry Andric std::defer_lock); 302480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 303*0b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 304*0b57cec5SDimitry Andric 30581ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread); 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric if (reg_data_addr != LLDB_INVALID_ADDRESS) { 308*0b57cec5SDimitry Andric // The registers data is in contiguous memory, just create the register 309*0b57cec5SDimitry Andric // context using the address provided 3109dba64beSDimitry Andric LLDB_LOGF(log, 3119dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 312*0b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 313*0b57cec5SDimitry Andric ") creating memory register context", 314*0b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID(), reg_data_addr); 315*0b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextMemory>( 316*0b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr); 317*0b57cec5SDimitry Andric } else { 318*0b57cec5SDimitry Andric // No register data address is provided, query the python plug-in to let it 319*0b57cec5SDimitry Andric // make up the data as it sees fit 3209dba64beSDimitry Andric LLDB_LOGF(log, 3219dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 322*0b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 323*0b57cec5SDimitry Andric ") fetching register data from python", 324*0b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID()); 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric StructuredData::StringSP reg_context_data = 327*0b57cec5SDimitry Andric m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, 328*0b57cec5SDimitry Andric thread->GetID()); 329*0b57cec5SDimitry Andric if (reg_context_data) { 3305ffd83dbSDimitry Andric std::string value = std::string(reg_context_data->GetValue()); 331*0b57cec5SDimitry Andric DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length())); 332*0b57cec5SDimitry Andric if (data_sp->GetByteSize()) { 333*0b57cec5SDimitry Andric RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory( 334*0b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); 335*0b57cec5SDimitry Andric if (reg_ctx_memory) { 336*0b57cec5SDimitry Andric reg_ctx_sp.reset(reg_ctx_memory); 337*0b57cec5SDimitry Andric reg_ctx_memory->SetAllRegisterData(data_sp); 338*0b57cec5SDimitry Andric } 339*0b57cec5SDimitry Andric } 340*0b57cec5SDimitry Andric } 341*0b57cec5SDimitry Andric } 342*0b57cec5SDimitry Andric // if we still have no register data, fallback on a dummy context to avoid 343*0b57cec5SDimitry Andric // crashing 344*0b57cec5SDimitry Andric if (!reg_ctx_sp) { 3459dba64beSDimitry Andric LLDB_LOGF(log, 3469dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid " 347*0b57cec5SDimitry Andric "= 0x%" PRIx64 ") forcing a dummy register context", 348*0b57cec5SDimitry Andric thread->GetID()); 349*0b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextDummy>( 350*0b57cec5SDimitry Andric *thread, 0, target.GetArchitecture().GetAddressByteSize()); 351*0b57cec5SDimitry Andric } 352*0b57cec5SDimitry Andric return reg_ctx_sp; 353*0b57cec5SDimitry Andric } 354*0b57cec5SDimitry Andric 355*0b57cec5SDimitry Andric StopInfoSP 356*0b57cec5SDimitry Andric OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) { 357*0b57cec5SDimitry Andric // We should have gotten the thread stop info from the dictionary of data for 358*0b57cec5SDimitry Andric // the thread in the initial call to get_thread_info(), this should have been 359*0b57cec5SDimitry Andric // cached so we can return it here 360*0b57cec5SDimitry Andric StopInfoSP 361*0b57cec5SDimitry Andric stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 362*0b57cec5SDimitry Andric return stop_info_sp; 363*0b57cec5SDimitry Andric } 364*0b57cec5SDimitry Andric 365*0b57cec5SDimitry Andric lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid, 366*0b57cec5SDimitry Andric addr_t context) { 36781ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread); 368*0b57cec5SDimitry Andric 3699dba64beSDimitry Andric LLDB_LOGF(log, 3709dba64beSDimitry Andric "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 371*0b57cec5SDimitry Andric ", context = 0x%" PRIx64 ") fetching register data from python", 372*0b57cec5SDimitry Andric tid, context); 373*0b57cec5SDimitry Andric 374*0b57cec5SDimitry Andric if (m_interpreter && m_python_object_sp) { 375*0b57cec5SDimitry Andric // First thing we have to do is to try to get the API lock, and the 376*0b57cec5SDimitry Andric // interpreter lock. We're going to change the thread content of the 377*0b57cec5SDimitry Andric // process, and we're going to use python, which requires the API lock to 378*0b57cec5SDimitry Andric // do it. We need the interpreter lock to make sure thread_info_dict stays 379*0b57cec5SDimitry Andric // alive. 380*0b57cec5SDimitry Andric // 381*0b57cec5SDimitry Andric // If someone already has the API lock, that is ok, we just want to avoid 382*0b57cec5SDimitry Andric // external code from making new API calls while this call is happening. 383*0b57cec5SDimitry Andric // 384*0b57cec5SDimitry Andric // This is a recursive lock so we can grant it to any Python code called on 385*0b57cec5SDimitry Andric // the stack below us. 386*0b57cec5SDimitry Andric Target &target = m_process->GetTarget(); 387*0b57cec5SDimitry Andric std::unique_lock<std::recursive_mutex> api_lock(target.GetAPIMutex(), 388*0b57cec5SDimitry Andric std::defer_lock); 389480093f4SDimitry Andric (void)api_lock.try_lock(); // See above. 390*0b57cec5SDimitry Andric auto interpreter_lock = m_interpreter->AcquireInterpreterLock(); 391*0b57cec5SDimitry Andric 392*0b57cec5SDimitry Andric StructuredData::DictionarySP thread_info_dict = 393*0b57cec5SDimitry Andric m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context); 394*0b57cec5SDimitry Andric std::vector<bool> core_used_map; 395*0b57cec5SDimitry Andric if (thread_info_dict) { 396*0b57cec5SDimitry Andric ThreadList core_threads(m_process); 397*0b57cec5SDimitry Andric ThreadList &thread_list = m_process->GetThreadList(); 398*0b57cec5SDimitry Andric bool did_create = false; 399*0b57cec5SDimitry Andric ThreadSP thread_sp( 400*0b57cec5SDimitry Andric CreateThreadFromThreadInfo(*thread_info_dict, core_threads, 401*0b57cec5SDimitry Andric thread_list, core_used_map, &did_create)); 402*0b57cec5SDimitry Andric if (did_create) 403*0b57cec5SDimitry Andric thread_list.AddThread(thread_sp); 404*0b57cec5SDimitry Andric return thread_sp; 405*0b57cec5SDimitry Andric } 406*0b57cec5SDimitry Andric } 407*0b57cec5SDimitry Andric return ThreadSP(); 408*0b57cec5SDimitry Andric } 409*0b57cec5SDimitry Andric 410480093f4SDimitry Andric #endif // #if LLDB_ENABLE_PYTHON 411