LLVM 20.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 <cassert>
42#include <memory>
43#include <utility>
44
45using namespace llvm;
46using namespace llvm::msf;
47using namespace llvm::pdb;
48
49namespace llvm {
50namespace codeview {
51union DebugInfo;
52}
53} // namespace llvm
54
56 Expected<DbiStream &> DbiS = File.getPDBDbiStream();
57 if (DbiS)
58 return &DbiS.get();
59
60 consumeError(DbiS.takeError());
61 return nullptr;
62}
63
64NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile,
65 std::unique_ptr<BumpPtrAllocator> Allocator)
66 : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)),
67 Cache(*this, getDbiStreamPtr(*Pdb)), AddrToModuleIndex(IMapAllocator) {}
68
70
71Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer,
72 std::unique_ptr<IPDBSession> &Session) {
73 StringRef Path = Buffer->getBufferIdentifier();
74 auto Stream = std::make_unique<MemoryBufferByteStream>(
75 std::move(Buffer), llvm::endianness::little);
76
77 auto Allocator = std::make_unique<BumpPtrAllocator>();
78 auto File = std::make_unique<PDBFile>(Path, std::move(Stream), *Allocator);
79 if (auto EC = File->parseFileHeaders())
80 return EC;
81 if (auto EC = File->parseStreamData())
82 return EC;
83
84 Session =
85 std::make_unique<NativeSession>(std::move(File), std::move(Allocator));
86
87 return Error::success();
88}
89
91loadPdbFile(StringRef PdbPath, std::unique_ptr<BumpPtrAllocator> &Allocator) {
93 MemoryBuffer::getFile(PdbPath, /*IsText=*/false,
94 /*RequiresNullTerminator=*/false);
95 if (!ErrorOrBuffer)
96 return make_error<RawError>(ErrorOrBuffer.getError());
97 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
98
99 PdbPath = Buffer->getBufferIdentifier();
101 auto EC = identify_magic(PdbPath, Magic);
102 if (EC || Magic != file_magic::pdb)
103 return make_error<RawError>(EC);
104
105 auto Stream = std::make_unique<MemoryBufferByteStream>(
106 std::move(Buffer), llvm::endianness::little);
107
108 auto File = std::make_unique<PDBFile>(PdbPath, std::move(Stream), *Allocator);
109 if (auto EC = File->parseFileHeaders())
110 return std::move(EC);
111
112 if (auto EC = File->parseStreamData())
113 return std::move(EC);
114
115 return std::move(File);
116}
117
119 std::unique_ptr<IPDBSession> &Session) {
120 auto Allocator = std::make_unique<BumpPtrAllocator>();
121 auto PdbFile = loadPdbFile(PdbPath, Allocator);
122 if (!PdbFile)
123 return PdbFile.takeError();
124
125 Session = std::make_unique<NativeSession>(std::move(PdbFile.get()),
126 std::move(Allocator));
127 return Error::success();
128}
129
132 object::createBinary(ExePath);
133 if (!BinaryFile)
134 return BinaryFile.takeError();
135
136 const object::COFFObjectFile *ObjFile =
137 dyn_cast<object::COFFObjectFile>(BinaryFile->getBinary());
138 if (!ObjFile)
139 return make_error<RawError>(raw_error_code::invalid_format);
140
141 StringRef PdbPath;
142 const llvm::codeview::DebugInfo *PdbInfo = nullptr;
143 if (Error E = ObjFile->getDebugPDBInfo(PdbInfo, PdbPath))
144 return std::move(E);
145
146 return std::string(PdbPath);
147}
148
150 std::unique_ptr<IPDBSession> &Session) {
151 Expected<std::string> PdbPath = getPdbPathFromExe(ExePath);
152 if (!PdbPath)
153 return PdbPath.takeError();
154
156 auto EC = identify_magic(PdbPath.get(), Magic);
157 if (EC || Magic != file_magic::pdb)
158 return make_error<RawError>(EC);
159
160 auto Allocator = std::make_unique<BumpPtrAllocator>();
161 auto File = loadPdbFile(PdbPath.get(), Allocator);
162 if (!File)
163 return File.takeError();
164
165 Session = std::make_unique<NativeSession>(std::move(File.get()),
166 std::move(Allocator));
167
168 return Error::success();
169}
170
172NativeSession::searchForPdb(const PdbSearchOptions &Opts) {
173 Expected<std::string> PathOrErr = getPdbPathFromExe(Opts.ExePath);
174 if (!PathOrErr)
175 return PathOrErr.takeError();
176 StringRef PathFromExe = PathOrErr.get();
177 sys::path::Style Style = PathFromExe.starts_with("/")
180 StringRef PdbName = sys::path::filename(PathFromExe, Style);
181
182 // Check if pdb exists in the executable directory.
183 SmallString<128> PdbPath = StringRef(Opts.ExePath);
185 sys::path::append(PdbPath, PdbName);
186
187 auto Allocator = std::make_unique<BumpPtrAllocator>();
188
189 if (auto File = loadPdbFile(PdbPath, Allocator))
190 return std::string(PdbPath);
191 else
192 consumeError(File.takeError());
193
194 // Check path that was in the executable.
195 if (auto File = loadPdbFile(PathFromExe, Allocator))
196 return std::string(PathFromExe);
197 else
198 return File.takeError();
199
200 return make_error<RawError>("PDB not found");
201}
202
203uint64_t NativeSession::getLoadAddress() const { return LoadAddress; }
204
206 LoadAddress = Address;
207 return true;
208}
209
210std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() {
211 return PDBSymbol::createAs<PDBSymbolExe>(*this, getNativeGlobalScope());
212}
213
214std::unique_ptr<PDBSymbol>
216 return Cache.getSymbolById(SymbolId);
217}
218
220 uint32_t &Offset) const {
221 uint32_t RVA = VA - getLoadAddress();
222 return addressForRVA(RVA, Section, Offset);
223}
224
226 uint32_t &Offset) const {
227 Section = 0;
228 Offset = 0;
229
230 auto Dbi = Pdb->getPDBDbiStream();
231 if (!Dbi)
232 return false;
233
234 if ((int32_t)RVA < 0)
235 return true;
236
237 Offset = RVA;
238 for (; Section < Dbi->getSectionHeaders().size(); ++Section) {
239 auto &Sec = Dbi->getSectionHeaders()[Section];
240 if (RVA < Sec.VirtualAddress)
241 return true;
242 Offset = RVA - Sec.VirtualAddress;
243 }
244 return true;
245}
246
247std::unique_ptr<PDBSymbol>
249 uint32_t Section;
251 addressForVA(Address, Section, Offset);
252 return findSymbolBySectOffset(Section, Offset, Type);
253}
254
255std::unique_ptr<PDBSymbol> NativeSession::findSymbolByRVA(uint32_t RVA,
257 uint32_t Section;
259 addressForRVA(RVA, Section, Offset);
260 return findSymbolBySectOffset(Section, Offset, Type);
261}
262
263std::unique_ptr<PDBSymbol>
266 if (AddrToModuleIndex.empty())
267 parseSectionContribs();
268
269 return Cache.findSymbolBySectOffset(Sect, Offset, Type);
270}
271
272std::unique_ptr<IPDBEnumLineNumbers>
274 const IPDBSourceFile &File) const {
275 return nullptr;
276}
277
278std::unique_ptr<IPDBEnumLineNumbers>
280 uint32_t Length) const {
281 return Cache.findLineNumbersByVA(Address, Length);
282}
283
284std::unique_ptr<IPDBEnumLineNumbers>
286 return Cache.findLineNumbersByVA(getLoadAddress() + RVA, Length);
287}
288
289std::unique_ptr<IPDBEnumLineNumbers>
291 uint32_t Length) const {
292 uint64_t VA = getVAFromSectOffset(Section, Offset);
293 return Cache.findLineNumbersByVA(VA, Length);
294}
295
296std::unique_ptr<IPDBEnumSourceFiles>
299 PDB_NameSearchFlags Flags) const {
300 return nullptr;
301}
302
303std::unique_ptr<IPDBSourceFile>
306 PDB_NameSearchFlags Flags) const {
307 return nullptr;
308}
309
310std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>>
312 PDB_NameSearchFlags Flags) const {
313 return nullptr;
314}
315
316std::unique_ptr<PDBSymbolCompiland>
318 PDB_NameSearchFlags Flags) const {
319 return nullptr;
320}
321
322std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const {
323 return nullptr;
324}
325
326std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland(
327 const PDBSymbolCompiland &Compiland) const {
328 return nullptr;
329}
330
331std::unique_ptr<IPDBSourceFile>
333 return Cache.getSourceFileById(FileId);
334}
335
336std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const {
337 return nullptr;
338}
339
340std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const {
341 return nullptr;
342}
343
344std::unique_ptr<IPDBEnumInjectedSources>
346 auto ISS = Pdb->getInjectedSourceStream();
347 if (!ISS) {
348 consumeError(ISS.takeError());
349 return nullptr;
350 }
351 auto Strings = Pdb->getStringTable();
352 if (!Strings) {
353 consumeError(Strings.takeError());
354 return nullptr;
355 }
356 return std::make_unique<NativeEnumInjectedSources>(*Pdb, *ISS, *Strings);
357}
358
359std::unique_ptr<IPDBEnumSectionContribs>
361 return nullptr;
362}
363
364std::unique_ptr<IPDBEnumFrameData>
366 return nullptr;
367}
368
369void NativeSession::initializeExeSymbol() {
370 if (ExeSymbol == 0)
371 ExeSymbol = Cache.createSymbol<NativeExeSymbol>();
372}
373
375 const_cast<NativeSession &>(*this).initializeExeSymbol();
376
377 return Cache.getNativeSymbolById<NativeExeSymbol>(ExeSymbol);
378}
379
381 uint32_t Offset) const {
382 if (Section <= 0)
383 return 0;
384
385 auto Dbi = getDbiStreamPtr(*Pdb);
386 if (!Dbi)
387 return 0;
388
389 uint32_t MaxSection = Dbi->getSectionHeaders().size();
390 if (Section > MaxSection + 1)
391 Section = MaxSection + 1;
392 auto &Sec = Dbi->getSectionHeaders()[Section - 1];
393 return Sec.VirtualAddress + Offset;
394}
395
397 uint32_t Offset) const {
398 return LoadAddress + getRVAFromSectOffset(Section, Offset);
399}
400
402 ModuleIndex = 0;
403 auto Iter = AddrToModuleIndex.find(VA);
404 if (Iter == AddrToModuleIndex.end())
405 return false;
406 ModuleIndex = Iter.value();
407 return true;
408}
409
411 uint16_t &ModuleIndex) const {
412 ModuleIndex = 0;
413 auto Iter = AddrToModuleIndex.find(getVAFromSectOffset(Sect, Offset));
414 if (Iter == AddrToModuleIndex.end())
415 return false;
416 ModuleIndex = Iter.value();
417 return true;
418}
419
420void NativeSession::parseSectionContribs() {
421 auto Dbi = Pdb->getPDBDbiStream();
422 if (!Dbi)
423 return;
424
425 class Visitor : public ISectionContribVisitor {
426 NativeSession &Session;
427 IMap &AddrMap;
428
429 public:
430 Visitor(NativeSession &Session, IMap &AddrMap)
431 : Session(Session), AddrMap(AddrMap) {}
432 void visit(const SectionContrib &C) override {
433 if (C.Size == 0)
434 return;
435
436 uint64_t VA = Session.getVAFromSectOffset(C.ISect, C.Off);
437 uint64_t End = VA + C.Size;
438
439 // Ignore overlapping sections based on the assumption that a valid
440 // PDB file should not have overlaps.
441 if (!AddrMap.overlaps(VA, End))
442 AddrMap.insert(VA, End, C.Imod);
443 }
444 void visit(const SectionContrib2 &C) override { visit(C.Base); }
445 };
446
447 Visitor V(*this, AddrToModuleIndex);
448 Dbi->visitSectionContributions(V);
449}
450
453 auto *Dbi = getDbiStreamPtr(*Pdb);
454 assert(Dbi && "Dbi stream not present");
455
456 DbiModuleDescriptor Modi = Dbi->modules().getModuleDescriptor(Index);
457
458 uint16_t ModiStream = Modi.getModuleStreamIndex();
459 if (ModiStream == kInvalidStreamIndex)
460 return make_error<RawError>("Module stream not present");
461
462 std::unique_ptr<msf::MappedBlockStream> ModStreamData =
463 Pdb->createIndexedStream(ModiStream);
464
465 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
466 if (auto EC = ModS.reload())
467 return std::move(EC);
468
469 return std::move(ModS);
470}
This file defines the BumpPtrAllocator interface.
Lightweight arrays that are backed by an arbitrary BinaryStream.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool End
Definition: ELF_riscv.cpp:480
Provides ErrorOr<T> smart pointer.
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())
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
This file defines the SmallString class.
Represents either an error or a value T.
Definition: ErrorOr.h:56
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
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:51
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
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:480
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
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:1873
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
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:54