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 "clang/AST/ASTConsumer.h" 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/DeclGroup.h" 17 #include "clang/AST/DeclObjC.h" 18 #include "clang/AST/DeclTemplate.h" 19 #include "clang/Basic/Builtins.h" 20 #include "clang/Basic/IdentifierTable.h" 21 #include "clang/Basic/LangOptions.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "clang/Basic/TargetInfo.h" 24 #include "clang/Basic/Specifiers.h" 25 #include "clang/Sema/DeclSpec.h" 26 27 #include "llvm/Support/Casting.h" 28 29 #include "lldb/Core/Module.h" 30 #include "lldb/Core/PluginManager.h" 31 #include "lldb/Core/RegularExpression.h" 32 #include "lldb/Core/Scalar.h" 33 #include "lldb/Core/Section.h" 34 #include "lldb/Core/StreamFile.h" 35 #include "lldb/Core/StreamString.h" 36 #include "lldb/Core/Timer.h" 37 #include "lldb/Core/Value.h" 38 39 #include "lldb/Host/Host.h" 40 41 #include "lldb/Symbol/Block.h" 42 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" 43 #include "lldb/Symbol/CompileUnit.h" 44 #include "lldb/Symbol/LineTable.h" 45 #include "lldb/Symbol/ObjectFile.h" 46 #include "lldb/Symbol/SymbolVendor.h" 47 #include "lldb/Symbol/VariableList.h" 48 49 #include "lldb/Target/ObjCLanguageRuntime.h" 50 #include "lldb/Target/CPPLanguageRuntime.h" 51 52 #include "DWARFCompileUnit.h" 53 #include "DWARFDebugAbbrev.h" 54 #include "DWARFDebugAranges.h" 55 #include "DWARFDebugInfo.h" 56 #include "DWARFDebugInfoEntry.h" 57 #include "DWARFDebugLine.h" 58 #include "DWARFDebugPubnames.h" 59 #include "DWARFDebugRanges.h" 60 #include "DWARFDeclContext.h" 61 #include "DWARFDIECollection.h" 62 #include "DWARFFormValue.h" 63 #include "DWARFLocationList.h" 64 #include "LogChannelDWARF.h" 65 #include "SymbolFileDWARFDebugMap.h" 66 67 #include <map> 68 69 #include <ctype.h> 70 #include <string.h> 71 72 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 73 74 #ifdef ENABLE_DEBUG_PRINTF 75 #include <stdio.h> 76 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) 77 #else 78 #define DEBUG_PRINTF(fmt, ...) 79 #endif 80 81 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1) 82 83 using namespace lldb; 84 using namespace lldb_private; 85 86 //static inline bool 87 //child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag) 88 //{ 89 // switch (tag) 90 // { 91 // default: 92 // break; 93 // case DW_TAG_subprogram: 94 // case DW_TAG_inlined_subroutine: 95 // case DW_TAG_class_type: 96 // case DW_TAG_structure_type: 97 // case DW_TAG_union_type: 98 // return true; 99 // } 100 // return false; 101 //} 102 // 103 static AccessType 104 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility) 105 { 106 switch (dwarf_accessibility) 107 { 108 case DW_ACCESS_public: return eAccessPublic; 109 case DW_ACCESS_private: return eAccessPrivate; 110 case DW_ACCESS_protected: return eAccessProtected; 111 default: break; 112 } 113 return eAccessNone; 114 } 115 116 static const char* 117 removeHostnameFromPathname(const char* path_from_dwarf) 118 { 119 if (!path_from_dwarf || !path_from_dwarf[0]) 120 { 121 return path_from_dwarf; 122 } 123 124 const char *colon_pos = strchr(path_from_dwarf, ':'); 125 if (!colon_pos) 126 { 127 return path_from_dwarf; 128 } 129 130 // check whether we have a windows path, and so the first character 131 // is a drive-letter not a hostname. 132 if ( 133 colon_pos == path_from_dwarf + 1 && 134 isalpha(*path_from_dwarf) && 135 strlen(path_from_dwarf) > 2 && 136 '\\' == path_from_dwarf[2]) 137 { 138 return path_from_dwarf; 139 } 140 141 return colon_pos + 1; 142 } 143 144 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE) 145 146 class DIEStack 147 { 148 public: 149 150 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die) 151 { 152 m_dies.push_back (DIEInfo(cu, die)); 153 } 154 155 156 void LogDIEs (Log *log, SymbolFileDWARF *dwarf) 157 { 158 StreamString log_strm; 159 const size_t n = m_dies.size(); 160 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n); 161 for (size_t i=0; i<n; i++) 162 { 163 DWARFCompileUnit *cu = m_dies[i].cu; 164 const DWARFDebugInfoEntry *die = m_dies[i].die; 165 std::string qualified_name; 166 die->GetQualifiedName(dwarf, cu, qualified_name); 167 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n", 168 (uint64_t)i, 169 die->GetOffset(), 170 DW_TAG_value_to_name(die->Tag()), 171 qualified_name.c_str()); 172 } 173 log->PutCString(log_strm.GetData()); 174 } 175 void Pop () 176 { 177 m_dies.pop_back(); 178 } 179 180 class ScopedPopper 181 { 182 public: 183 ScopedPopper (DIEStack &die_stack) : 184 m_die_stack (die_stack), 185 m_valid (false) 186 { 187 } 188 189 void 190 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die) 191 { 192 m_valid = true; 193 m_die_stack.Push (cu, die); 194 } 195 196 ~ScopedPopper () 197 { 198 if (m_valid) 199 m_die_stack.Pop(); 200 } 201 202 203 204 protected: 205 DIEStack &m_die_stack; 206 bool m_valid; 207 }; 208 209 protected: 210 struct DIEInfo { 211 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) : 212 cu(c), 213 die(d) 214 { 215 } 216 DWARFCompileUnit *cu; 217 const DWARFDebugInfoEntry *die; 218 }; 219 typedef std::vector<DIEInfo> Stack; 220 Stack m_dies; 221 }; 222 #endif 223 224 void 225 SymbolFileDWARF::Initialize() 226 { 227 LogChannelDWARF::Initialize(); 228 PluginManager::RegisterPlugin (GetPluginNameStatic(), 229 GetPluginDescriptionStatic(), 230 CreateInstance); 231 } 232 233 void 234 SymbolFileDWARF::Terminate() 235 { 236 PluginManager::UnregisterPlugin (CreateInstance); 237 LogChannelDWARF::Initialize(); 238 } 239 240 241 lldb_private::ConstString 242 SymbolFileDWARF::GetPluginNameStatic() 243 { 244 static ConstString g_name("dwarf"); 245 return g_name; 246 } 247 248 const char * 249 SymbolFileDWARF::GetPluginDescriptionStatic() 250 { 251 return "DWARF and DWARF3 debug symbol file reader."; 252 } 253 254 255 SymbolFile* 256 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file) 257 { 258 return new SymbolFileDWARF(obj_file); 259 } 260 261 TypeList * 262 SymbolFileDWARF::GetTypeList () 263 { 264 if (GetDebugMapSymfile ()) 265 return m_debug_map_symfile->GetTypeList(); 266 return m_obj_file->GetModule()->GetTypeList(); 267 268 } 269 void 270 SymbolFileDWARF::GetTypes (DWARFCompileUnit* cu, 271 const DWARFDebugInfoEntry *die, 272 dw_offset_t min_die_offset, 273 dw_offset_t max_die_offset, 274 uint32_t type_mask, 275 TypeSet &type_set) 276 { 277 if (cu) 278 { 279 if (die) 280 { 281 const dw_offset_t die_offset = die->GetOffset(); 282 283 if (die_offset >= max_die_offset) 284 return; 285 286 if (die_offset >= min_die_offset) 287 { 288 const dw_tag_t tag = die->Tag(); 289 290 bool add_type = false; 291 292 switch (tag) 293 { 294 case DW_TAG_array_type: add_type = (type_mask & eTypeClassArray ) != 0; break; 295 case DW_TAG_unspecified_type: 296 case DW_TAG_base_type: add_type = (type_mask & eTypeClassBuiltin ) != 0; break; 297 case DW_TAG_class_type: add_type = (type_mask & eTypeClassClass ) != 0; break; 298 case DW_TAG_structure_type: add_type = (type_mask & eTypeClassStruct ) != 0; break; 299 case DW_TAG_union_type: add_type = (type_mask & eTypeClassUnion ) != 0; break; 300 case DW_TAG_enumeration_type: add_type = (type_mask & eTypeClassEnumeration ) != 0; break; 301 case DW_TAG_subroutine_type: 302 case DW_TAG_subprogram: 303 case DW_TAG_inlined_subroutine: add_type = (type_mask & eTypeClassFunction ) != 0; break; 304 case DW_TAG_pointer_type: add_type = (type_mask & eTypeClassPointer ) != 0; break; 305 case DW_TAG_rvalue_reference_type: 306 case DW_TAG_reference_type: add_type = (type_mask & eTypeClassReference ) != 0; break; 307 case DW_TAG_typedef: add_type = (type_mask & eTypeClassTypedef ) != 0; break; 308 case DW_TAG_ptr_to_member_type: add_type = (type_mask & eTypeClassMemberPointer ) != 0; break; 309 } 310 311 if (add_type) 312 { 313 const bool assert_not_being_parsed = true; 314 Type *type = ResolveTypeUID (cu, die, assert_not_being_parsed); 315 if (type) 316 { 317 if (type_set.find(type) == type_set.end()) 318 type_set.insert(type); 319 } 320 } 321 } 322 323 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); 324 child_die != NULL; 325 child_die = child_die->GetSibling()) 326 { 327 GetTypes (cu, child_die, min_die_offset, max_die_offset, type_mask, type_set); 328 } 329 } 330 } 331 } 332 333 size_t 334 SymbolFileDWARF::GetTypes (SymbolContextScope *sc_scope, 335 uint32_t type_mask, 336 TypeList &type_list) 337 338 { 339 TypeSet type_set; 340 341 CompileUnit *comp_unit = NULL; 342 DWARFCompileUnit* dwarf_cu = NULL; 343 if (sc_scope) 344 comp_unit = sc_scope->CalculateSymbolContextCompileUnit(); 345 346 if (comp_unit) 347 { 348 dwarf_cu = GetDWARFCompileUnit(comp_unit); 349 if (dwarf_cu == 0) 350 return 0; 351 GetTypes (dwarf_cu, 352 dwarf_cu->DIE(), 353 dwarf_cu->GetOffset(), 354 dwarf_cu->GetNextCompileUnitOffset(), 355 type_mask, 356 type_set); 357 } 358 else 359 { 360 DWARFDebugInfo* info = DebugInfo(); 361 if (info) 362 { 363 const size_t num_cus = info->GetNumCompileUnits(); 364 for (size_t cu_idx=0; cu_idx<num_cus; ++cu_idx) 365 { 366 dwarf_cu = info->GetCompileUnitAtIndex(cu_idx); 367 if (dwarf_cu) 368 { 369 GetTypes (dwarf_cu, 370 dwarf_cu->DIE(), 371 0, 372 UINT32_MAX, 373 type_mask, 374 type_set); 375 } 376 } 377 } 378 } 379 // if (m_using_apple_tables) 380 // { 381 // DWARFMappedHash::MemoryTable *apple_types = m_apple_types_ap.get(); 382 // if (apple_types) 383 // { 384 // apple_types->ForEach([this, &type_set, apple_types, type_mask](const DWARFMappedHash::DIEInfoArray &die_info_array) -> bool { 385 // 386 // for (auto die_info: die_info_array) 387 // { 388 // bool add_type = TagMatchesTypeMask (type_mask, 0); 389 // if (!add_type) 390 // { 391 // dw_tag_t tag = die_info.tag; 392 // if (tag == 0) 393 // { 394 // const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_info.offset, NULL); 395 // tag = die->Tag(); 396 // } 397 // add_type = TagMatchesTypeMask (type_mask, tag); 398 // } 399 // if (add_type) 400 // { 401 // Type *type = ResolveTypeUID(die_info.offset); 402 // 403 // if (type_set.find(type) == type_set.end()) 404 // type_set.insert(type); 405 // } 406 // } 407 // return true; // Keep iterating 408 // }); 409 // } 410 // } 411 // else 412 // { 413 // if (!m_indexed) 414 // Index (); 415 // 416 // m_type_index.ForEach([this, &type_set, type_mask](const char *name, uint32_t die_offset) -> bool { 417 // 418 // bool add_type = TagMatchesTypeMask (type_mask, 0); 419 // 420 // if (!add_type) 421 // { 422 // const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtr(die_offset, NULL); 423 // if (die) 424 // { 425 // const dw_tag_t tag = die->Tag(); 426 // add_type = TagMatchesTypeMask (type_mask, tag); 427 // } 428 // } 429 // 430 // if (add_type) 431 // { 432 // Type *type = ResolveTypeUID(die_offset); 433 // 434 // if (type_set.find(type) == type_set.end()) 435 // type_set.insert(type); 436 // } 437 // return true; // Keep iterating 438 // }); 439 // } 440 441 std::set<ClangASTType> clang_type_set; 442 size_t num_types_added = 0; 443 for (Type *type : type_set) 444 { 445 ClangASTType clang_type = type->GetClangForwardType(); 446 if (clang_type_set.find(clang_type) == clang_type_set.end()) 447 { 448 clang_type_set.insert(clang_type); 449 type_list.Insert (type->shared_from_this()); 450 ++num_types_added; 451 } 452 } 453 return num_types_added; 454 } 455 456 457 //---------------------------------------------------------------------- 458 // Gets the first parent that is a lexical block, function or inlined 459 // subroutine, or compile unit. 460 //---------------------------------------------------------------------- 461 static const DWARFDebugInfoEntry * 462 GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die) 463 { 464 const DWARFDebugInfoEntry *die; 465 for (die = child_die->GetParent(); die != NULL; die = die->GetParent()) 466 { 467 dw_tag_t tag = die->Tag(); 468 469 switch (tag) 470 { 471 case DW_TAG_compile_unit: 472 case DW_TAG_subprogram: 473 case DW_TAG_inlined_subroutine: 474 case DW_TAG_lexical_block: 475 return die; 476 } 477 } 478 return NULL; 479 } 480 481 482 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) : 483 SymbolFile (objfile), 484 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID 485 m_debug_map_module_wp (), 486 m_debug_map_symfile (NULL), 487 m_clang_tu_decl (NULL), 488 m_flags(), 489 m_data_debug_abbrev (), 490 m_data_debug_aranges (), 491 m_data_debug_frame (), 492 m_data_debug_info (), 493 m_data_debug_line (), 494 m_data_debug_loc (), 495 m_data_debug_ranges (), 496 m_data_debug_str (), 497 m_data_apple_names (), 498 m_data_apple_types (), 499 m_data_apple_namespaces (), 500 m_abbr(), 501 m_info(), 502 m_line(), 503 m_apple_names_ap (), 504 m_apple_types_ap (), 505 m_apple_namespaces_ap (), 506 m_apple_objc_ap (), 507 m_function_basename_index(), 508 m_function_fullname_index(), 509 m_function_method_index(), 510 m_function_selector_index(), 511 m_objc_class_selectors_index(), 512 m_global_index(), 513 m_type_index(), 514 m_namespace_index(), 515 m_indexed (false), 516 m_is_external_ast_source (false), 517 m_using_apple_tables (false), 518 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate), 519 m_ranges(), 520 m_unique_ast_type_map () 521 { 522 } 523 524 SymbolFileDWARF::~SymbolFileDWARF() 525 { 526 if (m_is_external_ast_source) 527 { 528 ModuleSP module_sp (m_obj_file->GetModule()); 529 if (module_sp) 530 module_sp->GetClangASTContext().RemoveExternalSource (); 531 } 532 } 533 534 static const ConstString & 535 GetDWARFMachOSegmentName () 536 { 537 static ConstString g_dwarf_section_name ("__DWARF"); 538 return g_dwarf_section_name; 539 } 540 541 UniqueDWARFASTTypeMap & 542 SymbolFileDWARF::GetUniqueDWARFASTTypeMap () 543 { 544 if (GetDebugMapSymfile ()) 545 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap (); 546 return m_unique_ast_type_map; 547 } 548 549 ClangASTContext & 550 SymbolFileDWARF::GetClangASTContext () 551 { 552 if (GetDebugMapSymfile ()) 553 return m_debug_map_symfile->GetClangASTContext (); 554 555 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext(); 556 if (!m_is_external_ast_source) 557 { 558 m_is_external_ast_source = true; 559 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap ( 560 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl, 561 SymbolFileDWARF::CompleteObjCInterfaceDecl, 562 SymbolFileDWARF::FindExternalVisibleDeclsByName, 563 SymbolFileDWARF::LayoutRecordType, 564 this)); 565 ast.SetExternalSource (ast_source_ap); 566 } 567 return ast; 568 } 569 570 void 571 SymbolFileDWARF::InitializeObject() 572 { 573 // Install our external AST source callbacks so we can complete Clang types. 574 ModuleSP module_sp (m_obj_file->GetModule()); 575 if (module_sp) 576 { 577 const SectionList *section_list = module_sp->GetSectionList(); 578 579 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 580 581 // Memory map the DWARF mach-o segment so we have everything mmap'ed 582 // to keep our heap memory usage down. 583 if (section) 584 m_obj_file->MemoryMapSectionData(section, m_dwarf_data); 585 } 586 get_apple_names_data(); 587 if (m_data_apple_names.GetByteSize() > 0) 588 { 589 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names")); 590 if (m_apple_names_ap->IsValid()) 591 m_using_apple_tables = true; 592 else 593 m_apple_names_ap.reset(); 594 } 595 get_apple_types_data(); 596 if (m_data_apple_types.GetByteSize() > 0) 597 { 598 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types")); 599 if (m_apple_types_ap->IsValid()) 600 m_using_apple_tables = true; 601 else 602 m_apple_types_ap.reset(); 603 } 604 605 get_apple_namespaces_data(); 606 if (m_data_apple_namespaces.GetByteSize() > 0) 607 { 608 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces")); 609 if (m_apple_namespaces_ap->IsValid()) 610 m_using_apple_tables = true; 611 else 612 m_apple_namespaces_ap.reset(); 613 } 614 615 get_apple_objc_data(); 616 if (m_data_apple_objc.GetByteSize() > 0) 617 { 618 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc")); 619 if (m_apple_objc_ap->IsValid()) 620 m_using_apple_tables = true; 621 else 622 m_apple_objc_ap.reset(); 623 } 624 } 625 626 bool 627 SymbolFileDWARF::SupportedVersion(uint16_t version) 628 { 629 return version == 2 || version == 3 || version == 4; 630 } 631 632 uint32_t 633 SymbolFileDWARF::CalculateAbilities () 634 { 635 uint32_t abilities = 0; 636 if (m_obj_file != NULL) 637 { 638 const Section* section = NULL; 639 const SectionList *section_list = m_obj_file->GetSectionList(); 640 if (section_list == NULL) 641 return 0; 642 643 uint64_t debug_abbrev_file_size = 0; 644 uint64_t debug_info_file_size = 0; 645 uint64_t debug_line_file_size = 0; 646 647 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 648 649 if (section) 650 section_list = §ion->GetChildren (); 651 652 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get(); 653 if (section != NULL) 654 { 655 debug_info_file_size = section->GetFileSize(); 656 657 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get(); 658 if (section) 659 debug_abbrev_file_size = section->GetFileSize(); 660 else 661 m_flags.Set (flagsGotDebugAbbrevData); 662 663 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get(); 664 if (!section) 665 m_flags.Set (flagsGotDebugArangesData); 666 667 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get(); 668 if (!section) 669 m_flags.Set (flagsGotDebugFrameData); 670 671 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get(); 672 if (section) 673 debug_line_file_size = section->GetFileSize(); 674 else 675 m_flags.Set (flagsGotDebugLineData); 676 677 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get(); 678 if (!section) 679 m_flags.Set (flagsGotDebugLocData); 680 681 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get(); 682 if (!section) 683 m_flags.Set (flagsGotDebugMacInfoData); 684 685 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get(); 686 if (!section) 687 m_flags.Set (flagsGotDebugPubNamesData); 688 689 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get(); 690 if (!section) 691 m_flags.Set (flagsGotDebugPubTypesData); 692 693 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get(); 694 if (!section) 695 m_flags.Set (flagsGotDebugRangesData); 696 697 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get(); 698 if (!section) 699 m_flags.Set (flagsGotDebugStrData); 700 } 701 else 702 { 703 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString(); 704 if (symfile_dir_cstr) 705 { 706 if (strcasestr(symfile_dir_cstr, ".dsym")) 707 { 708 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) 709 { 710 // We have a dSYM file that didn't have a any debug info. 711 // If the string table has a size of 1, then it was made from 712 // an executable with no debug info, or from an executable that 713 // was stripped. 714 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get(); 715 if (section && section->GetFileSize() == 1) 716 { 717 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info."); 718 } 719 } 720 } 721 } 722 } 723 724 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0) 725 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes; 726 727 if (debug_line_file_size > 0) 728 abilities |= LineTables; 729 } 730 return abilities; 731 } 732 733 const DWARFDataExtractor& 734 SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DWARFDataExtractor &data) 735 { 736 if (m_flags.IsClear (got_flag)) 737 { 738 ModuleSP module_sp (m_obj_file->GetModule()); 739 m_flags.Set (got_flag); 740 const SectionList *section_list = module_sp->GetSectionList(); 741 if (section_list) 742 { 743 SectionSP section_sp (section_list->FindSectionByType(sect_type, true)); 744 if (section_sp) 745 { 746 // See if we memory mapped the DWARF segment? 747 if (m_dwarf_data.GetByteSize()) 748 { 749 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize()); 750 } 751 else 752 { 753 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0) 754 data.Clear(); 755 } 756 } 757 } 758 } 759 return data; 760 } 761 762 const DWARFDataExtractor& 763 SymbolFileDWARF::get_debug_abbrev_data() 764 { 765 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev); 766 } 767 768 const DWARFDataExtractor& 769 SymbolFileDWARF::get_debug_aranges_data() 770 { 771 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges); 772 } 773 774 const DWARFDataExtractor& 775 SymbolFileDWARF::get_debug_frame_data() 776 { 777 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame); 778 } 779 780 const DWARFDataExtractor& 781 SymbolFileDWARF::get_debug_info_data() 782 { 783 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info); 784 } 785 786 const DWARFDataExtractor& 787 SymbolFileDWARF::get_debug_line_data() 788 { 789 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line); 790 } 791 792 const DWARFDataExtractor& 793 SymbolFileDWARF::get_debug_loc_data() 794 { 795 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc); 796 } 797 798 const DWARFDataExtractor& 799 SymbolFileDWARF::get_debug_ranges_data() 800 { 801 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges); 802 } 803 804 const DWARFDataExtractor& 805 SymbolFileDWARF::get_debug_str_data() 806 { 807 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str); 808 } 809 810 const DWARFDataExtractor& 811 SymbolFileDWARF::get_apple_names_data() 812 { 813 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names); 814 } 815 816 const DWARFDataExtractor& 817 SymbolFileDWARF::get_apple_types_data() 818 { 819 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types); 820 } 821 822 const DWARFDataExtractor& 823 SymbolFileDWARF::get_apple_namespaces_data() 824 { 825 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces); 826 } 827 828 const DWARFDataExtractor& 829 SymbolFileDWARF::get_apple_objc_data() 830 { 831 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc); 832 } 833 834 835 DWARFDebugAbbrev* 836 SymbolFileDWARF::DebugAbbrev() 837 { 838 if (m_abbr.get() == NULL) 839 { 840 const DWARFDataExtractor &debug_abbrev_data = get_debug_abbrev_data(); 841 if (debug_abbrev_data.GetByteSize() > 0) 842 { 843 m_abbr.reset(new DWARFDebugAbbrev()); 844 if (m_abbr.get()) 845 m_abbr->Parse(debug_abbrev_data); 846 } 847 } 848 return m_abbr.get(); 849 } 850 851 const DWARFDebugAbbrev* 852 SymbolFileDWARF::DebugAbbrev() const 853 { 854 return m_abbr.get(); 855 } 856 857 858 DWARFDebugInfo* 859 SymbolFileDWARF::DebugInfo() 860 { 861 if (m_info.get() == NULL) 862 { 863 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", 864 __PRETTY_FUNCTION__, static_cast<void*>(this)); 865 if (get_debug_info_data().GetByteSize() > 0) 866 { 867 m_info.reset(new DWARFDebugInfo()); 868 if (m_info.get()) 869 { 870 m_info->SetDwarfData(this); 871 } 872 } 873 } 874 return m_info.get(); 875 } 876 877 const DWARFDebugInfo* 878 SymbolFileDWARF::DebugInfo() const 879 { 880 return m_info.get(); 881 } 882 883 DWARFCompileUnit* 884 SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) 885 { 886 DWARFDebugInfo* info = DebugInfo(); 887 if (info) 888 { 889 if (GetDebugMapSymfile ()) 890 { 891 // The debug map symbol file made the compile units for this DWARF 892 // file which is .o file with DWARF in it, and we should have 893 // only 1 compile unit which is at offset zero in the DWARF. 894 // TODO: modify to support LTO .o files where each .o file might 895 // have multiple DW_TAG_compile_unit tags. 896 897 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit(0).get(); 898 if (dwarf_cu && dwarf_cu->GetUserData() == NULL) 899 dwarf_cu->SetUserData(comp_unit); 900 return dwarf_cu; 901 } 902 else 903 { 904 // Just a normal DWARF file whose user ID for the compile unit is 905 // the DWARF offset itself 906 907 DWARFCompileUnit *dwarf_cu = info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get(); 908 if (dwarf_cu && dwarf_cu->GetUserData() == NULL) 909 dwarf_cu->SetUserData(comp_unit); 910 return dwarf_cu; 911 912 } 913 } 914 return NULL; 915 } 916 917 918 DWARFDebugRanges* 919 SymbolFileDWARF::DebugRanges() 920 { 921 if (m_ranges.get() == NULL) 922 { 923 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", 924 __PRETTY_FUNCTION__, static_cast<void*>(this)); 925 if (get_debug_ranges_data().GetByteSize() > 0) 926 { 927 m_ranges.reset(new DWARFDebugRanges()); 928 if (m_ranges.get()) 929 m_ranges->Extract(this); 930 } 931 } 932 return m_ranges.get(); 933 } 934 935 const DWARFDebugRanges* 936 SymbolFileDWARF::DebugRanges() const 937 { 938 return m_ranges.get(); 939 } 940 941 lldb::CompUnitSP 942 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx) 943 { 944 CompUnitSP cu_sp; 945 if (dwarf_cu) 946 { 947 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData(); 948 if (comp_unit) 949 { 950 // We already parsed this compile unit, had out a shared pointer to it 951 cu_sp = comp_unit->shared_from_this(); 952 } 953 else 954 { 955 if (GetDebugMapSymfile ()) 956 { 957 // Let the debug map create the compile unit 958 cu_sp = m_debug_map_symfile->GetCompileUnit(this); 959 dwarf_cu->SetUserData(cu_sp.get()); 960 } 961 else 962 { 963 ModuleSP module_sp (m_obj_file->GetModule()); 964 if (module_sp) 965 { 966 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly (); 967 if (cu_die) 968 { 969 const char * cu_die_name = cu_die->GetName(this, dwarf_cu); 970 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL); 971 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0); 972 if (cu_die_name) 973 { 974 std::string ramapped_file; 975 FileSpec cu_file_spec; 976 977 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0') 978 { 979 // If we have a full path to the compile unit, we don't need to resolve 980 // the file. This can be expensive e.g. when the source files are NFS mounted. 981 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file)) 982 cu_file_spec.SetFile (ramapped_file.c_str(), false); 983 else 984 cu_file_spec.SetFile (cu_die_name, false); 985 } 986 else 987 { 988 // DWARF2/3 suggests the form hostname:pathname for compilation directory. 989 // Remove the host part if present. 990 cu_comp_dir = removeHostnameFromPathname(cu_comp_dir); 991 std::string fullpath(cu_comp_dir); 992 993 if (*fullpath.rbegin() != '/') 994 fullpath += '/'; 995 fullpath += cu_die_name; 996 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file)) 997 cu_file_spec.SetFile (ramapped_file.c_str(), false); 998 else 999 cu_file_spec.SetFile (fullpath.c_str(), false); 1000 } 1001 1002 cu_sp.reset(new CompileUnit (module_sp, 1003 dwarf_cu, 1004 cu_file_spec, 1005 MakeUserID(dwarf_cu->GetOffset()), 1006 cu_language)); 1007 if (cu_sp) 1008 { 1009 dwarf_cu->SetUserData(cu_sp.get()); 1010 1011 // Figure out the compile unit index if we weren't given one 1012 if (cu_idx == UINT32_MAX) 1013 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx); 1014 1015 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp); 1016 } 1017 } 1018 } 1019 } 1020 } 1021 } 1022 } 1023 return cu_sp; 1024 } 1025 1026 uint32_t 1027 SymbolFileDWARF::GetNumCompileUnits() 1028 { 1029 DWARFDebugInfo* info = DebugInfo(); 1030 if (info) 1031 return info->GetNumCompileUnits(); 1032 return 0; 1033 } 1034 1035 CompUnitSP 1036 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) 1037 { 1038 CompUnitSP cu_sp; 1039 DWARFDebugInfo* info = DebugInfo(); 1040 if (info) 1041 { 1042 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx); 1043 if (dwarf_cu) 1044 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx); 1045 } 1046 return cu_sp; 1047 } 1048 1049 Function * 1050 SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die) 1051 { 1052 DWARFDebugRanges::RangeList func_ranges; 1053 const char *name = NULL; 1054 const char *mangled = NULL; 1055 int decl_file = 0; 1056 int decl_line = 0; 1057 int decl_column = 0; 1058 int call_file = 0; 1059 int call_line = 0; 1060 int call_column = 0; 1061 DWARFExpression frame_base; 1062 1063 assert (die->Tag() == DW_TAG_subprogram); 1064 1065 if (die->Tag() != DW_TAG_subprogram) 1066 return NULL; 1067 1068 if (die->GetDIENamesAndRanges (this, 1069 dwarf_cu, 1070 name, 1071 mangled, 1072 func_ranges, 1073 decl_file, 1074 decl_line, 1075 decl_column, 1076 call_file, 1077 call_line, 1078 call_column, 1079 &frame_base)) 1080 { 1081 // Union of all ranges in the function DIE (if the function is discontiguous) 1082 AddressRange func_range; 1083 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0); 1084 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0); 1085 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr) 1086 { 1087 ModuleSP module_sp (m_obj_file->GetModule()); 1088 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList()); 1089 if (func_range.GetBaseAddress().IsValid()) 1090 func_range.SetByteSize(highest_func_addr - lowest_func_addr); 1091 } 1092 1093 if (func_range.GetBaseAddress().IsValid()) 1094 { 1095 Mangled func_name; 1096 if (mangled) 1097 func_name.SetValue(ConstString(mangled), true); 1098 else if (name) 1099 func_name.SetValue(ConstString(name), false); 1100 1101 FunctionSP func_sp; 1102 std::unique_ptr<Declaration> decl_ap; 1103 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1104 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 1105 decl_line, 1106 decl_column)); 1107 1108 // Supply the type _only_ if it has already been parsed 1109 Type *func_type = m_die_to_type.lookup (die); 1110 1111 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED); 1112 1113 if (FixupAddress (func_range.GetBaseAddress())) 1114 { 1115 const user_id_t func_user_id = MakeUserID(die->GetOffset()); 1116 func_sp.reset(new Function (sc.comp_unit, 1117 MakeUserID(func_user_id), // UserID is the DIE offset 1118 MakeUserID(func_user_id), 1119 func_name, 1120 func_type, 1121 func_range)); // first address range 1122 1123 if (func_sp.get() != NULL) 1124 { 1125 if (frame_base.IsValid()) 1126 func_sp->GetFrameBaseExpression() = frame_base; 1127 sc.comp_unit->AddFunction(func_sp); 1128 return func_sp.get(); 1129 } 1130 } 1131 } 1132 } 1133 return NULL; 1134 } 1135 1136 bool 1137 SymbolFileDWARF::FixupAddress (Address &addr) 1138 { 1139 SymbolFileDWARFDebugMap * debug_map_symfile = GetDebugMapSymfile (); 1140 if (debug_map_symfile) 1141 { 1142 return debug_map_symfile->LinkOSOAddress(addr); 1143 } 1144 // This is a normal DWARF file, no address fixups need to happen 1145 return true; 1146 } 1147 lldb::LanguageType 1148 SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc) 1149 { 1150 assert (sc.comp_unit); 1151 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1152 if (dwarf_cu) 1153 { 1154 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly(); 1155 if (die) 1156 { 1157 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0); 1158 if (language) 1159 return (lldb::LanguageType)language; 1160 } 1161 } 1162 return eLanguageTypeUnknown; 1163 } 1164 1165 size_t 1166 SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) 1167 { 1168 assert (sc.comp_unit); 1169 size_t functions_added = 0; 1170 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1171 if (dwarf_cu) 1172 { 1173 DWARFDIECollection function_dies; 1174 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies); 1175 size_t func_idx; 1176 for (func_idx = 0; func_idx < num_functions; ++func_idx) 1177 { 1178 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx); 1179 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL) 1180 { 1181 if (ParseCompileUnitFunction(sc, dwarf_cu, die)) 1182 ++functions_added; 1183 } 1184 } 1185 //FixupTypes(); 1186 } 1187 return functions_added; 1188 } 1189 1190 bool 1191 SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files) 1192 { 1193 assert (sc.comp_unit); 1194 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1195 if (dwarf_cu) 1196 { 1197 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 1198 1199 if (cu_die) 1200 { 1201 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL); 1202 1203 // DWARF2/3 suggests the form hostname:pathname for compilation directory. 1204 // Remove the host part if present. 1205 cu_comp_dir = removeHostnameFromPathname(cu_comp_dir); 1206 1207 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 1208 1209 // All file indexes in DWARF are one based and a file of index zero is 1210 // supposed to be the compile unit itself. 1211 support_files.Append (*sc.comp_unit); 1212 1213 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files); 1214 } 1215 } 1216 return false; 1217 } 1218 1219 struct ParseDWARFLineTableCallbackInfo 1220 { 1221 LineTable* line_table; 1222 std::unique_ptr<LineSequence> sequence_ap; 1223 }; 1224 1225 //---------------------------------------------------------------------- 1226 // ParseStatementTableCallback 1227 //---------------------------------------------------------------------- 1228 static void 1229 ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData) 1230 { 1231 if (state.row == DWARFDebugLine::State::StartParsingLineTable) 1232 { 1233 // Just started parsing the line table 1234 } 1235 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) 1236 { 1237 // Done parsing line table, nothing to do for the cleanup 1238 } 1239 else 1240 { 1241 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData; 1242 LineTable* line_table = info->line_table; 1243 1244 // If this is our first time here, we need to create a 1245 // sequence container. 1246 if (!info->sequence_ap.get()) 1247 { 1248 info->sequence_ap.reset(line_table->CreateLineSequenceContainer()); 1249 assert(info->sequence_ap.get()); 1250 } 1251 line_table->AppendLineEntryToSequence (info->sequence_ap.get(), 1252 state.address, 1253 state.line, 1254 state.column, 1255 state.file, 1256 state.is_stmt, 1257 state.basic_block, 1258 state.prologue_end, 1259 state.epilogue_begin, 1260 state.end_sequence); 1261 if (state.end_sequence) 1262 { 1263 // First, put the current sequence into the line table. 1264 line_table->InsertSequence(info->sequence_ap.get()); 1265 // Then, empty it to prepare for the next sequence. 1266 info->sequence_ap->Clear(); 1267 } 1268 } 1269 } 1270 1271 bool 1272 SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc) 1273 { 1274 assert (sc.comp_unit); 1275 if (sc.comp_unit->GetLineTable() != NULL) 1276 return true; 1277 1278 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 1279 if (dwarf_cu) 1280 { 1281 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 1282 if (dwarf_cu_die) 1283 { 1284 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 1285 if (cu_line_offset != DW_INVALID_OFFSET) 1286 { 1287 std::unique_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit)); 1288 if (line_table_ap.get()) 1289 { 1290 ParseDWARFLineTableCallbackInfo info; 1291 info.line_table = line_table_ap.get(); 1292 lldb::offset_t offset = cu_line_offset; 1293 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info); 1294 if (m_debug_map_symfile) 1295 { 1296 // We have an object file that has a line table with addresses 1297 // that are not linked. We need to link the line table and convert 1298 // the addresses that are relative to the .o file into addresses 1299 // for the main executable. 1300 sc.comp_unit->SetLineTable (m_debug_map_symfile->LinkOSOLineTable (this, line_table_ap.get())); 1301 } 1302 else 1303 { 1304 sc.comp_unit->SetLineTable(line_table_ap.release()); 1305 return true; 1306 } 1307 } 1308 } 1309 } 1310 } 1311 return false; 1312 } 1313 1314 size_t 1315 SymbolFileDWARF::ParseFunctionBlocks 1316 ( 1317 const SymbolContext& sc, 1318 Block *parent_block, 1319 DWARFCompileUnit* dwarf_cu, 1320 const DWARFDebugInfoEntry *die, 1321 addr_t subprogram_low_pc, 1322 uint32_t depth 1323 ) 1324 { 1325 size_t blocks_added = 0; 1326 while (die != NULL) 1327 { 1328 dw_tag_t tag = die->Tag(); 1329 1330 switch (tag) 1331 { 1332 case DW_TAG_inlined_subroutine: 1333 case DW_TAG_subprogram: 1334 case DW_TAG_lexical_block: 1335 { 1336 Block *block = NULL; 1337 if (tag == DW_TAG_subprogram) 1338 { 1339 // Skip any DW_TAG_subprogram DIEs that are inside 1340 // of a normal or inlined functions. These will be 1341 // parsed on their own as separate entities. 1342 1343 if (depth > 0) 1344 break; 1345 1346 block = parent_block; 1347 } 1348 else 1349 { 1350 BlockSP block_sp(new Block (MakeUserID(die->GetOffset()))); 1351 parent_block->AddChild(block_sp); 1352 block = block_sp.get(); 1353 } 1354 DWARFDebugRanges::RangeList ranges; 1355 const char *name = NULL; 1356 const char *mangled_name = NULL; 1357 1358 int decl_file = 0; 1359 int decl_line = 0; 1360 int decl_column = 0; 1361 int call_file = 0; 1362 int call_line = 0; 1363 int call_column = 0; 1364 if (die->GetDIENamesAndRanges (this, 1365 dwarf_cu, 1366 name, 1367 mangled_name, 1368 ranges, 1369 decl_file, decl_line, decl_column, 1370 call_file, call_line, call_column)) 1371 { 1372 if (tag == DW_TAG_subprogram) 1373 { 1374 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS); 1375 subprogram_low_pc = ranges.GetMinRangeBase(0); 1376 } 1377 else if (tag == DW_TAG_inlined_subroutine) 1378 { 1379 // We get called here for inlined subroutines in two ways. 1380 // The first time is when we are making the Function object 1381 // for this inlined concrete instance. Since we're creating a top level block at 1382 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to 1383 // adjust the containing address. 1384 // The second time is when we are parsing the blocks inside the function that contains 1385 // the inlined concrete instance. Since these will be blocks inside the containing "real" 1386 // function the offset will be for that function. 1387 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) 1388 { 1389 subprogram_low_pc = ranges.GetMinRangeBase(0); 1390 } 1391 } 1392 1393 const size_t num_ranges = ranges.GetSize(); 1394 for (size_t i = 0; i<num_ranges; ++i) 1395 { 1396 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i); 1397 const addr_t range_base = range.GetRangeBase(); 1398 if (range_base >= subprogram_low_pc) 1399 block->AddRange(Block::Range (range_base - subprogram_low_pc, range.GetByteSize())); 1400 else 1401 { 1402 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", 1403 block->GetID(), 1404 range_base, 1405 range.GetRangeEnd(), 1406 subprogram_low_pc); 1407 } 1408 } 1409 block->FinalizeRanges (); 1410 1411 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL)) 1412 { 1413 std::unique_ptr<Declaration> decl_ap; 1414 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1415 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 1416 decl_line, decl_column)); 1417 1418 std::unique_ptr<Declaration> call_ap; 1419 if (call_file != 0 || call_line != 0 || call_column != 0) 1420 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file), 1421 call_line, call_column)); 1422 1423 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get()); 1424 } 1425 1426 ++blocks_added; 1427 1428 if (die->HasChildren()) 1429 { 1430 blocks_added += ParseFunctionBlocks (sc, 1431 block, 1432 dwarf_cu, 1433 die->GetFirstChild(), 1434 subprogram_low_pc, 1435 depth + 1); 1436 } 1437 } 1438 } 1439 break; 1440 default: 1441 break; 1442 } 1443 1444 // Only parse siblings of the block if we are not at depth zero. A depth 1445 // of zero indicates we are currently parsing the top level 1446 // DW_TAG_subprogram DIE 1447 1448 if (depth == 0) 1449 die = NULL; 1450 else 1451 die = die->GetSibling(); 1452 } 1453 return blocks_added; 1454 } 1455 1456 bool 1457 SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu, 1458 const DWARFDebugInfoEntry *die, 1459 ClangASTContext::TemplateParameterInfos &template_param_infos) 1460 { 1461 const dw_tag_t tag = die->Tag(); 1462 1463 switch (tag) 1464 { 1465 case DW_TAG_template_type_parameter: 1466 case DW_TAG_template_value_parameter: 1467 { 1468 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64()); 1469 1470 DWARFDebugInfoEntry::Attributes attributes; 1471 const size_t num_attributes = die->GetAttributes (this, 1472 dwarf_cu, 1473 fixed_form_sizes, 1474 attributes); 1475 const char *name = NULL; 1476 Type *lldb_type = NULL; 1477 ClangASTType clang_type; 1478 uint64_t uval64 = 0; 1479 bool uval64_valid = false; 1480 if (num_attributes > 0) 1481 { 1482 DWARFFormValue form_value; 1483 for (size_t i=0; i<num_attributes; ++i) 1484 { 1485 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1486 1487 switch (attr) 1488 { 1489 case DW_AT_name: 1490 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1491 name = form_value.AsCString(&get_debug_str_data()); 1492 break; 1493 1494 case DW_AT_type: 1495 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1496 { 1497 const dw_offset_t type_die_offset = form_value.Reference(); 1498 lldb_type = ResolveTypeUID(type_die_offset); 1499 if (lldb_type) 1500 clang_type = lldb_type->GetClangForwardType(); 1501 } 1502 break; 1503 1504 case DW_AT_const_value: 1505 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1506 { 1507 uval64_valid = true; 1508 uval64 = form_value.Unsigned(); 1509 } 1510 break; 1511 default: 1512 break; 1513 } 1514 } 1515 1516 clang::ASTContext *ast = GetClangASTContext().getASTContext(); 1517 if (!clang_type) 1518 clang_type = GetClangASTContext().GetBasicType(eBasicTypeVoid); 1519 1520 if (clang_type) 1521 { 1522 bool is_signed = false; 1523 if (name && name[0]) 1524 template_param_infos.names.push_back(name); 1525 else 1526 template_param_infos.names.push_back(NULL); 1527 1528 if (tag == DW_TAG_template_value_parameter && 1529 lldb_type != NULL && 1530 clang_type.IsIntegerType (is_signed) && 1531 uval64_valid) 1532 { 1533 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed); 1534 template_param_infos.args.push_back (clang::TemplateArgument (*ast, 1535 llvm::APSInt(apint), 1536 clang_type.GetQualType())); 1537 } 1538 else 1539 { 1540 template_param_infos.args.push_back (clang::TemplateArgument (clang_type.GetQualType())); 1541 } 1542 } 1543 else 1544 { 1545 return false; 1546 } 1547 1548 } 1549 } 1550 return true; 1551 1552 default: 1553 break; 1554 } 1555 return false; 1556 } 1557 1558 bool 1559 SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu, 1560 const DWARFDebugInfoEntry *parent_die, 1561 ClangASTContext::TemplateParameterInfos &template_param_infos) 1562 { 1563 1564 if (parent_die == NULL) 1565 return false; 1566 1567 Args template_parameter_names; 1568 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); 1569 die != NULL; 1570 die = die->GetSibling()) 1571 { 1572 const dw_tag_t tag = die->Tag(); 1573 1574 switch (tag) 1575 { 1576 case DW_TAG_template_type_parameter: 1577 case DW_TAG_template_value_parameter: 1578 ParseTemplateDIE (dwarf_cu, die, template_param_infos); 1579 break; 1580 1581 default: 1582 break; 1583 } 1584 } 1585 if (template_param_infos.args.empty()) 1586 return false; 1587 return template_param_infos.args.size() == template_param_infos.names.size(); 1588 } 1589 1590 clang::ClassTemplateDecl * 1591 SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx, 1592 lldb::AccessType access_type, 1593 const char *parent_name, 1594 int tag_decl_kind, 1595 const ClangASTContext::TemplateParameterInfos &template_param_infos) 1596 { 1597 if (template_param_infos.IsValid()) 1598 { 1599 std::string template_basename(parent_name); 1600 template_basename.erase (template_basename.find('<')); 1601 ClangASTContext &ast = GetClangASTContext(); 1602 1603 return ast.CreateClassTemplateDecl (decl_ctx, 1604 access_type, 1605 template_basename.c_str(), 1606 tag_decl_kind, 1607 template_param_infos); 1608 } 1609 return NULL; 1610 } 1611 1612 class SymbolFileDWARF::DelayedAddObjCClassProperty 1613 { 1614 public: 1615 DelayedAddObjCClassProperty 1616 ( 1617 const ClangASTType &class_opaque_type, 1618 const char *property_name, 1619 const ClangASTType &property_opaque_type, // The property type is only required if you don't have an ivar decl 1620 clang::ObjCIvarDecl *ivar_decl, 1621 const char *property_setter_name, 1622 const char *property_getter_name, 1623 uint32_t property_attributes, 1624 const ClangASTMetadata *metadata 1625 ) : 1626 m_class_opaque_type (class_opaque_type), 1627 m_property_name (property_name), 1628 m_property_opaque_type (property_opaque_type), 1629 m_ivar_decl (ivar_decl), 1630 m_property_setter_name (property_setter_name), 1631 m_property_getter_name (property_getter_name), 1632 m_property_attributes (property_attributes) 1633 { 1634 if (metadata != NULL) 1635 { 1636 m_metadata_ap.reset(new ClangASTMetadata()); 1637 *m_metadata_ap = *metadata; 1638 } 1639 } 1640 1641 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs) 1642 { 1643 *this = rhs; 1644 } 1645 1646 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs) 1647 { 1648 m_class_opaque_type = rhs.m_class_opaque_type; 1649 m_property_name = rhs.m_property_name; 1650 m_property_opaque_type = rhs.m_property_opaque_type; 1651 m_ivar_decl = rhs.m_ivar_decl; 1652 m_property_setter_name = rhs.m_property_setter_name; 1653 m_property_getter_name = rhs.m_property_getter_name; 1654 m_property_attributes = rhs.m_property_attributes; 1655 1656 if (rhs.m_metadata_ap.get()) 1657 { 1658 m_metadata_ap.reset (new ClangASTMetadata()); 1659 *m_metadata_ap = *rhs.m_metadata_ap; 1660 } 1661 return *this; 1662 } 1663 1664 bool 1665 Finalize() 1666 { 1667 return m_class_opaque_type.AddObjCClassProperty (m_property_name, 1668 m_property_opaque_type, 1669 m_ivar_decl, 1670 m_property_setter_name, 1671 m_property_getter_name, 1672 m_property_attributes, 1673 m_metadata_ap.get()); 1674 } 1675 private: 1676 ClangASTType m_class_opaque_type; 1677 const char *m_property_name; 1678 ClangASTType m_property_opaque_type; 1679 clang::ObjCIvarDecl *m_ivar_decl; 1680 const char *m_property_setter_name; 1681 const char *m_property_getter_name; 1682 uint32_t m_property_attributes; 1683 std::unique_ptr<ClangASTMetadata> m_metadata_ap; 1684 }; 1685 1686 struct BitfieldInfo 1687 { 1688 uint64_t bit_size; 1689 uint64_t bit_offset; 1690 1691 BitfieldInfo () : 1692 bit_size (LLDB_INVALID_ADDRESS), 1693 bit_offset (LLDB_INVALID_ADDRESS) 1694 { 1695 } 1696 1697 void 1698 Clear() 1699 { 1700 bit_size = LLDB_INVALID_ADDRESS; 1701 bit_offset = LLDB_INVALID_ADDRESS; 1702 } 1703 1704 bool IsValid () 1705 { 1706 return (bit_size != LLDB_INVALID_ADDRESS) && 1707 (bit_offset != LLDB_INVALID_ADDRESS); 1708 } 1709 }; 1710 1711 1712 bool 1713 SymbolFileDWARF::ClassOrStructIsVirtual (DWARFCompileUnit* dwarf_cu, 1714 const DWARFDebugInfoEntry *parent_die) 1715 { 1716 if (parent_die) 1717 { 1718 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 1719 { 1720 dw_tag_t tag = die->Tag(); 1721 bool check_virtuality = false; 1722 switch (tag) 1723 { 1724 case DW_TAG_inheritance: 1725 case DW_TAG_subprogram: 1726 check_virtuality = true; 1727 break; 1728 default: 1729 break; 1730 } 1731 if (check_virtuality) 1732 { 1733 if (die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_virtuality, 0) != 0) 1734 return true; 1735 } 1736 } 1737 } 1738 return false; 1739 } 1740 1741 size_t 1742 SymbolFileDWARF::ParseChildMembers 1743 ( 1744 const SymbolContext& sc, 1745 DWARFCompileUnit* dwarf_cu, 1746 const DWARFDebugInfoEntry *parent_die, 1747 ClangASTType &class_clang_type, 1748 const LanguageType class_language, 1749 std::vector<clang::CXXBaseSpecifier *>& base_classes, 1750 std::vector<int>& member_accessibilities, 1751 DWARFDIECollection& member_function_dies, 1752 DelayedPropertyList& delayed_properties, 1753 AccessType& default_accessibility, 1754 bool &is_a_class, 1755 LayoutInfo &layout_info 1756 ) 1757 { 1758 if (parent_die == NULL) 1759 return 0; 1760 1761 size_t count = 0; 1762 const DWARFDebugInfoEntry *die; 1763 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64()); 1764 uint32_t member_idx = 0; 1765 BitfieldInfo last_field_info; 1766 ModuleSP module = GetObjectFile()->GetModule(); 1767 1768 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 1769 { 1770 dw_tag_t tag = die->Tag(); 1771 1772 switch (tag) 1773 { 1774 case DW_TAG_member: 1775 case DW_TAG_APPLE_property: 1776 { 1777 DWARFDebugInfoEntry::Attributes attributes; 1778 const size_t num_attributes = die->GetAttributes (this, 1779 dwarf_cu, 1780 fixed_form_sizes, 1781 attributes); 1782 if (num_attributes > 0) 1783 { 1784 Declaration decl; 1785 //DWARFExpression location; 1786 const char *name = NULL; 1787 const char *prop_name = NULL; 1788 const char *prop_getter_name = NULL; 1789 const char *prop_setter_name = NULL; 1790 uint32_t prop_attributes = 0; 1791 1792 1793 bool is_artificial = false; 1794 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 1795 AccessType accessibility = eAccessNone; 1796 uint32_t member_byte_offset = UINT32_MAX; 1797 size_t byte_size = 0; 1798 size_t bit_offset = 0; 1799 size_t bit_size = 0; 1800 bool is_external = false; // On DW_TAG_members, this means the member is static 1801 uint32_t i; 1802 for (i=0; i<num_attributes && !is_artificial; ++i) 1803 { 1804 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1805 DWARFFormValue form_value; 1806 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1807 { 1808 switch (attr) 1809 { 1810 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1811 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1812 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1813 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 1814 case DW_AT_type: encoding_uid = form_value.Reference(); break; 1815 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break; 1816 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break; 1817 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 1818 case DW_AT_data_member_location: 1819 if (form_value.BlockData()) 1820 { 1821 Value initialValue(0); 1822 Value memberOffset(0); 1823 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 1824 uint32_t block_length = form_value.Unsigned(); 1825 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 1826 if (DWARFExpression::Evaluate(NULL, // ExecutionContext * 1827 NULL, // ClangExpressionVariableList * 1828 NULL, // ClangExpressionDeclMap * 1829 NULL, // RegisterContext * 1830 module, 1831 debug_info_data, 1832 block_offset, 1833 block_length, 1834 eRegisterKindDWARF, 1835 &initialValue, 1836 memberOffset, 1837 NULL)) 1838 { 1839 member_byte_offset = memberOffset.ResolveValue(NULL).UInt(); 1840 } 1841 } 1842 else 1843 { 1844 // With DWARF 3 and later, if the value is an integer constant, 1845 // this form value is the offset in bytes from the beginning 1846 // of the containing entity. 1847 member_byte_offset = form_value.Unsigned(); 1848 } 1849 break; 1850 1851 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break; 1852 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 1853 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break; 1854 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break; 1855 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break; 1856 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break; 1857 case DW_AT_external: is_external = form_value.Boolean(); break; 1858 1859 default: 1860 case DW_AT_declaration: 1861 case DW_AT_description: 1862 case DW_AT_mutable: 1863 case DW_AT_visibility: 1864 case DW_AT_sibling: 1865 break; 1866 } 1867 } 1868 } 1869 1870 if (prop_name) 1871 { 1872 ConstString fixed_getter; 1873 ConstString fixed_setter; 1874 1875 // Check if the property getter/setter were provided as full 1876 // names. We want basenames, so we extract them. 1877 1878 if (prop_getter_name && prop_getter_name[0] == '-') 1879 { 1880 ObjCLanguageRuntime::MethodName prop_getter_method(prop_getter_name, true); 1881 prop_getter_name = prop_getter_method.GetSelector().GetCString(); 1882 } 1883 1884 if (prop_setter_name && prop_setter_name[0] == '-') 1885 { 1886 ObjCLanguageRuntime::MethodName prop_setter_method(prop_setter_name, true); 1887 prop_setter_name = prop_setter_method.GetSelector().GetCString(); 1888 } 1889 1890 // If the names haven't been provided, they need to be 1891 // filled in. 1892 1893 if (!prop_getter_name) 1894 { 1895 prop_getter_name = prop_name; 1896 } 1897 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly)) 1898 { 1899 StreamString ss; 1900 1901 ss.Printf("set%c%s:", 1902 toupper(prop_name[0]), 1903 &prop_name[1]); 1904 1905 fixed_setter.SetCString(ss.GetData()); 1906 prop_setter_name = fixed_setter.GetCString(); 1907 } 1908 } 1909 1910 // Clang has a DWARF generation bug where sometimes it 1911 // represents fields that are references with bad byte size 1912 // and bit size/offset information such as: 1913 // 1914 // DW_AT_byte_size( 0x00 ) 1915 // DW_AT_bit_size( 0x40 ) 1916 // DW_AT_bit_offset( 0xffffffffffffffc0 ) 1917 // 1918 // So check the bit offset to make sure it is sane, and if 1919 // the values are not sane, remove them. If we don't do this 1920 // then we will end up with a crash if we try to use this 1921 // type in an expression when clang becomes unhappy with its 1922 // recycled debug info. 1923 1924 if (bit_offset > 128) 1925 { 1926 bit_size = 0; 1927 bit_offset = 0; 1928 } 1929 1930 // FIXME: Make Clang ignore Objective-C accessibility for expressions 1931 if (class_language == eLanguageTypeObjC || 1932 class_language == eLanguageTypeObjC_plus_plus) 1933 accessibility = eAccessNone; 1934 1935 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name)) 1936 { 1937 // Not all compilers will mark the vtable pointer 1938 // member as artificial (llvm-gcc). We can't have 1939 // the virtual members in our classes otherwise it 1940 // throws off all child offsets since we end up 1941 // having and extra pointer sized member in our 1942 // class layouts. 1943 is_artificial = true; 1944 } 1945 1946 // Handle static members 1947 if (is_external && member_byte_offset == UINT32_MAX) 1948 { 1949 Type *var_type = ResolveTypeUID(encoding_uid); 1950 1951 if (var_type) 1952 { 1953 if (accessibility == eAccessNone) 1954 accessibility = eAccessPublic; 1955 class_clang_type.AddVariableToRecordType (name, 1956 var_type->GetClangLayoutType(), 1957 accessibility); 1958 } 1959 break; 1960 } 1961 1962 if (is_artificial == false) 1963 { 1964 Type *member_type = ResolveTypeUID(encoding_uid); 1965 1966 clang::FieldDecl *field_decl = NULL; 1967 if (tag == DW_TAG_member) 1968 { 1969 if (member_type) 1970 { 1971 if (accessibility == eAccessNone) 1972 accessibility = default_accessibility; 1973 member_accessibilities.push_back(accessibility); 1974 1975 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8)); 1976 if (bit_size > 0) 1977 { 1978 1979 BitfieldInfo this_field_info; 1980 this_field_info.bit_offset = field_bit_offset; 1981 this_field_info.bit_size = bit_size; 1982 1983 ///////////////////////////////////////////////////////////// 1984 // How to locate a field given the DWARF debug information 1985 // 1986 // AT_byte_size indicates the size of the word in which the 1987 // bit offset must be interpreted. 1988 // 1989 // AT_data_member_location indicates the byte offset of the 1990 // word from the base address of the structure. 1991 // 1992 // AT_bit_offset indicates how many bits into the word 1993 // (according to the host endianness) the low-order bit of 1994 // the field starts. AT_bit_offset can be negative. 1995 // 1996 // AT_bit_size indicates the size of the field in bits. 1997 ///////////////////////////////////////////////////////////// 1998 1999 if (byte_size == 0) 2000 byte_size = member_type->GetByteSize(); 2001 2002 if (GetObjectFile()->GetByteOrder() == eByteOrderLittle) 2003 { 2004 this_field_info.bit_offset += byte_size * 8; 2005 this_field_info.bit_offset -= (bit_offset + bit_size); 2006 } 2007 else 2008 { 2009 this_field_info.bit_offset += bit_offset; 2010 } 2011 2012 // Update the field bit offset we will report for layout 2013 field_bit_offset = this_field_info.bit_offset; 2014 2015 // If the member to be emitted did not start on a character boundary and there is 2016 // empty space between the last field and this one, then we need to emit an 2017 // anonymous member filling up the space up to its start. There are three cases 2018 // here: 2019 // 2020 // 1 If the previous member ended on a character boundary, then we can emit an 2021 // anonymous member starting at the most recent character boundary. 2022 // 2023 // 2 If the previous member did not end on a character boundary and the distance 2024 // from the end of the previous member to the current member is less than a 2025 // word width, then we can emit an anonymous member starting right after the 2026 // previous member and right before this member. 2027 // 2028 // 3 If the previous member did not end on a character boundary and the distance 2029 // from the end of the previous member to the current member is greater than 2030 // or equal a word width, then we act as in Case 1. 2031 2032 const uint64_t character_width = 8; 2033 const uint64_t word_width = 32; 2034 2035 // Objective-C has invalid DW_AT_bit_offset values in older versions 2036 // of clang, so we have to be careful and only insert unnamed bitfields 2037 // if we have a new enough clang. 2038 bool detect_unnamed_bitfields = true; 2039 2040 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus) 2041 detect_unnamed_bitfields = dwarf_cu->Supports_unnamed_objc_bitfields (); 2042 2043 if (detect_unnamed_bitfields) 2044 { 2045 BitfieldInfo anon_field_info; 2046 2047 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned 2048 { 2049 uint64_t last_field_end = 0; 2050 2051 if (last_field_info.IsValid()) 2052 last_field_end = last_field_info.bit_offset + last_field_info.bit_size; 2053 2054 if (this_field_info.bit_offset != last_field_end) 2055 { 2056 if (((last_field_end % character_width) == 0) || // case 1 2057 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3 2058 { 2059 anon_field_info.bit_size = this_field_info.bit_offset % character_width; 2060 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size; 2061 } 2062 else // case 2 2063 { 2064 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end; 2065 anon_field_info.bit_offset = last_field_end; 2066 } 2067 } 2068 } 2069 2070 if (anon_field_info.IsValid()) 2071 { 2072 clang::FieldDecl *unnamed_bitfield_decl = class_clang_type.AddFieldToRecordType (NULL, 2073 GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width), 2074 accessibility, 2075 anon_field_info.bit_size); 2076 2077 layout_info.field_offsets.insert(std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset)); 2078 } 2079 } 2080 last_field_info = this_field_info; 2081 } 2082 else 2083 { 2084 last_field_info.Clear(); 2085 } 2086 2087 ClangASTType member_clang_type = member_type->GetClangLayoutType(); 2088 2089 { 2090 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>). 2091 // If the current field is at the end of the structure, then there is definitely no room for extra 2092 // elements and we override the type to array[0]. 2093 2094 ClangASTType member_array_element_type; 2095 uint64_t member_array_size; 2096 bool member_array_is_incomplete; 2097 2098 if (member_clang_type.IsArrayType(&member_array_element_type, 2099 &member_array_size, 2100 &member_array_is_incomplete) && 2101 !member_array_is_incomplete) 2102 { 2103 uint64_t parent_byte_size = parent_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, UINT64_MAX); 2104 2105 if (member_byte_offset >= parent_byte_size) 2106 { 2107 if (member_array_size != 1) 2108 { 2109 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64, 2110 MakeUserID(die->GetOffset()), 2111 name, 2112 encoding_uid, 2113 MakeUserID(parent_die->GetOffset())); 2114 } 2115 2116 member_clang_type = GetClangASTContext().CreateArrayType(member_array_element_type, 0, false); 2117 } 2118 } 2119 } 2120 2121 field_decl = class_clang_type.AddFieldToRecordType (name, 2122 member_clang_type, 2123 accessibility, 2124 bit_size); 2125 2126 GetClangASTContext().SetMetadataAsUserID (field_decl, MakeUserID(die->GetOffset())); 2127 2128 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset)); 2129 2130 } 2131 else 2132 { 2133 if (name) 2134 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed", 2135 MakeUserID(die->GetOffset()), 2136 name, 2137 encoding_uid); 2138 else 2139 GetObjectFile()->GetModule()->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed", 2140 MakeUserID(die->GetOffset()), 2141 encoding_uid); 2142 } 2143 } 2144 2145 if (prop_name != NULL) 2146 { 2147 clang::ObjCIvarDecl *ivar_decl = NULL; 2148 2149 if (field_decl) 2150 { 2151 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl); 2152 assert (ivar_decl != NULL); 2153 } 2154 2155 ClangASTMetadata metadata; 2156 metadata.SetUserID (MakeUserID(die->GetOffset())); 2157 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type, 2158 prop_name, 2159 member_type->GetClangLayoutType(), 2160 ivar_decl, 2161 prop_setter_name, 2162 prop_getter_name, 2163 prop_attributes, 2164 &metadata)); 2165 2166 if (ivar_decl) 2167 GetClangASTContext().SetMetadataAsUserID (ivar_decl, MakeUserID(die->GetOffset())); 2168 } 2169 } 2170 } 2171 ++member_idx; 2172 } 2173 break; 2174 2175 case DW_TAG_subprogram: 2176 // Let the type parsing code handle this one for us. 2177 member_function_dies.Append (die); 2178 break; 2179 2180 case DW_TAG_inheritance: 2181 { 2182 is_a_class = true; 2183 if (default_accessibility == eAccessNone) 2184 default_accessibility = eAccessPrivate; 2185 // TODO: implement DW_TAG_inheritance type parsing 2186 DWARFDebugInfoEntry::Attributes attributes; 2187 const size_t num_attributes = die->GetAttributes (this, 2188 dwarf_cu, 2189 fixed_form_sizes, 2190 attributes); 2191 if (num_attributes > 0) 2192 { 2193 Declaration decl; 2194 DWARFExpression location; 2195 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 2196 AccessType accessibility = default_accessibility; 2197 bool is_virtual = false; 2198 bool is_base_of_class = true; 2199 off_t member_byte_offset = 0; 2200 uint32_t i; 2201 for (i=0; i<num_attributes; ++i) 2202 { 2203 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2204 DWARFFormValue form_value; 2205 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 2206 { 2207 switch (attr) 2208 { 2209 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2210 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2211 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2212 case DW_AT_type: encoding_uid = form_value.Reference(); break; 2213 case DW_AT_data_member_location: 2214 if (form_value.BlockData()) 2215 { 2216 Value initialValue(0); 2217 Value memberOffset(0); 2218 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 2219 uint32_t block_length = form_value.Unsigned(); 2220 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 2221 if (DWARFExpression::Evaluate (NULL, 2222 NULL, 2223 NULL, 2224 NULL, 2225 module, 2226 debug_info_data, 2227 block_offset, 2228 block_length, 2229 eRegisterKindDWARF, 2230 &initialValue, 2231 memberOffset, 2232 NULL)) 2233 { 2234 member_byte_offset = memberOffset.ResolveValue(NULL).UInt(); 2235 } 2236 } 2237 else 2238 { 2239 // With DWARF 3 and later, if the value is an integer constant, 2240 // this form value is the offset in bytes from the beginning 2241 // of the containing entity. 2242 member_byte_offset = form_value.Unsigned(); 2243 } 2244 break; 2245 2246 case DW_AT_accessibility: 2247 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 2248 break; 2249 2250 case DW_AT_virtuality: 2251 is_virtual = form_value.Boolean(); 2252 break; 2253 2254 case DW_AT_sibling: 2255 break; 2256 2257 default: 2258 break; 2259 } 2260 } 2261 } 2262 2263 Type *base_class_type = ResolveTypeUID(encoding_uid); 2264 assert(base_class_type); 2265 2266 ClangASTType base_class_clang_type = base_class_type->GetClangFullType(); 2267 assert (base_class_clang_type); 2268 if (class_language == eLanguageTypeObjC) 2269 { 2270 class_clang_type.SetObjCSuperClass(base_class_clang_type); 2271 } 2272 else 2273 { 2274 base_classes.push_back (base_class_clang_type.CreateBaseClassSpecifier (accessibility, 2275 is_virtual, 2276 is_base_of_class)); 2277 2278 if (is_virtual) 2279 { 2280 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't 2281 // give us a constant offset, but gives us a DWARF expressions that requires an actual object 2282 // in memory. the DW_AT_data_member_location for a virtual base class looks like: 2283 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus ) 2284 // Given this, there is really no valid response we can give to clang for virtual base 2285 // class offsets, and this should eventually be removed from LayoutRecordType() in the external 2286 // AST source in clang. 2287 } 2288 else 2289 { 2290 layout_info.base_offsets.insert(std::make_pair(base_class_clang_type.GetAsCXXRecordDecl(), 2291 clang::CharUnits::fromQuantity(member_byte_offset))); 2292 } 2293 } 2294 } 2295 } 2296 break; 2297 2298 default: 2299 break; 2300 } 2301 } 2302 2303 return count; 2304 } 2305 2306 2307 clang::DeclContext* 2308 SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid) 2309 { 2310 DWARFDebugInfo* debug_info = DebugInfo(); 2311 if (debug_info && UserIDMatches(type_uid)) 2312 { 2313 DWARFCompileUnitSP cu_sp; 2314 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp); 2315 if (die) 2316 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL); 2317 } 2318 return NULL; 2319 } 2320 2321 clang::DeclContext* 2322 SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid) 2323 { 2324 if (UserIDMatches(type_uid)) 2325 return GetClangDeclContextForDIEOffset (sc, type_uid); 2326 return NULL; 2327 } 2328 2329 Type* 2330 SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid) 2331 { 2332 if (UserIDMatches(type_uid)) 2333 { 2334 DWARFDebugInfo* debug_info = DebugInfo(); 2335 if (debug_info) 2336 { 2337 DWARFCompileUnitSP cu_sp; 2338 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp); 2339 const bool assert_not_being_parsed = true; 2340 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed); 2341 } 2342 } 2343 return NULL; 2344 } 2345 2346 Type* 2347 SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed) 2348 { 2349 if (die != NULL) 2350 { 2351 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 2352 if (log) 2353 GetObjectFile()->GetModule()->LogMessage (log, 2354 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'", 2355 die->GetOffset(), 2356 DW_TAG_value_to_name(die->Tag()), 2357 die->GetName(this, cu)); 2358 2359 // We might be coming in in the middle of a type tree (a class 2360 // withing a class, an enum within a class), so parse any needed 2361 // parent DIEs before we get to this one... 2362 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die); 2363 switch (decl_ctx_die->Tag()) 2364 { 2365 case DW_TAG_structure_type: 2366 case DW_TAG_union_type: 2367 case DW_TAG_class_type: 2368 { 2369 // Get the type, which could be a forward declaration 2370 if (log) 2371 GetObjectFile()->GetModule()->LogMessage (log, 2372 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x", 2373 die->GetOffset(), 2374 DW_TAG_value_to_name(die->Tag()), 2375 die->GetName(this, cu), 2376 decl_ctx_die->GetOffset()); 2377 // 2378 // Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed); 2379 // if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag())) 2380 // { 2381 // if (log) 2382 // GetObjectFile()->GetModule()->LogMessage (log, 2383 // "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function", 2384 // die->GetOffset(), 2385 // DW_TAG_value_to_name(die->Tag()), 2386 // die->GetName(this, cu), 2387 // decl_ctx_die->GetOffset()); 2388 // // Ask the type to complete itself if it already hasn't since if we 2389 // // want a function (method or static) from a class, the class must 2390 // // create itself and add it's own methods and class functions. 2391 // if (parent_type) 2392 // parent_type->GetClangFullType(); 2393 // } 2394 } 2395 break; 2396 2397 default: 2398 break; 2399 } 2400 return ResolveType (cu, die); 2401 } 2402 return NULL; 2403 } 2404 2405 // This function is used when SymbolFileDWARFDebugMap owns a bunch of 2406 // SymbolFileDWARF objects to detect if this DWARF file is the one that 2407 // can resolve a clang_type. 2408 bool 2409 SymbolFileDWARF::HasForwardDeclForClangType (const ClangASTType &clang_type) 2410 { 2411 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers(); 2412 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType()); 2413 return die != NULL; 2414 } 2415 2416 2417 bool 2418 SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (ClangASTType &clang_type) 2419 { 2420 // We have a struct/union/class/enum that needs to be fully resolved. 2421 ClangASTType clang_type_no_qualifiers = clang_type.RemoveFastQualifiers(); 2422 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers.GetOpaqueQualType()); 2423 if (die == NULL) 2424 { 2425 // We have already resolved this type... 2426 return true; 2427 } 2428 // Once we start resolving this type, remove it from the forward declaration 2429 // map in case anyone child members or other types require this type to get resolved. 2430 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition 2431 // are done. 2432 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers.GetOpaqueQualType()); 2433 2434 // Disable external storage for this type so we don't get anymore 2435 // clang::ExternalASTSource queries for this type. 2436 clang_type.SetHasExternalStorage (false); 2437 2438 DWARFDebugInfo* debug_info = DebugInfo(); 2439 2440 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get(); 2441 Type *type = m_die_to_type.lookup (die); 2442 2443 const dw_tag_t tag = die->Tag(); 2444 2445 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION)); 2446 if (log) 2447 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log, 2448 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...", 2449 MakeUserID(die->GetOffset()), 2450 DW_TAG_value_to_name(tag), 2451 type->GetName().AsCString()); 2452 assert (clang_type); 2453 DWARFDebugInfoEntry::Attributes attributes; 2454 2455 switch (tag) 2456 { 2457 case DW_TAG_structure_type: 2458 case DW_TAG_union_type: 2459 case DW_TAG_class_type: 2460 { 2461 LayoutInfo layout_info; 2462 2463 { 2464 if (die->HasChildren()) 2465 { 2466 LanguageType class_language = eLanguageTypeUnknown; 2467 if (clang_type.IsObjCObjectOrInterfaceType()) 2468 { 2469 class_language = eLanguageTypeObjC; 2470 // For objective C we don't start the definition when 2471 // the class is created. 2472 clang_type.StartTagDeclarationDefinition (); 2473 } 2474 2475 int tag_decl_kind = -1; 2476 AccessType default_accessibility = eAccessNone; 2477 if (tag == DW_TAG_structure_type) 2478 { 2479 tag_decl_kind = clang::TTK_Struct; 2480 default_accessibility = eAccessPublic; 2481 } 2482 else if (tag == DW_TAG_union_type) 2483 { 2484 tag_decl_kind = clang::TTK_Union; 2485 default_accessibility = eAccessPublic; 2486 } 2487 else if (tag == DW_TAG_class_type) 2488 { 2489 tag_decl_kind = clang::TTK_Class; 2490 default_accessibility = eAccessPrivate; 2491 } 2492 2493 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu)); 2494 std::vector<clang::CXXBaseSpecifier *> base_classes; 2495 std::vector<int> member_accessibilities; 2496 bool is_a_class = false; 2497 // Parse members and base classes first 2498 DWARFDIECollection member_function_dies; 2499 2500 DelayedPropertyList delayed_properties; 2501 ParseChildMembers (sc, 2502 dwarf_cu, 2503 die, 2504 clang_type, 2505 class_language, 2506 base_classes, 2507 member_accessibilities, 2508 member_function_dies, 2509 delayed_properties, 2510 default_accessibility, 2511 is_a_class, 2512 layout_info); 2513 2514 // Now parse any methods if there were any... 2515 size_t num_functions = member_function_dies.Size(); 2516 if (num_functions > 0) 2517 { 2518 for (size_t i=0; i<num_functions; ++i) 2519 { 2520 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i)); 2521 } 2522 } 2523 2524 if (class_language == eLanguageTypeObjC) 2525 { 2526 ConstString class_name (clang_type.GetTypeName()); 2527 if (class_name) 2528 { 2529 DIEArray method_die_offsets; 2530 if (m_using_apple_tables) 2531 { 2532 if (m_apple_objc_ap.get()) 2533 m_apple_objc_ap->FindByName(class_name.GetCString(), method_die_offsets); 2534 } 2535 else 2536 { 2537 if (!m_indexed) 2538 Index (); 2539 2540 m_objc_class_selectors_index.Find (class_name, method_die_offsets); 2541 } 2542 2543 if (!method_die_offsets.empty()) 2544 { 2545 DWARFDebugInfo* debug_info = DebugInfo(); 2546 2547 DWARFCompileUnit* method_cu = NULL; 2548 const size_t num_matches = method_die_offsets.size(); 2549 for (size_t i=0; i<num_matches; ++i) 2550 { 2551 const dw_offset_t die_offset = method_die_offsets[i]; 2552 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu); 2553 2554 if (method_die) 2555 ResolveType (method_cu, method_die); 2556 else 2557 { 2558 if (m_using_apple_tables) 2559 { 2560 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n", 2561 die_offset, class_name.GetCString()); 2562 } 2563 } 2564 } 2565 } 2566 2567 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end(); 2568 pi != pe; 2569 ++pi) 2570 pi->Finalize(); 2571 } 2572 } 2573 2574 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we 2575 // need to tell the clang type it is actually a class. 2576 if (class_language != eLanguageTypeObjC) 2577 { 2578 if (is_a_class && tag_decl_kind != clang::TTK_Class) 2579 clang_type.SetTagTypeKind (clang::TTK_Class); 2580 } 2581 2582 // Since DW_TAG_structure_type gets used for both classes 2583 // and structures, we may need to set any DW_TAG_member 2584 // fields to have a "private" access if none was specified. 2585 // When we parsed the child members we tracked that actual 2586 // accessibility value for each DW_TAG_member in the 2587 // "member_accessibilities" array. If the value for the 2588 // member is zero, then it was set to the "default_accessibility" 2589 // which for structs was "public". Below we correct this 2590 // by setting any fields to "private" that weren't correctly 2591 // set. 2592 if (is_a_class && !member_accessibilities.empty()) 2593 { 2594 // This is a class and all members that didn't have 2595 // their access specified are private. 2596 clang_type.SetDefaultAccessForRecordFields (eAccessPrivate, 2597 &member_accessibilities.front(), 2598 member_accessibilities.size()); 2599 } 2600 2601 if (!base_classes.empty()) 2602 { 2603 // Make sure all base classes refer to complete types and not 2604 // forward declarations. If we don't do this, clang will crash 2605 // with an assertion in the call to clang_type.SetBaseClassesForClassType() 2606 bool base_class_error = false; 2607 for (auto &base_class : base_classes) 2608 { 2609 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo(); 2610 if (type_source_info) 2611 { 2612 ClangASTType base_class_type (GetClangASTContext().getASTContext(), type_source_info->getType()); 2613 if (base_class_type.GetCompleteType() == false) 2614 { 2615 if (!base_class_error) 2616 { 2617 GetObjectFile()->GetModule()->ReportError ("DWARF DIE at 0x%8.8x for class '%s' has a base class '%s' that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s", 2618 die->GetOffset(), 2619 die->GetName(this, dwarf_cu), 2620 base_class_type.GetTypeName().GetCString(), 2621 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file"); 2622 } 2623 // We have no choice other than to pretend that the base class 2624 // is complete. If we don't do this, clang will crash when we 2625 // call setBases() inside of "clang_type.SetBaseClassesForClassType()" 2626 // below. Since we provide layout assistance, all ivars in this 2627 // class and other classes will be fine, this is the best we can do 2628 // short of crashing. 2629 base_class_type.StartTagDeclarationDefinition (); 2630 base_class_type.CompleteTagDeclarationDefinition (); 2631 } 2632 } 2633 } 2634 clang_type.SetBaseClassesForClassType (&base_classes.front(), 2635 base_classes.size()); 2636 2637 // Clang will copy each CXXBaseSpecifier in "base_classes" 2638 // so we have to free them all. 2639 ClangASTType::DeleteBaseClassSpecifiers (&base_classes.front(), 2640 base_classes.size()); 2641 } 2642 } 2643 } 2644 2645 clang_type.BuildIndirectFields (); 2646 clang_type.CompleteTagDeclarationDefinition (); 2647 2648 if (!layout_info.field_offsets.empty() || 2649 !layout_info.base_offsets.empty() || 2650 !layout_info.vbase_offsets.empty() ) 2651 { 2652 if (type) 2653 layout_info.bit_size = type->GetByteSize() * 8; 2654 if (layout_info.bit_size == 0) 2655 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8; 2656 2657 clang::CXXRecordDecl *record_decl = clang_type.GetAsCXXRecordDecl(); 2658 if (record_decl) 2659 { 2660 if (log) 2661 { 2662 GetObjectFile()->GetModule()->LogMessage (log, 2663 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])", 2664 static_cast<void*>(clang_type.GetOpaqueQualType()), 2665 static_cast<void*>(record_decl), 2666 layout_info.bit_size, 2667 layout_info.alignment, 2668 static_cast<uint32_t>(layout_info.field_offsets.size()), 2669 static_cast<uint32_t>(layout_info.base_offsets.size()), 2670 static_cast<uint32_t>(layout_info.vbase_offsets.size())); 2671 2672 uint32_t idx; 2673 { 2674 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end(); 2675 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx) 2676 { 2677 GetObjectFile()->GetModule()->LogMessage (log, 2678 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }", 2679 static_cast<void*>(clang_type.GetOpaqueQualType()), 2680 idx, 2681 static_cast<uint32_t>(pos->second), 2682 pos->first->getNameAsString().c_str()); 2683 } 2684 } 2685 2686 { 2687 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos, base_end = layout_info.base_offsets.end(); 2688 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx) 2689 { 2690 GetObjectFile()->GetModule()->LogMessage (log, 2691 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }", 2692 clang_type.GetOpaqueQualType(), 2693 idx, 2694 (uint32_t)base_pos->second.getQuantity(), 2695 base_pos->first->getNameAsString().c_str()); 2696 } 2697 } 2698 { 2699 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos, vbase_end = layout_info.vbase_offsets.end(); 2700 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx) 2701 { 2702 GetObjectFile()->GetModule()->LogMessage (log, 2703 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }", 2704 static_cast<void*>(clang_type.GetOpaqueQualType()), 2705 idx, 2706 static_cast<uint32_t>(vbase_pos->second.getQuantity()), 2707 vbase_pos->first->getNameAsString().c_str()); 2708 } 2709 } 2710 } 2711 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info)); 2712 } 2713 } 2714 } 2715 2716 return (bool)clang_type; 2717 2718 case DW_TAG_enumeration_type: 2719 clang_type.StartTagDeclarationDefinition (); 2720 if (die->HasChildren()) 2721 { 2722 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu)); 2723 bool is_signed = false; 2724 clang_type.IsIntegerType(is_signed); 2725 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), dwarf_cu, die); 2726 } 2727 clang_type.CompleteTagDeclarationDefinition (); 2728 return (bool)clang_type; 2729 2730 default: 2731 assert(false && "not a forward clang type decl!"); 2732 break; 2733 } 2734 return false; 2735 } 2736 2737 Type* 2738 SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed) 2739 { 2740 if (type_die != NULL) 2741 { 2742 Type *type = m_die_to_type.lookup (type_die); 2743 2744 if (type == NULL) 2745 type = GetTypeForDIE (dwarf_cu, type_die).get(); 2746 2747 if (assert_not_being_parsed) 2748 { 2749 if (type != DIE_IS_BEING_PARSED) 2750 return type; 2751 2752 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s", 2753 type_die->GetOffset(), 2754 DW_TAG_value_to_name(type_die->Tag()), 2755 type_die->GetName(this, dwarf_cu)); 2756 2757 } 2758 else 2759 return type; 2760 } 2761 return NULL; 2762 } 2763 2764 CompileUnit* 2765 SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx) 2766 { 2767 // Check if the symbol vendor already knows about this compile unit? 2768 if (dwarf_cu->GetUserData() == NULL) 2769 { 2770 // The symbol vendor doesn't know about this compile unit, we 2771 // need to parse and add it to the symbol vendor object. 2772 return ParseCompileUnit(dwarf_cu, cu_idx).get(); 2773 } 2774 return (CompileUnit*)dwarf_cu->GetUserData(); 2775 } 2776 2777 bool 2778 SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc) 2779 { 2780 sc.Clear(false); 2781 // Check if the symbol vendor already knows about this compile unit? 2782 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 2783 2784 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get(); 2785 if (sc.function == NULL) 2786 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die); 2787 2788 if (sc.function) 2789 { 2790 sc.module_sp = sc.function->CalculateSymbolContextModule(); 2791 return true; 2792 } 2793 2794 return false; 2795 } 2796 2797 2798 2799 SymbolFileDWARF::GlobalVariableMap & 2800 SymbolFileDWARF::GetGlobalAranges() 2801 { 2802 if (!m_global_aranges_ap) 2803 { 2804 m_global_aranges_ap.reset (new GlobalVariableMap()); 2805 2806 ModuleSP module_sp = GetObjectFile()->GetModule(); 2807 if (module_sp) 2808 { 2809 const size_t num_cus = module_sp->GetNumCompileUnits(); 2810 for (size_t i = 0; i < num_cus; ++i) 2811 { 2812 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i); 2813 if (cu_sp) 2814 { 2815 VariableListSP globals_sp = cu_sp->GetVariableList(true); 2816 if (globals_sp) 2817 { 2818 const size_t num_globals = globals_sp->GetSize(); 2819 for (size_t g = 0; g < num_globals; ++g) 2820 { 2821 VariableSP var_sp = globals_sp->GetVariableAtIndex(g); 2822 if (var_sp && !var_sp->GetLocationIsConstantValueData()) 2823 { 2824 const DWARFExpression &location = var_sp->LocationExpression(); 2825 Value location_result; 2826 Error error; 2827 if (location.Evaluate(NULL, NULL, NULL, LLDB_INVALID_ADDRESS, NULL, location_result, &error)) 2828 { 2829 if (location_result.GetValueType() == Value::eValueTypeFileAddress) 2830 { 2831 lldb::addr_t file_addr = location_result.GetScalar().ULongLong(); 2832 lldb::addr_t byte_size = 1; 2833 if (var_sp->GetType()) 2834 byte_size = var_sp->GetType()->GetByteSize(); 2835 m_global_aranges_ap->Append(GlobalVariableMap::Entry(file_addr, byte_size, var_sp.get())); 2836 } 2837 } 2838 } 2839 } 2840 } 2841 } 2842 } 2843 } 2844 m_global_aranges_ap->Sort(); 2845 } 2846 return *m_global_aranges_ap; 2847 } 2848 2849 2850 uint32_t 2851 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 2852 { 2853 Timer scoped_timer(__PRETTY_FUNCTION__, 2854 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%" PRIx64 " }, resolve_scope = 0x%8.8x)", 2855 static_cast<void*>(so_addr.GetSection().get()), 2856 so_addr.GetOffset(), resolve_scope); 2857 uint32_t resolved = 0; 2858 if (resolve_scope & ( eSymbolContextCompUnit | 2859 eSymbolContextFunction | 2860 eSymbolContextBlock | 2861 eSymbolContextLineEntry | 2862 eSymbolContextVariable )) 2863 { 2864 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 2865 2866 DWARFDebugInfo* debug_info = DebugInfo(); 2867 if (debug_info) 2868 { 2869 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr); 2870 if (cu_offset == DW_INVALID_OFFSET) 2871 { 2872 // Global variables are not in the compile unit address ranges. The only way to 2873 // currently find global variables is to iterate over the .debug_pubnames or the 2874 // __apple_names table and find all items in there that point to DW_TAG_variable 2875 // DIEs and then find the address that matches. 2876 if (resolve_scope & eSymbolContextVariable) 2877 { 2878 GlobalVariableMap &map = GetGlobalAranges(); 2879 const GlobalVariableMap::Entry *entry = map.FindEntryThatContains(file_vm_addr); 2880 if (entry && entry->data) 2881 { 2882 Variable *variable = entry->data; 2883 SymbolContextScope *scc = variable->GetSymbolContextScope(); 2884 if (scc) 2885 { 2886 scc->CalculateSymbolContext(&sc); 2887 sc.variable = variable; 2888 } 2889 return sc.GetResolvedMask(); 2890 } 2891 } 2892 } 2893 else 2894 { 2895 uint32_t cu_idx = DW_INVALID_INDEX; 2896 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get(); 2897 if (dwarf_cu) 2898 { 2899 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 2900 if (sc.comp_unit) 2901 { 2902 resolved |= eSymbolContextCompUnit; 2903 2904 bool force_check_line_table = false; 2905 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 2906 { 2907 DWARFDebugInfoEntry *function_die = NULL; 2908 DWARFDebugInfoEntry *block_die = NULL; 2909 if (resolve_scope & eSymbolContextBlock) 2910 { 2911 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die); 2912 } 2913 else 2914 { 2915 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL); 2916 } 2917 2918 if (function_die != NULL) 2919 { 2920 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get(); 2921 if (sc.function == NULL) 2922 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die); 2923 } 2924 else 2925 { 2926 // We might have had a compile unit that had discontiguous 2927 // address ranges where the gaps are symbols that don't have 2928 // any debug info. Discontiguous compile unit address ranges 2929 // should only happen when there aren't other functions from 2930 // other compile units in these gaps. This helps keep the size 2931 // of the aranges down. 2932 force_check_line_table = true; 2933 } 2934 2935 if (sc.function != NULL) 2936 { 2937 resolved |= eSymbolContextFunction; 2938 2939 if (resolve_scope & eSymbolContextBlock) 2940 { 2941 Block& block = sc.function->GetBlock (true); 2942 2943 if (block_die != NULL) 2944 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset())); 2945 else 2946 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset())); 2947 if (sc.block) 2948 resolved |= eSymbolContextBlock; 2949 } 2950 } 2951 } 2952 2953 if ((resolve_scope & eSymbolContextLineEntry) || force_check_line_table) 2954 { 2955 LineTable *line_table = sc.comp_unit->GetLineTable(); 2956 if (line_table != NULL) 2957 { 2958 // And address that makes it into this function should be in terms 2959 // of this debug file if there is no debug map, or it will be an 2960 // address in the .o file which needs to be fixed up to be in terms 2961 // of the debug map executable. Either way, calling FixupAddress() 2962 // will work for us. 2963 Address exe_so_addr (so_addr); 2964 if (FixupAddress(exe_so_addr)) 2965 { 2966 if (line_table->FindLineEntryByAddress (exe_so_addr, sc.line_entry)) 2967 { 2968 resolved |= eSymbolContextLineEntry; 2969 } 2970 } 2971 } 2972 } 2973 2974 if (force_check_line_table && !(resolved & eSymbolContextLineEntry)) 2975 { 2976 // We might have had a compile unit that had discontiguous 2977 // address ranges where the gaps are symbols that don't have 2978 // any debug info. Discontiguous compile unit address ranges 2979 // should only happen when there aren't other functions from 2980 // other compile units in these gaps. This helps keep the size 2981 // of the aranges down. 2982 sc.comp_unit = NULL; 2983 resolved &= ~eSymbolContextCompUnit; 2984 } 2985 } 2986 else 2987 { 2988 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.", 2989 cu_offset, 2990 cu_idx); 2991 } 2992 } 2993 } 2994 } 2995 } 2996 return resolved; 2997 } 2998 2999 3000 3001 uint32_t 3002 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 3003 { 3004 const uint32_t prev_size = sc_list.GetSize(); 3005 if (resolve_scope & eSymbolContextCompUnit) 3006 { 3007 DWARFDebugInfo* debug_info = DebugInfo(); 3008 if (debug_info) 3009 { 3010 uint32_t cu_idx; 3011 DWARFCompileUnit* dwarf_cu = NULL; 3012 3013 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx) 3014 { 3015 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 3016 const bool full_match = (bool)file_spec.GetDirectory(); 3017 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match); 3018 if (check_inlines || file_spec_matches_cu_file_spec) 3019 { 3020 SymbolContext sc (m_obj_file->GetModule()); 3021 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx); 3022 if (sc.comp_unit) 3023 { 3024 uint32_t file_idx = UINT32_MAX; 3025 3026 // If we are looking for inline functions only and we don't 3027 // find it in the support files, we are done. 3028 if (check_inlines) 3029 { 3030 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 3031 if (file_idx == UINT32_MAX) 3032 continue; 3033 } 3034 3035 if (line != 0) 3036 { 3037 LineTable *line_table = sc.comp_unit->GetLineTable(); 3038 3039 if (line_table != NULL && line != 0) 3040 { 3041 // We will have already looked up the file index if 3042 // we are searching for inline entries. 3043 if (!check_inlines) 3044 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 3045 3046 if (file_idx != UINT32_MAX) 3047 { 3048 uint32_t found_line; 3049 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry); 3050 found_line = sc.line_entry.line; 3051 3052 while (line_idx != UINT32_MAX) 3053 { 3054 sc.function = NULL; 3055 sc.block = NULL; 3056 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 3057 { 3058 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress(); 3059 if (file_vm_addr != LLDB_INVALID_ADDRESS) 3060 { 3061 DWARFDebugInfoEntry *function_die = NULL; 3062 DWARFDebugInfoEntry *block_die = NULL; 3063 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL); 3064 3065 if (function_die != NULL) 3066 { 3067 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get(); 3068 if (sc.function == NULL) 3069 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die); 3070 } 3071 3072 if (sc.function != NULL) 3073 { 3074 Block& block = sc.function->GetBlock (true); 3075 3076 if (block_die != NULL) 3077 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset())); 3078 else if (function_die != NULL) 3079 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset())); 3080 } 3081 } 3082 } 3083 3084 sc_list.Append(sc); 3085 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry); 3086 } 3087 } 3088 } 3089 else if (file_spec_matches_cu_file_spec && !check_inlines) 3090 { 3091 // only append the context if we aren't looking for inline call sites 3092 // by file and line and if the file spec matches that of the compile unit 3093 sc_list.Append(sc); 3094 } 3095 } 3096 else if (file_spec_matches_cu_file_spec && !check_inlines) 3097 { 3098 // only append the context if we aren't looking for inline call sites 3099 // by file and line and if the file spec matches that of the compile unit 3100 sc_list.Append(sc); 3101 } 3102 3103 if (!check_inlines) 3104 break; 3105 } 3106 } 3107 } 3108 } 3109 } 3110 return sc_list.GetSize() - prev_size; 3111 } 3112 3113 void 3114 SymbolFileDWARF::Index () 3115 { 3116 if (m_indexed) 3117 return; 3118 m_indexed = true; 3119 Timer scoped_timer (__PRETTY_FUNCTION__, 3120 "SymbolFileDWARF::Index (%s)", 3121 GetObjectFile()->GetFileSpec().GetFilename().AsCString("<Unknown>")); 3122 3123 DWARFDebugInfo* debug_info = DebugInfo(); 3124 if (debug_info) 3125 { 3126 uint32_t cu_idx = 0; 3127 const uint32_t num_compile_units = GetNumCompileUnits(); 3128 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 3129 { 3130 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 3131 3132 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1; 3133 3134 dwarf_cu->Index (cu_idx, 3135 m_function_basename_index, 3136 m_function_fullname_index, 3137 m_function_method_index, 3138 m_function_selector_index, 3139 m_objc_class_selectors_index, 3140 m_global_index, 3141 m_type_index, 3142 m_namespace_index); 3143 3144 // Keep memory down by clearing DIEs if this generate function 3145 // caused them to be parsed 3146 if (clear_dies) 3147 dwarf_cu->ClearDIEs (true); 3148 } 3149 3150 m_function_basename_index.Finalize(); 3151 m_function_fullname_index.Finalize(); 3152 m_function_method_index.Finalize(); 3153 m_function_selector_index.Finalize(); 3154 m_objc_class_selectors_index.Finalize(); 3155 m_global_index.Finalize(); 3156 m_type_index.Finalize(); 3157 m_namespace_index.Finalize(); 3158 3159 #if defined (ENABLE_DEBUG_PRINTF) 3160 StreamFile s(stdout, false); 3161 s.Printf ("DWARF index for '%s':", 3162 GetObjectFile()->GetFileSpec().GetPath().c_str()); 3163 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 3164 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 3165 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 3166 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 3167 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 3168 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 3169 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 3170 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s); 3171 #endif 3172 } 3173 } 3174 3175 bool 3176 SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl) 3177 { 3178 if (namespace_decl == NULL) 3179 { 3180 // Invalid namespace decl which means we aren't matching only things 3181 // in this symbol file, so return true to indicate it matches this 3182 // symbol file. 3183 return true; 3184 } 3185 3186 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext(); 3187 3188 if (namespace_ast == NULL) 3189 return true; // No AST in the "namespace_decl", return true since it 3190 // could then match any symbol file, including this one 3191 3192 if (namespace_ast == GetClangASTContext().getASTContext()) 3193 return true; // The ASTs match, return true 3194 3195 // The namespace AST was valid, and it does not match... 3196 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3197 3198 if (log) 3199 GetObjectFile()->GetModule()->LogMessage(log, "Valid namespace does not match symbol file"); 3200 3201 return false; 3202 } 3203 3204 bool 3205 SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl, 3206 DWARFCompileUnit* cu, 3207 const DWARFDebugInfoEntry* die) 3208 { 3209 // No namespace specified, so the answer is 3210 if (namespace_decl == NULL) 3211 return true; 3212 3213 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3214 3215 const DWARFDebugInfoEntry *decl_ctx_die = NULL; 3216 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die); 3217 if (decl_ctx_die) 3218 { 3219 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl(); 3220 3221 if (clang_namespace_decl) 3222 { 3223 if (decl_ctx_die->Tag() != DW_TAG_namespace) 3224 { 3225 if (log) 3226 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent is not a namespace"); 3227 return false; 3228 } 3229 3230 if (clang_namespace_decl == die_clang_decl_ctx) 3231 return true; 3232 else 3233 return false; 3234 } 3235 else 3236 { 3237 // We have a namespace_decl that was not NULL but it contained 3238 // a NULL "clang::NamespaceDecl", so this means the global namespace 3239 // So as long the contained decl context DIE isn't a namespace 3240 // we should be ok. 3241 if (decl_ctx_die->Tag() != DW_TAG_namespace) 3242 return true; 3243 } 3244 } 3245 3246 if (log) 3247 GetObjectFile()->GetModule()->LogMessage(log, "Found a match, but its parent doesn't exist"); 3248 3249 return false; 3250 } 3251 uint32_t 3252 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) 3253 { 3254 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3255 3256 if (log) 3257 GetObjectFile()->GetModule()->LogMessage (log, 3258 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)", 3259 name.GetCString(), 3260 static_cast<const void*>(namespace_decl), 3261 append, max_matches); 3262 3263 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 3264 return 0; 3265 3266 DWARFDebugInfo* info = DebugInfo(); 3267 if (info == NULL) 3268 return 0; 3269 3270 // If we aren't appending the results to this list, then clear the list 3271 if (!append) 3272 variables.Clear(); 3273 3274 // Remember how many variables are in the list before we search in case 3275 // we are appending the results to a variable list. 3276 const uint32_t original_size = variables.GetSize(); 3277 3278 DIEArray die_offsets; 3279 3280 if (m_using_apple_tables) 3281 { 3282 if (m_apple_names_ap.get()) 3283 { 3284 const char *name_cstr = name.GetCString(); 3285 llvm::StringRef basename; 3286 llvm::StringRef context; 3287 3288 if (!CPPLanguageRuntime::ExtractContextAndIdentifier(name_cstr, context, basename)) 3289 basename = name_cstr; 3290 3291 m_apple_names_ap->FindByName (basename.data(), die_offsets); 3292 } 3293 } 3294 else 3295 { 3296 // Index the DWARF if we haven't already 3297 if (!m_indexed) 3298 Index (); 3299 3300 m_global_index.Find (name, die_offsets); 3301 } 3302 3303 const size_t num_die_matches = die_offsets.size(); 3304 if (num_die_matches) 3305 { 3306 SymbolContext sc; 3307 sc.module_sp = m_obj_file->GetModule(); 3308 assert (sc.module_sp); 3309 3310 DWARFDebugInfo* debug_info = DebugInfo(); 3311 DWARFCompileUnit* dwarf_cu = NULL; 3312 const DWARFDebugInfoEntry* die = NULL; 3313 bool done = false; 3314 for (size_t i=0; i<num_die_matches && !done; ++i) 3315 { 3316 const dw_offset_t die_offset = die_offsets[i]; 3317 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3318 3319 if (die) 3320 { 3321 switch (die->Tag()) 3322 { 3323 default: 3324 case DW_TAG_subprogram: 3325 case DW_TAG_inlined_subroutine: 3326 case DW_TAG_try_block: 3327 case DW_TAG_catch_block: 3328 break; 3329 3330 case DW_TAG_variable: 3331 { 3332 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 3333 3334 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 3335 continue; 3336 3337 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 3338 3339 if (variables.GetSize() - original_size >= max_matches) 3340 done = true; 3341 } 3342 break; 3343 } 3344 } 3345 else 3346 { 3347 if (m_using_apple_tables) 3348 { 3349 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n", 3350 die_offset, name.GetCString()); 3351 } 3352 } 3353 } 3354 } 3355 3356 // Return the number of variable that were appended to the list 3357 const uint32_t num_matches = variables.GetSize() - original_size; 3358 if (log && num_matches > 0) 3359 { 3360 GetObjectFile()->GetModule()->LogMessage (log, 3361 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u", 3362 name.GetCString(), 3363 static_cast<const void*>(namespace_decl), 3364 append, max_matches, 3365 num_matches); 3366 } 3367 return num_matches; 3368 } 3369 3370 uint32_t 3371 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 3372 { 3373 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3374 3375 if (log) 3376 { 3377 GetObjectFile()->GetModule()->LogMessage (log, 3378 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)", 3379 regex.GetText(), append, 3380 max_matches); 3381 } 3382 3383 DWARFDebugInfo* info = DebugInfo(); 3384 if (info == NULL) 3385 return 0; 3386 3387 // If we aren't appending the results to this list, then clear the list 3388 if (!append) 3389 variables.Clear(); 3390 3391 // Remember how many variables are in the list before we search in case 3392 // we are appending the results to a variable list. 3393 const uint32_t original_size = variables.GetSize(); 3394 3395 DIEArray die_offsets; 3396 3397 if (m_using_apple_tables) 3398 { 3399 if (m_apple_names_ap.get()) 3400 { 3401 DWARFMappedHash::DIEInfoArray hash_data_array; 3402 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array)) 3403 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 3404 } 3405 } 3406 else 3407 { 3408 // Index the DWARF if we haven't already 3409 if (!m_indexed) 3410 Index (); 3411 3412 m_global_index.Find (regex, die_offsets); 3413 } 3414 3415 SymbolContext sc; 3416 sc.module_sp = m_obj_file->GetModule(); 3417 assert (sc.module_sp); 3418 3419 DWARFCompileUnit* dwarf_cu = NULL; 3420 const DWARFDebugInfoEntry* die = NULL; 3421 const size_t num_matches = die_offsets.size(); 3422 if (num_matches) 3423 { 3424 DWARFDebugInfo* debug_info = DebugInfo(); 3425 for (size_t i=0; i<num_matches; ++i) 3426 { 3427 const dw_offset_t die_offset = die_offsets[i]; 3428 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3429 3430 if (die) 3431 { 3432 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 3433 3434 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 3435 3436 if (variables.GetSize() - original_size >= max_matches) 3437 break; 3438 } 3439 else 3440 { 3441 if (m_using_apple_tables) 3442 { 3443 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n", 3444 die_offset, regex.GetText()); 3445 } 3446 } 3447 } 3448 } 3449 3450 // Return the number of variable that were appended to the list 3451 return variables.GetSize() - original_size; 3452 } 3453 3454 3455 bool 3456 SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset, 3457 DWARFCompileUnit *&dwarf_cu, 3458 SymbolContextList& sc_list) 3459 { 3460 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3461 return ResolveFunction (dwarf_cu, die, sc_list); 3462 } 3463 3464 3465 bool 3466 SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu, 3467 const DWARFDebugInfoEntry *die, 3468 SymbolContextList& sc_list) 3469 { 3470 SymbolContext sc; 3471 3472 if (die == NULL) 3473 return false; 3474 3475 // If we were passed a die that is not a function, just return false... 3476 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine) 3477 return false; 3478 3479 const DWARFDebugInfoEntry* inlined_die = NULL; 3480 if (die->Tag() == DW_TAG_inlined_subroutine) 3481 { 3482 inlined_die = die; 3483 3484 while ((die = die->GetParent()) != NULL) 3485 { 3486 if (die->Tag() == DW_TAG_subprogram) 3487 break; 3488 } 3489 } 3490 assert (die && die->Tag() == DW_TAG_subprogram); 3491 if (GetFunction (cu, die, sc)) 3492 { 3493 Address addr; 3494 // Parse all blocks if needed 3495 if (inlined_die) 3496 { 3497 Block &function_block = sc.function->GetBlock (true); 3498 sc.block = function_block.FindBlockByID (MakeUserID(inlined_die->GetOffset())); 3499 if (sc.block == NULL) 3500 sc.block = function_block.FindBlockByID (inlined_die->GetOffset()); 3501 if (sc.block == NULL || sc.block->GetStartAddress (addr) == false) 3502 addr.Clear(); 3503 } 3504 else 3505 { 3506 sc.block = NULL; 3507 addr = sc.function->GetAddressRange().GetBaseAddress(); 3508 } 3509 3510 if (addr.IsValid()) 3511 { 3512 sc_list.Append(sc); 3513 return true; 3514 } 3515 } 3516 3517 return false; 3518 } 3519 3520 void 3521 SymbolFileDWARF::FindFunctions (const ConstString &name, 3522 const NameToDIE &name_to_die, 3523 SymbolContextList& sc_list) 3524 { 3525 DIEArray die_offsets; 3526 if (name_to_die.Find (name, die_offsets)) 3527 { 3528 ParseFunctions (die_offsets, sc_list); 3529 } 3530 } 3531 3532 3533 void 3534 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 3535 const NameToDIE &name_to_die, 3536 SymbolContextList& sc_list) 3537 { 3538 DIEArray die_offsets; 3539 if (name_to_die.Find (regex, die_offsets)) 3540 { 3541 ParseFunctions (die_offsets, sc_list); 3542 } 3543 } 3544 3545 3546 void 3547 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 3548 const DWARFMappedHash::MemoryTable &memory_table, 3549 SymbolContextList& sc_list) 3550 { 3551 DIEArray die_offsets; 3552 DWARFMappedHash::DIEInfoArray hash_data_array; 3553 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array)) 3554 { 3555 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 3556 ParseFunctions (die_offsets, sc_list); 3557 } 3558 } 3559 3560 void 3561 SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets, 3562 SymbolContextList& sc_list) 3563 { 3564 const size_t num_matches = die_offsets.size(); 3565 if (num_matches) 3566 { 3567 SymbolContext sc; 3568 3569 DWARFCompileUnit* dwarf_cu = NULL; 3570 for (size_t i=0; i<num_matches; ++i) 3571 { 3572 const dw_offset_t die_offset = die_offsets[i]; 3573 ResolveFunction (die_offset, dwarf_cu, sc_list); 3574 } 3575 } 3576 } 3577 3578 bool 3579 SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die, 3580 const DWARFCompileUnit *dwarf_cu, 3581 uint32_t name_type_mask, 3582 const char *partial_name, 3583 const char *base_name_start, 3584 const char *base_name_end) 3585 { 3586 // If we are looking only for methods, throw away all the ones that are or aren't in C++ classes: 3587 if (name_type_mask == eFunctionNameTypeMethod || name_type_mask == eFunctionNameTypeBase) 3588 { 3589 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset()); 3590 if (!containing_decl_ctx) 3591 return false; 3592 3593 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()); 3594 3595 if (name_type_mask == eFunctionNameTypeMethod) 3596 { 3597 if (is_cxx_method == false) 3598 return false; 3599 } 3600 3601 if (name_type_mask == eFunctionNameTypeBase) 3602 { 3603 if (is_cxx_method == true) 3604 return false; 3605 } 3606 } 3607 3608 // Now we need to check whether the name we got back for this type matches the extra specifications 3609 // that were in the name we're looking up: 3610 if (base_name_start != partial_name || *base_name_end != '\0') 3611 { 3612 // First see if the stuff to the left matches the full name. To do that let's see if 3613 // we can pull out the mips linkage name attribute: 3614 3615 Mangled best_name; 3616 DWARFDebugInfoEntry::Attributes attributes; 3617 DWARFFormValue form_value; 3618 die->GetAttributes(this, dwarf_cu, NULL, attributes); 3619 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name); 3620 if (idx == UINT32_MAX) 3621 idx = attributes.FindAttributeIndex(DW_AT_linkage_name); 3622 if (idx != UINT32_MAX) 3623 { 3624 if (attributes.ExtractFormValueAtIndex(this, idx, form_value)) 3625 { 3626 const char *mangled_name = form_value.AsCString(&get_debug_str_data()); 3627 if (mangled_name) 3628 best_name.SetValue (ConstString(mangled_name), true); 3629 } 3630 } 3631 3632 if (!best_name) 3633 { 3634 idx = attributes.FindAttributeIndex(DW_AT_name); 3635 if (idx != UINT32_MAX && attributes.ExtractFormValueAtIndex(this, idx, form_value)) 3636 { 3637 const char *name = form_value.AsCString(&get_debug_str_data()); 3638 best_name.SetValue (ConstString(name), false); 3639 } 3640 } 3641 3642 if (best_name.GetDemangledName()) 3643 { 3644 const char *demangled = best_name.GetDemangledName().GetCString(); 3645 if (demangled) 3646 { 3647 std::string name_no_parens(partial_name, base_name_end - partial_name); 3648 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str()); 3649 if (partial_in_demangled == NULL) 3650 return false; 3651 else 3652 { 3653 // Sort out the case where our name is something like "Process::Destroy" and the match is 3654 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on 3655 // namespace boundaries... 3656 3657 if (partial_name[0] == ':' && partial_name[1] == ':') 3658 { 3659 // The partial name was already on a namespace boundary so all matches are good. 3660 return true; 3661 } 3662 else if (partial_in_demangled == demangled) 3663 { 3664 // They both start the same, so this is an good match. 3665 return true; 3666 } 3667 else 3668 { 3669 if (partial_in_demangled - demangled == 1) 3670 { 3671 // Only one character difference, can't be a namespace boundary... 3672 return false; 3673 } 3674 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':') 3675 { 3676 // We are on a namespace boundary, so this is also good. 3677 return true; 3678 } 3679 else 3680 return false; 3681 } 3682 } 3683 } 3684 } 3685 } 3686 3687 return true; 3688 } 3689 3690 uint32_t 3691 SymbolFileDWARF::FindFunctions (const ConstString &name, 3692 const lldb_private::ClangNamespaceDecl *namespace_decl, 3693 uint32_t name_type_mask, 3694 bool include_inlines, 3695 bool append, 3696 SymbolContextList& sc_list) 3697 { 3698 Timer scoped_timer (__PRETTY_FUNCTION__, 3699 "SymbolFileDWARF::FindFunctions (name = '%s')", 3700 name.AsCString()); 3701 3702 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup() 3703 assert ((name_type_mask & eFunctionNameTypeAuto) == 0); 3704 3705 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 3706 3707 if (log) 3708 { 3709 GetObjectFile()->GetModule()->LogMessage (log, 3710 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)", 3711 name.GetCString(), 3712 name_type_mask, 3713 append); 3714 } 3715 3716 // If we aren't appending the results to this list, then clear the list 3717 if (!append) 3718 sc_list.Clear(); 3719 3720 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 3721 return 0; 3722 3723 // If name is empty then we won't find anything. 3724 if (name.IsEmpty()) 3725 return 0; 3726 3727 // Remember how many sc_list are in the list before we search in case 3728 // we are appending the results to a variable list. 3729 3730 const char *name_cstr = name.GetCString(); 3731 3732 const uint32_t original_size = sc_list.GetSize(); 3733 3734 DWARFDebugInfo* info = DebugInfo(); 3735 if (info == NULL) 3736 return 0; 3737 3738 DWARFCompileUnit *dwarf_cu = NULL; 3739 std::set<const DWARFDebugInfoEntry *> resolved_dies; 3740 if (m_using_apple_tables) 3741 { 3742 if (m_apple_names_ap.get()) 3743 { 3744 3745 DIEArray die_offsets; 3746 3747 uint32_t num_matches = 0; 3748 3749 if (name_type_mask & eFunctionNameTypeFull) 3750 { 3751 // If they asked for the full name, match what they typed. At some point we may 3752 // want to canonicalize this (strip double spaces, etc. For now, we just add all the 3753 // dies that we find by exact match. 3754 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 3755 for (uint32_t i = 0; i < num_matches; i++) 3756 { 3757 const dw_offset_t die_offset = die_offsets[i]; 3758 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3759 if (die) 3760 { 3761 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 3762 continue; 3763 3764 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine) 3765 continue; 3766 3767 if (resolved_dies.find(die) == resolved_dies.end()) 3768 { 3769 if (ResolveFunction (dwarf_cu, die, sc_list)) 3770 resolved_dies.insert(die); 3771 } 3772 } 3773 else 3774 { 3775 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 3776 die_offset, name_cstr); 3777 } 3778 } 3779 } 3780 3781 if (name_type_mask & eFunctionNameTypeSelector) 3782 { 3783 if (namespace_decl && *namespace_decl) 3784 return 0; // no selectors in namespaces 3785 3786 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 3787 // Now make sure these are actually ObjC methods. In this case we can simply look up the name, 3788 // and if it is an ObjC method name, we're good. 3789 3790 for (uint32_t i = 0; i < num_matches; i++) 3791 { 3792 const dw_offset_t die_offset = die_offsets[i]; 3793 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3794 if (die) 3795 { 3796 const char *die_name = die->GetName(this, dwarf_cu); 3797 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name)) 3798 { 3799 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine) 3800 continue; 3801 3802 if (resolved_dies.find(die) == resolved_dies.end()) 3803 { 3804 if (ResolveFunction (dwarf_cu, die, sc_list)) 3805 resolved_dies.insert(die); 3806 } 3807 } 3808 } 3809 else 3810 { 3811 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 3812 die_offset, name_cstr); 3813 } 3814 } 3815 die_offsets.clear(); 3816 } 3817 3818 if (((name_type_mask & eFunctionNameTypeMethod) && !namespace_decl) || name_type_mask & eFunctionNameTypeBase) 3819 { 3820 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to 3821 // extract the base name, look that up, and if there is any other information in the name we were 3822 // passed in we have to post-filter based on that. 3823 3824 // FIXME: Arrange the logic above so that we don't calculate the base name twice: 3825 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 3826 3827 for (uint32_t i = 0; i < num_matches; i++) 3828 { 3829 const dw_offset_t die_offset = die_offsets[i]; 3830 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3831 if (die) 3832 { 3833 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine) 3834 continue; 3835 3836 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 3837 continue; 3838 3839 // If we get to here, the die is good, and we should add it: 3840 if (resolved_dies.find(die) == resolved_dies.end()) 3841 if (ResolveFunction (dwarf_cu, die, sc_list)) 3842 { 3843 bool keep_die = true; 3844 if ((name_type_mask & (eFunctionNameTypeBase|eFunctionNameTypeMethod)) != (eFunctionNameTypeBase|eFunctionNameTypeMethod)) 3845 { 3846 // We are looking for either basenames or methods, so we need to 3847 // trim out the ones we won't want by looking at the type 3848 SymbolContext sc; 3849 if (sc_list.GetLastContext(sc)) 3850 { 3851 if (sc.block) 3852 { 3853 // We have an inlined function 3854 } 3855 else if (sc.function) 3856 { 3857 Type *type = sc.function->GetType(); 3858 3859 if (type) 3860 { 3861 clang::DeclContext* decl_ctx = GetClangDeclContextContainingTypeUID (type->GetID()); 3862 if (decl_ctx->isRecord()) 3863 { 3864 if (name_type_mask & eFunctionNameTypeBase) 3865 { 3866 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1); 3867 keep_die = false; 3868 } 3869 } 3870 else 3871 { 3872 if (name_type_mask & eFunctionNameTypeMethod) 3873 { 3874 sc_list.RemoveContextAtIndex(sc_list.GetSize()-1); 3875 keep_die = false; 3876 } 3877 } 3878 } 3879 else 3880 { 3881 GetObjectFile()->GetModule()->ReportWarning ("function at die offset 0x%8.8x had no function type", 3882 die_offset); 3883 } 3884 } 3885 } 3886 } 3887 if (keep_die) 3888 resolved_dies.insert(die); 3889 } 3890 } 3891 else 3892 { 3893 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')", 3894 die_offset, name_cstr); 3895 } 3896 } 3897 die_offsets.clear(); 3898 } 3899 } 3900 } 3901 else 3902 { 3903 3904 // Index the DWARF if we haven't already 3905 if (!m_indexed) 3906 Index (); 3907 3908 if (name_type_mask & eFunctionNameTypeFull) 3909 { 3910 FindFunctions (name, m_function_fullname_index, sc_list); 3911 3912 // FIXME Temporary workaround for global/anonymous namespace 3913 // functions on FreeBSD and Linux 3914 #if defined (__FreeBSD__) || defined (__linux__) 3915 // If we didn't find any functions in the global namespace try 3916 // looking in the basename index but ignore any returned 3917 // functions that have a namespace (ie. mangled names starting with 3918 // '_ZN') but keep functions which have an anonymous namespace 3919 if (sc_list.GetSize() == 0) 3920 { 3921 SymbolContextList temp_sc_list; 3922 FindFunctions (name, m_function_basename_index, temp_sc_list); 3923 if (!namespace_decl) 3924 { 3925 SymbolContext sc; 3926 for (uint32_t i = 0; i < temp_sc_list.GetSize(); i++) 3927 { 3928 if (temp_sc_list.GetContextAtIndex(i, sc)) 3929 { 3930 ConstString mangled_name = sc.GetFunctionName(Mangled::ePreferMangled); 3931 ConstString demangled_name = sc.GetFunctionName(Mangled::ePreferDemangled); 3932 if (strncmp(mangled_name.GetCString(), "_ZN", 3) || 3933 !strncmp(demangled_name.GetCString(), "(anonymous namespace)", 21)) 3934 { 3935 sc_list.Append(sc); 3936 } 3937 } 3938 } 3939 } 3940 } 3941 #endif 3942 } 3943 DIEArray die_offsets; 3944 DWARFCompileUnit *dwarf_cu = NULL; 3945 3946 if (name_type_mask & eFunctionNameTypeBase) 3947 { 3948 uint32_t num_base = m_function_basename_index.Find(name, die_offsets); 3949 for (uint32_t i = 0; i < num_base; i++) 3950 { 3951 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 3952 if (die) 3953 { 3954 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine) 3955 continue; 3956 3957 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 3958 continue; 3959 3960 // If we get to here, the die is good, and we should add it: 3961 if (resolved_dies.find(die) == resolved_dies.end()) 3962 { 3963 if (ResolveFunction (dwarf_cu, die, sc_list)) 3964 resolved_dies.insert(die); 3965 } 3966 } 3967 } 3968 die_offsets.clear(); 3969 } 3970 3971 if (name_type_mask & eFunctionNameTypeMethod) 3972 { 3973 if (namespace_decl && *namespace_decl) 3974 return 0; // no methods in namespaces 3975 3976 uint32_t num_base = m_function_method_index.Find(name, die_offsets); 3977 { 3978 for (uint32_t i = 0; i < num_base; i++) 3979 { 3980 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 3981 if (die) 3982 { 3983 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine) 3984 continue; 3985 3986 // If we get to here, the die is good, and we should add it: 3987 if (resolved_dies.find(die) == resolved_dies.end()) 3988 { 3989 if (ResolveFunction (dwarf_cu, die, sc_list)) 3990 resolved_dies.insert(die); 3991 } 3992 } 3993 } 3994 } 3995 die_offsets.clear(); 3996 } 3997 3998 if ((name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl)) 3999 { 4000 FindFunctions (name, m_function_selector_index, sc_list); 4001 } 4002 4003 } 4004 4005 // Return the number of variable that were appended to the list 4006 const uint32_t num_matches = sc_list.GetSize() - original_size; 4007 4008 if (log && num_matches > 0) 4009 { 4010 GetObjectFile()->GetModule()->LogMessage (log, 4011 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u", 4012 name.GetCString(), 4013 name_type_mask, 4014 append, 4015 num_matches); 4016 } 4017 return num_matches; 4018 } 4019 4020 uint32_t 4021 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list) 4022 { 4023 Timer scoped_timer (__PRETTY_FUNCTION__, 4024 "SymbolFileDWARF::FindFunctions (regex = '%s')", 4025 regex.GetText()); 4026 4027 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 4028 4029 if (log) 4030 { 4031 GetObjectFile()->GetModule()->LogMessage (log, 4032 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)", 4033 regex.GetText(), 4034 append); 4035 } 4036 4037 4038 // If we aren't appending the results to this list, then clear the list 4039 if (!append) 4040 sc_list.Clear(); 4041 4042 // Remember how many sc_list are in the list before we search in case 4043 // we are appending the results to a variable list. 4044 uint32_t original_size = sc_list.GetSize(); 4045 4046 if (m_using_apple_tables) 4047 { 4048 if (m_apple_names_ap.get()) 4049 FindFunctions (regex, *m_apple_names_ap, sc_list); 4050 } 4051 else 4052 { 4053 // Index the DWARF if we haven't already 4054 if (!m_indexed) 4055 Index (); 4056 4057 FindFunctions (regex, m_function_basename_index, sc_list); 4058 4059 FindFunctions (regex, m_function_fullname_index, sc_list); 4060 } 4061 4062 // Return the number of variable that were appended to the list 4063 return sc_list.GetSize() - original_size; 4064 } 4065 4066 uint32_t 4067 SymbolFileDWARF::FindTypes (const SymbolContext& sc, 4068 const ConstString &name, 4069 const lldb_private::ClangNamespaceDecl *namespace_decl, 4070 bool append, 4071 uint32_t max_matches, 4072 TypeList& types) 4073 { 4074 DWARFDebugInfo* info = DebugInfo(); 4075 if (info == NULL) 4076 return 0; 4077 4078 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 4079 4080 if (log) 4081 { 4082 if (namespace_decl) 4083 GetObjectFile()->GetModule()->LogMessage (log, 4084 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)", 4085 name.GetCString(), 4086 static_cast<void*>(namespace_decl->GetNamespaceDecl()), 4087 namespace_decl->GetQualifiedName().c_str(), 4088 append, max_matches); 4089 else 4090 GetObjectFile()->GetModule()->LogMessage (log, 4091 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)", 4092 name.GetCString(), append, 4093 max_matches); 4094 } 4095 4096 // If we aren't appending the results to this list, then clear the list 4097 if (!append) 4098 types.Clear(); 4099 4100 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 4101 return 0; 4102 4103 DIEArray die_offsets; 4104 4105 if (m_using_apple_tables) 4106 { 4107 if (m_apple_types_ap.get()) 4108 { 4109 const char *name_cstr = name.GetCString(); 4110 m_apple_types_ap->FindByName (name_cstr, die_offsets); 4111 } 4112 } 4113 else 4114 { 4115 if (!m_indexed) 4116 Index (); 4117 4118 m_type_index.Find (name, die_offsets); 4119 } 4120 4121 const size_t num_die_matches = die_offsets.size(); 4122 4123 if (num_die_matches) 4124 { 4125 const uint32_t initial_types_size = types.GetSize(); 4126 DWARFCompileUnit* dwarf_cu = NULL; 4127 const DWARFDebugInfoEntry* die = NULL; 4128 DWARFDebugInfo* debug_info = DebugInfo(); 4129 for (size_t i=0; i<num_die_matches; ++i) 4130 { 4131 const dw_offset_t die_offset = die_offsets[i]; 4132 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 4133 4134 if (die) 4135 { 4136 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 4137 continue; 4138 4139 Type *matching_type = ResolveType (dwarf_cu, die); 4140 if (matching_type) 4141 { 4142 // We found a type pointer, now find the shared pointer form our type list 4143 types.InsertUnique (matching_type->shared_from_this()); 4144 if (types.GetSize() >= max_matches) 4145 break; 4146 } 4147 } 4148 else 4149 { 4150 if (m_using_apple_tables) 4151 { 4152 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 4153 die_offset, name.GetCString()); 4154 } 4155 } 4156 4157 } 4158 const uint32_t num_matches = types.GetSize() - initial_types_size; 4159 if (log && num_matches) 4160 { 4161 if (namespace_decl) 4162 { 4163 GetObjectFile()->GetModule()->LogMessage (log, 4164 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u", 4165 name.GetCString(), 4166 static_cast<void*>(namespace_decl->GetNamespaceDecl()), 4167 namespace_decl->GetQualifiedName().c_str(), 4168 append, max_matches, 4169 num_matches); 4170 } 4171 else 4172 { 4173 GetObjectFile()->GetModule()->LogMessage (log, 4174 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u", 4175 name.GetCString(), 4176 append, max_matches, 4177 num_matches); 4178 } 4179 } 4180 return num_matches; 4181 } 4182 return 0; 4183 } 4184 4185 4186 ClangNamespaceDecl 4187 SymbolFileDWARF::FindNamespace (const SymbolContext& sc, 4188 const ConstString &name, 4189 const lldb_private::ClangNamespaceDecl *parent_namespace_decl) 4190 { 4191 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 4192 4193 if (log) 4194 { 4195 GetObjectFile()->GetModule()->LogMessage (log, 4196 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")", 4197 name.GetCString()); 4198 } 4199 4200 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl)) 4201 return ClangNamespaceDecl(); 4202 4203 ClangNamespaceDecl namespace_decl; 4204 DWARFDebugInfo* info = DebugInfo(); 4205 if (info) 4206 { 4207 DIEArray die_offsets; 4208 4209 // Index if we already haven't to make sure the compile units 4210 // get indexed and make their global DIE index list 4211 if (m_using_apple_tables) 4212 { 4213 if (m_apple_namespaces_ap.get()) 4214 { 4215 const char *name_cstr = name.GetCString(); 4216 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets); 4217 } 4218 } 4219 else 4220 { 4221 if (!m_indexed) 4222 Index (); 4223 4224 m_namespace_index.Find (name, die_offsets); 4225 } 4226 4227 DWARFCompileUnit* dwarf_cu = NULL; 4228 const DWARFDebugInfoEntry* die = NULL; 4229 const size_t num_matches = die_offsets.size(); 4230 if (num_matches) 4231 { 4232 DWARFDebugInfo* debug_info = DebugInfo(); 4233 for (size_t i=0; i<num_matches; ++i) 4234 { 4235 const dw_offset_t die_offset = die_offsets[i]; 4236 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 4237 4238 if (die) 4239 { 4240 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die)) 4241 continue; 4242 4243 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die); 4244 if (clang_namespace_decl) 4245 { 4246 namespace_decl.SetASTContext (GetClangASTContext().getASTContext()); 4247 namespace_decl.SetNamespaceDecl (clang_namespace_decl); 4248 break; 4249 } 4250 } 4251 else 4252 { 4253 if (m_using_apple_tables) 4254 { 4255 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n", 4256 die_offset, name.GetCString()); 4257 } 4258 } 4259 4260 } 4261 } 4262 } 4263 if (log && namespace_decl.GetNamespaceDecl()) 4264 { 4265 GetObjectFile()->GetModule()->LogMessage (log, 4266 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"", 4267 name.GetCString(), 4268 static_cast<const void*>(namespace_decl.GetNamespaceDecl()), 4269 namespace_decl.GetQualifiedName().c_str()); 4270 } 4271 4272 return namespace_decl; 4273 } 4274 4275 uint32_t 4276 SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types) 4277 { 4278 // Remember how many sc_list are in the list before we search in case 4279 // we are appending the results to a variable list. 4280 uint32_t original_size = types.GetSize(); 4281 4282 const uint32_t num_die_offsets = die_offsets.size(); 4283 // Parse all of the types we found from the pubtypes matches 4284 uint32_t i; 4285 uint32_t num_matches = 0; 4286 for (i = 0; i < num_die_offsets; ++i) 4287 { 4288 Type *matching_type = ResolveTypeUID (die_offsets[i]); 4289 if (matching_type) 4290 { 4291 // We found a type pointer, now find the shared pointer form our type list 4292 types.InsertUnique (matching_type->shared_from_this()); 4293 ++num_matches; 4294 if (num_matches >= max_matches) 4295 break; 4296 } 4297 } 4298 4299 // Return the number of variable that were appended to the list 4300 return types.GetSize() - original_size; 4301 } 4302 4303 4304 size_t 4305 SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc, 4306 clang::DeclContext *containing_decl_ctx, 4307 DWARFCompileUnit* dwarf_cu, 4308 const DWARFDebugInfoEntry *parent_die, 4309 bool skip_artificial, 4310 bool &is_static, 4311 bool &is_variadic, 4312 TypeList* type_list, 4313 std::vector<ClangASTType>& function_param_types, 4314 std::vector<clang::ParmVarDecl*>& function_param_decls, 4315 unsigned &type_quals) // , 4316 // ClangASTContext::TemplateParameterInfos &template_param_infos)) 4317 { 4318 if (parent_die == NULL) 4319 return 0; 4320 4321 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64()); 4322 4323 size_t arg_idx = 0; 4324 const DWARFDebugInfoEntry *die; 4325 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 4326 { 4327 dw_tag_t tag = die->Tag(); 4328 switch (tag) 4329 { 4330 case DW_TAG_formal_parameter: 4331 { 4332 DWARFDebugInfoEntry::Attributes attributes; 4333 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 4334 if (num_attributes > 0) 4335 { 4336 const char *name = NULL; 4337 Declaration decl; 4338 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET; 4339 bool is_artificial = false; 4340 // one of None, Auto, Register, Extern, Static, PrivateExtern 4341 4342 clang::StorageClass storage = clang::SC_None; 4343 uint32_t i; 4344 for (i=0; i<num_attributes; ++i) 4345 { 4346 const dw_attr_t attr = attributes.AttributeAtIndex(i); 4347 DWARFFormValue form_value; 4348 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4349 { 4350 switch (attr) 4351 { 4352 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4353 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4354 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4355 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 4356 case DW_AT_type: param_type_die_offset = form_value.Reference(); break; 4357 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 4358 case DW_AT_location: 4359 // if (form_value.BlockData()) 4360 // { 4361 // const DWARFDataExtractor& debug_info_data = debug_info(); 4362 // uint32_t block_length = form_value.Unsigned(); 4363 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length); 4364 // } 4365 // else 4366 // { 4367 // } 4368 // break; 4369 case DW_AT_const_value: 4370 case DW_AT_default_value: 4371 case DW_AT_description: 4372 case DW_AT_endianity: 4373 case DW_AT_is_optional: 4374 case DW_AT_segment: 4375 case DW_AT_variable_parameter: 4376 default: 4377 case DW_AT_abstract_origin: 4378 case DW_AT_sibling: 4379 break; 4380 } 4381 } 4382 } 4383 4384 bool skip = false; 4385 if (skip_artificial) 4386 { 4387 if (is_artificial) 4388 { 4389 // In order to determine if a C++ member function is 4390 // "const" we have to look at the const-ness of "this"... 4391 // Ugly, but that 4392 if (arg_idx == 0) 4393 { 4394 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind())) 4395 { 4396 // Often times compilers omit the "this" name for the 4397 // specification DIEs, so we can't rely upon the name 4398 // being in the formal parameter DIE... 4399 if (name == NULL || ::strcmp(name, "this")==0) 4400 { 4401 Type *this_type = ResolveTypeUID (param_type_die_offset); 4402 if (this_type) 4403 { 4404 uint32_t encoding_mask = this_type->GetEncodingMask(); 4405 if (encoding_mask & Type::eEncodingIsPointerUID) 4406 { 4407 is_static = false; 4408 4409 if (encoding_mask & (1u << Type::eEncodingIsConstUID)) 4410 type_quals |= clang::Qualifiers::Const; 4411 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID)) 4412 type_quals |= clang::Qualifiers::Volatile; 4413 } 4414 } 4415 } 4416 } 4417 } 4418 skip = true; 4419 } 4420 else 4421 { 4422 4423 // HACK: Objective C formal parameters "self" and "_cmd" 4424 // are not marked as artificial in the DWARF... 4425 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 4426 if (comp_unit) 4427 { 4428 switch (comp_unit->GetLanguage()) 4429 { 4430 case eLanguageTypeObjC: 4431 case eLanguageTypeObjC_plus_plus: 4432 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0)) 4433 skip = true; 4434 break; 4435 default: 4436 break; 4437 } 4438 } 4439 } 4440 } 4441 4442 if (!skip) 4443 { 4444 Type *type = ResolveTypeUID(param_type_die_offset); 4445 if (type) 4446 { 4447 function_param_types.push_back (type->GetClangForwardType()); 4448 4449 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, 4450 type->GetClangForwardType(), 4451 storage); 4452 assert(param_var_decl); 4453 function_param_decls.push_back(param_var_decl); 4454 4455 GetClangASTContext().SetMetadataAsUserID (param_var_decl, MakeUserID(die->GetOffset())); 4456 } 4457 } 4458 } 4459 arg_idx++; 4460 } 4461 break; 4462 4463 case DW_TAG_unspecified_parameters: 4464 is_variadic = true; 4465 break; 4466 4467 case DW_TAG_template_type_parameter: 4468 case DW_TAG_template_value_parameter: 4469 // The one caller of this was never using the template_param_infos, 4470 // and the local variable was taking up a large amount of stack space 4471 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need 4472 // the template params back, we can add them back. 4473 // ParseTemplateDIE (dwarf_cu, die, template_param_infos); 4474 break; 4475 4476 default: 4477 break; 4478 } 4479 } 4480 return arg_idx; 4481 } 4482 4483 size_t 4484 SymbolFileDWARF::ParseChildEnumerators 4485 ( 4486 const SymbolContext& sc, 4487 lldb_private::ClangASTType &clang_type, 4488 bool is_signed, 4489 uint32_t enumerator_byte_size, 4490 DWARFCompileUnit* dwarf_cu, 4491 const DWARFDebugInfoEntry *parent_die 4492 ) 4493 { 4494 if (parent_die == NULL) 4495 return 0; 4496 4497 size_t enumerators_added = 0; 4498 const DWARFDebugInfoEntry *die; 4499 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64()); 4500 4501 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 4502 { 4503 const dw_tag_t tag = die->Tag(); 4504 if (tag == DW_TAG_enumerator) 4505 { 4506 DWARFDebugInfoEntry::Attributes attributes; 4507 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 4508 if (num_child_attributes > 0) 4509 { 4510 const char *name = NULL; 4511 bool got_value = false; 4512 int64_t enum_value = 0; 4513 Declaration decl; 4514 4515 uint32_t i; 4516 for (i=0; i<num_child_attributes; ++i) 4517 { 4518 const dw_attr_t attr = attributes.AttributeAtIndex(i); 4519 DWARFFormValue form_value; 4520 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4521 { 4522 switch (attr) 4523 { 4524 case DW_AT_const_value: 4525 got_value = true; 4526 if (is_signed) 4527 enum_value = form_value.Signed(); 4528 else 4529 enum_value = form_value.Unsigned(); 4530 break; 4531 4532 case DW_AT_name: 4533 name = form_value.AsCString(&get_debug_str_data()); 4534 break; 4535 4536 case DW_AT_description: 4537 default: 4538 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4539 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4540 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4541 case DW_AT_sibling: 4542 break; 4543 } 4544 } 4545 } 4546 4547 if (name && name[0] && got_value) 4548 { 4549 clang_type.AddEnumerationValueToEnumerationType (clang_type.GetEnumerationIntegerType(), 4550 decl, 4551 name, 4552 enum_value, 4553 enumerator_byte_size * 8); 4554 ++enumerators_added; 4555 } 4556 } 4557 } 4558 } 4559 return enumerators_added; 4560 } 4561 4562 void 4563 SymbolFileDWARF::ParseChildArrayInfo 4564 ( 4565 const SymbolContext& sc, 4566 DWARFCompileUnit* dwarf_cu, 4567 const DWARFDebugInfoEntry *parent_die, 4568 int64_t& first_index, 4569 std::vector<uint64_t>& element_orders, 4570 uint32_t& byte_stride, 4571 uint32_t& bit_stride 4572 ) 4573 { 4574 if (parent_die == NULL) 4575 return; 4576 4577 const DWARFDebugInfoEntry *die; 4578 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize(), dwarf_cu->IsDWARF64()); 4579 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 4580 { 4581 const dw_tag_t tag = die->Tag(); 4582 switch (tag) 4583 { 4584 case DW_TAG_subrange_type: 4585 { 4586 DWARFDebugInfoEntry::Attributes attributes; 4587 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 4588 if (num_child_attributes > 0) 4589 { 4590 uint64_t num_elements = 0; 4591 uint64_t lower_bound = 0; 4592 uint64_t upper_bound = 0; 4593 bool upper_bound_valid = false; 4594 uint32_t i; 4595 for (i=0; i<num_child_attributes; ++i) 4596 { 4597 const dw_attr_t attr = attributes.AttributeAtIndex(i); 4598 DWARFFormValue form_value; 4599 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4600 { 4601 switch (attr) 4602 { 4603 case DW_AT_name: 4604 break; 4605 4606 case DW_AT_count: 4607 num_elements = form_value.Unsigned(); 4608 break; 4609 4610 case DW_AT_bit_stride: 4611 bit_stride = form_value.Unsigned(); 4612 break; 4613 4614 case DW_AT_byte_stride: 4615 byte_stride = form_value.Unsigned(); 4616 break; 4617 4618 case DW_AT_lower_bound: 4619 lower_bound = form_value.Unsigned(); 4620 break; 4621 4622 case DW_AT_upper_bound: 4623 upper_bound_valid = true; 4624 upper_bound = form_value.Unsigned(); 4625 break; 4626 4627 default: 4628 case DW_AT_abstract_origin: 4629 case DW_AT_accessibility: 4630 case DW_AT_allocated: 4631 case DW_AT_associated: 4632 case DW_AT_data_location: 4633 case DW_AT_declaration: 4634 case DW_AT_description: 4635 case DW_AT_sibling: 4636 case DW_AT_threads_scaled: 4637 case DW_AT_type: 4638 case DW_AT_visibility: 4639 break; 4640 } 4641 } 4642 } 4643 4644 if (num_elements == 0) 4645 { 4646 if (upper_bound_valid && upper_bound >= lower_bound) 4647 num_elements = upper_bound - lower_bound + 1; 4648 } 4649 4650 element_orders.push_back (num_elements); 4651 } 4652 } 4653 break; 4654 } 4655 } 4656 } 4657 4658 TypeSP 4659 SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die) 4660 { 4661 TypeSP type_sp; 4662 if (die != NULL) 4663 { 4664 assert(dwarf_cu != NULL); 4665 Type *type_ptr = m_die_to_type.lookup (die); 4666 if (type_ptr == NULL) 4667 { 4668 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu); 4669 assert (lldb_cu); 4670 SymbolContext sc(lldb_cu); 4671 type_sp = ParseType(sc, dwarf_cu, die, NULL); 4672 } 4673 else if (type_ptr != DIE_IS_BEING_PARSED) 4674 { 4675 // Grab the existing type from the master types lists 4676 type_sp = type_ptr->shared_from_this(); 4677 } 4678 4679 } 4680 return type_sp; 4681 } 4682 4683 clang::DeclContext * 4684 SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset) 4685 { 4686 if (die_offset != DW_INVALID_OFFSET) 4687 { 4688 DWARFCompileUnitSP cu_sp; 4689 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp); 4690 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL); 4691 } 4692 return NULL; 4693 } 4694 4695 clang::DeclContext * 4696 SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset) 4697 { 4698 if (die_offset != DW_INVALID_OFFSET) 4699 { 4700 DWARFDebugInfo* debug_info = DebugInfo(); 4701 if (debug_info) 4702 { 4703 DWARFCompileUnitSP cu_sp; 4704 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp); 4705 if (die) 4706 return GetClangDeclContextForDIE (sc, cu_sp.get(), die); 4707 } 4708 } 4709 return NULL; 4710 } 4711 4712 clang::NamespaceDecl * 4713 SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die) 4714 { 4715 if (die && die->Tag() == DW_TAG_namespace) 4716 { 4717 // See if we already parsed this namespace DIE and associated it with a 4718 // uniqued namespace declaration 4719 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]); 4720 if (namespace_decl) 4721 return namespace_decl; 4722 else 4723 { 4724 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL); 4725 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL); 4726 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx); 4727 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 4728 if (log) 4729 { 4730 if (namespace_name) 4731 { 4732 GetObjectFile()->GetModule()->LogMessage (log, 4733 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)", 4734 static_cast<void*>(GetClangASTContext().getASTContext()), 4735 MakeUserID(die->GetOffset()), 4736 namespace_name, 4737 static_cast<void*>(namespace_decl), 4738 static_cast<void*>(namespace_decl->getOriginalNamespace())); 4739 } 4740 else 4741 { 4742 GetObjectFile()->GetModule()->LogMessage (log, 4743 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)", 4744 static_cast<void*>(GetClangASTContext().getASTContext()), 4745 MakeUserID(die->GetOffset()), 4746 static_cast<void*>(namespace_decl), 4747 static_cast<void*>(namespace_decl->getOriginalNamespace())); 4748 } 4749 } 4750 4751 if (namespace_decl) 4752 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die); 4753 return namespace_decl; 4754 } 4755 } 4756 return NULL; 4757 } 4758 4759 clang::DeclContext * 4760 SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die) 4761 { 4762 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die); 4763 if (clang_decl_ctx) 4764 return clang_decl_ctx; 4765 // If this DIE has a specification, or an abstract origin, then trace to those. 4766 4767 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET); 4768 if (die_offset != DW_INVALID_OFFSET) 4769 return GetClangDeclContextForDIEOffset (sc, die_offset); 4770 4771 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET); 4772 if (die_offset != DW_INVALID_OFFSET) 4773 return GetClangDeclContextForDIEOffset (sc, die_offset); 4774 4775 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 4776 if (log) 4777 GetObjectFile()->GetModule()->LogMessage(log, "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(this, cu)); 4778 // This is the DIE we want. Parse it, then query our map. 4779 bool assert_not_being_parsed = true; 4780 ResolveTypeUID (cu, die, assert_not_being_parsed); 4781 4782 clang_decl_ctx = GetCachedClangDeclContextForDIE (die); 4783 4784 return clang_decl_ctx; 4785 } 4786 4787 clang::DeclContext * 4788 SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy) 4789 { 4790 if (m_clang_tu_decl == NULL) 4791 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl(); 4792 4793 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die); 4794 4795 if (decl_ctx_die_copy) 4796 *decl_ctx_die_copy = decl_ctx_die; 4797 4798 if (decl_ctx_die) 4799 { 4800 4801 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die); 4802 if (pos != m_die_to_decl_ctx.end()) 4803 return pos->second; 4804 4805 switch (decl_ctx_die->Tag()) 4806 { 4807 case DW_TAG_compile_unit: 4808 return m_clang_tu_decl; 4809 4810 case DW_TAG_namespace: 4811 return ResolveNamespaceDIE (cu, decl_ctx_die); 4812 break; 4813 4814 case DW_TAG_structure_type: 4815 case DW_TAG_union_type: 4816 case DW_TAG_class_type: 4817 { 4818 Type* type = ResolveType (cu, decl_ctx_die); 4819 if (type) 4820 { 4821 clang::DeclContext *decl_ctx = type->GetClangForwardType().GetDeclContextForType (); 4822 if (decl_ctx) 4823 { 4824 LinkDeclContextToDIE (decl_ctx, decl_ctx_die); 4825 if (decl_ctx) 4826 return decl_ctx; 4827 } 4828 } 4829 } 4830 break; 4831 4832 default: 4833 break; 4834 } 4835 } 4836 return m_clang_tu_decl; 4837 } 4838 4839 4840 const DWARFDebugInfoEntry * 4841 SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die) 4842 { 4843 if (cu && die) 4844 { 4845 const DWARFDebugInfoEntry * const decl_die = die; 4846 4847 while (die != NULL) 4848 { 4849 // If this is the original DIE that we are searching for a declaration 4850 // for, then don't look in the cache as we don't want our own decl 4851 // context to be our decl context... 4852 if (decl_die != die) 4853 { 4854 switch (die->Tag()) 4855 { 4856 case DW_TAG_compile_unit: 4857 case DW_TAG_namespace: 4858 case DW_TAG_structure_type: 4859 case DW_TAG_union_type: 4860 case DW_TAG_class_type: 4861 return die; 4862 4863 default: 4864 break; 4865 } 4866 } 4867 4868 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET); 4869 if (die_offset != DW_INVALID_OFFSET) 4870 { 4871 DWARFCompileUnit *spec_cu = cu; 4872 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu); 4873 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die); 4874 if (spec_die_decl_ctx_die) 4875 return spec_die_decl_ctx_die; 4876 } 4877 4878 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET); 4879 if (die_offset != DW_INVALID_OFFSET) 4880 { 4881 DWARFCompileUnit *abs_cu = cu; 4882 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu); 4883 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die); 4884 if (abs_die_decl_ctx_die) 4885 return abs_die_decl_ctx_die; 4886 } 4887 4888 die = die->GetParent(); 4889 } 4890 } 4891 return NULL; 4892 } 4893 4894 4895 Symbol * 4896 SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name) 4897 { 4898 Symbol *objc_class_symbol = NULL; 4899 if (m_obj_file) 4900 { 4901 Symtab *symtab = m_obj_file->GetSymtab (); 4902 if (symtab) 4903 { 4904 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name, 4905 eSymbolTypeObjCClass, 4906 Symtab::eDebugNo, 4907 Symtab::eVisibilityAny); 4908 } 4909 } 4910 return objc_class_symbol; 4911 } 4912 4913 // Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't 4914 // then we can end up looking through all class types for a complete type and never find 4915 // the full definition. We need to know if this attribute is supported, so we determine 4916 // this here and cache th result. We also need to worry about the debug map DWARF file 4917 // if we are doing darwin DWARF in .o file debugging. 4918 bool 4919 SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu) 4920 { 4921 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) 4922 { 4923 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; 4924 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type()) 4925 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 4926 else 4927 { 4928 DWARFDebugInfo* debug_info = DebugInfo(); 4929 const uint32_t num_compile_units = GetNumCompileUnits(); 4930 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 4931 { 4932 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 4933 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) 4934 { 4935 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 4936 break; 4937 } 4938 } 4939 } 4940 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ()) 4941 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this); 4942 } 4943 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; 4944 } 4945 4946 // This function can be used when a DIE is found that is a forward declaration 4947 // DIE and we want to try and find a type that has the complete definition. 4948 TypeSP 4949 SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die, 4950 const ConstString &type_name, 4951 bool must_be_implementation) 4952 { 4953 4954 TypeSP type_sp; 4955 4956 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name))) 4957 return type_sp; 4958 4959 DIEArray die_offsets; 4960 4961 if (m_using_apple_tables) 4962 { 4963 if (m_apple_types_ap.get()) 4964 { 4965 const char *name_cstr = type_name.GetCString(); 4966 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation); 4967 } 4968 } 4969 else 4970 { 4971 if (!m_indexed) 4972 Index (); 4973 4974 m_type_index.Find (type_name, die_offsets); 4975 } 4976 4977 const size_t num_matches = die_offsets.size(); 4978 4979 DWARFCompileUnit* type_cu = NULL; 4980 const DWARFDebugInfoEntry* type_die = NULL; 4981 if (num_matches) 4982 { 4983 DWARFDebugInfo* debug_info = DebugInfo(); 4984 for (size_t i=0; i<num_matches; ++i) 4985 { 4986 const dw_offset_t die_offset = die_offsets[i]; 4987 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu); 4988 4989 if (type_die) 4990 { 4991 bool try_resolving_type = false; 4992 4993 // Don't try and resolve the DIE we are looking for with the DIE itself! 4994 if (type_die != die) 4995 { 4996 switch (type_die->Tag()) 4997 { 4998 case DW_TAG_class_type: 4999 case DW_TAG_structure_type: 5000 try_resolving_type = true; 5001 break; 5002 default: 5003 break; 5004 } 5005 } 5006 5007 if (try_resolving_type) 5008 { 5009 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type()) 5010 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0); 5011 5012 if (try_resolving_type) 5013 { 5014 Type *resolved_type = ResolveType (type_cu, type_die, false); 5015 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 5016 { 5017 DEBUG_PRINTF ("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 " (cu 0x%8.8" PRIx64 ")\n", 5018 MakeUserID(die->GetOffset()), 5019 m_obj_file->GetFileSpec().GetFilename().AsCString("<Unknown>"), 5020 MakeUserID(type_die->GetOffset()), 5021 MakeUserID(type_cu->GetOffset())); 5022 5023 if (die) 5024 m_die_to_type[die] = resolved_type; 5025 type_sp = resolved_type->shared_from_this(); 5026 break; 5027 } 5028 } 5029 } 5030 } 5031 else 5032 { 5033 if (m_using_apple_tables) 5034 { 5035 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 5036 die_offset, type_name.GetCString()); 5037 } 5038 } 5039 5040 } 5041 } 5042 return type_sp; 5043 } 5044 5045 5046 //---------------------------------------------------------------------- 5047 // This function helps to ensure that the declaration contexts match for 5048 // two different DIEs. Often times debug information will refer to a 5049 // forward declaration of a type (the equivalent of "struct my_struct;". 5050 // There will often be a declaration of that type elsewhere that has the 5051 // full definition. When we go looking for the full type "my_struct", we 5052 // will find one or more matches in the accelerator tables and we will 5053 // then need to make sure the type was in the same declaration context 5054 // as the original DIE. This function can efficiently compare two DIEs 5055 // and will return true when the declaration context matches, and false 5056 // when they don't. 5057 //---------------------------------------------------------------------- 5058 bool 5059 SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1, 5060 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2) 5061 { 5062 if (die1 == die2) 5063 return true; 5064 5065 #if defined (LLDB_CONFIGURATION_DEBUG) 5066 // You can't and shouldn't call this function with a compile unit from 5067 // two different SymbolFileDWARF instances. 5068 assert (DebugInfo()->ContainsCompileUnit (cu1)); 5069 assert (DebugInfo()->ContainsCompileUnit (cu2)); 5070 #endif 5071 5072 DWARFDIECollection decl_ctx_1; 5073 DWARFDIECollection decl_ctx_2; 5074 //The declaration DIE stack is a stack of the declaration context 5075 // DIEs all the way back to the compile unit. If a type "T" is 5076 // declared inside a class "B", and class "B" is declared inside 5077 // a class "A" and class "A" is in a namespace "lldb", and the 5078 // namespace is in a compile unit, there will be a stack of DIEs: 5079 // 5080 // [0] DW_TAG_class_type for "B" 5081 // [1] DW_TAG_class_type for "A" 5082 // [2] DW_TAG_namespace for "lldb" 5083 // [3] DW_TAG_compile_unit for the source file. 5084 // 5085 // We grab both contexts and make sure that everything matches 5086 // all the way back to the compiler unit. 5087 5088 // First lets grab the decl contexts for both DIEs 5089 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1); 5090 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2); 5091 // Make sure the context arrays have the same size, otherwise 5092 // we are done 5093 const size_t count1 = decl_ctx_1.Size(); 5094 const size_t count2 = decl_ctx_2.Size(); 5095 if (count1 != count2) 5096 return false; 5097 5098 // Make sure the DW_TAG values match all the way back up the 5099 // compile unit. If they don't, then we are done. 5100 const DWARFDebugInfoEntry *decl_ctx_die1; 5101 const DWARFDebugInfoEntry *decl_ctx_die2; 5102 size_t i; 5103 for (i=0; i<count1; i++) 5104 { 5105 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i); 5106 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i); 5107 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag()) 5108 return false; 5109 } 5110 #if defined LLDB_CONFIGURATION_DEBUG 5111 5112 // Make sure the top item in the decl context die array is always 5113 // DW_TAG_compile_unit. If it isn't then something went wrong in 5114 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function... 5115 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit); 5116 5117 #endif 5118 // Always skip the compile unit when comparing by only iterating up to 5119 // "count - 1". Here we compare the names as we go. 5120 for (i=0; i<count1 - 1; i++) 5121 { 5122 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i); 5123 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i); 5124 const char *name1 = decl_ctx_die1->GetName(this, cu1); 5125 const char *name2 = decl_ctx_die2->GetName(this, cu2); 5126 // If the string was from a DW_FORM_strp, then the pointer will often 5127 // be the same! 5128 if (name1 == name2) 5129 continue; 5130 5131 // Name pointers are not equal, so only compare the strings 5132 // if both are not NULL. 5133 if (name1 && name2) 5134 { 5135 // If the strings don't compare, we are done... 5136 if (strcmp(name1, name2) != 0) 5137 return false; 5138 } 5139 else 5140 { 5141 // One name was NULL while the other wasn't 5142 return false; 5143 } 5144 } 5145 // We made it through all of the checks and the declaration contexts 5146 // are equal. 5147 return true; 5148 } 5149 5150 5151 TypeSP 5152 SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx) 5153 { 5154 TypeSP type_sp; 5155 5156 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize(); 5157 if (dwarf_decl_ctx_count > 0) 5158 { 5159 const ConstString type_name(dwarf_decl_ctx[0].name); 5160 const dw_tag_t tag = dwarf_decl_ctx[0].tag; 5161 5162 if (type_name) 5163 { 5164 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS)); 5165 if (log) 5166 { 5167 GetObjectFile()->GetModule()->LogMessage (log, 5168 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')", 5169 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 5170 dwarf_decl_ctx.GetQualifiedName()); 5171 } 5172 5173 DIEArray die_offsets; 5174 5175 if (m_using_apple_tables) 5176 { 5177 if (m_apple_types_ap.get()) 5178 { 5179 const bool has_tag = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeTag); 5180 const bool has_qualified_name_hash = m_apple_types_ap->GetHeader().header_data.ContainsAtom (DWARFMappedHash::eAtomTypeQualNameHash); 5181 if (has_tag && has_qualified_name_hash) 5182 { 5183 const char *qualified_name = dwarf_decl_ctx.GetQualifiedName(); 5184 const uint32_t qualified_name_hash = MappedHash::HashStringUsingDJB (qualified_name); 5185 if (log) 5186 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTagAndQualifiedNameHash()"); 5187 m_apple_types_ap->FindByNameAndTagAndQualifiedNameHash (type_name.GetCString(), tag, qualified_name_hash, die_offsets); 5188 } 5189 else if (has_tag) 5190 { 5191 if (log) 5192 GetObjectFile()->GetModule()->LogMessage (log,"FindByNameAndTag()"); 5193 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets); 5194 } 5195 else 5196 { 5197 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets); 5198 } 5199 } 5200 } 5201 else 5202 { 5203 if (!m_indexed) 5204 Index (); 5205 5206 m_type_index.Find (type_name, die_offsets); 5207 } 5208 5209 const size_t num_matches = die_offsets.size(); 5210 5211 5212 DWARFCompileUnit* type_cu = NULL; 5213 const DWARFDebugInfoEntry* type_die = NULL; 5214 if (num_matches) 5215 { 5216 DWARFDebugInfo* debug_info = DebugInfo(); 5217 for (size_t i=0; i<num_matches; ++i) 5218 { 5219 const dw_offset_t die_offset = die_offsets[i]; 5220 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu); 5221 5222 if (type_die) 5223 { 5224 bool try_resolving_type = false; 5225 5226 // Don't try and resolve the DIE we are looking for with the DIE itself! 5227 const dw_tag_t type_tag = type_die->Tag(); 5228 // Make sure the tags match 5229 if (type_tag == tag) 5230 { 5231 // The tags match, lets try resolving this type 5232 try_resolving_type = true; 5233 } 5234 else 5235 { 5236 // The tags don't match, but we need to watch our for a 5237 // forward declaration for a struct and ("struct foo") 5238 // ends up being a class ("class foo { ... };") or 5239 // vice versa. 5240 switch (type_tag) 5241 { 5242 case DW_TAG_class_type: 5243 // We had a "class foo", see if we ended up with a "struct foo { ... };" 5244 try_resolving_type = (tag == DW_TAG_structure_type); 5245 break; 5246 case DW_TAG_structure_type: 5247 // We had a "struct foo", see if we ended up with a "class foo { ... };" 5248 try_resolving_type = (tag == DW_TAG_class_type); 5249 break; 5250 default: 5251 // Tags don't match, don't event try to resolve 5252 // using this type whose name matches.... 5253 break; 5254 } 5255 } 5256 5257 if (try_resolving_type) 5258 { 5259 DWARFDeclContext type_dwarf_decl_ctx; 5260 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx); 5261 5262 if (log) 5263 { 5264 GetObjectFile()->GetModule()->LogMessage (log, 5265 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)", 5266 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 5267 dwarf_decl_ctx.GetQualifiedName(), 5268 type_die->GetOffset(), 5269 type_dwarf_decl_ctx.GetQualifiedName()); 5270 } 5271 5272 // Make sure the decl contexts match all the way up 5273 if (dwarf_decl_ctx == type_dwarf_decl_ctx) 5274 { 5275 Type *resolved_type = ResolveType (type_cu, type_die, false); 5276 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 5277 { 5278 type_sp = resolved_type->shared_from_this(); 5279 break; 5280 } 5281 } 5282 } 5283 else 5284 { 5285 if (log) 5286 { 5287 std::string qualified_name; 5288 type_die->GetQualifiedName(this, type_cu, qualified_name); 5289 GetObjectFile()->GetModule()->LogMessage (log, 5290 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)", 5291 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 5292 dwarf_decl_ctx.GetQualifiedName(), 5293 type_die->GetOffset(), 5294 qualified_name.c_str()); 5295 } 5296 } 5297 } 5298 else 5299 { 5300 if (m_using_apple_tables) 5301 { 5302 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n", 5303 die_offset, type_name.GetCString()); 5304 } 5305 } 5306 5307 } 5308 } 5309 } 5310 } 5311 return type_sp; 5312 } 5313 5314 bool 5315 SymbolFileDWARF::CopyUniqueClassMethodTypes (SymbolFileDWARF *src_symfile, 5316 Type *class_type, 5317 DWARFCompileUnit* src_cu, 5318 const DWARFDebugInfoEntry *src_class_die, 5319 DWARFCompileUnit* dst_cu, 5320 const DWARFDebugInfoEntry *dst_class_die, 5321 DWARFDIECollection &failures) 5322 { 5323 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die) 5324 return false; 5325 if (src_class_die->Tag() != dst_class_die->Tag()) 5326 return false; 5327 5328 // We need to complete the class type so we can get all of the method types 5329 // parsed so we can then unique those types to their equivalent counterparts 5330 // in "dst_cu" and "dst_class_die" 5331 class_type->GetClangFullType(); 5332 5333 const DWARFDebugInfoEntry *src_die; 5334 const DWARFDebugInfoEntry *dst_die; 5335 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die; 5336 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die; 5337 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die_artificial; 5338 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die_artificial; 5339 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling()) 5340 { 5341 if (src_die->Tag() == DW_TAG_subprogram) 5342 { 5343 // Make sure this is a declaration and not a concrete instance by looking 5344 // for DW_AT_declaration set to 1. Sometimes concrete function instances 5345 // are placed inside the class definitions and shouldn't be included in 5346 // the list of things are are tracking here. 5347 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_declaration, 0) == 1) 5348 { 5349 const char *src_name = src_die->GetMangledName (src_symfile, src_cu); 5350 if (src_name) 5351 { 5352 ConstString src_const_name(src_name); 5353 if (src_die->GetAttributeValueAsUnsigned(src_symfile, src_cu, DW_AT_artificial, 0)) 5354 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die); 5355 else 5356 src_name_to_die.Append(src_const_name.GetCString(), src_die); 5357 } 5358 } 5359 } 5360 } 5361 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling()) 5362 { 5363 if (dst_die->Tag() == DW_TAG_subprogram) 5364 { 5365 // Make sure this is a declaration and not a concrete instance by looking 5366 // for DW_AT_declaration set to 1. Sometimes concrete function instances 5367 // are placed inside the class definitions and shouldn't be included in 5368 // the list of things are are tracking here. 5369 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_declaration, 0) == 1) 5370 { 5371 const char *dst_name = dst_die->GetMangledName (this, dst_cu); 5372 if (dst_name) 5373 { 5374 ConstString dst_const_name(dst_name); 5375 if (dst_die->GetAttributeValueAsUnsigned(this, dst_cu, DW_AT_artificial, 0)) 5376 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die); 5377 else 5378 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die); 5379 } 5380 } 5381 } 5382 } 5383 const uint32_t src_size = src_name_to_die.GetSize (); 5384 const uint32_t dst_size = dst_name_to_die.GetSize (); 5385 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION)); 5386 5387 // Is everything kosher so we can go through the members at top speed? 5388 bool fast_path = true; 5389 5390 if (src_size != dst_size) 5391 { 5392 if (src_size != 0 && dst_size != 0) 5393 { 5394 if (log) 5395 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)", 5396 src_class_die->GetOffset(), 5397 dst_class_die->GetOffset(), 5398 src_size, 5399 dst_size); 5400 } 5401 5402 fast_path = false; 5403 } 5404 5405 uint32_t idx; 5406 5407 if (fast_path) 5408 { 5409 for (idx = 0; idx < src_size; ++idx) 5410 { 5411 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx); 5412 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx); 5413 5414 if (src_die->Tag() != dst_die->Tag()) 5415 { 5416 if (log) 5417 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)", 5418 src_class_die->GetOffset(), 5419 dst_class_die->GetOffset(), 5420 src_die->GetOffset(), 5421 DW_TAG_value_to_name(src_die->Tag()), 5422 dst_die->GetOffset(), 5423 DW_TAG_value_to_name(src_die->Tag())); 5424 fast_path = false; 5425 } 5426 5427 const char *src_name = src_die->GetMangledName (src_symfile, src_cu); 5428 const char *dst_name = dst_die->GetMangledName (this, dst_cu); 5429 5430 // Make sure the names match 5431 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0)) 5432 continue; 5433 5434 if (log) 5435 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)", 5436 src_class_die->GetOffset(), 5437 dst_class_die->GetOffset(), 5438 src_die->GetOffset(), 5439 src_name, 5440 dst_die->GetOffset(), 5441 dst_name); 5442 5443 fast_path = false; 5444 } 5445 } 5446 5447 // Now do the work of linking the DeclContexts and Types. 5448 if (fast_path) 5449 { 5450 // We can do this quickly. Just run across the tables index-for-index since 5451 // we know each node has matching names and tags. 5452 for (idx = 0; idx < src_size; ++idx) 5453 { 5454 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx); 5455 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx); 5456 5457 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die]; 5458 if (src_decl_ctx) 5459 { 5460 if (log) 5461 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 5462 static_cast<void*>(src_decl_ctx), 5463 src_die->GetOffset(), dst_die->GetOffset()); 5464 LinkDeclContextToDIE (src_decl_ctx, dst_die); 5465 } 5466 else 5467 { 5468 if (log) 5469 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", 5470 src_die->GetOffset(), dst_die->GetOffset()); 5471 } 5472 5473 Type *src_child_type = m_die_to_type[src_die]; 5474 if (src_child_type) 5475 { 5476 if (log) 5477 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 5478 static_cast<void*>(src_child_type), 5479 src_child_type->GetID(), 5480 src_die->GetOffset(), dst_die->GetOffset()); 5481 m_die_to_type[dst_die] = src_child_type; 5482 } 5483 else 5484 { 5485 if (log) 5486 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset()); 5487 } 5488 } 5489 } 5490 else 5491 { 5492 // We must do this slowly. For each member of the destination, look 5493 // up a member in the source with the same name, check its tag, and 5494 // unique them if everything matches up. Report failures. 5495 5496 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) 5497 { 5498 src_name_to_die.Sort(); 5499 5500 for (idx = 0; idx < dst_size; ++idx) 5501 { 5502 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx); 5503 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx); 5504 src_die = src_name_to_die.Find(dst_name, NULL); 5505 5506 if (src_die && (src_die->Tag() == dst_die->Tag())) 5507 { 5508 clang::DeclContext *src_decl_ctx = src_symfile->m_die_to_decl_ctx[src_die]; 5509 if (src_decl_ctx) 5510 { 5511 if (log) 5512 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 5513 static_cast<void*>(src_decl_ctx), 5514 src_die->GetOffset(), 5515 dst_die->GetOffset()); 5516 LinkDeclContextToDIE (src_decl_ctx, dst_die); 5517 } 5518 else 5519 { 5520 if (log) 5521 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset()); 5522 } 5523 5524 Type *src_child_type = m_die_to_type[src_die]; 5525 if (src_child_type) 5526 { 5527 if (log) 5528 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 5529 static_cast<void*>(src_child_type), 5530 src_child_type->GetID(), 5531 src_die->GetOffset(), 5532 dst_die->GetOffset()); 5533 m_die_to_type[dst_die] = src_child_type; 5534 } 5535 else 5536 { 5537 if (log) 5538 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset()); 5539 } 5540 } 5541 else 5542 { 5543 if (log) 5544 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die->GetOffset()); 5545 5546 failures.Append(dst_die); 5547 } 5548 } 5549 } 5550 } 5551 5552 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize (); 5553 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize (); 5554 5555 UniqueCStringMap<const DWARFDebugInfoEntry *> name_to_die_artificial_not_in_src; 5556 5557 if (src_size_artificial && dst_size_artificial) 5558 { 5559 dst_name_to_die_artificial.Sort(); 5560 5561 for (idx = 0; idx < src_size_artificial; ++idx) 5562 { 5563 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx); 5564 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx); 5565 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, NULL); 5566 5567 if (dst_die) 5568 { 5569 // Both classes have the artificial types, link them 5570 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die]; 5571 if (src_decl_ctx) 5572 { 5573 if (log) 5574 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 5575 static_cast<void*>(src_decl_ctx), 5576 src_die->GetOffset(), dst_die->GetOffset()); 5577 LinkDeclContextToDIE (src_decl_ctx, dst_die); 5578 } 5579 else 5580 { 5581 if (log) 5582 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset()); 5583 } 5584 5585 Type *src_child_type = m_die_to_type[src_die]; 5586 if (src_child_type) 5587 { 5588 if (log) 5589 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 5590 static_cast<void*>(src_child_type), 5591 src_child_type->GetID(), 5592 src_die->GetOffset(), dst_die->GetOffset()); 5593 m_die_to_type[dst_die] = src_child_type; 5594 } 5595 else 5596 { 5597 if (log) 5598 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die->GetOffset(), dst_die->GetOffset()); 5599 } 5600 } 5601 } 5602 } 5603 5604 if (dst_size_artificial) 5605 { 5606 for (idx = 0; idx < dst_size_artificial; ++idx) 5607 { 5608 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx); 5609 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx); 5610 if (log) 5611 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die->GetOffset(), dst_name_artificial); 5612 5613 failures.Append(dst_die); 5614 } 5615 } 5616 5617 return (failures.Size() != 0); 5618 } 5619 5620 TypeSP 5621 SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr) 5622 { 5623 TypeSP type_sp; 5624 5625 if (type_is_new_ptr) 5626 *type_is_new_ptr = false; 5627 5628 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE) 5629 static DIEStack g_die_stack; 5630 DIEStack::ScopedPopper scoped_die_logger(g_die_stack); 5631 #endif 5632 5633 AccessType accessibility = eAccessNone; 5634 if (die != NULL) 5635 { 5636 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 5637 if (log) 5638 { 5639 const DWARFDebugInfoEntry *context_die; 5640 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die); 5641 5642 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')", 5643 die->GetOffset(), 5644 static_cast<void*>(context), 5645 context_die->GetOffset(), 5646 DW_TAG_value_to_name(die->Tag()), 5647 die->GetName(this, dwarf_cu)); 5648 5649 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE) 5650 scoped_die_logger.Push (dwarf_cu, die); 5651 g_die_stack.LogDIEs(log, this); 5652 #endif 5653 } 5654 // 5655 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 5656 // if (log && dwarf_cu) 5657 // { 5658 // StreamString s; 5659 // die->DumpLocation (this, dwarf_cu, s); 5660 // GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData()); 5661 // 5662 // } 5663 5664 Type *type_ptr = m_die_to_type.lookup (die); 5665 TypeList* type_list = GetTypeList(); 5666 if (type_ptr == NULL) 5667 { 5668 ClangASTContext &ast = GetClangASTContext(); 5669 if (type_is_new_ptr) 5670 *type_is_new_ptr = true; 5671 5672 const dw_tag_t tag = die->Tag(); 5673 5674 bool is_forward_declaration = false; 5675 DWARFDebugInfoEntry::Attributes attributes; 5676 const char *type_name_cstr = NULL; 5677 ConstString type_name_const_str; 5678 Type::ResolveState resolve_state = Type::eResolveStateUnresolved; 5679 uint64_t byte_size = 0; 5680 Declaration decl; 5681 5682 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 5683 ClangASTType clang_type; 5684 DWARFFormValue form_value; 5685 5686 dw_attr_t attr; 5687 5688 switch (tag) 5689 { 5690 case DW_TAG_base_type: 5691 case DW_TAG_pointer_type: 5692 case DW_TAG_reference_type: 5693 case DW_TAG_rvalue_reference_type: 5694 case DW_TAG_typedef: 5695 case DW_TAG_const_type: 5696 case DW_TAG_restrict_type: 5697 case DW_TAG_volatile_type: 5698 case DW_TAG_unspecified_type: 5699 { 5700 // Set a bit that lets us know that we are currently parsing this 5701 m_die_to_type[die] = DIE_IS_BEING_PARSED; 5702 5703 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 5704 uint32_t encoding = 0; 5705 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 5706 5707 if (num_attributes > 0) 5708 { 5709 uint32_t i; 5710 for (i=0; i<num_attributes; ++i) 5711 { 5712 attr = attributes.AttributeAtIndex(i); 5713 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 5714 { 5715 switch (attr) 5716 { 5717 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 5718 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 5719 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 5720 case DW_AT_name: 5721 5722 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 5723 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't 5724 // include the "&"... 5725 if (tag == DW_TAG_reference_type) 5726 { 5727 if (strchr (type_name_cstr, '&') == NULL) 5728 type_name_cstr = NULL; 5729 } 5730 if (type_name_cstr) 5731 type_name_const_str.SetCString(type_name_cstr); 5732 break; 5733 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 5734 case DW_AT_encoding: encoding = form_value.Unsigned(); break; 5735 case DW_AT_type: encoding_uid = form_value.Reference(); break; 5736 default: 5737 case DW_AT_sibling: 5738 break; 5739 } 5740 } 5741 } 5742 } 5743 5744 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid); 5745 5746 switch (tag) 5747 { 5748 default: 5749 break; 5750 5751 case DW_TAG_unspecified_type: 5752 if (strcmp(type_name_cstr, "nullptr_t") == 0 || 5753 strcmp(type_name_cstr, "decltype(nullptr)") == 0 ) 5754 { 5755 resolve_state = Type::eResolveStateFull; 5756 clang_type = ast.GetBasicType(eBasicTypeNullPtr); 5757 break; 5758 } 5759 // Fall through to base type below in case we can handle the type there... 5760 5761 case DW_TAG_base_type: 5762 resolve_state = Type::eResolveStateFull; 5763 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr, 5764 encoding, 5765 byte_size * 8); 5766 break; 5767 5768 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break; 5769 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break; 5770 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break; 5771 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break; 5772 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break; 5773 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break; 5774 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break; 5775 } 5776 5777 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL) 5778 { 5779 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus); 5780 5781 if (translation_unit_is_objc) 5782 { 5783 if (type_name_cstr != NULL) 5784 { 5785 static ConstString g_objc_type_name_id("id"); 5786 static ConstString g_objc_type_name_Class("Class"); 5787 static ConstString g_objc_type_name_selector("SEL"); 5788 5789 if (type_name_const_str == g_objc_type_name_id) 5790 { 5791 if (log) 5792 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.", 5793 die->GetOffset(), 5794 DW_TAG_value_to_name(die->Tag()), 5795 die->GetName(this, dwarf_cu)); 5796 clang_type = ast.GetBasicType(eBasicTypeObjCID); 5797 encoding_data_type = Type::eEncodingIsUID; 5798 encoding_uid = LLDB_INVALID_UID; 5799 resolve_state = Type::eResolveStateFull; 5800 5801 } 5802 else if (type_name_const_str == g_objc_type_name_Class) 5803 { 5804 if (log) 5805 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.", 5806 die->GetOffset(), 5807 DW_TAG_value_to_name(die->Tag()), 5808 die->GetName(this, dwarf_cu)); 5809 clang_type = ast.GetBasicType(eBasicTypeObjCClass); 5810 encoding_data_type = Type::eEncodingIsUID; 5811 encoding_uid = LLDB_INVALID_UID; 5812 resolve_state = Type::eResolveStateFull; 5813 } 5814 else if (type_name_const_str == g_objc_type_name_selector) 5815 { 5816 if (log) 5817 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.", 5818 die->GetOffset(), 5819 DW_TAG_value_to_name(die->Tag()), 5820 die->GetName(this, dwarf_cu)); 5821 clang_type = ast.GetBasicType(eBasicTypeObjCSel); 5822 encoding_data_type = Type::eEncodingIsUID; 5823 encoding_uid = LLDB_INVALID_UID; 5824 resolve_state = Type::eResolveStateFull; 5825 } 5826 } 5827 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID) 5828 { 5829 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id". 5830 5831 DWARFDebugInfoEntry* encoding_die = dwarf_cu->GetDIEPtr(encoding_uid); 5832 5833 if (encoding_die && encoding_die->Tag() == DW_TAG_structure_type) 5834 { 5835 if (const char *struct_name = encoding_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL)) 5836 { 5837 if (!strcmp(struct_name, "objc_object")) 5838 { 5839 if (log) 5840 GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.", 5841 die->GetOffset(), 5842 DW_TAG_value_to_name(die->Tag()), 5843 die->GetName(this, dwarf_cu)); 5844 clang_type = ast.GetBasicType(eBasicTypeObjCID); 5845 encoding_data_type = Type::eEncodingIsUID; 5846 encoding_uid = LLDB_INVALID_UID; 5847 resolve_state = Type::eResolveStateFull; 5848 } 5849 } 5850 } 5851 } 5852 } 5853 } 5854 5855 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 5856 this, 5857 type_name_const_str, 5858 byte_size, 5859 NULL, 5860 encoding_uid, 5861 encoding_data_type, 5862 &decl, 5863 clang_type, 5864 resolve_state)); 5865 5866 m_die_to_type[die] = type_sp.get(); 5867 5868 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false); 5869 // if (encoding_type != NULL) 5870 // { 5871 // if (encoding_type != DIE_IS_BEING_PARSED) 5872 // type_sp->SetEncodingType(encoding_type); 5873 // else 5874 // m_indirect_fixups.push_back(type_sp.get()); 5875 // } 5876 } 5877 break; 5878 5879 case DW_TAG_structure_type: 5880 case DW_TAG_union_type: 5881 case DW_TAG_class_type: 5882 { 5883 // Set a bit that lets us know that we are currently parsing this 5884 m_die_to_type[die] = DIE_IS_BEING_PARSED; 5885 bool byte_size_valid = false; 5886 5887 LanguageType class_language = eLanguageTypeUnknown; 5888 bool is_complete_objc_class = false; 5889 //bool struct_is_class = false; 5890 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 5891 if (num_attributes > 0) 5892 { 5893 uint32_t i; 5894 for (i=0; i<num_attributes; ++i) 5895 { 5896 attr = attributes.AttributeAtIndex(i); 5897 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 5898 { 5899 switch (attr) 5900 { 5901 case DW_AT_decl_file: 5902 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid()) 5903 { 5904 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always 5905 // point to the compile unit file, so we clear this invalid value 5906 // so that we can still unique types efficiently. 5907 decl.SetFile(FileSpec ("<invalid>", false)); 5908 } 5909 else 5910 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); 5911 break; 5912 5913 case DW_AT_decl_line: 5914 decl.SetLine(form_value.Unsigned()); 5915 break; 5916 5917 case DW_AT_decl_column: 5918 decl.SetColumn(form_value.Unsigned()); 5919 break; 5920 5921 case DW_AT_name: 5922 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 5923 type_name_const_str.SetCString(type_name_cstr); 5924 break; 5925 5926 case DW_AT_byte_size: 5927 byte_size = form_value.Unsigned(); 5928 byte_size_valid = true; 5929 break; 5930 5931 case DW_AT_accessibility: 5932 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 5933 break; 5934 5935 case DW_AT_declaration: 5936 is_forward_declaration = form_value.Boolean(); 5937 break; 5938 5939 case DW_AT_APPLE_runtime_class: 5940 class_language = (LanguageType)form_value.Signed(); 5941 break; 5942 5943 case DW_AT_APPLE_objc_complete_type: 5944 is_complete_objc_class = form_value.Signed(); 5945 break; 5946 5947 case DW_AT_allocated: 5948 case DW_AT_associated: 5949 case DW_AT_data_location: 5950 case DW_AT_description: 5951 case DW_AT_start_scope: 5952 case DW_AT_visibility: 5953 default: 5954 case DW_AT_sibling: 5955 break; 5956 } 5957 } 5958 } 5959 } 5960 5961 // UniqueDWARFASTType is large, so don't create a local variables on the 5962 // stack, put it on the heap. This function is often called recursively 5963 // and clang isn't good and sharing the stack space for variables in different blocks. 5964 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType()); 5965 5966 // Only try and unique the type if it has a name. 5967 if (type_name_const_str && 5968 GetUniqueDWARFASTTypeMap().Find (type_name_const_str, 5969 this, 5970 dwarf_cu, 5971 die, 5972 decl, 5973 byte_size_valid ? byte_size : -1, 5974 *unique_ast_entry_ap)) 5975 { 5976 // We have already parsed this type or from another 5977 // compile unit. GCC loves to use the "one definition 5978 // rule" which can result in multiple definitions 5979 // of the same class over and over in each compile 5980 // unit. 5981 type_sp = unique_ast_entry_ap->m_type_sp; 5982 if (type_sp) 5983 { 5984 m_die_to_type[die] = type_sp.get(); 5985 return type_sp; 5986 } 5987 } 5988 5989 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 5990 5991 int tag_decl_kind = -1; 5992 AccessType default_accessibility = eAccessNone; 5993 if (tag == DW_TAG_structure_type) 5994 { 5995 tag_decl_kind = clang::TTK_Struct; 5996 default_accessibility = eAccessPublic; 5997 } 5998 else if (tag == DW_TAG_union_type) 5999 { 6000 tag_decl_kind = clang::TTK_Union; 6001 default_accessibility = eAccessPublic; 6002 } 6003 else if (tag == DW_TAG_class_type) 6004 { 6005 tag_decl_kind = clang::TTK_Class; 6006 default_accessibility = eAccessPrivate; 6007 } 6008 6009 if (byte_size_valid && byte_size == 0 && type_name_cstr && 6010 die->HasChildren() == false && 6011 sc.comp_unit->GetLanguage() == eLanguageTypeObjC) 6012 { 6013 // Work around an issue with clang at the moment where 6014 // forward declarations for objective C classes are emitted 6015 // as: 6016 // DW_TAG_structure_type [2] 6017 // DW_AT_name( "ForwardObjcClass" ) 6018 // DW_AT_byte_size( 0x00 ) 6019 // DW_AT_decl_file( "..." ) 6020 // DW_AT_decl_line( 1 ) 6021 // 6022 // Note that there is no DW_AT_declaration and there are 6023 // no children, and the byte size is zero. 6024 is_forward_declaration = true; 6025 } 6026 6027 if (class_language == eLanguageTypeObjC || 6028 class_language == eLanguageTypeObjC_plus_plus) 6029 { 6030 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu)) 6031 { 6032 // We have a valid eSymbolTypeObjCClass class symbol whose 6033 // name matches the current objective C class that we 6034 // are trying to find and this DIE isn't the complete 6035 // definition (we checked is_complete_objc_class above and 6036 // know it is false), so the real definition is in here somewhere 6037 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true); 6038 6039 if (!type_sp && GetDebugMapSymfile ()) 6040 { 6041 // We weren't able to find a full declaration in 6042 // this DWARF, see if we have a declaration anywhere 6043 // else... 6044 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true); 6045 } 6046 6047 if (type_sp) 6048 { 6049 if (log) 6050 { 6051 GetObjectFile()->GetModule()->LogMessage (log, 6052 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64, 6053 static_cast<void*>(this), 6054 die->GetOffset(), 6055 DW_TAG_value_to_name(tag), 6056 type_name_cstr, 6057 type_sp->GetID()); 6058 } 6059 6060 // We found a real definition for this type elsewhere 6061 // so lets use it and cache the fact that we found 6062 // a complete type for this die 6063 m_die_to_type[die] = type_sp.get(); 6064 return type_sp; 6065 } 6066 } 6067 } 6068 6069 6070 if (is_forward_declaration) 6071 { 6072 // We have a forward declaration to a type and we need 6073 // to try and find a full declaration. We look in the 6074 // current type index just in case we have a forward 6075 // declaration followed by an actual declarations in the 6076 // DWARF. If this fails, we need to look elsewhere... 6077 if (log) 6078 { 6079 GetObjectFile()->GetModule()->LogMessage (log, 6080 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type", 6081 static_cast<void*>(this), 6082 die->GetOffset(), 6083 DW_TAG_value_to_name(tag), 6084 type_name_cstr); 6085 } 6086 6087 DWARFDeclContext die_decl_ctx; 6088 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx); 6089 6090 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 6091 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx); 6092 6093 if (!type_sp && GetDebugMapSymfile ()) 6094 { 6095 // We weren't able to find a full declaration in 6096 // this DWARF, see if we have a declaration anywhere 6097 // else... 6098 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx); 6099 } 6100 6101 if (type_sp) 6102 { 6103 if (log) 6104 { 6105 GetObjectFile()->GetModule()->LogMessage (log, 6106 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64, 6107 static_cast<void*>(this), 6108 die->GetOffset(), 6109 DW_TAG_value_to_name(tag), 6110 type_name_cstr, 6111 type_sp->GetID()); 6112 } 6113 6114 // We found a real definition for this type elsewhere 6115 // so lets use it and cache the fact that we found 6116 // a complete type for this die 6117 m_die_to_type[die] = type_sp.get(); 6118 return type_sp; 6119 } 6120 } 6121 assert (tag_decl_kind != -1); 6122 bool clang_type_was_created = false; 6123 clang_type.SetClangType(ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die)); 6124 if (!clang_type) 6125 { 6126 const DWARFDebugInfoEntry *decl_ctx_die; 6127 6128 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die); 6129 if (accessibility == eAccessNone && decl_ctx) 6130 { 6131 // Check the decl context that contains this class/struct/union. 6132 // If it is a class we must give it an accessibility. 6133 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind(); 6134 if (DeclKindIsCXXClass (containing_decl_kind)) 6135 accessibility = default_accessibility; 6136 } 6137 6138 ClangASTMetadata metadata; 6139 metadata.SetUserID(MakeUserID(die->GetOffset())); 6140 metadata.SetIsDynamicCXXType(ClassOrStructIsVirtual (dwarf_cu, die)); 6141 6142 if (type_name_cstr && strchr (type_name_cstr, '<')) 6143 { 6144 ClangASTContext::TemplateParameterInfos template_param_infos; 6145 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos)) 6146 { 6147 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx, 6148 accessibility, 6149 type_name_cstr, 6150 tag_decl_kind, 6151 template_param_infos); 6152 6153 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx, 6154 class_template_decl, 6155 tag_decl_kind, 6156 template_param_infos); 6157 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl); 6158 clang_type_was_created = true; 6159 6160 GetClangASTContext().SetMetadata (class_template_decl, metadata); 6161 GetClangASTContext().SetMetadata (class_specialization_decl, metadata); 6162 } 6163 } 6164 6165 if (!clang_type_was_created) 6166 { 6167 clang_type_was_created = true; 6168 clang_type = ast.CreateRecordType (decl_ctx, 6169 accessibility, 6170 type_name_cstr, 6171 tag_decl_kind, 6172 class_language, 6173 &metadata); 6174 } 6175 } 6176 6177 // Store a forward declaration to this class type in case any 6178 // parameters in any class methods need it for the clang 6179 // types for function prototypes. 6180 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die); 6181 type_sp.reset (new Type (MakeUserID(die->GetOffset()), 6182 this, 6183 type_name_const_str, 6184 byte_size, 6185 NULL, 6186 LLDB_INVALID_UID, 6187 Type::eEncodingIsUID, 6188 &decl, 6189 clang_type, 6190 Type::eResolveStateForward)); 6191 6192 type_sp->SetIsCompleteObjCClass(is_complete_objc_class); 6193 6194 6195 // Add our type to the unique type map so we don't 6196 // end up creating many copies of the same type over 6197 // and over in the ASTContext for our module 6198 unique_ast_entry_ap->m_type_sp = type_sp; 6199 unique_ast_entry_ap->m_symfile = this; 6200 unique_ast_entry_ap->m_cu = dwarf_cu; 6201 unique_ast_entry_ap->m_die = die; 6202 unique_ast_entry_ap->m_declaration = decl; 6203 unique_ast_entry_ap->m_byte_size = byte_size; 6204 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str, 6205 *unique_ast_entry_ap); 6206 6207 if (is_forward_declaration && die->HasChildren()) 6208 { 6209 // Check to see if the DIE actually has a definition, some version of GCC will 6210 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram, 6211 // members, or inheritance, so we can't trust it 6212 const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); 6213 while (child_die) 6214 { 6215 switch (child_die->Tag()) 6216 { 6217 case DW_TAG_inheritance: 6218 case DW_TAG_subprogram: 6219 case DW_TAG_member: 6220 case DW_TAG_APPLE_property: 6221 case DW_TAG_class_type: 6222 case DW_TAG_structure_type: 6223 case DW_TAG_enumeration_type: 6224 case DW_TAG_typedef: 6225 case DW_TAG_union_type: 6226 child_die = NULL; 6227 is_forward_declaration = false; 6228 break; 6229 default: 6230 child_die = child_die->GetSibling(); 6231 break; 6232 } 6233 } 6234 } 6235 6236 if (!is_forward_declaration) 6237 { 6238 // Always start the definition for a class type so that 6239 // if the class has child classes or types that require 6240 // the class to be created for use as their decl contexts 6241 // the class will be ready to accept these child definitions. 6242 if (die->HasChildren() == false) 6243 { 6244 // No children for this struct/union/class, lets finish it 6245 clang_type.StartTagDeclarationDefinition (); 6246 clang_type.CompleteTagDeclarationDefinition (); 6247 6248 if (tag == DW_TAG_structure_type) // this only applies in C 6249 { 6250 clang::RecordDecl *record_decl = clang_type.GetAsRecordDecl(); 6251 6252 if (record_decl) 6253 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo())); 6254 } 6255 } 6256 else if (clang_type_was_created) 6257 { 6258 // Start the definition if the class is not objective C since 6259 // the underlying decls respond to isCompleteDefinition(). Objective 6260 // C decls don't respond to isCompleteDefinition() so we can't 6261 // start the declaration definition right away. For C++ class/union/structs 6262 // we want to start the definition in case the class is needed as the 6263 // declaration context for a contained class or type without the need 6264 // to complete that type.. 6265 6266 if (class_language != eLanguageTypeObjC && 6267 class_language != eLanguageTypeObjC_plus_plus) 6268 clang_type.StartTagDeclarationDefinition (); 6269 6270 // Leave this as a forward declaration until we need 6271 // to know the details of the type. lldb_private::Type 6272 // will automatically call the SymbolFile virtual function 6273 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)" 6274 // When the definition needs to be defined. 6275 m_forward_decl_die_to_clang_type[die] = clang_type.GetOpaqueQualType(); 6276 m_forward_decl_clang_type_to_die[clang_type.RemoveFastQualifiers().GetOpaqueQualType()] = die; 6277 clang_type.SetHasExternalStorage (true); 6278 } 6279 } 6280 6281 } 6282 break; 6283 6284 case DW_TAG_enumeration_type: 6285 { 6286 // Set a bit that lets us know that we are currently parsing this 6287 m_die_to_type[die] = DIE_IS_BEING_PARSED; 6288 6289 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET; 6290 6291 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 6292 if (num_attributes > 0) 6293 { 6294 uint32_t i; 6295 6296 for (i=0; i<num_attributes; ++i) 6297 { 6298 attr = attributes.AttributeAtIndex(i); 6299 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 6300 { 6301 switch (attr) 6302 { 6303 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 6304 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 6305 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 6306 case DW_AT_name: 6307 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 6308 type_name_const_str.SetCString(type_name_cstr); 6309 break; 6310 case DW_AT_type: encoding_uid = form_value.Reference(); break; 6311 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 6312 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 6313 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break; 6314 case DW_AT_allocated: 6315 case DW_AT_associated: 6316 case DW_AT_bit_stride: 6317 case DW_AT_byte_stride: 6318 case DW_AT_data_location: 6319 case DW_AT_description: 6320 case DW_AT_start_scope: 6321 case DW_AT_visibility: 6322 case DW_AT_specification: 6323 case DW_AT_abstract_origin: 6324 case DW_AT_sibling: 6325 break; 6326 } 6327 } 6328 } 6329 6330 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 6331 6332 ClangASTType enumerator_clang_type; 6333 clang_type.SetClangType (ast.getASTContext(), m_forward_decl_die_to_clang_type.lookup (die)); 6334 if (!clang_type) 6335 { 6336 if (encoding_uid != DW_INVALID_OFFSET) 6337 { 6338 Type *enumerator_type = ResolveTypeUID(encoding_uid); 6339 if (enumerator_type) 6340 enumerator_clang_type = enumerator_type->GetClangFullType(); 6341 } 6342 6343 if (!enumerator_clang_type) 6344 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL, 6345 DW_ATE_signed, 6346 byte_size * 8); 6347 6348 clang_type = ast.CreateEnumerationType (type_name_cstr, 6349 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL), 6350 decl, 6351 enumerator_clang_type); 6352 } 6353 else 6354 { 6355 enumerator_clang_type = clang_type.GetEnumerationIntegerType (); 6356 } 6357 6358 LinkDeclContextToDIE(clang_type.GetDeclContextForType(), die); 6359 6360 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 6361 this, 6362 type_name_const_str, 6363 byte_size, 6364 NULL, 6365 encoding_uid, 6366 Type::eEncodingIsUID, 6367 &decl, 6368 clang_type, 6369 Type::eResolveStateForward)); 6370 6371 clang_type.StartTagDeclarationDefinition (); 6372 if (die->HasChildren()) 6373 { 6374 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu)); 6375 bool is_signed = false; 6376 enumerator_clang_type.IsIntegerType(is_signed); 6377 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), dwarf_cu, die); 6378 } 6379 clang_type.CompleteTagDeclarationDefinition (); 6380 } 6381 } 6382 break; 6383 6384 case DW_TAG_inlined_subroutine: 6385 case DW_TAG_subprogram: 6386 case DW_TAG_subroutine_type: 6387 { 6388 // Set a bit that lets us know that we are currently parsing this 6389 m_die_to_type[die] = DIE_IS_BEING_PARSED; 6390 6391 //const char *mangled = NULL; 6392 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 6393 bool is_variadic = false; 6394 bool is_inline = false; 6395 bool is_static = false; 6396 bool is_virtual = false; 6397 bool is_explicit = false; 6398 bool is_artificial = false; 6399 dw_offset_t specification_die_offset = DW_INVALID_OFFSET; 6400 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET; 6401 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET; 6402 6403 unsigned type_quals = 0; 6404 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern 6405 6406 6407 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 6408 if (num_attributes > 0) 6409 { 6410 uint32_t i; 6411 for (i=0; i<num_attributes; ++i) 6412 { 6413 attr = attributes.AttributeAtIndex(i); 6414 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 6415 { 6416 switch (attr) 6417 { 6418 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 6419 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 6420 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 6421 case DW_AT_name: 6422 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 6423 type_name_const_str.SetCString(type_name_cstr); 6424 break; 6425 6426 case DW_AT_linkage_name: 6427 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break; 6428 case DW_AT_type: type_die_offset = form_value.Reference(); break; 6429 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 6430 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break; 6431 case DW_AT_inline: is_inline = form_value.Boolean(); break; 6432 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break; 6433 case DW_AT_explicit: is_explicit = form_value.Boolean(); break; 6434 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 6435 6436 6437 case DW_AT_external: 6438 if (form_value.Unsigned()) 6439 { 6440 if (storage == clang::SC_None) 6441 storage = clang::SC_Extern; 6442 else 6443 storage = clang::SC_PrivateExtern; 6444 } 6445 break; 6446 6447 case DW_AT_specification: 6448 specification_die_offset = form_value.Reference(); 6449 break; 6450 6451 case DW_AT_abstract_origin: 6452 abstract_origin_die_offset = form_value.Reference(); 6453 break; 6454 6455 case DW_AT_object_pointer: 6456 object_pointer_die_offset = form_value.Reference(); 6457 break; 6458 6459 case DW_AT_allocated: 6460 case DW_AT_associated: 6461 case DW_AT_address_class: 6462 case DW_AT_calling_convention: 6463 case DW_AT_data_location: 6464 case DW_AT_elemental: 6465 case DW_AT_entry_pc: 6466 case DW_AT_frame_base: 6467 case DW_AT_high_pc: 6468 case DW_AT_low_pc: 6469 case DW_AT_prototyped: 6470 case DW_AT_pure: 6471 case DW_AT_ranges: 6472 case DW_AT_recursive: 6473 case DW_AT_return_addr: 6474 case DW_AT_segment: 6475 case DW_AT_start_scope: 6476 case DW_AT_static_link: 6477 case DW_AT_trampoline: 6478 case DW_AT_visibility: 6479 case DW_AT_vtable_elem_location: 6480 case DW_AT_description: 6481 case DW_AT_sibling: 6482 break; 6483 } 6484 } 6485 } 6486 } 6487 6488 std::string object_pointer_name; 6489 if (object_pointer_die_offset != DW_INVALID_OFFSET) 6490 { 6491 // Get the name from the object pointer die 6492 StreamString s; 6493 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s)) 6494 { 6495 object_pointer_name.assign(s.GetData()); 6496 } 6497 } 6498 6499 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 6500 6501 ClangASTType return_clang_type; 6502 Type *func_type = NULL; 6503 6504 if (type_die_offset != DW_INVALID_OFFSET) 6505 func_type = ResolveTypeUID(type_die_offset); 6506 6507 if (func_type) 6508 return_clang_type = func_type->GetClangForwardType(); 6509 else 6510 return_clang_type = ast.GetBasicType(eBasicTypeVoid); 6511 6512 6513 std::vector<ClangASTType> function_param_types; 6514 std::vector<clang::ParmVarDecl*> function_param_decls; 6515 6516 // Parse the function children for the parameters 6517 6518 const DWARFDebugInfoEntry *decl_ctx_die = NULL; 6519 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die); 6520 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind(); 6521 6522 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind); 6523 // Start off static. This will be set to false in ParseChildParameters(...) 6524 // if we find a "this" parameters as the first parameter 6525 if (is_cxx_method) 6526 is_static = true; 6527 6528 if (die->HasChildren()) 6529 { 6530 bool skip_artificial = true; 6531 ParseChildParameters (sc, 6532 containing_decl_ctx, 6533 dwarf_cu, 6534 die, 6535 skip_artificial, 6536 is_static, 6537 is_variadic, 6538 type_list, 6539 function_param_types, 6540 function_param_decls, 6541 type_quals); 6542 } 6543 6544 // clang_type will get the function prototype clang type after this call 6545 clang_type = ast.CreateFunctionType (return_clang_type, 6546 function_param_types.data(), 6547 function_param_types.size(), 6548 is_variadic, 6549 type_quals); 6550 6551 bool ignore_containing_context = false; 6552 6553 if (type_name_cstr) 6554 { 6555 bool type_handled = false; 6556 if (tag == DW_TAG_subprogram) 6557 { 6558 ObjCLanguageRuntime::MethodName objc_method (type_name_cstr, true); 6559 if (objc_method.IsValid(true)) 6560 { 6561 ClangASTType class_opaque_type; 6562 ConstString class_name(objc_method.GetClassName()); 6563 if (class_name) 6564 { 6565 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false)); 6566 6567 if (complete_objc_class_type_sp) 6568 { 6569 ClangASTType type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType(); 6570 if (type_clang_forward_type.IsObjCObjectOrInterfaceType ()) 6571 class_opaque_type = type_clang_forward_type; 6572 } 6573 } 6574 6575 if (class_opaque_type) 6576 { 6577 // If accessibility isn't set to anything valid, assume public for 6578 // now... 6579 if (accessibility == eAccessNone) 6580 accessibility = eAccessPublic; 6581 6582 clang::ObjCMethodDecl *objc_method_decl = class_opaque_type.AddMethodToObjCObjectType (type_name_cstr, 6583 clang_type, 6584 accessibility, 6585 is_artificial); 6586 type_handled = objc_method_decl != NULL; 6587 if (type_handled) 6588 { 6589 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die); 6590 GetClangASTContext().SetMetadataAsUserID (objc_method_decl, MakeUserID(die->GetOffset())); 6591 } 6592 else 6593 { 6594 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message", 6595 die->GetOffset(), 6596 tag, 6597 DW_TAG_value_to_name(tag)); 6598 } 6599 } 6600 } 6601 else if (is_cxx_method) 6602 { 6603 // Look at the parent of this DIE and see if is is 6604 // a class or struct and see if this is actually a 6605 // C++ method 6606 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die); 6607 if (class_type) 6608 { 6609 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset())) 6610 { 6611 // We uniqued the parent class of this function to another class 6612 // so we now need to associate all dies under "decl_ctx_die" to 6613 // DIEs in the DIE for "class_type"... 6614 SymbolFileDWARF *class_symfile = NULL; 6615 DWARFCompileUnitSP class_type_cu_sp; 6616 const DWARFDebugInfoEntry *class_type_die = NULL; 6617 6618 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 6619 if (debug_map_symfile) 6620 { 6621 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID())); 6622 class_type_die = class_symfile->DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp); 6623 } 6624 else 6625 { 6626 class_symfile = this; 6627 class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp); 6628 } 6629 if (class_type_die) 6630 { 6631 DWARFDIECollection failures; 6632 6633 CopyUniqueClassMethodTypes (class_symfile, 6634 class_type, 6635 class_type_cu_sp.get(), 6636 class_type_die, 6637 dwarf_cu, 6638 decl_ctx_die, 6639 failures); 6640 6641 // FIXME do something with these failures that's smarter than 6642 // just dropping them on the ground. Unfortunately classes don't 6643 // like having stuff added to them after their definitions are 6644 // complete... 6645 6646 type_ptr = m_die_to_type[die]; 6647 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) 6648 { 6649 type_sp = type_ptr->shared_from_this(); 6650 break; 6651 } 6652 } 6653 } 6654 6655 if (specification_die_offset != DW_INVALID_OFFSET) 6656 { 6657 // We have a specification which we are going to base our function 6658 // prototype off of, so we need this type to be completed so that the 6659 // m_die_to_decl_ctx for the method in the specification has a valid 6660 // clang decl context. 6661 class_type->GetClangForwardType(); 6662 // If we have a specification, then the function type should have been 6663 // made with the specification and not with this die. 6664 DWARFCompileUnitSP spec_cu_sp; 6665 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp); 6666 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die); 6667 if (spec_clang_decl_ctx) 6668 { 6669 LinkDeclContextToDIE(spec_clang_decl_ctx, die); 6670 } 6671 else 6672 { 6673 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x) has no decl\n", 6674 MakeUserID(die->GetOffset()), 6675 specification_die_offset); 6676 } 6677 type_handled = true; 6678 } 6679 else if (abstract_origin_die_offset != DW_INVALID_OFFSET) 6680 { 6681 // We have a specification which we are going to base our function 6682 // prototype off of, so we need this type to be completed so that the 6683 // m_die_to_decl_ctx for the method in the abstract origin has a valid 6684 // clang decl context. 6685 class_type->GetClangForwardType(); 6686 6687 DWARFCompileUnitSP abs_cu_sp; 6688 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp); 6689 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die); 6690 if (abs_clang_decl_ctx) 6691 { 6692 LinkDeclContextToDIE (abs_clang_decl_ctx, die); 6693 } 6694 else 6695 { 6696 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x) has no decl\n", 6697 MakeUserID(die->GetOffset()), 6698 abstract_origin_die_offset); 6699 } 6700 type_handled = true; 6701 } 6702 else 6703 { 6704 ClangASTType class_opaque_type = class_type->GetClangForwardType(); 6705 if (class_opaque_type.IsCXXClassType ()) 6706 { 6707 if (class_opaque_type.IsBeingDefined ()) 6708 { 6709 // Neither GCC 4.2 nor clang++ currently set a valid accessibility 6710 // in the DWARF for C++ methods... Default to public for now... 6711 if (accessibility == eAccessNone) 6712 accessibility = eAccessPublic; 6713 6714 if (!is_static && !die->HasChildren()) 6715 { 6716 // We have a C++ member function with no children (this pointer!) 6717 // and clang will get mad if we try and make a function that isn't 6718 // well formed in the DWARF, so we will just skip it... 6719 type_handled = true; 6720 } 6721 else 6722 { 6723 clang::CXXMethodDecl *cxx_method_decl; 6724 // REMOVE THE CRASH DESCRIPTION BELOW 6725 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s", 6726 type_name_cstr, 6727 class_type->GetName().GetCString(), 6728 MakeUserID(die->GetOffset()), 6729 m_obj_file->GetFileSpec().GetPath().c_str()); 6730 6731 const bool is_attr_used = false; 6732 6733 cxx_method_decl = class_opaque_type.AddMethodToCXXRecordType (type_name_cstr, 6734 clang_type, 6735 accessibility, 6736 is_virtual, 6737 is_static, 6738 is_inline, 6739 is_explicit, 6740 is_attr_used, 6741 is_artificial); 6742 6743 type_handled = cxx_method_decl != NULL; 6744 6745 if (type_handled) 6746 { 6747 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die); 6748 6749 Host::SetCrashDescription (NULL); 6750 6751 6752 ClangASTMetadata metadata; 6753 metadata.SetUserID(MakeUserID(die->GetOffset())); 6754 6755 if (!object_pointer_name.empty()) 6756 { 6757 metadata.SetObjectPtrName(object_pointer_name.c_str()); 6758 if (log) 6759 log->Printf ("Setting object pointer name: %s on method object %p.\n", 6760 object_pointer_name.c_str(), 6761 static_cast<void*>(cxx_method_decl)); 6762 } 6763 GetClangASTContext().SetMetadata (cxx_method_decl, metadata); 6764 } 6765 else 6766 { 6767 ignore_containing_context = true; 6768 } 6769 } 6770 } 6771 else 6772 { 6773 // We were asked to parse the type for a method in a class, yet the 6774 // class hasn't been asked to complete itself through the 6775 // clang::ExternalASTSource protocol, so we need to just have the 6776 // class complete itself and do things the right way, then our 6777 // DIE should then have an entry in the m_die_to_type map. First 6778 // we need to modify the m_die_to_type so it doesn't think we are 6779 // trying to parse this DIE anymore... 6780 m_die_to_type[die] = NULL; 6781 6782 // Now we get the full type to force our class type to complete itself 6783 // using the clang::ExternalASTSource protocol which will parse all 6784 // base classes and all methods (including the method for this DIE). 6785 class_type->GetClangFullType(); 6786 6787 // The type for this DIE should have been filled in the function call above 6788 type_ptr = m_die_to_type[die]; 6789 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) 6790 { 6791 type_sp = type_ptr->shared_from_this(); 6792 break; 6793 } 6794 6795 // FIXME This is fixing some even uglier behavior but we really need to 6796 // uniq the methods of each class as well as the class itself. 6797 // <rdar://problem/11240464> 6798 type_handled = true; 6799 } 6800 } 6801 } 6802 } 6803 } 6804 } 6805 6806 if (!type_handled) 6807 { 6808 // We just have a function that isn't part of a class 6809 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (ignore_containing_context ? GetClangASTContext().GetTranslationUnitDecl() : containing_decl_ctx, 6810 type_name_cstr, 6811 clang_type, 6812 storage, 6813 is_inline); 6814 6815 // if (template_param_infos.GetSize() > 0) 6816 // { 6817 // clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx, 6818 // function_decl, 6819 // type_name_cstr, 6820 // template_param_infos); 6821 // 6822 // ast.CreateFunctionTemplateSpecializationInfo (function_decl, 6823 // func_template_decl, 6824 // template_param_infos); 6825 // } 6826 // Add the decl to our DIE to decl context map 6827 assert (function_decl); 6828 LinkDeclContextToDIE(function_decl, die); 6829 if (!function_param_decls.empty()) 6830 ast.SetFunctionParameters (function_decl, 6831 &function_param_decls.front(), 6832 function_param_decls.size()); 6833 6834 ClangASTMetadata metadata; 6835 metadata.SetUserID(MakeUserID(die->GetOffset())); 6836 6837 if (!object_pointer_name.empty()) 6838 { 6839 metadata.SetObjectPtrName(object_pointer_name.c_str()); 6840 if (log) 6841 log->Printf ("Setting object pointer name: %s on function object %p.", 6842 object_pointer_name.c_str(), 6843 static_cast<void*>(function_decl)); 6844 } 6845 GetClangASTContext().SetMetadata (function_decl, metadata); 6846 } 6847 } 6848 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 6849 this, 6850 type_name_const_str, 6851 0, 6852 NULL, 6853 LLDB_INVALID_UID, 6854 Type::eEncodingIsUID, 6855 &decl, 6856 clang_type, 6857 Type::eResolveStateFull)); 6858 assert(type_sp.get()); 6859 } 6860 break; 6861 6862 case DW_TAG_array_type: 6863 { 6864 // Set a bit that lets us know that we are currently parsing this 6865 m_die_to_type[die] = DIE_IS_BEING_PARSED; 6866 6867 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET; 6868 int64_t first_index = 0; 6869 uint32_t byte_stride = 0; 6870 uint32_t bit_stride = 0; 6871 bool is_vector = false; 6872 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 6873 6874 if (num_attributes > 0) 6875 { 6876 uint32_t i; 6877 for (i=0; i<num_attributes; ++i) 6878 { 6879 attr = attributes.AttributeAtIndex(i); 6880 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 6881 { 6882 switch (attr) 6883 { 6884 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 6885 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 6886 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 6887 case DW_AT_name: 6888 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 6889 type_name_const_str.SetCString(type_name_cstr); 6890 break; 6891 6892 case DW_AT_type: type_die_offset = form_value.Reference(); break; 6893 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break; 6894 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break; 6895 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break; 6896 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break; 6897 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 6898 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break; 6899 case DW_AT_allocated: 6900 case DW_AT_associated: 6901 case DW_AT_data_location: 6902 case DW_AT_description: 6903 case DW_AT_ordering: 6904 case DW_AT_start_scope: 6905 case DW_AT_visibility: 6906 case DW_AT_specification: 6907 case DW_AT_abstract_origin: 6908 case DW_AT_sibling: 6909 break; 6910 } 6911 } 6912 } 6913 6914 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 6915 6916 Type *element_type = ResolveTypeUID(type_die_offset); 6917 6918 if (element_type) 6919 { 6920 std::vector<uint64_t> element_orders; 6921 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride); 6922 if (byte_stride == 0 && bit_stride == 0) 6923 byte_stride = element_type->GetByteSize(); 6924 ClangASTType array_element_type = element_type->GetClangForwardType(); 6925 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride; 6926 if (element_orders.size() > 0) 6927 { 6928 uint64_t num_elements = 0; 6929 std::vector<uint64_t>::const_reverse_iterator pos; 6930 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend(); 6931 for (pos = element_orders.rbegin(); pos != end; ++pos) 6932 { 6933 num_elements = *pos; 6934 clang_type = ast.CreateArrayType (array_element_type, 6935 num_elements, 6936 is_vector); 6937 array_element_type = clang_type; 6938 array_element_bit_stride = num_elements ? 6939 array_element_bit_stride * num_elements : 6940 array_element_bit_stride; 6941 } 6942 } 6943 else 6944 { 6945 clang_type = ast.CreateArrayType (array_element_type, 0, is_vector); 6946 } 6947 ConstString empty_name; 6948 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 6949 this, 6950 empty_name, 6951 array_element_bit_stride / 8, 6952 NULL, 6953 type_die_offset, 6954 Type::eEncodingIsUID, 6955 &decl, 6956 clang_type, 6957 Type::eResolveStateFull)); 6958 type_sp->SetEncodingType (element_type); 6959 } 6960 } 6961 } 6962 break; 6963 6964 case DW_TAG_ptr_to_member_type: 6965 { 6966 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 6967 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET; 6968 6969 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 6970 6971 if (num_attributes > 0) { 6972 uint32_t i; 6973 for (i=0; i<num_attributes; ++i) 6974 { 6975 attr = attributes.AttributeAtIndex(i); 6976 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 6977 { 6978 switch (attr) 6979 { 6980 case DW_AT_type: 6981 type_die_offset = form_value.Reference(); break; 6982 case DW_AT_containing_type: 6983 containing_type_die_offset = form_value.Reference(); break; 6984 } 6985 } 6986 } 6987 6988 Type *pointee_type = ResolveTypeUID(type_die_offset); 6989 Type *class_type = ResolveTypeUID(containing_type_die_offset); 6990 6991 ClangASTType pointee_clang_type = pointee_type->GetClangForwardType(); 6992 ClangASTType class_clang_type = class_type->GetClangLayoutType(); 6993 6994 clang_type = pointee_clang_type.CreateMemberPointerType(class_clang_type); 6995 6996 byte_size = clang_type.GetByteSize(nullptr); 6997 6998 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 6999 this, 7000 type_name_const_str, 7001 byte_size, 7002 NULL, 7003 LLDB_INVALID_UID, 7004 Type::eEncodingIsUID, 7005 NULL, 7006 clang_type, 7007 Type::eResolveStateForward)); 7008 } 7009 7010 break; 7011 } 7012 default: 7013 GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message", 7014 die->GetOffset(), 7015 tag, 7016 DW_TAG_value_to_name(tag)); 7017 break; 7018 } 7019 7020 if (type_sp.get()) 7021 { 7022 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 7023 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 7024 7025 SymbolContextScope * symbol_context_scope = NULL; 7026 if (sc_parent_tag == DW_TAG_compile_unit) 7027 { 7028 symbol_context_scope = sc.comp_unit; 7029 } 7030 else if (sc.function != NULL && sc_parent_die) 7031 { 7032 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 7033 if (symbol_context_scope == NULL) 7034 symbol_context_scope = sc.function; 7035 } 7036 7037 if (symbol_context_scope != NULL) 7038 { 7039 type_sp->SetSymbolContextScope(symbol_context_scope); 7040 } 7041 7042 // We are ready to put this type into the uniqued list up at the module level 7043 type_list->Insert (type_sp); 7044 7045 m_die_to_type[die] = type_sp.get(); 7046 } 7047 } 7048 else if (type_ptr != DIE_IS_BEING_PARSED) 7049 { 7050 type_sp = type_ptr->shared_from_this(); 7051 } 7052 } 7053 return type_sp; 7054 } 7055 7056 size_t 7057 SymbolFileDWARF::ParseTypes 7058 ( 7059 const SymbolContext& sc, 7060 DWARFCompileUnit* dwarf_cu, 7061 const DWARFDebugInfoEntry *die, 7062 bool parse_siblings, 7063 bool parse_children 7064 ) 7065 { 7066 size_t types_added = 0; 7067 while (die != NULL) 7068 { 7069 bool type_is_new = false; 7070 if (ParseType(sc, dwarf_cu, die, &type_is_new).get()) 7071 { 7072 if (type_is_new) 7073 ++types_added; 7074 } 7075 7076 if (parse_children && die->HasChildren()) 7077 { 7078 if (die->Tag() == DW_TAG_subprogram) 7079 { 7080 SymbolContext child_sc(sc); 7081 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get(); 7082 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true); 7083 } 7084 else 7085 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true); 7086 } 7087 7088 if (parse_siblings) 7089 die = die->GetSibling(); 7090 else 7091 die = NULL; 7092 } 7093 return types_added; 7094 } 7095 7096 7097 size_t 7098 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc) 7099 { 7100 assert(sc.comp_unit && sc.function); 7101 size_t functions_added = 0; 7102 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 7103 if (dwarf_cu) 7104 { 7105 dw_offset_t function_die_offset = sc.function->GetID(); 7106 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset); 7107 if (function_die) 7108 { 7109 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0); 7110 } 7111 } 7112 7113 return functions_added; 7114 } 7115 7116 7117 size_t 7118 SymbolFileDWARF::ParseTypes (const SymbolContext &sc) 7119 { 7120 // At least a compile unit must be valid 7121 assert(sc.comp_unit); 7122 size_t types_added = 0; 7123 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 7124 if (dwarf_cu) 7125 { 7126 if (sc.function) 7127 { 7128 dw_offset_t function_die_offset = sc.function->GetID(); 7129 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset); 7130 if (func_die && func_die->HasChildren()) 7131 { 7132 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true); 7133 } 7134 } 7135 else 7136 { 7137 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE(); 7138 if (dwarf_cu_die && dwarf_cu_die->HasChildren()) 7139 { 7140 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true); 7141 } 7142 } 7143 } 7144 7145 return types_added; 7146 } 7147 7148 size_t 7149 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc) 7150 { 7151 if (sc.comp_unit != NULL) 7152 { 7153 DWARFDebugInfo* info = DebugInfo(); 7154 if (info == NULL) 7155 return 0; 7156 7157 if (sc.function) 7158 { 7159 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitContainingDIE(sc.function->GetID()).get(); 7160 7161 if (dwarf_cu == NULL) 7162 return 0; 7163 7164 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID()); 7165 7166 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, LLDB_INVALID_ADDRESS); 7167 if (func_lo_pc != LLDB_INVALID_ADDRESS) 7168 { 7169 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true); 7170 7171 // Let all blocks know they have parse all their variables 7172 sc.function->GetBlock (false).SetDidParseVariables (true, true); 7173 return num_variables; 7174 } 7175 } 7176 else if (sc.comp_unit) 7177 { 7178 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID()).get(); 7179 7180 if (dwarf_cu == NULL) 7181 return 0; 7182 7183 uint32_t vars_added = 0; 7184 VariableListSP variables (sc.comp_unit->GetVariableList(false)); 7185 7186 if (variables.get() == NULL) 7187 { 7188 variables.reset(new VariableList()); 7189 sc.comp_unit->SetVariableList(variables); 7190 7191 DWARFCompileUnit* match_dwarf_cu = NULL; 7192 const DWARFDebugInfoEntry* die = NULL; 7193 DIEArray die_offsets; 7194 if (m_using_apple_tables) 7195 { 7196 if (m_apple_names_ap.get()) 7197 { 7198 DWARFMappedHash::DIEInfoArray hash_data_array; 7199 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(), 7200 dwarf_cu->GetNextCompileUnitOffset(), 7201 hash_data_array)) 7202 { 7203 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets); 7204 } 7205 } 7206 } 7207 else 7208 { 7209 // Index if we already haven't to make sure the compile units 7210 // get indexed and make their global DIE index list 7211 if (!m_indexed) 7212 Index (); 7213 7214 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(), 7215 dwarf_cu->GetNextCompileUnitOffset(), 7216 die_offsets); 7217 } 7218 7219 const size_t num_matches = die_offsets.size(); 7220 if (num_matches) 7221 { 7222 DWARFDebugInfo* debug_info = DebugInfo(); 7223 for (size_t i=0; i<num_matches; ++i) 7224 { 7225 const dw_offset_t die_offset = die_offsets[i]; 7226 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu); 7227 if (die) 7228 { 7229 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS)); 7230 if (var_sp) 7231 { 7232 variables->AddVariableIfUnique (var_sp); 7233 ++vars_added; 7234 } 7235 } 7236 else 7237 { 7238 if (m_using_apple_tables) 7239 { 7240 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_offset); 7241 } 7242 } 7243 7244 } 7245 } 7246 } 7247 return vars_added; 7248 } 7249 } 7250 return 0; 7251 } 7252 7253 7254 VariableSP 7255 SymbolFileDWARF::ParseVariableDIE 7256 ( 7257 const SymbolContext& sc, 7258 DWARFCompileUnit* dwarf_cu, 7259 const DWARFDebugInfoEntry *die, 7260 const lldb::addr_t func_low_pc 7261 ) 7262 { 7263 VariableSP var_sp (m_die_to_variable_sp[die]); 7264 if (var_sp) 7265 return var_sp; // Already been parsed! 7266 7267 const dw_tag_t tag = die->Tag(); 7268 ModuleSP module = GetObjectFile()->GetModule(); 7269 7270 if ((tag == DW_TAG_variable) || 7271 (tag == DW_TAG_constant) || 7272 (tag == DW_TAG_formal_parameter && sc.function)) 7273 { 7274 DWARFDebugInfoEntry::Attributes attributes; 7275 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 7276 if (num_attributes > 0) 7277 { 7278 const char *name = NULL; 7279 const char *mangled = NULL; 7280 Declaration decl; 7281 uint32_t i; 7282 lldb::user_id_t type_uid = LLDB_INVALID_UID; 7283 DWARFExpression location; 7284 bool is_external = false; 7285 bool is_artificial = false; 7286 bool location_is_const_value_data = false; 7287 bool has_explicit_location = false; 7288 DWARFFormValue const_value; 7289 //AccessType accessibility = eAccessNone; 7290 7291 for (i=0; i<num_attributes; ++i) 7292 { 7293 dw_attr_t attr = attributes.AttributeAtIndex(i); 7294 DWARFFormValue form_value; 7295 7296 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 7297 { 7298 switch (attr) 7299 { 7300 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 7301 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 7302 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 7303 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 7304 case DW_AT_linkage_name: 7305 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break; 7306 case DW_AT_type: type_uid = form_value.Reference(); break; 7307 case DW_AT_external: is_external = form_value.Boolean(); break; 7308 case DW_AT_const_value: 7309 // If we have already found a DW_AT_location attribute, ignore this attribute. 7310 if (!has_explicit_location) 7311 { 7312 location_is_const_value_data = true; 7313 // The constant value will be either a block, a data value or a string. 7314 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 7315 if (DWARFFormValue::IsBlockForm(form_value.Form())) 7316 { 7317 // Retrieve the value as a block expression. 7318 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 7319 uint32_t block_length = form_value.Unsigned(); 7320 location.CopyOpcodeData(module, debug_info_data, block_offset, block_length); 7321 } 7322 else if (DWARFFormValue::IsDataForm(form_value.Form())) 7323 { 7324 // Retrieve the value as a data expression. 7325 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64()); 7326 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 7327 uint32_t data_length = fixed_form_sizes[form_value.Form()]; 7328 if (data_length == 0) 7329 { 7330 const uint8_t *data_pointer = form_value.BlockData(); 7331 if (data_pointer) 7332 { 7333 form_value.Unsigned(); 7334 } 7335 else if (DWARFFormValue::IsDataForm(form_value.Form())) 7336 { 7337 // we need to get the byte size of the type later after we create the variable 7338 const_value = form_value; 7339 } 7340 } 7341 else 7342 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length); 7343 } 7344 else 7345 { 7346 // Retrieve the value as a string expression. 7347 if (form_value.Form() == DW_FORM_strp) 7348 { 7349 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (attributes.CompileUnitAtIndex(i)->GetAddressByteSize(), attributes.CompileUnitAtIndex(i)->IsDWARF64()); 7350 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 7351 uint32_t data_length = fixed_form_sizes[form_value.Form()]; 7352 location.CopyOpcodeData(module, debug_info_data, data_offset, data_length); 7353 } 7354 else 7355 { 7356 const char *str = form_value.AsCString(&debug_info_data); 7357 uint32_t string_offset = str - (const char *)debug_info_data.GetDataStart(); 7358 uint32_t string_length = strlen(str) + 1; 7359 location.CopyOpcodeData(module, debug_info_data, string_offset, string_length); 7360 } 7361 } 7362 } 7363 break; 7364 case DW_AT_location: 7365 { 7366 location_is_const_value_data = false; 7367 has_explicit_location = true; 7368 if (form_value.BlockData()) 7369 { 7370 const DWARFDataExtractor& debug_info_data = get_debug_info_data(); 7371 7372 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 7373 uint32_t block_length = form_value.Unsigned(); 7374 location.CopyOpcodeData(module, get_debug_info_data(), block_offset, block_length); 7375 } 7376 else 7377 { 7378 const DWARFDataExtractor& debug_loc_data = get_debug_loc_data(); 7379 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 7380 7381 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset); 7382 if (loc_list_length > 0) 7383 { 7384 location.CopyOpcodeData(module, debug_loc_data, debug_loc_offset, loc_list_length); 7385 assert (func_low_pc != LLDB_INVALID_ADDRESS); 7386 location.SetLocationListSlide (func_low_pc - attributes.CompileUnitAtIndex(i)->GetBaseAddress()); 7387 } 7388 } 7389 } 7390 break; 7391 7392 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 7393 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 7394 case DW_AT_declaration: 7395 case DW_AT_description: 7396 case DW_AT_endianity: 7397 case DW_AT_segment: 7398 case DW_AT_start_scope: 7399 case DW_AT_visibility: 7400 default: 7401 case DW_AT_abstract_origin: 7402 case DW_AT_sibling: 7403 case DW_AT_specification: 7404 break; 7405 } 7406 } 7407 } 7408 7409 ValueType scope = eValueTypeInvalid; 7410 7411 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 7412 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 7413 SymbolContextScope * symbol_context_scope = NULL; 7414 7415 // DWARF doesn't specify if a DW_TAG_variable is a local, global 7416 // or static variable, so we have to do a little digging by 7417 // looking at the location of a variable to see if it contains 7418 // a DW_OP_addr opcode _somewhere_ in the definition. I say 7419 // somewhere because clang likes to combine small global variables 7420 // into the same symbol and have locations like: 7421 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus 7422 // So if we don't have a DW_TAG_formal_parameter, we can look at 7423 // the location to see if it contains a DW_OP_addr opcode, and 7424 // then we can correctly classify our variables. 7425 if (tag == DW_TAG_formal_parameter) 7426 scope = eValueTypeVariableArgument; 7427 else 7428 { 7429 bool op_error = false; 7430 // Check if the location has a DW_OP_addr with any address value... 7431 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS; 7432 if (!location_is_const_value_data) 7433 { 7434 location_DW_OP_addr = location.GetLocation_DW_OP_addr (0, op_error); 7435 if (op_error) 7436 { 7437 StreamString strm; 7438 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL); 7439 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), strm.GetString().c_str()); 7440 } 7441 } 7442 7443 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS) 7444 { 7445 if (is_external) 7446 scope = eValueTypeVariableGlobal; 7447 else 7448 scope = eValueTypeVariableStatic; 7449 7450 7451 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile (); 7452 7453 if (debug_map_symfile) 7454 { 7455 // When leaving the DWARF in the .o files on darwin, 7456 // when we have a global variable that wasn't initialized, 7457 // the .o file might not have allocated a virtual 7458 // address for the global variable. In this case it will 7459 // have created a symbol for the global variable 7460 // that is undefined/data and external and the value will 7461 // be the byte size of the variable. When we do the 7462 // address map in SymbolFileDWARFDebugMap we rely on 7463 // having an address, we need to do some magic here 7464 // so we can get the correct address for our global 7465 // variable. The address for all of these entries 7466 // will be zero, and there will be an undefined symbol 7467 // in this object file, and the executable will have 7468 // a matching symbol with a good address. So here we 7469 // dig up the correct address and replace it in the 7470 // location for the variable, and set the variable's 7471 // symbol context scope to be that of the main executable 7472 // so the file address will resolve correctly. 7473 bool linked_oso_file_addr = false; 7474 if (is_external && location_DW_OP_addr == 0) 7475 { 7476 // we have a possible uninitialized extern global 7477 ConstString const_name(mangled ? mangled : name); 7478 ObjectFile *debug_map_objfile = debug_map_symfile->GetObjectFile(); 7479 if (debug_map_objfile) 7480 { 7481 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab(); 7482 if (debug_map_symtab) 7483 { 7484 Symbol *exe_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name, 7485 eSymbolTypeData, 7486 Symtab::eDebugYes, 7487 Symtab::eVisibilityExtern); 7488 if (exe_symbol) 7489 { 7490 if (exe_symbol->ValueIsAddress()) 7491 { 7492 const addr_t exe_file_addr = exe_symbol->GetAddress().GetFileAddress(); 7493 if (exe_file_addr != LLDB_INVALID_ADDRESS) 7494 { 7495 if (location.Update_DW_OP_addr (exe_file_addr)) 7496 { 7497 linked_oso_file_addr = true; 7498 symbol_context_scope = exe_symbol; 7499 } 7500 } 7501 } 7502 } 7503 } 7504 } 7505 } 7506 7507 if (!linked_oso_file_addr) 7508 { 7509 // The DW_OP_addr is not zero, but it contains a .o file address which 7510 // needs to be linked up correctly. 7511 const lldb::addr_t exe_file_addr = debug_map_symfile->LinkOSOFileAddress(this, location_DW_OP_addr); 7512 if (exe_file_addr != LLDB_INVALID_ADDRESS) 7513 { 7514 // Update the file address for this variable 7515 location.Update_DW_OP_addr (exe_file_addr); 7516 } 7517 else 7518 { 7519 // Variable didn't make it into the final executable 7520 return var_sp; 7521 } 7522 } 7523 } 7524 } 7525 else 7526 { 7527 scope = eValueTypeVariableLocal; 7528 } 7529 } 7530 7531 if (symbol_context_scope == NULL) 7532 { 7533 switch (parent_tag) 7534 { 7535 case DW_TAG_subprogram: 7536 case DW_TAG_inlined_subroutine: 7537 case DW_TAG_lexical_block: 7538 if (sc.function) 7539 { 7540 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 7541 if (symbol_context_scope == NULL) 7542 symbol_context_scope = sc.function; 7543 } 7544 break; 7545 7546 default: 7547 symbol_context_scope = sc.comp_unit; 7548 break; 7549 } 7550 } 7551 7552 if (symbol_context_scope) 7553 { 7554 SymbolFileTypeSP type_sp(new SymbolFileType(*this, type_uid)); 7555 7556 if (const_value.Form() && type_sp && type_sp->GetType()) 7557 location.CopyOpcodeData(const_value.Unsigned(), type_sp->GetType()->GetByteSize(), dwarf_cu->GetAddressByteSize()); 7558 7559 var_sp.reset (new Variable (MakeUserID(die->GetOffset()), 7560 name, 7561 mangled, 7562 type_sp, 7563 scope, 7564 symbol_context_scope, 7565 &decl, 7566 location, 7567 is_external, 7568 is_artificial)); 7569 7570 var_sp->SetLocationIsConstantValueData (location_is_const_value_data); 7571 } 7572 else 7573 { 7574 // Not ready to parse this variable yet. It might be a global 7575 // or static variable that is in a function scope and the function 7576 // in the symbol context wasn't filled in yet 7577 return var_sp; 7578 } 7579 } 7580 // Cache var_sp even if NULL (the variable was just a specification or 7581 // was missing vital information to be able to be displayed in the debugger 7582 // (missing location due to optimization, etc)) so we don't re-parse 7583 // this DIE over and over later... 7584 m_die_to_variable_sp[die] = var_sp; 7585 } 7586 return var_sp; 7587 } 7588 7589 7590 const DWARFDebugInfoEntry * 7591 SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset, 7592 dw_offset_t spec_block_die_offset, 7593 DWARFCompileUnit **result_die_cu_handle) 7594 { 7595 // Give the concrete function die specified by "func_die_offset", find the 7596 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 7597 // to "spec_block_die_offset" 7598 DWARFDebugInfo* info = DebugInfo(); 7599 7600 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle); 7601 if (die) 7602 { 7603 assert (*result_die_cu_handle); 7604 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle); 7605 } 7606 return NULL; 7607 } 7608 7609 7610 const DWARFDebugInfoEntry * 7611 SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu, 7612 const DWARFDebugInfoEntry *die, 7613 dw_offset_t spec_block_die_offset, 7614 DWARFCompileUnit **result_die_cu_handle) 7615 { 7616 if (die) 7617 { 7618 switch (die->Tag()) 7619 { 7620 case DW_TAG_subprogram: 7621 case DW_TAG_inlined_subroutine: 7622 case DW_TAG_lexical_block: 7623 { 7624 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset) 7625 { 7626 *result_die_cu_handle = dwarf_cu; 7627 return die; 7628 } 7629 7630 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset) 7631 { 7632 *result_die_cu_handle = dwarf_cu; 7633 return die; 7634 } 7635 } 7636 break; 7637 } 7638 7639 // Give the concrete function die specified by "func_die_offset", find the 7640 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 7641 // to "spec_block_die_offset" 7642 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling()) 7643 { 7644 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu, 7645 child_die, 7646 spec_block_die_offset, 7647 result_die_cu_handle); 7648 if (result_die) 7649 return result_die; 7650 } 7651 } 7652 7653 *result_die_cu_handle = NULL; 7654 return NULL; 7655 } 7656 7657 size_t 7658 SymbolFileDWARF::ParseVariables 7659 ( 7660 const SymbolContext& sc, 7661 DWARFCompileUnit* dwarf_cu, 7662 const lldb::addr_t func_low_pc, 7663 const DWARFDebugInfoEntry *orig_die, 7664 bool parse_siblings, 7665 bool parse_children, 7666 VariableList* cc_variable_list 7667 ) 7668 { 7669 if (orig_die == NULL) 7670 return 0; 7671 7672 VariableListSP variable_list_sp; 7673 7674 size_t vars_added = 0; 7675 const DWARFDebugInfoEntry *die = orig_die; 7676 while (die != NULL) 7677 { 7678 dw_tag_t tag = die->Tag(); 7679 7680 // Check to see if we have already parsed this variable or constant? 7681 if (m_die_to_variable_sp[die]) 7682 { 7683 if (cc_variable_list) 7684 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]); 7685 } 7686 else 7687 { 7688 // We haven't already parsed it, lets do that now. 7689 if ((tag == DW_TAG_variable) || 7690 (tag == DW_TAG_constant) || 7691 (tag == DW_TAG_formal_parameter && sc.function)) 7692 { 7693 if (variable_list_sp.get() == NULL) 7694 { 7695 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die); 7696 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 7697 switch (parent_tag) 7698 { 7699 case DW_TAG_compile_unit: 7700 if (sc.comp_unit != NULL) 7701 { 7702 variable_list_sp = sc.comp_unit->GetVariableList(false); 7703 if (variable_list_sp.get() == NULL) 7704 { 7705 variable_list_sp.reset(new VariableList()); 7706 sc.comp_unit->SetVariableList(variable_list_sp); 7707 } 7708 } 7709 else 7710 { 7711 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8" PRIx64 " %s with no valid compile unit in symbol context for 0x%8.8" PRIx64 " %s.\n", 7712 MakeUserID(sc_parent_die->GetOffset()), 7713 DW_TAG_value_to_name (parent_tag), 7714 MakeUserID(orig_die->GetOffset()), 7715 DW_TAG_value_to_name (orig_die->Tag())); 7716 } 7717 break; 7718 7719 case DW_TAG_subprogram: 7720 case DW_TAG_inlined_subroutine: 7721 case DW_TAG_lexical_block: 7722 if (sc.function != NULL) 7723 { 7724 // Check to see if we already have parsed the variables for the given scope 7725 7726 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 7727 if (block == NULL) 7728 { 7729 // This must be a specification or abstract origin with 7730 // a concrete block counterpart in the current function. We need 7731 // to find the concrete block so we can correctly add the 7732 // variable to it 7733 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu; 7734 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(), 7735 sc_parent_die->GetOffset(), 7736 &concrete_block_die_cu); 7737 if (concrete_block_die) 7738 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset())); 7739 } 7740 7741 if (block != NULL) 7742 { 7743 const bool can_create = false; 7744 variable_list_sp = block->GetBlockVariableList (can_create); 7745 if (variable_list_sp.get() == NULL) 7746 { 7747 variable_list_sp.reset(new VariableList()); 7748 block->SetVariableList(variable_list_sp); 7749 } 7750 } 7751 } 7752 break; 7753 7754 default: 7755 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8" PRIx64 " %s.\n", 7756 MakeUserID(orig_die->GetOffset()), 7757 DW_TAG_value_to_name (orig_die->Tag())); 7758 break; 7759 } 7760 } 7761 7762 if (variable_list_sp) 7763 { 7764 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc)); 7765 if (var_sp) 7766 { 7767 variable_list_sp->AddVariableIfUnique (var_sp); 7768 if (cc_variable_list) 7769 cc_variable_list->AddVariableIfUnique (var_sp); 7770 ++vars_added; 7771 } 7772 } 7773 } 7774 } 7775 7776 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram); 7777 7778 if (!skip_children && parse_children && die->HasChildren()) 7779 { 7780 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list); 7781 } 7782 7783 if (parse_siblings) 7784 die = die->GetSibling(); 7785 else 7786 die = NULL; 7787 } 7788 return vars_added; 7789 } 7790 7791 //------------------------------------------------------------------ 7792 // PluginInterface protocol 7793 //------------------------------------------------------------------ 7794 ConstString 7795 SymbolFileDWARF::GetPluginName() 7796 { 7797 return GetPluginNameStatic(); 7798 } 7799 7800 uint32_t 7801 SymbolFileDWARF::GetPluginVersion() 7802 { 7803 return 1; 7804 } 7805 7806 void 7807 SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl) 7808 { 7809 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 7810 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 7811 if (clang_type) 7812 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 7813 } 7814 7815 void 7816 SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl) 7817 { 7818 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 7819 ClangASTType clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 7820 if (clang_type) 7821 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 7822 } 7823 7824 void 7825 SymbolFileDWARF::DumpIndexes () 7826 { 7827 StreamFile s(stdout, false); 7828 7829 s.Printf ("DWARF index for (%s) '%s':", 7830 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(), 7831 GetObjectFile()->GetFileSpec().GetPath().c_str()); 7832 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 7833 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 7834 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 7835 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 7836 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 7837 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 7838 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 7839 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s); 7840 } 7841 7842 void 7843 SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context, 7844 const char *name, 7845 llvm::SmallVectorImpl <clang::NamedDecl *> *results) 7846 { 7847 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context); 7848 7849 if (iter == m_decl_ctx_to_die.end()) 7850 return; 7851 7852 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos) 7853 { 7854 const DWARFDebugInfoEntry *context_die = *pos; 7855 7856 if (!results) 7857 return; 7858 7859 DWARFDebugInfo* info = DebugInfo(); 7860 7861 DIEArray die_offsets; 7862 7863 DWARFCompileUnit* dwarf_cu = NULL; 7864 const DWARFDebugInfoEntry* die = NULL; 7865 7866 if (m_using_apple_tables) 7867 { 7868 if (m_apple_types_ap.get()) 7869 m_apple_types_ap->FindByName (name, die_offsets); 7870 } 7871 else 7872 { 7873 if (!m_indexed) 7874 Index (); 7875 7876 m_type_index.Find (ConstString(name), die_offsets); 7877 } 7878 7879 const size_t num_matches = die_offsets.size(); 7880 7881 if (num_matches) 7882 { 7883 for (size_t i = 0; i < num_matches; ++i) 7884 { 7885 const dw_offset_t die_offset = die_offsets[i]; 7886 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 7887 7888 if (die->GetParent() != context_die) 7889 continue; 7890 7891 Type *matching_type = ResolveType (dwarf_cu, die); 7892 7893 clang::QualType qual_type = matching_type->GetClangForwardType().GetQualType(); 7894 7895 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr())) 7896 { 7897 clang::TagDecl *tag_decl = tag_type->getDecl(); 7898 results->push_back(tag_decl); 7899 } 7900 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr())) 7901 { 7902 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); 7903 results->push_back(typedef_decl); 7904 } 7905 } 7906 } 7907 } 7908 } 7909 7910 void 7911 SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton, 7912 const clang::DeclContext *decl_context, 7913 clang::DeclarationName decl_name, 7914 llvm::SmallVectorImpl <clang::NamedDecl *> *results) 7915 { 7916 7917 switch (decl_context->getDeclKind()) 7918 { 7919 case clang::Decl::Namespace: 7920 case clang::Decl::TranslationUnit: 7921 { 7922 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 7923 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results); 7924 } 7925 break; 7926 default: 7927 break; 7928 } 7929 } 7930 7931 bool 7932 SymbolFileDWARF::LayoutRecordType (void *baton, 7933 const clang::RecordDecl *record_decl, 7934 uint64_t &size, 7935 uint64_t &alignment, 7936 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets, 7937 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets, 7938 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets) 7939 { 7940 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 7941 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets); 7942 } 7943 7944 7945 bool 7946 SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl, 7947 uint64_t &bit_size, 7948 uint64_t &alignment, 7949 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets, 7950 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets, 7951 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets) 7952 { 7953 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 7954 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl); 7955 bool success = false; 7956 base_offsets.clear(); 7957 vbase_offsets.clear(); 7958 if (pos != m_record_decl_to_layout_map.end()) 7959 { 7960 bit_size = pos->second.bit_size; 7961 alignment = pos->second.alignment; 7962 field_offsets.swap(pos->second.field_offsets); 7963 base_offsets.swap (pos->second.base_offsets); 7964 vbase_offsets.swap (pos->second.vbase_offsets); 7965 m_record_decl_to_layout_map.erase(pos); 7966 success = true; 7967 } 7968 else 7969 { 7970 bit_size = 0; 7971 alignment = 0; 7972 field_offsets.clear(); 7973 } 7974 7975 if (log) 7976 GetObjectFile()->GetModule()->LogMessage (log, 7977 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i", 7978 static_cast<const void*>(record_decl), 7979 bit_size, alignment, 7980 static_cast<uint32_t>(field_offsets.size()), 7981 static_cast<uint32_t>(base_offsets.size()), 7982 static_cast<uint32_t>(vbase_offsets.size()), 7983 success); 7984 return success; 7985 } 7986 7987 7988 SymbolFileDWARFDebugMap * 7989 SymbolFileDWARF::GetDebugMapSymfile () 7990 { 7991 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired()) 7992 { 7993 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock()); 7994 if (module_sp) 7995 { 7996 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 7997 if (sym_vendor) 7998 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile(); 7999 } 8000 } 8001 return m_debug_map_symfile; 8002 } 8003 8004 8005