Eric Christopher | c2ce004 | 2012-08-23 00:52:51 +0000 | [diff] [blame] | 1 | //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===// |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +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 | |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 10 | #include "SyntaxHighlighting.h" |
Zachary Turner | 82af943 | 2015-01-30 18:07:45 +0000 | [diff] [blame] | 11 | #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 13 | #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" |
| 14 | #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h" |
| 15 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
Frederic Riss | b3c9912 | 2014-10-10 15:51:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/DataTypes.h" |
Eric Christopher | 2cbd576 | 2013-01-07 19:32:41 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Dwarf.h" |
| 19 | #include "llvm/Support/Format.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | using namespace llvm; |
| 22 | using namespace dwarf; |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 23 | using namespace syntax; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 24 | |
Frederic Riss | 58ed53c | 2014-09-22 12:35:53 +0000 | [diff] [blame] | 25 | // Small helper to extract a DIE pointed by a reference |
| 26 | // attribute. It looks up the Unit containing the DIE and calls |
| 27 | // DIE.extractFast with the right unit. Returns new unit on success, |
| 28 | // nullptr otherwise. |
| 29 | static const DWARFUnit *findUnitAndExtractFast(DWARFDebugInfoEntryMinimal &DIE, |
| 30 | const DWARFUnit *Unit, |
| 31 | uint32_t *Offset) { |
| 32 | Unit = Unit->getUnitSection().getUnitForOffset(*Offset); |
| 33 | return (Unit && DIE.extractFast(Unit, Offset)) ? Unit : nullptr; |
| 34 | } |
| 35 | |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 36 | void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u, |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 37 | unsigned recurseDepth, |
| 38 | unsigned indent) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 39 | DataExtractor debug_info_data = u->getDebugInfoExtractor(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 40 | uint32_t offset = Offset; |
| 41 | |
| 42 | if (debug_info_data.isValidOffset(offset)) { |
Benjamin Kramer | f7e0a31 | 2011-11-05 15:35:00 +0000 | [diff] [blame] | 43 | uint32_t abbrCode = debug_info_data.getULEB128(&offset); |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 44 | WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 45 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 46 | if (abbrCode) { |
| 47 | if (AbbrevDecl) { |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 48 | auto tagString = TagString(getTag()); |
| 49 | if (!tagString.empty()) |
| 50 | WithColor(OS, syntax::Tag).get().indent(indent) << tagString; |
| 51 | else |
| 52 | WithColor(OS, syntax::Tag).get().indent(indent) |
| 53 | << format("DW_TAG_Unknown_%x", getTag()); |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 54 | |
Benjamin Kramer | 9bca64f | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 55 | OS << format(" [%u] %c\n", abbrCode, |
| 56 | AbbrevDecl->hasChildren() ? '*' : ' '); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 57 | |
Eric Christopher | 7f5870c | 2013-01-07 03:27:58 +0000 | [diff] [blame] | 58 | // Dump all data in the DIE for the attributes. |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 59 | for (const auto &AttrSpec : AbbrevDecl->attributes()) { |
| 60 | dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | const DWARFDebugInfoEntryMinimal *child = getFirstChild(); |
| 64 | if (recurseDepth > 0 && child) { |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 65 | while (child) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 66 | child->dump(OS, u, recurseDepth-1, indent+2); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 67 | child = child->getSibling(); |
| 68 | } |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 69 | } |
| 70 | } else { |
| 71 | OS << "Abbreviation code not found in 'debug_abbrev' class for code: " |
| 72 | << abbrCode << '\n'; |
| 73 | } |
| 74 | } else { |
Benjamin Kramer | f915acc | 2011-09-14 17:54:56 +0000 | [diff] [blame] | 75 | OS.indent(indent) << "NULL\n"; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Frederic Riss | b3c9912 | 2014-10-10 15:51:10 +0000 | [diff] [blame] | 80 | static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) { |
| 81 | OS << " ("; |
| 82 | do { |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 83 | uint64_t Shift = countTrailingZeros(Val); |
| 84 | assert(Shift < 64 && "undefined behavior"); |
| 85 | uint64_t Bit = 1ULL << Shift; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 86 | auto PropName = ApplePropertyString(Bit); |
| 87 | if (!PropName.empty()) |
Frederic Riss | b3c9912 | 2014-10-10 15:51:10 +0000 | [diff] [blame] | 88 | OS << PropName; |
| 89 | else |
| 90 | OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit); |
| 91 | if (!(Val ^= Bit)) |
| 92 | break; |
| 93 | OS << ", "; |
| 94 | } while (true); |
| 95 | OS << ")"; |
| 96 | } |
| 97 | |
Frederic Riss | e939b43 | 2014-10-23 04:08:34 +0000 | [diff] [blame] | 98 | static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges, |
| 99 | unsigned AddressSize, unsigned Indent) { |
| 100 | if (Ranges.empty()) |
| 101 | return; |
| 102 | |
| 103 | for (const auto &Range: Ranges) { |
| 104 | OS << '\n'; |
| 105 | OS.indent(Indent); |
| 106 | OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", |
| 107 | AddressSize*2, Range.first, |
| 108 | AddressSize*2, Range.second); |
| 109 | } |
| 110 | } |
| 111 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 112 | void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 113 | DWARFUnit *u, |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 114 | uint32_t *offset_ptr, |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 115 | dwarf::Attribute attr, |
| 116 | dwarf::Form form, |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 117 | unsigned indent) const { |
Frederic Riss | e939b43 | 2014-10-23 04:08:34 +0000 | [diff] [blame] | 118 | const char BaseIndent[] = " "; |
| 119 | OS << BaseIndent; |
Benjamin Kramer | 9bca64f | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 120 | OS.indent(indent+2); |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 121 | auto attrString = AttributeString(attr); |
| 122 | if (!attrString.empty()) |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 123 | WithColor(OS, syntax::Attribute) << attrString; |
Benjamin Kramer | 9bca64f | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 124 | else |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 125 | WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr); |
| 126 | |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 127 | auto formString = FormEncodingString(form); |
| 128 | if (!formString.empty()) |
Benjamin Kramer | 9bca64f | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 129 | OS << " [" << formString << ']'; |
| 130 | else |
| 131 | OS << format(" [DW_FORM_Unknown_%x]", form); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 132 | |
| 133 | DWARFFormValue formValue(form); |
| 134 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 135 | if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u)) |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 136 | return; |
| 137 | |
| 138 | OS << "\t("; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 139 | |
| 140 | StringRef Name; |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 141 | std::string File; |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 142 | auto Color = syntax::Enumerator; |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 143 | if (attr == DW_AT_decl_file || attr == DW_AT_call_file) { |
David Blaikie | cdec7ee | 2015-11-17 00:41:02 +0000 | [diff] [blame] | 144 | Color = syntax::String; |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 145 | if (const auto *LT = u->getContext().getLineTableForUnit(u)) |
| 146 | if (LT->getFileNameByIndex( |
| 147 | formValue.getAsUnsignedConstant().getValue(), |
| 148 | u->getCompilationDir(), |
| 149 | DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) { |
| 150 | File = '"' + File + '"'; |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 151 | Name = File; |
Frederic Riss | 955724e | 2014-09-22 12:36:04 +0000 | [diff] [blame] | 152 | } |
| 153 | } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant()) |
Frederic Riss | 878065b | 2014-09-04 19:39:20 +0000 | [diff] [blame] | 154 | Name = AttributeValueString(attr, *Val); |
| 155 | |
Mehdi Amini | 149f6ea | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 156 | if (!Name.empty()) |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 157 | WithColor(OS, Color) << Name; |
| 158 | else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) |
Frederic Riss | 0982e69 | 2014-09-05 07:21:50 +0000 | [diff] [blame] | 159 | OS << *formValue.getAsUnsignedConstant(); |
Adrian Prantl | 0c36a75 | 2015-01-06 16:50:25 +0000 | [diff] [blame] | 160 | else |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame^] | 161 | formValue.dump(OS); |
Frederic Riss | 878065b | 2014-09-04 19:39:20 +0000 | [diff] [blame] | 162 | |
Frederic Riss | d1cfc3c | 2014-10-06 03:36:31 +0000 | [diff] [blame] | 163 | // We have dumped the attribute raw value. For some attributes |
| 164 | // having both the raw value and the pretty-printed value is |
| 165 | // interesting. These attributes are handled below. |
Alexey Samsonov | bf19a57 | 2015-05-19 20:29:28 +0000 | [diff] [blame] | 166 | if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) { |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame^] | 167 | Optional<uint64_t> Ref = formValue.getAsReference(); |
Alexey Samsonov | bf19a57 | 2015-05-19 20:29:28 +0000 | [diff] [blame] | 168 | if (Ref.hasValue()) { |
| 169 | uint32_t RefOffset = Ref.getValue(); |
| 170 | DWARFDebugInfoEntryMinimal DIE; |
| 171 | if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &RefOffset)) |
| 172 | if (const char *Name = DIE.getName(RefU, DINameKind::LinkageName)) |
| 173 | OS << " \"" << Name << '\"'; |
| 174 | } |
Frederic Riss | b3c9912 | 2014-10-10 15:51:10 +0000 | [diff] [blame] | 175 | } else if (attr == DW_AT_APPLE_property_attribute) { |
| 176 | if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant()) |
| 177 | dumpApplePropertyAttribute(OS, *OptVal); |
Frederic Riss | e939b43 | 2014-10-23 04:08:34 +0000 | [diff] [blame] | 178 | } else if (attr == DW_AT_ranges) { |
| 179 | dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(), |
| 180 | sizeof(BaseIndent)+indent+4); |
Frederic Riss | d1cfc3c | 2014-10-06 03:36:31 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 183 | OS << ")\n"; |
| 184 | } |
| 185 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 186 | bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U, |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 187 | uint32_t *OffsetPtr) { |
| 188 | Offset = *OffsetPtr; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 189 | DataExtractor DebugInfoData = U->getDebugInfoExtractor(); |
Alexey Samsonov | 330b893 | 2013-10-28 23:58:58 +0000 | [diff] [blame] | 190 | uint32_t UEndOffset = U->getNextUnitOffset(); |
| 191 | if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset)) |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 192 | return false; |
| 193 | uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr); |
| 194 | if (0 == AbbrCode) { |
| 195 | // NULL debug tag entry. |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 196 | AbbrevDecl = nullptr; |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 197 | return true; |
| 198 | } |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 199 | AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 200 | if (nullptr == AbbrevDecl) { |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 201 | // Restore the original offset. |
| 202 | *OffsetPtr = Offset; |
| 203 | return false; |
| 204 | } |
Alexey Samsonov | 330b893 | 2013-10-28 23:58:58 +0000 | [diff] [blame] | 205 | ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes( |
| 206 | U->getAddressByteSize(), U->getVersion()); |
| 207 | assert(FixedFormSizes.size() > 0); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 208 | |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 209 | // Skip all data in the .debug_info for the attributes |
Alexey Samsonov | 1eabf98 | 2014-03-13 07:52:54 +0000 | [diff] [blame] | 210 | for (const auto &AttrSpec : AbbrevDecl->attributes()) { |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 211 | auto Form = AttrSpec.Form; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 212 | |
Alexey Samsonov | 330b893 | 2013-10-28 23:58:58 +0000 | [diff] [blame] | 213 | uint8_t FixedFormSize = |
| 214 | (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0; |
| 215 | if (FixedFormSize) |
| 216 | *OffsetPtr += FixedFormSize; |
| 217 | else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) { |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 218 | // Restore the original offset. |
| 219 | *OffsetPtr = Offset; |
| 220 | return false; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
Alexey Samsonov | c03f2ee | 2013-04-08 14:37:16 +0000 | [diff] [blame] | 223 | return true; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 226 | bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const { |
| 227 | return getTag() == DW_TAG_subprogram; |
| 228 | } |
| 229 | |
| 230 | bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const { |
| 231 | uint32_t Tag = getTag(); |
| 232 | return Tag == DW_TAG_subprogram || |
| 233 | Tag == DW_TAG_inlined_subroutine; |
| 234 | } |
| 235 | |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 236 | bool DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFUnit *U, |
| 237 | dwarf::Attribute Attr, DWARFFormValue &FormValue) const { |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 238 | if (!AbbrevDecl) |
| 239 | return false; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 240 | |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 241 | uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr); |
| 242 | if (AttrIdx == -1U) |
| 243 | return false; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 244 | |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 245 | DataExtractor DebugInfoData = U->getDebugInfoExtractor(); |
| 246 | uint32_t DebugInfoOffset = getOffset(); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 247 | |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 248 | // Skip the abbreviation code so we are at the data for the attributes |
| 249 | DebugInfoData.getULEB128(&DebugInfoOffset); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 250 | |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 251 | // Skip preceding attribute values. |
| 252 | for (uint32_t i = 0; i < AttrIdx; ++i) { |
| 253 | DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i), |
| 254 | DebugInfoData, &DebugInfoOffset, U); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 257 | FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx)); |
| 258 | return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U); |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 261 | const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString( |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 262 | const DWARFUnit *U, dwarf::Attribute Attr, |
| 263 | const char *FailValue) const { |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 264 | DWARFFormValue FormValue; |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 265 | if (!getAttributeValue(U, Attr, FormValue)) |
| 266 | return FailValue; |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame^] | 267 | Optional<const char *> Result = FormValue.getAsCString(); |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 268 | return Result.hasValue() ? Result.getValue() : FailValue; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 271 | uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress( |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 272 | const DWARFUnit *U, dwarf::Attribute Attr, |
| 273 | uint64_t FailValue) const { |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 274 | DWARFFormValue FormValue; |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 275 | if (!getAttributeValue(U, Attr, FormValue)) |
| 276 | return FailValue; |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame^] | 277 | Optional<uint64_t> Result = FormValue.getAsAddress(); |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 278 | return Result.hasValue() ? Result.getValue() : FailValue; |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 281 | uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant( |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 282 | const DWARFUnit *U, dwarf::Attribute Attr, |
| 283 | uint64_t FailValue) const { |
Alexey Samsonov | e3ba81b | 2013-08-27 09:20:22 +0000 | [diff] [blame] | 284 | DWARFFormValue FormValue; |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 285 | if (!getAttributeValue(U, Attr, FormValue)) |
| 286 | return FailValue; |
| 287 | Optional<uint64_t> Result = FormValue.getAsUnsignedConstant(); |
| 288 | return Result.hasValue() ? Result.getValue() : FailValue; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 289 | } |
| 290 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 291 | uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference( |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 292 | const DWARFUnit *U, dwarf::Attribute Attr, |
| 293 | uint64_t FailValue) const { |
Alexey Samsonov | caabb0e | 2013-10-17 13:28:16 +0000 | [diff] [blame] | 294 | DWARFFormValue FormValue; |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 295 | if (!getAttributeValue(U, Attr, FormValue)) |
| 296 | return FailValue; |
Greg Clayton | cddab27 | 2016-10-31 16:46:02 +0000 | [diff] [blame^] | 297 | Optional<uint64_t> Result = FormValue.getAsReference(); |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 298 | return Result.hasValue() ? Result.getValue() : FailValue; |
| 299 | } |
| 300 | |
| 301 | uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset( |
Greg Clayton | 6c27376 | 2016-10-27 16:32:04 +0000 | [diff] [blame] | 302 | const DWARFUnit *U, dwarf::Attribute Attr, |
| 303 | uint64_t FailValue) const { |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 304 | DWARFFormValue FormValue; |
| 305 | if (!getAttributeValue(U, Attr, FormValue)) |
| 306 | return FailValue; |
| 307 | Optional<uint64_t> Result = FormValue.getAsSectionOffset(); |
| 308 | return Result.hasValue() ? Result.getValue() : FailValue; |
Benjamin Kramer | aa2f78f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 309 | } |
Benjamin Kramer | 3266493 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 310 | |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 311 | uint64_t |
| 312 | DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U, |
| 313 | uint64_t FailValue) const { |
| 314 | uint64_t Result = |
Adrian Prantl | c4fbbcf | 2016-10-28 17:59:50 +0000 | [diff] [blame] | 315 | getAttributeValueAsSectionOffset(U, DW_AT_rnglists_base, -1ULL); |
Alexey Samsonov | aa90998 | 2014-06-13 22:31:03 +0000 | [diff] [blame] | 316 | if (Result != -1ULL) |
| 317 | return Result; |
| 318 | return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue); |
| 319 | } |
| 320 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 321 | bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U, |
Eric Christopher | 206cf64 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 322 | uint64_t &LowPC, |
| 323 | uint64_t &HighPC) const { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 324 | LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL); |
Alexey Samsonov | 7614212 | 2013-10-28 23:15:15 +0000 | [diff] [blame] | 325 | if (LowPC == -1ULL) |
| 326 | return false; |
| 327 | HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL); |
| 328 | if (HighPC == -1ULL) { |
| 329 | // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case |
| 330 | // it represents function size. |
| 331 | HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL); |
| 332 | if (HighPC != -1ULL) |
| 333 | HighPC += LowPC; |
| 334 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 335 | return (HighPC != -1ULL); |
| 336 | } |
| 337 | |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 338 | DWARFAddressRangesVector |
| 339 | DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 340 | if (isNULL()) |
Benjamin Kramer | ee26b62 | 2014-04-18 19:01:53 +0000 | [diff] [blame] | 341 | return DWARFAddressRangesVector(); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 342 | // Single range specified by low/high PC. |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 343 | uint64_t LowPC, HighPC; |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 344 | if (getLowAndHighPC(U, LowPC, HighPC)) { |
Benjamin Kramer | ee26b62 | 2014-04-18 19:01:53 +0000 | [diff] [blame] | 345 | return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC)); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 346 | } |
| 347 | // Multiple ranges from .debug_ranges section. |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 348 | uint32_t RangesOffset = |
| 349 | getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 350 | if (RangesOffset != -1U) { |
| 351 | DWARFDebugRangeList RangeList; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 352 | if (U->extractRangeList(RangesOffset, RangeList)) |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 353 | return RangeList.getAbsoluteRanges(U->getBaseAddress()); |
| 354 | } |
Benjamin Kramer | ee26b62 | 2014-04-18 19:01:53 +0000 | [diff] [blame] | 355 | return DWARFAddressRangesVector(); |
Alexey Samsonov | 762343d | 2014-04-18 17:25:46 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges( |
| 359 | const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const { |
| 360 | if (isNULL()) |
| 361 | return; |
| 362 | if (isSubprogramDIE()) { |
| 363 | const auto &DIERanges = getAddressRanges(U); |
| 364 | Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end()); |
| 365 | } |
| 366 | |
| 367 | const DWARFDebugInfoEntryMinimal *Child = getFirstChild(); |
| 368 | while (Child) { |
| 369 | Child->collectChildrenAddressRanges(U, Ranges); |
| 370 | Child = Child->getSibling(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress( |
| 375 | const DWARFUnit *U, const uint64_t Address) const { |
| 376 | for (const auto& R : getAddressRanges(U)) { |
| 377 | if (R.first <= Address && Address < R.second) |
| 378 | return true; |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 379 | } |
| 380 | return false; |
| 381 | } |
| 382 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 383 | const char * |
Alexey Samsonov | dce6734 | 2014-05-15 21:24:32 +0000 | [diff] [blame] | 384 | DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U, |
Frederic Riss | d4de180 | 2014-10-10 15:51:02 +0000 | [diff] [blame] | 385 | DINameKind Kind) const { |
| 386 | if (!isSubroutineDIE()) |
| 387 | return nullptr; |
| 388 | return getName(U, Kind); |
| 389 | } |
| 390 | |
| 391 | const char * |
| 392 | DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U, |
| 393 | DINameKind Kind) const { |
| 394 | if (Kind == DINameKind::None) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 395 | return nullptr; |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 396 | // Try to get mangled name only if it was asked for. |
Frederic Riss | d4de180 | 2014-10-10 15:51:02 +0000 | [diff] [blame] | 397 | if (Kind == DINameKind::LinkageName) { |
Alexey Samsonov | cd01472 | 2014-05-17 00:07:48 +0000 | [diff] [blame] | 398 | if (const char *name = |
| 399 | getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr)) |
| 400 | return name; |
| 401 | if (const char *name = |
| 402 | getAttributeValueAsString(U, DW_AT_linkage_name, nullptr)) |
| 403 | return name; |
| 404 | } |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 405 | if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr)) |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 406 | return name; |
| 407 | // Try to get name from specification DIE. |
| 408 | uint32_t spec_ref = |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 409 | getAttributeValueAsReference(U, DW_AT_specification, -1U); |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 410 | if (spec_ref != -1U) { |
| 411 | DWARFDebugInfoEntryMinimal spec_die; |
Frederic Riss | 58ed53c | 2014-09-22 12:35:53 +0000 | [diff] [blame] | 412 | if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) { |
Frederic Riss | d4de180 | 2014-10-10 15:51:02 +0000 | [diff] [blame] | 413 | if (const char *name = spec_die.getName(RefU, Kind)) |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 414 | return name; |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 417 | // Try to get name from abstract origin DIE. |
| 418 | uint32_t abs_origin_ref = |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 419 | getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U); |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 420 | if (abs_origin_ref != -1U) { |
| 421 | DWARFDebugInfoEntryMinimal abs_origin_die; |
Frederic Riss | 58ed53c | 2014-09-22 12:35:53 +0000 | [diff] [blame] | 422 | if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U, |
| 423 | &abs_origin_ref)) { |
Frederic Riss | d4de180 | 2014-10-10 15:51:02 +0000 | [diff] [blame] | 424 | if (const char *name = abs_origin_die.getName(RefU, Kind)) |
Alexey Samsonov | b604ff2 | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 425 | return name; |
| 426 | } |
| 427 | } |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 428 | return nullptr; |
Alexey Samsonov | f4462fa | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 429 | } |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 430 | |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 431 | void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U, |
Eric Christopher | 206cf64 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 432 | uint32_t &CallFile, |
| 433 | uint32_t &CallLine, |
| 434 | uint32_t &CallColumn) const { |
Alexey Samsonov | 48cbda5 | 2013-10-28 23:01:48 +0000 | [diff] [blame] | 435 | CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0); |
| 436 | CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0); |
| 437 | CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 440 | DWARFDebugInfoEntryInlinedChain |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 441 | DWARFDebugInfoEntryMinimal::getInlinedChainForAddress( |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 442 | const DWARFUnit *U, const uint64_t Address) const { |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 443 | DWARFDebugInfoEntryInlinedChain InlinedChain; |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 444 | InlinedChain.U = U; |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 445 | if (isNULL()) |
| 446 | return InlinedChain; |
| 447 | for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) { |
| 448 | // Append current DIE to inlined chain only if it has correct tag |
| 449 | // (e.g. it is not a lexical block). |
| 450 | if (DIE->isSubroutineDIE()) { |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 451 | InlinedChain.DIEs.push_back(*DIE); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 452 | } |
| 453 | // Try to get child which also contains provided address. |
| 454 | const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild(); |
| 455 | while (Child) { |
David Blaikie | 07e2244 | 2013-09-23 22:44:40 +0000 | [diff] [blame] | 456 | if (Child->addressRangeContainsAddress(U, Address)) { |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 457 | // Assume there is only one such child. |
| 458 | break; |
| 459 | } |
| 460 | Child = Child->getSibling(); |
| 461 | } |
| 462 | DIE = Child; |
| 463 | } |
| 464 | // Reverse the obtained chain to make the root of inlined chain last. |
Alexey Samsonov | 3211e61 | 2013-08-06 10:49:15 +0000 | [diff] [blame] | 465 | std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end()); |
Alexey Samsonov | c942e6b | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 466 | return InlinedChain; |
| 467 | } |