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; 6774e08ca0SZachary Turner default: 6874e08ca0SZachary Turner return lldb::LanguageType::eLanguageTypeUnknown; 6974e08ca0SZachary Turner } 7074e08ca0SZachary Turner } 717e8c7beaSZachary Turner 72b9c1b51eSKate Stone bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 73b9c1b51eSKate Stone uint32_t addr_length) { 74b9c1b51eSKate Stone return ((requested_line == 0 || actual_line == requested_line) && 75b9c1b51eSKate Stone addr_length > 0); 767e8c7beaSZachary Turner } 77c8316ed2SAaron Smith } // namespace 7874e08ca0SZachary Turner 79307f5ae8SZachary Turner static bool ShouldUseNativeReader() { 8071970b72SGreg Clayton #if defined(_WIN32) 81307f5ae8SZachary Turner llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER"); 82307f5ae8SZachary Turner return use_native.equals_lower("on") || use_native.equals_lower("yes") || 83307f5ae8SZachary Turner use_native.equals_lower("1") || use_native.equals_lower("true"); 8471970b72SGreg Clayton #else 8571970b72SGreg Clayton return true; 8671970b72SGreg Clayton #endif 87307f5ae8SZachary Turner } 88307f5ae8SZachary Turner 89b9c1b51eSKate Stone void SymbolFilePDB::Initialize() { 90307f5ae8SZachary Turner if (ShouldUseNativeReader()) { 91307f5ae8SZachary Turner npdb::SymbolFileNativePDB::Initialize(); 92307f5ae8SZachary Turner } else { 93b9c1b51eSKate Stone PluginManager::RegisterPlugin(GetPluginNameStatic(), 94b9c1b51eSKate Stone GetPluginDescriptionStatic(), CreateInstance, 9574e08ca0SZachary Turner DebuggerInitialize); 9674e08ca0SZachary Turner } 97307f5ae8SZachary Turner } 9874e08ca0SZachary Turner 99b9c1b51eSKate Stone void SymbolFilePDB::Terminate() { 100307f5ae8SZachary Turner if (ShouldUseNativeReader()) { 101307f5ae8SZachary Turner npdb::SymbolFileNativePDB::Terminate(); 102307f5ae8SZachary Turner } else { 10374e08ca0SZachary Turner PluginManager::UnregisterPlugin(CreateInstance); 10474e08ca0SZachary Turner } 105307f5ae8SZachary Turner } 10674e08ca0SZachary Turner 107b9c1b51eSKate Stone void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 10874e08ca0SZachary Turner 109b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 11074e08ca0SZachary Turner static ConstString g_name("pdb"); 11174e08ca0SZachary Turner return g_name; 11274e08ca0SZachary Turner } 11374e08ca0SZachary Turner 114b9c1b51eSKate Stone const char *SymbolFilePDB::GetPluginDescriptionStatic() { 11574e08ca0SZachary Turner return "Microsoft PDB debug symbol file reader."; 11674e08ca0SZachary Turner } 11774e08ca0SZachary Turner 11874e08ca0SZachary Turner lldb_private::SymbolFile * 119b9c1b51eSKate Stone SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { 12074e08ca0SZachary Turner return new SymbolFilePDB(obj_file); 12174e08ca0SZachary Turner } 12274e08ca0SZachary Turner 12374e08ca0SZachary Turner SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 12410a02577SAaron Smith : SymbolFile(object_file), m_session_up(), m_global_scope_up(), 12510a02577SAaron Smith m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} 12674e08ca0SZachary Turner 127b9c1b51eSKate Stone SymbolFilePDB::~SymbolFilePDB() {} 12874e08ca0SZachary Turner 129b9c1b51eSKate Stone uint32_t SymbolFilePDB::CalculateAbilities() { 1301f8552abSAaron Smith uint32_t abilities = 0; 1311f8552abSAaron Smith if (!m_obj_file) 1321f8552abSAaron Smith return 0; 1331f8552abSAaron Smith 134b9c1b51eSKate Stone if (!m_session_up) { 13574e08ca0SZachary Turner // Lazily load and match the PDB file, but only do this once. 13674e08ca0SZachary Turner std::string exePath = m_obj_file->GetFileSpec().GetPath(); 137b9c1b51eSKate Stone auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 138b9c1b51eSKate Stone m_session_up); 139b9c1b51eSKate Stone if (error) { 1404fd6a960SZachary Turner llvm::consumeError(std::move(error)); 1411f8552abSAaron Smith auto module_sp = m_obj_file->GetModule(); 1421f8552abSAaron Smith if (!module_sp) 1431f8552abSAaron Smith return 0; 1441f8552abSAaron Smith // See if any symbol file is specified through `--symfile` option. 1451f8552abSAaron Smith FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 1461f8552abSAaron Smith if (!symfile) 1471f8552abSAaron Smith return 0; 1481f8552abSAaron Smith error = loadDataForPDB(PDB_ReaderType::DIA, 149c8316ed2SAaron Smith llvm::StringRef(symfile.GetPath()), m_session_up); 1501f8552abSAaron Smith if (error) { 1511f8552abSAaron Smith llvm::consumeError(std::move(error)); 15274e08ca0SZachary Turner return 0; 15374e08ca0SZachary Turner } 154b8d8c62bSPavel Labath } 1551f8552abSAaron Smith } 156d5a925f4SAaron Smith if (!m_session_up) 1571f8552abSAaron Smith return 0; 1581f8552abSAaron Smith 1591f8552abSAaron Smith auto enum_tables_up = m_session_up->getEnumTables(); 1601f8552abSAaron Smith if (!enum_tables_up) 1611f8552abSAaron Smith return 0; 1621f8552abSAaron Smith while (auto table_up = enum_tables_up->getNext()) { 1631f8552abSAaron Smith if (table_up->getItemCount() == 0) 1641f8552abSAaron Smith continue; 1651f8552abSAaron Smith auto type = table_up->getTableType(); 1661f8552abSAaron Smith switch (type) { 1671f8552abSAaron Smith case PDB_TableType::Symbols: 1681f8552abSAaron Smith // This table represents a store of symbols with types listed in 1691f8552abSAaron Smith // PDBSym_Type 170c8316ed2SAaron Smith abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | 171c8316ed2SAaron Smith LocalVariables | VariableTypes); 1721f8552abSAaron Smith break; 1731f8552abSAaron Smith case PDB_TableType::LineNumbers: 1741f8552abSAaron Smith abilities |= LineTables; 1751f8552abSAaron Smith break; 176c8316ed2SAaron Smith default: 177c8316ed2SAaron Smith break; 1781f8552abSAaron Smith } 1791f8552abSAaron Smith } 1801f8552abSAaron Smith return abilities; 18174e08ca0SZachary Turner } 18274e08ca0SZachary Turner 183b9c1b51eSKate Stone void SymbolFilePDB::InitializeObject() { 184*d1304bbaSPavel Labath lldb::addr_t obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress(); 185c8316ed2SAaron Smith lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); 18674e08ca0SZachary Turner m_session_up->setLoadAddress(obj_load_address); 18710a02577SAaron Smith if (!m_global_scope_up) 18810a02577SAaron Smith m_global_scope_up = m_session_up->getGlobalScope(); 18910a02577SAaron Smith lldbassert(m_global_scope_up.get()); 19042dff790SZachary Turner 191b9c1b51eSKate Stone TypeSystem *type_system = 192b9c1b51eSKate Stone GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 193b9c1b51eSKate Stone ClangASTContext *clang_type_system = 194b9c1b51eSKate Stone llvm::dyn_cast_or_null<ClangASTContext>(type_system); 19510a02577SAaron Smith lldbassert(clang_type_system); 196b9c1b51eSKate Stone m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( 197b9c1b51eSKate Stone type_system, clang_type_system->GetTranslationUnitDecl()); 19874e08ca0SZachary Turner } 19974e08ca0SZachary Turner 200b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetNumCompileUnits() { 201b9c1b51eSKate Stone if (m_cached_compile_unit_count == 0) { 20210a02577SAaron Smith auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 20310a02577SAaron Smith if (!compilands) 20410a02577SAaron Smith return 0; 20510a02577SAaron Smith 20610a02577SAaron Smith // The linker could link *.dll (compiland language = LINK), or import 20705097246SAdrian Prantl // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be 20805097246SAdrian Prantl // found as a child of the global scope (PDB executable). Usually, such 20905097246SAdrian Prantl // compilands contain `thunk` symbols in which we are not interested for 21005097246SAdrian Prantl // now. However we still count them in the compiland list. If we perform 21105097246SAdrian Prantl // any compiland related activity, like finding symbols through 21205097246SAdrian Prantl // llvm::pdb::IPDBSession methods, such compilands will all be searched 21305097246SAdrian Prantl // automatically no matter whether we include them or not. 21474e08ca0SZachary Turner m_cached_compile_unit_count = compilands->getChildCount(); 21574e08ca0SZachary Turner 216b9c1b51eSKate Stone // The linker can inject an additional "dummy" compilation unit into the 2179d0eb996SAdrian McCarthy // PDB. Ignore this special compile unit for our purposes, if it is there. 2189d0eb996SAdrian McCarthy // It is always the last one. 21910a02577SAaron Smith auto last_compiland_up = 22010a02577SAaron Smith compilands->getChildAtIndex(m_cached_compile_unit_count - 1); 22110a02577SAaron Smith lldbassert(last_compiland_up.get()); 22210a02577SAaron Smith std::string name = last_compiland_up->getName(); 22374e08ca0SZachary Turner if (name == "* Linker *") 22474e08ca0SZachary Turner --m_cached_compile_unit_count; 22574e08ca0SZachary Turner } 22674e08ca0SZachary Turner return m_cached_compile_unit_count; 22774e08ca0SZachary Turner } 22874e08ca0SZachary Turner 22910a02577SAaron Smith void SymbolFilePDB::GetCompileUnitIndex( 230c8316ed2SAaron Smith const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { 23110a02577SAaron Smith auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 23210a02577SAaron Smith if (!results_up) 23310a02577SAaron Smith return; 234e664b5dcSAaron Smith auto uid = pdb_compiland.getSymIndexId(); 235fbdf0b93SRaphael Isemann for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 23610a02577SAaron Smith auto compiland_up = results_up->getChildAtIndex(cu_idx); 23710a02577SAaron Smith if (!compiland_up) 23810a02577SAaron Smith continue; 23910a02577SAaron Smith if (compiland_up->getSymIndexId() == uid) { 24010a02577SAaron Smith index = cu_idx; 24110a02577SAaron Smith return; 24210a02577SAaron Smith } 24310a02577SAaron Smith } 24410a02577SAaron Smith index = UINT32_MAX; 24510a02577SAaron Smith return; 24610a02577SAaron Smith } 24710a02577SAaron Smith 24810a02577SAaron Smith std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 24910a02577SAaron Smith SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 25010a02577SAaron Smith return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 25110a02577SAaron Smith } 25210a02577SAaron Smith 253b9c1b51eSKate Stone lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 25410a02577SAaron Smith if (index >= GetNumCompileUnits()) 25510a02577SAaron Smith return CompUnitSP(); 25674e08ca0SZachary Turner 25710a02577SAaron Smith // Assuming we always retrieve same compilands listed in same order through 25810a02577SAaron Smith // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 25910a02577SAaron Smith // compile unit makes no sense. 26010a02577SAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 26110a02577SAaron Smith if (!results) 26210a02577SAaron Smith return CompUnitSP(); 26310a02577SAaron Smith auto compiland_up = results->getChildAtIndex(index); 26410a02577SAaron Smith if (!compiland_up) 26510a02577SAaron Smith return CompUnitSP(); 26610a02577SAaron Smith return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 26774e08ca0SZachary Turner } 26874e08ca0SZachary Turner 269863f8c18SZachary Turner lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) { 270863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 27110a02577SAaron Smith if (!compiland_up) 27274e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 27310a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 27474e08ca0SZachary Turner if (!details) 27574e08ca0SZachary Turner return lldb::eLanguageTypeUnknown; 27674e08ca0SZachary Turner return TranslateLanguage(details->getLanguage()); 27774e08ca0SZachary Turner } 27874e08ca0SZachary Turner 279863f8c18SZachary Turner lldb_private::Function * 280863f8c18SZachary Turner SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func, 281863f8c18SZachary Turner CompileUnit &comp_unit) { 282863f8c18SZachary Turner if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId())) 283a5235af9SAleksandr Urakov return result.get(); 284a5235af9SAleksandr Urakov 285e664b5dcSAaron Smith auto file_vm_addr = pdb_func.getVirtualAddress(); 286308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 2877ac1c780SAaron Smith return nullptr; 2887ac1c780SAaron Smith 289e664b5dcSAaron Smith auto func_length = pdb_func.getLength(); 290c8316ed2SAaron Smith AddressRange func_range = 291863f8c18SZachary Turner AddressRange(file_vm_addr, func_length, 292863f8c18SZachary Turner GetObjectFile()->GetModule()->GetSectionList()); 2937ac1c780SAaron Smith if (!func_range.GetBaseAddress().IsValid()) 2947ac1c780SAaron Smith return nullptr; 2957ac1c780SAaron Smith 296e664b5dcSAaron Smith lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 2977ac1c780SAaron Smith if (!func_type) 2987ac1c780SAaron Smith return nullptr; 2997ac1c780SAaron Smith 300e664b5dcSAaron Smith user_id_t func_type_uid = pdb_func.getSignatureId(); 301f76fe682SAaron Smith 3027ac1c780SAaron Smith Mangled mangled = GetMangledForPDBFunc(pdb_func); 3037ac1c780SAaron Smith 304c8316ed2SAaron Smith FunctionSP func_sp = 305863f8c18SZachary Turner std::make_shared<Function>(&comp_unit, pdb_func.getSymIndexId(), 306c8316ed2SAaron Smith func_type_uid, mangled, func_type, func_range); 3077ac1c780SAaron Smith 308863f8c18SZachary Turner comp_unit.AddFunction(func_sp); 309c68925abSZachary Turner 310c68925abSZachary Turner TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 311c68925abSZachary Turner if (!type_system) 312c68925abSZachary Turner return nullptr; 313c68925abSZachary Turner ClangASTContext *clang_type_system = 314c68925abSZachary Turner llvm::dyn_cast_or_null<ClangASTContext>(type_system); 315c68925abSZachary Turner if (!clang_type_system) 316c68925abSZachary Turner return nullptr; 317c68925abSZachary Turner clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func); 318c68925abSZachary Turner 3197ac1c780SAaron Smith return func_sp.get(); 3207ac1c780SAaron Smith } 3217ac1c780SAaron Smith 322863f8c18SZachary Turner size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) { 3237ac1c780SAaron Smith size_t func_added = 0; 324863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 3257ac1c780SAaron Smith if (!compiland_up) 3267ac1c780SAaron Smith return 0; 3277ac1c780SAaron Smith auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 3287ac1c780SAaron Smith if (!results_up) 3297ac1c780SAaron Smith return 0; 3307ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 331863f8c18SZachary Turner auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId()); 3327ac1c780SAaron Smith if (!func_sp) { 333863f8c18SZachary Turner if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit)) 3347ac1c780SAaron Smith ++func_added; 3357ac1c780SAaron Smith } 3367ac1c780SAaron Smith } 3377ac1c780SAaron Smith return func_added; 33874e08ca0SZachary Turner } 33974e08ca0SZachary Turner 340863f8c18SZachary Turner bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) { 341863f8c18SZachary Turner if (comp_unit.GetLineTable()) 34210a02577SAaron Smith return true; 343863f8c18SZachary Turner return ParseCompileUnitLineTable(comp_unit, 0); 34474e08ca0SZachary Turner } 34574e08ca0SZachary Turner 346863f8c18SZachary Turner bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) { 34774e08ca0SZachary Turner // PDB doesn't contain information about macros 34874e08ca0SZachary Turner return false; 34974e08ca0SZachary Turner } 35074e08ca0SZachary Turner 351863f8c18SZachary Turner bool SymbolFilePDB::ParseSupportFiles( 352863f8c18SZachary Turner CompileUnit &comp_unit, lldb_private::FileSpecList &support_files) { 35374e08ca0SZachary Turner 354b9c1b51eSKate Stone // In theory this is unnecessary work for us, because all of this information 3559d0eb996SAdrian McCarthy // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 3569d0eb996SAdrian McCarthy // second time seems like a waste. Unfortunately, there's no good way around 3579d0eb996SAdrian McCarthy // this short of a moderate refactor since SymbolVendor depends on being able 3589d0eb996SAdrian McCarthy // to cache this list. 359863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 36010a02577SAaron Smith if (!compiland_up) 36174e08ca0SZachary Turner return false; 36210a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 36374e08ca0SZachary Turner if (!files || files->getChildCount() == 0) 36474e08ca0SZachary Turner return false; 36574e08ca0SZachary Turner 366b9c1b51eSKate Stone while (auto file = files->getNext()) { 3678f3be7a3SJonas Devlieghere FileSpec spec(file->getFileName(), FileSpec::Style::windows); 36810a02577SAaron Smith support_files.AppendIfUnique(spec); 36974e08ca0SZachary Turner } 3709ea80d25SPavel Labath 3719ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based), 3729ea80d25SPavel Labath // the zeroth file is the compile unit itself 373863f8c18SZachary Turner support_files.Insert(0, comp_unit); 3749ea80d25SPavel Labath 37574e08ca0SZachary Turner return true; 37674e08ca0SZachary Turner } 37774e08ca0SZachary Turner 378b9c1b51eSKate Stone bool SymbolFilePDB::ParseImportedModules( 379b9c1b51eSKate Stone const lldb_private::SymbolContext &sc, 3800f30a3b6SAdrian Prantl std::vector<SourceModule> &imported_modules) { 38174e08ca0SZachary Turner // PDB does not yet support module debug info 38274e08ca0SZachary Turner return false; 38374e08ca0SZachary Turner } 38474e08ca0SZachary Turner 385c8316ed2SAaron Smith static size_t ParseFunctionBlocksForPDBSymbol( 386ffc1b8fdSZachary Turner uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol, 387ffc1b8fdSZachary Turner lldb_private::Block *parent_block, bool is_top_parent) { 3887ac1c780SAaron Smith assert(pdb_symbol && parent_block); 3897ac1c780SAaron Smith 3907ac1c780SAaron Smith size_t num_added = 0; 3917ac1c780SAaron Smith switch (pdb_symbol->getSymTag()) { 3927ac1c780SAaron Smith case PDB_SymType::Block: 3937ac1c780SAaron Smith case PDB_SymType::Function: { 3947ac1c780SAaron Smith Block *block = nullptr; 3957ac1c780SAaron Smith auto &raw_sym = pdb_symbol->getRawSymbol(); 3967ac1c780SAaron Smith if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { 3977ac1c780SAaron Smith if (pdb_func->hasNoInlineAttribute()) 3987ac1c780SAaron Smith break; 3997ac1c780SAaron Smith if (is_top_parent) 4007ac1c780SAaron Smith block = parent_block; 4017ac1c780SAaron Smith else 4027ac1c780SAaron Smith break; 4037ac1c780SAaron Smith } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { 4047ac1c780SAaron Smith auto uid = pdb_symbol->getSymIndexId(); 4057ac1c780SAaron Smith if (parent_block->FindBlockByID(uid)) 4067ac1c780SAaron Smith break; 4077ac1c780SAaron Smith if (raw_sym.getVirtualAddress() < func_file_vm_addr) 4087ac1c780SAaron Smith break; 4097ac1c780SAaron Smith 4107ac1c780SAaron Smith auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); 4117ac1c780SAaron Smith parent_block->AddChild(block_sp); 4127ac1c780SAaron Smith block = block_sp.get(); 4137ac1c780SAaron Smith } else 4147ac1c780SAaron Smith llvm_unreachable("Unexpected PDB symbol!"); 4157ac1c780SAaron Smith 416c8316ed2SAaron Smith block->AddRange(Block::Range( 417c8316ed2SAaron Smith raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); 4187ac1c780SAaron Smith block->FinalizeRanges(); 4197ac1c780SAaron Smith ++num_added; 4207ac1c780SAaron Smith 4217ac1c780SAaron Smith auto results_up = pdb_symbol->findAllChildren(); 4227ac1c780SAaron Smith if (!results_up) 4237ac1c780SAaron Smith break; 4247ac1c780SAaron Smith while (auto symbol_up = results_up->getNext()) { 425c8316ed2SAaron Smith num_added += ParseFunctionBlocksForPDBSymbol( 426ffc1b8fdSZachary Turner func_file_vm_addr, symbol_up.get(), block, false); 4277ac1c780SAaron Smith } 4287ac1c780SAaron Smith } break; 429c8316ed2SAaron Smith default: 430c8316ed2SAaron Smith break; 4317ac1c780SAaron Smith } 4327ac1c780SAaron Smith return num_added; 4337ac1c780SAaron Smith } 4347ac1c780SAaron Smith 435ffc1b8fdSZachary Turner size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) { 4367ac1c780SAaron Smith size_t num_added = 0; 437ffc1b8fdSZachary Turner auto uid = func.GetID(); 4387ac1c780SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 4397ac1c780SAaron Smith if (!pdb_func_up) 4407ac1c780SAaron Smith return 0; 441ffc1b8fdSZachary Turner Block &parent_block = func.GetBlock(false); 442ffc1b8fdSZachary Turner num_added = ParseFunctionBlocksForPDBSymbol( 443ffc1b8fdSZachary Turner pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true); 4447ac1c780SAaron Smith return num_added; 445b9c1b51eSKate Stone } 446b9c1b51eSKate Stone 447863f8c18SZachary Turner size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) { 44866b84079SAaron Smith 44966b84079SAaron Smith size_t num_added = 0; 450ac0d41c7SZachary Turner auto compiland = GetPDBCompilandByUID(comp_unit.GetID()); 45166b84079SAaron Smith if (!compiland) 45266b84079SAaron Smith return 0; 45366b84079SAaron Smith 45466b84079SAaron Smith auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 45566b84079SAaron Smith std::unique_ptr<IPDBEnumSymbols> results; 45666b84079SAaron Smith PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 45766b84079SAaron Smith PDB_SymType::UDT}; 45866b84079SAaron Smith for (auto tag : tags_to_search) { 45966b84079SAaron Smith results = raw_sym.findAllChildren(tag); 46066b84079SAaron Smith if (!results || results->getChildCount() == 0) 46166b84079SAaron Smith continue; 46266b84079SAaron Smith while (auto symbol = results->getNext()) { 46366b84079SAaron Smith switch (symbol->getSymTag()) { 464ec40f818SAaron Smith case PDB_SymType::Enum: 465ec40f818SAaron Smith case PDB_SymType::UDT: 466ec40f818SAaron Smith case PDB_SymType::Typedef: 467ec40f818SAaron Smith break; 468ec40f818SAaron Smith default: 469ec40f818SAaron Smith continue; 470ec40f818SAaron Smith } 471ec40f818SAaron Smith 472ec40f818SAaron Smith // This should cause the type to get cached and stored in the `m_types` 473ec40f818SAaron Smith // lookup. 4747d2a74fcSAleksandr Urakov if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { 4757d2a74fcSAleksandr Urakov // Resolve the type completely to avoid a completion 4767d2a74fcSAleksandr Urakov // (and so a list change, which causes an iterators invalidation) 4777d2a74fcSAleksandr Urakov // during a TypeList dumping 4787d2a74fcSAleksandr Urakov type->GetFullCompilerType(); 479ec40f818SAaron Smith ++num_added; 480ec40f818SAaron Smith } 48166b84079SAaron Smith } 4827d2a74fcSAleksandr Urakov } 48366b84079SAaron Smith }; 48466b84079SAaron Smith 48566b84079SAaron Smith ParseTypesByTagFn(*compiland); 48666b84079SAaron Smith 48766b84079SAaron Smith // Also parse global types particularly coming from this compiland. 48805097246SAdrian Prantl // Unfortunately, PDB has no compiland information for each global type. We 48905097246SAdrian Prantl // have to parse them all. But ensure we only do this once. 49066b84079SAaron Smith static bool parse_all_global_types = false; 49166b84079SAaron Smith if (!parse_all_global_types) { 49266b84079SAaron Smith ParseTypesByTagFn(*m_global_scope_up); 49366b84079SAaron Smith parse_all_global_types = true; 49466b84079SAaron Smith } 495ec40f818SAaron Smith return num_added; 49674e08ca0SZachary Turner } 49774e08ca0SZachary Turner 49874e08ca0SZachary Turner size_t 499b9c1b51eSKate Stone SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 500cab0d23fSAaron Smith if (!sc.comp_unit) 501cab0d23fSAaron Smith return 0; 502cab0d23fSAaron Smith 503cab0d23fSAaron Smith size_t num_added = 0; 504cab0d23fSAaron Smith if (sc.function) { 505cab0d23fSAaron Smith auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 506cab0d23fSAaron Smith sc.function->GetID()); 507cab0d23fSAaron Smith if (!pdb_func) 508cab0d23fSAaron Smith return 0; 509cab0d23fSAaron Smith 510cab0d23fSAaron Smith num_added += ParseVariables(sc, *pdb_func); 511cab0d23fSAaron Smith sc.function->GetBlock(false).SetDidParseVariables(true, true); 512cab0d23fSAaron Smith } else if (sc.comp_unit) { 513cab0d23fSAaron Smith auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 514cab0d23fSAaron Smith if (!compiland) 515cab0d23fSAaron Smith return 0; 516cab0d23fSAaron Smith 517cab0d23fSAaron Smith if (sc.comp_unit->GetVariableList(false)) 518cab0d23fSAaron Smith return 0; 519cab0d23fSAaron Smith 520cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 521cab0d23fSAaron Smith if (results && results->getChildCount()) { 522cab0d23fSAaron Smith while (auto result = results->getNext()) { 523356aa4a9SAleksandr Urakov auto cu_id = GetCompilandId(*result); 524cab0d23fSAaron Smith // FIXME: We are not able to determine variable's compile unit. 525cab0d23fSAaron Smith if (cu_id == 0) 526cab0d23fSAaron Smith continue; 527cab0d23fSAaron Smith 528cab0d23fSAaron Smith if (cu_id == sc.comp_unit->GetID()) 529cab0d23fSAaron Smith num_added += ParseVariables(sc, *result); 530cab0d23fSAaron Smith } 531cab0d23fSAaron Smith } 532cab0d23fSAaron Smith 533cab0d23fSAaron Smith // FIXME: A `file static` or `global constant` variable appears both in 534cab0d23fSAaron Smith // compiland's children and global scope's children with unexpectedly 535cab0d23fSAaron Smith // different symbol's Id making it ambiguous. 536cab0d23fSAaron Smith 537cab0d23fSAaron Smith // FIXME: 'local constant', for example, const char var[] = "abc", declared 538cab0d23fSAaron Smith // in a function scope, can't be found in PDB. 539cab0d23fSAaron Smith 540cab0d23fSAaron Smith // Parse variables in this compiland. 541cab0d23fSAaron Smith num_added += ParseVariables(sc, *compiland); 542cab0d23fSAaron Smith } 543cab0d23fSAaron Smith 544cab0d23fSAaron Smith return num_added; 54574e08ca0SZachary Turner } 54674e08ca0SZachary Turner 547b9c1b51eSKate Stone lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 54842dff790SZachary Turner auto find_result = m_types.find(type_uid); 54942dff790SZachary Turner if (find_result != m_types.end()) 55042dff790SZachary Turner return find_result->second.get(); 55142dff790SZachary Turner 552b9c1b51eSKate Stone TypeSystem *type_system = 553b9c1b51eSKate Stone GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 554b9c1b51eSKate Stone ClangASTContext *clang_type_system = 555b9c1b51eSKate Stone llvm::dyn_cast_or_null<ClangASTContext>(type_system); 55642dff790SZachary Turner if (!clang_type_system) 55774e08ca0SZachary Turner return nullptr; 558709426b3SAleksandr Urakov PDBASTParser *pdb = clang_type_system->GetPDBParser(); 55942dff790SZachary Turner if (!pdb) 56042dff790SZachary Turner return nullptr; 56142dff790SZachary Turner 56242dff790SZachary Turner auto pdb_type = m_session_up->getSymbolById(type_uid); 56342dff790SZachary Turner if (pdb_type == nullptr) 56442dff790SZachary Turner return nullptr; 56542dff790SZachary Turner 56642dff790SZachary Turner lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 567d5a925f4SAaron Smith if (result) { 56842dff790SZachary Turner m_types.insert(std::make_pair(type_uid, result)); 569ec40f818SAaron Smith auto type_list = GetTypeList(); 570f76fe682SAaron Smith if (type_list) 571ec40f818SAaron Smith type_list->Insert(result); 572ec40f818SAaron Smith } 57342dff790SZachary Turner return result.get(); 57474e08ca0SZachary Turner } 57574e08ca0SZachary Turner 576eca07c59SAdrian Prantl llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID( 577eca07c59SAdrian Prantl lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 578eca07c59SAdrian Prantl return llvm::None; 579eca07c59SAdrian Prantl } 580eca07c59SAdrian Prantl 581b9c1b51eSKate Stone bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 5827d2a74fcSAleksandr Urakov std::lock_guard<std::recursive_mutex> guard( 5837d2a74fcSAleksandr Urakov GetObjectFile()->GetModule()->GetMutex()); 5847d2a74fcSAleksandr Urakov 5857d2a74fcSAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 5867d2a74fcSAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 5877d2a74fcSAleksandr Urakov if (!clang_ast_ctx) 58874e08ca0SZachary Turner return false; 5897d2a74fcSAleksandr Urakov 590709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 5917d2a74fcSAleksandr Urakov if (!pdb) 5927d2a74fcSAleksandr Urakov return false; 5937d2a74fcSAleksandr Urakov 5947d2a74fcSAleksandr Urakov return pdb->CompleteTypeFromPDB(compiler_type); 59574e08ca0SZachary Turner } 59674e08ca0SZachary Turner 597b9c1b51eSKate Stone lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 598709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 599709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 600709426b3SAleksandr Urakov if (!clang_ast_ctx) 601709426b3SAleksandr Urakov return CompilerDecl(); 602709426b3SAleksandr Urakov 603709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 604709426b3SAleksandr Urakov if (!pdb) 605709426b3SAleksandr Urakov return CompilerDecl(); 606709426b3SAleksandr Urakov 607709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 608709426b3SAleksandr Urakov if (!symbol) 609709426b3SAleksandr Urakov return CompilerDecl(); 610709426b3SAleksandr Urakov 611709426b3SAleksandr Urakov auto decl = pdb->GetDeclForSymbol(*symbol); 612709426b3SAleksandr Urakov if (!decl) 613709426b3SAleksandr Urakov return CompilerDecl(); 614709426b3SAleksandr Urakov 615709426b3SAleksandr Urakov return CompilerDecl(clang_ast_ctx, decl); 61674e08ca0SZachary Turner } 61774e08ca0SZachary Turner 61874e08ca0SZachary Turner lldb_private::CompilerDeclContext 619b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 620709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 621709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 622709426b3SAleksandr Urakov if (!clang_ast_ctx) 623709426b3SAleksandr Urakov return CompilerDeclContext(); 624709426b3SAleksandr Urakov 625709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 626709426b3SAleksandr Urakov if (!pdb) 627709426b3SAleksandr Urakov return CompilerDeclContext(); 628709426b3SAleksandr Urakov 629709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 630709426b3SAleksandr Urakov if (!symbol) 631709426b3SAleksandr Urakov return CompilerDeclContext(); 632709426b3SAleksandr Urakov 633709426b3SAleksandr Urakov auto decl_context = pdb->GetDeclContextForSymbol(*symbol); 634709426b3SAleksandr Urakov if (!decl_context) 635709426b3SAleksandr Urakov return GetDeclContextContainingUID(uid); 636709426b3SAleksandr Urakov 637709426b3SAleksandr Urakov return CompilerDeclContext(clang_ast_ctx, decl_context); 63874e08ca0SZachary Turner } 63974e08ca0SZachary Turner 64074e08ca0SZachary Turner lldb_private::CompilerDeclContext 641b9c1b51eSKate Stone SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 642709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 643709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 644709426b3SAleksandr Urakov if (!clang_ast_ctx) 645709426b3SAleksandr Urakov return CompilerDeclContext(); 646709426b3SAleksandr Urakov 647709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 648709426b3SAleksandr Urakov if (!pdb) 649709426b3SAleksandr Urakov return CompilerDeclContext(); 650709426b3SAleksandr Urakov 651709426b3SAleksandr Urakov auto symbol = m_session_up->getSymbolById(uid); 652709426b3SAleksandr Urakov if (!symbol) 653709426b3SAleksandr Urakov return CompilerDeclContext(); 654709426b3SAleksandr Urakov 655709426b3SAleksandr Urakov auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol); 656709426b3SAleksandr Urakov assert(decl_context); 657709426b3SAleksandr Urakov 658709426b3SAleksandr Urakov return CompilerDeclContext(clang_ast_ctx, decl_context); 65974e08ca0SZachary Turner } 66074e08ca0SZachary Turner 661b9c1b51eSKate Stone void SymbolFilePDB::ParseDeclsForContext( 662709426b3SAleksandr Urakov lldb_private::CompilerDeclContext decl_ctx) { 663709426b3SAleksandr Urakov ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( 664709426b3SAleksandr Urakov GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); 665709426b3SAleksandr Urakov if (!clang_ast_ctx) 666709426b3SAleksandr Urakov return; 667709426b3SAleksandr Urakov 668709426b3SAleksandr Urakov PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 669709426b3SAleksandr Urakov if (!pdb) 670709426b3SAleksandr Urakov return; 671709426b3SAleksandr Urakov 672709426b3SAleksandr Urakov pdb->ParseDeclsForDeclContext( 673709426b3SAleksandr Urakov static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext())); 674709426b3SAleksandr Urakov } 67574e08ca0SZachary Turner 67674e08ca0SZachary Turner uint32_t 677b9c1b51eSKate Stone SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 678991e4453SZachary Turner SymbolContextItem resolve_scope, 679b9c1b51eSKate Stone lldb_private::SymbolContext &sc) { 6807ac1c780SAaron Smith uint32_t resolved_flags = 0; 6814d4d63eeSPavel Labath if (resolve_scope & eSymbolContextCompUnit || 6824d4d63eeSPavel Labath resolve_scope & eSymbolContextVariable || 6834d4d63eeSPavel Labath resolve_scope & eSymbolContextFunction || 6844d4d63eeSPavel Labath resolve_scope & eSymbolContextBlock || 6857ac1c780SAaron Smith resolve_scope & eSymbolContextLineEntry) { 6867ac1c780SAaron Smith auto cu_sp = GetCompileUnitContainsAddress(so_addr); 6877ac1c780SAaron Smith if (!cu_sp) { 6887ac1c780SAaron Smith if (resolved_flags | eSymbolContextVariable) { 6897ac1c780SAaron Smith // TODO: Resolve variables 6907ac1c780SAaron Smith } 6917ac1c780SAaron Smith return 0; 6927ac1c780SAaron Smith } 6937ac1c780SAaron Smith sc.comp_unit = cu_sp.get(); 6947ac1c780SAaron Smith resolved_flags |= eSymbolContextCompUnit; 6957ac1c780SAaron Smith lldbassert(sc.module_sp == cu_sp->GetModule()); 6969ea80d25SPavel Labath } 6977ac1c780SAaron Smith 698398f81b3SAleksandr Urakov if (resolve_scope & eSymbolContextFunction || 699398f81b3SAleksandr Urakov resolve_scope & eSymbolContextBlock) { 7009ea80d25SPavel Labath addr_t file_vm_addr = so_addr.GetFileAddress(); 7019ea80d25SPavel Labath auto symbol_up = 7029ea80d25SPavel Labath m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); 7039ea80d25SPavel Labath if (symbol_up) { 7047ac1c780SAaron Smith auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 7057ac1c780SAaron Smith assert(pdb_func); 7067ac1c780SAaron Smith auto func_uid = pdb_func->getSymIndexId(); 7077ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 7087ac1c780SAaron Smith if (sc.function == nullptr) 709863f8c18SZachary Turner sc.function = 710863f8c18SZachary Turner ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit); 7117ac1c780SAaron Smith if (sc.function) { 7127ac1c780SAaron Smith resolved_flags |= eSymbolContextFunction; 7137ac1c780SAaron Smith if (resolve_scope & eSymbolContextBlock) { 714398f81b3SAleksandr Urakov auto block_symbol = m_session_up->findSymbolByAddress( 715398f81b3SAleksandr Urakov file_vm_addr, PDB_SymType::Block); 716398f81b3SAleksandr Urakov auto block_id = block_symbol ? block_symbol->getSymIndexId() 717398f81b3SAleksandr Urakov : sc.function->GetID(); 718398f81b3SAleksandr Urakov sc.block = sc.function->GetBlock(true).FindBlockByID(block_id); 7197ac1c780SAaron Smith if (sc.block) 7207ac1c780SAaron Smith resolved_flags |= eSymbolContextBlock; 7217ac1c780SAaron Smith } 7227ac1c780SAaron Smith } 7237ac1c780SAaron Smith } 7247ac1c780SAaron Smith } 7257ac1c780SAaron Smith 7267ac1c780SAaron Smith if (resolve_scope & eSymbolContextLineEntry) { 7277ac1c780SAaron Smith if (auto *line_table = sc.comp_unit->GetLineTable()) { 7287ac1c780SAaron Smith Address addr(so_addr); 7297ac1c780SAaron Smith if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 7307ac1c780SAaron Smith resolved_flags |= eSymbolContextLineEntry; 7317ac1c780SAaron Smith } 7327ac1c780SAaron Smith } 7339ea80d25SPavel Labath 7347ac1c780SAaron Smith return resolved_flags; 73574e08ca0SZachary Turner } 73674e08ca0SZachary Turner 737b9c1b51eSKate Stone uint32_t SymbolFilePDB::ResolveSymbolContext( 738b9c1b51eSKate Stone const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 739991e4453SZachary Turner SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) { 74010a02577SAaron Smith const size_t old_size = sc_list.GetSize(); 741b9c1b51eSKate Stone if (resolve_scope & lldb::eSymbolContextCompUnit) { 742b9c1b51eSKate Stone // Locate all compilation units with line numbers referencing the specified 7439d0eb996SAdrian McCarthy // file. For example, if `file_spec` is <vector>, then this should return 7449d0eb996SAdrian McCarthy // all source files and header files that reference <vector>, either 7459d0eb996SAdrian McCarthy // directly or indirectly. 746b9c1b51eSKate Stone auto compilands = m_session_up->findCompilandsForSourceFile( 747b9c1b51eSKate Stone file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 74874e08ca0SZachary Turner 74910a02577SAaron Smith if (!compilands) 75010a02577SAaron Smith return 0; 75110a02577SAaron Smith 7529d0eb996SAdrian McCarthy // For each one, either find its previously parsed data or parse it afresh 7539d0eb996SAdrian McCarthy // and add it to the symbol context list. 754b9c1b51eSKate Stone while (auto compiland = compilands->getNext()) { 75505097246SAdrian Prantl // If we're not checking inlines, then don't add line information for 75605097246SAdrian Prantl // this file unless the FileSpec matches. For inline functions, we don't 75705097246SAdrian Prantl // have to match the FileSpec since they could be defined in headers 75805097246SAdrian Prantl // other than file specified in FileSpec. 759b9c1b51eSKate Stone if (!check_inlines) { 760487b0c6bSAaron Smith std::string source_file = compiland->getSourceFileFullPath(); 76110a02577SAaron Smith if (source_file.empty()) 76210a02577SAaron Smith continue; 7638f3be7a3SJonas Devlieghere FileSpec this_spec(source_file, FileSpec::Style::windows); 76410a02577SAaron Smith bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 76510a02577SAaron Smith if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 76674e08ca0SZachary Turner continue; 76774e08ca0SZachary Turner } 76874e08ca0SZachary Turner 76974e08ca0SZachary Turner SymbolContext sc; 77010a02577SAaron Smith auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 771d5a925f4SAaron Smith if (!cu) 77210a02577SAaron Smith continue; 77374e08ca0SZachary Turner sc.comp_unit = cu.get(); 77474e08ca0SZachary Turner sc.module_sp = cu->GetModule(); 77574e08ca0SZachary Turner 776b9c1b51eSKate Stone // If we were asked to resolve line entries, add all entries to the line 7779d0eb996SAdrian McCarthy // table that match the requested line (or all lines if `line` == 0). 7787ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 7797ac1c780SAaron Smith eSymbolContextLineEntry)) { 780863f8c18SZachary Turner bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line); 7817ac1c780SAaron Smith 7827ac1c780SAaron Smith if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 7837ac1c780SAaron Smith // The query asks for line entries, but we can't get them for the 78405097246SAdrian Prantl // compile unit. This is not normal for `line` = 0. So just assert 78505097246SAdrian Prantl // it. 786f76fe682SAaron Smith assert(line && "Couldn't get all line entries!\n"); 7877ac1c780SAaron Smith 7887ac1c780SAaron Smith // Current compiland does not have the requested line. Search next. 7897ac1c780SAaron Smith continue; 7907ac1c780SAaron Smith } 7917ac1c780SAaron Smith 7927ac1c780SAaron Smith if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 7937ac1c780SAaron Smith if (!has_line_table) 7947ac1c780SAaron Smith continue; 7957ac1c780SAaron Smith 7967ac1c780SAaron Smith auto *line_table = sc.comp_unit->GetLineTable(); 7977ac1c780SAaron Smith lldbassert(line_table); 7987ac1c780SAaron Smith 7997ac1c780SAaron Smith uint32_t num_line_entries = line_table->GetSize(); 8007ac1c780SAaron Smith // Skip the terminal line entry. 8017ac1c780SAaron Smith --num_line_entries; 8027ac1c780SAaron Smith 80305097246SAdrian Prantl // If `line `!= 0, see if we can resolve function for each line entry 80405097246SAdrian Prantl // in the line table. 8057ac1c780SAaron Smith for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 8067ac1c780SAaron Smith ++line_idx) { 8077ac1c780SAaron Smith if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 8087ac1c780SAaron Smith continue; 8097ac1c780SAaron Smith 8107ac1c780SAaron Smith auto file_vm_addr = 8117ac1c780SAaron Smith sc.line_entry.range.GetBaseAddress().GetFileAddress(); 812308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 8137ac1c780SAaron Smith continue; 8147ac1c780SAaron Smith 815c8316ed2SAaron Smith auto symbol_up = m_session_up->findSymbolByAddress( 816c8316ed2SAaron Smith file_vm_addr, PDB_SymType::Function); 8177ac1c780SAaron Smith if (symbol_up) { 8187ac1c780SAaron Smith auto func_uid = symbol_up->getSymIndexId(); 8197ac1c780SAaron Smith sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 8207ac1c780SAaron Smith if (sc.function == nullptr) { 8217ac1c780SAaron Smith auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 8227ac1c780SAaron Smith assert(pdb_func); 823863f8c18SZachary Turner sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, 824863f8c18SZachary Turner *sc.comp_unit); 8257ac1c780SAaron Smith } 8267ac1c780SAaron Smith if (sc.function && (resolve_scope & eSymbolContextBlock)) { 8277ac1c780SAaron Smith Block &block = sc.function->GetBlock(true); 8287ac1c780SAaron Smith sc.block = block.FindBlockByID(sc.function->GetID()); 8297ac1c780SAaron Smith } 8307ac1c780SAaron Smith } 8317ac1c780SAaron Smith sc_list.Append(sc); 8327ac1c780SAaron Smith } 8337ac1c780SAaron Smith } else if (has_line_table) { 8347ac1c780SAaron Smith // We can parse line table for the compile unit. But no query to 8357ac1c780SAaron Smith // resolve function or block. We append `sc` to the list anyway. 8367ac1c780SAaron Smith sc_list.Append(sc); 8377ac1c780SAaron Smith } 8387ac1c780SAaron Smith } else { 8397ac1c780SAaron Smith // No query for line entry, function or block. But we have a valid 8407ac1c780SAaron Smith // compile unit, append `sc` to the list. 8417ac1c780SAaron Smith sc_list.Append(sc); 8427ac1c780SAaron Smith } 84374e08ca0SZachary Turner } 84474e08ca0SZachary Turner } 84510a02577SAaron Smith return sc_list.GetSize() - old_size; 84674e08ca0SZachary Turner } 84774e08ca0SZachary Turner 848cab0d23fSAaron Smith std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { 849356aa4a9SAleksandr Urakov // Cache public names at first 850356aa4a9SAleksandr Urakov if (m_public_names.empty()) 851356aa4a9SAleksandr Urakov if (auto result_up = 852356aa4a9SAleksandr Urakov m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol)) 853356aa4a9SAleksandr Urakov while (auto symbol_up = result_up->getNext()) 854356aa4a9SAleksandr Urakov if (auto addr = symbol_up->getRawSymbol().getVirtualAddress()) 855356aa4a9SAleksandr Urakov m_public_names[addr] = symbol_up->getRawSymbol().getName(); 856cab0d23fSAaron Smith 857356aa4a9SAleksandr Urakov // Look up the name in the cache 858356aa4a9SAleksandr Urakov return m_public_names.lookup(pdb_data.getVirtualAddress()); 859cab0d23fSAaron Smith } 860cab0d23fSAaron Smith 861cab0d23fSAaron Smith VariableSP SymbolFilePDB::ParseVariableForPDBData( 862cab0d23fSAaron Smith const lldb_private::SymbolContext &sc, 863cab0d23fSAaron Smith const llvm::pdb::PDBSymbolData &pdb_data) { 864cab0d23fSAaron Smith VariableSP var_sp; 865cab0d23fSAaron Smith uint32_t var_uid = pdb_data.getSymIndexId(); 866cab0d23fSAaron Smith auto result = m_variables.find(var_uid); 867cab0d23fSAaron Smith if (result != m_variables.end()) 868cab0d23fSAaron Smith return result->second; 869cab0d23fSAaron Smith 870cab0d23fSAaron Smith ValueType scope = eValueTypeInvalid; 871cab0d23fSAaron Smith bool is_static_member = false; 872cab0d23fSAaron Smith bool is_external = false; 873cab0d23fSAaron Smith bool is_artificial = false; 874cab0d23fSAaron Smith 875cab0d23fSAaron Smith switch (pdb_data.getDataKind()) { 876cab0d23fSAaron Smith case PDB_DataKind::Global: 877cab0d23fSAaron Smith scope = eValueTypeVariableGlobal; 878cab0d23fSAaron Smith is_external = true; 879cab0d23fSAaron Smith break; 880cab0d23fSAaron Smith case PDB_DataKind::Local: 881cab0d23fSAaron Smith scope = eValueTypeVariableLocal; 882cab0d23fSAaron Smith break; 883cab0d23fSAaron Smith case PDB_DataKind::FileStatic: 884cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 885cab0d23fSAaron Smith break; 886cab0d23fSAaron Smith case PDB_DataKind::StaticMember: 887cab0d23fSAaron Smith is_static_member = true; 888cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 889cab0d23fSAaron Smith break; 890cab0d23fSAaron Smith case PDB_DataKind::Member: 891cab0d23fSAaron Smith scope = eValueTypeVariableStatic; 892cab0d23fSAaron Smith break; 893cab0d23fSAaron Smith case PDB_DataKind::Param: 894cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 895cab0d23fSAaron Smith break; 896cab0d23fSAaron Smith case PDB_DataKind::Constant: 897cab0d23fSAaron Smith scope = eValueTypeConstResult; 898cab0d23fSAaron Smith break; 899cab0d23fSAaron Smith default: 900cab0d23fSAaron Smith break; 901cab0d23fSAaron Smith } 902cab0d23fSAaron Smith 903cab0d23fSAaron Smith switch (pdb_data.getLocationType()) { 904cab0d23fSAaron Smith case PDB_LocType::TLS: 905cab0d23fSAaron Smith scope = eValueTypeVariableThreadLocal; 906cab0d23fSAaron Smith break; 907cab0d23fSAaron Smith case PDB_LocType::RegRel: { 908cab0d23fSAaron Smith // It is a `this` pointer. 909cab0d23fSAaron Smith if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { 910cab0d23fSAaron Smith scope = eValueTypeVariableArgument; 911cab0d23fSAaron Smith is_artificial = true; 912cab0d23fSAaron Smith } 913cab0d23fSAaron Smith } break; 914cab0d23fSAaron Smith default: 915cab0d23fSAaron Smith break; 916cab0d23fSAaron Smith } 917cab0d23fSAaron Smith 918cab0d23fSAaron Smith Declaration decl; 919cab0d23fSAaron Smith if (!is_artificial && !pdb_data.isCompilerGenerated()) { 920cab0d23fSAaron Smith if (auto lines = pdb_data.getLineNumbers()) { 921cab0d23fSAaron Smith if (auto first_line = lines->getNext()) { 922cab0d23fSAaron Smith uint32_t src_file_id = first_line->getSourceFileId(); 923cab0d23fSAaron Smith auto src_file = m_session_up->getSourceFileById(src_file_id); 924cab0d23fSAaron Smith if (src_file) { 9258f3be7a3SJonas Devlieghere FileSpec spec(src_file->getFileName()); 926cab0d23fSAaron Smith decl.SetFile(spec); 927cab0d23fSAaron Smith decl.SetColumn(first_line->getColumnNumber()); 928cab0d23fSAaron Smith decl.SetLine(first_line->getLineNumber()); 929cab0d23fSAaron Smith } 930cab0d23fSAaron Smith } 931cab0d23fSAaron Smith } 932cab0d23fSAaron Smith } 933cab0d23fSAaron Smith 934cab0d23fSAaron Smith Variable::RangeList ranges; 935cab0d23fSAaron Smith SymbolContextScope *context_scope = sc.comp_unit; 936758657e5SAleksandr Urakov if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) { 937cab0d23fSAaron Smith if (sc.function) { 938758657e5SAleksandr Urakov Block &function_block = sc.function->GetBlock(true); 939758657e5SAleksandr Urakov Block *block = 940758657e5SAleksandr Urakov function_block.FindBlockByID(pdb_data.getLexicalParentId()); 941758657e5SAleksandr Urakov if (!block) 942758657e5SAleksandr Urakov block = &function_block; 943758657e5SAleksandr Urakov 944758657e5SAleksandr Urakov context_scope = block; 945758657e5SAleksandr Urakov 946758657e5SAleksandr Urakov for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges; 947758657e5SAleksandr Urakov ++i) { 948758657e5SAleksandr Urakov AddressRange range; 949758657e5SAleksandr Urakov if (!block->GetRangeAtIndex(i, range)) 950758657e5SAleksandr Urakov continue; 951758657e5SAleksandr Urakov 952758657e5SAleksandr Urakov ranges.Append(range.GetBaseAddress().GetFileAddress(), 953758657e5SAleksandr Urakov range.GetByteSize()); 954758657e5SAleksandr Urakov } 955cab0d23fSAaron Smith } 956cab0d23fSAaron Smith } 957cab0d23fSAaron Smith 958cab0d23fSAaron Smith SymbolFileTypeSP type_sp = 959cab0d23fSAaron Smith std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); 960cab0d23fSAaron Smith 961cab0d23fSAaron Smith auto var_name = pdb_data.getName(); 962cab0d23fSAaron Smith auto mangled = GetMangledForPDBData(pdb_data); 963cab0d23fSAaron Smith auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); 964cab0d23fSAaron Smith 965924d5608SJonas Devlieghere bool is_constant; 966924d5608SJonas Devlieghere DWARFExpression location = ConvertPDBLocationToDWARFExpression( 967758657e5SAleksandr Urakov GetObjectFile()->GetModule(), pdb_data, ranges, is_constant); 968cab0d23fSAaron Smith 969cab0d23fSAaron Smith var_sp = std::make_shared<Variable>( 970cab0d23fSAaron Smith var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, 971cab0d23fSAaron Smith ranges, &decl, location, is_external, is_artificial, is_static_member); 972924d5608SJonas Devlieghere var_sp->SetLocationIsConstantValueData(is_constant); 973cab0d23fSAaron Smith 974cab0d23fSAaron Smith m_variables.insert(std::make_pair(var_uid, var_sp)); 975cab0d23fSAaron Smith return var_sp; 976cab0d23fSAaron Smith } 977cab0d23fSAaron Smith 978cab0d23fSAaron Smith size_t 979cab0d23fSAaron Smith SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, 980cab0d23fSAaron Smith const llvm::pdb::PDBSymbol &pdb_symbol, 981cab0d23fSAaron Smith lldb_private::VariableList *variable_list) { 982cab0d23fSAaron Smith size_t num_added = 0; 983cab0d23fSAaron Smith 984cab0d23fSAaron Smith if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { 985cab0d23fSAaron Smith VariableListSP local_variable_list_sp; 986cab0d23fSAaron Smith 987cab0d23fSAaron Smith auto result = m_variables.find(pdb_data->getSymIndexId()); 988cab0d23fSAaron Smith if (result != m_variables.end()) { 989cab0d23fSAaron Smith if (variable_list) 990cab0d23fSAaron Smith variable_list->AddVariableIfUnique(result->second); 991cab0d23fSAaron Smith } else { 992cab0d23fSAaron Smith // Prepare right VariableList for this variable. 993cab0d23fSAaron Smith if (auto lexical_parent = pdb_data->getLexicalParent()) { 994cab0d23fSAaron Smith switch (lexical_parent->getSymTag()) { 995cab0d23fSAaron Smith case PDB_SymType::Exe: 996cab0d23fSAaron Smith assert(sc.comp_unit); 997cab0d23fSAaron Smith LLVM_FALLTHROUGH; 998cab0d23fSAaron Smith case PDB_SymType::Compiland: { 999cab0d23fSAaron Smith if (sc.comp_unit) { 1000cab0d23fSAaron Smith local_variable_list_sp = sc.comp_unit->GetVariableList(false); 1001cab0d23fSAaron Smith if (!local_variable_list_sp) { 1002cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 1003cab0d23fSAaron Smith sc.comp_unit->SetVariableList(local_variable_list_sp); 1004cab0d23fSAaron Smith } 1005cab0d23fSAaron Smith } 1006cab0d23fSAaron Smith } break; 1007cab0d23fSAaron Smith case PDB_SymType::Block: 1008cab0d23fSAaron Smith case PDB_SymType::Function: { 1009cab0d23fSAaron Smith if (sc.function) { 1010cab0d23fSAaron Smith Block *block = sc.function->GetBlock(true).FindBlockByID( 1011cab0d23fSAaron Smith lexical_parent->getSymIndexId()); 1012cab0d23fSAaron Smith if (block) { 1013cab0d23fSAaron Smith local_variable_list_sp = block->GetBlockVariableList(false); 1014cab0d23fSAaron Smith if (!local_variable_list_sp) { 1015cab0d23fSAaron Smith local_variable_list_sp = std::make_shared<VariableList>(); 1016cab0d23fSAaron Smith block->SetVariableList(local_variable_list_sp); 1017cab0d23fSAaron Smith } 1018cab0d23fSAaron Smith } 1019cab0d23fSAaron Smith } 1020cab0d23fSAaron Smith } break; 1021cab0d23fSAaron Smith default: 1022cab0d23fSAaron Smith break; 1023cab0d23fSAaron Smith } 1024cab0d23fSAaron Smith } 1025cab0d23fSAaron Smith 1026cab0d23fSAaron Smith if (local_variable_list_sp) { 1027cab0d23fSAaron Smith if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { 1028cab0d23fSAaron Smith local_variable_list_sp->AddVariableIfUnique(var_sp); 1029cab0d23fSAaron Smith if (variable_list) 1030cab0d23fSAaron Smith variable_list->AddVariableIfUnique(var_sp); 1031cab0d23fSAaron Smith ++num_added; 1032c68925abSZachary Turner PDBASTParser *ast = GetPDBAstParser(); 1033c68925abSZachary Turner if (ast) 1034c68925abSZachary Turner ast->GetDeclForSymbol(*pdb_data); 1035cab0d23fSAaron Smith } 1036cab0d23fSAaron Smith } 1037cab0d23fSAaron Smith } 1038cab0d23fSAaron Smith } 1039cab0d23fSAaron Smith 1040cab0d23fSAaron Smith if (auto results = pdb_symbol.findAllChildren()) { 1041cab0d23fSAaron Smith while (auto result = results->getNext()) 1042cab0d23fSAaron Smith num_added += ParseVariables(sc, *result, variable_list); 1043cab0d23fSAaron Smith } 1044cab0d23fSAaron Smith 1045cab0d23fSAaron Smith return num_added; 1046cab0d23fSAaron Smith } 1047cab0d23fSAaron Smith 1048b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindGlobalVariables( 1049b9c1b51eSKate Stone const lldb_private::ConstString &name, 105034cda14bSPavel Labath const lldb_private::CompilerDeclContext *parent_decl_ctx, 1051b9c1b51eSKate Stone uint32_t max_matches, lldb_private::VariableList &variables) { 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) { 1096cab0d23fSAaron Smith if (!regex.IsValid()) 1097cab0d23fSAaron Smith return 0; 1098cab0d23fSAaron Smith auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1099cab0d23fSAaron Smith if (!results) 1100cab0d23fSAaron Smith return 0; 1101cab0d23fSAaron Smith 1102cab0d23fSAaron Smith uint32_t matches = 0; 1103cab0d23fSAaron Smith size_t old_size = variables.GetSize(); 1104cab0d23fSAaron Smith while (auto pdb_data = results->getNext()) { 1105cab0d23fSAaron Smith if (max_matches > 0 && matches >= max_matches) 1106cab0d23fSAaron Smith break; 1107cab0d23fSAaron Smith 1108cab0d23fSAaron Smith auto var_name = pdb_data->getName(); 1109cab0d23fSAaron Smith if (var_name.empty()) 1110cab0d23fSAaron Smith continue; 1111cab0d23fSAaron Smith if (!regex.Execute(var_name)) 1112cab0d23fSAaron Smith continue; 1113cab0d23fSAaron Smith SymbolContext sc; 1114cab0d23fSAaron Smith sc.module_sp = m_obj_file->GetModule(); 1115cab0d23fSAaron Smith lldbassert(sc.module_sp.get()); 1116cab0d23fSAaron Smith 1117356aa4a9SAleksandr Urakov sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1118cab0d23fSAaron Smith // FIXME: We are not able to determine the compile unit. 1119cab0d23fSAaron Smith if (sc.comp_unit == nullptr) 1120cab0d23fSAaron Smith continue; 1121cab0d23fSAaron Smith 1122cab0d23fSAaron Smith ParseVariables(sc, *pdb_data, &variables); 1123cab0d23fSAaron Smith matches = variables.GetSize() - old_size; 1124cab0d23fSAaron Smith } 1125cab0d23fSAaron Smith 1126cab0d23fSAaron Smith return matches; 1127b9c1b51eSKate Stone } 1128b9c1b51eSKate Stone 1129e664b5dcSAaron Smith bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 11307ac1c780SAaron Smith bool include_inlines, 11317ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 11327ac1c780SAaron Smith lldb_private::SymbolContext sc; 1133a3a8cc80SAaron Smith sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 11347ac1c780SAaron Smith if (!sc.comp_unit) 11357ac1c780SAaron Smith return false; 11367ac1c780SAaron Smith sc.module_sp = sc.comp_unit->GetModule(); 1137863f8c18SZachary Turner sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit); 11387ac1c780SAaron Smith if (!sc.function) 11397ac1c780SAaron Smith return false; 11407ac1c780SAaron Smith 11417ac1c780SAaron Smith sc_list.Append(sc); 11427ac1c780SAaron Smith return true; 11437ac1c780SAaron Smith } 11447ac1c780SAaron Smith 11457ac1c780SAaron Smith bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 11467ac1c780SAaron Smith lldb_private::SymbolContextList &sc_list) { 1147c8316ed2SAaron Smith auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 11487ac1c780SAaron Smith if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 11497ac1c780SAaron Smith return false; 1150e664b5dcSAaron Smith return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 11517ac1c780SAaron Smith } 11527ac1c780SAaron Smith 11537ac1c780SAaron Smith void SymbolFilePDB::CacheFunctionNames() { 11547ac1c780SAaron Smith if (!m_func_full_names.IsEmpty()) 11557ac1c780SAaron Smith return; 11567ac1c780SAaron Smith 11577ac1c780SAaron Smith std::map<uint64_t, uint32_t> addr_ids; 11587ac1c780SAaron Smith 11597ac1c780SAaron Smith if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 11607ac1c780SAaron Smith while (auto pdb_func_up = results_up->getNext()) { 1161f76fe682SAaron Smith if (pdb_func_up->isCompilerGenerated()) 1162f76fe682SAaron Smith continue; 1163f76fe682SAaron Smith 11647ac1c780SAaron Smith auto name = pdb_func_up->getName(); 11657ac1c780SAaron Smith auto demangled_name = pdb_func_up->getUndecoratedName(); 11667ac1c780SAaron Smith if (name.empty() && demangled_name.empty()) 11677ac1c780SAaron Smith continue; 11687ac1c780SAaron Smith 1169f76fe682SAaron Smith auto uid = pdb_func_up->getSymIndexId(); 11707ac1c780SAaron Smith if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 11717ac1c780SAaron Smith addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 11727ac1c780SAaron Smith 11737ac1c780SAaron Smith if (auto parent = pdb_func_up->getClassParent()) { 11747ac1c780SAaron Smith 11757ac1c780SAaron Smith // PDB have symbols for class/struct methods or static methods in Enum 11767ac1c780SAaron Smith // Class. We won't bother to check if the parent is UDT or Enum here. 11777ac1c780SAaron Smith m_func_method_names.Append(ConstString(name), uid); 11787ac1c780SAaron Smith 117905097246SAdrian Prantl // To search a method name, like NS::Class:MemberFunc, LLDB searches 118005097246SAdrian Prantl // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does 118105097246SAdrian Prantl // not have inforamtion of this, we extract base names and cache them 118205097246SAdrian Prantl // by our own effort. 1183c1e530eeSAleksandr Urakov llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 11847ac1c780SAaron Smith if (!basename.empty()) 11857ac1c780SAaron Smith m_func_base_names.Append(ConstString(basename), uid); 11867ac1c780SAaron Smith else { 11877ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 11887ac1c780SAaron Smith } 11897ac1c780SAaron Smith 11907ac1c780SAaron Smith if (!demangled_name.empty()) 11917ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 11927ac1c780SAaron Smith 11937ac1c780SAaron Smith } else { 11947ac1c780SAaron Smith // Handle not-method symbols. 11957ac1c780SAaron Smith 1196c1e530eeSAleksandr Urakov // The function name might contain namespace, or its lexical scope. 1197c1e530eeSAleksandr Urakov llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1198c1e530eeSAleksandr Urakov if (!basename.empty()) 1199c1e530eeSAleksandr Urakov m_func_base_names.Append(ConstString(basename), uid); 1200c1e530eeSAleksandr Urakov else 12017ac1c780SAaron Smith m_func_base_names.Append(ConstString(name), uid); 12027ac1c780SAaron Smith 12037ac1c780SAaron Smith if (name == "main") { 12047ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 12057ac1c780SAaron Smith 12067ac1c780SAaron Smith if (!demangled_name.empty() && name != demangled_name) { 12077ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 12087ac1c780SAaron Smith m_func_base_names.Append(ConstString(demangled_name), uid); 12097ac1c780SAaron Smith } 12107ac1c780SAaron Smith } else if (!demangled_name.empty()) { 12117ac1c780SAaron Smith m_func_full_names.Append(ConstString(demangled_name), uid); 12127ac1c780SAaron Smith } else { 12137ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), uid); 12147ac1c780SAaron Smith } 12157ac1c780SAaron Smith } 12167ac1c780SAaron Smith } 12177ac1c780SAaron Smith } 12187ac1c780SAaron Smith 12197ac1c780SAaron Smith if (auto results_up = 12207ac1c780SAaron Smith m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 12217ac1c780SAaron Smith while (auto pub_sym_up = results_up->getNext()) { 12227ac1c780SAaron Smith if (!pub_sym_up->isFunction()) 12237ac1c780SAaron Smith continue; 12247ac1c780SAaron Smith auto name = pub_sym_up->getName(); 12257ac1c780SAaron Smith if (name.empty()) 12267ac1c780SAaron Smith continue; 12277ac1c780SAaron Smith 12287ac1c780SAaron Smith if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 12297ac1c780SAaron Smith auto vm_addr = pub_sym_up->getVirtualAddress(); 12307ac1c780SAaron Smith 12317ac1c780SAaron Smith // PDB public symbol has mangled name for its associated function. 12327ac1c780SAaron Smith if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { 12337ac1c780SAaron Smith // Cache mangled name. 12347ac1c780SAaron Smith m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); 12357ac1c780SAaron Smith } 12367ac1c780SAaron Smith } 12377ac1c780SAaron Smith } 12387ac1c780SAaron Smith } 12397ac1c780SAaron Smith // Sort them before value searching is working properly 12407ac1c780SAaron Smith m_func_full_names.Sort(); 12417ac1c780SAaron Smith m_func_full_names.SizeToFit(); 12427ac1c780SAaron Smith m_func_method_names.Sort(); 12437ac1c780SAaron Smith m_func_method_names.SizeToFit(); 12447ac1c780SAaron Smith m_func_base_names.Sort(); 12457ac1c780SAaron Smith m_func_base_names.SizeToFit(); 12467ac1c780SAaron Smith } 12477ac1c780SAaron Smith 1248b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindFunctions( 1249b9c1b51eSKate Stone const lldb_private::ConstString &name, 1250b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, 1251117b1fa1SZachary Turner FunctionNameType name_type_mask, bool include_inlines, bool append, 1252b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 12537ac1c780SAaron Smith if (!append) 12547ac1c780SAaron Smith sc_list.Clear(); 12557ac1c780SAaron Smith lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 12567ac1c780SAaron Smith 12577ac1c780SAaron Smith if (name_type_mask == eFunctionNameTypeNone) 12587ac1c780SAaron Smith return 0; 12597ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 12607ac1c780SAaron Smith return 0; 12617ac1c780SAaron Smith if (name.IsEmpty()) 12627ac1c780SAaron Smith return 0; 12637ac1c780SAaron Smith 12647ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 12654d4d63eeSPavel Labath if (name_type_mask & eFunctionNameTypeFull || 12664d4d63eeSPavel Labath name_type_mask & eFunctionNameTypeBase || 12677ac1c780SAaron Smith name_type_mask & eFunctionNameTypeMethod) { 12687ac1c780SAaron Smith CacheFunctionNames(); 12697ac1c780SAaron Smith 12707ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1271a5235af9SAleksandr Urakov auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list, 1272a5235af9SAleksandr Urakov &resolved_ids](UniqueCStringMap<uint32_t> &Names) { 12737ac1c780SAaron Smith std::vector<uint32_t> ids; 1274a5235af9SAleksandr Urakov if (!Names.GetValues(name, ids)) 1275a5235af9SAleksandr Urakov return; 1276a5235af9SAleksandr Urakov 1277a5235af9SAleksandr Urakov for (uint32_t id : ids) { 1278a5235af9SAleksandr Urakov if (resolved_ids.find(id) != resolved_ids.end()) 1279a5235af9SAleksandr Urakov continue; 1280a5235af9SAleksandr Urakov 1281a5235af9SAleksandr Urakov if (parent_decl_ctx && 1282a5235af9SAleksandr Urakov GetDeclContextContainingUID(id) != *parent_decl_ctx) 1283a5235af9SAleksandr Urakov continue; 1284a5235af9SAleksandr Urakov 12857ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 12867ac1c780SAaron Smith resolved_ids.insert(id); 12877ac1c780SAaron Smith } 12887ac1c780SAaron Smith }; 12897ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeFull) { 12907ac1c780SAaron Smith ResolveFn(m_func_full_names); 1291a5235af9SAleksandr Urakov ResolveFn(m_func_base_names); 1292a5235af9SAleksandr Urakov ResolveFn(m_func_method_names); 12937ac1c780SAaron Smith } 12947ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeBase) { 12957ac1c780SAaron Smith ResolveFn(m_func_base_names); 12967ac1c780SAaron Smith } 12977ac1c780SAaron Smith if (name_type_mask & eFunctionNameTypeMethod) { 12987ac1c780SAaron Smith ResolveFn(m_func_method_names); 12997ac1c780SAaron Smith } 13007ac1c780SAaron Smith } 13017ac1c780SAaron Smith return sc_list.GetSize() - old_size; 130274e08ca0SZachary Turner } 130374e08ca0SZachary Turner 130474e08ca0SZachary Turner uint32_t 1305b9c1b51eSKate Stone SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 1306b9c1b51eSKate Stone bool include_inlines, bool append, 1307b9c1b51eSKate Stone lldb_private::SymbolContextList &sc_list) { 13087ac1c780SAaron Smith if (!append) 13097ac1c780SAaron Smith sc_list.Clear(); 13107ac1c780SAaron Smith if (!regex.IsValid()) 13117ac1c780SAaron Smith return 0; 13127ac1c780SAaron Smith 13137ac1c780SAaron Smith auto old_size = sc_list.GetSize(); 13147ac1c780SAaron Smith CacheFunctionNames(); 13157ac1c780SAaron Smith 13167ac1c780SAaron Smith std::set<uint32_t> resolved_ids; 1317c8316ed2SAaron Smith auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, 1318c8316ed2SAaron Smith this](UniqueCStringMap<uint32_t> &Names) { 13197ac1c780SAaron Smith std::vector<uint32_t> ids; 13207ac1c780SAaron Smith if (Names.GetValues(regex, ids)) { 13217ac1c780SAaron Smith for (auto id : ids) { 13227ac1c780SAaron Smith if (resolved_ids.find(id) == resolved_ids.end()) 13237ac1c780SAaron Smith if (ResolveFunction(id, include_inlines, sc_list)) 13247ac1c780SAaron Smith resolved_ids.insert(id); 13257ac1c780SAaron Smith } 13267ac1c780SAaron Smith } 13277ac1c780SAaron Smith }; 13287ac1c780SAaron Smith ResolveFn(m_func_full_names); 13297ac1c780SAaron Smith ResolveFn(m_func_base_names); 13307ac1c780SAaron Smith 13317ac1c780SAaron Smith return sc_list.GetSize() - old_size; 133274e08ca0SZachary Turner } 133374e08ca0SZachary Turner 1334b9c1b51eSKate Stone void SymbolFilePDB::GetMangledNamesForFunction( 1335b9c1b51eSKate Stone const std::string &scope_qualified_name, 1336b9c1b51eSKate Stone std::vector<lldb_private::ConstString> &mangled_names) {} 133774e08ca0SZachary Turner 13388cfb12b9SAleksandr Urakov void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) { 13398cfb12b9SAleksandr Urakov std::set<lldb::addr_t> sym_addresses; 13408cfb12b9SAleksandr Urakov for (size_t i = 0; i < symtab.GetNumSymbols(); i++) 13418cfb12b9SAleksandr Urakov sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress()); 13428cfb12b9SAleksandr Urakov 13438cfb12b9SAleksandr Urakov auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>(); 13448cfb12b9SAleksandr Urakov if (!results) 13458cfb12b9SAleksandr Urakov return; 13468cfb12b9SAleksandr Urakov 13478cfb12b9SAleksandr Urakov auto section_list = m_obj_file->GetSectionList(); 13488cfb12b9SAleksandr Urakov if (!section_list) 13498cfb12b9SAleksandr Urakov return; 13508cfb12b9SAleksandr Urakov 13518cfb12b9SAleksandr Urakov while (auto pub_symbol = results->getNext()) { 13527db8b5c4SPavel Labath auto section_id = pub_symbol->getAddressSection(); 13538cfb12b9SAleksandr Urakov 13547db8b5c4SPavel Labath auto section = section_list->FindSectionByID(section_id); 13558cfb12b9SAleksandr Urakov if (!section) 13568cfb12b9SAleksandr Urakov continue; 13578cfb12b9SAleksandr Urakov 13588cfb12b9SAleksandr Urakov auto offset = pub_symbol->getAddressOffset(); 13598cfb12b9SAleksandr Urakov 13608cfb12b9SAleksandr Urakov auto file_addr = section->GetFileAddress() + offset; 13618cfb12b9SAleksandr Urakov if (sym_addresses.find(file_addr) != sym_addresses.end()) 13628cfb12b9SAleksandr Urakov continue; 13638cfb12b9SAleksandr Urakov sym_addresses.insert(file_addr); 13648cfb12b9SAleksandr Urakov 13658cfb12b9SAleksandr Urakov auto size = pub_symbol->getLength(); 13668cfb12b9SAleksandr Urakov symtab.AddSymbol( 13678cfb12b9SAleksandr Urakov Symbol(pub_symbol->getSymIndexId(), // symID 13688cfb12b9SAleksandr Urakov pub_symbol->getName().c_str(), // name 13698cfb12b9SAleksandr Urakov true, // name_is_mangled 13708cfb12b9SAleksandr Urakov pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type 13718cfb12b9SAleksandr Urakov true, // external 13728cfb12b9SAleksandr Urakov false, // is_debug 13738cfb12b9SAleksandr Urakov false, // is_trampoline 13748cfb12b9SAleksandr Urakov false, // is_artificial 13758cfb12b9SAleksandr Urakov section, // section_sp 13768cfb12b9SAleksandr Urakov offset, // value 13778cfb12b9SAleksandr Urakov size, // size 13788cfb12b9SAleksandr Urakov size != 0, // size_is_valid 13798cfb12b9SAleksandr Urakov false, // contains_linker_annotations 13808cfb12b9SAleksandr Urakov 0 // flags 13818cfb12b9SAleksandr Urakov )); 13828cfb12b9SAleksandr Urakov } 13838cfb12b9SAleksandr Urakov 13848cfb12b9SAleksandr Urakov symtab.CalculateSymbolSizes(); 13858cfb12b9SAleksandr Urakov symtab.Finalize(); 13868cfb12b9SAleksandr Urakov } 13878cfb12b9SAleksandr Urakov 1388b9c1b51eSKate Stone uint32_t SymbolFilePDB::FindTypes( 1389b9c1b51eSKate Stone const lldb_private::ConstString &name, 1390b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 1391b9c1b51eSKate Stone uint32_t max_matches, 139274e08ca0SZachary Turner llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1393b9c1b51eSKate Stone lldb_private::TypeMap &types) { 139442dff790SZachary Turner if (!append) 139542dff790SZachary Turner types.Clear(); 139642dff790SZachary Turner if (!name) 139742dff790SZachary Turner return 0; 13987ac1c780SAaron Smith if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 13997ac1c780SAaron Smith return 0; 140042dff790SZachary Turner 140142dff790SZachary Turner searched_symbol_files.clear(); 140242dff790SZachary Turner searched_symbol_files.insert(this); 140342dff790SZachary Turner 140486e9434dSAaron Smith // There is an assumption 'name' is not a regex 1405c1e530eeSAleksandr Urakov FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types); 140686e9434dSAaron Smith 140742dff790SZachary Turner return types.GetSize(); 140842dff790SZachary Turner } 140942dff790SZachary Turner 14104911023fSZachary Turner void SymbolFilePDB::DumpClangAST(Stream &s) { 14114911023fSZachary Turner auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 14124911023fSZachary Turner auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 14134911023fSZachary Turner if (!clang) 14144911023fSZachary Turner return; 14154911023fSZachary Turner clang->Dump(s); 14164911023fSZachary Turner } 14174911023fSZachary Turner 1418c8316ed2SAaron Smith void SymbolFilePDB::FindTypesByRegex( 1419c8316ed2SAaron Smith const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1420b9c1b51eSKate Stone lldb_private::TypeMap &types) { 1421b9c1b51eSKate Stone // When searching by regex, we need to go out of our way to limit the search 14229d0eb996SAdrian McCarthy // space as much as possible since this searches EVERYTHING in the PDB, 14239d0eb996SAdrian McCarthy // manually doing regex comparisons. PDB library isn't optimized for regex 14249d0eb996SAdrian McCarthy // searches or searches across multiple symbol types at the same time, so the 1425b9c1b51eSKate Stone // best we can do is to search enums, then typedefs, then classes one by one, 14269d0eb996SAdrian McCarthy // and do a regex comparison against each of them. 1427b9c1b51eSKate Stone PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 1428b9c1b51eSKate Stone PDB_SymType::UDT}; 142954fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 143042dff790SZachary Turner 143142dff790SZachary Turner uint32_t matches = 0; 143242dff790SZachary Turner 1433b9c1b51eSKate Stone for (auto tag : tags_to_search) { 143410a02577SAaron Smith results = m_global_scope_up->findAllChildren(tag); 143510a02577SAaron Smith if (!results) 143610a02577SAaron Smith continue; 143710a02577SAaron Smith 1438b9c1b51eSKate Stone while (auto result = results->getNext()) { 143942dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 144042dff790SZachary Turner break; 144142dff790SZachary Turner 144242dff790SZachary Turner std::string type_name; 144354fd7ff6SZachary Turner if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 144442dff790SZachary Turner type_name = enum_type->getName(); 1445b9c1b51eSKate Stone else if (auto typedef_type = 1446b9c1b51eSKate Stone llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 144742dff790SZachary Turner type_name = typedef_type->getName(); 144854fd7ff6SZachary Turner else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 144942dff790SZachary Turner type_name = class_type->getName(); 1450b9c1b51eSKate Stone else { 14519d0eb996SAdrian McCarthy // We're looking only for types that have names. Skip symbols, as well 14529d0eb996SAdrian McCarthy // as unnamed types such as arrays, pointers, etc. 145342dff790SZachary Turner continue; 145442dff790SZachary Turner } 145542dff790SZachary Turner 145686e9434dSAaron Smith if (!regex.Execute(type_name)) 145742dff790SZachary Turner continue; 145842dff790SZachary Turner 1459b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1460b9c1b51eSKate Stone // lookup. 146142dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 146242dff790SZachary Turner continue; 146342dff790SZachary Turner 146442dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 146542dff790SZachary Turner if (iter == m_types.end()) 146642dff790SZachary Turner continue; 146742dff790SZachary Turner types.Insert(iter->second); 146842dff790SZachary Turner ++matches; 146942dff790SZachary Turner } 147042dff790SZachary Turner } 147142dff790SZachary Turner } 147242dff790SZachary Turner 1473709426b3SAleksandr Urakov void SymbolFilePDB::FindTypesByName( 1474c1e530eeSAleksandr Urakov llvm::StringRef name, 1475709426b3SAleksandr Urakov const lldb_private::CompilerDeclContext *parent_decl_ctx, 1476709426b3SAleksandr Urakov uint32_t max_matches, lldb_private::TypeMap &types) { 147754fd7ff6SZachary Turner std::unique_ptr<IPDBEnumSymbols> results; 1478f76fe682SAaron Smith if (name.empty()) 1479f76fe682SAaron Smith return; 1480709426b3SAleksandr Urakov results = m_global_scope_up->findAllChildren(PDB_SymType::None); 148110a02577SAaron Smith if (!results) 148210a02577SAaron Smith return; 148342dff790SZachary Turner 148442dff790SZachary Turner uint32_t matches = 0; 148542dff790SZachary Turner 1486b9c1b51eSKate Stone while (auto result = results->getNext()) { 148742dff790SZachary Turner if (max_matches > 0 && matches >= max_matches) 148842dff790SZachary Turner break; 1489709426b3SAleksandr Urakov 1490c1e530eeSAleksandr Urakov if (MSVCUndecoratedNameParser::DropScope( 1491c1e530eeSAleksandr Urakov result->getRawSymbol().getName()) != name) 1492709426b3SAleksandr Urakov continue; 1493709426b3SAleksandr Urakov 1494b9c1b51eSKate Stone switch (result->getSymTag()) { 149554fd7ff6SZachary Turner case PDB_SymType::Enum: 149654fd7ff6SZachary Turner case PDB_SymType::UDT: 149754fd7ff6SZachary Turner case PDB_SymType::Typedef: 149842dff790SZachary Turner break; 149942dff790SZachary Turner default: 150005097246SAdrian Prantl // We're looking only for types that have names. Skip symbols, as well 150105097246SAdrian Prantl // as unnamed types such as arrays, pointers, etc. 150242dff790SZachary Turner continue; 150342dff790SZachary Turner } 150442dff790SZachary Turner 1505b9c1b51eSKate Stone // This should cause the type to get cached and stored in the `m_types` 1506b9c1b51eSKate Stone // lookup. 150742dff790SZachary Turner if (!ResolveTypeUID(result->getSymIndexId())) 150842dff790SZachary Turner continue; 150942dff790SZachary Turner 1510a5235af9SAleksandr Urakov if (parent_decl_ctx && GetDeclContextContainingUID( 1511a5235af9SAleksandr Urakov result->getSymIndexId()) != *parent_decl_ctx) 1512709426b3SAleksandr Urakov continue; 1513709426b3SAleksandr Urakov 151442dff790SZachary Turner auto iter = m_types.find(result->getSymIndexId()); 151542dff790SZachary Turner if (iter == m_types.end()) 151642dff790SZachary Turner continue; 151742dff790SZachary Turner types.Insert(iter->second); 151842dff790SZachary Turner ++matches; 151942dff790SZachary Turner } 152074e08ca0SZachary Turner } 152174e08ca0SZachary Turner 1522b9c1b51eSKate Stone size_t SymbolFilePDB::FindTypes( 1523b9c1b51eSKate Stone const std::vector<lldb_private::CompilerContext> &contexts, bool append, 1524b9c1b51eSKate Stone lldb_private::TypeMap &types) { 152542dff790SZachary Turner return 0; 152674e08ca0SZachary Turner } 152774e08ca0SZachary Turner 1528ec40f818SAaron Smith lldb_private::TypeList *SymbolFilePDB::GetTypeList() { 1529ec40f818SAaron Smith return m_obj_file->GetModule()->GetTypeList(); 1530ec40f818SAaron Smith } 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) { 15857ac1c780SAaron Smith TypeCollection type_collection; 15867ac1c780SAaron Smith uint32_t old_size = type_list.GetSize(); 1587c8316ed2SAaron Smith CompileUnit *cu = 1588c8316ed2SAaron Smith sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 15897ac1c780SAaron Smith if (cu) { 15907ac1c780SAaron Smith auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1591e664b5dcSAaron Smith if (!compiland_up) 1592e664b5dcSAaron Smith return 0; 1593e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 15947ac1c780SAaron Smith } else { 15957ac1c780SAaron Smith for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 15967ac1c780SAaron Smith auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1597d5a925f4SAaron Smith if (cu_sp) { 1598e664b5dcSAaron Smith if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1599e664b5dcSAaron Smith GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 16007ac1c780SAaron Smith } 16017ac1c780SAaron Smith } 16027ac1c780SAaron Smith } 16037ac1c780SAaron Smith 16047ac1c780SAaron Smith for (auto type : type_collection) { 16057ac1c780SAaron Smith type->GetForwardCompilerType(); 16067ac1c780SAaron Smith type_list.Insert(type->shared_from_this()); 16077ac1c780SAaron Smith } 16087ac1c780SAaron Smith return type_list.GetSize() - old_size; 160974e08ca0SZachary Turner } 161074e08ca0SZachary Turner 161174e08ca0SZachary Turner lldb_private::TypeSystem * 1612b9c1b51eSKate Stone SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1613b9c1b51eSKate Stone auto type_system = 1614b9c1b51eSKate Stone m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 161574e08ca0SZachary Turner if (type_system) 161674e08ca0SZachary Turner type_system->SetSymbolFile(this); 161774e08ca0SZachary Turner return type_system; 161874e08ca0SZachary Turner } 161974e08ca0SZachary Turner 1620c68925abSZachary Turner PDBASTParser *SymbolFilePDB::GetPDBAstParser() { 1621c68925abSZachary Turner auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1622c68925abSZachary Turner auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 1623c68925abSZachary Turner if (!clang_type_system) 1624c68925abSZachary Turner return nullptr; 1625c68925abSZachary Turner 1626c68925abSZachary Turner return clang_type_system->GetPDBParser(); 1627c68925abSZachary Turner } 1628c68925abSZachary Turner 1629c68925abSZachary Turner 1630b9c1b51eSKate Stone lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 1631b9c1b51eSKate Stone const lldb_private::ConstString &name, 1632b9c1b51eSKate Stone const lldb_private::CompilerDeclContext *parent_decl_ctx) { 1633709426b3SAleksandr Urakov auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1634709426b3SAleksandr Urakov auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); 1635709426b3SAleksandr Urakov if (!clang_type_system) 1636709426b3SAleksandr Urakov return CompilerDeclContext(); 1637709426b3SAleksandr Urakov 1638709426b3SAleksandr Urakov PDBASTParser *pdb = clang_type_system->GetPDBParser(); 1639709426b3SAleksandr Urakov if (!pdb) 1640709426b3SAleksandr Urakov return CompilerDeclContext(); 1641709426b3SAleksandr Urakov 1642709426b3SAleksandr Urakov clang::DeclContext *decl_context = nullptr; 1643709426b3SAleksandr Urakov if (parent_decl_ctx) 1644709426b3SAleksandr Urakov decl_context = static_cast<clang::DeclContext *>( 1645709426b3SAleksandr Urakov parent_decl_ctx->GetOpaqueDeclContext()); 1646709426b3SAleksandr Urakov 1647709426b3SAleksandr Urakov auto namespace_decl = 1648709426b3SAleksandr Urakov pdb->FindNamespaceDecl(decl_context, name.GetStringRef()); 1649709426b3SAleksandr Urakov if (!namespace_decl) 1650709426b3SAleksandr Urakov return CompilerDeclContext(); 1651709426b3SAleksandr Urakov 1652709426b3SAleksandr Urakov return CompilerDeclContext(type_system, 1653709426b3SAleksandr Urakov static_cast<clang::DeclContext *>(namespace_decl)); 165474e08ca0SZachary Turner } 165574e08ca0SZachary Turner 1656b9c1b51eSKate Stone lldb_private::ConstString SymbolFilePDB::GetPluginName() { 165774e08ca0SZachary Turner static ConstString g_name("pdb"); 165874e08ca0SZachary Turner return g_name; 165974e08ca0SZachary Turner } 166074e08ca0SZachary Turner 1661b9c1b51eSKate Stone uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 166274e08ca0SZachary Turner 1663b9c1b51eSKate Stone IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1664b9c1b51eSKate Stone 1665b9c1b51eSKate Stone const IPDBSession &SymbolFilePDB::GetPDBSession() const { 166642dff790SZachary Turner return *m_session_up; 166742dff790SZachary Turner } 166842dff790SZachary Turner 1669c8316ed2SAaron Smith lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, 1670c8316ed2SAaron Smith uint32_t index) { 167174e08ca0SZachary Turner auto found_cu = m_comp_units.find(id); 167274e08ca0SZachary Turner if (found_cu != m_comp_units.end()) 167374e08ca0SZachary Turner return found_cu->second; 167474e08ca0SZachary Turner 167510a02577SAaron Smith auto compiland_up = GetPDBCompilandByUID(id); 167610a02577SAaron Smith if (!compiland_up) 167710a02577SAaron Smith return CompUnitSP(); 167874e08ca0SZachary Turner 167974e08ca0SZachary Turner lldb::LanguageType lang; 168010a02577SAaron Smith auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 168174e08ca0SZachary Turner if (!details) 168274e08ca0SZachary Turner lang = lldb::eLanguageTypeC_plus_plus; 168374e08ca0SZachary Turner else 168474e08ca0SZachary Turner lang = TranslateLanguage(details->getLanguage()); 168574e08ca0SZachary Turner 1686f76fe682SAaron Smith if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1687f76fe682SAaron Smith return CompUnitSP(); 1688f76fe682SAaron Smith 1689487b0c6bSAaron Smith std::string path = compiland_up->getSourceFileFullPath(); 1690f76fe682SAaron Smith if (path.empty()) 1691f76fe682SAaron Smith return CompUnitSP(); 1692f76fe682SAaron Smith 1693b9c1b51eSKate Stone // Don't support optimized code for now, DebugInfoPDB does not return this 1694b9c1b51eSKate Stone // information. 1695ad2b63cbSGreg Clayton LazyBool optimized = eLazyBoolNo; 1696c8316ed2SAaron Smith auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, 1697c8316ed2SAaron Smith path.c_str(), id, lang, optimized); 169810a02577SAaron Smith 169910a02577SAaron Smith if (!cu_sp) 170010a02577SAaron Smith return CompUnitSP(); 170110a02577SAaron Smith 170210a02577SAaron Smith m_comp_units.insert(std::make_pair(id, cu_sp)); 170310a02577SAaron Smith if (index == UINT32_MAX) 1704e664b5dcSAaron Smith GetCompileUnitIndex(*compiland_up, index); 170510a02577SAaron Smith lldbassert(index != UINT32_MAX); 1706c8316ed2SAaron Smith m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index, 1707c8316ed2SAaron Smith cu_sp); 170810a02577SAaron Smith return cu_sp; 170974e08ca0SZachary Turner } 171074e08ca0SZachary Turner 1711863f8c18SZachary Turner bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit, 1712863f8c18SZachary Turner uint32_t match_line) { 1713863f8c18SZachary Turner auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 171410a02577SAaron Smith if (!compiland_up) 171510a02577SAaron Smith return false; 171674e08ca0SZachary Turner 1717b9c1b51eSKate Stone // LineEntry needs the *index* of the file into the list of support files 17189d0eb996SAdrian McCarthy // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 171905097246SAdrian Prantl // a globally unique idenfitifier in the namespace of the PDB. So, we have 172005097246SAdrian Prantl // to do a mapping so that we can hand out indices. 172142dff790SZachary Turner llvm::DenseMap<uint32_t, uint32_t> index_map; 172210a02577SAaron Smith BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 1723863f8c18SZachary Turner auto line_table = llvm::make_unique<LineTable>(&comp_unit); 172474e08ca0SZachary Turner 172510a02577SAaron Smith // Find contributions to `compiland` from all source and header files. 1726863f8c18SZachary Turner std::string path = comp_unit.GetPath(); 172710a02577SAaron Smith auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 172810a02577SAaron Smith if (!files) 172910a02577SAaron Smith return false; 173074e08ca0SZachary Turner 173105097246SAdrian Prantl // For each source and header file, create a LineSequence for contributions 173205097246SAdrian Prantl // to the compiland from that file, and add the sequence. 1733b9c1b51eSKate Stone while (auto file = files->getNext()) { 1734b9c1b51eSKate Stone std::unique_ptr<LineSequence> sequence( 1735b9c1b51eSKate Stone line_table->CreateLineSequenceContainer()); 173610a02577SAaron Smith auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 173710a02577SAaron Smith if (!lines) 173810a02577SAaron Smith continue; 173974e08ca0SZachary Turner int entry_count = lines->getChildCount(); 174074e08ca0SZachary Turner 17417e8c7beaSZachary Turner uint64_t prev_addr; 17427e8c7beaSZachary Turner uint32_t prev_length; 17437e8c7beaSZachary Turner uint32_t prev_line; 17447e8c7beaSZachary Turner uint32_t prev_source_idx; 17457e8c7beaSZachary Turner 1746b9c1b51eSKate Stone for (int i = 0; i < entry_count; ++i) { 174774e08ca0SZachary Turner auto line = lines->getChildAtIndex(i); 174874e08ca0SZachary Turner 17497e8c7beaSZachary Turner uint64_t lno = line->getLineNumber(); 17507e8c7beaSZachary Turner uint64_t addr = line->getVirtualAddress(); 17517e8c7beaSZachary Turner uint32_t length = line->getLength(); 175274e08ca0SZachary Turner uint32_t source_id = line->getSourceFileId(); 17537e8c7beaSZachary Turner uint32_t col = line->getColumnNumber(); 175474e08ca0SZachary Turner uint32_t source_idx = index_map[source_id]; 175574e08ca0SZachary Turner 175605097246SAdrian Prantl // There was a gap between the current entry and the previous entry if 175705097246SAdrian Prantl // the addresses don't perfectly line up. 17587e8c7beaSZachary Turner bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 17597e8c7beaSZachary Turner 1760b9c1b51eSKate Stone // Before inserting the current entry, insert a terminal entry at the end 17619d0eb996SAdrian McCarthy // of the previous entry's address range if the current entry resulted in 17629d0eb996SAdrian McCarthy // a gap from the previous entry. 1763b9c1b51eSKate Stone if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1764b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1765b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 17667e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 1767010edd37SAaron Smith 1768010edd37SAaron Smith line_table->InsertSequence(sequence.release()); 1769010edd37SAaron Smith sequence.reset(line_table->CreateLineSequenceContainer()); 17707e8c7beaSZachary Turner } 17717e8c7beaSZachary Turner 1772b9c1b51eSKate Stone if (ShouldAddLine(match_line, lno, length)) { 17737e8c7beaSZachary Turner bool is_statement = line->isStatement(); 177474e08ca0SZachary Turner bool is_prologue = false; 177574e08ca0SZachary Turner bool is_epilogue = false; 1776b9c1b51eSKate Stone auto func = 1777b9c1b51eSKate Stone m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1778b9c1b51eSKate Stone if (func) { 177954fd7ff6SZachary Turner auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 178010a02577SAaron Smith if (prologue) 17817e8c7beaSZachary Turner is_prologue = (addr == prologue->getVirtualAddress()); 178274e08ca0SZachary Turner 178354fd7ff6SZachary Turner auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 178410a02577SAaron Smith if (epilogue) 17857e8c7beaSZachary Turner is_epilogue = (addr == epilogue->getVirtualAddress()); 17867e8c7beaSZachary Turner } 178774e08ca0SZachary Turner 1788b9c1b51eSKate Stone line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1789b9c1b51eSKate Stone source_idx, is_statement, false, 17907e8c7beaSZachary Turner is_prologue, is_epilogue, false); 17917e8c7beaSZachary Turner } 17927e8c7beaSZachary Turner 17937e8c7beaSZachary Turner prev_addr = addr; 17947e8c7beaSZachary Turner prev_length = length; 17957e8c7beaSZachary Turner prev_line = lno; 17967e8c7beaSZachary Turner prev_source_idx = source_idx; 17977e8c7beaSZachary Turner } 17987e8c7beaSZachary Turner 1799b9c1b51eSKate Stone if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 18007e8c7beaSZachary Turner // The end is always a terminal entry, so insert it regardless. 1801b9c1b51eSKate Stone line_table->AppendLineEntryToSequence( 1802b9c1b51eSKate Stone sequence.get(), prev_addr + prev_length, prev_line, 0, 18037e8c7beaSZachary Turner prev_source_idx, false, false, false, false, true); 180474e08ca0SZachary Turner } 180574e08ca0SZachary Turner 18067e8c7beaSZachary Turner line_table->InsertSequence(sequence.release()); 180774e08ca0SZachary Turner } 180874e08ca0SZachary Turner 180910a02577SAaron Smith if (line_table->GetSize()) { 1810863f8c18SZachary Turner comp_unit.SetLineTable(line_table.release()); 181174e08ca0SZachary Turner return true; 181274e08ca0SZachary Turner } 181310a02577SAaron Smith return false; 181410a02577SAaron Smith } 181574e08ca0SZachary Turner 1816b9c1b51eSKate Stone void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 181710a02577SAaron Smith const PDBSymbolCompiland &compiland, 1818b9c1b51eSKate Stone llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 181905097246SAdrian Prantl // This is a hack, but we need to convert the source id into an index into 182005097246SAdrian Prantl // the support files array. We don't want to do path comparisons to avoid 18219d0eb996SAdrian McCarthy // basename / full path issues that may or may not even be a problem, so we 18229d0eb996SAdrian McCarthy // use the globally unique source file identifiers. Ideally we could use the 18239d0eb996SAdrian McCarthy // global identifiers everywhere, but LineEntry currently assumes indices. 182410a02577SAaron Smith auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 182510a02577SAaron Smith if (!source_files) 182610a02577SAaron Smith return; 18279ea80d25SPavel Labath 18289ea80d25SPavel Labath // LLDB uses the DWARF-like file numeration (one based) 18299ea80d25SPavel Labath int index = 1; 183074e08ca0SZachary Turner 1831b9c1b51eSKate Stone while (auto file = source_files->getNext()) { 183274e08ca0SZachary Turner uint32_t source_id = file->getUniqueId(); 183374e08ca0SZachary Turner index_map[source_id] = index++; 183474e08ca0SZachary Turner } 183574e08ca0SZachary Turner } 18367ac1c780SAaron Smith 18377ac1c780SAaron Smith lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 18387ac1c780SAaron Smith const lldb_private::Address &so_addr) { 18397ac1c780SAaron Smith lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1840308e39caSAaron Smith if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 18417ac1c780SAaron Smith return nullptr; 18427ac1c780SAaron Smith 1843308e39caSAaron Smith // If it is a PDB function's vm addr, this is the first sure bet. 1844308e39caSAaron Smith if (auto lines = 1845308e39caSAaron Smith m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { 1846308e39caSAaron Smith if (auto first_line = lines->getNext()) 1847308e39caSAaron Smith return ParseCompileUnitForUID(first_line->getCompilandId()); 18487ac1c780SAaron Smith } 18497ac1c780SAaron Smith 1850308e39caSAaron Smith // Otherwise we resort to section contributions. 1851308e39caSAaron Smith if (auto sec_contribs = m_session_up->getSectionContribs()) { 1852308e39caSAaron Smith while (auto section = sec_contribs->getNext()) { 1853308e39caSAaron Smith auto va = section->getVirtualAddress(); 1854308e39caSAaron Smith if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) 1855308e39caSAaron Smith return ParseCompileUnitForUID(section->getCompilandId()); 1856308e39caSAaron Smith } 1857308e39caSAaron Smith } 18587ac1c780SAaron Smith return nullptr; 18597ac1c780SAaron Smith } 18607ac1c780SAaron Smith 18617ac1c780SAaron Smith Mangled 1862e664b5dcSAaron Smith SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 18637ac1c780SAaron Smith Mangled mangled; 1864e664b5dcSAaron Smith auto func_name = pdb_func.getName(); 1865e664b5dcSAaron Smith auto func_undecorated_name = pdb_func.getUndecoratedName(); 18667ac1c780SAaron Smith std::string func_decorated_name; 18677ac1c780SAaron Smith 18687ac1c780SAaron Smith // Seek from public symbols for non-static function's decorated name if any. 18697ac1c780SAaron Smith // For static functions, they don't have undecorated names and aren't exposed 18707ac1c780SAaron Smith // in Public Symbols either. 18717ac1c780SAaron Smith if (!func_undecorated_name.empty()) { 1872c8316ed2SAaron Smith auto result_up = m_global_scope_up->findChildren( 1873c8316ed2SAaron Smith PDB_SymType::PublicSymbol, func_undecorated_name, 18747ac1c780SAaron Smith PDB_NameSearchFlags::NS_UndecoratedName); 18757ac1c780SAaron Smith if (result_up) { 18767ac1c780SAaron Smith while (auto symbol_up = result_up->getNext()) { 18777ac1c780SAaron Smith // For a public symbol, it is unique. 18787ac1c780SAaron Smith lldbassert(result_up->getChildCount() == 1); 18797ac1c780SAaron Smith if (auto *pdb_public_sym = 1880c8316ed2SAaron Smith llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( 1881c8316ed2SAaron Smith symbol_up.get())) { 18827ac1c780SAaron Smith if (pdb_public_sym->isFunction()) { 18837ac1c780SAaron Smith func_decorated_name = pdb_public_sym->getName(); 1884f76fe682SAaron Smith break; 18857ac1c780SAaron Smith } 18867ac1c780SAaron Smith } 18877ac1c780SAaron Smith } 18887ac1c780SAaron Smith } 18897ac1c780SAaron Smith } 18907ac1c780SAaron Smith if (!func_decorated_name.empty()) { 18917ac1c780SAaron Smith mangled.SetMangledName(ConstString(func_decorated_name)); 18927ac1c780SAaron Smith 18937ac1c780SAaron Smith // For MSVC, format of C funciton's decorated name depends on calling 18947ac1c780SAaron Smith // conventon. Unfortunately none of the format is recognized by current 18957ac1c780SAaron Smith // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 189605097246SAdrian Prantl // `__purecall` is retrieved as both its decorated and undecorated name 189705097246SAdrian Prantl // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` 189805097246SAdrian Prantl // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). 189905097246SAdrian Prantl // Mangled::GetDemangledName method will fail internally and caches an 190005097246SAdrian Prantl // empty string as its undecorated name. So we will face a contradition 190105097246SAdrian Prantl // here for the same symbol: 19027ac1c780SAaron Smith // non-empty undecorated name from PDB 19037ac1c780SAaron Smith // empty undecorated name from LLDB 19047ac1c780SAaron Smith if (!func_undecorated_name.empty() && 19057ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) 19067ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19077ac1c780SAaron Smith 19087ac1c780SAaron Smith // LLDB uses several flags to control how a C++ decorated name is 190905097246SAdrian Prantl // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the 191005097246SAdrian Prantl // yielded name could be different from what we retrieve from 19117ac1c780SAaron Smith // PDB source unless we also apply same flags in getting undecorated 19127ac1c780SAaron Smith // name through PDBSymbolFunc::getUndecoratedNameEx method. 19137ac1c780SAaron Smith if (!func_undecorated_name.empty() && 19147ac1c780SAaron Smith mangled.GetDemangledName(mangled.GuessLanguage()) != 19157ac1c780SAaron Smith ConstString(func_undecorated_name)) 19167ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19177ac1c780SAaron Smith } else if (!func_undecorated_name.empty()) { 19187ac1c780SAaron Smith mangled.SetDemangledName(ConstString(func_undecorated_name)); 19197ac1c780SAaron Smith } else if (!func_name.empty()) 19207ac1c780SAaron Smith mangled.SetValue(ConstString(func_name), false); 19217ac1c780SAaron Smith 19227ac1c780SAaron Smith return mangled; 19237ac1c780SAaron Smith } 19247ac1c780SAaron Smith 19257ac1c780SAaron Smith bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 19267ac1c780SAaron Smith const lldb_private::CompilerDeclContext *decl_ctx) { 19277ac1c780SAaron Smith if (decl_ctx == nullptr || !decl_ctx->IsValid()) 19287ac1c780SAaron Smith return true; 19297ac1c780SAaron Smith 19307ac1c780SAaron Smith TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 19317ac1c780SAaron Smith if (!decl_ctx_type_system) 19327ac1c780SAaron Smith return false; 19337ac1c780SAaron Smith TypeSystem *type_system = GetTypeSystemForLanguage( 19347ac1c780SAaron Smith decl_ctx_type_system->GetMinimumLanguage(nullptr)); 19357ac1c780SAaron Smith if (decl_ctx_type_system == type_system) 19367ac1c780SAaron Smith return true; // The type systems match, return true 19377ac1c780SAaron Smith 19387ac1c780SAaron Smith return false; 19397ac1c780SAaron Smith } 1940356aa4a9SAleksandr Urakov 1941356aa4a9SAleksandr Urakov uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) { 1942356aa4a9SAleksandr Urakov static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) { 1943356aa4a9SAleksandr Urakov return lhs < rhs.Offset; 1944356aa4a9SAleksandr Urakov }; 1945356aa4a9SAleksandr Urakov 1946356aa4a9SAleksandr Urakov // Cache section contributions 1947356aa4a9SAleksandr Urakov if (m_sec_contribs.empty()) { 1948356aa4a9SAleksandr Urakov if (auto SecContribs = m_session_up->getSectionContribs()) { 1949356aa4a9SAleksandr Urakov while (auto SectionContrib = SecContribs->getNext()) { 1950356aa4a9SAleksandr Urakov auto comp_id = SectionContrib->getCompilandId(); 1951356aa4a9SAleksandr Urakov if (!comp_id) 1952356aa4a9SAleksandr Urakov continue; 1953356aa4a9SAleksandr Urakov 1954356aa4a9SAleksandr Urakov auto sec = SectionContrib->getAddressSection(); 1955356aa4a9SAleksandr Urakov auto &sec_cs = m_sec_contribs[sec]; 1956356aa4a9SAleksandr Urakov 1957356aa4a9SAleksandr Urakov auto offset = SectionContrib->getAddressOffset(); 1958356aa4a9SAleksandr Urakov auto it = 1959356aa4a9SAleksandr Urakov std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper); 1960356aa4a9SAleksandr Urakov 1961356aa4a9SAleksandr Urakov auto size = SectionContrib->getLength(); 1962356aa4a9SAleksandr Urakov sec_cs.insert(it, {offset, size, comp_id}); 1963356aa4a9SAleksandr Urakov } 1964356aa4a9SAleksandr Urakov } 1965356aa4a9SAleksandr Urakov } 1966356aa4a9SAleksandr Urakov 1967356aa4a9SAleksandr Urakov // Check by line number 1968356aa4a9SAleksandr Urakov if (auto Lines = data.getLineNumbers()) { 1969356aa4a9SAleksandr Urakov if (auto FirstLine = Lines->getNext()) 1970356aa4a9SAleksandr Urakov return FirstLine->getCompilandId(); 1971356aa4a9SAleksandr Urakov } 1972356aa4a9SAleksandr Urakov 1973356aa4a9SAleksandr Urakov // Retrieve section + offset 1974356aa4a9SAleksandr Urakov uint32_t DataSection = data.getAddressSection(); 1975356aa4a9SAleksandr Urakov uint32_t DataOffset = data.getAddressOffset(); 1976356aa4a9SAleksandr Urakov if (DataSection == 0) { 1977356aa4a9SAleksandr Urakov if (auto RVA = data.getRelativeVirtualAddress()) 1978356aa4a9SAleksandr Urakov m_session_up->addressForRVA(RVA, DataSection, DataOffset); 1979356aa4a9SAleksandr Urakov } 1980356aa4a9SAleksandr Urakov 1981356aa4a9SAleksandr Urakov if (DataSection) { 1982356aa4a9SAleksandr Urakov // Search by section contributions 1983356aa4a9SAleksandr Urakov auto &sec_cs = m_sec_contribs[DataSection]; 1984356aa4a9SAleksandr Urakov auto it = 1985356aa4a9SAleksandr Urakov std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper); 1986356aa4a9SAleksandr Urakov if (it != sec_cs.begin()) { 1987356aa4a9SAleksandr Urakov --it; 1988356aa4a9SAleksandr Urakov if (DataOffset < it->Offset + it->Size) 1989356aa4a9SAleksandr Urakov return it->CompilandId; 1990356aa4a9SAleksandr Urakov } 1991356aa4a9SAleksandr Urakov } else { 1992356aa4a9SAleksandr Urakov // Search in lexical tree 1993356aa4a9SAleksandr Urakov auto LexParentId = data.getLexicalParentId(); 1994356aa4a9SAleksandr Urakov while (auto LexParent = m_session_up->getSymbolById(LexParentId)) { 1995356aa4a9SAleksandr Urakov if (LexParent->getSymTag() == PDB_SymType::Exe) 1996356aa4a9SAleksandr Urakov break; 1997356aa4a9SAleksandr Urakov if (LexParent->getSymTag() == PDB_SymType::Compiland) 1998356aa4a9SAleksandr Urakov return LexParentId; 1999356aa4a9SAleksandr Urakov LexParentId = LexParent->getRawSymbol().getLexicalParentId(); 2000356aa4a9SAleksandr Urakov } 2001356aa4a9SAleksandr Urakov } 2002356aa4a9SAleksandr Urakov 2003356aa4a9SAleksandr Urakov return 0; 2004356aa4a9SAleksandr Urakov } 2005