blob: ff6fffb42d86eb19bb4d01b7ed353a95bcd9514e [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;
76 default:
77 return lldb::LanguageType::eLanguageTypeUnknown;
78 }
79}
80
81static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath,
82 llvm::BumpPtrAllocator &Allocator) {
83 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
84 llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1,
85 /*RequiresNullTerminator=*/false);
86 if (!ErrorOrBuffer)
87 return nullptr;
88 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
89
90 llvm::StringRef Path = Buffer->getBufferIdentifier();
91 auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>(
92 std::move(Buffer), llvm::support::little);
93
94 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +000095 if (auto EC = File->parseFileHeaders()) {
96 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +000097 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +000098 }
99 if (auto EC = File->parseStreamData()) {
100 llvm::consumeError(std::move(EC));
Zachary Turner307f5ae2018-10-12 19:47:13 +0000101 return nullptr;
Zachary Turner8040eea2018-10-12 22:57:40 +0000102 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000103
104 return File;
105}
106
107static std::unique_ptr<PDBFile>
108loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) {
109 // Try to find a matching PDB for an EXE.
110 using namespace llvm::object;
111 auto expected_binary = createBinary(exe_path);
112
113 // If the file isn't a PE/COFF executable, fail.
114 if (!expected_binary) {
115 llvm::consumeError(expected_binary.takeError());
116 return nullptr;
117 }
118 OwningBinary<Binary> binary = std::move(*expected_binary);
119
120 auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary());
121 if (!obj)
122 return nullptr;
123 const llvm::codeview::DebugInfo *pdb_info = nullptr;
124
125 // If it doesn't have a debug directory, fail.
126 llvm::StringRef pdb_file;
127 auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file);
128 if (ec)
129 return nullptr;
130
131 // if the file doesn't exist, is not a pdb, or doesn't have a matching guid,
132 // fail.
133 llvm::file_magic magic;
134 ec = llvm::identify_magic(pdb_file, magic);
135 if (ec || magic != llvm::file_magic::pdb)
136 return nullptr;
137 std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator);
Zachary Turner8040eea2018-10-12 22:57:40 +0000138 if (!pdb)
139 return nullptr;
140
Zachary Turner307f5ae2018-10-12 19:47:13 +0000141 auto expected_info = pdb->getPDBInfoStream();
142 if (!expected_info) {
143 llvm::consumeError(expected_info.takeError());
144 return nullptr;
145 }
146 llvm::codeview::GUID guid;
147 memcpy(&guid, pdb_info->PDB70.Signature, 16);
148
149 if (expected_info->getGuid() != guid)
150 return nullptr;
151 return pdb;
152}
153
154static bool IsFunctionPrologue(const CompilandIndexItem &cci,
155 lldb::addr_t addr) {
156 // FIXME: Implement this.
157 return false;
158}
159
160static bool IsFunctionEpilogue(const CompilandIndexItem &cci,
161 lldb::addr_t addr) {
162 // FIXME: Implement this.
163 return false;
164}
165
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000166static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) {
167 switch (kind) {
168 case SimpleTypeKind::Boolean128:
169 case SimpleTypeKind::Boolean16:
170 case SimpleTypeKind::Boolean32:
171 case SimpleTypeKind::Boolean64:
172 case SimpleTypeKind::Boolean8:
173 return "bool";
174 case SimpleTypeKind::Byte:
175 case SimpleTypeKind::UnsignedCharacter:
176 return "unsigned char";
177 case SimpleTypeKind::NarrowCharacter:
178 return "char";
179 case SimpleTypeKind::SignedCharacter:
180 case SimpleTypeKind::SByte:
Zachary Turner71ebb722018-10-23 22:15:05 +0000181 return "signed char";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000182 case SimpleTypeKind::Character16:
183 return "char16_t";
184 case SimpleTypeKind::Character32:
185 return "char32_t";
186 case SimpleTypeKind::Complex80:
187 case SimpleTypeKind::Complex64:
188 case SimpleTypeKind::Complex32:
189 return "complex";
190 case SimpleTypeKind::Float128:
191 case SimpleTypeKind::Float80:
192 return "long double";
193 case SimpleTypeKind::Float64:
194 return "double";
195 case SimpleTypeKind::Float32:
196 return "float";
197 case SimpleTypeKind::Float16:
198 return "single";
199 case SimpleTypeKind::Int128:
200 return "__int128";
201 case SimpleTypeKind::Int64:
202 case SimpleTypeKind::Int64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000203 return "int64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000204 case SimpleTypeKind::Int32:
205 return "int";
206 case SimpleTypeKind::Int16:
207 return "short";
208 case SimpleTypeKind::UInt128:
209 return "unsigned __int128";
210 case SimpleTypeKind::UInt64:
211 case SimpleTypeKind::UInt64Quad:
Zachary Turner71ebb722018-10-23 22:15:05 +0000212 return "uint64_t";
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000213 case SimpleTypeKind::HResult:
214 return "HRESULT";
215 case SimpleTypeKind::UInt32:
216 return "unsigned";
217 case SimpleTypeKind::UInt16:
218 case SimpleTypeKind::UInt16Short:
219 return "unsigned short";
220 case SimpleTypeKind::Int32Long:
221 return "long";
222 case SimpleTypeKind::UInt32Long:
223 return "unsigned long";
224 case SimpleTypeKind::Void:
225 return "void";
226 case SimpleTypeKind::WideCharacter:
227 return "wchar_t";
228 default:
229 return "";
230 }
231}
232
233static bool IsClassRecord(TypeLeafKind kind) {
234 switch (kind) {
235 case LF_STRUCTURE:
236 case LF_CLASS:
237 case LF_INTERFACE:
238 return true;
239 default:
240 return false;
241 }
242}
243
Zachary Turner307f5ae2018-10-12 19:47:13 +0000244void SymbolFileNativePDB::Initialize() {
245 PluginManager::RegisterPlugin(GetPluginNameStatic(),
246 GetPluginDescriptionStatic(), CreateInstance,
247 DebuggerInitialize);
248}
249
250void SymbolFileNativePDB::Terminate() {
251 PluginManager::UnregisterPlugin(CreateInstance);
252}
253
Zachary Turnerb96181c2018-10-22 16:19:07 +0000254void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {}
Zachary Turner307f5ae2018-10-12 19:47:13 +0000255
Zachary Turnerb96181c2018-10-22 16:19:07 +0000256ConstString SymbolFileNativePDB::GetPluginNameStatic() {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000257 static ConstString g_name("native-pdb");
258 return g_name;
259}
260
261const char *SymbolFileNativePDB::GetPluginDescriptionStatic() {
262 return "Microsoft PDB debug symbol cross-platform file reader.";
263}
264
Zachary Turnerb96181c2018-10-22 16:19:07 +0000265SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000266 return new SymbolFileNativePDB(obj_file);
267}
268
Zachary Turnerb96181c2018-10-22 16:19:07 +0000269SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000270 : SymbolFile(object_file) {}
271
272SymbolFileNativePDB::~SymbolFileNativePDB() {}
273
274uint32_t SymbolFileNativePDB::CalculateAbilities() {
275 uint32_t abilities = 0;
276 if (!m_obj_file)
277 return 0;
278
279 if (!m_index) {
280 // Lazily load and match the PDB file, but only do this once.
281 std::unique_ptr<PDBFile> file_up =
282 loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator);
283
284 if (!file_up) {
285 auto module_sp = m_obj_file->GetModule();
286 if (!module_sp)
287 return 0;
288 // See if any symbol file is specified through `--symfile` option.
289 FileSpec symfile = module_sp->GetSymbolFileFileSpec();
290 if (!symfile)
291 return 0;
292 file_up = loadPDBFile(symfile.GetPath(), m_allocator);
293 }
294
295 if (!file_up)
296 return 0;
297
298 auto expected_index = PdbIndex::create(std::move(file_up));
299 if (!expected_index) {
300 llvm::consumeError(expected_index.takeError());
301 return 0;
302 }
303 m_index = std::move(*expected_index);
304 }
305 if (!m_index)
306 return 0;
307
308 // We don't especially have to be precise here. We only distinguish between
309 // stripped and not stripped.
310 abilities = kAllAbilities;
311
312 if (m_index->dbi().isStripped())
313 abilities &= ~(Blocks | LocalVariables);
314 return abilities;
315}
316
317void SymbolFileNativePDB::InitializeObject() {
Pavel Labathd1304bb2019-02-18 11:06:57 +0000318 m_obj_load_address = m_obj_file->GetBaseAddress().GetFileAddress();
Zachary Turner307f5ae2018-10-12 19:47:13 +0000319 m_index->SetLoadAddress(m_obj_load_address);
320 m_index->ParseSectionContribs();
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000321
Zachary Turner594c85e2018-12-17 19:43:33 +0000322 TypeSystem *ts = m_obj_file->GetModule()->GetTypeSystemForLanguage(
323 lldb::eLanguageTypeC_plus_plus);
324 if (ts)
325 ts->SetSymbolFile(this);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000326
Zachary Turner594c85e2018-12-17 19:43:33 +0000327 m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index);
Zachary Turner056e4ab2018-11-08 18:50:11 +0000328}
329
Zachary Turner307f5ae2018-10-12 19:47:13 +0000330uint32_t SymbolFileNativePDB::GetNumCompileUnits() {
331 const DbiModuleList &modules = m_index->dbi().modules();
332 uint32_t count = modules.getModuleCount();
333 if (count == 0)
334 return count;
335
336 // The linker can inject an additional "dummy" compilation unit into the
337 // PDB. Ignore this special compile unit for our purposes, if it is there.
338 // It is always the last one.
339 DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1);
340 if (last.getModuleName() == "* Linker *")
341 --count;
342 return count;
343}
344
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000345Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) {
346 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
347 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000348
349 if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) {
350 // This is a function. It must be global. Creating the Function entry for
351 // it automatically creates a block for it.
352 CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii);
353 return GetOrCreateFunction(block_id, *comp_unit)->GetBlock(false);
354 }
355
356 lldbassert(sym.kind() == S_BLOCK32);
357
358 // This is a block. Its parent is either a function or another block. In
359 // either case, its parent can be viewed as a block (e.g. a function contains
360 // 1 big block. So just get the parent block and add this block to it.
361 BlockSym block(static_cast<SymbolRecordKind>(sym.kind()));
362 cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block));
363 lldbassert(block.Parent != 0);
364 PdbCompilandSymId parent_id(block_id.modi, block.Parent);
365 Block &parent_block = GetOrCreateBlock(parent_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000366 lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id);
367 BlockSP child_block = std::make_shared<Block>(opaque_block_uid);
368 parent_block.AddChild(child_block);
Zachary Turner594c85e2018-12-17 19:43:33 +0000369
370 m_ast->GetOrCreateBlockDecl(block_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000371
372 m_blocks.insert({opaque_block_uid, child_block});
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000373 return *child_block;
374}
375
Zachary Turner6284aee2018-11-16 02:42:32 +0000376lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000377 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000378 const CompilandIndexItem *cci =
379 m_index->compilands().GetCompiland(func_id.modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000380 lldbassert(cci);
Zachary Turner6284aee2018-11-16 02:42:32 +0000381 CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(func_id.offset);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000382
383 lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32);
384 SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record);
385
386 auto file_vm_addr = m_index->MakeVirtualAddress(sol.so);
387 if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
388 return nullptr;
389
390 AddressRange func_range(file_vm_addr, sol.length,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000391 comp_unit.GetModule()->GetSectionList());
Zachary Turner307f5ae2018-10-12 19:47:13 +0000392 if (!func_range.GetBaseAddress().IsValid())
393 return nullptr;
394
Zachary Turnera42bbe32018-12-07 19:34:02 +0000395 ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind()));
396 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym_record, proc));
Zachary Turner44f19512019-01-10 20:57:32 +0000397 if (proc.FunctionType == TypeIndex::None())
398 return nullptr;
Zachary Turnera42bbe32018-12-07 19:34:02 +0000399 TypeSP func_type = GetOrCreateType(proc.FunctionType);
Zachary Turner44f19512019-01-10 20:57:32 +0000400 if (!func_type)
401 return nullptr;
Zachary Turner307f5ae2018-10-12 19:47:13 +0000402
Zachary Turnera42bbe32018-12-07 19:34:02 +0000403 PdbTypeSymId sig_id(proc.FunctionType, false);
404 Mangled mangled(proc.Name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000405 FunctionSP func_sp = std::make_shared<Function>(
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000406 &comp_unit, toOpaqueUid(func_id), toOpaqueUid(sig_id), mangled,
Zachary Turnera42bbe32018-12-07 19:34:02 +0000407 func_type.get(), func_range);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000408
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000409 comp_unit.AddFunction(func_sp);
410
Zachary Turner594c85e2018-12-17 19:43:33 +0000411 m_ast->GetOrCreateFunctionDecl(func_id);
Zachary Turnera42bbe32018-12-07 19:34:02 +0000412
Zachary Turner307f5ae2018-10-12 19:47:13 +0000413 return func_sp;
414}
415
416CompUnitSP
417SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) {
418 lldb::LanguageType lang =
419 cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage())
420 : lldb::eLanguageTypeUnknown;
421
422 LazyBool optimized = eLazyBoolNo;
423 if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations())
424 optimized = eLazyBoolYes;
425
Zachary Turner51f88af2018-12-19 19:45:30 +0000426 llvm::SmallString<64> source_file_name =
Zachary Turner307f5ae2018-10-12 19:47:13 +0000427 m_index->compilands().GetMainSourceFile(cci);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000428 FileSpec fs(source_file_name);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000429
430 CompUnitSP cu_sp =
431 std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs,
Zachary Turner6284aee2018-11-16 02:42:32 +0000432 toOpaqueUid(cci.m_id), lang, optimized);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000433
Zachary Turner6284aee2018-11-16 02:42:32 +0000434 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
435 cci.m_id.modi, cu_sp);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000436 return cu_sp;
437}
438
Zachary Turner6284aee2018-11-16 02:42:32 +0000439lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000440 const ModifierRecord &mr,
441 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000442 TpiStream &stream = m_index->tpi();
443
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000444 std::string name;
445 if (mr.ModifiedType.isSimple())
446 name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind());
447 else
448 name = computeTypeName(stream.typeCollection(), mr.ModifiedType);
449 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000450 lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType);
451
452 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(name),
453 modified_type->GetByteSize(), nullptr,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000454 LLDB_INVALID_UID, Type::eEncodingIsUID, decl,
455 ct, Type::eResolveStateFull);
456}
457
Zachary Turner594c85e2018-12-17 19:43:33 +0000458lldb::TypeSP
459SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id,
460 const llvm::codeview::PointerRecord &pr,
461 CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000462 TypeSP pointee = GetOrCreateType(pr.ReferentType);
Zachary Turner544a66d82018-11-01 16:37:29 +0000463 if (!pointee)
464 return nullptr;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000465
466 if (pr.isPointerToMember()) {
467 MemberPointerInfo mpi = pr.getMemberInfo();
Zachary Turner594c85e2018-12-17 19:43:33 +0000468 GetOrCreateType(mpi.ContainingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000469 }
470
Zachary Turner594c85e2018-12-17 19:43:33 +0000471 Declaration decl;
472 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(),
473 pr.getSize(), nullptr, LLDB_INVALID_UID,
474 Type::eEncodingIsUID, decl, ct,
475 Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000476}
477
Zachary Turner594c85e2018-12-17 19:43:33 +0000478lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti,
479 CompilerType ct) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000480 uint64_t uid = toOpaqueUid(PdbTypeSymId(ti, false));
Zachary Turner544a66d82018-11-01 16:37:29 +0000481 if (ti == TypeIndex::NullptrT()) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000482 Declaration decl;
Zachary Turner6284aee2018-11-16 02:42:32 +0000483 return std::make_shared<Type>(
484 uid, this, ConstString("std::nullptr_t"), 0, nullptr, LLDB_INVALID_UID,
485 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner544a66d82018-11-01 16:37:29 +0000486 }
487
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000488 if (ti.getSimpleMode() != SimpleTypeMode::Direct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000489 TypeSP direct_sp = GetOrCreateType(ti.makeDirect());
Zachary Turner71ebb722018-10-23 22:15:05 +0000490 uint32_t pointer_size = 0;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000491 switch (ti.getSimpleMode()) {
492 case SimpleTypeMode::FarPointer32:
493 case SimpleTypeMode::NearPointer32:
494 pointer_size = 4;
495 break;
496 case SimpleTypeMode::NearPointer64:
497 pointer_size = 8;
498 break;
499 default:
500 // 128-bit and 16-bit pointers unsupported.
501 return nullptr;
502 }
503 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000504 return std::make_shared<Type>(
505 uid, this, ConstString(), pointer_size, nullptr, LLDB_INVALID_UID,
506 Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000507 }
508
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000509 if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated)
510 return nullptr;
511
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000512 size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind());
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000513 llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind());
514
515 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000516 return std::make_shared<Type>(uid, this, ConstString(type_name), size,
517 nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID,
518 decl, ct, Type::eResolveStateFull);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000519}
520
Zachary Turner594c85e2018-12-17 19:43:33 +0000521static std::string GetUnqualifiedTypeName(const TagRecord &record) {
Zachary Turner44f19512019-01-10 20:57:32 +0000522 if (!record.hasUniqueName()) {
523 MSVCUndecoratedNameParser parser(record.Name);
524 llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers();
525
526 return specs.back().GetBaseName();
527 }
528
Zachary Turner056e4ab2018-11-08 18:50:11 +0000529 llvm::ms_demangle::Demangler demangler;
530 StringView sv(record.UniqueName.begin(), record.UniqueName.size());
531 llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv);
Zachary Turner44f19512019-01-10 20:57:32 +0000532 if (demangler.Error)
533 return record.Name;
534
Zachary Turner056e4ab2018-11-08 18:50:11 +0000535 llvm::ms_demangle::IdentifierNode *idn =
536 ttn->QualifiedName->getUnqualifiedIdentifier();
Zachary Turner594c85e2018-12-17 19:43:33 +0000537 return idn->toString();
Zachary Turner056e4ab2018-11-08 18:50:11 +0000538}
539
Zachary Turner594c85e2018-12-17 19:43:33 +0000540lldb::TypeSP
541SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id,
542 const TagRecord &record,
543 size_t size, CompilerType ct) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000544
Zachary Turner594c85e2018-12-17 19:43:33 +0000545 std::string uname = GetUnqualifiedTypeName(record);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000546
547 // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE.
548 Declaration decl;
Zachary Turner594c85e2018-12-17 19:43:33 +0000549 return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(uname),
550 size, nullptr, LLDB_INVALID_UID,
551 Type::eEncodingIsUID, decl, ct,
552 Type::eResolveStateForward);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000553}
554
Zachary Turner6284aee2018-11-16 02:42:32 +0000555lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000556 const ClassRecord &cr,
557 CompilerType ct) {
558 return CreateClassStructUnion(type_id, cr, cr.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000559}
560
Zachary Turner6284aee2018-11-16 02:42:32 +0000561lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000562 const UnionRecord &ur,
563 CompilerType ct) {
564 return CreateClassStructUnion(type_id, ur, ur.getSize(), ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000565}
566
Zachary Turner6284aee2018-11-16 02:42:32 +0000567lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000568 const EnumRecord &er,
569 CompilerType ct) {
570 std::string uname = GetUnqualifiedTypeName(er);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000571
572 Declaration decl;
573 TypeSP underlying_type = GetOrCreateType(er.UnderlyingType);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000574
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000575 return std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000576 toOpaqueUid(type_id), this, ConstString(uname),
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000577 underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000578 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000579 lldb_private::Type::eResolveStateForward);
580}
581
Zachary Turner6284aee2018-11-16 02:42:32 +0000582TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000583 const ArrayRecord &ar,
584 CompilerType ct) {
Zachary Turner511bff22018-10-30 18:57:08 +0000585 TypeSP element_type = GetOrCreateType(ar.ElementType);
Zachary Turner511bff22018-10-30 18:57:08 +0000586
587 Declaration decl;
588 TypeSP array_sp = std::make_shared<lldb_private::Type>(
Zachary Turner594c85e2018-12-17 19:43:33 +0000589 toOpaqueUid(type_id), this, ConstString(), ar.Size, nullptr,
590 LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct,
591 lldb_private::Type::eResolveStateFull);
Zachary Turner511bff22018-10-30 18:57:08 +0000592 array_sp->SetEncodingType(element_type.get());
593 return array_sp;
594}
595
Zachary Turner6284aee2018-11-16 02:42:32 +0000596TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id,
Zachary Turner594c85e2018-12-17 19:43:33 +0000597 const ProcedureRecord &pr,
598 CompilerType ct) {
Zachary Turner544a66d82018-11-01 16:37:29 +0000599 Declaration decl;
600 return std::make_shared<lldb_private::Type>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000601 toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID,
Zachary Turner594c85e2018-12-17 19:43:33 +0000602 lldb_private::Type::eEncodingIsUID, decl, ct,
Zachary Turner544a66d82018-11-01 16:37:29 +0000603 lldb_private::Type::eResolveStateFull);
604}
605
Zachary Turner594c85e2018-12-17 19:43:33 +0000606TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000607 if (type_id.index.isSimple())
Zachary Turner594c85e2018-12-17 19:43:33 +0000608 return CreateSimpleType(type_id.index, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000609
Zachary Turner6284aee2018-11-16 02:42:32 +0000610 TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi();
611 CVType cvt = stream.getType(type_id.index);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000612
613 if (cvt.kind() == LF_MODIFIER) {
614 ModifierRecord modifier;
615 llvm::cantFail(
616 TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier));
Zachary Turner594c85e2018-12-17 19:43:33 +0000617 return CreateModifierType(type_id, modifier, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000618 }
619
620 if (cvt.kind() == LF_POINTER) {
621 PointerRecord pointer;
622 llvm::cantFail(
623 TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer));
Zachary Turner594c85e2018-12-17 19:43:33 +0000624 return CreatePointerType(type_id, pointer, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000625 }
626
627 if (IsClassRecord(cvt.kind())) {
628 ClassRecord cr;
629 llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000630 return CreateTagType(type_id, cr, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000631 }
632
633 if (cvt.kind() == LF_ENUM) {
634 EnumRecord er;
635 llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er));
Zachary Turner594c85e2018-12-17 19:43:33 +0000636 return CreateTagType(type_id, er, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000637 }
638
639 if (cvt.kind() == LF_UNION) {
640 UnionRecord ur;
641 llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur));
Zachary Turner594c85e2018-12-17 19:43:33 +0000642 return CreateTagType(type_id, ur, ct);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000643 }
644
Zachary Turner511bff22018-10-30 18:57:08 +0000645 if (cvt.kind() == LF_ARRAY) {
646 ArrayRecord ar;
647 llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar));
Zachary Turner594c85e2018-12-17 19:43:33 +0000648 return CreateArrayType(type_id, ar, ct);
Zachary Turner511bff22018-10-30 18:57:08 +0000649 }
650
Zachary Turner544a66d82018-11-01 16:37:29 +0000651 if (cvt.kind() == LF_PROCEDURE) {
652 ProcedureRecord pr;
653 llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr));
Zachary Turner594c85e2018-12-17 19:43:33 +0000654 return CreateProcedureType(type_id, pr, ct);
Zachary Turner544a66d82018-11-01 16:37:29 +0000655 }
656
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000657 return nullptr;
658}
659
Zachary Turner6284aee2018-11-16 02:42:32 +0000660TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000661 // If they search for a UDT which is a forward ref, try and resolve the full
662 // decl and just map the forward ref uid to the full decl record.
Zachary Turner6284aee2018-11-16 02:42:32 +0000663 llvm::Optional<PdbTypeSymId> full_decl_uid;
664 if (IsForwardRefUdt(type_id, m_index->tpi())) {
665 auto expected_full_ti =
666 m_index->tpi().findFullDeclForForwardRef(type_id.index);
667 if (!expected_full_ti)
668 llvm::consumeError(expected_full_ti.takeError());
669 else if (*expected_full_ti != type_id.index) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000670 full_decl_uid = PdbTypeSymId(*expected_full_ti, false);
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000671
Zachary Turner6284aee2018-11-16 02:42:32 +0000672 // It's possible that a lookup would occur for the full decl causing it
673 // to be cached, then a second lookup would occur for the forward decl.
674 // We don't want to create a second full decl, so make sure the full
675 // decl hasn't already been cached.
676 auto full_iter = m_types.find(toOpaqueUid(*full_decl_uid));
677 if (full_iter != m_types.end()) {
678 TypeSP result = full_iter->second;
679 // Map the forward decl to the TypeSP for the full decl so we can take
680 // the fast path next time.
681 m_types[toOpaqueUid(type_id)] = result;
682 return result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000683 }
684 }
685 }
686
Zachary Turner6284aee2018-11-16 02:42:32 +0000687 PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id;
Zachary Turner594c85e2018-12-17 19:43:33 +0000688
689 clang::QualType qt = m_ast->GetOrCreateType(best_decl_id);
690
691 TypeSP result = CreateType(best_decl_id, m_ast->ToCompilerType(qt));
Zachary Turner544a66d82018-11-01 16:37:29 +0000692 if (!result)
693 return nullptr;
Zachary Turner6284aee2018-11-16 02:42:32 +0000694
695 uint64_t best_uid = toOpaqueUid(best_decl_id);
696 m_types[best_uid] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000697 // If we had both a forward decl and a full decl, make both point to the new
698 // type.
699 if (full_decl_uid)
Zachary Turner6284aee2018-11-16 02:42:32 +0000700 m_types[toOpaqueUid(type_id)] = result;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000701
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000702 return result;
703}
704
Zachary Turner6284aee2018-11-16 02:42:32 +0000705TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000706 // We can't use try_emplace / overwrite here because the process of creating
707 // a type could create nested types, which could invalidate iterators. So
708 // we have to do a 2-phase lookup / insert.
Zachary Turner6284aee2018-11-16 02:42:32 +0000709 auto iter = m_types.find(toOpaqueUid(type_id));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000710 if (iter != m_types.end())
711 return iter->second;
712
Zachary Turner44f19512019-01-10 20:57:32 +0000713 TypeSP type = CreateAndCacheType(type_id);
714 if (type)
715 m_obj_file->GetModule()->GetTypeList()->Insert(type);
716 return type;
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000717}
718
Zachary Turner6284aee2018-11-16 02:42:32 +0000719VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) {
720 CVSymbol sym = m_index->symrecords().readRecord(var_id.offset);
Zachary Turner2af34162018-11-13 20:07:57 +0000721 if (sym.kind() == S_CONSTANT)
Zachary Turner6284aee2018-11-16 02:42:32 +0000722 return CreateConstantSymbol(var_id, sym);
Zachary Turner2af34162018-11-13 20:07:57 +0000723
Zachary Turner9f727952018-10-26 09:06:38 +0000724 lldb::ValueType scope = eValueTypeInvalid;
725 TypeIndex ti;
726 llvm::StringRef name;
727 lldb::addr_t addr = 0;
728 uint16_t section = 0;
729 uint32_t offset = 0;
730 bool is_external = false;
731 switch (sym.kind()) {
732 case S_GDATA32:
733 is_external = true;
734 LLVM_FALLTHROUGH;
735 case S_LDATA32: {
736 DataSym ds(sym.kind());
737 llvm::cantFail(SymbolDeserializer::deserializeAs<DataSym>(sym, ds));
738 ti = ds.Type;
739 scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal
740 : eValueTypeVariableStatic;
741 name = ds.Name;
742 section = ds.Segment;
743 offset = ds.DataOffset;
744 addr = m_index->MakeVirtualAddress(ds.Segment, ds.DataOffset);
745 break;
746 }
747 case S_GTHREAD32:
748 is_external = true;
749 LLVM_FALLTHROUGH;
750 case S_LTHREAD32: {
751 ThreadLocalDataSym tlds(sym.kind());
752 llvm::cantFail(
753 SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(sym, tlds));
754 ti = tlds.Type;
755 name = tlds.Name;
756 section = tlds.Segment;
757 offset = tlds.DataOffset;
758 addr = m_index->MakeVirtualAddress(tlds.Segment, tlds.DataOffset);
759 scope = eValueTypeVariableThreadLocal;
760 break;
761 }
762 default:
763 llvm_unreachable("unreachable!");
764 }
765
766 CompUnitSP comp_unit;
767 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr);
768 if (modi) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000769 CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi);
Zachary Turner9f727952018-10-26 09:06:38 +0000770 comp_unit = GetOrCreateCompileUnit(cci);
771 }
772
773 Declaration decl;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000774 PdbTypeSymId tid(ti, false);
Zachary Turner9f727952018-10-26 09:06:38 +0000775 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000776 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner9f727952018-10-26 09:06:38 +0000777 Variable::RangeList ranges;
778
Zachary Turner22566332019-01-02 18:33:54 +0000779 m_ast->GetOrCreateVariableDecl(var_id);
Zachary Turner37900292018-12-20 23:32:37 +0000780
Zachary Turner9f727952018-10-26 09:06:38 +0000781 DWARFExpression location = MakeGlobalLocationExpression(
782 section, offset, GetObjectFile()->GetModule());
783
784 std::string global_name("::");
785 global_name += name;
786 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000787 toOpaqueUid(var_id), name.str().c_str(), global_name.c_str(), type_sp,
Zachary Turner9f727952018-10-26 09:06:38 +0000788 scope, comp_unit.get(), ranges, &decl, location, is_external, false,
789 false);
790 var_sp->SetLocationIsConstantValueData(false);
791
792 return var_sp;
793}
794
Zachary Turner2af34162018-11-13 20:07:57 +0000795lldb::VariableSP
Zachary Turner6284aee2018-11-16 02:42:32 +0000796SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id,
Zachary Turner2af34162018-11-13 20:07:57 +0000797 const CVSymbol &cvs) {
798 TpiStream &tpi = m_index->tpi();
799 ConstantSym constant(cvs.kind());
800
801 llvm::cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(cvs, constant));
802 std::string global_name("::");
803 global_name += constant.Name;
Zachary Turner9fbf9352018-11-16 03:16:27 +0000804 PdbTypeSymId tid(constant.Type, false);
Zachary Turner2af34162018-11-13 20:07:57 +0000805 SymbolFileTypeSP type_sp =
Zachary Turner6284aee2018-11-16 02:42:32 +0000806 std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid));
Zachary Turner2af34162018-11-13 20:07:57 +0000807
808 Declaration decl;
809 Variable::RangeList ranges;
810 ModuleSP module = GetObjectFile()->GetModule();
Zachary Turnera93458b2018-12-06 17:49:15 +0000811 DWARFExpression location = MakeConstantLocationExpression(
812 constant.Type, tpi, constant.Value, module);
Zachary Turner2af34162018-11-13 20:07:57 +0000813
814 VariableSP var_sp = std::make_shared<Variable>(
Zachary Turner6284aee2018-11-16 02:42:32 +0000815 toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(),
Zachary Turner2af34162018-11-13 20:07:57 +0000816 type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location,
817 false, false, false);
818 var_sp->SetLocationIsConstantValueData(true);
819 return var_sp;
820}
821
Zachary Turner6284aee2018-11-16 02:42:32 +0000822VariableSP
823SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) {
824 auto emplace_result = m_global_vars.try_emplace(toOpaqueUid(var_id), nullptr);
Zachary Turner9f727952018-10-26 09:06:38 +0000825 if (emplace_result.second)
Zachary Turner6284aee2018-11-16 02:42:32 +0000826 emplace_result.first->second = CreateGlobalVariable(var_id);
Zachary Turner9f727952018-10-26 09:06:38 +0000827
828 return emplace_result.first->second;
829}
830
Zachary Turner6284aee2018-11-16 02:42:32 +0000831lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) {
Zachary Turner9fbf9352018-11-16 03:16:27 +0000832 return GetOrCreateType(PdbTypeSymId(ti, false));
Zachary Turner2f7efbc2018-10-23 16:37:53 +0000833}
834
Zachary Turner6284aee2018-11-16 02:42:32 +0000835FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id,
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000836 CompileUnit &comp_unit) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000837 auto emplace_result = m_functions.try_emplace(toOpaqueUid(func_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000838 if (emplace_result.second)
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000839 emplace_result.first->second = CreateFunction(func_id, comp_unit);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000840
Zachary Turner307f5ae2018-10-12 19:47:13 +0000841 return emplace_result.first->second;
842}
843
844CompUnitSP
845SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000846
Zachary Turner307f5ae2018-10-12 19:47:13 +0000847 auto emplace_result =
Zachary Turner6284aee2018-11-16 02:42:32 +0000848 m_compilands.try_emplace(toOpaqueUid(cci.m_id), nullptr);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000849 if (emplace_result.second)
850 emplace_result.first->second = CreateCompileUnit(cci);
851
852 lldbassert(emplace_result.first->second);
853 return emplace_result.first->second;
854}
855
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000856Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) {
857 auto iter = m_blocks.find(toOpaqueUid(block_id));
858 if (iter != m_blocks.end())
859 return *iter->second;
860
861 return CreateBlock(block_id);
862}
863
Zachary Turner22566332019-01-02 18:33:54 +0000864void SymbolFileNativePDB::ParseDeclsForContext(
865 lldb_private::CompilerDeclContext decl_ctx) {
866 clang::DeclContext *context = m_ast->FromCompilerDeclContext(decl_ctx);
867 if (!context)
868 return;
869 m_ast->ParseDeclsForContext(*context);
870}
871
Zachary Turner307f5ae2018-10-12 19:47:13 +0000872lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) {
873 if (index >= GetNumCompileUnits())
874 return CompUnitSP();
875 lldbassert(index < UINT16_MAX);
876 if (index >= UINT16_MAX)
877 return nullptr;
878
879 CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index);
880
881 return GetOrCreateCompileUnit(item);
882}
883
Zachary Turner863f8c12019-01-11 18:03:20 +0000884lldb::LanguageType SymbolFileNativePDB::ParseLanguage(CompileUnit &comp_unit) {
885 PdbSymUid uid(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +0000886 lldbassert(uid.kind() == PdbSymUidKind::Compiland);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000887
Zachary Turner6284aee2018-11-16 02:42:32 +0000888 CompilandIndexItem *item =
889 m_index->compilands().GetCompiland(uid.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000890 lldbassert(item);
891 if (!item->m_compile_opts)
892 return lldb::eLanguageTypeUnknown;
893
894 return TranslateLanguage(item->m_compile_opts->getLanguage());
895}
896
Zachary Turnerb3130b42019-01-02 18:32:50 +0000897void SymbolFileNativePDB::AddSymbols(Symtab &symtab) { return; }
898
Zachary Turner863f8c12019-01-11 18:03:20 +0000899size_t SymbolFileNativePDB::ParseFunctions(CompileUnit &comp_unit) {
900 PdbSymUid uid{comp_unit.GetID()};
Zachary Turner44f19512019-01-10 20:57:32 +0000901 lldbassert(uid.kind() == PdbSymUidKind::Compiland);
902 uint16_t modi = uid.asCompiland().modi;
903 CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi);
904
Zachary Turner863f8c12019-01-11 18:03:20 +0000905 size_t count = comp_unit.GetNumFunctions();
Zachary Turner44f19512019-01-10 20:57:32 +0000906 const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray();
907 for (auto iter = syms.begin(); iter != syms.end(); ++iter) {
908 if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32)
909 continue;
910
911 PdbCompilandSymId sym_id{modi, iter.offset()};
912
Zachary Turner863f8c12019-01-11 18:03:20 +0000913 FunctionSP func = GetOrCreateFunction(sym_id, comp_unit);
Zachary Turner44f19512019-01-10 20:57:32 +0000914 }
915
Zachary Turner863f8c12019-01-11 18:03:20 +0000916 size_t new_count = comp_unit.GetNumFunctions();
Zachary Turner44f19512019-01-10 20:57:32 +0000917 lldbassert(new_count >= count);
918 return new_count - count;
Zachary Turner307f5ae2018-10-12 19:47:13 +0000919}
920
921static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) {
922 // If any of these flags are set, we need to resolve the compile unit.
923 uint32_t flags = eSymbolContextCompUnit;
924 flags |= eSymbolContextVariable;
925 flags |= eSymbolContextFunction;
926 flags |= eSymbolContextBlock;
927 flags |= eSymbolContextLineEntry;
928 return (resolve_scope & flags) != 0;
929}
930
Zachary Turner991e4452018-10-25 20:45:19 +0000931uint32_t SymbolFileNativePDB::ResolveSymbolContext(
932 const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000933 uint32_t resolved_flags = 0;
934 lldb::addr_t file_addr = addr.GetFileAddress();
935
936 if (NeedsResolvedCompileUnit(resolve_scope)) {
937 llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr);
938 if (!modi)
939 return 0;
Zachary Turner6284aee2018-11-16 02:42:32 +0000940 CompilandIndexItem *cci = m_index->compilands().GetCompiland(*modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +0000941 if (!cci)
942 return 0;
943
944 sc.comp_unit = GetOrCreateCompileUnit(*cci).get();
945 resolved_flags |= eSymbolContextCompUnit;
946 }
947
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000948 if (resolve_scope & eSymbolContextFunction ||
949 resolve_scope & eSymbolContextBlock) {
Zachary Turner307f5ae2018-10-12 19:47:13 +0000950 lldbassert(sc.comp_unit);
951 std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000952 // Search the matches in reverse. This way if there are multiple matches
953 // (for example we are 3 levels deep in a nested scope) it will find the
954 // innermost one first.
955 for (const auto &match : llvm::reverse(matches)) {
Zachary Turner6284aee2018-11-16 02:42:32 +0000956 if (match.uid.kind() != PdbSymUidKind::CompilandSym)
Zachary Turner307f5ae2018-10-12 19:47:13 +0000957 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000958
Zachary Turner6284aee2018-11-16 02:42:32 +0000959 PdbCompilandSymId csid = match.uid.asCompilandSym();
960 CVSymbol cvs = m_index->ReadSymbolRecord(csid);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000961 PDB_SymType type = CVSymToPDBSym(cvs.kind());
962 if (type != PDB_SymType::Function && type != PDB_SymType::Block)
Zachary Turner6284aee2018-11-16 02:42:32 +0000963 continue;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000964 if (type == PDB_SymType::Function) {
965 sc.function = GetOrCreateFunction(csid, *sc.comp_unit).get();
966 sc.block = sc.GetFunctionBlock();
967 }
968
969 if (type == PDB_SymType::Block) {
970 sc.block = &GetOrCreateBlock(csid);
971 sc.function = sc.block->CalculateSymbolContextFunction();
972 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000973 resolved_flags |= eSymbolContextFunction;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +0000974 resolved_flags |= eSymbolContextBlock;
975 break;
976 }
Zachary Turner307f5ae2018-10-12 19:47:13 +0000977 }
978
979 if (resolve_scope & eSymbolContextLineEntry) {
980 lldbassert(sc.comp_unit);
981 if (auto *line_table = sc.comp_unit->GetLineTable()) {
982 if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
983 resolved_flags |= eSymbolContextLineEntry;
984 }
985 }
986
987 return resolved_flags;
988}
989
Zachary Turnerb3130b42019-01-02 18:32:50 +0000990uint32_t SymbolFileNativePDB::ResolveSymbolContext(
991 const FileSpec &file_spec, uint32_t line, bool check_inlines,
992 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) {
993 return 0;
994}
995
Zachary Turner307f5ae2018-10-12 19:47:13 +0000996static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence,
997 const CompilandIndexItem &cci,
998 lldb::addr_t base_addr,
999 uint32_t file_number,
1000 const LineFragmentHeader &block,
1001 const LineNumberEntry &cur) {
1002 LineInfo cur_info(cur.Flags);
1003
1004 if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto())
1005 return;
1006
1007 uint64_t addr = base_addr + cur.Offset;
1008
1009 bool is_statement = cur_info.isStatement();
1010 bool is_prologue = IsFunctionPrologue(cci, addr);
1011 bool is_epilogue = IsFunctionEpilogue(cci, addr);
1012
1013 uint32_t lno = cur_info.getStartLine();
1014
1015 table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number,
1016 is_statement, false, is_prologue, is_epilogue,
1017 false);
1018}
1019
1020static void TerminateLineSequence(LineTable &table,
1021 const LineFragmentHeader &block,
1022 lldb::addr_t base_addr, uint32_t file_number,
1023 uint32_t last_line,
1024 std::unique_ptr<LineSequence> seq) {
1025 // The end is always a terminal entry, so insert it regardless.
1026 table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize,
1027 last_line, 0, file_number, false, false,
1028 false, false, true);
1029 table.InsertSequence(seq.release());
1030}
1031
Zachary Turner863f8c12019-01-11 18:03:20 +00001032bool SymbolFileNativePDB::ParseLineTable(CompileUnit &comp_unit) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001033 // Unfortunately LLDB is set up to parse the entire compile unit line table
1034 // all at once, even if all it really needs is line info for a specific
1035 // function. In the future it would be nice if it could set the sc.m_function
1036 // member, and we could only get the line info for the function in question.
Zachary Turner863f8c12019-01-11 18:03:20 +00001037 PdbSymUid cu_id(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +00001038 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1039 CompilandIndexItem *cci =
1040 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001041 lldbassert(cci);
Zachary Turner863f8c12019-01-11 18:03:20 +00001042 auto line_table = llvm::make_unique<LineTable>(&comp_unit);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001043
1044 // This is basically a copy of the .debug$S subsections from all original COFF
1045 // object files merged together with address relocations applied. We are
1046 // looking for all DEBUG_S_LINES subsections.
1047 for (const DebugSubsectionRecord &dssr :
1048 cci->m_debug_stream.getSubsectionsArray()) {
1049 if (dssr.kind() != DebugSubsectionKind::Lines)
1050 continue;
1051
1052 DebugLinesSubsectionRef lines;
1053 llvm::BinaryStreamReader reader(dssr.getRecordData());
1054 if (auto EC = lines.initialize(reader)) {
1055 llvm::consumeError(std::move(EC));
1056 return false;
1057 }
1058
1059 const LineFragmentHeader *lfh = lines.header();
1060 uint64_t virtual_addr =
1061 m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset);
1062
1063 const auto &checksums = cci->m_strings.checksums().getArray();
1064 const auto &strings = cci->m_strings.strings();
1065 for (const LineColumnEntry &group : lines) {
1066 // Indices in this structure are actually offsets of records in the
1067 // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index
1068 // into the global PDB string table.
1069 auto iter = checksums.at(group.NameIndex);
1070 if (iter == checksums.end())
1071 continue;
1072
1073 llvm::Expected<llvm::StringRef> efn =
1074 strings.getString(iter->FileNameOffset);
1075 if (!efn) {
1076 llvm::consumeError(efn.takeError());
1077 continue;
1078 }
1079
1080 // LLDB wants the index of the file in the list of support files.
1081 auto fn_iter = llvm::find(cci->m_file_list, *efn);
1082 lldbassert(fn_iter != cci->m_file_list.end());
Zachary Turnerb3130b42019-01-02 18:32:50 +00001083 // LLDB support file indices are 1-based.
1084 uint32_t file_index =
1085 1 + std::distance(cci->m_file_list.begin(), fn_iter);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001086
1087 std::unique_ptr<LineSequence> sequence(
1088 line_table->CreateLineSequenceContainer());
1089 lldbassert(!group.LineNumbers.empty());
1090
1091 for (const LineNumberEntry &entry : group.LineNumbers) {
1092 AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr,
1093 file_index, *lfh, entry);
1094 }
1095 LineInfo last_line(group.LineNumbers.back().Flags);
1096 TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index,
1097 last_line.getEndLine(), std::move(sequence));
1098 }
1099 }
1100
1101 if (line_table->GetSize() == 0)
1102 return false;
1103
Zachary Turner863f8c12019-01-11 18:03:20 +00001104 comp_unit.SetLineTable(line_table.release());
Zachary Turner307f5ae2018-10-12 19:47:13 +00001105 return true;
1106}
1107
Zachary Turner863f8c12019-01-11 18:03:20 +00001108bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit &comp_unit) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001109 // PDB doesn't contain information about macros
1110 return false;
1111}
1112
Zachary Turner863f8c12019-01-11 18:03:20 +00001113bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit &comp_unit,
1114 FileSpecList &support_files) {
1115 PdbSymUid cu_id(comp_unit.GetID());
Zachary Turner6284aee2018-11-16 02:42:32 +00001116 lldbassert(cu_id.kind() == PdbSymUidKind::Compiland);
1117 CompilandIndexItem *cci =
1118 m_index->compilands().GetCompiland(cu_id.asCompiland().modi);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001119 lldbassert(cci);
1120
1121 for (llvm::StringRef f : cci->m_file_list) {
1122 FileSpec::Style style =
1123 f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows;
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +00001124 FileSpec spec(f, style);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001125 support_files.Append(spec);
1126 }
1127
Zachary Turnerb3130b42019-01-02 18:32:50 +00001128 llvm::SmallString<64> main_source_file =
1129 m_index->compilands().GetMainSourceFile(*cci);
1130 FileSpec::Style style = main_source_file.startswith("/")
1131 ? FileSpec::Style::posix
1132 : FileSpec::Style::windows;
1133 FileSpec spec(main_source_file, style);
1134 support_files.Insert(0, spec);
Zachary Turner307f5ae2018-10-12 19:47:13 +00001135 return true;
1136}
1137
1138bool SymbolFileNativePDB::ParseImportedModules(
Adrian Prantl0f30a3b2019-02-13 18:10:41 +00001139 const SymbolContext &sc, std::vector<SourceModule> &imported_modules) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001140 // PDB does not yet support module debug info
1141 return false;
1142}
1143
Zachary Turnerffc1b8f2019-01-14 22:40:41 +00001144size_t SymbolFileNativePDB::ParseBlocksRecursive(Function &func) {
1145 GetOrCreateBlock(PdbSymUid(func.GetID()).asCompilandSym());
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001146 // FIXME: Parse child blocks
1147 return 1;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001148}
1149
Zachary Turner594c85e2018-12-17 19:43:33 +00001150void SymbolFileNativePDB::DumpClangAST(Stream &s) { m_ast->Dump(s); }
Zachary Turner49110232018-11-05 17:40:28 +00001151
Zachary Turner9f727952018-10-26 09:06:38 +00001152uint32_t SymbolFileNativePDB::FindGlobalVariables(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001153 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner9f727952018-10-26 09:06:38 +00001154 uint32_t max_matches, VariableList &variables) {
1155 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1156
1157 std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName(
1158 name.GetStringRef(), m_index->symrecords());
1159 for (const SymbolAndOffset &result : results) {
1160 VariableSP var;
1161 switch (result.second.kind()) {
1162 case SymbolKind::S_GDATA32:
1163 case SymbolKind::S_LDATA32:
1164 case SymbolKind::S_GTHREAD32:
Zachary Turner2af34162018-11-13 20:07:57 +00001165 case SymbolKind::S_LTHREAD32:
1166 case SymbolKind::S_CONSTANT: {
Zachary Turner9fbf9352018-11-16 03:16:27 +00001167 PdbGlobalSymId global(result.first, false);
Zachary Turner6284aee2018-11-16 02:42:32 +00001168 var = GetOrCreateGlobalVariable(global);
Zachary Turner9f727952018-10-26 09:06:38 +00001169 variables.AddVariable(var);
1170 break;
1171 }
1172 default:
1173 continue;
1174 }
1175 }
1176 return variables.GetSize();
1177}
1178
Zachary Turner307f5ae2018-10-12 19:47:13 +00001179uint32_t SymbolFileNativePDB::FindFunctions(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001180 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001181 FunctionNameType name_type_mask, bool include_inlines, bool append,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001182 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001183 // For now we only support lookup by method name.
1184 if (!(name_type_mask & eFunctionNameTypeMethod))
1185 return 0;
1186
1187 using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>;
1188
1189 std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName(
1190 name.GetStringRef(), m_index->symrecords());
1191 for (const SymbolAndOffset &match : matches) {
1192 if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF)
1193 continue;
1194 ProcRefSym proc(match.second.kind());
1195 cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc));
1196
1197 if (!IsValidRecord(proc))
1198 continue;
1199
Zachary Turner6284aee2018-11-16 02:42:32 +00001200 CompilandIndexItem &cci =
1201 m_index->compilands().GetOrCreateCompiland(proc.modi());
Zachary Turnerb96181c2018-10-22 16:19:07 +00001202 SymbolContext sc;
Zachary Turner307f5ae2018-10-12 19:47:13 +00001203
1204 sc.comp_unit = GetOrCreateCompileUnit(cci).get();
Zachary Turner9fbf9352018-11-16 03:16:27 +00001205 PdbCompilandSymId func_id(proc.modi(), proc.SymOffset);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001206 sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get();
Zachary Turner307f5ae2018-10-12 19:47:13 +00001207
1208 sc_list.Append(sc);
1209 }
1210
1211 return sc_list.GetSize();
1212}
1213
Zachary Turnerb96181c2018-10-22 16:19:07 +00001214uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression &regex,
1215 bool include_inlines, bool append,
1216 SymbolContextList &sc_list) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001217 return 0;
1218}
1219
Zachary Turnerb96181c2018-10-22 16:19:07 +00001220uint32_t SymbolFileNativePDB::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001221 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +00001222 bool append, uint32_t max_matches,
1223 llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001224 if (!append)
1225 types.Clear();
1226 if (!name)
1227 return 0;
1228
1229 searched_symbol_files.clear();
1230 searched_symbol_files.insert(this);
1231
1232 // There is an assumption 'name' is not a regex
1233 size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types);
1234
1235 return match_count;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001236}
1237
1238size_t
1239SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context,
1240 bool append, TypeMap &types) {
1241 return 0;
1242}
1243
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001244size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name,
1245 uint32_t max_matches,
1246 TypeMap &types) {
1247
1248 size_t match_count = 0;
1249 std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name);
1250 if (max_matches > 0 && max_matches < matches.size())
1251 matches.resize(max_matches);
1252
1253 for (TypeIndex ti : matches) {
1254 TypeSP type = GetOrCreateType(ti);
1255 if (!type)
1256 continue;
1257
1258 types.Insert(type);
1259 ++match_count;
1260 }
1261 return match_count;
1262}
1263
Zachary Turner863f8c12019-01-11 18:03:20 +00001264size_t SymbolFileNativePDB::ParseTypes(CompileUnit &comp_unit) {
Zachary Turner44f19512019-01-10 20:57:32 +00001265 // Only do the full type scan the first time.
1266 if (m_done_full_type_scan)
1267 return 0;
1268
1269 size_t old_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1270 LazyRandomTypeCollection &types = m_index->tpi().typeCollection();
1271
1272 // First process the entire TPI stream.
1273 for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) {
1274 TypeSP type = GetOrCreateType(*ti);
1275 if (type)
1276 (void)type->GetFullCompilerType();
1277 }
1278
1279 // Next look for S_UDT records in the globals stream.
1280 for (const uint32_t gid : m_index->globals().getGlobalsTable()) {
1281 PdbGlobalSymId global{gid, false};
1282 CVSymbol sym = m_index->ReadSymbolRecord(global);
1283 if (sym.kind() != S_UDT)
1284 continue;
1285
1286 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1287 bool is_typedef = true;
1288 if (IsTagRecord(PdbTypeSymId{udt.Type, false}, m_index->tpi())) {
1289 CVType cvt = m_index->tpi().getType(udt.Type);
1290 llvm::StringRef name = CVTagRecord::create(cvt).name();
1291 if (name == udt.Name)
1292 is_typedef = false;
1293 }
1294
1295 if (is_typedef)
1296 GetOrCreateTypedef(global);
1297 }
1298
1299 size_t new_count = m_obj_file->GetModule()->GetTypeList()->GetSize();
1300
1301 m_done_full_type_scan = true;
1302
1303 return new_count - old_count;
1304}
Zachary Turnerb96181c2018-10-22 16:19:07 +00001305
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001306size_t
1307SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit,
1308 VariableList &variables) {
1309 PdbSymUid sym_uid(comp_unit.GetID());
1310 lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland);
1311 return 0;
1312}
1313
1314VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id,
1315 PdbCompilandSymId var_id,
1316 bool is_param) {
1317 ModuleSP module = GetObjectFile()->GetModule();
Aleksandr Urakov758657e2019-02-01 10:01:18 +00001318 Block &block = GetOrCreateBlock(scope_id);
1319 VariableInfo var_info =
1320 GetVariableLocationInfo(*m_index, var_id, block, module);
Zachary Turner44f19512019-01-10 20:57:32 +00001321 if (!var_info.location || !var_info.ranges)
1322 return nullptr;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001323
1324 CompilandIndexItem *cii = m_index->compilands().GetCompiland(var_id.modi);
1325 CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(*cii);
1326 TypeSP type_sp = GetOrCreateType(var_info.type);
1327 std::string name = var_info.name.str();
1328 Declaration decl;
1329 SymbolFileTypeSP sftype =
1330 std::make_shared<SymbolFileType>(*this, type_sp->GetID());
1331
1332 ValueType var_scope =
1333 is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal;
1334 VariableSP var_sp = std::make_shared<Variable>(
1335 toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope,
1336 comp_unit_sp.get(), *var_info.ranges, &decl, *var_info.location, false,
1337 false, false);
1338
Zachary Turner594c85e2018-12-17 19:43:33 +00001339 if (!is_param)
Zachary Turner22566332019-01-02 18:33:54 +00001340 m_ast->GetOrCreateVariableDecl(scope_id, var_id);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001341
1342 m_local_variables[toOpaqueUid(var_id)] = var_sp;
1343 return var_sp;
1344}
1345
1346VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable(
1347 PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) {
1348 auto iter = m_local_variables.find(toOpaqueUid(var_id));
1349 if (iter != m_local_variables.end())
1350 return iter->second;
1351
1352 return CreateLocalVariable(scope_id, var_id, is_param);
1353}
1354
Zachary Turner44f19512019-01-10 20:57:32 +00001355TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) {
1356 CVSymbol sym = m_index->ReadSymbolRecord(id);
1357 lldbassert(sym.kind() == SymbolKind::S_UDT);
1358
1359 UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym));
1360
1361 TypeSP target_type = GetOrCreateType(udt.Type);
1362
1363 (void)m_ast->GetOrCreateTypedefDecl(id);
1364
1365 Declaration decl;
1366 return std::make_shared<lldb_private::Type>(
1367 toOpaqueUid(id), this, ConstString(udt.Name), target_type->GetByteSize(),
1368 nullptr, target_type->GetID(), lldb_private::Type::eEncodingIsTypedefUID,
1369 decl, target_type->GetForwardCompilerType(),
1370 lldb_private::Type::eResolveStateForward);
1371}
1372
1373TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) {
1374 auto iter = m_types.find(toOpaqueUid(id));
1375 if (iter != m_types.end())
1376 return iter->second;
1377
1378 return CreateTypedef(id);
1379}
1380
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001381size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) {
1382 Block &block = GetOrCreateBlock(block_id);
1383
1384 size_t count = 0;
1385
1386 CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi);
1387 CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset);
1388 uint32_t params_remaining = 0;
1389 switch (sym.kind()) {
1390 case S_GPROC32:
1391 case S_LPROC32: {
1392 ProcSym proc(static_cast<SymbolRecordKind>(sym.kind()));
1393 cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym, proc));
1394 CVType signature = m_index->tpi().getType(proc.FunctionType);
1395 ProcedureRecord sig;
1396 cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(signature, sig));
1397 params_remaining = sig.getParameterCount();
1398 break;
1399 }
1400 case S_BLOCK32:
1401 break;
1402 default:
1403 lldbassert(false && "Symbol is not a block!");
1404 return 0;
1405 }
1406
1407 VariableListSP variables = block.GetBlockVariableList(false);
1408 if (!variables) {
1409 variables = std::make_shared<VariableList>();
1410 block.SetVariableList(variables);
1411 }
1412
1413 CVSymbolArray syms = limitSymbolArrayToScope(
1414 cii->m_debug_stream.getSymbolArray(), block_id.offset);
1415
1416 // Skip the first record since it's a PROC32 or BLOCK32, and there's
1417 // no point examining it since we know it's not a local variable.
1418 syms.drop_front();
1419 auto iter = syms.begin();
1420 auto end = syms.end();
1421
1422 while (iter != end) {
1423 uint32_t record_offset = iter.offset();
1424 CVSymbol variable_cvs = *iter;
1425 PdbCompilandSymId child_sym_id(block_id.modi, record_offset);
1426 ++iter;
1427
1428 // If this is a block, recurse into its children and then skip it.
1429 if (variable_cvs.kind() == S_BLOCK32) {
1430 uint32_t block_end = getScopeEndOffset(variable_cvs);
1431 count += ParseVariablesForBlock(child_sym_id);
1432 iter = syms.at(block_end);
1433 continue;
1434 }
1435
1436 bool is_param = params_remaining > 0;
1437 VariableSP variable;
1438 switch (variable_cvs.kind()) {
1439 case S_REGREL32:
1440 case S_REGISTER:
1441 case S_LOCAL:
1442 variable = GetOrCreateLocalVariable(block_id, child_sym_id, is_param);
1443 if (is_param)
1444 --params_remaining;
Zachary Turner44f19512019-01-10 20:57:32 +00001445 if (variable)
1446 variables->AddVariableIfUnique(variable);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001447 break;
1448 default:
1449 break;
1450 }
1451 }
1452
1453 // Pass false for set_children, since we call this recursively so that the
1454 // children will call this for themselves.
1455 block.SetDidParseVariables(true, false);
1456
1457 return count;
1458}
1459
1460size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) {
1461 lldbassert(sc.function || sc.comp_unit);
1462
1463 VariableListSP variables;
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001464 if (sc.block) {
1465 PdbSymUid block_id(sc.block->GetID());
1466
1467 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1468 return count;
1469 }
1470
1471 if (sc.function) {
1472 PdbSymUid block_id(sc.function->GetID());
1473
1474 size_t count = ParseVariablesForBlock(block_id.asCompilandSym());
1475 return count;
1476 }
1477
1478 if (sc.comp_unit) {
1479 variables = sc.comp_unit->GetVariableList(false);
1480 if (!variables) {
1481 variables = std::make_shared<VariableList>();
1482 sc.comp_unit->SetVariableList(variables);
1483 }
1484 return ParseVariablesForCompileUnit(*sc.comp_unit, *variables);
1485 }
1486
1487 llvm_unreachable("Unreachable!");
1488}
1489
1490CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001491 clang::Decl *decl = m_ast->GetOrCreateDeclForUid(PdbSymUid(uid));
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001492
Zachary Turner594c85e2018-12-17 19:43:33 +00001493 return m_ast->ToCompilerDecl(*decl);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001494}
1495
1496CompilerDeclContext
1497SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001498 clang::DeclContext *context =
1499 m_ast->GetOrCreateDeclContextForUid(PdbSymUid(uid));
1500 if (!context)
1501 return {};
1502
1503 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnerd3d2b9b2018-12-13 18:17:51 +00001504}
1505
Zachary Turnera42bbe32018-12-07 19:34:02 +00001506CompilerDeclContext
1507SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001508 clang::DeclContext *context = m_ast->GetParentDeclContext(PdbSymUid(uid));
1509 return m_ast->ToCompilerDeclContext(*context);
Zachary Turnera42bbe32018-12-07 19:34:02 +00001510}
1511
Zachary Turnerb96181c2018-10-22 16:19:07 +00001512Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001513 auto iter = m_types.find(type_uid);
1514 // lldb should not be passing us non-sensical type uids. the only way it
1515 // could have a type uid in the first place is if we handed it out, in which
Zachary Turner9f727952018-10-26 09:06:38 +00001516 // case we should know about the type. However, that doesn't mean we've
1517 // instantiated it yet. We can vend out a UID for a future type. So if the
1518 // type doesn't exist, let's instantiate it now.
1519 if (iter != m_types.end())
1520 return &*iter->second;
1521
Zachary Turner6284aee2018-11-16 02:42:32 +00001522 PdbSymUid uid(type_uid);
1523 lldbassert(uid.kind() == PdbSymUidKind::Type);
1524 PdbTypeSymId type_id = uid.asTypeSym();
1525 if (type_id.index.isNoneType())
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001526 return nullptr;
Zachary Turner9f727952018-10-26 09:06:38 +00001527
Zachary Turner6284aee2018-11-16 02:42:32 +00001528 TypeSP type_sp = CreateAndCacheType(type_id);
Zachary Turner9f727952018-10-26 09:06:38 +00001529 return &*type_sp;
Zachary Turnerb96181c2018-10-22 16:19:07 +00001530}
1531
Adrian Prantleca07c52018-11-05 20:49:07 +00001532llvm::Optional<SymbolFile::ArrayInfo>
1533SymbolFileNativePDB::GetDynamicArrayInfoForUID(
1534 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) {
1535 return llvm::None;
1536}
1537
1538
Zachary Turnerb96181c2018-10-22 16:19:07 +00001539bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) {
Zachary Turner594c85e2018-12-17 19:43:33 +00001540 clang::QualType qt =
1541 clang::QualType::getFromOpaquePtr(compiler_type.GetOpaqueQualType());
Zachary Turner2f7efbc2018-10-23 16:37:53 +00001542
Zachary Turner594c85e2018-12-17 19:43:33 +00001543 return m_ast->CompleteType(qt);
Zachary Turnerb96181c2018-10-22 16:19:07 +00001544}
1545
1546size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
Zachary Turner117b1fa2018-10-25 20:45:40 +00001547 TypeClass type_mask,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001548 lldb_private::TypeList &type_list) {
1549 return 0;
1550}
1551
1552CompilerDeclContext
Adrian Prantl0e4c4822019-03-06 21:22:25 +00001553SymbolFileNativePDB::FindNamespace(ConstString name,
Zachary Turnerb96181c2018-10-22 16:19:07 +00001554 const CompilerDeclContext *parent_decl_ctx) {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001555 return {};
1556}
1557
Zachary Turnerb96181c2018-10-22 16:19:07 +00001558TypeSystem *
Zachary Turner307f5ae2018-10-12 19:47:13 +00001559SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
1560 auto type_system =
1561 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
1562 if (type_system)
1563 type_system->SetSymbolFile(this);
1564 return type_system;
1565}
1566
Zachary Turnerb96181c2018-10-22 16:19:07 +00001567ConstString SymbolFileNativePDB::GetPluginName() {
Zachary Turner307f5ae2018-10-12 19:47:13 +00001568 static ConstString g_name("pdb");
1569 return g_name;
1570}
1571
1572uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; }