blob: c958d0c94a86102e3711af81293bfd27b4830a9f [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 Smithec40f812018-01-23 20:35:19 +000023#include "lldb/Symbol/TypeList.h"
Aaron Smith86e94342017-12-22 05:26:50 +000024#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000025
Pavel Labathb8d8c622016-05-09 11:07:43 +000026#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000027#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000028#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
29#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
30#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000031#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000033#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000036#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000037#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
38#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000041#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000042#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
45
Aaron Smith7ac1c782018-02-09 05:31:28 +000046#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner42dff792016-04-15 00:21:26 +000047#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
48
49#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000050
Aaron Smith10a02572018-01-13 06:58:18 +000051using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000052using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000053using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055namespace {
56lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
57 switch (lang) {
58 case PDB_Lang::Cpp:
59 return lldb::LanguageType::eLanguageTypeC_plus_plus;
60 case PDB_Lang::C:
61 return lldb::LanguageType::eLanguageTypeC;
62 default:
63 return lldb::LanguageType::eLanguageTypeUnknown;
64 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
68 uint32_t addr_length) {
69 return ((requested_line == 0 || actual_line == requested_line) &&
70 addr_length > 0);
71}
Aaron Smithc8316ed2018-03-22 03:44:51 +000072} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000073
Kate Stoneb9c1b512016-09-06 20:57:50 +000074void SymbolFilePDB::Initialize() {
75 PluginManager::RegisterPlugin(GetPluginNameStatic(),
76 GetPluginDescriptionStatic(), CreateInstance,
77 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void SymbolFilePDB::Terminate() {
81 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
85
86lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
87 static ConstString g_name("pdb");
88 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091const char *SymbolFilePDB::GetPluginDescriptionStatic() {
92 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000093}
94
95lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000096SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
97 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +000098}
99
100SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000101 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
102 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000107 uint32_t abilities = 0;
108 if (!m_obj_file)
109 return 0;
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (!m_session_up) {
112 // Lazily load and match the PDB file, but only do this once.
113 std::string exePath = m_obj_file->GetFileSpec().GetPath();
114 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
115 m_session_up);
116 if (error) {
117 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000118 auto module_sp = m_obj_file->GetModule();
119 if (!module_sp)
120 return 0;
121 // See if any symbol file is specified through `--symfile` option.
122 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
123 if (!symfile)
124 return 0;
125 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000126 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000127 if (error) {
128 llvm::consumeError(std::move(error));
129 return 0;
130 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000133 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000134 return 0;
135
136 auto enum_tables_up = m_session_up->getEnumTables();
137 if (!enum_tables_up)
138 return 0;
139 while (auto table_up = enum_tables_up->getNext()) {
140 if (table_up->getItemCount() == 0)
141 continue;
142 auto type = table_up->getTableType();
143 switch (type) {
144 case PDB_TableType::Symbols:
145 // This table represents a store of symbols with types listed in
146 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000147 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
148 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000149 break;
150 case PDB_TableType::LineNumbers:
151 abilities |= LineTables;
152 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000153 default:
154 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000155 }
156 }
157 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void SymbolFilePDB::InitializeObject() {
161 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000162 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000164 if (!m_global_scope_up)
165 m_global_scope_up = m_session_up->getGlobalScope();
166 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 TypeSystem *type_system =
169 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
170 ClangASTContext *clang_type_system =
171 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000172 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
174 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000175}
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177uint32_t SymbolFilePDB::GetNumCompileUnits() {
178 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000179 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
180 if (!compilands)
181 return 0;
182
183 // The linker could link *.dll (compiland language = LINK), or import
184 // *.dll. For example, a compiland with name `Import:KERNEL32.dll`
185 // could be found as a child of the global scope (PDB executable).
186 // Usually, such compilands contain `thunk` symbols in which we are not
187 // interested for now. However we still count them in the compiland list.
188 // If we perform any compiland related activity, like finding symbols
189 // through llvm::pdb::IPDBSession methods, such compilands will all be
190 // searched automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000194 // PDB. Ignore this special compile unit for our purposes, if it is there.
195 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000196 auto last_compiland_up =
197 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
198 lldbassert(last_compiland_up.get());
199 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 if (name == "* Linker *")
201 --m_cached_compile_unit_count;
202 }
203 return m_cached_compile_unit_count;
204}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000205
Aaron Smith10a02572018-01-13 06:58:18 +0000206void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000207 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000208 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
209 if (!results_up)
210 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000211 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000212 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000213 auto compiland_up = results_up->getChildAtIndex(cu_idx);
214 if (!compiland_up)
215 continue;
216 if (compiland_up->getSymIndexId() == uid) {
217 index = cu_idx;
218 return;
219 }
220 }
221 index = UINT32_MAX;
222 return;
223}
224
225std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
226SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
227 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000231 if (index >= GetNumCompileUnits())
232 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233
Aaron Smith10a02572018-01-13 06:58:18 +0000234 // Assuming we always retrieve same compilands listed in same order through
235 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
236 // compile unit makes no sense.
237 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
238 if (!results)
239 return CompUnitSP();
240 auto compiland_up = results->getChildAtIndex(index);
241 if (!compiland_up)
242 return CompUnitSP();
243 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000244}
245
246lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
248 // What fields should I expect to be filled out on the SymbolContext? Is it
249 // safe to assume that `sc.comp_unit` is valid?
250 if (!sc.comp_unit)
251 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000252
Aaron Smith10a02572018-01-13 06:58:18 +0000253 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
254 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000256 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257 if (!details)
258 return lldb::eLanguageTypeUnknown;
259 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000260}
261
Aaron Smithc8316ed2018-03-22 03:44:51 +0000262lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
263 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000264 lldbassert(sc.comp_unit && sc.module_sp.get());
265
Aaron Smithe664b5d2018-03-19 21:14:19 +0000266 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000267 if (file_vm_addr == LLDB_INVALID_ADDRESS)
268 return nullptr;
269
Aaron Smithe664b5d2018-03-19 21:14:19 +0000270 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000271 AddressRange func_range =
272 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000273 if (!func_range.GetBaseAddress().IsValid())
274 return nullptr;
275
Aaron Smithc8316ed2018-03-22 03:44:51 +0000276 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000277 if (!func_type)
278 return nullptr;
279
Aaron Smithe664b5d2018-03-19 21:14:19 +0000280 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000281
Aaron Smith7ac1c782018-02-09 05:31:28 +0000282 Mangled mangled = GetMangledForPDBFunc(pdb_func);
283
Aaron Smithc8316ed2018-03-22 03:44:51 +0000284 FunctionSP func_sp =
285 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
286 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000287
288 sc.comp_unit->AddFunction(func_sp);
289 return func_sp.get();
290}
291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292size_t SymbolFilePDB::ParseCompileUnitFunctions(
293 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000294 lldbassert(sc.comp_unit);
295 size_t func_added = 0;
296 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
297 if (!compiland_up)
298 return 0;
299 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
300 if (!results_up)
301 return 0;
302 while (auto pdb_func_up = results_up->getNext()) {
303 auto func_sp =
304 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
305 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000306 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000307 ++func_added;
308 }
309 }
310 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313bool SymbolFilePDB::ParseCompileUnitLineTable(
314 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000315 lldbassert(sc.comp_unit);
316 if (sc.comp_unit->GetLineTable())
317 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000318 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000319}
320
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321bool SymbolFilePDB::ParseCompileUnitDebugMacros(
322 const lldb_private::SymbolContext &sc) {
323 // PDB doesn't contain information about macros
324 return false;
325}
326
327bool SymbolFilePDB::ParseCompileUnitSupportFiles(
328 const lldb_private::SymbolContext &sc,
329 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000330 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000333 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
334 // second time seems like a waste. Unfortunately, there's no good way around
335 // this short of a moderate refactor since SymbolVendor depends on being able
336 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000337 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
338 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000339 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000340 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000342 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343
344 while (auto file = files->getNext()) {
Aaron Smith10a02572018-01-13 06:58:18 +0000345 FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
346 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347 }
348 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000349}
350
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351bool SymbolFilePDB::ParseImportedModules(
352 const lldb_private::SymbolContext &sc,
353 std::vector<lldb_private::ConstString> &imported_modules) {
354 // PDB does not yet support module debug info
355 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000356}
357
Aaron Smithc8316ed2018-03-22 03:44:51 +0000358static size_t ParseFunctionBlocksForPDBSymbol(
359 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
360 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
361 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000362 assert(pdb_symbol && parent_block);
363
364 size_t num_added = 0;
365 switch (pdb_symbol->getSymTag()) {
366 case PDB_SymType::Block:
367 case PDB_SymType::Function: {
368 Block *block = nullptr;
369 auto &raw_sym = pdb_symbol->getRawSymbol();
370 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
371 if (pdb_func->hasNoInlineAttribute())
372 break;
373 if (is_top_parent)
374 block = parent_block;
375 else
376 break;
377 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
378 auto uid = pdb_symbol->getSymIndexId();
379 if (parent_block->FindBlockByID(uid))
380 break;
381 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
382 break;
383
384 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
385 parent_block->AddChild(block_sp);
386 block = block_sp.get();
387 } else
388 llvm_unreachable("Unexpected PDB symbol!");
389
Aaron Smithc8316ed2018-03-22 03:44:51 +0000390 block->AddRange(Block::Range(
391 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000392 block->FinalizeRanges();
393 ++num_added;
394
395 auto results_up = pdb_symbol->findAllChildren();
396 if (!results_up)
397 break;
398 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000399 num_added += ParseFunctionBlocksForPDBSymbol(
400 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000401 }
402 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000403 default:
404 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000405 }
406 return num_added;
407}
408
Zachary Turner74e08ca2016-03-02 22:05:52 +0000409size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000411 lldbassert(sc.comp_unit && sc.function);
412 size_t num_added = 0;
413 auto uid = sc.function->GetID();
414 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
415 if (!pdb_func_up)
416 return 0;
417 Block &parent_block = sc.function->GetBlock(false);
418 num_added =
419 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
420 pdb_func_up.get(), &parent_block, true);
421 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000422}
423
Kate Stoneb9c1b512016-09-06 20:57:50 +0000424size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000425 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000426 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000427 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000428
429 size_t num_added = 0;
430 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
431 if (!compiland)
432 return 0;
433
434 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
435 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000436 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
437 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000438 for (auto tag : tags_to_search) {
439 results = raw_sym.findAllChildren(tag);
440 if (!results || results->getChildCount() == 0)
441 continue;
442 while (auto symbol = results->getNext()) {
443 switch (symbol->getSymTag()) {
444 case PDB_SymType::Enum:
445 case PDB_SymType::UDT:
446 case PDB_SymType::Typedef:
447 break;
448 default:
449 continue;
450 }
451
452 // This should cause the type to get cached and stored in the `m_types`
453 // lookup.
454 if (!ResolveTypeUID(symbol->getSymIndexId()))
455 continue;
456
457 ++num_added;
458 }
Aaron Smithec40f812018-01-23 20:35:19 +0000459 }
Aaron Smith66b84072018-03-14 04:05:27 +0000460 };
Aaron Smithec40f812018-01-23 20:35:19 +0000461
Aaron Smith66b84072018-03-14 04:05:27 +0000462 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000463 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
464 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000465 if (!pdb_func)
466 return 0;
467 ParseTypesByTagFn(*pdb_func);
468 } else {
469 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000470
Aaron Smith66b84072018-03-14 04:05:27 +0000471 // Also parse global types particularly coming from this compiland.
472 // Unfortunately, PDB has no compiland information for each global type.
473 // We have to parse them all. But ensure we only do this once.
474 static bool parse_all_global_types = false;
475 if (!parse_all_global_types) {
476 ParseTypesByTagFn(*m_global_scope_up);
477 parse_all_global_types = true;
478 }
Aaron Smithec40f812018-01-23 20:35:19 +0000479 }
480 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000481}
482
483size_t
484SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
485 // TODO: Implement this
486 return size_t();
487}
488
489lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
490 auto find_result = m_types.find(type_uid);
491 if (find_result != m_types.end())
492 return find_result->second.get();
493
494 TypeSystem *type_system =
495 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
496 ClangASTContext *clang_type_system =
497 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
498 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000499 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000500 PDBASTParser *pdb =
501 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
502 if (!pdb)
503 return nullptr;
504
505 auto pdb_type = m_session_up->getSymbolById(type_uid);
506 if (pdb_type == nullptr)
507 return nullptr;
508
509 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000510 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000511 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000512 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000513 if (type_list)
514 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000515 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000517}
518
Kate Stoneb9c1b512016-09-06 20:57:50 +0000519bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
520 // TODO: Implement this
521 return false;
522}
523
524lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
525 return lldb_private::CompilerDecl();
526}
527
528lldb_private::CompilerDeclContext
529SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
530 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000531 // improve this later but it's not easy because PDB doesn't provide a high
532 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000533 return *m_tu_decl_ctx_up;
534}
535
536lldb_private::CompilerDeclContext
537SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
538 return *m_tu_decl_ctx_up;
539}
540
541void SymbolFilePDB::ParseDeclsForContext(
542 lldb_private::CompilerDeclContext decl_ctx) {}
543
544uint32_t
545SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
546 uint32_t resolve_scope,
547 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000548 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000549 if (resolve_scope & eSymbolContextCompUnit ||
550 resolve_scope & eSymbolContextVariable ||
551 resolve_scope & eSymbolContextFunction ||
552 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000553 resolve_scope & eSymbolContextLineEntry) {
554 addr_t file_vm_addr = so_addr.GetFileAddress();
555 auto symbol_up =
556 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
557 if (!symbol_up)
558 return 0;
559
560 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
561 if (!cu_sp) {
562 if (resolved_flags | eSymbolContextVariable) {
563 // TODO: Resolve variables
564 }
565 return 0;
566 }
567 sc.comp_unit = cu_sp.get();
568 resolved_flags |= eSymbolContextCompUnit;
569 lldbassert(sc.module_sp == cu_sp->GetModule());
570
571 switch (symbol_up->getSymTag()) {
572 case PDB_SymType::Function:
573 if (resolve_scope & eSymbolContextFunction) {
574 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
575 assert(pdb_func);
576 auto func_uid = pdb_func->getSymIndexId();
577 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
578 if (sc.function == nullptr)
Aaron Smithe664b5d2018-03-19 21:14:19 +0000579 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000580 if (sc.function) {
581 resolved_flags |= eSymbolContextFunction;
582 if (resolve_scope & eSymbolContextBlock) {
583 Block &block = sc.function->GetBlock(true);
584 sc.block = block.FindBlockByID(sc.function->GetID());
585 if (sc.block)
586 resolved_flags |= eSymbolContextBlock;
587 }
588 }
589 }
590 break;
591 default:
592 break;
593 }
594
595 if (resolve_scope & eSymbolContextLineEntry) {
596 if (auto *line_table = sc.comp_unit->GetLineTable()) {
597 Address addr(so_addr);
598 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
599 resolved_flags |= eSymbolContextLineEntry;
600 }
601 }
602 }
603 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604}
605
606uint32_t SymbolFilePDB::ResolveSymbolContext(
607 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
608 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000609 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000610 if (resolve_scope & lldb::eSymbolContextCompUnit) {
611 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000612 // file. For example, if `file_spec` is <vector>, then this should return
613 // all source files and header files that reference <vector>, either
614 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615 auto compilands = m_session_up->findCompilandsForSourceFile(
616 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
617
Aaron Smith10a02572018-01-13 06:58:18 +0000618 if (!compilands)
619 return 0;
620
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000621 // For each one, either find its previously parsed data or parse it afresh
622 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000623 while (auto compiland = compilands->getNext()) {
624 // If we're not checking inlines, then don't add line information for this
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000625 // file unless the FileSpec matches.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000626 // For inline functions, we don't have to match the FileSpec since they
627 // could be defined in headers other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000628 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000629 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000630 if (source_file.empty())
631 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000632 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
Aaron Smith10a02572018-01-13 06:58:18 +0000633 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
634 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000635 continue;
636 }
637
638 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000639 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000640 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000641 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000642 sc.comp_unit = cu.get();
643 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644
645 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000646 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000647 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
648 eSymbolContextLineEntry)) {
649 bool has_line_table = ParseCompileUnitLineTable(sc, line);
650
651 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
652 // The query asks for line entries, but we can't get them for the
653 // compile unit. This is not normal for `line` = 0. So just assert it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000654 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000655
656 // Current compiland does not have the requested line. Search next.
657 continue;
658 }
659
660 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
661 if (!has_line_table)
662 continue;
663
664 auto *line_table = sc.comp_unit->GetLineTable();
665 lldbassert(line_table);
666
667 uint32_t num_line_entries = line_table->GetSize();
668 // Skip the terminal line entry.
669 --num_line_entries;
670
671 // If `line `!= 0, see if we can resolve function for each line
672 // entry in the line table.
673 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
674 ++line_idx) {
675 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
676 continue;
677
678 auto file_vm_addr =
679 sc.line_entry.range.GetBaseAddress().GetFileAddress();
680 if (file_vm_addr == LLDB_INVALID_ADDRESS)
681 continue;
682
Aaron Smithc8316ed2018-03-22 03:44:51 +0000683 auto symbol_up = m_session_up->findSymbolByAddress(
684 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000685 if (symbol_up) {
686 auto func_uid = symbol_up->getSymIndexId();
687 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
688 if (sc.function == nullptr) {
689 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
690 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000691 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000692 }
693 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
694 Block &block = sc.function->GetBlock(true);
695 sc.block = block.FindBlockByID(sc.function->GetID());
696 }
697 }
698 sc_list.Append(sc);
699 }
700 } else if (has_line_table) {
701 // We can parse line table for the compile unit. But no query to
702 // resolve function or block. We append `sc` to the list anyway.
703 sc_list.Append(sc);
704 }
705 } else {
706 // No query for line entry, function or block. But we have a valid
707 // compile unit, append `sc` to the list.
708 sc_list.Append(sc);
709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000710 }
711 }
Aaron Smith10a02572018-01-13 06:58:18 +0000712 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000713}
714
715uint32_t SymbolFilePDB::FindGlobalVariables(
716 const lldb_private::ConstString &name,
717 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
718 uint32_t max_matches, lldb_private::VariableList &variables) {
719 return uint32_t();
720}
721
722uint32_t
723SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
724 bool append, uint32_t max_matches,
725 lldb_private::VariableList &variables) {
726 return uint32_t();
727}
728
Aaron Smithe664b5d2018-03-19 21:14:19 +0000729bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +0000730 bool include_inlines,
731 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000732 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +0000733 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000734 if (!sc.comp_unit)
735 return false;
736 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +0000737 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000738 if (!sc.function)
739 return false;
740
741 sc_list.Append(sc);
742 return true;
743}
744
745bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
746 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000747 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000748 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
749 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000750 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000751}
752
753void SymbolFilePDB::CacheFunctionNames() {
754 if (!m_func_full_names.IsEmpty())
755 return;
756
757 std::map<uint64_t, uint32_t> addr_ids;
758
759 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
760 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +0000761 if (pdb_func_up->isCompilerGenerated())
762 continue;
763
Aaron Smith7ac1c782018-02-09 05:31:28 +0000764 auto name = pdb_func_up->getName();
765 auto demangled_name = pdb_func_up->getUndecoratedName();
766 if (name.empty() && demangled_name.empty())
767 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000768
Aaron Smithf76fe682018-03-07 03:16:50 +0000769 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000770 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
771 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
772
773 if (auto parent = pdb_func_up->getClassParent()) {
774
775 // PDB have symbols for class/struct methods or static methods in Enum
776 // Class. We won't bother to check if the parent is UDT or Enum here.
777 m_func_method_names.Append(ConstString(name), uid);
778
779 ConstString cstr_name(name);
780
781 // To search a method name, like NS::Class:MemberFunc, LLDB searches its
782 // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not
783 // have inforamtion of this, we extract base names and cache them by our
784 // own effort.
785 llvm::StringRef basename;
786 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
787 if (cpp_method.IsValid()) {
788 llvm::StringRef context;
789 basename = cpp_method.GetBasename();
790 if (basename.empty())
791 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
792 context, basename);
793 }
794
795 if (!basename.empty())
796 m_func_base_names.Append(ConstString(basename), uid);
797 else {
798 m_func_base_names.Append(ConstString(name), uid);
799 }
800
801 if (!demangled_name.empty())
802 m_func_full_names.Append(ConstString(demangled_name), uid);
803
804 } else {
805 // Handle not-method symbols.
806
807 // The function name might contain namespace, or its lexical scope. It
808 // is not safe to get its base name by applying same scheme as we deal
809 // with the method names.
810 // FIXME: Remove namespace if function is static in a scope.
811 m_func_base_names.Append(ConstString(name), uid);
812
813 if (name == "main") {
814 m_func_full_names.Append(ConstString(name), uid);
815
816 if (!demangled_name.empty() && name != demangled_name) {
817 m_func_full_names.Append(ConstString(demangled_name), uid);
818 m_func_base_names.Append(ConstString(demangled_name), uid);
819 }
820 } else if (!demangled_name.empty()) {
821 m_func_full_names.Append(ConstString(demangled_name), uid);
822 } else {
823 m_func_full_names.Append(ConstString(name), uid);
824 }
825 }
826 }
827 }
828
829 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +0000830 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000831 while (auto pub_sym_up = results_up->getNext()) {
832 if (!pub_sym_up->isFunction())
833 continue;
834 auto name = pub_sym_up->getName();
835 if (name.empty())
836 continue;
837
838 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000839 auto vm_addr = pub_sym_up->getVirtualAddress();
840
841 // PDB public symbol has mangled name for its associated function.
842 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
843 // Cache mangled name.
844 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
845 }
846 }
847 }
848 }
849 // Sort them before value searching is working properly
850 m_func_full_names.Sort();
851 m_func_full_names.SizeToFit();
852 m_func_method_names.Sort();
853 m_func_method_names.SizeToFit();
854 m_func_base_names.Sort();
855 m_func_base_names.SizeToFit();
856}
857
Kate Stoneb9c1b512016-09-06 20:57:50 +0000858uint32_t SymbolFilePDB::FindFunctions(
859 const lldb_private::ConstString &name,
860 const lldb_private::CompilerDeclContext *parent_decl_ctx,
861 uint32_t name_type_mask, bool include_inlines, bool append,
862 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000863 if (!append)
864 sc_list.Clear();
865 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
866
867 if (name_type_mask == eFunctionNameTypeNone)
868 return 0;
869 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
870 return 0;
871 if (name.IsEmpty())
872 return 0;
873
874 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000875 if (name_type_mask & eFunctionNameTypeFull ||
876 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000877 name_type_mask & eFunctionNameTypeMethod) {
878 CacheFunctionNames();
879
880 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000881 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
882 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000883 std::vector<uint32_t> ids;
884 if (Names.GetValues(name, ids)) {
885 for (auto id : ids) {
886 if (resolved_ids.find(id) == resolved_ids.end()) {
887 if (ResolveFunction(id, include_inlines, sc_list))
888 resolved_ids.insert(id);
889 }
890 }
891 }
892 };
893 if (name_type_mask & eFunctionNameTypeFull) {
894 ResolveFn(m_func_full_names);
895 }
896 if (name_type_mask & eFunctionNameTypeBase) {
897 ResolveFn(m_func_base_names);
898 }
899 if (name_type_mask & eFunctionNameTypeMethod) {
900 ResolveFn(m_func_method_names);
901 }
902 }
903 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000904}
905
906uint32_t
907SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
908 bool include_inlines, bool append,
909 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000910 if (!append)
911 sc_list.Clear();
912 if (!regex.IsValid())
913 return 0;
914
915 auto old_size = sc_list.GetSize();
916 CacheFunctionNames();
917
918 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000919 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
920 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000921 std::vector<uint32_t> ids;
922 if (Names.GetValues(regex, ids)) {
923 for (auto id : ids) {
924 if (resolved_ids.find(id) == resolved_ids.end())
925 if (ResolveFunction(id, include_inlines, sc_list))
926 resolved_ids.insert(id);
927 }
928 }
929 };
930 ResolveFn(m_func_full_names);
931 ResolveFn(m_func_base_names);
932
933 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000934}
935
936void SymbolFilePDB::GetMangledNamesForFunction(
937 const std::string &scope_qualified_name,
938 std::vector<lldb_private::ConstString> &mangled_names) {}
939
940uint32_t SymbolFilePDB::FindTypes(
941 const lldb_private::SymbolContext &sc,
942 const lldb_private::ConstString &name,
943 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
944 uint32_t max_matches,
945 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
946 lldb_private::TypeMap &types) {
947 if (!append)
948 types.Clear();
949 if (!name)
950 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000951 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
952 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000953
954 searched_symbol_files.clear();
955 searched_symbol_files.insert(this);
956
957 std::string name_str = name.AsCString();
958
Aaron Smith86e94342017-12-22 05:26:50 +0000959 // There is an assumption 'name' is not a regex
960 FindTypesByName(name_str, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +0000961
Kate Stoneb9c1b512016-09-06 20:57:50 +0000962 return types.GetSize();
963}
964
Aaron Smithc8316ed2018-03-22 03:44:51 +0000965void SymbolFilePDB::FindTypesByRegex(
966 const lldb_private::RegularExpression &regex, uint32_t max_matches,
967 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000968 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000969 // space as much as possible since this searches EVERYTHING in the PDB,
970 // manually doing regex comparisons. PDB library isn't optimized for regex
971 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000973 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
975 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 std::unique_ptr<IPDBEnumSymbols> results;
977
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 uint32_t matches = 0;
979
980 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +0000981 results = m_global_scope_up->findAllChildren(tag);
982 if (!results)
983 continue;
984
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 while (auto result = results->getNext()) {
986 if (max_matches > 0 && matches >= max_matches)
987 break;
988
989 std::string type_name;
990 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
991 type_name = enum_type->getName();
992 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +0000993 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000994 type_name = typedef_type->getName();
995 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
996 type_name = class_type->getName();
997 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000998 // We're looking only for types that have names. Skip symbols, as well
999 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001000 continue;
1001 }
1002
Aaron Smith86e94342017-12-22 05:26:50 +00001003 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 continue;
1005
1006 // This should cause the type to get cached and stored in the `m_types`
1007 // lookup.
1008 if (!ResolveTypeUID(result->getSymIndexId()))
1009 continue;
1010
1011 auto iter = m_types.find(result->getSymIndexId());
1012 if (iter == m_types.end())
1013 continue;
1014 types.Insert(iter->second);
1015 ++matches;
1016 }
1017 }
1018}
1019
1020void SymbolFilePDB::FindTypesByName(const std::string &name,
1021 uint32_t max_matches,
1022 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001024 if (name.empty())
1025 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001026 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1027 PDB_NameSearchFlags::NS_Default);
1028 if (!results)
1029 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030
1031 uint32_t matches = 0;
1032
1033 while (auto result = results->getNext()) {
1034 if (max_matches > 0 && matches >= max_matches)
1035 break;
1036 switch (result->getSymTag()) {
1037 case PDB_SymType::Enum:
1038 case PDB_SymType::UDT:
1039 case PDB_SymType::Typedef:
1040 break;
1041 default:
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001042 // We're looking only for types that have names. Skip symbols, as well as
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 // unnamed types such as arrays, pointers, etc.
1044 continue;
1045 }
1046
1047 // This should cause the type to get cached and stored in the `m_types`
1048 // lookup.
1049 if (!ResolveTypeUID(result->getSymIndexId()))
1050 continue;
1051
1052 auto iter = m_types.find(result->getSymIndexId());
1053 if (iter == m_types.end())
1054 continue;
1055 types.Insert(iter->second);
1056 ++matches;
1057 }
1058}
1059
1060size_t SymbolFilePDB::FindTypes(
1061 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1062 lldb_private::TypeMap &types) {
1063 return 0;
1064}
1065
Aaron Smithec40f812018-01-23 20:35:19 +00001066lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1067 return m_obj_file->GetModule()->GetTypeList();
1068}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069
Aaron Smithc8316ed2018-03-22 03:44:51 +00001070void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1071 uint32_t type_mask,
1072 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001073 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001074 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001075 case PDB_SymType::ArrayType:
1076 can_parse = ((type_mask & eTypeClassArray) != 0);
1077 break;
1078 case PDB_SymType::BuiltinType:
1079 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1080 break;
1081 case PDB_SymType::Enum:
1082 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1083 break;
1084 case PDB_SymType::Function:
1085 case PDB_SymType::FunctionSig:
1086 can_parse = ((type_mask & eTypeClassFunction) != 0);
1087 break;
1088 case PDB_SymType::PointerType:
1089 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1090 eTypeClassMemberPointer)) != 0);
1091 break;
1092 case PDB_SymType::Typedef:
1093 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1094 break;
1095 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001096 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001097 assert(udt);
1098 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001099 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1100 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001101 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001102 default:
1103 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001104 }
1105
1106 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001107 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001108 auto result =
1109 std::find(type_collection.begin(), type_collection.end(), type);
1110 if (result == type_collection.end())
1111 type_collection.push_back(type);
1112 }
1113 }
1114
Aaron Smithe664b5d2018-03-19 21:14:19 +00001115 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001116 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001117 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001118}
1119
Kate Stoneb9c1b512016-09-06 20:57:50 +00001120size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1121 uint32_t type_mask,
1122 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001123 TypeCollection type_collection;
1124 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001125 CompileUnit *cu =
1126 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001127 if (cu) {
1128 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001129 if (!compiland_up)
1130 return 0;
1131 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001132 } else {
1133 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1134 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001135 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001136 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1137 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001138 }
1139 }
1140 }
1141
1142 for (auto type : type_collection) {
1143 type->GetForwardCompilerType();
1144 type_list.Insert(type->shared_from_this());
1145 }
1146 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001147}
1148
1149lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001150SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1151 auto type_system =
1152 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1153 if (type_system)
1154 type_system->SetSymbolFile(this);
1155 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001156}
1157
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1159 const lldb_private::SymbolContext &sc,
1160 const lldb_private::ConstString &name,
1161 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1162 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001163}
1164
Kate Stoneb9c1b512016-09-06 20:57:50 +00001165lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1166 static ConstString g_name("pdb");
1167 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001168}
1169
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1171
1172IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1173
1174const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1175 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001176}
1177
Aaron Smithc8316ed2018-03-22 03:44:51 +00001178lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1179 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 auto found_cu = m_comp_units.find(id);
1181 if (found_cu != m_comp_units.end())
1182 return found_cu->second;
1183
Aaron Smith10a02572018-01-13 06:58:18 +00001184 auto compiland_up = GetPDBCompilandByUID(id);
1185 if (!compiland_up)
1186 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001187
1188 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001189 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190 if (!details)
1191 lang = lldb::eLanguageTypeC_plus_plus;
1192 else
1193 lang = TranslateLanguage(details->getLanguage());
1194
Aaron Smithf76fe682018-03-07 03:16:50 +00001195 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1196 return CompUnitSP();
1197
Aaron Smith487b0c62018-03-20 00:18:22 +00001198 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001199 if (path.empty())
1200 return CompUnitSP();
1201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 // Don't support optimized code for now, DebugInfoPDB does not return this
1203 // information.
1204 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001205 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1206 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001207
1208 if (!cu_sp)
1209 return CompUnitSP();
1210
1211 m_comp_units.insert(std::make_pair(id, cu_sp));
1212 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001213 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001214 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001215 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1216 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001217 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001218}
1219
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220bool SymbolFilePDB::ParseCompileUnitLineTable(
1221 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001222 lldbassert(sc.comp_unit);
1223
1224 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1225 if (!compiland_up)
1226 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001227
Kate Stoneb9c1b512016-09-06 20:57:50 +00001228 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001229 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
1230 // a globally unique idenfitifier in the namespace of the PDB. So, we have to
1231 // do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001233 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001235
Aaron Smith10a02572018-01-13 06:58:18 +00001236 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001237 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001238 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1239 if (!files)
1240 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001241
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 // For each source and header file, create a LineSequence for contributions to
Aaron Smith10a02572018-01-13 06:58:18 +00001243 // the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001244 while (auto file = files->getNext()) {
1245 std::unique_ptr<LineSequence> sequence(
1246 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001247 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1248 if (!lines)
1249 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 uint64_t prev_addr;
1253 uint32_t prev_length;
1254 uint32_t prev_line;
1255 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001256
Kate Stoneb9c1b512016-09-06 20:57:50 +00001257 for (int i = 0; i < entry_count; ++i) {
1258 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001259
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 uint64_t lno = line->getLineNumber();
1261 uint64_t addr = line->getVirtualAddress();
1262 uint32_t length = line->getLength();
1263 uint32_t source_id = line->getSourceFileId();
1264 uint32_t col = line->getColumnNumber();
1265 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001266
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267 // There was a gap between the current entry and the previous entry if the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001268 // addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001269 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001270
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001272 // of the previous entry's address range if the current entry resulted in
1273 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001274 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1275 line_table->AppendLineEntryToSequence(
1276 sequence.get(), prev_addr + prev_length, prev_line, 0,
1277 prev_source_idx, false, false, false, false, true);
1278 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001279
Kate Stoneb9c1b512016-09-06 20:57:50 +00001280 if (ShouldAddLine(match_line, lno, length)) {
1281 bool is_statement = line->isStatement();
1282 bool is_prologue = false;
1283 bool is_epilogue = false;
1284 auto func =
1285 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1286 if (func) {
1287 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001288 if (prologue)
1289 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001290
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001292 if (epilogue)
1293 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001294 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001295
Kate Stoneb9c1b512016-09-06 20:57:50 +00001296 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1297 source_idx, is_statement, false,
1298 is_prologue, is_epilogue, false);
1299 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 prev_addr = addr;
1302 prev_length = length;
1303 prev_line = lno;
1304 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001305 }
1306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1308 // The end is always a terminal entry, so insert it regardless.
1309 line_table->AppendLineEntryToSequence(
1310 sequence.get(), prev_addr + prev_length, prev_line, 0,
1311 prev_source_idx, false, false, false, false, true);
1312 }
1313
1314 line_table->InsertSequence(sequence.release());
1315 }
1316
Aaron Smith10a02572018-01-13 06:58:18 +00001317 if (line_table->GetSize()) {
1318 sc.comp_unit->SetLineTable(line_table.release());
1319 return true;
1320 }
1321 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001322}
1323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001325 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1327 // This is a hack, but we need to convert the source id into an index into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001328 // support files array. We don't want to do path comparisons to avoid
1329 // basename / full path issues that may or may not even be a problem, so we
1330 // use the globally unique source file identifiers. Ideally we could use the
1331 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001332 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1333 if (!source_files)
1334 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 while (auto file = source_files->getNext()) {
1338 uint32_t source_id = file->getUniqueId();
1339 index_map[source_id] = index++;
1340 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001341}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001342
1343lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1344 const lldb_private::Address &so_addr) {
1345 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1346 if (file_vm_addr == LLDB_INVALID_ADDRESS)
1347 return nullptr;
1348
1349 auto lines_up =
1350 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200);
1351 if (!lines_up)
1352 return nullptr;
1353
1354 auto first_line_up = lines_up->getNext();
1355 if (!first_line_up)
1356 return nullptr;
1357 auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId());
1358 if (compiland_up) {
1359 return ParseCompileUnitForUID(compiland_up->getSymIndexId());
1360 }
1361
1362 return nullptr;
1363}
1364
1365Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001366SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001367 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001368 auto func_name = pdb_func.getName();
1369 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001370 std::string func_decorated_name;
1371
1372 // Seek from public symbols for non-static function's decorated name if any.
1373 // For static functions, they don't have undecorated names and aren't exposed
1374 // in Public Symbols either.
1375 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001376 auto result_up = m_global_scope_up->findChildren(
1377 PDB_SymType::PublicSymbol, func_undecorated_name,
1378 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001379 if (result_up) {
1380 while (auto symbol_up = result_up->getNext()) {
1381 // For a public symbol, it is unique.
1382 lldbassert(result_up->getChildCount() == 1);
1383 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001384 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1385 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001386 if (pdb_public_sym->isFunction()) {
1387 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001388 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001389 }
1390 }
1391 }
1392 }
1393 }
1394 if (!func_decorated_name.empty()) {
1395 mangled.SetMangledName(ConstString(func_decorated_name));
1396
1397 // For MSVC, format of C funciton's decorated name depends on calling
1398 // conventon. Unfortunately none of the format is recognized by current
1399 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1400 // `__purecall` is retrieved as both its decorated and
1401 // undecorated name (using PDBSymbolFunc::getUndecoratedName method).
1402 // However `__purecall` string is not treated as mangled in LLDB
1403 // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method
1404 // will fail internally and caches an empty string as its undecorated
1405 // name. So we will face a contradition here for the same symbol:
1406 // non-empty undecorated name from PDB
1407 // empty undecorated name from LLDB
1408 if (!func_undecorated_name.empty() &&
1409 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1410 mangled.SetDemangledName(ConstString(func_undecorated_name));
1411
1412 // LLDB uses several flags to control how a C++ decorated name is
1413 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled.
1414 // So the yielded name could be different from what we retrieve from
1415 // PDB source unless we also apply same flags in getting undecorated
1416 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1417 if (!func_undecorated_name.empty() &&
1418 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1419 ConstString(func_undecorated_name))
1420 mangled.SetDemangledName(ConstString(func_undecorated_name));
1421 } else if (!func_undecorated_name.empty()) {
1422 mangled.SetDemangledName(ConstString(func_undecorated_name));
1423 } else if (!func_name.empty())
1424 mangled.SetValue(ConstString(func_name), false);
1425
1426 return mangled;
1427}
1428
1429bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1430 const lldb_private::CompilerDeclContext *decl_ctx) {
1431 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1432 return true;
1433
1434 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1435 if (!decl_ctx_type_system)
1436 return false;
1437 TypeSystem *type_system = GetTypeSystemForLanguage(
1438 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1439 if (decl_ctx_type_system == type_system)
1440 return true; // The type systems match, return true
1441
1442 return false;
1443}