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