1 //===- FunctionDumper.cpp ------------------------------------ *- C++ *-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "FunctionDumper.h"
11 #include "BuiltinDumper.h"
12 #include "LinePrinter.h"
13 #include "llvm-pdbdump.h"
15 #include "llvm/DebugInfo/PDB/IPDBSession.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
27 #include "llvm/Support/Format.h"
33 void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
34 llvm::FunctionDumper &Dumper) {
35 uint32_t ClassParentId = Symbol.getClassParentId();
37 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
42 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
47 FunctionDumper::FunctionDumper(LinePrinter &P)
48 : PDBSymDumper(true), Printer(P) {}
50 void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
51 const char *Name, PointerType Pointer,
53 auto ReturnType = Symbol.getReturnType();
54 ReturnType->dump(OS, 0, *this);
56 uint32_t ClassParentId = Symbol.getClassParentId();
58 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
61 PDB_CallingConv CC = Symbol.getCallingConvention();
62 bool ShouldDumpCallingConvention = true;
63 if ((ClassParent && CC == PDB_CallingConv::Thiscall) ||
64 (!ClassParent && CC == PDB_CallingConv::NearStdcall)) {
65 ShouldDumpCallingConvention = false;
68 if (Pointer == PointerType::None) {
69 if (ShouldDumpCallingConvention)
70 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
73 WithColor(Printer, PDB_ColorItem::Identifier).get()
74 << ClassParent->getName();
79 if (ShouldDumpCallingConvention)
80 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
82 WithColor(Printer, PDB_ColorItem::Identifier).get()
83 << ClassParent->getName();
86 if (Pointer == PointerType::Reference)
91 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
96 if (auto ChildEnum = Symbol.getArguments()) {
98 while (auto Arg = ChildEnum->getNext()) {
99 Arg->dump(OS, 0, *this);
100 if (++Index < ChildEnum->getChildCount())
106 if (Symbol.isConstType())
107 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
108 if (Symbol.isVolatileType())
109 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
112 void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer,
113 raw_ostream &OS, int Indent) {
114 uint32_t FuncStart = Symbol.getRelativeVirtualAddress();
115 uint32_t FuncEnd = FuncStart + Symbol.getLength();
118 WithColor(Printer, PDB_ColorItem::Address).get() << "["
119 << format_hex(FuncStart, 8);
120 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
121 uint32_t Prologue = DebugStart->getRelativeVirtualAddress() - FuncStart;
122 WithColor(Printer, PDB_ColorItem::Offset).get() << "+" << Prologue;
124 WithColor(Printer, PDB_ColorItem::Address).get() << " - "
125 << format_hex(FuncEnd, 8);
126 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
127 uint32_t Epilogue = FuncEnd - DebugEnd->getRelativeVirtualAddress();
128 WithColor(Printer, PDB_ColorItem::Offset).get() << "-" << Epilogue;
130 WithColor(Printer, PDB_ColorItem::Address).get() << "] ";
132 if (Symbol.hasFramePointer())
133 WithColor(Printer, PDB_ColorItem::Address).get()
134 << "(" << Symbol.getLocalBasePointerRegisterId() << ")";
136 WithColor(Printer, PDB_ColorItem::Address).get() << "(FPO)";
139 if (Symbol.isVirtual() || Symbol.isPureVirtual())
140 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
142 auto Signature = Symbol.getSignature();
144 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
145 if (Pointer == PointerType::Pointer)
147 else if (Pointer == FunctionDumper::PointerType::Reference)
152 auto ReturnType = Signature->getReturnType();
153 ReturnType->dump(OS, 0, *this);
156 auto ClassParent = Symbol.getClassParent();
157 PDB_CallingConv CC = Signature->getCallingConvention();
158 if (Pointer != FunctionDumper::PointerType::None)
161 if ((ClassParent && CC != PDB_CallingConv::Thiscall) ||
162 (!ClassParent && CC != PDB_CallingConv::NearStdcall)) {
163 WithColor(Printer, PDB_ColorItem::Keyword).get()
164 << Signature->getCallingConvention() << " ";
166 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
167 if (Pointer != FunctionDumper::PointerType::None) {
168 if (Pointer == PointerType::Pointer)
170 else if (Pointer == FunctionDumper::PointerType::Reference)
176 if (auto Arguments = Symbol.getArguments()) {
178 while (auto Arg = Arguments->getNext()) {
179 auto ArgType = Arg->getType();
180 ArgType->dump(OS, 0, *this);
181 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
183 if (++Index < Arguments->getChildCount())
188 if (Symbol.isConstType())
189 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
190 if (Symbol.isVolatileType())
191 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
192 if (Symbol.isPureVirtual())
196 void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol, raw_ostream &OS,
198 uint32_t ElementTypeId = Symbol.getTypeId();
199 auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
203 ElementType->dump(OS, 0, *this);
205 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
209 void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
211 BuiltinDumper Dumper(Printer);
212 Dumper.start(Symbol, OS);
215 void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
217 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
218 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
221 void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol,
222 raw_ostream &OS, int Indent) {
223 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
224 // through to the real thing and dump it.
225 uint32_t TypeId = Symbol.getTypeId();
226 auto Type = Symbol.getSession().getSymbolById(TypeId);
229 Type->dump(OS, 0, *this);
232 void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
234 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
235 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
238 void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
240 uint32_t PointeeId = Symbol.getTypeId();
241 auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
245 if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
246 FunctionDumper NestedDumper(Printer);
247 PointerType Pointer =
248 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
249 NestedDumper.start(*FuncSig, nullptr, Pointer, OS);
251 if (Symbol.isConstType())
252 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
253 if (Symbol.isVolatileType())
254 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
255 PointeeType->dump(OS, Indent, *this);
256 OS << (Symbol.isReference() ? "&" : "*");
260 void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
262 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();