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