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("register %" PRIu32 " not found", reg_index);
32
33 return DoReadRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
34 reg_info->byte_size, reg_value);
35}
36
39 const RegisterValue &reg_value) {
40 uint32_t reg_to_write = reg_index;
41 RegisterValue value_to_write = reg_value;
42
43 // Check if this is a subregister of a full register.
44 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index);
45 assert(reg_info && "Expected valid register info for reg_index.");
46 if (reg_info->invalidate_regs &&
47 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
49
50 RegisterValue full_value;
51 uint32_t full_reg = reg_info->invalidate_regs[0];
52 const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg);
53
54 // Read the full register.
55 error = ReadRegister(full_reg_info, full_value);
56 if (error.Fail()) {
57 // full_reg_info was nullptr, or we couldn't read the register.
58 return error;
59 }
60
61 lldb::ByteOrder byte_order = GetByteOrder();
62 RegisterValue::BytesContainer dst(full_reg_info->byte_size);
63
64 // Get the bytes for the full register.
65 const uint32_t dest_size = full_value.GetAsMemoryData(
66 *full_reg_info, dst.data(), dst.size(), byte_order, error);
67 if (error.Success() && dest_size) {
69
70 // Get the bytes for the source data.
71 const uint32_t src_size = reg_value.GetAsMemoryData(
72 *reg_info, src.data(), src.size(), byte_order, error);
73 if (error.Success() && src_size && (src_size < dest_size)) {
74 // Copy the src bytes to the destination.
75 memcpy(dst.data() + (reg_info->byte_offset & 0x1), src.data(),
76 src_size);
77 // Set this full register as the value to write.
78 value_to_write.SetBytes(dst.data(), full_value.GetByteSize(),
79 byte_order);
80 value_to_write.SetType(*full_reg_info);
81 reg_to_write = full_reg;
82 }
83 }
84 }
85
86 const RegisterInfo *const register_to_write_info_p =
87 GetRegisterInfoAtIndex(reg_to_write);
88 assert(register_to_write_info_p &&
89 "register to write does not have valid RegisterInfo");
90 if (!register_to_write_info_p)
91 return Status("NativeRegisterContextLinux::%s failed to get RegisterInfo "
92 "for write register index %" PRIu32,
93 __FUNCTION__, reg_to_write);
94
95 return DoWriteRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
96 reg_value);
97}
98
102}
103
107}
108
111 nullptr, GetFPRBuffer(),
112 GetFPRSize());
113}
114
117 nullptr, GetFPRBuffer(),
118 GetFPRSize());
119}
120
122 unsigned int regset) {
124 static_cast<void *>(&regset), buf,
125 buf_size);
126}
127
129 unsigned int regset) {
131 static_cast<void *>(&regset), buf,
132 buf_size);
133}
134
136 const char *reg_name,
137 uint32_t size,
138 RegisterValue &value) {
140
141 long data;
143 PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
144 nullptr, 0, &data);
145
146 if (error.Success())
147 // First cast to an unsigned of the same size to avoid sign extension.
148 value.SetUInt(static_cast<unsigned long>(data), size);
149
150 LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
151 return error;
152}
153
155 uint32_t offset, const char *reg_name, const RegisterValue &value) {
157
158 void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
159 LLDB_LOG(log, "{0}: {1}", reg_name, buf);
160
162 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
163}
164
165llvm::Expected<ArchSpec>
167 size_t gpr64_size) {
168 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(gpr64_size);
169 struct iovec iov;
170 iov.iov_base = data.get();
171 iov.iov_len = gpr64_size;
172 unsigned int regset = llvm::ELF::NT_PRSTATUS;
174 &iov, sizeof(iov));
175 if (ST.Fail())
176 return ST.ToError();
177 return HostInfo::GetArchitecture(
178 iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
179}
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:359
#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:44
llvm::Error ToError() const
Definition: Status.cpp:89
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
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:331
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).