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 uint64_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, 0, 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
211 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
212 uint32_t *offset_ptr) {
213 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
214 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
215 const uint8_t cu_addr_size = cu->getAddressByteSize();
216 uint32_t offset = *offset_ptr;
217 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
220 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
223 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
226 uint16_t tag = AbbrevDecl->getTag();
228 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
229 if(cu && isCompileUnitTag)
230 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
232 // Skip all data in the .debug_info for the attributes
233 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
234 for (uint32_t i = 0; i != numAttributes; ++i) {
235 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
236 uint16_t form = AbbrevDecl->getFormByIndex(i);
238 if (isCompileUnitTag &&
239 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
240 DWARFFormValue form_value(form);
241 if (form_value.extractValue(debug_info_data, &offset, cu)) {
242 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
243 const_cast<DWARFCompileUnit*>(cu)
244 ->setBaseAddress(form_value.getUnsigned());
247 bool form_is_indirect = false;
249 form_is_indirect = false;
250 register uint32_t form_size = 0;
252 // Blocks if inlined data that have a length field and the data
253 // bytes // inlined in the .debug_info
255 form_size = debug_info_data.getULEB128(&offset);
258 form_size = debug_info_data.getU8(&offset);
261 form_size = debug_info_data.getU16(&offset);
264 form_size = debug_info_data.getU32(&offset);
267 // Inlined NULL terminated C-strings
269 debug_info_data.getCStr(&offset);
272 // Compile unit address sized values
274 case DW_FORM_ref_addr:
275 form_size = cu_addr_size;
307 // signed or unsigned LEB 128 values
310 case DW_FORM_ref_udata:
311 debug_info_data.getULEB128(&offset);
314 case DW_FORM_indirect:
315 form = debug_info_data.getULEB128(&offset);
316 form_is_indirect = true;
320 *offset_ptr = offset;
325 } while (form_is_indirect);
328 *offset_ptr = offset;
333 *offset_ptr = offset;
334 return true; // NULL debug tag entry
342 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
344 DWARFFormValue &form_value,
345 uint32_t *end_attr_offset_ptr)
348 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
350 if (attr_idx != -1U) {
351 uint32_t offset = getOffset();
353 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
355 // Skip the abbreviation code so we are at the data for the attributes
356 debug_info_data.getULEB128(&offset);
359 while (idx < attr_idx)
360 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
361 debug_info_data, &offset, cu);
363 const uint32_t attr_offset = offset;
364 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
365 if (form_value.extractValue(debug_info_data, &offset, cu)) {
366 if (end_attr_offset_ptr)
367 *end_attr_offset_ptr = offset;
377 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
378 const DWARFCompileUnit* cu,
380 const char* fail_value) const {
381 DWARFFormValue form_value;
382 if (getAttributeValue(cu, attr, form_value)) {
383 DataExtractor stringExtractor(cu->getContext().getStringSection(),
385 return form_value.getAsCString(&stringExtractor);
391 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
392 const DWARFCompileUnit* cu,
394 uint64_t fail_value) const {
395 DWARFFormValue form_value;
396 if (getAttributeValue(cu, attr, form_value))
397 return form_value.getUnsigned();
402 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
403 const DWARFCompileUnit* cu,
405 int64_t fail_value) const {
406 DWARFFormValue form_value;
407 if (getAttributeValue(cu, attr, form_value))
408 return form_value.getSigned();
413 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
414 const DWARFCompileUnit* cu,
416 uint64_t fail_value) const {
417 DWARFFormValue form_value;
418 if (getAttributeValue(cu, attr, form_value))
419 return form_value.getReference(cu);
424 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *cu,
425 DWARFDebugAranges *debug_aranges)
428 uint16_t tag = AbbrevDecl->getTag();
429 if (tag == DW_TAG_subprogram) {
430 uint64_t hi_pc = -1ULL;
431 uint64_t lo_pc = getAttributeValueAsUnsigned(cu, DW_AT_low_pc, -1ULL);
433 hi_pc = getAttributeValueAsUnsigned(cu, DW_AT_high_pc, -1ULL);
435 debug_aranges->appendRange(cu->getOffset(), lo_pc, hi_pc);
438 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
440 child->buildAddressRangeTable(cu, debug_aranges);
441 child = child->getSibling();