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