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