PrettyFunctionDumper.cpp 9.0 KB

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