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 Status NativeRegisterContextLinux::ReadRegisterRaw(uint32_t reg_index, 45 RegisterValue ®_value) { 46 const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index); 47 if (!reg_info) 48 return Status("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 Status 55 NativeRegisterContextLinux::WriteRegisterRaw(uint32_t reg_index, 56 const RegisterValue ®_value) { 57 uint32_t reg_to_write = reg_index; 58 RegisterValue value_to_write = reg_value; 59 60 // Check if this is a subregister of a full register. 61 const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index); 62 if (reg_info->invalidate_regs && 63 (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) { 64 Status error; 65 66 RegisterValue full_value; 67 uint32_t full_reg = reg_info->invalidate_regs[0]; 68 const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg); 69 70 // Read the full register. 71 error = ReadRegister(full_reg_info, full_value); 72 if (error.Fail()) 73 return error; 74 75 lldb::ByteOrder byte_order = GetByteOrder(); 76 uint8_t dst[RegisterValue::kMaxRegisterByteSize]; 77 78 // Get the bytes for the full register. 79 const uint32_t dest_size = full_value.GetAsMemoryData( 80 full_reg_info, dst, sizeof(dst), byte_order, error); 81 if (error.Success() && dest_size) { 82 uint8_t src[RegisterValue::kMaxRegisterByteSize]; 83 84 // Get the bytes for the source data. 85 const uint32_t src_size = reg_value.GetAsMemoryData( 86 reg_info, src, sizeof(src), byte_order, error); 87 if (error.Success() && src_size && (src_size < dest_size)) { 88 // Copy the src bytes to the destination. 89 memcpy(dst + (reg_info->byte_offset & 0x1), src, src_size); 90 // Set this full register as the value to write. 91 value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order); 92 value_to_write.SetType(full_reg_info); 93 reg_to_write = full_reg; 94 } 95 } 96 } 97 98 const RegisterInfo *const register_to_write_info_p = 99 GetRegisterInfoAtIndex(reg_to_write); 100 assert(register_to_write_info_p && 101 "register to write does not have valid RegisterInfo"); 102 if (!register_to_write_info_p) 103 return Status("NativeRegisterContextLinux::%s failed to get RegisterInfo " 104 "for write register index %" PRIu32, 105 __FUNCTION__, reg_to_write); 106 107 return DoWriteRegisterValue(reg_info->byte_offset, reg_info->name, reg_value); 108 } 109 110 Status NativeRegisterContextLinux::ReadGPR() { 111 void *buf = GetGPRBuffer(); 112 if (!buf) 113 return Status("GPR buffer is NULL"); 114 size_t buf_size = GetGPRSize(); 115 116 return DoReadGPR(buf, buf_size); 117 } 118 119 Status NativeRegisterContextLinux::WriteGPR() { 120 void *buf = GetGPRBuffer(); 121 if (!buf) 122 return Status("GPR buffer is NULL"); 123 size_t buf_size = GetGPRSize(); 124 125 return DoWriteGPR(buf, buf_size); 126 } 127 128 Status NativeRegisterContextLinux::ReadFPR() { 129 void *buf = GetFPRBuffer(); 130 if (!buf) 131 return Status("FPR buffer is NULL"); 132 size_t buf_size = GetFPRSize(); 133 134 return DoReadFPR(buf, buf_size); 135 } 136 137 Status NativeRegisterContextLinux::WriteFPR() { 138 void *buf = GetFPRBuffer(); 139 if (!buf) 140 return Status("FPR buffer is NULL"); 141 size_t buf_size = GetFPRSize(); 142 143 return DoWriteFPR(buf, buf_size); 144 } 145 146 Status NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size, 147 unsigned int regset) { 148 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(), 149 static_cast<void *>(®set), buf, 150 buf_size); 151 } 152 153 Status NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size, 154 unsigned int regset) { 155 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), 156 static_cast<void *>(®set), buf, 157 buf_size); 158 } 159 160 Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset, 161 const char *reg_name, 162 uint32_t size, 163 RegisterValue &value) { 164 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS)); 165 166 long data; 167 Status error = NativeProcessLinux::PtraceWrapper( 168 PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), 169 nullptr, 0, &data); 170 171 if (error.Success()) 172 // First cast to an unsigned of the same size to avoid sign extension. 173 value.SetUInt(static_cast<unsigned long>(data), size); 174 175 LLDB_LOG(log, "{0}: {1:x}", reg_name, data); 176 return error; 177 } 178 179 Status NativeRegisterContextLinux::DoWriteRegisterValue( 180 uint32_t offset, const char *reg_name, const RegisterValue &value) { 181 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS)); 182 183 void *buf = reinterpret_cast<void *>(value.GetAsUInt64()); 184 LLDB_LOG(log, "{0}: {1}", reg_name, buf); 185 186 return NativeProcessLinux::PtraceWrapper( 187 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf); 188 } 189 190 Status NativeRegisterContextLinux::DoReadGPR(void *buf, size_t buf_size) { 191 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), 192 nullptr, buf, buf_size); 193 } 194 195 Status NativeRegisterContextLinux::DoWriteGPR(void *buf, size_t buf_size) { 196 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), 197 nullptr, buf, buf_size); 198 } 199 200 Status NativeRegisterContextLinux::DoReadFPR(void *buf, size_t buf_size) { 201 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), 202 nullptr, buf, buf_size); 203 } 204 205 Status NativeRegisterContextLinux::DoWriteFPR(void *buf, size_t buf_size) { 206 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), 207 nullptr, buf, buf_size); 208 } 209