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 // 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 =
124 std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr,
125 cu_symbol->GetName().AsCString(nullptr),
127 }
128 return cu_sp;
129}
130
134
136 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
137 size_t num_added = 0;
138 // We must at least have a valid compile unit
139 const Symtab *symtab = m_objfile_sp->GetSymtab();
140 const Symbol *curr_symbol = nullptr;
141 const Symbol *next_symbol = nullptr;
142 // const char *prefix = m_objfile_sp->SymbolPrefix();
143 // if (prefix == NULL)
144 // prefix == "";
145 //
146 // const uint32_t prefix_len = strlen(prefix);
147
148 // If we don't have any source file symbols we will just have one compile
149 // unit for the entire object file
150 if (m_source_indexes.empty()) {
151 // The only time we will have a user ID of zero is when we don't have and
152 // source file symbols and we declare one compile unit for the entire
153 // object file
154 if (!m_func_indexes.empty()) {
155 }
156
157 if (!m_code_indexes.empty()) {
158 // StreamFile s(stdout);
159 // symtab->Dump(&s, m_code_indexes);
160
161 uint32_t idx = 0; // Index into the indexes
162 const uint32_t num_indexes = m_code_indexes.size();
163 for (idx = 0; idx < num_indexes; ++idx) {
164 uint32_t symbol_idx = m_code_indexes[idx];
165 curr_symbol = symtab->SymbolAtIndex(symbol_idx);
166 if (curr_symbol) {
167 // Union of all ranges in the function DIE (if the function is
168 // discontiguous)
169 AddressRange func_range(curr_symbol->GetAddress(), 0);
170 if (func_range.GetBaseAddress().IsSectionOffset()) {
171 uint32_t symbol_size = curr_symbol->GetByteSize();
172 if (symbol_size != 0 && !curr_symbol->GetSizeIsSibling())
173 func_range.SetByteSize(symbol_size);
174 else if (idx + 1 < num_indexes) {
175 next_symbol = symtab->SymbolAtIndex(m_code_indexes[idx + 1]);
176 if (next_symbol) {
177 func_range.SetByteSize(
178 next_symbol->GetAddressRef().GetOffset() -
179 curr_symbol->GetAddressRef().GetOffset());
180 }
181 }
182
183 FunctionSP func_sp(new Function(
184 &comp_unit,
185 symbol_idx, // UserID is the DIE offset
186 LLDB_INVALID_UID, // We don't have any type info
187 // for this function
188 curr_symbol->GetMangled(), // Linker/mangled name
189 nullptr, // no return type for a code symbol...
190 curr_symbol->GetAddress(), AddressRanges{func_range}));
191
192 if (func_sp.get() != nullptr) {
193 comp_unit.AddFunction(func_sp);
194 ++num_added;
195 }
196 }
197 }
198 }
199 }
200 } else {
201 // We assume we
202 }
203 return num_added;
204}
205
206size_t SymbolFileSymtab::ParseTypes(CompileUnit &comp_unit) { return 0; }
207
208bool SymbolFileSymtab::ParseLineTable(CompileUnit &comp_unit) { return false; }
209
211 return false;
212}
213
215 SupportFileList &support_files) {
216 return false;
217}
218
220 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
221 return false;
222}
223
225
227 return 0;
228}
229
231 return nullptr;
232}
233
234std::optional<SymbolFile::ArrayInfo>
236 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
237 return std::nullopt;
238}
239
241 return false;
242}
243
245 SymbolContextItem resolve_scope,
246 SymbolContext &sc) {
247 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
248 if (m_objfile_sp->GetSymtab() == nullptr)
249 return 0;
250
251 uint32_t resolved_flags = 0;
252 if (resolve_scope & eSymbolContextSymbol) {
253 sc.symbol = m_objfile_sp->GetSymtab()->FindSymbolContainingFileAddress(
254 so_addr.GetFileAddress());
255 if (sc.symbol)
256 resolved_flags |= eSymbolContextSymbol;
257 }
258 return resolved_flags;
259}
#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:281
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:400
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:646
SymbolFileCommon(lldb::ObjectFileSP objfile_sp)
Definition SymbolFile.h:555
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:147
Address & GetAddressRef()
Definition Symbol.h:73
bool GetSizeIsSibling() const
Definition Symbol.h:218
lldb::addr_t GetByteSize() const
Definition Symbol.cpp:431
ConstString GetName() const
Definition Symbol.cpp:511
Address GetAddress() const
Definition Symbol.h:89
Symbol * SymbolAtIndex(size_t idx)
Definition Symtab.cpp:225
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition Symtab.cpp:468
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition Symtab.cpp:622
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:496
std::vector< uint32_t > IndexCollection
Definition Symtab.h:24
#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:82
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP