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