1 //===- FunctionDumper.cpp ------------------------------------ *- C++ *-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FunctionDumper.h"
11 #include "BuiltinDumper.h"
12 #include "LinePrinter.h"
13 #include "llvm-pdbdump.h"
14 
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"
29 
30 using namespace llvm;
31 using namespace llvm::codeview;
32 using namespace llvm::pdb;
33 
34 namespace {
35 template <class T>
dumpClassParentWithScopeOperator(const T & Symbol,LinePrinter & Printer,FunctionDumper & Dumper)36 void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
37                                       FunctionDumper &Dumper) {
38   uint32_t ClassParentId = Symbol.getClassParentId();
39   auto ClassParent =
40       Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
41           ClassParentId);
42   if (!ClassParent)
43     return;
44 
45   WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
46   Printer << "::";
47 }
48 }
49 
FunctionDumper(LinePrinter & P)50 FunctionDumper::FunctionDumper(LinePrinter &P)
51     : PDBSymDumper(true), Printer(P) {}
52 
start(const PDBSymbolTypeFunctionSig & Symbol,const char * Name,PointerType Pointer)53 void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
54                            const char *Name, PointerType Pointer) {
55   auto ReturnType = Symbol.getReturnType();
56   ReturnType->dump(*this);
57   Printer << " ";
58   uint32_t ClassParentId = Symbol.getClassParentId();
59   auto ClassParent =
60       Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
61           ClassParentId);
62 
63   PDB_CallingConv CC = Symbol.getCallingConvention();
64   bool ShouldDumpCallingConvention = true;
65   if ((ClassParent && CC == CallingConvention::ThisCall) ||
66       (!ClassParent && CC == CallingConvention::NearStdCall)) {
67     ShouldDumpCallingConvention = false;
68   }
69 
70   if (Pointer == PointerType::None) {
71     if (ShouldDumpCallingConvention)
72       WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
73     if (ClassParent) {
74       Printer << "(";
75       WithColor(Printer, PDB_ColorItem::Identifier).get()
76           << ClassParent->getName();
77       Printer << "::)";
78     }
79   } else {
80     Printer << "(";
81     if (ShouldDumpCallingConvention)
82       WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
83     if (ClassParent) {
84       WithColor(Printer, PDB_ColorItem::Identifier).get()
85           << ClassParent->getName();
86       Printer << "::";
87     }
88     if (Pointer == PointerType::Reference)
89       Printer << "&";
90     else
91       Printer << "*";
92     if (Name)
93       WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
94     Printer << ")";
95   }
96 
97   Printer << "(";
98   if (auto ChildEnum = Symbol.getArguments()) {
99     uint32_t Index = 0;
100     while (auto Arg = ChildEnum->getNext()) {
101       Arg->dump(*this);
102       if (++Index < ChildEnum->getChildCount())
103         Printer << ", ";
104     }
105   }
106   Printer << ")";
107 
108   if (Symbol.isConstType())
109     WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
110   if (Symbol.isVolatileType())
111     WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
112 }
113 
start(const PDBSymbolFunc & Symbol,PointerType Pointer)114 void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
115   uint64_t FuncStart = Symbol.getVirtualAddress();
116   uint64_t FuncEnd = FuncStart + Symbol.getLength();
117 
118   Printer << "func [";
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;
123   }
124   Printer << " - ";
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;
129   }
130   Printer << "] (";
131 
132   if (Symbol.hasFramePointer()) {
133     WithColor(Printer, PDB_ColorItem::Register).get()
134         << Symbol.getLocalBasePointerRegisterId();
135   } else {
136     WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
137   }
138   Printer << ") ";
139 
140   if (Symbol.isVirtual() || Symbol.isPureVirtual())
141     WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
142 
143   auto Signature = Symbol.getSignature();
144   if (!Signature) {
145     WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
146     if (Pointer == PointerType::Pointer)
147       Printer << "*";
148     else if (Pointer == FunctionDumper::PointerType::Reference)
149       Printer << "&";
150     return;
151   }
152 
153   auto ReturnType = Signature->getReturnType();
154   ReturnType->dump(*this);
155   Printer << " ";
156 
157   auto ClassParent = Symbol.getClassParent();
158   CallingConvention CC = Signature->getCallingConvention();
159   if (Pointer != FunctionDumper::PointerType::None)
160     Printer << "(";
161 
162   if ((ClassParent && CC != CallingConvention::ThisCall) ||
163       (!ClassParent && CC != CallingConvention::NearStdCall)) {
164     WithColor(Printer, PDB_ColorItem::Keyword).get()
165         << Signature->getCallingConvention() << " ";
166   }
167   WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
168   if (Pointer != FunctionDumper::PointerType::None) {
169     if (Pointer == PointerType::Pointer)
170       Printer << "*";
171     else if (Pointer == FunctionDumper::PointerType::Reference)
172       Printer << "&";
173     Printer << ")";
174   }
175 
176   Printer << "(";
177   if (auto Arguments = Symbol.getArguments()) {
178     uint32_t Index = 0;
179     while (auto Arg = Arguments->getNext()) {
180       auto ArgType = Arg->getType();
181       ArgType->dump(*this);
182       WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
183                                                           << Arg->getName();
184       if (++Index < Arguments->getChildCount())
185         Printer << ", ";
186     }
187   }
188   Printer << ")";
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())
194     Printer << " = 0";
195 }
196 
dump(const PDBSymbolTypeArray & Symbol)197 void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
198   uint32_t ElementTypeId = Symbol.getTypeId();
199   auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
200   if (!ElementType)
201     return;
202 
203   ElementType->dump(*this);
204   Printer << "[";
205   WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
206   Printer << "]";
207 }
208 
dump(const PDBSymbolTypeBuiltin & Symbol)209 void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
210   BuiltinDumper Dumper(Printer);
211   Dumper.start(Symbol);
212 }
213 
dump(const PDBSymbolTypeEnum & Symbol)214 void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
215   dumpClassParentWithScopeOperator(Symbol, Printer, *this);
216   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
217 }
218 
dump(const PDBSymbolTypeFunctionArg & Symbol)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);
224   if (!Type)
225     return;
226   Type->dump(*this);
227 }
228 
dump(const PDBSymbolTypeTypedef & Symbol)229 void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
230   dumpClassParentWithScopeOperator(Symbol, Printer, *this);
231   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
232 }
233 
dump(const PDBSymbolTypePointer & Symbol)234 void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
235   uint32_t PointeeId = Symbol.getTypeId();
236   auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
237   if (!PointeeType)
238     return;
239 
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);
245   } else {
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() ? "&" : "*");
252   }
253 }
254 
dump(const PDBSymbolTypeUDT & Symbol)255 void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
256   WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
257 }
258