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 
14 #include "lldb/Utility/ArchSpec.h"
16 #include "lldb/lldb-enumerations.h"
17 #include "lldb/lldb-forward.h"
18 #include "lldb/lldb-private.h"
19 
20 #include <map>
21 #include <vector>
22 
23 namespace 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 
30 public:
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.
41  void Initialize(lldb::RegisterContextSP &reg_ctx);
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.
47  struct lldb_reg_info {
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,
68  lldb_private::AddressRange &func_range,
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,
85  lldb_private::AddressRange &func_range,
86  lldb_private::UnwindPlan &unwind_plan,
87  lldb::RegisterContextSP &reg_ctx);
88 
89  bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size,
90  size_t &offset);
91 
92 private:
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();
99  bool mov_rsp_rbp_pattern_p();
100  bool mov_rsp_rbx_pattern_p();
101  bool mov_rbp_rsp_pattern_p();
102  bool mov_rbx_rsp_pattern_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 
147  };
148 
167  };
168 
169  enum { kMaxInstructionByteSize = 32 };
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 
196  operator=(const x86AssemblyInspectionEngine &) = delete;
197 };
198 
199 } // namespace lldb_private
200 
201 #endif // LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H
lldb_private::x86AssemblyInspectionEngine::jmp_to_reg_p
bool jmp_to_reg_p()
Definition: x86AssemblyInspectionEngine.cpp:677
lldb_private::x86AssemblyInspectionEngine::x86_64_register_numbers
x86_64_register_numbers
Definition: x86AssemblyInspectionEngine.h:149
lldb_private::x86AssemblyInspectionEngine::k_machine_ebp
@ k_machine_ebp
Definition: x86AssemblyInspectionEngine.h:143
lldb_private::ArchSpec
Definition: ArchSpec.h:33
LLDB_INVALID_REGNUM
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:91
lldb_private::x86AssemblyInspectionEngine::i386_register_numbers
i386_register_numbers
Definition: x86AssemblyInspectionEngine.h:137
lldb_private::x86AssemblyInspectionEngine::mov_rsp_rbx_pattern_p
bool mov_rsp_rbx_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:395
lldb_private::x86AssemblyInspectionEngine::k_machine_r11
@ k_machine_r11
Definition: x86AssemblyInspectionEngine.h:161
lldb_private::x86AssemblyInspectionEngine::m_lldb_sp_regnum
uint32_t m_lldb_sp_regnum
Definition: x86AssemblyInspectionEngine.h:178
lldb_private::x86AssemblyInspectionEngine::k_machine_r13
@ k_machine_r13
Definition: x86AssemblyInspectionEngine.h:163
lldb_private::x86AssemblyInspectionEngine::lldb_reg_info::lldb_regnum
uint32_t lldb_regnum
Definition: x86AssemblyInspectionEngine.h:49
lldb_private::x86AssemblyInspectionEngine::pop_misc_reg_p
bool pop_misc_reg_p()
Definition: x86AssemblyInspectionEngine.cpp:589
lldb_private::x86AssemblyInspectionEngine::and_rsp_pattern_p
bool and_rsp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:550
lldb_private::x86AssemblyInspectionEngine
Definition: x86AssemblyInspectionEngine.h:28
lldb_private::x86AssemblyInspectionEngine::ret_pattern_p
bool ret_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:854
lldb_private::x86AssemblyInspectionEngine::extract_4
uint32_t extract_4(uint8_t *b)
Definition: x86AssemblyInspectionEngine.cpp:873
lldb_private::x86AssemblyInspectionEngine::add_rsp_pattern_p
bool add_rsp_pattern_p(int &amount)
Definition: x86AssemblyInspectionEngine.cpp:451
lldb_private::x86AssemblyInspectionEngine::call_next_insn_pattern_p
bool call_next_insn_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:605
lldb_private::x86AssemblyInspectionEngine::m_machine_sp_regnum
uint32_t m_machine_sp_regnum
Definition: x86AssemblyInspectionEngine.h:174
lldb_private::x86AssemblyInspectionEngine::FindFirstNonPrologueInstruction
bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size, size_t &offset)
Definition: x86AssemblyInspectionEngine.cpp:1580
lldb_private::x86AssemblyInspectionEngine::k_machine_eip
@ k_machine_eip
Definition: x86AssemblyInspectionEngine.h:146
lldb_private::x86AssemblyInspectionEngine::non_local_branch_p
bool non_local_branch_p(const lldb::addr_t current_func_text_offset, const lldb_private::AddressRange &func_range, const int instruction_length)
Definition: x86AssemblyInspectionEngine.cpp:841
lldb_private::x86AssemblyInspectionEngine::lldb_reg_info::lldb_reg_info
lldb_reg_info()=default
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::x86AssemblyInspectionEngine::Initialize
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...
lldb_private::x86AssemblyInspectionEngine::m_lldb_ip_regnum
uint32_t m_lldb_ip_regnum
Definition: x86AssemblyInspectionEngine.h:177
lldb_private::x86AssemblyInspectionEngine::k_machine_rsp
@ k_machine_rsp
Definition: x86AssemblyInspectionEngine.h:154
lldb_private::x86AssemblyInspectionEngine::k_machine_edi
@ k_machine_edi
Definition: x86AssemblyInspectionEngine.h:145
lldb_private::x86AssemblyInspectionEngine::m_wordsize
int m_wordsize
Definition: x86AssemblyInspectionEngine.h:188
lldb_private::x86AssemblyInspectionEngine::extract_4_signed
int32_t extract_4_signed(uint8_t *b)
Definition: x86AssemblyInspectionEngine.cpp:880
lldb_private::x86AssemblyInspectionEngine::pc_rel_branch_or_jump_p
bool pc_rel_branch_or_jump_p(const int instruction_length, int &offset)
Definition: x86AssemblyInspectionEngine.cpp:733
lldb_private::x86AssemblyInspectionEngine::m_cur_insn
uint8_t * m_cur_insn
Definition: x86AssemblyInspectionEngine.h:171
lldb_private::x86AssemblyInspectionEngine::k_machine_rsi
@ k_machine_rsi
Definition: x86AssemblyInspectionEngine.h:156
lldb_private::x86AssemblyInspectionEngine::CPU
CPU
Definition: x86AssemblyInspectionEngine.h:135
lldb_private::x86AssemblyInspectionEngine::k_machine_rax
@ k_machine_rax
Definition: x86AssemblyInspectionEngine.h:150
lldb_private::x86AssemblyInspectionEngine::k_cpu_unspecified
@ k_cpu_unspecified
Definition: x86AssemblyInspectionEngine.h:135
lldb_private::x86AssemblyInspectionEngine::lea_rbx_rsp_pattern_p
bool lea_rbx_rsp_pattern_p(int &amount)
Definition: x86AssemblyInspectionEngine.cpp:523
lldb_private::x86AssemblyInspectionEngine::push_misc_reg_p
bool push_misc_reg_p()
Definition: x86AssemblyInspectionEngine.cpp:354
lldb_private::x86AssemblyInspectionEngine::kMaxInstructionByteSize
@ kMaxInstructionByteSize
Definition: x86AssemblyInspectionEngine.h:169
lldb_private::x86AssemblyInspectionEngine::m_machine_alt_fp_regnum
uint32_t m_machine_alt_fp_regnum
Definition: x86AssemblyInspectionEngine.h:176
lldb_private::x86AssemblyInspectionEngine::extract_2
uint16_t extract_2(uint8_t *b)
Definition: x86AssemblyInspectionEngine.cpp:859
lldb_private::x86AssemblyInspectionEngine::AugmentUnwindPlanFromCallSite
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 ...
Definition: x86AssemblyInspectionEngine.cpp:1336
lldb_private::x86AssemblyInspectionEngine::push_rbp_pattern_p
bool push_rbp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:306
lldb_private::x86AssemblyInspectionEngine::m_reg_map
MachineRegnumToNameAndLLDBRegnum m_reg_map
Definition: x86AssemblyInspectionEngine.h:184
lldb-enumerations.h
lldb_private::x86AssemblyInspectionEngine::k_machine_rdi
@ k_machine_rdi
Definition: x86AssemblyInspectionEngine.h:157
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb_private::x86AssemblyInspectionEngine::push_imm_pattern_p
bool push_imm_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:319
lldb_private::x86AssemblyInspectionEngine::lldb_reg_info
One of the two initialize methods that can be called on this object; they must be called before any o...
Definition: x86AssemblyInspectionEngine.h:47
lldb_private::x86AssemblyInspectionEngine::extract_2_signed
int16_t extract_2_signed(uint8_t *b)
Definition: x86AssemblyInspectionEngine.cpp:866
lldb_private::x86AssemblyInspectionEngine::k_machine_edx
@ k_machine_edx
Definition: x86AssemblyInspectionEngine.h:140
lldb_private::x86AssemblyInspectionEngine::local_branch_p
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)
Definition: x86AssemblyInspectionEngine.cpp:814
lldb_private::x86AssemblyInspectionEngine::mov_rbp_rsp_pattern_p
bool mov_rbp_rsp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:408
lldb_private::x86AssemblyInspectionEngine::m_register_map_initialized
bool m_register_map_initialized
Definition: x86AssemblyInspectionEngine.h:190
lldb_private::x86AssemblyInspectionEngine::k_machine_rbp
@ k_machine_rbp
Definition: x86AssemblyInspectionEngine.h:155
lldb_private::x86AssemblyInspectionEngine::k_machine_ecx
@ k_machine_ecx
Definition: x86AssemblyInspectionEngine.h:139
lldb_private::x86AssemblyInspectionEngine::machine_regno_to_lldb_regno
bool machine_regno_to_lldb_regno(int machine_regno, uint32_t &lldb_regno)
Definition: x86AssemblyInspectionEngine.cpp:905
lldb_private::x86AssemblyInspectionEngine::x86AssemblyInspectionEngine
x86AssemblyInspectionEngine(const lldb_private::ArchSpec &arch)
default ctor
Definition: x86AssemblyInspectionEngine.cpp:23
lldb-private.h
lldb_private::x86AssemblyInspectionEngine::lea_rbp_rsp_pattern_p
bool lea_rbp_rsp_pattern_p(int &amount)
Definition: x86AssemblyInspectionEngine.cpp:496
lldb_private::x86AssemblyInspectionEngine::k_machine_r14
@ k_machine_r14
Definition: x86AssemblyInspectionEngine.h:164
lldb_private::x86AssemblyInspectionEngine::push_reg_p
bool push_reg_p(int &regno)
Definition: x86AssemblyInspectionEngine.cpp:365
lldb_private::x86AssemblyInspectionEngine::sub_rsp_pattern_p
bool sub_rsp_pattern_p(int &amount)
Definition: x86AssemblyInspectionEngine.cpp:433
uint32_t
lldb_private::x86AssemblyInspectionEngine::m_cpu
CPU m_cpu
Definition: x86AssemblyInspectionEngine.h:187
lldb_private::x86AssemblyInspectionEngine::push_extended_pattern_p
bool push_extended_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:335
lldb_private::x86AssemblyInspectionEngine::k_machine_r15
@ k_machine_r15
Definition: x86AssemblyInspectionEngine.h:165
lldb_private::x86AssemblyInspectionEngine::m_lldb_alt_fp_regnum
uint32_t m_lldb_alt_fp_regnum
Definition: x86AssemblyInspectionEngine.h:180
lldb_private::x86AssemblyInspectionEngine::operator=
const x86AssemblyInspectionEngine & operator=(const x86AssemblyInspectionEngine &)=delete
ArchSpec.h
lldb_private::x86AssemblyInspectionEngine::mov_rbx_rsp_pattern_p
bool mov_rbx_rsp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:421
lldb_private::x86AssemblyInspectionEngine::k_machine_r8
@ k_machine_r8
Definition: x86AssemblyInspectionEngine.h:158
uint16_t
lldb_private::x86AssemblyInspectionEngine::k_machine_r9
@ k_machine_r9
Definition: x86AssemblyInspectionEngine.h:159
lldb_private::x86AssemblyInspectionEngine::k_machine_rbx
@ k_machine_rbx
Definition: x86AssemblyInspectionEngine.h:153
lldb_private::x86AssemblyInspectionEngine::instruction_length
bool instruction_length(uint8_t *insn, int &length, uint32_t buffer_remaining_bytes)
Definition: x86AssemblyInspectionEngine.cpp:888
lldb_private::x86AssemblyInspectionEngine::k_machine_r10
@ k_machine_r10
Definition: x86AssemblyInspectionEngine.h:160
lldb-forward.h
lldb_private::x86AssemblyInspectionEngine::k_machine_rdx
@ k_machine_rdx
Definition: x86AssemblyInspectionEngine.h:152
lldb_private::x86AssemblyInspectionEngine::nonvolatile_reg_p
bool nonvolatile_reg_p(int machine_regno)
Definition: x86AssemblyInspectionEngine.cpp:262
lldb_private::x86AssemblyInspectionEngine::leave_pattern_p
bool leave_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:599
lldb_private::x86AssemblyInspectionEngine::push_0_pattern_p
bool push_0_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:312
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::x86AssemblyInspectionEngine::k_x86_64
@ k_x86_64
Definition: x86AssemblyInspectionEngine.h:135
lldb_private::x86AssemblyInspectionEngine::mov_rsp_rbp_pattern_p
bool mov_rsp_rbp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:382
lldb_private::x86AssemblyInspectionEngine::k_machine_eax
@ k_machine_eax
Definition: x86AssemblyInspectionEngine.h:138
lldb_private::x86AssemblyInspectionEngine::m_machine_ip_regnum
uint32_t m_machine_ip_regnum
Definition: x86AssemblyInspectionEngine.h:173
ConstString.h
lldb_private::x86AssemblyInspectionEngine::~x86AssemblyInspectionEngine
~x86AssemblyInspectionEngine()
default dtor
Definition: x86AssemblyInspectionEngine.cpp:38
lldb_private::x86AssemblyInspectionEngine::k_machine_r12
@ k_machine_r12
Definition: x86AssemblyInspectionEngine.h:162
lldb_private::x86AssemblyInspectionEngine::mov_reg_to_local_stack_frame_p
bool mov_reg_to_local_stack_frame_p(int &regno, int &rbp_offset)
Definition: x86AssemblyInspectionEngine.cpp:622
lldb_private::x86AssemblyInspectionEngine::k_i386
@ k_i386
Definition: x86AssemblyInspectionEngine.h:135
lldb_private::x86AssemblyInspectionEngine::k_machine_esi
@ k_machine_esi
Definition: x86AssemblyInspectionEngine.h:144
lldb_private::x86AssemblyInspectionEngine::m_lldb_fp_regnum
uint32_t m_lldb_fp_regnum
Definition: x86AssemblyInspectionEngine.h:179
lldb_private::x86AssemblyInspectionEngine::lea_rsp_pattern_p
bool lea_rsp_pattern_p(int &amount)
Definition: x86AssemblyInspectionEngine.cpp:470
lldb_private::x86AssemblyInspectionEngine::k_machine_rcx
@ k_machine_rcx
Definition: x86AssemblyInspectionEngine.h:151
lldb_private::UnwindPlan
Definition: UnwindPlan.h:53
lldb_private::x86AssemblyInspectionEngine::m_disasm_context
::LLVMDisasmContextRef m_disasm_context
Definition: x86AssemblyInspectionEngine.h:192
lldb_private::x86AssemblyInspectionEngine::MachineRegnumToNameAndLLDBRegnum
std::map< uint32_t, lldb_reg_info > MachineRegnumToNameAndLLDBRegnum
Definition: x86AssemblyInspectionEngine.h:182
lldb_private::x86AssemblyInspectionEngine::m_arch
lldb_private::ArchSpec m_arch
Definition: x86AssemblyInspectionEngine.h:186
lldb_private::x86AssemblyInspectionEngine::lldb_reg_info::name
const char * name
Definition: x86AssemblyInspectionEngine.h:48
lldb_private::x86AssemblyInspectionEngine::pop_rbp_pattern_p
bool pop_rbp_pattern_p()
Definition: x86AssemblyInspectionEngine.cpp:580
lldb_private::x86AssemblyInspectionEngine::GetNonCallSiteUnwindPlanFromAssembly
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.
Definition: x86AssemblyInspectionEngine.cpp:915
lldb_private::x86AssemblyInspectionEngine::m_machine_fp_regnum
uint32_t m_machine_fp_regnum
Definition: x86AssemblyInspectionEngine.h:175
lldb_private::x86AssemblyInspectionEngine::k_machine_rip
@ k_machine_rip
Definition: x86AssemblyInspectionEngine.h:166
lldb_private::x86AssemblyInspectionEngine::pop_reg_p
bool pop_reg_p(int &regno)
Definition: x86AssemblyInspectionEngine.cpp:563
lldb_private::x86AssemblyInspectionEngine::k_machine_ebx
@ k_machine_ebx
Definition: x86AssemblyInspectionEngine.h:141
lldb_private::x86AssemblyInspectionEngine::k_machine_esp
@ k_machine_esp
Definition: x86AssemblyInspectionEngine.h:142