Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 1 | //===-- FuncUnwinders.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/lldb-private.h" |
| 11 | #include "lldb/Symbol/FuncUnwinders.h" |
| 12 | #include "lldb/Symbol/ObjectFile.h" |
| 13 | #include "lldb/Symbol/UnwindPlan.h" |
| 14 | #include "lldb/Core/AddressRange.h" |
| 15 | #include "lldb/Core/Address.h" |
| 16 | #include "lldb/Symbol/UnwindTable.h" |
| 17 | #include "lldb/Utility/UnwindAssemblyProfiler.h" |
| 18 | #include "lldb/Utility/ArchDefaultUnwindPlan.h" |
| 19 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| 20 | #include "lldb/Target/Thread.h" |
| 21 | #include "lldb/Target/Target.h" |
| 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 27 | FuncUnwinders::FuncUnwinders |
| 28 | ( |
| 29 | UnwindTable& unwind_table, |
| 30 | UnwindAssemblyProfiler *assembly_profiler, |
| 31 | AddressRange range |
| 32 | ) : |
| 33 | m_unwind_table(unwind_table), |
| 34 | m_assembly_profiler(assembly_profiler), |
| 35 | m_range(range), |
| 36 | m_unwind_at_call_site_ap (), |
| 37 | m_unwind_at_non_call_site_ap (), |
| 38 | m_fast_unwind_ap (), |
| 39 | m_arch_default_unwind (NULL), |
| 40 | m_tried_unwind_at_call_site (false), |
| 41 | m_tried_unwind_at_non_call_site (false), |
| 42 | m_tried_fast_unwind (false), |
| 43 | m_tried_arch_default_unwind (false), |
| 44 | m_first_non_prologue_insn() |
| 45 | { |
| 46 | } |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 47 | |
| 48 | FuncUnwinders::~FuncUnwinders () |
| 49 | { |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | UnwindPlan* |
Jason Molenda | e4b0784 | 2010-11-12 05:23:10 +0000 | [diff] [blame] | 53 | FuncUnwinders::GetUnwindPlanAtCallSite (int current_offset) |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 54 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 55 | if (m_tried_unwind_at_call_site == false && m_unwind_at_call_site_ap.get() == NULL) |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 56 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 57 | m_tried_unwind_at_call_site = true; |
| 58 | // We have cases (e.g. with _sigtramp on Mac OS X) where the hand-written eh_frame unwind info for a |
| 59 | // function does not cover the entire range of the function and so the FDE only lists a subset of the |
| 60 | // address range. If we try to look up the unwind info by the starting address of the function |
| 61 | // (i.e. m_range.GetBaseAddress()) we may not find the eh_frame FDE. We need to use the actual byte offset |
| 62 | // into the function when looking it up. |
| 63 | |
| 64 | if (m_range.GetBaseAddress().IsValid()) |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 65 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 66 | Address current_pc (m_range.GetBaseAddress ()); |
| 67 | if (current_offset != -1) |
| 68 | current_pc.SetOffset (current_pc.GetOffset() + current_offset); |
| 69 | |
| 70 | DWARFCallFrameInfo *eh_frame = m_unwind_table.GetEHFrameInfo(); |
| 71 | if (eh_frame) |
| 72 | { |
| 73 | m_unwind_at_call_site_ap.reset (new UnwindPlan); |
| 74 | if (!eh_frame->GetUnwindPlan (current_pc, *m_unwind_at_call_site_ap)) |
| 75 | m_unwind_at_call_site_ap.reset(); |
| 76 | } |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 79 | return m_unwind_at_call_site_ap.get(); |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | UnwindPlan* |
| 83 | FuncUnwinders::GetUnwindPlanAtNonCallSite (Thread& thread) |
| 84 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 85 | if (m_tried_unwind_at_non_call_site == false && m_unwind_at_non_call_site_ap.get() == NULL) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 86 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 87 | m_tried_unwind_at_non_call_site = true; |
| 88 | m_unwind_at_non_call_site_ap.reset (new UnwindPlan); |
| 89 | if (!m_assembly_profiler->GetNonCallSiteUnwindPlanFromAssembly (m_range, thread, *m_unwind_at_non_call_site_ap)) |
| 90 | m_unwind_at_non_call_site_ap.reset(); |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 91 | } |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 92 | return m_unwind_at_non_call_site_ap.get(); |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | UnwindPlan* |
| 96 | FuncUnwinders::GetUnwindPlanFastUnwind (Thread& thread) |
| 97 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 98 | if (m_tried_fast_unwind == false && m_fast_unwind_ap.get() == NULL) |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 99 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 100 | m_tried_fast_unwind = true; |
| 101 | m_fast_unwind_ap.reset (new UnwindPlan); |
| 102 | if (!m_assembly_profiler->GetFastUnwindPlan (m_range, thread, *m_fast_unwind_ap)) |
| 103 | m_fast_unwind_ap.reset(); |
Jason Molenda | 8280cbe | 2010-10-25 11:12:07 +0000 | [diff] [blame] | 104 | } |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 105 | return m_fast_unwind_ap.get(); |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | UnwindPlan* |
| 109 | FuncUnwinders::GetUnwindPlanArchitectureDefault (Thread& thread) |
| 110 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 111 | if (m_tried_arch_default_unwind == false && m_arch_default_unwind == NULL) |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 112 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 113 | m_tried_arch_default_unwind = true; |
| 114 | Address current_pc; |
| 115 | Target *target = thread.CalculateTarget(); |
| 116 | if (target) |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 117 | { |
Greg Clayton | e2f9064 | 2011-01-08 00:05:12 +0000 | [diff] [blame^] | 118 | ArchSpec arch = target->GetArchitecture (); |
| 119 | ArchDefaultUnwindPlan *arch_default = ArchDefaultUnwindPlan::FindPlugin (arch); |
| 120 | if (arch_default) |
| 121 | m_arch_default_unwind = arch_default->GetArchDefaultUnwindPlan (thread, current_pc); |
Jason Molenda | 3a4ea24 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | return m_arch_default_unwind; |
| 126 | } |
| 127 | |
| 128 | Address& |
| 129 | FuncUnwinders::GetFirstNonPrologueInsn (Target& target) |
| 130 | { |
| 131 | if (m_first_non_prologue_insn.IsValid()) |
| 132 | return m_first_non_prologue_insn; |
| 133 | m_assembly_profiler->FirstNonPrologueInsn (m_range, target, NULL, m_first_non_prologue_insn); |
| 134 | return m_first_non_prologue_insn; |
| 135 | } |
| 136 | |
| 137 | const Address& |
| 138 | FuncUnwinders::GetFunctionStartAddress () const |
| 139 | { |
| 140 | return m_range.GetBaseAddress(); |
| 141 | } |
| 142 | |