blob: 76430b41f1838574f2332a650521501818f46379 [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"
Alexander Kornienkoe74e0f12018-09-17 15:40:01 +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) {
Jonas Devlieghere344cac52018-10-19 17:57:53 +000062 if (!DumpOpts.ShowAddresses)
63 return;
64
George Rimare1c30f72017-08-15 15:54:43 +000065 ArrayRef<SectionName> SectionNames;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000066 if (DumpOpts.Verbose)
George Rimare1c30f72017-08-15 15:54:43 +000067 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000068
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000069 for (const DWARFAddressRange &R : Ranges) {
Greg Claytonc8c10322016-12-13 18:25:19 +000070 OS << '\n';
71 OS.indent(Indent);
Jonas Devlieghere6f24c872018-01-16 11:17:57 +000072 R.dump(OS, AddressSize);
George Rimar6957ab52017-08-15 12:32:54 +000073
74 if (SectionNames.empty() || R.SectionIndex == -1ULL)
75 continue;
76
George Rimare1c30f72017-08-15 15:54:43 +000077 StringRef Name = SectionNames[R.SectionIndex].Name;
78 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000079
George Rimare1c30f72017-08-15 15:54:43 +000080 // Print section index if name is not unique.
81 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000082 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000083 }
84}
85
Reid Klecknera0587362017-08-29 21:41:21 +000086static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000087 DWARFUnit *U, unsigned Indent,
88 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000089 DWARFContext &Ctx = U->getContext();
90 const DWARFObject &Obj = Ctx.getDWARFObj();
91 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
92 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
93 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
94 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
95 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
96 Ctx.isLittleEndian(), 0);
97 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
98 .print(OS, MRI);
99 return;
100 }
101
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000102 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +0000103 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
Reid Klecknera0587362017-08-29 21:41:21 +0000104 uint32_t Offset = *FormValue.getAsSectionOffset();
George Rimar4c7dd9c2018-10-22 11:30:54 +0000105 if (!U->isDWOUnit() && !U->getLocSection()->Data.empty()) {
Reid Klecknera0587362017-08-29 21:41:21 +0000106 DWARFDebugLoc DebugLoc;
Wolfgang Pieb6214c112018-10-19 19:23:16 +0000107 DWARFDataExtractor Data(Obj, *U->getLocSection(), Ctx.isLittleEndian(),
Reid Klecknera0587362017-08-29 21:41:21 +0000108 Obj.getAddressSize());
109 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
Jonas Devliegherec1113822018-05-21 19:36:54 +0000110 if (LL) {
111 uint64_t BaseAddr = 0;
David Blaikie161dd3c2018-10-20 06:02:15 +0000112 if (Optional<SectionedAddress> BA = U->getBaseAddress())
Jonas Devliegherec1113822018-05-21 19:36:54 +0000113 BaseAddr = BA->Address;
114 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, BaseAddr,
115 Indent);
116 } else
Reid Klecknera0587362017-08-29 21:41:21 +0000117 OS << "error extracting location list.";
George Rimar4c7dd9c2018-10-22 11:30:54 +0000118 return;
119 }
120
121 StringRef LoclistsSectionData =
122 U->isDWOUnit() ? U->getLocSectionData() : Obj.getLoclistsSection().Data;
123 if (!LoclistsSectionData.empty()) {
124 DataExtractor Data(LoclistsSectionData, Ctx.isLittleEndian(),
125 Obj.getAddressSize());
126 auto LL = DWARFDebugLoclists::parseOneLocationList(Data, &Offset);
127
128 uint64_t BaseAddr = 0;
129 if (Optional<SectionedAddress> BA = U->getBaseAddress())
130 BaseAddr = BA->Address;
131
Reid Klecknera0587362017-08-29 21:41:21 +0000132 if (LL)
George Rimar4c7dd9c2018-10-22 11:30:54 +0000133 LL->dump(OS, BaseAddr, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI,
134 Indent);
Reid Klecknera0587362017-08-29 21:41:21 +0000135 else
136 OS << "error extracting location list.";
137 }
138 }
139}
140
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000141/// Dump the name encoded in the type tag.
142static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
143 StringRef TagStr = TagString(T);
144 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
145 return;
146 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
147}
148
149/// Recursively dump the DIE type name when applicable.
150static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
151 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
152
153 if (!D.isValid())
154 return;
155
156 if (const char *Name = D.getName(DINameKind::LinkageName)) {
157 OS << Name;
158 return;
159 }
160
161 // FIXME: We should have pretty printers per language. Currently we print
162 // everything as if it was C++ and fall back to the TAG type name.
163 const dwarf::Tag T = D.getTag();
164 switch (T) {
165 case DW_TAG_array_type:
166 case DW_TAG_pointer_type:
167 case DW_TAG_ptr_to_member_type:
168 case DW_TAG_reference_type:
169 case DW_TAG_rvalue_reference_type:
170 break;
171 default:
172 dumpTypeTagName(OS, T);
173 }
174
175 // Follow the DW_AT_type if possible.
176 dumpTypeName(OS, D);
177
178 switch (T) {
179 case DW_TAG_array_type:
180 OS << "[]";
181 break;
182 case DW_TAG_pointer_type:
183 OS << '*';
184 break;
185 case DW_TAG_ptr_to_member_type:
186 OS << '*';
187 break;
188 case DW_TAG_reference_type:
189 OS << '&';
190 break;
191 case DW_TAG_rvalue_reference_type:
192 OS << "&&";
193 break;
194 default:
195 break;
196 }
197}
198
Greg Claytonc8c10322016-12-13 18:25:19 +0000199static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
200 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000201 dwarf::Form Form, unsigned Indent,
202 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000203 if (!Die.isValid())
204 return;
205 const char BaseIndent[] = " ";
206 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000207 OS.indent(Indent + 2);
Pavel Labath9025f952018-03-21 11:46:37 +0000208 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000209
Pavel Labath9025f952018-03-21 11:46:37 +0000210 if (DumpOpts.Verbose || DumpOpts.ShowForm)
211 OS << formatv(" [{0}]", Form);
Adrian Prantl318d1192017-06-06 23:28:45 +0000212
Greg Claytonc8c10322016-12-13 18:25:19 +0000213 DWARFUnit *U = Die.getDwarfUnit();
214 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000215
Paul Robinsone5400f82017-11-07 19:57:12 +0000216 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
217 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000218 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000219
Greg Claytonc8c10322016-12-13 18:25:19 +0000220 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000221
Greg Claytonc8c10322016-12-13 18:25:19 +0000222 StringRef Name;
223 std::string File;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000224 auto Color = HighlightColor::Enumerator;
Greg Claytonc8c10322016-12-13 18:25:19 +0000225 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000226 Color = HighlightColor::String;
Greg Claytonc8c10322016-12-13 18:25:19 +0000227 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000228 if (LT->getFileNameByIndex(
229 formValue.getAsUnsignedConstant().getValue(),
230 U->getCompilationDir(),
231 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000232 File = '"' + File + '"';
233 Name = File;
234 }
235 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
236 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000237
Greg Claytonc8c10322016-12-13 18:25:19 +0000238 if (!Name.empty())
239 WithColor(OS, Color) << Name;
240 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
241 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000242 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
243 formValue.getAsUnsignedConstant()) {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000244 if (DumpOpts.ShowAddresses) {
245 // Print the actual address rather than the offset.
246 uint64_t LowPC, HighPC, Index;
247 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
248 OS << format("0x%016" PRIx64, HighPC);
249 else
250 formValue.dump(OS, DumpOpts);
251 }
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000252 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
253 Attr == DW_AT_data_member_location ||
254 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000255 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000256 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000257 formValue.dump(OS, DumpOpts);
258
Jonas Devlieghere88145232018-09-04 16:21:37 +0000259 std::string Space = DumpOpts.ShowAddresses ? " " : "";
260
Greg Claytonc8c10322016-12-13 18:25:19 +0000261 // We have dumped the attribute raw value. For some attributes
262 // having both the raw value and the pretty-printed value is
263 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000264 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000265 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
266 DINameKind::LinkageName))
Jonas Devlieghere88145232018-09-04 16:21:37 +0000267 OS << Space << "\"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000268 } else if (Attr == DW_AT_type) {
Jonas Devlieghere88145232018-09-04 16:21:37 +0000269 OS << Space << "\"";
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000270 dumpTypeName(OS, Die);
271 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000272 } else if (Attr == DW_AT_APPLE_property_attribute) {
273 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
274 dumpApplePropertyAttribute(OS, *OptVal);
275 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000276 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
Wolfgang Piebad605592018-05-18 20:12:54 +0000277 // For DW_FORM_rnglistx we need to dump the offset separately, since
278 // we have only dumped the index so far.
279 Optional<DWARFFormValue> Value = Die.find(DW_AT_ranges);
280 if (Value && Value->getForm() == DW_FORM_rnglistx)
281 if (auto RangeListOffset =
282 U->getRnglistOffset(*Value->getAsSectionOffset())) {
283 DWARFFormValue FV(dwarf::DW_FORM_sec_offset);
284 FV.setUValue(*RangeListOffset);
285 FV.dump(OS, DumpOpts);
286 }
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000287 if (auto RangesOrError = Die.getAddressRanges())
288 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
289 sizeof(BaseIndent) + Indent + 4, DumpOpts);
290 else
291 WithColor::error() << "decoding address ranges: "
292 << toString(RangesOrError.takeError()) << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000293 }
George Rimar6957ab52017-08-15 12:32:54 +0000294
Greg Claytonc8c10322016-12-13 18:25:19 +0000295 OS << ")\n";
296}
297
Adrian Prantlb4a67902017-10-04 22:26:19 +0000298bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000299
300bool DWARFDie::isSubroutineDIE() const {
301 auto Tag = getTag();
302 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
303}
304
Adrian Prantlb4a67902017-10-04 22:26:19 +0000305Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000306 if (!isValid())
307 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000308 auto AbbrevDecl = getAbbreviationDeclarationPtr();
309 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000310 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
311 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000312}
313
Greg Clayton97d22182017-01-13 21:08:18 +0000314Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000315DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
316 if (!isValid())
317 return None;
318 auto AbbrevDecl = getAbbreviationDeclarationPtr();
319 if (AbbrevDecl) {
320 for (auto Attr : Attrs) {
321 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
322 return Value;
323 }
324 }
325 return None;
326}
327
328Optional<DWARFFormValue>
329DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000330 std::vector<DWARFDie> Worklist;
331 Worklist.push_back(*this);
332
333 // Keep track if DIEs already seen to prevent infinite recursion.
334 // Empirically we rarely see a depth of more than 3 when dealing with valid
335 // DWARF. This corresponds to following the DW_AT_abstract_origin and
336 // DW_AT_specification just once.
337 SmallSet<DWARFDie, 3> Seen;
338
339 while (!Worklist.empty()) {
340 DWARFDie Die = Worklist.back();
341 Worklist.pop_back();
342
343 if (!Die.isValid())
344 continue;
345
346 if (Seen.count(Die))
347 continue;
348
349 Seen.insert(Die);
350
351 if (auto Value = Die.find(Attrs))
Greg Claytond6b67eb2017-11-27 22:12:44 +0000352 return Value;
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000353
354 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
355 Worklist.push_back(D);
356
357 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
358 Worklist.push_back(D);
Greg Claytond6b67eb2017-11-27 22:12:44 +0000359 }
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000360
Greg Claytonc109bbe2017-01-13 22:32:12 +0000361 return None;
362}
363
Greg Claytonc8c10322016-12-13 18:25:19 +0000364DWARFDie
365DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000366 if (auto SpecRef = toReference(find(Attr))) {
Paul Robinson11307fa2018-08-01 20:49:44 +0000367 if (auto SpecUnit = U->getUnitVector().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000368 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000369 }
370 return DWARFDie();
371}
372
Adrian Prantlb4a67902017-10-04 22:26:19 +0000373Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000374 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000375}
376
Greg Clayton2520c9e2016-12-19 20:36:41 +0000377Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000378 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000379 if (auto Address = FormValue->getAsAddress()) {
380 // High PC is an address.
381 return Address;
382 }
383 if (auto Offset = FormValue->getAsUnsignedConstant()) {
384 // High PC is an offset from LowPC.
385 return LowPC + *Offset;
386 }
387 }
388 return None;
389}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000390
George Rimara25d3292017-05-27 18:10:23 +0000391bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
392 uint64_t &SectionIndex) const {
393 auto F = find(DW_AT_low_pc);
394 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000395 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000396 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000397 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
398 LowPC = *LowPcAddr;
399 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000400 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000401 return true;
402 }
403 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000404}
405
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000406Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000407 if (isNULL())
408 return DWARFAddressRangesVector();
409 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000410 uint64_t LowPC, HighPC, Index;
411 if (getLowAndHighPC(LowPC, HighPC, Index))
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000412 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000413
Wolfgang Piebad605592018-05-18 20:12:54 +0000414 Optional<DWARFFormValue> Value = find(DW_AT_ranges);
415 if (Value) {
416 if (Value->getForm() == DW_FORM_rnglistx)
417 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
418 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
Greg Claytonc8c10322016-12-13 18:25:19 +0000419 }
420 return DWARFAddressRangesVector();
421}
422
Adrian Prantlb4a67902017-10-04 22:26:19 +0000423void DWARFDie::collectChildrenAddressRanges(
424 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000425 if (isNULL())
426 return;
427 if (isSubprogramDIE()) {
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000428 if (auto DIERangesOrError = getAddressRanges())
429 Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
430 DIERangesOrError.get().end());
431 else
432 llvm::consumeError(DIERangesOrError.takeError());
Greg Claytonc8c10322016-12-13 18:25:19 +0000433 }
434
Adrian Prantlb4a67902017-10-04 22:26:19 +0000435 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000436 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000437}
438
439bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000440 auto RangesOrError = getAddressRanges();
441 if (!RangesOrError) {
442 llvm::consumeError(RangesOrError.takeError());
443 return false;
444 }
445
446 for (const auto &R : RangesOrError.get())
George Rimar4671f2e2017-05-16 12:30:59 +0000447 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000448 return true;
Greg Claytonc8c10322016-12-13 18:25:19 +0000449 return false;
450}
451
Adrian Prantlb4a67902017-10-04 22:26:19 +0000452const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000453 if (!isSubroutineDIE())
454 return nullptr;
455 return getName(Kind);
456}
457
Adrian Prantlb4a67902017-10-04 22:26:19 +0000458const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000459 if (!isValid() || Kind == DINameKind::None)
460 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000461 // Try to get mangled name only if it was asked for.
462 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000463 if (auto Name = dwarf::toString(
464 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
465 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000466 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000467 }
Greg Clayton97d22182017-01-13 21:08:18 +0000468 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
469 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000470 return nullptr;
471}
472
David Blaikieefc4eba2017-02-06 20:19:02 +0000473uint64_t DWARFDie::getDeclLine() const {
474 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
475}
476
Greg Claytonc8c10322016-12-13 18:25:19 +0000477void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000478 uint32_t &CallColumn,
479 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000480 CallFile = toUnsigned(find(DW_AT_call_file), 0);
481 CallLine = toUnsigned(find(DW_AT_call_line), 0);
482 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000483 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000484}
485
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000486/// Helper to dump a DIE with all of its parents, but no siblings.
487static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
488 DIDumpOptions DumpOpts) {
489 if (!Die)
490 return Indent;
491 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000492 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000493 return Indent + 2;
494}
495
Adrian Prantld3f9f212017-09-20 17:44:00 +0000496void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000497 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000498 if (!isValid())
499 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000500 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000501 const uint32_t Offset = getOffset();
502 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000503 if (DumpOpts.ShowParents) {
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000504 DIDumpOptions ParentDumpOpts = DumpOpts;
505 ParentDumpOpts.ShowParents = false;
506 ParentDumpOpts.ShowChildren = false;
507 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000508 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000509
Greg Claytonc8c10322016-12-13 18:25:19 +0000510 if (debug_info_data.isValidOffset(offset)) {
511 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000512 if (DumpOpts.ShowAddresses)
Jonas Devlieghere69217532018-03-09 09:56:24 +0000513 WithColor(OS, HighlightColor::Address).get()
514 << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000515
Greg Claytonc8c10322016-12-13 18:25:19 +0000516 if (abbrCode) {
517 auto AbbrevDecl = getAbbreviationDeclarationPtr();
518 if (AbbrevDecl) {
Pavel Labath9025f952018-03-21 11:46:37 +0000519 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
520 << formatv("{0}", getTag());
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000521 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000522 OS << format(" [%u] %c", abbrCode,
523 AbbrevDecl->hasChildren() ? '*' : ' ');
524 OS << '\n';
525
Greg Claytonc8c10322016-12-13 18:25:19 +0000526 // Dump all data in the DIE for the attributes.
527 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000528 if (AttrSpec.Form == DW_FORM_implicit_const) {
529 // We are dumping .debug_info section ,
530 // implicit_const attribute values are not really stored here,
531 // but in .debug_abbrev section. So we just skip such attrs.
532 continue;
533 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000534 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000535 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000536 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000537
Greg Claytonc8c10322016-12-13 18:25:19 +0000538 DWARFDie child = getFirstChild();
Adrian Prantl5da51f42017-11-29 01:12:22 +0000539 if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
Adrian Prantld3f9f212017-09-20 17:44:00 +0000540 DumpOpts.RecurseDepth--;
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000541 DIDumpOptions ChildDumpOpts = DumpOpts;
542 ChildDumpOpts.ShowParents = false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000543 while (child) {
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000544 child.dump(OS, Indent + 2, ChildDumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000545 child = child.getSibling();
546 }
547 }
548 } else {
549 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000550 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000551 }
552 } else {
553 OS.indent(Indent) << "NULL\n";
554 }
555 }
556}
557
Adrian Prantl3d523a62017-08-16 17:43:01 +0000558LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
559
Greg Clayton78a07bf2016-12-21 21:37:06 +0000560DWARFDie DWARFDie::getParent() const {
561 if (isValid())
562 return U->getParent(Die);
563 return DWARFDie();
564}
565
566DWARFDie DWARFDie::getSibling() const {
567 if (isValid())
568 return U->getSibling(Die);
569 return DWARFDie();
570}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000571
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000572DWARFDie DWARFDie::getPreviousSibling() const {
573 if (isValid())
574 return U->getPreviousSibling(Die);
575 return DWARFDie();
576}
577
George Rimar0be860f2017-10-25 10:23:49 +0000578DWARFDie DWARFDie::getFirstChild() const {
579 if (isValid())
580 return U->getFirstChild(Die);
581 return DWARFDie();
582}
583
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000584DWARFDie DWARFDie::getLastChild() const {
585 if (isValid())
586 return U->getLastChild(Die);
587 return DWARFDie();
588}
589
Adrian Prantlb4a67902017-10-04 22:26:19 +0000590iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000591 return make_range(attribute_iterator(*this, false),
592 attribute_iterator(*this, true));
593}
594
Adrian Prantlb4a67902017-10-04 22:26:19 +0000595DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
596 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000597 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
598 assert(AbbrDecl && "Must have abbreviation declaration");
599 if (End) {
600 // This is the end iterator so we set the index to the attribute count.
601 Index = AbbrDecl->getNumAttributes();
602 } else {
603 // This is the begin iterator so we extract the value for this->Index.
604 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
605 updateForIndex(*AbbrDecl, 0);
606 }
607}
608
609void DWARFDie::attribute_iterator::updateForIndex(
610 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
611 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000612 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000613 auto NumAttrs = AbbrDecl.getNumAttributes();
614 if (Index < NumAttrs) {
615 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
616 // Add the previous byte size of any previous attribute value.
617 AttrValue.Offset += AttrValue.ByteSize;
618 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
619 uint32_t ParseOffset = AttrValue.Offset;
620 auto U = Die.getDwarfUnit();
621 assert(U && "Die must have valid DWARF unit");
622 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000623 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000624 (void)b;
625 assert(b && "extractValue cannot fail on fully parsed DWARF");
626 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
627 } else {
628 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
629 AttrValue.clear();
630 }
631}
632
633DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
634 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
635 updateForIndex(*AbbrDecl, Index + 1);
636 return *this;
637}