PrettyVariableDumper.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //===- PrettyVariableDumper.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 "PrettyVariableDumper.h"
  9. #include "LinePrinter.h"
  10. #include "PrettyBuiltinDumper.h"
  11. #include "PrettyFunctionDumper.h"
  12. #include "llvm-pdbutil.h"
  13. #include "llvm/DebugInfo/PDB/IPDBSession.h"
  14. #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
  15. #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
  16. #include "llvm/DebugInfo/PDB/PDBSymbolTypeArray.h"
  17. #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
  18. #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
  19. #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
  20. #include "llvm/DebugInfo/PDB/PDBSymbolTypePointer.h"
  21. #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
  22. #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
  23. #include "llvm/DebugInfo/PDB/PDBTypes.h"
  24. #include "llvm/Support/Format.h"
  25. using namespace llvm;
  26. using namespace llvm::codeview;
  27. using namespace llvm::pdb;
  28. VariableDumper::VariableDumper(LinePrinter &P)
  29. : PDBSymDumper(true), Printer(P) {}
  30. void VariableDumper::start(const PDBSymbolData &Var, uint32_t Offset) {
  31. if (Var.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated)
  32. return;
  33. if (Printer.IsSymbolExcluded(Var.getName()))
  34. return;
  35. auto VarType = Var.getType();
  36. uint64_t Length = VarType->getRawSymbol().getLength();
  37. switch (auto LocType = Var.getLocationType()) {
  38. case PDB_LocType::Static:
  39. Printer.NewLine();
  40. Printer << "data [";
  41. WithColor(Printer, PDB_ColorItem::Address).get()
  42. << format_hex(Var.getVirtualAddress(), 10);
  43. Printer << ", sizeof=" << Length << "] ";
  44. WithColor(Printer, PDB_ColorItem::Keyword).get() << "static ";
  45. dumpSymbolTypeAndName(*VarType, Var.getName());
  46. break;
  47. case PDB_LocType::Constant:
  48. if (isa<PDBSymbolTypeEnum>(*VarType))
  49. break;
  50. Printer.NewLine();
  51. Printer << "data [sizeof=" << Length << "] ";
  52. dumpSymbolTypeAndName(*VarType, Var.getName());
  53. Printer << " = ";
  54. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getValue();
  55. break;
  56. case PDB_LocType::ThisRel:
  57. Printer.NewLine();
  58. Printer << "data ";
  59. WithColor(Printer, PDB_ColorItem::Offset).get()
  60. << "+" << format_hex(Offset + Var.getOffset(), 4)
  61. << " [sizeof=" << Length << "] ";
  62. dumpSymbolTypeAndName(*VarType, Var.getName());
  63. break;
  64. case PDB_LocType::BitField:
  65. Printer.NewLine();
  66. Printer << "data ";
  67. WithColor(Printer, PDB_ColorItem::Offset).get()
  68. << "+" << format_hex(Offset + Var.getOffset(), 4)
  69. << " [sizeof=" << Length << "] ";
  70. dumpSymbolTypeAndName(*VarType, Var.getName());
  71. Printer << " : ";
  72. WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Var.getLength();
  73. break;
  74. default:
  75. Printer.NewLine();
  76. Printer << "data [sizeof=" << Length << "] ";
  77. Printer << "unknown(" << LocType << ") ";
  78. WithColor(Printer, PDB_ColorItem::Identifier).get() << Var.getName();
  79. break;
  80. }
  81. }
  82. void VariableDumper::startVbptr(uint32_t Offset, uint32_t Size) {
  83. Printer.NewLine();
  84. Printer << "vbptr ";
  85. WithColor(Printer, PDB_ColorItem::Offset).get()
  86. << "+" << format_hex(Offset, 4) << " [sizeof=" << Size << "] ";
  87. }
  88. void VariableDumper::start(const PDBSymbolTypeVTable &Var, uint32_t Offset) {
  89. Printer.NewLine();
  90. Printer << "vfptr ";
  91. auto VTableType = cast<PDBSymbolTypePointer>(Var.getType());
  92. uint32_t PointerSize = VTableType->getLength();
  93. WithColor(Printer, PDB_ColorItem::Offset).get()
  94. << "+" << format_hex(Offset + Var.getOffset(), 4)
  95. << " [sizeof=" << PointerSize << "] ";
  96. }
  97. void VariableDumper::dump(const PDBSymbolTypeArray &Symbol) {
  98. auto ElementType = Symbol.getElementType();
  99. assert(ElementType);
  100. if (!ElementType)
  101. return;
  102. ElementType->dump(*this);
  103. }
  104. void VariableDumper::dumpRight(const PDBSymbolTypeArray &Symbol) {
  105. auto ElementType = Symbol.getElementType();
  106. assert(ElementType);
  107. if (!ElementType)
  108. return;
  109. Printer << '[' << Symbol.getCount() << ']';
  110. ElementType->dumpRight(*this);
  111. }
  112. void VariableDumper::dump(const PDBSymbolTypeBuiltin &Symbol) {
  113. BuiltinDumper Dumper(Printer);
  114. Dumper.start(Symbol);
  115. }
  116. void VariableDumper::dump(const PDBSymbolTypeEnum &Symbol) {
  117. WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
  118. }
  119. void VariableDumper::dump(const PDBSymbolTypeFunctionSig &Symbol) {
  120. auto ReturnType = Symbol.getReturnType();
  121. ReturnType->dump(*this);
  122. Printer << " ";
  123. uint32_t ClassParentId = Symbol.getClassParentId();
  124. auto ClassParent =
  125. Symbol.getSession().getConcreteSymbolById<PDBSymbolTypeUDT>(
  126. ClassParentId);
  127. if (ClassParent) {
  128. WithColor(Printer, PDB_ColorItem::Identifier).get()
  129. << ClassParent->getName();
  130. Printer << "::";
  131. }
  132. }
  133. void VariableDumper::dumpRight(const PDBSymbolTypeFunctionSig &Symbol) {
  134. Printer << "(";
  135. if (auto Arguments = Symbol.getArguments()) {
  136. uint32_t Index = 0;
  137. while (auto Arg = Arguments->getNext()) {
  138. Arg->dump(*this);
  139. if (++Index < Arguments->getChildCount())
  140. Printer << ", ";
  141. }
  142. }
  143. Printer << ")";
  144. if (Symbol.isConstType())
  145. WithColor(Printer, PDB_ColorItem::Keyword).get() << " const";
  146. if (Symbol.isVolatileType())
  147. WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile";
  148. if (Symbol.getRawSymbol().isRestrictedType())
  149. WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict";
  150. }
  151. void VariableDumper::dump(const PDBSymbolTypePointer &Symbol) {
  152. auto PointeeType = Symbol.getPointeeType();
  153. if (!PointeeType)
  154. return;
  155. PointeeType->dump(*this);
  156. if (auto FuncSig = unique_dyn_cast<PDBSymbolTypeFunctionSig>(PointeeType)) {
  157. // A hack to get the calling convention in the right spot.
  158. Printer << " (";
  159. PDB_CallingConv CC = FuncSig->getCallingConvention();
  160. WithColor(Printer, PDB_ColorItem::Keyword).get() << CC << " ";
  161. } else if (isa<PDBSymbolTypeArray>(PointeeType)) {
  162. Printer << " (";
  163. }
  164. Printer << (Symbol.isReference() ? "&" : "*");
  165. if (Symbol.isConstType())
  166. WithColor(Printer, PDB_ColorItem::Keyword).get() << " const ";
  167. if (Symbol.isVolatileType())
  168. WithColor(Printer, PDB_ColorItem::Keyword).get() << " volatile ";
  169. if (Symbol.getRawSymbol().isRestrictedType())
  170. WithColor(Printer, PDB_ColorItem::Keyword).get() << " __restrict ";
  171. }
  172. void VariableDumper::dumpRight(const PDBSymbolTypePointer &Symbol) {
  173. auto PointeeType = Symbol.getPointeeType();
  174. assert(PointeeType);
  175. if (!PointeeType)
  176. return;
  177. if (isa<PDBSymbolTypeFunctionSig>(PointeeType) ||
  178. isa<PDBSymbolTypeArray>(PointeeType)) {
  179. Printer << ")";
  180. }
  181. PointeeType->dumpRight(*this);
  182. }
  183. void VariableDumper::dump(const PDBSymbolTypeTypedef &Symbol) {
  184. WithColor(Printer, PDB_ColorItem::Keyword).get() << "typedef ";
  185. WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
  186. }
  187. void VariableDumper::dump(const PDBSymbolTypeUDT &Symbol) {
  188. WithColor(Printer, PDB_ColorItem::Type).get() << Symbol.getName();
  189. }
  190. void VariableDumper::dumpSymbolTypeAndName(const PDBSymbol &Type,
  191. StringRef Name) {
  192. Type.dump(*this);
  193. WithColor(Printer, PDB_ColorItem::Identifier).get() << " " << Name;
  194. Type.dumpRight(*this);
  195. }