Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 1 | //===- PrettyClassLayoutGraphicalDumper.h -----------------------*- C++ -*-===// |
| 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 | |
| 10 | #include "PrettyClassLayoutGraphicalDumper.h" |
| 11 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 12 | #include "LinePrinter.h" |
| 13 | #include "PrettyClassDefinitionDumper.h" |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 14 | #include "PrettyEnumDumper.h" |
| 15 | #include "PrettyFunctionDumper.h" |
| 16 | #include "PrettyTypedefDumper.h" |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 17 | #include "PrettyVariableDumper.h" |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 18 | #include "PrettyVariableDumper.h" |
Zachary Turner | bd336e4 | 2017-06-09 20:46:17 +0000 | [diff] [blame^] | 19 | #include "llvm-pdbutil.h" |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 20 | |
| 21 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
| 22 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeBaseClass.h" |
| 23 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 24 | #include "llvm/DebugInfo/PDB/UDTLayout.h" |
| 25 | #include "llvm/Support/Format.h" |
| 26 | |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | using namespace llvm::pdb; |
| 29 | |
| 30 | PrettyClassLayoutGraphicalDumper::PrettyClassLayoutGraphicalDumper( |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 31 | LinePrinter &P, uint32_t RecurseLevel, uint32_t InitialOffset) |
| 32 | : PDBSymDumper(true), Printer(P), RecursionLevel(RecurseLevel), |
| 33 | ClassOffsetZero(InitialOffset), CurrentAbsoluteOffset(InitialOffset) {} |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 34 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 35 | bool PrettyClassLayoutGraphicalDumper::start(const UDTLayoutBase &Layout) { |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 36 | |
| 37 | if (RecursionLevel == 1 && |
| 38 | opts::pretty::ClassFormat == opts::pretty::ClassDefinitionFormat::All) { |
| 39 | for (auto &Other : Layout.other_items()) |
| 40 | Other->dump(*this); |
| 41 | for (auto &Func : Layout.funcs()) |
| 42 | Func->dump(*this); |
| 43 | } |
| 44 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 45 | const BitVector &UseMap = Layout.usedBytes(); |
| 46 | int NextPaddingByte = UseMap.find_first_unset(); |
| 47 | |
| 48 | for (auto &Item : Layout.layout_items()) { |
| 49 | // Calculate the absolute offset of the first byte of the next field. |
| 50 | uint32_t RelativeOffset = Item->getOffsetInParent(); |
| 51 | CurrentAbsoluteOffset = ClassOffsetZero + RelativeOffset; |
| 52 | |
| 53 | // Since there is storage there, it should be set! However, this might |
| 54 | // be an empty base, in which case it could extend outside the bounds of |
| 55 | // the parent class. |
| 56 | if (RelativeOffset < UseMap.size() && (Item->getSize() > 0)) { |
| 57 | assert(UseMap.test(RelativeOffset)); |
| 58 | |
| 59 | // If there is any remaining padding in this class, and the offset of the |
| 60 | // new item is after the padding, then we must have just jumped over some |
| 61 | // padding. Print a padding row and then look for where the next block |
| 62 | // of padding begins. |
| 63 | if ((NextPaddingByte >= 0) && |
| 64 | (RelativeOffset > uint32_t(NextPaddingByte))) { |
| 65 | printPaddingRow(RelativeOffset - NextPaddingByte); |
| 66 | NextPaddingByte = UseMap.find_next_unset(RelativeOffset); |
| 67 | } |
| 68 | } |
| 69 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 70 | CurrentItem = Item; |
| 71 | if (Item->isVBPtr()) { |
| 72 | VTableLayoutItem &Layout = static_cast<VTableLayoutItem &>(*CurrentItem); |
| 73 | |
| 74 | VariableDumper VarDumper(Printer); |
| 75 | VarDumper.startVbptr(CurrentAbsoluteOffset, Layout.getSize()); |
| 76 | } else { |
| 77 | if (auto Sym = Item->getSymbol()) |
| 78 | Sym->dump(*this); |
| 79 | } |
| 80 | |
| 81 | if (Item->getLayoutSize() > 0) { |
| 82 | uint32_t Prev = RelativeOffset + Item->getLayoutSize() - 1; |
Zachary Turner | f5cdd40 | 2017-05-26 00:15:15 +0000 | [diff] [blame] | 83 | if (Prev < UseMap.size()) |
| 84 | NextPaddingByte = UseMap.find_next_unset(Prev); |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 85 | } |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 88 | auto TailPadding = Layout.tailPadding(); |
| 89 | if (TailPadding > 0) { |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 90 | if (TailPadding != 1 || Layout.getSize() != 1) { |
| 91 | Printer.NewLine(); |
| 92 | WithColor(Printer, PDB_ColorItem::Padding).get() |
| 93 | << "<padding> (" << TailPadding << " bytes)"; |
| 94 | DumpedAnything = true; |
| 95 | } |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return DumpedAnything; |
| 99 | } |
| 100 | |
| 101 | void PrettyClassLayoutGraphicalDumper::printPaddingRow(uint32_t Amount) { |
| 102 | if (Amount == 0) |
| 103 | return; |
| 104 | |
| 105 | Printer.NewLine(); |
| 106 | WithColor(Printer, PDB_ColorItem::Padding).get() << "<padding> (" << Amount |
| 107 | << " bytes)"; |
| 108 | DumpedAnything = true; |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | void PrettyClassLayoutGraphicalDumper::dump( |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 112 | const PDBSymbolTypeBaseClass &Symbol) { |
| 113 | assert(CurrentItem != nullptr); |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 114 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 115 | Printer.NewLine(); |
| 116 | BaseClassLayout &Layout = static_cast<BaseClassLayout &>(*CurrentItem); |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 117 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 118 | std::string Label = "base"; |
| 119 | if (Layout.isVirtualBase()) { |
| 120 | Label.insert(Label.begin(), 'v'); |
| 121 | if (Layout.getBase().isIndirectVirtualBaseClass()) |
| 122 | Label.insert(Label.begin(), 'i'); |
| 123 | } |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 124 | Printer << Label << " "; |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 125 | |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 126 | uint32_t Size = Layout.isEmptyBase() ? 1 : Layout.getLayoutSize(); |
| 127 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 128 | WithColor(Printer, PDB_ColorItem::Offset).get() |
Zachary Turner | 1690164 | 2017-04-24 17:47:24 +0000 | [diff] [blame] | 129 | << "+" << format_hex(CurrentAbsoluteOffset, 4) << " [sizeof=" << Size |
| 130 | << "] "; |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 131 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 132 | WithColor(Printer, PDB_ColorItem::Identifier).get() << Layout.getName(); |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 133 | |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 134 | if (shouldRecurse()) { |
| 135 | Printer.Indent(); |
| 136 | uint32_t ChildOffsetZero = ClassOffsetZero + Layout.getOffsetInParent(); |
| 137 | PrettyClassLayoutGraphicalDumper BaseDumper(Printer, RecursionLevel + 1, |
| 138 | ChildOffsetZero); |
| 139 | DumpedAnything |= BaseDumper.start(Layout); |
| 140 | Printer.Unindent(); |
| 141 | } |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 142 | |
| 143 | DumpedAnything = true; |
| 144 | } |
| 145 | |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 146 | bool PrettyClassLayoutGraphicalDumper::shouldRecurse() const { |
| 147 | uint32_t Limit = opts::pretty::ClassRecursionDepth; |
| 148 | if (Limit == 0) |
| 149 | return true; |
| 150 | return RecursionLevel < Limit; |
| 151 | } |
| 152 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 153 | void PrettyClassLayoutGraphicalDumper::dump(const PDBSymbolData &Symbol) { |
| 154 | assert(CurrentItem != nullptr); |
| 155 | |
| 156 | DataMemberLayoutItem &Layout = |
| 157 | static_cast<DataMemberLayoutItem &>(*CurrentItem); |
| 158 | |
| 159 | VariableDumper VarDumper(Printer); |
| 160 | VarDumper.start(Symbol, ClassOffsetZero); |
| 161 | |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 162 | if (Layout.hasUDTLayout() && shouldRecurse()) { |
| 163 | uint32_t ChildOffsetZero = ClassOffsetZero + Layout.getOffsetInParent(); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 164 | Printer.Indent(); |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 165 | PrettyClassLayoutGraphicalDumper TypeDumper(Printer, RecursionLevel + 1, |
| 166 | ChildOffsetZero); |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 167 | TypeDumper.start(Layout.getUDTLayout()); |
| 168 | Printer.Unindent(); |
| 169 | } |
| 170 | |
| 171 | DumpedAnything = true; |
| 172 | } |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 173 | |
| 174 | void PrettyClassLayoutGraphicalDumper::dump(const PDBSymbolTypeVTable &Symbol) { |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 175 | assert(CurrentItem != nullptr); |
| 176 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 177 | VariableDumper VarDumper(Printer); |
| 178 | VarDumper.start(Symbol, ClassOffsetZero); |
| 179 | |
Zachary Turner | 4dc4f01 | 2017-04-13 21:11:00 +0000 | [diff] [blame] | 180 | DumpedAnything = true; |
Zachary Turner | 9e7dda3 | 2017-04-12 23:18:51 +0000 | [diff] [blame] | 181 | } |
Zachary Turner | da949c1 | 2017-04-24 17:47:52 +0000 | [diff] [blame] | 182 | |
| 183 | void PrettyClassLayoutGraphicalDumper::dump(const PDBSymbolTypeEnum &Symbol) { |
| 184 | DumpedAnything = true; |
| 185 | Printer.NewLine(); |
| 186 | EnumDumper Dumper(Printer); |
| 187 | Dumper.start(Symbol); |
| 188 | } |
| 189 | |
| 190 | void PrettyClassLayoutGraphicalDumper::dump( |
| 191 | const PDBSymbolTypeTypedef &Symbol) { |
| 192 | DumpedAnything = true; |
| 193 | Printer.NewLine(); |
| 194 | TypedefDumper Dumper(Printer); |
| 195 | Dumper.start(Symbol); |
| 196 | } |
| 197 | |
| 198 | void PrettyClassLayoutGraphicalDumper::dump( |
| 199 | const PDBSymbolTypeBuiltin &Symbol) {} |
| 200 | |
| 201 | void PrettyClassLayoutGraphicalDumper::dump(const PDBSymbolTypeUDT &Symbol) {} |
| 202 | |
| 203 | void PrettyClassLayoutGraphicalDumper::dump(const PDBSymbolFunc &Symbol) { |
| 204 | if (Printer.IsSymbolExcluded(Symbol.getName())) |
| 205 | return; |
| 206 | if (Symbol.isCompilerGenerated() && opts::pretty::ExcludeCompilerGenerated) |
| 207 | return; |
| 208 | if (Symbol.getLength() == 0 && !Symbol.isPureVirtual() && |
| 209 | !Symbol.isIntroVirtualFunction()) |
| 210 | return; |
| 211 | |
| 212 | DumpedAnything = true; |
| 213 | Printer.NewLine(); |
| 214 | FunctionDumper Dumper(Printer); |
| 215 | Dumper.start(Symbol, FunctionDumper::PointerType::None); |
| 216 | } |