blob: b45a5881dcb5ca0a3449aeca555900792c77acbf [file] [log] [blame]
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00001//===- NativeSession.cpp - Native implementation of IPDBSession -*- 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
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
10
11#include "llvm/ADT/STLExtras.h"
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000012#include "llvm/DebugInfo/CodeView/TypeIndex.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000013#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
14#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000015#include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h"
Nico Weberd100b5dd2019-07-16 18:04:26 +000016#include "llvm/DebugInfo/PDB/Native/NativeEnumInjectedSources.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000017#include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h"
Adrian McCarthy4d93d662017-03-29 19:27:08 +000018#include "llvm/DebugInfo/PDB/Native/NativeExeSymbol.h"
Zachary Turner5d629962018-09-07 00:12:56 +000019#include "llvm/DebugInfo/PDB/Native/NativeTypeBuiltin.h"
20#include "llvm/DebugInfo/PDB/Native/NativeTypeEnum.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000021#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turner8ab7dd602018-09-07 00:12:34 +000023#include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000024#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000025#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
26#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000027#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000028#include "llvm/Support/Allocator.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000029#include "llvm/Support/BinaryByteStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000030#include "llvm/Support/Error.h"
31#include "llvm/Support/ErrorOr.h"
32#include "llvm/Support/MemoryBuffer.h"
Adrian McCarthybf0afc32017-06-28 22:47:40 +000033
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000034#include <algorithm>
Adrian McCarthyb41f03e2017-08-04 22:37:58 +000035#include <cassert>
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000036#include <memory>
Adrian McCarthybf0afc32017-06-28 22:47:40 +000037#include <utility>
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000038
39using namespace llvm;
40using namespace llvm::msf;
41using namespace llvm::pdb;
42
Zachary Turner8ab7dd602018-09-07 00:12:34 +000043static 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}
Adrian McCarthy8d090fc2017-07-12 19:38:11 +000051
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000052NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
53 std::unique_ptr<BumpPtrAllocator> Allocator)
Zachary Turner8ab7dd602018-09-07 00:12:34 +000054 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)),
55 Cache(*this, getDbiStreamPtr(*Pdb)) {}
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000056
57NativeSession::~NativeSession() = default;
58
Peter Collingbourne75257bc2017-10-20 19:48:26 +000059Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000060 std::unique_ptr<IPDBSession> &Session) {
Peter Collingbourne75257bc2017-10-20 19:48:26 +000061 StringRef Path = Buffer->getBufferIdentifier();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000062 auto Stream = std::make_unique<MemoryBufferByteStream>(
Zachary Turner695ed562017-02-28 00:04:07 +000063 std::move(Buffer), llvm::support::little);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000064
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000065 auto Allocator = std::make_unique<BumpPtrAllocator>();
66 auto File = std::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000067 if (auto EC = File->parseFileHeaders())
68 return EC;
69 if (auto EC = File->parseStreamData())
70 return EC;
71
72 Session =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000073 std::make_unique<NativeSession>(std::move(File), std::move(Allocator));
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000074
75 return Error::success();
76}
77
78Error NativeSession::createFromExe(StringRef Path,
79 std::unique_ptr<IPDBSession> &Session) {
80 return make_error<RawError>(raw_error_code::feature_unsupported);
81}
82
83uint64_t NativeSession::getLoadAddress() const { return 0; }
84
Aaron Smith89a19ac2018-02-23 00:02:27 +000085bool NativeSession::setLoadAddress(uint64_t Address) { return false; }
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000086
Adrian McCarthy6a4b0802017-06-22 18:42:23 +000087std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
Zachary Turner7999b4f2018-09-05 23:30:38 +000088 return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000089}
90
91std::unique_ptr<PDBSymbol>
Zachary Turnercae734582018-09-10 21:30:59 +000092NativeSession::getSymbolById(SymIndexId SymbolId) const {
Zachary Turner8ab7dd602018-09-07 00:12:34 +000093 return Cache.getSymbolById(SymbolId);
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000094}
95
Aaron Smith53708a52018-03-26 22:10:02 +000096bool NativeSession::addressForVA(uint64_t VA, uint32_t &Section,
97 uint32_t &Offset) const {
98 return false;
99}
100
101bool NativeSession::addressForRVA(uint32_t VA, uint32_t &Section,
102 uint32_t &Offset) const {
103 return false;
104}
105
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000106std::unique_ptr<PDBSymbol>
107NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const {
108 return nullptr;
109}
110
Aaron Smith3dca0be2018-04-10 17:33:18 +0000111std::unique_ptr<PDBSymbol>
112NativeSession::findSymbolByRVA(uint32_t RVA, PDB_SymType Type) const {
113 return nullptr;
114}
115
116std::unique_ptr<PDBSymbol>
117NativeSession::findSymbolBySectOffset(uint32_t Sect, uint32_t Offset,
118 PDB_SymType Type) const {
119 return nullptr;
120}
121
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000122std::unique_ptr<IPDBEnumLineNumbers>
123NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland,
124 const IPDBSourceFile &File) const {
125 return nullptr;
126}
127
128std::unique_ptr<IPDBEnumLineNumbers>
129NativeSession::findLineNumbersByAddress(uint64_t Address,
130 uint32_t Length) const {
131 return nullptr;
132}
133
Aaron Smith40198f52018-03-15 06:04:51 +0000134std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smithed81a9d2018-03-26 22:13:22 +0000135NativeSession::findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const {
136 return nullptr;
137}
138
139std::unique_ptr<IPDBEnumLineNumbers>
Aaron Smith40198f52018-03-15 06:04:51 +0000140NativeSession::findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset,
141 uint32_t Length) const {
142 return nullptr;
143}
144
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000145std::unique_ptr<IPDBEnumSourceFiles>
146NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland,
147 StringRef Pattern,
148 PDB_NameSearchFlags Flags) const {
149 return nullptr;
150}
151
152std::unique_ptr<IPDBSourceFile>
153NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland,
154 StringRef Pattern,
155 PDB_NameSearchFlags Flags) const {
156 return nullptr;
157}
158
159std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
160NativeSession::findCompilandsForSourceFile(StringRef Pattern,
161 PDB_NameSearchFlags Flags) const {
162 return nullptr;
163}
164
165std::unique_ptr<PDBSymbolCompiland>
166NativeSession::findOneCompilandForSourceFile(StringRef Pattern,
167 PDB_NameSearchFlags Flags) const {
168 return nullptr;
169}
170
171std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
172 return nullptr;
173}
174
175std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
176 const PDBSymbolCompiland &Compiland) const {
177 return nullptr;
178}
179
180std::unique_ptr<IPDBSourceFile>
181NativeSession::getSourceFileById(uint32_t FileId) const {
182 return nullptr;
183}
184
185std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
186 return nullptr;
187}
Aaron Smith89bca9e2017-11-16 14:33:09 +0000188
189std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
190 return nullptr;
191}
Zachary Turner679aead2018-03-13 17:46:06 +0000192
193std::unique_ptr<IPDBEnumInjectedSources>
194NativeSession::getInjectedSources() const {
Nico Weberd100b5dd2019-07-16 18:04:26 +0000195 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 }
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000205 return std::make_unique<NativeEnumInjectedSources>(*Pdb, *ISS, *Strings);
Zachary Turner679aead2018-03-13 17:46:06 +0000206}
Aaron Smith523de052018-03-22 04:08:15 +0000207
208std::unique_ptr<IPDBEnumSectionContribs>
209NativeSession::getSectionContribs() const {
210 return nullptr;
211}
Zachary Turner7999b4f2018-09-05 23:30:38 +0000212
Aleksandr Urakovc43e0862018-10-23 08:14:53 +0000213std::unique_ptr<IPDBEnumFrameData>
214NativeSession::getFrameData() const {
215 return nullptr;
216}
217
Zachary Turner8ab7dd602018-09-07 00:12:34 +0000218void NativeSession::initializeExeSymbol() {
219 if (ExeSymbol == 0)
220 ExeSymbol = Cache.createSymbol<NativeExeSymbol>();
221}
222
223NativeExeSymbol &NativeSession::getNativeGlobalScope() const {
224 const_cast<NativeSession &>(*this).initializeExeSymbol();
225
226 return Cache.getNativeSymbolById<NativeExeSymbol>(ExeSymbol);
Zachary Turner7999b4f2018-09-05 23:30:38 +0000227}