LLDB mainline
NativeRegisterContextFreeBSD_arm64.cpp
Go to the documentation of this file.
1//===-- NativeRegisterContextFreeBSD_arm64.cpp ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#if defined(__aarch64__)
10
12
15#include "lldb/Utility/Status.h"
16
21
22// clang-format off
23#include <sys/param.h>
24#include <sys/ptrace.h>
25#include <sys/types.h>
26// clang-format on
27
28using namespace lldb;
29using namespace lldb_private;
30using namespace lldb_private::process_freebsd;
31
32// A NativeRegisterContext is constructed per thread, but all threads' registers
33// will contain the same fields. Therefore this mutex prevents each instance
34// competing with the other, and subsequent instances from having to detect the
35// fields all over again.
36static std::mutex g_register_flags_detector_mutex;
37static Arm64RegisterFlagsDetector g_register_flags_detector;
38
41 const ArchSpec &target_arch, NativeThreadFreeBSD &native_thread) {
42 std::lock_guard<std::mutex> lock(g_register_flags_detector_mutex);
43 if (!g_register_flags_detector.HasDetected()) {
44 NativeProcessFreeBSD &process = native_thread.GetProcess();
45 g_register_flags_detector.DetectFields(
47 process.GetAuxValue(AuxVector::AUXV_AT_HWCAP2).value_or(0),
48 /*hwcap3=*/0);
49 }
50
51 return new NativeRegisterContextFreeBSD_arm64(target_arch, native_thread);
52}
53
54NativeRegisterContextFreeBSD_arm64::NativeRegisterContextFreeBSD_arm64(
55 const ArchSpec &target_arch, NativeThreadFreeBSD &native_thread)
57 native_thread, new RegisterInfoPOSIX_arm64(target_arch, 0))
58#ifdef LLDB_HAS_FREEBSD_WATCHPOINT
59 ,
60 m_read_dbreg(false)
61#endif
62{
63 g_register_flags_detector.UpdateRegisterInfo(
64 GetRegisterInfoInterface().GetRegisterInfo(),
65 GetRegisterInfoInterface().GetRegisterCount());
66
67 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
68 ::memset(&m_hbp_regs, 0, sizeof(m_hbp_regs));
69}
70
72NativeRegisterContextFreeBSD_arm64::GetRegisterInfo() const {
73 return static_cast<RegisterInfoPOSIX_arm64 &>(*m_register_info_interface_up);
74}
75
76uint32_t NativeRegisterContextFreeBSD_arm64::GetRegisterSetCount() const {
77 return GetRegisterInfo().GetRegisterSetCount();
78}
79
80const RegisterSet *
81NativeRegisterContextFreeBSD_arm64::GetRegisterSet(uint32_t set_index) const {
82 return GetRegisterInfo().GetRegisterSet(set_index);
83}
84
85uint32_t NativeRegisterContextFreeBSD_arm64::GetUserRegisterCount() const {
86 uint32_t count = 0;
87 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
88 count += GetRegisterSet(set_index)->num_registers;
89 return count;
90}
91
92Status NativeRegisterContextFreeBSD_arm64::ReadRegisterSet(uint32_t set) {
93 switch (set) {
95 return NativeProcessFreeBSD::PtraceWrapper(PT_GETREGS, m_thread.GetID(),
96 m_reg_data.data());
99 PT_GETFPREGS, m_thread.GetID(),
100 m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));
101 }
102 llvm_unreachable("NativeRegisterContextFreeBSD_arm64::ReadRegisterSet");
103}
104
105Status NativeRegisterContextFreeBSD_arm64::WriteRegisterSet(uint32_t set) {
106 switch (set) {
108 return NativeProcessFreeBSD::PtraceWrapper(PT_SETREGS, m_thread.GetID(),
109 m_reg_data.data());
112 PT_SETFPREGS, m_thread.GetID(),
113 m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));
114 }
115 llvm_unreachable("NativeRegisterContextFreeBSD_arm64::WriteRegisterSet");
116}
117
118Status
119NativeRegisterContextFreeBSD_arm64::ReadRegister(const RegisterInfo *reg_info,
120 RegisterValue &reg_value) {
122
123 if (!reg_info)
124 return Status::FromErrorString("reg_info NULL");
125
126 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
127
128 if (reg == LLDB_INVALID_REGNUM)
130 "no lldb regnum for %s",
131 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
132
133 uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);
134 error = ReadRegisterSet(set);
135 if (error.Fail())
136 return error;
137
138 assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());
139 reg_value.SetBytes(m_reg_data.data() + reg_info->byte_offset,
141 return error;
142}
143
144Status NativeRegisterContextFreeBSD_arm64::WriteRegister(
145 const RegisterInfo *reg_info, const RegisterValue &reg_value) {
147
148 if (!reg_info)
149 return Status::FromErrorString("reg_info NULL");
150
151 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
152
153 if (reg == LLDB_INVALID_REGNUM)
155 "no lldb regnum for %s",
156 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
157
158 uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);
159 error = ReadRegisterSet(set);
160 if (error.Fail())
161 return error;
162
163 assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());
164 ::memcpy(m_reg_data.data() + reg_info->byte_offset, reg_value.GetBytes(),
165 reg_info->byte_size);
166
167 return WriteRegisterSet(set);
168}
169
170Status NativeRegisterContextFreeBSD_arm64::ReadAllRegisterValues(
173
174 error = ReadRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);
175 if (error.Fail())
176 return error;
177
178 error = ReadRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);
179 if (error.Fail())
180 return error;
181
182 data_sp.reset(new DataBufferHeap(m_reg_data.size(), 0));
183 uint8_t *dst = data_sp->GetBytes();
184 ::memcpy(dst, m_reg_data.data(), m_reg_data.size());
185
186 return error;
187}
188
189Status NativeRegisterContextFreeBSD_arm64::WriteAllRegisterValues(
190 const lldb::DataBufferSP &data_sp) {
192
193 if (!data_sp) {
195 "NativeRegisterContextFreeBSD_arm64::%s invalid data_sp provided",
196 __FUNCTION__);
197 return error;
198 }
199
200 if (data_sp->GetByteSize() != m_reg_data.size()) {
202 "NativeRegisterContextFreeBSD_arm64::%s data_sp contained mismatched "
203 "data size, expected %" PRIu64 ", actual %" PRIu64,
204 __FUNCTION__, m_reg_data.size(), data_sp->GetByteSize());
205 return error;
206 }
207
208 const uint8_t *src = data_sp->GetBytes();
209 if (src == nullptr) {
211 "NativeRegisterContextFreeBSD_arm64::%s "
212 "DataBuffer::GetBytes() returned a null "
213 "pointer",
214 __FUNCTION__);
215 return error;
216 }
217 ::memcpy(m_reg_data.data(), src, m_reg_data.size());
218
219 error = WriteRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);
220 if (error.Fail())
221 return error;
222
223 return WriteRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);
224}
225
226llvm::Error NativeRegisterContextFreeBSD_arm64::CopyHardwareWatchpointsFrom(
228#ifdef LLDB_HAS_FREEBSD_WATCHPOINT
229 auto &r_source = static_cast<NativeRegisterContextFreeBSD_arm64 &>(source);
230 llvm::Error error = r_source.ReadHardwareDebugInfo();
231 if (error)
232 return error;
233
234 m_dbreg = r_source.m_dbreg;
235 m_hbp_regs = r_source.m_hbp_regs;
236 m_hwp_regs = r_source.m_hwp_regs;
237 m_max_hbp_supported = r_source.m_max_hbp_supported;
238 m_max_hwp_supported = r_source.m_max_hwp_supported;
239 m_read_dbreg = true;
240
241 // on FreeBSD this writes both breakpoints and watchpoints
242 return WriteHardwareDebugRegs(eDREGTypeWATCH);
243#else
244 return llvm::Error::success();
245#endif
246}
247
248llvm::Error NativeRegisterContextFreeBSD_arm64::ReadHardwareDebugInfo() {
249#ifdef LLDB_HAS_FREEBSD_WATCHPOINT
251
252 // we're fully stateful, so no need to reread control registers ever
253 if (m_read_dbreg)
254 return llvm::Error::success();
255
257 m_thread.GetID(), &m_dbreg);
258 if (res.Fail())
259 return res.ToError();
260
261 LLDB_LOG(log, "m_dbreg read: debug_ver={0}, nbkpts={1}, nwtpts={2}",
262 m_dbreg.db_debug_ver, m_dbreg.db_nbkpts, m_dbreg.db_nwtpts);
263 m_max_hbp_supported = m_dbreg.db_nbkpts;
264 m_max_hwp_supported = m_dbreg.db_nwtpts;
265 assert(m_max_hbp_supported <= m_hbp_regs.size());
266 assert(m_max_hwp_supported <= m_hwp_regs.size());
267
268 m_read_dbreg = true;
269 return llvm::Error::success();
270#else
271 return llvm::createStringError(
272 llvm::inconvertibleErrorCode(),
273 "Hardware breakpoints/watchpoints require FreeBSD 14.0");
274#endif
275}
276
277llvm::Error
278NativeRegisterContextFreeBSD_arm64::WriteHardwareDebugRegs(DREGType) {
279#ifdef LLDB_HAS_FREEBSD_WATCHPOINT
280 assert(m_read_dbreg && "dbregs must be read before writing them back");
281
282 // copy data from m_*_regs to m_dbreg before writing it back
283 for (uint32_t i = 0; i < m_max_hbp_supported; i++) {
284 m_dbreg.db_breakregs[i].dbr_addr = m_hbp_regs[i].address;
285 m_dbreg.db_breakregs[i].dbr_ctrl = m_hbp_regs[i].control;
286 }
287 for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
288 m_dbreg.db_watchregs[i].dbw_addr = m_hwp_regs[i].address;
289 m_dbreg.db_watchregs[i].dbw_ctrl = m_hwp_regs[i].control;
290 }
291
292 return NativeProcessFreeBSD::PtraceWrapper(PT_SETDBREGS, m_thread.GetID(),
293 &m_dbreg)
294 .ToError();
295#else
296 return llvm::createStringError(
297 llvm::inconvertibleErrorCode(),
298 "Hardware breakpoints/watchpoints require FreeBSD 14.0");
299#endif
300}
301
302#endif // defined (__aarch64__)
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
@ AUXV_FREEBSD_AT_HWCAP
FreeBSD specific AT_HWCAP value.
Definition AuxVector.h:72
@ AUXV_AT_HWCAP2
Extension of AT_HWCAP.
Definition AuxVector.h:59
size_t GetRegisterSetCount() const override
This class manages the storage and detection of register field information.
void DetectFields(uint64_t hwcap, uint64_t hwcap2, uint64_t hwcap3)
For the registers listed in this class, detect which fields are present.
void UpdateRegisterInfo(const RegisterInfo *reg_info, uint32_t num_regs)
Add the field information of any registers named in this class, to the relevant RegisterInfo instance...
bool HasDetected() const
Returns true if field detection has been run at least once.
A subclass of DataBuffer that stores a data buffer on the heap.
std::optional< uint64_t > GetAuxValue(enum AuxVector::EntryType type)
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
const void * GetBytes() const
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
Manages communication with the inferior (debugee) process.
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, int data=0, int *result=nullptr)
static NativeRegisterContextFreeBSD * CreateHostNativeRegisterContextFreeBSD(const ArchSpec &target_arch, NativeThreadFreeBSD &native_thread)
#define LLDB_INVALID_REGNUM
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
Status WriteHardwareDebugRegs(int hwbType, ::pid_t tid, uint32_t max_supported, const std::array< NativeRegisterContextDBReg::DREG, 16 > &regs)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_offset
The byte offset in the register context data where this register's value is found.
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.
Registers are grouped into register sets.
size_t num_registers
The number of registers in REGISTERS array below.