blob: 6dd5ecb34e4511fd2f763f1198be0427cf042294 [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"
Greg Claytonc8c10322016-12-13 18:25:19 +000011#include "SyntaxHighlighting.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000017#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000018#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Reid Klecknera0587362017-08-29 21:41:21 +000019#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000021#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000022#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000024#include "llvm/Support/Format.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000025#include "llvm/Support/MathExtras.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000026#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000027#include <algorithm>
28#include <cassert>
29#include <cinttypes>
30#include <cstdint>
31#include <string>
32#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000033
34using namespace llvm;
35using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000036using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000037using namespace syntax;
38
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
65 for (size_t I = 0; I < Ranges.size(); ++I) {
66 const DWARFAddressRange &R = Ranges[I];
67
Greg Claytonc8c10322016-12-13 18:25:19 +000068 OS << '\n';
69 OS.indent(Indent);
George Rimar6957ab52017-08-15 12:32:54 +000070 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", AddressSize * 2,
71 R.LowPC, AddressSize * 2, R.HighPC);
72
73 if (SectionNames.empty() || R.SectionIndex == -1ULL)
74 continue;
75
George Rimare1c30f72017-08-15 15:54:43 +000076 StringRef Name = SectionNames[R.SectionIndex].Name;
77 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000078
George Rimare1c30f72017-08-15 15:54:43 +000079 // Print section index if name is not unique.
80 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000081 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000082 }
83}
84
Reid Klecknera0587362017-08-29 21:41:21 +000085static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000086 DWARFUnit *U, unsigned Indent,
87 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000088 DWARFContext &Ctx = U->getContext();
89 const DWARFObject &Obj = Ctx.getDWARFObj();
90 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
91 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
92 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
93 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
94 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
95 Ctx.isLittleEndian(), 0);
96 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
97 .print(OS, MRI);
98 return;
99 }
100
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000101 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +0000102 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
103 const DWARFSection &LocSection = Obj.getLocSection();
104 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
105 uint32_t Offset = *FormValue.getAsSectionOffset();
106
107 if (!LocSection.Data.empty()) {
108 DWARFDebugLoc DebugLoc;
109 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
110 Obj.getAddressSize());
111 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
112 if (LL)
113 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
114 else
115 OS << "error extracting location list.";
116 } else if (!LocDWOSection.Data.empty()) {
117 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
118 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
119 if (LL)
120 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
121 else
122 OS << "error extracting location list.";
123 }
124 }
125}
126
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000127/// Dump the name encoded in the type tag.
128static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
129 StringRef TagStr = TagString(T);
130 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
131 return;
132 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
133}
134
135/// Recursively dump the DIE type name when applicable.
136static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
137 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
138
139 if (!D.isValid())
140 return;
141
142 if (const char *Name = D.getName(DINameKind::LinkageName)) {
143 OS << Name;
144 return;
145 }
146
147 // FIXME: We should have pretty printers per language. Currently we print
148 // everything as if it was C++ and fall back to the TAG type name.
149 const dwarf::Tag T = D.getTag();
150 switch (T) {
151 case DW_TAG_array_type:
152 case DW_TAG_pointer_type:
153 case DW_TAG_ptr_to_member_type:
154 case DW_TAG_reference_type:
155 case DW_TAG_rvalue_reference_type:
156 break;
157 default:
158 dumpTypeTagName(OS, T);
159 }
160
161 // Follow the DW_AT_type if possible.
162 dumpTypeName(OS, D);
163
164 switch (T) {
165 case DW_TAG_array_type:
166 OS << "[]";
167 break;
168 case DW_TAG_pointer_type:
169 OS << '*';
170 break;
171 case DW_TAG_ptr_to_member_type:
172 OS << '*';
173 break;
174 case DW_TAG_reference_type:
175 OS << '&';
176 break;
177 case DW_TAG_rvalue_reference_type:
178 OS << "&&";
179 break;
180 default:
181 break;
182 }
183}
184
Greg Claytonc8c10322016-12-13 18:25:19 +0000185static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
186 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000187 dwarf::Form Form, unsigned Indent,
188 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000189 if (!Die.isValid())
190 return;
191 const char BaseIndent[] = " ";
192 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000193 OS.indent(Indent + 2);
Greg Claytonc8c10322016-12-13 18:25:19 +0000194 auto attrString = AttributeString(Attr);
195 if (!attrString.empty())
196 WithColor(OS, syntax::Attribute) << attrString;
197 else
198 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000199
Jonas Devliegheref91dc282017-10-02 16:02:04 +0000200 if (DumpOpts.Verbose || DumpOpts.ShowForm) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000201 auto formString = FormEncodingString(Form);
202 if (!formString.empty())
203 OS << " [" << formString << ']';
204 else
205 OS << format(" [DW_FORM_Unknown_%x]", Form);
206 }
207
Greg Claytonc8c10322016-12-13 18:25:19 +0000208 DWARFUnit *U = Die.getDwarfUnit();
209 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000210
Paul Robinsone5400f82017-11-07 19:57:12 +0000211 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
212 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000213 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000214
Greg Claytonc8c10322016-12-13 18:25:19 +0000215 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000216
Greg Claytonc8c10322016-12-13 18:25:19 +0000217 StringRef Name;
218 std::string File;
219 auto Color = syntax::Enumerator;
220 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
221 Color = syntax::String;
222 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000223 if (LT->getFileNameByIndex(
224 formValue.getAsUnsignedConstant().getValue(),
225 U->getCompilationDir(),
226 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000227 File = '"' + File + '"';
228 Name = File;
229 }
230 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
231 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000232
Greg Claytonc8c10322016-12-13 18:25:19 +0000233 if (!Name.empty())
234 WithColor(OS, Color) << Name;
235 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
236 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000237 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
238 formValue.getAsUnsignedConstant()) {
239 // Print the actual address rather than the offset.
240 uint64_t LowPC, HighPC, Index;
241 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
242 OS << format("0x%016" PRIx64, HighPC);
243 else
244 formValue.dump(OS, DumpOpts);
245 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
246 Attr == DW_AT_data_member_location ||
247 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000248 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000249 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000250 formValue.dump(OS, DumpOpts);
251
Greg Claytonc8c10322016-12-13 18:25:19 +0000252 // We have dumped the attribute raw value. For some attributes
253 // having both the raw value and the pretty-printed value is
254 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000255 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000256 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
257 DINameKind::LinkageName))
258 OS << " \"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000259 } else if (Attr == DW_AT_type) {
260 OS << " \"";
261 dumpTypeName(OS, Die);
262 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000263 } else if (Attr == DW_AT_APPLE_property_attribute) {
264 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
265 dumpApplePropertyAttribute(OS, *OptVal);
266 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000267 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
268 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
269 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000270 }
George Rimar6957ab52017-08-15 12:32:54 +0000271
Greg Claytonc8c10322016-12-13 18:25:19 +0000272 OS << ")\n";
273}
274
Adrian Prantlb4a67902017-10-04 22:26:19 +0000275bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000276
277bool DWARFDie::isSubroutineDIE() const {
278 auto Tag = getTag();
279 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
280}
281
Adrian Prantlb4a67902017-10-04 22:26:19 +0000282Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000283 if (!isValid())
284 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000285 auto AbbrevDecl = getAbbreviationDeclarationPtr();
286 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000287 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
288 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000289}
290
Greg Clayton97d22182017-01-13 21:08:18 +0000291Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000292DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
293 if (!isValid())
294 return None;
295 auto AbbrevDecl = getAbbreviationDeclarationPtr();
296 if (AbbrevDecl) {
297 for (auto Attr : Attrs) {
298 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
299 return Value;
300 }
301 }
302 return None;
303}
304
305Optional<DWARFFormValue>
306DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
307 if (!isValid())
308 return None;
David Blaikie1914c822017-03-13 21:46:37 +0000309 auto Die = *this;
310 if (auto Value = Die.find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000311 return Value;
David Blaikie1914c822017-03-13 21:46:37 +0000312 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
313 Die = D;
314 if (auto Value = Die.find(Attrs))
315 return Value;
316 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
317 Die = D;
318 if (auto Value = Die.find(Attrs))
319 return Value;
Greg Claytonc109bbe2017-01-13 22:32:12 +0000320 return None;
321}
322
Greg Claytonc8c10322016-12-13 18:25:19 +0000323DWARFDie
324DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000325 if (auto SpecRef = toReference(find(Attr))) {
326 if (auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000327 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000328 }
329 return DWARFDie();
330}
331
Adrian Prantlb4a67902017-10-04 22:26:19 +0000332Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000333 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000334}
335
Greg Clayton2520c9e2016-12-19 20:36:41 +0000336Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000337 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000338 if (auto Address = FormValue->getAsAddress()) {
339 // High PC is an address.
340 return Address;
341 }
342 if (auto Offset = FormValue->getAsUnsignedConstant()) {
343 // High PC is an offset from LowPC.
344 return LowPC + *Offset;
345 }
346 }
347 return None;
348}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000349
George Rimara25d3292017-05-27 18:10:23 +0000350bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
351 uint64_t &SectionIndex) const {
352 auto F = find(DW_AT_low_pc);
353 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000354 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000355 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000356 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
357 LowPC = *LowPcAddr;
358 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000359 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000360 return true;
361 }
362 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000363}
364
Adrian Prantlb4a67902017-10-04 22:26:19 +0000365DWARFAddressRangesVector DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000366 if (isNULL())
367 return DWARFAddressRangesVector();
368 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000369 uint64_t LowPC, HighPC, Index;
370 if (getLowAndHighPC(LowPC, HighPC, Index))
371 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000372
Greg Claytonc8c10322016-12-13 18:25:19 +0000373 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000374 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000375 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000376 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000377 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000378 return RangeList.getAbsoluteRanges(U->getBaseAddress());
379 }
380 return DWARFAddressRangesVector();
381}
382
Adrian Prantlb4a67902017-10-04 22:26:19 +0000383void DWARFDie::collectChildrenAddressRanges(
384 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000385 if (isNULL())
386 return;
387 if (isSubprogramDIE()) {
388 const auto &DIERanges = getAddressRanges();
389 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
390 }
391
Adrian Prantlb4a67902017-10-04 22:26:19 +0000392 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000393 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000394}
395
396bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000397 for (const auto &R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000398 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000399 return true;
400 }
401 return false;
402}
403
Adrian Prantlb4a67902017-10-04 22:26:19 +0000404const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000405 if (!isSubroutineDIE())
406 return nullptr;
407 return getName(Kind);
408}
409
Adrian Prantlb4a67902017-10-04 22:26:19 +0000410const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000411 if (!isValid() || Kind == DINameKind::None)
412 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000413 // Try to get mangled name only if it was asked for.
414 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000415 if (auto Name = dwarf::toString(
416 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
417 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000418 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000419 }
Greg Clayton97d22182017-01-13 21:08:18 +0000420 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
421 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000422 return nullptr;
423}
424
David Blaikieefc4eba2017-02-06 20:19:02 +0000425uint64_t DWARFDie::getDeclLine() const {
426 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
427}
428
Greg Claytonc8c10322016-12-13 18:25:19 +0000429void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000430 uint32_t &CallColumn,
431 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000432 CallFile = toUnsigned(find(DW_AT_call_file), 0);
433 CallLine = toUnsigned(find(DW_AT_call_line), 0);
434 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000435 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000436}
437
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000438/// Helper to dump a DIE with all of its parents, but no siblings.
439static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
440 DIDumpOptions DumpOpts) {
441 if (!Die)
442 return Indent;
443 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000444 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000445 return Indent + 2;
446}
447
Adrian Prantld3f9f212017-09-20 17:44:00 +0000448void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000449 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000450 if (!isValid())
451 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000452 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000453 const uint32_t Offset = getOffset();
454 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000455 if (DumpOpts.ShowParents) {
456 DumpOpts.ShowParents = false;
457 Indent = dumpParentChain(getParent(), OS, Indent, DumpOpts);
458 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000459
Greg Claytonc8c10322016-12-13 18:25:19 +0000460 if (debug_info_data.isValidOffset(offset)) {
461 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
462 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000463
Greg Claytonc8c10322016-12-13 18:25:19 +0000464 if (abbrCode) {
465 auto AbbrevDecl = getAbbreviationDeclarationPtr();
466 if (AbbrevDecl) {
467 auto tagString = TagString(getTag());
468 if (!tagString.empty())
469 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
470 else
471 WithColor(OS, syntax::Tag).get().indent(Indent)
Adrian Prantlb4a67902017-10-04 22:26:19 +0000472 << format("DW_TAG_Unknown_%x", getTag());
Adrian Prantl318d1192017-06-06 23:28:45 +0000473
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000474 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000475 OS << format(" [%u] %c", abbrCode,
476 AbbrevDecl->hasChildren() ? '*' : ' ');
477 OS << '\n';
478
Greg Claytonc8c10322016-12-13 18:25:19 +0000479 // Dump all data in the DIE for the attributes.
480 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000481 if (AttrSpec.Form == DW_FORM_implicit_const) {
482 // We are dumping .debug_info section ,
483 // implicit_const attribute values are not really stored here,
484 // but in .debug_abbrev section. So we just skip such attrs.
485 continue;
486 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000487 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000488 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000489 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000490
Greg Claytonc8c10322016-12-13 18:25:19 +0000491 DWARFDie child = getFirstChild();
Adrian Prantld3f9f212017-09-20 17:44:00 +0000492 if (DumpOpts.RecurseDepth > 0 && child) {
493 DumpOpts.RecurseDepth--;
Greg Claytonc8c10322016-12-13 18:25:19 +0000494 while (child) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000495 child.dump(OS, Indent + 2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000496 child = child.getSibling();
497 }
498 }
499 } else {
500 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000501 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000502 }
503 } else {
504 OS.indent(Indent) << "NULL\n";
505 }
506 }
507}
508
Adrian Prantl3d523a62017-08-16 17:43:01 +0000509LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
510
Greg Clayton78a07bf2016-12-21 21:37:06 +0000511DWARFDie DWARFDie::getParent() const {
512 if (isValid())
513 return U->getParent(Die);
514 return DWARFDie();
515}
516
517DWARFDie DWARFDie::getSibling() const {
518 if (isValid())
519 return U->getSibling(Die);
520 return DWARFDie();
521}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000522
George Rimar0be860f2017-10-25 10:23:49 +0000523DWARFDie DWARFDie::getFirstChild() const {
524 if (isValid())
525 return U->getFirstChild(Die);
526 return DWARFDie();
527}
528
Adrian Prantlb4a67902017-10-04 22:26:19 +0000529iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000530 return make_range(attribute_iterator(*this, false),
531 attribute_iterator(*this, true));
532}
533
Adrian Prantlb4a67902017-10-04 22:26:19 +0000534DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
535 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000536 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
537 assert(AbbrDecl && "Must have abbreviation declaration");
538 if (End) {
539 // This is the end iterator so we set the index to the attribute count.
540 Index = AbbrDecl->getNumAttributes();
541 } else {
542 // This is the begin iterator so we extract the value for this->Index.
543 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
544 updateForIndex(*AbbrDecl, 0);
545 }
546}
547
548void DWARFDie::attribute_iterator::updateForIndex(
549 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
550 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000551 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000552 auto NumAttrs = AbbrDecl.getNumAttributes();
553 if (Index < NumAttrs) {
554 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
555 // Add the previous byte size of any previous attribute value.
556 AttrValue.Offset += AttrValue.ByteSize;
557 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
558 uint32_t ParseOffset = AttrValue.Offset;
559 auto U = Die.getDwarfUnit();
560 assert(U && "Die must have valid DWARF unit");
561 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000562 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000563 (void)b;
564 assert(b && "extractValue cannot fail on fully parsed DWARF");
565 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
566 } else {
567 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
568 AttrValue.clear();
569 }
570}
571
572DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
573 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
574 updateForIndex(*AbbrDecl, Index + 1);
575 return *this;
576}