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