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