15ffd83dbSDimitry Andric //===-- OperatingSystemPython.cpp -----------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
9480093f4SDimitry Andric #include "lldb/Host/Config.h"
10480093f4SDimitry Andric
11480093f4SDimitry Andric #if LLDB_ENABLE_PYTHON
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "OperatingSystemPython.h"
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextDummy.h"
160b57cec5SDimitry Andric #include "Plugins/Process/Utility/RegisterContextMemory.h"
170b57cec5SDimitry Andric #include "Plugins/Process/Utility/ThreadMemory.h"
180b57cec5SDimitry Andric #include "lldb/Core/Debugger.h"
190b57cec5SDimitry Andric #include "lldb/Core/Module.h"
200b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
210b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
220b57cec5SDimitry Andric #include "lldb/Interpreter/CommandInterpreter.h"
230b57cec5SDimitry Andric #include "lldb/Interpreter/ScriptInterpreter.h"
240b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
250b57cec5SDimitry Andric #include "lldb/Symbol/VariableList.h"
260b57cec5SDimitry Andric #include "lldb/Target/Process.h"
270b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
280b57cec5SDimitry Andric #include "lldb/Target/Target.h"
290b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
300b57cec5SDimitry Andric #include "lldb/Target/ThreadList.h"
310b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
3281ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
330b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
340b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
350b57cec5SDimitry Andric #include "lldb/Utility/StructuredData.h"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric #include <memory>
380b57cec5SDimitry Andric
390b57cec5SDimitry Andric using namespace lldb;
400b57cec5SDimitry Andric using namespace lldb_private;
410b57cec5SDimitry Andric
LLDB_PLUGIN_DEFINE(OperatingSystemPython)425ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(OperatingSystemPython)
435ffd83dbSDimitry Andric
440b57cec5SDimitry Andric void OperatingSystemPython::Initialize() {
450b57cec5SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
460b57cec5SDimitry Andric GetPluginDescriptionStatic(), CreateInstance,
470b57cec5SDimitry Andric nullptr);
480b57cec5SDimitry Andric }
490b57cec5SDimitry Andric
Terminate()500b57cec5SDimitry Andric void OperatingSystemPython::Terminate() {
510b57cec5SDimitry Andric PluginManager::UnregisterPlugin(CreateInstance);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
CreateInstance(Process * process,bool force)540b57cec5SDimitry Andric OperatingSystem *OperatingSystemPython::CreateInstance(Process *process,
550b57cec5SDimitry Andric bool force) {
560b57cec5SDimitry Andric // Python OperatingSystem plug-ins must be requested by name, so force must
570b57cec5SDimitry Andric // be true
580b57cec5SDimitry Andric FileSpec python_os_plugin_spec(process->GetPythonOSPluginPath());
590b57cec5SDimitry Andric if (python_os_plugin_spec &&
600b57cec5SDimitry Andric FileSystem::Instance().Exists(python_os_plugin_spec)) {
610b57cec5SDimitry Andric std::unique_ptr<OperatingSystemPython> os_up(
620b57cec5SDimitry Andric new OperatingSystemPython(process, python_os_plugin_spec));
630b57cec5SDimitry Andric if (os_up.get() && os_up->IsValid())
640b57cec5SDimitry Andric return os_up.release();
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric return nullptr;
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric
GetPluginDescriptionStatic()69349cc55cSDimitry Andric llvm::StringRef OperatingSystemPython::GetPluginDescriptionStatic() {
700b57cec5SDimitry Andric return "Operating system plug-in that gathers OS information from a python "
710b57cec5SDimitry Andric "class that implements the necessary OperatingSystem functionality.";
720b57cec5SDimitry Andric }
730b57cec5SDimitry Andric
OperatingSystemPython(lldb_private::Process * process,const FileSpec & python_module_path)740b57cec5SDimitry Andric OperatingSystemPython::OperatingSystemPython(lldb_private::Process *process,
750b57cec5SDimitry Andric const FileSpec &python_module_path)
760b57cec5SDimitry Andric : OperatingSystem(process), m_thread_list_valobj_sp(), m_register_info_up(),
77*c9157d92SDimitry Andric m_interpreter(nullptr), m_script_object_sp() {
780b57cec5SDimitry Andric if (!process)
790b57cec5SDimitry Andric return;
800b57cec5SDimitry Andric TargetSP target_sp = process->CalculateTarget();
810b57cec5SDimitry Andric if (!target_sp)
820b57cec5SDimitry Andric return;
830b57cec5SDimitry Andric m_interpreter = target_sp->GetDebugger().GetScriptInterpreter();
84*c9157d92SDimitry Andric if (!m_interpreter)
85*c9157d92SDimitry Andric return;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric std::string os_plugin_class_name(
880b57cec5SDimitry Andric python_module_path.GetFilename().AsCString(""));
89*c9157d92SDimitry Andric if (os_plugin_class_name.empty())
90*c9157d92SDimitry Andric return;
91*c9157d92SDimitry Andric
92fe6060f1SDimitry Andric LoadScriptOptions options;
930b57cec5SDimitry Andric char python_module_path_cstr[PATH_MAX];
940b57cec5SDimitry Andric python_module_path.GetPath(python_module_path_cstr,
950b57cec5SDimitry Andric sizeof(python_module_path_cstr));
960b57cec5SDimitry Andric Status error;
97*c9157d92SDimitry Andric if (!m_interpreter->LoadScriptingModule(python_module_path_cstr, options,
98*c9157d92SDimitry Andric error))
99*c9157d92SDimitry Andric return;
100*c9157d92SDimitry Andric
1010b57cec5SDimitry Andric // Strip the ".py" extension if there is one
1020b57cec5SDimitry Andric size_t py_extension_pos = os_plugin_class_name.rfind(".py");
1030b57cec5SDimitry Andric if (py_extension_pos != std::string::npos)
1040b57cec5SDimitry Andric os_plugin_class_name.erase(py_extension_pos);
1050b57cec5SDimitry Andric // Add ".OperatingSystemPlugIn" to the module name to get a string like
1060b57cec5SDimitry Andric // "modulename.OperatingSystemPlugIn"
1070b57cec5SDimitry Andric os_plugin_class_name += ".OperatingSystemPlugIn";
108*c9157d92SDimitry Andric
109*c9157d92SDimitry Andric auto operating_system_interface =
110*c9157d92SDimitry Andric m_interpreter->CreateOperatingSystemInterface();
111*c9157d92SDimitry Andric if (!operating_system_interface)
112*c9157d92SDimitry Andric // FIXME: We should pass an Status& to raise the error to the user.
113*c9157d92SDimitry Andric // return llvm::createStringError(
114*c9157d92SDimitry Andric // llvm::inconvertibleErrorCode(),
115*c9157d92SDimitry Andric // "Failed to create scripted thread interface.");
116*c9157d92SDimitry Andric return;
117*c9157d92SDimitry Andric
118*c9157d92SDimitry Andric ExecutionContext exe_ctx(process);
119*c9157d92SDimitry Andric auto obj_or_err = operating_system_interface->CreatePluginObject(
120*c9157d92SDimitry Andric os_plugin_class_name, exe_ctx, nullptr);
121*c9157d92SDimitry Andric
122*c9157d92SDimitry Andric if (!obj_or_err) {
123*c9157d92SDimitry Andric llvm::consumeError(obj_or_err.takeError());
124*c9157d92SDimitry Andric return;
1250b57cec5SDimitry Andric }
126*c9157d92SDimitry Andric
127*c9157d92SDimitry Andric StructuredData::GenericSP owned_script_object_sp = *obj_or_err;
128*c9157d92SDimitry Andric if (!owned_script_object_sp->IsValid())
129*c9157d92SDimitry Andric // return llvm::createStringError(llvm::inconvertibleErrorCode(),
130*c9157d92SDimitry Andric // "Created script object is invalid.");
131*c9157d92SDimitry Andric return;
132*c9157d92SDimitry Andric
133*c9157d92SDimitry Andric m_script_object_sp = owned_script_object_sp;
134*c9157d92SDimitry Andric m_operating_system_interface_sp = operating_system_interface;
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
137fe6060f1SDimitry Andric OperatingSystemPython::~OperatingSystemPython() = default;
1380b57cec5SDimitry Andric
GetDynamicRegisterInfo()1390b57cec5SDimitry Andric DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
1400b57cec5SDimitry Andric if (m_register_info_up == nullptr) {
141*c9157d92SDimitry Andric if (!m_interpreter || !m_operating_system_interface_sp)
1420b57cec5SDimitry Andric return nullptr;
14381ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS);
1440b57cec5SDimitry Andric
1459dba64beSDimitry Andric LLDB_LOGF(log,
1469dba64beSDimitry Andric "OperatingSystemPython::GetDynamicRegisterInfo() fetching "
1470b57cec5SDimitry Andric "thread register definitions from python for pid %" PRIu64,
1480b57cec5SDimitry Andric m_process->GetID());
1490b57cec5SDimitry Andric
1500b57cec5SDimitry Andric StructuredData::DictionarySP dictionary =
151*c9157d92SDimitry Andric m_operating_system_interface_sp->GetRegisterInfo();
1520b57cec5SDimitry Andric if (!dictionary)
1530b57cec5SDimitry Andric return nullptr;
1540b57cec5SDimitry Andric
155fe013be4SDimitry Andric m_register_info_up = DynamicRegisterInfo::Create(
156e8d8bef9SDimitry Andric *dictionary, m_process->GetTarget().GetArchitecture());
157fe013be4SDimitry Andric assert(m_register_info_up);
1580b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisters() > 0);
1590b57cec5SDimitry Andric assert(m_register_info_up->GetNumRegisterSets() > 0);
1600b57cec5SDimitry Andric }
1610b57cec5SDimitry Andric return m_register_info_up.get();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric
UpdateThreadList(ThreadList & old_thread_list,ThreadList & core_thread_list,ThreadList & new_thread_list)1640b57cec5SDimitry Andric bool OperatingSystemPython::UpdateThreadList(ThreadList &old_thread_list,
1650b57cec5SDimitry Andric ThreadList &core_thread_list,
1660b57cec5SDimitry Andric ThreadList &new_thread_list) {
167*c9157d92SDimitry Andric if (!m_interpreter || !m_operating_system_interface_sp)
1680b57cec5SDimitry Andric return false;
1690b57cec5SDimitry Andric
17081ad6265SDimitry Andric Log *log = GetLog(LLDBLog::OS);
1710b57cec5SDimitry Andric
1729dba64beSDimitry Andric LLDB_LOGF(log,
1739dba64beSDimitry Andric "OperatingSystemPython::UpdateThreadList() fetching thread "
1740b57cec5SDimitry Andric "data from python for pid %" PRIu64,
1750b57cec5SDimitry Andric m_process->GetID());
1760b57cec5SDimitry Andric
1779dba64beSDimitry Andric // The threads that are in "core_thread_list" upon entry are the threads from
1780b57cec5SDimitry Andric // the lldb_private::Process subclass, no memory threads will be in this
1790b57cec5SDimitry Andric // list.
1800b57cec5SDimitry Andric StructuredData::ArraySP threads_list =
181*c9157d92SDimitry Andric m_operating_system_interface_sp->GetThreadInfo();
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric const uint32_t num_cores = core_thread_list.GetSize(false);
1840b57cec5SDimitry Andric
1850b57cec5SDimitry Andric // Make a map so we can keep track of which cores were used from the
1860b57cec5SDimitry Andric // core_thread list. Any real threads/cores that weren't used should later be
1870b57cec5SDimitry Andric // put back into the "new_thread_list".
1880b57cec5SDimitry Andric std::vector<bool> core_used_map(num_cores, false);
1890b57cec5SDimitry Andric if (threads_list) {
1900b57cec5SDimitry Andric if (log) {
1910b57cec5SDimitry Andric StreamString strm;
1920b57cec5SDimitry Andric threads_list->Dump(strm);
1939dba64beSDimitry Andric LLDB_LOGF(log, "threads_list = %s", strm.GetData());
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric
1960b57cec5SDimitry Andric const uint32_t num_threads = threads_list->GetSize();
1970b57cec5SDimitry Andric for (uint32_t i = 0; i < num_threads; ++i) {
1980b57cec5SDimitry Andric StructuredData::ObjectSP thread_dict_obj =
1990b57cec5SDimitry Andric threads_list->GetItemAtIndex(i);
2000b57cec5SDimitry Andric if (auto thread_dict = thread_dict_obj->GetAsDictionary()) {
2010b57cec5SDimitry Andric ThreadSP thread_sp(CreateThreadFromThreadInfo(
2020b57cec5SDimitry Andric *thread_dict, core_thread_list, old_thread_list, core_used_map,
2030b57cec5SDimitry Andric nullptr));
2040b57cec5SDimitry Andric if (thread_sp)
2050b57cec5SDimitry Andric new_thread_list.AddThread(thread_sp);
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric
2100b57cec5SDimitry Andric // Any real core threads that didn't end up backing a memory thread should
2110b57cec5SDimitry Andric // still be in the main thread list, and they should be inserted at the
2120b57cec5SDimitry Andric // beginning of the list
2130b57cec5SDimitry Andric uint32_t insert_idx = 0;
2140b57cec5SDimitry Andric for (uint32_t core_idx = 0; core_idx < num_cores; ++core_idx) {
2150b57cec5SDimitry Andric if (!core_used_map[core_idx]) {
2160b57cec5SDimitry Andric new_thread_list.InsertThread(
2170b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_idx, false), insert_idx);
2180b57cec5SDimitry Andric ++insert_idx;
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric return new_thread_list.GetSize(false) > 0;
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
CreateThreadFromThreadInfo(StructuredData::Dictionary & thread_dict,ThreadList & core_thread_list,ThreadList & old_thread_list,std::vector<bool> & core_used_map,bool * did_create_ptr)2250b57cec5SDimitry Andric ThreadSP OperatingSystemPython::CreateThreadFromThreadInfo(
2260b57cec5SDimitry Andric StructuredData::Dictionary &thread_dict, ThreadList &core_thread_list,
2270b57cec5SDimitry Andric ThreadList &old_thread_list, std::vector<bool> &core_used_map,
2280b57cec5SDimitry Andric bool *did_create_ptr) {
2290b57cec5SDimitry Andric ThreadSP thread_sp;
2300b57cec5SDimitry Andric tid_t tid = LLDB_INVALID_THREAD_ID;
2310b57cec5SDimitry Andric if (!thread_dict.GetValueForKeyAsInteger("tid", tid))
2320b57cec5SDimitry Andric return ThreadSP();
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andric uint32_t core_number;
2350b57cec5SDimitry Andric addr_t reg_data_addr;
2360b57cec5SDimitry Andric llvm::StringRef name;
2370b57cec5SDimitry Andric llvm::StringRef queue;
2380b57cec5SDimitry Andric
2390b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("core", core_number, UINT32_MAX);
2400b57cec5SDimitry Andric thread_dict.GetValueForKeyAsInteger("register_data_addr", reg_data_addr,
2410b57cec5SDimitry Andric LLDB_INVALID_ADDRESS);
2420b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("name", name);
2430b57cec5SDimitry Andric thread_dict.GetValueForKeyAsString("queue", queue);
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andric // See if a thread already exists for "tid"
2460b57cec5SDimitry Andric thread_sp = old_thread_list.FindThreadByID(tid, false);
2470b57cec5SDimitry Andric if (thread_sp) {
2480b57cec5SDimitry Andric // A thread already does exist for "tid", make sure it was an operating
2490b57cec5SDimitry Andric // system
2500b57cec5SDimitry Andric // plug-in generated thread.
2510b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread_sp)) {
2520b57cec5SDimitry Andric // We have thread ID overlap between the protocol threads and the
2530b57cec5SDimitry Andric // operating system threads, clear the thread so we create an operating
2540b57cec5SDimitry Andric // system thread for this.
2550b57cec5SDimitry Andric thread_sp.reset();
2560b57cec5SDimitry Andric }
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric if (!thread_sp) {
2600b57cec5SDimitry Andric if (did_create_ptr)
2610b57cec5SDimitry Andric *did_create_ptr = true;
2620b57cec5SDimitry Andric thread_sp = std::make_shared<ThreadMemory>(*m_process, tid, name, queue,
2630b57cec5SDimitry Andric reg_data_addr);
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric if (core_number < core_thread_list.GetSize(false)) {
2670b57cec5SDimitry Andric ThreadSP core_thread_sp(
2680b57cec5SDimitry Andric core_thread_list.GetThreadAtIndex(core_number, false));
2690b57cec5SDimitry Andric if (core_thread_sp) {
2700b57cec5SDimitry Andric // Keep track of which cores were set as the backing thread for memory
2710b57cec5SDimitry Andric // threads...
2720b57cec5SDimitry Andric if (core_number < core_used_map.size())
2730b57cec5SDimitry Andric core_used_map[core_number] = true;
2740b57cec5SDimitry Andric
2750b57cec5SDimitry Andric ThreadSP backing_core_thread_sp(core_thread_sp->GetBackingThread());
2760b57cec5SDimitry Andric if (backing_core_thread_sp) {
2770b57cec5SDimitry Andric thread_sp->SetBackingThread(backing_core_thread_sp);
2780b57cec5SDimitry Andric } else {
2790b57cec5SDimitry Andric thread_sp->SetBackingThread(core_thread_sp);
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric return thread_sp;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric
ThreadWasSelected(Thread * thread)2860b57cec5SDimitry Andric void OperatingSystemPython::ThreadWasSelected(Thread *thread) {}
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric RegisterContextSP
CreateRegisterContextForThread(Thread * thread,addr_t reg_data_addr)2890b57cec5SDimitry Andric OperatingSystemPython::CreateRegisterContextForThread(Thread *thread,
2900b57cec5SDimitry Andric addr_t reg_data_addr) {
2910b57cec5SDimitry Andric RegisterContextSP reg_ctx_sp;
292*c9157d92SDimitry Andric if (!m_interpreter || !m_script_object_sp || !thread)
2930b57cec5SDimitry Andric return reg_ctx_sp;
2940b57cec5SDimitry Andric
2950b57cec5SDimitry Andric if (!IsOperatingSystemPluginThread(thread->shared_from_this()))
2960b57cec5SDimitry Andric return reg_ctx_sp;
2970b57cec5SDimitry Andric
29881ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread);
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andric if (reg_data_addr != LLDB_INVALID_ADDRESS) {
3010b57cec5SDimitry Andric // The registers data is in contiguous memory, just create the register
3020b57cec5SDimitry Andric // context using the address provided
3039dba64beSDimitry Andric LLDB_LOGF(log,
3049dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid "
3050b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64 ", reg_data_addr = 0x%" PRIx64
3060b57cec5SDimitry Andric ") creating memory register context",
3070b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID(), reg_data_addr);
3080b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextMemory>(
3090b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), reg_data_addr);
3100b57cec5SDimitry Andric } else {
3110b57cec5SDimitry Andric // No register data address is provided, query the python plug-in to let it
3120b57cec5SDimitry Andric // make up the data as it sees fit
3139dba64beSDimitry Andric LLDB_LOGF(log,
3149dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid "
3150b57cec5SDimitry Andric "= 0x%" PRIx64 ", 0x%" PRIx64
3160b57cec5SDimitry Andric ") fetching register data from python",
3170b57cec5SDimitry Andric thread->GetID(), thread->GetProtocolID());
3180b57cec5SDimitry Andric
319*c9157d92SDimitry Andric std::optional<std::string> reg_context_data =
320*c9157d92SDimitry Andric m_operating_system_interface_sp->GetRegisterContextForTID(
3210b57cec5SDimitry Andric thread->GetID());
3220b57cec5SDimitry Andric if (reg_context_data) {
323*c9157d92SDimitry Andric std::string value = *reg_context_data;
3240b57cec5SDimitry Andric DataBufferSP data_sp(new DataBufferHeap(value.c_str(), value.length()));
3250b57cec5SDimitry Andric if (data_sp->GetByteSize()) {
3260b57cec5SDimitry Andric RegisterContextMemory *reg_ctx_memory = new RegisterContextMemory(
3270b57cec5SDimitry Andric *thread, 0, *GetDynamicRegisterInfo(), LLDB_INVALID_ADDRESS);
3280b57cec5SDimitry Andric if (reg_ctx_memory) {
3290b57cec5SDimitry Andric reg_ctx_sp.reset(reg_ctx_memory);
3300b57cec5SDimitry Andric reg_ctx_memory->SetAllRegisterData(data_sp);
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric // if we still have no register data, fallback on a dummy context to avoid
3360b57cec5SDimitry Andric // crashing
3370b57cec5SDimitry Andric if (!reg_ctx_sp) {
3389dba64beSDimitry Andric LLDB_LOGF(log,
3399dba64beSDimitry Andric "OperatingSystemPython::CreateRegisterContextForThread (tid "
3400b57cec5SDimitry Andric "= 0x%" PRIx64 ") forcing a dummy register context",
3410b57cec5SDimitry Andric thread->GetID());
342*c9157d92SDimitry Andric Target &target = m_process->GetTarget();
3430b57cec5SDimitry Andric reg_ctx_sp = std::make_shared<RegisterContextDummy>(
3440b57cec5SDimitry Andric *thread, 0, target.GetArchitecture().GetAddressByteSize());
3450b57cec5SDimitry Andric }
3460b57cec5SDimitry Andric return reg_ctx_sp;
3470b57cec5SDimitry Andric }
3480b57cec5SDimitry Andric
3490b57cec5SDimitry Andric StopInfoSP
CreateThreadStopReason(lldb_private::Thread * thread)3500b57cec5SDimitry Andric OperatingSystemPython::CreateThreadStopReason(lldb_private::Thread *thread) {
3510b57cec5SDimitry Andric // We should have gotten the thread stop info from the dictionary of data for
3520b57cec5SDimitry Andric // the thread in the initial call to get_thread_info(), this should have been
3530b57cec5SDimitry Andric // cached so we can return it here
3540b57cec5SDimitry Andric StopInfoSP
3550b57cec5SDimitry Andric stop_info_sp; //(StopInfo::CreateStopReasonWithSignal (*thread, SIGSTOP));
3560b57cec5SDimitry Andric return stop_info_sp;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric
CreateThread(lldb::tid_t tid,addr_t context)3590b57cec5SDimitry Andric lldb::ThreadSP OperatingSystemPython::CreateThread(lldb::tid_t tid,
3600b57cec5SDimitry Andric addr_t context) {
36181ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Thread);
3620b57cec5SDimitry Andric
3639dba64beSDimitry Andric LLDB_LOGF(log,
3649dba64beSDimitry Andric "OperatingSystemPython::CreateThread (tid = 0x%" PRIx64
3650b57cec5SDimitry Andric ", context = 0x%" PRIx64 ") fetching register data from python",
3660b57cec5SDimitry Andric tid, context);
3670b57cec5SDimitry Andric
368*c9157d92SDimitry Andric if (m_interpreter && m_script_object_sp) {
3690b57cec5SDimitry Andric
3700b57cec5SDimitry Andric StructuredData::DictionarySP thread_info_dict =
371*c9157d92SDimitry Andric m_operating_system_interface_sp->CreateThread(tid, context);
372*c9157d92SDimitry Andric
3730b57cec5SDimitry Andric std::vector<bool> core_used_map;
3740b57cec5SDimitry Andric if (thread_info_dict) {
3750b57cec5SDimitry Andric ThreadList core_threads(m_process);
3760b57cec5SDimitry Andric ThreadList &thread_list = m_process->GetThreadList();
3770b57cec5SDimitry Andric bool did_create = false;
3780b57cec5SDimitry Andric ThreadSP thread_sp(
3790b57cec5SDimitry Andric CreateThreadFromThreadInfo(*thread_info_dict, core_threads,
3800b57cec5SDimitry Andric thread_list, core_used_map, &did_create));
3810b57cec5SDimitry Andric if (did_create)
3820b57cec5SDimitry Andric thread_list.AddThread(thread_sp);
3830b57cec5SDimitry Andric return thread_sp;
3840b57cec5SDimitry Andric }
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric return ThreadSP();
3870b57cec5SDimitry Andric }
3880b57cec5SDimitry Andric
389480093f4SDimitry Andric #endif // #if LLDB_ENABLE_PYTHON
390