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