diff options
| author | nerix <nerixdev@outlook.de> | 2025-08-12 03:51:37 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-11 20:51:37 -0500 |
| commit | 7e2cc725dbe00a9c99b0d58df020ab970e53dd51 (patch) | |
| tree | 586de7fdbfb4e8a1a1e1287a25130a38145bcc2b /lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp | |
| parent | 713ec58dec1ec4d3866d62e563d7bd8e1ae3da53 (diff) | |
[LLDB][NativePDB] Find functions by basename (#152295)
This adds the ability for functions to be matched by their basename.
Before, the globals were searched for the name. This works if the full
name is available but fails for basenames.
PDB only includes the full names of functions, so we need to cache all
basenames. This is (again) very similar to
[SymbolFilePDB](https://github.com/llvm/llvm-project/blob/b242150b075a8a720b00821682a9469258bbcd30/lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp#L1291-L1383).
There are two main differences:
- We can't just access the parent of a function to determine that it's a
member function - we need to check the type of the function, and its
"this type".
- SymbolFilePDB saved the full method name in the map. However, when
searching for methods, only the basename is passed, so the function
never found any methods.
Fixes #51933.
---------
Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com>
Diffstat (limited to 'lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp')
| -rw-r--r-- | lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp | 153 |
1 files changed, 134 insertions, 19 deletions
diff --git a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp index dcea33dd9f85..986d647b4de2 100644 --- a/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp +++ b/lldb/source/Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.cpp @@ -39,6 +39,7 @@ #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" #include "llvm/DebugInfo/PDB/Native/NativeSession.h" #include "llvm/DebugInfo/PDB/Native/PDBFile.h" +#include "llvm/DebugInfo/PDB/Native/PublicsStream.h" #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" #include "llvm/DebugInfo/PDB/Native/TpiStream.h" #include "llvm/DebugInfo/PDB/PDB.h" @@ -1641,6 +1642,94 @@ void SymbolFileNativePDB::DumpClangAST(Stream &s, llvm::StringRef filter) { clang->GetNativePDBParser()->Dump(s, filter); } +void SymbolFileNativePDB::CacheFunctionNames() { + if (!m_func_full_names.IsEmpty()) + return; + + // (segment, code offset) -> gid + std::map<std::pair<uint16_t, uint32_t>, uint32_t> addr_ids; + + // First, find all function references in the globals table. + for (const uint32_t gid : m_index->globals().getGlobalsTable()) { + CVSymbol ref_sym = m_index->symrecords().readRecord(gid); + auto kind = ref_sym.kind(); + if (kind != S_PROCREF && kind != S_LPROCREF) + continue; + + ProcRefSym ref = + llvm::cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(ref_sym)); + if (ref.Name.empty()) + continue; + + // Find the function this is referencing. + CompilandIndexItem &cci = + m_index->compilands().GetOrCreateCompiland(ref.modi()); + auto iter = cci.m_debug_stream.getSymbolArray().at(ref.SymOffset); + if (iter == cci.m_debug_stream.getSymbolArray().end()) + continue; + kind = iter->kind(); + if (kind != S_GPROC32 && kind != S_LPROC32) + continue; + + ProcSym proc = + llvm::cantFail(SymbolDeserializer::deserializeAs<ProcSym>(*iter)); + if ((proc.Flags & ProcSymFlags::IsUnreachable) != ProcSymFlags::None) + continue; + if (proc.Name.empty()) + continue; + + // The function/procedure symbol only contains the demangled name. + // The mangled names are in the publics table. Save the address of this + // function to lookup the mangled name later. + addr_ids.emplace(std::make_pair(proc.Segment, proc.CodeOffset), gid); + + llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(proc.Name); + if (basename.empty()) + basename = proc.Name; + + m_func_base_names.Append(ConstString(basename), gid); + m_func_full_names.Append(ConstString(proc.Name), gid); + + // To see if this is a member function, check the type. + auto type = m_index->tpi().getType(proc.FunctionType); + if (type.kind() == LF_MFUNCTION) { + MemberFunctionRecord mfr; + llvm::cantFail( + TypeDeserializer::deserializeAs<MemberFunctionRecord>(type, mfr)); + if (!mfr.getThisType().isNoneType()) + m_func_method_names.Append(ConstString(basename), gid); + } + } + + // The publics stream contains all mangled function names and their address. + for (auto pid : m_index->publics().getPublicsTable()) { + PdbGlobalSymId global{pid, true}; + CVSymbol sym = m_index->ReadSymbolRecord(global); + auto kind = sym.kind(); + if (kind != S_PUB32) + continue; + PublicSym32 pub = + llvm::cantFail(SymbolDeserializer::deserializeAs<PublicSym32>(sym)); + // We only care about mangled names - if the name isn't mangled, it's + // already in the full name map. + if (!Mangled::IsMangledName(pub.Name)) + continue; + + // Check if this symbol is for one of our functions. + auto it = addr_ids.find({pub.Segment, pub.Offset}); + if (it != addr_ids.end()) + m_func_full_names.Append(ConstString(pub.Name), it->second); + } + + // Sort them before value searching is working properly. + m_func_full_names.Sort(); + m_func_full_names.SizeToFit(); + m_func_method_names.Sort(); + m_func_method_names.SizeToFit(); + m_func_base_names.Sort(); + m_func_base_names.SizeToFit(); +} + void SymbolFileNativePDB::FindGlobalVariables( ConstString name, const CompilerDeclContext &parent_decl_ctx, uint32_t max_matches, VariableList &variables) { @@ -1677,34 +1766,60 @@ void SymbolFileNativePDB::FindFunctions( if (name_type_mask & eFunctionNameTypeFull) name = lookup_info.GetName(); - // For now we only support lookup by method name or full name. if (!(name_type_mask & eFunctionNameTypeFull || + name_type_mask & eFunctionNameTypeBase || name_type_mask & eFunctionNameTypeMethod)) return; + CacheFunctionNames(); - using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; + std::set<uint32_t> resolved_ids; // avoid duplicate lookups + auto resolve_from = [&](UniqueCStringMap<uint32_t> &Names) { + std::vector<uint32_t> ids; + if (!Names.GetValues(name, ids)) + return; - std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName( - name.GetStringRef(), m_index->symrecords()); - for (const SymbolAndOffset &match : matches) { - if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF) - continue; - ProcRefSym proc(match.second.kind()); - cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc)); + for (uint32_t id : ids) { + if (!resolved_ids.insert(id).second) + continue; - if (!IsValidRecord(proc)) - continue; + PdbGlobalSymId global{id, false}; + if (parent_decl_ctx.IsValid() && + GetDeclContextContainingUID(toOpaqueUid(global)) != parent_decl_ctx) + continue; - CompilandIndexItem &cci = - m_index->compilands().GetOrCreateCompiland(proc.modi()); - SymbolContext sc; + CVSymbol sym = m_index->ReadSymbolRecord(global); + auto kind = sym.kind(); + lldbassert(kind == S_PROCREF || kind == S_LPROCREF); - sc.comp_unit = GetOrCreateCompileUnit(cci).get(); - PdbCompilandSymId func_id(proc.modi(), proc.SymOffset); - sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get(); + ProcRefSym proc = + cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(sym)); - sc_list.Append(sc); - } + if (!IsValidRecord(proc)) + continue; + + CompilandIndexItem &cci = + m_index->compilands().GetOrCreateCompiland(proc.modi()); + SymbolContext sc; + + sc.comp_unit = GetOrCreateCompileUnit(cci).get(); + if (!sc.comp_unit) + continue; + + PdbCompilandSymId func_id(proc.modi(), proc.SymOffset); + sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get(); + if (!sc.function) + continue; + + sc_list.Append(sc); + } + }; + + if (name_type_mask & eFunctionNameTypeFull) + resolve_from(m_func_full_names); + if (name_type_mask & eFunctionNameTypeBase) + resolve_from(m_func_base_names); + if (name_type_mask & eFunctionNameTypeMethod) + resolve_from(m_func_method_names); } void SymbolFileNativePDB::FindFunctions(const RegularExpression ®ex, |
