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