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