blob: 62bd73fc8821bf80362fc579edfceb23898c2887 [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() {
Greg Clayton71970b72018-12-12 18:14:27 +000081#if defined(_WIN32)
Zachary Turner307f5ae2018-10-12 19:47:13 +000082 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");
Greg Clayton71970b72018-12-12 18:14:27 +000085#else
86 return true;
87#endif
Zachary Turner307f5ae2018-10-12 19:47:13 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void SymbolFilePDB::Initialize() {
Zachary Turner307f5ae2018-10-12 19:47:13 +000091 if (ShouldUseNativeReader()) {
92 npdb::SymbolFileNativePDB::Initialize();
93 } else {
94 PluginManager::RegisterPlugin(GetPluginNameStatic(),
95 GetPluginDescriptionStatic(), CreateInstance,
96 DebuggerInitialize);
97 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000098}
99
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100void SymbolFilePDB::Terminate() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000101 if (ShouldUseNativeReader()) {
102 npdb::SymbolFileNativePDB::Terminate();
103 } else {
104 PluginManager::UnregisterPlugin(CreateInstance);
105 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000106}
107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
109
110lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
111 static ConstString g_name("pdb");
112 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000113}
114
Kate Stoneb9c1b512016-09-06 20:57:50 +0000115const char *SymbolFilePDB::GetPluginDescriptionStatic() {
116 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +0000117}
118
119lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
121 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000122}
123
124SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000125 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
126 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000131 uint32_t abilities = 0;
132 if (!m_obj_file)
133 return 0;
134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 if (!m_session_up) {
136 // Lazily load and match the PDB file, but only do this once.
137 std::string exePath = m_obj_file->GetFileSpec().GetPath();
138 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
139 m_session_up);
140 if (error) {
141 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000142 auto module_sp = m_obj_file->GetModule();
143 if (!module_sp)
144 return 0;
145 // See if any symbol file is specified through `--symfile` option.
146 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
147 if (!symfile)
148 return 0;
149 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000150 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000151 if (error) {
152 llvm::consumeError(std::move(error));
153 return 0;
154 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000157 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000158 return 0;
159
160 auto enum_tables_up = m_session_up->getEnumTables();
161 if (!enum_tables_up)
162 return 0;
163 while (auto table_up = enum_tables_up->getNext()) {
164 if (table_up->getItemCount() == 0)
165 continue;
166 auto type = table_up->getTableType();
167 switch (type) {
168 case PDB_TableType::Symbols:
169 // This table represents a store of symbols with types listed in
170 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000171 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
172 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000173 break;
174 case PDB_TableType::LineNumbers:
175 abilities |= LineTables;
176 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000177 default:
178 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000179 }
180 }
181 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184void SymbolFilePDB::InitializeObject() {
185 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000186 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000188 if (!m_global_scope_up)
189 m_global_scope_up = m_session_up->getGlobalScope();
190 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 TypeSystem *type_system =
193 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
194 ClangASTContext *clang_type_system =
195 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000196 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000197 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
198 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201uint32_t SymbolFilePDB::GetNumCompileUnits() {
202 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000203 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
204 if (!compilands)
205 return 0;
206
207 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000208 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
209 // found as a child of the global scope (PDB executable). Usually, such
210 // compilands contain `thunk` symbols in which we are not interested for
211 // now. However we still count them in the compiland list. If we perform
212 // any compiland related activity, like finding symbols through
213 // llvm::pdb::IPDBSession methods, such compilands will all be searched
214 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000215 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000218 // PDB. Ignore this special compile unit for our purposes, if it is there.
219 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000220 auto last_compiland_up =
221 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
222 lldbassert(last_compiland_up.get());
223 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 if (name == "* Linker *")
225 --m_cached_compile_unit_count;
226 }
227 return m_cached_compile_unit_count;
228}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000229
Aaron Smith10a02572018-01-13 06:58:18 +0000230void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000231 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000232 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
233 if (!results_up)
234 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000235 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000236 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000237 auto compiland_up = results_up->getChildAtIndex(cu_idx);
238 if (!compiland_up)
239 continue;
240 if (compiland_up->getSymIndexId() == uid) {
241 index = cu_idx;
242 return;
243 }
244 }
245 index = UINT32_MAX;
246 return;
247}
248
249std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
250SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
251 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
252}
253
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000255 if (index >= GetNumCompileUnits())
256 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257
Aaron Smith10a02572018-01-13 06:58:18 +0000258 // Assuming we always retrieve same compilands listed in same order through
259 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
260 // compile unit makes no sense.
261 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
262 if (!results)
263 return CompUnitSP();
264 auto compiland_up = results->getChildAtIndex(index);
265 if (!compiland_up)
266 return CompUnitSP();
267 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000268}
269
270lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000271SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
272 // What fields should I expect to be filled out on the SymbolContext? Is it
273 // safe to assume that `sc.comp_unit` is valid?
274 if (!sc.comp_unit)
275 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000276
Aaron Smith10a02572018-01-13 06:58:18 +0000277 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
278 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000280 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 if (!details)
282 return lldb::eLanguageTypeUnknown;
283 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000284}
285
Aaron Smithc8316ed2018-03-22 03:44:51 +0000286lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
287 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000288 lldbassert(sc.comp_unit && sc.module_sp.get());
289
Aleksandr Urakova5235af2018-12-03 13:31:13 +0000290 if (FunctionSP result =
291 sc.comp_unit->FindFunctionByUID(pdb_func.getSymIndexId()))
292 return result.get();
293
Aaron Smithe664b5d2018-03-19 21:14:19 +0000294 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000295 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000296 return nullptr;
297
Aaron Smithe664b5d2018-03-19 21:14:19 +0000298 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000299 AddressRange func_range =
300 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000301 if (!func_range.GetBaseAddress().IsValid())
302 return nullptr;
303
Aaron Smithc8316ed2018-03-22 03:44:51 +0000304 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000305 if (!func_type)
306 return nullptr;
307
Aaron Smithe664b5d2018-03-19 21:14:19 +0000308 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000309
Aaron Smith7ac1c782018-02-09 05:31:28 +0000310 Mangled mangled = GetMangledForPDBFunc(pdb_func);
311
Aaron Smithc8316ed2018-03-22 03:44:51 +0000312 FunctionSP func_sp =
313 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
314 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000315
316 sc.comp_unit->AddFunction(func_sp);
317 return func_sp.get();
318}
319
Kate Stoneb9c1b512016-09-06 20:57:50 +0000320size_t SymbolFilePDB::ParseCompileUnitFunctions(
321 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000322 lldbassert(sc.comp_unit);
323 size_t func_added = 0;
324 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
325 if (!compiland_up)
326 return 0;
327 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
328 if (!results_up)
329 return 0;
330 while (auto pdb_func_up = results_up->getNext()) {
331 auto func_sp =
332 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
333 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000334 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000335 ++func_added;
336 }
337 }
338 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000339}
340
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341bool SymbolFilePDB::ParseCompileUnitLineTable(
342 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000343 lldbassert(sc.comp_unit);
344 if (sc.comp_unit->GetLineTable())
345 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000347}
348
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349bool SymbolFilePDB::ParseCompileUnitDebugMacros(
350 const lldb_private::SymbolContext &sc) {
351 // PDB doesn't contain information about macros
352 return false;
353}
354
355bool SymbolFilePDB::ParseCompileUnitSupportFiles(
356 const lldb_private::SymbolContext &sc,
357 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000358 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000359
Kate Stoneb9c1b512016-09-06 20:57:50 +0000360 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000361 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
362 // second time seems like a waste. Unfortunately, there's no good way around
363 // this short of a moderate refactor since SymbolVendor depends on being able
364 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000365 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
366 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000367 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000368 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000369 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000370 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371
372 while (auto file = files->getNext()) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000373 FileSpec spec(file->getFileName(), FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000374 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000376
377 // LLDB uses the DWARF-like file numeration (one based),
378 // the zeroth file is the compile unit itself
379 support_files.Insert(0, *sc.comp_unit);
380
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000382}
383
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384bool SymbolFilePDB::ParseImportedModules(
385 const lldb_private::SymbolContext &sc,
386 std::vector<lldb_private::ConstString> &imported_modules) {
387 // PDB does not yet support module debug info
388 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000389}
390
Aaron Smithc8316ed2018-03-22 03:44:51 +0000391static size_t ParseFunctionBlocksForPDBSymbol(
392 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
393 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
394 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000395 assert(pdb_symbol && parent_block);
396
397 size_t num_added = 0;
398 switch (pdb_symbol->getSymTag()) {
399 case PDB_SymType::Block:
400 case PDB_SymType::Function: {
401 Block *block = nullptr;
402 auto &raw_sym = pdb_symbol->getRawSymbol();
403 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
404 if (pdb_func->hasNoInlineAttribute())
405 break;
406 if (is_top_parent)
407 block = parent_block;
408 else
409 break;
410 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
411 auto uid = pdb_symbol->getSymIndexId();
412 if (parent_block->FindBlockByID(uid))
413 break;
414 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
415 break;
416
417 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
418 parent_block->AddChild(block_sp);
419 block = block_sp.get();
420 } else
421 llvm_unreachable("Unexpected PDB symbol!");
422
Aaron Smithc8316ed2018-03-22 03:44:51 +0000423 block->AddRange(Block::Range(
424 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000425 block->FinalizeRanges();
426 ++num_added;
427
428 auto results_up = pdb_symbol->findAllChildren();
429 if (!results_up)
430 break;
431 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000432 num_added += ParseFunctionBlocksForPDBSymbol(
433 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000434 }
435 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000436 default:
437 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000438 }
439 return num_added;
440}
441
Zachary Turner74e08ca2016-03-02 22:05:52 +0000442size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000443SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000444 lldbassert(sc.comp_unit && sc.function);
445 size_t num_added = 0;
446 auto uid = sc.function->GetID();
447 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
448 if (!pdb_func_up)
449 return 0;
450 Block &parent_block = sc.function->GetBlock(false);
451 num_added =
452 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
453 pdb_func_up.get(), &parent_block, true);
454 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000455}
456
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000458 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000459 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000460 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000461
462 size_t num_added = 0;
463 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
464 if (!compiland)
465 return 0;
466
467 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
468 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000469 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
470 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000471 for (auto tag : tags_to_search) {
472 results = raw_sym.findAllChildren(tag);
473 if (!results || results->getChildCount() == 0)
474 continue;
475 while (auto symbol = results->getNext()) {
476 switch (symbol->getSymTag()) {
477 case PDB_SymType::Enum:
478 case PDB_SymType::UDT:
479 case PDB_SymType::Typedef:
480 break;
481 default:
482 continue;
483 }
484
485 // This should cause the type to get cached and stored in the `m_types`
486 // lookup.
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000487 if (auto type = ResolveTypeUID(symbol->getSymIndexId())) {
488 // Resolve the type completely to avoid a completion
489 // (and so a list change, which causes an iterators invalidation)
490 // during a TypeList dumping
491 type->GetFullCompilerType();
492 ++num_added;
493 }
Aaron Smith66b84072018-03-14 04:05:27 +0000494 }
Aaron Smithec40f812018-01-23 20:35:19 +0000495 }
Aaron Smith66b84072018-03-14 04:05:27 +0000496 };
Aaron Smithec40f812018-01-23 20:35:19 +0000497
Aaron Smith66b84072018-03-14 04:05:27 +0000498 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000499 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
500 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000501 if (!pdb_func)
502 return 0;
503 ParseTypesByTagFn(*pdb_func);
504 } else {
505 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000506
Aaron Smith66b84072018-03-14 04:05:27 +0000507 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000508 // Unfortunately, PDB has no compiland information for each global type. We
509 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000510 static bool parse_all_global_types = false;
511 if (!parse_all_global_types) {
512 ParseTypesByTagFn(*m_global_scope_up);
513 parse_all_global_types = true;
514 }
Aaron Smithec40f812018-01-23 20:35:19 +0000515 }
516 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517}
518
519size_t
520SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
Aaron Smithcab0d232018-05-23 01:52:42 +0000521 if (!sc.comp_unit)
522 return 0;
523
524 size_t num_added = 0;
525 if (sc.function) {
526 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
527 sc.function->GetID());
528 if (!pdb_func)
529 return 0;
530
531 num_added += ParseVariables(sc, *pdb_func);
532 sc.function->GetBlock(false).SetDidParseVariables(true, true);
533 } else if (sc.comp_unit) {
534 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
535 if (!compiland)
536 return 0;
537
538 if (sc.comp_unit->GetVariableList(false))
539 return 0;
540
541 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
542 if (results && results->getChildCount()) {
543 while (auto result = results->getNext()) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000544 auto cu_id = GetCompilandId(*result);
Aaron Smithcab0d232018-05-23 01:52:42 +0000545 // FIXME: We are not able to determine variable's compile unit.
546 if (cu_id == 0)
547 continue;
548
549 if (cu_id == sc.comp_unit->GetID())
550 num_added += ParseVariables(sc, *result);
551 }
552 }
553
554 // FIXME: A `file static` or `global constant` variable appears both in
555 // compiland's children and global scope's children with unexpectedly
556 // different symbol's Id making it ambiguous.
557
558 // FIXME: 'local constant', for example, const char var[] = "abc", declared
559 // in a function scope, can't be found in PDB.
560
561 // Parse variables in this compiland.
562 num_added += ParseVariables(sc, *compiland);
563 }
564
565 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000566}
567
568lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
569 auto find_result = m_types.find(type_uid);
570 if (find_result != m_types.end())
571 return find_result->second.get();
572
573 TypeSystem *type_system =
574 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
575 ClangASTContext *clang_type_system =
576 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
577 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000578 return nullptr;
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000579 PDBASTParser *pdb = clang_type_system->GetPDBParser();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000580 if (!pdb)
581 return nullptr;
582
583 auto pdb_type = m_session_up->getSymbolById(type_uid);
584 if (pdb_type == nullptr)
585 return nullptr;
586
587 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000588 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000589 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000590 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000591 if (type_list)
592 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000593 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000594 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000595}
596
Adrian Prantleca07c52018-11-05 20:49:07 +0000597llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID(
598 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
599 return llvm::None;
600}
601
Kate Stoneb9c1b512016-09-06 20:57:50 +0000602bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000603 std::lock_guard<std::recursive_mutex> guard(
604 GetObjectFile()->GetModule()->GetMutex());
605
606 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
607 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
608 if (!clang_ast_ctx)
609 return false;
610
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000611 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
Aleksandr Urakov7d2a74f2018-08-14 07:57:44 +0000612 if (!pdb)
613 return false;
614
615 return pdb->CompleteTypeFromPDB(compiler_type);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616}
617
618lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000619 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
620 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
621 if (!clang_ast_ctx)
622 return CompilerDecl();
623
624 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
625 if (!pdb)
626 return CompilerDecl();
627
628 auto symbol = m_session_up->getSymbolById(uid);
629 if (!symbol)
630 return CompilerDecl();
631
632 auto decl = pdb->GetDeclForSymbol(*symbol);
633 if (!decl)
634 return CompilerDecl();
635
636 return CompilerDecl(clang_ast_ctx, decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000637}
638
639lldb_private::CompilerDeclContext
640SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000641 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
642 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
643 if (!clang_ast_ctx)
644 return CompilerDeclContext();
645
646 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
647 if (!pdb)
648 return CompilerDeclContext();
649
650 auto symbol = m_session_up->getSymbolById(uid);
651 if (!symbol)
652 return CompilerDeclContext();
653
654 auto decl_context = pdb->GetDeclContextForSymbol(*symbol);
655 if (!decl_context)
656 return GetDeclContextContainingUID(uid);
657
658 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000659}
660
661lldb_private::CompilerDeclContext
662SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000663 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
664 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
665 if (!clang_ast_ctx)
666 return CompilerDeclContext();
667
668 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
669 if (!pdb)
670 return CompilerDeclContext();
671
672 auto symbol = m_session_up->getSymbolById(uid);
673 if (!symbol)
674 return CompilerDeclContext();
675
676 auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol);
677 assert(decl_context);
678
679 return CompilerDeclContext(clang_ast_ctx, decl_context);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000680}
681
682void SymbolFilePDB::ParseDeclsForContext(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000683 lldb_private::CompilerDeclContext decl_ctx) {
684 ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>(
685 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus));
686 if (!clang_ast_ctx)
687 return;
688
689 PDBASTParser *pdb = clang_ast_ctx->GetPDBParser();
690 if (!pdb)
691 return;
692
693 pdb->ParseDeclsForDeclContext(
694 static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext()));
695}
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696
697uint32_t
698SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
Zachary Turner991e4452018-10-25 20:45:19 +0000699 SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000700 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000701 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000702 if (resolve_scope & eSymbolContextCompUnit ||
703 resolve_scope & eSymbolContextVariable ||
704 resolve_scope & eSymbolContextFunction ||
705 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000706 resolve_scope & eSymbolContextLineEntry) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000707 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
708 if (!cu_sp) {
709 if (resolved_flags | eSymbolContextVariable) {
710 // TODO: Resolve variables
711 }
712 return 0;
713 }
714 sc.comp_unit = cu_sp.get();
715 resolved_flags |= eSymbolContextCompUnit;
716 lldbassert(sc.module_sp == cu_sp->GetModule());
Pavel Labath9ea80d22018-06-28 10:03:42 +0000717 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000718
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000719 if (resolve_scope & eSymbolContextFunction ||
720 resolve_scope & eSymbolContextBlock) {
Pavel Labath9ea80d22018-06-28 10:03:42 +0000721 addr_t file_vm_addr = so_addr.GetFileAddress();
722 auto symbol_up =
723 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
724 if (symbol_up) {
725 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
726 assert(pdb_func);
727 auto func_uid = pdb_func->getSymIndexId();
728 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
729 if (sc.function == nullptr)
730 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
731 if (sc.function) {
732 resolved_flags |= eSymbolContextFunction;
733 if (resolve_scope & eSymbolContextBlock) {
Aleksandr Urakov398f81b2018-08-29 07:26:11 +0000734 auto block_symbol = m_session_up->findSymbolByAddress(
735 file_vm_addr, PDB_SymType::Block);
736 auto block_id = block_symbol ? block_symbol->getSymIndexId()
737 : sc.function->GetID();
738 sc.block = sc.function->GetBlock(true).FindBlockByID(block_id);
Pavel Labath9ea80d22018-06-28 10:03:42 +0000739 if (sc.block)
740 resolved_flags |= eSymbolContextBlock;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000741 }
742 }
Aaron Smith7ac1c782018-02-09 05:31:28 +0000743 }
744 }
Pavel Labath9ea80d22018-06-28 10:03:42 +0000745
746 if (resolve_scope & eSymbolContextLineEntry) {
747 if (auto *line_table = sc.comp_unit->GetLineTable()) {
748 Address addr(so_addr);
749 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
750 resolved_flags |= eSymbolContextLineEntry;
751 }
752 }
753
Aaron Smith7ac1c782018-02-09 05:31:28 +0000754 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000755}
756
757uint32_t SymbolFilePDB::ResolveSymbolContext(
758 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +0000759 SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000760 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 if (resolve_scope & lldb::eSymbolContextCompUnit) {
762 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000763 // file. For example, if `file_spec` is <vector>, then this should return
764 // all source files and header files that reference <vector>, either
765 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 auto compilands = m_session_up->findCompilandsForSourceFile(
767 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
768
Aaron Smith10a02572018-01-13 06:58:18 +0000769 if (!compilands)
770 return 0;
771
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000772 // For each one, either find its previously parsed data or parse it afresh
773 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000775 // If we're not checking inlines, then don't add line information for
776 // this file unless the FileSpec matches. For inline functions, we don't
777 // have to match the FileSpec since they could be defined in headers
778 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000779 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000780 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000781 if (source_file.empty())
782 continue;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000783 FileSpec this_spec(source_file, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000784 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
785 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 continue;
787 }
788
789 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000790 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000791 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000792 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000793 sc.comp_unit = cu.get();
794 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000795
796 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000797 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000798 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
799 eSymbolContextLineEntry)) {
800 bool has_line_table = ParseCompileUnitLineTable(sc, line);
801
802 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
803 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000804 // compile unit. This is not normal for `line` = 0. So just assert
805 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000806 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000807
808 // Current compiland does not have the requested line. Search next.
809 continue;
810 }
811
812 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
813 if (!has_line_table)
814 continue;
815
816 auto *line_table = sc.comp_unit->GetLineTable();
817 lldbassert(line_table);
818
819 uint32_t num_line_entries = line_table->GetSize();
820 // Skip the terminal line entry.
821 --num_line_entries;
822
Adrian Prantl05097242018-04-30 16:49:04 +0000823 // If `line `!= 0, see if we can resolve function for each line entry
824 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000825 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
826 ++line_idx) {
827 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
828 continue;
829
830 auto file_vm_addr =
831 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000832 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000833 continue;
834
Aaron Smithc8316ed2018-03-22 03:44:51 +0000835 auto symbol_up = m_session_up->findSymbolByAddress(
836 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000837 if (symbol_up) {
838 auto func_uid = symbol_up->getSymIndexId();
839 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
840 if (sc.function == nullptr) {
841 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
842 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000843 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000844 }
845 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
846 Block &block = sc.function->GetBlock(true);
847 sc.block = block.FindBlockByID(sc.function->GetID());
848 }
849 }
850 sc_list.Append(sc);
851 }
852 } else if (has_line_table) {
853 // We can parse line table for the compile unit. But no query to
854 // resolve function or block. We append `sc` to the list anyway.
855 sc_list.Append(sc);
856 }
857 } else {
858 // No query for line entry, function or block. But we have a valid
859 // compile unit, append `sc` to the list.
860 sc_list.Append(sc);
861 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862 }
863 }
Aaron Smith10a02572018-01-13 06:58:18 +0000864 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000865}
866
Aaron Smithcab0d232018-05-23 01:52:42 +0000867std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000868 // Cache public names at first
869 if (m_public_names.empty())
870 if (auto result_up =
871 m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol))
872 while (auto symbol_up = result_up->getNext())
873 if (auto addr = symbol_up->getRawSymbol().getVirtualAddress())
874 m_public_names[addr] = symbol_up->getRawSymbol().getName();
Aaron Smithcab0d232018-05-23 01:52:42 +0000875
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +0000876 // Look up the name in the cache
877 return m_public_names.lookup(pdb_data.getVirtualAddress());
Aaron Smithcab0d232018-05-23 01:52:42 +0000878}
879
880VariableSP SymbolFilePDB::ParseVariableForPDBData(
881 const lldb_private::SymbolContext &sc,
882 const llvm::pdb::PDBSymbolData &pdb_data) {
883 VariableSP var_sp;
884 uint32_t var_uid = pdb_data.getSymIndexId();
885 auto result = m_variables.find(var_uid);
886 if (result != m_variables.end())
887 return result->second;
888
889 ValueType scope = eValueTypeInvalid;
890 bool is_static_member = false;
891 bool is_external = false;
892 bool is_artificial = false;
893
894 switch (pdb_data.getDataKind()) {
895 case PDB_DataKind::Global:
896 scope = eValueTypeVariableGlobal;
897 is_external = true;
898 break;
899 case PDB_DataKind::Local:
900 scope = eValueTypeVariableLocal;
901 break;
902 case PDB_DataKind::FileStatic:
903 scope = eValueTypeVariableStatic;
904 break;
905 case PDB_DataKind::StaticMember:
906 is_static_member = true;
907 scope = eValueTypeVariableStatic;
908 break;
909 case PDB_DataKind::Member:
910 scope = eValueTypeVariableStatic;
911 break;
912 case PDB_DataKind::Param:
913 scope = eValueTypeVariableArgument;
914 break;
915 case PDB_DataKind::Constant:
916 scope = eValueTypeConstResult;
917 break;
918 default:
919 break;
920 }
921
922 switch (pdb_data.getLocationType()) {
923 case PDB_LocType::TLS:
924 scope = eValueTypeVariableThreadLocal;
925 break;
926 case PDB_LocType::RegRel: {
927 // It is a `this` pointer.
928 if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
929 scope = eValueTypeVariableArgument;
930 is_artificial = true;
931 }
932 } break;
933 default:
934 break;
935 }
936
937 Declaration decl;
938 if (!is_artificial && !pdb_data.isCompilerGenerated()) {
939 if (auto lines = pdb_data.getLineNumbers()) {
940 if (auto first_line = lines->getNext()) {
941 uint32_t src_file_id = first_line->getSourceFileId();
942 auto src_file = m_session_up->getSourceFileById(src_file_id);
943 if (src_file) {
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000944 FileSpec spec(src_file->getFileName());
Aaron Smithcab0d232018-05-23 01:52:42 +0000945 decl.SetFile(spec);
946 decl.SetColumn(first_line->getColumnNumber());
947 decl.SetLine(first_line->getLineNumber());
948 }
949 }
950 }
951 }
952
953 Variable::RangeList ranges;
954 SymbolContextScope *context_scope = sc.comp_unit;
955 if (scope == eValueTypeVariableLocal) {
956 if (sc.function) {
957 context_scope = sc.function->GetBlock(true).FindBlockByID(
Aleksandr Urakov709426b2018-09-10 08:08:43 +0000958 pdb_data.getLexicalParentId());
Aaron Smithcab0d232018-05-23 01:52:42 +0000959 if (context_scope == nullptr)
960 context_scope = sc.function;
961 }
962 }
963
964 SymbolFileTypeSP type_sp =
965 std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
966
967 auto var_name = pdb_data.getName();
968 auto mangled = GetMangledForPDBData(pdb_data);
969 auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
970
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000971 bool is_constant;
972 DWARFExpression location = ConvertPDBLocationToDWARFExpression(
973 GetObjectFile()->GetModule(), pdb_data, is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000974
975 var_sp = std::make_shared<Variable>(
976 var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
977 ranges, &decl, location, is_external, is_artificial, is_static_member);
Jonas Devlieghere924d5602018-07-13 10:29:27 +0000978 var_sp->SetLocationIsConstantValueData(is_constant);
Aaron Smithcab0d232018-05-23 01:52:42 +0000979
980 m_variables.insert(std::make_pair(var_uid, var_sp));
981 return var_sp;
982}
983
984size_t
985SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
986 const llvm::pdb::PDBSymbol &pdb_symbol,
987 lldb_private::VariableList *variable_list) {
988 size_t num_added = 0;
989
990 if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
991 VariableListSP local_variable_list_sp;
992
993 auto result = m_variables.find(pdb_data->getSymIndexId());
994 if (result != m_variables.end()) {
995 if (variable_list)
996 variable_list->AddVariableIfUnique(result->second);
997 } else {
998 // Prepare right VariableList for this variable.
999 if (auto lexical_parent = pdb_data->getLexicalParent()) {
1000 switch (lexical_parent->getSymTag()) {
1001 case PDB_SymType::Exe:
1002 assert(sc.comp_unit);
1003 LLVM_FALLTHROUGH;
1004 case PDB_SymType::Compiland: {
1005 if (sc.comp_unit) {
1006 local_variable_list_sp = sc.comp_unit->GetVariableList(false);
1007 if (!local_variable_list_sp) {
1008 local_variable_list_sp = std::make_shared<VariableList>();
1009 sc.comp_unit->SetVariableList(local_variable_list_sp);
1010 }
1011 }
1012 } break;
1013 case PDB_SymType::Block:
1014 case PDB_SymType::Function: {
1015 if (sc.function) {
1016 Block *block = sc.function->GetBlock(true).FindBlockByID(
1017 lexical_parent->getSymIndexId());
1018 if (block) {
1019 local_variable_list_sp = block->GetBlockVariableList(false);
1020 if (!local_variable_list_sp) {
1021 local_variable_list_sp = std::make_shared<VariableList>();
1022 block->SetVariableList(local_variable_list_sp);
1023 }
1024 }
1025 }
1026 } break;
1027 default:
1028 break;
1029 }
1030 }
1031
1032 if (local_variable_list_sp) {
1033 if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
1034 local_variable_list_sp->AddVariableIfUnique(var_sp);
1035 if (variable_list)
1036 variable_list->AddVariableIfUnique(var_sp);
1037 ++num_added;
1038 }
1039 }
1040 }
1041 }
1042
1043 if (auto results = pdb_symbol.findAllChildren()) {
1044 while (auto result = results->getNext())
1045 num_added += ParseVariables(sc, *result, variable_list);
1046 }
1047
1048 return num_added;
1049}
1050
Kate Stoneb9c1b512016-09-06 20:57:50 +00001051uint32_t SymbolFilePDB::FindGlobalVariables(
1052 const lldb_private::ConstString &name,
Pavel Labath34cda142018-05-31 09:46:26 +00001053 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001054 uint32_t max_matches, lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001055 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1056 return 0;
1057 if (name.IsEmpty())
1058 return 0;
1059
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001060 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
Aaron Smithcab0d232018-05-23 01:52:42 +00001061 if (!results)
1062 return 0;
1063
1064 uint32_t matches = 0;
1065 size_t old_size = variables.GetSize();
1066 while (auto result = results->getNext()) {
1067 auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
1068 if (max_matches > 0 && matches >= max_matches)
1069 break;
1070
1071 SymbolContext sc;
1072 sc.module_sp = m_obj_file->GetModule();
1073 lldbassert(sc.module_sp.get());
1074
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001075 if (!name.GetStringRef().equals(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001076 MSVCUndecoratedNameParser::DropScope(pdb_data->getName())))
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001077 continue;
1078
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001079 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
1080 // FIXME: We are not able to determine the compile unit.
1081 if (sc.comp_unit == nullptr)
1082 continue;
1083
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001084 if (parent_decl_ctx && GetDeclContextContainingUID(
1085 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001086 continue;
1087
Aaron Smithcab0d232018-05-23 01:52:42 +00001088 ParseVariables(sc, *pdb_data, &variables);
1089 matches = variables.GetSize() - old_size;
1090 }
1091
1092 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001093}
1094
1095uint32_t
1096SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +00001097 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001098 lldb_private::VariableList &variables) {
Aaron Smithcab0d232018-05-23 01:52:42 +00001099 if (!regex.IsValid())
1100 return 0;
1101 auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
1102 if (!results)
1103 return 0;
1104
1105 uint32_t matches = 0;
1106 size_t old_size = variables.GetSize();
1107 while (auto pdb_data = results->getNext()) {
1108 if (max_matches > 0 && matches >= max_matches)
1109 break;
1110
1111 auto var_name = pdb_data->getName();
1112 if (var_name.empty())
1113 continue;
1114 if (!regex.Execute(var_name))
1115 continue;
1116 SymbolContext sc;
1117 sc.module_sp = m_obj_file->GetModule();
1118 lldbassert(sc.module_sp.get());
1119
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001120 sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get();
Aaron Smithcab0d232018-05-23 01:52:42 +00001121 // FIXME: We are not able to determine the compile unit.
1122 if (sc.comp_unit == nullptr)
1123 continue;
1124
1125 ParseVariables(sc, *pdb_data, &variables);
1126 matches = variables.GetSize() - old_size;
1127 }
1128
1129 return matches;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001130}
1131
Aaron Smithe664b5d2018-03-19 21:14:19 +00001132bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +00001133 bool include_inlines,
1134 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001135 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001136 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001137 if (!sc.comp_unit)
1138 return false;
1139 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +00001140 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001141 if (!sc.function)
1142 return false;
1143
1144 sc_list.Append(sc);
1145 return true;
1146}
1147
1148bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
1149 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001150 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001151 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
1152 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001153 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001154}
1155
1156void SymbolFilePDB::CacheFunctionNames() {
1157 if (!m_func_full_names.IsEmpty())
1158 return;
1159
1160 std::map<uint64_t, uint32_t> addr_ids;
1161
1162 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
1163 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +00001164 if (pdb_func_up->isCompilerGenerated())
1165 continue;
1166
Aaron Smith7ac1c782018-02-09 05:31:28 +00001167 auto name = pdb_func_up->getName();
1168 auto demangled_name = pdb_func_up->getUndecoratedName();
1169 if (name.empty() && demangled_name.empty())
1170 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001171
Aaron Smithf76fe682018-03-07 03:16:50 +00001172 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001173 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
1174 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
1175
1176 if (auto parent = pdb_func_up->getClassParent()) {
1177
1178 // PDB have symbols for class/struct methods or static methods in Enum
1179 // Class. We won't bother to check if the parent is UDT or Enum here.
1180 m_func_method_names.Append(ConstString(name), uid);
1181
Adrian Prantl05097242018-04-30 16:49:04 +00001182 // To search a method name, like NS::Class:MemberFunc, LLDB searches
1183 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
1184 // not have inforamtion of this, we extract base names and cache them
1185 // by our own effort.
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001186 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001187 if (!basename.empty())
1188 m_func_base_names.Append(ConstString(basename), uid);
1189 else {
1190 m_func_base_names.Append(ConstString(name), uid);
1191 }
1192
1193 if (!demangled_name.empty())
1194 m_func_full_names.Append(ConstString(demangled_name), uid);
1195
1196 } else {
1197 // Handle not-method symbols.
1198
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001199 // The function name might contain namespace, or its lexical scope.
1200 llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name);
1201 if (!basename.empty())
1202 m_func_base_names.Append(ConstString(basename), uid);
1203 else
1204 m_func_base_names.Append(ConstString(name), uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001205
1206 if (name == "main") {
1207 m_func_full_names.Append(ConstString(name), uid);
1208
1209 if (!demangled_name.empty() && name != demangled_name) {
1210 m_func_full_names.Append(ConstString(demangled_name), uid);
1211 m_func_base_names.Append(ConstString(demangled_name), uid);
1212 }
1213 } else if (!demangled_name.empty()) {
1214 m_func_full_names.Append(ConstString(demangled_name), uid);
1215 } else {
1216 m_func_full_names.Append(ConstString(name), uid);
1217 }
1218 }
1219 }
1220 }
1221
1222 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001223 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001224 while (auto pub_sym_up = results_up->getNext()) {
1225 if (!pub_sym_up->isFunction())
1226 continue;
1227 auto name = pub_sym_up->getName();
1228 if (name.empty())
1229 continue;
1230
1231 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001232 auto vm_addr = pub_sym_up->getVirtualAddress();
1233
1234 // PDB public symbol has mangled name for its associated function.
1235 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
1236 // Cache mangled name.
1237 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
1238 }
1239 }
1240 }
1241 }
1242 // Sort them before value searching is working properly
1243 m_func_full_names.Sort();
1244 m_func_full_names.SizeToFit();
1245 m_func_method_names.Sort();
1246 m_func_method_names.SizeToFit();
1247 m_func_base_names.Sort();
1248 m_func_base_names.SizeToFit();
1249}
1250
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251uint32_t SymbolFilePDB::FindFunctions(
1252 const lldb_private::ConstString &name,
1253 const lldb_private::CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001254 FunctionNameType name_type_mask, bool include_inlines, bool append,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001255 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001256 if (!append)
1257 sc_list.Clear();
1258 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
1259
1260 if (name_type_mask == eFunctionNameTypeNone)
1261 return 0;
1262 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1263 return 0;
1264 if (name.IsEmpty())
1265 return 0;
1266
1267 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +00001268 if (name_type_mask & eFunctionNameTypeFull ||
1269 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +00001270 name_type_mask & eFunctionNameTypeMethod) {
1271 CacheFunctionNames();
1272
1273 std::set<uint32_t> resolved_ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001274 auto ResolveFn = [this, &name, parent_decl_ctx, include_inlines, &sc_list,
1275 &resolved_ids](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001276 std::vector<uint32_t> ids;
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001277 if (!Names.GetValues(name, ids))
1278 return;
1279
1280 for (uint32_t id : ids) {
1281 if (resolved_ids.find(id) != resolved_ids.end())
1282 continue;
1283
1284 if (parent_decl_ctx &&
1285 GetDeclContextContainingUID(id) != *parent_decl_ctx)
1286 continue;
1287
1288 if (ResolveFunction(id, include_inlines, sc_list))
1289 resolved_ids.insert(id);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001290 }
1291 };
1292 if (name_type_mask & eFunctionNameTypeFull) {
1293 ResolveFn(m_func_full_names);
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001294 ResolveFn(m_func_base_names);
1295 ResolveFn(m_func_method_names);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001296 }
1297 if (name_type_mask & eFunctionNameTypeBase) {
1298 ResolveFn(m_func_base_names);
1299 }
1300 if (name_type_mask & eFunctionNameTypeMethod) {
1301 ResolveFn(m_func_method_names);
1302 }
1303 }
1304 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001305}
1306
1307uint32_t
1308SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1309 bool include_inlines, bool append,
1310 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001311 if (!append)
1312 sc_list.Clear();
1313 if (!regex.IsValid())
1314 return 0;
1315
1316 auto old_size = sc_list.GetSize();
1317 CacheFunctionNames();
1318
1319 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001320 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
1321 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001322 std::vector<uint32_t> ids;
1323 if (Names.GetValues(regex, ids)) {
1324 for (auto id : ids) {
1325 if (resolved_ids.find(id) == resolved_ids.end())
1326 if (ResolveFunction(id, include_inlines, sc_list))
1327 resolved_ids.insert(id);
1328 }
1329 }
1330 };
1331 ResolveFn(m_func_full_names);
1332 ResolveFn(m_func_base_names);
1333
1334 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335}
1336
1337void SymbolFilePDB::GetMangledNamesForFunction(
1338 const std::string &scope_qualified_name,
1339 std::vector<lldb_private::ConstString> &mangled_names) {}
1340
Aleksandr Urakov8cfb12b2018-11-30 06:56:37 +00001341void SymbolFilePDB::AddSymbols(lldb_private::Symtab &symtab) {
1342 std::set<lldb::addr_t> sym_addresses;
1343 for (size_t i = 0; i < symtab.GetNumSymbols(); i++)
1344 sym_addresses.insert(symtab.SymbolAtIndex(i)->GetFileAddress());
1345
1346 auto results = m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>();
1347 if (!results)
1348 return;
1349
1350 auto section_list = m_obj_file->GetSectionList();
1351 if (!section_list)
1352 return;
1353
1354 while (auto pub_symbol = results->getNext()) {
1355 auto section_idx = pub_symbol->getAddressSection() - 1;
1356 if (section_idx >= section_list->GetSize())
1357 continue;
1358
1359 auto section = section_list->GetSectionAtIndex(section_idx);
1360 if (!section)
1361 continue;
1362
1363 auto offset = pub_symbol->getAddressOffset();
1364
1365 auto file_addr = section->GetFileAddress() + offset;
1366 if (sym_addresses.find(file_addr) != sym_addresses.end())
1367 continue;
1368 sym_addresses.insert(file_addr);
1369
1370 auto size = pub_symbol->getLength();
1371 symtab.AddSymbol(
1372 Symbol(pub_symbol->getSymIndexId(), // symID
1373 pub_symbol->getName().c_str(), // name
1374 true, // name_is_mangled
1375 pub_symbol->isCode() ? eSymbolTypeCode : eSymbolTypeData, // type
1376 true, // external
1377 false, // is_debug
1378 false, // is_trampoline
1379 false, // is_artificial
1380 section, // section_sp
1381 offset, // value
1382 size, // size
1383 size != 0, // size_is_valid
1384 false, // contains_linker_annotations
1385 0 // flags
1386 ));
1387 }
1388
1389 symtab.CalculateSymbolSizes();
1390 symtab.Finalize();
1391}
1392
Kate Stoneb9c1b512016-09-06 20:57:50 +00001393uint32_t SymbolFilePDB::FindTypes(
1394 const lldb_private::SymbolContext &sc,
1395 const lldb_private::ConstString &name,
1396 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1397 uint32_t max_matches,
1398 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1399 lldb_private::TypeMap &types) {
1400 if (!append)
1401 types.Clear();
1402 if (!name)
1403 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001404 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1405 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001406
1407 searched_symbol_files.clear();
1408 searched_symbol_files.insert(this);
1409
Aaron Smith86e94342017-12-22 05:26:50 +00001410 // There is an assumption 'name' is not a regex
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001411 FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001412
Kate Stoneb9c1b512016-09-06 20:57:50 +00001413 return types.GetSize();
1414}
1415
Zachary Turner49110232018-11-05 17:40:28 +00001416void SymbolFilePDB::DumpClangAST(Stream &s) {
1417 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1418 auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1419 if (!clang)
1420 return;
1421 clang->Dump(s);
1422}
1423
Aaron Smithc8316ed2018-03-22 03:44:51 +00001424void SymbolFilePDB::FindTypesByRegex(
1425 const lldb_private::RegularExpression &regex, uint32_t max_matches,
1426 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001427 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001428 // space as much as possible since this searches EVERYTHING in the PDB,
1429 // manually doing regex comparisons. PDB library isn't optimized for regex
1430 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001432 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1434 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001435 std::unique_ptr<IPDBEnumSymbols> results;
1436
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 uint32_t matches = 0;
1438
1439 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001440 results = m_global_scope_up->findAllChildren(tag);
1441 if (!results)
1442 continue;
1443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 while (auto result = results->getNext()) {
1445 if (max_matches > 0 && matches >= max_matches)
1446 break;
1447
1448 std::string type_name;
1449 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1450 type_name = enum_type->getName();
1451 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001452 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001453 type_name = typedef_type->getName();
1454 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1455 type_name = class_type->getName();
1456 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001457 // We're looking only for types that have names. Skip symbols, as well
1458 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459 continue;
1460 }
1461
Aaron Smith86e94342017-12-22 05:26:50 +00001462 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001463 continue;
1464
1465 // This should cause the type to get cached and stored in the `m_types`
1466 // lookup.
1467 if (!ResolveTypeUID(result->getSymIndexId()))
1468 continue;
1469
1470 auto iter = m_types.find(result->getSymIndexId());
1471 if (iter == m_types.end())
1472 continue;
1473 types.Insert(iter->second);
1474 ++matches;
1475 }
1476 }
1477}
1478
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001479void SymbolFilePDB::FindTypesByName(
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001480 llvm::StringRef name,
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001481 const lldb_private::CompilerDeclContext *parent_decl_ctx,
1482 uint32_t max_matches, lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001483 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001484 if (name.empty())
1485 return;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001486 results = m_global_scope_up->findAllChildren(PDB_SymType::None);
Aaron Smith10a02572018-01-13 06:58:18 +00001487 if (!results)
1488 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001489
1490 uint32_t matches = 0;
1491
1492 while (auto result = results->getNext()) {
1493 if (max_matches > 0 && matches >= max_matches)
1494 break;
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001495
Aleksandr Urakovc1e530e2018-11-06 08:02:55 +00001496 if (MSVCUndecoratedNameParser::DropScope(
1497 result->getRawSymbol().getName()) != name)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001498 continue;
1499
Kate Stoneb9c1b512016-09-06 20:57:50 +00001500 switch (result->getSymTag()) {
1501 case PDB_SymType::Enum:
1502 case PDB_SymType::UDT:
1503 case PDB_SymType::Typedef:
1504 break;
1505 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001506 // We're looking only for types that have names. Skip symbols, as well
1507 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001508 continue;
1509 }
1510
1511 // This should cause the type to get cached and stored in the `m_types`
1512 // lookup.
1513 if (!ResolveTypeUID(result->getSymIndexId()))
1514 continue;
1515
Aleksandr Urakova5235af2018-12-03 13:31:13 +00001516 if (parent_decl_ctx && GetDeclContextContainingUID(
1517 result->getSymIndexId()) != *parent_decl_ctx)
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001518 continue;
1519
Kate Stoneb9c1b512016-09-06 20:57:50 +00001520 auto iter = m_types.find(result->getSymIndexId());
1521 if (iter == m_types.end())
1522 continue;
1523 types.Insert(iter->second);
1524 ++matches;
1525 }
1526}
1527
1528size_t SymbolFilePDB::FindTypes(
1529 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1530 lldb_private::TypeMap &types) {
1531 return 0;
1532}
1533
Aaron Smithec40f812018-01-23 20:35:19 +00001534lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1535 return m_obj_file->GetModule()->GetTypeList();
1536}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001537
Aaron Smithc8316ed2018-03-22 03:44:51 +00001538void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1539 uint32_t type_mask,
1540 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001541 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001542 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001543 case PDB_SymType::ArrayType:
1544 can_parse = ((type_mask & eTypeClassArray) != 0);
1545 break;
1546 case PDB_SymType::BuiltinType:
1547 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1548 break;
1549 case PDB_SymType::Enum:
1550 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1551 break;
1552 case PDB_SymType::Function:
1553 case PDB_SymType::FunctionSig:
1554 can_parse = ((type_mask & eTypeClassFunction) != 0);
1555 break;
1556 case PDB_SymType::PointerType:
1557 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1558 eTypeClassMemberPointer)) != 0);
1559 break;
1560 case PDB_SymType::Typedef:
1561 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1562 break;
1563 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001564 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001565 assert(udt);
1566 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001567 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1568 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001569 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001570 default:
1571 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001572 }
1573
1574 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001575 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001576 auto result =
1577 std::find(type_collection.begin(), type_collection.end(), type);
1578 if (result == type_collection.end())
1579 type_collection.push_back(type);
1580 }
1581 }
1582
Aaron Smithe664b5d2018-03-19 21:14:19 +00001583 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001584 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001585 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001586}
1587
Kate Stoneb9c1b512016-09-06 20:57:50 +00001588size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001589 TypeClass type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001590 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001591 TypeCollection type_collection;
1592 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001593 CompileUnit *cu =
1594 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001595 if (cu) {
1596 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001597 if (!compiland_up)
1598 return 0;
1599 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001600 } else {
1601 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1602 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001603 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001604 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1605 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001606 }
1607 }
1608 }
1609
1610 for (auto type : type_collection) {
1611 type->GetForwardCompilerType();
1612 type_list.Insert(type->shared_from_this());
1613 }
1614 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001615}
1616
1617lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001618SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1619 auto type_system =
1620 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1621 if (type_system)
1622 type_system->SetSymbolFile(this);
1623 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001624}
1625
Kate Stoneb9c1b512016-09-06 20:57:50 +00001626lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1627 const lldb_private::SymbolContext &sc,
1628 const lldb_private::ConstString &name,
1629 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
Aleksandr Urakov709426b2018-09-10 08:08:43 +00001630 auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
1631 auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system);
1632 if (!clang_type_system)
1633 return CompilerDeclContext();
1634
1635 PDBASTParser *pdb = clang_type_system->GetPDBParser();
1636 if (!pdb)
1637 return CompilerDeclContext();
1638
1639 clang::DeclContext *decl_context = nullptr;
1640 if (parent_decl_ctx)
1641 decl_context = static_cast<clang::DeclContext *>(
1642 parent_decl_ctx->GetOpaqueDeclContext());
1643
1644 auto namespace_decl =
1645 pdb->FindNamespaceDecl(decl_context, name.GetStringRef());
1646 if (!namespace_decl)
1647 return CompilerDeclContext();
1648
1649 return CompilerDeclContext(type_system,
1650 static_cast<clang::DeclContext *>(namespace_decl));
Zachary Turner74e08ca2016-03-02 22:05:52 +00001651}
1652
Kate Stoneb9c1b512016-09-06 20:57:50 +00001653lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1654 static ConstString g_name("pdb");
1655 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001656}
1657
Kate Stoneb9c1b512016-09-06 20:57:50 +00001658uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1659
1660IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1661
1662const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1663 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001664}
1665
Aaron Smithc8316ed2018-03-22 03:44:51 +00001666lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1667 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001668 auto found_cu = m_comp_units.find(id);
1669 if (found_cu != m_comp_units.end())
1670 return found_cu->second;
1671
Aaron Smith10a02572018-01-13 06:58:18 +00001672 auto compiland_up = GetPDBCompilandByUID(id);
1673 if (!compiland_up)
1674 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001675
1676 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001677 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 if (!details)
1679 lang = lldb::eLanguageTypeC_plus_plus;
1680 else
1681 lang = TranslateLanguage(details->getLanguage());
1682
Aaron Smithf76fe682018-03-07 03:16:50 +00001683 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1684 return CompUnitSP();
1685
Aaron Smith487b0c62018-03-20 00:18:22 +00001686 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001687 if (path.empty())
1688 return CompUnitSP();
1689
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690 // Don't support optimized code for now, DebugInfoPDB does not return this
1691 // information.
1692 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001693 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1694 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001695
1696 if (!cu_sp)
1697 return CompUnitSP();
1698
1699 m_comp_units.insert(std::make_pair(id, cu_sp));
1700 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001701 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001702 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001703 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1704 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001705 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001706}
1707
Kate Stoneb9c1b512016-09-06 20:57:50 +00001708bool SymbolFilePDB::ParseCompileUnitLineTable(
1709 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001710 lldbassert(sc.comp_unit);
1711
1712 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1713 if (!compiland_up)
1714 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001715
Kate Stoneb9c1b512016-09-06 20:57:50 +00001716 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001717 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001718 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1719 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001720 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001721 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001722 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001723
Aaron Smith10a02572018-01-13 06:58:18 +00001724 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001725 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001726 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1727 if (!files)
1728 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001729
Adrian Prantl05097242018-04-30 16:49:04 +00001730 // For each source and header file, create a LineSequence for contributions
1731 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001732 while (auto file = files->getNext()) {
1733 std::unique_ptr<LineSequence> sequence(
1734 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001735 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1736 if (!lines)
1737 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001739
Kate Stoneb9c1b512016-09-06 20:57:50 +00001740 uint64_t prev_addr;
1741 uint32_t prev_length;
1742 uint32_t prev_line;
1743 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001744
Kate Stoneb9c1b512016-09-06 20:57:50 +00001745 for (int i = 0; i < entry_count; ++i) {
1746 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001747
Kate Stoneb9c1b512016-09-06 20:57:50 +00001748 uint64_t lno = line->getLineNumber();
1749 uint64_t addr = line->getVirtualAddress();
1750 uint32_t length = line->getLength();
1751 uint32_t source_id = line->getSourceFileId();
1752 uint32_t col = line->getColumnNumber();
1753 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001754
Adrian Prantl05097242018-04-30 16:49:04 +00001755 // There was a gap between the current entry and the previous entry if
1756 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001757 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001758
Kate Stoneb9c1b512016-09-06 20:57:50 +00001759 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001760 // of the previous entry's address range if the current entry resulted in
1761 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001762 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1763 line_table->AppendLineEntryToSequence(
1764 sequence.get(), prev_addr + prev_length, prev_line, 0,
1765 prev_source_idx, false, false, false, false, true);
Aaron Smith010edd32018-06-08 02:45:25 +00001766
1767 line_table->InsertSequence(sequence.release());
1768 sequence.reset(line_table->CreateLineSequenceContainer());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001769 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001770
Kate Stoneb9c1b512016-09-06 20:57:50 +00001771 if (ShouldAddLine(match_line, lno, length)) {
1772 bool is_statement = line->isStatement();
1773 bool is_prologue = false;
1774 bool is_epilogue = false;
1775 auto func =
1776 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1777 if (func) {
1778 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001779 if (prologue)
1780 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001781
Kate Stoneb9c1b512016-09-06 20:57:50 +00001782 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001783 if (epilogue)
1784 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001785 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001786
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1788 source_idx, is_statement, false,
1789 is_prologue, is_epilogue, false);
1790 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001791
Kate Stoneb9c1b512016-09-06 20:57:50 +00001792 prev_addr = addr;
1793 prev_length = length;
1794 prev_line = lno;
1795 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001796 }
1797
Kate Stoneb9c1b512016-09-06 20:57:50 +00001798 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1799 // The end is always a terminal entry, so insert it regardless.
1800 line_table->AppendLineEntryToSequence(
1801 sequence.get(), prev_addr + prev_length, prev_line, 0,
1802 prev_source_idx, false, false, false, false, true);
1803 }
1804
1805 line_table->InsertSequence(sequence.release());
1806 }
1807
Aaron Smith10a02572018-01-13 06:58:18 +00001808 if (line_table->GetSize()) {
1809 sc.comp_unit->SetLineTable(line_table.release());
1810 return true;
1811 }
1812 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001813}
1814
Kate Stoneb9c1b512016-09-06 20:57:50 +00001815void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001816 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001818 // This is a hack, but we need to convert the source id into an index into
1819 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001820 // basename / full path issues that may or may not even be a problem, so we
1821 // use the globally unique source file identifiers. Ideally we could use the
1822 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001823 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1824 if (!source_files)
1825 return;
Pavel Labath9ea80d22018-06-28 10:03:42 +00001826
1827 // LLDB uses the DWARF-like file numeration (one based)
1828 int index = 1;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001829
Kate Stoneb9c1b512016-09-06 20:57:50 +00001830 while (auto file = source_files->getNext()) {
1831 uint32_t source_id = file->getUniqueId();
1832 index_map[source_id] = index++;
1833 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001834}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001835
1836lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001837 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001838 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001839 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001840 return nullptr;
1841
Aaron Smith308e39c2018-03-22 19:26:33 +00001842 // If it is a PDB function's vm addr, this is the first sure bet.
1843 if (auto lines =
1844 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1845 if (auto first_line = lines->getNext())
1846 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001847 }
1848
Aaron Smith308e39c2018-03-22 19:26:33 +00001849 // Otherwise we resort to section contributions.
1850 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1851 while (auto section = sec_contribs->getNext()) {
1852 auto va = section->getVirtualAddress();
1853 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1854 return ParseCompileUnitForUID(section->getCompilandId());
1855 }
1856 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001857 return nullptr;
1858}
1859
1860Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001861SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001862 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001863 auto func_name = pdb_func.getName();
1864 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001865 std::string func_decorated_name;
1866
1867 // Seek from public symbols for non-static function's decorated name if any.
1868 // For static functions, they don't have undecorated names and aren't exposed
1869 // in Public Symbols either.
1870 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001871 auto result_up = m_global_scope_up->findChildren(
1872 PDB_SymType::PublicSymbol, func_undecorated_name,
1873 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001874 if (result_up) {
1875 while (auto symbol_up = result_up->getNext()) {
1876 // For a public symbol, it is unique.
1877 lldbassert(result_up->getChildCount() == 1);
1878 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001879 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1880 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001881 if (pdb_public_sym->isFunction()) {
1882 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001883 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001884 }
1885 }
1886 }
1887 }
1888 }
1889 if (!func_decorated_name.empty()) {
1890 mangled.SetMangledName(ConstString(func_decorated_name));
1891
1892 // For MSVC, format of C funciton's decorated name depends on calling
1893 // conventon. Unfortunately none of the format is recognized by current
1894 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001895 // `__purecall` is retrieved as both its decorated and undecorated name
1896 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1897 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1898 // Mangled::GetDemangledName method will fail internally and caches an
1899 // empty string as its undecorated name. So we will face a contradition
1900 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001901 // non-empty undecorated name from PDB
1902 // empty undecorated name from LLDB
1903 if (!func_undecorated_name.empty() &&
1904 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1905 mangled.SetDemangledName(ConstString(func_undecorated_name));
1906
1907 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001908 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1909 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001910 // PDB source unless we also apply same flags in getting undecorated
1911 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1912 if (!func_undecorated_name.empty() &&
1913 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1914 ConstString(func_undecorated_name))
1915 mangled.SetDemangledName(ConstString(func_undecorated_name));
1916 } else if (!func_undecorated_name.empty()) {
1917 mangled.SetDemangledName(ConstString(func_undecorated_name));
1918 } else if (!func_name.empty())
1919 mangled.SetValue(ConstString(func_name), false);
1920
1921 return mangled;
1922}
1923
1924bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1925 const lldb_private::CompilerDeclContext *decl_ctx) {
1926 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1927 return true;
1928
1929 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1930 if (!decl_ctx_type_system)
1931 return false;
1932 TypeSystem *type_system = GetTypeSystemForLanguage(
1933 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1934 if (decl_ctx_type_system == type_system)
1935 return true; // The type systems match, return true
1936
1937 return false;
1938}
Aleksandr Urakov356aa4a2018-10-23 08:29:17 +00001939
1940uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) {
1941 static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) {
1942 return lhs < rhs.Offset;
1943 };
1944
1945 // Cache section contributions
1946 if (m_sec_contribs.empty()) {
1947 if (auto SecContribs = m_session_up->getSectionContribs()) {
1948 while (auto SectionContrib = SecContribs->getNext()) {
1949 auto comp_id = SectionContrib->getCompilandId();
1950 if (!comp_id)
1951 continue;
1952
1953 auto sec = SectionContrib->getAddressSection();
1954 auto &sec_cs = m_sec_contribs[sec];
1955
1956 auto offset = SectionContrib->getAddressOffset();
1957 auto it =
1958 std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper);
1959
1960 auto size = SectionContrib->getLength();
1961 sec_cs.insert(it, {offset, size, comp_id});
1962 }
1963 }
1964 }
1965
1966 // Check by line number
1967 if (auto Lines = data.getLineNumbers()) {
1968 if (auto FirstLine = Lines->getNext())
1969 return FirstLine->getCompilandId();
1970 }
1971
1972 // Retrieve section + offset
1973 uint32_t DataSection = data.getAddressSection();
1974 uint32_t DataOffset = data.getAddressOffset();
1975 if (DataSection == 0) {
1976 if (auto RVA = data.getRelativeVirtualAddress())
1977 m_session_up->addressForRVA(RVA, DataSection, DataOffset);
1978 }
1979
1980 if (DataSection) {
1981 // Search by section contributions
1982 auto &sec_cs = m_sec_contribs[DataSection];
1983 auto it =
1984 std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper);
1985 if (it != sec_cs.begin()) {
1986 --it;
1987 if (DataOffset < it->Offset + it->Size)
1988 return it->CompilandId;
1989 }
1990 } else {
1991 // Search in lexical tree
1992 auto LexParentId = data.getLexicalParentId();
1993 while (auto LexParent = m_session_up->getSymbolById(LexParentId)) {
1994 if (LexParent->getSymTag() == PDB_SymType::Exe)
1995 break;
1996 if (LexParent->getSymTag() == PDB_SymType::Compiland)
1997 return LexParentId;
1998 LexParentId = LexParent->getRawSymbol().getLexicalParentId();
1999 }
2000 }
2001
2002 return 0;
2003}