blob: 7f4181722e7c355af6326c9fecfa5c3ae9f1f8a7 [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;
Reid Kleckner72e2ba72016-01-13 19:32:35 +000030using namespace llvm::codeview;
Zachary Turneraea59922015-02-22 22:20:26 +000031
Zachary Turner9a818ad2015-02-22 22:03:38 +000032namespace {
33template <class T>
Zachary Turner2d11c202015-02-27 09:15:59 +000034void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer,
Zachary Turner9a818ad2015-02-22 22:03:38 +000035 llvm::FunctionDumper &Dumper) {
36 uint32_t ClassParentId = Symbol.getClassParentId();
37 auto ClassParent =
David Majnemer3f45d402015-02-22 22:33:57 +000038 Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>(
Zachary Turner9a818ad2015-02-22 22:03:38 +000039 ClassParentId);
40 if (!ClassParent)
41 return;
42
Zachary Turner2d11c202015-02-27 09:15:59 +000043 WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName();
44 Printer << "::";
Zachary Turner9a818ad2015-02-22 22:03:38 +000045}
46}
47
Zachary Turner2d11c202015-02-27 09:15:59 +000048FunctionDumper::FunctionDumper(LinePrinter &P)
49 : PDBSymDumper(true), Printer(P) {}
Zachary Turner9a818ad2015-02-22 22:03:38 +000050
51void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol,
Zachary Turnerb52d08d2015-03-01 06:51:29 +000052 const char *Name, PointerType Pointer) {
Zachary Turner9a818ad2015-02-22 22:03:38 +000053 auto ReturnType = Symbol.getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +000054 ReturnType->dump(*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
Reid Kleckner72e2ba72016-01-13 19:32:35 +000061 CallingConvention CC = Symbol.getCallingConvention();
Zachary Turner29c69102015-02-23 05:58:34 +000062 bool ShouldDumpCallingConvention = true;
Reid Kleckner72e2ba72016-01-13 19:32:35 +000063 if ((ClassParent && CC == CallingConvention::ThisCall) ||
64 (!ClassParent && CC == CallingConvention::NearStdCall)) {
Zachary Turner29c69102015-02-23 05:58:34 +000065 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()) {
Zachary Turnerb52d08d2015-03-01 06:51:29 +000099 Arg->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000100 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 Turnerb52d08d2015-03-01 06:51:29 +0000112void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000113 uint64_t FuncStart = Symbol.getVirtualAddress();
114 uint64_t FuncEnd = FuncStart + Symbol.getLength();
Zachary Turner29c69102015-02-23 05:58:34 +0000115
Zachary Turner7797c722015-03-02 04:39:56 +0000116 Printer << "func [";
117 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncStart, 10);
Zachary Turner2d11c202015-02-27 09:15:59 +0000118 if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000119 uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart;
Zachary Turner2d11c202015-02-27 09:15:59 +0000120 WithColor(Printer, PDB_ColorItem::Offset).get() << "+" << Prologue;
121 }
Zachary Turner7797c722015-03-02 04:39:56 +0000122 Printer << " - ";
123 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(FuncEnd, 10);
Zachary Turner2d11c202015-02-27 09:15:59 +0000124 if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000125 uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress();
Zachary Turner2d11c202015-02-27 09:15:59 +0000126 WithColor(Printer, PDB_ColorItem::Offset).get() << "-" << Epilogue;
127 }
Zachary Turner7797c722015-03-02 04:39:56 +0000128 Printer << "] (";
Zachary Turner29c69102015-02-23 05:58:34 +0000129
Zachary Turner7797c722015-03-02 04:39:56 +0000130 if (Symbol.hasFramePointer()) {
131 WithColor(Printer, PDB_ColorItem::Register).get()
132 << Symbol.getLocalBasePointerRegisterId();
133 } else {
134 WithColor(Printer, PDB_ColorItem::Register).get() << "FPO";
135 }
136 Printer << ") ";
Zachary Turner29c69102015-02-23 05:58:34 +0000137
Zachary Turner9a818ad2015-02-22 22:03:38 +0000138 if (Symbol.isVirtual() || Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000139 WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000140
141 auto Signature = Symbol.getSignature();
142 if (!Signature) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000143 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000144 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000145 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000146 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000147 Printer << "&";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000148 return;
149 }
150
151 auto ReturnType = Signature->getReturnType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000152 ReturnType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000153 Printer << " ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000154
Zachary Turner29c69102015-02-23 05:58:34 +0000155 auto ClassParent = Symbol.getClassParent();
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000156 CallingConvention CC = Signature->getCallingConvention();
Zachary Turner29c69102015-02-23 05:58:34 +0000157 if (Pointer != FunctionDumper::PointerType::None)
Zachary Turner2d11c202015-02-27 09:15:59 +0000158 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000159
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000160 if ((ClassParent && CC != CallingConvention::ThisCall) ||
161 (!ClassParent && CC != CallingConvention::NearStdCall)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000162 WithColor(Printer, PDB_ColorItem::Keyword).get()
163 << Signature->getCallingConvention() << " ";
164 }
165 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000166 if (Pointer != FunctionDumper::PointerType::None) {
167 if (Pointer == PointerType::Pointer)
Zachary Turner2d11c202015-02-27 09:15:59 +0000168 Printer << "*";
Zachary Turner29c69102015-02-23 05:58:34 +0000169 else if (Pointer == FunctionDumper::PointerType::Reference)
Zachary Turner2d11c202015-02-27 09:15:59 +0000170 Printer << "&";
171 Printer << ")";
Zachary Turner29c69102015-02-23 05:58:34 +0000172 }
Zachary Turner9a818ad2015-02-22 22:03:38 +0000173
Zachary Turner2d11c202015-02-27 09:15:59 +0000174 Printer << "(";
Zachary Turner29c69102015-02-23 05:58:34 +0000175 if (auto Arguments = Symbol.getArguments()) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000176 uint32_t Index = 0;
Zachary Turner29c69102015-02-23 05:58:34 +0000177 while (auto Arg = Arguments->getNext()) {
178 auto ArgType = Arg->getType();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000179 ArgType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000180 WithColor(Printer, PDB_ColorItem::Identifier).get() << " "
181 << Arg->getName();
Zachary Turner29c69102015-02-23 05:58:34 +0000182 if (++Index < Arguments->getChildCount())
Zachary Turner2d11c202015-02-27 09:15:59 +0000183 Printer << ", ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000184 }
185 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000186 Printer << ")";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000187 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000188 WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000189 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000190 WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000191 if (Symbol.isPureVirtual())
Zachary Turner2d11c202015-02-27 09:15:59 +0000192 Printer << " = 0";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000193}
194
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000195void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000196 uint32_t ElementTypeId = Symbol.getTypeId();
197 auto ElementType = Symbol.getSession().getSymbolById(ElementTypeId);
198 if (!ElementType)
199 return;
200
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000201 ElementType->dump(*this);
Zachary Turner2d11c202015-02-27 09:15:59 +0000202 Printer << "[";
203 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength();
204 Printer << "]";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000205}
206
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000207void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000208 BuiltinDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000209 Dumper.start(Symbol);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000210}
211
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000212void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000213 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
214 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000215}
216
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000217void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000218 // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill
Zachary Turner29c69102015-02-23 05:58:34 +0000219 // through to the real thing and dump it.
Zachary Turner9a818ad2015-02-22 22:03:38 +0000220 uint32_t TypeId = Symbol.getTypeId();
221 auto Type = Symbol.getSession().getSymbolById(TypeId);
222 if (!Type)
223 return;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000224 Type->dump(*this);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000225}
226
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000227void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000228 dumpClassParentWithScopeOperator(Symbol, Printer, *this);
229 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000230}
231
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000232void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) {
Zachary Turner9a818ad2015-02-22 22:03:38 +0000233 uint32_t PointeeId = Symbol.getTypeId();
234 auto PointeeType = Symbol.getSession().getSymbolById(PointeeId);
235 if (!PointeeType)
236 return;
237
238 if (auto FuncSig = dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType.get())) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000239 FunctionDumper NestedDumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000240 PointerType Pointer =
241 Symbol.isReference() ? PointerType::Reference : PointerType::Pointer;
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000242 NestedDumper.start(*FuncSig, nullptr, Pointer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000243 } else {
244 if (Symbol.isConstType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000245 WithColor(Printer, PDB_ColorItem::Keyword).get() << "const ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000246 if (Symbol.isVolatileType())
Zachary Turner2d11c202015-02-27 09:15:59 +0000247 WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile ";
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000248 PointeeType->dump(*this);
249 Printer << (Symbol.isReference() ? "&" : "*");
Zachary Turner9a818ad2015-02-22 22:03:38 +0000250 }
251}
252
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000253void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000254 WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000255}