1 //===-- UnwindTable.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/UnwindTable.h"
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"
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.
27 using namespace lldb_private;
29 UnwindTable::UnwindTable (ObjectFile& objfile) :
30 m_object_file (objfile),
32 m_initialized (false),
35 m_compact_unwind (nullptr)
39 // We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it
40 // until needed for something.
43 UnwindTable::Initialize ()
48 Mutex::Locker locker(m_mutex);
50 if (m_initialized) // check again once we've acquired the lock
53 SectionList* sl = m_object_file.GetSectionList ();
56 SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true);
59 m_eh_frame = new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindGCC, true);
61 sect = sl->FindSectionByType (eSectionTypeCompactUnwind, true);
64 m_compact_unwind = new CompactUnwindInfo(m_object_file, sect);
71 UnwindTable::~UnwindTable ()
78 UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc)
80 FuncUnwindersSP no_unwind_found;
84 Mutex::Locker locker(m_mutex);
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())
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))
97 if (pos->second->ContainsAddress (addr))
102 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid())
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))
107 return no_unwind_found;
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);
115 return func_unwinder_sp;
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.
123 UnwindTable::GetUncachedFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc)
125 FuncUnwindersSP no_unwind_found;
129 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid())
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))
134 return no_unwind_found;
138 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, range));
139 return func_unwinder_sp;
144 UnwindTable::Dump (Stream &s)
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)
152 s.Printf ("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance (begin, pos), pos->first);
158 UnwindTable::GetEHFrameInfo ()
165 UnwindTable::GetCompactUnwindInfo ()
168 return m_compact_unwind;
172 UnwindTable::GetArchitecture (lldb_private::ArchSpec &arch)
174 return m_object_file.GetArchitecture (arch);