1//===-- SymbolFilePDB.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 "SymbolFilePDB.h"
10
11#include "PDBASTParser.h"
12#include "PDBLocationToDWARFExpression.h"
13
14#include "clang/Lex/Lexer.h"
15
16#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
17#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
19#include "lldb/Symbol/CompileUnit.h"
20#include "lldb/Symbol/LineTable.h"
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Symbol/SymbolVendor.h"
24#include "lldb/Symbol/TypeList.h"
25#include "lldb/Symbol/TypeMap.h"
26#include "lldb/Symbol/Variable.h"
27#include "lldb/Utility/LLDBLog.h"
28#include "lldb/Utility/Log.h"
29#include "lldb/Utility/RegularExpression.h"
30
31#include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
32#include "llvm/DebugInfo/PDB/GenericError.h"
33#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
34#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
35#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
36#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
37#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
38#include "llvm/DebugInfo/PDB/IPDBTable.h"
39#include "llvm/DebugInfo/PDB/PDBSymbol.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
41#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
42#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
46#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
47#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
48#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
49#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
50#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
51#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
52#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
53#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
54
55#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
56#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
57#include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
58
59#if defined(_WIN32)
60#include "llvm/Config/llvm-config.h"
61#include <optional>
62#endif
63
64using namespace lldb;
65using namespace lldb_private;
66using namespace llvm::pdb;
67
68LLDB_PLUGIN_DEFINE(SymbolFilePDB)
69
70char SymbolFilePDB::ID;
71
72namespace {
73lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
74 switch (lang) {
75 case PDB_Lang::Cpp:
76 return lldb::LanguageType::eLanguageTypeC_plus_plus;
77 case PDB_Lang::C:
78 return lldb::LanguageType::eLanguageTypeC;
79 case PDB_Lang::Swift:
80 return lldb::LanguageType::eLanguageTypeSwift;
81 case PDB_Lang::Rust:
82 return lldb::LanguageType::eLanguageTypeRust;
83 case PDB_Lang::ObjC:
84 return lldb::LanguageType::eLanguageTypeObjC;
85 case PDB_Lang::ObjCpp:
86 return lldb::LanguageType::eLanguageTypeObjC_plus_plus;
87 default:
88 return lldb::LanguageType::eLanguageTypeUnknown;
89 }
90}
91
92bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
93 uint32_t addr_length) {
94 return ((requested_line == 0 || actual_line == requested_line) &&
95 addr_length > 0);
96}
97} // namespace
98
99static bool ShouldUseNativeReader() {
100#if defined(_WIN32)
101#if LLVM_ENABLE_DIA_SDK
102 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
103 if (!use_native.equals_insensitive("on") &&
104 !use_native.equals_insensitive("yes") &&
105 !use_native.equals_insensitive("1") &&
106 !use_native.equals_insensitive("true"))
107 return false;
108#endif
109#endif
110 return true;
111}
112
113void SymbolFilePDB::Initialize() {
114 if (ShouldUseNativeReader()) {
115 npdb::SymbolFileNativePDB::Initialize();
116 } else {
117 PluginManager::RegisterPlugin(name: GetPluginNameStatic(),
118 description: GetPluginDescriptionStatic(), create_callback: CreateInstance,
119 debugger_init_callback: DebuggerInitialize);
120 }
121}
122
123void SymbolFilePDB::Terminate() {
124 if (ShouldUseNativeReader()) {
125 npdb::SymbolFileNativePDB::Terminate();
126 } else {
127 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
128 }
129}
130
131void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
132
133llvm::StringRef SymbolFilePDB::GetPluginDescriptionStatic() {
134 return "Microsoft PDB debug symbol file reader.";
135}
136
137lldb_private::SymbolFile *
138SymbolFilePDB::CreateInstance(ObjectFileSP objfile_sp) {
139 return new SymbolFilePDB(std::move(objfile_sp));
140}
141
142SymbolFilePDB::SymbolFilePDB(lldb::ObjectFileSP objfile_sp)
143 : SymbolFileCommon(std::move(objfile_sp)), m_session_up(), m_global_scope_up() {}
144
145SymbolFilePDB::~SymbolFilePDB() = default;
146
147uint32_t SymbolFilePDB::CalculateAbilities() {
148 uint32_t abilities = 0;
149 if (!m_objfile_sp)
150 return 0;
151
152 if (!m_session_up) {
153 // Lazily load and match the PDB file, but only do this once.
154 std::string exePath = m_objfile_sp->GetFileSpec().GetPath();
155 auto error = loadDataForEXE(Type: PDB_ReaderType::DIA, Path: llvm::StringRef(exePath),
156 Session&: m_session_up);
157 if (error) {
158 llvm::consumeError(Err: std::move(error));
159 auto module_sp = m_objfile_sp->GetModule();
160 if (!module_sp)
161 return 0;
162 // See if any symbol file is specified through `--symfile` option.
163 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
164 if (!symfile)
165 return 0;
166 error = loadDataForPDB(Type: PDB_ReaderType::DIA,
167 Path: llvm::StringRef(symfile.GetPath()), Session&: m_session_up);
168 if (error) {
169 llvm::consumeError(Err: std::move(error));
170 return 0;
171 }
172 }
173 }
174 if (!m_session_up)
175 return 0;
176
177 auto enum_tables_up = m_session_up->getEnumTables();
178 if (!enum_tables_up)
179 return 0;
180 while (auto table_up = enum_tables_up->getNext()) {
181 if (table_up->getItemCount() == 0)
182 continue;
183 auto type = table_up->getTableType();
184 switch (type) {
185 case PDB_TableType::Symbols:
186 // This table represents a store of symbols with types listed in
187 // PDBSym_Type
188 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
189 LocalVariables | VariableTypes);
190 break;
191 case PDB_TableType::LineNumbers:
192 abilities |= LineTables;
193 break;
194 default:
195 break;
196 }
197 }
198 return abilities;
199}
200
201void SymbolFilePDB::InitializeObject() {
202 lldb::addr_t obj_load_address =
203 m_objfile_sp->GetBaseAddress().GetFileAddress();
204 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
205 m_session_up->setLoadAddress(obj_load_address);
206 if (!m_global_scope_up)
207 m_global_scope_up = m_session_up->getGlobalScope();
208 lldbassert(m_global_scope_up.get());
209}
210
211uint32_t SymbolFilePDB::CalculateNumCompileUnits() {
212 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
213 if (!compilands)
214 return 0;
215
216 // The linker could link *.dll (compiland language = LINK), or import
217 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
218 // found as a child of the global scope (PDB executable). Usually, such
219 // compilands contain `thunk` symbols in which we are not interested for
220 // now. However we still count them in the compiland list. If we perform
221 // any compiland related activity, like finding symbols through
222 // llvm::pdb::IPDBSession methods, such compilands will all be searched
223 // automatically no matter whether we include them or not.
224 uint32_t compile_unit_count = compilands->getChildCount();
225
226 // The linker can inject an additional "dummy" compilation unit into the
227 // PDB. Ignore this special compile unit for our purposes, if it is there.
228 // It is always the last one.
229 auto last_compiland_up = compilands->getChildAtIndex(Index: compile_unit_count - 1);
230 lldbassert(last_compiland_up.get());
231 std::string name = last_compiland_up->getName();
232 if (name == "* Linker *")
233 --compile_unit_count;
234 return compile_unit_count;
235}
236
237void SymbolFilePDB::GetCompileUnitIndex(
238 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
239 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
240 if (!results_up)
241 return;
242 auto uid = pdb_compiland.getSymIndexId();
243 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
244 auto compiland_up = results_up->getChildAtIndex(Index: cu_idx);
245 if (!compiland_up)
246 continue;
247 if (compiland_up->getSymIndexId() == uid) {
248 index = cu_idx;
249 return;
250 }
251 }
252 index = UINT32_MAX;
253}
254
255std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
256SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
257 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(SymbolId: uid);
258}
259
260lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
261 if (index >= GetNumCompileUnits())
262 return CompUnitSP();
263
264 // Assuming we always retrieve same compilands listed in same order through
265 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
266 // compile unit makes no sense.
267 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
268 if (!results)
269 return CompUnitSP();
270 auto compiland_up = results->getChildAtIndex(Index: index);
271 if (!compiland_up)
272 return CompUnitSP();
273 return ParseCompileUnitForUID(id: compiland_up->getSymIndexId(), index);
274}
275
276lldb::LanguageType SymbolFilePDB::ParseLanguage(CompileUnit &comp_unit) {
277 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
278 auto compiland_up = GetPDBCompilandByUID(uid: comp_unit.GetID());
279 if (!compiland_up)
280 return lldb::eLanguageTypeUnknown;
281 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
282 if (!details)
283 return lldb::eLanguageTypeUnknown;
284 return TranslateLanguage(lang: details->getLanguage());
285}
286
287lldb_private::Function *
288SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(const PDBSymbolFunc &pdb_func,
289 CompileUnit &comp_unit) {
290 if (FunctionSP result = comp_unit.FindFunctionByUID(uid: pdb_func.getSymIndexId()))
291 return result.get();
292
293 auto file_vm_addr = pdb_func.getVirtualAddress();
294 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
295 return nullptr;
296
297 auto func_length = pdb_func.getLength();
298 AddressRange func_range =
299 AddressRange(file_vm_addr, func_length,
300 GetObjectFile()->GetModule()->GetSectionList());
301 if (!func_range.GetBaseAddress().IsValid())
302 return nullptr;
303
304 lldb_private::Type *func_type = ResolveTypeUID(type_uid: pdb_func.getSymIndexId());
305 if (!func_type)
306 return nullptr;
307
308 user_id_t func_type_uid = pdb_func.getSignatureId();
309
310 Mangled mangled = GetMangledForPDBFunc(pdb_func);
311
312 FunctionSP func_sp =
313 std::make_shared<Function>(args: &comp_unit, args: pdb_func.getSymIndexId(),
314 args&: func_type_uid, args&: mangled, args&: func_type, args&: func_range);
315
316 comp_unit.AddFunction(function_sp&: func_sp);
317
318 LanguageType lang = ParseLanguage(comp_unit);
319 auto type_system_or_err = GetTypeSystemForLanguage(language: lang);
320 if (auto err = type_system_or_err.takeError()) {
321 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
322 "Unable to parse PDBFunc: {0}");
323 return nullptr;
324 }
325
326 auto ts = *type_system_or_err;
327 TypeSystemClang *clang_type_system =
328 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
329 if (!clang_type_system)
330 return nullptr;
331 clang_type_system->GetPDBParser()->GetDeclForSymbol(symbol: pdb_func);
332
333 return func_sp.get();
334}
335
336size_t SymbolFilePDB::ParseFunctions(CompileUnit &comp_unit) {
337 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
338 size_t func_added = 0;
339 auto compiland_up = GetPDBCompilandByUID(uid: comp_unit.GetID());
340 if (!compiland_up)
341 return 0;
342 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
343 if (!results_up)
344 return 0;
345 while (auto pdb_func_up = results_up->getNext()) {
346 auto func_sp = comp_unit.FindFunctionByUID(uid: pdb_func_up->getSymIndexId());
347 if (!func_sp) {
348 if (ParseCompileUnitFunctionForPDBFunc(pdb_func: *pdb_func_up, comp_unit))
349 ++func_added;
350 }
351 }
352 return func_added;
353}
354
355bool SymbolFilePDB::ParseLineTable(CompileUnit &comp_unit) {
356 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
357 if (comp_unit.GetLineTable())
358 return true;
359 return ParseCompileUnitLineTable(comp_unit, match_line: 0);
360}
361
362bool SymbolFilePDB::ParseDebugMacros(CompileUnit &comp_unit) {
363 // PDB doesn't contain information about macros
364 return false;
365}
366
367bool SymbolFilePDB::ParseSupportFiles(
368 CompileUnit &comp_unit, lldb_private::SupportFileList &support_files) {
369
370 // In theory this is unnecessary work for us, because all of this information
371 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
372 // second time seems like a waste. Unfortunately, there's no good way around
373 // this short of a moderate refactor since SymbolVendor depends on being able
374 // to cache this list.
375 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
376 auto compiland_up = GetPDBCompilandByUID(uid: comp_unit.GetID());
377 if (!compiland_up)
378 return false;
379 auto files = m_session_up->getSourceFilesForCompiland(Compiland: *compiland_up);
380 if (!files || files->getChildCount() == 0)
381 return false;
382
383 while (auto file = files->getNext()) {
384 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
385 support_files.AppendIfUnique(file: spec);
386 }
387
388 return true;
389}
390
391bool SymbolFilePDB::ParseImportedModules(
392 const lldb_private::SymbolContext &sc,
393 std::vector<SourceModule> &imported_modules) {
394 // PDB does not yet support module debug info
395 return false;
396}
397
398static size_t ParseFunctionBlocksForPDBSymbol(
399 uint64_t func_file_vm_addr, const llvm::pdb::PDBSymbol *pdb_symbol,
400 lldb_private::Block *parent_block, bool is_top_parent) {
401 assert(pdb_symbol && parent_block);
402
403 size_t num_added = 0;
404 switch (pdb_symbol->getSymTag()) {
405 case PDB_SymType::Block:
406 case PDB_SymType::Function: {
407 Block *block = nullptr;
408 auto &raw_sym = pdb_symbol->getRawSymbol();
409 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(Val: pdb_symbol)) {
410 if (pdb_func->hasNoInlineAttribute())
411 break;
412 if (is_top_parent)
413 block = parent_block;
414 else
415 break;
416 } else if (llvm::isa<PDBSymbolBlock>(Val: pdb_symbol)) {
417 auto uid = pdb_symbol->getSymIndexId();
418 if (parent_block->FindBlockByID(block_id: uid))
419 break;
420 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
421 break;
422
423 auto block_sp = std::make_shared<Block>(args: pdb_symbol->getSymIndexId());
424 parent_block->AddChild(child_block_sp: block_sp);
425 block = block_sp.get();
426 } else
427 llvm_unreachable("Unexpected PDB symbol!");
428
429 block->AddRange(range: Block::Range(
430 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
431 block->FinalizeRanges();
432 ++num_added;
433
434 auto results_up = pdb_symbol->findAllChildren();
435 if (!results_up)
436 break;
437 while (auto symbol_up = results_up->getNext()) {
438 num_added += ParseFunctionBlocksForPDBSymbol(
439 func_file_vm_addr, pdb_symbol: symbol_up.get(), parent_block: block, is_top_parent: false);
440 }
441 } break;
442 default:
443 break;
444 }
445 return num_added;
446}
447
448size_t SymbolFilePDB::ParseBlocksRecursive(Function &func) {
449 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
450 size_t num_added = 0;
451 auto uid = func.GetID();
452 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(SymbolId: uid);
453 if (!pdb_func_up)
454 return 0;
455 Block &parent_block = func.GetBlock(can_create: false);
456 num_added = ParseFunctionBlocksForPDBSymbol(
457 func_file_vm_addr: pdb_func_up->getVirtualAddress(), pdb_symbol: pdb_func_up.get(), parent_block: &parent_block, is_top_parent: true);
458 return num_added;
459}
460
461size_t SymbolFilePDB::ParseTypes(CompileUnit &comp_unit) {
462 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
463
464 size_t num_added = 0;
465 auto compiland = GetPDBCompilandByUID(uid: comp_unit.GetID());
466 if (!compiland)
467 return 0;
468
469 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
470 std::unique_ptr<IPDBEnumSymbols> results;
471 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
472 PDB_SymType::UDT};
473 for (auto tag : tags_to_search) {
474 results = raw_sym.findAllChildren(Type: tag);
475 if (!results || results->getChildCount() == 0)
476 continue;
477 while (auto symbol = results->getNext()) {
478 switch (symbol->getSymTag()) {
479 case PDB_SymType::Enum:
480 case PDB_SymType::UDT:
481 case PDB_SymType::Typedef:
482 break;
483 default:
484 continue;
485 }
486
487 // This should cause the type to get cached and stored in the `m_types`
488 // lookup.
489 if (auto type = ResolveTypeUID(type_uid: symbol->getSymIndexId())) {
490 // Resolve the type completely to avoid a completion
491 // (and so a list change, which causes an iterators invalidation)
492 // during a TypeList dumping
493 type->GetFullCompilerType();
494 ++num_added;
495 }
496 }
497 }
498 };
499
500 ParseTypesByTagFn(*compiland);
501
502 // Also parse global types particularly coming from this compiland.
503 // Unfortunately, PDB has no compiland information for each global type. We
504 // have to parse them all. But ensure we only do this once.
505 static bool parse_all_global_types = false;
506 if (!parse_all_global_types) {
507 ParseTypesByTagFn(*m_global_scope_up);
508 parse_all_global_types = true;
509 }
510 return num_added;
511}
512
513size_t
514SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
515 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
516 if (!sc.comp_unit)
517 return 0;
518
519 size_t num_added = 0;
520 if (sc.function) {
521 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
522 SymbolId: sc.function->GetID());
523 if (!pdb_func)
524 return 0;
525
526 num_added += ParseVariables(sc, pdb_data: *pdb_func);
527 sc.function->GetBlock(can_create: false).SetDidParseVariables(b: true, set_children: true);
528 } else if (sc.comp_unit) {
529 auto compiland = GetPDBCompilandByUID(uid: sc.comp_unit->GetID());
530 if (!compiland)
531 return 0;
532
533 if (sc.comp_unit->GetVariableList(can_create: false))
534 return 0;
535
536 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
537 if (results && results->getChildCount()) {
538 while (auto result = results->getNext()) {
539 auto cu_id = GetCompilandId(data: *result);
540 // FIXME: We are not able to determine variable's compile unit.
541 if (cu_id == 0)
542 continue;
543
544 if (cu_id == sc.comp_unit->GetID())
545 num_added += ParseVariables(sc, pdb_data: *result);
546 }
547 }
548
549 // FIXME: A `file static` or `global constant` variable appears both in
550 // compiland's children and global scope's children with unexpectedly
551 // different symbol's Id making it ambiguous.
552
553 // FIXME: 'local constant', for example, const char var[] = "abc", declared
554 // in a function scope, can't be found in PDB.
555
556 // Parse variables in this compiland.
557 num_added += ParseVariables(sc, pdb_data: *compiland);
558 }
559
560 return num_added;
561}
562
563lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
564 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
565 auto find_result = m_types.find(Val: type_uid);
566 if (find_result != m_types.end())
567 return find_result->second.get();
568
569 auto type_system_or_err =
570 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
571 if (auto err = type_system_or_err.takeError()) {
572 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
573 "Unable to ResolveTypeUID: {0}");
574 return nullptr;
575 }
576
577 auto ts = *type_system_or_err;
578 TypeSystemClang *clang_type_system =
579 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
580 if (!clang_type_system)
581 return nullptr;
582 PDBASTParser *pdb = clang_type_system->GetPDBParser();
583 if (!pdb)
584 return nullptr;
585
586 auto pdb_type = m_session_up->getSymbolById(SymbolId: type_uid);
587 if (pdb_type == nullptr)
588 return nullptr;
589
590 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(type: *pdb_type);
591 if (result) {
592 m_types.insert(KV: std::make_pair(x&: type_uid, y&: result));
593 }
594 return result.get();
595}
596
597std::optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
598 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
599 return std::nullopt;
600}
601
602bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
603 std::lock_guard<std::recursive_mutex> guard(
604 GetObjectFile()->GetModule()->GetMutex());
605
606 auto type_system_or_err =
607 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
608 if (auto err = type_system_or_err.takeError()) {
609 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
610 "Unable to get dynamic array info for UID: {0}");
611 return false;
612 }
613 auto ts = *type_system_or_err;
614 TypeSystemClang *clang_ast_ctx =
615 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
616
617 if (!clang_ast_ctx)
618 return false;
619
620 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
621 if (!pdb)
622 return false;
623
624 return pdb->CompleteTypeFromPDB(compiler_type);
625}
626
627lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
628 auto type_system_or_err =
629 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
630 if (auto err = type_system_or_err.takeError()) {
631 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
632 "Unable to get decl for UID: {0}");
633 return CompilerDecl();
634 }
635 auto ts = *type_system_or_err;
636 TypeSystemClang *clang_ast_ctx =
637 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
638 if (!clang_ast_ctx)
639 return CompilerDecl();
640
641 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
642 if (!pdb)
643 return CompilerDecl();
644
645 auto symbol = m_session_up->getSymbolById(SymbolId: uid);
646 if (!symbol)
647 return CompilerDecl();
648
649 auto decl = pdb->GetDeclForSymbol(symbol: *symbol);
650 if (!decl)
651 return CompilerDecl();
652
653 return clang_ast_ctx->GetCompilerDecl(decl);
654}
655
656lldb_private::CompilerDeclContext
657SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
658 auto type_system_or_err =
659 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
660 if (auto err = type_system_or_err.takeError()) {
661 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
662 "Unable to get DeclContext for UID: {0}");
663 return CompilerDeclContext();
664 }
665
666 auto ts = *type_system_or_err;
667 TypeSystemClang *clang_ast_ctx =
668 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
669 if (!clang_ast_ctx)
670 return CompilerDeclContext();
671
672 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
673 if (!pdb)
674 return CompilerDeclContext();
675
676 auto symbol = m_session_up->getSymbolById(SymbolId: uid);
677 if (!symbol)
678 return CompilerDeclContext();
679
680 auto decl_context = pdb->GetDeclContextForSymbol(symbol: *symbol);
681 if (!decl_context)
682 return GetDeclContextContainingUID(uid);
683
684 return clang_ast_ctx->CreateDeclContext(ctx: decl_context);
685}
686
687lldb_private::CompilerDeclContext
688SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
689 auto type_system_or_err =
690 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
691 if (auto err = type_system_or_err.takeError()) {
692 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
693 "Unable to get DeclContext containing UID: {0}");
694 return CompilerDeclContext();
695 }
696
697 auto ts = *type_system_or_err;
698 TypeSystemClang *clang_ast_ctx =
699 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
700 if (!clang_ast_ctx)
701 return CompilerDeclContext();
702
703 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
704 if (!pdb)
705 return CompilerDeclContext();
706
707 auto symbol = m_session_up->getSymbolById(SymbolId: uid);
708 if (!symbol)
709 return CompilerDeclContext();
710
711 auto decl_context = pdb->GetDeclContextContainingSymbol(symbol: *symbol);
712 assert(decl_context);
713
714 return clang_ast_ctx->CreateDeclContext(ctx: decl_context);
715}
716
717void SymbolFilePDB::ParseDeclsForContext(
718 lldb_private::CompilerDeclContext decl_ctx) {
719 auto type_system_or_err =
720 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
721 if (auto err = type_system_or_err.takeError()) {
722 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
723 "Unable to parse decls for context: {0}");
724 return;
725 }
726
727 auto ts = *type_system_or_err;
728 TypeSystemClang *clang_ast_ctx =
729 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
730 if (!clang_ast_ctx)
731 return;
732
733 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
734 if (!pdb)
735 return;
736
737 pdb->ParseDeclsForDeclContext(
738 decl_context: static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
739}
740
741uint32_t
742SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
743 SymbolContextItem resolve_scope,
744 lldb_private::SymbolContext &sc) {
745 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
746 uint32_t resolved_flags = 0;
747 if (resolve_scope & eSymbolContextCompUnit ||
748 resolve_scope & eSymbolContextVariable ||
749 resolve_scope & eSymbolContextFunction ||
750 resolve_scope & eSymbolContextBlock ||
751 resolve_scope & eSymbolContextLineEntry) {
752 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
753 if (!cu_sp) {
754 if (resolved_flags & eSymbolContextVariable) {
755 // TODO: Resolve variables
756 }
757 return 0;
758 }
759 sc.comp_unit = cu_sp.get();
760 resolved_flags |= eSymbolContextCompUnit;
761 lldbassert(sc.module_sp == cu_sp->GetModule());
762 }
763
764 if (resolve_scope & eSymbolContextFunction ||
765 resolve_scope & eSymbolContextBlock) {
766 addr_t file_vm_addr = so_addr.GetFileAddress();
767 auto symbol_up =
768 m_session_up->findSymbolByAddress(Address: file_vm_addr, Type: PDB_SymType::Function);
769 if (symbol_up) {
770 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(Val: symbol_up.get());
771 assert(pdb_func);
772 auto func_uid = pdb_func->getSymIndexId();
773 sc.function = sc.comp_unit->FindFunctionByUID(uid: func_uid).get();
774 if (sc.function == nullptr)
775 sc.function =
776 ParseCompileUnitFunctionForPDBFunc(pdb_func: *pdb_func, comp_unit&: *sc.comp_unit);
777 if (sc.function) {
778 resolved_flags |= eSymbolContextFunction;
779 if (resolve_scope & eSymbolContextBlock) {
780 auto block_symbol = m_session_up->findSymbolByAddress(
781 Address: file_vm_addr, Type: PDB_SymType::Block);
782 auto block_id = block_symbol ? block_symbol->getSymIndexId()
783 : sc.function->GetID();
784 sc.block = sc.function->GetBlock(can_create: true).FindBlockByID(block_id);
785 if (sc.block)
786 resolved_flags |= eSymbolContextBlock;
787 }
788 }
789 }
790 }
791
792 if (resolve_scope & eSymbolContextLineEntry) {
793 if (auto *line_table = sc.comp_unit->GetLineTable()) {
794 Address addr(so_addr);
795 if (line_table->FindLineEntryByAddress(so_addr: addr, line_entry&: sc.line_entry))
796 resolved_flags |= eSymbolContextLineEntry;
797 }
798 }
799
800 return resolved_flags;
801}
802
803uint32_t SymbolFilePDB::ResolveSymbolContext(
804 const lldb_private::SourceLocationSpec &src_location_spec,
805 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
806 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
807 const size_t old_size = sc_list.GetSize();
808 const FileSpec &file_spec = src_location_spec.GetFileSpec();
809 const uint32_t line = src_location_spec.GetLine().value_or(u: 0);
810 if (resolve_scope & lldb::eSymbolContextCompUnit) {
811 // Locate all compilation units with line numbers referencing the specified
812 // file. For example, if `file_spec` is <vector>, then this should return
813 // all source files and header files that reference <vector>, either
814 // directly or indirectly.
815 auto compilands = m_session_up->findCompilandsForSourceFile(
816 Pattern: file_spec.GetPath(), Flags: PDB_NameSearchFlags::NS_CaseInsensitive);
817
818 if (!compilands)
819 return 0;
820
821 // For each one, either find its previously parsed data or parse it afresh
822 // and add it to the symbol context list.
823 while (auto compiland = compilands->getNext()) {
824 // If we're not checking inlines, then don't add line information for
825 // this file unless the FileSpec matches. For inline functions, we don't
826 // have to match the FileSpec since they could be defined in headers
827 // other than file specified in FileSpec.
828 if (!src_location_spec.GetCheckInlines()) {
829 std::string source_file = compiland->getSourceFileFullPath();
830 if (source_file.empty())
831 continue;
832 FileSpec this_spec(source_file, FileSpec::Style::windows);
833 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
834 if (FileSpec::Compare(lhs: file_spec, rhs: this_spec, full: need_full_match) != 0)
835 continue;
836 }
837
838 SymbolContext sc;
839 auto cu = ParseCompileUnitForUID(id: compiland->getSymIndexId());
840 if (!cu)
841 continue;
842 sc.comp_unit = cu.get();
843 sc.module_sp = cu->GetModule();
844
845 // If we were asked to resolve line entries, add all entries to the line
846 // table that match the requested line (or all lines if `line` == 0).
847 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
848 eSymbolContextLineEntry)) {
849 bool has_line_table = ParseCompileUnitLineTable(comp_unit&: *sc.comp_unit, match_line: line);
850
851 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
852 // The query asks for line entries, but we can't get them for the
853 // compile unit. This is not normal for `line` = 0. So just assert
854 // it.
855 assert(line && "Couldn't get all line entries!\n");
856
857 // Current compiland does not have the requested line. Search next.
858 continue;
859 }
860
861 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
862 if (!has_line_table)
863 continue;
864
865 auto *line_table = sc.comp_unit->GetLineTable();
866 lldbassert(line_table);
867
868 uint32_t num_line_entries = line_table->GetSize();
869 // Skip the terminal line entry.
870 --num_line_entries;
871
872 // If `line `!= 0, see if we can resolve function for each line entry
873 // in the line table.
874 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
875 ++line_idx) {
876 if (!line_table->GetLineEntryAtIndex(idx: line_idx, line_entry&: sc.line_entry))
877 continue;
878
879 auto file_vm_addr =
880 sc.line_entry.range.GetBaseAddress().GetFileAddress();
881 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
882 continue;
883
884 auto symbol_up = m_session_up->findSymbolByAddress(
885 Address: file_vm_addr, Type: PDB_SymType::Function);
886 if (symbol_up) {
887 auto func_uid = symbol_up->getSymIndexId();
888 sc.function = sc.comp_unit->FindFunctionByUID(uid: func_uid).get();
889 if (sc.function == nullptr) {
890 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(Val: symbol_up.get());
891 assert(pdb_func);
892 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func: *pdb_func,
893 comp_unit&: *sc.comp_unit);
894 }
895 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
896 Block &block = sc.function->GetBlock(can_create: true);
897 sc.block = block.FindBlockByID(block_id: sc.function->GetID());
898 }
899 }
900 sc_list.Append(sc);
901 }
902 } else if (has_line_table) {
903 // We can parse line table for the compile unit. But no query to
904 // resolve function or block. We append `sc` to the list anyway.
905 sc_list.Append(sc);
906 }
907 } else {
908 // No query for line entry, function or block. But we have a valid
909 // compile unit, append `sc` to the list.
910 sc_list.Append(sc);
911 }
912 }
913 }
914 return sc_list.GetSize() - old_size;
915}
916
917std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
918 // Cache public names at first
919 if (m_public_names.empty())
920 if (auto result_up =
921 m_global_scope_up->findAllChildren(Type: PDB_SymType::PublicSymbol))
922 while (auto symbol_up = result_up->getNext())
923 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
924 m_public_names[addr] = symbol_up->getRawSymbol().getName();
925
926 // Look up the name in the cache
927 return m_public_names.lookup(Val: pdb_data.getVirtualAddress());
928}
929
930VariableSP SymbolFilePDB::ParseVariableForPDBData(
931 const lldb_private::SymbolContext &sc,
932 const llvm::pdb::PDBSymbolData &pdb_data) {
933 VariableSP var_sp;
934 uint32_t var_uid = pdb_data.getSymIndexId();
935 auto result = m_variables.find(Val: var_uid);
936 if (result != m_variables.end())
937 return result->second;
938
939 ValueType scope = eValueTypeInvalid;
940 bool is_static_member = false;
941 bool is_external = false;
942 bool is_artificial = false;
943
944 switch (pdb_data.getDataKind()) {
945 case PDB_DataKind::Global:
946 scope = eValueTypeVariableGlobal;
947 is_external = true;
948 break;
949 case PDB_DataKind::Local:
950 scope = eValueTypeVariableLocal;
951 break;
952 case PDB_DataKind::FileStatic:
953 scope = eValueTypeVariableStatic;
954 break;
955 case PDB_DataKind::StaticMember:
956 is_static_member = true;
957 scope = eValueTypeVariableStatic;
958 break;
959 case PDB_DataKind::Member:
960 scope = eValueTypeVariableStatic;
961 break;
962 case PDB_DataKind::Param:
963 scope = eValueTypeVariableArgument;
964 break;
965 case PDB_DataKind::Constant:
966 scope = eValueTypeConstResult;
967 break;
968 default:
969 break;
970 }
971
972 switch (pdb_data.getLocationType()) {
973 case PDB_LocType::TLS:
974 scope = eValueTypeVariableThreadLocal;
975 break;
976 case PDB_LocType::RegRel: {
977 // It is a `this` pointer.
978 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
979 scope = eValueTypeVariableArgument;
980 is_artificial = true;
981 }
982 } break;
983 default:
984 break;
985 }
986
987 Declaration decl;
988 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
989 if (auto lines = pdb_data.getLineNumbers()) {
990 if (auto first_line = lines->getNext()) {
991 uint32_t src_file_id = first_line->getSourceFileId();
992 auto src_file = m_session_up->getSourceFileById(FileId: src_file_id);
993 if (src_file) {
994 FileSpec spec(src_file->getFileName());
995 decl.SetFile(spec);
996 decl.SetColumn(first_line->getColumnNumber());
997 decl.SetLine(first_line->getLineNumber());
998 }
999 }
1000 }
1001 }
1002
1003 Variable::RangeList ranges;
1004 SymbolContextScope *context_scope = sc.comp_unit;
1005 if (scope == eValueTypeVariableLocal || scope == eValueTypeVariableArgument) {
1006 if (sc.function) {
1007 Block &function_block = sc.function->GetBlock(can_create: true);
1008 Block *block =
1009 function_block.FindBlockByID(block_id: pdb_data.getLexicalParentId());
1010 if (!block)
1011 block = &function_block;
1012
1013 context_scope = block;
1014
1015 for (size_t i = 0, num_ranges = block->GetNumRanges(); i < num_ranges;
1016 ++i) {
1017 AddressRange range;
1018 if (!block->GetRangeAtIndex(range_idx: i, range))
1019 continue;
1020
1021 ranges.Append(base: range.GetBaseAddress().GetFileAddress(),
1022 size: range.GetByteSize());
1023 }
1024 }
1025 }
1026
1027 SymbolFileTypeSP type_sp =
1028 std::make_shared<SymbolFileType>(args&: *this, args: pdb_data.getTypeId());
1029
1030 auto var_name = pdb_data.getName();
1031 auto mangled = GetMangledForPDBData(pdb_data);
1032 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
1033
1034 bool is_constant;
1035 ModuleSP module_sp = GetObjectFile()->GetModule();
1036 DWARFExpressionList location(module_sp,
1037 ConvertPDBLocationToDWARFExpression(
1038 module: module_sp, symbol: pdb_data, ranges, is_constant),
1039 nullptr);
1040
1041 var_sp = std::make_shared<Variable>(
1042 args&: var_uid, args: var_name.c_str(), args&: mangled_cstr, args&: type_sp, args&: scope, args&: context_scope,
1043 args&: ranges, args: &decl, args&: location, args&: is_external, args&: is_artificial, args&: is_constant,
1044 args&: is_static_member);
1045
1046 m_variables.insert(KV: std::make_pair(x&: var_uid, y&: var_sp));
1047 return var_sp;
1048}
1049
1050size_t
1051SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
1052 const llvm::pdb::PDBSymbol &pdb_symbol,
1053 lldb_private::VariableList *variable_list) {
1054 size_t num_added = 0;
1055
1056 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(Val: &pdb_symbol)) {
1057 VariableListSP local_variable_list_sp;
1058
1059 auto result = m_variables.find(Val: pdb_data->getSymIndexId());
1060 if (result != m_variables.end()) {
1061 if (variable_list)
1062 variable_list->AddVariableIfUnique(var_sp: result->second);
1063 } else {
1064 // Prepare right VariableList for this variable.
1065 if (auto lexical_parent = pdb_data->getLexicalParent()) {
1066 switch (lexical_parent->getSymTag()) {
1067 case PDB_SymType::Exe:
1068 assert(sc.comp_unit);
1069 [[fallthrough]];
1070 case PDB_SymType::Compiland: {
1071 if (sc.comp_unit) {
1072 local_variable_list_sp = sc.comp_unit->GetVariableList(can_create: false);
1073 if (!local_variable_list_sp) {
1074 local_variable_list_sp = std::make_shared<VariableList>();
1075 sc.comp_unit->SetVariableList(local_variable_list_sp);
1076 }
1077 }
1078 } break;
1079 case PDB_SymType::Block:
1080 case PDB_SymType::Function: {
1081 if (sc.function) {
1082 Block *block = sc.function->GetBlock(can_create: true).FindBlockByID(
1083 block_id: lexical_parent->getSymIndexId());
1084 if (block) {
1085 local_variable_list_sp = block->GetBlockVariableList(can_create: false);
1086 if (!local_variable_list_sp) {
1087 local_variable_list_sp = std::make_shared<VariableList>();
1088 block->SetVariableList(local_variable_list_sp);
1089 }
1090 }
1091 }
1092 } break;
1093 default:
1094 break;
1095 }
1096 }
1097
1098 if (local_variable_list_sp) {
1099 if (auto var_sp = ParseVariableForPDBData(sc, pdb_data: *pdb_data)) {
1100 local_variable_list_sp->AddVariableIfUnique(var_sp);
1101 if (variable_list)
1102 variable_list->AddVariableIfUnique(var_sp);
1103 ++num_added;
1104 PDBASTParser *ast = GetPDBAstParser();
1105 if (ast)
1106 ast->GetDeclForSymbol(symbol: *pdb_data);
1107 }
1108 }
1109 }
1110 }
1111
1112 if (auto results = pdb_symbol.findAllChildren()) {
1113 while (auto result = results->getNext())
1114 num_added += ParseVariables(sc, pdb_symbol: *result, variable_list);
1115 }
1116
1117 return num_added;
1118}
1119
1120void SymbolFilePDB::FindGlobalVariables(
1121 lldb_private::ConstString name, const CompilerDeclContext &parent_decl_ctx,
1122 uint32_t max_matches, lldb_private::VariableList &variables) {
1123 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1124 if (!DeclContextMatchesThisSymbolFile(decl_ctx: parent_decl_ctx))
1125 return;
1126 if (name.IsEmpty())
1127 return;
1128
1129 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1130 if (!results)
1131 return;
1132
1133 uint32_t matches = 0;
1134 size_t old_size = variables.GetSize();
1135 while (auto result = results->getNext()) {
1136 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(Val: result.get());
1137 if (max_matches > 0 && matches >= max_matches)
1138 break;
1139
1140 SymbolContext sc;
1141 sc.module_sp = m_objfile_sp->GetModule();
1142 lldbassert(sc.module_sp.get());
1143
1144 if (!name.GetStringRef().equals(
1145 RHS: MSVCUndecoratedNameParser::DropScope(name: pdb_data->getName())))
1146 continue;
1147
1148 sc.comp_unit = ParseCompileUnitForUID(id: GetCompilandId(data: *pdb_data)).get();
1149 // FIXME: We are not able to determine the compile unit.
1150 if (sc.comp_unit == nullptr)
1151 continue;
1152
1153 if (parent_decl_ctx.IsValid() &&
1154 GetDeclContextContainingUID(uid: result->getSymIndexId()) != parent_decl_ctx)
1155 continue;
1156
1157 ParseVariables(sc, pdb_symbol: *pdb_data, variable_list: &variables);
1158 matches = variables.GetSize() - old_size;
1159 }
1160}
1161
1162void SymbolFilePDB::FindGlobalVariables(
1163 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1164 lldb_private::VariableList &variables) {
1165 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1166 if (!regex.IsValid())
1167 return;
1168 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1169 if (!results)
1170 return;
1171
1172 uint32_t matches = 0;
1173 size_t old_size = variables.GetSize();
1174 while (auto pdb_data = results->getNext()) {
1175 if (max_matches > 0 && matches >= max_matches)
1176 break;
1177
1178 auto var_name = pdb_data->getName();
1179 if (var_name.empty())
1180 continue;
1181 if (!regex.Execute(string: var_name))
1182 continue;
1183 SymbolContext sc;
1184 sc.module_sp = m_objfile_sp->GetModule();
1185 lldbassert(sc.module_sp.get());
1186
1187 sc.comp_unit = ParseCompileUnitForUID(id: GetCompilandId(data: *pdb_data)).get();
1188 // FIXME: We are not able to determine the compile unit.
1189 if (sc.comp_unit == nullptr)
1190 continue;
1191
1192 ParseVariables(sc, pdb_symbol: *pdb_data, variable_list: &variables);
1193 matches = variables.GetSize() - old_size;
1194 }
1195}
1196
1197bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
1198 bool include_inlines,
1199 lldb_private::SymbolContextList &sc_list) {
1200 lldb_private::SymbolContext sc;
1201 sc.comp_unit = ParseCompileUnitForUID(id: pdb_func.getCompilandId()).get();
1202 if (!sc.comp_unit)
1203 return false;
1204 sc.module_sp = sc.comp_unit->GetModule();
1205 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, comp_unit&: *sc.comp_unit);
1206 if (!sc.function)
1207 return false;
1208
1209 sc_list.Append(sc);
1210 return true;
1211}
1212
1213bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1214 lldb_private::SymbolContextList &sc_list) {
1215 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(SymbolId: uid);
1216 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1217 return false;
1218 return ResolveFunction(pdb_func: *pdb_func_up, include_inlines, sc_list);
1219}
1220
1221void SymbolFilePDB::CacheFunctionNames() {
1222 if (!m_func_full_names.IsEmpty())
1223 return;
1224
1225 std::map<uint64_t, uint32_t> addr_ids;
1226
1227 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1228 while (auto pdb_func_up = results_up->getNext()) {
1229 if (pdb_func_up->isCompilerGenerated())
1230 continue;
1231
1232 auto name = pdb_func_up->getName();
1233 auto demangled_name = pdb_func_up->getUndecoratedName();
1234 if (name.empty() && demangled_name.empty())
1235 continue;
1236
1237 auto uid = pdb_func_up->getSymIndexId();
1238 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1239 addr_ids.insert(x: std::make_pair(x: pdb_func_up->getVirtualAddress(), y&: uid));
1240
1241 if (auto parent = pdb_func_up->getClassParent()) {
1242
1243 // PDB have symbols for class/struct methods or static methods in Enum
1244 // Class. We won't bother to check if the parent is UDT or Enum here.
1245 m_func_method_names.Append(unique_cstr: ConstString(name), value: uid);
1246
1247 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1248 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1249 // not have information of this, we extract base names and cache them
1250 // by our own effort.
1251 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1252 if (!basename.empty())
1253 m_func_base_names.Append(unique_cstr: ConstString(basename), value: uid);
1254 else {
1255 m_func_base_names.Append(unique_cstr: ConstString(name), value: uid);
1256 }
1257
1258 if (!demangled_name.empty())
1259 m_func_full_names.Append(unique_cstr: ConstString(demangled_name), value: uid);
1260
1261 } else {
1262 // Handle not-method symbols.
1263
1264 // The function name might contain namespace, or its lexical scope.
1265 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1266 if (!basename.empty())
1267 m_func_base_names.Append(unique_cstr: ConstString(basename), value: uid);
1268 else
1269 m_func_base_names.Append(unique_cstr: ConstString(name), value: uid);
1270
1271 if (name == "main") {
1272 m_func_full_names.Append(unique_cstr: ConstString(name), value: uid);
1273
1274 if (!demangled_name.empty() && name != demangled_name) {
1275 m_func_full_names.Append(unique_cstr: ConstString(demangled_name), value: uid);
1276 m_func_base_names.Append(unique_cstr: ConstString(demangled_name), value: uid);
1277 }
1278 } else if (!demangled_name.empty()) {
1279 m_func_full_names.Append(unique_cstr: ConstString(demangled_name), value: uid);
1280 } else {
1281 m_func_full_names.Append(unique_cstr: ConstString(name), value: uid);
1282 }
1283 }
1284 }
1285 }
1286
1287 if (auto results_up =
1288 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
1289 while (auto pub_sym_up = results_up->getNext()) {
1290 if (!pub_sym_up->isFunction())
1291 continue;
1292 auto name = pub_sym_up->getName();
1293 if (name.empty())
1294 continue;
1295
1296 if (CPlusPlusLanguage::IsCPPMangledName(name: name.c_str())) {
1297 auto vm_addr = pub_sym_up->getVirtualAddress();
1298
1299 // PDB public symbol has mangled name for its associated function.
1300 if (vm_addr && addr_ids.find(x: vm_addr) != addr_ids.end()) {
1301 // Cache mangled name.
1302 m_func_full_names.Append(unique_cstr: ConstString(name), value: addr_ids[vm_addr]);
1303 }
1304 }
1305 }
1306 }
1307 // Sort them before value searching is working properly
1308 m_func_full_names.Sort();
1309 m_func_full_names.SizeToFit();
1310 m_func_method_names.Sort();
1311 m_func_method_names.SizeToFit();
1312 m_func_base_names.Sort();
1313 m_func_base_names.SizeToFit();
1314}
1315
1316void SymbolFilePDB::FindFunctions(
1317 const lldb_private::Module::LookupInfo &lookup_info,
1318 const lldb_private::CompilerDeclContext &parent_decl_ctx,
1319 bool include_inlines,
1320 lldb_private::SymbolContextList &sc_list) {
1321 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1322 ConstString name = lookup_info.GetLookupName();
1323 FunctionNameType name_type_mask = lookup_info.GetNameTypeMask();
1324 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1325
1326 if (name_type_mask & eFunctionNameTypeFull)
1327 name = lookup_info.GetName();
1328
1329 if (name_type_mask == eFunctionNameTypeNone)
1330 return;
1331 if (!DeclContextMatchesThisSymbolFile(decl_ctx: parent_decl_ctx))
1332 return;
1333 if (name.IsEmpty())
1334 return;
1335
1336 if (name_type_mask & eFunctionNameTypeFull ||
1337 name_type_mask & eFunctionNameTypeBase ||
1338 name_type_mask & eFunctionNameTypeMethod) {
1339 CacheFunctionNames();
1340
1341 std::set<uint32_t> resolved_ids;
1342 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1343 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
1344 std::vector<uint32_t> ids;
1345 if (!Names.GetValues(unique_cstr: name, values&: ids))
1346 return;
1347
1348 for (uint32_t id : ids) {
1349 if (resolved_ids.find(x: id) != resolved_ids.end())
1350 continue;
1351
1352 if (parent_decl_ctx.IsValid() &&
1353 GetDeclContextContainingUID(uid: id) != parent_decl_ctx)
1354 continue;
1355
1356 if (ResolveFunction(uid: id, include_inlines, sc_list))
1357 resolved_ids.insert(x: id);
1358 }
1359 };
1360 if (name_type_mask & eFunctionNameTypeFull) {
1361 ResolveFn(m_func_full_names);
1362 ResolveFn(m_func_base_names);
1363 ResolveFn(m_func_method_names);
1364 }
1365 if (name_type_mask & eFunctionNameTypeBase)
1366 ResolveFn(m_func_base_names);
1367 if (name_type_mask & eFunctionNameTypeMethod)
1368 ResolveFn(m_func_method_names);
1369 }
1370}
1371
1372void SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1373 bool include_inlines,
1374 lldb_private::SymbolContextList &sc_list) {
1375 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1376 if (!regex.IsValid())
1377 return;
1378
1379 CacheFunctionNames();
1380
1381 std::set<uint32_t> resolved_ids;
1382 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1383 this](UniqueCStringMap<uint32_t> &Names) {
1384 std::vector<uint32_t> ids;
1385 if (Names.GetValues(regex, values&: ids)) {
1386 for (auto id : ids) {
1387 if (resolved_ids.find(x: id) == resolved_ids.end())
1388 if (ResolveFunction(uid: id, include_inlines, sc_list))
1389 resolved_ids.insert(x: id);
1390 }
1391 }
1392 };
1393 ResolveFn(m_func_full_names);
1394 ResolveFn(m_func_base_names);
1395}
1396
1397void SymbolFilePDB::GetMangledNamesForFunction(
1398 const std::string &scope_qualified_name,
1399 std::vector<lldb_private::ConstString> &mangled_names) {}
1400
1401void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1402 std::set<lldb::addr_t> sym_addresses;
1403 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1404 sym_addresses.insert(x: symtab.SymbolAtIndex(idx: i)->GetFileAddress());
1405
1406 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1407 if (!results)
1408 return;
1409
1410 auto section_list = m_objfile_sp->GetSectionList();
1411 if (!section_list)
1412 return;
1413
1414 while (auto pub_symbol = results->getNext()) {
1415 auto section_id = pub_symbol->getAddressSection();
1416
1417 auto section = section_list->FindSectionByID(sect_id: section_id);
1418 if (!section)
1419 continue;
1420
1421 auto offset = pub_symbol->getAddressOffset();
1422
1423 auto file_addr = section->GetFileAddress() + offset;
1424 if (sym_addresses.find(x: file_addr) != sym_addresses.end())
1425 continue;
1426 sym_addresses.insert(x: file_addr);
1427
1428 auto size = pub_symbol->getLength();
1429 symtab.AddSymbol(
1430 symbol: Symbol(pub_symbol->getSymIndexId(), // symID
1431 pub_symbol->getName().c_str(), // name
1432 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1433 true, // external
1434 false, // is_debug
1435 false, // is_trampoline
1436 false, // is_artificial
1437 section, // section_sp
1438 offset, // value
1439 size, // size
1440 size != 0, // size_is_valid
1441 false, // contains_linker_annotations
1442 0 // flags
1443 ));
1444 }
1445
1446 symtab.Finalize();
1447}
1448
1449void SymbolFilePDB::DumpClangAST(Stream &s) {
1450 auto type_system_or_err =
1451 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
1452 if (auto err = type_system_or_err.takeError()) {
1453 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1454 "Unable to dump ClangAST: {0}");
1455 return;
1456 }
1457
1458 auto ts = *type_system_or_err;
1459 TypeSystemClang *clang_type_system =
1460 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
1461 if (!clang_type_system)
1462 return;
1463 clang_type_system->Dump(output&: s.AsRawOstream());
1464}
1465
1466void SymbolFilePDB::FindTypesByRegex(
1467 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1468 lldb_private::TypeMap &types) {
1469 // When searching by regex, we need to go out of our way to limit the search
1470 // space as much as possible since this searches EVERYTHING in the PDB,
1471 // manually doing regex comparisons. PDB library isn't optimized for regex
1472 // searches or searches across multiple symbol types at the same time, so the
1473 // best we can do is to search enums, then typedefs, then classes one by one,
1474 // and do a regex comparison against each of them.
1475 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1476 PDB_SymType::UDT};
1477 std::unique_ptr<IPDBEnumSymbols> results;
1478
1479 uint32_t matches = 0;
1480
1481 for (auto tag : tags_to_search) {
1482 results = m_global_scope_up->findAllChildren(Type: tag);
1483 if (!results)
1484 continue;
1485
1486 while (auto result = results->getNext()) {
1487 if (max_matches > 0 && matches >= max_matches)
1488 break;
1489
1490 std::string type_name;
1491 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(Val: result.get()))
1492 type_name = enum_type->getName();
1493 else if (auto typedef_type =
1494 llvm::dyn_cast<PDBSymbolTypeTypedef>(Val: result.get()))
1495 type_name = typedef_type->getName();
1496 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(Val: result.get()))
1497 type_name = class_type->getName();
1498 else {
1499 // We're looking only for types that have names. Skip symbols, as well
1500 // as unnamed types such as arrays, pointers, etc.
1501 continue;
1502 }
1503
1504 if (!regex.Execute(string: type_name))
1505 continue;
1506
1507 // This should cause the type to get cached and stored in the `m_types`
1508 // lookup.
1509 if (!ResolveTypeUID(type_uid: result->getSymIndexId()))
1510 continue;
1511
1512 auto iter = m_types.find(Val: result->getSymIndexId());
1513 if (iter == m_types.end())
1514 continue;
1515 types.Insert(type: iter->second);
1516 ++matches;
1517 }
1518 }
1519}
1520
1521void SymbolFilePDB::FindTypes(const lldb_private::TypeQuery &query,
1522 lldb_private::TypeResults &type_results) {
1523
1524 // Make sure we haven't already searched this SymbolFile before.
1525 if (type_results.AlreadySearched(sym_file: this))
1526 return;
1527
1528 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1529
1530 std::unique_ptr<IPDBEnumSymbols> results;
1531 llvm::StringRef basename = query.GetTypeBasename().GetStringRef();
1532 if (basename.empty())
1533 return;
1534 results = m_global_scope_up->findAllChildren(Type: PDB_SymType::None);
1535 if (!results)
1536 return;
1537
1538 while (auto result = results->getNext()) {
1539
1540 switch (result->getSymTag()) {
1541 case PDB_SymType::Enum:
1542 case PDB_SymType::UDT:
1543 case PDB_SymType::Typedef:
1544 break;
1545 default:
1546 // We're looking only for types that have names. Skip symbols, as well
1547 // as unnamed types such as arrays, pointers, etc.
1548 continue;
1549 }
1550
1551 if (MSVCUndecoratedNameParser::DropScope(
1552 name: result->getRawSymbol().getName()) != basename)
1553 continue;
1554
1555 // This should cause the type to get cached and stored in the `m_types`
1556 // lookup.
1557 if (!ResolveTypeUID(type_uid: result->getSymIndexId()))
1558 continue;
1559
1560 auto iter = m_types.find(Val: result->getSymIndexId());
1561 if (iter == m_types.end())
1562 continue;
1563 // We resolved a type. Get the fully qualified name to ensure it matches.
1564 ConstString name = iter->second->GetQualifiedName();
1565 TypeQuery type_match(name.GetStringRef(), TypeQueryOptions::e_exact_match);
1566 if (query.ContextMatches(context: type_match.GetContextRef())) {
1567 type_results.InsertUnique(type_sp: iter->second);
1568 if (type_results.Done(query))
1569 return;
1570 }
1571 }
1572}
1573
1574void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1575 uint32_t type_mask,
1576 TypeCollection &type_collection) {
1577 bool can_parse = false;
1578 switch (pdb_symbol.getSymTag()) {
1579 case PDB_SymType::ArrayType:
1580 can_parse = ((type_mask & eTypeClassArray) != 0);
1581 break;
1582 case PDB_SymType::BuiltinType:
1583 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1584 break;
1585 case PDB_SymType::Enum:
1586 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1587 break;
1588 case PDB_SymType::Function:
1589 case PDB_SymType::FunctionSig:
1590 can_parse = ((type_mask & eTypeClassFunction) != 0);
1591 break;
1592 case PDB_SymType::PointerType:
1593 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1594 eTypeClassMemberPointer)) != 0);
1595 break;
1596 case PDB_SymType::Typedef:
1597 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1598 break;
1599 case PDB_SymType::UDT: {
1600 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(Val: &pdb_symbol);
1601 assert(udt);
1602 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1603 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1604 eTypeClassUnion)) != 0));
1605 } break;
1606 default:
1607 break;
1608 }
1609
1610 if (can_parse) {
1611 if (auto *type = ResolveTypeUID(type_uid: pdb_symbol.getSymIndexId())) {
1612 if (!llvm::is_contained(Range&: type_collection, Element: type))
1613 type_collection.push_back(x: type);
1614 }
1615 }
1616
1617 auto results_up = pdb_symbol.findAllChildren();
1618 while (auto symbol_up = results_up->getNext())
1619 GetTypesForPDBSymbol(pdb_symbol: *symbol_up, type_mask, type_collection);
1620}
1621
1622void SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1623 TypeClass type_mask,
1624 lldb_private::TypeList &type_list) {
1625 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1626 TypeCollection type_collection;
1627 CompileUnit *cu =
1628 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1629 if (cu) {
1630 auto compiland_up = GetPDBCompilandByUID(uid: cu->GetID());
1631 if (!compiland_up)
1632 return;
1633 GetTypesForPDBSymbol(pdb_symbol: *compiland_up, type_mask, type_collection);
1634 } else {
1635 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1636 auto cu_sp = ParseCompileUnitAtIndex(index: cu_idx);
1637 if (cu_sp) {
1638 if (auto compiland_up = GetPDBCompilandByUID(uid: cu_sp->GetID()))
1639 GetTypesForPDBSymbol(pdb_symbol: *compiland_up, type_mask, type_collection);
1640 }
1641 }
1642 }
1643
1644 for (auto type : type_collection) {
1645 type->GetForwardCompilerType();
1646 type_list.Insert(type: type->shared_from_this());
1647 }
1648}
1649
1650llvm::Expected<lldb::TypeSystemSP>
1651SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1652 auto type_system_or_err =
1653 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
1654 if (type_system_or_err) {
1655 if (auto ts = *type_system_or_err)
1656 ts->SetSymbolFile(this);
1657 }
1658 return type_system_or_err;
1659}
1660
1661PDBASTParser *SymbolFilePDB::GetPDBAstParser() {
1662 auto type_system_or_err =
1663 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
1664 if (auto err = type_system_or_err.takeError()) {
1665 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1666 "Unable to get PDB AST parser: {0}");
1667 return nullptr;
1668 }
1669
1670 auto ts = *type_system_or_err;
1671 auto *clang_type_system =
1672 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
1673 if (!clang_type_system)
1674 return nullptr;
1675
1676 return clang_type_system->GetPDBParser();
1677}
1678
1679lldb_private::CompilerDeclContext
1680SymbolFilePDB::FindNamespace(lldb_private::ConstString name,
1681 const CompilerDeclContext &parent_decl_ctx, bool) {
1682 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
1683 auto type_system_or_err =
1684 GetTypeSystemForLanguage(language: lldb::eLanguageTypeC_plus_plus);
1685 if (auto err = type_system_or_err.takeError()) {
1686 LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
1687 "Unable to find namespace {1}: {0}", name.AsCString());
1688 return CompilerDeclContext();
1689 }
1690 auto ts = *type_system_or_err;
1691 auto *clang_type_system =
1692 llvm::dyn_cast_or_null<TypeSystemClang>(Val: ts.get());
1693 if (!clang_type_system)
1694 return CompilerDeclContext();
1695
1696 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1697 if (!pdb)
1698 return CompilerDeclContext();
1699
1700 clang::DeclContext *decl_context = nullptr;
1701 if (parent_decl_ctx)
1702 decl_context = static_cast<clang::DeclContext *>(
1703 parent_decl_ctx.GetOpaqueDeclContext());
1704
1705 auto namespace_decl =
1706 pdb->FindNamespaceDecl(parent: decl_context, name: name.GetStringRef());
1707 if (!namespace_decl)
1708 return CompilerDeclContext();
1709
1710 return clang_type_system->CreateDeclContext(namespace_decl);
1711}
1712
1713IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1714
1715const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1716 return *m_session_up;
1717}
1718
1719lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1720 uint32_t index) {
1721 auto found_cu = m_comp_units.find(Val: id);
1722 if (found_cu != m_comp_units.end())
1723 return found_cu->second;
1724
1725 auto compiland_up = GetPDBCompilandByUID(uid: id);
1726 if (!compiland_up)
1727 return CompUnitSP();
1728
1729 lldb::LanguageType lang;
1730 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
1731 if (!details)
1732 lang = lldb::eLanguageTypeC_plus_plus;
1733 else
1734 lang = TranslateLanguage(lang: details->getLanguage());
1735
1736 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1737 return CompUnitSP();
1738
1739 std::string path = compiland_up->getSourceFileFullPath();
1740 if (path.empty())
1741 return CompUnitSP();
1742
1743 // Don't support optimized code for now, DebugInfoPDB does not return this
1744 // information.
1745 LazyBool optimized = eLazyBoolNo;
1746 auto cu_sp = std::make_shared<CompileUnit>(args: m_objfile_sp->GetModule(), args: nullptr,
1747 args: path.c_str(), args&: id, args&: lang, args&: optimized);
1748
1749 if (!cu_sp)
1750 return CompUnitSP();
1751
1752 m_comp_units.insert(KV: std::make_pair(x&: id, y&: cu_sp));
1753 if (index == UINT32_MAX)
1754 GetCompileUnitIndex(pdb_compiland: *compiland_up, index);
1755 lldbassert(index != UINT32_MAX);
1756 SetCompileUnitAtIndex(idx: index, cu_sp);
1757 return cu_sp;
1758}
1759
1760bool SymbolFilePDB::ParseCompileUnitLineTable(CompileUnit &comp_unit,
1761 uint32_t match_line) {
1762 auto compiland_up = GetPDBCompilandByUID(uid: comp_unit.GetID());
1763 if (!compiland_up)
1764 return false;
1765
1766 // LineEntry needs the *index* of the file into the list of support files
1767 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
1768 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1769 // to do a mapping so that we can hand out indices.
1770 llvm::DenseMap<uint32_t, uint32_t> index_map;
1771 BuildSupportFileIdToSupportFileIndexMap(pdb_compiland: *compiland_up, index_map);
1772 auto line_table = std::make_unique<LineTable>(args: &comp_unit);
1773
1774 // Find contributions to `compiland` from all source and header files.
1775 auto files = m_session_up->getSourceFilesForCompiland(Compiland: *compiland_up);
1776 if (!files)
1777 return false;
1778
1779 // For each source and header file, create a LineSequence for contributions
1780 // to the compiland from that file, and add the sequence.
1781 while (auto file = files->getNext()) {
1782 std::unique_ptr<LineSequence> sequence(
1783 line_table->CreateLineSequenceContainer());
1784 auto lines = m_session_up->findLineNumbers(Compiland: *compiland_up, File: *file);
1785 if (!lines)
1786 continue;
1787 int entry_count = lines->getChildCount();
1788
1789 uint64_t prev_addr;
1790 uint32_t prev_length;
1791 uint32_t prev_line;
1792 uint32_t prev_source_idx;
1793
1794 for (int i = 0; i < entry_count; ++i) {
1795 auto line = lines->getChildAtIndex(Index: i);
1796
1797 uint64_t lno = line->getLineNumber();
1798 uint64_t addr = line->getVirtualAddress();
1799 uint32_t length = line->getLength();
1800 uint32_t source_id = line->getSourceFileId();
1801 uint32_t col = line->getColumnNumber();
1802 uint32_t source_idx = index_map[source_id];
1803
1804 // There was a gap between the current entry and the previous entry if
1805 // the addresses don't perfectly line up.
1806 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
1807
1808 // Before inserting the current entry, insert a terminal entry at the end
1809 // of the previous entry's address range if the current entry resulted in
1810 // a gap from the previous entry.
1811 if (is_gap && ShouldAddLine(requested_line: match_line, actual_line: prev_line, addr_length: prev_length)) {
1812 line_table->AppendLineEntryToSequence(
1813 sequence: sequence.get(), file_addr: prev_addr + prev_length, line: prev_line, column: 0,
1814 file_idx: prev_source_idx, is_start_of_statement: false, is_start_of_basic_block: false, is_prologue_end: false, is_epilogue_begin: false, is_terminal_entry: true);
1815
1816 line_table->InsertSequence(sequence: sequence.get());
1817 sequence = line_table->CreateLineSequenceContainer();
1818 }
1819
1820 if (ShouldAddLine(requested_line: match_line, actual_line: lno, addr_length: length)) {
1821 bool is_statement = line->isStatement();
1822 bool is_prologue = false;
1823 bool is_epilogue = false;
1824 auto func =
1825 m_session_up->findSymbolByAddress(Address: addr, Type: PDB_SymType::Function);
1826 if (func) {
1827 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
1828 if (prologue)
1829 is_prologue = (addr == prologue->getVirtualAddress());
1830
1831 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
1832 if (epilogue)
1833 is_epilogue = (addr == epilogue->getVirtualAddress());
1834 }
1835
1836 line_table->AppendLineEntryToSequence(sequence: sequence.get(), file_addr: addr, line: lno, column: col,
1837 file_idx: source_idx, is_start_of_statement: is_statement, is_start_of_basic_block: false,
1838 is_prologue_end: is_prologue, is_epilogue_begin: is_epilogue, is_terminal_entry: false);
1839 }
1840
1841 prev_addr = addr;
1842 prev_length = length;
1843 prev_line = lno;
1844 prev_source_idx = source_idx;
1845 }
1846
1847 if (entry_count > 0 && ShouldAddLine(requested_line: match_line, actual_line: prev_line, addr_length: prev_length)) {
1848 // The end is always a terminal entry, so insert it regardless.
1849 line_table->AppendLineEntryToSequence(
1850 sequence: sequence.get(), file_addr: prev_addr + prev_length, line: prev_line, column: 0,
1851 file_idx: prev_source_idx, is_start_of_statement: false, is_start_of_basic_block: false, is_prologue_end: false, is_epilogue_begin: false, is_terminal_entry: true);
1852 }
1853
1854 line_table->InsertSequence(sequence: sequence.get());
1855 }
1856
1857 if (line_table->GetSize()) {
1858 comp_unit.SetLineTable(line_table.release());
1859 return true;
1860 }
1861 return false;
1862}
1863
1864void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
1865 const PDBSymbolCompiland &compiland,
1866 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1867 // This is a hack, but we need to convert the source id into an index into
1868 // the support files array. We don't want to do path comparisons to avoid
1869 // basename / full path issues that may or may not even be a problem, so we
1870 // use the globally unique source file identifiers. Ideally we could use the
1871 // global identifiers everywhere, but LineEntry currently assumes indices.
1872 auto source_files = m_session_up->getSourceFilesForCompiland(Compiland: compiland);
1873 if (!source_files)
1874 return;
1875
1876 int index = 0;
1877 while (auto file = source_files->getNext()) {
1878 uint32_t source_id = file->getUniqueId();
1879 index_map[source_id] = index++;
1880 }
1881}
1882
1883lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1884 const lldb_private::Address &so_addr) {
1885 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1886 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
1887 return nullptr;
1888
1889 // If it is a PDB function's vm addr, this is the first sure bet.
1890 if (auto lines =
1891 m_session_up->findLineNumbersByAddress(Address: file_vm_addr, /*Length=*/1)) {
1892 if (auto first_line = lines->getNext())
1893 return ParseCompileUnitForUID(id: first_line->getCompilandId());
1894 }
1895
1896 // Otherwise we resort to section contributions.
1897 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1898 while (auto section = sec_contribs->getNext()) {
1899 auto va = section->getVirtualAddress();
1900 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1901 return ParseCompileUnitForUID(id: section->getCompilandId());
1902 }
1903 }
1904 return nullptr;
1905}
1906
1907Mangled
1908SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
1909 Mangled mangled;
1910 auto func_name = pdb_func.getName();
1911 auto func_undecorated_name = pdb_func.getUndecoratedName();
1912 std::string func_decorated_name;
1913
1914 // Seek from public symbols for non-static function's decorated name if any.
1915 // For static functions, they don't have undecorated names and aren't exposed
1916 // in Public Symbols either.
1917 if (!func_undecorated_name.empty()) {
1918 auto result_up = m_global_scope_up->findChildren(
1919 Type: PDB_SymType::PublicSymbol, Name: func_undecorated_name,
1920 Flags: PDB_NameSearchFlags::NS_UndecoratedName);
1921 if (result_up) {
1922 while (auto symbol_up = result_up->getNext()) {
1923 // For a public symbol, it is unique.
1924 lldbassert(result_up->getChildCount() == 1);
1925 if (auto *pdb_public_sym =
1926 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1927 Val: symbol_up.get())) {
1928 if (pdb_public_sym->isFunction()) {
1929 func_decorated_name = pdb_public_sym->getName();
1930 break;
1931 }
1932 }
1933 }
1934 }
1935 }
1936 if (!func_decorated_name.empty()) {
1937 mangled.SetMangledName(ConstString(func_decorated_name));
1938
1939 // For MSVC, format of C function's decorated name depends on calling
1940 // convention. Unfortunately none of the format is recognized by current
1941 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1942 // `__purecall` is retrieved as both its decorated and undecorated name
1943 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1944 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1945 // Mangled::GetDemangledName method will fail internally and caches an
1946 // empty string as its undecorated name. So we will face a contradiction
1947 // here for the same symbol:
1948 // non-empty undecorated name from PDB
1949 // empty undecorated name from LLDB
1950 if (!func_undecorated_name.empty() && mangled.GetDemangledName().IsEmpty())
1951 mangled.SetDemangledName(ConstString(func_undecorated_name));
1952
1953 // LLDB uses several flags to control how a C++ decorated name is
1954 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1955 // yielded name could be different from what we retrieve from
1956 // PDB source unless we also apply same flags in getting undecorated
1957 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1958 if (!func_undecorated_name.empty() &&
1959 mangled.GetDemangledName() != ConstString(func_undecorated_name))
1960 mangled.SetDemangledName(ConstString(func_undecorated_name));
1961 } else if (!func_undecorated_name.empty()) {
1962 mangled.SetDemangledName(ConstString(func_undecorated_name));
1963 } else if (!func_name.empty())
1964 mangled.SetValue(ConstString(func_name));
1965
1966 return mangled;
1967}
1968
1969bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1970 const lldb_private::CompilerDeclContext &decl_ctx) {
1971 if (!decl_ctx.IsValid())
1972 return true;
1973
1974 TypeSystem *decl_ctx_type_system = decl_ctx.GetTypeSystem();
1975 if (!decl_ctx_type_system)
1976 return false;
1977 auto type_system_or_err = GetTypeSystemForLanguage(
1978 language: decl_ctx_type_system->GetMinimumLanguage(type: nullptr));
1979 if (auto err = type_system_or_err.takeError()) {
1980 LLDB_LOG_ERROR(
1981 GetLog(LLDBLog::Symbols), std::move(err),
1982 "Unable to determine if DeclContext matches this symbol file: {0}");
1983 return false;
1984 }
1985
1986 if (decl_ctx_type_system == type_system_or_err->get())
1987 return true; // The type systems match, return true
1988
1989 return false;
1990}
1991
1992uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1993 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1994 return lhs < rhs.Offset;
1995 };
1996
1997 // Cache section contributions
1998 if (m_sec_contribs.empty()) {
1999 if (auto SecContribs = m_session_up->getSectionContribs()) {
2000 while (auto SectionContrib = SecContribs->getNext()) {
2001 auto comp_id = SectionContrib->getCompilandId();
2002 if (!comp_id)
2003 continue;
2004
2005 auto sec = SectionContrib->getAddressSection();
2006 auto &sec_cs = m_sec_contribs[sec];
2007
2008 auto offset = SectionContrib->getAddressOffset();
2009 auto it = llvm::upper_bound(Range&: sec_cs, Value&: offset, C: pred_upper);
2010
2011 auto size = SectionContrib->getLength();
2012 sec_cs.insert(position: it, x: {.Offset: offset, .Size: size, .CompilandId: comp_id});
2013 }
2014 }
2015 }
2016
2017 // Check by line number
2018 if (auto Lines = data.getLineNumbers()) {
2019 if (auto FirstLine = Lines->getNext())
2020 return FirstLine->getCompilandId();
2021 }
2022
2023 // Retrieve section + offset
2024 uint32_t DataSection = data.getAddressSection();
2025 uint32_t DataOffset = data.getAddressOffset();
2026 if (DataSection == 0) {
2027 if (auto RVA = data.getRelativeVirtualAddress())
2028 m_session_up->addressForRVA(RVA, Section&: DataSection, Offset&: DataOffset);
2029 }
2030
2031 if (DataSection) {
2032 // Search by section contributions
2033 auto &sec_cs = m_sec_contribs[DataSection];
2034 auto it = llvm::upper_bound(Range&: sec_cs, Value&: DataOffset, C: pred_upper);
2035 if (it != sec_cs.begin()) {
2036 --it;
2037 if (DataOffset < it->Offset + it->Size)
2038 return it->CompilandId;
2039 }
2040 } else {
2041 // Search in lexical tree
2042 auto LexParentId = data.getLexicalParentId();
2043 while (auto LexParent = m_session_up->getSymbolById(SymbolId: LexParentId)) {
2044 if (LexParent->getSymTag() == PDB_SymType::Exe)
2045 break;
2046 if (LexParent->getSymTag() == PDB_SymType::Compiland)
2047 return LexParentId;
2048 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
2049 }
2050 }
2051
2052 return 0;
2053}
2054

source code of lldb/source/Plugins/SymbolFile/PDB/SymbolFilePDB.cpp