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 "lldb/Core/Module.h" 26 #include "lldb/Core/PluginManager.h" 27 #include "lldb/Core/RegularExpression.h" 28 #include "lldb/Core/Scalar.h" 29 #include "lldb/Core/Section.h" 30 #include "lldb/Core/StreamFile.h" 31 #include "lldb/Core/Timer.h" 32 #include "lldb/Core/Value.h" 33 34 #include "lldb/Symbol/Block.h" 35 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" 36 #include "lldb/Symbol/CompileUnit.h" 37 #include "lldb/Symbol/LineTable.h" 38 #include "lldb/Symbol/ObjectFile.h" 39 #include "lldb/Symbol/SymbolVendor.h" 40 #include "lldb/Symbol/VariableList.h" 41 42 #include "DWARFCompileUnit.h" 43 #include "DWARFDebugAbbrev.h" 44 #include "DWARFDebugAranges.h" 45 #include "DWARFDebugInfo.h" 46 #include "DWARFDebugInfoEntry.h" 47 #include "DWARFDebugLine.h" 48 #include "DWARFDebugPubnames.h" 49 #include "DWARFDebugRanges.h" 50 #include "DWARFDIECollection.h" 51 #include "DWARFFormValue.h" 52 #include "DWARFLocationList.h" 53 #include "LogChannelDWARF.h" 54 #include "SymbolFileDWARFDebugMap.h" 55 56 #include <map> 57 58 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 59 60 #ifdef ENABLE_DEBUG_PRINTF 61 #include <stdio.h> 62 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) 63 #else 64 #define DEBUG_PRINTF(fmt, ...) 65 #endif 66 67 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1) 68 69 using namespace lldb; 70 using namespace lldb_private; 71 72 73 static AccessType 74 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility) 75 { 76 switch (dwarf_accessibility) 77 { 78 case DW_ACCESS_public: return eAccessPublic; 79 case DW_ACCESS_private: return eAccessPrivate; 80 case DW_ACCESS_protected: return eAccessProtected; 81 default: break; 82 } 83 return eAccessNone; 84 } 85 86 void 87 SymbolFileDWARF::Initialize() 88 { 89 LogChannelDWARF::Initialize(); 90 PluginManager::RegisterPlugin (GetPluginNameStatic(), 91 GetPluginDescriptionStatic(), 92 CreateInstance); 93 } 94 95 void 96 SymbolFileDWARF::Terminate() 97 { 98 PluginManager::UnregisterPlugin (CreateInstance); 99 LogChannelDWARF::Initialize(); 100 } 101 102 103 const char * 104 SymbolFileDWARF::GetPluginNameStatic() 105 { 106 return "symbol-file.dwarf2"; 107 } 108 109 const char * 110 SymbolFileDWARF::GetPluginDescriptionStatic() 111 { 112 return "DWARF and DWARF3 debug symbol file reader."; 113 } 114 115 116 SymbolFile* 117 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file) 118 { 119 return new SymbolFileDWARF(obj_file); 120 } 121 122 TypeList * 123 SymbolFileDWARF::GetTypeList () 124 { 125 if (m_debug_map_symfile) 126 return m_debug_map_symfile->GetTypeList(); 127 return m_obj_file->GetModule()->GetTypeList(); 128 129 } 130 131 //---------------------------------------------------------------------- 132 // Gets the first parent that is a lexical block, function or inlined 133 // subroutine, or compile unit. 134 //---------------------------------------------------------------------- 135 static const DWARFDebugInfoEntry * 136 GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die) 137 { 138 const DWARFDebugInfoEntry *die; 139 for (die = child_die->GetParent(); die != NULL; die = die->GetParent()) 140 { 141 dw_tag_t tag = die->Tag(); 142 143 switch (tag) 144 { 145 case DW_TAG_compile_unit: 146 case DW_TAG_subprogram: 147 case DW_TAG_inlined_subroutine: 148 case DW_TAG_lexical_block: 149 return die; 150 } 151 } 152 return NULL; 153 } 154 155 156 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) : 157 SymbolFile (objfile), 158 m_debug_map_symfile (NULL), 159 m_clang_tu_decl (NULL), 160 m_flags(), 161 m_data_debug_abbrev(), 162 m_data_debug_frame(), 163 m_data_debug_info(), 164 m_data_debug_line(), 165 m_data_debug_loc(), 166 m_data_debug_ranges(), 167 m_data_debug_str(), 168 m_abbr(), 169 m_aranges(), 170 m_info(), 171 m_line(), 172 m_function_basename_index(), 173 m_function_fullname_index(), 174 m_function_method_index(), 175 m_function_selector_index(), 176 m_objc_class_selectors_index(), 177 m_global_index(), 178 m_type_index(), 179 m_namespace_index(), 180 m_indexed (false), 181 m_is_external_ast_source (false), 182 m_ranges(), 183 m_unique_ast_type_map () 184 { 185 } 186 187 SymbolFileDWARF::~SymbolFileDWARF() 188 { 189 if (m_is_external_ast_source) 190 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource (); 191 } 192 193 static const ConstString & 194 GetDWARFMachOSegmentName () 195 { 196 static ConstString g_dwarf_section_name ("__DWARF"); 197 return g_dwarf_section_name; 198 } 199 200 UniqueDWARFASTTypeMap & 201 SymbolFileDWARF::GetUniqueDWARFASTTypeMap () 202 { 203 if (m_debug_map_symfile) 204 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap (); 205 return m_unique_ast_type_map; 206 } 207 208 ClangASTContext & 209 SymbolFileDWARF::GetClangASTContext () 210 { 211 if (m_debug_map_symfile) 212 return m_debug_map_symfile->GetClangASTContext (); 213 214 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext(); 215 if (!m_is_external_ast_source) 216 { 217 m_is_external_ast_source = true; 218 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap ( 219 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl, 220 SymbolFileDWARF::CompleteObjCInterfaceDecl, 221 this)); 222 223 ast.SetExternalSource (ast_source_ap); 224 } 225 return ast; 226 } 227 228 void 229 SymbolFileDWARF::InitializeObject() 230 { 231 // Install our external AST source callbacks so we can complete Clang types. 232 Module *module = m_obj_file->GetModule(); 233 if (module) 234 { 235 const SectionList *section_list = m_obj_file->GetSectionList(); 236 237 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 238 239 // Memory map the DWARF mach-o segment so we have everything mmap'ed 240 // to keep our heap memory usage down. 241 if (section) 242 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data); 243 } 244 } 245 246 bool 247 SymbolFileDWARF::SupportedVersion(uint16_t version) 248 { 249 return version == 2 || version == 3; 250 } 251 252 uint32_t 253 SymbolFileDWARF::GetAbilities () 254 { 255 uint32_t abilities = 0; 256 if (m_obj_file != NULL) 257 { 258 const Section* section = NULL; 259 const SectionList *section_list = m_obj_file->GetSectionList(); 260 if (section_list == NULL) 261 return 0; 262 263 uint64_t debug_abbrev_file_size = 0; 264 uint64_t debug_aranges_file_size = 0; 265 uint64_t debug_frame_file_size = 0; 266 uint64_t debug_info_file_size = 0; 267 uint64_t debug_line_file_size = 0; 268 uint64_t debug_loc_file_size = 0; 269 uint64_t debug_macinfo_file_size = 0; 270 uint64_t debug_pubnames_file_size = 0; 271 uint64_t debug_pubtypes_file_size = 0; 272 uint64_t debug_ranges_file_size = 0; 273 uint64_t debug_str_file_size = 0; 274 275 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get(); 276 277 if (section) 278 section_list = §ion->GetChildren (); 279 280 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get(); 281 if (section != NULL) 282 { 283 debug_info_file_size = section->GetByteSize(); 284 285 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get(); 286 if (section) 287 debug_abbrev_file_size = section->GetByteSize(); 288 else 289 m_flags.Set (flagsGotDebugAbbrevData); 290 291 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get(); 292 if (section) 293 debug_aranges_file_size = section->GetByteSize(); 294 else 295 m_flags.Set (flagsGotDebugArangesData); 296 297 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get(); 298 if (section) 299 debug_frame_file_size = section->GetByteSize(); 300 else 301 m_flags.Set (flagsGotDebugFrameData); 302 303 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get(); 304 if (section) 305 debug_line_file_size = section->GetByteSize(); 306 else 307 m_flags.Set (flagsGotDebugLineData); 308 309 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get(); 310 if (section) 311 debug_loc_file_size = section->GetByteSize(); 312 else 313 m_flags.Set (flagsGotDebugLocData); 314 315 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get(); 316 if (section) 317 debug_macinfo_file_size = section->GetByteSize(); 318 else 319 m_flags.Set (flagsGotDebugMacInfoData); 320 321 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get(); 322 if (section) 323 debug_pubnames_file_size = section->GetByteSize(); 324 else 325 m_flags.Set (flagsGotDebugPubNamesData); 326 327 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get(); 328 if (section) 329 debug_pubtypes_file_size = section->GetByteSize(); 330 else 331 m_flags.Set (flagsGotDebugPubTypesData); 332 333 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get(); 334 if (section) 335 debug_ranges_file_size = section->GetByteSize(); 336 else 337 m_flags.Set (flagsGotDebugRangesData); 338 339 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get(); 340 if (section) 341 debug_str_file_size = section->GetByteSize(); 342 else 343 m_flags.Set (flagsGotDebugStrData); 344 } 345 346 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0) 347 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes; 348 349 if (debug_line_file_size > 0) 350 abilities |= LineTables; 351 352 if (debug_aranges_file_size > 0) 353 abilities |= AddressAcceleratorTable; 354 355 if (debug_pubnames_file_size > 0) 356 abilities |= FunctionAcceleratorTable; 357 358 if (debug_pubtypes_file_size > 0) 359 abilities |= TypeAcceleratorTable; 360 361 if (debug_macinfo_file_size > 0) 362 abilities |= MacroInformation; 363 364 if (debug_frame_file_size > 0) 365 abilities |= CallFrameInformation; 366 } 367 return abilities; 368 } 369 370 const DataExtractor& 371 SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data) 372 { 373 if (m_flags.IsClear (got_flag)) 374 { 375 m_flags.Set (got_flag); 376 const SectionList *section_list = m_obj_file->GetSectionList(); 377 if (section_list) 378 { 379 Section *section = section_list->FindSectionByType(sect_type, true).get(); 380 if (section) 381 { 382 // See if we memory mapped the DWARF segment? 383 if (m_dwarf_data.GetByteSize()) 384 { 385 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize()); 386 } 387 else 388 { 389 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0) 390 data.Clear(); 391 } 392 } 393 } 394 } 395 return data; 396 } 397 398 const DataExtractor& 399 SymbolFileDWARF::get_debug_abbrev_data() 400 { 401 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev); 402 } 403 404 const DataExtractor& 405 SymbolFileDWARF::get_debug_frame_data() 406 { 407 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame); 408 } 409 410 const DataExtractor& 411 SymbolFileDWARF::get_debug_info_data() 412 { 413 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info); 414 } 415 416 const DataExtractor& 417 SymbolFileDWARF::get_debug_line_data() 418 { 419 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line); 420 } 421 422 const DataExtractor& 423 SymbolFileDWARF::get_debug_loc_data() 424 { 425 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc); 426 } 427 428 const DataExtractor& 429 SymbolFileDWARF::get_debug_ranges_data() 430 { 431 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges); 432 } 433 434 const DataExtractor& 435 SymbolFileDWARF::get_debug_str_data() 436 { 437 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str); 438 } 439 440 441 DWARFDebugAbbrev* 442 SymbolFileDWARF::DebugAbbrev() 443 { 444 if (m_abbr.get() == NULL) 445 { 446 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data(); 447 if (debug_abbrev_data.GetByteSize() > 0) 448 { 449 m_abbr.reset(new DWARFDebugAbbrev()); 450 if (m_abbr.get()) 451 m_abbr->Parse(debug_abbrev_data); 452 } 453 } 454 return m_abbr.get(); 455 } 456 457 const DWARFDebugAbbrev* 458 SymbolFileDWARF::DebugAbbrev() const 459 { 460 return m_abbr.get(); 461 } 462 463 DWARFDebugAranges* 464 SymbolFileDWARF::DebugAranges() 465 { 466 // It turns out that llvm-gcc doesn't generate .debug_aranges in .o files 467 // and we are already parsing all of the DWARF because the .debug_pubnames 468 // is useless (it only mentions symbols that are externally visible), so 469 // don't use the .debug_aranges section, we should be using a debug aranges 470 // we got from SymbolFileDWARF::Index(). 471 472 if (!m_indexed) 473 Index(); 474 475 476 // if (m_aranges.get() == NULL) 477 // { 478 // Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); 479 // m_aranges.reset(new DWARFDebugAranges()); 480 // if (m_aranges.get()) 481 // { 482 // const DataExtractor &debug_aranges_data = get_debug_aranges_data(); 483 // if (debug_aranges_data.GetByteSize() > 0) 484 // m_aranges->Extract(debug_aranges_data); 485 // else 486 // m_aranges->Generate(this); 487 // } 488 // } 489 return m_aranges.get(); 490 } 491 492 const DWARFDebugAranges* 493 SymbolFileDWARF::DebugAranges() const 494 { 495 return m_aranges.get(); 496 } 497 498 499 DWARFDebugInfo* 500 SymbolFileDWARF::DebugInfo() 501 { 502 if (m_info.get() == NULL) 503 { 504 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); 505 if (get_debug_info_data().GetByteSize() > 0) 506 { 507 m_info.reset(new DWARFDebugInfo()); 508 if (m_info.get()) 509 { 510 m_info->SetDwarfData(this); 511 } 512 } 513 } 514 return m_info.get(); 515 } 516 517 const DWARFDebugInfo* 518 SymbolFileDWARF::DebugInfo() const 519 { 520 return m_info.get(); 521 } 522 523 DWARFCompileUnit* 524 SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid) 525 { 526 DWARFDebugInfo* info = DebugInfo(); 527 if (info) 528 return info->GetCompileUnit(cu_uid).get(); 529 return NULL; 530 } 531 532 533 DWARFDebugRanges* 534 SymbolFileDWARF::DebugRanges() 535 { 536 if (m_ranges.get() == NULL) 537 { 538 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this); 539 if (get_debug_ranges_data().GetByteSize() > 0) 540 { 541 m_ranges.reset(new DWARFDebugRanges()); 542 if (m_ranges.get()) 543 m_ranges->Extract(this); 544 } 545 } 546 return m_ranges.get(); 547 } 548 549 const DWARFDebugRanges* 550 SymbolFileDWARF::DebugRanges() const 551 { 552 return m_ranges.get(); 553 } 554 555 bool 556 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp) 557 { 558 if (curr_cu != NULL) 559 { 560 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly (); 561 if (cu_die) 562 { 563 const char * cu_die_name = cu_die->GetName(this, curr_cu); 564 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL); 565 LanguageType class_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0); 566 if (cu_die_name) 567 { 568 FileSpec cu_file_spec; 569 570 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0') 571 { 572 // If we have a full path to the compile unit, we don't need to resolve 573 // the file. This can be expensive e.g. when the source files are NFS mounted. 574 cu_file_spec.SetFile (cu_die_name, false); 575 } 576 else 577 { 578 std::string fullpath(cu_comp_dir); 579 if (*fullpath.rbegin() != '/') 580 fullpath += '/'; 581 fullpath += cu_die_name; 582 cu_file_spec.SetFile (fullpath.c_str(), false); 583 } 584 585 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), curr_cu, cu_file_spec, curr_cu->GetOffset(), class_language)); 586 if (compile_unit_sp.get()) 587 { 588 curr_cu->SetUserData(compile_unit_sp.get()); 589 return true; 590 } 591 } 592 } 593 } 594 return false; 595 } 596 597 uint32_t 598 SymbolFileDWARF::GetNumCompileUnits() 599 { 600 DWARFDebugInfo* info = DebugInfo(); 601 if (info) 602 return info->GetNumCompileUnits(); 603 return 0; 604 } 605 606 CompUnitSP 607 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) 608 { 609 CompUnitSP comp_unit; 610 DWARFDebugInfo* info = DebugInfo(); 611 if (info) 612 { 613 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx); 614 if (curr_cu != NULL) 615 { 616 // Our symbol vendor shouldn't be asking us to add a compile unit that 617 // has already been added to it, which this DWARF plug-in knows as it 618 // stores the lldb compile unit (CompileUnit) pointer in each 619 // DWARFCompileUnit object when it gets added. 620 assert(curr_cu->GetUserData() == NULL); 621 ParseCompileUnit(curr_cu, comp_unit); 622 } 623 } 624 return comp_unit; 625 } 626 627 static void 628 AddRangesToBlock 629 ( 630 Block& block, 631 DWARFDebugRanges::RangeList& ranges, 632 addr_t block_base_addr 633 ) 634 { 635 ranges.SubtractOffset (block_base_addr); 636 size_t range_idx = 0; 637 const DWARFDebugRanges::Range *debug_range; 638 for (range_idx = 0; (debug_range = ranges.RangeAtIndex(range_idx)) != NULL; range_idx++) 639 { 640 block.AddRange(debug_range->begin_offset, debug_range->end_offset); 641 } 642 } 643 644 645 Function * 646 SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die) 647 { 648 DWARFDebugRanges::RangeList func_ranges; 649 const char *name = NULL; 650 const char *mangled = NULL; 651 int decl_file = 0; 652 int decl_line = 0; 653 int decl_column = 0; 654 int call_file = 0; 655 int call_line = 0; 656 int call_column = 0; 657 DWARFExpression frame_base; 658 659 assert (die->Tag() == DW_TAG_subprogram); 660 661 if (die->Tag() != DW_TAG_subprogram) 662 return NULL; 663 664 const DWARFDebugInfoEntry *parent_die = die->GetParent(); 665 switch (parent_die->Tag()) 666 { 667 case DW_TAG_structure_type: 668 case DW_TAG_class_type: 669 // We have methods of a class or struct 670 { 671 Type *class_type = ResolveType (dwarf_cu, parent_die); 672 if (class_type) 673 class_type->GetClangFullType(); 674 } 675 break; 676 677 default: 678 // Parse the function prototype as a type that can then be added to concrete function instance 679 ParseTypes (sc, dwarf_cu, die, false, false); 680 break; 681 } 682 683 //FixupTypes(); 684 685 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base)) 686 { 687 // Union of all ranges in the function DIE (if the function is discontiguous) 688 AddressRange func_range; 689 lldb::addr_t lowest_func_addr = func_ranges.LowestAddress(0); 690 lldb::addr_t highest_func_addr = func_ranges.HighestAddress(0); 691 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr) 692 { 693 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList()); 694 if (func_range.GetBaseAddress().IsValid()) 695 func_range.SetByteSize(highest_func_addr - lowest_func_addr); 696 } 697 698 if (func_range.GetBaseAddress().IsValid()) 699 { 700 Mangled func_name; 701 if (mangled) 702 func_name.SetValue(mangled, true); 703 else if (name) 704 func_name.SetValue(name, false); 705 706 FunctionSP func_sp; 707 std::auto_ptr<Declaration> decl_ap; 708 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 709 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 710 decl_line, 711 decl_column)); 712 713 Type *func_type = m_die_to_type.lookup (die); 714 715 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED); 716 717 func_range.GetBaseAddress().ResolveLinkedAddress(); 718 719 func_sp.reset(new Function (sc.comp_unit, 720 die->GetOffset(), // UserID is the DIE offset 721 die->GetOffset(), 722 func_name, 723 func_type, 724 func_range)); // first address range 725 726 if (func_sp.get() != NULL) 727 { 728 func_sp->GetFrameBaseExpression() = frame_base; 729 sc.comp_unit->AddFunction(func_sp); 730 return func_sp.get(); 731 } 732 } 733 } 734 return NULL; 735 } 736 737 size_t 738 SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc) 739 { 740 assert (sc.comp_unit); 741 size_t functions_added = 0; 742 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 743 if (dwarf_cu) 744 { 745 DWARFDIECollection function_dies; 746 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies); 747 size_t func_idx; 748 for (func_idx = 0; func_idx < num_funtions; ++func_idx) 749 { 750 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx); 751 if (sc.comp_unit->FindFunctionByUID (die->GetOffset()).get() == NULL) 752 { 753 if (ParseCompileUnitFunction(sc, dwarf_cu, die)) 754 ++functions_added; 755 } 756 } 757 //FixupTypes(); 758 } 759 return functions_added; 760 } 761 762 bool 763 SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files) 764 { 765 assert (sc.comp_unit); 766 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 767 assert (curr_cu); 768 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly(); 769 770 if (cu_die) 771 { 772 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL); 773 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 774 775 // All file indexes in DWARF are one based and a file of index zero is 776 // supposed to be the compile unit itself. 777 support_files.Append (*sc.comp_unit); 778 779 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files); 780 } 781 return false; 782 } 783 784 struct ParseDWARFLineTableCallbackInfo 785 { 786 LineTable* line_table; 787 const SectionList *section_list; 788 lldb::addr_t prev_sect_file_base_addr; 789 lldb::addr_t curr_sect_file_base_addr; 790 bool is_oso_for_debug_map; 791 bool prev_in_final_executable; 792 DWARFDebugLine::Row prev_row; 793 SectionSP prev_section_sp; 794 SectionSP curr_section_sp; 795 }; 796 797 //---------------------------------------------------------------------- 798 // ParseStatementTableCallback 799 //---------------------------------------------------------------------- 800 static void 801 ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData) 802 { 803 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table; 804 if (state.row == DWARFDebugLine::State::StartParsingLineTable) 805 { 806 // Just started parsing the line table 807 } 808 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) 809 { 810 // Done parsing line table, nothing to do for the cleanup 811 } 812 else 813 { 814 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData; 815 // We have a new row, lets append it 816 817 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false) 818 { 819 info->prev_section_sp = info->curr_section_sp; 820 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr; 821 // If this is an end sequence entry, then we subtract one from the 822 // address to make sure we get an address that is not the end of 823 // a section. 824 if (state.end_sequence && state.address != 0) 825 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1); 826 else 827 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address); 828 829 if (info->curr_section_sp.get()) 830 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress (); 831 else 832 info->curr_sect_file_base_addr = 0; 833 } 834 if (info->curr_section_sp.get()) 835 { 836 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr; 837 // Check for the fancy section magic to determine if we 838 839 if (info->is_oso_for_debug_map) 840 { 841 // When this is a debug map object file that contains DWARF 842 // (referenced from an N_OSO debug map nlist entry) we will have 843 // a file address in the file range for our section from the 844 // original .o file, and a load address in the executable that 845 // contains the debug map. 846 // 847 // If the sections for the file range and load range are 848 // different, we have a remapped section for the function and 849 // this address is resolved. If they are the same, then the 850 // function for this address didn't make it into the final 851 // executable. 852 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL; 853 854 // If we are doing DWARF with debug map, then we need to carefully 855 // add each line table entry as there may be gaps as functions 856 // get moved around or removed. 857 if (!info->prev_row.end_sequence && info->prev_section_sp.get()) 858 { 859 if (info->prev_in_final_executable) 860 { 861 bool terminate_previous_entry = false; 862 if (!curr_in_final_executable) 863 { 864 // Check for the case where the previous line entry 865 // in a function made it into the final executable, 866 // yet the current line entry falls in a function 867 // that didn't. The line table used to be contiguous 868 // through this address range but now it isn't. We 869 // need to terminate the previous line entry so 870 // that we can reconstruct the line range correctly 871 // for it and to keep the line table correct. 872 terminate_previous_entry = true; 873 } 874 else if (info->curr_section_sp.get() != info->prev_section_sp.get()) 875 { 876 // Check for cases where the line entries used to be 877 // contiguous address ranges, but now they aren't. 878 // This can happen when order files specify the 879 // ordering of the functions. 880 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr; 881 Section *curr_sect = info->curr_section_sp.get(); 882 Section *prev_sect = info->prev_section_sp.get(); 883 assert (curr_sect->GetLinkedSection()); 884 assert (prev_sect->GetLinkedSection()); 885 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address; 886 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset; 887 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset; 888 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr; 889 if (object_file_addr_delta != linked_file_addr_delta) 890 terminate_previous_entry = true; 891 } 892 893 if (terminate_previous_entry) 894 { 895 line_table->InsertLineEntry (info->prev_section_sp, 896 state.address - info->prev_sect_file_base_addr, 897 info->prev_row.line, 898 info->prev_row.column, 899 info->prev_row.file, 900 false, // is_stmt 901 false, // basic_block 902 false, // state.prologue_end 903 false, // state.epilogue_begin 904 true); // end_sequence); 905 } 906 } 907 } 908 909 if (curr_in_final_executable) 910 { 911 line_table->InsertLineEntry (info->curr_section_sp, 912 curr_line_section_offset, 913 state.line, 914 state.column, 915 state.file, 916 state.is_stmt, 917 state.basic_block, 918 state.prologue_end, 919 state.epilogue_begin, 920 state.end_sequence); 921 info->prev_section_sp = info->curr_section_sp; 922 } 923 else 924 { 925 // If the current address didn't make it into the final 926 // executable, the current section will be the __text 927 // segment in the .o file, so we need to clear this so 928 // we can catch the next function that did make it into 929 // the final executable. 930 info->prev_section_sp.reset(); 931 info->curr_section_sp.reset(); 932 } 933 934 info->prev_in_final_executable = curr_in_final_executable; 935 } 936 else 937 { 938 // We are not in an object file that contains DWARF for an 939 // N_OSO, this is just a normal DWARF file. The DWARF spec 940 // guarantees that the addresses will be in increasing order 941 // so, since we store line tables in file address order, we 942 // can always just append the line entry without needing to 943 // search for the correct insertion point (we don't need to 944 // use LineEntry::InsertLineEntry()). 945 line_table->AppendLineEntry (info->curr_section_sp, 946 curr_line_section_offset, 947 state.line, 948 state.column, 949 state.file, 950 state.is_stmt, 951 state.basic_block, 952 state.prologue_end, 953 state.epilogue_begin, 954 state.end_sequence); 955 } 956 } 957 958 info->prev_row = state; 959 } 960 } 961 962 bool 963 SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc) 964 { 965 assert (sc.comp_unit); 966 if (sc.comp_unit->GetLineTable() != NULL) 967 return true; 968 969 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 970 if (dwarf_cu) 971 { 972 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly(); 973 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET); 974 if (cu_line_offset != DW_INVALID_OFFSET) 975 { 976 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit)); 977 if (line_table_ap.get()) 978 { 979 ParseDWARFLineTableCallbackInfo info = { line_table_ap.get(), m_obj_file->GetSectionList(), 0, 0, m_debug_map_symfile != NULL, false}; 980 uint32_t offset = cu_line_offset; 981 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info); 982 sc.comp_unit->SetLineTable(line_table_ap.release()); 983 return true; 984 } 985 } 986 } 987 return false; 988 } 989 990 size_t 991 SymbolFileDWARF::ParseFunctionBlocks 992 ( 993 const SymbolContext& sc, 994 Block *parent_block, 995 DWARFCompileUnit* dwarf_cu, 996 const DWARFDebugInfoEntry *die, 997 addr_t subprogram_low_pc, 998 bool parse_siblings, 999 bool parse_children 1000 ) 1001 { 1002 size_t blocks_added = 0; 1003 while (die != NULL) 1004 { 1005 dw_tag_t tag = die->Tag(); 1006 1007 switch (tag) 1008 { 1009 case DW_TAG_inlined_subroutine: 1010 case DW_TAG_subprogram: 1011 case DW_TAG_lexical_block: 1012 { 1013 DWARFDebugRanges::RangeList ranges; 1014 const char *name = NULL; 1015 const char *mangled_name = NULL; 1016 Block *block = NULL; 1017 if (tag != DW_TAG_subprogram) 1018 { 1019 BlockSP block_sp(new Block (die->GetOffset())); 1020 parent_block->AddChild(block_sp); 1021 block = block_sp.get(); 1022 } 1023 else 1024 { 1025 block = parent_block; 1026 } 1027 1028 int decl_file = 0; 1029 int decl_line = 0; 1030 int decl_column = 0; 1031 int call_file = 0; 1032 int call_line = 0; 1033 int call_column = 0; 1034 if (die->GetDIENamesAndRanges (this, 1035 dwarf_cu, 1036 name, 1037 mangled_name, 1038 ranges, 1039 decl_file, decl_line, decl_column, 1040 call_file, call_line, call_column)) 1041 { 1042 if (tag == DW_TAG_subprogram) 1043 { 1044 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS); 1045 subprogram_low_pc = ranges.LowestAddress(0); 1046 } 1047 else if (tag == DW_TAG_inlined_subroutine) 1048 { 1049 // We get called here for inlined subroutines in two ways. 1050 // The first time is when we are making the Function object 1051 // for this inlined concrete instance. Since we're creating a top level block at 1052 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to 1053 // adjust the containing address. 1054 // The second time is when we are parsing the blocks inside the function that contains 1055 // the inlined concrete instance. Since these will be blocks inside the containing "real" 1056 // function the offset will be for that function. 1057 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) 1058 { 1059 subprogram_low_pc = ranges.LowestAddress(0); 1060 } 1061 } 1062 1063 AddRangesToBlock (*block, ranges, subprogram_low_pc); 1064 1065 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL)) 1066 { 1067 std::auto_ptr<Declaration> decl_ap; 1068 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1069 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 1070 decl_line, decl_column)); 1071 1072 std::auto_ptr<Declaration> call_ap; 1073 if (call_file != 0 || call_line != 0 || call_column != 0) 1074 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file), 1075 call_line, call_column)); 1076 1077 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get()); 1078 } 1079 1080 ++blocks_added; 1081 1082 if (parse_children && die->HasChildren()) 1083 { 1084 blocks_added += ParseFunctionBlocks (sc, 1085 block, 1086 dwarf_cu, 1087 die->GetFirstChild(), 1088 subprogram_low_pc, 1089 true, 1090 true); 1091 } 1092 } 1093 } 1094 break; 1095 default: 1096 break; 1097 } 1098 1099 if (parse_siblings) 1100 die = die->GetSibling(); 1101 else 1102 die = NULL; 1103 } 1104 return blocks_added; 1105 } 1106 1107 size_t 1108 SymbolFileDWARF::ParseChildMembers 1109 ( 1110 const SymbolContext& sc, 1111 DWARFCompileUnit* dwarf_cu, 1112 const DWARFDebugInfoEntry *parent_die, 1113 clang_type_t class_clang_type, 1114 const LanguageType class_language, 1115 std::vector<clang::CXXBaseSpecifier *>& base_classes, 1116 std::vector<int>& member_accessibilities, 1117 DWARFDIECollection& member_function_dies, 1118 AccessType& default_accessibility, 1119 bool &is_a_class 1120 ) 1121 { 1122 if (parent_die == NULL) 1123 return 0; 1124 1125 size_t count = 0; 1126 const DWARFDebugInfoEntry *die; 1127 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 1128 uint32_t member_idx = 0; 1129 1130 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 1131 { 1132 dw_tag_t tag = die->Tag(); 1133 1134 switch (tag) 1135 { 1136 case DW_TAG_member: 1137 { 1138 DWARFDebugInfoEntry::Attributes attributes; 1139 const size_t num_attributes = die->GetAttributes (this, 1140 dwarf_cu, 1141 fixed_form_sizes, 1142 attributes); 1143 if (num_attributes > 0) 1144 { 1145 Declaration decl; 1146 //DWARFExpression location; 1147 const char *name = NULL; 1148 bool is_artificial = false; 1149 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 1150 AccessType accessibility = eAccessNone; 1151 //off_t member_offset = 0; 1152 size_t byte_size = 0; 1153 size_t bit_offset = 0; 1154 size_t bit_size = 0; 1155 uint32_t i; 1156 for (i=0; i<num_attributes && !is_artificial; ++i) 1157 { 1158 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1159 DWARFFormValue form_value; 1160 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1161 { 1162 switch (attr) 1163 { 1164 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1165 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1166 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1167 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 1168 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 1169 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break; 1170 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break; 1171 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 1172 case DW_AT_data_member_location: 1173 // if (form_value.BlockData()) 1174 // { 1175 // Value initialValue(0); 1176 // Value memberOffset(0); 1177 // const DataExtractor& debug_info_data = get_debug_info_data(); 1178 // uint32_t block_length = form_value.Unsigned(); 1179 // uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 1180 // if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL)) 1181 // { 1182 // member_offset = memberOffset.ResolveValue(NULL, NULL).UInt(); 1183 // } 1184 // } 1185 break; 1186 1187 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break; 1188 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 1189 case DW_AT_declaration: 1190 case DW_AT_description: 1191 case DW_AT_mutable: 1192 case DW_AT_visibility: 1193 default: 1194 case DW_AT_sibling: 1195 break; 1196 } 1197 } 1198 } 1199 1200 // FIXME: Make Clang ignore Objective-C accessibility for expressions 1201 1202 if (class_language == eLanguageTypeObjC || 1203 class_language == eLanguageTypeObjC_plus_plus) 1204 accessibility = eAccessNone; 1205 1206 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name)) 1207 { 1208 // Not all compilers will mark the vtable pointer 1209 // member as artificial (llvm-gcc). We can't have 1210 // the virtual members in our classes otherwise it 1211 // throws off all child offsets since we end up 1212 // having and extra pointer sized member in our 1213 // class layouts. 1214 is_artificial = true; 1215 } 1216 1217 if (is_artificial == false) 1218 { 1219 Type *member_type = ResolveTypeUID(encoding_uid); 1220 assert(member_type); 1221 if (accessibility == eAccessNone) 1222 accessibility = default_accessibility; 1223 member_accessibilities.push_back(accessibility); 1224 1225 GetClangASTContext().AddFieldToRecordType (class_clang_type, 1226 name, 1227 member_type->GetClangLayoutType(), 1228 accessibility, 1229 bit_size); 1230 } 1231 } 1232 ++member_idx; 1233 } 1234 break; 1235 1236 case DW_TAG_subprogram: 1237 // Let the type parsing code handle this one for us. 1238 member_function_dies.Append (die); 1239 break; 1240 1241 case DW_TAG_inheritance: 1242 { 1243 is_a_class = true; 1244 if (default_accessibility == eAccessNone) 1245 default_accessibility = eAccessPrivate; 1246 // TODO: implement DW_TAG_inheritance type parsing 1247 DWARFDebugInfoEntry::Attributes attributes; 1248 const size_t num_attributes = die->GetAttributes (this, 1249 dwarf_cu, 1250 fixed_form_sizes, 1251 attributes); 1252 if (num_attributes > 0) 1253 { 1254 Declaration decl; 1255 DWARFExpression location; 1256 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 1257 AccessType accessibility = default_accessibility; 1258 bool is_virtual = false; 1259 bool is_base_of_class = true; 1260 off_t member_offset = 0; 1261 uint32_t i; 1262 for (i=0; i<num_attributes; ++i) 1263 { 1264 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1265 DWARFFormValue form_value; 1266 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 1267 { 1268 switch (attr) 1269 { 1270 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1271 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1272 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1273 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 1274 case DW_AT_data_member_location: 1275 if (form_value.BlockData()) 1276 { 1277 Value initialValue(0); 1278 Value memberOffset(0); 1279 const DataExtractor& debug_info_data = get_debug_info_data(); 1280 uint32_t block_length = form_value.Unsigned(); 1281 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 1282 if (DWARFExpression::Evaluate (NULL, 1283 NULL, 1284 NULL, 1285 NULL, 1286 NULL, 1287 debug_info_data, 1288 block_offset, 1289 block_length, 1290 eRegisterKindDWARF, 1291 &initialValue, 1292 memberOffset, 1293 NULL)) 1294 { 1295 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt(); 1296 } 1297 } 1298 break; 1299 1300 case DW_AT_accessibility: 1301 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 1302 break; 1303 1304 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break; 1305 default: 1306 case DW_AT_sibling: 1307 break; 1308 } 1309 } 1310 } 1311 1312 Type *base_class_type = ResolveTypeUID(encoding_uid); 1313 assert(base_class_type); 1314 1315 clang_type_t base_class_clang_type = base_class_type->GetClangFullType(); 1316 assert (base_class_clang_type); 1317 if (class_language == eLanguageTypeObjC) 1318 { 1319 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type); 1320 } 1321 else 1322 { 1323 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type, 1324 accessibility, 1325 is_virtual, 1326 is_base_of_class)); 1327 } 1328 } 1329 } 1330 break; 1331 1332 default: 1333 break; 1334 } 1335 } 1336 return count; 1337 } 1338 1339 1340 clang::DeclContext* 1341 SymbolFileDWARF::GetClangDeclContextForTypeUID (lldb::user_id_t type_uid) 1342 { 1343 DWARFDebugInfo* debug_info = DebugInfo(); 1344 if (debug_info) 1345 { 1346 DWARFCompileUnitSP cu_sp; 1347 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp); 1348 if (die) 1349 return GetClangDeclContextForDIE (cu_sp.get(), die); 1350 } 1351 return NULL; 1352 } 1353 1354 Type* 1355 SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid) 1356 { 1357 DWARFDebugInfo* debug_info = DebugInfo(); 1358 if (debug_info) 1359 { 1360 DWARFCompileUnitSP cu_sp; 1361 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp); 1362 if (type_die != NULL) 1363 { 1364 // We might be coming in in the middle of a type tree (a class 1365 // withing a class, an enum within a class), so parse any needed 1366 // parent DIEs before we get to this one... 1367 const DWARFDebugInfoEntry* parent_die = type_die->GetParent(); 1368 switch (parent_die->Tag()) 1369 { 1370 case DW_TAG_structure_type: 1371 case DW_TAG_union_type: 1372 case DW_TAG_class_type: 1373 ResolveType(cu_sp.get(), parent_die); 1374 break; 1375 } 1376 return ResolveType (cu_sp.get(), type_die); 1377 } 1378 } 1379 return NULL; 1380 } 1381 1382 // This function is used when SymbolFileDWARFDebugMap owns a bunch of 1383 // SymbolFileDWARF objects to detect if this DWARF file is the one that 1384 // can resolve a clang_type. 1385 bool 1386 SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type) 1387 { 1388 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type); 1389 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers); 1390 return die != NULL; 1391 } 1392 1393 1394 lldb::clang_type_t 1395 SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type) 1396 { 1397 // We have a struct/union/class/enum that needs to be fully resolved. 1398 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type); 1399 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers); 1400 if (die == NULL) 1401 { 1402 // if (m_debug_map_symfile) 1403 // { 1404 // Type *type = m_die_to_type[die]; 1405 // if (type && type->GetSymbolFile() != this) 1406 // return type->GetClangType(); 1407 // } 1408 // We have already resolved this type... 1409 return clang_type; 1410 } 1411 // Once we start resolving this type, remove it from the forward declaration 1412 // map in case anyone child members or other types require this type to get resolved. 1413 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition 1414 // are done. 1415 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers); 1416 1417 1418 DWARFDebugInfo* debug_info = DebugInfo(); 1419 1420 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get(); 1421 Type *type = m_die_to_type.lookup (die); 1422 1423 const dw_tag_t tag = die->Tag(); 1424 1425 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") - resolve forward declaration...\n", 1426 die->GetOffset(), 1427 DW_TAG_value_to_name(tag), 1428 type->GetName().AsCString()); 1429 assert (clang_type); 1430 DWARFDebugInfoEntry::Attributes attributes; 1431 1432 ClangASTContext &ast = GetClangASTContext(); 1433 1434 switch (tag) 1435 { 1436 case DW_TAG_structure_type: 1437 case DW_TAG_union_type: 1438 case DW_TAG_class_type: 1439 ast.StartTagDeclarationDefinition (clang_type); 1440 if (die->HasChildren()) 1441 { 1442 LanguageType class_language = eLanguageTypeUnknown; 1443 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type); 1444 if (is_objc_class) 1445 class_language = eLanguageTypeObjC; 1446 1447 int tag_decl_kind = -1; 1448 AccessType default_accessibility = eAccessNone; 1449 if (tag == DW_TAG_structure_type) 1450 { 1451 tag_decl_kind = clang::TTK_Struct; 1452 default_accessibility = eAccessPublic; 1453 } 1454 else if (tag == DW_TAG_union_type) 1455 { 1456 tag_decl_kind = clang::TTK_Union; 1457 default_accessibility = eAccessPublic; 1458 } 1459 else if (tag == DW_TAG_class_type) 1460 { 1461 tag_decl_kind = clang::TTK_Class; 1462 default_accessibility = eAccessPrivate; 1463 } 1464 1465 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu)); 1466 std::vector<clang::CXXBaseSpecifier *> base_classes; 1467 std::vector<int> member_accessibilities; 1468 bool is_a_class = false; 1469 // Parse members and base classes first 1470 DWARFDIECollection member_function_dies; 1471 1472 ParseChildMembers (sc, 1473 curr_cu, 1474 die, 1475 clang_type, 1476 class_language, 1477 base_classes, 1478 member_accessibilities, 1479 member_function_dies, 1480 default_accessibility, 1481 is_a_class); 1482 1483 // Now parse any methods if there were any... 1484 size_t num_functions = member_function_dies.Size(); 1485 if (num_functions > 0) 1486 { 1487 for (size_t i=0; i<num_functions; ++i) 1488 { 1489 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i)); 1490 } 1491 } 1492 1493 if (class_language == eLanguageTypeObjC) 1494 { 1495 std::string class_str (ClangASTContext::GetTypeName (clang_type)); 1496 if (!class_str.empty()) 1497 { 1498 1499 ConstString class_name (class_str.c_str()); 1500 std::vector<NameToDIE::Info> method_die_infos; 1501 if (m_objc_class_selectors_index.Find (class_name, method_die_infos)) 1502 { 1503 DWARFCompileUnit* method_cu = NULL; 1504 DWARFCompileUnit* prev_method_cu = NULL; 1505 const size_t num_objc_methods = method_die_infos.size(); 1506 for (size_t i=0;i<num_objc_methods; ++i, prev_method_cu = method_cu) 1507 { 1508 method_cu = debug_info->GetCompileUnitAtIndex(method_die_infos[i].cu_idx); 1509 1510 if (method_cu != prev_method_cu) 1511 method_cu->ExtractDIEsIfNeeded (false); 1512 1513 DWARFDebugInfoEntry *method_die = method_cu->GetDIEAtIndexUnchecked(method_die_infos[i].die_idx); 1514 1515 ResolveType (method_cu, method_die); 1516 } 1517 } 1518 } 1519 } 1520 1521 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we 1522 // need to tell the clang type it is actually a class. 1523 if (class_language != eLanguageTypeObjC) 1524 { 1525 if (is_a_class && tag_decl_kind != clang::TTK_Class) 1526 ast.SetTagTypeKind (clang_type, clang::TTK_Class); 1527 } 1528 1529 // Since DW_TAG_structure_type gets used for both classes 1530 // and structures, we may need to set any DW_TAG_member 1531 // fields to have a "private" access if none was specified. 1532 // When we parsed the child members we tracked that actual 1533 // accessibility value for each DW_TAG_member in the 1534 // "member_accessibilities" array. If the value for the 1535 // member is zero, then it was set to the "default_accessibility" 1536 // which for structs was "public". Below we correct this 1537 // by setting any fields to "private" that weren't correctly 1538 // set. 1539 if (is_a_class && !member_accessibilities.empty()) 1540 { 1541 // This is a class and all members that didn't have 1542 // their access specified are private. 1543 ast.SetDefaultAccessForRecordFields (clang_type, 1544 eAccessPrivate, 1545 &member_accessibilities.front(), 1546 member_accessibilities.size()); 1547 } 1548 1549 if (!base_classes.empty()) 1550 { 1551 ast.SetBaseClassesForClassType (clang_type, 1552 &base_classes.front(), 1553 base_classes.size()); 1554 1555 // Clang will copy each CXXBaseSpecifier in "base_classes" 1556 // so we have to free them all. 1557 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(), 1558 base_classes.size()); 1559 } 1560 1561 } 1562 ast.CompleteTagDeclarationDefinition (clang_type); 1563 return clang_type; 1564 1565 case DW_TAG_enumeration_type: 1566 ast.StartTagDeclarationDefinition (clang_type); 1567 if (die->HasChildren()) 1568 { 1569 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu)); 1570 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die); 1571 } 1572 ast.CompleteTagDeclarationDefinition (clang_type); 1573 return clang_type; 1574 1575 default: 1576 assert(false && "not a forward clang type decl!"); 1577 break; 1578 } 1579 return NULL; 1580 } 1581 1582 Type* 1583 SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed) 1584 { 1585 if (type_die != NULL) 1586 { 1587 Type *type = m_die_to_type.lookup (type_die); 1588 if (type == NULL) 1589 type = GetTypeForDIE (curr_cu, type_die).get(); 1590 if (assert_not_being_parsed) 1591 assert (type != DIE_IS_BEING_PARSED); 1592 return type; 1593 } 1594 return NULL; 1595 } 1596 1597 CompileUnit* 1598 SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx) 1599 { 1600 // Check if the symbol vendor already knows about this compile unit? 1601 if (curr_cu->GetUserData() == NULL) 1602 { 1603 // The symbol vendor doesn't know about this compile unit, we 1604 // need to parse and add it to the symbol vendor object. 1605 CompUnitSP dc_cu; 1606 ParseCompileUnit(curr_cu, dc_cu); 1607 if (dc_cu.get()) 1608 { 1609 // Figure out the compile unit index if we weren't given one 1610 if (cu_idx == UINT32_MAX) 1611 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx); 1612 1613 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx); 1614 1615 if (m_debug_map_symfile) 1616 m_debug_map_symfile->SetCompileUnit(this, dc_cu); 1617 } 1618 } 1619 return (CompileUnit*)curr_cu->GetUserData(); 1620 } 1621 1622 bool 1623 SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc) 1624 { 1625 sc.Clear(); 1626 // Check if the symbol vendor already knows about this compile unit? 1627 sc.module_sp = m_obj_file->GetModule()->GetSP(); 1628 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX); 1629 1630 sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get(); 1631 if (sc.function == NULL) 1632 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die); 1633 1634 return sc.function != NULL; 1635 } 1636 1637 uint32_t 1638 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) 1639 { 1640 Timer scoped_timer(__PRETTY_FUNCTION__, 1641 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)", 1642 so_addr.GetSection(), 1643 so_addr.GetOffset(), 1644 resolve_scope); 1645 uint32_t resolved = 0; 1646 if (resolve_scope & ( eSymbolContextCompUnit | 1647 eSymbolContextFunction | 1648 eSymbolContextBlock | 1649 eSymbolContextLineEntry)) 1650 { 1651 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1652 1653 DWARFDebugAranges* debug_aranges = DebugAranges(); 1654 DWARFDebugInfo* debug_info = DebugInfo(); 1655 if (debug_aranges) 1656 { 1657 dw_offset_t cu_offset = debug_aranges->FindAddress(file_vm_addr); 1658 if (cu_offset != DW_INVALID_OFFSET) 1659 { 1660 uint32_t cu_idx; 1661 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get(); 1662 if (curr_cu) 1663 { 1664 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1665 assert(sc.comp_unit != NULL); 1666 resolved |= eSymbolContextCompUnit; 1667 1668 if (resolve_scope & eSymbolContextLineEntry) 1669 { 1670 LineTable *line_table = sc.comp_unit->GetLineTable(); 1671 if (line_table == NULL) 1672 { 1673 if (ParseCompileUnitLineTable(sc)) 1674 line_table = sc.comp_unit->GetLineTable(); 1675 } 1676 if (line_table != NULL) 1677 { 1678 if (so_addr.IsLinkedAddress()) 1679 { 1680 Address linked_addr (so_addr); 1681 linked_addr.ResolveLinkedAddress(); 1682 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry)) 1683 { 1684 resolved |= eSymbolContextLineEntry; 1685 } 1686 } 1687 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry)) 1688 { 1689 resolved |= eSymbolContextLineEntry; 1690 } 1691 } 1692 } 1693 1694 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 1695 { 1696 DWARFDebugInfoEntry *function_die = NULL; 1697 DWARFDebugInfoEntry *block_die = NULL; 1698 if (resolve_scope & eSymbolContextBlock) 1699 { 1700 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die); 1701 } 1702 else 1703 { 1704 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL); 1705 } 1706 1707 if (function_die != NULL) 1708 { 1709 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get(); 1710 if (sc.function == NULL) 1711 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die); 1712 } 1713 1714 if (sc.function != NULL) 1715 { 1716 resolved |= eSymbolContextFunction; 1717 1718 if (resolve_scope & eSymbolContextBlock) 1719 { 1720 Block& block = sc.function->GetBlock (true); 1721 1722 if (block_die != NULL) 1723 sc.block = block.FindBlockByID (block_die->GetOffset()); 1724 else 1725 sc.block = block.FindBlockByID (function_die->GetOffset()); 1726 if (sc.block) 1727 resolved |= eSymbolContextBlock; 1728 } 1729 } 1730 } 1731 } 1732 } 1733 } 1734 } 1735 return resolved; 1736 } 1737 1738 1739 1740 uint32_t 1741 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) 1742 { 1743 const uint32_t prev_size = sc_list.GetSize(); 1744 if (resolve_scope & eSymbolContextCompUnit) 1745 { 1746 DWARFDebugInfo* debug_info = DebugInfo(); 1747 if (debug_info) 1748 { 1749 uint32_t cu_idx; 1750 DWARFCompileUnit* curr_cu = NULL; 1751 1752 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx) 1753 { 1754 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1755 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0; 1756 if (check_inlines || file_spec_matches_cu_file_spec) 1757 { 1758 SymbolContext sc (m_obj_file->GetModule()); 1759 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx); 1760 assert(sc.comp_unit != NULL); 1761 1762 uint32_t file_idx = UINT32_MAX; 1763 1764 // If we are looking for inline functions only and we don't 1765 // find it in the support files, we are done. 1766 if (check_inlines) 1767 { 1768 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec); 1769 if (file_idx == UINT32_MAX) 1770 continue; 1771 } 1772 1773 if (line != 0) 1774 { 1775 LineTable *line_table = sc.comp_unit->GetLineTable(); 1776 1777 if (line_table != NULL && line != 0) 1778 { 1779 // We will have already looked up the file index if 1780 // we are searching for inline entries. 1781 if (!check_inlines) 1782 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec); 1783 1784 if (file_idx != UINT32_MAX) 1785 { 1786 uint32_t found_line; 1787 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry); 1788 found_line = sc.line_entry.line; 1789 1790 while (line_idx != UINT32_MAX) 1791 { 1792 sc.function = NULL; 1793 sc.block = NULL; 1794 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) 1795 { 1796 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress(); 1797 if (file_vm_addr != LLDB_INVALID_ADDRESS) 1798 { 1799 DWARFDebugInfoEntry *function_die = NULL; 1800 DWARFDebugInfoEntry *block_die = NULL; 1801 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL); 1802 1803 if (function_die != NULL) 1804 { 1805 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get(); 1806 if (sc.function == NULL) 1807 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die); 1808 } 1809 1810 if (sc.function != NULL) 1811 { 1812 Block& block = sc.function->GetBlock (true); 1813 1814 if (block_die != NULL) 1815 sc.block = block.FindBlockByID (block_die->GetOffset()); 1816 else 1817 sc.block = block.FindBlockByID (function_die->GetOffset()); 1818 } 1819 } 1820 } 1821 1822 sc_list.Append(sc); 1823 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry); 1824 } 1825 } 1826 } 1827 else if (file_spec_matches_cu_file_spec && !check_inlines) 1828 { 1829 // only append the context if we aren't looking for inline call sites 1830 // by file and line and if the file spec matches that of the compile unit 1831 sc_list.Append(sc); 1832 } 1833 } 1834 else if (file_spec_matches_cu_file_spec && !check_inlines) 1835 { 1836 // only append the context if we aren't looking for inline call sites 1837 // by file and line and if the file spec matches that of the compile unit 1838 sc_list.Append(sc); 1839 } 1840 1841 if (!check_inlines) 1842 break; 1843 } 1844 } 1845 } 1846 } 1847 return sc_list.GetSize() - prev_size; 1848 } 1849 1850 void 1851 SymbolFileDWARF::Index () 1852 { 1853 if (m_indexed) 1854 return; 1855 m_indexed = true; 1856 Timer scoped_timer (__PRETTY_FUNCTION__, 1857 "SymbolFileDWARF::Index (%s)", 1858 GetObjectFile()->GetFileSpec().GetFilename().AsCString()); 1859 1860 DWARFDebugInfo* debug_info = DebugInfo(); 1861 if (debug_info) 1862 { 1863 m_aranges.reset(new DWARFDebugAranges()); 1864 1865 uint32_t cu_idx = 0; 1866 const uint32_t num_compile_units = GetNumCompileUnits(); 1867 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 1868 { 1869 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx); 1870 1871 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1; 1872 1873 curr_cu->Index (cu_idx, 1874 m_function_basename_index, 1875 m_function_fullname_index, 1876 m_function_method_index, 1877 m_function_selector_index, 1878 m_objc_class_selectors_index, 1879 m_global_index, 1880 m_type_index, 1881 m_namespace_index, 1882 DebugRanges(), 1883 m_aranges.get()); 1884 1885 // Keep memory down by clearing DIEs if this generate function 1886 // caused them to be parsed 1887 if (clear_dies) 1888 curr_cu->ClearDIEs (true); 1889 } 1890 1891 m_aranges->Sort(); 1892 1893 #if defined (ENABLE_DEBUG_PRINTF) 1894 StreamFile s(stdout, false); 1895 s.Printf ("DWARF index for (%s) '%s/%s':", 1896 GetObjectFile()->GetModule()->GetArchitecture().AsCString(), 1897 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(), 1898 GetObjectFile()->GetFileSpec().GetFilename().AsCString()); 1899 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s); 1900 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s); 1901 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s); 1902 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s); 1903 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s); 1904 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s); 1905 s.Printf("\nTypes:\n"); m_type_index.Dump (&s); 1906 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s); 1907 #endif 1908 } 1909 } 1910 1911 uint32_t 1912 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables) 1913 { 1914 DWARFDebugInfo* info = DebugInfo(); 1915 if (info == NULL) 1916 return 0; 1917 1918 // If we aren't appending the results to this list, then clear the list 1919 if (!append) 1920 variables.Clear(); 1921 1922 // Remember how many variables are in the list before we search in case 1923 // we are appending the results to a variable list. 1924 const uint32_t original_size = variables.GetSize(); 1925 1926 // Index the DWARF if we haven't already 1927 if (!m_indexed) 1928 Index (); 1929 1930 SymbolContext sc; 1931 sc.module_sp = m_obj_file->GetModule()->GetSP(); 1932 assert (sc.module_sp); 1933 1934 DWARFCompileUnit* curr_cu = NULL; 1935 DWARFCompileUnit* prev_cu = NULL; 1936 const DWARFDebugInfoEntry* die = NULL; 1937 std::vector<NameToDIE::Info> die_info_array; 1938 const size_t num_matches = m_global_index.Find(name, die_info_array); 1939 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 1940 { 1941 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 1942 1943 if (curr_cu != prev_cu) 1944 curr_cu->ExtractDIEsIfNeeded (false); 1945 1946 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 1947 1948 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX); 1949 assert(sc.comp_unit != NULL); 1950 1951 ParseVariables(sc, curr_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 1952 1953 if (variables.GetSize() - original_size >= max_matches) 1954 break; 1955 } 1956 1957 // Return the number of variable that were appended to the list 1958 return variables.GetSize() - original_size; 1959 } 1960 1961 uint32_t 1962 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables) 1963 { 1964 DWARFDebugInfo* info = DebugInfo(); 1965 if (info == NULL) 1966 return 0; 1967 1968 // If we aren't appending the results to this list, then clear the list 1969 if (!append) 1970 variables.Clear(); 1971 1972 // Remember how many variables are in the list before we search in case 1973 // we are appending the results to a variable list. 1974 const uint32_t original_size = variables.GetSize(); 1975 1976 // Index the DWARF if we haven't already 1977 if (!m_indexed) 1978 Index (); 1979 1980 SymbolContext sc; 1981 sc.module_sp = m_obj_file->GetModule()->GetSP(); 1982 assert (sc.module_sp); 1983 1984 DWARFCompileUnit* curr_cu = NULL; 1985 DWARFCompileUnit* prev_cu = NULL; 1986 const DWARFDebugInfoEntry* die = NULL; 1987 std::vector<NameToDIE::Info> die_info_array; 1988 const size_t num_matches = m_global_index.Find(regex, die_info_array); 1989 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 1990 { 1991 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 1992 1993 if (curr_cu != prev_cu) 1994 curr_cu->ExtractDIEsIfNeeded (false); 1995 1996 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 1997 1998 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX); 1999 assert(sc.comp_unit != NULL); 2000 2001 ParseVariables(sc, curr_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables); 2002 2003 if (variables.GetSize() - original_size >= max_matches) 2004 break; 2005 } 2006 2007 // Return the number of variable that were appended to the list 2008 return variables.GetSize() - original_size; 2009 } 2010 2011 2012 void 2013 SymbolFileDWARF::FindFunctions 2014 ( 2015 const ConstString &name, 2016 const NameToDIE &name_to_die, 2017 SymbolContextList& sc_list 2018 ) 2019 { 2020 DWARFDebugInfo* info = DebugInfo(); 2021 if (info == NULL) 2022 return; 2023 2024 SymbolContext sc; 2025 sc.module_sp = m_obj_file->GetModule()->GetSP(); 2026 assert (sc.module_sp); 2027 2028 DWARFCompileUnit* curr_cu = NULL; 2029 DWARFCompileUnit* prev_cu = NULL; 2030 const DWARFDebugInfoEntry* die = NULL; 2031 std::vector<NameToDIE::Info> die_info_array; 2032 const size_t num_matches = name_to_die.Find (name, die_info_array); 2033 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 2034 { 2035 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 2036 2037 if (curr_cu != prev_cu) 2038 curr_cu->ExtractDIEsIfNeeded (false); 2039 2040 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 2041 2042 const DWARFDebugInfoEntry* inlined_die = NULL; 2043 if (die->Tag() == DW_TAG_inlined_subroutine) 2044 { 2045 inlined_die = die; 2046 2047 while ((die = die->GetParent()) != NULL) 2048 { 2049 if (die->Tag() == DW_TAG_subprogram) 2050 break; 2051 } 2052 } 2053 assert (die->Tag() == DW_TAG_subprogram); 2054 if (GetFunction (curr_cu, die, sc)) 2055 { 2056 Address addr; 2057 // Parse all blocks if needed 2058 if (inlined_die) 2059 { 2060 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset()); 2061 assert (sc.block != NULL); 2062 if (sc.block->GetStartAddress (addr) == false) 2063 addr.Clear(); 2064 } 2065 else 2066 { 2067 sc.block = NULL; 2068 addr = sc.function->GetAddressRange().GetBaseAddress(); 2069 } 2070 2071 if (addr.IsValid()) 2072 { 2073 2074 // We found the function, so we should find the line table 2075 // and line table entry as well 2076 LineTable *line_table = sc.comp_unit->GetLineTable(); 2077 if (line_table == NULL) 2078 { 2079 if (ParseCompileUnitLineTable(sc)) 2080 line_table = sc.comp_unit->GetLineTable(); 2081 } 2082 if (line_table != NULL) 2083 line_table->FindLineEntryByAddress (addr, sc.line_entry); 2084 2085 sc_list.Append(sc); 2086 } 2087 } 2088 } 2089 } 2090 2091 2092 void 2093 SymbolFileDWARF::FindFunctions 2094 ( 2095 const RegularExpression ®ex, 2096 const NameToDIE &name_to_die, 2097 SymbolContextList& sc_list 2098 ) 2099 { 2100 DWARFDebugInfo* info = DebugInfo(); 2101 if (info == NULL) 2102 return; 2103 2104 SymbolContext sc; 2105 sc.module_sp = m_obj_file->GetModule()->GetSP(); 2106 assert (sc.module_sp); 2107 2108 DWARFCompileUnit* curr_cu = NULL; 2109 DWARFCompileUnit* prev_cu = NULL; 2110 const DWARFDebugInfoEntry* die = NULL; 2111 std::vector<NameToDIE::Info> die_info_array; 2112 const size_t num_matches = name_to_die.Find(regex, die_info_array); 2113 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 2114 { 2115 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 2116 2117 if (curr_cu != prev_cu) 2118 curr_cu->ExtractDIEsIfNeeded (false); 2119 2120 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 2121 2122 const DWARFDebugInfoEntry* inlined_die = NULL; 2123 if (die->Tag() == DW_TAG_inlined_subroutine) 2124 { 2125 inlined_die = die; 2126 2127 while ((die = die->GetParent()) != NULL) 2128 { 2129 if (die->Tag() == DW_TAG_subprogram) 2130 break; 2131 } 2132 } 2133 assert (die->Tag() == DW_TAG_subprogram); 2134 if (GetFunction (curr_cu, die, sc)) 2135 { 2136 Address addr; 2137 // Parse all blocks if needed 2138 if (inlined_die) 2139 { 2140 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset()); 2141 assert (sc.block != NULL); 2142 if (sc.block->GetStartAddress (addr) == false) 2143 addr.Clear(); 2144 } 2145 else 2146 { 2147 sc.block = NULL; 2148 addr = sc.function->GetAddressRange().GetBaseAddress(); 2149 } 2150 2151 if (addr.IsValid()) 2152 { 2153 2154 // We found the function, so we should find the line table 2155 // and line table entry as well 2156 LineTable *line_table = sc.comp_unit->GetLineTable(); 2157 if (line_table == NULL) 2158 { 2159 if (ParseCompileUnitLineTable(sc)) 2160 line_table = sc.comp_unit->GetLineTable(); 2161 } 2162 if (line_table != NULL) 2163 line_table->FindLineEntryByAddress (addr, sc.line_entry); 2164 2165 sc_list.Append(sc); 2166 } 2167 } 2168 } 2169 } 2170 2171 uint32_t 2172 SymbolFileDWARF::FindFunctions 2173 ( 2174 const ConstString &name, 2175 uint32_t name_type_mask, 2176 bool append, 2177 SymbolContextList& sc_list 2178 ) 2179 { 2180 Timer scoped_timer (__PRETTY_FUNCTION__, 2181 "SymbolFileDWARF::FindFunctions (name = '%s')", 2182 name.AsCString()); 2183 2184 // If we aren't appending the results to this list, then clear the list 2185 if (!append) 2186 sc_list.Clear(); 2187 2188 // Remember how many sc_list are in the list before we search in case 2189 // we are appending the results to a variable list. 2190 uint32_t original_size = sc_list.GetSize(); 2191 2192 // Index the DWARF if we haven't already 2193 if (!m_indexed) 2194 Index (); 2195 2196 if (name_type_mask & eFunctionNameTypeBase) 2197 FindFunctions (name, m_function_basename_index, sc_list); 2198 2199 if (name_type_mask & eFunctionNameTypeFull) 2200 FindFunctions (name, m_function_fullname_index, sc_list); 2201 2202 if (name_type_mask & eFunctionNameTypeMethod) 2203 FindFunctions (name, m_function_method_index, sc_list); 2204 2205 if (name_type_mask & eFunctionNameTypeSelector) 2206 FindFunctions (name, m_function_selector_index, sc_list); 2207 2208 // Return the number of variable that were appended to the list 2209 return sc_list.GetSize() - original_size; 2210 } 2211 2212 2213 uint32_t 2214 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list) 2215 { 2216 Timer scoped_timer (__PRETTY_FUNCTION__, 2217 "SymbolFileDWARF::FindFunctions (regex = '%s')", 2218 regex.GetText()); 2219 2220 // If we aren't appending the results to this list, then clear the list 2221 if (!append) 2222 sc_list.Clear(); 2223 2224 // Remember how many sc_list are in the list before we search in case 2225 // we are appending the results to a variable list. 2226 uint32_t original_size = sc_list.GetSize(); 2227 2228 // Index the DWARF if we haven't already 2229 if (!m_indexed) 2230 Index (); 2231 2232 FindFunctions (regex, m_function_basename_index, sc_list); 2233 2234 FindFunctions (regex, m_function_fullname_index, sc_list); 2235 2236 // Return the number of variable that were appended to the list 2237 return sc_list.GetSize() - original_size; 2238 } 2239 2240 uint32_t 2241 SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, bool append, uint32_t max_matches, TypeList& types) 2242 { 2243 DWARFDebugInfo* info = DebugInfo(); 2244 if (info == NULL) 2245 return 0; 2246 2247 // If we aren't appending the results to this list, then clear the list 2248 if (!append) 2249 types.Clear(); 2250 2251 // Index if we already haven't to make sure the compile units 2252 // get indexed and make their global DIE index list 2253 if (!m_indexed) 2254 Index (); 2255 2256 const uint32_t initial_types_size = types.GetSize(); 2257 DWARFCompileUnit* curr_cu = NULL; 2258 DWARFCompileUnit* prev_cu = NULL; 2259 const DWARFDebugInfoEntry* die = NULL; 2260 std::vector<NameToDIE::Info> die_info_array; 2261 const size_t num_matches = m_type_index.Find (name, die_info_array); 2262 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 2263 { 2264 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 2265 2266 if (curr_cu != prev_cu) 2267 curr_cu->ExtractDIEsIfNeeded (false); 2268 2269 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 2270 2271 Type *matching_type = ResolveType (curr_cu, die); 2272 if (matching_type) 2273 { 2274 // We found a type pointer, now find the shared pointer form our type list 2275 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID())); 2276 assert (type_sp.get() != NULL); 2277 types.InsertUnique (type_sp); 2278 if (types.GetSize() >= max_matches) 2279 break; 2280 } 2281 } 2282 return types.GetSize() - initial_types_size; 2283 } 2284 2285 2286 ClangNamespaceDecl 2287 SymbolFileDWARF::FindNamespace (const SymbolContext& sc, 2288 const ConstString &name) 2289 { 2290 ClangNamespaceDecl namespace_decl; 2291 DWARFDebugInfo* info = DebugInfo(); 2292 if (info) 2293 { 2294 // Index if we already haven't to make sure the compile units 2295 // get indexed and make their global DIE index list 2296 if (!m_indexed) 2297 Index (); 2298 2299 DWARFCompileUnit* curr_cu = NULL; 2300 DWARFCompileUnit* prev_cu = NULL; 2301 const DWARFDebugInfoEntry* die = NULL; 2302 std::vector<NameToDIE::Info> die_info_array; 2303 const size_t num_matches = m_namespace_index.Find (name, die_info_array); 2304 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu) 2305 { 2306 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx); 2307 2308 if (curr_cu != prev_cu) 2309 curr_cu->ExtractDIEsIfNeeded (false); 2310 2311 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx); 2312 2313 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (curr_cu, die); 2314 if (clang_namespace_decl) 2315 { 2316 namespace_decl.SetASTContext (GetClangASTContext().getASTContext()); 2317 namespace_decl.SetNamespaceDecl (clang_namespace_decl); 2318 } 2319 } 2320 } 2321 return namespace_decl; 2322 } 2323 2324 uint32_t 2325 SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types) 2326 { 2327 // Remember how many sc_list are in the list before we search in case 2328 // we are appending the results to a variable list. 2329 uint32_t original_size = types.GetSize(); 2330 2331 const uint32_t num_die_offsets = die_offsets.size(); 2332 // Parse all of the types we found from the pubtypes matches 2333 uint32_t i; 2334 uint32_t num_matches = 0; 2335 for (i = 0; i < num_die_offsets; ++i) 2336 { 2337 Type *matching_type = ResolveTypeUID (die_offsets[i]); 2338 if (matching_type) 2339 { 2340 // We found a type pointer, now find the shared pointer form our type list 2341 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID())); 2342 assert (type_sp.get() != NULL); 2343 types.InsertUnique (type_sp); 2344 ++num_matches; 2345 if (num_matches >= max_matches) 2346 break; 2347 } 2348 } 2349 2350 // Return the number of variable that were appended to the list 2351 return types.GetSize() - original_size; 2352 } 2353 2354 2355 size_t 2356 SymbolFileDWARF::ParseChildParameters 2357 ( 2358 const SymbolContext& sc, 2359 TypeSP& type_sp, 2360 DWARFCompileUnit* dwarf_cu, 2361 const DWARFDebugInfoEntry *parent_die, 2362 bool skip_artificial, 2363 TypeList* type_list, 2364 std::vector<clang_type_t>& function_param_types, 2365 std::vector<clang::ParmVarDecl*>& function_param_decls, 2366 unsigned &type_quals 2367 ) 2368 { 2369 if (parent_die == NULL) 2370 return 0; 2371 2372 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 2373 2374 size_t arg_idx = 0; 2375 const DWARFDebugInfoEntry *die; 2376 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 2377 { 2378 dw_tag_t tag = die->Tag(); 2379 switch (tag) 2380 { 2381 case DW_TAG_formal_parameter: 2382 { 2383 DWARFDebugInfoEntry::Attributes attributes; 2384 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 2385 if (num_attributes > 0) 2386 { 2387 const char *name = NULL; 2388 Declaration decl; 2389 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET; 2390 bool is_artificial = false; 2391 // one of None, Auto, Register, Extern, Static, PrivateExtern 2392 2393 clang::StorageClass storage = clang::SC_None; 2394 uint32_t i; 2395 for (i=0; i<num_attributes; ++i) 2396 { 2397 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2398 DWARFFormValue form_value; 2399 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 2400 { 2401 switch (attr) 2402 { 2403 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2404 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2405 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2406 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 2407 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break; 2408 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 2409 case DW_AT_location: 2410 // if (form_value.BlockData()) 2411 // { 2412 // const DataExtractor& debug_info_data = debug_info(); 2413 // uint32_t block_length = form_value.Unsigned(); 2414 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length); 2415 // } 2416 // else 2417 // { 2418 // } 2419 // break; 2420 case DW_AT_const_value: 2421 case DW_AT_default_value: 2422 case DW_AT_description: 2423 case DW_AT_endianity: 2424 case DW_AT_is_optional: 2425 case DW_AT_segment: 2426 case DW_AT_variable_parameter: 2427 default: 2428 case DW_AT_abstract_origin: 2429 case DW_AT_sibling: 2430 break; 2431 } 2432 } 2433 } 2434 2435 bool skip = false; 2436 if (skip_artificial) 2437 { 2438 if (is_artificial) 2439 { 2440 // In order to determine if a C++ member function is 2441 // "const" we have to look at the const-ness of "this"... 2442 // Ugly, but that 2443 if (arg_idx == 0) 2444 { 2445 const DWARFDebugInfoEntry *grandparent_die = parent_die->GetParent(); 2446 if (grandparent_die && (grandparent_die->Tag() == DW_TAG_structure_type || 2447 grandparent_die->Tag() == DW_TAG_class_type)) 2448 { 2449 LanguageType language = sc.comp_unit->GetLanguage(); 2450 if (language == eLanguageTypeObjC_plus_plus || language == eLanguageTypeC_plus_plus) 2451 { 2452 // Often times compilers omit the "this" name for the 2453 // specification DIEs, so we can't rely upon the name 2454 // being in the formal parameter DIE... 2455 if (name == NULL || ::strcmp(name, "this")==0) 2456 { 2457 Type *this_type = ResolveTypeUID (param_type_die_offset); 2458 if (this_type) 2459 { 2460 uint32_t encoding_mask = this_type->GetEncodingMask(); 2461 if (encoding_mask & Type::eEncodingIsPointerUID) 2462 { 2463 if (encoding_mask & (1u << Type::eEncodingIsConstUID)) 2464 type_quals |= clang::Qualifiers::Const; 2465 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID)) 2466 type_quals |= clang::Qualifiers::Volatile; 2467 } 2468 } 2469 } 2470 } 2471 } 2472 } 2473 skip = true; 2474 } 2475 else 2476 { 2477 2478 // HACK: Objective C formal parameters "self" and "_cmd" 2479 // are not marked as artificial in the DWARF... 2480 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX); 2481 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus)) 2482 { 2483 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0)) 2484 skip = true; 2485 } 2486 } 2487 } 2488 2489 if (!skip) 2490 { 2491 Type *type = ResolveTypeUID(param_type_die_offset); 2492 if (type) 2493 { 2494 function_param_types.push_back (type->GetClangForwardType()); 2495 2496 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage); 2497 assert(param_var_decl); 2498 function_param_decls.push_back(param_var_decl); 2499 } 2500 } 2501 } 2502 arg_idx++; 2503 } 2504 break; 2505 2506 default: 2507 break; 2508 } 2509 } 2510 return arg_idx; 2511 } 2512 2513 size_t 2514 SymbolFileDWARF::ParseChildEnumerators 2515 ( 2516 const SymbolContext& sc, 2517 clang_type_t enumerator_clang_type, 2518 uint32_t enumerator_byte_size, 2519 DWARFCompileUnit* dwarf_cu, 2520 const DWARFDebugInfoEntry *parent_die 2521 ) 2522 { 2523 if (parent_die == NULL) 2524 return 0; 2525 2526 size_t enumerators_added = 0; 2527 const DWARFDebugInfoEntry *die; 2528 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 2529 2530 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 2531 { 2532 const dw_tag_t tag = die->Tag(); 2533 if (tag == DW_TAG_enumerator) 2534 { 2535 DWARFDebugInfoEntry::Attributes attributes; 2536 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 2537 if (num_child_attributes > 0) 2538 { 2539 const char *name = NULL; 2540 bool got_value = false; 2541 int64_t enum_value = 0; 2542 Declaration decl; 2543 2544 uint32_t i; 2545 for (i=0; i<num_child_attributes; ++i) 2546 { 2547 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2548 DWARFFormValue form_value; 2549 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 2550 { 2551 switch (attr) 2552 { 2553 case DW_AT_const_value: 2554 got_value = true; 2555 enum_value = form_value.Unsigned(); 2556 break; 2557 2558 case DW_AT_name: 2559 name = form_value.AsCString(&get_debug_str_data()); 2560 break; 2561 2562 case DW_AT_description: 2563 default: 2564 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2565 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2566 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2567 case DW_AT_sibling: 2568 break; 2569 } 2570 } 2571 } 2572 2573 if (name && name[0] && got_value) 2574 { 2575 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type, 2576 enumerator_clang_type, 2577 decl, 2578 name, 2579 enum_value, 2580 enumerator_byte_size * 8); 2581 ++enumerators_added; 2582 } 2583 } 2584 } 2585 } 2586 return enumerators_added; 2587 } 2588 2589 void 2590 SymbolFileDWARF::ParseChildArrayInfo 2591 ( 2592 const SymbolContext& sc, 2593 DWARFCompileUnit* dwarf_cu, 2594 const DWARFDebugInfoEntry *parent_die, 2595 int64_t& first_index, 2596 std::vector<uint64_t>& element_orders, 2597 uint32_t& byte_stride, 2598 uint32_t& bit_stride 2599 ) 2600 { 2601 if (parent_die == NULL) 2602 return; 2603 2604 const DWARFDebugInfoEntry *die; 2605 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize()); 2606 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling()) 2607 { 2608 const dw_tag_t tag = die->Tag(); 2609 switch (tag) 2610 { 2611 case DW_TAG_enumerator: 2612 { 2613 DWARFDebugInfoEntry::Attributes attributes; 2614 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 2615 if (num_child_attributes > 0) 2616 { 2617 const char *name = NULL; 2618 bool got_value = false; 2619 int64_t enum_value = 0; 2620 2621 uint32_t i; 2622 for (i=0; i<num_child_attributes; ++i) 2623 { 2624 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2625 DWARFFormValue form_value; 2626 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 2627 { 2628 switch (attr) 2629 { 2630 case DW_AT_const_value: 2631 got_value = true; 2632 enum_value = form_value.Unsigned(); 2633 break; 2634 2635 case DW_AT_name: 2636 name = form_value.AsCString(&get_debug_str_data()); 2637 break; 2638 2639 case DW_AT_description: 2640 default: 2641 case DW_AT_decl_file: 2642 case DW_AT_decl_line: 2643 case DW_AT_decl_column: 2644 case DW_AT_sibling: 2645 break; 2646 } 2647 } 2648 } 2649 } 2650 } 2651 break; 2652 2653 case DW_TAG_subrange_type: 2654 { 2655 DWARFDebugInfoEntry::Attributes attributes; 2656 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes); 2657 if (num_child_attributes > 0) 2658 { 2659 const char *name = NULL; 2660 bool got_value = false; 2661 uint64_t byte_size = 0; 2662 int64_t enum_value = 0; 2663 uint64_t num_elements = 0; 2664 uint64_t lower_bound = 0; 2665 uint64_t upper_bound = 0; 2666 uint32_t i; 2667 for (i=0; i<num_child_attributes; ++i) 2668 { 2669 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2670 DWARFFormValue form_value; 2671 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 2672 { 2673 switch (attr) 2674 { 2675 case DW_AT_const_value: 2676 got_value = true; 2677 enum_value = form_value.Unsigned(); 2678 break; 2679 2680 case DW_AT_name: 2681 name = form_value.AsCString(&get_debug_str_data()); 2682 break; 2683 2684 case DW_AT_count: 2685 num_elements = form_value.Unsigned(); 2686 break; 2687 2688 case DW_AT_bit_stride: 2689 bit_stride = form_value.Unsigned(); 2690 break; 2691 2692 case DW_AT_byte_stride: 2693 byte_stride = form_value.Unsigned(); 2694 break; 2695 2696 case DW_AT_byte_size: 2697 byte_size = form_value.Unsigned(); 2698 break; 2699 2700 case DW_AT_lower_bound: 2701 lower_bound = form_value.Unsigned(); 2702 break; 2703 2704 case DW_AT_upper_bound: 2705 upper_bound = form_value.Unsigned(); 2706 break; 2707 2708 default: 2709 case DW_AT_abstract_origin: 2710 case DW_AT_accessibility: 2711 case DW_AT_allocated: 2712 case DW_AT_associated: 2713 case DW_AT_data_location: 2714 case DW_AT_declaration: 2715 case DW_AT_description: 2716 case DW_AT_sibling: 2717 case DW_AT_threads_scaled: 2718 case DW_AT_type: 2719 case DW_AT_visibility: 2720 break; 2721 } 2722 } 2723 } 2724 2725 if (upper_bound > lower_bound) 2726 num_elements = upper_bound - lower_bound + 1; 2727 2728 if (num_elements > 0) 2729 element_orders.push_back (num_elements); 2730 } 2731 } 2732 break; 2733 } 2734 } 2735 } 2736 2737 TypeSP 2738 SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die) 2739 { 2740 TypeSP type_sp; 2741 if (die != NULL) 2742 { 2743 assert(curr_cu != NULL); 2744 Type *type_ptr = m_die_to_type.lookup (die); 2745 if (type_ptr == NULL) 2746 { 2747 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu); 2748 assert (lldb_cu); 2749 SymbolContext sc(lldb_cu); 2750 type_sp = ParseType(sc, curr_cu, die, NULL); 2751 } 2752 else if (type_ptr != DIE_IS_BEING_PARSED) 2753 { 2754 // Grab the existing type from the master types lists 2755 type_sp = GetTypeList()->FindType(type_ptr->GetID()); 2756 } 2757 2758 } 2759 return type_sp; 2760 } 2761 2762 clang::DeclContext * 2763 SymbolFileDWARF::GetClangDeclContextForDIEOffset (dw_offset_t die_offset) 2764 { 2765 if (die_offset != DW_INVALID_OFFSET) 2766 { 2767 DWARFCompileUnitSP cu_sp; 2768 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp); 2769 return GetClangDeclContextForDIE (cu_sp.get(), die); 2770 } 2771 return NULL; 2772 } 2773 2774 2775 clang::NamespaceDecl * 2776 SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die) 2777 { 2778 if (die->Tag() == DW_TAG_namespace) 2779 { 2780 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL); 2781 if (namespace_name) 2782 { 2783 Declaration decl; // TODO: fill in the decl object 2784 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextForDIE (curr_cu, die->GetParent())); 2785 if (namespace_decl) 2786 m_die_to_decl_ctx[die] = (clang::DeclContext*)namespace_decl; 2787 return namespace_decl; 2788 } 2789 } 2790 return NULL; 2791 } 2792 2793 clang::DeclContext * 2794 SymbolFileDWARF::GetClangDeclContextForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die) 2795 { 2796 if (m_clang_tu_decl == NULL) 2797 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl(); 2798 2799 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x )\n", die->GetOffset()); 2800 const DWARFDebugInfoEntry * const decl_die = die; 2801 clang::DeclContext *decl_ctx = NULL; 2802 2803 while (die != NULL) 2804 { 2805 // If this is the original DIE that we are searching for a declaration 2806 // for, then don't look in the cache as we don't want our own decl 2807 // context to be our decl context... 2808 if (decl_die != die) 2809 { 2810 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die); 2811 if (pos != m_die_to_decl_ctx.end()) 2812 { 2813 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset()); 2814 return pos->second; 2815 } 2816 2817 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) checking parent 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset()); 2818 2819 switch (die->Tag()) 2820 { 2821 case DW_TAG_namespace: 2822 { 2823 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL); 2824 if (namespace_name) 2825 { 2826 Declaration decl; // TODO: fill in the decl object 2827 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextForDIE (curr_cu, die)); 2828 if (namespace_decl) 2829 { 2830 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset()); 2831 m_die_to_decl_ctx[die] = (clang::DeclContext*)namespace_decl; 2832 } 2833 return namespace_decl; 2834 } 2835 } 2836 break; 2837 2838 case DW_TAG_structure_type: 2839 case DW_TAG_union_type: 2840 case DW_TAG_class_type: 2841 { 2842 Type* type = ResolveType (curr_cu, die); 2843 pos = m_die_to_decl_ctx.find(die); 2844 if (pos != m_die_to_decl_ctx.end()) 2845 { 2846 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset()); 2847 return pos->second; 2848 } 2849 else 2850 { 2851 if (type) 2852 { 2853 decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ()); 2854 if (decl_ctx) 2855 return decl_ctx; 2856 } 2857 } 2858 } 2859 break; 2860 2861 default: 2862 break; 2863 } 2864 } 2865 2866 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET); 2867 if (die_offset != DW_INVALID_OFFSET) 2868 { 2869 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) check DW_AT_specification 0x%8.8x\n", decl_die->GetOffset(), die_offset); 2870 decl_ctx = GetClangDeclContextForDIEOffset (die_offset); 2871 if (decl_ctx != m_clang_tu_decl) 2872 return decl_ctx; 2873 } 2874 2875 die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET); 2876 if (die_offset != DW_INVALID_OFFSET) 2877 { 2878 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) check DW_AT_abstract_origin 0x%8.8x\n", decl_die->GetOffset(), die_offset); 2879 decl_ctx = GetClangDeclContextForDIEOffset (die_offset); 2880 if (decl_ctx != m_clang_tu_decl) 2881 return decl_ctx; 2882 } 2883 2884 die = die->GetParent(); 2885 } 2886 // Right now we have only one translation unit per module... 2887 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), curr_cu->GetFirstDIEOffset()); 2888 return m_clang_tu_decl; 2889 } 2890 2891 // This function can be used when a DIE is found that is a forward declaration 2892 // DIE and we want to try and find a type that has the complete definition. 2893 TypeSP 2894 SymbolFileDWARF::FindDefinitionTypeForDIE ( 2895 DWARFCompileUnit* cu, 2896 const DWARFDebugInfoEntry *die, 2897 const ConstString &type_name 2898 ) 2899 { 2900 TypeSP type_sp; 2901 2902 if (cu == NULL || die == NULL || !type_name) 2903 return type_sp; 2904 2905 if (!m_indexed) 2906 Index (); 2907 2908 const dw_tag_t type_tag = die->Tag(); 2909 std::vector<NameToDIE::Info> die_info_array; 2910 const size_t num_matches = m_type_index.Find (type_name, die_info_array); 2911 if (num_matches > 0) 2912 { 2913 DWARFCompileUnit* type_cu = NULL; 2914 DWARFCompileUnit* curr_cu = cu; 2915 DWARFDebugInfo *info = DebugInfo(); 2916 for (size_t i=0; i<num_matches; ++i) 2917 { 2918 type_cu = info->GetCompileUnitAtIndex (die_info_array[i].cu_idx); 2919 2920 if (type_cu != curr_cu) 2921 { 2922 type_cu->ExtractDIEsIfNeeded (false); 2923 curr_cu = type_cu; 2924 } 2925 2926 DWARFDebugInfoEntry *type_die = type_cu->GetDIEAtIndexUnchecked (die_info_array[i].die_idx); 2927 2928 if (type_die != die && type_die->Tag() == type_tag) 2929 { 2930 // Hold off on comparing parent DIE tags until 2931 // we know what happens with stuff in namespaces 2932 // for gcc and clang... 2933 //DWARFDebugInfoEntry *parent_die = die->GetParent(); 2934 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent(); 2935 //if (parent_die->Tag() == parent_type_die->Tag()) 2936 { 2937 Type *resolved_type = ResolveType (type_cu, type_die, false); 2938 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) 2939 { 2940 DEBUG_PRINTF ("resolved 0x%8.8x (cu 0x%8.8x) from %s to 0x%8.8x (cu 0x%8.8x)\n", 2941 die->GetOffset(), 2942 curr_cu->GetOffset(), 2943 m_obj_file->GetFileSpec().GetFilename().AsCString(), 2944 type_die->GetOffset(), 2945 type_cu->GetOffset()); 2946 2947 m_die_to_type[die] = resolved_type; 2948 type_sp = GetTypeList()->FindType(resolved_type->GetID()); 2949 if (!type_sp) 2950 { 2951 DEBUG_PRINTF("unable to resolve type '%s' from DIE 0x%8.8x\n", type_name.GetCString(), die->GetOffset()); 2952 } 2953 break; 2954 } 2955 } 2956 } 2957 } 2958 } 2959 return type_sp; 2960 } 2961 2962 TypeSP 2963 SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr) 2964 { 2965 TypeSP type_sp; 2966 2967 if (type_is_new_ptr) 2968 *type_is_new_ptr = false; 2969 2970 AccessType accessibility = eAccessNone; 2971 if (die != NULL) 2972 { 2973 Type *type_ptr = m_die_to_type.lookup (die); 2974 TypeList* type_list = GetTypeList(); 2975 if (type_ptr == NULL) 2976 { 2977 ClangASTContext &ast = GetClangASTContext(); 2978 if (type_is_new_ptr) 2979 *type_is_new_ptr = true; 2980 2981 const dw_tag_t tag = die->Tag(); 2982 2983 bool is_forward_declaration = false; 2984 DWARFDebugInfoEntry::Attributes attributes; 2985 const char *type_name_cstr = NULL; 2986 ConstString type_name_const_str; 2987 Type::ResolveState resolve_state = Type::eResolveStateUnresolved; 2988 size_t byte_size = 0; 2989 bool byte_size_valid = false; 2990 Declaration decl; 2991 2992 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 2993 clang_type_t clang_type = NULL; 2994 2995 dw_attr_t attr; 2996 2997 switch (tag) 2998 { 2999 case DW_TAG_base_type: 3000 case DW_TAG_pointer_type: 3001 case DW_TAG_reference_type: 3002 case DW_TAG_typedef: 3003 case DW_TAG_const_type: 3004 case DW_TAG_restrict_type: 3005 case DW_TAG_volatile_type: 3006 { 3007 // Set a bit that lets us know that we are currently parsing this 3008 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3009 3010 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3011 uint32_t encoding = 0; 3012 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 3013 3014 if (num_attributes > 0) 3015 { 3016 uint32_t i; 3017 for (i=0; i<num_attributes; ++i) 3018 { 3019 attr = attributes.AttributeAtIndex(i); 3020 DWARFFormValue form_value; 3021 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3022 { 3023 switch (attr) 3024 { 3025 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3026 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3027 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3028 case DW_AT_name: 3029 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3030 type_name_const_str.SetCString(type_name_cstr); 3031 break; 3032 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 3033 case DW_AT_encoding: encoding = form_value.Unsigned(); break; 3034 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 3035 default: 3036 case DW_AT_sibling: 3037 break; 3038 } 3039 } 3040 } 3041 } 3042 3043 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") type => 0x%8.8x\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid); 3044 3045 switch (tag) 3046 { 3047 default: 3048 break; 3049 3050 case DW_TAG_base_type: 3051 resolve_state = Type::eResolveStateFull; 3052 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr, 3053 encoding, 3054 byte_size * 8); 3055 break; 3056 3057 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break; 3058 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break; 3059 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break; 3060 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break; 3061 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break; 3062 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break; 3063 } 3064 3065 if (type_name_cstr != NULL && sc.comp_unit != NULL && 3066 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus)) 3067 { 3068 static ConstString g_objc_type_name_id("id"); 3069 static ConstString g_objc_type_name_Class("Class"); 3070 static ConstString g_objc_type_name_selector("SEL"); 3071 3072 if (type_name_const_str == g_objc_type_name_id) 3073 { 3074 clang_type = ast.GetBuiltInType_objc_id(); 3075 resolve_state = Type::eResolveStateFull; 3076 3077 } 3078 else if (type_name_const_str == g_objc_type_name_Class) 3079 { 3080 clang_type = ast.GetBuiltInType_objc_Class(); 3081 resolve_state = Type::eResolveStateFull; 3082 } 3083 else if (type_name_const_str == g_objc_type_name_selector) 3084 { 3085 clang_type = ast.GetBuiltInType_objc_selector(); 3086 resolve_state = Type::eResolveStateFull; 3087 } 3088 } 3089 3090 type_sp.reset( new Type (die->GetOffset(), 3091 this, 3092 type_name_const_str, 3093 byte_size, 3094 NULL, 3095 encoding_uid, 3096 encoding_data_type, 3097 &decl, 3098 clang_type, 3099 resolve_state)); 3100 3101 m_die_to_type[die] = type_sp.get(); 3102 3103 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false); 3104 // if (encoding_type != NULL) 3105 // { 3106 // if (encoding_type != DIE_IS_BEING_PARSED) 3107 // type_sp->SetEncodingType(encoding_type); 3108 // else 3109 // m_indirect_fixups.push_back(type_sp.get()); 3110 // } 3111 } 3112 break; 3113 3114 case DW_TAG_structure_type: 3115 case DW_TAG_union_type: 3116 case DW_TAG_class_type: 3117 { 3118 // Set a bit that lets us know that we are currently parsing this 3119 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3120 3121 LanguageType class_language = eLanguageTypeUnknown; 3122 //bool struct_is_class = false; 3123 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3124 if (num_attributes > 0) 3125 { 3126 uint32_t i; 3127 for (i=0; i<num_attributes; ++i) 3128 { 3129 attr = attributes.AttributeAtIndex(i); 3130 DWARFFormValue form_value; 3131 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3132 { 3133 switch (attr) 3134 { 3135 case DW_AT_decl_file: 3136 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); 3137 break; 3138 3139 case DW_AT_decl_line: 3140 decl.SetLine(form_value.Unsigned()); 3141 break; 3142 3143 case DW_AT_decl_column: 3144 decl.SetColumn(form_value.Unsigned()); 3145 break; 3146 3147 case DW_AT_name: 3148 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3149 type_name_const_str.SetCString(type_name_cstr); 3150 break; 3151 3152 case DW_AT_byte_size: 3153 byte_size = form_value.Unsigned(); 3154 byte_size_valid = true; 3155 break; 3156 3157 case DW_AT_accessibility: 3158 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 3159 break; 3160 3161 case DW_AT_declaration: 3162 is_forward_declaration = form_value.Unsigned() != 0; 3163 break; 3164 3165 case DW_AT_APPLE_runtime_class: 3166 class_language = (LanguageType)form_value.Signed(); 3167 break; 3168 3169 case DW_AT_allocated: 3170 case DW_AT_associated: 3171 case DW_AT_data_location: 3172 case DW_AT_description: 3173 case DW_AT_start_scope: 3174 case DW_AT_visibility: 3175 default: 3176 case DW_AT_sibling: 3177 break; 3178 } 3179 } 3180 } 3181 } 3182 3183 UniqueDWARFASTType unique_ast_entry; 3184 if (decl.IsValid()) 3185 { 3186 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str, 3187 this, 3188 dwarf_cu, 3189 die, 3190 decl, 3191 byte_size_valid ? byte_size : -1, 3192 unique_ast_entry)) 3193 { 3194 // We have already parsed this type or from another 3195 // compile unit. GCC loves to use the "one definition 3196 // rule" which can result in multiple definitions 3197 // of the same class over and over in each compile 3198 // unit. 3199 type_sp = unique_ast_entry.m_type_sp; 3200 if (type_sp) 3201 { 3202 m_die_to_type[die] = type_sp.get(); 3203 return type_sp; 3204 } 3205 } 3206 } 3207 3208 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr); 3209 3210 int tag_decl_kind = -1; 3211 AccessType default_accessibility = eAccessNone; 3212 if (tag == DW_TAG_structure_type) 3213 { 3214 tag_decl_kind = clang::TTK_Struct; 3215 default_accessibility = eAccessPublic; 3216 } 3217 else if (tag == DW_TAG_union_type) 3218 { 3219 tag_decl_kind = clang::TTK_Union; 3220 default_accessibility = eAccessPublic; 3221 } 3222 else if (tag == DW_TAG_class_type) 3223 { 3224 tag_decl_kind = clang::TTK_Class; 3225 default_accessibility = eAccessPrivate; 3226 } 3227 3228 3229 if (is_forward_declaration) 3230 { 3231 // We have a forward declaration to a type and we need 3232 // to try and find a full declaration. We look in the 3233 // current type index just in case we have a forward 3234 // declaration followed by an actual declarations in the 3235 // DWARF. If this fails, we need to look elsewhere... 3236 3237 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 3238 3239 if (!type_sp && m_debug_map_symfile) 3240 { 3241 // We weren't able to find a full declaration in 3242 // this DWARF, see if we have a declaration anywhere 3243 // else... 3244 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 3245 } 3246 3247 if (type_sp) 3248 { 3249 // We found a real definition for this type elsewhere 3250 // so lets use it and cache the fact that we found 3251 // a complete type for this die 3252 m_die_to_type[die] = type_sp.get(); 3253 return type_sp; 3254 } 3255 } 3256 assert (tag_decl_kind != -1); 3257 bool clang_type_was_created = false; 3258 clang_type = m_forward_decl_die_to_clang_type.lookup (die); 3259 if (clang_type == NULL) 3260 { 3261 clang_type_was_created = true; 3262 clang_type = ast.CreateRecordType (type_name_cstr, 3263 tag_decl_kind, 3264 GetClangDeclContextForDIE (dwarf_cu, die), 3265 class_language); 3266 } 3267 3268 // Store a forward declaration to this class type in case any 3269 // parameters in any class methods need it for the clang 3270 // types for function prototypes. 3271 m_die_to_decl_ctx[die] = ClangASTContext::GetDeclContextForType (clang_type); 3272 type_sp.reset (new Type (die->GetOffset(), 3273 this, 3274 type_name_const_str, 3275 byte_size, 3276 NULL, 3277 LLDB_INVALID_UID, 3278 Type::eEncodingIsUID, 3279 &decl, 3280 clang_type, 3281 Type::eResolveStateForward)); 3282 3283 3284 // Add our type to the unique type map so we don't 3285 // end up creating many copies of the same type over 3286 // and over in the ASTContext for our module 3287 unique_ast_entry.m_type_sp = type_sp; 3288 unique_ast_entry.m_symfile = this; 3289 unique_ast_entry.m_cu = dwarf_cu; 3290 unique_ast_entry.m_die = die; 3291 unique_ast_entry.m_declaration = decl; 3292 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str, 3293 unique_ast_entry); 3294 3295 if (die->HasChildren() == false && is_forward_declaration == false) 3296 { 3297 // No children for this struct/union/class, lets finish it 3298 ast.StartTagDeclarationDefinition (clang_type); 3299 ast.CompleteTagDeclarationDefinition (clang_type); 3300 } 3301 else if (clang_type_was_created) 3302 { 3303 // Leave this as a forward declaration until we need 3304 // to know the details of the type. lldb_private::Type 3305 // will automatically call the SymbolFile virtual function 3306 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)" 3307 // When the definition needs to be defined. 3308 m_forward_decl_die_to_clang_type[die] = clang_type; 3309 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die; 3310 ClangASTContext::SetHasExternalStorage (clang_type, true); 3311 } 3312 } 3313 break; 3314 3315 case DW_TAG_enumeration_type: 3316 { 3317 // Set a bit that lets us know that we are currently parsing this 3318 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3319 3320 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET; 3321 3322 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3323 if (num_attributes > 0) 3324 { 3325 uint32_t i; 3326 3327 for (i=0; i<num_attributes; ++i) 3328 { 3329 attr = attributes.AttributeAtIndex(i); 3330 DWARFFormValue form_value; 3331 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3332 { 3333 switch (attr) 3334 { 3335 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3336 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3337 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3338 case DW_AT_name: 3339 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3340 type_name_const_str.SetCString(type_name_cstr); 3341 break; 3342 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break; 3343 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 3344 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 3345 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 3346 case DW_AT_allocated: 3347 case DW_AT_associated: 3348 case DW_AT_bit_stride: 3349 case DW_AT_byte_stride: 3350 case DW_AT_data_location: 3351 case DW_AT_description: 3352 case DW_AT_start_scope: 3353 case DW_AT_visibility: 3354 case DW_AT_specification: 3355 case DW_AT_abstract_origin: 3356 case DW_AT_sibling: 3357 break; 3358 } 3359 } 3360 } 3361 3362 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr); 3363 3364 clang_type_t enumerator_clang_type = NULL; 3365 clang_type = m_forward_decl_die_to_clang_type.lookup (die); 3366 if (clang_type == NULL) 3367 { 3368 if (die->GetOffset() == 0x1c436) 3369 printf("REMOVE THIS!!!\n"); 3370 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL, 3371 DW_ATE_signed, 3372 byte_size * 8); 3373 clang_type = ast.CreateEnumerationType (type_name_cstr, 3374 GetClangDeclContextForDIE (dwarf_cu, die), 3375 decl, 3376 enumerator_clang_type); 3377 } 3378 else 3379 { 3380 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type); 3381 assert (enumerator_clang_type != NULL); 3382 } 3383 3384 m_die_to_decl_ctx[die] = ClangASTContext::GetDeclContextForType (clang_type); 3385 type_sp.reset( new Type (die->GetOffset(), 3386 this, 3387 type_name_const_str, 3388 byte_size, 3389 NULL, 3390 encoding_uid, 3391 Type::eEncodingIsUID, 3392 &decl, 3393 clang_type, 3394 Type::eResolveStateForward)); 3395 3396 #if LEAVE_ENUMS_FORWARD_DECLARED 3397 // Leave this as a forward declaration until we need 3398 // to know the details of the type. lldb_private::Type 3399 // will automatically call the SymbolFile virtual function 3400 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)" 3401 // When the definition needs to be defined. 3402 m_forward_decl_die_to_clang_type[die] = clang_type; 3403 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die; 3404 ClangASTContext::SetHasExternalStorage (clang_type, true); 3405 #else 3406 ast.StartTagDeclarationDefinition (clang_type); 3407 if (die->HasChildren()) 3408 { 3409 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu)); 3410 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die); 3411 } 3412 ast.CompleteTagDeclarationDefinition (clang_type); 3413 #endif 3414 } 3415 } 3416 break; 3417 3418 case DW_TAG_inlined_subroutine: 3419 case DW_TAG_subprogram: 3420 case DW_TAG_subroutine_type: 3421 { 3422 // Set a bit that lets us know that we are currently parsing this 3423 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3424 3425 const char *mangled = NULL; 3426 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 3427 bool is_variadic = false; 3428 bool is_inline = false; 3429 bool is_static = false; 3430 bool is_virtual = false; 3431 bool is_explicit = false; 3432 3433 unsigned type_quals = 0; 3434 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern 3435 3436 3437 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3438 if (num_attributes > 0) 3439 { 3440 uint32_t i; 3441 for (i=0; i<num_attributes; ++i) 3442 { 3443 attr = attributes.AttributeAtIndex(i); 3444 DWARFFormValue form_value; 3445 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3446 { 3447 switch (attr) 3448 { 3449 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3450 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3451 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3452 case DW_AT_name: 3453 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3454 type_name_const_str.SetCString(type_name_cstr); 3455 break; 3456 3457 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break; 3458 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break; 3459 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 3460 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 3461 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break; 3462 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break; 3463 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break; 3464 3465 case DW_AT_external: 3466 if (form_value.Unsigned()) 3467 { 3468 if (storage == clang::SC_None) 3469 storage = clang::SC_Extern; 3470 else 3471 storage = clang::SC_PrivateExtern; 3472 } 3473 break; 3474 3475 case DW_AT_allocated: 3476 case DW_AT_associated: 3477 case DW_AT_address_class: 3478 case DW_AT_artificial: 3479 case DW_AT_calling_convention: 3480 case DW_AT_data_location: 3481 case DW_AT_elemental: 3482 case DW_AT_entry_pc: 3483 case DW_AT_frame_base: 3484 case DW_AT_high_pc: 3485 case DW_AT_low_pc: 3486 case DW_AT_object_pointer: 3487 case DW_AT_prototyped: 3488 case DW_AT_pure: 3489 case DW_AT_ranges: 3490 case DW_AT_recursive: 3491 case DW_AT_return_addr: 3492 case DW_AT_segment: 3493 case DW_AT_specification: 3494 case DW_AT_start_scope: 3495 case DW_AT_static_link: 3496 case DW_AT_trampoline: 3497 case DW_AT_visibility: 3498 case DW_AT_vtable_elem_location: 3499 case DW_AT_abstract_origin: 3500 case DW_AT_description: 3501 case DW_AT_sibling: 3502 break; 3503 } 3504 } 3505 } 3506 } 3507 3508 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr); 3509 3510 clang_type_t return_clang_type = NULL; 3511 Type *func_type = NULL; 3512 3513 if (type_die_offset != DW_INVALID_OFFSET) 3514 func_type = ResolveTypeUID(type_die_offset); 3515 3516 if (func_type) 3517 return_clang_type = func_type->GetClangLayoutType(); 3518 else 3519 return_clang_type = ast.GetBuiltInType_void(); 3520 3521 3522 std::vector<clang_type_t> function_param_types; 3523 std::vector<clang::ParmVarDecl*> function_param_decls; 3524 3525 // Parse the function children for the parameters 3526 if (die->HasChildren()) 3527 { 3528 bool skip_artificial = true; 3529 ParseChildParameters (sc, 3530 type_sp, 3531 dwarf_cu, 3532 die, 3533 skip_artificial, 3534 type_list, 3535 function_param_types, 3536 function_param_decls, 3537 type_quals); 3538 } 3539 3540 // clang_type will get the function prototype clang type after this call 3541 clang_type = ast.CreateFunctionType (return_clang_type, 3542 &function_param_types[0], 3543 function_param_types.size(), 3544 is_variadic, 3545 type_quals); 3546 3547 if (type_name_cstr) 3548 { 3549 bool type_handled = false; 3550 const DWARFDebugInfoEntry *parent_die = die->GetParent(); 3551 if (tag == DW_TAG_subprogram) 3552 { 3553 if (type_name_cstr[1] == '[' && (type_name_cstr[0] == '-' || type_name_cstr[0] == '+')) 3554 { 3555 // We need to find the DW_TAG_class_type or 3556 // DW_TAG_struct_type by name so we can add this 3557 // as a member function of the class. 3558 const char *class_name_start = type_name_cstr + 2; 3559 const char *class_name_end = ::strchr (class_name_start, ' '); 3560 SymbolContext empty_sc; 3561 clang_type_t class_opaque_type = NULL; 3562 if (class_name_start < class_name_end) 3563 { 3564 ConstString class_name (class_name_start, class_name_end - class_name_start); 3565 TypeList types; 3566 const uint32_t match_count = FindTypes (empty_sc, class_name, true, UINT32_MAX, types); 3567 if (match_count > 0) 3568 { 3569 for (uint32_t i=0; i<match_count; ++i) 3570 { 3571 Type *type = types.GetTypeAtIndex (i).get(); 3572 clang_type_t type_clang_forward_type = type->GetClangForwardType(); 3573 if (ClangASTContext::IsObjCClassType (type_clang_forward_type)) 3574 { 3575 class_opaque_type = type_clang_forward_type; 3576 break; 3577 } 3578 } 3579 } 3580 } 3581 3582 if (class_opaque_type) 3583 { 3584 // If accessibility isn't set to anything valid, assume public for 3585 // now... 3586 if (accessibility == eAccessNone) 3587 accessibility = eAccessPublic; 3588 3589 clang::ObjCMethodDecl *objc_method_decl; 3590 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type, 3591 type_name_cstr, 3592 clang_type, 3593 accessibility); 3594 type_handled = objc_method_decl != NULL; 3595 } 3596 } 3597 else if (parent_die->Tag() == DW_TAG_class_type || 3598 parent_die->Tag() == DW_TAG_structure_type) 3599 { 3600 // Look at the parent of this DIE and see if is is 3601 // a class or struct and see if this is actually a 3602 // C++ method 3603 Type *class_type = ResolveType (dwarf_cu, parent_die); 3604 if (class_type) 3605 { 3606 clang_type_t class_opaque_type = class_type->GetClangForwardType(); 3607 if (ClangASTContext::IsCXXClassType (class_opaque_type)) 3608 { 3609 // Neither GCC 4.2 nor clang++ currently set a valid accessibility 3610 // in the DWARF for C++ methods... Default to public for now... 3611 if (accessibility == eAccessNone) 3612 accessibility = eAccessPublic; 3613 3614 if (!is_static && !die->HasChildren()) 3615 { 3616 // We have a C++ member function with no children (this pointer!) 3617 // and clang will get mad if we try and make a function that isn't 3618 // well formed in the DWARF, so we will just skip it... 3619 type_handled = true; 3620 } 3621 else 3622 { 3623 clang::CXXMethodDecl *cxx_method_decl; 3624 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type, 3625 type_name_cstr, 3626 clang_type, 3627 accessibility, 3628 is_virtual, 3629 is_static, 3630 is_inline, 3631 is_explicit); 3632 type_handled = cxx_method_decl != NULL; 3633 } 3634 } 3635 } 3636 } 3637 } 3638 3639 if (!type_handled) 3640 { 3641 // We just have a function that isn't part of a class 3642 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (type_name_cstr, 3643 clang_type, 3644 storage, 3645 is_inline); 3646 3647 // Add the decl to our DIE to decl context map 3648 assert (function_decl); 3649 m_die_to_decl_ctx[die] = function_decl; 3650 if (!function_param_decls.empty()) 3651 ast.SetFunctionParameters (function_decl, 3652 &function_param_decls.front(), 3653 function_param_decls.size()); 3654 } 3655 } 3656 type_sp.reset( new Type (die->GetOffset(), 3657 this, 3658 type_name_const_str, 3659 0, 3660 NULL, 3661 LLDB_INVALID_UID, 3662 Type::eEncodingIsUID, 3663 &decl, 3664 clang_type, 3665 Type::eResolveStateFull)); 3666 assert(type_sp.get()); 3667 } 3668 break; 3669 3670 case DW_TAG_array_type: 3671 { 3672 // Set a bit that lets us know that we are currently parsing this 3673 m_die_to_type[die] = DIE_IS_BEING_PARSED; 3674 3675 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET; 3676 int64_t first_index = 0; 3677 uint32_t byte_stride = 0; 3678 uint32_t bit_stride = 0; 3679 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3680 3681 if (num_attributes > 0) 3682 { 3683 uint32_t i; 3684 for (i=0; i<num_attributes; ++i) 3685 { 3686 attr = attributes.AttributeAtIndex(i); 3687 DWARFFormValue form_value; 3688 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3689 { 3690 switch (attr) 3691 { 3692 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 3693 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 3694 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 3695 case DW_AT_name: 3696 type_name_cstr = form_value.AsCString(&get_debug_str_data()); 3697 type_name_const_str.SetCString(type_name_cstr); 3698 break; 3699 3700 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break; 3701 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break; 3702 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break; 3703 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break; 3704 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 3705 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break; 3706 case DW_AT_allocated: 3707 case DW_AT_associated: 3708 case DW_AT_data_location: 3709 case DW_AT_description: 3710 case DW_AT_ordering: 3711 case DW_AT_start_scope: 3712 case DW_AT_visibility: 3713 case DW_AT_specification: 3714 case DW_AT_abstract_origin: 3715 case DW_AT_sibling: 3716 break; 3717 } 3718 } 3719 } 3720 3721 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr); 3722 3723 Type *element_type = ResolveTypeUID(type_die_offset); 3724 3725 if (element_type) 3726 { 3727 std::vector<uint64_t> element_orders; 3728 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride); 3729 // We have an array that claims to have no members, lets give it at least one member... 3730 if (element_orders.empty()) 3731 element_orders.push_back (1); 3732 if (byte_stride == 0 && bit_stride == 0) 3733 byte_stride = element_type->GetByteSize(); 3734 clang_type_t array_element_type = element_type->GetClangFullType(); 3735 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride; 3736 uint64_t num_elements = 0; 3737 std::vector<uint64_t>::const_reverse_iterator pos; 3738 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend(); 3739 for (pos = element_orders.rbegin(); pos != end; ++pos) 3740 { 3741 num_elements = *pos; 3742 clang_type = ast.CreateArrayType (array_element_type, 3743 num_elements, 3744 num_elements * array_element_bit_stride); 3745 array_element_type = clang_type; 3746 array_element_bit_stride = array_element_bit_stride * num_elements; 3747 } 3748 ConstString empty_name; 3749 type_sp.reset( new Type (die->GetOffset(), 3750 this, 3751 empty_name, 3752 array_element_bit_stride / 8, 3753 NULL, 3754 type_die_offset, 3755 Type::eEncodingIsUID, 3756 &decl, 3757 clang_type, 3758 Type::eResolveStateFull)); 3759 type_sp->SetEncodingType (element_type); 3760 } 3761 } 3762 } 3763 break; 3764 3765 case DW_TAG_ptr_to_member_type: 3766 { 3767 dw_offset_t type_die_offset = DW_INVALID_OFFSET; 3768 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET; 3769 3770 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 3771 3772 if (num_attributes > 0) { 3773 uint32_t i; 3774 for (i=0; i<num_attributes; ++i) 3775 { 3776 attr = attributes.AttributeAtIndex(i); 3777 DWARFFormValue form_value; 3778 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 3779 { 3780 switch (attr) 3781 { 3782 case DW_AT_type: 3783 type_die_offset = form_value.Reference(dwarf_cu); break; 3784 case DW_AT_containing_type: 3785 containing_type_die_offset = form_value.Reference(dwarf_cu); break; 3786 } 3787 } 3788 } 3789 3790 Type *pointee_type = ResolveTypeUID(type_die_offset); 3791 Type *class_type = ResolveTypeUID(containing_type_die_offset); 3792 3793 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType(); 3794 clang_type_t class_clang_type = class_type->GetClangLayoutType(); 3795 3796 clang_type = ast.CreateMemberPointerType(pointee_clang_type, 3797 class_clang_type); 3798 3799 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(), 3800 clang_type) / 8; 3801 3802 type_sp.reset( new Type (die->GetOffset(), 3803 this, 3804 type_name_const_str, 3805 byte_size, 3806 NULL, 3807 LLDB_INVALID_UID, 3808 Type::eEncodingIsUID, 3809 NULL, 3810 clang_type, 3811 Type::eResolveStateForward)); 3812 } 3813 3814 break; 3815 } 3816 default: 3817 assert(false && "Unhandled type tag!"); 3818 break; 3819 } 3820 3821 if (type_sp.get()) 3822 { 3823 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 3824 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 3825 3826 SymbolContextScope * symbol_context_scope = NULL; 3827 if (sc_parent_tag == DW_TAG_compile_unit) 3828 { 3829 symbol_context_scope = sc.comp_unit; 3830 } 3831 else if (sc.function != NULL) 3832 { 3833 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset()); 3834 if (symbol_context_scope == NULL) 3835 symbol_context_scope = sc.function; 3836 } 3837 3838 if (symbol_context_scope != NULL) 3839 { 3840 type_sp->SetSymbolContextScope(symbol_context_scope); 3841 } 3842 3843 // We are ready to put this type into the uniqued list up at the module level 3844 type_list->Insert (type_sp); 3845 3846 m_die_to_type[die] = type_sp.get(); 3847 } 3848 } 3849 else if (type_ptr != DIE_IS_BEING_PARSED) 3850 { 3851 type_sp = type_list->FindType(type_ptr->GetID()); 3852 } 3853 } 3854 return type_sp; 3855 } 3856 3857 size_t 3858 SymbolFileDWARF::ParseTypes 3859 ( 3860 const SymbolContext& sc, 3861 DWARFCompileUnit* dwarf_cu, 3862 const DWARFDebugInfoEntry *die, 3863 bool parse_siblings, 3864 bool parse_children 3865 ) 3866 { 3867 size_t types_added = 0; 3868 while (die != NULL) 3869 { 3870 bool type_is_new = false; 3871 if (ParseType(sc, dwarf_cu, die, &type_is_new).get()) 3872 { 3873 if (type_is_new) 3874 ++types_added; 3875 } 3876 3877 if (parse_children && die->HasChildren()) 3878 { 3879 if (die->Tag() == DW_TAG_subprogram) 3880 { 3881 SymbolContext child_sc(sc); 3882 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get(); 3883 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true); 3884 } 3885 else 3886 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true); 3887 } 3888 3889 if (parse_siblings) 3890 die = die->GetSibling(); 3891 else 3892 die = NULL; 3893 } 3894 return types_added; 3895 } 3896 3897 3898 size_t 3899 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc) 3900 { 3901 assert(sc.comp_unit && sc.function); 3902 size_t functions_added = 0; 3903 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 3904 if (dwarf_cu) 3905 { 3906 dw_offset_t function_die_offset = sc.function->GetID(); 3907 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset); 3908 if (function_die) 3909 { 3910 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, false, true); 3911 } 3912 } 3913 3914 return functions_added; 3915 } 3916 3917 3918 size_t 3919 SymbolFileDWARF::ParseTypes (const SymbolContext &sc) 3920 { 3921 // At least a compile unit must be valid 3922 assert(sc.comp_unit); 3923 size_t types_added = 0; 3924 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID()); 3925 if (dwarf_cu) 3926 { 3927 if (sc.function) 3928 { 3929 dw_offset_t function_die_offset = sc.function->GetID(); 3930 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset); 3931 if (func_die && func_die->HasChildren()) 3932 { 3933 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true); 3934 } 3935 } 3936 else 3937 { 3938 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE(); 3939 if (dwarf_cu_die && dwarf_cu_die->HasChildren()) 3940 { 3941 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true); 3942 } 3943 } 3944 } 3945 3946 return types_added; 3947 } 3948 3949 size_t 3950 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc) 3951 { 3952 if (sc.comp_unit != NULL) 3953 { 3954 DWARFDebugInfo* info = DebugInfo(); 3955 if (info == NULL) 3956 return 0; 3957 3958 uint32_t cu_idx = UINT32_MAX; 3959 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get(); 3960 3961 if (dwarf_cu == NULL) 3962 return 0; 3963 3964 if (sc.function) 3965 { 3966 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID()); 3967 3968 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS); 3969 assert (func_lo_pc != DW_INVALID_ADDRESS); 3970 3971 return ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true); 3972 } 3973 else if (sc.comp_unit) 3974 { 3975 uint32_t vars_added = 0; 3976 VariableListSP variables (sc.comp_unit->GetVariableList(false)); 3977 3978 if (variables.get() == NULL) 3979 { 3980 variables.reset(new VariableList()); 3981 sc.comp_unit->SetVariableList(variables); 3982 3983 // Index if we already haven't to make sure the compile units 3984 // get indexed and make their global DIE index list 3985 if (!m_indexed) 3986 Index (); 3987 3988 std::vector<NameToDIE::Info> global_die_info_array; 3989 const size_t num_globals = m_global_index.FindAllEntriesForCompileUnitWithIndex (cu_idx, global_die_info_array); 3990 for (size_t idx=0; idx<num_globals; ++idx) 3991 { 3992 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, dwarf_cu->GetDIEAtIndexUnchecked(global_die_info_array[idx].die_idx), LLDB_INVALID_ADDRESS)); 3993 if (var_sp) 3994 { 3995 variables->AddVariableIfUnique (var_sp); 3996 ++vars_added; 3997 } 3998 } 3999 } 4000 return vars_added; 4001 } 4002 } 4003 return 0; 4004 } 4005 4006 4007 VariableSP 4008 SymbolFileDWARF::ParseVariableDIE 4009 ( 4010 const SymbolContext& sc, 4011 DWARFCompileUnit* dwarf_cu, 4012 const DWARFDebugInfoEntry *die, 4013 const lldb::addr_t func_low_pc 4014 ) 4015 { 4016 4017 VariableSP var_sp (m_die_to_variable_sp[die]); 4018 if (var_sp) 4019 return var_sp; // Already been parsed! 4020 4021 const dw_tag_t tag = die->Tag(); 4022 DWARFDebugInfoEntry::Attributes attributes; 4023 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes); 4024 if (num_attributes > 0) 4025 { 4026 const char *name = NULL; 4027 const char *mangled = NULL; 4028 Declaration decl; 4029 uint32_t i; 4030 Type *var_type = NULL; 4031 DWARFExpression location; 4032 bool is_external = false; 4033 bool is_artificial = false; 4034 AccessType accessibility = eAccessNone; 4035 4036 for (i=0; i<num_attributes; ++i) 4037 { 4038 dw_attr_t attr = attributes.AttributeAtIndex(i); 4039 DWARFFormValue form_value; 4040 if (attributes.ExtractFormValueAtIndex(this, i, form_value)) 4041 { 4042 switch (attr) 4043 { 4044 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 4045 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 4046 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 4047 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break; 4048 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break; 4049 case DW_AT_type: var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break; 4050 case DW_AT_external: is_external = form_value.Unsigned() != 0; break; 4051 case DW_AT_location: 4052 { 4053 if (form_value.BlockData()) 4054 { 4055 const DataExtractor& debug_info_data = get_debug_info_data(); 4056 4057 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 4058 uint32_t block_length = form_value.Unsigned(); 4059 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length); 4060 } 4061 else 4062 { 4063 const DataExtractor& debug_loc_data = get_debug_loc_data(); 4064 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 4065 4066 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset); 4067 if (loc_list_length > 0) 4068 { 4069 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length); 4070 assert (func_low_pc != LLDB_INVALID_ADDRESS); 4071 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress()); 4072 } 4073 } 4074 } 4075 break; 4076 4077 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break; 4078 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 4079 case DW_AT_const_value: 4080 case DW_AT_declaration: 4081 case DW_AT_description: 4082 case DW_AT_endianity: 4083 case DW_AT_segment: 4084 case DW_AT_start_scope: 4085 case DW_AT_visibility: 4086 default: 4087 case DW_AT_abstract_origin: 4088 case DW_AT_sibling: 4089 case DW_AT_specification: 4090 break; 4091 } 4092 } 4093 } 4094 4095 if (location.IsValid()) 4096 { 4097 assert(var_type != DIE_IS_BEING_PARSED); 4098 4099 ValueType scope = eValueTypeInvalid; 4100 4101 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die); 4102 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 4103 4104 if (tag == DW_TAG_formal_parameter) 4105 scope = eValueTypeVariableArgument; 4106 else if (is_external || parent_tag == DW_TAG_compile_unit) 4107 scope = eValueTypeVariableGlobal; 4108 else 4109 scope = eValueTypeVariableLocal; 4110 4111 SymbolContextScope * symbol_context_scope = NULL; 4112 if (parent_tag == DW_TAG_compile_unit) 4113 { 4114 symbol_context_scope = sc.comp_unit; 4115 } 4116 else if (sc.function != NULL) 4117 { 4118 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset()); 4119 if (symbol_context_scope == NULL) 4120 symbol_context_scope = sc.function; 4121 } 4122 4123 assert(symbol_context_scope != NULL); 4124 var_sp.reset (new Variable(die->GetOffset(), 4125 name, 4126 mangled, 4127 var_type, 4128 scope, 4129 symbol_context_scope, 4130 &decl, 4131 location, 4132 is_external, 4133 is_artificial)); 4134 4135 } 4136 } 4137 // Cache var_sp even if NULL (the variable was just a specification or 4138 // was missing vital information to be able to be displayed in the debugger 4139 // (missing location due to optimization, etc)) so we don't re-parse 4140 // this DIE over and over later... 4141 m_die_to_variable_sp[die] = var_sp; 4142 return var_sp; 4143 } 4144 4145 size_t 4146 SymbolFileDWARF::ParseVariables 4147 ( 4148 const SymbolContext& sc, 4149 DWARFCompileUnit* dwarf_cu, 4150 const lldb::addr_t func_low_pc, 4151 const DWARFDebugInfoEntry *orig_die, 4152 bool parse_siblings, 4153 bool parse_children, 4154 VariableList* cc_variable_list 4155 ) 4156 { 4157 if (orig_die == NULL) 4158 return 0; 4159 4160 size_t vars_added = 0; 4161 const DWARFDebugInfoEntry *die = orig_die; 4162 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die); 4163 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0; 4164 VariableListSP variables; 4165 switch (parent_tag) 4166 { 4167 case DW_TAG_compile_unit: 4168 if (sc.comp_unit != NULL) 4169 { 4170 variables = sc.comp_unit->GetVariableList(false); 4171 if (variables.get() == NULL) 4172 { 4173 variables.reset(new VariableList()); 4174 sc.comp_unit->SetVariableList(variables); 4175 } 4176 } 4177 else 4178 { 4179 assert(!"Parent DIE was a compile unit, yet we don't have a valid compile unit in the symbol context..."); 4180 vars_added = 0; 4181 } 4182 break; 4183 4184 case DW_TAG_subprogram: 4185 case DW_TAG_inlined_subroutine: 4186 case DW_TAG_lexical_block: 4187 if (sc.function != NULL) 4188 { 4189 // Check to see if we already have parsed the variables for the given scope 4190 4191 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset()); 4192 assert (block != NULL); 4193 variables = block->GetVariableList(false, false); 4194 if (variables.get() == NULL) 4195 { 4196 variables.reset(new VariableList()); 4197 block->SetVariableList(variables); 4198 } 4199 } 4200 else 4201 { 4202 assert(!"Parent DIE was a function or block, yet we don't have a function in the symbol context..."); 4203 vars_added = 0; 4204 } 4205 break; 4206 4207 default: 4208 assert(!"Didn't find appropriate parent DIE for variable list..."); 4209 break; 4210 } 4211 4212 // We need to have a variable list at this point that we can add variables to 4213 assert(variables.get()); 4214 4215 while (die != NULL) 4216 { 4217 dw_tag_t tag = die->Tag(); 4218 4219 // Check to see if we have already parsed this variable or constant? 4220 if (m_die_to_variable_sp[die]) 4221 { 4222 if (cc_variable_list) 4223 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]); 4224 } 4225 else 4226 { 4227 // We haven't already parsed it, lets do that now. 4228 if ((tag == DW_TAG_variable) || 4229 (tag == DW_TAG_constant) || 4230 (tag == DW_TAG_formal_parameter && sc.function)) 4231 { 4232 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc)); 4233 if (var_sp) 4234 { 4235 variables->AddVariableIfUnique (var_sp); 4236 if (cc_variable_list) 4237 cc_variable_list->AddVariableIfUnique (var_sp); 4238 ++vars_added; 4239 } 4240 } 4241 } 4242 4243 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram); 4244 4245 if (!skip_children && parse_children && die->HasChildren()) 4246 { 4247 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list); 4248 } 4249 4250 if (parse_siblings) 4251 die = die->GetSibling(); 4252 else 4253 die = NULL; 4254 } 4255 4256 return vars_added; 4257 } 4258 4259 //------------------------------------------------------------------ 4260 // PluginInterface protocol 4261 //------------------------------------------------------------------ 4262 const char * 4263 SymbolFileDWARF::GetPluginName() 4264 { 4265 return "SymbolFileDWARF"; 4266 } 4267 4268 const char * 4269 SymbolFileDWARF::GetShortPluginName() 4270 { 4271 return GetPluginNameStatic(); 4272 } 4273 4274 uint32_t 4275 SymbolFileDWARF::GetPluginVersion() 4276 { 4277 return 1; 4278 } 4279 4280 void 4281 SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl) 4282 { 4283 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 4284 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 4285 if (clang_type) 4286 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 4287 } 4288 4289 void 4290 SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl) 4291 { 4292 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton; 4293 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl); 4294 if (clang_type) 4295 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type); 4296 } 4297 4298