| Greg Clayton | dc5eb69 | 2011-04-25 18:36:36 +0000 | [diff] [blame] | 1 | //===-- UnwindTable.cpp -----------------------------------------*- C++ -*-===// |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 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 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 10 | #include "lldb/Symbol/UnwindTable.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 11 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | |
| Greg Clayton | a51ed9b | 2010-09-23 01:09:21 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/Section.h" |
| 16 | #include "lldb/Symbol/ObjectFile.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/FuncUnwinders.h" |
| 18 | #include "lldb/Symbol/SymbolContext.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 20 | |
| 21 | // There is one UnwindTable object per ObjectFile. |
| 22 | // It contains a list of Unwind objects -- one per function, populated lazily -- for the ObjectFile. |
| 23 | // Each Unwind object has multiple UnwindPlans for different scenarios. |
| 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 28 | UnwindTable::UnwindTable (ObjectFile& objfile) : |
| 29 | m_object_file (objfile), |
| 30 | m_unwinds (), |
| 31 | m_initialized (false), |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame^] | 32 | m_mutex (), |
| Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 33 | m_eh_frame (nullptr) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 34 | { |
| 35 | } |
| 36 | |
| 37 | // We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it |
| 38 | // until needed for something. |
| 39 | |
| 40 | void |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 41 | UnwindTable::Initialize () |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 42 | { |
| 43 | if (m_initialized) |
| 44 | return; |
| 45 | |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame^] | 46 | Mutex::Locker locker(m_mutex); |
| 47 | |
| 48 | if (m_initialized) // check again once we've acquired the lock |
| 49 | return; |
| 50 | |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 51 | SectionList* sl = m_object_file.GetSectionList (); |
| 52 | if (sl) |
| 53 | { |
| 54 | SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true); |
| 55 | if (sect.get()) |
| 56 | { |
| 57 | m_eh_frame = new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindGCC, true); |
| 58 | } |
| 59 | } |
| 60 | |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 61 | m_initialized = true; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | UnwindTable::~UnwindTable () |
| 65 | { |
| 66 | if (m_eh_frame) |
| 67 | delete m_eh_frame; |
| 68 | } |
| 69 | |
| 70 | FuncUnwindersSP |
| 71 | UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) |
| 72 | { |
| 73 | FuncUnwindersSP no_unwind_found; |
| 74 | |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 75 | Initialize(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 76 | |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame^] | 77 | Mutex::Locker locker(m_mutex); |
| 78 | |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 79 | // There is an UnwindTable per object file, so we can safely use file handles |
| 80 | addr_t file_addr = addr.GetFileAddress(); |
| 81 | iterator end = m_unwinds.end (); |
| 82 | iterator insert_pos = end; |
| 83 | if (!m_unwinds.empty()) |
| 84 | { |
| 85 | insert_pos = m_unwinds.lower_bound (file_addr); |
| 86 | iterator pos = insert_pos; |
| 87 | if ((pos == m_unwinds.end ()) || (pos != m_unwinds.begin() && pos->second->GetFunctionStartAddress() != addr)) |
| 88 | --pos; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 89 | |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 90 | if (pos->second->ContainsAddress (addr)) |
| 91 | return pos->second; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | AddressRange range; |
| Greg Clayton | 7e14f91 | 2011-04-23 02:04:55 +0000 | [diff] [blame] | 95 | if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 96 | { |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 97 | // Does the eh_frame unwind info has a function bounds for this addr? |
| Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 98 | if (m_eh_frame == nullptr || !m_eh_frame->GetAddressRange (addr, range)) |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 99 | { |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 100 | return no_unwind_found; |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| Jason Molenda | 5976200 | 2010-11-04 00:53:20 +0000 | [diff] [blame] | 103 | |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 104 | FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 105 | m_unwinds.insert (insert_pos, std::make_pair(range.GetBaseAddress().GetFileAddress(), func_unwinder_sp)); |
| Michael Sartain | 762df1f | 2013-10-10 00:16:10 +0000 | [diff] [blame] | 106 | // StreamFile s(stdout, false); |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 107 | // Dump (s); |
| 108 | return func_unwinder_sp; |
| 109 | } |
| 110 | |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 111 | // Ignore any existing FuncUnwinders for this function, create a new one and don't add it to the |
| 112 | // UnwindTable. This is intended for use by target modules show-unwind where we want to create |
| 113 | // new UnwindPlans, not re-use existing ones. |
| 114 | |
| 115 | FuncUnwindersSP |
| 116 | UnwindTable::GetUncachedFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) |
| 117 | { |
| 118 | FuncUnwindersSP no_unwind_found; |
| 119 | Initialize(); |
| 120 | |
| 121 | AddressRange range; |
| 122 | if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) |
| 123 | { |
| 124 | // Does the eh_frame unwind info has a function bounds for this addr? |
| Ed Maste | d4612ad | 2014-04-20 13:17:36 +0000 | [diff] [blame] | 125 | if (m_eh_frame == nullptr || !m_eh_frame->GetAddressRange (addr, range)) |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 126 | { |
| 127 | return no_unwind_found; |
| 128 | } |
| 129 | } |
| 130 | |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 131 | FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); |
| Jason Molenda | 380241a | 2012-07-12 00:20:07 +0000 | [diff] [blame] | 132 | return func_unwinder_sp; |
| 133 | } |
| 134 | |
| 135 | |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 136 | void |
| 137 | UnwindTable::Dump (Stream &s) |
| 138 | { |
| Jason Molenda | 5cba569 | 2014-06-18 23:32:53 +0000 | [diff] [blame^] | 139 | Mutex::Locker locker(m_mutex); |
| Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 140 | s.Printf("UnwindTable for '%s':\n", m_object_file.GetFileSpec().GetPath().c_str()); |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 141 | const_iterator begin = m_unwinds.begin(); |
| 142 | const_iterator end = m_unwinds.end(); |
| 143 | for (const_iterator pos = begin; pos != end; ++pos) |
| 144 | { |
| Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 145 | s.Printf ("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance (begin, pos), pos->first); |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 146 | } |
| 147 | s.EOL(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | DWARFCallFrameInfo * |
| 151 | UnwindTable::GetEHFrameInfo () |
| 152 | { |
| Greg Clayton | b0848c5 | 2011-01-08 00:05:12 +0000 | [diff] [blame] | 153 | Initialize(); |
| Jason Molenda | fbcb7f2 | 2010-09-10 07:49:16 +0000 | [diff] [blame] | 154 | return m_eh_frame; |
| 155 | } |
| Jason Molenda | ab35aa9 | 2014-05-23 01:48:10 +0000 | [diff] [blame] | 156 | |
| 157 | bool |
| 158 | UnwindTable::GetArchitecture (lldb_private::ArchSpec &arch) |
| 159 | { |
| 160 | return m_object_file.GetArchitecture (arch); |
| 161 | } |