174e08ca0SZachary Turner //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===// 274e08ca0SZachary Turner // 374e08ca0SZachary Turner // The LLVM Compiler Infrastructure 474e08ca0SZachary Turner // 574e08ca0SZachary Turner // This file is distributed under the University of Illinois Open Source 674e08ca0SZachary Turner // License. See LICENSE.TXT for details. 774e08ca0SZachary Turner // 874e08ca0SZachary Turner //===----------------------------------------------------------------------===// 974e08ca0SZachary Turner 1074e08ca0SZachary Turner #include "SymbolFilePDB.h" 1174e08ca0SZachary Turner 1242dff790SZachary Turner #include "clang/Lex/Lexer.h" 1342dff790SZachary Turner 1474e08ca0SZachary Turner #include "lldb/Core/Module.h" 1574e08ca0SZachary Turner #include "lldb/Core/PluginManager.h" 1642dff790SZachary Turner #include "lldb/Symbol/ClangASTContext.h" 1774e08ca0SZachary Turner #include "lldb/Symbol/CompileUnit.h" 1874e08ca0SZachary Turner #include "lldb/Symbol/LineTable.h" 1974e08ca0SZachary Turner #include "lldb/Symbol/ObjectFile.h" 2074e08ca0SZachary Turner #include "lldb/Symbol/SymbolContext.h" 2110a02577SAaron Smith #include "lldb/Symbol/SymbolVendor.h" 22ec40f818SAaron Smith #include "lldb/Symbol/TypeList.h" 23308e39caSAaron Smith #include "lldb/Symbol/TypeMap.h" 24cab0d23fSAaron Smith #include "lldb/Symbol/Variable.h" 2586e9434dSAaron Smith #include "lldb/Utility/RegularExpression.h" 2674e08ca0SZachary Turner 27b8d8c62bSPavel Labath #include "llvm/DebugInfo/PDB/GenericError.h" 281f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 2974e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 3074e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 31308e39caSAaron Smith #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h" 3274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 331f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBTable.h" 3474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbol.h" 357ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 3674e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 3774e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 381f8552abSAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 3974e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 4074e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 4174e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 4274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 437ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 4442dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 4542dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 4642dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 4742dff790SZachary Turner 48*7d2a74fcSAleksandr Urakov #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" // For IsCPPMangledName 4942dff790SZachary Turner #include "Plugins/SymbolFile/PDB/PDBASTParser.h" 50924d5608SJonas Devlieghere #include "Plugins/SymbolFile/PDB/PDBLocationToDWARFExpression.h" 5142dff790SZachary Turner 5242dff790SZachary Turner #include <regex> 5374e08ca0SZachary Turner 5410a02577SAaron Smith using namespace lldb; 5574e08ca0SZachary Turner using namespace lldb_private; 5654fd7ff6SZachary Turner using namespace llvm::pdb; 5774e08ca0SZachary Turner 58b9c1b51eSKate Stone namespace { 59b9c1b51eSKate Stone lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 60b9c1b51eSKate Stone switch (lang) { 6154fd7ff6SZachary Turner case PDB_Lang::Cpp: 6274e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeC_plus_plus; 6354fd7ff6SZachary Turner case PDB_Lang::C: 6474e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeC; 6574e08ca0SZachary Turner default: 6674e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeUnknown; 6774e08ca0SZachary Turner } 6874e08ca0SZachary Turner } 697e8c7beaSZachary Turner 70b9c1b51eSKate Stone bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 71b9c1b51eSKate Stone uint32_t addr_length) { 72b9c1b51eSKate Stone return ((requested_line == 0 || actual_line == requested_line) && 73b9c1b51eSKate Stone addr_length > 0); 747e8c7beaSZachary Turner } 75c8316ed2SAaron Smith } // namespace 7674e08ca0SZachary Turner 77b9c1b51eSKate Stone void SymbolFilePDB::Initialize() { 78b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 79b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 8074e08ca0SZachary Turner DebuggerInitialize); 8174e08ca0SZachary Turner } 8274e08ca0SZachary Turner 83b9c1b51eSKate Stone void SymbolFilePDB::Terminate() { 8474e08ca0SZachary Turner PluginManager::UnregisterPlugin(CreateInstance); 8574e08ca0SZachary Turner } 8674e08ca0SZachary Turner 87b9c1b51eSKate Stone void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 8874e08ca0SZachary Turner 89b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 9074e08ca0SZachary Turner static ConstString g_name("pdb"); 9174e08ca0SZachary Turner return g_name; 9274e08ca0SZachary Turner } 9374e08ca0SZachary Turner 94b9c1b51eSKate Stone const char *SymbolFilePDB::GetPluginDescriptionStatic() { 9574e08ca0SZachary Turner return "Microsoft PDB debug symbol file reader."; 9674e08ca0SZachary Turner } 9774e08ca0SZachary Turner 9874e08ca0SZachary Turner lldb_private::SymbolFile * 99b9c1b51eSKate Stone SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { 10074e08ca0SZachary Turner return new SymbolFilePDB(obj_file); 10174e08ca0SZachary Turner } 10274e08ca0SZachary Turner 10374e08ca0SZachary Turner SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 10410a02577SAaron Smith : SymbolFile(object_file), m_session_up(), m_global_scope_up(), 10510a02577SAaron Smith m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} 10674e08ca0SZachary Turner 107b9c1b51eSKate Stone SymbolFilePDB::~SymbolFilePDB() {} 10874e08ca0SZachary Turner 109b9c1b51eSKate Stone uint32_t SymbolFilePDB::CalculateAbilities() { 1101f8552abSAaron Smith uint32_t abilities = 0; 1111f8552abSAaron Smith if (!m_obj_file) 1121f8552abSAaron Smith return 0; 1131f8552abSAaron Smith 114b9c1b51eSKate Stone if (!m_session_up) { 11574e08ca0SZachary Turner // Lazily load and match the PDB file, but only do this once. 11674e08ca0SZachary Turner std::string exePath = m_obj_file->GetFileSpec().GetPath(); 117b9c1b51eSKate Stone auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 118b9c1b51eSKate Stone m_session_up); 119b9c1b51eSKate Stone if (error) { 1204fd6a960SZachary Turner llvm::consumeError(std::move(error)); 1211f8552abSAaron Smith auto module_sp = m_obj_file->GetModule(); 1221f8552abSAaron Smith if (!module_sp) 1231f8552abSAaron Smith return 0; 1241f8552abSAaron Smith // See if any symbol file is specified through `--symfile` option. 1251f8552abSAaron Smith FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 1261f8552abSAaron Smith if (!symfile) 1271f8552abSAaron Smith return 0; 1281f8552abSAaron Smith error = loadDataForPDB(PDB_ReaderType::DIA, 129c8316ed2SAaron Smith llvm::StringRef(symfile.GetPath()), m_session_up); 1301f8552abSAaron Smith if (error) { 1311f8552abSAaron Smith llvm::consumeError(std::move(error)); 13274e08ca0SZachary Turner return 0; 13374e08ca0SZachary Turner } 134b8d8c62bSPavel Labath } 1351f8552abSAaron Smith } 136d5a925f4SAaron Smith if (!m_session_up) 1371f8552abSAaron Smith return 0; 1381f8552abSAaron Smith 1391f8552abSAaron Smith auto enum_tables_up = m_session_up->getEnumTables(); 1401f8552abSAaron Smith if (!enum_tables_up) 1411f8552abSAaron Smith return 0; 1421f8552abSAaron Smith while (auto table_up = enum_tables_up->getNext()) { 1431f8552abSAaron Smith if (table_up->getItemCount() == 0) 1441f8552abSAaron Smith continue; 1451f8552abSAaron Smith auto type = table_up->getTableType(); 1461f8552abSAaron Smith switch (type) { 1471f8552abSAaron Smith case PDB_TableType::Symbols: 1481f8552abSAaron Smith // This table represents a store of symbols with types listed in 1491f8552abSAaron Smith // PDBSym_Type 150c8316ed2SAaron Smith abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | 151c8316ed2SAaron Smith LocalVariables | VariableTypes); 1521f8552abSAaron Smith break; 1531f8552abSAaron Smith case PDB_TableType::LineNumbers: 1541f8552abSAaron Smith abilities |= LineTables; 1551f8552abSAaron Smith break; 156c8316ed2SAaron Smith default: 157c8316ed2SAaron Smith break; 1581f8552abSAaron Smith } 1591f8552abSAaron Smith } 1601f8552abSAaron Smith return abilities; 16174e08ca0SZachary Turner } 16274e08ca0SZachary Turner 163b9c1b51eSKate Stone void SymbolFilePDB::InitializeObject() { 16474e08ca0SZachary Turner lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); 165c8316ed2SAaron Smith lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); 16674e08ca0SZachary Turner m_session_up->setLoadAddress(obj_load_address); 16710a02577SAaron Smith if (!m_global_scope_up) 16810a02577SAaron Smith m_global_scope_up = m_session_up->getGlobalScope(); 16910a02577SAaron Smith lldbassert(m_global_scope_up.get()); 17042dff790SZachary Turner 171b9c1b51eSKate Stone TypeSystem *type_system = 172b9c1b51eSKate Stone GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 173b9c1b51eSKate Stone ClangASTContext *clang_type_system = 174b9c1b51eSKate Stone llvm::dyn_cast_or_null<ClangASTContext>(type_system); 17510a02577SAaron Smith lldbassert(clang_type_system); 176b9c1b51eSKate Stone m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( 177b9c1b51eSKate Stone type_system, clang_type_system->GetTranslationUnitDecl()); 17874e08ca0SZachary Turner } 17974e08ca0SZachary Turner 180b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetNumCompileUnits() { 181b9c1b51eSKate Stone if (m_cached_compile_unit_count == 0) { 18210a02577SAaron Smith auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 18310a02577SAaron Smith if (!compilands) 18410a02577SAaron Smith return 0; 18510a02577SAaron Smith 18610a02577SAaron Smith // The linker could link *.dll (compiland language = LINK), or import 18705097246SAdrian Prantl // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be 18805097246SAdrian Prantl // found as a child of the global scope (PDB executable). Usually, such 18905097246SAdrian Prantl // compilands contain `thunk` symbols in which we are not interested for 19005097246SAdrian Prantl // now. However we still count them in the compiland list. If we perform 19105097246SAdrian Prantl // any compiland related activity, like finding symbols through 19205097246SAdrian Prantl // llvm::pdb::IPDBSession methods, such compilands will all be searched 19305097246SAdrian Prantl // automatically no matter whether we include them or not. 19474e08ca0SZachary Turner m_cached_compile_unit_count = compilands->getChildCount(); 19574e08ca0SZachary Turner 196b9c1b51eSKate Stone // The linker can inject an additional "dummy" compilation unit into the 1979d0eb996SAdrian McCarthy // PDB. Ignore this special compile unit for our purposes, if it is there. 1989d0eb996SAdrian McCarthy // It is always the last one. 19910a02577SAaron Smith auto last_compiland_up = 20010a02577SAaron Smith compilands->getChildAtIndex(m_cached_compile_unit_count - 1); 20110a02577SAaron Smith lldbassert(last_compiland_up.get()); 20210a02577SAaron Smith std::string name = last_compiland_up->getName(); 20374e08ca0SZachary Turner if (name == "* Linker *") 20474e08ca0SZachary Turner --m_cached_compile_unit_count; 20574e08ca0SZachary Turner } 20674e08ca0SZachary Turner return m_cached_compile_unit_count; 20774e08ca0SZachary Turner } 20874e08ca0SZachary Turner 20910a02577SAaron Smith void SymbolFilePDB::GetCompileUnitIndex( 210c8316ed2SAaron Smith const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { 21110a02577SAaron Smith auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 21210a02577SAaron Smith if (!results_up) 21310a02577SAaron Smith return; 214e664b5dcSAaron Smith auto uid = pdb_compiland.getSymIndexId(); 215fbdf0b93SRaphael Isemann for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 21610a02577SAaron Smith auto compiland_up = results_up->getChildAtIndex(cu_idx); 21710a02577SAaron Smith if (!compiland_up) 21810a02577SAaron Smith continue; 21910a02577SAaron Smith if (compiland_up->getSymIndexId() == uid) { 22010a02577SAaron Smith index = cu_idx; 22110a02577SAaron Smith return; 22210a02577SAaron Smith } 22310a02577SAaron Smith } 22410a02577SAaron Smith index = UINT32_MAX; 22510a02577SAaron Smith return; 22610a02577SAaron Smith } 22710a02577SAaron Smith 22810a02577SAaron Smith std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 22910a02577SAaron Smith SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 23010a02577SAaron Smith return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 23110a02577SAaron Smith } 23210a02577SAaron Smith 233b9c1b51eSKate Stone lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 23410a02577SAaron Smith if (index >= GetNumCompileUnits()) 23510a02577SAaron Smith return CompUnitSP(); 23674e08ca0SZachary Turner 23710a02577SAaron Smith // Assuming we always retrieve same compilands listed in same order through 23810a02577SAaron Smith // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 23910a02577SAaron Smith // compile unit makes no sense. 24010a02577SAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 24110a02577SAaron Smith if (!results) 24210a02577SAaron Smith return CompUnitSP(); 24310a02577SAaron Smith auto compiland_up = results->getChildAtIndex(index); 24410a02577SAaron Smith if (!compiland_up) 24510a02577SAaron Smith return CompUnitSP(); 24610a02577SAaron Smith return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 24774e08ca0SZachary Turner } 24874e08ca0SZachary Turner 24974e08ca0SZachary Turner lldb::LanguageType 250b9c1b51eSKate Stone SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { 25174e08ca0SZachary Turner // What fields should I expect to be filled out on the SymbolContext? Is it 25274e08ca0SZachary Turner // safe to assume that `sc.comp_unit` is valid? 25374e08ca0SZachary Turner if (!sc.comp_unit) 25474e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 25574e08ca0SZachary Turner 25610a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 25710a02577SAaron Smith if (!compiland_up) 25874e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 25910a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 26074e08ca0SZachary Turner if (!details) 26174e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 26274e08ca0SZachary Turner return TranslateLanguage(details->getLanguage()); 26374e08ca0SZachary Turner } 26474e08ca0SZachary Turner 265c8316ed2SAaron Smith lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc( 266c8316ed2SAaron Smith const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) { 2677ac1c780SAaron Smith lldbassert(sc.comp_unit && sc.module_sp.get()); 2687ac1c780SAaron Smith 269e664b5dcSAaron Smith auto file_vm_addr = pdb_func.getVirtualAddress(); 270308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 2717ac1c780SAaron Smith return nullptr; 2727ac1c780SAaron Smith 273e664b5dcSAaron Smith auto func_length = pdb_func.getLength(); 274c8316ed2SAaron Smith AddressRange func_range = 275c8316ed2SAaron Smith AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList()); 2767ac1c780SAaron Smith if (!func_range.GetBaseAddress().IsValid()) 2777ac1c780SAaron Smith return nullptr; 2787ac1c780SAaron Smith 279e664b5dcSAaron Smith lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 2807ac1c780SAaron Smith if (!func_type) 2817ac1c780SAaron Smith return nullptr; 2827ac1c780SAaron Smith 283e664b5dcSAaron Smith user_id_t func_type_uid = pdb_func.getSignatureId(); 284f76fe682SAaron Smith 2857ac1c780SAaron Smith Mangled mangled = GetMangledForPDBFunc(pdb_func); 2867ac1c780SAaron Smith 287c8316ed2SAaron Smith FunctionSP func_sp = 288c8316ed2SAaron Smith std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(), 289c8316ed2SAaron Smith func_type_uid, mangled, func_type, func_range); 2907ac1c780SAaron Smith 2917ac1c780SAaron Smith sc.comp_unit->AddFunction(func_sp); 2927ac1c780SAaron Smith return func_sp.get(); 2937ac1c780SAaron Smith } 2947ac1c780SAaron Smith 295b9c1b51eSKate Stone size_t SymbolFilePDB::ParseCompileUnitFunctions( 296b9c1b51eSKate Stone const lldb_private::SymbolContext &sc) { 2977ac1c780SAaron Smith lldbassert(sc.comp_unit); 2987ac1c780SAaron Smith size_t func_added = 0; 2997ac1c780SAaron Smith auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 3007ac1c780SAaron Smith if (!compiland_up) 3017ac1c780SAaron Smith return 0; 3027ac1c780SAaron Smith auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 3037ac1c780SAaron Smith if (!results_up) 3047ac1c780SAaron Smith return 0; 3057ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 3067ac1c780SAaron Smith auto func_sp = 3077ac1c780SAaron Smith sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId()); 3087ac1c780SAaron Smith if (!func_sp) { 309e664b5dcSAaron Smith if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc)) 3107ac1c780SAaron Smith ++func_added; 3117ac1c780SAaron Smith } 3127ac1c780SAaron Smith } 3137ac1c780SAaron Smith return func_added; 31474e08ca0SZachary Turner } 31574e08ca0SZachary Turner 316b9c1b51eSKate Stone bool SymbolFilePDB::ParseCompileUnitLineTable( 317b9c1b51eSKate Stone const lldb_private::SymbolContext &sc) { 31810a02577SAaron Smith lldbassert(sc.comp_unit); 31910a02577SAaron Smith if (sc.comp_unit->GetLineTable()) 32010a02577SAaron Smith return true; 32174e08ca0SZachary Turner return ParseCompileUnitLineTable(sc, 0); 32274e08ca0SZachary Turner } 32374e08ca0SZachary Turner 324b9c1b51eSKate Stone bool SymbolFilePDB::ParseCompileUnitDebugMacros( 325b9c1b51eSKate Stone const lldb_private::SymbolContext &sc) { 32674e08ca0SZachary Turner // PDB doesn't contain information about macros 32774e08ca0SZachary Turner return false; 32874e08ca0SZachary Turner } 32974e08ca0SZachary Turner 330b9c1b51eSKate Stone bool SymbolFilePDB::ParseCompileUnitSupportFiles( 331b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 332b9c1b51eSKate Stone lldb_private::FileSpecList &support_files) { 33310a02577SAaron Smith lldbassert(sc.comp_unit); 33474e08ca0SZachary Turner 335b9c1b51eSKate Stone // In theory this is unnecessary work for us, because all of this information 3369d0eb996SAdrian McCarthy // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 3379d0eb996SAdrian McCarthy // second time seems like a waste. Unfortunately, there's no good way around 3389d0eb996SAdrian McCarthy // this short of a moderate refactor since SymbolVendor depends on being able 3399d0eb996SAdrian McCarthy // to cache this list. 34010a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 34110a02577SAaron Smith if (!compiland_up) 34274e08ca0SZachary Turner return false; 34310a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 34474e08ca0SZachary Turner if (!files || files->getChildCount() == 0) 34574e08ca0SZachary Turner return false; 34674e08ca0SZachary Turner 347b9c1b51eSKate Stone while (auto file = files->getNext()) { 3482cb7cf8eSPavel Labath FileSpec spec(file->getFileName(), false, FileSpec::Style::windows); 34910a02577SAaron Smith support_files.AppendIfUnique(spec); 35074e08ca0SZachary Turner } 3519ea80d25SPavel Labath 3529ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based), 3539ea80d25SPavel Labath // the zeroth file is the compile unit itself 3549ea80d25SPavel Labath support_files.Insert(0, *sc.comp_unit); 3559ea80d25SPavel Labath 35674e08ca0SZachary Turner return true; 35774e08ca0SZachary Turner } 35874e08ca0SZachary Turner 359b9c1b51eSKate Stone bool SymbolFilePDB::ParseImportedModules( 360b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 361b9c1b51eSKate Stone std::vector<lldb_private::ConstString> &imported_modules) { 36274e08ca0SZachary Turner // PDB does not yet support module debug info 36374e08ca0SZachary Turner return false; 36474e08ca0SZachary Turner } 36574e08ca0SZachary Turner 366c8316ed2SAaron Smith static size_t ParseFunctionBlocksForPDBSymbol( 367c8316ed2SAaron Smith const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr, 368c8316ed2SAaron Smith const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block, 3697ac1c780SAaron Smith bool is_top_parent) { 3707ac1c780SAaron Smith assert(pdb_symbol && parent_block); 3717ac1c780SAaron Smith 3727ac1c780SAaron Smith size_t num_added = 0; 3737ac1c780SAaron Smith switch (pdb_symbol->getSymTag()) { 3747ac1c780SAaron Smith case PDB_SymType::Block: 3757ac1c780SAaron Smith case PDB_SymType::Function: { 3767ac1c780SAaron Smith Block *block = nullptr; 3777ac1c780SAaron Smith auto &raw_sym = pdb_symbol->getRawSymbol(); 3787ac1c780SAaron Smith if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { 3797ac1c780SAaron Smith if (pdb_func->hasNoInlineAttribute()) 3807ac1c780SAaron Smith break; 3817ac1c780SAaron Smith if (is_top_parent) 3827ac1c780SAaron Smith block = parent_block; 3837ac1c780SAaron Smith else 3847ac1c780SAaron Smith break; 3857ac1c780SAaron Smith } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { 3867ac1c780SAaron Smith auto uid = pdb_symbol->getSymIndexId(); 3877ac1c780SAaron Smith if (parent_block->FindBlockByID(uid)) 3887ac1c780SAaron Smith break; 3897ac1c780SAaron Smith if (raw_sym.getVirtualAddress() < func_file_vm_addr) 3907ac1c780SAaron Smith break; 3917ac1c780SAaron Smith 3927ac1c780SAaron Smith auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); 3937ac1c780SAaron Smith parent_block->AddChild(block_sp); 3947ac1c780SAaron Smith block = block_sp.get(); 3957ac1c780SAaron Smith } else 3967ac1c780SAaron Smith llvm_unreachable("Unexpected PDB symbol!"); 3977ac1c780SAaron Smith 398c8316ed2SAaron Smith block->AddRange(Block::Range( 399c8316ed2SAaron Smith raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); 4007ac1c780SAaron Smith block->FinalizeRanges(); 4017ac1c780SAaron Smith ++num_added; 4027ac1c780SAaron Smith 4037ac1c780SAaron Smith auto results_up = pdb_symbol->findAllChildren(); 4047ac1c780SAaron Smith if (!results_up) 4057ac1c780SAaron Smith break; 4067ac1c780SAaron Smith while (auto symbol_up = results_up->getNext()) { 407c8316ed2SAaron Smith num_added += ParseFunctionBlocksForPDBSymbol( 408c8316ed2SAaron Smith sc, func_file_vm_addr, symbol_up.get(), block, false); 4097ac1c780SAaron Smith } 4107ac1c780SAaron Smith } break; 411c8316ed2SAaron Smith default: 412c8316ed2SAaron Smith break; 4137ac1c780SAaron Smith } 4147ac1c780SAaron Smith return num_added; 4157ac1c780SAaron Smith } 4167ac1c780SAaron Smith 41774e08ca0SZachary Turner size_t 418b9c1b51eSKate Stone SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { 4197ac1c780SAaron Smith lldbassert(sc.comp_unit && sc.function); 4207ac1c780SAaron Smith size_t num_added = 0; 4217ac1c780SAaron Smith auto uid = sc.function->GetID(); 4227ac1c780SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 4237ac1c780SAaron Smith if (!pdb_func_up) 4247ac1c780SAaron Smith return 0; 4257ac1c780SAaron Smith Block &parent_block = sc.function->GetBlock(false); 4267ac1c780SAaron Smith num_added = 4277ac1c780SAaron Smith ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(), 4287ac1c780SAaron Smith pdb_func_up.get(), &parent_block, true); 4297ac1c780SAaron Smith return num_added; 430b9c1b51eSKate Stone } 431b9c1b51eSKate Stone 432b9c1b51eSKate Stone size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { 433ec40f818SAaron Smith lldbassert(sc.module_sp.get()); 43466b84079SAaron Smith if (!sc.comp_unit) 435ec40f818SAaron Smith return 0; 43666b84079SAaron Smith 43766b84079SAaron Smith size_t num_added = 0; 43866b84079SAaron Smith auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 43966b84079SAaron Smith if (!compiland) 44066b84079SAaron Smith return 0; 44166b84079SAaron Smith 44266b84079SAaron Smith auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 44366b84079SAaron Smith std::unique_ptr<IPDBEnumSymbols> results; 44466b84079SAaron Smith PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 44566b84079SAaron Smith PDB_SymType::UDT}; 44666b84079SAaron Smith for (auto tag : tags_to_search) { 44766b84079SAaron Smith results = raw_sym.findAllChildren(tag); 44866b84079SAaron Smith if (!results || results->getChildCount() == 0) 44966b84079SAaron Smith continue; 45066b84079SAaron Smith while (auto symbol = results->getNext()) { 45166b84079SAaron Smith switch (symbol->getSymTag()) { 452ec40f818SAaron Smith case PDB_SymType::Enum: 453ec40f818SAaron Smith case PDB_SymType::UDT: 454ec40f818SAaron Smith case PDB_SymType::Typedef: 455ec40f818SAaron Smith break; 456ec40f818SAaron Smith default: 457ec40f818SAaron Smith continue; 458ec40f818SAaron Smith } 459ec40f818SAaron Smith 460ec40f818SAaron Smith // This should cause the type to get cached and stored in the `m_types` 461ec40f818SAaron Smith // lookup. 462*7d2a74fcSAleksandr Urakov if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { 463*7d2a74fcSAleksandr Urakov // Resolve the type completely to avoid a completion 464*7d2a74fcSAleksandr Urakov // (and so a list change, which causes an iterators invalidation) 465*7d2a74fcSAleksandr Urakov // during a TypeList dumping 466*7d2a74fcSAleksandr Urakov type->GetFullCompilerType(); 467ec40f818SAaron Smith ++num_added; 468ec40f818SAaron Smith } 46966b84079SAaron Smith } 470*7d2a74fcSAleksandr Urakov } 47166b84079SAaron Smith }; 47266b84079SAaron Smith 47366b84079SAaron Smith if (sc.function) { 474c8316ed2SAaron Smith auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 475c8316ed2SAaron Smith sc.function->GetID()); 47666b84079SAaron Smith if (!pdb_func) 47766b84079SAaron Smith return 0; 47866b84079SAaron Smith ParseTypesByTagFn(*pdb_func); 47966b84079SAaron Smith } else { 48066b84079SAaron Smith ParseTypesByTagFn(*compiland); 48166b84079SAaron Smith 48266b84079SAaron Smith // Also parse global types particularly coming from this compiland. 48305097246SAdrian Prantl // Unfortunately, PDB has no compiland information for each global type. We 48405097246SAdrian Prantl // have to parse them all. But ensure we only do this once. 48566b84079SAaron Smith static bool parse_all_global_types = false; 48666b84079SAaron Smith if (!parse_all_global_types) { 48766b84079SAaron Smith ParseTypesByTagFn(*m_global_scope_up); 48866b84079SAaron Smith parse_all_global_types = true; 48966b84079SAaron Smith } 49066b84079SAaron Smith } 491ec40f818SAaron Smith return num_added; 49274e08ca0SZachary Turner } 49374e08ca0SZachary Turner 49474e08ca0SZachary Turner size_t 495b9c1b51eSKate Stone SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 496cab0d23fSAaron Smith if (!sc.comp_unit) 497cab0d23fSAaron Smith return 0; 498cab0d23fSAaron Smith 499cab0d23fSAaron Smith size_t num_added = 0; 500cab0d23fSAaron Smith if (sc.function) { 501cab0d23fSAaron Smith auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 502cab0d23fSAaron Smith sc.function->GetID()); 503cab0d23fSAaron Smith if (!pdb_func) 504cab0d23fSAaron Smith return 0; 505cab0d23fSAaron Smith 506cab0d23fSAaron Smith num_added += ParseVariables(sc, *pdb_func); 507cab0d23fSAaron Smith sc.function->GetBlock(false).SetDidParseVariables(true, true); 508cab0d23fSAaron Smith } else if (sc.comp_unit) { 509cab0d23fSAaron Smith auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 510cab0d23fSAaron Smith if (!compiland) 511cab0d23fSAaron Smith return 0; 512cab0d23fSAaron Smith 513cab0d23fSAaron Smith if (sc.comp_unit->GetVariableList(false)) 514cab0d23fSAaron Smith return 0; 515cab0d23fSAaron Smith 516cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 517cab0d23fSAaron Smith if (results && results->getChildCount()) { 518cab0d23fSAaron Smith while (auto result = results->getNext()) { 519cab0d23fSAaron Smith auto cu_id = result->getCompilandId(); 520cab0d23fSAaron Smith // FIXME: We are not able to determine variable's compile unit. 521cab0d23fSAaron Smith if (cu_id == 0) 522cab0d23fSAaron Smith continue; 523cab0d23fSAaron Smith 524cab0d23fSAaron Smith if (cu_id == sc.comp_unit->GetID()) 525cab0d23fSAaron Smith num_added += ParseVariables(sc, *result); 526cab0d23fSAaron Smith } 527cab0d23fSAaron Smith } 528cab0d23fSAaron Smith 529cab0d23fSAaron Smith // FIXME: A `file static` or `global constant` variable appears both in 530cab0d23fSAaron Smith // compiland's children and global scope's children with unexpectedly 531cab0d23fSAaron Smith // different symbol's Id making it ambiguous. 532cab0d23fSAaron Smith 533cab0d23fSAaron Smith // FIXME: 'local constant', for example, const char var[] = "abc", declared 534cab0d23fSAaron Smith // in a function scope, can't be found in PDB. 535cab0d23fSAaron Smith 536cab0d23fSAaron Smith // Parse variables in this compiland. 537cab0d23fSAaron Smith num_added += ParseVariables(sc, *compiland); 538cab0d23fSAaron Smith } 539cab0d23fSAaron Smith 540cab0d23fSAaron Smith return num_added; 54174e08ca0SZachary Turner } 54274e08ca0SZachary Turner 543b9c1b51eSKate Stone lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 54442dff790SZachary Turner auto find_result = m_types.find(type_uid); 54542dff790SZachary Turner if (find_result != m_types.end()) 54642dff790SZachary Turner return find_result->second.get(); 54742dff790SZachary Turner 548b9c1b51eSKate Stone TypeSystem *type_system = 549b9c1b51eSKate Stone GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 550b9c1b51eSKate Stone ClangASTContext *clang_type_system = 551b9c1b51eSKate Stone llvm::dyn_cast_or_null<ClangASTContext>(type_system); 55242dff790SZachary Turner if (!clang_type_system) 55374e08ca0SZachary Turner return nullptr; 554b9c1b51eSKate Stone PDBASTParser *pdb = 555b9c1b51eSKate Stone llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); 55642dff790SZachary Turner if (!pdb) 55742dff790SZachary Turner return nullptr; 55842dff790SZachary Turner 55942dff790SZachary Turner auto pdb_type = m_session_up->getSymbolById(type_uid); 56042dff790SZachary Turner if (pdb_type == nullptr) 56142dff790SZachary Turner return nullptr; 56242dff790SZachary Turner 56342dff790SZachary Turner lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 564d5a925f4SAaron Smith if (result) { 56542dff790SZachary Turner m_types.insert(std::make_pair(type_uid, result)); 566ec40f818SAaron Smith auto type_list = GetTypeList(); 567f76fe682SAaron Smith if (type_list) 568ec40f818SAaron Smith type_list->Insert(result); 569ec40f818SAaron Smith } 57042dff790SZachary Turner return result.get(); 57174e08ca0SZachary Turner } 57274e08ca0SZachary Turner 573b9c1b51eSKate Stone bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 574*7d2a74fcSAleksandr Urakov std::lock_guard<std::recursive_mutex> guard( 575*7d2a74fcSAleksandr Urakov GetObjectFile()->GetModule()->GetMutex()); 576*7d2a74fcSAleksandr Urakov 577*7d2a74fcSAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 578*7d2a74fcSAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 579*7d2a74fcSAleksandr Urakov if (!clang_ast_ctx) 58074e08ca0SZachary Turner return false; 581*7d2a74fcSAleksandr Urakov 582*7d2a74fcSAleksandr Urakov PDBASTParser *pdb = 583*7d2a74fcSAleksandr Urakov llvm::dyn_cast<PDBASTParser>(clang_ast_ctx->GetPDBParser()); 584*7d2a74fcSAleksandr Urakov if (!pdb) 585*7d2a74fcSAleksandr Urakov return false; 586*7d2a74fcSAleksandr Urakov 587*7d2a74fcSAleksandr Urakov return pdb->CompleteTypeFromPDB(compiler_type); 58874e08ca0SZachary Turner } 58974e08ca0SZachary Turner 590b9c1b51eSKate Stone lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 59174e08ca0SZachary Turner return lldb_private::CompilerDecl(); 59274e08ca0SZachary Turner } 59374e08ca0SZachary Turner 59474e08ca0SZachary Turner lldb_private::CompilerDeclContext 595b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 596b9c1b51eSKate Stone // PDB always uses the translation unit decl context for everything. We can 5979d0eb996SAdrian McCarthy // improve this later but it's not easy because PDB doesn't provide a high 5989d0eb996SAdrian McCarthy // enough level of type fidelity in this area. 59942dff790SZachary Turner return *m_tu_decl_ctx_up; 60074e08ca0SZachary Turner } 60174e08ca0SZachary Turner 60274e08ca0SZachary Turner lldb_private::CompilerDeclContext 603b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 60442dff790SZachary Turner return *m_tu_decl_ctx_up; 60574e08ca0SZachary Turner } 60674e08ca0SZachary Turner 607b9c1b51eSKate Stone void SymbolFilePDB::ParseDeclsForContext( 608b9c1b51eSKate Stone lldb_private::CompilerDeclContext decl_ctx) {} 60974e08ca0SZachary Turner 61074e08ca0SZachary Turner uint32_t 611b9c1b51eSKate Stone SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 612b9c1b51eSKate Stone uint32_t resolve_scope, 613b9c1b51eSKate Stone lldb_private::SymbolContext &sc) { 6147ac1c780SAaron Smith uint32_t resolved_flags = 0; 6154d4d63eeSPavel Labath if (resolve_scope & eSymbolContextCompUnit || 6164d4d63eeSPavel Labath resolve_scope & eSymbolContextVariable || 6174d4d63eeSPavel Labath resolve_scope & eSymbolContextFunction || 6184d4d63eeSPavel Labath resolve_scope & eSymbolContextBlock || 6197ac1c780SAaron Smith resolve_scope & eSymbolContextLineEntry) { 6207ac1c780SAaron Smith auto cu_sp = GetCompileUnitContainsAddress(so_addr); 6217ac1c780SAaron Smith if (!cu_sp) { 6227ac1c780SAaron Smith if (resolved_flags | eSymbolContextVariable) { 6237ac1c780SAaron Smith // TODO: Resolve variables 6247ac1c780SAaron Smith } 6257ac1c780SAaron Smith return 0; 6267ac1c780SAaron Smith } 6277ac1c780SAaron Smith sc.comp_unit = cu_sp.get(); 6287ac1c780SAaron Smith resolved_flags |= eSymbolContextCompUnit; 6297ac1c780SAaron Smith lldbassert(sc.module_sp == cu_sp->GetModule()); 6309ea80d25SPavel Labath } 6317ac1c780SAaron Smith 6327ac1c780SAaron Smith if (resolve_scope & eSymbolContextFunction) { 6339ea80d25SPavel Labath addr_t file_vm_addr = so_addr.GetFileAddress(); 6349ea80d25SPavel Labath auto symbol_up = 6359ea80d25SPavel Labath m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); 6369ea80d25SPavel Labath if (symbol_up) { 6377ac1c780SAaron Smith auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 6387ac1c780SAaron Smith assert(pdb_func); 6397ac1c780SAaron Smith auto func_uid = pdb_func->getSymIndexId(); 6407ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 6417ac1c780SAaron Smith if (sc.function == nullptr) 642e664b5dcSAaron Smith sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); 6437ac1c780SAaron Smith if (sc.function) { 6447ac1c780SAaron Smith resolved_flags |= eSymbolContextFunction; 6457ac1c780SAaron Smith if (resolve_scope & eSymbolContextBlock) { 6467ac1c780SAaron Smith Block &block = sc.function->GetBlock(true); 6477ac1c780SAaron Smith sc.block = block.FindBlockByID(sc.function->GetID()); 6487ac1c780SAaron Smith if (sc.block) 6497ac1c780SAaron Smith resolved_flags |= eSymbolContextBlock; 6507ac1c780SAaron Smith } 6517ac1c780SAaron Smith } 6527ac1c780SAaron Smith } 6537ac1c780SAaron Smith } 6547ac1c780SAaron Smith 6557ac1c780SAaron Smith if (resolve_scope & eSymbolContextLineEntry) { 6567ac1c780SAaron Smith if (auto *line_table = sc.comp_unit->GetLineTable()) { 6577ac1c780SAaron Smith Address addr(so_addr); 6587ac1c780SAaron Smith if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 6597ac1c780SAaron Smith resolved_flags |= eSymbolContextLineEntry; 6607ac1c780SAaron Smith } 6617ac1c780SAaron Smith } 6629ea80d25SPavel Labath 6637ac1c780SAaron Smith return resolved_flags; 66474e08ca0SZachary Turner } 66574e08ca0SZachary Turner 666b9c1b51eSKate Stone uint32_t SymbolFilePDB::ResolveSymbolContext( 667b9c1b51eSKate Stone const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 668b9c1b51eSKate Stone uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) { 66910a02577SAaron Smith const size_t old_size = sc_list.GetSize(); 670b9c1b51eSKate Stone if (resolve_scope & lldb::eSymbolContextCompUnit) { 671b9c1b51eSKate Stone // Locate all compilation units with line numbers referencing the specified 6729d0eb996SAdrian McCarthy // file. For example, if `file_spec` is <vector>, then this should return 6739d0eb996SAdrian McCarthy // all source files and header files that reference <vector>, either 6749d0eb996SAdrian McCarthy // directly or indirectly. 675b9c1b51eSKate Stone auto compilands = m_session_up->findCompilandsForSourceFile( 676b9c1b51eSKate Stone file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 67774e08ca0SZachary Turner 67810a02577SAaron Smith if (!compilands) 67910a02577SAaron Smith return 0; 68010a02577SAaron Smith 6819d0eb996SAdrian McCarthy // For each one, either find its previously parsed data or parse it afresh 6829d0eb996SAdrian McCarthy // and add it to the symbol context list. 683b9c1b51eSKate Stone while (auto compiland = compilands->getNext()) { 68405097246SAdrian Prantl // If we're not checking inlines, then don't add line information for 68505097246SAdrian Prantl // this file unless the FileSpec matches. For inline functions, we don't 68605097246SAdrian Prantl // have to match the FileSpec since they could be defined in headers 68705097246SAdrian Prantl // other than file specified in FileSpec. 688b9c1b51eSKate Stone if (!check_inlines) { 689487b0c6bSAaron Smith std::string source_file = compiland->getSourceFileFullPath(); 69010a02577SAaron Smith if (source_file.empty()) 69110a02577SAaron Smith continue; 6922cb7cf8eSPavel Labath FileSpec this_spec(source_file, false, FileSpec::Style::windows); 69310a02577SAaron Smith bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 69410a02577SAaron Smith if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 69574e08ca0SZachary Turner continue; 69674e08ca0SZachary Turner } 69774e08ca0SZachary Turner 69874e08ca0SZachary Turner SymbolContext sc; 69910a02577SAaron Smith auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 700d5a925f4SAaron Smith if (!cu) 70110a02577SAaron Smith continue; 70274e08ca0SZachary Turner sc.comp_unit = cu.get(); 70374e08ca0SZachary Turner sc.module_sp = cu->GetModule(); 70474e08ca0SZachary Turner 705b9c1b51eSKate Stone // If we were asked to resolve line entries, add all entries to the line 7069d0eb996SAdrian McCarthy // table that match the requested line (or all lines if `line` == 0). 7077ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 7087ac1c780SAaron Smith eSymbolContextLineEntry)) { 7097ac1c780SAaron Smith bool has_line_table = ParseCompileUnitLineTable(sc, line); 7107ac1c780SAaron Smith 7117ac1c780SAaron Smith if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 7127ac1c780SAaron Smith // The query asks for line entries, but we can't get them for the 71305097246SAdrian Prantl // compile unit. This is not normal for `line` = 0. So just assert 71405097246SAdrian Prantl // it. 715f76fe682SAaron Smith assert(line && "Couldn't get all line entries!\n"); 7167ac1c780SAaron Smith 7177ac1c780SAaron Smith // Current compiland does not have the requested line. Search next. 7187ac1c780SAaron Smith continue; 7197ac1c780SAaron Smith } 7207ac1c780SAaron Smith 7217ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 7227ac1c780SAaron Smith if (!has_line_table) 7237ac1c780SAaron Smith continue; 7247ac1c780SAaron Smith 7257ac1c780SAaron Smith auto *line_table = sc.comp_unit->GetLineTable(); 7267ac1c780SAaron Smith lldbassert(line_table); 7277ac1c780SAaron Smith 7287ac1c780SAaron Smith uint32_t num_line_entries = line_table->GetSize(); 7297ac1c780SAaron Smith // Skip the terminal line entry. 7307ac1c780SAaron Smith --num_line_entries; 7317ac1c780SAaron Smith 73205097246SAdrian Prantl // If `line `!= 0, see if we can resolve function for each line entry 73305097246SAdrian Prantl // in the line table. 7347ac1c780SAaron Smith for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 7357ac1c780SAaron Smith ++line_idx) { 7367ac1c780SAaron Smith if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 7377ac1c780SAaron Smith continue; 7387ac1c780SAaron Smith 7397ac1c780SAaron Smith auto file_vm_addr = 7407ac1c780SAaron Smith sc.line_entry.range.GetBaseAddress().GetFileAddress(); 741308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 7427ac1c780SAaron Smith continue; 7437ac1c780SAaron Smith 744c8316ed2SAaron Smith auto symbol_up = m_session_up->findSymbolByAddress( 745c8316ed2SAaron Smith file_vm_addr, PDB_SymType::Function); 7467ac1c780SAaron Smith if (symbol_up) { 7477ac1c780SAaron Smith auto func_uid = symbol_up->getSymIndexId(); 7487ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 7497ac1c780SAaron Smith if (sc.function == nullptr) { 7507ac1c780SAaron Smith auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 7517ac1c780SAaron Smith assert(pdb_func); 752e664b5dcSAaron Smith sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); 7537ac1c780SAaron Smith } 7547ac1c780SAaron Smith if (sc.function && (resolve_scope & eSymbolContextBlock)) { 7557ac1c780SAaron Smith Block &block = sc.function->GetBlock(true); 7567ac1c780SAaron Smith sc.block = block.FindBlockByID(sc.function->GetID()); 7577ac1c780SAaron Smith } 7587ac1c780SAaron Smith } 7597ac1c780SAaron Smith sc_list.Append(sc); 7607ac1c780SAaron Smith } 7617ac1c780SAaron Smith } else if (has_line_table) { 7627ac1c780SAaron Smith // We can parse line table for the compile unit. But no query to 7637ac1c780SAaron Smith // resolve function or block. We append `sc` to the list anyway. 7647ac1c780SAaron Smith sc_list.Append(sc); 7657ac1c780SAaron Smith } 7667ac1c780SAaron Smith } else { 7677ac1c780SAaron Smith // No query for line entry, function or block. But we have a valid 7687ac1c780SAaron Smith // compile unit, append `sc` to the list. 7697ac1c780SAaron Smith sc_list.Append(sc); 7707ac1c780SAaron Smith } 77174e08ca0SZachary Turner } 77274e08ca0SZachary Turner } 77310a02577SAaron Smith return sc_list.GetSize() - old_size; 77474e08ca0SZachary Turner } 77574e08ca0SZachary Turner 776cab0d23fSAaron Smith std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { 777cab0d23fSAaron Smith std::string decorated_name; 778cab0d23fSAaron Smith auto vm_addr = pdb_data.getVirtualAddress(); 779cab0d23fSAaron Smith if (vm_addr != LLDB_INVALID_ADDRESS && vm_addr) { 780cab0d23fSAaron Smith auto result_up = 781cab0d23fSAaron Smith m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol); 782cab0d23fSAaron Smith if (result_up) { 783cab0d23fSAaron Smith while (auto symbol_up = result_up->getNext()) { 784cab0d23fSAaron Smith if (symbol_up->getRawSymbol().getVirtualAddress() == vm_addr) { 785cab0d23fSAaron Smith decorated_name = symbol_up->getRawSymbol().getName(); 786cab0d23fSAaron Smith break; 787cab0d23fSAaron Smith } 788cab0d23fSAaron Smith } 789cab0d23fSAaron Smith } 790cab0d23fSAaron Smith } 791cab0d23fSAaron Smith if (!decorated_name.empty()) 792cab0d23fSAaron Smith return decorated_name; 793cab0d23fSAaron Smith 794cab0d23fSAaron Smith return std::string(); 795cab0d23fSAaron Smith } 796cab0d23fSAaron Smith 797cab0d23fSAaron Smith VariableSP SymbolFilePDB::ParseVariableForPDBData( 798cab0d23fSAaron Smith const lldb_private::SymbolContext &sc, 799cab0d23fSAaron Smith const llvm::pdb::PDBSymbolData &pdb_data) { 800cab0d23fSAaron Smith VariableSP var_sp; 801cab0d23fSAaron Smith uint32_t var_uid = pdb_data.getSymIndexId(); 802cab0d23fSAaron Smith auto result = m_variables.find(var_uid); 803cab0d23fSAaron Smith if (result != m_variables.end()) 804cab0d23fSAaron Smith return result->second; 805cab0d23fSAaron Smith 806cab0d23fSAaron Smith ValueType scope = eValueTypeInvalid; 807cab0d23fSAaron Smith bool is_static_member = false; 808cab0d23fSAaron Smith bool is_external = false; 809cab0d23fSAaron Smith bool is_artificial = false; 810cab0d23fSAaron Smith 811cab0d23fSAaron Smith switch (pdb_data.getDataKind()) { 812cab0d23fSAaron Smith case PDB_DataKind::Global: 813cab0d23fSAaron Smith scope = eValueTypeVariableGlobal; 814cab0d23fSAaron Smith is_external = true; 815cab0d23fSAaron Smith break; 816cab0d23fSAaron Smith case PDB_DataKind::Local: 817cab0d23fSAaron Smith scope = eValueTypeVariableLocal; 818cab0d23fSAaron Smith break; 819cab0d23fSAaron Smith case PDB_DataKind::FileStatic: 820cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 821cab0d23fSAaron Smith break; 822cab0d23fSAaron Smith case PDB_DataKind::StaticMember: 823cab0d23fSAaron Smith is_static_member = true; 824cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 825cab0d23fSAaron Smith break; 826cab0d23fSAaron Smith case PDB_DataKind::Member: 827cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 828cab0d23fSAaron Smith break; 829cab0d23fSAaron Smith case PDB_DataKind::Param: 830cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 831cab0d23fSAaron Smith break; 832cab0d23fSAaron Smith case PDB_DataKind::Constant: 833cab0d23fSAaron Smith scope = eValueTypeConstResult; 834cab0d23fSAaron Smith break; 835cab0d23fSAaron Smith default: 836cab0d23fSAaron Smith break; 837cab0d23fSAaron Smith } 838cab0d23fSAaron Smith 839cab0d23fSAaron Smith switch (pdb_data.getLocationType()) { 840cab0d23fSAaron Smith case PDB_LocType::TLS: 841cab0d23fSAaron Smith scope = eValueTypeVariableThreadLocal; 842cab0d23fSAaron Smith break; 843cab0d23fSAaron Smith case PDB_LocType::RegRel: { 844cab0d23fSAaron Smith // It is a `this` pointer. 845cab0d23fSAaron Smith if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { 846cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 847cab0d23fSAaron Smith is_artificial = true; 848cab0d23fSAaron Smith } 849cab0d23fSAaron Smith } break; 850cab0d23fSAaron Smith default: 851cab0d23fSAaron Smith break; 852cab0d23fSAaron Smith } 853cab0d23fSAaron Smith 854cab0d23fSAaron Smith Declaration decl; 855cab0d23fSAaron Smith if (!is_artificial && !pdb_data.isCompilerGenerated()) { 856cab0d23fSAaron Smith if (auto lines = pdb_data.getLineNumbers()) { 857cab0d23fSAaron Smith if (auto first_line = lines->getNext()) { 858cab0d23fSAaron Smith uint32_t src_file_id = first_line->getSourceFileId(); 859cab0d23fSAaron Smith auto src_file = m_session_up->getSourceFileById(src_file_id); 860cab0d23fSAaron Smith if (src_file) { 861cab0d23fSAaron Smith FileSpec spec(src_file->getFileName(), /*resolve_path*/ false); 862cab0d23fSAaron Smith decl.SetFile(spec); 863cab0d23fSAaron Smith decl.SetColumn(first_line->getColumnNumber()); 864cab0d23fSAaron Smith decl.SetLine(first_line->getLineNumber()); 865cab0d23fSAaron Smith } 866cab0d23fSAaron Smith } 867cab0d23fSAaron Smith } 868cab0d23fSAaron Smith } 869cab0d23fSAaron Smith 870cab0d23fSAaron Smith Variable::RangeList ranges; 871cab0d23fSAaron Smith SymbolContextScope *context_scope = sc.comp_unit; 872cab0d23fSAaron Smith if (scope == eValueTypeVariableLocal) { 873cab0d23fSAaron Smith if (sc.function) { 874cab0d23fSAaron Smith context_scope = sc.function->GetBlock(true).FindBlockByID( 875cab0d23fSAaron Smith pdb_data.getClassParentId()); 876cab0d23fSAaron Smith if (context_scope == nullptr) 877cab0d23fSAaron Smith context_scope = sc.function; 878cab0d23fSAaron Smith } 879cab0d23fSAaron Smith } 880cab0d23fSAaron Smith 881cab0d23fSAaron Smith SymbolFileTypeSP type_sp = 882cab0d23fSAaron Smith std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); 883cab0d23fSAaron Smith 884cab0d23fSAaron Smith auto var_name = pdb_data.getName(); 885cab0d23fSAaron Smith auto mangled = GetMangledForPDBData(pdb_data); 886cab0d23fSAaron Smith auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); 887cab0d23fSAaron Smith 888924d5608SJonas Devlieghere bool is_constant; 889924d5608SJonas Devlieghere DWARFExpression location = ConvertPDBLocationToDWARFExpression( 890924d5608SJonas Devlieghere GetObjectFile()->GetModule(), pdb_data, is_constant); 891cab0d23fSAaron Smith 892cab0d23fSAaron Smith var_sp = std::make_shared<Variable>( 893cab0d23fSAaron Smith var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, 894cab0d23fSAaron Smith ranges, &decl, location, is_external, is_artificial, is_static_member); 895924d5608SJonas Devlieghere var_sp->SetLocationIsConstantValueData(is_constant); 896cab0d23fSAaron Smith 897cab0d23fSAaron Smith m_variables.insert(std::make_pair(var_uid, var_sp)); 898cab0d23fSAaron Smith return var_sp; 899cab0d23fSAaron Smith } 900cab0d23fSAaron Smith 901cab0d23fSAaron Smith size_t 902cab0d23fSAaron Smith SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, 903cab0d23fSAaron Smith const llvm::pdb::PDBSymbol &pdb_symbol, 904cab0d23fSAaron Smith lldb_private::VariableList *variable_list) { 905cab0d23fSAaron Smith size_t num_added = 0; 906cab0d23fSAaron Smith 907cab0d23fSAaron Smith if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { 908cab0d23fSAaron Smith VariableListSP local_variable_list_sp; 909cab0d23fSAaron Smith 910cab0d23fSAaron Smith auto result = m_variables.find(pdb_data->getSymIndexId()); 911cab0d23fSAaron Smith if (result != m_variables.end()) { 912cab0d23fSAaron Smith if (variable_list) 913cab0d23fSAaron Smith variable_list->AddVariableIfUnique(result->second); 914cab0d23fSAaron Smith } else { 915cab0d23fSAaron Smith // Prepare right VariableList for this variable. 916cab0d23fSAaron Smith if (auto lexical_parent = pdb_data->getLexicalParent()) { 917cab0d23fSAaron Smith switch (lexical_parent->getSymTag()) { 918cab0d23fSAaron Smith case PDB_SymType::Exe: 919cab0d23fSAaron Smith assert(sc.comp_unit); 920cab0d23fSAaron Smith LLVM_FALLTHROUGH; 921cab0d23fSAaron Smith case PDB_SymType::Compiland: { 922cab0d23fSAaron Smith if (sc.comp_unit) { 923cab0d23fSAaron Smith local_variable_list_sp = sc.comp_unit->GetVariableList(false); 924cab0d23fSAaron Smith if (!local_variable_list_sp) { 925cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 926cab0d23fSAaron Smith sc.comp_unit->SetVariableList(local_variable_list_sp); 927cab0d23fSAaron Smith } 928cab0d23fSAaron Smith } 929cab0d23fSAaron Smith } break; 930cab0d23fSAaron Smith case PDB_SymType::Block: 931cab0d23fSAaron Smith case PDB_SymType::Function: { 932cab0d23fSAaron Smith if (sc.function) { 933cab0d23fSAaron Smith Block *block = sc.function->GetBlock(true).FindBlockByID( 934cab0d23fSAaron Smith lexical_parent->getSymIndexId()); 935cab0d23fSAaron Smith if (block) { 936cab0d23fSAaron Smith local_variable_list_sp = block->GetBlockVariableList(false); 937cab0d23fSAaron Smith if (!local_variable_list_sp) { 938cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 939cab0d23fSAaron Smith block->SetVariableList(local_variable_list_sp); 940cab0d23fSAaron Smith } 941cab0d23fSAaron Smith } 942cab0d23fSAaron Smith } 943cab0d23fSAaron Smith } break; 944cab0d23fSAaron Smith default: 945cab0d23fSAaron Smith break; 946cab0d23fSAaron Smith } 947cab0d23fSAaron Smith } 948cab0d23fSAaron Smith 949cab0d23fSAaron Smith if (local_variable_list_sp) { 950cab0d23fSAaron Smith if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { 951cab0d23fSAaron Smith local_variable_list_sp->AddVariableIfUnique(var_sp); 952cab0d23fSAaron Smith if (variable_list) 953cab0d23fSAaron Smith variable_list->AddVariableIfUnique(var_sp); 954cab0d23fSAaron Smith ++num_added; 955cab0d23fSAaron Smith } 956cab0d23fSAaron Smith } 957cab0d23fSAaron Smith } 958cab0d23fSAaron Smith } 959cab0d23fSAaron Smith 960cab0d23fSAaron Smith if (auto results = pdb_symbol.findAllChildren()) { 961cab0d23fSAaron Smith while (auto result = results->getNext()) 962cab0d23fSAaron Smith num_added += ParseVariables(sc, *result, variable_list); 963cab0d23fSAaron Smith } 964cab0d23fSAaron Smith 965cab0d23fSAaron Smith return num_added; 966cab0d23fSAaron Smith } 967cab0d23fSAaron Smith 968b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindGlobalVariables( 969b9c1b51eSKate Stone const lldb_private::ConstString &name, 97034cda14bSPavel Labath const lldb_private::CompilerDeclContext *parent_decl_ctx, 971b9c1b51eSKate Stone uint32_t max_matches, lldb_private::VariableList &variables) { 972cab0d23fSAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 973cab0d23fSAaron Smith return 0; 974cab0d23fSAaron Smith if (name.IsEmpty()) 975cab0d23fSAaron Smith return 0; 976cab0d23fSAaron Smith 977cab0d23fSAaron Smith auto results = 978cab0d23fSAaron Smith m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(), 979cab0d23fSAaron Smith PDB_NameSearchFlags::NS_CaseSensitive); 980cab0d23fSAaron Smith if (!results) 981cab0d23fSAaron Smith return 0; 982cab0d23fSAaron Smith 983cab0d23fSAaron Smith uint32_t matches = 0; 984cab0d23fSAaron Smith size_t old_size = variables.GetSize(); 985cab0d23fSAaron Smith while (auto result = results->getNext()) { 986cab0d23fSAaron Smith auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get()); 987cab0d23fSAaron Smith if (max_matches > 0 && matches >= max_matches) 988cab0d23fSAaron Smith break; 989cab0d23fSAaron Smith 990cab0d23fSAaron Smith SymbolContext sc; 991cab0d23fSAaron Smith sc.module_sp = m_obj_file->GetModule(); 992cab0d23fSAaron Smith lldbassert(sc.module_sp.get()); 993cab0d23fSAaron Smith 994cab0d23fSAaron Smith sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get(); 995cab0d23fSAaron Smith // FIXME: We are not able to determine the compile unit. 996cab0d23fSAaron Smith if (sc.comp_unit == nullptr) 997cab0d23fSAaron Smith continue; 998cab0d23fSAaron Smith 999cab0d23fSAaron Smith ParseVariables(sc, *pdb_data, &variables); 1000cab0d23fSAaron Smith matches = variables.GetSize() - old_size; 1001cab0d23fSAaron Smith } 1002cab0d23fSAaron Smith 1003cab0d23fSAaron Smith return matches; 100474e08ca0SZachary Turner } 100574e08ca0SZachary Turner 100674e08ca0SZachary Turner uint32_t 1007b9c1b51eSKate Stone SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, 100834cda14bSPavel Labath uint32_t max_matches, 1009b9c1b51eSKate Stone lldb_private::VariableList &variables) { 1010cab0d23fSAaron Smith if (!regex.IsValid()) 1011cab0d23fSAaron Smith return 0; 1012cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1013cab0d23fSAaron Smith if (!results) 1014cab0d23fSAaron Smith return 0; 1015cab0d23fSAaron Smith 1016cab0d23fSAaron Smith uint32_t matches = 0; 1017cab0d23fSAaron Smith size_t old_size = variables.GetSize(); 1018cab0d23fSAaron Smith while (auto pdb_data = results->getNext()) { 1019cab0d23fSAaron Smith if (max_matches > 0 && matches >= max_matches) 1020cab0d23fSAaron Smith break; 1021cab0d23fSAaron Smith 1022cab0d23fSAaron Smith auto var_name = pdb_data->getName(); 1023cab0d23fSAaron Smith if (var_name.empty()) 1024cab0d23fSAaron Smith continue; 1025cab0d23fSAaron Smith if (!regex.Execute(var_name)) 1026cab0d23fSAaron Smith continue; 1027cab0d23fSAaron Smith SymbolContext sc; 1028cab0d23fSAaron Smith sc.module_sp = m_obj_file->GetModule(); 1029cab0d23fSAaron Smith lldbassert(sc.module_sp.get()); 1030cab0d23fSAaron Smith 1031cab0d23fSAaron Smith sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get(); 1032cab0d23fSAaron Smith // FIXME: We are not able to determine the compile unit. 1033cab0d23fSAaron Smith if (sc.comp_unit == nullptr) 1034cab0d23fSAaron Smith continue; 1035cab0d23fSAaron Smith 1036cab0d23fSAaron Smith ParseVariables(sc, *pdb_data, &variables); 1037cab0d23fSAaron Smith matches = variables.GetSize() - old_size; 1038cab0d23fSAaron Smith } 1039cab0d23fSAaron Smith 1040cab0d23fSAaron Smith return matches; 1041b9c1b51eSKate Stone } 1042b9c1b51eSKate Stone 1043e664b5dcSAaron Smith bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 10447ac1c780SAaron Smith bool include_inlines, 10457ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 10467ac1c780SAaron Smith lldb_private::SymbolContext sc; 1047a3a8cc80SAaron Smith sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 10487ac1c780SAaron Smith if (!sc.comp_unit) 10497ac1c780SAaron Smith return false; 10507ac1c780SAaron Smith sc.module_sp = sc.comp_unit->GetModule(); 1051a3a8cc80SAaron Smith sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); 10527ac1c780SAaron Smith if (!sc.function) 10537ac1c780SAaron Smith return false; 10547ac1c780SAaron Smith 10557ac1c780SAaron Smith sc_list.Append(sc); 10567ac1c780SAaron Smith return true; 10577ac1c780SAaron Smith } 10587ac1c780SAaron Smith 10597ac1c780SAaron Smith bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 10607ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 1061c8316ed2SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 10627ac1c780SAaron Smith if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 10637ac1c780SAaron Smith return false; 1064e664b5dcSAaron Smith return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 10657ac1c780SAaron Smith } 10667ac1c780SAaron Smith 10677ac1c780SAaron Smith void SymbolFilePDB::CacheFunctionNames() { 10687ac1c780SAaron Smith if (!m_func_full_names.IsEmpty()) 10697ac1c780SAaron Smith return; 10707ac1c780SAaron Smith 10717ac1c780SAaron Smith std::map<uint64_t, uint32_t> addr_ids; 10727ac1c780SAaron Smith 10737ac1c780SAaron Smith if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 10747ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 1075f76fe682SAaron Smith if (pdb_func_up->isCompilerGenerated()) 1076f76fe682SAaron Smith continue; 1077f76fe682SAaron Smith 10787ac1c780SAaron Smith auto name = pdb_func_up->getName(); 10797ac1c780SAaron Smith auto demangled_name = pdb_func_up->getUndecoratedName(); 10807ac1c780SAaron Smith if (name.empty() && demangled_name.empty()) 10817ac1c780SAaron Smith continue; 10827ac1c780SAaron Smith 1083f76fe682SAaron Smith auto uid = pdb_func_up->getSymIndexId(); 10847ac1c780SAaron Smith if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 10857ac1c780SAaron Smith addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 10867ac1c780SAaron Smith 10877ac1c780SAaron Smith if (auto parent = pdb_func_up->getClassParent()) { 10887ac1c780SAaron Smith 10897ac1c780SAaron Smith // PDB have symbols for class/struct methods or static methods in Enum 10907ac1c780SAaron Smith // Class. We won't bother to check if the parent is UDT or Enum here. 10917ac1c780SAaron Smith m_func_method_names.Append(ConstString(name), uid); 10927ac1c780SAaron Smith 10937ac1c780SAaron Smith ConstString cstr_name(name); 10947ac1c780SAaron Smith 109505097246SAdrian Prantl // To search a method name, like NS::Class:MemberFunc, LLDB searches 109605097246SAdrian Prantl // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does 109705097246SAdrian Prantl // not have inforamtion of this, we extract base names and cache them 109805097246SAdrian Prantl // by our own effort. 10997ac1c780SAaron Smith llvm::StringRef basename; 11007ac1c780SAaron Smith CPlusPlusLanguage::MethodName cpp_method(cstr_name); 11017ac1c780SAaron Smith if (cpp_method.IsValid()) { 11027ac1c780SAaron Smith llvm::StringRef context; 11037ac1c780SAaron Smith basename = cpp_method.GetBasename(); 11047ac1c780SAaron Smith if (basename.empty()) 11057ac1c780SAaron Smith CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(), 11067ac1c780SAaron Smith context, basename); 11077ac1c780SAaron Smith } 11087ac1c780SAaron Smith 11097ac1c780SAaron Smith if (!basename.empty()) 11107ac1c780SAaron Smith m_func_base_names.Append(ConstString(basename), uid); 11117ac1c780SAaron Smith else { 11127ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 11137ac1c780SAaron Smith } 11147ac1c780SAaron Smith 11157ac1c780SAaron Smith if (!demangled_name.empty()) 11167ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 11177ac1c780SAaron Smith 11187ac1c780SAaron Smith } else { 11197ac1c780SAaron Smith // Handle not-method symbols. 11207ac1c780SAaron Smith 11217ac1c780SAaron Smith // The function name might contain namespace, or its lexical scope. It 11227ac1c780SAaron Smith // is not safe to get its base name by applying same scheme as we deal 11237ac1c780SAaron Smith // with the method names. 11247ac1c780SAaron Smith // FIXME: Remove namespace if function is static in a scope. 11257ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 11267ac1c780SAaron Smith 11277ac1c780SAaron Smith if (name == "main") { 11287ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 11297ac1c780SAaron Smith 11307ac1c780SAaron Smith if (!demangled_name.empty() && name != demangled_name) { 11317ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 11327ac1c780SAaron Smith m_func_base_names.Append(ConstString(demangled_name), uid); 11337ac1c780SAaron Smith } 11347ac1c780SAaron Smith } else if (!demangled_name.empty()) { 11357ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 11367ac1c780SAaron Smith } else { 11377ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 11387ac1c780SAaron Smith } 11397ac1c780SAaron Smith } 11407ac1c780SAaron Smith } 11417ac1c780SAaron Smith } 11427ac1c780SAaron Smith 11437ac1c780SAaron Smith if (auto results_up = 11447ac1c780SAaron Smith m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 11457ac1c780SAaron Smith while (auto pub_sym_up = results_up->getNext()) { 11467ac1c780SAaron Smith if (!pub_sym_up->isFunction()) 11477ac1c780SAaron Smith continue; 11487ac1c780SAaron Smith auto name = pub_sym_up->getName(); 11497ac1c780SAaron Smith if (name.empty()) 11507ac1c780SAaron Smith continue; 11517ac1c780SAaron Smith 11527ac1c780SAaron Smith if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 11537ac1c780SAaron Smith auto vm_addr = pub_sym_up->getVirtualAddress(); 11547ac1c780SAaron Smith 11557ac1c780SAaron Smith // PDB public symbol has mangled name for its associated function. 11567ac1c780SAaron Smith if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { 11577ac1c780SAaron Smith // Cache mangled name. 11587ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); 11597ac1c780SAaron Smith } 11607ac1c780SAaron Smith } 11617ac1c780SAaron Smith } 11627ac1c780SAaron Smith } 11637ac1c780SAaron Smith // Sort them before value searching is working properly 11647ac1c780SAaron Smith m_func_full_names.Sort(); 11657ac1c780SAaron Smith m_func_full_names.SizeToFit(); 11667ac1c780SAaron Smith m_func_method_names.Sort(); 11677ac1c780SAaron Smith m_func_method_names.SizeToFit(); 11687ac1c780SAaron Smith m_func_base_names.Sort(); 11697ac1c780SAaron Smith m_func_base_names.SizeToFit(); 11707ac1c780SAaron Smith } 11717ac1c780SAaron Smith 1172b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindFunctions( 1173b9c1b51eSKate Stone const lldb_private::ConstString &name, 1174b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, 1175b9c1b51eSKate Stone uint32_t name_type_mask, bool include_inlines, bool append, 1176b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 11777ac1c780SAaron Smith if (!append) 11787ac1c780SAaron Smith sc_list.Clear(); 11797ac1c780SAaron Smith lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 11807ac1c780SAaron Smith 11817ac1c780SAaron Smith if (name_type_mask == eFunctionNameTypeNone) 11827ac1c780SAaron Smith return 0; 11837ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 11847ac1c780SAaron Smith return 0; 11857ac1c780SAaron Smith if (name.IsEmpty()) 11867ac1c780SAaron Smith return 0; 11877ac1c780SAaron Smith 11887ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 11894d4d63eeSPavel Labath if (name_type_mask & eFunctionNameTypeFull || 11904d4d63eeSPavel Labath name_type_mask & eFunctionNameTypeBase || 11917ac1c780SAaron Smith name_type_mask & eFunctionNameTypeMethod) { 11927ac1c780SAaron Smith CacheFunctionNames(); 11937ac1c780SAaron Smith 11947ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1195c8316ed2SAaron Smith auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, 1196c8316ed2SAaron Smith this](UniqueCStringMap<uint32_t> &Names) { 11977ac1c780SAaron Smith std::vector<uint32_t> ids; 11987ac1c780SAaron Smith if (Names.GetValues(name, ids)) { 11997ac1c780SAaron Smith for (auto id : ids) { 12007ac1c780SAaron Smith if (resolved_ids.find(id) == resolved_ids.end()) { 12017ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 12027ac1c780SAaron Smith resolved_ids.insert(id); 12037ac1c780SAaron Smith } 12047ac1c780SAaron Smith } 12057ac1c780SAaron Smith } 12067ac1c780SAaron Smith }; 12077ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeFull) { 12087ac1c780SAaron Smith ResolveFn(m_func_full_names); 12097ac1c780SAaron Smith } 12107ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeBase) { 12117ac1c780SAaron Smith ResolveFn(m_func_base_names); 12127ac1c780SAaron Smith } 12137ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeMethod) { 12147ac1c780SAaron Smith ResolveFn(m_func_method_names); 12157ac1c780SAaron Smith } 12167ac1c780SAaron Smith } 12177ac1c780SAaron Smith return sc_list.GetSize() - old_size; 121874e08ca0SZachary Turner } 121974e08ca0SZachary Turner 122074e08ca0SZachary Turner uint32_t 1221b9c1b51eSKate Stone SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 1222b9c1b51eSKate Stone bool include_inlines, bool append, 1223b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 12247ac1c780SAaron Smith if (!append) 12257ac1c780SAaron Smith sc_list.Clear(); 12267ac1c780SAaron Smith if (!regex.IsValid()) 12277ac1c780SAaron Smith return 0; 12287ac1c780SAaron Smith 12297ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 12307ac1c780SAaron Smith CacheFunctionNames(); 12317ac1c780SAaron Smith 12327ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1233c8316ed2SAaron Smith auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, 1234c8316ed2SAaron Smith this](UniqueCStringMap<uint32_t> &Names) { 12357ac1c780SAaron Smith std::vector<uint32_t> ids; 12367ac1c780SAaron Smith if (Names.GetValues(regex, ids)) { 12377ac1c780SAaron Smith for (auto id : ids) { 12387ac1c780SAaron Smith if (resolved_ids.find(id) == resolved_ids.end()) 12397ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 12407ac1c780SAaron Smith resolved_ids.insert(id); 12417ac1c780SAaron Smith } 12427ac1c780SAaron Smith } 12437ac1c780SAaron Smith }; 12447ac1c780SAaron Smith ResolveFn(m_func_full_names); 12457ac1c780SAaron Smith ResolveFn(m_func_base_names); 12467ac1c780SAaron Smith 12477ac1c780SAaron Smith return sc_list.GetSize() - old_size; 124874e08ca0SZachary Turner } 124974e08ca0SZachary Turner 1250b9c1b51eSKate Stone void SymbolFilePDB::GetMangledNamesForFunction( 1251b9c1b51eSKate Stone const std::string &scope_qualified_name, 1252b9c1b51eSKate Stone std::vector<lldb_private::ConstString> &mangled_names) {} 125374e08ca0SZachary Turner 1254b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindTypes( 1255b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 1256b9c1b51eSKate Stone const lldb_private::ConstString &name, 1257b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 1258b9c1b51eSKate Stone uint32_t max_matches, 125974e08ca0SZachary Turner llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1260b9c1b51eSKate Stone lldb_private::TypeMap &types) { 126142dff790SZachary Turner if (!append) 126242dff790SZachary Turner types.Clear(); 126342dff790SZachary Turner if (!name) 126442dff790SZachary Turner return 0; 12657ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 12667ac1c780SAaron Smith return 0; 126742dff790SZachary Turner 126842dff790SZachary Turner searched_symbol_files.clear(); 126942dff790SZachary Turner searched_symbol_files.insert(this); 127042dff790SZachary Turner 127142dff790SZachary Turner std::string name_str = name.AsCString(); 127242dff790SZachary Turner 127386e9434dSAaron Smith // There is an assumption 'name' is not a regex 127442dff790SZachary Turner FindTypesByName(name_str, max_matches, types); 127586e9434dSAaron Smith 127642dff790SZachary Turner return types.GetSize(); 127742dff790SZachary Turner } 127842dff790SZachary Turner 1279c8316ed2SAaron Smith void SymbolFilePDB::FindTypesByRegex( 1280c8316ed2SAaron Smith const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1281b9c1b51eSKate Stone lldb_private::TypeMap &types) { 1282b9c1b51eSKate Stone // When searching by regex, we need to go out of our way to limit the search 12839d0eb996SAdrian McCarthy // space as much as possible since this searches EVERYTHING in the PDB, 12849d0eb996SAdrian McCarthy // manually doing regex comparisons. PDB library isn't optimized for regex 12859d0eb996SAdrian McCarthy // searches or searches across multiple symbol types at the same time, so the 1286b9c1b51eSKate Stone // best we can do is to search enums, then typedefs, then classes one by one, 12879d0eb996SAdrian McCarthy // and do a regex comparison against each of them. 1288b9c1b51eSKate Stone PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 1289b9c1b51eSKate Stone PDB_SymType::UDT}; 129054fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 129142dff790SZachary Turner 129242dff790SZachary Turner uint32_t matches = 0; 129342dff790SZachary Turner 1294b9c1b51eSKate Stone for (auto tag : tags_to_search) { 129510a02577SAaron Smith results = m_global_scope_up->findAllChildren(tag); 129610a02577SAaron Smith if (!results) 129710a02577SAaron Smith continue; 129810a02577SAaron Smith 1299b9c1b51eSKate Stone while (auto result = results->getNext()) { 130042dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 130142dff790SZachary Turner break; 130242dff790SZachary Turner 130342dff790SZachary Turner std::string type_name; 130454fd7ff6SZachary Turner if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 130542dff790SZachary Turner type_name = enum_type->getName(); 1306b9c1b51eSKate Stone else if (auto typedef_type = 1307b9c1b51eSKate Stone llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 130842dff790SZachary Turner type_name = typedef_type->getName(); 130954fd7ff6SZachary Turner else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 131042dff790SZachary Turner type_name = class_type->getName(); 1311b9c1b51eSKate Stone else { 13129d0eb996SAdrian McCarthy // We're looking only for types that have names. Skip symbols, as well 13139d0eb996SAdrian McCarthy // as unnamed types such as arrays, pointers, etc. 131442dff790SZachary Turner continue; 131542dff790SZachary Turner } 131642dff790SZachary Turner 131786e9434dSAaron Smith if (!regex.Execute(type_name)) 131842dff790SZachary Turner continue; 131942dff790SZachary Turner 1320b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1321b9c1b51eSKate Stone // lookup. 132242dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 132342dff790SZachary Turner continue; 132442dff790SZachary Turner 132542dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 132642dff790SZachary Turner if (iter == m_types.end()) 132742dff790SZachary Turner continue; 132842dff790SZachary Turner types.Insert(iter->second); 132942dff790SZachary Turner ++matches; 133042dff790SZachary Turner } 133142dff790SZachary Turner } 133242dff790SZachary Turner } 133342dff790SZachary Turner 1334b9c1b51eSKate Stone void SymbolFilePDB::FindTypesByName(const std::string &name, 1335b9c1b51eSKate Stone uint32_t max_matches, 1336b9c1b51eSKate Stone lldb_private::TypeMap &types) { 133754fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 1338f76fe682SAaron Smith if (name.empty()) 1339f76fe682SAaron Smith return; 134010a02577SAaron Smith results = m_global_scope_up->findChildren(PDB_SymType::None, name, 1341b9c1b51eSKate Stone PDB_NameSearchFlags::NS_Default); 134210a02577SAaron Smith if (!results) 134310a02577SAaron Smith return; 134442dff790SZachary Turner 134542dff790SZachary Turner uint32_t matches = 0; 134642dff790SZachary Turner 1347b9c1b51eSKate Stone while (auto result = results->getNext()) { 134842dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 134942dff790SZachary Turner break; 1350b9c1b51eSKate Stone switch (result->getSymTag()) { 135154fd7ff6SZachary Turner case PDB_SymType::Enum: 135254fd7ff6SZachary Turner case PDB_SymType::UDT: 135354fd7ff6SZachary Turner case PDB_SymType::Typedef: 135442dff790SZachary Turner break; 135542dff790SZachary Turner default: 135605097246SAdrian Prantl // We're looking only for types that have names. Skip symbols, as well 135705097246SAdrian Prantl // as unnamed types such as arrays, pointers, etc. 135842dff790SZachary Turner continue; 135942dff790SZachary Turner } 136042dff790SZachary Turner 1361b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1362b9c1b51eSKate Stone // lookup. 136342dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 136442dff790SZachary Turner continue; 136542dff790SZachary Turner 136642dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 136742dff790SZachary Turner if (iter == m_types.end()) 136842dff790SZachary Turner continue; 136942dff790SZachary Turner types.Insert(iter->second); 137042dff790SZachary Turner ++matches; 137142dff790SZachary Turner } 137274e08ca0SZachary Turner } 137374e08ca0SZachary Turner 1374b9c1b51eSKate Stone size_t SymbolFilePDB::FindTypes( 1375b9c1b51eSKate Stone const std::vector<lldb_private::CompilerContext> &contexts, bool append, 1376b9c1b51eSKate Stone lldb_private::TypeMap &types) { 137742dff790SZachary Turner return 0; 137874e08ca0SZachary Turner } 137974e08ca0SZachary Turner 1380ec40f818SAaron Smith lldb_private::TypeList *SymbolFilePDB::GetTypeList() { 1381ec40f818SAaron Smith return m_obj_file->GetModule()->GetTypeList(); 1382ec40f818SAaron Smith } 138374e08ca0SZachary Turner 1384c8316ed2SAaron Smith void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, 13857ac1c780SAaron Smith uint32_t type_mask, 13867ac1c780SAaron Smith TypeCollection &type_collection) { 13877ac1c780SAaron Smith bool can_parse = false; 1388e664b5dcSAaron Smith switch (pdb_symbol.getSymTag()) { 13897ac1c780SAaron Smith case PDB_SymType::ArrayType: 13907ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassArray) != 0); 13917ac1c780SAaron Smith break; 13927ac1c780SAaron Smith case PDB_SymType::BuiltinType: 13937ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassBuiltin) != 0); 13947ac1c780SAaron Smith break; 13957ac1c780SAaron Smith case PDB_SymType::Enum: 13967ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassEnumeration) != 0); 13977ac1c780SAaron Smith break; 13987ac1c780SAaron Smith case PDB_SymType::Function: 13997ac1c780SAaron Smith case PDB_SymType::FunctionSig: 14007ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassFunction) != 0); 14017ac1c780SAaron Smith break; 14027ac1c780SAaron Smith case PDB_SymType::PointerType: 14037ac1c780SAaron Smith can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | 14047ac1c780SAaron Smith eTypeClassMemberPointer)) != 0); 14057ac1c780SAaron Smith break; 14067ac1c780SAaron Smith case PDB_SymType::Typedef: 14077ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassTypedef) != 0); 14087ac1c780SAaron Smith break; 14097ac1c780SAaron Smith case PDB_SymType::UDT: { 1410e664b5dcSAaron Smith auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); 14117ac1c780SAaron Smith assert(udt); 14127ac1c780SAaron Smith can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && 14137ac1c780SAaron Smith ((type_mask & (eTypeClassClass | eTypeClassStruct | 14147ac1c780SAaron Smith eTypeClassUnion)) != 0)); 14157ac1c780SAaron Smith } break; 1416c8316ed2SAaron Smith default: 1417c8316ed2SAaron Smith break; 14187ac1c780SAaron Smith } 14197ac1c780SAaron Smith 14207ac1c780SAaron Smith if (can_parse) { 1421e664b5dcSAaron Smith if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { 14227ac1c780SAaron Smith auto result = 14237ac1c780SAaron Smith std::find(type_collection.begin(), type_collection.end(), type); 14247ac1c780SAaron Smith if (result == type_collection.end()) 14257ac1c780SAaron Smith type_collection.push_back(type); 14267ac1c780SAaron Smith } 14277ac1c780SAaron Smith } 14287ac1c780SAaron Smith 1429e664b5dcSAaron Smith auto results_up = pdb_symbol.findAllChildren(); 14307ac1c780SAaron Smith while (auto symbol_up = results_up->getNext()) 1431e664b5dcSAaron Smith GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); 14327ac1c780SAaron Smith } 14337ac1c780SAaron Smith 1434b9c1b51eSKate Stone size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1435b9c1b51eSKate Stone uint32_t type_mask, 1436b9c1b51eSKate Stone lldb_private::TypeList &type_list) { 14377ac1c780SAaron Smith TypeCollection type_collection; 14387ac1c780SAaron Smith uint32_t old_size = type_list.GetSize(); 1439c8316ed2SAaron Smith CompileUnit *cu = 1440c8316ed2SAaron Smith sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 14417ac1c780SAaron Smith if (cu) { 14427ac1c780SAaron Smith auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1443e664b5dcSAaron Smith if (!compiland_up) 1444e664b5dcSAaron Smith return 0; 1445e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 14467ac1c780SAaron Smith } else { 14477ac1c780SAaron Smith for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 14487ac1c780SAaron Smith auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1449d5a925f4SAaron Smith if (cu_sp) { 1450e664b5dcSAaron Smith if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1451e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 14527ac1c780SAaron Smith } 14537ac1c780SAaron Smith } 14547ac1c780SAaron Smith } 14557ac1c780SAaron Smith 14567ac1c780SAaron Smith for (auto type : type_collection) { 14577ac1c780SAaron Smith type->GetForwardCompilerType(); 14587ac1c780SAaron Smith type_list.Insert(type->shared_from_this()); 14597ac1c780SAaron Smith } 14607ac1c780SAaron Smith return type_list.GetSize() - old_size; 146174e08ca0SZachary Turner } 146274e08ca0SZachary Turner 146374e08ca0SZachary Turner lldb_private::TypeSystem * 1464b9c1b51eSKate Stone SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1465b9c1b51eSKate Stone auto type_system = 1466b9c1b51eSKate Stone m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 146774e08ca0SZachary Turner if (type_system) 146874e08ca0SZachary Turner type_system->SetSymbolFile(this); 146974e08ca0SZachary Turner return type_system; 147074e08ca0SZachary Turner } 147174e08ca0SZachary Turner 1472b9c1b51eSKate Stone lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 1473b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 1474b9c1b51eSKate Stone const lldb_private::ConstString &name, 1475b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx) { 147674e08ca0SZachary Turner return lldb_private::CompilerDeclContext(); 147774e08ca0SZachary Turner } 147874e08ca0SZachary Turner 1479b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginName() { 148074e08ca0SZachary Turner static ConstString g_name("pdb"); 148174e08ca0SZachary Turner return g_name; 148274e08ca0SZachary Turner } 148374e08ca0SZachary Turner 1484b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 148574e08ca0SZachary Turner 1486b9c1b51eSKate Stone IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1487b9c1b51eSKate Stone 1488b9c1b51eSKate Stone const IPDBSession &SymbolFilePDB::GetPDBSession() const { 148942dff790SZachary Turner return *m_session_up; 149042dff790SZachary Turner } 149142dff790SZachary Turner 1492c8316ed2SAaron Smith lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, 1493c8316ed2SAaron Smith uint32_t index) { 149474e08ca0SZachary Turner auto found_cu = m_comp_units.find(id); 149574e08ca0SZachary Turner if (found_cu != m_comp_units.end()) 149674e08ca0SZachary Turner return found_cu->second; 149774e08ca0SZachary Turner 149810a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(id); 149910a02577SAaron Smith if (!compiland_up) 150010a02577SAaron Smith return CompUnitSP(); 150174e08ca0SZachary Turner 150274e08ca0SZachary Turner lldb::LanguageType lang; 150310a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 150474e08ca0SZachary Turner if (!details) 150574e08ca0SZachary Turner lang = lldb::eLanguageTypeC_plus_plus; 150674e08ca0SZachary Turner else 150774e08ca0SZachary Turner lang = TranslateLanguage(details->getLanguage()); 150874e08ca0SZachary Turner 1509f76fe682SAaron Smith if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1510f76fe682SAaron Smith return CompUnitSP(); 1511f76fe682SAaron Smith 1512487b0c6bSAaron Smith std::string path = compiland_up->getSourceFileFullPath(); 1513f76fe682SAaron Smith if (path.empty()) 1514f76fe682SAaron Smith return CompUnitSP(); 1515f76fe682SAaron Smith 1516b9c1b51eSKate Stone // Don't support optimized code for now, DebugInfoPDB does not return this 1517b9c1b51eSKate Stone // information. 1518ad2b63cbSGreg Clayton LazyBool optimized = eLazyBoolNo; 1519c8316ed2SAaron Smith auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, 1520c8316ed2SAaron Smith path.c_str(), id, lang, optimized); 152110a02577SAaron Smith 152210a02577SAaron Smith if (!cu_sp) 152310a02577SAaron Smith return CompUnitSP(); 152410a02577SAaron Smith 152510a02577SAaron Smith m_comp_units.insert(std::make_pair(id, cu_sp)); 152610a02577SAaron Smith if (index == UINT32_MAX) 1527e664b5dcSAaron Smith GetCompileUnitIndex(*compiland_up, index); 152810a02577SAaron Smith lldbassert(index != UINT32_MAX); 1529c8316ed2SAaron Smith m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index, 1530c8316ed2SAaron Smith cu_sp); 153110a02577SAaron Smith return cu_sp; 153274e08ca0SZachary Turner } 153374e08ca0SZachary Turner 1534b9c1b51eSKate Stone bool SymbolFilePDB::ParseCompileUnitLineTable( 1535b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, uint32_t match_line) { 153610a02577SAaron Smith lldbassert(sc.comp_unit); 153710a02577SAaron Smith 153810a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 153910a02577SAaron Smith if (!compiland_up) 154010a02577SAaron Smith return false; 154174e08ca0SZachary Turner 1542b9c1b51eSKate Stone // LineEntry needs the *index* of the file into the list of support files 15439d0eb996SAdrian McCarthy // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 154405097246SAdrian Prantl // a globally unique idenfitifier in the namespace of the PDB. So, we have 154505097246SAdrian Prantl // to do a mapping so that we can hand out indices. 154642dff790SZachary Turner llvm::DenseMap<uint32_t, uint32_t> index_map; 154710a02577SAaron Smith BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 154874e08ca0SZachary Turner auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); 154974e08ca0SZachary Turner 155010a02577SAaron Smith // Find contributions to `compiland` from all source and header files. 155174e08ca0SZachary Turner std::string path = sc.comp_unit->GetPath(); 155210a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 155310a02577SAaron Smith if (!files) 155410a02577SAaron Smith return false; 155574e08ca0SZachary Turner 155605097246SAdrian Prantl // For each source and header file, create a LineSequence for contributions 155705097246SAdrian Prantl // to the compiland from that file, and add the sequence. 1558b9c1b51eSKate Stone while (auto file = files->getNext()) { 1559b9c1b51eSKate Stone std::unique_ptr<LineSequence> sequence( 1560b9c1b51eSKate Stone line_table->CreateLineSequenceContainer()); 156110a02577SAaron Smith auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 156210a02577SAaron Smith if (!lines) 156310a02577SAaron Smith continue; 156474e08ca0SZachary Turner int entry_count = lines->getChildCount(); 156574e08ca0SZachary Turner 15667e8c7beaSZachary Turner uint64_t prev_addr; 15677e8c7beaSZachary Turner uint32_t prev_length; 15687e8c7beaSZachary Turner uint32_t prev_line; 15697e8c7beaSZachary Turner uint32_t prev_source_idx; 15707e8c7beaSZachary Turner 1571b9c1b51eSKate Stone for (int i = 0; i < entry_count; ++i) { 157274e08ca0SZachary Turner auto line = lines->getChildAtIndex(i); 157374e08ca0SZachary Turner 15747e8c7beaSZachary Turner uint64_t lno = line->getLineNumber(); 15757e8c7beaSZachary Turner uint64_t addr = line->getVirtualAddress(); 15767e8c7beaSZachary Turner uint32_t length = line->getLength(); 157774e08ca0SZachary Turner uint32_t source_id = line->getSourceFileId(); 15787e8c7beaSZachary Turner uint32_t col = line->getColumnNumber(); 157974e08ca0SZachary Turner uint32_t source_idx = index_map[source_id]; 158074e08ca0SZachary Turner 158105097246SAdrian Prantl // There was a gap between the current entry and the previous entry if 158205097246SAdrian Prantl // the addresses don't perfectly line up. 15837e8c7beaSZachary Turner bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 15847e8c7beaSZachary Turner 1585b9c1b51eSKate Stone // Before inserting the current entry, insert a terminal entry at the end 15869d0eb996SAdrian McCarthy // of the previous entry's address range if the current entry resulted in 15879d0eb996SAdrian McCarthy // a gap from the previous entry. 1588b9c1b51eSKate Stone if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1589b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1590b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 15917e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 1592010edd37SAaron Smith 1593010edd37SAaron Smith line_table->InsertSequence(sequence.release()); 1594010edd37SAaron Smith sequence.reset(line_table->CreateLineSequenceContainer()); 15957e8c7beaSZachary Turner } 15967e8c7beaSZachary Turner 1597b9c1b51eSKate Stone if (ShouldAddLine(match_line, lno, length)) { 15987e8c7beaSZachary Turner bool is_statement = line->isStatement(); 159974e08ca0SZachary Turner bool is_prologue = false; 160074e08ca0SZachary Turner bool is_epilogue = false; 1601b9c1b51eSKate Stone auto func = 1602b9c1b51eSKate Stone m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1603b9c1b51eSKate Stone if (func) { 160454fd7ff6SZachary Turner auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 160510a02577SAaron Smith if (prologue) 16067e8c7beaSZachary Turner is_prologue = (addr == prologue->getVirtualAddress()); 160774e08ca0SZachary Turner 160854fd7ff6SZachary Turner auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 160910a02577SAaron Smith if (epilogue) 16107e8c7beaSZachary Turner is_epilogue = (addr == epilogue->getVirtualAddress()); 16117e8c7beaSZachary Turner } 161274e08ca0SZachary Turner 1613b9c1b51eSKate Stone line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1614b9c1b51eSKate Stone source_idx, is_statement, false, 16157e8c7beaSZachary Turner is_prologue, is_epilogue, false); 16167e8c7beaSZachary Turner } 16177e8c7beaSZachary Turner 16187e8c7beaSZachary Turner prev_addr = addr; 16197e8c7beaSZachary Turner prev_length = length; 16207e8c7beaSZachary Turner prev_line = lno; 16217e8c7beaSZachary Turner prev_source_idx = source_idx; 16227e8c7beaSZachary Turner } 16237e8c7beaSZachary Turner 1624b9c1b51eSKate Stone if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 16257e8c7beaSZachary Turner // The end is always a terminal entry, so insert it regardless. 1626b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1627b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 16287e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 162974e08ca0SZachary Turner } 163074e08ca0SZachary Turner 16317e8c7beaSZachary Turner line_table->InsertSequence(sequence.release()); 163274e08ca0SZachary Turner } 163374e08ca0SZachary Turner 163410a02577SAaron Smith if (line_table->GetSize()) { 163574e08ca0SZachary Turner sc.comp_unit->SetLineTable(line_table.release()); 163674e08ca0SZachary Turner return true; 163774e08ca0SZachary Turner } 163810a02577SAaron Smith return false; 163910a02577SAaron Smith } 164074e08ca0SZachary Turner 1641b9c1b51eSKate Stone void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 164210a02577SAaron Smith const PDBSymbolCompiland &compiland, 1643b9c1b51eSKate Stone llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 164405097246SAdrian Prantl // This is a hack, but we need to convert the source id into an index into 164505097246SAdrian Prantl // the support files array. We don't want to do path comparisons to avoid 16469d0eb996SAdrian McCarthy // basename / full path issues that may or may not even be a problem, so we 16479d0eb996SAdrian McCarthy // use the globally unique source file identifiers. Ideally we could use the 16489d0eb996SAdrian McCarthy // global identifiers everywhere, but LineEntry currently assumes indices. 164910a02577SAaron Smith auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 165010a02577SAaron Smith if (!source_files) 165110a02577SAaron Smith return; 16529ea80d25SPavel Labath 16539ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based) 16549ea80d25SPavel Labath int index = 1; 165574e08ca0SZachary Turner 1656b9c1b51eSKate Stone while (auto file = source_files->getNext()) { 165774e08ca0SZachary Turner uint32_t source_id = file->getUniqueId(); 165874e08ca0SZachary Turner index_map[source_id] = index++; 165974e08ca0SZachary Turner } 166074e08ca0SZachary Turner } 16617ac1c780SAaron Smith 16627ac1c780SAaron Smith lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 16637ac1c780SAaron Smith const lldb_private::Address &so_addr) { 16647ac1c780SAaron Smith lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1665308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 16667ac1c780SAaron Smith return nullptr; 16677ac1c780SAaron Smith 1668308e39caSAaron Smith // If it is a PDB function's vm addr, this is the first sure bet. 1669308e39caSAaron Smith if (auto lines = 1670308e39caSAaron Smith m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { 1671308e39caSAaron Smith if (auto first_line = lines->getNext()) 1672308e39caSAaron Smith return ParseCompileUnitForUID(first_line->getCompilandId()); 16737ac1c780SAaron Smith } 16747ac1c780SAaron Smith 1675308e39caSAaron Smith // Otherwise we resort to section contributions. 1676308e39caSAaron Smith if (auto sec_contribs = m_session_up->getSectionContribs()) { 1677308e39caSAaron Smith while (auto section = sec_contribs->getNext()) { 1678308e39caSAaron Smith auto va = section->getVirtualAddress(); 1679308e39caSAaron Smith if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) 1680308e39caSAaron Smith return ParseCompileUnitForUID(section->getCompilandId()); 1681308e39caSAaron Smith } 1682308e39caSAaron Smith } 16837ac1c780SAaron Smith return nullptr; 16847ac1c780SAaron Smith } 16857ac1c780SAaron Smith 16867ac1c780SAaron Smith Mangled 1687e664b5dcSAaron Smith SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 16887ac1c780SAaron Smith Mangled mangled; 1689e664b5dcSAaron Smith auto func_name = pdb_func.getName(); 1690e664b5dcSAaron Smith auto func_undecorated_name = pdb_func.getUndecoratedName(); 16917ac1c780SAaron Smith std::string func_decorated_name; 16927ac1c780SAaron Smith 16937ac1c780SAaron Smith // Seek from public symbols for non-static function's decorated name if any. 16947ac1c780SAaron Smith // For static functions, they don't have undecorated names and aren't exposed 16957ac1c780SAaron Smith // in Public Symbols either. 16967ac1c780SAaron Smith if (!func_undecorated_name.empty()) { 1697c8316ed2SAaron Smith auto result_up = m_global_scope_up->findChildren( 1698c8316ed2SAaron Smith PDB_SymType::PublicSymbol, func_undecorated_name, 16997ac1c780SAaron Smith PDB_NameSearchFlags::NS_UndecoratedName); 17007ac1c780SAaron Smith if (result_up) { 17017ac1c780SAaron Smith while (auto symbol_up = result_up->getNext()) { 17027ac1c780SAaron Smith // For a public symbol, it is unique. 17037ac1c780SAaron Smith lldbassert(result_up->getChildCount() == 1); 17047ac1c780SAaron Smith if (auto *pdb_public_sym = 1705c8316ed2SAaron Smith llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( 1706c8316ed2SAaron Smith symbol_up.get())) { 17077ac1c780SAaron Smith if (pdb_public_sym->isFunction()) { 17087ac1c780SAaron Smith func_decorated_name = pdb_public_sym->getName(); 1709f76fe682SAaron Smith break; 17107ac1c780SAaron Smith } 17117ac1c780SAaron Smith } 17127ac1c780SAaron Smith } 17137ac1c780SAaron Smith } 17147ac1c780SAaron Smith } 17157ac1c780SAaron Smith if (!func_decorated_name.empty()) { 17167ac1c780SAaron Smith mangled.SetMangledName(ConstString(func_decorated_name)); 17177ac1c780SAaron Smith 17187ac1c780SAaron Smith // For MSVC, format of C funciton's decorated name depends on calling 17197ac1c780SAaron Smith // conventon. Unfortunately none of the format is recognized by current 17207ac1c780SAaron Smith // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 172105097246SAdrian Prantl // `__purecall` is retrieved as both its decorated and undecorated name 172205097246SAdrian Prantl // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` 172305097246SAdrian Prantl // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). 172405097246SAdrian Prantl // Mangled::GetDemangledName method will fail internally and caches an 172505097246SAdrian Prantl // empty string as its undecorated name. So we will face a contradition 172605097246SAdrian Prantl // here for the same symbol: 17277ac1c780SAaron Smith // non-empty undecorated name from PDB 17287ac1c780SAaron Smith // empty undecorated name from LLDB 17297ac1c780SAaron Smith if (!func_undecorated_name.empty() && 17307ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) 17317ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 17327ac1c780SAaron Smith 17337ac1c780SAaron Smith // LLDB uses several flags to control how a C++ decorated name is 173405097246SAdrian Prantl // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the 173505097246SAdrian Prantl // yielded name could be different from what we retrieve from 17367ac1c780SAaron Smith // PDB source unless we also apply same flags in getting undecorated 17377ac1c780SAaron Smith // name through PDBSymbolFunc::getUndecoratedNameEx method. 17387ac1c780SAaron Smith if (!func_undecorated_name.empty() && 17397ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()) != 17407ac1c780SAaron Smith ConstString(func_undecorated_name)) 17417ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 17427ac1c780SAaron Smith } else if (!func_undecorated_name.empty()) { 17437ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 17447ac1c780SAaron Smith } else if (!func_name.empty()) 17457ac1c780SAaron Smith mangled.SetValue(ConstString(func_name), false); 17467ac1c780SAaron Smith 17477ac1c780SAaron Smith return mangled; 17487ac1c780SAaron Smith } 17497ac1c780SAaron Smith 17507ac1c780SAaron Smith bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 17517ac1c780SAaron Smith const lldb_private::CompilerDeclContext *decl_ctx) { 17527ac1c780SAaron Smith if (decl_ctx == nullptr || !decl_ctx->IsValid()) 17537ac1c780SAaron Smith return true; 17547ac1c780SAaron Smith 17557ac1c780SAaron Smith TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 17567ac1c780SAaron Smith if (!decl_ctx_type_system) 17577ac1c780SAaron Smith return false; 17587ac1c780SAaron Smith TypeSystem *type_system = GetTypeSystemForLanguage( 17597ac1c780SAaron Smith decl_ctx_type_system->GetMinimumLanguage(nullptr)); 17607ac1c780SAaron Smith if (decl_ctx_type_system == type_system) 17617ac1c780SAaron Smith return true; // The type systems match, return true 17627ac1c780SAaron Smith 17637ac1c780SAaron Smith return false; 17647ac1c780SAaron Smith } 1765