blob: 33cb00dec9e6eaeb57ade5c0c368d92a504c6db9 [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"
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +000013#include "llvm/ADT/SmallSet.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000014#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000017#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000018#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Reid Klecknera0587362017-08-29 21:41:21 +000019#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000021#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000022#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000024#include "llvm/Support/Format.h"
Pavel Labath9025f952018-03-21 11:46:37 +000025#include "llvm/Support/FormatVariadic.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000026#include "llvm/Support/MathExtras.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000027#include "llvm/Support/WithColor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000028#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000029#include <algorithm>
30#include <cassert>
31#include <cinttypes>
32#include <cstdint>
33#include <string>
34#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000035
36using namespace llvm;
37using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000038using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000039
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000040static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000041 OS << " (";
42 do {
43 uint64_t Shift = countTrailingZeros(Val);
44 assert(Shift < 64 && "undefined behavior");
45 uint64_t Bit = 1ULL << Shift;
46 auto PropName = ApplePropertyString(Bit);
47 if (!PropName.empty())
48 OS << PropName;
49 else
50 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
51 if (!(Val ^= Bit))
52 break;
53 OS << ", ";
54 } while (true);
55 OS << ")";
56}
57
George Rimar6957ab52017-08-15 12:32:54 +000058static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
59 const DWARFAddressRangesVector &Ranges,
60 unsigned AddressSize, unsigned Indent,
61 const DIDumpOptions &DumpOpts) {
George Rimare1c30f72017-08-15 15:54:43 +000062 ArrayRef<SectionName> SectionNames;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000063 if (DumpOpts.Verbose)
George Rimare1c30f72017-08-15 15:54:43 +000064 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000065
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000066 for (const DWARFAddressRange &R : Ranges) {
George Rimar6957ab52017-08-15 12:32:54 +000067
Greg Claytonc8c10322016-12-13 18:25:19 +000068 OS << '\n';
69 OS.indent(Indent);
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000070 R.dump(OS, AddressSize);
George Rimar6957ab52017-08-15 12:32:54 +000071
72 if (SectionNames.empty() || R.SectionIndex == -1ULL)
73 continue;
74
George Rimare1c30f72017-08-15 15:54:43 +000075 StringRef Name = SectionNames[R.SectionIndex].Name;
76 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000077
George Rimare1c30f72017-08-15 15:54:43 +000078 // Print section index if name is not unique.
79 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000080 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000081 }
82}
83
Reid Klecknera0587362017-08-29 21:41:21 +000084static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000085 DWARFUnit *U, unsigned Indent,
86 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000087 DWARFContext &Ctx = U->getContext();
88 const DWARFObject &Obj = Ctx.getDWARFObj();
89 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
90 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
91 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
92 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
93 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
94 Ctx.isLittleEndian(), 0);
95 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
96 .print(OS, MRI);
97 return;
98 }
99
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000100 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +0000101 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
102 const DWARFSection &LocSection = Obj.getLocSection();
103 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
104 uint32_t Offset = *FormValue.getAsSectionOffset();
105
106 if (!LocSection.Data.empty()) {
107 DWARFDebugLoc DebugLoc;
108 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
109 Obj.getAddressSize());
110 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
111 if (LL)
112 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
113 else
114 OS << "error extracting location list.";
115 } else if (!LocDWOSection.Data.empty()) {
116 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
117 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
118 if (LL)
119 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
120 else
121 OS << "error extracting location list.";
122 }
123 }
124}
125
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000126/// Dump the name encoded in the type tag.
127static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
128 StringRef TagStr = TagString(T);
129 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
130 return;
131 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
132}
133
134/// Recursively dump the DIE type name when applicable.
135static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
136 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
137
138 if (!D.isValid())
139 return;
140
141 if (const char *Name = D.getName(DINameKind::LinkageName)) {
142 OS << Name;
143 return;
144 }
145
146 // FIXME: We should have pretty printers per language. Currently we print
147 // everything as if it was C++ and fall back to the TAG type name.
148 const dwarf::Tag T = D.getTag();
149 switch (T) {
150 case DW_TAG_array_type:
151 case DW_TAG_pointer_type:
152 case DW_TAG_ptr_to_member_type:
153 case DW_TAG_reference_type:
154 case DW_TAG_rvalue_reference_type:
155 break;
156 default:
157 dumpTypeTagName(OS, T);
158 }
159
160 // Follow the DW_AT_type if possible.
161 dumpTypeName(OS, D);
162
163 switch (T) {
164 case DW_TAG_array_type:
165 OS << "[]";
166 break;
167 case DW_TAG_pointer_type:
168 OS << '*';
169 break;
170 case DW_TAG_ptr_to_member_type:
171 OS << '*';
172 break;
173 case DW_TAG_reference_type:
174 OS << '&';
175 break;
176 case DW_TAG_rvalue_reference_type:
177 OS << "&&";
178 break;
179 default:
180 break;
181 }
182}
183
Greg Claytonc8c10322016-12-13 18:25:19 +0000184static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
185 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000186 dwarf::Form Form, unsigned Indent,
187 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000188 if (!Die.isValid())
189 return;
190 const char BaseIndent[] = " ";
191 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000192 OS.indent(Indent + 2);
Pavel Labath9025f952018-03-21 11:46:37 +0000193 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000194
Pavel Labath9025f952018-03-21 11:46:37 +0000195 if (DumpOpts.Verbose || DumpOpts.ShowForm)
196 OS << formatv(" [{0}]", Form);
Adrian Prantl318d1192017-06-06 23:28:45 +0000197
Greg Claytonc8c10322016-12-13 18:25:19 +0000198 DWARFUnit *U = Die.getDwarfUnit();
199 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000200
Paul Robinsone5400f82017-11-07 19:57:12 +0000201 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
202 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000203 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000204
Greg Claytonc8c10322016-12-13 18:25:19 +0000205 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000206
Greg Claytonc8c10322016-12-13 18:25:19 +0000207 StringRef Name;
208 std::string File;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000209 auto Color = HighlightColor::Enumerator;
Greg Claytonc8c10322016-12-13 18:25:19 +0000210 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000211 Color = HighlightColor::String;
Greg Claytonc8c10322016-12-13 18:25:19 +0000212 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000213 if (LT->getFileNameByIndex(
214 formValue.getAsUnsignedConstant().getValue(),
215 U->getCompilationDir(),
216 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000217 File = '"' + File + '"';
218 Name = File;
219 }
220 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
221 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000222
Greg Claytonc8c10322016-12-13 18:25:19 +0000223 if (!Name.empty())
224 WithColor(OS, Color) << Name;
225 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
226 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000227 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
228 formValue.getAsUnsignedConstant()) {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000229 if (DumpOpts.ShowAddresses) {
230 // Print the actual address rather than the offset.
231 uint64_t LowPC, HighPC, Index;
232 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
233 OS << format("0x%016" PRIx64, HighPC);
234 else
235 formValue.dump(OS, DumpOpts);
236 }
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000237 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
238 Attr == DW_AT_data_member_location ||
239 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000240 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000241 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000242 formValue.dump(OS, DumpOpts);
243
Greg Claytonc8c10322016-12-13 18:25:19 +0000244 // We have dumped the attribute raw value. For some attributes
245 // having both the raw value and the pretty-printed value is
246 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000247 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000248 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
249 DINameKind::LinkageName))
250 OS << " \"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000251 } else if (Attr == DW_AT_type) {
252 OS << " \"";
253 dumpTypeName(OS, Die);
254 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000255 } else if (Attr == DW_AT_APPLE_property_attribute) {
256 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
257 dumpApplePropertyAttribute(OS, *OptVal);
258 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000259 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
Wolfgang Piebad605592018-05-18 20:12:54 +0000260 // For DW_FORM_rnglistx we need to dump the offset separately, since
261 // we have only dumped the index so far.
262 Optional<DWARFFormValue> Value = Die.find(DW_AT_ranges);
263 if (Value && Value->getForm() == DW_FORM_rnglistx)
264 if (auto RangeListOffset =
265 U->getRnglistOffset(*Value->getAsSectionOffset())) {
266 DWARFFormValue FV(dwarf::DW_FORM_sec_offset);
267 FV.setUValue(*RangeListOffset);
268 FV.dump(OS, DumpOpts);
269 }
George Rimar6957ab52017-08-15 12:32:54 +0000270 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
271 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000272 }
George Rimar6957ab52017-08-15 12:32:54 +0000273
Greg Claytonc8c10322016-12-13 18:25:19 +0000274 OS << ")\n";
275}
276
Adrian Prantlb4a67902017-10-04 22:26:19 +0000277bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000278
279bool DWARFDie::isSubroutineDIE() const {
280 auto Tag = getTag();
281 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
282}
283
Adrian Prantlb4a67902017-10-04 22:26:19 +0000284Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000285 if (!isValid())
286 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000287 auto AbbrevDecl = getAbbreviationDeclarationPtr();
288 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000289 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
290 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000291}
292
Greg Clayton97d22182017-01-13 21:08:18 +0000293Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000294DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
295 if (!isValid())
296 return None;
297 auto AbbrevDecl = getAbbreviationDeclarationPtr();
298 if (AbbrevDecl) {
299 for (auto Attr : Attrs) {
300 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
301 return Value;
302 }
303 }
304 return None;
305}
306
307Optional<DWARFFormValue>
308DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000309 std::vector<DWARFDie> Worklist;
310 Worklist.push_back(*this);
311
312 // Keep track if DIEs already seen to prevent infinite recursion.
313 // Empirically we rarely see a depth of more than 3 when dealing with valid
314 // DWARF. This corresponds to following the DW_AT_abstract_origin and
315 // DW_AT_specification just once.
316 SmallSet<DWARFDie, 3> Seen;
317
318 while (!Worklist.empty()) {
319 DWARFDie Die = Worklist.back();
320 Worklist.pop_back();
321
322 if (!Die.isValid())
323 continue;
324
325 if (Seen.count(Die))
326 continue;
327
328 Seen.insert(Die);
329
330 if (auto Value = Die.find(Attrs))
Greg Claytond6b67eb2017-11-27 22:12:44 +0000331 return Value;
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000332
333 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
334 Worklist.push_back(D);
335
336 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
337 Worklist.push_back(D);
Greg Claytond6b67eb2017-11-27 22:12:44 +0000338 }
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000339
Greg Claytonc109bbe2017-01-13 22:32:12 +0000340 return None;
341}
342
Greg Claytonc8c10322016-12-13 18:25:19 +0000343DWARFDie
344DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000345 if (auto SpecRef = toReference(find(Attr))) {
346 if (auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000347 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000348 }
349 return DWARFDie();
350}
351
Adrian Prantlb4a67902017-10-04 22:26:19 +0000352Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000353 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000354}
355
Greg Clayton2520c9e2016-12-19 20:36:41 +0000356Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000357 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000358 if (auto Address = FormValue->getAsAddress()) {
359 // High PC is an address.
360 return Address;
361 }
362 if (auto Offset = FormValue->getAsUnsignedConstant()) {
363 // High PC is an offset from LowPC.
364 return LowPC + *Offset;
365 }
366 }
367 return None;
368}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000369
George Rimara25d3292017-05-27 18:10:23 +0000370bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
371 uint64_t &SectionIndex) const {
372 auto F = find(DW_AT_low_pc);
373 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000374 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000375 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000376 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
377 LowPC = *LowPcAddr;
378 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000379 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000380 return true;
381 }
382 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000383}
384
Adrian Prantlb4a67902017-10-04 22:26:19 +0000385DWARFAddressRangesVector DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000386 if (isNULL())
387 return DWARFAddressRangesVector();
388 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000389 uint64_t LowPC, HighPC, Index;
390 if (getLowAndHighPC(LowPC, HighPC, Index))
391 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000392
Wolfgang Piebad605592018-05-18 20:12:54 +0000393 Optional<DWARFFormValue> Value = find(DW_AT_ranges);
394 if (Value) {
395 if (Value->getForm() == DW_FORM_rnglistx)
396 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
397 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
Greg Claytonc8c10322016-12-13 18:25:19 +0000398 }
399 return DWARFAddressRangesVector();
400}
401
Adrian Prantlb4a67902017-10-04 22:26:19 +0000402void DWARFDie::collectChildrenAddressRanges(
403 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000404 if (isNULL())
405 return;
406 if (isSubprogramDIE()) {
407 const auto &DIERanges = getAddressRanges();
408 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
409 }
410
Adrian Prantlb4a67902017-10-04 22:26:19 +0000411 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000412 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000413}
414
415bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000416 for (const auto &R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000417 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000418 return true;
419 }
420 return false;
421}
422
Adrian Prantlb4a67902017-10-04 22:26:19 +0000423const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000424 if (!isSubroutineDIE())
425 return nullptr;
426 return getName(Kind);
427}
428
Adrian Prantlb4a67902017-10-04 22:26:19 +0000429const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000430 if (!isValid() || Kind == DINameKind::None)
431 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000432 // Try to get mangled name only if it was asked for.
433 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000434 if (auto Name = dwarf::toString(
435 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
436 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000437 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000438 }
Greg Clayton97d22182017-01-13 21:08:18 +0000439 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
440 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000441 return nullptr;
442}
443
David Blaikieefc4eba2017-02-06 20:19:02 +0000444uint64_t DWARFDie::getDeclLine() const {
445 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
446}
447
Greg Claytonc8c10322016-12-13 18:25:19 +0000448void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000449 uint32_t &CallColumn,
450 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000451 CallFile = toUnsigned(find(DW_AT_call_file), 0);
452 CallLine = toUnsigned(find(DW_AT_call_line), 0);
453 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000454 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000455}
456
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000457/// Helper to dump a DIE with all of its parents, but no siblings.
458static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
459 DIDumpOptions DumpOpts) {
460 if (!Die)
461 return Indent;
462 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000463 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000464 return Indent + 2;
465}
466
Adrian Prantld3f9f212017-09-20 17:44:00 +0000467void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000468 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000469 if (!isValid())
470 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000471 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000472 const uint32_t Offset = getOffset();
473 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000474 if (DumpOpts.ShowParents) {
475 DumpOpts.ShowParents = false;
476 Indent = dumpParentChain(getParent(), OS, Indent, DumpOpts);
477 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000478
Greg Claytonc8c10322016-12-13 18:25:19 +0000479 if (debug_info_data.isValidOffset(offset)) {
480 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000481 if (DumpOpts.ShowAddresses)
Jonas Devlieghere69217532018-03-09 09:56:24 +0000482 WithColor(OS, HighlightColor::Address).get()
483 << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000484
Greg Claytonc8c10322016-12-13 18:25:19 +0000485 if (abbrCode) {
486 auto AbbrevDecl = getAbbreviationDeclarationPtr();
487 if (AbbrevDecl) {
Pavel Labath9025f952018-03-21 11:46:37 +0000488 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
489 << formatv("{0}", getTag());
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000490 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000491 OS << format(" [%u] %c", abbrCode,
492 AbbrevDecl->hasChildren() ? '*' : ' ');
493 OS << '\n';
494
Greg Claytonc8c10322016-12-13 18:25:19 +0000495 // Dump all data in the DIE for the attributes.
496 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000497 if (AttrSpec.Form == DW_FORM_implicit_const) {
498 // We are dumping .debug_info section ,
499 // implicit_const attribute values are not really stored here,
500 // but in .debug_abbrev section. So we just skip such attrs.
501 continue;
502 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000503 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000504 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000505 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000506
Greg Claytonc8c10322016-12-13 18:25:19 +0000507 DWARFDie child = getFirstChild();
Adrian Prantl5da51f42017-11-29 01:12:22 +0000508 if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
Adrian Prantld3f9f212017-09-20 17:44:00 +0000509 DumpOpts.RecurseDepth--;
Greg Claytonc8c10322016-12-13 18:25:19 +0000510 while (child) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000511 child.dump(OS, Indent + 2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000512 child = child.getSibling();
513 }
514 }
515 } else {
516 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000517 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000518 }
519 } else {
520 OS.indent(Indent) << "NULL\n";
521 }
522 }
523}
524
Adrian Prantl3d523a62017-08-16 17:43:01 +0000525LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
526
Greg Clayton78a07bf2016-12-21 21:37:06 +0000527DWARFDie DWARFDie::getParent() const {
528 if (isValid())
529 return U->getParent(Die);
530 return DWARFDie();
531}
532
533DWARFDie DWARFDie::getSibling() const {
534 if (isValid())
535 return U->getSibling(Die);
536 return DWARFDie();
537}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000538
George Rimar0be860f2017-10-25 10:23:49 +0000539DWARFDie DWARFDie::getFirstChild() const {
540 if (isValid())
541 return U->getFirstChild(Die);
542 return DWARFDie();
543}
544
Adrian Prantlb4a67902017-10-04 22:26:19 +0000545iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000546 return make_range(attribute_iterator(*this, false),
547 attribute_iterator(*this, true));
548}
549
Adrian Prantlb4a67902017-10-04 22:26:19 +0000550DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
551 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000552 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
553 assert(AbbrDecl && "Must have abbreviation declaration");
554 if (End) {
555 // This is the end iterator so we set the index to the attribute count.
556 Index = AbbrDecl->getNumAttributes();
557 } else {
558 // This is the begin iterator so we extract the value for this->Index.
559 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
560 updateForIndex(*AbbrDecl, 0);
561 }
562}
563
564void DWARFDie::attribute_iterator::updateForIndex(
565 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
566 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000567 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000568 auto NumAttrs = AbbrDecl.getNumAttributes();
569 if (Index < NumAttrs) {
570 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
571 // Add the previous byte size of any previous attribute value.
572 AttrValue.Offset += AttrValue.ByteSize;
573 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
574 uint32_t ParseOffset = AttrValue.Offset;
575 auto U = Die.getDwarfUnit();
576 assert(U && "Die must have valid DWARF unit");
577 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000578 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000579 (void)b;
580 assert(b && "extractValue cannot fail on fully parsed DWARF");
581 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
582 } else {
583 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
584 AttrValue.clear();
585 }
586}
587
588DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
589 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
590 updateForIndex(*AbbrDecl, Index + 1);
591 return *this;
592}