blob: 9125bd5b1fe3c577c71df7bc1f2d46d3fda1a751 [file] [log] [blame]
Eugene Zelenkoab7f6d02015-10-21 18:46:17 +00001//===-- UnwindAssemblyInstEmulation.h ---------------------------*- C++ -*-===//
Greg Claytonffc922e32011-04-25 21:05:07 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Greg Claytonffc922e32011-04-25 21:05:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_UnwindAssemblyInstEmulation_h_
10#define liblldb_UnwindAssemblyInstEmulation_h_
11
Greg Clayton2ed751b2011-04-26 04:39:08 +000012#include "lldb/Core/EmulateInstruction.h"
Greg Claytone5b34982011-04-29 22:50:31 +000013#include "lldb/Symbol/UnwindPlan.h"
Greg Clayton7be25422011-04-25 21:14:26 +000014#include "lldb/Target/UnwindAssembly.h"
Pavel Labathd821c992018-08-07 11:07:21 +000015#include "lldb/Utility/RegisterValue.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/lldb-private.h"
Greg Claytonffc922e32011-04-25 21:05:07 +000017
Kate Stoneb9c1b512016-09-06 20:57:50 +000018class UnwindAssemblyInstEmulation : public lldb_private::UnwindAssembly {
Greg Claytonffc922e32011-04-25 21:05:07 +000019public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000020 ~UnwindAssemblyInstEmulation() override = default;
Greg Claytonffc922e32011-04-25 21:05:07 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022 bool GetNonCallSiteUnwindPlanFromAssembly(
23 lldb_private::AddressRange &func, lldb_private::Thread &thread,
24 lldb_private::UnwindPlan &unwind_plan) override;
Greg Claytonffc922e32011-04-25 21:05:07 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026 bool
Jason Molenda6853cca2016-10-11 02:24:00 +000027 GetNonCallSiteUnwindPlanFromAssembly(lldb_private::AddressRange &func,
28 uint8_t *opcode_data, size_t opcode_size,
29 lldb_private::UnwindPlan &unwind_plan);
30
31 bool
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 AugmentUnwindPlanFromCallSite(lldb_private::AddressRange &func,
33 lldb_private::Thread &thread,
34 lldb_private::UnwindPlan &unwind_plan) override;
Greg Claytonffc922e32011-04-25 21:05:07 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 bool GetFastUnwindPlan(lldb_private::AddressRange &func,
37 lldb_private::Thread &thread,
38 lldb_private::UnwindPlan &unwind_plan) override;
Greg Claytonffc922e32011-04-25 21:05:07 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 // thread may be NULL in which case we only use the Target (e.g. if this is
41 // called pre-process-launch).
42 bool
43 FirstNonPrologueInsn(lldb_private::AddressRange &func,
44 const lldb_private::ExecutionContext &exe_ctx,
45 lldb_private::Address &first_non_prologue_insn) override;
Greg Claytonffc922e32011-04-25 21:05:07 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 static lldb_private::UnwindAssembly *
48 CreateInstance(const lldb_private::ArchSpec &arch);
Greg Claytonffc922e32011-04-25 21:05:07 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 // PluginInterface protocol
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 static void Initialize();
Greg Claytonffc922e32011-04-25 21:05:07 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 static void Terminate();
Greg Claytonffc922e32011-04-25 21:05:07 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 static lldb_private::ConstString GetPluginNameStatic();
Greg Claytonffc922e32011-04-25 21:05:07 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 static const char *GetPluginDescriptionStatic();
Greg Claytonffc922e32011-04-25 21:05:07 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 lldb_private::ConstString GetPluginName() override;
60
61 uint32_t GetPluginVersion() override;
62
Greg Claytonffc922e32011-04-25 21:05:07 +000063private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 // Call CreateInstance to get an instance of this class
65 UnwindAssemblyInstEmulation(const lldb_private::ArchSpec &arch,
66 lldb_private::EmulateInstruction *inst_emulator)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000067 : UnwindAssembly(arch), m_inst_emulator_up(inst_emulator),
Konrad Kleine248a1302019-05-23 11:14:47 +000068 m_range_ptr(nullptr), m_unwind_plan_ptr(nullptr), m_curr_row(),
Jason Molenda6853cca2016-10-11 02:24:00 +000069 m_cfa_reg_info(), m_fp_is_cfa(false), m_register_values(),
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 m_pushed_regs(), m_curr_row_modified(false),
71 m_forward_branch_offset(0) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000072 if (m_inst_emulator_up.get()) {
73 m_inst_emulator_up->SetBaton(this);
74 m_inst_emulator_up->SetCallbacks(ReadMemory, WriteMemory, ReadRegister,
Kate Stoneb9c1b512016-09-06 20:57:50 +000075 WriteRegister);
Eugene Zelenkoab7f6d02015-10-21 18:46:17 +000076 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 }
Eugene Zelenkoab7f6d02015-10-21 18:46:17 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 static size_t
80 ReadMemory(lldb_private::EmulateInstruction *instruction, void *baton,
81 const lldb_private::EmulateInstruction::Context &context,
82 lldb::addr_t addr, void *dst, size_t length);
Greg Clayton2ed751b2011-04-26 04:39:08 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 static size_t
85 WriteMemory(lldb_private::EmulateInstruction *instruction, void *baton,
86 const lldb_private::EmulateInstruction::Context &context,
87 lldb::addr_t addr, const void *dst, size_t length);
Greg Clayton2ed751b2011-04-26 04:39:08 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 static bool ReadRegister(lldb_private::EmulateInstruction *instruction,
90 void *baton,
91 const lldb_private::RegisterInfo *reg_info,
92 lldb_private::RegisterValue &reg_value);
Greg Clayton31f1d2f2011-05-11 18:39:18 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 static bool
95 WriteRegister(lldb_private::EmulateInstruction *instruction, void *baton,
96 const lldb_private::EmulateInstruction::Context &context,
97 const lldb_private::RegisterInfo *reg_info,
98 const lldb_private::RegisterValue &reg_value);
Greg Clayton31f1d2f2011-05-11 18:39:18 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 // size_t
101 // ReadMemory (lldb_private::EmulateInstruction *instruction,
102 // const lldb_private::EmulateInstruction::Context &context,
103 // lldb::addr_t addr,
104 // void *dst,
105 // size_t length);
Greg Clayton31f1d2f2011-05-11 18:39:18 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 size_t WriteMemory(lldb_private::EmulateInstruction *instruction,
108 const lldb_private::EmulateInstruction::Context &context,
109 lldb::addr_t addr, const void *dst, size_t length);
Greg Clayton2ed751b2011-04-26 04:39:08 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 bool ReadRegister(lldb_private::EmulateInstruction *instruction,
112 const lldb_private::RegisterInfo *reg_info,
113 lldb_private::RegisterValue &reg_value);
Greg Clayton2ed751b2011-04-26 04:39:08 +0000114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115 bool WriteRegister(lldb_private::EmulateInstruction *instruction,
116 const lldb_private::EmulateInstruction::Context &context,
117 const lldb_private::RegisterInfo *reg_info,
118 const lldb_private::RegisterValue &reg_value);
Jason Molenda7cd5e832012-10-23 03:08:31 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 static uint64_t
121 MakeRegisterKindValuePair(const lldb_private::RegisterInfo &reg_info);
Tamas Berghammer44ff9cce2015-06-24 11:27:32 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 void SetRegisterValue(const lldb_private::RegisterInfo &reg_info,
124 const lldb_private::RegisterValue &reg_value);
125
126 bool GetRegisterValue(const lldb_private::RegisterInfo &reg_info,
127 lldb_private::RegisterValue &reg_value);
128
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000129 std::unique_ptr<lldb_private::EmulateInstruction> m_inst_emulator_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 lldb_private::AddressRange *m_range_ptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 lldb_private::UnwindPlan *m_unwind_plan_ptr;
132 lldb_private::UnwindPlan::RowSP m_curr_row;
133 typedef std::map<uint64_t, uint64_t> PushedRegisterToAddrMap;
134 uint64_t m_initial_sp;
135 lldb_private::RegisterInfo m_cfa_reg_info;
136 bool m_fp_is_cfa;
137 typedef std::map<uint64_t, lldb_private::RegisterValue> RegisterValueMap;
138 RegisterValueMap m_register_values;
139 PushedRegisterToAddrMap m_pushed_regs;
140
141 // While processing the instruction stream, we need to communicate some state
142 // change
143 // information up to the higher level loop that makes decisions about how to
144 // push
145 // the unwind instructions for the UnwindPlan we're constructing.
146
147 // The instruction we're processing updated the UnwindPlan::Row contents
148 bool m_curr_row_modified;
149 // The instruction is branching forward with the given offset. 0 value means
150 // no branching.
151 uint32_t m_forward_branch_offset;
Greg Claytonffc922e32011-04-25 21:05:07 +0000152};
153
154#endif // liblldb_UnwindAssemblyInstEmulation_h_