blob: cb1b66abde3ab6c37221e580b848c8c761d115e1 [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"
Aaron Smithec40f812018-01-23 20:35:19 +000022#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000023#include "lldb/Symbol/TypeMap.h"
Aaron Smithcab0d232018-05-23 01:52:42 +000024#include "lldb/Symbol/Variable.h"
Aaron Smith86e94342017-12-22 05:26:50 +000025#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000026
Pavel Labathb8d8c622016-05-09 11:07:43 +000027#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000028#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000029#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
30#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000031#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000033#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000035#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000036#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
37#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000038#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000039#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
41#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
42#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000043#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000044#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
46#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
47
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +000048#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" // For IsCPPMangledName
Zachary Turner307f5ae2018-10-12 19:47:13 +000049#include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
Zachary Turner42dff792016-04-15 00:21:26 +000050#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Jonas Devlieghere924d5602018-07-13 10:29:27 +000051#include "Plugins/SymbolFile/PDB/PDBLocationToDWARFExpression.h"
Zachary Turner42dff792016-04-15 00:21:26 +000052
53#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000054
Aaron Smith10a02572018-01-13 06:58:18 +000055using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000056using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000057using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059namespace {
60lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
61 switch (lang) {
62 case PDB_Lang::Cpp:
63 return lldb::LanguageType::eLanguageTypeC_plus_plus;
64 case PDB_Lang::C:
65 return lldb::LanguageType::eLanguageTypeC;
66 default:
67 return lldb::LanguageType::eLanguageTypeUnknown;
68 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000069}
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
72 uint32_t addr_length) {
73 return ((requested_line == 0 || actual_line == requested_line) &&
74 addr_length > 0);
75}
Aaron Smithc8316ed2018-03-22 03:44:51 +000076} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000077
Zachary Turner307f5ae2018-10-12 19:47:13 +000078static bool ShouldUseNativeReader() {
79#if !defined(_WIN32)
80 return true;
81#endif
82 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
83 return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
84 use_native.equals_lower("1") || use_native.equals_lower("true");
85}
86
Kate Stoneb9c1b512016-09-06 20:57:50 +000087void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000088 if (ShouldUseNativeReader()) {
89 npdb::SymbolFileNativePDB::Initialize();
90 } else {
91 PluginManager::RegisterPlugin(GetPluginNameStatic(),
92 GetPluginDescriptionStatic(), CreateInstance,
93 DebuggerInitialize);
94 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000095}
96
Kate Stoneb9c1b512016-09-06 20:57:50 +000097void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000098 if (ShouldUseNativeReader()) {
99 npdb::SymbolFileNativePDB::Terminate();
100 } else {
101 PluginManager::UnregisterPlugin(CreateInstance);
102 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000103}
104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
106
107lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
108 static ConstString g_name("pdb");
109 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000110}
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112const char *SymbolFilePDB::GetPluginDescriptionStatic() {
113 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000114}
115
116lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
118 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000119}
120
121SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000122 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
123 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000128 uint32_t abilities = 0;
129 if (!m_obj_file)
130 return 0;
131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 if (!m_session_up) {
133 // Lazily load and match the PDB file, but only do this once.
134 std::string exePath = m_obj_file->GetFileSpec().GetPath();
135 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
136 m_session_up);
137 if (error) {
138 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000139 auto module_sp = m_obj_file->GetModule();
140 if (!module_sp)
141 return 0;
142 // See if any symbol file is specified through `--symfile` option.
143 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
144 if (!symfile)
145 return 0;
146 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000147 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000148 if (error) {
149 llvm::consumeError(std::move(error));
150 return 0;
151 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000152 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000154 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000155 return 0;
156
157 auto enum_tables_up = m_session_up->getEnumTables();
158 if (!enum_tables_up)
159 return 0;
160 while (auto table_up = enum_tables_up->getNext()) {
161 if (table_up->getItemCount() == 0)
162 continue;
163 auto type = table_up->getTableType();
164 switch (type) {
165 case PDB_TableType::Symbols:
166 // This table represents a store of symbols with types listed in
167 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000168 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
169 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000170 break;
171 case PDB_TableType::LineNumbers:
172 abilities |= LineTables;
173 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000174 default:
175 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000176 }
177 }
178 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181void SymbolFilePDB::InitializeObject() {
182 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000183 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000185 if (!m_global_scope_up)
186 m_global_scope_up = m_session_up->getGlobalScope();
187 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000188
Kate Stoneb9c1b512016-09-06 20:57:50 +0000189 TypeSystem *type_system =
190 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
191 ClangASTContext *clang_type_system =
192 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000193 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
195 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000196}
197
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198uint32_t SymbolFilePDB::GetNumCompileUnits() {
199 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000200 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
201 if (!compilands)
202 return 0;
203
204 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000205 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
206 // found as a child of the global scope (PDB executable). Usually, such
207 // compilands contain `thunk` symbols in which we are not interested for
208 // now. However we still count them in the compiland list. If we perform
209 // any compiland related activity, like finding symbols through
210 // llvm::pdb::IPDBSession methods, such compilands will all be searched
211 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000215 // PDB. Ignore this special compile unit for our purposes, if it is there.
216 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000217 auto last_compiland_up =
218 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
219 lldbassert(last_compiland_up.get());
220 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 if (name == "* Linker *")
222 --m_cached_compile_unit_count;
223 }
224 return m_cached_compile_unit_count;
225}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000226
Aaron Smith10a02572018-01-13 06:58:18 +0000227void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000228 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000229 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
230 if (!results_up)
231 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000232 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000233 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000234 auto compiland_up = results_up->getChildAtIndex(cu_idx);
235 if (!compiland_up)
236 continue;
237 if (compiland_up->getSymIndexId() == uid) {
238 index = cu_idx;
239 return;
240 }
241 }
242 index = UINT32_MAX;
243 return;
244}
245
246std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
247SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
248 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
249}
250
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000252 if (index >= GetNumCompileUnits())
253 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254
Aaron Smith10a02572018-01-13 06:58:18 +0000255 // Assuming we always retrieve same compilands listed in same order through
256 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
257 // compile unit makes no sense.
258 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
259 if (!results)
260 return CompUnitSP();
261 auto compiland_up = results->getChildAtIndex(index);
262 if (!compiland_up)
263 return CompUnitSP();
264 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000265}
266
267lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
269 // What fields should I expect to be filled out on the SymbolContext? Is it
270 // safe to assume that `sc.comp_unit` is valid?
271 if (!sc.comp_unit)
272 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000273
Aaron Smith10a02572018-01-13 06:58:18 +0000274 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
275 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000277 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 if (!details)
279 return lldb::eLanguageTypeUnknown;
280 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000281}
282
Aaron Smithc8316ed2018-03-22 03:44:51 +0000283lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
284 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000285 lldbassert(sc.comp_unit && sc.module_sp.get());
286
Aaron Smithe664b5d2018-03-19 21:14:19 +0000287 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000288 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000289 return nullptr;
290
Aaron Smithe664b5d2018-03-19 21:14:19 +0000291 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000292 AddressRange func_range =
293 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000294 if (!func_range.GetBaseAddress().IsValid())
295 return nullptr;
296
Aaron Smithc8316ed2018-03-22 03:44:51 +0000297 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000298 if (!func_type)
299 return nullptr;
300
Aaron Smithe664b5d2018-03-19 21:14:19 +0000301 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000302
Aaron Smith7ac1c782018-02-09 05:31:28 +0000303 Mangled mangled = GetMangledForPDBFunc(pdb_func);
304
Aaron Smithc8316ed2018-03-22 03:44:51 +0000305 FunctionSP func_sp =
306 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
307 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000308
309 sc.comp_unit->AddFunction(func_sp);
310 return func_sp.get();
311}
312
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313size_t SymbolFilePDB::ParseCompileUnitFunctions(
314 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000315 lldbassert(sc.comp_unit);
316 size_t func_added = 0;
317 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
318 if (!compiland_up)
319 return 0;
320 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
321 if (!results_up)
322 return 0;
323 while (auto pdb_func_up = results_up->getNext()) {
324 auto func_sp =
325 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
326 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000327 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000328 ++func_added;
329 }
330 }
331 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000332}
333
Kate Stoneb9c1b512016-09-06 20:57:50 +0000334bool SymbolFilePDB::ParseCompileUnitLineTable(
335 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000336 lldbassert(sc.comp_unit);
337 if (sc.comp_unit->GetLineTable())
338 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000340}
341
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342bool SymbolFilePDB::ParseCompileUnitDebugMacros(
343 const lldb_private::SymbolContext &sc) {
344 // PDB doesn't contain information about macros
345 return false;
346}
347
348bool SymbolFilePDB::ParseCompileUnitSupportFiles(
349 const lldb_private::SymbolContext &sc,
350 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000351 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000352
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000354 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
355 // second time seems like a waste. Unfortunately, there's no good way around
356 // this short of a moderate refactor since SymbolVendor depends on being able
357 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000358 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
359 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000360 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000361 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000362 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000363 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000364
365 while (auto file = files->getNext()) {
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000366 FileSpec spec(file->getFileName(), false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000367 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000369
370 // LLDB uses the DWARF-like file numeration (one based),
371 // the zeroth file is the compile unit itself
372 support_files.Insert(0, *sc.comp_unit);
373
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000375}
376
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377bool SymbolFilePDB::ParseImportedModules(
378 const lldb_private::SymbolContext &sc,
379 std::vector<lldb_private::ConstString> &imported_modules) {
380 // PDB does not yet support module debug info
381 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000382}
383
Aaron Smithc8316ed2018-03-22 03:44:51 +0000384static size_t ParseFunctionBlocksForPDBSymbol(
385 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
386 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
387 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000388 assert(pdb_symbol && parent_block);
389
390 size_t num_added = 0;
391 switch (pdb_symbol->getSymTag()) {
392 case PDB_SymType::Block:
393 case PDB_SymType::Function: {
394 Block *block = nullptr;
395 auto &raw_sym = pdb_symbol->getRawSymbol();
396 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
397 if (pdb_func->hasNoInlineAttribute())
398 break;
399 if (is_top_parent)
400 block = parent_block;
401 else
402 break;
403 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
404 auto uid = pdb_symbol->getSymIndexId();
405 if (parent_block->FindBlockByID(uid))
406 break;
407 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
408 break;
409
410 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
411 parent_block->AddChild(block_sp);
412 block = block_sp.get();
413 } else
414 llvm_unreachable("Unexpected PDB symbol!");
415
Aaron Smithc8316ed2018-03-22 03:44:51 +0000416 block->AddRange(Block::Range(
417 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000418 block->FinalizeRanges();
419 ++num_added;
420
421 auto results_up = pdb_symbol->findAllChildren();
422 if (!results_up)
423 break;
424 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000425 num_added += ParseFunctionBlocksForPDBSymbol(
426 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000427 }
428 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000429 default:
430 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000431 }
432 return num_added;
433}
434
Zachary Turner74e08ca2016-03-02 22:05:52 +0000435size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000437 lldbassert(sc.comp_unit && sc.function);
438 size_t num_added = 0;
439 auto uid = sc.function->GetID();
440 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
441 if (!pdb_func_up)
442 return 0;
443 Block &parent_block = sc.function->GetBlock(false);
444 num_added =
445 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
446 pdb_func_up.get(), &parent_block, true);
447 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000448}
449
Kate Stoneb9c1b512016-09-06 20:57:50 +0000450size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000451 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000452 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000453 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000454
455 size_t num_added = 0;
456 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
457 if (!compiland)
458 return 0;
459
460 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
461 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000462 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
463 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000464 for (auto tag : tags_to_search) {
465 results = raw_sym.findAllChildren(tag);
466 if (!results || results->getChildCount() == 0)
467 continue;
468 while (auto symbol = results->getNext()) {
469 switch (symbol->getSymTag()) {
470 case PDB_SymType::Enum:
471 case PDB_SymType::UDT:
472 case PDB_SymType::Typedef:
473 break;
474 default:
475 continue;
476 }
477
478 // This should cause the type to get cached and stored in the `m_types`
479 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000480 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
481 // Resolve the type completely to avoid a completion
482 // (and so a list change, which causes an iterators invalidation)
483 // during a TypeList dumping
484 type->GetFullCompilerType();
485 ++num_added;
486 }
Aaron Smith66b84072018-03-14 04:05:27 +0000487 }
Aaron Smithec40f812018-01-23 20:35:19 +0000488 }
Aaron Smith66b84072018-03-14 04:05:27 +0000489 };
Aaron Smithec40f812018-01-23 20:35:19 +0000490
Aaron Smith66b84072018-03-14 04:05:27 +0000491 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000492 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
493 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000494 if (!pdb_func)
495 return 0;
496 ParseTypesByTagFn(*pdb_func);
497 } else {
498 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000499
Aaron Smith66b84072018-03-14 04:05:27 +0000500 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000501 // Unfortunately, PDB has no compiland information for each global type. We
502 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000503 static bool parse_all_global_types = false;
504 if (!parse_all_global_types) {
505 ParseTypesByTagFn(*m_global_scope_up);
506 parse_all_global_types = true;
507 }
Aaron Smithec40f812018-01-23 20:35:19 +0000508 }
509 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000510}
511
512size_t
513SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000514 if (!sc.comp_unit)
515 return 0;
516
517 size_t num_added = 0;
518 if (sc.function) {
519 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
520 sc.function->GetID());
521 if (!pdb_func)
522 return 0;
523
524 num_added += ParseVariables(sc, *pdb_func);
525 sc.function->GetBlock(false).SetDidParseVariables(true, true);
526 } else if (sc.comp_unit) {
527 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
528 if (!compiland)
529 return 0;
530
531 if (sc.comp_unit->GetVariableList(false))
532 return 0;
533
534 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
535 if (results && results->getChildCount()) {
536 while (auto result = results->getNext()) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000537 auto cu_id = GetCompilandId(*result);
Aaron Smithcab0d232018-05-23 01:52:42 +0000538 // FIXME: We are not able to determine variable's compile unit.
539 if (cu_id == 0)
540 continue;
541
542 if (cu_id == sc.comp_unit->GetID())
543 num_added += ParseVariables(sc, *result);
544 }
545 }
546
547 // FIXME: A `file static` or `global constant` variable appears both in
548 // compiland's children and global scope's children with unexpectedly
549 // different symbol's Id making it ambiguous.
550
551 // FIXME: 'local constant', for example, const char var[] = "abc", declared
552 // in a function scope, can't be found in PDB.
553
554 // Parse variables in this compiland.
555 num_added += ParseVariables(sc, *compiland);
556 }
557
558 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559}
560
561lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
562 auto find_result = m_types.find(type_uid);
563 if (find_result != m_types.end())
564 return find_result->second.get();
565
566 TypeSystem *type_system =
567 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
568 ClangASTContext *clang_type_system =
569 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
570 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000571 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000572 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000573 if (!pdb)
574 return nullptr;
575
576 auto pdb_type = m_session_up->getSymbolById(type_uid);
577 if (pdb_type == nullptr)
578 return nullptr;
579
580 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000581 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000582 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000583 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000584 if (type_list)
585 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000586 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000587 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000588}
589
Kate Stoneb9c1b512016-09-06 20:57:50 +0000590bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000591 std::lock_guard<std::recursive_mutex> guard(
592 GetObjectFile()->GetModule()->GetMutex());
593
594 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
595 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
596 if (!clang_ast_ctx)
597 return false;
598
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000599 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000600 if (!pdb)
601 return false;
602
603 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000604}
605
606lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000607 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
608 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
609 if (!clang_ast_ctx)
610 return CompilerDecl();
611
612 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
613 if (!pdb)
614 return CompilerDecl();
615
616 auto symbol = m_session_up->getSymbolById(uid);
617 if (!symbol)
618 return CompilerDecl();
619
620 auto decl = pdb->GetDeclForSymbol(*symbol);
621 if (!decl)
622 return CompilerDecl();
623
624 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625}
626
627lldb_private::CompilerDeclContext
628SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000629 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
630 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
631 if (!clang_ast_ctx)
632 return CompilerDeclContext();
633
634 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
635 if (!pdb)
636 return CompilerDeclContext();
637
638 auto symbol = m_session_up->getSymbolById(uid);
639 if (!symbol)
640 return CompilerDeclContext();
641
642 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
643 if (!decl_context)
644 return GetDeclContextContainingUID(uid);
645
646 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000647}
648
649lldb_private::CompilerDeclContext
650SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000651 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
652 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
653 if (!clang_ast_ctx)
654 return CompilerDeclContext();
655
656 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
657 if (!pdb)
658 return CompilerDeclContext();
659
660 auto symbol = m_session_up->getSymbolById(uid);
661 if (!symbol)
662 return CompilerDeclContext();
663
664 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
665 assert(decl_context);
666
667 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000668}
669
670void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000671 lldb_private::CompilerDeclContext decl_ctx) {
672 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
673 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
674 if (!clang_ast_ctx)
675 return;
676
677 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
678 if (!pdb)
679 return;
680
681 pdb->ParseDeclsForDeclContext(
682 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
683}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684
685uint32_t
686SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000687 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000688 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000689 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000690 if (resolve_scope & eSymbolContextCompUnit ||
691 resolve_scope & eSymbolContextVariable ||
692 resolve_scope & eSymbolContextFunction ||
693 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000694 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000695 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
696 if (!cu_sp) {
697 if (resolved_flags | eSymbolContextVariable) {
698 // TODO: Resolve variables
699 }
700 return 0;
701 }
702 sc.comp_unit = cu_sp.get();
703 resolved_flags |= eSymbolContextCompUnit;
704 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000705 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000706
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000707 if (resolve_scope & eSymbolContextFunction ||
708 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000709 addr_t file_vm_addr = so_addr.GetFileAddress();
710 auto symbol_up =
711 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
712 if (symbol_up) {
713 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
714 assert(pdb_func);
715 auto func_uid = pdb_func->getSymIndexId();
716 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
717 if (sc.function == nullptr)
718 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
719 if (sc.function) {
720 resolved_flags |= eSymbolContextFunction;
721 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000722 auto block_symbol = m_session_up->findSymbolByAddress(
723 file_vm_addr, PDB_SymType::Block);
724 auto block_id = block_symbol ? block_symbol->getSymIndexId()
725 : sc.function->GetID();
726 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000727 if (sc.block)
728 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000729 }
730 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000731 }
732 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000733
734 if (resolve_scope & eSymbolContextLineEntry) {
735 if (auto *line_table = sc.comp_unit->GetLineTable()) {
736 Address addr(so_addr);
737 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
738 resolved_flags |= eSymbolContextLineEntry;
739 }
740 }
741
Aaron Smith7ac1c782018-02-09 05:31:28 +0000742 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000743}
744
745uint32_t SymbolFilePDB::ResolveSymbolContext(
746 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000747 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000748 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000749 if (resolve_scope & lldb::eSymbolContextCompUnit) {
750 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000751 // file. For example, if `file_spec` is <vector>, then this should return
752 // all source files and header files that reference <vector>, either
753 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 auto compilands = m_session_up->findCompilandsForSourceFile(
755 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
756
Aaron Smith10a02572018-01-13 06:58:18 +0000757 if (!compilands)
758 return 0;
759
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000760 // For each one, either find its previously parsed data or parse it afresh
761 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000762 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000763 // If we're not checking inlines, then don't add line information for
764 // this file unless the FileSpec matches. For inline functions, we don't
765 // have to match the FileSpec since they could be defined in headers
766 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000767 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000768 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000769 if (source_file.empty())
770 continue;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000771 FileSpec this_spec(source_file, false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000772 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
773 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 continue;
775 }
776
777 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000778 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000779 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000780 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000781 sc.comp_unit = cu.get();
782 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783
784 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000785 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000786 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
787 eSymbolContextLineEntry)) {
788 bool has_line_table = ParseCompileUnitLineTable(sc, line);
789
790 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
791 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000792 // compile unit. This is not normal for `line` = 0. So just assert
793 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000794 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000795
796 // Current compiland does not have the requested line. Search next.
797 continue;
798 }
799
800 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
801 if (!has_line_table)
802 continue;
803
804 auto *line_table = sc.comp_unit->GetLineTable();
805 lldbassert(line_table);
806
807 uint32_t num_line_entries = line_table->GetSize();
808 // Skip the terminal line entry.
809 --num_line_entries;
810
Adrian Prantl05097242018-04-30 16:49:04 +0000811 // If `line `!= 0, see if we can resolve function for each line entry
812 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000813 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
814 ++line_idx) {
815 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
816 continue;
817
818 auto file_vm_addr =
819 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000820 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000821 continue;
822
Aaron Smithc8316ed2018-03-22 03:44:51 +0000823 auto symbol_up = m_session_up->findSymbolByAddress(
824 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000825 if (symbol_up) {
826 auto func_uid = symbol_up->getSymIndexId();
827 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
828 if (sc.function == nullptr) {
829 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
830 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000831 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000832 }
833 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
834 Block &block = sc.function->GetBlock(true);
835 sc.block = block.FindBlockByID(sc.function->GetID());
836 }
837 }
838 sc_list.Append(sc);
839 }
840 } else if (has_line_table) {
841 // We can parse line table for the compile unit. But no query to
842 // resolve function or block. We append `sc` to the list anyway.
843 sc_list.Append(sc);
844 }
845 } else {
846 // No query for line entry, function or block. But we have a valid
847 // compile unit, append `sc` to the list.
848 sc_list.Append(sc);
849 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000850 }
851 }
Aaron Smith10a02572018-01-13 06:58:18 +0000852 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853}
854
Aaron Smithcab0d232018-05-23 01:52:42 +0000855std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000856 // Cache public names at first
857 if (m_public_names.empty())
858 if (auto result_up =
859 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
860 while (auto symbol_up = result_up->getNext())
861 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
862 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000863
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000864 // Look up the name in the cache
865 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000866}
867
868VariableSP SymbolFilePDB::ParseVariableForPDBData(
869 const lldb_private::SymbolContext &sc,
870 const llvm::pdb::PDBSymbolData &pdb_data) {
871 VariableSP var_sp;
872 uint32_t var_uid = pdb_data.getSymIndexId();
873 auto result = m_variables.find(var_uid);
874 if (result != m_variables.end())
875 return result->second;
876
877 ValueType scope = eValueTypeInvalid;
878 bool is_static_member = false;
879 bool is_external = false;
880 bool is_artificial = false;
881
882 switch (pdb_data.getDataKind()) {
883 case PDB_DataKind::Global:
884 scope = eValueTypeVariableGlobal;
885 is_external = true;
886 break;
887 case PDB_DataKind::Local:
888 scope = eValueTypeVariableLocal;
889 break;
890 case PDB_DataKind::FileStatic:
891 scope = eValueTypeVariableStatic;
892 break;
893 case PDB_DataKind::StaticMember:
894 is_static_member = true;
895 scope = eValueTypeVariableStatic;
896 break;
897 case PDB_DataKind::Member:
898 scope = eValueTypeVariableStatic;
899 break;
900 case PDB_DataKind::Param:
901 scope = eValueTypeVariableArgument;
902 break;
903 case PDB_DataKind::Constant:
904 scope = eValueTypeConstResult;
905 break;
906 default:
907 break;
908 }
909
910 switch (pdb_data.getLocationType()) {
911 case PDB_LocType::TLS:
912 scope = eValueTypeVariableThreadLocal;
913 break;
914 case PDB_LocType::RegRel: {
915 // It is a `this` pointer.
916 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
917 scope = eValueTypeVariableArgument;
918 is_artificial = true;
919 }
920 } break;
921 default:
922 break;
923 }
924
925 Declaration decl;
926 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
927 if (auto lines = pdb_data.getLineNumbers()) {
928 if (auto first_line = lines->getNext()) {
929 uint32_t src_file_id = first_line->getSourceFileId();
930 auto src_file = m_session_up->getSourceFileById(src_file_id);
931 if (src_file) {
932 FileSpec spec(src_file->getFileName(), /*resolve_path*/ false);
933 decl.SetFile(spec);
934 decl.SetColumn(first_line->getColumnNumber());
935 decl.SetLine(first_line->getLineNumber());
936 }
937 }
938 }
939 }
940
941 Variable::RangeList ranges;
942 SymbolContextScope *context_scope = sc.comp_unit;
943 if (scope == eValueTypeVariableLocal) {
944 if (sc.function) {
945 context_scope = sc.function->GetBlock(true).FindBlockByID(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000946 pdb_data.getLexicalParentId());
Aaron Smithcab0d232018-05-23 01:52:42 +0000947 if (context_scope == nullptr)
948 context_scope = sc.function;
949 }
950 }
951
952 SymbolFileTypeSP type_sp =
953 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
954
955 auto var_name = pdb_data.getName();
956 auto mangled = GetMangledForPDBData(pdb_data);
957 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
958
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000959 bool is_constant;
960 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
961 GetObjectFile()->GetModule(), pdb_data, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000962
963 var_sp = std::make_shared<Variable>(
964 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
965 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000966 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000967
968 m_variables.insert(std::make_pair(var_uid, var_sp));
969 return var_sp;
970}
971
972size_t
973SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
974 const llvm::pdb::PDBSymbol &pdb_symbol,
975 lldb_private::VariableList *variable_list) {
976 size_t num_added = 0;
977
978 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
979 VariableListSP local_variable_list_sp;
980
981 auto result = m_variables.find(pdb_data->getSymIndexId());
982 if (result != m_variables.end()) {
983 if (variable_list)
984 variable_list->AddVariableIfUnique(result->second);
985 } else {
986 // Prepare right VariableList for this variable.
987 if (auto lexical_parent = pdb_data->getLexicalParent()) {
988 switch (lexical_parent->getSymTag()) {
989 case PDB_SymType::Exe:
990 assert(sc.comp_unit);
991 LLVM_FALLTHROUGH;
992 case PDB_SymType::Compiland: {
993 if (sc.comp_unit) {
994 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
995 if (!local_variable_list_sp) {
996 local_variable_list_sp = std::make_shared<VariableList>();
997 sc.comp_unit->SetVariableList(local_variable_list_sp);
998 }
999 }
1000 } break;
1001 case PDB_SymType::Block:
1002 case PDB_SymType::Function: {
1003 if (sc.function) {
1004 Block *block = sc.function->GetBlock(true).FindBlockByID(
1005 lexical_parent->getSymIndexId());
1006 if (block) {
1007 local_variable_list_sp = block->GetBlockVariableList(false);
1008 if (!local_variable_list_sp) {
1009 local_variable_list_sp = std::make_shared<VariableList>();
1010 block->SetVariableList(local_variable_list_sp);
1011 }
1012 }
1013 }
1014 } break;
1015 default:
1016 break;
1017 }
1018 }
1019
1020 if (local_variable_list_sp) {
1021 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1022 local_variable_list_sp->AddVariableIfUnique(var_sp);
1023 if (variable_list)
1024 variable_list->AddVariableIfUnique(var_sp);
1025 ++num_added;
1026 }
1027 }
1028 }
1029 }
1030
1031 if (auto results = pdb_symbol.findAllChildren()) {
1032 while (auto result = results->getNext())
1033 num_added += ParseVariables(sc, *result, variable_list);
1034 }
1035
1036 return num_added;
1037}
1038
Kate Stoneb9c1b512016-09-06 20:57:50 +00001039uint32_t SymbolFilePDB::FindGlobalVariables(
1040 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +00001041 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001042 uint32_t max_matches, lldb_private::VariableList &variables) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001043 if (!parent_decl_ctx)
1044 parent_decl_ctx = m_tu_decl_ctx_up.get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001045 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1046 return 0;
1047 if (name.IsEmpty())
1048 return 0;
1049
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001050 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001051 if (!results)
1052 return 0;
1053
1054 uint32_t matches = 0;
1055 size_t old_size = variables.GetSize();
1056 while (auto result = results->getNext()) {
1057 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1058 if (max_matches > 0 && matches >= max_matches)
1059 break;
1060
1061 SymbolContext sc;
1062 sc.module_sp = m_obj_file->GetModule();
1063 lldbassert(sc.module_sp.get());
1064
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001065 if (!name.GetStringRef().equals(
1066 PDBASTParser::PDBNameDropScope(pdb_data->getName())))
1067 continue;
1068
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001069 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1070 // FIXME: We are not able to determine the compile unit.
1071 if (sc.comp_unit == nullptr)
1072 continue;
1073
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001074 auto actual_parent_decl_ctx =
1075 GetDeclContextContainingUID(result->getSymIndexId());
1076 if (actual_parent_decl_ctx != *parent_decl_ctx)
1077 continue;
1078
Aaron Smithcab0d232018-05-23 01:52:42 +00001079 ParseVariables(sc, *pdb_data, &variables);
1080 matches = variables.GetSize() - old_size;
1081 }
1082
1083 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001084}
1085
1086uint32_t
1087SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001088 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001090 if (!regex.IsValid())
1091 return 0;
1092 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1093 if (!results)
1094 return 0;
1095
1096 uint32_t matches = 0;
1097 size_t old_size = variables.GetSize();
1098 while (auto pdb_data = results->getNext()) {
1099 if (max_matches > 0 && matches >= max_matches)
1100 break;
1101
1102 auto var_name = pdb_data->getName();
1103 if (var_name.empty())
1104 continue;
1105 if (!regex.Execute(var_name))
1106 continue;
1107 SymbolContext sc;
1108 sc.module_sp = m_obj_file->GetModule();
1109 lldbassert(sc.module_sp.get());
1110
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001111 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001112 // FIXME: We are not able to determine the compile unit.
1113 if (sc.comp_unit == nullptr)
1114 continue;
1115
1116 ParseVariables(sc, *pdb_data, &variables);
1117 matches = variables.GetSize() - old_size;
1118 }
1119
1120 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001121}
1122
Aaron Smithe664b5d2018-03-19 21:14:19 +00001123bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001124 bool include_inlines,
1125 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001126 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001127 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001128 if (!sc.comp_unit)
1129 return false;
1130 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001131 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001132 if (!sc.function)
1133 return false;
1134
1135 sc_list.Append(sc);
1136 return true;
1137}
1138
1139bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1140 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001141 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001142 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1143 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001144 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001145}
1146
1147void SymbolFilePDB::CacheFunctionNames() {
1148 if (!m_func_full_names.IsEmpty())
1149 return;
1150
1151 std::map<uint64_t, uint32_t> addr_ids;
1152
1153 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1154 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001155 if (pdb_func_up->isCompilerGenerated())
1156 continue;
1157
Aaron Smith7ac1c782018-02-09 05:31:28 +00001158 auto name = pdb_func_up->getName();
1159 auto demangled_name = pdb_func_up->getUndecoratedName();
1160 if (name.empty() && demangled_name.empty())
1161 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001162
Aaron Smithf76fe682018-03-07 03:16:50 +00001163 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001164 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1165 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1166
1167 if (auto parent = pdb_func_up->getClassParent()) {
1168
1169 // PDB have symbols for class/struct methods or static methods in Enum
1170 // Class. We won't bother to check if the parent is UDT or Enum here.
1171 m_func_method_names.Append(ConstString(name), uid);
1172
1173 ConstString cstr_name(name);
1174
Adrian Prantl05097242018-04-30 16:49:04 +00001175 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1176 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1177 // not have inforamtion of this, we extract base names and cache them
1178 // by our own effort.
Aaron Smith7ac1c782018-02-09 05:31:28 +00001179 llvm::StringRef basename;
1180 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
1181 if (cpp_method.IsValid()) {
1182 llvm::StringRef context;
1183 basename = cpp_method.GetBasename();
1184 if (basename.empty())
1185 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
1186 context, basename);
1187 }
1188
1189 if (!basename.empty())
1190 m_func_base_names.Append(ConstString(basename), uid);
1191 else {
1192 m_func_base_names.Append(ConstString(name), uid);
1193 }
1194
1195 if (!demangled_name.empty())
1196 m_func_full_names.Append(ConstString(demangled_name), uid);
1197
1198 } else {
1199 // Handle not-method symbols.
1200
1201 // The function name might contain namespace, or its lexical scope. It
1202 // is not safe to get its base name by applying same scheme as we deal
1203 // with the method names.
1204 // FIXME: Remove namespace if function is static in a scope.
1205 m_func_base_names.Append(ConstString(name), uid);
1206
1207 if (name == "main") {
1208 m_func_full_names.Append(ConstString(name), uid);
1209
1210 if (!demangled_name.empty() && name != demangled_name) {
1211 m_func_full_names.Append(ConstString(demangled_name), uid);
1212 m_func_base_names.Append(ConstString(demangled_name), uid);
1213 }
1214 } else if (!demangled_name.empty()) {
1215 m_func_full_names.Append(ConstString(demangled_name), uid);
1216 } else {
1217 m_func_full_names.Append(ConstString(name), uid);
1218 }
1219 }
1220 }
1221 }
1222
1223 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001224 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001225 while (auto pub_sym_up = results_up->getNext()) {
1226 if (!pub_sym_up->isFunction())
1227 continue;
1228 auto name = pub_sym_up->getName();
1229 if (name.empty())
1230 continue;
1231
1232 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001233 auto vm_addr = pub_sym_up->getVirtualAddress();
1234
1235 // PDB public symbol has mangled name for its associated function.
1236 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1237 // Cache mangled name.
1238 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1239 }
1240 }
1241 }
1242 }
1243 // Sort them before value searching is working properly
1244 m_func_full_names.Sort();
1245 m_func_full_names.SizeToFit();
1246 m_func_method_names.Sort();
1247 m_func_method_names.SizeToFit();
1248 m_func_base_names.Sort();
1249 m_func_base_names.SizeToFit();
1250}
1251
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252uint32_t SymbolFilePDB::FindFunctions(
1253 const lldb_private::ConstString &name,
1254 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001255 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001256 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001257 if (!append)
1258 sc_list.Clear();
1259 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1260
1261 if (name_type_mask == eFunctionNameTypeNone)
1262 return 0;
1263 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1264 return 0;
1265 if (name.IsEmpty())
1266 return 0;
1267
1268 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001269 if (name_type_mask & eFunctionNameTypeFull ||
1270 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001271 name_type_mask & eFunctionNameTypeMethod) {
1272 CacheFunctionNames();
1273
1274 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001275 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
1276 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001277 std::vector<uint32_t> ids;
1278 if (Names.GetValues(name, ids)) {
1279 for (auto id : ids) {
1280 if (resolved_ids.find(id) == resolved_ids.end()) {
1281 if (ResolveFunction(id, include_inlines, sc_list))
1282 resolved_ids.insert(id);
1283 }
1284 }
1285 }
1286 };
1287 if (name_type_mask & eFunctionNameTypeFull) {
1288 ResolveFn(m_func_full_names);
1289 }
1290 if (name_type_mask & eFunctionNameTypeBase) {
1291 ResolveFn(m_func_base_names);
1292 }
1293 if (name_type_mask & eFunctionNameTypeMethod) {
1294 ResolveFn(m_func_method_names);
1295 }
1296 }
1297 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298}
1299
1300uint32_t
1301SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1302 bool include_inlines, bool append,
1303 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001304 if (!append)
1305 sc_list.Clear();
1306 if (!regex.IsValid())
1307 return 0;
1308
1309 auto old_size = sc_list.GetSize();
1310 CacheFunctionNames();
1311
1312 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001313 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1314 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001315 std::vector<uint32_t> ids;
1316 if (Names.GetValues(regex, ids)) {
1317 for (auto id : ids) {
1318 if (resolved_ids.find(id) == resolved_ids.end())
1319 if (ResolveFunction(id, include_inlines, sc_list))
1320 resolved_ids.insert(id);
1321 }
1322 }
1323 };
1324 ResolveFn(m_func_full_names);
1325 ResolveFn(m_func_base_names);
1326
1327 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328}
1329
1330void SymbolFilePDB::GetMangledNamesForFunction(
1331 const std::string &scope_qualified_name,
1332 std::vector<lldb_private::ConstString> &mangled_names) {}
1333
1334uint32_t SymbolFilePDB::FindTypes(
1335 const lldb_private::SymbolContext &sc,
1336 const lldb_private::ConstString &name,
1337 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1338 uint32_t max_matches,
1339 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1340 lldb_private::TypeMap &types) {
1341 if (!append)
1342 types.Clear();
1343 if (!name)
1344 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001345 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1346 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001347
1348 searched_symbol_files.clear();
1349 searched_symbol_files.insert(this);
1350
1351 std::string name_str = name.AsCString();
1352
Aaron Smith86e94342017-12-22 05:26:50 +00001353 // There is an assumption 'name' is not a regex
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001354 FindTypesByName(name_str, parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001355
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 return types.GetSize();
1357}
1358
Aaron Smithc8316ed2018-03-22 03:44:51 +00001359void SymbolFilePDB::FindTypesByRegex(
1360 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1361 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001362 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001363 // space as much as possible since this searches EVERYTHING in the PDB,
1364 // manually doing regex comparisons. PDB library isn't optimized for regex
1365 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001366 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001367 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001368 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1369 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 std::unique_ptr<IPDBEnumSymbols> results;
1371
Kate Stoneb9c1b512016-09-06 20:57:50 +00001372 uint32_t matches = 0;
1373
1374 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001375 results = m_global_scope_up->findAllChildren(tag);
1376 if (!results)
1377 continue;
1378
Kate Stoneb9c1b512016-09-06 20:57:50 +00001379 while (auto result = results->getNext()) {
1380 if (max_matches > 0 && matches >= max_matches)
1381 break;
1382
1383 std::string type_name;
1384 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1385 type_name = enum_type->getName();
1386 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001387 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001388 type_name = typedef_type->getName();
1389 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1390 type_name = class_type->getName();
1391 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001392 // We're looking only for types that have names. Skip symbols, as well
1393 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001394 continue;
1395 }
1396
Aaron Smith86e94342017-12-22 05:26:50 +00001397 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001398 continue;
1399
1400 // This should cause the type to get cached and stored in the `m_types`
1401 // lookup.
1402 if (!ResolveTypeUID(result->getSymIndexId()))
1403 continue;
1404
1405 auto iter = m_types.find(result->getSymIndexId());
1406 if (iter == m_types.end())
1407 continue;
1408 types.Insert(iter->second);
1409 ++matches;
1410 }
1411 }
1412}
1413
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001414void SymbolFilePDB::FindTypesByName(
1415 const std::string &name,
1416 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1417 uint32_t max_matches, lldb_private::TypeMap &types) {
1418 if (!parent_decl_ctx)
1419 parent_decl_ctx = m_tu_decl_ctx_up.get();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001421 if (name.empty())
1422 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001423 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001424 if (!results)
1425 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426
1427 uint32_t matches = 0;
1428
1429 while (auto result = results->getNext()) {
1430 if (max_matches > 0 && matches >= max_matches)
1431 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001432
1433 if (PDBASTParser::PDBNameDropScope(result->getRawSymbol().getName()) !=
1434 name)
1435 continue;
1436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 switch (result->getSymTag()) {
1438 case PDB_SymType::Enum:
1439 case PDB_SymType::UDT:
1440 case PDB_SymType::Typedef:
1441 break;
1442 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001443 // We're looking only for types that have names. Skip symbols, as well
1444 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 continue;
1446 }
1447
1448 // This should cause the type to get cached and stored in the `m_types`
1449 // lookup.
1450 if (!ResolveTypeUID(result->getSymIndexId()))
1451 continue;
1452
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001453 auto actual_parent_decl_ctx =
1454 GetDeclContextContainingUID(result->getSymIndexId());
1455 if (actual_parent_decl_ctx != *parent_decl_ctx)
1456 continue;
1457
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 auto iter = m_types.find(result->getSymIndexId());
1459 if (iter == m_types.end())
1460 continue;
1461 types.Insert(iter->second);
1462 ++matches;
1463 }
1464}
1465
1466size_t SymbolFilePDB::FindTypes(
1467 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1468 lldb_private::TypeMap &types) {
1469 return 0;
1470}
1471
Aaron Smithec40f812018-01-23 20:35:19 +00001472lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1473 return m_obj_file->GetModule()->GetTypeList();
1474}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001475
Aaron Smithc8316ed2018-03-22 03:44:51 +00001476void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1477 uint32_t type_mask,
1478 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001479 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001480 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001481 case PDB_SymType::ArrayType:
1482 can_parse = ((type_mask & eTypeClassArray) != 0);
1483 break;
1484 case PDB_SymType::BuiltinType:
1485 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1486 break;
1487 case PDB_SymType::Enum:
1488 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1489 break;
1490 case PDB_SymType::Function:
1491 case PDB_SymType::FunctionSig:
1492 can_parse = ((type_mask & eTypeClassFunction) != 0);
1493 break;
1494 case PDB_SymType::PointerType:
1495 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1496 eTypeClassMemberPointer)) != 0);
1497 break;
1498 case PDB_SymType::Typedef:
1499 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1500 break;
1501 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001502 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001503 assert(udt);
1504 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001505 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1506 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001507 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001508 default:
1509 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001510 }
1511
1512 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001513 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001514 auto result =
1515 std::find(type_collection.begin(), type_collection.end(), type);
1516 if (result == type_collection.end())
1517 type_collection.push_back(type);
1518 }
1519 }
1520
Aaron Smithe664b5d2018-03-19 21:14:19 +00001521 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001522 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001523 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001524}
1525
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001527 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001529 TypeCollection type_collection;
1530 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001531 CompileUnit *cu =
1532 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001533 if (cu) {
1534 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001535 if (!compiland_up)
1536 return 0;
1537 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001538 } else {
1539 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1540 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001541 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001542 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1543 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001544 }
1545 }
1546 }
1547
1548 for (auto type : type_collection) {
1549 type->GetForwardCompilerType();
1550 type_list.Insert(type->shared_from_this());
1551 }
1552 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001553}
1554
1555lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001556SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1557 auto type_system =
1558 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1559 if (type_system)
1560 type_system->SetSymbolFile(this);
1561 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001562}
1563
Kate Stoneb9c1b512016-09-06 20:57:50 +00001564lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1565 const lldb_private::SymbolContext &sc,
1566 const lldb_private::ConstString &name,
1567 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001568 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1569 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1570 if (!clang_type_system)
1571 return CompilerDeclContext();
1572
1573 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1574 if (!pdb)
1575 return CompilerDeclContext();
1576
1577 clang::DeclContext *decl_context = nullptr;
1578 if (parent_decl_ctx)
1579 decl_context = static_cast<clang::DeclContext *>(
1580 parent_decl_ctx->GetOpaqueDeclContext());
1581
1582 auto namespace_decl =
1583 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1584 if (!namespace_decl)
1585 return CompilerDeclContext();
1586
1587 return CompilerDeclContext(type_system,
1588 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001589}
1590
Kate Stoneb9c1b512016-09-06 20:57:50 +00001591lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1592 static ConstString g_name("pdb");
1593 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001594}
1595
Kate Stoneb9c1b512016-09-06 20:57:50 +00001596uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1597
1598IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1599
1600const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1601 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001602}
1603
Aaron Smithc8316ed2018-03-22 03:44:51 +00001604lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1605 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606 auto found_cu = m_comp_units.find(id);
1607 if (found_cu != m_comp_units.end())
1608 return found_cu->second;
1609
Aaron Smith10a02572018-01-13 06:58:18 +00001610 auto compiland_up = GetPDBCompilandByUID(id);
1611 if (!compiland_up)
1612 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001613
1614 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001615 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001616 if (!details)
1617 lang = lldb::eLanguageTypeC_plus_plus;
1618 else
1619 lang = TranslateLanguage(details->getLanguage());
1620
Aaron Smithf76fe682018-03-07 03:16:50 +00001621 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1622 return CompUnitSP();
1623
Aaron Smith487b0c62018-03-20 00:18:22 +00001624 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001625 if (path.empty())
1626 return CompUnitSP();
1627
Kate Stoneb9c1b512016-09-06 20:57:50 +00001628 // Don't support optimized code for now, DebugInfoPDB does not return this
1629 // information.
1630 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001631 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1632 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001633
1634 if (!cu_sp)
1635 return CompUnitSP();
1636
1637 m_comp_units.insert(std::make_pair(id, cu_sp));
1638 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001639 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001640 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001641 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1642 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001643 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001644}
1645
Kate Stoneb9c1b512016-09-06 20:57:50 +00001646bool SymbolFilePDB::ParseCompileUnitLineTable(
1647 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001648 lldbassert(sc.comp_unit);
1649
1650 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1651 if (!compiland_up)
1652 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001653
Kate Stoneb9c1b512016-09-06 20:57:50 +00001654 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001655 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001656 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1657 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001659 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001660 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001661
Aaron Smith10a02572018-01-13 06:58:18 +00001662 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001663 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001664 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1665 if (!files)
1666 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001667
Adrian Prantl05097242018-04-30 16:49:04 +00001668 // For each source and header file, create a LineSequence for contributions
1669 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001670 while (auto file = files->getNext()) {
1671 std::unique_ptr<LineSequence> sequence(
1672 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001673 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1674 if (!lines)
1675 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001676 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001677
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 uint64_t prev_addr;
1679 uint32_t prev_length;
1680 uint32_t prev_line;
1681 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001682
Kate Stoneb9c1b512016-09-06 20:57:50 +00001683 for (int i = 0; i < entry_count; ++i) {
1684 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001685
Kate Stoneb9c1b512016-09-06 20:57:50 +00001686 uint64_t lno = line->getLineNumber();
1687 uint64_t addr = line->getVirtualAddress();
1688 uint32_t length = line->getLength();
1689 uint32_t source_id = line->getSourceFileId();
1690 uint32_t col = line->getColumnNumber();
1691 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001692
Adrian Prantl05097242018-04-30 16:49:04 +00001693 // There was a gap between the current entry and the previous entry if
1694 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001695 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001696
Kate Stoneb9c1b512016-09-06 20:57:50 +00001697 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001698 // of the previous entry's address range if the current entry resulted in
1699 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001700 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1701 line_table->AppendLineEntryToSequence(
1702 sequence.get(), prev_addr + prev_length, prev_line, 0,
1703 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001704
1705 line_table->InsertSequence(sequence.release());
1706 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001708
Kate Stoneb9c1b512016-09-06 20:57:50 +00001709 if (ShouldAddLine(match_line, lno, length)) {
1710 bool is_statement = line->isStatement();
1711 bool is_prologue = false;
1712 bool is_epilogue = false;
1713 auto func =
1714 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1715 if (func) {
1716 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001717 if (prologue)
1718 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001719
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001721 if (epilogue)
1722 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001723 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001724
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1726 source_idx, is_statement, false,
1727 is_prologue, is_epilogue, false);
1728 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001729
Kate Stoneb9c1b512016-09-06 20:57:50 +00001730 prev_addr = addr;
1731 prev_length = length;
1732 prev_line = lno;
1733 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001734 }
1735
Kate Stoneb9c1b512016-09-06 20:57:50 +00001736 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1737 // The end is always a terminal entry, so insert it regardless.
1738 line_table->AppendLineEntryToSequence(
1739 sequence.get(), prev_addr + prev_length, prev_line, 0,
1740 prev_source_idx, false, false, false, false, true);
1741 }
1742
1743 line_table->InsertSequence(sequence.release());
1744 }
1745
Aaron Smith10a02572018-01-13 06:58:18 +00001746 if (line_table->GetSize()) {
1747 sc.comp_unit->SetLineTable(line_table.release());
1748 return true;
1749 }
1750 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001751}
1752
Kate Stoneb9c1b512016-09-06 20:57:50 +00001753void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001754 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001755 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001756 // This is a hack, but we need to convert the source id into an index into
1757 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001758 // basename / full path issues that may or may not even be a problem, so we
1759 // use the globally unique source file identifiers. Ideally we could use the
1760 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001761 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1762 if (!source_files)
1763 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001764
1765 // LLDB uses the DWARF-like file numeration (one based)
1766 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001767
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768 while (auto file = source_files->getNext()) {
1769 uint32_t source_id = file->getUniqueId();
1770 index_map[source_id] = index++;
1771 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001772}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001773
1774lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001775 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001776 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001777 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001778 return nullptr;
1779
Aaron Smith308e39c2018-03-22 19:26:33 +00001780 // If it is a PDB function's vm addr, this is the first sure bet.
1781 if (auto lines =
1782 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1783 if (auto first_line = lines->getNext())
1784 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001785 }
1786
Aaron Smith308e39c2018-03-22 19:26:33 +00001787 // Otherwise we resort to section contributions.
1788 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1789 while (auto section = sec_contribs->getNext()) {
1790 auto va = section->getVirtualAddress();
1791 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1792 return ParseCompileUnitForUID(section->getCompilandId());
1793 }
1794 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001795 return nullptr;
1796}
1797
1798Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001799SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001800 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001801 auto func_name = pdb_func.getName();
1802 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001803 std::string func_decorated_name;
1804
1805 // Seek from public symbols for non-static function's decorated name if any.
1806 // For static functions, they don't have undecorated names and aren't exposed
1807 // in Public Symbols either.
1808 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001809 auto result_up = m_global_scope_up->findChildren(
1810 PDB_SymType::PublicSymbol, func_undecorated_name,
1811 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001812 if (result_up) {
1813 while (auto symbol_up = result_up->getNext()) {
1814 // For a public symbol, it is unique.
1815 lldbassert(result_up->getChildCount() == 1);
1816 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001817 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1818 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001819 if (pdb_public_sym->isFunction()) {
1820 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001821 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001822 }
1823 }
1824 }
1825 }
1826 }
1827 if (!func_decorated_name.empty()) {
1828 mangled.SetMangledName(ConstString(func_decorated_name));
1829
1830 // For MSVC, format of C funciton's decorated name depends on calling
1831 // conventon. Unfortunately none of the format is recognized by current
1832 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001833 // `__purecall` is retrieved as both its decorated and undecorated name
1834 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1835 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1836 // Mangled::GetDemangledName method will fail internally and caches an
1837 // empty string as its undecorated name. So we will face a contradition
1838 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001839 // non-empty undecorated name from PDB
1840 // empty undecorated name from LLDB
1841 if (!func_undecorated_name.empty() &&
1842 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1843 mangled.SetDemangledName(ConstString(func_undecorated_name));
1844
1845 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001846 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1847 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001848 // PDB source unless we also apply same flags in getting undecorated
1849 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1850 if (!func_undecorated_name.empty() &&
1851 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1852 ConstString(func_undecorated_name))
1853 mangled.SetDemangledName(ConstString(func_undecorated_name));
1854 } else if (!func_undecorated_name.empty()) {
1855 mangled.SetDemangledName(ConstString(func_undecorated_name));
1856 } else if (!func_name.empty())
1857 mangled.SetValue(ConstString(func_name), false);
1858
1859 return mangled;
1860}
1861
1862bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1863 const lldb_private::CompilerDeclContext *decl_ctx) {
1864 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1865 return true;
1866
1867 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1868 if (!decl_ctx_type_system)
1869 return false;
1870 TypeSystem *type_system = GetTypeSystemForLanguage(
1871 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1872 if (decl_ctx_type_system == type_system)
1873 return true; // The type systems match, return true
1874
1875 return false;
1876}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001877
1878uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1879 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1880 return lhs < rhs.Offset;
1881 };
1882
1883 // Cache section contributions
1884 if (m_sec_contribs.empty()) {
1885 if (auto SecContribs = m_session_up->getSectionContribs()) {
1886 while (auto SectionContrib = SecContribs->getNext()) {
1887 auto comp_id = SectionContrib->getCompilandId();
1888 if (!comp_id)
1889 continue;
1890
1891 auto sec = SectionContrib->getAddressSection();
1892 auto &sec_cs = m_sec_contribs[sec];
1893
1894 auto offset = SectionContrib->getAddressOffset();
1895 auto it =
1896 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
1897
1898 auto size = SectionContrib->getLength();
1899 sec_cs.insert(it, {offset, size, comp_id});
1900 }
1901 }
1902 }
1903
1904 // Check by line number
1905 if (auto Lines = data.getLineNumbers()) {
1906 if (auto FirstLine = Lines->getNext())
1907 return FirstLine->getCompilandId();
1908 }
1909
1910 // Retrieve section + offset
1911 uint32_t DataSection = data.getAddressSection();
1912 uint32_t DataOffset = data.getAddressOffset();
1913 if (DataSection == 0) {
1914 if (auto RVA = data.getRelativeVirtualAddress())
1915 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
1916 }
1917
1918 if (DataSection) {
1919 // Search by section contributions
1920 auto &sec_cs = m_sec_contribs[DataSection];
1921 auto it =
1922 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
1923 if (it != sec_cs.begin()) {
1924 --it;
1925 if (DataOffset < it->Offset + it->Size)
1926 return it->CompilandId;
1927 }
1928 } else {
1929 // Search in lexical tree
1930 auto LexParentId = data.getLexicalParentId();
1931 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
1932 if (LexParent->getSymTag() == PDB_SymType::Exe)
1933 break;
1934 if (LexParent->getSymTag() == PDB_SymType::Compiland)
1935 return LexParentId;
1936 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
1937 }
1938 }
1939
1940 return 0;
1941}