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 
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"
18 
19 #include <memory>
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
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() {}
28 
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) {}
34 
35 ThreadMemory::~ThreadMemory() { DestroyThread(); }
36 
37 void ThreadMemory::WillResume(StateType resume_state) {
38   if (m_backing_thread_sp)
39     m_backing_thread_sp->WillResume(resume_state);
40 }
41 
42 void ThreadMemory::ClearStackFrames() {
43   if (m_backing_thread_sp)
44     m_backing_thread_sp->ClearStackFrames();
45   Thread::ClearStackFrames();
46 }
47 
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;
53 }
54 
55 RegisterContextSP
56 ThreadMemory::CreateRegisterContextForFrame(StackFrame *frame) {
57   RegisterContextSP reg_ctx_sp;
58   uint32_t concrete_frame_idx = 0;
59 
60   if (frame)
61     concrete_frame_idx = frame->GetConcreteFrameIndex();
62 
63   if (concrete_frame_idx == 0) {
64     reg_ctx_sp = GetRegisterContext();
65   } else {
66     Unwind *unwinder = GetUnwinder();
67     if (unwinder != nullptr)
68       reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
69   }
70   return reg_ctx_sp;
71 }
72 
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);
81       return true;
82     }
83   } else {
84     ProcessSP process_sp(GetProcess());
85 
86     if (process_sp) {
87       OperatingSystem *os = process_sp->GetOperatingSystem();
88       if (os) {
89         SetStopInfo(os->CreateThreadStopReason(this));
90         return true;
91       }
92     }
93   }
94   return false;
95 }
96 
97 void ThreadMemory::RefreshStateAfterStop() {
98   if (m_backing_thread_sp)
99     return m_backing_thread_sp->RefreshStateAfterStop();
100 
101   if (m_reg_context_sp)
102     m_reg_context_sp->InvalidateAllRegisters();
103 }
104