blob: 82f373e9c5cba6ab3a9a31cb33472e48b3af7ba8 [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"
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"
Zachary Turner264b5d92017-06-07 03:48:56 +000014#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000015#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"
Reid Klecknera0587362017-08-29 21:41:21 +000018#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000021#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000022#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000023#include "llvm/Support/Format.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000024#include "llvm/Support/MathExtras.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000025#include "llvm/Support/WithColor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000026#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000027#include <algorithm>
28#include <cassert>
29#include <cinttypes>
30#include <cstdint>
31#include <string>
32#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000033
34using namespace llvm;
35using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000036using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000037
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000038static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000039 OS << " (";
40 do {
41 uint64_t Shift = countTrailingZeros(Val);
42 assert(Shift < 64 && "undefined behavior");
43 uint64_t Bit = 1ULL << Shift;
44 auto PropName = ApplePropertyString(Bit);
45 if (!PropName.empty())
46 OS << PropName;
47 else
48 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
49 if (!(Val ^= Bit))
50 break;
51 OS << ", ";
52 } while (true);
53 OS << ")";
54}
55
George Rimar6957ab52017-08-15 12:32:54 +000056static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
57 const DWARFAddressRangesVector &Ranges,
58 unsigned AddressSize, unsigned Indent,
59 const DIDumpOptions &DumpOpts) {
George Rimare1c30f72017-08-15 15:54:43 +000060 ArrayRef<SectionName> SectionNames;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000061 if (DumpOpts.Verbose)
George Rimare1c30f72017-08-15 15:54:43 +000062 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000063
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000064 for (const DWARFAddressRange &R : Ranges) {
George Rimar6957ab52017-08-15 12:32:54 +000065
Greg Claytonc8c10322016-12-13 18:25:19 +000066 OS << '\n';
67 OS.indent(Indent);
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000068 R.dump(OS, AddressSize);
George Rimar6957ab52017-08-15 12:32:54 +000069
70 if (SectionNames.empty() || R.SectionIndex == -1ULL)
71 continue;
72
George Rimare1c30f72017-08-15 15:54:43 +000073 StringRef Name = SectionNames[R.SectionIndex].Name;
74 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000075
George Rimare1c30f72017-08-15 15:54:43 +000076 // Print section index if name is not unique.
77 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000078 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000079 }
80}
81
Reid Klecknera0587362017-08-29 21:41:21 +000082static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000083 DWARFUnit *U, unsigned Indent,
84 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000085 DWARFContext &Ctx = U->getContext();
86 const DWARFObject &Obj = Ctx.getDWARFObj();
87 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
88 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
89 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
90 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
91 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
92 Ctx.isLittleEndian(), 0);
93 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
94 .print(OS, MRI);
95 return;
96 }
97
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000098 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +000099 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
100 const DWARFSection &LocSection = Obj.getLocSection();
101 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
102 uint32_t Offset = *FormValue.getAsSectionOffset();
103
104 if (!LocSection.Data.empty()) {
105 DWARFDebugLoc DebugLoc;
106 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
107 Obj.getAddressSize());
108 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
109 if (LL)
110 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
111 else
112 OS << "error extracting location list.";
113 } else if (!LocDWOSection.Data.empty()) {
114 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
115 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
116 if (LL)
117 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
118 else
119 OS << "error extracting location list.";
120 }
121 }
122}
123
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000124/// Dump the name encoded in the type tag.
125static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
126 StringRef TagStr = TagString(T);
127 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
128 return;
129 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
130}
131
132/// Recursively dump the DIE type name when applicable.
133static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
134 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
135
136 if (!D.isValid())
137 return;
138
139 if (const char *Name = D.getName(DINameKind::LinkageName)) {
140 OS << Name;
141 return;
142 }
143
144 // FIXME: We should have pretty printers per language. Currently we print
145 // everything as if it was C++ and fall back to the TAG type name.
146 const dwarf::Tag T = D.getTag();
147 switch (T) {
148 case DW_TAG_array_type:
149 case DW_TAG_pointer_type:
150 case DW_TAG_ptr_to_member_type:
151 case DW_TAG_reference_type:
152 case DW_TAG_rvalue_reference_type:
153 break;
154 default:
155 dumpTypeTagName(OS, T);
156 }
157
158 // Follow the DW_AT_type if possible.
159 dumpTypeName(OS, D);
160
161 switch (T) {
162 case DW_TAG_array_type:
163 OS << "[]";
164 break;
165 case DW_TAG_pointer_type:
166 OS << '*';
167 break;
168 case DW_TAG_ptr_to_member_type:
169 OS << '*';
170 break;
171 case DW_TAG_reference_type:
172 OS << '&';
173 break;
174 case DW_TAG_rvalue_reference_type:
175 OS << "&&";
176 break;
177 default:
178 break;
179 }
180}
181
Greg Claytonc8c10322016-12-13 18:25:19 +0000182static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
183 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000184 dwarf::Form Form, unsigned Indent,
185 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000186 if (!Die.isValid())
187 return;
188 const char BaseIndent[] = " ";
189 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000190 OS.indent(Indent + 2);
Greg Claytonc8c10322016-12-13 18:25:19 +0000191 auto attrString = AttributeString(Attr);
192 if (!attrString.empty())
Jonas Devlieghere69217532018-03-09 09:56:24 +0000193 WithColor(OS, HighlightColor::Attribute) << attrString;
Greg Claytonc8c10322016-12-13 18:25:19 +0000194 else
Jonas Devlieghere69217532018-03-09 09:56:24 +0000195 WithColor(OS, HighlightColor::Attribute).get()
196 << format("DW_AT_Unknown_%x", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000197
Jonas Devliegheref91dc282017-10-02 16:02:04 +0000198 if (DumpOpts.Verbose || DumpOpts.ShowForm) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000199 auto formString = FormEncodingString(Form);
200 if (!formString.empty())
201 OS << " [" << formString << ']';
202 else
203 OS << format(" [DW_FORM_Unknown_%x]", Form);
204 }
205
Greg Claytonc8c10322016-12-13 18:25:19 +0000206 DWARFUnit *U = Die.getDwarfUnit();
207 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000208
Paul Robinsone5400f82017-11-07 19:57:12 +0000209 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
210 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000211 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000212
Greg Claytonc8c10322016-12-13 18:25:19 +0000213 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000214
Greg Claytonc8c10322016-12-13 18:25:19 +0000215 StringRef Name;
216 std::string File;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000217 auto Color = HighlightColor::Enumerator;
Greg Claytonc8c10322016-12-13 18:25:19 +0000218 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000219 Color = HighlightColor::String;
Greg Claytonc8c10322016-12-13 18:25:19 +0000220 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000221 if (LT->getFileNameByIndex(
222 formValue.getAsUnsignedConstant().getValue(),
223 U->getCompilationDir(),
224 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000225 File = '"' + File + '"';
226 Name = File;
227 }
228 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
229 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000230
Greg Claytonc8c10322016-12-13 18:25:19 +0000231 if (!Name.empty())
232 WithColor(OS, Color) << Name;
233 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
234 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000235 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
236 formValue.getAsUnsignedConstant()) {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000237 if (DumpOpts.ShowAddresses) {
238 // Print the actual address rather than the offset.
239 uint64_t LowPC, HighPC, Index;
240 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
241 OS << format("0x%016" PRIx64, HighPC);
242 else
243 formValue.dump(OS, DumpOpts);
244 }
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000245 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
246 Attr == DW_AT_data_member_location ||
247 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000248 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000249 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000250 formValue.dump(OS, DumpOpts);
251
Greg Claytonc8c10322016-12-13 18:25:19 +0000252 // We have dumped the attribute raw value. For some attributes
253 // having both the raw value and the pretty-printed value is
254 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000255 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000256 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
257 DINameKind::LinkageName))
258 OS << " \"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000259 } else if (Attr == DW_AT_type) {
260 OS << " \"";
261 dumpTypeName(OS, Die);
262 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000263 } else if (Attr == DW_AT_APPLE_property_attribute) {
264 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
265 dumpApplePropertyAttribute(OS, *OptVal);
266 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000267 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
268 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
269 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000270 }
George Rimar6957ab52017-08-15 12:32:54 +0000271
Greg Claytonc8c10322016-12-13 18:25:19 +0000272 OS << ")\n";
273}
274
Adrian Prantlb4a67902017-10-04 22:26:19 +0000275bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000276
277bool DWARFDie::isSubroutineDIE() const {
278 auto Tag = getTag();
279 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
280}
281
Adrian Prantlb4a67902017-10-04 22:26:19 +0000282Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000283 if (!isValid())
284 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000285 auto AbbrevDecl = getAbbreviationDeclarationPtr();
286 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000287 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
288 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000289}
290
Greg Clayton97d22182017-01-13 21:08:18 +0000291Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000292DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
293 if (!isValid())
294 return None;
295 auto AbbrevDecl = getAbbreviationDeclarationPtr();
296 if (AbbrevDecl) {
297 for (auto Attr : Attrs) {
298 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
299 return Value;
300 }
301 }
302 return None;
303}
304
305Optional<DWARFFormValue>
306DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
307 if (!isValid())
308 return None;
Greg Claytond6b67eb2017-11-27 22:12:44 +0000309 if (auto Value = find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000310 return Value;
Greg Claytond6b67eb2017-11-27 22:12:44 +0000311 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_abstract_origin)) {
312 if (auto Value = Die.findRecursively(Attrs))
313 return Value;
314 }
315 if (auto Die = getAttributeValueAsReferencedDie(DW_AT_specification)) {
316 if (auto Value = Die.findRecursively(Attrs))
317 return Value;
318 }
Greg Claytonc109bbe2017-01-13 22:32:12 +0000319 return None;
320}
321
Greg Claytonc8c10322016-12-13 18:25:19 +0000322DWARFDie
323DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000324 if (auto SpecRef = toReference(find(Attr))) {
325 if (auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000326 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000327 }
328 return DWARFDie();
329}
330
Adrian Prantlb4a67902017-10-04 22:26:19 +0000331Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000332 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000333}
334
Greg Clayton2520c9e2016-12-19 20:36:41 +0000335Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000336 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000337 if (auto Address = FormValue->getAsAddress()) {
338 // High PC is an address.
339 return Address;
340 }
341 if (auto Offset = FormValue->getAsUnsignedConstant()) {
342 // High PC is an offset from LowPC.
343 return LowPC + *Offset;
344 }
345 }
346 return None;
347}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000348
George Rimara25d3292017-05-27 18:10:23 +0000349bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
350 uint64_t &SectionIndex) const {
351 auto F = find(DW_AT_low_pc);
352 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000353 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000354 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000355 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
356 LowPC = *LowPcAddr;
357 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000358 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000359 return true;
360 }
361 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000362}
363
Adrian Prantlb4a67902017-10-04 22:26:19 +0000364DWARFAddressRangesVector DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000365 if (isNULL())
366 return DWARFAddressRangesVector();
367 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000368 uint64_t LowPC, HighPC, Index;
369 if (getLowAndHighPC(LowPC, HighPC, Index))
370 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000371
Greg Claytonc8c10322016-12-13 18:25:19 +0000372 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000373 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000374 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000375 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000376 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000377 return RangeList.getAbsoluteRanges(U->getBaseAddress());
378 }
379 return DWARFAddressRangesVector();
380}
381
Adrian Prantlb4a67902017-10-04 22:26:19 +0000382void DWARFDie::collectChildrenAddressRanges(
383 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000384 if (isNULL())
385 return;
386 if (isSubprogramDIE()) {
387 const auto &DIERanges = getAddressRanges();
388 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
389 }
390
Adrian Prantlb4a67902017-10-04 22:26:19 +0000391 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000392 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000393}
394
395bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000396 for (const auto &R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000397 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000398 return true;
399 }
400 return false;
401}
402
Adrian Prantlb4a67902017-10-04 22:26:19 +0000403const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000404 if (!isSubroutineDIE())
405 return nullptr;
406 return getName(Kind);
407}
408
Adrian Prantlb4a67902017-10-04 22:26:19 +0000409const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000410 if (!isValid() || Kind == DINameKind::None)
411 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000412 // Try to get mangled name only if it was asked for.
413 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000414 if (auto Name = dwarf::toString(
415 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
416 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000417 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000418 }
Greg Clayton97d22182017-01-13 21:08:18 +0000419 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
420 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000421 return nullptr;
422}
423
David Blaikieefc4eba2017-02-06 20:19:02 +0000424uint64_t DWARFDie::getDeclLine() const {
425 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
426}
427
Greg Claytonc8c10322016-12-13 18:25:19 +0000428void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000429 uint32_t &CallColumn,
430 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000431 CallFile = toUnsigned(find(DW_AT_call_file), 0);
432 CallLine = toUnsigned(find(DW_AT_call_line), 0);
433 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000434 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000435}
436
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000437/// Helper to dump a DIE with all of its parents, but no siblings.
438static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
439 DIDumpOptions DumpOpts) {
440 if (!Die)
441 return Indent;
442 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000443 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000444 return Indent + 2;
445}
446
Adrian Prantld3f9f212017-09-20 17:44:00 +0000447void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000448 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000449 if (!isValid())
450 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000451 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000452 const uint32_t Offset = getOffset();
453 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000454 if (DumpOpts.ShowParents) {
455 DumpOpts.ShowParents = false;
456 Indent = dumpParentChain(getParent(), OS, Indent, DumpOpts);
457 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000458
Greg Claytonc8c10322016-12-13 18:25:19 +0000459 if (debug_info_data.isValidOffset(offset)) {
460 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000461 if (DumpOpts.ShowAddresses)
Jonas Devlieghere69217532018-03-09 09:56:24 +0000462 WithColor(OS, HighlightColor::Address).get()
463 << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000464
Greg Claytonc8c10322016-12-13 18:25:19 +0000465 if (abbrCode) {
466 auto AbbrevDecl = getAbbreviationDeclarationPtr();
467 if (AbbrevDecl) {
468 auto tagString = TagString(getTag());
469 if (!tagString.empty())
Jonas Devlieghere69217532018-03-09 09:56:24 +0000470 WithColor(OS, HighlightColor::Tag).get().indent(Indent) << tagString;
Greg Claytonc8c10322016-12-13 18:25:19 +0000471 else
Jonas Devlieghere69217532018-03-09 09:56:24 +0000472 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
Adrian Prantlb4a67902017-10-04 22:26:19 +0000473 << format("DW_TAG_Unknown_%x", getTag());
Adrian Prantl318d1192017-06-06 23:28:45 +0000474
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000475 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000476 OS << format(" [%u] %c", abbrCode,
477 AbbrevDecl->hasChildren() ? '*' : ' ');
478 OS << '\n';
479
Greg Claytonc8c10322016-12-13 18:25:19 +0000480 // Dump all data in the DIE for the attributes.
481 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000482 if (AttrSpec.Form == DW_FORM_implicit_const) {
483 // We are dumping .debug_info section ,
484 // implicit_const attribute values are not really stored here,
485 // but in .debug_abbrev section. So we just skip such attrs.
486 continue;
487 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000488 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000489 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000490 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000491
Greg Claytonc8c10322016-12-13 18:25:19 +0000492 DWARFDie child = getFirstChild();
Adrian Prantl5da51f42017-11-29 01:12:22 +0000493 if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
Adrian Prantld3f9f212017-09-20 17:44:00 +0000494 DumpOpts.RecurseDepth--;
Greg Claytonc8c10322016-12-13 18:25:19 +0000495 while (child) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000496 child.dump(OS, Indent + 2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000497 child = child.getSibling();
498 }
499 }
500 } else {
501 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000502 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000503 }
504 } else {
505 OS.indent(Indent) << "NULL\n";
506 }
507 }
508}
509
Adrian Prantl3d523a62017-08-16 17:43:01 +0000510LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
511
Greg Clayton78a07bf2016-12-21 21:37:06 +0000512DWARFDie DWARFDie::getParent() const {
513 if (isValid())
514 return U->getParent(Die);
515 return DWARFDie();
516}
517
518DWARFDie DWARFDie::getSibling() const {
519 if (isValid())
520 return U->getSibling(Die);
521 return DWARFDie();
522}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000523
George Rimar0be860f2017-10-25 10:23:49 +0000524DWARFDie DWARFDie::getFirstChild() const {
525 if (isValid())
526 return U->getFirstChild(Die);
527 return DWARFDie();
528}
529
Adrian Prantlb4a67902017-10-04 22:26:19 +0000530iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000531 return make_range(attribute_iterator(*this, false),
532 attribute_iterator(*this, true));
533}
534
Adrian Prantlb4a67902017-10-04 22:26:19 +0000535DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
536 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000537 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
538 assert(AbbrDecl && "Must have abbreviation declaration");
539 if (End) {
540 // This is the end iterator so we set the index to the attribute count.
541 Index = AbbrDecl->getNumAttributes();
542 } else {
543 // This is the begin iterator so we extract the value for this->Index.
544 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
545 updateForIndex(*AbbrDecl, 0);
546 }
547}
548
549void DWARFDie::attribute_iterator::updateForIndex(
550 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
551 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000552 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000553 auto NumAttrs = AbbrDecl.getNumAttributes();
554 if (Index < NumAttrs) {
555 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
556 // Add the previous byte size of any previous attribute value.
557 AttrValue.Offset += AttrValue.ByteSize;
558 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
559 uint32_t ParseOffset = AttrValue.Offset;
560 auto U = Die.getDwarfUnit();
561 assert(U && "Die must have valid DWARF unit");
562 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000563 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000564 (void)b;
565 assert(b && "extractValue cannot fail on fully parsed DWARF");
566 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
567 } else {
568 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
569 AttrValue.clear();
570 }
571}
572
573DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
574 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
575 updateForIndex(*AbbrDecl, Index + 1);
576 return *this;
577}