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/TypeMap.h"
23 #include "lldb/Utility/RegularExpression.h"
24 
25 #include "llvm/DebugInfo/PDB/GenericError.h"
26 #include "llvm/DebugInfo/PDB/IPDBDataStream.h"
27 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
28 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
29 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
30 #include "llvm/DebugInfo/PDB/IPDBTable.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
36 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
37 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
38 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
39 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
40 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
41 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
42 
43 #include "Plugins/SymbolFile/PDB/PDBASTParser.h"
44 
45 #include <regex>
46 
47 using namespace lldb;
48 using namespace lldb_private;
49 using namespace llvm::pdb;
50 
51 namespace {
52 lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
53   switch (lang) {
54   case PDB_Lang::Cpp:
55     return lldb::LanguageType::eLanguageTypeC_plus_plus;
56   case PDB_Lang::C:
57     return lldb::LanguageType::eLanguageTypeC;
58   default:
59     return lldb::LanguageType::eLanguageTypeUnknown;
60   }
61 }
62 
63 bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
64                    uint32_t addr_length) {
65   return ((requested_line == 0 || actual_line == requested_line) &&
66           addr_length > 0);
67 }
68 }
69 
70 void SymbolFilePDB::Initialize() {
71   PluginManager::RegisterPlugin(GetPluginNameStatic(),
72                                 GetPluginDescriptionStatic(), CreateInstance,
73                                 DebuggerInitialize);
74 }
75 
76 void SymbolFilePDB::Terminate() {
77   PluginManager::UnregisterPlugin(CreateInstance);
78 }
79 
80 void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
81 
82 lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
83   static ConstString g_name("pdb");
84   return g_name;
85 }
86 
87 const char *SymbolFilePDB::GetPluginDescriptionStatic() {
88   return "Microsoft PDB debug symbol file reader.";
89 }
90 
91 lldb_private::SymbolFile *
92 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
93   return new SymbolFilePDB(obj_file);
94 }
95 
96 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
97     : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
98       m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
99 
100 SymbolFilePDB::~SymbolFilePDB() {}
101 
102 uint32_t SymbolFilePDB::CalculateAbilities() {
103   uint32_t abilities = 0;
104   if (!m_obj_file)
105     return 0;
106 
107   if (!m_session_up) {
108     // Lazily load and match the PDB file, but only do this once.
109     std::string exePath = m_obj_file->GetFileSpec().GetPath();
110     auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
111                                 m_session_up);
112     if (error) {
113       llvm::consumeError(std::move(error));
114       auto module_sp = m_obj_file->GetModule();
115       if (!module_sp)
116         return 0;
117       // See if any symbol file is specified through `--symfile` option.
118       FileSpec symfile = module_sp->GetSymbolFileFileSpec();
119       if (!symfile)
120         return 0;
121       error = loadDataForPDB(PDB_ReaderType::DIA,
122                              llvm::StringRef(symfile.GetPath()),
123                              m_session_up);
124       if (error) {
125         llvm::consumeError(std::move(error));
126         return 0;
127       }
128     }
129   }
130   if (!m_session_up.get())
131     return 0;
132 
133   auto enum_tables_up = m_session_up->getEnumTables();
134   if (!enum_tables_up)
135     return 0;
136   while (auto table_up = enum_tables_up->getNext()) {
137     if (table_up->getItemCount() == 0)
138       continue;
139     auto type = table_up->getTableType();
140     switch (type) {
141     case PDB_TableType::Symbols:
142       // This table represents a store of symbols with types listed in
143       // PDBSym_Type
144       abilities |= (CompileUnits | Functions | Blocks |
145                     GlobalVariables | LocalVariables | VariableTypes);
146       break;
147     case PDB_TableType::LineNumbers:
148       abilities |= LineTables;
149       break;
150     default: break;
151     }
152   }
153   return abilities;
154 }
155 
156 void SymbolFilePDB::InitializeObject() {
157   lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
158   lldbassert(obj_load_address &&
159              obj_load_address != LLDB_INVALID_ADDRESS);
160   m_session_up->setLoadAddress(obj_load_address);
161   if (!m_global_scope_up)
162     m_global_scope_up = m_session_up->getGlobalScope();
163   lldbassert(m_global_scope_up.get());
164 
165   TypeSystem *type_system =
166       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
167   ClangASTContext *clang_type_system =
168       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
169   lldbassert(clang_type_system);
170   m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
171       type_system, clang_type_system->GetTranslationUnitDecl());
172 }
173 
174 uint32_t SymbolFilePDB::GetNumCompileUnits() {
175   if (m_cached_compile_unit_count == 0) {
176     auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
177     if (!compilands)
178       return 0;
179 
180     // The linker could link *.dll (compiland language = LINK), or import
181     // *.dll. For example, a compiland with name `Import:KERNEL32.dll`
182     // could be found as a child of the global scope (PDB executable).
183     // Usually, such compilands contain `thunk` symbols in which we are not
184     // interested for now. However we still count them in the compiland list.
185     // If we perform any compiland related activity, like finding symbols
186     // through llvm::pdb::IPDBSession methods, such compilands will all be
187     // searched automatically no matter whether we include them or not.
188     m_cached_compile_unit_count = compilands->getChildCount();
189 
190     // The linker can inject an additional "dummy" compilation unit into the
191     // PDB. Ignore this special compile unit for our purposes, if it is there.
192     // It is always the last one.
193     auto last_compiland_up =
194         compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
195     lldbassert(last_compiland_up.get());
196     std::string name = last_compiland_up->getName();
197     if (name == "* Linker *")
198       --m_cached_compile_unit_count;
199   }
200   return m_cached_compile_unit_count;
201 }
202 
203 void SymbolFilePDB::GetCompileUnitIndex(
204     const llvm::pdb::PDBSymbolCompiland *pdb_compiland,
205     uint32_t &index) {
206   if (!pdb_compiland)
207     return;
208 
209   auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
210   if (!results_up)
211     return;
212   auto uid = pdb_compiland->getSymIndexId();
213   for (int cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
214     auto compiland_up = results_up->getChildAtIndex(cu_idx);
215     if (!compiland_up)
216       continue;
217     if (compiland_up->getSymIndexId() == uid) {
218       index = cu_idx;
219       return;
220     }
221   }
222   index = UINT32_MAX;
223   return;
224 }
225 
226 std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
227 SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
228   return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
229 }
230 
231 lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
232   if (index >= GetNumCompileUnits())
233     return CompUnitSP();
234 
235   // Assuming we always retrieve same compilands listed in same order through
236   // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
237   // compile unit makes no sense.
238   auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
239   if (!results)
240     return CompUnitSP();
241   auto compiland_up = results->getChildAtIndex(index);
242   if (!compiland_up)
243     return CompUnitSP();
244   return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
245 }
246 
247 lldb::LanguageType
248 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
249   // What fields should I expect to be filled out on the SymbolContext?  Is it
250   // safe to assume that `sc.comp_unit` is valid?
251   if (!sc.comp_unit)
252     return lldb::eLanguageTypeUnknown;
253 
254   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
255   if (!compiland_up)
256     return lldb::eLanguageTypeUnknown;
257   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
258   if (!details)
259     return lldb::eLanguageTypeUnknown;
260   return TranslateLanguage(details->getLanguage());
261 }
262 
263 size_t SymbolFilePDB::ParseCompileUnitFunctions(
264     const lldb_private::SymbolContext &sc) {
265   // TODO: Implement this
266   return size_t();
267 }
268 
269 bool SymbolFilePDB::ParseCompileUnitLineTable(
270     const lldb_private::SymbolContext &sc) {
271   lldbassert(sc.comp_unit);
272   if (sc.comp_unit->GetLineTable())
273     return true;
274   return ParseCompileUnitLineTable(sc, 0);
275 }
276 
277 bool SymbolFilePDB::ParseCompileUnitDebugMacros(
278     const lldb_private::SymbolContext &sc) {
279   // PDB doesn't contain information about macros
280   return false;
281 }
282 
283 bool SymbolFilePDB::ParseCompileUnitSupportFiles(
284     const lldb_private::SymbolContext &sc,
285     lldb_private::FileSpecList &support_files) {
286   lldbassert(sc.comp_unit);
287 
288   // In theory this is unnecessary work for us, because all of this information
289   // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
290   // second time seems like a waste.  Unfortunately, there's no good way around
291   // this short of a moderate refactor since SymbolVendor depends on being able
292   // to cache this list.
293   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
294   if (!compiland_up)
295     return false;
296   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
297   if (!files || files->getChildCount() == 0)
298     return false;
299 
300   while (auto file = files->getNext()) {
301     FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
302     support_files.AppendIfUnique(spec);
303   }
304   return true;
305 }
306 
307 bool SymbolFilePDB::ParseImportedModules(
308     const lldb_private::SymbolContext &sc,
309     std::vector<lldb_private::ConstString> &imported_modules) {
310   // PDB does not yet support module debug info
311   return false;
312 }
313 
314 size_t
315 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
316   // TODO: Implement this
317   return size_t();
318 }
319 
320 size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
321   // TODO: Implement this
322   return size_t();
323 }
324 
325 size_t
326 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
327   // TODO: Implement this
328   return size_t();
329 }
330 
331 lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
332   auto find_result = m_types.find(type_uid);
333   if (find_result != m_types.end())
334     return find_result->second.get();
335 
336   TypeSystem *type_system =
337       GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
338   ClangASTContext *clang_type_system =
339       llvm::dyn_cast_or_null<ClangASTContext>(type_system);
340   if (!clang_type_system)
341     return nullptr;
342   PDBASTParser *pdb =
343       llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
344   if (!pdb)
345     return nullptr;
346 
347   auto pdb_type = m_session_up->getSymbolById(type_uid);
348   if (pdb_type == nullptr)
349     return nullptr;
350 
351   lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
352   if (result.get())
353     m_types.insert(std::make_pair(type_uid, result));
354   return result.get();
355 }
356 
357 bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
358   // TODO: Implement this
359   return false;
360 }
361 
362 lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
363   return lldb_private::CompilerDecl();
364 }
365 
366 lldb_private::CompilerDeclContext
367 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
368   // PDB always uses the translation unit decl context for everything.  We can
369   // improve this later but it's not easy because PDB doesn't provide a high
370   // enough level of type fidelity in this area.
371   return *m_tu_decl_ctx_up;
372 }
373 
374 lldb_private::CompilerDeclContext
375 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
376   return *m_tu_decl_ctx_up;
377 }
378 
379 void SymbolFilePDB::ParseDeclsForContext(
380     lldb_private::CompilerDeclContext decl_ctx) {}
381 
382 uint32_t
383 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
384                                     uint32_t resolve_scope,
385                                     lldb_private::SymbolContext &sc) {
386   return uint32_t();
387 }
388 
389 std::string SymbolFilePDB::GetSourceFileNameForPDBCompiland(
390     const PDBSymbolCompiland *pdb_compiland) {
391   if (!pdb_compiland)
392     return std::string();
393 
394   std::string source_file_name;
395   // `getSourceFileName` returns the basename of the original source file
396   // used to generate this compiland.  It does not return the full path.
397   // Currently the only way to get that is to do a basename lookup to get the
398   // IPDBSourceFile, but this is ambiguous in the case of two source files
399   // with the same name contributing to the same compiland. This is an edge
400   // case that we ignore for now, although we need to a long-term solution.
401   std::string file_name = pdb_compiland->getSourceFileName();
402   if (!file_name.empty()) {
403     auto one_src_file_up =
404       m_session_up->findOneSourceFile(pdb_compiland, file_name,
405                                       PDB_NameSearchFlags::NS_CaseInsensitive);
406     if (one_src_file_up)
407       source_file_name = one_src_file_up->getFileName();
408   }
409   // For some reason, source file name could be empty, so we will walk through
410   // all source files of this compiland, and determine the right source file
411   // if any that is used to generate this compiland based on language
412   // indicated in compilanddetails language field.
413   if (!source_file_name.empty())
414     return source_file_name;
415 
416   auto details_up = pdb_compiland->findOneChild<PDBSymbolCompilandDetails>();
417   PDB_Lang pdb_lang = details_up ? details_up->getLanguage() : PDB_Lang::Cpp;
418   auto src_files_up =
419     m_session_up->getSourceFilesForCompiland(*pdb_compiland);
420   if (src_files_up) {
421     while (auto file_up = src_files_up->getNext()) {
422       FileSpec file_spec(file_up->getFileName(), false,
423                          FileSpec::ePathSyntaxWindows);
424       auto file_extension = file_spec.GetFileNameExtension();
425       if (pdb_lang == PDB_Lang::Cpp || pdb_lang == PDB_Lang::C) {
426         static const char* exts[] = { "cpp", "c", "cc", "cxx" };
427         if (llvm::is_contained(exts, file_extension.GetStringRef().lower()))
428           source_file_name = file_up->getFileName();
429         break;
430       } else if (pdb_lang == PDB_Lang::Masm &&
431                  ConstString::Compare(file_extension, ConstString("ASM"),
432                                       false) == 0) {
433         source_file_name = file_up->getFileName();
434         break;
435       }
436     }
437   }
438   return source_file_name;
439 }
440 
441 uint32_t SymbolFilePDB::ResolveSymbolContext(
442     const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
443     uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
444   const size_t old_size = sc_list.GetSize();
445   if (resolve_scope & lldb::eSymbolContextCompUnit) {
446     // Locate all compilation units with line numbers referencing the specified
447     // file.  For example, if `file_spec` is <vector>, then this should return
448     // all source files and header files that reference <vector>, either
449     // directly or indirectly.
450     auto compilands = m_session_up->findCompilandsForSourceFile(
451         file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
452 
453     if (!compilands)
454       return 0;
455 
456     // For each one, either find its previously parsed data or parse it afresh
457     // and add it to the symbol context list.
458     while (auto compiland = compilands->getNext()) {
459       // If we're not checking inlines, then don't add line information for this
460       // file unless the FileSpec matches.
461       if (!check_inlines) {
462         // `getSourceFileName` returns the basename of the original source file
463         // used to generate this compiland.  It does not return the full path.
464         // Currently the only way to get that is to do a basename lookup to get
465         // the IPDBSourceFile, but this is ambiguous in the case of two source
466         // files with the same name contributing to the same compiland.  This is
467         // a moderately extreme edge case, so we consider this OK for now,
468         // although we need to find a long-term solution.
469         std::string source_file =
470             GetSourceFileNameForPDBCompiland(compiland.get());
471         if (source_file.empty())
472           continue;
473         FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
474         bool need_full_match = !file_spec.GetDirectory().IsEmpty();
475         if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
476           continue;
477       }
478 
479       SymbolContext sc;
480       auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
481       if (!cu.get())
482         continue;
483       sc.comp_unit = cu.get();
484       sc.module_sp = cu->GetModule();
485       sc_list.Append(sc);
486 
487       // If we were asked to resolve line entries, add all entries to the line
488       // table that match the requested line (or all lines if `line` == 0).
489       if (resolve_scope & lldb::eSymbolContextLineEntry)
490         ParseCompileUnitLineTable(sc, line);
491     }
492   }
493   return sc_list.GetSize() - old_size;
494 }
495 
496 uint32_t SymbolFilePDB::FindGlobalVariables(
497     const lldb_private::ConstString &name,
498     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
499     uint32_t max_matches, lldb_private::VariableList &variables) {
500   return uint32_t();
501 }
502 
503 uint32_t
504 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
505                                    bool append, uint32_t max_matches,
506                                    lldb_private::VariableList &variables) {
507   return uint32_t();
508 }
509 
510 uint32_t SymbolFilePDB::FindFunctions(
511     const lldb_private::ConstString &name,
512     const lldb_private::CompilerDeclContext *parent_decl_ctx,
513     uint32_t name_type_mask, bool include_inlines, bool append,
514     lldb_private::SymbolContextList &sc_list) {
515   return uint32_t();
516 }
517 
518 uint32_t
519 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
520                              bool include_inlines, bool append,
521                              lldb_private::SymbolContextList &sc_list) {
522   return uint32_t();
523 }
524 
525 void SymbolFilePDB::GetMangledNamesForFunction(
526     const std::string &scope_qualified_name,
527     std::vector<lldb_private::ConstString> &mangled_names) {}
528 
529 uint32_t SymbolFilePDB::FindTypes(
530     const lldb_private::SymbolContext &sc,
531     const lldb_private::ConstString &name,
532     const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
533     uint32_t max_matches,
534     llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
535     lldb_private::TypeMap &types) {
536   if (!append)
537     types.Clear();
538   if (!name)
539     return 0;
540 
541   searched_symbol_files.clear();
542   searched_symbol_files.insert(this);
543 
544   std::string name_str = name.AsCString();
545 
546   // There is an assumption 'name' is not a regex
547   FindTypesByName(name_str, max_matches, types);
548 
549   return types.GetSize();
550 }
551 
552 void
553 SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression &regex,
554                                 uint32_t max_matches,
555                                 lldb_private::TypeMap &types) {
556   // When searching by regex, we need to go out of our way to limit the search
557   // space as much as possible since this searches EVERYTHING in the PDB,
558   // manually doing regex comparisons.  PDB library isn't optimized for regex
559   // searches or searches across multiple symbol types at the same time, so the
560   // best we can do is to search enums, then typedefs, then classes one by one,
561   // and do a regex comparison against each of them.
562   PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
563                                   PDB_SymType::UDT};
564   std::unique_ptr<IPDBEnumSymbols> results;
565 
566   uint32_t matches = 0;
567 
568   for (auto tag : tags_to_search) {
569     results = m_global_scope_up->findAllChildren(tag);
570     if (!results)
571       continue;
572 
573     while (auto result = results->getNext()) {
574       if (max_matches > 0 && matches >= max_matches)
575         break;
576 
577       std::string type_name;
578       if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
579         type_name = enum_type->getName();
580       else if (auto typedef_type =
581                    llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
582         type_name = typedef_type->getName();
583       else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
584         type_name = class_type->getName();
585       else {
586         // We're looking only for types that have names.  Skip symbols, as well
587         // as unnamed types such as arrays, pointers, etc.
588         continue;
589       }
590 
591       if (!regex.Execute(type_name))
592         continue;
593 
594       // This should cause the type to get cached and stored in the `m_types`
595       // lookup.
596       if (!ResolveTypeUID(result->getSymIndexId()))
597         continue;
598 
599       auto iter = m_types.find(result->getSymIndexId());
600       if (iter == m_types.end())
601         continue;
602       types.Insert(iter->second);
603       ++matches;
604     }
605   }
606 }
607 
608 void SymbolFilePDB::FindTypesByName(const std::string &name,
609                                     uint32_t max_matches,
610                                     lldb_private::TypeMap &types) {
611   std::unique_ptr<IPDBEnumSymbols> results;
612   results = m_global_scope_up->findChildren(PDB_SymType::None, name,
613                                             PDB_NameSearchFlags::NS_Default);
614   if (!results)
615     return;
616 
617   uint32_t matches = 0;
618 
619   while (auto result = results->getNext()) {
620     if (max_matches > 0 && matches >= max_matches)
621       break;
622     switch (result->getSymTag()) {
623     case PDB_SymType::Enum:
624     case PDB_SymType::UDT:
625     case PDB_SymType::Typedef:
626       break;
627     default:
628       // We're looking only for types that have names.  Skip symbols, as well as
629       // unnamed types such as arrays, pointers, etc.
630       continue;
631     }
632 
633     // This should cause the type to get cached and stored in the `m_types`
634     // lookup.
635     if (!ResolveTypeUID(result->getSymIndexId()))
636       continue;
637 
638     auto iter = m_types.find(result->getSymIndexId());
639     if (iter == m_types.end())
640       continue;
641     types.Insert(iter->second);
642     ++matches;
643   }
644 }
645 
646 size_t SymbolFilePDB::FindTypes(
647     const std::vector<lldb_private::CompilerContext> &contexts, bool append,
648     lldb_private::TypeMap &types) {
649   return 0;
650 }
651 
652 lldb_private::TypeList *SymbolFilePDB::GetTypeList() { return nullptr; }
653 
654 size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
655                                uint32_t type_mask,
656                                lldb_private::TypeList &type_list) {
657   return size_t();
658 }
659 
660 lldb_private::TypeSystem *
661 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
662   auto type_system =
663       m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
664   if (type_system)
665     type_system->SetSymbolFile(this);
666   return type_system;
667 }
668 
669 lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
670     const lldb_private::SymbolContext &sc,
671     const lldb_private::ConstString &name,
672     const lldb_private::CompilerDeclContext *parent_decl_ctx) {
673   return lldb_private::CompilerDeclContext();
674 }
675 
676 lldb_private::ConstString SymbolFilePDB::GetPluginName() {
677   static ConstString g_name("pdb");
678   return g_name;
679 }
680 
681 uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
682 
683 IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
684 
685 const IPDBSession &SymbolFilePDB::GetPDBSession() const {
686   return *m_session_up;
687 }
688 
689 lldb::CompUnitSP
690 SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) {
691   auto found_cu = m_comp_units.find(id);
692   if (found_cu != m_comp_units.end())
693     return found_cu->second;
694 
695   auto compiland_up = GetPDBCompilandByUID(id);
696   if (!compiland_up)
697     return CompUnitSP();
698   std::string path = GetSourceFileNameForPDBCompiland(compiland_up.get());
699   if (path.empty())
700     return CompUnitSP();
701 
702   lldb::LanguageType lang;
703   auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
704   if (!details)
705     lang = lldb::eLanguageTypeC_plus_plus;
706   else
707     lang = TranslateLanguage(details->getLanguage());
708 
709   // Don't support optimized code for now, DebugInfoPDB does not return this
710   // information.
711   LazyBool optimized = eLazyBoolNo;
712   auto cu_sp = std::make_shared<CompileUnit>(
713       m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
714 
715   if (!cu_sp)
716     return CompUnitSP();
717 
718   m_comp_units.insert(std::make_pair(id, cu_sp));
719   if (index == UINT32_MAX)
720     GetCompileUnitIndex(compiland_up.get(), index);
721   lldbassert(index != UINT32_MAX);
722   m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
723       index, cu_sp);
724   return cu_sp;
725 }
726 
727 bool SymbolFilePDB::ParseCompileUnitLineTable(
728     const lldb_private::SymbolContext &sc, uint32_t match_line) {
729   lldbassert(sc.comp_unit);
730 
731   auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
732   if (!compiland_up)
733     return false;
734 
735   // LineEntry needs the *index* of the file into the list of support files
736   // returned by ParseCompileUnitSupportFiles.  But the underlying SDK gives us
737   // a globally unique idenfitifier in the namespace of the PDB.  So, we have to
738   // do a mapping so that we can hand out indices.
739   llvm::DenseMap<uint32_t, uint32_t> index_map;
740   BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
741   auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
742 
743   // Find contributions to `compiland` from all source and header files.
744   std::string path = sc.comp_unit->GetPath();
745   auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
746   if (!files)
747     return false;
748 
749   // For each source and header file, create a LineSequence for contributions to
750   // the compiland from that file, and add the sequence.
751   while (auto file = files->getNext()) {
752     std::unique_ptr<LineSequence> sequence(
753         line_table->CreateLineSequenceContainer());
754     auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
755     if (!lines)
756       continue;
757     int entry_count = lines->getChildCount();
758 
759     uint64_t prev_addr;
760     uint32_t prev_length;
761     uint32_t prev_line;
762     uint32_t prev_source_idx;
763 
764     for (int i = 0; i < entry_count; ++i) {
765       auto line = lines->getChildAtIndex(i);
766 
767       uint64_t lno = line->getLineNumber();
768       uint64_t addr = line->getVirtualAddress();
769       uint32_t length = line->getLength();
770       uint32_t source_id = line->getSourceFileId();
771       uint32_t col = line->getColumnNumber();
772       uint32_t source_idx = index_map[source_id];
773 
774       // There was a gap between the current entry and the previous entry if the
775       // addresses don't perfectly line up.
776       bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
777 
778       // Before inserting the current entry, insert a terminal entry at the end
779       // of the previous entry's address range if the current entry resulted in
780       // a gap from the previous entry.
781       if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
782         line_table->AppendLineEntryToSequence(
783             sequence.get(), prev_addr + prev_length, prev_line, 0,
784             prev_source_idx, false, false, false, false, true);
785       }
786 
787       if (ShouldAddLine(match_line, lno, length)) {
788         bool is_statement = line->isStatement();
789         bool is_prologue = false;
790         bool is_epilogue = false;
791         auto func =
792             m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
793         if (func) {
794           auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
795           if (prologue)
796             is_prologue = (addr == prologue->getVirtualAddress());
797 
798           auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
799           if (epilogue)
800             is_epilogue = (addr == epilogue->getVirtualAddress());
801         }
802 
803         line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
804                                               source_idx, is_statement, false,
805                                               is_prologue, is_epilogue, false);
806       }
807 
808       prev_addr = addr;
809       prev_length = length;
810       prev_line = lno;
811       prev_source_idx = source_idx;
812     }
813 
814     if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
815       // The end is always a terminal entry, so insert it regardless.
816       line_table->AppendLineEntryToSequence(
817           sequence.get(), prev_addr + prev_length, prev_line, 0,
818           prev_source_idx, false, false, false, false, true);
819     }
820 
821     line_table->InsertSequence(sequence.release());
822   }
823 
824   if (line_table->GetSize()) {
825     sc.comp_unit->SetLineTable(line_table.release());
826     return true;
827   }
828   return false;
829 }
830 
831 void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
832     const PDBSymbolCompiland &compiland,
833     llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
834   // This is a hack, but we need to convert the source id into an index into the
835   // support files array.  We don't want to do path comparisons to avoid
836   // basename / full path issues that may or may not even be a problem, so we
837   // use the globally unique source file identifiers.  Ideally we could use the
838   // global identifiers everywhere, but LineEntry currently assumes indices.
839   auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
840   if (!source_files)
841     return;
842   int index = 0;
843 
844   while (auto file = source_files->getNext()) {
845     uint32_t source_id = file->getUniqueId();
846     index_map[source_id] = index++;
847   }
848 }
849