[dwarfdump] Dump DW_AT_(decl|call)_line attribute values as decimal values.
[oota-llvm.git] / lib / DebugInfo / DWARFDebugInfoEntry.cpp
1 //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DWARFDebugInfoEntry.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "DWARFDebugAbbrev.h"
14 #include "llvm/DebugInfo/DWARFFormValue.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
19 using namespace llvm;
20 using namespace dwarf;
21 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
22
23 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
24                                       unsigned recurseDepth,
25                                       unsigned indent) const {
26   DataExtractor debug_info_data = u->getDebugInfoExtractor();
27   uint32_t offset = Offset;
28
29   if (debug_info_data.isValidOffset(offset)) {
30     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
31
32     OS << format("\n0x%8.8x: ", Offset);
33     if (abbrCode) {
34       if (AbbrevDecl) {
35         const char *tagString = TagString(getTag());
36         if (tagString)
37           OS.indent(indent) << tagString;
38         else
39           OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
40         OS << format(" [%u] %c\n", abbrCode,
41                      AbbrevDecl->hasChildren() ? '*' : ' ');
42
43         // Dump all data in the DIE for the attributes.
44         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
45           dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
46         }
47
48         const DWARFDebugInfoEntryMinimal *child = getFirstChild();
49         if (recurseDepth > 0 && child) {
50           while (child) {
51             child->dump(OS, u, recurseDepth-1, indent+2);
52             child = child->getSibling();
53           }
54         }
55       } else {
56         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
57            << abbrCode << '\n';
58       }
59     } else {
60       OS.indent(indent) << "NULL\n";
61     }
62   }
63 }
64
65 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
66                                                const DWARFUnit *u,
67                                                uint32_t *offset_ptr,
68                                                uint16_t attr, uint16_t form,
69                                                unsigned indent) const {
70   OS << "            ";
71   OS.indent(indent+2);
72   const char *attrString = AttributeString(attr);
73   if (attrString)
74     OS << attrString;
75   else
76     OS << format("DW_AT_Unknown_%x", attr);
77   const char *formString = FormEncodingString(form);
78   if (formString)
79     OS << " [" << formString << ']';
80   else
81     OS << format(" [DW_FORM_Unknown_%x]", form);
82
83   DWARFFormValue formValue(form);
84
85   if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
86     return;
87
88   OS << "\t(";
89   
90   const char *Name = nullptr;
91   if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
92     Name = AttributeValueString(attr, *Val);
93
94   if (Name) {
95     OS << Name;
96   } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
97     OS << *formValue.getAsUnsignedConstant();
98   } else {
99     formValue.dump(OS, u);
100   }
101
102   OS << ")\n";
103 }
104
105 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
106                                              uint32_t *OffsetPtr) {
107   Offset = *OffsetPtr;
108   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
109   uint32_t UEndOffset = U->getNextUnitOffset();
110   if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
111     return false;
112   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
113   if (0 == AbbrCode) {
114     // NULL debug tag entry.
115     AbbrevDecl = nullptr;
116     return true;
117   }
118   AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
119   if (nullptr == AbbrevDecl) {
120     // Restore the original offset.
121     *OffsetPtr = Offset;
122     return false;
123   }
124   ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
125       U->getAddressByteSize(), U->getVersion());
126   assert(FixedFormSizes.size() > 0);
127
128   // Skip all data in the .debug_info for the attributes
129   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
130     uint16_t Form = AttrSpec.Form;
131
132     uint8_t FixedFormSize =
133         (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
134     if (FixedFormSize)
135       *OffsetPtr += FixedFormSize;
136     else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
137       // Restore the original offset.
138       *OffsetPtr = Offset;
139       return false;
140     }
141   }
142   return true;
143 }
144
145 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
146   return getTag() == DW_TAG_subprogram;
147 }
148
149 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
150   uint32_t Tag = getTag();
151   return Tag == DW_TAG_subprogram ||
152          Tag == DW_TAG_inlined_subroutine;
153 }
154
155 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
156     const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
157   if (!AbbrevDecl)
158     return false;
159
160   uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
161   if (AttrIdx == -1U)
162     return false;
163
164   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
165   uint32_t DebugInfoOffset = getOffset();
166
167   // Skip the abbreviation code so we are at the data for the attributes
168   DebugInfoData.getULEB128(&DebugInfoOffset);
169
170   // Skip preceding attribute values.
171   for (uint32_t i = 0; i < AttrIdx; ++i) {
172     DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
173                               DebugInfoData, &DebugInfoOffset, U);
174   }
175
176   FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
177   return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
178 }
179
180 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
181     const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
182   DWARFFormValue FormValue;
183   if (!getAttributeValue(U, Attr, FormValue))
184     return FailValue;
185   Optional<const char *> Result = FormValue.getAsCString(U);
186   return Result.hasValue() ? Result.getValue() : FailValue;
187 }
188
189 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
190     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
191   DWARFFormValue FormValue;
192   if (!getAttributeValue(U, Attr, FormValue))
193     return FailValue;
194   Optional<uint64_t> Result = FormValue.getAsAddress(U);
195   return Result.hasValue() ? Result.getValue() : FailValue;
196 }
197
198 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
199     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
200   DWARFFormValue FormValue;
201   if (!getAttributeValue(U, Attr, FormValue))
202     return FailValue;
203   Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
204   return Result.hasValue() ? Result.getValue() : FailValue;
205 }
206
207 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
208     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
209   DWARFFormValue FormValue;
210   if (!getAttributeValue(U, Attr, FormValue))
211     return FailValue;
212   Optional<uint64_t> Result = FormValue.getAsReference(U);
213   return Result.hasValue() ? Result.getValue() : FailValue;
214 }
215
216 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
217     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
218   DWARFFormValue FormValue;
219   if (!getAttributeValue(U, Attr, FormValue))
220     return FailValue;
221   Optional<uint64_t> Result = FormValue.getAsSectionOffset();
222   return Result.hasValue() ? Result.getValue() : FailValue;
223 }
224
225 uint64_t
226 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
227                                                    uint64_t FailValue) const {
228   uint64_t Result =
229       getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
230   if (Result != -1ULL)
231     return Result;
232   return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
233 }
234
235 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
236                                                  uint64_t &LowPC,
237                                                  uint64_t &HighPC) const {
238   LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
239   if (LowPC == -1ULL)
240     return false;
241   HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
242   if (HighPC == -1ULL) {
243     // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
244     // it represents function size.
245     HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
246     if (HighPC != -1ULL)
247       HighPC += LowPC;
248   }
249   return (HighPC != -1ULL);
250 }
251
252 DWARFAddressRangesVector
253 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
254   if (isNULL())
255     return DWARFAddressRangesVector();
256   // Single range specified by low/high PC.
257   uint64_t LowPC, HighPC;
258   if (getLowAndHighPC(U, LowPC, HighPC)) {
259     return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
260   }
261   // Multiple ranges from .debug_ranges section.
262   uint32_t RangesOffset =
263       getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
264   if (RangesOffset != -1U) {
265     DWARFDebugRangeList RangeList;
266     if (U->extractRangeList(RangesOffset, RangeList))
267       return RangeList.getAbsoluteRanges(U->getBaseAddress());
268   }
269   return DWARFAddressRangesVector();
270 }
271
272 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
273     const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
274   if (isNULL())
275     return;
276   if (isSubprogramDIE()) {
277     const auto &DIERanges = getAddressRanges(U);
278     Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
279   }
280
281   const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
282   while (Child) {
283     Child->collectChildrenAddressRanges(U, Ranges);
284     Child = Child->getSibling();
285   }
286 }
287
288 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
289     const DWARFUnit *U, const uint64_t Address) const {
290   for (const auto& R : getAddressRanges(U)) {
291     if (R.first <= Address && Address < R.second)
292       return true;
293   }
294   return false;
295 }
296
297 const char *
298 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
299                                               FunctionNameKind Kind) const {
300   if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
301     return nullptr;
302   // Try to get mangled name only if it was asked for.
303   if (Kind == FunctionNameKind::LinkageName) {
304     if (const char *name =
305             getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
306       return name;
307     if (const char *name =
308             getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
309       return name;
310   }
311   if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
312     return name;
313   // Try to get name from specification DIE.
314   uint32_t spec_ref =
315       getAttributeValueAsReference(U, DW_AT_specification, -1U);
316   if (spec_ref != -1U) {
317     DWARFDebugInfoEntryMinimal spec_die;
318     if (spec_die.extractFast(U, &spec_ref)) {
319       if (const char *name = spec_die.getSubroutineName(U, Kind))
320         return name;
321     }
322   }
323   // Try to get name from abstract origin DIE.
324   uint32_t abs_origin_ref =
325       getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
326   if (abs_origin_ref != -1U) {
327     DWARFDebugInfoEntryMinimal abs_origin_die;
328     if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
329       if (const char *name = abs_origin_die.getSubroutineName(U, Kind))
330         return name;
331     }
332   }
333   return nullptr;
334 }
335
336 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
337                                                 uint32_t &CallFile,
338                                                 uint32_t &CallLine,
339                                                 uint32_t &CallColumn) const {
340   CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
341   CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
342   CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
343 }
344
345 DWARFDebugInfoEntryInlinedChain
346 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
347     const DWARFUnit *U, const uint64_t Address) const {
348   DWARFDebugInfoEntryInlinedChain InlinedChain;
349   InlinedChain.U = U;
350   if (isNULL())
351     return InlinedChain;
352   for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
353     // Append current DIE to inlined chain only if it has correct tag
354     // (e.g. it is not a lexical block).
355     if (DIE->isSubroutineDIE()) {
356       InlinedChain.DIEs.push_back(*DIE);
357     }
358     // Try to get child which also contains provided address.
359     const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
360     while (Child) {
361       if (Child->addressRangeContainsAddress(U, Address)) {
362         // Assume there is only one such child.
363         break;
364       }
365       Child = Child->getSibling();
366     }
367     DIE = Child;
368   }
369   // Reverse the obtained chain to make the root of inlined chain last.
370   std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
371   return InlinedChain;
372 }