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 m_read_dbreg(false) {
59 g_register_flags_detector.UpdateRegisterInfo(
60 GetRegisterInfoInterface().GetRegisterInfo(),
61 GetRegisterInfoInterface().GetRegisterCount());
62
63 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
64 ::memset(&m_hbp_regs, 0, sizeof(m_hbp_regs));
65}
66
68NativeRegisterContextFreeBSD_arm64::GetRegisterInfo() const {
69 return static_cast<RegisterInfoPOSIX_arm64 &>(*m_register_info_interface_up);
70}
71
72uint32_t NativeRegisterContextFreeBSD_arm64::GetRegisterSetCount() const {
73 return GetRegisterInfo().GetRegisterSetCount();
74}
75
76const RegisterSet *
77NativeRegisterContextFreeBSD_arm64::GetRegisterSet(uint32_t set_index) const {
78 return GetRegisterInfo().GetRegisterSet(set_index);
79}
80
81uint32_t NativeRegisterContextFreeBSD_arm64::GetUserRegisterCount() const {
82 uint32_t count = 0;
83 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
84 count += GetRegisterSet(set_index)->num_registers;
85 return count;
86}
87
88Status NativeRegisterContextFreeBSD_arm64::ReadRegisterSet(uint32_t set) {
89 switch (set) {
91 return NativeProcessFreeBSD::PtraceWrapper(PT_GETREGS, m_thread.GetID(),
92 m_reg_data.data());
95 PT_GETFPREGS, m_thread.GetID(),
96 m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));
97 }
98 llvm_unreachable("NativeRegisterContextFreeBSD_arm64::ReadRegisterSet");
99}
100
101Status NativeRegisterContextFreeBSD_arm64::WriteRegisterSet(uint32_t set) {
102 switch (set) {
104 return NativeProcessFreeBSD::PtraceWrapper(PT_SETREGS, m_thread.GetID(),
105 m_reg_data.data());
108 PT_SETFPREGS, m_thread.GetID(),
109 m_reg_data.data() + sizeof(RegisterInfoPOSIX_arm64::GPR));
110 }
111 llvm_unreachable("NativeRegisterContextFreeBSD_arm64::WriteRegisterSet");
112}
113
114Status
115NativeRegisterContextFreeBSD_arm64::ReadRegister(const RegisterInfo *reg_info,
116 RegisterValue &reg_value) {
118
119 if (!reg_info)
120 return Status::FromErrorString("reg_info NULL");
121
122 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
123
124 if (reg == LLDB_INVALID_REGNUM)
126 "no lldb regnum for %s",
127 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
128
129 uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);
130 error = ReadRegisterSet(set);
131 if (error.Fail())
132 return error;
133
134 assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());
135 reg_value.SetBytes(m_reg_data.data() + reg_info->byte_offset,
137 return error;
138}
139
140Status NativeRegisterContextFreeBSD_arm64::WriteRegister(
141 const RegisterInfo *reg_info, const RegisterValue &reg_value) {
143
144 if (!reg_info)
145 return Status::FromErrorString("reg_info NULL");
146
147 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
148
149 if (reg == LLDB_INVALID_REGNUM)
151 "no lldb regnum for %s",
152 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
153
154 uint32_t set = GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg);
155 error = ReadRegisterSet(set);
156 if (error.Fail())
157 return error;
158
159 assert(reg_info->byte_offset + reg_info->byte_size <= m_reg_data.size());
160 ::memcpy(m_reg_data.data() + reg_info->byte_offset, reg_value.GetBytes(),
161 reg_info->byte_size);
162
163 return WriteRegisterSet(set);
164}
165
166Status NativeRegisterContextFreeBSD_arm64::ReadAllRegisterValues(
169
170 error = ReadRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);
171 if (error.Fail())
172 return error;
173
174 error = ReadRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);
175 if (error.Fail())
176 return error;
177
178 data_sp.reset(new DataBufferHeap(m_reg_data.size(), 0));
179 uint8_t *dst = data_sp->GetBytes();
180 ::memcpy(dst, m_reg_data.data(), m_reg_data.size());
181
182 return error;
183}
184
185Status NativeRegisterContextFreeBSD_arm64::WriteAllRegisterValues(
186 const lldb::DataBufferSP &data_sp) {
188
189 if (!data_sp) {
191 "NativeRegisterContextFreeBSD_arm64::%s invalid data_sp provided",
192 __FUNCTION__);
193 return error;
194 }
195
196 if (data_sp->GetByteSize() != m_reg_data.size()) {
198 "NativeRegisterContextFreeBSD_arm64::%s data_sp contained mismatched "
199 "data size, expected %" PRIu64 ", actual %" PRIu64,
200 __FUNCTION__, m_reg_data.size(), data_sp->GetByteSize());
201 return error;
202 }
203
204 const uint8_t *src = data_sp->GetBytes();
205 if (src == nullptr) {
207 "NativeRegisterContextFreeBSD_arm64::%s "
208 "DataBuffer::GetBytes() returned a null "
209 "pointer",
210 __FUNCTION__);
211 return error;
212 }
213 ::memcpy(m_reg_data.data(), src, m_reg_data.size());
214
215 error = WriteRegisterSet(RegisterInfoPOSIX_arm64::GPRegSet);
216 if (error.Fail())
217 return error;
218
219 return WriteRegisterSet(RegisterInfoPOSIX_arm64::FPRegSet);
220}
221
222llvm::Error NativeRegisterContextFreeBSD_arm64::CopyHardwareWatchpointsFrom(
224 auto &r_source = static_cast<NativeRegisterContextFreeBSD_arm64 &>(source);
225 llvm::Error error = r_source.ReadHardwareDebugInfo();
226 if (error)
227 return error;
228
229 m_dbreg = r_source.m_dbreg;
230 m_hbp_regs = r_source.m_hbp_regs;
231 m_hwp_regs = r_source.m_hwp_regs;
232 m_max_hbp_supported = r_source.m_max_hbp_supported;
233 m_max_hwp_supported = r_source.m_max_hwp_supported;
234 m_read_dbreg = true;
235
236 // on FreeBSD this writes both breakpoints and watchpoints
237 return WriteHardwareDebugRegs(eDREGTypeWATCH);
238}
239
240llvm::Error NativeRegisterContextFreeBSD_arm64::ReadHardwareDebugInfo() {
242
243 // we're fully stateful, so no need to reread control registers ever
244 if (m_read_dbreg)
245 return llvm::Error::success();
246
248 m_thread.GetID(), &m_dbreg);
249 if (res.Fail())
250 return res.ToError();
251
252 LLDB_LOG(log, "m_dbreg read: debug_ver={0}, nbkpts={1}, nwtpts={2}",
253 m_dbreg.db_debug_ver, m_dbreg.db_nbkpts, m_dbreg.db_nwtpts);
254 m_max_hbp_supported = m_dbreg.db_nbkpts;
255 m_max_hwp_supported = m_dbreg.db_nwtpts;
256 assert(m_max_hbp_supported <= m_hbp_regs.size());
257 assert(m_max_hwp_supported <= m_hwp_regs.size());
258
259 m_read_dbreg = true;
260 return llvm::Error::success();
261}
262
263llvm::Error
264NativeRegisterContextFreeBSD_arm64::WriteHardwareDebugRegs(DREGType) {
265 assert(m_read_dbreg && "dbregs must be read before writing them back");
266
267 // copy data from m_*_regs to m_dbreg before writing it back
268 for (uint32_t i = 0; i < m_max_hbp_supported; i++) {
269 m_dbreg.db_breakregs[i].dbr_addr = m_hbp_regs[i].address;
270 m_dbreg.db_breakregs[i].dbr_ctrl = m_hbp_regs[i].control;
271 }
272 for (uint32_t i = 0; i < m_max_hwp_supported; i++) {
273 m_dbreg.db_watchregs[i].dbw_addr = m_hwp_regs[i].address;
274 m_dbreg.db_watchregs[i].dbw_ctrl = m_hwp_regs[i].control;
275 }
276
277 return NativeProcessFreeBSD::PtraceWrapper(PT_SETDBREGS, m_thread.GetID(),
278 &m_dbreg)
279 .ToError();
280}
281
282#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.