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