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/ArmUnwindInfo.h"
17 #include "lldb/Symbol/CompactUnwindInfo.h"
18 #include "lldb/Symbol/DWARFCallFrameInfo.h"
19 #include "lldb/Symbol/FuncUnwinders.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "lldb/Symbol/SymbolContext.h"
23 // There is one UnwindTable object per ObjectFile. It contains a list of Unwind
24 // objects -- one per function, populated lazily -- for the ObjectFile. Each
25 // Unwind object has multiple UnwindPlans for different scenarios.
28 using namespace lldb_private;
30 UnwindTable::UnwindTable(ObjectFile &objfile)
31 : m_object_file(objfile), m_unwinds(), m_initialized(false), m_mutex(),
32 m_eh_frame_up(), m_compact_unwind_up(), m_arm_unwind_up() {}
34 // We can't do some of this initialization when the ObjectFile is running its
35 // ctor; delay doing it until needed for something.
37 void UnwindTable::Initialize() {
41 std::lock_guard<std::mutex> guard(m_mutex);
43 if (m_initialized) // check again once we've acquired the lock
47 SectionList *sl = m_object_file.GetSectionList();
51 SectionSP sect = sl->FindSectionByType(eSectionTypeEHFrame, true);
54 new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::EH));
57 sect = sl->FindSectionByType(eSectionTypeDWARFDebugFrame, true);
59 m_debug_frame_up.reset(
60 new DWARFCallFrameInfo(m_object_file, sect, DWARFCallFrameInfo::DWARF));
63 sect = sl->FindSectionByType(eSectionTypeCompactUnwind, true);
65 m_compact_unwind_up.reset(new CompactUnwindInfo(m_object_file, sect));
68 sect = sl->FindSectionByType(eSectionTypeARMexidx, true);
70 SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
71 if (sect_extab.get()) {
72 m_arm_unwind_up.reset(new ArmUnwindInfo(m_object_file, sect, sect_extab));
77 UnwindTable::~UnwindTable() {}
79 llvm::Optional<AddressRange> UnwindTable::GetAddressRange(const Address &addr,
83 // First check the symbol context
84 if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
86 range.GetBaseAddress().IsValid())
89 // Does the eh_frame unwind info has a function bounds for this addr?
90 if (m_eh_frame_up && m_eh_frame_up->GetAddressRange(addr, range))
93 // Try debug_frame as well
94 if (m_debug_frame_up && m_debug_frame_up->GetAddressRange(addr, range))
101 UnwindTable::GetFuncUnwindersContainingAddress(const Address &addr,
105 std::lock_guard<std::mutex> guard(m_mutex);
107 // There is an UnwindTable per object file, so we can safely use file handles
108 addr_t file_addr = addr.GetFileAddress();
109 iterator end = m_unwinds.end();
110 iterator insert_pos = end;
111 if (!m_unwinds.empty()) {
112 insert_pos = m_unwinds.lower_bound(file_addr);
113 iterator pos = insert_pos;
114 if ((pos == m_unwinds.end()) ||
115 (pos != m_unwinds.begin() &&
116 pos->second->GetFunctionStartAddress() != addr))
119 if (pos->second->ContainsAddress(addr))
123 auto range_or = GetAddressRange(addr, sc);
127 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or));
128 m_unwinds.insert(insert_pos,
129 std::make_pair(range_or->GetBaseAddress().GetFileAddress(),
131 return func_unwinder_sp;
134 // Ignore any existing FuncUnwinders for this function, create a new one and
135 // don't add it to the UnwindTable. This is intended for use by target modules
136 // show-unwind where we want to create new UnwindPlans, not re-use existing
139 UnwindTable::GetUncachedFuncUnwindersContainingAddress(const Address &addr,
143 auto range_or = GetAddressRange(addr, sc);
147 return std::make_shared<FuncUnwinders>(*this, *range_or);
150 void UnwindTable::Dump(Stream &s) {
151 std::lock_guard<std::mutex> guard(m_mutex);
152 s.Printf("UnwindTable for '%s':\n",
153 m_object_file.GetFileSpec().GetPath().c_str());
154 const_iterator begin = m_unwinds.begin();
155 const_iterator end = m_unwinds.end();
156 for (const_iterator pos = begin; pos != end; ++pos) {
157 s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos),
163 DWARFCallFrameInfo *UnwindTable::GetEHFrameInfo() {
165 return m_eh_frame_up.get();
168 DWARFCallFrameInfo *UnwindTable::GetDebugFrameInfo() {
170 return m_debug_frame_up.get();
173 CompactUnwindInfo *UnwindTable::GetCompactUnwindInfo() {
175 return m_compact_unwind_up.get();
178 ArmUnwindInfo *UnwindTable::GetArmUnwindInfo() {
180 return m_arm_unwind_up.get();
183 bool UnwindTable::GetArchitecture(lldb_private::ArchSpec &arch) {
184 return m_object_file.GetArchitecture(arch);
187 bool UnwindTable::GetAllowAssemblyEmulationUnwindPlans() {
188 return m_object_file.AllowAssemblyEmulationUnwindPlans();