1c156427dSZachary Turner //===-- OperatingSystemPython.cpp --------------------------------*- C++-*-===// 2b3e77600SGreg Clayton // 3b3e77600SGreg Clayton // The LLVM Compiler Infrastructure 4b3e77600SGreg Clayton // 5b3e77600SGreg Clayton // This file is distributed under the University of Illinois Open Source 6b3e77600SGreg Clayton // License. See LICENSE.TXT for details. 7b3e77600SGreg Clayton // 8b3e77600SGreg Clayton //===----------------------------------------------------------------------===// 993a64300SDaniel Malea 10b3e77600SGreg Clayton #ifndef LLDB_DISABLE_PYTHON 11b3e77600SGreg Clayton 12b3e77600SGreg Clayton #include "OperatingSystemPython.h" 13b3e77600SGreg Clayton // C Includes 14b3e77600SGreg Clayton // C++ Includes 15b3e77600SGreg Clayton // Other libraries and framework includes 16b9c1b51eSKate Stone #include "Plugins/Process/Utility/DynamicRegisterInfo.h" 17b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextDummy.h" 18b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextMemory.h" 19b9c1b51eSKate Stone #include "Plugins/Process/Utility/ThreadMemory.h" 20b3e77600SGreg Clayton #include "lldb/Core/ArchSpec.h" 215790759aSEnrico Granata #include "lldb/Core/Debugger.h" 22b3e77600SGreg Clayton #include "lldb/Core/Module.h" 23b3e77600SGreg Clayton #include "lldb/Core/PluginManager.h" 24b3e77600SGreg Clayton #include "lldb/Core/RegisterValue.h" 25b3e77600SGreg Clayton #include "lldb/Core/ValueObjectVariable.h" 265790759aSEnrico Granata #include "lldb/Interpreter/CommandInterpreter.h" 270641ca1aSZachary Turner #include "lldb/Interpreter/ScriptInterpreter.h" 28b3e77600SGreg Clayton #include "lldb/Symbol/ObjectFile.h" 29b3e77600SGreg Clayton #include "lldb/Symbol/VariableList.h" 30b3e77600SGreg Clayton #include "lldb/Target/Process.h" 31b3e77600SGreg Clayton #include "lldb/Target/StopInfo.h" 32b3e77600SGreg Clayton #include "lldb/Target/Target.h" 33b3e77600SGreg Clayton #include "lldb/Target/Thread.h" 34b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h" 35666cc0b2SZachary Turner #include "lldb/Utility/DataBufferHeap.h" 36bf9a7730SZachary Turner #include "lldb/Utility/StreamString.h" 37*f2a8bccfSPavel Labath #include "lldb/Utility/StructuredData.h" 38b3e77600SGreg Clayton 39b3e77600SGreg Clayton using namespace lldb; 40b3e77600SGreg Clayton using namespace lldb_private; 41b3e77600SGreg Clayton 42b9c1b51eSKate Stone void OperatingSystemPython::Initialize() { 43b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 44b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 45b9c1b51eSKate Stone nullptr); 46b3e77600SGreg Clayton } 47b3e77600SGreg Clayton 48b9c1b51eSKate Stone void OperatingSystemPython::Terminate() { 49b3e77600SGreg Clayton PluginManager::UnregisterPlugin(CreateInstance); 50b3e77600SGreg Clayton } 51b3e77600SGreg Clayton 52b9c1b51eSKate Stone OperatingSystem *OperatingSystemPython::CreateInstance(Process *process, 53b9c1b51eSKate Stone bool force) { 54b9c1b51eSKate Stone // Python OperatingSystem plug-ins must be requested by name, so force must be 55b9c1b51eSKate Stone // true 56c9d645d3SGreg Clayton FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath()); 57b9c1b51eSKate Stone if (python_os_plugin_spec && python_os_plugin_spec.Exists()) { 58b9c1b51eSKate Stone std::unique_ptr<OperatingSystemPython> os_ap( 59b9c1b51eSKate Stone new OperatingSystemPython(process, python_os_plugin_spec)); 60c9d645d3SGreg Clayton if (os_ap.get() && os_ap->IsValid()) 61c9d645d3SGreg Clayton return os_ap.release(); 62c9d645d3SGreg Clayton } 63b3e77600SGreg Clayton return NULL; 64b3e77600SGreg Clayton } 65b3e77600SGreg Clayton 66b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginNameStatic() { 6757abc5d6SGreg Clayton static ConstString g_name("python"); 6857abc5d6SGreg Clayton return g_name; 69b3e77600SGreg Clayton } 70b3e77600SGreg Clayton 71b9c1b51eSKate Stone const char *OperatingSystemPython::GetPluginDescriptionStatic() { 72b9c1b51eSKate Stone return "Operating system plug-in that gathers OS information from a python " 73b9c1b51eSKate Stone "class that implements the necessary OperatingSystem functionality."; 74b3e77600SGreg Clayton } 75b3e77600SGreg Clayton 76b9c1b51eSKate Stone OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process, 77b9c1b51eSKate Stone const FileSpec &python_module_path) 78b9c1b51eSKate Stone : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_ap(), 79b9c1b51eSKate Stone m_interpreter(NULL), m_python_object_sp() { 805790759aSEnrico Granata if (!process) 815790759aSEnrico Granata return; 82a4d8747dSGreg Clayton TargetSP target_sp = process->CalculateTarget(); 835790759aSEnrico Granata if (!target_sp) 845790759aSEnrico Granata return; 85b9c1b51eSKate Stone m_interpreter = 86b9c1b51eSKate Stone target_sp->GetDebugger().GetCommandInterpreter().GetScriptInterpreter(); 87b9c1b51eSKate Stone if (m_interpreter) { 882443cbd7SGreg Clayton 89b9c1b51eSKate Stone std::string os_plugin_class_name( 90b9c1b51eSKate Stone python_module_path.GetFilename().AsCString("")); 91b9c1b51eSKate Stone if (!os_plugin_class_name.empty()) { 92c9d645d3SGreg Clayton const bool init_session = false; 93c9d645d3SGreg Clayton const bool allow_reload = true; 94c9d645d3SGreg Clayton char python_module_path_cstr[PATH_MAX]; 95b9c1b51eSKate Stone python_module_path.GetPath(python_module_path_cstr, 96b9c1b51eSKate Stone sizeof(python_module_path_cstr)); 9797206d57SZachary Turner Status error; 98b9c1b51eSKate Stone if (m_interpreter->LoadScriptingModule( 99b9c1b51eSKate Stone python_module_path_cstr, allow_reload, init_session, error)) { 100c9d645d3SGreg Clayton // Strip the ".py" extension if there is one 101c9d645d3SGreg Clayton size_t py_extension_pos = os_plugin_class_name.rfind(".py"); 102c9d645d3SGreg Clayton if (py_extension_pos != std::string::npos) 103c9d645d3SGreg Clayton os_plugin_class_name.erase(py_extension_pos); 104b9c1b51eSKate Stone // Add ".OperatingSystemPlugIn" to the module name to get a string like 105b9c1b51eSKate Stone // "modulename.OperatingSystemPlugIn" 106c9d645d3SGreg Clayton os_plugin_class_name += ".OperatingSystemPlugIn"; 1070641ca1aSZachary Turner StructuredData::ObjectSP object_sp = 108b9c1b51eSKate Stone m_interpreter->OSPlugin_CreatePluginObject( 109b9c1b51eSKate Stone os_plugin_class_name.c_str(), process->CalculateProcess()); 1100641ca1aSZachary Turner if (object_sp && object_sp->IsValid()) 111a4d8747dSGreg Clayton m_python_object_sp = object_sp; 112c9d645d3SGreg Clayton } 1132443cbd7SGreg Clayton } 1145790759aSEnrico Granata } 115b3e77600SGreg Clayton } 116b3e77600SGreg Clayton 117b9c1b51eSKate Stone OperatingSystemPython::~OperatingSystemPython() {} 118b3e77600SGreg Clayton 119b9c1b51eSKate Stone DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() { 120b9c1b51eSKate Stone if (m_register_info_ap.get() == NULL) { 121a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 1225790759aSEnrico Granata return NULL; 12362f80036SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS)); 1241c22be69SGreg Clayton 1251c22be69SGreg Clayton if (log) 126b9c1b51eSKate Stone log->Printf("OperatingSystemPython::GetDynamicRegisterInfo() fetching " 127b9c1b51eSKate Stone "thread register definitions from python for pid %" PRIu64, 128b9c1b51eSKate Stone m_process->GetID()); 1291c22be69SGreg Clayton 130b9c1b51eSKate Stone StructuredData::DictionarySP dictionary = 131b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterInfo(m_python_object_sp); 1325790759aSEnrico Granata if (!dictionary) 1335790759aSEnrico Granata return NULL; 134b3e77600SGreg Clayton 135b9c1b51eSKate Stone m_register_info_ap.reset(new DynamicRegisterInfo( 136b9c1b51eSKate Stone *dictionary, m_process->GetTarget().GetArchitecture())); 1372443cbd7SGreg Clayton assert(m_register_info_ap->GetNumRegisters() > 0); 1382443cbd7SGreg Clayton assert(m_register_info_ap->GetNumRegisterSets() > 0); 139b3e77600SGreg Clayton } 140b3e77600SGreg Clayton return m_register_info_ap.get(); 141b3e77600SGreg Clayton } 142b3e77600SGreg Clayton 143b3e77600SGreg Clayton //------------------------------------------------------------------ 144b3e77600SGreg Clayton // PluginInterface protocol 145b3e77600SGreg Clayton //------------------------------------------------------------------ 146b9c1b51eSKate Stone ConstString OperatingSystemPython::GetPluginName() { 147b3e77600SGreg Clayton return GetPluginNameStatic(); 148b3e77600SGreg Clayton } 149b3e77600SGreg Clayton 150b9c1b51eSKate Stone uint32_t OperatingSystemPython::GetPluginVersion() { return 1; } 151b3e77600SGreg Clayton 152b9c1b51eSKate Stone bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list, 153ba4e61d3SAndrew Kaylor ThreadList &core_thread_list, 154b9c1b51eSKate Stone ThreadList &new_thread_list) { 155a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp) 156a85e6b6cSDaniel Malea return false; 1571c22be69SGreg Clayton 15862f80036SGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OS)); 1591c22be69SGreg Clayton 160ab745c2aSGreg Clayton // First thing we have to do is to try to get the API lock, and the run lock. 161ab745c2aSGreg Clayton // We're going to change the thread content of the process, and we're going 162ab745c2aSGreg Clayton // to use python, which requires the API lock to do it. 163ab745c2aSGreg Clayton // 164ab745c2aSGreg Clayton // If someone already has the API lock, that is ok, we just want to avoid 165ab745c2aSGreg Clayton // external code from making new API calls while this call is happening. 166ab745c2aSGreg Clayton // 167ab745c2aSGreg Clayton // This is a recursive lock so we can grant it to any Python code called on 168ab745c2aSGreg Clayton // the stack below us. 16985e276b8SJim Ingham Target &target = m_process->GetTarget(); 170b9c1b51eSKate Stone std::unique_lock<std::recursive_mutex> lock(target.GetAPIMutex(), 171b9c1b51eSKate Stone std::defer_lock); 172bb19a13cSSaleem Abdulrasool lock.try_lock(); 17385e276b8SJim Ingham 1741c22be69SGreg Clayton if (log) 175b9c1b51eSKate Stone log->Printf("OperatingSystemPython::UpdateThreadList() fetching thread " 176b9c1b51eSKate Stone "data from python for pid %" PRIu64, 177b9c1b51eSKate Stone m_process->GetID()); 1781c22be69SGreg Clayton 179b9c1b51eSKate Stone // The threads that are in "new_thread_list" upon entry are the threads from 180b9c1b51eSKate Stone // the 181b3ae8761SGreg Clayton // lldb_private::Process subclass, no memory threads will be in this list. 182b3ae8761SGreg Clayton 183b9c1b51eSKate Stone auto interpreter_lock = 184b9c1b51eSKate Stone m_interpreter 185b9c1b51eSKate Stone ->AcquireInterpreterLock(); // to make sure threads_list stays alive 186b9c1b51eSKate Stone StructuredData::ArraySP threads_list = 187b9c1b51eSKate Stone m_interpreter->OSPlugin_ThreadsInfo(m_python_object_sp); 188e98008ccSGreg Clayton 189e98008ccSGreg Clayton const uint32_t num_cores = core_thread_list.GetSize(false); 190e98008ccSGreg Clayton 191e98008ccSGreg Clayton // Make a map so we can keep track of which cores were used from the 192e98008ccSGreg Clayton // core_thread list. Any real threads/cores that weren't used should 193e98008ccSGreg Clayton // later be put back into the "new_thread_list". 194e98008ccSGreg Clayton std::vector<bool> core_used_map(num_cores, false); 195b9c1b51eSKate Stone if (threads_list) { 196b9c1b51eSKate Stone if (log) { 19762f80036SGreg Clayton StreamString strm; 1980641ca1aSZachary Turner threads_list->Dump(strm); 199c156427dSZachary Turner log->Printf("threads_list = %s", strm.GetData()); 20062f80036SGreg Clayton } 2010641ca1aSZachary Turner 2020641ca1aSZachary Turner const uint32_t num_threads = threads_list->GetSize(); 203b9c1b51eSKate Stone for (uint32_t i = 0; i < num_threads; ++i) { 204b9c1b51eSKate Stone StructuredData::ObjectSP thread_dict_obj = 205b9c1b51eSKate Stone threads_list->GetItemAtIndex(i); 206b9c1b51eSKate Stone if (auto thread_dict = thread_dict_obj->GetAsDictionary()) { 207b9c1b51eSKate Stone ThreadSP thread_sp( 208b9c1b51eSKate Stone CreateThreadFromThreadInfo(*thread_dict, core_thread_list, 209b9c1b51eSKate Stone old_thread_list, core_used_map, NULL)); 210a4d8747dSGreg Clayton if (thread_sp) 211435ce139SGreg Clayton new_thread_list.AddThread(thread_sp); 212435ce139SGreg Clayton } 213435ce139SGreg Clayton } 214435ce139SGreg Clayton } 2156e0ff1a3SGreg Clayton 216e98008ccSGreg Clayton // Any real core threads that didn't end up backing a memory thread should 217b9c1b51eSKate Stone // still be in the main thread list, and they should be inserted at the 218b9c1b51eSKate Stone // beginning 219e98008ccSGreg Clayton // of the list 220e98008ccSGreg Clayton uint32_t insert_idx = 0; 221b9c1b51eSKate Stone for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) { 222b9c1b51eSKate Stone if (core_used_map[core_idx] == false) { 223b9c1b51eSKate Stone new_thread_list.InsertThread( 224b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx); 225e98008ccSGreg Clayton ++insert_idx; 226e98008ccSGreg Clayton } 227e98008ccSGreg Clayton } 2286e0ff1a3SGreg Clayton 229b3e77600SGreg Clayton return new_thread_list.GetSize(false) > 0; 230b3e77600SGreg Clayton } 231b3e77600SGreg Clayton 232b9c1b51eSKate Stone ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo( 233b9c1b51eSKate Stone StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list, 234b9c1b51eSKate Stone ThreadList &old_thread_list, std::vector<bool> &core_used_map, 235b9c1b51eSKate Stone bool *did_create_ptr) { 236a4d8747dSGreg Clayton ThreadSP thread_sp; 2370641ca1aSZachary Turner tid_t tid = LLDB_INVALID_THREAD_ID; 2380641ca1aSZachary Turner if (!thread_dict.GetValueForKeyAsInteger("tid", tid)) 2390641ca1aSZachary Turner return ThreadSP(); 240a4d8747dSGreg Clayton 2410641ca1aSZachary Turner uint32_t core_number; 2420641ca1aSZachary Turner addr_t reg_data_addr; 2432833321fSZachary Turner llvm::StringRef name; 2442833321fSZachary Turner llvm::StringRef queue; 2450641ca1aSZachary Turner 2460641ca1aSZachary Turner thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX); 247b9c1b51eSKate Stone thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr, 248b9c1b51eSKate Stone LLDB_INVALID_ADDRESS); 2490641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("name", name); 2500641ca1aSZachary Turner thread_dict.GetValueForKeyAsString("queue", queue); 251a4d8747dSGreg Clayton 252160c9d81SGreg Clayton // See if a thread already exists for "tid" 253b3ae8761SGreg Clayton thread_sp = old_thread_list.FindThreadByID(tid, false); 254b9c1b51eSKate Stone if (thread_sp) { 255b9c1b51eSKate Stone // A thread already does exist for "tid", make sure it was an operating 256b9c1b51eSKate Stone // system 257160c9d81SGreg Clayton // plug-in generated thread. 258b9c1b51eSKate Stone if (!IsOperatingSystemPluginThread(thread_sp)) { 259160c9d81SGreg Clayton // We have thread ID overlap between the protocol threads and the 260160c9d81SGreg Clayton // operating system threads, clear the thread so we create an 261160c9d81SGreg Clayton // operating system thread for this. 262160c9d81SGreg Clayton thread_sp.reset(); 263160c9d81SGreg Clayton } 264160c9d81SGreg Clayton } 265160c9d81SGreg Clayton 266b9c1b51eSKate Stone if (!thread_sp) { 267a4d8747dSGreg Clayton if (did_create_ptr) 268a4d8747dSGreg Clayton *did_create_ptr = true; 2692833321fSZachary Turner thread_sp.reset( 2702833321fSZachary Turner new ThreadMemory(*m_process, tid, name, queue, reg_data_addr)); 271b3ae8761SGreg Clayton } 272b3ae8761SGreg Clayton 273b9c1b51eSKate Stone if (core_number < core_thread_list.GetSize(false)) { 274b9c1b51eSKate Stone ThreadSP core_thread_sp( 275b9c1b51eSKate Stone core_thread_list.GetThreadAtIndex(core_number, false)); 276b9c1b51eSKate Stone if (core_thread_sp) { 277b9c1b51eSKate Stone // Keep track of which cores were set as the backing thread for memory 278b9c1b51eSKate Stone // threads... 279e98008ccSGreg Clayton if (core_number < core_used_map.size()) 280e98008ccSGreg Clayton core_used_map[core_number] = true; 281e98008ccSGreg Clayton 282160c9d81SGreg Clayton ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread()); 283b9c1b51eSKate Stone if (backing_core_thread_sp) { 284160c9d81SGreg Clayton thread_sp->SetBackingThread(backing_core_thread_sp); 285b9c1b51eSKate Stone } else { 286160c9d81SGreg Clayton thread_sp->SetBackingThread(core_thread_sp); 287160c9d81SGreg Clayton } 288160c9d81SGreg Clayton } 289a4d8747dSGreg Clayton } 290a4d8747dSGreg Clayton return thread_sp; 291a4d8747dSGreg Clayton } 292a4d8747dSGreg Clayton 293b9c1b51eSKate Stone void OperatingSystemPython::ThreadWasSelected(Thread *thread) {} 294b3e77600SGreg Clayton 295b3e77600SGreg Clayton RegisterContextSP 296b9c1b51eSKate Stone OperatingSystemPython::CreateRegisterContextForThread(Thread *thread, 297b9c1b51eSKate Stone addr_t reg_data_addr) { 298435ce139SGreg Clayton RegisterContextSP reg_ctx_sp; 299a4d8747dSGreg Clayton if (!m_interpreter || !m_python_object_sp || !thread) 300160c9d81SGreg Clayton return reg_ctx_sp; 301160c9d81SGreg Clayton 302160c9d81SGreg Clayton if (!IsOperatingSystemPluginThread(thread->shared_from_this())) 303160c9d81SGreg Clayton return reg_ctx_sp; 3041c22be69SGreg Clayton 305b9c1b51eSKate Stone // First thing we have to do is get the API lock, and the run lock. We're 306b9c1b51eSKate Stone // going to change the thread 307b9c1b51eSKate Stone // content of the process, and we're going to use python, which requires the 308b9c1b51eSKate Stone // API lock to do it. 309b9c1b51eSKate Stone // So get & hold that. This is a recursive lock so we can grant it to any 310b9c1b51eSKate Stone // Python code called on the stack below us. 31185e276b8SJim Ingham Target &target = m_process->GetTarget(); 312bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex()); 31385e276b8SJim Ingham 3145160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 3151c22be69SGreg Clayton 316b9c1b51eSKate Stone auto lock = 317b9c1b51eSKate Stone m_interpreter 318b9c1b51eSKate Stone ->AcquireInterpreterLock(); // to make sure python objects stays alive 319b9c1b51eSKate Stone if (reg_data_addr != LLDB_INVALID_ADDRESS) { 320ead45e01SGreg Clayton // The registers data is in contiguous memory, just create the register 321ead45e01SGreg Clayton // context using the address provided 322ead45e01SGreg Clayton if (log) 323b9c1b51eSKate Stone log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid " 324b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64 325b9c1b51eSKate Stone ") creating memory register context", 326b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID(), reg_data_addr); 327b9c1b51eSKate Stone reg_ctx_sp.reset(new RegisterContextMemory( 328b9c1b51eSKate Stone *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr)); 329b9c1b51eSKate Stone } else { 330ead45e01SGreg Clayton // No register data address is provided, query the python plug-in to let 331ead45e01SGreg Clayton // it make up the data as it sees fit 3321c22be69SGreg Clayton if (log) 333b9c1b51eSKate Stone log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid " 334b9c1b51eSKate Stone "= 0x%" PRIx64 ", 0x%" PRIx64 335b9c1b51eSKate Stone ") fetching register data from python", 336b9c1b51eSKate Stone thread->GetID(), thread->GetProtocolID()); 3371c22be69SGreg Clayton 338b9c1b51eSKate Stone StructuredData::StringSP reg_context_data = 339b9c1b51eSKate Stone m_interpreter->OSPlugin_RegisterContextData(m_python_object_sp, 340b9c1b51eSKate Stone thread->GetID()); 341b9c1b51eSKate Stone if (reg_context_data) { 3420641ca1aSZachary Turner std::string value = reg_context_data->GetValue(); 3430641ca1aSZachary Turner DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length())); 344b9c1b51eSKate Stone if (data_sp->GetByteSize()) { 345b9c1b51eSKate Stone RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory( 346b9c1b51eSKate Stone *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS); 347b9c1b51eSKate Stone if (reg_ctx_memory) { 348435ce139SGreg Clayton reg_ctx_sp.reset(reg_ctx_memory); 349435ce139SGreg Clayton reg_ctx_memory->SetAllRegisterData(data_sp); 350435ce139SGreg Clayton } 351435ce139SGreg Clayton } 352435ce139SGreg Clayton } 353ead45e01SGreg Clayton } 354b9c1b51eSKate Stone // if we still have no register data, fallback on a dummy context to avoid 355b9c1b51eSKate Stone // crashing 356b9c1b51eSKate Stone if (!reg_ctx_sp) { 357cbd79b6cSEnrico Granata if (log) 358b9c1b51eSKate Stone log->Printf("OperatingSystemPython::CreateRegisterContextForThread (tid " 359b9c1b51eSKate Stone "= 0x%" PRIx64 ") forcing a dummy register context", 360b9c1b51eSKate Stone thread->GetID()); 361b9c1b51eSKate Stone reg_ctx_sp.reset(new RegisterContextDummy( 362b9c1b51eSKate Stone *thread, 0, target.GetArchitecture().GetAddressByteSize())); 363cbd79b6cSEnrico Granata } 364b3e77600SGreg Clayton return reg_ctx_sp; 365b3e77600SGreg Clayton } 366b3e77600SGreg Clayton 367b3e77600SGreg Clayton StopInfoSP 368b9c1b51eSKate Stone OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) { 369b3e77600SGreg Clayton // We should have gotten the thread stop info from the dictionary of data for 370b3e77600SGreg Clayton // the thread in the initial call to get_thread_info(), this should have been 371b3e77600SGreg Clayton // cached so we can return it here 372b9c1b51eSKate Stone StopInfoSP 373b9c1b51eSKate Stone stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP)); 374b3e77600SGreg Clayton return stop_info_sp; 375b3e77600SGreg Clayton } 376b3e77600SGreg Clayton 377b9c1b51eSKate Stone lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid, 378b9c1b51eSKate Stone addr_t context) { 3795160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 380a4d8747dSGreg Clayton 381a4d8747dSGreg Clayton if (log) 382b9c1b51eSKate Stone log->Printf("OperatingSystemPython::CreateThread (tid = 0x%" PRIx64 383b9c1b51eSKate Stone ", context = 0x%" PRIx64 ") fetching register data from python", 384b9c1b51eSKate Stone tid, context); 385a4d8747dSGreg Clayton 386b9c1b51eSKate Stone if (m_interpreter && m_python_object_sp) { 387b9c1b51eSKate Stone // First thing we have to do is get the API lock, and the run lock. We're 388b9c1b51eSKate Stone // going to change the thread 389b9c1b51eSKate Stone // content of the process, and we're going to use python, which requires the 390b9c1b51eSKate Stone // API lock to do it. 391b9c1b51eSKate Stone // So get & hold that. This is a recursive lock so we can grant it to any 392b9c1b51eSKate Stone // Python code called on the stack below us. 393a4d8747dSGreg Clayton Target &target = m_process->GetTarget(); 394bb19a13cSSaleem Abdulrasool std::lock_guard<std::recursive_mutex> guard(target.GetAPIMutex()); 395a4d8747dSGreg Clayton 396b9c1b51eSKate Stone auto lock = m_interpreter->AcquireInterpreterLock(); // to make sure 397b9c1b51eSKate Stone // thread_info_dict 398b9c1b51eSKate Stone // stays alive 399b9c1b51eSKate Stone StructuredData::DictionarySP thread_info_dict = 400b9c1b51eSKate Stone m_interpreter->OSPlugin_CreateThread(m_python_object_sp, tid, context); 401e98008ccSGreg Clayton std::vector<bool> core_used_map; 402b9c1b51eSKate Stone if (thread_info_dict) { 403b3ae8761SGreg Clayton ThreadList core_threads(m_process); 404a4d8747dSGreg Clayton ThreadList &thread_list = m_process->GetThreadList(); 405a4d8747dSGreg Clayton bool did_create = false; 406b9c1b51eSKate Stone ThreadSP thread_sp( 407b9c1b51eSKate Stone CreateThreadFromThreadInfo(*thread_info_dict, core_threads, 408b9c1b51eSKate Stone thread_list, core_used_map, &did_create)); 409a4d8747dSGreg Clayton if (did_create) 410a4d8747dSGreg Clayton thread_list.AddThread(thread_sp); 411a4d8747dSGreg Clayton return thread_sp; 412a4d8747dSGreg Clayton } 413a4d8747dSGreg Clayton } 414a4d8747dSGreg Clayton return ThreadSP(); 415a4d8747dSGreg Clayton } 416a4d8747dSGreg Clayton 417b3e77600SGreg Clayton #endif // #ifndef LLDB_DISABLE_PYTHON 418