xref: /openbsd-src/gnu/llvm/lldb/source/Plugins/Process/Linux/NativeRegisterContextLinux.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
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 
20 using namespace lldb_private;
21 using namespace lldb_private::process_linux;
22 
GetByteOrder() const23 lldb::ByteOrder NativeRegisterContextLinux::GetByteOrder() const {
24   return m_thread.GetProcess().GetByteOrder();
25 }
26 
ReadRegisterRaw(uint32_t reg_index,RegisterValue & reg_value)27 Status 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(GetPtraceOffset(reg_index), reg_info->name,
34                              reg_info->byte_size, reg_value);
35 }
36 
37 Status
WriteRegisterRaw(uint32_t reg_index,const RegisterValue & reg_value)38 NativeRegisterContextLinux::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(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     uint8_t dst[RegisterValue::kMaxRegisterByteSize];
63 
64     // Get the bytes for the full register.
65     const uint32_t dest_size = full_value.GetAsMemoryData(
66         *full_reg_info, dst, sizeof(dst), byte_order, error);
67     if (error.Success() && dest_size) {
68       uint8_t src[RegisterValue::kMaxRegisterByteSize];
69 
70       // Get the bytes for the source data.
71       const uint32_t src_size = reg_value.GetAsMemoryData(
72           *reg_info, src, sizeof(src), byte_order, error);
73       if (error.Success() && src_size && (src_size < dest_size)) {
74         // Copy the src bytes to the destination.
75         memcpy(dst + (reg_info->byte_offset & 0x1), src, src_size);
76         // Set this full register as the value to write.
77         value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order);
78         value_to_write.SetType(*full_reg_info);
79         reg_to_write = full_reg;
80       }
81     }
82   }
83 
84   const RegisterInfo *const register_to_write_info_p =
85       GetRegisterInfoAtIndex(reg_to_write);
86   assert(register_to_write_info_p &&
87          "register to write does not have valid RegisterInfo");
88   if (!register_to_write_info_p)
89     return Status("NativeRegisterContextLinux::%s failed to get RegisterInfo "
90                   "for write register index %" PRIu32,
91                   __FUNCTION__, reg_to_write);
92 
93   return DoWriteRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
94                               reg_value);
95 }
96 
ReadGPR()97 Status NativeRegisterContextLinux::ReadGPR() {
98   return NativeProcessLinux::PtraceWrapper(
99       PTRACE_GETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize());
100 }
101 
WriteGPR()102 Status NativeRegisterContextLinux::WriteGPR() {
103   return NativeProcessLinux::PtraceWrapper(
104       PTRACE_SETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize());
105 }
106 
ReadFPR()107 Status NativeRegisterContextLinux::ReadFPR() {
108   return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(),
109                                            nullptr, GetFPRBuffer(),
110                                            GetFPRSize());
111 }
112 
WriteFPR()113 Status NativeRegisterContextLinux::WriteFPR() {
114   return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(),
115                                            nullptr, GetFPRBuffer(),
116                                            GetFPRSize());
117 }
118 
ReadRegisterSet(void * buf,size_t buf_size,unsigned int regset)119 Status NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size,
120                                                    unsigned int regset) {
121   return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(),
122                                            static_cast<void *>(&regset), buf,
123                                            buf_size);
124 }
125 
WriteRegisterSet(void * buf,size_t buf_size,unsigned int regset)126 Status NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size,
127                                                     unsigned int regset) {
128   return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(),
129                                            static_cast<void *>(&regset), buf,
130                                            buf_size);
131 }
132 
DoReadRegisterValue(uint32_t offset,const char * reg_name,uint32_t size,RegisterValue & value)133 Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset,
134                                                        const char *reg_name,
135                                                        uint32_t size,
136                                                        RegisterValue &value) {
137   Log *log = GetLog(POSIXLog::Registers);
138 
139   long data;
140   Status error = NativeProcessLinux::PtraceWrapper(
141       PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
142       nullptr, 0, &data);
143 
144   if (error.Success())
145     // First cast to an unsigned of the same size to avoid sign extension.
146     value.SetUInt(static_cast<unsigned long>(data), size);
147 
148   LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
149   return error;
150 }
151 
DoWriteRegisterValue(uint32_t offset,const char * reg_name,const RegisterValue & value)152 Status NativeRegisterContextLinux::DoWriteRegisterValue(
153     uint32_t offset, const char *reg_name, const RegisterValue &value) {
154   Log *log = GetLog(POSIXLog::Registers);
155 
156   void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
157   LLDB_LOG(log, "{0}: {1}", reg_name, buf);
158 
159   return NativeProcessLinux::PtraceWrapper(
160       PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
161 }
162 
163 llvm::Expected<ArchSpec>
DetermineArchitectureViaGPR(lldb::tid_t tid,size_t gpr64_size)164 NativeRegisterContextLinux::DetermineArchitectureViaGPR(lldb::tid_t tid,
165                                                         size_t gpr64_size) {
166   std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(gpr64_size);
167   struct iovec iov;
168   iov.iov_base = data.get();
169   iov.iov_len = gpr64_size;
170   unsigned int regset = llvm::ELF::NT_PRSTATUS;
171   Status ST = NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, tid, &regset,
172                                                 &iov, sizeof(iov));
173   if (ST.Fail())
174     return ST.ToError();
175   return HostInfo::GetArchitecture(
176       iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
177 }
178