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