Exract most of DWARFCompileUnit into a new DWARFUnit to prepare for the coming DWARFT...
[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
22 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
23                                       unsigned recurseDepth,
24                                       unsigned indent) const {
25   DataExtractor debug_info_data = u->getDebugInfoExtractor();
26   uint32_t offset = Offset;
27
28   if (debug_info_data.isValidOffset(offset)) {
29     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
30
31     OS << format("\n0x%8.8x: ", Offset);
32     if (abbrCode) {
33       if (AbbrevDecl) {
34         const char *tagString = TagString(getTag());
35         if (tagString)
36           OS.indent(indent) << tagString;
37         else
38           OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39         OS << format(" [%u] %c\n", abbrCode,
40                      AbbrevDecl->hasChildren() ? '*' : ' ');
41
42         // Dump all data in the DIE for the attributes.
43         const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
44         for (uint32_t i = 0; i != numAttributes; ++i) {
45           uint16_t attr = AbbrevDecl->getAttrByIndex(i);
46           uint16_t form = AbbrevDecl->getFormByIndex(i);
47           dumpAttribute(OS, u, &offset, attr, form, indent);
48         }
49
50         const DWARFDebugInfoEntryMinimal *child = getFirstChild();
51         if (recurseDepth > 0 && child) {
52           while (child) {
53             child->dump(OS, u, recurseDepth-1, indent+2);
54             child = child->getSibling();
55           }
56         }
57       } else {
58         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
59            << abbrCode << '\n';
60       }
61     } else {
62       OS.indent(indent) << "NULL\n";
63     }
64   }
65 }
66
67 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
68                                                const DWARFUnit *u,
69                                                uint32_t *offset_ptr,
70                                                uint16_t attr, uint16_t form,
71                                                unsigned indent) const {
72   OS << "            ";
73   OS.indent(indent+2);
74   const char *attrString = AttributeString(attr);
75   if (attrString)
76     OS << attrString;
77   else
78     OS << format("DW_AT_Unknown_%x", attr);
79   const char *formString = FormEncodingString(form);
80   if (formString)
81     OS << " [" << formString << ']';
82   else
83     OS << format(" [DW_FORM_Unknown_%x]", form);
84
85   DWARFFormValue formValue(form);
86
87   if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
88     return;
89
90   OS << "\t(";
91   formValue.dump(OS, u);
92   OS << ")\n";
93 }
94
95 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
96                                              const uint8_t *FixedFormSizes,
97                                              uint32_t *OffsetPtr) {
98   Offset = *OffsetPtr;
99   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
100   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
101   if (0 == AbbrCode) {
102     // NULL debug tag entry.
103     AbbrevDecl = NULL;
104     return true;
105   }
106   AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
107   assert(AbbrevDecl);
108   assert(FixedFormSizes); // For best performance this should be specified!
109
110   // Skip all data in the .debug_info for the attributes
111   for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
112     uint16_t Form = AbbrevDecl->getFormByIndex(i);
113
114     // FIXME: Currently we're checking if this is less than the last
115     // entry in the fixed_form_sizes table, but this should be changed
116     // to use dynamic dispatch.
117     uint8_t FixedFormSize =
118         (Form < DW_FORM_ref_sig8) ? FixedFormSizes[Form] : 0;
119     if (FixedFormSize)
120       *OffsetPtr += FixedFormSize;
121     else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr,
122                                         U)) {
123       // Restore the original offset.
124       *OffsetPtr = Offset;
125       return false;
126     }
127   }
128   return true;
129 }
130
131 bool DWARFDebugInfoEntryMinimal::extract(const DWARFUnit *U,
132                                          uint32_t *OffsetPtr) {
133   DataExtractor DebugInfoData = U->getDebugInfoExtractor();
134   const uint32_t UEndOffset = U->getNextUnitOffset();
135   Offset = *OffsetPtr;
136   if ((Offset >= UEndOffset) || !DebugInfoData.isValidOffset(Offset))
137     return false;
138   uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
139   if (0 == AbbrCode) {
140     // NULL debug tag entry.
141     AbbrevDecl = NULL;
142     return true;
143   }
144   AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
145   if (0 == AbbrevDecl) {
146     // Restore the original offset.
147     *OffsetPtr = Offset;
148     return false;
149   }
150   bool IsCompileUnitTag = (AbbrevDecl->getTag() == DW_TAG_compile_unit);
151   if (IsCompileUnitTag)
152     const_cast<DWARFUnit *>(U)->setBaseAddress(0);
153
154   // Skip all data in the .debug_info for the attributes
155   for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
156     uint16_t Attr = AbbrevDecl->getAttrByIndex(i);
157     uint16_t Form = AbbrevDecl->getFormByIndex(i);
158
159     if (IsCompileUnitTag &&
160         ((Attr == DW_AT_entry_pc) || (Attr == DW_AT_low_pc))) {
161       DWARFFormValue FormValue(Form);
162       if (FormValue.extractValue(DebugInfoData, OffsetPtr, U)) {
163         if (Attr == DW_AT_low_pc || Attr == DW_AT_entry_pc)
164           const_cast<DWARFUnit *>(U)->setBaseAddress(FormValue.getUnsigned());
165       }
166     } else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
167       // Restore the original offset.
168       *OffsetPtr = Offset;
169       return false;
170     }
171   }
172   return true;
173 }
174
175 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
176   return getTag() == DW_TAG_subprogram;
177 }
178
179 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
180   uint32_t Tag = getTag();
181   return Tag == DW_TAG_subprogram ||
182          Tag == DW_TAG_inlined_subroutine;
183 }
184
185 uint32_t DWARFDebugInfoEntryMinimal::getAttributeValue(
186     const DWARFUnit *u, const uint16_t attr, DWARFFormValue &form_value,
187     uint32_t *end_attr_offset_ptr) const {
188   if (AbbrevDecl) {
189     uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
190
191     if (attr_idx != -1U) {
192       uint32_t offset = getOffset();
193
194       DataExtractor debug_info_data = u->getDebugInfoExtractor();
195
196       // Skip the abbreviation code so we are at the data for the attributes
197       debug_info_data.getULEB128(&offset);
198
199       uint32_t idx = 0;
200       while (idx < attr_idx)
201         DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
202                                   debug_info_data, &offset, u);
203
204       const uint32_t attr_offset = offset;
205       form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
206       if (form_value.extractValue(debug_info_data, &offset, u)) {
207         if (end_attr_offset_ptr)
208           *end_attr_offset_ptr = offset;
209         return attr_offset;
210       }
211     }
212   }
213
214   return 0;
215 }
216
217 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
218     const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
219   DWARFFormValue FormValue;
220   if (getAttributeValue(U, Attr, FormValue))
221     return FormValue.getAsCString(U);
222   return FailValue;
223 }
224
225 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
226     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
227   DWARFFormValue FormValue;
228   if (getAttributeValue(U, Attr, FormValue))
229     return FormValue.getAsAddress(U);
230   return FailValue;
231 }
232
233 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
234     const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
235   DWARFFormValue FormValue;
236   if (getAttributeValue(U, Attr, FormValue))
237     return FormValue.getUnsigned();
238   return FailValue;
239 }
240
241 int64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
242     const DWARFUnit *u, const uint16_t attr, int64_t fail_value) const {
243   DWARFFormValue form_value;
244   if (getAttributeValue(u, attr, form_value))
245       return form_value.getSigned();
246   return fail_value;
247 }
248
249 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
250     const DWARFUnit *u, const uint16_t attr, uint64_t fail_value) const {
251   DWARFFormValue form_value;
252   if (getAttributeValue(u, attr, form_value))
253       return form_value.getReference(u);
254   return fail_value;
255 }
256
257 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
258                                                  uint64_t &LowPC,
259                                                  uint64_t &HighPC) const {
260   HighPC = -1ULL;
261   LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
262   if (LowPC != -1ULL)
263     HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
264   return (HighPC != -1ULL);
265 }
266
267 void DWARFDebugInfoEntryMinimal::buildAddressRangeTable(
268     const DWARFUnit *U, DWARFDebugAranges *DebugAranges,
269     uint32_t UOffsetInAranges) const {
270   if (AbbrevDecl) {
271     if (isSubprogramDIE()) {
272       uint64_t LowPC, HighPC;
273       if (getLowAndHighPC(U, LowPC, HighPC))
274         DebugAranges->appendRange(UOffsetInAranges, LowPC, HighPC);
275       // FIXME: try to append ranges from .debug_ranges section.
276     }
277
278     const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
279     while (Child) {
280       Child->buildAddressRangeTable(U, DebugAranges, UOffsetInAranges);
281       Child = Child->getSibling();
282     }
283   }
284 }
285
286 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
287     const DWARFUnit *U, const uint64_t Address) const {
288   if (isNULL())
289     return false;
290   uint64_t LowPC, HighPC;
291   if (getLowAndHighPC(U, LowPC, HighPC))
292     return (LowPC <= Address && Address <= HighPC);
293   // Try to get address ranges from .debug_ranges section.
294   uint32_t RangesOffset = getAttributeValueAsReference(U, DW_AT_ranges, -1U);
295   if (RangesOffset != -1U) {
296     DWARFDebugRangeList RangeList;
297     if (U->extractRangeList(RangesOffset, RangeList))
298       return RangeList.containsAddress(U->getBaseAddress(), Address);
299   }
300   return false;
301 }
302
303 const char *
304 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
305   if (!isSubroutineDIE())
306     return 0;
307   // Try to get mangled name if possible.
308   if (const char *name =
309       getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, 0))
310     return name;
311   if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name, 0))
312     return name;
313   if (const char *name = getAttributeValueAsString(U, DW_AT_name, 0))
314     return name;
315   // Try to get name from specification DIE.
316   uint32_t spec_ref =
317       getAttributeValueAsReference(U, DW_AT_specification, -1U);
318   if (spec_ref != -1U) {
319     DWARFDebugInfoEntryMinimal spec_die;
320     if (spec_die.extract(U, &spec_ref)) {
321       if (const char *name = spec_die.getSubroutineName(U))
322         return name;
323     }
324   }
325   // Try to get name from abstract origin DIE.
326   uint32_t abs_origin_ref =
327       getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
328   if (abs_origin_ref != -1U) {
329     DWARFDebugInfoEntryMinimal abs_origin_die;
330     if (abs_origin_die.extract(U, &abs_origin_ref)) {
331       if (const char *name = abs_origin_die.getSubroutineName(U))
332         return name;
333     }
334   }
335   return 0;
336 }
337
338 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
339                                                 uint32_t &CallFile,
340                                                 uint32_t &CallLine,
341                                                 uint32_t &CallColumn) const {
342   CallFile = getAttributeValueAsUnsigned(U, DW_AT_call_file, 0);
343   CallLine = getAttributeValueAsUnsigned(U, DW_AT_call_line, 0);
344   CallColumn = getAttributeValueAsUnsigned(U, DW_AT_call_column, 0);
345 }
346
347 DWARFDebugInfoEntryInlinedChain
348 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
349     const DWARFUnit *U, const uint64_t Address) const {
350   DWARFDebugInfoEntryInlinedChain InlinedChain;
351   InlinedChain.U = U;
352   if (isNULL())
353     return InlinedChain;
354   for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
355     // Append current DIE to inlined chain only if it has correct tag
356     // (e.g. it is not a lexical block).
357     if (DIE->isSubroutineDIE()) {
358       InlinedChain.DIEs.push_back(*DIE);
359     }
360     // Try to get child which also contains provided address.
361     const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
362     while (Child) {
363       if (Child->addressRangeContainsAddress(U, Address)) {
364         // Assume there is only one such child.
365         break;
366       }
367       Child = Child->getSibling();
368     }
369     DIE = Child;
370   }
371   // Reverse the obtained chain to make the root of inlined chain last.
372   std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());
373   return InlinedChain;
374 }