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