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