1*0b57cec5SDimitry Andric //===- CVSymbolVisitor.cpp --------------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CVSymbolVisitor.h"
10*0b57cec5SDimitry Andric
11*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
12*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h"
14*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
15*0b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamArray.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
17*0b57cec5SDimitry Andric
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric using namespace llvm::codeview;
20*0b57cec5SDimitry Andric
CVSymbolVisitor(SymbolVisitorCallbacks & Callbacks)21*0b57cec5SDimitry Andric CVSymbolVisitor::CVSymbolVisitor(SymbolVisitorCallbacks &Callbacks)
22*0b57cec5SDimitry Andric : Callbacks(Callbacks) {}
23*0b57cec5SDimitry Andric
24*0b57cec5SDimitry Andric template <typename T>
visitKnownRecord(CVSymbol & Record,SymbolVisitorCallbacks & Callbacks)25*0b57cec5SDimitry Andric static Error visitKnownRecord(CVSymbol &Record,
26*0b57cec5SDimitry Andric SymbolVisitorCallbacks &Callbacks) {
27*0b57cec5SDimitry Andric SymbolRecordKind RK = static_cast<SymbolRecordKind>(Record.kind());
28*0b57cec5SDimitry Andric T KnownRecord(RK);
29*0b57cec5SDimitry Andric if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord))
30*0b57cec5SDimitry Andric return EC;
31*0b57cec5SDimitry Andric return Error::success();
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric
finishVisitation(CVSymbol & Record,SymbolVisitorCallbacks & Callbacks)34*0b57cec5SDimitry Andric static Error finishVisitation(CVSymbol &Record,
35*0b57cec5SDimitry Andric SymbolVisitorCallbacks &Callbacks) {
36*0b57cec5SDimitry Andric switch (Record.kind()) {
37*0b57cec5SDimitry Andric default:
38*0b57cec5SDimitry Andric if (auto EC = Callbacks.visitUnknownSymbol(Record))
39*0b57cec5SDimitry Andric return EC;
40*0b57cec5SDimitry Andric break;
41*0b57cec5SDimitry Andric #define SYMBOL_RECORD(EnumName, EnumVal, Name) \
42*0b57cec5SDimitry Andric case EnumName: { \
43*0b57cec5SDimitry Andric if (auto EC = visitKnownRecord<Name>(Record, Callbacks)) \
44*0b57cec5SDimitry Andric return EC; \
45*0b57cec5SDimitry Andric break; \
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \
48*0b57cec5SDimitry Andric SYMBOL_RECORD(EnumVal, EnumVal, AliasName)
49*0b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
50*0b57cec5SDimitry Andric }
51*0b57cec5SDimitry Andric
52*0b57cec5SDimitry Andric if (auto EC = Callbacks.visitSymbolEnd(Record))
53*0b57cec5SDimitry Andric return EC;
54*0b57cec5SDimitry Andric
55*0b57cec5SDimitry Andric return Error::success();
56*0b57cec5SDimitry Andric }
57*0b57cec5SDimitry Andric
visitSymbolRecord(CVSymbol & Record)58*0b57cec5SDimitry Andric Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record) {
59*0b57cec5SDimitry Andric if (auto EC = Callbacks.visitSymbolBegin(Record))
60*0b57cec5SDimitry Andric return EC;
61*0b57cec5SDimitry Andric return finishVisitation(Record, Callbacks);
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric
visitSymbolRecord(CVSymbol & Record,uint32_t Offset)64*0b57cec5SDimitry Andric Error CVSymbolVisitor::visitSymbolRecord(CVSymbol &Record, uint32_t Offset) {
65*0b57cec5SDimitry Andric if (auto EC = Callbacks.visitSymbolBegin(Record, Offset))
66*0b57cec5SDimitry Andric return EC;
67*0b57cec5SDimitry Andric return finishVisitation(Record, Callbacks);
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric
visitSymbolStream(const CVSymbolArray & Symbols)70*0b57cec5SDimitry Andric Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols) {
71*0b57cec5SDimitry Andric for (auto I : Symbols) {
72*0b57cec5SDimitry Andric if (auto EC = visitSymbolRecord(I))
73*0b57cec5SDimitry Andric return EC;
74*0b57cec5SDimitry Andric }
75*0b57cec5SDimitry Andric return Error::success();
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric
visitSymbolStream(const CVSymbolArray & Symbols,uint32_t InitialOffset)78*0b57cec5SDimitry Andric Error CVSymbolVisitor::visitSymbolStream(const CVSymbolArray &Symbols,
79*0b57cec5SDimitry Andric uint32_t InitialOffset) {
80*0b57cec5SDimitry Andric for (auto I : Symbols) {
81*0b57cec5SDimitry Andric if (auto EC = visitSymbolRecord(I, InitialOffset + Symbols.skew()))
82*0b57cec5SDimitry Andric return EC;
83 InitialOffset += I.length();
84 }
85 return Error::success();
86 }
87
visitSymbolStreamFiltered(const CVSymbolArray & Symbols,const FilterOptions & Filter)88 Error CVSymbolVisitor::visitSymbolStreamFiltered(const CVSymbolArray &Symbols,
89 const FilterOptions &Filter) {
90 if (!Filter.SymbolOffset)
91 return visitSymbolStream(Symbols);
92 uint32_t SymbolOffset = *Filter.SymbolOffset;
93 uint32_t ParentRecurseDepth = Filter.ParentRecursiveDepth.value_or(0);
94 uint32_t ChildrenRecurseDepth = Filter.ChildRecursiveDepth.value_or(0);
95 if (!Symbols.isOffsetValid(SymbolOffset))
96 return createStringError(inconvertibleErrorCode(), "Invalid symbol offset");
97 CVSymbol Sym = *Symbols.at(SymbolOffset);
98 uint32_t SymEndOffset =
99 symbolOpensScope(Sym.kind()) ? getScopeEndOffset(Sym) : 0;
100
101 std::vector<uint32_t> ParentOffsets;
102 std::vector<uint32_t> ParentEndOffsets;
103 uint32_t ChildrenDepth = 0;
104 for (auto Begin = Symbols.begin(), End = Symbols.end(); Begin != End;
105 ++Begin) {
106 uint32_t BeginOffset = Begin.offset();
107 CVSymbol BeginSym = *Begin;
108 if (BeginOffset < SymbolOffset) {
109 if (symbolOpensScope(Begin->kind())) {
110 uint32_t EndOffset = getScopeEndOffset(BeginSym);
111 if (SymbolOffset < EndOffset) {
112 ParentOffsets.push_back(BeginOffset);
113 ParentEndOffsets.push_back(EndOffset);
114 }
115 }
116 } else if (BeginOffset == SymbolOffset) {
117 // Found symbol at offset. Visit its parent up to ParentRecurseDepth.
118 if (ParentRecurseDepth >= ParentOffsets.size())
119 ParentRecurseDepth = ParentOffsets.size();
120 uint32_t StartIndex = ParentOffsets.size() - ParentRecurseDepth;
121 while (StartIndex < ParentOffsets.size()) {
122 if (!Symbols.isOffsetValid(ParentOffsets[StartIndex]))
123 break;
124 CVSymbol Parent = *Symbols.at(ParentOffsets[StartIndex]);
125 if (auto EC = visitSymbolRecord(Parent, ParentOffsets[StartIndex]))
126 return EC;
127 ++StartIndex;
128 }
129 if (auto EC = visitSymbolRecord(Sym, SymbolOffset))
130 return EC;
131 } else if (BeginOffset <= SymEndOffset) {
132 if (ChildrenRecurseDepth) {
133 // Visit children.
134 if (symbolEndsScope(Begin->kind()))
135 --ChildrenDepth;
136 if (ChildrenDepth < ChildrenRecurseDepth ||
137 BeginOffset == SymEndOffset) {
138 if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
139 return EC;
140 }
141 if (symbolOpensScope(Begin->kind()))
142 ++ChildrenDepth;
143 }
144 } else {
145 // Visit parents' ends.
146 if (ParentRecurseDepth && BeginOffset == ParentEndOffsets.back()) {
147 if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
148 return EC;
149 ParentEndOffsets.pop_back();
150 --ParentRecurseDepth;
151 }
152 }
153 }
154 return Error::success();
155 }
156