1*0b57cec5SDimitry Andric //===-- RegisterContextThreadMemory.cpp -----------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/OperatingSystem.h"
10*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
11*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
12*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
13*0b57cec5SDimitry Andric #include "lldb/lldb-private.h"
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "RegisterContextThreadMemory.h"
16*0b57cec5SDimitry Andric 
17*0b57cec5SDimitry Andric using namespace lldb;
18*0b57cec5SDimitry Andric using namespace lldb_private;
19*0b57cec5SDimitry Andric 
RegisterContextThreadMemory(Thread & thread,lldb::addr_t register_data_addr)20*0b57cec5SDimitry Andric RegisterContextThreadMemory::RegisterContextThreadMemory(
21*0b57cec5SDimitry Andric     Thread &thread, lldb::addr_t register_data_addr)
22*0b57cec5SDimitry Andric     : RegisterContext(thread, 0), m_thread_wp(thread.shared_from_this()),
23*0b57cec5SDimitry Andric       m_reg_ctx_sp(), m_register_data_addr(register_data_addr), m_stop_id(0) {}
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric RegisterContextThreadMemory::~RegisterContextThreadMemory() = default;
26*0b57cec5SDimitry Andric 
UpdateRegisterContext()27*0b57cec5SDimitry Andric void RegisterContextThreadMemory::UpdateRegisterContext() {
28*0b57cec5SDimitry Andric   ThreadSP thread_sp(m_thread_wp.lock());
29*0b57cec5SDimitry Andric   if (thread_sp) {
30*0b57cec5SDimitry Andric     ProcessSP process_sp(thread_sp->GetProcess());
31*0b57cec5SDimitry Andric 
32*0b57cec5SDimitry Andric     if (process_sp) {
33*0b57cec5SDimitry Andric       const uint32_t stop_id = process_sp->GetModID().GetStopID();
34*0b57cec5SDimitry Andric       if (m_stop_id != stop_id) {
35*0b57cec5SDimitry Andric         m_stop_id = stop_id;
36*0b57cec5SDimitry Andric         m_reg_ctx_sp.reset();
37*0b57cec5SDimitry Andric       }
38*0b57cec5SDimitry Andric       if (!m_reg_ctx_sp) {
39*0b57cec5SDimitry Andric         ThreadSP backing_thread_sp(thread_sp->GetBackingThread());
40*0b57cec5SDimitry Andric         if (backing_thread_sp) {
41*0b57cec5SDimitry Andric           m_reg_ctx_sp = backing_thread_sp->GetRegisterContext();
42*0b57cec5SDimitry Andric         } else {
43*0b57cec5SDimitry Andric           OperatingSystem *os = process_sp->GetOperatingSystem();
44*0b57cec5SDimitry Andric           if (os->IsOperatingSystemPluginThread(thread_sp))
45*0b57cec5SDimitry Andric             m_reg_ctx_sp = os->CreateRegisterContextForThread(
46*0b57cec5SDimitry Andric                 thread_sp.get(), m_register_data_addr);
47*0b57cec5SDimitry Andric         }
48*0b57cec5SDimitry Andric       }
49*0b57cec5SDimitry Andric     } else {
50*0b57cec5SDimitry Andric       m_reg_ctx_sp.reset();
51*0b57cec5SDimitry Andric     }
52*0b57cec5SDimitry Andric   } else {
53*0b57cec5SDimitry Andric     m_reg_ctx_sp.reset();
54*0b57cec5SDimitry Andric   }
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric // Subclasses must override these functions
InvalidateAllRegisters()58*0b57cec5SDimitry Andric void RegisterContextThreadMemory::InvalidateAllRegisters() {
59*0b57cec5SDimitry Andric   UpdateRegisterContext();
60*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
61*0b57cec5SDimitry Andric     m_reg_ctx_sp->InvalidateAllRegisters();
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric 
GetRegisterCount()64*0b57cec5SDimitry Andric size_t RegisterContextThreadMemory::GetRegisterCount() {
65*0b57cec5SDimitry Andric   UpdateRegisterContext();
66*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
67*0b57cec5SDimitry Andric     return m_reg_ctx_sp->GetRegisterCount();
68*0b57cec5SDimitry Andric   return 0;
69*0b57cec5SDimitry Andric }
70*0b57cec5SDimitry Andric 
71*0b57cec5SDimitry Andric const RegisterInfo *
GetRegisterInfoAtIndex(size_t reg)72*0b57cec5SDimitry Andric RegisterContextThreadMemory::GetRegisterInfoAtIndex(size_t reg) {
73*0b57cec5SDimitry Andric   UpdateRegisterContext();
74*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
75*0b57cec5SDimitry Andric     return m_reg_ctx_sp->GetRegisterInfoAtIndex(reg);
76*0b57cec5SDimitry Andric   return nullptr;
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
GetRegisterSetCount()79*0b57cec5SDimitry Andric size_t RegisterContextThreadMemory::GetRegisterSetCount() {
80*0b57cec5SDimitry Andric   UpdateRegisterContext();
81*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
82*0b57cec5SDimitry Andric     return m_reg_ctx_sp->GetRegisterSetCount();
83*0b57cec5SDimitry Andric   return 0;
84*0b57cec5SDimitry Andric }
85*0b57cec5SDimitry Andric 
GetRegisterSet(size_t reg_set)86*0b57cec5SDimitry Andric const RegisterSet *RegisterContextThreadMemory::GetRegisterSet(size_t reg_set) {
87*0b57cec5SDimitry Andric   UpdateRegisterContext();
88*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
89*0b57cec5SDimitry Andric     return m_reg_ctx_sp->GetRegisterSet(reg_set);
90*0b57cec5SDimitry Andric   return nullptr;
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric 
ReadRegister(const RegisterInfo * reg_info,RegisterValue & reg_value)93*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::ReadRegister(const RegisterInfo *reg_info,
94*0b57cec5SDimitry Andric                                                RegisterValue &reg_value) {
95*0b57cec5SDimitry Andric   UpdateRegisterContext();
96*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
97*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ReadRegister(reg_info, reg_value);
98*0b57cec5SDimitry Andric   return false;
99*0b57cec5SDimitry Andric }
100*0b57cec5SDimitry Andric 
WriteRegister(const RegisterInfo * reg_info,const RegisterValue & reg_value)101*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::WriteRegister(
102*0b57cec5SDimitry Andric     const RegisterInfo *reg_info, const RegisterValue &reg_value) {
103*0b57cec5SDimitry Andric   UpdateRegisterContext();
104*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
105*0b57cec5SDimitry Andric     return m_reg_ctx_sp->WriteRegister(reg_info, reg_value);
106*0b57cec5SDimitry Andric   return false;
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric 
ReadAllRegisterValues(lldb::WritableDataBufferSP & data_sp)109*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::ReadAllRegisterValues(
110*0b57cec5SDimitry Andric     lldb::WritableDataBufferSP &data_sp) {
111*0b57cec5SDimitry Andric   UpdateRegisterContext();
112*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
113*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ReadAllRegisterValues(data_sp);
114*0b57cec5SDimitry Andric   return false;
115*0b57cec5SDimitry Andric }
116*0b57cec5SDimitry Andric 
WriteAllRegisterValues(const lldb::DataBufferSP & data_sp)117*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::WriteAllRegisterValues(
118*0b57cec5SDimitry Andric     const lldb::DataBufferSP &data_sp) {
119*0b57cec5SDimitry Andric   UpdateRegisterContext();
120*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
121*0b57cec5SDimitry Andric     return m_reg_ctx_sp->WriteAllRegisterValues(data_sp);
122*0b57cec5SDimitry Andric   return false;
123*0b57cec5SDimitry Andric }
124*0b57cec5SDimitry Andric 
CopyFromRegisterContext(lldb::RegisterContextSP reg_ctx_sp)125*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::CopyFromRegisterContext(
126*0b57cec5SDimitry Andric     lldb::RegisterContextSP reg_ctx_sp) {
127*0b57cec5SDimitry Andric   UpdateRegisterContext();
128*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
129*0b57cec5SDimitry Andric     return m_reg_ctx_sp->CopyFromRegisterContext(reg_ctx_sp);
130*0b57cec5SDimitry Andric   return false;
131*0b57cec5SDimitry Andric }
132*0b57cec5SDimitry Andric 
ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind,uint32_t num)133*0b57cec5SDimitry Andric uint32_t RegisterContextThreadMemory::ConvertRegisterKindToRegisterNumber(
134*0b57cec5SDimitry Andric     lldb::RegisterKind kind, uint32_t num) {
135*0b57cec5SDimitry Andric   UpdateRegisterContext();
136*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
137*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ConvertRegisterKindToRegisterNumber(kind, num);
138*0b57cec5SDimitry Andric   return false;
139*0b57cec5SDimitry Andric }
140*0b57cec5SDimitry Andric 
NumSupportedHardwareBreakpoints()141*0b57cec5SDimitry Andric uint32_t RegisterContextThreadMemory::NumSupportedHardwareBreakpoints() {
142*0b57cec5SDimitry Andric   UpdateRegisterContext();
143*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
144*0b57cec5SDimitry Andric     return m_reg_ctx_sp->NumSupportedHardwareBreakpoints();
145*0b57cec5SDimitry Andric   return false;
146*0b57cec5SDimitry Andric }
147*0b57cec5SDimitry Andric 
SetHardwareBreakpoint(lldb::addr_t addr,size_t size)148*0b57cec5SDimitry Andric uint32_t RegisterContextThreadMemory::SetHardwareBreakpoint(lldb::addr_t addr,
149*0b57cec5SDimitry Andric                                                             size_t size) {
150*0b57cec5SDimitry Andric   UpdateRegisterContext();
151*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
152*0b57cec5SDimitry Andric     return m_reg_ctx_sp->SetHardwareBreakpoint(addr, size);
153*0b57cec5SDimitry Andric   return 0;
154*0b57cec5SDimitry Andric }
155*0b57cec5SDimitry Andric 
ClearHardwareBreakpoint(uint32_t hw_idx)156*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::ClearHardwareBreakpoint(uint32_t hw_idx) {
157*0b57cec5SDimitry Andric   UpdateRegisterContext();
158*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
159*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ClearHardwareBreakpoint(hw_idx);
160*0b57cec5SDimitry Andric   return false;
161*0b57cec5SDimitry Andric }
162*0b57cec5SDimitry Andric 
NumSupportedHardwareWatchpoints()163*0b57cec5SDimitry Andric uint32_t RegisterContextThreadMemory::NumSupportedHardwareWatchpoints() {
164*0b57cec5SDimitry Andric   UpdateRegisterContext();
165*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
166*0b57cec5SDimitry Andric     return m_reg_ctx_sp->NumSupportedHardwareWatchpoints();
167*0b57cec5SDimitry Andric   return 0;
168*0b57cec5SDimitry Andric }
169*0b57cec5SDimitry Andric 
SetHardwareWatchpoint(lldb::addr_t addr,size_t size,bool read,bool write)170*0b57cec5SDimitry Andric uint32_t RegisterContextThreadMemory::SetHardwareWatchpoint(lldb::addr_t addr,
171*0b57cec5SDimitry Andric                                                             size_t size,
172*0b57cec5SDimitry Andric                                                             bool read,
173*0b57cec5SDimitry Andric                                                             bool write) {
174*0b57cec5SDimitry Andric   UpdateRegisterContext();
175*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
176*0b57cec5SDimitry Andric     return m_reg_ctx_sp->SetHardwareWatchpoint(addr, size, read, write);
177*0b57cec5SDimitry Andric   return 0;
178*0b57cec5SDimitry Andric }
179*0b57cec5SDimitry Andric 
ClearHardwareWatchpoint(uint32_t hw_index)180*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::ClearHardwareWatchpoint(uint32_t hw_index) {
181*0b57cec5SDimitry Andric   UpdateRegisterContext();
182*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
183*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ClearHardwareWatchpoint(hw_index);
184*0b57cec5SDimitry Andric   return false;
185*0b57cec5SDimitry Andric }
186*0b57cec5SDimitry Andric 
HardwareSingleStep(bool enable)187*0b57cec5SDimitry Andric bool RegisterContextThreadMemory::HardwareSingleStep(bool enable) {
188*0b57cec5SDimitry Andric   UpdateRegisterContext();
189*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
190*0b57cec5SDimitry Andric     return m_reg_ctx_sp->HardwareSingleStep(enable);
191*0b57cec5SDimitry Andric   return false;
192*0b57cec5SDimitry Andric }
193*0b57cec5SDimitry Andric 
ReadRegisterValueFromMemory(const lldb_private::RegisterInfo * reg_info,lldb::addr_t src_addr,uint32_t src_len,RegisterValue & reg_value)194*0b57cec5SDimitry Andric Status RegisterContextThreadMemory::ReadRegisterValueFromMemory(
195*0b57cec5SDimitry Andric     const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr,
196*0b57cec5SDimitry Andric     uint32_t src_len, RegisterValue &reg_value) {
197*0b57cec5SDimitry Andric   UpdateRegisterContext();
198*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
199*0b57cec5SDimitry Andric     return m_reg_ctx_sp->ReadRegisterValueFromMemory(reg_info, src_addr,
200*0b57cec5SDimitry Andric                                                      src_len, reg_value);
201*0b57cec5SDimitry Andric   Status error;
202*0b57cec5SDimitry Andric   error.SetErrorString("invalid register context");
203*0b57cec5SDimitry Andric   return error;
204*0b57cec5SDimitry Andric }
205*0b57cec5SDimitry Andric 
WriteRegisterValueToMemory(const lldb_private::RegisterInfo * reg_info,lldb::addr_t dst_addr,uint32_t dst_len,const RegisterValue & reg_value)206*0b57cec5SDimitry Andric Status RegisterContextThreadMemory::WriteRegisterValueToMemory(
207*0b57cec5SDimitry Andric     const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr,
208*0b57cec5SDimitry Andric     uint32_t dst_len, const RegisterValue &reg_value) {
209*0b57cec5SDimitry Andric   UpdateRegisterContext();
210*0b57cec5SDimitry Andric   if (m_reg_ctx_sp)
211*0b57cec5SDimitry Andric     return m_reg_ctx_sp->WriteRegisterValueToMemory(reg_info, dst_addr, dst_len,
212*0b57cec5SDimitry Andric                                                     reg_value);
213*0b57cec5SDimitry Andric   Status error;
214*0b57cec5SDimitry Andric   error.SetErrorString("invalid register context");
215*0b57cec5SDimitry Andric   return error;
216*0b57cec5SDimitry Andric }
217