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
36}
37
40}
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
55 : SymbolFileCommon(std::move(objfile_sp)), m_source_indexes(),
56 m_func_indexes(), m_code_indexes(), m_objc_class_name_to_index() {}
57
59 uint32_t abilities = 0;
60 if (m_objfile_sp) {
61 const Symtab *symtab = m_objfile_sp->GetSymtab();
62 if (symtab) {
63 // The snippet of code below will get the indexes the module symbol table
64 // entries that are code, data, or function related (debug info), sort
65 // them by value (address) and dump the sorted symbols.
68 abilities |= CompileUnits;
69 }
70
75 abilities |= Functions;
76 }
77
82 abilities |= Functions;
83 }
84
88 abilities |= GlobalVariables;
89 }
90
93 objc_class_indexes)) {
94 symtab->AppendSymbolNamesToMap(objc_class_indexes, true, true,
97 }
98 }
99 }
100 return abilities;
101}
102
104 // If we don't have any source file symbols we will just have one compile
105 // unit for the entire object file
106 if (m_source_indexes.empty())
107 return 0;
108
109 // If we have any source file symbols we will logically organize the object
110 // symbols using these.
111 return m_source_indexes.size();
112}
113
115 CompUnitSP cu_sp;
116
117 // If we don't have any source file symbols we will just have one compile
118 // unit for the entire object file
119 if (idx < m_source_indexes.size()) {
120 const Symbol *cu_symbol =
121 m_objfile_sp->GetSymtab()->SymbolAtIndex(m_source_indexes[idx]);
122 if (cu_symbol)
123 cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
124 cu_symbol->GetName().AsCString(), 0,
126 }
127 return cu_sp;
128}
129
132}
133
135 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
136 size_t num_added = 0;
137 // We must at least have a valid compile unit
138 const Symtab *symtab = m_objfile_sp->GetSymtab();
139 const Symbol *curr_symbol = nullptr;
140 const Symbol *next_symbol = nullptr;
141 // const char *prefix = m_objfile_sp->SymbolPrefix();
142 // if (prefix == NULL)
143 // prefix == "";
144 //
145 // const uint32_t prefix_len = strlen(prefix);
146
147 // If we don't have any source file symbols we will just have one compile
148 // unit for the entire object file
149 if (m_source_indexes.empty()) {
150 // The only time we will have a user ID of zero is when we don't have and
151 // source file symbols and we declare one compile unit for the entire
152 // object file
153 if (!m_func_indexes.empty()) {
154 }
155
156 if (!m_code_indexes.empty()) {
157 // StreamFile s(stdout);
158 // symtab->Dump(&s, m_code_indexes);
159
160 uint32_t idx = 0; // Index into the indexes
161 const uint32_t num_indexes = m_code_indexes.size();
162 for (idx = 0; idx < num_indexes; ++idx) {
163 uint32_t symbol_idx = m_code_indexes[idx];
164 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
165 if (curr_symbol) {
166 // Union of all ranges in the function DIE (if the function is
167 // discontiguous)
168 AddressRange func_range(curr_symbol->GetAddress(), 0);
169 if (func_range.GetBaseAddress().IsSectionOffset()) {
170 uint32_t symbol_size = curr_symbol->GetByteSize();
171 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
172 func_range.SetByteSize(symbol_size);
173 else if (idx + 1 < num_indexes) {
174 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
175 if (next_symbol) {
176 func_range.SetByteSize(
177 next_symbol->GetAddressRef().GetOffset() -
178 curr_symbol->GetAddressRef().GetOffset());
179 }
180 }
181
182 FunctionSP func_sp(
183 new Function(&comp_unit,
184 symbol_idx, // UserID is the DIE offset
185 LLDB_INVALID_UID, // We don't have any type info
186 // for this function
187 curr_symbol->GetMangled(), // Linker/mangled name
188 nullptr, // no return type for a code symbol...
189 func_range)); // first address range
190
191 if (func_sp.get() != nullptr) {
192 comp_unit.AddFunction(func_sp);
193 ++num_added;
194 }
195 }
196 }
197 }
198 }
199 } else {
200 // We assume we
201 }
202 return num_added;
203}
204
205size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
206
207bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
208
210 return false;
211}
212
214 SupportFileList &support_files) {
215 return false;
216}
217
219 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
220 return false;
221}
222
224
226 return 0;
227}
228
230 return nullptr;
231}
232
233std::optional<SymbolFile::ArrayInfo>
235 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
236 return std::nullopt;
237}
238
240 return false;
241}
242
244 SymbolContextItem resolve_scope,
245 SymbolContext &sc) {
246 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
247 if (m_objfile_sp->GetSymtab() == nullptr)
248 return 0;
249
250 uint32_t resolved_flags = 0;
251 if (resolve_scope & eSymbolContextSymbol) {
252 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
253 so_addr.GetFileAddress());
254 if (sc.symbol)
255 resolved_flags |= eSymbolContextSymbol;
256 }
257 return resolved_flags;
258}
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
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.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
Definition: AddressRange.h:239
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
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:41
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that describes a function.
Definition: Function.h:399
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.
Definition: FileSpecList.h:23
"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.
Definition: SymbolContext.h:34
Symbol * symbol
The Symbol for a given query.
Containing protected virtual methods for child classes to override.
Definition: SymbolFile.h:496
lldb::ObjectFileSP m_objfile_sp
Definition: SymbolFile.h:601
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:50
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
Definition: SymbolFile.cpp:36
Mangled & GetMangled()
Definition: Symbol.h:146
Address & GetAddressRef()
Definition: Symbol.h:72
bool GetSizeIsSibling() const
Definition: Symbol.h:217
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:468
ConstString GetName() const
Definition: Symbol.cpp:548
Address GetAddress() const
Definition: Symbol.h:88
Symbol * SymbolAtIndex(size_t idx)
Definition: Symtab.cpp:228
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition: Symtab.cpp:471
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition: Symtab.cpp:625
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:499
std::vector< uint32_t > IndexCollection
Definition: Symtab.h:24
#define LLDB_INVALID_UID
Definition: lldb-defines.h:88
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Function > FunctionSP
Definition: lldb-forward.h:350
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:370
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eSymbolTypeObjCClass
@ eSymbolTypeSourceFile
uint64_t user_id_t
Definition: lldb-types.h:82
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP
Definition: lldb-forward.h:330