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
9#include "RegisterContextMinidump_x86_64.h"
10
11#include "lldb/Utility/DataBufferHeap.h"
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_base: 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(N: 2);
31 break;
32 case lldb_rflags_x86_64:
33 return bytes.take_front(N: 4);
34 break;
35 default:
36 return bytes.take_front(N: 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(dest: reg_dest.data(), src: reg_src, n: reg_dest.size());
45}
46
47lldb::DataBufferSP lldb_private::minidump::ConvertMinidumpContext_x86_64(
48 llvm::ArrayRef<uint8_t> source_data,
49 RegisterInfoInterface *target_reg_interface) {
50
51 const RegisterInfo *reg_info = target_reg_interface->GetRegisterInfo();
52
53 lldb::WritableDataBufferSP result_context_buf(
54 new DataBufferHeap(target_reg_interface->GetGPRSize(), 0));
55 uint8_t *result_base = result_context_buf->GetBytes();
56
57 if (source_data.size() < sizeof(MinidumpContext_x86_64))
58 return nullptr;
59
60 const MinidumpContext_x86_64 *context;
61 consumeObject(Buffer&: source_data, Object&: context);
62
63 const MinidumpContext_x86_64_Flags context_flags =
64 static_cast<MinidumpContext_x86_64_Flags>(
65 static_cast<uint32_t>(context->context_flags));
66 auto x86_64_Flag = MinidumpContext_x86_64_Flags::x86_64_Flag;
67 auto ControlFlag = MinidumpContext_x86_64_Flags::Control;
68 auto IntegerFlag = MinidumpContext_x86_64_Flags::Integer;
69 auto SegmentsFlag = MinidumpContext_x86_64_Flags::Segments;
70
71 if ((context_flags & x86_64_Flag) != x86_64_Flag)
72 return nullptr;
73
74 if ((context_flags & ControlFlag) == ControlFlag) {
75 writeRegister(reg_src: &context->cs, context: result_base, reg: reg_info[lldb_cs_x86_64]);
76 writeRegister(reg_src: &context->ss, context: result_base, reg: reg_info[lldb_ss_x86_64]);
77 writeRegister(reg_src: &context->eflags, context: result_base, reg: reg_info[lldb_rflags_x86_64]);
78 writeRegister(reg_src: &context->rsp, context: result_base, reg: reg_info[lldb_rsp_x86_64]);
79 writeRegister(reg_src: &context->rip, context: result_base, reg: reg_info[lldb_rip_x86_64]);
80 }
81
82 if ((context_flags & SegmentsFlag) == SegmentsFlag) {
83 writeRegister(reg_src: &context->ds, context: result_base, reg: reg_info[lldb_ds_x86_64]);
84 writeRegister(reg_src: &context->es, context: result_base, reg: reg_info[lldb_es_x86_64]);
85 writeRegister(reg_src: &context->fs, context: result_base, reg: reg_info[lldb_fs_x86_64]);
86 writeRegister(reg_src: &context->gs, context: result_base, reg: reg_info[lldb_gs_x86_64]);
87 }
88
89 if ((context_flags & IntegerFlag) == IntegerFlag) {
90 writeRegister(reg_src: &context->rax, context: result_base, reg: reg_info[lldb_rax_x86_64]);
91 writeRegister(reg_src: &context->rcx, context: result_base, reg: reg_info[lldb_rcx_x86_64]);
92 writeRegister(reg_src: &context->rdx, context: result_base, reg: reg_info[lldb_rdx_x86_64]);
93 writeRegister(reg_src: &context->rbx, context: result_base, reg: reg_info[lldb_rbx_x86_64]);
94 writeRegister(reg_src: &context->rbp, context: result_base, reg: reg_info[lldb_rbp_x86_64]);
95 writeRegister(reg_src: &context->rsi, context: result_base, reg: reg_info[lldb_rsi_x86_64]);
96 writeRegister(reg_src: &context->rdi, context: result_base, reg: reg_info[lldb_rdi_x86_64]);
97 writeRegister(reg_src: &context->r8, context: result_base, reg: reg_info[lldb_r8_x86_64]);
98 writeRegister(reg_src: &context->r9, context: result_base, reg: reg_info[lldb_r9_x86_64]);
99 writeRegister(reg_src: &context->r10, context: result_base, reg: reg_info[lldb_r10_x86_64]);
100 writeRegister(reg_src: &context->r11, context: result_base, reg: reg_info[lldb_r11_x86_64]);
101 writeRegister(reg_src: &context->r12, context: result_base, reg: reg_info[lldb_r12_x86_64]);
102 writeRegister(reg_src: &context->r13, context: result_base, reg: reg_info[lldb_r13_x86_64]);
103 writeRegister(reg_src: &context->r14, context: result_base, reg: reg_info[lldb_r14_x86_64]);
104 writeRegister(reg_src: &context->r15, context: result_base, reg: reg_info[lldb_r15_x86_64]);
105 }
106
107 // TODO parse the floating point registers
108
109 return result_context_buf;
110}
111

source code of lldb/source/Plugins/Process/minidump/RegisterContextMinidump_x86_64.cpp