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"
18#include <sys/uio.h>
19
20using namespace lldb_private;
21using namespace lldb_private::process_linux;
22
25}
26
28 RegisterValue &reg_value) {
29 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
30 if (!reg_info)
31 return Status::FromErrorStringWithFormat("register %" PRIu32 " not found",
32 reg_index);
33
34 return DoReadRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
35 reg_info->byte_size, reg_value);
36}
37
40 const RegisterValue &reg_value) {
41 uint32_t reg_to_write = reg_index;
42 RegisterValue value_to_write = reg_value;
43
44 // Check if this is a subregister of a full register.
45 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index);
46 assert(reg_info && "Expected valid register info for reg_index.");
47 if (reg_info->invalidate_regs &&
48 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
50
51 RegisterValue full_value;
52 uint32_t full_reg = reg_info->invalidate_regs[0];
53 const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg);
54
55 // Read the full register.
56 error = ReadRegister(full_reg_info, full_value);
57 if (error.Fail()) {
58 // full_reg_info was nullptr, or we couldn't read the register.
59 return error;
60 }
61
62 lldb::ByteOrder byte_order = GetByteOrder();
63 RegisterValue::BytesContainer dst(full_reg_info->byte_size);
64
65 // Get the bytes for the full register.
66 const uint32_t dest_size = full_value.GetAsMemoryData(
67 *full_reg_info, dst.data(), dst.size(), byte_order, error);
68 if (error.Success() && dest_size) {
70
71 // Get the bytes for the source data.
72 const uint32_t src_size = reg_value.GetAsMemoryData(
73 *reg_info, src.data(), src.size(), byte_order, error);
74 if (error.Success() && src_size && (src_size < dest_size)) {
75 // Copy the src bytes to the destination.
76 memcpy(dst.data() + (reg_info->byte_offset & 0x1), src.data(),
77 src_size);
78 // Set this full register as the value to write.
79 value_to_write.SetBytes(dst.data(), full_value.GetByteSize(),
80 byte_order);
81 value_to_write.SetType(*full_reg_info);
82 reg_to_write = full_reg;
83 }
84 }
85 }
86
87 const RegisterInfo *const register_to_write_info_p =
88 GetRegisterInfoAtIndex(reg_to_write);
89 assert(register_to_write_info_p &&
90 "register to write does not have valid RegisterInfo");
91 if (!register_to_write_info_p)
93 "NativeRegisterContextLinux::%s failed to get RegisterInfo "
94 "for write register index %" PRIu32,
95 __FUNCTION__, reg_to_write);
96
97 return DoWriteRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
98 reg_value);
99}
100
104}
105
109}
110
113 nullptr, GetFPRBuffer(),
114 GetFPRSize());
115}
116
119 nullptr, GetFPRBuffer(),
120 GetFPRSize());
121}
122
124 unsigned int regset) {
126 static_cast<void *>(&regset), buf,
127 buf_size);
128}
129
131 unsigned int regset) {
133 static_cast<void *>(&regset), buf,
134 buf_size);
135}
136
138 const char *reg_name,
139 uint32_t size,
140 RegisterValue &value) {
142
143 long data;
145 PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
146 nullptr, 0, &data);
147
148 if (error.Success())
149 // First cast to an unsigned of the same size to avoid sign extension.
150 value.SetUInt(static_cast<unsigned long>(data), size);
151
152 LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
153 return error;
154}
155
157 uint32_t offset, const char *reg_name, const RegisterValue &value) {
159
160 void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
161 LLDB_LOG(log, "{0}: {1}", reg_name, buf);
162
164 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
165}
166
167llvm::Expected<ArchSpec>
169 size_t gpr64_size) {
170 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(gpr64_size);
171 struct iovec iov;
172 iov.iov_base = data.get();
173 iov.iov_len = gpr64_size;
174 unsigned int regset = llvm::ELF::NT_PRSTATUS;
176 &iov, sizeof(iov));
177 if (ST.Fail())
178 return ST.ToError();
179 return HostInfo::GetArchitecture(
180 iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
181}
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
#define PTRACE_GETREGS
Definition: Ptrace.h:24
#define PTRACE_SETREGSET
Definition: Ptrace.h:39
#define PTRACE_GETFPREGS
Definition: Ptrace.h:30
#define PTRACE_SETREGS
Definition: Ptrace.h:27
#define PTRACE_SETFPREGS
Definition: Ptrace.h:33
#define PTRACE_GETREGSET
Definition: Ptrace.h:36
const RegisterInfo * GetRegisterInfoAtIndex(uint32_t reg_index) const override
virtual Status ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
NativeProcessProtocol & GetProcess()
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)
Definition: RegisterValue.h:95
llvm::SmallVector< uint8_t, kTypicalRegisterByteSize > BytesContainer
Definition: RegisterValue.h:41
An error handling class.
Definition: Status.h:118
llvm::Error ToError() const
FIXME: Replace all uses with takeError() instead.
Definition: Status.cpp:139
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
bool Fail() const
Test for error condition.
Definition: Status.cpp:294
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
Definition: lldb-defines.h:87
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
ByteOrder
Byte ordering definitions.
uint64_t tid_t
Definition: lldb-types.h:84
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).