1 //===-- DWARFDebugInfoEntry.cpp ---------------------------------*- C++ -*-===// 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 12 #include <assert.h> 13 14 #include <algorithm> 15 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/Stream.h" 18 #include "lldb/Expression/DWARFExpression.h" 19 #include "lldb/Symbol/ObjectFile.h" 20 21 #include "DWARFCompileUnit.h" 22 #include "SymbolFileDWARF.h" 23 #include "DWARFDebugAbbrev.h" 24 #include "DWARFDebugAranges.h" 25 #include "DWARFDebugInfo.h" 26 #include "DWARFDeclContext.h" 27 #include "DWARFDIECollection.h" 28 #include "DWARFFormValue.h" 29 #include "DWARFLocationDescription.h" 30 #include "DWARFLocationList.h" 31 #include "DWARFDebugRanges.h" 32 33 using namespace lldb_private; 34 using namespace std; 35 extern int g_verbose; 36 37 38 39 DWARFDebugInfoEntry::Attributes::Attributes() : 40 m_infos() 41 { 42 } 43 44 DWARFDebugInfoEntry::Attributes::~Attributes() 45 { 46 } 47 48 49 uint32_t 50 DWARFDebugInfoEntry::Attributes::FindAttributeIndex(dw_attr_t attr) const 51 { 52 collection::const_iterator end = m_infos.end(); 53 collection::const_iterator beg = m_infos.begin(); 54 collection::const_iterator pos; 55 for (pos = beg; pos != end; ++pos) 56 { 57 if (pos->attr == attr) 58 return std::distance(beg, pos); 59 } 60 return UINT32_MAX; 61 } 62 63 void 64 DWARFDebugInfoEntry::Attributes::Append(const DWARFCompileUnit *cu, dw_offset_t attr_die_offset, dw_attr_t attr, dw_form_t form) 65 { 66 Info info = { cu, attr_die_offset, attr, form }; 67 m_infos.push_back(info); 68 } 69 70 bool 71 DWARFDebugInfoEntry::Attributes::ContainsAttribute(dw_attr_t attr) const 72 { 73 return FindAttributeIndex(attr) != UINT32_MAX; 74 } 75 76 bool 77 DWARFDebugInfoEntry::Attributes::RemoveAttribute(dw_attr_t attr) 78 { 79 uint32_t attr_index = FindAttributeIndex(attr); 80 if (attr_index != UINT32_MAX) 81 { 82 m_infos.erase(m_infos.begin() + attr_index); 83 return true; 84 } 85 return false; 86 } 87 88 bool 89 DWARFDebugInfoEntry::Attributes::ExtractFormValueAtIndex (SymbolFileDWARF* dwarf2Data, uint32_t i, DWARFFormValue &form_value) const 90 { 91 form_value.SetForm(FormAtIndex(i)); 92 lldb::offset_t offset = DIEOffsetAtIndex(i); 93 return form_value.ExtractValue(dwarf2Data->get_debug_info_data(), &offset, CompileUnitAtIndex(i)); 94 } 95 96 uint64_t 97 DWARFDebugInfoEntry::Attributes::FormValueAsUnsigned (SymbolFileDWARF* dwarf2Data, dw_attr_t attr, uint64_t fail_value) const 98 { 99 const uint32_t attr_idx = FindAttributeIndex (attr); 100 if (attr_idx != UINT32_MAX) 101 return FormValueAsUnsignedAtIndex (dwarf2Data, attr_idx, fail_value); 102 return fail_value; 103 } 104 105 uint64_t 106 DWARFDebugInfoEntry::Attributes::FormValueAsUnsignedAtIndex(SymbolFileDWARF* dwarf2Data, uint32_t i, uint64_t fail_value) const 107 { 108 DWARFFormValue form_value; 109 if (ExtractFormValueAtIndex(dwarf2Data, i, form_value)) 110 return form_value.Reference(CompileUnitAtIndex(i)); 111 return fail_value; 112 } 113 114 115 116 bool 117 DWARFDebugInfoEntry::FastExtract 118 ( 119 const DataExtractor& debug_info_data, 120 const DWARFCompileUnit* cu, 121 const uint8_t *fixed_form_sizes, 122 lldb::offset_t *offset_ptr 123 ) 124 { 125 m_offset = *offset_ptr; 126 m_parent_idx = 0; 127 m_sibling_idx = 0; 128 m_empty_children = false; 129 const uint64_t abbr_idx = debug_info_data.GetULEB128 (offset_ptr); 130 assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE)); 131 m_abbr_idx = abbr_idx; 132 133 //assert (fixed_form_sizes); // For best performance this should be specified! 134 135 if (m_abbr_idx) 136 { 137 lldb::offset_t offset = *offset_ptr; 138 139 const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx); 140 141 if (abbrevDecl == NULL) 142 { 143 cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid abbreviation code %u, please file a bug and attach the file at the start of this error message", 144 m_offset, 145 (unsigned)abbr_idx); 146 // WE can't parse anymore if the DWARF is borked... 147 *offset_ptr = UINT32_MAX; 148 return false; 149 } 150 m_tag = abbrevDecl->Tag(); 151 m_has_children = abbrevDecl->HasChildren(); 152 // Skip all data in the .debug_info for the attributes 153 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 154 register uint32_t i; 155 register dw_form_t form; 156 for (i=0; i<numAttributes; ++i) 157 { 158 form = abbrevDecl->GetFormByIndexUnchecked(i); 159 160 const uint8_t fixed_skip_size = fixed_form_sizes [form]; 161 if (fixed_skip_size) 162 offset += fixed_skip_size; 163 else 164 { 165 bool form_is_indirect = false; 166 do 167 { 168 form_is_indirect = false; 169 register uint32_t form_size = 0; 170 switch (form) 171 { 172 // Blocks if inlined data that have a length field and the data bytes 173 // inlined in the .debug_info 174 case DW_FORM_exprloc : 175 case DW_FORM_block : form_size = debug_info_data.GetULEB128 (&offset); break; 176 case DW_FORM_block1 : form_size = debug_info_data.GetU8_unchecked (&offset); break; 177 case DW_FORM_block2 : form_size = debug_info_data.GetU16_unchecked (&offset);break; 178 case DW_FORM_block4 : form_size = debug_info_data.GetU32_unchecked (&offset);break; 179 180 // Inlined NULL terminated C-strings 181 case DW_FORM_string : 182 debug_info_data.GetCStr (&offset); 183 break; 184 185 // Compile unit address sized values 186 case DW_FORM_addr : 187 case DW_FORM_ref_addr : 188 form_size = cu->GetAddressByteSize(); 189 break; 190 191 // 0 sized form 192 case DW_FORM_flag_present: 193 form_size = 0; 194 break; 195 196 // 1 byte values 197 case DW_FORM_data1 : 198 case DW_FORM_flag : 199 case DW_FORM_ref1 : 200 form_size = 1; 201 break; 202 203 // 2 byte values 204 case DW_FORM_data2 : 205 case DW_FORM_ref2 : 206 form_size = 2; 207 break; 208 209 // 4 byte values 210 case DW_FORM_strp : 211 case DW_FORM_data4 : 212 case DW_FORM_ref4 : 213 form_size = 4; 214 break; 215 216 // 8 byte values 217 case DW_FORM_data8 : 218 case DW_FORM_ref8 : 219 case DW_FORM_ref_sig8 : 220 form_size = 8; 221 break; 222 223 // signed or unsigned LEB 128 values 224 case DW_FORM_sdata : 225 case DW_FORM_udata : 226 case DW_FORM_ref_udata : 227 debug_info_data.Skip_LEB128 (&offset); 228 break; 229 230 case DW_FORM_indirect : 231 form_is_indirect = true; 232 form = debug_info_data.GetULEB128 (&offset); 233 break; 234 235 case DW_FORM_sec_offset : 236 if (cu->GetAddressByteSize () == 4) 237 debug_info_data.GetU32 (offset_ptr); 238 else 239 debug_info_data.GetU64 (offset_ptr); 240 break; 241 242 default: 243 *offset_ptr = m_offset; 244 return false; 245 } 246 offset += form_size; 247 248 } while (form_is_indirect); 249 } 250 } 251 *offset_ptr = offset; 252 return true; 253 } 254 else 255 { 256 m_tag = 0; 257 m_has_children = false; 258 return true; // NULL debug tag entry 259 } 260 261 return false; 262 } 263 264 //---------------------------------------------------------------------- 265 // Extract 266 // 267 // Extract a debug info entry for a given compile unit from the 268 // .debug_info and .debug_abbrev data within the SymbolFileDWARF class 269 // starting at the given offset 270 //---------------------------------------------------------------------- 271 bool 272 DWARFDebugInfoEntry::Extract 273 ( 274 SymbolFileDWARF* dwarf2Data, 275 const DWARFCompileUnit* cu, 276 lldb::offset_t *offset_ptr 277 ) 278 { 279 const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data(); 280 // const DataExtractor& debug_str_data = dwarf2Data->get_debug_str_data(); 281 const uint32_t cu_end_offset = cu->GetNextCompileUnitOffset(); 282 const uint8_t cu_addr_size = cu->GetAddressByteSize(); 283 lldb::offset_t offset = *offset_ptr; 284 // if (offset >= cu_end_offset) 285 // Log::Error("DIE at offset 0x%8.8x is beyond the end of the current compile unit (0x%8.8x)", m_offset, cu_end_offset); 286 if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset)) 287 { 288 m_offset = offset; 289 290 const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset); 291 assert (abbr_idx < (1 << DIE_ABBR_IDX_BITSIZE)); 292 m_abbr_idx = abbr_idx; 293 if (abbr_idx) 294 { 295 const DWARFAbbreviationDeclaration *abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx); 296 297 if (abbrevDecl) 298 { 299 m_tag = abbrevDecl->Tag(); 300 m_has_children = abbrevDecl->HasChildren(); 301 302 bool isCompileUnitTag = m_tag == DW_TAG_compile_unit; 303 if (cu && isCompileUnitTag) 304 ((DWARFCompileUnit*)cu)->SetBaseAddress(0); 305 306 // Skip all data in the .debug_info for the attributes 307 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 308 uint32_t i; 309 dw_attr_t attr; 310 dw_form_t form; 311 for (i=0; i<numAttributes; ++i) 312 { 313 abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form); 314 315 if (isCompileUnitTag && ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) 316 { 317 DWARFFormValue form_value(form); 318 if (form_value.ExtractValue(debug_info_data, &offset, cu)) 319 { 320 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc) 321 ((DWARFCompileUnit*)cu)->SetBaseAddress(form_value.Unsigned()); 322 } 323 } 324 else 325 { 326 bool form_is_indirect = false; 327 do 328 { 329 form_is_indirect = false; 330 register uint32_t form_size = 0; 331 switch (form) 332 { 333 // Blocks if inlined data that have a length field and the data bytes 334 // inlined in the .debug_info 335 case DW_FORM_exprloc : 336 case DW_FORM_block : form_size = debug_info_data.GetULEB128(&offset); break; 337 case DW_FORM_block1 : form_size = debug_info_data.GetU8(&offset); break; 338 case DW_FORM_block2 : form_size = debug_info_data.GetU16(&offset); break; 339 case DW_FORM_block4 : form_size = debug_info_data.GetU32(&offset); break; 340 341 // Inlined NULL terminated C-strings 342 case DW_FORM_string : debug_info_data.GetCStr(&offset); break; 343 344 // Compile unit address sized values 345 case DW_FORM_addr : 346 case DW_FORM_ref_addr : 347 form_size = cu_addr_size; 348 break; 349 350 // 0 sized form 351 case DW_FORM_flag_present: 352 form_size = 0; 353 break; 354 355 // 1 byte values 356 case DW_FORM_data1 : 357 case DW_FORM_flag : 358 case DW_FORM_ref1 : 359 form_size = 1; 360 break; 361 362 // 2 byte values 363 case DW_FORM_data2 : 364 case DW_FORM_ref2 : 365 form_size = 2; 366 break; 367 368 // 4 byte values 369 case DW_FORM_strp : 370 form_size = 4; 371 break; 372 373 case DW_FORM_data4 : 374 case DW_FORM_ref4 : 375 form_size = 4; 376 break; 377 378 // 8 byte values 379 case DW_FORM_data8 : 380 case DW_FORM_ref8 : 381 case DW_FORM_ref_sig8 : 382 form_size = 8; 383 break; 384 385 // signed or unsigned LEB 128 values 386 case DW_FORM_sdata : 387 case DW_FORM_udata : 388 case DW_FORM_ref_udata : 389 debug_info_data.Skip_LEB128(&offset); 390 break; 391 392 case DW_FORM_indirect : 393 form = debug_info_data.GetULEB128(&offset); 394 form_is_indirect = true; 395 break; 396 397 case DW_FORM_sec_offset : 398 if (cu->GetAddressByteSize () == 4) 399 debug_info_data.GetU32 (offset_ptr); 400 else 401 debug_info_data.GetU64 (offset_ptr); 402 break; 403 404 default: 405 *offset_ptr = offset; 406 return false; 407 } 408 409 offset += form_size; 410 } while (form_is_indirect); 411 } 412 } 413 *offset_ptr = offset; 414 return true; 415 } 416 } 417 else 418 { 419 m_tag = 0; 420 m_has_children = false; 421 *offset_ptr = offset; 422 return true; // NULL debug tag entry 423 } 424 } 425 426 return false; 427 } 428 429 //---------------------------------------------------------------------- 430 // DumpAncestry 431 // 432 // Dumps all of a debug information entries parents up until oldest and 433 // all of it's attributes to the specified stream. 434 //---------------------------------------------------------------------- 435 void 436 DWARFDebugInfoEntry::DumpAncestry 437 ( 438 SymbolFileDWARF* dwarf2Data, 439 const DWARFCompileUnit* cu, 440 const DWARFDebugInfoEntry* oldest, 441 Stream &s, 442 uint32_t recurse_depth 443 ) const 444 { 445 const DWARFDebugInfoEntry* parent = GetParent(); 446 if (parent && parent != oldest) 447 parent->DumpAncestry(dwarf2Data, cu, oldest, s, 0); 448 Dump(dwarf2Data, cu, s, recurse_depth); 449 } 450 451 //---------------------------------------------------------------------- 452 // Compare two DIE by comparing all their attributes values, and 453 // following all DW_FORM_ref attributes and comparing their contents as 454 // well (except for DW_AT_sibling attributes. 455 // 456 // DWARFDebugInfoEntry::CompareState compare_state; 457 // int result = DWARFDebugInfoEntry::Compare(this, 0x00017ccb, 0x0001eb2b, compare_state, false, true); 458 //---------------------------------------------------------------------- 459 //int 460 //DWARFDebugInfoEntry::Compare 461 //( 462 // SymbolFileDWARF* dwarf2Data, 463 // dw_offset_t a_die_offset, 464 // dw_offset_t b_die_offset, 465 // CompareState &compare_state, 466 // bool compare_siblings, 467 // bool compare_children 468 //) 469 //{ 470 // if (a_die_offset == b_die_offset) 471 // return 0; 472 // 473 // DWARFCompileUnitSP a_cu_sp; 474 // DWARFCompileUnitSP b_cu_sp; 475 // const DWARFDebugInfoEntry* a_die = dwarf2Data->DebugInfo()->GetDIEPtr(a_die_offset, &a_cu_sp); 476 // const DWARFDebugInfoEntry* b_die = dwarf2Data->DebugInfo()->GetDIEPtr(b_die_offset, &b_cu_sp); 477 // 478 // return Compare(dwarf2Data, a_cu_sp.get(), a_die, b_cu_sp.get(), b_die, compare_state, compare_siblings, compare_children); 479 //} 480 // 481 //int 482 //DWARFDebugInfoEntry::Compare 483 //( 484 // SymbolFileDWARF* dwarf2Data, 485 // DWARFCompileUnit* a_cu, const DWARFDebugInfoEntry* a_die, 486 // DWARFCompileUnit* b_cu, const DWARFDebugInfoEntry* b_die, 487 // CompareState &compare_state, 488 // bool compare_siblings, 489 // bool compare_children 490 //) 491 //{ 492 // if (a_die == b_die) 493 // return 0; 494 // 495 // if (!compare_state.AddTypePair(a_die->GetOffset(), b_die->GetOffset())) 496 // { 497 // // We are already comparing both of these types, so let 498 // // compares complete for the real result 499 // return 0; 500 // } 501 // 502 // //printf("DWARFDebugInfoEntry::Compare(0x%8.8x, 0x%8.8x)\n", a_die->GetOffset(), b_die->GetOffset()); 503 // 504 // // Do we have two valid DIEs? 505 // if (a_die && b_die) 506 // { 507 // // Both DIE are valid 508 // int result = 0; 509 // 510 // const dw_tag_t a_tag = a_die->Tag(); 511 // const dw_tag_t b_tag = b_die->Tag(); 512 // if (a_tag == 0 && b_tag == 0) 513 // return 0; 514 // 515 // //printf(" comparing tags: %s and %s\n", DW_TAG_value_to_name(a_tag), DW_TAG_value_to_name(b_tag)); 516 // 517 // if (a_tag < b_tag) 518 // return -1; 519 // else if (a_tag > b_tag) 520 // return 1; 521 // 522 // DWARFDebugInfoEntry::Attributes a_attrs; 523 // DWARFDebugInfoEntry::Attributes b_attrs; 524 // size_t a_attr_count = a_die->GetAttributes(dwarf2Data, a_cu, a_attrs); 525 // size_t b_attr_count = b_die->GetAttributes(dwarf2Data, b_cu, b_attrs); 526 // if (a_attr_count != b_attr_count) 527 // { 528 // a_attrs.RemoveAttribute(DW_AT_sibling); 529 // b_attrs.RemoveAttribute(DW_AT_sibling); 530 // } 531 // 532 // a_attr_count = a_attrs.Size(); 533 // b_attr_count = b_attrs.Size(); 534 // 535 // DWARFFormValue a_form_value; 536 // DWARFFormValue b_form_value; 537 // 538 // if (a_attr_count != b_attr_count) 539 // { 540 // uint32_t is_decl_index = a_attrs.FindAttributeIndex(DW_AT_declaration); 541 // uint32_t a_name_index = UINT32_MAX; 542 // uint32_t b_name_index = UINT32_MAX; 543 // if (is_decl_index != UINT32_MAX) 544 // { 545 // if (a_attr_count == 2) 546 // { 547 // a_name_index = a_attrs.FindAttributeIndex(DW_AT_name); 548 // b_name_index = b_attrs.FindAttributeIndex(DW_AT_name); 549 // } 550 // } 551 // else 552 // { 553 // is_decl_index = b_attrs.FindAttributeIndex(DW_AT_declaration); 554 // if (is_decl_index != UINT32_MAX && a_attr_count == 2) 555 // { 556 // a_name_index = a_attrs.FindAttributeIndex(DW_AT_name); 557 // b_name_index = b_attrs.FindAttributeIndex(DW_AT_name); 558 // } 559 // } 560 // if (a_name_index != UINT32_MAX && b_name_index != UINT32_MAX) 561 // { 562 // if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, a_name_index, a_form_value) && 563 // b_attrs.ExtractFormValueAtIndex(dwarf2Data, b_name_index, b_form_value)) 564 // { 565 // result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, &dwarf2Data->get_debug_str_data()); 566 // if (result == 0) 567 // { 568 // a_attr_count = b_attr_count = 0; 569 // compare_children = false; 570 // } 571 // } 572 // } 573 // } 574 // 575 // if (a_attr_count < b_attr_count) 576 // return -1; 577 // if (a_attr_count > b_attr_count) 578 // return 1; 579 // 580 // 581 // // The number of attributes are the same... 582 // if (a_attr_count > 0) 583 // { 584 // const DataExtractor* debug_str_data_ptr = &dwarf2Data->get_debug_str_data(); 585 // 586 // uint32_t i; 587 // for (i=0; i<a_attr_count; ++i) 588 // { 589 // const dw_attr_t a_attr = a_attrs.AttributeAtIndex(i); 590 // const dw_attr_t b_attr = b_attrs.AttributeAtIndex(i); 591 // //printf(" comparing attributes\n\t\t0x%8.8x: %s %s\t\t0x%8.8x: %s %s\n", 592 // // a_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(a_attrs.FormAtIndex(i)), DW_AT_value_to_name(a_attr), 593 // // b_attrs.DIEOffsetAtIndex(i), DW_FORM_value_to_name(b_attrs.FormAtIndex(i)), DW_AT_value_to_name(b_attr)); 594 // 595 // if (a_attr < b_attr) 596 // return -1; 597 // else if (a_attr > b_attr) 598 // return 1; 599 // 600 // switch (a_attr) 601 // { 602 // // Since we call a form of GetAttributes which inlines the 603 // // attributes from DW_AT_abstract_origin and DW_AT_specification 604 // // we don't care if their values mismatch... 605 // case DW_AT_abstract_origin: 606 // case DW_AT_specification: 607 // case DW_AT_sibling: 608 // case DW_AT_containing_type: 609 // //printf(" action = IGNORE\n"); 610 // result = 0; 611 // break; // ignore 612 // 613 // default: 614 // if (a_attrs.ExtractFormValueAtIndex(dwarf2Data, i, a_form_value) && 615 // b_attrs.ExtractFormValueAtIndex(dwarf2Data, i, b_form_value)) 616 // result = DWARFFormValue::Compare (a_form_value, b_form_value, a_cu, b_cu, debug_str_data_ptr); 617 // break; 618 // } 619 // 620 // //printf("\t result = %i\n", result); 621 // 622 // if (result != 0) 623 // { 624 // // Attributes weren't equal, lets see if we care? 625 // switch (a_attr) 626 // { 627 // case DW_AT_decl_file: 628 // // TODO: add the ability to compare files in two different compile units 629 // if (a_cu == b_cu) 630 // { 631 // //printf(" action = RETURN RESULT\n"); 632 // return result; // Only return the compare results when the compile units are the same and the decl_file attributes can be compared 633 // } 634 // else 635 // { 636 // result = 0; 637 // //printf(" action = IGNORE\n"); 638 // } 639 // break; 640 // 641 // default: 642 // switch (a_attrs.FormAtIndex(i)) 643 // { 644 // case DW_FORM_ref1: 645 // case DW_FORM_ref2: 646 // case DW_FORM_ref4: 647 // case DW_FORM_ref8: 648 // case DW_FORM_ref_udata: 649 // case DW_FORM_ref_addr: 650 // //printf(" action = COMPARE DIEs 0x%8.8x 0x%8.8x\n", (dw_offset_t)a_form_value.Reference(a_cu), (dw_offset_t)b_form_value.Reference(b_cu)); 651 // // These attribute values refer to other DIEs, so lets compare those instead of their DIE offsets... 652 // result = Compare(dwarf2Data, a_form_value.Reference(a_cu), b_form_value.Reference(b_cu), compare_state, false, true); 653 // if (result != 0) 654 // return result; 655 // break; 656 // 657 // default: 658 // // We do care that they were different, return this result... 659 // //printf(" action = RETURN RESULT\n"); 660 // return result; 661 // } 662 // } 663 // } 664 // } 665 // } 666 // //printf(" SUCCESS\n\t\t0x%8.8x: %s\n\t\t0x%8.8x: %s\n", a_die->GetOffset(), DW_TAG_value_to_name(a_tag), b_die->GetOffset(), DW_TAG_value_to_name(b_tag)); 667 // 668 // if (compare_children) 669 // { 670 // bool a_has_children = a_die->HasChildren(); 671 // bool b_has_children = b_die->HasChildren(); 672 // if (a_has_children == b_has_children) 673 // { 674 // // Both either have kids or don't 675 // if (a_has_children) 676 // result = Compare( dwarf2Data, 677 // a_cu, a_die->GetFirstChild(), 678 // b_cu, b_die->GetFirstChild(), 679 // compare_state, true, compare_children); 680 // else 681 // result = 0; 682 // } 683 // else if (!a_has_children) 684 // result = -1; // A doesn't have kids, but B does 685 // else 686 // result = 1; // A has kids, but B doesn't 687 // } 688 // 689 // if (compare_siblings) 690 // { 691 // result = Compare( dwarf2Data, 692 // a_cu, a_die->GetSibling(), 693 // b_cu, b_die->GetSibling(), 694 // compare_state, true, compare_children); 695 // } 696 // 697 // return result; 698 // } 699 // 700 // if (a_die == NULL) 701 // return -1; // a_die is NULL, yet b_die is non-NULL 702 // else 703 // return 1; // a_die is non-NULL, yet b_die is NULL 704 // 705 //} 706 // 707 // 708 //int 709 //DWARFDebugInfoEntry::Compare 710 //( 711 // SymbolFileDWARF* dwarf2Data, 712 // const DWARFCompileUnit* cu_a, 713 // const DWARFDebugInfoEntry* die_a, 714 // const DWARFCompileUnit* cu_a, 715 // const DWARFDebugInfoEntry* die_b, 716 // CompareState &compare_state 717 //) 718 //{ 719 //} 720 721 //---------------------------------------------------------------------- 722 // GetDIENamesAndRanges 723 // 724 // Gets the valid address ranges for a given DIE by looking for a 725 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges 726 // attributes. 727 //---------------------------------------------------------------------- 728 bool 729 DWARFDebugInfoEntry::GetDIENamesAndRanges 730 ( 731 SymbolFileDWARF* dwarf2Data, 732 const DWARFCompileUnit* cu, 733 const char * &name, 734 const char * &mangled, 735 DWARFDebugRanges::RangeList& ranges, 736 int& decl_file, 737 int& decl_line, 738 int& decl_column, 739 int& call_file, 740 int& call_line, 741 int& call_column, 742 DWARFExpression *frame_base 743 ) const 744 { 745 if (dwarf2Data == NULL) 746 return false; 747 748 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 749 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 750 std::vector<dw_offset_t> die_offsets; 751 bool set_frame_base_loclist_addr = false; 752 753 lldb::offset_t offset; 754 const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset); 755 756 if (abbrevDecl) 757 { 758 const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data(); 759 760 if (!debug_info_data.ValidOffset(offset)) 761 return false; 762 763 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 764 uint32_t i; 765 dw_attr_t attr; 766 dw_form_t form; 767 for (i=0; i<numAttributes; ++i) 768 { 769 abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form); 770 DWARFFormValue form_value(form); 771 if (form_value.ExtractValue(debug_info_data, &offset, cu)) 772 { 773 switch (attr) 774 { 775 case DW_AT_low_pc: 776 case DW_AT_entry_pc: 777 lo_pc = form_value.Unsigned(); 778 break; 779 780 case DW_AT_high_pc: 781 hi_pc = form_value.Unsigned(); 782 break; 783 784 case DW_AT_ranges: 785 { 786 const DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges(); 787 debug_ranges->FindRanges(form_value.Unsigned(), ranges); 788 // All DW_AT_ranges are relative to the base address of the 789 // compile unit. We add the compile unit base address to make 790 // sure all the addresses are properly fixed up. 791 ranges.Slide(cu->GetBaseAddress()); 792 } 793 break; 794 795 case DW_AT_name: 796 if (name == NULL) 797 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 798 break; 799 800 case DW_AT_MIPS_linkage_name: 801 case DW_AT_linkage_name: 802 if (mangled == NULL) 803 mangled = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 804 break; 805 806 case DW_AT_abstract_origin: 807 die_offsets.push_back(form_value.Reference(cu)); 808 break; 809 810 case DW_AT_specification: 811 die_offsets.push_back(form_value.Reference(cu)); 812 break; 813 814 case DW_AT_decl_file: 815 if (decl_file == 0) 816 decl_file = form_value.Unsigned(); 817 break; 818 819 case DW_AT_decl_line: 820 if (decl_line == 0) 821 decl_line = form_value.Unsigned(); 822 break; 823 824 case DW_AT_decl_column: 825 if (decl_column == 0) 826 decl_column = form_value.Unsigned(); 827 break; 828 829 case DW_AT_call_file: 830 if (call_file == 0) 831 call_file = form_value.Unsigned(); 832 break; 833 834 case DW_AT_call_line: 835 if (call_line == 0) 836 call_line = form_value.Unsigned(); 837 break; 838 839 case DW_AT_call_column: 840 if (call_column == 0) 841 call_column = form_value.Unsigned(); 842 break; 843 844 case DW_AT_frame_base: 845 if (frame_base) 846 { 847 if (form_value.BlockData()) 848 { 849 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 850 uint32_t block_length = form_value.Unsigned(); 851 frame_base->SetOpcodeData(debug_info_data, block_offset, block_length); 852 } 853 else 854 { 855 const DataExtractor &debug_loc_data = dwarf2Data->get_debug_loc_data(); 856 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 857 858 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset); 859 if (loc_list_length > 0) 860 { 861 frame_base->SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length); 862 if (lo_pc != LLDB_INVALID_ADDRESS) 863 { 864 assert (lo_pc >= cu->GetBaseAddress()); 865 frame_base->SetLocationListSlide(lo_pc - cu->GetBaseAddress()); 866 } 867 else 868 { 869 set_frame_base_loclist_addr = true; 870 } 871 } 872 } 873 } 874 break; 875 876 default: 877 break; 878 } 879 } 880 } 881 } 882 883 if (ranges.IsEmpty()) 884 { 885 if (lo_pc != LLDB_INVALID_ADDRESS) 886 { 887 if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) 888 ranges.Append(DWARFDebugRanges::Range (lo_pc, hi_pc - lo_pc)); 889 else 890 ranges.Append(DWARFDebugRanges::Range (lo_pc, 0)); 891 } 892 } 893 894 if (set_frame_base_loclist_addr) 895 { 896 dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0); 897 assert (lowest_range_pc >= cu->GetBaseAddress()); 898 frame_base->SetLocationListSlide (lowest_range_pc - cu->GetBaseAddress()); 899 } 900 901 if (ranges.IsEmpty() || name == NULL || mangled == NULL) 902 { 903 std::vector<dw_offset_t>::const_iterator pos; 904 std::vector<dw_offset_t>::const_iterator end = die_offsets.end(); 905 for (pos = die_offsets.begin(); pos != end; ++pos) 906 { 907 DWARFCompileUnitSP cu_sp_ptr; 908 const DWARFDebugInfoEntry* die = NULL; 909 dw_offset_t die_offset = *pos; 910 if (die_offset != DW_INVALID_OFFSET) 911 { 912 die = dwarf2Data->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr); 913 if (die) 914 die->GetDIENamesAndRanges(dwarf2Data, cu_sp_ptr.get(), name, mangled, ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column); 915 } 916 } 917 } 918 return !ranges.IsEmpty(); 919 } 920 921 //---------------------------------------------------------------------- 922 // Dump 923 // 924 // Dumps a debug information entry and all of it's attributes to the 925 // specified stream. 926 //---------------------------------------------------------------------- 927 void 928 DWARFDebugInfoEntry::Dump 929 ( 930 SymbolFileDWARF* dwarf2Data, 931 const DWARFCompileUnit* cu, 932 Stream &s, 933 uint32_t recurse_depth 934 ) const 935 { 936 const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data(); 937 lldb::offset_t offset = m_offset; 938 939 if (debug_info_data.ValidOffset(offset)) 940 { 941 dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset); 942 943 s.Printf("\n0x%8.8x: ", m_offset); 944 s.Indent(); 945 if (abbrCode != m_abbr_idx) 946 { 947 s.Printf( "error: DWARF has been modified\n"); 948 } 949 else if (abbrCode) 950 { 951 const DWARFAbbreviationDeclaration* abbrevDecl = cu->GetAbbreviations()->GetAbbreviationDeclaration (abbrCode); 952 953 if (abbrevDecl) 954 { 955 s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag())); 956 s.Printf( " [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*':' '); 957 958 // Dump all data in the .debug_info for the attributes 959 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 960 uint32_t i; 961 dw_attr_t attr; 962 dw_form_t form; 963 for (i=0; i<numAttributes; ++i) 964 { 965 abbrevDecl->GetAttrAndFormByIndexUnchecked(i, attr, form); 966 967 DumpAttribute(dwarf2Data, cu, debug_info_data, &offset, s, attr, form); 968 } 969 970 const DWARFDebugInfoEntry* child = GetFirstChild(); 971 if (recurse_depth > 0 && child) 972 { 973 s.IndentMore(); 974 975 while (child) 976 { 977 child->Dump(dwarf2Data, cu, s, recurse_depth-1); 978 child = child->GetSibling(); 979 } 980 s.IndentLess(); 981 } 982 } 983 else 984 s.Printf( "Abbreviation code note found in 'debug_abbrev' class for code: %u\n", abbrCode); 985 } 986 else 987 { 988 s.Printf( "NULL\n"); 989 } 990 } 991 } 992 993 void 994 DWARFDebugInfoEntry::DumpLocation 995 ( 996 SymbolFileDWARF* dwarf2Data, 997 DWARFCompileUnit* cu, 998 Stream &s 999 ) const 1000 { 1001 const DWARFDebugInfoEntry *cu_die = cu->GetCompileUnitDIEOnly(); 1002 const char *cu_name = NULL; 1003 if (cu_die != NULL) 1004 cu_name = cu_die->GetName (dwarf2Data, cu); 1005 const char *obj_file_name = NULL; 1006 ObjectFile *obj_file = dwarf2Data->GetObjectFile(); 1007 if (obj_file) 1008 obj_file_name = obj_file->GetFileSpec().GetFilename().AsCString(); 1009 const char *die_name = GetName (dwarf2Data, cu); 1010 s.Printf ("0x%8.8x/0x%8.8x: %-30s (from %s in %s)", 1011 cu->GetOffset(), 1012 GetOffset(), 1013 die_name ? die_name : "", 1014 cu_name ? cu_name : "<NULL>", 1015 obj_file_name ? obj_file_name : "<NULL>"); 1016 } 1017 1018 //---------------------------------------------------------------------- 1019 // DumpAttribute 1020 // 1021 // Dumps a debug information entry attribute along with it's form. Any 1022 // special display of attributes is done (disassemble location lists, 1023 // show enumeration values for attributes, etc). 1024 //---------------------------------------------------------------------- 1025 void 1026 DWARFDebugInfoEntry::DumpAttribute 1027 ( 1028 SymbolFileDWARF* dwarf2Data, 1029 const DWARFCompileUnit* cu, 1030 const DataExtractor& debug_info_data, 1031 lldb::offset_t *offset_ptr, 1032 Stream &s, 1033 dw_attr_t attr, 1034 dw_form_t form 1035 ) 1036 { 1037 bool verbose = s.GetVerbose(); 1038 bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm); 1039 1040 const DataExtractor* debug_str_data = dwarf2Data ? &dwarf2Data->get_debug_str_data() : NULL; 1041 if (verbose) 1042 s.Offset (*offset_ptr); 1043 else 1044 s.Printf (" "); 1045 s.Indent(DW_AT_value_to_name(attr)); 1046 1047 if (show_form) 1048 { 1049 s.Printf( "[%s", DW_FORM_value_to_name(form)); 1050 } 1051 1052 DWARFFormValue form_value(form); 1053 1054 if (!form_value.ExtractValue(debug_info_data, offset_ptr, cu)) 1055 return; 1056 1057 if (show_form) 1058 { 1059 if (form == DW_FORM_indirect) 1060 { 1061 s.Printf( " [%s]", DW_FORM_value_to_name(form_value.Form())); 1062 } 1063 1064 s.PutCString("] "); 1065 } 1066 1067 s.PutCString("( "); 1068 1069 // Always dump form value if verbose is enabled 1070 if (verbose) 1071 { 1072 form_value.Dump(s, debug_str_data, cu); 1073 } 1074 1075 1076 // Check to see if we have any special attribute formatters 1077 switch (attr) 1078 { 1079 case DW_AT_stmt_list: 1080 if ( verbose ) s.PutCString(" ( "); 1081 s.Printf( "0x%8.8" PRIx64, form_value.Unsigned()); 1082 if ( verbose ) s.PutCString(" )"); 1083 break; 1084 1085 case DW_AT_language: 1086 if ( verbose ) s.PutCString(" ( "); 1087 s.PutCString(DW_LANG_value_to_name(form_value.Unsigned())); 1088 if ( verbose ) s.PutCString(" )"); 1089 break; 1090 1091 case DW_AT_encoding: 1092 if ( verbose ) s.PutCString(" ( "); 1093 s.PutCString(DW_ATE_value_to_name(form_value.Unsigned())); 1094 if ( verbose ) s.PutCString(" )"); 1095 break; 1096 1097 case DW_AT_frame_base: 1098 case DW_AT_location: 1099 case DW_AT_data_member_location: 1100 { 1101 const uint8_t* blockData = form_value.BlockData(); 1102 if (blockData) 1103 { 1104 if (!verbose) 1105 form_value.Dump(s, debug_str_data, cu); 1106 1107 // Location description is inlined in data in the form value 1108 DataExtractor locationData(debug_info_data, (*offset_ptr) - form_value.Unsigned(), form_value.Unsigned()); 1109 if ( verbose ) s.PutCString(" ( "); 1110 print_dwarf_expression (s, locationData, DWARFCompileUnit::GetAddressByteSize(cu), 4, false); 1111 if ( verbose ) s.PutCString(" )"); 1112 } 1113 else 1114 { 1115 // We have a location list offset as the value that is 1116 // the offset into the .debug_loc section that describes 1117 // the value over it's lifetime 1118 uint64_t debug_loc_offset = form_value.Unsigned(); 1119 if (dwarf2Data) 1120 { 1121 if ( !verbose ) 1122 form_value.Dump(s, debug_str_data, cu); 1123 DWARFLocationList::Dump(s, cu, dwarf2Data->get_debug_loc_data(), debug_loc_offset); 1124 } 1125 else 1126 { 1127 if ( !verbose ) 1128 form_value.Dump(s, NULL, cu); 1129 } 1130 } 1131 } 1132 break; 1133 1134 case DW_AT_abstract_origin: 1135 case DW_AT_specification: 1136 { 1137 uint64_t abstract_die_offset = form_value.Reference(cu); 1138 form_value.Dump(s, debug_str_data, cu); 1139 // *ostrm_ptr << HEX32 << abstract_die_offset << " ( "; 1140 if ( verbose ) s.PutCString(" ( "); 1141 GetName(dwarf2Data, cu, abstract_die_offset, s); 1142 if ( verbose ) s.PutCString(" )"); 1143 } 1144 break; 1145 1146 case DW_AT_type: 1147 { 1148 uint64_t type_die_offset = form_value.Reference(cu); 1149 if (!verbose) 1150 form_value.Dump(s, debug_str_data, cu); 1151 s.PutCString(" ( "); 1152 AppendTypeName(dwarf2Data, cu, type_die_offset, s); 1153 s.PutCString(" )"); 1154 } 1155 break; 1156 1157 case DW_AT_ranges: 1158 { 1159 if ( !verbose ) 1160 form_value.Dump(s, debug_str_data, cu); 1161 lldb::offset_t ranges_offset = form_value.Unsigned(); 1162 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0; 1163 if (dwarf2Data) 1164 DWARFDebugRanges::Dump(s, dwarf2Data->get_debug_ranges_data(), &ranges_offset, base_addr); 1165 } 1166 break; 1167 1168 default: 1169 if ( !verbose ) 1170 form_value.Dump(s, debug_str_data, cu); 1171 break; 1172 } 1173 1174 s.PutCString(" )\n"); 1175 } 1176 1177 //---------------------------------------------------------------------- 1178 // Get all attribute values for a given DIE, including following any 1179 // specification or abstract origin attributes and including those in 1180 // the results. Any duplicate attributes will have the first instance 1181 // take precedence (this can happen for declaration attributes). 1182 //---------------------------------------------------------------------- 1183 size_t 1184 DWARFDebugInfoEntry::GetAttributes 1185 ( 1186 SymbolFileDWARF* dwarf2Data, 1187 const DWARFCompileUnit* cu, 1188 const uint8_t *fixed_form_sizes, 1189 DWARFDebugInfoEntry::Attributes& attributes, 1190 uint32_t curr_depth 1191 ) const 1192 { 1193 lldb::offset_t offset; 1194 const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset); 1195 1196 if (abbrevDecl) 1197 { 1198 const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data(); 1199 1200 if (fixed_form_sizes == NULL) 1201 fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize(cu->GetAddressByteSize()); 1202 1203 const uint32_t num_attributes = abbrevDecl->NumAttributes(); 1204 uint32_t i; 1205 dw_attr_t attr; 1206 dw_form_t form; 1207 DWARFFormValue form_value; 1208 for (i=0; i<num_attributes; ++i) 1209 { 1210 abbrevDecl->GetAttrAndFormByIndexUnchecked (i, attr, form); 1211 1212 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin 1213 // attributes, the depth will be non-zero. We need to omit certain 1214 // attributes that don't make sense. 1215 switch (attr) 1216 { 1217 case DW_AT_sibling: 1218 case DW_AT_declaration: 1219 if (curr_depth > 0) 1220 { 1221 // This attribute doesn't make sense when combined with 1222 // the DIE that references this DIE. We know a DIE is 1223 // referencing this DIE because curr_depth is not zero 1224 break; 1225 } 1226 // Fall through... 1227 default: 1228 attributes.Append(cu, offset, attr, form); 1229 break; 1230 } 1231 1232 if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) 1233 { 1234 form_value.SetForm(form); 1235 if (form_value.ExtractValue(debug_info_data, &offset, cu)) 1236 { 1237 const DWARFDebugInfoEntry* die = NULL; 1238 dw_offset_t die_offset = form_value.Reference(cu); 1239 if (cu->ContainsDIEOffset(die_offset)) 1240 { 1241 die = const_cast<DWARFCompileUnit*>(cu)->GetDIEPtr(die_offset); 1242 if (die) 1243 die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes, curr_depth + 1); 1244 } 1245 else 1246 { 1247 DWARFCompileUnitSP cu_sp_ptr; 1248 die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(die_offset, &cu_sp_ptr); 1249 if (die) 1250 die->GetAttributes(dwarf2Data, cu_sp_ptr.get(), fixed_form_sizes, attributes, curr_depth + 1); 1251 } 1252 } 1253 } 1254 else 1255 { 1256 const uint8_t fixed_skip_size = fixed_form_sizes [form]; 1257 if (fixed_skip_size) 1258 offset += fixed_skip_size; 1259 else 1260 DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu); 1261 } 1262 } 1263 } 1264 else 1265 { 1266 attributes.Clear(); 1267 } 1268 return attributes.Size(); 1269 1270 } 1271 1272 //---------------------------------------------------------------------- 1273 // GetAttributeValue 1274 // 1275 // Get the value of an attribute and return the .debug_info offset of the 1276 // attribute if it was properly extracted into form_value, or zero 1277 // if we fail since an offset of zero is invalid for an attribute (it 1278 // would be a compile unit header). 1279 //---------------------------------------------------------------------- 1280 dw_offset_t 1281 DWARFDebugInfoEntry::GetAttributeValue 1282 ( 1283 SymbolFileDWARF* dwarf2Data, 1284 const DWARFCompileUnit* cu, 1285 const dw_attr_t attr, 1286 DWARFFormValue& form_value, 1287 dw_offset_t* end_attr_offset_ptr 1288 ) const 1289 { 1290 lldb::offset_t offset; 1291 const DWARFAbbreviationDeclaration* abbrevDecl = GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset); 1292 1293 if (abbrevDecl) 1294 { 1295 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr); 1296 1297 if (attr_idx != DW_INVALID_INDEX) 1298 { 1299 const DataExtractor& debug_info_data = dwarf2Data->get_debug_info_data(); 1300 1301 uint32_t idx=0; 1302 while (idx<attr_idx) 1303 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), debug_info_data, &offset, cu); 1304 1305 const dw_offset_t attr_offset = offset; 1306 form_value.SetForm(abbrevDecl->GetFormByIndex(idx)); 1307 if (form_value.ExtractValue(debug_info_data, &offset, cu)) 1308 { 1309 if (end_attr_offset_ptr) 1310 *end_attr_offset_ptr = offset; 1311 return attr_offset; 1312 } 1313 } 1314 } 1315 1316 return 0; 1317 } 1318 1319 //---------------------------------------------------------------------- 1320 // GetAttributeValueAsString 1321 // 1322 // Get the value of an attribute as a string return it. The resulting 1323 // pointer to the string data exists within the supplied SymbolFileDWARF 1324 // and will only be available as long as the SymbolFileDWARF is still around 1325 // and it's content doesn't change. 1326 //---------------------------------------------------------------------- 1327 const char* 1328 DWARFDebugInfoEntry::GetAttributeValueAsString 1329 ( 1330 SymbolFileDWARF* dwarf2Data, 1331 const DWARFCompileUnit* cu, 1332 const dw_attr_t attr, 1333 const char* fail_value) const 1334 { 1335 DWARFFormValue form_value; 1336 if (GetAttributeValue(dwarf2Data, cu, attr, form_value)) 1337 return form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1338 return fail_value; 1339 } 1340 1341 //---------------------------------------------------------------------- 1342 // GetAttributeValueAsUnsigned 1343 // 1344 // Get the value of an attribute as unsigned and return it. 1345 //---------------------------------------------------------------------- 1346 uint64_t 1347 DWARFDebugInfoEntry::GetAttributeValueAsUnsigned 1348 ( 1349 SymbolFileDWARF* dwarf2Data, 1350 const DWARFCompileUnit* cu, 1351 const dw_attr_t attr, 1352 uint64_t fail_value 1353 ) const 1354 { 1355 DWARFFormValue form_value; 1356 if (GetAttributeValue(dwarf2Data, cu, attr, form_value)) 1357 return form_value.Unsigned(); 1358 return fail_value; 1359 } 1360 1361 //---------------------------------------------------------------------- 1362 // GetAttributeValueAsSigned 1363 // 1364 // Get the value of an attribute a signed value and return it. 1365 //---------------------------------------------------------------------- 1366 int64_t 1367 DWARFDebugInfoEntry::GetAttributeValueAsSigned 1368 ( 1369 SymbolFileDWARF* dwarf2Data, 1370 const DWARFCompileUnit* cu, 1371 const dw_attr_t attr, 1372 int64_t fail_value 1373 ) const 1374 { 1375 DWARFFormValue form_value; 1376 if (GetAttributeValue(dwarf2Data, cu, attr, form_value)) 1377 return form_value.Signed(); 1378 return fail_value; 1379 } 1380 1381 //---------------------------------------------------------------------- 1382 // GetAttributeValueAsReference 1383 // 1384 // Get the value of an attribute as reference and fix up and compile 1385 // unit relative offsets as needed. 1386 //---------------------------------------------------------------------- 1387 uint64_t 1388 DWARFDebugInfoEntry::GetAttributeValueAsReference 1389 ( 1390 SymbolFileDWARF* dwarf2Data, 1391 const DWARFCompileUnit* cu, 1392 const dw_attr_t attr, 1393 uint64_t fail_value 1394 ) const 1395 { 1396 DWARFFormValue form_value; 1397 if (GetAttributeValue(dwarf2Data, cu, attr, form_value)) 1398 return form_value.Reference(cu); 1399 return fail_value; 1400 } 1401 1402 //---------------------------------------------------------------------- 1403 // GetAttributeValueAsLocation 1404 // 1405 // Get the value of an attribute as reference and fix up and compile 1406 // unit relative offsets as needed. 1407 //---------------------------------------------------------------------- 1408 dw_offset_t 1409 DWARFDebugInfoEntry::GetAttributeValueAsLocation 1410 ( 1411 SymbolFileDWARF* dwarf2Data, 1412 const DWARFCompileUnit* cu, 1413 const dw_attr_t attr, 1414 DataExtractor& location_data, 1415 uint32_t &block_size 1416 ) const 1417 { 1418 block_size = 0; 1419 DWARFFormValue form_value; 1420 1421 // Empty out data in case we don't find anything 1422 location_data.Clear(); 1423 dw_offset_t end_addr_offset = DW_INVALID_OFFSET; 1424 const dw_offset_t attr_offset = GetAttributeValue(dwarf2Data, cu, attr, form_value, &end_addr_offset); 1425 if (attr_offset) 1426 { 1427 const uint8_t* blockData = form_value.BlockData(); 1428 if (blockData) 1429 { 1430 // We have an inlined location list in the .debug_info section 1431 const DataExtractor& debug_info = dwarf2Data->get_debug_info_data(); 1432 dw_offset_t block_offset = blockData - debug_info.GetDataStart(); 1433 block_size = (end_addr_offset - attr_offset) - form_value.Unsigned(); 1434 location_data.SetData(debug_info, block_offset, block_size); 1435 } 1436 else 1437 { 1438 // We have a location list offset as the value that is 1439 // the offset into the .debug_loc section that describes 1440 // the value over it's lifetime 1441 lldb::offset_t debug_loc_offset = form_value.Unsigned(); 1442 if (dwarf2Data) 1443 { 1444 assert(dwarf2Data->get_debug_loc_data().GetAddressByteSize() == cu->GetAddressByteSize()); 1445 return DWARFLocationList::Extract(dwarf2Data->get_debug_loc_data(), &debug_loc_offset, location_data); 1446 } 1447 } 1448 } 1449 return attr_offset; 1450 } 1451 1452 //---------------------------------------------------------------------- 1453 // GetName 1454 // 1455 // Get value of the DW_AT_name attribute and return it if one exists, 1456 // else return NULL. 1457 //---------------------------------------------------------------------- 1458 const char* 1459 DWARFDebugInfoEntry::GetName 1460 ( 1461 SymbolFileDWARF* dwarf2Data, 1462 const DWARFCompileUnit* cu 1463 ) const 1464 { 1465 DWARFFormValue form_value; 1466 if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value)) 1467 return form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1468 return NULL; 1469 } 1470 1471 1472 //---------------------------------------------------------------------- 1473 // GetMangledName 1474 // 1475 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if 1476 // one exists, else return the value of the DW_AT_name attribute 1477 //---------------------------------------------------------------------- 1478 const char* 1479 DWARFDebugInfoEntry::GetMangledName 1480 ( 1481 SymbolFileDWARF* dwarf2Data, 1482 const DWARFCompileUnit* cu, 1483 bool substitute_name_allowed 1484 ) const 1485 { 1486 const char* name = NULL; 1487 DWARFFormValue form_value; 1488 1489 if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value)) 1490 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1491 1492 if (GetAttributeValue(dwarf2Data, cu, DW_AT_linkage_name, form_value)) 1493 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1494 1495 if (substitute_name_allowed && name == NULL) 1496 { 1497 if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value)) 1498 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1499 } 1500 return name; 1501 } 1502 1503 1504 //---------------------------------------------------------------------- 1505 // GetPubname 1506 // 1507 // Get value the name for a DIE as it should appear for a 1508 // .debug_pubnames or .debug_pubtypes section. 1509 //---------------------------------------------------------------------- 1510 const char* 1511 DWARFDebugInfoEntry::GetPubname 1512 ( 1513 SymbolFileDWARF* dwarf2Data, 1514 const DWARFCompileUnit* cu 1515 ) const 1516 { 1517 const char* name = NULL; 1518 if (!dwarf2Data) 1519 return name; 1520 1521 DWARFFormValue form_value; 1522 1523 if (GetAttributeValue(dwarf2Data, cu, DW_AT_MIPS_linkage_name, form_value)) 1524 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1525 else if (GetAttributeValue(dwarf2Data, cu, DW_AT_linkage_name, form_value)) 1526 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1527 else if (GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value)) 1528 name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1529 else if (GetAttributeValue(dwarf2Data, cu, DW_AT_specification, form_value)) 1530 { 1531 // The specification DIE may be in another compile unit so we need 1532 // to get a die and its compile unit. 1533 DWARFCompileUnitSP cu_sp_ptr; 1534 const DWARFDebugInfoEntry* die = const_cast<SymbolFileDWARF*>(dwarf2Data)->DebugInfo()->GetDIEPtr(form_value.Reference(cu), &cu_sp_ptr); 1535 if (die) 1536 return die->GetPubname(dwarf2Data, cu_sp_ptr.get()); 1537 } 1538 return name; 1539 } 1540 1541 1542 //---------------------------------------------------------------------- 1543 // GetName 1544 // 1545 // Get value of the DW_AT_name attribute for a debug information entry 1546 // that exists at offset "die_offset" and place that value into the 1547 // supplied stream object. If the DIE is a NULL object "NULL" is placed 1548 // into the stream, and if no DW_AT_name attribute exists for the DIE 1549 // then nothing is printed. 1550 //---------------------------------------------------------------------- 1551 bool 1552 DWARFDebugInfoEntry::GetName 1553 ( 1554 SymbolFileDWARF* dwarf2Data, 1555 const DWARFCompileUnit* cu, 1556 const dw_offset_t die_offset, 1557 Stream &s 1558 ) 1559 { 1560 if (dwarf2Data == NULL) 1561 { 1562 s.PutCString("NULL"); 1563 return false; 1564 } 1565 1566 DWARFDebugInfoEntry die; 1567 lldb::offset_t offset = die_offset; 1568 if (die.Extract(dwarf2Data, cu, &offset)) 1569 { 1570 if (die.IsNULL()) 1571 { 1572 s.PutCString("NULL"); 1573 return true; 1574 } 1575 else 1576 { 1577 DWARFFormValue form_value; 1578 if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value)) 1579 { 1580 const char* name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1581 if (name) 1582 { 1583 s.PutCString(name); 1584 return true; 1585 } 1586 } 1587 } 1588 } 1589 return false; 1590 } 1591 1592 //---------------------------------------------------------------------- 1593 // AppendTypeName 1594 // 1595 // Follows the type name definition down through all needed tags to 1596 // end up with a fully qualified type name and dump the results to 1597 // the supplied stream. This is used to show the name of types given 1598 // a type identifier. 1599 //---------------------------------------------------------------------- 1600 bool 1601 DWARFDebugInfoEntry::AppendTypeName 1602 ( 1603 SymbolFileDWARF* dwarf2Data, 1604 const DWARFCompileUnit* cu, 1605 const dw_offset_t die_offset, 1606 Stream &s 1607 ) 1608 { 1609 if (dwarf2Data == NULL) 1610 { 1611 s.PutCString("NULL"); 1612 return false; 1613 } 1614 1615 DWARFDebugInfoEntry die; 1616 lldb::offset_t offset = die_offset; 1617 if (die.Extract(dwarf2Data, cu, &offset)) 1618 { 1619 if (die.IsNULL()) 1620 { 1621 s.PutCString("NULL"); 1622 return true; 1623 } 1624 else 1625 { 1626 const char* name = die.GetPubname(dwarf2Data, cu); 1627 // if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_name, form_value)) 1628 // name = form_value.AsCString(&dwarf2Data->get_debug_str_data()); 1629 if (name) 1630 s.PutCString(name); 1631 else 1632 { 1633 bool result = true; 1634 const DWARFAbbreviationDeclaration* abbrevDecl = die.GetAbbreviationDeclarationPtr(dwarf2Data, cu, offset); 1635 1636 if (abbrevDecl == NULL) 1637 return false; 1638 1639 switch (abbrevDecl->Tag()) 1640 { 1641 case DW_TAG_array_type: break; // print out a "[]" after printing the full type of the element below 1642 case DW_TAG_base_type: s.PutCString("base "); break; 1643 case DW_TAG_class_type: s.PutCString("class "); break; 1644 case DW_TAG_const_type: s.PutCString("const "); break; 1645 case DW_TAG_enumeration_type: s.PutCString("enum "); break; 1646 case DW_TAG_file_type: s.PutCString("file "); break; 1647 case DW_TAG_interface_type: s.PutCString("interface "); break; 1648 case DW_TAG_packed_type: s.PutCString("packed "); break; 1649 case DW_TAG_pointer_type: break; // print out a '*' after printing the full type below 1650 case DW_TAG_ptr_to_member_type: break; // print out a '*' after printing the full type below 1651 case DW_TAG_reference_type: break; // print out a '&' after printing the full type below 1652 case DW_TAG_restrict_type: s.PutCString("restrict "); break; 1653 case DW_TAG_set_type: s.PutCString("set "); break; 1654 case DW_TAG_shared_type: s.PutCString("shared "); break; 1655 case DW_TAG_string_type: s.PutCString("string "); break; 1656 case DW_TAG_structure_type: s.PutCString("struct "); break; 1657 case DW_TAG_subrange_type: s.PutCString("subrange "); break; 1658 case DW_TAG_subroutine_type: s.PutCString("function "); break; 1659 case DW_TAG_thrown_type: s.PutCString("thrown "); break; 1660 case DW_TAG_union_type: s.PutCString("union "); break; 1661 case DW_TAG_unspecified_type: s.PutCString("unspecified "); break; 1662 case DW_TAG_volatile_type: s.PutCString("volatile "); break; 1663 default: 1664 return false; 1665 } 1666 1667 // Follow the DW_AT_type if possible 1668 DWARFFormValue form_value; 1669 if (die.GetAttributeValue(dwarf2Data, cu, DW_AT_type, form_value)) 1670 { 1671 uint64_t next_die_offset = form_value.Reference(cu); 1672 result = AppendTypeName(dwarf2Data, cu, next_die_offset, s); 1673 } 1674 1675 switch (abbrevDecl->Tag()) 1676 { 1677 case DW_TAG_array_type: s.PutCString("[]"); break; 1678 case DW_TAG_pointer_type: s.PutChar('*'); break; 1679 case DW_TAG_ptr_to_member_type: s.PutChar('*'); break; 1680 case DW_TAG_reference_type: s.PutChar('&'); break; 1681 default: 1682 break; 1683 } 1684 return result; 1685 } 1686 } 1687 } 1688 return false; 1689 } 1690 1691 bool 1692 DWARFDebugInfoEntry::Contains (const DWARFDebugInfoEntry *die) const 1693 { 1694 if (die) 1695 { 1696 const dw_offset_t die_offset = die->GetOffset(); 1697 if (die_offset > GetOffset()) 1698 { 1699 const DWARFDebugInfoEntry *sibling = GetSibling(); 1700 assert (sibling); // TODO: take this out 1701 if (sibling) 1702 return die_offset < sibling->GetOffset(); 1703 } 1704 } 1705 return false; 1706 } 1707 1708 //---------------------------------------------------------------------- 1709 // BuildAddressRangeTable 1710 //---------------------------------------------------------------------- 1711 void 1712 DWARFDebugInfoEntry::BuildAddressRangeTable 1713 ( 1714 SymbolFileDWARF* dwarf2Data, 1715 const DWARFCompileUnit* cu, 1716 DWARFDebugAranges* debug_aranges 1717 ) const 1718 { 1719 if (m_tag) 1720 { 1721 if (m_tag == DW_TAG_subprogram) 1722 { 1723 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 1724 dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 1725 if (lo_pc != LLDB_INVALID_ADDRESS) 1726 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, LLDB_INVALID_ADDRESS); 1727 if (hi_pc != LLDB_INVALID_ADDRESS) 1728 { 1729 /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x - 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc); 1730 debug_aranges->AppendRange (cu->GetOffset(), lo_pc, hi_pc); 1731 } 1732 } 1733 1734 1735 const DWARFDebugInfoEntry* child = GetFirstChild(); 1736 while (child) 1737 { 1738 child->BuildAddressRangeTable(dwarf2Data, cu, debug_aranges); 1739 child = child->GetSibling(); 1740 } 1741 } 1742 } 1743 1744 //---------------------------------------------------------------------- 1745 // BuildFunctionAddressRangeTable 1746 // 1747 // This function is very similar to the BuildAddressRangeTable function 1748 // except that the actual DIE offset for the function is placed in the 1749 // table instead of the compile unit offset (which is the way the 1750 // standard .debug_aranges section does it). 1751 //---------------------------------------------------------------------- 1752 void 1753 DWARFDebugInfoEntry::BuildFunctionAddressRangeTable 1754 ( 1755 SymbolFileDWARF* dwarf2Data, 1756 const DWARFCompileUnit* cu, 1757 DWARFDebugAranges* debug_aranges 1758 ) const 1759 { 1760 if (m_tag) 1761 { 1762 if (m_tag == DW_TAG_subprogram) 1763 { 1764 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 1765 dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 1766 if (lo_pc != LLDB_INVALID_ADDRESS) 1767 hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, LLDB_INVALID_ADDRESS); 1768 if (hi_pc != LLDB_INVALID_ADDRESS) 1769 { 1770 // printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY 1771 debug_aranges->AppendRange (GetOffset(), lo_pc, hi_pc); 1772 } 1773 } 1774 1775 const DWARFDebugInfoEntry* child = GetFirstChild(); 1776 while (child) 1777 { 1778 child->BuildFunctionAddressRangeTable(dwarf2Data, cu, debug_aranges); 1779 child = child->GetSibling(); 1780 } 1781 } 1782 } 1783 1784 void 1785 DWARFDebugInfoEntry::GetDeclContextDIEs (SymbolFileDWARF* dwarf2Data, 1786 DWARFCompileUnit* cu, 1787 DWARFDIECollection &decl_context_dies) const 1788 { 1789 const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu); 1790 if (parent_decl_ctx_die && parent_decl_ctx_die != this) 1791 { 1792 decl_context_dies.Append(parent_decl_ctx_die); 1793 parent_decl_ctx_die->GetDeclContextDIEs (dwarf2Data, cu, decl_context_dies); 1794 } 1795 } 1796 1797 void 1798 DWARFDebugInfoEntry::GetDWARFDeclContext (SymbolFileDWARF* dwarf2Data, 1799 DWARFCompileUnit* cu, 1800 DWARFDeclContext &dwarf_decl_ctx) const 1801 { 1802 const dw_tag_t tag = Tag(); 1803 if (tag != DW_TAG_compile_unit) 1804 { 1805 dwarf_decl_ctx.AppendDeclContext(tag, GetName(dwarf2Data, cu)); 1806 const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu); 1807 if (parent_decl_ctx_die && parent_decl_ctx_die != this) 1808 { 1809 if (parent_decl_ctx_die->Tag() != DW_TAG_compile_unit) 1810 parent_decl_ctx_die->GetDWARFDeclContext (dwarf2Data, cu, dwarf_decl_ctx); 1811 } 1812 } 1813 } 1814 1815 1816 bool 1817 DWARFDebugInfoEntry::MatchesDWARFDeclContext (SymbolFileDWARF* dwarf2Data, 1818 DWARFCompileUnit* cu, 1819 const DWARFDeclContext &dwarf_decl_ctx) const 1820 { 1821 1822 DWARFDeclContext this_dwarf_decl_ctx; 1823 GetDWARFDeclContext (dwarf2Data, cu, this_dwarf_decl_ctx); 1824 return this_dwarf_decl_ctx == dwarf_decl_ctx; 1825 } 1826 1827 const DWARFDebugInfoEntry * 1828 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data, 1829 DWARFCompileUnit* cu) const 1830 { 1831 DWARFDebugInfoEntry::Attributes attributes; 1832 GetAttributes(dwarf2Data, cu, NULL, attributes); 1833 return GetParentDeclContextDIE (dwarf2Data, cu, attributes); 1834 } 1835 1836 const DWARFDebugInfoEntry * 1837 DWARFDebugInfoEntry::GetParentDeclContextDIE (SymbolFileDWARF* dwarf2Data, 1838 DWARFCompileUnit* cu, 1839 const DWARFDebugInfoEntry::Attributes& attributes) const 1840 { 1841 const DWARFDebugInfoEntry * die = this; 1842 1843 while (die != NULL) 1844 { 1845 // If this is the original DIE that we are searching for a declaration 1846 // for, then don't look in the cache as we don't want our own decl 1847 // context to be our decl context... 1848 if (die != this) 1849 { 1850 switch (die->Tag()) 1851 { 1852 case DW_TAG_compile_unit: 1853 case DW_TAG_namespace: 1854 case DW_TAG_structure_type: 1855 case DW_TAG_union_type: 1856 case DW_TAG_class_type: 1857 return die; 1858 1859 default: 1860 break; 1861 } 1862 } 1863 1864 dw_offset_t die_offset; 1865 1866 die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_specification, DW_INVALID_OFFSET); 1867 if (die_offset != DW_INVALID_OFFSET) 1868 { 1869 const DWARFDebugInfoEntry *spec_die = cu->GetDIEPtr (die_offset); 1870 if (spec_die) 1871 { 1872 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = spec_die->GetParentDeclContextDIE (dwarf2Data, cu); 1873 if (spec_die_decl_ctx_die) 1874 return spec_die_decl_ctx_die; 1875 } 1876 } 1877 1878 die_offset = attributes.FormValueAsUnsigned(dwarf2Data, DW_AT_abstract_origin, DW_INVALID_OFFSET); 1879 if (die_offset != DW_INVALID_OFFSET) 1880 { 1881 const DWARFDebugInfoEntry *abs_die = cu->GetDIEPtr (die_offset); 1882 if (abs_die) 1883 { 1884 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = abs_die->GetParentDeclContextDIE (dwarf2Data, cu); 1885 if (abs_die_decl_ctx_die) 1886 return abs_die_decl_ctx_die; 1887 } 1888 } 1889 1890 die = die->GetParent(); 1891 } 1892 return NULL; 1893 } 1894 1895 1896 const char * 1897 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data, 1898 DWARFCompileUnit* cu, 1899 std::string &storage) const 1900 { 1901 DWARFDebugInfoEntry::Attributes attributes; 1902 GetAttributes(dwarf2Data, cu, NULL, attributes); 1903 return GetQualifiedName (dwarf2Data, cu, attributes, storage); 1904 } 1905 1906 const char* 1907 DWARFDebugInfoEntry::GetQualifiedName (SymbolFileDWARF* dwarf2Data, 1908 DWARFCompileUnit* cu, 1909 const DWARFDebugInfoEntry::Attributes& attributes, 1910 std::string &storage) const 1911 { 1912 1913 const char *name = GetName (dwarf2Data, cu); 1914 1915 if (name) 1916 { 1917 const DWARFDebugInfoEntry *parent_decl_ctx_die = GetParentDeclContextDIE (dwarf2Data, cu); 1918 storage.clear(); 1919 // TODO: change this to get the correct decl context parent.... 1920 while (parent_decl_ctx_die) 1921 { 1922 const dw_tag_t parent_tag = parent_decl_ctx_die->Tag(); 1923 switch (parent_tag) 1924 { 1925 case DW_TAG_namespace: 1926 { 1927 const char *namespace_name = parent_decl_ctx_die->GetName (dwarf2Data, cu); 1928 if (namespace_name) 1929 { 1930 storage.insert (0, "::"); 1931 storage.insert (0, namespace_name); 1932 } 1933 else 1934 { 1935 storage.insert (0, "(anonymous namespace)::"); 1936 } 1937 parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu); 1938 } 1939 break; 1940 1941 case DW_TAG_class_type: 1942 case DW_TAG_structure_type: 1943 case DW_TAG_union_type: 1944 { 1945 const char *class_union_struct_name = parent_decl_ctx_die->GetName (dwarf2Data, cu); 1946 1947 if (class_union_struct_name) 1948 { 1949 storage.insert (0, "::"); 1950 storage.insert (0, class_union_struct_name); 1951 } 1952 parent_decl_ctx_die = parent_decl_ctx_die->GetParentDeclContextDIE(dwarf2Data, cu); 1953 } 1954 break; 1955 1956 default: 1957 parent_decl_ctx_die = NULL; 1958 break; 1959 } 1960 } 1961 1962 if (storage.empty()) 1963 storage.append ("::"); 1964 1965 storage.append (name); 1966 } 1967 if (storage.empty()) 1968 return NULL; 1969 return storage.c_str(); 1970 } 1971 1972 1973 //---------------------------------------------------------------------- 1974 // LookupAddress 1975 //---------------------------------------------------------------------- 1976 bool 1977 DWARFDebugInfoEntry::LookupAddress 1978 ( 1979 const dw_addr_t address, 1980 SymbolFileDWARF* dwarf2Data, 1981 const DWARFCompileUnit* cu, 1982 DWARFDebugInfoEntry** function_die, 1983 DWARFDebugInfoEntry** block_die 1984 ) 1985 { 1986 bool found_address = false; 1987 if (m_tag) 1988 { 1989 bool check_children = false; 1990 bool match_addr_range = false; 1991 // printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, DW_TAG_value_to_name(tag), address); 1992 switch (m_tag) 1993 { 1994 case DW_TAG_array_type : break; 1995 case DW_TAG_class_type : check_children = true; break; 1996 case DW_TAG_entry_point : break; 1997 case DW_TAG_enumeration_type : break; 1998 case DW_TAG_formal_parameter : break; 1999 case DW_TAG_imported_declaration : break; 2000 case DW_TAG_label : break; 2001 case DW_TAG_lexical_block : check_children = true; match_addr_range = true; break; 2002 case DW_TAG_member : break; 2003 case DW_TAG_pointer_type : break; 2004 case DW_TAG_reference_type : break; 2005 case DW_TAG_compile_unit : match_addr_range = true; break; 2006 case DW_TAG_string_type : break; 2007 case DW_TAG_structure_type : check_children = true; break; 2008 case DW_TAG_subroutine_type : break; 2009 case DW_TAG_typedef : break; 2010 case DW_TAG_union_type : break; 2011 case DW_TAG_unspecified_parameters : break; 2012 case DW_TAG_variant : break; 2013 case DW_TAG_common_block : check_children = true; break; 2014 case DW_TAG_common_inclusion : break; 2015 case DW_TAG_inheritance : break; 2016 case DW_TAG_inlined_subroutine : check_children = true; match_addr_range = true; break; 2017 case DW_TAG_module : match_addr_range = true; break; 2018 case DW_TAG_ptr_to_member_type : break; 2019 case DW_TAG_set_type : break; 2020 case DW_TAG_subrange_type : break; 2021 case DW_TAG_with_stmt : break; 2022 case DW_TAG_access_declaration : break; 2023 case DW_TAG_base_type : break; 2024 case DW_TAG_catch_block : match_addr_range = true; break; 2025 case DW_TAG_const_type : break; 2026 case DW_TAG_constant : break; 2027 case DW_TAG_enumerator : break; 2028 case DW_TAG_file_type : break; 2029 case DW_TAG_friend : break; 2030 case DW_TAG_namelist : break; 2031 case DW_TAG_namelist_item : break; 2032 case DW_TAG_packed_type : break; 2033 case DW_TAG_subprogram : match_addr_range = true; break; 2034 case DW_TAG_template_type_parameter : break; 2035 case DW_TAG_template_value_parameter : break; 2036 case DW_TAG_thrown_type : break; 2037 case DW_TAG_try_block : match_addr_range = true; break; 2038 case DW_TAG_variant_part : break; 2039 case DW_TAG_variable : break; 2040 case DW_TAG_volatile_type : break; 2041 case DW_TAG_dwarf_procedure : break; 2042 case DW_TAG_restrict_type : break; 2043 case DW_TAG_interface_type : break; 2044 case DW_TAG_namespace : check_children = true; break; 2045 case DW_TAG_imported_module : break; 2046 case DW_TAG_unspecified_type : break; 2047 case DW_TAG_partial_unit : break; 2048 case DW_TAG_imported_unit : break; 2049 case DW_TAG_shared_type : break; 2050 default: break; 2051 } 2052 2053 if (match_addr_range) 2054 { 2055 dw_addr_t lo_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 2056 if (lo_pc != LLDB_INVALID_ADDRESS) 2057 { 2058 dw_addr_t hi_pc = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_high_pc, LLDB_INVALID_ADDRESS); 2059 if (hi_pc != LLDB_INVALID_ADDRESS) 2060 { 2061 // printf("\n0x%8.8x: %30s: address = 0x%8.8x [0x%8.8x - 0x%8.8x) ", m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc); 2062 if ((lo_pc <= address) && (address < hi_pc)) 2063 { 2064 found_address = true; 2065 // puts("***MATCH***"); 2066 switch (m_tag) 2067 { 2068 case DW_TAG_compile_unit: // File 2069 check_children = ((function_die != NULL) || (block_die != NULL)); 2070 break; 2071 2072 case DW_TAG_subprogram: // Function 2073 if (function_die) 2074 *function_die = this; 2075 check_children = (block_die != NULL); 2076 break; 2077 2078 case DW_TAG_inlined_subroutine: // Inlined Function 2079 case DW_TAG_lexical_block: // Block { } in code 2080 if (block_die) 2081 { 2082 *block_die = this; 2083 check_children = true; 2084 } 2085 break; 2086 2087 default: 2088 check_children = true; 2089 break; 2090 } 2091 } 2092 } 2093 else 2094 { // compile units may not have a valid high/low pc when there 2095 // are address gaps in subroutines so we must always search 2096 // if there is no valid high and low PC 2097 check_children = (m_tag == DW_TAG_compile_unit) && ((function_die != NULL) || (block_die != NULL)); 2098 } 2099 } 2100 else 2101 { 2102 dw_offset_t debug_ranges_offset = GetAttributeValueAsUnsigned(dwarf2Data, cu, DW_AT_ranges, DW_INVALID_OFFSET); 2103 if (debug_ranges_offset != DW_INVALID_OFFSET) 2104 { 2105 DWARFDebugRanges::RangeList ranges; 2106 DWARFDebugRanges* debug_ranges = dwarf2Data->DebugRanges(); 2107 debug_ranges->FindRanges(debug_ranges_offset, ranges); 2108 // All DW_AT_ranges are relative to the base address of the 2109 // compile unit. We add the compile unit base address to make 2110 // sure all the addresses are properly fixed up. 2111 ranges.Slide (cu->GetBaseAddress()); 2112 if (ranges.FindEntryThatContains(address)) 2113 { 2114 found_address = true; 2115 // puts("***MATCH***"); 2116 switch (m_tag) 2117 { 2118 case DW_TAG_compile_unit: // File 2119 check_children = ((function_die != NULL) || (block_die != NULL)); 2120 break; 2121 2122 case DW_TAG_subprogram: // Function 2123 if (function_die) 2124 *function_die = this; 2125 check_children = (block_die != NULL); 2126 break; 2127 2128 case DW_TAG_inlined_subroutine: // Inlined Function 2129 case DW_TAG_lexical_block: // Block { } in code 2130 if (block_die) 2131 { 2132 *block_die = this; 2133 check_children = true; 2134 } 2135 break; 2136 2137 default: 2138 check_children = true; 2139 break; 2140 } 2141 } 2142 else 2143 { 2144 check_children = false; 2145 } 2146 } 2147 } 2148 } 2149 2150 2151 if (check_children) 2152 { 2153 // printf("checking children\n"); 2154 DWARFDebugInfoEntry* child = GetFirstChild(); 2155 while (child) 2156 { 2157 if (child->LookupAddress(address, dwarf2Data, cu, function_die, block_die)) 2158 return true; 2159 child = child->GetSibling(); 2160 } 2161 } 2162 } 2163 return found_address; 2164 } 2165 2166 const DWARFAbbreviationDeclaration* 2167 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr (SymbolFileDWARF* dwarf2Data, 2168 const DWARFCompileUnit *cu, 2169 lldb::offset_t &offset) const 2170 { 2171 if (dwarf2Data) 2172 { 2173 offset = GetOffset(); 2174 2175 const DWARFAbbreviationDeclaration* abbrev_decl = cu->GetAbbreviations()->GetAbbreviationDeclaration (m_abbr_idx); 2176 if (abbrev_decl) 2177 { 2178 // Make sure the abbreviation code still matches. If it doesn't and 2179 // the DWARF data was mmap'ed, the backing file might have been modified 2180 // which is bad news. 2181 const uint64_t abbrev_code = dwarf2Data->get_debug_info_data().GetULEB128 (&offset); 2182 2183 if (abbrev_decl->Code() == abbrev_code) 2184 return abbrev_decl; 2185 2186 dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("0x%8.8x: the DWARF debug information has been modified (abbrev code was %u, and is now %u)", 2187 GetOffset(), 2188 (uint32_t)abbrev_decl->Code(), 2189 (uint32_t)abbrev_code); 2190 } 2191 } 2192 offset = DW_INVALID_OFFSET; 2193 return NULL; 2194 } 2195 2196 2197 bool 2198 DWARFDebugInfoEntry::OffsetLessThan (const DWARFDebugInfoEntry& a, const DWARFDebugInfoEntry& b) 2199 { 2200 return a.GetOffset() < b.GetOffset(); 2201 } 2202 2203 void 2204 DWARFDebugInfoEntry::DumpDIECollection (Stream &strm, DWARFDebugInfoEntry::collection &die_collection) 2205 { 2206 DWARFDebugInfoEntry::const_iterator pos; 2207 DWARFDebugInfoEntry::const_iterator end = die_collection.end(); 2208 strm.PutCString("\noffset parent sibling child\n"); 2209 strm.PutCString("-------- -------- -------- --------\n"); 2210 for (pos = die_collection.begin(); pos != end; ++pos) 2211 { 2212 const DWARFDebugInfoEntry& die_ref = *pos; 2213 const DWARFDebugInfoEntry* p = die_ref.GetParent(); 2214 const DWARFDebugInfoEntry* s = die_ref.GetSibling(); 2215 const DWARFDebugInfoEntry* c = die_ref.GetFirstChild(); 2216 strm.Printf("%.8x: %.8x %.8x %.8x 0x%4.4x %s%s\n", 2217 die_ref.GetOffset(), 2218 p ? p->GetOffset() : 0, 2219 s ? s->GetOffset() : 0, 2220 c ? c->GetOffset() : 0, 2221 die_ref.Tag(), 2222 DW_TAG_value_to_name(die_ref.Tag()), 2223 die_ref.HasChildren() ? " *" : ""); 2224 } 2225 } 2226 2227 2228