1 //===-- SymbolFileDWARF.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 "SymbolFileDWARF.h" 11 12 // Other libraries and framework includes 13 #include "llvm/Support/Casting.h" 14 15 #include "lldb/Core/ArchSpec.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/ModuleList.h" 18 #include "lldb/Core/ModuleSpec.h" 19 #include "lldb/Core/PluginManager.h" 20 #include "lldb/Core/RegularExpression.h" 21 #include "lldb/Core/Scalar.h" 22 #include "lldb/Core/Section.h" 23 #include "lldb/Core/StreamFile.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Timer.h" 26 #include "lldb/Core/Value.h" 27 28 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h" 29 30 #include "lldb/Host/FileSystem.h" 31 #include "lldb/Host/Host.h" 32 33 #include "lldb/Interpreter/OptionValueFileSpecList.h" 34 #include "lldb/Interpreter/OptionValueProperties.h" 35 36 #include "lldb/Symbol/Block.h" 37 #include "lldb/Symbol/ClangASTContext.h" 38 #include "lldb/Symbol/CompilerDecl.h" 39 #include "lldb/Symbol/CompilerDeclContext.h" 40 #include "lldb/Symbol/CompileUnit.h" 41 #include "lldb/Symbol/LineTable.h" 42 #include "lldb/Symbol/ObjectFile.h" 43 #include "lldb/Symbol/SymbolVendor.h" 44 #include "lldb/Symbol/TypeSystem.h" 45 #include "lldb/Symbol/VariableList.h" 46 #include "lldb/Symbol/TypeMap.h" 47 48 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 49 #include "Plugins/Language/ObjC/ObjCLanguage.h" 50 51 #include "lldb/Target/Language.h" 52 53 #include "lldb/Utility/TaskPool.h" 54 55 #include "DWARFASTParser.h" 56 #include "DWARFCompileUnit.h" 57 #include "DWARFDebugAbbrev.h" 58 #include "DWARFDebugAranges.h" 59 #include "DWARFDebugInfo.h" 60 #include "DWARFDebugLine.h" 61 #include "DWARFDebugPubnames.h" 62 #include "DWARFDebugRanges.h" 63 #include "DWARFDeclContext.h" 64 #include "DWARFDIECollection.h" 65 #include "DWARFFormValue.h" 66 #include "LogChannelDWARF.h" 67 #include "SymbolFileDWARFDwo.h" 68 #include "SymbolFileDWARFDebugMap.h" 69 70 #include <map> 71 72 #include <ctype.h> 73 #include <string.h> 74 75 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 76 77 #ifdef ENABLE_DEBUG_PRINTF 78 #include <stdio.h> 79 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) 80 #else 81 #define DEBUG_PRINTF(fmt, ...) 82 #endif 83 84 using namespace lldb; 85 using namespace lldb_private; 86 87 //static inline bool 88 //child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag) 89 //{ 90 // switch (tag) 91 // { 92 // default: 93 // break; 94 // case DW_TAG_subprogram: 95 // case DW_TAG_inlined_subroutine: 96 // case DW_TAG_class_type: 97 // case DW_TAG_structure_type: 98 // case DW_TAG_union_type: 99 // return true; 100 // } 101 // return false; 102 //} 103 // 104 105 namespace { 106 107 PropertyDefinition 108 g_properties[] = 109 { 110 { "comp-dir-symlink-paths" , OptionValue::eTypeFileSpecList, true, 0 , nullptr, nullptr, "If the DW_AT_comp_dir matches any of these paths the symbolic links will be resolved at DWARF parse time." }, 111 { nullptr , OptionValue::eTypeInvalid , false, 0, nullptr, nullptr, nullptr } 112 }; 113 114 enum 115 { 116 ePropertySymLinkPaths 117 }; 118 119 120 class PluginProperties : public Properties 121 { 122 public: 123 static ConstString 124 GetSettingName() 125 { 126 return SymbolFileDWARF::GetPluginNameStatic(); 127 } 128 129 PluginProperties() 130 { 131 m_collection_sp.reset (new OptionValueProperties(GetSettingName())); 132 m_collection_sp->Initialize(g_properties); 133 } 134 135 FileSpecList& 136 GetSymLinkPaths() 137 { 138 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, true, ePropertySymLinkPaths); 139 assert(option_value); 140 return option_value->GetCurrentValue(); 141 } 142 143 }; 144 145 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP; 146 147 static const SymbolFileDWARFPropertiesSP& 148 GetGlobalPluginProperties() 149 { 150 static const auto g_settings_sp(std::make_shared<PluginProperties>()); 151 return g_settings_sp; 152 } 153 154 } // anonymous namespace end 155 156 157 static const char* 158 removeHostnameFromPathname(const char* path_from_dwarf) 159 { 160 if (!path_from_dwarf || !path_from_dwarf[0]) 161 { 162 return path_from_dwarf; 163 } 164 165 const char *colon_pos = strchr(path_from_dwarf, ':'); 166 if (nullptr == colon_pos) 167 { 168 return path_from_dwarf; 169 } 170 171 const char *slash_pos = strchr(path_from_dwarf, '/'); 172 if (slash_pos && (slash_pos < colon_pos)) 173 { 174 return path_from_dwarf; 175 } 176 177 // check whether we have a windows path, and so the first character 178 // is a drive-letter not a hostname. 179 if ( 180 colon_pos == path_from_dwarf + 1 && 181 isalpha(*path_from_dwarf) && 182 strlen(path_from_dwarf) > 2 && 183 '\\' == path_from_dwarf[2]) 184 { 185 return path_from_dwarf; 186 } 187 188 return colon_pos + 1; 189 } 190 191 static const char* 192 resolveCompDir(const char* path_from_dwarf) 193 { 194 if (!path_from_dwarf) 195 return nullptr; 196 197 // DWARF2/3 suggests the form hostname:pathname for compilation directory. 198 // Remove the host part if present. 199 const char* local_path = removeHostnameFromPathname(path_from_dwarf); 200 if (!local_path) 201 return nullptr; 202 203 bool is_symlink = false; 204 FileSpec local_path_spec(local_path, false); 205 const auto& file_specs = GetGlobalPluginProperties()->GetSymLinkPaths(); 206 for (size_t i = 0; i < file_specs.GetSize() && !is_symlink; ++i) 207 is_symlink = FileSpec::Equal(file_specs.GetFileSpecAtIndex(i), local_path_spec, true); 208 209 if (!is_symlink) 210 return local_path; 211 212 if (!local_path_spec.IsSymbolicLink()) 213 return local_path; 214 215 FileSpec resolved_local_path_spec; 216 const auto error = FileSystem::Readlink(local_path_spec, resolved_local_path_spec); 217 if (error.Success()) 218 return resolved_local_path_spec.GetCString(); 219 220 return nullptr; 221 } 222 223 224 void 225 SymbolFileDWARF::Initialize() 226 { 227 LogChannelDWARF::Initialize(); 228 PluginManager::RegisterPlugin (GetPluginNameStatic(), 229 GetPluginDescriptionStatic(), 230 CreateInstance, 231 DebuggerInitialize); 232 } 233 234 void 235 SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) 236 { 237 if (!PluginManager::GetSettingForSymbolFilePlugin(debugger, PluginProperties::GetSettingName())) 238 { 239 const bool is_global_setting = true; 240 PluginManager::CreateSettingForSymbolFilePlugin(debugger, 241 GetGlobalPluginProperties()->GetValueProperties(), 242 ConstString ("Properties for the dwarf symbol-file plug-in."), 243 is_global_setting); 244 } 245 } 246 247 void 248 SymbolFileDWARF::Terminate() 249 { 250 PluginManager::UnregisterPlugin (CreateInstance); 251 LogChannelDWARF::Initialize(); 252 } 253 254 255 lldb_private::ConstString 256 SymbolFileDWARF::GetPluginNameStatic() 257 { 258 static ConstString g_name("dwarf"); 259 return g_name; 260 } 261 262 const char * 263 SymbolFileDWARF::GetPluginDescriptionStatic() 264 { 265 return "DWARF and DWARF3 debug symbol file reader."; 266 } 267 268 269 SymbolFile* 270 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file) 271 { 272 return new SymbolFileDWARF(obj_file); 273 } 274 275 TypeList * 276 SymbolFileDWARF::GetTypeList () 277 { 278 if (GetDebugMapSymfile ()) 279 return m_debug_map_symfile->GetTypeList(); 280 return m_obj_file->GetModule()->GetTypeList(); 281 282 } 283 void 284 SymbolFileDWARF::GetTypes (const DWARFDIE &die, 285 dw_offset_t min_die_offset, 286 dw_offset_t max_die_offset, 287 uint32_t type_mask, 288 TypeSet &type_set) 289 { 290 if (die) 291 { 292 const dw_offset_t die_offset = die.GetOffset(); 293 294 if (die_offset >= max_die_offset) 295 return; 296 297 if (die_offset >= min_die_offset) 298 { 299 const dw_tag_t tag = die.Tag(); 300 301 bool add_type = false; 302 303 switch (tag) 304 { 305 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break; 306 case DW_TAG_unspecified_type: 307 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break; 308 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break; 309 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break; 310 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break; 311 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break; 312 case DW_TAG_subroutine_type: 313 case DW_TAG_subprogram: 314 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break; 315 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break; 316 case DW_TAG_rvalue_reference_type: 317 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break; 318 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break; 319 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break; 320 } 321 322 if (add_type) 323 { 324 const bool assert_not_being_parsed = true; 325 Type *type = ResolveTypeUID (die, assert_not_being_parsed); 326 if (type) 327 { 328 if (type_set.find(type) == type_set.end()) 329 type_set.insert(type); 330 } 331 } 332 } 333 334 for (DWARFDIE child_die = die.GetFirstChild(); 335 child_die.IsValid(); 336 child_die = child_die.GetSibling()) 337 { 338 GetTypes (child_die, min_die_offset, max_die_offset, type_mask, type_set); 339 } 340 } 341 } 342 343 size_t 344 SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope, 345 uint32_t type_mask, 346 TypeList &type_list) 347 348 { 349 TypeSet type_set; 350 351 CompileUnit *comp_unit = NULL; 352 DWARFCompileUnit* dwarf_cu = NULL; 353 if (sc_scope) 354 comp_unit = sc_scope->CalculateSymbolContextCompileUnit(); 355 356 if (comp_unit) 357 { 358 dwarf_cu = GetDWARFCompileUnit(comp_unit); 359 if (dwarf_cu == 0) 360 return 0; 361 GetTypes (dwarf_cu->DIE(), 362 dwarf_cu->GetOffset(), 363 dwarf_cu->GetNextCompileUnitOffset(), 364 type_mask, 365 type_set); 366 } 367 else 368 { 369 DWARFDebugInfo* info = DebugInfo(); 370 if (info) 371 { 372 const size_t num_cus = info->GetNumCompileUnits(); 373 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx) 374 { 375 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx); 376 if (dwarf_cu) 377 { 378 GetTypes (dwarf_cu->DIE(), 379 0, 380 UINT32_MAX, 381 type_mask, 382 type_set); 383 } 384 } 385 } 386 } 387 388 std::set<CompilerType> compiler_type_set; 389 size_t num_types_added = 0; 390 for (Type *type : type_set) 391 { 392 CompilerType compiler_type = type->GetForwardCompilerType (); 393 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) 394 { 395 compiler_type_set.insert(compiler_type); 396 type_list.Insert (type->shared_from_this()); 397 ++num_types_added; 398 } 399 } 400 return num_types_added; 401 } 402 403 404 //---------------------------------------------------------------------- 405 // Gets the first parent that is a lexical block, function or inlined 406 // subroutine, or compile unit. 407 //---------------------------------------------------------------------- 408 DWARFDIE 409 SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) 410 { 411 DWARFDIE die; 412 for (die = child_die.GetParent(); die; die = die.GetParent()) 413 { 414 dw_tag_t tag = die.Tag(); 415 416 switch (tag) 417 { 418 case DW_TAG_compile_unit: 419 case DW_TAG_subprogram: 420 case DW_TAG_inlined_subroutine: 421 case DW_TAG_lexical_block: 422 return die; 423 } 424 } 425 return DWARFDIE(); 426 } 427 428 429 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) : 430 SymbolFile (objfile), 431 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID 432 m_debug_map_module_wp (), 433 m_debug_map_symfile (NULL), 434 m_data_debug_abbrev (), 435 m_data_debug_aranges (), 436 m_data_debug_frame (), 437 m_data_debug_info (), 438 m_data_debug_line (), 439 m_data_debug_loc (), 440 m_data_debug_ranges (), 441 m_data_debug_str (), 442 m_data_apple_names (), 443 m_data_apple_types (), 444 m_data_apple_namespaces (), 445 m_abbr(), 446 m_info(), 447 m_line(), 448 m_apple_names_ap (), 449 m_apple_types_ap (), 450 m_apple_namespaces_ap (), 451 m_apple_objc_ap (), 452 m_function_basename_index(), 453 m_function_fullname_index(), 454 m_function_method_index(), 455 m_function_selector_index(), 456 m_objc_class_selectors_index(), 457 m_global_index(), 458 m_type_index(), 459 m_namespace_index(), 460 m_indexed (false), 461 m_using_apple_tables (false), 462 m_fetched_external_modules (false), 463 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate), 464 m_ranges(), 465 m_unique_ast_type_map () 466 { 467 } 468 469 SymbolFileDWARF::~SymbolFileDWARF() 470 { 471 } 472 473 static const ConstString & 474 GetDWARFMachOSegmentName () 475 { 476 static ConstString g_dwarf_section_name ("__DWARF"); 477 return g_dwarf_section_name; 478 } 479 480 UniqueDWARFASTTypeMap & 481 SymbolFileDWARF::GetUniqueDWARFASTTypeMap () 482 { 483 if (GetDebugMapSymfile ()) 484 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap (); 485 return m_unique_ast_type_map; 486 } 487 488 TypeSystem * 489 SymbolFileDWARF::GetTypeSystemForLanguage (LanguageType language) 490 { 491 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile (); 492 TypeSystem *type_system; 493 if (debug_map_symfile) 494 { 495 type_system = debug_map_symfile->GetTypeSystemForLanguage(language); 496 } 497 else 498 { 499 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 500 if (type_system) 501 type_system->SetSymbolFile(this); 502 } 503 return type_system; 504 } 505 506 void 507 SymbolFileDWARF::InitializeObject() 508 { 509 ModuleSP module_sp (m_obj_file->GetModule()); 510 if (module_sp) 511 { 512 const SectionList *section_list = module_sp->GetSectionList(); 513 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 514 515 // Memory map the DWARF mach-o segment so we have everything mmap'ed 516 // to keep our heap memory usage down. 517 if (section) 518 m_obj_file->MemoryMapSectionData(section, m_dwarf_data); 519 } 520 521 get_apple_names_data(); 522 if (m_data_apple_names.m_data.GetByteSize() > 0) 523 { 524 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names.m_data, 525 get_debug_str_data(), 526 ".apple_names")); 527 if (m_apple_names_ap->IsValid()) 528 m_using_apple_tables = true; 529 else 530 m_apple_names_ap.reset(); 531 } 532 get_apple_types_data(); 533 if (m_data_apple_types.m_data.GetByteSize() > 0) 534 { 535 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types.m_data, 536 get_debug_str_data(), 537 ".apple_types")); 538 if (m_apple_types_ap->IsValid()) 539 m_using_apple_tables = true; 540 else 541 m_apple_types_ap.reset(); 542 } 543 544 get_apple_namespaces_data(); 545 if (m_data_apple_namespaces.m_data.GetByteSize() > 0) 546 { 547 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces.m_data, 548 get_debug_str_data(), 549 ".apple_namespaces")); 550 if (m_apple_namespaces_ap->IsValid()) 551 m_using_apple_tables = true; 552 else 553 m_apple_namespaces_ap.reset(); 554 } 555 556 get_apple_objc_data(); 557 if (m_data_apple_objc.m_data.GetByteSize() > 0) 558 { 559 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc.m_data, 560 get_debug_str_data(), 561 ".apple_objc")); 562 if (m_apple_objc_ap->IsValid()) 563 m_using_apple_tables = true; 564 else 565 m_apple_objc_ap.reset(); 566 } 567 } 568 569 bool 570 SymbolFileDWARF::SupportedVersion(uint16_t version) 571 { 572 return version == 2 || version == 3 || version == 4; 573 } 574 575 uint32_t 576 SymbolFileDWARF::CalculateAbilities () 577 { 578 uint32_t abilities = 0; 579 if (m_obj_file != NULL) 580 { 581 const Section* section = NULL; 582 const SectionList *section_list = m_obj_file->GetSectionList(); 583 if (section_list == NULL) 584 return 0; 585 586 uint64_t debug_abbrev_file_size = 0; 587 uint64_t debug_info_file_size = 0; 588 uint64_t debug_line_file_size = 0; 589 590 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 591 592 if (section) 593 section_list = §ion->GetChildren (); 594 595 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get(); 596 if (section != NULL) 597 { 598 debug_info_file_size = section->GetFileSize(); 599 600 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get(); 601 if (section) 602 debug_abbrev_file_size = section->GetFileSize(); 603 604 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get(); 605 if (section) 606 debug_line_file_size = section->GetFileSize(); 607 } 608 else 609 { 610 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString(); 611 if (symfile_dir_cstr) 612 { 613 if (strcasestr(symfile_dir_cstr, ".dsym")) 614 { 615 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) 616 { 617 // We have a dSYM file that didn't have a any debug info. 618 // If the string table has a size of 1, then it was made from 619 // an executable with no debug info, or from an executable that 620 // was stripped. 621 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get(); 622 if (section && section->GetFileSize() == 1) 623 { 624 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info."); 625 } 626 } 627 } 628 } 629 } 630 631 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0) 632 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes; 633 634 if (debug_line_file_size > 0) 635 abilities |= LineTables; 636 } 637 return abilities; 638 } 639 640 const DWARFDataExtractor& 641 SymbolFileDWARF::GetCachedSectionData (lldb::SectionType sect_type, DWARFDataSegment& data_segment) 642 { 643 std::call_once(data_segment.m_flag, 644 &SymbolFileDWARF::LoadSectionData, 645 this, 646 sect_type, 647 std::ref(data_segment.m_data)); 648 return data_segment.m_data; 649 } 650 651 void 652 SymbolFileDWARF::LoadSectionData (lldb::SectionType sect_type, DWARFDataExtractor& data) 653 { 654 ModuleSP module_sp (m_obj_file->GetModule()); 655 const SectionList *section_list = module_sp->GetSectionList(); 656 if (section_list) 657 { 658 SectionSP section_sp (section_list->FindSectionByType(sect_type, true)); 659 if (section_sp) 660 { 661 // See if we memory mapped the DWARF segment? 662 if (m_dwarf_data.GetByteSize()) 663 { 664 data.SetData(m_dwarf_data, section_sp->GetOffset(), section_sp->GetFileSize()); 665 } 666 else 667 { 668 if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0) 669 data.Clear(); 670 } 671 } 672 } 673 } 674 675 const DWARFDataExtractor& 676 SymbolFileDWARF::get_debug_abbrev_data() 677 { 678 return GetCachedSectionData (eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev); 679 } 680 681 const DWARFDataExtractor& 682 SymbolFileDWARF::get_debug_addr_data() 683 { 684 return GetCachedSectionData (eSectionTypeDWARFDebugAddr, m_data_debug_addr); 685 } 686 687 const DWARFDataExtractor& 688 SymbolFileDWARF::get_debug_aranges_data() 689 { 690 return GetCachedSectionData (eSectionTypeDWARFDebugAranges, m_data_debug_aranges); 691 } 692 693 const DWARFDataExtractor& 694 SymbolFileDWARF::get_debug_frame_data() 695 { 696 return GetCachedSectionData (eSectionTypeDWARFDebugFrame, m_data_debug_frame); 697 } 698 699 const DWARFDataExtractor& 700 SymbolFileDWARF::get_debug_info_data() 701 { 702 return GetCachedSectionData (eSectionTypeDWARFDebugInfo, m_data_debug_info); 703 } 704 705 const DWARFDataExtractor& 706 SymbolFileDWARF::get_debug_line_data() 707 { 708 return GetCachedSectionData (eSectionTypeDWARFDebugLine, m_data_debug_line); 709 } 710 711 const DWARFDataExtractor& 712 SymbolFileDWARF::get_debug_loc_data() 713 { 714 return GetCachedSectionData (eSectionTypeDWARFDebugLoc, m_data_debug_loc); 715 } 716 717 const DWARFDataExtractor& 718 SymbolFileDWARF::get_debug_ranges_data() 719 { 720 return GetCachedSectionData (eSectionTypeDWARFDebugRanges, m_data_debug_ranges); 721 } 722 723 const DWARFDataExtractor& 724 SymbolFileDWARF::get_debug_str_data() 725 { 726 return GetCachedSectionData (eSectionTypeDWARFDebugStr, m_data_debug_str); 727 } 728 729 const DWARFDataExtractor& 730 SymbolFileDWARF::get_debug_str_offsets_data() 731 { 732 return GetCachedSectionData (eSectionTypeDWARFDebugStrOffsets, m_data_debug_str_offsets); 733 } 734 735 const DWARFDataExtractor& 736 SymbolFileDWARF::get_apple_names_data() 737 { 738 return GetCachedSectionData (eSectionTypeDWARFAppleNames, m_data_apple_names); 739 } 740 741 const DWARFDataExtractor& 742 SymbolFileDWARF::get_apple_types_data() 743 { 744 return GetCachedSectionData (eSectionTypeDWARFAppleTypes, m_data_apple_types); 745 } 746 747 const DWARFDataExtractor& 748 SymbolFileDWARF::get_apple_namespaces_data() 749 { 750 return GetCachedSectionData (eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces); 751 } 752 753 const DWARFDataExtractor& 754 SymbolFileDWARF::get_apple_objc_data() 755 { 756 return GetCachedSectionData (eSectionTypeDWARFAppleObjC, m_data_apple_objc); 757 } 758 759 760 DWARFDebugAbbrev* 761 SymbolFileDWARF::DebugAbbrev() 762 { 763 if (m_abbr.get() == NULL) 764 { 765 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data(); 766 if (debug_abbrev_data.GetByteSize() > 0) 767 { 768 m_abbr.reset(new DWARFDebugAbbrev()); 769 if (m_abbr.get()) 770 m_abbr->Parse(debug_abbrev_data); 771 } 772 } 773 return m_abbr.get(); 774 } 775 776 const DWARFDebugAbbrev* 777 SymbolFileDWARF::DebugAbbrev() const 778 { 779 return m_abbr.get(); 780 } 781 782 783 DWARFDebugInfo* 784 SymbolFileDWARF::DebugInfo() 785 { 786 if (m_info.get() == NULL) 787 { 788 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", 789 __PRETTY_FUNCTION__, static_cast<void*>(this)); 790 if (get_debug_info_data().GetByteSize() > 0) 791 { 792 m_info.reset(new DWARFDebugInfo()); 793 if (m_info.get()) 794 { 795 m_info->SetDwarfData(this); 796 } 797 } 798 } 799 return m_info.get(); 800 } 801 802 const DWARFDebugInfo* 803 SymbolFileDWARF::DebugInfo() const 804 { 805 return m_info.get(); 806 } 807 808 DWARFCompileUnit* 809 SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) 810 { 811 if (!comp_unit) 812 return nullptr; 813 814 DWARFDebugInfo* info = DebugInfo(); 815 if (info) 816 { 817 if (GetDebugMapSymfile ()) 818 { 819 // The debug map symbol file made the compile units for this DWARF 820 // file which is .o file with DWARF in it, and we should have 821 // only 1 compile unit which is at offset zero in the DWARF. 822 // TODO: modify to support LTO .o files where each .o file might 823 // have multiple DW_TAG_compile_unit tags. 824 825 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0); 826 if (dwarf_cu && dwarf_cu->GetUserData() == NULL) 827 dwarf_cu->SetUserData(comp_unit); 828 return dwarf_cu; 829 } 830 else 831 { 832 // Just a normal DWARF file whose user ID for the compile unit is 833 // the DWARF offset itself 834 835 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID()); 836 if (dwarf_cu && dwarf_cu->GetUserData() == NULL) 837 dwarf_cu->SetUserData(comp_unit); 838 return dwarf_cu; 839 840 } 841 } 842 return NULL; 843 } 844 845 846 DWARFDebugRanges* 847 SymbolFileDWARF::DebugRanges() 848 { 849 if (m_ranges.get() == NULL) 850 { 851 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", 852 __PRETTY_FUNCTION__, static_cast<void*>(this)); 853 if (get_debug_ranges_data().GetByteSize() > 0) 854 { 855 m_ranges.reset(new DWARFDebugRanges()); 856 if (m_ranges.get()) 857 m_ranges->Extract(this); 858 } 859 } 860 return m_ranges.get(); 861 } 862 863 const DWARFDebugRanges* 864 SymbolFileDWARF::DebugRanges() const 865 { 866 return m_ranges.get(); 867 } 868 869 lldb::CompUnitSP 870 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx) 871 { 872 CompUnitSP cu_sp; 873 if (dwarf_cu) 874 { 875 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData(); 876 if (comp_unit) 877 { 878 // We already parsed this compile unit, had out a shared pointer to it 879 cu_sp = comp_unit->shared_from_this(); 880 } 881 else 882 { 883 if (dwarf_cu->GetSymbolFileDWARF() != this) 884 { 885 return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu, cu_idx); 886 } 887 else if (GetDebugMapSymfile ()) 888 { 889 // Let the debug map create the compile unit 890 cu_sp = m_debug_map_symfile->GetCompileUnit(this); 891 dwarf_cu->SetUserData(cu_sp.get()); 892 } 893 else 894 { 895 ModuleSP module_sp (m_obj_file->GetModule()); 896 if (module_sp) 897 { 898 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly (); 899 if (cu_die) 900 { 901 FileSpec cu_file_spec{cu_die.GetName(), false}; 902 if (cu_file_spec) 903 { 904 // If we have a full path to the compile unit, we don't need to resolve 905 // the file. This can be expensive e.g. when the source files are NFS mounted. 906 if (cu_file_spec.IsRelative()) 907 { 908 const char *cu_comp_dir{cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)}; 909 cu_file_spec.PrependPathComponent(resolveCompDir(cu_comp_dir)); 910 } 911 912 std::string remapped_file; 913 if (module_sp->RemapSourceFile(cu_file_spec.GetCString(), remapped_file)) 914 cu_file_spec.SetFile(remapped_file, false); 915 } 916 917 LanguageType cu_language = DWARFCompileUnit::LanguageTypeFromDWARF(cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0)); 918 919 bool is_optimized = dwarf_cu->GetIsOptimized (); 920 cu_sp.reset(new CompileUnit (module_sp, 921 dwarf_cu, 922 cu_file_spec, 923 dwarf_cu->GetID(), 924 cu_language, 925 is_optimized)); 926 if (cu_sp) 927 { 928 // If we just created a compile unit with an invalid file spec, try and get the 929 // first entry in the supports files from the line table as that should be the 930 // compile unit. 931 if (!cu_file_spec) 932 { 933 cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1); 934 if (cu_file_spec) 935 { 936 (FileSpec &)(*cu_sp) = cu_file_spec; 937 // Also fix the invalid file spec which was copied from the compile unit. 938 cu_sp->GetSupportFiles().Replace(0, cu_file_spec); 939 } 940 } 941 942 dwarf_cu->SetUserData(cu_sp.get()); 943 944 // Figure out the compile unit index if we weren't given one 945 if (cu_idx == UINT32_MAX) 946 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx); 947 948 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp); 949 } 950 } 951 } 952 } 953 } 954 } 955 return cu_sp; 956 } 957 958 uint32_t 959 SymbolFileDWARF::GetNumCompileUnits() 960 { 961 DWARFDebugInfo* info = DebugInfo(); 962 if (info) 963 return info->GetNumCompileUnits(); 964 return 0; 965 } 966 967 CompUnitSP 968 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) 969 { 970 CompUnitSP cu_sp; 971 DWARFDebugInfo* info = DebugInfo(); 972 if (info) 973 { 974 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx); 975 if (dwarf_cu) 976 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx); 977 } 978 return cu_sp; 979 } 980 981 Function * 982 SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, const DWARFDIE &die) 983 { 984 if (die.IsValid()) 985 { 986 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType()); 987 988 if (type_system) 989 { 990 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 991 if (dwarf_ast) 992 return dwarf_ast->ParseFunctionFromDWARF(sc, die); 993 } 994 } 995 return nullptr; 996 } 997 998 bool 999 SymbolFileDWARF::FixupAddress (Address &addr) 1000 { 1001 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile (); 1002 if (debug_map_symfile) 1003 { 1004 return debug_map_symfile->LinkOSOAddress(addr); 1005 } 1006 // This is a normal DWARF file, no address fixups need to happen 1007 return true; 1008 } 1009 lldb::LanguageType 1010 SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc) 1011 { 1012 assert (sc.comp_unit); 1013 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1014 if (dwarf_cu) 1015 return dwarf_cu->GetLanguageType(); 1016 else 1017 return eLanguageTypeUnknown; 1018 } 1019 1020 size_t 1021 SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) 1022 { 1023 assert (sc.comp_unit); 1024 size_t functions_added = 0; 1025 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1026 if (dwarf_cu) 1027 { 1028 DWARFDIECollection function_dies; 1029 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies); 1030 size_t func_idx; 1031 for (func_idx = 0; func_idx < num_functions; ++func_idx) 1032 { 1033 DWARFDIE die = function_dies.GetDIEAtIndex(func_idx); 1034 if (sc.comp_unit->FindFunctionByUID (die.GetID()).get() == NULL) 1035 { 1036 if (ParseCompileUnitFunction(sc, die)) 1037 ++functions_added; 1038 } 1039 } 1040 //FixupTypes(); 1041 } 1042 return functions_added; 1043 } 1044 1045 bool 1046 SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files) 1047 { 1048 assert (sc.comp_unit); 1049 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1050 if (dwarf_cu) 1051 { 1052 const DWARFDIE cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 1053 1054 if (cu_die) 1055 { 1056 const char * cu_comp_dir = resolveCompDir(cu_die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr)); 1057 1058 const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET); 1059 1060 // All file indexes in DWARF are one based and a file of index zero is 1061 // supposed to be the compile unit itself. 1062 support_files.Append (*sc.comp_unit); 1063 1064 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files); 1065 } 1066 } 1067 return false; 1068 } 1069 1070 bool 1071 SymbolFileDWARF::ParseImportedModules (const lldb_private::SymbolContext &sc, std::vector<lldb_private::ConstString> &imported_modules) 1072 { 1073 assert (sc.comp_unit); 1074 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1075 if (dwarf_cu) 1076 { 1077 if (ClangModulesDeclVendor::LanguageSupportsClangModules(sc.comp_unit->GetLanguage())) 1078 { 1079 UpdateExternalModuleListIfNeeded(); 1080 for (const auto &pair : m_external_type_modules) 1081 { 1082 imported_modules.push_back(pair.first); 1083 } 1084 } 1085 } 1086 return false; 1087 } 1088 1089 struct ParseDWARFLineTableCallbackInfo 1090 { 1091 LineTable* line_table; 1092 std::unique_ptr<LineSequence> sequence_ap; 1093 lldb::addr_t addr_mask; 1094 }; 1095 1096 //---------------------------------------------------------------------- 1097 // ParseStatementTableCallback 1098 //---------------------------------------------------------------------- 1099 static void 1100 ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData) 1101 { 1102 if (state.row == DWARFDebugLine::State::StartParsingLineTable) 1103 { 1104 // Just started parsing the line table 1105 } 1106 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) 1107 { 1108 // Done parsing line table, nothing to do for the cleanup 1109 } 1110 else 1111 { 1112 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData; 1113 LineTable* line_table = info->line_table; 1114 1115 // If this is our first time here, we need to create a 1116 // sequence container. 1117 if (!info->sequence_ap.get()) 1118 { 1119 info->sequence_ap.reset(line_table->CreateLineSequenceContainer()); 1120 assert(info->sequence_ap.get()); 1121 } 1122 line_table->AppendLineEntryToSequence (info->sequence_ap.get(), 1123 state.address & info->addr_mask, 1124 state.line, 1125 state.column, 1126 state.file, 1127 state.is_stmt, 1128 state.basic_block, 1129 state.prologue_end, 1130 state.epilogue_begin, 1131 state.end_sequence); 1132 if (state.end_sequence) 1133 { 1134 // First, put the current sequence into the line table. 1135 line_table->InsertSequence(info->sequence_ap.get()); 1136 // Then, empty it to prepare for the next sequence. 1137 info->sequence_ap->Clear(); 1138 } 1139 } 1140 } 1141 1142 bool 1143 SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc) 1144 { 1145 assert (sc.comp_unit); 1146 if (sc.comp_unit->GetLineTable() != NULL) 1147 return true; 1148 1149 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1150 if (dwarf_cu) 1151 { 1152 const DWARFDIE dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 1153 if (dwarf_cu_die) 1154 { 1155 const dw_offset_t cu_line_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, DW_INVALID_OFFSET); 1156 if (cu_line_offset != DW_INVALID_OFFSET) 1157 { 1158 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit)); 1159 if (line_table_ap.get()) 1160 { 1161 ParseDWARFLineTableCallbackInfo info; 1162 info.line_table = line_table_ap.get(); 1163 1164 /* 1165 * MIPS: 1166 * The SymbolContext may not have a valid target, thus we may not be able 1167 * to call Address::GetOpcodeLoadAddress() which would clear the bit #0 1168 * for MIPS. Use ArchSpec to clear the bit #0. 1169 */ 1170 ArchSpec arch; 1171 GetObjectFile()->GetArchitecture(arch); 1172 switch (arch.GetMachine()) 1173 { 1174 case llvm::Triple::mips: 1175 case llvm::Triple::mipsel: 1176 case llvm::Triple::mips64: 1177 case llvm::Triple::mips64el: 1178 info.addr_mask = ~((lldb::addr_t)1); 1179 break; 1180 default: 1181 info.addr_mask = ~((lldb::addr_t)0); 1182 break; 1183 } 1184 1185 lldb::offset_t offset = cu_line_offset; 1186 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info); 1187 if (m_debug_map_symfile) 1188 { 1189 // We have an object file that has a line table with addresses 1190 // that are not linked. We need to link the line table and convert 1191 // the addresses that are relative to the .o file into addresses 1192 // for the main executable. 1193 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get())); 1194 } 1195 else 1196 { 1197 sc.comp_unit->SetLineTable(line_table_ap.release()); 1198 return true; 1199 } 1200 } 1201 } 1202 } 1203 } 1204 return false; 1205 } 1206 1207 size_t 1208 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext& sc, 1209 Block *parent_block, 1210 const DWARFDIE &orig_die, 1211 addr_t subprogram_low_pc, 1212 uint32_t depth) 1213 { 1214 size_t blocks_added = 0; 1215 DWARFDIE die = orig_die; 1216 while (die) 1217 { 1218 dw_tag_t tag = die.Tag(); 1219 1220 switch (tag) 1221 { 1222 case DW_TAG_inlined_subroutine: 1223 case DW_TAG_subprogram: 1224 case DW_TAG_lexical_block: 1225 { 1226 Block *block = NULL; 1227 if (tag == DW_TAG_subprogram) 1228 { 1229 // Skip any DW_TAG_subprogram DIEs that are inside 1230 // of a normal or inlined functions. These will be 1231 // parsed on their own as separate entities. 1232 1233 if (depth > 0) 1234 break; 1235 1236 block = parent_block; 1237 } 1238 else 1239 { 1240 BlockSP block_sp(new Block (die.GetID())); 1241 parent_block->AddChild(block_sp); 1242 block = block_sp.get(); 1243 } 1244 DWARFRangeList ranges; 1245 const char *name = NULL; 1246 const char *mangled_name = NULL; 1247 1248 int decl_file = 0; 1249 int decl_line = 0; 1250 int decl_column = 0; 1251 int call_file = 0; 1252 int call_line = 0; 1253 int call_column = 0; 1254 if (die.GetDIENamesAndRanges (name, 1255 mangled_name, 1256 ranges, 1257 decl_file, decl_line, decl_column, 1258 call_file, call_line, call_column, nullptr)) 1259 { 1260 if (tag == DW_TAG_subprogram) 1261 { 1262 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS); 1263 subprogram_low_pc = ranges.GetMinRangeBase(0); 1264 } 1265 else if (tag == DW_TAG_inlined_subroutine) 1266 { 1267 // We get called here for inlined subroutines in two ways. 1268 // The first time is when we are making the Function object 1269 // for this inlined concrete instance. Since we're creating a top level block at 1270 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to 1271 // adjust the containing address. 1272 // The second time is when we are parsing the blocks inside the function that contains 1273 // the inlined concrete instance. Since these will be blocks inside the containing "real" 1274 // function the offset will be for that function. 1275 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) 1276 { 1277 subprogram_low_pc = ranges.GetMinRangeBase(0); 1278 } 1279 } 1280 1281 const size_t num_ranges = ranges.GetSize(); 1282 for (size_t i = 0; i<num_ranges; ++i) 1283 { 1284 const DWARFRangeList::Entry &range = ranges.GetEntryRef (i); 1285 const addr_t range_base = range.GetRangeBase(); 1286 if (range_base >= subprogram_low_pc) 1287 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize())); 1288 else 1289 { 1290 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64 ") which has a base that is less than the function's low PC 0x%" PRIx64 ". Please file a bug and attach the file at the start of this error message", 1291 block->GetID(), 1292 range_base, 1293 range.GetRangeEnd(), 1294 subprogram_low_pc); 1295 } 1296 } 1297 block->FinalizeRanges (); 1298 1299 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL)) 1300 { 1301 std::unique_ptr<Declaration> decl_ap; 1302 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1303 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 1304 decl_line, decl_column)); 1305 1306 std::unique_ptr<Declaration> call_ap; 1307 if (call_file != 0 || call_line != 0 || call_column != 0) 1308 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file), 1309 call_line, call_column)); 1310 1311 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get()); 1312 } 1313 1314 ++blocks_added; 1315 1316 if (die.HasChildren()) 1317 { 1318 blocks_added += ParseFunctionBlocks (sc, 1319 block, 1320 die.GetFirstChild(), 1321 subprogram_low_pc, 1322 depth + 1); 1323 } 1324 } 1325 } 1326 break; 1327 default: 1328 break; 1329 } 1330 1331 // Only parse siblings of the block if we are not at depth zero. A depth 1332 // of zero indicates we are currently parsing the top level 1333 // DW_TAG_subprogram DIE 1334 1335 if (depth == 0) 1336 die.Clear(); 1337 else 1338 die = die.GetSibling(); 1339 } 1340 return blocks_added; 1341 } 1342 1343 bool 1344 SymbolFileDWARF::ClassOrStructIsVirtual (const DWARFDIE &parent_die) 1345 { 1346 if (parent_die) 1347 { 1348 for (DWARFDIE die = parent_die.GetFirstChild(); die; die = die.GetSibling()) 1349 { 1350 dw_tag_t tag = die.Tag(); 1351 bool check_virtuality = false; 1352 switch (tag) 1353 { 1354 case DW_TAG_inheritance: 1355 case DW_TAG_subprogram: 1356 check_virtuality = true; 1357 break; 1358 default: 1359 break; 1360 } 1361 if (check_virtuality) 1362 { 1363 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0) 1364 return true; 1365 } 1366 } 1367 } 1368 return false; 1369 } 1370 1371 void 1372 SymbolFileDWARF::ParseDeclsForContext (CompilerDeclContext decl_ctx) 1373 { 1374 TypeSystem *type_system = decl_ctx.GetTypeSystem(); 1375 DWARFASTParser *ast_parser = type_system->GetDWARFParser(); 1376 std::vector<DWARFDIE> decl_ctx_die_list = ast_parser->GetDIEForDeclContext(decl_ctx); 1377 1378 for (DWARFDIE decl_ctx_die : decl_ctx_die_list) 1379 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl; decl = decl.GetSibling()) 1380 ast_parser->GetDeclForUIDFromDWARF(decl); 1381 } 1382 1383 CompilerDecl 1384 SymbolFileDWARF::GetDeclForUID (lldb::user_id_t type_uid) 1385 { 1386 if (UserIDMatches(type_uid)) 1387 { 1388 DWARFDebugInfo* debug_info = DebugInfo(); 1389 if (debug_info) 1390 { 1391 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid)); 1392 if (die) 1393 { 1394 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 1395 if (dwarf_ast) 1396 return dwarf_ast->GetDeclForUIDFromDWARF(die); 1397 } 1398 } 1399 } 1400 return CompilerDecl(); 1401 } 1402 1403 CompilerDeclContext 1404 SymbolFileDWARF::GetDeclContextForUID (lldb::user_id_t type_uid) 1405 { 1406 if (UserIDMatches(type_uid)) 1407 { 1408 DWARFDebugInfo* debug_info = DebugInfo(); 1409 if (debug_info) 1410 { 1411 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid)); 1412 if (die) 1413 { 1414 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 1415 if (dwarf_ast) 1416 return dwarf_ast->GetDeclContextForUIDFromDWARF(die); 1417 } 1418 } 1419 } 1420 return CompilerDeclContext(); 1421 } 1422 1423 CompilerDeclContext 1424 SymbolFileDWARF::GetDeclContextContainingUID (lldb::user_id_t type_uid) 1425 { 1426 if (UserIDMatches(type_uid)) 1427 { 1428 DWARFDebugInfo* debug_info = DebugInfo(); 1429 if (debug_info) 1430 { 1431 DWARFDIE die = debug_info->GetDIE(DIERef(type_uid)); 1432 if (die) 1433 { 1434 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 1435 if (dwarf_ast) 1436 return dwarf_ast->GetDeclContextContainingUIDFromDWARF(die); 1437 } 1438 } 1439 } 1440 return CompilerDeclContext(); 1441 } 1442 1443 1444 Type* 1445 SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid) 1446 { 1447 if (UserIDMatches(type_uid)) 1448 { 1449 DWARFDebugInfo* debug_info = DebugInfo(); 1450 if (debug_info) 1451 { 1452 DWARFDIE type_die = debug_info->GetDIE (DIERef(type_uid)); 1453 if (type_die) 1454 { 1455 const bool assert_not_being_parsed = true; 1456 return ResolveTypeUID (type_die, assert_not_being_parsed); 1457 } 1458 } 1459 } 1460 return NULL; 1461 } 1462 1463 Type* 1464 SymbolFileDWARF::ResolveTypeUID (const DWARFDIE &die, bool assert_not_being_parsed) 1465 { 1466 if (die) 1467 { 1468 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 1469 if (log) 1470 GetObjectFile()->GetModule()->LogMessage (log, 1471 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'", 1472 die.GetOffset(), 1473 die.GetTagAsCString(), 1474 die.GetName()); 1475 1476 // We might be coming in in the middle of a type tree (a class 1477 // withing a class, an enum within a class), so parse any needed 1478 // parent DIEs before we get to this one... 1479 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE (die); 1480 if (decl_ctx_die) 1481 { 1482 if (log) 1483 { 1484 switch (decl_ctx_die.Tag()) 1485 { 1486 case DW_TAG_structure_type: 1487 case DW_TAG_union_type: 1488 case DW_TAG_class_type: 1489 { 1490 // Get the type, which could be a forward declaration 1491 if (log) 1492 GetObjectFile()->GetModule()->LogMessage (log, 1493 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x", 1494 die.GetOffset(), 1495 die.GetTagAsCString(), 1496 die.GetName(), 1497 decl_ctx_die.GetOffset()); 1498 } 1499 break; 1500 1501 default: 1502 break; 1503 } 1504 } 1505 } 1506 return ResolveType (die); 1507 } 1508 return NULL; 1509 } 1510 1511 // This function is used when SymbolFileDWARFDebugMap owns a bunch of 1512 // SymbolFileDWARF objects to detect if this DWARF file is the one that 1513 // can resolve a compiler_type. 1514 bool 1515 SymbolFileDWARF::HasForwardDeclForClangType (const CompilerType &compiler_type) 1516 { 1517 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type); 1518 if (GetForwardDeclClangTypeToDie().count (compiler_type_no_qualifiers.GetOpaqueQualType())) 1519 { 1520 return true; 1521 } 1522 TypeSystem *type_system = compiler_type.GetTypeSystem(); 1523 if (type_system) 1524 { 1525 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 1526 if (dwarf_ast) 1527 return dwarf_ast->CanCompleteType(compiler_type); 1528 } 1529 return false; 1530 } 1531 1532 1533 bool 1534 SymbolFileDWARF::CompleteType (CompilerType &compiler_type) 1535 { 1536 TypeSystem *type_system = compiler_type.GetTypeSystem(); 1537 if (type_system) 1538 { 1539 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 1540 if (dwarf_ast && dwarf_ast->CanCompleteType(compiler_type)) 1541 return dwarf_ast->CompleteType(compiler_type); 1542 } 1543 1544 // We have a struct/union/class/enum that needs to be fully resolved. 1545 CompilerType compiler_type_no_qualifiers = ClangASTContext::RemoveFastQualifiers(compiler_type); 1546 auto die_it = GetForwardDeclClangTypeToDie().find (compiler_type_no_qualifiers.GetOpaqueQualType()); 1547 if (die_it == GetForwardDeclClangTypeToDie().end()) 1548 { 1549 // We have already resolved this type... 1550 return true; 1551 } 1552 1553 DWARFDebugInfo* debug_info = DebugInfo(); 1554 DWARFDIE dwarf_die = debug_info->GetDIE(die_it->getSecond()); 1555 1556 assert(UserIDMatches(die_it->getSecond().GetUID()) && "CompleteType called on the wrong SymbolFile"); 1557 1558 // Once we start resolving this type, remove it from the forward declaration 1559 // map in case anyone child members or other types require this type to get resolved. 1560 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition 1561 // are done. 1562 GetForwardDeclClangTypeToDie().erase (die_it); 1563 1564 Type *type = GetDIEToType().lookup (dwarf_die.GetDIE()); 1565 1566 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION)); 1567 if (log) 1568 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log, 1569 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...", 1570 dwarf_die.GetID(), 1571 dwarf_die.GetTagAsCString(), 1572 type->GetName().AsCString()); 1573 assert (compiler_type); 1574 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser(); 1575 if (dwarf_ast) 1576 return dwarf_ast->CompleteTypeFromDWARF (dwarf_die, type, compiler_type); 1577 return false; 1578 } 1579 1580 Type* 1581 SymbolFileDWARF::ResolveType (const DWARFDIE &die, bool assert_not_being_parsed, bool resolve_function_context) 1582 { 1583 if (die) 1584 { 1585 Type *type = GetDIEToType().lookup (die.GetDIE()); 1586 1587 if (type == NULL) 1588 type = GetTypeForDIE (die, resolve_function_context).get(); 1589 1590 if (assert_not_being_parsed) 1591 { 1592 if (type != DIE_IS_BEING_PARSED) 1593 return type; 1594 1595 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s", 1596 die.GetOffset(), 1597 die.GetTagAsCString(), 1598 die.GetName()); 1599 1600 } 1601 else 1602 return type; 1603 } 1604 return nullptr; 1605 } 1606 1607 CompileUnit* 1608 SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx) 1609 { 1610 // Check if the symbol vendor already knows about this compile unit? 1611 if (dwarf_cu->GetUserData() == NULL) 1612 { 1613 // The symbol vendor doesn't know about this compile unit, we 1614 // need to parse and add it to the symbol vendor object. 1615 return ParseCompileUnit(dwarf_cu, cu_idx).get(); 1616 } 1617 return (CompileUnit*)dwarf_cu->GetUserData(); 1618 } 1619 1620 size_t 1621 SymbolFileDWARF::GetObjCMethodDIEOffsets (ConstString class_name, DIEArray &method_die_offsets) 1622 { 1623 method_die_offsets.clear(); 1624 if (m_using_apple_tables) 1625 { 1626 if (m_apple_objc_ap.get()) 1627 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets); 1628 } 1629 else 1630 { 1631 if (!m_indexed) 1632 Index (); 1633 1634 m_objc_class_selectors_index.Find (class_name, method_die_offsets); 1635 } 1636 return method_die_offsets.size(); 1637 } 1638 1639 bool 1640 SymbolFileDWARF::GetFunction (const DWARFDIE &die, SymbolContext& sc) 1641 { 1642 sc.Clear(false); 1643 1644 if (die) 1645 { 1646 // Check if the symbol vendor already knows about this compile unit? 1647 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX); 1648 1649 sc.function = sc.comp_unit->FindFunctionByUID (die.GetID()).get(); 1650 if (sc.function == NULL) 1651 sc.function = ParseCompileUnitFunction(sc, die); 1652 1653 if (sc.function) 1654 { 1655 sc.module_sp = sc.function->CalculateSymbolContextModule(); 1656 return true; 1657 } 1658 } 1659 1660 return false; 1661 } 1662 1663 lldb::ModuleSP 1664 SymbolFileDWARF::GetDWOModule (ConstString name) 1665 { 1666 UpdateExternalModuleListIfNeeded(); 1667 const auto &pos = m_external_type_modules.find(name); 1668 if (pos != m_external_type_modules.end()) 1669 return pos->second; 1670 else 1671 return lldb::ModuleSP(); 1672 } 1673 1674 void 1675 SymbolFileDWARF::UpdateExternalModuleListIfNeeded() 1676 { 1677 if (m_fetched_external_modules) 1678 return; 1679 m_fetched_external_modules = true; 1680 1681 DWARFDebugInfo * debug_info = DebugInfo(); 1682 1683 const uint32_t num_compile_units = GetNumCompileUnits(); 1684 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 1685 { 1686 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 1687 1688 const DWARFDIE die = dwarf_cu->GetCompileUnitDIEOnly(); 1689 if (die && die.HasChildren() == false) 1690 { 1691 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr); 1692 1693 if (name) 1694 { 1695 ConstString const_name(name); 1696 if (m_external_type_modules.find(const_name) == m_external_type_modules.end()) 1697 { 1698 ModuleSP module_sp; 1699 const char *dwo_path = die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr); 1700 if (dwo_path) 1701 { 1702 ModuleSpec dwo_module_spec; 1703 dwo_module_spec.GetFileSpec().SetFile(dwo_path, false); 1704 dwo_module_spec.GetArchitecture() = m_obj_file->GetModule()->GetArchitecture(); 1705 //printf ("Loading dwo = '%s'\n", dwo_path); 1706 Error error = ModuleList::GetSharedModule (dwo_module_spec, module_sp, NULL, NULL, NULL); 1707 } 1708 m_external_type_modules[const_name] = module_sp; 1709 } 1710 } 1711 } 1712 } 1713 } 1714 1715 SymbolFileDWARF::GlobalVariableMap & 1716 SymbolFileDWARF::GetGlobalAranges() 1717 { 1718 if (!m_global_aranges_ap) 1719 { 1720 m_global_aranges_ap.reset (new GlobalVariableMap()); 1721 1722 ModuleSP module_sp = GetObjectFile()->GetModule(); 1723 if (module_sp) 1724 { 1725 const size_t num_cus = module_sp->GetNumCompileUnits(); 1726 for (size_t i = 0; i < num_cus; ++i) 1727 { 1728 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i); 1729 if (cu_sp) 1730 { 1731 VariableListSP globals_sp = cu_sp->GetVariableList(true); 1732 if (globals_sp) 1733 { 1734 const size_t num_globals = globals_sp->GetSize(); 1735 for (size_t g = 0; g < num_globals; ++g) 1736 { 1737 VariableSP var_sp = globals_sp->GetVariableAtIndex(g); 1738 if (var_sp && !var_sp->GetLocationIsConstantValueData()) 1739 { 1740 const DWARFExpression &location = var_sp->LocationExpression(); 1741 Value location_result; 1742 Error error; 1743 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error)) 1744 { 1745 if (location_result.GetValueType() == Value::eValueTypeFileAddress) 1746 { 1747 lldb::addr_t file_addr = location_result.GetScalar().ULongLong(); 1748 lldb::addr_t byte_size = 1; 1749 if (var_sp->GetType()) 1750 byte_size = var_sp->GetType()->GetByteSize(); 1751 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get())); 1752 } 1753 } 1754 } 1755 } 1756 } 1757 } 1758 } 1759 } 1760 m_global_aranges_ap->Sort(); 1761 } 1762 return *m_global_aranges_ap; 1763 } 1764 1765 1766 uint32_t 1767 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 1768 { 1769 Timer scoped_timer(__PRETTY_FUNCTION__, 1770 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)", 1771 static_cast<void*>(so_addr.GetSection().get()), 1772 so_addr.GetOffset(), resolve_scope); 1773 uint32_t resolved = 0; 1774 if (resolve_scope & ( eSymbolContextCompUnit | 1775 eSymbolContextFunction | 1776 eSymbolContextBlock | 1777 eSymbolContextLineEntry | 1778 eSymbolContextVariable )) 1779 { 1780 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1781 1782 DWARFDebugInfo* debug_info = DebugInfo(); 1783 if (debug_info) 1784 { 1785 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr); 1786 if (cu_offset == DW_INVALID_OFFSET) 1787 { 1788 // Global variables are not in the compile unit address ranges. The only way to 1789 // currently find global variables is to iterate over the .debug_pubnames or the 1790 // __apple_names table and find all items in there that point to DW_TAG_variable 1791 // DIEs and then find the address that matches. 1792 if (resolve_scope & eSymbolContextVariable) 1793 { 1794 GlobalVariableMap &map = GetGlobalAranges(); 1795 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr); 1796 if (entry && entry->data) 1797 { 1798 Variable *variable = entry->data; 1799 SymbolContextScope *scc = variable->GetSymbolContextScope(); 1800 if (scc) 1801 { 1802 scc->CalculateSymbolContext(&sc); 1803 sc.variable = variable; 1804 } 1805 return sc.GetResolvedMask(); 1806 } 1807 } 1808 } 1809 else 1810 { 1811 uint32_t cu_idx = DW_INVALID_INDEX; 1812 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx); 1813 if (dwarf_cu) 1814 { 1815 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 1816 if (sc.comp_unit) 1817 { 1818 resolved |= eSymbolContextCompUnit; 1819 1820 bool force_check_line_table = false; 1821 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 1822 { 1823 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr); 1824 DWARFDIE block_die; 1825 if (function_die) 1826 { 1827 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get(); 1828 if (sc.function == NULL) 1829 sc.function = ParseCompileUnitFunction(sc, function_die); 1830 1831 if (sc.function && (resolve_scope & eSymbolContextBlock)) 1832 block_die = function_die.LookupDeepestBlock(file_vm_addr); 1833 } 1834 else 1835 { 1836 // We might have had a compile unit that had discontiguous 1837 // address ranges where the gaps are symbols that don't have 1838 // any debug info. Discontiguous compile unit address ranges 1839 // should only happen when there aren't other functions from 1840 // other compile units in these gaps. This helps keep the size 1841 // of the aranges down. 1842 force_check_line_table = true; 1843 } 1844 1845 if (sc.function != NULL) 1846 { 1847 resolved |= eSymbolContextFunction; 1848 1849 if (resolve_scope & eSymbolContextBlock) 1850 { 1851 Block& block = sc.function->GetBlock (true); 1852 1853 if (block_die) 1854 sc.block = block.FindBlockByID (block_die.GetID()); 1855 else 1856 sc.block = block.FindBlockByID (function_die.GetID()); 1857 if (sc.block) 1858 resolved |= eSymbolContextBlock; 1859 } 1860 } 1861 } 1862 1863 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table) 1864 { 1865 LineTable *line_table = sc.comp_unit->GetLineTable(); 1866 if (line_table != NULL) 1867 { 1868 // And address that makes it into this function should be in terms 1869 // of this debug file if there is no debug map, or it will be an 1870 // address in the .o file which needs to be fixed up to be in terms 1871 // of the debug map executable. Either way, calling FixupAddress() 1872 // will work for us. 1873 Address exe_so_addr (so_addr); 1874 if (FixupAddress(exe_so_addr)) 1875 { 1876 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry)) 1877 { 1878 resolved |= eSymbolContextLineEntry; 1879 } 1880 } 1881 } 1882 } 1883 1884 if (force_check_line_table && !(resolved & eSymbolContextLineEntry)) 1885 { 1886 // We might have had a compile unit that had discontiguous 1887 // address ranges where the gaps are symbols that don't have 1888 // any debug info. Discontiguous compile unit address ranges 1889 // should only happen when there aren't other functions from 1890 // other compile units in these gaps. This helps keep the size 1891 // of the aranges down. 1892 sc.comp_unit = NULL; 1893 resolved &= ~eSymbolContextCompUnit; 1894 } 1895 } 1896 else 1897 { 1898 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.", 1899 cu_offset, 1900 cu_idx); 1901 } 1902 } 1903 } 1904 } 1905 } 1906 return resolved; 1907 } 1908 1909 1910 1911 uint32_t 1912 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 1913 { 1914 const uint32_t prev_size = sc_list.GetSize(); 1915 if (resolve_scope & eSymbolContextCompUnit) 1916 { 1917 DWARFDebugInfo* debug_info = DebugInfo(); 1918 if (debug_info) 1919 { 1920 uint32_t cu_idx; 1921 DWARFCompileUnit* dwarf_cu = NULL; 1922 1923 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx) 1924 { 1925 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 1926 const bool full_match = (bool)file_spec.GetDirectory(); 1927 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match); 1928 if (check_inlines || file_spec_matches_cu_file_spec) 1929 { 1930 SymbolContext sc (m_obj_file->GetModule()); 1931 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 1932 if (sc.comp_unit) 1933 { 1934 uint32_t file_idx = UINT32_MAX; 1935 1936 // If we are looking for inline functions only and we don't 1937 // find it in the support files, we are done. 1938 if (check_inlines) 1939 { 1940 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 1941 if (file_idx == UINT32_MAX) 1942 continue; 1943 } 1944 1945 if (line != 0) 1946 { 1947 LineTable *line_table = sc.comp_unit->GetLineTable(); 1948 1949 if (line_table != NULL && line != 0) 1950 { 1951 // We will have already looked up the file index if 1952 // we are searching for inline entries. 1953 if (!check_inlines) 1954 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 1955 1956 if (file_idx != UINT32_MAX) 1957 { 1958 uint32_t found_line; 1959 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry); 1960 found_line = sc.line_entry.line; 1961 1962 while (line_idx != UINT32_MAX) 1963 { 1964 sc.function = NULL; 1965 sc.block = NULL; 1966 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 1967 { 1968 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress(); 1969 if (file_vm_addr != LLDB_INVALID_ADDRESS) 1970 { 1971 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr); 1972 DWARFDIE block_die; 1973 if (function_die) 1974 { 1975 sc.function = sc.comp_unit->FindFunctionByUID (function_die.GetID()).get(); 1976 if (sc.function == NULL) 1977 sc.function = ParseCompileUnitFunction(sc, function_die); 1978 1979 if (sc.function && (resolve_scope & eSymbolContextBlock)) 1980 block_die = function_die.LookupDeepestBlock(file_vm_addr); 1981 } 1982 1983 if (sc.function != NULL) 1984 { 1985 Block& block = sc.function->GetBlock (true); 1986 1987 if (block_die) 1988 sc.block = block.FindBlockByID (block_die.GetID()); 1989 else if (function_die) 1990 sc.block = block.FindBlockByID (function_die.GetID()); 1991 } 1992 } 1993 } 1994 1995 sc_list.Append(sc); 1996 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry); 1997 } 1998 } 1999 } 2000 else if (file_spec_matches_cu_file_spec && !check_inlines) 2001 { 2002 // only append the context if we aren't looking for inline call sites 2003 // by file and line and if the file spec matches that of the compile unit 2004 sc_list.Append(sc); 2005 } 2006 } 2007 else if (file_spec_matches_cu_file_spec && !check_inlines) 2008 { 2009 // only append the context if we aren't looking for inline call sites 2010 // by file and line and if the file spec matches that of the compile unit 2011 sc_list.Append(sc); 2012 } 2013 2014 if (!check_inlines) 2015 break; 2016 } 2017 } 2018 } 2019 } 2020 } 2021 return sc_list.GetSize() - prev_size; 2022 } 2023 2024 void 2025 SymbolFileDWARF::Index () 2026 { 2027 if (m_indexed) 2028 return; 2029 m_indexed = true; 2030 Timer scoped_timer (__PRETTY_FUNCTION__, 2031 "SymbolFileDWARF::Index (%s)", 2032 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>")); 2033 2034 DWARFDebugInfo* debug_info = DebugInfo(); 2035 if (debug_info) 2036 { 2037 const uint32_t num_compile_units = GetNumCompileUnits(); 2038 std::vector<NameToDIE> function_basename_index(num_compile_units); 2039 std::vector<NameToDIE> function_fullname_index(num_compile_units); 2040 std::vector<NameToDIE> function_method_index(num_compile_units); 2041 std::vector<NameToDIE> function_selector_index(num_compile_units); 2042 std::vector<NameToDIE> objc_class_selectors_index(num_compile_units); 2043 std::vector<NameToDIE> global_index(num_compile_units); 2044 std::vector<NameToDIE> type_index(num_compile_units); 2045 std::vector<NameToDIE> namespace_index(num_compile_units); 2046 2047 auto parser_fn = [this, 2048 debug_info, 2049 &function_basename_index, 2050 &function_fullname_index, 2051 &function_method_index, 2052 &function_selector_index, 2053 &objc_class_selectors_index, 2054 &global_index, 2055 &type_index, 2056 &namespace_index](uint32_t cu_idx) 2057 { 2058 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 2059 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded(false) > 1; 2060 2061 dwarf_cu->Index(function_basename_index[cu_idx], 2062 function_fullname_index[cu_idx], 2063 function_method_index[cu_idx], 2064 function_selector_index[cu_idx], 2065 objc_class_selectors_index[cu_idx], 2066 global_index[cu_idx], 2067 type_index[cu_idx], 2068 namespace_index[cu_idx]); 2069 2070 // Keep memory down by clearing DIEs if this generate function 2071 // caused them to be parsed 2072 if (clear_dies) 2073 dwarf_cu->ClearDIEs(true); 2074 2075 return cu_idx; 2076 }; 2077 2078 TaskRunner<uint32_t> task_runner; 2079 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 2080 task_runner.AddTask(parser_fn, cu_idx); 2081 2082 while (true) 2083 { 2084 std::future<uint32_t> f = task_runner.WaitForNextCompletedTask(); 2085 if (!f.valid()) 2086 break; 2087 uint32_t cu_idx = f.get(); 2088 2089 m_function_basename_index.Append(function_basename_index[cu_idx]); 2090 m_function_fullname_index.Append(function_fullname_index[cu_idx]); 2091 m_function_method_index.Append(function_method_index[cu_idx]); 2092 m_function_selector_index.Append(function_selector_index[cu_idx]); 2093 m_objc_class_selectors_index.Append(objc_class_selectors_index[cu_idx]); 2094 m_global_index.Append(global_index[cu_idx]); 2095 m_type_index.Append(type_index[cu_idx]); 2096 m_namespace_index.Append(namespace_index[cu_idx]); 2097 } 2098 2099 TaskPool::RunTasks( 2100 [&]() { m_function_basename_index.Finalize(); }, 2101 [&]() { m_function_fullname_index.Finalize(); }, 2102 [&]() { m_function_method_index.Finalize(); }, 2103 [&]() { m_function_selector_index.Finalize(); }, 2104 [&]() { m_objc_class_selectors_index.Finalize(); }, 2105 [&]() { m_global_index.Finalize(); }, 2106 [&]() { m_type_index.Finalize(); }, 2107 [&]() { m_namespace_index.Finalize(); }); 2108 2109 #if defined (ENABLE_DEBUG_PRINTF) 2110 StreamFile s(stdout, false); 2111 s.Printf ("DWARF index for '%s':", 2112 GetObjectFile()->GetFileSpec().GetPath().c_str()); 2113 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 2114 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 2115 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 2116 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 2117 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 2118 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 2119 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 2120 s.Printf("\nNamespaces:\n") m_namespace_index.Dump (&s); 2121 #endif 2122 } 2123 } 2124 2125 bool 2126 SymbolFileDWARF::DeclContextMatchesThisSymbolFile (const lldb_private::CompilerDeclContext *decl_ctx) 2127 { 2128 if (decl_ctx == nullptr || !decl_ctx->IsValid()) 2129 { 2130 // Invalid namespace decl which means we aren't matching only things 2131 // in this symbol file, so return true to indicate it matches this 2132 // symbol file. 2133 return true; 2134 } 2135 2136 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 2137 TypeSystem *type_system = GetTypeSystemForLanguage(decl_ctx_type_system->GetMinimumLanguage(nullptr)); 2138 if (decl_ctx_type_system == type_system) 2139 return true; // The type systems match, return true 2140 2141 // The namespace AST was valid, and it does not match... 2142 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2143 2144 if (log) 2145 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file"); 2146 2147 return false; 2148 } 2149 2150 uint32_t 2151 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, VariableList& variables) 2152 { 2153 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2154 2155 if (log) 2156 GetObjectFile()->GetModule()->LogMessage (log, 2157 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables)", 2158 name.GetCString(), 2159 static_cast<const void*>(parent_decl_ctx), 2160 append, max_matches); 2161 2162 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2163 return 0; 2164 2165 DWARFDebugInfo* info = DebugInfo(); 2166 if (info == NULL) 2167 return 0; 2168 2169 // If we aren't appending the results to this list, then clear the list 2170 if (!append) 2171 variables.Clear(); 2172 2173 // Remember how many variables are in the list before we search in case 2174 // we are appending the results to a variable list. 2175 const uint32_t original_size = variables.GetSize(); 2176 2177 DIEArray die_offsets; 2178 2179 if (m_using_apple_tables) 2180 { 2181 if (m_apple_names_ap.get()) 2182 { 2183 const char *name_cstr = name.GetCString(); 2184 llvm::StringRef basename; 2185 llvm::StringRef context; 2186 2187 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name_cstr, context, basename)) 2188 basename = name_cstr; 2189 2190 m_apple_names_ap->FindByName (basename.data(), die_offsets); 2191 } 2192 } 2193 else 2194 { 2195 // Index the DWARF if we haven't already 2196 if (!m_indexed) 2197 Index (); 2198 2199 m_global_index.Find (name, die_offsets); 2200 } 2201 2202 const size_t num_die_matches = die_offsets.size(); 2203 if (num_die_matches) 2204 { 2205 SymbolContext sc; 2206 sc.module_sp = m_obj_file->GetModule(); 2207 assert (sc.module_sp); 2208 2209 DWARFDebugInfo* debug_info = DebugInfo(); 2210 bool done = false; 2211 for (size_t i=0; i<num_die_matches && !done; ++i) 2212 { 2213 const DIERef& die_ref = die_offsets[i]; 2214 DWARFDIE die = debug_info->GetDIE (die_ref); 2215 2216 if (die) 2217 { 2218 switch (die.Tag()) 2219 { 2220 default: 2221 case DW_TAG_subprogram: 2222 case DW_TAG_inlined_subroutine: 2223 case DW_TAG_try_block: 2224 case DW_TAG_catch_block: 2225 break; 2226 2227 case DW_TAG_variable: 2228 { 2229 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX); 2230 2231 if (parent_decl_ctx) 2232 { 2233 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 2234 if (dwarf_ast) 2235 { 2236 CompilerDeclContext actual_parent_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die); 2237 if (!actual_parent_decl_ctx || actual_parent_decl_ctx != *parent_decl_ctx) 2238 continue; 2239 } 2240 } 2241 2242 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables); 2243 2244 if (variables.GetSize() - original_size >= max_matches) 2245 done = true; 2246 } 2247 break; 2248 } 2249 } 2250 else 2251 { 2252 if (m_using_apple_tables) 2253 { 2254 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n", 2255 die_ref.die_offset, name.GetCString()); 2256 } 2257 } 2258 } 2259 } 2260 2261 // Return the number of variable that were appended to the list 2262 const uint32_t num_matches = variables.GetSize() - original_size; 2263 if (log && num_matches > 0) 2264 { 2265 GetObjectFile()->GetModule()->LogMessage (log, 2266 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", parent_decl_ctx=%p, append=%u, max_matches=%u, variables) => %u", 2267 name.GetCString(), 2268 static_cast<const void*>(parent_decl_ctx), 2269 append, max_matches, 2270 num_matches); 2271 } 2272 return num_matches; 2273 } 2274 2275 uint32_t 2276 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 2277 { 2278 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2279 2280 if (log) 2281 { 2282 GetObjectFile()->GetModule()->LogMessage (log, 2283 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)", 2284 regex.GetText(), append, 2285 max_matches); 2286 } 2287 2288 DWARFDebugInfo* info = DebugInfo(); 2289 if (info == NULL) 2290 return 0; 2291 2292 // If we aren't appending the results to this list, then clear the list 2293 if (!append) 2294 variables.Clear(); 2295 2296 // Remember how many variables are in the list before we search in case 2297 // we are appending the results to a variable list. 2298 const uint32_t original_size = variables.GetSize(); 2299 2300 DIEArray die_offsets; 2301 2302 if (m_using_apple_tables) 2303 { 2304 if (m_apple_names_ap.get()) 2305 { 2306 DWARFMappedHash::DIEInfoArray hash_data_array; 2307 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array)) 2308 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 2309 } 2310 } 2311 else 2312 { 2313 // Index the DWARF if we haven't already 2314 if (!m_indexed) 2315 Index (); 2316 2317 m_global_index.Find (regex, die_offsets); 2318 } 2319 2320 SymbolContext sc; 2321 sc.module_sp = m_obj_file->GetModule(); 2322 assert (sc.module_sp); 2323 2324 const size_t num_matches = die_offsets.size(); 2325 if (num_matches) 2326 { 2327 DWARFDebugInfo* debug_info = DebugInfo(); 2328 for (size_t i=0; i<num_matches; ++i) 2329 { 2330 const DIERef& die_ref = die_offsets[i]; 2331 DWARFDIE die = debug_info->GetDIE (die_ref); 2332 2333 if (die) 2334 { 2335 sc.comp_unit = GetCompUnitForDWARFCompUnit(die.GetCU(), UINT32_MAX); 2336 2337 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables); 2338 2339 if (variables.GetSize() - original_size >= max_matches) 2340 break; 2341 } 2342 else 2343 { 2344 if (m_using_apple_tables) 2345 { 2346 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n", 2347 die_ref.die_offset, regex.GetText()); 2348 } 2349 } 2350 } 2351 } 2352 2353 // Return the number of variable that were appended to the list 2354 return variables.GetSize() - original_size; 2355 } 2356 2357 2358 bool 2359 SymbolFileDWARF::ResolveFunction (const DIERef& die_ref, 2360 bool include_inlines, 2361 SymbolContextList& sc_list) 2362 { 2363 DWARFDIE die = DebugInfo()->GetDIE (die_ref); 2364 return ResolveFunction (die, include_inlines, sc_list); 2365 } 2366 2367 2368 bool 2369 SymbolFileDWARF::ResolveFunction (const DWARFDIE &orig_die, 2370 bool include_inlines, 2371 SymbolContextList& sc_list) 2372 { 2373 SymbolContext sc; 2374 2375 if (!orig_die) 2376 return false; 2377 2378 // If we were passed a die that is not a function, just return false... 2379 if (!(orig_die.Tag() == DW_TAG_subprogram || (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine))) 2380 return false; 2381 2382 DWARFDIE die = orig_die; 2383 DWARFDIE inlined_die; 2384 if (die.Tag() == DW_TAG_inlined_subroutine) 2385 { 2386 inlined_die = die; 2387 2388 while (1) 2389 { 2390 die = die.GetParent(); 2391 2392 if (die) 2393 { 2394 if (die.Tag() == DW_TAG_subprogram) 2395 break; 2396 } 2397 else 2398 break; 2399 } 2400 } 2401 assert (die && die.Tag() == DW_TAG_subprogram); 2402 if (GetFunction (die, sc)) 2403 { 2404 Address addr; 2405 // Parse all blocks if needed 2406 if (inlined_die) 2407 { 2408 Block &function_block = sc.function->GetBlock (true); 2409 sc.block = function_block.FindBlockByID (inlined_die.GetID()); 2410 if (sc.block == NULL) 2411 sc.block = function_block.FindBlockByID (inlined_die.GetOffset()); 2412 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false) 2413 addr.Clear(); 2414 } 2415 else 2416 { 2417 sc.block = NULL; 2418 addr = sc.function->GetAddressRange().GetBaseAddress(); 2419 } 2420 2421 if (addr.IsValid()) 2422 { 2423 sc_list.Append(sc); 2424 return true; 2425 } 2426 } 2427 2428 return false; 2429 } 2430 2431 void 2432 SymbolFileDWARF::FindFunctions (const ConstString &name, 2433 const NameToDIE &name_to_die, 2434 bool include_inlines, 2435 SymbolContextList& sc_list) 2436 { 2437 DIEArray die_offsets; 2438 if (name_to_die.Find (name, die_offsets)) 2439 { 2440 ParseFunctions (die_offsets, include_inlines, sc_list); 2441 } 2442 } 2443 2444 2445 void 2446 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 2447 const NameToDIE &name_to_die, 2448 bool include_inlines, 2449 SymbolContextList& sc_list) 2450 { 2451 DIEArray die_offsets; 2452 if (name_to_die.Find (regex, die_offsets)) 2453 { 2454 ParseFunctions (die_offsets, include_inlines, sc_list); 2455 } 2456 } 2457 2458 2459 void 2460 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 2461 const DWARFMappedHash::MemoryTable &memory_table, 2462 bool include_inlines, 2463 SymbolContextList& sc_list) 2464 { 2465 DIEArray die_offsets; 2466 DWARFMappedHash::DIEInfoArray hash_data_array; 2467 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array)) 2468 { 2469 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 2470 ParseFunctions (die_offsets, include_inlines, sc_list); 2471 } 2472 } 2473 2474 void 2475 SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets, 2476 bool include_inlines, 2477 SymbolContextList& sc_list) 2478 { 2479 const size_t num_matches = die_offsets.size(); 2480 if (num_matches) 2481 { 2482 for (size_t i=0; i<num_matches; ++i) 2483 ResolveFunction (die_offsets[i], include_inlines, sc_list); 2484 } 2485 } 2486 2487 bool 2488 SymbolFileDWARF::DIEInDeclContext (const CompilerDeclContext *decl_ctx, 2489 const DWARFDIE &die) 2490 { 2491 // If we have no parent decl context to match this DIE matches, and if the parent 2492 // decl context isn't valid, we aren't trying to look for any particular decl 2493 // context so any die matches. 2494 if (decl_ctx == nullptr || !decl_ctx->IsValid()) 2495 return true; 2496 2497 if (die) 2498 { 2499 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 2500 if (dwarf_ast) 2501 { 2502 CompilerDeclContext actual_decl_ctx = dwarf_ast->GetDeclContextContainingUIDFromDWARF (die); 2503 if (actual_decl_ctx) 2504 return actual_decl_ctx == *decl_ctx; 2505 } 2506 } 2507 return false; 2508 } 2509 2510 uint32_t 2511 SymbolFileDWARF::FindFunctions (const ConstString &name, 2512 const CompilerDeclContext *parent_decl_ctx, 2513 uint32_t name_type_mask, 2514 bool include_inlines, 2515 bool append, 2516 SymbolContextList& sc_list) 2517 { 2518 Timer scoped_timer (__PRETTY_FUNCTION__, 2519 "SymbolFileDWARF::FindFunctions (name = '%s')", 2520 name.AsCString()); 2521 2522 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup() 2523 assert ((name_type_mask & eFunctionNameTypeAuto) == 0); 2524 2525 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2526 2527 if (log) 2528 { 2529 GetObjectFile()->GetModule()->LogMessage (log, 2530 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)", 2531 name.GetCString(), 2532 name_type_mask, 2533 append); 2534 } 2535 2536 // If we aren't appending the results to this list, then clear the list 2537 if (!append) 2538 sc_list.Clear(); 2539 2540 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2541 return 0; 2542 2543 // If name is empty then we won't find anything. 2544 if (name.IsEmpty()) 2545 return 0; 2546 2547 // Remember how many sc_list are in the list before we search in case 2548 // we are appending the results to a variable list. 2549 2550 const char *name_cstr = name.GetCString(); 2551 2552 const uint32_t original_size = sc_list.GetSize(); 2553 2554 DWARFDebugInfo* info = DebugInfo(); 2555 if (info == NULL) 2556 return 0; 2557 2558 std::set<const DWARFDebugInfoEntry *> resolved_dies; 2559 if (m_using_apple_tables) 2560 { 2561 if (m_apple_names_ap.get()) 2562 { 2563 2564 DIEArray die_offsets; 2565 2566 uint32_t num_matches = 0; 2567 2568 if (name_type_mask & eFunctionNameTypeFull) 2569 { 2570 // If they asked for the full name, match what they typed. At some point we may 2571 // want to canonicalize this (strip double spaces, etc. For now, we just add all the 2572 // dies that we find by exact match. 2573 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 2574 for (uint32_t i = 0; i < num_matches; i++) 2575 { 2576 const DIERef& die_ref = die_offsets[i]; 2577 DWARFDIE die = info->GetDIE (die_ref); 2578 if (die) 2579 { 2580 if (!DIEInDeclContext(parent_decl_ctx, die)) 2581 continue; // The containing decl contexts don't match 2582 2583 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) 2584 { 2585 if (ResolveFunction (die, include_inlines, sc_list)) 2586 resolved_dies.insert(die.GetDIE()); 2587 } 2588 } 2589 else 2590 { 2591 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 2592 die_ref.die_offset, name_cstr); 2593 } 2594 } 2595 } 2596 2597 if (name_type_mask & eFunctionNameTypeSelector) 2598 { 2599 if (parent_decl_ctx && parent_decl_ctx->IsValid()) 2600 return 0; // no selectors in namespaces 2601 2602 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 2603 // Now make sure these are actually ObjC methods. In this case we can simply look up the name, 2604 // and if it is an ObjC method name, we're good. 2605 2606 for (uint32_t i = 0; i < num_matches; i++) 2607 { 2608 const DIERef& die_ref = die_offsets[i]; 2609 DWARFDIE die = info->GetDIE (die_ref); 2610 if (die) 2611 { 2612 const char *die_name = die.GetName(); 2613 if (ObjCLanguage::IsPossibleObjCMethodName(die_name)) 2614 { 2615 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) 2616 { 2617 if (ResolveFunction (die, include_inlines, sc_list)) 2618 resolved_dies.insert(die.GetDIE()); 2619 } 2620 } 2621 } 2622 else 2623 { 2624 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 2625 die_ref.die_offset, name_cstr); 2626 } 2627 } 2628 die_offsets.clear(); 2629 } 2630 2631 if (((name_type_mask & eFunctionNameTypeMethod) && !parent_decl_ctx) || name_type_mask & eFunctionNameTypeBase) 2632 { 2633 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to 2634 // extract the base name, look that up, and if there is any other information in the name we were 2635 // passed in we have to post-filter based on that. 2636 2637 // FIXME: Arrange the logic above so that we don't calculate the base name twice: 2638 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 2639 2640 for (uint32_t i = 0; i < num_matches; i++) 2641 { 2642 const DIERef& die_ref = die_offsets[i]; 2643 DWARFDIE die = info->GetDIE (die_ref); 2644 if (die) 2645 { 2646 if (!DIEInDeclContext(parent_decl_ctx, die)) 2647 continue; // The containing decl contexts don't match 2648 2649 2650 // If we get to here, the die is good, and we should add it: 2651 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end() && ResolveFunction (die, include_inlines, sc_list)) 2652 { 2653 bool keep_die = true; 2654 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod)) 2655 { 2656 // We are looking for either basenames or methods, so we need to 2657 // trim out the ones we won't want by looking at the type 2658 SymbolContext sc; 2659 if (sc_list.GetLastContext(sc)) 2660 { 2661 if (sc.block) 2662 { 2663 // We have an inlined function 2664 } 2665 else if (sc.function) 2666 { 2667 Type *type = sc.function->GetType(); 2668 2669 if (type) 2670 { 2671 CompilerDeclContext decl_ctx = GetDeclContextContainingUID (type->GetID()); 2672 if (decl_ctx.IsStructUnionOrClass()) 2673 { 2674 if (name_type_mask & eFunctionNameTypeBase) 2675 { 2676 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1); 2677 keep_die = false; 2678 } 2679 } 2680 else 2681 { 2682 if (name_type_mask & eFunctionNameTypeMethod) 2683 { 2684 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1); 2685 keep_die = false; 2686 } 2687 } 2688 } 2689 else 2690 { 2691 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type", 2692 die_ref.die_offset); 2693 } 2694 } 2695 } 2696 } 2697 if (keep_die) 2698 resolved_dies.insert(die.GetDIE()); 2699 } 2700 } 2701 else 2702 { 2703 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 2704 die_ref.die_offset, name_cstr); 2705 } 2706 } 2707 die_offsets.clear(); 2708 } 2709 } 2710 } 2711 else 2712 { 2713 2714 // Index the DWARF if we haven't already 2715 if (!m_indexed) 2716 Index (); 2717 2718 if (name_type_mask & eFunctionNameTypeFull) 2719 { 2720 FindFunctions (name, m_function_fullname_index, include_inlines, sc_list); 2721 2722 // FIXME Temporary workaround for global/anonymous namespace 2723 // functions debugging FreeBSD and Linux binaries. 2724 // If we didn't find any functions in the global namespace try 2725 // looking in the basename index but ignore any returned 2726 // functions that have a namespace but keep functions which 2727 // have an anonymous namespace 2728 // TODO: The arch in the object file isn't correct for MSVC 2729 // binaries on windows, we should find a way to make it 2730 // correct and handle those symbols as well. 2731 if (sc_list.GetSize() == original_size) 2732 { 2733 ArchSpec arch; 2734 if (!parent_decl_ctx && 2735 GetObjectFile()->GetArchitecture(arch) && 2736 (arch.GetTriple().isOSFreeBSD() || arch.GetTriple().isOSLinux() || 2737 arch.GetMachine() == llvm::Triple::hexagon)) 2738 { 2739 SymbolContextList temp_sc_list; 2740 FindFunctions (name, m_function_basename_index, include_inlines, temp_sc_list); 2741 SymbolContext sc; 2742 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) 2743 { 2744 if (temp_sc_list.GetContextAtIndex(i, sc)) 2745 { 2746 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled); 2747 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled); 2748 // Mangled names on Linux and FreeBSD are of the form: 2749 // _ZN18function_namespace13function_nameEv. 2750 if (strncmp(mangled_name.GetCString(), "_ZN", 3) || 2751 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21)) 2752 { 2753 sc_list.Append(sc); 2754 } 2755 } 2756 } 2757 } 2758 } 2759 } 2760 DIEArray die_offsets; 2761 if (name_type_mask & eFunctionNameTypeBase) 2762 { 2763 uint32_t num_base = m_function_basename_index.Find(name, die_offsets); 2764 for (uint32_t i = 0; i < num_base; i++) 2765 { 2766 DWARFDIE die = info->GetDIE (die_offsets[i]); 2767 if (die) 2768 { 2769 if (!DIEInDeclContext(parent_decl_ctx, die)) 2770 continue; // The containing decl contexts don't match 2771 2772 // If we get to here, the die is good, and we should add it: 2773 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) 2774 { 2775 if (ResolveFunction (die, include_inlines, sc_list)) 2776 resolved_dies.insert(die.GetDIE()); 2777 } 2778 } 2779 } 2780 die_offsets.clear(); 2781 } 2782 2783 if (name_type_mask & eFunctionNameTypeMethod) 2784 { 2785 if (parent_decl_ctx && parent_decl_ctx->IsValid()) 2786 return 0; // no methods in namespaces 2787 2788 uint32_t num_base = m_function_method_index.Find(name, die_offsets); 2789 { 2790 for (uint32_t i = 0; i < num_base; i++) 2791 { 2792 DWARFDIE die = info->GetDIE (die_offsets[i]); 2793 if (die) 2794 { 2795 // If we get to here, the die is good, and we should add it: 2796 if (resolved_dies.find(die.GetDIE()) == resolved_dies.end()) 2797 { 2798 if (ResolveFunction (die, include_inlines, sc_list)) 2799 resolved_dies.insert(die.GetDIE()); 2800 } 2801 } 2802 } 2803 } 2804 die_offsets.clear(); 2805 } 2806 2807 if ((name_type_mask & eFunctionNameTypeSelector) && (!parent_decl_ctx || !parent_decl_ctx->IsValid())) 2808 { 2809 FindFunctions (name, m_function_selector_index, include_inlines, sc_list); 2810 } 2811 2812 } 2813 2814 // Return the number of variable that were appended to the list 2815 const uint32_t num_matches = sc_list.GetSize() - original_size; 2816 2817 if (log && num_matches > 0) 2818 { 2819 GetObjectFile()->GetModule()->LogMessage (log, 2820 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => %u", 2821 name.GetCString(), 2822 name_type_mask, 2823 include_inlines, 2824 append, 2825 num_matches); 2826 } 2827 return num_matches; 2828 } 2829 2830 uint32_t 2831 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list) 2832 { 2833 Timer scoped_timer (__PRETTY_FUNCTION__, 2834 "SymbolFileDWARF::FindFunctions (regex = '%s')", 2835 regex.GetText()); 2836 2837 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2838 2839 if (log) 2840 { 2841 GetObjectFile()->GetModule()->LogMessage (log, 2842 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)", 2843 regex.GetText(), 2844 append); 2845 } 2846 2847 2848 // If we aren't appending the results to this list, then clear the list 2849 if (!append) 2850 sc_list.Clear(); 2851 2852 // Remember how many sc_list are in the list before we search in case 2853 // we are appending the results to a variable list. 2854 uint32_t original_size = sc_list.GetSize(); 2855 2856 if (m_using_apple_tables) 2857 { 2858 if (m_apple_names_ap.get()) 2859 FindFunctions (regex, *m_apple_names_ap, include_inlines, sc_list); 2860 } 2861 else 2862 { 2863 // Index the DWARF if we haven't already 2864 if (!m_indexed) 2865 Index (); 2866 2867 FindFunctions (regex, m_function_basename_index, include_inlines, sc_list); 2868 2869 FindFunctions (regex, m_function_fullname_index, include_inlines, sc_list); 2870 } 2871 2872 // Return the number of variable that were appended to the list 2873 return sc_list.GetSize() - original_size; 2874 } 2875 2876 uint32_t 2877 SymbolFileDWARF::FindTypes (const SymbolContext& sc, 2878 const ConstString &name, 2879 const CompilerDeclContext *parent_decl_ctx, 2880 bool append, 2881 uint32_t max_matches, 2882 TypeMap& types) 2883 { 2884 DWARFDebugInfo* info = DebugInfo(); 2885 if (info == NULL) 2886 return 0; 2887 2888 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2889 2890 if (log) 2891 { 2892 if (parent_decl_ctx) 2893 GetObjectFile()->GetModule()->LogMessage (log, 2894 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list)", 2895 name.GetCString(), 2896 static_cast<const void*>(parent_decl_ctx), 2897 parent_decl_ctx->GetName().AsCString("<NULL>"), 2898 append, max_matches); 2899 else 2900 GetObjectFile()->GetModule()->LogMessage (log, 2901 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list)", 2902 name.GetCString(), append, 2903 max_matches); 2904 } 2905 2906 // If we aren't appending the results to this list, then clear the list 2907 if (!append) 2908 types.Clear(); 2909 2910 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2911 return 0; 2912 2913 DIEArray die_offsets; 2914 2915 if (m_using_apple_tables) 2916 { 2917 if (m_apple_types_ap.get()) 2918 { 2919 const char *name_cstr = name.GetCString(); 2920 m_apple_types_ap->FindByName (name_cstr, die_offsets); 2921 } 2922 } 2923 else 2924 { 2925 if (!m_indexed) 2926 Index (); 2927 2928 m_type_index.Find (name, die_offsets); 2929 } 2930 2931 const size_t num_die_matches = die_offsets.size(); 2932 2933 if (num_die_matches) 2934 { 2935 const uint32_t initial_types_size = types.GetSize(); 2936 DWARFDebugInfo* debug_info = DebugInfo(); 2937 for (size_t i=0; i<num_die_matches; ++i) 2938 { 2939 const DIERef& die_ref = die_offsets[i]; 2940 DWARFDIE die = debug_info->GetDIE (die_ref); 2941 2942 if (die) 2943 { 2944 if (!DIEInDeclContext(parent_decl_ctx, die)) 2945 continue; // The containing decl contexts don't match 2946 2947 Type *matching_type = ResolveType (die, true, true); 2948 if (matching_type) 2949 { 2950 // We found a type pointer, now find the shared pointer form our type list 2951 types.InsertUnique (matching_type->shared_from_this()); 2952 if (types.GetSize() >= max_matches) 2953 break; 2954 } 2955 } 2956 else 2957 { 2958 if (m_using_apple_tables) 2959 { 2960 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 2961 die_ref.die_offset, name.GetCString()); 2962 } 2963 } 2964 2965 } 2966 const uint32_t num_matches = types.GetSize() - initial_types_size; 2967 if (log && num_matches) 2968 { 2969 if (parent_decl_ctx) 2970 { 2971 GetObjectFile()->GetModule()->LogMessage (log, 2972 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u", 2973 name.GetCString(), 2974 static_cast<const void*>(parent_decl_ctx), 2975 parent_decl_ctx->GetName().AsCString("<NULL>"), 2976 append, max_matches, 2977 num_matches); 2978 } 2979 else 2980 { 2981 GetObjectFile()->GetModule()->LogMessage (log, 2982 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = NULL, append=%u, max_matches=%u, type_list) => %u", 2983 name.GetCString(), 2984 append, max_matches, 2985 num_matches); 2986 } 2987 } 2988 return num_matches; 2989 } 2990 else 2991 { 2992 UpdateExternalModuleListIfNeeded(); 2993 2994 for (const auto &pair : m_external_type_modules) 2995 { 2996 ModuleSP external_module_sp = pair.second; 2997 if (external_module_sp) 2998 { 2999 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor(); 3000 if (sym_vendor) 3001 { 3002 const uint32_t num_external_matches = sym_vendor->FindTypes (sc, 3003 name, 3004 parent_decl_ctx, 3005 append, 3006 max_matches, 3007 types); 3008 if (num_external_matches) 3009 return num_external_matches; 3010 } 3011 } 3012 } 3013 } 3014 3015 return 0; 3016 } 3017 3018 3019 size_t 3020 SymbolFileDWARF::FindTypes (const std::vector<CompilerContext> &context, 3021 bool append, 3022 TypeMap& types) 3023 { 3024 if (!append) 3025 types.Clear(); 3026 3027 if (context.empty()) 3028 return 0; 3029 3030 DIEArray die_offsets; 3031 3032 ConstString name = context.back().name; 3033 3034 if (m_using_apple_tables) 3035 { 3036 if (m_apple_types_ap.get()) 3037 { 3038 const char *name_cstr = name.GetCString(); 3039 m_apple_types_ap->FindByName (name_cstr, die_offsets); 3040 } 3041 } 3042 else 3043 { 3044 if (!m_indexed) 3045 Index (); 3046 3047 m_type_index.Find (name, die_offsets); 3048 } 3049 3050 const size_t num_die_matches = die_offsets.size(); 3051 3052 if (num_die_matches) 3053 { 3054 size_t num_matches = 0; 3055 DWARFDebugInfo* debug_info = DebugInfo(); 3056 for (size_t i=0; i<num_die_matches; ++i) 3057 { 3058 const DIERef& die_ref = die_offsets[i]; 3059 DWARFDIE die = debug_info->GetDIE (die_ref); 3060 3061 if (die) 3062 { 3063 std::vector<CompilerContext> die_context; 3064 die.GetDWOContext(die_context); 3065 if (die_context != context) 3066 continue; 3067 3068 Type *matching_type = ResolveType (die, true, true); 3069 if (matching_type) 3070 { 3071 // We found a type pointer, now find the shared pointer form our type list 3072 types.InsertUnique (matching_type->shared_from_this()); 3073 ++num_matches; 3074 } 3075 } 3076 else 3077 { 3078 if (m_using_apple_tables) 3079 { 3080 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 3081 die_ref.die_offset, name.GetCString()); 3082 } 3083 } 3084 3085 } 3086 return num_matches; 3087 } 3088 return 0; 3089 } 3090 3091 3092 CompilerDeclContext 3093 SymbolFileDWARF::FindNamespace (const SymbolContext& sc, 3094 const ConstString &name, 3095 const CompilerDeclContext *parent_decl_ctx) 3096 { 3097 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3098 3099 if (log) 3100 { 3101 GetObjectFile()->GetModule()->LogMessage (log, 3102 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")", 3103 name.GetCString()); 3104 } 3105 3106 CompilerDeclContext namespace_decl_ctx; 3107 3108 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 3109 return namespace_decl_ctx; 3110 3111 3112 DWARFDebugInfo* info = DebugInfo(); 3113 if (info) 3114 { 3115 DIEArray die_offsets; 3116 3117 // Index if we already haven't to make sure the compile units 3118 // get indexed and make their global DIE index list 3119 if (m_using_apple_tables) 3120 { 3121 if (m_apple_namespaces_ap.get()) 3122 { 3123 const char *name_cstr = name.GetCString(); 3124 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets); 3125 } 3126 } 3127 else 3128 { 3129 if (!m_indexed) 3130 Index (); 3131 3132 m_namespace_index.Find (name, die_offsets); 3133 } 3134 3135 const size_t num_matches = die_offsets.size(); 3136 if (num_matches) 3137 { 3138 DWARFDebugInfo* debug_info = DebugInfo(); 3139 for (size_t i=0; i<num_matches; ++i) 3140 { 3141 const DIERef& die_ref = die_offsets[i]; 3142 DWARFDIE die = debug_info->GetDIE (die_ref); 3143 3144 if (die) 3145 { 3146 if (!DIEInDeclContext (parent_decl_ctx, die)) 3147 continue; // The containing decl contexts don't match 3148 3149 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 3150 if (dwarf_ast) 3151 { 3152 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF (die); 3153 if (namespace_decl_ctx) 3154 break; 3155 } 3156 } 3157 else 3158 { 3159 if (m_using_apple_tables) 3160 { 3161 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n", 3162 die_ref.die_offset, name.GetCString()); 3163 } 3164 } 3165 3166 } 3167 } 3168 } 3169 if (log && namespace_decl_ctx) 3170 { 3171 GetObjectFile()->GetModule()->LogMessage (log, 3172 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => CompilerDeclContext(%p/%p) \"%s\"", 3173 name.GetCString(), 3174 static_cast<const void*>(namespace_decl_ctx.GetTypeSystem()), 3175 static_cast<const void*>(namespace_decl_ctx.GetOpaqueDeclContext()), 3176 namespace_decl_ctx.GetName().AsCString("<NULL>")); 3177 } 3178 3179 return namespace_decl_ctx; 3180 } 3181 3182 TypeSP 3183 SymbolFileDWARF::GetTypeForDIE (const DWARFDIE &die, bool resolve_function_context) 3184 { 3185 TypeSP type_sp; 3186 if (die) 3187 { 3188 Type *type_ptr = GetDIEToType().lookup (die.GetDIE()); 3189 if (type_ptr == NULL) 3190 { 3191 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU()); 3192 assert (lldb_cu); 3193 SymbolContext sc(lldb_cu); 3194 const DWARFDebugInfoEntry* parent_die = die.GetParent().GetDIE(); 3195 while (parent_die != nullptr) 3196 { 3197 if (parent_die->Tag() == DW_TAG_subprogram) 3198 break; 3199 parent_die = parent_die->GetParent(); 3200 } 3201 SymbolContext sc_backup = sc; 3202 if (resolve_function_context && parent_die != nullptr && !GetFunction(DWARFDIE(die.GetCU(),parent_die), sc)) 3203 sc = sc_backup; 3204 3205 type_sp = ParseType(sc, die, NULL); 3206 } 3207 else if (type_ptr != DIE_IS_BEING_PARSED) 3208 { 3209 // Grab the existing type from the master types lists 3210 type_sp = type_ptr->shared_from_this(); 3211 } 3212 3213 } 3214 return type_sp; 3215 } 3216 3217 3218 DWARFDIE 3219 SymbolFileDWARF::GetDeclContextDIEContainingDIE (const DWARFDIE &orig_die) 3220 { 3221 if (orig_die) 3222 { 3223 DWARFDIE die = orig_die; 3224 3225 while (die) 3226 { 3227 // If this is the original DIE that we are searching for a declaration 3228 // for, then don't look in the cache as we don't want our own decl 3229 // context to be our decl context... 3230 if (orig_die != die) 3231 { 3232 switch (die.Tag()) 3233 { 3234 case DW_TAG_compile_unit: 3235 case DW_TAG_namespace: 3236 case DW_TAG_structure_type: 3237 case DW_TAG_union_type: 3238 case DW_TAG_class_type: 3239 case DW_TAG_lexical_block: 3240 case DW_TAG_subprogram: 3241 return die; 3242 3243 default: 3244 break; 3245 } 3246 } 3247 3248 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification); 3249 if (spec_die) 3250 { 3251 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die); 3252 if (decl_ctx_die) 3253 return decl_ctx_die; 3254 } 3255 3256 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin); 3257 if (abs_die) 3258 { 3259 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die); 3260 if (decl_ctx_die) 3261 return decl_ctx_die; 3262 } 3263 3264 die = die.GetParent(); 3265 } 3266 } 3267 return DWARFDIE(); 3268 } 3269 3270 3271 Symbol * 3272 SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name) 3273 { 3274 Symbol *objc_class_symbol = NULL; 3275 if (m_obj_file) 3276 { 3277 Symtab *symtab = m_obj_file->GetSymtab (); 3278 if (symtab) 3279 { 3280 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name, 3281 eSymbolTypeObjCClass, 3282 Symtab::eDebugNo, 3283 Symtab::eVisibilityAny); 3284 } 3285 } 3286 return objc_class_symbol; 3287 } 3288 3289 // Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't 3290 // then we can end up looking through all class types for a complete type and never find 3291 // the full definition. We need to know if this attribute is supported, so we determine 3292 // this here and cache th result. We also need to worry about the debug map DWARF file 3293 // if we are doing darwin DWARF in .o file debugging. 3294 bool 3295 SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu) 3296 { 3297 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) 3298 { 3299 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; 3300 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type()) 3301 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 3302 else 3303 { 3304 DWARFDebugInfo* debug_info = DebugInfo(); 3305 const uint32_t num_compile_units = GetNumCompileUnits(); 3306 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 3307 { 3308 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 3309 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) 3310 { 3311 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 3312 break; 3313 } 3314 } 3315 } 3316 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ()) 3317 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this); 3318 } 3319 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; 3320 } 3321 3322 // This function can be used when a DIE is found that is a forward declaration 3323 // DIE and we want to try and find a type that has the complete definition. 3324 TypeSP 3325 SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDIE &die, 3326 const ConstString &type_name, 3327 bool must_be_implementation) 3328 { 3329 3330 TypeSP type_sp; 3331 3332 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name))) 3333 return type_sp; 3334 3335 DIEArray die_offsets; 3336 3337 if (m_using_apple_tables) 3338 { 3339 if (m_apple_types_ap.get()) 3340 { 3341 const char *name_cstr = type_name.GetCString(); 3342 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation); 3343 } 3344 } 3345 else 3346 { 3347 if (!m_indexed) 3348 Index (); 3349 3350 m_type_index.Find (type_name, die_offsets); 3351 } 3352 3353 const size_t num_matches = die_offsets.size(); 3354 3355 if (num_matches) 3356 { 3357 DWARFDebugInfo* debug_info = DebugInfo(); 3358 for (size_t i=0; i<num_matches; ++i) 3359 { 3360 const DIERef& die_ref = die_offsets[i]; 3361 DWARFDIE type_die = debug_info->GetDIE (die_ref); 3362 3363 if (type_die) 3364 { 3365 bool try_resolving_type = false; 3366 3367 // Don't try and resolve the DIE we are looking for with the DIE itself! 3368 if (type_die != die) 3369 { 3370 switch (type_die.Tag()) 3371 { 3372 case DW_TAG_class_type: 3373 case DW_TAG_structure_type: 3374 try_resolving_type = true; 3375 break; 3376 default: 3377 break; 3378 } 3379 } 3380 3381 if (try_resolving_type) 3382 { 3383 if (must_be_implementation && type_die.Supports_DW_AT_APPLE_objc_complete_type()) 3384 try_resolving_type = type_die.GetAttributeValueAsUnsigned (DW_AT_APPLE_objc_complete_type, 0); 3385 3386 if (try_resolving_type) 3387 { 3388 Type *resolved_type = ResolveType (type_die, false, true); 3389 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 3390 { 3391 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n", 3392 die.GetID(), 3393 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"), 3394 type_die.GetID(), 3395 type_cu->GetID()); 3396 3397 if (die) 3398 GetDIEToType()[die.GetDIE()] = resolved_type; 3399 type_sp = resolved_type->shared_from_this(); 3400 break; 3401 } 3402 } 3403 } 3404 } 3405 else 3406 { 3407 if (m_using_apple_tables) 3408 { 3409 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 3410 die_ref.die_offset, type_name.GetCString()); 3411 } 3412 } 3413 3414 } 3415 } 3416 return type_sp; 3417 } 3418 3419 3420 //---------------------------------------------------------------------- 3421 // This function helps to ensure that the declaration contexts match for 3422 // two different DIEs. Often times debug information will refer to a 3423 // forward declaration of a type (the equivalent of "struct my_struct;". 3424 // There will often be a declaration of that type elsewhere that has the 3425 // full definition. When we go looking for the full type "my_struct", we 3426 // will find one or more matches in the accelerator tables and we will 3427 // then need to make sure the type was in the same declaration context 3428 // as the original DIE. This function can efficiently compare two DIEs 3429 // and will return true when the declaration context matches, and false 3430 // when they don't. 3431 //---------------------------------------------------------------------- 3432 bool 3433 SymbolFileDWARF::DIEDeclContextsMatch (const DWARFDIE &die1, 3434 const DWARFDIE &die2) 3435 { 3436 if (die1 == die2) 3437 return true; 3438 3439 DWARFDIECollection decl_ctx_1; 3440 DWARFDIECollection decl_ctx_2; 3441 //The declaration DIE stack is a stack of the declaration context 3442 // DIEs all the way back to the compile unit. If a type "T" is 3443 // declared inside a class "B", and class "B" is declared inside 3444 // a class "A" and class "A" is in a namespace "lldb", and the 3445 // namespace is in a compile unit, there will be a stack of DIEs: 3446 // 3447 // [0] DW_TAG_class_type for "B" 3448 // [1] DW_TAG_class_type for "A" 3449 // [2] DW_TAG_namespace for "lldb" 3450 // [3] DW_TAG_compile_unit for the source file. 3451 // 3452 // We grab both contexts and make sure that everything matches 3453 // all the way back to the compiler unit. 3454 3455 // First lets grab the decl contexts for both DIEs 3456 die1.GetDeclContextDIEs (decl_ctx_1); 3457 die2.GetDeclContextDIEs (decl_ctx_2); 3458 // Make sure the context arrays have the same size, otherwise 3459 // we are done 3460 const size_t count1 = decl_ctx_1.Size(); 3461 const size_t count2 = decl_ctx_2.Size(); 3462 if (count1 != count2) 3463 return false; 3464 3465 // Make sure the DW_TAG values match all the way back up the 3466 // compile unit. If they don't, then we are done. 3467 DWARFDIE decl_ctx_die1; 3468 DWARFDIE decl_ctx_die2; 3469 size_t i; 3470 for (i=0; i<count1; i++) 3471 { 3472 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i); 3473 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i); 3474 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag()) 3475 return false; 3476 } 3477 #if defined LLDB_CONFIGURATION_DEBUG 3478 3479 // Make sure the top item in the decl context die array is always 3480 // DW_TAG_compile_unit. If it isn't then something went wrong in 3481 // the DWARFDIE::GetDeclContextDIEs() function... 3482 assert (decl_ctx_1.GetDIEAtIndex (count1 - 1).Tag() == DW_TAG_compile_unit); 3483 3484 #endif 3485 // Always skip the compile unit when comparing by only iterating up to 3486 // "count - 1". Here we compare the names as we go. 3487 for (i=0; i<count1 - 1; i++) 3488 { 3489 decl_ctx_die1 = decl_ctx_1.GetDIEAtIndex (i); 3490 decl_ctx_die2 = decl_ctx_2.GetDIEAtIndex (i); 3491 const char *name1 = decl_ctx_die1.GetName(); 3492 const char *name2 = decl_ctx_die2.GetName(); 3493 // If the string was from a DW_FORM_strp, then the pointer will often 3494 // be the same! 3495 if (name1 == name2) 3496 continue; 3497 3498 // Name pointers are not equal, so only compare the strings 3499 // if both are not NULL. 3500 if (name1 && name2) 3501 { 3502 // If the strings don't compare, we are done... 3503 if (strcmp(name1, name2) != 0) 3504 return false; 3505 } 3506 else 3507 { 3508 // One name was NULL while the other wasn't 3509 return false; 3510 } 3511 } 3512 // We made it through all of the checks and the declaration contexts 3513 // are equal. 3514 return true; 3515 } 3516 3517 3518 TypeSP 3519 SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx) 3520 { 3521 TypeSP type_sp; 3522 3523 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize(); 3524 if (dwarf_decl_ctx_count > 0) 3525 { 3526 const ConstString type_name(dwarf_decl_ctx[0].name); 3527 const dw_tag_t tag = dwarf_decl_ctx[0].tag; 3528 3529 if (type_name) 3530 { 3531 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS)); 3532 if (log) 3533 { 3534 GetObjectFile()->GetModule()->LogMessage (log, 3535 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')", 3536 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 3537 dwarf_decl_ctx.GetQualifiedName()); 3538 } 3539 3540 DIEArray die_offsets; 3541 3542 if (m_using_apple_tables) 3543 { 3544 if (m_apple_types_ap.get()) 3545 { 3546 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag); 3547 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash); 3548 if (has_tag && has_qualified_name_hash) 3549 { 3550 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName(); 3551 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name); 3552 if (log) 3553 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()"); 3554 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets); 3555 } 3556 else if (has_tag) 3557 { 3558 if (log) 3559 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()"); 3560 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets); 3561 } 3562 else 3563 { 3564 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets); 3565 } 3566 } 3567 } 3568 else 3569 { 3570 if (!m_indexed) 3571 Index (); 3572 3573 m_type_index.Find (type_name, die_offsets); 3574 } 3575 3576 const size_t num_matches = die_offsets.size(); 3577 3578 3579 if (num_matches) 3580 { 3581 DWARFDebugInfo* debug_info = DebugInfo(); 3582 for (size_t i=0; i<num_matches; ++i) 3583 { 3584 const DIERef& die_ref = die_offsets[i]; 3585 DWARFDIE type_die = debug_info->GetDIE (die_ref); 3586 3587 if (type_die) 3588 { 3589 bool try_resolving_type = false; 3590 3591 // Don't try and resolve the DIE we are looking for with the DIE itself! 3592 const dw_tag_t type_tag = type_die.Tag(); 3593 // Make sure the tags match 3594 if (type_tag == tag) 3595 { 3596 // The tags match, lets try resolving this type 3597 try_resolving_type = true; 3598 } 3599 else 3600 { 3601 // The tags don't match, but we need to watch our for a 3602 // forward declaration for a struct and ("struct foo") 3603 // ends up being a class ("class foo { ... };") or 3604 // vice versa. 3605 switch (type_tag) 3606 { 3607 case DW_TAG_class_type: 3608 // We had a "class foo", see if we ended up with a "struct foo { ... };" 3609 try_resolving_type = (tag == DW_TAG_structure_type); 3610 break; 3611 case DW_TAG_structure_type: 3612 // We had a "struct foo", see if we ended up with a "class foo { ... };" 3613 try_resolving_type = (tag == DW_TAG_class_type); 3614 break; 3615 default: 3616 // Tags don't match, don't event try to resolve 3617 // using this type whose name matches.... 3618 break; 3619 } 3620 } 3621 3622 if (try_resolving_type) 3623 { 3624 DWARFDeclContext type_dwarf_decl_ctx; 3625 type_die.GetDWARFDeclContext (type_dwarf_decl_ctx); 3626 3627 if (log) 3628 { 3629 GetObjectFile()->GetModule()->LogMessage (log, 3630 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)", 3631 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 3632 dwarf_decl_ctx.GetQualifiedName(), 3633 type_die.GetOffset(), 3634 type_dwarf_decl_ctx.GetQualifiedName()); 3635 } 3636 3637 // Make sure the decl contexts match all the way up 3638 if (dwarf_decl_ctx == type_dwarf_decl_ctx) 3639 { 3640 Type *resolved_type = ResolveType (type_die, false); 3641 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 3642 { 3643 type_sp = resolved_type->shared_from_this(); 3644 break; 3645 } 3646 } 3647 } 3648 else 3649 { 3650 if (log) 3651 { 3652 std::string qualified_name; 3653 type_die.GetQualifiedName(qualified_name); 3654 GetObjectFile()->GetModule()->LogMessage (log, 3655 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)", 3656 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 3657 dwarf_decl_ctx.GetQualifiedName(), 3658 type_die.GetOffset(), 3659 qualified_name.c_str()); 3660 } 3661 } 3662 } 3663 else 3664 { 3665 if (m_using_apple_tables) 3666 { 3667 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 3668 die_ref.die_offset, type_name.GetCString()); 3669 } 3670 } 3671 3672 } 3673 } 3674 } 3675 } 3676 return type_sp; 3677 } 3678 3679 TypeSP 3680 SymbolFileDWARF::ParseType (const SymbolContext& sc, const DWARFDIE &die, bool *type_is_new_ptr) 3681 { 3682 TypeSP type_sp; 3683 3684 if (die) 3685 { 3686 TypeSystem *type_system = GetTypeSystemForLanguage(die.GetCU()->GetLanguageType()); 3687 3688 if (type_system) 3689 { 3690 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 3691 if (dwarf_ast) 3692 { 3693 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); 3694 type_sp = dwarf_ast->ParseTypeFromDWARF (sc, die, log, type_is_new_ptr); 3695 if (type_sp) 3696 { 3697 TypeList* type_list = GetTypeList(); 3698 if (type_list) 3699 type_list->Insert(type_sp); 3700 } 3701 } 3702 } 3703 } 3704 3705 return type_sp; 3706 } 3707 3708 size_t 3709 SymbolFileDWARF::ParseTypes 3710 ( 3711 const SymbolContext& sc, 3712 const DWARFDIE &orig_die, 3713 bool parse_siblings, 3714 bool parse_children 3715 ) 3716 { 3717 size_t types_added = 0; 3718 DWARFDIE die = orig_die; 3719 while (die) 3720 { 3721 bool type_is_new = false; 3722 if (ParseType(sc, die, &type_is_new).get()) 3723 { 3724 if (type_is_new) 3725 ++types_added; 3726 } 3727 3728 if (parse_children && die.HasChildren()) 3729 { 3730 if (die.Tag() == DW_TAG_subprogram) 3731 { 3732 SymbolContext child_sc(sc); 3733 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get(); 3734 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true); 3735 } 3736 else 3737 types_added += ParseTypes(sc, die.GetFirstChild(), true, true); 3738 } 3739 3740 if (parse_siblings) 3741 die = die.GetSibling(); 3742 else 3743 die.Clear(); 3744 } 3745 return types_added; 3746 } 3747 3748 3749 size_t 3750 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc) 3751 { 3752 assert(sc.comp_unit && sc.function); 3753 size_t functions_added = 0; 3754 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 3755 if (dwarf_cu) 3756 { 3757 const dw_offset_t function_die_offset = sc.function->GetID(); 3758 DWARFDIE function_die = dwarf_cu->GetDIE (function_die_offset); 3759 if (function_die) 3760 { 3761 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), function_die, LLDB_INVALID_ADDRESS, 0); 3762 } 3763 } 3764 3765 return functions_added; 3766 } 3767 3768 3769 size_t 3770 SymbolFileDWARF::ParseTypes (const SymbolContext &sc) 3771 { 3772 // At least a compile unit must be valid 3773 assert(sc.comp_unit); 3774 size_t types_added = 0; 3775 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 3776 if (dwarf_cu) 3777 { 3778 if (sc.function) 3779 { 3780 dw_offset_t function_die_offset = sc.function->GetID(); 3781 DWARFDIE func_die = dwarf_cu->GetDIE(function_die_offset); 3782 if (func_die && func_die.HasChildren()) 3783 { 3784 types_added = ParseTypes(sc, func_die.GetFirstChild(), true, true); 3785 } 3786 } 3787 else 3788 { 3789 DWARFDIE dwarf_cu_die = dwarf_cu->DIE(); 3790 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) 3791 { 3792 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true); 3793 } 3794 } 3795 } 3796 3797 return types_added; 3798 } 3799 3800 size_t 3801 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc) 3802 { 3803 if (sc.comp_unit != NULL) 3804 { 3805 DWARFDebugInfo* info = DebugInfo(); 3806 if (info == NULL) 3807 return 0; 3808 3809 if (sc.function) 3810 { 3811 DWARFDIE function_die = info->GetDIE(DIERef(sc.function->GetID())); 3812 3813 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress (DW_AT_low_pc, LLDB_INVALID_ADDRESS); 3814 if (func_lo_pc != LLDB_INVALID_ADDRESS) 3815 { 3816 const size_t num_variables = ParseVariables(sc, function_die.GetFirstChild(), func_lo_pc, true, true); 3817 3818 // Let all blocks know they have parse all their variables 3819 sc.function->GetBlock (false).SetDidParseVariables (true, true); 3820 return num_variables; 3821 } 3822 } 3823 else if (sc.comp_unit) 3824 { 3825 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()); 3826 3827 if (dwarf_cu == NULL) 3828 return 0; 3829 3830 uint32_t vars_added = 0; 3831 VariableListSP variables (sc.comp_unit->GetVariableList(false)); 3832 3833 if (variables.get() == NULL) 3834 { 3835 variables.reset(new VariableList()); 3836 sc.comp_unit->SetVariableList(variables); 3837 3838 DIEArray die_offsets; 3839 if (m_using_apple_tables) 3840 { 3841 if (m_apple_names_ap.get()) 3842 { 3843 DWARFMappedHash::DIEInfoArray hash_data_array; 3844 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(), 3845 dwarf_cu->GetNextCompileUnitOffset(), 3846 hash_data_array)) 3847 { 3848 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 3849 } 3850 } 3851 } 3852 else 3853 { 3854 // Index if we already haven't to make sure the compile units 3855 // get indexed and make their global DIE index list 3856 if (!m_indexed) 3857 Index (); 3858 3859 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(), 3860 die_offsets); 3861 } 3862 3863 const size_t num_matches = die_offsets.size(); 3864 if (num_matches) 3865 { 3866 DWARFDebugInfo* debug_info = DebugInfo(); 3867 for (size_t i=0; i<num_matches; ++i) 3868 { 3869 const DIERef& die_ref = die_offsets[i]; 3870 DWARFDIE die = debug_info->GetDIE (die_ref); 3871 if (die) 3872 { 3873 VariableSP var_sp (ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS)); 3874 if (var_sp) 3875 { 3876 variables->AddVariableIfUnique (var_sp); 3877 ++vars_added; 3878 } 3879 } 3880 else 3881 { 3882 if (m_using_apple_tables) 3883 { 3884 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_ref.die_offset); 3885 } 3886 } 3887 3888 } 3889 } 3890 } 3891 return vars_added; 3892 } 3893 } 3894 return 0; 3895 } 3896 3897 VariableSP 3898 SymbolFileDWARF::ParseVariableDIE 3899 ( 3900 const SymbolContext& sc, 3901 const DWARFDIE &die, 3902 const lldb::addr_t func_low_pc 3903 ) 3904 { 3905 if (die.GetDWARF() != this) 3906 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc); 3907 3908 VariableSP var_sp; 3909 if (!die) 3910 return var_sp; 3911 3912 var_sp = GetDIEToVariable()[die.GetDIE()]; 3913 if (var_sp) 3914 return var_sp; // Already been parsed! 3915 3916 const dw_tag_t tag = die.Tag(); 3917 ModuleSP module = GetObjectFile()->GetModule(); 3918 3919 if ((tag == DW_TAG_variable) || 3920 (tag == DW_TAG_constant) || 3921 (tag == DW_TAG_formal_parameter && sc.function)) 3922 { 3923 DWARFAttributes attributes; 3924 const size_t num_attributes = die.GetAttributes(attributes); 3925 DWARFDIE spec_die; 3926 if (num_attributes > 0) 3927 { 3928 const char *name = NULL; 3929 const char *mangled = NULL; 3930 Declaration decl; 3931 uint32_t i; 3932 DWARFFormValue type_die_form; 3933 DWARFExpression location(die.GetCU()); 3934 bool is_external = false; 3935 bool is_artificial = false; 3936 bool location_is_const_value_data = false; 3937 bool has_explicit_location = false; 3938 DWARFFormValue const_value; 3939 //AccessType accessibility = eAccessNone; 3940 3941 for (i=0; i<num_attributes; ++i) 3942 { 3943 dw_attr_t attr = attributes.AttributeAtIndex(i); 3944 DWARFFormValue form_value; 3945 3946 if (attributes.ExtractFormValueAtIndex(i, form_value)) 3947 { 3948 switch (attr) 3949 { 3950 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3951 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3952 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3953 case DW_AT_name: name = form_value.AsCString(); break; 3954 case DW_AT_linkage_name: 3955 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(); break; 3956 case DW_AT_type: type_die_form = form_value; break; 3957 case DW_AT_external: is_external = form_value.Boolean(); break; 3958 case DW_AT_const_value: 3959 // If we have already found a DW_AT_location attribute, ignore this attribute. 3960 if (!has_explicit_location) 3961 { 3962 location_is_const_value_data = true; 3963 // The constant value will be either a block, a data value or a string. 3964 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 3965 if (DWARFFormValue::IsBlockForm(form_value.Form())) 3966 { 3967 // Retrieve the value as a block expression. 3968 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 3969 uint32_t block_length = form_value.Unsigned(); 3970 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length); 3971 } 3972 else if (DWARFFormValue::IsDataForm(form_value.Form())) 3973 { 3974 // Retrieve the value as a data expression. 3975 DWARFFormValue::FixedFormSizes fixed_form_sizes = 3976 DWARFFormValue::GetFixedFormSizesForAddressSize ( 3977 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), 3978 attributes.CompileUnitAtIndex(i)->IsDWARF64()); 3979 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 3980 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form()); 3981 if (data_length == 0) 3982 { 3983 const uint8_t *data_pointer = form_value.BlockData(); 3984 if (data_pointer) 3985 { 3986 form_value.Unsigned(); 3987 } 3988 else if (DWARFFormValue::IsDataForm(form_value.Form())) 3989 { 3990 // we need to get the byte size of the type later after we create the variable 3991 const_value = form_value; 3992 } 3993 } 3994 else 3995 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length); 3996 } 3997 else 3998 { 3999 // Retrieve the value as a string expression. 4000 if (form_value.Form() == DW_FORM_strp) 4001 { 4002 DWARFFormValue::FixedFormSizes fixed_form_sizes = 4003 DWARFFormValue::GetFixedFormSizesForAddressSize ( 4004 attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), 4005 attributes.CompileUnitAtIndex(i)->IsDWARF64()); 4006 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 4007 uint32_t data_length = fixed_form_sizes.GetSize(form_value.Form()); 4008 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length); 4009 } 4010 else 4011 { 4012 const char *str = form_value.AsCString(); 4013 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart(); 4014 uint32_t string_length = strlen(str) + 1; 4015 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length); 4016 } 4017 } 4018 } 4019 break; 4020 case DW_AT_location: 4021 { 4022 location_is_const_value_data = false; 4023 has_explicit_location = true; 4024 if (form_value.BlockData()) 4025 { 4026 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 4027 4028 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 4029 uint32_t block_length = form_value.Unsigned(); 4030 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length); 4031 } 4032 else 4033 { 4034 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data(); 4035 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 4036 4037 size_t loc_list_length = DWARFExpression::LocationListSize(die.GetCU(), debug_loc_data, debug_loc_offset); 4038 if (loc_list_length > 0) 4039 { 4040 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length); 4041 assert (func_low_pc != LLDB_INVALID_ADDRESS); 4042 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress()); 4043 } 4044 } 4045 } 4046 break; 4047 case DW_AT_specification: 4048 { 4049 DWARFDebugInfo* debug_info = DebugInfo(); 4050 if (debug_info) 4051 spec_die = debug_info->GetDIE(DIERef(form_value)); 4052 break; 4053 } 4054 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 4055 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 4056 case DW_AT_declaration: 4057 case DW_AT_description: 4058 case DW_AT_endianity: 4059 case DW_AT_segment: 4060 case DW_AT_start_scope: 4061 case DW_AT_visibility: 4062 default: 4063 case DW_AT_abstract_origin: 4064 case DW_AT_sibling: 4065 break; 4066 } 4067 } 4068 } 4069 4070 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die); 4071 const dw_tag_t parent_tag = die.GetParent().Tag(); 4072 bool is_static_member = parent_tag == DW_TAG_compile_unit && (parent_context_die.Tag() == DW_TAG_class_type || parent_context_die.Tag() == DW_TAG_structure_type); 4073 4074 ValueType scope = eValueTypeInvalid; 4075 4076 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die); 4077 SymbolContextScope * symbol_context_scope = NULL; 4078 4079 if (!mangled) 4080 { 4081 // LLDB relies on the mangled name (DW_TAG_linkage_name or DW_AT_MIPS_linkage_name) to 4082 // generate fully qualified names of global variables with commands like "frame var j". 4083 // For example, if j were an int variable holding a value 4 and declared in a namespace 4084 // B which in turn is contained in a namespace A, the command "frame var j" returns 4085 // "(int) A::B::j = 4". If the compiler does not emit a linkage name, we should be able 4086 // to generate a fully qualified name from the declaration context. 4087 if (parent_tag == DW_TAG_compile_unit && 4088 Language::LanguageIsCPlusPlus(die.GetLanguage())) 4089 { 4090 DWARFDeclContext decl_ctx; 4091 4092 die.GetDWARFDeclContext(decl_ctx); 4093 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString(); 4094 } 4095 } 4096 4097 // DWARF doesn't specify if a DW_TAG_variable is a local, global 4098 // or static variable, so we have to do a little digging by 4099 // looking at the location of a variable to see if it contains 4100 // a DW_OP_addr opcode _somewhere_ in the definition. I say 4101 // somewhere because clang likes to combine small global variables 4102 // into the same symbol and have locations like: 4103 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus 4104 // So if we don't have a DW_TAG_formal_parameter, we can look at 4105 // the location to see if it contains a DW_OP_addr opcode, and 4106 // then we can correctly classify our variables. 4107 if (tag == DW_TAG_formal_parameter) 4108 scope = eValueTypeVariableArgument; 4109 else 4110 { 4111 bool op_error = false; 4112 // Check if the location has a DW_OP_addr with any address value... 4113 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS; 4114 if (!location_is_const_value_data) 4115 { 4116 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error); 4117 if (op_error) 4118 { 4119 StreamString strm; 4120 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL); 4121 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die.GetOffset(), die.GetTagAsCString(), strm.GetString().c_str()); 4122 } 4123 } 4124 4125 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS) 4126 { 4127 if (is_external) 4128 scope = eValueTypeVariableGlobal; 4129 else 4130 scope = eValueTypeVariableStatic; 4131 4132 4133 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile (); 4134 4135 if (debug_map_symfile) 4136 { 4137 // When leaving the DWARF in the .o files on darwin, 4138 // when we have a global variable that wasn't initialized, 4139 // the .o file might not have allocated a virtual 4140 // address for the global variable. In this case it will 4141 // have created a symbol for the global variable 4142 // that is undefined/data and external and the value will 4143 // be the byte size of the variable. When we do the 4144 // address map in SymbolFileDWARFDebugMap we rely on 4145 // having an address, we need to do some magic here 4146 // so we can get the correct address for our global 4147 // variable. The address for all of these entries 4148 // will be zero, and there will be an undefined symbol 4149 // in this object file, and the executable will have 4150 // a matching symbol with a good address. So here we 4151 // dig up the correct address and replace it in the 4152 // location for the variable, and set the variable's 4153 // symbol context scope to be that of the main executable 4154 // so the file address will resolve correctly. 4155 bool linked_oso_file_addr = false; 4156 if (is_external && location_DW_OP_addr == 0) 4157 { 4158 // we have a possible uninitialized extern global 4159 ConstString const_name(mangled ? mangled : name); 4160 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile(); 4161 if (debug_map_objfile) 4162 { 4163 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab(); 4164 if (debug_map_symtab) 4165 { 4166 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name, 4167 eSymbolTypeData, 4168 Symtab::eDebugYes, 4169 Symtab::eVisibilityExtern); 4170 if (exe_symbol) 4171 { 4172 if (exe_symbol->ValueIsAddress()) 4173 { 4174 const addr_t exe_file_addr = exe_symbol->GetAddressRef().GetFileAddress(); 4175 if (exe_file_addr != LLDB_INVALID_ADDRESS) 4176 { 4177 if (location.Update_DW_OP_addr (exe_file_addr)) 4178 { 4179 linked_oso_file_addr = true; 4180 symbol_context_scope = exe_symbol; 4181 } 4182 } 4183 } 4184 } 4185 } 4186 } 4187 } 4188 4189 if (!linked_oso_file_addr) 4190 { 4191 // The DW_OP_addr is not zero, but it contains a .o file address which 4192 // needs to be linked up correctly. 4193 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr); 4194 if (exe_file_addr != LLDB_INVALID_ADDRESS) 4195 { 4196 // Update the file address for this variable 4197 location.Update_DW_OP_addr (exe_file_addr); 4198 } 4199 else 4200 { 4201 // Variable didn't make it into the final executable 4202 return var_sp; 4203 } 4204 } 4205 } 4206 } 4207 else 4208 { 4209 scope = eValueTypeVariableLocal; 4210 } 4211 } 4212 4213 if (symbol_context_scope == NULL) 4214 { 4215 switch (parent_tag) 4216 { 4217 case DW_TAG_subprogram: 4218 case DW_TAG_inlined_subroutine: 4219 case DW_TAG_lexical_block: 4220 if (sc.function) 4221 { 4222 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID()); 4223 if (symbol_context_scope == NULL) 4224 symbol_context_scope = sc.function; 4225 } 4226 break; 4227 4228 default: 4229 symbol_context_scope = sc.comp_unit; 4230 break; 4231 } 4232 } 4233 4234 if (symbol_context_scope) 4235 { 4236 SymbolFileTypeSP type_sp(new SymbolFileType(*this, DIERef(type_die_form).GetUID())); 4237 4238 if (const_value.Form() && type_sp && type_sp->GetType()) 4239 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), die.GetCU()->GetAddressByteSize()); 4240 4241 var_sp.reset (new Variable (die.GetID(), 4242 name, 4243 mangled, 4244 type_sp, 4245 scope, 4246 symbol_context_scope, 4247 &decl, 4248 location, 4249 is_external, 4250 is_artificial, 4251 is_static_member)); 4252 4253 var_sp->SetLocationIsConstantValueData (location_is_const_value_data); 4254 } 4255 else 4256 { 4257 // Not ready to parse this variable yet. It might be a global 4258 // or static variable that is in a function scope and the function 4259 // in the symbol context wasn't filled in yet 4260 return var_sp; 4261 } 4262 } 4263 // Cache var_sp even if NULL (the variable was just a specification or 4264 // was missing vital information to be able to be displayed in the debugger 4265 // (missing location due to optimization, etc)) so we don't re-parse 4266 // this DIE over and over later... 4267 GetDIEToVariable()[die.GetDIE()] = var_sp; 4268 if (spec_die) 4269 GetDIEToVariable()[spec_die.GetDIE()] = var_sp; 4270 } 4271 return var_sp; 4272 } 4273 4274 4275 DWARFDIE 4276 SymbolFileDWARF::FindBlockContainingSpecification (const DIERef& func_die_ref, 4277 dw_offset_t spec_block_die_offset) 4278 { 4279 // Give the concrete function die specified by "func_die_offset", find the 4280 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 4281 // to "spec_block_die_offset" 4282 return FindBlockContainingSpecification (DebugInfo()->GetDIE (func_die_ref), spec_block_die_offset); 4283 } 4284 4285 4286 DWARFDIE 4287 SymbolFileDWARF::FindBlockContainingSpecification(const DWARFDIE &die, 4288 dw_offset_t spec_block_die_offset) 4289 { 4290 if (die) 4291 { 4292 switch (die.Tag()) 4293 { 4294 case DW_TAG_subprogram: 4295 case DW_TAG_inlined_subroutine: 4296 case DW_TAG_lexical_block: 4297 { 4298 if (die.GetAttributeValueAsReference (DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset) 4299 return die; 4300 4301 if (die.GetAttributeValueAsReference (DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset) 4302 return die; 4303 } 4304 break; 4305 } 4306 4307 // Give the concrete function die specified by "func_die_offset", find the 4308 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 4309 // to "spec_block_die_offset" 4310 for (DWARFDIE child_die = die.GetFirstChild(); child_die; child_die = child_die.GetSibling()) 4311 { 4312 DWARFDIE result_die = FindBlockContainingSpecification (child_die, spec_block_die_offset); 4313 if (result_die) 4314 return result_die; 4315 } 4316 } 4317 4318 return DWARFDIE(); 4319 } 4320 4321 size_t 4322 SymbolFileDWARF::ParseVariables (const SymbolContext& sc, 4323 const DWARFDIE &orig_die, 4324 const lldb::addr_t func_low_pc, 4325 bool parse_siblings, 4326 bool parse_children, 4327 VariableList* cc_variable_list) 4328 { 4329 if (!orig_die) 4330 return 0; 4331 4332 VariableListSP variable_list_sp; 4333 4334 size_t vars_added = 0; 4335 DWARFDIE die = orig_die; 4336 while (die) 4337 { 4338 dw_tag_t tag = die.Tag(); 4339 4340 // Check to see if we have already parsed this variable or constant? 4341 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()]; 4342 if (var_sp) 4343 { 4344 if (cc_variable_list) 4345 cc_variable_list->AddVariableIfUnique (var_sp); 4346 } 4347 else 4348 { 4349 // We haven't already parsed it, lets do that now. 4350 if ((tag == DW_TAG_variable) || 4351 (tag == DW_TAG_constant) || 4352 (tag == DW_TAG_formal_parameter && sc.function)) 4353 { 4354 if (variable_list_sp.get() == NULL) 4355 { 4356 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die); 4357 dw_tag_t parent_tag = sc_parent_die.Tag(); 4358 switch (parent_tag) 4359 { 4360 case DW_TAG_compile_unit: 4361 if (sc.comp_unit != NULL) 4362 { 4363 variable_list_sp = sc.comp_unit->GetVariableList(false); 4364 if (variable_list_sp.get() == NULL) 4365 { 4366 variable_list_sp.reset(new VariableList()); 4367 sc.comp_unit->SetVariableList(variable_list_sp); 4368 } 4369 } 4370 else 4371 { 4372 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n", 4373 sc_parent_die.GetID(), 4374 sc_parent_die.GetTagAsCString(), 4375 orig_die.GetID(), 4376 orig_die.GetTagAsCString()); 4377 } 4378 break; 4379 4380 case DW_TAG_subprogram: 4381 case DW_TAG_inlined_subroutine: 4382 case DW_TAG_lexical_block: 4383 if (sc.function != NULL) 4384 { 4385 // Check to see if we already have parsed the variables for the given scope 4386 4387 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID()); 4388 if (block == NULL) 4389 { 4390 // This must be a specification or abstract origin with 4391 // a concrete block counterpart in the current function. We need 4392 // to find the concrete block so we can correctly add the 4393 // variable to it 4394 const DWARFDIE concrete_block_die = FindBlockContainingSpecification (DIERef(sc.function->GetID()), 4395 sc_parent_die.GetOffset()); 4396 if (concrete_block_die) 4397 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die.GetID()); 4398 } 4399 4400 if (block != NULL) 4401 { 4402 const bool can_create = false; 4403 variable_list_sp = block->GetBlockVariableList (can_create); 4404 if (variable_list_sp.get() == NULL) 4405 { 4406 variable_list_sp.reset(new VariableList()); 4407 block->SetVariableList(variable_list_sp); 4408 } 4409 } 4410 } 4411 break; 4412 4413 default: 4414 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n", 4415 orig_die.GetID(), 4416 orig_die.GetTagAsCString()); 4417 break; 4418 } 4419 } 4420 4421 if (variable_list_sp) 4422 { 4423 VariableSP var_sp (ParseVariableDIE(sc, die, func_low_pc)); 4424 if (var_sp) 4425 { 4426 variable_list_sp->AddVariableIfUnique (var_sp); 4427 if (cc_variable_list) 4428 cc_variable_list->AddVariableIfUnique (var_sp); 4429 ++vars_added; 4430 } 4431 } 4432 } 4433 } 4434 4435 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram); 4436 4437 if (!skip_children && parse_children && die.HasChildren()) 4438 { 4439 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, true, cc_variable_list); 4440 } 4441 4442 if (parse_siblings) 4443 die = die.GetSibling(); 4444 else 4445 die.Clear(); 4446 } 4447 return vars_added; 4448 } 4449 4450 //------------------------------------------------------------------ 4451 // PluginInterface protocol 4452 //------------------------------------------------------------------ 4453 ConstString 4454 SymbolFileDWARF::GetPluginName() 4455 { 4456 return GetPluginNameStatic(); 4457 } 4458 4459 uint32_t 4460 SymbolFileDWARF::GetPluginVersion() 4461 { 4462 return 1; 4463 } 4464 4465 void 4466 SymbolFileDWARF::DumpIndexes () 4467 { 4468 StreamFile s(stdout, false); 4469 4470 s.Printf ("DWARF index for (%s) '%s':", 4471 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(), 4472 GetObjectFile()->GetFileSpec().GetPath().c_str()); 4473 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 4474 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 4475 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 4476 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 4477 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 4478 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 4479 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 4480 s.Printf("\nNamespaces:\n"); m_namespace_index.Dump (&s); 4481 } 4482 4483 4484 SymbolFileDWARFDebugMap * 4485 SymbolFileDWARF::GetDebugMapSymfile () 4486 { 4487 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) 4488 { 4489 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock()); 4490 if (module_sp) 4491 { 4492 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 4493 if (sym_vendor) 4494 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile(); 4495 } 4496 } 4497 return m_debug_map_symfile; 4498 } 4499 4500 DWARFExpression::LocationListFormat 4501 SymbolFileDWARF::GetLocationListFormat() const 4502 { 4503 return DWARFExpression::RegularLocationList; 4504 } 4505