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/Basic/Builtins.h" 18 #include "clang/Basic/IdentifierTable.h" 19 #include "clang/Basic/LangOptions.h" 20 #include "clang/Basic/SourceManager.h" 21 #include "clang/Basic/TargetInfo.h" 22 #include "clang/Basic/Specifiers.h" 23 #include "clang/Sema/DeclSpec.h" 24 25 #include "llvm/Support/Casting.h" 26 27 #include "lldb/Core/Module.h" 28 #include "lldb/Core/PluginManager.h" 29 #include "lldb/Core/RegularExpression.h" 30 #include "lldb/Core/Scalar.h" 31 #include "lldb/Core/Section.h" 32 #include "lldb/Core/StreamFile.h" 33 #include "lldb/Core/StreamString.h" 34 #include "lldb/Core/Timer.h" 35 #include "lldb/Core/Value.h" 36 37 #include "lldb/Host/Host.h" 38 39 #include "lldb/Symbol/Block.h" 40 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" 41 #include "lldb/Symbol/CompileUnit.h" 42 #include "lldb/Symbol/LineTable.h" 43 #include "lldb/Symbol/ObjectFile.h" 44 #include "lldb/Symbol/SymbolVendor.h" 45 #include "lldb/Symbol/VariableList.h" 46 47 #include "lldb/Target/ObjCLanguageRuntime.h" 48 #include "lldb/Target/CPPLanguageRuntime.h" 49 50 #include "DWARFCompileUnit.h" 51 #include "DWARFDebugAbbrev.h" 52 #include "DWARFDebugAranges.h" 53 #include "DWARFDebugInfo.h" 54 #include "DWARFDebugInfoEntry.h" 55 #include "DWARFDebugLine.h" 56 #include "DWARFDebugPubnames.h" 57 #include "DWARFDebugRanges.h" 58 #include "DWARFDIECollection.h" 59 #include "DWARFFormValue.h" 60 #include "DWARFLocationList.h" 61 #include "LogChannelDWARF.h" 62 #include "SymbolFileDWARFDebugMap.h" 63 64 #include <map> 65 66 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 67 68 #ifdef ENABLE_DEBUG_PRINTF 69 #include <stdio.h> 70 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) 71 #else 72 #define DEBUG_PRINTF(fmt, ...) 73 #endif 74 75 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1) 76 77 using namespace lldb; 78 using namespace lldb_private; 79 80 81 static AccessType 82 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility) 83 { 84 switch (dwarf_accessibility) 85 { 86 case DW_ACCESS_public: return eAccessPublic; 87 case DW_ACCESS_private: return eAccessPrivate; 88 case DW_ACCESS_protected: return eAccessProtected; 89 default: break; 90 } 91 return eAccessNone; 92 } 93 94 void 95 SymbolFileDWARF::Initialize() 96 { 97 LogChannelDWARF::Initialize(); 98 PluginManager::RegisterPlugin (GetPluginNameStatic(), 99 GetPluginDescriptionStatic(), 100 CreateInstance); 101 } 102 103 void 104 SymbolFileDWARF::Terminate() 105 { 106 PluginManager::UnregisterPlugin (CreateInstance); 107 LogChannelDWARF::Initialize(); 108 } 109 110 111 const char * 112 SymbolFileDWARF::GetPluginNameStatic() 113 { 114 return "dwarf"; 115 } 116 117 const char * 118 SymbolFileDWARF::GetPluginDescriptionStatic() 119 { 120 return "DWARF and DWARF3 debug symbol file reader."; 121 } 122 123 124 SymbolFile* 125 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file) 126 { 127 return new SymbolFileDWARF(obj_file); 128 } 129 130 TypeList * 131 SymbolFileDWARF::GetTypeList () 132 { 133 if (m_debug_map_symfile) 134 return m_debug_map_symfile->GetTypeList(); 135 return m_obj_file->GetModule()->GetTypeList(); 136 137 } 138 139 //---------------------------------------------------------------------- 140 // Gets the first parent that is a lexical block, function or inlined 141 // subroutine, or compile unit. 142 //---------------------------------------------------------------------- 143 static const DWARFDebugInfoEntry * 144 GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die) 145 { 146 const DWARFDebugInfoEntry *die; 147 for (die = child_die->GetParent(); die != NULL; die = die->GetParent()) 148 { 149 dw_tag_t tag = die->Tag(); 150 151 switch (tag) 152 { 153 case DW_TAG_compile_unit: 154 case DW_TAG_subprogram: 155 case DW_TAG_inlined_subroutine: 156 case DW_TAG_lexical_block: 157 return die; 158 } 159 } 160 return NULL; 161 } 162 163 164 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) : 165 SymbolFile (objfile), 166 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID 167 m_debug_map_symfile (NULL), 168 m_clang_tu_decl (NULL), 169 m_flags(), 170 m_data_debug_abbrev (), 171 m_data_debug_aranges (), 172 m_data_debug_frame (), 173 m_data_debug_info (), 174 m_data_debug_line (), 175 m_data_debug_loc (), 176 m_data_debug_ranges (), 177 m_data_debug_str (), 178 m_data_apple_names (), 179 m_data_apple_types (), 180 m_data_apple_namespaces (), 181 m_abbr(), 182 m_info(), 183 m_line(), 184 m_apple_names_ap (), 185 m_apple_types_ap (), 186 m_apple_namespaces_ap (), 187 m_apple_objc_ap (), 188 m_function_basename_index(), 189 m_function_fullname_index(), 190 m_function_method_index(), 191 m_function_selector_index(), 192 m_objc_class_selectors_index(), 193 m_global_index(), 194 m_type_index(), 195 m_namespace_index(), 196 m_indexed (false), 197 m_is_external_ast_source (false), 198 m_using_apple_tables (false), 199 m_ranges(), 200 m_unique_ast_type_map () 201 { 202 } 203 204 SymbolFileDWARF::~SymbolFileDWARF() 205 { 206 if (m_is_external_ast_source) 207 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource (); 208 } 209 210 static const ConstString & 211 GetDWARFMachOSegmentName () 212 { 213 static ConstString g_dwarf_section_name ("__DWARF"); 214 return g_dwarf_section_name; 215 } 216 217 UniqueDWARFASTTypeMap & 218 SymbolFileDWARF::GetUniqueDWARFASTTypeMap () 219 { 220 if (m_debug_map_symfile) 221 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap (); 222 return m_unique_ast_type_map; 223 } 224 225 ClangASTContext & 226 SymbolFileDWARF::GetClangASTContext () 227 { 228 if (m_debug_map_symfile) 229 return m_debug_map_symfile->GetClangASTContext (); 230 231 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext(); 232 if (!m_is_external_ast_source) 233 { 234 m_is_external_ast_source = true; 235 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap ( 236 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl, 237 SymbolFileDWARF::CompleteObjCInterfaceDecl, 238 SymbolFileDWARF::FindExternalVisibleDeclsByName, 239 this)); 240 241 ast.SetExternalSource (ast_source_ap); 242 } 243 return ast; 244 } 245 246 void 247 SymbolFileDWARF::InitializeObject() 248 { 249 // Install our external AST source callbacks so we can complete Clang types. 250 Module *module = m_obj_file->GetModule(); 251 if (module) 252 { 253 const SectionList *section_list = m_obj_file->GetSectionList(); 254 255 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 256 257 // Memory map the DWARF mach-o segment so we have everything mmap'ed 258 // to keep our heap memory usage down. 259 if (section) 260 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data); 261 } 262 get_apple_names_data(); 263 if (m_data_apple_names.GetByteSize() > 0) 264 { 265 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names")); 266 if (m_apple_names_ap->IsValid()) 267 m_using_apple_tables = true; 268 else 269 m_apple_names_ap.reset(); 270 } 271 get_apple_types_data(); 272 if (m_data_apple_types.GetByteSize() > 0) 273 { 274 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types")); 275 if (m_apple_types_ap->IsValid()) 276 m_using_apple_tables = true; 277 else 278 m_apple_types_ap.reset(); 279 } 280 281 get_apple_namespaces_data(); 282 if (m_data_apple_namespaces.GetByteSize() > 0) 283 { 284 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces")); 285 if (m_apple_namespaces_ap->IsValid()) 286 m_using_apple_tables = true; 287 else 288 m_apple_namespaces_ap.reset(); 289 } 290 291 get_apple_objc_data(); 292 if (m_data_apple_objc.GetByteSize() > 0) 293 { 294 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc")); 295 if (m_apple_objc_ap->IsValid()) 296 m_using_apple_tables = true; 297 else 298 m_apple_objc_ap.reset(); 299 } 300 } 301 302 bool 303 SymbolFileDWARF::SupportedVersion(uint16_t version) 304 { 305 return version == 2 || version == 3; 306 } 307 308 uint32_t 309 SymbolFileDWARF::GetAbilities () 310 { 311 uint32_t abilities = 0; 312 if (m_obj_file != NULL) 313 { 314 const Section* section = NULL; 315 const SectionList *section_list = m_obj_file->GetSectionList(); 316 if (section_list == NULL) 317 return 0; 318 319 uint64_t debug_abbrev_file_size = 0; 320 uint64_t debug_aranges_file_size = 0; 321 uint64_t debug_frame_file_size = 0; 322 uint64_t debug_info_file_size = 0; 323 uint64_t debug_line_file_size = 0; 324 uint64_t debug_loc_file_size = 0; 325 uint64_t debug_macinfo_file_size = 0; 326 uint64_t debug_pubnames_file_size = 0; 327 uint64_t debug_pubtypes_file_size = 0; 328 uint64_t debug_ranges_file_size = 0; 329 uint64_t debug_str_file_size = 0; 330 331 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 332 333 if (section) 334 section_list = §ion->GetChildren (); 335 336 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get(); 337 if (section != NULL) 338 { 339 debug_info_file_size = section->GetByteSize(); 340 341 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get(); 342 if (section) 343 debug_abbrev_file_size = section->GetByteSize(); 344 else 345 m_flags.Set (flagsGotDebugAbbrevData); 346 347 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get(); 348 if (section) 349 debug_aranges_file_size = section->GetByteSize(); 350 else 351 m_flags.Set (flagsGotDebugArangesData); 352 353 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get(); 354 if (section) 355 debug_frame_file_size = section->GetByteSize(); 356 else 357 m_flags.Set (flagsGotDebugFrameData); 358 359 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get(); 360 if (section) 361 debug_line_file_size = section->GetByteSize(); 362 else 363 m_flags.Set (flagsGotDebugLineData); 364 365 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get(); 366 if (section) 367 debug_loc_file_size = section->GetByteSize(); 368 else 369 m_flags.Set (flagsGotDebugLocData); 370 371 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get(); 372 if (section) 373 debug_macinfo_file_size = section->GetByteSize(); 374 else 375 m_flags.Set (flagsGotDebugMacInfoData); 376 377 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get(); 378 if (section) 379 debug_pubnames_file_size = section->GetByteSize(); 380 else 381 m_flags.Set (flagsGotDebugPubNamesData); 382 383 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get(); 384 if (section) 385 debug_pubtypes_file_size = section->GetByteSize(); 386 else 387 m_flags.Set (flagsGotDebugPubTypesData); 388 389 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get(); 390 if (section) 391 debug_ranges_file_size = section->GetByteSize(); 392 else 393 m_flags.Set (flagsGotDebugRangesData); 394 395 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get(); 396 if (section) 397 debug_str_file_size = section->GetByteSize(); 398 else 399 m_flags.Set (flagsGotDebugStrData); 400 } 401 402 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0) 403 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes; 404 405 if (debug_line_file_size > 0) 406 abilities |= LineTables; 407 408 if (debug_aranges_file_size > 0) 409 abilities |= AddressAcceleratorTable; 410 411 if (debug_pubnames_file_size > 0) 412 abilities |= FunctionAcceleratorTable; 413 414 if (debug_pubtypes_file_size > 0) 415 abilities |= TypeAcceleratorTable; 416 417 if (debug_macinfo_file_size > 0) 418 abilities |= MacroInformation; 419 420 if (debug_frame_file_size > 0) 421 abilities |= CallFrameInformation; 422 } 423 return abilities; 424 } 425 426 const DataExtractor& 427 SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data) 428 { 429 if (m_flags.IsClear (got_flag)) 430 { 431 m_flags.Set (got_flag); 432 const SectionList *section_list = m_obj_file->GetSectionList(); 433 if (section_list) 434 { 435 Section *section = section_list->FindSectionByType(sect_type, true).get(); 436 if (section) 437 { 438 // See if we memory mapped the DWARF segment? 439 if (m_dwarf_data.GetByteSize()) 440 { 441 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize()); 442 } 443 else 444 { 445 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0) 446 data.Clear(); 447 } 448 } 449 } 450 } 451 return data; 452 } 453 454 const DataExtractor& 455 SymbolFileDWARF::get_debug_abbrev_data() 456 { 457 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev); 458 } 459 460 const DataExtractor& 461 SymbolFileDWARF::get_debug_aranges_data() 462 { 463 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges); 464 } 465 466 const DataExtractor& 467 SymbolFileDWARF::get_debug_frame_data() 468 { 469 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame); 470 } 471 472 const DataExtractor& 473 SymbolFileDWARF::get_debug_info_data() 474 { 475 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info); 476 } 477 478 const DataExtractor& 479 SymbolFileDWARF::get_debug_line_data() 480 { 481 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line); 482 } 483 484 const DataExtractor& 485 SymbolFileDWARF::get_debug_loc_data() 486 { 487 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc); 488 } 489 490 const DataExtractor& 491 SymbolFileDWARF::get_debug_ranges_data() 492 { 493 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges); 494 } 495 496 const DataExtractor& 497 SymbolFileDWARF::get_debug_str_data() 498 { 499 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str); 500 } 501 502 const DataExtractor& 503 SymbolFileDWARF::get_apple_names_data() 504 { 505 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names); 506 } 507 508 const DataExtractor& 509 SymbolFileDWARF::get_apple_types_data() 510 { 511 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types); 512 } 513 514 const DataExtractor& 515 SymbolFileDWARF::get_apple_namespaces_data() 516 { 517 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces); 518 } 519 520 const DataExtractor& 521 SymbolFileDWARF::get_apple_objc_data() 522 { 523 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc); 524 } 525 526 527 DWARFDebugAbbrev* 528 SymbolFileDWARF::DebugAbbrev() 529 { 530 if (m_abbr.get() == NULL) 531 { 532 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data(); 533 if (debug_abbrev_data.GetByteSize() > 0) 534 { 535 m_abbr.reset(new DWARFDebugAbbrev()); 536 if (m_abbr.get()) 537 m_abbr->Parse(debug_abbrev_data); 538 } 539 } 540 return m_abbr.get(); 541 } 542 543 const DWARFDebugAbbrev* 544 SymbolFileDWARF::DebugAbbrev() const 545 { 546 return m_abbr.get(); 547 } 548 549 550 DWARFDebugInfo* 551 SymbolFileDWARF::DebugInfo() 552 { 553 if (m_info.get() == NULL) 554 { 555 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); 556 if (get_debug_info_data().GetByteSize() > 0) 557 { 558 m_info.reset(new DWARFDebugInfo()); 559 if (m_info.get()) 560 { 561 m_info->SetDwarfData(this); 562 } 563 } 564 } 565 return m_info.get(); 566 } 567 568 const DWARFDebugInfo* 569 SymbolFileDWARF::DebugInfo() const 570 { 571 return m_info.get(); 572 } 573 574 DWARFCompileUnit* 575 SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid) 576 { 577 DWARFDebugInfo* info = DebugInfo(); 578 if (info && UserIDMatches(cu_uid)) 579 return info->GetCompileUnit((dw_offset_t)cu_uid).get(); 580 return NULL; 581 } 582 583 584 DWARFDebugRanges* 585 SymbolFileDWARF::DebugRanges() 586 { 587 if (m_ranges.get() == NULL) 588 { 589 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); 590 if (get_debug_ranges_data().GetByteSize() > 0) 591 { 592 m_ranges.reset(new DWARFDebugRanges()); 593 if (m_ranges.get()) 594 m_ranges->Extract(this); 595 } 596 } 597 return m_ranges.get(); 598 } 599 600 const DWARFDebugRanges* 601 SymbolFileDWARF::DebugRanges() const 602 { 603 return m_ranges.get(); 604 } 605 606 bool 607 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp) 608 { 609 if (curr_cu != NULL) 610 { 611 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly (); 612 if (cu_die) 613 { 614 const char * cu_die_name = cu_die->GetName(this, curr_cu); 615 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL); 616 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0); 617 if (cu_die_name) 618 { 619 FileSpec cu_file_spec; 620 621 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0') 622 { 623 // If we have a full path to the compile unit, we don't need to resolve 624 // the file. This can be expensive e.g. when the source files are NFS mounted. 625 cu_file_spec.SetFile (cu_die_name, false); 626 } 627 else 628 { 629 std::string fullpath(cu_comp_dir); 630 if (*fullpath.rbegin() != '/') 631 fullpath += '/'; 632 fullpath += cu_die_name; 633 cu_file_spec.SetFile (fullpath.c_str(), false); 634 } 635 636 compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(), 637 curr_cu, 638 cu_file_spec, 639 MakeUserID(curr_cu->GetOffset()), 640 cu_language)); 641 if (compile_unit_sp.get()) 642 { 643 curr_cu->SetUserData(compile_unit_sp.get()); 644 return true; 645 } 646 } 647 } 648 } 649 return false; 650 } 651 652 uint32_t 653 SymbolFileDWARF::GetNumCompileUnits() 654 { 655 DWARFDebugInfo* info = DebugInfo(); 656 if (info) 657 return info->GetNumCompileUnits(); 658 return 0; 659 } 660 661 CompUnitSP 662 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) 663 { 664 CompUnitSP comp_unit; 665 DWARFDebugInfo* info = DebugInfo(); 666 if (info) 667 { 668 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx); 669 if (curr_cu != NULL) 670 { 671 // Our symbol vendor shouldn't be asking us to add a compile unit that 672 // has already been added to it, which this DWARF plug-in knows as it 673 // stores the lldb compile unit (CompileUnit) pointer in each 674 // DWARFCompileUnit object when it gets added. 675 assert(curr_cu->GetUserData() == NULL); 676 ParseCompileUnit(curr_cu, comp_unit); 677 } 678 } 679 return comp_unit; 680 } 681 682 static void 683 AddRangesToBlock (Block& block, 684 DWARFDebugRanges::RangeList& ranges, 685 addr_t block_base_addr) 686 { 687 const size_t num_ranges = ranges.GetSize(); 688 for (size_t i = 0; i<num_ranges; ++i) 689 { 690 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i); 691 const addr_t range_base = range.GetRangeBase(); 692 assert (range_base >= block_base_addr); 693 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));; 694 } 695 block.FinalizeRanges (); 696 } 697 698 699 Function * 700 SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die) 701 { 702 DWARFDebugRanges::RangeList func_ranges; 703 const char *name = NULL; 704 const char *mangled = NULL; 705 int decl_file = 0; 706 int decl_line = 0; 707 int decl_column = 0; 708 int call_file = 0; 709 int call_line = 0; 710 int call_column = 0; 711 DWARFExpression frame_base; 712 713 assert (die->Tag() == DW_TAG_subprogram); 714 715 if (die->Tag() != DW_TAG_subprogram) 716 return NULL; 717 718 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base)) 719 { 720 // Union of all ranges in the function DIE (if the function is discontiguous) 721 AddressRange func_range; 722 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0); 723 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0); 724 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr) 725 { 726 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList()); 727 if (func_range.GetBaseAddress().IsValid()) 728 func_range.SetByteSize(highest_func_addr - lowest_func_addr); 729 } 730 731 if (func_range.GetBaseAddress().IsValid()) 732 { 733 Mangled func_name; 734 if (mangled) 735 func_name.SetValue(mangled, true); 736 else if (name) 737 func_name.SetValue(name, false); 738 739 FunctionSP func_sp; 740 std::auto_ptr<Declaration> decl_ap; 741 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 742 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 743 decl_line, 744 decl_column)); 745 746 // Supply the type _only_ if it has already been parsed 747 Type *func_type = m_die_to_type.lookup (die); 748 749 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED); 750 751 func_range.GetBaseAddress().ResolveLinkedAddress(); 752 753 const user_id_t func_user_id = MakeUserID(die->GetOffset()); 754 func_sp.reset(new Function (sc.comp_unit, 755 func_user_id, // UserID is the DIE offset 756 func_user_id, 757 func_name, 758 func_type, 759 func_range)); // first address range 760 761 if (func_sp.get() != NULL) 762 { 763 if (frame_base.IsValid()) 764 func_sp->GetFrameBaseExpression() = frame_base; 765 sc.comp_unit->AddFunction(func_sp); 766 return func_sp.get(); 767 } 768 } 769 } 770 return NULL; 771 } 772 773 size_t 774 SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) 775 { 776 assert (sc.comp_unit); 777 size_t functions_added = 0; 778 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 779 if (dwarf_cu) 780 { 781 DWARFDIECollection function_dies; 782 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies); 783 size_t func_idx; 784 for (func_idx = 0; func_idx < num_funtions; ++func_idx) 785 { 786 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx); 787 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL) 788 { 789 if (ParseCompileUnitFunction(sc, dwarf_cu, die)) 790 ++functions_added; 791 } 792 } 793 //FixupTypes(); 794 } 795 return functions_added; 796 } 797 798 bool 799 SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files) 800 { 801 assert (sc.comp_unit); 802 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 803 assert (curr_cu); 804 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly(); 805 806 if (cu_die) 807 { 808 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL); 809 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 810 811 // All file indexes in DWARF are one based and a file of index zero is 812 // supposed to be the compile unit itself. 813 support_files.Append (*sc.comp_unit); 814 815 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files); 816 } 817 return false; 818 } 819 820 struct ParseDWARFLineTableCallbackInfo 821 { 822 LineTable* line_table; 823 const SectionList *section_list; 824 lldb::addr_t prev_sect_file_base_addr; 825 lldb::addr_t curr_sect_file_base_addr; 826 bool is_oso_for_debug_map; 827 bool prev_in_final_executable; 828 DWARFDebugLine::Row prev_row; 829 SectionSP prev_section_sp; 830 SectionSP curr_section_sp; 831 }; 832 833 //---------------------------------------------------------------------- 834 // ParseStatementTableCallback 835 //---------------------------------------------------------------------- 836 static void 837 ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData) 838 { 839 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table; 840 if (state.row == DWARFDebugLine::State::StartParsingLineTable) 841 { 842 // Just started parsing the line table 843 } 844 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) 845 { 846 // Done parsing line table, nothing to do for the cleanup 847 } 848 else 849 { 850 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData; 851 // We have a new row, lets append it 852 853 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false) 854 { 855 info->prev_section_sp = info->curr_section_sp; 856 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr; 857 // If this is an end sequence entry, then we subtract one from the 858 // address to make sure we get an address that is not the end of 859 // a section. 860 if (state.end_sequence && state.address != 0) 861 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1); 862 else 863 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address); 864 865 if (info->curr_section_sp.get()) 866 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress (); 867 else 868 info->curr_sect_file_base_addr = 0; 869 } 870 if (info->curr_section_sp.get()) 871 { 872 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr; 873 // Check for the fancy section magic to determine if we 874 875 if (info->is_oso_for_debug_map) 876 { 877 // When this is a debug map object file that contains DWARF 878 // (referenced from an N_OSO debug map nlist entry) we will have 879 // a file address in the file range for our section from the 880 // original .o file, and a load address in the executable that 881 // contains the debug map. 882 // 883 // If the sections for the file range and load range are 884 // different, we have a remapped section for the function and 885 // this address is resolved. If they are the same, then the 886 // function for this address didn't make it into the final 887 // executable. 888 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL; 889 890 // If we are doing DWARF with debug map, then we need to carefully 891 // add each line table entry as there may be gaps as functions 892 // get moved around or removed. 893 if (!info->prev_row.end_sequence && info->prev_section_sp.get()) 894 { 895 if (info->prev_in_final_executable) 896 { 897 bool terminate_previous_entry = false; 898 if (!curr_in_final_executable) 899 { 900 // Check for the case where the previous line entry 901 // in a function made it into the final executable, 902 // yet the current line entry falls in a function 903 // that didn't. The line table used to be contiguous 904 // through this address range but now it isn't. We 905 // need to terminate the previous line entry so 906 // that we can reconstruct the line range correctly 907 // for it and to keep the line table correct. 908 terminate_previous_entry = true; 909 } 910 else if (info->curr_section_sp.get() != info->prev_section_sp.get()) 911 { 912 // Check for cases where the line entries used to be 913 // contiguous address ranges, but now they aren't. 914 // This can happen when order files specify the 915 // ordering of the functions. 916 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr; 917 Section *curr_sect = info->curr_section_sp.get(); 918 Section *prev_sect = info->prev_section_sp.get(); 919 assert (curr_sect->GetLinkedSection()); 920 assert (prev_sect->GetLinkedSection()); 921 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address; 922 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset; 923 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset; 924 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr; 925 if (object_file_addr_delta != linked_file_addr_delta) 926 terminate_previous_entry = true; 927 } 928 929 if (terminate_previous_entry) 930 { 931 line_table->InsertLineEntry (info->prev_section_sp, 932 state.address - info->prev_sect_file_base_addr, 933 info->prev_row.line, 934 info->prev_row.column, 935 info->prev_row.file, 936 false, // is_stmt 937 false, // basic_block 938 false, // state.prologue_end 939 false, // state.epilogue_begin 940 true); // end_sequence); 941 } 942 } 943 } 944 945 if (curr_in_final_executable) 946 { 947 line_table->InsertLineEntry (info->curr_section_sp, 948 curr_line_section_offset, 949 state.line, 950 state.column, 951 state.file, 952 state.is_stmt, 953 state.basic_block, 954 state.prologue_end, 955 state.epilogue_begin, 956 state.end_sequence); 957 info->prev_section_sp = info->curr_section_sp; 958 } 959 else 960 { 961 // If the current address didn't make it into the final 962 // executable, the current section will be the __text 963 // segment in the .o file, so we need to clear this so 964 // we can catch the next function that did make it into 965 // the final executable. 966 info->prev_section_sp.reset(); 967 info->curr_section_sp.reset(); 968 } 969 970 info->prev_in_final_executable = curr_in_final_executable; 971 } 972 else 973 { 974 // We are not in an object file that contains DWARF for an 975 // N_OSO, this is just a normal DWARF file. The DWARF spec 976 // guarantees that the addresses will be in increasing order 977 // so, since we store line tables in file address order, we 978 // can always just append the line entry without needing to 979 // search for the correct insertion point (we don't need to 980 // use LineEntry::InsertLineEntry()). 981 line_table->AppendLineEntry (info->curr_section_sp, 982 curr_line_section_offset, 983 state.line, 984 state.column, 985 state.file, 986 state.is_stmt, 987 state.basic_block, 988 state.prologue_end, 989 state.epilogue_begin, 990 state.end_sequence); 991 } 992 } 993 994 info->prev_row = state; 995 } 996 } 997 998 bool 999 SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc) 1000 { 1001 assert (sc.comp_unit); 1002 if (sc.comp_unit->GetLineTable() != NULL) 1003 return true; 1004 1005 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 1006 if (dwarf_cu) 1007 { 1008 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 1009 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 1010 if (cu_line_offset != DW_INVALID_OFFSET) 1011 { 1012 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit)); 1013 if (line_table_ap.get()) 1014 { 1015 ParseDWARFLineTableCallbackInfo info = { 1016 line_table_ap.get(), 1017 m_obj_file->GetSectionList(), 1018 0, 1019 0, 1020 m_debug_map_symfile != NULL, 1021 false, 1022 DWARFDebugLine::Row(), 1023 SectionSP(), 1024 SectionSP() 1025 }; 1026 uint32_t offset = cu_line_offset; 1027 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info); 1028 sc.comp_unit->SetLineTable(line_table_ap.release()); 1029 return true; 1030 } 1031 } 1032 } 1033 return false; 1034 } 1035 1036 size_t 1037 SymbolFileDWARF::ParseFunctionBlocks 1038 ( 1039 const SymbolContext& sc, 1040 Block *parent_block, 1041 DWARFCompileUnit* dwarf_cu, 1042 const DWARFDebugInfoEntry *die, 1043 addr_t subprogram_low_pc, 1044 uint32_t depth 1045 ) 1046 { 1047 size_t blocks_added = 0; 1048 while (die != NULL) 1049 { 1050 dw_tag_t tag = die->Tag(); 1051 1052 switch (tag) 1053 { 1054 case DW_TAG_inlined_subroutine: 1055 case DW_TAG_subprogram: 1056 case DW_TAG_lexical_block: 1057 { 1058 Block *block = NULL; 1059 if (tag == DW_TAG_subprogram) 1060 { 1061 // Skip any DW_TAG_subprogram DIEs that are inside 1062 // of a normal or inlined functions. These will be 1063 // parsed on their own as separate entities. 1064 1065 if (depth > 0) 1066 break; 1067 1068 block = parent_block; 1069 } 1070 else 1071 { 1072 BlockSP block_sp(new Block (MakeUserID(die->GetOffset()))); 1073 parent_block->AddChild(block_sp); 1074 block = block_sp.get(); 1075 } 1076 DWARFDebugRanges::RangeList ranges; 1077 const char *name = NULL; 1078 const char *mangled_name = NULL; 1079 1080 int decl_file = 0; 1081 int decl_line = 0; 1082 int decl_column = 0; 1083 int call_file = 0; 1084 int call_line = 0; 1085 int call_column = 0; 1086 if (die->GetDIENamesAndRanges (this, 1087 dwarf_cu, 1088 name, 1089 mangled_name, 1090 ranges, 1091 decl_file, decl_line, decl_column, 1092 call_file, call_line, call_column)) 1093 { 1094 if (tag == DW_TAG_subprogram) 1095 { 1096 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS); 1097 subprogram_low_pc = ranges.GetMinRangeBase(0); 1098 } 1099 else if (tag == DW_TAG_inlined_subroutine) 1100 { 1101 // We get called here for inlined subroutines in two ways. 1102 // The first time is when we are making the Function object 1103 // for this inlined concrete instance. Since we're creating a top level block at 1104 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to 1105 // adjust the containing address. 1106 // The second time is when we are parsing the blocks inside the function that contains 1107 // the inlined concrete instance. Since these will be blocks inside the containing "real" 1108 // function the offset will be for that function. 1109 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) 1110 { 1111 subprogram_low_pc = ranges.GetMinRangeBase(0); 1112 } 1113 } 1114 1115 AddRangesToBlock (*block, ranges, subprogram_low_pc); 1116 1117 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL)) 1118 { 1119 std::auto_ptr<Declaration> decl_ap; 1120 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1121 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 1122 decl_line, decl_column)); 1123 1124 std::auto_ptr<Declaration> call_ap; 1125 if (call_file != 0 || call_line != 0 || call_column != 0) 1126 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file), 1127 call_line, call_column)); 1128 1129 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get()); 1130 } 1131 1132 ++blocks_added; 1133 1134 if (die->HasChildren()) 1135 { 1136 blocks_added += ParseFunctionBlocks (sc, 1137 block, 1138 dwarf_cu, 1139 die->GetFirstChild(), 1140 subprogram_low_pc, 1141 depth + 1); 1142 } 1143 } 1144 } 1145 break; 1146 default: 1147 break; 1148 } 1149 1150 // Only parse siblings of the block if we are not at depth zero. A depth 1151 // of zero indicates we are currently parsing the top level 1152 // DW_TAG_subprogram DIE 1153 1154 if (depth == 0) 1155 die = NULL; 1156 else 1157 die = die->GetSibling(); 1158 } 1159 return blocks_added; 1160 } 1161 1162 bool 1163 SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu, 1164 const DWARFDebugInfoEntry *parent_die, 1165 ClangASTContext::TemplateParameterInfos &template_param_infos) 1166 { 1167 1168 if (parent_die == NULL) 1169 return NULL; 1170 1171 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 1172 1173 Args template_parameter_names; 1174 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild(); 1175 die != NULL; 1176 die = die->GetSibling()) 1177 { 1178 const dw_tag_t tag = die->Tag(); 1179 1180 switch (tag) 1181 { 1182 case DW_TAG_template_type_parameter: 1183 case DW_TAG_template_value_parameter: 1184 { 1185 DWARFDebugInfoEntry::Attributes attributes; 1186 const size_t num_attributes = die->GetAttributes (this, 1187 dwarf_cu, 1188 fixed_form_sizes, 1189 attributes); 1190 const char *name = NULL; 1191 Type *lldb_type = NULL; 1192 clang_type_t clang_type = NULL; 1193 uint64_t uval64 = 0; 1194 bool uval64_valid = false; 1195 if (num_attributes > 0) 1196 { 1197 DWARFFormValue form_value; 1198 for (size_t i=0; i<num_attributes; ++i) 1199 { 1200 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1201 1202 switch (attr) 1203 { 1204 case DW_AT_name: 1205 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1206 name = form_value.AsCString(&get_debug_str_data()); 1207 break; 1208 1209 case DW_AT_type: 1210 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1211 { 1212 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu); 1213 lldb_type = ResolveTypeUID(type_die_offset); 1214 if (lldb_type) 1215 clang_type = lldb_type->GetClangForwardType(); 1216 } 1217 break; 1218 1219 case DW_AT_const_value: 1220 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1221 { 1222 uval64_valid = true; 1223 uval64 = form_value.Unsigned(); 1224 } 1225 break; 1226 default: 1227 break; 1228 } 1229 } 1230 1231 if (name && lldb_type && clang_type) 1232 { 1233 bool is_signed = false; 1234 template_param_infos.names.push_back(name); 1235 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type)); 1236 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid) 1237 { 1238 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed); 1239 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type)); 1240 } 1241 else 1242 { 1243 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type)); 1244 } 1245 } 1246 else 1247 { 1248 return false; 1249 } 1250 1251 } 1252 } 1253 break; 1254 1255 default: 1256 break; 1257 } 1258 } 1259 if (template_param_infos.args.empty()) 1260 return false; 1261 return template_param_infos.args.size() == template_param_infos.names.size(); 1262 } 1263 1264 clang::ClassTemplateDecl * 1265 SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx, 1266 lldb::AccessType access_type, 1267 const char *parent_name, 1268 int tag_decl_kind, 1269 const ClangASTContext::TemplateParameterInfos &template_param_infos) 1270 { 1271 if (template_param_infos.IsValid()) 1272 { 1273 std::string template_basename(parent_name); 1274 template_basename.erase (template_basename.find('<')); 1275 ClangASTContext &ast = GetClangASTContext(); 1276 1277 return ast.CreateClassTemplateDecl (decl_ctx, 1278 access_type, 1279 template_basename.c_str(), 1280 tag_decl_kind, 1281 template_param_infos); 1282 } 1283 return NULL; 1284 } 1285 1286 size_t 1287 SymbolFileDWARF::ParseChildMembers 1288 ( 1289 const SymbolContext& sc, 1290 DWARFCompileUnit* dwarf_cu, 1291 const DWARFDebugInfoEntry *parent_die, 1292 clang_type_t class_clang_type, 1293 const LanguageType class_language, 1294 std::vector<clang::CXXBaseSpecifier *>& base_classes, 1295 std::vector<int>& member_accessibilities, 1296 DWARFDIECollection& member_function_dies, 1297 AccessType& default_accessibility, 1298 bool &is_a_class 1299 ) 1300 { 1301 if (parent_die == NULL) 1302 return 0; 1303 1304 size_t count = 0; 1305 const DWARFDebugInfoEntry *die; 1306 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 1307 uint32_t member_idx = 0; 1308 1309 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 1310 { 1311 dw_tag_t tag = die->Tag(); 1312 1313 switch (tag) 1314 { 1315 case DW_TAG_member: 1316 { 1317 DWARFDebugInfoEntry::Attributes attributes; 1318 const size_t num_attributes = die->GetAttributes (this, 1319 dwarf_cu, 1320 fixed_form_sizes, 1321 attributes); 1322 if (num_attributes > 0) 1323 { 1324 Declaration decl; 1325 //DWARFExpression location; 1326 const char *name = NULL; 1327 bool is_artificial = false; 1328 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 1329 AccessType accessibility = eAccessNone; 1330 //off_t member_offset = 0; 1331 size_t byte_size = 0; 1332 size_t bit_offset = 0; 1333 size_t bit_size = 0; 1334 uint32_t i; 1335 for (i=0; i<num_attributes && !is_artificial; ++i) 1336 { 1337 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1338 DWARFFormValue form_value; 1339 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1340 { 1341 switch (attr) 1342 { 1343 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1344 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1345 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1346 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 1347 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 1348 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break; 1349 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break; 1350 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 1351 case DW_AT_data_member_location: 1352 // if (form_value.BlockData()) 1353 // { 1354 // Value initialValue(0); 1355 // Value memberOffset(0); 1356 // const DataExtractor& debug_info_data = get_debug_info_data(); 1357 // uint32_t block_length = form_value.Unsigned(); 1358 // uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 1359 // if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL)) 1360 // { 1361 // member_offset = memberOffset.ResolveValue(NULL, NULL).UInt(); 1362 // } 1363 // } 1364 break; 1365 1366 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break; 1367 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 1368 case DW_AT_declaration: 1369 case DW_AT_description: 1370 case DW_AT_mutable: 1371 case DW_AT_visibility: 1372 default: 1373 case DW_AT_sibling: 1374 break; 1375 } 1376 } 1377 } 1378 1379 // Clang has a DWARF generation bug where sometimes it 1380 // represents fields that are references with bad byte size 1381 // and bit size/offset information such as: 1382 // 1383 // DW_AT_byte_size( 0x00 ) 1384 // DW_AT_bit_size( 0x40 ) 1385 // DW_AT_bit_offset( 0xffffffffffffffc0 ) 1386 // 1387 // So check the bit offset to make sure it is sane, and if 1388 // the values are not sane, remove them. If we don't do this 1389 // then we will end up with a crash if we try to use this 1390 // type in an expression when clang becomes unhappy with its 1391 // recycled debug info. 1392 1393 if (bit_offset > 128) 1394 { 1395 bit_size = 0; 1396 bit_offset = 0; 1397 } 1398 1399 // FIXME: Make Clang ignore Objective-C accessibility for expressions 1400 if (class_language == eLanguageTypeObjC || 1401 class_language == eLanguageTypeObjC_plus_plus) 1402 accessibility = eAccessNone; 1403 1404 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name)) 1405 { 1406 // Not all compilers will mark the vtable pointer 1407 // member as artificial (llvm-gcc). We can't have 1408 // the virtual members in our classes otherwise it 1409 // throws off all child offsets since we end up 1410 // having and extra pointer sized member in our 1411 // class layouts. 1412 is_artificial = true; 1413 } 1414 1415 if (is_artificial == false) 1416 { 1417 Type *member_type = ResolveTypeUID(encoding_uid); 1418 if (member_type) 1419 { 1420 if (accessibility == eAccessNone) 1421 accessibility = default_accessibility; 1422 member_accessibilities.push_back(accessibility); 1423 1424 GetClangASTContext().AddFieldToRecordType (class_clang_type, 1425 name, 1426 member_type->GetClangLayoutType(), 1427 accessibility, 1428 bit_size); 1429 } 1430 else 1431 { 1432 if (name) 1433 ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed", 1434 MakeUserID(die->GetOffset()), 1435 name, 1436 encoding_uid); 1437 else 1438 ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed", 1439 MakeUserID(die->GetOffset()), 1440 encoding_uid); 1441 } 1442 } 1443 } 1444 ++member_idx; 1445 } 1446 break; 1447 1448 case DW_TAG_subprogram: 1449 // Let the type parsing code handle this one for us. 1450 member_function_dies.Append (die); 1451 break; 1452 1453 case DW_TAG_inheritance: 1454 { 1455 is_a_class = true; 1456 if (default_accessibility == eAccessNone) 1457 default_accessibility = eAccessPrivate; 1458 // TODO: implement DW_TAG_inheritance type parsing 1459 DWARFDebugInfoEntry::Attributes attributes; 1460 const size_t num_attributes = die->GetAttributes (this, 1461 dwarf_cu, 1462 fixed_form_sizes, 1463 attributes); 1464 if (num_attributes > 0) 1465 { 1466 Declaration decl; 1467 DWARFExpression location; 1468 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 1469 AccessType accessibility = default_accessibility; 1470 bool is_virtual = false; 1471 bool is_base_of_class = true; 1472 off_t member_offset = 0; 1473 uint32_t i; 1474 for (i=0; i<num_attributes; ++i) 1475 { 1476 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1477 DWARFFormValue form_value; 1478 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1479 { 1480 switch (attr) 1481 { 1482 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1483 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1484 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1485 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 1486 case DW_AT_data_member_location: 1487 if (form_value.BlockData()) 1488 { 1489 Value initialValue(0); 1490 Value memberOffset(0); 1491 const DataExtractor& debug_info_data = get_debug_info_data(); 1492 uint32_t block_length = form_value.Unsigned(); 1493 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 1494 if (DWARFExpression::Evaluate (NULL, 1495 NULL, 1496 NULL, 1497 NULL, 1498 NULL, 1499 debug_info_data, 1500 block_offset, 1501 block_length, 1502 eRegisterKindDWARF, 1503 &initialValue, 1504 memberOffset, 1505 NULL)) 1506 { 1507 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt(); 1508 } 1509 } 1510 break; 1511 1512 case DW_AT_accessibility: 1513 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 1514 break; 1515 1516 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break; 1517 default: 1518 case DW_AT_sibling: 1519 break; 1520 } 1521 } 1522 } 1523 1524 Type *base_class_type = ResolveTypeUID(encoding_uid); 1525 assert(base_class_type); 1526 1527 clang_type_t base_class_clang_type = base_class_type->GetClangFullType(); 1528 assert (base_class_clang_type); 1529 if (class_language == eLanguageTypeObjC) 1530 { 1531 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type); 1532 } 1533 else 1534 { 1535 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type, 1536 accessibility, 1537 is_virtual, 1538 is_base_of_class)); 1539 } 1540 } 1541 } 1542 break; 1543 1544 default: 1545 break; 1546 } 1547 } 1548 return count; 1549 } 1550 1551 1552 clang::DeclContext* 1553 SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid) 1554 { 1555 DWARFDebugInfo* debug_info = DebugInfo(); 1556 if (debug_info && UserIDMatches(type_uid)) 1557 { 1558 DWARFCompileUnitSP cu_sp; 1559 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp); 1560 if (die) 1561 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL); 1562 } 1563 return NULL; 1564 } 1565 1566 clang::DeclContext* 1567 SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid) 1568 { 1569 if (UserIDMatches(type_uid)) 1570 return GetClangDeclContextForDIEOffset (sc, type_uid); 1571 return NULL; 1572 } 1573 1574 Type* 1575 SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid) 1576 { 1577 if (UserIDMatches(type_uid)) 1578 { 1579 DWARFDebugInfo* debug_info = DebugInfo(); 1580 if (debug_info) 1581 { 1582 DWARFCompileUnitSP cu_sp; 1583 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp); 1584 if (type_die != NULL) 1585 { 1586 // We might be coming in in the middle of a type tree (a class 1587 // withing a class, an enum within a class), so parse any needed 1588 // parent DIEs before we get to this one... 1589 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu_sp.get(), type_die); 1590 switch (decl_ctx_die->Tag()) 1591 { 1592 case DW_TAG_structure_type: 1593 case DW_TAG_union_type: 1594 case DW_TAG_class_type: 1595 ResolveType(cu_sp.get(), decl_ctx_die); 1596 break; 1597 } 1598 return ResolveType (cu_sp.get(), type_die); 1599 } 1600 } 1601 } 1602 return NULL; 1603 } 1604 1605 // This function is used when SymbolFileDWARFDebugMap owns a bunch of 1606 // SymbolFileDWARF objects to detect if this DWARF file is the one that 1607 // can resolve a clang_type. 1608 bool 1609 SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type) 1610 { 1611 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type); 1612 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers); 1613 return die != NULL; 1614 } 1615 1616 1617 lldb::clang_type_t 1618 SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type) 1619 { 1620 // We have a struct/union/class/enum that needs to be fully resolved. 1621 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type); 1622 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers); 1623 if (die == NULL) 1624 { 1625 // We have already resolved this type... 1626 return clang_type; 1627 } 1628 // Once we start resolving this type, remove it from the forward declaration 1629 // map in case anyone child members or other types require this type to get resolved. 1630 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition 1631 // are done. 1632 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers); 1633 1634 1635 // Disable external storage for this type so we don't get anymore 1636 // clang::ExternalASTSource queries for this type. 1637 ClangASTContext::SetHasExternalStorage (clang_type, false); 1638 1639 DWARFDebugInfo* debug_info = DebugInfo(); 1640 1641 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get(); 1642 Type *type = m_die_to_type.lookup (die); 1643 1644 const dw_tag_t tag = die->Tag(); 1645 1646 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\") - resolve forward declaration...\n", 1647 MakeUserID(die->GetOffset()), 1648 DW_TAG_value_to_name(tag), 1649 type->GetName().AsCString()); 1650 assert (clang_type); 1651 DWARFDebugInfoEntry::Attributes attributes; 1652 1653 ClangASTContext &ast = GetClangASTContext(); 1654 1655 switch (tag) 1656 { 1657 case DW_TAG_structure_type: 1658 case DW_TAG_union_type: 1659 case DW_TAG_class_type: 1660 ast.StartTagDeclarationDefinition (clang_type); 1661 if (die->HasChildren()) 1662 { 1663 LanguageType class_language = eLanguageTypeUnknown; 1664 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type); 1665 if (is_objc_class) 1666 class_language = eLanguageTypeObjC; 1667 1668 int tag_decl_kind = -1; 1669 AccessType default_accessibility = eAccessNone; 1670 if (tag == DW_TAG_structure_type) 1671 { 1672 tag_decl_kind = clang::TTK_Struct; 1673 default_accessibility = eAccessPublic; 1674 } 1675 else if (tag == DW_TAG_union_type) 1676 { 1677 tag_decl_kind = clang::TTK_Union; 1678 default_accessibility = eAccessPublic; 1679 } 1680 else if (tag == DW_TAG_class_type) 1681 { 1682 tag_decl_kind = clang::TTK_Class; 1683 default_accessibility = eAccessPrivate; 1684 } 1685 1686 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu)); 1687 std::vector<clang::CXXBaseSpecifier *> base_classes; 1688 std::vector<int> member_accessibilities; 1689 bool is_a_class = false; 1690 // Parse members and base classes first 1691 DWARFDIECollection member_function_dies; 1692 1693 ParseChildMembers (sc, 1694 curr_cu, 1695 die, 1696 clang_type, 1697 class_language, 1698 base_classes, 1699 member_accessibilities, 1700 member_function_dies, 1701 default_accessibility, 1702 is_a_class); 1703 1704 // Now parse any methods if there were any... 1705 size_t num_functions = member_function_dies.Size(); 1706 if (num_functions > 0) 1707 { 1708 for (size_t i=0; i<num_functions; ++i) 1709 { 1710 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i)); 1711 } 1712 } 1713 1714 if (class_language == eLanguageTypeObjC) 1715 { 1716 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type)); 1717 if (!class_str.empty()) 1718 { 1719 1720 DIEArray method_die_offsets; 1721 if (m_using_apple_tables) 1722 { 1723 if (m_apple_objc_ap.get()) 1724 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets); 1725 } 1726 else 1727 { 1728 if (!m_indexed) 1729 Index (); 1730 1731 ConstString class_name (class_str.c_str()); 1732 m_objc_class_selectors_index.Find (class_name, method_die_offsets); 1733 } 1734 1735 if (!method_die_offsets.empty()) 1736 { 1737 DWARFDebugInfo* debug_info = DebugInfo(); 1738 1739 DWARFCompileUnit* method_cu = NULL; 1740 const size_t num_matches = method_die_offsets.size(); 1741 for (size_t i=0; i<num_matches; ++i) 1742 { 1743 const dw_offset_t die_offset = method_die_offsets[i]; 1744 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu); 1745 1746 ResolveType (method_cu, method_die); 1747 } 1748 } 1749 } 1750 } 1751 1752 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we 1753 // need to tell the clang type it is actually a class. 1754 if (class_language != eLanguageTypeObjC) 1755 { 1756 if (is_a_class && tag_decl_kind != clang::TTK_Class) 1757 ast.SetTagTypeKind (clang_type, clang::TTK_Class); 1758 } 1759 1760 // Since DW_TAG_structure_type gets used for both classes 1761 // and structures, we may need to set any DW_TAG_member 1762 // fields to have a "private" access if none was specified. 1763 // When we parsed the child members we tracked that actual 1764 // accessibility value for each DW_TAG_member in the 1765 // "member_accessibilities" array. If the value for the 1766 // member is zero, then it was set to the "default_accessibility" 1767 // which for structs was "public". Below we correct this 1768 // by setting any fields to "private" that weren't correctly 1769 // set. 1770 if (is_a_class && !member_accessibilities.empty()) 1771 { 1772 // This is a class and all members that didn't have 1773 // their access specified are private. 1774 ast.SetDefaultAccessForRecordFields (clang_type, 1775 eAccessPrivate, 1776 &member_accessibilities.front(), 1777 member_accessibilities.size()); 1778 } 1779 1780 if (!base_classes.empty()) 1781 { 1782 ast.SetBaseClassesForClassType (clang_type, 1783 &base_classes.front(), 1784 base_classes.size()); 1785 1786 // Clang will copy each CXXBaseSpecifier in "base_classes" 1787 // so we have to free them all. 1788 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(), 1789 base_classes.size()); 1790 } 1791 1792 } 1793 ast.CompleteTagDeclarationDefinition (clang_type); 1794 return clang_type; 1795 1796 case DW_TAG_enumeration_type: 1797 ast.StartTagDeclarationDefinition (clang_type); 1798 if (die->HasChildren()) 1799 { 1800 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu)); 1801 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die); 1802 } 1803 ast.CompleteTagDeclarationDefinition (clang_type); 1804 return clang_type; 1805 1806 default: 1807 assert(false && "not a forward clang type decl!"); 1808 break; 1809 } 1810 return NULL; 1811 } 1812 1813 Type* 1814 SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed) 1815 { 1816 if (type_die != NULL) 1817 { 1818 Type *type = m_die_to_type.lookup (type_die); 1819 if (type == NULL) 1820 type = GetTypeForDIE (curr_cu, type_die).get(); 1821 if (assert_not_being_parsed) 1822 assert (type != DIE_IS_BEING_PARSED); 1823 return type; 1824 } 1825 return NULL; 1826 } 1827 1828 CompileUnit* 1829 SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx) 1830 { 1831 // Check if the symbol vendor already knows about this compile unit? 1832 if (curr_cu->GetUserData() == NULL) 1833 { 1834 // The symbol vendor doesn't know about this compile unit, we 1835 // need to parse and add it to the symbol vendor object. 1836 CompUnitSP dc_cu; 1837 ParseCompileUnit(curr_cu, dc_cu); 1838 if (dc_cu.get()) 1839 { 1840 // Figure out the compile unit index if we weren't given one 1841 if (cu_idx == UINT32_MAX) 1842 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx); 1843 1844 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx); 1845 1846 if (m_debug_map_symfile) 1847 m_debug_map_symfile->SetCompileUnit(this, dc_cu); 1848 } 1849 } 1850 return (CompileUnit*)curr_cu->GetUserData(); 1851 } 1852 1853 bool 1854 SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc) 1855 { 1856 sc.Clear(); 1857 // Check if the symbol vendor already knows about this compile unit? 1858 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX); 1859 1860 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get(); 1861 if (sc.function == NULL) 1862 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die); 1863 1864 if (sc.function) 1865 { 1866 sc.module_sp = sc.function->CalculateSymbolContextModule(); 1867 return true; 1868 } 1869 1870 return false; 1871 } 1872 1873 uint32_t 1874 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 1875 { 1876 Timer scoped_timer(__PRETTY_FUNCTION__, 1877 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)", 1878 so_addr.GetSection(), 1879 so_addr.GetOffset(), 1880 resolve_scope); 1881 uint32_t resolved = 0; 1882 if (resolve_scope & ( eSymbolContextCompUnit | 1883 eSymbolContextFunction | 1884 eSymbolContextBlock | 1885 eSymbolContextLineEntry)) 1886 { 1887 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1888 1889 DWARFDebugInfo* debug_info = DebugInfo(); 1890 if (debug_info) 1891 { 1892 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr); 1893 if (cu_offset != DW_INVALID_OFFSET) 1894 { 1895 uint32_t cu_idx; 1896 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get(); 1897 if (curr_cu) 1898 { 1899 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1900 assert(sc.comp_unit != NULL); 1901 resolved |= eSymbolContextCompUnit; 1902 1903 if (resolve_scope & eSymbolContextLineEntry) 1904 { 1905 LineTable *line_table = sc.comp_unit->GetLineTable(); 1906 if (line_table == NULL) 1907 { 1908 if (ParseCompileUnitLineTable(sc)) 1909 line_table = sc.comp_unit->GetLineTable(); 1910 } 1911 if (line_table != NULL) 1912 { 1913 if (so_addr.IsLinkedAddress()) 1914 { 1915 Address linked_addr (so_addr); 1916 linked_addr.ResolveLinkedAddress(); 1917 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry)) 1918 { 1919 resolved |= eSymbolContextLineEntry; 1920 } 1921 } 1922 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry)) 1923 { 1924 resolved |= eSymbolContextLineEntry; 1925 } 1926 } 1927 } 1928 1929 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 1930 { 1931 DWARFDebugInfoEntry *function_die = NULL; 1932 DWARFDebugInfoEntry *block_die = NULL; 1933 if (resolve_scope & eSymbolContextBlock) 1934 { 1935 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die); 1936 } 1937 else 1938 { 1939 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL); 1940 } 1941 1942 if (function_die != NULL) 1943 { 1944 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get(); 1945 if (sc.function == NULL) 1946 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die); 1947 } 1948 1949 if (sc.function != NULL) 1950 { 1951 resolved |= eSymbolContextFunction; 1952 1953 if (resolve_scope & eSymbolContextBlock) 1954 { 1955 Block& block = sc.function->GetBlock (true); 1956 1957 if (block_die != NULL) 1958 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset())); 1959 else 1960 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset())); 1961 if (sc.block) 1962 resolved |= eSymbolContextBlock; 1963 } 1964 } 1965 } 1966 } 1967 } 1968 } 1969 } 1970 return resolved; 1971 } 1972 1973 1974 1975 uint32_t 1976 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 1977 { 1978 const uint32_t prev_size = sc_list.GetSize(); 1979 if (resolve_scope & eSymbolContextCompUnit) 1980 { 1981 DWARFDebugInfo* debug_info = DebugInfo(); 1982 if (debug_info) 1983 { 1984 uint32_t cu_idx; 1985 DWARFCompileUnit* curr_cu = NULL; 1986 1987 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx) 1988 { 1989 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1990 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0; 1991 if (check_inlines || file_spec_matches_cu_file_spec) 1992 { 1993 SymbolContext sc (m_obj_file->GetModule()); 1994 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1995 assert(sc.comp_unit != NULL); 1996 1997 uint32_t file_idx = UINT32_MAX; 1998 1999 // If we are looking for inline functions only and we don't 2000 // find it in the support files, we are done. 2001 if (check_inlines) 2002 { 2003 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 2004 if (file_idx == UINT32_MAX) 2005 continue; 2006 } 2007 2008 if (line != 0) 2009 { 2010 LineTable *line_table = sc.comp_unit->GetLineTable(); 2011 2012 if (line_table != NULL && line != 0) 2013 { 2014 // We will have already looked up the file index if 2015 // we are searching for inline entries. 2016 if (!check_inlines) 2017 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true); 2018 2019 if (file_idx != UINT32_MAX) 2020 { 2021 uint32_t found_line; 2022 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry); 2023 found_line = sc.line_entry.line; 2024 2025 while (line_idx != UINT32_MAX) 2026 { 2027 sc.function = NULL; 2028 sc.block = NULL; 2029 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 2030 { 2031 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress(); 2032 if (file_vm_addr != LLDB_INVALID_ADDRESS) 2033 { 2034 DWARFDebugInfoEntry *function_die = NULL; 2035 DWARFDebugInfoEntry *block_die = NULL; 2036 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL); 2037 2038 if (function_die != NULL) 2039 { 2040 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get(); 2041 if (sc.function == NULL) 2042 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die); 2043 } 2044 2045 if (sc.function != NULL) 2046 { 2047 Block& block = sc.function->GetBlock (true); 2048 2049 if (block_die != NULL) 2050 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset())); 2051 else 2052 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset())); 2053 } 2054 } 2055 } 2056 2057 sc_list.Append(sc); 2058 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry); 2059 } 2060 } 2061 } 2062 else if (file_spec_matches_cu_file_spec && !check_inlines) 2063 { 2064 // only append the context if we aren't looking for inline call sites 2065 // by file and line and if the file spec matches that of the compile unit 2066 sc_list.Append(sc); 2067 } 2068 } 2069 else if (file_spec_matches_cu_file_spec && !check_inlines) 2070 { 2071 // only append the context if we aren't looking for inline call sites 2072 // by file and line and if the file spec matches that of the compile unit 2073 sc_list.Append(sc); 2074 } 2075 2076 if (!check_inlines) 2077 break; 2078 } 2079 } 2080 } 2081 } 2082 return sc_list.GetSize() - prev_size; 2083 } 2084 2085 void 2086 SymbolFileDWARF::Index () 2087 { 2088 if (m_indexed) 2089 return; 2090 m_indexed = true; 2091 Timer scoped_timer (__PRETTY_FUNCTION__, 2092 "SymbolFileDWARF::Index (%s)", 2093 GetObjectFile()->GetFileSpec().GetFilename().AsCString()); 2094 2095 DWARFDebugInfo* debug_info = DebugInfo(); 2096 if (debug_info) 2097 { 2098 uint32_t cu_idx = 0; 2099 const uint32_t num_compile_units = GetNumCompileUnits(); 2100 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 2101 { 2102 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 2103 2104 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1; 2105 2106 curr_cu->Index (cu_idx, 2107 m_function_basename_index, 2108 m_function_fullname_index, 2109 m_function_method_index, 2110 m_function_selector_index, 2111 m_objc_class_selectors_index, 2112 m_global_index, 2113 m_type_index, 2114 m_namespace_index); 2115 2116 // Keep memory down by clearing DIEs if this generate function 2117 // caused them to be parsed 2118 if (clear_dies) 2119 curr_cu->ClearDIEs (true); 2120 } 2121 2122 m_function_basename_index.Finalize(); 2123 m_function_fullname_index.Finalize(); 2124 m_function_method_index.Finalize(); 2125 m_function_selector_index.Finalize(); 2126 m_objc_class_selectors_index.Finalize(); 2127 m_global_index.Finalize(); 2128 m_type_index.Finalize(); 2129 m_namespace_index.Finalize(); 2130 2131 #if defined (ENABLE_DEBUG_PRINTF) 2132 StreamFile s(stdout, false); 2133 s.Printf ("DWARF index for '%s/%s':", 2134 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(), 2135 GetObjectFile()->GetFileSpec().GetFilename().AsCString()); 2136 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 2137 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 2138 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 2139 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 2140 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 2141 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 2142 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 2143 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s); 2144 #endif 2145 } 2146 } 2147 2148 bool 2149 SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl) 2150 { 2151 if (namespace_decl == NULL) 2152 { 2153 // Invalid namespace decl which means we aren't matching only things 2154 // in this symbol file, so return true to indicate it matches this 2155 // symbol file. 2156 return true; 2157 } 2158 2159 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext(); 2160 2161 if (namespace_ast == NULL) 2162 return true; // No AST in the "namespace_decl", return true since it 2163 // could then match any symbol file, including this one 2164 2165 if (namespace_ast == GetClangASTContext().getASTContext()) 2166 return true; // The ASTs match, return true 2167 2168 // The namespace AST was valid, and it does not match... 2169 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2170 2171 if (log) 2172 log->Printf("Valid namespace does not match symbol file"); 2173 2174 return false; 2175 } 2176 2177 bool 2178 SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl, 2179 DWARFCompileUnit* cu, 2180 const DWARFDebugInfoEntry* die) 2181 { 2182 // No namespace specified, so the answesr i 2183 if (namespace_decl == NULL) 2184 return true; 2185 2186 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2187 2188 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die); 2189 if (decl_ctx_die) 2190 { 2191 2192 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl(); 2193 if (clang_namespace_decl) 2194 { 2195 if (decl_ctx_die->Tag() != DW_TAG_namespace) 2196 { 2197 if (log) 2198 log->Printf("Found a match, but its parent is not a namespace"); 2199 return false; 2200 } 2201 2202 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl); 2203 2204 if (pos == m_decl_ctx_to_die.end()) 2205 { 2206 if (log) 2207 log->Printf("Found a match in a namespace, but its parent is not the requested namespace"); 2208 2209 return false; 2210 } 2211 2212 return pos->second.count (decl_ctx_die); 2213 } 2214 else 2215 { 2216 // We have a namespace_decl that was not NULL but it contained 2217 // a NULL "clang::NamespaceDecl", so this means the global namespace 2218 // So as long the the contained decl context DIE isn't a namespace 2219 // we should be ok. 2220 if (decl_ctx_die->Tag() != DW_TAG_namespace) 2221 return true; 2222 } 2223 } 2224 2225 if (log) 2226 log->Printf("Found a match, but its parent doesn't exist"); 2227 2228 return false; 2229 } 2230 uint32_t 2231 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables) 2232 { 2233 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2234 2235 if (log) 2236 { 2237 log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", name=\"%s\", append=%u, max_matches=%u, variables)", 2238 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2239 m_obj_file->GetFileSpec().GetFilename().GetCString(), 2240 name.GetCString(), append, max_matches); 2241 } 2242 2243 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 2244 return 0; 2245 2246 DWARFDebugInfo* info = DebugInfo(); 2247 if (info == NULL) 2248 return 0; 2249 2250 // If we aren't appending the results to this list, then clear the list 2251 if (!append) 2252 variables.Clear(); 2253 2254 // Remember how many variables are in the list before we search in case 2255 // we are appending the results to a variable list. 2256 const uint32_t original_size = variables.GetSize(); 2257 2258 DIEArray die_offsets; 2259 2260 if (m_using_apple_tables) 2261 { 2262 if (m_apple_names_ap.get()) 2263 { 2264 const char *name_cstr = name.GetCString(); 2265 const char *base_name_start; 2266 const char *base_name_end = NULL; 2267 2268 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end)) 2269 base_name_start = name_cstr; 2270 2271 m_apple_names_ap->FindByName (base_name_start, die_offsets); 2272 } 2273 } 2274 else 2275 { 2276 // Index the DWARF if we haven't already 2277 if (!m_indexed) 2278 Index (); 2279 2280 m_global_index.Find (name, die_offsets); 2281 } 2282 2283 const size_t num_matches = die_offsets.size(); 2284 if (num_matches) 2285 { 2286 SymbolContext sc; 2287 sc.module_sp = m_obj_file->GetModule(); 2288 assert (sc.module_sp); 2289 2290 DWARFDebugInfo* debug_info = DebugInfo(); 2291 DWARFCompileUnit* dwarf_cu = NULL; 2292 const DWARFDebugInfoEntry* die = NULL; 2293 for (size_t i=0; i<num_matches; ++i) 2294 { 2295 const dw_offset_t die_offset = die_offsets[i]; 2296 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 2297 2298 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 2299 assert(sc.comp_unit != NULL); 2300 2301 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 2302 continue; 2303 2304 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 2305 2306 if (variables.GetSize() - original_size >= max_matches) 2307 break; 2308 } 2309 } 2310 2311 // Return the number of variable that were appended to the list 2312 return variables.GetSize() - original_size; 2313 } 2314 2315 uint32_t 2316 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 2317 { 2318 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2319 2320 if (log) 2321 { 2322 log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", regex=\"%s\", append=%u, max_matches=%u, variables)", 2323 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2324 m_obj_file->GetFileSpec().GetFilename().GetCString(), 2325 regex.GetText(), append, max_matches); 2326 } 2327 2328 DWARFDebugInfo* info = DebugInfo(); 2329 if (info == NULL) 2330 return 0; 2331 2332 // If we aren't appending the results to this list, then clear the list 2333 if (!append) 2334 variables.Clear(); 2335 2336 // Remember how many variables are in the list before we search in case 2337 // we are appending the results to a variable list. 2338 const uint32_t original_size = variables.GetSize(); 2339 2340 DIEArray die_offsets; 2341 2342 if (m_using_apple_tables) 2343 { 2344 if (m_apple_names_ap.get()) 2345 m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, die_offsets); 2346 } 2347 else 2348 { 2349 // Index the DWARF if we haven't already 2350 if (!m_indexed) 2351 Index (); 2352 2353 m_global_index.Find (regex, die_offsets); 2354 } 2355 2356 SymbolContext sc; 2357 sc.module_sp = m_obj_file->GetModule(); 2358 assert (sc.module_sp); 2359 2360 DWARFCompileUnit* dwarf_cu = NULL; 2361 const DWARFDebugInfoEntry* die = NULL; 2362 const size_t num_matches = die_offsets.size(); 2363 if (num_matches) 2364 { 2365 DWARFDebugInfo* debug_info = DebugInfo(); 2366 for (size_t i=0; i<num_matches; ++i) 2367 { 2368 const dw_offset_t die_offset = die_offsets[i]; 2369 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 2370 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 2371 2372 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 2373 2374 if (variables.GetSize() - original_size >= max_matches) 2375 break; 2376 } 2377 } 2378 2379 // Return the number of variable that were appended to the list 2380 return variables.GetSize() - original_size; 2381 } 2382 2383 2384 bool 2385 SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset, 2386 DWARFCompileUnit *&dwarf_cu, 2387 SymbolContextList& sc_list) 2388 { 2389 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 2390 return ResolveFunction (dwarf_cu, die, sc_list); 2391 } 2392 2393 2394 bool 2395 SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu, 2396 const DWARFDebugInfoEntry *die, 2397 SymbolContextList& sc_list) 2398 { 2399 SymbolContext sc; 2400 2401 if (die == NULL) 2402 return false; 2403 2404 // If we were passed a die that is not a function, just return false... 2405 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine) 2406 return false; 2407 2408 const DWARFDebugInfoEntry* inlined_die = NULL; 2409 if (die->Tag() == DW_TAG_inlined_subroutine) 2410 { 2411 inlined_die = die; 2412 2413 while ((die = die->GetParent()) != NULL) 2414 { 2415 if (die->Tag() == DW_TAG_subprogram) 2416 break; 2417 } 2418 } 2419 assert (die->Tag() == DW_TAG_subprogram); 2420 if (GetFunction (cu, die, sc)) 2421 { 2422 Address addr; 2423 // Parse all blocks if needed 2424 if (inlined_die) 2425 { 2426 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset())); 2427 assert (sc.block != NULL); 2428 if (sc.block->GetStartAddress (addr) == false) 2429 addr.Clear(); 2430 } 2431 else 2432 { 2433 sc.block = NULL; 2434 addr = sc.function->GetAddressRange().GetBaseAddress(); 2435 } 2436 2437 if (addr.IsValid()) 2438 { 2439 2440 // We found the function, so we should find the line table 2441 // and line table entry as well 2442 LineTable *line_table = sc.comp_unit->GetLineTable(); 2443 if (line_table == NULL) 2444 { 2445 if (ParseCompileUnitLineTable(sc)) 2446 line_table = sc.comp_unit->GetLineTable(); 2447 } 2448 if (line_table != NULL) 2449 line_table->FindLineEntryByAddress (addr, sc.line_entry); 2450 2451 sc_list.Append(sc); 2452 return true; 2453 } 2454 } 2455 2456 return false; 2457 } 2458 2459 void 2460 SymbolFileDWARF::FindFunctions (const ConstString &name, 2461 const NameToDIE &name_to_die, 2462 SymbolContextList& sc_list) 2463 { 2464 DIEArray die_offsets; 2465 if (name_to_die.Find (name, die_offsets)) 2466 { 2467 ParseFunctions (die_offsets, sc_list); 2468 } 2469 } 2470 2471 2472 void 2473 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 2474 const NameToDIE &name_to_die, 2475 SymbolContextList& sc_list) 2476 { 2477 DIEArray die_offsets; 2478 if (name_to_die.Find (regex, die_offsets)) 2479 { 2480 ParseFunctions (die_offsets, sc_list); 2481 } 2482 } 2483 2484 2485 void 2486 SymbolFileDWARF::FindFunctions (const RegularExpression ®ex, 2487 const DWARFMappedHash::MemoryTable &memory_table, 2488 SymbolContextList& sc_list) 2489 { 2490 DIEArray die_offsets; 2491 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, die_offsets)) 2492 { 2493 ParseFunctions (die_offsets, sc_list); 2494 } 2495 } 2496 2497 void 2498 SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets, 2499 SymbolContextList& sc_list) 2500 { 2501 const size_t num_matches = die_offsets.size(); 2502 if (num_matches) 2503 { 2504 SymbolContext sc; 2505 2506 DWARFCompileUnit* dwarf_cu = NULL; 2507 for (size_t i=0; i<num_matches; ++i) 2508 { 2509 const dw_offset_t die_offset = die_offsets[i]; 2510 ResolveFunction (die_offset, dwarf_cu, sc_list); 2511 } 2512 } 2513 } 2514 2515 bool 2516 SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die, 2517 const DWARFCompileUnit *dwarf_cu, 2518 uint32_t name_type_mask, 2519 const char *partial_name, 2520 const char *base_name_start, 2521 const char *base_name_end) 2522 { 2523 // If we are looking only for methods, throw away all the ones that aren't in C++ classes: 2524 if (name_type_mask == eFunctionNameTypeMethod 2525 || name_type_mask == eFunctionNameTypeBase) 2526 { 2527 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset()); 2528 if (!containing_decl_ctx) 2529 return false; 2530 2531 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()); 2532 2533 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod) 2534 return false; 2535 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase) 2536 return false; 2537 } 2538 2539 // Now we need to check whether the name we got back for this type matches the extra specifications 2540 // that were in the name we're looking up: 2541 if (base_name_start != partial_name || *base_name_end != '\0') 2542 { 2543 // First see if the stuff to the left matches the full name. To do that let's see if 2544 // we can pull out the mips linkage name attribute: 2545 2546 Mangled best_name; 2547 2548 DWARFDebugInfoEntry::Attributes attributes; 2549 die->GetAttributes(this, dwarf_cu, NULL, attributes); 2550 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name); 2551 if (idx != UINT32_MAX) 2552 { 2553 DWARFFormValue form_value; 2554 if (attributes.ExtractFormValueAtIndex(this, idx, form_value)) 2555 { 2556 const char *name = form_value.AsCString(&get_debug_str_data()); 2557 best_name.SetValue (name, true); 2558 } 2559 } 2560 if (best_name) 2561 { 2562 const char *demangled = best_name.GetDemangledName().GetCString(); 2563 if (demangled) 2564 { 2565 std::string name_no_parens(partial_name, base_name_end - partial_name); 2566 if (strstr (demangled, name_no_parens.c_str()) == NULL) 2567 return false; 2568 } 2569 } 2570 } 2571 2572 return true; 2573 } 2574 2575 uint32_t 2576 SymbolFileDWARF::FindFunctions (const ConstString &name, 2577 const lldb_private::ClangNamespaceDecl *namespace_decl, 2578 uint32_t name_type_mask, 2579 bool append, 2580 SymbolContextList& sc_list) 2581 { 2582 Timer scoped_timer (__PRETTY_FUNCTION__, 2583 "SymbolFileDWARF::FindFunctions (name = '%s')", 2584 name.AsCString()); 2585 2586 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2587 2588 if (log) 2589 { 2590 log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)", 2591 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2592 m_obj_file->GetFileSpec().GetFilename().GetCString(), 2593 name.GetCString(), name_type_mask, append); 2594 } 2595 2596 // If we aren't appending the results to this list, then clear the list 2597 if (!append) 2598 sc_list.Clear(); 2599 2600 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 2601 return 0; 2602 2603 // If name is empty then we won't find anything. 2604 if (name.IsEmpty()) 2605 return 0; 2606 2607 // Remember how many sc_list are in the list before we search in case 2608 // we are appending the results to a variable list. 2609 2610 const uint32_t original_size = sc_list.GetSize(); 2611 2612 const char *name_cstr = name.GetCString(); 2613 uint32_t effective_name_type_mask = eFunctionNameTypeNone; 2614 const char *base_name_start = name_cstr; 2615 const char *base_name_end = name_cstr + strlen(name_cstr); 2616 2617 if (name_type_mask & eFunctionNameTypeAuto) 2618 { 2619 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr)) 2620 effective_name_type_mask = eFunctionNameTypeFull; 2621 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr)) 2622 effective_name_type_mask = eFunctionNameTypeFull; 2623 else 2624 { 2625 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr)) 2626 effective_name_type_mask |= eFunctionNameTypeSelector; 2627 2628 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end)) 2629 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase); 2630 } 2631 } 2632 else 2633 { 2634 effective_name_type_mask = name_type_mask; 2635 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase) 2636 { 2637 // If they've asked for a CPP method or function name and it can't be that, we don't 2638 // even need to search for CPP methods or names. 2639 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end)) 2640 { 2641 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase); 2642 if (effective_name_type_mask == eFunctionNameTypeNone) 2643 return 0; 2644 } 2645 } 2646 2647 if (effective_name_type_mask & eFunctionNameTypeSelector) 2648 { 2649 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr)) 2650 { 2651 effective_name_type_mask &= ~(eFunctionNameTypeSelector); 2652 if (effective_name_type_mask == eFunctionNameTypeNone) 2653 return 0; 2654 } 2655 } 2656 } 2657 2658 DWARFDebugInfo* info = DebugInfo(); 2659 if (info == NULL) 2660 return 0; 2661 2662 DWARFCompileUnit *dwarf_cu = NULL; 2663 if (m_using_apple_tables) 2664 { 2665 if (m_apple_names_ap.get()) 2666 { 2667 2668 DIEArray die_offsets; 2669 2670 uint32_t num_matches = 0; 2671 2672 if (effective_name_type_mask & eFunctionNameTypeFull) 2673 { 2674 // If they asked for the full name, match what they typed. At some point we may 2675 // want to canonicalize this (strip double spaces, etc. For now, we just add all the 2676 // dies that we find by exact match. 2677 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 2678 for (uint32_t i = 0; i < num_matches; i++) 2679 { 2680 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 2681 if (die) 2682 { 2683 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 2684 continue; 2685 2686 ResolveFunction (dwarf_cu, die, sc_list); 2687 } 2688 } 2689 } 2690 else 2691 { 2692 if (effective_name_type_mask & eFunctionNameTypeSelector) 2693 { 2694 if (namespace_decl && *namespace_decl) 2695 return 0; // no selectors in namespaces 2696 2697 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets); 2698 // Now make sure these are actually ObjC methods. In this case we can simply look up the name, 2699 // and if it is an ObjC method name, we're good. 2700 2701 for (uint32_t i = 0; i < num_matches; i++) 2702 { 2703 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 2704 if (die) 2705 { 2706 const char *die_name = die->GetName(this, dwarf_cu); 2707 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name)) 2708 ResolveFunction (dwarf_cu, die, sc_list); 2709 } 2710 } 2711 die_offsets.clear(); 2712 } 2713 2714 if (effective_name_type_mask & eFunctionNameTypeMethod 2715 || effective_name_type_mask & eFunctionNameTypeBase) 2716 { 2717 if ((effective_name_type_mask & eFunctionNameTypeMethod) && 2718 (namespace_decl && *namespace_decl)) 2719 return 0; // no methods in namespaces 2720 2721 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to 2722 // extract the base name, look that up, and if there is any other information in the name we were 2723 // passed in we have to post-filter based on that. 2724 2725 // FIXME: Arrange the logic above so that we don't calculate the base name twice: 2726 std::string base_name(base_name_start, base_name_end - base_name_start); 2727 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets); 2728 2729 for (uint32_t i = 0; i < num_matches; i++) 2730 { 2731 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 2732 if (die) 2733 { 2734 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 2735 continue; 2736 2737 if (!FunctionDieMatchesPartialName(die, 2738 dwarf_cu, 2739 effective_name_type_mask, 2740 name_cstr, 2741 base_name_start, 2742 base_name_end)) 2743 continue; 2744 2745 // If we get to here, the die is good, and we should add it: 2746 ResolveFunction (dwarf_cu, die, sc_list); 2747 } 2748 } 2749 die_offsets.clear(); 2750 } 2751 } 2752 } 2753 } 2754 else 2755 { 2756 2757 // Index the DWARF if we haven't already 2758 if (!m_indexed) 2759 Index (); 2760 2761 if (name_type_mask & eFunctionNameTypeFull) 2762 FindFunctions (name, m_function_fullname_index, sc_list); 2763 2764 std::string base_name(base_name_start, base_name_end - base_name_start); 2765 ConstString base_name_const(base_name.c_str()); 2766 DIEArray die_offsets; 2767 DWARFCompileUnit *dwarf_cu = NULL; 2768 2769 if (effective_name_type_mask & eFunctionNameTypeBase) 2770 { 2771 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets); 2772 for (uint32_t i = 0; i < num_base; i++) 2773 { 2774 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 2775 if (die) 2776 { 2777 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 2778 continue; 2779 2780 if (!FunctionDieMatchesPartialName(die, 2781 dwarf_cu, 2782 effective_name_type_mask, 2783 name_cstr, 2784 base_name_start, 2785 base_name_end)) 2786 continue; 2787 2788 // If we get to here, the die is good, and we should add it: 2789 ResolveFunction (dwarf_cu, die, sc_list); 2790 } 2791 } 2792 die_offsets.clear(); 2793 } 2794 2795 if (effective_name_type_mask & eFunctionNameTypeMethod) 2796 { 2797 if (namespace_decl && *namespace_decl) 2798 return 0; // no methods in namespaces 2799 2800 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets); 2801 { 2802 for (uint32_t i = 0; i < num_base; i++) 2803 { 2804 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu); 2805 if (die) 2806 { 2807 if (!FunctionDieMatchesPartialName(die, 2808 dwarf_cu, 2809 effective_name_type_mask, 2810 name_cstr, 2811 base_name_start, 2812 base_name_end)) 2813 continue; 2814 2815 // If we get to here, the die is good, and we should add it: 2816 ResolveFunction (dwarf_cu, die, sc_list); 2817 } 2818 } 2819 } 2820 die_offsets.clear(); 2821 } 2822 2823 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl)) 2824 { 2825 FindFunctions (name, m_function_selector_index, sc_list); 2826 } 2827 2828 } 2829 2830 // Return the number of variable that were appended to the list 2831 return sc_list.GetSize() - original_size; 2832 } 2833 2834 uint32_t 2835 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list) 2836 { 2837 Timer scoped_timer (__PRETTY_FUNCTION__, 2838 "SymbolFileDWARF::FindFunctions (regex = '%s')", 2839 regex.GetText()); 2840 2841 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2842 2843 if (log) 2844 { 2845 log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", regex=\"%s\"append=%u, sc_list)", 2846 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2847 m_obj_file->GetFileSpec().GetFilename().GetCString(), 2848 regex.GetText(), append); 2849 } 2850 2851 2852 // If we aren't appending the results to this list, then clear the list 2853 if (!append) 2854 sc_list.Clear(); 2855 2856 // Remember how many sc_list are in the list before we search in case 2857 // we are appending the results to a variable list. 2858 uint32_t original_size = sc_list.GetSize(); 2859 2860 if (m_using_apple_tables) 2861 { 2862 if (m_apple_names_ap.get()) 2863 FindFunctions (regex, *m_apple_names_ap, sc_list); 2864 } 2865 else 2866 { 2867 // Index the DWARF if we haven't already 2868 if (!m_indexed) 2869 Index (); 2870 2871 FindFunctions (regex, m_function_basename_index, sc_list); 2872 2873 FindFunctions (regex, m_function_fullname_index, sc_list); 2874 } 2875 2876 // Return the number of variable that were appended to the list 2877 return sc_list.GetSize() - original_size; 2878 } 2879 2880 void 2881 SymbolFileDWARF::ReportError (const char *format, ...) 2882 { 2883 ::fprintf (stderr, 2884 "error: %s/%s ", 2885 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2886 m_obj_file->GetFileSpec().GetFilename().GetCString()); 2887 2888 if (m_obj_file->GetModule()->GetObjectName()) 2889 ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString()); 2890 2891 va_list args; 2892 va_start (args, format); 2893 vfprintf (stderr, format, args); 2894 va_end (args); 2895 } 2896 2897 void 2898 SymbolFileDWARF::ReportWarning (const char *format, ...) 2899 { 2900 ::fprintf (stderr, 2901 "warning: %s/%s ", 2902 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2903 m_obj_file->GetFileSpec().GetFilename().GetCString()); 2904 2905 if (m_obj_file->GetModule()->GetObjectName()) 2906 ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString()); 2907 2908 va_list args; 2909 va_start (args, format); 2910 vfprintf (stderr, format, args); 2911 va_end (args); 2912 } 2913 2914 uint32_t 2915 SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types) 2916 { 2917 DWARFDebugInfo* info = DebugInfo(); 2918 if (info == NULL) 2919 return 0; 2920 2921 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2922 2923 if (log) 2924 { 2925 log->Printf ("SymbolFileDWARF::FindTypes (file=\"%s/%s\", sc, name=\"%s\", append=%u, max_matches=%u, type_list)", 2926 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2927 m_obj_file->GetFileSpec().GetFilename().GetCString(), 2928 name.GetCString(), append, max_matches); 2929 } 2930 2931 // If we aren't appending the results to this list, then clear the list 2932 if (!append) 2933 types.Clear(); 2934 2935 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl)) 2936 return 0; 2937 2938 DIEArray die_offsets; 2939 2940 if (m_using_apple_tables) 2941 { 2942 if (m_apple_types_ap.get()) 2943 { 2944 const char *name_cstr = name.GetCString(); 2945 m_apple_types_ap->FindByName (name_cstr, die_offsets); 2946 } 2947 } 2948 else 2949 { 2950 if (!m_indexed) 2951 Index (); 2952 2953 m_type_index.Find (name, die_offsets); 2954 } 2955 2956 2957 const size_t num_matches = die_offsets.size(); 2958 2959 if (num_matches) 2960 { 2961 const uint32_t initial_types_size = types.GetSize(); 2962 DWARFCompileUnit* dwarf_cu = NULL; 2963 const DWARFDebugInfoEntry* die = NULL; 2964 DWARFDebugInfo* debug_info = DebugInfo(); 2965 for (size_t i=0; i<num_matches; ++i) 2966 { 2967 const dw_offset_t die_offset = die_offsets[i]; 2968 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 2969 2970 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die)) 2971 continue; 2972 2973 Type *matching_type = ResolveType (dwarf_cu, die); 2974 if (matching_type) 2975 { 2976 // We found a type pointer, now find the shared pointer form our type list 2977 types.InsertUnique (TypeSP (matching_type)); 2978 if (types.GetSize() >= max_matches) 2979 break; 2980 } 2981 } 2982 return types.GetSize() - initial_types_size; 2983 } 2984 return 0; 2985 } 2986 2987 2988 ClangNamespaceDecl 2989 SymbolFileDWARF::FindNamespace (const SymbolContext& sc, 2990 const ConstString &name, 2991 const lldb_private::ClangNamespaceDecl *parent_namespace_decl) 2992 { 2993 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2994 2995 if (log) 2996 { 2997 log->Printf ("SymbolFileDWARF::FindNamespace (file=\"%s/%s\", sc, name=\"%s\")", 2998 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 2999 m_obj_file->GetFileSpec().GetFilename().GetCString(), 3000 name.GetCString()); 3001 } 3002 3003 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl)) 3004 return ClangNamespaceDecl(); 3005 3006 ClangNamespaceDecl namespace_decl; 3007 DWARFDebugInfo* info = DebugInfo(); 3008 if (info) 3009 { 3010 DIEArray die_offsets; 3011 3012 // Index if we already haven't to make sure the compile units 3013 // get indexed and make their global DIE index list 3014 if (m_using_apple_tables) 3015 { 3016 if (m_apple_namespaces_ap.get()) 3017 { 3018 const char *name_cstr = name.GetCString(); 3019 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets); 3020 } 3021 } 3022 else 3023 { 3024 if (!m_indexed) 3025 Index (); 3026 3027 m_namespace_index.Find (name, die_offsets); 3028 } 3029 3030 DWARFCompileUnit* dwarf_cu = NULL; 3031 const DWARFDebugInfoEntry* die = NULL; 3032 const size_t num_matches = die_offsets.size(); 3033 if (num_matches) 3034 { 3035 DWARFDebugInfo* debug_info = DebugInfo(); 3036 for (size_t i=0; i<num_matches; ++i) 3037 { 3038 const dw_offset_t die_offset = die_offsets[i]; 3039 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 3040 3041 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die)) 3042 continue; 3043 3044 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die); 3045 if (clang_namespace_decl) 3046 { 3047 namespace_decl.SetASTContext (GetClangASTContext().getASTContext()); 3048 namespace_decl.SetNamespaceDecl (clang_namespace_decl); 3049 } 3050 } 3051 } 3052 } 3053 return namespace_decl; 3054 } 3055 3056 uint32_t 3057 SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types) 3058 { 3059 // Remember how many sc_list are in the list before we search in case 3060 // we are appending the results to a variable list. 3061 uint32_t original_size = types.GetSize(); 3062 3063 const uint32_t num_die_offsets = die_offsets.size(); 3064 // Parse all of the types we found from the pubtypes matches 3065 uint32_t i; 3066 uint32_t num_matches = 0; 3067 for (i = 0; i < num_die_offsets; ++i) 3068 { 3069 Type *matching_type = ResolveTypeUID (die_offsets[i]); 3070 if (matching_type) 3071 { 3072 // We found a type pointer, now find the shared pointer form our type list 3073 types.InsertUnique (TypeSP (matching_type)); 3074 ++num_matches; 3075 if (num_matches >= max_matches) 3076 break; 3077 } 3078 } 3079 3080 // Return the number of variable that were appended to the list 3081 return types.GetSize() - original_size; 3082 } 3083 3084 3085 size_t 3086 SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc, 3087 clang::DeclContext *containing_decl_ctx, 3088 TypeSP& type_sp, 3089 DWARFCompileUnit* dwarf_cu, 3090 const DWARFDebugInfoEntry *parent_die, 3091 bool skip_artificial, 3092 bool &is_static, 3093 TypeList* type_list, 3094 std::vector<clang_type_t>& function_param_types, 3095 std::vector<clang::ParmVarDecl*>& function_param_decls, 3096 unsigned &type_quals) 3097 { 3098 if (parent_die == NULL) 3099 return 0; 3100 3101 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 3102 3103 size_t arg_idx = 0; 3104 const DWARFDebugInfoEntry *die; 3105 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 3106 { 3107 dw_tag_t tag = die->Tag(); 3108 switch (tag) 3109 { 3110 case DW_TAG_formal_parameter: 3111 { 3112 DWARFDebugInfoEntry::Attributes attributes; 3113 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 3114 if (num_attributes > 0) 3115 { 3116 const char *name = NULL; 3117 Declaration decl; 3118 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET; 3119 bool is_artificial = false; 3120 // one of None, Auto, Register, Extern, Static, PrivateExtern 3121 3122 clang::StorageClass storage = clang::SC_None; 3123 uint32_t i; 3124 for (i=0; i<num_attributes; ++i) 3125 { 3126 const dw_attr_t attr = attributes.AttributeAtIndex(i); 3127 DWARFFormValue form_value; 3128 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3129 { 3130 switch (attr) 3131 { 3132 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3133 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3134 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3135 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 3136 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break; 3137 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 3138 case DW_AT_location: 3139 // if (form_value.BlockData()) 3140 // { 3141 // const DataExtractor& debug_info_data = debug_info(); 3142 // uint32_t block_length = form_value.Unsigned(); 3143 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length); 3144 // } 3145 // else 3146 // { 3147 // } 3148 // break; 3149 case DW_AT_const_value: 3150 case DW_AT_default_value: 3151 case DW_AT_description: 3152 case DW_AT_endianity: 3153 case DW_AT_is_optional: 3154 case DW_AT_segment: 3155 case DW_AT_variable_parameter: 3156 default: 3157 case DW_AT_abstract_origin: 3158 case DW_AT_sibling: 3159 break; 3160 } 3161 } 3162 } 3163 3164 bool skip = false; 3165 if (skip_artificial) 3166 { 3167 if (is_artificial) 3168 { 3169 // In order to determine if a C++ member function is 3170 // "const" we have to look at the const-ness of "this"... 3171 // Ugly, but that 3172 if (arg_idx == 0) 3173 { 3174 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind())) 3175 { 3176 // Often times compilers omit the "this" name for the 3177 // specification DIEs, so we can't rely upon the name 3178 // being in the formal parameter DIE... 3179 if (name == NULL || ::strcmp(name, "this")==0) 3180 { 3181 Type *this_type = ResolveTypeUID (param_type_die_offset); 3182 if (this_type) 3183 { 3184 uint32_t encoding_mask = this_type->GetEncodingMask(); 3185 if (encoding_mask & Type::eEncodingIsPointerUID) 3186 { 3187 is_static = false; 3188 3189 if (encoding_mask & (1u << Type::eEncodingIsConstUID)) 3190 type_quals |= clang::Qualifiers::Const; 3191 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID)) 3192 type_quals |= clang::Qualifiers::Volatile; 3193 } 3194 } 3195 } 3196 } 3197 } 3198 skip = true; 3199 } 3200 else 3201 { 3202 3203 // HACK: Objective C formal parameters "self" and "_cmd" 3204 // are not marked as artificial in the DWARF... 3205 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 3206 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus)) 3207 { 3208 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0)) 3209 skip = true; 3210 } 3211 } 3212 } 3213 3214 if (!skip) 3215 { 3216 Type *type = ResolveTypeUID(param_type_die_offset); 3217 if (type) 3218 { 3219 function_param_types.push_back (type->GetClangForwardType()); 3220 3221 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage); 3222 assert(param_var_decl); 3223 function_param_decls.push_back(param_var_decl); 3224 } 3225 } 3226 } 3227 arg_idx++; 3228 } 3229 break; 3230 3231 default: 3232 break; 3233 } 3234 } 3235 return arg_idx; 3236 } 3237 3238 size_t 3239 SymbolFileDWARF::ParseChildEnumerators 3240 ( 3241 const SymbolContext& sc, 3242 clang_type_t enumerator_clang_type, 3243 uint32_t enumerator_byte_size, 3244 DWARFCompileUnit* dwarf_cu, 3245 const DWARFDebugInfoEntry *parent_die 3246 ) 3247 { 3248 if (parent_die == NULL) 3249 return 0; 3250 3251 size_t enumerators_added = 0; 3252 const DWARFDebugInfoEntry *die; 3253 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 3254 3255 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 3256 { 3257 const dw_tag_t tag = die->Tag(); 3258 if (tag == DW_TAG_enumerator) 3259 { 3260 DWARFDebugInfoEntry::Attributes attributes; 3261 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 3262 if (num_child_attributes > 0) 3263 { 3264 const char *name = NULL; 3265 bool got_value = false; 3266 int64_t enum_value = 0; 3267 Declaration decl; 3268 3269 uint32_t i; 3270 for (i=0; i<num_child_attributes; ++i) 3271 { 3272 const dw_attr_t attr = attributes.AttributeAtIndex(i); 3273 DWARFFormValue form_value; 3274 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3275 { 3276 switch (attr) 3277 { 3278 case DW_AT_const_value: 3279 got_value = true; 3280 enum_value = form_value.Unsigned(); 3281 break; 3282 3283 case DW_AT_name: 3284 name = form_value.AsCString(&get_debug_str_data()); 3285 break; 3286 3287 case DW_AT_description: 3288 default: 3289 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3290 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3291 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3292 case DW_AT_sibling: 3293 break; 3294 } 3295 } 3296 } 3297 3298 if (name && name[0] && got_value) 3299 { 3300 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type, 3301 enumerator_clang_type, 3302 decl, 3303 name, 3304 enum_value, 3305 enumerator_byte_size * 8); 3306 ++enumerators_added; 3307 } 3308 } 3309 } 3310 } 3311 return enumerators_added; 3312 } 3313 3314 void 3315 SymbolFileDWARF::ParseChildArrayInfo 3316 ( 3317 const SymbolContext& sc, 3318 DWARFCompileUnit* dwarf_cu, 3319 const DWARFDebugInfoEntry *parent_die, 3320 int64_t& first_index, 3321 std::vector<uint64_t>& element_orders, 3322 uint32_t& byte_stride, 3323 uint32_t& bit_stride 3324 ) 3325 { 3326 if (parent_die == NULL) 3327 return; 3328 3329 const DWARFDebugInfoEntry *die; 3330 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 3331 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 3332 { 3333 const dw_tag_t tag = die->Tag(); 3334 switch (tag) 3335 { 3336 case DW_TAG_enumerator: 3337 { 3338 DWARFDebugInfoEntry::Attributes attributes; 3339 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 3340 if (num_child_attributes > 0) 3341 { 3342 const char *name = NULL; 3343 bool got_value = false; 3344 int64_t enum_value = 0; 3345 3346 uint32_t i; 3347 for (i=0; i<num_child_attributes; ++i) 3348 { 3349 const dw_attr_t attr = attributes.AttributeAtIndex(i); 3350 DWARFFormValue form_value; 3351 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3352 { 3353 switch (attr) 3354 { 3355 case DW_AT_const_value: 3356 got_value = true; 3357 enum_value = form_value.Unsigned(); 3358 break; 3359 3360 case DW_AT_name: 3361 name = form_value.AsCString(&get_debug_str_data()); 3362 break; 3363 3364 case DW_AT_description: 3365 default: 3366 case DW_AT_decl_file: 3367 case DW_AT_decl_line: 3368 case DW_AT_decl_column: 3369 case DW_AT_sibling: 3370 break; 3371 } 3372 } 3373 } 3374 } 3375 } 3376 break; 3377 3378 case DW_TAG_subrange_type: 3379 { 3380 DWARFDebugInfoEntry::Attributes attributes; 3381 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 3382 if (num_child_attributes > 0) 3383 { 3384 const char *name = NULL; 3385 bool got_value = false; 3386 uint64_t byte_size = 0; 3387 int64_t enum_value = 0; 3388 uint64_t num_elements = 0; 3389 uint64_t lower_bound = 0; 3390 uint64_t upper_bound = 0; 3391 uint32_t i; 3392 for (i=0; i<num_child_attributes; ++i) 3393 { 3394 const dw_attr_t attr = attributes.AttributeAtIndex(i); 3395 DWARFFormValue form_value; 3396 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3397 { 3398 switch (attr) 3399 { 3400 case DW_AT_const_value: 3401 got_value = true; 3402 enum_value = form_value.Unsigned(); 3403 break; 3404 3405 case DW_AT_name: 3406 name = form_value.AsCString(&get_debug_str_data()); 3407 break; 3408 3409 case DW_AT_count: 3410 num_elements = form_value.Unsigned(); 3411 break; 3412 3413 case DW_AT_bit_stride: 3414 bit_stride = form_value.Unsigned(); 3415 break; 3416 3417 case DW_AT_byte_stride: 3418 byte_stride = form_value.Unsigned(); 3419 break; 3420 3421 case DW_AT_byte_size: 3422 byte_size = form_value.Unsigned(); 3423 break; 3424 3425 case DW_AT_lower_bound: 3426 lower_bound = form_value.Unsigned(); 3427 break; 3428 3429 case DW_AT_upper_bound: 3430 upper_bound = form_value.Unsigned(); 3431 break; 3432 3433 default: 3434 case DW_AT_abstract_origin: 3435 case DW_AT_accessibility: 3436 case DW_AT_allocated: 3437 case DW_AT_associated: 3438 case DW_AT_data_location: 3439 case DW_AT_declaration: 3440 case DW_AT_description: 3441 case DW_AT_sibling: 3442 case DW_AT_threads_scaled: 3443 case DW_AT_type: 3444 case DW_AT_visibility: 3445 break; 3446 } 3447 } 3448 } 3449 3450 if (upper_bound > lower_bound) 3451 num_elements = upper_bound - lower_bound + 1; 3452 3453 if (num_elements > 0) 3454 element_orders.push_back (num_elements); 3455 } 3456 } 3457 break; 3458 } 3459 } 3460 } 3461 3462 TypeSP 3463 SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die) 3464 { 3465 TypeSP type_sp; 3466 if (die != NULL) 3467 { 3468 assert(curr_cu != NULL); 3469 Type *type_ptr = m_die_to_type.lookup (die); 3470 if (type_ptr == NULL) 3471 { 3472 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu); 3473 assert (lldb_cu); 3474 SymbolContext sc(lldb_cu); 3475 type_sp = ParseType(sc, curr_cu, die, NULL); 3476 } 3477 else if (type_ptr != DIE_IS_BEING_PARSED) 3478 { 3479 // Grab the existing type from the master types lists 3480 type_sp = type_ptr; 3481 } 3482 3483 } 3484 return type_sp; 3485 } 3486 3487 clang::DeclContext * 3488 SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset) 3489 { 3490 if (die_offset != DW_INVALID_OFFSET) 3491 { 3492 DWARFCompileUnitSP cu_sp; 3493 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp); 3494 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL); 3495 } 3496 return NULL; 3497 } 3498 3499 clang::DeclContext * 3500 SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset) 3501 { 3502 if (die_offset != DW_INVALID_OFFSET) 3503 { 3504 DWARFDebugInfo* debug_info = DebugInfo(); 3505 if (debug_info) 3506 { 3507 DWARFCompileUnitSP cu_sp; 3508 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp); 3509 if (die) 3510 return GetClangDeclContextForDIE (sc, cu_sp.get(), die); 3511 } 3512 } 3513 return NULL; 3514 } 3515 3516 clang::NamespaceDecl * 3517 SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die) 3518 { 3519 if (die && die->Tag() == DW_TAG_namespace) 3520 { 3521 // See if we already parsed this namespace DIE and associated it with a 3522 // uniqued namespace declaration 3523 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]); 3524 if (namespace_decl) 3525 return namespace_decl; 3526 else 3527 { 3528 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL); 3529 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL); 3530 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx); 3531 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 3532 if (log) 3533 { 3534 const char *object_name = m_obj_file->GetModule()->GetObjectName().GetCString(); 3535 if (namespace_name) 3536 { 3537 log->Printf ("ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl * %p in %s/%s%s%s%s (original = %p)", 3538 GetClangASTContext().getASTContext(), 3539 MakeUserID(die->GetOffset()), 3540 namespace_name, 3541 namespace_decl, 3542 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 3543 m_obj_file->GetFileSpec().GetFilename().GetCString(), 3544 object_name ? "(" : "", 3545 object_name ? object_name : "", 3546 object_name ? "(" : "", 3547 namespace_decl->getOriginalNamespace()); 3548 } 3549 else 3550 { 3551 log->Printf ("ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl * %p in %s/%s%s%s%s (original = %p)", 3552 GetClangASTContext().getASTContext(), 3553 MakeUserID(die->GetOffset()), 3554 namespace_decl, 3555 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 3556 m_obj_file->GetFileSpec().GetFilename().GetCString(), 3557 object_name ? "(" : "", 3558 object_name ? object_name : "", 3559 object_name ? "(" : "", 3560 namespace_decl->getOriginalNamespace()); 3561 } 3562 } 3563 3564 if (namespace_decl) 3565 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die); 3566 return namespace_decl; 3567 } 3568 } 3569 return NULL; 3570 } 3571 3572 clang::DeclContext * 3573 SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die) 3574 { 3575 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die); 3576 if (clang_decl_ctx) 3577 return clang_decl_ctx; 3578 // If this DIE has a specification, or an abstract origin, then trace to those. 3579 3580 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET); 3581 if (die_offset != DW_INVALID_OFFSET) 3582 return GetClangDeclContextForDIEOffset (sc, die_offset); 3583 3584 die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET); 3585 if (die_offset != DW_INVALID_OFFSET) 3586 return GetClangDeclContextForDIEOffset (sc, die_offset); 3587 3588 // This is the DIE we want. Parse it, then query our map. 3589 3590 ParseType(sc, curr_cu, die, NULL); 3591 3592 clang_decl_ctx = GetCachedClangDeclContextForDIE (die); 3593 3594 return clang_decl_ctx; 3595 } 3596 3597 clang::DeclContext * 3598 SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy) 3599 { 3600 if (m_clang_tu_decl == NULL) 3601 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl(); 3602 3603 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die); 3604 3605 if (decl_ctx_die_copy) 3606 *decl_ctx_die_copy = decl_ctx_die; 3607 3608 if (decl_ctx_die) 3609 { 3610 3611 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die); 3612 if (pos != m_die_to_decl_ctx.end()) 3613 return pos->second; 3614 3615 switch (decl_ctx_die->Tag()) 3616 { 3617 case DW_TAG_compile_unit: 3618 return m_clang_tu_decl; 3619 3620 case DW_TAG_namespace: 3621 return ResolveNamespaceDIE (cu, decl_ctx_die); 3622 break; 3623 3624 case DW_TAG_structure_type: 3625 case DW_TAG_union_type: 3626 case DW_TAG_class_type: 3627 { 3628 Type* type = ResolveType (cu, decl_ctx_die); 3629 if (type) 3630 { 3631 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ()); 3632 if (decl_ctx) 3633 { 3634 LinkDeclContextToDIE (decl_ctx, decl_ctx_die); 3635 if (decl_ctx) 3636 return decl_ctx; 3637 } 3638 } 3639 } 3640 break; 3641 3642 default: 3643 break; 3644 } 3645 } 3646 return m_clang_tu_decl; 3647 } 3648 3649 3650 const DWARFDebugInfoEntry * 3651 SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die) 3652 { 3653 if (cu && die) 3654 { 3655 const DWARFDebugInfoEntry * const decl_die = die; 3656 3657 while (die != NULL) 3658 { 3659 // If this is the original DIE that we are searching for a declaration 3660 // for, then don't look in the cache as we don't want our own decl 3661 // context to be our decl context... 3662 if (decl_die != die) 3663 { 3664 switch (die->Tag()) 3665 { 3666 case DW_TAG_compile_unit: 3667 case DW_TAG_namespace: 3668 case DW_TAG_structure_type: 3669 case DW_TAG_union_type: 3670 case DW_TAG_class_type: 3671 return die; 3672 3673 default: 3674 break; 3675 } 3676 } 3677 3678 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET); 3679 if (die_offset != DW_INVALID_OFFSET) 3680 { 3681 DWARFCompileUnit *spec_cu = cu; 3682 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu); 3683 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die); 3684 if (spec_die_decl_ctx_die) 3685 return spec_die_decl_ctx_die; 3686 } 3687 3688 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET); 3689 if (die_offset != DW_INVALID_OFFSET) 3690 { 3691 DWARFCompileUnit *abs_cu = cu; 3692 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu); 3693 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die); 3694 if (abs_die_decl_ctx_die) 3695 return abs_die_decl_ctx_die; 3696 } 3697 3698 die = die->GetParent(); 3699 } 3700 } 3701 return NULL; 3702 } 3703 3704 3705 3706 // This function can be used when a DIE is found that is a forward declaration 3707 // DIE and we want to try and find a type that has the complete definition. 3708 TypeSP 3709 SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu, 3710 const DWARFDebugInfoEntry *die, 3711 const ConstString &type_name) 3712 { 3713 TypeSP type_sp; 3714 3715 if (cu == NULL || die == NULL || !type_name) 3716 return type_sp; 3717 3718 DIEArray die_offsets; 3719 3720 if (m_using_apple_tables) 3721 { 3722 if (m_apple_types_ap.get()) 3723 { 3724 const char *name_cstr = type_name.GetCString(); 3725 m_apple_types_ap->FindByName (name_cstr, die_offsets); 3726 } 3727 } 3728 else 3729 { 3730 if (!m_indexed) 3731 Index (); 3732 3733 m_type_index.Find (type_name, die_offsets); 3734 } 3735 3736 3737 const size_t num_matches = die_offsets.size(); 3738 3739 const dw_tag_t type_tag = die->Tag(); 3740 3741 DWARFCompileUnit* type_cu = NULL; 3742 const DWARFDebugInfoEntry* type_die = NULL; 3743 if (num_matches) 3744 { 3745 DWARFDebugInfo* debug_info = DebugInfo(); 3746 for (size_t i=0; i<num_matches; ++i) 3747 { 3748 const dw_offset_t die_offset = die_offsets[i]; 3749 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu); 3750 3751 if (type_die != die && type_die->Tag() == type_tag) 3752 { 3753 // Hold off on comparing parent DIE tags until 3754 // we know what happens with stuff in namespaces 3755 // for gcc and clang... 3756 //DWARFDebugInfoEntry *parent_die = die->GetParent(); 3757 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent(); 3758 //if (parent_die->Tag() == parent_type_die->Tag()) 3759 { 3760 Type *resolved_type = ResolveType (type_cu, type_die, false); 3761 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 3762 { 3763 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n", 3764 MakeUserID(die->GetOffset()), 3765 MakeUserID(curr_cu->GetOffset()), 3766 m_obj_file->GetFileSpec().GetFilename().AsCString(), 3767 MakeUserID(type_die->GetOffset()), 3768 MakeUserID(type_cu->GetOffset())); 3769 3770 m_die_to_type[die] = resolved_type; 3771 type_sp = resolved_type; 3772 break; 3773 } 3774 } 3775 } 3776 } 3777 } 3778 return type_sp; 3779 } 3780 3781 TypeSP 3782 SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr) 3783 { 3784 TypeSP type_sp; 3785 3786 if (type_is_new_ptr) 3787 *type_is_new_ptr = false; 3788 3789 AccessType accessibility = eAccessNone; 3790 if (die != NULL) 3791 { 3792 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 3793 if (log && dwarf_cu) 3794 { 3795 StreamString s; 3796 die->DumpLocation (this, dwarf_cu, s); 3797 log->Printf ("SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData()); 3798 3799 } 3800 3801 Type *type_ptr = m_die_to_type.lookup (die); 3802 TypeList* type_list = GetTypeList(); 3803 if (type_ptr == NULL) 3804 { 3805 ClangASTContext &ast = GetClangASTContext(); 3806 if (type_is_new_ptr) 3807 *type_is_new_ptr = true; 3808 3809 const dw_tag_t tag = die->Tag(); 3810 3811 bool is_forward_declaration = false; 3812 DWARFDebugInfoEntry::Attributes attributes; 3813 const char *type_name_cstr = NULL; 3814 ConstString type_name_const_str; 3815 Type::ResolveState resolve_state = Type::eResolveStateUnresolved; 3816 size_t byte_size = 0; 3817 bool byte_size_valid = false; 3818 Declaration decl; 3819 3820 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 3821 clang_type_t clang_type = NULL; 3822 3823 dw_attr_t attr; 3824 3825 switch (tag) 3826 { 3827 case DW_TAG_base_type: 3828 case DW_TAG_pointer_type: 3829 case DW_TAG_reference_type: 3830 case DW_TAG_typedef: 3831 case DW_TAG_const_type: 3832 case DW_TAG_restrict_type: 3833 case DW_TAG_volatile_type: 3834 case DW_TAG_unspecified_type: 3835 { 3836 // Set a bit that lets us know that we are currently parsing this 3837 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3838 3839 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3840 uint32_t encoding = 0; 3841 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 3842 3843 if (num_attributes > 0) 3844 { 3845 uint32_t i; 3846 for (i=0; i<num_attributes; ++i) 3847 { 3848 attr = attributes.AttributeAtIndex(i); 3849 DWARFFormValue form_value; 3850 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3851 { 3852 switch (attr) 3853 { 3854 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3855 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3856 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3857 case DW_AT_name: 3858 3859 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3860 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't 3861 // include the "&"... 3862 if (tag == DW_TAG_reference_type) 3863 { 3864 if (strchr (type_name_cstr, '&') == NULL) 3865 type_name_cstr = NULL; 3866 } 3867 if (type_name_cstr) 3868 type_name_const_str.SetCString(type_name_cstr); 3869 break; 3870 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 3871 case DW_AT_encoding: encoding = form_value.Unsigned(); break; 3872 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 3873 default: 3874 case DW_AT_sibling: 3875 break; 3876 } 3877 } 3878 } 3879 } 3880 3881 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); 3882 3883 switch (tag) 3884 { 3885 default: 3886 break; 3887 3888 case DW_TAG_unspecified_type: 3889 if (strcmp(type_name_cstr, "nullptr_t") == 0) 3890 { 3891 resolve_state = Type::eResolveStateFull; 3892 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr(); 3893 break; 3894 } 3895 // Fall through to base type below in case we can handle the type there... 3896 3897 case DW_TAG_base_type: 3898 resolve_state = Type::eResolveStateFull; 3899 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr, 3900 encoding, 3901 byte_size * 8); 3902 break; 3903 3904 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break; 3905 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break; 3906 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break; 3907 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break; 3908 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break; 3909 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break; 3910 } 3911 3912 if (type_name_cstr != NULL && sc.comp_unit != NULL && 3913 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus)) 3914 { 3915 static ConstString g_objc_type_name_id("id"); 3916 static ConstString g_objc_type_name_Class("Class"); 3917 static ConstString g_objc_type_name_selector("SEL"); 3918 3919 if (type_name_const_str == g_objc_type_name_id) 3920 { 3921 clang_type = ast.GetBuiltInType_objc_id(); 3922 resolve_state = Type::eResolveStateFull; 3923 3924 } 3925 else if (type_name_const_str == g_objc_type_name_Class) 3926 { 3927 clang_type = ast.GetBuiltInType_objc_Class(); 3928 resolve_state = Type::eResolveStateFull; 3929 } 3930 else if (type_name_const_str == g_objc_type_name_selector) 3931 { 3932 clang_type = ast.GetBuiltInType_objc_selector(); 3933 resolve_state = Type::eResolveStateFull; 3934 } 3935 } 3936 3937 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 3938 this, 3939 type_name_const_str, 3940 byte_size, 3941 NULL, 3942 encoding_uid, 3943 encoding_data_type, 3944 &decl, 3945 clang_type, 3946 resolve_state)); 3947 3948 m_die_to_type[die] = type_sp.get(); 3949 3950 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false); 3951 // if (encoding_type != NULL) 3952 // { 3953 // if (encoding_type != DIE_IS_BEING_PARSED) 3954 // type_sp->SetEncodingType(encoding_type); 3955 // else 3956 // m_indirect_fixups.push_back(type_sp.get()); 3957 // } 3958 } 3959 break; 3960 3961 case DW_TAG_structure_type: 3962 case DW_TAG_union_type: 3963 case DW_TAG_class_type: 3964 { 3965 // Set a bit that lets us know that we are currently parsing this 3966 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3967 3968 LanguageType class_language = eLanguageTypeUnknown; 3969 //bool struct_is_class = false; 3970 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3971 if (num_attributes > 0) 3972 { 3973 uint32_t i; 3974 for (i=0; i<num_attributes; ++i) 3975 { 3976 attr = attributes.AttributeAtIndex(i); 3977 DWARFFormValue form_value; 3978 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3979 { 3980 switch (attr) 3981 { 3982 case DW_AT_decl_file: 3983 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); 3984 break; 3985 3986 case DW_AT_decl_line: 3987 decl.SetLine(form_value.Unsigned()); 3988 break; 3989 3990 case DW_AT_decl_column: 3991 decl.SetColumn(form_value.Unsigned()); 3992 break; 3993 3994 case DW_AT_name: 3995 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3996 type_name_const_str.SetCString(type_name_cstr); 3997 break; 3998 3999 case DW_AT_byte_size: 4000 byte_size = form_value.Unsigned(); 4001 byte_size_valid = true; 4002 break; 4003 4004 case DW_AT_accessibility: 4005 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 4006 break; 4007 4008 case DW_AT_declaration: 4009 is_forward_declaration = form_value.Unsigned() != 0; 4010 break; 4011 4012 case DW_AT_APPLE_runtime_class: 4013 class_language = (LanguageType)form_value.Signed(); 4014 break; 4015 4016 case DW_AT_allocated: 4017 case DW_AT_associated: 4018 case DW_AT_data_location: 4019 case DW_AT_description: 4020 case DW_AT_start_scope: 4021 case DW_AT_visibility: 4022 default: 4023 case DW_AT_sibling: 4024 break; 4025 } 4026 } 4027 } 4028 } 4029 4030 UniqueDWARFASTType unique_ast_entry; 4031 if (decl.IsValid()) 4032 { 4033 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str, 4034 this, 4035 dwarf_cu, 4036 die, 4037 decl, 4038 byte_size_valid ? byte_size : -1, 4039 unique_ast_entry)) 4040 { 4041 // We have already parsed this type or from another 4042 // compile unit. GCC loves to use the "one definition 4043 // rule" which can result in multiple definitions 4044 // of the same class over and over in each compile 4045 // unit. 4046 type_sp = unique_ast_entry.m_type_sp; 4047 if (type_sp) 4048 { 4049 m_die_to_type[die] = type_sp.get(); 4050 return type_sp; 4051 } 4052 } 4053 } 4054 4055 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 4056 4057 int tag_decl_kind = -1; 4058 AccessType default_accessibility = eAccessNone; 4059 if (tag == DW_TAG_structure_type) 4060 { 4061 tag_decl_kind = clang::TTK_Struct; 4062 default_accessibility = eAccessPublic; 4063 } 4064 else if (tag == DW_TAG_union_type) 4065 { 4066 tag_decl_kind = clang::TTK_Union; 4067 default_accessibility = eAccessPublic; 4068 } 4069 else if (tag == DW_TAG_class_type) 4070 { 4071 tag_decl_kind = clang::TTK_Class; 4072 default_accessibility = eAccessPrivate; 4073 } 4074 4075 4076 if (is_forward_declaration) 4077 { 4078 // We have a forward declaration to a type and we need 4079 // to try and find a full declaration. We look in the 4080 // current type index just in case we have a forward 4081 // declaration followed by an actual declarations in the 4082 // DWARF. If this fails, we need to look elsewhere... 4083 4084 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 4085 4086 if (!type_sp && m_debug_map_symfile) 4087 { 4088 // We weren't able to find a full declaration in 4089 // this DWARF, see if we have a declaration anywhere 4090 // else... 4091 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 4092 } 4093 4094 if (type_sp) 4095 { 4096 // We found a real definition for this type elsewhere 4097 // so lets use it and cache the fact that we found 4098 // a complete type for this die 4099 m_die_to_type[die] = type_sp.get(); 4100 return type_sp; 4101 } 4102 } 4103 assert (tag_decl_kind != -1); 4104 bool clang_type_was_created = false; 4105 clang_type = m_forward_decl_die_to_clang_type.lookup (die); 4106 if (clang_type == NULL) 4107 { 4108 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL); 4109 if (accessibility == eAccessNone && decl_ctx) 4110 { 4111 // Check the decl context that contains this class/struct/union. 4112 // If it is a class we must give it an accessability. 4113 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind(); 4114 if (DeclKindIsCXXClass (containing_decl_kind)) 4115 accessibility = default_accessibility; 4116 } 4117 4118 if (type_name_cstr && strchr (type_name_cstr, '<')) 4119 { 4120 ClangASTContext::TemplateParameterInfos template_param_infos; 4121 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos)) 4122 { 4123 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx, 4124 accessibility, 4125 type_name_cstr, 4126 tag_decl_kind, 4127 template_param_infos); 4128 4129 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx, 4130 class_template_decl, 4131 tag_decl_kind, 4132 template_param_infos); 4133 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl); 4134 clang_type_was_created = true; 4135 } 4136 } 4137 4138 if (!clang_type_was_created) 4139 { 4140 clang_type_was_created = true; 4141 clang_type = ast.CreateRecordType (decl_ctx, 4142 accessibility, 4143 type_name_cstr, 4144 tag_decl_kind, 4145 class_language); 4146 } 4147 } 4148 4149 // Store a forward declaration to this class type in case any 4150 // parameters in any class methods need it for the clang 4151 // types for function prototypes. 4152 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die); 4153 type_sp.reset (new Type (MakeUserID(die->GetOffset()), 4154 this, 4155 type_name_const_str, 4156 byte_size, 4157 NULL, 4158 LLDB_INVALID_UID, 4159 Type::eEncodingIsUID, 4160 &decl, 4161 clang_type, 4162 Type::eResolveStateForward)); 4163 4164 4165 // Add our type to the unique type map so we don't 4166 // end up creating many copies of the same type over 4167 // and over in the ASTContext for our module 4168 unique_ast_entry.m_type_sp = type_sp; 4169 unique_ast_entry.m_symfile = this; 4170 unique_ast_entry.m_cu = dwarf_cu; 4171 unique_ast_entry.m_die = die; 4172 unique_ast_entry.m_declaration = decl; 4173 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str, 4174 unique_ast_entry); 4175 4176 if (die->HasChildren() == false && is_forward_declaration == false) 4177 { 4178 // No children for this struct/union/class, lets finish it 4179 ast.StartTagDeclarationDefinition (clang_type); 4180 ast.CompleteTagDeclarationDefinition (clang_type); 4181 } 4182 else if (clang_type_was_created) 4183 { 4184 // Leave this as a forward declaration until we need 4185 // to know the details of the type. lldb_private::Type 4186 // will automatically call the SymbolFile virtual function 4187 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)" 4188 // When the definition needs to be defined. 4189 m_forward_decl_die_to_clang_type[die] = clang_type; 4190 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die; 4191 ClangASTContext::SetHasExternalStorage (clang_type, true); 4192 } 4193 } 4194 break; 4195 4196 case DW_TAG_enumeration_type: 4197 { 4198 // Set a bit that lets us know that we are currently parsing this 4199 m_die_to_type[die] = DIE_IS_BEING_PARSED; 4200 4201 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET; 4202 4203 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 4204 if (num_attributes > 0) 4205 { 4206 uint32_t i; 4207 4208 for (i=0; i<num_attributes; ++i) 4209 { 4210 attr = attributes.AttributeAtIndex(i); 4211 DWARFFormValue form_value; 4212 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4213 { 4214 switch (attr) 4215 { 4216 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4217 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4218 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4219 case DW_AT_name: 4220 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 4221 type_name_const_str.SetCString(type_name_cstr); 4222 break; 4223 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 4224 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 4225 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 4226 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 4227 case DW_AT_allocated: 4228 case DW_AT_associated: 4229 case DW_AT_bit_stride: 4230 case DW_AT_byte_stride: 4231 case DW_AT_data_location: 4232 case DW_AT_description: 4233 case DW_AT_start_scope: 4234 case DW_AT_visibility: 4235 case DW_AT_specification: 4236 case DW_AT_abstract_origin: 4237 case DW_AT_sibling: 4238 break; 4239 } 4240 } 4241 } 4242 4243 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 4244 4245 clang_type_t enumerator_clang_type = NULL; 4246 clang_type = m_forward_decl_die_to_clang_type.lookup (die); 4247 if (clang_type == NULL) 4248 { 4249 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL, 4250 DW_ATE_signed, 4251 byte_size * 8); 4252 clang_type = ast.CreateEnumerationType (type_name_cstr, 4253 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL), 4254 decl, 4255 enumerator_clang_type); 4256 } 4257 else 4258 { 4259 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type); 4260 assert (enumerator_clang_type != NULL); 4261 } 4262 4263 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die); 4264 4265 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 4266 this, 4267 type_name_const_str, 4268 byte_size, 4269 NULL, 4270 encoding_uid, 4271 Type::eEncodingIsUID, 4272 &decl, 4273 clang_type, 4274 Type::eResolveStateForward)); 4275 4276 ast.StartTagDeclarationDefinition (clang_type); 4277 if (die->HasChildren()) 4278 { 4279 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu)); 4280 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die); 4281 } 4282 ast.CompleteTagDeclarationDefinition (clang_type); 4283 } 4284 } 4285 break; 4286 4287 case DW_TAG_inlined_subroutine: 4288 case DW_TAG_subprogram: 4289 case DW_TAG_subroutine_type: 4290 { 4291 // Set a bit that lets us know that we are currently parsing this 4292 m_die_to_type[die] = DIE_IS_BEING_PARSED; 4293 4294 const char *mangled = NULL; 4295 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 4296 bool is_variadic = false; 4297 bool is_inline = false; 4298 bool is_static = false; 4299 bool is_virtual = false; 4300 bool is_explicit = false; 4301 bool is_artificial = false; 4302 dw_offset_t specification_die_offset = DW_INVALID_OFFSET; 4303 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET; 4304 4305 unsigned type_quals = 0; 4306 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern 4307 4308 4309 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 4310 if (num_attributes > 0) 4311 { 4312 uint32_t i; 4313 for (i=0; i<num_attributes; ++i) 4314 { 4315 attr = attributes.AttributeAtIndex(i); 4316 DWARFFormValue form_value; 4317 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4318 { 4319 switch (attr) 4320 { 4321 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4322 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4323 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4324 case DW_AT_name: 4325 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 4326 type_name_const_str.SetCString(type_name_cstr); 4327 break; 4328 4329 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break; 4330 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break; 4331 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 4332 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 4333 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break; 4334 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break; 4335 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break; 4336 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 4337 4338 4339 case DW_AT_external: 4340 if (form_value.Unsigned()) 4341 { 4342 if (storage == clang::SC_None) 4343 storage = clang::SC_Extern; 4344 else 4345 storage = clang::SC_PrivateExtern; 4346 } 4347 break; 4348 4349 case DW_AT_specification: 4350 specification_die_offset = form_value.Reference(dwarf_cu); 4351 break; 4352 4353 case DW_AT_abstract_origin: 4354 abstract_origin_die_offset = form_value.Reference(dwarf_cu); 4355 break; 4356 4357 case DW_AT_allocated: 4358 case DW_AT_associated: 4359 case DW_AT_address_class: 4360 case DW_AT_calling_convention: 4361 case DW_AT_data_location: 4362 case DW_AT_elemental: 4363 case DW_AT_entry_pc: 4364 case DW_AT_frame_base: 4365 case DW_AT_high_pc: 4366 case DW_AT_low_pc: 4367 case DW_AT_object_pointer: 4368 case DW_AT_prototyped: 4369 case DW_AT_pure: 4370 case DW_AT_ranges: 4371 case DW_AT_recursive: 4372 case DW_AT_return_addr: 4373 case DW_AT_segment: 4374 case DW_AT_start_scope: 4375 case DW_AT_static_link: 4376 case DW_AT_trampoline: 4377 case DW_AT_visibility: 4378 case DW_AT_vtable_elem_location: 4379 case DW_AT_description: 4380 case DW_AT_sibling: 4381 break; 4382 } 4383 } 4384 } 4385 } 4386 4387 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 4388 4389 clang_type_t return_clang_type = NULL; 4390 Type *func_type = NULL; 4391 4392 if (type_die_offset != DW_INVALID_OFFSET) 4393 func_type = ResolveTypeUID(type_die_offset); 4394 4395 if (func_type) 4396 return_clang_type = func_type->GetClangLayoutType(); 4397 else 4398 return_clang_type = ast.GetBuiltInType_void(); 4399 4400 4401 std::vector<clang_type_t> function_param_types; 4402 std::vector<clang::ParmVarDecl*> function_param_decls; 4403 4404 // Parse the function children for the parameters 4405 4406 const DWARFDebugInfoEntry *decl_ctx_die = NULL; 4407 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die); 4408 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind(); 4409 4410 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind); 4411 // Start off static. This will be set to false in ParseChildParameters(...) 4412 // if we find a "this" paramters as the first parameter 4413 if (is_cxx_method) 4414 is_static = true; 4415 4416 if (die->HasChildren()) 4417 { 4418 bool skip_artificial = true; 4419 ParseChildParameters (sc, 4420 containing_decl_ctx, 4421 type_sp, 4422 dwarf_cu, 4423 die, 4424 skip_artificial, 4425 is_static, 4426 type_list, 4427 function_param_types, 4428 function_param_decls, 4429 type_quals); 4430 } 4431 4432 // clang_type will get the function prototype clang type after this call 4433 clang_type = ast.CreateFunctionType (return_clang_type, 4434 &function_param_types[0], 4435 function_param_types.size(), 4436 is_variadic, 4437 type_quals); 4438 4439 if (type_name_cstr) 4440 { 4441 bool type_handled = false; 4442 if (tag == DW_TAG_subprogram) 4443 { 4444 if (ObjCLanguageRuntime::IsPossibleObjCMethodName (type_name_cstr)) 4445 { 4446 // We need to find the DW_TAG_class_type or 4447 // DW_TAG_struct_type by name so we can add this 4448 // as a member function of the class. 4449 const char *class_name_start = type_name_cstr + 2; 4450 const char *class_name_end = ::strchr (class_name_start, ' '); 4451 SymbolContext empty_sc; 4452 clang_type_t class_opaque_type = NULL; 4453 if (class_name_start < class_name_end) 4454 { 4455 ConstString class_name (class_name_start, class_name_end - class_name_start); 4456 TypeList types; 4457 const uint32_t match_count = FindTypes (empty_sc, class_name, NULL, true, UINT32_MAX, types); 4458 if (match_count > 0) 4459 { 4460 for (uint32_t i=0; i<match_count; ++i) 4461 { 4462 Type *type = types.GetTypeAtIndex (i).get(); 4463 clang_type_t type_clang_forward_type = type->GetClangForwardType(); 4464 if (ClangASTContext::IsObjCClassType (type_clang_forward_type)) 4465 { 4466 class_opaque_type = type_clang_forward_type; 4467 break; 4468 } 4469 } 4470 } 4471 } 4472 4473 if (class_opaque_type) 4474 { 4475 // If accessibility isn't set to anything valid, assume public for 4476 // now... 4477 if (accessibility == eAccessNone) 4478 accessibility = eAccessPublic; 4479 4480 clang::ObjCMethodDecl *objc_method_decl; 4481 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type, 4482 type_name_cstr, 4483 clang_type, 4484 accessibility); 4485 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die); 4486 type_handled = objc_method_decl != NULL; 4487 } 4488 } 4489 else if (is_cxx_method) 4490 { 4491 // Look at the parent of this DIE and see if is is 4492 // a class or struct and see if this is actually a 4493 // C++ method 4494 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die); 4495 if (class_type) 4496 { 4497 if (specification_die_offset != DW_INVALID_OFFSET) 4498 { 4499 // We have a specification which we are going to base our function 4500 // prototype off of, so we need this type to be completed so that the 4501 // m_die_to_decl_ctx for the method in the specification has a valid 4502 // clang decl context. 4503 class_type->GetClangFullType(); 4504 // If we have a specification, then the function type should have been 4505 // made with the specification and not with this die. 4506 DWARFCompileUnitSP spec_cu_sp; 4507 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp); 4508 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die); 4509 if (spec_clang_decl_ctx) 4510 { 4511 LinkDeclContextToDIE(spec_clang_decl_ctx, die); 4512 } 4513 else 4514 { 4515 ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n", 4516 MakeUserID(die->GetOffset()), 4517 specification_die_offset); 4518 } 4519 type_handled = true; 4520 } 4521 else if (abstract_origin_die_offset != DW_INVALID_OFFSET) 4522 { 4523 // We have a specification which we are going to base our function 4524 // prototype off of, so we need this type to be completed so that the 4525 // m_die_to_decl_ctx for the method in the abstract origin has a valid 4526 // clang decl context. 4527 class_type->GetClangFullType(); 4528 4529 DWARFCompileUnitSP abs_cu_sp; 4530 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp); 4531 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die); 4532 if (abs_clang_decl_ctx) 4533 { 4534 LinkDeclContextToDIE (abs_clang_decl_ctx, die); 4535 } 4536 else 4537 { 4538 ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n", 4539 MakeUserID(die->GetOffset()), 4540 abstract_origin_die_offset); 4541 } 4542 type_handled = true; 4543 } 4544 else 4545 { 4546 clang_type_t class_opaque_type = class_type->GetClangForwardType(); 4547 if (ClangASTContext::IsCXXClassType (class_opaque_type)) 4548 { 4549 if (ClangASTContext::IsBeingDefined (class_opaque_type)) 4550 { 4551 // Neither GCC 4.2 nor clang++ currently set a valid accessibility 4552 // in the DWARF for C++ methods... Default to public for now... 4553 if (accessibility == eAccessNone) 4554 accessibility = eAccessPublic; 4555 4556 if (!is_static && !die->HasChildren()) 4557 { 4558 // We have a C++ member function with no children (this pointer!) 4559 // and clang will get mad if we try and make a function that isn't 4560 // well formed in the DWARF, so we will just skip it... 4561 type_handled = true; 4562 } 4563 else 4564 { 4565 clang::CXXMethodDecl *cxx_method_decl; 4566 // REMOVE THE CRASH DESCRIPTION BELOW 4567 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8llx from %s/%s", 4568 type_name_cstr, 4569 class_type->GetName().GetCString(), 4570 MakeUserID(die->GetOffset()), 4571 m_obj_file->GetFileSpec().GetDirectory().GetCString(), 4572 m_obj_file->GetFileSpec().GetFilename().GetCString()); 4573 4574 const bool is_attr_used = false; 4575 4576 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type, 4577 type_name_cstr, 4578 clang_type, 4579 accessibility, 4580 is_virtual, 4581 is_static, 4582 is_inline, 4583 is_explicit, 4584 is_attr_used, 4585 is_artificial); 4586 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die); 4587 4588 type_handled = cxx_method_decl != NULL; 4589 } 4590 } 4591 else 4592 { 4593 // We were asked to parse the type for a method in a class, yet the 4594 // class hasn't been asked to complete itself through the 4595 // clang::ExternalASTSource protocol, so we need to just have the 4596 // class complete itself and do things the right way, then our 4597 // DIE should then have an entry in the m_die_to_type map. First 4598 // we need to modify the m_die_to_type so it doesn't think we are 4599 // trying to parse this DIE anymore... 4600 m_die_to_type[die] = NULL; 4601 4602 // Now we get the full type to force our class type to complete itself 4603 // using the clang::ExternalASTSource protocol which will parse all 4604 // base classes and all methods (including the method for this DIE). 4605 class_type->GetClangFullType(); 4606 4607 // The type for this DIE should have been filled in the function call above 4608 type_ptr = m_die_to_type[die]; 4609 if (type_ptr) 4610 { 4611 type_sp = type_ptr; 4612 break; 4613 } 4614 } 4615 } 4616 } 4617 } 4618 } 4619 } 4620 4621 if (!type_handled) 4622 { 4623 // We just have a function that isn't part of a class 4624 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx, 4625 type_name_cstr, 4626 clang_type, 4627 storage, 4628 is_inline); 4629 4630 // Add the decl to our DIE to decl context map 4631 assert (function_decl); 4632 LinkDeclContextToDIE(function_decl, die); 4633 if (!function_param_decls.empty()) 4634 ast.SetFunctionParameters (function_decl, 4635 &function_param_decls.front(), 4636 function_param_decls.size()); 4637 } 4638 } 4639 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 4640 this, 4641 type_name_const_str, 4642 0, 4643 NULL, 4644 LLDB_INVALID_UID, 4645 Type::eEncodingIsUID, 4646 &decl, 4647 clang_type, 4648 Type::eResolveStateFull)); 4649 assert(type_sp.get()); 4650 } 4651 break; 4652 4653 case DW_TAG_array_type: 4654 { 4655 // Set a bit that lets us know that we are currently parsing this 4656 m_die_to_type[die] = DIE_IS_BEING_PARSED; 4657 4658 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET; 4659 int64_t first_index = 0; 4660 uint32_t byte_stride = 0; 4661 uint32_t bit_stride = 0; 4662 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 4663 4664 if (num_attributes > 0) 4665 { 4666 uint32_t i; 4667 for (i=0; i<num_attributes; ++i) 4668 { 4669 attr = attributes.AttributeAtIndex(i); 4670 DWARFFormValue form_value; 4671 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4672 { 4673 switch (attr) 4674 { 4675 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4676 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4677 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4678 case DW_AT_name: 4679 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 4680 type_name_const_str.SetCString(type_name_cstr); 4681 break; 4682 4683 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break; 4684 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 4685 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break; 4686 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break; 4687 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 4688 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 4689 case DW_AT_allocated: 4690 case DW_AT_associated: 4691 case DW_AT_data_location: 4692 case DW_AT_description: 4693 case DW_AT_ordering: 4694 case DW_AT_start_scope: 4695 case DW_AT_visibility: 4696 case DW_AT_specification: 4697 case DW_AT_abstract_origin: 4698 case DW_AT_sibling: 4699 break; 4700 } 4701 } 4702 } 4703 4704 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr); 4705 4706 Type *element_type = ResolveTypeUID(type_die_offset); 4707 4708 if (element_type) 4709 { 4710 std::vector<uint64_t> element_orders; 4711 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride); 4712 // We have an array that claims to have no members, lets give it at least one member... 4713 if (element_orders.empty()) 4714 element_orders.push_back (1); 4715 if (byte_stride == 0 && bit_stride == 0) 4716 byte_stride = element_type->GetByteSize(); 4717 clang_type_t array_element_type = element_type->GetClangFullType(); 4718 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride; 4719 uint64_t num_elements = 0; 4720 std::vector<uint64_t>::const_reverse_iterator pos; 4721 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend(); 4722 for (pos = element_orders.rbegin(); pos != end; ++pos) 4723 { 4724 num_elements = *pos; 4725 clang_type = ast.CreateArrayType (array_element_type, 4726 num_elements, 4727 num_elements * array_element_bit_stride); 4728 array_element_type = clang_type; 4729 array_element_bit_stride = array_element_bit_stride * num_elements; 4730 } 4731 ConstString empty_name; 4732 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 4733 this, 4734 empty_name, 4735 array_element_bit_stride / 8, 4736 NULL, 4737 type_die_offset, 4738 Type::eEncodingIsUID, 4739 &decl, 4740 clang_type, 4741 Type::eResolveStateFull)); 4742 type_sp->SetEncodingType (element_type); 4743 } 4744 } 4745 } 4746 break; 4747 4748 case DW_TAG_ptr_to_member_type: 4749 { 4750 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 4751 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET; 4752 4753 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 4754 4755 if (num_attributes > 0) { 4756 uint32_t i; 4757 for (i=0; i<num_attributes; ++i) 4758 { 4759 attr = attributes.AttributeAtIndex(i); 4760 DWARFFormValue form_value; 4761 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4762 { 4763 switch (attr) 4764 { 4765 case DW_AT_type: 4766 type_die_offset = form_value.Reference(dwarf_cu); break; 4767 case DW_AT_containing_type: 4768 containing_type_die_offset = form_value.Reference(dwarf_cu); break; 4769 } 4770 } 4771 } 4772 4773 Type *pointee_type = ResolveTypeUID(type_die_offset); 4774 Type *class_type = ResolveTypeUID(containing_type_die_offset); 4775 4776 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType(); 4777 clang_type_t class_clang_type = class_type->GetClangLayoutType(); 4778 4779 clang_type = ast.CreateMemberPointerType(pointee_clang_type, 4780 class_clang_type); 4781 4782 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(), 4783 clang_type) / 8; 4784 4785 type_sp.reset( new Type (MakeUserID(die->GetOffset()), 4786 this, 4787 type_name_const_str, 4788 byte_size, 4789 NULL, 4790 LLDB_INVALID_UID, 4791 Type::eEncodingIsUID, 4792 NULL, 4793 clang_type, 4794 Type::eResolveStateForward)); 4795 } 4796 4797 break; 4798 } 4799 default: 4800 assert(false && "Unhandled type tag!"); 4801 break; 4802 } 4803 4804 if (type_sp.get()) 4805 { 4806 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 4807 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 4808 4809 SymbolContextScope * symbol_context_scope = NULL; 4810 if (sc_parent_tag == DW_TAG_compile_unit) 4811 { 4812 symbol_context_scope = sc.comp_unit; 4813 } 4814 else if (sc.function != NULL) 4815 { 4816 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 4817 if (symbol_context_scope == NULL) 4818 symbol_context_scope = sc.function; 4819 } 4820 4821 if (symbol_context_scope != NULL) 4822 { 4823 type_sp->SetSymbolContextScope(symbol_context_scope); 4824 } 4825 4826 // We are ready to put this type into the uniqued list up at the module level 4827 type_list->Insert (type_sp); 4828 4829 m_die_to_type[die] = type_sp.get(); 4830 } 4831 } 4832 else if (type_ptr != DIE_IS_BEING_PARSED) 4833 { 4834 type_sp = type_ptr; 4835 } 4836 } 4837 return type_sp; 4838 } 4839 4840 size_t 4841 SymbolFileDWARF::ParseTypes 4842 ( 4843 const SymbolContext& sc, 4844 DWARFCompileUnit* dwarf_cu, 4845 const DWARFDebugInfoEntry *die, 4846 bool parse_siblings, 4847 bool parse_children 4848 ) 4849 { 4850 size_t types_added = 0; 4851 while (die != NULL) 4852 { 4853 bool type_is_new = false; 4854 if (ParseType(sc, dwarf_cu, die, &type_is_new).get()) 4855 { 4856 if (type_is_new) 4857 ++types_added; 4858 } 4859 4860 if (parse_children && die->HasChildren()) 4861 { 4862 if (die->Tag() == DW_TAG_subprogram) 4863 { 4864 SymbolContext child_sc(sc); 4865 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get(); 4866 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true); 4867 } 4868 else 4869 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true); 4870 } 4871 4872 if (parse_siblings) 4873 die = die->GetSibling(); 4874 else 4875 die = NULL; 4876 } 4877 return types_added; 4878 } 4879 4880 4881 size_t 4882 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc) 4883 { 4884 assert(sc.comp_unit && sc.function); 4885 size_t functions_added = 0; 4886 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 4887 if (dwarf_cu) 4888 { 4889 dw_offset_t function_die_offset = sc.function->GetID(); 4890 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset); 4891 if (function_die) 4892 { 4893 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0); 4894 } 4895 } 4896 4897 return functions_added; 4898 } 4899 4900 4901 size_t 4902 SymbolFileDWARF::ParseTypes (const SymbolContext &sc) 4903 { 4904 // At least a compile unit must be valid 4905 assert(sc.comp_unit); 4906 size_t types_added = 0; 4907 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 4908 if (dwarf_cu) 4909 { 4910 if (sc.function) 4911 { 4912 dw_offset_t function_die_offset = sc.function->GetID(); 4913 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset); 4914 if (func_die && func_die->HasChildren()) 4915 { 4916 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true); 4917 } 4918 } 4919 else 4920 { 4921 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE(); 4922 if (dwarf_cu_die && dwarf_cu_die->HasChildren()) 4923 { 4924 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true); 4925 } 4926 } 4927 } 4928 4929 return types_added; 4930 } 4931 4932 size_t 4933 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc) 4934 { 4935 if (sc.comp_unit != NULL) 4936 { 4937 DWARFDebugInfo* info = DebugInfo(); 4938 if (info == NULL) 4939 return 0; 4940 4941 uint32_t cu_idx = UINT32_MAX; 4942 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get(); 4943 4944 if (dwarf_cu == NULL) 4945 return 0; 4946 4947 if (sc.function) 4948 { 4949 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID()); 4950 4951 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS); 4952 assert (func_lo_pc != DW_INVALID_ADDRESS); 4953 4954 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true); 4955 4956 // Let all blocks know they have parse all their variables 4957 sc.function->GetBlock (false).SetDidParseVariables (true, true); 4958 4959 return num_variables; 4960 } 4961 else if (sc.comp_unit) 4962 { 4963 uint32_t vars_added = 0; 4964 VariableListSP variables (sc.comp_unit->GetVariableList(false)); 4965 4966 if (variables.get() == NULL) 4967 { 4968 variables.reset(new VariableList()); 4969 sc.comp_unit->SetVariableList(variables); 4970 4971 DWARFCompileUnit* match_dwarf_cu = NULL; 4972 const DWARFDebugInfoEntry* die = NULL; 4973 DIEArray die_offsets; 4974 if (m_using_apple_tables) 4975 { 4976 if (m_apple_names_ap.get()) 4977 m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(), 4978 dwarf_cu->GetNextCompileUnitOffset(), 4979 die_offsets); 4980 } 4981 else 4982 { 4983 // Index if we already haven't to make sure the compile units 4984 // get indexed and make their global DIE index list 4985 if (!m_indexed) 4986 Index (); 4987 4988 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(), 4989 dwarf_cu->GetNextCompileUnitOffset(), 4990 die_offsets); 4991 } 4992 4993 const size_t num_matches = die_offsets.size(); 4994 if (num_matches) 4995 { 4996 DWARFDebugInfo* debug_info = DebugInfo(); 4997 for (size_t i=0; i<num_matches; ++i) 4998 { 4999 const dw_offset_t die_offset = die_offsets[i]; 5000 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu); 5001 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS)); 5002 if (var_sp) 5003 { 5004 variables->AddVariableIfUnique (var_sp); 5005 ++vars_added; 5006 } 5007 } 5008 } 5009 } 5010 return vars_added; 5011 } 5012 } 5013 return 0; 5014 } 5015 5016 5017 VariableSP 5018 SymbolFileDWARF::ParseVariableDIE 5019 ( 5020 const SymbolContext& sc, 5021 DWARFCompileUnit* dwarf_cu, 5022 const DWARFDebugInfoEntry *die, 5023 const lldb::addr_t func_low_pc 5024 ) 5025 { 5026 5027 VariableSP var_sp (m_die_to_variable_sp[die]); 5028 if (var_sp) 5029 return var_sp; // Already been parsed! 5030 5031 const dw_tag_t tag = die->Tag(); 5032 5033 if ((tag == DW_TAG_variable) || 5034 (tag == DW_TAG_constant) || 5035 (tag == DW_TAG_formal_parameter && sc.function)) 5036 { 5037 DWARFDebugInfoEntry::Attributes attributes; 5038 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 5039 if (num_attributes > 0) 5040 { 5041 const char *name = NULL; 5042 const char *mangled = NULL; 5043 Declaration decl; 5044 uint32_t i; 5045 Type *var_type = NULL; 5046 DWARFExpression location; 5047 bool is_external = false; 5048 bool is_artificial = false; 5049 bool location_is_const_value_data = false; 5050 AccessType accessibility = eAccessNone; 5051 5052 for (i=0; i<num_attributes; ++i) 5053 { 5054 dw_attr_t attr = attributes.AttributeAtIndex(i); 5055 DWARFFormValue form_value; 5056 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 5057 { 5058 switch (attr) 5059 { 5060 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 5061 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 5062 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 5063 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 5064 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break; 5065 case DW_AT_type: var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break; 5066 case DW_AT_external: is_external = form_value.Unsigned() != 0; break; 5067 case DW_AT_const_value: 5068 location_is_const_value_data = true; 5069 // Fall through... 5070 case DW_AT_location: 5071 { 5072 if (form_value.BlockData()) 5073 { 5074 const DataExtractor& debug_info_data = get_debug_info_data(); 5075 5076 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 5077 uint32_t block_length = form_value.Unsigned(); 5078 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length); 5079 } 5080 else 5081 { 5082 const DataExtractor& debug_loc_data = get_debug_loc_data(); 5083 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 5084 5085 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset); 5086 if (loc_list_length > 0) 5087 { 5088 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length); 5089 assert (func_low_pc != LLDB_INVALID_ADDRESS); 5090 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress()); 5091 } 5092 } 5093 } 5094 break; 5095 5096 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 5097 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 5098 case DW_AT_declaration: 5099 case DW_AT_description: 5100 case DW_AT_endianity: 5101 case DW_AT_segment: 5102 case DW_AT_start_scope: 5103 case DW_AT_visibility: 5104 default: 5105 case DW_AT_abstract_origin: 5106 case DW_AT_sibling: 5107 case DW_AT_specification: 5108 break; 5109 } 5110 } 5111 } 5112 5113 if (location.IsValid()) 5114 { 5115 assert(var_type != DIE_IS_BEING_PARSED); 5116 5117 ValueType scope = eValueTypeInvalid; 5118 5119 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 5120 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 5121 5122 if (tag == DW_TAG_formal_parameter) 5123 scope = eValueTypeVariableArgument; 5124 else if (is_external || parent_tag == DW_TAG_compile_unit) 5125 scope = eValueTypeVariableGlobal; 5126 else 5127 scope = eValueTypeVariableLocal; 5128 5129 SymbolContextScope * symbol_context_scope = NULL; 5130 switch (parent_tag) 5131 { 5132 case DW_TAG_subprogram: 5133 case DW_TAG_inlined_subroutine: 5134 case DW_TAG_lexical_block: 5135 if (sc.function) 5136 { 5137 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 5138 if (symbol_context_scope == NULL) 5139 symbol_context_scope = sc.function; 5140 } 5141 break; 5142 5143 default: 5144 symbol_context_scope = sc.comp_unit; 5145 break; 5146 } 5147 5148 if (symbol_context_scope) 5149 { 5150 var_sp.reset (new Variable (MakeUserID(die->GetOffset()), 5151 name, 5152 mangled, 5153 var_type, 5154 scope, 5155 symbol_context_scope, 5156 &decl, 5157 location, 5158 is_external, 5159 is_artificial)); 5160 5161 var_sp->SetLocationIsConstantValueData (location_is_const_value_data); 5162 } 5163 else 5164 { 5165 // Not ready to parse this variable yet. It might be a global 5166 // or static variable that is in a function scope and the function 5167 // in the symbol context wasn't filled in yet 5168 return var_sp; 5169 } 5170 } 5171 } 5172 // Cache var_sp even if NULL (the variable was just a specification or 5173 // was missing vital information to be able to be displayed in the debugger 5174 // (missing location due to optimization, etc)) so we don't re-parse 5175 // this DIE over and over later... 5176 m_die_to_variable_sp[die] = var_sp; 5177 } 5178 return var_sp; 5179 } 5180 5181 5182 const DWARFDebugInfoEntry * 5183 SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset, 5184 dw_offset_t spec_block_die_offset, 5185 DWARFCompileUnit **result_die_cu_handle) 5186 { 5187 // Give the concrete function die specified by "func_die_offset", find the 5188 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 5189 // to "spec_block_die_offset" 5190 DWARFDebugInfo* info = DebugInfo(); 5191 5192 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle); 5193 if (die) 5194 { 5195 assert (*result_die_cu_handle); 5196 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle); 5197 } 5198 return NULL; 5199 } 5200 5201 5202 const DWARFDebugInfoEntry * 5203 SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu, 5204 const DWARFDebugInfoEntry *die, 5205 dw_offset_t spec_block_die_offset, 5206 DWARFCompileUnit **result_die_cu_handle) 5207 { 5208 if (die) 5209 { 5210 switch (die->Tag()) 5211 { 5212 case DW_TAG_subprogram: 5213 case DW_TAG_inlined_subroutine: 5214 case DW_TAG_lexical_block: 5215 { 5216 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset) 5217 { 5218 *result_die_cu_handle = dwarf_cu; 5219 return die; 5220 } 5221 5222 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset) 5223 { 5224 *result_die_cu_handle = dwarf_cu; 5225 return die; 5226 } 5227 } 5228 break; 5229 } 5230 5231 // Give the concrete function die specified by "func_die_offset", find the 5232 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 5233 // to "spec_block_die_offset" 5234 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling()) 5235 { 5236 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu, 5237 child_die, 5238 spec_block_die_offset, 5239 result_die_cu_handle); 5240 if (result_die) 5241 return result_die; 5242 } 5243 } 5244 5245 *result_die_cu_handle = NULL; 5246 return NULL; 5247 } 5248 5249 size_t 5250 SymbolFileDWARF::ParseVariables 5251 ( 5252 const SymbolContext& sc, 5253 DWARFCompileUnit* dwarf_cu, 5254 const lldb::addr_t func_low_pc, 5255 const DWARFDebugInfoEntry *orig_die, 5256 bool parse_siblings, 5257 bool parse_children, 5258 VariableList* cc_variable_list 5259 ) 5260 { 5261 if (orig_die == NULL) 5262 return 0; 5263 5264 VariableListSP variable_list_sp; 5265 5266 size_t vars_added = 0; 5267 const DWARFDebugInfoEntry *die = orig_die; 5268 while (die != NULL) 5269 { 5270 dw_tag_t tag = die->Tag(); 5271 5272 // Check to see if we have already parsed this variable or constant? 5273 if (m_die_to_variable_sp[die]) 5274 { 5275 if (cc_variable_list) 5276 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]); 5277 } 5278 else 5279 { 5280 // We haven't already parsed it, lets do that now. 5281 if ((tag == DW_TAG_variable) || 5282 (tag == DW_TAG_constant) || 5283 (tag == DW_TAG_formal_parameter && sc.function)) 5284 { 5285 if (variable_list_sp.get() == NULL) 5286 { 5287 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die); 5288 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 5289 switch (parent_tag) 5290 { 5291 case DW_TAG_compile_unit: 5292 if (sc.comp_unit != NULL) 5293 { 5294 variable_list_sp = sc.comp_unit->GetVariableList(false); 5295 if (variable_list_sp.get() == NULL) 5296 { 5297 variable_list_sp.reset(new VariableList()); 5298 sc.comp_unit->SetVariableList(variable_list_sp); 5299 } 5300 } 5301 else 5302 { 5303 ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n", 5304 MakeUserID(sc_parent_die->GetOffset()), 5305 DW_TAG_value_to_name (parent_tag), 5306 MakeUserID(orig_die->GetOffset()), 5307 DW_TAG_value_to_name (orig_die->Tag())); 5308 } 5309 break; 5310 5311 case DW_TAG_subprogram: 5312 case DW_TAG_inlined_subroutine: 5313 case DW_TAG_lexical_block: 5314 if (sc.function != NULL) 5315 { 5316 // Check to see if we already have parsed the variables for the given scope 5317 5318 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset())); 5319 if (block == NULL) 5320 { 5321 // This must be a specification or abstract origin with 5322 // a concrete block couterpart in the current function. We need 5323 // to find the concrete block so we can correctly add the 5324 // variable to it 5325 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu; 5326 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(), 5327 sc_parent_die->GetOffset(), 5328 &concrete_block_die_cu); 5329 if (concrete_block_die) 5330 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset())); 5331 } 5332 5333 if (block != NULL) 5334 { 5335 const bool can_create = false; 5336 variable_list_sp = block->GetBlockVariableList (can_create); 5337 if (variable_list_sp.get() == NULL) 5338 { 5339 variable_list_sp.reset(new VariableList()); 5340 block->SetVariableList(variable_list_sp); 5341 } 5342 } 5343 } 5344 break; 5345 5346 default: 5347 ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n", 5348 MakeUserID(orig_die->GetOffset()), 5349 DW_TAG_value_to_name (orig_die->Tag())); 5350 break; 5351 } 5352 } 5353 5354 if (variable_list_sp) 5355 { 5356 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc)); 5357 if (var_sp) 5358 { 5359 variable_list_sp->AddVariableIfUnique (var_sp); 5360 if (cc_variable_list) 5361 cc_variable_list->AddVariableIfUnique (var_sp); 5362 ++vars_added; 5363 } 5364 } 5365 } 5366 } 5367 5368 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram); 5369 5370 if (!skip_children && parse_children && die->HasChildren()) 5371 { 5372 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list); 5373 } 5374 5375 if (parse_siblings) 5376 die = die->GetSibling(); 5377 else 5378 die = NULL; 5379 } 5380 return vars_added; 5381 } 5382 5383 //------------------------------------------------------------------ 5384 // PluginInterface protocol 5385 //------------------------------------------------------------------ 5386 const char * 5387 SymbolFileDWARF::GetPluginName() 5388 { 5389 return "SymbolFileDWARF"; 5390 } 5391 5392 const char * 5393 SymbolFileDWARF::GetShortPluginName() 5394 { 5395 return GetPluginNameStatic(); 5396 } 5397 5398 uint32_t 5399 SymbolFileDWARF::GetPluginVersion() 5400 { 5401 return 1; 5402 } 5403 5404 void 5405 SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl) 5406 { 5407 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 5408 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 5409 if (clang_type) 5410 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 5411 } 5412 5413 void 5414 SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl) 5415 { 5416 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 5417 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 5418 if (clang_type) 5419 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 5420 } 5421 5422 void 5423 SymbolFileDWARF::DumpIndexes () 5424 { 5425 StreamFile s(stdout, false); 5426 5427 s.Printf ("DWARF index for (%s) '%s/%s':", 5428 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(), 5429 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(), 5430 GetObjectFile()->GetFileSpec().GetFilename().AsCString()); 5431 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 5432 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 5433 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 5434 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 5435 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 5436 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 5437 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 5438 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s); 5439 } 5440 5441 void 5442 SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context, 5443 const char *name, 5444 llvm::SmallVectorImpl <clang::NamedDecl *> *results) 5445 { 5446 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context); 5447 5448 if (iter == m_decl_ctx_to_die.end()) 5449 return; 5450 5451 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos) 5452 { 5453 const DWARFDebugInfoEntry *context_die = *pos; 5454 5455 if (!results) 5456 return; 5457 5458 DWARFDebugInfo* info = DebugInfo(); 5459 5460 DIEArray die_offsets; 5461 5462 DWARFCompileUnit* dwarf_cu = NULL; 5463 const DWARFDebugInfoEntry* die = NULL; 5464 size_t num_matches = m_type_index.Find (ConstString(name), die_offsets); 5465 5466 if (num_matches) 5467 { 5468 for (size_t i = 0; i < num_matches; ++i) 5469 { 5470 const dw_offset_t die_offset = die_offsets[i]; 5471 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu); 5472 5473 if (die->GetParent() != context_die) 5474 continue; 5475 5476 Type *matching_type = ResolveType (dwarf_cu, die); 5477 5478 lldb::clang_type_t type = matching_type->GetClangFullType(); 5479 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type); 5480 5481 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr())) 5482 { 5483 clang::TagDecl *tag_decl = tag_type->getDecl(); 5484 results->push_back(tag_decl); 5485 } 5486 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr())) 5487 { 5488 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); 5489 results->push_back(typedef_decl); 5490 } 5491 } 5492 } 5493 } 5494 } 5495 5496 void 5497 SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton, 5498 const clang::DeclContext *decl_context, 5499 clang::DeclarationName decl_name, 5500 llvm::SmallVectorImpl <clang::NamedDecl *> *results) 5501 { 5502 5503 switch (decl_context->getDeclKind()) 5504 { 5505 case clang::Decl::Namespace: 5506 case clang::Decl::TranslationUnit: 5507 { 5508 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 5509 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results); 5510 } 5511 break; 5512 default: 5513 break; 5514 } 5515 } 5516