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