blob: c43456b1c57b5780016ef843f61066173481967e [file] [log] [blame]
Eric Christopherc2ce0042012-08-23 00:52:51 +00001//===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
Benjamin Krameraa2f78f2011-09-13 19:42:23 +00002//
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 Prantl0c36a752015-01-06 16:50:25 +000010#include "SyntaxHighlighting.h"
Zachary Turner82af9432015-01-30 18:07:45 +000011#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 Rissb3c99122014-10-10 15:51:10 +000016#include "llvm/Support/DataTypes.h"
Eric Christopher2cbd5762013-01-07 19:32:41 +000017#include "llvm/Support/Debug.h"
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000018#include "llvm/Support/Dwarf.h"
19#include "llvm/Support/Format.h"
20#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
22using namespace dwarf;
Adrian Prantl0c36a752015-01-06 16:50:25 +000023using namespace syntax;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000024
Frederic Riss58ed53c2014-09-22 12:35:53 +000025// 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.
29static 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 Riss955724e2014-09-22 12:36:04 +000036void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u,
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000037 unsigned recurseDepth,
38 unsigned indent) const {
David Blaikie07e22442013-09-23 22:44:40 +000039 DataExtractor debug_info_data = u->getDebugInfoExtractor();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000040 uint32_t offset = Offset;
41
42 if (debug_info_data.isValidOffset(offset)) {
Benjamin Kramerf7e0a312011-11-05 15:35:00 +000043 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl0c36a752015-01-06 16:50:25 +000044 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000045
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000046 if (abbrCode) {
47 if (AbbrevDecl) {
Mehdi Amini149f6ea2016-10-05 05:59:29 +000048 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 Prantl0c36a752015-01-06 16:50:25 +000054
Benjamin Kramer9bca64f2011-09-15 05:43:00 +000055 OS << format(" [%u] %c\n", abbrCode,
56 AbbrevDecl->hasChildren() ? '*' : ' ');
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000057
Eric Christopher7f5870c2013-01-07 03:27:58 +000058 // Dump all data in the DIE for the attributes.
Alexey Samsonov1eabf982014-03-13 07:52:54 +000059 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
60 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000061 }
62
63 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
64 if (recurseDepth > 0 && child) {
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000065 while (child) {
David Blaikie07e22442013-09-23 22:44:40 +000066 child->dump(OS, u, recurseDepth-1, indent+2);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000067 child = child->getSibling();
68 }
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000069 }
70 } else {
71 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
72 << abbrCode << '\n';
73 }
74 } else {
Benjamin Kramerf915acc2011-09-14 17:54:56 +000075 OS.indent(indent) << "NULL\n";
Benjamin Krameraa2f78f2011-09-13 19:42:23 +000076 }
77 }
78}
79
Frederic Rissb3c99122014-10-10 15:51:10 +000080static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
81 OS << " (";
82 do {
Michael Ilsemanaddddc42014-12-15 18:48:43 +000083 uint64_t Shift = countTrailingZeros(Val);
84 assert(Shift < 64 && "undefined behavior");
85 uint64_t Bit = 1ULL << Shift;
Mehdi Amini149f6ea2016-10-05 05:59:29 +000086 auto PropName = ApplePropertyString(Bit);
87 if (!PropName.empty())
Frederic Rissb3c99122014-10-10 15:51:10 +000088 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 Risse939b432014-10-23 04:08:34 +000098static 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 Krameraa2f78f2011-09-13 19:42:23 +0000112void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
Frederic Riss955724e2014-09-22 12:36:04 +0000113 DWARFUnit *u,
David Blaikie07e22442013-09-23 22:44:40 +0000114 uint32_t *offset_ptr,
115 uint16_t attr, uint16_t form,
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000116 unsigned indent) const {
Frederic Risse939b432014-10-23 04:08:34 +0000117 const char BaseIndent[] = " ";
118 OS << BaseIndent;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000119 OS.indent(indent+2);
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000120 auto attrString = AttributeString(attr);
121 if (!attrString.empty())
Adrian Prantl0c36a752015-01-06 16:50:25 +0000122 WithColor(OS, syntax::Attribute) << attrString;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000123 else
Adrian Prantl0c36a752015-01-06 16:50:25 +0000124 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
125
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000126 auto formString = FormEncodingString(form);
127 if (!formString.empty())
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000128 OS << " [" << formString << ']';
129 else
130 OS << format(" [DW_FORM_Unknown_%x]", form);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000131
132 DWARFFormValue formValue(form);
133
David Blaikie07e22442013-09-23 22:44:40 +0000134 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000135 return;
136
137 OS << "\t(";
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000138
139 StringRef Name;
Frederic Riss955724e2014-09-22 12:36:04 +0000140 std::string File;
Adrian Prantl0c36a752015-01-06 16:50:25 +0000141 auto Color = syntax::Enumerator;
Frederic Riss955724e2014-09-22 12:36:04 +0000142 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
David Blaikiecdec7ee2015-11-17 00:41:02 +0000143 Color = syntax::String;
Frederic Riss955724e2014-09-22 12:36:04 +0000144 if (const auto *LT = u->getContext().getLineTableForUnit(u))
145 if (LT->getFileNameByIndex(
146 formValue.getAsUnsignedConstant().getValue(),
147 u->getCompilationDir(),
148 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
149 File = '"' + File + '"';
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000150 Name = File;
Frederic Riss955724e2014-09-22 12:36:04 +0000151 }
152 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
Frederic Riss878065b2014-09-04 19:39:20 +0000153 Name = AttributeValueString(attr, *Val);
154
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000155 if (!Name.empty())
Adrian Prantl0c36a752015-01-06 16:50:25 +0000156 WithColor(OS, Color) << Name;
157 else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
Frederic Riss0982e692014-09-05 07:21:50 +0000158 OS << *formValue.getAsUnsignedConstant();
Adrian Prantl0c36a752015-01-06 16:50:25 +0000159 else
Frederic Riss878065b2014-09-04 19:39:20 +0000160 formValue.dump(OS, u);
Frederic Riss878065b2014-09-04 19:39:20 +0000161
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000162 // We have dumped the attribute raw value. For some attributes
163 // having both the raw value and the pretty-printed value is
164 // interesting. These attributes are handled below.
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000165 if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) {
166 Optional<uint64_t> Ref = formValue.getAsReference(u);
167 if (Ref.hasValue()) {
168 uint32_t RefOffset = Ref.getValue();
169 DWARFDebugInfoEntryMinimal DIE;
170 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &RefOffset))
171 if (const char *Name = DIE.getName(RefU, DINameKind::LinkageName))
172 OS << " \"" << Name << '\"';
173 }
Frederic Rissb3c99122014-10-10 15:51:10 +0000174 } else if (attr == DW_AT_APPLE_property_attribute) {
175 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
176 dumpApplePropertyAttribute(OS, *OptVal);
Frederic Risse939b432014-10-23 04:08:34 +0000177 } else if (attr == DW_AT_ranges) {
178 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
179 sizeof(BaseIndent)+indent+4);
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000180 }
181
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000182 OS << ")\n";
183}
184
David Blaikie07e22442013-09-23 22:44:40 +0000185bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000186 uint32_t *OffsetPtr) {
187 Offset = *OffsetPtr;
David Blaikie07e22442013-09-23 22:44:40 +0000188 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
Alexey Samsonov330b8932013-10-28 23:58:58 +0000189 uint32_t UEndOffset = U->getNextUnitOffset();
190 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000191 return false;
192 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
193 if (0 == AbbrCode) {
194 // NULL debug tag entry.
Craig Topper2617dcc2014-04-15 06:32:26 +0000195 AbbrevDecl = nullptr;
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000196 return true;
197 }
David Blaikie07e22442013-09-23 22:44:40 +0000198 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Craig Topper2617dcc2014-04-15 06:32:26 +0000199 if (nullptr == AbbrevDecl) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000200 // Restore the original offset.
201 *OffsetPtr = Offset;
202 return false;
203 }
Alexey Samsonov330b8932013-10-28 23:58:58 +0000204 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
205 U->getAddressByteSize(), U->getVersion());
206 assert(FixedFormSizes.size() > 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000207
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000208 // Skip all data in the .debug_info for the attributes
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000209 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
210 uint16_t Form = AttrSpec.Form;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000211
Alexey Samsonov330b8932013-10-28 23:58:58 +0000212 uint8_t FixedFormSize =
213 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
214 if (FixedFormSize)
215 *OffsetPtr += FixedFormSize;
216 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000217 // Restore the original offset.
218 *OffsetPtr = Offset;
219 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000220 }
221 }
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000222 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000223}
224
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000225bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
226 return getTag() == DW_TAG_subprogram;
227}
228
229bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
230 uint32_t Tag = getTag();
231 return Tag == DW_TAG_subprogram ||
232 Tag == DW_TAG_inlined_subroutine;
233}
234
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000235bool DWARFDebugInfoEntryMinimal::getAttributeValue(
236 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
237 if (!AbbrevDecl)
238 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000239
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000240 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
241 if (AttrIdx == -1U)
242 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000243
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000244 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
245 uint32_t DebugInfoOffset = getOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000246
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000247 // Skip the abbreviation code so we are at the data for the attributes
248 DebugInfoData.getULEB128(&DebugInfoOffset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000249
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000250 // Skip preceding attribute values.
251 for (uint32_t i = 0; i < AttrIdx; ++i) {
252 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
253 DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000254 }
255
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000256 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
257 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000258}
259
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000260const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
David Blaikie07e22442013-09-23 22:44:40 +0000261 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000262 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000263 if (!getAttributeValue(U, Attr, FormValue))
264 return FailValue;
265 Optional<const char *> Result = FormValue.getAsCString(U);
266 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000267}
268
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000269uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
David Blaikie07e22442013-09-23 22:44:40 +0000270 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000271 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000272 if (!getAttributeValue(U, Attr, FormValue))
273 return FailValue;
274 Optional<uint64_t> Result = FormValue.getAsAddress(U);
275 return Result.hasValue() ? Result.getValue() : FailValue;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000276}
277
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000278uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
David Blaikie07e22442013-09-23 22:44:40 +0000279 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000280 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000281 if (!getAttributeValue(U, Attr, FormValue))
282 return FailValue;
283 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
284 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000285}
286
David Blaikie07e22442013-09-23 22:44:40 +0000287uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000288 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
289 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000290 if (!getAttributeValue(U, Attr, FormValue))
291 return FailValue;
292 Optional<uint64_t> Result = FormValue.getAsReference(U);
293 return Result.hasValue() ? Result.getValue() : FailValue;
294}
295
296uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
297 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
298 DWARFFormValue FormValue;
299 if (!getAttributeValue(U, Attr, FormValue))
300 return FailValue;
301 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
302 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000303}
Benjamin Kramer32664932011-09-14 20:52:27 +0000304
Alexey Samsonovaa909982014-06-13 22:31:03 +0000305uint64_t
306DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
307 uint64_t FailValue) const {
308 uint64_t Result =
309 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
310 if (Result != -1ULL)
311 return Result;
312 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
313}
314
David Blaikie07e22442013-09-23 22:44:40 +0000315bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000316 uint64_t &LowPC,
317 uint64_t &HighPC) const {
David Blaikie07e22442013-09-23 22:44:40 +0000318 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
Alexey Samsonov76142122013-10-28 23:15:15 +0000319 if (LowPC == -1ULL)
320 return false;
321 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
322 if (HighPC == -1ULL) {
323 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
324 // it represents function size.
325 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
326 if (HighPC != -1ULL)
327 HighPC += LowPC;
328 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000329 return (HighPC != -1ULL);
330}
331
Alexey Samsonov762343d2014-04-18 17:25:46 +0000332DWARFAddressRangesVector
333DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000334 if (isNULL())
Benjamin Krameree26b622014-04-18 19:01:53 +0000335 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000336 // Single range specified by low/high PC.
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000337 uint64_t LowPC, HighPC;
Alexey Samsonov762343d2014-04-18 17:25:46 +0000338 if (getLowAndHighPC(U, LowPC, HighPC)) {
Benjamin Krameree26b622014-04-18 19:01:53 +0000339 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
Alexey Samsonov762343d2014-04-18 17:25:46 +0000340 }
341 // Multiple ranges from .debug_ranges section.
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000342 uint32_t RangesOffset =
343 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000344 if (RangesOffset != -1U) {
345 DWARFDebugRangeList RangeList;
David Blaikie07e22442013-09-23 22:44:40 +0000346 if (U->extractRangeList(RangesOffset, RangeList))
Alexey Samsonov762343d2014-04-18 17:25:46 +0000347 return RangeList.getAbsoluteRanges(U->getBaseAddress());
348 }
Benjamin Krameree26b622014-04-18 19:01:53 +0000349 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000350}
351
352void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
353 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
354 if (isNULL())
355 return;
356 if (isSubprogramDIE()) {
357 const auto &DIERanges = getAddressRanges(U);
358 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
359 }
360
361 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
362 while (Child) {
363 Child->collectChildrenAddressRanges(U, Ranges);
364 Child = Child->getSibling();
365 }
366}
367
368bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
369 const DWARFUnit *U, const uint64_t Address) const {
370 for (const auto& R : getAddressRanges(U)) {
371 if (R.first <= Address && Address < R.second)
372 return true;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000373 }
374 return false;
375}
376
David Blaikie07e22442013-09-23 22:44:40 +0000377const char *
Alexey Samsonovdce67342014-05-15 21:24:32 +0000378DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
Frederic Rissd4de1802014-10-10 15:51:02 +0000379 DINameKind Kind) const {
380 if (!isSubroutineDIE())
381 return nullptr;
382 return getName(U, Kind);
383}
384
385const char *
386DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
387 DINameKind Kind) const {
388 if (Kind == DINameKind::None)
Craig Topper2617dcc2014-04-15 06:32:26 +0000389 return nullptr;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000390 // Try to get mangled name only if it was asked for.
Frederic Rissd4de1802014-10-10 15:51:02 +0000391 if (Kind == DINameKind::LinkageName) {
Alexey Samsonovcd014722014-05-17 00:07:48 +0000392 if (const char *name =
393 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
394 return name;
395 if (const char *name =
396 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
397 return name;
398 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000399 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000400 return name;
401 // Try to get name from specification DIE.
402 uint32_t spec_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000403 getAttributeValueAsReference(U, DW_AT_specification, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000404 if (spec_ref != -1U) {
405 DWARFDebugInfoEntryMinimal spec_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000406 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000407 if (const char *name = spec_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000408 return name;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000409 }
410 }
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000411 // Try to get name from abstract origin DIE.
412 uint32_t abs_origin_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000413 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000414 if (abs_origin_ref != -1U) {
415 DWARFDebugInfoEntryMinimal abs_origin_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000416 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
417 &abs_origin_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000418 if (const char *name = abs_origin_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000419 return name;
420 }
421 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000422 return nullptr;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000423}
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000424
David Blaikie07e22442013-09-23 22:44:40 +0000425void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000426 uint32_t &CallFile,
427 uint32_t &CallLine,
428 uint32_t &CallColumn) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000429 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
430 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
431 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000432}
433
Alexey Samsonov3211e612013-08-06 10:49:15 +0000434DWARFDebugInfoEntryInlinedChain
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000435DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
David Blaikie07e22442013-09-23 22:44:40 +0000436 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000437 DWARFDebugInfoEntryInlinedChain InlinedChain;
David Blaikie07e22442013-09-23 22:44:40 +0000438 InlinedChain.U = U;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000439 if (isNULL())
440 return InlinedChain;
441 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
442 // Append current DIE to inlined chain only if it has correct tag
443 // (e.g. it is not a lexical block).
444 if (DIE->isSubroutineDIE()) {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000445 InlinedChain.DIEs.push_back(*DIE);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000446 }
447 // Try to get child which also contains provided address.
448 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
449 while (Child) {
David Blaikie07e22442013-09-23 22:44:40 +0000450 if (Child->addressRangeContainsAddress(U, Address)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000451 // Assume there is only one such child.
452 break;
453 }
454 Child = Child->getSibling();
455 }
456 DIE = Child;
457 }
458 // Reverse the obtained chain to make the root of inlined chain last.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000459 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000460 return InlinedChain;
461}