1 //===-- DWARFCompileUnit.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 "DWARFCompileUnit.h" 11 12 #include "lldb/Core/Mangled.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/Stream.h" 15 #include "lldb/Core/Timer.h" 16 #include "lldb/Symbol/CompileUnit.h" 17 #include "lldb/Symbol/LineTable.h" 18 #include "lldb/Symbol/ObjectFile.h" 19 #include "lldb/Target/ObjCLanguageRuntime.h" 20 21 #include "DWARFDebugAbbrev.h" 22 #include "DWARFDebugAranges.h" 23 #include "DWARFDebugInfo.h" 24 #include "DWARFDIECollection.h" 25 #include "DWARFFormValue.h" 26 #include "LogChannelDWARF.h" 27 #include "NameToDIE.h" 28 #include "SymbolFileDWARF.h" 29 #include "SymbolFileDWARFDebugMap.h" 30 31 using namespace lldb; 32 using namespace lldb_private; 33 using namespace std; 34 35 36 extern int g_verbose; 37 38 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) : 39 m_dwarf2Data (dwarf2Data), 40 m_abbrevs (NULL), 41 m_user_data (NULL), 42 m_die_array (), 43 m_func_aranges_ap (), 44 m_base_addr (0), 45 m_offset (DW_INVALID_OFFSET), 46 m_length (0), 47 m_version (0), 48 m_addr_size (DWARFCompileUnit::GetDefaultAddressSize()), 49 m_producer (eProducerInvalid), 50 m_producer_version_major (0), 51 m_producer_version_minor (0), 52 m_producer_version_update (0), 53 m_is_dwarf64 (false) 54 { 55 } 56 57 void 58 DWARFCompileUnit::Clear() 59 { 60 m_offset = DW_INVALID_OFFSET; 61 m_length = 0; 62 m_version = 0; 63 m_abbrevs = NULL; 64 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize(); 65 m_base_addr = 0; 66 m_die_array.clear(); 67 m_func_aranges_ap.reset(); 68 m_user_data = NULL; 69 m_producer = eProducerInvalid; 70 m_is_dwarf64 = false; 71 } 72 73 bool 74 DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr) 75 { 76 Clear(); 77 78 m_offset = *offset_ptr; 79 80 if (debug_info.ValidOffset(*offset_ptr)) 81 { 82 dw_offset_t abbr_offset; 83 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev(); 84 m_length = debug_info.GetDWARFInitialLength(offset_ptr); 85 m_is_dwarf64 = debug_info.IsDWARF64(); 86 m_version = debug_info.GetU16(offset_ptr); 87 abbr_offset = debug_info.GetDWARFOffset(offset_ptr); 88 m_addr_size = debug_info.GetU8 (offset_ptr); 89 90 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); 91 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); 92 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset); 93 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); 94 95 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL) 96 { 97 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset); 98 return true; 99 } 100 101 // reset the offset to where we tried to parse from if anything went wrong 102 *offset_ptr = m_offset; 103 } 104 105 return false; 106 } 107 108 109 void 110 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die) 111 { 112 if (m_die_array.size() > 1) 113 { 114 // std::vectors never get any smaller when resized to a smaller size, 115 // or when clear() or erase() are called, the size will report that it 116 // is smaller, but the memory allocated remains intact (call capacity() 117 // to see this). So we need to create a temporary vector and swap the 118 // contents which will cause just the internal pointers to be swapped 119 // so that when "tmp_array" goes out of scope, it will destroy the 120 // contents. 121 122 // Save at least the compile unit DIE 123 DWARFDebugInfoEntry::collection tmp_array; 124 m_die_array.swap(tmp_array); 125 if (keep_compile_unit_die) 126 m_die_array.push_back(tmp_array.front()); 127 } 128 } 129 130 //---------------------------------------------------------------------- 131 // ParseCompileUnitDIEsIfNeeded 132 // 133 // Parses a compile unit and indexes its DIEs if it hasn't already been 134 // done. 135 //---------------------------------------------------------------------- 136 size_t 137 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only) 138 { 139 const size_t initial_die_array_size = m_die_array.size(); 140 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1) 141 return 0; // Already parsed 142 143 Timer scoped_timer (__PRETTY_FUNCTION__, 144 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )", 145 m_offset, 146 cu_die_only); 147 148 // Set the offset to that of the first DIE and calculate the start of the 149 // next compilation unit header. 150 lldb::offset_t offset = GetFirstDIEOffset(); 151 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset(); 152 153 DWARFDebugInfoEntry die; 154 // Keep a flat array of the DIE for binary lookup by DIE offset 155 if (!cu_die_only) 156 { 157 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS)); 158 if (log) 159 { 160 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log, 161 "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]", 162 GetOffset()); 163 } 164 } 165 166 uint32_t depth = 0; 167 // We are in our compile unit, parse starting at the offset 168 // we were told to parse 169 const DWARFDataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data(); 170 std::vector<uint32_t> die_index_stack; 171 die_index_stack.reserve(32); 172 die_index_stack.push_back(0); 173 bool prev_die_had_children = false; 174 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64); 175 while (offset < next_cu_offset && 176 die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset)) 177 { 178 // if (log) 179 // log->Printf("0x%8.8x: %*.*s%s%s", 180 // die.GetOffset(), 181 // depth * 2, depth * 2, "", 182 // DW_TAG_value_to_name (die.Tag()), 183 // die.HasChildren() ? " *" : ""); 184 185 const bool null_die = die.IsNULL(); 186 if (depth == 0) 187 { 188 uint64_t base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 189 if (base_addr == LLDB_INVALID_ADDRESS) 190 base_addr = die.GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_entry_pc, 0); 191 SetBaseAddress (base_addr); 192 if (initial_die_array_size == 0) 193 AddDIE (die); 194 if (cu_die_only) 195 return 1; 196 } 197 else 198 { 199 if (null_die) 200 { 201 if (prev_die_had_children) 202 { 203 // This will only happen if a DIE says is has children 204 // but all it contains is a NULL tag. Since we are removing 205 // the NULL DIEs from the list (saves up to 25% in C++ code), 206 // we need a way to let the DIE know that it actually doesn't 207 // have children. 208 if (!m_die_array.empty()) 209 m_die_array.back().SetEmptyChildren(true); 210 } 211 } 212 else 213 { 214 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]); 215 216 if (die_index_stack.back()) 217 m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back()); 218 219 // Only push the DIE if it isn't a NULL DIE 220 m_die_array.push_back(die); 221 } 222 } 223 224 if (null_die) 225 { 226 // NULL DIE. 227 if (!die_index_stack.empty()) 228 die_index_stack.pop_back(); 229 230 if (depth > 0) 231 --depth; 232 if (depth == 0) 233 break; // We are done with this compile unit! 234 235 prev_die_had_children = false; 236 } 237 else 238 { 239 die_index_stack.back() = m_die_array.size() - 1; 240 // Normal DIE 241 const bool die_has_children = die.HasChildren(); 242 if (die_has_children) 243 { 244 die_index_stack.push_back(0); 245 ++depth; 246 } 247 prev_die_had_children = die_has_children; 248 } 249 } 250 251 // Give a little bit of info if we encounter corrupt DWARF (our offset 252 // should always terminate at or before the start of the next compilation 253 // unit header). 254 if (offset > next_cu_offset) 255 { 256 m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8" PRIx64 "\n", 257 GetOffset(), 258 offset); 259 } 260 261 // Since std::vector objects will double their size, we really need to 262 // make a new array with the perfect size so we don't end up wasting 263 // space. So here we copy and swap to make sure we don't have any extra 264 // memory taken up. 265 266 if (m_die_array.size () < m_die_array.capacity()) 267 { 268 DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end()); 269 exact_size_die_array.swap (m_die_array); 270 } 271 Log *verbose_log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE)); 272 if (verbose_log) 273 { 274 StreamString strm; 275 Dump(&strm); 276 if (m_die_array.empty()) 277 strm.Printf("error: no DIE for compile unit"); 278 else 279 m_die_array[0].Dump(m_dwarf2Data, this, strm, UINT32_MAX); 280 verbose_log->PutCString (strm.GetString().c_str()); 281 } 282 283 return m_die_array.size(); 284 } 285 286 287 dw_offset_t 288 DWARFCompileUnit::GetAbbrevOffset() const 289 { 290 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET; 291 } 292 293 294 295 bool 296 DWARFCompileUnit::Verify(Stream *s) const 297 { 298 const DWARFDataExtractor& debug_info = m_dwarf2Data->get_debug_info_data(); 299 bool valid_offset = debug_info.ValidOffset(m_offset); 300 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1); 301 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version); 302 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset()); 303 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8)); 304 bool verbose = s->GetVerbose(); 305 if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK) 306 { 307 if (verbose) 308 s->Printf(" 0x%8.8x: OK\n", m_offset); 309 return true; 310 } 311 else 312 { 313 s->Printf(" 0x%8.8x: ", m_offset); 314 315 m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0); 316 s->EOL(); 317 if (valid_offset) 318 { 319 if (!length_OK) 320 s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length); 321 if (!version_OK) 322 s->Printf(" The 16 bit compile unit header version is not supported.\n"); 323 if (!abbr_offset_OK) 324 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset()); 325 if (!addr_size_OK) 326 s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size); 327 } 328 else 329 s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n"); 330 } 331 return false; 332 } 333 334 335 void 336 DWARFCompileUnit::Dump(Stream *s) const 337 { 338 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n", 339 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset()); 340 } 341 342 343 static uint8_t g_default_addr_size = 4; 344 345 uint8_t 346 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu) 347 { 348 if (cu) 349 return cu->GetAddressByteSize(); 350 return DWARFCompileUnit::GetDefaultAddressSize(); 351 } 352 353 bool 354 DWARFCompileUnit::IsDWARF64(const DWARFCompileUnit* cu) 355 { 356 if (cu) 357 return cu->IsDWARF64(); 358 return false; 359 } 360 361 uint8_t 362 DWARFCompileUnit::GetDefaultAddressSize() 363 { 364 return g_default_addr_size; 365 } 366 367 void 368 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size) 369 { 370 g_default_addr_size = addr_size; 371 } 372 373 void 374 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data, 375 DWARFDebugAranges* debug_aranges) 376 { 377 // This function is usually called if there in no .debug_aranges section 378 // in order to produce a compile unit level set of address ranges that 379 // is accurate. 380 381 // First get the compile unit DIE only and check if it has a DW_AT_ranges 382 const DWARFDebugInfoEntry* die = GetCompileUnitDIEOnly(); 383 384 const dw_offset_t cu_offset = GetOffset(); 385 if (die) 386 { 387 DWARFDebugRanges::RangeList ranges; 388 const size_t num_ranges = die->GetAttributeAddressRanges(dwarf2Data, this, ranges, false); 389 if (num_ranges > 0) 390 { 391 // This compile unit has DW_AT_ranges, assume this is correct if it 392 // is present since clang no longer makes .debug_aranges by default 393 // and it emits DW_AT_ranges for DW_TAG_compile_units. GCC also does 394 // this with recent GCC builds. 395 for (size_t i=0; i<num_ranges; ++i) 396 { 397 const DWARFDebugRanges::RangeList::Entry &range = ranges.GetEntryRef(i); 398 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd()); 399 } 400 401 return; // We got all of our ranges from the DW_AT_ranges attribute 402 } 403 } 404 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF 405 406 // If the DIEs weren't parsed, then we don't want all dies for all compile units 407 // to stay loaded when they weren't needed. So we can end up parsing the DWARF 408 // and then throwing them all away to keep memory usage down. 409 const bool clear_dies = ExtractDIEsIfNeeded (false) > 1; 410 411 die = DIE(); 412 if (die) 413 die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges); 414 415 if (debug_aranges->IsEmpty()) 416 { 417 // We got nothing from the functions, maybe we have a line tables only 418 // situation. Check the line tables and build the arange table from this. 419 SymbolContext sc; 420 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this); 421 if (sc.comp_unit) 422 { 423 SymbolFileDWARFDebugMap *debug_map_sym_file = m_dwarf2Data->GetDebugMapSymfile(); 424 if (debug_map_sym_file == NULL) 425 { 426 LineTable *line_table = sc.comp_unit->GetLineTable(); 427 428 if (line_table) 429 { 430 LineTable::FileAddressRanges file_ranges; 431 const bool append = true; 432 const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append); 433 for (uint32_t idx=0; idx<num_ranges; ++idx) 434 { 435 const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx); 436 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd()); 437 printf ("0x%8.8x: [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 ")\n", GetOffset(), range.GetRangeBase(), range.GetRangeEnd()); 438 } 439 } 440 } 441 else 442 debug_map_sym_file->AddOSOARanges(dwarf2Data,debug_aranges); 443 } 444 } 445 446 // Keep memory down by clearing DIEs if this generate function 447 // caused them to be parsed 448 if (clear_dies) 449 ClearDIEs (true); 450 451 } 452 453 454 const DWARFDebugAranges & 455 DWARFCompileUnit::GetFunctionAranges () 456 { 457 if (m_func_aranges_ap.get() == NULL) 458 { 459 m_func_aranges_ap.reset (new DWARFDebugAranges()); 460 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES)); 461 462 if (log) 463 { 464 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log, 465 "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]", 466 GetOffset()); 467 } 468 const DWARFDebugInfoEntry* die = DIE(); 469 if (die) 470 die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get()); 471 const bool minimize = false; 472 m_func_aranges_ap->Sort(minimize); 473 } 474 return *m_func_aranges_ap.get(); 475 } 476 477 bool 478 DWARFCompileUnit::LookupAddress 479 ( 480 const dw_addr_t address, 481 DWARFDebugInfoEntry** function_die_handle, 482 DWARFDebugInfoEntry** block_die_handle 483 ) 484 { 485 bool success = false; 486 487 if (function_die_handle != NULL && DIE()) 488 { 489 490 const DWARFDebugAranges &func_aranges = GetFunctionAranges (); 491 492 // Re-check the aranges auto pointer contents in case it was created above 493 if (!func_aranges.IsEmpty()) 494 { 495 *function_die_handle = GetDIEPtr(func_aranges.FindAddress(address)); 496 if (*function_die_handle != NULL) 497 { 498 success = true; 499 if (block_die_handle != NULL) 500 { 501 DWARFDebugInfoEntry* child = (*function_die_handle)->GetFirstChild(); 502 while (child) 503 { 504 if (child->LookupAddress(address, m_dwarf2Data, this, NULL, block_die_handle)) 505 break; 506 child = child->GetSibling(); 507 } 508 } 509 } 510 } 511 } 512 return success; 513 } 514 515 //---------------------------------------------------------------------- 516 // Compare function DWARFDebugAranges::Range structures 517 //---------------------------------------------------------------------- 518 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die1, const DWARFDebugInfoEntry& die2) 519 { 520 return die1.GetOffset() < die2.GetOffset(); 521 } 522 523 //---------------------------------------------------------------------- 524 // GetDIEPtr() 525 // 526 // Get the DIE (Debug Information Entry) with the specified offset. 527 //---------------------------------------------------------------------- 528 DWARFDebugInfoEntry* 529 DWARFCompileUnit::GetDIEPtr(dw_offset_t die_offset) 530 { 531 if (die_offset != DW_INVALID_OFFSET) 532 { 533 ExtractDIEsIfNeeded (false); 534 DWARFDebugInfoEntry compare_die; 535 compare_die.SetOffset(die_offset); 536 DWARFDebugInfoEntry::iterator end = m_die_array.end(); 537 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); 538 if (pos != end) 539 { 540 if (die_offset == (*pos).GetOffset()) 541 return &(*pos); 542 } 543 } 544 return NULL; // Not found in any compile units 545 } 546 547 //---------------------------------------------------------------------- 548 // GetDIEPtrContainingOffset() 549 // 550 // Get the DIE (Debug Information Entry) that contains the specified 551 // .debug_info offset. 552 //---------------------------------------------------------------------- 553 const DWARFDebugInfoEntry* 554 DWARFCompileUnit::GetDIEPtrContainingOffset(dw_offset_t die_offset) 555 { 556 if (die_offset != DW_INVALID_OFFSET) 557 { 558 ExtractDIEsIfNeeded (false); 559 DWARFDebugInfoEntry compare_die; 560 compare_die.SetOffset(die_offset); 561 DWARFDebugInfoEntry::iterator end = m_die_array.end(); 562 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, compare_die, CompareDIEOffset); 563 if (pos != end) 564 { 565 if (die_offset >= (*pos).GetOffset()) 566 { 567 DWARFDebugInfoEntry::iterator next = pos + 1; 568 if (next != end) 569 { 570 if (die_offset < (*next).GetOffset()) 571 return &(*pos); 572 } 573 } 574 } 575 } 576 return NULL; // Not found in any compile units 577 } 578 579 580 581 size_t 582 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const 583 { 584 size_t old_size = dies.Size(); 585 DWARFDebugInfoEntry::const_iterator pos; 586 DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); 587 for (pos = m_die_array.begin(); pos != end; ++pos) 588 { 589 if (pos->Tag() == tag) 590 dies.Append (&(*pos)); 591 } 592 593 // Return the number of DIEs added to the collection 594 return dies.Size() - old_size; 595 } 596 597 //void 598 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx) 599 //{ 600 // m_global_die_indexes.push_back (die_idx); 601 //} 602 // 603 // 604 //void 605 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die) 606 //{ 607 // // Indexes to all file level global and static variables 608 // m_global_die_indexes; 609 // 610 // if (m_die_array.empty()) 611 // return; 612 // 613 // const DWARFDebugInfoEntry* first_die = &m_die_array[0]; 614 // const DWARFDebugInfoEntry* end = first_die + m_die_array.size(); 615 // if (first_die <= die && die < end) 616 // m_global_die_indexes.push_back (die - first_die); 617 //} 618 619 620 void 621 DWARFCompileUnit::Index (const uint32_t cu_idx, 622 NameToDIE& func_basenames, 623 NameToDIE& func_fullnames, 624 NameToDIE& func_methods, 625 NameToDIE& func_selectors, 626 NameToDIE& objc_class_selectors, 627 NameToDIE& globals, 628 NameToDIE& types, 629 NameToDIE& namespaces) 630 { 631 const DWARFDataExtractor* debug_str = &m_dwarf2Data->get_debug_str_data(); 632 633 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64); 634 635 Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS)); 636 637 if (log) 638 { 639 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log, 640 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]", 641 GetOffset()); 642 } 643 644 DWARFDebugInfoEntry::const_iterator pos; 645 DWARFDebugInfoEntry::const_iterator begin = m_die_array.begin(); 646 DWARFDebugInfoEntry::const_iterator end = m_die_array.end(); 647 for (pos = begin; pos != end; ++pos) 648 { 649 const DWARFDebugInfoEntry &die = *pos; 650 651 const dw_tag_t tag = die.Tag(); 652 653 switch (tag) 654 { 655 case DW_TAG_subprogram: 656 case DW_TAG_inlined_subroutine: 657 case DW_TAG_base_type: 658 case DW_TAG_class_type: 659 case DW_TAG_constant: 660 case DW_TAG_enumeration_type: 661 case DW_TAG_string_type: 662 case DW_TAG_subroutine_type: 663 case DW_TAG_structure_type: 664 case DW_TAG_union_type: 665 case DW_TAG_typedef: 666 case DW_TAG_namespace: 667 case DW_TAG_variable: 668 case DW_TAG_unspecified_type: 669 break; 670 671 default: 672 continue; 673 } 674 675 DWARFDebugInfoEntry::Attributes attributes; 676 const char *name = NULL; 677 const char *mangled_cstr = NULL; 678 bool is_declaration = false; 679 //bool is_artificial = false; 680 bool has_address = false; 681 bool has_location = false; 682 bool is_global_or_static_variable = false; 683 684 dw_offset_t specification_die_offset = DW_INVALID_OFFSET; 685 const size_t num_attributes = die.GetAttributes(m_dwarf2Data, this, fixed_form_sizes, attributes); 686 if (num_attributes > 0) 687 { 688 for (uint32_t i=0; i<num_attributes; ++i) 689 { 690 dw_attr_t attr = attributes.AttributeAtIndex(i); 691 DWARFFormValue form_value; 692 switch (attr) 693 { 694 case DW_AT_name: 695 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 696 name = form_value.AsCString(debug_str); 697 break; 698 699 case DW_AT_declaration: 700 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 701 is_declaration = form_value.Unsigned() != 0; 702 break; 703 704 // case DW_AT_artificial: 705 // if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 706 // is_artificial = form_value.Unsigned() != 0; 707 // break; 708 709 case DW_AT_MIPS_linkage_name: 710 case DW_AT_linkage_name: 711 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 712 mangled_cstr = form_value.AsCString(debug_str); 713 break; 714 715 case DW_AT_low_pc: 716 case DW_AT_high_pc: 717 case DW_AT_ranges: 718 has_address = true; 719 break; 720 721 case DW_AT_entry_pc: 722 has_address = true; 723 break; 724 725 case DW_AT_location: 726 has_location = true; 727 if (tag == DW_TAG_variable) 728 { 729 const DWARFDebugInfoEntry* parent_die = die.GetParent(); 730 while ( parent_die != NULL ) 731 { 732 switch (parent_die->Tag()) 733 { 734 case DW_TAG_subprogram: 735 case DW_TAG_lexical_block: 736 case DW_TAG_inlined_subroutine: 737 // Even if this is a function level static, we don't add it. We could theoretically 738 // add these if we wanted to by introspecting into the DW_AT_location and seeing 739 // if the location describes a hard coded address, but we dont want the performance 740 // penalty of that right now. 741 is_global_or_static_variable = false; 742 // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value)) 743 // { 744 // // If we have valid block data, then we have location expression bytes 745 // // that are fixed (not a location list). 746 // const uint8_t *block_data = form_value.BlockData(); 747 // if (block_data) 748 // { 749 // uint32_t block_length = form_value.Unsigned(); 750 // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize()) 751 // { 752 // if (block_data[0] == DW_OP_addr) 753 // add_die = true; 754 // } 755 // } 756 // } 757 parent_die = NULL; // Terminate the while loop. 758 break; 759 760 case DW_TAG_compile_unit: 761 is_global_or_static_variable = true; 762 parent_die = NULL; // Terminate the while loop. 763 break; 764 765 default: 766 parent_die = parent_die->GetParent(); // Keep going in the while loop. 767 break; 768 } 769 } 770 } 771 break; 772 773 case DW_AT_specification: 774 if (attributes.ExtractFormValueAtIndex(m_dwarf2Data, i, form_value)) 775 specification_die_offset = form_value.Reference(this); 776 break; 777 } 778 } 779 } 780 781 switch (tag) 782 { 783 case DW_TAG_subprogram: 784 if (has_address) 785 { 786 if (name) 787 { 788 // Note, this check is also done in ParseMethodName, but since this is a hot loop, we do the 789 // simple inlined check outside the call. 790 ObjCLanguageRuntime::MethodName objc_method(name, true); 791 if (objc_method.IsValid(true)) 792 { 793 ConstString objc_class_name_with_category (objc_method.GetClassNameWithCategory()); 794 ConstString objc_selector_name (objc_method.GetSelector()); 795 ConstString objc_fullname_no_category_name (objc_method.GetFullNameWithoutCategory(true)); 796 ConstString objc_class_name_no_category (objc_method.GetClassName()); 797 func_fullnames.Insert (ConstString(name), die.GetOffset()); 798 if (objc_class_name_with_category) 799 objc_class_selectors.Insert(objc_class_name_with_category, die.GetOffset()); 800 if (objc_class_name_no_category && objc_class_name_no_category != objc_class_name_with_category) 801 objc_class_selectors.Insert(objc_class_name_no_category, die.GetOffset()); 802 if (objc_selector_name) 803 func_selectors.Insert (objc_selector_name, die.GetOffset()); 804 if (objc_fullname_no_category_name) 805 func_fullnames.Insert (objc_fullname_no_category_name, die.GetOffset()); 806 } 807 // If we have a mangled name, then the DW_AT_name attribute 808 // is usually the method name without the class or any parameters 809 const DWARFDebugInfoEntry *parent = die.GetParent(); 810 bool is_method = false; 811 if (parent) 812 { 813 dw_tag_t parent_tag = parent->Tag(); 814 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) 815 { 816 is_method = true; 817 } 818 else 819 { 820 if (specification_die_offset != DW_INVALID_OFFSET) 821 { 822 const DWARFDebugInfoEntry *specification_die = m_dwarf2Data->DebugInfo()->GetDIEPtr (specification_die_offset, NULL); 823 if (specification_die) 824 { 825 parent = specification_die->GetParent(); 826 if (parent) 827 { 828 parent_tag = parent->Tag(); 829 830 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type) 831 is_method = true; 832 } 833 } 834 } 835 } 836 } 837 838 839 if (is_method) 840 func_methods.Insert (ConstString(name), die.GetOffset()); 841 else 842 func_basenames.Insert (ConstString(name), die.GetOffset()); 843 844 if (!is_method && !mangled_cstr && !objc_method.IsValid(true)) 845 func_fullnames.Insert (ConstString(name), die.GetOffset()); 846 } 847 if (mangled_cstr) 848 { 849 // Make sure our mangled name isn't the same string table entry 850 // as our name. If it starts with '_', then it is ok, else compare 851 // the string to make sure it isn't the same and we don't end up 852 // with duplicate entries 853 if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (name && ::strcmp(name, mangled_cstr) != 0))) 854 { 855 Mangled mangled (ConstString(mangled_cstr), true); 856 func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); 857 if (mangled.GetDemangledName()) 858 func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset()); 859 } 860 } 861 } 862 break; 863 864 case DW_TAG_inlined_subroutine: 865 if (has_address) 866 { 867 if (name) 868 func_basenames.Insert (ConstString(name), die.GetOffset()); 869 if (mangled_cstr) 870 { 871 // Make sure our mangled name isn't the same string table entry 872 // as our name. If it starts with '_', then it is ok, else compare 873 // the string to make sure it isn't the same and we don't end up 874 // with duplicate entries 875 if (name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) 876 { 877 Mangled mangled (ConstString(mangled_cstr), true); 878 func_fullnames.Insert (mangled.GetMangledName(), die.GetOffset()); 879 if (mangled.GetDemangledName()) 880 func_fullnames.Insert (mangled.GetDemangledName(), die.GetOffset()); 881 } 882 } 883 else 884 func_fullnames.Insert (ConstString(name), die.GetOffset()); 885 } 886 break; 887 888 case DW_TAG_base_type: 889 case DW_TAG_class_type: 890 case DW_TAG_constant: 891 case DW_TAG_enumeration_type: 892 case DW_TAG_string_type: 893 case DW_TAG_subroutine_type: 894 case DW_TAG_structure_type: 895 case DW_TAG_union_type: 896 case DW_TAG_typedef: 897 case DW_TAG_unspecified_type: 898 if (name && is_declaration == false) 899 { 900 types.Insert (ConstString(name), die.GetOffset()); 901 } 902 break; 903 904 case DW_TAG_namespace: 905 if (name) 906 namespaces.Insert (ConstString(name), die.GetOffset()); 907 break; 908 909 case DW_TAG_variable: 910 if (name && has_location && is_global_or_static_variable) 911 { 912 globals.Insert (ConstString(name), die.GetOffset()); 913 // Be sure to include variables by their mangled and demangled 914 // names if they have any since a variable can have a basename 915 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled 916 // mangled name "(anonymous namespace)::i"... 917 918 // Make sure our mangled name isn't the same string table entry 919 // as our name. If it starts with '_', then it is ok, else compare 920 // the string to make sure it isn't the same and we don't end up 921 // with duplicate entries 922 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) 923 { 924 Mangled mangled (ConstString(mangled_cstr), true); 925 globals.Insert (mangled.GetMangledName(), die.GetOffset()); 926 if (mangled.GetDemangledName()) 927 globals.Insert (mangled.GetDemangledName(), die.GetOffset()); 928 } 929 } 930 break; 931 932 default: 933 continue; 934 } 935 } 936 } 937 938 bool 939 DWARFCompileUnit::Supports_unnamed_objc_bitfields () 940 { 941 if (GetProducer() == eProducerClang) 942 { 943 const uint32_t major_version = GetProducerVersionMajor(); 944 if (major_version > 425 || (major_version == 425 && GetProducerVersionUpdate() >= 13)) 945 return true; 946 else 947 return false; 948 } 949 return true; // Assume all other compilers didn't have incorrect ObjC bitfield info 950 } 951 952 bool 953 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type () 954 { 955 if (GetProducer() == eProducerLLVMGCC) 956 return false; 957 return true; 958 } 959 960 bool 961 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid() 962 { 963 // llvm-gcc makes completely invalid decl file attributes and won't ever 964 // be fixed, so we need to know to ignore these. 965 return GetProducer() == eProducerLLVMGCC; 966 } 967 968 void 969 DWARFCompileUnit::ParseProducerInfo () 970 { 971 m_producer_version_major = UINT32_MAX; 972 m_producer_version_minor = UINT32_MAX; 973 m_producer_version_update = UINT32_MAX; 974 975 const DWARFDebugInfoEntry *die = GetCompileUnitDIEOnly(); 976 if (die) 977 { 978 979 const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL); 980 if (producer_cstr) 981 { 982 RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$"); 983 if (llvm_gcc_regex.Execute (producer_cstr)) 984 { 985 m_producer = eProducerLLVMGCC; 986 } 987 else if (strstr(producer_cstr, "clang")) 988 { 989 static RegularExpression g_clang_version_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"); 990 RegularExpression::Match regex_match(3); 991 if (g_clang_version_regex.Execute (producer_cstr, ®ex_match)) 992 { 993 std::string str; 994 if (regex_match.GetMatchAtIndex (producer_cstr, 1, str)) 995 m_producer_version_major = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10); 996 if (regex_match.GetMatchAtIndex (producer_cstr, 2, str)) 997 m_producer_version_minor = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10); 998 if (regex_match.GetMatchAtIndex (producer_cstr, 3, str)) 999 m_producer_version_update = Args::StringToUInt32(str.c_str(), UINT32_MAX, 10); 1000 } 1001 m_producer = eProducerClang; 1002 } 1003 else if (strstr(producer_cstr, "GNU")) 1004 m_producer = eProducerGCC; 1005 } 1006 } 1007 if (m_producer == eProducerInvalid) 1008 m_producer = eProcucerOther; 1009 } 1010 1011 DWARFCompileUnit::Producer 1012 DWARFCompileUnit::GetProducer () 1013 { 1014 if (m_producer == eProducerInvalid) 1015 ParseProducerInfo (); 1016 return m_producer; 1017 } 1018 1019 1020 uint32_t 1021 DWARFCompileUnit::GetProducerVersionMajor() 1022 { 1023 if (m_producer_version_major == 0) 1024 ParseProducerInfo (); 1025 return m_producer_version_major; 1026 } 1027 1028 uint32_t 1029 DWARFCompileUnit::GetProducerVersionMinor() 1030 { 1031 if (m_producer_version_minor == 0) 1032 ParseProducerInfo (); 1033 return m_producer_version_minor; 1034 } 1035 1036 uint32_t 1037 DWARFCompileUnit::GetProducerVersionUpdate() 1038 { 1039 if (m_producer_version_update == 0) 1040 ParseProducerInfo (); 1041 return m_producer_version_update; 1042 } 1043 1044 bool 1045 DWARFCompileUnit::IsDWARF64() const 1046 { 1047 return m_is_dwarf64; 1048 } 1049 1050