blob: aafbbfa64689e4bf30b383cbed5d1dae665d3b37 [file] [log] [blame]
Zachary Turner9a818ad2015-02-22 22:03:38 +00001//===- 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"
Zachary Turnerd270d222015-02-26 23:49:23 +000011#include "BuiltinDumper.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turner29c69102015-02-23 05:58:34 +000013#include "llvm-pdbdump.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000014
15#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner29c69102015-02-23 05:58:34 +000016#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000017#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
Zachary Turner29c69102015-02-23 05:58:34 +000018#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
19#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000020#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000021#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"
Zachary Turner29c69102015-02-23 05:58:34 +000027#include "llvm/Support/Format.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000028
Zachary Turneraea59922015-02-22 22:20:26 +000029using namespace llvm;
30
Zachary Turner9a818ad2015-02-22 22:03:38 +000031namespace {
32template <class T>
Zachary Turner2d11c202015-02-27 09:15:59 +000033void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
Zachary Turner9a818ad2015-02-22 22:03:38 +000034 llvm::FunctionDumper &Dumper) {
35 uint32_t ClassParentId = Symbol.getClassParentId();
36 auto ClassParent =
David Majnemer3f45d402015-02-22 22:33:57 +000037 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
Zachary Turner9a818ad2015-02-22 22:03:38 +000038 ClassParentId);
39 if (!ClassParent)
40 return;
41
Zachary Turner2d11c202015-02-27 09:15:59 +000042 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
43 Printer << "::";
Zachary Turner9a818ad2015-02-22 22:03:38 +000044}
45}
46
Zachary Turner2d11c202015-02-27 09:15:59 +000047FunctionDumper::FunctionDumper(LinePrinter &P)
48 : PDBSymDumper(true), Printer(P) {}
Zachary Turner9a818ad2015-02-22 22:03:38 +000049
50void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
Zachary Turnerd270d222015-02-26 23:49:23 +000051 const char *Name, PointerType Pointer,
52 raw_ostream &OS) {
Zachary Turner9a818ad2015-02-22 22:03:38 +000053 auto ReturnType = Symbol.getReturnType();
54 ReturnType->dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +000055 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +000056 uint32_t ClassParentId = Symbol.getClassParentId();
57 auto ClassParent =
58 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
59 ClassParentId);
60
Zachary Turner29c69102015-02-23 05:58:34 +000061 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;
66 }
67
Zachary Turner9a818ad2015-02-22 22:03:38 +000068 if (Pointer == PointerType::None) {
Zachary Turner29c69102015-02-23 05:58:34 +000069 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000070 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
71 if (ClassParent) {
72 Printer << "(";
73 WithColor(Printer, PDB_ColorItem::Identifier).get()
74 << ClassParent->getName();
75 Printer << "::)";
76 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000077 } else {
Zachary Turner2d11c202015-02-27 09:15:59 +000078 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +000079 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000080 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
81 if (ClassParent) {
82 WithColor(Printer, PDB_ColorItem::Identifier).get()
83 << ClassParent->getName();
84 Printer << "::";
85 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000086 if (Pointer == PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +000087 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +000088 else
Zachary Turner2d11c202015-02-27 09:15:59 +000089 Printer << "*";
Zachary Turnerd270d222015-02-26 23:49:23 +000090 if (Name)
Zachary Turner2d11c202015-02-27 09:15:59 +000091 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
92 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +000093 }
94
Zachary Turner2d11c202015-02-27 09:15:59 +000095 Printer << "(";
Zachary Turner9a818ad2015-02-22 22:03:38 +000096 if (auto ChildEnum = Symbol.getArguments()) {
97 uint32_t Index = 0;
98 while (auto Arg = ChildEnum->getNext()) {
99 Arg->dump(OS, 0, *this);
100 if (++Index < ChildEnum->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000101 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000102 }
103 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000104 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000105
106 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000107 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000108 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000109 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000110}
111
Zachary Turner29c69102015-02-23 05:58:34 +0000112void 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();
116
Zachary Turner2d11c202015-02-27 09:15:59 +0000117 Printer << "func ";
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;
123 }
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;
129 }
130 WithColor(Printer, PDB_ColorItem::Address).get() << "] ";
Zachary Turner29c69102015-02-23 05:58:34 +0000131
132 if (Symbol.hasFramePointer())
Zachary Turner2d11c202015-02-27 09:15:59 +0000133 WithColor(Printer, PDB_ColorItem::Address).get()
134 << "(" << Symbol.getLocalBasePointerRegisterId() << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000135 else
Zachary Turner2d11c202015-02-27 09:15:59 +0000136 WithColor(Printer, PDB_ColorItem::Address).get() << "(FPO)";
Zachary Turner29c69102015-02-23 05:58:34 +0000137
Zachary Turner2d11c202015-02-27 09:15:59 +0000138 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000139 if (Symbol.isVirtual() || Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000140 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000141
142 auto Signature = Symbol.getSignature();
143 if (!Signature) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000144 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000145 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000146 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000147 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000148 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000149 return;
150 }
151
152 auto ReturnType = Signature->getReturnType();
153 ReturnType->dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000154 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000155
Zachary Turner29c69102015-02-23 05:58:34 +0000156 auto ClassParent = Symbol.getClassParent();
157 PDB_CallingConv CC = Signature->getCallingConvention();
158 if (Pointer != FunctionDumper::PointerType::None)
Zachary Turner2d11c202015-02-27 09:15:59 +0000159 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000160
161 if ((ClassParent && CC != PDB_CallingConv::Thiscall) ||
Zachary Turner2d11c202015-02-27 09:15:59 +0000162 (!ClassParent && CC != PDB_CallingConv::NearStdcall)) {
163 WithColor(Printer, PDB_ColorItem::Keyword).get()
164 << Signature->getCallingConvention() << " ";
165 }
166 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000167 if (Pointer != FunctionDumper::PointerType::None) {
168 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000169 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000170 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000171 Printer << "&";
172 Printer << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000173 }
Zachary Turner9a818ad2015-02-22 22:03:38 +0000174
Zachary Turner2d11c202015-02-27 09:15:59 +0000175 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000176 if (auto Arguments = Symbol.getArguments()) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000177 uint32_t Index = 0;
Zachary Turner29c69102015-02-23 05:58:34 +0000178 while (auto Arg = Arguments->getNext()) {
179 auto ArgType = Arg->getType();
180 ArgType->dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000181 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
182 << Arg->getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000183 if (++Index < Arguments->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000184 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000185 }
186 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000187 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000188 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000189 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000190 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000191 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000192 if (Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000193 Printer << " = 0";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000194}
195
196void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol, raw_ostream &OS,
197 int Indent) {
198 uint32_t ElementTypeId = Symbol.getTypeId();
199 auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
200 if (!ElementType)
201 return;
202
203 ElementType->dump(OS, 0, *this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000204 Printer << "[";
205 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
206 Printer << "]";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000207}
208
209void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol, raw_ostream &OS,
210 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000211 BuiltinDumper Dumper(Printer);
Zachary Turnerd270d222015-02-26 23:49:23 +0000212 Dumper.start(Symbol, OS);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000213}
214
215void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol, raw_ostream &OS,
216 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000217 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
218 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000219}
220
221void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol,
222 raw_ostream &OS, int Indent) {
223 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
Zachary Turner29c69102015-02-23 05:58:34 +0000224 // through to the real thing and dump it.
Zachary Turner9a818ad2015-02-22 22:03:38 +0000225 uint32_t TypeId = Symbol.getTypeId();
226 auto Type = Symbol.getSession().getSymbolById(TypeId);
227 if (!Type)
228 return;
229 Type->dump(OS, 0, *this);
230}
231
232void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol, raw_ostream &OS,
233 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000234 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
235 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000236}
237
238void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol, raw_ostream &OS,
239 int Indent) {
240 uint32_t PointeeId = Symbol.getTypeId();
241 auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
242 if (!PointeeType)
243 return;
244
245 if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000246 FunctionDumper NestedDumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000247 PointerType Pointer =
248 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
Zachary Turnerd270d222015-02-26 23:49:23 +0000249 NestedDumper.start(*FuncSig, nullptr, Pointer, OS);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000250 } else {
251 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000252 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000253 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000254 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000255 PointeeType->dump(OS, Indent, *this);
256 OS << (Symbol.isReference() ? "&" : "*");
257 }
258}
259
260void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol, raw_ostream &OS,
261 int Indent) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000262 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000263}