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 ®_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(GetPtraceOffset(reg_index), reg_info->name, 38 reg_info->byte_size, reg_value); 39 } 40 41 Status 42 NativeRegisterContextLinux::WriteRegisterRaw(uint32_t reg_index, 43 const RegisterValue ®_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(GetPtraceOffset(reg_index), reg_info->name, 95 reg_value); 96 } 97 98 Status NativeRegisterContextLinux::ReadGPR() { 99 return NativeProcessLinux::PtraceWrapper( 100 PTRACE_GETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize()); 101 } 102 103 Status NativeRegisterContextLinux::WriteGPR() { 104 return NativeProcessLinux::PtraceWrapper( 105 PTRACE_SETREGS, m_thread.GetID(), nullptr, GetGPRBuffer(), GetGPRSize()); 106 } 107 108 Status NativeRegisterContextLinux::ReadFPR() { 109 return NativeProcessLinux::PtraceWrapper(PTRACE_GETFPREGS, m_thread.GetID(), 110 nullptr, GetFPRBuffer(), 111 GetFPRSize()); 112 } 113 114 Status NativeRegisterContextLinux::WriteFPR() { 115 return NativeProcessLinux::PtraceWrapper(PTRACE_SETFPREGS, m_thread.GetID(), 116 nullptr, GetFPRBuffer(), 117 GetFPRSize()); 118 } 119 120 Status NativeRegisterContextLinux::ReadRegisterSet(void *buf, size_t buf_size, 121 unsigned int regset) { 122 return NativeProcessLinux::PtraceWrapper(PTRACE_GETREGSET, m_thread.GetID(), 123 static_cast<void *>(®set), buf, 124 buf_size); 125 } 126 127 Status NativeRegisterContextLinux::WriteRegisterSet(void *buf, size_t buf_size, 128 unsigned int regset) { 129 return NativeProcessLinux::PtraceWrapper(PTRACE_SETREGSET, m_thread.GetID(), 130 static_cast<void *>(®set), buf, 131 buf_size); 132 } 133 134 Status NativeRegisterContextLinux::DoReadRegisterValue(uint32_t offset, 135 const char *reg_name, 136 uint32_t size, 137 RegisterValue &value) { 138 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS)); 139 140 long data; 141 Status error = NativeProcessLinux::PtraceWrapper( 142 PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), 143 nullptr, 0, &data); 144 145 if (error.Success()) 146 // First cast to an unsigned of the same size to avoid sign extension. 147 value.SetUInt(static_cast<unsigned long>(data), size); 148 149 LLDB_LOG(log, "{0}: {1:x}", reg_name, data); 150 return error; 151 } 152 153 Status NativeRegisterContextLinux::DoWriteRegisterValue( 154 uint32_t offset, const char *reg_name, const RegisterValue &value) { 155 Log *log(ProcessPOSIXLog::GetLogIfAllCategoriesSet(POSIX_LOG_REGISTERS)); 156 157 void *buf = reinterpret_cast<void *>(value.GetAsUInt64()); 158 LLDB_LOG(log, "{0}: {1}", reg_name, buf); 159 160 return NativeProcessLinux::PtraceWrapper( 161 PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf); 162 } 163