1 //===-- x86AssemblyInspectionEngine.h ---------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_x86AssemblyInspectionEngine_h_ 11 #define liblldb_x86AssemblyInspectionEngine_h_ 12 13 #include "llvm-c/Disassembler.h" 14 15 #include "lldb/Utility/ArchSpec.h" 16 #include "lldb/Utility/ConstString.h" 17 #include "lldb/lldb-enumerations.h" 18 #include "lldb/lldb-forward.h" 19 #include "lldb/lldb-private.h" 20 21 #include <map> 22 #include <vector> 23 24 namespace lldb_private { 25 26 // x86AssemblyInspectionEngine - a class which will take a buffer of bytes 27 // of i386/x86_64 instructions and create an UnwindPlan based on those 28 // assembly instructions. 29 class x86AssemblyInspectionEngine { 30 31 public: 32 /// default ctor 33 x86AssemblyInspectionEngine(const lldb_private::ArchSpec &arch); 34 35 /// default dtor 36 ~x86AssemblyInspectionEngine(); 37 38 /// One of the two initialize methods that can be called on this object; 39 /// they must be called before any of the assembly inspection methods 40 /// are called. This one should be used if the caller has access to a 41 /// valid RegisterContext. 42 void Initialize(lldb::RegisterContextSP ®_ctx); 43 44 /// One of the two initialize methods that can be called on this object; 45 /// they must be called before any of the assembly inspection methods 46 /// are called. This one takes a vector of register name and lldb 47 /// register numbers. 48 struct lldb_reg_info { 49 const char *name; 50 uint32_t lldb_regnum; lldb_reg_infolldb_reg_info51 lldb_reg_info() : name(nullptr), lldb_regnum(LLDB_INVALID_REGNUM) {} 52 }; 53 void Initialize(std::vector<lldb_reg_info> ®_info); 54 55 /// Create an UnwindPlan for a "non-call site" stack frame situation. 56 /// This is usually when this function/method is currently executing, and may 57 /// be at 58 /// a location where exception-handling style unwind information (eh_frame, 59 /// compact unwind info, arm unwind info) 60 /// are not valid. 61 /// \p data is a pointer to the instructions for the function 62 /// \p size is the size of the instruction buffer above 63 /// \p func_range is the start Address and size of the function, to be 64 /// included in the UnwindPlan 65 /// \p unwind_plan is the unwind plan that this method creates 66 /// \returns true if it was able to create an UnwindPlan; false if not. 67 bool 68 GetNonCallSiteUnwindPlanFromAssembly(uint8_t *data, size_t size, 69 lldb_private::AddressRange &func_range, 70 lldb_private::UnwindPlan &unwind_plan); 71 72 /// Take an existing UnwindPlan, probably from eh_frame which may be missing 73 /// description 74 /// of the epilogue instructions, and add the epilogue description to it based 75 /// on the 76 /// instructions in the function. 77 /// 78 /// The \p unwind_plan 's register numbers must be converted into the lldb 79 /// register numbering 80 /// scheme OR a RegisterContext must be provided in \p reg_ctx. If the \p 81 /// unwind_plan 82 /// register numbers are already in lldb register numbering, \p reg_ctx may be 83 /// null. 84 /// \returns true if the \p unwind_plan was updated, false if it was not. 85 bool AugmentUnwindPlanFromCallSite(uint8_t *data, size_t size, 86 lldb_private::AddressRange &func_range, 87 lldb_private::UnwindPlan &unwind_plan, 88 lldb::RegisterContextSP ®_ctx); 89 90 bool FindFirstNonPrologueInstruction(uint8_t *data, size_t size, 91 size_t &offset); 92 93 private: 94 bool nonvolatile_reg_p(int machine_regno); 95 bool push_rbp_pattern_p(); 96 bool push_0_pattern_p(); 97 bool push_imm_pattern_p(); 98 bool push_extended_pattern_p(); 99 bool push_misc_reg_p(); 100 bool mov_rsp_rbp_pattern_p(); 101 bool mov_rsp_rbx_pattern_p(); 102 bool mov_rbp_rsp_pattern_p(); 103 bool mov_rbx_rsp_pattern_p(); 104 bool sub_rsp_pattern_p(int &amount); 105 bool add_rsp_pattern_p(int &amount); 106 bool lea_rsp_pattern_p(int &amount); 107 bool lea_rbp_rsp_pattern_p(int &amount); 108 bool lea_rbx_rsp_pattern_p(int &amount); 109 bool and_rsp_pattern_p(); 110 bool push_reg_p(int ®no); 111 bool pop_reg_p(int ®no); 112 bool pop_rbp_pattern_p(); 113 bool pop_misc_reg_p(); 114 bool leave_pattern_p(); 115 bool call_next_insn_pattern_p(); 116 bool mov_reg_to_local_stack_frame_p(int ®no, int &rbp_offset); 117 bool ret_pattern_p(); 118 uint32_t extract_4(uint8_t *b); 119 120 bool instruction_length(uint8_t *insn, int &length, uint32_t buffer_remaining_bytes); 121 122 bool machine_regno_to_lldb_regno(int machine_regno, uint32_t &lldb_regno); 123 124 enum CPU { k_i386, k_x86_64, k_cpu_unspecified }; 125 126 enum i386_register_numbers { 127 k_machine_eax = 0, 128 k_machine_ecx = 1, 129 k_machine_edx = 2, 130 k_machine_ebx = 3, 131 k_machine_esp = 4, 132 k_machine_ebp = 5, 133 k_machine_esi = 6, 134 k_machine_edi = 7, 135 k_machine_eip = 8 136 }; 137 138 enum x86_64_register_numbers { 139 k_machine_rax = 0, 140 k_machine_rcx = 1, 141 k_machine_rdx = 2, 142 k_machine_rbx = 3, 143 k_machine_rsp = 4, 144 k_machine_rbp = 5, 145 k_machine_rsi = 6, 146 k_machine_rdi = 7, 147 k_machine_r8 = 8, 148 k_machine_r9 = 9, 149 k_machine_r10 = 10, 150 k_machine_r11 = 11, 151 k_machine_r12 = 12, 152 k_machine_r13 = 13, 153 k_machine_r14 = 14, 154 k_machine_r15 = 15, 155 k_machine_rip = 16 156 }; 157 158 enum { kMaxInstructionByteSize = 32 }; 159 160 uint8_t *m_cur_insn; 161 162 uint32_t m_machine_ip_regnum; 163 uint32_t m_machine_sp_regnum; 164 uint32_t m_machine_fp_regnum; 165 uint32_t m_machine_alt_fp_regnum; 166 uint32_t m_lldb_ip_regnum; 167 uint32_t m_lldb_sp_regnum; 168 uint32_t m_lldb_fp_regnum; 169 uint32_t m_lldb_alt_fp_regnum; 170 171 typedef std::map<uint32_t, lldb_reg_info> MachineRegnumToNameAndLLDBRegnum; 172 173 MachineRegnumToNameAndLLDBRegnum m_reg_map; 174 175 lldb_private::ArchSpec m_arch; 176 CPU m_cpu; 177 int m_wordsize; 178 179 bool m_register_map_initialized; 180 181 ::LLVMDisasmContextRef m_disasm_context; 182 183 DISALLOW_COPY_AND_ASSIGN(x86AssemblyInspectionEngine); 184 }; 185 186 } // namespace lldb_private 187 188 #endif // liblldb_x86AssemblyInspectionEngine_h_ 189