1435933ddSDimitry Andric //===-- ThreadMemory.cpp ----------------------------------------------*- C++
2435933ddSDimitry Andric //-*-===//
3ac7ddfbfSEd Maste //
4ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
5ac7ddfbfSEd Maste //
6ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
7ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
8ac7ddfbfSEd Maste //
9ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
10ac7ddfbfSEd Maste 
11ac7ddfbfSEd Maste #include "Plugins/Process/Utility/ThreadMemory.h"
12435933ddSDimitry Andric #include "Plugins/Process/Utility/RegisterContextThreadMemory.h"
13ac7ddfbfSEd Maste #include "lldb/Target/OperatingSystem.h"
14ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
15435933ddSDimitry Andric #include "lldb/Target/RegisterContext.h"
16ac7ddfbfSEd Maste #include "lldb/Target/StopInfo.h"
17ac7ddfbfSEd Maste #include "lldb/Target/Unwind.h"
18ac7ddfbfSEd Maste 
19ac7ddfbfSEd Maste using namespace lldb;
20ac7ddfbfSEd Maste using namespace lldb_private;
21ac7ddfbfSEd Maste 
ThreadMemory(Process & process,tid_t tid,const ValueObjectSP & thread_info_valobj_sp)22435933ddSDimitry Andric ThreadMemory::ThreadMemory(Process &process, tid_t tid,
23435933ddSDimitry Andric                            const ValueObjectSP &thread_info_valobj_sp)
24435933ddSDimitry Andric     : Thread(process, tid), m_backing_thread_sp(),
25435933ddSDimitry Andric       m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue() {}
26ac7ddfbfSEd Maste 
ThreadMemory(Process & process,lldb::tid_t tid,llvm::StringRef name,llvm::StringRef queue,lldb::addr_t register_data_addr)275517e702SDimitry Andric ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
285517e702SDimitry Andric                            llvm::StringRef name, llvm::StringRef queue,
295517e702SDimitry Andric                            lldb::addr_t register_data_addr)
30435933ddSDimitry Andric     : Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
315517e702SDimitry Andric       m_name(name), m_queue(queue), m_register_data_addr(register_data_addr) {}
32ac7ddfbfSEd Maste 
~ThreadMemory()33435933ddSDimitry Andric ThreadMemory::~ThreadMemory() { DestroyThread(); }
34ac7ddfbfSEd Maste 
WillResume(StateType resume_state)35435933ddSDimitry Andric void ThreadMemory::WillResume(StateType resume_state) {
36ac7ddfbfSEd Maste   if (m_backing_thread_sp)
37ac7ddfbfSEd Maste     m_backing_thread_sp->WillResume(resume_state);
38ac7ddfbfSEd Maste }
39ac7ddfbfSEd Maste 
ClearStackFrames()40435933ddSDimitry Andric void ThreadMemory::ClearStackFrames() {
41ac7ddfbfSEd Maste   if (m_backing_thread_sp)
42ac7ddfbfSEd Maste     m_backing_thread_sp->ClearStackFrames();
43ac7ddfbfSEd Maste   Thread::ClearStackFrames();
44ac7ddfbfSEd Maste }
45ac7ddfbfSEd Maste 
GetRegisterContext()46435933ddSDimitry Andric RegisterContextSP ThreadMemory::GetRegisterContext() {
47ac7ddfbfSEd Maste   if (!m_reg_context_sp)
48435933ddSDimitry Andric     m_reg_context_sp.reset(
49435933ddSDimitry Andric         new RegisterContextThreadMemory(*this, m_register_data_addr));
50ac7ddfbfSEd Maste   return m_reg_context_sp;
51ac7ddfbfSEd Maste }
52ac7ddfbfSEd Maste 
53ac7ddfbfSEd Maste RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)54435933ddSDimitry Andric ThreadMemory::CreateRegisterContextForFrame(StackFrame *frame) {
55ac7ddfbfSEd Maste   RegisterContextSP reg_ctx_sp;
56ac7ddfbfSEd Maste   uint32_t concrete_frame_idx = 0;
57ac7ddfbfSEd Maste 
58ac7ddfbfSEd Maste   if (frame)
59ac7ddfbfSEd Maste     concrete_frame_idx = frame->GetConcreteFrameIndex();
60ac7ddfbfSEd Maste 
61435933ddSDimitry Andric   if (concrete_frame_idx == 0) {
62ac7ddfbfSEd Maste     reg_ctx_sp = GetRegisterContext();
63435933ddSDimitry Andric   } else {
64ac7ddfbfSEd Maste     Unwind *unwinder = GetUnwinder();
65*4ba319b5SDimitry Andric     if (unwinder != nullptr)
66ac7ddfbfSEd Maste       reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
67ac7ddfbfSEd Maste   }
68ac7ddfbfSEd Maste   return reg_ctx_sp;
69ac7ddfbfSEd Maste }
70ac7ddfbfSEd Maste 
CalculateStopInfo()71435933ddSDimitry Andric bool ThreadMemory::CalculateStopInfo() {
72435933ddSDimitry Andric   if (m_backing_thread_sp) {
73435933ddSDimitry Andric     lldb::StopInfoSP backing_stop_info_sp(
74435933ddSDimitry Andric         m_backing_thread_sp->GetPrivateStopInfo());
75435933ddSDimitry Andric     if (backing_stop_info_sp &&
76435933ddSDimitry Andric         backing_stop_info_sp->IsValidForOperatingSystemThread(*this)) {
77ac7ddfbfSEd Maste       backing_stop_info_sp->SetThread(shared_from_this());
78ac7ddfbfSEd Maste       SetStopInfo(backing_stop_info_sp);
79ac7ddfbfSEd Maste       return true;
80ac7ddfbfSEd Maste     }
81435933ddSDimitry Andric   } else {
82ac7ddfbfSEd Maste     ProcessSP process_sp(GetProcess());
83ac7ddfbfSEd Maste 
84435933ddSDimitry Andric     if (process_sp) {
85ac7ddfbfSEd Maste       OperatingSystem *os = process_sp->GetOperatingSystem();
86435933ddSDimitry Andric       if (os) {
87ac7ddfbfSEd Maste         SetStopInfo(os->CreateThreadStopReason(this));
88ac7ddfbfSEd Maste         return true;
89ac7ddfbfSEd Maste       }
90ac7ddfbfSEd Maste     }
91ac7ddfbfSEd Maste   }
92ac7ddfbfSEd Maste   return false;
93ac7ddfbfSEd Maste }
94ac7ddfbfSEd Maste 
RefreshStateAfterStop()95435933ddSDimitry Andric void ThreadMemory::RefreshStateAfterStop() {
96ac7ddfbfSEd Maste   if (m_backing_thread_sp)
97ac7ddfbfSEd Maste     return m_backing_thread_sp->RefreshStateAfterStop();
98ac7ddfbfSEd Maste 
99ac7ddfbfSEd Maste   if (m_reg_context_sp)
100ac7ddfbfSEd Maste     m_reg_context_sp->InvalidateAllRegisters();
101ac7ddfbfSEd Maste }
102