1 //===-- NativeRegisterContextLinux.cpp --------------------------*- C++ -*-===//
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 "lldb/Host/common/NativeProcessProtocol.h"
12 #include "lldb/Host/common/NativeThreadProtocol.h"
13 #include "lldb/Host/linux/Ptrace.h"
14 #include "lldb/Utility/RegisterValue.h"
15 
16 #include "Plugins/Process/Linux/NativeProcessLinux.h"
17 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
18 
19 using namespace lldb_private;
20 using namespace lldb_private::process_linux;
21 
22 NativeRegisterContextLinux::NativeRegisterContextLinux(
23     NativeThreadProtocol &native_thread,
24     RegisterInfoInterface *reg_info_interface_p)
25     : NativeRegisterContextRegisterInfo(native_thread, reg_info_interface_p) {}
26 
27 lldb::ByteOrder NativeRegisterContextLinux::GetByteOrder() const {
28   return m_thread.GetProcess().GetByteOrder();
29 }
30 
31 Status NativeRegisterContextLinux::ReadRegisterRaw(uint32_t reg_index,
32                                                    RegisterValue &reg_value) {
33   const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
34   if (!reg_info)
35     return Status("register %" PRIu32 " not found", reg_index);
36 
37   return DoReadRegisterValue(reg_info->byte_offset, reg_info->name,
38                              reg_info->byte_size, reg_value);
39 }
40 
41 Status
42 NativeRegisterContextLinux::WriteRegisterRaw(uint32_t reg_index,
43                                              const RegisterValue &reg_value) {
44   uint32_t reg_to_write = reg_index;
45   RegisterValue value_to_write = reg_value;
46 
47   // Check if this is a subregister of a full register.
48   const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index);
49   if (reg_info->invalidate_regs &&
50       (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
51     Status error;
52 
53     RegisterValue full_value;
54     uint32_t full_reg = reg_info->invalidate_regs[0];
55     const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg);
56 
57     // Read the full register.
58     error = ReadRegister(full_reg_info, full_value);
59     if (error.Fail())
60       return error;
61 
62     lldb::ByteOrder byte_order = GetByteOrder();
63     uint8_t dst[RegisterValue::kMaxRegisterByteSize];
64 
65     // Get the bytes for the full register.
66     const uint32_t dest_size = full_value.GetAsMemoryData(
67         full_reg_info, dst, sizeof(dst), byte_order, error);
68     if (error.Success() && dest_size) {
69       uint8_t src[RegisterValue::kMaxRegisterByteSize];
70 
71       // Get the bytes for the source data.
72       const uint32_t src_size = reg_value.GetAsMemoryData(
73           reg_info, src, sizeof(src), byte_order, error);
74       if (error.Success() && src_size && (src_size < dest_size)) {
75         // Copy the src bytes to the destination.
76         memcpy(dst + (reg_info->byte_offset & 0x1), src, src_size);
77         // Set this full register as the value to write.
78         value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order);
79         value_to_write.SetType(full_reg_info);
80         reg_to_write = full_reg;
81       }
82     }
83   }
84 
85   const RegisterInfo *const register_to_write_info_p =
86       GetRegisterInfoAtIndex(reg_to_write);
87   assert(register_to_write_info_p &&
88          "register to write does not have valid RegisterInfo");
89   if (!register_to_write_info_p)
90     return Status("NativeRegisterContextLinux::%s failed to get RegisterInfo "
91                   "for write register index %" PRIu32,
92                   __FUNCTION__, reg_to_write);
93 
94   return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, reg_value);
95 }
96 
97 Status NativeRegisterContextLinux::ReadGPR() {
98   void *buf = GetGPRBuffer();
99   if (!buf)
100     return Status("GPR buffer is NULL");
101   size_t buf_size = GetGPRSize();
102 
103   return DoReadGPR(buf, buf_size);
104 }
105 
106 Status NativeRegisterContextLinux::WriteGPR() {
107   void *buf = GetGPRBuffer();
108   if (!buf)
109     return Status("GPR buffer is NULL");
110   size_t buf_size = GetGPRSize();
111 
112   return DoWriteGPR(buf, buf_size);
113 }
114 
115 Status NativeRegisterContextLinux::ReadFPR() {
116   void *buf = GetFPRBuffer();
117   if (!buf)
118     return Status("FPR buffer is NULL");
119   size_t buf_size = GetFPRSize();
120 
121   return DoReadFPR(buf, buf_size);
122 }
123 
124 Status NativeRegisterContextLinux::WriteFPR() {
125   void *buf = GetFPRBuffer();
126   if (!buf)
127     return Status("FPR buffer is NULL");
128   size_t buf_size = GetFPRSize();
129 
130   return DoWriteFPR(buf, buf_size);
131 }
132 
133 Status NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size,
134                                                    unsigned int regset) {
135   return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(),
136                                            static_cast<void *>(&regset), buf,
137                                            buf_size);
138 }
139 
140 Status NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size,
141                                                     unsigned int regset) {
142   return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(),
143                                            static_cast<void *>(&regset), buf,
144                                            buf_size);
145 }
146 
147 Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset,
148                                                        const char *reg_name,
149                                                        uint32_t size,
150                                                        RegisterValue &value) {
151   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS));
152 
153   long data;
154   Status error = NativeProcessLinux::PtraceWrapper(
155       PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
156       nullptr, 0, &data);
157 
158   if (error.Success())
159     // First cast to an unsigned of the same size to avoid sign extension.
160     value.SetUInt(static_cast<unsigned long>(data), size);
161 
162   LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
163   return error;
164 }
165 
166 Status NativeRegisterContextLinux::DoWriteRegisterValue(
167     uint32_t offset, const char *reg_name, const RegisterValue &value) {
168   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS));
169 
170   void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
171   LLDB_LOG(log, "{0}: {1}", reg_name, buf);
172 
173   return NativeProcessLinux::PtraceWrapper(
174       PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
175 }
176 
177 Status NativeRegisterContextLinux::DoReadGPR(void *buf, size_t buf_size) {
178   return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(),
179                                            nullptr, buf, buf_size);
180 }
181 
182 Status NativeRegisterContextLinux::DoWriteGPR(void *buf, size_t buf_size) {
183   return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(),
184                                            nullptr, buf, buf_size);
185 }
186 
187 Status NativeRegisterContextLinux::DoReadFPR(void *buf, size_t buf_size) {
188   return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(),
189                                            nullptr, buf, buf_size);
190 }
191 
192 Status NativeRegisterContextLinux::DoWriteFPR(void *buf, size_t buf_size) {
193   return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(),
194                                            nullptr, buf, buf_size);
195 }
196