blob: 981f33c1f4fe276c2539d48a2f29acad3354fdbd [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
248bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
Greg Clayton52fe1f62016-12-14 22:38:08 +0000249 if (auto LowPCVal = getAttributeValueAsAddress(DW_AT_low_pc))
250 LowPC = *LowPCVal;
251 else
Greg Claytonc8c10322016-12-13 18:25:19 +0000252 return false;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000253
254 if (auto HighPCVal = getAttributeValueAsAddress(DW_AT_high_pc)) {
255 // High PC is an address.
256 HighPC = *HighPCVal;
257 } else if (auto Offset = getAttributeValueAsUnsignedConstant(DW_AT_high_pc)) {
258 // High PC is an offset from LowPC.
259 HighPC = LowPC + *Offset;
260 } else
261 return false;
262 return true;
Greg Claytonc8c10322016-12-13 18:25:19 +0000263}
264
265DWARFAddressRangesVector
266DWARFDie::getAddressRanges() const {
267 if (isNULL())
268 return DWARFAddressRangesVector();
269 // Single range specified by low/high PC.
270 uint64_t LowPC, HighPC;
271 if (getLowAndHighPC(LowPC, HighPC)) {
272 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
273 }
274 // Multiple ranges from .debug_ranges section.
Greg Clayton52fe1f62016-12-14 22:38:08 +0000275 auto RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges);
276 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000277 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000278 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000279 return RangeList.getAbsoluteRanges(U->getBaseAddress());
280 }
281 return DWARFAddressRangesVector();
282}
283
284void
285DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
286 if (isNULL())
287 return;
288 if (isSubprogramDIE()) {
289 const auto &DIERanges = getAddressRanges();
290 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
291 }
292
293 DWARFDie Child = getFirstChild();
294 while (Child) {
295 Child.collectChildrenAddressRanges(Ranges);
296 Child = Child.getSibling();
297 }
298}
299
300bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
301 for (const auto& R : getAddressRanges()) {
302 if (R.first <= Address && Address < R.second)
303 return true;
304 }
305 return false;
306}
307
308const char *
309DWARFDie::getSubroutineName(DINameKind Kind) const {
310 if (!isSubroutineDIE())
311 return nullptr;
312 return getName(Kind);
313}
314
315const char *
316DWARFDie::getName(DINameKind Kind) const {
317 if (!isValid() || Kind == DINameKind::None)
318 return nullptr;
319 const char *name = nullptr;
320 // Try to get mangled name only if it was asked for.
321 if (Kind == DINameKind::LinkageName) {
322 if ((name = getAttributeValueAsString(DW_AT_MIPS_linkage_name, nullptr)))
323 return name;
324 if ((name = getAttributeValueAsString(DW_AT_linkage_name, nullptr)))
325 return name;
326 }
327 if ((name = getAttributeValueAsString(DW_AT_name, nullptr)))
328 return name;
329 // Try to get name from specification DIE.
330 DWARFDie SpecDie = getAttributeValueAsReferencedDie(DW_AT_specification);
331 if (SpecDie && (name = SpecDie.getName(Kind)))
332 return name;
333 // Try to get name from abstract origin DIE.
334 DWARFDie AbsDie = getAttributeValueAsReferencedDie(DW_AT_abstract_origin);
335 if (AbsDie && (name = AbsDie.getName(Kind)))
336 return name;
337 return nullptr;
338}
339
340void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
341 uint32_t &CallColumn) const {
342 CallFile = getAttributeValueAsUnsignedConstant(DW_AT_call_file, 0);
343 CallLine = getAttributeValueAsUnsignedConstant(DW_AT_call_line, 0);
344 CallColumn = getAttributeValueAsUnsignedConstant(DW_AT_call_column, 0);
345}
346
347void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
348 unsigned Indent) const {
349 if (!isValid())
350 return;
351 DataExtractor debug_info_data = U->getDebugInfoExtractor();
352 const uint32_t Offset = getOffset();
353 uint32_t offset = Offset;
354
355 if (debug_info_data.isValidOffset(offset)) {
356 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
357 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
358
359 if (abbrCode) {
360 auto AbbrevDecl = getAbbreviationDeclarationPtr();
361 if (AbbrevDecl) {
362 auto tagString = TagString(getTag());
363 if (!tagString.empty())
364 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
365 else
366 WithColor(OS, syntax::Tag).get().indent(Indent)
367 << format("DW_TAG_Unknown_%x", getTag());
368
369 OS << format(" [%u] %c\n", abbrCode,
370 AbbrevDecl->hasChildren() ? '*' : ' ');
371
372 // Dump all data in the DIE for the attributes.
373 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
374 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
375 Indent);
376 }
377
378 DWARFDie child = getFirstChild();
379 if (RecurseDepth > 0 && child) {
380 while (child) {
381 child.dump(OS, RecurseDepth-1, Indent+2);
382 child = child.getSibling();
383 }
384 }
385 } else {
386 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
387 << abbrCode << '\n';
388 }
389 } else {
390 OS.indent(Indent) << "NULL\n";
391 }
392 }
393}
394
395
396void DWARFDie::getInlinedChainForAddress(
397 const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
398 if (isNULL())
399 return;
400 DWARFDie DIE(*this);
401 while (DIE) {
402 // Append current DIE to inlined chain only if it has correct tag
403 // (e.g. it is not a lexical block).
404 if (DIE.isSubroutineDIE())
405 InlinedChain.push_back(DIE);
406
407 // Try to get child which also contains provided address.
408 DWARFDie Child = DIE.getFirstChild();
409 while (Child) {
410 if (Child.addressRangeContainsAddress(Address)) {
411 // Assume there is only one such child.
412 break;
413 }
414 Child = Child.getSibling();
415 }
416 DIE = Child;
417 }
418 // Reverse the obtained chain to make the root of inlined chain last.
419 std::reverse(InlinedChain.begin(), InlinedChain.end());
420}
421