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/HostInfo.h"
16 #include "lldb/Host/linux/Ptrace.h"
18 #include <sys/uio.h>
19 
20 using namespace lldb_private;
21 using namespace lldb_private::process_linux;
22 
24  return m_thread.GetProcess().GetByteOrder();
25 }
26 
28  RegisterValue &reg_value) {
29  const RegisterInfo *const reg_info = GetRegisterInfoAtIndex(reg_index);
30  if (!reg_info)
31  return Status("register %" PRIu32 " not found", reg_index);
32 
33  return DoReadRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
34  reg_info->byte_size, reg_value);
35 }
36 
37 Status
39  const RegisterValue &reg_value) {
40  uint32_t reg_to_write = reg_index;
41  RegisterValue value_to_write = reg_value;
42 
43  // Check if this is a subregister of a full register.
44  const RegisterInfo *reg_info = GetRegisterInfoAtIndex(reg_index);
45  assert(reg_info && "Expected valid register info for reg_index.");
46  if (reg_info->invalidate_regs &&
47  (reg_info->invalidate_regs[0] != LLDB_INVALID_REGNUM)) {
48  Status error;
49 
50  RegisterValue full_value;
51  uint32_t full_reg = reg_info->invalidate_regs[0];
52  const RegisterInfo *full_reg_info = GetRegisterInfoAtIndex(full_reg);
53 
54  // Read the full register.
55  error = ReadRegister(full_reg_info, full_value);
56  if (error.Fail()) {
57  // full_reg_info was nullptr, or we couldn't read the register.
58  return error;
59  }
60 
61  lldb::ByteOrder byte_order = GetByteOrder();
63 
64  // Get the bytes for the full register.
65  const uint32_t dest_size = full_value.GetAsMemoryData(
66  *full_reg_info, dst, sizeof(dst), byte_order, error);
67  if (error.Success() && dest_size) {
69 
70  // Get the bytes for the source data.
71  const uint32_t src_size = reg_value.GetAsMemoryData(
72  *reg_info, src, sizeof(src), byte_order, error);
73  if (error.Success() && src_size && (src_size < dest_size)) {
74  // Copy the src bytes to the destination.
75  memcpy(dst + (reg_info->byte_offset & 0x1), src, src_size);
76  // Set this full register as the value to write.
77  value_to_write.SetBytes(dst, full_value.GetByteSize(), byte_order);
78  value_to_write.SetType(*full_reg_info);
79  reg_to_write = full_reg;
80  }
81  }
82  }
83 
84  const RegisterInfo *const register_to_write_info_p =
85  GetRegisterInfoAtIndex(reg_to_write);
86  assert(register_to_write_info_p &&
87  "register to write does not have valid RegisterInfo");
88  if (!register_to_write_info_p)
89  return Status("NativeRegisterContextLinux::%s failed to get RegisterInfo "
90  "for write register index %" PRIu32,
91  __FUNCTION__, reg_to_write);
92 
93  return DoWriteRegisterValue(GetPtraceOffset(reg_index), reg_info->name,
94  reg_value);
95 }
96 
100 }
101 
105 }
106 
109  nullptr, GetFPRBuffer(),
110  GetFPRSize());
111 }
112 
115  nullptr, GetFPRBuffer(),
116  GetFPRSize());
117 }
118 
120  unsigned int regset) {
122  static_cast<void *>(&regset), buf,
123  buf_size);
124 }
125 
127  unsigned int regset) {
129  static_cast<void *>(&regset), buf,
130  buf_size);
131 }
132 
134  const char *reg_name,
135  uint32_t size,
136  RegisterValue &value) {
138 
139  long data;
141  PTRACE_PEEKUSER, m_thread.GetID(), reinterpret_cast<void *>(offset),
142  nullptr, 0, &data);
143 
144  if (error.Success())
145  // First cast to an unsigned of the same size to avoid sign extension.
146  value.SetUInt(static_cast<unsigned long>(data), size);
147 
148  LLDB_LOG(log, "{0}: {1:x}", reg_name, data);
149  return error;
150 }
151 
153  uint32_t offset, const char *reg_name, const RegisterValue &value) {
155 
156  void *buf = reinterpret_cast<void *>(value.GetAsUInt64());
157  LLDB_LOG(log, "{0}: {1}", reg_name, buf);
158 
160  PTRACE_POKEUSER, m_thread.GetID(), reinterpret_cast<void *>(offset), buf);
161 }
162 
163 llvm::Expected<ArchSpec>
165  size_t gpr64_size) {
166  std::unique_ptr<uint8_t[]> data = std::make_unique<uint8_t[]>(gpr64_size);
167  struct iovec iov;
168  iov.iov_base = data.get();
169  iov.iov_len = gpr64_size;
170  unsigned int regset = llvm::ELF::NT_PRSTATUS;
172  &iov, sizeof(iov));
173  if (ST.Fail())
174  return ST.ToError();
175  return HostInfo::GetArchitecture(
176  iov.iov_len < gpr64_size ? HostInfo::eArchKind32 : HostInfo::eArchKind64);
177 }
lldb_private::process_linux::NativeRegisterContextLinux::ReadGPR
virtual Status ReadGPR()
Definition: NativeRegisterContextLinux.cpp:97
lldb_private::process_linux::NativeRegisterContextLinux::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: NativeRegisterContextLinux.cpp:23
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:79
lldb_private::RegisterValue::GetByteSize
uint32_t GetByteSize() const
Definition: RegisterValue.cpp:706
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:1935
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:133
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:113
lldb_private::NativeThreadProtocol::GetProcess
NativeProcessProtocol & GetProcess()
Definition: NativeThreadProtocol.h:41
lldb_private::process_linux::NativeRegisterContextLinux::GetFPRSize
virtual size_t GetFPRSize()=0
RegisterValue.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::RegisterValue::SetBytes
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
Definition: RegisterValue.cpp:745
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:152
lldb_private::RegisterValue::GetAsUInt64
uint64_t GetAsUInt64(uint64_t fail_value=UINT64_MAX, bool *success_ptr=nullptr) const
Definition: RegisterValue.cpp:556
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
PTRACE_SETREGSET
#define PTRACE_SETREGSET
Definition: Ptrace.h:39
lldb_private::process_linux
Definition: IntelPTCollector.h:26
lldb_private::Status::Fail
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
ProcessPOSIXLog.h
lldb_private::process_linux::NativeRegisterContextLinux::ReadFPR
virtual Status ReadFPR()
Definition: NativeRegisterContextLinux.cpp:107
lldb_private::RegisterValue::SetType
void SetType(RegisterValue::Type type)
Definition: RegisterValue.h:85
HostInfo.h
arm64_dwarf::x1
@ x1
Definition: ARM64_DWARF_Registers.h:18
NativeProcessProtocol.h
lldb_private::POSIXLog::Registers
@ Registers
lldb_private::Status
Definition: Status.h:44
uint32_t
lldb_private::process_linux::NativeRegisterContextLinux::WriteGPR
virtual Status WriteGPR()
Definition: NativeRegisterContextLinux.cpp:102
lldb_private::RegisterValue::SetUInt
bool SetUInt(uint64_t uint, uint32_t byte_size)
Definition: RegisterValue.cpp:727
NativeThreadProtocol.h
lldb_private::process_linux::NativeRegisterContextLinux::ReadRegisterRaw
virtual Status ReadRegisterRaw(uint32_t reg_index, RegisterValue &reg_value)
Definition: NativeRegisterContextLinux.cpp:27
lldb_private::NativeThreadProtocol::GetID
lldb::tid_t GetID() const
Definition: NativeThreadProtocol.h:39
lldb_private::process_linux::NativeRegisterContextLinux::WriteRegisterSet
virtual Status WriteRegisterSet(void *buf, size_t buf_size, unsigned int regset)
Definition: NativeRegisterContextLinux.cpp:126
Ptrace.h
LLDB_LOG
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition: Log.h:337
NativeRegisterContextLinux.h
lldb_private::NativeProcessProtocol::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Definition: NativeProcessProtocol.h:195
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::Status::ToError
llvm::Error ToError() const
Definition: Status.cpp:89
lldb_private::Log
Definition: Log.h:115
lldb_private::process_linux::NativeRegisterContextLinux::ReadRegisterSet
virtual Status ReadRegisterSet(void *buf, size_t buf_size, unsigned int regset)
Definition: NativeRegisterContextLinux.cpp:119
lldb_private::process_linux::NativeRegisterContextLinux::GetPtraceOffset
virtual uint32_t GetPtraceOffset(uint32_t reg_index)
Definition: NativeRegisterContextLinux.h:122
lldb_private::RegisterValue::kMaxRegisterByteSize
@ kMaxRegisterByteSize
Definition: RegisterValue.h:31
lldb_private::GetLog
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
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:38
lldb_private::NativeRegisterContextRegisterInfo::GetRegisterInfoAtIndex
const RegisterInfo * GetRegisterInfoAtIndex(uint32_t reg_index) const override
Definition: NativeRegisterContextRegisterInfo.cpp:31
lldb_private::process_linux::NativeRegisterContextLinux::DetermineArchitectureViaGPR
static llvm::Expected< ArchSpec > DetermineArchitectureViaGPR(lldb::tid_t tid, size_t gpr64_size)
Definition: NativeRegisterContextLinux.cpp:164
lldb::tid_t
uint64_t tid_t
Definition: lldb-types.h:86
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:139
lldb_private::process_linux::NativeRegisterContextLinux::GetGPRSize
virtual size_t GetGPRSize() const
Definition: NativeRegisterContextLinux.h:114