LLDB mainline
UnwindTable.cpp
Go to the documentation of this file.
1//===-- UnwindTable.cpp ---------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include <cstdio>
12#include <optional>
13
14#include "lldb/Core/Module.h"
15#include "lldb/Core/Section.h"
24
25// There is one UnwindTable object per ObjectFile. It contains a list of Unwind
26// objects -- one per function, populated lazily -- for the ObjectFile. Each
27// Unwind object has multiple UnwindPlans for different scenarios.
28
29using namespace lldb;
30using namespace lldb_private;
31
33 : m_module(module), m_unwinds(), m_initialized(false), m_mutex(),
34 m_object_file_unwind_up(), m_eh_frame_up(), m_compact_unwind_up(),
35 m_arm_unwind_up() {}
36
37// We can't do some of this initialization when the ObjectFile is running its
38// ctor; delay doing it until needed for something.
39
41 if (m_initialized)
42 return;
43
44 std::lock_guard<std::mutex> guard(m_mutex);
45
46 if (m_initialized) // check again once we've acquired the lock
47 return;
48 m_initialized = true;
49 ObjectFile *object_file = m_module.GetObjectFile();
50 if (!object_file)
51 return;
52
54
56 if (!sl)
57 return;
58
60 if (sect.get()) {
61 m_eh_frame_up = std::make_unique<DWARFCallFrameInfo>(
62 *object_file, sect, DWARFCallFrameInfo::EH);
63 }
64
66 if (sect) {
67 m_debug_frame_up = std::make_unique<DWARFCallFrameInfo>(
68 *object_file, sect, DWARFCallFrameInfo::DWARF);
69 }
70
72 if (sect) {
74 std::make_unique<CompactUnwindInfo>(*object_file, sect);
75 }
76
78 if (sect) {
79 SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
80 if (sect_extab.get()) {
82 std::make_unique<ArmUnwindInfo>(*object_file, sect, sect_extab);
83 }
84 }
85}
86
88 if (!m_initialized)
89 return Initialize();
90
91 std::lock_guard<std::mutex> guard(m_mutex);
92
93 ObjectFile *object_file = m_module.GetObjectFile();
94 if (!object_file)
95 return;
96
99
101 if (!sl)
102 return;
103
105 if (!m_eh_frame_up && sect) {
106 m_eh_frame_up = std::make_unique<DWARFCallFrameInfo>(
107 *object_file, sect, DWARFCallFrameInfo::EH);
108 }
109
111 if (!m_debug_frame_up && sect) {
112 m_debug_frame_up = std::make_unique<DWARFCallFrameInfo>(
113 *object_file, sect, DWARFCallFrameInfo::DWARF);
114 }
115
117 if (!m_compact_unwind_up && sect) {
119 std::make_unique<CompactUnwindInfo>(*object_file, sect);
120 }
121
122 sect = sl->FindSectionByType(eSectionTypeARMexidx, true);
123 if (!m_arm_unwind_up && sect) {
124 SectionSP sect_extab = sl->FindSectionByType(eSectionTypeARMextab, true);
125 if (sect_extab.get()) {
127 std::make_unique<ArmUnwindInfo>(*object_file, sect, sect_extab);
128 }
129 }
130}
131
132UnwindTable::~UnwindTable() = default;
133
134std::optional<AddressRange>
136 AddressRange range;
137
138 // First check the unwind info from the object file plugin
140 m_object_file_unwind_up->GetAddressRange(addr, range))
141 return range;
142
143 // Check the symbol context
144 if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0,
145 false, range) &&
146 range.GetBaseAddress().IsValid())
147 return range;
148
149 // Does the eh_frame unwind info has a function bounds for this addr?
150 if (m_eh_frame_up && m_eh_frame_up->GetAddressRange(addr, range))
151 return range;
152
153 // Try debug_frame as well
154 if (m_debug_frame_up && m_debug_frame_up->GetAddressRange(addr, range))
155 return range;
156
157 return std::nullopt;
158}
159
162 SymbolContext &sc) {
163 Initialize();
164
165 std::lock_guard<std::mutex> guard(m_mutex);
166
167 // There is an UnwindTable per object file, so we can safely use file handles
168 addr_t file_addr = addr.GetFileAddress();
169 iterator end = m_unwinds.end();
170 iterator insert_pos = end;
171 if (!m_unwinds.empty()) {
172 insert_pos = m_unwinds.lower_bound(file_addr);
173 iterator pos = insert_pos;
174 if ((pos == m_unwinds.end()) ||
175 (pos != m_unwinds.begin() &&
176 pos->second->GetFunctionStartAddress() != addr))
177 --pos;
178
179 if (pos->second->ContainsAddress(addr))
180 return pos->second;
181 }
182
183 auto range_or = GetAddressRange(addr, sc);
184 if (!range_or)
185 return nullptr;
186
187 FuncUnwindersSP func_unwinder_sp(new FuncUnwinders(*this, *range_or));
188 m_unwinds.insert(insert_pos,
189 std::make_pair(range_or->GetBaseAddress().GetFileAddress(),
190 func_unwinder_sp));
191 return func_unwinder_sp;
192}
193
194// Ignore any existing FuncUnwinders for this function, create a new one and
195// don't add it to the UnwindTable. This is intended for use by target modules
196// show-unwind where we want to create new UnwindPlans, not re-use existing
197// ones.
199 const Address &addr, const SymbolContext &sc) {
200 Initialize();
201
202 auto range_or = GetAddressRange(addr, sc);
203 if (!range_or)
204 return nullptr;
205
206 return std::make_shared<FuncUnwinders>(*this, *range_or);
207}
208
210 std::lock_guard<std::mutex> guard(m_mutex);
211 s.Format("UnwindTable for '{0}':\n", m_module.GetFileSpec());
212 const_iterator begin = m_unwinds.begin();
213 const_iterator end = m_unwinds.end();
214 for (const_iterator pos = begin; pos != end; ++pos) {
215 s.Printf("[%u] 0x%16.16" PRIx64 "\n", (unsigned)std::distance(begin, pos),
216 pos->first);
217 }
218 s.EOL();
219}
220
222 Initialize();
223 return m_object_file_unwind_up.get();
224}
225
227 Initialize();
228 return m_eh_frame_up.get();
229}
230
232 Initialize();
233 return m_debug_frame_up.get();
234}
235
237 Initialize();
238 return m_compact_unwind_up.get();
239}
240
242 Initialize();
243 return m_arm_unwind_up.get();
244}
245
247
249
251 if (ObjectFile *object_file = m_module.GetObjectFile())
252 return object_file->AllowAssemblyEmulationUnwindPlans();
253 return false;
254}
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
An architecture specification class.
Definition: ArchSpec.h:31
A class that describes an executable image and its associated object and symbol files.
Definition: Module.h:88
virtual ObjectFile * GetObjectFile()
Get the object file representation for the current architecture.
Definition: Module.cpp:1184
virtual SymbolFile * GetSymbolFile(bool can_create=true, Stream *feedback_strm=nullptr)
Get the module's symbol file.
Definition: Module.cpp:992
const ArchSpec & GetArchitecture() const
Get const accessor for the module architecture.
Definition: Module.cpp:1035
virtual SectionList * GetSectionList()
Get the unified section list for the module.
Definition: Module.cpp:1224
const FileSpec & GetFileSpec() const
Get const accessor for the module file specification.
Definition: Module.h:452
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
virtual std::unique_ptr< CallFrameInfo > CreateCallFrameInfo()
Creates a plugin-specific call frame info.
Definition: ObjectFile.cpp:653
lldb::SectionSP FindSectionByType(lldb::SectionType sect_type, bool check_children, size_t start_idx=0) const
Definition: Section.cpp:592
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Format(const char *format, Args &&... args)
Definition: Stream.h:353
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:155
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
bool GetAddressRange(uint32_t scope, uint32_t range_idx, bool use_inline_block_range, AddressRange &range) const
Get the address range contained within a symbol context.
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:50
std::optional< AddressRange > GetAddressRange(const Address &addr, const SymbolContext &sc)
lldb_private::DWARFCallFrameInfo * GetEHFrameInfo()
std::unique_ptr< DWARFCallFrameInfo > m_debug_frame_up
Definition: UnwindTable.h:83
std::unique_ptr< CallFrameInfo > m_object_file_unwind_up
Definition: UnwindTable.h:81
bool GetAllowAssemblyEmulationUnwindPlans()
void Update()
Called after a SymbolFile has been added to a Module to add any new unwind sections that may now be a...
Definition: UnwindTable.cpp:87
ArmUnwindInfo * GetArmUnwindInfo()
std::unique_ptr< DWARFCallFrameInfo > m_eh_frame_up
Definition: UnwindTable.h:82
UnwindTable(Module &module)
Create an Unwind table using the data in the given module.
Definition: UnwindTable.cpp:32
std::unique_ptr< CompactUnwindInfo > m_compact_unwind_up
Definition: UnwindTable.h:84
SymbolFile * GetSymbolFile()
collection::const_iterator const_iterator
Definition: UnwindTable.h:73
lldb::FuncUnwindersSP GetFuncUnwindersContainingAddress(const Address &addr, SymbolContext &sc)
lldb_private::DWARFCallFrameInfo * GetDebugFrameInfo()
lldb::FuncUnwindersSP GetUncachedFuncUnwindersContainingAddress(const Address &addr, const SymbolContext &sc)
lldb_private::CompactUnwindInfo * GetCompactUnwindInfo()
lldb_private::CallFrameInfo * GetObjectFileUnwindInfo()
collection::iterator iterator
Definition: UnwindTable.h:72
std::unique_ptr< ArmUnwindInfo > m_arm_unwind_up
Definition: UnwindTable.h:85
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::FuncUnwinders > FuncUnwindersSP
Definition: lldb-forward.h:348
std::shared_ptr< lldb_private::Section > SectionSP
Definition: lldb-forward.h:406
uint64_t addr_t
Definition: lldb-types.h:79
@ eSectionTypeDWARFDebugFrame
@ eSectionTypeARMextab
@ eSectionTypeCompactUnwind
compact unwind section in Mach-O, __TEXT,__unwind_info
@ eSectionTypeEHFrame
@ eSectionTypeARMexidx