1 //===-- SymbolFileNativePDB.cpp -------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "SymbolFileNativePDB.h"
11 #include "Plugins/ExpressionParser/Clang/ClangUtil.h"
12 #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
13 #include "Plugins/ObjectFile/PDB/ObjectFilePDB.h"
14 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.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/Variable.h"
23 #include "lldb/Symbol/VariableList.h"
24 #include "lldb/Utility/LLDBLog.h"
25 #include "lldb/Utility/Log.h"
27 #include "llvm/DebugInfo/CodeView/CVRecord.h"
28 #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
29 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
30 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
31 #include "llvm/DebugInfo/CodeView/RecordName.h"
32 #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
33 #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.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/NativeSession.h"
40 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
41 #include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
42 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
43 #include "llvm/DebugInfo/PDB/PDB.h"
44 #include "llvm/DebugInfo/PDB/PDBTypes.h"
45 #include "llvm/Demangle/MicrosoftDemangle.h"
46 #include "llvm/Object/COFF.h"
47 #include "llvm/Support/Allocator.h"
48 #include "llvm/Support/BinaryStreamReader.h"
49 #include "llvm/Support/Error.h"
50 #include "llvm/Support/ErrorOr.h"
51 #include "llvm/Support/MemoryBuffer.h"
53 #include "DWARFLocationExpression.h"
54 #include "PdbSymUid.h"
56 #include "UdtRecordCompleter.h"
58 #include <string_view>
61 using namespace lldb_private
;
63 using namespace llvm::codeview
;
64 using namespace llvm::pdb
;
66 char SymbolFileNativePDB::ID
;
68 static lldb::LanguageType
TranslateLanguage(PDB_Lang lang
) {
71 return lldb::LanguageType::eLanguageTypeC_plus_plus
;
73 return lldb::LanguageType::eLanguageTypeC
;
75 return lldb::LanguageType::eLanguageTypeSwift
;
77 return lldb::LanguageType::eLanguageTypeRust
;
79 return lldb::LanguageType::eLanguageTypeObjC
;
80 case PDB_Lang::ObjCpp
:
81 return lldb::LanguageType::eLanguageTypeObjC_plus_plus
;
83 return lldb::LanguageType::eLanguageTypeUnknown
;
87 static std::unique_ptr
<PDBFile
>
88 loadMatchingPDBFile(std::string exe_path
, llvm::BumpPtrAllocator
&allocator
) {
89 // Try to find a matching PDB for an EXE.
90 using namespace llvm::object
;
91 auto expected_binary
= createBinary(exe_path
);
93 // If the file isn't a PE/COFF executable, fail.
94 if (!expected_binary
) {
95 llvm::consumeError(expected_binary
.takeError());
98 OwningBinary
<Binary
> binary
= std::move(*expected_binary
);
100 // TODO: Avoid opening the PE/COFF binary twice by reading this information
101 // directly from the lldb_private::ObjectFile.
102 auto *obj
= llvm::dyn_cast
<llvm::object::COFFObjectFile
>(binary
.getBinary());
105 const llvm::codeview::DebugInfo
*pdb_info
= nullptr;
107 // If it doesn't have a debug directory, fail.
108 llvm::StringRef pdb_file
;
109 if (llvm::Error e
= obj
->getDebugPDBInfo(pdb_info
, pdb_file
)) {
110 consumeError(std::move(e
));
114 // If the file doesn't exist, perhaps the path specified at build time
115 // doesn't match the PDB's current location, so check the location of the
117 if (!FileSystem::Instance().Exists(pdb_file
)) {
118 const auto exe_dir
= FileSpec(exe_path
).CopyByRemovingLastPathComponent();
119 const auto pdb_name
= FileSpec(pdb_file
).GetFilename().GetCString();
120 pdb_file
= exe_dir
.CopyByAppendingPathComponent(pdb_name
).GetPathAsConstString().GetStringRef();
123 // If the file is not a PDB or if it doesn't have a matching GUID, fail.
124 auto pdb
= ObjectFilePDB::loadPDBFile(std::string(pdb_file
), allocator
);
128 auto expected_info
= pdb
->getPDBInfoStream();
129 if (!expected_info
) {
130 llvm::consumeError(expected_info
.takeError());
133 llvm::codeview::GUID guid
;
134 memcpy(&guid
, pdb_info
->PDB70
.Signature
, 16);
136 if (expected_info
->getGuid() != guid
)
141 static bool IsFunctionPrologue(const CompilandIndexItem
&cci
,
143 // FIXME: Implement this.
147 static bool IsFunctionEpilogue(const CompilandIndexItem
&cci
,
149 // FIXME: Implement this.
153 static llvm::StringRef
GetSimpleTypeName(SimpleTypeKind kind
) {
155 case SimpleTypeKind::Boolean128
:
156 case SimpleTypeKind::Boolean16
:
157 case SimpleTypeKind::Boolean32
:
158 case SimpleTypeKind::Boolean64
:
159 case SimpleTypeKind::Boolean8
:
161 case SimpleTypeKind::Byte
:
162 case SimpleTypeKind::UnsignedCharacter
:
163 return "unsigned char";
164 case SimpleTypeKind::NarrowCharacter
:
166 case SimpleTypeKind::SignedCharacter
:
167 case SimpleTypeKind::SByte
:
168 return "signed char";
169 case SimpleTypeKind::Character16
:
171 case SimpleTypeKind::Character32
:
173 case SimpleTypeKind::Character8
:
175 case SimpleTypeKind::Complex80
:
176 case SimpleTypeKind::Complex64
:
177 case SimpleTypeKind::Complex32
:
179 case SimpleTypeKind::Float128
:
180 case SimpleTypeKind::Float80
:
181 return "long double";
182 case SimpleTypeKind::Float64
:
184 case SimpleTypeKind::Float32
:
186 case SimpleTypeKind::Float16
:
188 case SimpleTypeKind::Int128
:
190 case SimpleTypeKind::Int64
:
191 case SimpleTypeKind::Int64Quad
:
193 case SimpleTypeKind::Int32
:
195 case SimpleTypeKind::Int16
:
197 case SimpleTypeKind::UInt128
:
198 return "unsigned __int128";
199 case SimpleTypeKind::UInt64
:
200 case SimpleTypeKind::UInt64Quad
:
202 case SimpleTypeKind::HResult
:
204 case SimpleTypeKind::UInt32
:
206 case SimpleTypeKind::UInt16
:
207 case SimpleTypeKind::UInt16Short
:
208 return "unsigned short";
209 case SimpleTypeKind::Int32Long
:
211 case SimpleTypeKind::UInt32Long
:
212 return "unsigned long";
213 case SimpleTypeKind::Void
:
215 case SimpleTypeKind::WideCharacter
:
222 static bool IsClassRecord(TypeLeafKind kind
) {
233 static std::optional
<CVTagRecord
>
234 GetNestedTagDefinition(const NestedTypeRecord
&Record
,
235 const CVTagRecord
&parent
, TpiStream
&tpi
) {
236 // An LF_NESTTYPE is essentially a nested typedef / using declaration, but it
237 // is also used to indicate the primary definition of a nested class. That is
238 // to say, if you have:
243 // Then in the debug info, this will appear as:
244 // LF_STRUCTURE `A::B` [type index = N]
246 // LF_NESTTYPE [name = `B`, index = N]
247 // LF_NESTTYPE [name = `C`, index = N]
248 // In order to accurately reconstruct the decl context hierarchy, we need to
249 // know which ones are actual definitions and which ones are just aliases.
251 // If it's a simple type, then this is something like `using foo = int`.
252 if (Record
.Type
.isSimple())
255 CVType cvt
= tpi
.getType(Record
.Type
);
257 if (!IsTagRecord(cvt
))
260 // If it's an inner definition, then treat whatever name we have here as a
261 // single component of a mangled name. So we can inject it into the parent's
262 // mangled name to see if it matches.
263 CVTagRecord child
= CVTagRecord::create(cvt
);
264 std::string qname
= std::string(parent
.asTag().getUniqueName());
265 if (qname
.size() < 4 || child
.asTag().getUniqueName().size() < 4)
268 // qname[3] is the tag type identifier (struct, class, union, etc). Since the
269 // inner tag type is not necessarily the same as the outer tag type, re-write
270 // it to match the inner tag type.
271 qname
[3] = child
.asTag().getUniqueName()[3];
275 piece
+= Record
.Name
;
276 piece
.push_back('@');
277 qname
.insert(4, std::move(piece
));
278 if (qname
!= child
.asTag().UniqueName
)
281 return std::move(child
);
284 void SymbolFileNativePDB::Initialize() {
285 PluginManager::RegisterPlugin(GetPluginNameStatic(),
286 GetPluginDescriptionStatic(), CreateInstance
,
290 void SymbolFileNativePDB::Terminate() {
291 PluginManager::UnregisterPlugin(CreateInstance
);
294 void SymbolFileNativePDB::DebuggerInitialize(Debugger
&debugger
) {}
296 llvm::StringRef
SymbolFileNativePDB::GetPluginDescriptionStatic() {
297 return "Microsoft PDB debug symbol cross-platform file reader.";
300 SymbolFile
*SymbolFileNativePDB::CreateInstance(ObjectFileSP objfile_sp
) {
301 return new SymbolFileNativePDB(std::move(objfile_sp
));
304 SymbolFileNativePDB::SymbolFileNativePDB(ObjectFileSP objfile_sp
)
305 : SymbolFileCommon(std::move(objfile_sp
)) {}
307 SymbolFileNativePDB::~SymbolFileNativePDB() = default;
309 uint32_t SymbolFileNativePDB::CalculateAbilities() {
310 uint32_t abilities
= 0;
315 // Lazily load and match the PDB file, but only do this once.
317 if (auto *pdb
= llvm::dyn_cast
<ObjectFilePDB
>(m_objfile_sp
.get())) {
318 pdb_file
= &pdb
->GetPDBFile();
320 m_file_up
= loadMatchingPDBFile(m_objfile_sp
->GetFileSpec().GetPath(),
322 pdb_file
= m_file_up
.get();
328 auto expected_index
= PdbIndex::create(pdb_file
);
329 if (!expected_index
) {
330 llvm::consumeError(expected_index
.takeError());
333 m_index
= std::move(*expected_index
);
338 // We don't especially have to be precise here. We only distinguish between
339 // stripped and not stripped.
340 abilities
= kAllAbilities
;
342 if (m_index
->dbi().isStripped())
343 abilities
&= ~(Blocks
| LocalVariables
);
347 void SymbolFileNativePDB::InitializeObject() {
348 m_obj_load_address
= m_objfile_sp
->GetModule()
352 m_index
->SetLoadAddress(m_obj_load_address
);
353 m_index
->ParseSectionContribs();
355 auto ts_or_err
= m_objfile_sp
->GetModule()->GetTypeSystemForLanguage(
356 lldb::eLanguageTypeC_plus_plus
);
357 if (auto err
= ts_or_err
.takeError()) {
358 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols
), std::move(err
),
359 "Failed to initialize: {0}");
361 if (auto ts
= *ts_or_err
)
362 ts
->SetSymbolFile(this);
367 uint32_t SymbolFileNativePDB::CalculateNumCompileUnits() {
368 const DbiModuleList
&modules
= m_index
->dbi().modules();
369 uint32_t count
= modules
.getModuleCount();
373 // The linker can inject an additional "dummy" compilation unit into the
374 // PDB. Ignore this special compile unit for our purposes, if it is there.
375 // It is always the last one.
376 DbiModuleDescriptor last
= modules
.getModuleDescriptor(count
- 1);
377 if (last
.getModuleName() == "* Linker *")
382 Block
*SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id
) {
383 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(block_id
.modi
);
384 CVSymbol sym
= cii
->m_debug_stream
.readSymbolAtOffset(block_id
.offset
);
385 CompUnitSP comp_unit
= GetOrCreateCompileUnit(*cii
);
386 lldb::user_id_t opaque_block_uid
= toOpaqueUid(block_id
);
387 auto ts_or_err
= GetTypeSystemForLanguage(comp_unit
->GetLanguage());
388 if (auto err
= ts_or_err
.takeError())
390 auto ts
= *ts_or_err
;
393 PdbAstBuilder
* ast_builder
= ts
->GetNativePDBParser();
395 switch (sym
.kind()) {
398 // This is a function. It must be global. Creating the Function entry
399 // for it automatically creates a block for it.
400 FunctionSP func
= GetOrCreateFunction(block_id
, *comp_unit
);
402 Block
&block
= func
->GetBlock(false);
403 if (block
.GetNumRanges() == 0)
404 block
.AddRange(Block::Range(0, func
->GetAddressRange().GetByteSize()));
410 // This is a block. Its parent is either a function or another block. In
411 // either case, its parent can be viewed as a block (e.g. a function
412 // contains 1 big block. So just get the parent block and add this block
414 BlockSym
block(static_cast<SymbolRecordKind
>(sym
.kind()));
415 cantFail(SymbolDeserializer::deserializeAs
<BlockSym
>(sym
, block
));
416 lldbassert(block
.Parent
!= 0);
417 PdbCompilandSymId
parent_id(block_id
.modi
, block
.Parent
);
418 Block
*parent_block
= GetOrCreateBlock(parent_id
);
421 Function
*func
= parent_block
->CalculateSymbolContextFunction();
423 lldb::addr_t block_base
=
424 m_index
->MakeVirtualAddress(block
.Segment
, block
.CodeOffset
);
425 lldb::addr_t func_base
=
426 func
->GetAddressRange().GetBaseAddress().GetFileAddress();
427 BlockSP child_block
= parent_block
->CreateChild(opaque_block_uid
);
428 if (block_base
>= func_base
)
429 child_block
->AddRange(Block::Range(block_base
- func_base
, block
.CodeSize
));
431 GetObjectFile()->GetModule()->ReportError(
432 "S_BLOCK32 at modi: {0:d} offset: {1:d}: adding range "
433 "[{2:x16}-{3:x16}) which has a base that is less than the "
435 "low PC 0x%" PRIx64
". Please file a bug and attach the file at the "
436 "start of this error message",
437 block_id
.modi
, block_id
.offset
, block_base
,
438 block_base
+ block
.CodeSize
, func_base
);
440 ast_builder
->GetOrCreateBlockDecl(block_id
);
441 m_blocks
.insert({opaque_block_uid
, child_block
});
445 // This ensures line table is parsed first so we have inline sites info.
446 comp_unit
->GetLineTable();
448 std::shared_ptr
<InlineSite
> inline_site
= m_inline_sites
[opaque_block_uid
];
449 Block
*parent_block
= GetOrCreateBlock(inline_site
->parent_id
);
452 BlockSP child_block
= parent_block
->CreateChild(opaque_block_uid
);
453 ast_builder
->GetOrCreateInlinedFunctionDecl(block_id
);
454 // Copy ranges from InlineSite to Block.
455 for (size_t i
= 0; i
< inline_site
->ranges
.GetSize(); ++i
) {
456 auto *entry
= inline_site
->ranges
.GetEntryAtIndex(i
);
457 child_block
->AddRange(
458 Block::Range(entry
->GetRangeBase(), entry
->GetByteSize()));
460 child_block
->FinalizeRanges();
462 // Get the inlined function callsite info.
463 Declaration
&decl
= inline_site
->inline_function_info
->GetDeclaration();
464 Declaration
&callsite
= inline_site
->inline_function_info
->GetCallSite();
465 child_block
->SetInlinedFunctionInfo(
466 inline_site
->inline_function_info
->GetName().GetCString(), nullptr,
468 m_blocks
.insert({opaque_block_uid
, child_block
});
472 lldbassert(false && "Symbol is not a block!");
478 lldb::FunctionSP
SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id
,
479 CompileUnit
&comp_unit
) {
480 const CompilandIndexItem
*cci
=
481 m_index
->compilands().GetCompiland(func_id
.modi
);
483 CVSymbol sym_record
= cci
->m_debug_stream
.readSymbolAtOffset(func_id
.offset
);
485 lldbassert(sym_record
.kind() == S_LPROC32
|| sym_record
.kind() == S_GPROC32
);
486 SegmentOffsetLength sol
= GetSegmentOffsetAndLength(sym_record
);
489 m_index
->MakeVirtualAddress(sol
.so
.segment
, sol
.so
.offset
);
490 if (file_vm_addr
== LLDB_INVALID_ADDRESS
|| file_vm_addr
== 0)
493 AddressRange
func_range(file_vm_addr
, sol
.length
,
494 comp_unit
.GetModule()->GetSectionList());
495 if (!func_range
.GetBaseAddress().IsValid())
498 ProcSym
proc(static_cast<SymbolRecordKind
>(sym_record
.kind()));
499 cantFail(SymbolDeserializer::deserializeAs
<ProcSym
>(sym_record
, proc
));
500 if (proc
.FunctionType
== TypeIndex::None())
502 TypeSP func_type
= GetOrCreateType(proc
.FunctionType
);
506 PdbTypeSymId
sig_id(proc
.FunctionType
, false);
507 Mangled
mangled(proc
.Name
);
508 FunctionSP func_sp
= std::make_shared
<Function
>(
509 &comp_unit
, toOpaqueUid(func_id
), toOpaqueUid(sig_id
), mangled
,
510 func_type
.get(), AddressRanges
{func_range
});
512 comp_unit
.AddFunction(func_sp
);
514 auto ts_or_err
= GetTypeSystemForLanguage(comp_unit
.GetLanguage());
515 if (auto err
= ts_or_err
.takeError())
517 auto ts
= *ts_or_err
;
520 ts
->GetNativePDBParser()->GetOrCreateFunctionDecl(func_id
);
526 SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem
&cci
) {
527 lldb::LanguageType lang
=
528 cci
.m_compile_opts
? TranslateLanguage(cci
.m_compile_opts
->getLanguage())
529 : lldb::eLanguageTypeUnknown
;
531 LazyBool optimized
= eLazyBoolNo
;
532 if (cci
.m_compile_opts
&& cci
.m_compile_opts
->hasOptimizations())
533 optimized
= eLazyBoolYes
;
535 llvm::SmallString
<64> source_file_name
=
536 m_index
->compilands().GetMainSourceFile(cci
);
537 FileSpec
fs(llvm::sys::path::convert_to_slash(
538 source_file_name
, llvm::sys::path::Style::windows_backslash
));
540 CompUnitSP cu_sp
= std::make_shared
<CompileUnit
>(
541 m_objfile_sp
->GetModule(), nullptr, std::make_shared
<SupportFile
>(fs
),
542 toOpaqueUid(cci
.m_id
), lang
, optimized
);
544 SetCompileUnitAtIndex(cci
.m_id
.modi
, cu_sp
);
548 lldb::TypeSP
SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id
,
549 const ModifierRecord
&mr
,
551 TpiStream
&stream
= m_index
->tpi();
554 if (mr
.ModifiedType
.isSimple())
555 name
= std::string(GetSimpleTypeName(mr
.ModifiedType
.getSimpleKind()));
557 name
= computeTypeName(stream
.typeCollection(), mr
.ModifiedType
);
559 lldb::TypeSP modified_type
= GetOrCreateType(mr
.ModifiedType
);
561 return MakeType(toOpaqueUid(type_id
), ConstString(name
),
562 modified_type
->GetByteSize(nullptr), nullptr,
563 LLDB_INVALID_UID
, Type::eEncodingIsUID
, decl
, ct
,
564 Type::ResolveState::Full
);
568 SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id
,
569 const llvm::codeview::PointerRecord
&pr
,
571 TypeSP pointee
= GetOrCreateType(pr
.ReferentType
);
575 if (pr
.isPointerToMember()) {
576 MemberPointerInfo mpi
= pr
.getMemberInfo();
577 GetOrCreateType(mpi
.ContainingType
);
581 return MakeType(toOpaqueUid(type_id
), ConstString(), pr
.getSize(), nullptr,
582 LLDB_INVALID_UID
, Type::eEncodingIsUID
, decl
, ct
,
583 Type::ResolveState::Full
);
586 lldb::TypeSP
SymbolFileNativePDB::CreateSimpleType(TypeIndex ti
,
588 uint64_t uid
= toOpaqueUid(PdbTypeSymId(ti
, false));
589 if (ti
== TypeIndex::NullptrT()) {
591 return MakeType(uid
, ConstString("std::nullptr_t"), 0, nullptr,
592 LLDB_INVALID_UID
, Type::eEncodingIsUID
, decl
, ct
,
593 Type::ResolveState::Full
);
596 if (ti
.getSimpleMode() != SimpleTypeMode::Direct
) {
597 TypeSP direct_sp
= GetOrCreateType(ti
.makeDirect());
598 uint32_t pointer_size
= 0;
599 switch (ti
.getSimpleMode()) {
600 case SimpleTypeMode::FarPointer32
:
601 case SimpleTypeMode::NearPointer32
:
604 case SimpleTypeMode::NearPointer64
:
608 // 128-bit and 16-bit pointers unsupported.
612 return MakeType(uid
, ConstString(), pointer_size
, nullptr, LLDB_INVALID_UID
,
613 Type::eEncodingIsUID
, decl
, ct
, Type::ResolveState::Full
);
616 if (ti
.getSimpleKind() == SimpleTypeKind::NotTranslated
)
619 size_t size
= GetTypeSizeForSimpleKind(ti
.getSimpleKind());
620 llvm::StringRef type_name
= GetSimpleTypeName(ti
.getSimpleKind());
623 return MakeType(uid
, ConstString(type_name
), size
, nullptr, LLDB_INVALID_UID
,
624 Type::eEncodingIsUID
, decl
, ct
, Type::ResolveState::Full
);
627 static std::string
GetUnqualifiedTypeName(const TagRecord
&record
) {
628 if (!record
.hasUniqueName()) {
629 MSVCUndecoratedNameParser
parser(record
.Name
);
630 llvm::ArrayRef
<MSVCUndecoratedNameSpecifier
> specs
= parser
.GetSpecifiers();
632 return std::string(specs
.back().GetBaseName());
635 llvm::ms_demangle::Demangler demangler
;
636 std::string_view
sv(record
.UniqueName
.begin(), record
.UniqueName
.size());
637 llvm::ms_demangle::TagTypeNode
*ttn
= demangler
.parseTagUniqueName(sv
);
639 return std::string(record
.Name
);
641 llvm::ms_demangle::IdentifierNode
*idn
=
642 ttn
->QualifiedName
->getUnqualifiedIdentifier();
643 return idn
->toString();
647 SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id
,
648 const TagRecord
&record
,
649 size_t size
, CompilerType ct
) {
651 std::string uname
= GetUnqualifiedTypeName(record
);
653 // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE.
655 return MakeType(toOpaqueUid(type_id
), ConstString(uname
), size
, nullptr,
656 LLDB_INVALID_UID
, Type::eEncodingIsUID
, decl
, ct
,
657 Type::ResolveState::Forward
);
660 lldb::TypeSP
SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id
,
661 const ClassRecord
&cr
,
663 return CreateClassStructUnion(type_id
, cr
, cr
.getSize(), ct
);
666 lldb::TypeSP
SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id
,
667 const UnionRecord
&ur
,
669 return CreateClassStructUnion(type_id
, ur
, ur
.getSize(), ct
);
672 lldb::TypeSP
SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id
,
673 const EnumRecord
&er
,
675 std::string uname
= GetUnqualifiedTypeName(er
);
678 TypeSP underlying_type
= GetOrCreateType(er
.UnderlyingType
);
680 return MakeType(toOpaqueUid(type_id
), ConstString(uname
),
681 underlying_type
->GetByteSize(nullptr), nullptr,
682 LLDB_INVALID_UID
, lldb_private::Type::eEncodingIsUID
, decl
,
683 ct
, lldb_private::Type::ResolveState::Forward
);
686 TypeSP
SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id
,
687 const ArrayRecord
&ar
,
689 TypeSP element_type
= GetOrCreateType(ar
.ElementType
);
693 MakeType(toOpaqueUid(type_id
), ConstString(), ar
.Size
, nullptr,
694 LLDB_INVALID_UID
, lldb_private::Type::eEncodingIsUID
, decl
, ct
,
695 lldb_private::Type::ResolveState::Full
);
696 array_sp
->SetEncodingType(element_type
.get());
700 TypeSP
SymbolFileNativePDB::CreateFunctionType(PdbTypeSymId type_id
,
701 const MemberFunctionRecord
&mfr
,
704 return MakeType(toOpaqueUid(type_id
), ConstString(), 0, nullptr,
705 LLDB_INVALID_UID
, lldb_private::Type::eEncodingIsUID
, decl
,
706 ct
, lldb_private::Type::ResolveState::Full
);
709 TypeSP
SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id
,
710 const ProcedureRecord
&pr
,
713 return MakeType(toOpaqueUid(type_id
), ConstString(), 0, nullptr,
714 LLDB_INVALID_UID
, lldb_private::Type::eEncodingIsUID
, decl
,
715 ct
, lldb_private::Type::ResolveState::Full
);
718 TypeSP
SymbolFileNativePDB::CreateType(PdbTypeSymId type_id
, CompilerType ct
) {
719 if (type_id
.index
.isSimple())
720 return CreateSimpleType(type_id
.index
, ct
);
722 TpiStream
&stream
= type_id
.is_ipi
? m_index
->ipi() : m_index
->tpi();
723 CVType cvt
= stream
.getType(type_id
.index
);
725 if (cvt
.kind() == LF_MODIFIER
) {
726 ModifierRecord modifier
;
728 TypeDeserializer::deserializeAs
<ModifierRecord
>(cvt
, modifier
));
729 return CreateModifierType(type_id
, modifier
, ct
);
732 if (cvt
.kind() == LF_POINTER
) {
733 PointerRecord pointer
;
735 TypeDeserializer::deserializeAs
<PointerRecord
>(cvt
, pointer
));
736 return CreatePointerType(type_id
, pointer
, ct
);
739 if (IsClassRecord(cvt
.kind())) {
741 llvm::cantFail(TypeDeserializer::deserializeAs
<ClassRecord
>(cvt
, cr
));
742 return CreateTagType(type_id
, cr
, ct
);
745 if (cvt
.kind() == LF_ENUM
) {
747 llvm::cantFail(TypeDeserializer::deserializeAs
<EnumRecord
>(cvt
, er
));
748 return CreateTagType(type_id
, er
, ct
);
751 if (cvt
.kind() == LF_UNION
) {
753 llvm::cantFail(TypeDeserializer::deserializeAs
<UnionRecord
>(cvt
, ur
));
754 return CreateTagType(type_id
, ur
, ct
);
757 if (cvt
.kind() == LF_ARRAY
) {
759 llvm::cantFail(TypeDeserializer::deserializeAs
<ArrayRecord
>(cvt
, ar
));
760 return CreateArrayType(type_id
, ar
, ct
);
763 if (cvt
.kind() == LF_PROCEDURE
) {
765 llvm::cantFail(TypeDeserializer::deserializeAs
<ProcedureRecord
>(cvt
, pr
));
766 return CreateProcedureType(type_id
, pr
, ct
);
768 if (cvt
.kind() == LF_MFUNCTION
) {
769 MemberFunctionRecord mfr
;
770 llvm::cantFail(TypeDeserializer::deserializeAs
<MemberFunctionRecord
>(cvt
, mfr
));
771 return CreateFunctionType(type_id
, mfr
, ct
);
777 TypeSP
SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id
) {
778 // If they search for a UDT which is a forward ref, try and resolve the full
779 // decl and just map the forward ref uid to the full decl record.
780 std::optional
<PdbTypeSymId
> full_decl_uid
;
781 if (IsForwardRefUdt(type_id
, m_index
->tpi())) {
782 auto expected_full_ti
=
783 m_index
->tpi().findFullDeclForForwardRef(type_id
.index
);
784 if (!expected_full_ti
)
785 llvm::consumeError(expected_full_ti
.takeError());
786 else if (*expected_full_ti
!= type_id
.index
) {
787 full_decl_uid
= PdbTypeSymId(*expected_full_ti
, false);
789 // It's possible that a lookup would occur for the full decl causing it
790 // to be cached, then a second lookup would occur for the forward decl.
791 // We don't want to create a second full decl, so make sure the full
792 // decl hasn't already been cached.
793 auto full_iter
= m_types
.find(toOpaqueUid(*full_decl_uid
));
794 if (full_iter
!= m_types
.end()) {
795 TypeSP result
= full_iter
->second
;
796 // Map the forward decl to the TypeSP for the full decl so we can take
797 // the fast path next time.
798 m_types
[toOpaqueUid(type_id
)] = result
;
804 PdbTypeSymId best_decl_id
= full_decl_uid
? *full_decl_uid
: type_id
;
805 auto ts_or_err
= GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus
);
806 if (auto err
= ts_or_err
.takeError())
808 auto ts
= *ts_or_err
;
812 PdbAstBuilder
* ast_builder
= ts
->GetNativePDBParser();
813 clang::QualType qt
= ast_builder
->GetOrCreateType(best_decl_id
);
817 TypeSP result
= CreateType(best_decl_id
, ast_builder
->ToCompilerType(qt
));
821 uint64_t best_uid
= toOpaqueUid(best_decl_id
);
822 m_types
[best_uid
] = result
;
823 // If we had both a forward decl and a full decl, make both point to the new
826 m_types
[toOpaqueUid(type_id
)] = result
;
831 TypeSP
SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id
) {
832 // We can't use try_emplace / overwrite here because the process of creating
833 // a type could create nested types, which could invalidate iterators. So
834 // we have to do a 2-phase lookup / insert.
835 auto iter
= m_types
.find(toOpaqueUid(type_id
));
836 if (iter
!= m_types
.end())
839 TypeSP type
= CreateAndCacheType(type_id
);
841 GetTypeList().Insert(type
);
845 VariableSP
SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id
) {
846 CVSymbol sym
= m_index
->symrecords().readRecord(var_id
.offset
);
847 if (sym
.kind() == S_CONSTANT
)
848 return CreateConstantSymbol(var_id
, sym
);
850 lldb::ValueType scope
= eValueTypeInvalid
;
852 llvm::StringRef name
;
853 lldb::addr_t addr
= 0;
854 uint16_t section
= 0;
856 bool is_external
= false;
857 switch (sym
.kind()) {
862 DataSym
ds(sym
.kind());
863 llvm::cantFail(SymbolDeserializer::deserializeAs
<DataSym
>(sym
, ds
));
865 scope
= (sym
.kind() == S_GDATA32
) ? eValueTypeVariableGlobal
866 : eValueTypeVariableStatic
;
868 section
= ds
.Segment
;
869 offset
= ds
.DataOffset
;
870 addr
= m_index
->MakeVirtualAddress(ds
.Segment
, ds
.DataOffset
);
877 ThreadLocalDataSym
tlds(sym
.kind());
879 SymbolDeserializer::deserializeAs
<ThreadLocalDataSym
>(sym
, tlds
));
882 section
= tlds
.Segment
;
883 offset
= tlds
.DataOffset
;
884 addr
= m_index
->MakeVirtualAddress(tlds
.Segment
, tlds
.DataOffset
);
885 scope
= eValueTypeVariableThreadLocal
;
889 llvm_unreachable("unreachable!");
892 CompUnitSP comp_unit
;
893 std::optional
<uint16_t> modi
= m_index
->GetModuleIndexForVa(addr
);
894 // Some globals has modi points to the linker module, ignore them.
895 if (!modi
|| modi
>= GetNumCompileUnits())
898 CompilandIndexItem
&cci
= m_index
->compilands().GetOrCreateCompiland(*modi
);
899 comp_unit
= GetOrCreateCompileUnit(cci
);
902 PdbTypeSymId
tid(ti
, false);
903 SymbolFileTypeSP type_sp
=
904 std::make_shared
<SymbolFileType
>(*this, toOpaqueUid(tid
));
905 Variable::RangeList ranges
;
906 auto ts_or_err
= GetTypeSystemForLanguage(comp_unit
->GetLanguage());
907 if (auto err
= ts_or_err
.takeError())
909 auto ts
= *ts_or_err
;
913 ts
->GetNativePDBParser()->GetOrCreateVariableDecl(var_id
);
915 ModuleSP module_sp
= GetObjectFile()->GetModule();
916 DWARFExpressionList
location(
917 module_sp
, MakeGlobalLocationExpression(section
, offset
, module_sp
),
920 std::string
global_name("::");
922 bool artificial
= false;
923 bool location_is_constant_data
= false;
924 bool static_member
= false;
925 VariableSP var_sp
= std::make_shared
<Variable
>(
926 toOpaqueUid(var_id
), name
.str().c_str(), global_name
.c_str(), type_sp
,
927 scope
, comp_unit
.get(), ranges
, &decl
, location
, is_external
, artificial
,
928 location_is_constant_data
, static_member
);
934 SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id
,
935 const CVSymbol
&cvs
) {
936 TpiStream
&tpi
= m_index
->tpi();
937 ConstantSym
constant(cvs
.kind());
939 llvm::cantFail(SymbolDeserializer::deserializeAs
<ConstantSym
>(cvs
, constant
));
940 std::string
global_name("::");
941 global_name
+= constant
.Name
;
942 PdbTypeSymId
tid(constant
.Type
, false);
943 SymbolFileTypeSP type_sp
=
944 std::make_shared
<SymbolFileType
>(*this, toOpaqueUid(tid
));
947 Variable::RangeList ranges
;
948 ModuleSP module
= GetObjectFile()->GetModule();
949 DWARFExpressionList
location(module
,
950 MakeConstantLocationExpression(
951 constant
.Type
, tpi
, constant
.Value
, module
),
954 bool external
= false;
955 bool artificial
= false;
956 bool location_is_constant_data
= true;
957 bool static_member
= false;
958 VariableSP var_sp
= std::make_shared
<Variable
>(
959 toOpaqueUid(var_id
), constant
.Name
.str().c_str(), global_name
.c_str(),
960 type_sp
, eValueTypeVariableGlobal
, module
.get(), ranges
, &decl
, location
,
961 external
, artificial
, location_is_constant_data
, static_member
);
966 SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id
) {
967 auto emplace_result
= m_global_vars
.try_emplace(toOpaqueUid(var_id
), nullptr);
968 if (emplace_result
.second
) {
969 if (VariableSP var_sp
= CreateGlobalVariable(var_id
))
970 emplace_result
.first
->second
= var_sp
;
975 return emplace_result
.first
->second
;
978 lldb::TypeSP
SymbolFileNativePDB::GetOrCreateType(TypeIndex ti
) {
979 return GetOrCreateType(PdbTypeSymId(ti
, false));
982 FunctionSP
SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id
,
983 CompileUnit
&comp_unit
) {
984 auto emplace_result
= m_functions
.try_emplace(toOpaqueUid(func_id
), nullptr);
985 if (emplace_result
.second
)
986 emplace_result
.first
->second
= CreateFunction(func_id
, comp_unit
);
988 return emplace_result
.first
->second
;
992 SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem
&cci
) {
994 auto emplace_result
=
995 m_compilands
.try_emplace(toOpaqueUid(cci
.m_id
), nullptr);
996 if (emplace_result
.second
)
997 emplace_result
.first
->second
= CreateCompileUnit(cci
);
999 lldbassert(emplace_result
.first
->second
);
1000 return emplace_result
.first
->second
;
1003 Block
*SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id
) {
1004 auto iter
= m_blocks
.find(toOpaqueUid(block_id
));
1005 if (iter
!= m_blocks
.end())
1006 return iter
->second
.get();
1008 return CreateBlock(block_id
);
1011 void SymbolFileNativePDB::ParseDeclsForContext(
1012 lldb_private::CompilerDeclContext decl_ctx
) {
1013 TypeSystem
* ts_or_err
= decl_ctx
.GetTypeSystem();
1016 PdbAstBuilder
* ast_builder
= ts_or_err
->GetNativePDBParser();
1017 clang::DeclContext
*context
= ast_builder
->FromCompilerDeclContext(decl_ctx
);
1020 ast_builder
->ParseDeclsForContext(*context
);
1023 lldb::CompUnitSP
SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index
) {
1024 if (index
>= GetNumCompileUnits())
1025 return CompUnitSP();
1026 lldbassert(index
< UINT16_MAX
);
1027 if (index
>= UINT16_MAX
)
1030 CompilandIndexItem
&item
= m_index
->compilands().GetOrCreateCompiland(index
);
1032 return GetOrCreateCompileUnit(item
);
1035 lldb::LanguageType
SymbolFileNativePDB::ParseLanguage(CompileUnit
&comp_unit
) {
1036 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1037 PdbSymUid
uid(comp_unit
.GetID());
1038 lldbassert(uid
.kind() == PdbSymUidKind::Compiland
);
1040 CompilandIndexItem
*item
=
1041 m_index
->compilands().GetCompiland(uid
.asCompiland().modi
);
1043 if (!item
->m_compile_opts
)
1044 return lldb::eLanguageTypeUnknown
;
1046 return TranslateLanguage(item
->m_compile_opts
->getLanguage());
1049 void SymbolFileNativePDB::AddSymbols(Symtab
&symtab
) {}
1051 size_t SymbolFileNativePDB::ParseFunctions(CompileUnit
&comp_unit
) {
1052 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1053 PdbSymUid uid
{comp_unit
.GetID()};
1054 lldbassert(uid
.kind() == PdbSymUidKind::Compiland
);
1055 uint16_t modi
= uid
.asCompiland().modi
;
1056 CompilandIndexItem
&cii
= m_index
->compilands().GetOrCreateCompiland(modi
);
1058 size_t count
= comp_unit
.GetNumFunctions();
1059 const CVSymbolArray
&syms
= cii
.m_debug_stream
.getSymbolArray();
1060 for (auto iter
= syms
.begin(); iter
!= syms
.end(); ++iter
) {
1061 if (iter
->kind() != S_LPROC32
&& iter
->kind() != S_GPROC32
)
1064 PdbCompilandSymId sym_id
{modi
, iter
.offset()};
1066 FunctionSP func
= GetOrCreateFunction(sym_id
, comp_unit
);
1069 size_t new_count
= comp_unit
.GetNumFunctions();
1070 lldbassert(new_count
>= count
);
1071 return new_count
- count
;
1074 static bool NeedsResolvedCompileUnit(uint32_t resolve_scope
) {
1075 // If any of these flags are set, we need to resolve the compile unit.
1076 uint32_t flags
= eSymbolContextCompUnit
;
1077 flags
|= eSymbolContextVariable
;
1078 flags
|= eSymbolContextFunction
;
1079 flags
|= eSymbolContextBlock
;
1080 flags
|= eSymbolContextLineEntry
;
1081 return (resolve_scope
& flags
) != 0;
1084 uint32_t SymbolFileNativePDB::ResolveSymbolContext(
1085 const Address
&addr
, SymbolContextItem resolve_scope
, SymbolContext
&sc
) {
1086 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1087 uint32_t resolved_flags
= 0;
1088 lldb::addr_t file_addr
= addr
.GetFileAddress();
1090 if (NeedsResolvedCompileUnit(resolve_scope
)) {
1091 std::optional
<uint16_t> modi
= m_index
->GetModuleIndexForVa(file_addr
);
1094 CompUnitSP cu_sp
= GetCompileUnitAtIndex(*modi
);
1098 sc
.comp_unit
= cu_sp
.get();
1099 resolved_flags
|= eSymbolContextCompUnit
;
1102 if (resolve_scope
& eSymbolContextFunction
||
1103 resolve_scope
& eSymbolContextBlock
) {
1104 lldbassert(sc
.comp_unit
);
1105 std::vector
<SymbolAndUid
> matches
= m_index
->FindSymbolsByVa(file_addr
);
1106 // Search the matches in reverse. This way if there are multiple matches
1107 // (for example we are 3 levels deep in a nested scope) it will find the
1108 // innermost one first.
1109 for (const auto &match
: llvm::reverse(matches
)) {
1110 if (match
.uid
.kind() != PdbSymUidKind::CompilandSym
)
1113 PdbCompilandSymId csid
= match
.uid
.asCompilandSym();
1114 CVSymbol cvs
= m_index
->ReadSymbolRecord(csid
);
1115 PDB_SymType type
= CVSymToPDBSym(cvs
.kind());
1116 if (type
!= PDB_SymType::Function
&& type
!= PDB_SymType::Block
)
1118 if (type
== PDB_SymType::Function
) {
1119 sc
.function
= GetOrCreateFunction(csid
, *sc
.comp_unit
).get();
1121 Block
&block
= sc
.function
->GetBlock(true);
1123 sc
.function
->GetAddressRange().GetBaseAddress().GetFileAddress();
1124 addr_t offset
= file_addr
- func_base
;
1125 sc
.block
= block
.FindInnermostBlockByOffset(offset
);
1129 if (type
== PDB_SymType::Block
) {
1130 Block
*block
= GetOrCreateBlock(csid
);
1133 sc
.function
= block
->CalculateSymbolContextFunction();
1135 sc
.function
->GetBlock(true);
1137 sc
.function
->GetAddressRange().GetBaseAddress().GetFileAddress();
1138 addr_t offset
= file_addr
- func_base
;
1139 sc
.block
= block
->FindInnermostBlockByOffset(offset
);
1143 resolved_flags
|= eSymbolContextFunction
;
1145 resolved_flags
|= eSymbolContextBlock
;
1150 if (resolve_scope
& eSymbolContextLineEntry
) {
1151 lldbassert(sc
.comp_unit
);
1152 if (auto *line_table
= sc
.comp_unit
->GetLineTable()) {
1153 if (line_table
->FindLineEntryByAddress(addr
, sc
.line_entry
))
1154 resolved_flags
|= eSymbolContextLineEntry
;
1158 return resolved_flags
;
1161 uint32_t SymbolFileNativePDB::ResolveSymbolContext(
1162 const SourceLocationSpec
&src_location_spec
,
1163 lldb::SymbolContextItem resolve_scope
, SymbolContextList
&sc_list
) {
1164 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1165 const uint32_t prev_size
= sc_list
.GetSize();
1166 if (resolve_scope
& eSymbolContextCompUnit
) {
1167 for (uint32_t cu_idx
= 0, num_cus
= GetNumCompileUnits(); cu_idx
< num_cus
;
1169 CompileUnit
*cu
= ParseCompileUnitAtIndex(cu_idx
).get();
1173 bool file_spec_matches_cu_file_spec
= FileSpec::Match(
1174 src_location_spec
.GetFileSpec(), cu
->GetPrimaryFile());
1175 if (file_spec_matches_cu_file_spec
) {
1176 cu
->ResolveSymbolContext(src_location_spec
, resolve_scope
, sc_list
);
1181 return sc_list
.GetSize() - prev_size
;
1184 bool SymbolFileNativePDB::ParseLineTable(CompileUnit
&comp_unit
) {
1185 // Unfortunately LLDB is set up to parse the entire compile unit line table
1186 // all at once, even if all it really needs is line info for a specific
1187 // function. In the future it would be nice if it could set the sc.m_function
1188 // member, and we could only get the line info for the function in question.
1189 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1190 PdbSymUid
cu_id(comp_unit
.GetID());
1191 lldbassert(cu_id
.kind() == PdbSymUidKind::Compiland
);
1192 uint16_t modi
= cu_id
.asCompiland().modi
;
1193 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(modi
);
1196 // Parse DEBUG_S_LINES subsections first, then parse all S_INLINESITE records
1197 // in this CU. Add line entries into the set first so that if there are line
1198 // entries with same addres, the later is always more accurate than the
1200 std::set
<LineTable::Entry
, LineTableEntryComparator
> line_set
;
1202 // This is basically a copy of the .debug$S subsections from all original COFF
1203 // object files merged together with address relocations applied. We are
1204 // looking for all DEBUG_S_LINES subsections.
1205 for (const DebugSubsectionRecord
&dssr
:
1206 cii
->m_debug_stream
.getSubsectionsArray()) {
1207 if (dssr
.kind() != DebugSubsectionKind::Lines
)
1210 DebugLinesSubsectionRef lines
;
1211 llvm::BinaryStreamReader
reader(dssr
.getRecordData());
1212 if (auto EC
= lines
.initialize(reader
)) {
1213 llvm::consumeError(std::move(EC
));
1217 const LineFragmentHeader
*lfh
= lines
.header();
1218 uint64_t virtual_addr
=
1219 m_index
->MakeVirtualAddress(lfh
->RelocSegment
, lfh
->RelocOffset
);
1220 if (virtual_addr
== LLDB_INVALID_ADDRESS
)
1223 for (const LineColumnEntry
&group
: lines
) {
1224 llvm::Expected
<uint32_t> file_index_or_err
=
1225 GetFileIndex(*cii
, group
.NameIndex
);
1226 if (!file_index_or_err
)
1228 uint32_t file_index
= file_index_or_err
.get();
1229 lldbassert(!group
.LineNumbers
.empty());
1230 CompilandIndexItem::GlobalLineTable::Entry
line_entry(
1231 LLDB_INVALID_ADDRESS
, 0);
1232 for (const LineNumberEntry
&entry
: group
.LineNumbers
) {
1233 LineInfo
cur_info(entry
.Flags
);
1235 if (cur_info
.isAlwaysStepInto() || cur_info
.isNeverStepInto())
1238 uint64_t addr
= virtual_addr
+ entry
.Offset
;
1240 bool is_statement
= cur_info
.isStatement();
1241 bool is_prologue
= IsFunctionPrologue(*cii
, addr
);
1242 bool is_epilogue
= IsFunctionEpilogue(*cii
, addr
);
1244 uint32_t lno
= cur_info
.getStartLine();
1246 LineTable::Entry
new_entry(addr
, lno
, 0, file_index
, is_statement
, false,
1247 is_prologue
, is_epilogue
, false);
1248 // Terminal entry has lower precedence than new entry.
1249 auto iter
= line_set
.find(new_entry
);
1250 if (iter
!= line_set
.end() && iter
->is_terminal_entry
)
1251 line_set
.erase(iter
);
1252 line_set
.insert(new_entry
);
1254 if (line_entry
.GetRangeBase() != LLDB_INVALID_ADDRESS
) {
1255 line_entry
.SetRangeEnd(addr
);
1256 cii
->m_global_line_table
.Append(line_entry
);
1258 line_entry
.SetRangeBase(addr
);
1259 line_entry
.data
= {file_index
, lno
};
1261 LineInfo
last_line(group
.LineNumbers
.back().Flags
);
1262 line_set
.emplace(virtual_addr
+ lfh
->CodeSize
, last_line
.getEndLine(), 0,
1263 file_index
, false, false, false, false, true);
1265 if (line_entry
.GetRangeBase() != LLDB_INVALID_ADDRESS
) {
1266 line_entry
.SetRangeEnd(virtual_addr
+ lfh
->CodeSize
);
1267 cii
->m_global_line_table
.Append(line_entry
);
1272 cii
->m_global_line_table
.Sort();
1274 // Parse all S_INLINESITE in this CU.
1275 const CVSymbolArray
&syms
= cii
->m_debug_stream
.getSymbolArray();
1276 for (auto iter
= syms
.begin(); iter
!= syms
.end();) {
1277 if (iter
->kind() != S_LPROC32
&& iter
->kind() != S_GPROC32
) {
1282 uint32_t record_offset
= iter
.offset();
1283 CVSymbol func_record
=
1284 cii
->m_debug_stream
.readSymbolAtOffset(record_offset
);
1285 SegmentOffsetLength sol
= GetSegmentOffsetAndLength(func_record
);
1286 addr_t file_vm_addr
=
1287 m_index
->MakeVirtualAddress(sol
.so
.segment
, sol
.so
.offset
);
1288 if (file_vm_addr
== LLDB_INVALID_ADDRESS
)
1291 AddressRange
func_range(file_vm_addr
, sol
.length
,
1292 comp_unit
.GetModule()->GetSectionList());
1293 Address func_base
= func_range
.GetBaseAddress();
1294 PdbCompilandSymId func_id
{modi
, record_offset
};
1296 // Iterate all S_INLINESITEs in the function.
1297 auto parse_inline_sites
= [&](SymbolKind kind
, PdbCompilandSymId id
) {
1298 if (kind
!= S_INLINESITE
)
1301 ParseInlineSite(id
, func_base
);
1303 for (const auto &line_entry
:
1304 m_inline_sites
[toOpaqueUid(id
)]->line_entries
) {
1305 // If line_entry is not terminal entry, remove previous line entry at
1306 // the same address and insert new one. Terminal entry inside an inline
1307 // site might not be terminal entry for its parent.
1308 if (!line_entry
.is_terminal_entry
)
1309 line_set
.erase(line_entry
);
1310 line_set
.insert(line_entry
);
1312 // No longer useful after adding to line_set.
1313 m_inline_sites
[toOpaqueUid(id
)]->line_entries
.clear();
1316 ParseSymbolArrayInScope(func_id
, parse_inline_sites
);
1317 // Jump to the end of the function record.
1318 iter
= syms
.at(getScopeEndOffset(func_record
));
1321 cii
->m_global_line_table
.Clear();
1323 // Add line entries in line_set to line_table.
1324 auto line_table
= std::make_unique
<LineTable
>(&comp_unit
);
1325 std::unique_ptr
<LineSequence
> sequence(
1326 line_table
->CreateLineSequenceContainer());
1327 for (const auto &line_entry
: line_set
) {
1328 line_table
->AppendLineEntryToSequence(
1329 sequence
.get(), line_entry
.file_addr
, line_entry
.line
,
1330 line_entry
.column
, line_entry
.file_idx
,
1331 line_entry
.is_start_of_statement
, line_entry
.is_start_of_basic_block
,
1332 line_entry
.is_prologue_end
, line_entry
.is_epilogue_begin
,
1333 line_entry
.is_terminal_entry
);
1335 line_table
->InsertSequence(sequence
.get());
1337 if (line_table
->GetSize() == 0)
1340 comp_unit
.SetLineTable(line_table
.release());
1344 bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit
&comp_unit
) {
1345 // PDB doesn't contain information about macros
1349 llvm::Expected
<uint32_t>
1350 SymbolFileNativePDB::GetFileIndex(const CompilandIndexItem
&cii
,
1352 if (!cii
.m_strings
.hasChecksums() || !cii
.m_strings
.hasStrings())
1353 return llvm::make_error
<RawError
>(raw_error_code::no_entry
);
1355 const auto &checksums
= cii
.m_strings
.checksums().getArray();
1356 const auto &strings
= cii
.m_strings
.strings();
1357 // Indices in this structure are actually offsets of records in the
1358 // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index
1359 // into the global PDB string table.
1360 auto iter
= checksums
.at(file_id
);
1361 if (iter
== checksums
.end())
1362 return llvm::make_error
<RawError
>(raw_error_code::no_entry
);
1364 llvm::Expected
<llvm::StringRef
> efn
= strings
.getString(iter
->FileNameOffset
);
1366 return efn
.takeError();
1369 // LLDB wants the index of the file in the list of support files.
1370 auto fn_iter
= llvm::find(cii
.m_file_list
, *efn
);
1371 if (fn_iter
!= cii
.m_file_list
.end())
1372 return std::distance(cii
.m_file_list
.begin(), fn_iter
);
1373 return llvm::make_error
<RawError
>(raw_error_code::no_entry
);
1376 bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit
&comp_unit
,
1377 SupportFileList
&support_files
) {
1378 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1379 PdbSymUid
cu_id(comp_unit
.GetID());
1380 lldbassert(cu_id
.kind() == PdbSymUidKind::Compiland
);
1381 CompilandIndexItem
*cci
=
1382 m_index
->compilands().GetCompiland(cu_id
.asCompiland().modi
);
1385 for (llvm::StringRef f
: cci
->m_file_list
) {
1386 FileSpec::Style style
=
1387 f
.starts_with("/") ? FileSpec::Style::posix
: FileSpec::Style::windows
;
1388 FileSpec
spec(f
, style
);
1389 support_files
.Append(spec
);
1394 bool SymbolFileNativePDB::ParseImportedModules(
1395 const SymbolContext
&sc
, std::vector
<SourceModule
> &imported_modules
) {
1396 // PDB does not yet support module debug info
1400 void SymbolFileNativePDB::ParseInlineSite(PdbCompilandSymId id
,
1401 Address func_addr
) {
1402 lldb::user_id_t opaque_uid
= toOpaqueUid(id
);
1403 if (m_inline_sites
.contains(opaque_uid
))
1406 addr_t func_base
= func_addr
.GetFileAddress();
1407 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(id
.modi
);
1408 CVSymbol sym
= cii
->m_debug_stream
.readSymbolAtOffset(id
.offset
);
1409 CompUnitSP comp_unit
= GetOrCreateCompileUnit(*cii
);
1411 InlineSiteSym
inline_site(static_cast<SymbolRecordKind
>(sym
.kind()));
1412 cantFail(SymbolDeserializer::deserializeAs
<InlineSiteSym
>(sym
, inline_site
));
1413 PdbCompilandSymId
parent_id(id
.modi
, inline_site
.Parent
);
1415 std::shared_ptr
<InlineSite
> inline_site_sp
=
1416 std::make_shared
<InlineSite
>(parent_id
);
1418 // Get the inlined function declaration info.
1419 auto iter
= cii
->m_inline_map
.find(inline_site
.Inlinee
);
1420 if (iter
== cii
->m_inline_map
.end())
1422 InlineeSourceLine inlinee_line
= iter
->second
;
1424 const SupportFileList
&files
= comp_unit
->GetSupportFiles();
1426 llvm::Expected
<uint32_t> file_index_or_err
=
1427 GetFileIndex(*cii
, inlinee_line
.Header
->FileID
);
1428 if (!file_index_or_err
)
1430 uint32_t file_offset
= file_index_or_err
.get();
1431 decl_file
= files
.GetFileSpecAtIndex(file_offset
);
1432 uint32_t decl_line
= inlinee_line
.Header
->SourceLineNum
;
1433 std::unique_ptr
<Declaration
> decl_up
=
1434 std::make_unique
<Declaration
>(decl_file
, decl_line
);
1436 // Parse range and line info.
1437 uint32_t code_offset
= 0;
1438 int32_t line_offset
= 0;
1439 std::optional
<uint32_t> code_offset_base
;
1440 std::optional
<uint32_t> code_offset_end
;
1441 std::optional
<int32_t> cur_line_offset
;
1442 std::optional
<int32_t> next_line_offset
;
1443 std::optional
<uint32_t> next_file_offset
;
1445 bool is_terminal_entry
= false;
1446 bool is_start_of_statement
= true;
1447 // The first instruction is the prologue end.
1448 bool is_prologue_end
= true;
1450 auto update_code_offset
= [&](uint32_t code_delta
) {
1451 if (!code_offset_base
)
1452 code_offset_base
= code_offset
;
1453 else if (!code_offset_end
)
1454 code_offset_end
= *code_offset_base
+ code_delta
;
1456 auto update_line_offset
= [&](int32_t line_delta
) {
1457 line_offset
+= line_delta
;
1458 if (!code_offset_base
|| !cur_line_offset
)
1459 cur_line_offset
= line_offset
;
1461 next_line_offset
= line_offset
;
1464 auto update_file_offset
= [&](uint32_t offset
) {
1465 if (!code_offset_base
)
1466 file_offset
= offset
;
1468 next_file_offset
= offset
;
1471 for (auto &annot
: inline_site
.annotations()) {
1472 switch (annot
.OpCode
) {
1473 case BinaryAnnotationsOpCode::CodeOffset
:
1474 case BinaryAnnotationsOpCode::ChangeCodeOffset
:
1475 case BinaryAnnotationsOpCode::ChangeCodeOffsetBase
:
1476 code_offset
+= annot
.U1
;
1477 update_code_offset(annot
.U1
);
1479 case BinaryAnnotationsOpCode::ChangeLineOffset
:
1480 update_line_offset(annot
.S1
);
1482 case BinaryAnnotationsOpCode::ChangeCodeLength
:
1483 update_code_offset(annot
.U1
);
1484 code_offset
+= annot
.U1
;
1485 is_terminal_entry
= true;
1487 case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset
:
1488 code_offset
+= annot
.U1
;
1489 update_code_offset(annot
.U1
);
1490 update_line_offset(annot
.S1
);
1492 case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset
:
1493 code_offset
+= annot
.U2
;
1494 update_code_offset(annot
.U2
);
1495 update_code_offset(annot
.U1
);
1496 code_offset
+= annot
.U1
;
1497 is_terminal_entry
= true;
1499 case BinaryAnnotationsOpCode::ChangeFile
:
1500 update_file_offset(annot
.U1
);
1506 // Add range if current range is finished.
1507 if (code_offset_base
&& code_offset_end
&& cur_line_offset
) {
1508 inline_site_sp
->ranges
.Append(RangeSourceLineVector::Entry(
1509 *code_offset_base
, *code_offset_end
- *code_offset_base
,
1510 decl_line
+ *cur_line_offset
));
1511 // Set base, end, file offset and line offset for next range.
1512 if (next_file_offset
)
1513 file_offset
= *next_file_offset
;
1514 if (next_line_offset
) {
1515 cur_line_offset
= next_line_offset
;
1516 next_line_offset
= std::nullopt
;
1518 code_offset_base
= is_terminal_entry
? std::nullopt
: code_offset_end
;
1519 code_offset_end
= next_file_offset
= std::nullopt
;
1521 if (code_offset_base
&& cur_line_offset
) {
1522 if (is_terminal_entry
) {
1523 LineTable::Entry
line_entry(
1524 func_base
+ *code_offset_base
, decl_line
+ *cur_line_offset
, 0,
1525 file_offset
, false, false, false, false, true);
1526 inline_site_sp
->line_entries
.push_back(line_entry
);
1528 LineTable::Entry
line_entry(func_base
+ *code_offset_base
,
1529 decl_line
+ *cur_line_offset
, 0,
1530 file_offset
, is_start_of_statement
, false,
1531 is_prologue_end
, false, false);
1532 inline_site_sp
->line_entries
.push_back(line_entry
);
1533 is_prologue_end
= false;
1534 is_start_of_statement
= false;
1537 if (is_terminal_entry
)
1538 is_start_of_statement
= true;
1539 is_terminal_entry
= false;
1542 inline_site_sp
->ranges
.Sort();
1544 // Get the inlined function callsite info.
1545 std::unique_ptr
<Declaration
> callsite_up
;
1546 if (!inline_site_sp
->ranges
.IsEmpty()) {
1547 auto *entry
= inline_site_sp
->ranges
.GetEntryAtIndex(0);
1548 addr_t base_offset
= entry
->GetRangeBase();
1549 if (cii
->m_debug_stream
.readSymbolAtOffset(parent_id
.offset
).kind() ==
1551 // Its parent is another inline site, lookup parent site's range vector
1552 // for callsite line.
1553 ParseInlineSite(parent_id
, func_base
);
1554 std::shared_ptr
<InlineSite
> parent_site
=
1555 m_inline_sites
[toOpaqueUid(parent_id
)];
1556 FileSpec
&parent_decl_file
=
1557 parent_site
->inline_function_info
->GetDeclaration().GetFile();
1558 if (auto *parent_entry
=
1559 parent_site
->ranges
.FindEntryThatContains(base_offset
)) {
1561 std::make_unique
<Declaration
>(parent_decl_file
, parent_entry
->data
);
1564 // Its parent is a function, lookup global line table for callsite.
1565 if (auto *entry
= cii
->m_global_line_table
.FindEntryThatContains(
1566 func_base
+ base_offset
)) {
1567 const FileSpec
&callsite_file
=
1568 files
.GetFileSpecAtIndex(entry
->data
.first
);
1570 std::make_unique
<Declaration
>(callsite_file
, entry
->data
.second
);
1575 // Get the inlined function name.
1576 CVType inlinee_cvt
= m_index
->ipi().getType(inline_site
.Inlinee
);
1577 std::string inlinee_name
;
1578 if (inlinee_cvt
.kind() == LF_MFUNC_ID
) {
1579 MemberFuncIdRecord mfr
;
1581 TypeDeserializer::deserializeAs
<MemberFuncIdRecord
>(inlinee_cvt
, mfr
));
1582 LazyRandomTypeCollection
&types
= m_index
->tpi().typeCollection();
1583 inlinee_name
.append(std::string(types
.getTypeName(mfr
.ClassType
)));
1584 inlinee_name
.append("::");
1585 inlinee_name
.append(mfr
.getName().str());
1586 } else if (inlinee_cvt
.kind() == LF_FUNC_ID
) {
1588 cantFail(TypeDeserializer::deserializeAs
<FuncIdRecord
>(inlinee_cvt
, fir
));
1589 TypeIndex parent_idx
= fir
.getParentScope();
1590 if (!parent_idx
.isNoneType()) {
1591 LazyRandomTypeCollection
&ids
= m_index
->ipi().typeCollection();
1592 inlinee_name
.append(std::string(ids
.getTypeName(parent_idx
)));
1593 inlinee_name
.append("::");
1595 inlinee_name
.append(fir
.getName().str());
1597 inline_site_sp
->inline_function_info
= std::make_shared
<InlineFunctionInfo
>(
1598 inlinee_name
.c_str(), llvm::StringRef(), decl_up
.get(),
1601 m_inline_sites
[opaque_uid
] = inline_site_sp
;
1604 size_t SymbolFileNativePDB::ParseBlocksRecursive(Function
&func
) {
1605 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1606 PdbCompilandSymId func_id
= PdbSymUid(func
.GetID()).asCompilandSym();
1607 // After we iterate through inline sites inside the function, we already get
1608 // all the info needed, removing from the map to save memory.
1609 std::set
<uint64_t> remove_uids
;
1610 auto parse_blocks
= [&](SymbolKind kind
, PdbCompilandSymId id
) {
1611 if (kind
== S_GPROC32
|| kind
== S_LPROC32
|| kind
== S_BLOCK32
||
1612 kind
== S_INLINESITE
) {
1613 GetOrCreateBlock(id
);
1614 if (kind
== S_INLINESITE
)
1615 remove_uids
.insert(toOpaqueUid(id
));
1620 size_t count
= ParseSymbolArrayInScope(func_id
, parse_blocks
);
1621 for (uint64_t uid
: remove_uids
) {
1622 m_inline_sites
.erase(uid
);
1627 size_t SymbolFileNativePDB::ParseSymbolArrayInScope(
1628 PdbCompilandSymId parent_id
,
1629 llvm::function_ref
<bool(SymbolKind
, PdbCompilandSymId
)> fn
) {
1630 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(parent_id
.modi
);
1631 CVSymbolArray syms
=
1632 cii
->m_debug_stream
.getSymbolArrayForScope(parent_id
.offset
);
1635 for (auto iter
= syms
.begin(); iter
!= syms
.end(); ++iter
) {
1636 PdbCompilandSymId
child_id(parent_id
.modi
, iter
.offset());
1637 if (fn(iter
->kind(), child_id
))
1644 void SymbolFileNativePDB::DumpClangAST(Stream
&s
) {
1645 auto ts_or_err
= GetTypeSystemForLanguage(eLanguageTypeC_plus_plus
);
1648 auto ts
= *ts_or_err
;
1649 TypeSystemClang
*clang
= llvm::dyn_cast_or_null
<TypeSystemClang
>(ts
.get());
1652 clang
->GetNativePDBParser()->Dump(s
);
1655 void SymbolFileNativePDB::FindGlobalVariables(
1656 ConstString name
, const CompilerDeclContext
&parent_decl_ctx
,
1657 uint32_t max_matches
, VariableList
&variables
) {
1658 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1659 using SymbolAndOffset
= std::pair
<uint32_t, llvm::codeview::CVSymbol
>;
1661 std::vector
<SymbolAndOffset
> results
= m_index
->globals().findRecordsByName(
1662 name
.GetStringRef(), m_index
->symrecords());
1663 for (const SymbolAndOffset
&result
: results
) {
1664 switch (result
.second
.kind()) {
1665 case SymbolKind::S_GDATA32
:
1666 case SymbolKind::S_LDATA32
:
1667 case SymbolKind::S_GTHREAD32
:
1668 case SymbolKind::S_LTHREAD32
:
1669 case SymbolKind::S_CONSTANT
: {
1670 PdbGlobalSymId
global(result
.first
, false);
1671 if (VariableSP var
= GetOrCreateGlobalVariable(global
))
1672 variables
.AddVariable(var
);
1681 void SymbolFileNativePDB::FindFunctions(
1682 const Module::LookupInfo
&lookup_info
,
1683 const CompilerDeclContext
&parent_decl_ctx
, bool include_inlines
,
1684 SymbolContextList
&sc_list
) {
1685 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1686 ConstString name
= lookup_info
.GetLookupName();
1687 FunctionNameType name_type_mask
= lookup_info
.GetNameTypeMask();
1688 if (name_type_mask
& eFunctionNameTypeFull
)
1689 name
= lookup_info
.GetName();
1691 // For now we only support lookup by method name or full name.
1692 if (!(name_type_mask
& eFunctionNameTypeFull
||
1693 name_type_mask
& eFunctionNameTypeMethod
))
1696 using SymbolAndOffset
= std::pair
<uint32_t, llvm::codeview::CVSymbol
>;
1698 std::vector
<SymbolAndOffset
> matches
= m_index
->globals().findRecordsByName(
1699 name
.GetStringRef(), m_index
->symrecords());
1700 for (const SymbolAndOffset
&match
: matches
) {
1701 if (match
.second
.kind() != S_PROCREF
&& match
.second
.kind() != S_LPROCREF
)
1703 ProcRefSym
proc(match
.second
.kind());
1704 cantFail(SymbolDeserializer::deserializeAs
<ProcRefSym
>(match
.second
, proc
));
1706 if (!IsValidRecord(proc
))
1709 CompilandIndexItem
&cci
=
1710 m_index
->compilands().GetOrCreateCompiland(proc
.modi());
1713 sc
.comp_unit
= GetOrCreateCompileUnit(cci
).get();
1714 PdbCompilandSymId
func_id(proc
.modi(), proc
.SymOffset
);
1715 sc
.function
= GetOrCreateFunction(func_id
, *sc
.comp_unit
).get();
1721 void SymbolFileNativePDB::FindFunctions(const RegularExpression
®ex
,
1722 bool include_inlines
,
1723 SymbolContextList
&sc_list
) {}
1725 void SymbolFileNativePDB::FindTypes(const lldb_private::TypeQuery
&query
,
1726 lldb_private::TypeResults
&results
) {
1728 // Make sure we haven't already searched this SymbolFile before.
1729 if (results
.AlreadySearched(this))
1732 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1734 std::vector
<TypeIndex
> matches
=
1735 m_index
->tpi().findRecordsByName(query
.GetTypeBasename().GetStringRef());
1737 for (TypeIndex type_idx
: matches
) {
1738 TypeSP type_sp
= GetOrCreateType(type_idx
);
1742 // We resolved a type. Get the fully qualified name to ensure it matches.
1743 ConstString name
= type_sp
->GetQualifiedName();
1744 TypeQuery
type_match(name
.GetStringRef(), TypeQueryOptions::e_exact_match
);
1745 if (query
.ContextMatches(type_match
.GetContextRef())) {
1746 results
.InsertUnique(type_sp
);
1747 if (results
.Done(query
))
1753 void SymbolFileNativePDB::FindTypesByName(llvm::StringRef name
,
1754 uint32_t max_matches
,
1757 std::vector
<TypeIndex
> matches
= m_index
->tpi().findRecordsByName(name
);
1758 if (max_matches
> 0 && max_matches
< matches
.size())
1759 matches
.resize(max_matches
);
1761 for (TypeIndex ti
: matches
) {
1762 TypeSP type
= GetOrCreateType(ti
);
1770 size_t SymbolFileNativePDB::ParseTypes(CompileUnit
&comp_unit
) {
1771 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
1772 // Only do the full type scan the first time.
1773 if (m_done_full_type_scan
)
1776 const size_t old_count
= GetTypeList().GetSize();
1777 LazyRandomTypeCollection
&types
= m_index
->tpi().typeCollection();
1779 // First process the entire TPI stream.
1780 for (auto ti
= types
.getFirst(); ti
; ti
= types
.getNext(*ti
)) {
1781 TypeSP type
= GetOrCreateType(*ti
);
1783 (void)type
->GetFullCompilerType();
1786 // Next look for S_UDT records in the globals stream.
1787 for (const uint32_t gid
: m_index
->globals().getGlobalsTable()) {
1788 PdbGlobalSymId global
{gid
, false};
1789 CVSymbol sym
= m_index
->ReadSymbolRecord(global
);
1790 if (sym
.kind() != S_UDT
)
1793 UDTSym udt
= llvm::cantFail(SymbolDeserializer::deserializeAs
<UDTSym
>(sym
));
1794 bool is_typedef
= true;
1795 if (IsTagRecord(PdbTypeSymId
{udt
.Type
, false}, m_index
->tpi())) {
1796 CVType cvt
= m_index
->tpi().getType(udt
.Type
);
1797 llvm::StringRef name
= CVTagRecord::create(cvt
).name();
1798 if (name
== udt
.Name
)
1803 GetOrCreateTypedef(global
);
1806 const size_t new_count
= GetTypeList().GetSize();
1808 m_done_full_type_scan
= true;
1810 return new_count
- old_count
;
1814 SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit
&comp_unit
,
1815 VariableList
&variables
) {
1816 PdbSymUid
sym_uid(comp_unit
.GetID());
1817 lldbassert(sym_uid
.kind() == PdbSymUidKind::Compiland
);
1818 for (const uint32_t gid
: m_index
->globals().getGlobalsTable()) {
1819 PdbGlobalSymId global
{gid
, false};
1820 CVSymbol sym
= m_index
->ReadSymbolRecord(global
);
1821 // TODO: S_CONSTANT is not handled here to prevent a possible crash in
1822 // lldb_private::npdb::MakeConstantLocationExpression when it's a record
1823 // type (e.g. std::strong_ordering::equal). That function needs to be
1824 // updated to handle this case when we add S_CONSTANT case here.
1825 switch (sym
.kind()) {
1826 case SymbolKind::S_GDATA32
:
1827 case SymbolKind::S_LDATA32
:
1828 case SymbolKind::S_GTHREAD32
:
1829 case SymbolKind::S_LTHREAD32
: {
1830 if (VariableSP var
= GetOrCreateGlobalVariable(global
))
1831 variables
.AddVariable(var
);
1838 return variables
.GetSize();
1841 VariableSP
SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id
,
1842 PdbCompilandSymId var_id
,
1844 ModuleSP module
= GetObjectFile()->GetModule();
1845 Block
*block
= GetOrCreateBlock(scope_id
);
1849 // Get function block.
1850 Block
*func_block
= block
;
1851 while (func_block
->GetParent()) {
1852 func_block
= func_block
->GetParent();
1856 func_block
->GetStartAddress(addr
);
1857 VariableInfo var_info
=
1858 GetVariableLocationInfo(*m_index
, var_id
, *func_block
, module
);
1859 Function
*func
= func_block
->CalculateSymbolContextFunction();
1862 // Use empty dwarf expr if optimized away so that it won't be filtered out
1863 // when lookuping local variables in this scope.
1864 if (!var_info
.location
.IsValid())
1865 var_info
.location
= DWARFExpressionList(module
, DWARFExpression(), nullptr);
1866 var_info
.location
.SetFuncFileAddress(
1867 func
->GetAddressRange().GetBaseAddress().GetFileAddress());
1868 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(var_id
.modi
);
1869 CompUnitSP comp_unit_sp
= GetOrCreateCompileUnit(*cii
);
1870 TypeSP type_sp
= GetOrCreateType(var_info
.type
);
1873 std::string name
= var_info
.name
.str();
1875 SymbolFileTypeSP sftype
=
1876 std::make_shared
<SymbolFileType
>(*this, type_sp
->GetID());
1878 is_param
|= var_info
.is_param
;
1879 ValueType var_scope
=
1880 is_param
? eValueTypeVariableArgument
: eValueTypeVariableLocal
;
1881 bool external
= false;
1882 bool artificial
= false;
1883 bool location_is_constant_data
= false;
1884 bool static_member
= false;
1885 Variable::RangeList scope_ranges
;
1886 VariableSP var_sp
= std::make_shared
<Variable
>(
1887 toOpaqueUid(var_id
), name
.c_str(), name
.c_str(), sftype
, var_scope
, block
,
1888 scope_ranges
, &decl
, var_info
.location
, external
, artificial
,
1889 location_is_constant_data
, static_member
);
1891 auto ts_or_err
= GetTypeSystemForLanguage(comp_unit_sp
->GetLanguage());
1892 if (auto err
= ts_or_err
.takeError())
1894 auto ts
= *ts_or_err
;
1898 ts
->GetNativePDBParser()->GetOrCreateVariableDecl(scope_id
, var_id
);
1900 m_local_variables
[toOpaqueUid(var_id
)] = var_sp
;
1904 VariableSP
SymbolFileNativePDB::GetOrCreateLocalVariable(
1905 PdbCompilandSymId scope_id
, PdbCompilandSymId var_id
, bool is_param
) {
1906 auto iter
= m_local_variables
.find(toOpaqueUid(var_id
));
1907 if (iter
!= m_local_variables
.end())
1908 return iter
->second
;
1910 return CreateLocalVariable(scope_id
, var_id
, is_param
);
1913 TypeSP
SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id
) {
1914 CVSymbol sym
= m_index
->ReadSymbolRecord(id
);
1915 lldbassert(sym
.kind() == SymbolKind::S_UDT
);
1917 UDTSym udt
= llvm::cantFail(SymbolDeserializer::deserializeAs
<UDTSym
>(sym
));
1919 TypeSP target_type
= GetOrCreateType(udt
.Type
);
1921 auto ts_or_err
= GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus
);
1922 if (auto err
= ts_or_err
.takeError())
1924 auto ts
= *ts_or_err
;
1928 ts
->GetNativePDBParser()->GetOrCreateTypedefDecl(id
);
1932 toOpaqueUid(id
), ConstString(udt
.Name
), target_type
->GetByteSize(nullptr),
1933 nullptr, target_type
->GetID(), lldb_private::Type::eEncodingIsTypedefUID
,
1934 decl
, target_type
->GetForwardCompilerType(),
1935 lldb_private::Type::ResolveState::Forward
);
1938 TypeSP
SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id
) {
1939 auto iter
= m_types
.find(toOpaqueUid(id
));
1940 if (iter
!= m_types
.end())
1941 return iter
->second
;
1943 return CreateTypedef(id
);
1946 size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id
) {
1947 Block
*block
= GetOrCreateBlock(block_id
);
1953 CompilandIndexItem
*cii
= m_index
->compilands().GetCompiland(block_id
.modi
);
1954 CVSymbol sym
= cii
->m_debug_stream
.readSymbolAtOffset(block_id
.offset
);
1955 uint32_t params_remaining
= 0;
1956 switch (sym
.kind()) {
1959 ProcSym
proc(static_cast<SymbolRecordKind
>(sym
.kind()));
1960 cantFail(SymbolDeserializer::deserializeAs
<ProcSym
>(sym
, proc
));
1961 CVType signature
= m_index
->tpi().getType(proc
.FunctionType
);
1962 if (signature
.kind() == LF_PROCEDURE
) {
1963 ProcedureRecord sig
;
1964 if (llvm::Error e
= TypeDeserializer::deserializeAs
<ProcedureRecord
>(
1966 llvm::consumeError(std::move(e
));
1969 params_remaining
= sig
.getParameterCount();
1970 } else if (signature
.kind() == LF_MFUNCTION
) {
1971 MemberFunctionRecord sig
;
1972 if (llvm::Error e
= TypeDeserializer::deserializeAs
<MemberFunctionRecord
>(
1974 llvm::consumeError(std::move(e
));
1977 params_remaining
= sig
.getParameterCount();
1987 lldbassert(false && "Symbol is not a block!");
1991 VariableListSP variables
= block
->GetBlockVariableList(false);
1993 variables
= std::make_shared
<VariableList
>();
1994 block
->SetVariableList(variables
);
1997 CVSymbolArray syms
= limitSymbolArrayToScope(
1998 cii
->m_debug_stream
.getSymbolArray(), block_id
.offset
);
2000 // Skip the first record since it's a PROC32 or BLOCK32, and there's
2001 // no point examining it since we know it's not a local variable.
2003 auto iter
= syms
.begin();
2004 auto end
= syms
.end();
2006 while (iter
!= end
) {
2007 uint32_t record_offset
= iter
.offset();
2008 CVSymbol variable_cvs
= *iter
;
2009 PdbCompilandSymId
child_sym_id(block_id
.modi
, record_offset
);
2012 // If this is a block or inline site, recurse into its children and then
2014 if (variable_cvs
.kind() == S_BLOCK32
||
2015 variable_cvs
.kind() == S_INLINESITE
) {
2016 uint32_t block_end
= getScopeEndOffset(variable_cvs
);
2017 count
+= ParseVariablesForBlock(child_sym_id
);
2018 iter
= syms
.at(block_end
);
2022 bool is_param
= params_remaining
> 0;
2023 VariableSP variable
;
2024 switch (variable_cvs
.kind()) {
2028 variable
= GetOrCreateLocalVariable(block_id
, child_sym_id
, is_param
);
2032 variables
->AddVariableIfUnique(variable
);
2039 // Pass false for set_children, since we call this recursively so that the
2040 // children will call this for themselves.
2041 block
->SetDidParseVariables(true, false);
2046 size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext
&sc
) {
2047 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
2048 lldbassert(sc
.function
|| sc
.comp_unit
);
2050 VariableListSP variables
;
2052 PdbSymUid
block_id(sc
.block
->GetID());
2054 size_t count
= ParseVariablesForBlock(block_id
.asCompilandSym());
2059 PdbSymUid
block_id(sc
.function
->GetID());
2061 size_t count
= ParseVariablesForBlock(block_id
.asCompilandSym());
2066 variables
= sc
.comp_unit
->GetVariableList(false);
2068 variables
= std::make_shared
<VariableList
>();
2069 sc
.comp_unit
->SetVariableList(variables
);
2071 return ParseVariablesForCompileUnit(*sc
.comp_unit
, *variables
);
2074 llvm_unreachable("Unreachable!");
2077 CompilerDecl
SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid
) {
2078 auto ts_or_err
= GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus
);
2079 if (auto err
= ts_or_err
.takeError())
2080 return CompilerDecl();
2081 auto ts
= *ts_or_err
;
2085 if (auto decl
= ts
->GetNativePDBParser()->GetOrCreateDeclForUid(uid
))
2087 return CompilerDecl();
2091 SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid
) {
2092 auto ts_or_err
= GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus
);
2093 if (auto err
= ts_or_err
.takeError())
2095 auto ts
= *ts_or_err
;
2099 PdbAstBuilder
*ast_builder
= ts
->GetNativePDBParser();
2100 clang::DeclContext
*context
=
2101 ast_builder
->GetOrCreateDeclContextForUid(PdbSymUid(uid
));
2105 return ast_builder
->ToCompilerDeclContext(*context
);
2109 SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid
) {
2110 auto ts_or_err
= GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus
);
2111 if (auto err
= ts_or_err
.takeError())
2112 return CompilerDeclContext();
2113 auto ts
= *ts_or_err
;
2117 PdbAstBuilder
*ast_builder
= ts
->GetNativePDBParser();
2118 clang::DeclContext
*context
= ast_builder
->GetParentDeclContext(PdbSymUid(uid
));
2120 return CompilerDeclContext();
2121 return ast_builder
->ToCompilerDeclContext(*context
);
2124 Type
*SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid
) {
2125 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
2126 auto iter
= m_types
.find(type_uid
);
2127 // lldb should not be passing us non-sensical type uids. the only way it
2128 // could have a type uid in the first place is if we handed it out, in which
2129 // case we should know about the type. However, that doesn't mean we've
2130 // instantiated it yet. We can vend out a UID for a future type. So if the
2131 // type doesn't exist, let's instantiate it now.
2132 if (iter
!= m_types
.end())
2133 return &*iter
->second
;
2135 PdbSymUid
uid(type_uid
);
2136 lldbassert(uid
.kind() == PdbSymUidKind::Type
);
2137 PdbTypeSymId type_id
= uid
.asTypeSym();
2138 if (type_id
.index
.isNoneType())
2141 TypeSP type_sp
= CreateAndCacheType(type_id
);
2147 std::optional
<SymbolFile::ArrayInfo
>
2148 SymbolFileNativePDB::GetDynamicArrayInfoForUID(
2149 lldb::user_id_t type_uid
, const lldb_private::ExecutionContext
*exe_ctx
) {
2150 return std::nullopt
;
2153 bool SymbolFileNativePDB::CompleteType(CompilerType
&compiler_type
) {
2154 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
2155 auto ts
= compiler_type
.GetTypeSystem();
2156 auto clang_type_system
= ts
.dyn_cast_or_null
<TypeSystemClang
>();
2157 if (!clang_type_system
)
2160 PdbAstBuilder
*ast_builder
=
2161 static_cast<PdbAstBuilder
*>(clang_type_system
->GetNativePDBParser());
2163 ast_builder
->GetClangASTImporter().CanImport(compiler_type
))
2164 return ast_builder
->GetClangASTImporter().CompleteType(compiler_type
);
2165 clang::QualType qt
=
2166 clang::QualType::getFromOpaquePtr(compiler_type
.GetOpaqueQualType());
2168 return ast_builder
->CompleteType(qt
);
2171 void SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope
*sc_scope
,
2172 TypeClass type_mask
,
2173 lldb_private::TypeList
&type_list
) {}
2175 CompilerDeclContext
SymbolFileNativePDB::FindNamespace(
2176 ConstString name
, const CompilerDeclContext
&parent_decl_ctx
, bool) {
2180 llvm::Expected
<lldb::TypeSystemSP
>
2181 SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language
) {
2182 auto type_system_or_err
=
2183 m_objfile_sp
->GetModule()->GetTypeSystemForLanguage(language
);
2184 if (type_system_or_err
)
2185 if (auto ts
= *type_system_or_err
)
2186 ts
->SetSymbolFile(this);
2187 return type_system_or_err
;
2190 uint64_t SymbolFileNativePDB::GetDebugInfoSize(bool load_all_debug_info
) {
2191 // PDB files are a separate file that contains all debug info.
2192 return m_index
->pdb().getFileSize();
2195 void SymbolFileNativePDB::BuildParentMap() {
2196 LazyRandomTypeCollection
&types
= m_index
->tpi().typeCollection();
2198 llvm::DenseMap
<TypeIndex
, TypeIndex
> forward_to_full
;
2199 llvm::DenseMap
<TypeIndex
, TypeIndex
> full_to_forward
;
2201 struct RecordIndices
{
2206 llvm::StringMap
<RecordIndices
> record_indices
;
2208 for (auto ti
= types
.getFirst(); ti
; ti
= types
.getNext(*ti
)) {
2209 CVType type
= types
.getType(*ti
);
2210 if (!IsTagRecord(type
))
2213 CVTagRecord tag
= CVTagRecord::create(type
);
2215 RecordIndices
&indices
= record_indices
[tag
.asTag().getUniqueName()];
2216 if (tag
.asTag().isForwardRef())
2217 indices
.forward
= *ti
;
2221 if (indices
.full
!= TypeIndex::None() &&
2222 indices
.forward
!= TypeIndex::None()) {
2223 forward_to_full
[indices
.forward
] = indices
.full
;
2224 full_to_forward
[indices
.full
] = indices
.forward
;
2227 // We're looking for LF_NESTTYPE records in the field list, so ignore
2228 // forward references (no field list), and anything without a nested class
2229 // (since there won't be any LF_NESTTYPE records).
2230 if (tag
.asTag().isForwardRef() || !tag
.asTag().containsNestedClass())
2233 struct ProcessTpiStream
: public TypeVisitorCallbacks
{
2234 ProcessTpiStream(PdbIndex
&index
, TypeIndex parent
,
2235 const CVTagRecord
&parent_cvt
,
2236 llvm::DenseMap
<TypeIndex
, TypeIndex
> &parents
)
2237 : index(index
), parents(parents
), parent(parent
),
2238 parent_cvt(parent_cvt
) {}
2241 llvm::DenseMap
<TypeIndex
, TypeIndex
> &parents
;
2243 unsigned unnamed_type_index
= 1;
2245 const CVTagRecord
&parent_cvt
;
2247 llvm::Error
visitKnownMember(CVMemberRecord
&CVR
,
2248 NestedTypeRecord
&Record
) override
{
2249 std::string unnamed_type_name
;
2250 if (Record
.Name
.empty()) {
2252 llvm::formatv("<unnamed-type-$S{0}>", unnamed_type_index
).str();
2253 Record
.Name
= unnamed_type_name
;
2254 ++unnamed_type_index
;
2256 std::optional
<CVTagRecord
> tag
=
2257 GetNestedTagDefinition(Record
, parent_cvt
, index
.tpi());
2259 return llvm::ErrorSuccess();
2261 parents
[Record
.Type
] = parent
;
2262 return llvm::ErrorSuccess();
2266 CVType field_list_cvt
= m_index
->tpi().getType(tag
.asTag().FieldList
);
2267 ProcessTpiStream
process(*m_index
, *ti
, tag
, m_parent_types
);
2268 FieldListRecord field_list
;
2269 if (llvm::Error error
= TypeDeserializer::deserializeAs
<FieldListRecord
>(
2270 field_list_cvt
, field_list
))
2271 llvm::consumeError(std::move(error
));
2272 if (llvm::Error error
= visitMemberRecordStream(field_list
.Data
, process
))
2273 llvm::consumeError(std::move(error
));
2276 // Now that we know the forward -> full mapping of all type indices, we can
2277 // re-write all the indices. At the end of this process, we want a mapping
2278 // consisting of fwd -> full and full -> full for all child -> parent indices.
2279 // We can re-write the values in place, but for the keys, we must save them
2280 // off so that we don't modify the map in place while also iterating it.
2281 std::vector
<TypeIndex
> full_keys
;
2282 std::vector
<TypeIndex
> fwd_keys
;
2283 for (auto &entry
: m_parent_types
) {
2284 TypeIndex key
= entry
.first
;
2285 TypeIndex value
= entry
.second
;
2287 auto iter
= forward_to_full
.find(value
);
2288 if (iter
!= forward_to_full
.end())
2289 entry
.second
= iter
->second
;
2291 iter
= forward_to_full
.find(key
);
2292 if (iter
!= forward_to_full
.end())
2293 fwd_keys
.push_back(key
);
2295 full_keys
.push_back(key
);
2297 for (TypeIndex fwd
: fwd_keys
) {
2298 TypeIndex full
= forward_to_full
[fwd
];
2299 TypeIndex parent_idx
= m_parent_types
[fwd
];
2300 m_parent_types
[full
] = parent_idx
;
2302 for (TypeIndex full
: full_keys
) {
2303 TypeIndex fwd
= full_to_forward
[full
];
2304 m_parent_types
[fwd
] = m_parent_types
[full
];
2308 std::optional
<PdbCompilandSymId
>
2309 SymbolFileNativePDB::FindSymbolScope(PdbCompilandSymId id
) {
2310 CVSymbol sym
= m_index
->ReadSymbolRecord(id
);
2311 if (symbolOpensScope(sym
.kind())) {
2312 // If this exact symbol opens a scope, we can just directly access its
2314 id
.offset
= getScopeParentOffset(sym
);
2315 // Global symbols have parent offset of 0. Return std::nullopt to indicate
2318 return std::nullopt
;
2322 // Otherwise we need to start at the beginning and iterate forward until we
2323 // reach (or pass) this particular symbol
2324 CompilandIndexItem
&cii
= m_index
->compilands().GetOrCreateCompiland(id
.modi
);
2325 const CVSymbolArray
&syms
= cii
.m_debug_stream
.getSymbolArray();
2327 auto begin
= syms
.begin();
2328 auto end
= syms
.at(id
.offset
);
2329 std::vector
<PdbCompilandSymId
> scope_stack
;
2331 while (begin
!= end
) {
2332 if (begin
.offset() > id
.offset
) {
2333 // We passed it. We couldn't even find this symbol record.
2334 lldbassert(false && "Invalid compiland symbol id!");
2335 return std::nullopt
;
2338 // We haven't found the symbol yet. Check if we need to open or close the
2340 if (symbolOpensScope(begin
->kind())) {
2341 // We can use the end offset of the scope to determine whether or not
2342 // we can just outright skip this entire scope.
2343 uint32_t scope_end
= getScopeEndOffset(*begin
);
2344 if (scope_end
< id
.offset
) {
2345 begin
= syms
.at(scope_end
);
2347 // The symbol we're looking for is somewhere in this scope.
2348 scope_stack
.emplace_back(id
.modi
, begin
.offset());
2350 } else if (symbolEndsScope(begin
->kind())) {
2351 scope_stack
.pop_back();
2355 if (scope_stack
.empty())
2356 return std::nullopt
;
2357 // We have a match! Return the top of the stack
2358 return scope_stack
.back();
2361 std::optional
<llvm::codeview::TypeIndex
>
2362 SymbolFileNativePDB::GetParentType(llvm::codeview::TypeIndex ti
) {
2363 auto parent_iter
= m_parent_types
.find(ti
);
2364 if (parent_iter
== m_parent_types
.end())
2365 return std::nullopt
;
2366 return parent_iter
->second
;