blob: b811ed70644fa9d9711e36196da409143941c4b2 [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;
Stephen Hinesdce4a402014-05-29 02:49:00 -070021typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000022
David Blaikiecd7c4982013-09-23 22:44:40 +000023void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000024 unsigned recurseDepth,
25 unsigned indent) const {
David Blaikiecd7c4982013-09-23 22:44:40 +000026 DataExtractor debug_info_data = u->getDebugInfoExtractor();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000027 uint32_t offset = Offset;
28
29 if (debug_info_data.isValidOffset(offset)) {
Benjamin Kramer80cc2592011-11-05 15:35:00 +000030 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000031
Benjamin Kramer09422552011-09-14 17:54:56 +000032 OS << format("\n0x%8.8x: ", Offset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000033 if (abbrCode) {
34 if (AbbrevDecl) {
Benjamin Kramer75c63082011-09-15 05:43:00 +000035 const char *tagString = TagString(getTag());
36 if (tagString)
37 OS.indent(indent) << tagString;
38 else
39 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
40 OS << format(" [%u] %c\n", abbrCode,
41 AbbrevDecl->hasChildren() ? '*' : ' ');
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000042
Eric Christophere5ef3052013-01-07 03:27:58 +000043 // Dump all data in the DIE for the attributes.
Stephen Hines36b56882014-04-23 16:57:46 -070044 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
45 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000046 }
47
48 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
49 if (recurseDepth > 0 && child) {
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000050 while (child) {
David Blaikiecd7c4982013-09-23 22:44:40 +000051 child->dump(OS, u, recurseDepth-1, indent+2);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000052 child = child->getSibling();
53 }
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000054 }
55 } else {
56 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
57 << abbrCode << '\n';
58 }
59 } else {
Benjamin Kramer09422552011-09-14 17:54:56 +000060 OS.indent(indent) << "NULL\n";
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000061 }
62 }
63}
64
65void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
David Blaikiecd7c4982013-09-23 22:44:40 +000066 const DWARFUnit *u,
67 uint32_t *offset_ptr,
68 uint16_t attr, uint16_t form,
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000069 unsigned indent) const {
David Blaikiea9b69792013-08-19 03:36:23 +000070 OS << " ";
Benjamin Kramer75c63082011-09-15 05:43:00 +000071 OS.indent(indent+2);
72 const char *attrString = AttributeString(attr);
73 if (attrString)
74 OS << attrString;
75 else
76 OS << format("DW_AT_Unknown_%x", attr);
77 const char *formString = FormEncodingString(form);
78 if (formString)
79 OS << " [" << formString << ']';
80 else
81 OS << format(" [DW_FORM_Unknown_%x]", form);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000082
83 DWARFFormValue formValue(form);
84
David Blaikiecd7c4982013-09-23 22:44:40 +000085 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000086 return;
87
88 OS << "\t(";
David Blaikiecd7c4982013-09-23 22:44:40 +000089 formValue.dump(OS, u);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +000090 OS << ")\n";
91}
92
David Blaikiecd7c4982013-09-23 22:44:40 +000093bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +000094 uint32_t *OffsetPtr) {
95 Offset = *OffsetPtr;
David Blaikiecd7c4982013-09-23 22:44:40 +000096 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
Alexey Samsonov39f62fa2013-10-28 23:58:58 +000097 uint32_t UEndOffset = U->getNextUnitOffset();
98 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +000099 return false;
100 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
101 if (0 == AbbrCode) {
102 // NULL debug tag entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700103 AbbrevDecl = nullptr;
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000104 return true;
105 }
David Blaikiecd7c4982013-09-23 22:44:40 +0000106 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700107 if (nullptr == AbbrevDecl) {
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000108 // Restore the original offset.
109 *OffsetPtr = Offset;
110 return false;
111 }
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000112 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
113 U->getAddressByteSize(), U->getVersion());
114 assert(FixedFormSizes.size() > 0);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000115
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000116 // Skip all data in the .debug_info for the attributes
Stephen Hines36b56882014-04-23 16:57:46 -0700117 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
118 uint16_t Form = AttrSpec.Form;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000119
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000120 uint8_t FixedFormSize =
121 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
122 if (FixedFormSize)
123 *OffsetPtr += FixedFormSize;
124 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000125 // Restore the original offset.
126 *OffsetPtr = Offset;
127 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000128 }
129 }
Alexey Samsonovd6b89ef2013-04-08 14:37:16 +0000130 return true;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000131}
132
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000133bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
134 return getTag() == DW_TAG_subprogram;
135}
136
137bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
138 uint32_t Tag = getTag();
139 return Tag == DW_TAG_subprogram ||
140 Tag == DW_TAG_inlined_subroutine;
141}
142
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000143bool DWARFDebugInfoEntryMinimal::getAttributeValue(
144 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
145 if (!AbbrevDecl)
146 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000147
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000148 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
149 if (AttrIdx == -1U)
150 return false;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000151
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000152 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
153 uint32_t DebugInfoOffset = getOffset();
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000154
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000155 // Skip the abbreviation code so we are at the data for the attributes
156 DebugInfoData.getULEB128(&DebugInfoOffset);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000157
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000158 // Skip preceding attribute values.
159 for (uint32_t i = 0; i < AttrIdx; ++i) {
160 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
161 DebugInfoData, &DebugInfoOffset, U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000162 }
163
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000164 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
165 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000166}
167
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000168const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
David Blaikiecd7c4982013-09-23 22:44:40 +0000169 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000170 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000171 if (!getAttributeValue(U, Attr, FormValue))
172 return FailValue;
173 Optional<const char *> Result = FormValue.getAsCString(U);
174 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000175}
176
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000177uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
David Blaikiecd7c4982013-09-23 22:44:40 +0000178 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000179 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000180 if (!getAttributeValue(U, Attr, FormValue))
181 return FailValue;
182 Optional<uint64_t> Result = FormValue.getAsAddress(U);
183 return Result.hasValue() ? Result.getValue() : FailValue;
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000184}
185
Alexey Samsonovc5253232013-10-28 23:01:48 +0000186uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
David Blaikiecd7c4982013-09-23 22:44:40 +0000187 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
Alexey Samsonov63fd2af2013-08-27 09:20:22 +0000188 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000189 if (!getAttributeValue(U, Attr, FormValue))
190 return FailValue;
191 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
192 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000193}
194
David Blaikiecd7c4982013-09-23 22:44:40 +0000195uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
Alexey Samsonov2e56d572013-10-17 13:28:16 +0000196 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
197 DWARFFormValue FormValue;
Alexey Samsonovc5253232013-10-28 23:01:48 +0000198 if (!getAttributeValue(U, Attr, FormValue))
199 return FailValue;
200 Optional<uint64_t> Result = FormValue.getAsReference(U);
201 return Result.hasValue() ? Result.getValue() : FailValue;
202}
203
204uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
205 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
206 DWARFFormValue FormValue;
207 if (!getAttributeValue(U, Attr, FormValue))
208 return FailValue;
209 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
210 return Result.hasValue() ? Result.getValue() : FailValue;
Benjamin Kramer72c0d7f2011-09-13 19:42:23 +0000211}
Benjamin Kramer10df8062011-09-14 20:52:27 +0000212
David Blaikiecd7c4982013-09-23 22:44:40 +0000213bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
Eric Christopher203e6f62012-10-30 21:36:43 +0000214 uint64_t &LowPC,
215 uint64_t &HighPC) const {
David Blaikiecd7c4982013-09-23 22:44:40 +0000216 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
Alexey Samsonovd2d54e22013-10-28 23:15:15 +0000217 if (LowPC == -1ULL)
218 return false;
219 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
220 if (HighPC == -1ULL) {
221 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
222 // it represents function size.
223 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
224 if (HighPC != -1ULL)
225 HighPC += LowPC;
226 }
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000227 return (HighPC != -1ULL);
228}
229
Stephen Hinesdce4a402014-05-29 02:49:00 -0700230DWARFAddressRangesVector
231DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000232 if (isNULL())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700233 return DWARFAddressRangesVector();
234 // Single range specified by low/high PC.
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000235 uint64_t LowPC, HighPC;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700236 if (getLowAndHighPC(U, LowPC, HighPC)) {
237 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
238 }
239 // Multiple ranges from .debug_ranges section.
Alexey Samsonovc5253232013-10-28 23:01:48 +0000240 uint32_t RangesOffset =
241 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000242 if (RangesOffset != -1U) {
243 DWARFDebugRangeList RangeList;
David Blaikiecd7c4982013-09-23 22:44:40 +0000244 if (U->extractRangeList(RangesOffset, RangeList))
Stephen Hinesdce4a402014-05-29 02:49:00 -0700245 return RangeList.getAbsoluteRanges(U->getBaseAddress());
246 }
247 return DWARFAddressRangesVector();
248}
249
250void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
251 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
252 if (isNULL())
253 return;
254 if (isSubprogramDIE()) {
255 const auto &DIERanges = getAddressRanges(U);
256 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
257 }
258
259 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
260 while (Child) {
261 Child->collectChildrenAddressRanges(U, Ranges);
262 Child = Child->getSibling();
263 }
264}
265
266bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
267 const DWARFUnit *U, const uint64_t Address) const {
268 for (const auto& R : getAddressRanges(U)) {
269 if (R.first <= Address && Address < R.second)
270 return true;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000271 }
272 return false;
273}
274
David Blaikiecd7c4982013-09-23 22:44:40 +0000275const char *
Stephen Hinesdce4a402014-05-29 02:49:00 -0700276DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
277 FunctionNameKind Kind) const {
278 if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
279 return nullptr;
280 // Try to get mangled name only if it was asked for.
281 if (Kind == FunctionNameKind::LinkageName) {
282 if (const char *name =
283 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
284 return name;
285 if (const char *name =
286 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
287 return name;
288 }
289 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000290 return name;
291 // Try to get name from specification DIE.
292 uint32_t spec_ref =
David Blaikiecd7c4982013-09-23 22:44:40 +0000293 getAttributeValueAsReference(U, DW_AT_specification, -1U);
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000294 if (spec_ref != -1U) {
295 DWARFDebugInfoEntryMinimal spec_die;
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000296 if (spec_die.extractFast(U, &spec_ref)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700297 if (const char *name = spec_die.getSubroutineName(U, Kind))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000298 return name;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000299 }
300 }
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000301 // Try to get name from abstract origin DIE.
302 uint32_t abs_origin_ref =
David Blaikiecd7c4982013-09-23 22:44:40 +0000303 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000304 if (abs_origin_ref != -1U) {
305 DWARFDebugInfoEntryMinimal abs_origin_die;
Alexey Samsonov39f62fa2013-10-28 23:58:58 +0000306 if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700307 if (const char *name = abs_origin_die.getSubroutineName(U, Kind))
Alexey Samsonov9d26b0b2012-07-17 15:28:35 +0000308 return name;
309 }
310 }
Stephen Hinesdce4a402014-05-29 02:49:00 -0700311 return nullptr;
Alexey Samsonov3e25c4a2012-07-02 05:54:45 +0000312}
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000313
David Blaikiecd7c4982013-09-23 22:44:40 +0000314void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
Eric Christopher203e6f62012-10-30 21:36:43 +0000315 uint32_t &CallFile,
316 uint32_t &CallLine,
317 uint32_t &CallColumn) const {
Alexey Samsonovc5253232013-10-28 23:01:48 +0000318 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
319 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
320 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000321}
322
Alexey Samsonove6642902013-08-06 10:49:15 +0000323DWARFDebugInfoEntryInlinedChain
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000324DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
David Blaikiecd7c4982013-09-23 22:44:40 +0000325 const DWARFUnit *U, const uint64_t Address) const {
Alexey Samsonove6642902013-08-06 10:49:15 +0000326 DWARFDebugInfoEntryInlinedChain InlinedChain;
David Blaikiecd7c4982013-09-23 22:44:40 +0000327 InlinedChain.U = U;
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000328 if (isNULL())
329 return InlinedChain;
330 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
331 // Append current DIE to inlined chain only if it has correct tag
332 // (e.g. it is not a lexical block).
333 if (DIE->isSubroutineDIE()) {
Alexey Samsonove6642902013-08-06 10:49:15 +0000334 InlinedChain.DIEs.push_back(*DIE);
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000335 }
336 // Try to get child which also contains provided address.
337 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
338 while (Child) {
David Blaikiecd7c4982013-09-23 22:44:40 +0000339 if (Child->addressRangeContainsAddress(U, Address)) {
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000340 // Assume there is only one such child.
341 break;
342 }
343 Child = Child->getSibling();
344 }
345 DIE = Child;
346 }
347 // Reverse the obtained chain to make the root of inlined chain last.
Alexey Samsonove6642902013-08-06 10:49:15 +0000348 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
Alexey Samsonov5eae90d2012-09-04 08:12:33 +0000349 return InlinedChain;
350}