1 //===-- DWARFDebugInfoEntry.cpp ---------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "DWARFDebugInfoEntry.h" 10 11 #include <assert.h> 12 13 #include <algorithm> 14 15 #include "lldb/Core/Module.h" 16 #include "lldb/Expression/DWARFExpression.h" 17 #include "lldb/Symbol/ObjectFile.h" 18 #include "lldb/Utility/Stream.h" 19 20 #include "DWARFUnit.h" 21 #include "DWARFDebugAbbrev.h" 22 #include "DWARFDebugAranges.h" 23 #include "DWARFDebugInfo.h" 24 #include "DWARFDebugRanges.h" 25 #include "DWARFDeclContext.h" 26 #include "DWARFFormValue.h" 27 #include "SymbolFileDWARF.h" 28 #include "SymbolFileDWARFDwo.h" 29 30 using namespace lldb_private; 31 using namespace std; 32 extern int g_verbose; 33 34 bool DWARFDebugInfoEntry::FastExtract( 35 const DWARFDataExtractor &debug_info_data, const DWARFUnit *cu, 36 lldb::offset_t *offset_ptr) { 37 m_offset = *offset_ptr; 38 m_parent_idx = 0; 39 m_sibling_idx = 0; 40 const uint64_t abbr_idx = debug_info_data.GetULEB128(offset_ptr); 41 lldbassert(abbr_idx <= UINT16_MAX); 42 m_abbr_idx = abbr_idx; 43 44 // assert (fixed_form_sizes); // For best performance this should be 45 // specified! 46 47 if (m_abbr_idx) { 48 lldb::offset_t offset = *offset_ptr; 49 50 const DWARFAbbreviationDeclaration *abbrevDecl = 51 cu->GetAbbreviations()->GetAbbreviationDeclaration(m_abbr_idx); 52 53 if (abbrevDecl == nullptr) { 54 cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError( 55 "{0x%8.8x}: invalid abbreviation code %u, please file a bug and " 56 "attach the file at the start of this error message", 57 m_offset, (unsigned)abbr_idx); 58 // WE can't parse anymore if the DWARF is borked... 59 *offset_ptr = UINT32_MAX; 60 return false; 61 } 62 m_tag = abbrevDecl->Tag(); 63 m_has_children = abbrevDecl->HasChildren(); 64 // Skip all data in the .debug_info for the attributes 65 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 66 uint32_t i; 67 dw_form_t form; 68 for (i = 0; i < numAttributes; ++i) { 69 form = abbrevDecl->GetFormByIndexUnchecked(i); 70 71 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu); 72 if (fixed_skip_size) 73 offset += *fixed_skip_size; 74 else { 75 bool form_is_indirect = false; 76 do { 77 form_is_indirect = false; 78 uint32_t form_size = 0; 79 switch (form) { 80 // Blocks if inlined data that have a length field and the data bytes 81 // inlined in the .debug_info 82 case DW_FORM_exprloc: 83 case DW_FORM_block: 84 form_size = debug_info_data.GetULEB128(&offset); 85 break; 86 case DW_FORM_block1: 87 form_size = debug_info_data.GetU8_unchecked(&offset); 88 break; 89 case DW_FORM_block2: 90 form_size = debug_info_data.GetU16_unchecked(&offset); 91 break; 92 case DW_FORM_block4: 93 form_size = debug_info_data.GetU32_unchecked(&offset); 94 break; 95 96 // Inlined NULL terminated C-strings 97 case DW_FORM_string: 98 debug_info_data.GetCStr(&offset); 99 break; 100 101 // Compile unit address sized values 102 case DW_FORM_addr: 103 form_size = cu->GetAddressByteSize(); 104 break; 105 case DW_FORM_ref_addr: 106 if (cu->GetVersion() <= 2) 107 form_size = cu->GetAddressByteSize(); 108 else 109 form_size = 4; 110 break; 111 112 // 0 sized form 113 case DW_FORM_flag_present: 114 form_size = 0; 115 break; 116 117 // 1 byte values 118 case DW_FORM_addrx1: 119 case DW_FORM_data1: 120 case DW_FORM_flag: 121 case DW_FORM_ref1: 122 case DW_FORM_strx1: 123 form_size = 1; 124 break; 125 126 // 2 byte values 127 case DW_FORM_addrx2: 128 case DW_FORM_data2: 129 case DW_FORM_ref2: 130 case DW_FORM_strx2: 131 form_size = 2; 132 break; 133 134 // 3 byte values 135 case DW_FORM_addrx3: 136 case DW_FORM_strx3: 137 form_size = 3; 138 break; 139 140 // 4 byte values 141 case DW_FORM_addrx4: 142 case DW_FORM_data4: 143 case DW_FORM_ref4: 144 case DW_FORM_strx4: 145 form_size = 4; 146 break; 147 148 // 8 byte values 149 case DW_FORM_data8: 150 case DW_FORM_ref8: 151 case DW_FORM_ref_sig8: 152 form_size = 8; 153 break; 154 155 // signed or unsigned LEB 128 values 156 case DW_FORM_addrx: 157 case DW_FORM_rnglistx: 158 case DW_FORM_sdata: 159 case DW_FORM_udata: 160 case DW_FORM_ref_udata: 161 case DW_FORM_GNU_addr_index: 162 case DW_FORM_GNU_str_index: 163 case DW_FORM_strx: 164 debug_info_data.Skip_LEB128(&offset); 165 break; 166 167 case DW_FORM_indirect: 168 form_is_indirect = true; 169 form = debug_info_data.GetULEB128(&offset); 170 break; 171 172 case DW_FORM_strp: 173 case DW_FORM_sec_offset: 174 debug_info_data.GetU32(&offset); 175 break; 176 177 case DW_FORM_implicit_const: 178 form_size = 0; 179 break; 180 181 default: 182 *offset_ptr = m_offset; 183 return false; 184 } 185 offset += form_size; 186 187 } while (form_is_indirect); 188 } 189 } 190 *offset_ptr = offset; 191 return true; 192 } else { 193 m_tag = 0; 194 m_has_children = false; 195 return true; // NULL debug tag entry 196 } 197 198 return false; 199 } 200 201 // Extract 202 // 203 // Extract a debug info entry for a given compile unit from the .debug_info and 204 // .debug_abbrev data within the SymbolFileDWARF class starting at the given 205 // offset 206 bool DWARFDebugInfoEntry::Extract(const DWARFUnit *cu, 207 lldb::offset_t *offset_ptr) { 208 const DWARFDataExtractor &debug_info_data = cu->GetData(); 209 // const DWARFDataExtractor& debug_str_data = 210 // dwarf2Data->get_debug_str_data(); 211 const uint32_t cu_end_offset = cu->GetNextUnitOffset(); 212 lldb::offset_t offset = *offset_ptr; 213 // if (offset >= cu_end_offset) 214 // Log::Status("DIE at offset 0x%8.8x is beyond the end of the current 215 // compile unit (0x%8.8x)", m_offset, cu_end_offset); 216 if ((offset < cu_end_offset) && debug_info_data.ValidOffset(offset)) { 217 m_offset = offset; 218 219 const uint64_t abbr_idx = debug_info_data.GetULEB128(&offset); 220 lldbassert(abbr_idx <= UINT16_MAX); 221 m_abbr_idx = abbr_idx; 222 if (abbr_idx) { 223 const DWARFAbbreviationDeclaration *abbrevDecl = 224 cu->GetAbbreviations()->GetAbbreviationDeclaration(abbr_idx); 225 226 if (abbrevDecl) { 227 m_tag = abbrevDecl->Tag(); 228 m_has_children = abbrevDecl->HasChildren(); 229 230 bool isCompileUnitTag = (m_tag == DW_TAG_compile_unit || 231 m_tag == DW_TAG_partial_unit); 232 if (cu && isCompileUnitTag) 233 const_cast<DWARFUnit *>(cu)->SetBaseAddress(0); 234 235 // Skip all data in the .debug_info for the attributes 236 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 237 for (uint32_t i = 0; i < numAttributes; ++i) { 238 DWARFFormValue form_value(cu); 239 dw_attr_t attr; 240 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 241 dw_form_t form = form_value.Form(); 242 243 if (isCompileUnitTag && 244 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) { 245 if (form_value.ExtractValue(debug_info_data, &offset)) { 246 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc) 247 const_cast<DWARFUnit *>(cu)->SetBaseAddress( 248 form_value.Address()); 249 } 250 } else { 251 bool form_is_indirect = false; 252 do { 253 form_is_indirect = false; 254 uint32_t form_size = 0; 255 switch (form) { 256 // Blocks if inlined data that have a length field and the data 257 // bytes inlined in the .debug_info 258 case DW_FORM_exprloc: 259 case DW_FORM_block: 260 form_size = debug_info_data.GetULEB128(&offset); 261 break; 262 case DW_FORM_block1: 263 form_size = debug_info_data.GetU8(&offset); 264 break; 265 case DW_FORM_block2: 266 form_size = debug_info_data.GetU16(&offset); 267 break; 268 case DW_FORM_block4: 269 form_size = debug_info_data.GetU32(&offset); 270 break; 271 272 // Inlined NULL terminated C-strings 273 case DW_FORM_string: 274 debug_info_data.GetCStr(&offset); 275 break; 276 277 // Compile unit address sized values 278 case DW_FORM_addr: 279 form_size = cu->GetAddressByteSize(); 280 break; 281 case DW_FORM_ref_addr: 282 if (cu->GetVersion() <= 2) 283 form_size = cu->GetAddressByteSize(); 284 else 285 form_size = 4; 286 break; 287 288 // 0 sized form 289 case DW_FORM_flag_present: 290 case DW_FORM_implicit_const: 291 form_size = 0; 292 break; 293 294 // 1 byte values 295 case DW_FORM_data1: 296 case DW_FORM_flag: 297 case DW_FORM_ref1: 298 form_size = 1; 299 break; 300 301 // 2 byte values 302 case DW_FORM_data2: 303 case DW_FORM_ref2: 304 form_size = 2; 305 break; 306 307 // 4 byte values 308 case DW_FORM_data4: 309 case DW_FORM_ref4: 310 form_size = 4; 311 break; 312 313 // 8 byte values 314 case DW_FORM_data8: 315 case DW_FORM_ref8: 316 case DW_FORM_ref_sig8: 317 form_size = 8; 318 break; 319 320 // signed or unsigned LEB 128 values 321 case DW_FORM_addrx: 322 case DW_FORM_sdata: 323 case DW_FORM_udata: 324 case DW_FORM_ref_udata: 325 case DW_FORM_GNU_addr_index: 326 case DW_FORM_GNU_str_index: 327 debug_info_data.Skip_LEB128(&offset); 328 break; 329 330 case DW_FORM_indirect: 331 form = debug_info_data.GetULEB128(&offset); 332 form_is_indirect = true; 333 break; 334 335 case DW_FORM_strp: 336 case DW_FORM_sec_offset: 337 debug_info_data.GetU32(&offset); 338 break; 339 340 default: 341 *offset_ptr = offset; 342 return false; 343 } 344 345 offset += form_size; 346 } while (form_is_indirect); 347 } 348 } 349 *offset_ptr = offset; 350 return true; 351 } 352 } else { 353 m_tag = 0; 354 m_has_children = false; 355 *offset_ptr = offset; 356 return true; // NULL debug tag entry 357 } 358 } 359 360 return false; 361 } 362 363 static DWARFRangeList GetRangesOrReportError(const DWARFUnit &unit, 364 const DWARFDebugInfoEntry &die, 365 const DWARFFormValue &value) { 366 llvm::Expected<DWARFRangeList> expected_ranges = 367 (value.Form() == DW_FORM_rnglistx) 368 ? unit.FindRnglistFromIndex(value.Unsigned()) 369 : unit.FindRnglistFromOffset(value.Unsigned()); 370 if (expected_ranges) 371 return std::move(*expected_ranges); 372 unit.GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError( 373 "{0x%8.8x}: DIE has DW_AT_ranges(0x%" PRIx64 ") attribute, but " 374 "range extraction failed (%s), please file a bug " 375 "and attach the file at the start of this error message", 376 die.GetOffset(), value.Unsigned(), 377 toString(expected_ranges.takeError()).c_str()); 378 return DWARFRangeList(); 379 } 380 381 // GetDIENamesAndRanges 382 // 383 // Gets the valid address ranges for a given DIE by looking for a 384 // DW_AT_low_pc/DW_AT_high_pc pair, DW_AT_entry_pc, or DW_AT_ranges attributes. 385 bool DWARFDebugInfoEntry::GetDIENamesAndRanges( 386 const DWARFUnit *cu, const char *&name, const char *&mangled, 387 DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, 388 int &call_file, int &call_line, int &call_column, 389 DWARFExpression *frame_base) const { 390 SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile(); 391 if (dwo_symbol_file) 392 return GetDIENamesAndRanges( 393 dwo_symbol_file->GetCompileUnit(), name, mangled, ranges, decl_file, 394 decl_line, decl_column, call_file, call_line, call_column, frame_base); 395 396 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 397 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 398 std::vector<DIERef> die_refs; 399 bool set_frame_base_loclist_addr = false; 400 401 lldb::offset_t offset; 402 const DWARFAbbreviationDeclaration *abbrevDecl = 403 GetAbbreviationDeclarationPtr(cu, offset); 404 405 SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF(); 406 lldb::ModuleSP module = dwarf2Data->GetObjectFile()->GetModule(); 407 408 if (abbrevDecl) { 409 const DWARFDataExtractor &debug_info_data = cu->GetData(); 410 411 if (!debug_info_data.ValidOffset(offset)) 412 return false; 413 414 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 415 bool do_offset = false; 416 417 for (uint32_t i = 0; i < numAttributes; ++i) { 418 DWARFFormValue form_value(cu); 419 dw_attr_t attr; 420 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 421 422 if (form_value.ExtractValue(debug_info_data, &offset)) { 423 switch (attr) { 424 case DW_AT_low_pc: 425 lo_pc = form_value.Address(); 426 427 if (do_offset) 428 hi_pc += lo_pc; 429 do_offset = false; 430 break; 431 432 case DW_AT_entry_pc: 433 lo_pc = form_value.Address(); 434 break; 435 436 case DW_AT_high_pc: 437 if (form_value.Form() == DW_FORM_addr || 438 form_value.Form() == DW_FORM_addrx || 439 form_value.Form() == DW_FORM_GNU_addr_index) { 440 hi_pc = form_value.Address(); 441 } else { 442 hi_pc = form_value.Unsigned(); 443 if (lo_pc == LLDB_INVALID_ADDRESS) 444 do_offset = hi_pc != LLDB_INVALID_ADDRESS; 445 else 446 hi_pc += lo_pc; // DWARF 4 introduces <offset-from-lo-pc> to save 447 // on relocations 448 } 449 break; 450 451 case DW_AT_ranges: 452 ranges = GetRangesOrReportError(*cu, *this, form_value); 453 break; 454 455 case DW_AT_name: 456 if (name == nullptr) 457 name = form_value.AsCString(); 458 break; 459 460 case DW_AT_MIPS_linkage_name: 461 case DW_AT_linkage_name: 462 if (mangled == nullptr) 463 mangled = form_value.AsCString(); 464 break; 465 466 case DW_AT_abstract_origin: 467 die_refs.emplace_back(form_value); 468 break; 469 470 case DW_AT_specification: 471 die_refs.emplace_back(form_value); 472 break; 473 474 case DW_AT_decl_file: 475 if (decl_file == 0) 476 decl_file = form_value.Unsigned(); 477 break; 478 479 case DW_AT_decl_line: 480 if (decl_line == 0) 481 decl_line = form_value.Unsigned(); 482 break; 483 484 case DW_AT_decl_column: 485 if (decl_column == 0) 486 decl_column = form_value.Unsigned(); 487 break; 488 489 case DW_AT_call_file: 490 if (call_file == 0) 491 call_file = form_value.Unsigned(); 492 break; 493 494 case DW_AT_call_line: 495 if (call_line == 0) 496 call_line = form_value.Unsigned(); 497 break; 498 499 case DW_AT_call_column: 500 if (call_column == 0) 501 call_column = form_value.Unsigned(); 502 break; 503 504 case DW_AT_frame_base: 505 if (frame_base) { 506 if (form_value.BlockData()) { 507 uint32_t block_offset = 508 form_value.BlockData() - debug_info_data.GetDataStart(); 509 uint32_t block_length = form_value.Unsigned(); 510 *frame_base = DWARFExpression(module, debug_info_data, cu, 511 block_offset, block_length); 512 } else { 513 const DWARFDataExtractor &debug_loc_data = 514 dwarf2Data->DebugLocData(); 515 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 516 517 size_t loc_list_length = DWARFExpression::LocationListSize( 518 cu, debug_loc_data, debug_loc_offset); 519 if (loc_list_length > 0) { 520 *frame_base = 521 DWARFExpression(module, debug_loc_data, cu, 522 debug_loc_offset, loc_list_length); 523 if (lo_pc != LLDB_INVALID_ADDRESS) { 524 assert(lo_pc >= cu->GetBaseAddress()); 525 frame_base->SetLocationListSlide(lo_pc - 526 cu->GetBaseAddress()); 527 } else { 528 set_frame_base_loclist_addr = true; 529 } 530 } 531 } 532 } 533 break; 534 535 default: 536 break; 537 } 538 } 539 } 540 } 541 542 if (ranges.IsEmpty()) { 543 if (lo_pc != LLDB_INVALID_ADDRESS) { 544 if (hi_pc != LLDB_INVALID_ADDRESS && hi_pc > lo_pc) 545 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 546 else 547 ranges.Append(DWARFRangeList::Entry(lo_pc, 0)); 548 } 549 } 550 551 if (set_frame_base_loclist_addr) { 552 dw_addr_t lowest_range_pc = ranges.GetMinRangeBase(0); 553 assert(lowest_range_pc >= cu->GetBaseAddress()); 554 frame_base->SetLocationListSlide(lowest_range_pc - cu->GetBaseAddress()); 555 } 556 557 if (ranges.IsEmpty() || name == nullptr || mangled == nullptr) { 558 for (const DIERef &die_ref : die_refs) { 559 if (die_ref.die_offset != DW_INVALID_OFFSET) { 560 DWARFDIE die = dwarf2Data->GetDIE(die_ref); 561 if (die) 562 die.GetDIE()->GetDIENamesAndRanges(die.GetCU(), name, mangled, ranges, 563 decl_file, decl_line, decl_column, 564 call_file, call_line, call_column); 565 } 566 } 567 } 568 return !ranges.IsEmpty(); 569 } 570 571 // Dump 572 // 573 // Dumps a debug information entry and all of it's attributes to the specified 574 // stream. 575 void DWARFDebugInfoEntry::Dump(const DWARFUnit *cu, Stream &s, 576 uint32_t recurse_depth) const { 577 const DWARFDataExtractor &debug_info_data = cu->GetData(); 578 lldb::offset_t offset = m_offset; 579 580 if (debug_info_data.ValidOffset(offset)) { 581 dw_uleb128_t abbrCode = debug_info_data.GetULEB128(&offset); 582 583 s.Printf("\n0x%8.8x: ", m_offset); 584 s.Indent(); 585 if (abbrCode != m_abbr_idx) { 586 s.Printf("error: DWARF has been modified\n"); 587 } else if (abbrCode) { 588 const DWARFAbbreviationDeclaration *abbrevDecl = 589 cu->GetAbbreviations()->GetAbbreviationDeclaration(abbrCode); 590 591 if (abbrevDecl) { 592 s.PutCString(DW_TAG_value_to_name(abbrevDecl->Tag())); 593 s.Printf(" [%u] %c\n", abbrCode, abbrevDecl->HasChildren() ? '*' : ' '); 594 595 // Dump all data in the .debug_info for the attributes 596 const uint32_t numAttributes = abbrevDecl->NumAttributes(); 597 for (uint32_t i = 0; i < numAttributes; ++i) { 598 DWARFFormValue form_value(cu); 599 dw_attr_t attr; 600 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 601 602 DumpAttribute(cu, debug_info_data, &offset, s, attr, form_value); 603 } 604 605 const DWARFDebugInfoEntry *child = GetFirstChild(); 606 if (recurse_depth > 0 && child) { 607 s.IndentMore(); 608 609 while (child) { 610 child->Dump(cu, s, recurse_depth - 1); 611 child = child->GetSibling(); 612 } 613 s.IndentLess(); 614 } 615 } else 616 s.Printf("Abbreviation code note found in 'debug_abbrev' class for " 617 "code: %u\n", 618 abbrCode); 619 } else { 620 s.Printf("NULL\n"); 621 } 622 } 623 } 624 625 // DumpAttribute 626 // 627 // Dumps a debug information entry attribute along with it's form. Any special 628 // display of attributes is done (disassemble location lists, show enumeration 629 // values for attributes, etc). 630 void DWARFDebugInfoEntry::DumpAttribute( 631 const DWARFUnit *cu, const DWARFDataExtractor &debug_info_data, 632 lldb::offset_t *offset_ptr, Stream &s, dw_attr_t attr, 633 DWARFFormValue &form_value) { 634 bool show_form = s.GetFlags().Test(DWARFDebugInfo::eDumpFlag_ShowForm); 635 636 s.Printf(" "); 637 s.Indent(DW_AT_value_to_name(attr)); 638 639 if (show_form) { 640 s.Printf("[%s", DW_FORM_value_to_name(form_value.Form())); 641 } 642 643 if (!form_value.ExtractValue(debug_info_data, offset_ptr)) 644 return; 645 646 if (show_form) { 647 if (form_value.Form() == DW_FORM_indirect) { 648 s.Printf(" [%s]", DW_FORM_value_to_name(form_value.Form())); 649 } 650 651 s.PutCString("] "); 652 } 653 654 s.PutCString("( "); 655 656 SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF(); 657 658 // Check to see if we have any special attribute formatters 659 switch (attr) { 660 case DW_AT_stmt_list: 661 s.Printf("0x%8.8" PRIx64, form_value.Unsigned()); 662 break; 663 664 case DW_AT_language: 665 s.PutCString(DW_LANG_value_to_name(form_value.Unsigned())); 666 break; 667 668 case DW_AT_encoding: 669 s.PutCString(DW_ATE_value_to_name(form_value.Unsigned())); 670 break; 671 672 case DW_AT_frame_base: 673 case DW_AT_location: 674 case DW_AT_data_member_location: { 675 const uint8_t *blockData = form_value.BlockData(); 676 if (blockData) { 677 // Location description is inlined in data in the form value 678 DWARFDataExtractor locationData(debug_info_data, 679 (*offset_ptr) - form_value.Unsigned(), 680 form_value.Unsigned()); 681 DWARFExpression::PrintDWARFExpression( 682 s, locationData, DWARFUnit::GetAddressByteSize(cu), 4, false); 683 } else { 684 // We have a location list offset as the value that is the offset into 685 // the .debug_loc section that describes the value over it's lifetime 686 uint64_t debug_loc_offset = form_value.Unsigned(); 687 if (dwarf2Data) { 688 DWARFExpression::PrintDWARFLocationList( 689 s, cu, dwarf2Data->DebugLocData(), debug_loc_offset); 690 } 691 } 692 } break; 693 694 case DW_AT_abstract_origin: 695 case DW_AT_specification: { 696 DWARFDIE abstract_die = form_value.Reference(); 697 form_value.Dump(s); 698 // *ostrm_ptr << HEX32 << abstract_die.GetOffset() << " ( "; 699 abstract_die.GetName(s); 700 } break; 701 702 case DW_AT_type: { 703 DWARFDIE type_die = form_value.Reference(); 704 s.PutCString(" ( "); 705 type_die.AppendTypeName(s); 706 s.PutCString(" )"); 707 } break; 708 709 default: 710 break; 711 } 712 713 s.PutCString(" )\n"); 714 } 715 716 // Get all attribute values for a given DIE, including following any 717 // specification or abstract origin attributes and including those in the 718 // results. Any duplicate attributes will have the first instance take 719 // precedence (this can happen for declaration attributes). 720 size_t DWARFDebugInfoEntry::GetAttributes( 721 const DWARFUnit *cu, DWARFAttributes &attributes, 722 uint32_t curr_depth) const { 723 const DWARFAbbreviationDeclaration *abbrevDecl = nullptr; 724 lldb::offset_t offset = 0; 725 if (cu) 726 abbrevDecl = GetAbbreviationDeclarationPtr(cu, offset); 727 728 if (abbrevDecl) { 729 const DWARFDataExtractor &debug_info_data = cu->GetData(); 730 731 const uint32_t num_attributes = abbrevDecl->NumAttributes(); 732 for (uint32_t i = 0; i < num_attributes; ++i) { 733 DWARFFormValue form_value(cu); 734 dw_attr_t attr; 735 abbrevDecl->GetAttrAndFormValueByIndex(i, attr, form_value); 736 const dw_form_t form = form_value.Form(); 737 738 // If we are tracking down DW_AT_specification or DW_AT_abstract_origin 739 // attributes, the depth will be non-zero. We need to omit certain 740 // attributes that don't make sense. 741 switch (attr) { 742 case DW_AT_sibling: 743 case DW_AT_declaration: 744 if (curr_depth > 0) { 745 // This attribute doesn't make sense when combined with the DIE that 746 // references this DIE. We know a DIE is referencing this DIE because 747 // curr_depth is not zero 748 break; 749 } 750 LLVM_FALLTHROUGH; 751 default: 752 attributes.Append(cu, offset, attr, form); 753 break; 754 } 755 756 if ((attr == DW_AT_specification) || (attr == DW_AT_abstract_origin)) { 757 if (form_value.ExtractValue(debug_info_data, &offset)) { 758 DWARFDIE spec_die = form_value.Reference(); 759 if (spec_die) 760 spec_die.GetAttributes(attributes, curr_depth + 1); 761 } 762 } else { 763 llvm::Optional<uint8_t> fixed_skip_size = DWARFFormValue::GetFixedSize(form, cu); 764 if (fixed_skip_size) 765 offset += *fixed_skip_size; 766 else 767 DWARFFormValue::SkipValue(form, debug_info_data, &offset, cu); 768 } 769 } 770 } else { 771 attributes.Clear(); 772 } 773 return attributes.Size(); 774 } 775 776 // GetAttributeValue 777 // 778 // Get the value of an attribute and return the .debug_info offset of the 779 // attribute if it was properly extracted into form_value, or zero if we fail 780 // since an offset of zero is invalid for an attribute (it would be a compile 781 // unit header). 782 dw_offset_t DWARFDebugInfoEntry::GetAttributeValue( 783 const DWARFUnit *cu, const dw_attr_t attr, DWARFFormValue &form_value, 784 dw_offset_t *end_attr_offset_ptr, 785 bool check_specification_or_abstract_origin) const { 786 SymbolFileDWARFDwo *dwo_symbol_file = cu->GetDwoSymbolFile(); 787 if (dwo_symbol_file && m_tag != DW_TAG_compile_unit && 788 m_tag != DW_TAG_partial_unit) 789 return GetAttributeValue(dwo_symbol_file->GetCompileUnit(), attr, 790 form_value, end_attr_offset_ptr, 791 check_specification_or_abstract_origin); 792 793 lldb::offset_t offset; 794 const DWARFAbbreviationDeclaration *abbrevDecl = 795 GetAbbreviationDeclarationPtr(cu, offset); 796 797 if (abbrevDecl) { 798 uint32_t attr_idx = abbrevDecl->FindAttributeIndex(attr); 799 800 if (attr_idx != DW_INVALID_INDEX) { 801 const DWARFDataExtractor &debug_info_data = cu->GetData(); 802 803 uint32_t idx = 0; 804 while (idx < attr_idx) 805 DWARFFormValue::SkipValue(abbrevDecl->GetFormByIndex(idx++), 806 debug_info_data, &offset, cu); 807 808 const dw_offset_t attr_offset = offset; 809 form_value.SetUnit(cu); 810 form_value.SetForm(abbrevDecl->GetFormByIndex(idx)); 811 if (form_value.ExtractValue(debug_info_data, &offset)) { 812 if (end_attr_offset_ptr) 813 *end_attr_offset_ptr = offset; 814 return attr_offset; 815 } 816 } 817 } 818 819 if (check_specification_or_abstract_origin) { 820 if (GetAttributeValue(cu, DW_AT_specification, form_value)) { 821 DWARFDIE die = form_value.Reference(); 822 if (die) { 823 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 824 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 825 if (die_offset) 826 return die_offset; 827 } 828 } 829 830 if (GetAttributeValue(cu, DW_AT_abstract_origin, form_value)) { 831 DWARFDIE die = form_value.Reference(); 832 if (die) { 833 dw_offset_t die_offset = die.GetDIE()->GetAttributeValue( 834 die.GetCU(), attr, form_value, end_attr_offset_ptr, false); 835 if (die_offset) 836 return die_offset; 837 } 838 } 839 } 840 841 if (!dwo_symbol_file) 842 return 0; 843 844 DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit(); 845 if (!dwo_cu) 846 return 0; 847 848 DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); 849 if (!dwo_cu_die.IsValid()) 850 return 0; 851 852 return dwo_cu_die.GetDIE()->GetAttributeValue( 853 dwo_cu, attr, form_value, end_attr_offset_ptr, 854 check_specification_or_abstract_origin); 855 } 856 857 // GetAttributeValueAsString 858 // 859 // Get the value of an attribute as a string return it. The resulting pointer 860 // to the string data exists within the supplied SymbolFileDWARF and will only 861 // be available as long as the SymbolFileDWARF is still around and it's content 862 // doesn't change. 863 const char *DWARFDebugInfoEntry::GetAttributeValueAsString( 864 const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value, 865 bool check_specification_or_abstract_origin) const { 866 DWARFFormValue form_value; 867 if (GetAttributeValue(cu, attr, form_value, nullptr, 868 check_specification_or_abstract_origin)) 869 return form_value.AsCString(); 870 return fail_value; 871 } 872 873 // GetAttributeValueAsUnsigned 874 // 875 // Get the value of an attribute as unsigned and return it. 876 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsUnsigned( 877 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 878 bool check_specification_or_abstract_origin) const { 879 DWARFFormValue form_value; 880 if (GetAttributeValue(cu, attr, form_value, nullptr, 881 check_specification_or_abstract_origin)) 882 return form_value.Unsigned(); 883 return fail_value; 884 } 885 886 // GetAttributeValueAsReference 887 // 888 // Get the value of an attribute as reference and fix up and compile unit 889 // relative offsets as needed. 890 DWARFDIE DWARFDebugInfoEntry::GetAttributeValueAsReference( 891 const DWARFUnit *cu, const dw_attr_t attr, 892 bool check_specification_or_abstract_origin) const { 893 DWARFFormValue form_value; 894 if (GetAttributeValue(cu, attr, form_value, nullptr, 895 check_specification_or_abstract_origin)) 896 return form_value.Reference(); 897 return {}; 898 } 899 900 uint64_t DWARFDebugInfoEntry::GetAttributeValueAsAddress( 901 const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value, 902 bool check_specification_or_abstract_origin) const { 903 DWARFFormValue form_value; 904 if (GetAttributeValue(cu, attr, form_value, nullptr, 905 check_specification_or_abstract_origin)) 906 return form_value.Address(); 907 return fail_value; 908 } 909 910 // GetAttributeHighPC 911 // 912 // Get the hi_pc, adding hi_pc to lo_pc when specified as an <offset-from-low- 913 // pc>. 914 // 915 // Returns the hi_pc or fail_value. 916 dw_addr_t DWARFDebugInfoEntry::GetAttributeHighPC( 917 const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value, 918 bool check_specification_or_abstract_origin) const { 919 DWARFFormValue form_value; 920 if (GetAttributeValue(cu, DW_AT_high_pc, form_value, nullptr, 921 check_specification_or_abstract_origin)) { 922 dw_form_t form = form_value.Form(); 923 if (form == DW_FORM_addr || form == DW_FORM_addrx || 924 form == DW_FORM_GNU_addr_index) 925 return form_value.Address(); 926 927 // DWARF4 can specify the hi_pc as an <offset-from-lowpc> 928 return lo_pc + form_value.Unsigned(); 929 } 930 return fail_value; 931 } 932 933 // GetAttributeAddressRange 934 // 935 // Get the lo_pc and hi_pc, adding hi_pc to lo_pc when specified as an <offset- 936 // from-low-pc>. 937 // 938 // Returns true or sets lo_pc and hi_pc to fail_value. 939 bool DWARFDebugInfoEntry::GetAttributeAddressRange( 940 const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc, 941 uint64_t fail_value, bool check_specification_or_abstract_origin) const { 942 lo_pc = GetAttributeValueAsAddress(cu, DW_AT_low_pc, fail_value, 943 check_specification_or_abstract_origin); 944 if (lo_pc != fail_value) { 945 hi_pc = GetAttributeHighPC(cu, lo_pc, fail_value, 946 check_specification_or_abstract_origin); 947 if (hi_pc != fail_value) 948 return true; 949 } 950 lo_pc = fail_value; 951 hi_pc = fail_value; 952 return false; 953 } 954 955 size_t DWARFDebugInfoEntry::GetAttributeAddressRanges( 956 const DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc, 957 bool check_specification_or_abstract_origin) const { 958 ranges.Clear(); 959 960 DWARFFormValue form_value; 961 if (GetAttributeValue(cu, DW_AT_ranges, form_value)) { 962 ranges = GetRangesOrReportError(*cu, *this, form_value); 963 } else if (check_hi_lo_pc) { 964 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 965 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 966 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS, 967 check_specification_or_abstract_origin)) { 968 if (lo_pc < hi_pc) 969 ranges.Append(DWARFRangeList::Entry(lo_pc, hi_pc - lo_pc)); 970 } 971 } 972 return ranges.GetSize(); 973 } 974 975 // GetName 976 // 977 // Get value of the DW_AT_name attribute and return it if one exists, else 978 // return NULL. 979 const char *DWARFDebugInfoEntry::GetName(const DWARFUnit *cu) const { 980 return GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 981 } 982 983 // GetMangledName 984 // 985 // Get value of the DW_AT_MIPS_linkage_name attribute and return it if one 986 // exists, else return the value of the DW_AT_name attribute 987 const char * 988 DWARFDebugInfoEntry::GetMangledName(const DWARFUnit *cu, 989 bool substitute_name_allowed) const { 990 const char *name = nullptr; 991 992 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 993 if (name) 994 return name; 995 996 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 997 if (name) 998 return name; 999 1000 if (!substitute_name_allowed) 1001 return nullptr; 1002 1003 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 1004 return name; 1005 } 1006 1007 // GetPubname 1008 // 1009 // Get value the name for a DIE as it should appear for a .debug_pubnames or 1010 // .debug_pubtypes section. 1011 const char *DWARFDebugInfoEntry::GetPubname(const DWARFUnit *cu) const { 1012 const char *name = nullptr; 1013 if (!cu) 1014 return name; 1015 1016 name = GetAttributeValueAsString(cu, DW_AT_MIPS_linkage_name, nullptr, true); 1017 if (name) 1018 return name; 1019 1020 name = GetAttributeValueAsString(cu, DW_AT_linkage_name, nullptr, true); 1021 if (name) 1022 return name; 1023 1024 name = GetAttributeValueAsString(cu, DW_AT_name, nullptr, true); 1025 return name; 1026 } 1027 1028 // BuildAddressRangeTable 1029 void DWARFDebugInfoEntry::BuildAddressRangeTable( 1030 const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 1031 if (m_tag) { 1032 if (m_tag == DW_TAG_subprogram) { 1033 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 1034 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 1035 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) { 1036 /// printf("BuildAddressRangeTable() 0x%8.8x: %30s: [0x%8.8x - 1037 /// 0x%8.8x)\n", m_offset, DW_TAG_value_to_name(tag), lo_pc, hi_pc); 1038 debug_aranges->AppendRange(cu->GetOffset(), lo_pc, hi_pc); 1039 } 1040 } 1041 1042 const DWARFDebugInfoEntry *child = GetFirstChild(); 1043 while (child) { 1044 child->BuildAddressRangeTable(cu, debug_aranges); 1045 child = child->GetSibling(); 1046 } 1047 } 1048 } 1049 1050 // BuildFunctionAddressRangeTable 1051 // 1052 // This function is very similar to the BuildAddressRangeTable function except 1053 // that the actual DIE offset for the function is placed in the table instead 1054 // of the compile unit offset (which is the way the standard .debug_aranges 1055 // section does it). 1056 void DWARFDebugInfoEntry::BuildFunctionAddressRangeTable( 1057 const DWARFUnit *cu, DWARFDebugAranges *debug_aranges) const { 1058 if (m_tag) { 1059 if (m_tag == DW_TAG_subprogram) { 1060 dw_addr_t lo_pc = LLDB_INVALID_ADDRESS; 1061 dw_addr_t hi_pc = LLDB_INVALID_ADDRESS; 1062 if (GetAttributeAddressRange(cu, lo_pc, hi_pc, LLDB_INVALID_ADDRESS)) { 1063 // printf("BuildAddressRangeTable() 0x%8.8x: [0x%16.16" PRIx64 " - 1064 // 0x%16.16" PRIx64 ")\n", m_offset, lo_pc, hi_pc); // DEBUG ONLY 1065 debug_aranges->AppendRange(GetOffset(), lo_pc, hi_pc); 1066 } 1067 } 1068 1069 const DWARFDebugInfoEntry *child = GetFirstChild(); 1070 while (child) { 1071 child->BuildFunctionAddressRangeTable(cu, debug_aranges); 1072 child = child->GetSibling(); 1073 } 1074 } 1075 } 1076 1077 void DWARFDebugInfoEntry::GetDWARFDeclContext( 1078 DWARFUnit *cu, DWARFDeclContext &dwarf_decl_ctx) const { 1079 const dw_tag_t tag = Tag(); 1080 if (tag != DW_TAG_compile_unit && tag != DW_TAG_partial_unit) { 1081 dwarf_decl_ctx.AppendDeclContext(tag, GetName(cu)); 1082 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 1083 if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != this) { 1084 if (parent_decl_ctx_die.Tag() != DW_TAG_compile_unit && 1085 parent_decl_ctx_die.Tag() != DW_TAG_partial_unit) 1086 parent_decl_ctx_die.GetDIE()->GetDWARFDeclContext( 1087 parent_decl_ctx_die.GetCU(), dwarf_decl_ctx); 1088 } 1089 } 1090 } 1091 1092 DWARFDIE 1093 DWARFDebugInfoEntry::GetParentDeclContextDIE(DWARFUnit *cu) const { 1094 DWARFAttributes attributes; 1095 GetAttributes(cu, attributes); 1096 return GetParentDeclContextDIE(cu, attributes); 1097 } 1098 1099 DWARFDIE 1100 DWARFDebugInfoEntry::GetParentDeclContextDIE( 1101 DWARFUnit *cu, const DWARFAttributes &attributes) const { 1102 DWARFDIE die(cu, const_cast<DWARFDebugInfoEntry *>(this)); 1103 1104 while (die) { 1105 // If this is the original DIE that we are searching for a declaration for, 1106 // then don't look in the cache as we don't want our own decl context to be 1107 // our decl context... 1108 if (die.GetDIE() != this) { 1109 switch (die.Tag()) { 1110 case DW_TAG_compile_unit: 1111 case DW_TAG_partial_unit: 1112 case DW_TAG_namespace: 1113 case DW_TAG_structure_type: 1114 case DW_TAG_union_type: 1115 case DW_TAG_class_type: 1116 return die; 1117 1118 default: 1119 break; 1120 } 1121 } 1122 1123 DWARFDIE spec_die = attributes.FormValueAsReference(DW_AT_specification); 1124 if (spec_die) { 1125 DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE(); 1126 if (decl_ctx_die) 1127 return decl_ctx_die; 1128 } 1129 1130 DWARFDIE abs_die = attributes.FormValueAsReference(DW_AT_abstract_origin); 1131 if (abs_die) { 1132 DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE(); 1133 if (decl_ctx_die) 1134 return decl_ctx_die; 1135 } 1136 1137 die = die.GetParent(); 1138 } 1139 return DWARFDIE(); 1140 } 1141 1142 const char *DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 1143 std::string &storage) const { 1144 DWARFAttributes attributes; 1145 GetAttributes(cu, attributes); 1146 return GetQualifiedName(cu, attributes, storage); 1147 } 1148 1149 const char * 1150 DWARFDebugInfoEntry::GetQualifiedName(DWARFUnit *cu, 1151 const DWARFAttributes &attributes, 1152 std::string &storage) const { 1153 1154 const char *name = GetName(cu); 1155 1156 if (name) { 1157 DWARFDIE parent_decl_ctx_die = GetParentDeclContextDIE(cu); 1158 storage.clear(); 1159 // TODO: change this to get the correct decl context parent.... 1160 while (parent_decl_ctx_die) { 1161 const dw_tag_t parent_tag = parent_decl_ctx_die.Tag(); 1162 switch (parent_tag) { 1163 case DW_TAG_namespace: { 1164 const char *namespace_name = parent_decl_ctx_die.GetName(); 1165 if (namespace_name) { 1166 storage.insert(0, "::"); 1167 storage.insert(0, namespace_name); 1168 } else { 1169 storage.insert(0, "(anonymous namespace)::"); 1170 } 1171 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 1172 } break; 1173 1174 case DW_TAG_class_type: 1175 case DW_TAG_structure_type: 1176 case DW_TAG_union_type: { 1177 const char *class_union_struct_name = parent_decl_ctx_die.GetName(); 1178 1179 if (class_union_struct_name) { 1180 storage.insert(0, "::"); 1181 storage.insert(0, class_union_struct_name); 1182 } 1183 parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); 1184 } break; 1185 1186 default: 1187 parent_decl_ctx_die.Clear(); 1188 break; 1189 } 1190 } 1191 1192 if (storage.empty()) 1193 storage.append("::"); 1194 1195 storage.append(name); 1196 } 1197 if (storage.empty()) 1198 return nullptr; 1199 return storage.c_str(); 1200 } 1201 1202 bool DWARFDebugInfoEntry::LookupAddress(const dw_addr_t address, 1203 const DWARFUnit *cu, 1204 DWARFDebugInfoEntry **function_die, 1205 DWARFDebugInfoEntry **block_die) { 1206 bool found_address = false; 1207 if (m_tag) { 1208 bool check_children = false; 1209 bool match_addr_range = false; 1210 // printf("0x%8.8x: %30s: address = 0x%8.8x - ", m_offset, 1211 // DW_TAG_value_to_name(tag), address); 1212 switch (m_tag) { 1213 case DW_TAG_array_type: 1214 break; 1215 case DW_TAG_class_type: 1216 check_children = true; 1217 break; 1218 case DW_TAG_entry_point: 1219 case DW_TAG_enumeration_type: 1220 case DW_TAG_formal_parameter: 1221 case DW_TAG_imported_declaration: 1222 case DW_TAG_label: 1223 break; 1224 case DW_TAG_lexical_block: 1225 check_children = true; 1226 match_addr_range = true; 1227 break; 1228 case DW_TAG_member: 1229 case DW_TAG_pointer_type: 1230 case DW_TAG_reference_type: 1231 break; 1232 case DW_TAG_compile_unit: 1233 match_addr_range = true; 1234 break; 1235 case DW_TAG_string_type: 1236 break; 1237 case DW_TAG_structure_type: 1238 check_children = true; 1239 break; 1240 case DW_TAG_subroutine_type: 1241 case DW_TAG_typedef: 1242 case DW_TAG_union_type: 1243 case DW_TAG_unspecified_parameters: 1244 case DW_TAG_variant: 1245 break; 1246 case DW_TAG_common_block: 1247 check_children = true; 1248 break; 1249 case DW_TAG_common_inclusion: 1250 case DW_TAG_inheritance: 1251 break; 1252 case DW_TAG_inlined_subroutine: 1253 check_children = true; 1254 match_addr_range = true; 1255 break; 1256 case DW_TAG_module: 1257 match_addr_range = true; 1258 break; 1259 case DW_TAG_ptr_to_member_type: 1260 case DW_TAG_set_type: 1261 case DW_TAG_subrange_type: 1262 case DW_TAG_with_stmt: 1263 case DW_TAG_access_declaration: 1264 case DW_TAG_base_type: 1265 break; 1266 case DW_TAG_catch_block: 1267 match_addr_range = true; 1268 break; 1269 case DW_TAG_const_type: 1270 case DW_TAG_constant: 1271 case DW_TAG_enumerator: 1272 case DW_TAG_file_type: 1273 case DW_TAG_friend: 1274 case DW_TAG_namelist: 1275 case DW_TAG_namelist_item: 1276 case DW_TAG_packed_type: 1277 break; 1278 case DW_TAG_subprogram: 1279 match_addr_range = true; 1280 break; 1281 case DW_TAG_template_type_parameter: 1282 case DW_TAG_template_value_parameter: 1283 case DW_TAG_GNU_template_parameter_pack: 1284 case DW_TAG_thrown_type: 1285 break; 1286 case DW_TAG_try_block: 1287 match_addr_range = true; 1288 break; 1289 case DW_TAG_variant_part: 1290 case DW_TAG_variable: 1291 case DW_TAG_volatile_type: 1292 case DW_TAG_dwarf_procedure: 1293 case DW_TAG_restrict_type: 1294 case DW_TAG_interface_type: 1295 break; 1296 case DW_TAG_namespace: 1297 check_children = true; 1298 break; 1299 case DW_TAG_imported_module: 1300 case DW_TAG_unspecified_type: 1301 break; 1302 case DW_TAG_partial_unit: 1303 match_addr_range = true; 1304 break; 1305 case DW_TAG_imported_unit: 1306 case DW_TAG_shared_type: 1307 default: 1308 break; 1309 } 1310 1311 if (match_addr_range) { 1312 dw_addr_t lo_pc = 1313 GetAttributeValueAsAddress(cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 1314 if (lo_pc != LLDB_INVALID_ADDRESS) { 1315 dw_addr_t hi_pc = GetAttributeHighPC(cu, lo_pc, LLDB_INVALID_ADDRESS); 1316 if (hi_pc != LLDB_INVALID_ADDRESS) { 1317 // printf("\n0x%8.8x: %30s: address = 0x%8.8x [0x%8.8x - 0x%8.8x) ", 1318 // m_offset, DW_TAG_value_to_name(tag), address, lo_pc, hi_pc); 1319 if ((lo_pc <= address) && (address < hi_pc)) { 1320 found_address = true; 1321 // puts("***MATCH***"); 1322 switch (m_tag) { 1323 case DW_TAG_compile_unit: // File 1324 case DW_TAG_partial_unit: // File 1325 check_children = 1326 ((function_die != nullptr) || (block_die != nullptr)); 1327 break; 1328 1329 case DW_TAG_subprogram: // Function 1330 if (function_die) 1331 *function_die = this; 1332 check_children = (block_die != nullptr); 1333 break; 1334 1335 case DW_TAG_inlined_subroutine: // Inlined Function 1336 case DW_TAG_lexical_block: // Block { } in code 1337 if (block_die) { 1338 *block_die = this; 1339 check_children = true; 1340 } 1341 break; 1342 1343 default: 1344 check_children = true; 1345 break; 1346 } 1347 } 1348 } else { 1349 // Compile units may not have a valid high/low pc when there 1350 // are address gaps in subroutines so we must always search 1351 // if there is no valid high and low PC. 1352 check_children = 1353 (m_tag == DW_TAG_compile_unit || m_tag == DW_TAG_partial_unit) && 1354 ((function_die != nullptr) || (block_die != nullptr)); 1355 } 1356 } else { 1357 DWARFRangeList ranges; 1358 if (GetAttributeAddressRanges(cu, ranges, /*check_hi_lo_pc*/ false) && 1359 ranges.FindEntryThatContains(address)) { 1360 found_address = true; 1361 // puts("***MATCH***"); 1362 switch (m_tag) { 1363 case DW_TAG_compile_unit: // File 1364 case DW_TAG_partial_unit: // File 1365 check_children = 1366 ((function_die != nullptr) || (block_die != nullptr)); 1367 break; 1368 1369 case DW_TAG_subprogram: // Function 1370 if (function_die) 1371 *function_die = this; 1372 check_children = (block_die != nullptr); 1373 break; 1374 1375 case DW_TAG_inlined_subroutine: // Inlined Function 1376 case DW_TAG_lexical_block: // Block { } in code 1377 if (block_die) { 1378 *block_die = this; 1379 check_children = true; 1380 } 1381 break; 1382 1383 default: 1384 check_children = true; 1385 break; 1386 } 1387 } else { 1388 check_children = false; 1389 } 1390 } 1391 } 1392 1393 if (check_children) { 1394 // printf("checking children\n"); 1395 DWARFDebugInfoEntry *child = GetFirstChild(); 1396 while (child) { 1397 if (child->LookupAddress(address, cu, function_die, block_die)) 1398 return true; 1399 child = child->GetSibling(); 1400 } 1401 } 1402 } 1403 return found_address; 1404 } 1405 1406 const DWARFAbbreviationDeclaration * 1407 DWARFDebugInfoEntry::GetAbbreviationDeclarationPtr( 1408 const DWARFUnit *cu, lldb::offset_t &offset) const { 1409 if (cu) { 1410 offset = GetOffset(); 1411 1412 const DWARFAbbreviationDeclarationSet *abbrev_set = cu->GetAbbreviations(); 1413 if (abbrev_set) { 1414 const DWARFAbbreviationDeclaration *abbrev_decl = 1415 abbrev_set->GetAbbreviationDeclaration(m_abbr_idx); 1416 if (abbrev_decl) { 1417 // Make sure the abbreviation code still matches. If it doesn't and the 1418 // DWARF data was mmap'ed, the backing file might have been modified 1419 // which is bad news. 1420 const uint64_t abbrev_code = cu->GetData().GetULEB128(&offset); 1421 1422 if (abbrev_decl->Code() == abbrev_code) 1423 return abbrev_decl; 1424 1425 SymbolFileDWARF *dwarf2Data = cu->GetSymbolFileDWARF(); 1426 1427 dwarf2Data->GetObjectFile()->GetModule()->ReportErrorIfModifyDetected( 1428 "0x%8.8x: the DWARF debug information has been modified (abbrev " 1429 "code was %u, and is now %u)", 1430 GetOffset(), (uint32_t)abbrev_decl->Code(), (uint32_t)abbrev_code); 1431 } 1432 } 1433 } 1434 offset = DW_INVALID_OFFSET; 1435 return nullptr; 1436 } 1437 1438 bool DWARFDebugInfoEntry::operator==(const DWARFDebugInfoEntry &rhs) const { 1439 return m_offset == rhs.m_offset && m_parent_idx == rhs.m_parent_idx && 1440 m_sibling_idx == rhs.m_sibling_idx && 1441 m_abbr_idx == rhs.m_abbr_idx && m_has_children == rhs.m_has_children && 1442 m_tag == rhs.m_tag; 1443 } 1444 1445 bool DWARFDebugInfoEntry::operator!=(const DWARFDebugInfoEntry &rhs) const { 1446 return !(*this == rhs); 1447 } 1448