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