LLVM  15.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/Optional.h"
13 #include "llvm/ADT/PointerUnion.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/iterator.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Error.h"
23 
24 namespace llvm {
25 namespace codeview {
26 class LazyRandomTypeCollection;
27 }
28 namespace object {
29 class COFFObjectFile;
30 } // namespace object
31 
32 namespace pdb {
33 class InputFile;
34 class LinePrinter;
35 class PDBFile;
36 class NativeSession;
37 class SymbolGroupIterator;
38 class SymbolGroup;
39 
40 class 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 
57 public:
58  InputFile(PDBFile *Pdb) { PdbOrObj = Pdb; }
59  InputFile(object::COFFObjectFile *Obj) { PdbOrObj = Obj; }
60  InputFile(MemoryBuffer *Buffer) { PdbOrObj = Buffer; }
61  ~InputFile();
62  InputFile(InputFile &&Other) = default;
63 
65  bool AllowUnknownFile = false);
66 
67  PDBFile &pdb();
68  const PDBFile &pdb() const;
70  const object::COFFObjectFile &obj() const;
72  const MemoryBuffer &unknown() const;
73 
74  StringRef getFilePath() const;
75 
76  bool hasTypes() const;
77  bool hasIds() const;
78 
81 
85 
86  bool isPdb() const;
87  bool isObj() const;
88  bool isUnknown() const;
89 };
90 
91 class SymbolGroup {
92  friend class SymbolGroupIterator;
93 
94 public:
95  explicit SymbolGroup(InputFile *File, uint32_t GroupIndex = 0);
96 
99 
101  bool Append = false) const;
102 
104  bool Append = false) const;
105 
106  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 
118 private:
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;
125  StringRef Name;
126  codeview::DebugSubsectionArray Subsections;
127  std::shared_ptr<ModuleDebugStreamRef> DebugStream;
130 };
131 
133  : public iterator_facade_base<SymbolGroupIterator,
134  std::forward_iterator_tag, SymbolGroup> {
135 public:
137  explicit SymbolGroupIterator(InputFile &File);
140 
141  const SymbolGroup &operator*() const;
143 
144  bool operator==(const SymbolGroupIterator &R) const;
146 
147 private:
148  void scanToNextDebugS();
149  bool isEnd() const;
150 
151  uint32_t Index = 0;
154 };
155 
159  uint32_t Index);
160 
161 bool shouldDumpSymbolGroup(uint32_t Idx, const SymbolGroup &Group,
162  const FilterOptions &Filters);
163 
164 // TODO: Change these callbacks to be function_refs (de-templatify them).
165 template <typename CallbackT>
166 Error 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 
177 template <typename CallbackT>
178 Error iterateSymbolGroups(InputFile &Input, const PrintScope &HeaderScope,
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 
204 template <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
FilterOptions::DumpModi
llvm::Optional< uint32_t > DumpModi
Definition: LinePrinter.h:33
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:17
Optional.h
llvm::pdb::InputFile::InputFile
InputFile(MemoryBuffer *Buffer)
Definition: InputFile.h:60
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:329
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
llvm::Optional
Definition: APInt.h:33
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
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:50
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
Printer
print alias Alias Set Printer
Definition: AliasSetTracker.cpp:733
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:418
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:49
llvm::pdb::InputFile::isObj
bool isObj() const
Definition: InputFile.cpp:407
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::pdb::SymbolGroup::getDebugSubsections
codeview::DebugSubsectionArray getDebugSubsections() const
Definition: InputFile.h:108
StringMap.h
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:178
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
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::X86AS::SS
@ SS
Definition: X86.h:197
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:59
ObjectFile.h
ModuleName
Definition: ItaniumDemangle.h:989
llvm::pdb::SymbolGroupIterator
Definition: InputFile.h:132
PointerUnion.h
llvm::pdb::iterateOneModule
Error iterateOneModule(InputFile &File, const PrintScope &HeaderScope, const SymbolGroup &SG, uint32_t Modi, CallbackT Callback)
Definition: InputFile.h:166
llvm::pdb::SymbolGroup::getFile
InputFile & getFile()
Definition: InputFile.h:114
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:58
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:155
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:58
llvm::object::COFFObjectFile
Definition: COFF.h:799
llvm::pdb::iterateModuleSubsections
Error iterateModuleSubsections(InputFile &File, const PrintScope &HeaderScope, llvm::function_ref< Error(uint32_t, const SymbolGroup &, SubsectionT &)> Callback)
Definition: InputFile.h:205
LinePrinter.h
llvm::pdb::SymbolGroup::name
StringRef name() const
Definition: InputFile.cpp:183
ModuleDebugStream.h
Binary.h
llvm::pdb::InputFile
Definition: InputFile.h:40
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:116
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
llvm::pdb::SymbolGroup::getFile
const InputFile & getFile() const
Definition: InputFile.h:113
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:91
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1238