LLVM  14.0.0git
SymbolCache.h
Go to the documentation of this file.
1 //==- SymbolCache.h - Cache of native symbols and ids ------------*- 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_SYMBOLCACHE_H
10 #define LLVM_DEBUGINFO_PDB_NATIVE_SYMBOLCACHE_H
11 
12 #include "llvm/ADT/DenseMap.h"
13 #include "llvm/ADT/IntervalMap.h"
22 
23 #include <memory>
24 #include <vector>
25 
26 namespace llvm {
27 namespace pdb {
28 class DbiStream;
29 class PDBFile;
30 
31 class SymbolCache {
32  NativeSession &Session;
33  DbiStream *Dbi = nullptr;
34 
35  /// Cache of all stable symbols, indexed by SymIndexId. Just because a
36  /// symbol has been parsed does not imply that it will be stable and have
37  /// an Id. Id allocation is an implementation, with the only guarantee
38  /// being that once an Id is allocated, the symbol can be assumed to be
39  /// cached.
40  mutable std::vector<std::unique_ptr<NativeRawSymbol>> Cache;
41 
42  /// For type records from the TPI stream which have been paresd and cached,
43  /// stores a mapping to SymIndexId of the cached symbol.
44  mutable DenseMap<codeview::TypeIndex, SymIndexId> TypeIndexToSymbolId;
45 
46  /// For field list members which have been parsed and cached, stores a mapping
47  /// from (IndexOfClass, MemberIndex) to the corresponding SymIndexId of the
48  /// cached symbol.
50  FieldListMembersToSymbolId;
51 
52  /// List of SymIndexIds for each compiland, indexed by compiland index as they
53  /// appear in the PDB file.
54  mutable std::vector<SymIndexId> Compilands;
55 
56  /// List of source files, indexed by unique source file index.
57  mutable std::vector<std::unique_ptr<NativeSourceFile>> SourceFiles;
58 
59  /// Map from string table offset to source file Id.
60  mutable DenseMap<uint32_t, SymIndexId> FileNameOffsetToId;
61 
62  /// Map from global symbol offset to SymIndexId.
63  mutable DenseMap<uint32_t, SymIndexId> GlobalOffsetToSymbolId;
64 
65  /// Map from segment and code offset to function symbols.
66  mutable DenseMap<std::pair<uint32_t, uint32_t>, SymIndexId> AddressToSymbolId;
67  /// Map from segment and code offset to public symbols.
69  AddressToPublicSymId;
70 
71  /// Map from module index and symbol table offset to SymIndexId.
73  SymTabOffsetToSymbolId;
74 
75  struct LineTableEntry {
76  uint64_t Addr;
77  codeview::LineInfo Line;
78  uint32_t ColumnNumber;
79  uint32_t FileNameIndex;
80  bool IsTerminalEntry;
81  };
82 
83  std::vector<LineTableEntry> findLineTable(uint16_t Modi) const;
85 
86  SymIndexId createSymbolPlaceholder() const {
87  SymIndexId Id = Cache.size();
88  Cache.push_back(nullptr);
89  return Id;
90  }
91 
92  template <typename ConcreteSymbolT, typename CVRecordT, typename... Args>
93  SymIndexId createSymbolForType(codeview::TypeIndex TI, codeview::CVType CVT,
94  Args &&...ConstructorArgs) const {
95  CVRecordT Record;
96  if (auto EC =
97  codeview::TypeDeserializer::deserializeAs<CVRecordT>(CVT, Record)) {
99  return 0;
100  }
101 
102  return createSymbol<ConcreteSymbolT>(
103  TI, std::move(Record), std::forward<Args>(ConstructorArgs)...);
104  }
105 
106  SymIndexId createSymbolForModifiedType(codeview::TypeIndex ModifierTI,
107  codeview::CVType CVT) const;
108 
109  SymIndexId createSimpleType(codeview::TypeIndex TI,
110  codeview::ModifierOptions Mods) const;
111 
112  std::unique_ptr<PDBSymbol> findFunctionSymbolBySectOffset(uint32_t Sect,
113  uint32_t Offset);
114  std::unique_ptr<PDBSymbol> findPublicSymbolBySectOffset(uint32_t Sect,
115  uint32_t Offset);
116 
117 public:
118  SymbolCache(NativeSession &Session, DbiStream *Dbi);
119 
120  template <typename ConcreteSymbolT, typename... Args>
121  SymIndexId createSymbol(Args &&...ConstructorArgs) const {
122  SymIndexId Id = Cache.size();
123 
124  // Initial construction must not access the cache, since it must be done
125  // atomically.
126  auto Result = std::make_unique<ConcreteSymbolT>(
127  Session, Id, std::forward<Args>(ConstructorArgs)...);
128  Result->SymbolId = Id;
129 
130  NativeRawSymbol *NRS = static_cast<NativeRawSymbol *>(Result.get());
131  Cache.push_back(std::move(Result));
132 
133  // After the item is in the cache, we can do further initialization which
134  // is then allowed to access the cache.
135  NRS->initialize();
136  return Id;
137  }
138 
139  std::unique_ptr<IPDBEnumSymbols>
141 
142  std::unique_ptr<IPDBEnumSymbols>
143  createTypeEnumerator(std::vector<codeview::TypeLeafKind> Kinds);
144 
145  std::unique_ptr<IPDBEnumSymbols>
147 
149 
150  template <typename ConcreteSymbolT, typename... Args>
152  uint32_t Index,
153  Args &&... ConstructorArgs) {
154  SymIndexId SymId = Cache.size();
155  std::pair<codeview::TypeIndex, uint32_t> Key{FieldListTI, Index};
156  auto Result = FieldListMembersToSymbolId.try_emplace(Key, SymId);
157  if (Result.second)
158  SymId =
159  createSymbol<ConcreteSymbolT>(std::forward<Args>(ConstructorArgs)...);
160  else
161  SymId = Result.first->second;
162  return SymId;
163  }
164 
167  uint64_t ParentAddr, uint16_t Modi,
168  uint32_t RecordOffset) const;
169 
170  std::unique_ptr<PDBSymbol>
172 
173  std::unique_ptr<IPDBEnumLineNumbers>
174  findLineNumbersByVA(uint64_t VA, uint32_t Length) const;
175 
176  std::unique_ptr<PDBSymbolCompiland> getOrCreateCompiland(uint32_t Index);
177  uint32_t getNumCompilands() const;
178 
179  std::unique_ptr<PDBSymbol> getSymbolById(SymIndexId SymbolId) const;
180 
182 
183  template <typename ConcreteT>
184  ConcreteT &getNativeSymbolById(SymIndexId SymbolId) const {
185  return static_cast<ConcreteT &>(getNativeSymbolById(SymbolId));
186  }
187 
188  std::unique_ptr<IPDBSourceFile> getSourceFileById(SymIndexId FileId) const;
189  SymIndexId
190  getOrCreateSourceFile(const codeview::FileChecksumEntry &Checksum) const;
191 };
192 
193 } // namespace pdb
194 } // namespace llvm
195 
196 #endif
llvm::pdb::SymbolCache::createTypeEnumerator
std::unique_ptr< IPDBEnumSymbols > createTypeEnumerator(codeview::TypeLeafKind Kind)
Definition: SymbolCache.cpp:84
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::codeview::ModifierOptions
ModifierOptions
Equivalent to CV_modifier_t.
Definition: CodeView.h:302
llvm::codeview::SymbolKind
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:47
SymbolRecord.h
llvm::pdb::SymbolCache::getOrCreateInlineSymbol
SymIndexId getOrCreateInlineSymbol(codeview::InlineSiteSym Sym, uint64_t ParentAddr, uint16_t Modi, uint32_t RecordOffset) const
Definition: SymbolCache.cpp:295
llvm::pdb::SymbolCache::getSourceFileById
std::unique_ptr< IPDBSourceFile > getSourceFileById(SymIndexId FileId) const
Definition: SymbolCache.cpp:609
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
DenseMap.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::pdb::SymbolCache::createSymbol
SymIndexId createSymbol(Args &&...ConstructorArgs) const
Definition: SymbolCache.h:121
llvm::pdb::DbiStream
Definition: DbiStream.h:37
llvm::pdb::SymbolCache
Definition: SymbolCache.h:31
llvm::pdb::SymbolCache::getOrCreateFieldListMember
SymIndexId getOrCreateFieldListMember(codeview::TypeIndex FieldListTI, uint32_t Index, Args &&... ConstructorArgs)
Definition: SymbolCache.h:151
llvm::consumeError
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1035
llvm::pdb::PDB_SymType
PDB_SymType
These values correspond to the SymTagEnum enumeration, and are documented here: https://msdn....
Definition: PDBTypes.h:243
llvm::codeview::InlineSiteSym
Definition: SymbolRecord.h:333
llvm::pdb::SymIndexId
uint32_t SymIndexId
Definition: PDBTypes.h:26
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::codeview::FileChecksumEntry
Definition: DebugChecksumsSubsection.h:31
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::pdb::SymbolCache::getNativeSymbolById
ConcreteT & getNativeSymbolById(SymIndexId SymbolId) const
Definition: SymbolCache.h:184
Line.h
llvm::pdb::SymbolCache::getSymbolById
std::unique_ptr< PDBSymbol > getSymbolById(SymIndexId SymbolId) const
Definition: SymbolCache.cpp:242
llvm::pdb::SymbolCache::findSymbolBySectOffset
std::unique_ptr< PDBSymbol > findSymbolBySectOffset(uint32_t Sect, uint32_t Offset, PDB_SymType Type)
Definition: SymbolCache.cpp:309
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
uint64_t
TypeRecord.h
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::DenseMap
Definition: DenseMap.h:714
llvm::pdb::SymbolCache::getNumCompilands
uint32_t getNumCompilands() const
Definition: SymbolCache.cpp:262
llvm::Record
Definition: Record.h:1472
llvm::codeview::CVRecord< TypeLeafKind >
uint32_t
llvm::codeview::LineInfo
Definition: Line.h:21
llvm::pdb::SymbolCache::findSymbolByTypeIndex
SymIndexId findSymbolByTypeIndex(codeview::TypeIndex TI) const
Definition: SymbolCache.cpp:153
uint16_t
TypeDeserializer.h
NativeSourceFile.h
NativeRawSymbol.h
TypeIndex.h
ModuleDebugStream.h
llvm::pdb::NativeSession
Definition: NativeSession.h:28
llvm::pdb::SymbolCache::getOrCreateCompiland
std::unique_ptr< PDBSymbolCompiland > getOrCreateCompiland(uint32_t Index)
Definition: SymbolCache.cpp:592
llvm::codeview::TypeLeafKind
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Definition: CodeView.h:33
IntervalMap.h
llvm::DenseMapBase< DenseMap< KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >, KeyT, ValueT, DenseMapInfo< KeyT >, llvm::detail::DenseMapPair< KeyT, ValueT > >::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:222
llvm::codeview::TypeIndex
A 32-bit type reference.
Definition: TypeIndex.h:95
llvm::pdb::SymbolCache::getOrCreateGlobalSymbolByOffset
SymIndexId getOrCreateGlobalSymbolByOffset(uint32_t Offset)
Definition: SymbolCache.cpp:269
llvm::pdb::SymbolCache::getNativeSymbolById
NativeRawSymbol & getNativeSymbolById(SymIndexId SymbolId) const
Definition: SymbolCache.cpp:258
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::pdb::SymbolCache::getOrCreateSourceFile
SymIndexId getOrCreateSourceFile(const codeview::FileChecksumEntry &Checksum) const
Definition: SymbolCache.cpp:621
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:228
llvm::pdb::SymbolCache::SymbolCache
SymbolCache(NativeSession &Session, DbiStream *Dbi)
Definition: SymbolCache.cpp:73
llvm::pdb::NativeRawSymbol
Definition: NativeRawSymbol.h:21
llvm::pdb::SymbolCache::findLineNumbersByVA
std::unique_ptr< IPDBEnumLineNumbers > findLineNumbersByVA(uint64_t VA, uint32_t Length) const
Definition: SymbolCache.cpp:528
llvm::pdb::SymbolCache::createGlobalsEnumerator
std::unique_ptr< IPDBEnumSymbols > createGlobalsEnumerator(codeview::SymbolKind Kind)
Definition: SymbolCache.cpp:101