blob: 871da7a247e0b0b92ffa19dd1eb9c97cf7b3ebb7 [file] [log] [blame]
Zachary Turner74e08ca2016-03-02 22:05:52 +00001//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "SymbolFilePDB.h"
11
Zachary Turner42dff792016-04-15 00:21:26 +000012#include "clang/Lex/Lexer.h"
13
Zachary Turner74e08ca2016-03-02 22:05:52 +000014#include "lldb/Core/Module.h"
15#include "lldb/Core/PluginManager.h"
Zachary Turner42dff792016-04-15 00:21:26 +000016#include "lldb/Symbol/ClangASTContext.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/LineTable.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Symbol/SymbolContext.h"
Aaron Smith10a02572018-01-13 06:58:18 +000021#include "lldb/Symbol/SymbolVendor.h"
Zachary Turner42dff792016-04-15 00:21:26 +000022#include "lldb/Symbol/TypeMap.h"
Aaron Smithec40f812018-01-23 20:35:19 +000023#include "lldb/Symbol/TypeList.h"
Aaron Smith86e94342017-12-22 05:26:50 +000024#include "lldb/Utility/RegularExpression.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000025
Pavel Labathb8d8c622016-05-09 11:07:43 +000026#include "llvm/DebugInfo/PDB/GenericError.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000027#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000028#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
29#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
30#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000031#include "llvm/DebugInfo/PDB/IPDBTable.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000032#include "llvm/DebugInfo/PDB/PDBSymbol.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000033#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000034#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
Aaron Smith1f8552a2017-12-22 00:04:36 +000036#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner74e08ca2016-03-02 22:05:52 +000037#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
38#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
39#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Aaron Smith7ac1c782018-02-09 05:31:28 +000041#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
Zachary Turner42dff792016-04-15 00:21:26 +000042#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
44#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
45
Aaron Smith7ac1c782018-02-09 05:31:28 +000046#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
Zachary Turner42dff792016-04-15 00:21:26 +000047#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
48
49#include <regex>
Zachary Turner74e08ca2016-03-02 22:05:52 +000050
Aaron Smith10a02572018-01-13 06:58:18 +000051using namespace lldb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000052using namespace lldb_private;
Zachary Turner54fd7ff2016-05-04 20:33:53 +000053using namespace llvm::pdb;
Zachary Turner74e08ca2016-03-02 22:05:52 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055namespace {
56lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
57 switch (lang) {
58 case PDB_Lang::Cpp:
59 return lldb::LanguageType::eLanguageTypeC_plus_plus;
60 case PDB_Lang::C:
61 return lldb::LanguageType::eLanguageTypeC;
62 default:
63 return lldb::LanguageType::eLanguageTypeUnknown;
64 }
Zachary Turner74e08ca2016-03-02 22:05:52 +000065}
66
Kate Stoneb9c1b512016-09-06 20:57:50 +000067bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
68 uint32_t addr_length) {
69 return ((requested_line == 0 || actual_line == requested_line) &&
70 addr_length > 0);
71}
Zachary Turner74e08ca2016-03-02 22:05:52 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074void SymbolFilePDB::Initialize() {
75 PluginManager::RegisterPlugin(GetPluginNameStatic(),
76 GetPluginDescriptionStatic(), CreateInstance,
77 DebuggerInitialize);
Zachary Turner74e08ca2016-03-02 22:05:52 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void SymbolFilePDB::Terminate() {
81 PluginManager::UnregisterPlugin(CreateInstance);
Zachary Turner74e08ca2016-03-02 22:05:52 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
85
86lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
87 static ConstString g_name("pdb");
88 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +000089}
90
Kate Stoneb9c1b512016-09-06 20:57:50 +000091const char *SymbolFilePDB::GetPluginDescriptionStatic() {
92 return "Microsoft PDB debug symbol file reader.";
Zachary Turner74e08ca2016-03-02 22:05:52 +000093}
94
95lldb_private::SymbolFile *
Kate Stoneb9c1b512016-09-06 20:57:50 +000096SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
97 return new SymbolFilePDB(obj_file);
Zachary Turner74e08ca2016-03-02 22:05:52 +000098}
99
100SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
Aaron Smith10a02572018-01-13 06:58:18 +0000101 : SymbolFile(object_file), m_session_up(), m_global_scope_up(),
102 m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104SymbolFilePDB::~SymbolFilePDB() {}
Zachary Turner74e08ca2016-03-02 22:05:52 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106uint32_t SymbolFilePDB::CalculateAbilities() {
Aaron Smith1f8552a2017-12-22 00:04:36 +0000107 uint32_t abilities = 0;
108 if (!m_obj_file)
109 return 0;
110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 if (!m_session_up) {
112 // Lazily load and match the PDB file, but only do this once.
113 std::string exePath = m_obj_file->GetFileSpec().GetPath();
114 auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
115 m_session_up);
116 if (error) {
117 llvm::consumeError(std::move(error));
Aaron Smith1f8552a2017-12-22 00:04:36 +0000118 auto module_sp = m_obj_file->GetModule();
119 if (!module_sp)
120 return 0;
121 // See if any symbol file is specified through `--symfile` option.
122 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
123 if (!symfile)
124 return 0;
125 error = loadDataForPDB(PDB_ReaderType::DIA,
126 llvm::StringRef(symfile.GetPath()),
127 m_session_up);
128 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 Smith1f8552a2017-12-22 00:04:36 +0000134 if (!m_session_up.get())
135 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
148 abilities |= (CompileUnits | Functions | Blocks |
149 GlobalVariables | LocalVariables | VariableTypes);
150 break;
151 case PDB_TableType::LineNumbers:
152 abilities |= LineTables;
153 break;
154 default: break;
155 }
156 }
157 return abilities;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160void SymbolFilePDB::InitializeObject() {
161 lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
Aaron Smith10a02572018-01-13 06:58:18 +0000162 lldbassert(obj_load_address &&
163 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
185 // *.dll. For example, a compiland with name `Import:KERNEL32.dll`
186 // could be found as a child of the global scope (PDB executable).
187 // Usually, such compilands contain `thunk` symbols in which we are not
188 // interested for now. However we still count them in the compiland list.
189 // If we perform any compiland related activity, like finding symbols
190 // through llvm::pdb::IPDBSession methods, such compilands will all be
191 // searched 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(
208 const llvm::pdb::PDBSymbolCompiland *pdb_compiland,
209 uint32_t &index) {
210 if (!pdb_compiland)
211 return;
212
213 auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
214 if (!results_up)
215 return;
216 auto uid = pdb_compiland->getSymIndexId();
Raphael Isemannfbdf0b92018-01-22 06:56:09 +0000217 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
Aaron Smith10a02572018-01-13 06:58:18 +0000218 auto compiland_up = results_up->getChildAtIndex(cu_idx);
219 if (!compiland_up)
220 continue;
221 if (compiland_up->getSymIndexId() == uid) {
222 index = cu_idx;
223 return;
224 }
225 }
226 index = UINT32_MAX;
227 return;
228}
229
230std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
231SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
232 return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
Aaron Smith10a02572018-01-13 06:58:18 +0000236 if (index >= GetNumCompileUnits())
237 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238
Aaron Smith10a02572018-01-13 06:58:18 +0000239 // Assuming we always retrieve same compilands listed in same order through
240 // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
241 // compile unit makes no sense.
242 auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
243 if (!results)
244 return CompUnitSP();
245 auto compiland_up = results->getChildAtIndex(index);
246 if (!compiland_up)
247 return CompUnitSP();
248 return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000249}
250
251lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000252SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
253 // What fields should I expect to be filled out on the SymbolContext? Is it
254 // safe to assume that `sc.comp_unit` is valid?
255 if (!sc.comp_unit)
256 return lldb::eLanguageTypeUnknown;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000257
Aaron Smith10a02572018-01-13 06:58:18 +0000258 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
259 if (!compiland_up)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 return lldb::eLanguageTypeUnknown;
Aaron Smith10a02572018-01-13 06:58:18 +0000261 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 if (!details)
263 return lldb::eLanguageTypeUnknown;
264 return TranslateLanguage(details->getLanguage());
Zachary Turner74e08ca2016-03-02 22:05:52 +0000265}
266
Aaron Smith7ac1c782018-02-09 05:31:28 +0000267lldb_private::Function *
268SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
269 const PDBSymbolFunc *pdb_func,
270 const lldb_private::SymbolContext &sc) {
271 assert(pdb_func != nullptr);
272 lldbassert(sc.comp_unit && sc.module_sp.get());
273
274 auto file_vm_addr = pdb_func->getVirtualAddress();
275 if (file_vm_addr == LLDB_INVALID_ADDRESS)
276 return nullptr;
277
278 auto func_length = pdb_func->getLength();
279 AddressRange func_range = AddressRange(file_vm_addr,
280 func_length,
281 sc.module_sp->GetSectionList());
282 if (!func_range.GetBaseAddress().IsValid())
283 return nullptr;
284
Aaron Smith7ac1c782018-02-09 05:31:28 +0000285 lldb_private::Type* func_type = ResolveTypeUID(pdb_func->getSymIndexId());
286 if (!func_type)
287 return nullptr;
288
Aaron Smithf76fe682018-03-07 03:16:50 +0000289 user_id_t func_type_uid = pdb_func->getSignatureId();
290
Aaron Smith7ac1c782018-02-09 05:31:28 +0000291 Mangled mangled = GetMangledForPDBFunc(pdb_func);
292
293 FunctionSP func_sp = std::make_shared<Function>(sc.comp_unit,
294 pdb_func->getSymIndexId(),
295 func_type_uid,
296 mangled,
297 func_type,
298 func_range);
299
300 sc.comp_unit->AddFunction(func_sp);
301 return func_sp.get();
302}
303
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304size_t SymbolFilePDB::ParseCompileUnitFunctions(
305 const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000306 lldbassert(sc.comp_unit);
307 size_t func_added = 0;
308 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
309 if (!compiland_up)
310 return 0;
311 auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
312 if (!results_up)
313 return 0;
314 while (auto pdb_func_up = results_up->getNext()) {
315 auto func_sp =
316 sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
317 if (!func_sp) {
318 if (ParseCompileUnitFunctionForPDBFunc(pdb_func_up.get(), sc))
319 ++func_added;
320 }
321 }
322 return func_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000323}
324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325bool SymbolFilePDB::ParseCompileUnitLineTable(
326 const lldb_private::SymbolContext &sc) {
Aaron Smith10a02572018-01-13 06:58:18 +0000327 lldbassert(sc.comp_unit);
328 if (sc.comp_unit->GetLineTable())
329 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000330 return ParseCompileUnitLineTable(sc, 0);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000331}
332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333bool SymbolFilePDB::ParseCompileUnitDebugMacros(
334 const lldb_private::SymbolContext &sc) {
335 // PDB doesn't contain information about macros
336 return false;
337}
338
339bool SymbolFilePDB::ParseCompileUnitSupportFiles(
340 const lldb_private::SymbolContext &sc,
341 lldb_private::FileSpecList &support_files) {
Aaron Smith10a02572018-01-13 06:58:18 +0000342 lldbassert(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +0000343
Kate Stoneb9c1b512016-09-06 20:57:50 +0000344 // In theory this is unnecessary work for us, because all of this information
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000345 // is easily (and quickly) accessible from DebugInfoPDB, so caching it a
346 // second time seems like a waste. Unfortunately, there's no good way around
347 // this short of a moderate refactor since SymbolVendor depends on being able
348 // to cache this list.
Aaron Smith10a02572018-01-13 06:58:18 +0000349 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
350 if (!compiland_up)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000351 return false;
Aaron Smith10a02572018-01-13 06:58:18 +0000352 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 if (!files || files->getChildCount() == 0)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000354 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000355
356 while (auto file = files->getNext()) {
Aaron Smith10a02572018-01-13 06:58:18 +0000357 FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows);
358 support_files.AppendIfUnique(spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 }
360 return true;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000361}
362
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363bool SymbolFilePDB::ParseImportedModules(
364 const lldb_private::SymbolContext &sc,
365 std::vector<lldb_private::ConstString> &imported_modules) {
366 // PDB does not yet support module debug info
367 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000368}
369
Aaron Smith7ac1c782018-02-09 05:31:28 +0000370static size_t
371ParseFunctionBlocksForPDBSymbol(const lldb_private::SymbolContext &sc,
372 uint64_t func_file_vm_addr,
373 const llvm::pdb::PDBSymbol *pdb_symbol,
374 lldb_private::Block *parent_block,
375 bool is_top_parent) {
376 assert(pdb_symbol && parent_block);
377
378 size_t num_added = 0;
379 switch (pdb_symbol->getSymTag()) {
380 case PDB_SymType::Block:
381 case PDB_SymType::Function: {
382 Block *block = nullptr;
383 auto &raw_sym = pdb_symbol->getRawSymbol();
384 if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
385 if (pdb_func->hasNoInlineAttribute())
386 break;
387 if (is_top_parent)
388 block = parent_block;
389 else
390 break;
391 } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
392 auto uid = pdb_symbol->getSymIndexId();
393 if (parent_block->FindBlockByID(uid))
394 break;
395 if (raw_sym.getVirtualAddress() < func_file_vm_addr)
396 break;
397
398 auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
399 parent_block->AddChild(block_sp);
400 block = block_sp.get();
401 } else
402 llvm_unreachable("Unexpected PDB symbol!");
403
404 block->AddRange(
405 Block::Range(raw_sym.getVirtualAddress() - func_file_vm_addr,
406 raw_sym.getLength()));
407 block->FinalizeRanges();
408 ++num_added;
409
410 auto results_up = pdb_symbol->findAllChildren();
411 if (!results_up)
412 break;
413 while (auto symbol_up = results_up->getNext()) {
414 num_added += ParseFunctionBlocksForPDBSymbol(sc, func_file_vm_addr,
415 symbol_up.get(),
416 block, false);
417 }
418 } break;
419 default: break;
420 }
421 return num_added;
422}
423
Zachary Turner74e08ca2016-03-02 22:05:52 +0000424size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000426 lldbassert(sc.comp_unit && sc.function);
427 size_t num_added = 0;
428 auto uid = sc.function->GetID();
429 auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
430 if (!pdb_func_up)
431 return 0;
432 Block &parent_block = sc.function->GetBlock(false);
433 num_added =
434 ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
435 pdb_func_up.get(), &parent_block, true);
436 return num_added;
Zachary Turner74e08ca2016-03-02 22:05:52 +0000437}
438
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
Aaron Smithec40f812018-01-23 20:35:19 +0000440 lldbassert(sc.module_sp.get());
Aaron Smith66b84072018-03-14 04:05:27 +0000441 if (!sc.comp_unit)
Aaron Smithec40f812018-01-23 20:35:19 +0000442 return 0;
Aaron Smith66b84072018-03-14 04:05:27 +0000443
444 size_t num_added = 0;
445 auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
446 if (!compiland)
447 return 0;
448
449 auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
450 std::unique_ptr<IPDBEnumSymbols> results;
451 PDB_SymType tags_to_search[] = { PDB_SymType::Enum, PDB_SymType::Typedef,
452 PDB_SymType::UDT };
453 for (auto tag : tags_to_search) {
454 results = raw_sym.findAllChildren(tag);
455 if (!results || results->getChildCount() == 0)
456 continue;
457 while (auto symbol = results->getNext()) {
458 switch (symbol->getSymTag()) {
459 case PDB_SymType::Enum:
460 case PDB_SymType::UDT:
461 case PDB_SymType::Typedef:
462 break;
463 default:
464 continue;
465 }
466
467 // This should cause the type to get cached and stored in the `m_types`
468 // lookup.
469 if (!ResolveTypeUID(symbol->getSymIndexId()))
470 continue;
471
472 ++num_added;
473 }
Aaron Smithec40f812018-01-23 20:35:19 +0000474 }
Aaron Smith66b84072018-03-14 04:05:27 +0000475 };
Aaron Smithec40f812018-01-23 20:35:19 +0000476
Aaron Smith66b84072018-03-14 04:05:27 +0000477 if (sc.function) {
478 auto pdb_func =
479 m_session_up->getConcreteSymbolById<PDBSymbolFunc>(sc.function->GetID());
480 if (!pdb_func)
481 return 0;
482 ParseTypesByTagFn(*pdb_func);
483 } else {
484 ParseTypesByTagFn(*compiland);
Aaron Smithec40f812018-01-23 20:35:19 +0000485
Aaron Smith66b84072018-03-14 04:05:27 +0000486 // Also parse global types particularly coming from this compiland.
487 // Unfortunately, PDB has no compiland information for each global type.
488 // We have to parse them all. But ensure we only do this once.
489 static bool parse_all_global_types = false;
490 if (!parse_all_global_types) {
491 ParseTypesByTagFn(*m_global_scope_up);
492 parse_all_global_types = true;
493 }
Aaron Smithec40f812018-01-23 20:35:19 +0000494 }
495 return num_added;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000496}
497
498size_t
499SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
500 // TODO: Implement this
501 return size_t();
502}
503
504lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
505 auto find_result = m_types.find(type_uid);
506 if (find_result != m_types.end())
507 return find_result->second.get();
508
509 TypeSystem *type_system =
510 GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
511 ClangASTContext *clang_type_system =
512 llvm::dyn_cast_or_null<ClangASTContext>(type_system);
513 if (!clang_type_system)
Zachary Turner74e08ca2016-03-02 22:05:52 +0000514 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 PDBASTParser *pdb =
516 llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
517 if (!pdb)
518 return nullptr;
519
520 auto pdb_type = m_session_up->getSymbolById(type_uid);
521 if (pdb_type == nullptr)
522 return nullptr;
523
524 lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
Aaron Smithec40f812018-01-23 20:35:19 +0000525 if (result.get()) {
Aaron Smith86e94342017-12-22 05:26:50 +0000526 m_types.insert(std::make_pair(type_uid, result));
Aaron Smithec40f812018-01-23 20:35:19 +0000527 auto type_list = GetTypeList();
Aaron Smithf76fe682018-03-07 03:16:50 +0000528 if (type_list)
529 type_list->Insert(result);
Aaron Smithec40f812018-01-23 20:35:19 +0000530 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 return result.get();
Zachary Turner74e08ca2016-03-02 22:05:52 +0000532}
533
Kate Stoneb9c1b512016-09-06 20:57:50 +0000534bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
535 // TODO: Implement this
536 return false;
537}
538
539lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
540 return lldb_private::CompilerDecl();
541}
542
543lldb_private::CompilerDeclContext
544SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
545 // PDB always uses the translation unit decl context for everything. We can
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000546 // improve this later but it's not easy because PDB doesn't provide a high
547 // enough level of type fidelity in this area.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548 return *m_tu_decl_ctx_up;
549}
550
551lldb_private::CompilerDeclContext
552SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
553 return *m_tu_decl_ctx_up;
554}
555
556void SymbolFilePDB::ParseDeclsForContext(
557 lldb_private::CompilerDeclContext decl_ctx) {}
558
559uint32_t
560SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
561 uint32_t resolve_scope,
562 lldb_private::SymbolContext &sc) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000563 uint32_t resolved_flags = 0;
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000564 if (resolve_scope & eSymbolContextCompUnit ||
565 resolve_scope & eSymbolContextVariable ||
566 resolve_scope & eSymbolContextFunction ||
567 resolve_scope & eSymbolContextBlock ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000568 resolve_scope & eSymbolContextLineEntry) {
569 addr_t file_vm_addr = so_addr.GetFileAddress();
570 auto symbol_up =
571 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None);
572 if (!symbol_up)
573 return 0;
574
575 auto cu_sp = GetCompileUnitContainsAddress(so_addr);
576 if (!cu_sp) {
577 if (resolved_flags | eSymbolContextVariable) {
578 // TODO: Resolve variables
579 }
580 return 0;
581 }
582 sc.comp_unit = cu_sp.get();
583 resolved_flags |= eSymbolContextCompUnit;
584 lldbassert(sc.module_sp == cu_sp->GetModule());
585
586 switch (symbol_up->getSymTag()) {
587 case PDB_SymType::Function:
588 if (resolve_scope & eSymbolContextFunction) {
589 auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
590 assert(pdb_func);
591 auto func_uid = pdb_func->getSymIndexId();
592 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
593 if (sc.function == nullptr)
594 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
595 if (sc.function) {
596 resolved_flags |= eSymbolContextFunction;
597 if (resolve_scope & eSymbolContextBlock) {
598 Block &block = sc.function->GetBlock(true);
599 sc.block = block.FindBlockByID(sc.function->GetID());
600 if (sc.block)
601 resolved_flags |= eSymbolContextBlock;
602 }
603 }
604 }
605 break;
606 default:
607 break;
608 }
609
610 if (resolve_scope & eSymbolContextLineEntry) {
611 if (auto *line_table = sc.comp_unit->GetLineTable()) {
612 Address addr(so_addr);
613 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
614 resolved_flags |= eSymbolContextLineEntry;
615 }
616 }
617 }
618 return resolved_flags;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000619}
620
Aaron Smith10a02572018-01-13 06:58:18 +0000621std::string SymbolFilePDB::GetSourceFileNameForPDBCompiland(
622 const PDBSymbolCompiland *pdb_compiland) {
623 if (!pdb_compiland)
624 return std::string();
625
626 std::string source_file_name;
627 // `getSourceFileName` returns the basename of the original source file
628 // used to generate this compiland. It does not return the full path.
629 // Currently the only way to get that is to do a basename lookup to get the
630 // IPDBSourceFile, but this is ambiguous in the case of two source files
631 // with the same name contributing to the same compiland. This is an edge
632 // case that we ignore for now, although we need to a long-term solution.
633 std::string file_name = pdb_compiland->getSourceFileName();
634 if (!file_name.empty()) {
635 auto one_src_file_up =
Aaron Smithf76fe682018-03-07 03:16:50 +0000636 m_session_up->findOneSourceFile(pdb_compiland, file_name,
637 PDB_NameSearchFlags::NS_CaseInsensitive);
Aaron Smith10a02572018-01-13 06:58:18 +0000638 if (one_src_file_up)
639 source_file_name = one_src_file_up->getFileName();
640 }
641 // For some reason, source file name could be empty, so we will walk through
642 // all source files of this compiland, and determine the right source file
643 // if any that is used to generate this compiland based on language
644 // indicated in compilanddetails language field.
645 if (!source_file_name.empty())
646 return source_file_name;
647
648 auto details_up = pdb_compiland->findOneChild<PDBSymbolCompilandDetails>();
649 PDB_Lang pdb_lang = details_up ? details_up->getLanguage() : PDB_Lang::Cpp;
650 auto src_files_up =
Aaron Smithf76fe682018-03-07 03:16:50 +0000651 m_session_up->getSourceFilesForCompiland(*pdb_compiland);
Aaron Smith10a02572018-01-13 06:58:18 +0000652 if (src_files_up) {
653 while (auto file_up = src_files_up->getNext()) {
654 FileSpec file_spec(file_up->getFileName(), false,
655 FileSpec::ePathSyntaxWindows);
656 auto file_extension = file_spec.GetFileNameExtension();
657 if (pdb_lang == PDB_Lang::Cpp || pdb_lang == PDB_Lang::C) {
658 static const char* exts[] = { "cpp", "c", "cc", "cxx" };
Aaron Smithdee18b82018-03-09 18:50:19 +0000659 if (llvm::is_contained(exts, file_extension.GetStringRef().lower())) {
Aaron Smith10a02572018-01-13 06:58:18 +0000660 source_file_name = file_up->getFileName();
Aaron Smithdee18b82018-03-09 18:50:19 +0000661 break;
662 }
Aaron Smith10a02572018-01-13 06:58:18 +0000663 } else if (pdb_lang == PDB_Lang::Masm &&
664 ConstString::Compare(file_extension, ConstString("ASM"),
665 false) == 0) {
666 source_file_name = file_up->getFileName();
667 break;
668 }
669 }
670 }
671 return source_file_name;
672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674uint32_t SymbolFilePDB::ResolveSymbolContext(
675 const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
676 uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
Aaron Smith10a02572018-01-13 06:58:18 +0000677 const size_t old_size = sc_list.GetSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000678 if (resolve_scope & lldb::eSymbolContextCompUnit) {
679 // Locate all compilation units with line numbers referencing the specified
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000680 // file. For example, if `file_spec` is <vector>, then this should return
681 // all source files and header files that reference <vector>, either
682 // directly or indirectly.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000683 auto compilands = m_session_up->findCompilandsForSourceFile(
684 file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
685
Aaron Smith10a02572018-01-13 06:58:18 +0000686 if (!compilands)
687 return 0;
688
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000689 // For each one, either find its previously parsed data or parse it afresh
690 // and add it to the symbol context list.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 while (auto compiland = compilands->getNext()) {
692 // If we're not checking inlines, then don't add line information for this
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000693 // file unless the FileSpec matches.
Aaron Smith7ac1c782018-02-09 05:31:28 +0000694 // For inline functions, we don't have to match the FileSpec since they
695 // could be defined in headers other than file specified in FileSpec.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000696 if (!check_inlines) {
697 // `getSourceFileName` returns the basename of the original source file
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000698 // used to generate this compiland. It does not return the full path.
699 // Currently the only way to get that is to do a basename lookup to get
700 // the IPDBSourceFile, but this is ambiguous in the case of two source
701 // files with the same name contributing to the same compiland. This is
702 // a moderately extreme edge case, so we consider this OK for now,
703 // although we need to find a long-term solution.
Aaron Smith10a02572018-01-13 06:58:18 +0000704 std::string source_file =
705 GetSourceFileNameForPDBCompiland(compiland.get());
706 if (source_file.empty())
707 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000708 FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows);
Aaron Smith10a02572018-01-13 06:58:18 +0000709 bool need_full_match = !file_spec.GetDirectory().IsEmpty();
710 if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000711 continue;
712 }
713
714 SymbolContext sc;
Aaron Smith10a02572018-01-13 06:58:18 +0000715 auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
716 if (!cu.get())
717 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000718 sc.comp_unit = cu.get();
719 sc.module_sp = cu->GetModule();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000720
721 // If we were asked to resolve line entries, add all entries to the line
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +0000722 // table that match the requested line (or all lines if `line` == 0).
Aaron Smith7ac1c782018-02-09 05:31:28 +0000723 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
724 eSymbolContextLineEntry)) {
725 bool has_line_table = ParseCompileUnitLineTable(sc, line);
726
727 if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
728 // The query asks for line entries, but we can't get them for the
729 // compile unit. This is not normal for `line` = 0. So just assert it.
Aaron Smithf76fe682018-03-07 03:16:50 +0000730 assert(line && "Couldn't get all line entries!\n");
Aaron Smith7ac1c782018-02-09 05:31:28 +0000731
732 // Current compiland does not have the requested line. Search next.
733 continue;
734 }
735
736 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
737 if (!has_line_table)
738 continue;
739
740 auto *line_table = sc.comp_unit->GetLineTable();
741 lldbassert(line_table);
742
743 uint32_t num_line_entries = line_table->GetSize();
744 // Skip the terminal line entry.
745 --num_line_entries;
746
747 // If `line `!= 0, see if we can resolve function for each line
748 // entry in the line table.
749 for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
750 ++line_idx) {
751 if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
752 continue;
753
754 auto file_vm_addr =
755 sc.line_entry.range.GetBaseAddress().GetFileAddress();
756 if (file_vm_addr == LLDB_INVALID_ADDRESS)
757 continue;
758
759 auto symbol_up =
760 m_session_up->findSymbolByAddress(file_vm_addr,
761 PDB_SymType::Function);
762 if (symbol_up) {
763 auto func_uid = symbol_up->getSymIndexId();
764 sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
765 if (sc.function == nullptr) {
766 auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
767 assert(pdb_func);
768 sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
769 }
770 if (sc.function && (resolve_scope & eSymbolContextBlock)) {
771 Block &block = sc.function->GetBlock(true);
772 sc.block = block.FindBlockByID(sc.function->GetID());
773 }
774 }
775 sc_list.Append(sc);
776 }
777 } else if (has_line_table) {
778 // We can parse line table for the compile unit. But no query to
779 // resolve function or block. We append `sc` to the list anyway.
780 sc_list.Append(sc);
781 }
782 } else {
783 // No query for line entry, function or block. But we have a valid
784 // compile unit, append `sc` to the list.
785 sc_list.Append(sc);
786 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000787 }
788 }
Aaron Smith10a02572018-01-13 06:58:18 +0000789 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000790}
791
792uint32_t SymbolFilePDB::FindGlobalVariables(
793 const lldb_private::ConstString &name,
794 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
795 uint32_t max_matches, lldb_private::VariableList &variables) {
796 return uint32_t();
797}
798
799uint32_t
800SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression &regex,
801 bool append, uint32_t max_matches,
802 lldb_private::VariableList &variables) {
803 return uint32_t();
804}
805
Aaron Smith7ac1c782018-02-09 05:31:28 +0000806bool SymbolFilePDB::ResolveFunction(llvm::pdb::PDBSymbolFunc *pdb_func,
807 bool include_inlines,
808 lldb_private::SymbolContextList &sc_list) {
809 if (!pdb_func)
810 return false;
811 lldb_private::SymbolContext sc;
812 auto file_vm_addr = pdb_func->getVirtualAddress();
813 if (file_vm_addr == LLDB_INVALID_ADDRESS)
814 return false;
815
816 Address so_addr(file_vm_addr);
817 sc.comp_unit = GetCompileUnitContainsAddress(so_addr).get();
818 if (!sc.comp_unit)
819 return false;
820 sc.module_sp = sc.comp_unit->GetModule();
821 auto symbol_up =
822 m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
823 if (!symbol_up)
824 return false;
825
826 auto *func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
827 assert(func);
828 sc.function = ParseCompileUnitFunctionForPDBFunc(func, sc);
829 if (!sc.function)
830 return false;
831
832 sc_list.Append(sc);
833 return true;
834}
835
836bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
837 lldb_private::SymbolContextList &sc_list) {
838 auto pdb_func_up =
839 m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
840 if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
841 return false;
842 return ResolveFunction(pdb_func_up.get(), include_inlines, sc_list);
843}
844
845void SymbolFilePDB::CacheFunctionNames() {
846 if (!m_func_full_names.IsEmpty())
847 return;
848
849 std::map<uint64_t, uint32_t> addr_ids;
850
851 if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
852 while (auto pdb_func_up = results_up->getNext()) {
Aaron Smithf76fe682018-03-07 03:16:50 +0000853 if (pdb_func_up->isCompilerGenerated())
854 continue;
855
Aaron Smith7ac1c782018-02-09 05:31:28 +0000856 auto name = pdb_func_up->getName();
857 auto demangled_name = pdb_func_up->getUndecoratedName();
858 if (name.empty() && demangled_name.empty())
859 continue;
Aaron Smith7ac1c782018-02-09 05:31:28 +0000860
Aaron Smithf76fe682018-03-07 03:16:50 +0000861 auto uid = pdb_func_up->getSymIndexId();
Aaron Smith7ac1c782018-02-09 05:31:28 +0000862 if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
863 addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
864
865 if (auto parent = pdb_func_up->getClassParent()) {
866
867 // PDB have symbols for class/struct methods or static methods in Enum
868 // Class. We won't bother to check if the parent is UDT or Enum here.
869 m_func_method_names.Append(ConstString(name), uid);
870
871 ConstString cstr_name(name);
872
873 // To search a method name, like NS::Class:MemberFunc, LLDB searches its
874 // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not
875 // have inforamtion of this, we extract base names and cache them by our
876 // own effort.
877 llvm::StringRef basename;
878 CPlusPlusLanguage::MethodName cpp_method(cstr_name);
879 if (cpp_method.IsValid()) {
880 llvm::StringRef context;
881 basename = cpp_method.GetBasename();
882 if (basename.empty())
883 CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
884 context, basename);
885 }
886
887 if (!basename.empty())
888 m_func_base_names.Append(ConstString(basename), uid);
889 else {
890 m_func_base_names.Append(ConstString(name), uid);
891 }
892
893 if (!demangled_name.empty())
894 m_func_full_names.Append(ConstString(demangled_name), uid);
895
896 } else {
897 // Handle not-method symbols.
898
899 // The function name might contain namespace, or its lexical scope. It
900 // is not safe to get its base name by applying same scheme as we deal
901 // with the method names.
902 // FIXME: Remove namespace if function is static in a scope.
903 m_func_base_names.Append(ConstString(name), uid);
904
905 if (name == "main") {
906 m_func_full_names.Append(ConstString(name), uid);
907
908 if (!demangled_name.empty() && name != demangled_name) {
909 m_func_full_names.Append(ConstString(demangled_name), uid);
910 m_func_base_names.Append(ConstString(demangled_name), uid);
911 }
912 } else if (!demangled_name.empty()) {
913 m_func_full_names.Append(ConstString(demangled_name), uid);
914 } else {
915 m_func_full_names.Append(ConstString(name), uid);
916 }
917 }
918 }
919 }
920
921 if (auto results_up =
922 m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
923 while (auto pub_sym_up = results_up->getNext()) {
924 if (!pub_sym_up->isFunction())
925 continue;
926 auto name = pub_sym_up->getName();
927 if (name.empty())
928 continue;
929
930 if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000931 auto vm_addr = pub_sym_up->getVirtualAddress();
932
933 // PDB public symbol has mangled name for its associated function.
934 if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
935 // Cache mangled name.
936 m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
937 }
938 }
939 }
940 }
941 // Sort them before value searching is working properly
942 m_func_full_names.Sort();
943 m_func_full_names.SizeToFit();
944 m_func_method_names.Sort();
945 m_func_method_names.SizeToFit();
946 m_func_base_names.Sort();
947 m_func_base_names.SizeToFit();
948}
949
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950uint32_t SymbolFilePDB::FindFunctions(
951 const lldb_private::ConstString &name,
952 const lldb_private::CompilerDeclContext *parent_decl_ctx,
953 uint32_t name_type_mask, bool include_inlines, bool append,
954 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +0000955 if (!append)
956 sc_list.Clear();
957 lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
958
959 if (name_type_mask == eFunctionNameTypeNone)
960 return 0;
961 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
962 return 0;
963 if (name.IsEmpty())
964 return 0;
965
966 auto old_size = sc_list.GetSize();
Pavel Labath4d4d63e2018-02-09 11:37:01 +0000967 if (name_type_mask & eFunctionNameTypeFull ||
968 name_type_mask & eFunctionNameTypeBase ||
Aaron Smith7ac1c782018-02-09 05:31:28 +0000969 name_type_mask & eFunctionNameTypeMethod) {
970 CacheFunctionNames();
971
972 std::set<uint32_t> resolved_ids;
973 auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, this] (
974 UniqueCStringMap<uint32_t> &Names)
975 {
976 std::vector<uint32_t> ids;
977 if (Names.GetValues(name, ids)) {
978 for (auto id : ids) {
979 if (resolved_ids.find(id) == resolved_ids.end()) {
980 if (ResolveFunction(id, include_inlines, sc_list))
981 resolved_ids.insert(id);
982 }
983 }
984 }
985 };
986 if (name_type_mask & eFunctionNameTypeFull) {
987 ResolveFn(m_func_full_names);
988 }
989 if (name_type_mask & eFunctionNameTypeBase) {
990 ResolveFn(m_func_base_names);
991 }
992 if (name_type_mask & eFunctionNameTypeMethod) {
993 ResolveFn(m_func_method_names);
994 }
995 }
996 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000997}
998
999uint32_t
1000SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression &regex,
1001 bool include_inlines, bool append,
1002 lldb_private::SymbolContextList &sc_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001003 if (!append)
1004 sc_list.Clear();
1005 if (!regex.IsValid())
1006 return 0;
1007
1008 auto old_size = sc_list.GetSize();
1009 CacheFunctionNames();
1010
1011 std::set<uint32_t> resolved_ids;
1012 auto ResolveFn = [&regex, include_inlines, &sc_list, &resolved_ids, this] (
1013 UniqueCStringMap<uint32_t> &Names)
1014 {
1015 std::vector<uint32_t> ids;
1016 if (Names.GetValues(regex, ids)) {
1017 for (auto id : ids) {
1018 if (resolved_ids.find(id) == resolved_ids.end())
1019 if (ResolveFunction(id, include_inlines, sc_list))
1020 resolved_ids.insert(id);
1021 }
1022 }
1023 };
1024 ResolveFn(m_func_full_names);
1025 ResolveFn(m_func_base_names);
1026
1027 return sc_list.GetSize() - old_size;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001028}
1029
1030void SymbolFilePDB::GetMangledNamesForFunction(
1031 const std::string &scope_qualified_name,
1032 std::vector<lldb_private::ConstString> &mangled_names) {}
1033
1034uint32_t SymbolFilePDB::FindTypes(
1035 const lldb_private::SymbolContext &sc,
1036 const lldb_private::ConstString &name,
1037 const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
1038 uint32_t max_matches,
1039 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
1040 lldb_private::TypeMap &types) {
1041 if (!append)
1042 types.Clear();
1043 if (!name)
1044 return 0;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001045 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
1046 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001047
1048 searched_symbol_files.clear();
1049 searched_symbol_files.insert(this);
1050
1051 std::string name_str = name.AsCString();
1052
Aaron Smith86e94342017-12-22 05:26:50 +00001053 // There is an assumption 'name' is not a regex
1054 FindTypesByName(name_str, max_matches, types);
1055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 return types.GetSize();
1057}
1058
Aaron Smith86e94342017-12-22 05:26:50 +00001059void
1060SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression &regex,
1061 uint32_t max_matches,
1062 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001063 // When searching by regex, we need to go out of our way to limit the search
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001064 // space as much as possible since this searches EVERYTHING in the PDB,
1065 // manually doing regex comparisons. PDB library isn't optimized for regex
1066 // searches or searches across multiple symbol types at the same time, so the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001067 // best we can do is to search enums, then typedefs, then classes one by one,
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001068 // and do a regex comparison against each of them.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001069 PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
1070 PDB_SymType::UDT};
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 std::unique_ptr<IPDBEnumSymbols> results;
1072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 uint32_t matches = 0;
1074
1075 for (auto tag : tags_to_search) {
Aaron Smith10a02572018-01-13 06:58:18 +00001076 results = m_global_scope_up->findAllChildren(tag);
1077 if (!results)
1078 continue;
1079
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 while (auto result = results->getNext()) {
1081 if (max_matches > 0 && matches >= max_matches)
1082 break;
1083
1084 std::string type_name;
1085 if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
1086 type_name = enum_type->getName();
1087 else if (auto typedef_type =
Aaron Smith7ac1c782018-02-09 05:31:28 +00001088 llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001089 type_name = typedef_type->getName();
1090 else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
1091 type_name = class_type->getName();
1092 else {
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001093 // We're looking only for types that have names. Skip symbols, as well
1094 // as unnamed types such as arrays, pointers, etc.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001095 continue;
1096 }
1097
Aaron Smith86e94342017-12-22 05:26:50 +00001098 if (!regex.Execute(type_name))
Kate Stoneb9c1b512016-09-06 20:57:50 +00001099 continue;
1100
1101 // This should cause the type to get cached and stored in the `m_types`
1102 // lookup.
1103 if (!ResolveTypeUID(result->getSymIndexId()))
1104 continue;
1105
1106 auto iter = m_types.find(result->getSymIndexId());
1107 if (iter == m_types.end())
1108 continue;
1109 types.Insert(iter->second);
1110 ++matches;
1111 }
1112 }
1113}
1114
1115void SymbolFilePDB::FindTypesByName(const std::string &name,
1116 uint32_t max_matches,
1117 lldb_private::TypeMap &types) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001118 std::unique_ptr<IPDBEnumSymbols> results;
Aaron Smithf76fe682018-03-07 03:16:50 +00001119 if (name.empty())
1120 return;
Aaron Smith10a02572018-01-13 06:58:18 +00001121 results = m_global_scope_up->findChildren(PDB_SymType::None, name,
1122 PDB_NameSearchFlags::NS_Default);
1123 if (!results)
1124 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001125
1126 uint32_t matches = 0;
1127
1128 while (auto result = results->getNext()) {
1129 if (max_matches > 0 && matches >= max_matches)
1130 break;
1131 switch (result->getSymTag()) {
1132 case PDB_SymType::Enum:
1133 case PDB_SymType::UDT:
1134 case PDB_SymType::Typedef:
1135 break;
1136 default:
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001137 // We're looking only for types that have names. Skip symbols, as well as
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138 // unnamed types such as arrays, pointers, etc.
1139 continue;
1140 }
1141
1142 // This should cause the type to get cached and stored in the `m_types`
1143 // lookup.
1144 if (!ResolveTypeUID(result->getSymIndexId()))
1145 continue;
1146
1147 auto iter = m_types.find(result->getSymIndexId());
1148 if (iter == m_types.end())
1149 continue;
1150 types.Insert(iter->second);
1151 ++matches;
1152 }
1153}
1154
1155size_t SymbolFilePDB::FindTypes(
1156 const std::vector<lldb_private::CompilerContext> &contexts, bool append,
1157 lldb_private::TypeMap &types) {
1158 return 0;
1159}
1160
Aaron Smithec40f812018-01-23 20:35:19 +00001161lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
1162 return m_obj_file->GetModule()->GetTypeList();
1163}
Kate Stoneb9c1b512016-09-06 20:57:50 +00001164
Aaron Smith7ac1c782018-02-09 05:31:28 +00001165void
1166SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol *pdb_symbol,
1167 uint32_t type_mask,
1168 TypeCollection &type_collection) {
1169 if (!pdb_symbol)
1170 return;
1171
1172 bool can_parse = false;
1173 switch (pdb_symbol->getSymTag()) {
1174 case PDB_SymType::ArrayType:
1175 can_parse = ((type_mask & eTypeClassArray) != 0);
1176 break;
1177 case PDB_SymType::BuiltinType:
1178 can_parse = ((type_mask & eTypeClassBuiltin) != 0);
1179 break;
1180 case PDB_SymType::Enum:
1181 can_parse = ((type_mask & eTypeClassEnumeration) != 0);
1182 break;
1183 case PDB_SymType::Function:
1184 case PDB_SymType::FunctionSig:
1185 can_parse = ((type_mask & eTypeClassFunction) != 0);
1186 break;
1187 case PDB_SymType::PointerType:
1188 can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
1189 eTypeClassMemberPointer)) != 0);
1190 break;
1191 case PDB_SymType::Typedef:
1192 can_parse = ((type_mask & eTypeClassTypedef) != 0);
1193 break;
1194 case PDB_SymType::UDT: {
1195 auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(pdb_symbol);
1196 assert(udt);
1197 can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
1198 ((type_mask & (eTypeClassClass | eTypeClassStruct |
1199 eTypeClassUnion)) != 0));
1200 } break;
1201 default:break;
1202 }
1203
1204 if (can_parse) {
1205 if (auto *type = ResolveTypeUID(pdb_symbol->getSymIndexId())) {
1206 auto result =
1207 std::find(type_collection.begin(), type_collection.end(), type);
1208 if (result == type_collection.end())
1209 type_collection.push_back(type);
1210 }
1211 }
1212
1213 auto results_up = pdb_symbol->findAllChildren();
1214 while (auto symbol_up = results_up->getNext())
1215 GetTypesForPDBSymbol(symbol_up.get(), type_mask, type_collection);
1216}
1217
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
1219 uint32_t type_mask,
1220 lldb_private::TypeList &type_list) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001221 TypeCollection type_collection;
1222 uint32_t old_size = type_list.GetSize();
1223 CompileUnit *cu = sc_scope ?
1224 sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
1225 if (cu) {
1226 auto compiland_up = GetPDBCompilandByUID(cu->GetID());
1227 GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1228 } else {
1229 for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
1230 auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
1231 if (cu_sp.get()) {
1232 auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID());
1233 GetTypesForPDBSymbol(compiland_up.get(), type_mask, type_collection);
1234 }
1235 }
1236 }
1237
1238 for (auto type : type_collection) {
1239 type->GetForwardCompilerType();
1240 type_list.Insert(type->shared_from_this());
1241 }
1242 return type_list.GetSize() - old_size;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001243}
1244
1245lldb_private::TypeSystem *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001246SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1247 auto type_system =
1248 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1249 if (type_system)
1250 type_system->SetSymbolFile(this);
1251 return type_system;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001252}
1253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
1255 const lldb_private::SymbolContext &sc,
1256 const lldb_private::ConstString &name,
1257 const lldb_private::CompilerDeclContext *parent_decl_ctx) {
1258 return lldb_private::CompilerDeclContext();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001259}
1260
Kate Stoneb9c1b512016-09-06 20:57:50 +00001261lldb_private::ConstString SymbolFilePDB::GetPluginName() {
1262 static ConstString g_name("pdb");
1263 return g_name;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001264}
1265
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
1267
1268IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
1269
1270const IPDBSession &SymbolFilePDB::GetPDBSession() const {
1271 return *m_session_up;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001272}
1273
Aaron Smith10a02572018-01-13 06:58:18 +00001274lldb::CompUnitSP
1275SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001276 auto found_cu = m_comp_units.find(id);
1277 if (found_cu != m_comp_units.end())
1278 return found_cu->second;
1279
Aaron Smith10a02572018-01-13 06:58:18 +00001280 auto compiland_up = GetPDBCompilandByUID(id);
1281 if (!compiland_up)
1282 return CompUnitSP();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001283
1284 lldb::LanguageType lang;
Aaron Smith10a02572018-01-13 06:58:18 +00001285 auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 if (!details)
1287 lang = lldb::eLanguageTypeC_plus_plus;
1288 else
1289 lang = TranslateLanguage(details->getLanguage());
1290
Aaron Smithf76fe682018-03-07 03:16:50 +00001291 if (lang == lldb::LanguageType::eLanguageTypeUnknown)
1292 return CompUnitSP();
1293
1294 std::string path = GetSourceFileNameForPDBCompiland(compiland_up.get());
1295 if (path.empty())
1296 return CompUnitSP();
1297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 // Don't support optimized code for now, DebugInfoPDB does not return this
1299 // information.
1300 LazyBool optimized = eLazyBoolNo;
Aaron Smith10a02572018-01-13 06:58:18 +00001301 auto cu_sp = std::make_shared<CompileUnit>(
Kate Stoneb9c1b512016-09-06 20:57:50 +00001302 m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized);
Aaron Smith10a02572018-01-13 06:58:18 +00001303
1304 if (!cu_sp)
1305 return CompUnitSP();
1306
1307 m_comp_units.insert(std::make_pair(id, cu_sp));
1308 if (index == UINT32_MAX)
1309 GetCompileUnitIndex(compiland_up.get(), index);
1310 lldbassert(index != UINT32_MAX);
1311 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
1312 index, cu_sp);
1313 return cu_sp;
Zachary Turner42dff792016-04-15 00:21:26 +00001314}
1315
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316bool SymbolFilePDB::ParseCompileUnitLineTable(
1317 const lldb_private::SymbolContext &sc, uint32_t match_line) {
Aaron Smith10a02572018-01-13 06:58:18 +00001318 lldbassert(sc.comp_unit);
1319
1320 auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
1321 if (!compiland_up)
1322 return false;
Zachary Turner42dff792016-04-15 00:21:26 +00001323
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324 // LineEntry needs the *index* of the file into the list of support files
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001325 // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
1326 // a globally unique idenfitifier in the namespace of the PDB. So, we have to
1327 // do a mapping so that we can hand out indices.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001328 llvm::DenseMap<uint32_t, uint32_t> index_map;
Aaron Smith10a02572018-01-13 06:58:18 +00001329 BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001331
Aaron Smith10a02572018-01-13 06:58:18 +00001332 // Find contributions to `compiland` from all source and header files.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 std::string path = sc.comp_unit->GetPath();
Aaron Smith10a02572018-01-13 06:58:18 +00001334 auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
1335 if (!files)
1336 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 // For each source and header file, create a LineSequence for contributions to
Aaron Smith10a02572018-01-13 06:58:18 +00001339 // the compiland from that file, and add the sequence.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001340 while (auto file = files->getNext()) {
1341 std::unique_ptr<LineSequence> sequence(
1342 line_table->CreateLineSequenceContainer());
Aaron Smith10a02572018-01-13 06:58:18 +00001343 auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
1344 if (!lines)
1345 continue;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001346 int entry_count = lines->getChildCount();
Zachary Turner74e08ca2016-03-02 22:05:52 +00001347
Kate Stoneb9c1b512016-09-06 20:57:50 +00001348 uint64_t prev_addr;
1349 uint32_t prev_length;
1350 uint32_t prev_line;
1351 uint32_t prev_source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001352
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353 for (int i = 0; i < entry_count; ++i) {
1354 auto line = lines->getChildAtIndex(i);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001355
Kate Stoneb9c1b512016-09-06 20:57:50 +00001356 uint64_t lno = line->getLineNumber();
1357 uint64_t addr = line->getVirtualAddress();
1358 uint32_t length = line->getLength();
1359 uint32_t source_id = line->getSourceFileId();
1360 uint32_t col = line->getColumnNumber();
1361 uint32_t source_idx = index_map[source_id];
Zachary Turner74e08ca2016-03-02 22:05:52 +00001362
Kate Stoneb9c1b512016-09-06 20:57:50 +00001363 // There was a gap between the current entry and the previous entry if the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001364 // addresses don't perfectly line up.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001365 bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
Zachary Turner74e08ca2016-03-02 22:05:52 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 // Before inserting the current entry, insert a terminal entry at the end
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001368 // of the previous entry's address range if the current entry resulted in
1369 // a gap from the previous entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001370 if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
1371 line_table->AppendLineEntryToSequence(
1372 sequence.get(), prev_addr + prev_length, prev_line, 0,
1373 prev_source_idx, false, false, false, false, true);
1374 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001375
Kate Stoneb9c1b512016-09-06 20:57:50 +00001376 if (ShouldAddLine(match_line, lno, length)) {
1377 bool is_statement = line->isStatement();
1378 bool is_prologue = false;
1379 bool is_epilogue = false;
1380 auto func =
1381 m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
1382 if (func) {
1383 auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
Aaron Smith10a02572018-01-13 06:58:18 +00001384 if (prologue)
1385 is_prologue = (addr == prologue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001386
Kate Stoneb9c1b512016-09-06 20:57:50 +00001387 auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
Aaron Smith10a02572018-01-13 06:58:18 +00001388 if (epilogue)
1389 is_epilogue = (addr == epilogue->getVirtualAddress());
Zachary Turner74e08ca2016-03-02 22:05:52 +00001390 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392 line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
1393 source_idx, is_statement, false,
1394 is_prologue, is_epilogue, false);
1395 }
Zachary Turner7e8c7be2016-03-10 00:06:26 +00001396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397 prev_addr = addr;
1398 prev_length = length;
1399 prev_line = lno;
1400 prev_source_idx = source_idx;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001401 }
1402
Kate Stoneb9c1b512016-09-06 20:57:50 +00001403 if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
1404 // The end is always a terminal entry, so insert it regardless.
1405 line_table->AppendLineEntryToSequence(
1406 sequence.get(), prev_addr + prev_length, prev_line, 0,
1407 prev_source_idx, false, false, false, false, true);
1408 }
1409
1410 line_table->InsertSequence(sequence.release());
1411 }
1412
Aaron Smith10a02572018-01-13 06:58:18 +00001413 if (line_table->GetSize()) {
1414 sc.comp_unit->SetLineTable(line_table.release());
1415 return true;
1416 }
1417 return false;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001418}
1419
Kate Stoneb9c1b512016-09-06 20:57:50 +00001420void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
Aaron Smith10a02572018-01-13 06:58:18 +00001421 const PDBSymbolCompiland &compiland,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001422 llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
1423 // This is a hack, but we need to convert the source id into an index into the
Adrian McCarthy9d0eb9962017-01-27 21:42:28 +00001424 // support files array. We don't want to do path comparisons to avoid
1425 // basename / full path issues that may or may not even be a problem, so we
1426 // use the globally unique source file identifiers. Ideally we could use the
1427 // global identifiers everywhere, but LineEntry currently assumes indices.
Aaron Smith10a02572018-01-13 06:58:18 +00001428 auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
1429 if (!source_files)
1430 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 int index = 0;
Zachary Turner74e08ca2016-03-02 22:05:52 +00001432
Kate Stoneb9c1b512016-09-06 20:57:50 +00001433 while (auto file = source_files->getNext()) {
1434 uint32_t source_id = file->getUniqueId();
1435 index_map[source_id] = index++;
1436 }
Zachary Turner74e08ca2016-03-02 22:05:52 +00001437}
Aaron Smith7ac1c782018-02-09 05:31:28 +00001438
1439lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
1440 const lldb_private::Address &so_addr) {
1441 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1442 if (file_vm_addr == LLDB_INVALID_ADDRESS)
1443 return nullptr;
1444
1445 auto lines_up =
1446 m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200);
1447 if (!lines_up)
1448 return nullptr;
1449
1450 auto first_line_up = lines_up->getNext();
1451 if (!first_line_up)
1452 return nullptr;
1453 auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId());
1454 if (compiland_up) {
1455 return ParseCompileUnitForUID(compiland_up->getSymIndexId());
1456 }
1457
1458 return nullptr;
1459}
1460
1461Mangled
1462SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc *pdb_func) {
1463 Mangled mangled;
1464 if (!pdb_func)
1465 return mangled;
1466
1467 auto func_name = pdb_func->getName();
1468 auto func_undecorated_name = pdb_func->getUndecoratedName();
1469 std::string func_decorated_name;
1470
1471 // Seek from public symbols for non-static function's decorated name if any.
1472 // For static functions, they don't have undecorated names and aren't exposed
1473 // in Public Symbols either.
1474 if (!func_undecorated_name.empty()) {
1475 auto result_up =
1476 m_global_scope_up->findChildren(PDB_SymType::PublicSymbol,
1477 func_undecorated_name,
1478 PDB_NameSearchFlags::NS_UndecoratedName);
1479 if (result_up) {
1480 while (auto symbol_up = result_up->getNext()) {
1481 // For a public symbol, it is unique.
1482 lldbassert(result_up->getChildCount() == 1);
1483 if (auto *pdb_public_sym =
Aaron Smithf76fe682018-03-07 03:16:50 +00001484 llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(symbol_up.get())) {
Aaron Smith7ac1c782018-02-09 05:31:28 +00001485 if (pdb_public_sym->isFunction()) {
1486 func_decorated_name = pdb_public_sym->getName();
Aaron Smithf76fe682018-03-07 03:16:50 +00001487 break;
Aaron Smith7ac1c782018-02-09 05:31:28 +00001488 }
1489 }
1490 }
1491 }
1492 }
1493 if (!func_decorated_name.empty()) {
1494 mangled.SetMangledName(ConstString(func_decorated_name));
1495
1496 // For MSVC, format of C funciton's decorated name depends on calling
1497 // conventon. Unfortunately none of the format is recognized by current
1498 // LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
1499 // `__purecall` is retrieved as both its decorated and
1500 // undecorated name (using PDBSymbolFunc::getUndecoratedName method).
1501 // However `__purecall` string is not treated as mangled in LLDB
1502 // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method
1503 // will fail internally and caches an empty string as its undecorated
1504 // name. So we will face a contradition here for the same symbol:
1505 // non-empty undecorated name from PDB
1506 // empty undecorated name from LLDB
1507 if (!func_undecorated_name.empty() &&
1508 mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
1509 mangled.SetDemangledName(ConstString(func_undecorated_name));
1510
1511 // LLDB uses several flags to control how a C++ decorated name is
1512 // undecorated for MSVC. See `safeUndecorateName` in Class Mangled.
1513 // So the yielded name could be different from what we retrieve from
1514 // PDB source unless we also apply same flags in getting undecorated
1515 // name through PDBSymbolFunc::getUndecoratedNameEx method.
1516 if (!func_undecorated_name.empty() &&
1517 mangled.GetDemangledName(mangled.GuessLanguage()) !=
1518 ConstString(func_undecorated_name))
1519 mangled.SetDemangledName(ConstString(func_undecorated_name));
1520 } else if (!func_undecorated_name.empty()) {
1521 mangled.SetDemangledName(ConstString(func_undecorated_name));
1522 } else if (!func_name.empty())
1523 mangled.SetValue(ConstString(func_name), false);
1524
1525 return mangled;
1526}
1527
1528bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
1529 const lldb_private::CompilerDeclContext *decl_ctx) {
1530 if (decl_ctx == nullptr || !decl_ctx->IsValid())
1531 return true;
1532
1533 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
1534 if (!decl_ctx_type_system)
1535 return false;
1536 TypeSystem *type_system = GetTypeSystemForLanguage(
1537 decl_ctx_type_system->GetMinimumLanguage(nullptr));
1538 if (decl_ctx_type_system == type_system)
1539 return true; // The type systems match, return true
1540
1541 return false;
1542}