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