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 "lldb/Core/Module.h"
13 #include "lldb/Core/PluginManager.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/LineTable.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/SymbolContext.h"
18 
19 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
20 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
21 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
29 
30 using namespace lldb_private;
31 
32 namespace
33 {
34     lldb::LanguageType TranslateLanguage(llvm::PDB_Lang lang)
35     {
36         switch (lang)
37         {
38         case llvm::PDB_Lang::Cpp:
39             return lldb::LanguageType::eLanguageTypeC_plus_plus;
40         case llvm::PDB_Lang::C:
41             return lldb::LanguageType::eLanguageTypeC;
42         default:
43             return lldb::LanguageType::eLanguageTypeUnknown;
44         }
45     }
46 
47     bool
48     ShouldAddLine(uint32_t requested_line, uint32_t actual_line, uint32_t addr_length)
49     {
50         return ((requested_line == 0 || actual_line == requested_line) && addr_length > 0);
51     }
52 }
53 
54 void
55 SymbolFilePDB::Initialize()
56 {
57     PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
58                                   DebuggerInitialize);
59 }
60 
61 void
62 SymbolFilePDB::Terminate()
63 {
64     PluginManager::UnregisterPlugin(CreateInstance);
65 }
66 
67 void
68 SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger)
69 {
70 }
71 
72 lldb_private::ConstString
73 SymbolFilePDB::GetPluginNameStatic()
74 {
75     static ConstString g_name("pdb");
76     return g_name;
77 }
78 
79 const char *
80 SymbolFilePDB::GetPluginDescriptionStatic()
81 {
82     return "Microsoft PDB debug symbol file reader.";
83 }
84 
85 lldb_private::SymbolFile *
86 SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file)
87 {
88     return new SymbolFilePDB(obj_file);
89 }
90 
91 SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
92     : SymbolFile(object_file), m_cached_compile_unit_count(0)
93 {
94 }
95 
96 SymbolFilePDB::~SymbolFilePDB()
97 {
98 }
99 
100 uint32_t
101 SymbolFilePDB::CalculateAbilities()
102 {
103     if (!m_session_up)
104     {
105         // Lazily load and match the PDB file, but only do this once.
106         std::string exePath = m_obj_file->GetFileSpec().GetPath();
107         auto error = llvm::loadDataForEXE(llvm::PDB_ReaderType::DIA, llvm::StringRef(exePath), m_session_up);
108         if (error != llvm::PDB_ErrorCode::Success)
109             return 0;
110     }
111     return CompileUnits | LineTables;
112 }
113 
114 void
115 SymbolFilePDB::InitializeObject()
116 {
117     lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
118     m_session_up->setLoadAddress(obj_load_address);
119 }
120 
121 uint32_t
122 SymbolFilePDB::GetNumCompileUnits()
123 {
124     if (m_cached_compile_unit_count == 0)
125     {
126         auto global = m_session_up->getGlobalScope();
127         auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>();
128         m_cached_compile_unit_count = compilands->getChildCount();
129 
130         // The linker can inject an additional "dummy" compilation unit into the PDB.
131         // Ignore this special compile unit for our purposes, if it is there.  It is
132         // always the last one.
133         auto last_cu = compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
134         std::string name = last_cu->getName();
135         if (name == "* Linker *")
136             --m_cached_compile_unit_count;
137     }
138     return m_cached_compile_unit_count;
139 }
140 
141 lldb::CompUnitSP
142 SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index)
143 {
144     auto global = m_session_up->getGlobalScope();
145     auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>();
146     auto cu = compilands->getChildAtIndex(index);
147 
148     uint32_t id = cu->getSymIndexId();
149 
150     return ParseCompileUnitForSymIndex(id);
151 }
152 
153 lldb::LanguageType
154 SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc)
155 {
156     // What fields should I expect to be filled out on the SymbolContext?  Is it
157     // safe to assume that `sc.comp_unit` is valid?
158     if (!sc.comp_unit)
159         return lldb::eLanguageTypeUnknown;
160 
161     auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID());
162     if (!cu)
163         return lldb::eLanguageTypeUnknown;
164     auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>();
165     if (!details)
166         return lldb::eLanguageTypeUnknown;
167     return TranslateLanguage(details->getLanguage());
168 }
169 
170 size_t
171 SymbolFilePDB::ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc)
172 {
173     // TODO: Implement this
174     return size_t();
175 }
176 
177 bool
178 SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc)
179 {
180     return ParseCompileUnitLineTable(sc, 0);
181 }
182 
183 bool
184 SymbolFilePDB::ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc)
185 {
186     // PDB doesn't contain information about macros
187     return false;
188 }
189 
190 bool
191 SymbolFilePDB::ParseCompileUnitSupportFiles(const lldb_private::SymbolContext &sc,
192                                             lldb_private::FileSpecList &support_files)
193 {
194     if (!sc.comp_unit)
195         return false;
196 
197     // In theory this is unnecessary work for us, because all of this information is easily
198     // (and quickly) accessible from DebugInfoPDB, so caching it a second time seems like a waste.
199     // Unfortunately, there's no good way around this short of a moderate refactor, since SymbolVendor
200     // depends on being able to cache this list.
201     auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID());
202     if (!cu)
203         return false;
204     auto files = m_session_up->getSourceFilesForCompiland(*cu);
205     if (!files || files->getChildCount() == 0)
206         return false;
207 
208     while (auto file = files->getNext())
209     {
210         FileSpec spec(file->getFileName(), false);
211         support_files.Append(spec);
212     }
213     return true;
214 }
215 
216 bool
217 SymbolFilePDB::ParseImportedModules(const lldb_private::SymbolContext &sc,
218                                     std::vector<lldb_private::ConstString> &imported_modules)
219 {
220     // PDB does not yet support module debug info
221     return false;
222 }
223 
224 size_t
225 SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc)
226 {
227     // TODO: Implement this
228     return size_t();
229 }
230 
231 size_t
232 SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc)
233 {
234     // TODO: Implement this
235     return size_t();
236 }
237 
238 size_t
239 SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc)
240 {
241     // TODO: Implement this
242     return size_t();
243 }
244 
245 lldb_private::Type *
246 SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid)
247 {
248     return nullptr;
249 }
250 
251 bool
252 SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type)
253 {
254     // TODO: Implement this
255     return false;
256 }
257 
258 lldb_private::CompilerDecl
259 SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid)
260 {
261     return lldb_private::CompilerDecl();
262 }
263 
264 lldb_private::CompilerDeclContext
265 SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid)
266 {
267     return lldb_private::CompilerDeclContext();
268 }
269 
270 lldb_private::CompilerDeclContext
271 SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid)
272 {
273     return lldb_private::CompilerDeclContext();
274 }
275 
276 void
277 SymbolFilePDB::ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)
278 {
279 }
280 
281 uint32_t
282 SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, uint32_t resolve_scope,
283                                     lldb_private::SymbolContext &sc)
284 {
285     return uint32_t();
286 }
287 
288 uint32_t
289 SymbolFilePDB::ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
290                                     uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list)
291 {
292     if (resolve_scope & lldb::eSymbolContextCompUnit)
293     {
294         // Locate all compilation units with line numbers referencing the specified file.  For example, if
295         // `file_spec` is <vector>, then this should return all source files and header files that reference
296         // <vector>, either directly or indirectly.
297         auto compilands =
298             m_session_up->findCompilandsForSourceFile(file_spec.GetPath(), llvm::PDB_NameSearchFlags::NS_CaseInsensitive);
299 
300         // For each one, either find get its previously parsed data, or parse it afresh and add it to
301         // the symbol context list.
302         while (auto compiland = compilands->getNext())
303         {
304             // If we're not checking inlines, then don't add line information for this file unless the FileSpec
305             // matches.
306             if (!check_inlines)
307             {
308                 // `getSourceFileName` returns the basename of the original source file used to generate this compiland.
309                 // It does not return the full path.  Currently the only way to get that is to do a basename lookup to
310                 // get the IPDBSourceFile, but this is ambiguous in the case of two source files with the same name
311                 // contributing to the same compiland.  This is a moderately extreme edge case, so we consider this ok
312                 // for now, although we need to find a long term solution.
313                 std::string source_file = compiland->getSourceFileName();
314                 auto pdb_file = m_session_up->findOneSourceFile(compiland.get(), source_file,
315                                                                 llvm::PDB_NameSearchFlags::NS_CaseInsensitive);
316                 source_file = pdb_file->getFileName();
317                 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
318                 if (!file_spec.FileEquals(this_spec))
319                     continue;
320             }
321 
322             SymbolContext sc;
323             auto cu = ParseCompileUnitForSymIndex(compiland->getSymIndexId());
324             sc.comp_unit = cu.get();
325             sc.module_sp = cu->GetModule();
326             sc_list.Append(sc);
327 
328             // If we were asked to resolve line entries, add all entries to the line table that match the requested
329             // line (or all lines if `line` == 0)
330             if (resolve_scope & lldb::eSymbolContextLineEntry)
331                 ParseCompileUnitLineTable(sc, line);
332         }
333     }
334     return sc_list.GetSize();
335 }
336 
337 uint32_t
338 SymbolFilePDB::FindGlobalVariables(const lldb_private::ConstString &name,
339                                    const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
340                                    uint32_t max_matches, lldb_private::VariableList &variables)
341 {
342     return uint32_t();
343 }
344 
345 uint32_t
346 SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex, bool append, uint32_t max_matches,
347                                    lldb_private::VariableList &variables)
348 {
349     return uint32_t();
350 }
351 
352 uint32_t
353 SymbolFilePDB::FindFunctions(const lldb_private::ConstString &name,
354                              const lldb_private::CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask,
355                              bool include_inlines, bool append, lldb_private::SymbolContextList &sc_list)
356 {
357     return uint32_t();
358 }
359 
360 uint32_t
361 SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex, bool include_inlines, bool append,
362                              lldb_private::SymbolContextList &sc_list)
363 {
364     return uint32_t();
365 }
366 
367 void
368 SymbolFilePDB::GetMangledNamesForFunction(const std::string &scope_qualified_name,
369                                           std::vector<lldb_private::ConstString> &mangled_names)
370 {
371 }
372 
373 uint32_t
374 SymbolFilePDB::FindTypes(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name,
375                          const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches,
376                          llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
377                          lldb_private::TypeMap &types)
378 {
379     return uint32_t();
380 }
381 
382 size_t
383 SymbolFilePDB::FindTypes(const std::vector<lldb_private::CompilerContext> &context, bool append,
384                          lldb_private::TypeMap &types)
385 {
386     return size_t();
387 }
388 
389 lldb_private::TypeList *
390 SymbolFilePDB::GetTypeList()
391 {
392     return nullptr;
393 }
394 
395 size_t
396 SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, uint32_t type_mask,
397                         lldb_private::TypeList &type_list)
398 {
399     return size_t();
400 }
401 
402 lldb_private::TypeSystem *
403 SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language)
404 {
405     auto type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
406     if (type_system)
407         type_system->SetSymbolFile(this);
408     return type_system;
409 }
410 
411 lldb_private::CompilerDeclContext
412 SymbolFilePDB::FindNamespace(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name,
413                              const lldb_private::CompilerDeclContext *parent_decl_ctx)
414 {
415     return lldb_private::CompilerDeclContext();
416 }
417 
418 lldb_private::ConstString
419 SymbolFilePDB::GetPluginName()
420 {
421     static ConstString g_name("pdb");
422     return g_name;
423 }
424 
425 uint32_t
426 SymbolFilePDB::GetPluginVersion()
427 {
428     return 1;
429 }
430 
431 lldb::CompUnitSP
432 SymbolFilePDB::ParseCompileUnitForSymIndex(uint32_t id)
433 {
434     auto found_cu = m_comp_units.find(id);
435     if (found_cu != m_comp_units.end())
436         return found_cu->second;
437 
438     auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(id);
439 
440     // `getSourceFileName` returns the basename of the original source file used to generate this compiland.  It does
441     // not return the full path.  Currently the only way to get that is to do a basename lookup to get the
442     // IPDBSourceFile, but this is ambiguous in the case of two source files with the same name contributing to the
443     // same compiland. This is a moderately extreme edge case, so we consider this ok for now, although we need to find
444     // a long term solution.
445     auto file = m_session_up->findOneSourceFile(cu.get(), cu->getSourceFileName(),
446                                                 llvm::PDB_NameSearchFlags::NS_CaseInsensitive);
447     std::string path = file->getFileName();
448 
449     lldb::LanguageType lang;
450     auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>();
451     if (!details)
452         lang = lldb::eLanguageTypeC_plus_plus;
453     else
454         lang = TranslateLanguage(details->getLanguage());
455 
456     // Don't support optimized code for now, DebugInfoPDB does not return this information.
457     bool optimized = false;
458     auto result = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
459     m_comp_units.insert(std::make_pair(id, result));
460     return result;
461 }
462 
463 bool
464 SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc, uint32_t match_line)
465 {
466     auto global = m_session_up->getGlobalScope();
467     auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID());
468 
469     // LineEntry needs the *index* of the file into the list of support files returned by
470     // ParseCompileUnitSupportFiles.  But the underlying SDK gives us a globally unique
471     // idenfitifier in the namespace of the PDB.  So, we have to do a mapping so that we
472     // can hand out indices.
473     std::unordered_map<uint32_t, uint32_t> index_map;
474     BuildSupportFileIdToSupportFileIndexMap(*cu, index_map);
475     auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
476 
477     // Find contributions to `cu` from all source and header files.
478     std::string path = sc.comp_unit->GetPath();
479     auto files = m_session_up->getSourceFilesForCompiland(*cu);
480 
481     // For each source and header file, create a LineSequence for contributions to the cu
482     // from that file, and add the sequence.
483     while (auto file = files->getNext())
484     {
485         std::unique_ptr<LineSequence> sequence(line_table->CreateLineSequenceContainer());
486         auto lines = m_session_up->findLineNumbers(*cu, *file);
487         int entry_count = lines->getChildCount();
488 
489         uint64_t prev_addr;
490         uint32_t prev_length;
491         uint32_t prev_line;
492         uint32_t prev_source_idx;
493 
494         for (int i = 0; i < entry_count; ++i)
495         {
496             auto line = lines->getChildAtIndex(i);
497 
498             uint64_t lno = line->getLineNumber();
499             uint64_t addr = line->getVirtualAddress();
500             uint32_t length = line->getLength();
501             uint32_t source_id = line->getSourceFileId();
502             uint32_t col = line->getColumnNumber();
503             uint32_t source_idx = index_map[source_id];
504 
505             // There was a gap between the current entry and the previous entry if the addresses don't perfectly line
506             // up.
507             bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
508 
509             // Before inserting the current entry, insert a terminal entry at the end of the previous entry's address
510             // range if the current entry resulted in a gap from the previous entry.
511             if (is_gap && ShouldAddLine(match_line, prev_line, prev_length))
512             {
513                 line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0,
514                                                       prev_source_idx, false, false, false, false, true);
515             }
516 
517             if (ShouldAddLine(match_line, lno, length))
518             {
519                 bool is_statement = line->isStatement();
520                 bool is_prologue = false;
521                 bool is_epilogue = false;
522                 auto func = m_session_up->findSymbolByAddress(addr, llvm::PDB_SymType::Function);
523                 if (func)
524                 {
525                     auto prologue = func->findOneChild<llvm::PDBSymbolFuncDebugStart>();
526                     is_prologue = (addr == prologue->getVirtualAddress());
527 
528                     auto epilogue = func->findOneChild<llvm::PDBSymbolFuncDebugEnd>();
529                     is_epilogue = (addr == epilogue->getVirtualAddress());
530                 }
531 
532                 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, source_idx, is_statement, false,
533                                                       is_prologue, is_epilogue, false);
534             }
535 
536             prev_addr = addr;
537             prev_length = length;
538             prev_line = lno;
539             prev_source_idx = source_idx;
540         }
541 
542         if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length))
543         {
544             // The end is always a terminal entry, so insert it regardless.
545             line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0,
546                                                   prev_source_idx, false, false, false, false, true);
547         }
548 
549         line_table->InsertSequence(sequence.release());
550     }
551 
552     sc.comp_unit->SetLineTable(line_table.release());
553     return true;
554 }
555 
556 void
557 SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(const llvm::PDBSymbolCompiland &cu,
558                                                        std::unordered_map<uint32_t, uint32_t> &index_map) const
559 {
560     // This is a hack, but we need to convert the source id into an index into the support
561     // files array.  We don't want to do path comparisons to avoid basename / full path
562     // issues that may or may not even be a problem, so we use the globally unique source
563     // file identifiers.  Ideally we could use the global identifiers everywhere, but LineEntry
564     // currently assumes indices.
565     auto source_files = m_session_up->getSourceFilesForCompiland(cu);
566     int index = 0;
567 
568     while (auto file = source_files->getNext())
569     {
570         uint32_t source_id = file->getUniqueId();
571         index_map[source_id] = index++;
572     }
573 }
574