1 //===-- SymbolFileNativePDB.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 "SymbolFileNativePDB.h" 11 12 #include "clang/AST/Attr.h" 13 #include "clang/AST/CharUnits.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/DeclCXX.h" 16 17 #include "lldb/Core/Module.h" 18 #include "lldb/Core/PluginManager.h" 19 #include "lldb/Symbol/ClangASTContext.h" 20 #include "lldb/Symbol/ClangASTImporter.h" 21 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 22 #include "lldb/Symbol/CompileUnit.h" 23 #include "lldb/Symbol/LineTable.h" 24 #include "lldb/Symbol/ObjectFile.h" 25 #include "lldb/Symbol/SymbolContext.h" 26 #include "lldb/Symbol/SymbolVendor.h" 27 28 #include "llvm/DebugInfo/CodeView/CVRecord.h" 29 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" 30 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" 31 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" 32 #include "llvm/DebugInfo/CodeView/RecordName.h" 33 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" 34 #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" 35 #include "llvm/DebugInfo/PDB/Native/DbiStream.h" 36 #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" 37 #include "llvm/DebugInfo/PDB/Native/InfoStream.h" 38 #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" 39 #include "llvm/DebugInfo/PDB/Native/PDBFile.h" 40 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" 41 #include "llvm/DebugInfo/PDB/Native/TpiStream.h" 42 #include "llvm/DebugInfo/PDB/PDBTypes.h" 43 #include "llvm/Object/COFF.h" 44 #include "llvm/Support/Allocator.h" 45 #include "llvm/Support/BinaryStreamReader.h" 46 #include "llvm/Support/ErrorOr.h" 47 #include "llvm/Support/MemoryBuffer.h" 48 49 #include "PdbSymUid.h" 50 #include "PdbUtil.h" 51 #include "UdtRecordCompleter.h" 52 53 using namespace lldb; 54 using namespace lldb_private; 55 using namespace npdb; 56 using namespace llvm::codeview; 57 using namespace llvm::pdb; 58 59 static lldb::LanguageType TranslateLanguage(PDB_Lang lang) { 60 switch (lang) { 61 case PDB_Lang::Cpp: 62 return lldb::LanguageType::eLanguageTypeC_plus_plus; 63 case PDB_Lang::C: 64 return lldb::LanguageType::eLanguageTypeC; 65 default: 66 return lldb::LanguageType::eLanguageTypeUnknown; 67 } 68 } 69 70 static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath, 71 llvm::BumpPtrAllocator &Allocator) { 72 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer = 73 llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1, 74 /*RequiresNullTerminator=*/false); 75 if (!ErrorOrBuffer) 76 return nullptr; 77 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer); 78 79 llvm::StringRef Path = Buffer->getBufferIdentifier(); 80 auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>( 81 std::move(Buffer), llvm::support::little); 82 83 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator); 84 if (auto EC = File->parseFileHeaders()) { 85 llvm::consumeError(std::move(EC)); 86 return nullptr; 87 } 88 if (auto EC = File->parseStreamData()) { 89 llvm::consumeError(std::move(EC)); 90 return nullptr; 91 } 92 93 return File; 94 } 95 96 static std::unique_ptr<PDBFile> 97 loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) { 98 // Try to find a matching PDB for an EXE. 99 using namespace llvm::object; 100 auto expected_binary = createBinary(exe_path); 101 102 // If the file isn't a PE/COFF executable, fail. 103 if (!expected_binary) { 104 llvm::consumeError(expected_binary.takeError()); 105 return nullptr; 106 } 107 OwningBinary<Binary> binary = std::move(*expected_binary); 108 109 auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary()); 110 if (!obj) 111 return nullptr; 112 const llvm::codeview::DebugInfo *pdb_info = nullptr; 113 114 // If it doesn't have a debug directory, fail. 115 llvm::StringRef pdb_file; 116 auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file); 117 if (ec) 118 return nullptr; 119 120 // if the file doesn't exist, is not a pdb, or doesn't have a matching guid, 121 // fail. 122 llvm::file_magic magic; 123 ec = llvm::identify_magic(pdb_file, magic); 124 if (ec || magic != llvm::file_magic::pdb) 125 return nullptr; 126 std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator); 127 if (!pdb) 128 return nullptr; 129 130 auto expected_info = pdb->getPDBInfoStream(); 131 if (!expected_info) { 132 llvm::consumeError(expected_info.takeError()); 133 return nullptr; 134 } 135 llvm::codeview::GUID guid; 136 memcpy(&guid, pdb_info->PDB70.Signature, 16); 137 138 if (expected_info->getGuid() != guid) 139 return nullptr; 140 return pdb; 141 } 142 143 static bool IsFunctionPrologue(const CompilandIndexItem &cci, 144 lldb::addr_t addr) { 145 // FIXME: Implement this. 146 return false; 147 } 148 149 static bool IsFunctionEpilogue(const CompilandIndexItem &cci, 150 lldb::addr_t addr) { 151 // FIXME: Implement this. 152 return false; 153 } 154 155 static clang::MSInheritanceAttr::Spelling 156 GetMSInheritance(LazyRandomTypeCollection &tpi, const ClassRecord &record) { 157 if (record.DerivationList == TypeIndex::None()) 158 return clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance; 159 160 CVType bases = tpi.getType(record.DerivationList); 161 ArgListRecord base_list; 162 cantFail(TypeDeserializer::deserializeAs<ArgListRecord>(bases, base_list)); 163 if (base_list.ArgIndices.empty()) 164 return clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance; 165 166 int base_count = 0; 167 for (TypeIndex ti : base_list.ArgIndices) { 168 CVType base = tpi.getType(ti); 169 if (base.kind() == LF_VBCLASS || base.kind() == LF_IVBCLASS) 170 return clang::MSInheritanceAttr::Spelling::Keyword_virtual_inheritance; 171 ++base_count; 172 } 173 174 if (base_count > 1) 175 return clang::MSInheritanceAttr::Keyword_multiple_inheritance; 176 return clang::MSInheritanceAttr::Keyword_single_inheritance; 177 } 178 179 static lldb::BasicType GetCompilerTypeForSimpleKind(SimpleTypeKind kind) { 180 switch (kind) { 181 case SimpleTypeKind::Boolean128: 182 case SimpleTypeKind::Boolean16: 183 case SimpleTypeKind::Boolean32: 184 case SimpleTypeKind::Boolean64: 185 case SimpleTypeKind::Boolean8: 186 return lldb::eBasicTypeBool; 187 case SimpleTypeKind::Byte: 188 case SimpleTypeKind::UnsignedCharacter: 189 return lldb::eBasicTypeUnsignedChar; 190 case SimpleTypeKind::NarrowCharacter: 191 return lldb::eBasicTypeChar; 192 case SimpleTypeKind::SignedCharacter: 193 case SimpleTypeKind::SByte: 194 return lldb::eBasicTypeSignedChar; 195 case SimpleTypeKind::Character16: 196 return lldb::eBasicTypeChar16; 197 case SimpleTypeKind::Character32: 198 return lldb::eBasicTypeChar32; 199 case SimpleTypeKind::Complex80: 200 return lldb::eBasicTypeLongDoubleComplex; 201 case SimpleTypeKind::Complex64: 202 return lldb::eBasicTypeDoubleComplex; 203 case SimpleTypeKind::Complex32: 204 return lldb::eBasicTypeFloatComplex; 205 case SimpleTypeKind::Float128: 206 case SimpleTypeKind::Float80: 207 return lldb::eBasicTypeLongDouble; 208 case SimpleTypeKind::Float64: 209 return lldb::eBasicTypeDouble; 210 case SimpleTypeKind::Float32: 211 return lldb::eBasicTypeFloat; 212 case SimpleTypeKind::Float16: 213 return lldb::eBasicTypeHalf; 214 case SimpleTypeKind::Int128: 215 return lldb::eBasicTypeInt128; 216 case SimpleTypeKind::Int64: 217 case SimpleTypeKind::Int64Quad: 218 return lldb::eBasicTypeLongLong; 219 case SimpleTypeKind::Int32: 220 return lldb::eBasicTypeInt; 221 case SimpleTypeKind::Int16: 222 case SimpleTypeKind::Int16Short: 223 return lldb::eBasicTypeShort; 224 case SimpleTypeKind::UInt128: 225 return lldb::eBasicTypeUnsignedInt128; 226 case SimpleTypeKind::UInt64: 227 case SimpleTypeKind::UInt64Quad: 228 return lldb::eBasicTypeUnsignedLongLong; 229 case SimpleTypeKind::HResult: 230 case SimpleTypeKind::UInt32: 231 return lldb::eBasicTypeUnsignedInt; 232 case SimpleTypeKind::UInt16: 233 case SimpleTypeKind::UInt16Short: 234 return lldb::eBasicTypeUnsignedShort; 235 case SimpleTypeKind::Int32Long: 236 return lldb::eBasicTypeLong; 237 case SimpleTypeKind::UInt32Long: 238 return lldb::eBasicTypeUnsignedLong; 239 case SimpleTypeKind::Void: 240 return lldb::eBasicTypeVoid; 241 case SimpleTypeKind::WideCharacter: 242 return lldb::eBasicTypeWChar; 243 default: 244 return lldb::eBasicTypeInvalid; 245 } 246 } 247 248 static size_t GetTypeSizeForSimpleKind(SimpleTypeKind kind) { 249 switch (kind) { 250 case SimpleTypeKind::Boolean128: 251 case SimpleTypeKind::Int128: 252 case SimpleTypeKind::UInt128: 253 case SimpleTypeKind::Float128: 254 return 16; 255 case SimpleTypeKind::Complex80: 256 case SimpleTypeKind::Float80: 257 return 10; 258 case SimpleTypeKind::Boolean64: 259 case SimpleTypeKind::Complex64: 260 case SimpleTypeKind::UInt64: 261 case SimpleTypeKind::UInt64Quad: 262 case SimpleTypeKind::Float64: 263 case SimpleTypeKind::Int64: 264 case SimpleTypeKind::Int64Quad: 265 return 8; 266 case SimpleTypeKind::Boolean32: 267 case SimpleTypeKind::Character32: 268 case SimpleTypeKind::Complex32: 269 case SimpleTypeKind::Float32: 270 case SimpleTypeKind::Int32: 271 case SimpleTypeKind::Int32Long: 272 case SimpleTypeKind::UInt32Long: 273 case SimpleTypeKind::HResult: 274 case SimpleTypeKind::UInt32: 275 return 4; 276 case SimpleTypeKind::Boolean16: 277 case SimpleTypeKind::Character16: 278 case SimpleTypeKind::Float16: 279 case SimpleTypeKind::Int16: 280 case SimpleTypeKind::Int16Short: 281 case SimpleTypeKind::UInt16: 282 case SimpleTypeKind::UInt16Short: 283 case SimpleTypeKind::WideCharacter: 284 return 2; 285 case SimpleTypeKind::Boolean8: 286 case SimpleTypeKind::Byte: 287 case SimpleTypeKind::UnsignedCharacter: 288 case SimpleTypeKind::NarrowCharacter: 289 case SimpleTypeKind::SignedCharacter: 290 case SimpleTypeKind::SByte: 291 return 1; 292 case SimpleTypeKind::Void: 293 default: 294 return 0; 295 } 296 } 297 298 static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) { 299 switch (kind) { 300 case SimpleTypeKind::Boolean128: 301 case SimpleTypeKind::Boolean16: 302 case SimpleTypeKind::Boolean32: 303 case SimpleTypeKind::Boolean64: 304 case SimpleTypeKind::Boolean8: 305 return "bool"; 306 case SimpleTypeKind::Byte: 307 case SimpleTypeKind::UnsignedCharacter: 308 return "unsigned char"; 309 case SimpleTypeKind::NarrowCharacter: 310 return "char"; 311 case SimpleTypeKind::SignedCharacter: 312 case SimpleTypeKind::SByte: 313 return "signed chr"; 314 case SimpleTypeKind::Character16: 315 return "char16_t"; 316 case SimpleTypeKind::Character32: 317 return "char32_t"; 318 case SimpleTypeKind::Complex80: 319 case SimpleTypeKind::Complex64: 320 case SimpleTypeKind::Complex32: 321 return "complex"; 322 case SimpleTypeKind::Float128: 323 case SimpleTypeKind::Float80: 324 return "long double"; 325 case SimpleTypeKind::Float64: 326 return "double"; 327 case SimpleTypeKind::Float32: 328 return "float"; 329 case SimpleTypeKind::Float16: 330 return "single"; 331 case SimpleTypeKind::Int128: 332 return "__int128"; 333 case SimpleTypeKind::Int64: 334 case SimpleTypeKind::Int64Quad: 335 return "__int64"; 336 case SimpleTypeKind::Int32: 337 return "int"; 338 case SimpleTypeKind::Int16: 339 return "short"; 340 case SimpleTypeKind::UInt128: 341 return "unsigned __int128"; 342 case SimpleTypeKind::UInt64: 343 case SimpleTypeKind::UInt64Quad: 344 return "unsigned __int64"; 345 case SimpleTypeKind::HResult: 346 return "HRESULT"; 347 case SimpleTypeKind::UInt32: 348 return "unsigned"; 349 case SimpleTypeKind::UInt16: 350 case SimpleTypeKind::UInt16Short: 351 return "unsigned short"; 352 case SimpleTypeKind::Int32Long: 353 return "long"; 354 case SimpleTypeKind::UInt32Long: 355 return "unsigned long"; 356 case SimpleTypeKind::Void: 357 return "void"; 358 case SimpleTypeKind::WideCharacter: 359 return "wchar_t"; 360 default: 361 return ""; 362 } 363 } 364 365 static bool IsClassRecord(TypeLeafKind kind) { 366 switch (kind) { 367 case LF_STRUCTURE: 368 case LF_CLASS: 369 case LF_INTERFACE: 370 return true; 371 default: 372 return false; 373 } 374 } 375 376 static PDB_SymType GetPdbSymType(TpiStream &tpi, TypeIndex ti) { 377 if (ti.isSimple()) { 378 if (ti.getSimpleMode() == SimpleTypeMode::Direct) 379 return PDB_SymType::BuiltinType; 380 return PDB_SymType::PointerType; 381 } 382 383 CVType cvt = tpi.getType(ti); 384 TypeLeafKind kind = cvt.kind(); 385 if (kind != LF_MODIFIER) 386 return CVTypeToPDBType(kind); 387 388 // If this is an LF_MODIFIER, look through it to get the kind that it 389 // modifies. Note that it's not possible to have an LF_MODIFIER that 390 // modifies another LF_MODIFIER, although this would handle that anyway. 391 ModifierRecord mr; 392 llvm::cantFail(TypeDeserializer::deserializeAs<ModifierRecord>(cvt, mr)); 393 return GetPdbSymType(tpi, mr.ModifiedType); 394 } 395 396 static clang::TagTypeKind TranslateUdtKind(const TagRecord &cr) { 397 switch (cr.Kind) { 398 case TypeRecordKind::Class: 399 return clang::TTK_Class; 400 case TypeRecordKind::Struct: 401 return clang::TTK_Struct; 402 case TypeRecordKind::Union: 403 return clang::TTK_Union; 404 case TypeRecordKind::Interface: 405 return clang::TTK_Interface; 406 case TypeRecordKind::Enum: 407 return clang::TTK_Enum; 408 default: 409 lldbassert(false && "Invalid tag record kind!"); 410 return clang::TTK_Struct; 411 } 412 } 413 414 void SymbolFileNativePDB::Initialize() { 415 PluginManager::RegisterPlugin(GetPluginNameStatic(), 416 GetPluginDescriptionStatic(), CreateInstance, 417 DebuggerInitialize); 418 } 419 420 void SymbolFileNativePDB::Terminate() { 421 PluginManager::UnregisterPlugin(CreateInstance); 422 } 423 424 void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {} 425 426 ConstString SymbolFileNativePDB::GetPluginNameStatic() { 427 static ConstString g_name("native-pdb"); 428 return g_name; 429 } 430 431 const char *SymbolFileNativePDB::GetPluginDescriptionStatic() { 432 return "Microsoft PDB debug symbol cross-platform file reader."; 433 } 434 435 SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) { 436 return new SymbolFileNativePDB(obj_file); 437 } 438 439 SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file) 440 : SymbolFile(object_file) {} 441 442 SymbolFileNativePDB::~SymbolFileNativePDB() {} 443 444 uint32_t SymbolFileNativePDB::CalculateAbilities() { 445 uint32_t abilities = 0; 446 if (!m_obj_file) 447 return 0; 448 449 if (!m_index) { 450 // Lazily load and match the PDB file, but only do this once. 451 std::unique_ptr<PDBFile> file_up = 452 loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator); 453 454 if (!file_up) { 455 auto module_sp = m_obj_file->GetModule(); 456 if (!module_sp) 457 return 0; 458 // See if any symbol file is specified through `--symfile` option. 459 FileSpec symfile = module_sp->GetSymbolFileFileSpec(); 460 if (!symfile) 461 return 0; 462 file_up = loadPDBFile(symfile.GetPath(), m_allocator); 463 } 464 465 if (!file_up) 466 return 0; 467 468 auto expected_index = PdbIndex::create(std::move(file_up)); 469 if (!expected_index) { 470 llvm::consumeError(expected_index.takeError()); 471 return 0; 472 } 473 m_index = std::move(*expected_index); 474 } 475 if (!m_index) 476 return 0; 477 478 // We don't especially have to be precise here. We only distinguish between 479 // stripped and not stripped. 480 abilities = kAllAbilities; 481 482 if (m_index->dbi().isStripped()) 483 abilities &= ~(Blocks | LocalVariables); 484 return abilities; 485 } 486 487 void SymbolFileNativePDB::InitializeObject() { 488 m_obj_load_address = m_obj_file->GetFileOffset(); 489 m_index->SetLoadAddress(m_obj_load_address); 490 m_index->ParseSectionContribs(); 491 492 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus); 493 m_clang = llvm::dyn_cast_or_null<ClangASTContext>(ts); 494 m_importer = llvm::make_unique<ClangASTImporter>(); 495 lldbassert(m_clang); 496 } 497 498 uint32_t SymbolFileNativePDB::GetNumCompileUnits() { 499 const DbiModuleList &modules = m_index->dbi().modules(); 500 uint32_t count = modules.getModuleCount(); 501 if (count == 0) 502 return count; 503 504 // The linker can inject an additional "dummy" compilation unit into the 505 // PDB. Ignore this special compile unit for our purposes, if it is there. 506 // It is always the last one. 507 DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1); 508 if (last.getModuleName() == "* Linker *") 509 --count; 510 return count; 511 } 512 513 lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbSymUid func_uid, 514 const SymbolContext &sc) { 515 lldbassert(func_uid.tag() == PDB_SymType::Function); 516 517 PdbSymUid cuid = PdbSymUid::makeCompilandId(func_uid.asCuSym().modi); 518 519 const CompilandIndexItem *cci = m_index->compilands().GetCompiland(cuid); 520 lldbassert(cci); 521 CVSymbol sym_record = 522 cci->m_debug_stream.readSymbolAtOffset(func_uid.asCuSym().offset); 523 524 lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32); 525 SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record); 526 527 auto file_vm_addr = m_index->MakeVirtualAddress(sol.so); 528 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) 529 return nullptr; 530 531 AddressRange func_range(file_vm_addr, sol.length, 532 sc.module_sp->GetSectionList()); 533 if (!func_range.GetBaseAddress().IsValid()) 534 return nullptr; 535 536 Type *func_type = nullptr; 537 538 // FIXME: Resolve types and mangled names. 539 PdbSymUid sig_uid = 540 PdbSymUid::makeTypeSymId(PDB_SymType::FunctionSig, TypeIndex{0}, false); 541 Mangled mangled(getSymbolName(sym_record)); 542 543 FunctionSP func_sp = std::make_shared<Function>( 544 sc.comp_unit, func_uid.toOpaqueId(), sig_uid.toOpaqueId(), mangled, 545 func_type, func_range); 546 547 sc.comp_unit->AddFunction(func_sp); 548 return func_sp; 549 } 550 551 CompUnitSP 552 SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) { 553 lldb::LanguageType lang = 554 cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage()) 555 : lldb::eLanguageTypeUnknown; 556 557 LazyBool optimized = eLazyBoolNo; 558 if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations()) 559 optimized = eLazyBoolYes; 560 561 llvm::StringRef source_file_name = 562 m_index->compilands().GetMainSourceFile(cci); 563 FileSpec fs(source_file_name, false); 564 565 CompUnitSP cu_sp = 566 std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs, 567 cci.m_uid.toOpaqueId(), lang, optimized); 568 569 const PdbCompilandId &cuid = cci.m_uid.asCompiland(); 570 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cuid.modi, 571 cu_sp); 572 return cu_sp; 573 } 574 575 lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbSymUid type_uid, 576 const ModifierRecord &mr) { 577 TpiStream &stream = m_index->tpi(); 578 579 TypeSP t = GetOrCreateType(mr.ModifiedType); 580 CompilerType ct = t->GetForwardCompilerType(); 581 if ((mr.Modifiers & ModifierOptions::Const) != ModifierOptions::None) 582 ct = ct.AddConstModifier(); 583 if ((mr.Modifiers & ModifierOptions::Volatile) != ModifierOptions::None) 584 ct = ct.AddVolatileModifier(); 585 std::string name; 586 if (mr.ModifiedType.isSimple()) 587 name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind()); 588 else 589 name = computeTypeName(stream.typeCollection(), mr.ModifiedType); 590 Declaration decl; 591 return std::make_shared<Type>(type_uid.toOpaqueId(), m_clang->GetSymbolFile(), 592 ConstString(name), t->GetByteSize(), nullptr, 593 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, 594 ct, Type::eResolveStateFull); 595 } 596 597 lldb::TypeSP SymbolFileNativePDB::CreatePointerType( 598 PdbSymUid type_uid, const llvm::codeview::PointerRecord &pr) { 599 TypeSP pointee = GetOrCreateType(pr.ReferentType); 600 CompilerType pointee_ct = pointee->GetForwardCompilerType(); 601 lldbassert(pointee_ct); 602 Declaration decl; 603 604 if (pr.isPointerToMember()) { 605 MemberPointerInfo mpi = pr.getMemberInfo(); 606 TypeSP class_type = GetOrCreateType(mpi.ContainingType); 607 608 CompilerType ct = ClangASTContext::CreateMemberPointerType( 609 class_type->GetLayoutCompilerType(), pointee_ct); 610 611 return std::make_shared<Type>( 612 type_uid.toOpaqueId(), m_clang->GetSymbolFile(), ConstString(), 613 pr.getSize(), nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, decl, ct, 614 Type::eResolveStateFull); 615 } 616 617 CompilerType pointer_ct = pointee_ct; 618 if (pr.getMode() == PointerMode::LValueReference) 619 pointer_ct = pointer_ct.GetLValueReferenceType(); 620 else if (pr.getMode() == PointerMode::RValueReference) 621 pointer_ct = pointer_ct.GetRValueReferenceType(); 622 else 623 pointer_ct = pointer_ct.GetPointerType(); 624 625 if ((pr.getOptions() & PointerOptions::Const) != PointerOptions::None) 626 pointer_ct = pointer_ct.AddConstModifier(); 627 628 if ((pr.getOptions() & PointerOptions::Volatile) != PointerOptions::None) 629 pointer_ct = pointer_ct.AddVolatileModifier(); 630 631 if ((pr.getOptions() & PointerOptions::Restrict) != PointerOptions::None) 632 pointer_ct = pointer_ct.AddRestrictModifier(); 633 634 return std::make_shared<Type>(type_uid.toOpaqueId(), m_clang->GetSymbolFile(), 635 ConstString(), pr.getSize(), nullptr, 636 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, 637 pointer_ct, Type::eResolveStateFull); 638 } 639 640 lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti) { 641 if (ti.getSimpleMode() != SimpleTypeMode::Direct) { 642 PdbSymUid uid = 643 PdbSymUid::makeTypeSymId(PDB_SymType::PointerType, ti, false); 644 TypeSP direct_sp = GetOrCreateType(ti.makeDirect()); 645 CompilerType ct = direct_sp->GetFullCompilerType(); 646 ct = ct.GetPointerType(); 647 uint32_t pointer_size = 4; 648 switch (ti.getSimpleMode()) { 649 case SimpleTypeMode::FarPointer32: 650 case SimpleTypeMode::NearPointer32: 651 pointer_size = 4; 652 break; 653 case SimpleTypeMode::NearPointer64: 654 pointer_size = 8; 655 break; 656 default: 657 // 128-bit and 16-bit pointers unsupported. 658 return nullptr; 659 } 660 Declaration decl; 661 return std::make_shared<Type>(uid.toOpaqueId(), m_clang->GetSymbolFile(), 662 ConstString(), pointer_size, nullptr, 663 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, 664 ct, Type::eResolveStateFull); 665 } 666 667 PdbSymUid uid = PdbSymUid::makeTypeSymId(PDB_SymType::BuiltinType, ti, false); 668 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated) 669 return nullptr; 670 671 lldb::BasicType bt = GetCompilerTypeForSimpleKind(ti.getSimpleKind()); 672 lldbassert(bt != lldb::eBasicTypeInvalid); 673 CompilerType ct = m_clang->GetBasicType(bt); 674 size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind()); 675 676 llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind()); 677 678 Declaration decl; 679 return std::make_shared<Type>(uid.toOpaqueId(), m_clang->GetSymbolFile(), 680 ConstString(type_name), size, nullptr, 681 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, 682 ct, Type::eResolveStateFull); 683 } 684 685 lldb::TypeSP SymbolFileNativePDB::CreateClassStructUnion( 686 PdbSymUid type_uid, llvm::StringRef name, size_t size, 687 clang::TagTypeKind ttk, clang::MSInheritanceAttr::Spelling inheritance) { 688 689 // Some UDT with trival ctor has zero length. Just ignore. 690 if (size == 0) 691 return nullptr; 692 693 // Ignore unnamed-tag UDTs. 694 name = DropNameScope(name); 695 if (name.empty()) 696 return nullptr; 697 698 clang::DeclContext *decl_context = m_clang->GetTranslationUnitDecl(); 699 700 lldb::AccessType access = 701 (ttk == clang::TTK_Class) ? lldb::eAccessPrivate : lldb::eAccessPublic; 702 703 ClangASTMetadata metadata; 704 metadata.SetUserID(type_uid.toOpaqueId()); 705 metadata.SetIsDynamicCXXType(false); 706 707 CompilerType ct = 708 m_clang->CreateRecordType(decl_context, access, name.str().c_str(), ttk, 709 lldb::eLanguageTypeC_plus_plus, &metadata); 710 lldbassert(ct.IsValid()); 711 712 clang::CXXRecordDecl *record_decl = 713 m_clang->GetAsCXXRecordDecl(ct.GetOpaqueQualType()); 714 lldbassert(record_decl); 715 716 clang::MSInheritanceAttr *attr = clang::MSInheritanceAttr::CreateImplicit( 717 *m_clang->getASTContext(), inheritance); 718 record_decl->addAttr(attr); 719 720 ClangASTContext::StartTagDeclarationDefinition(ct); 721 722 // Even if it's possible, don't complete it at this point. Just mark it 723 // forward resolved, and if/when LLDB needs the full definition, it can 724 // ask us. 725 ClangASTContext::SetHasExternalStorage(ct.GetOpaqueQualType(), true); 726 727 // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE. 728 Declaration decl; 729 return std::make_shared<Type>(type_uid.toOpaqueId(), m_clang->GetSymbolFile(), 730 ConstString(name), size, nullptr, 731 LLDB_INVALID_UID, Type::eEncodingIsUID, decl, 732 ct, Type::eResolveStateForward); 733 } 734 735 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbSymUid type_uid, 736 const ClassRecord &cr) { 737 clang::TagTypeKind ttk = TranslateUdtKind(cr); 738 739 clang::MSInheritanceAttr::Spelling inheritance = 740 GetMSInheritance(m_index->tpi().typeCollection(), cr); 741 return CreateClassStructUnion(type_uid, cr.getName(), cr.getSize(), ttk, 742 inheritance); 743 } 744 745 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbSymUid type_uid, 746 const UnionRecord &ur) { 747 return CreateClassStructUnion( 748 type_uid, ur.getName(), ur.getSize(), clang::TTK_Union, 749 clang::MSInheritanceAttr::Spelling::Keyword_single_inheritance); 750 } 751 752 lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbSymUid type_uid, 753 const EnumRecord &er) { 754 llvm::StringRef name = DropNameScope(er.getName()); 755 756 clang::DeclContext *decl_context = m_clang->GetTranslationUnitDecl(); 757 758 Declaration decl; 759 TypeSP underlying_type = GetOrCreateType(er.UnderlyingType); 760 CompilerType enum_ct = m_clang->CreateEnumerationType( 761 name.str().c_str(), decl_context, decl, 762 underlying_type->GetFullCompilerType(), er.isScoped()); 763 764 ClangASTContext::StartTagDeclarationDefinition(enum_ct); 765 766 // We're just going to forward resolve this for now. We'll complete 767 // it only if the user requests. 768 return std::make_shared<lldb_private::Type>( 769 type_uid.toOpaqueId(), m_clang->GetSymbolFile(), ConstString(name), 770 underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID, 771 lldb_private::Type::eEncodingIsUID, decl, enum_ct, 772 lldb_private::Type::eResolveStateForward); 773 } 774 775 TypeSP SymbolFileNativePDB::CreateType(PdbSymUid type_uid) { 776 const PdbTypeSymId &tsid = type_uid.asTypeSym(); 777 TypeIndex index(tsid.index); 778 779 if (index.getIndex() < TypeIndex::FirstNonSimpleIndex) 780 return CreateSimpleType(index); 781 782 TpiStream &stream = tsid.is_ipi ? m_index->ipi() : m_index->tpi(); 783 CVType cvt = stream.getType(index); 784 785 if (cvt.kind() == LF_MODIFIER) { 786 ModifierRecord modifier; 787 llvm::cantFail( 788 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier)); 789 return CreateModifierType(type_uid, modifier); 790 } 791 792 if (cvt.kind() == LF_POINTER) { 793 PointerRecord pointer; 794 llvm::cantFail( 795 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer)); 796 return CreatePointerType(type_uid, pointer); 797 } 798 799 if (IsClassRecord(cvt.kind())) { 800 ClassRecord cr; 801 llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr)); 802 return CreateTagType(type_uid, cr); 803 } 804 805 if (cvt.kind() == LF_ENUM) { 806 EnumRecord er; 807 llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er)); 808 return CreateTagType(type_uid, er); 809 } 810 811 if (cvt.kind() == LF_UNION) { 812 UnionRecord ur; 813 llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur)); 814 return CreateTagType(type_uid, ur); 815 } 816 817 return nullptr; 818 } 819 820 TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbSymUid type_uid) { 821 // If they search for a UDT which is a forward ref, try and resolve the full 822 // decl and just map the forward ref uid to the full decl record. 823 llvm::Optional<PdbSymUid> full_decl_uid; 824 if (type_uid.tag() == PDB_SymType::UDT || 825 type_uid.tag() == PDB_SymType::Enum) { 826 const PdbTypeSymId &type_id = type_uid.asTypeSym(); 827 TypeIndex ti(type_id.index); 828 lldbassert(!ti.isSimple()); 829 CVType cvt = m_index->tpi().getType(ti); 830 831 if (IsForwardRefUdt(cvt)) { 832 auto expected_full_ti = m_index->tpi().findFullDeclForForwardRef(ti); 833 if (!expected_full_ti) 834 llvm::consumeError(expected_full_ti.takeError()); 835 else { 836 full_decl_uid = PdbSymUid::makeTypeSymId( 837 type_uid.tag(), *expected_full_ti, type_id.is_ipi); 838 839 // It's possible that a lookup would occur for the full decl causing it 840 // to be cached, then a second lookup would occur for the forward decl. 841 // We don't want to create a second full decl, so make sure the full 842 // decl hasn't already been cached. 843 auto full_iter = m_types.find(full_decl_uid->toOpaqueId()); 844 if (full_iter != m_types.end()) { 845 TypeSP result = full_iter->second; 846 // Map the forward decl to the TypeSP for the full decl so we can take 847 // the fast path next time. 848 m_types[type_uid.toOpaqueId()] = result; 849 return result; 850 } 851 } 852 } 853 } 854 855 PdbSymUid best_uid = full_decl_uid ? *full_decl_uid : type_uid; 856 TypeSP result = CreateType(best_uid); 857 m_types[best_uid.toOpaqueId()] = result; 858 // If we had both a forward decl and a full decl, make both point to the new 859 // type. 860 if (full_decl_uid) 861 m_types[type_uid.toOpaqueId()] = result; 862 863 const PdbTypeSymId &type_id = best_uid.asTypeSym(); 864 if (best_uid.tag() == PDB_SymType::UDT || 865 best_uid.tag() == PDB_SymType::Enum) { 866 clang::TagDecl *record_decl = 867 m_clang->GetAsTagDecl(result->GetForwardCompilerType()); 868 lldbassert(record_decl); 869 870 TypeIndex ti(type_id.index); 871 CVType cvt = m_index->tpi().getType(ti); 872 m_uid_to_decl[best_uid.toOpaqueId()] = record_decl; 873 m_decl_to_status[record_decl] = 874 DeclStatus(best_uid.toOpaqueId(), Type::eResolveStateForward); 875 } 876 return result; 877 } 878 879 TypeSP SymbolFileNativePDB::GetOrCreateType(PdbSymUid type_uid) { 880 lldbassert(PdbSymUid::isTypeSym(type_uid.tag())); 881 // We can't use try_emplace / overwrite here because the process of creating 882 // a type could create nested types, which could invalidate iterators. So 883 // we have to do a 2-phase lookup / insert. 884 auto iter = m_types.find(type_uid.toOpaqueId()); 885 if (iter != m_types.end()) 886 return iter->second; 887 888 return CreateAndCacheType(type_uid); 889 } 890 891 lldb::TypeSP 892 SymbolFileNativePDB::GetOrCreateType(llvm::codeview::TypeIndex ti) { 893 PDB_SymType pdbst = GetPdbSymType(m_index->tpi(), ti); 894 PdbSymUid tuid = PdbSymUid::makeTypeSymId(pdbst, ti, false); 895 return GetOrCreateType(tuid); 896 } 897 898 FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbSymUid func_uid, 899 const SymbolContext &sc) { 900 lldbassert(func_uid.tag() == PDB_SymType::Function); 901 auto emplace_result = m_functions.try_emplace(func_uid.toOpaqueId(), nullptr); 902 if (emplace_result.second) 903 emplace_result.first->second = CreateFunction(func_uid, sc); 904 905 lldbassert(emplace_result.first->second); 906 return emplace_result.first->second; 907 } 908 909 CompUnitSP 910 SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) { 911 auto emplace_result = 912 m_compilands.try_emplace(cci.m_uid.toOpaqueId(), nullptr); 913 if (emplace_result.second) 914 emplace_result.first->second = CreateCompileUnit(cci); 915 916 lldbassert(emplace_result.first->second); 917 return emplace_result.first->second; 918 } 919 920 lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) { 921 if (index >= GetNumCompileUnits()) 922 return CompUnitSP(); 923 lldbassert(index < UINT16_MAX); 924 if (index >= UINT16_MAX) 925 return nullptr; 926 927 CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index); 928 929 return GetOrCreateCompileUnit(item); 930 } 931 932 lldb::LanguageType 933 SymbolFileNativePDB::ParseCompileUnitLanguage(const SymbolContext &sc) { 934 // What fields should I expect to be filled out on the SymbolContext? Is it 935 // safe to assume that `sc.comp_unit` is valid? 936 if (!sc.comp_unit) 937 return lldb::eLanguageTypeUnknown; 938 PdbSymUid uid = PdbSymUid::fromOpaqueId(sc.comp_unit->GetID()); 939 lldbassert(uid.tag() == PDB_SymType::Compiland); 940 941 CompilandIndexItem *item = m_index->compilands().GetCompiland(uid); 942 lldbassert(item); 943 if (!item->m_compile_opts) 944 return lldb::eLanguageTypeUnknown; 945 946 return TranslateLanguage(item->m_compile_opts->getLanguage()); 947 } 948 949 size_t SymbolFileNativePDB::ParseCompileUnitFunctions(const SymbolContext &sc) { 950 lldbassert(sc.comp_unit); 951 return false; 952 } 953 954 static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) { 955 // If any of these flags are set, we need to resolve the compile unit. 956 uint32_t flags = eSymbolContextCompUnit; 957 flags |= eSymbolContextVariable; 958 flags |= eSymbolContextFunction; 959 flags |= eSymbolContextBlock; 960 flags |= eSymbolContextLineEntry; 961 return (resolve_scope & flags) != 0; 962 } 963 964 uint32_t SymbolFileNativePDB::ResolveSymbolContext(const Address &addr, 965 uint32_t resolve_scope, 966 SymbolContext &sc) { 967 uint32_t resolved_flags = 0; 968 lldb::addr_t file_addr = addr.GetFileAddress(); 969 970 if (NeedsResolvedCompileUnit(resolve_scope)) { 971 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr); 972 if (!modi) 973 return 0; 974 PdbSymUid cuid = PdbSymUid::makeCompilandId(*modi); 975 CompilandIndexItem *cci = m_index->compilands().GetCompiland(cuid); 976 if (!cci) 977 return 0; 978 979 sc.comp_unit = GetOrCreateCompileUnit(*cci).get(); 980 resolved_flags |= eSymbolContextCompUnit; 981 } 982 983 if (resolve_scope & eSymbolContextFunction) { 984 lldbassert(sc.comp_unit); 985 std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr); 986 for (const auto &match : matches) { 987 if (match.uid.tag() != PDB_SymType::Function) 988 continue; 989 sc.function = GetOrCreateFunction(match.uid, sc).get(); 990 } 991 resolved_flags |= eSymbolContextFunction; 992 } 993 994 if (resolve_scope & eSymbolContextLineEntry) { 995 lldbassert(sc.comp_unit); 996 if (auto *line_table = sc.comp_unit->GetLineTable()) { 997 if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) 998 resolved_flags |= eSymbolContextLineEntry; 999 } 1000 } 1001 1002 return resolved_flags; 1003 } 1004 1005 static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence, 1006 const CompilandIndexItem &cci, 1007 lldb::addr_t base_addr, 1008 uint32_t file_number, 1009 const LineFragmentHeader &block, 1010 const LineNumberEntry &cur) { 1011 LineInfo cur_info(cur.Flags); 1012 1013 if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto()) 1014 return; 1015 1016 uint64_t addr = base_addr + cur.Offset; 1017 1018 bool is_statement = cur_info.isStatement(); 1019 bool is_prologue = IsFunctionPrologue(cci, addr); 1020 bool is_epilogue = IsFunctionEpilogue(cci, addr); 1021 1022 uint32_t lno = cur_info.getStartLine(); 1023 1024 table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number, 1025 is_statement, false, is_prologue, is_epilogue, 1026 false); 1027 } 1028 1029 static void TerminateLineSequence(LineTable &table, 1030 const LineFragmentHeader &block, 1031 lldb::addr_t base_addr, uint32_t file_number, 1032 uint32_t last_line, 1033 std::unique_ptr<LineSequence> seq) { 1034 // The end is always a terminal entry, so insert it regardless. 1035 table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize, 1036 last_line, 0, file_number, false, false, 1037 false, false, true); 1038 table.InsertSequence(seq.release()); 1039 } 1040 1041 bool SymbolFileNativePDB::ParseCompileUnitLineTable(const SymbolContext &sc) { 1042 // Unfortunately LLDB is set up to parse the entire compile unit line table 1043 // all at once, even if all it really needs is line info for a specific 1044 // function. In the future it would be nice if it could set the sc.m_function 1045 // member, and we could only get the line info for the function in question. 1046 lldbassert(sc.comp_unit); 1047 PdbSymUid cu_id = PdbSymUid::fromOpaqueId(sc.comp_unit->GetID()); 1048 lldbassert(cu_id.isCompiland()); 1049 CompilandIndexItem *cci = m_index->compilands().GetCompiland(cu_id); 1050 lldbassert(cci); 1051 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); 1052 1053 // This is basically a copy of the .debug$S subsections from all original COFF 1054 // object files merged together with address relocations applied. We are 1055 // looking for all DEBUG_S_LINES subsections. 1056 for (const DebugSubsectionRecord &dssr : 1057 cci->m_debug_stream.getSubsectionsArray()) { 1058 if (dssr.kind() != DebugSubsectionKind::Lines) 1059 continue; 1060 1061 DebugLinesSubsectionRef lines; 1062 llvm::BinaryStreamReader reader(dssr.getRecordData()); 1063 if (auto EC = lines.initialize(reader)) { 1064 llvm::consumeError(std::move(EC)); 1065 return false; 1066 } 1067 1068 const LineFragmentHeader *lfh = lines.header(); 1069 uint64_t virtual_addr = 1070 m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset); 1071 1072 const auto &checksums = cci->m_strings.checksums().getArray(); 1073 const auto &strings = cci->m_strings.strings(); 1074 for (const LineColumnEntry &group : lines) { 1075 // Indices in this structure are actually offsets of records in the 1076 // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index 1077 // into the global PDB string table. 1078 auto iter = checksums.at(group.NameIndex); 1079 if (iter == checksums.end()) 1080 continue; 1081 1082 llvm::Expected<llvm::StringRef> efn = 1083 strings.getString(iter->FileNameOffset); 1084 if (!efn) { 1085 llvm::consumeError(efn.takeError()); 1086 continue; 1087 } 1088 1089 // LLDB wants the index of the file in the list of support files. 1090 auto fn_iter = llvm::find(cci->m_file_list, *efn); 1091 lldbassert(fn_iter != cci->m_file_list.end()); 1092 uint32_t file_index = std::distance(cci->m_file_list.begin(), fn_iter); 1093 1094 std::unique_ptr<LineSequence> sequence( 1095 line_table->CreateLineSequenceContainer()); 1096 lldbassert(!group.LineNumbers.empty()); 1097 1098 for (const LineNumberEntry &entry : group.LineNumbers) { 1099 AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr, 1100 file_index, *lfh, entry); 1101 } 1102 LineInfo last_line(group.LineNumbers.back().Flags); 1103 TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index, 1104 last_line.getEndLine(), std::move(sequence)); 1105 } 1106 } 1107 1108 if (line_table->GetSize() == 0) 1109 return false; 1110 1111 sc.comp_unit->SetLineTable(line_table.release()); 1112 return true; 1113 } 1114 1115 bool SymbolFileNativePDB::ParseCompileUnitDebugMacros(const SymbolContext &sc) { 1116 // PDB doesn't contain information about macros 1117 return false; 1118 } 1119 1120 bool SymbolFileNativePDB::ParseCompileUnitSupportFiles( 1121 const SymbolContext &sc, FileSpecList &support_files) { 1122 lldbassert(sc.comp_unit); 1123 1124 PdbSymUid comp_uid = PdbSymUid::fromOpaqueId(sc.comp_unit->GetID()); 1125 lldbassert(comp_uid.tag() == PDB_SymType::Compiland); 1126 1127 const CompilandIndexItem *cci = m_index->compilands().GetCompiland(comp_uid); 1128 lldbassert(cci); 1129 1130 for (llvm::StringRef f : cci->m_file_list) { 1131 FileSpec::Style style = 1132 f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows; 1133 FileSpec spec(f, false, style); 1134 support_files.Append(spec); 1135 } 1136 1137 return true; 1138 } 1139 1140 bool SymbolFileNativePDB::ParseImportedModules( 1141 const SymbolContext &sc, std::vector<ConstString> &imported_modules) { 1142 // PDB does not yet support module debug info 1143 return false; 1144 } 1145 1146 size_t SymbolFileNativePDB::ParseFunctionBlocks(const SymbolContext &sc) { 1147 lldbassert(sc.comp_unit && sc.function); 1148 return 0; 1149 } 1150 1151 uint32_t SymbolFileNativePDB::FindFunctions( 1152 const ConstString &name, const CompilerDeclContext *parent_decl_ctx, 1153 uint32_t name_type_mask, bool include_inlines, bool append, 1154 SymbolContextList &sc_list) { 1155 // For now we only support lookup by method name. 1156 if (!(name_type_mask & eFunctionNameTypeMethod)) 1157 return 0; 1158 1159 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; 1160 1161 std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName( 1162 name.GetStringRef(), m_index->symrecords()); 1163 for (const SymbolAndOffset &match : matches) { 1164 if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF) 1165 continue; 1166 ProcRefSym proc(match.second.kind()); 1167 cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc)); 1168 1169 if (!IsValidRecord(proc)) 1170 continue; 1171 1172 PdbSymUid cuid = PdbSymUid::makeCompilandId(proc); 1173 CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(cuid); 1174 SymbolContext sc; 1175 1176 sc.comp_unit = GetOrCreateCompileUnit(cci).get(); 1177 sc.module_sp = sc.comp_unit->GetModule(); 1178 PdbSymUid func_uid = PdbSymUid::makeCuSymId(proc); 1179 sc.function = GetOrCreateFunction(func_uid, sc).get(); 1180 1181 sc_list.Append(sc); 1182 } 1183 1184 return sc_list.GetSize(); 1185 } 1186 1187 uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression ®ex, 1188 bool include_inlines, bool append, 1189 SymbolContextList &sc_list) { 1190 return 0; 1191 } 1192 1193 uint32_t SymbolFileNativePDB::FindTypes( 1194 const SymbolContext &sc, const ConstString &name, 1195 const CompilerDeclContext *parent_decl_ctx, bool append, 1196 uint32_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files, 1197 TypeMap &types) { 1198 if (!append) 1199 types.Clear(); 1200 if (!name) 1201 return 0; 1202 1203 searched_symbol_files.clear(); 1204 searched_symbol_files.insert(this); 1205 1206 // There is an assumption 'name' is not a regex 1207 size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types); 1208 1209 return match_count; 1210 } 1211 1212 size_t 1213 SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context, 1214 bool append, TypeMap &types) { 1215 return 0; 1216 } 1217 1218 size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name, 1219 uint32_t max_matches, 1220 TypeMap &types) { 1221 1222 size_t match_count = 0; 1223 std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name); 1224 if (max_matches > 0 && max_matches < matches.size()) 1225 matches.resize(max_matches); 1226 1227 for (TypeIndex ti : matches) { 1228 TypeSP type = GetOrCreateType(ti); 1229 if (!type) 1230 continue; 1231 1232 types.Insert(type); 1233 ++match_count; 1234 } 1235 return match_count; 1236 } 1237 1238 size_t SymbolFileNativePDB::ParseTypes(const SymbolContext &sc) { return 0; } 1239 1240 Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) { 1241 auto iter = m_types.find(type_uid); 1242 // lldb should not be passing us non-sensical type uids. the only way it 1243 // could have a type uid in the first place is if we handed it out, in which 1244 // case we should know about the type. So this is not a get-or-create type 1245 // operation, it is strictly a get, and the type is guaranteed to exist. 1246 // 1247 // However, since the implementation is not yet complete, we don't currently 1248 // support all possible use cases. For example, we currently create all 1249 // functions with indices of 0 for the signature type simply because this is 1250 // not yet implemented. At the time the function object is created we should 1251 // be creating an lldb::TypeSP for this, adding it to the m_types, and 1252 // returning a valid Type object for it and putting it in this map. Once all 1253 // cases like this are handled, we can promote this to an assert. 1254 if (iter == m_types.end()) 1255 return nullptr; 1256 return &*iter->second; 1257 } 1258 1259 bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) { 1260 // If this is not in our map, it's an error. 1261 clang::TagDecl *tag_decl = m_clang->GetAsTagDecl(compiler_type); 1262 lldbassert(tag_decl); 1263 auto status_iter = m_decl_to_status.find(tag_decl); 1264 lldbassert(status_iter != m_decl_to_status.end()); 1265 1266 // If it's already complete, just return. 1267 DeclStatus &status = status_iter->second; 1268 if (status.status == Type::eResolveStateFull) 1269 return true; 1270 1271 PdbSymUid uid = PdbSymUid::fromOpaqueId(status.uid); 1272 lldbassert(uid.tag() == PDB_SymType::UDT || uid.tag() == PDB_SymType::Enum); 1273 1274 const PdbTypeSymId &type_id = uid.asTypeSym(); 1275 1276 ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(), 1277 false); 1278 1279 // In CreateAndCacheType, we already go out of our way to resolve forward 1280 // ref UDTs to full decls, and the uids we vend out always refer to full 1281 // decls if a full decl exists in the debug info. So if we don't have a full 1282 // decl here, it means one doesn't exist in the debug info, and we can't 1283 // complete the type. 1284 CVType cvt = m_index->tpi().getType(TypeIndex(type_id.index)); 1285 if (IsForwardRefUdt(cvt)) 1286 return false; 1287 1288 auto types_iter = m_types.find(uid.toOpaqueId()); 1289 lldbassert(types_iter != m_types.end()); 1290 1291 TypeIndex field_list_ti = GetFieldListIndex(cvt); 1292 CVType field_list_cvt = m_index->tpi().getType(field_list_ti); 1293 if (field_list_cvt.kind() != LF_FIELDLIST) 1294 return false; 1295 1296 // Visit all members of this class, then perform any finalization necessary 1297 // to complete the class. 1298 UdtRecordCompleter completer(uid, compiler_type, *tag_decl, *this); 1299 auto error = 1300 llvm::codeview::visitMemberRecordStream(field_list_cvt.data(), completer); 1301 completer.complete(); 1302 1303 status.status = Type::eResolveStateFull; 1304 if (!error) 1305 return true; 1306 1307 llvm::consumeError(std::move(error)); 1308 return false; 1309 } 1310 1311 size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, 1312 uint32_t type_mask, 1313 lldb_private::TypeList &type_list) { 1314 return 0; 1315 } 1316 1317 CompilerDeclContext 1318 SymbolFileNativePDB::FindNamespace(const SymbolContext &sc, 1319 const ConstString &name, 1320 const CompilerDeclContext *parent_decl_ctx) { 1321 return {}; 1322 } 1323 1324 TypeSystem * 1325 SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { 1326 auto type_system = 1327 m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 1328 if (type_system) 1329 type_system->SetSymbolFile(this); 1330 return type_system; 1331 } 1332 1333 ConstString SymbolFileNativePDB::GetPluginName() { 1334 static ConstString g_name("pdb"); 1335 return g_name; 1336 } 1337 1338 uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; } 1339