LLVM 22.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"
22#include "llvm/Support/Error.h"
23
24namespace llvm {
25namespace codeview {
26class LazyRandomTypeCollection;
27}
28namespace object {
29class COFFObjectFile;
30} // namespace object
31
32namespace pdb {
33class InputFile;
34class LinePrinter;
35class PDBFile;
36class NativeSession;
37class SymbolGroupIterator;
38class SymbolGroup;
39
40class InputFile {
41 InputFile();
42
43 std::unique_ptr<NativeSession> PdbSession;
45 std::unique_ptr<MemoryBuffer> UnknownFile;
47
48 using TypeCollectionPtr = std::unique_ptr<codeview::LazyRandomTypeCollection>;
49
50 TypeCollectionPtr Types;
51 TypeCollectionPtr Ids;
52
53 enum TypeCollectionKind { kTypes, kIds };
55 getOrCreateTypeCollection(TypeCollectionKind Kind);
56
57public:
58 InputFile(PDBFile *Pdb);
60 InputFile(MemoryBuffer *Buffer);
63
65 bool AllowUnknownFile = false);
66
68 LLVM_ABI const PDBFile &pdb() const;
70 LLVM_ABI const object::COFFObjectFile &obj() const;
72 LLVM_ABI const MemoryBuffer &unknown() const;
73
75
76 LLVM_ABI bool hasTypes() const;
77 LLVM_ABI bool hasIds() const;
78
81
85
86 LLVM_ABI bool isPdb() const;
87 LLVM_ABI bool isObj() const;
88 LLVM_ABI bool isUnknown() const;
89};
90
92 friend class SymbolGroupIterator;
93
94public:
95 LLVM_ABI explicit SymbolGroup(InputFile *File, uint32_t GroupIndex = 0);
96
99
101 bool Append = false) const;
102
104 bool Append = false) const;
105
106 LLVM_ABI StringRef name() const;
107
109 return Subsections;
110 }
112
113 const InputFile &getFile() const { return *File; }
114 InputFile &getFile() { return *File; }
115
116 bool hasDebugStream() const { return DebugStream != nullptr; }
117
118private:
119 void initializeForPdb(uint32_t Modi);
120 void updatePdbModi(uint32_t Modi);
121 void updateDebugS(const codeview::DebugSubsectionArray &SS);
122
123 void rebuildChecksumMap();
124 InputFile *File = nullptr;
127 std::shared_ptr<ModuleDebugStreamRef> DebugStream;
130};
131
133 : public iterator_facade_base<SymbolGroupIterator,
134 std::forward_iterator_tag, SymbolGroup> {
135public:
137 LLVM_ABI explicit SymbolGroupIterator(InputFile &File);
140
141 LLVM_ABI const SymbolGroup &operator*() const;
143
144 LLVM_ABI bool operator==(const SymbolGroupIterator &R) const;
146
147private:
148 void scanToNextDebugS();
149 bool isEnd() const;
150
151 uint32_t Index = 0;
152 std::optional<object::section_iterator> SectionIter;
154};
155
160
162 const FilterOptions &Filters);
163
164// TODO: Change these callbacks to be function_refs (de-templatify them).
165template <typename CallbackT>
166Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope,
167 const SymbolGroup &SG, uint32_t Modi,
168 CallbackT Callback) {
169 HeaderScope.P.formatLine(
170 "Mod {0:4} | `{1}`: ",
171 fmt_align(Modi, AlignStyle::Right, HeaderScope.LabelWidth), SG.name());
172
173 AutoIndent Indent(HeaderScope);
174 return Callback(Modi, SG);
175}
176
177template <typename CallbackT>
179 CallbackT Callback) {
180 AutoIndent Indent(HeaderScope);
181
182 FilterOptions Filters = HeaderScope.P.getFilters();
183 if (Filters.DumpModi) {
184 uint32_t Modi = *Filters.DumpModi;
185 SymbolGroup SG(&Input, Modi);
186 return iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(Modi)),
187 SG, Modi, Callback);
188 }
189
190 uint32_t I = 0;
191
192 for (const auto &SG : Input.symbol_groups()) {
193 if (shouldDumpSymbolGroup(I, SG, Filters))
194 if (auto Err =
195 iterateOneModule(Input, withLabelWidth(HeaderScope, NumDigits(I)),
196 SG, I, Callback))
197 return Err;
198
199 ++I;
200 }
201 return Error::success();
202}
203
204template <typename SubsectionT>
206 InputFile &File, const PrintScope &HeaderScope,
207 llvm::function_ref<Error(uint32_t, const SymbolGroup &, SubsectionT &)>
208 Callback) {
209
210 return iterateSymbolGroups(
211 File, HeaderScope, [&](uint32_t Modi, const SymbolGroup &SG) -> Error {
212 for (const auto &SS : SG.getDebugSubsections()) {
213 SubsectionT Subsection;
214
215 if (SS.kind() != Subsection.kind())
216 continue;
217
218 BinaryStreamReader Reader(SS.getRecordData());
219 if (auto Err = Subsection.initialize(Reader))
220 continue;
221 if (auto Err = Callback(Modi, SG, Subsection))
222 return Err;
223 }
224 return Error::success();
225 });
226}
227
228} // namespace pdb
229} // namespace llvm
230
231#endif
This file defines the StringMap class.
#define LLVM_ABI
Definition: Compiler.h:213
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
uint32_t Index
#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:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
Tagged union holding either a T or a Error.
Definition: Error.h:485
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:52
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:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
LLVM Value Representation.
Definition: Value.h:75
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.
LLVM_ABI bool hasIds() const
Definition: InputFile.cpp:400
LLVM_ABI MemoryBuffer & unknown()
Definition: InputFile.cpp:369
LLVM_ABI SymbolGroupIterator symbol_groups_begin()
Definition: InputFile.cpp:476
LLVM_ABI iterator_range< SymbolGroupIterator > symbol_groups()
Definition: InputFile.cpp:471
InputFile(InputFile &&Other)=default
LLVM_ABI StringRef getFilePath() const
Definition: InputFile.cpp:379
LLVM_ABI codeview::LazyRandomTypeCollection & types()
Definition: InputFile.cpp:457
static LLVM_ABI Expected< InputFile > open(StringRef Path, bool AllowUnknownFile=false)
Definition: InputFile.cpp:301
LLVM_ABI SymbolGroupIterator symbol_groups_end()
Definition: InputFile.cpp:480
LLVM_ABI bool isUnknown() const
Definition: InputFile.cpp:412
LLVM_ABI PDBFile & pdb()
Definition: InputFile.cpp:349
LLVM_ABI bool hasTypes() const
Definition: InputFile.cpp:388
LLVM_ABI codeview::LazyRandomTypeCollection & ids()
Definition: InputFile.cpp:461
LLVM_ABI object::COFFObjectFile & obj()
Definition: InputFile.cpp:359
LLVM_ABI bool isObj() const
Definition: InputFile.cpp:408
LLVM_ABI bool isPdb() const
Definition: InputFile.cpp:406
const FilterOptions & getFilters() const
Definition: LinePrinter.h:94
void formatLine(const char *Fmt, Ts &&...Items)
Definition: LinePrinter.h:64
LLVM_ABI SymbolGroupIterator & operator++()
Definition: InputFile.cpp:513
SymbolGroupIterator(const SymbolGroupIterator &Other)=default
LLVM_ABI const SymbolGroup & operator*() const
Definition: InputFile.cpp:504
SymbolGroupIterator & operator=(const SymbolGroupIterator &R)=default
LLVM_ABI bool operator==(const SymbolGroupIterator &R) const
Definition: InputFile.cpp:493
LLVM_ABI Expected< StringRef > getNameFromChecksums(uint32_t Offset) const
Definition: InputFile.cpp:239
LLVM_ABI Expected< StringRef > getNameFromStringTable(uint32_t Offset) const
Definition: InputFile.cpp:235
LLVM_ABI void formatFromFileName(LinePrinter &Printer, StringRef File, bool Append=false) const
Definition: InputFile.cpp:259
LLVM_ABI const ModuleDebugStreamRef & getPdbModuleStream() const
Definition: InputFile.cpp:230
LLVM_ABI void formatFromChecksumsOffset(LinePrinter &Printer, uint32_t Offset, bool Append=false) const
Definition: InputFile.cpp:272
codeview::DebugSubsectionArray getDebugSubsections() const
Definition: InputFile.h:108
LLVM_ABI StringRef name() const
Definition: InputFile.cpp:184
bool hasDebugStream() const
Definition: InputFile.h:116
const InputFile & getFile() const
Definition: InputFile.h:113
InputFile & getFile()
Definition: InputFile.h:114
LLVM_ABI 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:130
LLVM_ABI 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:66
Error iterateModuleSubsections(InputFile &File, const PrintScope &HeaderScope, llvm::function_ref< Error(uint32_t, const SymbolGroup &, SubsectionT &)> Callback)
Definition: InputFile.h:205
Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope, const SymbolGroup &SG, uint32_t Modi, CallbackT Callback)
Definition: InputFile.h:166
Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope, CallbackT Callback)
Definition: InputFile.h:178
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:34