LLDB mainline
RegisterContextMinidump_x86_64.cpp
Go to the documentation of this file.
1//===-- RegisterContextMinidump_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
10
12
13// C includes
14// C++ includes
15
16using namespace lldb_private;
17using namespace minidump;
18
19static llvm::MutableArrayRef<uint8_t> getDestRegister(uint8_t *context,
20 const RegisterInfo &reg) {
21 auto bytes = reg.mutable_data(context);
22
23 switch (reg.kinds[lldb::eRegisterKindLLDB]) {
24 case lldb_cs_x86_64:
25 case lldb_ds_x86_64:
26 case lldb_es_x86_64:
27 case lldb_fs_x86_64:
28 case lldb_gs_x86_64:
29 case lldb_ss_x86_64:
30 return bytes.take_front(2);
31 break;
33 return bytes.take_front(4);
34 break;
35 default:
36 return bytes.take_front(8);
37 break;
38 }
39}
40
41static void writeRegister(const void *reg_src, uint8_t *context,
42 const RegisterInfo &reg) {
43 llvm::MutableArrayRef<uint8_t> reg_dest = getDestRegister(context, reg);
44 memcpy(reg_dest.data(), reg_src, reg_dest.size());
45}
46
47// TODO: Fix the registers in this file!
48// writeRegister checks x86_64 registers without base registers. This causes
49// an overlap in the register enum values. So we were truncating fs_base.
50// We should standardize to the x86_64_with_base registers.
51static void writeBaseRegister(const void *reg_src, uint8_t *context,
52 const RegisterInfo &reg) {
53 auto bytes = reg.mutable_data(context);
54 llvm::MutableArrayRef<uint8_t> reg_dest = bytes.take_front(8);
55 memcpy(reg_dest.data(), reg_src, reg_dest.size());
56}
57
59 llvm::ArrayRef<uint8_t> source_data,
60 RegisterInfoInterface *target_reg_interface) {
61
62 const RegisterInfo *reg_info = target_reg_interface->GetRegisterInfo();
63
64 lldb::WritableDataBufferSP result_context_buf(
65 new DataBufferHeap(target_reg_interface->GetGPRSize(), 0));
66 uint8_t *result_base = result_context_buf->GetBytes();
67
68 if (source_data.size() < sizeof(MinidumpContext_x86_64))
69 return nullptr;
70
71 const MinidumpContext_x86_64 *context;
72 consumeObject(source_data, context);
73
74 const MinidumpContext_x86_64_Flags context_flags =
76 static_cast<uint32_t>(context->context_flags));
77 auto x86_64_Flag = MinidumpContext_x86_64_Flags::x86_64_Flag;
78 auto ControlFlag = MinidumpContext_x86_64_Flags::Control;
79 auto IntegerFlag = MinidumpContext_x86_64_Flags::Integer;
80 auto SegmentsFlag = MinidumpContext_x86_64_Flags::Segments;
81 auto LLDBSpecificFlag = MinidumpContext_x86_64_Flags::LLDBSpecific;
82
83 if ((context_flags & x86_64_Flag) != x86_64_Flag)
84 return nullptr;
85
86 if ((context_flags & ControlFlag) == ControlFlag) {
87 writeRegister(&context->cs, result_base, reg_info[lldb_cs_x86_64]);
88 writeRegister(&context->ss, result_base, reg_info[lldb_ss_x86_64]);
89 writeRegister(&context->eflags, result_base, reg_info[lldb_rflags_x86_64]);
90 writeRegister(&context->rsp, result_base, reg_info[lldb_rsp_x86_64]);
91 writeRegister(&context->rip, result_base, reg_info[lldb_rip_x86_64]);
92 }
93
94 if ((context_flags & SegmentsFlag) == SegmentsFlag) {
95 writeRegister(&context->ds, result_base, reg_info[lldb_ds_x86_64]);
96 writeRegister(&context->es, result_base, reg_info[lldb_es_x86_64]);
97 writeRegister(&context->fs, result_base, reg_info[lldb_fs_x86_64]);
98 writeRegister(&context->gs, result_base, reg_info[lldb_gs_x86_64]);
99 }
100
101 if ((context_flags & IntegerFlag) == IntegerFlag) {
102 writeRegister(&context->rax, result_base, reg_info[lldb_rax_x86_64]);
103 writeRegister(&context->rcx, result_base, reg_info[lldb_rcx_x86_64]);
104 writeRegister(&context->rdx, result_base, reg_info[lldb_rdx_x86_64]);
105 writeRegister(&context->rbx, result_base, reg_info[lldb_rbx_x86_64]);
106 writeRegister(&context->rbp, result_base, reg_info[lldb_rbp_x86_64]);
107 writeRegister(&context->rsi, result_base, reg_info[lldb_rsi_x86_64]);
108 writeRegister(&context->rdi, result_base, reg_info[lldb_rdi_x86_64]);
109 writeRegister(&context->r8, result_base, reg_info[lldb_r8_x86_64]);
110 writeRegister(&context->r9, result_base, reg_info[lldb_r9_x86_64]);
111 writeRegister(&context->r10, result_base, reg_info[lldb_r10_x86_64]);
112 writeRegister(&context->r11, result_base, reg_info[lldb_r11_x86_64]);
113 writeRegister(&context->r12, result_base, reg_info[lldb_r12_x86_64]);
114 writeRegister(&context->r13, result_base, reg_info[lldb_r13_x86_64]);
115 writeRegister(&context->r14, result_base, reg_info[lldb_r14_x86_64]);
116 writeRegister(&context->r15, result_base, reg_info[lldb_r15_x86_64]);
117 }
118
119 // See comment on base regsiter
120 if ((context_flags & LLDBSpecificFlag) == LLDBSpecificFlag) {
121 writeBaseRegister(&context->fs_base, result_base,
123 writeBaseRegister(&context->gs_base, result_base,
125 }
126
127 // TODO parse the floating point registers
128
129 return result_context_buf;
130}
static void writeRegister(const void *reg_src, uint8_t *context, const RegisterInfo &reg)
static void writeBaseRegister(const void *reg_src, uint8_t *context, const RegisterInfo &reg)
static llvm::MutableArrayRef< uint8_t > getDestRegister(uint8_t *context, const RegisterInfo &reg)
A subclass of DataBuffer that stores a data buffer on the heap.
RegisterInfo interface to patch RegisterInfo structure for archs.
virtual size_t GetGPRSize() const =0
virtual const lldb_private::RegisterInfo * GetRegisterInfo() const =0
Status consumeObject(llvm::ArrayRef< uint8_t > &Buffer, const T *&Object)
Definition: MinidumpTypes.h:50
lldb::DataBufferSP ConvertMinidumpContext_x86_64(llvm::ArrayRef< uint8_t > source_data, RegisterInfoInterface *target_reg_interface)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:337
@ eRegisterKindLLDB
lldb's internal register numbers
Every register is described in detail including its name, alternate name (optional),...
llvm::MutableArrayRef< uint8_t > mutable_data(uint8_t *context_base) const
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.