blob: e27d4699ae2fa42cf944a4ec067cdb2c75529e78 [file] [log] [blame]
Zachary Turner307f5ae2018-10-12 19:47:13 +00001//===-- SymbolFileNativePDB.cpp ---------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Zachary Turner307f5ae2018-10-12 19:47:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "SymbolFileNativePDB.h"
10
Zachary Turner2f7efbc2018-10-23 16:37:53 +000011#include "clang/AST/Attr.h"
12#include "clang/AST/CharUnits.h"
13#include "clang/AST/Decl.h"
14#include "clang/AST/DeclCXX.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000015#include "clang/AST/Type.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000016
Zachary Turner44f19512019-01-10 20:57:32 +000017#include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/PluginManager.h"
Zachary Turner9f727952018-10-26 09:06:38 +000020#include "lldb/Core/StreamBuffer.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000021#include "lldb/Core/StreamFile.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000022#include "lldb/Symbol/ClangASTContext.h"
23#include "lldb/Symbol/ClangASTImporter.h"
24#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000025#include "lldb/Symbol/ClangUtil.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000026#include "lldb/Symbol/CompileUnit.h"
27#include "lldb/Symbol/LineTable.h"
28#include "lldb/Symbol/ObjectFile.h"
29#include "lldb/Symbol/SymbolContext.h"
30#include "lldb/Symbol/SymbolVendor.h"
Zachary Turner9f727952018-10-26 09:06:38 +000031#include "lldb/Symbol/Variable.h"
32#include "lldb/Symbol/VariableList.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000033
34#include "llvm/DebugInfo/CodeView/CVRecord.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000035#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000036#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000037#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000038#include "llvm/DebugInfo/CodeView/RecordName.h"
39#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
Zachary Turnera42bbe32018-12-07 19:34:02 +000040#include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000041#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000042#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
43#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
44#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
45#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
46#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
47#include "llvm/DebugInfo/PDB/Native/SymbolStream.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000048#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000049#include "llvm/DebugInfo/PDB/PDBTypes.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000050#include "llvm/Demangle/MicrosoftDemangle.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000051#include "llvm/Object/COFF.h"
52#include "llvm/Support/Allocator.h"
53#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner056e4ab2018-11-08 18:50:11 +000054#include "llvm/Support/Error.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000055#include "llvm/Support/ErrorOr.h"
56#include "llvm/Support/MemoryBuffer.h"
57
Zachary Turnera93458b2018-12-06 17:49:15 +000058#include "DWARFLocationExpression.h"
Zachary Turner594c85e2018-12-17 19:43:33 +000059#include "PdbAstBuilder.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000060#include "PdbSymUid.h"
61#include "PdbUtil.h"
Zachary Turner2f7efbc2018-10-23 16:37:53 +000062#include "UdtRecordCompleter.h"
Zachary Turner307f5ae2018-10-12 19:47:13 +000063
64using namespace lldb;
65using namespace lldb_private;
Zachary Turner2f7efbc2018-10-23 16:37:53 +000066using namespace npdb;
Zachary Turner307f5ae2018-10-12 19:47:13 +000067using namespace llvm::codeview;
68using namespace llvm::pdb;
69
70static lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
71 switch (lang) {
72 case PDB_Lang::Cpp:
73 return lldb::LanguageType::eLanguageTypeC_plus_plus;
74 case PDB_Lang::C:
75 return lldb::LanguageType::eLanguageTypeC;
Nathan Lanza0561be62019-03-11 23:30:58 +000076 case PDB_Lang::Swift:
77 return lldb::LanguageType::eLanguageTypeSwift;
Zachary Turner307f5ae2018-10-12 19:47:13 +000078 default:
79 return lldb::LanguageType::eLanguageTypeUnknown;
80 }
81}
82
83static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath,
84 llvm::BumpPtrAllocator &Allocator) {
85 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
86 llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1,
87 /*RequiresNullTerminator=*/false);
88 if (!ErrorOrBuffer)
89 return nullptr;
90 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
91
92 llvm::StringRef Path = Buffer->getBufferIdentifier();
93 auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>(
94 std::move(Buffer), llvm::support::little);
95
96 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +000097 if (auto EC = File->parseFileHeaders()) {
98 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +000099 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +0000100 }
101 if (auto EC = File->parseStreamData()) {
102 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +0000103 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +0000104 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000105
106 return File;
107}
108
109static std::unique_ptr<PDBFile>
110loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) {
111 // Try to find a matching PDB for an EXE.
112 using namespace llvm::object;
113 auto expected_binary = createBinary(exe_path);
114
115 // If the file isn't a PE/COFF executable, fail.
116 if (!expected_binary) {
117 llvm::consumeError(expected_binary.takeError());
118 return nullptr;
119 }
120 OwningBinary<Binary> binary = std::move(*expected_binary);
121
122 auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary());
123 if (!obj)
124 return nullptr;
125 const llvm::codeview::DebugInfo *pdb_info = nullptr;
126
127 // If it doesn't have a debug directory, fail.
128 llvm::StringRef pdb_file;
129 auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file);
130 if (ec)
131 return nullptr;
132
133 // if the file doesn't exist, is not a pdb, or doesn't have a matching guid,
134 // fail.
135 llvm::file_magic magic;
136 ec = llvm::identify_magic(pdb_file, magic);
137 if (ec || magic != llvm::file_magic::pdb)
138 return nullptr;
139 std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +0000140 if (!pdb)
141 return nullptr;
142
Zachary Turner307f5ae2018-10-12 19:47:13 +0000143 auto expected_info = pdb->getPDBInfoStream();
144 if (!expected_info) {
145 llvm::consumeError(expected_info.takeError());
146 return nullptr;
147 }
148 llvm::codeview::GUID guid;
149 memcpy(&guid, pdb_info->PDB70.Signature, 16);
150
151 if (expected_info->getGuid() != guid)
152 return nullptr;
153 return pdb;
154}
155
156static bool IsFunctionPrologue(const CompilandIndexItem &cci,
157 lldb::addr_t addr) {
158 // FIXME: Implement this.
159 return false;
160}
161
162static bool IsFunctionEpilogue(const CompilandIndexItem &cci,
163 lldb::addr_t addr) {
164 // FIXME: Implement this.
165 return false;
166}
167
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000168static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) {
169 switch (kind) {
170 case SimpleTypeKind::Boolean128:
171 case SimpleTypeKind::Boolean16:
172 case SimpleTypeKind::Boolean32:
173 case SimpleTypeKind::Boolean64:
174 case SimpleTypeKind::Boolean8:
175 return "bool";
176 case SimpleTypeKind::Byte:
177 case SimpleTypeKind::UnsignedCharacter:
178 return "unsigned char";
179 case SimpleTypeKind::NarrowCharacter:
180 return "char";
181 case SimpleTypeKind::SignedCharacter:
182 case SimpleTypeKind::SByte:
Zachary Turner71ebb722018-10-23 22:15:05 +0000183 return "signed char";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000184 case SimpleTypeKind::Character16:
185 return "char16_t";
186 case SimpleTypeKind::Character32:
187 return "char32_t";
188 case SimpleTypeKind::Complex80:
189 case SimpleTypeKind::Complex64:
190 case SimpleTypeKind::Complex32:
191 return "complex";
192 case SimpleTypeKind::Float128:
193 case SimpleTypeKind::Float80:
194 return "long double";
195 case SimpleTypeKind::Float64:
196 return "double";
197 case SimpleTypeKind::Float32:
198 return "float";
199 case SimpleTypeKind::Float16:
200 return "single";
201 case SimpleTypeKind::Int128:
202 return "__int128";
203 case SimpleTypeKind::Int64:
204 case SimpleTypeKind::Int64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000205 return "int64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000206 case SimpleTypeKind::Int32:
207 return "int";
208 case SimpleTypeKind::Int16:
209 return "short";
210 case SimpleTypeKind::UInt128:
211 return "unsigned __int128";
212 case SimpleTypeKind::UInt64:
213 case SimpleTypeKind::UInt64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000214 return "uint64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000215 case SimpleTypeKind::HResult:
216 return "HRESULT";
217 case SimpleTypeKind::UInt32:
218 return "unsigned";
219 case SimpleTypeKind::UInt16:
220 case SimpleTypeKind::UInt16Short:
221 return "unsigned short";
222 case SimpleTypeKind::Int32Long:
223 return "long";
224 case SimpleTypeKind::UInt32Long:
225 return "unsigned long";
226 case SimpleTypeKind::Void:
227 return "void";
228 case SimpleTypeKind::WideCharacter:
229 return "wchar_t";
230 default:
231 return "";
232 }
233}
234
235static bool IsClassRecord(TypeLeafKind kind) {
236 switch (kind) {
237 case LF_STRUCTURE:
238 case LF_CLASS:
239 case LF_INTERFACE:
240 return true;
241 default:
242 return false;
243 }
244}
245
Zachary Turner307f5ae2018-10-12 19:47:13 +0000246void SymbolFileNativePDB::Initialize() {
247 PluginManager::RegisterPlugin(GetPluginNameStatic(),
248 GetPluginDescriptionStatic(), CreateInstance,
249 DebuggerInitialize);
250}
251
252void SymbolFileNativePDB::Terminate() {
253 PluginManager::UnregisterPlugin(CreateInstance);
254}
255
Zachary Turnerb96181c2018-10-22 16:19:07 +0000256void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {}
Zachary Turner307f5ae2018-10-12 19:47:13 +0000257
Zachary Turnerb96181c2018-10-22 16:19:07 +0000258ConstString SymbolFileNativePDB::GetPluginNameStatic() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000259 static ConstString g_name("native-pdb");
260 return g_name;
261}
262
263const char *SymbolFileNativePDB::GetPluginDescriptionStatic() {
264 return "Microsoft PDB debug symbol cross-platform file reader.";
265}
266
Zachary Turnerb96181c2018-10-22 16:19:07 +0000267SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000268 return new SymbolFileNativePDB(obj_file);
269}
270
Zachary Turnerb96181c2018-10-22 16:19:07 +0000271SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000272 : SymbolFile(object_file) {}
273
274SymbolFileNativePDB::~SymbolFileNativePDB() {}
275
276uint32_t SymbolFileNativePDB::CalculateAbilities() {
277 uint32_t abilities = 0;
278 if (!m_obj_file)
279 return 0;
280
281 if (!m_index) {
282 // Lazily load and match the PDB file, but only do this once.
283 std::unique_ptr<PDBFile> file_up =
284 loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator);
285
286 if (!file_up) {
287 auto module_sp = m_obj_file->GetModule();
288 if (!module_sp)
289 return 0;
290 // See if any symbol file is specified through `--symfile` option.
291 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
292 if (!symfile)
293 return 0;
294 file_up = loadPDBFile(symfile.GetPath(), m_allocator);
295 }
296
297 if (!file_up)
298 return 0;
299
300 auto expected_index = PdbIndex::create(std::move(file_up));
301 if (!expected_index) {
302 llvm::consumeError(expected_index.takeError());
303 return 0;
304 }
305 m_index = std::move(*expected_index);
306 }
307 if (!m_index)
308 return 0;
309
310 // We don't especially have to be precise here. We only distinguish between
311 // stripped and not stripped.
312 abilities = kAllAbilities;
313
314 if (m_index->dbi().isStripped())
315 abilities &= ~(Blocks | LocalVariables);
316 return abilities;
317}
318
319void SymbolFileNativePDB::InitializeObject() {
Pavel Labathd1304bb2019-02-18 11:06:57 +0000320 m_obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress();
Zachary Turner307f5ae2018-10-12 19:47:13 +0000321 m_index->SetLoadAddress(m_obj_load_address);
322 m_index->ParseSectionContribs();
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000323
Zachary Turner594c85e2018-12-17 19:43:33 +0000324 TypeSystem *ts = m_obj_file->GetModule()->GetTypeSystemForLanguage(
325 lldb::eLanguageTypeC_plus_plus);
326 if (ts)
327 ts->SetSymbolFile(this);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000328
Zachary Turner594c85e2018-12-17 19:43:33 +0000329 m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000330}
331
Zachary Turner307f5ae2018-10-12 19:47:13 +0000332uint32_t SymbolFileNativePDB::GetNumCompileUnits() {
333 const DbiModuleList &modules = m_index->dbi().modules();
334 uint32_t count = modules.getModuleCount();
335 if (count == 0)
336 return count;
337
338 // The linker can inject an additional "dummy" compilation unit into the
339 // PDB. Ignore this special compile unit for our purposes, if it is there.
340 // It is always the last one.
341 DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1);
342 if (last.getModuleName() == "* Linker *")
343 --count;
344 return count;
345}
346
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000347Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
348 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
349 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000350
351 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) {
352 // This is a function. It must be global. Creating the Function entry for
353 // it automatically creates a block for it.
354 CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii);
355 return GetOrCreateFunction(block_id, *comp_unit)->GetBlock(false);
356 }
357
358 lldbassert(sym.kind() == S_BLOCK32);
359
360 // This is a block. Its parent is either a function or another block. In
361 // either case, its parent can be viewed as a block (e.g. a function contains
362 // 1 big block. So just get the parent block and add this block to it.
363 BlockSym block(static_cast<SymbolRecordKind>(sym.kind()));
364 cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block));
365 lldbassert(block.Parent != 0);
366 PdbCompilandSymId parent_id(block_id.modi, block.Parent);
367 Block &parent_block = GetOrCreateBlock(parent_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000368 lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id);
369 BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
370 parent_block.AddChild(child_block);
Zachary Turner594c85e2018-12-17 19:43:33 +0000371
372 m_ast->GetOrCreateBlockDecl(block_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000373
374 m_blocks.insert({opaque_block_uid, child_block});
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000375 return *child_block;
376}
377
Zachary Turner6284aee2018-11-16 02:42:32 +0000378lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000379 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000380 const CompilandIndexItem *cci =
381 m_index->compilands().GetCompiland(func_id.modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000382 lldbassert(cci);
Zachary Turner6284aee2018-11-16 02:42:32 +0000383 CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(func_id.offset);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000384
385 lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32);
386 SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record);
387
388 auto file_vm_addr = m_index->MakeVirtualAddress(sol.so);
389 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
390 return nullptr;
391
392 AddressRange func_range(file_vm_addr, sol.length,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000393 comp_unit.GetModule()->GetSectionList());
Zachary Turner307f5ae2018-10-12 19:47:13 +0000394 if (!func_range.GetBaseAddress().IsValid())
395 return nullptr;
396
Zachary Turnera42bbe32018-12-07 19:34:02 +0000397 ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind()));
398 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym_record, proc));
Zachary Turner44f19512019-01-10 20:57:32 +0000399 if (proc.FunctionType == TypeIndex::None())
400 return nullptr;
Zachary Turnera42bbe32018-12-07 19:34:02 +0000401 TypeSP func_type = GetOrCreateType(proc.FunctionType);
Zachary Turner44f19512019-01-10 20:57:32 +0000402 if (!func_type)
403 return nullptr;
Zachary Turner307f5ae2018-10-12 19:47:13 +0000404
Zachary Turnera42bbe32018-12-07 19:34:02 +0000405 PdbTypeSymId sig_id(proc.FunctionType, false);
406 Mangled mangled(proc.Name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000407 FunctionSP func_sp = std::make_shared<Function>(
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000408 &comp_unit, toOpaqueUid(func_id), toOpaqueUid(sig_id), mangled,
Zachary Turnera42bbe32018-12-07 19:34:02 +0000409 func_type.get(), func_range);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000410
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000411 comp_unit.AddFunction(func_sp);
412
Zachary Turner594c85e2018-12-17 19:43:33 +0000413 m_ast->GetOrCreateFunctionDecl(func_id);
Zachary Turnera42bbe32018-12-07 19:34:02 +0000414
Zachary Turner307f5ae2018-10-12 19:47:13 +0000415 return func_sp;
416}
417
418CompUnitSP
419SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) {
420 lldb::LanguageType lang =
421 cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage())
422 : lldb::eLanguageTypeUnknown;
423
424 LazyBool optimized = eLazyBoolNo;
425 if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations())
426 optimized = eLazyBoolYes;
427
Zachary Turner51f88af2018-12-19 19:45:30 +0000428 llvm::SmallString<64> source_file_name =
Zachary Turner307f5ae2018-10-12 19:47:13 +0000429 m_index->compilands().GetMainSourceFile(cci);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000430 FileSpec fs(source_file_name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000431
432 CompUnitSP cu_sp =
433 std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs,
Zachary Turner6284aee2018-11-16 02:42:32 +0000434 toOpaqueUid(cci.m_id), lang, optimized);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000435
Zachary Turner6284aee2018-11-16 02:42:32 +0000436 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
437 cci.m_id.modi, cu_sp);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000438 return cu_sp;
439}
440
Zachary Turner6284aee2018-11-16 02:42:32 +0000441lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000442 const ModifierRecord &mr,
443 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000444 TpiStream &stream = m_index->tpi();
445
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000446 std::string name;
447 if (mr.ModifiedType.isSimple())
448 name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind());
449 else
450 name = computeTypeName(stream.typeCollection(), mr.ModifiedType);
451 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000452 lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType);
453
454 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(name),
455 modified_type->GetByteSize(), nullptr,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000456 LLDB_INVALID_UID, Type::eEncodingIsUID, decl,
457 ct, Type::eResolveStateFull);
458}
459
Zachary Turner594c85e2018-12-17 19:43:33 +0000460lldb::TypeSP
461SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id,
462 const llvm::codeview::PointerRecord &pr,
463 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000464 TypeSP pointee = GetOrCreateType(pr.ReferentType);
Zachary Turner544a66d82018-11-01 16:37:29 +0000465 if (!pointee)
466 return nullptr;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000467
468 if (pr.isPointerToMember()) {
469 MemberPointerInfo mpi = pr.getMemberInfo();
Zachary Turner594c85e2018-12-17 19:43:33 +0000470 GetOrCreateType(mpi.ContainingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000471 }
472
Zachary Turner594c85e2018-12-17 19:43:33 +0000473 Declaration decl;
474 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(),
475 pr.getSize(), nullptr, LLDB_INVALID_UID,
476 Type::eEncodingIsUID, decl, ct,
477 Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000478}
479
Zachary Turner594c85e2018-12-17 19:43:33 +0000480lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti,
481 CompilerType ct) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000482 uint64_t uid = toOpaqueUid(PdbTypeSymId(ti, false));
Zachary Turner544a66d82018-11-01 16:37:29 +0000483 if (ti == TypeIndex::NullptrT()) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000484 Declaration decl;
Zachary Turner6284aee2018-11-16 02:42:32 +0000485 return std::make_shared<Type>(
486 uid, this, ConstString("std::nullptr_t"), 0, nullptr, LLDB_INVALID_UID,
487 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner544a66d82018-11-01 16:37:29 +0000488 }
489
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000490 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000491 TypeSP direct_sp = GetOrCreateType(ti.makeDirect());
Zachary Turner71ebb722018-10-23 22:15:05 +0000492 uint32_t pointer_size = 0;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000493 switch (ti.getSimpleMode()) {
494 case SimpleTypeMode::FarPointer32:
495 case SimpleTypeMode::NearPointer32:
496 pointer_size = 4;
497 break;
498 case SimpleTypeMode::NearPointer64:
499 pointer_size = 8;
500 break;
501 default:
502 // 128-bit and 16-bit pointers unsupported.
503 return nullptr;
504 }
505 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000506 return std::make_shared<Type>(
507 uid, this, ConstString(), pointer_size, nullptr, LLDB_INVALID_UID,
508 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000509 }
510
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000511 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
512 return nullptr;
513
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000514 size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind());
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000515 llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind());
516
517 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000518 return std::make_shared<Type>(uid, this, ConstString(type_name), size,
519 nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID,
520 decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000521}
522
Zachary Turner594c85e2018-12-17 19:43:33 +0000523static std::string GetUnqualifiedTypeName(const TagRecord &record) {
Zachary Turner44f19512019-01-10 20:57:32 +0000524 if (!record.hasUniqueName()) {
525 MSVCUndecoratedNameParser parser(record.Name);
526 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
527
528 return specs.back().GetBaseName();
529 }
530
Zachary Turner056e4ab2018-11-08 18:50:11 +0000531 llvm::ms_demangle::Demangler demangler;
532 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
533 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
Zachary Turner44f19512019-01-10 20:57:32 +0000534 if (demangler.Error)
535 return record.Name;
536
Zachary Turner056e4ab2018-11-08 18:50:11 +0000537 llvm::ms_demangle::IdentifierNode *idn =
538 ttn->QualifiedName->getUnqualifiedIdentifier();
Zachary Turner594c85e2018-12-17 19:43:33 +0000539 return idn->toString();
Zachary Turner056e4ab2018-11-08 18:50:11 +0000540}
541
Zachary Turner594c85e2018-12-17 19:43:33 +0000542lldb::TypeSP
543SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id,
544 const TagRecord &record,
545 size_t size, CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000546
Zachary Turner594c85e2018-12-17 19:43:33 +0000547 std::string uname = GetUnqualifiedTypeName(record);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000548
549 // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE.
550 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000551 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(uname),
552 size, nullptr, LLDB_INVALID_UID,
553 Type::eEncodingIsUID, decl, ct,
554 Type::eResolveStateForward);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000555}
556
Zachary Turner6284aee2018-11-16 02:42:32 +0000557lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000558 const ClassRecord &cr,
559 CompilerType ct) {
560 return CreateClassStructUnion(type_id, cr, cr.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000561}
562
Zachary Turner6284aee2018-11-16 02:42:32 +0000563lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000564 const UnionRecord &ur,
565 CompilerType ct) {
566 return CreateClassStructUnion(type_id, ur, ur.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000567}
568
Zachary Turner6284aee2018-11-16 02:42:32 +0000569lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000570 const EnumRecord &er,
571 CompilerType ct) {
572 std::string uname = GetUnqualifiedTypeName(er);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000573
574 Declaration decl;
575 TypeSP underlying_type = GetOrCreateType(er.UnderlyingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000576
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000577 return std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000578 toOpaqueUid(type_id), this, ConstString(uname),
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000579 underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000580 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000581 lldb_private::Type::eResolveStateForward);
582}
583
Zachary Turner6284aee2018-11-16 02:42:32 +0000584TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000585 const ArrayRecord &ar,
586 CompilerType ct) {
Zachary Turner511bff22018-10-30 18:57:08 +0000587 TypeSP element_type = GetOrCreateType(ar.ElementType);
Zachary Turner511bff22018-10-30 18:57:08 +0000588
589 Declaration decl;
590 TypeSP array_sp = std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000591 toOpaqueUid(type_id), this, ConstString(), ar.Size, nullptr,
592 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct,
593 lldb_private::Type::eResolveStateFull);
Zachary Turner511bff22018-10-30 18:57:08 +0000594 array_sp->SetEncodingType(element_type.get());
595 return array_sp;
596}
597
Aleksandr Urakov869f9342019-05-13 09:41:57 +0000598
599TypeSP SymbolFileNativePDB::CreateFunctionType(PdbTypeSymId type_id,
600 const MemberFunctionRecord &mfr,
601 CompilerType ct) {
602 Declaration decl;
603 return std::make_shared<lldb_private::Type>(
604 toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID,
605 lldb_private::Type::eEncodingIsUID, decl, ct,
606 lldb_private::Type::eResolveStateFull);
607}
608
Zachary Turner6284aee2018-11-16 02:42:32 +0000609TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000610 const ProcedureRecord &pr,
611 CompilerType ct) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000612 Declaration decl;
613 return std::make_shared<lldb_private::Type>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000614 toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000615 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner544a66d82018-11-01 16:37:29 +0000616 lldb_private::Type::eResolveStateFull);
617}
618
Zachary Turner594c85e2018-12-17 19:43:33 +0000619TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000620 if (type_id.index.isSimple())
Zachary Turner594c85e2018-12-17 19:43:33 +0000621 return CreateSimpleType(type_id.index, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000622
Zachary Turner6284aee2018-11-16 02:42:32 +0000623 TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi();
624 CVType cvt = stream.getType(type_id.index);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000625
626 if (cvt.kind() == LF_MODIFIER) {
627 ModifierRecord modifier;
628 llvm::cantFail(
629 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
Zachary Turner594c85e2018-12-17 19:43:33 +0000630 return CreateModifierType(type_id, modifier, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000631 }
632
633 if (cvt.kind() == LF_POINTER) {
634 PointerRecord pointer;
635 llvm::cantFail(
636 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
Zachary Turner594c85e2018-12-17 19:43:33 +0000637 return CreatePointerType(type_id, pointer, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000638 }
639
640 if (IsClassRecord(cvt.kind())) {
641 ClassRecord cr;
642 llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000643 return CreateTagType(type_id, cr, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000644 }
645
646 if (cvt.kind() == LF_ENUM) {
647 EnumRecord er;
648 llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er));
Zachary Turner594c85e2018-12-17 19:43:33 +0000649 return CreateTagType(type_id, er, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000650 }
651
652 if (cvt.kind() == LF_UNION) {
653 UnionRecord ur;
654 llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur));
Zachary Turner594c85e2018-12-17 19:43:33 +0000655 return CreateTagType(type_id, ur, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000656 }
657
Zachary Turner511bff22018-10-30 18:57:08 +0000658 if (cvt.kind() == LF_ARRAY) {
659 ArrayRecord ar;
660 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
Zachary Turner594c85e2018-12-17 19:43:33 +0000661 return CreateArrayType(type_id, ar, ct);
Zachary Turner511bff22018-10-30 18:57:08 +0000662 }
663
Zachary Turner544a66d82018-11-01 16:37:29 +0000664 if (cvt.kind() == LF_PROCEDURE) {
665 ProcedureRecord pr;
666 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000667 return CreateProcedureType(type_id, pr, ct);
Zachary Turner544a66d82018-11-01 16:37:29 +0000668 }
Aleksandr Urakov869f9342019-05-13 09:41:57 +0000669 if (cvt.kind() == LF_MFUNCTION) {
670 MemberFunctionRecord mfr;
671 llvm::cantFail(TypeDeserializer::deserializeAs<MemberFunctionRecord>(cvt, mfr));
672 return CreateFunctionType(type_id, mfr, ct);
673 }
Zachary Turner544a66d82018-11-01 16:37:29 +0000674
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000675 return nullptr;
676}
677
Zachary Turner6284aee2018-11-16 02:42:32 +0000678TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000679 // If they search for a UDT which is a forward ref, try and resolve the full
680 // decl and just map the forward ref uid to the full decl record.
Zachary Turner6284aee2018-11-16 02:42:32 +0000681 llvm::Optional<PdbTypeSymId> full_decl_uid;
682 if (IsForwardRefUdt(type_id, m_index->tpi())) {
683 auto expected_full_ti =
684 m_index->tpi().findFullDeclForForwardRef(type_id.index);
685 if (!expected_full_ti)
686 llvm::consumeError(expected_full_ti.takeError());
687 else if (*expected_full_ti != type_id.index) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000688 full_decl_uid = PdbTypeSymId(*expected_full_ti, false);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000689
Zachary Turner6284aee2018-11-16 02:42:32 +0000690 // It's possible that a lookup would occur for the full decl causing it
691 // to be cached, then a second lookup would occur for the forward decl.
692 // We don't want to create a second full decl, so make sure the full
693 // decl hasn't already been cached.
694 auto full_iter = m_types.find(toOpaqueUid(*full_decl_uid));
695 if (full_iter != m_types.end()) {
696 TypeSP result = full_iter->second;
697 // Map the forward decl to the TypeSP for the full decl so we can take
698 // the fast path next time.
699 m_types[toOpaqueUid(type_id)] = result;
700 return result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000701 }
702 }
703 }
704
Zachary Turner6284aee2018-11-16 02:42:32 +0000705 PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id;
Zachary Turner594c85e2018-12-17 19:43:33 +0000706
707 clang::QualType qt = m_ast->GetOrCreateType(best_decl_id);
708
709 TypeSP result = CreateType(best_decl_id, m_ast->ToCompilerType(qt));
Zachary Turner544a66d82018-11-01 16:37:29 +0000710 if (!result)
711 return nullptr;
Zachary Turner6284aee2018-11-16 02:42:32 +0000712
713 uint64_t best_uid = toOpaqueUid(best_decl_id);
714 m_types[best_uid] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000715 // If we had both a forward decl and a full decl, make both point to the new
716 // type.
717 if (full_decl_uid)
Zachary Turner6284aee2018-11-16 02:42:32 +0000718 m_types[toOpaqueUid(type_id)] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000719
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000720 return result;
721}
722
Zachary Turner6284aee2018-11-16 02:42:32 +0000723TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000724 // We can't use try_emplace / overwrite here because the process of creating
725 // a type could create nested types, which could invalidate iterators. So
726 // we have to do a 2-phase lookup / insert.
Zachary Turner6284aee2018-11-16 02:42:32 +0000727 auto iter = m_types.find(toOpaqueUid(type_id));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000728 if (iter != m_types.end())
729 return iter->second;
730
Zachary Turner44f19512019-01-10 20:57:32 +0000731 TypeSP type = CreateAndCacheType(type_id);
732 if (type)
733 m_obj_file->GetModule()->GetTypeList()->Insert(type);
734 return type;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000735}
736
Zachary Turner6284aee2018-11-16 02:42:32 +0000737VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
738 CVSymbol sym = m_index->symrecords().readRecord(var_id.offset);
Zachary Turner2af34162018-11-13 20:07:57 +0000739 if (sym.kind() == S_CONSTANT)
Zachary Turner6284aee2018-11-16 02:42:32 +0000740 return CreateConstantSymbol(var_id, sym);
Zachary Turner2af34162018-11-13 20:07:57 +0000741
Zachary Turner9f727952018-10-26 09:06:38 +0000742 lldb::ValueType scope = eValueTypeInvalid;
743 TypeIndex ti;
744 llvm::StringRef name;
745 lldb::addr_t addr = 0;
746 uint16_t section = 0;
747 uint32_t offset = 0;
748 bool is_external = false;
749 switch (sym.kind()) {
750 case S_GDATA32:
751 is_external = true;
752 LLVM_FALLTHROUGH;
753 case S_LDATA32: {
754 DataSym ds(sym.kind());
755 llvm::cantFail(SymbolDeserializer::deserializeAs<DataSym>(sym, ds));
756 ti = ds.Type;
757 scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal
758 : eValueTypeVariableStatic;
759 name = ds.Name;
760 section = ds.Segment;
761 offset = ds.DataOffset;
762 addr = m_index->MakeVirtualAddress(ds.Segment, ds.DataOffset);
763 break;
764 }
765 case S_GTHREAD32:
766 is_external = true;
767 LLVM_FALLTHROUGH;
768 case S_LTHREAD32: {
769 ThreadLocalDataSym tlds(sym.kind());
770 llvm::cantFail(
771 SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(sym, tlds));
772 ti = tlds.Type;
773 name = tlds.Name;
774 section = tlds.Segment;
775 offset = tlds.DataOffset;
776 addr = m_index->MakeVirtualAddress(tlds.Segment, tlds.DataOffset);
777 scope = eValueTypeVariableThreadLocal;
778 break;
779 }
780 default:
781 llvm_unreachable("unreachable!");
782 }
783
784 CompUnitSP comp_unit;
785 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr);
786 if (modi) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000787 CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi);
Zachary Turner9f727952018-10-26 09:06:38 +0000788 comp_unit = GetOrCreateCompileUnit(cci);
789 }
790
791 Declaration decl;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000792 PdbTypeSymId tid(ti, false);
Zachary Turner9f727952018-10-26 09:06:38 +0000793 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000794 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner9f727952018-10-26 09:06:38 +0000795 Variable::RangeList ranges;
796
Zachary Turner22566332019-01-02 18:33:54 +0000797 m_ast->GetOrCreateVariableDecl(var_id);
Zachary Turner37900292018-12-20 23:32:37 +0000798
Zachary Turner9f727952018-10-26 09:06:38 +0000799 DWARFExpression location = MakeGlobalLocationExpression(
800 section, offset, GetObjectFile()->GetModule());
801
802 std::string global_name("::");
803 global_name += name;
804 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000805 toOpaqueUid(var_id), name.str().c_str(), global_name.c_str(), type_sp,
Zachary Turner9f727952018-10-26 09:06:38 +0000806 scope, comp_unit.get(), ranges, &decl, location, is_external, false,
807 false);
808 var_sp->SetLocationIsConstantValueData(false);
809
810 return var_sp;
811}
812
Zachary Turner2af34162018-11-13 20:07:57 +0000813lldb::VariableSP
Zachary Turner6284aee2018-11-16 02:42:32 +0000814SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
Zachary Turner2af34162018-11-13 20:07:57 +0000815 const CVSymbol &cvs) {
816 TpiStream &tpi = m_index->tpi();
817 ConstantSym constant(cvs.kind());
818
819 llvm::cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(cvs, constant));
820 std::string global_name("::");
821 global_name += constant.Name;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000822 PdbTypeSymId tid(constant.Type, false);
Zachary Turner2af34162018-11-13 20:07:57 +0000823 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000824 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner2af34162018-11-13 20:07:57 +0000825
826 Declaration decl;
827 Variable::RangeList ranges;
828 ModuleSP module = GetObjectFile()->GetModule();
Zachary Turnera93458b2018-12-06 17:49:15 +0000829 DWARFExpression location = MakeConstantLocationExpression(
830 constant.Type, tpi, constant.Value, module);
Zachary Turner2af34162018-11-13 20:07:57 +0000831
832 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000833 toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(),
Zachary Turner2af34162018-11-13 20:07:57 +0000834 type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location,
835 false, false, false);
836 var_sp->SetLocationIsConstantValueData(true);
837 return var_sp;
838}
839
Zachary Turner6284aee2018-11-16 02:42:32 +0000840VariableSP
841SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) {
842 auto emplace_result = m_global_vars.try_emplace(toOpaqueUid(var_id), nullptr);
Zachary Turner9f727952018-10-26 09:06:38 +0000843 if (emplace_result.second)
Zachary Turner6284aee2018-11-16 02:42:32 +0000844 emplace_result.first->second = CreateGlobalVariable(var_id);
Zachary Turner9f727952018-10-26 09:06:38 +0000845
846 return emplace_result.first->second;
847}
848
Zachary Turner6284aee2018-11-16 02:42:32 +0000849lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000850 return GetOrCreateType(PdbTypeSymId(ti, false));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000851}
852
Zachary Turner6284aee2018-11-16 02:42:32 +0000853FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000854 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000855 auto emplace_result = m_functions.try_emplace(toOpaqueUid(func_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000856 if (emplace_result.second)
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000857 emplace_result.first->second = CreateFunction(func_id, comp_unit);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000858
Zachary Turner307f5ae2018-10-12 19:47:13 +0000859 return emplace_result.first->second;
860}
861
862CompUnitSP
863SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000864
Zachary Turner307f5ae2018-10-12 19:47:13 +0000865 auto emplace_result =
Zachary Turner6284aee2018-11-16 02:42:32 +0000866 m_compilands.try_emplace(toOpaqueUid(cci.m_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000867 if (emplace_result.second)
868 emplace_result.first->second = CreateCompileUnit(cci);
869
870 lldbassert(emplace_result.first->second);
871 return emplace_result.first->second;
872}
873
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000874Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
875 auto iter = m_blocks.find(toOpaqueUid(block_id));
876 if (iter != m_blocks.end())
877 return *iter->second;
878
879 return CreateBlock(block_id);
880}
881
Zachary Turner22566332019-01-02 18:33:54 +0000882void SymbolFileNativePDB::ParseDeclsForContext(
883 lldb_private::CompilerDeclContext decl_ctx) {
884 clang::DeclContext *context = m_ast->FromCompilerDeclContext(decl_ctx);
885 if (!context)
886 return;
887 m_ast->ParseDeclsForContext(*context);
888}
889
Zachary Turner307f5ae2018-10-12 19:47:13 +0000890lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) {
891 if (index >= GetNumCompileUnits())
892 return CompUnitSP();
893 lldbassert(index < UINT16_MAX);
894 if (index >= UINT16_MAX)
895 return nullptr;
896
897 CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index);
898
899 return GetOrCreateCompileUnit(item);
900}
901
Zachary Turner863f8c12019-01-11 18:03:20 +0000902lldb::LanguageType SymbolFileNativePDB::ParseLanguage(CompileUnit &comp_unit) {
903 PdbSymUid uid(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +0000904 lldbassert(uid.kind() == PdbSymUidKind::Compiland);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000905
Zachary Turner6284aee2018-11-16 02:42:32 +0000906 CompilandIndexItem *item =
907 m_index->compilands().GetCompiland(uid.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000908 lldbassert(item);
909 if (!item->m_compile_opts)
910 return lldb::eLanguageTypeUnknown;
911
912 return TranslateLanguage(item->m_compile_opts->getLanguage());
913}
914
Zachary Turnerb3130b42019-01-02 18:32:50 +0000915void SymbolFileNativePDB::AddSymbols(Symtab &symtab) { return; }
916
Zachary Turner863f8c12019-01-11 18:03:20 +0000917size_t SymbolFileNativePDB::ParseFunctions(CompileUnit &comp_unit) {
918 PdbSymUid uid{comp_unit.GetID()};
Zachary Turner44f19512019-01-10 20:57:32 +0000919 lldbassert(uid.kind() == PdbSymUidKind::Compiland);
920 uint16_t modi = uid.asCompiland().modi;
921 CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi);
922
Zachary Turner863f8c12019-01-11 18:03:20 +0000923 size_t count = comp_unit.GetNumFunctions();
Zachary Turner44f19512019-01-10 20:57:32 +0000924 const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray();
925 for (auto iter = syms.begin(); iter != syms.end(); ++iter) {
926 if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32)
927 continue;
928
929 PdbCompilandSymId sym_id{modi, iter.offset()};
930
Zachary Turner863f8c12019-01-11 18:03:20 +0000931 FunctionSP func = GetOrCreateFunction(sym_id, comp_unit);
Zachary Turner44f19512019-01-10 20:57:32 +0000932 }
933
Zachary Turner863f8c12019-01-11 18:03:20 +0000934 size_t new_count = comp_unit.GetNumFunctions();
Zachary Turner44f19512019-01-10 20:57:32 +0000935 lldbassert(new_count >= count);
936 return new_count - count;
Zachary Turner307f5ae2018-10-12 19:47:13 +0000937}
938
939static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) {
940 // If any of these flags are set, we need to resolve the compile unit.
941 uint32_t flags = eSymbolContextCompUnit;
942 flags |= eSymbolContextVariable;
943 flags |= eSymbolContextFunction;
944 flags |= eSymbolContextBlock;
945 flags |= eSymbolContextLineEntry;
946 return (resolve_scope & flags) != 0;
947}
948
Zachary Turner991e4452018-10-25 20:45:19 +0000949uint32_t SymbolFileNativePDB::ResolveSymbolContext(
950 const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000951 uint32_t resolved_flags = 0;
952 lldb::addr_t file_addr = addr.GetFileAddress();
953
954 if (NeedsResolvedCompileUnit(resolve_scope)) {
955 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr);
956 if (!modi)
957 return 0;
Zachary Turner6284aee2018-11-16 02:42:32 +0000958 CompilandIndexItem *cci = m_index->compilands().GetCompiland(*modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000959 if (!cci)
960 return 0;
961
962 sc.comp_unit = GetOrCreateCompileUnit(*cci).get();
963 resolved_flags |= eSymbolContextCompUnit;
964 }
965
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000966 if (resolve_scope & eSymbolContextFunction ||
967 resolve_scope & eSymbolContextBlock) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000968 lldbassert(sc.comp_unit);
969 std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000970 // Search the matches in reverse. This way if there are multiple matches
971 // (for example we are 3 levels deep in a nested scope) it will find the
972 // innermost one first.
973 for (const auto &match : llvm::reverse(matches)) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000974 if (match.uid.kind() != PdbSymUidKind::CompilandSym)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000975 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000976
Zachary Turner6284aee2018-11-16 02:42:32 +0000977 PdbCompilandSymId csid = match.uid.asCompilandSym();
978 CVSymbol cvs = m_index->ReadSymbolRecord(csid);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000979 PDB_SymType type = CVSymToPDBSym(cvs.kind());
980 if (type != PDB_SymType::Function && type != PDB_SymType::Block)
Zachary Turner6284aee2018-11-16 02:42:32 +0000981 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000982 if (type == PDB_SymType::Function) {
983 sc.function = GetOrCreateFunction(csid, *sc.comp_unit).get();
984 sc.block = sc.GetFunctionBlock();
985 }
986
987 if (type == PDB_SymType::Block) {
988 sc.block = &GetOrCreateBlock(csid);
989 sc.function = sc.block->CalculateSymbolContextFunction();
990 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000991 resolved_flags |= eSymbolContextFunction;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000992 resolved_flags |= eSymbolContextBlock;
993 break;
994 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000995 }
996
997 if (resolve_scope & eSymbolContextLineEntry) {
998 lldbassert(sc.comp_unit);
999 if (auto *line_table = sc.comp_unit->GetLineTable()) {
1000 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
1001 resolved_flags |= eSymbolContextLineEntry;
1002 }
1003 }
1004
1005 return resolved_flags;
1006}
1007
Zachary Turnerb3130b42019-01-02 18:32:50 +00001008uint32_t SymbolFileNativePDB::ResolveSymbolContext(
1009 const FileSpec &file_spec, uint32_t line, bool check_inlines,
1010 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
1011 return 0;
1012}
1013
Zachary Turner307f5ae2018-10-12 19:47:13 +00001014static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence,
1015 const CompilandIndexItem &cci,
1016 lldb::addr_t base_addr,
1017 uint32_t file_number,
1018 const LineFragmentHeader &block,
1019 const LineNumberEntry &cur) {
1020 LineInfo cur_info(cur.Flags);
1021
1022 if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto())
1023 return;
1024
1025 uint64_t addr = base_addr + cur.Offset;
1026
1027 bool is_statement = cur_info.isStatement();
1028 bool is_prologue = IsFunctionPrologue(cci, addr);
1029 bool is_epilogue = IsFunctionEpilogue(cci, addr);
1030
1031 uint32_t lno = cur_info.getStartLine();
1032
1033 table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number,
1034 is_statement, false, is_prologue, is_epilogue,
1035 false);
1036}
1037
1038static void TerminateLineSequence(LineTable &table,
1039 const LineFragmentHeader &block,
1040 lldb::addr_t base_addr, uint32_t file_number,
1041 uint32_t last_line,
1042 std::unique_ptr<LineSequence> seq) {
1043 // The end is always a terminal entry, so insert it regardless.
1044 table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize,
1045 last_line, 0, file_number, false, false,
1046 false, false, true);
1047 table.InsertSequence(seq.release());
1048}
1049
Zachary Turner863f8c12019-01-11 18:03:20 +00001050bool SymbolFileNativePDB::ParseLineTable(CompileUnit &comp_unit) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001051 // Unfortunately LLDB is set up to parse the entire compile unit line table
1052 // all at once, even if all it really needs is line info for a specific
1053 // function. In the future it would be nice if it could set the sc.m_function
1054 // member, and we could only get the line info for the function in question.
Zachary Turner863f8c12019-01-11 18:03:20 +00001055 PdbSymUid cu_id(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +00001056 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1057 CompilandIndexItem *cci =
1058 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001059 lldbassert(cci);
Zachary Turner863f8c12019-01-11 18:03:20 +00001060 auto line_table = llvm::make_unique<LineTable>(&comp_unit);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001061
1062 // This is basically a copy of the .debug$S subsections from all original COFF
1063 // object files merged together with address relocations applied. We are
1064 // looking for all DEBUG_S_LINES subsections.
1065 for (const DebugSubsectionRecord &dssr :
1066 cci->m_debug_stream.getSubsectionsArray()) {
1067 if (dssr.kind() != DebugSubsectionKind::Lines)
1068 continue;
1069
1070 DebugLinesSubsectionRef lines;
1071 llvm::BinaryStreamReader reader(dssr.getRecordData());
1072 if (auto EC = lines.initialize(reader)) {
1073 llvm::consumeError(std::move(EC));
1074 return false;
1075 }
1076
1077 const LineFragmentHeader *lfh = lines.header();
1078 uint64_t virtual_addr =
1079 m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset);
1080
1081 const auto &checksums = cci->m_strings.checksums().getArray();
1082 const auto &strings = cci->m_strings.strings();
1083 for (const LineColumnEntry &group : lines) {
1084 // Indices in this structure are actually offsets of records in the
1085 // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index
1086 // into the global PDB string table.
1087 auto iter = checksums.at(group.NameIndex);
1088 if (iter == checksums.end())
1089 continue;
1090
1091 llvm::Expected<llvm::StringRef> efn =
1092 strings.getString(iter->FileNameOffset);
1093 if (!efn) {
1094 llvm::consumeError(efn.takeError());
1095 continue;
1096 }
1097
1098 // LLDB wants the index of the file in the list of support files.
1099 auto fn_iter = llvm::find(cci->m_file_list, *efn);
1100 lldbassert(fn_iter != cci->m_file_list.end());
Zachary Turnerb3130b42019-01-02 18:32:50 +00001101 // LLDB support file indices are 1-based.
1102 uint32_t file_index =
1103 1 + std::distance(cci->m_file_list.begin(), fn_iter);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001104
1105 std::unique_ptr<LineSequence> sequence(
1106 line_table->CreateLineSequenceContainer());
1107 lldbassert(!group.LineNumbers.empty());
1108
1109 for (const LineNumberEntry &entry : group.LineNumbers) {
1110 AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr,
1111 file_index, *lfh, entry);
1112 }
1113 LineInfo last_line(group.LineNumbers.back().Flags);
1114 TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index,
1115 last_line.getEndLine(), std::move(sequence));
1116 }
1117 }
1118
1119 if (line_table->GetSize() == 0)
1120 return false;
1121
Zachary Turner863f8c12019-01-11 18:03:20 +00001122 comp_unit.SetLineTable(line_table.release());
Zachary Turner307f5ae2018-10-12 19:47:13 +00001123 return true;
1124}
1125
Zachary Turner863f8c12019-01-11 18:03:20 +00001126bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit &comp_unit) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001127 // PDB doesn't contain information about macros
1128 return false;
1129}
1130
Zachary Turner863f8c12019-01-11 18:03:20 +00001131bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit &comp_unit,
1132 FileSpecList &support_files) {
1133 PdbSymUid cu_id(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +00001134 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1135 CompilandIndexItem *cci =
1136 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001137 lldbassert(cci);
1138
1139 for (llvm::StringRef f : cci->m_file_list) {
1140 FileSpec::Style style =
1141 f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001142 FileSpec spec(f, style);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001143 support_files.Append(spec);
1144 }
1145
Zachary Turnerb3130b42019-01-02 18:32:50 +00001146 llvm::SmallString<64> main_source_file =
1147 m_index->compilands().GetMainSourceFile(*cci);
1148 FileSpec::Style style = main_source_file.startswith("/")
1149 ? FileSpec::Style::posix
1150 : FileSpec::Style::windows;
1151 FileSpec spec(main_source_file, style);
1152 support_files.Insert(0, spec);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001153 return true;
1154}
1155
1156bool SymbolFileNativePDB::ParseImportedModules(
Adrian Prantl0f30a3b2019-02-13 18:10:41 +00001157 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001158 // PDB does not yet support module debug info
1159 return false;
1160}
1161
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001162size_t SymbolFileNativePDB::ParseBlocksRecursive(Function &func) {
1163 GetOrCreateBlock(PdbSymUid(func.GetID()).asCompilandSym());
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001164 // FIXME: Parse child blocks
1165 return 1;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001166}
1167
Zachary Turner594c85e2018-12-17 19:43:33 +00001168void SymbolFileNativePDB::DumpClangAST(Stream &s) { m_ast->Dump(s); }
Zachary Turner49110232018-11-05 17:40:28 +00001169
Zachary Turner9f727952018-10-26 09:06:38 +00001170uint32_t SymbolFileNativePDB::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001171 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner9f727952018-10-26 09:06:38 +00001172 uint32_t max_matches, VariableList &variables) {
1173 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1174
1175 std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName(
1176 name.GetStringRef(), m_index->symrecords());
1177 for (const SymbolAndOffset &result : results) {
1178 VariableSP var;
1179 switch (result.second.kind()) {
1180 case SymbolKind::S_GDATA32:
1181 case SymbolKind::S_LDATA32:
1182 case SymbolKind::S_GTHREAD32:
Zachary Turner2af34162018-11-13 20:07:57 +00001183 case SymbolKind::S_LTHREAD32:
1184 case SymbolKind::S_CONSTANT: {
Zachary Turner9fbf9352018-11-16 03:16:27 +00001185 PdbGlobalSymId global(result.first, false);
Zachary Turner6284aee2018-11-16 02:42:32 +00001186 var = GetOrCreateGlobalVariable(global);
Zachary Turner9f727952018-10-26 09:06:38 +00001187 variables.AddVariable(var);
1188 break;
1189 }
1190 default:
1191 continue;
1192 }
1193 }
1194 return variables.GetSize();
1195}
1196
Zachary Turner307f5ae2018-10-12 19:47:13 +00001197uint32_t SymbolFileNativePDB::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001198 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001199 FunctionNameType name_type_mask, bool include_inlines, bool append,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001200 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001201 // For now we only support lookup by method name.
1202 if (!(name_type_mask & eFunctionNameTypeMethod))
1203 return 0;
1204
1205 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1206
1207 std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName(
1208 name.GetStringRef(), m_index->symrecords());
1209 for (const SymbolAndOffset &match : matches) {
1210 if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF)
1211 continue;
1212 ProcRefSym proc(match.second.kind());
1213 cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc));
1214
1215 if (!IsValidRecord(proc))
1216 continue;
1217
Zachary Turner6284aee2018-11-16 02:42:32 +00001218 CompilandIndexItem &cci =
1219 m_index->compilands().GetOrCreateCompiland(proc.modi());
Zachary Turnerb96181c2018-10-22 16:19:07 +00001220 SymbolContext sc;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001221
1222 sc.comp_unit = GetOrCreateCompileUnit(cci).get();
Zachary Turner9fbf9352018-11-16 03:16:27 +00001223 PdbCompilandSymId func_id(proc.modi(), proc.SymOffset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001224 sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get();
Zachary Turner307f5ae2018-10-12 19:47:13 +00001225
1226 sc_list.Append(sc);
1227 }
1228
1229 return sc_list.GetSize();
1230}
1231
Zachary Turnerb96181c2018-10-22 16:19:07 +00001232uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression &regex,
1233 bool include_inlines, bool append,
1234 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001235 return 0;
1236}
1237
Zachary Turnerb96181c2018-10-22 16:19:07 +00001238uint32_t SymbolFileNativePDB::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001239 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00001240 bool append, uint32_t max_matches,
1241 llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001242 if (!append)
1243 types.Clear();
1244 if (!name)
1245 return 0;
1246
1247 searched_symbol_files.clear();
1248 searched_symbol_files.insert(this);
1249
1250 // There is an assumption 'name' is not a regex
1251 size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types);
1252
1253 return match_count;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001254}
1255
1256size_t
1257SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context,
1258 bool append, TypeMap &types) {
1259 return 0;
1260}
1261
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001262size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name,
1263 uint32_t max_matches,
1264 TypeMap &types) {
1265
1266 size_t match_count = 0;
1267 std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name);
1268 if (max_matches > 0 && max_matches < matches.size())
1269 matches.resize(max_matches);
1270
1271 for (TypeIndex ti : matches) {
1272 TypeSP type = GetOrCreateType(ti);
1273 if (!type)
1274 continue;
1275
1276 types.Insert(type);
1277 ++match_count;
1278 }
1279 return match_count;
1280}
1281
Zachary Turner863f8c12019-01-11 18:03:20 +00001282size_t SymbolFileNativePDB::ParseTypes(CompileUnit &comp_unit) {
Zachary Turner44f19512019-01-10 20:57:32 +00001283 // Only do the full type scan the first time.
1284 if (m_done_full_type_scan)
1285 return 0;
1286
1287 size_t old_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1288 LazyRandomTypeCollection &types = m_index->tpi().typeCollection();
1289
1290 // First process the entire TPI stream.
1291 for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) {
1292 TypeSP type = GetOrCreateType(*ti);
1293 if (type)
1294 (void)type->GetFullCompilerType();
1295 }
1296
1297 // Next look for S_UDT records in the globals stream.
1298 for (const uint32_t gid : m_index->globals().getGlobalsTable()) {
1299 PdbGlobalSymId global{gid, false};
1300 CVSymbol sym = m_index->ReadSymbolRecord(global);
1301 if (sym.kind() != S_UDT)
1302 continue;
1303
1304 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1305 bool is_typedef = true;
1306 if (IsTagRecord(PdbTypeSymId{udt.Type, false}, m_index->tpi())) {
1307 CVType cvt = m_index->tpi().getType(udt.Type);
1308 llvm::StringRef name = CVTagRecord::create(cvt).name();
1309 if (name == udt.Name)
1310 is_typedef = false;
1311 }
1312
1313 if (is_typedef)
1314 GetOrCreateTypedef(global);
1315 }
1316
1317 size_t new_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1318
1319 m_done_full_type_scan = true;
1320
1321 return new_count - old_count;
1322}
Zachary Turnerb96181c2018-10-22 16:19:07 +00001323
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001324size_t
1325SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit,
1326 VariableList &variables) {
1327 PdbSymUid sym_uid(comp_unit.GetID());
1328 lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland);
1329 return 0;
1330}
1331
1332VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
1333 PdbCompilandSymId var_id,
1334 bool is_param) {
1335 ModuleSP module = GetObjectFile()->GetModule();
Aleksandr Urakov758657e2019-02-01 10:01:18 +00001336 Block &block = GetOrCreateBlock(scope_id);
1337 VariableInfo var_info =
1338 GetVariableLocationInfo(*m_index, var_id, block, module);
Zachary Turner44f19512019-01-10 20:57:32 +00001339 if (!var_info.location || !var_info.ranges)
1340 return nullptr;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001341
1342 CompilandIndexItem *cii = m_index->compilands().GetCompiland(var_id.modi);
1343 CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(*cii);
1344 TypeSP type_sp = GetOrCreateType(var_info.type);
1345 std::string name = var_info.name.str();
1346 Declaration decl;
1347 SymbolFileTypeSP sftype =
1348 std::make_shared<SymbolFileType>(*this, type_sp->GetID());
1349
1350 ValueType var_scope =
1351 is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal;
1352 VariableSP var_sp = std::make_shared<Variable>(
1353 toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope,
1354 comp_unit_sp.get(), *var_info.ranges, &decl, *var_info.location, false,
1355 false, false);
1356
Zachary Turner594c85e2018-12-17 19:43:33 +00001357 if (!is_param)
Zachary Turner22566332019-01-02 18:33:54 +00001358 m_ast->GetOrCreateVariableDecl(scope_id, var_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001359
1360 m_local_variables[toOpaqueUid(var_id)] = var_sp;
1361 return var_sp;
1362}
1363
1364VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable(
1365 PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) {
1366 auto iter = m_local_variables.find(toOpaqueUid(var_id));
1367 if (iter != m_local_variables.end())
1368 return iter->second;
1369
1370 return CreateLocalVariable(scope_id, var_id, is_param);
1371}
1372
Zachary Turner44f19512019-01-10 20:57:32 +00001373TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) {
1374 CVSymbol sym = m_index->ReadSymbolRecord(id);
1375 lldbassert(sym.kind() == SymbolKind::S_UDT);
1376
1377 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1378
1379 TypeSP target_type = GetOrCreateType(udt.Type);
1380
1381 (void)m_ast->GetOrCreateTypedefDecl(id);
1382
1383 Declaration decl;
1384 return std::make_shared<lldb_private::Type>(
1385 toOpaqueUid(id), this, ConstString(udt.Name), target_type->GetByteSize(),
1386 nullptr, target_type->GetID(), lldb_private::Type::eEncodingIsTypedefUID,
1387 decl, target_type->GetForwardCompilerType(),
1388 lldb_private::Type::eResolveStateForward);
1389}
1390
1391TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) {
1392 auto iter = m_types.find(toOpaqueUid(id));
1393 if (iter != m_types.end())
1394 return iter->second;
1395
1396 return CreateTypedef(id);
1397}
1398
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001399size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
1400 Block &block = GetOrCreateBlock(block_id);
1401
1402 size_t count = 0;
1403
1404 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
1405 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
1406 uint32_t params_remaining = 0;
1407 switch (sym.kind()) {
1408 case S_GPROC32:
1409 case S_LPROC32: {
1410 ProcSym proc(static_cast<SymbolRecordKind>(sym.kind()));
1411 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym, proc));
1412 CVType signature = m_index->tpi().getType(proc.FunctionType);
1413 ProcedureRecord sig;
1414 cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(signature, sig));
1415 params_remaining = sig.getParameterCount();
1416 break;
1417 }
1418 case S_BLOCK32:
1419 break;
1420 default:
1421 lldbassert(false && "Symbol is not a block!");
1422 return 0;
1423 }
1424
1425 VariableListSP variables = block.GetBlockVariableList(false);
1426 if (!variables) {
1427 variables = std::make_shared<VariableList>();
1428 block.SetVariableList(variables);
1429 }
1430
1431 CVSymbolArray syms = limitSymbolArrayToScope(
1432 cii->m_debug_stream.getSymbolArray(), block_id.offset);
1433
1434 // Skip the first record since it's a PROC32 or BLOCK32, and there's
1435 // no point examining it since we know it's not a local variable.
1436 syms.drop_front();
1437 auto iter = syms.begin();
1438 auto end = syms.end();
1439
1440 while (iter != end) {
1441 uint32_t record_offset = iter.offset();
1442 CVSymbol variable_cvs = *iter;
1443 PdbCompilandSymId child_sym_id(block_id.modi, record_offset);
1444 ++iter;
1445
1446 // If this is a block, recurse into its children and then skip it.
1447 if (variable_cvs.kind() == S_BLOCK32) {
1448 uint32_t block_end = getScopeEndOffset(variable_cvs);
1449 count += ParseVariablesForBlock(child_sym_id);
1450 iter = syms.at(block_end);
1451 continue;
1452 }
1453
1454 bool is_param = params_remaining > 0;
1455 VariableSP variable;
1456 switch (variable_cvs.kind()) {
1457 case S_REGREL32:
1458 case S_REGISTER:
1459 case S_LOCAL:
1460 variable = GetOrCreateLocalVariable(block_id, child_sym_id, is_param);
1461 if (is_param)
1462 --params_remaining;
Zachary Turner44f19512019-01-10 20:57:32 +00001463 if (variable)
1464 variables->AddVariableIfUnique(variable);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001465 break;
1466 default:
1467 break;
1468 }
1469 }
1470
1471 // Pass false for set_children, since we call this recursively so that the
1472 // children will call this for themselves.
1473 block.SetDidParseVariables(true, false);
1474
1475 return count;
1476}
1477
1478size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) {
1479 lldbassert(sc.function || sc.comp_unit);
1480
1481 VariableListSP variables;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001482 if (sc.block) {
1483 PdbSymUid block_id(sc.block->GetID());
1484
1485 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1486 return count;
1487 }
1488
1489 if (sc.function) {
1490 PdbSymUid block_id(sc.function->GetID());
1491
1492 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1493 return count;
1494 }
1495
1496 if (sc.comp_unit) {
1497 variables = sc.comp_unit->GetVariableList(false);
1498 if (!variables) {
1499 variables = std::make_shared<VariableList>();
1500 sc.comp_unit->SetVariableList(variables);
1501 }
1502 return ParseVariablesForCompileUnit(*sc.comp_unit, *variables);
1503 }
1504
1505 llvm_unreachable("Unreachable!");
1506}
1507
1508CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001509 clang::Decl *decl = m_ast->GetOrCreateDeclForUid(PdbSymUid(uid));
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001510
Zachary Turner594c85e2018-12-17 19:43:33 +00001511 return m_ast->ToCompilerDecl(*decl);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001512}
1513
1514CompilerDeclContext
1515SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001516 clang::DeclContext *context =
1517 m_ast->GetOrCreateDeclContextForUid(PdbSymUid(uid));
1518 if (!context)
1519 return {};
1520
1521 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001522}
1523
Zachary Turnera42bbe32018-12-07 19:34:02 +00001524CompilerDeclContext
1525SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001526 clang::DeclContext *context = m_ast->GetParentDeclContext(PdbSymUid(uid));
1527 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnera42bbe32018-12-07 19:34:02 +00001528}
1529
Zachary Turnerb96181c2018-10-22 16:19:07 +00001530Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001531 auto iter = m_types.find(type_uid);
1532 // lldb should not be passing us non-sensical type uids. the only way it
1533 // could have a type uid in the first place is if we handed it out, in which
Zachary Turner9f727952018-10-26 09:06:38 +00001534 // case we should know about the type. However, that doesn't mean we've
1535 // instantiated it yet. We can vend out a UID for a future type. So if the
1536 // type doesn't exist, let's instantiate it now.
1537 if (iter != m_types.end())
1538 return &*iter->second;
1539
Zachary Turner6284aee2018-11-16 02:42:32 +00001540 PdbSymUid uid(type_uid);
1541 lldbassert(uid.kind() == PdbSymUidKind::Type);
1542 PdbTypeSymId type_id = uid.asTypeSym();
1543 if (type_id.index.isNoneType())
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001544 return nullptr;
Zachary Turner9f727952018-10-26 09:06:38 +00001545
Zachary Turner6284aee2018-11-16 02:42:32 +00001546 TypeSP type_sp = CreateAndCacheType(type_id);
Zachary Turner9f727952018-10-26 09:06:38 +00001547 return &*type_sp;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001548}
1549
Adrian Prantleca07c52018-11-05 20:49:07 +00001550llvm::Optional<SymbolFile::ArrayInfo>
1551SymbolFileNativePDB::GetDynamicArrayInfoForUID(
1552 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1553 return llvm::None;
1554}
1555
1556
Zachary Turnerb96181c2018-10-22 16:19:07 +00001557bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001558 clang::QualType qt =
1559 clang::QualType::getFromOpaquePtr(compiler_type.GetOpaqueQualType());
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001560
Zachary Turner594c85e2018-12-17 19:43:33 +00001561 return m_ast->CompleteType(qt);
Zachary Turnerb96181c2018-10-22 16:19:07 +00001562}
1563
1564size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001565 TypeClass type_mask,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001566 lldb_private::TypeList &type_list) {
1567 return 0;
1568}
1569
1570CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001571SymbolFileNativePDB::FindNamespace(ConstString name,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001572 const CompilerDeclContext *parent_decl_ctx) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001573 return {};
1574}
1575
Zachary Turnerb96181c2018-10-22 16:19:07 +00001576TypeSystem *
Zachary Turner307f5ae2018-10-12 19:47:13 +00001577SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1578 auto type_system =
1579 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1580 if (type_system)
1581 type_system->SetSymbolFile(this);
1582 return type_system;
1583}
1584
Zachary Turnerb96181c2018-10-22 16:19:07 +00001585ConstString SymbolFileNativePDB::GetPluginName() {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001586 static ConstString g_name("pdb");
1587 return g_name;
1588}
1589
1590uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; }