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