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