Eric Christopher | bd5bc21 | 2012-08-23 00:52:51 +0000 | [diff] [blame] | 1 | //===-- DWARFDebugInfoEntry.cpp -------------------------------------------===// |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 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 "DWARFFormValue.h" |
| 15 | #include "llvm/Support/Dwarf.h" |
| 16 | #include "llvm/Support/Format.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | using namespace llvm; |
| 19 | using namespace dwarf; |
| 20 | |
| 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; |
| 27 | |
| 28 | if (debug_info_data.isValidOffset(offset)) { |
Benjamin Kramer | 80cc259 | 2011-11-05 15:35:00 +0000 | [diff] [blame] | 29 | uint32_t abbrCode = debug_info_data.getULEB128(&offset); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 30 | |
Benjamin Kramer | 0942255 | 2011-09-14 17:54:56 +0000 | [diff] [blame] | 31 | OS << format("\n0x%8.8x: ", Offset); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 32 | if (abbrCode) { |
| 33 | if (AbbrevDecl) { |
Benjamin Kramer | 75c6308 | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 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() ? '*' : ' '); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 41 | |
| 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); |
| 48 | } |
| 49 | |
| 50 | const DWARFDebugInfoEntryMinimal *child = getFirstChild(); |
| 51 | if (recurseDepth > 0 && child) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 52 | while (child) { |
Benjamin Kramer | 15ec085 | 2011-09-14 00:15:32 +0000 | [diff] [blame] | 53 | child->dump(OS, cu, recurseDepth-1, indent+2); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 54 | child = child->getSibling(); |
| 55 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 56 | } |
| 57 | } else { |
| 58 | OS << "Abbreviation code not found in 'debug_abbrev' class for code: " |
| 59 | << abbrCode << '\n'; |
| 60 | } |
| 61 | } else { |
Benjamin Kramer | 0942255 | 2011-09-14 17:54:56 +0000 | [diff] [blame] | 62 | OS.indent(indent) << "NULL\n"; |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS, |
| 68 | const DWARFCompileUnit *cu, |
| 69 | uint32_t* offset_ptr, |
| 70 | uint16_t attr, |
| 71 | uint16_t form, |
| 72 | unsigned indent) const { |
Benjamin Kramer | 0942255 | 2011-09-14 17:54:56 +0000 | [diff] [blame] | 73 | OS << format("0x%8.8x: ", *offset_ptr); |
Benjamin Kramer | 75c6308 | 2011-09-15 05:43:00 +0000 | [diff] [blame] | 74 | OS.indent(indent+2); |
| 75 | const char *attrString = AttributeString(attr); |
| 76 | if (attrString) |
| 77 | OS << attrString; |
| 78 | else |
| 79 | OS << format("DW_AT_Unknown_%x", attr); |
| 80 | const char *formString = FormEncodingString(form); |
| 81 | if (formString) |
| 82 | OS << " [" << formString << ']'; |
| 83 | else |
| 84 | OS << format(" [DW_FORM_Unknown_%x]", form); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 85 | |
| 86 | DWARFFormValue formValue(form); |
| 87 | |
| 88 | if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu)) |
| 89 | return; |
| 90 | |
| 91 | OS << "\t("; |
Benjamin Kramer | 34f864f | 2011-09-15 16:57:13 +0000 | [diff] [blame] | 92 | formValue.dump(OS, cu); |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 93 | OS << ")\n"; |
| 94 | } |
| 95 | |
| 96 | bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu, |
| 97 | const uint8_t *fixed_form_sizes, |
| 98 | uint32_t *offset_ptr) { |
| 99 | Offset = *offset_ptr; |
| 100 | |
| 101 | DataExtractor debug_info_data = cu->getDebugInfoExtractor(); |
| 102 | uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr); |
| 103 | |
Eric Christopher | 08cdb6e | 2012-08-24 01:14:21 +0000 | [diff] [blame] | 104 | assert(fixed_form_sizes); // For best performance this should be specified! |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 105 | |
| 106 | if (abbrCode) { |
| 107 | uint32_t offset = *offset_ptr; |
| 108 | |
| 109 | AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode); |
| 110 | |
| 111 | // Skip all data in the .debug_info for the attributes |
| 112 | const uint32_t numAttributes = AbbrevDecl->getNumAttributes(); |
| 113 | uint32_t i; |
| 114 | uint16_t form; |
| 115 | for (i=0; i<numAttributes; ++i) { |
| 116 | form = AbbrevDecl->getFormByIndex(i); |
| 117 | |
| 118 | const uint8_t fixed_skip_size = fixed_form_sizes[form]; |
| 119 | if (fixed_skip_size) |
| 120 | offset += fixed_skip_size; |
| 121 | else { |
| 122 | bool form_is_indirect = false; |
| 123 | do { |
| 124 | form_is_indirect = false; |
| 125 | uint32_t form_size = 0; |
| 126 | switch (form) { |
| 127 | // Blocks if inlined data that have a length field and the data bytes |
| 128 | // inlined in the .debug_info. |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 129 | case DW_FORM_exprloc: |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 130 | case DW_FORM_block: |
| 131 | form_size = debug_info_data.getULEB128(&offset); |
| 132 | break; |
| 133 | case DW_FORM_block1: |
| 134 | form_size = debug_info_data.getU8(&offset); |
| 135 | break; |
| 136 | case DW_FORM_block2: |
| 137 | form_size = debug_info_data.getU16(&offset); |
| 138 | break; |
| 139 | case DW_FORM_block4: |
| 140 | form_size = debug_info_data.getU32(&offset); |
| 141 | break; |
| 142 | |
| 143 | // Inlined NULL terminated C-strings |
| 144 | case DW_FORM_string: |
| 145 | debug_info_data.getCStr(&offset); |
| 146 | break; |
| 147 | |
| 148 | // Compile unit address sized values |
| 149 | case DW_FORM_addr: |
| 150 | case DW_FORM_ref_addr: |
| 151 | form_size = cu->getAddressByteSize(); |
| 152 | break; |
| 153 | |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 154 | // 0 sized form. |
| 155 | case DW_FORM_flag_present: |
| 156 | form_size = 0; |
| 157 | break; |
| 158 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 159 | // 1 byte values |
| 160 | case DW_FORM_data1: |
| 161 | case DW_FORM_flag: |
| 162 | case DW_FORM_ref1: |
| 163 | form_size = 1; |
| 164 | break; |
| 165 | |
| 166 | // 2 byte values |
| 167 | case DW_FORM_data2: |
| 168 | case DW_FORM_ref2: |
| 169 | form_size = 2; |
| 170 | break; |
| 171 | |
| 172 | // 4 byte values |
| 173 | case DW_FORM_strp: |
| 174 | case DW_FORM_data4: |
| 175 | case DW_FORM_ref4: |
| 176 | form_size = 4; |
| 177 | break; |
| 178 | |
| 179 | // 8 byte values |
| 180 | case DW_FORM_data8: |
| 181 | case DW_FORM_ref8: |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 182 | case DW_FORM_ref_sig8: |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 183 | form_size = 8; |
| 184 | break; |
| 185 | |
| 186 | // signed or unsigned LEB 128 values |
| 187 | case DW_FORM_sdata: |
| 188 | case DW_FORM_udata: |
| 189 | case DW_FORM_ref_udata: |
| 190 | debug_info_data.getULEB128(&offset); |
| 191 | break; |
| 192 | |
| 193 | case DW_FORM_indirect: |
| 194 | form_is_indirect = true; |
| 195 | form = debug_info_data.getULEB128(&offset); |
| 196 | break; |
| 197 | |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 198 | case DW_FORM_sec_offset: |
| 199 | if (cu->getAddressByteSize() == 4) |
| 200 | debug_info_data.getU32(offset_ptr); |
| 201 | else |
| 202 | debug_info_data.getU64(offset_ptr); |
| 203 | break; |
| 204 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 205 | default: |
| 206 | *offset_ptr = Offset; |
| 207 | return false; |
| 208 | } |
| 209 | offset += form_size; |
| 210 | |
| 211 | } while (form_is_indirect); |
| 212 | } |
| 213 | } |
| 214 | *offset_ptr = offset; |
| 215 | return true; |
| 216 | } else { |
| 217 | AbbrevDecl = NULL; |
| 218 | return true; // NULL debug tag entry |
| 219 | } |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool |
| 223 | DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu, |
| 224 | uint32_t *offset_ptr) { |
| 225 | DataExtractor debug_info_data = cu->getDebugInfoExtractor(); |
| 226 | const uint32_t cu_end_offset = cu->getNextCompileUnitOffset(); |
| 227 | const uint8_t cu_addr_size = cu->getAddressByteSize(); |
| 228 | uint32_t offset = *offset_ptr; |
| 229 | if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) { |
| 230 | Offset = offset; |
| 231 | |
| 232 | uint64_t abbrCode = debug_info_data.getULEB128(&offset); |
| 233 | |
| 234 | if (abbrCode) { |
| 235 | AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode); |
| 236 | |
| 237 | if (AbbrevDecl) { |
| 238 | uint16_t tag = AbbrevDecl->getTag(); |
| 239 | |
| 240 | bool isCompileUnitTag = tag == DW_TAG_compile_unit; |
| 241 | if(cu && isCompileUnitTag) |
| 242 | const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0); |
| 243 | |
| 244 | // Skip all data in the .debug_info for the attributes |
| 245 | const uint32_t numAttributes = AbbrevDecl->getNumAttributes(); |
| 246 | for (uint32_t i = 0; i != numAttributes; ++i) { |
| 247 | uint16_t attr = AbbrevDecl->getAttrByIndex(i); |
| 248 | uint16_t form = AbbrevDecl->getFormByIndex(i); |
| 249 | |
| 250 | if (isCompileUnitTag && |
| 251 | ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) { |
| 252 | DWARFFormValue form_value(form); |
| 253 | if (form_value.extractValue(debug_info_data, &offset, cu)) { |
| 254 | if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc) |
| 255 | const_cast<DWARFCompileUnit*>(cu) |
| 256 | ->setBaseAddress(form_value.getUnsigned()); |
| 257 | } |
| 258 | } else { |
| 259 | bool form_is_indirect = false; |
| 260 | do { |
| 261 | form_is_indirect = false; |
| 262 | register uint32_t form_size = 0; |
| 263 | switch (form) { |
| 264 | // Blocks if inlined data that have a length field and the data |
| 265 | // bytes // inlined in the .debug_info |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 266 | case DW_FORM_exprloc: |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 267 | case DW_FORM_block: |
| 268 | form_size = debug_info_data.getULEB128(&offset); |
| 269 | break; |
| 270 | case DW_FORM_block1: |
| 271 | form_size = debug_info_data.getU8(&offset); |
| 272 | break; |
| 273 | case DW_FORM_block2: |
| 274 | form_size = debug_info_data.getU16(&offset); |
| 275 | break; |
| 276 | case DW_FORM_block4: |
| 277 | form_size = debug_info_data.getU32(&offset); |
| 278 | break; |
| 279 | |
| 280 | // Inlined NULL terminated C-strings |
| 281 | case DW_FORM_string: |
| 282 | debug_info_data.getCStr(&offset); |
| 283 | break; |
| 284 | |
| 285 | // Compile unit address sized values |
| 286 | case DW_FORM_addr: |
| 287 | case DW_FORM_ref_addr: |
| 288 | form_size = cu_addr_size; |
| 289 | break; |
| 290 | |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 291 | // 0 byte value |
| 292 | case DW_FORM_flag_present: |
| 293 | form_size = 0; |
| 294 | break; |
| 295 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 296 | // 1 byte values |
| 297 | case DW_FORM_data1: |
| 298 | case DW_FORM_flag: |
| 299 | case DW_FORM_ref1: |
| 300 | form_size = 1; |
| 301 | break; |
| 302 | |
| 303 | // 2 byte values |
| 304 | case DW_FORM_data2: |
| 305 | case DW_FORM_ref2: |
| 306 | form_size = 2; |
| 307 | break; |
| 308 | |
| 309 | // 4 byte values |
| 310 | case DW_FORM_strp: |
| 311 | form_size = 4; |
| 312 | break; |
| 313 | |
| 314 | case DW_FORM_data4: |
| 315 | case DW_FORM_ref4: |
| 316 | form_size = 4; |
| 317 | break; |
| 318 | |
| 319 | // 8 byte values |
| 320 | case DW_FORM_data8: |
| 321 | case DW_FORM_ref8: |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 322 | case DW_FORM_ref_sig8: |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 323 | form_size = 8; |
| 324 | break; |
| 325 | |
| 326 | // signed or unsigned LEB 128 values |
| 327 | case DW_FORM_sdata: |
| 328 | case DW_FORM_udata: |
| 329 | case DW_FORM_ref_udata: |
| 330 | debug_info_data.getULEB128(&offset); |
| 331 | break; |
| 332 | |
| 333 | case DW_FORM_indirect: |
| 334 | form = debug_info_data.getULEB128(&offset); |
| 335 | form_is_indirect = true; |
| 336 | break; |
| 337 | |
Eric Christopher | 3887a90 | 2012-08-24 01:14:23 +0000 | [diff] [blame] | 338 | case DW_FORM_sec_offset: |
| 339 | if (cu->getAddressByteSize() == 4) |
| 340 | debug_info_data.getU32(offset_ptr); |
| 341 | else |
| 342 | debug_info_data.getU64(offset_ptr); |
| 343 | break; |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 344 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 345 | default: |
| 346 | *offset_ptr = offset; |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | offset += form_size; |
| 351 | } while (form_is_indirect); |
| 352 | } |
| 353 | } |
| 354 | *offset_ptr = offset; |
| 355 | return true; |
| 356 | } |
| 357 | } else { |
| 358 | AbbrevDecl = NULL; |
| 359 | *offset_ptr = offset; |
| 360 | return true; // NULL debug tag entry |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | return false; |
| 365 | } |
| 366 | |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 367 | bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const { |
| 368 | return getTag() == DW_TAG_subprogram; |
| 369 | } |
| 370 | |
| 371 | bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const { |
| 372 | uint32_t Tag = getTag(); |
| 373 | return Tag == DW_TAG_subprogram || |
| 374 | Tag == DW_TAG_inlined_subroutine; |
| 375 | } |
| 376 | |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 377 | uint32_t |
| 378 | DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu, |
| 379 | const uint16_t attr, |
| 380 | DWARFFormValue &form_value, |
| 381 | uint32_t *end_attr_offset_ptr) |
| 382 | const { |
| 383 | if (AbbrevDecl) { |
| 384 | uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr); |
| 385 | |
| 386 | if (attr_idx != -1U) { |
| 387 | uint32_t offset = getOffset(); |
| 388 | |
| 389 | DataExtractor debug_info_data = cu->getDebugInfoExtractor(); |
| 390 | |
| 391 | // Skip the abbreviation code so we are at the data for the attributes |
| 392 | debug_info_data.getULEB128(&offset); |
| 393 | |
| 394 | uint32_t idx = 0; |
| 395 | while (idx < attr_idx) |
| 396 | DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++), |
| 397 | debug_info_data, &offset, cu); |
| 398 | |
| 399 | const uint32_t attr_offset = offset; |
| 400 | form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx)); |
Benjamin Kramer | 4aa3fea | 2011-09-13 21:47:32 +0000 | [diff] [blame] | 401 | if (form_value.extractValue(debug_info_data, &offset, cu)) { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 402 | if (end_attr_offset_ptr) |
| 403 | *end_attr_offset_ptr = offset; |
| 404 | return attr_offset; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | const char* |
| 413 | DWARFDebugInfoEntryMinimal::getAttributeValueAsString( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 414 | const DWARFCompileUnit* cu, |
| 415 | const uint16_t attr, |
| 416 | const char* fail_value) |
| 417 | const { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 418 | DWARFFormValue form_value; |
| 419 | if (getAttributeValue(cu, attr, form_value)) { |
| 420 | DataExtractor stringExtractor(cu->getContext().getStringSection(), |
| 421 | false, 0); |
| 422 | return form_value.getAsCString(&stringExtractor); |
| 423 | } |
| 424 | return fail_value; |
| 425 | } |
| 426 | |
| 427 | uint64_t |
| 428 | DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 429 | const DWARFCompileUnit* cu, |
| 430 | const uint16_t attr, |
| 431 | uint64_t fail_value) const { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 432 | DWARFFormValue form_value; |
| 433 | if (getAttributeValue(cu, attr, form_value)) |
| 434 | return form_value.getUnsigned(); |
| 435 | return fail_value; |
| 436 | } |
| 437 | |
| 438 | int64_t |
| 439 | DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 440 | const DWARFCompileUnit* cu, |
| 441 | const uint16_t attr, |
| 442 | int64_t fail_value) const { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 443 | DWARFFormValue form_value; |
| 444 | if (getAttributeValue(cu, attr, form_value)) |
| 445 | return form_value.getSigned(); |
| 446 | return fail_value; |
| 447 | } |
| 448 | |
| 449 | uint64_t |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 450 | DWARFDebugInfoEntryMinimal::getAttributeValueAsReference( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 451 | const DWARFCompileUnit* cu, |
| 452 | const uint16_t attr, |
| 453 | uint64_t fail_value) |
| 454 | const { |
Benjamin Kramer | 72c0d7f | 2011-09-13 19:42:23 +0000 | [diff] [blame] | 455 | DWARFFormValue form_value; |
| 456 | if (getAttributeValue(cu, attr, form_value)) |
| 457 | return form_value.getReference(cu); |
| 458 | return fail_value; |
| 459 | } |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 460 | |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 461 | bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFCompileUnit *CU, |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 462 | uint64_t &LowPC, |
| 463 | uint64_t &HighPC) const { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 464 | HighPC = -1ULL; |
| 465 | LowPC = getAttributeValueAsUnsigned(CU, DW_AT_low_pc, -1ULL); |
| 466 | if (LowPC != -1ULL) |
| 467 | HighPC = getAttributeValueAsUnsigned(CU, DW_AT_high_pc, -1ULL); |
| 468 | return (HighPC != -1ULL); |
| 469 | } |
| 470 | |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 471 | void |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 472 | DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *CU, |
| 473 | DWARFDebugAranges *DebugAranges) |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 474 | const { |
| 475 | if (AbbrevDecl) { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 476 | if (isSubprogramDIE()) { |
| 477 | uint64_t LowPC, HighPC; |
| 478 | if (getLowAndHighPC(CU, LowPC, HighPC)) { |
| 479 | DebugAranges->appendRange(CU->getOffset(), LowPC, HighPC); |
| 480 | } |
| 481 | // FIXME: try to append ranges from .debug_ranges section. |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | const DWARFDebugInfoEntryMinimal *child = getFirstChild(); |
| 485 | while (child) { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 486 | child->buildAddressRangeTable(CU, DebugAranges); |
Benjamin Kramer | 10df806 | 2011-09-14 20:52:27 +0000 | [diff] [blame] | 487 | child = child->getSibling(); |
| 488 | } |
| 489 | } |
| 490 | } |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 491 | |
| 492 | bool |
| 493 | DWARFDebugInfoEntryMinimal::addressRangeContainsAddress( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 494 | const DWARFCompileUnit *CU, |
| 495 | const uint64_t Address) |
| 496 | const { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 497 | if (isNULL()) |
| 498 | return false; |
| 499 | uint64_t LowPC, HighPC; |
| 500 | if (getLowAndHighPC(CU, LowPC, HighPC)) |
| 501 | return (LowPC <= Address && Address <= HighPC); |
| 502 | // Try to get address ranges from .debug_ranges section. |
| 503 | uint32_t RangesOffset = getAttributeValueAsReference(CU, DW_AT_ranges, -1U); |
| 504 | if (RangesOffset != -1U) { |
| 505 | DWARFDebugRangeList RangeList; |
| 506 | if (CU->extractRangeList(RangesOffset, RangeList)) |
| 507 | return RangeList.containsAddress(CU->getBaseAddress(), Address); |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 512 | const char* |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 513 | DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFCompileUnit *CU) |
| 514 | const { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 515 | if (!isSubroutineDIE()) |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 516 | return 0; |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 517 | // Try to get mangled name if possible. |
| 518 | if (const char *name = |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 519 | getAttributeValueAsString(CU, DW_AT_MIPS_linkage_name, 0)) |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 520 | return name; |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 521 | if (const char *name = getAttributeValueAsString(CU, DW_AT_linkage_name, 0)) |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 522 | return name; |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 523 | if (const char *name = getAttributeValueAsString(CU, DW_AT_name, 0)) |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 524 | return name; |
| 525 | // Try to get name from specification DIE. |
| 526 | uint32_t spec_ref = |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 527 | getAttributeValueAsReference(CU, DW_AT_specification, -1U); |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 528 | if (spec_ref != -1U) { |
| 529 | DWARFDebugInfoEntryMinimal spec_die; |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 530 | if (spec_die.extract(CU, &spec_ref)) { |
| 531 | if (const char *name = spec_die.getSubroutineName(CU)) |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 532 | return name; |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 535 | // Try to get name from abstract origin DIE. |
| 536 | uint32_t abs_origin_ref = |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 537 | getAttributeValueAsReference(CU, DW_AT_abstract_origin, -1U); |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 538 | if (abs_origin_ref != -1U) { |
| 539 | DWARFDebugInfoEntryMinimal abs_origin_die; |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 540 | if (abs_origin_die.extract(CU, &abs_origin_ref)) { |
| 541 | if (const char *name = abs_origin_die.getSubroutineName(CU)) |
Alexey Samsonov | 9d26b0b | 2012-07-17 15:28:35 +0000 | [diff] [blame] | 542 | return name; |
| 543 | } |
| 544 | } |
| 545 | return 0; |
Alexey Samsonov | 3e25c4a | 2012-07-02 05:54:45 +0000 | [diff] [blame] | 546 | } |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 547 | |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 548 | void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFCompileUnit *CU, |
| 549 | uint32_t &CallFile, |
| 550 | uint32_t &CallLine, |
| 551 | uint32_t &CallColumn) const { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 552 | CallFile = getAttributeValueAsUnsigned(CU, DW_AT_call_file, 0); |
| 553 | CallLine = getAttributeValueAsUnsigned(CU, DW_AT_call_line, 0); |
| 554 | CallColumn = getAttributeValueAsUnsigned(CU, DW_AT_call_column, 0); |
| 555 | } |
| 556 | |
| 557 | DWARFDebugInfoEntryMinimal::InlinedChain |
| 558 | DWARFDebugInfoEntryMinimal::getInlinedChainForAddress( |
Eric Christopher | 203e6f6 | 2012-10-30 21:36:43 +0000 | [diff] [blame] | 559 | const DWARFCompileUnit *CU, |
| 560 | const uint64_t Address) |
| 561 | const { |
Alexey Samsonov | 5eae90d | 2012-09-04 08:12:33 +0000 | [diff] [blame] | 562 | DWARFDebugInfoEntryMinimal::InlinedChain InlinedChain; |
| 563 | if (isNULL()) |
| 564 | return InlinedChain; |
| 565 | for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) { |
| 566 | // Append current DIE to inlined chain only if it has correct tag |
| 567 | // (e.g. it is not a lexical block). |
| 568 | if (DIE->isSubroutineDIE()) { |
| 569 | InlinedChain.push_back(*DIE); |
| 570 | } |
| 571 | // Try to get child which also contains provided address. |
| 572 | const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild(); |
| 573 | while (Child) { |
| 574 | if (Child->addressRangeContainsAddress(CU, Address)) { |
| 575 | // Assume there is only one such child. |
| 576 | break; |
| 577 | } |
| 578 | Child = Child->getSibling(); |
| 579 | } |
| 580 | DIE = Child; |
| 581 | } |
| 582 | // Reverse the obtained chain to make the root of inlined chain last. |
| 583 | std::reverse(InlinedChain.begin(), InlinedChain.end()); |
| 584 | return InlinedChain; |
| 585 | } |