LLVM  17.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 
12 #include "llvm/ADT/PointerUnion.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/iterator.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Support/Error.h"
22 
23 namespace llvm {
24 namespace codeview {
25 class LazyRandomTypeCollection;
26 }
27 namespace object {
28 class COFFObjectFile;
29 } // namespace object
30 
31 namespace pdb {
32 class InputFile;
33 class LinePrinter;
34 class PDBFile;
35 class NativeSession;
36 class SymbolGroupIterator;
37 class SymbolGroup;
38 
39 class 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 
56 public:
57  InputFile(PDBFile *Pdb) { PdbOrObj = Pdb; }
58  InputFile(object::COFFObjectFile *Obj) { PdbOrObj = Obj; }
59  InputFile(MemoryBuffer *Buffer) { PdbOrObj = Buffer; }
60  ~InputFile();
61  InputFile(InputFile &&Other) = default;
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 
90 class SymbolGroup {
91  friend class SymbolGroupIterator;
92 
93 public:
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 
117 private:
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;
124  StringRef Name;
125  codeview::DebugSubsectionArray Subsections;
126  std::shared_ptr<ModuleDebugStreamRef> DebugStream;
129 };
130 
132  : public iterator_facade_base<SymbolGroupIterator,
133  std::forward_iterator_tag, SymbolGroup> {
134 public:
136  explicit SymbolGroupIterator(InputFile &File);
139 
140  const SymbolGroup &operator*() const;
142 
143  bool operator==(const SymbolGroupIterator &R) const;
145 
146 private:
147  void scanToNextDebugS();
148  bool isEnd() const;
149 
150  uint32_t Index = 0;
151  std::optional<object::section_iterator> SectionIter;
153 };
154 
158  uint32_t Index);
159 
160 bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group,
161  const FilterOptions &Filters);
162 
163 // TODO: Change these callbacks to be function_refs (de-templatify them).
164 template <typename CallbackT>
165 Error 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 
176 template <typename CallbackT>
177 Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope,
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 
203 template <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
llvm::pdb::InputFile::types
codeview::LazyRandomTypeCollection & types()
Definition: InputFile.cpp:456
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::pdb::InputFile::InputFile
InputFile(MemoryBuffer *Buffer)
Definition: InputFile.h:59
llvm::pdb::InputFile::isPdb
bool isPdb() const
Definition: InputFile.cpp:405
llvm::pdb::ModuleDebugStreamRef
Definition: ModuleDebugStream.h:31
llvm::pdb::LinePrinter
Definition: LinePrinter.h:50
llvm::pdb::SymbolGroupIterator::operator*
const SymbolGroup & operator*() const
Definition: InputFile.cpp:503
llvm::pdb::NumDigits
int NumDigits(uint64_t N)
Returns the number of digits in the given integer.
Definition: FormatUtil.h:64
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
llvm::pdb::SymbolGroup::getNameFromStringTable
Expected< StringRef > getNameFromStringTable(uint32_t Offset) const
Definition: InputFile.cpp:234
llvm::pdb::SymbolGroup::formatFromFileName
void formatFromFileName(LinePrinter &Printer, StringRef File, bool Append=false) const
Definition: InputFile.cpp:258
llvm::pdb::SymbolGroupIterator::operator==
bool operator==(const SymbolGroupIterator &R) const
Definition: InputFile.cpp:492
llvm::pdb::InputFile::isUnknown
bool isUnknown() const
Definition: InputFile.cpp:411
Error.h
llvm::pdb::InputFile::symbol_groups_begin
SymbolGroupIterator symbol_groups_begin()
Definition: InputFile.cpp:475
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::pdb::SymbolGroup::getNameFromChecksums
Expected< StringRef > getNameFromChecksums(uint32_t Offset) const
Definition: InputFile.cpp:238
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
llvm::pdb::SymbolGroupIterator::operator++
SymbolGroupIterator & operator++()
Definition: InputFile.cpp:512
llvm::pdb::withLabelWidth
PrintScope withLabelWidth(const PrintScope &Scope, uint32_t W)
Definition: LinePrinter.h:128
llvm::codeview::StringsAndChecksumsRef
Definition: StringsAndChecksums.h:23
FilterOptions
Definition: LinePrinter.h:24
llvm::pdb::SymbolGroupIterator::SymbolGroupIterator
SymbolGroupIterator()
Definition: InputFile.cpp:483
DebugChecksumsSubsection.h
llvm::PPCISD::SC
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
Definition: PPCISelLowering.h:420
llvm::pdb::SymbolGroup::SymbolGroup
SymbolGroup(InputFile *File, uint32_t GroupIndex=0)
Definition: InputFile.cpp:156
llvm::pdb::InputFile::ids
codeview::LazyRandomTypeCollection & ids()
Definition: InputFile.cpp:460
llvm::VarStreamArray< DebugSubsectionRecord >
llvm::codeview::LazyRandomTypeCollection
Provides amortized O(1) random access to a CodeView type stream.
Definition: LazyRandomTypeCollection.h:48
llvm::pdb::InputFile::isObj
bool isObj() const
Definition: InputFile.cpp:407
llvm::dwarf::Index
Index
Definition: Dwarf.h:550
llvm::pdb::SymbolGroup::getDebugSubsections
codeview::DebugSubsectionArray getDebugSubsections() const
Definition: InputFile.h:107
llvm::pdb::AutoIndent
Definition: LinePrinter.h:132
llvm::pdb::PrintScope::P
LinePrinter & P
Definition: LinePrinter.h:123
llvm::pdb::iterateSymbolGroups
Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope, CallbackT Callback)
Definition: InputFile.h:177
FilterOptions::DumpModi
std::optional< uint32_t > DumpModi
Definition: LinePrinter.h:33
llvm::StringMap
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:110
llvm::pdb::InputFile::hasTypes
bool hasTypes() const
Definition: InputFile.cpp:387
llvm::pdb::getModuleDebugStream
Expected< ModuleDebugStreamRef > getModuleDebugStream(PDBFile &File, StringRef &ModuleName, uint32_t Index)
Definition: InputFile.cpp:38
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::pdb::InputFile::unknown
MemoryBuffer & unknown()
Definition: InputFile.cpp:368
llvm::pdb::InputFile::pdb
PDBFile & pdb()
Definition: InputFile.cpp:348
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::pdb::LinePrinter::formatLine
void formatLine(const char *Fmt, Ts &&...Items)
Definition: LinePrinter.h:63
llvm::pdb::SymbolGroup::getPdbModuleStream
const ModuleDebugStreamRef & getPdbModuleStream() const
Definition: InputFile.cpp:229
iterator.h
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::pdb::SymbolGroup::formatFromChecksumsOffset
void formatFromChecksumsOffset(LinePrinter &Printer, uint32_t Offset, bool Append=false) const
Definition: InputFile.cpp:271
llvm::iterator_facade_base
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
llvm::pdb::InputFile::InputFile
InputFile(object::COFFObjectFile *Obj)
Definition: InputFile.h:58
ObjectFile.h
ModuleName
Definition: ItaniumDemangle.h:999
llvm::pdb::SymbolGroupIterator
Definition: InputFile.h:131
PointerUnion.h
llvm::pdb::iterateOneModule
Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope, const SymbolGroup &SG, uint32_t Modi, CallbackT Callback)
Definition: InputFile.h:165
llvm::X86AS::SS
@ SS
Definition: X86.h:202
llvm::pdb::SymbolGroup::getFile
InputFile & getFile()
Definition: InputFile.h:113
llvm::pdb::LinePrinter::getFilters
const FilterOptions & getFilters() const
Definition: LinePrinter.h:92
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Printer
Memory true print Memory SSA Printer
Definition: MemorySSA.cpp:78
llvm::pdb::InputFile::getFilePath
StringRef getFilePath() const
Definition: InputFile.cpp:378
uint32_t
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:112
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringsAndChecksums.h
llvm::pdb::shouldDumpSymbolGroup
bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group, const FilterOptions &Filters)
Definition: InputFile.cpp:576
llvm::pdb::InputFile::InputFile
InputFile(PDBFile *Pdb)
Definition: InputFile.h:57
llvm::object::COFFObjectFile
Definition: COFF.h:788
llvm::pdb::iterateModuleSubsections
Error iterateModuleSubsections(InputFile &File, const PrintScope &HeaderScope, llvm::function_ref< Error(uint32_t, const SymbolGroup &, SubsectionT &)> Callback)
Definition: InputFile.h:204
LinePrinter.h
llvm::pdb::SymbolGroup::name
StringRef name() const
Definition: InputFile.cpp:183
ModuleDebugStream.h
Binary.h
Other
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1260
llvm::pdb::InputFile
Definition: InputFile.h:39
llvm::pdb::PDBFile
Definition: PDBFile.h:40
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::pdb::InputFile::obj
object::COFFObjectFile & obj()
Definition: InputFile.cpp:358
llvm::pdb::SymbolGroupIterator::operator=
SymbolGroupIterator & operator=(const SymbolGroupIterator &R)=default
llvm::pdb::InputFile::symbol_groups
iterator_range< SymbolGroupIterator > symbol_groups()
Definition: InputFile.cpp:470
llvm::pdb::SymbolGroup::hasDebugStream
bool hasDebugStream() const
Definition: InputFile.h:115
llvm::pdb::InputFile::~InputFile
~InputFile()
llvm::pdb::PrintScope
Definition: LinePrinter.h:117
llvm::pdb::InputFile::hasIds
bool hasIds() const
Definition: InputFile.cpp:399
llvm::pdb::InputFile::symbol_groups_end
SymbolGroupIterator symbol_groups_end()
Definition: InputFile.cpp:479
StringMap.h
llvm::pdb::SymbolGroup::getFile
const InputFile & getFile() const
Definition: InputFile.h:112
llvm::AlignStyle::Right
@ Right
llvm::ARMBuildAttrs::File
@ File
Definition: ARMBuildAttributes.h:36
llvm::pdb::PrintScope::LabelWidth
uint32_t LabelWidth
Definition: LinePrinter.h:125
llvm::fmt_align
detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
Definition: FormatAdapters.h:86
llvm::pdb::InputFile::open
static Expected< InputFile > open(StringRef Path, bool AllowUnknownFile=false)
Definition: InputFile.cpp:300
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::pdb::SymbolGroup
Definition: InputFile.h:90