NativeInlineSiteSymbol.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //===- NativeInlineSiteSymbol.cpp - info about inline sites -----*- 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. #include "llvm/DebugInfo/PDB/Native/NativeInlineSiteSymbol.h"
  9. #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
  10. #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
  11. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  12. #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
  13. #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
  14. #include "llvm/DebugInfo/PDB/Native/NativeEnumLineNumbers.h"
  15. #include "llvm/DebugInfo/PDB/Native/NativeLineNumber.h"
  16. #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
  17. #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
  18. #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
  19. #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
  20. #include "llvm/DebugInfo/PDB/PDBExtras.h"
  21. using namespace llvm;
  22. using namespace llvm::codeview;
  23. using namespace llvm::pdb;
  24. NativeInlineSiteSymbol::NativeInlineSiteSymbol(
  25. NativeSession &Session, SymIndexId Id, const codeview::InlineSiteSym &Sym,
  26. uint64_t ParentAddr)
  27. : NativeRawSymbol(Session, PDB_SymType::InlineSite, Id), Sym(Sym),
  28. ParentAddr(ParentAddr) {}
  29. NativeInlineSiteSymbol::~NativeInlineSiteSymbol() = default;
  30. void NativeInlineSiteSymbol::dump(raw_ostream &OS, int Indent,
  31. PdbSymbolIdField ShowIdFields,
  32. PdbSymbolIdField RecurseIdFields) const {
  33. NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
  34. dumpSymbolField(OS, "name", getName(), Indent);
  35. }
  36. static std::optional<InlineeSourceLine>
  37. findInlineeByTypeIndex(TypeIndex Id, ModuleDebugStreamRef &ModS) {
  38. for (const auto &SS : ModS.getSubsectionsArray()) {
  39. if (SS.kind() != DebugSubsectionKind::InlineeLines)
  40. continue;
  41. DebugInlineeLinesSubsectionRef InlineeLines;
  42. BinaryStreamReader Reader(SS.getRecordData());
  43. if (auto EC = InlineeLines.initialize(Reader)) {
  44. consumeError(std::move(EC));
  45. continue;
  46. }
  47. for (const InlineeSourceLine &Line : InlineeLines)
  48. if (Line.Header->Inlinee == Id)
  49. return Line;
  50. }
  51. return std::nullopt;
  52. }
  53. std::string NativeInlineSiteSymbol::getName() const {
  54. auto Tpi = Session.getPDBFile().getPDBTpiStream();
  55. if (!Tpi) {
  56. consumeError(Tpi.takeError());
  57. return "";
  58. }
  59. auto Ipi = Session.getPDBFile().getPDBIpiStream();
  60. if (!Ipi) {
  61. consumeError(Ipi.takeError());
  62. return "";
  63. }
  64. LazyRandomTypeCollection &Types = Tpi->typeCollection();
  65. LazyRandomTypeCollection &Ids = Ipi->typeCollection();
  66. CVType InlineeType = Ids.getType(Sym.Inlinee);
  67. std::string QualifiedName;
  68. if (InlineeType.kind() == LF_MFUNC_ID) {
  69. MemberFuncIdRecord MFRecord;
  70. cantFail(TypeDeserializer::deserializeAs<MemberFuncIdRecord>(InlineeType,
  71. MFRecord));
  72. TypeIndex ClassTy = MFRecord.getClassType();
  73. QualifiedName.append(std::string(Types.getTypeName(ClassTy)));
  74. QualifiedName.append("::");
  75. } else if (InlineeType.kind() == LF_FUNC_ID) {
  76. FuncIdRecord FRecord;
  77. cantFail(
  78. TypeDeserializer::deserializeAs<FuncIdRecord>(InlineeType, FRecord));
  79. TypeIndex ParentScope = FRecord.getParentScope();
  80. if (!ParentScope.isNoneType()) {
  81. QualifiedName.append(std::string(Ids.getTypeName(ParentScope)));
  82. QualifiedName.append("::");
  83. }
  84. }
  85. QualifiedName.append(std::string(Ids.getTypeName(Sym.Inlinee)));
  86. return QualifiedName;
  87. }
  88. void NativeInlineSiteSymbol::getLineOffset(uint32_t OffsetInFunc,
  89. uint32_t &LineOffset,
  90. uint32_t &FileOffset) const {
  91. LineOffset = 0;
  92. FileOffset = 0;
  93. uint32_t CodeOffset = 0;
  94. std::optional<uint32_t> CodeOffsetBase;
  95. std::optional<uint32_t> CodeOffsetEnd;
  96. std::optional<int32_t> CurLineOffset;
  97. std::optional<int32_t> NextLineOffset;
  98. std::optional<uint32_t> NextFileOffset;
  99. auto UpdateCodeOffset = [&](uint32_t Delta) {
  100. if (!CodeOffsetBase)
  101. CodeOffsetBase = CodeOffset;
  102. else if (!CodeOffsetEnd)
  103. CodeOffsetEnd = *CodeOffsetBase + Delta;
  104. };
  105. auto UpdateLineOffset = [&](int32_t Delta) {
  106. LineOffset += Delta;
  107. if (!CodeOffsetBase || !CurLineOffset)
  108. CurLineOffset = LineOffset;
  109. else
  110. NextLineOffset = LineOffset;
  111. };
  112. auto UpdateFileOffset = [&](uint32_t Offset) {
  113. if (!CodeOffsetBase)
  114. FileOffset = Offset;
  115. else
  116. NextFileOffset = Offset;
  117. };
  118. auto ValidateAndReset = [&]() {
  119. // Current range is finished. Check if OffsetInFunc is in the range.
  120. if (CodeOffsetBase && CodeOffsetEnd && CurLineOffset) {
  121. if (CodeOffsetBase <= OffsetInFunc && OffsetInFunc < CodeOffsetEnd) {
  122. LineOffset = *CurLineOffset;
  123. return true;
  124. }
  125. // Set base, end, file offset and line offset for next range.
  126. if (NextFileOffset)
  127. FileOffset = *NextFileOffset;
  128. if (NextLineOffset) {
  129. CurLineOffset = NextLineOffset;
  130. NextLineOffset = std::nullopt;
  131. }
  132. CodeOffsetBase = CodeOffsetEnd;
  133. CodeOffsetEnd = NextFileOffset = std::nullopt;
  134. }
  135. return false;
  136. };
  137. for (const auto &Annot : Sym.annotations()) {
  138. switch (Annot.OpCode) {
  139. case BinaryAnnotationsOpCode::CodeOffset:
  140. case BinaryAnnotationsOpCode::ChangeCodeOffset:
  141. case BinaryAnnotationsOpCode::ChangeCodeOffsetBase:
  142. CodeOffset += Annot.U1;
  143. UpdateCodeOffset(Annot.U1);
  144. break;
  145. case BinaryAnnotationsOpCode::ChangeCodeLength:
  146. UpdateCodeOffset(Annot.U1);
  147. break;
  148. case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset:
  149. CodeOffset += Annot.U2;
  150. UpdateCodeOffset(Annot.U2);
  151. UpdateCodeOffset(Annot.U1);
  152. break;
  153. case BinaryAnnotationsOpCode::ChangeLineOffset:
  154. UpdateLineOffset(Annot.S1);
  155. break;
  156. case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset:
  157. CodeOffset += Annot.U1;
  158. UpdateCodeOffset(Annot.U1);
  159. UpdateLineOffset(Annot.S1);
  160. break;
  161. case BinaryAnnotationsOpCode::ChangeFile:
  162. UpdateFileOffset(Annot.U1);
  163. break;
  164. default:
  165. break;
  166. }
  167. if (ValidateAndReset())
  168. return;
  169. }
  170. }
  171. std::unique_ptr<IPDBEnumLineNumbers>
  172. NativeInlineSiteSymbol::findInlineeLinesByVA(uint64_t VA,
  173. uint32_t Length) const {
  174. uint16_t Modi;
  175. if (!Session.moduleIndexForVA(VA, Modi))
  176. return nullptr;
  177. Expected<ModuleDebugStreamRef> ModS = Session.getModuleDebugStream(Modi);
  178. if (!ModS) {
  179. consumeError(ModS.takeError());
  180. return nullptr;
  181. }
  182. Expected<DebugChecksumsSubsectionRef> Checksums =
  183. ModS->findChecksumsSubsection();
  184. if (!Checksums) {
  185. consumeError(Checksums.takeError());
  186. return nullptr;
  187. }
  188. // Get the line number offset and source file offset.
  189. uint32_t SrcLineOffset;
  190. uint32_t SrcFileOffset;
  191. getLineOffset(VA - ParentAddr, SrcLineOffset, SrcFileOffset);
  192. // Get line info from inlinee line table.
  193. std::optional<InlineeSourceLine> Inlinee =
  194. findInlineeByTypeIndex(Sym.Inlinee, ModS.get());
  195. if (!Inlinee)
  196. return nullptr;
  197. uint32_t SrcLine = Inlinee->Header->SourceLineNum + SrcLineOffset;
  198. uint32_t SrcCol = 0; // Inline sites don't seem to have column info.
  199. uint32_t FileChecksumOffset =
  200. (SrcFileOffset == 0) ? Inlinee->Header->FileID : SrcFileOffset;
  201. auto ChecksumIter = Checksums->getArray().at(FileChecksumOffset);
  202. uint32_t SrcFileId =
  203. Session.getSymbolCache().getOrCreateSourceFile(*ChecksumIter);
  204. uint32_t LineSect, LineOff;
  205. Session.addressForVA(VA, LineSect, LineOff);
  206. NativeLineNumber LineNum(Session, SrcLine, SrcCol, LineSect, LineOff, Length,
  207. SrcFileId, Modi);
  208. auto SrcFile = Session.getSymbolCache().getSourceFileById(SrcFileId);
  209. std::vector<NativeLineNumber> Lines{LineNum};
  210. return std::make_unique<NativeEnumLineNumbers>(std::move(Lines));
  211. }