SymbolCache.h 7.3 KB

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