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 ::memset(&m_hwp_regs, 0, sizeof(m_hwp_regs));
82 ::memset(&m_hbp_regs, 0, sizeof(m_hbp_regs));
83
84 // 16 is just a maximum value, query hardware for actual watchpoint count
85 m_max_hwp_supported = 16;
86 m_max_hbp_supported = 16;
87 m_refresh_hwdebug_info = true;
88}
89
90RegisterInfoPOSIX_arm &NativeRegisterContextLinux_arm::GetRegisterInfo() const {
91 return static_cast<RegisterInfoPOSIX_arm &>(*m_register_info_interface_up);
92}
93
94uint32_t NativeRegisterContextLinux_arm::GetRegisterSetCount() const {
95 return GetRegisterInfo().GetRegisterSetCount();
96}
97
98uint32_t NativeRegisterContextLinux_arm::GetUserRegisterCount() const {
99 uint32_t count = 0;
100 for (uint32_t set_index = 0; set_index < GetRegisterSetCount(); ++set_index)
101 count += GetRegisterSet(set_index)->num_registers;
102 return count;
103}
104
105const RegisterSet *
106NativeRegisterContextLinux_arm::GetRegisterSet(uint32_t set_index) const {
107 return GetRegisterInfo().GetRegisterSet(set_index);
108}
109
110Status
111NativeRegisterContextLinux_arm::ReadRegister(const RegisterInfo *reg_info,
112 RegisterValue &reg_value) {
114
115 if (!reg_info) {
116 error = Status::FromErrorString("reg_info NULL");
117 return error;
118 }
119
120 const uint32_t reg = reg_info->kinds[lldb::eRegisterKindLLDB];
121
122 if (IsFPR(reg)) {
123 error = ReadFPR();
124 if (error.Fail())
125 return error;
126 } else if (IsTLS(reg)) {
127 error = ReadTLS();
128 if (error.Success())
129 reg_value.SetUInt32(m_tls.tpidruro);
130 return error;
131 } else {
132 uint32_t full_reg = reg;
133 bool is_subreg = reg_info->invalidate_regs &&
134 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM);
135
136 if (is_subreg) {
137 // Read the full aligned 64-bit register.
138 full_reg = reg_info->invalidate_regs[0];
139 }
140
141 error = ReadRegisterRaw(full_reg, reg_value);
142
143 if (error.Success()) {
144 // If our read was not aligned (for ah,bh,ch,dh), shift our returned
145 // value one byte to the right.
146 if (is_subreg && (reg_info->byte_offset & 0x1))
147 reg_value.SetUInt64(reg_value.GetAsUInt64() >> 8);
148
149 // If our return byte size was greater than the return value reg size,
150 // then use the type specified by reg_info rather than the uint64_t
151 // default
152 if (reg_value.GetByteSize() > reg_info->byte_size)
153 reg_value.SetType(*reg_info);
154 }
155 return error;
156 }
157
158 // Get pointer to m_fpr variable and set the data from it.
159 uint32_t fpr_offset = CalculateFprOffset(reg_info);
160 assert(fpr_offset < sizeof m_fpr);
161 uint8_t *src = (uint8_t *)&m_fpr + fpr_offset;
162 switch (reg_info->byte_size) {
163 case 2:
164 reg_value.SetUInt16(*(uint16_t *)src);
165 break;
166 case 4:
167 reg_value.SetUInt32(*(uint32_t *)src);
168 break;
169 case 8:
170 reg_value.SetUInt64(*(uint64_t *)src);
171 break;
172 case 16:
173 reg_value.SetBytes(src, 16, GetByteOrder());
174 break;
175 default:
176 assert(false && "Unhandled data size.");
177 error = Status::FromErrorStringWithFormat("unhandled byte size: %" PRIu32,
178 reg_info->byte_size);
179 break;
180 }
181
182 return error;
183}
184
185Status
186NativeRegisterContextLinux_arm::WriteRegister(const RegisterInfo *reg_info,
187 const RegisterValue &reg_value) {
188 if (!reg_info)
189 return Status::FromErrorString("reg_info NULL");
190
191 const uint32_t reg_index = reg_info->kinds[lldb::eRegisterKindLLDB];
192 if (reg_index == LLDB_INVALID_REGNUM)
194 "no lldb regnum for %s",
195 reg_info && reg_info->name ? reg_info->name : "<unknown register>");
196
197 if (IsGPR(reg_index))
198 return WriteRegisterRaw(reg_index, reg_value);
199
200 if (IsFPR(reg_index)) {
201 // Get pointer to m_fpr variable and set the data to it.
202 uint32_t fpr_offset = CalculateFprOffset(reg_info);
203 assert(fpr_offset < sizeof m_fpr);
204 uint8_t *dst = (uint8_t *)&m_fpr + fpr_offset;
205 ::memcpy(dst, reg_value.GetBytes(), reg_info->byte_size);
206
207 return WriteFPR();
208 }
209
210 if (IsTLS(reg_index))
212 "writing to a thread pointer register is not implemented");
213
215 "failed - register wasn't recognized to be a GPR or an FPR, "
216 "write strategy unknown");
217}
218
219Status NativeRegisterContextLinux_arm::ReadAllRegisterValues(
222
223 data_sp.reset(new DataBufferHeap(REG_CONTEXT_SIZE, 0));
224 error = ReadGPR();
225 if (error.Fail())
226 return error;
227
228 error = ReadFPR();
229 if (error.Fail())
230 return error;
231
232 error = ReadTLS();
233 if (error.Fail())
234 return error;
235
236 uint8_t *dst = data_sp->GetBytes();
237 ::memcpy(dst, &m_gpr_arm, GetGPRSize());
238 dst += GetGPRSize();
239 ::memcpy(dst, &m_fpr, sizeof(m_fpr));
240 dst += sizeof(m_fpr);
241 ::memcpy(dst, &m_tls, sizeof(m_tls));
242
243 return error;
244}
245
246Status NativeRegisterContextLinux_arm::WriteAllRegisterValues(
247 const lldb::DataBufferSP &data_sp) {
249
250 if (!data_sp) {
252 "NativeRegisterContextLinux_arm::%s invalid data_sp provided",
253 __FUNCTION__);
254 return error;
255 }
256
257 if (data_sp->GetByteSize() != REG_CONTEXT_SIZE) {
259 "NativeRegisterContextLinux_arm::%s data_sp contained mismatched "
260 "data size, expected %" PRIu64 ", actual %" PRIu64,
261 __FUNCTION__, (uint64_t)REG_CONTEXT_SIZE, data_sp->GetByteSize());
262 return error;
263 }
264
265 const uint8_t *src = data_sp->GetBytes();
266 if (src == nullptr) {
268 "NativeRegisterContextLinux_arm::%s "
269 "DataBuffer::GetBytes() returned a null "
270 "pointer",
271 __FUNCTION__);
272 return error;
273 }
274 ::memcpy(&m_gpr_arm, src, GetRegisterInfoInterface().GetGPRSize());
275
276 error = WriteGPR();
277 if (error.Fail())
278 return error;
279
280 src += GetRegisterInfoInterface().GetGPRSize();
281 ::memcpy(&m_fpr, src, sizeof(m_fpr));
282
283 error = WriteFPR();
284 if (error.Fail())
285 return error;
286
287 // Note: writing to a thread pointer register is not implemented.
288
289 return error;
290}
291
292bool NativeRegisterContextLinux_arm::IsGPR(unsigned reg) const {
293 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
295 return true;
296 return false;
297}
298
299bool NativeRegisterContextLinux_arm::IsFPR(unsigned reg) const {
300 if (GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
302 return true;
303 return false;
304}
305
306bool NativeRegisterContextLinux_arm::IsTLS(unsigned reg) const {
307 return GetRegisterInfo().GetRegisterSetFromRegisterIndex(reg) ==
309}
310
311llvm::Error NativeRegisterContextLinux_arm::ReadHardwareDebugInfo() {
312 if (!m_refresh_hwdebug_info)
313 return llvm::Error::success();
314
315#ifdef __arm__
316 unsigned int cap_val;
318 PTRACE_GETHBPREGS, m_thread.GetID(), nullptr, &cap_val,
319 sizeof(unsigned int));
320
321 if (error.Fail())
322 return error.ToError();
323
324 m_max_hwp_supported = (cap_val >> 8) & 0xff;
325 m_max_hbp_supported = cap_val & 0xff;
326 m_refresh_hwdebug_info = false;
327
328 return error.ToError();
329#else // __aarch64__
330 return arm64::ReadHardwareDebugInfo(m_thread.GetID(), m_max_hwp_supported,
331 m_max_hbp_supported)
332 .ToError();
333#endif // ifdef __arm__
334}
335
336llvm::Error
337NativeRegisterContextLinux_arm::WriteHardwareDebugRegs(DREGType hwbType) {
338#ifdef __arm__
339 uint32_t max_index = m_max_hbp_supported;
340 if (hwbType == eDREGTypeWATCH)
341 max_index = m_max_hwp_supported;
342
343 for (uint32_t idx = 0; idx < max_index; ++idx)
344 if (auto error = WriteHardwareDebugReg(hwbType, idx))
345 return error;
346
347 return llvm::Error::success();
348#else // __aarch64__
349 uint32_t max_supported =
351 ? m_max_hwp_supported
352 : m_max_hbp_supported;
353 auto &regs = (hwbType == NativeRegisterContextDBReg::eDREGTypeWATCH)
354 ? m_hwp_regs
355 : m_hbp_regs;
356 return arm64::WriteHardwareDebugRegs(hwbType, m_thread.GetID(), max_supported,
357 regs)
358 .ToError();
359#endif // ifdef __arm__
360}
361
362#ifdef __arm__
363llvm::Error
364NativeRegisterContextLinux_arm::WriteHardwareDebugReg(DREGType hwbType,
365 int hwb_index) {
367 lldb::addr_t *addr_buf;
368 uint32_t *ctrl_buf;
369 int addr_idx = (hwb_index << 1) + 1;
370 int ctrl_idx = addr_idx + 1;
371
373 addr_idx *= -1;
374 addr_buf = &m_hwp_regs[hwb_index].address;
375 ctrl_idx *= -1;
376 ctrl_buf = &m_hwp_regs[hwb_index].control;
377 } else {
378 addr_buf = &m_hbp_regs[hwb_index].address;
379 ctrl_buf = &m_hbp_regs[hwb_index].control;
380 }
381
383 PTRACE_SETHBPREGS, m_thread.GetID(), (PTRACE_TYPE_ARG3)(intptr_t)addr_idx,
384 addr_buf, sizeof(unsigned int));
385
386 if (error.Fail())
387 return error.ToError();
388
390 PTRACE_SETHBPREGS, m_thread.GetID(), (PTRACE_TYPE_ARG3)(intptr_t)ctrl_idx,
391 ctrl_buf, sizeof(unsigned int));
392
393 return error.ToError();
394}
395#endif // ifdef __arm__
396
397uint32_t NativeRegisterContextLinux_arm::CalculateFprOffset(
398 const RegisterInfo *reg_info) const {
399 return reg_info->byte_offset - GetGPRSize();
400}
401
402Status NativeRegisterContextLinux_arm::DoReadRegisterValue(
403 uint32_t offset, const char *reg_name, uint32_t size,
404 RegisterValue &value) {
405 // PTRACE_PEEKUSER don't work in the aarch64 linux kernel used on android
406 // devices (always return "Bad address"). To avoid using PTRACE_PEEKUSER we
407 // read out the full GPR register set instead. This approach is about 4 times
408 // slower but the performance overhead is negligible in comparison to
409 // processing time in lldb-server.
410 assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
411 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
413 "Register isn't fit into the size of the GPR area");
414
415 Status error = ReadGPR();
416 if (error.Fail())
417 return error;
418
419 value.SetUInt32(m_gpr_arm[offset / sizeof(uint32_t)]);
420 return Status();
421}
422
423Status NativeRegisterContextLinux_arm::DoWriteRegisterValue(
424 uint32_t offset, const char *reg_name, const RegisterValue &value) {
425 // PTRACE_POKEUSER don't work in the aarch64 linux kernel used on android
426 // devices (always return "Bad address"). To avoid using PTRACE_POKEUSER we
427 // read out the full GPR register set, modify the requested register and
428 // write it back. This approach is about 4 times slower but the performance
429 // overhead is negligible in comparison to processing time in lldb-server.
430 assert(offset % 4 == 0 && "Try to write a register with unaligned offset");
431 if (offset + sizeof(uint32_t) > sizeof(m_gpr_arm))
433 "Register isn't fit into the size of the GPR area");
434
435 Status error = ReadGPR();
436 if (error.Fail())
437 return error;
438
439 uint32_t reg_value = value.GetAsUInt32();
440 // As precaution for an undefined behavior encountered while setting PC we
441 // will clear thumb bit of new PC if we are already in thumb mode; that is
442 // CPSR thumb mode bit is set.
443 if (offset / sizeof(uint32_t) == gpr_pc_arm) {
444 // Check if we are already in thumb mode and thumb bit of current PC is
445 // read out to be zero and thumb bit of next PC is read out to be one.
446 if ((m_gpr_arm[gpr_cpsr_arm] & 0x20) && !(m_gpr_arm[gpr_pc_arm] & 0x01) &&
447 (value.GetAsUInt32() & 0x01)) {
448 reg_value &= (~1ull);
449 }
450 }
451
452 m_gpr_arm[offset / sizeof(uint32_t)] = reg_value;
453 return WriteGPR();
454}
455
456Status NativeRegisterContextLinux_arm::ReadGPR() {
457#ifdef __arm__
459#else // __aarch64__
460 struct iovec ioVec;
461 ioVec.iov_base = GetGPRBuffer();
462 ioVec.iov_len = GetGPRSize();
463
464 return ReadRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
465#endif // __arm__
466}
467
468Status NativeRegisterContextLinux_arm::WriteGPR() {
469#ifdef __arm__
471#else // __aarch64__
472 struct iovec ioVec;
473 ioVec.iov_base = GetGPRBuffer();
474 ioVec.iov_len = GetGPRSize();
475
476 return WriteRegisterSet(&ioVec, GetGPRSize(), NT_PRSTATUS);
477#endif // __arm__
478}
479
480Status NativeRegisterContextLinux_arm::ReadFPR() {
481#ifdef __arm__
482 return NativeProcessLinux::PtraceWrapper(PTRACE_GETVFPREGS, m_thread.GetID(),
483 nullptr, GetFPRBuffer(),
484 GetFPRSize());
485#else // __aarch64__
486 struct iovec ioVec;
487 ioVec.iov_base = GetFPRBuffer();
488 ioVec.iov_len = GetFPRSize();
489
490 return ReadRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
491#endif // __arm__
492}
493
494Status NativeRegisterContextLinux_arm::WriteFPR() {
495#ifdef __arm__
496 return NativeProcessLinux::PtraceWrapper(PTRACE_SETVFPREGS, m_thread.GetID(),
497 nullptr, GetFPRBuffer(),
498 GetFPRSize());
499#else // __aarch64__
500 struct iovec ioVec;
501 ioVec.iov_base = GetFPRBuffer();
502 ioVec.iov_len = GetFPRSize();
503
504 return WriteRegisterSet(&ioVec, GetFPRSize(), NT_ARM_VFP);
505#endif // __arm__
506}
507
508Status NativeRegisterContextLinux_arm::ReadTLS() {
509#ifdef __arm__
511 m_thread.GetID(), nullptr,
512 GetTLSBuffer(), GetTLSSize());
513#else // __aarch64__
515
516 struct iovec ioVec;
517 ioVec.iov_base = GetTLSBuffer();
518 ioVec.iov_len = GetTLSSize();
519
520 return ReadRegisterSet(&ioVec, GetTLSSize(), NT_ARM_TLS);
521#endif // __arm__
522}
523
524#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.