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