LLDB mainline
NativeRegisterContextLinux_arm.cpp
Go to the documentation of this file.
1//===-- NativeRegisterContextLinux_arm.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(__arm__) || defined(__arm64__) || defined(__aarch64__)
10
12
17#include "lldb/Host/HostInfo.h"
19#include "lldb/Utility/Log.h"
21#include "lldb/Utility/Status.h"
22
23#include <elf.h>
24#include <sys/uio.h>
25
26#if defined(__arm64__) || defined(__aarch64__)
28#endif
29
31#include <asm/ptrace.h>
32
33#define REG_CONTEXT_SIZE (GetGPRSize() + sizeof(m_fpr) + sizeof(m_tls))
34
35#ifndef PTRACE_GETVFPREGS
36#define PTRACE_GETVFPREGS 27
37#define PTRACE_SETVFPREGS 28
38#endif
39#if defined(__arm__) && !defined(PTRACE_GETHBPREGS)
40#define PTRACE_GETHBPREGS 29
41#define PTRACE_SETHBPREGS 30
42#endif
43#if !defined(PTRACE_TYPE_ARG3)
44#define PTRACE_TYPE_ARG3 void *
45#endif
46#if !defined(PTRACE_TYPE_ARG4)
47#define PTRACE_TYPE_ARG4 void *
48#endif
49
50using namespace lldb;
51using namespace lldb_private;
52using namespace lldb_private::process_linux;
53
54#if defined(__arm__)
55
56std::unique_ptr<NativeRegisterContextLinux>
58 const ArchSpec &target_arch, NativeThreadLinux &native_thread) {
59 return std::make_unique<NativeRegisterContextLinux_arm>(target_arch,
60 native_thread);
61}
62
63llvm::Expected<ArchSpec>
65 return HostInfo::GetArchitecture();
66}
67
68#endif // defined(__arm__)
69
70NativeRegisterContextLinux_arm::NativeRegisterContextLinux_arm(
71 const ArchSpec &target_arch, NativeThreadProtocol &native_thread)
73 native_thread,
74 new RegisterInfoPOSIX_arm(target_arch, /*has_tls_reg=*/true)),
75 NativeRegisterContextLinux(native_thread) {
76 assert(target_arch.GetMachine() == llvm::Triple::arm);
77
78 ::memset(&m_fpr, 0, sizeof(m_fpr));
79 ::memset(&m_tls, 0, sizeof(m_tls));
80 ::memset(&m_gpr_arm, 0, sizeof(m_gpr_arm));
81
82 // 16 is just a maximum value, query hardware for actual watchpoint count
83 m_max_hwp_supported = 16;
84 m_max_hbp_supported = 16;
85 m_refresh_hwdebug_info = true;
86}
87
88RegisterInfoPOSIX_arm &NativeRegisterContextLinux_arm::GetRegisterInfo() const {
89 return static_cast<RegisterInfoPOSIX_arm &>(*m_register_info_interface_up);
90}
91
92uint32_t NativeRegisterContextLinux_arm::GetRegisterSetCount() const {
93 return GetRegisterInfo().GetRegisterSetCount();
94}
95
96uint32_t NativeRegisterContextLinux_arm::GetUserRegisterCount() const {
97 uint32_t count = 0;
98 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
99 count += GetRegisterSet(set_index)->num_registers;
100 return count;
101}
102
103const RegisterSet *
104NativeRegisterContextLinux_arm::GetRegisterSet(uint32_t set_index) const {
105 return GetRegisterInfo().GetRegisterSet(set_index);
106}
107
108Status
109NativeRegisterContextLinux_arm::ReadRegister(const RegisterInfo *reg_info,
110 RegisterValue &reg_value) {
112
113 if (!reg_info) {
114 error = Status::FromErrorString("reg_info NULL");
115 return error;
116 }
117
118 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
119
120 if (IsFPR(reg)) {
121 error = ReadFPR();
122 if (error.Fail())
123 return error;
124 } else if (IsTLS(reg)) {
125 error = ReadTLS();
126 if (error.Success())
127 reg_value.SetUInt32(m_tls.tpidruro);
128 return error;
129 } else {
130 uint32_t full_reg = reg;
131 bool is_subreg = reg_info->invalidate_regs &&
132 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
133
134 if (is_subreg) {
135 // Read the full aligned 64-bit register.
136 full_reg = reg_info->invalidate_regs[0];
137 }
138
139 error = ReadRegisterRaw(full_reg, reg_value);
140
141 if (error.Success()) {
142 // If our read was not aligned (for ah,bh,ch,dh), shift our returned
143 // value one byte to the right.
144 if (is_subreg && (reg_info->byte_offset & 0x1))
145 reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
146
147 // If our return byte size was greater than the return value reg size,
148 // then use the type specified by reg_info rather than the uint64_t
149 // default
150 if (reg_value.GetByteSize() > reg_info->byte_size)
151 reg_value.SetType(*reg_info);
152 }
153 return error;
154 }
155
156 // Get pointer to m_fpr variable and set the data from it.
157 uint32_t fpr_offset = CalculateFprOffset(reg_info);
158 assert(fpr_offset < sizeof m_fpr);
159 uint8_t *src = (uint8_t *)&m_fpr + fpr_offset;
160 switch (reg_info->byte_size) {
161 case 2:
162 reg_value.SetUInt16(*(uint16_t *)src);
163 break;
164 case 4:
165 reg_value.SetUInt32(*(uint32_t *)src);
166 break;
167 case 8:
168 reg_value.SetUInt64(*(uint64_t *)src);
169 break;
170 case 16:
171 reg_value.SetBytes(src, 16, GetByteOrder());
172 break;
173 default:
174 assert(false && "Unhandled data size.");
175 error = Status::FromErrorStringWithFormat("unhandled byte size: %" PRIu32,
176 reg_info->byte_size);
177 break;
178 }
179
180 return error;
181}
182
183Status
184NativeRegisterContextLinux_arm::WriteRegister(const RegisterInfo *reg_info,
185 const RegisterValue &reg_value) {
186 if (!reg_info)
187 return Status::FromErrorString("reg_info NULL");
188
189 const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
190 if (reg_index == LLDB_INVALID_REGNUM)
192 "no lldb regnum for %s",
193 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
194
195 if (IsGPR(reg_index))
196 return WriteRegisterRaw(reg_index, reg_value);
197
198 if (IsFPR(reg_index)) {
199 // Get pointer to m_fpr variable and set the data to it.
200 uint32_t fpr_offset = CalculateFprOffset(reg_info);
201 assert(fpr_offset < sizeof m_fpr);
202 uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset;
203 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
204
205 return WriteFPR();
206 }
207
208 if (IsTLS(reg_index))
210 "writing to a thread pointer register is not implemented");
211
213 "failed - register wasn't recognized to be a GPR or an FPR, "
214 "write strategy unknown");
215}
216
217Status NativeRegisterContextLinux_arm::ReadAllRegisterValues(
220
221 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
222 error = ReadGPR();
223 if (error.Fail())
224 return error;
225
226 error = ReadFPR();
227 if (error.Fail())
228 return error;
229
230 error = ReadTLS();
231 if (error.Fail())
232 return error;
233
234 uint8_t *dst = data_sp->GetBytes();
235 ::memcpy(dst, &m_gpr_arm, GetGPRSize());
236 dst += GetGPRSize();
237 ::memcpy(dst, &m_fpr, sizeof(m_fpr));
238 dst += sizeof(m_fpr);
239 ::memcpy(dst, &m_tls, sizeof(m_tls));
240
241 return error;
242}
243
244Status NativeRegisterContextLinux_arm::WriteAllRegisterValues(
245 const lldb::DataBufferSP &data_sp) {
247
248 if (!data_sp) {
250 "NativeRegisterContextLinux_arm::%s invalid data_sp provided",
251 __FUNCTION__);
252 return error;
253 }
254
255 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
257 "NativeRegisterContextLinux_arm::%s data_sp contained mismatched "
258 "data size, expected %" PRIu64 ", actual %" PRIu64,
259 __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize());
260 return error;
261 }
262
263 const uint8_t *src = data_sp->GetBytes();
264 if (src == nullptr) {
266 "NativeRegisterContextLinux_arm::%s "
267 "DataBuffer::GetBytes() returned a null "
268 "pointer",
269 __FUNCTION__);
270 return error;
271 }
272 ::memcpy(&m_gpr_arm, src, GetRegisterInfoInterface().GetGPRSize());
273
274 error = WriteGPR();
275 if (error.Fail())
276 return error;
277
278 src += GetRegisterInfoInterface().GetGPRSize();
279 ::memcpy(&m_fpr, src, sizeof(m_fpr));
280
281 error = WriteFPR();
282 if (error.Fail())
283 return error;
284
285 // Note: writing to a thread pointer register is not implemented.
286
287 return error;
288}
289
290bool NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const {
291 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
293 return true;
294 return false;
295}
296
297bool NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const {
298 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
300 return true;
301 return false;
302}
303
304bool NativeRegisterContextLinux_arm::IsTLS(unsigned reg) const {
305 return GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
307}
308
309llvm::Error NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() {
310 if (!m_refresh_hwdebug_info)
311 return llvm::Error::success();
312
313#ifdef __arm__
314 unsigned int cap_val;
316 PTRACE_GETHBPREGS, m_thread.GetID(), nullptr, &cap_val,
317 sizeof(unsigned int));
318
319 if (error.Fail())
320 return error.ToError();
321
322 m_max_hwp_supported = (cap_val >> 8) & 0xff;
323 m_max_hbp_supported = cap_val & 0xff;
324 m_refresh_hwdebug_info = false;
325
326 return error.ToError();
327#else // __aarch64__
328 return arm64::ReadHardwareDebugInfo(m_thread.GetID(), m_max_hwp_supported,
329 m_max_hbp_supported)
330 .ToError();
331#endif // ifdef __arm__
332}
333
334llvm::Error
335NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(DREGType hwbType) {
336#ifdef __arm__
337 uint32_t max_index = m_max_hbp_supported;
338 if (hwbType == eDREGTypeWATCH)
339 max_index = m_max_hwp_supported;
340
341 for (uint32_t idx = 0; idx < max_index; ++idx)
342 if (auto error = WriteHardwareDebugReg(hwbType, idx))
343 return error;
344
345 return llvm::Error::success();
346#else // __aarch64__
347 uint32_t max_supported =
349 ? m_max_hwp_supported
350 : m_max_hbp_supported;
351 auto &regs = (hwbType == NativeRegisterContextDBReg::eDREGTypeWATCH)
352 ? m_hwp_regs
353 : m_hbp_regs;
354 return arm64::WriteHardwareDebugRegs(hwbType, m_thread.GetID(), max_supported,
355 regs)
356 .ToError();
357#endif // ifdef __arm__
358}
359
360#ifdef __arm__
361llvm::Error
362NativeRegisterContextLinux_arm::WriteHardwareDebugReg(DREGType hwbType,
363 int hwb_index) {
365 lldb::addr_t *addr_buf;
366 uint32_t *ctrl_buf;
367 int addr_idx = (hwb_index << 1) + 1;
368 int ctrl_idx = addr_idx + 1;
369
371 addr_idx *= -1;
372 addr_buf = &m_hwp_regs[hwb_index].address;
373 ctrl_idx *= -1;
374 ctrl_buf = &m_hwp_regs[hwb_index].control;
375 } else {
376 addr_buf = &m_hbp_regs[hwb_index].address;
377 ctrl_buf = &m_hbp_regs[hwb_index].control;
378 }
379
381 PTRACE_SETHBPREGS, m_thread.GetID(), (PTRACE_TYPE_ARG3)(intptr_t)addr_idx,
382 addr_buf, sizeof(unsigned int));
383
384 if (error.Fail())
385 return error.ToError();
386
388 PTRACE_SETHBPREGS, m_thread.GetID(), (PTRACE_TYPE_ARG3)(intptr_t)ctrl_idx,
389 ctrl_buf, sizeof(unsigned int));
390
391 return error.ToError();
392}
393#endif // ifdef __arm__
394
395uint32_t NativeRegisterContextLinux_arm::CalculateFprOffset(
396 const RegisterInfo *reg_info) const {
397 return reg_info->byte_offset - GetGPRSize();
398}
399
400Status NativeRegisterContextLinux_arm::DoReadRegisterValue(
401 uint32_t offset, const char *reg_name, uint32_t size,
402 RegisterValue &value) {
403 // PTRACE_PEEKUSER don't work in the aarch64 linux kernel used on android
404 // devices (always return "Bad address"). To avoid using PTRACE_PEEKUSER we
405 // read out the full GPR register set instead. This approach is about 4 times
406 // slower but the performance overhead is negligible in comparison to
407 // processing time in lldb-server.
408 assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
409 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
411 "Register isn't fit into the size of the GPR area");
412
413 Status error = ReadGPR();
414 if (error.Fail())
415 return error;
416
417 value.SetUInt32(m_gpr_arm[offset / sizeof(uint32_t)]);
418 return Status();
419}
420
421Status NativeRegisterContextLinux_arm::DoWriteRegisterValue(
422 uint32_t offset, const char *reg_name, const RegisterValue &value) {
423 // PTRACE_POKEUSER don't work in the aarch64 linux kernel used on android
424 // devices (always return "Bad address"). To avoid using PTRACE_POKEUSER we
425 // read out the full GPR register set, modify the requested register and
426 // write it back. This approach is about 4 times slower but the performance
427 // overhead is negligible in comparison to processing time in lldb-server.
428 assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
429 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
431 "Register isn't fit into the size of the GPR area");
432
433 Status error = ReadGPR();
434 if (error.Fail())
435 return error;
436
437 uint32_t reg_value = value.GetAsUInt32();
438 // As precaution for an undefined behavior encountered while setting PC we
439 // will clear thumb bit of new PC if we are already in thumb mode; that is
440 // CPSR thumb mode bit is set.
441 if (offset / sizeof(uint32_t) == gpr_pc_arm) {
442 // Check if we are already in thumb mode and thumb bit of current PC is
443 // read out to be zero and thumb bit of next PC is read out to be one.
444 if ((m_gpr_arm[gpr_cpsr_arm] & 0x20) && !(m_gpr_arm[gpr_pc_arm] & 0x01) &&
445 (value.GetAsUInt32() & 0x01)) {
446 reg_value &= (~1ull);
447 }
448 }
449
450 m_gpr_arm[offset / sizeof(uint32_t)] = reg_value;
451 return WriteGPR();
452}
453
454Status NativeRegisterContextLinux_arm::ReadGPR() {
455#ifdef __arm__
457#else // __aarch64__
458 struct iovec ioVec;
459 ioVec.iov_base = GetGPRBuffer();
460 ioVec.iov_len = GetGPRSize();
461
462 return ReadRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
463#endif // __arm__
464}
465
466Status NativeRegisterContextLinux_arm::WriteGPR() {
467#ifdef __arm__
469#else // __aarch64__
470 struct iovec ioVec;
471 ioVec.iov_base = GetGPRBuffer();
472 ioVec.iov_len = GetGPRSize();
473
474 return WriteRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
475#endif // __arm__
476}
477
478Status NativeRegisterContextLinux_arm::ReadFPR() {
479#ifdef __arm__
480 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, m_thread.GetID(),
481 nullptr, GetFPRBuffer(),
482 GetFPRSize());
483#else // __aarch64__
484 struct iovec ioVec;
485 ioVec.iov_base = GetFPRBuffer();
486 ioVec.iov_len = GetFPRSize();
487
488 return ReadRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
489#endif // __arm__
490}
491
492Status NativeRegisterContextLinux_arm::WriteFPR() {
493#ifdef __arm__
494 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, m_thread.GetID(),
495 nullptr, GetFPRBuffer(),
496 GetFPRSize());
497#else // __aarch64__
498 struct iovec ioVec;
499 ioVec.iov_base = GetFPRBuffer();
500 ioVec.iov_len = GetFPRSize();
501
502 return WriteRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
503#endif // __arm__
504}
505
506Status NativeRegisterContextLinux_arm::ReadTLS() {
507#ifdef __arm__
509 m_thread.GetID(), nullptr,
510 GetTLSBuffer(), GetTLSSize());
511#else // __aarch64__
513
514 struct iovec ioVec;
515 ioVec.iov_base = GetTLSBuffer();
516 ioVec.iov_len = GetTLSSize();
517
518 return ReadRegisterSet(&ioVec, GetTLSSize(), NT_ARM_TLS);
519#endif // __arm__
520}
521
522#endif // defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
static llvm::raw_ostream & error(Stream &strm)
#define PTRACE_GET_THREAD_AREA
Definition Ptrace.h:47
#define REG_CONTEXT_SIZE
size_t GetRegisterSetCount() const override
A subclass of DataBuffer that stores a data buffer on the heap.
void SetUInt64(uint64_t uint, Type t=eTypeUInt64)
void SetUInt16(uint16_t uint)
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
const void * GetBytes() const
void SetType(RegisterValue::Type type)
uint32_t GetAsUInt32(uint32_t fail_value=UINT32_MAX, bool *success_ptr=nullptr) const
void SetUInt32(uint32_t uint, Type t=eTypeUInt32)
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, void *data=nullptr, size_t data_size=0, long *result=nullptr)
}
static std::unique_ptr< NativeRegisterContextLinux > CreateHostNativeRegisterContextLinux(const ArchSpec &target_arch, NativeThreadLinux &native_thread)
static llvm::Expected< ArchSpec > DetermineArchitecture(lldb::tid_t tid)
#define LLDB_INVALID_REGNUM
Status WriteHardwareDebugRegs(int hwbType, ::pid_t tid, uint32_t max_supported, const std::array< NativeRegisterContextDBReg::DREG, 16 > &regs)
Status ReadHardwareDebugInfo(::pid_t tid, uint32_t &max_hwp_supported, uint32_t &max_hbp_supported)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
uint64_t tid_t
Definition lldb-types.h:84
@ 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.
uint32_t * invalidate_regs
List of registers (terminated with LLDB_INVALID_REGNUM).
Registers are grouped into register sets.