blob: bde25ec82d11b48c25831d27404f31a353bfb198 [file] [log] [blame]
Eric Christopherbd5bc212012-08-23 00:52:51 +00001//===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
Benjamin Kramer72c0d7f2011-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
10#include "DWARFDebugInfoEntry.h"
11#include "DWARFCompileUnit.h"
12#include "DWARFContext.h"
13#include "DWARFDebugAbbrev.h"
Alexey Samsonovcd614552013-04-17 08:29:02 +000014#include "llvm/DebugInfo/DWARFFormValue.h"
Eric Christopherdd8e9f32013-01-07 19:32:41 +000015#include "llvm/Support/Debug.h"
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000016#include "llvm/Support/Dwarf.h"
17#include "llvm/Support/Format.h"
18#include "llvm/Support/raw_ostream.h"
19using namespace llvm;
20using namespace dwarf;
21
David Blaikiecd7c4982013-09-23 22:44:40 +000022void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000023 unsigned recurseDepth,
24 unsigned indent) const {
David Blaikiecd7c4982013-09-23 22:44:40 +000025 DataExtractor debug_info_data = u->getDebugInfoExtractor();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000026 uint32_t offset = Offset;
27
28 if (debug_info_data.isValidOffset(offset)) {
Benjamin Kramer80cc2592011-11-05 15:35:00 +000029 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000030
Benjamin Kramer09422552011-09-14 17:54:56 +000031 OS << format("\n0x%8.8x: ", Offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000032 if (abbrCode) {
33 if (AbbrevDecl) {
Benjamin Kramer75c63082011-09-15 05:43:00 +000034 const char *tagString = TagString(getTag());
35 if (tagString)
36 OS.indent(indent) << tagString;
37 else
38 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39 OS << format(" [%u] %c\n", abbrCode,
40 AbbrevDecl->hasChildren() ? '*' : ' ');
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000041
Eric Christophere5ef3052013-01-07 03:27:58 +000042 // Dump all data in the DIE for the attributes.
Stephen Hines36b56882014-04-23 16:57:46 -070043 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
44 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000045 }
46
47 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
48 if (recurseDepth > 0 && child) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000049 while (child) {
David Blaikiecd7c4982013-09-23 22:44:40 +000050 child->dump(OS, u, recurseDepth-1, indent+2);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000051 child = child->getSibling();
52 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000053 }
54 } else {
55 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
56 << abbrCode << '\n';
57 }
58 } else {
Benjamin Kramer09422552011-09-14 17:54:56 +000059 OS.indent(indent) << "NULL\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000060 }
61 }
62}
63
64void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
David Blaikiecd7c4982013-09-23 22:44:40 +000065 const DWARFUnit *u,
66 uint32_t *offset_ptr,
67 uint16_t attr, uint16_t form,
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000068 unsigned indent) const {
David Blaikiea9b69792013-08-19 03:36:23 +000069 OS << " ";
Benjamin Kramer75c63082011-09-15 05:43:00 +000070 OS.indent(indent+2);
71 const char *attrString = AttributeString(attr);
72 if (attrString)
73 OS << attrString;
74 else
75 OS << format("DW_AT_Unknown_%x", attr);
76 const char *formString = FormEncodingString(form);
77 if (formString)
78 OS << " [" << formString << ']';
79 else
80 OS << format(" [DW_FORM_Unknown_%x]", form);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000081
82 DWARFFormValue formValue(form);
83
David Blaikiecd7c4982013-09-23 22:44:40 +000084 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000085 return;
86
87 OS << "\t(";
David Blaikiecd7c4982013-09-23 22:44:40 +000088 formValue.dump(OS, u);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000089 OS << ")\n";
90}
91
David Blaikiecd7c4982013-09-23 22:44:40 +000092bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +000093 uint32_t *OffsetPtr) {
94 Offset = *OffsetPtr;
David Blaikiecd7c4982013-09-23 22:44:40 +000095 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
Alexey Samsonov39f62fa2013-10-28 23:58:58 +000096 uint32_t UEndOffset = U->getNextUnitOffset();
97 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +000098 return false;
99 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
100 if (0 == AbbrCode) {
101 // NULL debug tag entry.
102 AbbrevDecl = NULL;
103 return true;
104 }
David Blaikiecd7c4982013-09-23 22:44:40 +0000105 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000106 if (0 == AbbrevDecl) {
107 // Restore the original offset.
108 *OffsetPtr = Offset;
109 return false;
110 }
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000111 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
112 U->getAddressByteSize(), U->getVersion());
113 assert(FixedFormSizes.size() > 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000114
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000115 // Skip all data in the .debug_info for the attributes
Stephen Hines36b56882014-04-23 16:57:46 -0700116 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
117 uint16_t Form = AttrSpec.Form;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000118
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000119 uint8_t FixedFormSize =
120 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
121 if (FixedFormSize)
122 *OffsetPtr += FixedFormSize;
123 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000124 // Restore the original offset.
125 *OffsetPtr = Offset;
126 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000127 }
128 }
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000129 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000130}
131
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000132bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
133 return getTag() == DW_TAG_subprogram;
134}
135
136bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
137 uint32_t Tag = getTag();
138 return Tag == DW_TAG_subprogram ||
139 Tag == DW_TAG_inlined_subroutine;
140}
141
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000142bool DWARFDebugInfoEntryMinimal::getAttributeValue(
143 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
144 if (!AbbrevDecl)
145 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000146
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000147 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
148 if (AttrIdx == -1U)
149 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000150
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000151 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
152 uint32_t DebugInfoOffset = getOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000153
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000154 // Skip the abbreviation code so we are at the data for the attributes
155 DebugInfoData.getULEB128(&DebugInfoOffset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000156
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000157 // Skip preceding attribute values.
158 for (uint32_t i = 0; i < AttrIdx; ++i) {
159 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
160 DebugInfoData, &DebugInfoOffset, U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000161 }
162
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000163 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
164 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000165}
166
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000167const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
David Blaikiecd7c4982013-09-23 22:44:40 +0000168 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000169 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000170 if (!getAttributeValue(U, Attr, FormValue))
171 return FailValue;
172 Optional<const char *> Result = FormValue.getAsCString(U);
173 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000174}
175
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000176uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
David Blaikiecd7c4982013-09-23 22:44:40 +0000177 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000178 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000179 if (!getAttributeValue(U, Attr, FormValue))
180 return FailValue;
181 Optional<uint64_t> Result = FormValue.getAsAddress(U);
182 return Result.hasValue() ? Result.getValue() : FailValue;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000183}
184
Alexey Samsonovc5253232013-10-28 23:01:48 +0000185uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
David Blaikiecd7c4982013-09-23 22:44:40 +0000186 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000187 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000188 if (!getAttributeValue(U, Attr, FormValue))
189 return FailValue;
190 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
191 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000192}
193
David Blaikiecd7c4982013-09-23 22:44:40 +0000194uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000195 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
196 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000197 if (!getAttributeValue(U, Attr, FormValue))
198 return FailValue;
199 Optional<uint64_t> Result = FormValue.getAsReference(U);
200 return Result.hasValue() ? Result.getValue() : FailValue;
201}
202
203uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
204 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
205 DWARFFormValue FormValue;
206 if (!getAttributeValue(U, Attr, FormValue))
207 return FailValue;
208 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
209 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000210}
Benjamin Kramer10df8062011-09-14 20:52:27 +0000211
David Blaikiecd7c4982013-09-23 22:44:40 +0000212bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
Eric Christopher203e6f62012-10-30 21:36:43 +0000213 uint64_t &LowPC,
214 uint64_t &HighPC) const {
David Blaikiecd7c4982013-09-23 22:44:40 +0000215 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
Alexey Samsonovd2d54e22013-10-28 23:15:15 +0000216 if (LowPC == -1ULL)
217 return false;
218 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
219 if (HighPC == -1ULL) {
220 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
221 // it represents function size.
222 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
223 if (HighPC != -1ULL)
224 HighPC += LowPC;
225 }
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000226 return (HighPC != -1ULL);
227}
228
David Blaikiecd7c4982013-09-23 22:44:40 +0000229void DWARFDebugInfoEntryMinimal::buildAddressRangeTable(
230 const DWARFUnit *U, DWARFDebugAranges *DebugAranges,
231 uint32_t UOffsetInAranges) const {
Benjamin Kramer10df8062011-09-14 20:52:27 +0000232 if (AbbrevDecl) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000233 if (isSubprogramDIE()) {
234 uint64_t LowPC, HighPC;
David Blaikiecd7c4982013-09-23 22:44:40 +0000235 if (getLowAndHighPC(U, LowPC, HighPC))
236 DebugAranges->appendRange(UOffsetInAranges, LowPC, HighPC);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000237 // FIXME: try to append ranges from .debug_ranges section.
Benjamin Kramer10df8062011-09-14 20:52:27 +0000238 }
239
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000240 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
241 while (Child) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000242 Child->buildAddressRangeTable(U, DebugAranges, UOffsetInAranges);
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000243 Child = Child->getSibling();
Benjamin Kramer10df8062011-09-14 20:52:27 +0000244 }
245 }
246}
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000247
David Blaikiecd7c4982013-09-23 22:44:40 +0000248bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
249 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000250 if (isNULL())
251 return false;
252 uint64_t LowPC, HighPC;
David Blaikiecd7c4982013-09-23 22:44:40 +0000253 if (getLowAndHighPC(U, LowPC, HighPC))
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000254 return (LowPC <= Address && Address <= HighPC);
255 // Try to get address ranges from .debug_ranges section.
Alexey Samsonovc5253232013-10-28 23:01:48 +0000256 uint32_t RangesOffset =
257 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000258 if (RangesOffset != -1U) {
259 DWARFDebugRangeList RangeList;
David Blaikiecd7c4982013-09-23 22:44:40 +0000260 if (U->extractRangeList(RangesOffset, RangeList))
261 return RangeList.containsAddress(U->getBaseAddress(), Address);
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000262 }
263 return false;
264}
265
David Blaikiecd7c4982013-09-23 22:44:40 +0000266const char *
267DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000268 if (!isSubroutineDIE())
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000269 return 0;
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000270 // Try to get mangled name if possible.
271 if (const char *name =
David Blaikiecd7c4982013-09-23 22:44:40 +0000272 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, 0))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000273 return name;
David Blaikiecd7c4982013-09-23 22:44:40 +0000274 if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name, 0))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000275 return name;
David Blaikiecd7c4982013-09-23 22:44:40 +0000276 if (const char *name = getAttributeValueAsString(U, DW_AT_name, 0))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000277 return name;
278 // Try to get name from specification DIE.
279 uint32_t spec_ref =
David Blaikiecd7c4982013-09-23 22:44:40 +0000280 getAttributeValueAsReference(U, DW_AT_specification, -1U);
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000281 if (spec_ref != -1U) {
282 DWARFDebugInfoEntryMinimal spec_die;
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000283 if (spec_die.extractFast(U, &spec_ref)) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000284 if (const char *name = spec_die.getSubroutineName(U))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000285 return name;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000286 }
287 }
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000288 // Try to get name from abstract origin DIE.
289 uint32_t abs_origin_ref =
David Blaikiecd7c4982013-09-23 22:44:40 +0000290 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000291 if (abs_origin_ref != -1U) {
292 DWARFDebugInfoEntryMinimal abs_origin_die;
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000293 if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000294 if (const char *name = abs_origin_die.getSubroutineName(U))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000295 return name;
296 }
297 }
298 return 0;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000299}
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000300
David Blaikiecd7c4982013-09-23 22:44:40 +0000301void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
Eric Christopher203e6f62012-10-30 21:36:43 +0000302 uint32_t &CallFile,
303 uint32_t &CallLine,
304 uint32_t &CallColumn) const {
Alexey Samsonovc5253232013-10-28 23:01:48 +0000305 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
306 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
307 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000308}
309
Alexey Samsonove6642902013-08-06 10:49:15 +0000310DWARFDebugInfoEntryInlinedChain
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000311DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
David Blaikiecd7c4982013-09-23 22:44:40 +0000312 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonove6642902013-08-06 10:49:15 +0000313 DWARFDebugInfoEntryInlinedChain InlinedChain;
David Blaikiecd7c4982013-09-23 22:44:40 +0000314 InlinedChain.U = U;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000315 if (isNULL())
316 return InlinedChain;
317 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
318 // Append current DIE to inlined chain only if it has correct tag
319 // (e.g. it is not a lexical block).
320 if (DIE->isSubroutineDIE()) {
Alexey Samsonove6642902013-08-06 10:49:15 +0000321 InlinedChain.DIEs.push_back(*DIE);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000322 }
323 // Try to get child which also contains provided address.
324 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
325 while (Child) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000326 if (Child->addressRangeContainsAddress(U, Address)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000327 // Assume there is only one such child.
328 break;
329 }
330 Child = Child->getSibling();
331 }
332 DIE = Child;
333 }
334 // Reverse the obtained chain to make the root of inlined chain last.
Alexey Samsonove6642902013-08-06 10:49:15 +0000335 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000336 return InlinedChain;
337}