blob: bdb5436d50d288e109bf2157d3105936719289fc [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
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000012#include "PDBASTParser.h"
13#include "PDBLocationToDWARFExpression.h"
14
Zachary Turner42dff792016-04-15 00:21:26 +000015#include "clang/Lex/Lexer.h"
16
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Core/Module.h"
18#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000019#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000020#include "lldb/Symbol/CompileUnit.h"
21#include "lldb/Symbol/LineTable.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000024#include "lldb/Symbol/SymbolVendor.h"
Aaron Smithec40f812018-01-23 20:35:19 +000025#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000026#include "lldb/Symbol/TypeMap.h"
Aaron Smithcab0d232018-05-23 01:52:42 +000027#include "lldb/Symbol/Variable.h"
Aaron Smith86e94342017-12-22 05:26:50 +000028#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000029
Pavel Labathb8d8c622016-05-09 11:07:43 +000030#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000031#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
33#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000034#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000035#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000036#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000037#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000038#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000039#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000041#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000042#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000046#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000047#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
48#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
49#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
50
Jonas Devlieghere672d2c12018-11-11 23:16:43 +000051#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +000052#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000053#include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h"
Zachary Turner42dff792016-04-15 00:21:26 +000054
55#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000056
Aaron Smith10a02572018-01-13 06:58:18 +000057using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000058using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000059using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061namespace {
62lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
63 switch (lang) {
64 case PDB_Lang::Cpp:
65 return lldb::LanguageType::eLanguageTypeC_plus_plus;
66 case PDB_Lang::C:
67 return lldb::LanguageType::eLanguageTypeC;
68 default:
69 return lldb::LanguageType::eLanguageTypeUnknown;
70 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000071}
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
74 uint32_t addr_length) {
75 return ((requested_line == 0 || actual_line == requested_line) &&
76 addr_length > 0);
77}
Aaron Smithc8316ed2018-03-22 03:44:51 +000078} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000079
Zachary Turner307f5ae2018-10-12 19:47:13 +000080static bool ShouldUseNativeReader() {
81#if !defined(_WIN32)
82 return true;
83#endif
84 llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER");
85 return use_native.equals_lower("on") || use_native.equals_lower("yes") ||
86 use_native.equals_lower("1") || use_native.equals_lower("true");
87}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000090 if (ShouldUseNativeReader()) {
91 npdb::SymbolFileNativePDB::Initialize();
92 } else {
93 PluginManager::RegisterPlugin(GetPluginNameStatic(),
94 GetPluginDescriptionStatic(), CreateInstance,
95 DebuggerInitialize);
96 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000097}
98
Kate Stoneb9c1b512016-09-06 20:57:50 +000099void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000100 if (ShouldUseNativeReader()) {
101 npdb::SymbolFileNativePDB::Terminate();
102 } else {
103 PluginManager::UnregisterPlugin(CreateInstance);
104 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
108
109lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
110 static ConstString g_name("pdb");
111 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114const char *SymbolFilePDB::GetPluginDescriptionStatic() {
115 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000116}
117
118lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
120 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000121}
122
123SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000124 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
125 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000130 uint32_t abilities = 0;
131 if (!m_obj_file)
132 return 0;
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 if (!m_session_up) {
135 // Lazily load and match the PDB file, but only do this once.
136 std::string exePath = m_obj_file->GetFileSpec().GetPath();
137 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
138 m_session_up);
139 if (error) {
140 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000141 auto module_sp = m_obj_file->GetModule();
142 if (!module_sp)
143 return 0;
144 // See if any symbol file is specified through `--symfile` option.
145 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
146 if (!symfile)
147 return 0;
148 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000149 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000150 if (error) {
151 llvm::consumeError(std::move(error));
152 return 0;
153 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000154 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000156 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000157 return 0;
158
159 auto enum_tables_up = m_session_up->getEnumTables();
160 if (!enum_tables_up)
161 return 0;
162 while (auto table_up = enum_tables_up->getNext()) {
163 if (table_up->getItemCount() == 0)
164 continue;
165 auto type = table_up->getTableType();
166 switch (type) {
167 case PDB_TableType::Symbols:
168 // This table represents a store of symbols with types listed in
169 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000170 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
171 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000172 break;
173 case PDB_TableType::LineNumbers:
174 abilities |= LineTables;
175 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000176 default:
177 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000178 }
179 }
180 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000181}
182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183void SymbolFilePDB::InitializeObject() {
184 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000185 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000187 if (!m_global_scope_up)
188 m_global_scope_up = m_session_up->getGlobalScope();
189 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000190
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 TypeSystem *type_system =
192 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
193 ClangASTContext *clang_type_system =
194 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000195 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
197 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000198}
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200uint32_t SymbolFilePDB::GetNumCompileUnits() {
201 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000202 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
203 if (!compilands)
204 return 0;
205
206 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000207 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
208 // found as a child of the global scope (PDB executable). Usually, such
209 // compilands contain `thunk` symbols in which we are not interested for
210 // now. However we still count them in the compiland list. If we perform
211 // any compiland related activity, like finding symbols through
212 // llvm::pdb::IPDBSession methods, such compilands will all be searched
213 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000217 // PDB. Ignore this special compile unit for our purposes, if it is there.
218 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000219 auto last_compiland_up =
220 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
221 lldbassert(last_compiland_up.get());
222 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 if (name == "* Linker *")
224 --m_cached_compile_unit_count;
225 }
226 return m_cached_compile_unit_count;
227}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000228
Aaron Smith10a02572018-01-13 06:58:18 +0000229void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000230 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000231 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
232 if (!results_up)
233 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000234 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000235 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000236 auto compiland_up = results_up->getChildAtIndex(cu_idx);
237 if (!compiland_up)
238 continue;
239 if (compiland_up->getSymIndexId() == uid) {
240 index = cu_idx;
241 return;
242 }
243 }
244 index = UINT32_MAX;
245 return;
246}
247
248std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
249SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
250 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000254 if (index >= GetNumCompileUnits())
255 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256
Aaron Smith10a02572018-01-13 06:58:18 +0000257 // Assuming we always retrieve same compilands listed in same order through
258 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
259 // compile unit makes no sense.
260 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
261 if (!results)
262 return CompUnitSP();
263 auto compiland_up = results->getChildAtIndex(index);
264 if (!compiland_up)
265 return CompUnitSP();
266 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000267}
268
269lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
271 // What fields should I expect to be filled out on the SymbolContext? Is it
272 // safe to assume that `sc.comp_unit` is valid?
273 if (!sc.comp_unit)
274 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000275
Aaron Smith10a02572018-01-13 06:58:18 +0000276 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
277 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000279 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 if (!details)
281 return lldb::eLanguageTypeUnknown;
282 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000283}
284
Aaron Smithc8316ed2018-03-22 03:44:51 +0000285lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
286 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000287 lldbassert(sc.comp_unit && sc.module_sp.get());
288
Aleksandr Urakova5235af2018-12-03 13:31:13 +0000289 if (FunctionSP result =
290 sc.comp_unit->FindFunctionByUID(pdb_func.getSymIndexId()))
291 return result.get();
292
Aaron Smithe664b5d2018-03-19 21:14:19 +0000293 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000294 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000295 return nullptr;
296
Aaron Smithe664b5d2018-03-19 21:14:19 +0000297 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000298 AddressRange func_range =
299 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000300 if (!func_range.GetBaseAddress().IsValid())
301 return nullptr;
302
Aaron Smithc8316ed2018-03-22 03:44:51 +0000303 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000304 if (!func_type)
305 return nullptr;
306
Aaron Smithe664b5d2018-03-19 21:14:19 +0000307 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000308
Aaron Smith7ac1c782018-02-09 05:31:28 +0000309 Mangled mangled = GetMangledForPDBFunc(pdb_func);
310
Aaron Smithc8316ed2018-03-22 03:44:51 +0000311 FunctionSP func_sp =
312 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
313 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000314
315 sc.comp_unit->AddFunction(func_sp);
316 return func_sp.get();
317}
318
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319size_t SymbolFilePDB::ParseCompileUnitFunctions(
320 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000321 lldbassert(sc.comp_unit);
322 size_t func_added = 0;
323 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
324 if (!compiland_up)
325 return 0;
326 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
327 if (!results_up)
328 return 0;
329 while (auto pdb_func_up = results_up->getNext()) {
330 auto func_sp =
331 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
332 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000333 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000334 ++func_added;
335 }
336 }
337 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000338}
339
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340bool SymbolFilePDB::ParseCompileUnitLineTable(
341 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000342 lldbassert(sc.comp_unit);
343 if (sc.comp_unit->GetLineTable())
344 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000345 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000346}
347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348bool SymbolFilePDB::ParseCompileUnitDebugMacros(
349 const lldb_private::SymbolContext &sc) {
350 // PDB doesn't contain information about macros
351 return false;
352}
353
354bool SymbolFilePDB::ParseCompileUnitSupportFiles(
355 const lldb_private::SymbolContext &sc,
356 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000357 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000358
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000360 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
361 // second time seems like a waste. Unfortunately, there's no good way around
362 // this short of a moderate refactor since SymbolVendor depends on being able
363 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000364 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
365 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000366 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000367 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000368 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000369 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370
371 while (auto file = files->getNext()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000372 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000373 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000374 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000375
376 // LLDB uses the DWARF-like file numeration (one based),
377 // the zeroth file is the compile unit itself
378 support_files.Insert(0, *sc.comp_unit);
379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000381}
382
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383bool SymbolFilePDB::ParseImportedModules(
384 const lldb_private::SymbolContext &sc,
385 std::vector<lldb_private::ConstString> &imported_modules) {
386 // PDB does not yet support module debug info
387 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000388}
389
Aaron Smithc8316ed2018-03-22 03:44:51 +0000390static size_t ParseFunctionBlocksForPDBSymbol(
391 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
392 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
393 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000394 assert(pdb_symbol && parent_block);
395
396 size_t num_added = 0;
397 switch (pdb_symbol->getSymTag()) {
398 case PDB_SymType::Block:
399 case PDB_SymType::Function: {
400 Block *block = nullptr;
401 auto &raw_sym = pdb_symbol->getRawSymbol();
402 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
403 if (pdb_func->hasNoInlineAttribute())
404 break;
405 if (is_top_parent)
406 block = parent_block;
407 else
408 break;
409 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
410 auto uid = pdb_symbol->getSymIndexId();
411 if (parent_block->FindBlockByID(uid))
412 break;
413 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
414 break;
415
416 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
417 parent_block->AddChild(block_sp);
418 block = block_sp.get();
419 } else
420 llvm_unreachable("Unexpected PDB symbol!");
421
Aaron Smithc8316ed2018-03-22 03:44:51 +0000422 block->AddRange(Block::Range(
423 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000424 block->FinalizeRanges();
425 ++num_added;
426
427 auto results_up = pdb_symbol->findAllChildren();
428 if (!results_up)
429 break;
430 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000431 num_added += ParseFunctionBlocksForPDBSymbol(
432 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000433 }
434 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000435 default:
436 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000437 }
438 return num_added;
439}
440
Zachary Turner74e08ca2016-03-02 22:05:52 +0000441size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000442SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000443 lldbassert(sc.comp_unit && sc.function);
444 size_t num_added = 0;
445 auto uid = sc.function->GetID();
446 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
447 if (!pdb_func_up)
448 return 0;
449 Block &parent_block = sc.function->GetBlock(false);
450 num_added =
451 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
452 pdb_func_up.get(), &parent_block, true);
453 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000454}
455
Kate Stoneb9c1b512016-09-06 20:57:50 +0000456size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000457 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000458 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000459 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000460
461 size_t num_added = 0;
462 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
463 if (!compiland)
464 return 0;
465
466 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
467 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000468 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
469 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000470 for (auto tag : tags_to_search) {
471 results = raw_sym.findAllChildren(tag);
472 if (!results || results->getChildCount() == 0)
473 continue;
474 while (auto symbol = results->getNext()) {
475 switch (symbol->getSymTag()) {
476 case PDB_SymType::Enum:
477 case PDB_SymType::UDT:
478 case PDB_SymType::Typedef:
479 break;
480 default:
481 continue;
482 }
483
484 // This should cause the type to get cached and stored in the `m_types`
485 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000486 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
487 // Resolve the type completely to avoid a completion
488 // (and so a list change, which causes an iterators invalidation)
489 // during a TypeList dumping
490 type->GetFullCompilerType();
491 ++num_added;
492 }
Aaron Smith66b84072018-03-14 04:05:27 +0000493 }
Aaron Smithec40f812018-01-23 20:35:19 +0000494 }
Aaron Smith66b84072018-03-14 04:05:27 +0000495 };
Aaron Smithec40f812018-01-23 20:35:19 +0000496
Aaron Smith66b84072018-03-14 04:05:27 +0000497 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000498 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
499 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000500 if (!pdb_func)
501 return 0;
502 ParseTypesByTagFn(*pdb_func);
503 } else {
504 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000505
Aaron Smith66b84072018-03-14 04:05:27 +0000506 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000507 // Unfortunately, PDB has no compiland information for each global type. We
508 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000509 static bool parse_all_global_types = false;
510 if (!parse_all_global_types) {
511 ParseTypesByTagFn(*m_global_scope_up);
512 parse_all_global_types = true;
513 }
Aaron Smithec40f812018-01-23 20:35:19 +0000514 }
515 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000516}
517
518size_t
519SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000520 if (!sc.comp_unit)
521 return 0;
522
523 size_t num_added = 0;
524 if (sc.function) {
525 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
526 sc.function->GetID());
527 if (!pdb_func)
528 return 0;
529
530 num_added += ParseVariables(sc, *pdb_func);
531 sc.function->GetBlock(false).SetDidParseVariables(true, true);
532 } else if (sc.comp_unit) {
533 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
534 if (!compiland)
535 return 0;
536
537 if (sc.comp_unit->GetVariableList(false))
538 return 0;
539
540 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
541 if (results && results->getChildCount()) {
542 while (auto result = results->getNext()) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000543 auto cu_id = GetCompilandId(*result);
Aaron Smithcab0d232018-05-23 01:52:42 +0000544 // FIXME: We are not able to determine variable's compile unit.
545 if (cu_id == 0)
546 continue;
547
548 if (cu_id == sc.comp_unit->GetID())
549 num_added += ParseVariables(sc, *result);
550 }
551 }
552
553 // FIXME: A `file static` or `global constant` variable appears both in
554 // compiland's children and global scope's children with unexpectedly
555 // different symbol's Id making it ambiguous.
556
557 // FIXME: 'local constant', for example, const char var[] = "abc", declared
558 // in a function scope, can't be found in PDB.
559
560 // Parse variables in this compiland.
561 num_added += ParseVariables(sc, *compiland);
562 }
563
564 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565}
566
567lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
568 auto find_result = m_types.find(type_uid);
569 if (find_result != m_types.end())
570 return find_result->second.get();
571
572 TypeSystem *type_system =
573 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
574 ClangASTContext *clang_type_system =
575 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
576 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000577 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000578 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 if (!pdb)
580 return nullptr;
581
582 auto pdb_type = m_session_up->getSymbolById(type_uid);
583 if (pdb_type == nullptr)
584 return nullptr;
585
586 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000587 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000588 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000589 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000590 if (type_list)
591 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000592 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000593 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000594}
595
Adrian Prantleca07c52018-11-05 20:49:07 +0000596llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
597 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
598 return llvm::None;
599}
600
Kate Stoneb9c1b512016-09-06 20:57:50 +0000601bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000602 std::lock_guard<std::recursive_mutex> guard(
603 GetObjectFile()->GetModule()->GetMutex());
604
605 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
606 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
607 if (!clang_ast_ctx)
608 return false;
609
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000610 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000611 if (!pdb)
612 return false;
613
614 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000615}
616
617lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000618 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
619 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
620 if (!clang_ast_ctx)
621 return CompilerDecl();
622
623 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
624 if (!pdb)
625 return CompilerDecl();
626
627 auto symbol = m_session_up->getSymbolById(uid);
628 if (!symbol)
629 return CompilerDecl();
630
631 auto decl = pdb->GetDeclForSymbol(*symbol);
632 if (!decl)
633 return CompilerDecl();
634
635 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636}
637
638lldb_private::CompilerDeclContext
639SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000640 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
641 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
642 if (!clang_ast_ctx)
643 return CompilerDeclContext();
644
645 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
646 if (!pdb)
647 return CompilerDeclContext();
648
649 auto symbol = m_session_up->getSymbolById(uid);
650 if (!symbol)
651 return CompilerDeclContext();
652
653 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
654 if (!decl_context)
655 return GetDeclContextContainingUID(uid);
656
657 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658}
659
660lldb_private::CompilerDeclContext
661SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000662 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
663 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
664 if (!clang_ast_ctx)
665 return CompilerDeclContext();
666
667 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
668 if (!pdb)
669 return CompilerDeclContext();
670
671 auto symbol = m_session_up->getSymbolById(uid);
672 if (!symbol)
673 return CompilerDeclContext();
674
675 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
676 assert(decl_context);
677
678 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000679}
680
681void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000682 lldb_private::CompilerDeclContext decl_ctx) {
683 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
684 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
685 if (!clang_ast_ctx)
686 return;
687
688 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
689 if (!pdb)
690 return;
691
692 pdb->ParseDeclsForDeclContext(
693 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
694}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695
696uint32_t
697SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000698 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000700 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000701 if (resolve_scope & eSymbolContextCompUnit ||
702 resolve_scope & eSymbolContextVariable ||
703 resolve_scope & eSymbolContextFunction ||
704 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000705 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000706 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
707 if (!cu_sp) {
708 if (resolved_flags | eSymbolContextVariable) {
709 // TODO: Resolve variables
710 }
711 return 0;
712 }
713 sc.comp_unit = cu_sp.get();
714 resolved_flags |= eSymbolContextCompUnit;
715 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000716 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000717
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000718 if (resolve_scope & eSymbolContextFunction ||
719 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000720 addr_t file_vm_addr = so_addr.GetFileAddress();
721 auto symbol_up =
722 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
723 if (symbol_up) {
724 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
725 assert(pdb_func);
726 auto func_uid = pdb_func->getSymIndexId();
727 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
728 if (sc.function == nullptr)
729 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
730 if (sc.function) {
731 resolved_flags |= eSymbolContextFunction;
732 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000733 auto block_symbol = m_session_up->findSymbolByAddress(
734 file_vm_addr, PDB_SymType::Block);
735 auto block_id = block_symbol ? block_symbol->getSymIndexId()
736 : sc.function->GetID();
737 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000738 if (sc.block)
739 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000740 }
741 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000742 }
743 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000744
745 if (resolve_scope & eSymbolContextLineEntry) {
746 if (auto *line_table = sc.comp_unit->GetLineTable()) {
747 Address addr(so_addr);
748 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
749 resolved_flags |= eSymbolContextLineEntry;
750 }
751 }
752
Aaron Smith7ac1c782018-02-09 05:31:28 +0000753 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754}
755
756uint32_t SymbolFilePDB::ResolveSymbolContext(
757 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000758 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000759 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000760 if (resolve_scope & lldb::eSymbolContextCompUnit) {
761 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000762 // file. For example, if `file_spec` is <vector>, then this should return
763 // all source files and header files that reference <vector>, either
764 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000765 auto compilands = m_session_up->findCompilandsForSourceFile(
766 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
767
Aaron Smith10a02572018-01-13 06:58:18 +0000768 if (!compilands)
769 return 0;
770
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000771 // For each one, either find its previously parsed data or parse it afresh
772 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000774 // If we're not checking inlines, then don't add line information for
775 // this file unless the FileSpec matches. For inline functions, we don't
776 // have to match the FileSpec since they could be defined in headers
777 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000779 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000780 if (source_file.empty())
781 continue;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000782 FileSpec this_spec(source_file, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000783 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
784 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000785 continue;
786 }
787
788 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000789 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000790 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000791 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792 sc.comp_unit = cu.get();
793 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794
795 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000796 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000797 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
798 eSymbolContextLineEntry)) {
799 bool has_line_table = ParseCompileUnitLineTable(sc, line);
800
801 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
802 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000803 // compile unit. This is not normal for `line` = 0. So just assert
804 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000805 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000806
807 // Current compiland does not have the requested line. Search next.
808 continue;
809 }
810
811 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
812 if (!has_line_table)
813 continue;
814
815 auto *line_table = sc.comp_unit->GetLineTable();
816 lldbassert(line_table);
817
818 uint32_t num_line_entries = line_table->GetSize();
819 // Skip the terminal line entry.
820 --num_line_entries;
821
Adrian Prantl05097242018-04-30 16:49:04 +0000822 // If `line `!= 0, see if we can resolve function for each line entry
823 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000824 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
825 ++line_idx) {
826 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
827 continue;
828
829 auto file_vm_addr =
830 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000831 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000832 continue;
833
Aaron Smithc8316ed2018-03-22 03:44:51 +0000834 auto symbol_up = m_session_up->findSymbolByAddress(
835 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000836 if (symbol_up) {
837 auto func_uid = symbol_up->getSymIndexId();
838 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
839 if (sc.function == nullptr) {
840 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
841 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000842 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000843 }
844 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
845 Block &block = sc.function->GetBlock(true);
846 sc.block = block.FindBlockByID(sc.function->GetID());
847 }
848 }
849 sc_list.Append(sc);
850 }
851 } else if (has_line_table) {
852 // We can parse line table for the compile unit. But no query to
853 // resolve function or block. We append `sc` to the list anyway.
854 sc_list.Append(sc);
855 }
856 } else {
857 // No query for line entry, function or block. But we have a valid
858 // compile unit, append `sc` to the list.
859 sc_list.Append(sc);
860 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000861 }
862 }
Aaron Smith10a02572018-01-13 06:58:18 +0000863 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864}
865
Aaron Smithcab0d232018-05-23 01:52:42 +0000866std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000867 // Cache public names at first
868 if (m_public_names.empty())
869 if (auto result_up =
870 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
871 while (auto symbol_up = result_up->getNext())
872 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
873 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000874
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000875 // Look up the name in the cache
876 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000877}
878
879VariableSP SymbolFilePDB::ParseVariableForPDBData(
880 const lldb_private::SymbolContext &sc,
881 const llvm::pdb::PDBSymbolData &pdb_data) {
882 VariableSP var_sp;
883 uint32_t var_uid = pdb_data.getSymIndexId();
884 auto result = m_variables.find(var_uid);
885 if (result != m_variables.end())
886 return result->second;
887
888 ValueType scope = eValueTypeInvalid;
889 bool is_static_member = false;
890 bool is_external = false;
891 bool is_artificial = false;
892
893 switch (pdb_data.getDataKind()) {
894 case PDB_DataKind::Global:
895 scope = eValueTypeVariableGlobal;
896 is_external = true;
897 break;
898 case PDB_DataKind::Local:
899 scope = eValueTypeVariableLocal;
900 break;
901 case PDB_DataKind::FileStatic:
902 scope = eValueTypeVariableStatic;
903 break;
904 case PDB_DataKind::StaticMember:
905 is_static_member = true;
906 scope = eValueTypeVariableStatic;
907 break;
908 case PDB_DataKind::Member:
909 scope = eValueTypeVariableStatic;
910 break;
911 case PDB_DataKind::Param:
912 scope = eValueTypeVariableArgument;
913 break;
914 case PDB_DataKind::Constant:
915 scope = eValueTypeConstResult;
916 break;
917 default:
918 break;
919 }
920
921 switch (pdb_data.getLocationType()) {
922 case PDB_LocType::TLS:
923 scope = eValueTypeVariableThreadLocal;
924 break;
925 case PDB_LocType::RegRel: {
926 // It is a `this` pointer.
927 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
928 scope = eValueTypeVariableArgument;
929 is_artificial = true;
930 }
931 } break;
932 default:
933 break;
934 }
935
936 Declaration decl;
937 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
938 if (auto lines = pdb_data.getLineNumbers()) {
939 if (auto first_line = lines->getNext()) {
940 uint32_t src_file_id = first_line->getSourceFileId();
941 auto src_file = m_session_up->getSourceFileById(src_file_id);
942 if (src_file) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000943 FileSpec spec(src_file->getFileName());
Aaron Smithcab0d232018-05-23 01:52:42 +0000944 decl.SetFile(spec);
945 decl.SetColumn(first_line->getColumnNumber());
946 decl.SetLine(first_line->getLineNumber());
947 }
948 }
949 }
950 }
951
952 Variable::RangeList ranges;
953 SymbolContextScope *context_scope = sc.comp_unit;
954 if (scope == eValueTypeVariableLocal) {
955 if (sc.function) {
956 context_scope = sc.function->GetBlock(true).FindBlockByID(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000957 pdb_data.getLexicalParentId());
Aaron Smithcab0d232018-05-23 01:52:42 +0000958 if (context_scope == nullptr)
959 context_scope = sc.function;
960 }
961 }
962
963 SymbolFileTypeSP type_sp =
964 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
965
966 auto var_name = pdb_data.getName();
967 auto mangled = GetMangledForPDBData(pdb_data);
968 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
969
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000970 bool is_constant;
971 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
972 GetObjectFile()->GetModule(), pdb_data, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000973
974 var_sp = std::make_shared<Variable>(
975 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
976 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000977 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000978
979 m_variables.insert(std::make_pair(var_uid, var_sp));
980 return var_sp;
981}
982
983size_t
984SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
985 const llvm::pdb::PDBSymbol &pdb_symbol,
986 lldb_private::VariableList *variable_list) {
987 size_t num_added = 0;
988
989 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
990 VariableListSP local_variable_list_sp;
991
992 auto result = m_variables.find(pdb_data->getSymIndexId());
993 if (result != m_variables.end()) {
994 if (variable_list)
995 variable_list->AddVariableIfUnique(result->second);
996 } else {
997 // Prepare right VariableList for this variable.
998 if (auto lexical_parent = pdb_data->getLexicalParent()) {
999 switch (lexical_parent->getSymTag()) {
1000 case PDB_SymType::Exe:
1001 assert(sc.comp_unit);
1002 LLVM_FALLTHROUGH;
1003 case PDB_SymType::Compiland: {
1004 if (sc.comp_unit) {
1005 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1006 if (!local_variable_list_sp) {
1007 local_variable_list_sp = std::make_shared<VariableList>();
1008 sc.comp_unit->SetVariableList(local_variable_list_sp);
1009 }
1010 }
1011 } break;
1012 case PDB_SymType::Block:
1013 case PDB_SymType::Function: {
1014 if (sc.function) {
1015 Block *block = sc.function->GetBlock(true).FindBlockByID(
1016 lexical_parent->getSymIndexId());
1017 if (block) {
1018 local_variable_list_sp = block->GetBlockVariableList(false);
1019 if (!local_variable_list_sp) {
1020 local_variable_list_sp = std::make_shared<VariableList>();
1021 block->SetVariableList(local_variable_list_sp);
1022 }
1023 }
1024 }
1025 } break;
1026 default:
1027 break;
1028 }
1029 }
1030
1031 if (local_variable_list_sp) {
1032 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1033 local_variable_list_sp->AddVariableIfUnique(var_sp);
1034 if (variable_list)
1035 variable_list->AddVariableIfUnique(var_sp);
1036 ++num_added;
1037 }
1038 }
1039 }
1040 }
1041
1042 if (auto results = pdb_symbol.findAllChildren()) {
1043 while (auto result = results->getNext())
1044 num_added += ParseVariables(sc, *result, variable_list);
1045 }
1046
1047 return num_added;
1048}
1049
Kate Stoneb9c1b512016-09-06 20:57:50 +00001050uint32_t SymbolFilePDB::FindGlobalVariables(
1051 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +00001052 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001054 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1055 return 0;
1056 if (name.IsEmpty())
1057 return 0;
1058
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001059 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001060 if (!results)
1061 return 0;
1062
1063 uint32_t matches = 0;
1064 size_t old_size = variables.GetSize();
1065 while (auto result = results->getNext()) {
1066 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1067 if (max_matches > 0 && matches >= max_matches)
1068 break;
1069
1070 SymbolContext sc;
1071 sc.module_sp = m_obj_file->GetModule();
1072 lldbassert(sc.module_sp.get());
1073
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001074 if (!name.GetStringRef().equals(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001075 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001076 continue;
1077
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001078 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1079 // FIXME: We are not able to determine the compile unit.
1080 if (sc.comp_unit == nullptr)
1081 continue;
1082
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001083 if (parent_decl_ctx && GetDeclContextContainingUID(
1084 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001085 continue;
1086
Aaron Smithcab0d232018-05-23 01:52:42 +00001087 ParseVariables(sc, *pdb_data, &variables);
1088 matches = variables.GetSize() - old_size;
1089 }
1090
1091 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092}
1093
1094uint32_t
1095SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001096 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001098 if (!regex.IsValid())
1099 return 0;
1100 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1101 if (!results)
1102 return 0;
1103
1104 uint32_t matches = 0;
1105 size_t old_size = variables.GetSize();
1106 while (auto pdb_data = results->getNext()) {
1107 if (max_matches > 0 && matches >= max_matches)
1108 break;
1109
1110 auto var_name = pdb_data->getName();
1111 if (var_name.empty())
1112 continue;
1113 if (!regex.Execute(var_name))
1114 continue;
1115 SymbolContext sc;
1116 sc.module_sp = m_obj_file->GetModule();
1117 lldbassert(sc.module_sp.get());
1118
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001119 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001120 // FIXME: We are not able to determine the compile unit.
1121 if (sc.comp_unit == nullptr)
1122 continue;
1123
1124 ParseVariables(sc, *pdb_data, &variables);
1125 matches = variables.GetSize() - old_size;
1126 }
1127
1128 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001129}
1130
Aaron Smithe664b5d2018-03-19 21:14:19 +00001131bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001132 bool include_inlines,
1133 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001134 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001135 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001136 if (!sc.comp_unit)
1137 return false;
1138 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001139 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001140 if (!sc.function)
1141 return false;
1142
1143 sc_list.Append(sc);
1144 return true;
1145}
1146
1147bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1148 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001149 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001150 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1151 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001152 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001153}
1154
1155void SymbolFilePDB::CacheFunctionNames() {
1156 if (!m_func_full_names.IsEmpty())
1157 return;
1158
1159 std::map<uint64_t, uint32_t> addr_ids;
1160
1161 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1162 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001163 if (pdb_func_up->isCompilerGenerated())
1164 continue;
1165
Aaron Smith7ac1c782018-02-09 05:31:28 +00001166 auto name = pdb_func_up->getName();
1167 auto demangled_name = pdb_func_up->getUndecoratedName();
1168 if (name.empty() && demangled_name.empty())
1169 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001170
Aaron Smithf76fe682018-03-07 03:16:50 +00001171 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001172 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1173 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1174
1175 if (auto parent = pdb_func_up->getClassParent()) {
1176
1177 // PDB have symbols for class/struct methods or static methods in Enum
1178 // Class. We won't bother to check if the parent is UDT or Enum here.
1179 m_func_method_names.Append(ConstString(name), uid);
1180
Adrian Prantl05097242018-04-30 16:49:04 +00001181 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1182 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1183 // not have inforamtion of this, we extract base names and cache them
1184 // by our own effort.
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001185 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001186 if (!basename.empty())
1187 m_func_base_names.Append(ConstString(basename), uid);
1188 else {
1189 m_func_base_names.Append(ConstString(name), uid);
1190 }
1191
1192 if (!demangled_name.empty())
1193 m_func_full_names.Append(ConstString(demangled_name), uid);
1194
1195 } else {
1196 // Handle not-method symbols.
1197
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001198 // The function name might contain namespace, or its lexical scope.
1199 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1200 if (!basename.empty())
1201 m_func_base_names.Append(ConstString(basename), uid);
1202 else
1203 m_func_base_names.Append(ConstString(name), uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001204
1205 if (name == "main") {
1206 m_func_full_names.Append(ConstString(name), uid);
1207
1208 if (!demangled_name.empty() && name != demangled_name) {
1209 m_func_full_names.Append(ConstString(demangled_name), uid);
1210 m_func_base_names.Append(ConstString(demangled_name), uid);
1211 }
1212 } else if (!demangled_name.empty()) {
1213 m_func_full_names.Append(ConstString(demangled_name), uid);
1214 } else {
1215 m_func_full_names.Append(ConstString(name), uid);
1216 }
1217 }
1218 }
1219 }
1220
1221 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001222 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001223 while (auto pub_sym_up = results_up->getNext()) {
1224 if (!pub_sym_up->isFunction())
1225 continue;
1226 auto name = pub_sym_up->getName();
1227 if (name.empty())
1228 continue;
1229
1230 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001231 auto vm_addr = pub_sym_up->getVirtualAddress();
1232
1233 // PDB public symbol has mangled name for its associated function.
1234 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1235 // Cache mangled name.
1236 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1237 }
1238 }
1239 }
1240 }
1241 // Sort them before value searching is working properly
1242 m_func_full_names.Sort();
1243 m_func_full_names.SizeToFit();
1244 m_func_method_names.Sort();
1245 m_func_method_names.SizeToFit();
1246 m_func_base_names.Sort();
1247 m_func_base_names.SizeToFit();
1248}
1249
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250uint32_t SymbolFilePDB::FindFunctions(
1251 const lldb_private::ConstString &name,
1252 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001253 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001255 if (!append)
1256 sc_list.Clear();
1257 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1258
1259 if (name_type_mask == eFunctionNameTypeNone)
1260 return 0;
1261 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1262 return 0;
1263 if (name.IsEmpty())
1264 return 0;
1265
1266 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001267 if (name_type_mask & eFunctionNameTypeFull ||
1268 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001269 name_type_mask & eFunctionNameTypeMethod) {
1270 CacheFunctionNames();
1271
1272 std::set<uint32_t> resolved_ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001273 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1274 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001275 std::vector<uint32_t> ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001276 if (!Names.GetValues(name, ids))
1277 return;
1278
1279 for (uint32_t id : ids) {
1280 if (resolved_ids.find(id) != resolved_ids.end())
1281 continue;
1282
1283 if (parent_decl_ctx &&
1284 GetDeclContextContainingUID(id) != *parent_decl_ctx)
1285 continue;
1286
1287 if (ResolveFunction(id, include_inlines, sc_list))
1288 resolved_ids.insert(id);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001289 }
1290 };
1291 if (name_type_mask & eFunctionNameTypeFull) {
1292 ResolveFn(m_func_full_names);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001293 ResolveFn(m_func_base_names);
1294 ResolveFn(m_func_method_names);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001295 }
1296 if (name_type_mask & eFunctionNameTypeBase) {
1297 ResolveFn(m_func_base_names);
1298 }
1299 if (name_type_mask & eFunctionNameTypeMethod) {
1300 ResolveFn(m_func_method_names);
1301 }
1302 }
1303 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304}
1305
1306uint32_t
1307SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1308 bool include_inlines, bool append,
1309 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001310 if (!append)
1311 sc_list.Clear();
1312 if (!regex.IsValid())
1313 return 0;
1314
1315 auto old_size = sc_list.GetSize();
1316 CacheFunctionNames();
1317
1318 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001319 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1320 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001321 std::vector<uint32_t> ids;
1322 if (Names.GetValues(regex, ids)) {
1323 for (auto id : ids) {
1324 if (resolved_ids.find(id) == resolved_ids.end())
1325 if (ResolveFunction(id, include_inlines, sc_list))
1326 resolved_ids.insert(id);
1327 }
1328 }
1329 };
1330 ResolveFn(m_func_full_names);
1331 ResolveFn(m_func_base_names);
1332
1333 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001334}
1335
1336void SymbolFilePDB::GetMangledNamesForFunction(
1337 const std::string &scope_qualified_name,
1338 std::vector<lldb_private::ConstString> &mangled_names) {}
1339
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001340void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1341 std::set<lldb::addr_t> sym_addresses;
1342 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1343 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
1344
1345 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1346 if (!results)
1347 return;
1348
1349 auto section_list = m_obj_file->GetSectionList();
1350 if (!section_list)
1351 return;
1352
1353 while (auto pub_symbol = results->getNext()) {
1354 auto section_idx = pub_symbol->getAddressSection() - 1;
1355 if (section_idx >= section_list->GetSize())
1356 continue;
1357
1358 auto section = section_list->GetSectionAtIndex(section_idx);
1359 if (!section)
1360 continue;
1361
1362 auto offset = pub_symbol->getAddressOffset();
1363
1364 auto file_addr = section->GetFileAddress() + offset;
1365 if (sym_addresses.find(file_addr) != sym_addresses.end())
1366 continue;
1367 sym_addresses.insert(file_addr);
1368
1369 auto size = pub_symbol->getLength();
1370 symtab.AddSymbol(
1371 Symbol(pub_symbol->getSymIndexId(), // symID
1372 pub_symbol->getName().c_str(), // name
1373 true, // name_is_mangled
1374 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1375 true, // external
1376 false, // is_debug
1377 false, // is_trampoline
1378 false, // is_artificial
1379 section, // section_sp
1380 offset, // value
1381 size, // size
1382 size != 0, // size_is_valid
1383 false, // contains_linker_annotations
1384 0 // flags
1385 ));
1386 }
1387
1388 symtab.CalculateSymbolSizes();
1389 symtab.Finalize();
1390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392uint32_t SymbolFilePDB::FindTypes(
1393 const lldb_private::SymbolContext &sc,
1394 const lldb_private::ConstString &name,
1395 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1396 uint32_t max_matches,
1397 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1398 lldb_private::TypeMap &types) {
1399 if (!append)
1400 types.Clear();
1401 if (!name)
1402 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001403 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1404 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001405
1406 searched_symbol_files.clear();
1407 searched_symbol_files.insert(this);
1408
Aaron Smith86e94342017-12-22 05:26:50 +00001409 // There is an assumption 'name' is not a regex
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001410 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001411
Kate Stoneb9c1b512016-09-06 20:57:50 +00001412 return types.GetSize();
1413}
1414
Zachary Turner49110232018-11-05 17:40:28 +00001415void SymbolFilePDB::DumpClangAST(Stream &s) {
1416 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1417 auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1418 if (!clang)
1419 return;
1420 clang->Dump(s);
1421}
1422
Aaron Smithc8316ed2018-03-22 03:44:51 +00001423void SymbolFilePDB::FindTypesByRegex(
1424 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1425 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001426 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001427 // space as much as possible since this searches EVERYTHING in the PDB,
1428 // manually doing regex comparisons. PDB library isn't optimized for regex
1429 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001430 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001431 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001432 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1433 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 std::unique_ptr<IPDBEnumSymbols> results;
1435
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436 uint32_t matches = 0;
1437
1438 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001439 results = m_global_scope_up->findAllChildren(tag);
1440 if (!results)
1441 continue;
1442
Kate Stoneb9c1b512016-09-06 20:57:50 +00001443 while (auto result = results->getNext()) {
1444 if (max_matches > 0 && matches >= max_matches)
1445 break;
1446
1447 std::string type_name;
1448 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1449 type_name = enum_type->getName();
1450 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001451 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 type_name = typedef_type->getName();
1453 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1454 type_name = class_type->getName();
1455 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001456 // We're looking only for types that have names. Skip symbols, as well
1457 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001458 continue;
1459 }
1460
Aaron Smith86e94342017-12-22 05:26:50 +00001461 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001462 continue;
1463
1464 // This should cause the type to get cached and stored in the `m_types`
1465 // lookup.
1466 if (!ResolveTypeUID(result->getSymIndexId()))
1467 continue;
1468
1469 auto iter = m_types.find(result->getSymIndexId());
1470 if (iter == m_types.end())
1471 continue;
1472 types.Insert(iter->second);
1473 ++matches;
1474 }
1475 }
1476}
1477
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001478void SymbolFilePDB::FindTypesByName(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001479 llvm::StringRef name,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001480 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1481 uint32_t max_matches, lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001483 if (name.empty())
1484 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001485 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001486 if (!results)
1487 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488
1489 uint32_t matches = 0;
1490
1491 while (auto result = results->getNext()) {
1492 if (max_matches > 0 && matches >= max_matches)
1493 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001494
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001495 if (MSVCUndecoratedNameParser::DropScope(
1496 result->getRawSymbol().getName()) != name)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001497 continue;
1498
Kate Stoneb9c1b512016-09-06 20:57:50 +00001499 switch (result->getSymTag()) {
1500 case PDB_SymType::Enum:
1501 case PDB_SymType::UDT:
1502 case PDB_SymType::Typedef:
1503 break;
1504 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001505 // We're looking only for types that have names. Skip symbols, as well
1506 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 continue;
1508 }
1509
1510 // This should cause the type to get cached and stored in the `m_types`
1511 // lookup.
1512 if (!ResolveTypeUID(result->getSymIndexId()))
1513 continue;
1514
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001515 if (parent_decl_ctx && GetDeclContextContainingUID(
1516 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001517 continue;
1518
Kate Stoneb9c1b512016-09-06 20:57:50 +00001519 auto iter = m_types.find(result->getSymIndexId());
1520 if (iter == m_types.end())
1521 continue;
1522 types.Insert(iter->second);
1523 ++matches;
1524 }
1525}
1526
1527size_t SymbolFilePDB::FindTypes(
1528 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1529 lldb_private::TypeMap &types) {
1530 return 0;
1531}
1532
Aaron Smithec40f812018-01-23 20:35:19 +00001533lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1534 return m_obj_file->GetModule()->GetTypeList();
1535}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001536
Aaron Smithc8316ed2018-03-22 03:44:51 +00001537void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1538 uint32_t type_mask,
1539 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001540 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001541 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001542 case PDB_SymType::ArrayType:
1543 can_parse = ((type_mask & eTypeClassArray) != 0);
1544 break;
1545 case PDB_SymType::BuiltinType:
1546 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1547 break;
1548 case PDB_SymType::Enum:
1549 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1550 break;
1551 case PDB_SymType::Function:
1552 case PDB_SymType::FunctionSig:
1553 can_parse = ((type_mask & eTypeClassFunction) != 0);
1554 break;
1555 case PDB_SymType::PointerType:
1556 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1557 eTypeClassMemberPointer)) != 0);
1558 break;
1559 case PDB_SymType::Typedef:
1560 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1561 break;
1562 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001563 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001564 assert(udt);
1565 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001566 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1567 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001568 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001569 default:
1570 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001571 }
1572
1573 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001574 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001575 auto result =
1576 std::find(type_collection.begin(), type_collection.end(), type);
1577 if (result == type_collection.end())
1578 type_collection.push_back(type);
1579 }
1580 }
1581
Aaron Smithe664b5d2018-03-19 21:14:19 +00001582 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001583 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001584 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001585}
1586
Kate Stoneb9c1b512016-09-06 20:57:50 +00001587size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001588 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001589 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001590 TypeCollection type_collection;
1591 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001592 CompileUnit *cu =
1593 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001594 if (cu) {
1595 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001596 if (!compiland_up)
1597 return 0;
1598 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001599 } else {
1600 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1601 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001602 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001603 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1604 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001605 }
1606 }
1607 }
1608
1609 for (auto type : type_collection) {
1610 type->GetForwardCompilerType();
1611 type_list.Insert(type->shared_from_this());
1612 }
1613 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001614}
1615
1616lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001617SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1618 auto type_system =
1619 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1620 if (type_system)
1621 type_system->SetSymbolFile(this);
1622 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001623}
1624
Kate Stoneb9c1b512016-09-06 20:57:50 +00001625lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1626 const lldb_private::SymbolContext &sc,
1627 const lldb_private::ConstString &name,
1628 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001629 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1630 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1631 if (!clang_type_system)
1632 return CompilerDeclContext();
1633
1634 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1635 if (!pdb)
1636 return CompilerDeclContext();
1637
1638 clang::DeclContext *decl_context = nullptr;
1639 if (parent_decl_ctx)
1640 decl_context = static_cast<clang::DeclContext *>(
1641 parent_decl_ctx->GetOpaqueDeclContext());
1642
1643 auto namespace_decl =
1644 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1645 if (!namespace_decl)
1646 return CompilerDeclContext();
1647
1648 return CompilerDeclContext(type_system,
1649 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001650}
1651
Kate Stoneb9c1b512016-09-06 20:57:50 +00001652lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1653 static ConstString g_name("pdb");
1654 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001655}
1656
Kate Stoneb9c1b512016-09-06 20:57:50 +00001657uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1658
1659IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1660
1661const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1662 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001663}
1664
Aaron Smithc8316ed2018-03-22 03:44:51 +00001665lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1666 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667 auto found_cu = m_comp_units.find(id);
1668 if (found_cu != m_comp_units.end())
1669 return found_cu->second;
1670
Aaron Smith10a02572018-01-13 06:58:18 +00001671 auto compiland_up = GetPDBCompilandByUID(id);
1672 if (!compiland_up)
1673 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001674
1675 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001676 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001677 if (!details)
1678 lang = lldb::eLanguageTypeC_plus_plus;
1679 else
1680 lang = TranslateLanguage(details->getLanguage());
1681
Aaron Smithf76fe682018-03-07 03:16:50 +00001682 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1683 return CompUnitSP();
1684
Aaron Smith487b0c62018-03-20 00:18:22 +00001685 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001686 if (path.empty())
1687 return CompUnitSP();
1688
Kate Stoneb9c1b512016-09-06 20:57:50 +00001689 // Don't support optimized code for now, DebugInfoPDB does not return this
1690 // information.
1691 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001692 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1693 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001694
1695 if (!cu_sp)
1696 return CompUnitSP();
1697
1698 m_comp_units.insert(std::make_pair(id, cu_sp));
1699 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001700 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001701 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001702 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1703 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001704 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001705}
1706
Kate Stoneb9c1b512016-09-06 20:57:50 +00001707bool SymbolFilePDB::ParseCompileUnitLineTable(
1708 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001709 lldbassert(sc.comp_unit);
1710
1711 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1712 if (!compiland_up)
1713 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001714
Kate Stoneb9c1b512016-09-06 20:57:50 +00001715 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001716 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001717 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1718 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001719 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001720 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001722
Aaron Smith10a02572018-01-13 06:58:18 +00001723 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001724 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001725 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1726 if (!files)
1727 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001728
Adrian Prantl05097242018-04-30 16:49:04 +00001729 // For each source and header file, create a LineSequence for contributions
1730 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 while (auto file = files->getNext()) {
1732 std::unique_ptr<LineSequence> sequence(
1733 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001734 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1735 if (!lines)
1736 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001737 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001738
Kate Stoneb9c1b512016-09-06 20:57:50 +00001739 uint64_t prev_addr;
1740 uint32_t prev_length;
1741 uint32_t prev_line;
1742 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001743
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744 for (int i = 0; i < entry_count; ++i) {
1745 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001746
Kate Stoneb9c1b512016-09-06 20:57:50 +00001747 uint64_t lno = line->getLineNumber();
1748 uint64_t addr = line->getVirtualAddress();
1749 uint32_t length = line->getLength();
1750 uint32_t source_id = line->getSourceFileId();
1751 uint32_t col = line->getColumnNumber();
1752 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001753
Adrian Prantl05097242018-04-30 16:49:04 +00001754 // There was a gap between the current entry and the previous entry if
1755 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001756 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001757
Kate Stoneb9c1b512016-09-06 20:57:50 +00001758 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001759 // of the previous entry's address range if the current entry resulted in
1760 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001761 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1762 line_table->AppendLineEntryToSequence(
1763 sequence.get(), prev_addr + prev_length, prev_line, 0,
1764 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001765
1766 line_table->InsertSequence(sequence.release());
1767 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001768 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001769
Kate Stoneb9c1b512016-09-06 20:57:50 +00001770 if (ShouldAddLine(match_line, lno, length)) {
1771 bool is_statement = line->isStatement();
1772 bool is_prologue = false;
1773 bool is_epilogue = false;
1774 auto func =
1775 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1776 if (func) {
1777 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001778 if (prologue)
1779 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001780
Kate Stoneb9c1b512016-09-06 20:57:50 +00001781 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001782 if (epilogue)
1783 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001784 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001785
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1787 source_idx, is_statement, false,
1788 is_prologue, is_epilogue, false);
1789 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001790
Kate Stoneb9c1b512016-09-06 20:57:50 +00001791 prev_addr = addr;
1792 prev_length = length;
1793 prev_line = lno;
1794 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001795 }
1796
Kate Stoneb9c1b512016-09-06 20:57:50 +00001797 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1798 // The end is always a terminal entry, so insert it regardless.
1799 line_table->AppendLineEntryToSequence(
1800 sequence.get(), prev_addr + prev_length, prev_line, 0,
1801 prev_source_idx, false, false, false, false, true);
1802 }
1803
1804 line_table->InsertSequence(sequence.release());
1805 }
1806
Aaron Smith10a02572018-01-13 06:58:18 +00001807 if (line_table->GetSize()) {
1808 sc.comp_unit->SetLineTable(line_table.release());
1809 return true;
1810 }
1811 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001812}
1813
Kate Stoneb9c1b512016-09-06 20:57:50 +00001814void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001815 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001816 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001817 // This is a hack, but we need to convert the source id into an index into
1818 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001819 // basename / full path issues that may or may not even be a problem, so we
1820 // use the globally unique source file identifiers. Ideally we could use the
1821 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001822 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1823 if (!source_files)
1824 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001825
1826 // LLDB uses the DWARF-like file numeration (one based)
1827 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001828
Kate Stoneb9c1b512016-09-06 20:57:50 +00001829 while (auto file = source_files->getNext()) {
1830 uint32_t source_id = file->getUniqueId();
1831 index_map[source_id] = index++;
1832 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001833}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001834
1835lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001836 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001837 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001838 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001839 return nullptr;
1840
Aaron Smith308e39c2018-03-22 19:26:33 +00001841 // If it is a PDB function's vm addr, this is the first sure bet.
1842 if (auto lines =
1843 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1844 if (auto first_line = lines->getNext())
1845 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001846 }
1847
Aaron Smith308e39c2018-03-22 19:26:33 +00001848 // Otherwise we resort to section contributions.
1849 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1850 while (auto section = sec_contribs->getNext()) {
1851 auto va = section->getVirtualAddress();
1852 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1853 return ParseCompileUnitForUID(section->getCompilandId());
1854 }
1855 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001856 return nullptr;
1857}
1858
1859Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001860SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001861 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001862 auto func_name = pdb_func.getName();
1863 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001864 std::string func_decorated_name;
1865
1866 // Seek from public symbols for non-static function's decorated name if any.
1867 // For static functions, they don't have undecorated names and aren't exposed
1868 // in Public Symbols either.
1869 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001870 auto result_up = m_global_scope_up->findChildren(
1871 PDB_SymType::PublicSymbol, func_undecorated_name,
1872 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001873 if (result_up) {
1874 while (auto symbol_up = result_up->getNext()) {
1875 // For a public symbol, it is unique.
1876 lldbassert(result_up->getChildCount() == 1);
1877 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001878 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1879 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001880 if (pdb_public_sym->isFunction()) {
1881 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001882 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001883 }
1884 }
1885 }
1886 }
1887 }
1888 if (!func_decorated_name.empty()) {
1889 mangled.SetMangledName(ConstString(func_decorated_name));
1890
1891 // For MSVC, format of C funciton's decorated name depends on calling
1892 // conventon. Unfortunately none of the format is recognized by current
1893 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001894 // `__purecall` is retrieved as both its decorated and undecorated name
1895 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1896 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1897 // Mangled::GetDemangledName method will fail internally and caches an
1898 // empty string as its undecorated name. So we will face a contradition
1899 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001900 // non-empty undecorated name from PDB
1901 // empty undecorated name from LLDB
1902 if (!func_undecorated_name.empty() &&
1903 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1904 mangled.SetDemangledName(ConstString(func_undecorated_name));
1905
1906 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001907 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1908 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001909 // PDB source unless we also apply same flags in getting undecorated
1910 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1911 if (!func_undecorated_name.empty() &&
1912 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1913 ConstString(func_undecorated_name))
1914 mangled.SetDemangledName(ConstString(func_undecorated_name));
1915 } else if (!func_undecorated_name.empty()) {
1916 mangled.SetDemangledName(ConstString(func_undecorated_name));
1917 } else if (!func_name.empty())
1918 mangled.SetValue(ConstString(func_name), false);
1919
1920 return mangled;
1921}
1922
1923bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1924 const lldb_private::CompilerDeclContext *decl_ctx) {
1925 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1926 return true;
1927
1928 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1929 if (!decl_ctx_type_system)
1930 return false;
1931 TypeSystem *type_system = GetTypeSystemForLanguage(
1932 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1933 if (decl_ctx_type_system == type_system)
1934 return true; // The type systems match, return true
1935
1936 return false;
1937}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001938
1939uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1940 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1941 return lhs < rhs.Offset;
1942 };
1943
1944 // Cache section contributions
1945 if (m_sec_contribs.empty()) {
1946 if (auto SecContribs = m_session_up->getSectionContribs()) {
1947 while (auto SectionContrib = SecContribs->getNext()) {
1948 auto comp_id = SectionContrib->getCompilandId();
1949 if (!comp_id)
1950 continue;
1951
1952 auto sec = SectionContrib->getAddressSection();
1953 auto &sec_cs = m_sec_contribs[sec];
1954
1955 auto offset = SectionContrib->getAddressOffset();
1956 auto it =
1957 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
1958
1959 auto size = SectionContrib->getLength();
1960 sec_cs.insert(it, {offset, size, comp_id});
1961 }
1962 }
1963 }
1964
1965 // Check by line number
1966 if (auto Lines = data.getLineNumbers()) {
1967 if (auto FirstLine = Lines->getNext())
1968 return FirstLine->getCompilandId();
1969 }
1970
1971 // Retrieve section + offset
1972 uint32_t DataSection = data.getAddressSection();
1973 uint32_t DataOffset = data.getAddressOffset();
1974 if (DataSection == 0) {
1975 if (auto RVA = data.getRelativeVirtualAddress())
1976 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
1977 }
1978
1979 if (DataSection) {
1980 // Search by section contributions
1981 auto &sec_cs = m_sec_contribs[DataSection];
1982 auto it =
1983 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
1984 if (it != sec_cs.begin()) {
1985 --it;
1986 if (DataOffset < it->Offset + it->Size)
1987 return it->CompilandId;
1988 }
1989 } else {
1990 // Search in lexical tree
1991 auto LexParentId = data.getLexicalParentId();
1992 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
1993 if (LexParent->getSymTag() == PDB_SymType::Exe)
1994 break;
1995 if (LexParent->getSymTag() == PDB_SymType::Compiland)
1996 return LexParentId;
1997 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
1998 }
1999 }
2000
2001 return 0;
2002}