blob: 11fb726f4338b5fe3bb87ce412f0bc5cfd9438c6 [file] [log] [blame]
Greg Claytonc8c10322016-12-13 18:25:19 +00001//===-- DWARFDie.cpp ------------------------------------------------------===//
2//
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 "llvm/DebugInfo/DWARF/DWARFDie.h"
11#include "SyntaxHighlighting.h"
12#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
13#include "llvm/DebugInfo/DWARF/DWARFContext.h"
14#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
15#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17#include "llvm/Support/DataTypes.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/Dwarf.h"
20#include "llvm/Support/Format.h"
21#include "llvm/Support/raw_ostream.h"
22
23using namespace llvm;
24using namespace dwarf;
25using namespace syntax;
26
27namespace {
28 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
29 OS << " (";
30 do {
31 uint64_t Shift = countTrailingZeros(Val);
32 assert(Shift < 64 && "undefined behavior");
33 uint64_t Bit = 1ULL << Shift;
34 auto PropName = ApplePropertyString(Bit);
35 if (!PropName.empty())
36 OS << PropName;
37 else
38 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
39 if (!(Val ^= Bit))
40 break;
41 OS << ", ";
42 } while (true);
43 OS << ")";
44}
45
46static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
47 unsigned AddressSize, unsigned Indent) {
48 if (Ranges.empty())
49 return;
50
51 for (const auto &Range: Ranges) {
52 OS << '\n';
53 OS.indent(Indent);
54 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
55 AddressSize*2, Range.first,
56 AddressSize*2, Range.second);
57 }
58}
59
60static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
61 uint32_t *OffsetPtr, dwarf::Attribute Attr,
62 dwarf::Form Form, unsigned Indent) {
63 if (!Die.isValid())
64 return;
65 const char BaseIndent[] = " ";
66 OS << BaseIndent;
67 OS.indent(Indent+2);
68 auto attrString = AttributeString(Attr);
69 if (!attrString.empty())
70 WithColor(OS, syntax::Attribute) << attrString;
71 else
72 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
73
74 auto formString = FormEncodingString(Form);
75 if (!formString.empty())
76 OS << " [" << formString << ']';
77 else
78 OS << format(" [DW_FORM_Unknown_%x]", Form);
79
80 DWARFUnit *U = Die.getDwarfUnit();
81 DWARFFormValue formValue(Form);
82
83 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
84 return;
85
86 OS << "\t(";
87
88 StringRef Name;
89 std::string File;
90 auto Color = syntax::Enumerator;
91 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
92 Color = syntax::String;
93 if (const auto *LT = U->getContext().getLineTableForUnit(U))
94 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
95 File = '"' + File + '"';
96 Name = File;
97 }
98 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
99 Name = AttributeValueString(Attr, *Val);
100
101 if (!Name.empty())
102 WithColor(OS, Color) << Name;
103 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
104 OS << *formValue.getAsUnsignedConstant();
105 else
106 formValue.dump(OS);
107
108 // We have dumped the attribute raw value. For some attributes
109 // having both the raw value and the pretty-printed value is
110 // interesting. These attributes are handled below.
111 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
112 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
113 OS << " \"" << Name << '\"';
114 } else if (Attr == DW_AT_APPLE_property_attribute) {
115 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
116 dumpApplePropertyAttribute(OS, *OptVal);
117 } else if (Attr == DW_AT_ranges) {
118 dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
119 sizeof(BaseIndent)+Indent+4);
120 }
121
122 OS << ")\n";
123}
124
125} // end anonymous namespace
126
127bool DWARFDie::isSubprogramDIE() const {
128 return getTag() == DW_TAG_subprogram;
129}
130
131bool DWARFDie::isSubroutineDIE() const {
132 auto Tag = getTag();
133 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
134}
135
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000136Optional<DWARFFormValue>
Greg Clayton97d22182017-01-13 21:08:18 +0000137DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000138 if (!isValid())
139 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000140 auto AbbrevDecl = getAbbreviationDeclarationPtr();
141 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000142 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
143 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000144}
145
Greg Clayton97d22182017-01-13 21:08:18 +0000146Optional<DWARFFormValue>
147DWARFDie::findRecursively(dwarf::Attribute Attr) const {
148 if (!isValid())
149 return None;
150 if (auto Value = find(Attr))
151 return Value;
152 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
153 if (auto Value = Die.find(Attr))
154 return Value;
155 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_specification))
156 if (auto Value = Die.find(Attr))
157 return Value;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000158 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000159}
160
Greg Claytonc8c10322016-12-13 18:25:19 +0000161DWARFDie
162DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000163 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000164 if (SpecRef) {
165 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000166 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000167 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000168 }
169 return DWARFDie();
170}
171
Greg Clayton52fe1f62016-12-14 22:38:08 +0000172Optional<uint64_t>
173DWARFDie::getRangesBaseAttribute() const {
Greg Clayton97d22182017-01-13 21:08:18 +0000174 auto Result = toSectionOffset(find(DW_AT_rnglists_base));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000175 if (Result)
Greg Claytonc8c10322016-12-13 18:25:19 +0000176 return Result;
Greg Clayton97d22182017-01-13 21:08:18 +0000177 return toSectionOffset(find(DW_AT_GNU_ranges_base));
Greg Claytonc8c10322016-12-13 18:25:19 +0000178}
179
Greg Clayton2520c9e2016-12-19 20:36:41 +0000180Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000181 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000182 if (auto Address = FormValue->getAsAddress()) {
183 // High PC is an address.
184 return Address;
185 }
186 if (auto Offset = FormValue->getAsUnsignedConstant()) {
187 // High PC is an offset from LowPC.
188 return LowPC + *Offset;
189 }
190 }
191 return None;
192}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000193
Greg Clayton2520c9e2016-12-19 20:36:41 +0000194bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000195 auto LowPcAddr = toAddress(find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000196 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000197 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000198 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
199 LowPC = *LowPcAddr;
200 HighPC = *HighPcAddr;
201 return true;
202 }
203 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000204}
205
206DWARFAddressRangesVector
207DWARFDie::getAddressRanges() const {
208 if (isNULL())
209 return DWARFAddressRangesVector();
210 // Single range specified by low/high PC.
211 uint64_t LowPC, HighPC;
212 if (getLowAndHighPC(LowPC, HighPC)) {
213 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
214 }
215 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000216 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000217 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000218 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000219 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000220 return RangeList.getAbsoluteRanges(U->getBaseAddress());
221 }
222 return DWARFAddressRangesVector();
223}
224
225void
226DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
227 if (isNULL())
228 return;
229 if (isSubprogramDIE()) {
230 const auto &DIERanges = getAddressRanges();
231 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
232 }
233
Greg Clayton93e4fe82017-01-05 23:47:37 +0000234 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000235 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000236}
237
238bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
239 for (const auto& R : getAddressRanges()) {
240 if (R.first <= Address && Address < R.second)
241 return true;
242 }
243 return false;
244}
245
246const char *
247DWARFDie::getSubroutineName(DINameKind Kind) const {
248 if (!isSubroutineDIE())
249 return nullptr;
250 return getName(Kind);
251}
252
253const char *
254DWARFDie::getName(DINameKind Kind) const {
255 if (!isValid() || Kind == DINameKind::None)
256 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000257 // Try to get mangled name only if it was asked for.
258 if (Kind == DINameKind::LinkageName) {
Greg Clayton97d22182017-01-13 21:08:18 +0000259 if (auto Name = dwarf::toString(findRecursively(DW_AT_MIPS_linkage_name),
260 nullptr))
261 return Name;
262 if (auto Name = dwarf::toString(findRecursively(DW_AT_linkage_name),
263 nullptr))
264 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000265 }
Greg Clayton97d22182017-01-13 21:08:18 +0000266 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
267 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000268 return nullptr;
269}
270
271void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
272 uint32_t &CallColumn) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000273 CallFile = toUnsigned(find(DW_AT_call_file), 0);
274 CallLine = toUnsigned(find(DW_AT_call_line), 0);
275 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000276}
277
278void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
279 unsigned Indent) const {
280 if (!isValid())
281 return;
282 DataExtractor debug_info_data = U->getDebugInfoExtractor();
283 const uint32_t Offset = getOffset();
284 uint32_t offset = Offset;
285
286 if (debug_info_data.isValidOffset(offset)) {
287 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
288 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
289
290 if (abbrCode) {
291 auto AbbrevDecl = getAbbreviationDeclarationPtr();
292 if (AbbrevDecl) {
293 auto tagString = TagString(getTag());
294 if (!tagString.empty())
295 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
296 else
297 WithColor(OS, syntax::Tag).get().indent(Indent)
298 << format("DW_TAG_Unknown_%x", getTag());
299
300 OS << format(" [%u] %c\n", abbrCode,
301 AbbrevDecl->hasChildren() ? '*' : ' ');
302
303 // Dump all data in the DIE for the attributes.
304 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
305 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
306 Indent);
307 }
308
309 DWARFDie child = getFirstChild();
310 if (RecurseDepth > 0 && child) {
311 while (child) {
312 child.dump(OS, RecurseDepth-1, Indent+2);
313 child = child.getSibling();
314 }
315 }
316 } else {
317 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
318 << abbrCode << '\n';
319 }
320 } else {
321 OS.indent(Indent) << "NULL\n";
322 }
323 }
324}
325
326
327void DWARFDie::getInlinedChainForAddress(
328 const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
329 if (isNULL())
330 return;
331 DWARFDie DIE(*this);
332 while (DIE) {
333 // Append current DIE to inlined chain only if it has correct tag
334 // (e.g. it is not a lexical block).
335 if (DIE.isSubroutineDIE())
336 InlinedChain.push_back(DIE);
337
338 // Try to get child which also contains provided address.
339 DWARFDie Child = DIE.getFirstChild();
340 while (Child) {
341 if (Child.addressRangeContainsAddress(Address)) {
342 // Assume there is only one such child.
343 break;
344 }
345 Child = Child.getSibling();
346 }
347 DIE = Child;
348 }
349 // Reverse the obtained chain to make the root of inlined chain last.
350 std::reverse(InlinedChain.begin(), InlinedChain.end());
351}
352
Greg Clayton78a07bf2016-12-21 21:37:06 +0000353DWARFDie DWARFDie::getParent() const {
354 if (isValid())
355 return U->getParent(Die);
356 return DWARFDie();
357}
358
359DWARFDie DWARFDie::getSibling() const {
360 if (isValid())
361 return U->getSibling(Die);
362 return DWARFDie();
363}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000364
365iterator_range<DWARFDie::attribute_iterator>
366DWARFDie::attributes() const {
367 return make_range(attribute_iterator(*this, false),
368 attribute_iterator(*this, true));
369}
370
371DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
372 Die(D), AttrValue(0), Index(0) {
373 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
374 assert(AbbrDecl && "Must have abbreviation declaration");
375 if (End) {
376 // This is the end iterator so we set the index to the attribute count.
377 Index = AbbrDecl->getNumAttributes();
378 } else {
379 // This is the begin iterator so we extract the value for this->Index.
380 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
381 updateForIndex(*AbbrDecl, 0);
382 }
383}
384
385void DWARFDie::attribute_iterator::updateForIndex(
386 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
387 Index = I;
388 // AbbrDecl must be valid befor calling this function.
389 auto NumAttrs = AbbrDecl.getNumAttributes();
390 if (Index < NumAttrs) {
391 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
392 // Add the previous byte size of any previous attribute value.
393 AttrValue.Offset += AttrValue.ByteSize;
394 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
395 uint32_t ParseOffset = AttrValue.Offset;
396 auto U = Die.getDwarfUnit();
397 assert(U && "Die must have valid DWARF unit");
398 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
399 &ParseOffset, U);
400 (void)b;
401 assert(b && "extractValue cannot fail on fully parsed DWARF");
402 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
403 } else {
404 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
405 AttrValue.clear();
406 }
407}
408
409DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
410 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
411 updateForIndex(*AbbrDecl, Index + 1);
412 return *this;
413}