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