The first part of an lldb native stack unwinder.
The Unwind and RegisterContext subclasses still need
to be finished; none of this code is used by lldb at
this point (unless you call into it by hand).
The ObjectFile class now has an UnwindTable object.
The UnwindTable object has a series of FuncUnwinders
objects (Function Unwinders) -- one for each function
in that ObjectFile we've backtraced through during this
debug session.
The FuncUnwinders object has a few different UnwindPlans.
UnwindPlans are a generic way of describing how to find
the canonical address of a given function's stack frame
(the CFA idea from DWARF/eh_frame) and how to restore the
caller frame's register values, if they have been saved
by this function.
UnwindPlans are created from different sources. One source is the
eh_frame exception handling information generated by the compiler
for unwinding an exception throw. Another source is an assembly
language inspection class (UnwindAssemblyProfiler, uses the Plugin
architecture) which looks at the instructions in the funciton
prologue and describes the stack movements/register saves that are
done.
Two additional types of UnwindPlans that are worth noting are
the "fast" stack UnwindPlan which is useful for making a first
pass over a thread's stack, determining how many stack frames there
are and retrieving the pc and CFA values for each frame (enough
to create StackFrameIDs). Only a minimal set of registers is
recovered during a fast stack walk.
The final UnwindPlan is an architectural default unwind plan.
These are provided by the ArchDefaultUnwindPlan class (which uses
the plugin architecture). When no symbol/function address range can
be found for a given pc value -- when we have no eh_frame information
and when we don't have a start address so we can't examine the assembly
language instrucitons -- we have to make a best guess about how to
unwind. That's when we use the architectural default UnwindPlan.
On x86_64, this would be to assume that rbp is used as a stack pointer
and we can use that to find the caller's frame pointer and pc value.
It's a last-ditch best guess about how to unwind out of a frame.
There are heuristics about when to use one UnwindPlan versues the other --
this will all happen in the still-begin-written UnwindLLDB subclass of
Unwind which runs the UnwindPlans.
git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@113581 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Symbol/UnwindTable.cpp b/source/Symbol/UnwindTable.cpp
new file mode 100644
index 0000000..dcab193
--- /dev/null
+++ b/source/Symbol/UnwindTable.cpp
@@ -0,0 +1,132 @@
+//===-- UnwindTable.cpp ----------------------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Symbol/ObjectFile.h"
+
+#include "lldb/Symbol/FuncUnwinders.h"
+#include "lldb/Symbol/SymbolContext.h"
+#include "lldb/Core/Section.h"
+#include "lldb/Core/Module.h"
+#include "lldb/lldb-forward.h"
+#include "lldb/Utility/UnwindAssemblyProfiler.h"
+#include "lldb/Symbol/DWARFCallFrameInfo.h"
+
+#include "lldb/Symbol/UnwindTable.h"
+#include <stdio.h>
+
+// There is one UnwindTable object per ObjectFile.
+// It contains a list of Unwind objects -- one per function, populated lazily -- for the ObjectFile.
+// Each Unwind object has multiple UnwindPlans for different scenarios.
+
+using namespace lldb;
+using namespace lldb_private;
+
+UnwindTable::UnwindTable (ObjectFile& objfile) : m_object_file(objfile),
+ m_unwinds(),
+ m_initialized(false),
+ m_eh_frame(NULL),
+ m_assembly_profiler(NULL)
+{
+}
+
+// We can't do some of this initialization when the ObjectFile is running its ctor; delay doing it
+// until needed for something.
+
+void
+UnwindTable::initialize ()
+{
+ if (m_initialized)
+ return;
+
+ SectionList* sl = m_object_file.GetSectionList ();
+ if (sl)
+ {
+ SectionSP sect = sl->FindSectionByType (eSectionTypeEHFrame, true);
+ if (sect.get())
+ {
+ m_eh_frame = new DWARFCallFrameInfo(m_object_file, sect, eRegisterKindGCC, true);
+ }
+ }
+
+ ArchSpec arch;
+ ConstString str;
+ m_object_file.GetTargetTriple (str);
+ arch.SetArchFromTargetTriple (str.GetCString());
+ m_assembly_profiler = UnwindAssemblyProfiler::FindPlugin (arch);
+
+ m_initialized = true;
+}
+
+UnwindTable::~UnwindTable ()
+{
+ if (m_eh_frame)
+ delete m_eh_frame;
+}
+
+FuncUnwindersSP
+UnwindTable::GetFuncUnwindersContainingAddress (const Address& addr, SymbolContext &sc)
+{
+ FuncUnwindersSP no_unwind_found;
+
+ initialize();
+
+ // Create a FuncUnwinders object for the binary search below
+ AddressRange search_range(addr, 1);
+ FuncUnwindersSP search_unwind(new FuncUnwinders (*this, NULL, search_range));
+
+ const_iterator idx;
+ idx = std::lower_bound (m_unwinds.begin(), m_unwinds.end(), search_unwind);
+
+ bool found_match = true;
+ if (m_unwinds.size() == 0)
+ {
+ found_match = false;
+ }
+ else if (idx == m_unwinds.end())
+ {
+ --idx;
+ }
+ if (idx != m_unwinds.begin() && (*idx)->GetFunctionStartAddress().GetOffset() != addr.GetOffset())
+ {
+ --idx;
+ }
+ if (found_match && (*idx)->ContainsAddress (addr))
+ {
+ return *idx;
+ }
+
+ AddressRange range;
+ if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, range))
+ {
+ FuncUnwindersSP unw(new FuncUnwinders(*this, m_assembly_profiler, range));
+ m_unwinds.push_back (unw);
+ std::sort (m_unwinds.begin(), m_unwinds.end());
+ return unw;
+ }
+ else
+ {
+ // Does the eh_frame unwind info has a function bounds defined for this addr?
+ if (m_eh_frame->GetAddressRange (addr, range))
+ {
+ FuncUnwindersSP unw(new FuncUnwinders(*this, m_assembly_profiler, range));
+ m_unwinds.push_back (unw);
+ std::sort (m_unwinds.begin(), m_unwinds.end());
+ return unw;
+ // FIXME we should create a syntheic Symbol based on the address range with a synthesized symbol name
+ }
+ }
+ return no_unwind_found;
+}
+
+DWARFCallFrameInfo *
+UnwindTable::GetEHFrameInfo ()
+{
+ initialize();
+ return m_eh_frame;
+}