LLVM 20.0.0git
CVSymbolVisitor.cpp
Go to the documentation of this file.
1//===- CVSymbolVisitor.cpp --------------------------------------*- 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
10
15
16using namespace llvm;
17using namespace llvm::codeview;
18
20 : Callbacks(Callbacks) {}
21
22template <typename T>
24 SymbolVisitorCallbacks &Callbacks) {
25 SymbolRecordKind RK = static_cast<SymbolRecordKind>(Record.kind());
26 T KnownRecord(RK);
27 if (auto EC = Callbacks.visitKnownRecord(Record, KnownRecord))
28 return EC;
29 return Error::success();
30}
31
33 SymbolVisitorCallbacks &Callbacks) {
34 switch (Record.kind()) {
35 default:
36 if (auto EC = Callbacks.visitUnknownSymbol(Record))
37 return EC;
38 break;
39#define SYMBOL_RECORD(EnumName, EnumVal, Name) \
40 case EnumName: { \
41 if (auto EC = visitKnownRecord<Name>(Record, Callbacks)) \
42 return EC; \
43 break; \
44 }
45#define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName) \
46 SYMBOL_RECORD(EnumVal, EnumVal, AliasName)
47#include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
48 }
49
50 if (auto EC = Callbacks.visitSymbolEnd(Record))
51 return EC;
52
53 return Error::success();
54}
55
57 if (auto EC = Callbacks.visitSymbolBegin(Record))
58 return EC;
59 return finishVisitation(Record, Callbacks);
60}
61
63 if (auto EC = Callbacks.visitSymbolBegin(Record, Offset))
64 return EC;
65 return finishVisitation(Record, Callbacks);
66}
67
69 for (auto I : Symbols) {
70 if (auto EC = visitSymbolRecord(I))
71 return EC;
72 }
73 return Error::success();
74}
75
77 uint32_t InitialOffset) {
78 for (auto I : Symbols) {
79 if (auto EC = visitSymbolRecord(I, InitialOffset + Symbols.skew()))
80 return EC;
81 InitialOffset += I.length();
82 }
83 return Error::success();
84}
85
87 const FilterOptions &Filter) {
88 if (!Filter.SymbolOffset)
90 uint32_t SymbolOffset = *Filter.SymbolOffset;
91 uint32_t ParentRecurseDepth = Filter.ParentRecursiveDepth.value_or(0);
92 uint32_t ChildrenRecurseDepth = Filter.ChildRecursiveDepth.value_or(0);
93 if (!Symbols.isOffsetValid(SymbolOffset))
94 return createStringError(inconvertibleErrorCode(), "Invalid symbol offset");
95 CVSymbol Sym = *Symbols.at(SymbolOffset);
96 uint32_t SymEndOffset =
98
99 std::vector<uint32_t> ParentOffsets;
100 std::vector<uint32_t> ParentEndOffsets;
101 uint32_t ChildrenDepth = 0;
102 for (auto Begin = Symbols.begin(), End = Symbols.end(); Begin != End;
103 ++Begin) {
104 uint32_t BeginOffset = Begin.offset();
105 CVSymbol BeginSym = *Begin;
106 if (BeginOffset < SymbolOffset) {
107 if (symbolOpensScope(Begin->kind())) {
108 uint32_t EndOffset = getScopeEndOffset(BeginSym);
109 if (SymbolOffset < EndOffset) {
110 ParentOffsets.push_back(BeginOffset);
111 ParentEndOffsets.push_back(EndOffset);
112 }
113 }
114 } else if (BeginOffset == SymbolOffset) {
115 // Found symbol at offset. Visit its parent up to ParentRecurseDepth.
116 if (ParentRecurseDepth >= ParentOffsets.size())
117 ParentRecurseDepth = ParentOffsets.size();
118 uint32_t StartIndex = ParentOffsets.size() - ParentRecurseDepth;
119 while (StartIndex < ParentOffsets.size()) {
120 if (!Symbols.isOffsetValid(ParentOffsets[StartIndex]))
121 break;
122 CVSymbol Parent = *Symbols.at(ParentOffsets[StartIndex]);
123 if (auto EC = visitSymbolRecord(Parent, ParentOffsets[StartIndex]))
124 return EC;
125 ++StartIndex;
126 }
127 if (auto EC = visitSymbolRecord(Sym, SymbolOffset))
128 return EC;
129 } else if (BeginOffset <= SymEndOffset) {
130 if (ChildrenRecurseDepth) {
131 // Visit children.
132 if (symbolEndsScope(Begin->kind()))
133 --ChildrenDepth;
134 if (ChildrenDepth < ChildrenRecurseDepth ||
135 BeginOffset == SymEndOffset) {
136 if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
137 return EC;
138 }
139 if (symbolOpensScope(Begin->kind()))
140 ++ChildrenDepth;
141 }
142 } else {
143 // Visit parents' ends.
144 if (ParentRecurseDepth && BeginOffset == ParentEndOffsets.back()) {
145 if (auto EC = visitSymbolRecord(BeginSym, BeginOffset))
146 return EC;
147 ParentEndOffsets.pop_back();
148 --ParentRecurseDepth;
149 }
150 }
151 }
152 return Error::success();
153}
Lightweight arrays that are backed by an arbitrary BinaryStream.
static Error finishVisitation(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks)
static Error visitKnownRecord(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks)
bool End
Definition: ELF_riscv.cpp:480
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define I(x, y, z)
Definition: MD5.cpp:58
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
CVRecord is a fat pointer (base + size pair) to a symbol or type record.
Definition: CVRecord.h:29
CVSymbolVisitor(SymbolVisitorCallbacks &Callbacks)
Error visitSymbolStreamFiltered(const CVSymbolArray &Symbols, const FilterOptions &Filter)
Error visitSymbolRecord(CVSymbol &Record)
Error visitSymbolStream(const CVSymbolArray &Symbols)
virtual Error visitSymbolEnd(CVSymbol &Record)
virtual Error visitSymbolBegin(CVSymbol &Record, uint32_t Offset)
Paired begin/end actions for all symbols.
virtual Error visitUnknownSymbol(CVSymbol &Record)
Action to take on unknown symbols. By default, they are ignored.
bool symbolEndsScope(SymbolKind Kind)
Return true if this ssymbol ends a scope.
SymbolRecordKind
Distinguishes individual records in the Symbols subsection of a .debug$S section.
Definition: CodeView.h:41
uint32_t getScopeEndOffset(const CVSymbol &Symbol)
Given a symbol P for which symbolOpensScope(P) == true, return the corresponding end offset.
bool symbolOpensScope(SymbolKind Kind)
Return true if this symbol opens a scope.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291