xref: /src/contrib/llvm-project/lldb/source/Plugins/Process/Utility/ThreadMemory.cpp (revision 972a253a57b6f144b0e4a3e2080a2a0076ec55a0)
1cfca06d7SDimitry Andric //===-- ThreadMemory.cpp --------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
9f034231aSEd Maste #include "Plugins/Process/Utility/ThreadMemory.h"
105f29bb8aSDimitry Andric 
1114f1b3e8SDimitry Andric #include "Plugins/Process/Utility/RegisterContextThreadMemory.h"
12f034231aSEd Maste #include "lldb/Target/OperatingSystem.h"
13f034231aSEd Maste #include "lldb/Target/Process.h"
1414f1b3e8SDimitry Andric #include "lldb/Target/RegisterContext.h"
15f034231aSEd Maste #include "lldb/Target/StopInfo.h"
16f034231aSEd Maste #include "lldb/Target/Unwind.h"
17f034231aSEd Maste 
185f29bb8aSDimitry Andric #include <memory>
195f29bb8aSDimitry Andric 
20f034231aSEd Maste using namespace lldb;
21f034231aSEd Maste using namespace lldb_private;
22f034231aSEd Maste 
ThreadMemory(Process & process,tid_t tid,const ValueObjectSP & thread_info_valobj_sp)2314f1b3e8SDimitry Andric ThreadMemory::ThreadMemory(Process &process, tid_t tid,
2414f1b3e8SDimitry Andric                            const ValueObjectSP &thread_info_valobj_sp)
2514f1b3e8SDimitry Andric     : Thread(process, tid), m_backing_thread_sp(),
2608e8dd7bSDimitry Andric       m_thread_info_valobj_sp(thread_info_valobj_sp), m_name(), m_queue(),
2708e8dd7bSDimitry Andric       m_register_data_addr(LLDB_INVALID_ADDRESS) {}
28f034231aSEd Maste 
ThreadMemory(Process & process,lldb::tid_t tid,llvm::StringRef name,llvm::StringRef queue,lldb::addr_t register_data_addr)29b76161e4SDimitry Andric ThreadMemory::ThreadMemory(Process &process, lldb::tid_t tid,
30b76161e4SDimitry Andric                            llvm::StringRef name, llvm::StringRef queue,
31b76161e4SDimitry Andric                            lldb::addr_t register_data_addr)
3214f1b3e8SDimitry Andric     : Thread(process, tid), m_backing_thread_sp(), m_thread_info_valobj_sp(),
33cfca06d7SDimitry Andric       m_name(std::string(name)), m_queue(std::string(queue)),
34cfca06d7SDimitry Andric       m_register_data_addr(register_data_addr) {}
35f034231aSEd Maste 
~ThreadMemory()3614f1b3e8SDimitry Andric ThreadMemory::~ThreadMemory() { DestroyThread(); }
37f034231aSEd Maste 
WillResume(StateType resume_state)3814f1b3e8SDimitry Andric void ThreadMemory::WillResume(StateType resume_state) {
39f034231aSEd Maste   if (m_backing_thread_sp)
40f034231aSEd Maste     m_backing_thread_sp->WillResume(resume_state);
41f034231aSEd Maste }
42f034231aSEd Maste 
ClearStackFrames()4314f1b3e8SDimitry Andric void ThreadMemory::ClearStackFrames() {
44f034231aSEd Maste   if (m_backing_thread_sp)
45f034231aSEd Maste     m_backing_thread_sp->ClearStackFrames();
46f034231aSEd Maste   Thread::ClearStackFrames();
47f034231aSEd Maste }
48f034231aSEd Maste 
GetRegisterContext()4914f1b3e8SDimitry Andric RegisterContextSP ThreadMemory::GetRegisterContext() {
50f034231aSEd Maste   if (!m_reg_context_sp)
515f29bb8aSDimitry Andric     m_reg_context_sp = std::make_shared<RegisterContextThreadMemory>(
525f29bb8aSDimitry Andric         *this, m_register_data_addr);
53f034231aSEd Maste   return m_reg_context_sp;
54f034231aSEd Maste }
55f034231aSEd Maste 
56f034231aSEd Maste RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)5714f1b3e8SDimitry Andric ThreadMemory::CreateRegisterContextForFrame(StackFrame *frame) {
58f034231aSEd Maste   uint32_t concrete_frame_idx = 0;
59f034231aSEd Maste 
60f034231aSEd Maste   if (frame)
61f034231aSEd Maste     concrete_frame_idx = frame->GetConcreteFrameIndex();
62f034231aSEd Maste 
63cfca06d7SDimitry Andric   if (concrete_frame_idx == 0)
64cfca06d7SDimitry Andric     return GetRegisterContext();
65cfca06d7SDimitry Andric   return GetUnwinder().CreateRegisterContextForFrame(frame);
66f034231aSEd Maste }
67f034231aSEd Maste 
CalculateStopInfo()6814f1b3e8SDimitry Andric bool ThreadMemory::CalculateStopInfo() {
6914f1b3e8SDimitry Andric   if (m_backing_thread_sp) {
7014f1b3e8SDimitry Andric     lldb::StopInfoSP backing_stop_info_sp(
7114f1b3e8SDimitry Andric         m_backing_thread_sp->GetPrivateStopInfo());
7214f1b3e8SDimitry Andric     if (backing_stop_info_sp &&
7314f1b3e8SDimitry Andric         backing_stop_info_sp->IsValidForOperatingSystemThread(*this)) {
74f034231aSEd Maste       backing_stop_info_sp->SetThread(shared_from_this());
75f034231aSEd Maste       SetStopInfo(backing_stop_info_sp);
76f034231aSEd Maste       return true;
77f034231aSEd Maste     }
7814f1b3e8SDimitry Andric   } else {
79f034231aSEd Maste     ProcessSP process_sp(GetProcess());
80f034231aSEd Maste 
8114f1b3e8SDimitry Andric     if (process_sp) {
82f034231aSEd Maste       OperatingSystem *os = process_sp->GetOperatingSystem();
8314f1b3e8SDimitry Andric       if (os) {
84f034231aSEd Maste         SetStopInfo(os->CreateThreadStopReason(this));
85f034231aSEd Maste         return true;
86f034231aSEd Maste       }
87f034231aSEd Maste     }
88f034231aSEd Maste   }
89f034231aSEd Maste   return false;
90f034231aSEd Maste }
91f034231aSEd Maste 
RefreshStateAfterStop()9214f1b3e8SDimitry Andric void ThreadMemory::RefreshStateAfterStop() {
93f034231aSEd Maste   if (m_backing_thread_sp)
94f034231aSEd Maste     return m_backing_thread_sp->RefreshStateAfterStop();
95f034231aSEd Maste 
96f034231aSEd Maste   if (m_reg_context_sp)
97f034231aSEd Maste     m_reg_context_sp->InvalidateAllRegisters();
98f034231aSEd Maste }
99