1254721Semaste//===-- FuncUnwinders.cpp ----------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "lldb/Core/AddressRange.h" 11254721Semaste#include "lldb/Core/Address.h" 12254721Semaste#include "lldb/Symbol/FuncUnwinders.h" 13254721Semaste#include "lldb/Symbol/DWARFCallFrameInfo.h" 14254721Semaste#include "lldb/Symbol/ObjectFile.h" 15254721Semaste#include "lldb/Symbol/UnwindPlan.h" 16254721Semaste#include "lldb/Symbol/UnwindTable.h" 17254721Semaste#include "lldb/Target/ABI.h" 18254721Semaste#include "lldb/Target/ExecutionContext.h" 19254721Semaste#include "lldb/Target/Process.h" 20254721Semaste#include "lldb/Target/Thread.h" 21254721Semaste#include "lldb/Target/Target.h" 22254721Semaste#include "lldb/Target/UnwindAssembly.h" 23254721Semaste 24254721Semasteusing namespace lldb; 25254721Semasteusing namespace lldb_private; 26254721Semaste 27254721Semaste 28254721SemasteFuncUnwinders::FuncUnwinders 29254721Semaste( 30254721Semaste UnwindTable& unwind_table, 31269024Semaste const lldb::UnwindAssemblySP& assembly_profiler, 32254721Semaste AddressRange range 33254721Semaste) : 34254721Semaste m_unwind_table(unwind_table), 35254721Semaste m_assembly_profiler(assembly_profiler), 36254721Semaste m_range(range), 37254721Semaste m_mutex (Mutex::eMutexTypeNormal), 38254721Semaste m_unwind_plan_call_site_sp (), 39254721Semaste m_unwind_plan_non_call_site_sp (), 40254721Semaste m_unwind_plan_fast_sp (), 41254721Semaste m_unwind_plan_arch_default_sp (), 42254721Semaste m_tried_unwind_at_call_site (false), 43254721Semaste m_tried_unwind_at_non_call_site (false), 44254721Semaste m_tried_unwind_fast (false), 45254721Semaste m_tried_unwind_arch_default (false), 46254721Semaste m_tried_unwind_arch_default_at_func_entry (false), 47254721Semaste m_first_non_prologue_insn() 48254721Semaste{ 49254721Semaste} 50254721Semaste 51254721SemasteFuncUnwinders::~FuncUnwinders () 52254721Semaste{ 53254721Semaste} 54254721Semaste 55254721SemasteUnwindPlanSP 56254721SemasteFuncUnwinders::GetUnwindPlanAtCallSite (int current_offset) 57254721Semaste{ 58254721Semaste // Lock the mutex to ensure we can always give out the most appropriate 59254721Semaste // information. We want to make sure if someone requests a call site unwind 60254721Semaste // plan, that they get one and don't run into a race condition where one 61254721Semaste // thread has started to create the unwind plan and has put it into 62254721Semaste // m_unwind_plan_call_site_sp, and have another thread enter this function 63254721Semaste // and return the partially filled in m_unwind_plan_call_site_sp pointer. 64254721Semaste // We also want to make sure that we lock out other unwind plans from 65254721Semaste // being accessed until this one is done creating itself in case someone 66254721Semaste // had some code like: 67254721Semaste // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...) 68254721Semaste // if (best_unwind_plan == NULL) 69254721Semaste // best_unwind_plan = GetUnwindPlanAtNonCallSite (...) 70254721Semaste Mutex::Locker locker (m_mutex); 71254721Semaste if (m_tried_unwind_at_call_site == false && m_unwind_plan_call_site_sp.get() == NULL) 72254721Semaste { 73254721Semaste m_tried_unwind_at_call_site = true; 74254721Semaste // We have cases (e.g. with _sigtramp on Mac OS X) where the hand-written eh_frame unwind info for a 75254721Semaste // function does not cover the entire range of the function and so the FDE only lists a subset of the 76254721Semaste // address range. If we try to look up the unwind info by the starting address of the function 77254721Semaste // (i.e. m_range.GetBaseAddress()) we may not find the eh_frame FDE. We need to use the actual byte offset 78254721Semaste // into the function when looking it up. 79254721Semaste 80254721Semaste if (m_range.GetBaseAddress().IsValid()) 81254721Semaste { 82254721Semaste Address current_pc (m_range.GetBaseAddress ()); 83254721Semaste if (current_offset != -1) 84254721Semaste current_pc.SetOffset (current_pc.GetOffset() + current_offset); 85254721Semaste 86254721Semaste DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo(); 87254721Semaste if (eh_frame) 88254721Semaste { 89254721Semaste m_unwind_plan_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 90254721Semaste if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_plan_call_site_sp)) 91254721Semaste m_unwind_plan_call_site_sp.reset(); 92254721Semaste } 93254721Semaste } 94254721Semaste } 95254721Semaste return m_unwind_plan_call_site_sp; 96254721Semaste} 97254721Semaste 98254721SemasteUnwindPlanSP 99254721SemasteFuncUnwinders::GetUnwindPlanAtNonCallSite (Thread& thread) 100254721Semaste{ 101254721Semaste // Lock the mutex to ensure we can always give out the most appropriate 102254721Semaste // information. We want to make sure if someone requests an unwind 103254721Semaste // plan, that they get one and don't run into a race condition where one 104254721Semaste // thread has started to create the unwind plan and has put it into 105254721Semaste // the unique pointer member variable, and have another thread enter this function 106254721Semaste // and return the partially filled pointer contained in the unique pointer. 107254721Semaste // We also want to make sure that we lock out other unwind plans from 108254721Semaste // being accessed until this one is done creating itself in case someone 109254721Semaste // had some code like: 110254721Semaste // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...) 111254721Semaste // if (best_unwind_plan == NULL) 112254721Semaste // best_unwind_plan = GetUnwindPlanAtNonCallSite (...) 113254721Semaste Mutex::Locker locker (m_mutex); 114254721Semaste if (m_tried_unwind_at_non_call_site == false && m_unwind_plan_non_call_site_sp.get() == NULL) 115254721Semaste { 116254721Semaste m_tried_unwind_at_non_call_site = true; 117263363Semaste if (m_assembly_profiler) 118263363Semaste { 119263363Semaste m_unwind_plan_non_call_site_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 120263363Semaste if (!m_assembly_profiler->GetNonCallSiteUnwindPlanFromAssembly (m_range, thread, *m_unwind_plan_non_call_site_sp)) 121263363Semaste m_unwind_plan_non_call_site_sp.reset(); 122263363Semaste } 123254721Semaste } 124254721Semaste return m_unwind_plan_non_call_site_sp; 125254721Semaste} 126254721Semaste 127254721SemasteUnwindPlanSP 128254721SemasteFuncUnwinders::GetUnwindPlanFastUnwind (Thread& thread) 129254721Semaste{ 130254721Semaste // Lock the mutex to ensure we can always give out the most appropriate 131254721Semaste // information. We want to make sure if someone requests an unwind 132254721Semaste // plan, that they get one and don't run into a race condition where one 133254721Semaste // thread has started to create the unwind plan and has put it into 134254721Semaste // the unique pointer member variable, and have another thread enter this function 135254721Semaste // and return the partially filled pointer contained in the unique pointer. 136254721Semaste // We also want to make sure that we lock out other unwind plans from 137254721Semaste // being accessed until this one is done creating itself in case someone 138254721Semaste // had some code like: 139254721Semaste // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...) 140254721Semaste // if (best_unwind_plan == NULL) 141254721Semaste // best_unwind_plan = GetUnwindPlanAtNonCallSite (...) 142254721Semaste Mutex::Locker locker (m_mutex); 143254721Semaste if (m_tried_unwind_fast == false && m_unwind_plan_fast_sp.get() == NULL) 144254721Semaste { 145254721Semaste m_tried_unwind_fast = true; 146263363Semaste if (m_assembly_profiler) 147263363Semaste { 148263363Semaste m_unwind_plan_fast_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 149263363Semaste if (!m_assembly_profiler->GetFastUnwindPlan (m_range, thread, *m_unwind_plan_fast_sp)) 150263363Semaste m_unwind_plan_fast_sp.reset(); 151263363Semaste } 152254721Semaste } 153254721Semaste return m_unwind_plan_fast_sp; 154254721Semaste} 155254721Semaste 156254721SemasteUnwindPlanSP 157254721SemasteFuncUnwinders::GetUnwindPlanArchitectureDefault (Thread& thread) 158254721Semaste{ 159254721Semaste // Lock the mutex to ensure we can always give out the most appropriate 160254721Semaste // information. We want to make sure if someone requests an unwind 161254721Semaste // plan, that they get one and don't run into a race condition where one 162254721Semaste // thread has started to create the unwind plan and has put it into 163254721Semaste // the unique pointer member variable, and have another thread enter this function 164254721Semaste // and return the partially filled pointer contained in the unique pointer. 165254721Semaste // We also want to make sure that we lock out other unwind plans from 166254721Semaste // being accessed until this one is done creating itself in case someone 167254721Semaste // had some code like: 168254721Semaste // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...) 169254721Semaste // if (best_unwind_plan == NULL) 170254721Semaste // best_unwind_plan = GetUnwindPlanAtNonCallSite (...) 171254721Semaste Mutex::Locker locker (m_mutex); 172254721Semaste if (m_tried_unwind_arch_default == false && m_unwind_plan_arch_default_sp.get() == NULL) 173254721Semaste { 174254721Semaste m_tried_unwind_arch_default = true; 175254721Semaste Address current_pc; 176254721Semaste ProcessSP process_sp (thread.CalculateProcess()); 177254721Semaste if (process_sp) 178254721Semaste { 179254721Semaste ABI *abi = process_sp->GetABI().get(); 180254721Semaste if (abi) 181254721Semaste { 182254721Semaste m_unwind_plan_arch_default_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 183254721Semaste if (m_unwind_plan_arch_default_sp) 184254721Semaste abi->CreateDefaultUnwindPlan(*m_unwind_plan_arch_default_sp); 185254721Semaste } 186254721Semaste } 187254721Semaste } 188254721Semaste 189254721Semaste return m_unwind_plan_arch_default_sp; 190254721Semaste} 191254721Semaste 192254721SemasteUnwindPlanSP 193254721SemasteFuncUnwinders::GetUnwindPlanArchitectureDefaultAtFunctionEntry (Thread& thread) 194254721Semaste{ 195254721Semaste // Lock the mutex to ensure we can always give out the most appropriate 196254721Semaste // information. We want to make sure if someone requests an unwind 197254721Semaste // plan, that they get one and don't run into a race condition where one 198254721Semaste // thread has started to create the unwind plan and has put it into 199254721Semaste // the unique pointer member variable, and have another thread enter this function 200254721Semaste // and return the partially filled pointer contained in the unique pointer. 201254721Semaste // We also want to make sure that we lock out other unwind plans from 202254721Semaste // being accessed until this one is done creating itself in case someone 203254721Semaste // had some code like: 204254721Semaste // UnwindPlan *best_unwind_plan = ...GetUnwindPlanAtCallSite (...) 205254721Semaste // if (best_unwind_plan == NULL) 206254721Semaste // best_unwind_plan = GetUnwindPlanAtNonCallSite (...) 207254721Semaste Mutex::Locker locker (m_mutex); 208254721Semaste if (m_tried_unwind_arch_default_at_func_entry == false && m_unwind_plan_arch_default_at_func_entry_sp.get() == NULL) 209254721Semaste { 210254721Semaste m_tried_unwind_arch_default_at_func_entry = true; 211254721Semaste Address current_pc; 212254721Semaste ProcessSP process_sp (thread.CalculateProcess()); 213254721Semaste if (process_sp) 214254721Semaste { 215254721Semaste ABI *abi = process_sp->GetABI().get(); 216254721Semaste if (abi) 217254721Semaste { 218254721Semaste m_unwind_plan_arch_default_at_func_entry_sp.reset (new UnwindPlan (lldb::eRegisterKindGeneric)); 219254721Semaste if (m_unwind_plan_arch_default_at_func_entry_sp) 220254721Semaste abi->CreateFunctionEntryUnwindPlan(*m_unwind_plan_arch_default_at_func_entry_sp); 221254721Semaste } 222254721Semaste } 223254721Semaste } 224254721Semaste 225263363Semaste return m_unwind_plan_arch_default_at_func_entry_sp; 226254721Semaste} 227254721Semaste 228254721Semaste 229254721SemasteAddress& 230254721SemasteFuncUnwinders::GetFirstNonPrologueInsn (Target& target) 231254721Semaste{ 232254721Semaste if (m_first_non_prologue_insn.IsValid()) 233254721Semaste return m_first_non_prologue_insn; 234254721Semaste ExecutionContext exe_ctx (target.shared_from_this(), false); 235263363Semaste if (m_assembly_profiler) 236263363Semaste m_assembly_profiler->FirstNonPrologueInsn (m_range, exe_ctx, m_first_non_prologue_insn); 237254721Semaste return m_first_non_prologue_insn; 238254721Semaste} 239254721Semaste 240254721Semasteconst Address& 241254721SemasteFuncUnwinders::GetFunctionStartAddress () const 242254721Semaste{ 243254721Semaste return m_range.GetBaseAddress(); 244254721Semaste} 245254721Semaste 246254721Semastevoid 247254721SemasteFuncUnwinders::InvalidateNonCallSiteUnwindPlan (lldb_private::Thread& thread) 248254721Semaste{ 249254721Semaste UnwindPlanSP arch_default = GetUnwindPlanArchitectureDefault (thread); 250254721Semaste if (arch_default && m_tried_unwind_at_call_site) 251254721Semaste { 252254721Semaste m_unwind_plan_call_site_sp = arch_default; 253254721Semaste } 254254721Semaste} 255