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