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