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