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