Path: blob/main/contrib/llvm-project/lldb/source/Plugins/UnwindAssembly/x86/x86AssemblyInspectionEngine.h
39654 views
//===-- x86AssemblyInspectionEngine.h ---------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H9#define LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H1011#include "llvm-c/Disassembler.h"1213#include "lldb/Utility/ArchSpec.h"14#include "lldb/Utility/ConstString.h"15#include "lldb/lldb-enumerations.h"16#include "lldb/lldb-forward.h"17#include "lldb/lldb-private.h"1819#include <map>20#include <vector>2122namespace lldb_private {2324// x86AssemblyInspectionEngine - a class which will take a buffer of bytes25// of i386/x86_64 instructions and create an UnwindPlan based on those26// assembly instructions.27class x86AssemblyInspectionEngine {2829public:30/// default ctor31x86AssemblyInspectionEngine(const lldb_private::ArchSpec &arch);3233/// default dtor34~x86AssemblyInspectionEngine();3536/// One of the two initialize methods that can be called on this object;37/// they must be called before any of the assembly inspection methods38/// are called. This one should be used if the caller has access to a39/// valid RegisterContext.40void Initialize(lldb::RegisterContextSP ®_ctx);4142/// One of the two initialize methods that can be called on this object;43/// they must be called before any of the assembly inspection methods44/// are called. This one takes a vector of register name and lldb45/// register numbers.46struct lldb_reg_info {47const char *name = nullptr;48uint32_t lldb_regnum = LLDB_INVALID_REGNUM;49lldb_reg_info() = default;50};51void Initialize(std::vector<lldb_reg_info> ®_info);5253/// Create an UnwindPlan for a "non-call site" stack frame situation.54/// This is usually when this function/method is currently executing, and may55/// be at56/// a location where exception-handling style unwind information (eh_frame,57/// compact unwind info, arm unwind info)58/// are not valid.59/// \p data is a pointer to the instructions for the function60/// \p size is the size of the instruction buffer above61/// \p func_range is the start Address and size of the function, to be62/// included in the UnwindPlan63/// \p unwind_plan is the unwind plan that this method creates64/// \returns true if it was able to create an UnwindPlan; false if not.65bool66GetNonCallSiteUnwindPlanFromAssembly(uint8_t *data, size_t size,67lldb_private::AddressRange &func_range,68lldb_private::UnwindPlan &unwind_plan);6970/// Take an existing UnwindPlan, probably from eh_frame which may be missing71/// description72/// of the epilogue instructions, and add the epilogue description to it based73/// on the74/// instructions in the function.75///76/// The \p unwind_plan 's register numbers must be converted into the lldb77/// register numbering78/// scheme OR a RegisterContext must be provided in \p reg_ctx. If the \p79/// unwind_plan80/// register numbers are already in lldb register numbering, \p reg_ctx may be81/// null.82/// \returns true if the \p unwind_plan was updated, false if it was not.83bool AugmentUnwindPlanFromCallSite(uint8_t *data, size_t size,84lldb_private::AddressRange &func_range,85lldb_private::UnwindPlan &unwind_plan,86lldb::RegisterContextSP ®_ctx);8788bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size,89size_t &offset);9091private:92bool nonvolatile_reg_p(int machine_regno);93bool push_rbp_pattern_p();94bool push_0_pattern_p();95bool push_imm_pattern_p();96bool push_extended_pattern_p();97bool push_misc_reg_p();98bool mov_rsp_rbp_pattern_p();99bool mov_rsp_rbx_pattern_p();100bool mov_rbp_rsp_pattern_p();101bool mov_rbx_rsp_pattern_p();102bool sub_rsp_pattern_p(int &amount);103bool add_rsp_pattern_p(int &amount);104bool lea_rsp_pattern_p(int &amount);105bool lea_rbp_rsp_pattern_p(int &amount);106bool lea_rbx_rsp_pattern_p(int &amount);107bool and_rsp_pattern_p();108bool push_reg_p(int ®no);109bool pop_reg_p(int ®no);110bool pop_rbp_pattern_p();111bool pop_misc_reg_p();112bool leave_pattern_p();113bool call_next_insn_pattern_p();114bool mov_reg_to_local_stack_frame_p(int ®no, int &rbp_offset);115bool ret_pattern_p();116bool jmp_to_reg_p();117bool pc_rel_branch_or_jump_p (const int instruction_length, int &offset);118bool non_local_branch_p (const lldb::addr_t current_func_text_offset,119const lldb_private::AddressRange &func_range,120const int instruction_length);121bool local_branch_p (const lldb::addr_t current_func_text_offset,122const lldb_private::AddressRange &func_range,123const int instruction_length,124lldb::addr_t &target_insn_offset);125uint16_t extract_2(uint8_t *b);126int16_t extract_2_signed(uint8_t *b);127uint32_t extract_4(uint8_t *b);128int32_t extract_4_signed(uint8_t *b);129130bool instruction_length(uint8_t *insn, int &length, uint32_t buffer_remaining_bytes);131132bool machine_regno_to_lldb_regno(int machine_regno, uint32_t &lldb_regno);133134enum CPU { k_i386, k_x86_64, k_cpu_unspecified };135136enum i386_register_numbers {137k_machine_eax = 0,138k_machine_ecx = 1,139k_machine_edx = 2,140k_machine_ebx = 3,141k_machine_esp = 4,142k_machine_ebp = 5,143k_machine_esi = 6,144k_machine_edi = 7,145k_machine_eip = 8146};147148enum x86_64_register_numbers {149k_machine_rax = 0,150k_machine_rcx = 1,151k_machine_rdx = 2,152k_machine_rbx = 3,153k_machine_rsp = 4,154k_machine_rbp = 5,155k_machine_rsi = 6,156k_machine_rdi = 7,157k_machine_r8 = 8,158k_machine_r9 = 9,159k_machine_r10 = 10,160k_machine_r11 = 11,161k_machine_r12 = 12,162k_machine_r13 = 13,163k_machine_r14 = 14,164k_machine_r15 = 15,165k_machine_rip = 16166};167168enum { kMaxInstructionByteSize = 32 };169170uint8_t *m_cur_insn;171172uint32_t m_machine_ip_regnum;173uint32_t m_machine_sp_regnum;174uint32_t m_machine_fp_regnum;175uint32_t m_machine_alt_fp_regnum;176uint32_t m_lldb_ip_regnum;177uint32_t m_lldb_sp_regnum;178uint32_t m_lldb_fp_regnum;179uint32_t m_lldb_alt_fp_regnum;180181typedef std::map<uint32_t, lldb_reg_info> MachineRegnumToNameAndLLDBRegnum;182183MachineRegnumToNameAndLLDBRegnum m_reg_map;184185lldb_private::ArchSpec m_arch;186CPU m_cpu;187int m_wordsize;188189bool m_register_map_initialized;190191::LLVMDisasmContextRef m_disasm_context;192193x86AssemblyInspectionEngine(const x86AssemblyInspectionEngine &) = delete;194const x86AssemblyInspectionEngine &195operator=(const x86AssemblyInspectionEngine &) = delete;196};197198} // namespace lldb_private199200#endif // LLDB_SOURCE_PLUGINS_UNWINDASSEMBLY_X86_X86ASSEMBLYINSPECTIONENGINE_H201202203