1 //===-- UnwindAssemblyInstEmulation.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_UnwindAssemblyInstEmulation_h_
11 #define liblldb_UnwindAssemblyInstEmulation_h_
12 
13 // C Includes
14 // C++ Includes
15 // Other libraries and framework includes
16 // Project includes
17 #include "lldb/lldb-private.h"
18 #include "lldb/Core/EmulateInstruction.h"
19 #include "lldb/Core/RegisterValue.h"
20 #include "lldb/Symbol/UnwindPlan.h"
21 #include "lldb/Target/UnwindAssembly.h"
22 
23 class UnwindAssemblyInstEmulation : public lldb_private::UnwindAssembly
24 {
25 public:
26     ~UnwindAssemblyInstEmulation() override = default;
27 
28     bool
29     GetNonCallSiteUnwindPlanFromAssembly(lldb_private::AddressRange& func,
30                                          lldb_private::Thread& thread,
31                                          lldb_private::UnwindPlan& unwind_plan) override;
32 
33     bool
34     AugmentUnwindPlanFromCallSite(lldb_private::AddressRange& func,
35                                   lldb_private::Thread& thread,
36                                   lldb_private::UnwindPlan& unwind_plan) override;
37 
38     bool
39     GetFastUnwindPlan(lldb_private::AddressRange& func,
40                       lldb_private::Thread& thread,
41                       lldb_private::UnwindPlan &unwind_plan) override;
42 
43     // thread may be NULL in which case we only use the Target (e.g. if this is called pre-process-launch).
44     bool
45     FirstNonPrologueInsn(lldb_private::AddressRange& func,
46                          const lldb_private::ExecutionContext &exe_ctx,
47                          lldb_private::Address& first_non_prologue_insn) override;
48 
49     static lldb_private::UnwindAssembly *
50     CreateInstance (const lldb_private::ArchSpec &arch);
51 
52     //------------------------------------------------------------------
53     // PluginInterface protocol
54     //------------------------------------------------------------------
55     static void
56     Initialize();
57 
58     static void
59     Terminate();
60 
61     static lldb_private::ConstString
62     GetPluginNameStatic();
63 
64     static const char *
65     GetPluginDescriptionStatic();
66 
67     lldb_private::ConstString
68     GetPluginName() override;
69 
70     uint32_t
71     GetPluginVersion() override;
72 
73 private:
74     // Call CreateInstance to get an instance of this class
75     UnwindAssemblyInstEmulation(const lldb_private::ArchSpec &arch,
76                                 lldb_private::EmulateInstruction *inst_emulator) :
77         UnwindAssembly (arch),
78         m_inst_emulator_ap (inst_emulator),
79         m_range_ptr (NULL),
80         m_thread_ptr (NULL),
81         m_unwind_plan_ptr (NULL),
82         m_curr_row (),
83         m_cfa_reg_info (),
84         m_fp_is_cfa (false),
85         m_register_values (),
86         m_pushed_regs(),
87         m_curr_row_modified (false),
88         m_forward_branch_offset (0)
89     {
90         if (m_inst_emulator_ap.get())
91         {
92             m_inst_emulator_ap->SetBaton (this);
93             m_inst_emulator_ap->SetCallbacks (ReadMemory, WriteMemory, ReadRegister, WriteRegister);
94         }
95     }
96 
97     static size_t
98     ReadMemory (lldb_private::EmulateInstruction *instruction,
99                 void *baton,
100                 const lldb_private::EmulateInstruction::Context &context,
101                 lldb::addr_t addr,
102                 void *dst,
103                 size_t length);
104 
105     static size_t
106     WriteMemory (lldb_private::EmulateInstruction *instruction,
107                  void *baton,
108                  const lldb_private::EmulateInstruction::Context &context,
109                  lldb::addr_t addr,
110                  const void *dst,
111                  size_t length);
112 
113     static bool
114     ReadRegister (lldb_private::EmulateInstruction *instruction,
115                   void *baton,
116                   const lldb_private::RegisterInfo *reg_info,
117                   lldb_private::RegisterValue &reg_value);
118 
119     static bool
120     WriteRegister (lldb_private::EmulateInstruction *instruction,
121                    void *baton,
122                    const lldb_private::EmulateInstruction::Context &context,
123                    const lldb_private::RegisterInfo *reg_info,
124                    const lldb_private::RegisterValue &reg_value);
125 
126 //    size_t
127 //    ReadMemory (lldb_private::EmulateInstruction *instruction,
128 //                const lldb_private::EmulateInstruction::Context &context,
129 //                lldb::addr_t addr,
130 //                void *dst,
131 //                size_t length);
132 
133     size_t
134     WriteMemory (lldb_private::EmulateInstruction *instruction,
135                  const lldb_private::EmulateInstruction::Context &context,
136                  lldb::addr_t addr,
137                  const void *dst,
138                  size_t length);
139 
140     bool
141     ReadRegister (lldb_private::EmulateInstruction *instruction,
142                   const lldb_private::RegisterInfo *reg_info,
143                   lldb_private::RegisterValue &reg_value);
144 
145     bool
146     WriteRegister (lldb_private::EmulateInstruction *instruction,
147                    const lldb_private::EmulateInstruction::Context &context,
148                    const lldb_private::RegisterInfo *reg_info,
149                    const lldb_private::RegisterValue &reg_value);
150 
151     static uint64_t
152     MakeRegisterKindValuePair (const lldb_private::RegisterInfo &reg_info);
153 
154     void
155     SetRegisterValue (const lldb_private::RegisterInfo &reg_info,
156                       const lldb_private::RegisterValue &reg_value);
157 
158     bool
159     GetRegisterValue (const lldb_private::RegisterInfo &reg_info,
160                       lldb_private::RegisterValue &reg_value);
161 
162     std::unique_ptr<lldb_private::EmulateInstruction> m_inst_emulator_ap;
163     lldb_private::AddressRange* m_range_ptr;
164     lldb_private::Thread* m_thread_ptr;
165     lldb_private::UnwindPlan* m_unwind_plan_ptr;
166     lldb_private::UnwindPlan::RowSP m_curr_row;
167     typedef std::map<uint64_t, uint64_t> PushedRegisterToAddrMap;
168     uint64_t m_initial_sp;
169     lldb_private::RegisterInfo m_cfa_reg_info;
170     bool m_fp_is_cfa;
171     typedef std::map<uint64_t, lldb_private::RegisterValue> RegisterValueMap;
172     RegisterValueMap m_register_values;
173     PushedRegisterToAddrMap m_pushed_regs;
174 
175     // While processing the instruction stream, we need to communicate some state change
176     // information up to the higher level loop that makes decisions about how to push
177     // the unwind instructions for the UnwindPlan we're constructing.
178 
179     // The instruction we're processing updated the UnwindPlan::Row contents
180     bool m_curr_row_modified;
181     // The instruction is branching forward with the given offset. 0 value means no branching.
182     uint32_t m_forward_branch_offset;
183 };
184 
185 #endif // liblldb_UnwindAssemblyInstEmulation_h_
186