LLDB  mainline
NativeRegisterContextLinux.cpp
Go to the documentation of this file.
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 
10 
13 #include "lldb/Host/linux/Ptrace.h"
15 
18 
19 using namespace lldb_private;
20 using namespace lldb_private::process_linux;
21 
23  return m_thread.GetProcess().GetByteOrder();
24 }
25 
27  RegisterValue &reg_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
38  const RegisterValue &reg_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();
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) {
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 
96 }
97 
101 }
102 
105  nullptr, GetFPRBuffer(),
106  GetFPRSize());
107 }
108 
111  nullptr, GetFPRBuffer(),
112  GetFPRSize());
113 }
114 
116  unsigned int regset) {
118  static_cast<void *>(&regset), buf,
119  buf_size);
120 }
121 
123  unsigned int regset) {
125  static_cast<void *>(&regset), buf,
126  buf_size);
127 }
128 
130  const char *reg_name,
131  uint32_t size,
132  RegisterValue &value) {
134 
135  long data;
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 
149  uint32_t offset, const char *reg_name, const RegisterValue &value) {
151 
152  void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
153  LLDB_LOG(log, "{0}: {1}", reg_name, buf);
154 
156  PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
157 }
lldb_private::process_linux::NativeRegisterContextLinux::ReadGPR
virtual Status ReadGPR()
Definition: NativeRegisterContextLinux.cpp:93
lldb_private::process_linux::NativeRegisterContextLinux::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: NativeRegisterContextLinux.cpp:22
PTRACE_GETREGSET
#define PTRACE_GETREGSET
Definition: Ptrace.h:36
PTRACE_SETREGS
#define PTRACE_SETREGS
Definition: Ptrace.h:27
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:91
lldb_private::RegisterValue::GetByteSize
uint32_t GetByteSize() const
Definition: RegisterValue.cpp:713
lldb_private::process_linux::NativeProcessLinux::PtraceWrapper
static Status PtraceWrapper(int req, lldb::pid_t pid, void *addr=nullptr, void *data=nullptr, size_t data_size=0, long *result=nullptr)
}
Definition: NativeProcessLinux.cpp:1980
lldb_private::NativeRegisterContext::ReadRegister
virtual Status ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
lldb_private::RegisterValue
Definition: RegisterValue.h:28
lldb_private::process_linux::NativeRegisterContextLinux::DoReadRegisterValue
virtual Status DoReadRegisterValue(uint32_t offset, const char *reg_name, uint32_t size, RegisterValue &value)
Definition: NativeRegisterContextLinux.cpp:129
lldb_private::process_linux::NativeRegisterContextLinux::GetFPRBuffer
virtual void * GetFPRBuffer()=0
PTRACE_GETREGS
#define PTRACE_GETREGS
Definition: Ptrace.h:24
lldb_private::process_linux::NativeRegisterContextLinux::WriteFPR
virtual Status WriteFPR()
Definition: NativeRegisterContextLinux.cpp:109
lldb_private::NativeThreadProtocol::GetProcess
NativeProcessProtocol & GetProcess()
Definition: NativeThreadProtocol.h:37
lldb_private::process_linux::NativeRegisterContextLinux::GetFPRSize
virtual size_t GetFPRSize()=0
RegisterValue.h
lldb_private::RegisterValue::SetBytes
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
Definition: RegisterValue.cpp:752
PTRACE_SETFPREGS
#define PTRACE_SETFPREGS
Definition: Ptrace.h:33
lldb_private::process_linux::NativeRegisterContextLinux::GetGPRBuffer
virtual void * GetGPRBuffer()=0
lldb_private::process_linux::NativeRegisterContextLinux::DoWriteRegisterValue
virtual Status DoWriteRegisterValue(uint32_t offset, const char *reg_name, const RegisterValue &value)
Definition: NativeRegisterContextLinux.cpp:148
lldb_private::RegisterValue::GetAsUInt64
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
Definition: RegisterValue.cpp:563
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
POSIX_LOG_REGISTERS
#define POSIX_LOG_REGISTERS
Definition: ProcessPOSIXLog.h:20
PTRACE_SETREGSET
#define PTRACE_SETREGSET
Definition: Ptrace.h:39
lldb_private::process_linux
Definition: IntelPTManager.h:24
ProcessPOSIXLog.h
lldb_private::process_linux::NativeRegisterContextLinux::ReadFPR
virtual Status ReadFPR()
Definition: NativeRegisterContextLinux.cpp:103
lldb_private::RegisterValue::kMaxRegisterByteSize
@ kMaxRegisterByteSize
Definition: RegisterValue.h:31
lldb_private::RegisterValue::SetType
void SetType(RegisterValue::Type type)
Definition: RegisterValue.h:85
NativeProcessProtocol.h
arm64_dwarf::x1
@ x1
Definition: ARM64_DWARF_Registers.h:18
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::process_linux::NativeRegisterContextLinux::WriteGPR
virtual Status WriteGPR()
Definition: NativeRegisterContextLinux.cpp:98
lldb_private::RegisterValue::SetUInt
bool SetUInt(uint64_t uint, uint32_t byte_size)
Definition: RegisterValue.cpp:734
NativeThreadProtocol.h
lldb_private::RegisterValue::GetAsMemoryData
uint32_t GetAsMemoryData(const RegisterInfo *reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition: RegisterValue.cpp:38
lldb_private::process_linux::NativeRegisterContextLinux::ReadRegisterRaw
virtual Status ReadRegisterRaw(uint32_t reg_index, RegisterValue &reg_value)
Definition: NativeRegisterContextLinux.cpp:26
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:35
lldb_private::process_linux::NativeRegisterContextLinux::WriteRegisterSet
virtual Status WriteRegisterSet(void *buf, size_t buf_size, unsigned int regset)
Definition: NativeRegisterContextLinux.cpp:122
Ptrace.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:242
NativeRegisterContextLinux.h
lldb_private::NativeProcessProtocol::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: NativeProcessProtocol.h:184
lldb_private::NativeRegisterContext::m_thread
NativeThreadProtocol & m_thread
Definition: NativeRegisterContext.h:176
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
NativeProcessLinux.h
lldb_private::Log
Definition: Log.h:49
lldb_private::process_linux::NativeRegisterContextLinux::ReadRegisterSet
virtual Status ReadRegisterSet(void *buf, size_t buf_size, unsigned int regset)
Definition: NativeRegisterContextLinux.cpp:115
lldb_private::ProcessPOSIXLog::GetLogIfAllCategoriesSet
static Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: ProcessPOSIXLog.h:33
lldb_private::process_linux::NativeRegisterContextLinux::GetPtraceOffset
virtual uint32_t GetPtraceOffset(uint32_t reg_index)
Definition: NativeRegisterContextLinux.h:116
PTRACE_GETFPREGS
#define PTRACE_GETFPREGS
Definition: Ptrace.h:30
lldb_private::process_linux::NativeRegisterContextLinux::WriteRegisterRaw
virtual Status WriteRegisterRaw(uint32_t reg_index, const RegisterValue &reg_value)
Definition: NativeRegisterContextLinux.cpp:37
lldb_private::NativeRegisterContextRegisterInfo::GetRegisterInfoAtIndex
const RegisterInfo * GetRegisterInfoAtIndex(uint32_t reg_index) const override
Definition: NativeRegisterContextRegisterInfo.cpp:31
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138
lldb_private::process_linux::NativeRegisterContextLinux::GetGPRSize
virtual size_t GetGPRSize() const
Definition: NativeRegisterContextLinux.h:108