PDBSymbol.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PDBSymbol.h - base class for user-facing symbol types -----*- C++-*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_PDB_PDBSYMBOL_H
  14. #define LLVM_DEBUGINFO_PDB_PDBSYMBOL_H
  15. #include "ConcreteSymbolEnumerator.h"
  16. #include "IPDBRawSymbol.h"
  17. #include "PDBExtras.h"
  18. #include "PDBTypes.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/Support/Casting.h"
  21. #define FORWARD_SYMBOL_METHOD(MethodName) \
  22. decltype(auto) MethodName() const { return RawSymbol->MethodName(); }
  23. #define FORWARD_CONCRETE_SYMBOL_ID_METHOD_WITH_NAME(ConcreteType, PrivateName, \
  24. PublicName) \
  25. decltype(auto) PublicName##Id() const { \
  26. return RawSymbol->PrivateName##Id(); \
  27. } \
  28. std::unique_ptr<ConcreteType> PublicName() const { \
  29. uint32_t Id = PublicName##Id(); \
  30. return getConcreteSymbolByIdHelper<ConcreteType>(Id); \
  31. }
  32. #define FORWARD_SYMBOL_ID_METHOD_WITH_NAME(PrivateName, PublicName) \
  33. FORWARD_CONCRETE_SYMBOL_ID_METHOD_WITH_NAME(PDBSymbol, PrivateName, \
  34. PublicName)
  35. #define FORWARD_SYMBOL_ID_METHOD(MethodName) \
  36. FORWARD_SYMBOL_ID_METHOD_WITH_NAME(MethodName, MethodName)
  37. namespace llvm {
  38. class StringRef;
  39. class raw_ostream;
  40. namespace pdb {
  41. class IPDBSession;
  42. #define DECLARE_PDB_SYMBOL_CONCRETE_TYPE(TagValue) \
  43. private: \
  44. using PDBSymbol::PDBSymbol; \
  45. friend class PDBSymbol; \
  46. \
  47. public: \
  48. static const PDB_SymType Tag = TagValue; \
  49. static bool classof(const PDBSymbol *S) { return S->getSymTag() == Tag; }
  50. #define DECLARE_PDB_SYMBOL_CUSTOM_TYPE(Condition) \
  51. private: \
  52. using PDBSymbol::PDBSymbol; \
  53. friend class PDBSymbol; \
  54. \
  55. public: \
  56. static bool classof(const PDBSymbol *S) { return Condition; }
  57. /// PDBSymbol defines the base of the inheritance hierarchy for concrete symbol
  58. /// types (e.g. functions, executables, vtables, etc). All concrete symbol
  59. /// types inherit from PDBSymbol and expose the exact set of methods that are
  60. /// valid for that particular symbol type, as described in the Microsoft
  61. /// reference "Lexical and Class Hierarchy of Symbol Types":
  62. /// https://msdn.microsoft.com/en-us/library/370hs6k4.aspx
  63. class PDBSymbol {
  64. static std::unique_ptr<PDBSymbol> createSymbol(const IPDBSession &PDBSession,
  65. PDB_SymType Tag);
  66. protected:
  67. explicit PDBSymbol(const IPDBSession &PDBSession);
  68. PDBSymbol(PDBSymbol &&Other);
  69. public:
  70. static std::unique_ptr<PDBSymbol>
  71. create(const IPDBSession &PDBSession,
  72. std::unique_ptr<IPDBRawSymbol> RawSymbol);
  73. static std::unique_ptr<PDBSymbol> create(const IPDBSession &PDBSession,
  74. IPDBRawSymbol &RawSymbol);
  75. template <typename ConcreteT>
  76. static std::unique_ptr<ConcreteT>
  77. createAs(const IPDBSession &PDBSession,
  78. std::unique_ptr<IPDBRawSymbol> RawSymbol) {
  79. std::unique_ptr<PDBSymbol> S = create(PDBSession, std::move(RawSymbol));
  80. return unique_dyn_cast_or_null<ConcreteT>(std::move(S));
  81. }
  82. template <typename ConcreteT>
  83. static std::unique_ptr<ConcreteT> createAs(const IPDBSession &PDBSession,
  84. IPDBRawSymbol &RawSymbol) {
  85. std::unique_ptr<PDBSymbol> S = create(PDBSession, RawSymbol);
  86. return unique_dyn_cast_or_null<ConcreteT>(std::move(S));
  87. }
  88. virtual ~PDBSymbol();
  89. /// Dumps the contents of a symbol a raw_ostream. By default this will just
  90. /// call dump() on the underlying RawSymbol, which allows us to discover
  91. /// unknown properties, but individual implementations of PDBSymbol may
  92. /// override the behavior to only dump known fields.
  93. virtual void dump(PDBSymDumper &Dumper) const = 0;
  94. /// For certain PDBSymbolTypes, dumps additional information for the type that
  95. /// normally goes on the right side of the symbol.
  96. virtual void dumpRight(PDBSymDumper &Dumper) const {}
  97. void defaultDump(raw_ostream &OS, int Indent, PdbSymbolIdField ShowFlags,
  98. PdbSymbolIdField RecurseFlags) const;
  99. void dumpProperties() const;
  100. void dumpChildStats() const;
  101. PDB_SymType getSymTag() const;
  102. uint32_t getSymIndexId() const;
  103. template <typename T> std::unique_ptr<T> findOneChild() const {
  104. auto Enumerator(findAllChildren<T>());
  105. if (!Enumerator)
  106. return nullptr;
  107. return Enumerator->getNext();
  108. }
  109. template <typename T>
  110. std::unique_ptr<ConcreteSymbolEnumerator<T>> findAllChildren() const {
  111. auto BaseIter = RawSymbol->findChildren(T::Tag);
  112. if (!BaseIter)
  113. return nullptr;
  114. return std::make_unique<ConcreteSymbolEnumerator<T>>(std::move(BaseIter));
  115. }
  116. std::unique_ptr<IPDBEnumSymbols> findAllChildren(PDB_SymType Type) const;
  117. std::unique_ptr<IPDBEnumSymbols> findAllChildren() const;
  118. std::unique_ptr<IPDBEnumSymbols>
  119. findChildren(PDB_SymType Type, StringRef Name,
  120. PDB_NameSearchFlags Flags) const;
  121. std::unique_ptr<IPDBEnumSymbols> findChildrenByRVA(PDB_SymType Type,
  122. StringRef Name,
  123. PDB_NameSearchFlags Flags,
  124. uint32_t RVA) const;
  125. std::unique_ptr<IPDBEnumSymbols> findInlineFramesByVA(uint64_t VA) const;
  126. std::unique_ptr<IPDBEnumSymbols> findInlineFramesByRVA(uint32_t RVA) const;
  127. std::unique_ptr<IPDBEnumLineNumbers>
  128. findInlineeLinesByVA(uint64_t VA, uint32_t Length) const;
  129. std::unique_ptr<IPDBEnumLineNumbers>
  130. findInlineeLinesByRVA(uint32_t RVA, uint32_t Length) const;
  131. std::string getName() const;
  132. const IPDBRawSymbol &getRawSymbol() const { return *RawSymbol; }
  133. IPDBRawSymbol &getRawSymbol() { return *RawSymbol; }
  134. const IPDBSession &getSession() const { return Session; }
  135. std::unique_ptr<IPDBEnumSymbols> getChildStats(TagStats &Stats) const;
  136. protected:
  137. std::unique_ptr<PDBSymbol> getSymbolByIdHelper(uint32_t Id) const;
  138. template <typename ConcreteType>
  139. std::unique_ptr<ConcreteType> getConcreteSymbolByIdHelper(uint32_t Id) const {
  140. return unique_dyn_cast_or_null<ConcreteType>(getSymbolByIdHelper(Id));
  141. }
  142. const IPDBSession &Session;
  143. std::unique_ptr<IPDBRawSymbol> OwnedRawSymbol;
  144. IPDBRawSymbol *RawSymbol = nullptr;
  145. };
  146. } // namespace llvm
  147. }
  148. #endif
  149. #ifdef __GNUC__
  150. #pragma GCC diagnostic pop
  151. #endif