1 | //===-- SymbolFileNativePDB.cpp -------------------------------------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "SymbolFileNativePDB.h" |
10 | |
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" |
26 | |
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" |
52 | |
53 | #include "DWARFLocationExpression.h" |
54 | #include "PdbSymUid.h" |
55 | #include "PdbUtil.h" |
56 | #include "UdtRecordCompleter.h" |
57 | #include <optional> |
58 | #include <string_view> |
59 | |
60 | using namespace lldb; |
61 | using namespace lldb_private; |
62 | using namespace npdb; |
63 | using namespace llvm::codeview; |
64 | using namespace llvm::pdb; |
65 | |
66 | char SymbolFileNativePDB::ID; |
67 | |
68 | static lldb::LanguageType TranslateLanguage(PDB_Lang lang) { |
69 | switch (lang) { |
70 | case PDB_Lang::Cpp: |
71 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
72 | case PDB_Lang::C: |
73 | return lldb::LanguageType::eLanguageTypeC; |
74 | case PDB_Lang::Swift: |
75 | return lldb::LanguageType::eLanguageTypeSwift; |
76 | case PDB_Lang::Rust: |
77 | return lldb::LanguageType::eLanguageTypeRust; |
78 | case PDB_Lang::ObjC: |
79 | return lldb::LanguageType::eLanguageTypeObjC; |
80 | case PDB_Lang::ObjCpp: |
81 | return lldb::LanguageType::eLanguageTypeObjC_plus_plus; |
82 | default: |
83 | return lldb::LanguageType::eLanguageTypeUnknown; |
84 | } |
85 | } |
86 | |
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(Path: exe_path); |
92 | |
93 | // If the file isn't a PE/COFF executable, fail. |
94 | if (!expected_binary) { |
95 | llvm::consumeError(Err: expected_binary.takeError()); |
96 | return nullptr; |
97 | } |
98 | OwningBinary<Binary> binary = std::move(*expected_binary); |
99 | |
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>(Val: binary.getBinary()); |
103 | if (!obj) |
104 | return nullptr; |
105 | const llvm::codeview::DebugInfo *pdb_info = nullptr; |
106 | |
107 | // If it doesn't have a debug directory, fail. |
108 | llvm::StringRef pdb_file; |
109 | if (llvm::Error e = obj->getDebugPDBInfo(Info&: pdb_info, PDBFileName&: pdb_file)) { |
110 | consumeError(Err: std::move(e)); |
111 | return nullptr; |
112 | } |
113 | |
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 |
116 | // executable. |
117 | if (!FileSystem::Instance().Exists(path: 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(component: pdb_name).GetPathAsConstString().GetStringRef(); |
121 | } |
122 | |
123 | // If the file is not a PDB or if it doesn't have a matching GUID, fail. |
124 | auto pdb = ObjectFilePDB::loadPDBFile(PdbPath: std::string(pdb_file), Allocator&: allocator); |
125 | if (!pdb) |
126 | return nullptr; |
127 | |
128 | auto expected_info = pdb->getPDBInfoStream(); |
129 | if (!expected_info) { |
130 | llvm::consumeError(Err: expected_info.takeError()); |
131 | return nullptr; |
132 | } |
133 | llvm::codeview::GUID guid; |
134 | memcpy(dest: &guid, src: pdb_info->PDB70.Signature, n: 16); |
135 | |
136 | if (expected_info->getGuid() != guid) |
137 | return nullptr; |
138 | return pdb; |
139 | } |
140 | |
141 | static bool IsFunctionPrologue(const CompilandIndexItem &cci, |
142 | lldb::addr_t addr) { |
143 | // FIXME: Implement this. |
144 | return false; |
145 | } |
146 | |
147 | static bool IsFunctionEpilogue(const CompilandIndexItem &cci, |
148 | lldb::addr_t addr) { |
149 | // FIXME: Implement this. |
150 | return false; |
151 | } |
152 | |
153 | static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) { |
154 | switch (kind) { |
155 | case SimpleTypeKind::Boolean128: |
156 | case SimpleTypeKind::Boolean16: |
157 | case SimpleTypeKind::Boolean32: |
158 | case SimpleTypeKind::Boolean64: |
159 | case SimpleTypeKind::Boolean8: |
160 | return "bool" ; |
161 | case SimpleTypeKind::Byte: |
162 | case SimpleTypeKind::UnsignedCharacter: |
163 | return "unsigned char" ; |
164 | case SimpleTypeKind::NarrowCharacter: |
165 | return "char" ; |
166 | case SimpleTypeKind::SignedCharacter: |
167 | case SimpleTypeKind::SByte: |
168 | return "signed char" ; |
169 | case SimpleTypeKind::Character16: |
170 | return "char16_t" ; |
171 | case SimpleTypeKind::Character32: |
172 | return "char32_t" ; |
173 | case SimpleTypeKind::Character8: |
174 | return "char8_t" ; |
175 | case SimpleTypeKind::Complex80: |
176 | case SimpleTypeKind::Complex64: |
177 | case SimpleTypeKind::Complex32: |
178 | return "complex" ; |
179 | case SimpleTypeKind::Float128: |
180 | case SimpleTypeKind::Float80: |
181 | return "long double" ; |
182 | case SimpleTypeKind::Float64: |
183 | return "double" ; |
184 | case SimpleTypeKind::Float32: |
185 | return "float" ; |
186 | case SimpleTypeKind::Float16: |
187 | return "single" ; |
188 | case SimpleTypeKind::Int128: |
189 | return "__int128" ; |
190 | case SimpleTypeKind::Int64: |
191 | case SimpleTypeKind::Int64Quad: |
192 | return "int64_t" ; |
193 | case SimpleTypeKind::Int32: |
194 | return "int" ; |
195 | case SimpleTypeKind::Int16: |
196 | return "short" ; |
197 | case SimpleTypeKind::UInt128: |
198 | return "unsigned __int128" ; |
199 | case SimpleTypeKind::UInt64: |
200 | case SimpleTypeKind::UInt64Quad: |
201 | return "uint64_t" ; |
202 | case SimpleTypeKind::HResult: |
203 | return "HRESULT" ; |
204 | case SimpleTypeKind::UInt32: |
205 | return "unsigned" ; |
206 | case SimpleTypeKind::UInt16: |
207 | case SimpleTypeKind::UInt16Short: |
208 | return "unsigned short" ; |
209 | case SimpleTypeKind::Int32Long: |
210 | return "long" ; |
211 | case SimpleTypeKind::UInt32Long: |
212 | return "unsigned long" ; |
213 | case SimpleTypeKind::Void: |
214 | return "void" ; |
215 | case SimpleTypeKind::WideCharacter: |
216 | return "wchar_t" ; |
217 | default: |
218 | return "" ; |
219 | } |
220 | } |
221 | |
222 | static bool IsClassRecord(TypeLeafKind kind) { |
223 | switch (kind) { |
224 | case LF_STRUCTURE: |
225 | case LF_CLASS: |
226 | case LF_INTERFACE: |
227 | return true; |
228 | default: |
229 | return false; |
230 | } |
231 | } |
232 | |
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: |
239 | // struct A { |
240 | // struct B {}; |
241 | // using C = B; |
242 | // }; |
243 | // Then in the debug info, this will appear as: |
244 | // LF_STRUCTURE `A::B` [type index = N] |
245 | // LF_STRUCTURE `A` |
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. |
250 | |
251 | // If it's a simple type, then this is something like `using foo = int`. |
252 | if (Record.Type.isSimple()) |
253 | return std::nullopt; |
254 | |
255 | CVType cvt = tpi.getType(Index: Record.Type); |
256 | |
257 | if (!IsTagRecord(cvt)) |
258 | return std::nullopt; |
259 | |
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(type: cvt); |
264 | std::string qname = std::string(parent.asTag().getUniqueName()); |
265 | if (qname.size() < 4 || child.asTag().getUniqueName().size() < 4) |
266 | return std::nullopt; |
267 | |
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]; |
272 | std::string piece; |
273 | if (qname[3] == 'W') |
274 | piece = "4" ; |
275 | piece += Record.Name; |
276 | piece.push_back(c: '@'); |
277 | qname.insert(pos1: 4, str: std::move(piece)); |
278 | if (qname != child.asTag().UniqueName) |
279 | return std::nullopt; |
280 | |
281 | return std::move(child); |
282 | } |
283 | |
284 | void SymbolFileNativePDB::Initialize() { |
285 | PluginManager::RegisterPlugin(name: GetPluginNameStatic(), |
286 | description: GetPluginDescriptionStatic(), create_callback: CreateInstance, |
287 | debugger_init_callback: DebuggerInitialize); |
288 | } |
289 | |
290 | void SymbolFileNativePDB::Terminate() { |
291 | PluginManager::UnregisterPlugin(create_callback: CreateInstance); |
292 | } |
293 | |
294 | void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {} |
295 | |
296 | llvm::StringRef SymbolFileNativePDB::GetPluginDescriptionStatic() { |
297 | return "Microsoft PDB debug symbol cross-platform file reader." ; |
298 | } |
299 | |
300 | SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFileSP objfile_sp) { |
301 | return new SymbolFileNativePDB(std::move(objfile_sp)); |
302 | } |
303 | |
304 | SymbolFileNativePDB::SymbolFileNativePDB(ObjectFileSP objfile_sp) |
305 | : SymbolFileCommon(std::move(objfile_sp)) {} |
306 | |
307 | SymbolFileNativePDB::~SymbolFileNativePDB() = default; |
308 | |
309 | uint32_t SymbolFileNativePDB::CalculateAbilities() { |
310 | uint32_t abilities = 0; |
311 | if (!m_objfile_sp) |
312 | return 0; |
313 | |
314 | if (!m_index) { |
315 | // Lazily load and match the PDB file, but only do this once. |
316 | PDBFile *pdb_file; |
317 | if (auto *pdb = llvm::dyn_cast<ObjectFilePDB>(Val: m_objfile_sp.get())) { |
318 | pdb_file = &pdb->GetPDBFile(); |
319 | } else { |
320 | m_file_up = loadMatchingPDBFile(exe_path: m_objfile_sp->GetFileSpec().GetPath(), |
321 | allocator&: m_allocator); |
322 | pdb_file = m_file_up.get(); |
323 | } |
324 | |
325 | if (!pdb_file) |
326 | return 0; |
327 | |
328 | auto expected_index = PdbIndex::create(pdb_file); |
329 | if (!expected_index) { |
330 | llvm::consumeError(Err: expected_index.takeError()); |
331 | return 0; |
332 | } |
333 | m_index = std::move(*expected_index); |
334 | } |
335 | if (!m_index) |
336 | return 0; |
337 | |
338 | // We don't especially have to be precise here. We only distinguish between |
339 | // stripped and not stripped. |
340 | abilities = kAllAbilities; |
341 | |
342 | if (m_index->dbi().isStripped()) |
343 | abilities &= ~(Blocks | LocalVariables); |
344 | return abilities; |
345 | } |
346 | |
347 | void SymbolFileNativePDB::InitializeObject() { |
348 | m_obj_load_address = m_objfile_sp->GetModule() |
349 | ->GetObjectFile() |
350 | ->GetBaseAddress() |
351 | .GetFileAddress(); |
352 | m_index->SetLoadAddress(m_obj_load_address); |
353 | m_index->ParseSectionContribs(); |
354 | |
355 | auto ts_or_err = m_objfile_sp->GetModule()->GetTypeSystemForLanguage( |
356 | language: 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}" ); |
360 | } else { |
361 | if (auto ts = *ts_or_err) |
362 | ts->SetSymbolFile(this); |
363 | BuildParentMap(); |
364 | } |
365 | } |
366 | |
367 | uint32_t SymbolFileNativePDB::CalculateNumCompileUnits() { |
368 | const DbiModuleList &modules = m_index->dbi().modules(); |
369 | uint32_t count = modules.getModuleCount(); |
370 | if (count == 0) |
371 | return count; |
372 | |
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(Modi: count - 1); |
377 | if (last.getModuleName() == "* Linker *" ) |
378 | --count; |
379 | return count; |
380 | } |
381 | |
382 | Block *SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) { |
383 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi: block_id.modi); |
384 | CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(Offset: block_id.offset); |
385 | CompUnitSP comp_unit = GetOrCreateCompileUnit(cci: *cii); |
386 | lldb::user_id_t opaque_block_uid = toOpaqueUid(cid: block_id); |
387 | auto ts_or_err = GetTypeSystemForLanguage(language: comp_unit->GetLanguage()); |
388 | if (auto err = ts_or_err.takeError()) |
389 | return nullptr; |
390 | auto ts = *ts_or_err; |
391 | if (!ts) |
392 | return nullptr; |
393 | PdbAstBuilder* ast_builder = ts->GetNativePDBParser(); |
394 | |
395 | switch (sym.kind()) { |
396 | case S_GPROC32: |
397 | case S_LPROC32: |
398 | // This is a function. It must be global. Creating the Function entry |
399 | // for it automatically creates a block for it. |
400 | if (FunctionSP func = GetOrCreateFunction(func_id: block_id, comp_unit&: *comp_unit)) |
401 | return &func->GetBlock(can_create: false); |
402 | break; |
403 | case S_BLOCK32: { |
404 | // This is a block. Its parent is either a function or another block. In |
405 | // either case, its parent can be viewed as a block (e.g. a function |
406 | // contains 1 big block. So just get the parent block and add this block |
407 | // to it. |
408 | BlockSym block(static_cast<SymbolRecordKind>(sym.kind())); |
409 | cantFail(Err: SymbolDeserializer::deserializeAs<BlockSym>(Symbol: sym, Record&: block)); |
410 | lldbassert(block.Parent != 0); |
411 | PdbCompilandSymId parent_id(block_id.modi, block.Parent); |
412 | Block *parent_block = GetOrCreateBlock(block_id: parent_id); |
413 | if (!parent_block) |
414 | return nullptr; |
415 | Function *func = parent_block->CalculateSymbolContextFunction(); |
416 | lldbassert(func); |
417 | lldb::addr_t block_base = |
418 | m_index->MakeVirtualAddress(segment: block.Segment, offset: block.CodeOffset); |
419 | lldb::addr_t func_base = func->GetAddress().GetFileAddress(); |
420 | BlockSP child_block = parent_block->CreateChild(uid: opaque_block_uid); |
421 | if (block_base >= func_base) |
422 | child_block->AddRange(range: Block::Range(block_base - func_base, block.CodeSize)); |
423 | else { |
424 | GetObjectFile()->GetModule()->ReportError( |
425 | format: "S_BLOCK32 at modi: {0:d} offset: {1:d}: adding range " |
426 | "[{2:x16}-{3:x16}) which has a base that is less than the " |
427 | "function's " |
428 | "low PC 0x%" PRIx64 ". Please file a bug and attach the file at the " |
429 | "start of this error message" , |
430 | args&: block_id.modi, args&: block_id.offset, args&: block_base, |
431 | args: block_base + block.CodeSize, args&: func_base); |
432 | } |
433 | ast_builder->GetOrCreateBlockDecl(block_id); |
434 | m_blocks.insert(KV: {opaque_block_uid, child_block}); |
435 | break; |
436 | } |
437 | case S_INLINESITE: { |
438 | // This ensures line table is parsed first so we have inline sites info. |
439 | comp_unit->GetLineTable(); |
440 | |
441 | std::shared_ptr<InlineSite> inline_site = m_inline_sites[opaque_block_uid]; |
442 | Block *parent_block = GetOrCreateBlock(block_id: inline_site->parent_id); |
443 | if (!parent_block) |
444 | return nullptr; |
445 | BlockSP child_block = parent_block->CreateChild(uid: opaque_block_uid); |
446 | ast_builder->GetOrCreateInlinedFunctionDecl(inlinesite_id: block_id); |
447 | // Copy ranges from InlineSite to Block. |
448 | for (size_t i = 0; i < inline_site->ranges.GetSize(); ++i) { |
449 | auto *entry = inline_site->ranges.GetEntryAtIndex(i); |
450 | child_block->AddRange( |
451 | range: Block::Range(entry->GetRangeBase(), entry->GetByteSize())); |
452 | } |
453 | child_block->FinalizeRanges(); |
454 | |
455 | // Get the inlined function callsite info. |
456 | Declaration &decl = inline_site->inline_function_info->GetDeclaration(); |
457 | Declaration &callsite = inline_site->inline_function_info->GetCallSite(); |
458 | child_block->SetInlinedFunctionInfo( |
459 | name: inline_site->inline_function_info->GetName().GetCString(), mangled: nullptr, |
460 | decl_ptr: &decl, call_decl_ptr: &callsite); |
461 | m_blocks.insert(KV: {opaque_block_uid, child_block}); |
462 | break; |
463 | } |
464 | default: |
465 | lldbassert(false && "Symbol is not a block!" ); |
466 | } |
467 | |
468 | return nullptr; |
469 | } |
470 | |
471 | lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id, |
472 | CompileUnit &comp_unit) { |
473 | const CompilandIndexItem *cci = |
474 | m_index->compilands().GetCompiland(modi: func_id.modi); |
475 | lldbassert(cci); |
476 | CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(Offset: func_id.offset); |
477 | |
478 | lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32); |
479 | SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym: sym_record); |
480 | |
481 | auto file_vm_addr = |
482 | m_index->MakeVirtualAddress(segment: sol.so.segment, offset: sol.so.offset); |
483 | if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) |
484 | return nullptr; |
485 | |
486 | Address func_addr(file_vm_addr, comp_unit.GetModule()->GetSectionList()); |
487 | if (!func_addr.IsValid()) |
488 | return nullptr; |
489 | |
490 | ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind())); |
491 | cantFail(Err: SymbolDeserializer::deserializeAs<ProcSym>(Symbol: sym_record, Record&: proc)); |
492 | if (proc.FunctionType == TypeIndex::None()) |
493 | return nullptr; |
494 | TypeSP func_type = GetOrCreateType(ti: proc.FunctionType); |
495 | if (!func_type) |
496 | return nullptr; |
497 | |
498 | PdbTypeSymId sig_id(proc.FunctionType, false); |
499 | Mangled mangled(proc.Name); |
500 | FunctionSP func_sp = std::make_shared<Function>( |
501 | args: &comp_unit, args: toOpaqueUid(cid: func_id), args: toOpaqueUid(cid: sig_id), args&: mangled, |
502 | args: func_type.get(), args&: func_addr, |
503 | args: AddressRanges{AddressRange(func_addr, sol.length)}); |
504 | |
505 | comp_unit.AddFunction(function_sp&: func_sp); |
506 | |
507 | auto ts_or_err = GetTypeSystemForLanguage(language: comp_unit.GetLanguage()); |
508 | if (auto err = ts_or_err.takeError()) |
509 | return func_sp; |
510 | auto ts = *ts_or_err; |
511 | if (!ts) |
512 | return func_sp; |
513 | ts->GetNativePDBParser()->GetOrCreateFunctionDecl(func_id); |
514 | |
515 | return func_sp; |
516 | } |
517 | |
518 | CompUnitSP |
519 | SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) { |
520 | lldb::LanguageType lang = |
521 | cci.m_compile_opts ? TranslateLanguage(lang: cci.m_compile_opts->getLanguage()) |
522 | : lldb::eLanguageTypeUnknown; |
523 | |
524 | LazyBool optimized = eLazyBoolNo; |
525 | if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations()) |
526 | optimized = eLazyBoolYes; |
527 | |
528 | llvm::SmallString<64> source_file_name = |
529 | m_index->compilands().GetMainSourceFile(item: cci); |
530 | FileSpec fs(llvm::sys::path::convert_to_slash( |
531 | path: source_file_name, style: llvm::sys::path::Style::windows_backslash)); |
532 | |
533 | CompUnitSP cu_sp = std::make_shared<CompileUnit>( |
534 | args: m_objfile_sp->GetModule(), args: nullptr, args: std::make_shared<SupportFile>(args&: fs), |
535 | args: toOpaqueUid(cid: cci.m_id), args&: lang, args&: optimized); |
536 | |
537 | SetCompileUnitAtIndex(idx: cci.m_id.modi, cu_sp); |
538 | return cu_sp; |
539 | } |
540 | |
541 | lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id, |
542 | const ModifierRecord &mr, |
543 | CompilerType ct) { |
544 | TpiStream &stream = m_index->tpi(); |
545 | |
546 | std::string name; |
547 | if (mr.ModifiedType.isSimple()) |
548 | name = std::string(GetSimpleTypeName(kind: mr.ModifiedType.getSimpleKind())); |
549 | else |
550 | name = computeTypeName(Types&: stream.typeCollection(), Index: mr.ModifiedType); |
551 | Declaration decl; |
552 | lldb::TypeSP modified_type = GetOrCreateType(ti: mr.ModifiedType); |
553 | |
554 | return MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(name), |
555 | byte_size: llvm::expectedToOptional(E: modified_type->GetByteSize(exe_scope: nullptr)), |
556 | context: nullptr, LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
557 | compiler_type_resolve_state: Type::ResolveState::Full); |
558 | } |
559 | |
560 | lldb::TypeSP |
561 | SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id, |
562 | const llvm::codeview::PointerRecord &pr, |
563 | CompilerType ct) { |
564 | TypeSP pointee = GetOrCreateType(ti: pr.ReferentType); |
565 | if (!pointee) |
566 | return nullptr; |
567 | |
568 | if (pr.isPointerToMember()) { |
569 | MemberPointerInfo mpi = pr.getMemberInfo(); |
570 | GetOrCreateType(ti: mpi.ContainingType); |
571 | } |
572 | |
573 | Declaration decl; |
574 | return MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(), byte_size: pr.getSize(), context: nullptr, |
575 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
576 | compiler_type_resolve_state: Type::ResolveState::Full); |
577 | } |
578 | |
579 | lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti, |
580 | CompilerType ct) { |
581 | uint64_t uid = toOpaqueUid(cid: PdbTypeSymId(ti, false)); |
582 | if (ti == TypeIndex::NullptrT()) { |
583 | Declaration decl; |
584 | return MakeType(uid, name: ConstString("std::nullptr_t" ), byte_size: 0, context: nullptr, |
585 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
586 | compiler_type_resolve_state: Type::ResolveState::Full); |
587 | } |
588 | |
589 | if (ti.getSimpleMode() != SimpleTypeMode::Direct) { |
590 | TypeSP direct_sp = GetOrCreateType(ti: ti.makeDirect()); |
591 | uint32_t pointer_size = 0; |
592 | switch (ti.getSimpleMode()) { |
593 | case SimpleTypeMode::FarPointer32: |
594 | case SimpleTypeMode::NearPointer32: |
595 | pointer_size = 4; |
596 | break; |
597 | case SimpleTypeMode::NearPointer64: |
598 | pointer_size = 8; |
599 | break; |
600 | default: |
601 | // 128-bit and 16-bit pointers unsupported. |
602 | return nullptr; |
603 | } |
604 | Declaration decl; |
605 | return MakeType(uid, name: ConstString(), byte_size: pointer_size, context: nullptr, LLDB_INVALID_UID, |
606 | encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, compiler_type_resolve_state: Type::ResolveState::Full); |
607 | } |
608 | |
609 | if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated) |
610 | return nullptr; |
611 | |
612 | size_t size = GetTypeSizeForSimpleKind(kind: ti.getSimpleKind()); |
613 | llvm::StringRef type_name = GetSimpleTypeName(kind: ti.getSimpleKind()); |
614 | |
615 | Declaration decl; |
616 | return MakeType(uid, name: ConstString(type_name), byte_size: size, context: nullptr, LLDB_INVALID_UID, |
617 | encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, compiler_type_resolve_state: Type::ResolveState::Full); |
618 | } |
619 | |
620 | static std::string GetUnqualifiedTypeName(const TagRecord &record) { |
621 | if (!record.hasUniqueName()) { |
622 | MSVCUndecoratedNameParser parser(record.Name); |
623 | llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers(); |
624 | |
625 | return std::string(specs.back().GetBaseName()); |
626 | } |
627 | |
628 | llvm::ms_demangle::Demangler demangler; |
629 | std::string_view sv(record.UniqueName.begin(), record.UniqueName.size()); |
630 | llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(MangledName&: sv); |
631 | if (demangler.Error) |
632 | return std::string(record.Name); |
633 | |
634 | llvm::ms_demangle::IdentifierNode *idn = |
635 | ttn->QualifiedName->getUnqualifiedIdentifier(); |
636 | return idn->toString(); |
637 | } |
638 | |
639 | lldb::TypeSP |
640 | SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id, |
641 | const TagRecord &record, |
642 | size_t size, CompilerType ct) { |
643 | |
644 | std::string uname = GetUnqualifiedTypeName(record); |
645 | |
646 | // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE. |
647 | Declaration decl; |
648 | return MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(uname), byte_size: size, context: nullptr, |
649 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
650 | compiler_type_resolve_state: Type::ResolveState::Forward); |
651 | } |
652 | |
653 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
654 | const ClassRecord &cr, |
655 | CompilerType ct) { |
656 | return CreateClassStructUnion(type_id, record: cr, size: cr.getSize(), ct); |
657 | } |
658 | |
659 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
660 | const UnionRecord &ur, |
661 | CompilerType ct) { |
662 | return CreateClassStructUnion(type_id, record: ur, size: ur.getSize(), ct); |
663 | } |
664 | |
665 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
666 | const EnumRecord &er, |
667 | CompilerType ct) { |
668 | std::string uname = GetUnqualifiedTypeName(record: er); |
669 | |
670 | Declaration decl; |
671 | TypeSP underlying_type = GetOrCreateType(ti: er.UnderlyingType); |
672 | |
673 | return MakeType( |
674 | uid: toOpaqueUid(cid: type_id), name: ConstString(uname), |
675 | byte_size: llvm::expectedToOptional(E: underlying_type->GetByteSize(exe_scope: nullptr)), context: nullptr, |
676 | LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
677 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Forward); |
678 | } |
679 | |
680 | TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id, |
681 | const ArrayRecord &ar, |
682 | CompilerType ct) { |
683 | TypeSP element_type = GetOrCreateType(ti: ar.ElementType); |
684 | |
685 | Declaration decl; |
686 | TypeSP array_sp = |
687 | MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(), byte_size: ar.Size, context: nullptr, |
688 | LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, compiler_qual_type: ct, |
689 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
690 | array_sp->SetEncodingType(element_type.get()); |
691 | return array_sp; |
692 | } |
693 | |
694 | TypeSP SymbolFileNativePDB::CreateFunctionType(PdbTypeSymId type_id, |
695 | const MemberFunctionRecord &mfr, |
696 | CompilerType ct) { |
697 | Declaration decl; |
698 | return MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(), byte_size: 0, context: nullptr, |
699 | LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, |
700 | compiler_qual_type: ct, compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
701 | } |
702 | |
703 | TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id, |
704 | const ProcedureRecord &pr, |
705 | CompilerType ct) { |
706 | Declaration decl; |
707 | return MakeType(uid: toOpaqueUid(cid: type_id), name: ConstString(), byte_size: 0, context: nullptr, |
708 | LLDB_INVALID_UID, encoding_uid_type: lldb_private::Type::eEncodingIsUID, decl, |
709 | compiler_qual_type: ct, compiler_type_resolve_state: lldb_private::Type::ResolveState::Full); |
710 | } |
711 | |
712 | TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) { |
713 | if (type_id.index.isSimple()) |
714 | return CreateSimpleType(ti: type_id.index, ct); |
715 | |
716 | TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi(); |
717 | CVType cvt = stream.getType(Index: type_id.index); |
718 | |
719 | if (cvt.kind() == LF_MODIFIER) { |
720 | ModifierRecord modifier; |
721 | llvm::cantFail( |
722 | Err: TypeDeserializer::deserializeAs<ModifierRecord>(CVT&: cvt, Record&: modifier)); |
723 | return CreateModifierType(type_id, mr: modifier, ct); |
724 | } |
725 | |
726 | if (cvt.kind() == LF_POINTER) { |
727 | PointerRecord pointer; |
728 | llvm::cantFail( |
729 | Err: TypeDeserializer::deserializeAs<PointerRecord>(CVT&: cvt, Record&: pointer)); |
730 | return CreatePointerType(type_id, pr: pointer, ct); |
731 | } |
732 | |
733 | if (IsClassRecord(kind: cvt.kind())) { |
734 | ClassRecord cr; |
735 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<ClassRecord>(CVT&: cvt, Record&: cr)); |
736 | return CreateTagType(type_id, cr, ct); |
737 | } |
738 | |
739 | if (cvt.kind() == LF_ENUM) { |
740 | EnumRecord er; |
741 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<EnumRecord>(CVT&: cvt, Record&: er)); |
742 | return CreateTagType(type_id, er, ct); |
743 | } |
744 | |
745 | if (cvt.kind() == LF_UNION) { |
746 | UnionRecord ur; |
747 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<UnionRecord>(CVT&: cvt, Record&: ur)); |
748 | return CreateTagType(type_id, ur, ct); |
749 | } |
750 | |
751 | if (cvt.kind() == LF_ARRAY) { |
752 | ArrayRecord ar; |
753 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<ArrayRecord>(CVT&: cvt, Record&: ar)); |
754 | return CreateArrayType(type_id, ar, ct); |
755 | } |
756 | |
757 | if (cvt.kind() == LF_PROCEDURE) { |
758 | ProcedureRecord pr; |
759 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<ProcedureRecord>(CVT&: cvt, Record&: pr)); |
760 | return CreateProcedureType(type_id, pr, ct); |
761 | } |
762 | if (cvt.kind() == LF_MFUNCTION) { |
763 | MemberFunctionRecord mfr; |
764 | llvm::cantFail(Err: TypeDeserializer::deserializeAs<MemberFunctionRecord>(CVT&: cvt, Record&: mfr)); |
765 | return CreateFunctionType(type_id, mfr, ct); |
766 | } |
767 | |
768 | return nullptr; |
769 | } |
770 | |
771 | TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) { |
772 | // If they search for a UDT which is a forward ref, try and resolve the full |
773 | // decl and just map the forward ref uid to the full decl record. |
774 | std::optional<PdbTypeSymId> full_decl_uid; |
775 | if (IsForwardRefUdt(id: type_id, tpi&: m_index->tpi())) { |
776 | auto expected_full_ti = |
777 | m_index->tpi().findFullDeclForForwardRef(ForwardRefTI: type_id.index); |
778 | if (!expected_full_ti) |
779 | llvm::consumeError(Err: expected_full_ti.takeError()); |
780 | else if (*expected_full_ti != type_id.index) { |
781 | full_decl_uid = PdbTypeSymId(*expected_full_ti, false); |
782 | |
783 | // It's possible that a lookup would occur for the full decl causing it |
784 | // to be cached, then a second lookup would occur for the forward decl. |
785 | // We don't want to create a second full decl, so make sure the full |
786 | // decl hasn't already been cached. |
787 | auto full_iter = m_types.find(Val: toOpaqueUid(cid: *full_decl_uid)); |
788 | if (full_iter != m_types.end()) { |
789 | TypeSP result = full_iter->second; |
790 | // Map the forward decl to the TypeSP for the full decl so we can take |
791 | // the fast path next time. |
792 | m_types[toOpaqueUid(cid: type_id)] = result; |
793 | return result; |
794 | } |
795 | } |
796 | } |
797 | |
798 | PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id; |
799 | auto ts_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus); |
800 | if (auto err = ts_or_err.takeError()) |
801 | return nullptr; |
802 | auto ts = *ts_or_err; |
803 | if (!ts) |
804 | return nullptr; |
805 | |
806 | PdbAstBuilder* ast_builder = ts->GetNativePDBParser(); |
807 | clang::QualType qt = ast_builder->GetOrCreateType(type: best_decl_id); |
808 | if (qt.isNull()) |
809 | return nullptr; |
810 | |
811 | TypeSP result = CreateType(type_id: best_decl_id, ct: ast_builder->ToCompilerType(qt)); |
812 | if (!result) |
813 | return nullptr; |
814 | |
815 | uint64_t best_uid = toOpaqueUid(cid: best_decl_id); |
816 | m_types[best_uid] = result; |
817 | // If we had both a forward decl and a full decl, make both point to the new |
818 | // type. |
819 | if (full_decl_uid) |
820 | m_types[toOpaqueUid(cid: type_id)] = result; |
821 | |
822 | return result; |
823 | } |
824 | |
825 | TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) { |
826 | // We can't use try_emplace / overwrite here because the process of creating |
827 | // a type could create nested types, which could invalidate iterators. So |
828 | // we have to do a 2-phase lookup / insert. |
829 | auto iter = m_types.find(Val: toOpaqueUid(cid: type_id)); |
830 | if (iter != m_types.end()) |
831 | return iter->second; |
832 | |
833 | TypeSP type = CreateAndCacheType(type_id); |
834 | if (type) |
835 | GetTypeList().Insert(type); |
836 | return type; |
837 | } |
838 | |
839 | VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) { |
840 | CVSymbol sym = m_index->symrecords().readRecord(Offset: var_id.offset); |
841 | if (sym.kind() == S_CONSTANT) |
842 | return CreateConstantSymbol(var_id, cvs: sym); |
843 | |
844 | lldb::ValueType scope = eValueTypeInvalid; |
845 | TypeIndex ti; |
846 | llvm::StringRef name; |
847 | lldb::addr_t addr = 0; |
848 | uint16_t section = 0; |
849 | uint32_t offset = 0; |
850 | bool is_external = false; |
851 | switch (sym.kind()) { |
852 | case S_GDATA32: |
853 | is_external = true; |
854 | [[fallthrough]]; |
855 | case S_LDATA32: { |
856 | DataSym ds(sym.kind()); |
857 | llvm::cantFail(Err: SymbolDeserializer::deserializeAs<DataSym>(Symbol: sym, Record&: ds)); |
858 | ti = ds.Type; |
859 | scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal |
860 | : eValueTypeVariableStatic; |
861 | name = ds.Name; |
862 | section = ds.Segment; |
863 | offset = ds.DataOffset; |
864 | addr = m_index->MakeVirtualAddress(segment: ds.Segment, offset: ds.DataOffset); |
865 | break; |
866 | } |
867 | case S_GTHREAD32: |
868 | is_external = true; |
869 | [[fallthrough]]; |
870 | case S_LTHREAD32: { |
871 | ThreadLocalDataSym tlds(sym.kind()); |
872 | llvm::cantFail( |
873 | Err: SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(Symbol: sym, Record&: tlds)); |
874 | ti = tlds.Type; |
875 | name = tlds.Name; |
876 | section = tlds.Segment; |
877 | offset = tlds.DataOffset; |
878 | addr = m_index->MakeVirtualAddress(segment: tlds.Segment, offset: tlds.DataOffset); |
879 | scope = eValueTypeVariableThreadLocal; |
880 | break; |
881 | } |
882 | default: |
883 | llvm_unreachable("unreachable!" ); |
884 | } |
885 | |
886 | CompUnitSP comp_unit; |
887 | std::optional<uint16_t> modi = m_index->GetModuleIndexForVa(va: addr); |
888 | // Some globals has modi points to the linker module, ignore them. |
889 | if (!modi || modi >= GetNumCompileUnits()) |
890 | return nullptr; |
891 | |
892 | CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(modi: *modi); |
893 | comp_unit = GetOrCreateCompileUnit(cci); |
894 | |
895 | Declaration decl; |
896 | PdbTypeSymId tid(ti, false); |
897 | SymbolFileTypeSP type_sp = |
898 | std::make_shared<SymbolFileType>(args&: *this, args: toOpaqueUid(cid: tid)); |
899 | Variable::RangeList ranges; |
900 | auto ts_or_err = GetTypeSystemForLanguage(language: comp_unit->GetLanguage()); |
901 | if (auto err = ts_or_err.takeError()) |
902 | return nullptr; |
903 | auto ts = *ts_or_err; |
904 | if (!ts) |
905 | return nullptr; |
906 | |
907 | ts->GetNativePDBParser()->GetOrCreateVariableDecl(var_id); |
908 | |
909 | ModuleSP module_sp = GetObjectFile()->GetModule(); |
910 | DWARFExpressionList location( |
911 | module_sp, MakeGlobalLocationExpression(section, offset, module: module_sp), |
912 | nullptr); |
913 | |
914 | std::string global_name("::" ); |
915 | global_name += name; |
916 | bool artificial = false; |
917 | bool location_is_constant_data = false; |
918 | bool static_member = false; |
919 | VariableSP var_sp = std::make_shared<Variable>( |
920 | args: toOpaqueUid(cid: var_id), args: name.str().c_str(), args: global_name.c_str(), args&: type_sp, |
921 | args&: scope, args: comp_unit.get(), args&: ranges, args: &decl, args&: location, args&: is_external, args&: artificial, |
922 | args&: location_is_constant_data, args&: static_member); |
923 | |
924 | return var_sp; |
925 | } |
926 | |
927 | lldb::VariableSP |
928 | SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id, |
929 | const CVSymbol &cvs) { |
930 | TpiStream &tpi = m_index->tpi(); |
931 | ConstantSym constant(cvs.kind()); |
932 | |
933 | llvm::cantFail(Err: SymbolDeserializer::deserializeAs<ConstantSym>(Symbol: cvs, Record&: constant)); |
934 | std::string global_name("::" ); |
935 | global_name += constant.Name; |
936 | PdbTypeSymId tid(constant.Type, false); |
937 | SymbolFileTypeSP type_sp = |
938 | std::make_shared<SymbolFileType>(args&: *this, args: toOpaqueUid(cid: tid)); |
939 | |
940 | Declaration decl; |
941 | Variable::RangeList ranges; |
942 | ModuleSP module = GetObjectFile()->GetModule(); |
943 | DWARFExpressionList location(module, |
944 | MakeConstantLocationExpression( |
945 | underlying_ti: constant.Type, tpi, constant: constant.Value, module), |
946 | nullptr); |
947 | |
948 | bool external = false; |
949 | bool artificial = false; |
950 | bool location_is_constant_data = true; |
951 | bool static_member = false; |
952 | VariableSP var_sp = std::make_shared<Variable>( |
953 | args: toOpaqueUid(cid: var_id), args: constant.Name.str().c_str(), args: global_name.c_str(), |
954 | args&: type_sp, args: eValueTypeVariableGlobal, args: module.get(), args&: ranges, args: &decl, args&: location, |
955 | args&: external, args&: artificial, args&: location_is_constant_data, args&: static_member); |
956 | return var_sp; |
957 | } |
958 | |
959 | VariableSP |
960 | SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) { |
961 | auto emplace_result = m_global_vars.try_emplace(Key: toOpaqueUid(cid: var_id), Args: nullptr); |
962 | if (emplace_result.second) { |
963 | if (VariableSP var_sp = CreateGlobalVariable(var_id)) |
964 | emplace_result.first->second = var_sp; |
965 | else |
966 | return nullptr; |
967 | } |
968 | |
969 | return emplace_result.first->second; |
970 | } |
971 | |
972 | lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) { |
973 | return GetOrCreateType(type_id: PdbTypeSymId(ti, false)); |
974 | } |
975 | |
976 | FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id, |
977 | CompileUnit &comp_unit) { |
978 | auto emplace_result = m_functions.try_emplace(Key: toOpaqueUid(cid: func_id), Args: nullptr); |
979 | if (emplace_result.second) |
980 | emplace_result.first->second = CreateFunction(func_id, comp_unit); |
981 | |
982 | return emplace_result.first->second; |
983 | } |
984 | |
985 | CompUnitSP |
986 | SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) { |
987 | |
988 | auto emplace_result = |
989 | m_compilands.try_emplace(Key: toOpaqueUid(cid: cci.m_id), Args: nullptr); |
990 | if (emplace_result.second) |
991 | emplace_result.first->second = CreateCompileUnit(cci); |
992 | |
993 | lldbassert(emplace_result.first->second); |
994 | return emplace_result.first->second; |
995 | } |
996 | |
997 | Block *SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) { |
998 | auto iter = m_blocks.find(Val: toOpaqueUid(cid: block_id)); |
999 | if (iter != m_blocks.end()) |
1000 | return iter->second.get(); |
1001 | |
1002 | return CreateBlock(block_id); |
1003 | } |
1004 | |
1005 | void SymbolFileNativePDB::ParseDeclsForContext( |
1006 | lldb_private::CompilerDeclContext decl_ctx) { |
1007 | TypeSystem* ts_or_err = decl_ctx.GetTypeSystem(); |
1008 | if (!ts_or_err) |
1009 | return; |
1010 | PdbAstBuilder* ast_builder = ts_or_err->GetNativePDBParser(); |
1011 | clang::DeclContext *context = ast_builder->FromCompilerDeclContext(context: decl_ctx); |
1012 | if (!context) |
1013 | return; |
1014 | ast_builder->ParseDeclsForContext(context&: *context); |
1015 | } |
1016 | |
1017 | lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) { |
1018 | if (index >= GetNumCompileUnits()) |
1019 | return CompUnitSP(); |
1020 | lldbassert(index < UINT16_MAX); |
1021 | if (index >= UINT16_MAX) |
1022 | return nullptr; |
1023 | |
1024 | CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(modi: index); |
1025 | |
1026 | return GetOrCreateCompileUnit(cci: item); |
1027 | } |
1028 | |
1029 | lldb::LanguageType SymbolFileNativePDB::ParseLanguage(CompileUnit &comp_unit) { |
1030 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1031 | PdbSymUid uid(comp_unit.GetID()); |
1032 | lldbassert(uid.kind() == PdbSymUidKind::Compiland); |
1033 | |
1034 | CompilandIndexItem *item = |
1035 | m_index->compilands().GetCompiland(modi: uid.asCompiland().modi); |
1036 | lldbassert(item); |
1037 | if (!item->m_compile_opts) |
1038 | return lldb::eLanguageTypeUnknown; |
1039 | |
1040 | return TranslateLanguage(lang: item->m_compile_opts->getLanguage()); |
1041 | } |
1042 | |
1043 | void SymbolFileNativePDB::AddSymbols(Symtab &symtab) {} |
1044 | |
1045 | size_t SymbolFileNativePDB::ParseFunctions(CompileUnit &comp_unit) { |
1046 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1047 | PdbSymUid uid{comp_unit.GetID()}; |
1048 | lldbassert(uid.kind() == PdbSymUidKind::Compiland); |
1049 | uint16_t modi = uid.asCompiland().modi; |
1050 | CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi); |
1051 | |
1052 | size_t count = comp_unit.GetNumFunctions(); |
1053 | const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray(); |
1054 | for (auto iter = syms.begin(); iter != syms.end(); ++iter) { |
1055 | if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32) |
1056 | continue; |
1057 | |
1058 | PdbCompilandSymId sym_id{modi, iter.offset()}; |
1059 | |
1060 | FunctionSP func = GetOrCreateFunction(func_id: sym_id, comp_unit); |
1061 | } |
1062 | |
1063 | size_t new_count = comp_unit.GetNumFunctions(); |
1064 | lldbassert(new_count >= count); |
1065 | return new_count - count; |
1066 | } |
1067 | |
1068 | static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) { |
1069 | // If any of these flags are set, we need to resolve the compile unit. |
1070 | uint32_t flags = eSymbolContextCompUnit; |
1071 | flags |= eSymbolContextVariable; |
1072 | flags |= eSymbolContextFunction; |
1073 | flags |= eSymbolContextBlock; |
1074 | flags |= eSymbolContextLineEntry; |
1075 | return (resolve_scope & flags) != 0; |
1076 | } |
1077 | |
1078 | uint32_t SymbolFileNativePDB::ResolveSymbolContext( |
1079 | const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) { |
1080 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1081 | uint32_t resolved_flags = 0; |
1082 | lldb::addr_t file_addr = addr.GetFileAddress(); |
1083 | |
1084 | if (NeedsResolvedCompileUnit(resolve_scope)) { |
1085 | std::optional<uint16_t> modi = m_index->GetModuleIndexForVa(va: file_addr); |
1086 | if (!modi) |
1087 | return 0; |
1088 | CompUnitSP cu_sp = GetCompileUnitAtIndex(idx: *modi); |
1089 | if (!cu_sp) |
1090 | return 0; |
1091 | |
1092 | sc.comp_unit = cu_sp.get(); |
1093 | resolved_flags |= eSymbolContextCompUnit; |
1094 | } |
1095 | |
1096 | if (resolve_scope & eSymbolContextFunction || |
1097 | resolve_scope & eSymbolContextBlock) { |
1098 | lldbassert(sc.comp_unit); |
1099 | std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(va: file_addr); |
1100 | // Search the matches in reverse. This way if there are multiple matches |
1101 | // (for example we are 3 levels deep in a nested scope) it will find the |
1102 | // innermost one first. |
1103 | for (const auto &match : llvm::reverse(C&: matches)) { |
1104 | if (match.uid.kind() != PdbSymUidKind::CompilandSym) |
1105 | continue; |
1106 | |
1107 | PdbCompilandSymId csid = match.uid.asCompilandSym(); |
1108 | CVSymbol cvs = m_index->ReadSymbolRecord(cu_sym: csid); |
1109 | PDB_SymType type = CVSymToPDBSym(kind: cvs.kind()); |
1110 | if (type != PDB_SymType::Function && type != PDB_SymType::Block) |
1111 | continue; |
1112 | if (type == PDB_SymType::Function) { |
1113 | sc.function = GetOrCreateFunction(func_id: csid, comp_unit&: *sc.comp_unit).get(); |
1114 | if (sc.function) { |
1115 | Block &block = sc.function->GetBlock(can_create: true); |
1116 | addr_t func_base = sc.function->GetAddress().GetFileAddress(); |
1117 | addr_t offset = file_addr - func_base; |
1118 | sc.block = block.FindInnermostBlockByOffset(offset); |
1119 | } |
1120 | } |
1121 | |
1122 | if (type == PDB_SymType::Block) { |
1123 | Block *block = GetOrCreateBlock(block_id: csid); |
1124 | if (!block) |
1125 | continue; |
1126 | sc.function = block->CalculateSymbolContextFunction(); |
1127 | if (sc.function) { |
1128 | sc.function->GetBlock(can_create: true); |
1129 | addr_t func_base = sc.function->GetAddress().GetFileAddress(); |
1130 | addr_t offset = file_addr - func_base; |
1131 | sc.block = block->FindInnermostBlockByOffset(offset); |
1132 | } |
1133 | } |
1134 | if (sc.function) |
1135 | resolved_flags |= eSymbolContextFunction; |
1136 | if (sc.block) |
1137 | resolved_flags |= eSymbolContextBlock; |
1138 | break; |
1139 | } |
1140 | } |
1141 | |
1142 | if (resolve_scope & eSymbolContextLineEntry) { |
1143 | lldbassert(sc.comp_unit); |
1144 | if (auto *line_table = sc.comp_unit->GetLineTable()) { |
1145 | if (line_table->FindLineEntryByAddress(so_addr: addr, line_entry&: sc.line_entry)) |
1146 | resolved_flags |= eSymbolContextLineEntry; |
1147 | } |
1148 | } |
1149 | |
1150 | return resolved_flags; |
1151 | } |
1152 | |
1153 | uint32_t SymbolFileNativePDB::ResolveSymbolContext( |
1154 | const SourceLocationSpec &src_location_spec, |
1155 | lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { |
1156 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1157 | const uint32_t prev_size = sc_list.GetSize(); |
1158 | if (resolve_scope & eSymbolContextCompUnit) { |
1159 | for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus; |
1160 | ++cu_idx) { |
1161 | CompileUnit *cu = ParseCompileUnitAtIndex(index: cu_idx).get(); |
1162 | if (!cu) |
1163 | continue; |
1164 | |
1165 | bool file_spec_matches_cu_file_spec = FileSpec::Match( |
1166 | pattern: src_location_spec.GetFileSpec(), file: cu->GetPrimaryFile()); |
1167 | if (file_spec_matches_cu_file_spec) { |
1168 | cu->ResolveSymbolContext(src_location_spec, resolve_scope, sc_list); |
1169 | break; |
1170 | } |
1171 | } |
1172 | } |
1173 | return sc_list.GetSize() - prev_size; |
1174 | } |
1175 | |
1176 | bool SymbolFileNativePDB::ParseLineTable(CompileUnit &comp_unit) { |
1177 | // Unfortunately LLDB is set up to parse the entire compile unit line table |
1178 | // all at once, even if all it really needs is line info for a specific |
1179 | // function. In the future it would be nice if it could set the sc.m_function |
1180 | // member, and we could only get the line info for the function in question. |
1181 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1182 | PdbSymUid cu_id(comp_unit.GetID()); |
1183 | lldbassert(cu_id.kind() == PdbSymUidKind::Compiland); |
1184 | uint16_t modi = cu_id.asCompiland().modi; |
1185 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi); |
1186 | lldbassert(cii); |
1187 | |
1188 | // Parse DEBUG_S_LINES subsections first, then parse all S_INLINESITE records |
1189 | // in this CU. Add line entries into the set first so that if there are line |
1190 | // entries with same addres, the later is always more accurate than the |
1191 | // former. |
1192 | std::set<LineTable::Entry, LineTableEntryComparator> line_set; |
1193 | |
1194 | // This is basically a copy of the .debug$S subsections from all original COFF |
1195 | // object files merged together with address relocations applied. We are |
1196 | // looking for all DEBUG_S_LINES subsections. |
1197 | for (const DebugSubsectionRecord &dssr : |
1198 | cii->m_debug_stream.getSubsectionsArray()) { |
1199 | if (dssr.kind() != DebugSubsectionKind::Lines) |
1200 | continue; |
1201 | |
1202 | DebugLinesSubsectionRef lines; |
1203 | llvm::BinaryStreamReader reader(dssr.getRecordData()); |
1204 | if (auto EC = lines.initialize(Reader: reader)) { |
1205 | llvm::consumeError(Err: std::move(EC)); |
1206 | return false; |
1207 | } |
1208 | |
1209 | const LineFragmentHeader *lfh = lines.header(); |
1210 | uint64_t virtual_addr = |
1211 | m_index->MakeVirtualAddress(segment: lfh->RelocSegment, offset: lfh->RelocOffset); |
1212 | if (virtual_addr == LLDB_INVALID_ADDRESS) |
1213 | continue; |
1214 | |
1215 | for (const LineColumnEntry &group : lines) { |
1216 | llvm::Expected<uint32_t> file_index_or_err = |
1217 | GetFileIndex(cii: *cii, file_id: group.NameIndex); |
1218 | if (!file_index_or_err) |
1219 | continue; |
1220 | uint32_t file_index = file_index_or_err.get(); |
1221 | lldbassert(!group.LineNumbers.empty()); |
1222 | CompilandIndexItem::GlobalLineTable::Entry line_entry( |
1223 | LLDB_INVALID_ADDRESS, 0); |
1224 | for (const LineNumberEntry &entry : group.LineNumbers) { |
1225 | LineInfo cur_info(entry.Flags); |
1226 | |
1227 | if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto()) |
1228 | continue; |
1229 | |
1230 | uint64_t addr = virtual_addr + entry.Offset; |
1231 | |
1232 | bool is_statement = cur_info.isStatement(); |
1233 | bool is_prologue = IsFunctionPrologue(cci: *cii, addr); |
1234 | bool is_epilogue = IsFunctionEpilogue(cci: *cii, addr); |
1235 | |
1236 | uint32_t lno = cur_info.getStartLine(); |
1237 | |
1238 | LineTable::Entry new_entry(addr, lno, 0, file_index, is_statement, false, |
1239 | is_prologue, is_epilogue, false); |
1240 | // Terminal entry has lower precedence than new entry. |
1241 | auto iter = line_set.find(x: new_entry); |
1242 | if (iter != line_set.end() && iter->is_terminal_entry) |
1243 | line_set.erase(position: iter); |
1244 | line_set.insert(x: new_entry); |
1245 | |
1246 | if (line_entry.GetRangeBase() != LLDB_INVALID_ADDRESS) { |
1247 | line_entry.SetRangeEnd(addr); |
1248 | cii->m_global_line_table.Append(entry: line_entry); |
1249 | } |
1250 | line_entry.SetRangeBase(addr); |
1251 | line_entry.data = {file_index, lno}; |
1252 | } |
1253 | LineInfo last_line(group.LineNumbers.back().Flags); |
1254 | line_set.emplace(args: virtual_addr + lfh->CodeSize, args: last_line.getEndLine(), args: 0, |
1255 | args&: file_index, args: false, args: false, args: false, args: false, args: true); |
1256 | |
1257 | if (line_entry.GetRangeBase() != LLDB_INVALID_ADDRESS) { |
1258 | line_entry.SetRangeEnd(virtual_addr + lfh->CodeSize); |
1259 | cii->m_global_line_table.Append(entry: line_entry); |
1260 | } |
1261 | } |
1262 | } |
1263 | |
1264 | cii->m_global_line_table.Sort(); |
1265 | |
1266 | // Parse all S_INLINESITE in this CU. |
1267 | const CVSymbolArray &syms = cii->m_debug_stream.getSymbolArray(); |
1268 | for (auto iter = syms.begin(); iter != syms.end();) { |
1269 | if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32) { |
1270 | ++iter; |
1271 | continue; |
1272 | } |
1273 | |
1274 | uint32_t record_offset = iter.offset(); |
1275 | CVSymbol func_record = |
1276 | cii->m_debug_stream.readSymbolAtOffset(Offset: record_offset); |
1277 | SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym: func_record); |
1278 | addr_t file_vm_addr = |
1279 | m_index->MakeVirtualAddress(segment: sol.so.segment, offset: sol.so.offset); |
1280 | if (file_vm_addr == LLDB_INVALID_ADDRESS) |
1281 | continue; |
1282 | |
1283 | Address func_base(file_vm_addr, comp_unit.GetModule()->GetSectionList()); |
1284 | PdbCompilandSymId func_id{modi, record_offset}; |
1285 | |
1286 | // Iterate all S_INLINESITEs in the function. |
1287 | auto parse_inline_sites = [&](SymbolKind kind, PdbCompilandSymId id) { |
1288 | if (kind != S_INLINESITE) |
1289 | return false; |
1290 | |
1291 | ParseInlineSite(inline_site_id: id, func_addr: func_base); |
1292 | |
1293 | for (const auto &line_entry : |
1294 | m_inline_sites[toOpaqueUid(cid: id)]->line_entries) { |
1295 | // If line_entry is not terminal entry, remove previous line entry at |
1296 | // the same address and insert new one. Terminal entry inside an inline |
1297 | // site might not be terminal entry for its parent. |
1298 | if (!line_entry.is_terminal_entry) |
1299 | line_set.erase(x: line_entry); |
1300 | line_set.insert(x: line_entry); |
1301 | } |
1302 | // No longer useful after adding to line_set. |
1303 | m_inline_sites[toOpaqueUid(cid: id)]->line_entries.clear(); |
1304 | return true; |
1305 | }; |
1306 | ParseSymbolArrayInScope(parent: func_id, fn: parse_inline_sites); |
1307 | // Jump to the end of the function record. |
1308 | iter = syms.at(Offset: getScopeEndOffset(Symbol: func_record)); |
1309 | } |
1310 | |
1311 | cii->m_global_line_table.Clear(); |
1312 | |
1313 | // Add line entries in line_set to line_table. |
1314 | std::vector<LineTable::Sequence> sequence(1); |
1315 | for (const auto &line_entry : line_set) { |
1316 | LineTable::AppendLineEntryToSequence( |
1317 | sequence&: sequence.back(), file_addr: line_entry.file_addr, line: line_entry.line, |
1318 | column: line_entry.column, file_idx: line_entry.file_idx, |
1319 | is_start_of_statement: line_entry.is_start_of_statement, is_start_of_basic_block: line_entry.is_start_of_basic_block, |
1320 | is_prologue_end: line_entry.is_prologue_end, is_epilogue_begin: line_entry.is_epilogue_begin, |
1321 | is_terminal_entry: line_entry.is_terminal_entry); |
1322 | } |
1323 | auto line_table = |
1324 | std::make_unique<LineTable>(args: &comp_unit, args: std::move(sequence)); |
1325 | |
1326 | if (line_table->GetSize() == 0) |
1327 | return false; |
1328 | |
1329 | comp_unit.SetLineTable(line_table.release()); |
1330 | return true; |
1331 | } |
1332 | |
1333 | bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit &comp_unit) { |
1334 | // PDB doesn't contain information about macros |
1335 | return false; |
1336 | } |
1337 | |
1338 | llvm::Expected<uint32_t> |
1339 | SymbolFileNativePDB::GetFileIndex(const CompilandIndexItem &cii, |
1340 | uint32_t file_id) { |
1341 | if (!cii.m_strings.hasChecksums() || !cii.m_strings.hasStrings()) |
1342 | return llvm::make_error<RawError>(Args: raw_error_code::no_entry); |
1343 | |
1344 | const auto &checksums = cii.m_strings.checksums().getArray(); |
1345 | const auto &strings = cii.m_strings.strings(); |
1346 | // Indices in this structure are actually offsets of records in the |
1347 | // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index |
1348 | // into the global PDB string table. |
1349 | auto iter = checksums.at(Offset: file_id); |
1350 | if (iter == checksums.end()) |
1351 | return llvm::make_error<RawError>(Args: raw_error_code::no_entry); |
1352 | |
1353 | llvm::Expected<llvm::StringRef> efn = strings.getString(Offset: iter->FileNameOffset); |
1354 | if (!efn) { |
1355 | return efn.takeError(); |
1356 | } |
1357 | |
1358 | // LLDB wants the index of the file in the list of support files. |
1359 | auto fn_iter = llvm::find(Range: cii.m_file_list, Val: *efn); |
1360 | if (fn_iter != cii.m_file_list.end()) |
1361 | return std::distance(first: cii.m_file_list.begin(), last: fn_iter); |
1362 | return llvm::make_error<RawError>(Args: raw_error_code::no_entry); |
1363 | } |
1364 | |
1365 | bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit &comp_unit, |
1366 | SupportFileList &support_files) { |
1367 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1368 | PdbSymUid cu_id(comp_unit.GetID()); |
1369 | lldbassert(cu_id.kind() == PdbSymUidKind::Compiland); |
1370 | CompilandIndexItem *cci = |
1371 | m_index->compilands().GetCompiland(modi: cu_id.asCompiland().modi); |
1372 | lldbassert(cci); |
1373 | |
1374 | for (llvm::StringRef f : cci->m_file_list) { |
1375 | FileSpec::Style style = |
1376 | f.starts_with(Prefix: "/" ) ? FileSpec::Style::posix : FileSpec::Style::windows; |
1377 | FileSpec spec(f, style); |
1378 | support_files.Append(file: spec); |
1379 | } |
1380 | return true; |
1381 | } |
1382 | |
1383 | bool SymbolFileNativePDB::ParseImportedModules( |
1384 | const SymbolContext &sc, std::vector<SourceModule> &imported_modules) { |
1385 | // PDB does not yet support module debug info |
1386 | return false; |
1387 | } |
1388 | |
1389 | void SymbolFileNativePDB::ParseInlineSite(PdbCompilandSymId id, |
1390 | Address func_addr) { |
1391 | lldb::user_id_t opaque_uid = toOpaqueUid(cid: id); |
1392 | if (m_inline_sites.contains(Val: opaque_uid)) |
1393 | return; |
1394 | |
1395 | addr_t func_base = func_addr.GetFileAddress(); |
1396 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi: id.modi); |
1397 | CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(Offset: id.offset); |
1398 | CompUnitSP comp_unit = GetOrCreateCompileUnit(cci: *cii); |
1399 | |
1400 | InlineSiteSym inline_site(static_cast<SymbolRecordKind>(sym.kind())); |
1401 | cantFail(Err: SymbolDeserializer::deserializeAs<InlineSiteSym>(Symbol: sym, Record&: inline_site)); |
1402 | PdbCompilandSymId parent_id(id.modi, inline_site.Parent); |
1403 | |
1404 | std::shared_ptr<InlineSite> inline_site_sp = |
1405 | std::make_shared<InlineSite>(args&: parent_id); |
1406 | |
1407 | // Get the inlined function declaration info. |
1408 | auto iter = cii->m_inline_map.find(x: inline_site.Inlinee); |
1409 | if (iter == cii->m_inline_map.end()) |
1410 | return; |
1411 | InlineeSourceLine inlinee_line = iter->second; |
1412 | |
1413 | const SupportFileList &files = comp_unit->GetSupportFiles(); |
1414 | FileSpec decl_file; |
1415 | llvm::Expected<uint32_t> file_index_or_err = |
1416 | GetFileIndex(cii: *cii, file_id: inlinee_line.Header->FileID); |
1417 | if (!file_index_or_err) |
1418 | return; |
1419 | uint32_t file_offset = file_index_or_err.get(); |
1420 | decl_file = files.GetFileSpecAtIndex(idx: file_offset); |
1421 | uint32_t decl_line = inlinee_line.Header->SourceLineNum; |
1422 | std::unique_ptr<Declaration> decl_up = |
1423 | std::make_unique<Declaration>(args&: decl_file, args&: decl_line); |
1424 | |
1425 | // Parse range and line info. |
1426 | uint32_t code_offset = 0; |
1427 | int32_t line_offset = 0; |
1428 | std::optional<uint32_t> code_offset_base; |
1429 | std::optional<uint32_t> code_offset_end; |
1430 | std::optional<int32_t> cur_line_offset; |
1431 | std::optional<int32_t> next_line_offset; |
1432 | std::optional<uint32_t> next_file_offset; |
1433 | |
1434 | bool is_terminal_entry = false; |
1435 | bool is_start_of_statement = true; |
1436 | // The first instruction is the prologue end. |
1437 | bool is_prologue_end = true; |
1438 | |
1439 | auto update_code_offset = [&](uint32_t code_delta) { |
1440 | if (!code_offset_base) |
1441 | code_offset_base = code_offset; |
1442 | else if (!code_offset_end) |
1443 | code_offset_end = *code_offset_base + code_delta; |
1444 | }; |
1445 | auto update_line_offset = [&](int32_t line_delta) { |
1446 | line_offset += line_delta; |
1447 | if (!code_offset_base || !cur_line_offset) |
1448 | cur_line_offset = line_offset; |
1449 | else |
1450 | next_line_offset = line_offset; |
1451 | ; |
1452 | }; |
1453 | auto update_file_offset = [&](uint32_t offset) { |
1454 | if (!code_offset_base) |
1455 | file_offset = offset; |
1456 | else |
1457 | next_file_offset = offset; |
1458 | }; |
1459 | |
1460 | for (auto &annot : inline_site.annotations()) { |
1461 | switch (annot.OpCode) { |
1462 | case BinaryAnnotationsOpCode::CodeOffset: |
1463 | case BinaryAnnotationsOpCode::ChangeCodeOffset: |
1464 | case BinaryAnnotationsOpCode::ChangeCodeOffsetBase: |
1465 | code_offset += annot.U1; |
1466 | update_code_offset(annot.U1); |
1467 | break; |
1468 | case BinaryAnnotationsOpCode::ChangeLineOffset: |
1469 | update_line_offset(annot.S1); |
1470 | break; |
1471 | case BinaryAnnotationsOpCode::ChangeCodeLength: |
1472 | update_code_offset(annot.U1); |
1473 | code_offset += annot.U1; |
1474 | is_terminal_entry = true; |
1475 | break; |
1476 | case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset: |
1477 | code_offset += annot.U1; |
1478 | update_code_offset(annot.U1); |
1479 | update_line_offset(annot.S1); |
1480 | break; |
1481 | case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset: |
1482 | code_offset += annot.U2; |
1483 | update_code_offset(annot.U2); |
1484 | update_code_offset(annot.U1); |
1485 | code_offset += annot.U1; |
1486 | is_terminal_entry = true; |
1487 | break; |
1488 | case BinaryAnnotationsOpCode::ChangeFile: |
1489 | update_file_offset(annot.U1); |
1490 | break; |
1491 | default: |
1492 | break; |
1493 | } |
1494 | |
1495 | // Add range if current range is finished. |
1496 | if (code_offset_base && code_offset_end && cur_line_offset) { |
1497 | inline_site_sp->ranges.Append(entry: RangeSourceLineVector::Entry( |
1498 | *code_offset_base, *code_offset_end - *code_offset_base, |
1499 | decl_line + *cur_line_offset)); |
1500 | // Set base, end, file offset and line offset for next range. |
1501 | if (next_file_offset) |
1502 | file_offset = *next_file_offset; |
1503 | if (next_line_offset) { |
1504 | cur_line_offset = next_line_offset; |
1505 | next_line_offset = std::nullopt; |
1506 | } |
1507 | code_offset_base = is_terminal_entry ? std::nullopt : code_offset_end; |
1508 | code_offset_end = next_file_offset = std::nullopt; |
1509 | } |
1510 | if (code_offset_base && cur_line_offset) { |
1511 | if (is_terminal_entry) { |
1512 | LineTable::Entry line_entry( |
1513 | func_base + *code_offset_base, decl_line + *cur_line_offset, 0, |
1514 | file_offset, false, false, false, false, true); |
1515 | inline_site_sp->line_entries.push_back(x: line_entry); |
1516 | } else { |
1517 | LineTable::Entry line_entry(func_base + *code_offset_base, |
1518 | decl_line + *cur_line_offset, 0, |
1519 | file_offset, is_start_of_statement, false, |
1520 | is_prologue_end, false, false); |
1521 | inline_site_sp->line_entries.push_back(x: line_entry); |
1522 | is_prologue_end = false; |
1523 | is_start_of_statement = false; |
1524 | } |
1525 | } |
1526 | if (is_terminal_entry) |
1527 | is_start_of_statement = true; |
1528 | is_terminal_entry = false; |
1529 | } |
1530 | |
1531 | inline_site_sp->ranges.Sort(); |
1532 | |
1533 | // Get the inlined function callsite info. |
1534 | std::unique_ptr<Declaration> callsite_up; |
1535 | if (!inline_site_sp->ranges.IsEmpty()) { |
1536 | auto *entry = inline_site_sp->ranges.GetEntryAtIndex(i: 0); |
1537 | addr_t base_offset = entry->GetRangeBase(); |
1538 | if (cii->m_debug_stream.readSymbolAtOffset(Offset: parent_id.offset).kind() == |
1539 | S_INLINESITE) { |
1540 | // Its parent is another inline site, lookup parent site's range vector |
1541 | // for callsite line. |
1542 | ParseInlineSite(id: parent_id, func_addr: func_base); |
1543 | std::shared_ptr<InlineSite> parent_site = |
1544 | m_inline_sites[toOpaqueUid(cid: parent_id)]; |
1545 | FileSpec &parent_decl_file = |
1546 | parent_site->inline_function_info->GetDeclaration().GetFile(); |
1547 | if (auto *parent_entry = |
1548 | parent_site->ranges.FindEntryThatContains(addr: base_offset)) { |
1549 | callsite_up = |
1550 | std::make_unique<Declaration>(args&: parent_decl_file, args&: parent_entry->data); |
1551 | } |
1552 | } else { |
1553 | // Its parent is a function, lookup global line table for callsite. |
1554 | if (auto *entry = cii->m_global_line_table.FindEntryThatContains( |
1555 | addr: func_base + base_offset)) { |
1556 | const FileSpec &callsite_file = |
1557 | files.GetFileSpecAtIndex(idx: entry->data.first); |
1558 | callsite_up = |
1559 | std::make_unique<Declaration>(args: callsite_file, args&: entry->data.second); |
1560 | } |
1561 | } |
1562 | } |
1563 | |
1564 | // Get the inlined function name. |
1565 | CVType inlinee_cvt = m_index->ipi().getType(Index: inline_site.Inlinee); |
1566 | std::string inlinee_name; |
1567 | if (inlinee_cvt.kind() == LF_MFUNC_ID) { |
1568 | MemberFuncIdRecord mfr; |
1569 | cantFail( |
1570 | Err: TypeDeserializer::deserializeAs<MemberFuncIdRecord>(CVT&: inlinee_cvt, Record&: mfr)); |
1571 | LazyRandomTypeCollection &types = m_index->tpi().typeCollection(); |
1572 | inlinee_name.append(str: std::string(types.getTypeName(Index: mfr.ClassType))); |
1573 | inlinee_name.append(s: "::" ); |
1574 | inlinee_name.append(str: mfr.getName().str()); |
1575 | } else if (inlinee_cvt.kind() == LF_FUNC_ID) { |
1576 | FuncIdRecord fir; |
1577 | cantFail(Err: TypeDeserializer::deserializeAs<FuncIdRecord>(CVT&: inlinee_cvt, Record&: fir)); |
1578 | TypeIndex parent_idx = fir.getParentScope(); |
1579 | if (!parent_idx.isNoneType()) { |
1580 | LazyRandomTypeCollection &ids = m_index->ipi().typeCollection(); |
1581 | inlinee_name.append(str: std::string(ids.getTypeName(Index: parent_idx))); |
1582 | inlinee_name.append(s: "::" ); |
1583 | } |
1584 | inlinee_name.append(str: fir.getName().str()); |
1585 | } |
1586 | inline_site_sp->inline_function_info = std::make_shared<InlineFunctionInfo>( |
1587 | args: inlinee_name.c_str(), args: llvm::StringRef(), args: decl_up.get(), |
1588 | args: callsite_up.get()); |
1589 | |
1590 | m_inline_sites[opaque_uid] = inline_site_sp; |
1591 | } |
1592 | |
1593 | size_t SymbolFileNativePDB::ParseBlocksRecursive(Function &func) { |
1594 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1595 | PdbCompilandSymId func_id = PdbSymUid(func.GetID()).asCompilandSym(); |
1596 | // After we iterate through inline sites inside the function, we already get |
1597 | // all the info needed, removing from the map to save memory. |
1598 | std::set<uint64_t> remove_uids; |
1599 | auto parse_blocks = [&](SymbolKind kind, PdbCompilandSymId id) { |
1600 | if (kind == S_GPROC32 || kind == S_LPROC32 || kind == S_BLOCK32 || |
1601 | kind == S_INLINESITE) { |
1602 | GetOrCreateBlock(block_id: id); |
1603 | if (kind == S_INLINESITE) |
1604 | remove_uids.insert(x: toOpaqueUid(cid: id)); |
1605 | return true; |
1606 | } |
1607 | return false; |
1608 | }; |
1609 | size_t count = ParseSymbolArrayInScope(parent: func_id, fn: parse_blocks); |
1610 | for (uint64_t uid : remove_uids) { |
1611 | m_inline_sites.erase(Val: uid); |
1612 | } |
1613 | return count; |
1614 | } |
1615 | |
1616 | size_t SymbolFileNativePDB::ParseSymbolArrayInScope( |
1617 | PdbCompilandSymId parent_id, |
1618 | llvm::function_ref<bool(SymbolKind, PdbCompilandSymId)> fn) { |
1619 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi: parent_id.modi); |
1620 | CVSymbolArray syms = |
1621 | cii->m_debug_stream.getSymbolArrayForScope(ScopeBegin: parent_id.offset); |
1622 | |
1623 | size_t count = 1; |
1624 | for (auto iter = syms.begin(); iter != syms.end(); ++iter) { |
1625 | PdbCompilandSymId child_id(parent_id.modi, iter.offset()); |
1626 | if (fn(iter->kind(), child_id)) |
1627 | ++count; |
1628 | } |
1629 | |
1630 | return count; |
1631 | } |
1632 | |
1633 | void SymbolFileNativePDB::DumpClangAST(Stream &s, llvm::StringRef filter) { |
1634 | auto ts_or_err = GetTypeSystemForLanguage(language: eLanguageTypeC_plus_plus); |
1635 | if (!ts_or_err) |
1636 | return; |
1637 | auto ts = *ts_or_err; |
1638 | TypeSystemClang *clang = llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get()); |
1639 | if (!clang) |
1640 | return; |
1641 | clang->GetNativePDBParser()->Dump(stream&: s, filter); |
1642 | } |
1643 | |
1644 | void SymbolFileNativePDB::FindGlobalVariables( |
1645 | ConstString name, const CompilerDeclContext &parent_decl_ctx, |
1646 | uint32_t max_matches, VariableList &variables) { |
1647 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1648 | using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; |
1649 | |
1650 | std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName( |
1651 | Name: name.GetStringRef(), Symbols: m_index->symrecords()); |
1652 | for (const SymbolAndOffset &result : results) { |
1653 | switch (result.second.kind()) { |
1654 | case SymbolKind::S_GDATA32: |
1655 | case SymbolKind::S_LDATA32: |
1656 | case SymbolKind::S_GTHREAD32: |
1657 | case SymbolKind::S_LTHREAD32: |
1658 | case SymbolKind::S_CONSTANT: { |
1659 | PdbGlobalSymId global(result.first, false); |
1660 | if (VariableSP var = GetOrCreateGlobalVariable(var_id: global)) |
1661 | variables.AddVariable(var_sp: var); |
1662 | break; |
1663 | } |
1664 | default: |
1665 | continue; |
1666 | } |
1667 | } |
1668 | } |
1669 | |
1670 | void SymbolFileNativePDB::FindFunctions( |
1671 | const Module::LookupInfo &lookup_info, |
1672 | const CompilerDeclContext &parent_decl_ctx, bool include_inlines, |
1673 | SymbolContextList &sc_list) { |
1674 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1675 | ConstString name = lookup_info.GetLookupName(); |
1676 | FunctionNameType name_type_mask = lookup_info.GetNameTypeMask(); |
1677 | if (name_type_mask & eFunctionNameTypeFull) |
1678 | name = lookup_info.GetName(); |
1679 | |
1680 | // For now we only support lookup by method name or full name. |
1681 | if (!(name_type_mask & eFunctionNameTypeFull || |
1682 | name_type_mask & eFunctionNameTypeMethod)) |
1683 | return; |
1684 | |
1685 | using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; |
1686 | |
1687 | std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName( |
1688 | Name: name.GetStringRef(), Symbols: m_index->symrecords()); |
1689 | for (const SymbolAndOffset &match : matches) { |
1690 | if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF) |
1691 | continue; |
1692 | ProcRefSym proc(match.second.kind()); |
1693 | cantFail(Err: SymbolDeserializer::deserializeAs<ProcRefSym>(Symbol: match.second, Record&: proc)); |
1694 | |
1695 | if (!IsValidRecord(sym: proc)) |
1696 | continue; |
1697 | |
1698 | CompilandIndexItem &cci = |
1699 | m_index->compilands().GetOrCreateCompiland(modi: proc.modi()); |
1700 | SymbolContext sc; |
1701 | |
1702 | sc.comp_unit = GetOrCreateCompileUnit(cci).get(); |
1703 | PdbCompilandSymId func_id(proc.modi(), proc.SymOffset); |
1704 | sc.function = GetOrCreateFunction(func_id, comp_unit&: *sc.comp_unit).get(); |
1705 | |
1706 | sc_list.Append(sc); |
1707 | } |
1708 | } |
1709 | |
1710 | void SymbolFileNativePDB::FindFunctions(const RegularExpression ®ex, |
1711 | bool include_inlines, |
1712 | SymbolContextList &sc_list) {} |
1713 | |
1714 | void SymbolFileNativePDB::FindTypes(const lldb_private::TypeQuery &query, |
1715 | lldb_private::TypeResults &results) { |
1716 | |
1717 | // Make sure we haven't already searched this SymbolFile before. |
1718 | if (results.AlreadySearched(sym_file: this)) |
1719 | return; |
1720 | |
1721 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1722 | |
1723 | std::vector<TypeIndex> matches = |
1724 | m_index->tpi().findRecordsByName(Name: query.GetTypeBasename().GetStringRef()); |
1725 | |
1726 | for (TypeIndex type_idx : matches) { |
1727 | TypeSP type_sp = GetOrCreateType(ti: type_idx); |
1728 | if (!type_sp) |
1729 | continue; |
1730 | |
1731 | // We resolved a type. Get the fully qualified name to ensure it matches. |
1732 | ConstString name = type_sp->GetQualifiedName(); |
1733 | TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match); |
1734 | if (query.ContextMatches(context: type_match.GetContextRef())) { |
1735 | results.InsertUnique(type_sp); |
1736 | if (results.Done(query)) |
1737 | return; |
1738 | } |
1739 | } |
1740 | } |
1741 | |
1742 | void SymbolFileNativePDB::FindTypesByName(llvm::StringRef name, |
1743 | uint32_t max_matches, |
1744 | TypeMap &types) { |
1745 | |
1746 | std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(Name: name); |
1747 | if (max_matches > 0 && max_matches < matches.size()) |
1748 | matches.resize(new_size: max_matches); |
1749 | |
1750 | for (TypeIndex ti : matches) { |
1751 | TypeSP type = GetOrCreateType(ti); |
1752 | if (!type) |
1753 | continue; |
1754 | |
1755 | types.Insert(type); |
1756 | } |
1757 | } |
1758 | |
1759 | size_t SymbolFileNativePDB::ParseTypes(CompileUnit &comp_unit) { |
1760 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
1761 | // Only do the full type scan the first time. |
1762 | if (m_done_full_type_scan) |
1763 | return 0; |
1764 | |
1765 | const size_t old_count = GetTypeList().GetSize(); |
1766 | LazyRandomTypeCollection &types = m_index->tpi().typeCollection(); |
1767 | |
1768 | // First process the entire TPI stream. |
1769 | for (auto ti = types.getFirst(); ti; ti = types.getNext(Prev: *ti)) { |
1770 | TypeSP type = GetOrCreateType(ti: *ti); |
1771 | if (type) |
1772 | (void)type->GetFullCompilerType(); |
1773 | } |
1774 | |
1775 | // Next look for S_UDT records in the globals stream. |
1776 | for (const uint32_t gid : m_index->globals().getGlobalsTable()) { |
1777 | PdbGlobalSymId global{gid, false}; |
1778 | CVSymbol sym = m_index->ReadSymbolRecord(global); |
1779 | if (sym.kind() != S_UDT) |
1780 | continue; |
1781 | |
1782 | UDTSym udt = llvm::cantFail(ValOrErr: SymbolDeserializer::deserializeAs<UDTSym>(Symbol: sym)); |
1783 | bool is_typedef = true; |
1784 | if (IsTagRecord(id: PdbTypeSymId{udt.Type, false}, tpi&: m_index->tpi())) { |
1785 | CVType cvt = m_index->tpi().getType(Index: udt.Type); |
1786 | llvm::StringRef name = CVTagRecord::create(type: cvt).name(); |
1787 | if (name == udt.Name) |
1788 | is_typedef = false; |
1789 | } |
1790 | |
1791 | if (is_typedef) |
1792 | GetOrCreateTypedef(id: global); |
1793 | } |
1794 | |
1795 | const size_t new_count = GetTypeList().GetSize(); |
1796 | |
1797 | m_done_full_type_scan = true; |
1798 | |
1799 | return new_count - old_count; |
1800 | } |
1801 | |
1802 | size_t |
1803 | SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit, |
1804 | VariableList &variables) { |
1805 | PdbSymUid sym_uid(comp_unit.GetID()); |
1806 | lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland); |
1807 | for (const uint32_t gid : m_index->globals().getGlobalsTable()) { |
1808 | PdbGlobalSymId global{gid, false}; |
1809 | CVSymbol sym = m_index->ReadSymbolRecord(global); |
1810 | // TODO: S_CONSTANT is not handled here to prevent a possible crash in |
1811 | // lldb_private::npdb::MakeConstantLocationExpression when it's a record |
1812 | // type (e.g. std::strong_ordering::equal). That function needs to be |
1813 | // updated to handle this case when we add S_CONSTANT case here. |
1814 | switch (sym.kind()) { |
1815 | case SymbolKind::S_GDATA32: |
1816 | case SymbolKind::S_LDATA32: |
1817 | case SymbolKind::S_GTHREAD32: |
1818 | case SymbolKind::S_LTHREAD32: { |
1819 | if (VariableSP var = GetOrCreateGlobalVariable(var_id: global)) |
1820 | variables.AddVariable(var_sp: var); |
1821 | break; |
1822 | } |
1823 | default: |
1824 | break; |
1825 | } |
1826 | } |
1827 | return variables.GetSize(); |
1828 | } |
1829 | |
1830 | VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id, |
1831 | PdbCompilandSymId var_id, |
1832 | bool is_param) { |
1833 | ModuleSP module = GetObjectFile()->GetModule(); |
1834 | Block *block = GetOrCreateBlock(block_id: scope_id); |
1835 | if (!block) |
1836 | return nullptr; |
1837 | |
1838 | // Get function block. |
1839 | Block *func_block = block; |
1840 | while (func_block->GetParent()) { |
1841 | func_block = func_block->GetParent(); |
1842 | } |
1843 | |
1844 | Address addr; |
1845 | func_block->GetStartAddress(addr); |
1846 | VariableInfo var_info = |
1847 | GetVariableLocationInfo(index&: *m_index, var_id, func_block&: *func_block, module); |
1848 | Function *func = func_block->CalculateSymbolContextFunction(); |
1849 | if (!func) |
1850 | return nullptr; |
1851 | // Use empty dwarf expr if optimized away so that it won't be filtered out |
1852 | // when lookuping local variables in this scope. |
1853 | if (!var_info.location.IsValid()) |
1854 | var_info.location = DWARFExpressionList(module, DWARFExpression(), nullptr); |
1855 | var_info.location.SetFuncFileAddress(func->GetAddress().GetFileAddress()); |
1856 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi: var_id.modi); |
1857 | CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(cci: *cii); |
1858 | TypeSP type_sp = GetOrCreateType(ti: var_info.type); |
1859 | if (!type_sp) |
1860 | return nullptr; |
1861 | std::string name = var_info.name.str(); |
1862 | Declaration decl; |
1863 | SymbolFileTypeSP sftype = |
1864 | std::make_shared<SymbolFileType>(args&: *this, args: type_sp->GetID()); |
1865 | |
1866 | is_param |= var_info.is_param; |
1867 | ValueType var_scope = |
1868 | is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal; |
1869 | bool external = false; |
1870 | bool artificial = false; |
1871 | bool location_is_constant_data = false; |
1872 | bool static_member = false; |
1873 | Variable::RangeList scope_ranges; |
1874 | VariableSP var_sp = std::make_shared<Variable>( |
1875 | args: toOpaqueUid(cid: var_id), args: name.c_str(), args: name.c_str(), args&: sftype, args&: var_scope, args&: block, |
1876 | args&: scope_ranges, args: &decl, args&: var_info.location, args&: external, args&: artificial, |
1877 | args&: location_is_constant_data, args&: static_member); |
1878 | if (!is_param) { |
1879 | auto ts_or_err = GetTypeSystemForLanguage(language: comp_unit_sp->GetLanguage()); |
1880 | if (auto err = ts_or_err.takeError()) |
1881 | return nullptr; |
1882 | auto ts = *ts_or_err; |
1883 | if (!ts) |
1884 | return nullptr; |
1885 | |
1886 | ts->GetNativePDBParser()->GetOrCreateVariableDecl(scope_id, var_id); |
1887 | } |
1888 | m_local_variables[toOpaqueUid(cid: var_id)] = var_sp; |
1889 | return var_sp; |
1890 | } |
1891 | |
1892 | VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable( |
1893 | PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) { |
1894 | auto iter = m_local_variables.find(Val: toOpaqueUid(cid: var_id)); |
1895 | if (iter != m_local_variables.end()) |
1896 | return iter->second; |
1897 | |
1898 | return CreateLocalVariable(scope_id, var_id, is_param); |
1899 | } |
1900 | |
1901 | TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) { |
1902 | CVSymbol sym = m_index->ReadSymbolRecord(global: id); |
1903 | lldbassert(sym.kind() == SymbolKind::S_UDT); |
1904 | |
1905 | UDTSym udt = llvm::cantFail(ValOrErr: SymbolDeserializer::deserializeAs<UDTSym>(Symbol: sym)); |
1906 | |
1907 | TypeSP target_type = GetOrCreateType(ti: udt.Type); |
1908 | |
1909 | auto ts_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus); |
1910 | if (auto err = ts_or_err.takeError()) |
1911 | return nullptr; |
1912 | auto ts = *ts_or_err; |
1913 | if (!ts) |
1914 | return nullptr; |
1915 | |
1916 | ts->GetNativePDBParser()->GetOrCreateTypedefDecl(id); |
1917 | |
1918 | Declaration decl; |
1919 | return MakeType(uid: toOpaqueUid(cid: id), name: ConstString(udt.Name), |
1920 | byte_size: llvm::expectedToOptional(E: target_type->GetByteSize(exe_scope: nullptr)), |
1921 | context: nullptr, encoding_uid: target_type->GetID(), |
1922 | encoding_uid_type: lldb_private::Type::eEncodingIsTypedefUID, decl, |
1923 | compiler_qual_type: target_type->GetForwardCompilerType(), |
1924 | compiler_type_resolve_state: lldb_private::Type::ResolveState::Forward); |
1925 | } |
1926 | |
1927 | TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) { |
1928 | auto iter = m_types.find(Val: toOpaqueUid(cid: id)); |
1929 | if (iter != m_types.end()) |
1930 | return iter->second; |
1931 | |
1932 | return CreateTypedef(id); |
1933 | } |
1934 | |
1935 | size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) { |
1936 | Block *block = GetOrCreateBlock(block_id); |
1937 | if (!block) |
1938 | return 0; |
1939 | |
1940 | size_t count = 0; |
1941 | |
1942 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(modi: block_id.modi); |
1943 | CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(Offset: block_id.offset); |
1944 | uint32_t params_remaining = 0; |
1945 | switch (sym.kind()) { |
1946 | case S_GPROC32: |
1947 | case S_LPROC32: { |
1948 | ProcSym proc(static_cast<SymbolRecordKind>(sym.kind())); |
1949 | cantFail(Err: SymbolDeserializer::deserializeAs<ProcSym>(Symbol: sym, Record&: proc)); |
1950 | CVType signature = m_index->tpi().getType(Index: proc.FunctionType); |
1951 | if (signature.kind() == LF_PROCEDURE) { |
1952 | ProcedureRecord sig; |
1953 | if (llvm::Error e = TypeDeserializer::deserializeAs<ProcedureRecord>( |
1954 | CVT&: signature, Record&: sig)) { |
1955 | llvm::consumeError(Err: std::move(e)); |
1956 | return 0; |
1957 | } |
1958 | params_remaining = sig.getParameterCount(); |
1959 | } else if (signature.kind() == LF_MFUNCTION) { |
1960 | MemberFunctionRecord sig; |
1961 | if (llvm::Error e = TypeDeserializer::deserializeAs<MemberFunctionRecord>( |
1962 | CVT&: signature, Record&: sig)) { |
1963 | llvm::consumeError(Err: std::move(e)); |
1964 | return 0; |
1965 | } |
1966 | params_remaining = sig.getParameterCount(); |
1967 | } else |
1968 | return 0; |
1969 | break; |
1970 | } |
1971 | case S_BLOCK32: |
1972 | break; |
1973 | case S_INLINESITE: |
1974 | break; |
1975 | default: |
1976 | lldbassert(false && "Symbol is not a block!" ); |
1977 | return 0; |
1978 | } |
1979 | |
1980 | VariableListSP variables = block->GetBlockVariableList(can_create: false); |
1981 | if (!variables) { |
1982 | variables = std::make_shared<VariableList>(); |
1983 | block->SetVariableList(variables); |
1984 | } |
1985 | |
1986 | CVSymbolArray syms = limitSymbolArrayToScope( |
1987 | Symbols: cii->m_debug_stream.getSymbolArray(), ScopeBegin: block_id.offset); |
1988 | |
1989 | // Skip the first record since it's a PROC32 or BLOCK32, and there's |
1990 | // no point examining it since we know it's not a local variable. |
1991 | syms.drop_front(); |
1992 | auto iter = syms.begin(); |
1993 | auto end = syms.end(); |
1994 | |
1995 | while (iter != end) { |
1996 | uint32_t record_offset = iter.offset(); |
1997 | CVSymbol variable_cvs = *iter; |
1998 | PdbCompilandSymId child_sym_id(block_id.modi, record_offset); |
1999 | ++iter; |
2000 | |
2001 | // If this is a block or inline site, recurse into its children and then |
2002 | // skip it. |
2003 | if (variable_cvs.kind() == S_BLOCK32 || |
2004 | variable_cvs.kind() == S_INLINESITE) { |
2005 | uint32_t block_end = getScopeEndOffset(Symbol: variable_cvs); |
2006 | count += ParseVariablesForBlock(block_id: child_sym_id); |
2007 | iter = syms.at(Offset: block_end); |
2008 | continue; |
2009 | } |
2010 | |
2011 | bool is_param = params_remaining > 0; |
2012 | VariableSP variable; |
2013 | switch (variable_cvs.kind()) { |
2014 | case S_REGREL32: |
2015 | case S_REGISTER: |
2016 | case S_LOCAL: |
2017 | variable = GetOrCreateLocalVariable(scope_id: block_id, var_id: child_sym_id, is_param); |
2018 | if (is_param) |
2019 | --params_remaining; |
2020 | if (variable) |
2021 | variables->AddVariableIfUnique(var_sp: variable); |
2022 | break; |
2023 | default: |
2024 | break; |
2025 | } |
2026 | } |
2027 | |
2028 | // Pass false for set_children, since we call this recursively so that the |
2029 | // children will call this for themselves. |
2030 | block->SetDidParseVariables(b: true, set_children: false); |
2031 | |
2032 | return count; |
2033 | } |
2034 | |
2035 | size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) { |
2036 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
2037 | lldbassert(sc.function || sc.comp_unit); |
2038 | |
2039 | VariableListSP variables; |
2040 | if (sc.block) { |
2041 | PdbSymUid block_id(sc.block->GetID()); |
2042 | |
2043 | size_t count = ParseVariablesForBlock(block_id: block_id.asCompilandSym()); |
2044 | return count; |
2045 | } |
2046 | |
2047 | if (sc.function) { |
2048 | PdbSymUid block_id(sc.function->GetID()); |
2049 | |
2050 | size_t count = ParseVariablesForBlock(block_id: block_id.asCompilandSym()); |
2051 | return count; |
2052 | } |
2053 | |
2054 | if (sc.comp_unit) { |
2055 | variables = sc.comp_unit->GetVariableList(can_create: false); |
2056 | if (!variables) { |
2057 | variables = std::make_shared<VariableList>(); |
2058 | sc.comp_unit->SetVariableList(variables); |
2059 | } |
2060 | return ParseVariablesForCompileUnit(comp_unit&: *sc.comp_unit, variables&: *variables); |
2061 | } |
2062 | |
2063 | llvm_unreachable("Unreachable!" ); |
2064 | } |
2065 | |
2066 | CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) { |
2067 | auto ts_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus); |
2068 | if (auto err = ts_or_err.takeError()) |
2069 | return CompilerDecl(); |
2070 | auto ts = *ts_or_err; |
2071 | if (!ts) |
2072 | return {}; |
2073 | |
2074 | if (auto decl = ts->GetNativePDBParser()->GetOrCreateDeclForUid(uid)) |
2075 | return *decl; |
2076 | return CompilerDecl(); |
2077 | } |
2078 | |
2079 | CompilerDeclContext |
2080 | SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) { |
2081 | auto ts_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus); |
2082 | if (auto err = ts_or_err.takeError()) |
2083 | return {}; |
2084 | auto ts = *ts_or_err; |
2085 | if (!ts) |
2086 | return {}; |
2087 | |
2088 | PdbAstBuilder *ast_builder = ts->GetNativePDBParser(); |
2089 | clang::DeclContext *context = |
2090 | ast_builder->GetOrCreateDeclContextForUid(uid: PdbSymUid(uid)); |
2091 | if (!context) |
2092 | return {}; |
2093 | |
2094 | return ast_builder->ToCompilerDeclContext(context&: *context); |
2095 | } |
2096 | |
2097 | CompilerDeclContext |
2098 | SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { |
2099 | auto ts_or_err = GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus); |
2100 | if (auto err = ts_or_err.takeError()) |
2101 | return CompilerDeclContext(); |
2102 | auto ts = *ts_or_err; |
2103 | if (!ts) |
2104 | return {}; |
2105 | |
2106 | PdbAstBuilder *ast_builder = ts->GetNativePDBParser(); |
2107 | clang::DeclContext *context = ast_builder->GetParentDeclContext(uid: PdbSymUid(uid)); |
2108 | if (!context) |
2109 | return CompilerDeclContext(); |
2110 | return ast_builder->ToCompilerDeclContext(context&: *context); |
2111 | } |
2112 | |
2113 | Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) { |
2114 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
2115 | auto iter = m_types.find(Val: type_uid); |
2116 | // lldb should not be passing us non-sensical type uids. the only way it |
2117 | // could have a type uid in the first place is if we handed it out, in which |
2118 | // case we should know about the type. However, that doesn't mean we've |
2119 | // instantiated it yet. We can vend out a UID for a future type. So if the |
2120 | // type doesn't exist, let's instantiate it now. |
2121 | if (iter != m_types.end()) |
2122 | return &*iter->second; |
2123 | |
2124 | PdbSymUid uid(type_uid); |
2125 | lldbassert(uid.kind() == PdbSymUidKind::Type); |
2126 | PdbTypeSymId type_id = uid.asTypeSym(); |
2127 | if (type_id.index.isNoneType()) |
2128 | return nullptr; |
2129 | |
2130 | TypeSP type_sp = CreateAndCacheType(type_id); |
2131 | if (!type_sp) |
2132 | return nullptr; |
2133 | return &*type_sp; |
2134 | } |
2135 | |
2136 | std::optional<SymbolFile::ArrayInfo> |
2137 | SymbolFileNativePDB::GetDynamicArrayInfoForUID( |
2138 | lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { |
2139 | return std::nullopt; |
2140 | } |
2141 | |
2142 | bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) { |
2143 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
2144 | auto clang_type_system = compiler_type.GetTypeSystem<TypeSystemClang>(); |
2145 | if (!clang_type_system) |
2146 | return false; |
2147 | |
2148 | PdbAstBuilder *ast_builder = |
2149 | static_cast<PdbAstBuilder *>(clang_type_system->GetNativePDBParser()); |
2150 | if (ast_builder && |
2151 | ast_builder->GetClangASTImporter().CanImport(type: compiler_type)) |
2152 | return ast_builder->GetClangASTImporter().CompleteType(compiler_type); |
2153 | clang::QualType qt = |
2154 | clang::QualType::getFromOpaquePtr(Ptr: compiler_type.GetOpaqueQualType()); |
2155 | |
2156 | return ast_builder->CompleteType(qt); |
2157 | } |
2158 | |
2159 | void SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, |
2160 | TypeClass type_mask, |
2161 | lldb_private::TypeList &type_list) {} |
2162 | |
2163 | CompilerDeclContext SymbolFileNativePDB::FindNamespace( |
2164 | ConstString name, const CompilerDeclContext &parent_decl_ctx, bool) { |
2165 | return {}; |
2166 | } |
2167 | |
2168 | llvm::Expected<lldb::TypeSystemSP> |
2169 | SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { |
2170 | auto type_system_or_err = |
2171 | m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language); |
2172 | if (type_system_or_err) |
2173 | if (auto ts = *type_system_or_err) |
2174 | ts->SetSymbolFile(this); |
2175 | return type_system_or_err; |
2176 | } |
2177 | |
2178 | uint64_t SymbolFileNativePDB::GetDebugInfoSize(bool load_all_debug_info) { |
2179 | // PDB files are a separate file that contains all debug info. |
2180 | return m_index->pdb().getFileSize(); |
2181 | } |
2182 | |
2183 | void SymbolFileNativePDB::BuildParentMap() { |
2184 | LazyRandomTypeCollection &types = m_index->tpi().typeCollection(); |
2185 | |
2186 | llvm::DenseMap<TypeIndex, TypeIndex> forward_to_full; |
2187 | llvm::DenseMap<TypeIndex, TypeIndex> full_to_forward; |
2188 | |
2189 | struct RecordIndices { |
2190 | TypeIndex forward; |
2191 | TypeIndex full; |
2192 | }; |
2193 | |
2194 | llvm::StringMap<RecordIndices> record_indices; |
2195 | |
2196 | for (auto ti = types.getFirst(); ti; ti = types.getNext(Prev: *ti)) { |
2197 | CVType type = types.getType(Index: *ti); |
2198 | if (!IsTagRecord(cvt: type)) |
2199 | continue; |
2200 | |
2201 | CVTagRecord tag = CVTagRecord::create(type); |
2202 | |
2203 | RecordIndices &indices = record_indices[tag.asTag().getUniqueName()]; |
2204 | if (tag.asTag().isForwardRef()) |
2205 | indices.forward = *ti; |
2206 | else |
2207 | indices.full = *ti; |
2208 | |
2209 | if (indices.full != TypeIndex::None() && |
2210 | indices.forward != TypeIndex::None()) { |
2211 | forward_to_full[indices.forward] = indices.full; |
2212 | full_to_forward[indices.full] = indices.forward; |
2213 | } |
2214 | |
2215 | // We're looking for LF_NESTTYPE records in the field list, so ignore |
2216 | // forward references (no field list), and anything without a nested class |
2217 | // (since there won't be any LF_NESTTYPE records). |
2218 | if (tag.asTag().isForwardRef() || !tag.asTag().containsNestedClass()) |
2219 | continue; |
2220 | |
2221 | struct ProcessTpiStream : public TypeVisitorCallbacks { |
2222 | ProcessTpiStream(PdbIndex &index, TypeIndex parent, |
2223 | const CVTagRecord &parent_cvt, |
2224 | llvm::DenseMap<TypeIndex, TypeIndex> &parents) |
2225 | : index(index), parents(parents), parent(parent), |
2226 | parent_cvt(parent_cvt) {} |
2227 | |
2228 | PdbIndex &index; |
2229 | llvm::DenseMap<TypeIndex, TypeIndex> &parents; |
2230 | |
2231 | unsigned unnamed_type_index = 1; |
2232 | TypeIndex parent; |
2233 | const CVTagRecord &parent_cvt; |
2234 | |
2235 | llvm::Error visitKnownMember(CVMemberRecord &CVR, |
2236 | NestedTypeRecord &Record) override { |
2237 | std::string unnamed_type_name; |
2238 | if (Record.Name.empty()) { |
2239 | unnamed_type_name = |
2240 | llvm::formatv(Fmt: "<unnamed-type-$S{0}>" , Vals&: unnamed_type_index).str(); |
2241 | Record.Name = unnamed_type_name; |
2242 | ++unnamed_type_index; |
2243 | } |
2244 | std::optional<CVTagRecord> tag = |
2245 | GetNestedTagDefinition(Record, parent: parent_cvt, tpi&: index.tpi()); |
2246 | if (!tag) |
2247 | return llvm::ErrorSuccess(); |
2248 | |
2249 | parents[Record.Type] = parent; |
2250 | return llvm::ErrorSuccess(); |
2251 | } |
2252 | }; |
2253 | |
2254 | CVType field_list_cvt = m_index->tpi().getType(Index: tag.asTag().FieldList); |
2255 | ProcessTpiStream process(*m_index, *ti, tag, m_parent_types); |
2256 | FieldListRecord field_list; |
2257 | if (llvm::Error error = TypeDeserializer::deserializeAs<FieldListRecord>( |
2258 | CVT&: field_list_cvt, Record&: field_list)) |
2259 | llvm::consumeError(Err: std::move(error)); |
2260 | if (llvm::Error error = visitMemberRecordStream(FieldList: field_list.Data, Callbacks&: process)) |
2261 | llvm::consumeError(Err: std::move(error)); |
2262 | } |
2263 | |
2264 | // Now that we know the forward -> full mapping of all type indices, we can |
2265 | // re-write all the indices. At the end of this process, we want a mapping |
2266 | // consisting of fwd -> full and full -> full for all child -> parent indices. |
2267 | // We can re-write the values in place, but for the keys, we must save them |
2268 | // off so that we don't modify the map in place while also iterating it. |
2269 | std::vector<TypeIndex> full_keys; |
2270 | std::vector<TypeIndex> fwd_keys; |
2271 | for (auto &entry : m_parent_types) { |
2272 | TypeIndex key = entry.first; |
2273 | TypeIndex value = entry.second; |
2274 | |
2275 | auto iter = forward_to_full.find(Val: value); |
2276 | if (iter != forward_to_full.end()) |
2277 | entry.second = iter->second; |
2278 | |
2279 | iter = forward_to_full.find(Val: key); |
2280 | if (iter != forward_to_full.end()) |
2281 | fwd_keys.push_back(x: key); |
2282 | else |
2283 | full_keys.push_back(x: key); |
2284 | } |
2285 | for (TypeIndex fwd : fwd_keys) { |
2286 | TypeIndex full = forward_to_full[fwd]; |
2287 | TypeIndex parent_idx = m_parent_types[fwd]; |
2288 | m_parent_types[full] = parent_idx; |
2289 | } |
2290 | for (TypeIndex full : full_keys) { |
2291 | TypeIndex fwd = full_to_forward[full]; |
2292 | m_parent_types[fwd] = m_parent_types[full]; |
2293 | } |
2294 | } |
2295 | |
2296 | std::optional<PdbCompilandSymId> |
2297 | SymbolFileNativePDB::FindSymbolScope(PdbCompilandSymId id) { |
2298 | CVSymbol sym = m_index->ReadSymbolRecord(cu_sym: id); |
2299 | if (symbolOpensScope(Kind: sym.kind())) { |
2300 | // If this exact symbol opens a scope, we can just directly access its |
2301 | // parent. |
2302 | id.offset = getScopeParentOffset(Symbol: sym); |
2303 | // Global symbols have parent offset of 0. Return std::nullopt to indicate |
2304 | // this. |
2305 | if (id.offset == 0) |
2306 | return std::nullopt; |
2307 | return id; |
2308 | } |
2309 | |
2310 | // Otherwise we need to start at the beginning and iterate forward until we |
2311 | // reach (or pass) this particular symbol |
2312 | CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi: id.modi); |
2313 | const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray(); |
2314 | |
2315 | auto begin = syms.begin(); |
2316 | auto end = syms.at(Offset: id.offset); |
2317 | std::vector<PdbCompilandSymId> scope_stack; |
2318 | |
2319 | while (begin != end) { |
2320 | if (begin.offset() > id.offset) { |
2321 | // We passed it. We couldn't even find this symbol record. |
2322 | lldbassert(false && "Invalid compiland symbol id!" ); |
2323 | return std::nullopt; |
2324 | } |
2325 | |
2326 | // We haven't found the symbol yet. Check if we need to open or close the |
2327 | // scope stack. |
2328 | if (symbolOpensScope(Kind: begin->kind())) { |
2329 | // We can use the end offset of the scope to determine whether or not |
2330 | // we can just outright skip this entire scope. |
2331 | uint32_t scope_end = getScopeEndOffset(Symbol: *begin); |
2332 | if (scope_end < id.offset) { |
2333 | begin = syms.at(Offset: scope_end); |
2334 | } else { |
2335 | // The symbol we're looking for is somewhere in this scope. |
2336 | scope_stack.emplace_back(args&: id.modi, args: begin.offset()); |
2337 | } |
2338 | } else if (symbolEndsScope(Kind: begin->kind())) { |
2339 | scope_stack.pop_back(); |
2340 | } |
2341 | ++begin; |
2342 | } |
2343 | if (scope_stack.empty()) |
2344 | return std::nullopt; |
2345 | // We have a match! Return the top of the stack |
2346 | return scope_stack.back(); |
2347 | } |
2348 | |
2349 | std::optional<llvm::codeview::TypeIndex> |
2350 | SymbolFileNativePDB::GetParentType(llvm::codeview::TypeIndex ti) { |
2351 | auto parent_iter = m_parent_types.find(Val: ti); |
2352 | if (parent_iter == m_parent_types.end()) |
2353 | return std::nullopt; |
2354 | return parent_iter->second; |
2355 | } |
2356 | |