| 1 | //===- PrettyFunctionDumper.cpp --------------------------------- *- C++ *-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "PrettyFunctionDumper.h" |
| 10 | #include "PrettyBuiltinDumper.h" |
| 11 | |
| 12 | #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h" |
| 13 | #include "llvm/DebugInfo/PDB/IPDBSession.h" |
| 14 | #include "llvm/DebugInfo/PDB/Native/LinePrinter.h" |
| 15 | #include "llvm/DebugInfo/PDB/PDBExtras.h" |
| 16 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
| 17 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 18 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" |
| 19 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" |
| 20 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h" |
| 21 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.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 | #include "llvm/Support/FormatVariadic.h" |
| 30 | |
| 31 | using namespace llvm; |
| 32 | using namespace llvm::codeview; |
| 33 | using namespace llvm::pdb; |
| 34 | |
| 35 | namespace { |
| 36 | template <class T> |
| 37 | void dumpClassParentWithScopeOperator(const T &Symbol, LinePrinter &Printer, |
| 38 | FunctionDumper &Dumper) { |
| 39 | uint32_t ClassParentId = Symbol.getClassParentId(); |
| 40 | auto ClassParent = |
| 41 | Symbol.getSession().template getConcreteSymbolById<PDBSymbolTypeUDT>( |
| 42 | ClassParentId); |
| 43 | if (!ClassParent) |
| 44 | return; |
| 45 | |
| 46 | WithColor(Printer, PDB_ColorItem::Type).get() << ClassParent->getName(); |
| 47 | Printer << "::" ; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | FunctionDumper::FunctionDumper(LinePrinter &P) |
| 52 | : PDBSymDumper(true), Printer(P) {} |
| 53 | |
| 54 | void FunctionDumper::start(const PDBSymbolTypeFunctionSig &Symbol, |
| 55 | const char *Name, PointerType Pointer) { |
| 56 | auto ReturnType = Symbol.getReturnType(); |
| 57 | if (!ReturnType) |
| 58 | Printer << "<unknown-type>" ; |
| 59 | else |
| 60 | ReturnType->dump(Dumper&: *this); |
| 61 | Printer << " " ; |
| 62 | uint32_t ClassParentId = Symbol.getClassParentId(); |
| 63 | auto ClassParent = |
| 64 | Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>( |
| 65 | SymbolId: ClassParentId); |
| 66 | |
| 67 | PDB_CallingConv CC = Symbol.getCallingConvention(); |
| 68 | bool ShouldDumpCallingConvention = true; |
| 69 | if ((ClassParent && CC == CallingConvention::ThisCall) || |
| 70 | (!ClassParent && CC == CallingConvention::NearStdCall)) { |
| 71 | ShouldDumpCallingConvention = false; |
| 72 | } |
| 73 | |
| 74 | if (Pointer == PointerType::None) { |
| 75 | if (ShouldDumpCallingConvention) |
| 76 | WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " " ; |
| 77 | if (ClassParent) { |
| 78 | Printer << "(" ; |
| 79 | WithColor(Printer, PDB_ColorItem::Identifier).get() |
| 80 | << ClassParent->getName(); |
| 81 | Printer << "::)" ; |
| 82 | } |
| 83 | } else { |
| 84 | Printer << "(" ; |
| 85 | if (ShouldDumpCallingConvention) |
| 86 | WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " " ; |
| 87 | if (ClassParent) { |
| 88 | WithColor(Printer, PDB_ColorItem::Identifier).get() |
| 89 | << ClassParent->getName(); |
| 90 | Printer << "::" ; |
| 91 | } |
| 92 | if (Pointer == PointerType::Reference) |
| 93 | Printer << "&" ; |
| 94 | else |
| 95 | Printer << "*" ; |
| 96 | if (Name) |
| 97 | WithColor(Printer, PDB_ColorItem::Identifier).get() << Name; |
| 98 | Printer << ")" ; |
| 99 | } |
| 100 | |
| 101 | Printer << "(" ; |
| 102 | if (auto ChildEnum = Symbol.getArguments()) { |
| 103 | uint32_t Index = 0; |
| 104 | while (auto Arg = ChildEnum->getNext()) { |
| 105 | Arg->dump(Dumper&: *this); |
| 106 | if (++Index < ChildEnum->getChildCount()) |
| 107 | Printer << ", " ; |
| 108 | } |
| 109 | } |
| 110 | Printer << ")" ; |
| 111 | |
| 112 | if (Symbol.isConstType()) |
| 113 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " const" ; |
| 114 | if (Symbol.isVolatileType()) |
| 115 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile" ; |
| 116 | } |
| 117 | |
| 118 | void FunctionDumper::start(const PDBSymbolFunc &Symbol, PointerType Pointer) { |
| 119 | uint64_t FuncStart = Symbol.getVirtualAddress(); |
| 120 | uint64_t FuncEnd = FuncStart + Symbol.getLength(); |
| 121 | |
| 122 | Printer << "func [" ; |
| 123 | WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(N: FuncStart, Width: 10); |
| 124 | if (auto DebugStart = Symbol.findOneChild<PDBSymbolFuncDebugStart>()) { |
| 125 | uint64_t Prologue = DebugStart->getVirtualAddress() - FuncStart; |
| 126 | WithColor(Printer, PDB_ColorItem::Offset).get() |
| 127 | << formatv(Fmt: "+{0,2}" , Vals&: Prologue); |
| 128 | } |
| 129 | Printer << " - " ; |
| 130 | WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(N: FuncEnd, Width: 10); |
| 131 | if (auto DebugEnd = Symbol.findOneChild<PDBSymbolFuncDebugEnd>()) { |
| 132 | uint64_t Epilogue = FuncEnd - DebugEnd->getVirtualAddress(); |
| 133 | WithColor(Printer, PDB_ColorItem::Offset).get() |
| 134 | << formatv(Fmt: "-{0,2}" , Vals&: Epilogue); |
| 135 | } |
| 136 | |
| 137 | WithColor(Printer, PDB_ColorItem::Comment).get() |
| 138 | << formatv(Fmt: " | sizeof={0,3}" , Vals: Symbol.getLength()); |
| 139 | Printer << "] (" ; |
| 140 | |
| 141 | if (Symbol.hasFramePointer()) { |
| 142 | WithColor(Printer, PDB_ColorItem::Register).get() |
| 143 | << CPURegister{Symbol.getRawSymbol().getPlatform(), |
| 144 | Symbol.getLocalBasePointerRegisterId()}; |
| 145 | } else { |
| 146 | WithColor(Printer, PDB_ColorItem::Register).get() << "FPO" ; |
| 147 | } |
| 148 | Printer << ") " ; |
| 149 | |
| 150 | if (Symbol.isVirtual() || Symbol.isPureVirtual()) |
| 151 | WithColor(Printer, PDB_ColorItem::Keyword).get() << "virtual " ; |
| 152 | |
| 153 | auto Signature = Symbol.getSignature(); |
| 154 | if (!Signature) { |
| 155 | WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName(); |
| 156 | if (Pointer == PointerType::Pointer) |
| 157 | Printer << "*" ; |
| 158 | else if (Pointer == FunctionDumper::PointerType::Reference) |
| 159 | Printer << "&" ; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | auto ReturnType = Signature->getReturnType(); |
| 164 | ReturnType->dump(Dumper&: *this); |
| 165 | Printer << " " ; |
| 166 | |
| 167 | auto ClassParent = Symbol.getClassParent(); |
| 168 | CallingConvention CC = Signature->getCallingConvention(); |
| 169 | if (Pointer != FunctionDumper::PointerType::None) |
| 170 | Printer << "(" ; |
| 171 | |
| 172 | if ((ClassParent && CC != CallingConvention::ThisCall) || |
| 173 | (!ClassParent && CC != CallingConvention::NearStdCall)) { |
| 174 | WithColor(Printer, PDB_ColorItem::Keyword).get() |
| 175 | << Signature->getCallingConvention() << " " ; |
| 176 | } |
| 177 | WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName(); |
| 178 | if (Pointer != FunctionDumper::PointerType::None) { |
| 179 | if (Pointer == PointerType::Pointer) |
| 180 | Printer << "*" ; |
| 181 | else if (Pointer == FunctionDumper::PointerType::Reference) |
| 182 | Printer << "&" ; |
| 183 | Printer << ")" ; |
| 184 | } |
| 185 | |
| 186 | Printer << "(" ; |
| 187 | if (auto Arguments = Symbol.getArguments()) { |
| 188 | uint32_t Index = 0; |
| 189 | while (auto Arg = Arguments->getNext()) { |
| 190 | auto ArgType = Arg->getType(); |
| 191 | ArgType->dump(Dumper&: *this); |
| 192 | WithColor(Printer, PDB_ColorItem::Identifier).get() << " " |
| 193 | << Arg->getName(); |
| 194 | if (++Index < Arguments->getChildCount()) |
| 195 | Printer << ", " ; |
| 196 | } |
| 197 | if (Signature->isCVarArgs()) |
| 198 | Printer << ", ..." ; |
| 199 | } |
| 200 | Printer << ")" ; |
| 201 | if (Symbol.isConstType()) |
| 202 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " const" ; |
| 203 | if (Symbol.isVolatileType()) |
| 204 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile" ; |
| 205 | if (Symbol.isPureVirtual()) |
| 206 | Printer << " = 0" ; |
| 207 | } |
| 208 | |
| 209 | void FunctionDumper::dump(const PDBSymbolTypeArray &Symbol) { |
| 210 | auto ElementType = Symbol.getElementType(); |
| 211 | |
| 212 | ElementType->dump(Dumper&: *this); |
| 213 | Printer << "[" ; |
| 214 | WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Symbol.getLength(); |
| 215 | Printer << "]" ; |
| 216 | } |
| 217 | |
| 218 | void FunctionDumper::dump(const PDBSymbolTypeBuiltin &Symbol) { |
| 219 | BuiltinDumper Dumper(Printer); |
| 220 | Dumper.start(Symbol); |
| 221 | } |
| 222 | |
| 223 | void FunctionDumper::dump(const PDBSymbolTypeEnum &Symbol) { |
| 224 | dumpClassParentWithScopeOperator(Symbol, Printer, Dumper&: *this); |
| 225 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
| 226 | } |
| 227 | |
| 228 | void FunctionDumper::dump(const PDBSymbolTypeFunctionArg &Symbol) { |
| 229 | // PDBSymbolTypeFunctionArg is just a shim over the real argument. Just drill |
| 230 | // through to the real thing and dump it. |
| 231 | uint32_t TypeId = Symbol.getTypeId(); |
| 232 | auto Type = Symbol.getSession().getSymbolById(SymbolId: TypeId); |
| 233 | if (Type) |
| 234 | Type->dump(Dumper&: *this); |
| 235 | else |
| 236 | Printer << "<unknown-type>" ; |
| 237 | } |
| 238 | |
| 239 | void FunctionDumper::dump(const PDBSymbolTypeTypedef &Symbol) { |
| 240 | dumpClassParentWithScopeOperator(Symbol, Printer, Dumper&: *this); |
| 241 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
| 242 | } |
| 243 | |
| 244 | void FunctionDumper::dump(const PDBSymbolTypePointer &Symbol) { |
| 245 | auto PointeeType = Symbol.getPointeeType(); |
| 246 | if (!PointeeType) |
| 247 | return; |
| 248 | |
| 249 | if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(Val&: PointeeType)) { |
| 250 | FunctionDumper NestedDumper(Printer); |
| 251 | PointerType Pointer = |
| 252 | Symbol.isReference() ? PointerType::Reference : PointerType::Pointer; |
| 253 | NestedDumper.start(Symbol: *FuncSig, Name: nullptr, Pointer); |
| 254 | } else { |
| 255 | if (Symbol.isConstType()) |
| 256 | WithColor(Printer, PDB_ColorItem::Keyword).get() << "const " ; |
| 257 | if (Symbol.isVolatileType()) |
| 258 | WithColor(Printer, PDB_ColorItem::Keyword).get() << "volatile " ; |
| 259 | PointeeType->dump(Dumper&: *this); |
| 260 | Printer << (Symbol.isReference() ? "&" : "*" ); |
| 261 | |
| 262 | if (Symbol.getRawSymbol().isRestrictedType()) |
| 263 | WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict" ; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | void FunctionDumper::dump(const PDBSymbolTypeUDT &Symbol) { |
| 268 | WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName(); |
| 269 | } |
| 270 | |