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 // Only source-file entries provide a genuine debug-info ability. Code
67 // and data entries remain available as symbols but are not equivalent
68 // to debug-info functions or global variables. The symbol indexes are
69 // built later by InitializeSymbolIndexes() so we don't need to iterate
70 // over all of the symbols to calculate our abilities.
71 if (symtab->HasSourceFiles())
72 abilities |= CompileUnits;
73 }
74 }
75 return abilities;
76}
77
79 // GetModuleMutex() dereferences the object file, so check it first.
80 if (!m_objfile_sp)
81 return;
82 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
84 return;
85 const Symtab *symtab = m_objfile_sp->GetSymtab();
86 if (!symtab)
87 return;
89
90 // Get the indexes of the source file and non-debug code entries in the
91 // module symbol table.
93
98 }
99}
100
102 // Compile units are only created from source file symbols, so don't build
103 // the symbol indexes, which iterates over all of the symbols, if there
104 // aren't any. Nothing else uses the indexes without a compile unit.
105 if (!m_objfile_sp)
106 return 0;
107 const Symtab *symtab = m_objfile_sp->GetSymtab();
108 if (!symtab || !symtab->HasSourceFiles())
109 return 0;
110
112
113 // Each source file symbol is a compile unit, and the object file symbols
114 // are logically organized using these.
115 return m_source_indexes.size();
116}
117
120
121 CompUnitSP cu_sp;
122
123 // Each source file symbol is a compile unit.
124 if (idx < m_source_indexes.size()) {
125 const Symbol *cu_symbol =
126 m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
127 if (cu_symbol)
128 cu_sp =
129 std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
130 cu_symbol->GetName().AsCString(nullptr),
132 }
133 return cu_sp;
134}
135
139
141 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
143 size_t num_added = 0;
144 // We must at least have a valid compile unit
145 const Symtab *symtab = m_objfile_sp->GetSymtab();
146 const Symbol *curr_symbol = nullptr;
147 const Symbol *next_symbol = nullptr;
148 // const char *prefix = m_objfile_sp->SymbolPrefix();
149 // if (prefix == NULL)
150 // prefix == "";
151 //
152 // const uint32_t prefix_len = strlen(prefix);
153
154 // If we don't have any source file symbols we will just have one compile
155 // unit for the entire object file
156 if (m_source_indexes.empty()) {
157 // The only time we will have a user ID of zero is when we don't have and
158 // source file symbols and we declare one compile unit for the entire
159 // object file
160 if (!m_code_indexes.empty()) {
161 // StreamFile s(stdout);
162 // symtab->Dump(&s, m_code_indexes);
163
164 uint32_t idx = 0; // Index into the indexes
165 const uint32_t num_indexes = m_code_indexes.size();
166 for (idx = 0; idx < num_indexes; ++idx) {
167 uint32_t symbol_idx = m_code_indexes[idx];
168 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
169 if (curr_symbol) {
170 // Union of all ranges in the function DIE (if the function is
171 // discontiguous)
172 AddressRange func_range(curr_symbol->GetAddress(), 0);
173 if (func_range.GetBaseAddress().IsSectionOffset()) {
174 uint32_t symbol_size = curr_symbol->GetByteSize();
175 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
176 func_range.SetByteSize(symbol_size);
177 else if (idx + 1 < num_indexes) {
178 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
179 if (next_symbol) {
180 func_range.SetByteSize(
181 next_symbol->GetAddressRef().GetOffset() -
182 curr_symbol->GetAddressRef().GetOffset());
183 }
184 }
185
186 FunctionSP func_sp(new Function(
187 &comp_unit,
188 symbol_idx, // UserID is the DIE offset
189 LLDB_INVALID_UID, // We don't have any type info
190 // for this function
191 curr_symbol->GetMangled(), // Linker/mangled name
192 nullptr, // no return type for a code symbol...
193 curr_symbol->GetAddress(), AddressRanges{func_range}));
194
195 if (func_sp.get() != nullptr) {
196 comp_unit.AddFunction(func_sp);
197 ++num_added;
198 }
199 }
200 }
201 }
202 }
203 } else {
204 // We assume we
205 }
206 return num_added;
207}
208
209size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
210
211bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
212
214 return false;
215}
216
218 SupportFileList &support_files) {
219 return false;
220}
221
223 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
224 return false;
225}
226
228
230 return 0;
231}
232
234 return nullptr;
235}
236
237std::optional<SymbolFile::ArrayInfo>
239 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
240 return std::nullopt;
241}
242
244 return false;
245}
246
248 SymbolContextItem resolve_scope,
249 SymbolContext &sc) {
250 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
251 if (m_objfile_sp->GetSymtab() == nullptr)
252 return 0;
253
254 uint32_t resolved_flags = 0;
255 if (resolve_scope & eSymbolContextSymbol) {
256 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
257 so_addr.GetFileAddress());
258 if (sc.symbol)
259 resolved_flags |= eSymbolContextSymbol;
260 }
261 return resolved_flags;
262}
#define LLDB_PLUGIN_DEFINE(PluginName)
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()
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
void InitializeSymbolIndexes()
Build the symbol indexes from the object file's symbol table the first time they are needed,...
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::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:677
SymbolFileCommon(lldb::ObjectFileSP objfile_sp)
Definition SymbolFile.h:586
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:470
ConstString GetName() const
Definition Symbol.cpp:615
Address GetAddress() const
Definition Symbol.h:98
Symbol * SymbolAtIndex(size_t idx)
Definition Symtab.cpp:262
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition Symtab.cpp:612
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:486
size_t GetNumSymbols() const
Definition Symtab.cpp:83
bool HasSourceFiles() const
Check if this symbol table contains any source file symbols.
Definition Symtab.cpp:109
#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.
@ eSymbolTypeSourceFile
uint64_t user_id_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP