PDBSymbol.cpp
8.2 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//===- PDBSymbol.cpp - base class for user-facing symbol types --*- 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/PDBSymbol.h"
#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
#include "llvm/DebugInfo/PDB/IPDBSession.h"
#include "llvm/DebugInfo/PDB/PDBExtras.h"
#include "llvm/DebugInfo/PDB/PDBSymbolAnnotation.h"
#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompilandEnv.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCustom.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
#include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"
#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeCustom.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeDimension.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeFriend.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeManaged.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTable.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeVTableShape.h"
#include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"
#include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"
#include "llvm/DebugInfo/PDB/PDBTypes.h"
#include <algorithm>
#include <memory>
using namespace llvm;
using namespace llvm::pdb;
PDBSymbol::PDBSymbol(const IPDBSession &PDBSession) : Session(PDBSession) {}
PDBSymbol::PDBSymbol(PDBSymbol &&Other)
: Session(Other.Session), RawSymbol(std::move(Other.RawSymbol)) {}
PDBSymbol::~PDBSymbol() = default;
#define FACTORY_SYMTAG_CASE(Tag, Type) \
case PDB_SymType::Tag: \
return std::unique_ptr<PDBSymbol>(new Type(PDBSession));
std::unique_ptr<PDBSymbol>
PDBSymbol::createSymbol(const IPDBSession &PDBSession, PDB_SymType Tag) {
switch (Tag) {
FACTORY_SYMTAG_CASE(Exe, PDBSymbolExe)
FACTORY_SYMTAG_CASE(Compiland, PDBSymbolCompiland)
FACTORY_SYMTAG_CASE(CompilandDetails, PDBSymbolCompilandDetails)
FACTORY_SYMTAG_CASE(CompilandEnv, PDBSymbolCompilandEnv)
FACTORY_SYMTAG_CASE(Function, PDBSymbolFunc)
FACTORY_SYMTAG_CASE(Block, PDBSymbolBlock)
FACTORY_SYMTAG_CASE(Data, PDBSymbolData)
FACTORY_SYMTAG_CASE(Annotation, PDBSymbolAnnotation)
FACTORY_SYMTAG_CASE(Label, PDBSymbolLabel)
FACTORY_SYMTAG_CASE(PublicSymbol, PDBSymbolPublicSymbol)
FACTORY_SYMTAG_CASE(UDT, PDBSymbolTypeUDT)
FACTORY_SYMTAG_CASE(Enum, PDBSymbolTypeEnum)
FACTORY_SYMTAG_CASE(FunctionSig, PDBSymbolTypeFunctionSig)
FACTORY_SYMTAG_CASE(PointerType, PDBSymbolTypePointer)
FACTORY_SYMTAG_CASE(ArrayType, PDBSymbolTypeArray)
FACTORY_SYMTAG_CASE(BuiltinType, PDBSymbolTypeBuiltin)
FACTORY_SYMTAG_CASE(Typedef, PDBSymbolTypeTypedef)
FACTORY_SYMTAG_CASE(BaseClass, PDBSymbolTypeBaseClass)
FACTORY_SYMTAG_CASE(Friend, PDBSymbolTypeFriend)
FACTORY_SYMTAG_CASE(FunctionArg, PDBSymbolTypeFunctionArg)
FACTORY_SYMTAG_CASE(FuncDebugStart, PDBSymbolFuncDebugStart)
FACTORY_SYMTAG_CASE(FuncDebugEnd, PDBSymbolFuncDebugEnd)
FACTORY_SYMTAG_CASE(UsingNamespace, PDBSymbolUsingNamespace)
FACTORY_SYMTAG_CASE(VTableShape, PDBSymbolTypeVTableShape)
FACTORY_SYMTAG_CASE(VTable, PDBSymbolTypeVTable)
FACTORY_SYMTAG_CASE(Custom, PDBSymbolCustom)
FACTORY_SYMTAG_CASE(Thunk, PDBSymbolThunk)
FACTORY_SYMTAG_CASE(CustomType, PDBSymbolTypeCustom)
FACTORY_SYMTAG_CASE(ManagedType, PDBSymbolTypeManaged)
FACTORY_SYMTAG_CASE(Dimension, PDBSymbolTypeDimension)
default:
return std::unique_ptr<PDBSymbol>(new PDBSymbolUnknown(PDBSession));
}
}
std::unique_ptr<PDBSymbol>
PDBSymbol::create(const IPDBSession &PDBSession,
std::unique_ptr<IPDBRawSymbol> RawSymbol) {
auto SymbolPtr = createSymbol(PDBSession, RawSymbol->getSymTag());
SymbolPtr->RawSymbol = RawSymbol.get();
SymbolPtr->OwnedRawSymbol = std::move(RawSymbol);
return SymbolPtr;
}
std::unique_ptr<PDBSymbol> PDBSymbol::create(const IPDBSession &PDBSession,
IPDBRawSymbol &RawSymbol) {
auto SymbolPtr = createSymbol(PDBSession, RawSymbol.getSymTag());
SymbolPtr->RawSymbol = &RawSymbol;
return SymbolPtr;
}
void PDBSymbol::defaultDump(raw_ostream &OS, int Indent,
PdbSymbolIdField ShowFlags,
PdbSymbolIdField RecurseFlags) const {
RawSymbol->dump(OS, Indent, ShowFlags, RecurseFlags);
}
void PDBSymbol::dumpProperties() const {
outs() << "\n";
defaultDump(outs(), 0, PdbSymbolIdField::All, PdbSymbolIdField::None);
outs().flush();
}
void PDBSymbol::dumpChildStats() const {
TagStats Stats;
getChildStats(Stats);
outs() << "\n";
for (auto &Stat : Stats) {
outs() << Stat.first << ": " << Stat.second << "\n";
}
outs().flush();
}
PDB_SymType PDBSymbol::getSymTag() const { return RawSymbol->getSymTag(); }
uint32_t PDBSymbol::getSymIndexId() const { return RawSymbol->getSymIndexId(); }
std::unique_ptr<IPDBEnumSymbols> PDBSymbol::findAllChildren() const {
return findAllChildren(PDB_SymType::None);
}
std::unique_ptr<IPDBEnumSymbols>
PDBSymbol::findAllChildren(PDB_SymType Type) const {
return RawSymbol->findChildren(Type);
}
std::unique_ptr<IPDBEnumSymbols>
PDBSymbol::findChildren(PDB_SymType Type, StringRef Name,
PDB_NameSearchFlags Flags) const {
return RawSymbol->findChildren(Type, Name, Flags);
}
std::unique_ptr<IPDBEnumSymbols>
PDBSymbol::findChildrenByRVA(PDB_SymType Type, StringRef Name,
PDB_NameSearchFlags Flags, uint32_t RVA) const {
return RawSymbol->findChildrenByRVA(Type, Name, Flags, RVA);
}
std::unique_ptr<IPDBEnumSymbols>
PDBSymbol::findInlineFramesByRVA(uint32_t RVA) const {
return RawSymbol->findInlineFramesByRVA(RVA);
}
std::unique_ptr<IPDBEnumSymbols>
PDBSymbol::getChildStats(TagStats &Stats) const {
std::unique_ptr<IPDBEnumSymbols> Result(findAllChildren());
if (!Result)
return nullptr;
Stats.clear();
while (auto Child = Result->getNext()) {
++Stats[Child->getSymTag()];
}
Result->reset();
return Result;
}
std::unique_ptr<PDBSymbol> PDBSymbol::getSymbolByIdHelper(uint32_t Id) const {
return Session.getSymbolById(Id);
}
void llvm::pdb::dumpSymbolIdField(raw_ostream &OS, StringRef Name,
SymIndexId Value, int Indent,
const IPDBSession &Session,
PdbSymbolIdField FieldId,
PdbSymbolIdField ShowFlags,
PdbSymbolIdField RecurseFlags) {
if ((FieldId & ShowFlags) == PdbSymbolIdField::None)
return;
OS << "\n";
OS.indent(Indent);
OS << Name << ": " << Value;
// Don't recurse unless the user requested it.
if ((FieldId & RecurseFlags) == PdbSymbolIdField::None)
return;
// And obviously don't recurse on the symbol itself.
if (FieldId == PdbSymbolIdField::SymIndexId)
return;
auto Child = Session.getSymbolById(Value);
// It could have been a placeholder symbol for a type we don't yet support,
// so just exit in that case.
if (!Child)
return;
// Don't recurse more than once, so pass PdbSymbolIdField::None) for the
// recurse flags.
Child->defaultDump(OS, Indent + 2, ShowFlags, PdbSymbolIdField::None);
}