1 //===-- SymbolFilePDB.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SymbolFilePDB.h" 10 11 #include "PDBASTParser.h" 12 #include "PDBLocationToDWARFExpression.h" 13 14 #include "clang/Lex/Lexer.h" 15 16 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Symbol/CompileUnit.h" 20 #include "lldb/Symbol/LineTable.h" 21 #include "lldb/Symbol/ObjectFile.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Symbol/SymbolVendor.h" 24 #include "lldb/Symbol/TypeList.h" 25 #include "lldb/Symbol/TypeMap.h" 26 #include "lldb/Symbol/Variable.h" 27 #include "lldb/Utility/LLDBLog.h" 28 #include "lldb/Utility/Log.h" 29 #include "lldb/Utility/RegularExpression.h" 30 31 #include "llvm/Config/llvm-config.h" // for LLVM_ENABLE_DIA_SDK 32 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h" 33 #include "llvm/DebugInfo/PDB/GenericError.h" 34 #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 35 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 36 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 37 #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h" 38 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 39 #include "llvm/DebugInfo/PDB/IPDBTable.h" 40 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 41 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 42 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 43 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 44 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 45 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 46 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 47 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 48 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 49 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 50 #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h" 51 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 52 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h" 53 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 54 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 55 56 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 57 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" 58 #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h" 59 60 #if defined(_WIN32) 61 #include "llvm/Config/llvm-config.h" 62 #include <optional> 63 #endif 64 65 using namespace lldb; 66 using namespace lldb_private; 67 using namespace llvm::pdb; 68 69 LLDB_PLUGIN_DEFINE(SymbolFilePDB) 70 71 char SymbolFilePDB::ID; 72 73 namespace { 74 lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 75 switch (lang) { 76 case PDB_Lang::Cpp: 77 return lldb::LanguageType::eLanguageTypeC_plus_plus; 78 case PDB_Lang::C: 79 return lldb::LanguageType::eLanguageTypeC; 80 case PDB_Lang::Swift: 81 return lldb::LanguageType::eLanguageTypeSwift; 82 case PDB_Lang::Rust: 83 return lldb::LanguageType::eLanguageTypeRust; 84 case PDB_Lang::ObjC: 85 return lldb::LanguageType::eLanguageTypeObjC; 86 case PDB_Lang::ObjCpp: 87 return lldb::LanguageType::eLanguageTypeObjC_plus_plus; 88 default: 89 return lldb::LanguageType::eLanguageTypeUnknown; 90 } 91 } 92 93 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 94 uint32_t addr_length) { 95 return ((requested_line == 0 || actual_line == requested_line) && 96 addr_length > 0); 97 } 98 } // namespace 99 100 static bool ShouldUseNativeReader() { 101 #if defined(_WIN32) 102 #if LLVM_ENABLE_DIA_SDK 103 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER"); 104 if (!use_native.equals_insensitive("on") && 105 !use_native.equals_insensitive("yes") && 106 !use_native.equals_insensitive("1") && 107 !use_native.equals_insensitive("true")) 108 return false; 109 #endif 110 #endif 111 return true; 112 } 113 114 void SymbolFilePDB::Initialize() { 115 if (ShouldUseNativeReader()) { 116 npdb::SymbolFileNativePDB::Initialize(); 117 } else { 118 PluginManager::RegisterPlugin(GetPluginNameStatic(), 119 GetPluginDescriptionStatic(), CreateInstance, 120 DebuggerInitialize); 121 } 122 } 123 124 void SymbolFilePDB::Terminate() { 125 if (ShouldUseNativeReader()) { 126 npdb::SymbolFileNativePDB::Terminate(); 127 } else { 128 PluginManager::UnregisterPlugin(CreateInstance); 129 } 130 } 131 132 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 133 134 llvm::StringRef SymbolFilePDB::GetPluginDescriptionStatic() { 135 return "Microsoft PDB debug symbol file reader."; 136 } 137 138 lldb_private::SymbolFile * 139 SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) { 140 return new SymbolFilePDB(std::move(objfile_sp)); 141 } 142 143 SymbolFilePDB::SymbolFilePDB(lldb::ObjectFileSP objfile_sp) 144 : SymbolFileCommon(std::move(objfile_sp)), m_session_up(), m_global_scope_up() {} 145 146 SymbolFilePDB::~SymbolFilePDB() = default; 147 148 uint32_t SymbolFilePDB::CalculateAbilities() { 149 uint32_t abilities = 0; 150 if (!m_objfile_sp) 151 return 0; 152 153 if (!m_session_up) { 154 // Lazily load and match the PDB file, but only do this once. 155 std::string exePath = m_objfile_sp->GetFileSpec().GetPath(); 156 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 157 m_session_up); 158 if (error) { 159 llvm::consumeError(std::move(error)); 160 auto module_sp = m_objfile_sp->GetModule(); 161 if (!module_sp) 162 return 0; 163 // See if any symbol file is specified through `--symfile` option. 164 FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 165 if (!symfile) 166 return 0; 167 error = loadDataForPDB(PDB_ReaderType::DIA, 168 llvm::StringRef(symfile.GetPath()), m_session_up); 169 if (error) { 170 llvm::consumeError(std::move(error)); 171 return 0; 172 } 173 } 174 } 175 if (!m_session_up) 176 return 0; 177 178 auto enum_tables_up = m_session_up->getEnumTables(); 179 if (!enum_tables_up) 180 return 0; 181 while (auto table_up = enum_tables_up->getNext()) { 182 if (table_up->getItemCount() == 0) 183 continue; 184 auto type = table_up->getTableType(); 185 switch (type) { 186 case PDB_TableType::Symbols: 187 // This table represents a store of symbols with types listed in 188 // PDBSym_Type 189 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | 190 LocalVariables | VariableTypes); 191 break; 192 case PDB_TableType::LineNumbers: 193 abilities |= LineTables; 194 break; 195 default: 196 break; 197 } 198 } 199 return abilities; 200 } 201 202 void SymbolFilePDB::InitializeObject() { 203 lldb::addr_t obj_load_address = 204 m_objfile_sp->GetBaseAddress().GetFileAddress(); 205 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); 206 m_session_up->setLoadAddress(obj_load_address); 207 if (!m_global_scope_up) 208 m_global_scope_up = m_session_up->getGlobalScope(); 209 lldbassert(m_global_scope_up.get()); 210 } 211 212 uint32_t SymbolFilePDB::CalculateNumCompileUnits() { 213 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 214 if (!compilands) 215 return 0; 216 217 // The linker could link *.dll (compiland language = LINK), or import 218 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be 219 // found as a child of the global scope (PDB executable). Usually, such 220 // compilands contain `thunk` symbols in which we are not interested for 221 // now. However we still count them in the compiland list. If we perform 222 // any compiland related activity, like finding symbols through 223 // llvm::pdb::IPDBSession methods, such compilands will all be searched 224 // automatically no matter whether we include them or not. 225 uint32_t compile_unit_count = compilands->getChildCount(); 226 227 // The linker can inject an additional "dummy" compilation unit into the 228 // PDB. Ignore this special compile unit for our purposes, if it is there. 229 // It is always the last one. 230 auto last_compiland_up = compilands->getChildAtIndex(compile_unit_count - 1); 231 lldbassert(last_compiland_up.get()); 232 std::string name = last_compiland_up->getName(); 233 if (name == "* Linker *") 234 --compile_unit_count; 235 return compile_unit_count; 236 } 237 238 void SymbolFilePDB::GetCompileUnitIndex( 239 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { 240 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 241 if (!results_up) 242 return; 243 auto uid = pdb_compiland.getSymIndexId(); 244 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 245 auto compiland_up = results_up->getChildAtIndex(cu_idx); 246 if (!compiland_up) 247 continue; 248 if (compiland_up->getSymIndexId() == uid) { 249 index = cu_idx; 250 return; 251 } 252 } 253 index = UINT32_MAX; 254 } 255 256 std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 257 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 258 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 259 } 260 261 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 262 if (index >= GetNumCompileUnits()) 263 return CompUnitSP(); 264 265 // Assuming we always retrieve same compilands listed in same order through 266 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 267 // compile unit makes no sense. 268 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 269 if (!results) 270 return CompUnitSP(); 271 auto compiland_up = results->getChildAtIndex(index); 272 if (!compiland_up) 273 return CompUnitSP(); 274 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 275 } 276 277 lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) { 278 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 279 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 280 if (!compiland_up) 281 return lldb::eLanguageTypeUnknown; 282 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 283 if (!details) 284 return lldb::eLanguageTypeUnknown; 285 return TranslateLanguage(details->getLanguage()); 286 } 287 288 lldb_private::Function * 289 SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func, 290 CompileUnit &comp_unit) { 291 if (FunctionSP result = comp_unit.FindFunctionByUID(pdb_func.getSymIndexId())) 292 return result.get(); 293 294 auto file_vm_addr = pdb_func.getVirtualAddress(); 295 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 296 return nullptr; 297 298 auto func_length = pdb_func.getLength(); 299 Address func_addr(file_vm_addr, 300 GetObjectFile()->GetModule()->GetSectionList()); 301 if (!func_addr.IsValid()) 302 return nullptr; 303 304 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 305 if (!func_type) 306 return nullptr; 307 308 user_id_t func_type_uid = pdb_func.getSignatureId(); 309 310 Mangled mangled = GetMangledForPDBFunc(pdb_func); 311 312 FunctionSP func_sp = std::make_shared<Function>( 313 &comp_unit, pdb_func.getSymIndexId(), func_type_uid, mangled, func_type, 314 func_addr, AddressRanges{AddressRange(func_addr, func_length)}); 315 316 comp_unit.AddFunction(func_sp); 317 318 LanguageType lang = ParseLanguage(comp_unit); 319 auto type_system_or_err = GetTypeSystemForLanguage(lang); 320 if (auto err = type_system_or_err.takeError()) { 321 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 322 "Unable to parse PDBFunc: {0}"); 323 return nullptr; 324 } 325 326 auto ts = *type_system_or_err; 327 TypeSystemClang *clang_type_system = 328 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 329 if (!clang_type_system) 330 return nullptr; 331 clang_type_system->GetPDBParser()->GetDeclForSymbol(pdb_func); 332 333 return func_sp.get(); 334 } 335 336 size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) { 337 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 338 size_t func_added = 0; 339 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 340 if (!compiland_up) 341 return 0; 342 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 343 if (!results_up) 344 return 0; 345 while (auto pdb_func_up = results_up->getNext()) { 346 auto func_sp = comp_unit.FindFunctionByUID(pdb_func_up->getSymIndexId()); 347 if (!func_sp) { 348 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, comp_unit)) 349 ++func_added; 350 } 351 } 352 return func_added; 353 } 354 355 bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) { 356 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 357 if (comp_unit.GetLineTable()) 358 return true; 359 return ParseCompileUnitLineTable(comp_unit, 0); 360 } 361 362 bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) { 363 // PDB doesn't contain information about macros 364 return false; 365 } 366 367 bool SymbolFilePDB::ParseSupportFiles( 368 CompileUnit &comp_unit, lldb_private::SupportFileList &support_files) { 369 370 // In theory this is unnecessary work for us, because all of this information 371 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 372 // second time seems like a waste. Unfortunately, there's no good way around 373 // this short of a moderate refactor since SymbolVendor depends on being able 374 // to cache this list. 375 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 376 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 377 if (!compiland_up) 378 return false; 379 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 380 if (!files || files->getChildCount() == 0) 381 return false; 382 383 while (auto file = files->getNext()) { 384 FileSpec spec(file->getFileName(), FileSpec::Style::windows); 385 support_files.AppendIfUnique(spec); 386 } 387 388 return true; 389 } 390 391 bool SymbolFilePDB::ParseImportedModules( 392 const lldb_private::SymbolContext &sc, 393 std::vector<SourceModule> &imported_modules) { 394 // PDB does not yet support module debug info 395 return false; 396 } 397 398 static size_t ParseFunctionBlocksForPDBSymbol( 399 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol, 400 lldb_private::Block *parent_block, bool is_top_parent) { 401 assert(pdb_symbol && parent_block); 402 403 size_t num_added = 0; 404 405 if (!is_top_parent) { 406 // Ranges for the top block were parsed together with the function. 407 if (pdb_symbol->getSymTag() != PDB_SymType::Block) 408 return num_added; 409 410 auto &raw_sym = pdb_symbol->getRawSymbol(); 411 assert(llvm::isa<PDBSymbolBlock>(pdb_symbol)); 412 auto uid = pdb_symbol->getSymIndexId(); 413 if (parent_block->FindBlockByID(uid)) 414 return num_added; 415 if (raw_sym.getVirtualAddress() < func_file_vm_addr) 416 return num_added; 417 418 Block *block = parent_block->CreateChild(pdb_symbol->getSymIndexId()).get(); 419 block->AddRange(Block::Range( 420 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); 421 block->FinalizeRanges(); 422 } 423 auto results_up = pdb_symbol->findAllChildren(); 424 if (!results_up) 425 return num_added; 426 427 while (auto symbol_up = results_up->getNext()) { 428 num_added += ParseFunctionBlocksForPDBSymbol( 429 func_file_vm_addr, symbol_up.get(), parent_block, false); 430 } 431 return num_added; 432 } 433 434 size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) { 435 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 436 size_t num_added = 0; 437 auto uid = func.GetID(); 438 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 439 if (!pdb_func_up) 440 return 0; 441 Block &parent_block = func.GetBlock(false); 442 num_added = ParseFunctionBlocksForPDBSymbol( 443 pdb_func_up->getVirtualAddress(), pdb_func_up.get(), &parent_block, true); 444 return num_added; 445 } 446 447 size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) { 448 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 449 450 size_t num_added = 0; 451 auto compiland = GetPDBCompilandByUID(comp_unit.GetID()); 452 if (!compiland) 453 return 0; 454 455 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 456 std::unique_ptr<IPDBEnumSymbols> results; 457 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 458 PDB_SymType::UDT}; 459 for (auto tag : tags_to_search) { 460 results = raw_sym.findAllChildren(tag); 461 if (!results || results->getChildCount() == 0) 462 continue; 463 while (auto symbol = results->getNext()) { 464 switch (symbol->getSymTag()) { 465 case PDB_SymType::Enum: 466 case PDB_SymType::UDT: 467 case PDB_SymType::Typedef: 468 break; 469 default: 470 continue; 471 } 472 473 // This should cause the type to get cached and stored in the `m_types` 474 // lookup. 475 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { 476 // Resolve the type completely to avoid a completion 477 // (and so a list change, which causes an iterators invalidation) 478 // during a TypeList dumping 479 type->GetFullCompilerType(); 480 ++num_added; 481 } 482 } 483 } 484 }; 485 486 ParseTypesByTagFn(*compiland); 487 488 // Also parse global types particularly coming from this compiland. 489 // Unfortunately, PDB has no compiland information for each global type. We 490 // have to parse them all. But ensure we only do this once. 491 static bool parse_all_global_types = false; 492 if (!parse_all_global_types) { 493 ParseTypesByTagFn(*m_global_scope_up); 494 parse_all_global_types = true; 495 } 496 return num_added; 497 } 498 499 size_t 500 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 501 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 502 if (!sc.comp_unit) 503 return 0; 504 505 size_t num_added = 0; 506 if (sc.function) { 507 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( 508 sc.function->GetID()); 509 if (!pdb_func) 510 return 0; 511 512 num_added += ParseVariables(sc, *pdb_func); 513 sc.function->GetBlock(false).SetDidParseVariables(true, true); 514 } else if (sc.comp_unit) { 515 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 516 if (!compiland) 517 return 0; 518 519 if (sc.comp_unit->GetVariableList(false)) 520 return 0; 521 522 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 523 if (results && results->getChildCount()) { 524 while (auto result = results->getNext()) { 525 auto cu_id = GetCompilandId(*result); 526 // FIXME: We are not able to determine variable's compile unit. 527 if (cu_id == 0) 528 continue; 529 530 if (cu_id == sc.comp_unit->GetID()) 531 num_added += ParseVariables(sc, *result); 532 } 533 } 534 535 // FIXME: A `file static` or `global constant` variable appears both in 536 // compiland's children and global scope's children with unexpectedly 537 // different symbol's Id making it ambiguous. 538 539 // FIXME: 'local constant', for example, const char var[] = "abc", declared 540 // in a function scope, can't be found in PDB. 541 542 // Parse variables in this compiland. 543 num_added += ParseVariables(sc, *compiland); 544 } 545 546 return num_added; 547 } 548 549 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 550 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 551 auto find_result = m_types.find(type_uid); 552 if (find_result != m_types.end()) 553 return find_result->second.get(); 554 555 auto type_system_or_err = 556 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 557 if (auto err = type_system_or_err.takeError()) { 558 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 559 "Unable to ResolveTypeUID: {0}"); 560 return nullptr; 561 } 562 563 auto ts = *type_system_or_err; 564 TypeSystemClang *clang_type_system = 565 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 566 if (!clang_type_system) 567 return nullptr; 568 PDBASTParser *pdb = clang_type_system->GetPDBParser(); 569 if (!pdb) 570 return nullptr; 571 572 auto pdb_type = m_session_up->getSymbolById(type_uid); 573 if (pdb_type == nullptr) 574 return nullptr; 575 576 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 577 if (result) { 578 m_types.insert(std::make_pair(type_uid, result)); 579 } 580 return result.get(); 581 } 582 583 std::optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID( 584 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 585 return std::nullopt; 586 } 587 588 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 589 std::lock_guard<std::recursive_mutex> guard( 590 GetObjectFile()->GetModule()->GetMutex()); 591 592 auto type_system_or_err = 593 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 594 if (auto err = type_system_or_err.takeError()) { 595 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 596 "Unable to get dynamic array info for UID: {0}"); 597 return false; 598 } 599 auto ts = *type_system_or_err; 600 TypeSystemClang *clang_ast_ctx = 601 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 602 603 if (!clang_ast_ctx) 604 return false; 605 606 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 607 if (!pdb) 608 return false; 609 610 return pdb->CompleteTypeFromPDB(compiler_type); 611 } 612 613 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 614 auto type_system_or_err = 615 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 616 if (auto err = type_system_or_err.takeError()) { 617 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 618 "Unable to get decl for UID: {0}"); 619 return CompilerDecl(); 620 } 621 auto ts = *type_system_or_err; 622 TypeSystemClang *clang_ast_ctx = 623 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 624 if (!clang_ast_ctx) 625 return CompilerDecl(); 626 627 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 628 if (!pdb) 629 return CompilerDecl(); 630 631 auto symbol = m_session_up->getSymbolById(uid); 632 if (!symbol) 633 return CompilerDecl(); 634 635 auto decl = pdb->GetDeclForSymbol(*symbol); 636 if (!decl) 637 return CompilerDecl(); 638 639 return clang_ast_ctx->GetCompilerDecl(decl); 640 } 641 642 lldb_private::CompilerDeclContext 643 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 644 auto type_system_or_err = 645 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 646 if (auto err = type_system_or_err.takeError()) { 647 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 648 "Unable to get DeclContext for UID: {0}"); 649 return CompilerDeclContext(); 650 } 651 652 auto ts = *type_system_or_err; 653 TypeSystemClang *clang_ast_ctx = 654 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 655 if (!clang_ast_ctx) 656 return CompilerDeclContext(); 657 658 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 659 if (!pdb) 660 return CompilerDeclContext(); 661 662 auto symbol = m_session_up->getSymbolById(uid); 663 if (!symbol) 664 return CompilerDeclContext(); 665 666 auto decl_context = pdb->GetDeclContextForSymbol(*symbol); 667 if (!decl_context) 668 return GetDeclContextContainingUID(uid); 669 670 return clang_ast_ctx->CreateDeclContext(decl_context); 671 } 672 673 lldb_private::CompilerDeclContext 674 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 675 auto type_system_or_err = 676 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 677 if (auto err = type_system_or_err.takeError()) { 678 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 679 "Unable to get DeclContext containing UID: {0}"); 680 return CompilerDeclContext(); 681 } 682 683 auto ts = *type_system_or_err; 684 TypeSystemClang *clang_ast_ctx = 685 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 686 if (!clang_ast_ctx) 687 return CompilerDeclContext(); 688 689 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 690 if (!pdb) 691 return CompilerDeclContext(); 692 693 auto symbol = m_session_up->getSymbolById(uid); 694 if (!symbol) 695 return CompilerDeclContext(); 696 697 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol); 698 assert(decl_context); 699 700 return clang_ast_ctx->CreateDeclContext(decl_context); 701 } 702 703 void SymbolFilePDB::ParseDeclsForContext( 704 lldb_private::CompilerDeclContext decl_ctx) { 705 auto type_system_or_err = 706 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 707 if (auto err = type_system_or_err.takeError()) { 708 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 709 "Unable to parse decls for context: {0}"); 710 return; 711 } 712 713 auto ts = *type_system_or_err; 714 TypeSystemClang *clang_ast_ctx = 715 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 716 if (!clang_ast_ctx) 717 return; 718 719 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); 720 if (!pdb) 721 return; 722 723 pdb->ParseDeclsForDeclContext( 724 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext())); 725 } 726 727 uint32_t 728 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 729 SymbolContextItem resolve_scope, 730 lldb_private::SymbolContext &sc) { 731 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 732 uint32_t resolved_flags = 0; 733 if (resolve_scope & eSymbolContextCompUnit || 734 resolve_scope & eSymbolContextVariable || 735 resolve_scope & eSymbolContextFunction || 736 resolve_scope & eSymbolContextBlock || 737 resolve_scope & eSymbolContextLineEntry) { 738 auto cu_sp = GetCompileUnitContainsAddress(so_addr); 739 if (!cu_sp) { 740 if (resolved_flags & eSymbolContextVariable) { 741 // TODO: Resolve variables 742 } 743 return 0; 744 } 745 sc.comp_unit = cu_sp.get(); 746 resolved_flags |= eSymbolContextCompUnit; 747 lldbassert(sc.module_sp == cu_sp->GetModule()); 748 } 749 750 if (resolve_scope & eSymbolContextFunction || 751 resolve_scope & eSymbolContextBlock) { 752 addr_t file_vm_addr = so_addr.GetFileAddress(); 753 auto symbol_up = 754 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); 755 if (symbol_up) { 756 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 757 assert(pdb_func); 758 auto func_uid = pdb_func->getSymIndexId(); 759 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 760 if (sc.function == nullptr) 761 sc.function = 762 ParseCompileUnitFunctionForPDBFunc(*pdb_func, *sc.comp_unit); 763 if (sc.function) { 764 resolved_flags |= eSymbolContextFunction; 765 if (resolve_scope & eSymbolContextBlock) { 766 auto block_symbol = m_session_up->findSymbolByAddress( 767 file_vm_addr, PDB_SymType::Block); 768 auto block_id = block_symbol ? block_symbol->getSymIndexId() 769 : sc.function->GetID(); 770 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id); 771 if (sc.block) 772 resolved_flags |= eSymbolContextBlock; 773 } 774 } 775 } 776 } 777 778 if (resolve_scope & eSymbolContextLineEntry) { 779 if (auto *line_table = sc.comp_unit->GetLineTable()) { 780 Address addr(so_addr); 781 if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 782 resolved_flags |= eSymbolContextLineEntry; 783 } 784 } 785 786 return resolved_flags; 787 } 788 789 uint32_t SymbolFilePDB::ResolveSymbolContext( 790 const lldb_private::SourceLocationSpec &src_location_spec, 791 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) { 792 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 793 const size_t old_size = sc_list.GetSize(); 794 const FileSpec &file_spec = src_location_spec.GetFileSpec(); 795 const uint32_t line = src_location_spec.GetLine().value_or(0); 796 if (resolve_scope & lldb::eSymbolContextCompUnit) { 797 // Locate all compilation units with line numbers referencing the specified 798 // file. For example, if `file_spec` is <vector>, then this should return 799 // all source files and header files that reference <vector>, either 800 // directly or indirectly. 801 auto compilands = m_session_up->findCompilandsForSourceFile( 802 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 803 804 if (!compilands) 805 return 0; 806 807 // For each one, either find its previously parsed data or parse it afresh 808 // and add it to the symbol context list. 809 while (auto compiland = compilands->getNext()) { 810 // If we're not checking inlines, then don't add line information for 811 // this file unless the FileSpec matches. For inline functions, we don't 812 // have to match the FileSpec since they could be defined in headers 813 // other than file specified in FileSpec. 814 if (!src_location_spec.GetCheckInlines()) { 815 std::string source_file = compiland->getSourceFileFullPath(); 816 if (source_file.empty()) 817 continue; 818 FileSpec this_spec(source_file, FileSpec::Style::windows); 819 bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 820 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 821 continue; 822 } 823 824 SymbolContext sc; 825 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 826 if (!cu) 827 continue; 828 sc.comp_unit = cu.get(); 829 sc.module_sp = cu->GetModule(); 830 831 // If we were asked to resolve line entries, add all entries to the line 832 // table that match the requested line (or all lines if `line` == 0). 833 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 834 eSymbolContextLineEntry)) { 835 bool has_line_table = ParseCompileUnitLineTable(*sc.comp_unit, line); 836 837 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 838 // The query asks for line entries, but we can't get them for the 839 // compile unit. This is not normal for `line` = 0. So just assert 840 // it. 841 assert(line && "Couldn't get all line entries!\n"); 842 843 // Current compiland does not have the requested line. Search next. 844 continue; 845 } 846 847 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 848 if (!has_line_table) 849 continue; 850 851 auto *line_table = sc.comp_unit->GetLineTable(); 852 lldbassert(line_table); 853 854 uint32_t num_line_entries = line_table->GetSize(); 855 // Skip the terminal line entry. 856 --num_line_entries; 857 858 // If `line `!= 0, see if we can resolve function for each line entry 859 // in the line table. 860 for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 861 ++line_idx) { 862 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 863 continue; 864 865 auto file_vm_addr = 866 sc.line_entry.range.GetBaseAddress().GetFileAddress(); 867 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 868 continue; 869 870 auto symbol_up = m_session_up->findSymbolByAddress( 871 file_vm_addr, PDB_SymType::Function); 872 if (symbol_up) { 873 auto func_uid = symbol_up->getSymIndexId(); 874 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 875 if (sc.function == nullptr) { 876 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 877 assert(pdb_func); 878 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, 879 *sc.comp_unit); 880 } 881 if (sc.function && (resolve_scope & eSymbolContextBlock)) { 882 Block &block = sc.function->GetBlock(true); 883 sc.block = block.FindBlockByID(sc.function->GetID()); 884 } 885 } 886 sc_list.Append(sc); 887 } 888 } else if (has_line_table) { 889 // We can parse line table for the compile unit. But no query to 890 // resolve function or block. We append `sc` to the list anyway. 891 sc_list.Append(sc); 892 } 893 } else { 894 // No query for line entry, function or block. But we have a valid 895 // compile unit, append `sc` to the list. 896 sc_list.Append(sc); 897 } 898 } 899 } 900 return sc_list.GetSize() - old_size; 901 } 902 903 std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { 904 // Cache public names at first 905 if (m_public_names.empty()) 906 if (auto result_up = 907 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol)) 908 while (auto symbol_up = result_up->getNext()) 909 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress()) 910 m_public_names[addr] = symbol_up->getRawSymbol().getName(); 911 912 // Look up the name in the cache 913 return m_public_names.lookup(pdb_data.getVirtualAddress()); 914 } 915 916 VariableSP SymbolFilePDB::ParseVariableForPDBData( 917 const lldb_private::SymbolContext &sc, 918 const llvm::pdb::PDBSymbolData &pdb_data) { 919 VariableSP var_sp; 920 uint32_t var_uid = pdb_data.getSymIndexId(); 921 auto result = m_variables.find(var_uid); 922 if (result != m_variables.end()) 923 return result->second; 924 925 ValueType scope = eValueTypeInvalid; 926 bool is_static_member = false; 927 bool is_external = false; 928 bool is_artificial = false; 929 930 switch (pdb_data.getDataKind()) { 931 case PDB_DataKind::Global: 932 scope = eValueTypeVariableGlobal; 933 is_external = true; 934 break; 935 case PDB_DataKind::Local: 936 scope = eValueTypeVariableLocal; 937 break; 938 case PDB_DataKind::FileStatic: 939 scope = eValueTypeVariableStatic; 940 break; 941 case PDB_DataKind::StaticMember: 942 is_static_member = true; 943 scope = eValueTypeVariableStatic; 944 break; 945 case PDB_DataKind::Member: 946 scope = eValueTypeVariableStatic; 947 break; 948 case PDB_DataKind::Param: 949 scope = eValueTypeVariableArgument; 950 break; 951 case PDB_DataKind::Constant: 952 scope = eValueTypeConstResult; 953 break; 954 default: 955 break; 956 } 957 958 switch (pdb_data.getLocationType()) { 959 case PDB_LocType::TLS: 960 scope = eValueTypeVariableThreadLocal; 961 break; 962 case PDB_LocType::RegRel: { 963 // It is a `this` pointer. 964 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { 965 scope = eValueTypeVariableArgument; 966 is_artificial = true; 967 } 968 } break; 969 default: 970 break; 971 } 972 973 Declaration decl; 974 if (!is_artificial && !pdb_data.isCompilerGenerated()) { 975 if (auto lines = pdb_data.getLineNumbers()) { 976 if (auto first_line = lines->getNext()) { 977 uint32_t src_file_id = first_line->getSourceFileId(); 978 auto src_file = m_session_up->getSourceFileById(src_file_id); 979 if (src_file) { 980 FileSpec spec(src_file->getFileName()); 981 decl.SetFile(spec); 982 decl.SetColumn(first_line->getColumnNumber()); 983 decl.SetLine(first_line->getLineNumber()); 984 } 985 } 986 } 987 } 988 989 Variable::RangeList ranges; 990 SymbolContextScope *context_scope = sc.comp_unit; 991 if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) { 992 if (sc.function) { 993 Block &function_block = sc.function->GetBlock(true); 994 Block *block = 995 function_block.FindBlockByID(pdb_data.getLexicalParentId()); 996 if (!block) 997 block = &function_block; 998 999 context_scope = block; 1000 1001 for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges; 1002 ++i) { 1003 AddressRange range; 1004 if (!block->GetRangeAtIndex(i, range)) 1005 continue; 1006 1007 ranges.Append(range.GetBaseAddress().GetFileAddress(), 1008 range.GetByteSize()); 1009 } 1010 } 1011 } 1012 1013 SymbolFileTypeSP type_sp = 1014 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); 1015 1016 auto var_name = pdb_data.getName(); 1017 auto mangled = GetMangledForPDBData(pdb_data); 1018 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); 1019 1020 bool is_constant; 1021 ModuleSP module_sp = GetObjectFile()->GetModule(); 1022 DWARFExpressionList location(module_sp, 1023 ConvertPDBLocationToDWARFExpression( 1024 module_sp, pdb_data, ranges, is_constant), 1025 nullptr); 1026 1027 var_sp = std::make_shared<Variable>( 1028 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, 1029 ranges, &decl, location, is_external, is_artificial, is_constant, 1030 is_static_member); 1031 1032 m_variables.insert(std::make_pair(var_uid, var_sp)); 1033 return var_sp; 1034 } 1035 1036 size_t 1037 SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, 1038 const llvm::pdb::PDBSymbol &pdb_symbol, 1039 lldb_private::VariableList *variable_list) { 1040 size_t num_added = 0; 1041 1042 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { 1043 VariableListSP local_variable_list_sp; 1044 1045 auto result = m_variables.find(pdb_data->getSymIndexId()); 1046 if (result != m_variables.end()) { 1047 if (variable_list) 1048 variable_list->AddVariableIfUnique(result->second); 1049 } else { 1050 // Prepare right VariableList for this variable. 1051 if (auto lexical_parent = pdb_data->getLexicalParent()) { 1052 switch (lexical_parent->getSymTag()) { 1053 case PDB_SymType::Exe: 1054 assert(sc.comp_unit); 1055 [[fallthrough]]; 1056 case PDB_SymType::Compiland: { 1057 if (sc.comp_unit) { 1058 local_variable_list_sp = sc.comp_unit->GetVariableList(false); 1059 if (!local_variable_list_sp) { 1060 local_variable_list_sp = std::make_shared<VariableList>(); 1061 sc.comp_unit->SetVariableList(local_variable_list_sp); 1062 } 1063 } 1064 } break; 1065 case PDB_SymType::Block: 1066 case PDB_SymType::Function: { 1067 if (sc.function) { 1068 Block *block = sc.function->GetBlock(true).FindBlockByID( 1069 lexical_parent->getSymIndexId()); 1070 if (block) { 1071 local_variable_list_sp = block->GetBlockVariableList(false); 1072 if (!local_variable_list_sp) { 1073 local_variable_list_sp = std::make_shared<VariableList>(); 1074 block->SetVariableList(local_variable_list_sp); 1075 } 1076 } 1077 } 1078 } break; 1079 default: 1080 break; 1081 } 1082 } 1083 1084 if (local_variable_list_sp) { 1085 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { 1086 local_variable_list_sp->AddVariableIfUnique(var_sp); 1087 if (variable_list) 1088 variable_list->AddVariableIfUnique(var_sp); 1089 ++num_added; 1090 PDBASTParser *ast = GetPDBAstParser(); 1091 if (ast) 1092 ast->GetDeclForSymbol(*pdb_data); 1093 } 1094 } 1095 } 1096 } 1097 1098 if (auto results = pdb_symbol.findAllChildren()) { 1099 while (auto result = results->getNext()) 1100 num_added += ParseVariables(sc, *result, variable_list); 1101 } 1102 1103 return num_added; 1104 } 1105 1106 void SymbolFilePDB::FindGlobalVariables( 1107 lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx, 1108 uint32_t max_matches, lldb_private::VariableList &variables) { 1109 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1110 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1111 return; 1112 if (name.IsEmpty()) 1113 return; 1114 1115 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1116 if (!results) 1117 return; 1118 1119 uint32_t matches = 0; 1120 size_t old_size = variables.GetSize(); 1121 while (auto result = results->getNext()) { 1122 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get()); 1123 if (max_matches > 0 && matches >= max_matches) 1124 break; 1125 1126 SymbolContext sc; 1127 sc.module_sp = m_objfile_sp->GetModule(); 1128 lldbassert(sc.module_sp.get()); 1129 1130 if (name.GetStringRef() != 1131 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())) 1132 continue; 1133 1134 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1135 // FIXME: We are not able to determine the compile unit. 1136 if (sc.comp_unit == nullptr) 1137 continue; 1138 1139 if (parent_decl_ctx.IsValid() && 1140 GetDeclContextContainingUID(result->getSymIndexId()) != parent_decl_ctx) 1141 continue; 1142 1143 ParseVariables(sc, *pdb_data, &variables); 1144 matches = variables.GetSize() - old_size; 1145 } 1146 } 1147 1148 void SymbolFilePDB::FindGlobalVariables( 1149 const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1150 lldb_private::VariableList &variables) { 1151 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1152 if (!regex.IsValid()) 1153 return; 1154 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); 1155 if (!results) 1156 return; 1157 1158 uint32_t matches = 0; 1159 size_t old_size = variables.GetSize(); 1160 while (auto pdb_data = results->getNext()) { 1161 if (max_matches > 0 && matches >= max_matches) 1162 break; 1163 1164 auto var_name = pdb_data->getName(); 1165 if (var_name.empty()) 1166 continue; 1167 if (!regex.Execute(var_name)) 1168 continue; 1169 SymbolContext sc; 1170 sc.module_sp = m_objfile_sp->GetModule(); 1171 lldbassert(sc.module_sp.get()); 1172 1173 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); 1174 // FIXME: We are not able to determine the compile unit. 1175 if (sc.comp_unit == nullptr) 1176 continue; 1177 1178 ParseVariables(sc, *pdb_data, &variables); 1179 matches = variables.GetSize() - old_size; 1180 } 1181 } 1182 1183 bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 1184 bool include_inlines, 1185 lldb_private::SymbolContextList &sc_list) { 1186 lldb_private::SymbolContext sc; 1187 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 1188 if (!sc.comp_unit) 1189 return false; 1190 sc.module_sp = sc.comp_unit->GetModule(); 1191 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, *sc.comp_unit); 1192 if (!sc.function) 1193 return false; 1194 1195 sc_list.Append(sc); 1196 return true; 1197 } 1198 1199 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 1200 lldb_private::SymbolContextList &sc_list) { 1201 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 1202 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 1203 return false; 1204 return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 1205 } 1206 1207 void SymbolFilePDB::CacheFunctionNames() { 1208 if (!m_func_full_names.IsEmpty()) 1209 return; 1210 1211 std::map<uint64_t, uint32_t> addr_ids; 1212 1213 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 1214 while (auto pdb_func_up = results_up->getNext()) { 1215 if (pdb_func_up->isCompilerGenerated()) 1216 continue; 1217 1218 auto name = pdb_func_up->getName(); 1219 auto demangled_name = pdb_func_up->getUndecoratedName(); 1220 if (name.empty() && demangled_name.empty()) 1221 continue; 1222 1223 auto uid = pdb_func_up->getSymIndexId(); 1224 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 1225 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 1226 1227 if (auto parent = pdb_func_up->getClassParent()) { 1228 1229 // PDB have symbols for class/struct methods or static methods in Enum 1230 // Class. We won't bother to check if the parent is UDT or Enum here. 1231 m_func_method_names.Append(ConstString(name), uid); 1232 1233 // To search a method name, like NS::Class:MemberFunc, LLDB searches 1234 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does 1235 // not have information of this, we extract base names and cache them 1236 // by our own effort. 1237 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1238 if (!basename.empty()) 1239 m_func_base_names.Append(ConstString(basename), uid); 1240 else { 1241 m_func_base_names.Append(ConstString(name), uid); 1242 } 1243 1244 if (!demangled_name.empty()) 1245 m_func_full_names.Append(ConstString(demangled_name), uid); 1246 1247 } else { 1248 // Handle not-method symbols. 1249 1250 // The function name might contain namespace, or its lexical scope. 1251 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); 1252 if (!basename.empty()) 1253 m_func_base_names.Append(ConstString(basename), uid); 1254 else 1255 m_func_base_names.Append(ConstString(name), uid); 1256 1257 if (name == "main") { 1258 m_func_full_names.Append(ConstString(name), uid); 1259 1260 if (!demangled_name.empty() && name != demangled_name) { 1261 m_func_full_names.Append(ConstString(demangled_name), uid); 1262 m_func_base_names.Append(ConstString(demangled_name), uid); 1263 } 1264 } else if (!demangled_name.empty()) { 1265 m_func_full_names.Append(ConstString(demangled_name), uid); 1266 } else { 1267 m_func_full_names.Append(ConstString(name), uid); 1268 } 1269 } 1270 } 1271 } 1272 1273 if (auto results_up = 1274 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 1275 while (auto pub_sym_up = results_up->getNext()) { 1276 if (!pub_sym_up->isFunction()) 1277 continue; 1278 auto name = pub_sym_up->getName(); 1279 if (name.empty()) 1280 continue; 1281 1282 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 1283 // PDB public symbol has mangled name for its associated function. 1284 if (auto vm_addr = pub_sym_up->getVirtualAddress()) { 1285 if (auto it = addr_ids.find(vm_addr); it != addr_ids.end()) 1286 // Cache mangled name. 1287 m_func_full_names.Append(ConstString(name), it->second); 1288 } 1289 } 1290 } 1291 } 1292 // Sort them before value searching is working properly 1293 m_func_full_names.Sort(); 1294 m_func_full_names.SizeToFit(); 1295 m_func_method_names.Sort(); 1296 m_func_method_names.SizeToFit(); 1297 m_func_base_names.Sort(); 1298 m_func_base_names.SizeToFit(); 1299 } 1300 1301 void SymbolFilePDB::FindFunctions( 1302 const lldb_private::Module::LookupInfo &lookup_info, 1303 const lldb_private::CompilerDeclContext &parent_decl_ctx, 1304 bool include_inlines, 1305 lldb_private::SymbolContextList &sc_list) { 1306 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1307 ConstString name = lookup_info.GetLookupName(); 1308 FunctionNameType name_type_mask = lookup_info.GetNameTypeMask(); 1309 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 1310 1311 if (name_type_mask & eFunctionNameTypeFull) 1312 name = lookup_info.GetName(); 1313 1314 if (name_type_mask == eFunctionNameTypeNone) 1315 return; 1316 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1317 return; 1318 if (name.IsEmpty()) 1319 return; 1320 1321 if (name_type_mask & eFunctionNameTypeFull || 1322 name_type_mask & eFunctionNameTypeBase || 1323 name_type_mask & eFunctionNameTypeMethod) { 1324 CacheFunctionNames(); 1325 1326 std::set<uint32_t> resolved_ids; 1327 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list, 1328 &resolved_ids](UniqueCStringMap<uint32_t> &Names) { 1329 std::vector<uint32_t> ids; 1330 if (!Names.GetValues(name, ids)) 1331 return; 1332 1333 for (uint32_t id : ids) { 1334 if (resolved_ids.find(id) != resolved_ids.end()) 1335 continue; 1336 1337 if (parent_decl_ctx.IsValid() && 1338 GetDeclContextContainingUID(id) != parent_decl_ctx) 1339 continue; 1340 1341 if (ResolveFunction(id, include_inlines, sc_list)) 1342 resolved_ids.insert(id); 1343 } 1344 }; 1345 if (name_type_mask & eFunctionNameTypeFull) { 1346 ResolveFn(m_func_full_names); 1347 ResolveFn(m_func_base_names); 1348 ResolveFn(m_func_method_names); 1349 } 1350 if (name_type_mask & eFunctionNameTypeBase) 1351 ResolveFn(m_func_base_names); 1352 if (name_type_mask & eFunctionNameTypeMethod) 1353 ResolveFn(m_func_method_names); 1354 } 1355 } 1356 1357 void SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 1358 bool include_inlines, 1359 lldb_private::SymbolContextList &sc_list) { 1360 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1361 if (!regex.IsValid()) 1362 return; 1363 1364 CacheFunctionNames(); 1365 1366 std::set<uint32_t> resolved_ids; 1367 auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, 1368 this](UniqueCStringMap<uint32_t> &Names) { 1369 std::vector<uint32_t> ids; 1370 if (Names.GetValues(regex, ids)) { 1371 for (auto id : ids) { 1372 if (resolved_ids.find(id) == resolved_ids.end()) 1373 if (ResolveFunction(id, include_inlines, sc_list)) 1374 resolved_ids.insert(id); 1375 } 1376 } 1377 }; 1378 ResolveFn(m_func_full_names); 1379 ResolveFn(m_func_base_names); 1380 } 1381 1382 void SymbolFilePDB::GetMangledNamesForFunction( 1383 const std::string &scope_qualified_name, 1384 std::vector<lldb_private::ConstString> &mangled_names) {} 1385 1386 void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) { 1387 std::set<lldb::addr_t> sym_addresses; 1388 for (size_t i = 0; i < symtab.GetNumSymbols(); i++) 1389 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress()); 1390 1391 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>(); 1392 if (!results) 1393 return; 1394 1395 auto section_list = m_objfile_sp->GetSectionList(); 1396 if (!section_list) 1397 return; 1398 1399 while (auto pub_symbol = results->getNext()) { 1400 auto section_id = pub_symbol->getAddressSection(); 1401 1402 auto section = section_list->FindSectionByID(section_id); 1403 if (!section) 1404 continue; 1405 1406 auto offset = pub_symbol->getAddressOffset(); 1407 1408 auto file_addr = section->GetFileAddress() + offset; 1409 if (sym_addresses.find(file_addr) != sym_addresses.end()) 1410 continue; 1411 sym_addresses.insert(file_addr); 1412 1413 auto size = pub_symbol->getLength(); 1414 symtab.AddSymbol( 1415 Symbol(pub_symbol->getSymIndexId(), // symID 1416 pub_symbol->getName().c_str(), // name 1417 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type 1418 true, // external 1419 false, // is_debug 1420 false, // is_trampoline 1421 false, // is_artificial 1422 section, // section_sp 1423 offset, // value 1424 size, // size 1425 size != 0, // size_is_valid 1426 false, // contains_linker_annotations 1427 0 // flags 1428 )); 1429 } 1430 1431 symtab.Finalize(); 1432 } 1433 1434 void SymbolFilePDB::DumpClangAST(Stream &s) { 1435 auto type_system_or_err = 1436 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1437 if (auto err = type_system_or_err.takeError()) { 1438 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 1439 "Unable to dump ClangAST: {0}"); 1440 return; 1441 } 1442 1443 auto ts = *type_system_or_err; 1444 TypeSystemClang *clang_type_system = 1445 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 1446 if (!clang_type_system) 1447 return; 1448 clang_type_system->Dump(s.AsRawOstream()); 1449 } 1450 1451 void SymbolFilePDB::FindTypesByRegex( 1452 const lldb_private::RegularExpression ®ex, uint32_t max_matches, 1453 lldb_private::TypeMap &types) { 1454 // When searching by regex, we need to go out of our way to limit the search 1455 // space as much as possible since this searches EVERYTHING in the PDB, 1456 // manually doing regex comparisons. PDB library isn't optimized for regex 1457 // searches or searches across multiple symbol types at the same time, so the 1458 // best we can do is to search enums, then typedefs, then classes one by one, 1459 // and do a regex comparison against each of them. 1460 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 1461 PDB_SymType::UDT}; 1462 std::unique_ptr<IPDBEnumSymbols> results; 1463 1464 uint32_t matches = 0; 1465 1466 for (auto tag : tags_to_search) { 1467 results = m_global_scope_up->findAllChildren(tag); 1468 if (!results) 1469 continue; 1470 1471 while (auto result = results->getNext()) { 1472 if (max_matches > 0 && matches >= max_matches) 1473 break; 1474 1475 std::string type_name; 1476 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 1477 type_name = enum_type->getName(); 1478 else if (auto typedef_type = 1479 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 1480 type_name = typedef_type->getName(); 1481 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 1482 type_name = class_type->getName(); 1483 else { 1484 // We're looking only for types that have names. Skip symbols, as well 1485 // as unnamed types such as arrays, pointers, etc. 1486 continue; 1487 } 1488 1489 if (!regex.Execute(type_name)) 1490 continue; 1491 1492 // This should cause the type to get cached and stored in the `m_types` 1493 // lookup. 1494 if (!ResolveTypeUID(result->getSymIndexId())) 1495 continue; 1496 1497 auto iter = m_types.find(result->getSymIndexId()); 1498 if (iter == m_types.end()) 1499 continue; 1500 types.Insert(iter->second); 1501 ++matches; 1502 } 1503 } 1504 } 1505 1506 void SymbolFilePDB::FindTypes(const lldb_private::TypeQuery &query, 1507 lldb_private::TypeResults &type_results) { 1508 1509 // Make sure we haven't already searched this SymbolFile before. 1510 if (type_results.AlreadySearched(this)) 1511 return; 1512 1513 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1514 1515 std::unique_ptr<IPDBEnumSymbols> results; 1516 llvm::StringRef basename = query.GetTypeBasename().GetStringRef(); 1517 if (basename.empty()) 1518 return; 1519 results = m_global_scope_up->findAllChildren(PDB_SymType::None); 1520 if (!results) 1521 return; 1522 1523 while (auto result = results->getNext()) { 1524 1525 switch (result->getSymTag()) { 1526 case PDB_SymType::Enum: 1527 case PDB_SymType::UDT: 1528 case PDB_SymType::Typedef: 1529 break; 1530 default: 1531 // We're looking only for types that have names. Skip symbols, as well 1532 // as unnamed types such as arrays, pointers, etc. 1533 continue; 1534 } 1535 1536 if (MSVCUndecoratedNameParser::DropScope( 1537 result->getRawSymbol().getName()) != basename) 1538 continue; 1539 1540 // This should cause the type to get cached and stored in the `m_types` 1541 // lookup. 1542 if (!ResolveTypeUID(result->getSymIndexId())) 1543 continue; 1544 1545 auto iter = m_types.find(result->getSymIndexId()); 1546 if (iter == m_types.end()) 1547 continue; 1548 // We resolved a type. Get the fully qualified name to ensure it matches. 1549 ConstString name = iter->second->GetQualifiedName(); 1550 TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match); 1551 if (query.ContextMatches(type_match.GetContextRef())) { 1552 type_results.InsertUnique(iter->second); 1553 if (type_results.Done(query)) 1554 return; 1555 } 1556 } 1557 } 1558 1559 void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, 1560 uint32_t type_mask, 1561 TypeCollection &type_collection) { 1562 bool can_parse = false; 1563 switch (pdb_symbol.getSymTag()) { 1564 case PDB_SymType::ArrayType: 1565 can_parse = ((type_mask & eTypeClassArray) != 0); 1566 break; 1567 case PDB_SymType::BuiltinType: 1568 can_parse = ((type_mask & eTypeClassBuiltin) != 0); 1569 break; 1570 case PDB_SymType::Enum: 1571 can_parse = ((type_mask & eTypeClassEnumeration) != 0); 1572 break; 1573 case PDB_SymType::Function: 1574 case PDB_SymType::FunctionSig: 1575 can_parse = ((type_mask & eTypeClassFunction) != 0); 1576 break; 1577 case PDB_SymType::PointerType: 1578 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | 1579 eTypeClassMemberPointer)) != 0); 1580 break; 1581 case PDB_SymType::Typedef: 1582 can_parse = ((type_mask & eTypeClassTypedef) != 0); 1583 break; 1584 case PDB_SymType::UDT: { 1585 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); 1586 assert(udt); 1587 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && 1588 ((type_mask & (eTypeClassClass | eTypeClassStruct | 1589 eTypeClassUnion)) != 0)); 1590 } break; 1591 default: 1592 break; 1593 } 1594 1595 if (can_parse) { 1596 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { 1597 if (!llvm::is_contained(type_collection, type)) 1598 type_collection.push_back(type); 1599 } 1600 } 1601 1602 auto results_up = pdb_symbol.findAllChildren(); 1603 while (auto symbol_up = results_up->getNext()) 1604 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); 1605 } 1606 1607 void SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1608 TypeClass type_mask, 1609 lldb_private::TypeList &type_list) { 1610 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1611 TypeCollection type_collection; 1612 CompileUnit *cu = 1613 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 1614 if (cu) { 1615 auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1616 if (!compiland_up) 1617 return; 1618 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1619 } else { 1620 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 1621 auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1622 if (cu_sp) { 1623 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1624 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1625 } 1626 } 1627 } 1628 1629 for (auto type : type_collection) { 1630 type->GetForwardCompilerType(); 1631 type_list.Insert(type->shared_from_this()); 1632 } 1633 } 1634 1635 llvm::Expected<lldb::TypeSystemSP> 1636 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1637 auto type_system_or_err = 1638 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language); 1639 if (type_system_or_err) { 1640 if (auto ts = *type_system_or_err) 1641 ts->SetSymbolFile(this); 1642 } 1643 return type_system_or_err; 1644 } 1645 1646 PDBASTParser *SymbolFilePDB::GetPDBAstParser() { 1647 auto type_system_or_err = 1648 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1649 if (auto err = type_system_or_err.takeError()) { 1650 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 1651 "Unable to get PDB AST parser: {0}"); 1652 return nullptr; 1653 } 1654 1655 auto ts = *type_system_or_err; 1656 auto *clang_type_system = 1657 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 1658 if (!clang_type_system) 1659 return nullptr; 1660 1661 return clang_type_system->GetPDBParser(); 1662 } 1663 1664 lldb_private::CompilerDeclContext 1665 SymbolFilePDB::FindNamespace(lldb_private::ConstString name, 1666 const CompilerDeclContext &parent_decl_ctx, bool) { 1667 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1668 auto type_system_or_err = 1669 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 1670 if (auto err = type_system_or_err.takeError()) { 1671 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err), 1672 "Unable to find namespace {1}: {0}", name.AsCString()); 1673 return CompilerDeclContext(); 1674 } 1675 auto ts = *type_system_or_err; 1676 auto *clang_type_system = 1677 llvm::dyn_cast_or_null<TypeSystemClang>(ts.get()); 1678 if (!clang_type_system) 1679 return CompilerDeclContext(); 1680 1681 PDBASTParser *pdb = clang_type_system->GetPDBParser(); 1682 if (!pdb) 1683 return CompilerDeclContext(); 1684 1685 clang::DeclContext *decl_context = nullptr; 1686 if (parent_decl_ctx) 1687 decl_context = static_cast<clang::DeclContext *>( 1688 parent_decl_ctx.GetOpaqueDeclContext()); 1689 1690 auto namespace_decl = 1691 pdb->FindNamespaceDecl(decl_context, name.GetStringRef()); 1692 if (!namespace_decl) 1693 return CompilerDeclContext(); 1694 1695 return clang_type_system->CreateDeclContext(namespace_decl); 1696 } 1697 1698 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1699 1700 const IPDBSession &SymbolFilePDB::GetPDBSession() const { 1701 return *m_session_up; 1702 } 1703 1704 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, 1705 uint32_t index) { 1706 auto found_cu = m_comp_units.find(id); 1707 if (found_cu != m_comp_units.end()) 1708 return found_cu->second; 1709 1710 auto compiland_up = GetPDBCompilandByUID(id); 1711 if (!compiland_up) 1712 return CompUnitSP(); 1713 1714 lldb::LanguageType lang; 1715 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 1716 if (!details) 1717 lang = lldb::eLanguageTypeC_plus_plus; 1718 else 1719 lang = TranslateLanguage(details->getLanguage()); 1720 1721 if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1722 return CompUnitSP(); 1723 1724 std::string path = compiland_up->getSourceFileFullPath(); 1725 if (path.empty()) 1726 return CompUnitSP(); 1727 1728 // Don't support optimized code for now, DebugInfoPDB does not return this 1729 // information. 1730 LazyBool optimized = eLazyBoolNo; 1731 auto cu_sp = std::make_shared<CompileUnit>(m_objfile_sp->GetModule(), nullptr, 1732 path.c_str(), id, lang, optimized); 1733 1734 if (!cu_sp) 1735 return CompUnitSP(); 1736 1737 m_comp_units.insert(std::make_pair(id, cu_sp)); 1738 if (index == UINT32_MAX) 1739 GetCompileUnitIndex(*compiland_up, index); 1740 lldbassert(index != UINT32_MAX); 1741 SetCompileUnitAtIndex(index, cu_sp); 1742 return cu_sp; 1743 } 1744 1745 bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit, 1746 uint32_t match_line) { 1747 auto compiland_up = GetPDBCompilandByUID(comp_unit.GetID()); 1748 if (!compiland_up) 1749 return false; 1750 1751 // LineEntry needs the *index* of the file into the list of support files 1752 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 1753 // a globally unique idenfitifier in the namespace of the PDB. So, we have 1754 // to do a mapping so that we can hand out indices. 1755 llvm::DenseMap<uint32_t, uint32_t> index_map; 1756 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 1757 auto line_table = std::make_unique<LineTable>(&comp_unit); 1758 1759 // Find contributions to `compiland` from all source and header files. 1760 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 1761 if (!files) 1762 return false; 1763 1764 // For each source and header file, create a LineSequence for contributions 1765 // to the compiland from that file, and add the sequence. 1766 while (auto file = files->getNext()) { 1767 std::unique_ptr<LineSequence> sequence( 1768 line_table->CreateLineSequenceContainer()); 1769 auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 1770 if (!lines) 1771 continue; 1772 int entry_count = lines->getChildCount(); 1773 1774 uint64_t prev_addr; 1775 uint32_t prev_length; 1776 uint32_t prev_line; 1777 uint32_t prev_source_idx; 1778 1779 for (int i = 0; i < entry_count; ++i) { 1780 auto line = lines->getChildAtIndex(i); 1781 1782 uint64_t lno = line->getLineNumber(); 1783 uint64_t addr = line->getVirtualAddress(); 1784 uint32_t length = line->getLength(); 1785 uint32_t source_id = line->getSourceFileId(); 1786 uint32_t col = line->getColumnNumber(); 1787 uint32_t source_idx = index_map[source_id]; 1788 1789 // There was a gap between the current entry and the previous entry if 1790 // the addresses don't perfectly line up. 1791 bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 1792 1793 // Before inserting the current entry, insert a terminal entry at the end 1794 // of the previous entry's address range if the current entry resulted in 1795 // a gap from the previous entry. 1796 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1797 line_table->AppendLineEntryToSequence( 1798 sequence.get(), prev_addr + prev_length, prev_line, 0, 1799 prev_source_idx, false, false, false, false, true); 1800 1801 line_table->InsertSequence(sequence.get()); 1802 sequence = line_table->CreateLineSequenceContainer(); 1803 } 1804 1805 if (ShouldAddLine(match_line, lno, length)) { 1806 bool is_statement = line->isStatement(); 1807 bool is_prologue = false; 1808 bool is_epilogue = false; 1809 auto func = 1810 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1811 if (func) { 1812 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 1813 if (prologue) 1814 is_prologue = (addr == prologue->getVirtualAddress()); 1815 1816 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 1817 if (epilogue) 1818 is_epilogue = (addr == epilogue->getVirtualAddress()); 1819 } 1820 1821 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1822 source_idx, is_statement, false, 1823 is_prologue, is_epilogue, false); 1824 } 1825 1826 prev_addr = addr; 1827 prev_length = length; 1828 prev_line = lno; 1829 prev_source_idx = source_idx; 1830 } 1831 1832 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 1833 // The end is always a terminal entry, so insert it regardless. 1834 line_table->AppendLineEntryToSequence( 1835 sequence.get(), prev_addr + prev_length, prev_line, 0, 1836 prev_source_idx, false, false, false, false, true); 1837 } 1838 1839 line_table->InsertSequence(sequence.get()); 1840 } 1841 1842 if (line_table->GetSize()) { 1843 comp_unit.SetLineTable(line_table.release()); 1844 return true; 1845 } 1846 return false; 1847 } 1848 1849 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 1850 const PDBSymbolCompiland &compiland, 1851 llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 1852 // This is a hack, but we need to convert the source id into an index into 1853 // the support files array. We don't want to do path comparisons to avoid 1854 // basename / full path issues that may or may not even be a problem, so we 1855 // use the globally unique source file identifiers. Ideally we could use the 1856 // global identifiers everywhere, but LineEntry currently assumes indices. 1857 auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 1858 if (!source_files) 1859 return; 1860 1861 int index = 0; 1862 while (auto file = source_files->getNext()) { 1863 uint32_t source_id = file->getUniqueId(); 1864 index_map[source_id] = index++; 1865 } 1866 } 1867 1868 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 1869 const lldb_private::Address &so_addr) { 1870 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1871 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 1872 return nullptr; 1873 1874 // If it is a PDB function's vm addr, this is the first sure bet. 1875 if (auto lines = 1876 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { 1877 if (auto first_line = lines->getNext()) 1878 return ParseCompileUnitForUID(first_line->getCompilandId()); 1879 } 1880 1881 // Otherwise we resort to section contributions. 1882 if (auto sec_contribs = m_session_up->getSectionContribs()) { 1883 while (auto section = sec_contribs->getNext()) { 1884 auto va = section->getVirtualAddress(); 1885 if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) 1886 return ParseCompileUnitForUID(section->getCompilandId()); 1887 } 1888 } 1889 return nullptr; 1890 } 1891 1892 Mangled 1893 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 1894 Mangled mangled; 1895 auto func_name = pdb_func.getName(); 1896 auto func_undecorated_name = pdb_func.getUndecoratedName(); 1897 std::string func_decorated_name; 1898 1899 // Seek from public symbols for non-static function's decorated name if any. 1900 // For static functions, they don't have undecorated names and aren't exposed 1901 // in Public Symbols either. 1902 if (!func_undecorated_name.empty()) { 1903 auto result_up = m_global_scope_up->findChildren( 1904 PDB_SymType::PublicSymbol, func_undecorated_name, 1905 PDB_NameSearchFlags::NS_UndecoratedName); 1906 if (result_up) { 1907 while (auto symbol_up = result_up->getNext()) { 1908 // For a public symbol, it is unique. 1909 lldbassert(result_up->getChildCount() == 1); 1910 if (auto *pdb_public_sym = 1911 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( 1912 symbol_up.get())) { 1913 if (pdb_public_sym->isFunction()) { 1914 func_decorated_name = pdb_public_sym->getName(); 1915 break; 1916 } 1917 } 1918 } 1919 } 1920 } 1921 if (!func_decorated_name.empty()) { 1922 mangled.SetMangledName(ConstString(func_decorated_name)); 1923 1924 // For MSVC, format of C function's decorated name depends on calling 1925 // convention. Unfortunately none of the format is recognized by current 1926 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 1927 // `__purecall` is retrieved as both its decorated and undecorated name 1928 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` 1929 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). 1930 // Mangled::GetDemangledName method will fail internally and caches an 1931 // empty string as its undecorated name. So we will face a contradiction 1932 // here for the same symbol: 1933 // non-empty undecorated name from PDB 1934 // empty undecorated name from LLDB 1935 if (!func_undecorated_name.empty() && mangled.GetDemangledName().IsEmpty()) 1936 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1937 1938 // LLDB uses several flags to control how a C++ decorated name is 1939 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the 1940 // yielded name could be different from what we retrieve from 1941 // PDB source unless we also apply same flags in getting undecorated 1942 // name through PDBSymbolFunc::getUndecoratedNameEx method. 1943 if (!func_undecorated_name.empty() && 1944 mangled.GetDemangledName() != ConstString(func_undecorated_name)) 1945 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1946 } else if (!func_undecorated_name.empty()) { 1947 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1948 } else if (!func_name.empty()) 1949 mangled.SetValue(ConstString(func_name)); 1950 1951 return mangled; 1952 } 1953 1954 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 1955 const lldb_private::CompilerDeclContext &decl_ctx) { 1956 if (!decl_ctx.IsValid()) 1957 return true; 1958 1959 TypeSystem *decl_ctx_type_system = decl_ctx.GetTypeSystem(); 1960 if (!decl_ctx_type_system) 1961 return false; 1962 auto type_system_or_err = GetTypeSystemForLanguage( 1963 decl_ctx_type_system->GetMinimumLanguage(nullptr)); 1964 if (auto err = type_system_or_err.takeError()) { 1965 LLDB_LOG_ERROR( 1966 GetLog(LLDBLog::Symbols), std::move(err), 1967 "Unable to determine if DeclContext matches this symbol file: {0}"); 1968 return false; 1969 } 1970 1971 if (decl_ctx_type_system == type_system_or_err->get()) 1972 return true; // The type systems match, return true 1973 1974 return false; 1975 } 1976 1977 uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) { 1978 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) { 1979 return lhs < rhs.Offset; 1980 }; 1981 1982 // Cache section contributions 1983 if (m_sec_contribs.empty()) { 1984 if (auto SecContribs = m_session_up->getSectionContribs()) { 1985 while (auto SectionContrib = SecContribs->getNext()) { 1986 auto comp_id = SectionContrib->getCompilandId(); 1987 if (!comp_id) 1988 continue; 1989 1990 auto sec = SectionContrib->getAddressSection(); 1991 auto &sec_cs = m_sec_contribs[sec]; 1992 1993 auto offset = SectionContrib->getAddressOffset(); 1994 auto it = llvm::upper_bound(sec_cs, offset, pred_upper); 1995 1996 auto size = SectionContrib->getLength(); 1997 sec_cs.insert(it, {offset, size, comp_id}); 1998 } 1999 } 2000 } 2001 2002 // Check by line number 2003 if (auto Lines = data.getLineNumbers()) { 2004 if (auto FirstLine = Lines->getNext()) 2005 return FirstLine->getCompilandId(); 2006 } 2007 2008 // Retrieve section + offset 2009 uint32_t DataSection = data.getAddressSection(); 2010 uint32_t DataOffset = data.getAddressOffset(); 2011 if (DataSection == 0) { 2012 if (auto RVA = data.getRelativeVirtualAddress()) 2013 m_session_up->addressForRVA(RVA, DataSection, DataOffset); 2014 } 2015 2016 if (DataSection) { 2017 // Search by section contributions 2018 auto &sec_cs = m_sec_contribs[DataSection]; 2019 auto it = llvm::upper_bound(sec_cs, DataOffset, pred_upper); 2020 if (it != sec_cs.begin()) { 2021 --it; 2022 if (DataOffset < it->Offset + it->Size) 2023 return it->CompilandId; 2024 } 2025 } else { 2026 // Search in lexical tree 2027 auto LexParentId = data.getLexicalParentId(); 2028 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) { 2029 if (LexParent->getSymTag() == PDB_SymType::Exe) 2030 break; 2031 if (LexParent->getSymTag() == PDB_SymType::Compiland) 2032 return LexParentId; 2033 LexParentId = LexParent->getRawSymbol().getLexicalParentId(); 2034 } 2035 } 2036 2037 return 0; 2038 } 2039