LLVM 19.0.0git
InputFile.h
Go to the documentation of this file.
1//===- InputFile.h -------------------------------------------- *- 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#ifndef LLVM_DEBUGINFO_PDB_NATIVE_INPUTFILE_H
10#define LLVM_DEBUGINFO_PDB_NATIVE_INPUTFILE_H
11
13#include "llvm/ADT/StringMap.h"
14#include "llvm/ADT/iterator.h"
19#include "llvm/Object/Binary.h"
21#include "llvm/Support/Error.h"
22
23namespace llvm {
24namespace codeview {
25class LazyRandomTypeCollection;
26}
27namespace object {
28class COFFObjectFile;
29} // namespace object
30
31namespace pdb {
32class InputFile;
33class LinePrinter;
34class PDBFile;
35class NativeSession;
36class SymbolGroupIterator;
37class SymbolGroup;
38
39class InputFile {
40 InputFile();
41
42 std::unique_ptr<NativeSession> PdbSession;
44 std::unique_ptr<MemoryBuffer> UnknownFile;
46
47 using TypeCollectionPtr = std::unique_ptr<codeview::LazyRandomTypeCollection>;
48
49 TypeCollectionPtr Types;
50 TypeCollectionPtr Ids;
51
52 enum TypeCollectionKind { kTypes, kIds };
54 getOrCreateTypeCollection(TypeCollectionKind Kind);
55
56public:
57 InputFile(PDBFile *Pdb) { PdbOrObj = Pdb; }
58 InputFile(object::COFFObjectFile *Obj) { PdbOrObj = Obj; }
59 InputFile(MemoryBuffer *Buffer) { PdbOrObj = Buffer; }
62
64 bool AllowUnknownFile = false);
65
66 PDBFile &pdb();
67 const PDBFile &pdb() const;
69 const object::COFFObjectFile &obj() const;
71 const MemoryBuffer &unknown() const;
72
73 StringRef getFilePath() const;
74
75 bool hasTypes() const;
76 bool hasIds() const;
77
80
84
85 bool isPdb() const;
86 bool isObj() const;
87 bool isUnknown() const;
88};
89
91 friend class SymbolGroupIterator;
92
93public:
94 explicit SymbolGroup(InputFile *File, uint32_t GroupIndex = 0);
95
98
100 bool Append = false) const;
101
103 bool Append = false) const;
104
105 StringRef name() const;
106
108 return Subsections;
109 }
111
112 const InputFile &getFile() const { return *File; }
113 InputFile &getFile() { return *File; }
114
115 bool hasDebugStream() const { return DebugStream != nullptr; }
116
117private:
118 void initializeForPdb(uint32_t Modi);
119 void updatePdbModi(uint32_t Modi);
120 void updateDebugS(const codeview::DebugSubsectionArray &SS);
121
122 void rebuildChecksumMap();
123 InputFile *File = nullptr;
126 std::shared_ptr<ModuleDebugStreamRef> DebugStream;
129};
130
132 : public iterator_facade_base<SymbolGroupIterator,
133 std::forward_iterator_tag, SymbolGroup> {
134public:
136 explicit SymbolGroupIterator(InputFile &File);
139
140 const SymbolGroup &operator*() const;
142
143 bool operator==(const SymbolGroupIterator &R) const;
145
146private:
147 void scanToNextDebugS();
148 bool isEnd() const;
149
150 uint32_t Index = 0;
151 std::optional<object::section_iterator> SectionIter;
153};
154
159
161 const FilterOptions &Filters);
162
163// TODO: Change these callbacks to be function_refs (de-templatify them).
164template <typename CallbackT>
165Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope,
166 const SymbolGroup &SG, uint32_t Modi,
167 CallbackT Callback) {
168 HeaderScope.P.formatLine(
169 "Mod {0:4} | `{1}`: ",
170 fmt_align(Modi, AlignStyle::Right, HeaderScope.LabelWidth), SG.name());
171
172 AutoIndent Indent(HeaderScope);
173 return Callback(Modi, SG);
174}
175
176template <typename CallbackT>
178 CallbackT Callback) {
179 AutoIndent Indent(HeaderScope);
180
181 FilterOptions Filters = HeaderScope.P.getFilters();
182 if (Filters.DumpModi) {
183 uint32_t Modi = *Filters.DumpModi;
184 SymbolGroup SG(&Input, Modi);
185 return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)),
186 SG, Modi, Callback);
187 }
188
189 uint32_t I = 0;
190
191 for (const auto &SG : Input.symbol_groups()) {
192 if (shouldDumpSymbolGroup(I, SG, Filters))
193 if (auto Err =
194 iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)),
195 SG, I, Callback))
196 return Err;
197
198 ++I;
199 }
200 return Error::success();
201}
202
203template <typename SubsectionT>
205 InputFile &File, const PrintScope &HeaderScope,
206 llvm::function_ref<Error(uint32_t, const SymbolGroup &, SubsectionT &)>
207 Callback) {
208
209 return iterateSymbolGroups(
210 File, HeaderScope, [&](uint32_t Modi, const SymbolGroup &SG) -> Error {
211 for (const auto &SS : SG.getDebugSubsections()) {
212 SubsectionT Subsection;
213
214 if (SS.kind() != Subsection.kind())
215 continue;
216
217 BinaryStreamReader Reader(SS.getRecordData());
218 if (auto Err = Subsection.initialize(Reader))
219 continue;
220 if (auto Err = Callback(Modi, SG, Subsection))
221 return Err;
222 }
223 return Error::success();
224 });
225}
226
227} // namespace pdb
228} // namespace llvm
229
230#endif
This file defines the StringMap class.
dxil pretty DXIL Metadata Pretty Printer
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::string Name
#define I(x, y, z)
Definition: MD5.cpp:58
This file defines the PointerUnion class, which is a discriminated union of pointer types.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:118
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
Provides amortized O(1) random access to a CodeView type stream.
An efficient, type-erasing, non-owning reference to a callable.
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
A range adaptor for a pair of iterators.
bool hasIds() const
Definition: InputFile.cpp:400
MemoryBuffer & unknown()
Definition: InputFile.cpp:369
SymbolGroupIterator symbol_groups_begin()
Definition: InputFile.cpp:476
iterator_range< SymbolGroupIterator > symbol_groups()
Definition: InputFile.cpp:471
InputFile(InputFile &&Other)=default
StringRef getFilePath() const
Definition: InputFile.cpp:379
codeview::LazyRandomTypeCollection & types()
Definition: InputFile.cpp:457
static Expected< InputFile > open(StringRef Path, bool AllowUnknownFile=false)
Definition: InputFile.cpp:301
InputFile(PDBFile *Pdb)
Definition: InputFile.h:57
SymbolGroupIterator symbol_groups_end()
Definition: InputFile.cpp:480
bool isUnknown() const
Definition: InputFile.cpp:412
InputFile(object::COFFObjectFile *Obj)
Definition: InputFile.h:58
bool hasTypes() const
Definition: InputFile.cpp:388
codeview::LazyRandomTypeCollection & ids()
Definition: InputFile.cpp:461
InputFile(MemoryBuffer *Buffer)
Definition: InputFile.h:59
object::COFFObjectFile & obj()
Definition: InputFile.cpp:359
bool isObj() const
Definition: InputFile.cpp:408
bool isPdb() const
Definition: InputFile.cpp:406
const FilterOptions & getFilters() const
Definition: LinePrinter.h:92
void formatLine(const char *Fmt, Ts &&...Items)
Definition: LinePrinter.h:63
SymbolGroupIterator & operator++()
Definition: InputFile.cpp:513
SymbolGroupIterator(const SymbolGroupIterator &Other)=default
const SymbolGroup & operator*() const
Definition: InputFile.cpp:504
SymbolGroupIterator & operator=(const SymbolGroupIterator &R)=default
bool operator==(const SymbolGroupIterator &R) const
Definition: InputFile.cpp:493
Expected< StringRef > getNameFromChecksums(uint32_t Offset) const
Definition: InputFile.cpp:239
Expected< StringRef > getNameFromStringTable(uint32_t Offset) const
Definition: InputFile.cpp:235
void formatFromFileName(LinePrinter &Printer, StringRef File, bool Append=false) const
Definition: InputFile.cpp:259
const ModuleDebugStreamRef & getPdbModuleStream() const
Definition: InputFile.cpp:230
void formatFromChecksumsOffset(LinePrinter &Printer, uint32_t Offset, bool Append=false) const
Definition: InputFile.cpp:272
codeview::DebugSubsectionArray getDebugSubsections() const
Definition: InputFile.h:107
StringRef name() const
Definition: InputFile.cpp:184
bool hasDebugStream() const
Definition: InputFile.h:115
const InputFile & getFile() const
Definition: InputFile.h:112
InputFile & getFile()
Definition: InputFile.h:113
bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group, const FilterOptions &Filters)
Definition: InputFile.cpp:577
PrintScope withLabelWidth(const PrintScope &Scope, uint32_t W)
Definition: LinePrinter.h:128
Expected< ModuleDebugStreamRef > getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index)
Definition: InputFile.cpp:39
int NumDigits(uint64_t N)
Returns the number of digits in the given integer.
Definition: FormatUtil.h:64
Error iterateModuleSubsections(InputFile &File, const PrintScope &HeaderScope, llvm::function_ref< Error(uint32_t, const SymbolGroup &, SubsectionT &)> Callback)
Definition: InputFile.h:204
Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope, const SymbolGroup &SG, uint32_t Modi, CallbackT Callback)
Definition: InputFile.h:165
Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope, CallbackT Callback)
Definition: InputFile.h:177
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
std::optional< uint32_t > DumpModi
Definition: LinePrinter.h:33