blob: 06d6087f31c2b68ca8d51c6d5414550e3d577cbe [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFilePDB.h"
11
Zachary Turner42dff792016-04-15 00:21:26 +000012#include "clang/Lex/Lexer.h"
13
Zachary Turner74e08ca2016-03-02 22:05:52 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/LineTable.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000021#include "lldb/Symbol/SymbolVendor.h"
Aaron Smithec40f812018-01-23 20:35:19 +000022#include "lldb/Symbol/TypeList.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000023#include "lldb/Symbol/TypeMap.h"
Aaron Smith86e94342017-12-22 05:26:50 +000024#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000025
Pavel Labathb8d8c622016-05-09 11:07:43 +000026#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000027#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000028#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
29#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Aaron Smith308e39c2018-03-22 19:26:33 +000030#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000031#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000032#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000033#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000034#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000035#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
36#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000037#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000038#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
41#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000042#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000043#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
45#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
46
Aaron Smith7ac1c782018-02-09 05:31:28 +000047#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner42dff792016-04-15 00:21:26 +000048#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
49
50#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000051
Aaron Smith10a02572018-01-13 06:58:18 +000052using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000053using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000054using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056namespace {
57lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
58 switch (lang) {
59 case PDB_Lang::Cpp:
60 return lldb::LanguageType::eLanguageTypeC_plus_plus;
61 case PDB_Lang::C:
62 return lldb::LanguageType::eLanguageTypeC;
63 default:
64 return lldb::LanguageType::eLanguageTypeUnknown;
65 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000066}
67
Kate Stoneb9c1b512016-09-06 20:57:50 +000068bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
69 uint32_t addr_length) {
70 return ((requested_line == 0 || actual_line == requested_line) &&
71 addr_length > 0);
72}
Aaron Smithc8316ed2018-03-22 03:44:51 +000073} // namespace
Zachary Turner74e08ca2016-03-02 22:05:52 +000074
Kate Stoneb9c1b512016-09-06 20:57:50 +000075void SymbolFilePDB::Initialize() {
76 PluginManager::RegisterPlugin(GetPluginNameStatic(),
77 GetPluginDescriptionStatic(), CreateInstance,
78 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081void SymbolFilePDB::Terminate() {
82 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000083}
84
Kate Stoneb9c1b512016-09-06 20:57:50 +000085void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
86
87lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
88 static ConstString g_name("pdb");
89 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000090}
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092const char *SymbolFilePDB::GetPluginDescriptionStatic() {
93 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000094}
95
96lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000097SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
98 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +000099}
100
101SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000102 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
103 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000108 uint32_t abilities = 0;
109 if (!m_obj_file)
110 return 0;
111
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 if (!m_session_up) {
113 // Lazily load and match the PDB file, but only do this once.
114 std::string exePath = m_obj_file->GetFileSpec().GetPath();
115 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
116 m_session_up);
117 if (error) {
118 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000119 auto module_sp = m_obj_file->GetModule();
120 if (!module_sp)
121 return 0;
122 // See if any symbol file is specified through `--symfile` option.
123 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
124 if (!symfile)
125 return 0;
126 error = loadDataForPDB(PDB_ReaderType::DIA,
Aaron Smithc8316ed2018-03-22 03:44:51 +0000127 llvm::StringRef(symfile.GetPath()), m_session_up);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000128 if (error) {
129 llvm::consumeError(std::move(error));
130 return 0;
131 }
Zachary Turner74e08ca2016-03-02 22:05:52 +0000132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 }
Aaron Smithd5a925f2018-03-22 19:21:34 +0000134 if (!m_session_up)
Aaron Smith1f8552a2017-12-22 00:04:36 +0000135 return 0;
136
137 auto enum_tables_up = m_session_up->getEnumTables();
138 if (!enum_tables_up)
139 return 0;
140 while (auto table_up = enum_tables_up->getNext()) {
141 if (table_up->getItemCount() == 0)
142 continue;
143 auto type = table_up->getTableType();
144 switch (type) {
145 case PDB_TableType::Symbols:
146 // This table represents a store of symbols with types listed in
147 // PDBSym_Type
Aaron Smithc8316ed2018-03-22 03:44:51 +0000148 abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
149 LocalVariables | VariableTypes);
Aaron Smith1f8552a2017-12-22 00:04:36 +0000150 break;
151 case PDB_TableType::LineNumbers:
152 abilities |= LineTables;
153 break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000154 default:
155 break;
Aaron Smith1f8552a2017-12-22 00:04:36 +0000156 }
157 }
158 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161void SymbolFilePDB::InitializeObject() {
162 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000163 lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164 m_session_up->setLoadAddress(obj_load_address);
Aaron Smith10a02572018-01-13 06:58:18 +0000165 if (!m_global_scope_up)
166 m_global_scope_up = m_session_up->getGlobalScope();
167 lldbassert(m_global_scope_up.get());
Zachary Turner42dff792016-04-15 00:21:26 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 TypeSystem *type_system =
170 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
171 ClangASTContext *clang_type_system =
172 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
Aaron Smith10a02572018-01-13 06:58:18 +0000173 lldbassert(clang_type_system);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
175 type_system, clang_type_system->GetTranslationUnitDecl());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000176}
177
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178uint32_t SymbolFilePDB::GetNumCompileUnits() {
179 if (m_cached_compile_unit_count == 0) {
Aaron Smith10a02572018-01-13 06:58:18 +0000180 auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
181 if (!compilands)
182 return 0;
183
184 // The linker could link *.dll (compiland language = LINK), or import
Adrian Prantl05097242018-04-30 16:49:04 +0000185 // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
186 // found as a child of the global scope (PDB executable). Usually, such
187 // compilands contain `thunk` symbols in which we are not interested for
188 // now. However we still count them in the compiland list. If we perform
189 // any compiland related activity, like finding symbols through
190 // llvm::pdb::IPDBSession methods, such compilands will all be searched
191 // automatically no matter whether we include them or not.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192 m_cached_compile_unit_count = compilands->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 // The linker can inject an additional "dummy" compilation unit into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000195 // PDB. Ignore this special compile unit for our purposes, if it is there.
196 // It is always the last one.
Aaron Smith10a02572018-01-13 06:58:18 +0000197 auto last_compiland_up =
198 compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
199 lldbassert(last_compiland_up.get());
200 std::string name = last_compiland_up->getName();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 if (name == "* Linker *")
202 --m_cached_compile_unit_count;
203 }
204 return m_cached_compile_unit_count;
205}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000206
Aaron Smith10a02572018-01-13 06:58:18 +0000207void SymbolFilePDB::GetCompileUnitIndex(
Aaron Smithc8316ed2018-03-22 03:44:51 +0000208 const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000209 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
210 if (!results_up)
211 return;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000212 auto uid = pdb_compiland.getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000213 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000214 auto compiland_up = results_up->getChildAtIndex(cu_idx);
215 if (!compiland_up)
216 continue;
217 if (compiland_up->getSymIndexId() == uid) {
218 index = cu_idx;
219 return;
220 }
221 }
222 index = UINT32_MAX;
223 return;
224}
225
226std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
227SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
228 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
229}
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000232 if (index >= GetNumCompileUnits())
233 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234
Aaron Smith10a02572018-01-13 06:58:18 +0000235 // Assuming we always retrieve same compilands listed in same order through
236 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
237 // compile unit makes no sense.
238 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
239 if (!results)
240 return CompUnitSP();
241 auto compiland_up = results->getChildAtIndex(index);
242 if (!compiland_up)
243 return CompUnitSP();
244 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000245}
246
247lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
249 // What fields should I expect to be filled out on the SymbolContext? Is it
250 // safe to assume that `sc.comp_unit` is valid?
251 if (!sc.comp_unit)
252 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000253
Aaron Smith10a02572018-01-13 06:58:18 +0000254 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
255 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000257 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 if (!details)
259 return lldb::eLanguageTypeUnknown;
260 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000261}
262
Aaron Smithc8316ed2018-03-22 03:44:51 +0000263lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
264 const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000265 lldbassert(sc.comp_unit && sc.module_sp.get());
266
Aaron Smithe664b5d2018-03-19 21:14:19 +0000267 auto file_vm_addr = pdb_func.getVirtualAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000268 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000269 return nullptr;
270
Aaron Smithe664b5d2018-03-19 21:14:19 +0000271 auto func_length = pdb_func.getLength();
Aaron Smithc8316ed2018-03-22 03:44:51 +0000272 AddressRange func_range =
273 AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000274 if (!func_range.GetBaseAddress().IsValid())
275 return nullptr;
276
Aaron Smithc8316ed2018-03-22 03:44:51 +0000277 lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
Aaron Smith7ac1c782018-02-09 05:31:28 +0000278 if (!func_type)
279 return nullptr;
280
Aaron Smithe664b5d2018-03-19 21:14:19 +0000281 user_id_t func_type_uid = pdb_func.getSignatureId();
Aaron Smithf76fe682018-03-07 03:16:50 +0000282
Aaron Smith7ac1c782018-02-09 05:31:28 +0000283 Mangled mangled = GetMangledForPDBFunc(pdb_func);
284
Aaron Smithc8316ed2018-03-22 03:44:51 +0000285 FunctionSP func_sp =
286 std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
287 func_type_uid, mangled, func_type, func_range);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000288
289 sc.comp_unit->AddFunction(func_sp);
290 return func_sp.get();
291}
292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293size_t SymbolFilePDB::ParseCompileUnitFunctions(
294 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000295 lldbassert(sc.comp_unit);
296 size_t func_added = 0;
297 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
298 if (!compiland_up)
299 return 0;
300 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
301 if (!results_up)
302 return 0;
303 while (auto pdb_func_up = results_up->getNext()) {
304 auto func_sp =
305 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
306 if (!func_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +0000307 if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
Aaron Smith7ac1c782018-02-09 05:31:28 +0000308 ++func_added;
309 }
310 }
311 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000312}
313
Kate Stoneb9c1b512016-09-06 20:57:50 +0000314bool SymbolFilePDB::ParseCompileUnitLineTable(
315 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000316 lldbassert(sc.comp_unit);
317 if (sc.comp_unit->GetLineTable())
318 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000319 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000320}
321
Kate Stoneb9c1b512016-09-06 20:57:50 +0000322bool SymbolFilePDB::ParseCompileUnitDebugMacros(
323 const lldb_private::SymbolContext &sc) {
324 // PDB doesn't contain information about macros
325 return false;
326}
327
328bool SymbolFilePDB::ParseCompileUnitSupportFiles(
329 const lldb_private::SymbolContext &sc,
330 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000331 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000334 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
335 // second time seems like a waste. Unfortunately, there's no good way around
336 // this short of a moderate refactor since SymbolVendor depends on being able
337 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000338 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
339 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000340 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000341 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000342 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000343 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344
345 while (auto file = files->getNext()) {
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000346 FileSpec spec(file->getFileName(), false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000347 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 }
349 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000350}
351
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352bool SymbolFilePDB::ParseImportedModules(
353 const lldb_private::SymbolContext &sc,
354 std::vector<lldb_private::ConstString> &imported_modules) {
355 // PDB does not yet support module debug info
356 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000357}
358
Aaron Smithc8316ed2018-03-22 03:44:51 +0000359static size_t ParseFunctionBlocksForPDBSymbol(
360 const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
361 const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
362 bool is_top_parent) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000363 assert(pdb_symbol && parent_block);
364
365 size_t num_added = 0;
366 switch (pdb_symbol->getSymTag()) {
367 case PDB_SymType::Block:
368 case PDB_SymType::Function: {
369 Block *block = nullptr;
370 auto &raw_sym = pdb_symbol->getRawSymbol();
371 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
372 if (pdb_func->hasNoInlineAttribute())
373 break;
374 if (is_top_parent)
375 block = parent_block;
376 else
377 break;
378 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
379 auto uid = pdb_symbol->getSymIndexId();
380 if (parent_block->FindBlockByID(uid))
381 break;
382 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
383 break;
384
385 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
386 parent_block->AddChild(block_sp);
387 block = block_sp.get();
388 } else
389 llvm_unreachable("Unexpected PDB symbol!");
390
Aaron Smithc8316ed2018-03-22 03:44:51 +0000391 block->AddRange(Block::Range(
392 raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
Aaron Smith7ac1c782018-02-09 05:31:28 +0000393 block->FinalizeRanges();
394 ++num_added;
395
396 auto results_up = pdb_symbol->findAllChildren();
397 if (!results_up)
398 break;
399 while (auto symbol_up = results_up->getNext()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000400 num_added += ParseFunctionBlocksForPDBSymbol(
401 sc, func_file_vm_addr, symbol_up.get(), block, false);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000402 }
403 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000404 default:
405 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000406 }
407 return num_added;
408}
409
Zachary Turner74e08ca2016-03-02 22:05:52 +0000410size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000411SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000412 lldbassert(sc.comp_unit && sc.function);
413 size_t num_added = 0;
414 auto uid = sc.function->GetID();
415 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
416 if (!pdb_func_up)
417 return 0;
418 Block &parent_block = sc.function->GetBlock(false);
419 num_added =
420 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
421 pdb_func_up.get(), &parent_block, true);
422 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000423}
424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000426 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000427 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000428 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000429
430 size_t num_added = 0;
431 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
432 if (!compiland)
433 return 0;
434
435 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
436 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000437 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
438 PDB_SymType::UDT};
Aaron Smith66b84072018-03-14 04:05:27 +0000439 for (auto tag : tags_to_search) {
440 results = raw_sym.findAllChildren(tag);
441 if (!results || results->getChildCount() == 0)
442 continue;
443 while (auto symbol = results->getNext()) {
444 switch (symbol->getSymTag()) {
445 case PDB_SymType::Enum:
446 case PDB_SymType::UDT:
447 case PDB_SymType::Typedef:
448 break;
449 default:
450 continue;
451 }
452
453 // This should cause the type to get cached and stored in the `m_types`
454 // lookup.
455 if (!ResolveTypeUID(symbol->getSymIndexId()))
456 continue;
457
458 ++num_added;
459 }
Aaron Smithec40f812018-01-23 20:35:19 +0000460 }
Aaron Smith66b84072018-03-14 04:05:27 +0000461 };
Aaron Smithec40f812018-01-23 20:35:19 +0000462
Aaron Smith66b84072018-03-14 04:05:27 +0000463 if (sc.function) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000464 auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
465 sc.function->GetID());
Aaron Smith66b84072018-03-14 04:05:27 +0000466 if (!pdb_func)
467 return 0;
468 ParseTypesByTagFn(*pdb_func);
469 } else {
470 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000471
Aaron Smith66b84072018-03-14 04:05:27 +0000472 // Also parse global types particularly coming from this compiland.
Adrian Prantl05097242018-04-30 16:49:04 +0000473 // Unfortunately, PDB has no compiland information for each global type. We
474 // have to parse them all. But ensure we only do this once.
Aaron Smith66b84072018-03-14 04:05:27 +0000475 static bool parse_all_global_types = false;
476 if (!parse_all_global_types) {
477 ParseTypesByTagFn(*m_global_scope_up);
478 parse_all_global_types = true;
479 }
Aaron Smithec40f812018-01-23 20:35:19 +0000480 }
481 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482}
483
484size_t
485SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
486 // TODO: Implement this
487 return size_t();
488}
489
490lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
491 auto find_result = m_types.find(type_uid);
492 if (find_result != m_types.end())
493 return find_result->second.get();
494
495 TypeSystem *type_system =
496 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
497 ClangASTContext *clang_type_system =
498 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
499 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000500 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000501 PDBASTParser *pdb =
502 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
503 if (!pdb)
504 return nullptr;
505
506 auto pdb_type = m_session_up->getSymbolById(type_uid);
507 if (pdb_type == nullptr)
508 return nullptr;
509
510 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithd5a925f2018-03-22 19:21:34 +0000511 if (result) {
Aaron Smith86e94342017-12-22 05:26:50 +0000512 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000513 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000514 if (type_list)
515 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000516 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000517 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000518}
519
Kate Stoneb9c1b512016-09-06 20:57:50 +0000520bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
521 // TODO: Implement this
522 return false;
523}
524
525lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
526 return lldb_private::CompilerDecl();
527}
528
529lldb_private::CompilerDeclContext
530SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
531 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000532 // improve this later but it's not easy because PDB doesn't provide a high
533 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534 return *m_tu_decl_ctx_up;
535}
536
537lldb_private::CompilerDeclContext
538SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
539 return *m_tu_decl_ctx_up;
540}
541
542void SymbolFilePDB::ParseDeclsForContext(
543 lldb_private::CompilerDeclContext decl_ctx) {}
544
545uint32_t
546SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
547 uint32_t resolve_scope,
548 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000549 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000550 if (resolve_scope & eSymbolContextCompUnit ||
551 resolve_scope & eSymbolContextVariable ||
552 resolve_scope & eSymbolContextFunction ||
553 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000554 resolve_scope & eSymbolContextLineEntry) {
555 addr_t file_vm_addr = so_addr.GetFileAddress();
556 auto symbol_up =
557 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
558 if (!symbol_up)
559 return 0;
560
561 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
562 if (!cu_sp) {
563 if (resolved_flags | eSymbolContextVariable) {
564 // TODO: Resolve variables
565 }
566 return 0;
567 }
568 sc.comp_unit = cu_sp.get();
569 resolved_flags |= eSymbolContextCompUnit;
570 lldbassert(sc.module_sp == cu_sp->GetModule());
571
572 switch (symbol_up->getSymTag()) {
573 case PDB_SymType::Function:
574 if (resolve_scope & eSymbolContextFunction) {
575 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
576 assert(pdb_func);
577 auto func_uid = pdb_func->getSymIndexId();
578 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
579 if (sc.function == nullptr)
Aaron Smithe664b5d2018-03-19 21:14:19 +0000580 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000581 if (sc.function) {
582 resolved_flags |= eSymbolContextFunction;
583 if (resolve_scope & eSymbolContextBlock) {
584 Block &block = sc.function->GetBlock(true);
585 sc.block = block.FindBlockByID(sc.function->GetID());
586 if (sc.block)
587 resolved_flags |= eSymbolContextBlock;
588 }
589 }
590 }
591 break;
592 default:
593 break;
594 }
595
596 if (resolve_scope & eSymbolContextLineEntry) {
597 if (auto *line_table = sc.comp_unit->GetLineTable()) {
598 Address addr(so_addr);
599 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
600 resolved_flags |= eSymbolContextLineEntry;
601 }
602 }
603 }
604 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000605}
606
607uint32_t SymbolFilePDB::ResolveSymbolContext(
608 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
609 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000610 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000611 if (resolve_scope & lldb::eSymbolContextCompUnit) {
612 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000613 // file. For example, if `file_spec` is <vector>, then this should return
614 // all source files and header files that reference <vector>, either
615 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000616 auto compilands = m_session_up->findCompilandsForSourceFile(
617 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
618
Aaron Smith10a02572018-01-13 06:58:18 +0000619 if (!compilands)
620 return 0;
621
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000622 // For each one, either find its previously parsed data or parse it afresh
623 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000624 while (auto compiland = compilands->getNext()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000625 // If we're not checking inlines, then don't add line information for
626 // this file unless the FileSpec matches. For inline functions, we don't
627 // have to match the FileSpec since they could be defined in headers
628 // other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000629 if (!check_inlines) {
Aaron Smith487b0c62018-03-20 00:18:22 +0000630 std::string source_file = compiland->getSourceFileFullPath();
Aaron Smith10a02572018-01-13 06:58:18 +0000631 if (source_file.empty())
632 continue;
Pavel Labath2cb7cf82018-05-14 14:52:47 +0000633 FileSpec this_spec(source_file, false, FileSpec::Style::windows);
Aaron Smith10a02572018-01-13 06:58:18 +0000634 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
635 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000636 continue;
637 }
638
639 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000640 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
Aaron Smithd5a925f2018-03-22 19:21:34 +0000641 if (!cu)
Aaron Smith10a02572018-01-13 06:58:18 +0000642 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000643 sc.comp_unit = cu.get();
644 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000645
646 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000647 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000648 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
649 eSymbolContextLineEntry)) {
650 bool has_line_table = ParseCompileUnitLineTable(sc, line);
651
652 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
653 // The query asks for line entries, but we can't get them for the
Adrian Prantl05097242018-04-30 16:49:04 +0000654 // compile unit. This is not normal for `line` = 0. So just assert
655 // it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000656 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000657
658 // Current compiland does not have the requested line. Search next.
659 continue;
660 }
661
662 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
663 if (!has_line_table)
664 continue;
665
666 auto *line_table = sc.comp_unit->GetLineTable();
667 lldbassert(line_table);
668
669 uint32_t num_line_entries = line_table->GetSize();
670 // Skip the terminal line entry.
671 --num_line_entries;
672
Adrian Prantl05097242018-04-30 16:49:04 +0000673 // If `line `!= 0, see if we can resolve function for each line entry
674 // in the line table.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000675 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
676 ++line_idx) {
677 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
678 continue;
679
680 auto file_vm_addr =
681 sc.line_entry.range.GetBaseAddress().GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +0000682 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +0000683 continue;
684
Aaron Smithc8316ed2018-03-22 03:44:51 +0000685 auto symbol_up = m_session_up->findSymbolByAddress(
686 file_vm_addr, PDB_SymType::Function);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000687 if (symbol_up) {
688 auto func_uid = symbol_up->getSymIndexId();
689 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
690 if (sc.function == nullptr) {
691 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
692 assert(pdb_func);
Aaron Smithe664b5d2018-03-19 21:14:19 +0000693 sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000694 }
695 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
696 Block &block = sc.function->GetBlock(true);
697 sc.block = block.FindBlockByID(sc.function->GetID());
698 }
699 }
700 sc_list.Append(sc);
701 }
702 } else if (has_line_table) {
703 // We can parse line table for the compile unit. But no query to
704 // resolve function or block. We append `sc` to the list anyway.
705 sc_list.Append(sc);
706 }
707 } else {
708 // No query for line entry, function or block. But we have a valid
709 // compile unit, append `sc` to the list.
710 sc_list.Append(sc);
711 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000712 }
713 }
Aaron Smith10a02572018-01-13 06:58:18 +0000714 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000715}
716
717uint32_t SymbolFilePDB::FindGlobalVariables(
718 const lldb_private::ConstString &name,
719 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
720 uint32_t max_matches, lldb_private::VariableList &variables) {
721 return uint32_t();
722}
723
724uint32_t
725SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
726 bool append, uint32_t max_matches,
727 lldb_private::VariableList &variables) {
728 return uint32_t();
729}
730
Aaron Smithe664b5d2018-03-19 21:14:19 +0000731bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
Aaron Smith7ac1c782018-02-09 05:31:28 +0000732 bool include_inlines,
733 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000734 lldb_private::SymbolContext sc;
Aaron Smitha3a8cc82018-03-20 00:34:18 +0000735 sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000736 if (!sc.comp_unit)
737 return false;
738 sc.module_sp = sc.comp_unit->GetModule();
Aaron Smitha3a8cc82018-03-20 00:34:18 +0000739 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000740 if (!sc.function)
741 return false;
742
743 sc_list.Append(sc);
744 return true;
745}
746
747bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
748 lldb_private::SymbolContextList &sc_list) {
Aaron Smithc8316ed2018-03-22 03:44:51 +0000749 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000750 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
751 return false;
Aaron Smithe664b5d2018-03-19 21:14:19 +0000752 return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
Aaron Smith7ac1c782018-02-09 05:31:28 +0000753}
754
755void SymbolFilePDB::CacheFunctionNames() {
756 if (!m_func_full_names.IsEmpty())
757 return;
758
759 std::map<uint64_t, uint32_t> addr_ids;
760
761 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
762 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +0000763 if (pdb_func_up->isCompilerGenerated())
764 continue;
765
Aaron Smith7ac1c782018-02-09 05:31:28 +0000766 auto name = pdb_func_up->getName();
767 auto demangled_name = pdb_func_up->getUndecoratedName();
768 if (name.empty() && demangled_name.empty())
769 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000770
Aaron Smithf76fe682018-03-07 03:16:50 +0000771 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000772 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
773 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
774
775 if (auto parent = pdb_func_up->getClassParent()) {
776
777 // PDB have symbols for class/struct methods or static methods in Enum
778 // Class. We won't bother to check if the parent is UDT or Enum here.
779 m_func_method_names.Append(ConstString(name), uid);
780
781 ConstString cstr_name(name);
782
Adrian Prantl05097242018-04-30 16:49:04 +0000783 // To search a method name, like NS::Class:MemberFunc, LLDB searches
784 // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
785 // not have inforamtion of this, we extract base names and cache them
786 // by our own effort.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000787 llvm::StringRef basename;
788 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
789 if (cpp_method.IsValid()) {
790 llvm::StringRef context;
791 basename = cpp_method.GetBasename();
792 if (basename.empty())
793 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
794 context, basename);
795 }
796
797 if (!basename.empty())
798 m_func_base_names.Append(ConstString(basename), uid);
799 else {
800 m_func_base_names.Append(ConstString(name), uid);
801 }
802
803 if (!demangled_name.empty())
804 m_func_full_names.Append(ConstString(demangled_name), uid);
805
806 } else {
807 // Handle not-method symbols.
808
809 // The function name might contain namespace, or its lexical scope. It
810 // is not safe to get its base name by applying same scheme as we deal
811 // with the method names.
812 // FIXME: Remove namespace if function is static in a scope.
813 m_func_base_names.Append(ConstString(name), uid);
814
815 if (name == "main") {
816 m_func_full_names.Append(ConstString(name), uid);
817
818 if (!demangled_name.empty() && name != demangled_name) {
819 m_func_full_names.Append(ConstString(demangled_name), uid);
820 m_func_base_names.Append(ConstString(demangled_name), uid);
821 }
822 } else if (!demangled_name.empty()) {
823 m_func_full_names.Append(ConstString(demangled_name), uid);
824 } else {
825 m_func_full_names.Append(ConstString(name), uid);
826 }
827 }
828 }
829 }
830
831 if (auto results_up =
Aaron Smithc8316ed2018-03-22 03:44:51 +0000832 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000833 while (auto pub_sym_up = results_up->getNext()) {
834 if (!pub_sym_up->isFunction())
835 continue;
836 auto name = pub_sym_up->getName();
837 if (name.empty())
838 continue;
839
840 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000841 auto vm_addr = pub_sym_up->getVirtualAddress();
842
843 // PDB public symbol has mangled name for its associated function.
844 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
845 // Cache mangled name.
846 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
847 }
848 }
849 }
850 }
851 // Sort them before value searching is working properly
852 m_func_full_names.Sort();
853 m_func_full_names.SizeToFit();
854 m_func_method_names.Sort();
855 m_func_method_names.SizeToFit();
856 m_func_base_names.Sort();
857 m_func_base_names.SizeToFit();
858}
859
Kate Stoneb9c1b512016-09-06 20:57:50 +0000860uint32_t SymbolFilePDB::FindFunctions(
861 const lldb_private::ConstString &name,
862 const lldb_private::CompilerDeclContext *parent_decl_ctx,
863 uint32_t name_type_mask, bool include_inlines, bool append,
864 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000865 if (!append)
866 sc_list.Clear();
867 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
868
869 if (name_type_mask == eFunctionNameTypeNone)
870 return 0;
871 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
872 return 0;
873 if (name.IsEmpty())
874 return 0;
875
876 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000877 if (name_type_mask & eFunctionNameTypeFull ||
878 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000879 name_type_mask & eFunctionNameTypeMethod) {
880 CacheFunctionNames();
881
882 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000883 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
884 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000885 std::vector<uint32_t> ids;
886 if (Names.GetValues(name, ids)) {
887 for (auto id : ids) {
888 if (resolved_ids.find(id) == resolved_ids.end()) {
889 if (ResolveFunction(id, include_inlines, sc_list))
890 resolved_ids.insert(id);
891 }
892 }
893 }
894 };
895 if (name_type_mask & eFunctionNameTypeFull) {
896 ResolveFn(m_func_full_names);
897 }
898 if (name_type_mask & eFunctionNameTypeBase) {
899 ResolveFn(m_func_base_names);
900 }
901 if (name_type_mask & eFunctionNameTypeMethod) {
902 ResolveFn(m_func_method_names);
903 }
904 }
905 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000906}
907
908uint32_t
909SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
910 bool include_inlines, bool append,
911 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000912 if (!append)
913 sc_list.Clear();
914 if (!regex.IsValid())
915 return 0;
916
917 auto old_size = sc_list.GetSize();
918 CacheFunctionNames();
919
920 std::set<uint32_t> resolved_ids;
Aaron Smithc8316ed2018-03-22 03:44:51 +0000921 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids,
922 this](UniqueCStringMap<uint32_t> &Names) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000923 std::vector<uint32_t> ids;
924 if (Names.GetValues(regex, ids)) {
925 for (auto id : ids) {
926 if (resolved_ids.find(id) == resolved_ids.end())
927 if (ResolveFunction(id, include_inlines, sc_list))
928 resolved_ids.insert(id);
929 }
930 }
931 };
932 ResolveFn(m_func_full_names);
933 ResolveFn(m_func_base_names);
934
935 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000936}
937
938void SymbolFilePDB::GetMangledNamesForFunction(
939 const std::string &scope_qualified_name,
940 std::vector<lldb_private::ConstString> &mangled_names) {}
941
942uint32_t SymbolFilePDB::FindTypes(
943 const lldb_private::SymbolContext &sc,
944 const lldb_private::ConstString &name,
945 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
946 uint32_t max_matches,
947 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
948 lldb_private::TypeMap &types) {
949 if (!append)
950 types.Clear();
951 if (!name)
952 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000953 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
954 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000955
956 searched_symbol_files.clear();
957 searched_symbol_files.insert(this);
958
959 std::string name_str = name.AsCString();
960
Aaron Smith86e94342017-12-22 05:26:50 +0000961 // There is an assumption 'name' is not a regex
962 FindTypesByName(name_str, max_matches, types);
Aaron Smithc8316ed2018-03-22 03:44:51 +0000963
Kate Stoneb9c1b512016-09-06 20:57:50 +0000964 return types.GetSize();
965}
966
Aaron Smithc8316ed2018-03-22 03:44:51 +0000967void SymbolFilePDB::FindTypesByRegex(
968 const lldb_private::RegularExpression &regex, uint32_t max_matches,
969 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000970 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000971 // space as much as possible since this searches EVERYTHING in the PDB,
972 // manually doing regex comparisons. PDB library isn't optimized for regex
973 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +0000974 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000975 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000976 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
977 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +0000978 std::unique_ptr<IPDBEnumSymbols> results;
979
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 uint32_t matches = 0;
981
982 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +0000983 results = m_global_scope_up->findAllChildren(tag);
984 if (!results)
985 continue;
986
Kate Stoneb9c1b512016-09-06 20:57:50 +0000987 while (auto result = results->getNext()) {
988 if (max_matches > 0 && matches >= max_matches)
989 break;
990
991 std::string type_name;
992 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
993 type_name = enum_type->getName();
994 else if (auto typedef_type =
Aaron Smithc8316ed2018-03-22 03:44:51 +0000995 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000996 type_name = typedef_type->getName();
997 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
998 type_name = class_type->getName();
999 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001000 // We're looking only for types that have names. Skip symbols, as well
1001 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001002 continue;
1003 }
1004
Aaron Smith86e94342017-12-22 05:26:50 +00001005 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001006 continue;
1007
1008 // This should cause the type to get cached and stored in the `m_types`
1009 // lookup.
1010 if (!ResolveTypeUID(result->getSymIndexId()))
1011 continue;
1012
1013 auto iter = m_types.find(result->getSymIndexId());
1014 if (iter == m_types.end())
1015 continue;
1016 types.Insert(iter->second);
1017 ++matches;
1018 }
1019 }
1020}
1021
1022void SymbolFilePDB::FindTypesByName(const std::string &name,
1023 uint32_t max_matches,
1024 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001025 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001026 if (name.empty())
1027 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001028 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1029 PDB_NameSearchFlags::NS_Default);
1030 if (!results)
1031 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001032
1033 uint32_t matches = 0;
1034
1035 while (auto result = results->getNext()) {
1036 if (max_matches > 0 && matches >= max_matches)
1037 break;
1038 switch (result->getSymTag()) {
1039 case PDB_SymType::Enum:
1040 case PDB_SymType::UDT:
1041 case PDB_SymType::Typedef:
1042 break;
1043 default:
Adrian Prantl05097242018-04-30 16:49:04 +00001044 // We're looking only for types that have names. Skip symbols, as well
1045 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001046 continue;
1047 }
1048
1049 // This should cause the type to get cached and stored in the `m_types`
1050 // lookup.
1051 if (!ResolveTypeUID(result->getSymIndexId()))
1052 continue;
1053
1054 auto iter = m_types.find(result->getSymIndexId());
1055 if (iter == m_types.end())
1056 continue;
1057 types.Insert(iter->second);
1058 ++matches;
1059 }
1060}
1061
1062size_t SymbolFilePDB::FindTypes(
1063 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1064 lldb_private::TypeMap &types) {
1065 return 0;
1066}
1067
Aaron Smithec40f812018-01-23 20:35:19 +00001068lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1069 return m_obj_file->GetModule()->GetTypeList();
1070}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071
Aaron Smithc8316ed2018-03-22 03:44:51 +00001072void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
1073 uint32_t type_mask,
1074 TypeCollection &type_collection) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001075 bool can_parse = false;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001076 switch (pdb_symbol.getSymTag()) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001077 case PDB_SymType::ArrayType:
1078 can_parse = ((type_mask & eTypeClassArray) != 0);
1079 break;
1080 case PDB_SymType::BuiltinType:
1081 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1082 break;
1083 case PDB_SymType::Enum:
1084 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1085 break;
1086 case PDB_SymType::Function:
1087 case PDB_SymType::FunctionSig:
1088 can_parse = ((type_mask & eTypeClassFunction) != 0);
1089 break;
1090 case PDB_SymType::PointerType:
1091 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1092 eTypeClassMemberPointer)) != 0);
1093 break;
1094 case PDB_SymType::Typedef:
1095 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1096 break;
1097 case PDB_SymType::UDT: {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001098 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001099 assert(udt);
1100 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
Aaron Smithc8316ed2018-03-22 03:44:51 +00001101 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1102 eTypeClassUnion)) != 0));
Aaron Smith7ac1c782018-02-09 05:31:28 +00001103 } break;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001104 default:
1105 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001106 }
1107
1108 if (can_parse) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001109 if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001110 auto result =
1111 std::find(type_collection.begin(), type_collection.end(), type);
1112 if (result == type_collection.end())
1113 type_collection.push_back(type);
1114 }
1115 }
1116
Aaron Smithe664b5d2018-03-19 21:14:19 +00001117 auto results_up = pdb_symbol.findAllChildren();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001118 while (auto symbol_up = results_up->getNext())
Aaron Smithe664b5d2018-03-19 21:14:19 +00001119 GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001120}
1121
Kate Stoneb9c1b512016-09-06 20:57:50 +00001122size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1123 uint32_t type_mask,
1124 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001125 TypeCollection type_collection;
1126 uint32_t old_size = type_list.GetSize();
Aaron Smithc8316ed2018-03-22 03:44:51 +00001127 CompileUnit *cu =
1128 sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001129 if (cu) {
1130 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
Aaron Smithe664b5d2018-03-19 21:14:19 +00001131 if (!compiland_up)
1132 return 0;
1133 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001134 } else {
1135 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1136 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
Aaron Smithd5a925f2018-03-22 19:21:34 +00001137 if (cu_sp) {
Aaron Smithe664b5d2018-03-19 21:14:19 +00001138 if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
1139 GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001140 }
1141 }
1142 }
1143
1144 for (auto type : type_collection) {
1145 type->GetForwardCompilerType();
1146 type_list.Insert(type->shared_from_this());
1147 }
1148 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001149}
1150
1151lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001152SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1153 auto type_system =
1154 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1155 if (type_system)
1156 type_system->SetSymbolFile(this);
1157 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001158}
1159
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1161 const lldb_private::SymbolContext &sc,
1162 const lldb_private::ConstString &name,
1163 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1164 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001165}
1166
Kate Stoneb9c1b512016-09-06 20:57:50 +00001167lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1168 static ConstString g_name("pdb");
1169 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001170}
1171
Kate Stoneb9c1b512016-09-06 20:57:50 +00001172uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1173
1174IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1175
1176const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1177 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001178}
1179
Aaron Smithc8316ed2018-03-22 03:44:51 +00001180lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
1181 uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001182 auto found_cu = m_comp_units.find(id);
1183 if (found_cu != m_comp_units.end())
1184 return found_cu->second;
1185
Aaron Smith10a02572018-01-13 06:58:18 +00001186 auto compiland_up = GetPDBCompilandByUID(id);
1187 if (!compiland_up)
1188 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189
1190 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001191 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001192 if (!details)
1193 lang = lldb::eLanguageTypeC_plus_plus;
1194 else
1195 lang = TranslateLanguage(details->getLanguage());
1196
Aaron Smithf76fe682018-03-07 03:16:50 +00001197 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1198 return CompUnitSP();
1199
Aaron Smith487b0c62018-03-20 00:18:22 +00001200 std::string path = compiland_up->getSourceFileFullPath();
Aaron Smithf76fe682018-03-07 03:16:50 +00001201 if (path.empty())
1202 return CompUnitSP();
1203
Kate Stoneb9c1b512016-09-06 20:57:50 +00001204 // Don't support optimized code for now, DebugInfoPDB does not return this
1205 // information.
1206 LazyBool optimized = eLazyBoolNo;
Aaron Smithc8316ed2018-03-22 03:44:51 +00001207 auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
1208 path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001209
1210 if (!cu_sp)
1211 return CompUnitSP();
1212
1213 m_comp_units.insert(std::make_pair(id, cu_sp));
1214 if (index == UINT32_MAX)
Aaron Smithe664b5d2018-03-19 21:14:19 +00001215 GetCompileUnitIndex(*compiland_up, index);
Aaron Smith10a02572018-01-13 06:58:18 +00001216 lldbassert(index != UINT32_MAX);
Aaron Smithc8316ed2018-03-22 03:44:51 +00001217 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
1218 cu_sp);
Aaron Smith10a02572018-01-13 06:58:18 +00001219 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001220}
1221
Kate Stoneb9c1b512016-09-06 20:57:50 +00001222bool SymbolFilePDB::ParseCompileUnitLineTable(
1223 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001224 lldbassert(sc.comp_unit);
1225
1226 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1227 if (!compiland_up)
1228 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001231 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
Adrian Prantl05097242018-04-30 16:49:04 +00001232 // a globally unique idenfitifier in the namespace of the PDB. So, we have
1233 // to do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001234 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001235 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001237
Aaron Smith10a02572018-01-13 06:58:18 +00001238 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001239 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001240 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1241 if (!files)
1242 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001243
Adrian Prantl05097242018-04-30 16:49:04 +00001244 // For each source and header file, create a LineSequence for contributions
1245 // to the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246 while (auto file = files->getNext()) {
1247 std::unique_ptr<LineSequence> sequence(
1248 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001249 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1250 if (!lines)
1251 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001252 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 uint64_t prev_addr;
1255 uint32_t prev_length;
1256 uint32_t prev_line;
1257 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001258
Kate Stoneb9c1b512016-09-06 20:57:50 +00001259 for (int i = 0; i < entry_count; ++i) {
1260 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001261
Kate Stoneb9c1b512016-09-06 20:57:50 +00001262 uint64_t lno = line->getLineNumber();
1263 uint64_t addr = line->getVirtualAddress();
1264 uint32_t length = line->getLength();
1265 uint32_t source_id = line->getSourceFileId();
1266 uint32_t col = line->getColumnNumber();
1267 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001268
Adrian Prantl05097242018-04-30 16:49:04 +00001269 // There was a gap between the current entry and the previous entry if
1270 // the addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001271 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001274 // of the previous entry's address range if the current entry resulted in
1275 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1277 line_table->AppendLineEntryToSequence(
1278 sequence.get(), prev_addr + prev_length, prev_line, 0,
1279 prev_source_idx, false, false, false, false, true);
1280 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001281
Kate Stoneb9c1b512016-09-06 20:57:50 +00001282 if (ShouldAddLine(match_line, lno, length)) {
1283 bool is_statement = line->isStatement();
1284 bool is_prologue = false;
1285 bool is_epilogue = false;
1286 auto func =
1287 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1288 if (func) {
1289 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001290 if (prologue)
1291 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001292
Kate Stoneb9c1b512016-09-06 20:57:50 +00001293 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001294 if (epilogue)
1295 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001296 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1299 source_idx, is_statement, false,
1300 is_prologue, is_epilogue, false);
1301 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001302
Kate Stoneb9c1b512016-09-06 20:57:50 +00001303 prev_addr = addr;
1304 prev_length = length;
1305 prev_line = lno;
1306 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001307 }
1308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1310 // The end is always a terminal entry, so insert it regardless.
1311 line_table->AppendLineEntryToSequence(
1312 sequence.get(), prev_addr + prev_length, prev_line, 0,
1313 prev_source_idx, false, false, false, false, true);
1314 }
1315
1316 line_table->InsertSequence(sequence.release());
1317 }
1318
Aaron Smith10a02572018-01-13 06:58:18 +00001319 if (line_table->GetSize()) {
1320 sc.comp_unit->SetLineTable(line_table.release());
1321 return true;
1322 }
1323 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001324}
1325
Kate Stoneb9c1b512016-09-06 20:57:50 +00001326void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001327 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
Adrian Prantl05097242018-04-30 16:49:04 +00001329 // This is a hack, but we need to convert the source id into an index into
1330 // the support files array. We don't want to do path comparisons to avoid
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001331 // basename / full path issues that may or may not even be a problem, so we
1332 // use the globally unique source file identifiers. Ideally we could use the
1333 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001334 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1335 if (!source_files)
1336 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001338
Kate Stoneb9c1b512016-09-06 20:57:50 +00001339 while (auto file = source_files->getNext()) {
1340 uint32_t source_id = file->getUniqueId();
1341 index_map[source_id] = index++;
1342 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001343}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001344
1345lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
Aaron Smith308e39c2018-03-22 19:26:33 +00001346 const lldb_private::Address &so_addr) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001347 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
Aaron Smith308e39c2018-03-22 19:26:33 +00001348 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
Aaron Smith7ac1c782018-02-09 05:31:28 +00001349 return nullptr;
1350
Aaron Smith308e39c2018-03-22 19:26:33 +00001351 // If it is a PDB function's vm addr, this is the first sure bet.
1352 if (auto lines =
1353 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
1354 if (auto first_line = lines->getNext())
1355 return ParseCompileUnitForUID(first_line->getCompilandId());
Aaron Smith7ac1c782018-02-09 05:31:28 +00001356 }
1357
Aaron Smith308e39c2018-03-22 19:26:33 +00001358 // Otherwise we resort to section contributions.
1359 if (auto sec_contribs = m_session_up->getSectionContribs()) {
1360 while (auto section = sec_contribs->getNext()) {
1361 auto va = section->getVirtualAddress();
1362 if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
1363 return ParseCompileUnitForUID(section->getCompilandId());
1364 }
1365 }
Aaron Smith7ac1c782018-02-09 05:31:28 +00001366 return nullptr;
1367}
1368
1369Mangled
Aaron Smithe664b5d2018-03-19 21:14:19 +00001370SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001371 Mangled mangled;
Aaron Smithe664b5d2018-03-19 21:14:19 +00001372 auto func_name = pdb_func.getName();
1373 auto func_undecorated_name = pdb_func.getUndecoratedName();
Aaron Smith7ac1c782018-02-09 05:31:28 +00001374 std::string func_decorated_name;
1375
1376 // Seek from public symbols for non-static function's decorated name if any.
1377 // For static functions, they don't have undecorated names and aren't exposed
1378 // in Public Symbols either.
1379 if (!func_undecorated_name.empty()) {
Aaron Smithc8316ed2018-03-22 03:44:51 +00001380 auto result_up = m_global_scope_up->findChildren(
1381 PDB_SymType::PublicSymbol, func_undecorated_name,
1382 PDB_NameSearchFlags::NS_UndecoratedName);
Aaron Smith7ac1c782018-02-09 05:31:28 +00001383 if (result_up) {
1384 while (auto symbol_up = result_up->getNext()) {
1385 // For a public symbol, it is unique.
1386 lldbassert(result_up->getChildCount() == 1);
1387 if (auto *pdb_public_sym =
Aaron Smithc8316ed2018-03-22 03:44:51 +00001388 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
1389 symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001390 if (pdb_public_sym->isFunction()) {
1391 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001392 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001393 }
1394 }
1395 }
1396 }
1397 }
1398 if (!func_decorated_name.empty()) {
1399 mangled.SetMangledName(ConstString(func_decorated_name));
1400
1401 // For MSVC, format of C funciton's decorated name depends on calling
1402 // conventon. Unfortunately none of the format is recognized by current
1403 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
Adrian Prantl05097242018-04-30 16:49:04 +00001404 // `__purecall` is retrieved as both its decorated and undecorated name
1405 // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
1406 // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
1407 // Mangled::GetDemangledName method will fail internally and caches an
1408 // empty string as its undecorated name. So we will face a contradition
1409 // here for the same symbol:
Aaron Smith7ac1c782018-02-09 05:31:28 +00001410 // non-empty undecorated name from PDB
1411 // empty undecorated name from LLDB
1412 if (!func_undecorated_name.empty() &&
1413 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1414 mangled.SetDemangledName(ConstString(func_undecorated_name));
1415
1416 // LLDB uses several flags to control how a C++ decorated name is
Adrian Prantl05097242018-04-30 16:49:04 +00001417 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
1418 // yielded name could be different from what we retrieve from
Aaron Smith7ac1c782018-02-09 05:31:28 +00001419 // PDB source unless we also apply same flags in getting undecorated
1420 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1421 if (!func_undecorated_name.empty() &&
1422 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1423 ConstString(func_undecorated_name))
1424 mangled.SetDemangledName(ConstString(func_undecorated_name));
1425 } else if (!func_undecorated_name.empty()) {
1426 mangled.SetDemangledName(ConstString(func_undecorated_name));
1427 } else if (!func_name.empty())
1428 mangled.SetValue(ConstString(func_name), false);
1429
1430 return mangled;
1431}
1432
1433bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1434 const lldb_private::CompilerDeclContext *decl_ctx) {
1435 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1436 return true;
1437
1438 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1439 if (!decl_ctx_type_system)
1440 return false;
1441 TypeSystem *type_system = GetTypeSystemForLanguage(
1442 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1443 if (decl_ctx_type_system == type_system)
1444 return true; // The type systems match, return true
1445
1446 return false;
1447}