Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 1 | //===- DWARFDie.cpp -------------------------------------------------------===// |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +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 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/DWARF/DWARFDie.h" |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 11 | #include "SyntaxHighlighting.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/None.h" |
| 13 | #include "llvm/ADT/Optional.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/Dwarf.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h" |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/DWARF/DWARFUnit.h" |
| 21 | #include "llvm/Support/DataExtractor.h" |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 25 | #include <algorithm> |
| 26 | #include <cassert> |
| 27 | #include <cinttypes> |
| 28 | #include <cstdint> |
| 29 | #include <string> |
| 30 | #include <utility> |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 31 | |
| 32 | using namespace llvm; |
| 33 | using namespace dwarf; |
| 34 | using namespace syntax; |
| 35 | |
Eugene Zelenko | e94042c | 2017-02-27 23:43:14 +0000 | [diff] [blame] | 36 | static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 37 | OS << " ("; |
| 38 | do { |
| 39 | uint64_t Shift = countTrailingZeros(Val); |
| 40 | assert(Shift < 64 && "undefined behavior"); |
| 41 | uint64_t Bit = 1ULL << Shift; |
| 42 | auto PropName = ApplePropertyString(Bit); |
| 43 | if (!PropName.empty()) |
| 44 | OS << PropName; |
| 45 | else |
| 46 | OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit); |
| 47 | if (!(Val ^= Bit)) |
| 48 | break; |
| 49 | OS << ", "; |
| 50 | } while (true); |
| 51 | OS << ")"; |
| 52 | } |
| 53 | |
| 54 | static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges, |
| 55 | unsigned AddressSize, unsigned Indent) { |
| 56 | if (Ranges.empty()) |
| 57 | return; |
| 58 | |
| 59 | for (const auto &Range: Ranges) { |
| 60 | OS << '\n'; |
| 61 | OS.indent(Indent); |
| 62 | OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", |
George Rimar | 4671f2e | 2017-05-16 12:30:59 +0000 | [diff] [blame] | 63 | AddressSize*2, Range.LowPC, |
| 64 | AddressSize*2, Range.HighPC); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, |
| 69 | uint32_t *OffsetPtr, dwarf::Attribute Attr, |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 70 | dwarf::Form Form, unsigned Indent, |
| 71 | DIDumpOptions DumpOpts) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 72 | if (!Die.isValid()) |
| 73 | return; |
| 74 | const char BaseIndent[] = " "; |
| 75 | OS << BaseIndent; |
| 76 | OS.indent(Indent+2); |
| 77 | auto attrString = AttributeString(Attr); |
| 78 | if (!attrString.empty()) |
| 79 | WithColor(OS, syntax::Attribute) << attrString; |
| 80 | else |
| 81 | WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr); |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 82 | |
| 83 | if (!DumpOpts.Brief) { |
| 84 | auto formString = FormEncodingString(Form); |
| 85 | if (!formString.empty()) |
| 86 | OS << " [" << formString << ']'; |
| 87 | else |
| 88 | OS << format(" [DW_FORM_Unknown_%x]", Form); |
| 89 | } |
| 90 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 91 | DWARFUnit *U = Die.getDwarfUnit(); |
| 92 | DWARFFormValue formValue(Form); |
| 93 | |
| 94 | if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U)) |
| 95 | return; |
| 96 | |
| 97 | OS << "\t("; |
| 98 | |
| 99 | StringRef Name; |
| 100 | std::string File; |
| 101 | auto Color = syntax::Enumerator; |
| 102 | if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) { |
| 103 | Color = syntax::String; |
| 104 | if (const auto *LT = U->getContext().getLineTableForUnit(U)) |
| 105 | if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) { |
| 106 | File = '"' + File + '"'; |
| 107 | Name = File; |
| 108 | } |
| 109 | } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant()) |
| 110 | Name = AttributeValueString(Attr, *Val); |
| 111 | |
| 112 | if (!Name.empty()) |
| 113 | WithColor(OS, Color) << Name; |
| 114 | else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line) |
| 115 | OS << *formValue.getAsUnsignedConstant(); |
| 116 | else |
| 117 | formValue.dump(OS); |
| 118 | |
| 119 | // We have dumped the attribute raw value. For some attributes |
| 120 | // having both the raw value and the pretty-printed value is |
| 121 | // interesting. These attributes are handled below. |
| 122 | if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) { |
| 123 | if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName)) |
| 124 | OS << " \"" << Name << '\"'; |
| 125 | } else if (Attr == DW_AT_APPLE_property_attribute) { |
| 126 | if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant()) |
| 127 | dumpApplePropertyAttribute(OS, *OptVal); |
| 128 | } else if (Attr == DW_AT_ranges) { |
| 129 | dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(), |
| 130 | sizeof(BaseIndent)+Indent+4); |
| 131 | } |
| 132 | |
| 133 | OS << ")\n"; |
| 134 | } |
| 135 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 136 | bool DWARFDie::isSubprogramDIE() const { |
| 137 | return getTag() == DW_TAG_subprogram; |
| 138 | } |
| 139 | |
| 140 | bool DWARFDie::isSubroutineDIE() const { |
| 141 | auto Tag = getTag(); |
| 142 | return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine; |
| 143 | } |
| 144 | |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 145 | Optional<DWARFFormValue> |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 146 | DWARFDie::find(dwarf::Attribute Attr) const { |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 147 | if (!isValid()) |
| 148 | return None; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 149 | auto AbbrevDecl = getAbbreviationDeclarationPtr(); |
| 150 | if (AbbrevDecl) |
Greg Clayton | 1cbf3fa | 2016-12-13 23:20:56 +0000 | [diff] [blame] | 151 | return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U); |
| 152 | return None; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 155 | Optional<DWARFFormValue> |
Greg Clayton | c109bbe | 2017-01-13 22:32:12 +0000 | [diff] [blame] | 156 | DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const { |
| 157 | if (!isValid()) |
| 158 | return None; |
| 159 | auto AbbrevDecl = getAbbreviationDeclarationPtr(); |
| 160 | if (AbbrevDecl) { |
| 161 | for (auto Attr : Attrs) { |
| 162 | if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U)) |
| 163 | return Value; |
| 164 | } |
| 165 | } |
| 166 | return None; |
| 167 | } |
| 168 | |
| 169 | Optional<DWARFFormValue> |
| 170 | DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const { |
| 171 | if (!isValid()) |
| 172 | return None; |
David Blaikie | 1914c82 | 2017-03-13 21:46:37 +0000 | [diff] [blame] | 173 | auto Die = *this; |
| 174 | if (auto Value = Die.find(Attrs)) |
Greg Clayton | c109bbe | 2017-01-13 22:32:12 +0000 | [diff] [blame] | 175 | return Value; |
David Blaikie | 1914c82 | 2017-03-13 21:46:37 +0000 | [diff] [blame] | 176 | if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) |
| 177 | Die = D; |
| 178 | if (auto Value = Die.find(Attrs)) |
| 179 | return Value; |
| 180 | if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification)) |
| 181 | Die = D; |
| 182 | if (auto Value = Die.find(Attrs)) |
| 183 | return Value; |
Greg Clayton | c109bbe | 2017-01-13 22:32:12 +0000 | [diff] [blame] | 184 | return None; |
| 185 | } |
| 186 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 187 | DWARFDie |
| 188 | DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const { |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 189 | auto SpecRef = toReference(find(Attr)); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 190 | if (SpecRef) { |
| 191 | auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 192 | if (SpecUnit) |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 193 | return SpecUnit->getDIEForOffset(*SpecRef); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 194 | } |
| 195 | return DWARFDie(); |
| 196 | } |
| 197 | |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 198 | Optional<uint64_t> |
| 199 | DWARFDie::getRangesBaseAttribute() const { |
Greg Clayton | c109bbe | 2017-01-13 22:32:12 +0000 | [diff] [blame] | 200 | return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base})); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Greg Clayton | 2520c9e | 2016-12-19 20:36:41 +0000 | [diff] [blame] | 203 | Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const { |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 204 | if (auto FormValue = find(DW_AT_high_pc)) { |
Greg Clayton | 2520c9e | 2016-12-19 20:36:41 +0000 | [diff] [blame] | 205 | if (auto Address = FormValue->getAsAddress()) { |
| 206 | // High PC is an address. |
| 207 | return Address; |
| 208 | } |
| 209 | if (auto Offset = FormValue->getAsUnsignedConstant()) { |
| 210 | // High PC is an offset from LowPC. |
| 211 | return LowPC + *Offset; |
| 212 | } |
| 213 | } |
| 214 | return None; |
| 215 | } |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 216 | |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 217 | bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, |
| 218 | uint64_t &SectionIndex) const { |
| 219 | auto F = find(DW_AT_low_pc); |
| 220 | auto LowPcAddr = toAddress(F); |
Greg Clayton | 2520c9e | 2016-12-19 20:36:41 +0000 | [diff] [blame] | 221 | if (!LowPcAddr) |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 222 | return false; |
Greg Clayton | 2520c9e | 2016-12-19 20:36:41 +0000 | [diff] [blame] | 223 | if (auto HighPcAddr = getHighPC(*LowPcAddr)) { |
| 224 | LowPC = *LowPcAddr; |
| 225 | HighPC = *HighPcAddr; |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 226 | SectionIndex = F->getSectionIndex(); |
Greg Clayton | 2520c9e | 2016-12-19 20:36:41 +0000 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | return false; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | DWARFAddressRangesVector |
| 233 | DWARFDie::getAddressRanges() const { |
| 234 | if (isNULL()) |
| 235 | return DWARFAddressRangesVector(); |
| 236 | // Single range specified by low/high PC. |
George Rimar | a25d329 | 2017-05-27 18:10:23 +0000 | [diff] [blame] | 237 | uint64_t LowPC, HighPC, Index; |
| 238 | if (getLowAndHighPC(LowPC, HighPC, Index)) |
| 239 | return {{LowPC, HighPC, Index}}; |
George Rimar | 4671f2e | 2017-05-16 12:30:59 +0000 | [diff] [blame] | 240 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 241 | // Multiple ranges from .debug_ranges section. |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 242 | auto RangesOffset = toSectionOffset(find(DW_AT_ranges)); |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 243 | if (RangesOffset) { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 244 | DWARFDebugRangeList RangeList; |
Greg Clayton | 52fe1f6 | 2016-12-14 22:38:08 +0000 | [diff] [blame] | 245 | if (U->extractRangeList(*RangesOffset, RangeList)) |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 246 | return RangeList.getAbsoluteRanges(U->getBaseAddress()); |
| 247 | } |
| 248 | return DWARFAddressRangesVector(); |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const { |
| 253 | if (isNULL()) |
| 254 | return; |
| 255 | if (isSubprogramDIE()) { |
| 256 | const auto &DIERanges = getAddressRanges(); |
| 257 | Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end()); |
| 258 | } |
| 259 | |
Greg Clayton | 93e4fe8 | 2017-01-05 23:47:37 +0000 | [diff] [blame] | 260 | for (auto Child: children()) |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 261 | Child.collectChildrenAddressRanges(Ranges); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const { |
| 265 | for (const auto& R : getAddressRanges()) { |
George Rimar | 4671f2e | 2017-05-16 12:30:59 +0000 | [diff] [blame] | 266 | if (R.LowPC <= Address && Address < R.HighPC) |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 267 | return true; |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | const char * |
| 273 | DWARFDie::getSubroutineName(DINameKind Kind) const { |
| 274 | if (!isSubroutineDIE()) |
| 275 | return nullptr; |
| 276 | return getName(Kind); |
| 277 | } |
| 278 | |
| 279 | const char * |
| 280 | DWARFDie::getName(DINameKind Kind) const { |
| 281 | if (!isValid() || Kind == DINameKind::None) |
| 282 | return nullptr; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 283 | // Try to get mangled name only if it was asked for. |
| 284 | if (Kind == DINameKind::LinkageName) { |
Greg Clayton | c109bbe | 2017-01-13 22:32:12 +0000 | [diff] [blame] | 285 | if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name, |
| 286 | DW_AT_linkage_name}), nullptr)) |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 287 | return Name; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 288 | } |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 289 | if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr)) |
| 290 | return Name; |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 291 | return nullptr; |
| 292 | } |
| 293 | |
David Blaikie | efc4eba | 2017-02-06 20:19:02 +0000 | [diff] [blame] | 294 | uint64_t DWARFDie::getDeclLine() const { |
| 295 | return toUnsigned(findRecursively(DW_AT_decl_line), 0); |
| 296 | } |
| 297 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 298 | void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 299 | uint32_t &CallColumn, |
| 300 | uint32_t &CallDiscriminator) const { |
Greg Clayton | 97d2218 | 2017-01-13 21:08:18 +0000 | [diff] [blame] | 301 | CallFile = toUnsigned(find(DW_AT_call_file), 0); |
| 302 | CallLine = toUnsigned(find(DW_AT_call_line), 0); |
| 303 | CallColumn = toUnsigned(find(DW_AT_call_column), 0); |
Dehao Chen | ef700d5 | 2017-04-17 20:10:39 +0000 | [diff] [blame] | 304 | CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 307 | void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent, |
| 308 | DIDumpOptions DumpOpts) const { |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 309 | if (!isValid()) |
| 310 | return; |
Paul Robinson | 17536b9 | 2017-06-29 16:52:08 +0000 | [diff] [blame^] | 311 | DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor(); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 312 | const uint32_t Offset = getOffset(); |
| 313 | uint32_t offset = Offset; |
| 314 | |
| 315 | if (debug_info_data.isValidOffset(offset)) { |
| 316 | uint32_t abbrCode = debug_info_data.getULEB128(&offset); |
| 317 | WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset); |
| 318 | |
| 319 | if (abbrCode) { |
| 320 | auto AbbrevDecl = getAbbreviationDeclarationPtr(); |
| 321 | if (AbbrevDecl) { |
| 322 | auto tagString = TagString(getTag()); |
| 323 | if (!tagString.empty()) |
| 324 | WithColor(OS, syntax::Tag).get().indent(Indent) << tagString; |
| 325 | else |
| 326 | WithColor(OS, syntax::Tag).get().indent(Indent) |
| 327 | << format("DW_TAG_Unknown_%x", getTag()); |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 328 | |
| 329 | if (!DumpOpts.Brief) |
| 330 | OS << format(" [%u] %c", abbrCode, |
| 331 | AbbrevDecl->hasChildren() ? '*' : ' '); |
| 332 | OS << '\n'; |
| 333 | |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 334 | // Dump all data in the DIE for the attributes. |
| 335 | for (const auto &AttrSpec : AbbrevDecl->attributes()) { |
Victor Leschuk | 96d9981 | 2017-02-25 13:15:57 +0000 | [diff] [blame] | 336 | if (AttrSpec.Form == DW_FORM_implicit_const) { |
| 337 | // We are dumping .debug_info section , |
| 338 | // implicit_const attribute values are not really stored here, |
| 339 | // but in .debug_abbrev section. So we just skip such attrs. |
| 340 | continue; |
| 341 | } |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 342 | dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form, |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 343 | Indent, DumpOpts); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | DWARFDie child = getFirstChild(); |
| 347 | if (RecurseDepth > 0 && child) { |
| 348 | while (child) { |
Adrian Prantl | 318d119 | 2017-06-06 23:28:45 +0000 | [diff] [blame] | 349 | child.dump(OS, RecurseDepth-1, Indent+2, DumpOpts); |
Greg Clayton | c8c1032 | 2016-12-13 18:25:19 +0000 | [diff] [blame] | 350 | child = child.getSibling(); |
| 351 | } |
| 352 | } |
| 353 | } else { |
| 354 | OS << "Abbreviation code not found in 'debug_abbrev' class for code: " |
| 355 | << abbrCode << '\n'; |
| 356 | } |
| 357 | } else { |
| 358 | OS.indent(Indent) << "NULL\n"; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Greg Clayton | 78a07bf | 2016-12-21 21:37:06 +0000 | [diff] [blame] | 363 | DWARFDie DWARFDie::getParent() const { |
| 364 | if (isValid()) |
| 365 | return U->getParent(Die); |
| 366 | return DWARFDie(); |
| 367 | } |
| 368 | |
| 369 | DWARFDie DWARFDie::getSibling() const { |
| 370 | if (isValid()) |
| 371 | return U->getSibling(Die); |
| 372 | return DWARFDie(); |
| 373 | } |
Greg Clayton | 0e62ee7 | 2017-01-13 00:13:42 +0000 | [diff] [blame] | 374 | |
| 375 | iterator_range<DWARFDie::attribute_iterator> |
| 376 | DWARFDie::attributes() const { |
| 377 | return make_range(attribute_iterator(*this, false), |
| 378 | attribute_iterator(*this, true)); |
| 379 | } |
| 380 | |
| 381 | DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) : |
| 382 | Die(D), AttrValue(0), Index(0) { |
| 383 | auto AbbrDecl = Die.getAbbreviationDeclarationPtr(); |
| 384 | assert(AbbrDecl && "Must have abbreviation declaration"); |
| 385 | if (End) { |
| 386 | // This is the end iterator so we set the index to the attribute count. |
| 387 | Index = AbbrDecl->getNumAttributes(); |
| 388 | } else { |
| 389 | // This is the begin iterator so we extract the value for this->Index. |
| 390 | AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize(); |
| 391 | updateForIndex(*AbbrDecl, 0); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void DWARFDie::attribute_iterator::updateForIndex( |
| 396 | const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) { |
| 397 | Index = I; |
| 398 | // AbbrDecl must be valid befor calling this function. |
| 399 | auto NumAttrs = AbbrDecl.getNumAttributes(); |
| 400 | if (Index < NumAttrs) { |
| 401 | AttrValue.Attr = AbbrDecl.getAttrByIndex(Index); |
| 402 | // Add the previous byte size of any previous attribute value. |
| 403 | AttrValue.Offset += AttrValue.ByteSize; |
| 404 | AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index)); |
| 405 | uint32_t ParseOffset = AttrValue.Offset; |
| 406 | auto U = Die.getDwarfUnit(); |
| 407 | assert(U && "Die must have valid DWARF unit"); |
| 408 | bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(), |
| 409 | &ParseOffset, U); |
| 410 | (void)b; |
| 411 | assert(b && "extractValue cannot fail on fully parsed DWARF"); |
| 412 | AttrValue.ByteSize = ParseOffset - AttrValue.Offset; |
| 413 | } else { |
| 414 | assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only"); |
| 415 | AttrValue.clear(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() { |
| 420 | if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr()) |
| 421 | updateForIndex(*AbbrDecl, Index + 1); |
| 422 | return *this; |
| 423 | } |