blob: e8c82c1def40a2576f474ccaa2869dbd3c764af2 [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- 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
Zachary Turner42dff792016-04-15 00:21:26 +000012#include "clang/Lex/Lexer.h"
13
Zachary Turner74e08ca2016-03-02 22:05:52 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/LineTable.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/SymbolContext.h"
Zachary Turner42dff792016-04-15 00:21:26 +000021#include "lldb/Symbol/TypeMap.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000022
Pavel Labathb8d8c622016-05-09 11:07:43 +000023#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000024#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
25#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
26#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
27#include "llvm/DebugInfo/PDB/PDBSymbol.h"
28#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
29#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
30#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
31#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
32#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
33#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Zachary Turner42dff792016-04-15 00:21:26 +000034#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
36#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
37
38#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
39
40#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000041
42using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000043using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000044
45namespace
46{
Zachary Turner54fd7ff2016-05-04 20:33:53 +000047lldb::LanguageType
48TranslateLanguage(PDB_Lang lang)
49{
50 switch (lang)
Zachary Turner74e08ca2016-03-02 22:05:52 +000051 {
Zachary Turner54fd7ff2016-05-04 20:33:53 +000052 case PDB_Lang::Cpp:
Zachary Turner74e08ca2016-03-02 22:05:52 +000053 return lldb::LanguageType::eLanguageTypeC_plus_plus;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000054 case PDB_Lang::C:
Zachary Turner74e08ca2016-03-02 22:05:52 +000055 return lldb::LanguageType::eLanguageTypeC;
56 default:
57 return lldb::LanguageType::eLanguageTypeUnknown;
58 }
59 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +000060
61 bool
62 ShouldAddLine(uint32_t requested_line, uint32_t actual_line, uint32_t addr_length)
63 {
64 return ((requested_line == 0 || actual_line == requested_line) && addr_length > 0);
65 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000066}
67
68void
69SymbolFilePDB::Initialize()
70{
71 PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
72 DebuggerInitialize);
73}
74
75void
76SymbolFilePDB::Terminate()
77{
78 PluginManager::UnregisterPlugin(CreateInstance);
79}
80
81void
82SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger)
83{
84}
85
86lldb_private::ConstString
87SymbolFilePDB::GetPluginNameStatic()
88{
89 static ConstString g_name("pdb");
90 return g_name;
91}
92
93const char *
94SymbolFilePDB::GetPluginDescriptionStatic()
95{
96 return "Microsoft PDB debug symbol file reader.";
97}
98
99lldb_private::SymbolFile *
100SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file)
101{
102 return new SymbolFilePDB(obj_file);
103}
104
105SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
106 : SymbolFile(object_file), m_cached_compile_unit_count(0)
107{
108}
109
110SymbolFilePDB::~SymbolFilePDB()
111{
112}
113
114uint32_t
115SymbolFilePDB::CalculateAbilities()
116{
117 if (!m_session_up)
118 {
119 // Lazily load and match the PDB file, but only do this once.
120 std::string exePath = m_obj_file->GetFileSpec().GetPath();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000121 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), m_session_up);
Zachary Turner50c58c02016-05-06 21:35:47 +0000122 if (error)
Pavel Labathb8d8c622016-05-09 11:07:43 +0000123 {
124 handleAllErrors(std::move(error), [](const GenericError &GE) {});
Zachary Turner74e08ca2016-03-02 22:05:52 +0000125 return 0;
Pavel Labathb8d8c622016-05-09 11:07:43 +0000126 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000127 }
128 return CompileUnits | LineTables;
129}
130
131void
132SymbolFilePDB::InitializeObject()
133{
134 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
135 m_session_up->setLoadAddress(obj_load_address);
Zachary Turner42dff792016-04-15 00:21:26 +0000136
137 TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
138 ClangASTContext *clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
139 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000140}
141
142uint32_t
143SymbolFilePDB::GetNumCompileUnits()
144{
145 if (m_cached_compile_unit_count == 0)
146 {
147 auto global = m_session_up->getGlobalScope();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000148 auto compilands = global->findAllChildren<PDBSymbolCompiland>();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000149 m_cached_compile_unit_count = compilands->getChildCount();
150
151 // The linker can inject an additional "dummy" compilation unit into the PDB.
152 // Ignore this special compile unit for our purposes, if it is there. It is
153 // always the last one.
154 auto last_cu = compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
155 std::string name = last_cu->getName();
156 if (name == "* Linker *")
157 --m_cached_compile_unit_count;
158 }
159 return m_cached_compile_unit_count;
160}
161
162lldb::CompUnitSP
163SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index)
164{
165 auto global = m_session_up->getGlobalScope();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000166 auto compilands = global->findAllChildren<PDBSymbolCompiland>();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000167 auto cu = compilands->getChildAtIndex(index);
168
169 uint32_t id = cu->getSymIndexId();
170
171 return ParseCompileUnitForSymIndex(id);
172}
173
174lldb::LanguageType
175SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc)
176{
177 // What fields should I expect to be filled out on the SymbolContext? Is it
178 // safe to assume that `sc.comp_unit` is valid?
179 if (!sc.comp_unit)
180 return lldb::eLanguageTypeUnknown;
181
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000182 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(sc.comp_unit->GetID());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000183 if (!cu)
184 return lldb::eLanguageTypeUnknown;
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000185 auto details = cu->findOneChild<PDBSymbolCompilandDetails>();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000186 if (!details)
187 return lldb::eLanguageTypeUnknown;
188 return TranslateLanguage(details->getLanguage());
189}
190
191size_t
192SymbolFilePDB::ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc)
193{
194 // TODO: Implement this
195 return size_t();
196}
197
198bool
199SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc)
200{
201 return ParseCompileUnitLineTable(sc, 0);
202}
203
204bool
205SymbolFilePDB::ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc)
206{
207 // PDB doesn't contain information about macros
208 return false;
209}
210
211bool
212SymbolFilePDB::ParseCompileUnitSupportFiles(const lldb_private::SymbolContext &sc,
213 lldb_private::FileSpecList &support_files)
214{
215 if (!sc.comp_unit)
216 return false;
217
218 // In theory this is unnecessary work for us, because all of this information is easily
219 // (and quickly) accessible from DebugInfoPDB, so caching it a second time seems like a waste.
220 // Unfortunately, there's no good way around this short of a moderate refactor, since SymbolVendor
221 // depends on being able to cache this list.
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000222 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(sc.comp_unit->GetID());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000223 if (!cu)
224 return false;
225 auto files = m_session_up->getSourceFilesForCompiland(*cu);
226 if (!files || files->getChildCount() == 0)
227 return false;
228
229 while (auto file = files->getNext())
230 {
231 FileSpec spec(file->getFileName(), false);
232 support_files.Append(spec);
233 }
234 return true;
235}
236
237bool
238SymbolFilePDB::ParseImportedModules(const lldb_private::SymbolContext &sc,
239 std::vector<lldb_private::ConstString> &imported_modules)
240{
241 // PDB does not yet support module debug info
242 return false;
243}
244
245size_t
246SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc)
247{
248 // TODO: Implement this
249 return size_t();
250}
251
252size_t
253SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc)
254{
255 // TODO: Implement this
256 return size_t();
257}
258
259size_t
260SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc)
261{
262 // TODO: Implement this
263 return size_t();
264}
265
266lldb_private::Type *
267SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid)
268{
Zachary Turner42dff792016-04-15 00:21:26 +0000269 auto find_result = m_types.find(type_uid);
270 if (find_result != m_types.end())
271 return find_result->second.get();
272
273 TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
274 ClangASTContext *clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
275 if (!clang_type_system)
276 return nullptr;
277 PDBASTParser *pdb = llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
278 if (!pdb)
279 return nullptr;
280
281 auto pdb_type = m_session_up->getSymbolById(type_uid);
282 if (pdb_type == nullptr)
283 return nullptr;
284
285 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
286 m_types.insert(std::make_pair(type_uid, result));
287 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000288}
289
290bool
291SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type)
292{
293 // TODO: Implement this
294 return false;
295}
296
297lldb_private::CompilerDecl
298SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid)
299{
300 return lldb_private::CompilerDecl();
301}
302
303lldb_private::CompilerDeclContext
304SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid)
305{
Zachary Turner42dff792016-04-15 00:21:26 +0000306 // PDB always uses the translation unit decl context for everything. We can improve this later
307 // but it's not easy because PDB doesn't provide a high enough level of type fidelity in this area.
308 return *m_tu_decl_ctx_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000309}
310
311lldb_private::CompilerDeclContext
312SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid)
313{
Zachary Turner42dff792016-04-15 00:21:26 +0000314 return *m_tu_decl_ctx_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000315}
316
317void
318SymbolFilePDB::ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx)
319{
320}
321
322uint32_t
323SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, uint32_t resolve_scope,
324 lldb_private::SymbolContext &sc)
325{
326 return uint32_t();
327}
328
329uint32_t
330SymbolFilePDB::ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
331 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list)
332{
333 if (resolve_scope & lldb::eSymbolContextCompUnit)
334 {
335 // Locate all compilation units with line numbers referencing the specified file. For example, if
336 // `file_spec` is <vector>, then this should return all source files and header files that reference
337 // <vector>, either directly or indirectly.
338 auto compilands =
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000339 m_session_up->findCompilandsForSourceFile(file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000340
341 // For each one, either find get its previously parsed data, or parse it afresh and add it to
342 // the symbol context list.
343 while (auto compiland = compilands->getNext())
344 {
345 // If we're not checking inlines, then don't add line information for this file unless the FileSpec
346 // matches.
347 if (!check_inlines)
348 {
349 // `getSourceFileName` returns the basename of the original source file used to generate this compiland.
350 // It does not return the full path. Currently the only way to get that is to do a basename lookup to
351 // get the IPDBSourceFile, but this is ambiguous in the case of two source files with the same name
352 // contributing to the same compiland. This is a moderately extreme edge case, so we consider this ok
353 // for now, although we need to find a long term solution.
354 std::string source_file = compiland->getSourceFileName();
355 auto pdb_file = m_session_up->findOneSourceFile(compiland.get(), source_file,
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000356 PDB_NameSearchFlags::NS_CaseInsensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000357 source_file = pdb_file->getFileName();
358 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
359 if (!file_spec.FileEquals(this_spec))
360 continue;
361 }
362
363 SymbolContext sc;
364 auto cu = ParseCompileUnitForSymIndex(compiland->getSymIndexId());
365 sc.comp_unit = cu.get();
366 sc.module_sp = cu->GetModule();
367 sc_list.Append(sc);
368
369 // If we were asked to resolve line entries, add all entries to the line table that match the requested
370 // line (or all lines if `line` == 0)
371 if (resolve_scope & lldb::eSymbolContextLineEntry)
372 ParseCompileUnitLineTable(sc, line);
373 }
374 }
375 return sc_list.GetSize();
376}
377
378uint32_t
379SymbolFilePDB::FindGlobalVariables(const lldb_private::ConstString &name,
380 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
381 uint32_t max_matches, lldb_private::VariableList &variables)
382{
383 return uint32_t();
384}
385
386uint32_t
387SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex, bool append, uint32_t max_matches,
388 lldb_private::VariableList &variables)
389{
390 return uint32_t();
391}
392
393uint32_t
394SymbolFilePDB::FindFunctions(const lldb_private::ConstString &name,
395 const lldb_private::CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask,
396 bool include_inlines, bool append, lldb_private::SymbolContextList &sc_list)
397{
398 return uint32_t();
399}
400
401uint32_t
402SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex, bool include_inlines, bool append,
403 lldb_private::SymbolContextList &sc_list)
404{
405 return uint32_t();
406}
407
408void
409SymbolFilePDB::GetMangledNamesForFunction(const std::string &scope_qualified_name,
410 std::vector<lldb_private::ConstString> &mangled_names)
411{
412}
413
414uint32_t
415SymbolFilePDB::FindTypes(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name,
416 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches,
417 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
418 lldb_private::TypeMap &types)
419{
Zachary Turner42dff792016-04-15 00:21:26 +0000420 if (!append)
421 types.Clear();
422 if (!name)
423 return 0;
424
425 searched_symbol_files.clear();
426 searched_symbol_files.insert(this);
427
428 std::string name_str = name.AsCString();
429
430 // If this might be a regex, we have to return EVERY symbol and process them one by one, which is going
431 // to destroy performance on large PDB files. So try really hard not to use a regex match.
432 if (name_str.find_first_of("[]?*.-+\\") != std::string::npos)
433 FindTypesByRegex(name_str, max_matches, types);
434 else
435 FindTypesByName(name_str, max_matches, types);
436 return types.GetSize();
437}
438
439void
440SymbolFilePDB::FindTypesByRegex(const std::string &regex, uint32_t max_matches, lldb_private::TypeMap &types)
441{
442 // When searching by regex, we need to go out of our way to limit the search space as much as possible, since
443 // the way this is implemented is by searching EVERYTHING in the PDB and manually doing a regex compare. PDB
444 // library isn't optimized for regex searches or searches across multiple symbol types at the same time, so the
445 // best we can do is to search enums, then typedefs, then classes one by one, and do a regex compare against all
446 // of them.
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000447 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, PDB_SymType::UDT};
Zachary Turner42dff792016-04-15 00:21:26 +0000448 auto global = m_session_up->getGlobalScope();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000449 std::unique_ptr<IPDBEnumSymbols> results;
Zachary Turner42dff792016-04-15 00:21:26 +0000450
451 std::regex re(regex);
452
453 uint32_t matches = 0;
454
455 for (auto tag : tags_to_search)
456 {
457 results = global->findAllChildren(tag);
458 while (auto result = results->getNext())
459 {
460 if (max_matches > 0 && matches >= max_matches)
461 break;
462
463 std::string type_name;
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000464 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
Zachary Turner42dff792016-04-15 00:21:26 +0000465 type_name = enum_type->getName();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000466 else if (auto typedef_type = llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Zachary Turner42dff792016-04-15 00:21:26 +0000467 type_name = typedef_type->getName();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000468 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
Zachary Turner42dff792016-04-15 00:21:26 +0000469 type_name = class_type->getName();
470 else
471 {
472 // We're only looking for types that have names. Skip symbols, as well as
473 // unnamed types such as arrays, pointers, etc.
474 continue;
475 }
476
477 if (!std::regex_match(type_name, re))
478 continue;
479
480 // This should cause the type to get cached and stored in the `m_types` lookup.
481 if (!ResolveTypeUID(result->getSymIndexId()))
482 continue;
483
484 auto iter = m_types.find(result->getSymIndexId());
485 if (iter == m_types.end())
486 continue;
487 types.Insert(iter->second);
488 ++matches;
489 }
490 }
491}
492
493void
494SymbolFilePDB::FindTypesByName(const std::string &name, uint32_t max_matches, lldb_private::TypeMap &types)
495{
496 auto global = m_session_up->getGlobalScope();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000497 std::unique_ptr<IPDBEnumSymbols> results;
498 results = global->findChildren(PDB_SymType::None, name.c_str(), PDB_NameSearchFlags::NS_Default);
Zachary Turner42dff792016-04-15 00:21:26 +0000499
500 uint32_t matches = 0;
501
502 while (auto result = results->getNext())
503 {
504 if (max_matches > 0 && matches >= max_matches)
505 break;
506 switch (result->getSymTag())
507 {
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000508 case PDB_SymType::Enum:
509 case PDB_SymType::UDT:
510 case PDB_SymType::Typedef:
Zachary Turner42dff792016-04-15 00:21:26 +0000511 break;
512 default:
513 // We're only looking for types that have names. Skip symbols, as well as
514 // unnamed types such as arrays, pointers, etc.
515 continue;
516 }
517
518 // This should cause the type to get cached and stored in the `m_types` lookup.
519 if (!ResolveTypeUID(result->getSymIndexId()))
520 continue;
521
522 auto iter = m_types.find(result->getSymIndexId());
523 if (iter == m_types.end())
524 continue;
525 types.Insert(iter->second);
526 ++matches;
527 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000528}
529
530size_t
Zachary Turner42dff792016-04-15 00:21:26 +0000531SymbolFilePDB::FindTypes(const std::vector<lldb_private::CompilerContext> &contexts, bool append,
Zachary Turner74e08ca2016-03-02 22:05:52 +0000532 lldb_private::TypeMap &types)
533{
Zachary Turner42dff792016-04-15 00:21:26 +0000534 return 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000535}
536
537lldb_private::TypeList *
538SymbolFilePDB::GetTypeList()
539{
540 return nullptr;
541}
542
543size_t
544SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, uint32_t type_mask,
545 lldb_private::TypeList &type_list)
546{
547 return size_t();
548}
549
550lldb_private::TypeSystem *
551SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language)
552{
553 auto type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
554 if (type_system)
555 type_system->SetSymbolFile(this);
556 return type_system;
557}
558
559lldb_private::CompilerDeclContext
560SymbolFilePDB::FindNamespace(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name,
561 const lldb_private::CompilerDeclContext *parent_decl_ctx)
562{
563 return lldb_private::CompilerDeclContext();
564}
565
566lldb_private::ConstString
567SymbolFilePDB::GetPluginName()
568{
569 static ConstString g_name("pdb");
570 return g_name;
571}
572
573uint32_t
574SymbolFilePDB::GetPluginVersion()
575{
576 return 1;
577}
578
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000579IPDBSession &
Zachary Turner42dff792016-04-15 00:21:26 +0000580SymbolFilePDB::GetPDBSession()
581{
582 return *m_session_up;
583}
584
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000585const IPDBSession &
Zachary Turner42dff792016-04-15 00:21:26 +0000586SymbolFilePDB::GetPDBSession() const
587{
588 return *m_session_up;
589}
590
Zachary Turner74e08ca2016-03-02 22:05:52 +0000591lldb::CompUnitSP
592SymbolFilePDB::ParseCompileUnitForSymIndex(uint32_t id)
593{
594 auto found_cu = m_comp_units.find(id);
595 if (found_cu != m_comp_units.end())
596 return found_cu->second;
597
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000598 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(id);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000599
600 // `getSourceFileName` returns the basename of the original source file used to generate this compiland. It does
601 // not return the full path. Currently the only way to get that is to do a basename lookup to get the
602 // IPDBSourceFile, but this is ambiguous in the case of two source files with the same name contributing to the
603 // same compiland. This is a moderately extreme edge case, so we consider this ok for now, although we need to find
604 // a long term solution.
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000605 auto file =
606 m_session_up->findOneSourceFile(cu.get(), cu->getSourceFileName(), PDB_NameSearchFlags::NS_CaseInsensitive);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000607 std::string path = file->getFileName();
608
609 lldb::LanguageType lang;
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000610 auto details = cu->findOneChild<PDBSymbolCompilandDetails>();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000611 if (!details)
612 lang = lldb::eLanguageTypeC_plus_plus;
613 else
614 lang = TranslateLanguage(details->getLanguage());
615
616 // Don't support optimized code for now, DebugInfoPDB does not return this information.
617 bool optimized = false;
618 auto result = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
619 m_comp_units.insert(std::make_pair(id, result));
620 return result;
621}
622
623bool
624SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc, uint32_t match_line)
625{
626 auto global = m_session_up->getGlobalScope();
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000627 auto cu = m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(sc.comp_unit->GetID());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000628
629 // LineEntry needs the *index* of the file into the list of support files returned by
630 // ParseCompileUnitSupportFiles. But the underlying SDK gives us a globally unique
631 // idenfitifier in the namespace of the PDB. So, we have to do a mapping so that we
632 // can hand out indices.
Zachary Turner42dff792016-04-15 00:21:26 +0000633 llvm::DenseMap<uint32_t, uint32_t> index_map;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000634 BuildSupportFileIdToSupportFileIndexMap(*cu, index_map);
635 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
636
637 // Find contributions to `cu` from all source and header files.
638 std::string path = sc.comp_unit->GetPath();
639 auto files = m_session_up->getSourceFilesForCompiland(*cu);
640
641 // For each source and header file, create a LineSequence for contributions to the cu
642 // from that file, and add the sequence.
643 while (auto file = files->getNext())
644 {
645 std::unique_ptr<LineSequence> sequence(line_table->CreateLineSequenceContainer());
646 auto lines = m_session_up->findLineNumbers(*cu, *file);
647 int entry_count = lines->getChildCount();
648
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000649 uint64_t prev_addr;
650 uint32_t prev_length;
651 uint32_t prev_line;
652 uint32_t prev_source_idx;
653
Zachary Turner74e08ca2016-03-02 22:05:52 +0000654 for (int i = 0; i < entry_count; ++i)
655 {
656 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000657
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000658 uint64_t lno = line->getLineNumber();
659 uint64_t addr = line->getVirtualAddress();
660 uint32_t length = line->getLength();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000661 uint32_t source_id = line->getSourceFileId();
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000662 uint32_t col = line->getColumnNumber();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000663 uint32_t source_idx = index_map[source_id];
664
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000665 // There was a gap between the current entry and the previous entry if the addresses don't perfectly line
666 // up.
667 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
668
669 // Before inserting the current entry, insert a terminal entry at the end of the previous entry's address
670 // range if the current entry resulted in a gap from the previous entry.
671 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length))
Zachary Turner74e08ca2016-03-02 22:05:52 +0000672 {
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000673 line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0,
674 prev_source_idx, false, false, false, false, true);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000675 }
676
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000677 if (ShouldAddLine(match_line, lno, length))
678 {
679 bool is_statement = line->isStatement();
680 bool is_prologue = false;
681 bool is_epilogue = false;
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000682 auto func = m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000683 if (func)
684 {
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000685 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000686 is_prologue = (addr == prologue->getVirtualAddress());
687
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000688 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000689 is_epilogue = (addr == epilogue->getVirtualAddress());
690 }
691
692 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, source_idx, is_statement, false,
693 is_prologue, is_epilogue, false);
694 }
695
696 prev_addr = addr;
697 prev_length = length;
698 prev_line = lno;
699 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000700 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000701
702 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length))
703 {
704 // The end is always a terminal entry, so insert it regardless.
705 line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0,
706 prev_source_idx, false, false, false, false, true);
707 }
708
709 line_table->InsertSequence(sequence.release());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000710 }
711
712 sc.comp_unit->SetLineTable(line_table.release());
713 return true;
714}
715
716void
Zachary Turner54fd7ff2016-05-04 20:33:53 +0000717SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(const PDBSymbolCompiland &cu,
Zachary Turner42dff792016-04-15 00:21:26 +0000718 llvm::DenseMap<uint32_t, uint32_t> &index_map) const
Zachary Turner74e08ca2016-03-02 22:05:52 +0000719{
720 // This is a hack, but we need to convert the source id into an index into the support
721 // files array. We don't want to do path comparisons to avoid basename / full path
722 // issues that may or may not even be a problem, so we use the globally unique source
723 // file identifiers. Ideally we could use the global identifiers everywhere, but LineEntry
724 // currently assumes indices.
725 auto source_files = m_session_up->getSourceFilesForCompiland(cu);
726 int index = 0;
727
728 while (auto file = source_files->getNext())
729 {
730 uint32_t source_id = file->getUniqueId();
731 index_map[source_id] = index++;
732 }
733}