174e08ca0SZachary Turner //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===// 274e08ca0SZachary Turner // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 674e08ca0SZachary Turner // 774e08ca0SZachary Turner //===----------------------------------------------------------------------===// 874e08ca0SZachary Turner 974e08ca0SZachary Turner #include "SymbolFilePDB.h" 1074e08ca0SZachary Turner 11c1e530eeSAleksandr Urakov #include "PDBASTParser.h" 12c1e530eeSAleksandr Urakov #include "PDBLocationToDWARFExpression.h" 13c1e530eeSAleksandr Urakov 1442dff790SZachary Turner #include "clang/Lex/Lexer.h" 1542dff790SZachary Turner 1674e08ca0SZachary Turner #include "lldb/Core/Module.h" 1774e08ca0SZachary Turner #include "lldb/Core/PluginManager.h" 1842dff790SZachary Turner #include "lldb/Symbol/ClangASTContext.h" 1974e08ca0SZachary Turner #include "lldb/Symbol/CompileUnit.h" 2074e08ca0SZachary Turner #include "lldb/Symbol/LineTable.h" 2174e08ca0SZachary Turner #include "lldb/Symbol/ObjectFile.h" 2274e08ca0SZachary Turner #include "lldb/Symbol/SymbolContext.h" 2310a02577SAaron Smith #include "lldb/Symbol/SymbolVendor.h" 24ec40f818SAaron Smith #include "lldb/Symbol/TypeList.h" 25308e39caSAaron Smith #include "lldb/Symbol/TypeMap.h" 26cab0d23fSAaron Smith #include "lldb/Symbol/Variable.h" 2786e9434dSAaron Smith #include "lldb/Utility/RegularExpression.h" 2874e08ca0SZachary Turner 29b8d8c62bSPavel Labath #include "llvm/DebugInfo/PDB/GenericError.h" 301f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 3174e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 3274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 33308e39caSAaron Smith #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h" 3474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 351f8552abSAaron Smith #include "llvm/DebugInfo/PDB/IPDBTable.h" 3674e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbol.h" 377ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 3874e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 3974e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 401f8552abSAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 4174e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 4274e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 4374e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 4474e08ca0SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 457ac1c780SAaron Smith #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 4642dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 4742dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 4842dff790SZachary Turner #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 4942dff790SZachary Turner 50672d2c12SJonas Devlieghere #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 51c1e530eeSAleksandr Urakov #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" 52307f5ae8SZachary Turner #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h" 5342dff790SZachary Turner 5442dff790SZachary Turner #include <regex> 5574e08ca0SZachary Turner 5610a02577SAaron Smith using namespace lldb; 5774e08ca0SZachary Turner using namespace lldb_private; 5854fd7ff6SZachary Turner using namespace llvm::pdb; 5974e08ca0SZachary Turner 60b9c1b51eSKate Stone namespace { 61b9c1b51eSKate Stone lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 62b9c1b51eSKate Stone switch (lang) { 6354fd7ff6SZachary Turner case PDB_Lang::Cpp: 6474e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeC_plus_plus; 6554fd7ff6SZachary Turner case PDB_Lang::C: 6674e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeC; 670561be6cSNathan Lanza case PDB_Lang::Swift: 680561be6cSNathan Lanza return lldb::LanguageType::eLanguageTypeSwift; 6974e08ca0SZachary Turner default: 7074e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeUnknown; 7174e08ca0SZachary Turner } 7274e08ca0SZachary Turner } 737e8c7beaSZachary Turner 74b9c1b51eSKate Stone bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 75b9c1b51eSKate Stone uint32_t addr_length) { 76b9c1b51eSKate Stone return ((requested_line == 0 || actual_line == requested_line) && 77b9c1b51eSKate Stone addr_length > 0); 787e8c7beaSZachary Turner } 79c8316ed2SAaron Smith } // namespace 8074e08ca0SZachary Turner 81307f5ae8SZachary Turner static bool ShouldUseNativeReader() { 8271970b72SGreg Clayton #if defined(_WIN32) 83307f5ae8SZachary Turner llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER"); 84307f5ae8SZachary Turner return use_native.equals_lower("on") || use_native.equals_lower("yes") || 85307f5ae8SZachary Turner use_native.equals_lower("1") || use_native.equals_lower("true"); 8671970b72SGreg Clayton #else 8771970b72SGreg Clayton return true; 8871970b72SGreg Clayton #endif 89307f5ae8SZachary Turner } 90307f5ae8SZachary Turner 91b9c1b51eSKate Stone void SymbolFilePDB::Initialize() { 92307f5ae8SZachary Turner if (ShouldUseNativeReader()) { 93307f5ae8SZachary Turner npdb::SymbolFileNativePDB::Initialize(); 94307f5ae8SZachary Turner } else { 95b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 96b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 9774e08ca0SZachary Turner DebuggerInitialize); 9874e08ca0SZachary Turner } 99307f5ae8SZachary Turner } 10074e08ca0SZachary Turner 101b9c1b51eSKate Stone void SymbolFilePDB::Terminate() { 102307f5ae8SZachary Turner if (ShouldUseNativeReader()) { 103307f5ae8SZachary Turner npdb::SymbolFileNativePDB::Terminate(); 104307f5ae8SZachary Turner } else { 10574e08ca0SZachary Turner PluginManager::UnregisterPlugin(CreateInstance); 10674e08ca0SZachary Turner } 107307f5ae8SZachary Turner } 10874e08ca0SZachary Turner 109b9c1b51eSKate Stone void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 11074e08ca0SZachary Turner 111b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 11274e08ca0SZachary Turner static ConstString g_name("pdb"); 11374e08ca0SZachary Turner return g_name; 11474e08ca0SZachary Turner } 11574e08ca0SZachary Turner 116b9c1b51eSKate Stone const char *SymbolFilePDB::GetPluginDescriptionStatic() { 11774e08ca0SZachary Turner return "Microsoft PDB debug symbol file reader."; 11874e08ca0SZachary Turner } 11974e08ca0SZachary Turner 12074e08ca0SZachary Turner lldb_private::SymbolFile * 121b9c1b51eSKate Stone SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { 12274e08ca0SZachary Turner return new SymbolFilePDB(obj_file); 12374e08ca0SZachary Turner } 12474e08ca0SZachary Turner 12574e08ca0SZachary Turner SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 126e0119909SPavel Labath : SymbolFile(object_file), m_session_up(), m_global_scope_up() {} 12774e08ca0SZachary Turner 128b9c1b51eSKate Stone SymbolFilePDB::~SymbolFilePDB() {} 12974e08ca0SZachary Turner 130b9c1b51eSKate Stone uint32_t SymbolFilePDB::CalculateAbilities() { 1311f8552abSAaron Smith uint32_t abilities = 0; 1321f8552abSAaron Smith if (!m_obj_file) 1331f8552abSAaron Smith return 0; 1341f8552abSAaron Smith 135b9c1b51eSKate Stone if (!m_session_up) { 13674e08ca0SZachary Turner // Lazily load and match the PDB file, but only do this once. 13774e08ca0SZachary Turner std::string exePath = m_obj_file->GetFileSpec().GetPath(); 138b9c1b51eSKate Stone auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 139b9c1b51eSKate Stone m_session_up); 140b9c1b51eSKate Stone if (error) { 1414fd6a960SZachary Turner llvm::consumeError(std::move(error)); 1421f8552abSAaron Smith auto module_sp = m_obj_file->GetModule(); 1431f8552abSAaron Smith if (!module_sp) 1441f8552abSAaron Smith return 0; 1451f8552abSAaron Smith // See if any symbol file is specified through `--symfile` option. 1461f8552abSAaron Smith FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 1471f8552abSAaron Smith if (!symfile) 1481f8552abSAaron Smith return 0; 1491f8552abSAaron Smith error = loadDataForPDB(PDB_ReaderType::DIA, 150c8316ed2SAaron Smith llvm::StringRef(symfile.GetPath()), m_session_up); 1511f8552abSAaron Smith if (error) { 1521f8552abSAaron Smith llvm::consumeError(std::move(error)); 15374e08ca0SZachary Turner return 0; 15474e08ca0SZachary Turner } 155b8d8c62bSPavel Labath } 1561f8552abSAaron Smith } 157d5a925f4SAaron Smith if (!m_session_up) 1581f8552abSAaron Smith return 0; 1591f8552abSAaron Smith 1601f8552abSAaron Smith auto enum_tables_up = m_session_up->getEnumTables(); 1611f8552abSAaron Smith if (!enum_tables_up) 1621f8552abSAaron Smith return 0; 1631f8552abSAaron Smith while (auto table_up = enum_tables_up->getNext()) { 1641f8552abSAaron Smith if (table_up->getItemCount() == 0) 1651f8552abSAaron Smith continue; 1661f8552abSAaron Smith auto type = table_up->getTableType(); 1671f8552abSAaron Smith switch (type) { 1681f8552abSAaron Smith case PDB_TableType::Symbols: 1691f8552abSAaron Smith // This table represents a store of symbols with types listed in 1701f8552abSAaron Smith // PDBSym_Type 171c8316ed2SAaron Smith abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | 172c8316ed2SAaron Smith LocalVariables | VariableTypes); 1731f8552abSAaron Smith break; 1741f8552abSAaron Smith case PDB_TableType::LineNumbers: 1751f8552abSAaron Smith abilities |= LineTables; 1761f8552abSAaron Smith break; 177c8316ed2SAaron Smith default: 178c8316ed2SAaron Smith break; 1791f8552abSAaron Smith } 1801f8552abSAaron Smith } 1811f8552abSAaron Smith return abilities; 18274e08ca0SZachary Turner } 18374e08ca0SZachary Turner 184b9c1b51eSKate Stone void SymbolFilePDB::InitializeObject() { 185d1304bbaSPavel Labath lldb::addr_t obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress(); 186c8316ed2SAaron Smith lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); 18774e08ca0SZachary Turner m_session_up->setLoadAddress(obj_load_address); 18810a02577SAaron Smith if (!m_global_scope_up) 18910a02577SAaron Smith m_global_scope_up = m_session_up->getGlobalScope(); 19010a02577SAaron Smith lldbassert(m_global_scope_up.get()); 19174e08ca0SZachary Turner } 19274e08ca0SZachary Turner 193e0119909SPavel Labath uint32_t SymbolFilePDB::CalculateNumCompileUnits() { 19410a02577SAaron Smith auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 19510a02577SAaron Smith if (!compilands) 19610a02577SAaron Smith return 0; 19710a02577SAaron Smith 19810a02577SAaron Smith // The linker could link *.dll (compiland language = LINK), or import 19905097246SAdrian Prantl // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be 20005097246SAdrian Prantl // found as a child of the global scope (PDB executable). Usually, such 20105097246SAdrian Prantl // compilands contain `thunk` symbols in which we are not interested for 20205097246SAdrian Prantl // now. However we still count them in the compiland list. If we perform 20305097246SAdrian Prantl // any compiland related activity, like finding symbols through 20405097246SAdrian Prantl // llvm::pdb::IPDBSession methods, such compilands will all be searched 20505097246SAdrian Prantl // automatically no matter whether we include them or not. 206e0119909SPavel Labath uint32_t compile_unit_count = compilands->getChildCount(); 20774e08ca0SZachary Turner 208b9c1b51eSKate Stone // The linker can inject an additional "dummy" compilation unit into the 2099d0eb996SAdrian McCarthy // PDB. Ignore this special compile unit for our purposes, if it is there. 2109d0eb996SAdrian McCarthy // It is always the last one. 211e0119909SPavel Labath auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1); 21210a02577SAaron Smith lldbassert(last_compiland_up.get()); 21310a02577SAaron Smith std::string name = last_compiland_up->getName(); 21474e08ca0SZachary Turner if (name == "* Linker *") 215e0119909SPavel Labath --compile_unit_count; 216e0119909SPavel Labath return compile_unit_count; 21774e08ca0SZachary Turner } 21874e08ca0SZachary Turner 21910a02577SAaron Smith void SymbolFilePDB::GetCompileUnitIndex( 220c8316ed2SAaron Smith const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { 22110a02577SAaron Smith auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 22210a02577SAaron Smith if (!results_up) 22310a02577SAaron Smith return; 224e664b5dcSAaron Smith auto uid = pdb_compiland.getSymIndexId(); 225fbdf0b93SRaphael Isemann for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 22610a02577SAaron Smith auto compiland_up = results_up->getChildAtIndex(cu_idx); 22710a02577SAaron Smith if (!compiland_up) 22810a02577SAaron Smith continue; 22910a02577SAaron Smith if (compiland_up->getSymIndexId() == uid) { 23010a02577SAaron Smith index = cu_idx; 23110a02577SAaron Smith return; 23210a02577SAaron Smith } 23310a02577SAaron Smith } 23410a02577SAaron Smith index = UINT32_MAX; 23510a02577SAaron Smith return; 23610a02577SAaron Smith } 23710a02577SAaron Smith 23810a02577SAaron Smith std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 23910a02577SAaron Smith SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 24010a02577SAaron Smith return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 24110a02577SAaron Smith } 24210a02577SAaron Smith 243b9c1b51eSKate Stone lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 24410a02577SAaron Smith if (index >= GetNumCompileUnits()) 24510a02577SAaron Smith return CompUnitSP(); 24674e08ca0SZachary Turner 24710a02577SAaron Smith // Assuming we always retrieve same compilands listed in same order through 24810a02577SAaron Smith // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 24910a02577SAaron Smith // compile unit makes no sense. 25010a02577SAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 25110a02577SAaron Smith if (!results) 25210a02577SAaron Smith return CompUnitSP(); 25310a02577SAaron Smith auto compiland_up = results->getChildAtIndex(index); 25410a02577SAaron Smith if (!compiland_up) 25510a02577SAaron Smith return CompUnitSP(); 25610a02577SAaron Smith return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 25774e08ca0SZachary Turner } 25874e08ca0SZachary Turner 259863f8c18SZachary Turner lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) { 260*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 261863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 26210a02577SAaron Smith if (!compiland_up) 26374e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 26410a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 26574e08ca0SZachary Turner if (!details) 26674e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 26774e08ca0SZachary Turner return TranslateLanguage(details->getLanguage()); 26874e08ca0SZachary Turner } 26974e08ca0SZachary Turner 270863f8c18SZachary Turner lldb_private::Function * 271863f8c18SZachary Turner SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func, 272863f8c18SZachary Turner CompileUnit &comp_unit) { 273863f8c18SZachary Turner if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId())) 274a5235af9SAleksandr Urakov return result.get(); 275a5235af9SAleksandr Urakov 276e664b5dcSAaron Smith auto file_vm_addr = pdb_func.getVirtualAddress(); 277308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 2787ac1c780SAaron Smith return nullptr; 2797ac1c780SAaron Smith 280e664b5dcSAaron Smith auto func_length = pdb_func.getLength(); 281c8316ed2SAaron Smith AddressRange func_range = 282863f8c18SZachary Turner AddressRange(file_vm_addr, func_length, 283863f8c18SZachary Turner GetObjectFile()->GetModule()->GetSectionList()); 2847ac1c780SAaron Smith if (!func_range.GetBaseAddress().IsValid()) 2857ac1c780SAaron Smith return nullptr; 2867ac1c780SAaron Smith 287e664b5dcSAaron Smith lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 2887ac1c780SAaron Smith if (!func_type) 2897ac1c780SAaron Smith return nullptr; 2907ac1c780SAaron Smith 291e664b5dcSAaron Smith user_id_t func_type_uid = pdb_func.getSignatureId(); 292f76fe682SAaron Smith 2937ac1c780SAaron Smith Mangled mangled = GetMangledForPDBFunc(pdb_func); 2947ac1c780SAaron Smith 295c8316ed2SAaron Smith FunctionSP func_sp = 296863f8c18SZachary Turner std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(), 297c8316ed2SAaron Smith func_type_uid, mangled, func_type, func_range); 2987ac1c780SAaron Smith 299863f8c18SZachary Turner comp_unit.AddFunction(func_sp); 300c68925abSZachary Turner 301d0050d1bSNathan Lanza LanguageType lang = ParseLanguage(comp_unit); 302d0050d1bSNathan Lanza TypeSystem *type_system = GetTypeSystemForLanguage(lang); 303c68925abSZachary Turner if (!type_system) 304c68925abSZachary Turner return nullptr; 305c68925abSZachary Turner ClangASTContext *clang_type_system = 306c68925abSZachary Turner llvm::dyn_cast_or_null<ClangASTContext>(type_system); 307c68925abSZachary Turner if (!clang_type_system) 308c68925abSZachary Turner return nullptr; 309c68925abSZachary Turner clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func); 310c68925abSZachary Turner 3117ac1c780SAaron Smith return func_sp.get(); 3127ac1c780SAaron Smith } 3137ac1c780SAaron Smith 314863f8c18SZachary Turner size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) { 315*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 3167ac1c780SAaron Smith size_t func_added = 0; 317863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 3187ac1c780SAaron Smith if (!compiland_up) 3197ac1c780SAaron Smith return 0; 3207ac1c780SAaron Smith auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 3217ac1c780SAaron Smith if (!results_up) 3227ac1c780SAaron Smith return 0; 3237ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 324863f8c18SZachary Turner auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId()); 3257ac1c780SAaron Smith if (!func_sp) { 326863f8c18SZachary Turner if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit)) 3277ac1c780SAaron Smith ++func_added; 3287ac1c780SAaron Smith } 3297ac1c780SAaron Smith } 3307ac1c780SAaron Smith return func_added; 33174e08ca0SZachary Turner } 33274e08ca0SZachary Turner 333863f8c18SZachary Turner bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) { 334*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 335863f8c18SZachary Turner if (comp_unit.GetLineTable()) 33610a02577SAaron Smith return true; 337863f8c18SZachary Turner return ParseCompileUnitLineTable(comp_unit, 0); 33874e08ca0SZachary Turner } 33974e08ca0SZachary Turner 340863f8c18SZachary Turner bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) { 34174e08ca0SZachary Turner // PDB doesn't contain information about macros 34274e08ca0SZachary Turner return false; 34374e08ca0SZachary Turner } 34474e08ca0SZachary Turner 345863f8c18SZachary Turner bool SymbolFilePDB::ParseSupportFiles( 346863f8c18SZachary Turner CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) { 34774e08ca0SZachary Turner 348b9c1b51eSKate Stone // In theory this is unnecessary work for us, because all of this information 3499d0eb996SAdrian McCarthy // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 3509d0eb996SAdrian McCarthy // second time seems like a waste. Unfortunately, there's no good way around 3519d0eb996SAdrian McCarthy // this short of a moderate refactor since SymbolVendor depends on being able 3529d0eb996SAdrian McCarthy // to cache this list. 353*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 354863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 35510a02577SAaron Smith if (!compiland_up) 35674e08ca0SZachary Turner return false; 35710a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 35874e08ca0SZachary Turner if (!files || files->getChildCount() == 0) 35974e08ca0SZachary Turner return false; 36074e08ca0SZachary Turner 361b9c1b51eSKate Stone while (auto file = files->getNext()) { 3628f3be7a3SJonas Devlieghere FileSpec spec(file->getFileName(), FileSpec::Style::windows); 36310a02577SAaron Smith support_files.AppendIfUnique(spec); 36474e08ca0SZachary Turner } 3659ea80d25SPavel Labath 3669ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based), 3679ea80d25SPavel Labath // the zeroth file is the compile unit itself 368863f8c18SZachary Turner support_files.Insert(0, comp_unit); 3699ea80d25SPavel Labath 37074e08ca0SZachary Turner return true; 37174e08ca0SZachary Turner } 37274e08ca0SZachary Turner 373b9c1b51eSKate Stone bool SymbolFilePDB::ParseImportedModules( 374b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 3750f30a3b6SAdrian Prantl std::vector<SourceModule> &imported_modules) { 37674e08ca0SZachary Turner // PDB does not yet support module debug info 37774e08ca0SZachary Turner return false; 37874e08ca0SZachary Turner } 37974e08ca0SZachary Turner 380c8316ed2SAaron Smith static size_t ParseFunctionBlocksForPDBSymbol( 381ffc1b8fdSZachary Turner uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol, 382ffc1b8fdSZachary Turner lldb_private::Block *parent_block, bool is_top_parent) { 3837ac1c780SAaron Smith assert(pdb_symbol && parent_block); 3847ac1c780SAaron Smith 3857ac1c780SAaron Smith size_t num_added = 0; 3867ac1c780SAaron Smith switch (pdb_symbol->getSymTag()) { 3877ac1c780SAaron Smith case PDB_SymType::Block: 3887ac1c780SAaron Smith case PDB_SymType::Function: { 3897ac1c780SAaron Smith Block *block = nullptr; 3907ac1c780SAaron Smith auto &raw_sym = pdb_symbol->getRawSymbol(); 3917ac1c780SAaron Smith if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { 3927ac1c780SAaron Smith if (pdb_func->hasNoInlineAttribute()) 3937ac1c780SAaron Smith break; 3947ac1c780SAaron Smith if (is_top_parent) 3957ac1c780SAaron Smith block = parent_block; 3967ac1c780SAaron Smith else 3977ac1c780SAaron Smith break; 3987ac1c780SAaron Smith } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { 3997ac1c780SAaron Smith auto uid = pdb_symbol->getSymIndexId(); 4007ac1c780SAaron Smith if (parent_block->FindBlockByID(uid)) 4017ac1c780SAaron Smith break; 4027ac1c780SAaron Smith if (raw_sym.getVirtualAddress() < func_file_vm_addr) 4037ac1c780SAaron Smith break; 4047ac1c780SAaron Smith 4057ac1c780SAaron Smith auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); 4067ac1c780SAaron Smith parent_block->AddChild(block_sp); 4077ac1c780SAaron Smith block = block_sp.get(); 4087ac1c780SAaron Smith } else 4097ac1c780SAaron Smith llvm_unreachable("Unexpected PDB symbol!"); 4107ac1c780SAaron Smith 411c8316ed2SAaron Smith block->AddRange(Block::Range( 412c8316ed2SAaron Smith raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); 4137ac1c780SAaron Smith block->FinalizeRanges(); 4147ac1c780SAaron Smith ++num_added; 4157ac1c780SAaron Smith 4167ac1c780SAaron Smith auto results_up = pdb_symbol->findAllChildren(); 4177ac1c780SAaron Smith if (!results_up) 4187ac1c780SAaron Smith break; 4197ac1c780SAaron Smith while (auto symbol_up = results_up->getNext()) { 420c8316ed2SAaron Smith num_added += ParseFunctionBlocksForPDBSymbol( 421ffc1b8fdSZachary Turner func_file_vm_addr, symbol_up.get(), block, false); 4227ac1c780SAaron Smith } 4237ac1c780SAaron Smith } break; 424c8316ed2SAaron Smith default: 425c8316ed2SAaron Smith break; 4267ac1c780SAaron Smith } 4277ac1c780SAaron Smith return num_added; 4287ac1c780SAaron Smith } 4297ac1c780SAaron Smith 430ffc1b8fdSZachary Turner size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) { 431*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 4327ac1c780SAaron Smith size_t num_added = 0; 433ffc1b8fdSZachary Turner auto uid = func.GetID(); 4347ac1c780SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 4357ac1c780SAaron Smith if (!pdb_func_up) 4367ac1c780SAaron Smith return 0; 437ffc1b8fdSZachary Turner Block &parent_block = func.GetBlock(false); 438ffc1b8fdSZachary Turner num_added = ParseFunctionBlocksForPDBSymbol( 439ffc1b8fdSZachary Turner pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true); 4407ac1c780SAaron Smith return num_added; 441b9c1b51eSKate Stone } 442b9c1b51eSKate Stone 443863f8c18SZachary Turner size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) { 444*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 44566b84079SAaron Smith 44666b84079SAaron Smith size_t num_added = 0; 447ac0d41c7SZachary Turner auto compiland = GetPDBCompilandByUID(comp_unit.GetID()); 44866b84079SAaron Smith if (!compiland) 44966b84079SAaron Smith return 0; 45066b84079SAaron Smith 45166b84079SAaron Smith auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 45266b84079SAaron Smith std::unique_ptr<IPDBEnumSymbols> results; 45366b84079SAaron Smith PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 45466b84079SAaron Smith PDB_SymType::UDT}; 45566b84079SAaron Smith for (auto tag : tags_to_search) { 45666b84079SAaron Smith results = raw_sym.findAllChildren(tag); 45766b84079SAaron Smith if (!results || results->getChildCount() == 0) 45866b84079SAaron Smith continue; 45966b84079SAaron Smith while (auto symbol = results->getNext()) { 46066b84079SAaron Smith switch (symbol->getSymTag()) { 461ec40f818SAaron Smith case PDB_SymType::Enum: 462ec40f818SAaron Smith case PDB_SymType::UDT: 463ec40f818SAaron Smith case PDB_SymType::Typedef: 464ec40f818SAaron Smith break; 465ec40f818SAaron Smith default: 466ec40f818SAaron Smith continue; 467ec40f818SAaron Smith } 468ec40f818SAaron Smith 469ec40f818SAaron Smith // This should cause the type to get cached and stored in the `m_types` 470ec40f818SAaron Smith // lookup. 4717d2a74fcSAleksandr Urakov if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { 4727d2a74fcSAleksandr Urakov // Resolve the type completely to avoid a completion 4737d2a74fcSAleksandr Urakov // (and so a list change, which causes an iterators invalidation) 4747d2a74fcSAleksandr Urakov // during a TypeList dumping 4757d2a74fcSAleksandr Urakov type->GetFullCompilerType(); 476ec40f818SAaron Smith ++num_added; 477ec40f818SAaron Smith } 47866b84079SAaron Smith } 4797d2a74fcSAleksandr Urakov } 48066b84079SAaron Smith }; 48166b84079SAaron Smith 48266b84079SAaron Smith ParseTypesByTagFn(*compiland); 48366b84079SAaron Smith 48466b84079SAaron Smith // Also parse global types particularly coming from this compiland. 48505097246SAdrian Prantl // Unfortunately, PDB has no compiland information for each global type. We 48605097246SAdrian Prantl // have to parse them all. But ensure we only do this once. 48766b84079SAaron Smith static bool parse_all_global_types = false; 48866b84079SAaron Smith if (!parse_all_global_types) { 48966b84079SAaron Smith ParseTypesByTagFn(*m_global_scope_up); 49066b84079SAaron Smith parse_all_global_types = true; 49166b84079SAaron Smith } 492ec40f818SAaron Smith return num_added; 49374e08ca0SZachary Turner } 49474e08ca0SZachary Turner 49574e08ca0SZachary Turner size_t 496b9c1b51eSKate Stone SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 497*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 498cab0d23fSAaron Smith if (!sc.comp_unit) 499cab0d23fSAaron Smith return 0; 500cab0d23fSAaron Smith 501cab0d23fSAaron Smith size_t num_added = 0; 502cab0d23fSAaron Smith if (sc.function) { 503cab0d23fSAaron Smith auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 504cab0d23fSAaron Smith sc.function->GetID()); 505cab0d23fSAaron Smith if (!pdb_func) 506cab0d23fSAaron Smith return 0; 507cab0d23fSAaron Smith 508cab0d23fSAaron Smith num_added += ParseVariables(sc, *pdb_func); 509cab0d23fSAaron Smith sc.function->GetBlock(false).SetDidParseVariables(true, true); 510cab0d23fSAaron Smith } else if (sc.comp_unit) { 511cab0d23fSAaron Smith auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 512cab0d23fSAaron Smith if (!compiland) 513cab0d23fSAaron Smith return 0; 514cab0d23fSAaron Smith 515cab0d23fSAaron Smith if (sc.comp_unit->GetVariableList(false)) 516cab0d23fSAaron Smith return 0; 517cab0d23fSAaron Smith 518cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 519cab0d23fSAaron Smith if (results && results->getChildCount()) { 520cab0d23fSAaron Smith while (auto result = results->getNext()) { 521356aa4a9SAleksandr Urakov auto cu_id = GetCompilandId(*result); 522cab0d23fSAaron Smith // FIXME: We are not able to determine variable's compile unit. 523cab0d23fSAaron Smith if (cu_id == 0) 524cab0d23fSAaron Smith continue; 525cab0d23fSAaron Smith 526cab0d23fSAaron Smith if (cu_id == sc.comp_unit->GetID()) 527cab0d23fSAaron Smith num_added += ParseVariables(sc, *result); 528cab0d23fSAaron Smith } 529cab0d23fSAaron Smith } 530cab0d23fSAaron Smith 531cab0d23fSAaron Smith // FIXME: A `file static` or `global constant` variable appears both in 532cab0d23fSAaron Smith // compiland's children and global scope's children with unexpectedly 533cab0d23fSAaron Smith // different symbol's Id making it ambiguous. 534cab0d23fSAaron Smith 535cab0d23fSAaron Smith // FIXME: 'local constant', for example, const char var[] = "abc", declared 536cab0d23fSAaron Smith // in a function scope, can't be found in PDB. 537cab0d23fSAaron Smith 538cab0d23fSAaron Smith // Parse variables in this compiland. 539cab0d23fSAaron Smith num_added += ParseVariables(sc, *compiland); 540cab0d23fSAaron Smith } 541cab0d23fSAaron Smith 542cab0d23fSAaron Smith return num_added; 54374e08ca0SZachary Turner } 54474e08ca0SZachary Turner 545b9c1b51eSKate Stone lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 546*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 54742dff790SZachary Turner auto find_result = m_types.find(type_uid); 54842dff790SZachary Turner if (find_result != m_types.end()) 54942dff790SZachary Turner return find_result->second.get(); 55042dff790SZachary Turner 551b9c1b51eSKate Stone TypeSystem *type_system = 552b9c1b51eSKate Stone GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 553b9c1b51eSKate Stone ClangASTContext *clang_type_system = 554b9c1b51eSKate Stone llvm::dyn_cast_or_null<ClangASTContext>(type_system); 55542dff790SZachary Turner if (!clang_type_system) 55674e08ca0SZachary Turner return nullptr; 557709426b3SAleksandr Urakov PDBASTParser *pdb = clang_type_system->GetPDBParser(); 55842dff790SZachary Turner if (!pdb) 55942dff790SZachary Turner return nullptr; 56042dff790SZachary Turner 56142dff790SZachary Turner auto pdb_type = m_session_up->getSymbolById(type_uid); 56242dff790SZachary Turner if (pdb_type == nullptr) 56342dff790SZachary Turner return nullptr; 56442dff790SZachary Turner 56542dff790SZachary Turner lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 566d5a925f4SAaron Smith if (result) { 56742dff790SZachary Turner m_types.insert(std::make_pair(type_uid, result)); 568f46e8974SPavel Labath GetTypeList().Insert(result); 569ec40f818SAaron Smith } 57042dff790SZachary Turner return result.get(); 57174e08ca0SZachary Turner } 57274e08ca0SZachary Turner 573eca07c59SAdrian Prantl llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID( 574eca07c59SAdrian Prantl lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 575eca07c59SAdrian Prantl return llvm::None; 576eca07c59SAdrian Prantl } 577eca07c59SAdrian Prantl 578b9c1b51eSKate Stone bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 5797d2a74fcSAleksandr Urakov std::lock_guard<std::recursive_mutex> guard( 5807d2a74fcSAleksandr Urakov GetObjectFile()->GetModule()->GetMutex()); 5817d2a74fcSAleksandr Urakov 5827d2a74fcSAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 5837d2a74fcSAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 5847d2a74fcSAleksandr Urakov if (!clang_ast_ctx) 58574e08ca0SZachary Turner return false; 5867d2a74fcSAleksandr Urakov 587709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 5887d2a74fcSAleksandr Urakov if (!pdb) 5897d2a74fcSAleksandr Urakov return false; 5907d2a74fcSAleksandr Urakov 5917d2a74fcSAleksandr Urakov return pdb->CompleteTypeFromPDB(compiler_type); 59274e08ca0SZachary Turner } 59374e08ca0SZachary Turner 594b9c1b51eSKate Stone lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 595709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 596709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 597709426b3SAleksandr Urakov if (!clang_ast_ctx) 598709426b3SAleksandr Urakov return CompilerDecl(); 599709426b3SAleksandr Urakov 600709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 601709426b3SAleksandr Urakov if (!pdb) 602709426b3SAleksandr Urakov return CompilerDecl(); 603709426b3SAleksandr Urakov 604709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 605709426b3SAleksandr Urakov if (!symbol) 606709426b3SAleksandr Urakov return CompilerDecl(); 607709426b3SAleksandr Urakov 608709426b3SAleksandr Urakov auto decl = pdb->GetDeclForSymbol(*symbol); 609709426b3SAleksandr Urakov if (!decl) 610709426b3SAleksandr Urakov return CompilerDecl(); 611709426b3SAleksandr Urakov 612709426b3SAleksandr Urakov return CompilerDecl(clang_ast_ctx, decl); 61374e08ca0SZachary Turner } 61474e08ca0SZachary Turner 61574e08ca0SZachary Turner lldb_private::CompilerDeclContext 616b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 617709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 618709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 619709426b3SAleksandr Urakov if (!clang_ast_ctx) 620709426b3SAleksandr Urakov return CompilerDeclContext(); 621709426b3SAleksandr Urakov 622709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 623709426b3SAleksandr Urakov if (!pdb) 624709426b3SAleksandr Urakov return CompilerDeclContext(); 625709426b3SAleksandr Urakov 626709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 627709426b3SAleksandr Urakov if (!symbol) 628709426b3SAleksandr Urakov return CompilerDeclContext(); 629709426b3SAleksandr Urakov 630709426b3SAleksandr Urakov auto decl_context = pdb->GetDeclContextForSymbol(*symbol); 631709426b3SAleksandr Urakov if (!decl_context) 632709426b3SAleksandr Urakov return GetDeclContextContainingUID(uid); 633709426b3SAleksandr Urakov 634709426b3SAleksandr Urakov return CompilerDeclContext(clang_ast_ctx, decl_context); 63574e08ca0SZachary Turner } 63674e08ca0SZachary Turner 63774e08ca0SZachary Turner lldb_private::CompilerDeclContext 638b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 639709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 640709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 641709426b3SAleksandr Urakov if (!clang_ast_ctx) 642709426b3SAleksandr Urakov return CompilerDeclContext(); 643709426b3SAleksandr Urakov 644709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 645709426b3SAleksandr Urakov if (!pdb) 646709426b3SAleksandr Urakov return CompilerDeclContext(); 647709426b3SAleksandr Urakov 648709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 649709426b3SAleksandr Urakov if (!symbol) 650709426b3SAleksandr Urakov return CompilerDeclContext(); 651709426b3SAleksandr Urakov 652709426b3SAleksandr Urakov auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol); 653709426b3SAleksandr Urakov assert(decl_context); 654709426b3SAleksandr Urakov 655709426b3SAleksandr Urakov return CompilerDeclContext(clang_ast_ctx, decl_context); 65674e08ca0SZachary Turner } 65774e08ca0SZachary Turner 658b9c1b51eSKate Stone void SymbolFilePDB::ParseDeclsForContext( 659709426b3SAleksandr Urakov lldb_private::CompilerDeclContext decl_ctx) { 660709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 661709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 662709426b3SAleksandr Urakov if (!clang_ast_ctx) 663709426b3SAleksandr Urakov return; 664709426b3SAleksandr Urakov 665709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 666709426b3SAleksandr Urakov if (!pdb) 667709426b3SAleksandr Urakov return; 668709426b3SAleksandr Urakov 669709426b3SAleksandr Urakov pdb->ParseDeclsForDeclContext( 670709426b3SAleksandr Urakov static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext())); 671709426b3SAleksandr Urakov } 67274e08ca0SZachary Turner 67374e08ca0SZachary Turner uint32_t 674b9c1b51eSKate Stone SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 675991e4453SZachary Turner SymbolContextItem resolve_scope, 676b9c1b51eSKate Stone lldb_private::SymbolContext &sc) { 677*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 6787ac1c780SAaron Smith uint32_t resolved_flags = 0; 6794d4d63eeSPavel Labath if (resolve_scope & eSymbolContextCompUnit || 6804d4d63eeSPavel Labath resolve_scope & eSymbolContextVariable || 6814d4d63eeSPavel Labath resolve_scope & eSymbolContextFunction || 6824d4d63eeSPavel Labath resolve_scope & eSymbolContextBlock || 6837ac1c780SAaron Smith resolve_scope & eSymbolContextLineEntry) { 6847ac1c780SAaron Smith auto cu_sp = GetCompileUnitContainsAddress(so_addr); 6857ac1c780SAaron Smith if (!cu_sp) { 6867ac1c780SAaron Smith if (resolved_flags | eSymbolContextVariable) { 6877ac1c780SAaron Smith // TODO: Resolve variables 6887ac1c780SAaron Smith } 6897ac1c780SAaron Smith return 0; 6907ac1c780SAaron Smith } 6917ac1c780SAaron Smith sc.comp_unit = cu_sp.get(); 6927ac1c780SAaron Smith resolved_flags |= eSymbolContextCompUnit; 6937ac1c780SAaron Smith lldbassert(sc.module_sp == cu_sp->GetModule()); 6949ea80d25SPavel Labath } 6957ac1c780SAaron Smith 696398f81b3SAleksandr Urakov if (resolve_scope & eSymbolContextFunction || 697398f81b3SAleksandr Urakov resolve_scope & eSymbolContextBlock) { 6989ea80d25SPavel Labath addr_t file_vm_addr = so_addr.GetFileAddress(); 6999ea80d25SPavel Labath auto symbol_up = 7009ea80d25SPavel Labath m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); 7019ea80d25SPavel Labath if (symbol_up) { 7027ac1c780SAaron Smith auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 7037ac1c780SAaron Smith assert(pdb_func); 7047ac1c780SAaron Smith auto func_uid = pdb_func->getSymIndexId(); 7057ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 7067ac1c780SAaron Smith if (sc.function == nullptr) 707863f8c18SZachary Turner sc.function = 708863f8c18SZachary Turner ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit); 7097ac1c780SAaron Smith if (sc.function) { 7107ac1c780SAaron Smith resolved_flags |= eSymbolContextFunction; 7117ac1c780SAaron Smith if (resolve_scope & eSymbolContextBlock) { 712398f81b3SAleksandr Urakov auto block_symbol = m_session_up->findSymbolByAddress( 713398f81b3SAleksandr Urakov file_vm_addr, PDB_SymType::Block); 714398f81b3SAleksandr Urakov auto block_id = block_symbol ? block_symbol->getSymIndexId() 715398f81b3SAleksandr Urakov : sc.function->GetID(); 716398f81b3SAleksandr Urakov sc.block = sc.function->GetBlock(true).FindBlockByID(block_id); 7177ac1c780SAaron Smith if (sc.block) 7187ac1c780SAaron Smith resolved_flags |= eSymbolContextBlock; 7197ac1c780SAaron Smith } 7207ac1c780SAaron Smith } 7217ac1c780SAaron Smith } 7227ac1c780SAaron Smith } 7237ac1c780SAaron Smith 7247ac1c780SAaron Smith if (resolve_scope & eSymbolContextLineEntry) { 7257ac1c780SAaron Smith if (auto *line_table = sc.comp_unit->GetLineTable()) { 7267ac1c780SAaron Smith Address addr(so_addr); 7277ac1c780SAaron Smith if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 7287ac1c780SAaron Smith resolved_flags |= eSymbolContextLineEntry; 7297ac1c780SAaron Smith } 7307ac1c780SAaron Smith } 7319ea80d25SPavel Labath 7327ac1c780SAaron Smith return resolved_flags; 73374e08ca0SZachary Turner } 73474e08ca0SZachary Turner 735b9c1b51eSKate Stone uint32_t SymbolFilePDB::ResolveSymbolContext( 736b9c1b51eSKate Stone const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 737991e4453SZachary Turner SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) { 738*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 73910a02577SAaron Smith const size_t old_size = sc_list.GetSize(); 740b9c1b51eSKate Stone if (resolve_scope & lldb::eSymbolContextCompUnit) { 741b9c1b51eSKate Stone // Locate all compilation units with line numbers referencing the specified 7429d0eb996SAdrian McCarthy // file. For example, if `file_spec` is <vector>, then this should return 7439d0eb996SAdrian McCarthy // all source files and header files that reference <vector>, either 7449d0eb996SAdrian McCarthy // directly or indirectly. 745b9c1b51eSKate Stone auto compilands = m_session_up->findCompilandsForSourceFile( 746b9c1b51eSKate Stone file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 74774e08ca0SZachary Turner 74810a02577SAaron Smith if (!compilands) 74910a02577SAaron Smith return 0; 75010a02577SAaron Smith 7519d0eb996SAdrian McCarthy // For each one, either find its previously parsed data or parse it afresh 7529d0eb996SAdrian McCarthy // and add it to the symbol context list. 753b9c1b51eSKate Stone while (auto compiland = compilands->getNext()) { 75405097246SAdrian Prantl // If we're not checking inlines, then don't add line information for 75505097246SAdrian Prantl // this file unless the FileSpec matches. For inline functions, we don't 75605097246SAdrian Prantl // have to match the FileSpec since they could be defined in headers 75705097246SAdrian Prantl // other than file specified in FileSpec. 758b9c1b51eSKate Stone if (!check_inlines) { 759487b0c6bSAaron Smith std::string source_file = compiland->getSourceFileFullPath(); 76010a02577SAaron Smith if (source_file.empty()) 76110a02577SAaron Smith continue; 7628f3be7a3SJonas Devlieghere FileSpec this_spec(source_file, FileSpec::Style::windows); 76310a02577SAaron Smith bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 76410a02577SAaron Smith if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 76574e08ca0SZachary Turner continue; 76674e08ca0SZachary Turner } 76774e08ca0SZachary Turner 76874e08ca0SZachary Turner SymbolContext sc; 76910a02577SAaron Smith auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 770d5a925f4SAaron Smith if (!cu) 77110a02577SAaron Smith continue; 77274e08ca0SZachary Turner sc.comp_unit = cu.get(); 77374e08ca0SZachary Turner sc.module_sp = cu->GetModule(); 77474e08ca0SZachary Turner 775b9c1b51eSKate Stone // If we were asked to resolve line entries, add all entries to the line 7769d0eb996SAdrian McCarthy // table that match the requested line (or all lines if `line` == 0). 7777ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 7787ac1c780SAaron Smith eSymbolContextLineEntry)) { 779863f8c18SZachary Turner bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line); 7807ac1c780SAaron Smith 7817ac1c780SAaron Smith if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 7827ac1c780SAaron Smith // The query asks for line entries, but we can't get them for the 78305097246SAdrian Prantl // compile unit. This is not normal for `line` = 0. So just assert 78405097246SAdrian Prantl // it. 785f76fe682SAaron Smith assert(line && "Couldn't get all line entries!\n"); 7867ac1c780SAaron Smith 7877ac1c780SAaron Smith // Current compiland does not have the requested line. Search next. 7887ac1c780SAaron Smith continue; 7897ac1c780SAaron Smith } 7907ac1c780SAaron Smith 7917ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 7927ac1c780SAaron Smith if (!has_line_table) 7937ac1c780SAaron Smith continue; 7947ac1c780SAaron Smith 7957ac1c780SAaron Smith auto *line_table = sc.comp_unit->GetLineTable(); 7967ac1c780SAaron Smith lldbassert(line_table); 7977ac1c780SAaron Smith 7987ac1c780SAaron Smith uint32_t num_line_entries = line_table->GetSize(); 7997ac1c780SAaron Smith // Skip the terminal line entry. 8007ac1c780SAaron Smith --num_line_entries; 8017ac1c780SAaron Smith 80205097246SAdrian Prantl // If `line `!= 0, see if we can resolve function for each line entry 80305097246SAdrian Prantl // in the line table. 8047ac1c780SAaron Smith for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 8057ac1c780SAaron Smith ++line_idx) { 8067ac1c780SAaron Smith if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 8077ac1c780SAaron Smith continue; 8087ac1c780SAaron Smith 8097ac1c780SAaron Smith auto file_vm_addr = 8107ac1c780SAaron Smith sc.line_entry.range.GetBaseAddress().GetFileAddress(); 811308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 8127ac1c780SAaron Smith continue; 8137ac1c780SAaron Smith 814c8316ed2SAaron Smith auto symbol_up = m_session_up->findSymbolByAddress( 815c8316ed2SAaron Smith file_vm_addr, PDB_SymType::Function); 8167ac1c780SAaron Smith if (symbol_up) { 8177ac1c780SAaron Smith auto func_uid = symbol_up->getSymIndexId(); 8187ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 8197ac1c780SAaron Smith if (sc.function == nullptr) { 8207ac1c780SAaron Smith auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 8217ac1c780SAaron Smith assert(pdb_func); 822863f8c18SZachary Turner sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, 823863f8c18SZachary Turner *sc.comp_unit); 8247ac1c780SAaron Smith } 8257ac1c780SAaron Smith if (sc.function && (resolve_scope & eSymbolContextBlock)) { 8267ac1c780SAaron Smith Block &block = sc.function->GetBlock(true); 8277ac1c780SAaron Smith sc.block = block.FindBlockByID(sc.function->GetID()); 8287ac1c780SAaron Smith } 8297ac1c780SAaron Smith } 8307ac1c780SAaron Smith sc_list.Append(sc); 8317ac1c780SAaron Smith } 8327ac1c780SAaron Smith } else if (has_line_table) { 8337ac1c780SAaron Smith // We can parse line table for the compile unit. But no query to 8347ac1c780SAaron Smith // resolve function or block. We append `sc` to the list anyway. 8357ac1c780SAaron Smith sc_list.Append(sc); 8367ac1c780SAaron Smith } 8377ac1c780SAaron Smith } else { 8387ac1c780SAaron Smith // No query for line entry, function or block. But we have a valid 8397ac1c780SAaron Smith // compile unit, append `sc` to the list. 8407ac1c780SAaron Smith sc_list.Append(sc); 8417ac1c780SAaron Smith } 84274e08ca0SZachary Turner } 84374e08ca0SZachary Turner } 84410a02577SAaron Smith return sc_list.GetSize() - old_size; 84574e08ca0SZachary Turner } 84674e08ca0SZachary Turner 847cab0d23fSAaron Smith std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { 848356aa4a9SAleksandr Urakov // Cache public names at first 849356aa4a9SAleksandr Urakov if (m_public_names.empty()) 850356aa4a9SAleksandr Urakov if (auto result_up = 851356aa4a9SAleksandr Urakov m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol)) 852356aa4a9SAleksandr Urakov while (auto symbol_up = result_up->getNext()) 853356aa4a9SAleksandr Urakov if (auto addr = symbol_up->getRawSymbol().getVirtualAddress()) 854356aa4a9SAleksandr Urakov m_public_names[addr] = symbol_up->getRawSymbol().getName(); 855cab0d23fSAaron Smith 856356aa4a9SAleksandr Urakov // Look up the name in the cache 857356aa4a9SAleksandr Urakov return m_public_names.lookup(pdb_data.getVirtualAddress()); 858cab0d23fSAaron Smith } 859cab0d23fSAaron Smith 860cab0d23fSAaron Smith VariableSP SymbolFilePDB::ParseVariableForPDBData( 861cab0d23fSAaron Smith const lldb_private::SymbolContext &sc, 862cab0d23fSAaron Smith const llvm::pdb::PDBSymbolData &pdb_data) { 863cab0d23fSAaron Smith VariableSP var_sp; 864cab0d23fSAaron Smith uint32_t var_uid = pdb_data.getSymIndexId(); 865cab0d23fSAaron Smith auto result = m_variables.find(var_uid); 866cab0d23fSAaron Smith if (result != m_variables.end()) 867cab0d23fSAaron Smith return result->second; 868cab0d23fSAaron Smith 869cab0d23fSAaron Smith ValueType scope = eValueTypeInvalid; 870cab0d23fSAaron Smith bool is_static_member = false; 871cab0d23fSAaron Smith bool is_external = false; 872cab0d23fSAaron Smith bool is_artificial = false; 873cab0d23fSAaron Smith 874cab0d23fSAaron Smith switch (pdb_data.getDataKind()) { 875cab0d23fSAaron Smith case PDB_DataKind::Global: 876cab0d23fSAaron Smith scope = eValueTypeVariableGlobal; 877cab0d23fSAaron Smith is_external = true; 878cab0d23fSAaron Smith break; 879cab0d23fSAaron Smith case PDB_DataKind::Local: 880cab0d23fSAaron Smith scope = eValueTypeVariableLocal; 881cab0d23fSAaron Smith break; 882cab0d23fSAaron Smith case PDB_DataKind::FileStatic: 883cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 884cab0d23fSAaron Smith break; 885cab0d23fSAaron Smith case PDB_DataKind::StaticMember: 886cab0d23fSAaron Smith is_static_member = true; 887cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 888cab0d23fSAaron Smith break; 889cab0d23fSAaron Smith case PDB_DataKind::Member: 890cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 891cab0d23fSAaron Smith break; 892cab0d23fSAaron Smith case PDB_DataKind::Param: 893cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 894cab0d23fSAaron Smith break; 895cab0d23fSAaron Smith case PDB_DataKind::Constant: 896cab0d23fSAaron Smith scope = eValueTypeConstResult; 897cab0d23fSAaron Smith break; 898cab0d23fSAaron Smith default: 899cab0d23fSAaron Smith break; 900cab0d23fSAaron Smith } 901cab0d23fSAaron Smith 902cab0d23fSAaron Smith switch (pdb_data.getLocationType()) { 903cab0d23fSAaron Smith case PDB_LocType::TLS: 904cab0d23fSAaron Smith scope = eValueTypeVariableThreadLocal; 905cab0d23fSAaron Smith break; 906cab0d23fSAaron Smith case PDB_LocType::RegRel: { 907cab0d23fSAaron Smith // It is a `this` pointer. 908cab0d23fSAaron Smith if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { 909cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 910cab0d23fSAaron Smith is_artificial = true; 911cab0d23fSAaron Smith } 912cab0d23fSAaron Smith } break; 913cab0d23fSAaron Smith default: 914cab0d23fSAaron Smith break; 915cab0d23fSAaron Smith } 916cab0d23fSAaron Smith 917cab0d23fSAaron Smith Declaration decl; 918cab0d23fSAaron Smith if (!is_artificial && !pdb_data.isCompilerGenerated()) { 919cab0d23fSAaron Smith if (auto lines = pdb_data.getLineNumbers()) { 920cab0d23fSAaron Smith if (auto first_line = lines->getNext()) { 921cab0d23fSAaron Smith uint32_t src_file_id = first_line->getSourceFileId(); 922cab0d23fSAaron Smith auto src_file = m_session_up->getSourceFileById(src_file_id); 923cab0d23fSAaron Smith if (src_file) { 9248f3be7a3SJonas Devlieghere FileSpec spec(src_file->getFileName()); 925cab0d23fSAaron Smith decl.SetFile(spec); 926cab0d23fSAaron Smith decl.SetColumn(first_line->getColumnNumber()); 927cab0d23fSAaron Smith decl.SetLine(first_line->getLineNumber()); 928cab0d23fSAaron Smith } 929cab0d23fSAaron Smith } 930cab0d23fSAaron Smith } 931cab0d23fSAaron Smith } 932cab0d23fSAaron Smith 933cab0d23fSAaron Smith Variable::RangeList ranges; 934cab0d23fSAaron Smith SymbolContextScope *context_scope = sc.comp_unit; 935758657e5SAleksandr Urakov if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) { 936cab0d23fSAaron Smith if (sc.function) { 937758657e5SAleksandr Urakov Block &function_block = sc.function->GetBlock(true); 938758657e5SAleksandr Urakov Block *block = 939758657e5SAleksandr Urakov function_block.FindBlockByID(pdb_data.getLexicalParentId()); 940758657e5SAleksandr Urakov if (!block) 941758657e5SAleksandr Urakov block = &function_block; 942758657e5SAleksandr Urakov 943758657e5SAleksandr Urakov context_scope = block; 944758657e5SAleksandr Urakov 945758657e5SAleksandr Urakov for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges; 946758657e5SAleksandr Urakov ++i) { 947758657e5SAleksandr Urakov AddressRange range; 948758657e5SAleksandr Urakov if (!block->GetRangeAtIndex(i, range)) 949758657e5SAleksandr Urakov continue; 950758657e5SAleksandr Urakov 951758657e5SAleksandr Urakov ranges.Append(range.GetBaseAddress().GetFileAddress(), 952758657e5SAleksandr Urakov range.GetByteSize()); 953758657e5SAleksandr Urakov } 954cab0d23fSAaron Smith } 955cab0d23fSAaron Smith } 956cab0d23fSAaron Smith 957cab0d23fSAaron Smith SymbolFileTypeSP type_sp = 958cab0d23fSAaron Smith std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); 959cab0d23fSAaron Smith 960cab0d23fSAaron Smith auto var_name = pdb_data.getName(); 961cab0d23fSAaron Smith auto mangled = GetMangledForPDBData(pdb_data); 962cab0d23fSAaron Smith auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); 963cab0d23fSAaron Smith 964924d5608SJonas Devlieghere bool is_constant; 965924d5608SJonas Devlieghere DWARFExpression location = ConvertPDBLocationToDWARFExpression( 966758657e5SAleksandr Urakov GetObjectFile()->GetModule(), pdb_data, ranges, is_constant); 967cab0d23fSAaron Smith 968cab0d23fSAaron Smith var_sp = std::make_shared<Variable>( 969cab0d23fSAaron Smith var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, 970cab0d23fSAaron Smith ranges, &decl, location, is_external, is_artificial, is_static_member); 971924d5608SJonas Devlieghere var_sp->SetLocationIsConstantValueData(is_constant); 972cab0d23fSAaron Smith 973cab0d23fSAaron Smith m_variables.insert(std::make_pair(var_uid, var_sp)); 974cab0d23fSAaron Smith return var_sp; 975cab0d23fSAaron Smith } 976cab0d23fSAaron Smith 977cab0d23fSAaron Smith size_t 978cab0d23fSAaron Smith SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, 979cab0d23fSAaron Smith const llvm::pdb::PDBSymbol &pdb_symbol, 980cab0d23fSAaron Smith lldb_private::VariableList *variable_list) { 981cab0d23fSAaron Smith size_t num_added = 0; 982cab0d23fSAaron Smith 983cab0d23fSAaron Smith if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { 984cab0d23fSAaron Smith VariableListSP local_variable_list_sp; 985cab0d23fSAaron Smith 986cab0d23fSAaron Smith auto result = m_variables.find(pdb_data->getSymIndexId()); 987cab0d23fSAaron Smith if (result != m_variables.end()) { 988cab0d23fSAaron Smith if (variable_list) 989cab0d23fSAaron Smith variable_list->AddVariableIfUnique(result->second); 990cab0d23fSAaron Smith } else { 991cab0d23fSAaron Smith // Prepare right VariableList for this variable. 992cab0d23fSAaron Smith if (auto lexical_parent = pdb_data->getLexicalParent()) { 993cab0d23fSAaron Smith switch (lexical_parent->getSymTag()) { 994cab0d23fSAaron Smith case PDB_SymType::Exe: 995cab0d23fSAaron Smith assert(sc.comp_unit); 996cab0d23fSAaron Smith LLVM_FALLTHROUGH; 997cab0d23fSAaron Smith case PDB_SymType::Compiland: { 998cab0d23fSAaron Smith if (sc.comp_unit) { 999cab0d23fSAaron Smith local_variable_list_sp = sc.comp_unit->GetVariableList(false); 1000cab0d23fSAaron Smith if (!local_variable_list_sp) { 1001cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 1002cab0d23fSAaron Smith sc.comp_unit->SetVariableList(local_variable_list_sp); 1003cab0d23fSAaron Smith } 1004cab0d23fSAaron Smith } 1005cab0d23fSAaron Smith } break; 1006cab0d23fSAaron Smith case PDB_SymType::Block: 1007cab0d23fSAaron Smith case PDB_SymType::Function: { 1008cab0d23fSAaron Smith if (sc.function) { 1009cab0d23fSAaron Smith Block *block = sc.function->GetBlock(true).FindBlockByID( 1010cab0d23fSAaron Smith lexical_parent->getSymIndexId()); 1011cab0d23fSAaron Smith if (block) { 1012cab0d23fSAaron Smith local_variable_list_sp = block->GetBlockVariableList(false); 1013cab0d23fSAaron Smith if (!local_variable_list_sp) { 1014cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 1015cab0d23fSAaron Smith block->SetVariableList(local_variable_list_sp); 1016cab0d23fSAaron Smith } 1017cab0d23fSAaron Smith } 1018cab0d23fSAaron Smith } 1019cab0d23fSAaron Smith } break; 1020cab0d23fSAaron Smith default: 1021cab0d23fSAaron Smith break; 1022cab0d23fSAaron Smith } 1023cab0d23fSAaron Smith } 1024cab0d23fSAaron Smith 1025cab0d23fSAaron Smith if (local_variable_list_sp) { 1026cab0d23fSAaron Smith if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { 1027cab0d23fSAaron Smith local_variable_list_sp->AddVariableIfUnique(var_sp); 1028cab0d23fSAaron Smith if (variable_list) 1029cab0d23fSAaron Smith variable_list->AddVariableIfUnique(var_sp); 1030cab0d23fSAaron Smith ++num_added; 1031c68925abSZachary Turner PDBASTParser *ast = GetPDBAstParser(); 1032c68925abSZachary Turner if (ast) 1033c68925abSZachary Turner ast->GetDeclForSymbol(*pdb_data); 1034cab0d23fSAaron Smith } 1035cab0d23fSAaron Smith } 1036cab0d23fSAaron Smith } 1037cab0d23fSAaron Smith } 1038cab0d23fSAaron Smith 1039cab0d23fSAaron Smith if (auto results = pdb_symbol.findAllChildren()) { 1040cab0d23fSAaron Smith while (auto result = results->getNext()) 1041cab0d23fSAaron Smith num_added += ParseVariables(sc, *result, variable_list); 1042cab0d23fSAaron Smith } 1043cab0d23fSAaron Smith 1044cab0d23fSAaron Smith return num_added; 1045cab0d23fSAaron Smith } 1046cab0d23fSAaron Smith 1047b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindGlobalVariables( 10480e4c4821SAdrian Prantl lldb_private::ConstString name, 104934cda14bSPavel Labath const lldb_private::CompilerDeclContext *parent_decl_ctx, 1050b9c1b51eSKate Stone uint32_t max_matches, lldb_private::VariableList &variables) { 1051*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1052cab0d23fSAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1053cab0d23fSAaron Smith return 0; 1054cab0d23fSAaron Smith if (name.IsEmpty()) 1055cab0d23fSAaron Smith return 0; 1056cab0d23fSAaron Smith 1057709426b3SAleksandr Urakov auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1058cab0d23fSAaron Smith if (!results) 1059cab0d23fSAaron Smith return 0; 1060cab0d23fSAaron Smith 1061cab0d23fSAaron Smith uint32_t matches = 0; 1062cab0d23fSAaron Smith size_t old_size = variables.GetSize(); 1063cab0d23fSAaron Smith while (auto result = results->getNext()) { 1064cab0d23fSAaron Smith auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get()); 1065cab0d23fSAaron Smith if (max_matches > 0 && matches >= max_matches) 1066cab0d23fSAaron Smith break; 1067cab0d23fSAaron Smith 1068cab0d23fSAaron Smith SymbolContext sc; 1069cab0d23fSAaron Smith sc.module_sp = m_obj_file->GetModule(); 1070cab0d23fSAaron Smith lldbassert(sc.module_sp.get()); 1071cab0d23fSAaron Smith 1072709426b3SAleksandr Urakov if (!name.GetStringRef().equals( 1073c1e530eeSAleksandr Urakov MSVCUndecoratedNameParser::DropScope(pdb_data->getName()))) 1074709426b3SAleksandr Urakov continue; 1075709426b3SAleksandr Urakov 1076356aa4a9SAleksandr Urakov sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1077356aa4a9SAleksandr Urakov // FIXME: We are not able to determine the compile unit. 1078356aa4a9SAleksandr Urakov if (sc.comp_unit == nullptr) 1079356aa4a9SAleksandr Urakov continue; 1080356aa4a9SAleksandr Urakov 1081a5235af9SAleksandr Urakov if (parent_decl_ctx && GetDeclContextContainingUID( 1082a5235af9SAleksandr Urakov result->getSymIndexId()) != *parent_decl_ctx) 1083709426b3SAleksandr Urakov continue; 1084709426b3SAleksandr Urakov 1085cab0d23fSAaron Smith ParseVariables(sc, *pdb_data, &variables); 1086cab0d23fSAaron Smith matches = variables.GetSize() - old_size; 1087cab0d23fSAaron Smith } 1088cab0d23fSAaron Smith 1089cab0d23fSAaron Smith return matches; 109074e08ca0SZachary Turner } 109174e08ca0SZachary Turner 109274e08ca0SZachary Turner uint32_t 1093b9c1b51eSKate Stone SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, 109434cda14bSPavel Labath uint32_t max_matches, 1095b9c1b51eSKate Stone lldb_private::VariableList &variables) { 1096*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1097cab0d23fSAaron Smith if (!regex.IsValid()) 1098cab0d23fSAaron Smith return 0; 1099cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1100cab0d23fSAaron Smith if (!results) 1101cab0d23fSAaron Smith return 0; 1102cab0d23fSAaron Smith 1103cab0d23fSAaron Smith uint32_t matches = 0; 1104cab0d23fSAaron Smith size_t old_size = variables.GetSize(); 1105cab0d23fSAaron Smith while (auto pdb_data = results->getNext()) { 1106cab0d23fSAaron Smith if (max_matches > 0 && matches >= max_matches) 1107cab0d23fSAaron Smith break; 1108cab0d23fSAaron Smith 1109cab0d23fSAaron Smith auto var_name = pdb_data->getName(); 1110cab0d23fSAaron Smith if (var_name.empty()) 1111cab0d23fSAaron Smith continue; 1112cab0d23fSAaron Smith if (!regex.Execute(var_name)) 1113cab0d23fSAaron Smith continue; 1114cab0d23fSAaron Smith SymbolContext sc; 1115cab0d23fSAaron Smith sc.module_sp = m_obj_file->GetModule(); 1116cab0d23fSAaron Smith lldbassert(sc.module_sp.get()); 1117cab0d23fSAaron Smith 1118356aa4a9SAleksandr Urakov sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1119cab0d23fSAaron Smith // FIXME: We are not able to determine the compile unit. 1120cab0d23fSAaron Smith if (sc.comp_unit == nullptr) 1121cab0d23fSAaron Smith continue; 1122cab0d23fSAaron Smith 1123cab0d23fSAaron Smith ParseVariables(sc, *pdb_data, &variables); 1124cab0d23fSAaron Smith matches = variables.GetSize() - old_size; 1125cab0d23fSAaron Smith } 1126cab0d23fSAaron Smith 1127cab0d23fSAaron Smith return matches; 1128b9c1b51eSKate Stone } 1129b9c1b51eSKate Stone 1130e664b5dcSAaron Smith bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 11317ac1c780SAaron Smith bool include_inlines, 11327ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 11337ac1c780SAaron Smith lldb_private::SymbolContext sc; 1134a3a8cc80SAaron Smith sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 11357ac1c780SAaron Smith if (!sc.comp_unit) 11367ac1c780SAaron Smith return false; 11377ac1c780SAaron Smith sc.module_sp = sc.comp_unit->GetModule(); 1138863f8c18SZachary Turner sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit); 11397ac1c780SAaron Smith if (!sc.function) 11407ac1c780SAaron Smith return false; 11417ac1c780SAaron Smith 11427ac1c780SAaron Smith sc_list.Append(sc); 11437ac1c780SAaron Smith return true; 11447ac1c780SAaron Smith } 11457ac1c780SAaron Smith 11467ac1c780SAaron Smith bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 11477ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 1148c8316ed2SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 11497ac1c780SAaron Smith if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 11507ac1c780SAaron Smith return false; 1151e664b5dcSAaron Smith return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 11527ac1c780SAaron Smith } 11537ac1c780SAaron Smith 11547ac1c780SAaron Smith void SymbolFilePDB::CacheFunctionNames() { 11557ac1c780SAaron Smith if (!m_func_full_names.IsEmpty()) 11567ac1c780SAaron Smith return; 11577ac1c780SAaron Smith 11587ac1c780SAaron Smith std::map<uint64_t, uint32_t> addr_ids; 11597ac1c780SAaron Smith 11607ac1c780SAaron Smith if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 11617ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 1162f76fe682SAaron Smith if (pdb_func_up->isCompilerGenerated()) 1163f76fe682SAaron Smith continue; 1164f76fe682SAaron Smith 11657ac1c780SAaron Smith auto name = pdb_func_up->getName(); 11667ac1c780SAaron Smith auto demangled_name = pdb_func_up->getUndecoratedName(); 11677ac1c780SAaron Smith if (name.empty() && demangled_name.empty()) 11687ac1c780SAaron Smith continue; 11697ac1c780SAaron Smith 1170f76fe682SAaron Smith auto uid = pdb_func_up->getSymIndexId(); 11717ac1c780SAaron Smith if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 11727ac1c780SAaron Smith addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 11737ac1c780SAaron Smith 11747ac1c780SAaron Smith if (auto parent = pdb_func_up->getClassParent()) { 11757ac1c780SAaron Smith 11767ac1c780SAaron Smith // PDB have symbols for class/struct methods or static methods in Enum 11777ac1c780SAaron Smith // Class. We won't bother to check if the parent is UDT or Enum here. 11787ac1c780SAaron Smith m_func_method_names.Append(ConstString(name), uid); 11797ac1c780SAaron Smith 118005097246SAdrian Prantl // To search a method name, like NS::Class:MemberFunc, LLDB searches 118105097246SAdrian Prantl // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does 118205097246SAdrian Prantl // not have inforamtion of this, we extract base names and cache them 118305097246SAdrian Prantl // by our own effort. 1184c1e530eeSAleksandr Urakov llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 11857ac1c780SAaron Smith if (!basename.empty()) 11867ac1c780SAaron Smith m_func_base_names.Append(ConstString(basename), uid); 11877ac1c780SAaron Smith else { 11887ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 11897ac1c780SAaron Smith } 11907ac1c780SAaron Smith 11917ac1c780SAaron Smith if (!demangled_name.empty()) 11927ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 11937ac1c780SAaron Smith 11947ac1c780SAaron Smith } else { 11957ac1c780SAaron Smith // Handle not-method symbols. 11967ac1c780SAaron Smith 1197c1e530eeSAleksandr Urakov // The function name might contain namespace, or its lexical scope. 1198c1e530eeSAleksandr Urakov llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1199c1e530eeSAleksandr Urakov if (!basename.empty()) 1200c1e530eeSAleksandr Urakov m_func_base_names.Append(ConstString(basename), uid); 1201c1e530eeSAleksandr Urakov else 12027ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 12037ac1c780SAaron Smith 12047ac1c780SAaron Smith if (name == "main") { 12057ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 12067ac1c780SAaron Smith 12077ac1c780SAaron Smith if (!demangled_name.empty() && name != demangled_name) { 12087ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 12097ac1c780SAaron Smith m_func_base_names.Append(ConstString(demangled_name), uid); 12107ac1c780SAaron Smith } 12117ac1c780SAaron Smith } else if (!demangled_name.empty()) { 12127ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 12137ac1c780SAaron Smith } else { 12147ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 12157ac1c780SAaron Smith } 12167ac1c780SAaron Smith } 12177ac1c780SAaron Smith } 12187ac1c780SAaron Smith } 12197ac1c780SAaron Smith 12207ac1c780SAaron Smith if (auto results_up = 12217ac1c780SAaron Smith m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 12227ac1c780SAaron Smith while (auto pub_sym_up = results_up->getNext()) { 12237ac1c780SAaron Smith if (!pub_sym_up->isFunction()) 12247ac1c780SAaron Smith continue; 12257ac1c780SAaron Smith auto name = pub_sym_up->getName(); 12267ac1c780SAaron Smith if (name.empty()) 12277ac1c780SAaron Smith continue; 12287ac1c780SAaron Smith 12297ac1c780SAaron Smith if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 12307ac1c780SAaron Smith auto vm_addr = pub_sym_up->getVirtualAddress(); 12317ac1c780SAaron Smith 12327ac1c780SAaron Smith // PDB public symbol has mangled name for its associated function. 12337ac1c780SAaron Smith if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { 12347ac1c780SAaron Smith // Cache mangled name. 12357ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); 12367ac1c780SAaron Smith } 12377ac1c780SAaron Smith } 12387ac1c780SAaron Smith } 12397ac1c780SAaron Smith } 12407ac1c780SAaron Smith // Sort them before value searching is working properly 12417ac1c780SAaron Smith m_func_full_names.Sort(); 12427ac1c780SAaron Smith m_func_full_names.SizeToFit(); 12437ac1c780SAaron Smith m_func_method_names.Sort(); 12447ac1c780SAaron Smith m_func_method_names.SizeToFit(); 12457ac1c780SAaron Smith m_func_base_names.Sort(); 12467ac1c780SAaron Smith m_func_base_names.SizeToFit(); 12477ac1c780SAaron Smith } 12487ac1c780SAaron Smith 1249b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindFunctions( 12500e4c4821SAdrian Prantl lldb_private::ConstString name, 1251b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, 1252117b1fa1SZachary Turner FunctionNameType name_type_mask, bool include_inlines, bool append, 1253b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 1254*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 12557ac1c780SAaron Smith if (!append) 12567ac1c780SAaron Smith sc_list.Clear(); 12577ac1c780SAaron Smith lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 12587ac1c780SAaron Smith 12597ac1c780SAaron Smith if (name_type_mask == eFunctionNameTypeNone) 12607ac1c780SAaron Smith return 0; 12617ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 12627ac1c780SAaron Smith return 0; 12637ac1c780SAaron Smith if (name.IsEmpty()) 12647ac1c780SAaron Smith return 0; 12657ac1c780SAaron Smith 12667ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 12674d4d63eeSPavel Labath if (name_type_mask & eFunctionNameTypeFull || 12684d4d63eeSPavel Labath name_type_mask & eFunctionNameTypeBase || 12697ac1c780SAaron Smith name_type_mask & eFunctionNameTypeMethod) { 12707ac1c780SAaron Smith CacheFunctionNames(); 12717ac1c780SAaron Smith 12727ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1273a5235af9SAleksandr Urakov auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list, 1274a5235af9SAleksandr Urakov &resolved_ids](UniqueCStringMap<uint32_t> &Names) { 12757ac1c780SAaron Smith std::vector<uint32_t> ids; 1276a5235af9SAleksandr Urakov if (!Names.GetValues(name, ids)) 1277a5235af9SAleksandr Urakov return; 1278a5235af9SAleksandr Urakov 1279a5235af9SAleksandr Urakov for (uint32_t id : ids) { 1280a5235af9SAleksandr Urakov if (resolved_ids.find(id) != resolved_ids.end()) 1281a5235af9SAleksandr Urakov continue; 1282a5235af9SAleksandr Urakov 1283a5235af9SAleksandr Urakov if (parent_decl_ctx && 1284a5235af9SAleksandr Urakov GetDeclContextContainingUID(id) != *parent_decl_ctx) 1285a5235af9SAleksandr Urakov continue; 1286a5235af9SAleksandr Urakov 12877ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 12887ac1c780SAaron Smith resolved_ids.insert(id); 12897ac1c780SAaron Smith } 12907ac1c780SAaron Smith }; 12917ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeFull) { 12927ac1c780SAaron Smith ResolveFn(m_func_full_names); 1293a5235af9SAleksandr Urakov ResolveFn(m_func_base_names); 1294a5235af9SAleksandr Urakov ResolveFn(m_func_method_names); 12957ac1c780SAaron Smith } 12967ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeBase) { 12977ac1c780SAaron Smith ResolveFn(m_func_base_names); 12987ac1c780SAaron Smith } 12997ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeMethod) { 13007ac1c780SAaron Smith ResolveFn(m_func_method_names); 13017ac1c780SAaron Smith } 13027ac1c780SAaron Smith } 13037ac1c780SAaron Smith return sc_list.GetSize() - old_size; 130474e08ca0SZachary Turner } 130574e08ca0SZachary Turner 130674e08ca0SZachary Turner uint32_t 1307b9c1b51eSKate Stone SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 1308b9c1b51eSKate Stone bool include_inlines, bool append, 1309b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 1310*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 13117ac1c780SAaron Smith if (!append) 13127ac1c780SAaron Smith sc_list.Clear(); 13137ac1c780SAaron Smith if (!regex.IsValid()) 13147ac1c780SAaron Smith return 0; 13157ac1c780SAaron Smith 13167ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 13177ac1c780SAaron Smith CacheFunctionNames(); 13187ac1c780SAaron Smith 13197ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1320c8316ed2SAaron Smith auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, 1321c8316ed2SAaron Smith this](UniqueCStringMap<uint32_t> &Names) { 13227ac1c780SAaron Smith std::vector<uint32_t> ids; 13237ac1c780SAaron Smith if (Names.GetValues(regex, ids)) { 13247ac1c780SAaron Smith for (auto id : ids) { 13257ac1c780SAaron Smith if (resolved_ids.find(id) == resolved_ids.end()) 13267ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 13277ac1c780SAaron Smith resolved_ids.insert(id); 13287ac1c780SAaron Smith } 13297ac1c780SAaron Smith } 13307ac1c780SAaron Smith }; 13317ac1c780SAaron Smith ResolveFn(m_func_full_names); 13327ac1c780SAaron Smith ResolveFn(m_func_base_names); 13337ac1c780SAaron Smith 13347ac1c780SAaron Smith return sc_list.GetSize() - old_size; 133574e08ca0SZachary Turner } 133674e08ca0SZachary Turner 1337b9c1b51eSKate Stone void SymbolFilePDB::GetMangledNamesForFunction( 1338b9c1b51eSKate Stone const std::string &scope_qualified_name, 1339b9c1b51eSKate Stone std::vector<lldb_private::ConstString> &mangled_names) {} 134074e08ca0SZachary Turner 13418cfb12b9SAleksandr Urakov void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) { 13428cfb12b9SAleksandr Urakov std::set<lldb::addr_t> sym_addresses; 13438cfb12b9SAleksandr Urakov for (size_t i = 0; i < symtab.GetNumSymbols(); i++) 13448cfb12b9SAleksandr Urakov sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress()); 13458cfb12b9SAleksandr Urakov 13468cfb12b9SAleksandr Urakov auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>(); 13478cfb12b9SAleksandr Urakov if (!results) 13488cfb12b9SAleksandr Urakov return; 13498cfb12b9SAleksandr Urakov 13508cfb12b9SAleksandr Urakov auto section_list = m_obj_file->GetSectionList(); 13518cfb12b9SAleksandr Urakov if (!section_list) 13528cfb12b9SAleksandr Urakov return; 13538cfb12b9SAleksandr Urakov 13548cfb12b9SAleksandr Urakov while (auto pub_symbol = results->getNext()) { 13557db8b5c4SPavel Labath auto section_id = pub_symbol->getAddressSection(); 13568cfb12b9SAleksandr Urakov 13577db8b5c4SPavel Labath auto section = section_list->FindSectionByID(section_id); 13588cfb12b9SAleksandr Urakov if (!section) 13598cfb12b9SAleksandr Urakov continue; 13608cfb12b9SAleksandr Urakov 13618cfb12b9SAleksandr Urakov auto offset = pub_symbol->getAddressOffset(); 13628cfb12b9SAleksandr Urakov 13638cfb12b9SAleksandr Urakov auto file_addr = section->GetFileAddress() + offset; 13648cfb12b9SAleksandr Urakov if (sym_addresses.find(file_addr) != sym_addresses.end()) 13658cfb12b9SAleksandr Urakov continue; 13668cfb12b9SAleksandr Urakov sym_addresses.insert(file_addr); 13678cfb12b9SAleksandr Urakov 13688cfb12b9SAleksandr Urakov auto size = pub_symbol->getLength(); 13698cfb12b9SAleksandr Urakov symtab.AddSymbol( 13708cfb12b9SAleksandr Urakov Symbol(pub_symbol->getSymIndexId(), // symID 13718cfb12b9SAleksandr Urakov pub_symbol->getName().c_str(), // name 13728cfb12b9SAleksandr Urakov true, // name_is_mangled 13738cfb12b9SAleksandr Urakov pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type 13748cfb12b9SAleksandr Urakov true, // external 13758cfb12b9SAleksandr Urakov false, // is_debug 13768cfb12b9SAleksandr Urakov false, // is_trampoline 13778cfb12b9SAleksandr Urakov false, // is_artificial 13788cfb12b9SAleksandr Urakov section, // section_sp 13798cfb12b9SAleksandr Urakov offset, // value 13808cfb12b9SAleksandr Urakov size, // size 13818cfb12b9SAleksandr Urakov size != 0, // size_is_valid 13828cfb12b9SAleksandr Urakov false, // contains_linker_annotations 13838cfb12b9SAleksandr Urakov 0 // flags 13848cfb12b9SAleksandr Urakov )); 13858cfb12b9SAleksandr Urakov } 13868cfb12b9SAleksandr Urakov 13878cfb12b9SAleksandr Urakov symtab.CalculateSymbolSizes(); 13888cfb12b9SAleksandr Urakov symtab.Finalize(); 13898cfb12b9SAleksandr Urakov } 13908cfb12b9SAleksandr Urakov 1391b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindTypes( 13920e4c4821SAdrian Prantl lldb_private::ConstString name, 1393b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 1394b9c1b51eSKate Stone uint32_t max_matches, 139574e08ca0SZachary Turner llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1396b9c1b51eSKate Stone lldb_private::TypeMap &types) { 1397*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 139842dff790SZachary Turner if (!append) 139942dff790SZachary Turner types.Clear(); 140042dff790SZachary Turner if (!name) 140142dff790SZachary Turner return 0; 14027ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 14037ac1c780SAaron Smith return 0; 140442dff790SZachary Turner 140542dff790SZachary Turner searched_symbol_files.clear(); 140642dff790SZachary Turner searched_symbol_files.insert(this); 140742dff790SZachary Turner 140886e9434dSAaron Smith // There is an assumption 'name' is not a regex 1409c1e530eeSAleksandr Urakov FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types); 141086e9434dSAaron Smith 141142dff790SZachary Turner return types.GetSize(); 141242dff790SZachary Turner } 141342dff790SZachary Turner 14144911023fSZachary Turner void SymbolFilePDB::DumpClangAST(Stream &s) { 14154911023fSZachary Turner auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 14164911023fSZachary Turner auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 14174911023fSZachary Turner if (!clang) 14184911023fSZachary Turner return; 14194911023fSZachary Turner clang->Dump(s); 14204911023fSZachary Turner } 14214911023fSZachary Turner 1422c8316ed2SAaron Smith void SymbolFilePDB::FindTypesByRegex( 1423c8316ed2SAaron Smith const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1424b9c1b51eSKate Stone lldb_private::TypeMap &types) { 1425b9c1b51eSKate Stone // When searching by regex, we need to go out of our way to limit the search 14269d0eb996SAdrian McCarthy // space as much as possible since this searches EVERYTHING in the PDB, 14279d0eb996SAdrian McCarthy // manually doing regex comparisons. PDB library isn't optimized for regex 14289d0eb996SAdrian McCarthy // searches or searches across multiple symbol types at the same time, so the 1429b9c1b51eSKate Stone // best we can do is to search enums, then typedefs, then classes one by one, 14309d0eb996SAdrian McCarthy // and do a regex comparison against each of them. 1431b9c1b51eSKate Stone PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 1432b9c1b51eSKate Stone PDB_SymType::UDT}; 143354fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 143442dff790SZachary Turner 143542dff790SZachary Turner uint32_t matches = 0; 143642dff790SZachary Turner 1437b9c1b51eSKate Stone for (auto tag : tags_to_search) { 143810a02577SAaron Smith results = m_global_scope_up->findAllChildren(tag); 143910a02577SAaron Smith if (!results) 144010a02577SAaron Smith continue; 144110a02577SAaron Smith 1442b9c1b51eSKate Stone while (auto result = results->getNext()) { 144342dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 144442dff790SZachary Turner break; 144542dff790SZachary Turner 144642dff790SZachary Turner std::string type_name; 144754fd7ff6SZachary Turner if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 144842dff790SZachary Turner type_name = enum_type->getName(); 1449b9c1b51eSKate Stone else if (auto typedef_type = 1450b9c1b51eSKate Stone llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 145142dff790SZachary Turner type_name = typedef_type->getName(); 145254fd7ff6SZachary Turner else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 145342dff790SZachary Turner type_name = class_type->getName(); 1454b9c1b51eSKate Stone else { 14559d0eb996SAdrian McCarthy // We're looking only for types that have names. Skip symbols, as well 14569d0eb996SAdrian McCarthy // as unnamed types such as arrays, pointers, etc. 145742dff790SZachary Turner continue; 145842dff790SZachary Turner } 145942dff790SZachary Turner 146086e9434dSAaron Smith if (!regex.Execute(type_name)) 146142dff790SZachary Turner continue; 146242dff790SZachary Turner 1463b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1464b9c1b51eSKate Stone // lookup. 146542dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 146642dff790SZachary Turner continue; 146742dff790SZachary Turner 146842dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 146942dff790SZachary Turner if (iter == m_types.end()) 147042dff790SZachary Turner continue; 147142dff790SZachary Turner types.Insert(iter->second); 147242dff790SZachary Turner ++matches; 147342dff790SZachary Turner } 147442dff790SZachary Turner } 147542dff790SZachary Turner } 147642dff790SZachary Turner 1477709426b3SAleksandr Urakov void SymbolFilePDB::FindTypesByName( 1478c1e530eeSAleksandr Urakov llvm::StringRef name, 1479709426b3SAleksandr Urakov const lldb_private::CompilerDeclContext *parent_decl_ctx, 1480709426b3SAleksandr Urakov uint32_t max_matches, lldb_private::TypeMap &types) { 148154fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 1482f76fe682SAaron Smith if (name.empty()) 1483f76fe682SAaron Smith return; 1484709426b3SAleksandr Urakov results = m_global_scope_up->findAllChildren(PDB_SymType::None); 148510a02577SAaron Smith if (!results) 148610a02577SAaron Smith return; 148742dff790SZachary Turner 148842dff790SZachary Turner uint32_t matches = 0; 148942dff790SZachary Turner 1490b9c1b51eSKate Stone while (auto result = results->getNext()) { 149142dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 149242dff790SZachary Turner break; 1493709426b3SAleksandr Urakov 1494c1e530eeSAleksandr Urakov if (MSVCUndecoratedNameParser::DropScope( 1495c1e530eeSAleksandr Urakov result->getRawSymbol().getName()) != name) 1496709426b3SAleksandr Urakov continue; 1497709426b3SAleksandr Urakov 1498b9c1b51eSKate Stone switch (result->getSymTag()) { 149954fd7ff6SZachary Turner case PDB_SymType::Enum: 150054fd7ff6SZachary Turner case PDB_SymType::UDT: 150154fd7ff6SZachary Turner case PDB_SymType::Typedef: 150242dff790SZachary Turner break; 150342dff790SZachary Turner default: 150405097246SAdrian Prantl // We're looking only for types that have names. Skip symbols, as well 150505097246SAdrian Prantl // as unnamed types such as arrays, pointers, etc. 150642dff790SZachary Turner continue; 150742dff790SZachary Turner } 150842dff790SZachary Turner 1509b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1510b9c1b51eSKate Stone // lookup. 151142dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 151242dff790SZachary Turner continue; 151342dff790SZachary Turner 1514a5235af9SAleksandr Urakov if (parent_decl_ctx && GetDeclContextContainingUID( 1515a5235af9SAleksandr Urakov result->getSymIndexId()) != *parent_decl_ctx) 1516709426b3SAleksandr Urakov continue; 1517709426b3SAleksandr Urakov 151842dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 151942dff790SZachary Turner if (iter == m_types.end()) 152042dff790SZachary Turner continue; 152142dff790SZachary Turner types.Insert(iter->second); 152242dff790SZachary Turner ++matches; 152342dff790SZachary Turner } 152474e08ca0SZachary Turner } 152574e08ca0SZachary Turner 1526b9c1b51eSKate Stone size_t SymbolFilePDB::FindTypes( 1527b9c1b51eSKate Stone const std::vector<lldb_private::CompilerContext> &contexts, bool append, 1528b9c1b51eSKate Stone lldb_private::TypeMap &types) { 152942dff790SZachary Turner return 0; 153074e08ca0SZachary Turner } 153174e08ca0SZachary Turner 1532c8316ed2SAaron Smith void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, 15337ac1c780SAaron Smith uint32_t type_mask, 15347ac1c780SAaron Smith TypeCollection &type_collection) { 15357ac1c780SAaron Smith bool can_parse = false; 1536e664b5dcSAaron Smith switch (pdb_symbol.getSymTag()) { 15377ac1c780SAaron Smith case PDB_SymType::ArrayType: 15387ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassArray) != 0); 15397ac1c780SAaron Smith break; 15407ac1c780SAaron Smith case PDB_SymType::BuiltinType: 15417ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassBuiltin) != 0); 15427ac1c780SAaron Smith break; 15437ac1c780SAaron Smith case PDB_SymType::Enum: 15447ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassEnumeration) != 0); 15457ac1c780SAaron Smith break; 15467ac1c780SAaron Smith case PDB_SymType::Function: 15477ac1c780SAaron Smith case PDB_SymType::FunctionSig: 15487ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassFunction) != 0); 15497ac1c780SAaron Smith break; 15507ac1c780SAaron Smith case PDB_SymType::PointerType: 15517ac1c780SAaron Smith can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | 15527ac1c780SAaron Smith eTypeClassMemberPointer)) != 0); 15537ac1c780SAaron Smith break; 15547ac1c780SAaron Smith case PDB_SymType::Typedef: 15557ac1c780SAaron Smith can_parse = ((type_mask & eTypeClassTypedef) != 0); 15567ac1c780SAaron Smith break; 15577ac1c780SAaron Smith case PDB_SymType::UDT: { 1558e664b5dcSAaron Smith auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); 15597ac1c780SAaron Smith assert(udt); 15607ac1c780SAaron Smith can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && 15617ac1c780SAaron Smith ((type_mask & (eTypeClassClass | eTypeClassStruct | 15627ac1c780SAaron Smith eTypeClassUnion)) != 0)); 15637ac1c780SAaron Smith } break; 1564c8316ed2SAaron Smith default: 1565c8316ed2SAaron Smith break; 15667ac1c780SAaron Smith } 15677ac1c780SAaron Smith 15687ac1c780SAaron Smith if (can_parse) { 1569e664b5dcSAaron Smith if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { 15707ac1c780SAaron Smith auto result = 15717ac1c780SAaron Smith std::find(type_collection.begin(), type_collection.end(), type); 15727ac1c780SAaron Smith if (result == type_collection.end()) 15737ac1c780SAaron Smith type_collection.push_back(type); 15747ac1c780SAaron Smith } 15757ac1c780SAaron Smith } 15767ac1c780SAaron Smith 1577e664b5dcSAaron Smith auto results_up = pdb_symbol.findAllChildren(); 15787ac1c780SAaron Smith while (auto symbol_up = results_up->getNext()) 1579e664b5dcSAaron Smith GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); 15807ac1c780SAaron Smith } 15817ac1c780SAaron Smith 1582b9c1b51eSKate Stone size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1583117b1fa1SZachary Turner TypeClass type_mask, 1584b9c1b51eSKate Stone lldb_private::TypeList &type_list) { 1585*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 15867ac1c780SAaron Smith TypeCollection type_collection; 15877ac1c780SAaron Smith uint32_t old_size = type_list.GetSize(); 1588c8316ed2SAaron Smith CompileUnit *cu = 1589c8316ed2SAaron Smith sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 15907ac1c780SAaron Smith if (cu) { 15917ac1c780SAaron Smith auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1592e664b5dcSAaron Smith if (!compiland_up) 1593e664b5dcSAaron Smith return 0; 1594e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 15957ac1c780SAaron Smith } else { 15967ac1c780SAaron Smith for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 15977ac1c780SAaron Smith auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1598d5a925f4SAaron Smith if (cu_sp) { 1599e664b5dcSAaron Smith if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1600e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 16017ac1c780SAaron Smith } 16027ac1c780SAaron Smith } 16037ac1c780SAaron Smith } 16047ac1c780SAaron Smith 16057ac1c780SAaron Smith for (auto type : type_collection) { 16067ac1c780SAaron Smith type->GetForwardCompilerType(); 16077ac1c780SAaron Smith type_list.Insert(type->shared_from_this()); 16087ac1c780SAaron Smith } 16097ac1c780SAaron Smith return type_list.GetSize() - old_size; 161074e08ca0SZachary Turner } 161174e08ca0SZachary Turner 161274e08ca0SZachary Turner lldb_private::TypeSystem * 1613b9c1b51eSKate Stone SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1614b9c1b51eSKate Stone auto type_system = 1615b9c1b51eSKate Stone m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 161674e08ca0SZachary Turner if (type_system) 161774e08ca0SZachary Turner type_system->SetSymbolFile(this); 161874e08ca0SZachary Turner return type_system; 161974e08ca0SZachary Turner } 162074e08ca0SZachary Turner 1621c68925abSZachary Turner PDBASTParser *SymbolFilePDB::GetPDBAstParser() { 1622c68925abSZachary Turner auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1623c68925abSZachary Turner auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 1624c68925abSZachary Turner if (!clang_type_system) 1625c68925abSZachary Turner return nullptr; 1626c68925abSZachary Turner 1627c68925abSZachary Turner return clang_type_system->GetPDBParser(); 1628c68925abSZachary Turner } 1629c68925abSZachary Turner 1630c68925abSZachary Turner 1631b9c1b51eSKate Stone lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 16320e4c4821SAdrian Prantl lldb_private::ConstString name, 1633b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx) { 1634*656ddeb2SPavel Labath std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1635709426b3SAleksandr Urakov auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1636709426b3SAleksandr Urakov auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 1637709426b3SAleksandr Urakov if (!clang_type_system) 1638709426b3SAleksandr Urakov return CompilerDeclContext(); 1639709426b3SAleksandr Urakov 1640709426b3SAleksandr Urakov PDBASTParser *pdb = clang_type_system->GetPDBParser(); 1641709426b3SAleksandr Urakov if (!pdb) 1642709426b3SAleksandr Urakov return CompilerDeclContext(); 1643709426b3SAleksandr Urakov 1644709426b3SAleksandr Urakov clang::DeclContext *decl_context = nullptr; 1645709426b3SAleksandr Urakov if (parent_decl_ctx) 1646709426b3SAleksandr Urakov decl_context = static_cast<clang::DeclContext *>( 1647709426b3SAleksandr Urakov parent_decl_ctx->GetOpaqueDeclContext()); 1648709426b3SAleksandr Urakov 1649709426b3SAleksandr Urakov auto namespace_decl = 1650709426b3SAleksandr Urakov pdb->FindNamespaceDecl(decl_context, name.GetStringRef()); 1651709426b3SAleksandr Urakov if (!namespace_decl) 1652709426b3SAleksandr Urakov return CompilerDeclContext(); 1653709426b3SAleksandr Urakov 1654709426b3SAleksandr Urakov return CompilerDeclContext(type_system, 1655709426b3SAleksandr Urakov static_cast<clang::DeclContext *>(namespace_decl)); 165674e08ca0SZachary Turner } 165774e08ca0SZachary Turner 1658b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginName() { 165974e08ca0SZachary Turner static ConstString g_name("pdb"); 166074e08ca0SZachary Turner return g_name; 166174e08ca0SZachary Turner } 166274e08ca0SZachary Turner 1663b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 166474e08ca0SZachary Turner 1665b9c1b51eSKate Stone IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1666b9c1b51eSKate Stone 1667b9c1b51eSKate Stone const IPDBSession &SymbolFilePDB::GetPDBSession() const { 166842dff790SZachary Turner return *m_session_up; 166942dff790SZachary Turner } 167042dff790SZachary Turner 1671c8316ed2SAaron Smith lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, 1672c8316ed2SAaron Smith uint32_t index) { 167374e08ca0SZachary Turner auto found_cu = m_comp_units.find(id); 167474e08ca0SZachary Turner if (found_cu != m_comp_units.end()) 167574e08ca0SZachary Turner return found_cu->second; 167674e08ca0SZachary Turner 167710a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(id); 167810a02577SAaron Smith if (!compiland_up) 167910a02577SAaron Smith return CompUnitSP(); 168074e08ca0SZachary Turner 168174e08ca0SZachary Turner lldb::LanguageType lang; 168210a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 168374e08ca0SZachary Turner if (!details) 168474e08ca0SZachary Turner lang = lldb::eLanguageTypeC_plus_plus; 168574e08ca0SZachary Turner else 168674e08ca0SZachary Turner lang = TranslateLanguage(details->getLanguage()); 168774e08ca0SZachary Turner 1688f76fe682SAaron Smith if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1689f76fe682SAaron Smith return CompUnitSP(); 1690f76fe682SAaron Smith 1691487b0c6bSAaron Smith std::string path = compiland_up->getSourceFileFullPath(); 1692f76fe682SAaron Smith if (path.empty()) 1693f76fe682SAaron Smith return CompUnitSP(); 1694f76fe682SAaron Smith 1695b9c1b51eSKate Stone // Don't support optimized code for now, DebugInfoPDB does not return this 1696b9c1b51eSKate Stone // information. 1697ad2b63cbSGreg Clayton LazyBool optimized = eLazyBoolNo; 1698c8316ed2SAaron Smith auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, 1699c8316ed2SAaron Smith path.c_str(), id, lang, optimized); 170010a02577SAaron Smith 170110a02577SAaron Smith if (!cu_sp) 170210a02577SAaron Smith return CompUnitSP(); 170310a02577SAaron Smith 170410a02577SAaron Smith m_comp_units.insert(std::make_pair(id, cu_sp)); 170510a02577SAaron Smith if (index == UINT32_MAX) 1706e664b5dcSAaron Smith GetCompileUnitIndex(*compiland_up, index); 170710a02577SAaron Smith lldbassert(index != UINT32_MAX); 1708e0119909SPavel Labath SetCompileUnitAtIndex(index, cu_sp); 170910a02577SAaron Smith return cu_sp; 171074e08ca0SZachary Turner } 171174e08ca0SZachary Turner 1712863f8c18SZachary Turner bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit, 1713863f8c18SZachary Turner uint32_t match_line) { 1714863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 171510a02577SAaron Smith if (!compiland_up) 171610a02577SAaron Smith return false; 171774e08ca0SZachary Turner 1718b9c1b51eSKate Stone // LineEntry needs the *index* of the file into the list of support files 17199d0eb996SAdrian McCarthy // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 172005097246SAdrian Prantl // a globally unique idenfitifier in the namespace of the PDB. So, we have 172105097246SAdrian Prantl // to do a mapping so that we can hand out indices. 172242dff790SZachary Turner llvm::DenseMap<uint32_t, uint32_t> index_map; 172310a02577SAaron Smith BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 1724863f8c18SZachary Turner auto line_table = llvm::make_unique<LineTable>(&comp_unit); 172574e08ca0SZachary Turner 172610a02577SAaron Smith // Find contributions to `compiland` from all source and header files. 1727863f8c18SZachary Turner std::string path = comp_unit.GetPath(); 172810a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 172910a02577SAaron Smith if (!files) 173010a02577SAaron Smith return false; 173174e08ca0SZachary Turner 173205097246SAdrian Prantl // For each source and header file, create a LineSequence for contributions 173305097246SAdrian Prantl // to the compiland from that file, and add the sequence. 1734b9c1b51eSKate Stone while (auto file = files->getNext()) { 1735b9c1b51eSKate Stone std::unique_ptr<LineSequence> sequence( 1736b9c1b51eSKate Stone line_table->CreateLineSequenceContainer()); 173710a02577SAaron Smith auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 173810a02577SAaron Smith if (!lines) 173910a02577SAaron Smith continue; 174074e08ca0SZachary Turner int entry_count = lines->getChildCount(); 174174e08ca0SZachary Turner 17427e8c7beaSZachary Turner uint64_t prev_addr; 17437e8c7beaSZachary Turner uint32_t prev_length; 17447e8c7beaSZachary Turner uint32_t prev_line; 17457e8c7beaSZachary Turner uint32_t prev_source_idx; 17467e8c7beaSZachary Turner 1747b9c1b51eSKate Stone for (int i = 0; i < entry_count; ++i) { 174874e08ca0SZachary Turner auto line = lines->getChildAtIndex(i); 174974e08ca0SZachary Turner 17507e8c7beaSZachary Turner uint64_t lno = line->getLineNumber(); 17517e8c7beaSZachary Turner uint64_t addr = line->getVirtualAddress(); 17527e8c7beaSZachary Turner uint32_t length = line->getLength(); 175374e08ca0SZachary Turner uint32_t source_id = line->getSourceFileId(); 17547e8c7beaSZachary Turner uint32_t col = line->getColumnNumber(); 175574e08ca0SZachary Turner uint32_t source_idx = index_map[source_id]; 175674e08ca0SZachary Turner 175705097246SAdrian Prantl // There was a gap between the current entry and the previous entry if 175805097246SAdrian Prantl // the addresses don't perfectly line up. 17597e8c7beaSZachary Turner bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 17607e8c7beaSZachary Turner 1761b9c1b51eSKate Stone // Before inserting the current entry, insert a terminal entry at the end 17629d0eb996SAdrian McCarthy // of the previous entry's address range if the current entry resulted in 17639d0eb996SAdrian McCarthy // a gap from the previous entry. 1764b9c1b51eSKate Stone if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1765b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1766b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 17677e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 1768010edd37SAaron Smith 1769010edd37SAaron Smith line_table->InsertSequence(sequence.release()); 1770010edd37SAaron Smith sequence.reset(line_table->CreateLineSequenceContainer()); 17717e8c7beaSZachary Turner } 17727e8c7beaSZachary Turner 1773b9c1b51eSKate Stone if (ShouldAddLine(match_line, lno, length)) { 17747e8c7beaSZachary Turner bool is_statement = line->isStatement(); 177574e08ca0SZachary Turner bool is_prologue = false; 177674e08ca0SZachary Turner bool is_epilogue = false; 1777b9c1b51eSKate Stone auto func = 1778b9c1b51eSKate Stone m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1779b9c1b51eSKate Stone if (func) { 178054fd7ff6SZachary Turner auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 178110a02577SAaron Smith if (prologue) 17827e8c7beaSZachary Turner is_prologue = (addr == prologue->getVirtualAddress()); 178374e08ca0SZachary Turner 178454fd7ff6SZachary Turner auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 178510a02577SAaron Smith if (epilogue) 17867e8c7beaSZachary Turner is_epilogue = (addr == epilogue->getVirtualAddress()); 17877e8c7beaSZachary Turner } 178874e08ca0SZachary Turner 1789b9c1b51eSKate Stone line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1790b9c1b51eSKate Stone source_idx, is_statement, false, 17917e8c7beaSZachary Turner is_prologue, is_epilogue, false); 17927e8c7beaSZachary Turner } 17937e8c7beaSZachary Turner 17947e8c7beaSZachary Turner prev_addr = addr; 17957e8c7beaSZachary Turner prev_length = length; 17967e8c7beaSZachary Turner prev_line = lno; 17977e8c7beaSZachary Turner prev_source_idx = source_idx; 17987e8c7beaSZachary Turner } 17997e8c7beaSZachary Turner 1800b9c1b51eSKate Stone if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 18017e8c7beaSZachary Turner // The end is always a terminal entry, so insert it regardless. 1802b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1803b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 18047e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 180574e08ca0SZachary Turner } 180674e08ca0SZachary Turner 18077e8c7beaSZachary Turner line_table->InsertSequence(sequence.release()); 180874e08ca0SZachary Turner } 180974e08ca0SZachary Turner 181010a02577SAaron Smith if (line_table->GetSize()) { 1811863f8c18SZachary Turner comp_unit.SetLineTable(line_table.release()); 181274e08ca0SZachary Turner return true; 181374e08ca0SZachary Turner } 181410a02577SAaron Smith return false; 181510a02577SAaron Smith } 181674e08ca0SZachary Turner 1817b9c1b51eSKate Stone void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 181810a02577SAaron Smith const PDBSymbolCompiland &compiland, 1819b9c1b51eSKate Stone llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 182005097246SAdrian Prantl // This is a hack, but we need to convert the source id into an index into 182105097246SAdrian Prantl // the support files array. We don't want to do path comparisons to avoid 18229d0eb996SAdrian McCarthy // basename / full path issues that may or may not even be a problem, so we 18239d0eb996SAdrian McCarthy // use the globally unique source file identifiers. Ideally we could use the 18249d0eb996SAdrian McCarthy // global identifiers everywhere, but LineEntry currently assumes indices. 182510a02577SAaron Smith auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 182610a02577SAaron Smith if (!source_files) 182710a02577SAaron Smith return; 18289ea80d25SPavel Labath 18299ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based) 18309ea80d25SPavel Labath int index = 1; 183174e08ca0SZachary Turner 1832b9c1b51eSKate Stone while (auto file = source_files->getNext()) { 183374e08ca0SZachary Turner uint32_t source_id = file->getUniqueId(); 183474e08ca0SZachary Turner index_map[source_id] = index++; 183574e08ca0SZachary Turner } 183674e08ca0SZachary Turner } 18377ac1c780SAaron Smith 18387ac1c780SAaron Smith lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 18397ac1c780SAaron Smith const lldb_private::Address &so_addr) { 18407ac1c780SAaron Smith lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1841308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 18427ac1c780SAaron Smith return nullptr; 18437ac1c780SAaron Smith 1844308e39caSAaron Smith // If it is a PDB function's vm addr, this is the first sure bet. 1845308e39caSAaron Smith if (auto lines = 1846308e39caSAaron Smith m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { 1847308e39caSAaron Smith if (auto first_line = lines->getNext()) 1848308e39caSAaron Smith return ParseCompileUnitForUID(first_line->getCompilandId()); 18497ac1c780SAaron Smith } 18507ac1c780SAaron Smith 1851308e39caSAaron Smith // Otherwise we resort to section contributions. 1852308e39caSAaron Smith if (auto sec_contribs = m_session_up->getSectionContribs()) { 1853308e39caSAaron Smith while (auto section = sec_contribs->getNext()) { 1854308e39caSAaron Smith auto va = section->getVirtualAddress(); 1855308e39caSAaron Smith if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) 1856308e39caSAaron Smith return ParseCompileUnitForUID(section->getCompilandId()); 1857308e39caSAaron Smith } 1858308e39caSAaron Smith } 18597ac1c780SAaron Smith return nullptr; 18607ac1c780SAaron Smith } 18617ac1c780SAaron Smith 18627ac1c780SAaron Smith Mangled 1863e664b5dcSAaron Smith SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 18647ac1c780SAaron Smith Mangled mangled; 1865e664b5dcSAaron Smith auto func_name = pdb_func.getName(); 1866e664b5dcSAaron Smith auto func_undecorated_name = pdb_func.getUndecoratedName(); 18677ac1c780SAaron Smith std::string func_decorated_name; 18687ac1c780SAaron Smith 18697ac1c780SAaron Smith // Seek from public symbols for non-static function's decorated name if any. 18707ac1c780SAaron Smith // For static functions, they don't have undecorated names and aren't exposed 18717ac1c780SAaron Smith // in Public Symbols either. 18727ac1c780SAaron Smith if (!func_undecorated_name.empty()) { 1873c8316ed2SAaron Smith auto result_up = m_global_scope_up->findChildren( 1874c8316ed2SAaron Smith PDB_SymType::PublicSymbol, func_undecorated_name, 18757ac1c780SAaron Smith PDB_NameSearchFlags::NS_UndecoratedName); 18767ac1c780SAaron Smith if (result_up) { 18777ac1c780SAaron Smith while (auto symbol_up = result_up->getNext()) { 18787ac1c780SAaron Smith // For a public symbol, it is unique. 18797ac1c780SAaron Smith lldbassert(result_up->getChildCount() == 1); 18807ac1c780SAaron Smith if (auto *pdb_public_sym = 1881c8316ed2SAaron Smith llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( 1882c8316ed2SAaron Smith symbol_up.get())) { 18837ac1c780SAaron Smith if (pdb_public_sym->isFunction()) { 18847ac1c780SAaron Smith func_decorated_name = pdb_public_sym->getName(); 1885f76fe682SAaron Smith break; 18867ac1c780SAaron Smith } 18877ac1c780SAaron Smith } 18887ac1c780SAaron Smith } 18897ac1c780SAaron Smith } 18907ac1c780SAaron Smith } 18917ac1c780SAaron Smith if (!func_decorated_name.empty()) { 18927ac1c780SAaron Smith mangled.SetMangledName(ConstString(func_decorated_name)); 18937ac1c780SAaron Smith 18947ac1c780SAaron Smith // For MSVC, format of C funciton's decorated name depends on calling 18957ac1c780SAaron Smith // conventon. Unfortunately none of the format is recognized by current 18967ac1c780SAaron Smith // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 189705097246SAdrian Prantl // `__purecall` is retrieved as both its decorated and undecorated name 189805097246SAdrian Prantl // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` 189905097246SAdrian Prantl // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). 190005097246SAdrian Prantl // Mangled::GetDemangledName method will fail internally and caches an 190105097246SAdrian Prantl // empty string as its undecorated name. So we will face a contradition 190205097246SAdrian Prantl // here for the same symbol: 19037ac1c780SAaron Smith // non-empty undecorated name from PDB 19047ac1c780SAaron Smith // empty undecorated name from LLDB 19057ac1c780SAaron Smith if (!func_undecorated_name.empty() && 19067ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) 19077ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19087ac1c780SAaron Smith 19097ac1c780SAaron Smith // LLDB uses several flags to control how a C++ decorated name is 191005097246SAdrian Prantl // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the 191105097246SAdrian Prantl // yielded name could be different from what we retrieve from 19127ac1c780SAaron Smith // PDB source unless we also apply same flags in getting undecorated 19137ac1c780SAaron Smith // name through PDBSymbolFunc::getUndecoratedNameEx method. 19147ac1c780SAaron Smith if (!func_undecorated_name.empty() && 19157ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()) != 19167ac1c780SAaron Smith ConstString(func_undecorated_name)) 19177ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19187ac1c780SAaron Smith } else if (!func_undecorated_name.empty()) { 19197ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19207ac1c780SAaron Smith } else if (!func_name.empty()) 19217ac1c780SAaron Smith mangled.SetValue(ConstString(func_name), false); 19227ac1c780SAaron Smith 19237ac1c780SAaron Smith return mangled; 19247ac1c780SAaron Smith } 19257ac1c780SAaron Smith 19267ac1c780SAaron Smith bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 19277ac1c780SAaron Smith const lldb_private::CompilerDeclContext *decl_ctx) { 19287ac1c780SAaron Smith if (decl_ctx == nullptr || !decl_ctx->IsValid()) 19297ac1c780SAaron Smith return true; 19307ac1c780SAaron Smith 19317ac1c780SAaron Smith TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 19327ac1c780SAaron Smith if (!decl_ctx_type_system) 19337ac1c780SAaron Smith return false; 19347ac1c780SAaron Smith TypeSystem *type_system = GetTypeSystemForLanguage( 19357ac1c780SAaron Smith decl_ctx_type_system->GetMinimumLanguage(nullptr)); 19367ac1c780SAaron Smith if (decl_ctx_type_system == type_system) 19377ac1c780SAaron Smith return true; // The type systems match, return true 19387ac1c780SAaron Smith 19397ac1c780SAaron Smith return false; 19407ac1c780SAaron Smith } 1941356aa4a9SAleksandr Urakov 1942356aa4a9SAleksandr Urakov uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) { 1943356aa4a9SAleksandr Urakov static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) { 1944356aa4a9SAleksandr Urakov return lhs < rhs.Offset; 1945356aa4a9SAleksandr Urakov }; 1946356aa4a9SAleksandr Urakov 1947356aa4a9SAleksandr Urakov // Cache section contributions 1948356aa4a9SAleksandr Urakov if (m_sec_contribs.empty()) { 1949356aa4a9SAleksandr Urakov if (auto SecContribs = m_session_up->getSectionContribs()) { 1950356aa4a9SAleksandr Urakov while (auto SectionContrib = SecContribs->getNext()) { 1951356aa4a9SAleksandr Urakov auto comp_id = SectionContrib->getCompilandId(); 1952356aa4a9SAleksandr Urakov if (!comp_id) 1953356aa4a9SAleksandr Urakov continue; 1954356aa4a9SAleksandr Urakov 1955356aa4a9SAleksandr Urakov auto sec = SectionContrib->getAddressSection(); 1956356aa4a9SAleksandr Urakov auto &sec_cs = m_sec_contribs[sec]; 1957356aa4a9SAleksandr Urakov 1958356aa4a9SAleksandr Urakov auto offset = SectionContrib->getAddressOffset(); 1959356aa4a9SAleksandr Urakov auto it = 1960356aa4a9SAleksandr Urakov std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper); 1961356aa4a9SAleksandr Urakov 1962356aa4a9SAleksandr Urakov auto size = SectionContrib->getLength(); 1963356aa4a9SAleksandr Urakov sec_cs.insert(it, {offset, size, comp_id}); 1964356aa4a9SAleksandr Urakov } 1965356aa4a9SAleksandr Urakov } 1966356aa4a9SAleksandr Urakov } 1967356aa4a9SAleksandr Urakov 1968356aa4a9SAleksandr Urakov // Check by line number 1969356aa4a9SAleksandr Urakov if (auto Lines = data.getLineNumbers()) { 1970356aa4a9SAleksandr Urakov if (auto FirstLine = Lines->getNext()) 1971356aa4a9SAleksandr Urakov return FirstLine->getCompilandId(); 1972356aa4a9SAleksandr Urakov } 1973356aa4a9SAleksandr Urakov 1974356aa4a9SAleksandr Urakov // Retrieve section + offset 1975356aa4a9SAleksandr Urakov uint32_t DataSection = data.getAddressSection(); 1976356aa4a9SAleksandr Urakov uint32_t DataOffset = data.getAddressOffset(); 1977356aa4a9SAleksandr Urakov if (DataSection == 0) { 1978356aa4a9SAleksandr Urakov if (auto RVA = data.getRelativeVirtualAddress()) 1979356aa4a9SAleksandr Urakov m_session_up->addressForRVA(RVA, DataSection, DataOffset); 1980356aa4a9SAleksandr Urakov } 1981356aa4a9SAleksandr Urakov 1982356aa4a9SAleksandr Urakov if (DataSection) { 1983356aa4a9SAleksandr Urakov // Search by section contributions 1984356aa4a9SAleksandr Urakov auto &sec_cs = m_sec_contribs[DataSection]; 1985356aa4a9SAleksandr Urakov auto it = 1986356aa4a9SAleksandr Urakov std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper); 1987356aa4a9SAleksandr Urakov if (it != sec_cs.begin()) { 1988356aa4a9SAleksandr Urakov --it; 1989356aa4a9SAleksandr Urakov if (DataOffset < it->Offset + it->Size) 1990356aa4a9SAleksandr Urakov return it->CompilandId; 1991356aa4a9SAleksandr Urakov } 1992356aa4a9SAleksandr Urakov } else { 1993356aa4a9SAleksandr Urakov // Search in lexical tree 1994356aa4a9SAleksandr Urakov auto LexParentId = data.getLexicalParentId(); 1995356aa4a9SAleksandr Urakov while (auto LexParent = m_session_up->getSymbolById(LexParentId)) { 1996356aa4a9SAleksandr Urakov if (LexParent->getSymTag() == PDB_SymType::Exe) 1997356aa4a9SAleksandr Urakov break; 1998356aa4a9SAleksandr Urakov if (LexParent->getSymTag() == PDB_SymType::Compiland) 1999356aa4a9SAleksandr Urakov return LexParentId; 2000356aa4a9SAleksandr Urakov LexParentId = LexParent->getRawSymbol().getLexicalParentId(); 2001356aa4a9SAleksandr Urakov } 2002356aa4a9SAleksandr Urakov } 2003356aa4a9SAleksandr Urakov 2004356aa4a9SAleksandr Urakov return 0; 2005356aa4a9SAleksandr Urakov } 2006