blob: 7ae38e6e05355d4452f49fe344e597e3a3d34e76 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDie.cpp -------------------------------------------------------===//
Greg Claytonc8c10322016-12-13 18:25:19 +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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000011#include "llvm/ADT/None.h"
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000015#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000016#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000017#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Reid Klecknera0587362017-08-29 21:41:21 +000018#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000021#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000022#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000023#include "llvm/Support/Format.h"
Pavel Labath9025f952018-03-21 11:46:37 +000024#include "llvm/Support/FormatVariadic.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000025#include "llvm/Support/MathExtras.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000026#include "llvm/Support/WithColor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000027#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000028#include <algorithm>
29#include <cassert>
30#include <cinttypes>
31#include <cstdint>
32#include <string>
33#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000034
35using namespace llvm;
36using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000037using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000038
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000039static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000040 OS << " (";
41 do {
42 uint64_t Shift = countTrailingZeros(Val);
43 assert(Shift < 64 && "undefined behavior");
44 uint64_t Bit = 1ULL << Shift;
45 auto PropName = ApplePropertyString(Bit);
46 if (!PropName.empty())
47 OS << PropName;
48 else
49 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
50 if (!(Val ^= Bit))
51 break;
52 OS << ", ";
53 } while (true);
54 OS << ")";
55}
56
George Rimar6957ab52017-08-15 12:32:54 +000057static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
58 const DWARFAddressRangesVector &Ranges,
59 unsigned AddressSize, unsigned Indent,
60 const DIDumpOptions &DumpOpts) {
George Rimare1c30f72017-08-15 15:54:43 +000061 ArrayRef<SectionName> SectionNames;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000062 if (DumpOpts.Verbose)
George Rimare1c30f72017-08-15 15:54:43 +000063 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000064
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000065 for (const DWARFAddressRange &R : Ranges) {
George Rimar6957ab52017-08-15 12:32:54 +000066
Greg Claytonc8c10322016-12-13 18:25:19 +000067 OS << '\n';
68 OS.indent(Indent);
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000069 R.dump(OS, AddressSize);
George Rimar6957ab52017-08-15 12:32:54 +000070
71 if (SectionNames.empty() || R.SectionIndex == -1ULL)
72 continue;
73
George Rimare1c30f72017-08-15 15:54:43 +000074 StringRef Name = SectionNames[R.SectionIndex].Name;
75 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000076
George Rimare1c30f72017-08-15 15:54:43 +000077 // Print section index if name is not unique.
78 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000079 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000080 }
81}
82
Reid Klecknera0587362017-08-29 21:41:21 +000083static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000084 DWARFUnit *U, unsigned Indent,
85 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000086 DWARFContext &Ctx = U->getContext();
87 const DWARFObject &Obj = Ctx.getDWARFObj();
88 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
89 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
90 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
91 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
92 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
93 Ctx.isLittleEndian(), 0);
94 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
95 .print(OS, MRI);
96 return;
97 }
98
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000099 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +0000100 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
101 const DWARFSection &LocSection = Obj.getLocSection();
102 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
103 uint32_t Offset = *FormValue.getAsSectionOffset();
104
105 if (!LocSection.Data.empty()) {
106 DWARFDebugLoc DebugLoc;
107 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
108 Obj.getAddressSize());
109 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
110 if (LL)
111 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
112 else
113 OS << "error extracting location list.";
114 } else if (!LocDWOSection.Data.empty()) {
115 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
116 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
117 if (LL)
118 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
119 else
120 OS << "error extracting location list.";
121 }
122 }
123}
124
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000125/// Dump the name encoded in the type tag.
126static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
127 StringRef TagStr = TagString(T);
128 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
129 return;
130 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
131}
132
133/// Recursively dump the DIE type name when applicable.
134static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
135 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
136
137 if (!D.isValid())
138 return;
139
140 if (const char *Name = D.getName(DINameKind::LinkageName)) {
141 OS << Name;
142 return;
143 }
144
145 // FIXME: We should have pretty printers per language. Currently we print
146 // everything as if it was C++ and fall back to the TAG type name.
147 const dwarf::Tag T = D.getTag();
148 switch (T) {
149 case DW_TAG_array_type:
150 case DW_TAG_pointer_type:
151 case DW_TAG_ptr_to_member_type:
152 case DW_TAG_reference_type:
153 case DW_TAG_rvalue_reference_type:
154 break;
155 default:
156 dumpTypeTagName(OS, T);
157 }
158
159 // Follow the DW_AT_type if possible.
160 dumpTypeName(OS, D);
161
162 switch (T) {
163 case DW_TAG_array_type:
164 OS << "[]";
165 break;
166 case DW_TAG_pointer_type:
167 OS << '*';
168 break;
169 case DW_TAG_ptr_to_member_type:
170 OS << '*';
171 break;
172 case DW_TAG_reference_type:
173 OS << '&';
174 break;
175 case DW_TAG_rvalue_reference_type:
176 OS << "&&";
177 break;
178 default:
179 break;
180 }
181}
182
Greg Claytonc8c10322016-12-13 18:25:19 +0000183static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
184 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000185 dwarf::Form Form, unsigned Indent,
186 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000187 if (!Die.isValid())
188 return;
189 const char BaseIndent[] = " ";
190 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000191 OS.indent(Indent + 2);
Pavel Labath9025f952018-03-21 11:46:37 +0000192 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000193
Pavel Labath9025f952018-03-21 11:46:37 +0000194 if (DumpOpts.Verbose || DumpOpts.ShowForm)
195 OS << formatv(" [{0}]", Form);
Adrian Prantl318d1192017-06-06 23:28:45 +0000196
Greg Claytonc8c10322016-12-13 18:25:19 +0000197 DWARFUnit *U = Die.getDwarfUnit();
198 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000199
Paul Robinsone5400f82017-11-07 19:57:12 +0000200 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
201 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000202 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000203
Greg Claytonc8c10322016-12-13 18:25:19 +0000204 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000205
Greg Claytonc8c10322016-12-13 18:25:19 +0000206 StringRef Name;
207 std::string File;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000208 auto Color = HighlightColor::Enumerator;
Greg Claytonc8c10322016-12-13 18:25:19 +0000209 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000210 Color = HighlightColor::String;
Greg Claytonc8c10322016-12-13 18:25:19 +0000211 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000212 if (LT->getFileNameByIndex(
213 formValue.getAsUnsignedConstant().getValue(),
214 U->getCompilationDir(),
215 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000216 File = '"' + File + '"';
217 Name = File;
218 }
219 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
220 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000221
Greg Claytonc8c10322016-12-13 18:25:19 +0000222 if (!Name.empty())
223 WithColor(OS, Color) << Name;
224 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
225 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000226 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
227 formValue.getAsUnsignedConstant()) {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000228 if (DumpOpts.ShowAddresses) {
229 // Print the actual address rather than the offset.
230 uint64_t LowPC, HighPC, Index;
231 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
232 OS << format("0x%016" PRIx64, HighPC);
233 else
234 formValue.dump(OS, DumpOpts);
235 }
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000236 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
237 Attr == DW_AT_data_member_location ||
238 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000239 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000240 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000241 formValue.dump(OS, DumpOpts);
242
Greg Claytonc8c10322016-12-13 18:25:19 +0000243 // We have dumped the attribute raw value. For some attributes
244 // having both the raw value and the pretty-printed value is
245 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000246 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000247 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
248 DINameKind::LinkageName))
249 OS << " \"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000250 } else if (Attr == DW_AT_type) {
251 OS << " \"";
252 dumpTypeName(OS, Die);
253 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000254 } else if (Attr == DW_AT_APPLE_property_attribute) {
255 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
256 dumpApplePropertyAttribute(OS, *OptVal);
257 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000258 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
259 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
260 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000261 }
George Rimar6957ab52017-08-15 12:32:54 +0000262
Greg Claytonc8c10322016-12-13 18:25:19 +0000263 OS << ")\n";
264}
265
Adrian Prantlb4a67902017-10-04 22:26:19 +0000266bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000267
268bool DWARFDie::isSubroutineDIE() const {
269 auto Tag = getTag();
270 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
271}
272
Adrian Prantlb4a67902017-10-04 22:26:19 +0000273Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000274 if (!isValid())
275 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000276 auto AbbrevDecl = getAbbreviationDeclarationPtr();
277 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000278 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
279 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000280}
281
Greg Clayton97d22182017-01-13 21:08:18 +0000282Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000283DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
284 if (!isValid())
285 return None;
286 auto AbbrevDecl = getAbbreviationDeclarationPtr();
287 if (AbbrevDecl) {
288 for (auto Attr : Attrs) {
289 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
290 return Value;
291 }
292 }
293 return None;
294}
295
296Optional<DWARFFormValue>
297DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
298 if (!isValid())
299 return None;
Greg Claytond6b67eb2017-11-27 22:12:44 +0000300 if (auto Value = find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000301 return Value;
Greg Claytond6b67eb2017-11-27 22:12:44 +0000302 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) {
303 if (auto Value = Die.findRecursively(Attrs))
304 return Value;
305 }
306 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_specification)) {
307 if (auto Value = Die.findRecursively(Attrs))
308 return Value;
309 }
Greg Claytonc109bbe2017-01-13 22:32:12 +0000310 return None;
311}
312
Greg Claytonc8c10322016-12-13 18:25:19 +0000313DWARFDie
314DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000315 if (auto SpecRef = toReference(find(Attr))) {
316 if (auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000317 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000318 }
319 return DWARFDie();
320}
321
Adrian Prantlb4a67902017-10-04 22:26:19 +0000322Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000323 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000324}
325
Greg Clayton2520c9e2016-12-19 20:36:41 +0000326Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000327 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000328 if (auto Address = FormValue->getAsAddress()) {
329 // High PC is an address.
330 return Address;
331 }
332 if (auto Offset = FormValue->getAsUnsignedConstant()) {
333 // High PC is an offset from LowPC.
334 return LowPC + *Offset;
335 }
336 }
337 return None;
338}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000339
George Rimara25d3292017-05-27 18:10:23 +0000340bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
341 uint64_t &SectionIndex) const {
342 auto F = find(DW_AT_low_pc);
343 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000344 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000345 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000346 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
347 LowPC = *LowPcAddr;
348 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000349 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000350 return true;
351 }
352 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000353}
354
Adrian Prantlb4a67902017-10-04 22:26:19 +0000355DWARFAddressRangesVector DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000356 if (isNULL())
357 return DWARFAddressRangesVector();
358 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000359 uint64_t LowPC, HighPC, Index;
360 if (getLowAndHighPC(LowPC, HighPC, Index))
361 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000362
Greg Claytonc8c10322016-12-13 18:25:19 +0000363 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000364 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000365 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000366 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000367 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000368 return RangeList.getAbsoluteRanges(U->getBaseAddress());
369 }
370 return DWARFAddressRangesVector();
371}
372
Adrian Prantlb4a67902017-10-04 22:26:19 +0000373void DWARFDie::collectChildrenAddressRanges(
374 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000375 if (isNULL())
376 return;
377 if (isSubprogramDIE()) {
378 const auto &DIERanges = getAddressRanges();
379 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
380 }
381
Adrian Prantlb4a67902017-10-04 22:26:19 +0000382 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000383 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000384}
385
386bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000387 for (const auto &R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000388 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000389 return true;
390 }
391 return false;
392}
393
Adrian Prantlb4a67902017-10-04 22:26:19 +0000394const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000395 if (!isSubroutineDIE())
396 return nullptr;
397 return getName(Kind);
398}
399
Adrian Prantlb4a67902017-10-04 22:26:19 +0000400const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000401 if (!isValid() || Kind == DINameKind::None)
402 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000403 // Try to get mangled name only if it was asked for.
404 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000405 if (auto Name = dwarf::toString(
406 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
407 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000408 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000409 }
Greg Clayton97d22182017-01-13 21:08:18 +0000410 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
411 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000412 return nullptr;
413}
414
David Blaikieefc4eba2017-02-06 20:19:02 +0000415uint64_t DWARFDie::getDeclLine() const {
416 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
417}
418
Greg Claytonc8c10322016-12-13 18:25:19 +0000419void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000420 uint32_t &CallColumn,
421 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000422 CallFile = toUnsigned(find(DW_AT_call_file), 0);
423 CallLine = toUnsigned(find(DW_AT_call_line), 0);
424 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000425 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000426}
427
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000428/// Helper to dump a DIE with all of its parents, but no siblings.
429static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
430 DIDumpOptions DumpOpts) {
431 if (!Die)
432 return Indent;
433 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000434 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000435 return Indent + 2;
436}
437
Adrian Prantld3f9f212017-09-20 17:44:00 +0000438void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000439 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000440 if (!isValid())
441 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000442 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000443 const uint32_t Offset = getOffset();
444 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000445 if (DumpOpts.ShowParents) {
446 DumpOpts.ShowParents = false;
447 Indent = dumpParentChain(getParent(), OS, Indent, DumpOpts);
448 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000449
Greg Claytonc8c10322016-12-13 18:25:19 +0000450 if (debug_info_data.isValidOffset(offset)) {
451 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000452 if (DumpOpts.ShowAddresses)
Jonas Devlieghere69217532018-03-09 09:56:24 +0000453 WithColor(OS, HighlightColor::Address).get()
454 << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000455
Greg Claytonc8c10322016-12-13 18:25:19 +0000456 if (abbrCode) {
457 auto AbbrevDecl = getAbbreviationDeclarationPtr();
458 if (AbbrevDecl) {
Pavel Labath9025f952018-03-21 11:46:37 +0000459 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
460 << formatv("{0}", getTag());
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000461 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000462 OS << format(" [%u] %c", abbrCode,
463 AbbrevDecl->hasChildren() ? '*' : ' ');
464 OS << '\n';
465
Greg Claytonc8c10322016-12-13 18:25:19 +0000466 // Dump all data in the DIE for the attributes.
467 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000468 if (AttrSpec.Form == DW_FORM_implicit_const) {
469 // We are dumping .debug_info section ,
470 // implicit_const attribute values are not really stored here,
471 // but in .debug_abbrev section. So we just skip such attrs.
472 continue;
473 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000474 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000475 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000476 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000477
Greg Claytonc8c10322016-12-13 18:25:19 +0000478 DWARFDie child = getFirstChild();
Adrian Prantl5da51f42017-11-29 01:12:22 +0000479 if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
Adrian Prantld3f9f212017-09-20 17:44:00 +0000480 DumpOpts.RecurseDepth--;
Greg Claytonc8c10322016-12-13 18:25:19 +0000481 while (child) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000482 child.dump(OS, Indent + 2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000483 child = child.getSibling();
484 }
485 }
486 } else {
487 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000488 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000489 }
490 } else {
491 OS.indent(Indent) << "NULL\n";
492 }
493 }
494}
495
Adrian Prantl3d523a62017-08-16 17:43:01 +0000496LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
497
Greg Clayton78a07bf2016-12-21 21:37:06 +0000498DWARFDie DWARFDie::getParent() const {
499 if (isValid())
500 return U->getParent(Die);
501 return DWARFDie();
502}
503
504DWARFDie DWARFDie::getSibling() const {
505 if (isValid())
506 return U->getSibling(Die);
507 return DWARFDie();
508}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000509
George Rimar0be860f2017-10-25 10:23:49 +0000510DWARFDie DWARFDie::getFirstChild() const {
511 if (isValid())
512 return U->getFirstChild(Die);
513 return DWARFDie();
514}
515
Adrian Prantlb4a67902017-10-04 22:26:19 +0000516iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000517 return make_range(attribute_iterator(*this, false),
518 attribute_iterator(*this, true));
519}
520
Adrian Prantlb4a67902017-10-04 22:26:19 +0000521DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
522 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000523 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
524 assert(AbbrDecl && "Must have abbreviation declaration");
525 if (End) {
526 // This is the end iterator so we set the index to the attribute count.
527 Index = AbbrDecl->getNumAttributes();
528 } else {
529 // This is the begin iterator so we extract the value for this->Index.
530 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
531 updateForIndex(*AbbrDecl, 0);
532 }
533}
534
535void DWARFDie::attribute_iterator::updateForIndex(
536 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
537 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000538 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000539 auto NumAttrs = AbbrDecl.getNumAttributes();
540 if (Index < NumAttrs) {
541 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
542 // Add the previous byte size of any previous attribute value.
543 AttrValue.Offset += AttrValue.ByteSize;
544 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
545 uint32_t ParseOffset = AttrValue.Offset;
546 auto U = Die.getDwarfUnit();
547 assert(U && "Die must have valid DWARF unit");
548 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000549 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000550 (void)b;
551 assert(b && "extractValue cannot fail on fully parsed DWARF");
552 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
553 } else {
554 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
555 AttrValue.clear();
556 }
557}
558
559DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
560 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
561 updateForIndex(*AbbrDecl, Index + 1);
562 return *this;
563}