LLDB mainline
SymbolFileSymtab.cpp
Go to the documentation of this file.
1//===-- SymbolFileSymtab.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
9#include "SymbolFileSymtab.h"
10
11#include "lldb/Core/Module.h"
16#include "lldb/Symbol/Symbol.h"
18#include "lldb/Symbol/Symtab.h"
21#include "lldb/Utility/Timer.h"
22
23#include <memory>
24#include <optional>
25
26using namespace lldb;
27using namespace lldb_private;
28
30
32
37
41
43 return "Reads debug symbols from an object file's symbol table.";
44}
45
47 return new SymbolFileSymtab(std::move(objfile_sp));
48}
49
51 TypeClass type_mask,
52 lldb_private::TypeList &type_list) {}
53
57
59 uint32_t abilities = 0;
60 if (m_objfile_sp) {
61 const Symtab *symtab = m_objfile_sp->GetSymtab();
62 if (symtab) {
63 if (symtab->GetNumSymbols() > 0)
64 abilities |= Symbols;
65
66 // Get the indexes of source, code, data, and function-related entries in
67 // the module symbol table. Only source-file entries provide a genuine
68 // debug-info ability. Code and data entries remain available as symbols
69 // but are not equivalent to debug-info functions or global variables.
72 abilities |= CompileUnits;
73 }
74
79 }
80
85 }
86
90 }
91
94 objc_class_indexes)) {
95 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
98 }
99 }
100 }
101 return abilities;
102}
103
105 // If we don't have any source file symbols we will just have one compile
106 // unit for the entire object file
107 if (m_source_indexes.empty())
108 return 0;
109
110 // If we have any source file symbols we will logically organize the object
111 // symbols using these.
112 return m_source_indexes.size();
113}
114
116 CompUnitSP cu_sp;
117
118 // If we don't have any source file symbols we will just have one compile
119 // unit for the entire object file
120 if (idx < m_source_indexes.size()) {
121 const Symbol *cu_symbol =
122 m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
123 if (cu_symbol)
124 cu_sp =
125 std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
126 cu_symbol->GetName().AsCString(nullptr),
128 }
129 return cu_sp;
130}
131
135
137 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
138 size_t num_added = 0;
139 // We must at least have a valid compile unit
140 const Symtab *symtab = m_objfile_sp->GetSymtab();
141 const Symbol *curr_symbol = nullptr;
142 const Symbol *next_symbol = nullptr;
143 // const char *prefix = m_objfile_sp->SymbolPrefix();
144 // if (prefix == NULL)
145 // prefix == "";
146 //
147 // const uint32_t prefix_len = strlen(prefix);
148
149 // If we don't have any source file symbols we will just have one compile
150 // unit for the entire object file
151 if (m_source_indexes.empty()) {
152 // The only time we will have a user ID of zero is when we don't have and
153 // source file symbols and we declare one compile unit for the entire
154 // object file
155 if (!m_func_indexes.empty()) {
156 }
157
158 if (!m_code_indexes.empty()) {
159 // StreamFile s(stdout);
160 // symtab->Dump(&s, m_code_indexes);
161
162 uint32_t idx = 0; // Index into the indexes
163 const uint32_t num_indexes = m_code_indexes.size();
164 for (idx = 0; idx < num_indexes; ++idx) {
165 uint32_t symbol_idx = m_code_indexes[idx];
166 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
167 if (curr_symbol) {
168 // Union of all ranges in the function DIE (if the function is
169 // discontiguous)
170 AddressRange func_range(curr_symbol->GetAddress(), 0);
171 if (func_range.GetBaseAddress().IsSectionOffset()) {
172 uint32_t symbol_size = curr_symbol->GetByteSize();
173 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
174 func_range.SetByteSize(symbol_size);
175 else if (idx + 1 < num_indexes) {
176 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
177 if (next_symbol) {
178 func_range.SetByteSize(
179 next_symbol->GetAddressRef().GetOffset() -
180 curr_symbol->GetAddressRef().GetOffset());
181 }
182 }
183
184 FunctionSP func_sp(new Function(
185 &comp_unit,
186 symbol_idx, // UserID is the DIE offset
187 LLDB_INVALID_UID, // We don't have any type info
188 // for this function
189 curr_symbol->GetMangled(), // Linker/mangled name
190 nullptr, // no return type for a code symbol...
191 curr_symbol->GetAddress(), AddressRanges{func_range}));
192
193 if (func_sp.get() != nullptr) {
194 comp_unit.AddFunction(func_sp);
195 ++num_added;
196 }
197 }
198 }
199 }
200 }
201 } else {
202 // We assume we
203 }
204 return num_added;
205}
206
207size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
208
209bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
210
212 return false;
213}
214
216 SupportFileList &support_files) {
217 return false;
218}
219
221 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
222 return false;
223}
224
226
228 return 0;
229}
230
232 return nullptr;
233}
234
235std::optional<SymbolFile::ArrayInfo>
237 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
238 return std::nullopt;
239}
240
242 return false;
243}
244
246 SymbolContextItem resolve_scope,
247 SymbolContext &sc) {
248 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
249 if (m_objfile_sp->GetSymtab() == nullptr)
250 return 0;
251
252 uint32_t resolved_flags = 0;
253 if (resolve_scope & eSymbolContextSymbol) {
254 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
255 so_addr.GetFileAddress());
256 if (sc.symbol)
257 resolved_flags |= eSymbolContextSymbol;
258 }
259 return resolved_flags;
260}
#define LLDB_PLUGIN_DEFINE(PluginName)
lldb_private::Symtab::IndexCollection m_data_indexes
bool CompleteType(lldb_private::CompilerType &compiler_type) override
lldb_private::Symtab::IndexCollection m_source_indexes
uint32_t CalculateAbilities() override
uint32_t CalculateNumCompileUnits() override
std::optional< ArrayInfo > GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) override
If type_uid points to an array type, return its characteristics.
void GetTypes(lldb_private::SymbolContextScope *sc_scope, lldb::TypeClass type_mask, lldb_private::TypeList &type_list) override
static void Initialize()
lldb_private::Symtab::IndexCollection m_func_indexes
static char ID
LLVM RTTI support.
bool ParseImportedModules(const lldb_private::SymbolContext &sc, std::vector< lldb_private::SourceModule > &imported_modules) override
lldb_private::Symtab::IndexCollection m_code_indexes
size_t ParseBlocksRecursive(lldb_private::Function &func) override
lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override
size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override
bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override
static void Terminate()
bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override
size_t ParseVariablesForContext(const lldb_private::SymbolContext &sc) override
static llvm::StringRef GetPluginDescriptionStatic()
SymbolFileSymtab(lldb::ObjectFileSP objfile_sp)
lldb_private::Symtab::NameToIndexMap m_objc_class_name_to_index
lldb_private::Type * ResolveTypeUID(lldb::user_id_t type_uid) override
uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr, lldb::SymbolContextItem resolve_scope, lldb_private::SymbolContext &sc) override
static lldb_private::SymbolFile * CreateInstance(lldb::ObjectFileSP objfile_sp)
bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit, lldb_private::SupportFileList &support_files) override
static llvm::StringRef GetPluginNameStatic()
lldb::LanguageType ParseLanguage(lldb_private::CompileUnit &comp_unit) override
size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override
A section + offset based address range class.
Address & GetBaseAddress()
Get accessor for the base address of the range.
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetFileAddress() const
Get the file address.
Definition Address.cpp:283
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition Address.h:329
bool IsSectionOffset() const
Check if an address is section offset.
Definition Address.h:342
A class that describes a compilation unit.
Definition CompileUnit.h:43
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
Generic representation of a type in a programming language.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that describes a function.
Definition Function.h:386
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A list of support files for a CompileUnit.
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
Defines a symbol context baton that can be handed other debug core functions.
Symbol * symbol
The Symbol for a given query.
lldb::ObjectFileSP m_objfile_sp
Definition SymbolFile.h:671
SymbolFileCommon(lldb::ObjectFileSP objfile_sp)
Definition SymbolFile.h:580
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
Mangled & GetMangled()
Definition Symbol.h:162
Address & GetAddressRef()
Definition Symbol.h:78
bool GetSizeIsSibling() const
Definition Symbol.h:246
lldb::addr_t GetByteSize() const
Definition Symbol.cpp:469
ConstString GetName() const
Definition Symbol.cpp:612
Address GetAddress() const
Definition Symbol.h:98
Symbol * SymbolAtIndex(size_t idx)
Definition Symtab.cpp:226
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition Symtab.cpp:450
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition Symtab.cpp:604
uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes, uint32_t start_idx=0, uint32_t end_index=UINT32_MAX) const
Definition Symtab.cpp:478
std::vector< uint32_t > IndexCollection
Definition Symtab.h:24
size_t GetNumSymbols() const
Definition Symtab.cpp:75
#define LLDB_INVALID_UID
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::Function > FunctionSP
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eSymbolTypeObjCClass
@ eSymbolTypeSourceFile
uint64_t user_id_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP