aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/lib/DebugInfo/PDB/Native/NativeInlineSiteSymbol.cpp
blob: 8314353c3890dab6c7634943135d57a5f58b81a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===- NativeInlineSiteSymbol.cpp - info about inline sites -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/PDB/Native/NativeInlineSiteSymbol.h"

#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
#include "llvm/DebugInfo/PDB/Native/NativeEnumLineNumbers.h"
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"

using namespace llvm;
using namespace llvm::codeview;
using namespace llvm::pdb;

NativeInlineSiteSymbol::NativeInlineSiteSymbol(
    NativeSession &Session, SymIndexId Id, const codeview::InlineSiteSym &Sym,
    uint64_t ParentAddr)
    : NativeRawSymbol(Session, PDB_SymType::InlineSite, Id), Sym(Sym),
      ParentAddr(ParentAddr) {}

NativeInlineSiteSymbol::~NativeInlineSiteSymbol() {}

void NativeInlineSiteSymbol::dump(raw_ostream &OS, int Indent,
                                  PdbSymbolIdField ShowIdFields,
                                  PdbSymbolIdField RecurseIdFields) const {
  NativeRawSymbol::dump(OS, Indent, ShowIdFields, RecurseIdFields);
  dumpSymbolField(OS, "name", getName(), Indent);
}

static Optional<InlineeSourceLine>
findInlineeByTypeIndex(TypeIndex Id, ModuleDebugStreamRef &ModS) {
  for (const auto &SS : ModS.getSubsectionsArray()) {
    if (SS.kind() != DebugSubsectionKind::InlineeLines)
      continue;

    DebugInlineeLinesSubsectionRef InlineeLines;
    BinaryStreamReader Reader(SS.getRecordData());
    if (auto EC = InlineeLines.initialize(Reader)) {
      consumeError(std::move(EC));
      continue;
    }

    for (const InlineeSourceLine &Line : InlineeLines)
      if (Line.Header->Inlinee == Id)
        return Line;
  }
  return None;
}

std::string NativeInlineSiteSymbol::getName() const {
  auto Tpi = Session.getPDBFile().getPDBTpiStream();
  if (!Tpi) {
    consumeError(Tpi.takeError());
    return "";
  }
  auto Ipi = Session.getPDBFile().getPDBIpiStream();
  if (!Ipi) {
    consumeError(Ipi.takeError());
    return "";
  }

  LazyRandomTypeCollection &Types = Tpi->typeCollection();
  LazyRandomTypeCollection &Ids = Ipi->typeCollection();
  CVType InlineeType = Ids.getType(Sym.Inlinee);
  std::string QualifiedName;
  if (InlineeType.kind() == LF_MFUNC_ID) {
    MemberFuncIdRecord MFRecord;
    cantFail(TypeDeserializer::deserializeAs<MemberFuncIdRecord>(InlineeType,
                                                                 MFRecord));
    TypeIndex ClassTy = MFRecord.getClassType();
    QualifiedName.append(std::string(Types.getTypeName(ClassTy)));
    QualifiedName.append("::");
  } else if (InlineeType.kind() == LF_FUNC_ID) {
    FuncIdRecord FRecord;
    cantFail(
        TypeDeserializer::deserializeAs<FuncIdRecord>(InlineeType, FRecord));
    TypeIndex ParentScope = FRecord.getParentScope();
    if (!ParentScope.isNoneType()) {
      QualifiedName.append(std::string(Ids.getTypeName(ParentScope)));
      QualifiedName.append("::");
    }
  }

  QualifiedName.append(std::string(Ids.getTypeName(Sym.Inlinee)));
  return QualifiedName;
}

void NativeInlineSiteSymbol::getLineOffset(uint32_t OffsetInFunc,
                                           uint32_t &LineOffset,
                                           uint32_t &FileOffset) const {
  LineOffset = 0;
  FileOffset = 0;
  uint32_t CodeOffset = 0;
  for (const auto &Annot : Sym.annotations()) {
    switch (Annot.OpCode) {
    case BinaryAnnotationsOpCode::CodeOffset:
    case BinaryAnnotationsOpCode::ChangeCodeOffset:
    case BinaryAnnotationsOpCode::ChangeCodeLength:
      CodeOffset += Annot.U1;
      break;
    case BinaryAnnotationsOpCode::ChangeCodeLengthAndCodeOffset:
      CodeOffset += Annot.U2;
      break;
    case BinaryAnnotationsOpCode::ChangeLineOffset:
    case BinaryAnnotationsOpCode::ChangeCodeOffsetAndLineOffset:
      CodeOffset += Annot.U1;
      LineOffset += Annot.S1;
      break;
    case BinaryAnnotationsOpCode::ChangeFile:
      FileOffset = Annot.U1;
      break;
    default:
      break;
    }

    if (CodeOffset >= OffsetInFunc)
      return;
  }
}

std::unique_ptr<IPDBEnumLineNumbers>
NativeInlineSiteSymbol::findInlineeLinesByVA(uint64_t VA,
                                             uint32_t Length) const {
  uint16_t Modi;
  if (!Session.moduleIndexForVA(VA, Modi))
    return nullptr;

  Expected<ModuleDebugStreamRef> ModS = Session.getModuleDebugStream(Modi);
  if (!ModS) {
    consumeError(ModS.takeError());
    return nullptr;
  }

  Expected<DebugChecksumsSubsectionRef> Checksums =
      ModS->findChecksumsSubsection();
  if (!Checksums) {
    consumeError(Checksums.takeError());
    return nullptr;
  }

  // Get the line number offset and source file offset.
  uint32_t SrcLineOffset;
  uint32_t SrcFileOffset;
  getLineOffset(VA - ParentAddr, SrcLineOffset, SrcFileOffset);

  // Get line info from inlinee line table.
  Optional<InlineeSourceLine> Inlinee =
      findInlineeByTypeIndex(Sym.Inlinee, ModS.get());

  if (!Inlinee)
    return nullptr;

  uint32_t SrcLine = Inlinee->Header->SourceLineNum + SrcLineOffset;
  uint32_t SrcCol = 0; // Inline sites don't seem to have column info.
  uint32_t FileChecksumOffset =
      (SrcFileOffset == 0) ? Inlinee->Header->FileID : SrcFileOffset;

  auto ChecksumIter = Checksums->getArray().at(FileChecksumOffset);
  uint32_t SrcFileId =
      Session.getSymbolCache().getOrCreateSourceFile(*ChecksumIter);

  uint32_t LineSect, LineOff;
  Session.addressForVA(VA, LineSect, LineOff);
  NativeLineNumber LineNum(Session, SrcLine, SrcCol, LineSect, LineOff, Length,
                           SrcFileId, Modi);
  auto SrcFile = Session.getSymbolCache().getSourceFileById(SrcFileId);
  std::vector<NativeLineNumber> Lines{LineNum};

  return std::make_unique<NativeEnumLineNumbers>(std::move(Lines));
}