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 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 28 UnwindTable::UnwindTable (ObjectFile& objfile) : 29 m_object_file (objfile), 30 m_unwinds (), 31 m_initialized (false), 32 m_eh_frame (nullptr) 33 { 34 } 35 36 // We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it 37 // until needed for something. 38 39 void 40 UnwindTable::Initialize () 41 { 42 if (m_initialized) 43 return; 44 45 SectionList* sl = m_object_file.GetSectionList (); 46 if (sl) 47 { 48 SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true); 49 if (sect.get()) 50 { 51 m_eh_frame = new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindGCC, true); 52 } 53 } 54 55 m_initialized = true; 56 } 57 58 UnwindTable::~UnwindTable () 59 { 60 if (m_eh_frame) 61 delete m_eh_frame; 62 } 63 64 FuncUnwindersSP 65 UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) 66 { 67 FuncUnwindersSP no_unwind_found; 68 69 Initialize(); 70 71 // There is an UnwindTable per object file, so we can safely use file handles 72 addr_t file_addr = addr.GetFileAddress(); 73 iterator end = m_unwinds.end (); 74 iterator insert_pos = end; 75 if (!m_unwinds.empty()) 76 { 77 insert_pos = m_unwinds.lower_bound (file_addr); 78 iterator pos = insert_pos; 79 if ((pos == m_unwinds.end ()) || (pos != m_unwinds.begin() && pos->second->GetFunctionStartAddress() != addr)) 80 --pos; 81 82 if (pos->second->ContainsAddress (addr)) 83 return pos->second; 84 } 85 86 AddressRange range; 87 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) 88 { 89 // Does the eh_frame unwind info has a function bounds for this addr? 90 if (m_eh_frame == nullptr || !m_eh_frame->GetAddressRange (addr, range)) 91 { 92 return no_unwind_found; 93 } 94 } 95 96 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 97 m_unwinds.insert (insert_pos, std::make_pair(range.GetBaseAddress().GetFileAddress(), func_unwinder_sp)); 98 // StreamFile s(stdout, false); 99 // Dump (s); 100 return func_unwinder_sp; 101 } 102 103 // Ignore any existing FuncUnwinders for this function, create a new one and don't add it to the 104 // UnwindTable. This is intended for use by target modules show-unwind where we want to create 105 // new UnwindPlans, not re-use existing ones. 106 107 FuncUnwindersSP 108 UnwindTable::GetUncachedFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc) 109 { 110 FuncUnwindersSP no_unwind_found; 111 Initialize(); 112 113 AddressRange range; 114 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid()) 115 { 116 // Does the eh_frame unwind info has a function bounds for this addr? 117 if (m_eh_frame == nullptr || !m_eh_frame->GetAddressRange (addr, range)) 118 { 119 return no_unwind_found; 120 } 121 } 122 123 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range)); 124 return func_unwinder_sp; 125 } 126 127 128 void 129 UnwindTable::Dump (Stream &s) 130 { 131 s.Printf("UnwindTable for '%s':\n", m_object_file.GetFileSpec().GetPath().c_str()); 132 const_iterator begin = m_unwinds.begin(); 133 const_iterator end = m_unwinds.end(); 134 for (const_iterator pos = begin; pos != end; ++pos) 135 { 136 s.Printf ("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance (begin, pos), pos->first); 137 } 138 s.EOL(); 139 } 140 141 DWARFCallFrameInfo * 142 UnwindTable::GetEHFrameInfo () 143 { 144 Initialize(); 145 return m_eh_frame; 146 } 147 148 bool 149 UnwindTable::GetArchitecture (lldb_private::ArchSpec &arch) 150 { 151 return m_object_file.GetArchitecture (arch); 152 } 153