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 ®_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 ®_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 *>(®set), 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 *>(®set), 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 if (log) 175 log->Printf("NativeRegisterContextLinux::%s() reg %s: 0x%lx", __FUNCTION__, 176 reg_name, data); 177 178 return error; 179 } 180 181 Error NativeRegisterContextLinux::DoWriteRegisterValue( 182 uint32_t offset, const char *reg_name, const RegisterValue &value) { 183 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS)); 184 185 void *buf = reinterpret_cast<void *>(value.GetAsUInt64()); 186 187 if (log) 188 log->Printf("NativeRegisterContextLinux::%s() reg %s: %p", __FUNCTION__, 189 reg_name, buf); 190 191 return NativeProcessLinux::PtraceWrapper( 192 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf); 193 } 194 195 Error NativeRegisterContextLinux::DoReadGPR(void *buf, size_t buf_size) { 196 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGS, m_thread.GetID(), 197 nullptr, buf, buf_size); 198 } 199 200 Error NativeRegisterContextLinux::DoWriteGPR(void *buf, size_t buf_size) { 201 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGS, m_thread.GetID(), 202 nullptr, buf, buf_size); 203 } 204 205 Error NativeRegisterContextLinux::DoReadFPR(void *buf, size_t buf_size) { 206 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), 207 nullptr, buf, buf_size); 208 } 209 210 Error NativeRegisterContextLinux::DoWriteFPR(void *buf, size_t buf_size) { 211 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), 212 nullptr, buf, buf_size); 213 } 214