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