blob: 62d5e666aef922eaee97f31229e1de6576a3c745 [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) {
Adrian Prantl0c36a752015-01-06 16:50:25 +000048 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 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;
Frederic Rissb3c99122014-10-10 15:51:10 +000086 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 Risse939b432014-10-23 04:08:34 +000097static 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 Krameraa2f78f2011-09-13 19:42:23 +0000111void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
Frederic Riss955724e2014-09-22 12:36:04 +0000112 DWARFUnit *u,
David Blaikie07e22442013-09-23 22:44:40 +0000113 uint32_t *offset_ptr,
114 uint16_t attr, uint16_t form,
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000115 unsigned indent) const {
Frederic Risse939b432014-10-23 04:08:34 +0000116 const char BaseIndent[] = " ";
117 OS << BaseIndent;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000118 OS.indent(indent+2);
119 const char *attrString = AttributeString(attr);
120 if (attrString)
Adrian Prantl0c36a752015-01-06 16:50:25 +0000121 WithColor(OS, syntax::Attribute) << attrString;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000122 else
Adrian Prantl0c36a752015-01-06 16:50:25 +0000123 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
124
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000125 const char *formString = FormEncodingString(form);
126 if (formString)
127 OS << " [" << formString << ']';
128 else
129 OS << format(" [DW_FORM_Unknown_%x]", form);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000130
131 DWARFFormValue formValue(form);
132
David Blaikie07e22442013-09-23 22:44:40 +0000133 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000134 return;
135
136 OS << "\t(";
Frederic Riss878065b2014-09-04 19:39:20 +0000137
138 const char *Name = nullptr;
Frederic Riss955724e2014-09-22 12:36:04 +0000139 std::string File;
Adrian Prantl0c36a752015-01-06 16:50:25 +0000140 auto Color = syntax::Enumerator;
Frederic Riss955724e2014-09-22 12:36:04 +0000141 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
David Blaikiecdec7ee2015-11-17 00:41:02 +0000142 Color = syntax::String;
Frederic Riss955724e2014-09-22 12:36:04 +0000143 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 Riss878065b2014-09-04 19:39:20 +0000152 Name = AttributeValueString(attr, *Val);
153
Adrian Prantl0c36a752015-01-06 16:50:25 +0000154 if (Name)
155 WithColor(OS, Color) << Name;
156 else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
Frederic Riss0982e692014-09-05 07:21:50 +0000157 OS << *formValue.getAsUnsignedConstant();
Adrian Prantl0c36a752015-01-06 16:50:25 +0000158 else
Frederic Riss878065b2014-09-04 19:39:20 +0000159 formValue.dump(OS, u);
Frederic Riss878065b2014-09-04 19:39:20 +0000160
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000161 // 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.
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000164 if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) {
165 Optional<uint64_t> Ref = formValue.getAsReference(u);
166 if (Ref.hasValue()) {
167 uint32_t RefOffset = Ref.getValue();
168 DWARFDebugInfoEntryMinimal DIE;
169 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &RefOffset))
170 if (const char *Name = DIE.getName(RefU, DINameKind::LinkageName))
171 OS << " \"" << Name << '\"';
172 }
Frederic Rissb3c99122014-10-10 15:51:10 +0000173 } else if (attr == DW_AT_APPLE_property_attribute) {
174 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
175 dumpApplePropertyAttribute(OS, *OptVal);
Frederic Risse939b432014-10-23 04:08:34 +0000176 } else if (attr == DW_AT_ranges) {
177 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
178 sizeof(BaseIndent)+indent+4);
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000179 }
180
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000181 OS << ")\n";
182}
183
David Blaikie07e22442013-09-23 22:44:40 +0000184bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000185 uint32_t *OffsetPtr) {
186 Offset = *OffsetPtr;
David Blaikie07e22442013-09-23 22:44:40 +0000187 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
Alexey Samsonov330b8932013-10-28 23:58:58 +0000188 uint32_t UEndOffset = U->getNextUnitOffset();
189 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000190 return false;
191 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
192 if (0 == AbbrCode) {
193 // NULL debug tag entry.
Craig Topper2617dcc2014-04-15 06:32:26 +0000194 AbbrevDecl = nullptr;
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000195 return true;
196 }
David Blaikie07e22442013-09-23 22:44:40 +0000197 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Craig Topper2617dcc2014-04-15 06:32:26 +0000198 if (nullptr == AbbrevDecl) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000199 // Restore the original offset.
200 *OffsetPtr = Offset;
201 return false;
202 }
Alexey Samsonov330b8932013-10-28 23:58:58 +0000203 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
204 U->getAddressByteSize(), U->getVersion());
205 assert(FixedFormSizes.size() > 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000206
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000207 // Skip all data in the .debug_info for the attributes
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000208 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
209 uint16_t Form = AttrSpec.Form;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000210
Alexey Samsonov330b8932013-10-28 23:58:58 +0000211 uint8_t FixedFormSize =
212 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
213 if (FixedFormSize)
214 *OffsetPtr += FixedFormSize;
215 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000216 // Restore the original offset.
217 *OffsetPtr = Offset;
218 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000219 }
220 }
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000221 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000222}
223
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000224bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
225 return getTag() == DW_TAG_subprogram;
226}
227
228bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
229 uint32_t Tag = getTag();
230 return Tag == DW_TAG_subprogram ||
231 Tag == DW_TAG_inlined_subroutine;
232}
233
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000234bool DWARFDebugInfoEntryMinimal::getAttributeValue(
235 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
236 if (!AbbrevDecl)
237 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000238
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000239 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
240 if (AttrIdx == -1U)
241 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000242
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000243 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
244 uint32_t DebugInfoOffset = getOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000245
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000246 // Skip the abbreviation code so we are at the data for the attributes
247 DebugInfoData.getULEB128(&DebugInfoOffset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000248
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000249 // Skip preceding attribute values.
250 for (uint32_t i = 0; i < AttrIdx; ++i) {
251 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
252 DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000253 }
254
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000255 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
256 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000257}
258
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000259const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
David Blaikie07e22442013-09-23 22:44:40 +0000260 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000261 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000262 if (!getAttributeValue(U, Attr, FormValue))
263 return FailValue;
264 Optional<const char *> Result = FormValue.getAsCString(U);
265 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000266}
267
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000268uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
David Blaikie07e22442013-09-23 22:44:40 +0000269 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000270 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000271 if (!getAttributeValue(U, Attr, FormValue))
272 return FailValue;
273 Optional<uint64_t> Result = FormValue.getAsAddress(U);
274 return Result.hasValue() ? Result.getValue() : FailValue;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000275}
276
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000277uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
David Blaikie07e22442013-09-23 22:44:40 +0000278 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000279 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000280 if (!getAttributeValue(U, Attr, FormValue))
281 return FailValue;
282 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
283 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000284}
285
David Blaikie07e22442013-09-23 22:44:40 +0000286uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000287 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
288 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000289 if (!getAttributeValue(U, Attr, FormValue))
290 return FailValue;
291 Optional<uint64_t> Result = FormValue.getAsReference(U);
292 return Result.hasValue() ? Result.getValue() : FailValue;
293}
294
295uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
296 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
297 DWARFFormValue FormValue;
298 if (!getAttributeValue(U, Attr, FormValue))
299 return FailValue;
300 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
301 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000302}
Benjamin Kramer32664932011-09-14 20:52:27 +0000303
Alexey Samsonovaa909982014-06-13 22:31:03 +0000304uint64_t
305DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
306 uint64_t FailValue) const {
307 uint64_t Result =
308 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
309 if (Result != -1ULL)
310 return Result;
311 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
312}
313
David Blaikie07e22442013-09-23 22:44:40 +0000314bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000315 uint64_t &LowPC,
316 uint64_t &HighPC) const {
David Blaikie07e22442013-09-23 22:44:40 +0000317 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
Alexey Samsonov76142122013-10-28 23:15:15 +0000318 if (LowPC == -1ULL)
319 return false;
320 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
321 if (HighPC == -1ULL) {
322 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
323 // it represents function size.
324 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
325 if (HighPC != -1ULL)
326 HighPC += LowPC;
327 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000328 return (HighPC != -1ULL);
329}
330
Alexey Samsonov762343d2014-04-18 17:25:46 +0000331DWARFAddressRangesVector
332DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000333 if (isNULL())
Benjamin Krameree26b622014-04-18 19:01:53 +0000334 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000335 // Single range specified by low/high PC.
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000336 uint64_t LowPC, HighPC;
Alexey Samsonov762343d2014-04-18 17:25:46 +0000337 if (getLowAndHighPC(U, LowPC, HighPC)) {
Benjamin Krameree26b622014-04-18 19:01:53 +0000338 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
Alexey Samsonov762343d2014-04-18 17:25:46 +0000339 }
340 // Multiple ranges from .debug_ranges section.
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000341 uint32_t RangesOffset =
342 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000343 if (RangesOffset != -1U) {
344 DWARFDebugRangeList RangeList;
David Blaikie07e22442013-09-23 22:44:40 +0000345 if (U->extractRangeList(RangesOffset, RangeList))
Alexey Samsonov762343d2014-04-18 17:25:46 +0000346 return RangeList.getAbsoluteRanges(U->getBaseAddress());
347 }
Benjamin Krameree26b622014-04-18 19:01:53 +0000348 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000349}
350
351void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
352 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
353 if (isNULL())
354 return;
355 if (isSubprogramDIE()) {
356 const auto &DIERanges = getAddressRanges(U);
357 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
358 }
359
360 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
361 while (Child) {
362 Child->collectChildrenAddressRanges(U, Ranges);
363 Child = Child->getSibling();
364 }
365}
366
367bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
368 const DWARFUnit *U, const uint64_t Address) const {
369 for (const auto& R : getAddressRanges(U)) {
370 if (R.first <= Address && Address < R.second)
371 return true;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000372 }
373 return false;
374}
375
David Blaikie07e22442013-09-23 22:44:40 +0000376const char *
Alexey Samsonovdce67342014-05-15 21:24:32 +0000377DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
Frederic Rissd4de1802014-10-10 15:51:02 +0000378 DINameKind Kind) const {
379 if (!isSubroutineDIE())
380 return nullptr;
381 return getName(U, Kind);
382}
383
384const char *
385DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
386 DINameKind Kind) const {
387 if (Kind == DINameKind::None)
Craig Topper2617dcc2014-04-15 06:32:26 +0000388 return nullptr;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000389 // Try to get mangled name only if it was asked for.
Frederic Rissd4de1802014-10-10 15:51:02 +0000390 if (Kind == DINameKind::LinkageName) {
Alexey Samsonovcd014722014-05-17 00:07:48 +0000391 if (const char *name =
392 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
393 return name;
394 if (const char *name =
395 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
396 return name;
397 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000398 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000399 return name;
400 // Try to get name from specification DIE.
401 uint32_t spec_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000402 getAttributeValueAsReference(U, DW_AT_specification, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000403 if (spec_ref != -1U) {
404 DWARFDebugInfoEntryMinimal spec_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000405 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000406 if (const char *name = spec_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000407 return name;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000408 }
409 }
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000410 // Try to get name from abstract origin DIE.
411 uint32_t abs_origin_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000412 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000413 if (abs_origin_ref != -1U) {
414 DWARFDebugInfoEntryMinimal abs_origin_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000415 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
416 &abs_origin_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000417 if (const char *name = abs_origin_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000418 return name;
419 }
420 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000421 return nullptr;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000422}
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000423
David Blaikie07e22442013-09-23 22:44:40 +0000424void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000425 uint32_t &CallFile,
426 uint32_t &CallLine,
427 uint32_t &CallColumn) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000428 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
429 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
430 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000431}
432
Alexey Samsonov3211e612013-08-06 10:49:15 +0000433DWARFDebugInfoEntryInlinedChain
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000434DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
David Blaikie07e22442013-09-23 22:44:40 +0000435 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000436 DWARFDebugInfoEntryInlinedChain InlinedChain;
David Blaikie07e22442013-09-23 22:44:40 +0000437 InlinedChain.U = U;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000438 if (isNULL())
439 return InlinedChain;
440 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
441 // Append current DIE to inlined chain only if it has correct tag
442 // (e.g. it is not a lexical block).
443 if (DIE->isSubroutineDIE()) {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000444 InlinedChain.DIEs.push_back(*DIE);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000445 }
446 // Try to get child which also contains provided address.
447 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
448 while (Child) {
David Blaikie07e22442013-09-23 22:44:40 +0000449 if (Child->addressRangeContainsAddress(U, Address)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000450 // Assume there is only one such child.
451 break;
452 }
453 Child = Child->getSibling();
454 }
455 DIE = Child;
456 }
457 // Reverse the obtained chain to make the root of inlined chain last.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000458 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000459 return InlinedChain;
460}