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   return NativeProcessLinux::PtraceWrapper(
99       PTRACE_GETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize());
100 }
101 
102 Status NativeRegisterContextLinux::WriteGPR() {
103   return NativeProcessLinux::PtraceWrapper(
104       PTRACE_SETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize());
105 }
106 
107 Status NativeRegisterContextLinux::ReadFPR() {
108   return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(),
109                                            nullptr, GetFPRBuffer(),
110                                            GetFPRSize());
111 }
112 
113 Status NativeRegisterContextLinux::WriteFPR() {
114   return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(),
115                                            nullptr, GetFPRBuffer(),
116                                            GetFPRSize());
117 }
118 
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 
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 
133 Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset,
134                                                        const char *reg_name,
135                                                        uint32_t size,
136                                                        RegisterValue &value) {
137   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_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 
152 Status NativeRegisterContextLinux::DoWriteRegisterValue(
153     uint32_t offset, const char *reg_name, const RegisterValue &value) {
154   Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_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