blob: fc49fdeaad5c0b902e09a5e813a7af894ced9b4d [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
Greg Claytonc8c10322016-12-13 18:25:19 +000010#include "SyntaxHighlighting.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"
14#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000015#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
17#include "llvm/DebugInfo/DWARF/DWARFDie.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 ")",
63 AddressSize*2, Range.first,
64 AddressSize*2, Range.second);
65 }
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>
153DWARFDie::findRecursively(dwarf::Attribute Attr) const {
154 if (!isValid())
155 return None;
156 if (auto Value = find(Attr))
157 return Value;
158 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
159 if (auto Value = Die.find(Attr))
160 return Value;
161 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_specification))
162 if (auto Value = Die.find(Attr))
163 return Value;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000164 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000165}
166
Greg Claytonc109bbe2017-01-13 22:32:12 +0000167Optional<DWARFFormValue>
168DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
169 if (!isValid())
170 return None;
171 auto AbbrevDecl = getAbbreviationDeclarationPtr();
172 if (AbbrevDecl) {
173 for (auto Attr : Attrs) {
174 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
175 return Value;
176 }
177 }
178 return None;
179}
180
181Optional<DWARFFormValue>
182DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
183 if (!isValid())
184 return None;
185 if (auto Value = find(Attrs))
186 return Value;
187 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
188 if (auto Value = Die.find(Attrs))
189 return Value;
190 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_specification))
191 if (auto Value = Die.find(Attrs))
192 return Value;
193 return None;
194}
195
Greg Claytonc8c10322016-12-13 18:25:19 +0000196DWARFDie
197DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000198 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000199 if (SpecRef) {
200 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000201 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000202 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000203 }
204 return DWARFDie();
205}
206
Greg Clayton52fe1f62016-12-14 22:38:08 +0000207Optional<uint64_t>
208DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000209 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000210}
211
Greg Clayton2520c9e2016-12-19 20:36:41 +0000212Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000213 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000214 if (auto Address = FormValue->getAsAddress()) {
215 // High PC is an address.
216 return Address;
217 }
218 if (auto Offset = FormValue->getAsUnsignedConstant()) {
219 // High PC is an offset from LowPC.
220 return LowPC + *Offset;
221 }
222 }
223 return None;
224}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000225
Greg Clayton2520c9e2016-12-19 20:36:41 +0000226bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000227 auto LowPcAddr = toAddress(find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000228 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000229 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000230 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
231 LowPC = *LowPcAddr;
232 HighPC = *HighPcAddr;
233 return true;
234 }
235 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000236}
237
238DWARFAddressRangesVector
239DWARFDie::getAddressRanges() const {
240 if (isNULL())
241 return DWARFAddressRangesVector();
242 // Single range specified by low/high PC.
243 uint64_t LowPC, HighPC;
244 if (getLowAndHighPC(LowPC, HighPC)) {
245 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
246 }
247 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000248 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000249 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000250 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000251 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000252 return RangeList.getAbsoluteRanges(U->getBaseAddress());
253 }
254 return DWARFAddressRangesVector();
255}
256
257void
258DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
259 if (isNULL())
260 return;
261 if (isSubprogramDIE()) {
262 const auto &DIERanges = getAddressRanges();
263 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
264 }
265
Greg Clayton93e4fe82017-01-05 23:47:37 +0000266 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000267 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000268}
269
270bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
271 for (const auto& R : getAddressRanges()) {
272 if (R.first <= Address && Address < R.second)
273 return true;
274 }
275 return false;
276}
277
278const char *
279DWARFDie::getSubroutineName(DINameKind Kind) const {
280 if (!isSubroutineDIE())
281 return nullptr;
282 return getName(Kind);
283}
284
285const char *
286DWARFDie::getName(DINameKind Kind) const {
287 if (!isValid() || Kind == DINameKind::None)
288 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000289 // Try to get mangled name only if it was asked for.
290 if (Kind == DINameKind::LinkageName) {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000291 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
292 DW_AT_linkage_name}), nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000293 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000294 }
Greg Clayton97d22182017-01-13 21:08:18 +0000295 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
296 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000297 return nullptr;
298}
299
David Blaikieefc4eba2017-02-06 20:19:02 +0000300uint64_t DWARFDie::getDeclLine() const {
301 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
302}
303
Greg Claytonc8c10322016-12-13 18:25:19 +0000304void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
305 uint32_t &CallColumn) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000306 CallFile = toUnsigned(find(DW_AT_call_file), 0);
307 CallLine = toUnsigned(find(DW_AT_call_line), 0);
308 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000309}
310
311void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
312 unsigned Indent) const {
313 if (!isValid())
314 return;
315 DataExtractor debug_info_data = U->getDebugInfoExtractor();
316 const uint32_t Offset = getOffset();
317 uint32_t offset = Offset;
318
319 if (debug_info_data.isValidOffset(offset)) {
320 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
321 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
322
323 if (abbrCode) {
324 auto AbbrevDecl = getAbbreviationDeclarationPtr();
325 if (AbbrevDecl) {
326 auto tagString = TagString(getTag());
327 if (!tagString.empty())
328 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
329 else
330 WithColor(OS, syntax::Tag).get().indent(Indent)
331 << format("DW_TAG_Unknown_%x", getTag());
332
333 OS << format(" [%u] %c\n", abbrCode,
334 AbbrevDecl->hasChildren() ? '*' : ' ');
335
336 // Dump all data in the DIE for the attributes.
337 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000338 if (AttrSpec.Form == DW_FORM_implicit_const) {
339 // We are dumping .debug_info section ,
340 // implicit_const attribute values are not really stored here,
341 // but in .debug_abbrev section. So we just skip such attrs.
342 continue;
343 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000344 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
345 Indent);
346 }
347
348 DWARFDie child = getFirstChild();
349 if (RecurseDepth > 0 && child) {
350 while (child) {
351 child.dump(OS, RecurseDepth-1, Indent+2);
352 child = child.getSibling();
353 }
354 }
355 } else {
356 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
357 << abbrCode << '\n';
358 }
359 } else {
360 OS.indent(Indent) << "NULL\n";
361 }
362 }
363}
364
Greg Claytonc8c10322016-12-13 18:25:19 +0000365void DWARFDie::getInlinedChainForAddress(
366 const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
367 if (isNULL())
368 return;
369 DWARFDie DIE(*this);
370 while (DIE) {
371 // Append current DIE to inlined chain only if it has correct tag
372 // (e.g. it is not a lexical block).
373 if (DIE.isSubroutineDIE())
374 InlinedChain.push_back(DIE);
375
376 // Try to get child which also contains provided address.
377 DWARFDie Child = DIE.getFirstChild();
378 while (Child) {
379 if (Child.addressRangeContainsAddress(Address)) {
380 // Assume there is only one such child.
381 break;
382 }
383 Child = Child.getSibling();
384 }
385 DIE = Child;
386 }
387 // Reverse the obtained chain to make the root of inlined chain last.
388 std::reverse(InlinedChain.begin(), InlinedChain.end());
389}
390
Greg Clayton78a07bf2016-12-21 21:37:06 +0000391DWARFDie DWARFDie::getParent() const {
392 if (isValid())
393 return U->getParent(Die);
394 return DWARFDie();
395}
396
397DWARFDie DWARFDie::getSibling() const {
398 if (isValid())
399 return U->getSibling(Die);
400 return DWARFDie();
401}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000402
403iterator_range<DWARFDie::attribute_iterator>
404DWARFDie::attributes() const {
405 return make_range(attribute_iterator(*this, false),
406 attribute_iterator(*this, true));
407}
408
409DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
410 Die(D), AttrValue(0), Index(0) {
411 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
412 assert(AbbrDecl && "Must have abbreviation declaration");
413 if (End) {
414 // This is the end iterator so we set the index to the attribute count.
415 Index = AbbrDecl->getNumAttributes();
416 } else {
417 // This is the begin iterator so we extract the value for this->Index.
418 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
419 updateForIndex(*AbbrDecl, 0);
420 }
421}
422
423void DWARFDie::attribute_iterator::updateForIndex(
424 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
425 Index = I;
426 // AbbrDecl must be valid befor calling this function.
427 auto NumAttrs = AbbrDecl.getNumAttributes();
428 if (Index < NumAttrs) {
429 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
430 // Add the previous byte size of any previous attribute value.
431 AttrValue.Offset += AttrValue.ByteSize;
432 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
433 uint32_t ParseOffset = AttrValue.Offset;
434 auto U = Die.getDwarfUnit();
435 assert(U && "Die must have valid DWARF unit");
436 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
437 &ParseOffset, U);
438 (void)b;
439 assert(b && "extractValue cannot fail on fully parsed DWARF");
440 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
441 } else {
442 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
443 AttrValue.clear();
444 }
445}
446
447DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
448 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
449 updateForIndex(*AbbrDecl, Index + 1);
450 return *this;
451}