LLDB  mainline
RegisterContextLinux_x86_64.cpp
Go to the documentation of this file.
1 //===-- RegisterContextLinux_x86_64.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 
12 #include <vector>
13 
14 using namespace lldb_private;
15 using namespace lldb;
16 
17 typedef struct _GPR {
18  uint64_t r15;
19  uint64_t r14;
20  uint64_t r13;
21  uint64_t r12;
22  uint64_t rbp;
23  uint64_t rbx;
24  uint64_t r11;
25  uint64_t r10;
26  uint64_t r9;
27  uint64_t r8;
28  uint64_t rax;
29  uint64_t rcx;
30  uint64_t rdx;
31  uint64_t rsi;
32  uint64_t rdi;
33  uint64_t orig_rax;
34  uint64_t rip;
35  uint64_t cs;
36  uint64_t rflags;
37  uint64_t rsp;
38  uint64_t ss;
39  uint64_t fs_base;
40  uint64_t gs_base;
41  uint64_t ds;
42  uint64_t es;
43  uint64_t fs;
44  uint64_t gs;
45 } GPR;
46 
47 struct DBG {
48  uint64_t dr[8];
49 };
50 
51 struct UserArea {
52  GPR gpr; // General purpose registers.
53  int32_t fpvalid; // True if FPU is being used.
54  int32_t pad0;
55  FXSAVE fpr; // General purpose floating point registers (see FPR for extended
56  // register sets).
57  uint64_t tsize; // Text segment size.
58  uint64_t dsize; // Data segment size.
59  uint64_t ssize; // Stack segment size.
60  uint64_t start_code; // VM address of text.
61  uint64_t start_stack; // VM address of stack bottom (top in rsp).
62  int64_t signal; // Signal causing core dump.
63  int32_t reserved; // Unused.
64  int32_t pad1;
65  uint64_t ar0; // Location of GPR's.
66  FXSAVE *fpstate; // Location of FPR's.
67  uint64_t magic; // Identifier for core dumps.
68  char u_comm[32]; // Command causing core dump.
69  DBG dbg; // Debug registers.
70  uint64_t error_code; // CPU error code.
71  uint64_t fault_address; // Control register CR3.
72 };
73 
74 #define DR_OFFSET(reg_index) \
75  (LLVM_EXTENSION offsetof(UserArea, dbg) + \
76  LLVM_EXTENSION offsetof(DBG, dr[reg_index]))
77 
78 // Include RegisterInfos_x86_64 to declare our g_register_infos_x86_64
79 // structure.
80 #define DECLARE_REGISTER_INFOS_X86_64_STRUCT
81 #include "RegisterInfos_x86_64.h"
82 #undef DECLARE_REGISTER_INFOS_X86_64_STRUCT
83 
84 static std::vector<lldb_private::RegisterInfo> &GetPrivateRegisterInfoVector() {
85  static std::vector<lldb_private::RegisterInfo> g_register_infos;
86  return g_register_infos;
87 }
88 
89 static const RegisterInfo *
91  std::vector<lldb_private::RegisterInfo> &g_register_infos =
93 
94  // Allocate RegisterInfo only once
95  if (g_register_infos.empty()) {
96  // Copy the register information from base class
97  std::unique_ptr<RegisterContextLinux_i386> reg_interface(
98  new RegisterContextLinux_i386(arch));
99  const RegisterInfo *base_info = reg_interface->GetRegisterInfo();
100  g_register_infos.insert(g_register_infos.end(), &base_info[0],
101  &base_info[k_num_registers_i386]);
102 
103 // Include RegisterInfos_x86_64 to update the g_register_infos structure
104 // with x86_64 offsets.
105 #define UPDATE_REGISTER_INFOS_I386_STRUCT_WITH_X86_64_OFFSETS
106 #include "RegisterInfos_x86_64.h"
107 #undef UPDATE_REGISTER_INFOS_I386_STRUCT_WITH_X86_64_OFFSETS
108  }
109 
110  return &g_register_infos[0];
111 }
112 
113 static const RegisterInfo *GetRegisterInfoPtr(const ArchSpec &target_arch) {
114  switch (target_arch.GetMachine()) {
115  case llvm::Triple::x86:
116  return GetRegisterInfo_i386(target_arch);
117  case llvm::Triple::x86_64:
118  return g_register_infos_x86_64;
119  default:
120  assert(false && "Unhandled target architecture.");
121  return nullptr;
122  }
123 }
124 
125 static uint32_t GetRegisterInfoCount(const ArchSpec &target_arch) {
126  switch (target_arch.GetMachine()) {
127  case llvm::Triple::x86: {
128  assert(!GetPrivateRegisterInfoVector().empty() &&
129  "i386 register info not yet filled.");
130  return static_cast<uint32_t>(GetPrivateRegisterInfoVector().size());
131  }
132  case llvm::Triple::x86_64:
133  return static_cast<uint32_t>(sizeof(g_register_infos_x86_64) /
134  sizeof(g_register_infos_x86_64[0]));
135  default:
136  assert(false && "Unhandled target architecture.");
137  return 0;
138  }
139 }
140 
141 static uint32_t GetUserRegisterInfoCount(const ArchSpec &target_arch) {
142  switch (target_arch.GetMachine()) {
143  case llvm::Triple::x86:
144  return static_cast<uint32_t>(k_num_user_registers_i386);
145  case llvm::Triple::x86_64:
146  return static_cast<uint32_t>(k_num_user_registers_x86_64);
147  default:
148  assert(false && "Unhandled target architecture.");
149  return 0;
150  }
151 }
152 
154  const ArchSpec &target_arch)
155  : lldb_private::RegisterInfoInterface(target_arch),
156  m_register_info_p(GetRegisterInfoPtr(target_arch)),
157  m_register_info_count(GetRegisterInfoCount(target_arch)),
158  m_user_register_count(GetUserRegisterInfoCount(target_arch)) {
159  RegisterInfo orig_ax = {
160  "orig_rax",
161  nullptr,
162  sizeof(((GPR *)nullptr)->orig_rax),
163  (LLVM_EXTENSION offsetof(GPR, orig_rax)),
165  eFormatHex,
168  nullptr,
169  nullptr,
170  };
171  d_register_infos.push_back(orig_ax);
172 }
173 
174 size_t RegisterContextLinux_x86_64::GetGPRSize() const { return sizeof(GPR); }
175 
176 const std::vector<lldb_private::RegisterInfo> *
178  return &d_register_infos;
179 }
180 
181 const RegisterInfo *RegisterContextLinux_x86_64::GetRegisterInfo() const {
182  return m_register_info_p;
183 }
184 
186  return m_register_info_count;
187 }
188 
190  return m_user_register_count;
191 }
RegisterContextLinux_x86_64::GetUserRegisterCount
uint32_t GetUserRegisterCount() const override
Definition: RegisterContextLinux_x86_64.cpp:189
_GPR::ds
uint64_t ds
Definition: RegisterContextLinux_x86_64.cpp:41
DBG
Definition: RegisterContextFreeBSD_i386.cpp:38
r12
@ r12
Definition: CompactUnwindInfo.cpp:705
lldb_private::ArchSpec
Definition: ArchSpec.h:33
rax
@ rax
Definition: CompactUnwindInfo.cpp:693
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:91
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
_GPR::es
uint64_t es
Definition: RegisterContextLinux_x86_64.cpp:42
UserArea::start_stack
uint64_t start_stack
Definition: RegisterContextLinux_x86_64.cpp:61
RegisterContextLinux_x86_64::m_register_info_p
const lldb_private::RegisterInfo * m_register_info_p
Definition: RegisterContextLinux_x86_64.h:30
RegisterContextLinux_x86_64::GetRegisterInfo
const lldb_private::RegisterInfo * GetRegisterInfo() const override
Definition: RegisterContextLinux_x86_64.cpp:181
RegisterContextLinux_x86_64.h
r10
@ r10
Definition: CompactUnwindInfo.cpp:703
lldb_private::RegisterInfoInterface
Definition: RegisterInfoInterface.h:21
RegisterContextLinux_x86_64::GetDynamicRegisterInfoP
const std::vector< lldb_private::RegisterInfo > * GetDynamicRegisterInfoP() const override
Definition: RegisterContextLinux_x86_64.cpp:177
GetPrivateRegisterInfoVector
static std::vector< lldb_private::RegisterInfo > & GetPrivateRegisterInfoVector()
Definition: RegisterContextLinux_x86_64.cpp:84
RegisterContextLinux_x86_64::d_register_infos
std::vector< lldb_private::RegisterInfo > d_register_infos
Definition: RegisterContextLinux_x86_64.h:33
RegisterInfos_x86_64.h
UserArea::start_code
uint64_t start_code
Definition: RegisterContextLinux_x86_64.cpp:60
GetRegisterInfoCount
static uint32_t GetRegisterInfoCount(const ArchSpec &target_arch)
Definition: RegisterContextLinux_x86_64.cpp:125
g_register_infos
static const RegisterInfo g_register_infos[]
Definition: ABIMacOSX_arm.cpp:37
rdi
@ rdi
Definition: CompactUnwindInfo.cpp:698
GetUserRegisterInfoCount
static uint32_t GetUserRegisterInfoCount(const ArchSpec &target_arch)
Definition: RegisterContextLinux_x86_64.cpp:141
UserArea
Definition: RegisterContext_mips.h:368
RegisterContextLinux_i386.h
RegisterContextPOSIX_x86.h
r8
@ r8
Definition: CompactUnwindInfo.cpp:701
rcx
@ rcx
Definition: CompactUnwindInfo.cpp:695
_GPR::orig_rax
uint64_t orig_rax
Definition: RegisterContextLinux_x86_64.cpp:33
UserArea::pad0
int32_t pad0
Definition: RegisterContextLinux_x86_64.cpp:54
rbx
@ rbx
Definition: CompactUnwindInfo.cpp:696
r11
@ r11
Definition: CompactUnwindInfo.cpp:704
UserArea::ar0
uint64_t ar0
Definition: RegisterContextLinux_x86_64.cpp:65
r14
@ r14
Definition: CompactUnwindInfo.cpp:707
RegisterContextLinux_x86_64::RegisterContextLinux_x86_64
RegisterContextLinux_x86_64(const lldb_private::ArchSpec &target_arch)
Definition: RegisterContextLinux_x86_64.cpp:153
lldb::eEncodingUint
@ eEncodingUint
unsigned integer
Definition: lldb-enumerations.h:148
lldb_private::FXSAVE
Definition: RegisterContext_x86.h:266
_GPR::gs_base
uint64_t gs_base
Definition: RegisterContextLinux_x86_64.cpp:40
UserArea::fault_address
uint64_t fault_address
Definition: RegisterContextLinux_x86_64.cpp:71
UserArea::ssize
uint64_t ssize
Definition: RegisterContextLinux_x86_64.cpp:59
GetRegisterInfo_i386
static const RegisterInfo * GetRegisterInfo_i386(const lldb_private::ArchSpec &arch)
Definition: RegisterContextLinux_x86_64.cpp:90
UserArea::error_code
uint64_t error_code
Definition: RegisterContextLinux_x86_64.cpp:70
RegisterContextLinux_i386
Definition: RegisterContextLinux_i386.h:14
GPR
struct _GPR GPR
UserArea::fpstate
FXSAVE * fpstate
Definition: RegisterContextLinux_x86_64.cpp:66
RegisterContextLinux_x86_64::GetRegisterCount
uint32_t GetRegisterCount() const override
Definition: RegisterContextLinux_x86_64.cpp:185
GPR
Definition: RegisterContextFreeBSD_i386.cpp:16
UserArea::tsize
uint64_t tsize
Definition: RegisterContextLinux_x86_64.cpp:57
lldb_private::k_num_registers_i386
@ k_num_registers_i386
Definition: lldb-x86-register-enums.h:127
UserArea::fpr
FXSAVE fpr
Definition: RegisterContextLinux_x86_64.cpp:55
rdx
@ rdx
Definition: CompactUnwindInfo.cpp:694
uint32_t
_GPR::fs
uint64_t fs
Definition: RegisterContextLinux_x86_64.cpp:43
GetRegisterInfoPtr
static const RegisterInfo * GetRegisterInfoPtr(const ArchSpec &target_arch)
Definition: RegisterContextLinux_x86_64.cpp:113
_GPR::gs
uint64_t gs
Definition: RegisterContextLinux_x86_64.cpp:44
rip
@ rip
Definition: CompactUnwindInfo.cpp:709
_GPR::fs_base
uint64_t fs_base
Definition: RegisterContextLinux_x86_64.cpp:39
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::eFormatHex
@ eFormatHex
Definition: lldb-enumerations.h:169
r15
@ r15
Definition: CompactUnwindInfo.cpp:708
RegisterContextLinux_x86_64::m_user_register_count
uint32_t m_user_register_count
Definition: RegisterContextLinux_x86_64.h:32
lldb_private::k_num_user_registers_x86_64
@ k_num_user_registers_x86_64
Definition: lldb-x86-register-enums.h:318
rsi
@ rsi
Definition: CompactUnwindInfo.cpp:697
r9
@ r9
Definition: CompactUnwindInfo.cpp:702
lldb
Definition: SBAddress.h:15
UserArea::dsize
uint64_t dsize
Definition: RegisterContextLinux_x86_64.cpp:58
RegisterContextLinux_x86_64::m_register_info_count
uint32_t m_register_info_count
Definition: RegisterContextLinux_x86_64.h:31
rbp
@ rbp
Definition: CompactUnwindInfo.cpp:699
UserArea::signal
int64_t signal
Definition: RegisterContextLinux_x86_64.cpp:62
lldb_private::k_num_user_registers_i386
@ k_num_user_registers_i386
Definition: lldb-x86-register-enums.h:132
UserArea::magic
uint64_t magic
Definition: RegisterContextLinux_x86_64.cpp:67
rsp
@ rsp
Definition: CompactUnwindInfo.cpp:700
r13
@ r13
Definition: CompactUnwindInfo.cpp:706
UserArea::pad1
int32_t pad1
Definition: RegisterContextLinux_x86_64.cpp:64
RegisterContextLinux_x86_64::GetGPRSize
size_t GetGPRSize() const override
Definition: RegisterContextLinux_x86_64.cpp:174