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
9#include "NativeRegisterContextLinux.h"
10
11#include "Plugins/Process/Linux/NativeProcessLinux.h"
12#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
13#include "lldb/Host/HostInfo.h"
14#include "lldb/Host/common/NativeProcessProtocol.h"
15#include "lldb/Host/common/NativeThreadProtocol.h"
16#include "lldb/Host/linux/Ptrace.h"
17#include "lldb/Utility/RegisterValue.h"
18#include <sys/uio.h>
19
20using namespace lldb_private;
21using namespace lldb_private::process_linux;
22
23lldb::ByteOrder NativeRegisterContextLinux::GetByteOrder() const {
24 return m_thread.GetProcess().GetByteOrder();
25}
26
27Status NativeRegisterContextLinux::ReadRegisterRaw(uint32_t reg_index,
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(offset: GetPtraceOffset(reg_index), reg_name: reg_info->name,
34 size: reg_info->byte_size, value&: reg_value);
35}
36
37Status
38NativeRegisterContextLinux::WriteRegisterRaw(uint32_t reg_index,
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)) {
48 Status error;
49
50 RegisterValue full_value;
51 uint32_t full_reg = reg_info->invalidate_regs[0];
52 const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(reg_index: full_reg);
53
54 // Read the full register.
55 error = ReadRegister(reg_info: full_reg_info, reg_value&: 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 reg_info: *full_reg_info, dst: dst.data(), dst_len: dst.size(), dst_byte_order: byte_order, error);
67 if (error.Success() && dest_size) {
68 RegisterValue::BytesContainer src(reg_info->byte_size);
69
70 // Get the bytes for the source data.
71 const uint32_t src_size = reg_value.GetAsMemoryData(
72 reg_info: *reg_info, dst: src.data(), dst_len: src.size(), dst_byte_order: byte_order, error);
73 if (error.Success() && src_size && (src_size < dest_size)) {
74 // Copy the src bytes to the destination.
75 memcpy(dest: dst.data() + (reg_info->byte_offset & 0x1), src: src.data(),
76 n: src_size);
77 // Set this full register as the value to write.
78 value_to_write.SetBytes(bytes: dst.data(), length: 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_index: 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(offset: GetPtraceOffset(reg_index), reg_name: reg_info->name,
96 value: reg_value);
97}
98
99Status NativeRegisterContextLinux::ReadGPR() {
100 return NativeProcessLinux::PtraceWrapper(
101 PTRACE_GETREGS, pid: m_thread.GetID(), addr: nullptr, data: GetGPRBuffer(), data_size: GetGPRSize());
102}
103
104Status NativeRegisterContextLinux::WriteGPR() {
105 return NativeProcessLinux::PtraceWrapper(
106 PTRACE_SETREGS, pid: m_thread.GetID(), addr: nullptr, data: GetGPRBuffer(), data_size: GetGPRSize());
107}
108
109Status NativeRegisterContextLinux::ReadFPR() {
110 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, pid: m_thread.GetID(),
111 addr: nullptr, data: GetFPRBuffer(),
112 data_size: GetFPRSize());
113}
114
115Status NativeRegisterContextLinux::WriteFPR() {
116 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, pid: m_thread.GetID(),
117 addr: nullptr, data: GetFPRBuffer(),
118 data_size: GetFPRSize());
119}
120
121Status NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size,
122 unsigned int regset) {
123 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, pid: m_thread.GetID(),
124 addr: static_cast<void *>(&regset), data: buf,
125 data_size: buf_size);
126}
127
128Status NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size,
129 unsigned int regset) {
130 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, pid: m_thread.GetID(),
131 addr: static_cast<void *>(&regset), data: buf,
132 data_size: buf_size);
133}
134
135Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset,
136 const char *reg_name,
137 uint32_t size,
138 RegisterValue &value) {
139 Log *log = GetLog(mask: POSIXLog::Registers);
140
141 long data;
142 Status error = NativeProcessLinux::PtraceWrapper(
143 req: PTRACE_PEEKUSER, pid: m_thread.GetID(), addr: reinterpret_cast<void *>(offset),
144 data: nullptr, data_size: 0, result: &data);
145
146 if (error.Success())
147 // First cast to an unsigned of the same size to avoid sign extension.
148 value.SetUInt(uint: static_cast<unsigned long>(data), byte_size: size);
149
150 LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
151 return error;
152}
153
154Status NativeRegisterContextLinux::DoWriteRegisterValue(
155 uint32_t offset, const char *reg_name, const RegisterValue &value) {
156 Log *log = GetLog(mask: POSIXLog::Registers);
157
158 void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
159 LLDB_LOG(log, "{0}: {1}", reg_name, buf);
160
161 return NativeProcessLinux::PtraceWrapper(
162 req: PTRACE_POKEUSER, pid: m_thread.GetID(), addr: reinterpret_cast<void *>(offset), data: buf);
163}
164
165llvm::Expected<ArchSpec>
166NativeRegisterContextLinux::DetermineArchitectureViaGPR(lldb::tid_t tid,
167 size_t gpr64_size) {
168 std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(num: 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;
173 Status ST = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, pid: tid, addr: &regset,
174 data: &iov, data_size: sizeof(iov));
175 if (ST.Fail())
176 return ST.ToError();
177 return HostInfo::GetArchitecture(
178 arch_kind: iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
179}
180

source code of lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp