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 "clang/Lex/Lexer.h" 13 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/PluginManager.h" 16 #include "lldb/Symbol/ClangASTContext.h" 17 #include "lldb/Symbol/CompileUnit.h" 18 #include "lldb/Symbol/LineTable.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 #include "lldb/Symbol/SymbolContext.h" 21 #include "lldb/Symbol/SymbolVendor.h" 22 #include "lldb/Symbol/TypeMap.h" 23 #include "lldb/Symbol/TypeList.h" 24 #include "lldb/Utility/RegularExpression.h" 25 26 #include "llvm/DebugInfo/PDB/GenericError.h" 27 #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 28 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 29 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 30 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 31 #include "llvm/DebugInfo/PDB/IPDBTable.h" 32 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 33 #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" 34 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 35 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 36 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 37 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 38 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 39 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 40 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 41 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" 42 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 43 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 44 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 45 46 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 47 #include "Plugins/SymbolFile/PDB/PDBASTParser.h" 48 49 #include <regex> 50 51 using namespace lldb; 52 using namespace lldb_private; 53 using namespace llvm::pdb; 54 55 namespace { 56 lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 57 switch (lang) { 58 case PDB_Lang::Cpp: 59 return lldb::LanguageType::eLanguageTypeC_plus_plus; 60 case PDB_Lang::C: 61 return lldb::LanguageType::eLanguageTypeC; 62 default: 63 return lldb::LanguageType::eLanguageTypeUnknown; 64 } 65 } 66 67 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 68 uint32_t addr_length) { 69 return ((requested_line == 0 || actual_line == requested_line) && 70 addr_length > 0); 71 } 72 } 73 74 void SymbolFilePDB::Initialize() { 75 PluginManager::RegisterPlugin(GetPluginNameStatic(), 76 GetPluginDescriptionStatic(), CreateInstance, 77 DebuggerInitialize); 78 } 79 80 void SymbolFilePDB::Terminate() { 81 PluginManager::UnregisterPlugin(CreateInstance); 82 } 83 84 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 85 86 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 87 static ConstString g_name("pdb"); 88 return g_name; 89 } 90 91 const char *SymbolFilePDB::GetPluginDescriptionStatic() { 92 return "Microsoft PDB debug symbol file reader."; 93 } 94 95 lldb_private::SymbolFile * 96 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { 97 return new SymbolFilePDB(obj_file); 98 } 99 100 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 101 : SymbolFile(object_file), m_session_up(), m_global_scope_up(), 102 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} 103 104 SymbolFilePDB::~SymbolFilePDB() {} 105 106 uint32_t SymbolFilePDB::CalculateAbilities() { 107 uint32_t abilities = 0; 108 if (!m_obj_file) 109 return 0; 110 111 if (!m_session_up) { 112 // Lazily load and match the PDB file, but only do this once. 113 std::string exePath = m_obj_file->GetFileSpec().GetPath(); 114 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 115 m_session_up); 116 if (error) { 117 llvm::consumeError(std::move(error)); 118 auto module_sp = m_obj_file->GetModule(); 119 if (!module_sp) 120 return 0; 121 // See if any symbol file is specified through `--symfile` option. 122 FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 123 if (!symfile) 124 return 0; 125 error = loadDataForPDB(PDB_ReaderType::DIA, 126 llvm::StringRef(symfile.GetPath()), 127 m_session_up); 128 if (error) { 129 llvm::consumeError(std::move(error)); 130 return 0; 131 } 132 } 133 } 134 if (!m_session_up.get()) 135 return 0; 136 137 auto enum_tables_up = m_session_up->getEnumTables(); 138 if (!enum_tables_up) 139 return 0; 140 while (auto table_up = enum_tables_up->getNext()) { 141 if (table_up->getItemCount() == 0) 142 continue; 143 auto type = table_up->getTableType(); 144 switch (type) { 145 case PDB_TableType::Symbols: 146 // This table represents a store of symbols with types listed in 147 // PDBSym_Type 148 abilities |= (CompileUnits | Functions | Blocks | 149 GlobalVariables | LocalVariables | VariableTypes); 150 break; 151 case PDB_TableType::LineNumbers: 152 abilities |= LineTables; 153 break; 154 default: break; 155 } 156 } 157 return abilities; 158 } 159 160 void SymbolFilePDB::InitializeObject() { 161 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); 162 lldbassert(obj_load_address && 163 obj_load_address != LLDB_INVALID_ADDRESS); 164 m_session_up->setLoadAddress(obj_load_address); 165 if (!m_global_scope_up) 166 m_global_scope_up = m_session_up->getGlobalScope(); 167 lldbassert(m_global_scope_up.get()); 168 169 TypeSystem *type_system = 170 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 171 ClangASTContext *clang_type_system = 172 llvm::dyn_cast_or_null<ClangASTContext>(type_system); 173 lldbassert(clang_type_system); 174 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( 175 type_system, clang_type_system->GetTranslationUnitDecl()); 176 } 177 178 uint32_t SymbolFilePDB::GetNumCompileUnits() { 179 if (m_cached_compile_unit_count == 0) { 180 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 181 if (!compilands) 182 return 0; 183 184 // The linker could link *.dll (compiland language = LINK), or import 185 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` 186 // could be found as a child of the global scope (PDB executable). 187 // Usually, such compilands contain `thunk` symbols in which we are not 188 // interested for now. However we still count them in the compiland list. 189 // If we perform any compiland related activity, like finding symbols 190 // through llvm::pdb::IPDBSession methods, such compilands will all be 191 // searched automatically no matter whether we include them or not. 192 m_cached_compile_unit_count = compilands->getChildCount(); 193 194 // The linker can inject an additional "dummy" compilation unit into the 195 // PDB. Ignore this special compile unit for our purposes, if it is there. 196 // It is always the last one. 197 auto last_compiland_up = 198 compilands->getChildAtIndex(m_cached_compile_unit_count - 1); 199 lldbassert(last_compiland_up.get()); 200 std::string name = last_compiland_up->getName(); 201 if (name == "* Linker *") 202 --m_cached_compile_unit_count; 203 } 204 return m_cached_compile_unit_count; 205 } 206 207 void SymbolFilePDB::GetCompileUnitIndex( 208 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, 209 uint32_t &index) { 210 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 211 if (!results_up) 212 return; 213 auto uid = pdb_compiland.getSymIndexId(); 214 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 215 auto compiland_up = results_up->getChildAtIndex(cu_idx); 216 if (!compiland_up) 217 continue; 218 if (compiland_up->getSymIndexId() == uid) { 219 index = cu_idx; 220 return; 221 } 222 } 223 index = UINT32_MAX; 224 return; 225 } 226 227 std::unique_ptr<llvm::pdb::PDBSymbolCompiland> 228 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { 229 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); 230 } 231 232 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 233 if (index >= GetNumCompileUnits()) 234 return CompUnitSP(); 235 236 // Assuming we always retrieve same compilands listed in same order through 237 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a 238 // compile unit makes no sense. 239 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); 240 if (!results) 241 return CompUnitSP(); 242 auto compiland_up = results->getChildAtIndex(index); 243 if (!compiland_up) 244 return CompUnitSP(); 245 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); 246 } 247 248 lldb::LanguageType 249 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { 250 // What fields should I expect to be filled out on the SymbolContext? Is it 251 // safe to assume that `sc.comp_unit` is valid? 252 if (!sc.comp_unit) 253 return lldb::eLanguageTypeUnknown; 254 255 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 256 if (!compiland_up) 257 return lldb::eLanguageTypeUnknown; 258 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 259 if (!details) 260 return lldb::eLanguageTypeUnknown; 261 return TranslateLanguage(details->getLanguage()); 262 } 263 264 lldb_private::Function * 265 SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc( 266 const PDBSymbolFunc &pdb_func, 267 const lldb_private::SymbolContext &sc) { 268 lldbassert(sc.comp_unit && sc.module_sp.get()); 269 270 auto file_vm_addr = pdb_func.getVirtualAddress(); 271 if (file_vm_addr == LLDB_INVALID_ADDRESS) 272 return nullptr; 273 274 auto func_length = pdb_func.getLength(); 275 AddressRange func_range = AddressRange(file_vm_addr, 276 func_length, 277 sc.module_sp->GetSectionList()); 278 if (!func_range.GetBaseAddress().IsValid()) 279 return nullptr; 280 281 lldb_private::Type* func_type = ResolveTypeUID(pdb_func.getSymIndexId()); 282 if (!func_type) 283 return nullptr; 284 285 user_id_t func_type_uid = pdb_func.getSignatureId(); 286 287 Mangled mangled = GetMangledForPDBFunc(pdb_func); 288 289 FunctionSP func_sp = std::make_shared<Function>(sc.comp_unit, 290 pdb_func.getSymIndexId(), 291 func_type_uid, 292 mangled, 293 func_type, 294 func_range); 295 296 sc.comp_unit->AddFunction(func_sp); 297 return func_sp.get(); 298 } 299 300 size_t SymbolFilePDB::ParseCompileUnitFunctions( 301 const lldb_private::SymbolContext &sc) { 302 lldbassert(sc.comp_unit); 303 size_t func_added = 0; 304 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 305 if (!compiland_up) 306 return 0; 307 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); 308 if (!results_up) 309 return 0; 310 while (auto pdb_func_up = results_up->getNext()) { 311 auto func_sp = 312 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId()); 313 if (!func_sp) { 314 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc)) 315 ++func_added; 316 } 317 } 318 return func_added; 319 } 320 321 bool SymbolFilePDB::ParseCompileUnitLineTable( 322 const lldb_private::SymbolContext &sc) { 323 lldbassert(sc.comp_unit); 324 if (sc.comp_unit->GetLineTable()) 325 return true; 326 return ParseCompileUnitLineTable(sc, 0); 327 } 328 329 bool SymbolFilePDB::ParseCompileUnitDebugMacros( 330 const lldb_private::SymbolContext &sc) { 331 // PDB doesn't contain information about macros 332 return false; 333 } 334 335 bool SymbolFilePDB::ParseCompileUnitSupportFiles( 336 const lldb_private::SymbolContext &sc, 337 lldb_private::FileSpecList &support_files) { 338 lldbassert(sc.comp_unit); 339 340 // In theory this is unnecessary work for us, because all of this information 341 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 342 // second time seems like a waste. Unfortunately, there's no good way around 343 // this short of a moderate refactor since SymbolVendor depends on being able 344 // to cache this list. 345 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 346 if (!compiland_up) 347 return false; 348 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 349 if (!files || files->getChildCount() == 0) 350 return false; 351 352 while (auto file = files->getNext()) { 353 FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows); 354 support_files.AppendIfUnique(spec); 355 } 356 return true; 357 } 358 359 bool SymbolFilePDB::ParseImportedModules( 360 const lldb_private::SymbolContext &sc, 361 std::vector<lldb_private::ConstString> &imported_modules) { 362 // PDB does not yet support module debug info 363 return false; 364 } 365 366 static size_t 367 ParseFunctionBlocksForPDBSymbol(const lldb_private::SymbolContext &sc, 368 uint64_t func_file_vm_addr, 369 const llvm::pdb::PDBSymbol *pdb_symbol, 370 lldb_private::Block *parent_block, 371 bool is_top_parent) { 372 assert(pdb_symbol && parent_block); 373 374 size_t num_added = 0; 375 switch (pdb_symbol->getSymTag()) { 376 case PDB_SymType::Block: 377 case PDB_SymType::Function: { 378 Block *block = nullptr; 379 auto &raw_sym = pdb_symbol->getRawSymbol(); 380 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { 381 if (pdb_func->hasNoInlineAttribute()) 382 break; 383 if (is_top_parent) 384 block = parent_block; 385 else 386 break; 387 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { 388 auto uid = pdb_symbol->getSymIndexId(); 389 if (parent_block->FindBlockByID(uid)) 390 break; 391 if (raw_sym.getVirtualAddress() < func_file_vm_addr) 392 break; 393 394 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); 395 parent_block->AddChild(block_sp); 396 block = block_sp.get(); 397 } else 398 llvm_unreachable("Unexpected PDB symbol!"); 399 400 block->AddRange( 401 Block::Range(raw_sym.getVirtualAddress() - func_file_vm_addr, 402 raw_sym.getLength())); 403 block->FinalizeRanges(); 404 ++num_added; 405 406 auto results_up = pdb_symbol->findAllChildren(); 407 if (!results_up) 408 break; 409 while (auto symbol_up = results_up->getNext()) { 410 num_added += ParseFunctionBlocksForPDBSymbol(sc, func_file_vm_addr, 411 symbol_up.get(), 412 block, false); 413 } 414 } break; 415 default: break; 416 } 417 return num_added; 418 } 419 420 size_t 421 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { 422 lldbassert(sc.comp_unit && sc.function); 423 size_t num_added = 0; 424 auto uid = sc.function->GetID(); 425 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 426 if (!pdb_func_up) 427 return 0; 428 Block &parent_block = sc.function->GetBlock(false); 429 num_added = 430 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(), 431 pdb_func_up.get(), &parent_block, true); 432 return num_added; 433 } 434 435 size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { 436 lldbassert(sc.module_sp.get()); 437 if (!sc.comp_unit) 438 return 0; 439 440 size_t num_added = 0; 441 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); 442 if (!compiland) 443 return 0; 444 445 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { 446 std::unique_ptr<IPDBEnumSymbols> results; 447 PDB_SymType tags_to_search[] = { PDB_SymType::Enum, PDB_SymType::Typedef, 448 PDB_SymType::UDT }; 449 for (auto tag : tags_to_search) { 450 results = raw_sym.findAllChildren(tag); 451 if (!results || results->getChildCount() == 0) 452 continue; 453 while (auto symbol = results->getNext()) { 454 switch (symbol->getSymTag()) { 455 case PDB_SymType::Enum: 456 case PDB_SymType::UDT: 457 case PDB_SymType::Typedef: 458 break; 459 default: 460 continue; 461 } 462 463 // This should cause the type to get cached and stored in the `m_types` 464 // lookup. 465 if (!ResolveTypeUID(symbol->getSymIndexId())) 466 continue; 467 468 ++num_added; 469 } 470 } 471 }; 472 473 if (sc.function) { 474 auto pdb_func = 475 m_session_up->getConcreteSymbolById<PDBSymbolFunc>(sc.function->GetID()); 476 if (!pdb_func) 477 return 0; 478 ParseTypesByTagFn(*pdb_func); 479 } else { 480 ParseTypesByTagFn(*compiland); 481 482 // Also parse global types particularly coming from this compiland. 483 // Unfortunately, PDB has no compiland information for each global type. 484 // We have to parse them all. But ensure we only do this once. 485 static bool parse_all_global_types = false; 486 if (!parse_all_global_types) { 487 ParseTypesByTagFn(*m_global_scope_up); 488 parse_all_global_types = true; 489 } 490 } 491 return num_added; 492 } 493 494 size_t 495 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 496 // TODO: Implement this 497 return size_t(); 498 } 499 500 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 501 auto find_result = m_types.find(type_uid); 502 if (find_result != m_types.end()) 503 return find_result->second.get(); 504 505 TypeSystem *type_system = 506 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 507 ClangASTContext *clang_type_system = 508 llvm::dyn_cast_or_null<ClangASTContext>(type_system); 509 if (!clang_type_system) 510 return nullptr; 511 PDBASTParser *pdb = 512 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); 513 if (!pdb) 514 return nullptr; 515 516 auto pdb_type = m_session_up->getSymbolById(type_uid); 517 if (pdb_type == nullptr) 518 return nullptr; 519 520 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 521 if (result.get()) { 522 m_types.insert(std::make_pair(type_uid, result)); 523 auto type_list = GetTypeList(); 524 if (type_list) 525 type_list->Insert(result); 526 } 527 return result.get(); 528 } 529 530 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 531 // TODO: Implement this 532 return false; 533 } 534 535 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 536 return lldb_private::CompilerDecl(); 537 } 538 539 lldb_private::CompilerDeclContext 540 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 541 // PDB always uses the translation unit decl context for everything. We can 542 // improve this later but it's not easy because PDB doesn't provide a high 543 // enough level of type fidelity in this area. 544 return *m_tu_decl_ctx_up; 545 } 546 547 lldb_private::CompilerDeclContext 548 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 549 return *m_tu_decl_ctx_up; 550 } 551 552 void SymbolFilePDB::ParseDeclsForContext( 553 lldb_private::CompilerDeclContext decl_ctx) {} 554 555 uint32_t 556 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 557 uint32_t resolve_scope, 558 lldb_private::SymbolContext &sc) { 559 uint32_t resolved_flags = 0; 560 if (resolve_scope & eSymbolContextCompUnit || 561 resolve_scope & eSymbolContextVariable || 562 resolve_scope & eSymbolContextFunction || 563 resolve_scope & eSymbolContextBlock || 564 resolve_scope & eSymbolContextLineEntry) { 565 addr_t file_vm_addr = so_addr.GetFileAddress(); 566 auto symbol_up = 567 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None); 568 if (!symbol_up) 569 return 0; 570 571 auto cu_sp = GetCompileUnitContainsAddress(so_addr); 572 if (!cu_sp) { 573 if (resolved_flags | eSymbolContextVariable) { 574 // TODO: Resolve variables 575 } 576 return 0; 577 } 578 sc.comp_unit = cu_sp.get(); 579 resolved_flags |= eSymbolContextCompUnit; 580 lldbassert(sc.module_sp == cu_sp->GetModule()); 581 582 switch (symbol_up->getSymTag()) { 583 case PDB_SymType::Function: 584 if (resolve_scope & eSymbolContextFunction) { 585 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 586 assert(pdb_func); 587 auto func_uid = pdb_func->getSymIndexId(); 588 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 589 if (sc.function == nullptr) 590 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); 591 if (sc.function) { 592 resolved_flags |= eSymbolContextFunction; 593 if (resolve_scope & eSymbolContextBlock) { 594 Block &block = sc.function->GetBlock(true); 595 sc.block = block.FindBlockByID(sc.function->GetID()); 596 if (sc.block) 597 resolved_flags |= eSymbolContextBlock; 598 } 599 } 600 } 601 break; 602 default: 603 break; 604 } 605 606 if (resolve_scope & eSymbolContextLineEntry) { 607 if (auto *line_table = sc.comp_unit->GetLineTable()) { 608 Address addr(so_addr); 609 if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 610 resolved_flags |= eSymbolContextLineEntry; 611 } 612 } 613 } 614 return resolved_flags; 615 } 616 617 uint32_t SymbolFilePDB::ResolveSymbolContext( 618 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 619 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) { 620 const size_t old_size = sc_list.GetSize(); 621 if (resolve_scope & lldb::eSymbolContextCompUnit) { 622 // Locate all compilation units with line numbers referencing the specified 623 // file. For example, if `file_spec` is <vector>, then this should return 624 // all source files and header files that reference <vector>, either 625 // directly or indirectly. 626 auto compilands = m_session_up->findCompilandsForSourceFile( 627 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 628 629 if (!compilands) 630 return 0; 631 632 // For each one, either find its previously parsed data or parse it afresh 633 // and add it to the symbol context list. 634 while (auto compiland = compilands->getNext()) { 635 // If we're not checking inlines, then don't add line information for this 636 // file unless the FileSpec matches. 637 // For inline functions, we don't have to match the FileSpec since they 638 // could be defined in headers other than file specified in FileSpec. 639 if (!check_inlines) { 640 std::string source_file = compiland->getSourceFileFullPath(); 641 if (source_file.empty()) 642 continue; 643 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); 644 bool need_full_match = !file_spec.GetDirectory().IsEmpty(); 645 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) 646 continue; 647 } 648 649 SymbolContext sc; 650 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); 651 if (!cu.get()) 652 continue; 653 sc.comp_unit = cu.get(); 654 sc.module_sp = cu->GetModule(); 655 656 // If we were asked to resolve line entries, add all entries to the line 657 // table that match the requested line (or all lines if `line` == 0). 658 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | 659 eSymbolContextLineEntry)) { 660 bool has_line_table = ParseCompileUnitLineTable(sc, line); 661 662 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { 663 // The query asks for line entries, but we can't get them for the 664 // compile unit. This is not normal for `line` = 0. So just assert it. 665 assert(line && "Couldn't get all line entries!\n"); 666 667 // Current compiland does not have the requested line. Search next. 668 continue; 669 } 670 671 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { 672 if (!has_line_table) 673 continue; 674 675 auto *line_table = sc.comp_unit->GetLineTable(); 676 lldbassert(line_table); 677 678 uint32_t num_line_entries = line_table->GetSize(); 679 // Skip the terminal line entry. 680 --num_line_entries; 681 682 // If `line `!= 0, see if we can resolve function for each line 683 // entry in the line table. 684 for (uint32_t line_idx = 0; line && line_idx < num_line_entries; 685 ++line_idx) { 686 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) 687 continue; 688 689 auto file_vm_addr = 690 sc.line_entry.range.GetBaseAddress().GetFileAddress(); 691 if (file_vm_addr == LLDB_INVALID_ADDRESS) 692 continue; 693 694 auto symbol_up = 695 m_session_up->findSymbolByAddress(file_vm_addr, 696 PDB_SymType::Function); 697 if (symbol_up) { 698 auto func_uid = symbol_up->getSymIndexId(); 699 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); 700 if (sc.function == nullptr) { 701 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); 702 assert(pdb_func); 703 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); 704 } 705 if (sc.function && (resolve_scope & eSymbolContextBlock)) { 706 Block &block = sc.function->GetBlock(true); 707 sc.block = block.FindBlockByID(sc.function->GetID()); 708 } 709 } 710 sc_list.Append(sc); 711 } 712 } else if (has_line_table) { 713 // We can parse line table for the compile unit. But no query to 714 // resolve function or block. We append `sc` to the list anyway. 715 sc_list.Append(sc); 716 } 717 } else { 718 // No query for line entry, function or block. But we have a valid 719 // compile unit, append `sc` to the list. 720 sc_list.Append(sc); 721 } 722 } 723 } 724 return sc_list.GetSize() - old_size; 725 } 726 727 uint32_t SymbolFilePDB::FindGlobalVariables( 728 const lldb_private::ConstString &name, 729 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 730 uint32_t max_matches, lldb_private::VariableList &variables) { 731 return uint32_t(); 732 } 733 734 uint32_t 735 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, 736 bool append, uint32_t max_matches, 737 lldb_private::VariableList &variables) { 738 return uint32_t(); 739 } 740 741 bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, 742 bool include_inlines, 743 lldb_private::SymbolContextList &sc_list) { 744 lldb_private::SymbolContext sc; 745 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); 746 if (!sc.comp_unit) 747 return false; 748 sc.module_sp = sc.comp_unit->GetModule(); 749 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); 750 if (!sc.function) 751 return false; 752 753 sc_list.Append(sc); 754 return true; 755 } 756 757 bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, 758 lldb_private::SymbolContextList &sc_list) { 759 auto pdb_func_up = 760 m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); 761 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) 762 return false; 763 return ResolveFunction(*pdb_func_up, include_inlines, sc_list); 764 } 765 766 void SymbolFilePDB::CacheFunctionNames() { 767 if (!m_func_full_names.IsEmpty()) 768 return; 769 770 std::map<uint64_t, uint32_t> addr_ids; 771 772 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { 773 while (auto pdb_func_up = results_up->getNext()) { 774 if (pdb_func_up->isCompilerGenerated()) 775 continue; 776 777 auto name = pdb_func_up->getName(); 778 auto demangled_name = pdb_func_up->getUndecoratedName(); 779 if (name.empty() && demangled_name.empty()) 780 continue; 781 782 auto uid = pdb_func_up->getSymIndexId(); 783 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) 784 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); 785 786 if (auto parent = pdb_func_up->getClassParent()) { 787 788 // PDB have symbols for class/struct methods or static methods in Enum 789 // Class. We won't bother to check if the parent is UDT or Enum here. 790 m_func_method_names.Append(ConstString(name), uid); 791 792 ConstString cstr_name(name); 793 794 // To search a method name, like NS::Class:MemberFunc, LLDB searches its 795 // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not 796 // have inforamtion of this, we extract base names and cache them by our 797 // own effort. 798 llvm::StringRef basename; 799 CPlusPlusLanguage::MethodName cpp_method(cstr_name); 800 if (cpp_method.IsValid()) { 801 llvm::StringRef context; 802 basename = cpp_method.GetBasename(); 803 if (basename.empty()) 804 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(), 805 context, basename); 806 } 807 808 if (!basename.empty()) 809 m_func_base_names.Append(ConstString(basename), uid); 810 else { 811 m_func_base_names.Append(ConstString(name), uid); 812 } 813 814 if (!demangled_name.empty()) 815 m_func_full_names.Append(ConstString(demangled_name), uid); 816 817 } else { 818 // Handle not-method symbols. 819 820 // The function name might contain namespace, or its lexical scope. It 821 // is not safe to get its base name by applying same scheme as we deal 822 // with the method names. 823 // FIXME: Remove namespace if function is static in a scope. 824 m_func_base_names.Append(ConstString(name), uid); 825 826 if (name == "main") { 827 m_func_full_names.Append(ConstString(name), uid); 828 829 if (!demangled_name.empty() && name != demangled_name) { 830 m_func_full_names.Append(ConstString(demangled_name), uid); 831 m_func_base_names.Append(ConstString(demangled_name), uid); 832 } 833 } else if (!demangled_name.empty()) { 834 m_func_full_names.Append(ConstString(demangled_name), uid); 835 } else { 836 m_func_full_names.Append(ConstString(name), uid); 837 } 838 } 839 } 840 } 841 842 if (auto results_up = 843 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { 844 while (auto pub_sym_up = results_up->getNext()) { 845 if (!pub_sym_up->isFunction()) 846 continue; 847 auto name = pub_sym_up->getName(); 848 if (name.empty()) 849 continue; 850 851 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { 852 auto vm_addr = pub_sym_up->getVirtualAddress(); 853 854 // PDB public symbol has mangled name for its associated function. 855 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { 856 // Cache mangled name. 857 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); 858 } 859 } 860 } 861 } 862 // Sort them before value searching is working properly 863 m_func_full_names.Sort(); 864 m_func_full_names.SizeToFit(); 865 m_func_method_names.Sort(); 866 m_func_method_names.SizeToFit(); 867 m_func_base_names.Sort(); 868 m_func_base_names.SizeToFit(); 869 } 870 871 uint32_t SymbolFilePDB::FindFunctions( 872 const lldb_private::ConstString &name, 873 const lldb_private::CompilerDeclContext *parent_decl_ctx, 874 uint32_t name_type_mask, bool include_inlines, bool append, 875 lldb_private::SymbolContextList &sc_list) { 876 if (!append) 877 sc_list.Clear(); 878 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); 879 880 if (name_type_mask == eFunctionNameTypeNone) 881 return 0; 882 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 883 return 0; 884 if (name.IsEmpty()) 885 return 0; 886 887 auto old_size = sc_list.GetSize(); 888 if (name_type_mask & eFunctionNameTypeFull || 889 name_type_mask & eFunctionNameTypeBase || 890 name_type_mask & eFunctionNameTypeMethod) { 891 CacheFunctionNames(); 892 893 std::set<uint32_t> resolved_ids; 894 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, this] ( 895 UniqueCStringMap<uint32_t> &Names) 896 { 897 std::vector<uint32_t> ids; 898 if (Names.GetValues(name, ids)) { 899 for (auto id : ids) { 900 if (resolved_ids.find(id) == resolved_ids.end()) { 901 if (ResolveFunction(id, include_inlines, sc_list)) 902 resolved_ids.insert(id); 903 } 904 } 905 } 906 }; 907 if (name_type_mask & eFunctionNameTypeFull) { 908 ResolveFn(m_func_full_names); 909 } 910 if (name_type_mask & eFunctionNameTypeBase) { 911 ResolveFn(m_func_base_names); 912 } 913 if (name_type_mask & eFunctionNameTypeMethod) { 914 ResolveFn(m_func_method_names); 915 } 916 } 917 return sc_list.GetSize() - old_size; 918 } 919 920 uint32_t 921 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 922 bool include_inlines, bool append, 923 lldb_private::SymbolContextList &sc_list) { 924 if (!append) 925 sc_list.Clear(); 926 if (!regex.IsValid()) 927 return 0; 928 929 auto old_size = sc_list.GetSize(); 930 CacheFunctionNames(); 931 932 std::set<uint32_t> resolved_ids; 933 auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, this] ( 934 UniqueCStringMap<uint32_t> &Names) 935 { 936 std::vector<uint32_t> ids; 937 if (Names.GetValues(regex, ids)) { 938 for (auto id : ids) { 939 if (resolved_ids.find(id) == resolved_ids.end()) 940 if (ResolveFunction(id, include_inlines, sc_list)) 941 resolved_ids.insert(id); 942 } 943 } 944 }; 945 ResolveFn(m_func_full_names); 946 ResolveFn(m_func_base_names); 947 948 return sc_list.GetSize() - old_size; 949 } 950 951 void SymbolFilePDB::GetMangledNamesForFunction( 952 const std::string &scope_qualified_name, 953 std::vector<lldb_private::ConstString> &mangled_names) {} 954 955 uint32_t SymbolFilePDB::FindTypes( 956 const lldb_private::SymbolContext &sc, 957 const lldb_private::ConstString &name, 958 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 959 uint32_t max_matches, 960 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 961 lldb_private::TypeMap &types) { 962 if (!append) 963 types.Clear(); 964 if (!name) 965 return 0; 966 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 967 return 0; 968 969 searched_symbol_files.clear(); 970 searched_symbol_files.insert(this); 971 972 std::string name_str = name.AsCString(); 973 974 // There is an assumption 'name' is not a regex 975 FindTypesByName(name_str, max_matches, types); 976 977 return types.GetSize(); 978 } 979 980 void 981 SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression ®ex, 982 uint32_t max_matches, 983 lldb_private::TypeMap &types) { 984 // When searching by regex, we need to go out of our way to limit the search 985 // space as much as possible since this searches EVERYTHING in the PDB, 986 // manually doing regex comparisons. PDB library isn't optimized for regex 987 // searches or searches across multiple symbol types at the same time, so the 988 // best we can do is to search enums, then typedefs, then classes one by one, 989 // and do a regex comparison against each of them. 990 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 991 PDB_SymType::UDT}; 992 std::unique_ptr<IPDBEnumSymbols> results; 993 994 uint32_t matches = 0; 995 996 for (auto tag : tags_to_search) { 997 results = m_global_scope_up->findAllChildren(tag); 998 if (!results) 999 continue; 1000 1001 while (auto result = results->getNext()) { 1002 if (max_matches > 0 && matches >= max_matches) 1003 break; 1004 1005 std::string type_name; 1006 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 1007 type_name = enum_type->getName(); 1008 else if (auto typedef_type = 1009 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 1010 type_name = typedef_type->getName(); 1011 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 1012 type_name = class_type->getName(); 1013 else { 1014 // We're looking only for types that have names. Skip symbols, as well 1015 // as unnamed types such as arrays, pointers, etc. 1016 continue; 1017 } 1018 1019 if (!regex.Execute(type_name)) 1020 continue; 1021 1022 // This should cause the type to get cached and stored in the `m_types` 1023 // lookup. 1024 if (!ResolveTypeUID(result->getSymIndexId())) 1025 continue; 1026 1027 auto iter = m_types.find(result->getSymIndexId()); 1028 if (iter == m_types.end()) 1029 continue; 1030 types.Insert(iter->second); 1031 ++matches; 1032 } 1033 } 1034 } 1035 1036 void SymbolFilePDB::FindTypesByName(const std::string &name, 1037 uint32_t max_matches, 1038 lldb_private::TypeMap &types) { 1039 std::unique_ptr<IPDBEnumSymbols> results; 1040 if (name.empty()) 1041 return; 1042 results = m_global_scope_up->findChildren(PDB_SymType::None, name, 1043 PDB_NameSearchFlags::NS_Default); 1044 if (!results) 1045 return; 1046 1047 uint32_t matches = 0; 1048 1049 while (auto result = results->getNext()) { 1050 if (max_matches > 0 && matches >= max_matches) 1051 break; 1052 switch (result->getSymTag()) { 1053 case PDB_SymType::Enum: 1054 case PDB_SymType::UDT: 1055 case PDB_SymType::Typedef: 1056 break; 1057 default: 1058 // We're looking only for types that have names. Skip symbols, as well as 1059 // unnamed types such as arrays, pointers, etc. 1060 continue; 1061 } 1062 1063 // This should cause the type to get cached and stored in the `m_types` 1064 // lookup. 1065 if (!ResolveTypeUID(result->getSymIndexId())) 1066 continue; 1067 1068 auto iter = m_types.find(result->getSymIndexId()); 1069 if (iter == m_types.end()) 1070 continue; 1071 types.Insert(iter->second); 1072 ++matches; 1073 } 1074 } 1075 1076 size_t SymbolFilePDB::FindTypes( 1077 const std::vector<lldb_private::CompilerContext> &contexts, bool append, 1078 lldb_private::TypeMap &types) { 1079 return 0; 1080 } 1081 1082 lldb_private::TypeList *SymbolFilePDB::GetTypeList() { 1083 return m_obj_file->GetModule()->GetTypeList(); 1084 } 1085 1086 void 1087 SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, 1088 uint32_t type_mask, 1089 TypeCollection &type_collection) { 1090 bool can_parse = false; 1091 switch (pdb_symbol.getSymTag()) { 1092 case PDB_SymType::ArrayType: 1093 can_parse = ((type_mask & eTypeClassArray) != 0); 1094 break; 1095 case PDB_SymType::BuiltinType: 1096 can_parse = ((type_mask & eTypeClassBuiltin) != 0); 1097 break; 1098 case PDB_SymType::Enum: 1099 can_parse = ((type_mask & eTypeClassEnumeration) != 0); 1100 break; 1101 case PDB_SymType::Function: 1102 case PDB_SymType::FunctionSig: 1103 can_parse = ((type_mask & eTypeClassFunction) != 0); 1104 break; 1105 case PDB_SymType::PointerType: 1106 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | 1107 eTypeClassMemberPointer)) != 0); 1108 break; 1109 case PDB_SymType::Typedef: 1110 can_parse = ((type_mask & eTypeClassTypedef) != 0); 1111 break; 1112 case PDB_SymType::UDT: { 1113 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); 1114 assert(udt); 1115 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && 1116 ((type_mask & (eTypeClassClass | eTypeClassStruct | 1117 eTypeClassUnion)) != 0)); 1118 } break; 1119 default:break; 1120 } 1121 1122 if (can_parse) { 1123 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { 1124 auto result = 1125 std::find(type_collection.begin(), type_collection.end(), type); 1126 if (result == type_collection.end()) 1127 type_collection.push_back(type); 1128 } 1129 } 1130 1131 auto results_up = pdb_symbol.findAllChildren(); 1132 while (auto symbol_up = results_up->getNext()) 1133 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); 1134 } 1135 1136 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1137 uint32_t type_mask, 1138 lldb_private::TypeList &type_list) { 1139 TypeCollection type_collection; 1140 uint32_t old_size = type_list.GetSize(); 1141 CompileUnit *cu = sc_scope ? 1142 sc_scope->CalculateSymbolContextCompileUnit() : nullptr; 1143 if (cu) { 1144 auto compiland_up = GetPDBCompilandByUID(cu->GetID()); 1145 if (!compiland_up) 1146 return 0; 1147 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1148 } else { 1149 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { 1150 auto cu_sp = ParseCompileUnitAtIndex(cu_idx); 1151 if (cu_sp.get()) { 1152 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) 1153 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); 1154 } 1155 } 1156 } 1157 1158 for (auto type : type_collection) { 1159 type->GetForwardCompilerType(); 1160 type_list.Insert(type->shared_from_this()); 1161 } 1162 return type_list.GetSize() - old_size; 1163 } 1164 1165 lldb_private::TypeSystem * 1166 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1167 auto type_system = 1168 m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 1169 if (type_system) 1170 type_system->SetSymbolFile(this); 1171 return type_system; 1172 } 1173 1174 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 1175 const lldb_private::SymbolContext &sc, 1176 const lldb_private::ConstString &name, 1177 const lldb_private::CompilerDeclContext *parent_decl_ctx) { 1178 return lldb_private::CompilerDeclContext(); 1179 } 1180 1181 lldb_private::ConstString SymbolFilePDB::GetPluginName() { 1182 static ConstString g_name("pdb"); 1183 return g_name; 1184 } 1185 1186 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 1187 1188 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 1189 1190 const IPDBSession &SymbolFilePDB::GetPDBSession() const { 1191 return *m_session_up; 1192 } 1193 1194 lldb::CompUnitSP 1195 SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) { 1196 auto found_cu = m_comp_units.find(id); 1197 if (found_cu != m_comp_units.end()) 1198 return found_cu->second; 1199 1200 auto compiland_up = GetPDBCompilandByUID(id); 1201 if (!compiland_up) 1202 return CompUnitSP(); 1203 1204 lldb::LanguageType lang; 1205 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); 1206 if (!details) 1207 lang = lldb::eLanguageTypeC_plus_plus; 1208 else 1209 lang = TranslateLanguage(details->getLanguage()); 1210 1211 if (lang == lldb::LanguageType::eLanguageTypeUnknown) 1212 return CompUnitSP(); 1213 1214 std::string path = compiland_up->getSourceFileFullPath(); 1215 if (path.empty()) 1216 return CompUnitSP(); 1217 1218 // Don't support optimized code for now, DebugInfoPDB does not return this 1219 // information. 1220 LazyBool optimized = eLazyBoolNo; 1221 auto cu_sp = std::make_shared<CompileUnit>( 1222 m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized); 1223 1224 if (!cu_sp) 1225 return CompUnitSP(); 1226 1227 m_comp_units.insert(std::make_pair(id, cu_sp)); 1228 if (index == UINT32_MAX) 1229 GetCompileUnitIndex(*compiland_up, index); 1230 lldbassert(index != UINT32_MAX); 1231 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( 1232 index, cu_sp); 1233 return cu_sp; 1234 } 1235 1236 bool SymbolFilePDB::ParseCompileUnitLineTable( 1237 const lldb_private::SymbolContext &sc, uint32_t match_line) { 1238 lldbassert(sc.comp_unit); 1239 1240 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); 1241 if (!compiland_up) 1242 return false; 1243 1244 // LineEntry needs the *index* of the file into the list of support files 1245 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 1246 // a globally unique idenfitifier in the namespace of the PDB. So, we have to 1247 // do a mapping so that we can hand out indices. 1248 llvm::DenseMap<uint32_t, uint32_t> index_map; 1249 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); 1250 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); 1251 1252 // Find contributions to `compiland` from all source and header files. 1253 std::string path = sc.comp_unit->GetPath(); 1254 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); 1255 if (!files) 1256 return false; 1257 1258 // For each source and header file, create a LineSequence for contributions to 1259 // the compiland from that file, and add the sequence. 1260 while (auto file = files->getNext()) { 1261 std::unique_ptr<LineSequence> sequence( 1262 line_table->CreateLineSequenceContainer()); 1263 auto lines = m_session_up->findLineNumbers(*compiland_up, *file); 1264 if (!lines) 1265 continue; 1266 int entry_count = lines->getChildCount(); 1267 1268 uint64_t prev_addr; 1269 uint32_t prev_length; 1270 uint32_t prev_line; 1271 uint32_t prev_source_idx; 1272 1273 for (int i = 0; i < entry_count; ++i) { 1274 auto line = lines->getChildAtIndex(i); 1275 1276 uint64_t lno = line->getLineNumber(); 1277 uint64_t addr = line->getVirtualAddress(); 1278 uint32_t length = line->getLength(); 1279 uint32_t source_id = line->getSourceFileId(); 1280 uint32_t col = line->getColumnNumber(); 1281 uint32_t source_idx = index_map[source_id]; 1282 1283 // There was a gap between the current entry and the previous entry if the 1284 // addresses don't perfectly line up. 1285 bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 1286 1287 // Before inserting the current entry, insert a terminal entry at the end 1288 // of the previous entry's address range if the current entry resulted in 1289 // a gap from the previous entry. 1290 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 1291 line_table->AppendLineEntryToSequence( 1292 sequence.get(), prev_addr + prev_length, prev_line, 0, 1293 prev_source_idx, false, false, false, false, true); 1294 } 1295 1296 if (ShouldAddLine(match_line, lno, length)) { 1297 bool is_statement = line->isStatement(); 1298 bool is_prologue = false; 1299 bool is_epilogue = false; 1300 auto func = 1301 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 1302 if (func) { 1303 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 1304 if (prologue) 1305 is_prologue = (addr == prologue->getVirtualAddress()); 1306 1307 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 1308 if (epilogue) 1309 is_epilogue = (addr == epilogue->getVirtualAddress()); 1310 } 1311 1312 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 1313 source_idx, is_statement, false, 1314 is_prologue, is_epilogue, false); 1315 } 1316 1317 prev_addr = addr; 1318 prev_length = length; 1319 prev_line = lno; 1320 prev_source_idx = source_idx; 1321 } 1322 1323 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 1324 // The end is always a terminal entry, so insert it regardless. 1325 line_table->AppendLineEntryToSequence( 1326 sequence.get(), prev_addr + prev_length, prev_line, 0, 1327 prev_source_idx, false, false, false, false, true); 1328 } 1329 1330 line_table->InsertSequence(sequence.release()); 1331 } 1332 1333 if (line_table->GetSize()) { 1334 sc.comp_unit->SetLineTable(line_table.release()); 1335 return true; 1336 } 1337 return false; 1338 } 1339 1340 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 1341 const PDBSymbolCompiland &compiland, 1342 llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 1343 // This is a hack, but we need to convert the source id into an index into the 1344 // support files array. We don't want to do path comparisons to avoid 1345 // basename / full path issues that may or may not even be a problem, so we 1346 // use the globally unique source file identifiers. Ideally we could use the 1347 // global identifiers everywhere, but LineEntry currently assumes indices. 1348 auto source_files = m_session_up->getSourceFilesForCompiland(compiland); 1349 if (!source_files) 1350 return; 1351 int index = 0; 1352 1353 while (auto file = source_files->getNext()) { 1354 uint32_t source_id = file->getUniqueId(); 1355 index_map[source_id] = index++; 1356 } 1357 } 1358 1359 lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( 1360 const lldb_private::Address &so_addr) { 1361 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1362 if (file_vm_addr == LLDB_INVALID_ADDRESS) 1363 return nullptr; 1364 1365 auto lines_up = 1366 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200); 1367 if (!lines_up) 1368 return nullptr; 1369 1370 auto first_line_up = lines_up->getNext(); 1371 if (!first_line_up) 1372 return nullptr; 1373 auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId()); 1374 if (compiland_up) { 1375 return ParseCompileUnitForUID(compiland_up->getSymIndexId()); 1376 } 1377 1378 return nullptr; 1379 } 1380 1381 Mangled 1382 SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { 1383 Mangled mangled; 1384 auto func_name = pdb_func.getName(); 1385 auto func_undecorated_name = pdb_func.getUndecoratedName(); 1386 std::string func_decorated_name; 1387 1388 // Seek from public symbols for non-static function's decorated name if any. 1389 // For static functions, they don't have undecorated names and aren't exposed 1390 // in Public Symbols either. 1391 if (!func_undecorated_name.empty()) { 1392 auto result_up = 1393 m_global_scope_up->findChildren(PDB_SymType::PublicSymbol, 1394 func_undecorated_name, 1395 PDB_NameSearchFlags::NS_UndecoratedName); 1396 if (result_up) { 1397 while (auto symbol_up = result_up->getNext()) { 1398 // For a public symbol, it is unique. 1399 lldbassert(result_up->getChildCount() == 1); 1400 if (auto *pdb_public_sym = 1401 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(symbol_up.get())) { 1402 if (pdb_public_sym->isFunction()) { 1403 func_decorated_name = pdb_public_sym->getName(); 1404 break; 1405 } 1406 } 1407 } 1408 } 1409 } 1410 if (!func_decorated_name.empty()) { 1411 mangled.SetMangledName(ConstString(func_decorated_name)); 1412 1413 // For MSVC, format of C funciton's decorated name depends on calling 1414 // conventon. Unfortunately none of the format is recognized by current 1415 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, 1416 // `__purecall` is retrieved as both its decorated and 1417 // undecorated name (using PDBSymbolFunc::getUndecoratedName method). 1418 // However `__purecall` string is not treated as mangled in LLDB 1419 // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method 1420 // will fail internally and caches an empty string as its undecorated 1421 // name. So we will face a contradition here for the same symbol: 1422 // non-empty undecorated name from PDB 1423 // empty undecorated name from LLDB 1424 if (!func_undecorated_name.empty() && 1425 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) 1426 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1427 1428 // LLDB uses several flags to control how a C++ decorated name is 1429 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. 1430 // So the yielded name could be different from what we retrieve from 1431 // PDB source unless we also apply same flags in getting undecorated 1432 // name through PDBSymbolFunc::getUndecoratedNameEx method. 1433 if (!func_undecorated_name.empty() && 1434 mangled.GetDemangledName(mangled.GuessLanguage()) != 1435 ConstString(func_undecorated_name)) 1436 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1437 } else if (!func_undecorated_name.empty()) { 1438 mangled.SetDemangledName(ConstString(func_undecorated_name)); 1439 } else if (!func_name.empty()) 1440 mangled.SetValue(ConstString(func_name), false); 1441 1442 return mangled; 1443 } 1444 1445 bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( 1446 const lldb_private::CompilerDeclContext *decl_ctx) { 1447 if (decl_ctx == nullptr || !decl_ctx->IsValid()) 1448 return true; 1449 1450 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 1451 if (!decl_ctx_type_system) 1452 return false; 1453 TypeSystem *type_system = GetTypeSystemForLanguage( 1454 decl_ctx_type_system->GetMinimumLanguage(nullptr)); 1455 if (decl_ctx_type_system == type_system) 1456 return true; // The type systems match, return true 1457 1458 return false; 1459 } 1460