LLDB mainline
x86AssemblyInspectionEngine.h
Go to the documentation of this file.
1//===-- x86AssemblyInspectionEngine.h ---------------------------*- C++ -*-===//
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#ifndef LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H
10#define LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H
11
12#include "llvm-c/Disassembler.h"
13
17#include "lldb/lldb-forward.h"
18#include "lldb/lldb-private.h"
19
20#include <map>
21#include <vector>
22
23namespace lldb_private {
24
25// x86AssemblyInspectionEngine - a class which will take a buffer of bytes
26// of i386/x86_64 instructions and create an UnwindPlan based on those
27// assembly instructions.
29
30public:
31 /// default ctor
33
34 /// default dtor
36
37 /// One of the two initialize methods that can be called on this object;
38 /// they must be called before any of the assembly inspection methods
39 /// are called. This one should be used if the caller has access to a
40 /// valid RegisterContext.
42
43 /// One of the two initialize methods that can be called on this object;
44 /// they must be called before any of the assembly inspection methods
45 /// are called. This one takes a vector of register name and lldb
46 /// register numbers.
48 const char *name = nullptr;
50 lldb_reg_info() = default;
51 };
52 void Initialize(std::vector<lldb_reg_info> &reg_info);
53
54 /// Create an UnwindPlan for a "non-call site" stack frame situation.
55 /// This is usually when this function/method is currently executing, and may
56 /// be at
57 /// a location where exception-handling style unwind information (eh_frame,
58 /// compact unwind info, arm unwind info)
59 /// are not valid.
60 /// \p data is a pointer to the instructions for the function
61 /// \p size is the size of the instruction buffer above
62 /// \p func_range is the start Address and size of the function, to be
63 /// included in the UnwindPlan
64 /// \p unwind_plan is the unwind plan that this method creates
65 /// \returns true if it was able to create an UnwindPlan; false if not.
66 bool
67 GetNonCallSiteUnwindPlanFromAssembly(uint8_t *data, size_t size,
69 lldb_private::UnwindPlan &unwind_plan);
70
71 /// Take an existing UnwindPlan, probably from eh_frame which may be missing
72 /// description
73 /// of the epilogue instructions, and add the epilogue description to it based
74 /// on the
75 /// instructions in the function.
76 ///
77 /// The \p unwind_plan 's register numbers must be converted into the lldb
78 /// register numbering
79 /// scheme OR a RegisterContext must be provided in \p reg_ctx. If the \p
80 /// unwind_plan
81 /// register numbers are already in lldb register numbering, \p reg_ctx may be
82 /// null.
83 /// \returns true if the \p unwind_plan was updated, false if it was not.
84 bool AugmentUnwindPlanFromCallSite(uint8_t *data, size_t size,
86 lldb_private::UnwindPlan &unwind_plan,
88
89 bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size,
90 size_t &offset);
91
92private:
93 bool nonvolatile_reg_p(int machine_regno);
94 bool push_rbp_pattern_p();
95 bool push_0_pattern_p();
96 bool push_imm_pattern_p();
98 bool push_misc_reg_p();
103 bool sub_rsp_pattern_p(int &amount);
104 bool add_rsp_pattern_p(int &amount);
105 bool lea_rsp_pattern_p(int &amount);
106 bool lea_rbp_rsp_pattern_p(int &amount);
107 bool lea_rbx_rsp_pattern_p(int &amount);
108 bool and_rsp_pattern_p();
109 bool push_reg_p(int &regno);
110 bool pop_reg_p(int &regno);
111 bool pop_rbp_pattern_p();
112 bool pop_misc_reg_p();
113 bool leave_pattern_p();
115 bool mov_reg_to_local_stack_frame_p(int &regno, int &rbp_offset);
116 bool ret_pattern_p();
117 bool jmp_to_reg_p();
118 bool pc_rel_branch_or_jump_p (const int instruction_length, int &offset);
119 bool non_local_branch_p (const lldb::addr_t current_func_text_offset,
120 const lldb_private::AddressRange &func_range,
121 const int instruction_length);
122 bool local_branch_p (const lldb::addr_t current_func_text_offset,
123 const lldb_private::AddressRange &func_range,
124 const int instruction_length,
125 lldb::addr_t &target_insn_offset);
126 uint16_t extract_2(uint8_t *b);
127 int16_t extract_2_signed(uint8_t *b);
128 uint32_t extract_4(uint8_t *b);
129 int32_t extract_4_signed(uint8_t *b);
130
131 bool instruction_length(uint8_t *insn, int &length, uint32_t buffer_remaining_bytes);
132
133 bool machine_regno_to_lldb_regno(int machine_regno, uint32_t &lldb_regno);
134
136
146 k_machine_eip = 8
147 };
148
166 k_machine_rip = 16
167 };
168
170
171 uint8_t *m_cur_insn;
172
181
182 typedef std::map<uint32_t, lldb_reg_info> MachineRegnumToNameAndLLDBRegnum;
183
185
189
191
192 ::LLVMDisasmContextRef m_disasm_context;
193
197};
198
199} // namespace lldb_private
200
201#endif // LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H
A section + offset based address range class.
Definition: AddressRange.h:25
An architecture specification class.
Definition: ArchSpec.h:31
bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size, size_t &offset)
bool mov_reg_to_local_stack_frame_p(int &regno, int &rbp_offset)
bool non_local_branch_p(const lldb::addr_t current_func_text_offset, const lldb_private::AddressRange &func_range, const int instruction_length)
std::map< uint32_t, lldb_reg_info > MachineRegnumToNameAndLLDBRegnum
bool AugmentUnwindPlanFromCallSite(uint8_t *data, size_t size, lldb_private::AddressRange &func_range, lldb_private::UnwindPlan &unwind_plan, lldb::RegisterContextSP &reg_ctx)
Take an existing UnwindPlan, probably from eh_frame which may be missing description of the epilogue ...
bool pc_rel_branch_or_jump_p(const int instruction_length, int &offset)
void Initialize(lldb::RegisterContextSP &reg_ctx)
One of the two initialize methods that can be called on this object; they must be called before any o...
bool GetNonCallSiteUnwindPlanFromAssembly(uint8_t *data, size_t size, lldb_private::AddressRange &func_range, lldb_private::UnwindPlan &unwind_plan)
Create an UnwindPlan for a "non-call site" stack frame situation.
bool instruction_length(uint8_t *insn, int &length, uint32_t buffer_remaining_bytes)
x86AssemblyInspectionEngine(const x86AssemblyInspectionEngine &)=delete
const x86AssemblyInspectionEngine & operator=(const x86AssemblyInspectionEngine &)=delete
bool machine_regno_to_lldb_regno(int machine_regno, uint32_t &lldb_regno)
bool local_branch_p(const lldb::addr_t current_func_text_offset, const lldb_private::AddressRange &func_range, const int instruction_length, lldb::addr_t &target_insn_offset)
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:87
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:386
One of the two initialize methods that can be called on this object; they must be called before any o...