blob: 35567d0f67a010395ad7f36137f326cf1c7a8fcb [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();
Reid Klecknera0587362017-08-29 21:41:21 +0000105 if (!LocSection.Data.empty()) {
106 DWARFDebugLoc DebugLoc;
107 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
108 Obj.getAddressSize());
109 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
Jonas Devliegherec1113822018-05-21 19:36:54 +0000110 if (LL) {
111 uint64_t BaseAddr = 0;
112 if (Optional<BaseAddress> BA = U->getBaseAddress())
113 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.";
118 } else if (!LocDWOSection.Data.empty()) {
119 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
120 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
121 if (LL)
122 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
123 else
124 OS << "error extracting location list.";
125 }
126 }
127}
128
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000129/// Dump the name encoded in the type tag.
130static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
131 StringRef TagStr = TagString(T);
132 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
133 return;
134 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
135}
136
137/// Recursively dump the DIE type name when applicable.
138static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
139 DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
140
141 if (!D.isValid())
142 return;
143
144 if (const char *Name = D.getName(DINameKind::LinkageName)) {
145 OS << Name;
146 return;
147 }
148
149 // FIXME: We should have pretty printers per language. Currently we print
150 // everything as if it was C++ and fall back to the TAG type name.
151 const dwarf::Tag T = D.getTag();
152 switch (T) {
153 case DW_TAG_array_type:
154 case DW_TAG_pointer_type:
155 case DW_TAG_ptr_to_member_type:
156 case DW_TAG_reference_type:
157 case DW_TAG_rvalue_reference_type:
158 break;
159 default:
160 dumpTypeTagName(OS, T);
161 }
162
163 // Follow the DW_AT_type if possible.
164 dumpTypeName(OS, D);
165
166 switch (T) {
167 case DW_TAG_array_type:
168 OS << "[]";
169 break;
170 case DW_TAG_pointer_type:
171 OS << '*';
172 break;
173 case DW_TAG_ptr_to_member_type:
174 OS << '*';
175 break;
176 case DW_TAG_reference_type:
177 OS << '&';
178 break;
179 case DW_TAG_rvalue_reference_type:
180 OS << "&&";
181 break;
182 default:
183 break;
184 }
185}
186
Greg Claytonc8c10322016-12-13 18:25:19 +0000187static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
188 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000189 dwarf::Form Form, unsigned Indent,
190 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000191 if (!Die.isValid())
192 return;
193 const char BaseIndent[] = " ";
194 OS << BaseIndent;
Adrian Prantlb4a67902017-10-04 22:26:19 +0000195 OS.indent(Indent + 2);
Pavel Labath9025f952018-03-21 11:46:37 +0000196 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000197
Pavel Labath9025f952018-03-21 11:46:37 +0000198 if (DumpOpts.Verbose || DumpOpts.ShowForm)
199 OS << formatv(" [{0}]", Form);
Adrian Prantl318d1192017-06-06 23:28:45 +0000200
Greg Claytonc8c10322016-12-13 18:25:19 +0000201 DWARFUnit *U = Die.getDwarfUnit();
202 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000203
Paul Robinsone5400f82017-11-07 19:57:12 +0000204 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
205 U->getFormParams(), U))
Greg Claytonc8c10322016-12-13 18:25:19 +0000206 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000207
Greg Claytonc8c10322016-12-13 18:25:19 +0000208 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000209
Greg Claytonc8c10322016-12-13 18:25:19 +0000210 StringRef Name;
211 std::string File;
Jonas Devlieghere69217532018-03-09 09:56:24 +0000212 auto Color = HighlightColor::Enumerator;
Greg Claytonc8c10322016-12-13 18:25:19 +0000213 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
Jonas Devlieghere69217532018-03-09 09:56:24 +0000214 Color = HighlightColor::String;
Greg Claytonc8c10322016-12-13 18:25:19 +0000215 if (const auto *LT = U->getContext().getLineTableForUnit(U))
Adrian Prantlb4a67902017-10-04 22:26:19 +0000216 if (LT->getFileNameByIndex(
217 formValue.getAsUnsignedConstant().getValue(),
218 U->getCompilationDir(),
219 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000220 File = '"' + File + '"';
221 Name = File;
222 }
223 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
224 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000225
Greg Claytonc8c10322016-12-13 18:25:19 +0000226 if (!Name.empty())
227 WithColor(OS, Color) << Name;
228 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
229 OS << *formValue.getAsUnsignedConstant();
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000230 else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
231 formValue.getAsUnsignedConstant()) {
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000232 if (DumpOpts.ShowAddresses) {
233 // Print the actual address rather than the offset.
234 uint64_t LowPC, HighPC, Index;
235 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
236 OS << format("0x%016" PRIx64, HighPC);
237 else
238 formValue.dump(OS, DumpOpts);
239 }
Jonas Devlieghere6a9c5922017-11-27 16:40:46 +0000240 } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
241 Attr == DW_AT_data_member_location ||
242 Attr == DW_AT_GNU_call_site_value)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000243 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000244 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000245 formValue.dump(OS, DumpOpts);
246
Jonas Devlieghere88145232018-09-04 16:21:37 +0000247 std::string Space = DumpOpts.ShowAddresses ? " " : "";
248
Greg Claytonc8c10322016-12-13 18:25:19 +0000249 // We have dumped the attribute raw value. For some attributes
250 // having both the raw value and the pretty-printed value is
251 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000252 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000253 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
254 DINameKind::LinkageName))
Jonas Devlieghere88145232018-09-04 16:21:37 +0000255 OS << Space << "\"" << Name << '\"';
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000256 } else if (Attr == DW_AT_type) {
Jonas Devlieghere88145232018-09-04 16:21:37 +0000257 OS << Space << "\"";
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000258 dumpTypeName(OS, Die);
259 OS << '"';
Greg Claytonc8c10322016-12-13 18:25:19 +0000260 } else if (Attr == DW_AT_APPLE_property_attribute) {
261 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
262 dumpApplePropertyAttribute(OS, *OptVal);
263 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000264 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
Wolfgang Piebad605592018-05-18 20:12:54 +0000265 // For DW_FORM_rnglistx we need to dump the offset separately, since
266 // we have only dumped the index so far.
267 Optional<DWARFFormValue> Value = Die.find(DW_AT_ranges);
268 if (Value && Value->getForm() == DW_FORM_rnglistx)
269 if (auto RangeListOffset =
270 U->getRnglistOffset(*Value->getAsSectionOffset())) {
271 DWARFFormValue FV(dwarf::DW_FORM_sec_offset);
272 FV.setUValue(*RangeListOffset);
273 FV.dump(OS, DumpOpts);
274 }
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000275 if (auto RangesOrError = Die.getAddressRanges())
276 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
277 sizeof(BaseIndent) + Indent + 4, DumpOpts);
278 else
279 WithColor::error() << "decoding address ranges: "
280 << toString(RangesOrError.takeError()) << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000281 }
George Rimar6957ab52017-08-15 12:32:54 +0000282
Greg Claytonc8c10322016-12-13 18:25:19 +0000283 OS << ")\n";
284}
285
Adrian Prantlb4a67902017-10-04 22:26:19 +0000286bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
Greg Claytonc8c10322016-12-13 18:25:19 +0000287
288bool DWARFDie::isSubroutineDIE() const {
289 auto Tag = getTag();
290 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
291}
292
Adrian Prantlb4a67902017-10-04 22:26:19 +0000293Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000294 if (!isValid())
295 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000296 auto AbbrevDecl = getAbbreviationDeclarationPtr();
297 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000298 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
299 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000300}
301
Greg Clayton97d22182017-01-13 21:08:18 +0000302Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000303DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
304 if (!isValid())
305 return None;
306 auto AbbrevDecl = getAbbreviationDeclarationPtr();
307 if (AbbrevDecl) {
308 for (auto Attr : Attrs) {
309 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
310 return Value;
311 }
312 }
313 return None;
314}
315
316Optional<DWARFFormValue>
317DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000318 std::vector<DWARFDie> Worklist;
319 Worklist.push_back(*this);
320
321 // Keep track if DIEs already seen to prevent infinite recursion.
322 // Empirically we rarely see a depth of more than 3 when dealing with valid
323 // DWARF. This corresponds to following the DW_AT_abstract_origin and
324 // DW_AT_specification just once.
325 SmallSet<DWARFDie, 3> Seen;
326
327 while (!Worklist.empty()) {
328 DWARFDie Die = Worklist.back();
329 Worklist.pop_back();
330
331 if (!Die.isValid())
332 continue;
333
334 if (Seen.count(Die))
335 continue;
336
337 Seen.insert(Die);
338
339 if (auto Value = Die.find(Attrs))
Greg Claytond6b67eb2017-11-27 22:12:44 +0000340 return Value;
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000341
342 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
343 Worklist.push_back(D);
344
345 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
346 Worklist.push_back(D);
Greg Claytond6b67eb2017-11-27 22:12:44 +0000347 }
Jonas Devlieghere4bbcb5a2018-04-30 17:02:41 +0000348
Greg Claytonc109bbe2017-01-13 22:32:12 +0000349 return None;
350}
351
Greg Claytonc8c10322016-12-13 18:25:19 +0000352DWARFDie
353DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000354 if (auto SpecRef = toReference(find(Attr))) {
Paul Robinson11307fa2018-08-01 20:49:44 +0000355 if (auto SpecUnit = U->getUnitVector().getUnitForOffset(*SpecRef))
Greg Clayton52fe1f62016-12-14 22:38:08 +0000356 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000357 }
358 return DWARFDie();
359}
360
Adrian Prantlb4a67902017-10-04 22:26:19 +0000361Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000362 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000363}
364
Greg Clayton2520c9e2016-12-19 20:36:41 +0000365Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000366 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000367 if (auto Address = FormValue->getAsAddress()) {
368 // High PC is an address.
369 return Address;
370 }
371 if (auto Offset = FormValue->getAsUnsignedConstant()) {
372 // High PC is an offset from LowPC.
373 return LowPC + *Offset;
374 }
375 }
376 return None;
377}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000378
George Rimara25d3292017-05-27 18:10:23 +0000379bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
380 uint64_t &SectionIndex) const {
381 auto F = find(DW_AT_low_pc);
382 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000383 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000384 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000385 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
386 LowPC = *LowPcAddr;
387 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000388 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000389 return true;
390 }
391 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000392}
393
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000394Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000395 if (isNULL())
396 return DWARFAddressRangesVector();
397 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000398 uint64_t LowPC, HighPC, Index;
399 if (getLowAndHighPC(LowPC, HighPC, Index))
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000400 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000401
Wolfgang Piebad605592018-05-18 20:12:54 +0000402 Optional<DWARFFormValue> Value = find(DW_AT_ranges);
403 if (Value) {
404 if (Value->getForm() == DW_FORM_rnglistx)
405 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
406 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
Greg Claytonc8c10322016-12-13 18:25:19 +0000407 }
408 return DWARFAddressRangesVector();
409}
410
Adrian Prantlb4a67902017-10-04 22:26:19 +0000411void DWARFDie::collectChildrenAddressRanges(
412 DWARFAddressRangesVector &Ranges) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000413 if (isNULL())
414 return;
415 if (isSubprogramDIE()) {
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000416 if (auto DIERangesOrError = getAddressRanges())
417 Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
418 DIERangesOrError.get().end());
419 else
420 llvm::consumeError(DIERangesOrError.takeError());
Greg Claytonc8c10322016-12-13 18:25:19 +0000421 }
422
Adrian Prantlb4a67902017-10-04 22:26:19 +0000423 for (auto Child : children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000424 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000425}
426
427bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
Wolfgang Pieb61d8c8d2018-06-20 22:56:37 +0000428 auto RangesOrError = getAddressRanges();
429 if (!RangesOrError) {
430 llvm::consumeError(RangesOrError.takeError());
431 return false;
432 }
433
434 for (const auto &R : RangesOrError.get())
George Rimar4671f2e2017-05-16 12:30:59 +0000435 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000436 return true;
Greg Claytonc8c10322016-12-13 18:25:19 +0000437 return false;
438}
439
Adrian Prantlb4a67902017-10-04 22:26:19 +0000440const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000441 if (!isSubroutineDIE())
442 return nullptr;
443 return getName(Kind);
444}
445
Adrian Prantlb4a67902017-10-04 22:26:19 +0000446const char *DWARFDie::getName(DINameKind Kind) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000447 if (!isValid() || Kind == DINameKind::None)
448 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000449 // Try to get mangled name only if it was asked for.
450 if (Kind == DINameKind::LinkageName) {
Adrian Prantlb4a67902017-10-04 22:26:19 +0000451 if (auto Name = dwarf::toString(
452 findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
453 nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000454 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000455 }
Greg Clayton97d22182017-01-13 21:08:18 +0000456 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
457 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000458 return nullptr;
459}
460
David Blaikieefc4eba2017-02-06 20:19:02 +0000461uint64_t DWARFDie::getDeclLine() const {
462 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
463}
464
Greg Claytonc8c10322016-12-13 18:25:19 +0000465void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000466 uint32_t &CallColumn,
467 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000468 CallFile = toUnsigned(find(DW_AT_call_file), 0);
469 CallLine = toUnsigned(find(DW_AT_call_line), 0);
470 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000471 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000472}
473
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000474/// Helper to dump a DIE with all of its parents, but no siblings.
475static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
476 DIDumpOptions DumpOpts) {
477 if (!Die)
478 return Indent;
479 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000480 Die.dump(OS, Indent, DumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000481 return Indent + 2;
482}
483
Adrian Prantld3f9f212017-09-20 17:44:00 +0000484void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
Adrian Prantl318d1192017-06-06 23:28:45 +0000485 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000486 if (!isValid())
487 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000488 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000489 const uint32_t Offset = getOffset();
490 uint32_t offset = Offset;
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000491 if (DumpOpts.ShowParents) {
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000492 DIDumpOptions ParentDumpOpts = DumpOpts;
493 ParentDumpOpts.ShowParents = false;
494 ParentDumpOpts.ShowChildren = false;
495 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
Adrian Prantlc2bc7172017-09-18 21:27:44 +0000496 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000497
Greg Claytonc8c10322016-12-13 18:25:19 +0000498 if (debug_info_data.isValidOffset(offset)) {
499 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
Adrian Prantl01fb31c2017-12-08 23:32:47 +0000500 if (DumpOpts.ShowAddresses)
Jonas Devlieghere69217532018-03-09 09:56:24 +0000501 WithColor(OS, HighlightColor::Address).get()
502 << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000503
Greg Claytonc8c10322016-12-13 18:25:19 +0000504 if (abbrCode) {
505 auto AbbrevDecl = getAbbreviationDeclarationPtr();
506 if (AbbrevDecl) {
Pavel Labath9025f952018-03-21 11:46:37 +0000507 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
508 << formatv("{0}", getTag());
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000509 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000510 OS << format(" [%u] %c", abbrCode,
511 AbbrevDecl->hasChildren() ? '*' : ' ');
512 OS << '\n';
513
Greg Claytonc8c10322016-12-13 18:25:19 +0000514 // Dump all data in the DIE for the attributes.
515 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000516 if (AttrSpec.Form == DW_FORM_implicit_const) {
517 // We are dumping .debug_info section ,
518 // implicit_const attribute values are not really stored here,
519 // but in .debug_abbrev section. So we just skip such attrs.
520 continue;
521 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000522 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000523 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000524 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000525
Greg Claytonc8c10322016-12-13 18:25:19 +0000526 DWARFDie child = getFirstChild();
Adrian Prantl5da51f42017-11-29 01:12:22 +0000527 if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
Adrian Prantld3f9f212017-09-20 17:44:00 +0000528 DumpOpts.RecurseDepth--;
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000529 DIDumpOptions ChildDumpOpts = DumpOpts;
530 ChildDumpOpts.ShowParents = false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000531 while (child) {
Jonas Devliegherecb547cb2018-05-26 19:39:56 +0000532 child.dump(OS, Indent + 2, ChildDumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000533 child = child.getSibling();
534 }
535 }
536 } else {
537 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
Adrian Prantlb4a67902017-10-04 22:26:19 +0000538 << abbrCode << '\n';
Greg Claytonc8c10322016-12-13 18:25:19 +0000539 }
540 } else {
541 OS.indent(Indent) << "NULL\n";
542 }
543 }
544}
545
Adrian Prantl3d523a62017-08-16 17:43:01 +0000546LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
547
Greg Clayton78a07bf2016-12-21 21:37:06 +0000548DWARFDie DWARFDie::getParent() const {
549 if (isValid())
550 return U->getParent(Die);
551 return DWARFDie();
552}
553
554DWARFDie DWARFDie::getSibling() const {
555 if (isValid())
556 return U->getSibling(Die);
557 return DWARFDie();
558}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000559
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000560DWARFDie DWARFDie::getPreviousSibling() const {
561 if (isValid())
562 return U->getPreviousSibling(Die);
563 return DWARFDie();
564}
565
George Rimar0be860f2017-10-25 10:23:49 +0000566DWARFDie DWARFDie::getFirstChild() const {
567 if (isValid())
568 return U->getFirstChild(Die);
569 return DWARFDie();
570}
571
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000572DWARFDie DWARFDie::getLastChild() const {
573 if (isValid())
574 return U->getLastChild(Die);
575 return DWARFDie();
576}
577
Adrian Prantlb4a67902017-10-04 22:26:19 +0000578iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000579 return make_range(attribute_iterator(*this, false),
580 attribute_iterator(*this, true));
581}
582
Adrian Prantlb4a67902017-10-04 22:26:19 +0000583DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
584 : Die(D), AttrValue(0), Index(0) {
Greg Clayton0e62ee72017-01-13 00:13:42 +0000585 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
586 assert(AbbrDecl && "Must have abbreviation declaration");
587 if (End) {
588 // This is the end iterator so we set the index to the attribute count.
589 Index = AbbrDecl->getNumAttributes();
590 } else {
591 // This is the begin iterator so we extract the value for this->Index.
592 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
593 updateForIndex(*AbbrDecl, 0);
594 }
595}
596
597void DWARFDie::attribute_iterator::updateForIndex(
598 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
599 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000600 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000601 auto NumAttrs = AbbrDecl.getNumAttributes();
602 if (Index < NumAttrs) {
603 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
604 // Add the previous byte size of any previous attribute value.
605 AttrValue.Offset += AttrValue.ByteSize;
606 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
607 uint32_t ParseOffset = AttrValue.Offset;
608 auto U = Die.getDwarfUnit();
609 assert(U && "Die must have valid DWARF unit");
610 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
Paul Robinsone5400f82017-11-07 19:57:12 +0000611 &ParseOffset, U->getFormParams(), U);
Greg Clayton0e62ee72017-01-13 00:13:42 +0000612 (void)b;
613 assert(b && "extractValue cannot fail on fully parsed DWARF");
614 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
615 } else {
616 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
617 AttrValue.clear();
618 }
619}
620
621DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
622 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
623 updateForIndex(*AbbrDecl, Index + 1);
624 return *this;
625}