blob: ac7ab49575625014a3ce1337934a7d9e10910f92 [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 Turnerb52d08d2015-03-01 06:51:29 +000051 const char *Name, PointerType Pointer) {
Zachary Turner9a818ad2015-02-22 22:03:38 +000052 auto ReturnType = Symbol.getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +000053 ReturnType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +000054 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +000055 uint32_t ClassParentId = Symbol.getClassParentId();
56 auto ClassParent =
57 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
58 ClassParentId);
59
Zachary Turner29c69102015-02-23 05:58:34 +000060 PDB_CallingConv CC = Symbol.getCallingConvention();
61 bool ShouldDumpCallingConvention = true;
62 if ((ClassParent && CC == PDB_CallingConv::Thiscall) ||
63 (!ClassParent && CC == PDB_CallingConv::NearStdcall)) {
64 ShouldDumpCallingConvention = false;
65 }
66
Zachary Turner9a818ad2015-02-22 22:03:38 +000067 if (Pointer == PointerType::None) {
Zachary Turner29c69102015-02-23 05:58:34 +000068 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000069 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
70 if (ClassParent) {
71 Printer << "(";
72 WithColor(Printer, PDB_ColorItem::Identifier).get()
73 << ClassParent->getName();
74 Printer << "::)";
75 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000076 } else {
Zachary Turner2d11c202015-02-27 09:15:59 +000077 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +000078 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000079 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
80 if (ClassParent) {
81 WithColor(Printer, PDB_ColorItem::Identifier).get()
82 << ClassParent->getName();
83 Printer << "::";
84 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000085 if (Pointer == PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +000086 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +000087 else
Zachary Turner2d11c202015-02-27 09:15:59 +000088 Printer << "*";
Zachary Turnerd270d222015-02-26 23:49:23 +000089 if (Name)
Zachary Turner2d11c202015-02-27 09:15:59 +000090 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
91 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +000092 }
93
Zachary Turner2d11c202015-02-27 09:15:59 +000094 Printer << "(";
Zachary Turner9a818ad2015-02-22 22:03:38 +000095 if (auto ChildEnum = Symbol.getArguments()) {
96 uint32_t Index = 0;
97 while (auto Arg = ChildEnum->getNext()) {
Zachary Turnerb52d08d2015-03-01 06:51:29 +000098 Arg->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +000099 if (++Index < ChildEnum->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000100 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000101 }
102 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000103 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000104
105 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000106 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000107 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000108 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000109}
110
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000111void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
Zachary Turner29c69102015-02-23 05:58:34 +0000112 uint32_t FuncStart = Symbol.getRelativeVirtualAddress();
113 uint32_t FuncEnd = FuncStart + Symbol.getLength();
114
Zachary Turner2d11c202015-02-27 09:15:59 +0000115 Printer << "func ";
116 WithColor(Printer, PDB_ColorItem::Address).get() << "["
117 << format_hex(FuncStart, 8);
118 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
119 uint32_t Prologue = DebugStart->getRelativeVirtualAddress() - FuncStart;
120 WithColor(Printer, PDB_ColorItem::Offset).get() << "+" << Prologue;
121 }
122 WithColor(Printer, PDB_ColorItem::Address).get() << " - "
123 << format_hex(FuncEnd, 8);
124 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
125 uint32_t Epilogue = FuncEnd - DebugEnd->getRelativeVirtualAddress();
126 WithColor(Printer, PDB_ColorItem::Offset).get() << "-" << Epilogue;
127 }
128 WithColor(Printer, PDB_ColorItem::Address).get() << "] ";
Zachary Turner29c69102015-02-23 05:58:34 +0000129
130 if (Symbol.hasFramePointer())
Zachary Turner2d11c202015-02-27 09:15:59 +0000131 WithColor(Printer, PDB_ColorItem::Address).get()
132 << "(" << Symbol.getLocalBasePointerRegisterId() << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000133 else
Zachary Turner2d11c202015-02-27 09:15:59 +0000134 WithColor(Printer, PDB_ColorItem::Address).get() << "(FPO)";
Zachary Turner29c69102015-02-23 05:58:34 +0000135
Zachary Turner2d11c202015-02-27 09:15:59 +0000136 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000137 if (Symbol.isVirtual() || Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000138 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000139
140 auto Signature = Symbol.getSignature();
141 if (!Signature) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000142 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000143 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000144 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000145 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000146 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000147 return;
148 }
149
150 auto ReturnType = Signature->getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000151 ReturnType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000152 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000153
Zachary Turner29c69102015-02-23 05:58:34 +0000154 auto ClassParent = Symbol.getClassParent();
155 PDB_CallingConv CC = Signature->getCallingConvention();
156 if (Pointer != FunctionDumper::PointerType::None)
Zachary Turner2d11c202015-02-27 09:15:59 +0000157 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000158
159 if ((ClassParent && CC != PDB_CallingConv::Thiscall) ||
Zachary Turner2d11c202015-02-27 09:15:59 +0000160 (!ClassParent && CC != PDB_CallingConv::NearStdcall)) {
161 WithColor(Printer, PDB_ColorItem::Keyword).get()
162 << Signature->getCallingConvention() << " ";
163 }
164 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000165 if (Pointer != FunctionDumper::PointerType::None) {
166 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000167 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000168 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000169 Printer << "&";
170 Printer << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000171 }
Zachary Turner9a818ad2015-02-22 22:03:38 +0000172
Zachary Turner2d11c202015-02-27 09:15:59 +0000173 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000174 if (auto Arguments = Symbol.getArguments()) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000175 uint32_t Index = 0;
Zachary Turner29c69102015-02-23 05:58:34 +0000176 while (auto Arg = Arguments->getNext()) {
177 auto ArgType = Arg->getType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000178 ArgType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000179 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
180 << Arg->getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000181 if (++Index < Arguments->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000182 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000183 }
184 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000185 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000186 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000187 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000188 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000189 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000190 if (Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000191 Printer << " = 0";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000192}
193
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000194void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000195 uint32_t ElementTypeId = Symbol.getTypeId();
196 auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
197 if (!ElementType)
198 return;
199
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000200 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000201 Printer << "[";
202 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
203 Printer << "]";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000204}
205
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000206void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000207 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000208 Dumper.start(Symbol);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000209}
210
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000211void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000212 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
213 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000214}
215
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000216void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000217 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
Zachary Turner29c69102015-02-23 05:58:34 +0000218 // through to the real thing and dump it.
Zachary Turner9a818ad2015-02-22 22:03:38 +0000219 uint32_t TypeId = Symbol.getTypeId();
220 auto Type = Symbol.getSession().getSymbolById(TypeId);
221 if (!Type)
222 return;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000223 Type->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000224}
225
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000226void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000227 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
228 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000229}
230
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000231void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000232 uint32_t PointeeId = Symbol.getTypeId();
233 auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
234 if (!PointeeType)
235 return;
236
237 if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000238 FunctionDumper NestedDumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000239 PointerType Pointer =
240 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000241 NestedDumper.start(*FuncSig, nullptr, Pointer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000242 } else {
243 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000244 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000245 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000246 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000247 PointeeType->dump(*this);
248 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner9a818ad2015-02-22 22:03:38 +0000249 }
250}
251
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000252void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000253 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000254}