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