1 //===- NativeSession.cpp - Native implementation of IPDBSession -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
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"
40 using namespace llvm::msf;
41 using namespace llvm::pdb;
43 static DbiStream *getDbiStreamPtr(PDBFile &File) {
44 Expected<DbiStream &> DbiS = File.getPDBDbiStream();
48 consumeError(DbiS.takeError());
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)) {}
57 NativeSession::~NativeSession() = default;
59 Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
60 std::unique_ptr<IPDBSession> &Session) {
61 StringRef Path = Buffer->getBufferIdentifier();
62 auto Stream = llvm::make_unique<MemoryBufferByteStream>(
63 std::move(Buffer), llvm::support::little);
65 auto Allocator = llvm::make_unique<BumpPtrAllocator>();
66 auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
67 if (auto EC = File->parseFileHeaders())
69 if (auto EC = File->parseStreamData())
73 llvm::make_unique<NativeSession>(std::move(File), std::move(Allocator));
75 return Error::success();
78 Error NativeSession::createFromExe(StringRef Path,
79 std::unique_ptr<IPDBSession> &Session) {
80 return make_error<RawError>(raw_error_code::feature_unsupported);
83 uint64_t NativeSession::getLoadAddress() const { return 0; }
85 bool NativeSession::setLoadAddress(uint64_t Address) { return false; }
87 std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
88 return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
91 std::unique_ptr<PDBSymbol>
92 NativeSession::getSymbolById(SymIndexId SymbolId) const {
93 return Cache.getSymbolById(SymbolId);
96 bool NativeSession::addressForVA(uint64_t VA, uint32_t &Section,
97 uint32_t &Offset) const {
101 bool NativeSession::addressForRVA(uint32_t VA, uint32_t &Section,
102 uint32_t &Offset) const {
106 std::unique_ptr<PDBSymbol>
107 NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
111 std::unique_ptr<PDBSymbol>
112 NativeSession::findSymbolByRVA(uint32_t RVA, PDB_SymType Type) const {
116 std::unique_ptr<PDBSymbol>
117 NativeSession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
118 PDB_SymType Type) const {
122 std::unique_ptr<IPDBEnumLineNumbers>
123 NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland,
124 const IPDBSourceFile &File) const {
128 std::unique_ptr<IPDBEnumLineNumbers>
129 NativeSession::findLineNumbersByAddress(uint64_t Address,
130 uint32_t Length) const {
134 std::unique_ptr<IPDBEnumLineNumbers>
135 NativeSession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
139 std::unique_ptr<IPDBEnumLineNumbers>
140 NativeSession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
141 uint32_t Length) const {
145 std::unique_ptr<IPDBEnumSourceFiles>
146 NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland,
148 PDB_NameSearchFlags Flags) const {
152 std::unique_ptr<IPDBSourceFile>
153 NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
155 PDB_NameSearchFlags Flags) const {
159 std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
160 NativeSession::findCompilandsForSourceFile(StringRef Pattern,
161 PDB_NameSearchFlags Flags) const {
165 std::unique_ptr<PDBSymbolCompiland>
166 NativeSession::findOneCompilandForSourceFile(StringRef Pattern,
167 PDB_NameSearchFlags Flags) const {
171 std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
175 std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
176 const PDBSymbolCompiland &Compiland) const {
180 std::unique_ptr<IPDBSourceFile>
181 NativeSession::getSourceFileById(uint32_t FileId) const {
185 std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
189 std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
193 std::unique_ptr<IPDBEnumInjectedSources>
194 NativeSession::getInjectedSources() const {
195 auto ISS = Pdb->getInjectedSourceStream();
197 consumeError(ISS.takeError());
200 auto Strings = Pdb->getStringTable();
202 consumeError(Strings.takeError());
205 return make_unique<NativeEnumInjectedSources>(*Pdb, *ISS, *Strings);
208 std::unique_ptr<IPDBEnumSectionContribs>
209 NativeSession::getSectionContribs() const {
213 std::unique_ptr<IPDBEnumFrameData>
214 NativeSession::getFrameData() const {
218 void NativeSession::initializeExeSymbol() {
220 ExeSymbol = Cache.createSymbol<NativeExeSymbol>();
223 NativeExeSymbol &NativeSession::getNativeGlobalScope() const {
224 const_cast<NativeSession &>(*this).initializeExeSymbol();
226 return Cache.getNativeSymbolById<NativeExeSymbol>(ExeSymbol);