1 //===-- ThreadMemory.cpp ----------------------------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "Plugins/Process/Utility/ThreadMemory.h"
12 #include "Plugins/Process/Utility/RegisterContextThreadMemory.h"
13 #include "lldb/Target/OperatingSystem.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/StopInfo.h"
17 #include "lldb/Target/Unwind.h"
22 using namespace lldb_private;
24 ThreadMemory::ThreadMemory(Process &process, tid_t tid,
25 const ValueObjectSP &thread_info_valobj_sp)
26 : Thread(process, tid), m_backing_thread_sp(),
27 m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue() {}
29 ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
30 llvm::StringRef name, llvm::StringRef queue,
31 lldb::addr_t register_data_addr)
32 : Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
33 m_name(name), m_queue(queue), m_register_data_addr(register_data_addr) {}
35 ThreadMemory::~ThreadMemory() { DestroyThread(); }
37 void ThreadMemory::WillResume(StateType resume_state) {
38 if (m_backing_thread_sp)
39 m_backing_thread_sp->WillResume(resume_state);
42 void ThreadMemory::ClearStackFrames() {
43 if (m_backing_thread_sp)
44 m_backing_thread_sp->ClearStackFrames();
45 Thread::ClearStackFrames();
48 RegisterContextSP ThreadMemory::GetRegisterContext() {
49 if (!m_reg_context_sp)
50 m_reg_context_sp = std::make_shared<RegisterContextThreadMemory>(
51 *this, m_register_data_addr);
52 return m_reg_context_sp;
56 ThreadMemory::CreateRegisterContextForFrame(StackFrame *frame) {
57 RegisterContextSP reg_ctx_sp;
58 uint32_t concrete_frame_idx = 0;
61 concrete_frame_idx = frame->GetConcreteFrameIndex();
63 if (concrete_frame_idx == 0) {
64 reg_ctx_sp = GetRegisterContext();
66 Unwind *unwinder = GetUnwinder();
67 if (unwinder != nullptr)
68 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
73 bool ThreadMemory::CalculateStopInfo() {
74 if (m_backing_thread_sp) {
75 lldb::StopInfoSP backing_stop_info_sp(
76 m_backing_thread_sp->GetPrivateStopInfo());
77 if (backing_stop_info_sp &&
78 backing_stop_info_sp->IsValidForOperatingSystemThread(*this)) {
79 backing_stop_info_sp->SetThread(shared_from_this());
80 SetStopInfo(backing_stop_info_sp);
84 ProcessSP process_sp(GetProcess());
87 OperatingSystem *os = process_sp->GetOperatingSystem();
89 SetStopInfo(os->CreateThreadStopReason(this));
97 void ThreadMemory::RefreshStateAfterStop() {
98 if (m_backing_thread_sp)
99 return m_backing_thread_sp->RefreshStateAfterStop();
101 if (m_reg_context_sp)
102 m_reg_context_sp->InvalidateAllRegisters();