1bc980340SAmy Huang //===- NativeInlineSiteSymbol.cpp - info about inline sites -----*- C++ -*-===//
2bc980340SAmy Huang //
3bc980340SAmy Huang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bc980340SAmy Huang // See https://llvm.org/LICENSE.txt for license information.
5bc980340SAmy Huang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bc980340SAmy Huang //
7bc980340SAmy Huang //===----------------------------------------------------------------------===//
8bc980340SAmy Huang
9bc980340SAmy Huang #include "llvm/DebugInfo/PDB/Native/NativeInlineSiteSymbol.h"
10bc980340SAmy Huang
11bc980340SAmy Huang #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
12bc980340SAmy Huang #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
13bc980340SAmy Huang #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
14bc980340SAmy Huang #include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
15eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
16bc980340SAmy Huang #include "llvm/DebugInfo/PDB/Native/NativeEnumLineNumbers.h"
17ed98c1b3Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/NativeLineNumber.h"
18eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
19eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20ed98c1b3Sserge-sans-paille #include "llvm/DebugInfo/PDB/Native/SymbolCache.h"
21bc980340SAmy Huang #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
22eb4c8608Sserge-sans-paille #include "llvm/DebugInfo/PDB/PDBExtras.h"
23bc980340SAmy Huang
24bc980340SAmy Huang using namespace llvm;
25bc980340SAmy Huang using namespace llvm::codeview;
26bc980340SAmy Huang using namespace llvm::pdb;
27bc980340SAmy Huang
NativeInlineSiteSymbol(NativeSession & Session,SymIndexId Id,const codeview::InlineSiteSym & Sym,uint64_t ParentAddr)28bc980340SAmy Huang NativeInlineSiteSymbol::NativeInlineSiteSymbol(
29bc980340SAmy Huang NativeSession &Session, SymIndexId Id, const codeview::InlineSiteSym &Sym,
30bc980340SAmy Huang uint64_t ParentAddr)
31bc980340SAmy Huang : NativeRawSymbol(Session, PDB_SymType::InlineSite, Id), Sym(Sym),
32bc980340SAmy Huang ParentAddr(ParentAddr) {}
33bc980340SAmy Huang
343a3cb929SKazu Hirata NativeInlineSiteSymbol::~NativeInlineSiteSymbol() = default;
35bc980340SAmy Huang
dump(raw_ostream & OS,int Indent,PdbSymbolIdField ShowIdFields,PdbSymbolIdField RecurseIdFields) const36bc980340SAmy Huang void NativeInlineSiteSymbol::dump(raw_ostream &OS, int Indent,
37bc980340SAmy Huang PdbSymbolIdField ShowIdFields,
38bc980340SAmy Huang PdbSymbolIdField RecurseIdFields) const {
39bc980340SAmy Huang NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
40bc980340SAmy Huang dumpSymbolField(OS, "name", getName(), Indent);
41bc980340SAmy Huang }
42bc980340SAmy Huang
43*89fab98eSFangrui Song static std::optional<InlineeSourceLine>
findInlineeByTypeIndex(TypeIndex Id,ModuleDebugStreamRef & ModS)44bc980340SAmy Huang findInlineeByTypeIndex(TypeIndex Id, ModuleDebugStreamRef &ModS) {
45bc980340SAmy Huang for (const auto &SS : ModS.getSubsectionsArray()) {
46bc980340SAmy Huang if (SS.kind() != DebugSubsectionKind::InlineeLines)
47bc980340SAmy Huang continue;
48bc980340SAmy Huang
49bc980340SAmy Huang DebugInlineeLinesSubsectionRef InlineeLines;
50bc980340SAmy Huang BinaryStreamReader Reader(SS.getRecordData());
51bc980340SAmy Huang if (auto EC = InlineeLines.initialize(Reader)) {
52bc980340SAmy Huang consumeError(std::move(EC));
53bc980340SAmy Huang continue;
54bc980340SAmy Huang }
55bc980340SAmy Huang
56bc980340SAmy Huang for (const InlineeSourceLine &Line : InlineeLines)
57bc980340SAmy Huang if (Line.Header->Inlinee == Id)
58bc980340SAmy Huang return Line;
59bc980340SAmy Huang }
6011011599SKazu Hirata return std::nullopt;
61bc980340SAmy Huang }
62bc980340SAmy Huang
getName() const63bc980340SAmy Huang std::string NativeInlineSiteSymbol::getName() const {
64bc980340SAmy Huang auto Tpi = Session.getPDBFile().getPDBTpiStream();
65bc980340SAmy Huang if (!Tpi) {
66bc980340SAmy Huang consumeError(Tpi.takeError());
67bc980340SAmy Huang return "";
68bc980340SAmy Huang }
69bc980340SAmy Huang auto Ipi = Session.getPDBFile().getPDBIpiStream();
70bc980340SAmy Huang if (!Ipi) {
71bc980340SAmy Huang consumeError(Ipi.takeError());
72bc980340SAmy Huang return "";
73bc980340SAmy Huang }
74bc980340SAmy Huang
75bc980340SAmy Huang LazyRandomTypeCollection &Types = Tpi->typeCollection();
76bc980340SAmy Huang LazyRandomTypeCollection &Ids = Ipi->typeCollection();
77bc980340SAmy Huang CVType InlineeType = Ids.getType(Sym.Inlinee);
78bc980340SAmy Huang std::string QualifiedName;
79bc980340SAmy Huang if (InlineeType.kind() == LF_MFUNC_ID) {
80bc980340SAmy Huang MemberFuncIdRecord MFRecord;
81bc980340SAmy Huang cantFail(TypeDeserializer::deserializeAs<MemberFuncIdRecord>(InlineeType,
82bc980340SAmy Huang MFRecord));
83bc980340SAmy Huang TypeIndex ClassTy = MFRecord.getClassType();
84bc980340SAmy Huang QualifiedName.append(std::string(Types.getTypeName(ClassTy)));
85bc980340SAmy Huang QualifiedName.append("::");
86bc980340SAmy Huang } else if (InlineeType.kind() == LF_FUNC_ID) {
87bc980340SAmy Huang FuncIdRecord FRecord;
88bc980340SAmy Huang cantFail(
89bc980340SAmy Huang TypeDeserializer::deserializeAs<FuncIdRecord>(InlineeType, FRecord));
90bc980340SAmy Huang TypeIndex ParentScope = FRecord.getParentScope();
91bc980340SAmy Huang if (!ParentScope.isNoneType()) {
92bc980340SAmy Huang QualifiedName.append(std::string(Ids.getTypeName(ParentScope)));
93bc980340SAmy Huang QualifiedName.append("::");
94bc980340SAmy Huang }
95bc980340SAmy Huang }
96bc980340SAmy Huang
97bc980340SAmy Huang QualifiedName.append(std::string(Ids.getTypeName(Sym.Inlinee)));
98bc980340SAmy Huang return QualifiedName;
99bc980340SAmy Huang }
100bc980340SAmy Huang
getLineOffset(uint32_t OffsetInFunc,uint32_t & LineOffset,uint32_t & FileOffset) const101bc980340SAmy Huang void NativeInlineSiteSymbol::getLineOffset(uint32_t OffsetInFunc,
102bc980340SAmy Huang uint32_t &LineOffset,
103bc980340SAmy Huang uint32_t &FileOffset) const {
104bc980340SAmy Huang LineOffset = 0;
105bc980340SAmy Huang FileOffset = 0;
106bc980340SAmy Huang uint32_t CodeOffset = 0;
107*89fab98eSFangrui Song std::optional<uint32_t> CodeOffsetBase;
108*89fab98eSFangrui Song std::optional<uint32_t> CodeOffsetEnd;
109*89fab98eSFangrui Song std::optional<int32_t> CurLineOffset;
110*89fab98eSFangrui Song std::optional<int32_t> NextLineOffset;
111*89fab98eSFangrui Song std::optional<uint32_t> NextFileOffset;
1121da67eceSZequan Wu auto UpdateCodeOffset = [&](uint32_t Delta) {
1131da67eceSZequan Wu if (!CodeOffsetBase)
1141da67eceSZequan Wu CodeOffsetBase = CodeOffset;
1151da67eceSZequan Wu else if (!CodeOffsetEnd)
1161da67eceSZequan Wu CodeOffsetEnd = *CodeOffsetBase + Delta;
1171da67eceSZequan Wu };
1181da67eceSZequan Wu auto UpdateLineOffset = [&](int32_t Delta) {
1191da67eceSZequan Wu LineOffset += Delta;
1201da67eceSZequan Wu if (!CodeOffsetBase || !CurLineOffset)
1211da67eceSZequan Wu CurLineOffset = LineOffset;
1221da67eceSZequan Wu else
1231da67eceSZequan Wu NextLineOffset = LineOffset;
1241da67eceSZequan Wu };
1251da67eceSZequan Wu auto UpdateFileOffset = [&](uint32_t Offset) {
1261da67eceSZequan Wu if (!CodeOffsetBase)
1271da67eceSZequan Wu FileOffset = Offset;
1281da67eceSZequan Wu else
1291da67eceSZequan Wu NextFileOffset = Offset;
1301da67eceSZequan Wu };
1311da67eceSZequan Wu auto ValidateAndReset = [&]() {
1321da67eceSZequan Wu // Current range is finished. Check if OffsetInFunc is in the range.
1331da67eceSZequan Wu if (CodeOffsetBase && CodeOffsetEnd && CurLineOffset) {
1341da67eceSZequan Wu if (CodeOffsetBase <= OffsetInFunc && OffsetInFunc < CodeOffsetEnd) {
1351da67eceSZequan Wu LineOffset = *CurLineOffset;
1361da67eceSZequan Wu return true;
1371da67eceSZequan Wu }
1381da67eceSZequan Wu // Set base, end, file offset and line offset for next range.
1391da67eceSZequan Wu if (NextFileOffset)
1401da67eceSZequan Wu FileOffset = *NextFileOffset;
1411043eeafSZequan Wu if (NextLineOffset) {
1421043eeafSZequan Wu CurLineOffset = NextLineOffset;
14311011599SKazu Hirata NextLineOffset = std::nullopt;
1441043eeafSZequan Wu }
1451da67eceSZequan Wu CodeOffsetBase = CodeOffsetEnd;
14611011599SKazu Hirata CodeOffsetEnd = NextFileOffset = std::nullopt;
1471da67eceSZequan Wu }
1481da67eceSZequan Wu return false;
1491da67eceSZequan Wu };
150bc980340SAmy Huang for (const auto &Annot : Sym.annotations()) {
151bc980340SAmy Huang switch (Annot.OpCode) {
152bc980340SAmy Huang case BinaryAnnotationsOpCode::CodeOffset:
153bc980340SAmy Huang case BinaryAnnotationsOpCode::ChangeCodeOffset:
1541da67eceSZequan Wu case BinaryAnnotationsOpCode::ChangeCodeOffsetBase:
155bc980340SAmy Huang CodeOffset += Annot.U1;
1561da67eceSZequan Wu UpdateCodeOffset(Annot.U1);
1571da67eceSZequan Wu break;
1581da67eceSZequan Wu case BinaryAnnotationsOpCode::ChangeCodeLength:
1591da67eceSZequan Wu UpdateCodeOffset(Annot.U1);
160bc980340SAmy Huang break;
161bc980340SAmy Huang case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset:
162bc980340SAmy Huang CodeOffset += Annot.U2;
1631da67eceSZequan Wu UpdateCodeOffset(Annot.U2);
1641da67eceSZequan Wu UpdateCodeOffset(Annot.U1);
165bc980340SAmy Huang break;
166bc980340SAmy Huang case BinaryAnnotationsOpCode::ChangeLineOffset:
1671da67eceSZequan Wu UpdateLineOffset(Annot.S1);
1681da67eceSZequan Wu break;
169bc980340SAmy Huang case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset:
170bc980340SAmy Huang CodeOffset += Annot.U1;
1711da67eceSZequan Wu UpdateCodeOffset(Annot.U1);
1721da67eceSZequan Wu UpdateLineOffset(Annot.S1);
173bc980340SAmy Huang break;
174bc980340SAmy Huang case BinaryAnnotationsOpCode::ChangeFile:
1751da67eceSZequan Wu UpdateFileOffset(Annot.U1);
176bc980340SAmy Huang break;
177bc980340SAmy Huang default:
178bc980340SAmy Huang break;
179bc980340SAmy Huang }
180bc980340SAmy Huang
1811da67eceSZequan Wu if (ValidateAndReset())
182bc980340SAmy Huang return;
183bc980340SAmy Huang }
184bc980340SAmy Huang }
185bc980340SAmy Huang
186bc980340SAmy Huang std::unique_ptr<IPDBEnumLineNumbers>
findInlineeLinesByVA(uint64_t VA,uint32_t Length) const187bc980340SAmy Huang NativeInlineSiteSymbol::findInlineeLinesByVA(uint64_t VA,
188bc980340SAmy Huang uint32_t Length) const {
189bc980340SAmy Huang uint16_t Modi;
190bc980340SAmy Huang if (!Session.moduleIndexForVA(VA, Modi))
191bc980340SAmy Huang return nullptr;
192bc980340SAmy Huang
193bc980340SAmy Huang Expected<ModuleDebugStreamRef> ModS = Session.getModuleDebugStream(Modi);
194bc980340SAmy Huang if (!ModS) {
195bc980340SAmy Huang consumeError(ModS.takeError());
196bc980340SAmy Huang return nullptr;
197bc980340SAmy Huang }
198bc980340SAmy Huang
199bc980340SAmy Huang Expected<DebugChecksumsSubsectionRef> Checksums =
200bc980340SAmy Huang ModS->findChecksumsSubsection();
201bc980340SAmy Huang if (!Checksums) {
202bc980340SAmy Huang consumeError(Checksums.takeError());
203bc980340SAmy Huang return nullptr;
204bc980340SAmy Huang }
205bc980340SAmy Huang
206bc980340SAmy Huang // Get the line number offset and source file offset.
207bc980340SAmy Huang uint32_t SrcLineOffset;
208bc980340SAmy Huang uint32_t SrcFileOffset;
209bc980340SAmy Huang getLineOffset(VA - ParentAddr, SrcLineOffset, SrcFileOffset);
210bc980340SAmy Huang
211bc980340SAmy Huang // Get line info from inlinee line table.
212*89fab98eSFangrui Song std::optional<InlineeSourceLine> Inlinee =
213bc980340SAmy Huang findInlineeByTypeIndex(Sym.Inlinee, ModS.get());
214bc980340SAmy Huang
215bc980340SAmy Huang if (!Inlinee)
216bc980340SAmy Huang return nullptr;
217bc980340SAmy Huang
218bc980340SAmy Huang uint32_t SrcLine = Inlinee->Header->SourceLineNum + SrcLineOffset;
219bc980340SAmy Huang uint32_t SrcCol = 0; // Inline sites don't seem to have column info.
220bc980340SAmy Huang uint32_t FileChecksumOffset =
221bc980340SAmy Huang (SrcFileOffset == 0) ? Inlinee->Header->FileID : SrcFileOffset;
222bc980340SAmy Huang
223bc980340SAmy Huang auto ChecksumIter = Checksums->getArray().at(FileChecksumOffset);
224bc980340SAmy Huang uint32_t SrcFileId =
225bc980340SAmy Huang Session.getSymbolCache().getOrCreateSourceFile(*ChecksumIter);
226bc980340SAmy Huang
227bc980340SAmy Huang uint32_t LineSect, LineOff;
228bc980340SAmy Huang Session.addressForVA(VA, LineSect, LineOff);
229bc980340SAmy Huang NativeLineNumber LineNum(Session, SrcLine, SrcCol, LineSect, LineOff, Length,
230bc980340SAmy Huang SrcFileId, Modi);
231bc980340SAmy Huang auto SrcFile = Session.getSymbolCache().getSourceFileById(SrcFileId);
232bc980340SAmy Huang std::vector<NativeLineNumber> Lines{LineNum};
233bc980340SAmy Huang
234bc980340SAmy Huang return std::make_unique<NativeEnumLineNumbers>(std::move(Lines));
235bc980340SAmy Huang }
236