1 //===-- DWARFDebugInfoEntry.cpp --------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DWARFDebugInfoEntry.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "DWARFDebugAbbrev.h"
14 #include "DWARFFormValue.h"
15 #include "llvm/Support/Dwarf.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
19 using namespace dwarf;
21 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS,
22 const DWARFCompileUnit *cu,
23 unsigned recurseDepth,
24 unsigned indent) const {
25 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
26 uint32_t offset = Offset;
28 if (debug_info_data.isValidOffset(offset)) {
29 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
31 OS << format("\n0x%8.8x: ", Offset);
34 const char *tagString = TagString(getTag());
36 OS.indent(indent) << tagString;
38 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39 OS << format(" [%u] %c\n", abbrCode,
40 AbbrevDecl->hasChildren() ? '*' : ' ');
42 // Dump all data in the .debug_info 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, cu, &offset, attr, form, indent);
50 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
51 if (recurseDepth > 0 && child) {
53 child->dump(OS, cu, recurseDepth-1, indent+2);
54 child = child->getSibling();
58 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
62 OS.indent(indent) << "NULL\n";
67 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
68 const DWARFCompileUnit *cu,
72 unsigned indent) const {
73 OS << format("0x%8.8x: ", *offset_ptr);
75 const char *attrString = AttributeString(attr);
79 OS << format("DW_AT_Unknown_%x", attr);
80 const char *formString = FormEncodingString(form);
82 OS << " [" << formString << ']';
84 OS << format(" [DW_FORM_Unknown_%x]", form);
86 DWARFFormValue formValue(form);
88 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
92 formValue.dump(OS, cu);
96 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu,
97 const uint8_t *fixed_form_sizes,
98 uint32_t *offset_ptr) {
101 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
102 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
104 assert (fixed_form_sizes); // For best performance this should be specified!
107 uint32_t offset = *offset_ptr;
109 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
111 // Skip all data in the .debug_info for the attributes
112 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
115 for (i=0; i<numAttributes; ++i) {
116 form = AbbrevDecl->getFormByIndex(i);
118 const uint8_t fixed_skip_size = fixed_form_sizes[form];
120 offset += fixed_skip_size;
122 bool form_is_indirect = false;
124 form_is_indirect = false;
125 uint32_t form_size = 0;
127 // Blocks if inlined data that have a length field and the data bytes
128 // inlined in the .debug_info.
130 form_size = debug_info_data.getULEB128(&offset);
133 form_size = debug_info_data.getU8(&offset);
136 form_size = debug_info_data.getU16(&offset);
139 form_size = debug_info_data.getU32(&offset);
142 // Inlined NULL terminated C-strings
144 debug_info_data.getCStr(&offset);
147 // Compile unit address sized values
149 case DW_FORM_ref_addr:
150 form_size = cu->getAddressByteSize();
179 // signed or unsigned LEB 128 values
182 case DW_FORM_ref_udata:
183 debug_info_data.getULEB128(&offset);
186 case DW_FORM_indirect:
187 form_is_indirect = true;
188 form = debug_info_data.getULEB128(&offset);
192 *offset_ptr = Offset;
197 } while (form_is_indirect);
200 *offset_ptr = offset;
204 return true; // NULL debug tag entry
209 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
210 uint32_t *offset_ptr) {
211 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
212 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
213 const uint8_t cu_addr_size = cu->getAddressByteSize();
214 uint32_t offset = *offset_ptr;
215 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
218 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
221 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
224 uint16_t tag = AbbrevDecl->getTag();
226 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
227 if(cu && isCompileUnitTag)
228 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
230 // Skip all data in the .debug_info for the attributes
231 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
232 for (uint32_t i = 0; i != numAttributes; ++i) {
233 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
234 uint16_t form = AbbrevDecl->getFormByIndex(i);
236 if (isCompileUnitTag &&
237 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
238 DWARFFormValue form_value(form);
239 if (form_value.extractValue(debug_info_data, &offset, cu)) {
240 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
241 const_cast<DWARFCompileUnit*>(cu)
242 ->setBaseAddress(form_value.getUnsigned());
245 bool form_is_indirect = false;
247 form_is_indirect = false;
248 register uint32_t form_size = 0;
250 // Blocks if inlined data that have a length field and the data
251 // bytes // inlined in the .debug_info
253 form_size = debug_info_data.getULEB128(&offset);
256 form_size = debug_info_data.getU8(&offset);
259 form_size = debug_info_data.getU16(&offset);
262 form_size = debug_info_data.getU32(&offset);
265 // Inlined NULL terminated C-strings
267 debug_info_data.getCStr(&offset);
270 // Compile unit address sized values
272 case DW_FORM_ref_addr:
273 form_size = cu_addr_size;
305 // signed or unsigned LEB 128 values
308 case DW_FORM_ref_udata:
309 debug_info_data.getULEB128(&offset);
312 case DW_FORM_indirect:
313 form = debug_info_data.getULEB128(&offset);
314 form_is_indirect = true;
318 *offset_ptr = offset;
323 } while (form_is_indirect);
326 *offset_ptr = offset;
331 *offset_ptr = offset;
332 return true; // NULL debug tag entry
340 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
342 DWARFFormValue &form_value,
343 uint32_t *end_attr_offset_ptr)
346 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
348 if (attr_idx != -1U) {
349 uint32_t offset = getOffset();
351 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
353 // Skip the abbreviation code so we are at the data for the attributes
354 debug_info_data.getULEB128(&offset);
357 while (idx < attr_idx)
358 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
359 debug_info_data, &offset, cu);
361 const uint32_t attr_offset = offset;
362 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
363 if (form_value.extractValue(debug_info_data, &offset, cu)) {
364 if (end_attr_offset_ptr)
365 *end_attr_offset_ptr = offset;
375 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
376 const DWARFCompileUnit* cu,
378 const char* fail_value) const {
379 DWARFFormValue form_value;
380 if (getAttributeValue(cu, attr, form_value)) {
381 DataExtractor stringExtractor(cu->getContext().getStringSection(),
383 return form_value.getAsCString(&stringExtractor);
389 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
390 const DWARFCompileUnit* cu,
392 uint64_t fail_value) const {
393 DWARFFormValue form_value;
394 if (getAttributeValue(cu, attr, form_value))
395 return form_value.getUnsigned();
400 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
401 const DWARFCompileUnit* cu,
403 int64_t fail_value) const {
404 DWARFFormValue form_value;
405 if (getAttributeValue(cu, attr, form_value))
406 return form_value.getSigned();
411 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
412 const DWARFCompileUnit* cu,
414 uint64_t fail_value) const {
415 DWARFFormValue form_value;
416 if (getAttributeValue(cu, attr, form_value))
417 return form_value.getReference(cu);
422 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *cu,
423 DWARFDebugAranges *debug_aranges)
426 uint16_t tag = AbbrevDecl->getTag();
427 if (tag == DW_TAG_subprogram) {
428 uint64_t hi_pc = -1ULL;
429 uint64_t lo_pc = getAttributeValueAsUnsigned(cu, DW_AT_low_pc, -1ULL);
431 hi_pc = getAttributeValueAsUnsigned(cu, DW_AT_high_pc, -1ULL);
433 debug_aranges->appendRange(cu->getOffset(), lo_pc, hi_pc);
436 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
438 child->buildAddressRangeTable(cu, debug_aranges);
439 child = child->getSibling();