blob: 55378e59b3cb6469a2f757de98c88f2d41e94ccf [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,
Greg Clayton6c273762016-10-27 16:32:04 +0000115 dwarf::Attribute attr,
116 dwarf::Form form,
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000117 unsigned indent) const {
Frederic Risse939b432014-10-23 04:08:34 +0000118 const char BaseIndent[] = " ";
119 OS << BaseIndent;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000120 OS.indent(indent+2);
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000121 auto attrString = AttributeString(attr);
122 if (!attrString.empty())
Adrian Prantl0c36a752015-01-06 16:50:25 +0000123 WithColor(OS, syntax::Attribute) << attrString;
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000124 else
Adrian Prantl0c36a752015-01-06 16:50:25 +0000125 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", attr);
126
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000127 auto formString = FormEncodingString(form);
128 if (!formString.empty())
Benjamin Kramer9bca64f2011-09-15 05:43:00 +0000129 OS << " [" << formString << ']';
130 else
131 OS << format(" [DW_FORM_Unknown_%x]", form);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000132
133 DWARFFormValue formValue(form);
134
David Blaikie07e22442013-09-23 22:44:40 +0000135 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000136 return;
137
138 OS << "\t(";
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000139
140 StringRef Name;
Frederic Riss955724e2014-09-22 12:36:04 +0000141 std::string File;
Adrian Prantl0c36a752015-01-06 16:50:25 +0000142 auto Color = syntax::Enumerator;
Frederic Riss955724e2014-09-22 12:36:04 +0000143 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
David Blaikiecdec7ee2015-11-17 00:41:02 +0000144 Color = syntax::String;
Frederic Riss955724e2014-09-22 12:36:04 +0000145 if (const auto *LT = u->getContext().getLineTableForUnit(u))
146 if (LT->getFileNameByIndex(
147 formValue.getAsUnsignedConstant().getValue(),
148 u->getCompilationDir(),
149 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
150 File = '"' + File + '"';
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000151 Name = File;
Frederic Riss955724e2014-09-22 12:36:04 +0000152 }
153 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
Frederic Riss878065b2014-09-04 19:39:20 +0000154 Name = AttributeValueString(attr, *Val);
155
Mehdi Amini149f6ea2016-10-05 05:59:29 +0000156 if (!Name.empty())
Adrian Prantl0c36a752015-01-06 16:50:25 +0000157 WithColor(OS, Color) << Name;
158 else if (attr == DW_AT_decl_line || attr == DW_AT_call_line)
Frederic Riss0982e692014-09-05 07:21:50 +0000159 OS << *formValue.getAsUnsignedConstant();
Adrian Prantl0c36a752015-01-06 16:50:25 +0000160 else
Greg Claytoncddab272016-10-31 16:46:02 +0000161 formValue.dump(OS);
Frederic Riss878065b2014-09-04 19:39:20 +0000162
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000163 // We have dumped the attribute raw value. For some attributes
164 // having both the raw value and the pretty-printed value is
165 // interesting. These attributes are handled below.
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000166 if (attr == DW_AT_specification || attr == DW_AT_abstract_origin) {
Greg Claytoncddab272016-10-31 16:46:02 +0000167 Optional<uint64_t> Ref = formValue.getAsReference();
Alexey Samsonovbf19a572015-05-19 20:29:28 +0000168 if (Ref.hasValue()) {
169 uint32_t RefOffset = Ref.getValue();
170 DWARFDebugInfoEntryMinimal DIE;
171 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &RefOffset))
172 if (const char *Name = DIE.getName(RefU, DINameKind::LinkageName))
173 OS << " \"" << Name << '\"';
174 }
Frederic Rissb3c99122014-10-10 15:51:10 +0000175 } else if (attr == DW_AT_APPLE_property_attribute) {
176 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
177 dumpApplePropertyAttribute(OS, *OptVal);
Frederic Risse939b432014-10-23 04:08:34 +0000178 } else if (attr == DW_AT_ranges) {
179 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
180 sizeof(BaseIndent)+indent+4);
Frederic Rissd1cfc3c2014-10-06 03:36:31 +0000181 }
182
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000183 OS << ")\n";
184}
185
David Blaikie07e22442013-09-23 22:44:40 +0000186bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000187 uint32_t *OffsetPtr) {
188 Offset = *OffsetPtr;
David Blaikie07e22442013-09-23 22:44:40 +0000189 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
Alexey Samsonov330b8932013-10-28 23:58:58 +0000190 uint32_t UEndOffset = U->getNextUnitOffset();
191 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000192 return false;
193 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
194 if (0 == AbbrCode) {
195 // NULL debug tag entry.
Craig Topper2617dcc2014-04-15 06:32:26 +0000196 AbbrevDecl = nullptr;
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000197 return true;
198 }
David Blaikie07e22442013-09-23 22:44:40 +0000199 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Craig Topper2617dcc2014-04-15 06:32:26 +0000200 if (nullptr == AbbrevDecl) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000201 // Restore the original offset.
202 *OffsetPtr = Offset;
203 return false;
204 }
Alexey Samsonov330b8932013-10-28 23:58:58 +0000205 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
206 U->getAddressByteSize(), U->getVersion());
207 assert(FixedFormSizes.size() > 0);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000208
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000209 // Skip all data in the .debug_info for the attributes
Alexey Samsonov1eabf982014-03-13 07:52:54 +0000210 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Greg Clayton6c273762016-10-27 16:32:04 +0000211 auto Form = AttrSpec.Form;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000212
Alexey Samsonov330b8932013-10-28 23:58:58 +0000213 uint8_t FixedFormSize =
214 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
215 if (FixedFormSize)
216 *OffsetPtr += FixedFormSize;
217 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000218 // Restore the original offset.
219 *OffsetPtr = Offset;
220 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000221 }
222 }
Alexey Samsonovc03f2ee2013-04-08 14:37:16 +0000223 return true;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000224}
225
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000226bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
227 return getTag() == DW_TAG_subprogram;
228}
229
230bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
231 uint32_t Tag = getTag();
232 return Tag == DW_TAG_subprogram ||
233 Tag == DW_TAG_inlined_subroutine;
234}
235
Greg Clayton6c273762016-10-27 16:32:04 +0000236bool DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFUnit *U,
237 dwarf::Attribute Attr, DWARFFormValue &FormValue) const {
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000238 if (!AbbrevDecl)
239 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000240
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000241 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
242 if (AttrIdx == -1U)
243 return false;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000244
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000245 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
246 uint32_t DebugInfoOffset = getOffset();
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000247
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000248 // Skip the abbreviation code so we are at the data for the attributes
249 DebugInfoData.getULEB128(&DebugInfoOffset);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000250
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000251 // Skip preceding attribute values.
252 for (uint32_t i = 0; i < AttrIdx; ++i) {
253 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
254 DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000255 }
256
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000257 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
258 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000259}
260
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000261const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
Greg Clayton6c273762016-10-27 16:32:04 +0000262 const DWARFUnit *U, dwarf::Attribute Attr,
263 const char *FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000264 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000265 if (!getAttributeValue(U, Attr, FormValue))
266 return FailValue;
Greg Claytoncddab272016-10-31 16:46:02 +0000267 Optional<const char *> Result = FormValue.getAsCString();
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000268 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000269}
270
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000271uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
Greg Clayton6c273762016-10-27 16:32:04 +0000272 const DWARFUnit *U, dwarf::Attribute Attr,
273 uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000274 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000275 if (!getAttributeValue(U, Attr, FormValue))
276 return FailValue;
Greg Claytoncddab272016-10-31 16:46:02 +0000277 Optional<uint64_t> Result = FormValue.getAsAddress();
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000278 return Result.hasValue() ? Result.getValue() : FailValue;
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000279}
280
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000281uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
Greg Clayton6c273762016-10-27 16:32:04 +0000282 const DWARFUnit *U, dwarf::Attribute Attr,
283 uint64_t FailValue) const {
Alexey Samsonove3ba81b2013-08-27 09:20:22 +0000284 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000285 if (!getAttributeValue(U, Attr, FormValue))
286 return FailValue;
287 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
288 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000289}
290
David Blaikie07e22442013-09-23 22:44:40 +0000291uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
Greg Clayton6c273762016-10-27 16:32:04 +0000292 const DWARFUnit *U, dwarf::Attribute Attr,
293 uint64_t FailValue) const {
Alexey Samsonovcaabb0e2013-10-17 13:28:16 +0000294 DWARFFormValue FormValue;
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000295 if (!getAttributeValue(U, Attr, FormValue))
296 return FailValue;
Greg Claytoncddab272016-10-31 16:46:02 +0000297 Optional<uint64_t> Result = FormValue.getAsReference();
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000298 return Result.hasValue() ? Result.getValue() : FailValue;
299}
300
301uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
Greg Clayton6c273762016-10-27 16:32:04 +0000302 const DWARFUnit *U, dwarf::Attribute Attr,
303 uint64_t FailValue) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000304 DWARFFormValue FormValue;
305 if (!getAttributeValue(U, Attr, FormValue))
306 return FailValue;
307 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
308 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Krameraa2f78f2011-09-13 19:42:23 +0000309}
Benjamin Kramer32664932011-09-14 20:52:27 +0000310
Alexey Samsonovaa909982014-06-13 22:31:03 +0000311uint64_t
312DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
313 uint64_t FailValue) const {
314 uint64_t Result =
Adrian Prantlc4fbbcf2016-10-28 17:59:50 +0000315 getAttributeValueAsSectionOffset(U, DW_AT_rnglists_base, -1ULL);
Alexey Samsonovaa909982014-06-13 22:31:03 +0000316 if (Result != -1ULL)
317 return Result;
318 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
319}
320
David Blaikie07e22442013-09-23 22:44:40 +0000321bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000322 uint64_t &LowPC,
323 uint64_t &HighPC) const {
David Blaikie07e22442013-09-23 22:44:40 +0000324 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
Alexey Samsonov76142122013-10-28 23:15:15 +0000325 if (LowPC == -1ULL)
326 return false;
327 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
328 if (HighPC == -1ULL) {
329 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
330 // it represents function size.
331 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
332 if (HighPC != -1ULL)
333 HighPC += LowPC;
334 }
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000335 return (HighPC != -1ULL);
336}
337
Alexey Samsonov762343d2014-04-18 17:25:46 +0000338DWARFAddressRangesVector
339DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000340 if (isNULL())
Benjamin Krameree26b622014-04-18 19:01:53 +0000341 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000342 // Single range specified by low/high PC.
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000343 uint64_t LowPC, HighPC;
Alexey Samsonov762343d2014-04-18 17:25:46 +0000344 if (getLowAndHighPC(U, LowPC, HighPC)) {
Benjamin Krameree26b622014-04-18 19:01:53 +0000345 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
Alexey Samsonov762343d2014-04-18 17:25:46 +0000346 }
347 // Multiple ranges from .debug_ranges section.
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000348 uint32_t RangesOffset =
349 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000350 if (RangesOffset != -1U) {
351 DWARFDebugRangeList RangeList;
David Blaikie07e22442013-09-23 22:44:40 +0000352 if (U->extractRangeList(RangesOffset, RangeList))
Alexey Samsonov762343d2014-04-18 17:25:46 +0000353 return RangeList.getAbsoluteRanges(U->getBaseAddress());
354 }
Benjamin Krameree26b622014-04-18 19:01:53 +0000355 return DWARFAddressRangesVector();
Alexey Samsonov762343d2014-04-18 17:25:46 +0000356}
357
358void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
359 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
360 if (isNULL())
361 return;
362 if (isSubprogramDIE()) {
363 const auto &DIERanges = getAddressRanges(U);
364 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
365 }
366
367 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
368 while (Child) {
369 Child->collectChildrenAddressRanges(U, Ranges);
370 Child = Child->getSibling();
371 }
372}
373
374bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
375 const DWARFUnit *U, const uint64_t Address) const {
376 for (const auto& R : getAddressRanges(U)) {
377 if (R.first <= Address && Address < R.second)
378 return true;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000379 }
380 return false;
381}
382
David Blaikie07e22442013-09-23 22:44:40 +0000383const char *
Alexey Samsonovdce67342014-05-15 21:24:32 +0000384DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
Frederic Rissd4de1802014-10-10 15:51:02 +0000385 DINameKind Kind) const {
386 if (!isSubroutineDIE())
387 return nullptr;
388 return getName(U, Kind);
389}
390
391const char *
392DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
393 DINameKind Kind) const {
394 if (Kind == DINameKind::None)
Craig Topper2617dcc2014-04-15 06:32:26 +0000395 return nullptr;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000396 // Try to get mangled name only if it was asked for.
Frederic Rissd4de1802014-10-10 15:51:02 +0000397 if (Kind == DINameKind::LinkageName) {
Alexey Samsonovcd014722014-05-17 00:07:48 +0000398 if (const char *name =
399 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
400 return name;
401 if (const char *name =
402 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
403 return name;
404 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000405 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000406 return name;
407 // Try to get name from specification DIE.
408 uint32_t spec_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000409 getAttributeValueAsReference(U, DW_AT_specification, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000410 if (spec_ref != -1U) {
411 DWARFDebugInfoEntryMinimal spec_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000412 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000413 if (const char *name = spec_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000414 return name;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000415 }
416 }
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000417 // Try to get name from abstract origin DIE.
418 uint32_t abs_origin_ref =
David Blaikie07e22442013-09-23 22:44:40 +0000419 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000420 if (abs_origin_ref != -1U) {
421 DWARFDebugInfoEntryMinimal abs_origin_die;
Frederic Riss58ed53c2014-09-22 12:35:53 +0000422 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
423 &abs_origin_ref)) {
Frederic Rissd4de1802014-10-10 15:51:02 +0000424 if (const char *name = abs_origin_die.getName(RefU, Kind))
Alexey Samsonovb604ff22012-07-17 15:28:35 +0000425 return name;
426 }
427 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000428 return nullptr;
Alexey Samsonovf4462fa2012-07-02 05:54:45 +0000429}
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000430
David Blaikie07e22442013-09-23 22:44:40 +0000431void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
Eric Christopher206cf642012-10-30 21:36:43 +0000432 uint32_t &CallFile,
433 uint32_t &CallLine,
434 uint32_t &CallColumn) const {
Alexey Samsonov48cbda52013-10-28 23:01:48 +0000435 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
436 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
437 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000438}
439
Alexey Samsonov3211e612013-08-06 10:49:15 +0000440DWARFDebugInfoEntryInlinedChain
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000441DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
David Blaikie07e22442013-09-23 22:44:40 +0000442 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000443 DWARFDebugInfoEntryInlinedChain InlinedChain;
David Blaikie07e22442013-09-23 22:44:40 +0000444 InlinedChain.U = U;
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000445 if (isNULL())
446 return InlinedChain;
447 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
448 // Append current DIE to inlined chain only if it has correct tag
449 // (e.g. it is not a lexical block).
450 if (DIE->isSubroutineDIE()) {
Alexey Samsonov3211e612013-08-06 10:49:15 +0000451 InlinedChain.DIEs.push_back(*DIE);
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000452 }
453 // Try to get child which also contains provided address.
454 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
455 while (Child) {
David Blaikie07e22442013-09-23 22:44:40 +0000456 if (Child->addressRangeContainsAddress(U, Address)) {
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000457 // Assume there is only one such child.
458 break;
459 }
460 Child = Child->getSibling();
461 }
462 DIE = Child;
463 }
464 // Reverse the obtained chain to make the root of inlined chain last.
Alexey Samsonov3211e612013-08-06 10:49:15 +0000465 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
Alexey Samsonovc942e6b2012-09-04 08:12:33 +0000466 return InlinedChain;
467}