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/ObjectFile.h"
15 #include "lldb/Symbol/UnwindPlan.h"
16 #include "lldb/Symbol/UnwindTable.h"
17 #include "lldb/Target/ABI.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Process.h"
20 #include "lldb/Target/Thread.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Target/UnwindAssembly.h"
25 using namespace lldb_private;
28 FuncUnwinders::FuncUnwinders
30 UnwindTable& unwind_table,
31 UnwindAssembly *assembly_profiler,
34 m_unwind_table(unwind_table),
35 m_assembly_profiler(assembly_profiler),
37 m_mutex (Mutex::eMutexTypeNormal),
38 m_unwind_plan_call_site_sp (),
39 m_unwind_plan_non_call_site_sp (),
40 m_unwind_plan_fast_sp (),
41 m_unwind_plan_arch_default_sp (),
42 m_tried_unwind_at_call_site (false),
43 m_tried_unwind_at_non_call_site (false),
44 m_tried_unwind_fast (false),
45 m_tried_unwind_arch_default (false),
46 m_tried_unwind_arch_default_at_func_entry (false),
47 m_first_non_prologue_insn()
51 FuncUnwinders::~FuncUnwinders ()
56 FuncUnwinders::GetUnwindPlanAtCallSite (int current_offset)
58 // Lock the mutex to ensure we can always give out the most appropriate
59 // information. We want to make sure if someone requests a call site unwind
60 // plan, that they get one and don't run into a race condition where one
61 // thread has started to create the unwind plan and has put it into
62 // m_unwind_plan_call_site_sp, and have another thread enter this function
63 // and return the partially filled in m_unwind_plan_call_site_sp pointer.
64 // We also want to make sure that we lock out other unwind plans from
65 // being accessed until this one is done creating itself in case someone
66 // had some code like:
67 // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
68 // if (best_unwind_plan == NULL)
69 // best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
70 Mutex::Locker locker (m_mutex);
71 if (m_tried_unwind_at_call_site == false && m_unwind_plan_call_site_sp.get() == NULL)
73 m_tried_unwind_at_call_site = true;
74 // We have cases (e.g. with _sigtramp on Mac OS X) where the hand-written eh_frame unwind info for a
75 // function does not cover the entire range of the function and so the FDE only lists a subset of the
76 // address range. If we try to look up the unwind info by the starting address of the function
77 // (i.e. m_range.GetBaseAddress()) we may not find the eh_frame FDE. We need to use the actual byte offset
78 // into the function when looking it up.
80 if (m_range.GetBaseAddress().IsValid())
82 Address current_pc (m_range.GetBaseAddress ());
83 if (current_offset != -1)
84 current_pc.SetOffset (current_pc.GetOffset() + current_offset);
86 DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo();
89 m_unwind_plan_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
90 if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_plan_call_site_sp))
91 m_unwind_plan_call_site_sp.reset();
95 return m_unwind_plan_call_site_sp;
99 FuncUnwinders::GetUnwindPlanAtNonCallSite (Thread& thread)
101 // Lock the mutex to ensure we can always give out the most appropriate
102 // information. We want to make sure if someone requests an unwind
103 // plan, that they get one and don't run into a race condition where one
104 // thread has started to create the unwind plan and has put it into
105 // the unique pointer member variable, and have another thread enter this function
106 // and return the partially filled pointer contained in the unique pointer.
107 // We also want to make sure that we lock out other unwind plans from
108 // being accessed until this one is done creating itself in case someone
109 // had some code like:
110 // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
111 // if (best_unwind_plan == NULL)
112 // best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
113 Mutex::Locker locker (m_mutex);
114 if (m_tried_unwind_at_non_call_site == false && m_unwind_plan_non_call_site_sp.get() == NULL)
116 m_tried_unwind_at_non_call_site = true;
117 if (m_assembly_profiler)
119 m_unwind_plan_non_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
120 if (!m_assembly_profiler->GetNonCallSiteUnwindPlanFromAssembly (m_range, thread, *m_unwind_plan_non_call_site_sp))
121 m_unwind_plan_non_call_site_sp.reset();
124 return m_unwind_plan_non_call_site_sp;
128 FuncUnwinders::GetUnwindPlanFastUnwind (Thread& thread)
130 // Lock the mutex to ensure we can always give out the most appropriate
131 // information. We want to make sure if someone requests an unwind
132 // plan, that they get one and don't run into a race condition where one
133 // thread has started to create the unwind plan and has put it into
134 // the unique pointer member variable, and have another thread enter this function
135 // and return the partially filled pointer contained in the unique pointer.
136 // We also want to make sure that we lock out other unwind plans from
137 // being accessed until this one is done creating itself in case someone
138 // had some code like:
139 // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
140 // if (best_unwind_plan == NULL)
141 // best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
142 Mutex::Locker locker (m_mutex);
143 if (m_tried_unwind_fast == false && m_unwind_plan_fast_sp.get() == NULL)
145 m_tried_unwind_fast = true;
146 if (m_assembly_profiler)
148 m_unwind_plan_fast_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
149 if (!m_assembly_profiler->GetFastUnwindPlan (m_range, thread, *m_unwind_plan_fast_sp))
150 m_unwind_plan_fast_sp.reset();
153 return m_unwind_plan_fast_sp;
157 FuncUnwinders::GetUnwindPlanArchitectureDefault (Thread& thread)
159 // Lock the mutex to ensure we can always give out the most appropriate
160 // information. We want to make sure if someone requests an unwind
161 // plan, that they get one and don't run into a race condition where one
162 // thread has started to create the unwind plan and has put it into
163 // the unique pointer member variable, and have another thread enter this function
164 // and return the partially filled pointer contained in the unique pointer.
165 // We also want to make sure that we lock out other unwind plans from
166 // being accessed until this one is done creating itself in case someone
167 // had some code like:
168 // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
169 // if (best_unwind_plan == NULL)
170 // best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
171 Mutex::Locker locker (m_mutex);
172 if (m_tried_unwind_arch_default == false && m_unwind_plan_arch_default_sp.get() == NULL)
174 m_tried_unwind_arch_default = true;
176 ProcessSP process_sp (thread.CalculateProcess());
179 ABI *abi = process_sp->GetABI().get();
182 m_unwind_plan_arch_default_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
183 if (m_unwind_plan_arch_default_sp)
184 abi->CreateDefaultUnwindPlan(*m_unwind_plan_arch_default_sp);
189 return m_unwind_plan_arch_default_sp;
193 FuncUnwinders::GetUnwindPlanArchitectureDefaultAtFunctionEntry (Thread& thread)
195 // Lock the mutex to ensure we can always give out the most appropriate
196 // information. We want to make sure if someone requests an unwind
197 // plan, that they get one and don't run into a race condition where one
198 // thread has started to create the unwind plan and has put it into
199 // the unique pointer member variable, and have another thread enter this function
200 // and return the partially filled pointer contained in the unique pointer.
201 // We also want to make sure that we lock out other unwind plans from
202 // being accessed until this one is done creating itself in case someone
203 // had some code like:
204 // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...)
205 // if (best_unwind_plan == NULL)
206 // best_unwind_plan = GetUnwindPlanAtNonCallSite (...)
207 Mutex::Locker locker (m_mutex);
208 if (m_tried_unwind_arch_default_at_func_entry == false && m_unwind_plan_arch_default_at_func_entry_sp.get() == NULL)
210 m_tried_unwind_arch_default_at_func_entry = true;
212 ProcessSP process_sp (thread.CalculateProcess());
215 ABI *abi = process_sp->GetABI().get();
218 m_unwind_plan_arch_default_at_func_entry_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric));
219 if (m_unwind_plan_arch_default_at_func_entry_sp)
220 abi->CreateFunctionEntryUnwindPlan(*m_unwind_plan_arch_default_at_func_entry_sp);
225 return m_unwind_plan_arch_default_at_func_entry_sp;
230 FuncUnwinders::GetFirstNonPrologueInsn (Target& target)
232 if (m_first_non_prologue_insn.IsValid())
233 return m_first_non_prologue_insn;
234 ExecutionContext exe_ctx (target.shared_from_this(), false);
235 if (m_assembly_profiler)
236 m_assembly_profiler->FirstNonPrologueInsn (m_range, exe_ctx, m_first_non_prologue_insn);
237 return m_first_non_prologue_insn;
241 FuncUnwinders::GetFunctionStartAddress () const
243 return m_range.GetBaseAddress();
247 FuncUnwinders::InvalidateNonCallSiteUnwindPlan (lldb_private::Thread& thread)
249 UnwindPlanSP arch_default = GetUnwindPlanArchitectureDefault (thread);
250 if (arch_default && m_tried_unwind_at_call_site)
252 m_unwind_plan_call_site_sp = arch_default;