xref: /llvm-project/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===-- ThreadMemory.cpp ----------------------------------------------*- C++
2 //-*-===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Plugins/Process/Utility/ThreadMemory.h"
11 #include "Plugins/Process/Utility/RegisterContextThreadMemory.h"
12 #include "lldb/Target/OperatingSystem.h"
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/RegisterContext.h"
15 #include "lldb/Target/StopInfo.h"
16 #include "lldb/Target/Unwind.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 ThreadMemory::ThreadMemory(Process &process, tid_t tid,
22                            const ValueObjectSP &thread_info_valobj_sp)
23     : Thread(process, tid), m_backing_thread_sp(),
24       m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue() {}
25 
26 ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
27                            llvm::StringRef name, llvm::StringRef queue,
28                            lldb::addr_t register_data_addr)
29     : Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
30       m_name(name), m_queue(queue), m_register_data_addr(register_data_addr) {}
31 
32 ThreadMemory::~ThreadMemory() { DestroyThread(); }
33 
34 void ThreadMemory::WillResume(StateType resume_state) {
35   if (m_backing_thread_sp)
36     m_backing_thread_sp->WillResume(resume_state);
37 }
38 
39 void ThreadMemory::ClearStackFrames() {
40   if (m_backing_thread_sp)
41     m_backing_thread_sp->ClearStackFrames();
42   Thread::ClearStackFrames();
43 }
44 
45 RegisterContextSP ThreadMemory::GetRegisterContext() {
46   if (!m_reg_context_sp)
47     m_reg_context_sp.reset(
48         new RegisterContextThreadMemory(*this, m_register_data_addr));
49   return m_reg_context_sp;
50 }
51 
52 RegisterContextSP
53 ThreadMemory::CreateRegisterContextForFrame(StackFrame *frame) {
54   RegisterContextSP reg_ctx_sp;
55   uint32_t concrete_frame_idx = 0;
56 
57   if (frame)
58     concrete_frame_idx = frame->GetConcreteFrameIndex();
59 
60   if (concrete_frame_idx == 0) {
61     reg_ctx_sp = GetRegisterContext();
62   } else {
63     Unwind *unwinder = GetUnwinder();
64     if (unwinder != nullptr)
65       reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
66   }
67   return reg_ctx_sp;
68 }
69 
70 bool ThreadMemory::CalculateStopInfo() {
71   if (m_backing_thread_sp) {
72     lldb::StopInfoSP backing_stop_info_sp(
73         m_backing_thread_sp->GetPrivateStopInfo());
74     if (backing_stop_info_sp &&
75         backing_stop_info_sp->IsValidForOperatingSystemThread(*this)) {
76       backing_stop_info_sp->SetThread(shared_from_this());
77       SetStopInfo(backing_stop_info_sp);
78       return true;
79     }
80   } else {
81     ProcessSP process_sp(GetProcess());
82 
83     if (process_sp) {
84       OperatingSystem *os = process_sp->GetOperatingSystem();
85       if (os) {
86         SetStopInfo(os->CreateThreadStopReason(this));
87         return true;
88       }
89     }
90   }
91   return false;
92 }
93 
94 void ThreadMemory::RefreshStateAfterStop() {
95   if (m_backing_thread_sp)
96     return m_backing_thread_sp->RefreshStateAfterStop();
97 
98   if (m_reg_context_sp)
99     m_reg_context_sp->InvalidateAllRegisters();
100 }
101