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/Target/UnwindAssembly.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),
33 m_assembly_profiler (NULL),
38 // We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it
39 // until needed for something.
42 UnwindTable::Initialize ()
47 SectionList* sl = m_object_file.GetSectionList ();
50 SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true);
53 m_eh_frame = new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindGCC, true);
58 if (m_object_file.GetArchitecture (arch))
60 m_assembly_profiler = UnwindAssembly::FindPlugin (arch);
65 UnwindTable::~UnwindTable ()
72 UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc)
74 FuncUnwindersSP no_unwind_found;
78 // There is an UnwindTable per object file, so we can safely use file handles
79 addr_t file_addr = addr.GetFileAddress();
80 iterator end = m_unwinds.end ();
81 iterator insert_pos = end;
82 if (!m_unwinds.empty())
84 insert_pos = m_unwinds.lower_bound (file_addr);
85 iterator pos = insert_pos;
86 if ((pos == m_unwinds.end ()) || (pos != m_unwinds.begin() && pos->second->GetFunctionStartAddress() != addr))
89 if (pos->second->ContainsAddress (addr))
94 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid())
96 // Does the eh_frame unwind info has a function bounds for this addr?
97 if (m_eh_frame == NULL || !m_eh_frame->GetAddressRange (addr, range))
99 return no_unwind_found;
103 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, m_assembly_profiler, range));
104 m_unwinds.insert (insert_pos, std::make_pair(range.GetBaseAddress().GetFileAddress(), func_unwinder_sp));
105 // StreamFile s(stdout);
107 return func_unwinder_sp;
110 // Ignore any existing FuncUnwinders for this function, create a new one and don't add it to the
111 // UnwindTable. This is intended for use by target modules show-unwind where we want to create
112 // new UnwindPlans, not re-use existing ones.
115 UnwindTable::GetUncachedFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc)
117 FuncUnwindersSP no_unwind_found;
121 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, false, range) || !range.GetBaseAddress().IsValid())
123 // Does the eh_frame unwind info has a function bounds for this addr?
124 if (m_eh_frame == NULL || !m_eh_frame->GetAddressRange (addr, range))
126 return no_unwind_found;
130 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, m_assembly_profiler, range));
131 return func_unwinder_sp;
136 UnwindTable::Dump (Stream &s)
138 s.Printf("UnwindTable for '%s':\n", m_object_file.GetFileSpec().GetPath().c_str());
139 const_iterator begin = m_unwinds.begin();
140 const_iterator end = m_unwinds.end();
141 for (const_iterator pos = begin; pos != end; ++pos)
143 s.Printf ("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance (begin, pos), pos->first);
149 UnwindTable::GetEHFrameInfo ()