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/PDBExtras.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
18 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
19 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
20 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
28 #include "llvm/Support/Format.h"
31 using namespace llvm::codeview;
32 using namespace llvm::pdb;
36 void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
37 FunctionDumper &Dumper) {
38 uint32_t ClassParentId = Symbol.getClassParentId();
40 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
45 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
50 FunctionDumper::FunctionDumper(LinePrinter &P)
51 : PDBSymDumper(true), Printer(P) {}
53 void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
54 const char *Name, PointerType Pointer) {
55 auto ReturnType = Symbol.getReturnType();
56 ReturnType->dump(*this);
58 uint32_t ClassParentId = Symbol.getClassParentId();
60 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
63 PDB_CallingConv CC = Symbol.getCallingConvention();
64 bool ShouldDumpCallingConvention = true;
65 if ((ClassParent && CC == CallingConvention::ThisCall) ||
66 (!ClassParent && CC == CallingConvention::NearStdCall)) {
67 ShouldDumpCallingConvention = false;
70 if (Pointer == PointerType::None) {
71 if (ShouldDumpCallingConvention)
72 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
75 WithColor(Printer, PDB_ColorItem::Identifier).get()
76 << ClassParent->getName();
81 if (ShouldDumpCallingConvention)
82 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
84 WithColor(Printer, PDB_ColorItem::Identifier).get()
85 << ClassParent->getName();
88 if (Pointer == PointerType::Reference)
93 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
98 if (auto ChildEnum = Symbol.getArguments()) {
100 while (auto Arg = ChildEnum->getNext()) {
102 if (++Index < ChildEnum->getChildCount())
108 if (Symbol.isConstType())
109 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
110 if (Symbol.isVolatileType())
111 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
114 void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
115 uint64_t FuncStart = Symbol.getVirtualAddress();
116 uint64_t FuncEnd = FuncStart + Symbol.getLength();
119 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncStart, 10);
120 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
121 uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart;
122 WithColor(Printer, PDB_ColorItem::Offset).get() << "+" << Prologue;
125 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncEnd, 10);
126 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
127 uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress();
128 WithColor(Printer, PDB_ColorItem::Offset).get() << "-" << Epilogue;
132 if (Symbol.hasFramePointer()) {
133 WithColor(Printer, PDB_ColorItem::Register).get()
134 << Symbol.getLocalBasePointerRegisterId();
136 WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
140 if (Symbol.isVirtual() || Symbol.isPureVirtual())
141 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
143 auto Signature = Symbol.getSignature();
145 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
146 if (Pointer == PointerType::Pointer)
148 else if (Pointer == FunctionDumper::PointerType::Reference)
153 auto ReturnType = Signature->getReturnType();
154 ReturnType->dump(*this);
157 auto ClassParent = Symbol.getClassParent();
158 CallingConvention CC = Signature->getCallingConvention();
159 if (Pointer != FunctionDumper::PointerType::None)
162 if ((ClassParent && CC != CallingConvention::ThisCall) ||
163 (!ClassParent && CC != CallingConvention::NearStdCall)) {
164 WithColor(Printer, PDB_ColorItem::Keyword).get()
165 << Signature->getCallingConvention() << " ";
167 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
168 if (Pointer != FunctionDumper::PointerType::None) {
169 if (Pointer == PointerType::Pointer)
171 else if (Pointer == FunctionDumper::PointerType::Reference)
177 if (auto Arguments = Symbol.getArguments()) {
179 while (auto Arg = Arguments->getNext()) {
180 auto ArgType = Arg->getType();
181 ArgType->dump(*this);
182 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
184 if (++Index < Arguments->getChildCount())
189 if (Symbol.isConstType())
190 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
191 if (Symbol.isVolatileType())
192 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
193 if (Symbol.isPureVirtual())
197 void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
198 uint32_t ElementTypeId = Symbol.getTypeId();
199 auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
203 ElementType->dump(*this);
205 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
209 void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
210 BuiltinDumper Dumper(Printer);
211 Dumper.start(Symbol);
214 void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
215 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
216 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
219 void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
220 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
221 // through to the real thing and dump it.
222 uint32_t TypeId = Symbol.getTypeId();
223 auto Type = Symbol.getSession().getSymbolById(TypeId);
229 void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
230 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
231 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
234 void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
235 uint32_t PointeeId = Symbol.getTypeId();
236 auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
240 if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
241 FunctionDumper NestedDumper(Printer);
242 PointerType Pointer =
243 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
244 NestedDumper.start(*FuncSig, nullptr, Pointer);
246 if (Symbol.isConstType())
247 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
248 if (Symbol.isVolatileType())
249 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
250 PointeeType->dump(*this);
251 Printer << (Symbol.isReference() ? "&" : "*");
255 void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
256 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();