blob: 8b2043989b81f0a2eed5bb147a5fa44e05149446 [file] [log] [blame]
Zachary Turnera9054dd2017-01-11 00:35:43 +00001//===- PrettyFunctionDumper.cpp --------------------------------- *- C++ *-===//
Zachary Turner9a818ad2015-02-22 22:03:38 +00002//
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
Zachary Turnera9054dd2017-01-11 00:35:43 +000010#include "PrettyFunctionDumper.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000011#include "LinePrinter.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000012#include "PrettyBuiltinDumper.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 Turnerec28fc32016-05-04 20:32:13 +000016#include "llvm/DebugInfo/PDB/PDBExtras.h"
Zachary Turner29c69102015-02-23 05:58:34 +000017#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000018#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
Zachary Turner29c69102015-02-23 05:58:34 +000019#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
20#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000021#include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000022#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"
Zachary Turner29c69102015-02-23 05:58:34 +000028#include "llvm/Support/Format.h"
Zachary Turner0683be22017-05-14 01:13:40 +000029#include "llvm/Support/FormatVariadic.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000030
Zachary Turneraea59922015-02-22 22:20:26 +000031using namespace llvm;
Reid Kleckner72e2ba72016-01-13 19:32:35 +000032using namespace llvm::codeview;
Zachary Turnerec28fc32016-05-04 20:32:13 +000033using namespace llvm::pdb;
Zachary Turneraea59922015-02-22 22:20:26 +000034
Zachary Turner9a818ad2015-02-22 22:03:38 +000035namespace {
36template <class T>
Zachary Turner2d11c202015-02-27 09:15:59 +000037void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
Zachary Turnerec28fc32016-05-04 20:32:13 +000038 FunctionDumper &Dumper) {
Zachary Turner9a818ad2015-02-22 22:03:38 +000039 uint32_t ClassParentId = Symbol.getClassParentId();
40 auto ClassParent =
David Majnemer3f45d402015-02-22 22:33:57 +000041 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
Zachary Turner9a818ad2015-02-22 22:03:38 +000042 ClassParentId);
43 if (!ClassParent)
44 return;
45
Zachary Turner2d11c202015-02-27 09:15:59 +000046 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
47 Printer << "::";
Zachary Turner9a818ad2015-02-22 22:03:38 +000048}
49}
50
Zachary Turner2d11c202015-02-27 09:15:59 +000051FunctionDumper::FunctionDumper(LinePrinter &P)
52 : PDBSymDumper(true), Printer(P) {}
Zachary Turner9a818ad2015-02-22 22:03:38 +000053
54void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
Zachary Turnerb52d08d2015-03-01 06:51:29 +000055 const char *Name, PointerType Pointer) {
Zachary Turner9a818ad2015-02-22 22:03:38 +000056 auto ReturnType = Symbol.getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +000057 ReturnType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +000058 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +000059 uint32_t ClassParentId = Symbol.getClassParentId();
60 auto ClassParent =
61 Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
62 ClassParentId);
63
Zachary Turnerec28fc32016-05-04 20:32:13 +000064 PDB_CallingConv CC = Symbol.getCallingConvention();
Zachary Turner29c69102015-02-23 05:58:34 +000065 bool ShouldDumpCallingConvention = true;
Reid Kleckner72e2ba72016-01-13 19:32:35 +000066 if ((ClassParent && CC == CallingConvention::ThisCall) ||
67 (!ClassParent && CC == CallingConvention::NearStdCall)) {
Zachary Turner29c69102015-02-23 05:58:34 +000068 ShouldDumpCallingConvention = false;
69 }
70
Zachary Turner9a818ad2015-02-22 22:03:38 +000071 if (Pointer == PointerType::None) {
Zachary Turner29c69102015-02-23 05:58:34 +000072 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000073 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
74 if (ClassParent) {
75 Printer << "(";
76 WithColor(Printer, PDB_ColorItem::Identifier).get()
77 << ClassParent->getName();
78 Printer << "::)";
79 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000080 } else {
Zachary Turner2d11c202015-02-27 09:15:59 +000081 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +000082 if (ShouldDumpCallingConvention)
Zachary Turner2d11c202015-02-27 09:15:59 +000083 WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
84 if (ClassParent) {
85 WithColor(Printer, PDB_ColorItem::Identifier).get()
86 << ClassParent->getName();
87 Printer << "::";
88 }
Zachary Turner9a818ad2015-02-22 22:03:38 +000089 if (Pointer == PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +000090 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +000091 else
Zachary Turner2d11c202015-02-27 09:15:59 +000092 Printer << "*";
Zachary Turnerd270d222015-02-26 23:49:23 +000093 if (Name)
Zachary Turner2d11c202015-02-27 09:15:59 +000094 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
95 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +000096 }
97
Zachary Turner2d11c202015-02-27 09:15:59 +000098 Printer << "(";
Zachary Turner9a818ad2015-02-22 22:03:38 +000099 if (auto ChildEnum = Symbol.getArguments()) {
100 uint32_t Index = 0;
101 while (auto Arg = ChildEnum->getNext()) {
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000102 Arg->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000103 if (++Index < ChildEnum->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000104 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000105 }
106 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000107 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000108
109 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000110 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000111 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000112 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000113}
114
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000115void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000116 uint64_t FuncStart = Symbol.getVirtualAddress();
117 uint64_t FuncEnd = FuncStart + Symbol.getLength();
Zachary Turner29c69102015-02-23 05:58:34 +0000118
Zachary Turner7797c722015-03-02 04:39:56 +0000119 Printer << "func [";
120 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncStart, 10);
Zachary Turner2d11c202015-02-27 09:15:59 +0000121 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000122 uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart;
Zachary Turner0683be22017-05-14 01:13:40 +0000123 WithColor(Printer, PDB_ColorItem::Offset).get()
124 << formatv("+{0,2}", Prologue);
Zachary Turner2d11c202015-02-27 09:15:59 +0000125 }
Zachary Turner7797c722015-03-02 04:39:56 +0000126 Printer << " - ";
127 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncEnd, 10);
Zachary Turner2d11c202015-02-27 09:15:59 +0000128 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000129 uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress();
Zachary Turner0683be22017-05-14 01:13:40 +0000130 WithColor(Printer, PDB_ColorItem::Offset).get()
131 << formatv("-{0,2}", Epilogue);
Zachary Turner2d11c202015-02-27 09:15:59 +0000132 }
Zachary Turner0683be22017-05-14 01:13:40 +0000133
134 WithColor(Printer, PDB_ColorItem::Comment).get()
135 << formatv(" | sizeof={0,3}", Symbol.getLength());
Zachary Turner7797c722015-03-02 04:39:56 +0000136 Printer << "] (";
Zachary Turner29c69102015-02-23 05:58:34 +0000137
Zachary Turner7797c722015-03-02 04:39:56 +0000138 if (Symbol.hasFramePointer()) {
139 WithColor(Printer, PDB_ColorItem::Register).get()
140 << Symbol.getLocalBasePointerRegisterId();
141 } else {
142 WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
143 }
144 Printer << ") ";
Zachary Turner29c69102015-02-23 05:58:34 +0000145
Zachary Turner9a818ad2015-02-22 22:03:38 +0000146 if (Symbol.isVirtual() || Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000147 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000148
149 auto Signature = Symbol.getSignature();
150 if (!Signature) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000151 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000152 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000153 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000154 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000155 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000156 return;
157 }
158
159 auto ReturnType = Signature->getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000160 ReturnType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000161 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000162
Zachary Turner29c69102015-02-23 05:58:34 +0000163 auto ClassParent = Symbol.getClassParent();
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000164 CallingConvention CC = Signature->getCallingConvention();
Zachary Turner29c69102015-02-23 05:58:34 +0000165 if (Pointer != FunctionDumper::PointerType::None)
Zachary Turner2d11c202015-02-27 09:15:59 +0000166 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000167
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000168 if ((ClassParent && CC != CallingConvention::ThisCall) ||
169 (!ClassParent && CC != CallingConvention::NearStdCall)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000170 WithColor(Printer, PDB_ColorItem::Keyword).get()
171 << Signature->getCallingConvention() << " ";
172 }
173 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000174 if (Pointer != FunctionDumper::PointerType::None) {
175 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000176 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000177 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000178 Printer << "&";
179 Printer << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000180 }
Zachary Turner9a818ad2015-02-22 22:03:38 +0000181
Zachary Turner2d11c202015-02-27 09:15:59 +0000182 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000183 if (auto Arguments = Symbol.getArguments()) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000184 uint32_t Index = 0;
Zachary Turner29c69102015-02-23 05:58:34 +0000185 while (auto Arg = Arguments->getNext()) {
186 auto ArgType = Arg->getType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000187 ArgType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000188 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
189 << Arg->getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000190 if (++Index < Arguments->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000191 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000192 }
193 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000194 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000195 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000196 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000197 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000198 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000199 if (Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000200 Printer << " = 0";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000201}
202
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000203void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000204 auto ElementType = Symbol.getElementType();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000205
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000206 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000207 Printer << "[";
208 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
209 Printer << "]";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000210}
211
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000212void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000213 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000214 Dumper.start(Symbol);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000215}
216
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000217void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000218 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
219 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000220}
221
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000222void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000223 // 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;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000229 Type->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000230}
231
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000232void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000233 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
234 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000235}
236
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000237void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner1b1a70f2017-04-10 06:14:09 +0000238 auto PointeeType = Symbol.getPointeeType();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000239 if (!PointeeType)
240 return;
241
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000242 if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000243 FunctionDumper NestedDumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000244 PointerType Pointer =
245 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000246 NestedDumper.start(*FuncSig, nullptr, Pointer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000247 } else {
248 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000249 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000250 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000251 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000252 PointeeType->dump(*this);
253 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner9a818ad2015-02-22 22:03:38 +0000254 }
255}
256
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000257void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000258 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000259}