1 //===-- FuncUnwinders.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/FuncUnwinders.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Symbol/ArmUnwindInfo.h"
14 #include "lldb/Symbol/CompactUnwindInfo.h"
15 #include "lldb/Symbol/DWARFCallFrameInfo.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/UnwindPlan.h"
18 #include "lldb/Symbol/UnwindTable.h"
19 #include "lldb/Target/ABI.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterNumber.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Target/UnwindAssembly.h"
28 using namespace lldb_private;
30 //------------------------------------------------
32 //------------------------------------------------
34 FuncUnwinders::FuncUnwinders(UnwindTable &unwind_table, AddressRange range)
35 : m_unwind_table(unwind_table), m_range(range), m_mutex(),
36 m_unwind_plan_assembly_sp(), m_unwind_plan_eh_frame_sp(),
37 m_unwind_plan_eh_frame_augmented_sp(), m_unwind_plan_compact_unwind(),
38 m_unwind_plan_arm_unwind_sp(), m_unwind_plan_fast_sp(),
39 m_unwind_plan_arch_default_sp(),
40 m_unwind_plan_arch_default_at_func_entry_sp(),
41 m_tried_unwind_plan_assembly(false), m_tried_unwind_plan_eh_frame(false),
42 m_tried_unwind_plan_debug_frame(false),
43 m_tried_unwind_plan_eh_frame_augmented(false),
44 m_tried_unwind_plan_debug_frame_augmented(false),
45 m_tried_unwind_plan_compact_unwind(false),
46 m_tried_unwind_plan_arm_unwind(false), m_tried_unwind_fast(false),
47 m_tried_unwind_arch_default(false),
48 m_tried_unwind_arch_default_at_func_entry(false),
49 m_first_non_prologue_insn() {}
51 //------------------------------------------------
53 //------------------------------------------------
55 FuncUnwinders::~FuncUnwinders() {}
57 UnwindPlanSP FuncUnwinders::GetUnwindPlanAtCallSite(Target &target,
59 std::lock_guard<std::recursive_mutex> guard(m_mutex);
61 if (UnwindPlanSP plan_sp = GetEHFrameUnwindPlan(target, current_offset))
63 if (UnwindPlanSP plan_sp = GetDebugFrameUnwindPlan(target, current_offset))
65 if (UnwindPlanSP plan_sp = GetCompactUnwindUnwindPlan(target, current_offset))
67 if (UnwindPlanSP plan_sp = GetArmUnwindUnwindPlan(target, current_offset))
73 UnwindPlanSP FuncUnwinders::GetCompactUnwindUnwindPlan(Target &target,
75 std::lock_guard<std::recursive_mutex> guard(m_mutex);
76 if (m_unwind_plan_compact_unwind.size() > 0)
77 return m_unwind_plan_compact_unwind[0]; // FIXME support multiple compact
78 // unwind plans for one func
79 if (m_tried_unwind_plan_compact_unwind)
80 return UnwindPlanSP();
82 m_tried_unwind_plan_compact_unwind = true;
83 if (m_range.GetBaseAddress().IsValid()) {
84 Address current_pc(m_range.GetBaseAddress());
85 if (current_offset != -1)
86 current_pc.SetOffset(current_pc.GetOffset() + current_offset);
87 CompactUnwindInfo *compact_unwind = m_unwind_table.GetCompactUnwindInfo();
89 UnwindPlanSP unwind_plan_sp(new UnwindPlan(lldb::eRegisterKindGeneric));
90 if (compact_unwind->GetUnwindPlan(target, current_pc, *unwind_plan_sp)) {
91 m_unwind_plan_compact_unwind.push_back(unwind_plan_sp);
92 return m_unwind_plan_compact_unwind[0]; // FIXME support multiple
93 // compact unwind plans for one
98 return UnwindPlanSP();
101 UnwindPlanSP FuncUnwinders::GetEHFrameUnwindPlan(Target &target,
102 int current_offset) {
103 std::lock_guard<std::recursive_mutex> guard(m_mutex);
104 if (m_unwind_plan_eh_frame_sp.get() || m_tried_unwind_plan_eh_frame)
105 return m_unwind_plan_eh_frame_sp;
107 m_tried_unwind_plan_eh_frame = true;
108 if (m_range.GetBaseAddress().IsValid()) {
109 Address current_pc(m_range.GetBaseAddress());
110 if (current_offset != -1)
111 current_pc.SetOffset(current_pc.GetOffset() + current_offset);
112 DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo();
114 m_unwind_plan_eh_frame_sp.reset(
115 new UnwindPlan(lldb::eRegisterKindGeneric));
116 if (!eh_frame->GetUnwindPlan(current_pc, *m_unwind_plan_eh_frame_sp))
117 m_unwind_plan_eh_frame_sp.reset();
120 return m_unwind_plan_eh_frame_sp;
123 UnwindPlanSP FuncUnwinders::GetDebugFrameUnwindPlan(Target &target,
124 int current_offset) {
125 std::lock_guard<std::recursive_mutex> guard(m_mutex);
126 if (m_unwind_plan_debug_frame_sp || m_tried_unwind_plan_debug_frame)
127 return m_unwind_plan_debug_frame_sp;
129 m_tried_unwind_plan_debug_frame = true;
130 if (m_range.GetBaseAddress().IsValid()) {
131 Address current_pc(m_range.GetBaseAddress());
132 if (current_offset != -1)
133 current_pc.SetOffset(current_pc.GetOffset() + current_offset);
134 DWARFCallFrameInfo *debug_frame = m_unwind_table.GetDebugFrameInfo();
136 m_unwind_plan_debug_frame_sp.reset(
137 new UnwindPlan(lldb::eRegisterKindGeneric));
138 if (!debug_frame->GetUnwindPlan(current_pc,
139 *m_unwind_plan_debug_frame_sp))
140 m_unwind_plan_debug_frame_sp.reset();
143 return m_unwind_plan_debug_frame_sp;
146 UnwindPlanSP FuncUnwinders::GetArmUnwindUnwindPlan(Target &target,
147 int current_offset) {
148 std::lock_guard<std::recursive_mutex> guard(m_mutex);
149 if (m_unwind_plan_arm_unwind_sp.get() || m_tried_unwind_plan_arm_unwind)
150 return m_unwind_plan_arm_unwind_sp;
152 m_tried_unwind_plan_arm_unwind = true;
153 if (m_range.GetBaseAddress().IsValid()) {
154 Address current_pc(m_range.GetBaseAddress());
155 if (current_offset != -1)
156 current_pc.SetOffset(current_pc.GetOffset() + current_offset);
157 ArmUnwindInfo *arm_unwind_info = m_unwind_table.GetArmUnwindInfo();
158 if (arm_unwind_info) {
159 m_unwind_plan_arm_unwind_sp.reset(
160 new UnwindPlan(lldb::eRegisterKindGeneric));
161 if (!arm_unwind_info->GetUnwindPlan(target, current_pc,
162 *m_unwind_plan_arm_unwind_sp))
163 m_unwind_plan_arm_unwind_sp.reset();
166 return m_unwind_plan_arm_unwind_sp;
169 UnwindPlanSP FuncUnwinders::GetEHFrameAugmentedUnwindPlan(Target &target,
171 int current_offset) {
172 std::lock_guard<std::recursive_mutex> guard(m_mutex);
173 if (m_unwind_plan_eh_frame_augmented_sp.get() ||
174 m_tried_unwind_plan_eh_frame_augmented)
175 return m_unwind_plan_eh_frame_augmented_sp;
177 // Only supported on x86 architectures where we get eh_frame from the
178 // compiler that describes the prologue instructions perfectly, and sometimes
179 // the epilogue instructions too.
180 if (target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_32_i386 &&
181 target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64 &&
182 target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) {
183 m_tried_unwind_plan_eh_frame_augmented = true;
184 return m_unwind_plan_eh_frame_augmented_sp;
187 m_tried_unwind_plan_eh_frame_augmented = true;
189 UnwindPlanSP eh_frame_plan = GetEHFrameUnwindPlan(target, current_offset);
191 return m_unwind_plan_eh_frame_augmented_sp;
193 m_unwind_plan_eh_frame_augmented_sp.reset(new UnwindPlan(*eh_frame_plan));
195 // Augment the eh_frame instructions with epilogue descriptions if necessary
196 // so the UnwindPlan can be used at any instruction in the function.
198 UnwindAssemblySP assembly_profiler_sp(GetUnwindAssemblyProfiler(target));
199 if (assembly_profiler_sp) {
200 if (!assembly_profiler_sp->AugmentUnwindPlanFromCallSite(
201 m_range, thread, *m_unwind_plan_eh_frame_augmented_sp)) {
202 m_unwind_plan_eh_frame_augmented_sp.reset();
205 m_unwind_plan_eh_frame_augmented_sp.reset();
207 return m_unwind_plan_eh_frame_augmented_sp;
211 FuncUnwinders::GetDebugFrameAugmentedUnwindPlan(Target &target, Thread &thread,
212 int current_offset) {
213 std::lock_guard<std::recursive_mutex> guard(m_mutex);
214 if (m_unwind_plan_debug_frame_augmented_sp.get() ||
215 m_tried_unwind_plan_debug_frame_augmented)
216 return m_unwind_plan_debug_frame_augmented_sp;
218 // Only supported on x86 architectures where we get debug_frame from the
219 // compiler that describes the prologue instructions perfectly, and sometimes
220 // the epilogue instructions too.
221 if (target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_32_i386 &&
222 target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64 &&
223 target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h) {
224 m_tried_unwind_plan_debug_frame_augmented = true;
225 return m_unwind_plan_debug_frame_augmented_sp;
228 m_tried_unwind_plan_debug_frame_augmented = true;
230 UnwindPlanSP debug_frame_plan =
231 GetDebugFrameUnwindPlan(target, current_offset);
232 if (!debug_frame_plan)
233 return m_unwind_plan_debug_frame_augmented_sp;
235 m_unwind_plan_debug_frame_augmented_sp.reset(
236 new UnwindPlan(*debug_frame_plan));
238 // Augment the debug_frame instructions with epilogue descriptions if
239 // necessary so the UnwindPlan can be used at any instruction in the
242 UnwindAssemblySP assembly_profiler_sp(GetUnwindAssemblyProfiler(target));
243 if (assembly_profiler_sp) {
244 if (!assembly_profiler_sp->AugmentUnwindPlanFromCallSite(
245 m_range, thread, *m_unwind_plan_debug_frame_augmented_sp)) {
246 m_unwind_plan_debug_frame_augmented_sp.reset();
249 m_unwind_plan_debug_frame_augmented_sp.reset();
250 return m_unwind_plan_debug_frame_augmented_sp;
253 UnwindPlanSP FuncUnwinders::GetAssemblyUnwindPlan(Target &target,
255 int current_offset) {
256 std::lock_guard<std::recursive_mutex> guard(m_mutex);
257 if (m_unwind_plan_assembly_sp.get() || m_tried_unwind_plan_assembly ||
258 m_unwind_table.GetAllowAssemblyEmulationUnwindPlans() == false) {
259 return m_unwind_plan_assembly_sp;
262 m_tried_unwind_plan_assembly = true;
264 UnwindAssemblySP assembly_profiler_sp(GetUnwindAssemblyProfiler(target));
265 if (assembly_profiler_sp) {
266 m_unwind_plan_assembly_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
267 if (!assembly_profiler_sp->GetNonCallSiteUnwindPlanFromAssembly(
268 m_range, thread, *m_unwind_plan_assembly_sp)) {
269 m_unwind_plan_assembly_sp.reset();
272 return m_unwind_plan_assembly_sp;
275 // This method compares the pc unwind rule in the first row of two UnwindPlans.
276 // If they have the same way of getting the pc value (e.g. "CFA - 8" + "CFA is
277 // sp"), then it will return LazyBoolTrue.
278 LazyBool FuncUnwinders::CompareUnwindPlansForIdenticalInitialPCLocation(
279 Thread &thread, const UnwindPlanSP &a, const UnwindPlanSP &b) {
280 LazyBool plans_are_identical = eLazyBoolCalculate;
282 RegisterNumber pc_reg(thread, eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
283 uint32_t pc_reg_lldb_regnum = pc_reg.GetAsKind(eRegisterKindLLDB);
285 if (a.get() && b.get()) {
286 UnwindPlan::RowSP a_first_row = a->GetRowAtIndex(0);
287 UnwindPlan::RowSP b_first_row = b->GetRowAtIndex(0);
289 if (a_first_row.get() && b_first_row.get()) {
290 UnwindPlan::Row::RegisterLocation a_pc_regloc;
291 UnwindPlan::Row::RegisterLocation b_pc_regloc;
293 a_first_row->GetRegisterInfo(pc_reg_lldb_regnum, a_pc_regloc);
294 b_first_row->GetRegisterInfo(pc_reg_lldb_regnum, b_pc_regloc);
296 plans_are_identical = eLazyBoolYes;
298 if (a_first_row->GetCFAValue() != b_first_row->GetCFAValue()) {
299 plans_are_identical = eLazyBoolNo;
301 if (a_pc_regloc != b_pc_regloc) {
302 plans_are_identical = eLazyBoolNo;
306 return plans_are_identical;
309 UnwindPlanSP FuncUnwinders::GetUnwindPlanAtNonCallSite(Target &target,
311 int current_offset) {
312 UnwindPlanSP eh_frame_sp = GetEHFrameUnwindPlan(target, current_offset);
314 eh_frame_sp = GetDebugFrameUnwindPlan(target, current_offset);
315 UnwindPlanSP arch_default_at_entry_sp =
316 GetUnwindPlanArchitectureDefaultAtFunctionEntry(thread);
317 UnwindPlanSP arch_default_sp = GetUnwindPlanArchitectureDefault(thread);
318 UnwindPlanSP assembly_sp =
319 GetAssemblyUnwindPlan(target, thread, current_offset);
321 // This point of this code is to detect when a function is using a non-
322 // standard ABI, and the eh_frame correctly describes that alternate ABI.
323 // This is addressing a specific situation on x86_64 linux systems where one
324 // function in a library pushes a value on the stack and jumps to another
325 // function. So using an assembly instruction based unwind will not work
326 // when you're in the second function - the stack has been modified in a non-
327 // ABI way. But we have eh_frame that correctly describes how to unwind from
328 // this location. So we're looking to see if the initial pc register save
329 // location from the eh_frame is different from the assembly unwind, the arch
330 // default unwind, and the arch default at initial function entry.
332 // We may have eh_frame that describes the entire function -- or we may have
333 // eh_frame that only describes the unwind after the prologue has executed --
334 // so we need to check both the arch default (once the prologue has executed)
335 // and the arch default at initial function entry. And we may be running on
336 // a target where we have only some of the assembly/arch default unwind plans
339 if (CompareUnwindPlansForIdenticalInitialPCLocation(
340 thread, eh_frame_sp, arch_default_at_entry_sp) == eLazyBoolNo &&
341 CompareUnwindPlansForIdenticalInitialPCLocation(
342 thread, eh_frame_sp, arch_default_sp) == eLazyBoolNo &&
343 CompareUnwindPlansForIdenticalInitialPCLocation(
344 thread, assembly_sp, arch_default_sp) == eLazyBoolNo) {
348 if (UnwindPlanSP plan_sp =
349 GetEHFrameAugmentedUnwindPlan(target, thread, current_offset))
351 if (UnwindPlanSP plan_sp =
352 GetDebugFrameAugmentedUnwindPlan(target, thread, current_offset))
358 UnwindPlanSP FuncUnwinders::GetUnwindPlanFastUnwind(Target &target,
360 std::lock_guard<std::recursive_mutex> guard(m_mutex);
361 if (m_unwind_plan_fast_sp.get() || m_tried_unwind_fast)
362 return m_unwind_plan_fast_sp;
364 m_tried_unwind_fast = true;
366 UnwindAssemblySP assembly_profiler_sp(GetUnwindAssemblyProfiler(target));
367 if (assembly_profiler_sp) {
368 m_unwind_plan_fast_sp.reset(new UnwindPlan(lldb::eRegisterKindGeneric));
369 if (!assembly_profiler_sp->GetFastUnwindPlan(m_range, thread,
370 *m_unwind_plan_fast_sp)) {
371 m_unwind_plan_fast_sp.reset();
374 return m_unwind_plan_fast_sp;
377 UnwindPlanSP FuncUnwinders::GetUnwindPlanArchitectureDefault(Thread &thread) {
378 std::lock_guard<std::recursive_mutex> guard(m_mutex);
379 if (m_unwind_plan_arch_default_sp.get() || m_tried_unwind_arch_default)
380 return m_unwind_plan_arch_default_sp;
382 m_tried_unwind_arch_default = true;
385 ProcessSP process_sp(thread.CalculateProcess());
387 ABI *abi = process_sp->GetABI().get();
389 m_unwind_plan_arch_default_sp.reset(
390 new UnwindPlan(lldb::eRegisterKindGeneric));
391 if (!abi->CreateDefaultUnwindPlan(*m_unwind_plan_arch_default_sp)) {
392 m_unwind_plan_arch_default_sp.reset();
397 return m_unwind_plan_arch_default_sp;
401 FuncUnwinders::GetUnwindPlanArchitectureDefaultAtFunctionEntry(Thread &thread) {
402 std::lock_guard<std::recursive_mutex> guard(m_mutex);
403 if (m_unwind_plan_arch_default_at_func_entry_sp.get() ||
404 m_tried_unwind_arch_default_at_func_entry)
405 return m_unwind_plan_arch_default_at_func_entry_sp;
407 m_tried_unwind_arch_default_at_func_entry = true;
410 ProcessSP process_sp(thread.CalculateProcess());
412 ABI *abi = process_sp->GetABI().get();
414 m_unwind_plan_arch_default_at_func_entry_sp.reset(
415 new UnwindPlan(lldb::eRegisterKindGeneric));
416 if (!abi->CreateFunctionEntryUnwindPlan(
417 *m_unwind_plan_arch_default_at_func_entry_sp)) {
418 m_unwind_plan_arch_default_at_func_entry_sp.reset();
423 return m_unwind_plan_arch_default_at_func_entry_sp;
426 Address &FuncUnwinders::GetFirstNonPrologueInsn(Target &target) {
427 std::lock_guard<std::recursive_mutex> guard(m_mutex);
428 if (m_first_non_prologue_insn.IsValid())
429 return m_first_non_prologue_insn;
431 ExecutionContext exe_ctx(target.shared_from_this(), false);
432 UnwindAssemblySP assembly_profiler_sp(GetUnwindAssemblyProfiler(target));
433 if (assembly_profiler_sp)
434 assembly_profiler_sp->FirstNonPrologueInsn(m_range, exe_ctx,
435 m_first_non_prologue_insn);
436 return m_first_non_prologue_insn;
439 const Address &FuncUnwinders::GetFunctionStartAddress() const {
440 return m_range.GetBaseAddress();
443 lldb::UnwindAssemblySP
444 FuncUnwinders::GetUnwindAssemblyProfiler(Target &target) {
445 UnwindAssemblySP assembly_profiler_sp;
447 if (m_unwind_table.GetArchitecture(arch)) {
448 arch.MergeFrom(target.GetArchitecture());
449 assembly_profiler_sp = UnwindAssembly::FindPlugin(arch);
451 return assembly_profiler_sp;
454 Address FuncUnwinders::GetLSDAAddress(Target &target) {
457 UnwindPlanSP unwind_plan_sp = GetEHFrameUnwindPlan(target, -1);
458 if (unwind_plan_sp.get() == nullptr) {
459 unwind_plan_sp = GetCompactUnwindUnwindPlan(target, -1);
461 if (unwind_plan_sp.get() && unwind_plan_sp->GetLSDAAddress().IsValid()) {
462 lsda_addr = unwind_plan_sp->GetLSDAAddress();
467 Address FuncUnwinders::GetPersonalityRoutinePtrAddress(Target &target) {
468 Address personality_addr;
470 UnwindPlanSP unwind_plan_sp = GetEHFrameUnwindPlan(target, -1);
471 if (unwind_plan_sp.get() == nullptr) {
472 unwind_plan_sp = GetCompactUnwindUnwindPlan(target, -1);
474 if (unwind_plan_sp.get() &&
475 unwind_plan_sp->GetPersonalityFunctionPtr().IsValid()) {
476 personality_addr = unwind_plan_sp->GetPersonalityFunctionPtr();
479 return personality_addr;