1 //===- NativeSession.cpp - Native implementation of IPDBSession -*- C++ -*-===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
10
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
13 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
14 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
15 #include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
16 #include "llvm/DebugInfo/PDB/Native/NativeEnumInjectedSources.h"
17 #include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
18 #include "llvm/DebugInfo/PDB/Native/NativeExeSymbol.h"
19 #include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
20 #include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
21 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22 #include "llvm/DebugInfo/PDB/Native/RawError.h"
23 #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
24 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/BinaryByteStream.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/ErrorOr.h"
32 #include "llvm/Support/MemoryBuffer.h"
33
34 #include <algorithm>
35 #include <cassert>
36 #include <memory>
37 #include <utility>
38
39 using namespace llvm;
40 using namespace llvm::msf;
41 using namespace llvm::pdb;
42
getDbiStreamPtr(PDBFile & File)43 static DbiStream *getDbiStreamPtr(PDBFile &File) {
44 Expected<DbiStream &> DbiS = File.getPDBDbiStream();
45 if (DbiS)
46 return &DbiS.get();
47
48 consumeError(DbiS.takeError());
49 return nullptr;
50 }
51
NativeSession(std::unique_ptr<PDBFile> PdbFile,std::unique_ptr<BumpPtrAllocator> Allocator)52 NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
53 std::unique_ptr<BumpPtrAllocator> Allocator)
54 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)),
55 Cache(*this, getDbiStreamPtr(*Pdb)) {}
56
57 NativeSession::~NativeSession() = default;
58
createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,std::unique_ptr<IPDBSession> & Session)59 Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
60 std::unique_ptr<IPDBSession> &Session) {
61 StringRef Path = Buffer->getBufferIdentifier();
62 auto Stream = std::make_unique<MemoryBufferByteStream>(
63 std::move(Buffer), llvm::support::little);
64
65 auto Allocator = std::make_unique<BumpPtrAllocator>();
66 auto File = std::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
67 if (auto EC = File->parseFileHeaders())
68 return EC;
69 if (auto EC = File->parseStreamData())
70 return EC;
71
72 Session =
73 std::make_unique<NativeSession>(std::move(File), std::move(Allocator));
74
75 return Error::success();
76 }
77
createFromExe(StringRef Path,std::unique_ptr<IPDBSession> & Session)78 Error NativeSession::createFromExe(StringRef Path,
79 std::unique_ptr<IPDBSession> &Session) {
80 return make_error<RawError>(raw_error_code::feature_unsupported);
81 }
82
getLoadAddress() const83 uint64_t NativeSession::getLoadAddress() const { return 0; }
84
setLoadAddress(uint64_t Address)85 bool NativeSession::setLoadAddress(uint64_t Address) { return false; }
86
getGlobalScope()87 std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
88 return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
89 }
90
91 std::unique_ptr<PDBSymbol>
getSymbolById(SymIndexId SymbolId) const92 NativeSession::getSymbolById(SymIndexId SymbolId) const {
93 return Cache.getSymbolById(SymbolId);
94 }
95
addressForVA(uint64_t VA,uint32_t & Section,uint32_t & Offset) const96 bool NativeSession::addressForVA(uint64_t VA, uint32_t &Section,
97 uint32_t &Offset) const {
98 return false;
99 }
100
addressForRVA(uint32_t VA,uint32_t & Section,uint32_t & Offset) const101 bool NativeSession::addressForRVA(uint32_t VA, uint32_t &Section,
102 uint32_t &Offset) const {
103 return false;
104 }
105
106 std::unique_ptr<PDBSymbol>
findSymbolByAddress(uint64_t Address,PDB_SymType Type) const107 NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
108 return nullptr;
109 }
110
111 std::unique_ptr<PDBSymbol>
findSymbolByRVA(uint32_t RVA,PDB_SymType Type) const112 NativeSession::findSymbolByRVA(uint32_t RVA, PDB_SymType Type) const {
113 return nullptr;
114 }
115
116 std::unique_ptr<PDBSymbol>
findSymbolBySectOffset(uint32_t Sect,uint32_t Offset,PDB_SymType Type) const117 NativeSession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
118 PDB_SymType Type) const {
119 return nullptr;
120 }
121
122 std::unique_ptr<IPDBEnumLineNumbers>
findLineNumbers(const PDBSymbolCompiland & Compiland,const IPDBSourceFile & File) const123 NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland,
124 const IPDBSourceFile &File) const {
125 return nullptr;
126 }
127
128 std::unique_ptr<IPDBEnumLineNumbers>
findLineNumbersByAddress(uint64_t Address,uint32_t Length) const129 NativeSession::findLineNumbersByAddress(uint64_t Address,
130 uint32_t Length) const {
131 return nullptr;
132 }
133
134 std::unique_ptr<IPDBEnumLineNumbers>
findLineNumbersByRVA(uint32_t RVA,uint32_t Length) const135 NativeSession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
136 return nullptr;
137 }
138
139 std::unique_ptr<IPDBEnumLineNumbers>
findLineNumbersBySectOffset(uint32_t Section,uint32_t Offset,uint32_t Length) const140 NativeSession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
141 uint32_t Length) const {
142 return nullptr;
143 }
144
145 std::unique_ptr<IPDBEnumSourceFiles>
findSourceFiles(const PDBSymbolCompiland * Compiland,StringRef Pattern,PDB_NameSearchFlags Flags) const146 NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland,
147 StringRef Pattern,
148 PDB_NameSearchFlags Flags) const {
149 return nullptr;
150 }
151
152 std::unique_ptr<IPDBSourceFile>
findOneSourceFile(const PDBSymbolCompiland * Compiland,StringRef Pattern,PDB_NameSearchFlags Flags) const153 NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
154 StringRef Pattern,
155 PDB_NameSearchFlags Flags) const {
156 return nullptr;
157 }
158
159 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
findCompilandsForSourceFile(StringRef Pattern,PDB_NameSearchFlags Flags) const160 NativeSession::findCompilandsForSourceFile(StringRef Pattern,
161 PDB_NameSearchFlags Flags) const {
162 return nullptr;
163 }
164
165 std::unique_ptr<PDBSymbolCompiland>
findOneCompilandForSourceFile(StringRef Pattern,PDB_NameSearchFlags Flags) const166 NativeSession::findOneCompilandForSourceFile(StringRef Pattern,
167 PDB_NameSearchFlags Flags) const {
168 return nullptr;
169 }
170
getAllSourceFiles() const171 std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
172 return nullptr;
173 }
174
getSourceFilesForCompiland(const PDBSymbolCompiland & Compiland) const175 std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
176 const PDBSymbolCompiland &Compiland) const {
177 return nullptr;
178 }
179
180 std::unique_ptr<IPDBSourceFile>
getSourceFileById(uint32_t FileId) const181 NativeSession::getSourceFileById(uint32_t FileId) const {
182 return nullptr;
183 }
184
getDebugStreams() const185 std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
186 return nullptr;
187 }
188
getEnumTables() const189 std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
190 return nullptr;
191 }
192
193 std::unique_ptr<IPDBEnumInjectedSources>
getInjectedSources() const194 NativeSession::getInjectedSources() const {
195 auto ISS = Pdb->getInjectedSourceStream();
196 if (!ISS) {
197 consumeError(ISS.takeError());
198 return nullptr;
199 }
200 auto Strings = Pdb->getStringTable();
201 if (!Strings) {
202 consumeError(Strings.takeError());
203 return nullptr;
204 }
205 return std::make_unique<NativeEnumInjectedSources>(*Pdb, *ISS, *Strings);
206 }
207
208 std::unique_ptr<IPDBEnumSectionContribs>
getSectionContribs() const209 NativeSession::getSectionContribs() const {
210 return nullptr;
211 }
212
213 std::unique_ptr<IPDBEnumFrameData>
getFrameData() const214 NativeSession::getFrameData() const {
215 return nullptr;
216 }
217
initializeExeSymbol()218 void NativeSession::initializeExeSymbol() {
219 if (ExeSymbol == 0)
220 ExeSymbol = Cache.createSymbol<NativeExeSymbol>();
221 }
222
getNativeGlobalScope() const223 NativeExeSymbol &NativeSession::getNativeGlobalScope() const {
224 const_cast<NativeSession &>(*this).initializeExeSymbol();
225
226 return Cache.getNativeSymbolById<NativeExeSymbol>(ExeSymbol);
227 }
228