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