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