LLVM 17.0.0git
NativeSession.cpp
Go to the documentation of this file.
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
10
31#include "llvm/Object/Binary.h"
32#include "llvm/Object/COFF.h"
36#include "llvm/Support/Error.h"
39#include "llvm/Support/Path.h"
40
41#include <algorithm>
42#include <cassert>
43#include <memory>
44#include <utility>
45
46using namespace llvm;
47using namespace llvm::msf;
48using namespace llvm::pdb;
49
50namespace llvm {
51namespace codeview {
52union DebugInfo;
53}
54} // namespace llvm
55
57 Expected<DbiStream &> DbiS = File.getPDBDbiStream();
58 if (DbiS)
59 return &DbiS.get();
60
61 consumeError(DbiS.takeError());
62 return nullptr;
63}
64
65NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
66 std::unique_ptr<BumpPtrAllocator> Allocator)
67 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)),
68 Cache(*this, getDbiStreamPtr(*Pdb)), AddrToModuleIndex(IMapAllocator) {}
69
71
72Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
73 std::unique_ptr<IPDBSession> &Session) {
74 StringRef Path = Buffer->getBufferIdentifier();
75 auto Stream = std::make_unique<MemoryBufferByteStream>(
76 std::move(Buffer), llvm::support::little);
77
78 auto Allocator = std::make_unique<BumpPtrAllocator>();
79 auto File = std::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
80 if (auto EC = File->parseFileHeaders())
81 return EC;
82 if (auto EC = File->parseStreamData())
83 return EC;
84
85 Session =
86 std::make_unique<NativeSession>(std::move(File), std::move(Allocator));
87
88 return Error::success();
89}
90
92loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
94 MemoryBuffer::getFile(PdbPath, /*IsText=*/false,
95 /*RequiresNullTerminator=*/false);
96 if (!ErrorOrBuffer)
97 return make_error<RawError>(ErrorOrBuffer.getError());
98 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
99
100 PdbPath = Buffer->getBufferIdentifier();
102 auto EC = identify_magic(PdbPath, Magic);
103 if (EC || Magic != file_magic::pdb)
104 return make_error<RawError>(EC);
105
106 auto Stream = std::make_unique<MemoryBufferByteStream>(std::move(Buffer),
108
109 auto File = std::make_unique<PDBFile>(PdbPath, std::move(Stream), *Allocator);
110 if (auto EC = File->parseFileHeaders())
111 return std::move(EC);
112
113 if (auto EC = File->parseStreamData())
114 return std::move(EC);
115
116 return std::move(File);
117}
118
120 std::unique_ptr<IPDBSession> &Session) {
121 auto Allocator = std::make_unique<BumpPtrAllocator>();
122 auto PdbFile = loadPdbFile(PdbPath, Allocator);
123 if (!PdbFile)
124 return PdbFile.takeError();
125
126 Session = std::make_unique<NativeSession>(std::move(PdbFile.get()),
127 std::move(Allocator));
128 return Error::success();
129}
130
133 object::createBinary(ExePath);
134 if (!BinaryFile)
135 return BinaryFile.takeError();
136
137 const object::COFFObjectFile *ObjFile =
138 dyn_cast<object::COFFObjectFile>(BinaryFile->getBinary());
139 if (!ObjFile)
140 return make_error<RawError>(raw_error_code::invalid_format);
141
142 StringRef PdbPath;
143 const llvm::codeview::DebugInfo *PdbInfo = nullptr;
144 if (Error E = ObjFile->getDebugPDBInfo(PdbInfo, PdbPath))
145 return std::move(E);
146
147 return std::string(PdbPath);
148}
149
151 std::unique_ptr<IPDBSession> &Session) {
152 Expected<std::string> PdbPath = getPdbPathFromExe(ExePath);
153 if (!PdbPath)
154 return PdbPath.takeError();
155
157 auto EC = identify_magic(PdbPath.get(), Magic);
158 if (EC || Magic != file_magic::pdb)
159 return make_error<RawError>(EC);
160
161 auto Allocator = std::make_unique<BumpPtrAllocator>();
162 auto File = loadPdbFile(PdbPath.get(), Allocator);
163 if (!File)
164 return File.takeError();
165
166 Session = std::make_unique<NativeSession>(std::move(File.get()),
167 std::move(Allocator));
168
169 return Error::success();
170}
171
173NativeSession::searchForPdb(const PdbSearchOptions &Opts) {
174 Expected<std::string> PathOrErr = getPdbPathFromExe(Opts.ExePath);
175 if (!PathOrErr)
176 return PathOrErr.takeError();
177 StringRef PathFromExe = PathOrErr.get();
178 sys::path::Style Style = PathFromExe.startswith("/")
181 StringRef PdbName = sys::path::filename(PathFromExe, Style);
182
183 // Check if pdb exists in the executable directory.
184 SmallString<128> PdbPath = StringRef(Opts.ExePath);
186 sys::path::append(PdbPath, PdbName);
187
188 auto Allocator = std::make_unique<BumpPtrAllocator>();
189
190 if (auto File = loadPdbFile(PdbPath, Allocator))
191 return std::string(PdbPath);
192 else
193 consumeError(File.takeError());
194
195 // Check path that was in the executable.
196 if (auto File = loadPdbFile(PathFromExe, Allocator))
197 return std::string(PathFromExe);
198 else
199 return File.takeError();
200
201 return make_error<RawError>("PDB not found");
202}
203
204uint64_t NativeSession::getLoadAddress() const { return LoadAddress; }
205
207 LoadAddress = Address;
208 return true;
209}
210
211std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
212 return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
213}
214
215std::unique_ptr<PDBSymbol>
217 return Cache.getSymbolById(SymbolId);
218}
219
221 uint32_t &Offset) const {
222 uint32_t RVA = VA - getLoadAddress();
223 return addressForRVA(RVA, Section, Offset);
224}
225
227 uint32_t &Offset) const {
228 Section = 0;
229 Offset = 0;
230
231 auto Dbi = Pdb->getPDBDbiStream();
232 if (!Dbi)
233 return false;
234
235 if ((int32_t)RVA < 0)
236 return true;
237
238 Offset = RVA;
239 for (; Section < Dbi->getSectionHeaders().size(); ++Section) {
240 auto &Sec = Dbi->getSectionHeaders()[Section];
241 if (RVA < Sec.VirtualAddress)
242 return true;
243 Offset = RVA - Sec.VirtualAddress;
244 }
245 return true;
246}
247
248std::unique_ptr<PDBSymbol>
250 uint32_t Section;
252 addressForVA(Address, Section, Offset);
253 return findSymbolBySectOffset(Section, Offset, Type);
254}
255
256std::unique_ptr<PDBSymbol> NativeSession::findSymbolByRVA(uint32_t RVA,
258 uint32_t Section;
260 addressForRVA(RVA, Section, Offset);
261 return findSymbolBySectOffset(Section, Offset, Type);
262}
263
264std::unique_ptr<PDBSymbol>
267 if (AddrToModuleIndex.empty())
268 parseSectionContribs();
269
270 return Cache.findSymbolBySectOffset(Sect, Offset, Type);
271}
272
273std::unique_ptr<IPDBEnumLineNumbers>
275 const IPDBSourceFile &File) const {
276 return nullptr;
277}
278
279std::unique_ptr<IPDBEnumLineNumbers>
281 uint32_t Length) const {
282 return Cache.findLineNumbersByVA(Address, Length);
283}
284
285std::unique_ptr<IPDBEnumLineNumbers>
287 return Cache.findLineNumbersByVA(getLoadAddress() + RVA, Length);
288}
289
290std::unique_ptr<IPDBEnumLineNumbers>
292 uint32_t Length) const {
293 uint64_t VA = getVAFromSectOffset(Section, Offset);
294 return Cache.findLineNumbersByVA(VA, Length);
295}
296
297std::unique_ptr<IPDBEnumSourceFiles>
300 PDB_NameSearchFlags Flags) const {
301 return nullptr;
302}
303
304std::unique_ptr<IPDBSourceFile>
307 PDB_NameSearchFlags Flags) const {
308 return nullptr;
309}
310
311std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
313 PDB_NameSearchFlags Flags) const {
314 return nullptr;
315}
316
317std::unique_ptr<PDBSymbolCompiland>
319 PDB_NameSearchFlags Flags) const {
320 return nullptr;
321}
322
323std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
324 return nullptr;
325}
326
327std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
328 const PDBSymbolCompiland &Compiland) const {
329 return nullptr;
330}
331
332std::unique_ptr<IPDBSourceFile>
334 return Cache.getSourceFileById(FileId);
335}
336
337std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
338 return nullptr;
339}
340
341std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
342 return nullptr;
343}
344
345std::unique_ptr<IPDBEnumInjectedSources>
347 auto ISS = Pdb->getInjectedSourceStream();
348 if (!ISS) {
349 consumeError(ISS.takeError());
350 return nullptr;
351 }
352 auto Strings = Pdb->getStringTable();
353 if (!Strings) {
354 consumeError(Strings.takeError());
355 return nullptr;
356 }
357 return std::make_unique<NativeEnumInjectedSources>(*Pdb, *ISS, *Strings);
358}
359
360std::unique_ptr<IPDBEnumSectionContribs>
362 return nullptr;
363}
364
365std::unique_ptr<IPDBEnumFrameData>
367 return nullptr;
368}
369
370void NativeSession::initializeExeSymbol() {
371 if (ExeSymbol == 0)
372 ExeSymbol = Cache.createSymbol<NativeExeSymbol>();
373}
374
376 const_cast<NativeSession &>(*this).initializeExeSymbol();
377
378 return Cache.getNativeSymbolById<NativeExeSymbol>(ExeSymbol);
379}
380
382 uint32_t Offset) const {
383 if (Section <= 0)
384 return 0;
385
386 auto Dbi = getDbiStreamPtr(*Pdb);
387 if (!Dbi)
388 return 0;
389
390 uint32_t MaxSection = Dbi->getSectionHeaders().size();
391 if (Section > MaxSection + 1)
392 Section = MaxSection + 1;
393 auto &Sec = Dbi->getSectionHeaders()[Section - 1];
394 return Sec.VirtualAddress + Offset;
395}
396
398 uint32_t Offset) const {
399 return LoadAddress + getRVAFromSectOffset(Section, Offset);
400}
401
403 ModuleIndex = 0;
404 auto Iter = AddrToModuleIndex.find(VA);
405 if (Iter == AddrToModuleIndex.end())
406 return false;
407 ModuleIndex = Iter.value();
408 return true;
409}
410
412 uint16_t &ModuleIndex) const {
413 ModuleIndex = 0;
414 auto Iter = AddrToModuleIndex.find(getVAFromSectOffset(Sect, Offset));
415 if (Iter == AddrToModuleIndex.end())
416 return false;
417 ModuleIndex = Iter.value();
418 return true;
419}
420
421void NativeSession::parseSectionContribs() {
422 auto Dbi = Pdb->getPDBDbiStream();
423 if (!Dbi)
424 return;
425
426 class Visitor : public ISectionContribVisitor {
427 NativeSession &Session;
428 IMap &AddrMap;
429
430 public:
431 Visitor(NativeSession &Session, IMap &AddrMap)
432 : Session(Session), AddrMap(AddrMap) {}
433 void visit(const SectionContrib &C) override {
434 if (C.Size == 0)
435 return;
436
437 uint64_t VA = Session.getVAFromSectOffset(C.ISect, C.Off);
438 uint64_t End = VA + C.Size;
439
440 // Ignore overlapping sections based on the assumption that a valid
441 // PDB file should not have overlaps.
442 if (!AddrMap.overlaps(VA, End))
443 AddrMap.insert(VA, End, C.Imod);
444 }
445 void visit(const SectionContrib2 &C) override { visit(C.Base); }
446 };
447
448 Visitor V(*this, AddrToModuleIndex);
449 Dbi->visitSectionContributions(V);
450}
451
454 auto *Dbi = getDbiStreamPtr(*Pdb);
455 assert(Dbi && "Dbi stream not present");
456
457 DbiModuleDescriptor Modi = Dbi->modules().getModuleDescriptor(Index);
458
459 uint16_t ModiStream = Modi.getModuleStreamIndex();
460 if (ModiStream == kInvalidStreamIndex)
461 return make_error<RawError>("Module stream not present");
462
463 std::unique_ptr<msf::MappedBlockStream> ModStreamData =
464 Pdb->createIndexedStream(ModiStream);
465
466 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
467 if (auto EC = ModS.reload())
468 return std::move(EC);
469
470 return std::move(ModS);
471}
This file defines the BumpPtrAllocator interface.
Lightweight arrays that are backed by an arbitrary BinaryStream.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides ErrorOr<T> smart pointer.
return ToRemove size() > 0
static DbiStream * getDbiStreamPtr(NativeSession &Session)
static DbiStream * getDbiStreamPtr(PDBFile &File)
static Expected< std::string > getPdbPathFromExe(StringRef ExePath)
static Expected< std::unique_ptr< PDBFile > > loadPdbFile(StringRef PdbPath, std::unique_ptr< BumpPtrAllocator > &Allocator)
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::error_code getError() const
Definition: ErrorOr.h:153
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
const_iterator find(KeyT x) const
find - Return an iterator pointing to the first interval ending at or after x, or end().
Definition: IntervalMap.h:1172
bool empty() const
empty - Return true when no intervals are mapped.
Definition: IntervalMap.h:1101
const_iterator end() const
Definition: IntervalMap.h:1158
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool startswith(StringRef Prefix) const
Definition: StringRef.h:261
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Error getDebugPDBInfo(const debug_directory *DebugDir, const codeview::DebugInfo *&Info, StringRef &PDBFileName) const
Get PDB information out of a codeview debug directory entry.
IPDBSourceFile defines an interface used to represent source files whose information are stored in th...
std::unique_ptr< IPDBEnumFrameData > getFrameData() const override
std::unique_ptr< IPDBEnumSourceFiles > getSourceFilesForCompiland(const PDBSymbolCompiland &Compiland) const override
static Expected< std::string > searchForPdb(const PdbSearchOptions &Opts)
std::unique_ptr< PDBSymbolExe > getGlobalScope() override
bool addressForVA(uint64_t VA, uint32_t &Section, uint32_t &Offset) const override
std::unique_ptr< IPDBEnumSourceFiles > findSourceFiles(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
bool addressForRVA(uint32_t RVA, uint32_t &Section, uint32_t &Offset) const override
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByRVA(uint32_t RVA, uint32_t Length) const override
bool moduleIndexForSectOffset(uint32_t Sect, uint32_t Offset, uint16_t &ModuleIndex) const
uint64_t getVAFromSectOffset(uint32_t Section, uint32_t Offset) const
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersBySectOffset(uint32_t Section, uint32_t Offset, uint32_t Length) const override
static Error createFromPdb(std::unique_ptr< MemoryBuffer > MB, std::unique_ptr< IPDBSession > &Session)
static Error createFromExe(StringRef Path, std::unique_ptr< IPDBSession > &Session)
std::unique_ptr< PDBSymbol > findSymbolBySectOffset(uint32_t Sect, uint32_t Offset, PDB_SymType Type) override
std::unique_ptr< PDBSymbolCompiland > findOneCompilandForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
uint32_t getRVAFromSectOffset(uint32_t Section, uint32_t Offset) const
std::unique_ptr< IPDBEnumInjectedSources > getInjectedSources() const override
std::unique_ptr< IPDBEnumTables > getEnumTables() const override
static Error createFromPdbPath(StringRef PdbPath, std::unique_ptr< IPDBSession > &Session)
bool setLoadAddress(uint64_t Address) override
std::unique_ptr< IPDBSourceFile > getSourceFileById(uint32_t FileId) const override
std::unique_ptr< IPDBEnumSectionContribs > getSectionContribs() const override
NativeSession(std::unique_ptr< PDBFile > PdbFile, std::unique_ptr< BumpPtrAllocator > Allocator)
NativeExeSymbol & getNativeGlobalScope() const
std::unique_ptr< IPDBEnumChildren< PDBSymbolCompiland > > findCompilandsForSourceFile(llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByAddress(uint64_t Address, uint32_t Length) const override
std::unique_ptr< PDBSymbol > findSymbolByAddress(uint64_t Address, PDB_SymType Type) override
Expected< ModuleDebugStreamRef > getModuleDebugStream(uint32_t Index) const
std::unique_ptr< IPDBEnumSourceFiles > getAllSourceFiles() const override
bool moduleIndexForVA(uint64_t VA, uint16_t &ModuleIndex) const
std::unique_ptr< IPDBEnumDataStreams > getDebugStreams() const override
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbers(const PDBSymbolCompiland &Compiland, const IPDBSourceFile &File) const override
std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const override
std::unique_ptr< IPDBSourceFile > findOneSourceFile(const PDBSymbolCompiland *Compiland, llvm::StringRef Pattern, PDB_NameSearchFlags Flags) const override
std::unique_ptr< PDBSymbol > findSymbolByRVA(uint32_t RVA, PDB_SymType Type) override
uint64_t getLoadAddress() const override
std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const
NativeRawSymbol & getNativeSymbolById(SymIndexId SymbolId) const
std::unique_ptr< IPDBSourceFile > getSourceFileById(SymIndexId FileId) const
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByVA(uint64_t VA, uint32_t Length) const
SymIndexId createSymbol(Args &&...ConstructorArgs) const
Definition: SymbolCache.h:127
std::unique_ptr< PDBSymbol > findSymbolBySectOffset(uint32_t Sect, uint32_t Offset, PDB_SymType Type)
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static const char Magic[]
Definition: MSFCommon.h:23
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
const uint16_t kInvalidStreamIndex
Definition: RawConstants.h:19
PDB_NameSearchFlags
Defines flags used for enumerating child symbols.
Definition: PDBTypes.h:102
PDB_SymType
These values correspond to the SymTagEnum enumeration, and are documented here: https://msdn....
Definition: PDBTypes.h:243
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition: Path.cpp:474
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:456
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:33
@ Length
Definition: DWP.cpp:406
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1862
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1043
Definition: BitVector.h:851
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20
@ pdb
Windows PDB debug info file.
Definition: Magic.h:53