SymbolCache.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //==- SymbolCache.h - Cache of native symbols and ids ------------*- 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_NATIVE_SYMBOLCACHE_H
  14. #define LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/DebugInfo/CodeView/CVRecord.h"
  17. #include "llvm/DebugInfo/CodeView/CodeView.h"
  18. #include "llvm/DebugInfo/CodeView/Line.h"
  19. #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
  20. #include "llvm/DebugInfo/CodeView/TypeIndex.h"
  21. #include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h"
  22. #include "llvm/DebugInfo/PDB/Native/NativeSourceFile.h"
  23. #include "llvm/DebugInfo/PDB/PDBTypes.h"
  24. #include <memory>
  25. #include <vector>
  26. namespace llvm {
  27. namespace codeview {
  28. class InlineSiteSym;
  29. struct FileChecksumEntry;
  30. } // namespace codeview
  31. namespace pdb {
  32. class IPDBSourceFile;
  33. class NativeSession;
  34. class PDBSymbol;
  35. class PDBSymbolCompiland;
  36. class DbiStream;
  37. class SymbolCache {
  38. NativeSession &Session;
  39. DbiStream *Dbi = nullptr;
  40. /// Cache of all stable symbols, indexed by SymIndexId. Just because a
  41. /// symbol has been parsed does not imply that it will be stable and have
  42. /// an Id. Id allocation is an implementation, with the only guarantee
  43. /// being that once an Id is allocated, the symbol can be assumed to be
  44. /// cached.
  45. mutable std::vector<std::unique_ptr<NativeRawSymbol>> Cache;
  46. /// For type records from the TPI stream which have been paresd and cached,
  47. /// stores a mapping to SymIndexId of the cached symbol.
  48. mutable DenseMap<codeview::TypeIndex, SymIndexId> TypeIndexToSymbolId;
  49. /// For field list members which have been parsed and cached, stores a mapping
  50. /// from (IndexOfClass, MemberIndex) to the corresponding SymIndexId of the
  51. /// cached symbol.
  52. mutable DenseMap<std::pair<codeview::TypeIndex, uint32_t>, SymIndexId>
  53. FieldListMembersToSymbolId;
  54. /// List of SymIndexIds for each compiland, indexed by compiland index as they
  55. /// appear in the PDB file.
  56. mutable std::vector<SymIndexId> Compilands;
  57. /// List of source files, indexed by unique source file index.
  58. mutable std::vector<std::unique_ptr<NativeSourceFile>> SourceFiles;
  59. /// Map from string table offset to source file Id.
  60. mutable DenseMap<uint32_t, SymIndexId> FileNameOffsetToId;
  61. /// Map from global symbol offset to SymIndexId.
  62. mutable DenseMap<uint32_t, SymIndexId> GlobalOffsetToSymbolId;
  63. /// Map from segment and code offset to function symbols.
  64. mutable DenseMap<std::pair<uint32_t, uint32_t>, SymIndexId> AddressToSymbolId;
  65. /// Map from segment and code offset to public symbols.
  66. mutable DenseMap<std::pair<uint32_t, uint32_t>, SymIndexId>
  67. AddressToPublicSymId;
  68. /// Map from module index and symbol table offset to SymIndexId.
  69. mutable DenseMap<std::pair<uint16_t, uint32_t>, SymIndexId>
  70. SymTabOffsetToSymbolId;
  71. struct LineTableEntry {
  72. uint64_t Addr;
  73. codeview::LineInfo Line;
  74. uint32_t ColumnNumber;
  75. uint32_t FileNameIndex;
  76. bool IsTerminalEntry;
  77. };
  78. std::vector<LineTableEntry> findLineTable(uint16_t Modi) const;
  79. mutable DenseMap<uint16_t, std::vector<LineTableEntry>> LineTable;
  80. SymIndexId createSymbolPlaceholder() const {
  81. SymIndexId Id = Cache.size();
  82. Cache.push_back(nullptr);
  83. return Id;
  84. }
  85. template <typename ConcreteSymbolT, typename CVRecordT, typename... Args>
  86. SymIndexId createSymbolForType(codeview::TypeIndex TI, codeview::CVType CVT,
  87. Args &&...ConstructorArgs) const {
  88. CVRecordT Record;
  89. if (auto EC =
  90. codeview::TypeDeserializer::deserializeAs<CVRecordT>(CVT, Record)) {
  91. consumeError(std::move(EC));
  92. return 0;
  93. }
  94. return createSymbol<ConcreteSymbolT>(
  95. TI, std::move(Record), std::forward<Args>(ConstructorArgs)...);
  96. }
  97. SymIndexId createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
  98. codeview::CVType CVT) const;
  99. SymIndexId createSimpleType(codeview::TypeIndex TI,
  100. codeview::ModifierOptions Mods) const;
  101. std::unique_ptr<PDBSymbol> findFunctionSymbolBySectOffset(uint32_t Sect,
  102. uint32_t Offset);
  103. std::unique_ptr<PDBSymbol> findPublicSymbolBySectOffset(uint32_t Sect,
  104. uint32_t Offset);
  105. public:
  106. SymbolCache(NativeSession &Session, DbiStream *Dbi);
  107. template <typename ConcreteSymbolT, typename... Args>
  108. SymIndexId createSymbol(Args &&...ConstructorArgs) const {
  109. SymIndexId Id = Cache.size();
  110. // Initial construction must not access the cache, since it must be done
  111. // atomically.
  112. auto Result = std::make_unique<ConcreteSymbolT>(
  113. Session, Id, std::forward<Args>(ConstructorArgs)...);
  114. Result->SymbolId = Id;
  115. NativeRawSymbol *NRS = static_cast<NativeRawSymbol *>(Result.get());
  116. Cache.push_back(std::move(Result));
  117. // After the item is in the cache, we can do further initialization which
  118. // is then allowed to access the cache.
  119. NRS->initialize();
  120. return Id;
  121. }
  122. std::unique_ptr<IPDBEnumSymbols>
  123. createTypeEnumerator(codeview::TypeLeafKind Kind);
  124. std::unique_ptr<IPDBEnumSymbols>
  125. createTypeEnumerator(std::vector<codeview::TypeLeafKind> Kinds);
  126. std::unique_ptr<IPDBEnumSymbols>
  127. createGlobalsEnumerator(codeview::SymbolKind Kind);
  128. SymIndexId findSymbolByTypeIndex(codeview::TypeIndex TI) const;
  129. template <typename ConcreteSymbolT, typename... Args>
  130. SymIndexId getOrCreateFieldListMember(codeview::TypeIndex FieldListTI,
  131. uint32_t Index,
  132. Args &&... ConstructorArgs) {
  133. SymIndexId SymId = Cache.size();
  134. std::pair<codeview::TypeIndex, uint32_t> Key{FieldListTI, Index};
  135. auto Result = FieldListMembersToSymbolId.try_emplace(Key, SymId);
  136. if (Result.second)
  137. SymId =
  138. createSymbol<ConcreteSymbolT>(std::forward<Args>(ConstructorArgs)...);
  139. else
  140. SymId = Result.first->second;
  141. return SymId;
  142. }
  143. SymIndexId getOrCreateGlobalSymbolByOffset(uint32_t Offset);
  144. SymIndexId getOrCreateInlineSymbol(codeview::InlineSiteSym Sym,
  145. uint64_t ParentAddr, uint16_t Modi,
  146. uint32_t RecordOffset) const;
  147. std::unique_ptr<PDBSymbol>
  148. findSymbolBySectOffset(uint32_t Sect, uint32_t Offset, PDB_SymType Type);
  149. std::unique_ptr<IPDBEnumLineNumbers>
  150. findLineNumbersByVA(uint64_t VA, uint32_t Length) const;
  151. std::unique_ptr<PDBSymbolCompiland> getOrCreateCompiland(uint32_t Index);
  152. uint32_t getNumCompilands() const;
  153. std::unique_ptr<PDBSymbol> getSymbolById(SymIndexId SymbolId) const;
  154. NativeRawSymbol &getNativeSymbolById(SymIndexId SymbolId) const;
  155. template <typename ConcreteT>
  156. ConcreteT &getNativeSymbolById(SymIndexId SymbolId) const {
  157. return static_cast<ConcreteT &>(getNativeSymbolById(SymbolId));
  158. }
  159. std::unique_ptr<IPDBSourceFile> getSourceFileById(SymIndexId FileId) const;
  160. SymIndexId
  161. getOrCreateSourceFile(const codeview::FileChecksumEntry &Checksum) const;
  162. };
  163. } // namespace pdb
  164. } // namespace llvm
  165. #endif
  166. #ifdef __GNUC__
  167. #pragma GCC diagnostic pop
  168. #endif