blob: 33485e115ac3349dc9e7a679d681af8b1893fcd8 [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"
15#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"
Greg Claytonc8c10322016-12-13 18:25:19 +000018#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000019#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
20#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000021#include "llvm/Support/Dwarf.h"
22#include "llvm/Support/Format.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include "llvm/Support/MathExtras.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000024#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000025#include <algorithm>
26#include <cassert>
27#include <cinttypes>
28#include <cstdint>
29#include <string>
30#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000031
32using namespace llvm;
33using namespace dwarf;
34using namespace syntax;
35
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000036static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000037 OS << " (";
38 do {
39 uint64_t Shift = countTrailingZeros(Val);
40 assert(Shift < 64 && "undefined behavior");
41 uint64_t Bit = 1ULL << Shift;
42 auto PropName = ApplePropertyString(Bit);
43 if (!PropName.empty())
44 OS << PropName;
45 else
46 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
47 if (!(Val ^= Bit))
48 break;
49 OS << ", ";
50 } while (true);
51 OS << ")";
52}
53
54static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
55 unsigned AddressSize, unsigned Indent) {
56 if (Ranges.empty())
57 return;
58
59 for (const auto &Range: Ranges) {
60 OS << '\n';
61 OS.indent(Indent);
62 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
George Rimar4671f2e2017-05-16 12:30:59 +000063 AddressSize*2, Range.LowPC,
64 AddressSize*2, Range.HighPC);
Greg Claytonc8c10322016-12-13 18:25:19 +000065 }
66}
67
68static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
69 uint32_t *OffsetPtr, dwarf::Attribute Attr,
70 dwarf::Form Form, unsigned Indent) {
71 if (!Die.isValid())
72 return;
73 const char BaseIndent[] = " ";
74 OS << BaseIndent;
75 OS.indent(Indent+2);
76 auto attrString = AttributeString(Attr);
77 if (!attrString.empty())
78 WithColor(OS, syntax::Attribute) << attrString;
79 else
80 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
81
82 auto formString = FormEncodingString(Form);
83 if (!formString.empty())
84 OS << " [" << formString << ']';
85 else
86 OS << format(" [DW_FORM_Unknown_%x]", Form);
87
88 DWARFUnit *U = Die.getDwarfUnit();
89 DWARFFormValue formValue(Form);
90
91 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
92 return;
93
94 OS << "\t(";
95
96 StringRef Name;
97 std::string File;
98 auto Color = syntax::Enumerator;
99 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
100 Color = syntax::String;
101 if (const auto *LT = U->getContext().getLineTableForUnit(U))
102 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
103 File = '"' + File + '"';
104 Name = File;
105 }
106 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
107 Name = AttributeValueString(Attr, *Val);
108
109 if (!Name.empty())
110 WithColor(OS, Color) << Name;
111 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
112 OS << *formValue.getAsUnsignedConstant();
113 else
114 formValue.dump(OS);
115
116 // We have dumped the attribute raw value. For some attributes
117 // having both the raw value and the pretty-printed value is
118 // interesting. These attributes are handled below.
119 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
120 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
121 OS << " \"" << Name << '\"';
122 } else if (Attr == DW_AT_APPLE_property_attribute) {
123 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
124 dumpApplePropertyAttribute(OS, *OptVal);
125 } else if (Attr == DW_AT_ranges) {
126 dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
127 sizeof(BaseIndent)+Indent+4);
128 }
129
130 OS << ")\n";
131}
132
Greg Claytonc8c10322016-12-13 18:25:19 +0000133bool DWARFDie::isSubprogramDIE() const {
134 return getTag() == DW_TAG_subprogram;
135}
136
137bool DWARFDie::isSubroutineDIE() const {
138 auto Tag = getTag();
139 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
140}
141
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000142Optional<DWARFFormValue>
Greg Clayton97d22182017-01-13 21:08:18 +0000143DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000144 if (!isValid())
145 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000146 auto AbbrevDecl = getAbbreviationDeclarationPtr();
147 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000148 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
149 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000150}
151
Greg Clayton97d22182017-01-13 21:08:18 +0000152Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000153DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
154 if (!isValid())
155 return None;
156 auto AbbrevDecl = getAbbreviationDeclarationPtr();
157 if (AbbrevDecl) {
158 for (auto Attr : Attrs) {
159 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
160 return Value;
161 }
162 }
163 return None;
164}
165
166Optional<DWARFFormValue>
167DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
168 if (!isValid())
169 return None;
David Blaikie1914c822017-03-13 21:46:37 +0000170 auto Die = *this;
171 if (auto Value = Die.find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000172 return Value;
David Blaikie1914c822017-03-13 21:46:37 +0000173 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
174 Die = D;
175 if (auto Value = Die.find(Attrs))
176 return Value;
177 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
178 Die = D;
179 if (auto Value = Die.find(Attrs))
180 return Value;
Greg Claytonc109bbe2017-01-13 22:32:12 +0000181 return None;
182}
183
Greg Claytonc8c10322016-12-13 18:25:19 +0000184DWARFDie
185DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000186 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000187 if (SpecRef) {
188 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000189 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000190 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000191 }
192 return DWARFDie();
193}
194
Greg Clayton52fe1f62016-12-14 22:38:08 +0000195Optional<uint64_t>
196DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000197 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000198}
199
Greg Clayton2520c9e2016-12-19 20:36:41 +0000200Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000201 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000202 if (auto Address = FormValue->getAsAddress()) {
203 // High PC is an address.
204 return Address;
205 }
206 if (auto Offset = FormValue->getAsUnsignedConstant()) {
207 // High PC is an offset from LowPC.
208 return LowPC + *Offset;
209 }
210 }
211 return None;
212}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000213
George Rimara25d3292017-05-27 18:10:23 +0000214bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
215 uint64_t &SectionIndex) const {
216 auto F = find(DW_AT_low_pc);
217 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000218 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000219 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000220 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
221 LowPC = *LowPcAddr;
222 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000223 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000224 return true;
225 }
226 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000227}
228
229DWARFAddressRangesVector
230DWARFDie::getAddressRanges() const {
231 if (isNULL())
232 return DWARFAddressRangesVector();
233 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000234 uint64_t LowPC, HighPC, Index;
235 if (getLowAndHighPC(LowPC, HighPC, Index))
236 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000237
Greg Claytonc8c10322016-12-13 18:25:19 +0000238 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000239 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000240 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000241 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000242 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000243 return RangeList.getAbsoluteRanges(U->getBaseAddress());
244 }
245 return DWARFAddressRangesVector();
246}
247
248void
249DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
250 if (isNULL())
251 return;
252 if (isSubprogramDIE()) {
253 const auto &DIERanges = getAddressRanges();
254 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
255 }
256
Greg Clayton93e4fe82017-01-05 23:47:37 +0000257 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000258 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000259}
260
261bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
262 for (const auto& R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000263 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000264 return true;
265 }
266 return false;
267}
268
269const char *
270DWARFDie::getSubroutineName(DINameKind Kind) const {
271 if (!isSubroutineDIE())
272 return nullptr;
273 return getName(Kind);
274}
275
276const char *
277DWARFDie::getName(DINameKind Kind) const {
278 if (!isValid() || Kind == DINameKind::None)
279 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000280 // Try to get mangled name only if it was asked for.
281 if (Kind == DINameKind::LinkageName) {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000282 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
283 DW_AT_linkage_name}), nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000284 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000285 }
Greg Clayton97d22182017-01-13 21:08:18 +0000286 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
287 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000288 return nullptr;
289}
290
David Blaikieefc4eba2017-02-06 20:19:02 +0000291uint64_t DWARFDie::getDeclLine() const {
292 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
293}
294
Greg Claytonc8c10322016-12-13 18:25:19 +0000295void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000296 uint32_t &CallColumn,
297 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000298 CallFile = toUnsigned(find(DW_AT_call_file), 0);
299 CallLine = toUnsigned(find(DW_AT_call_line), 0);
300 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000301 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000302}
303
304void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
305 unsigned Indent) const {
306 if (!isValid())
307 return;
308 DataExtractor debug_info_data = U->getDebugInfoExtractor();
309 const uint32_t Offset = getOffset();
310 uint32_t offset = Offset;
311
312 if (debug_info_data.isValidOffset(offset)) {
313 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
314 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
315
316 if (abbrCode) {
317 auto AbbrevDecl = getAbbreviationDeclarationPtr();
318 if (AbbrevDecl) {
319 auto tagString = TagString(getTag());
320 if (!tagString.empty())
321 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
322 else
323 WithColor(OS, syntax::Tag).get().indent(Indent)
324 << format("DW_TAG_Unknown_%x", getTag());
325
326 OS << format(" [%u] %c\n", abbrCode,
327 AbbrevDecl->hasChildren() ? '*' : ' ');
328
329 // Dump all data in the DIE for the attributes.
330 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000331 if (AttrSpec.Form == DW_FORM_implicit_const) {
332 // We are dumping .debug_info section ,
333 // implicit_const attribute values are not really stored here,
334 // but in .debug_abbrev section. So we just skip such attrs.
335 continue;
336 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000337 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
338 Indent);
339 }
340
341 DWARFDie child = getFirstChild();
342 if (RecurseDepth > 0 && child) {
343 while (child) {
344 child.dump(OS, RecurseDepth-1, Indent+2);
345 child = child.getSibling();
346 }
347 }
348 } else {
349 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
350 << abbrCode << '\n';
351 }
352 } else {
353 OS.indent(Indent) << "NULL\n";
354 }
355 }
356}
357
Greg Clayton78a07bf2016-12-21 21:37:06 +0000358DWARFDie DWARFDie::getParent() const {
359 if (isValid())
360 return U->getParent(Die);
361 return DWARFDie();
362}
363
364DWARFDie DWARFDie::getSibling() const {
365 if (isValid())
366 return U->getSibling(Die);
367 return DWARFDie();
368}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000369
370iterator_range<DWARFDie::attribute_iterator>
371DWARFDie::attributes() const {
372 return make_range(attribute_iterator(*this, false),
373 attribute_iterator(*this, true));
374}
375
376DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
377 Die(D), AttrValue(0), Index(0) {
378 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
379 assert(AbbrDecl && "Must have abbreviation declaration");
380 if (End) {
381 // This is the end iterator so we set the index to the attribute count.
382 Index = AbbrDecl->getNumAttributes();
383 } else {
384 // This is the begin iterator so we extract the value for this->Index.
385 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
386 updateForIndex(*AbbrDecl, 0);
387 }
388}
389
390void DWARFDie::attribute_iterator::updateForIndex(
391 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
392 Index = I;
393 // AbbrDecl must be valid befor calling this function.
394 auto NumAttrs = AbbrDecl.getNumAttributes();
395 if (Index < NumAttrs) {
396 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
397 // Add the previous byte size of any previous attribute value.
398 AttrValue.Offset += AttrValue.ByteSize;
399 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
400 uint32_t ParseOffset = AttrValue.Offset;
401 auto U = Die.getDwarfUnit();
402 assert(U && "Die must have valid DWARF unit");
403 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
404 &ParseOffset, U);
405 (void)b;
406 assert(b && "extractValue cannot fail on fully parsed DWARF");
407 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
408 } else {
409 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
410 AttrValue.clear();
411 }
412}
413
414DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
415 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
416 updateForIndex(*AbbrDecl, Index + 1);
417 return *this;
418}