blob: 94777109e44f0c3800fc4b06bcbc43219fa1cd4a [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>
137DWARFDie::getAttributeValue(dwarf::Attribute Attr) const {
138 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
146const char *DWARFDie::getAttributeValueAsString(dwarf::Attribute Attr,
147 const char *FailValue) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000148 auto FormValue = getAttributeValue(Attr);
149 if (!FormValue)
Greg Claytonc8c10322016-12-13 18:25:19 +0000150 return FailValue;
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000151 Optional<const char *> Result = FormValue->getAsCString();
Greg Claytonc8c10322016-12-13 18:25:19 +0000152 return Result.hasValue() ? Result.getValue() : FailValue;
153}
154
155uint64_t DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr,
156 uint64_t FailValue) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000157 if (auto Value = getAttributeValueAsAddress(Attr))
158 return *Value;
159 return FailValue;
160}
161
162Optional<uint64_t>
163DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr) const {
164 if (auto FormValue = getAttributeValue(Attr))
165 return FormValue->getAsAddress();
166 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000167}
168
169int64_t DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr,
170 int64_t FailValue) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000171 if (auto Value = getAttributeValueAsSignedConstant(Attr))
172 return *Value;
173 return FailValue;
174}
175
176Optional<int64_t>
177DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr) const {
178 if (auto FormValue = getAttributeValue(Attr))
179 return FormValue->getAsSignedConstant();
180 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000181}
182
183uint64_t
184DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr,
185 uint64_t FailValue) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000186 if (auto Value = getAttributeValueAsUnsignedConstant(Attr))
187 return *Value;
188 return FailValue;
189}
190
191
192Optional<uint64_t>
193DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr) const {
194 if (auto FormValue = getAttributeValue(Attr))
195 return FormValue->getAsUnsignedConstant();
196 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000197}
198
199uint64_t DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr,
200 uint64_t FailValue) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000201 if (auto Value = getAttributeValueAsReference(Attr))
202 return *Value;
203 return FailValue;
204}
205
206
207Optional<uint64_t>
208DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr) const {
209 if (auto FormValue = getAttributeValue(Attr))
210 return FormValue->getAsReference();
211 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000212}
213
214uint64_t DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr,
215 uint64_t FailValue) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000216 if (auto Value = getAttributeValueAsSectionOffset(Attr))
217 return *Value;
218 return FailValue;
Greg Claytonc8c10322016-12-13 18:25:19 +0000219}
220
Greg Clayton52fe1f62016-12-14 22:38:08 +0000221Optional<uint64_t>
222DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr) const {
223 if (auto FormValue = getAttributeValue(Attr))
224 return FormValue->getAsSectionOffset();
225 return None;
226}
227
228
Greg Claytonc8c10322016-12-13 18:25:19 +0000229DWARFDie
230DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000231 auto SpecRef = getAttributeValueAsReference(Attr);
232 if (SpecRef) {
233 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000234 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000235 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000236 }
237 return DWARFDie();
238}
239
Greg Clayton52fe1f62016-12-14 22:38:08 +0000240Optional<uint64_t>
241DWARFDie::getRangesBaseAttribute() const {
242 auto Result = getAttributeValueAsSectionOffset(DW_AT_rnglists_base);
243 if (Result)
Greg Claytonc8c10322016-12-13 18:25:19 +0000244 return Result;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000245 return getAttributeValueAsSectionOffset(DW_AT_GNU_ranges_base);
Greg Claytonc8c10322016-12-13 18:25:19 +0000246}
247
Greg Clayton2520c9e2016-12-19 20:36:41 +0000248Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
249 if (auto FormValue = getAttributeValue(DW_AT_high_pc)) {
250 if (auto Address = FormValue->getAsAddress()) {
251 // High PC is an address.
252 return Address;
253 }
254 if (auto Offset = FormValue->getAsUnsignedConstant()) {
255 // High PC is an offset from LowPC.
256 return LowPC + *Offset;
257 }
258 }
259 return None;
260}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000261
Greg Clayton2520c9e2016-12-19 20:36:41 +0000262bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
263 auto LowPcAddr = getAttributeValueAsAddress(DW_AT_low_pc);
264 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000265 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000266 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
267 LowPC = *LowPcAddr;
268 HighPC = *HighPcAddr;
269 return true;
270 }
271 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000272}
273
274DWARFAddressRangesVector
275DWARFDie::getAddressRanges() const {
276 if (isNULL())
277 return DWARFAddressRangesVector();
278 // Single range specified by low/high PC.
279 uint64_t LowPC, HighPC;
280 if (getLowAndHighPC(LowPC, HighPC)) {
281 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
282 }
283 // Multiple ranges from .debug_ranges section.
Greg Clayton52fe1f62016-12-14 22:38:08 +0000284 auto RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges);
285 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000286 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000287 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000288 return RangeList.getAbsoluteRanges(U->getBaseAddress());
289 }
290 return DWARFAddressRangesVector();
291}
292
293void
294DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
295 if (isNULL())
296 return;
297 if (isSubprogramDIE()) {
298 const auto &DIERanges = getAddressRanges();
299 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
300 }
301
302 DWARFDie Child = getFirstChild();
303 while (Child) {
304 Child.collectChildrenAddressRanges(Ranges);
305 Child = Child.getSibling();
306 }
307}
308
309bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
310 for (const auto& R : getAddressRanges()) {
311 if (R.first <= Address && Address < R.second)
312 return true;
313 }
314 return false;
315}
316
317const char *
318DWARFDie::getSubroutineName(DINameKind Kind) const {
319 if (!isSubroutineDIE())
320 return nullptr;
321 return getName(Kind);
322}
323
324const char *
325DWARFDie::getName(DINameKind Kind) const {
326 if (!isValid() || Kind == DINameKind::None)
327 return nullptr;
328 const char *name = nullptr;
329 // Try to get mangled name only if it was asked for.
330 if (Kind == DINameKind::LinkageName) {
331 if ((name = getAttributeValueAsString(DW_AT_MIPS_linkage_name, nullptr)))
332 return name;
333 if ((name = getAttributeValueAsString(DW_AT_linkage_name, nullptr)))
334 return name;
335 }
336 if ((name = getAttributeValueAsString(DW_AT_name, nullptr)))
337 return name;
338 // Try to get name from specification DIE.
339 DWARFDie SpecDie = getAttributeValueAsReferencedDie(DW_AT_specification);
340 if (SpecDie && (name = SpecDie.getName(Kind)))
341 return name;
342 // Try to get name from abstract origin DIE.
343 DWARFDie AbsDie = getAttributeValueAsReferencedDie(DW_AT_abstract_origin);
344 if (AbsDie && (name = AbsDie.getName(Kind)))
345 return name;
346 return nullptr;
347}
348
349void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
350 uint32_t &CallColumn) const {
351 CallFile = getAttributeValueAsUnsignedConstant(DW_AT_call_file, 0);
352 CallLine = getAttributeValueAsUnsignedConstant(DW_AT_call_line, 0);
353 CallColumn = getAttributeValueAsUnsignedConstant(DW_AT_call_column, 0);
354}
355
356void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
357 unsigned Indent) const {
358 if (!isValid())
359 return;
360 DataExtractor debug_info_data = U->getDebugInfoExtractor();
361 const uint32_t Offset = getOffset();
362 uint32_t offset = Offset;
363
364 if (debug_info_data.isValidOffset(offset)) {
365 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
366 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
367
368 if (abbrCode) {
369 auto AbbrevDecl = getAbbreviationDeclarationPtr();
370 if (AbbrevDecl) {
371 auto tagString = TagString(getTag());
372 if (!tagString.empty())
373 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
374 else
375 WithColor(OS, syntax::Tag).get().indent(Indent)
376 << format("DW_TAG_Unknown_%x", getTag());
377
378 OS << format(" [%u] %c\n", abbrCode,
379 AbbrevDecl->hasChildren() ? '*' : ' ');
380
381 // Dump all data in the DIE for the attributes.
382 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
383 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
384 Indent);
385 }
386
387 DWARFDie child = getFirstChild();
388 if (RecurseDepth > 0 && child) {
389 while (child) {
390 child.dump(OS, RecurseDepth-1, Indent+2);
391 child = child.getSibling();
392 }
393 }
394 } else {
395 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
396 << abbrCode << '\n';
397 }
398 } else {
399 OS.indent(Indent) << "NULL\n";
400 }
401 }
402}
403
404
405void DWARFDie::getInlinedChainForAddress(
406 const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
407 if (isNULL())
408 return;
409 DWARFDie DIE(*this);
410 while (DIE) {
411 // Append current DIE to inlined chain only if it has correct tag
412 // (e.g. it is not a lexical block).
413 if (DIE.isSubroutineDIE())
414 InlinedChain.push_back(DIE);
415
416 // Try to get child which also contains provided address.
417 DWARFDie Child = DIE.getFirstChild();
418 while (Child) {
419 if (Child.addressRangeContainsAddress(Address)) {
420 // Assume there is only one such child.
421 break;
422 }
423 Child = Child.getSibling();
424 }
425 DIE = Child;
426 }
427 // Reverse the obtained chain to make the root of inlined chain last.
428 std::reverse(InlinedChain.begin(), InlinedChain.end());
429}
430