blob: cb41756d826a0947c3c569a773a6afd628a5524c [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/GenericError.h"
15#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
16#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000017#include "llvm/DebugInfo/PDB/Native/NativeBuiltinSymbol.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000018#include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
Adrian McCarthy4d93d662017-03-29 19:27:08 +000019#include "llvm/DebugInfo/PDB/Native/NativeExeSymbol.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000020#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
21#include "llvm/DebugInfo/PDB/Native/RawError.h"
22#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
23#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
24#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000025#include "llvm/Support/BinaryByteStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000026#include "llvm/Support/Error.h"
27#include "llvm/Support/ErrorOr.h"
28#include "llvm/Support/MemoryBuffer.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000029
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000030#include <algorithm>
31#include <memory>
Adrian McCarthybf0afc32017-06-28 22:47:40 +000032#include <utility>
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000033
34using namespace llvm;
35using namespace llvm::msf;
36using namespace llvm::pdb;
37
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000038namespace {
39// Maps codeview::SimpleTypeKind of a built-in type to the parameters necessary
40// to instantiate a NativeBuiltinSymbol for that type.
41static const struct {
42 codeview::SimpleTypeKind Kind;
43 PDB_BuiltinType Type;
44 uint32_t Size;
45} BuiltinTypes[] = {
46 {codeview::SimpleTypeKind::Int32, PDB_BuiltinType::Int, 4},
47 {codeview::SimpleTypeKind::UInt32, PDB_BuiltinType::UInt, 4},
48 {codeview::SimpleTypeKind::UInt32Long, PDB_BuiltinType::UInt, 4},
49 {codeview::SimpleTypeKind::UInt64Quad, PDB_BuiltinType::UInt, 8},
50 {codeview::SimpleTypeKind::NarrowCharacter, PDB_BuiltinType::Char, 1},
51 {codeview::SimpleTypeKind::SignedCharacter, PDB_BuiltinType::Char, 1},
52 {codeview::SimpleTypeKind::UnsignedCharacter, PDB_BuiltinType::UInt, 1},
53 {codeview::SimpleTypeKind::UInt16Short, PDB_BuiltinType::UInt, 2},
54 {codeview::SimpleTypeKind::Boolean8, PDB_BuiltinType::Bool, 1}
55 // This table can be grown as necessary, but these are the only types we've
56 // needed so far.
57};
58} // namespace
59
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000060NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
61 std::unique_ptr<BumpPtrAllocator> Allocator)
62 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)) {}
63
64NativeSession::~NativeSession() = default;
65
66Error NativeSession::createFromPdb(StringRef Path,
67 std::unique_ptr<IPDBSession> &Session) {
68 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
69 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
70 /*RequiresNullTerminator=*/false);
71 if (!ErrorOrBuffer)
72 return make_error<GenericError>(generic_error_code::invalid_path);
73
74 std::unique_ptr<MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
Zachary Turner695ed562017-02-28 00:04:07 +000075 auto Stream = llvm::make_unique<MemoryBufferByteStream>(
76 std::move(Buffer), llvm::support::little);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000077
78 auto Allocator = llvm::make_unique<BumpPtrAllocator>();
Zachary Turner7b327d02017-02-16 23:35:45 +000079 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000080 if (auto EC = File->parseFileHeaders())
81 return EC;
82 if (auto EC = File->parseStreamData())
83 return EC;
84
85 Session =
86 llvm::make_unique<NativeSession>(std::move(File), std::move(Allocator));
87
88 return Error::success();
89}
90
91Error NativeSession::createFromExe(StringRef Path,
92 std::unique_ptr<IPDBSession> &Session) {
93 return make_error<RawError>(raw_error_code::feature_unsupported);
94}
95
Adrian McCarthybf0afc32017-06-28 22:47:40 +000096std::unique_ptr<PDBSymbolCompiland>
97NativeSession::createCompilandSymbol(DbiModuleDescriptor MI) {
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000098 const auto Id = static_cast<SymIndexId>(SymbolCache.size());
Adrian McCarthybf0afc32017-06-28 22:47:40 +000099 SymbolCache.push_back(
100 llvm::make_unique<NativeCompilandSymbol>(*this, Id, MI));
101 return llvm::make_unique<PDBSymbolCompiland>(
102 *this, std::unique_ptr<IPDBRawSymbol>(SymbolCache[Id]->clone()));
103}
104
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000105SymIndexId NativeSession::findSymbolByTypeIndex(codeview::TypeIndex Index) {
106 // First see if it's already in our cache.
107 const auto Entry = TypeIndexToSymbolId.find(Index);
108 if (Entry != TypeIndexToSymbolId.end())
109 return Entry->second;
110
111 // Symbols for built-in types are created on the fly.
112 if (Index.isSimple()) {
113 // FIXME: We will eventually need to handle pointers to other simple types,
114 // which are still simple types in the world of CodeView TypeIndexes.
115 if (Index.getSimpleMode() != codeview::SimpleTypeMode::Direct)
116 return 0;
117 const auto Kind = Index.getSimpleKind();
118 const auto It = std::find_if(
119 std::begin(BuiltinTypes), std::end(BuiltinTypes),
120 [Kind](const auto &Builtin) { return Builtin.Kind == Kind; });
121 if (It == std::end(BuiltinTypes))
122 return 0;
123 SymIndexId Id = SymbolCache.size();
124 SymbolCache.emplace_back(
125 std::make_unique<NativeBuiltinSymbol>(*this, Id, It->Type, It->Size));
126 TypeIndexToSymbolId[Index] = Id;
127 return Id;
128 }
129
130 // TODO: Look up PDB type by type index
131
132 return 0;
133}
134
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000135uint64_t NativeSession::getLoadAddress() const { return 0; }
136
137void NativeSession::setLoadAddress(uint64_t Address) {}
138
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000139std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
Adrian McCarthy8d090fc2017-07-12 19:38:11 +0000140 const auto Id = static_cast<SymIndexId>(SymbolCache.size());
Adrian McCarthybf0afc32017-06-28 22:47:40 +0000141 SymbolCache.push_back(llvm::make_unique<NativeExeSymbol>(*this, Id));
142 auto RawSymbol = SymbolCache[Id]->clone();
Adrian McCarthy649b8e02017-02-24 00:10:47 +0000143 auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol)));
144 std::unique_ptr<PDBSymbolExe> ExeSymbol(
Adrian McCarthy6a4b0802017-06-22 18:42:23 +0000145 static_cast<PDBSymbolExe *>(PdbSymbol.release()));
Adrian McCarthy649b8e02017-02-24 00:10:47 +0000146 return ExeSymbol;
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000147}
148
149std::unique_ptr<PDBSymbol>
150NativeSession::getSymbolById(uint32_t SymbolId) const {
Adrian McCarthybf0afc32017-06-28 22:47:40 +0000151 // If the caller has a SymbolId, it'd better be in our SymbolCache.
152 return SymbolId < SymbolCache.size()
153 ? PDBSymbol::create(*this, SymbolCache[SymbolId]->clone())
154 : nullptr;
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000155}
156
157std::unique_ptr<PDBSymbol>
158NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
159 return nullptr;
160}
161
162std::unique_ptr<IPDBEnumLineNumbers>
163NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland,
164 const IPDBSourceFile &File) const {
165 return nullptr;
166}
167
168std::unique_ptr<IPDBEnumLineNumbers>
169NativeSession::findLineNumbersByAddress(uint64_t Address,
170 uint32_t Length) const {
171 return nullptr;
172}
173
174std::unique_ptr<IPDBEnumSourceFiles>
175NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland,
176 StringRef Pattern,
177 PDB_NameSearchFlags Flags) const {
178 return nullptr;
179}
180
181std::unique_ptr<IPDBSourceFile>
182NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
183 StringRef Pattern,
184 PDB_NameSearchFlags Flags) const {
185 return nullptr;
186}
187
188std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
189NativeSession::findCompilandsForSourceFile(StringRef Pattern,
190 PDB_NameSearchFlags Flags) const {
191 return nullptr;
192}
193
194std::unique_ptr<PDBSymbolCompiland>
195NativeSession::findOneCompilandForSourceFile(StringRef Pattern,
196 PDB_NameSearchFlags Flags) const {
197 return nullptr;
198}
199
200std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
201 return nullptr;
202}
203
204std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
205 const PDBSymbolCompiland &Compiland) const {
206 return nullptr;
207}
208
209std::unique_ptr<IPDBSourceFile>
210NativeSession::getSourceFileById(uint32_t FileId) const {
211 return nullptr;
212}
213
214std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
215 return nullptr;
216}