5140244dee9ee47b6b025bcec739029ba5680f4a
[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 {
97     formValue.dump(OS, u);
98   }
99
100   OS << ")\n";
101 }
102
103 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
104                                              uint32_t *OffsetPtr) {
105   Offset = *OffsetPtr;
106   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
107   uint32_t UEndOffset = U->getNextUnitOffset();
108   if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
109     return false;
110   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
111   if (0 == AbbrCode) {
112     // NULL debug tag entry.
113     AbbrevDecl = nullptr;
114     return true;
115   }
116   AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
117   if (nullptr == AbbrevDecl) {
118     // Restore the original offset.
119     *OffsetPtr = Offset;
120     return false;
121   }
122   ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
123       U->getAddressByteSize(), U->getVersion());
124   assert(FixedFormSizes.size() > 0);
125
126   // Skip all data in the .debug_info for the attributes
127   for (const auto &AttrSpec : AbbrevDecl->attributes()) {
128     uint16_t Form = AttrSpec.Form;
129
130     uint8_t FixedFormSize =
131         (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
132     if (FixedFormSize)
133       *OffsetPtr += FixedFormSize;
134     else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
135       // Restore the original offset.
136       *OffsetPtr = Offset;
137       return false;
138     }
139   }
140   return true;
141 }
142
143 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
144   return getTag() == DW_TAG_subprogram;
145 }
146
147 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
148   uint32_t Tag = getTag();
149   return Tag == DW_TAG_subprogram ||
150          Tag == DW_TAG_inlined_subroutine;
151 }
152
153 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
154     const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
155   if (!AbbrevDecl)
156     return false;
157
158   uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
159   if (AttrIdx == -1U)
160     return false;
161
162   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
163   uint32_t DebugInfoOffset = getOffset();
164
165   // Skip the abbreviation code so we are at the data for the attributes
166   DebugInfoData.getULEB128(&DebugInfoOffset);
167
168   // Skip preceding attribute values.
169   for (uint32_t i = 0; i < AttrIdx; ++i) {
170     DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
171                               DebugInfoData, &DebugInfoOffset, U);
172   }
173
174   FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
175   return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
176 }
177
178 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
179     const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
180   DWARFFormValue FormValue;
181   if (!getAttributeValue(U, Attr, FormValue))
182     return FailValue;
183   Optional<const char *> Result = FormValue.getAsCString(U);
184   return Result.hasValue() ? Result.getValue() : FailValue;
185 }
186
187 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
188     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
189   DWARFFormValue FormValue;
190   if (!getAttributeValue(U, Attr, FormValue))
191     return FailValue;
192   Optional<uint64_t> Result = FormValue.getAsAddress(U);
193   return Result.hasValue() ? Result.getValue() : FailValue;
194 }
195
196 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
197     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
198   DWARFFormValue FormValue;
199   if (!getAttributeValue(U, Attr, FormValue))
200     return FailValue;
201   Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
202   return Result.hasValue() ? Result.getValue() : FailValue;
203 }
204
205 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
206     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
207   DWARFFormValue FormValue;
208   if (!getAttributeValue(U, Attr, FormValue))
209     return FailValue;
210   Optional<uint64_t> Result = FormValue.getAsReference(U);
211   return Result.hasValue() ? Result.getValue() : FailValue;
212 }
213
214 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
215     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
216   DWARFFormValue FormValue;
217   if (!getAttributeValue(U, Attr, FormValue))
218     return FailValue;
219   Optional<uint64_t> Result = FormValue.getAsSectionOffset();
220   return Result.hasValue() ? Result.getValue() : FailValue;
221 }
222
223 uint64_t
224 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
225                                                    uint64_t FailValue) const {
226   uint64_t Result =
227       getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
228   if (Result != -1ULL)
229     return Result;
230   return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
231 }
232
233 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
234                                                  uint64_t &LowPC,
235                                                  uint64_t &HighPC) const {
236   LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
237   if (LowPC == -1ULL)
238     return false;
239   HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
240   if (HighPC == -1ULL) {
241     // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
242     // it represents function size.
243     HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
244     if (HighPC != -1ULL)
245       HighPC += LowPC;
246   }
247   return (HighPC != -1ULL);
248 }
249
250 DWARFAddressRangesVector
251 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
252   if (isNULL())
253     return DWARFAddressRangesVector();
254   // Single range specified by low/high PC.
255   uint64_t LowPC, HighPC;
256   if (getLowAndHighPC(U, LowPC, HighPC)) {
257     return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
258   }
259   // Multiple ranges from .debug_ranges section.
260   uint32_t RangesOffset =
261       getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
262   if (RangesOffset != -1U) {
263     DWARFDebugRangeList RangeList;
264     if (U->extractRangeList(RangesOffset, RangeList))
265       return RangeList.getAbsoluteRanges(U->getBaseAddress());
266   }
267   return DWARFAddressRangesVector();
268 }
269
270 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
271     const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
272   if (isNULL())
273     return;
274   if (isSubprogramDIE()) {
275     const auto &DIERanges = getAddressRanges(U);
276     Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
277   }
278
279   const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
280   while (Child) {
281     Child->collectChildrenAddressRanges(U, Ranges);
282     Child = Child->getSibling();
283   }
284 }
285
286 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
287     const DWARFUnit *U, const uint64_t Address) const {
288   for (const auto& R : getAddressRanges(U)) {
289     if (R.first <= Address && Address < R.second)
290       return true;
291   }
292   return false;
293 }
294
295 const char *
296 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
297                                               FunctionNameKind Kind) const {
298   if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
299     return nullptr;
300   // Try to get mangled name only if it was asked for.
301   if (Kind == FunctionNameKind::LinkageName) {
302     if (const char *name =
303             getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
304       return name;
305     if (const char *name =
306             getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
307       return name;
308   }
309   if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
310     return name;
311   // Try to get name from specification DIE.
312   uint32_t spec_ref =
313       getAttributeValueAsReference(U, DW_AT_specification, -1U);
314   if (spec_ref != -1U) {
315     DWARFDebugInfoEntryMinimal spec_die;
316     if (spec_die.extractFast(U, &spec_ref)) {
317       if (const char *name = spec_die.getSubroutineName(U, Kind))
318         return name;
319     }
320   }
321   // Try to get name from abstract origin DIE.
322   uint32_t abs_origin_ref =
323       getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
324   if (abs_origin_ref != -1U) {
325     DWARFDebugInfoEntryMinimal abs_origin_die;
326     if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
327       if (const char *name = abs_origin_die.getSubroutineName(U, Kind))
328         return name;
329     }
330   }
331   return nullptr;
332 }
333
334 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
335                                                 uint32_t &CallFile,
336                                                 uint32_t &CallLine,
337                                                 uint32_t &CallColumn) const {
338   CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
339   CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
340   CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
341 }
342
343 DWARFDebugInfoEntryInlinedChain
344 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
345     const DWARFUnit *U, const uint64_t Address) const {
346   DWARFDebugInfoEntryInlinedChain InlinedChain;
347   InlinedChain.U = U;
348   if (isNULL())
349     return InlinedChain;
350   for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
351     // Append current DIE to inlined chain only if it has correct tag
352     // (e.g. it is not a lexical block).
353     if (DIE->isSubroutineDIE()) {
354       InlinedChain.DIEs.push_back(*DIE);
355     }
356     // Try to get child which also contains provided address.
357     const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
358     while (Child) {
359       if (Child->addressRangeContainsAddress(U, Address)) {
360         // Assume there is only one such child.
361         break;
362       }
363       Child = Child->getSibling();
364     }
365     DIE = Child;
366   }
367   // Reverse the obtained chain to make the root of inlined chain last.
368   std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
369   return InlinedChain;
370 }