PrettyVariableDumper.cpp 7.4 KB

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