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