blob: ee0f8a1967ce8f4432d9e141a2531df5afd91960 [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"
Aaron Smith10a02572018-01-13 06:58:18 +000021#include "lldb/Symbol/SymbolVendor.h"
Zachary Turner42dff792016-04-15 00:21:26 +000022#include "lldb/Symbol/TypeMap.h"
Aaron Smith86e94342017-12-22 05:26:50 +000023#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000024
Pavel Labathb8d8c622016-05-09 11:07:43 +000025#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000026#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000027#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
28#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
29#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000030#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000031#include "llvm/DebugInfo/PDB/PDBSymbol.h"
32#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
33#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000034#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000035#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"
Zachary Turner42dff792016-04-15 00:21:26 +000039#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>
Zachary Turner74e08ca2016-03-02 22:05:52 +000046
Aaron Smith10a02572018-01-13 06:58:18 +000047using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000048using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000049using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051namespace {
52lldb::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 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063bool 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}
Zachary Turner74e08ca2016-03-02 22:05:52 +000068}
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070void SymbolFilePDB::Initialize() {
71 PluginManager::RegisterPlugin(GetPluginNameStatic(),
72 GetPluginDescriptionStatic(), CreateInstance,
73 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076void SymbolFilePDB::Terminate() {
77 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
81
82lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
83 static ConstString g_name("pdb");
84 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000085}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087const char *SymbolFilePDB::GetPluginDescriptionStatic() {
88 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000089}
90
91lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000092SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
93 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +000094}
95
96SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +000097 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
98 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000103 uint32_t abilities = 0;
104 if (!m_obj_file)
105 return 0;
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 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));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000114 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 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 }
Aaron Smith1f8552a2017-12-22 00:04:36 +0000130 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;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000154}
155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156void SymbolFilePDB::InitializeObject() {
157 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smith10a02572018-01-13 06:58:18 +0000158 lldbassert(obj_load_address &&
159 obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000161 if (!m_global_scope_up)
162 m_global_scope_up = m_session_up->getGlobalScope();
163 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 TypeSystem *type_system =
166 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
167 ClangASTContext *clang_type_system =
168 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000169 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
171 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000172}
173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174uint32_t SymbolFilePDB::GetNumCompileUnits() {
175 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000176 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000191 // PDB. Ignore this special compile unit for our purposes, if it is there.
192 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000193 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();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 if (name == "* Linker *")
198 --m_cached_compile_unit_count;
199 }
200 return m_cached_compile_unit_count;
201}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000202
Aaron Smith10a02572018-01-13 06:58:18 +0000203void 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
226std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
227SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
228 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000232 if (index >= GetNumCompileUnits())
233 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234
Aaron Smith10a02572018-01-13 06:58:18 +0000235 // 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);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000245}
246
247lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248SymbolFilePDB::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;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000253
Aaron Smith10a02572018-01-13 06:58:18 +0000254 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
255 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000257 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 if (!details)
259 return lldb::eLanguageTypeUnknown;
260 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000261}
262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263size_t SymbolFilePDB::ParseCompileUnitFunctions(
264 const lldb_private::SymbolContext &sc) {
265 // TODO: Implement this
266 return size_t();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000267}
268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269bool SymbolFilePDB::ParseCompileUnitLineTable(
270 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000271 lldbassert(sc.comp_unit);
272 if (sc.comp_unit->GetLineTable())
273 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000275}
276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277bool SymbolFilePDB::ParseCompileUnitDebugMacros(
278 const lldb_private::SymbolContext &sc) {
279 // PDB doesn't contain information about macros
280 return false;
281}
282
283bool SymbolFilePDB::ParseCompileUnitSupportFiles(
284 const lldb_private::SymbolContext &sc,
285 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000286 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000289 // 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.
Aaron Smith10a02572018-01-13 06:58:18 +0000293 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
294 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000295 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000296 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000298 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299
300 while (auto file = files->getNext()) {
Aaron Smith10a02572018-01-13 06:58:18 +0000301 FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
302 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 }
304 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000305}
306
Kate Stoneb9c1b512016-09-06 20:57:50 +0000307bool 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;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000312}
313
314size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
316 // TODO: Implement this
317 return size_t();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000318}
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
321 // TODO: Implement this
322 return size_t();
323}
324
325size_t
326SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
327 // TODO: Implement this
328 return size_t();
329}
330
331lldb_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)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000341 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 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);
Aaron Smith86e94342017-12-22 05:26:50 +0000352 if (result.get())
353 m_types.insert(std::make_pair(type_uid, result));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000355}
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
358 // TODO: Implement this
359 return false;
360}
361
362lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
363 return lldb_private::CompilerDecl();
364}
365
366lldb_private::CompilerDeclContext
367SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
368 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000369 // improve this later but it's not easy because PDB doesn't provide a high
370 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 return *m_tu_decl_ctx_up;
372}
373
374lldb_private::CompilerDeclContext
375SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
376 return *m_tu_decl_ctx_up;
377}
378
379void SymbolFilePDB::ParseDeclsForContext(
380 lldb_private::CompilerDeclContext decl_ctx) {}
381
382uint32_t
383SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
384 uint32_t resolve_scope,
385 lldb_private::SymbolContext &sc) {
386 return uint32_t();
387}
388
Aaron Smith10a02572018-01-13 06:58:18 +0000389std::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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000441uint32_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) {
Aaron Smith10a02572018-01-13 06:58:18 +0000444 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 if (resolve_scope & lldb::eSymbolContextCompUnit) {
446 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000447 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450 auto compilands = m_session_up->findCompilandsForSourceFile(
451 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
452
Aaron Smith10a02572018-01-13 06:58:18 +0000453 if (!compilands)
454 return 0;
455
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000456 // For each one, either find its previously parsed data or parse it afresh
457 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000458 while (auto compiland = compilands->getNext()) {
459 // If we're not checking inlines, then don't add line information for this
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000460 // file unless the FileSpec matches.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000461 if (!check_inlines) {
462 // `getSourceFileName` returns the basename of the original source file
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000463 // 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.
Aaron Smith10a02572018-01-13 06:58:18 +0000469 std::string source_file =
470 GetSourceFileNameForPDBCompiland(compiland.get());
471 if (source_file.empty())
472 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
Aaron Smith10a02572018-01-13 06:58:18 +0000474 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
475 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000476 continue;
477 }
478
479 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000480 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
481 if (!cu.get())
482 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 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
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000488 // table that match the requested line (or all lines if `line` == 0).
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489 if (resolve_scope & lldb::eSymbolContextLineEntry)
490 ParseCompileUnitLineTable(sc, line);
491 }
492 }
Aaron Smith10a02572018-01-13 06:58:18 +0000493 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000494}
495
496uint32_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
503uint32_t
504SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
505 bool append, uint32_t max_matches,
506 lldb_private::VariableList &variables) {
507 return uint32_t();
508}
509
510uint32_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
518uint32_t
519SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
520 bool include_inlines, bool append,
521 lldb_private::SymbolContextList &sc_list) {
522 return uint32_t();
523}
524
525void SymbolFilePDB::GetMangledNamesForFunction(
526 const std::string &scope_qualified_name,
527 std::vector<lldb_private::ConstString> &mangled_names) {}
528
529uint32_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
Aaron Smith86e94342017-12-22 05:26:50 +0000546 // There is an assumption 'name' is not a regex
547 FindTypesByName(name_str, max_matches, types);
548
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549 return types.GetSize();
550}
551
Aaron Smith86e94342017-12-22 05:26:50 +0000552void
553SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression &regex,
554 uint32_t max_matches,
555 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000556 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000557 // 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
Kate Stoneb9c1b512016-09-06 20:57:50 +0000560 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000561 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000562 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
563 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000564 std::unique_ptr<IPDBEnumSymbols> results;
565
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566 uint32_t matches = 0;
567
568 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +0000569 results = m_global_scope_up->findAllChildren(tag);
570 if (!results)
571 continue;
572
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 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 {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000586 // We're looking only for types that have names. Skip symbols, as well
587 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000588 continue;
589 }
590
Aaron Smith86e94342017-12-22 05:26:50 +0000591 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000592 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
608void SymbolFilePDB::FindTypesByName(const std::string &name,
609 uint32_t max_matches,
610 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smith10a02572018-01-13 06:58:18 +0000612 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
613 PDB_NameSearchFlags::NS_Default);
614 if (!results)
615 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616
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:
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000628 // We're looking only for types that have names. Skip symbols, as well as
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 // 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
646size_t SymbolFilePDB::FindTypes(
647 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
648 lldb_private::TypeMap &types) {
649 return 0;
650}
651
652lldb_private::TypeList *SymbolFilePDB::GetTypeList() { return nullptr; }
653
654size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
655 uint32_t type_mask,
656 lldb_private::TypeList &type_list) {
657 return size_t();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000658}
659
660lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000661SymbolFilePDB::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;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000667}
668
Kate Stoneb9c1b512016-09-06 20:57:50 +0000669lldb_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();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000674}
675
Kate Stoneb9c1b512016-09-06 20:57:50 +0000676lldb_private::ConstString SymbolFilePDB::GetPluginName() {
677 static ConstString g_name("pdb");
678 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000679}
680
Kate Stoneb9c1b512016-09-06 20:57:50 +0000681uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
682
683IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
684
685const IPDBSession &SymbolFilePDB::GetPDBSession() const {
686 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000687}
688
Aaron Smith10a02572018-01-13 06:58:18 +0000689lldb::CompUnitSP
690SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 auto found_cu = m_comp_units.find(id);
692 if (found_cu != m_comp_units.end())
693 return found_cu->second;
694
Aaron Smith10a02572018-01-13 06:58:18 +0000695 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();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701
702 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +0000703 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000704 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;
Aaron Smith10a02572018-01-13 06:58:18 +0000712 auto cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713 m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +0000714
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;
Zachary Turner42dff792016-04-15 00:21:26 +0000725}
726
Kate Stoneb9c1b512016-09-06 20:57:50 +0000727bool SymbolFilePDB::ParseCompileUnitLineTable(
728 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +0000729 lldbassert(sc.comp_unit);
730
731 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
732 if (!compiland_up)
733 return false;
Zachary Turner42dff792016-04-15 00:21:26 +0000734
Kate Stoneb9c1b512016-09-06 20:57:50 +0000735 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000736 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000739 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +0000740 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000741 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000742
Aaron Smith10a02572018-01-13 06:58:18 +0000743 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000744 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000745 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
746 if (!files)
747 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000748
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 // For each source and header file, create a LineSequence for contributions to
Aaron Smith10a02572018-01-13 06:58:18 +0000750 // the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000751 while (auto file = files->getNext()) {
752 std::unique_ptr<LineSequence> sequence(
753 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +0000754 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
755 if (!lines)
756 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000757 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759 uint64_t prev_addr;
760 uint32_t prev_length;
761 uint32_t prev_line;
762 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 for (int i = 0; i < entry_count; ++i) {
765 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000766
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 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];
Zachary Turner74e08ca2016-03-02 22:05:52 +0000773
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 // There was a gap between the current entry and the previous entry if the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000775 // addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000779 // of the previous entry's address range if the current entry resulted in
780 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 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 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000786
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 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>();
Aaron Smith10a02572018-01-13 06:58:18 +0000795 if (prologue)
796 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000797
Kate Stoneb9c1b512016-09-06 20:57:50 +0000798 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +0000799 if (epilogue)
800 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000801 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000802
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
804 source_idx, is_statement, false,
805 is_prologue, is_epilogue, false);
806 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +0000807
Kate Stoneb9c1b512016-09-06 20:57:50 +0000808 prev_addr = addr;
809 prev_length = length;
810 prev_line = lno;
811 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000812 }
813
Kate Stoneb9c1b512016-09-06 20:57:50 +0000814 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
Aaron Smith10a02572018-01-13 06:58:18 +0000824 if (line_table->GetSize()) {
825 sc.comp_unit->SetLineTable(line_table.release());
826 return true;
827 }
828 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000829}
830
Kate Stoneb9c1b512016-09-06 20:57:50 +0000831void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +0000832 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000833 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
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000835 // 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.
Aaron Smith10a02572018-01-13 06:58:18 +0000839 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
840 if (!source_files)
841 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000842 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000843
Kate Stoneb9c1b512016-09-06 20:57:50 +0000844 while (auto file = source_files->getNext()) {
845 uint32_t source_id = file->getUniqueId();
846 index_map[source_id] = index++;
847 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000848}