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