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/TypeMap.h" 22 #include "lldb/Utility/RegularExpression.h" 23 24 #include "llvm/DebugInfo/PDB/GenericError.h" 25 #include "llvm/DebugInfo/PDB/IPDBDataStream.h" 26 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" 27 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" 28 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" 29 #include "llvm/DebugInfo/PDB/IPDBTable.h" 30 #include "llvm/DebugInfo/PDB/PDBSymbol.h" 31 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" 32 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" 33 #include "llvm/DebugInfo/PDB/PDBSymbolData.h" 34 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" 35 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" 36 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" 37 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" 38 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" 39 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" 40 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" 41 42 #include "Plugins/SymbolFile/PDB/PDBASTParser.h" 43 44 #include <regex> 45 46 using namespace lldb_private; 47 using namespace llvm::pdb; 48 49 namespace { 50 lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 51 switch (lang) { 52 case PDB_Lang::Cpp: 53 return lldb::LanguageType::eLanguageTypeC_plus_plus; 54 case PDB_Lang::C: 55 return lldb::LanguageType::eLanguageTypeC; 56 default: 57 return lldb::LanguageType::eLanguageTypeUnknown; 58 } 59 } 60 61 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, 62 uint32_t addr_length) { 63 return ((requested_line == 0 || actual_line == requested_line) && 64 addr_length > 0); 65 } 66 } 67 68 void SymbolFilePDB::Initialize() { 69 PluginManager::RegisterPlugin(GetPluginNameStatic(), 70 GetPluginDescriptionStatic(), CreateInstance, 71 DebuggerInitialize); 72 } 73 74 void SymbolFilePDB::Terminate() { 75 PluginManager::UnregisterPlugin(CreateInstance); 76 } 77 78 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} 79 80 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { 81 static ConstString g_name("pdb"); 82 return g_name; 83 } 84 85 const char *SymbolFilePDB::GetPluginDescriptionStatic() { 86 return "Microsoft PDB debug symbol file reader."; 87 } 88 89 lldb_private::SymbolFile * 90 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { 91 return new SymbolFilePDB(obj_file); 92 } 93 94 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) 95 : SymbolFile(object_file), m_cached_compile_unit_count(0) {} 96 97 SymbolFilePDB::~SymbolFilePDB() {} 98 99 uint32_t SymbolFilePDB::CalculateAbilities() { 100 uint32_t abilities = 0; 101 if (!m_obj_file) 102 return 0; 103 104 if (!m_session_up) { 105 // Lazily load and match the PDB file, but only do this once. 106 std::string exePath = m_obj_file->GetFileSpec().GetPath(); 107 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), 108 m_session_up); 109 if (error) { 110 llvm::consumeError(std::move(error)); 111 auto module_sp = m_obj_file->GetModule(); 112 if (!module_sp) 113 return 0; 114 // See if any symbol file is specified through `--symfile` option. 115 FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 116 if (!symfile) 117 return 0; 118 error = loadDataForPDB(PDB_ReaderType::DIA, 119 llvm::StringRef(symfile.GetPath()), 120 m_session_up); 121 if (error) { 122 llvm::consumeError(std::move(error)); 123 return 0; 124 } 125 } 126 } 127 if (!m_session_up.get()) 128 return 0; 129 130 auto enum_tables_up = m_session_up->getEnumTables(); 131 if (!enum_tables_up) 132 return 0; 133 while (auto table_up = enum_tables_up->getNext()) { 134 if (table_up->getItemCount() == 0) 135 continue; 136 auto type = table_up->getTableType(); 137 switch (type) { 138 case PDB_TableType::Symbols: 139 // This table represents a store of symbols with types listed in 140 // PDBSym_Type 141 abilities |= (CompileUnits | Functions | Blocks | 142 GlobalVariables | LocalVariables | VariableTypes); 143 break; 144 case PDB_TableType::LineNumbers: 145 abilities |= LineTables; 146 break; 147 default: break; 148 } 149 } 150 return abilities; 151 } 152 153 void SymbolFilePDB::InitializeObject() { 154 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); 155 m_session_up->setLoadAddress(obj_load_address); 156 157 TypeSystem *type_system = 158 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 159 ClangASTContext *clang_type_system = 160 llvm::dyn_cast_or_null<ClangASTContext>(type_system); 161 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( 162 type_system, clang_type_system->GetTranslationUnitDecl()); 163 } 164 165 uint32_t SymbolFilePDB::GetNumCompileUnits() { 166 if (m_cached_compile_unit_count == 0) { 167 auto global = m_session_up->getGlobalScope(); 168 auto compilands = global->findAllChildren<PDBSymbolCompiland>(); 169 m_cached_compile_unit_count = compilands->getChildCount(); 170 171 // The linker can inject an additional "dummy" compilation unit into the 172 // PDB. Ignore this special compile unit for our purposes, if it is there. 173 // It is always the last one. 174 auto last_cu = compilands->getChildAtIndex(m_cached_compile_unit_count - 1); 175 std::string name = last_cu->getName(); 176 if (name == "* Linker *") 177 --m_cached_compile_unit_count; 178 } 179 return m_cached_compile_unit_count; 180 } 181 182 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { 183 auto global = m_session_up->getGlobalScope(); 184 auto compilands = global->findAllChildren<PDBSymbolCompiland>(); 185 auto cu = compilands->getChildAtIndex(index); 186 187 uint32_t id = cu->getSymIndexId(); 188 189 return ParseCompileUnitForSymIndex(id); 190 } 191 192 lldb::LanguageType 193 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { 194 // What fields should I expect to be filled out on the SymbolContext? Is it 195 // safe to assume that `sc.comp_unit` is valid? 196 if (!sc.comp_unit) 197 return lldb::eLanguageTypeUnknown; 198 199 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>( 200 sc.comp_unit->GetID()); 201 if (!cu) 202 return lldb::eLanguageTypeUnknown; 203 auto details = cu->findOneChild<PDBSymbolCompilandDetails>(); 204 if (!details) 205 return lldb::eLanguageTypeUnknown; 206 return TranslateLanguage(details->getLanguage()); 207 } 208 209 size_t SymbolFilePDB::ParseCompileUnitFunctions( 210 const lldb_private::SymbolContext &sc) { 211 // TODO: Implement this 212 return size_t(); 213 } 214 215 bool SymbolFilePDB::ParseCompileUnitLineTable( 216 const lldb_private::SymbolContext &sc) { 217 return ParseCompileUnitLineTable(sc, 0); 218 } 219 220 bool SymbolFilePDB::ParseCompileUnitDebugMacros( 221 const lldb_private::SymbolContext &sc) { 222 // PDB doesn't contain information about macros 223 return false; 224 } 225 226 bool SymbolFilePDB::ParseCompileUnitSupportFiles( 227 const lldb_private::SymbolContext &sc, 228 lldb_private::FileSpecList &support_files) { 229 if (!sc.comp_unit) 230 return false; 231 232 // In theory this is unnecessary work for us, because all of this information 233 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a 234 // second time seems like a waste. Unfortunately, there's no good way around 235 // this short of a moderate refactor since SymbolVendor depends on being able 236 // to cache this list. 237 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>( 238 sc.comp_unit->GetID()); 239 if (!cu) 240 return false; 241 auto files = m_session_up->getSourceFilesForCompiland(*cu); 242 if (!files || files->getChildCount() == 0) 243 return false; 244 245 while (auto file = files->getNext()) { 246 FileSpec spec(file->getFileName(), false); 247 support_files.Append(spec); 248 } 249 return true; 250 } 251 252 bool SymbolFilePDB::ParseImportedModules( 253 const lldb_private::SymbolContext &sc, 254 std::vector<lldb_private::ConstString> &imported_modules) { 255 // PDB does not yet support module debug info 256 return false; 257 } 258 259 size_t 260 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { 261 // TODO: Implement this 262 return size_t(); 263 } 264 265 size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { 266 // TODO: Implement this 267 return size_t(); 268 } 269 270 size_t 271 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { 272 // TODO: Implement this 273 return size_t(); 274 } 275 276 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 277 auto find_result = m_types.find(type_uid); 278 if (find_result != m_types.end()) 279 return find_result->second.get(); 280 281 TypeSystem *type_system = 282 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); 283 ClangASTContext *clang_type_system = 284 llvm::dyn_cast_or_null<ClangASTContext>(type_system); 285 if (!clang_type_system) 286 return nullptr; 287 PDBASTParser *pdb = 288 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); 289 if (!pdb) 290 return nullptr; 291 292 auto pdb_type = m_session_up->getSymbolById(type_uid); 293 if (pdb_type == nullptr) 294 return nullptr; 295 296 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); 297 if (result.get()) 298 m_types.insert(std::make_pair(type_uid, result)); 299 return result.get(); 300 } 301 302 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { 303 // TODO: Implement this 304 return false; 305 } 306 307 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { 308 return lldb_private::CompilerDecl(); 309 } 310 311 lldb_private::CompilerDeclContext 312 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { 313 // PDB always uses the translation unit decl context for everything. We can 314 // improve this later but it's not easy because PDB doesn't provide a high 315 // enough level of type fidelity in this area. 316 return *m_tu_decl_ctx_up; 317 } 318 319 lldb_private::CompilerDeclContext 320 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { 321 return *m_tu_decl_ctx_up; 322 } 323 324 void SymbolFilePDB::ParseDeclsForContext( 325 lldb_private::CompilerDeclContext decl_ctx) {} 326 327 uint32_t 328 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, 329 uint32_t resolve_scope, 330 lldb_private::SymbolContext &sc) { 331 return uint32_t(); 332 } 333 334 uint32_t SymbolFilePDB::ResolveSymbolContext( 335 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, 336 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) { 337 if (resolve_scope & lldb::eSymbolContextCompUnit) { 338 // Locate all compilation units with line numbers referencing the specified 339 // file. For example, if `file_spec` is <vector>, then this should return 340 // all source files and header files that reference <vector>, either 341 // directly or indirectly. 342 auto compilands = m_session_up->findCompilandsForSourceFile( 343 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); 344 345 // For each one, either find its previously parsed data or parse it afresh 346 // and add it to the symbol context list. 347 while (auto compiland = compilands->getNext()) { 348 // If we're not checking inlines, then don't add line information for this 349 // file unless the FileSpec matches. 350 if (!check_inlines) { 351 // `getSourceFileName` returns the basename of the original source file 352 // used to generate this compiland. It does not return the full path. 353 // Currently the only way to get that is to do a basename lookup to get 354 // the IPDBSourceFile, but this is ambiguous in the case of two source 355 // files with the same name contributing to the same compiland. This is 356 // a moderately extreme edge case, so we consider this OK for now, 357 // although we need to find a long-term solution. 358 std::string source_file = compiland->getSourceFileName(); 359 auto pdb_file = m_session_up->findOneSourceFile( 360 compiland.get(), source_file, 361 PDB_NameSearchFlags::NS_CaseInsensitive); 362 source_file = pdb_file->getFileName(); 363 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); 364 if (!file_spec.FileEquals(this_spec)) 365 continue; 366 } 367 368 SymbolContext sc; 369 auto cu = ParseCompileUnitForSymIndex(compiland->getSymIndexId()); 370 sc.comp_unit = cu.get(); 371 sc.module_sp = cu->GetModule(); 372 sc_list.Append(sc); 373 374 // If we were asked to resolve line entries, add all entries to the line 375 // table that match the requested line (or all lines if `line` == 0). 376 if (resolve_scope & lldb::eSymbolContextLineEntry) 377 ParseCompileUnitLineTable(sc, line); 378 } 379 } 380 return sc_list.GetSize(); 381 } 382 383 uint32_t SymbolFilePDB::FindGlobalVariables( 384 const lldb_private::ConstString &name, 385 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 386 uint32_t max_matches, lldb_private::VariableList &variables) { 387 return uint32_t(); 388 } 389 390 uint32_t 391 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, 392 bool append, uint32_t max_matches, 393 lldb_private::VariableList &variables) { 394 return uint32_t(); 395 } 396 397 uint32_t SymbolFilePDB::FindFunctions( 398 const lldb_private::ConstString &name, 399 const lldb_private::CompilerDeclContext *parent_decl_ctx, 400 uint32_t name_type_mask, bool include_inlines, bool append, 401 lldb_private::SymbolContextList &sc_list) { 402 return uint32_t(); 403 } 404 405 uint32_t 406 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, 407 bool include_inlines, bool append, 408 lldb_private::SymbolContextList &sc_list) { 409 return uint32_t(); 410 } 411 412 void SymbolFilePDB::GetMangledNamesForFunction( 413 const std::string &scope_qualified_name, 414 std::vector<lldb_private::ConstString> &mangled_names) {} 415 416 uint32_t SymbolFilePDB::FindTypes( 417 const lldb_private::SymbolContext &sc, 418 const lldb_private::ConstString &name, 419 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, 420 uint32_t max_matches, 421 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 422 lldb_private::TypeMap &types) { 423 if (!append) 424 types.Clear(); 425 if (!name) 426 return 0; 427 428 searched_symbol_files.clear(); 429 searched_symbol_files.insert(this); 430 431 std::string name_str = name.AsCString(); 432 433 // There is an assumption 'name' is not a regex 434 FindTypesByName(name_str, max_matches, types); 435 436 return types.GetSize(); 437 } 438 439 void 440 SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression ®ex, 441 uint32_t max_matches, 442 lldb_private::TypeMap &types) { 443 // When searching by regex, we need to go out of our way to limit the search 444 // space as much as possible since this searches EVERYTHING in the PDB, 445 // manually doing regex comparisons. PDB library isn't optimized for regex 446 // searches or searches across multiple symbol types at the same time, so the 447 // best we can do is to search enums, then typedefs, then classes one by one, 448 // and do a regex comparison against each of them. 449 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, 450 PDB_SymType::UDT}; 451 auto global = m_session_up->getGlobalScope(); 452 std::unique_ptr<IPDBEnumSymbols> results; 453 454 uint32_t matches = 0; 455 456 for (auto tag : tags_to_search) { 457 results = global->findAllChildren(tag); 458 while (auto result = results->getNext()) { 459 if (max_matches > 0 && matches >= max_matches) 460 break; 461 462 std::string type_name; 463 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) 464 type_name = enum_type->getName(); 465 else if (auto typedef_type = 466 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) 467 type_name = typedef_type->getName(); 468 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) 469 type_name = class_type->getName(); 470 else { 471 // We're looking only for types that have names. Skip symbols, as well 472 // as unnamed types such as arrays, pointers, etc. 473 continue; 474 } 475 476 if (!regex.Execute(type_name)) 477 continue; 478 479 // This should cause the type to get cached and stored in the `m_types` 480 // lookup. 481 if (!ResolveTypeUID(result->getSymIndexId())) 482 continue; 483 484 auto iter = m_types.find(result->getSymIndexId()); 485 if (iter == m_types.end()) 486 continue; 487 types.Insert(iter->second); 488 ++matches; 489 } 490 } 491 } 492 493 void SymbolFilePDB::FindTypesByName(const std::string &name, 494 uint32_t max_matches, 495 lldb_private::TypeMap &types) { 496 auto global = m_session_up->getGlobalScope(); 497 std::unique_ptr<IPDBEnumSymbols> results; 498 results = global->findChildren(PDB_SymType::None, name, 499 PDB_NameSearchFlags::NS_Default); 500 501 uint32_t matches = 0; 502 503 while (auto result = results->getNext()) { 504 if (max_matches > 0 && matches >= max_matches) 505 break; 506 switch (result->getSymTag()) { 507 case PDB_SymType::Enum: 508 case PDB_SymType::UDT: 509 case PDB_SymType::Typedef: 510 break; 511 default: 512 // We're looking only for types that have names. Skip symbols, as well as 513 // unnamed types such as arrays, pointers, etc. 514 continue; 515 } 516 517 // This should cause the type to get cached and stored in the `m_types` 518 // lookup. 519 if (!ResolveTypeUID(result->getSymIndexId())) 520 continue; 521 522 auto iter = m_types.find(result->getSymIndexId()); 523 if (iter == m_types.end()) 524 continue; 525 types.Insert(iter->second); 526 ++matches; 527 } 528 } 529 530 size_t SymbolFilePDB::FindTypes( 531 const std::vector<lldb_private::CompilerContext> &contexts, bool append, 532 lldb_private::TypeMap &types) { 533 return 0; 534 } 535 536 lldb_private::TypeList *SymbolFilePDB::GetTypeList() { return nullptr; } 537 538 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 539 uint32_t type_mask, 540 lldb_private::TypeList &type_list) { 541 return size_t(); 542 } 543 544 lldb_private::TypeSystem * 545 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 546 auto type_system = 547 m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 548 if (type_system) 549 type_system->SetSymbolFile(this); 550 return type_system; 551 } 552 553 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( 554 const lldb_private::SymbolContext &sc, 555 const lldb_private::ConstString &name, 556 const lldb_private::CompilerDeclContext *parent_decl_ctx) { 557 return lldb_private::CompilerDeclContext(); 558 } 559 560 lldb_private::ConstString SymbolFilePDB::GetPluginName() { 561 static ConstString g_name("pdb"); 562 return g_name; 563 } 564 565 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } 566 567 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } 568 569 const IPDBSession &SymbolFilePDB::GetPDBSession() const { 570 return *m_session_up; 571 } 572 573 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForSymIndex(uint32_t id) { 574 auto found_cu = m_comp_units.find(id); 575 if (found_cu != m_comp_units.end()) 576 return found_cu->second; 577 578 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(id); 579 580 // `getSourceFileName` returns the basename of the original source file used 581 // to generate this compiland. It does not return the full path. Currently 582 // the only way to get that is to do a basename lookup to get the 583 // IPDBSourceFile, but this is ambiguous in the case of two source files with 584 // the same name contributing to the same compiland. This is a moderately 585 // extreme edge case, so we consider this OK for now, although we need to find 586 // a long-term solution. 587 auto file = 588 m_session_up->findOneSourceFile(cu.get(), cu->getSourceFileName(), 589 PDB_NameSearchFlags::NS_CaseInsensitive); 590 std::string path = file->getFileName(); 591 592 lldb::LanguageType lang; 593 auto details = cu->findOneChild<PDBSymbolCompilandDetails>(); 594 if (!details) 595 lang = lldb::eLanguageTypeC_plus_plus; 596 else 597 lang = TranslateLanguage(details->getLanguage()); 598 599 // Don't support optimized code for now, DebugInfoPDB does not return this 600 // information. 601 LazyBool optimized = eLazyBoolNo; 602 auto result = std::make_shared<CompileUnit>( 603 m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized); 604 m_comp_units.insert(std::make_pair(id, result)); 605 return result; 606 } 607 608 bool SymbolFilePDB::ParseCompileUnitLineTable( 609 const lldb_private::SymbolContext &sc, uint32_t match_line) { 610 auto global = m_session_up->getGlobalScope(); 611 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>( 612 sc.comp_unit->GetID()); 613 614 // LineEntry needs the *index* of the file into the list of support files 615 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us 616 // a globally unique idenfitifier in the namespace of the PDB. So, we have to 617 // do a mapping so that we can hand out indices. 618 llvm::DenseMap<uint32_t, uint32_t> index_map; 619 BuildSupportFileIdToSupportFileIndexMap(*cu, index_map); 620 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); 621 622 // Find contributions to `cu` from all source and header files. 623 std::string path = sc.comp_unit->GetPath(); 624 auto files = m_session_up->getSourceFilesForCompiland(*cu); 625 626 // For each source and header file, create a LineSequence for contributions to 627 // the cu from that file, and add the sequence. 628 while (auto file = files->getNext()) { 629 std::unique_ptr<LineSequence> sequence( 630 line_table->CreateLineSequenceContainer()); 631 auto lines = m_session_up->findLineNumbers(*cu, *file); 632 int entry_count = lines->getChildCount(); 633 634 uint64_t prev_addr; 635 uint32_t prev_length; 636 uint32_t prev_line; 637 uint32_t prev_source_idx; 638 639 for (int i = 0; i < entry_count; ++i) { 640 auto line = lines->getChildAtIndex(i); 641 642 uint64_t lno = line->getLineNumber(); 643 uint64_t addr = line->getVirtualAddress(); 644 uint32_t length = line->getLength(); 645 uint32_t source_id = line->getSourceFileId(); 646 uint32_t col = line->getColumnNumber(); 647 uint32_t source_idx = index_map[source_id]; 648 649 // There was a gap between the current entry and the previous entry if the 650 // addresses don't perfectly line up. 651 bool is_gap = (i > 0) && (prev_addr + prev_length < addr); 652 653 // Before inserting the current entry, insert a terminal entry at the end 654 // of the previous entry's address range if the current entry resulted in 655 // a gap from the previous entry. 656 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { 657 line_table->AppendLineEntryToSequence( 658 sequence.get(), prev_addr + prev_length, prev_line, 0, 659 prev_source_idx, false, false, false, false, true); 660 } 661 662 if (ShouldAddLine(match_line, lno, length)) { 663 bool is_statement = line->isStatement(); 664 bool is_prologue = false; 665 bool is_epilogue = false; 666 auto func = 667 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); 668 if (func) { 669 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); 670 is_prologue = (addr == prologue->getVirtualAddress()); 671 672 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); 673 is_epilogue = (addr == epilogue->getVirtualAddress()); 674 } 675 676 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, 677 source_idx, is_statement, false, 678 is_prologue, is_epilogue, false); 679 } 680 681 prev_addr = addr; 682 prev_length = length; 683 prev_line = lno; 684 prev_source_idx = source_idx; 685 } 686 687 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { 688 // The end is always a terminal entry, so insert it regardless. 689 line_table->AppendLineEntryToSequence( 690 sequence.get(), prev_addr + prev_length, prev_line, 0, 691 prev_source_idx, false, false, false, false, true); 692 } 693 694 line_table->InsertSequence(sequence.release()); 695 } 696 697 sc.comp_unit->SetLineTable(line_table.release()); 698 return true; 699 } 700 701 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( 702 const PDBSymbolCompiland &cu, 703 llvm::DenseMap<uint32_t, uint32_t> &index_map) const { 704 // This is a hack, but we need to convert the source id into an index into the 705 // support files array. We don't want to do path comparisons to avoid 706 // basename / full path issues that may or may not even be a problem, so we 707 // use the globally unique source file identifiers. Ideally we could use the 708 // global identifiers everywhere, but LineEntry currently assumes indices. 709 auto source_files = m_session_up->getSourceFilesForCompiland(cu); 710 int index = 0; 711 712 while (auto file = source_files->getNext()) { 713 uint32_t source_id = file->getUniqueId(); 714 index_map[source_id] = index++; 715 } 716 } 717