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/Core/AddressRange.h"
11 #include "lldb/Core/Address.h"
12 #include "lldb/Symbol/FuncUnwinders.h"
13 #include "lldb/Symbol/DWARFCallFrameInfo.h"
14 #include "lldb/Symbol/CompactUnwindInfo.h"
15 #include "lldb/Symbol/ObjectFile.h"
16 #include "lldb/Symbol/UnwindPlan.h"
17 #include "lldb/Symbol/UnwindTable.h"
18 #include "lldb/Target/ABI.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/Thread.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/UnwindAssembly.h"
26 using namespace lldb_private;
28 //------------------------------------------------
30 //------------------------------------------------
32 FuncUnwinders::FuncUnwinders (UnwindTable& unwind_table, AddressRange range) :
33 m_unwind_table (unwind_table),
35 m_mutex (Mutex::eMutexTypeRecursive),
36 m_unwind_plan_assembly_sp (),
37 m_unwind_plan_eh_frame_sp (),
38 m_unwind_plan_eh_frame_augmented_sp (),
39 m_unwind_plan_compact_unwind (),
40 m_unwind_plan_fast_sp (),
41 m_unwind_plan_arch_default_sp (),
42 m_unwind_plan_arch_default_at_func_entry_sp (),
43 m_tried_unwind_plan_assembly (false),
44 m_tried_unwind_plan_eh_frame (false),
45 m_tried_unwind_plan_eh_frame_augmented (false),
46 m_tried_unwind_plan_compact_unwind (false),
47 m_tried_unwind_fast (false),
48 m_tried_unwind_arch_default (false),
49 m_tried_unwind_arch_default_at_func_entry (false),
50 m_first_non_prologue_insn ()
54 //------------------------------------------------
56 //------------------------------------------------
58 FuncUnwinders::~FuncUnwinders ()
63 FuncUnwinders::GetUnwindPlanAtCallSite (Target &target, int current_offset)
65 Mutex::Locker locker (m_mutex);
67 UnwindPlanSP unwind_plan_sp = GetEHFrameUnwindPlan (target, current_offset);
68 if (unwind_plan_sp.get() == nullptr)
70 unwind_plan_sp = GetCompactUnwindUnwindPlan (target, current_offset);
73 return unwind_plan_sp;
77 FuncUnwinders::GetCompactUnwindUnwindPlan (Target &target, int current_offset)
79 if (m_unwind_plan_compact_unwind.size() > 0)
80 return m_unwind_plan_compact_unwind[0]; // FIXME support multiple compact unwind plans for one func
81 if (m_tried_unwind_plan_compact_unwind)
82 return UnwindPlanSP();
84 Mutex::Locker lock (m_mutex);
85 m_tried_unwind_plan_compact_unwind = true;
86 if (m_range.GetBaseAddress().IsValid())
88 Address current_pc (m_range.GetBaseAddress ());
89 if (current_offset != -1)
90 current_pc.SetOffset (current_pc.GetOffset() + current_offset);
91 CompactUnwindInfo *compact_unwind = m_unwind_table.GetCompactUnwindInfo();
94 UnwindPlanSP unwind_plan_sp (new UnwindPlan (lldb::eRegisterKindGeneric));
95 if (compact_unwind->GetUnwindPlan (target, current_pc, *unwind_plan_sp))
97 m_unwind_plan_compact_unwind.push_back (unwind_plan_sp);
98 return m_unwind_plan_compact_unwind[0]; // FIXME support multiple compact unwind plans for one func
102 return UnwindPlanSP();
106 FuncUnwinders::GetEHFrameUnwindPlan (Target &target, int current_offset)
108 if (m_unwind_plan_eh_frame_sp.get() || m_tried_unwind_plan_eh_frame)
109 return m_unwind_plan_eh_frame_sp;
111 Mutex::Locker lock (m_mutex);
112 m_tried_unwind_plan_eh_frame = true;
113 if (m_range.GetBaseAddress().IsValid())
115 Address current_pc (m_range.GetBaseAddress ());
116 if (current_offset != -1)
117 current_pc.SetOffset (current_pc.GetOffset() + current_offset);
118 DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo();
121 m_unwind_plan_eh_frame_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
122 if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_plan_eh_frame_sp))
123 m_unwind_plan_eh_frame_sp.reset();
126 return m_unwind_plan_eh_frame_sp;
130 FuncUnwinders::GetEHFrameAugmentedUnwindPlan (Target &target, Thread &thread, int current_offset)
132 if (m_unwind_plan_eh_frame_augmented_sp.get() || m_tried_unwind_plan_eh_frame_augmented)
133 return m_unwind_plan_eh_frame_augmented_sp;
135 // Only supported on x86 architectures where we get eh_frame from the compiler that describes
136 // the prologue instructions perfectly, and sometimes the epilogue instructions too.
137 if (target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_32_i386
138 && target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64
139 && target.GetArchitecture().GetCore() != ArchSpec::eCore_x86_64_x86_64h)
141 m_tried_unwind_plan_eh_frame_augmented = true;
142 return m_unwind_plan_eh_frame_augmented_sp;
145 Mutex::Locker lock (m_mutex);
146 m_tried_unwind_plan_eh_frame_augmented = true;
148 if (m_range.GetBaseAddress().IsValid())
150 Address current_pc (m_range.GetBaseAddress ());
151 if (current_offset != -1)
152 current_pc.SetOffset (current_pc.GetOffset() + current_offset);
153 DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo();
156 m_unwind_plan_eh_frame_augmented_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
157 if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_plan_eh_frame_augmented_sp))
159 m_unwind_plan_eh_frame_augmented_sp.reset();
163 // Augment the eh_frame instructions with epilogue descriptions if necessary so the
164 // UnwindPlan can be used at any instruction in the function.
166 UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
167 if (assembly_profiler_sp)
169 if (!assembly_profiler_sp->AugmentUnwindPlanFromCallSite (m_range, thread, *m_unwind_plan_eh_frame_augmented_sp))
171 m_unwind_plan_eh_frame_augmented_sp.reset();
176 m_unwind_plan_eh_frame_augmented_sp.reset();
181 return m_unwind_plan_eh_frame_augmented_sp;
186 FuncUnwinders::GetAssemblyUnwindPlan (Target &target, Thread &thread, int current_offset)
188 if (m_unwind_plan_assembly_sp.get() || m_tried_unwind_plan_assembly)
189 return m_unwind_plan_assembly_sp;
191 Mutex::Locker lock (m_mutex);
192 m_tried_unwind_plan_assembly = true;
194 UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
195 if (assembly_profiler_sp)
197 m_unwind_plan_assembly_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
198 if (!assembly_profiler_sp->GetNonCallSiteUnwindPlanFromAssembly (m_range, thread, *m_unwind_plan_assembly_sp))
200 m_unwind_plan_assembly_sp.reset();
203 return m_unwind_plan_assembly_sp;
208 FuncUnwinders::GetUnwindPlanAtNonCallSite (Target& target, Thread& thread, int current_offset)
210 UnwindPlanSP non_call_site_unwindplan_sp = GetEHFrameAugmentedUnwindPlan (target, thread, current_offset);
211 if (non_call_site_unwindplan_sp.get() == nullptr)
213 non_call_site_unwindplan_sp = GetAssemblyUnwindPlan (target, thread, current_offset);
215 return non_call_site_unwindplan_sp;
219 FuncUnwinders::GetUnwindPlanFastUnwind (Thread& thread)
221 if (m_unwind_plan_fast_sp.get() || m_tried_unwind_fast)
222 return m_unwind_plan_fast_sp;
224 Mutex::Locker locker (m_mutex);
225 m_tried_unwind_fast = true;
227 UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
228 if (assembly_profiler_sp)
230 m_unwind_plan_fast_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
231 if (!assembly_profiler_sp->GetFastUnwindPlan (m_range, thread, *m_unwind_plan_fast_sp))
233 m_unwind_plan_fast_sp.reset();
236 return m_unwind_plan_fast_sp;
240 FuncUnwinders::GetUnwindPlanArchitectureDefault (Thread& thread)
242 if (m_unwind_plan_arch_default_sp.get() || m_tried_unwind_arch_default)
243 return m_unwind_plan_arch_default_sp;
245 Mutex::Locker locker (m_mutex);
246 m_tried_unwind_arch_default = true;
249 ProcessSP process_sp (thread.CalculateProcess());
252 ABI *abi = process_sp->GetABI().get();
255 m_unwind_plan_arch_default_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
256 if (!abi->CreateDefaultUnwindPlan(*m_unwind_plan_arch_default_sp))
258 m_unwind_plan_arch_default_sp.reset();
263 return m_unwind_plan_arch_default_sp;
267 FuncUnwinders::GetUnwindPlanArchitectureDefaultAtFunctionEntry (Thread& thread)
269 if (m_unwind_plan_arch_default_at_func_entry_sp.get() || m_tried_unwind_arch_default_at_func_entry)
270 return m_unwind_plan_arch_default_at_func_entry_sp;
272 Mutex::Locker locker (m_mutex);
273 m_tried_unwind_arch_default_at_func_entry = true;
276 ProcessSP process_sp (thread.CalculateProcess());
279 ABI *abi = process_sp->GetABI().get();
282 m_unwind_plan_arch_default_at_func_entry_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
283 if (!abi->CreateFunctionEntryUnwindPlan(*m_unwind_plan_arch_default_at_func_entry_sp))
285 m_unwind_plan_arch_default_at_func_entry_sp.reset();
290 return m_unwind_plan_arch_default_at_func_entry_sp;
295 FuncUnwinders::GetFirstNonPrologueInsn (Target& target)
297 if (m_first_non_prologue_insn.IsValid())
298 return m_first_non_prologue_insn;
300 Mutex::Locker locker (m_mutex);
301 ExecutionContext exe_ctx (target.shared_from_this(), false);
302 UnwindAssemblySP assembly_profiler_sp (GetUnwindAssemblyProfiler());
303 if (assembly_profiler_sp)
304 assembly_profiler_sp->FirstNonPrologueInsn (m_range, exe_ctx, m_first_non_prologue_insn);
305 return m_first_non_prologue_insn;
309 FuncUnwinders::GetFunctionStartAddress () const
311 return m_range.GetBaseAddress();
314 lldb::UnwindAssemblySP
315 FuncUnwinders::GetUnwindAssemblyProfiler ()
317 UnwindAssemblySP assembly_profiler_sp;
319 if (m_unwind_table.GetArchitecture (arch))
321 assembly_profiler_sp = UnwindAssembly::FindPlugin (arch);
323 return assembly_profiler_sp;
327 FuncUnwinders::GetLSDAAddress (Target &target)
331 UnwindPlanSP unwind_plan_sp = GetEHFrameUnwindPlan (target, -1);
332 if (unwind_plan_sp.get() == nullptr)
334 unwind_plan_sp = GetCompactUnwindUnwindPlan (target, -1);
336 if (unwind_plan_sp.get() && unwind_plan_sp->GetLSDAAddress().IsValid())
338 lsda_addr = unwind_plan_sp->GetLSDAAddress();
345 FuncUnwinders::GetPersonalityRoutinePtrAddress (Target &target)
347 Address personality_addr;
349 UnwindPlanSP unwind_plan_sp = GetEHFrameUnwindPlan (target, -1);
350 if (unwind_plan_sp.get() == nullptr)
352 unwind_plan_sp = GetCompactUnwindUnwindPlan (target, -1);
354 if (unwind_plan_sp.get() && unwind_plan_sp->GetPersonalityFunctionPtr().IsValid())
356 personality_addr = unwind_plan_sp->GetPersonalityFunctionPtr();
359 return personality_addr;