blob: bf66f1c3f4232658a74597a46b12284b7542cc4c [file] [log] [blame]
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00001//===- NativeSession.cpp - Native implementation of IPDBSession -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
11
12#include "llvm/ADT/STLExtras.h"
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000013#include "llvm/DebugInfo/CodeView/TypeIndex.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000014#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
15#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000016#include "llvm/DebugInfo/PDB/Native/NativeBuiltinSymbol.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000017#include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000018#include "llvm/DebugInfo/PDB/Native/NativeEnumSymbol.h"
19#include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
Adrian McCarthy4d93d662017-03-29 19:27:08 +000020#include "llvm/DebugInfo/PDB/Native/NativeExeSymbol.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000023#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000024#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000026#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000027#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000028#include "llvm/Support/BinaryByteStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000029#include "llvm/Support/Error.h"
30#include "llvm/Support/ErrorOr.h"
31#include "llvm/Support/MemoryBuffer.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000032
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000033#include <algorithm>
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000034#include <cassert>
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000035#include <memory>
Adrian McCarthybf0afc32017-06-28 22:47:40 +000036#include <utility>
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000037
38using namespace llvm;
39using namespace llvm::msf;
40using namespace llvm::pdb;
41
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000042namespace {
43// Maps codeview::SimpleTypeKind of a built-in type to the parameters necessary
44// to instantiate a NativeBuiltinSymbol for that type.
Reid Kleckner0962cb22017-07-12 19:46:35 +000045static const struct BuiltinTypeEntry {
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000046 codeview::SimpleTypeKind Kind;
47 PDB_BuiltinType Type;
48 uint32_t Size;
49} BuiltinTypes[] = {
50 {codeview::SimpleTypeKind::Int32, PDB_BuiltinType::Int, 4},
51 {codeview::SimpleTypeKind::UInt32, PDB_BuiltinType::UInt, 4},
52 {codeview::SimpleTypeKind::UInt32Long, PDB_BuiltinType::UInt, 4},
53 {codeview::SimpleTypeKind::UInt64Quad, PDB_BuiltinType::UInt, 8},
54 {codeview::SimpleTypeKind::NarrowCharacter, PDB_BuiltinType::Char, 1},
55 {codeview::SimpleTypeKind::SignedCharacter, PDB_BuiltinType::Char, 1},
56 {codeview::SimpleTypeKind::UnsignedCharacter, PDB_BuiltinType::UInt, 1},
57 {codeview::SimpleTypeKind::UInt16Short, PDB_BuiltinType::UInt, 2},
58 {codeview::SimpleTypeKind::Boolean8, PDB_BuiltinType::Bool, 1}
59 // This table can be grown as necessary, but these are the only types we've
60 // needed so far.
61};
62} // namespace
63
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000064NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
65 std::unique_ptr<BumpPtrAllocator> Allocator)
66 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)) {}
67
68NativeSession::~NativeSession() = default;
69
Peter Collingbourne75257bc2017-10-20 19:48:26 +000070Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000071 std::unique_ptr<IPDBSession> &Session) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +000072 StringRef Path = Buffer->getBufferIdentifier();
Zachary Turner695ed562017-02-28 00:04:07 +000073 auto Stream = llvm::make_unique<MemoryBufferByteStream>(
74 std::move(Buffer), llvm::support::little);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000075
76 auto Allocator = llvm::make_unique<BumpPtrAllocator>();
Zachary Turner7b327d02017-02-16 23:35:45 +000077 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000078 if (auto EC = File->parseFileHeaders())
79 return EC;
80 if (auto EC = File->parseStreamData())
81 return EC;
82
83 Session =
84 llvm::make_unique<NativeSession>(std::move(File), std::move(Allocator));
85
86 return Error::success();
87}
88
89Error NativeSession::createFromExe(StringRef Path,
90 std::unique_ptr<IPDBSession> &Session) {
91 return make_error<RawError>(raw_error_code::feature_unsupported);
92}
93
Adrian McCarthybf0afc32017-06-28 22:47:40 +000094std::unique_ptr<PDBSymbolCompiland>
95NativeSession::createCompilandSymbol(DbiModuleDescriptor MI) {
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000096 const auto Id = static_cast<SymIndexId>(SymbolCache.size());
Adrian McCarthybf0afc32017-06-28 22:47:40 +000097 SymbolCache.push_back(
98 llvm::make_unique<NativeCompilandSymbol>(*this, Id, MI));
99 return llvm::make_unique<PDBSymbolCompiland>(
100 *this, std::unique_ptr<IPDBRawSymbol>(SymbolCache[Id]->clone()));
101}
102
Adrian McCarthyb41f03e2017-08-04 22:37:58 +0000103std::unique_ptr<PDBSymbolTypeEnum>
104NativeSession::createEnumSymbol(codeview::TypeIndex Index) {
105 const auto Id = findSymbolByTypeIndex(Index);
106 return llvm::make_unique<PDBSymbolTypeEnum>(
107 *this, std::unique_ptr<IPDBRawSymbol>(SymbolCache[Id]->clone()));
108}
109
110std::unique_ptr<IPDBEnumSymbols>
111NativeSession::createTypeEnumerator(codeview::TypeLeafKind Kind) {
112 auto Tpi = Pdb->getPDBTpiStream();
113 if (!Tpi) {
114 consumeError(Tpi.takeError());
115 return nullptr;
116 }
117 auto &Types = Tpi->typeCollection();
118 return std::unique_ptr<IPDBEnumSymbols>(
119 new NativeEnumTypes(*this, Types, codeview::LF_ENUM));
120}
121
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000122SymIndexId NativeSession::findSymbolByTypeIndex(codeview::TypeIndex Index) {
123 // First see if it's already in our cache.
124 const auto Entry = TypeIndexToSymbolId.find(Index);
125 if (Entry != TypeIndexToSymbolId.end())
126 return Entry->second;
127
128 // Symbols for built-in types are created on the fly.
129 if (Index.isSimple()) {
130 // FIXME: We will eventually need to handle pointers to other simple types,
131 // which are still simple types in the world of CodeView TypeIndexes.
132 if (Index.getSimpleMode() != codeview::SimpleTypeMode::Direct)
133 return 0;
134 const auto Kind = Index.getSimpleKind();
Reid Kleckner0962cb22017-07-12 19:46:35 +0000135 const auto It =
136 std::find_if(std::begin(BuiltinTypes), std::end(BuiltinTypes),
137 [Kind](const BuiltinTypeEntry &Builtin) {
138 return Builtin.Kind == Kind;
139 });
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000140 if (It == std::end(BuiltinTypes))
141 return 0;
142 SymIndexId Id = SymbolCache.size();
143 SymbolCache.emplace_back(
Reid Kleckner0962cb22017-07-12 19:46:35 +0000144 llvm::make_unique<NativeBuiltinSymbol>(*this, Id, It->Type, It->Size));
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000145 TypeIndexToSymbolId[Index] = Id;
146 return Id;
147 }
148
Adrian McCarthyb41f03e2017-08-04 22:37:58 +0000149 // We need to instantiate and cache the desired type symbol.
150 auto Tpi = Pdb->getPDBTpiStream();
151 if (!Tpi) {
152 consumeError(Tpi.takeError());
153 return 0;
154 }
155 auto &Types = Tpi->typeCollection();
156 const auto &I = Types.getType(Index);
157 const auto Id = static_cast<SymIndexId>(SymbolCache.size());
158 // TODO(amccarth): Make this handle all types, not just LF_ENUMs.
159 assert(I.kind() == codeview::LF_ENUM);
160 SymbolCache.emplace_back(llvm::make_unique<NativeEnumSymbol>(*this, Id, I));
161 TypeIndexToSymbolId[Index] = Id;
162 return Id;
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000163}
164
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000165uint64_t NativeSession::getLoadAddress() const { return 0; }
166
Aaron Smith89a19ac2018-02-23 00:02:27 +0000167bool NativeSession::setLoadAddress(uint64_t Address) { return false; }
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000168
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000169std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000170 const auto Id = static_cast<SymIndexId>(SymbolCache.size());
Adrian McCarthybf0afc32017-06-28 22:47:40 +0000171 SymbolCache.push_back(llvm::make_unique<NativeExeSymbol>(*this, Id));
172 auto RawSymbol = SymbolCache[Id]->clone();
Adrian McCarthy649b8e02017-02-24 00:10:47 +0000173 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
174 std::unique_ptr<PDBSymbolExe> ExeSymbol(
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000175 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
Adrian McCarthy649b8e02017-02-24 00:10:47 +0000176 return ExeSymbol;
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000177}
178
179std::unique_ptr<PDBSymbol>
180NativeSession::getSymbolById(uint32_t SymbolId) const {
Adrian McCarthybf0afc32017-06-28 22:47:40 +0000181 // If the caller has a SymbolId, it'd better be in our SymbolCache.
182 return SymbolId < SymbolCache.size()
183 ? PDBSymbol::create(*this, SymbolCache[SymbolId]->clone())
184 : nullptr;
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000185}
186
Aaron Smith53708a52018-03-26 22:10:02 +0000187bool NativeSession::addressForVA(uint64_t VA, uint32_t &Section,
188 uint32_t &Offset) const {
189 return false;
190}
191
192bool NativeSession::addressForRVA(uint32_t VA, uint32_t &Section,
193 uint32_t &Offset) const {
194 return false;
195}
196
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000197std::unique_ptr<PDBSymbol>
198NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
199 return nullptr;
200}
201
Aaron Smith3dca0be2018-04-10 17:33:18 +0000202std::unique_ptr<PDBSymbol>
203NativeSession::findSymbolByRVA(uint32_t RVA, PDB_SymType Type) const {
204 return nullptr;
205}
206
207std::unique_ptr<PDBSymbol>
208NativeSession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
209 PDB_SymType Type) const {
210 return nullptr;
211}
212
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000213std::unique_ptr<IPDBEnumLineNumbers>
214NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland,
215 const IPDBSourceFile &File) const {
216 return nullptr;
217}
218
219std::unique_ptr<IPDBEnumLineNumbers>
220NativeSession::findLineNumbersByAddress(uint64_t Address,
221 uint32_t Length) const {
222 return nullptr;
223}
224
Aaron Smith40198f52018-03-15 06:04:51 +0000225std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smithed81a9d2018-03-26 22:13:22 +0000226NativeSession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
227 return nullptr;
228}
229
230std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smith40198f52018-03-15 06:04:51 +0000231NativeSession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
232 uint32_t Length) const {
233 return nullptr;
234}
235
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000236std::unique_ptr<IPDBEnumSourceFiles>
237NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland,
238 StringRef Pattern,
239 PDB_NameSearchFlags Flags) const {
240 return nullptr;
241}
242
243std::unique_ptr<IPDBSourceFile>
244NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
245 StringRef Pattern,
246 PDB_NameSearchFlags Flags) const {
247 return nullptr;
248}
249
250std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
251NativeSession::findCompilandsForSourceFile(StringRef Pattern,
252 PDB_NameSearchFlags Flags) const {
253 return nullptr;
254}
255
256std::unique_ptr<PDBSymbolCompiland>
257NativeSession::findOneCompilandForSourceFile(StringRef Pattern,
258 PDB_NameSearchFlags Flags) const {
259 return nullptr;
260}
261
262std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
263 return nullptr;
264}
265
266std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
267 const PDBSymbolCompiland &Compiland) const {
268 return nullptr;
269}
270
271std::unique_ptr<IPDBSourceFile>
272NativeSession::getSourceFileById(uint32_t FileId) const {
273 return nullptr;
274}
275
276std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
277 return nullptr;
278}
Aaron Smith89bca9e2017-11-16 14:33:09 +0000279
280std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
281 return nullptr;
282}
Zachary Turner679aead2018-03-13 17:46:06 +0000283
284std::unique_ptr<IPDBEnumInjectedSources>
285NativeSession::getInjectedSources() const {
286 return nullptr;
287}
Aaron Smith523de052018-03-22 04:08:15 +0000288
289std::unique_ptr<IPDBEnumSectionContribs>
290NativeSession::getSectionContribs() const {
291 return nullptr;
292}