LLDB mainline
NativeRegisterContextLinux.cpp
Go to the documentation of this file.
1//===-- NativeRegisterContextLinux.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
10
13#include "lldb/Host/HostInfo.h"
17
18// System includes - They have to be included after framework includes because
19// they define some macros which collide with variable names in other modules.
20#include <sys/ptrace.h>
21#include <sys/uio.h>
22
23#ifndef PTRACE_GETREGS
24#define PTRACE_GETREGS 12
25#endif
26
27#ifndef PTRACE_SETREGS
28#define PTRACE_SETREGS 13
29#endif
30
31#ifndef PTRACE_GETFPREGS
32#define PTRACE_GETFPREGS 14
33#endif
34
35#ifndef PTRACE_SETFPREGS
36#define PTRACE_SETFPREGS 15
37#endif
38
39#ifndef PTRACE_GETREGSET
40#define PTRACE_GETREGSET 0x4204
41#endif
42
43#ifndef PTRACE_SETREGSET
44#define PTRACE_SETREGSET 0x4205
45#endif
46
47using namespace lldb_private;
48using namespace lldb_private::process_linux;
49
51 return m_thread.GetProcess().GetByteOrder();
52}
53
55 RegisterValue &reg_value) {
56 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
57 if (!reg_info)
58 return Status::FromErrorStringWithFormat("register %" PRIu32 " not found",
59 reg_index);
60
61 return DoReadRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
62 reg_info->byte_size, reg_value);
63}
64
67 const RegisterValue &reg_value) {
68 uint32_t reg_to_write = reg_index;
69 RegisterValue value_to_write = reg_value;
70
71 // Check if this is a subregister of a full register.
72 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index);
73 assert(reg_info && "Expected valid register info for reg_index.");
74 if (reg_info->invalidate_regs &&
75 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
77
78 RegisterValue full_value;
79 uint32_t full_reg = reg_info->invalidate_regs[0];
80 const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg);
81
82 // Read the full register.
83 error = ReadRegister(full_reg_info, full_value);
84 if (error.Fail()) {
85 // full_reg_info was nullptr, or we couldn't read the register.
86 return error;
87 }
88
89 lldb::ByteOrder byte_order = GetByteOrder();
90 RegisterValue::BytesContainer dst(full_reg_info->byte_size);
91
92 // Get the bytes for the full register.
93 const uint32_t dest_size = full_value.GetAsMemoryData(
94 *full_reg_info, dst.data(), dst.size(), byte_order, error);
95 if (error.Success() && dest_size) {
97
98 // Get the bytes for the source data.
99 const uint32_t src_size = reg_value.GetAsMemoryData(
100 *reg_info, src.data(), src.size(), byte_order, error);
101 if (error.Success() && src_size && (src_size < dest_size)) {
102 // Copy the src bytes to the destination.
103 memcpy(dst.data() + (reg_info->byte_offset & 0x1), src.data(),
104 src_size);
105 // Set this full register as the value to write.
106 value_to_write.SetBytes(dst.data(), full_value.GetByteSize(),
107 byte_order);
108 value_to_write.SetType(*full_reg_info);
109 reg_to_write = full_reg;
110 }
111 }
112 }
113
114 const RegisterInfo *const register_to_write_info_p =
115 GetRegisterInfoAtIndex(reg_to_write);
116 assert(register_to_write_info_p &&
117 "register to write does not have valid RegisterInfo");
118 if (!register_to_write_info_p)
120 "NativeRegisterContextLinux::%s failed to get RegisterInfo "
121 "for write register index %" PRIu32,
122 __FUNCTION__, reg_to_write);
123
124 return DoWriteRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
125 reg_value);
126}
127
132
137
143
149
151 unsigned int regset) {
153 static_cast<void *>(&regset), buf,
154 buf_size);
155}
156
158 unsigned int regset) {
160 static_cast<void *>(&regset), buf,
161 buf_size);
162}
163
165 const char *reg_name,
166 uint32_t size,
167 RegisterValue &value) {
169
170 long data;
172 PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
173 nullptr, 0, &data);
174
175 if (error.Success())
176 // First cast to an unsigned of the same size to avoid sign extension.
177 value.SetUInt(static_cast<unsigned long>(data), size);
178
179 LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
180 return error;
181}
182
184 uint32_t offset, const char *reg_name, const RegisterValue &value) {
186
187 void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
188 LLDB_LOG(log, "{0}: {1}", reg_name, buf);
189
191 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
192}
193
194llvm::Expected<ArchSpec>
196 size_t gpr64_size) {
197 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(gpr64_size);
198 struct iovec iov;
199 iov.iov_base = data.get();
200 iov.iov_len = gpr64_size;
201 unsigned int regset = llvm::ELF::NT_PRSTATUS;
203 &iov, sizeof(iov));
204 if (ST.Fail())
205 return ST.ToError();
206 return HostInfo::GetArchitecture(
207 iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
208}
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:375
#define PTRACE_SETREGSET
#define PTRACE_SETREGS
#define PTRACE_SETFPREGS
#define PTRACE_GETREGSET
#define PTRACE_GETREGS
#define PTRACE_GETFPREGS
const RegisterInfo * GetRegisterInfoAtIndex(uint32_t reg_index) const override
virtual Status ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
bool SetUInt(uint64_t uint, uint32_t byte_size)
uint32_t GetAsMemoryData(const RegisterInfo &reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
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)
void SetType(RegisterValue::Type type)
llvm::SmallVector< uint8_t, kTypicalRegisterByteSize > BytesContainer
An error handling class.
Definition Status.h:118
llvm::Error ToError() const
FIXME: Replace all uses with takeError() instead.
Definition Status.cpp:138
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
bool Fail() const
Test for error condition.
Definition Status.cpp:293
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, void *data=nullptr, size_t data_size=0, long *result=nullptr)
}
virtual Status ReadRegisterSet(void *buf, size_t buf_size, unsigned int regset)
virtual Status WriteRegisterRaw(uint32_t reg_index, const RegisterValue &reg_value)
virtual Status WriteRegisterSet(void *buf, size_t buf_size, unsigned int regset)
virtual Status ReadRegisterRaw(uint32_t reg_index, RegisterValue &reg_value)
virtual Status DoReadRegisterValue(uint32_t offset, const char *reg_name, uint32_t size, RegisterValue &value)
static llvm::Expected< ArchSpec > DetermineArchitectureViaGPR(lldb::tid_t tid, size_t gpr64_size)
virtual Status DoWriteRegisterValue(uint32_t offset, const char *reg_name, const RegisterValue &value)
#define LLDB_INVALID_REGNUM
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:338
ByteOrder
Byte ordering definitions.
uint64_t tid_t
Definition lldb-types.h:85
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.
llvm::ArrayRef< uint8_t > data(const uint8_t *context_base) const
const char * name
Name of this register, can't be NULL.
uint32_t * invalidate_regs
List of registers (terminated with LLDB_INVALID_REGNUM).