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