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