1 //===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "SymbolFileDWARF.h" 10 11 #include "llvm/Support/Casting.h" 12 #include "llvm/Support/Threading.h" 13 14 #include "lldb/Core/Module.h" 15 #include "lldb/Core/ModuleList.h" 16 #include "lldb/Core/ModuleSpec.h" 17 #include "lldb/Core/PluginManager.h" 18 #include "lldb/Core/Section.h" 19 #include "lldb/Core/StreamFile.h" 20 #include "lldb/Core/Value.h" 21 #include "lldb/Utility/ArchSpec.h" 22 #include "lldb/Utility/RegularExpression.h" 23 #include "lldb/Utility/Scalar.h" 24 #include "lldb/Utility/StreamString.h" 25 #include "lldb/Utility/Timer.h" 26 27 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h" 28 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 29 30 #include "lldb/Host/FileSystem.h" 31 #include "lldb/Host/Host.h" 32 33 #include "lldb/Interpreter/OptionValueFileSpecList.h" 34 #include "lldb/Interpreter/OptionValueProperties.h" 35 36 #include "lldb/Symbol/Block.h" 37 #include "lldb/Symbol/ClangASTContext.h" 38 #include "lldb/Symbol/ClangUtil.h" 39 #include "lldb/Symbol/CompileUnit.h" 40 #include "lldb/Symbol/CompilerDecl.h" 41 #include "lldb/Symbol/CompilerDeclContext.h" 42 #include "lldb/Symbol/DebugMacros.h" 43 #include "lldb/Symbol/LineTable.h" 44 #include "lldb/Symbol/LocateSymbolFile.h" 45 #include "lldb/Symbol/ObjectFile.h" 46 #include "lldb/Symbol/SymbolVendor.h" 47 #include "lldb/Symbol/TypeMap.h" 48 #include "lldb/Symbol/TypeSystem.h" 49 #include "lldb/Symbol/VariableList.h" 50 51 #include "lldb/Target/Language.h" 52 #include "lldb/Target/Target.h" 53 54 #include "AppleDWARFIndex.h" 55 #include "DWARFASTParser.h" 56 #include "DWARFASTParserClang.h" 57 #include "DWARFCompileUnit.h" 58 #include "DWARFDebugAbbrev.h" 59 #include "DWARFDebugAranges.h" 60 #include "DWARFDebugInfo.h" 61 #include "DWARFDebugLine.h" 62 #include "DWARFDebugMacro.h" 63 #include "DWARFDebugRanges.h" 64 #include "DWARFDeclContext.h" 65 #include "DWARFFormValue.h" 66 #include "DWARFTypeUnit.h" 67 #include "DWARFUnit.h" 68 #include "DebugNamesDWARFIndex.h" 69 #include "LogChannelDWARF.h" 70 #include "ManualDWARFIndex.h" 71 #include "SymbolFileDWARFDebugMap.h" 72 #include "SymbolFileDWARFDwo.h" 73 #include "SymbolFileDWARFDwp.h" 74 75 #include "llvm/Support/FileSystem.h" 76 77 #include <algorithm> 78 #include <map> 79 #include <memory> 80 81 #include <ctype.h> 82 #include <string.h> 83 84 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 85 86 #ifdef ENABLE_DEBUG_PRINTF 87 #include <stdio.h> 88 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) 89 #else 90 #define DEBUG_PRINTF(fmt, ...) 91 #endif 92 93 using namespace lldb; 94 using namespace lldb_private; 95 96 // static inline bool 97 // child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag) 98 //{ 99 // switch (tag) 100 // { 101 // default: 102 // break; 103 // case DW_TAG_subprogram: 104 // case DW_TAG_inlined_subroutine: 105 // case DW_TAG_class_type: 106 // case DW_TAG_structure_type: 107 // case DW_TAG_union_type: 108 // return true; 109 // } 110 // return false; 111 //} 112 // 113 114 namespace { 115 116 #define LLDB_PROPERTIES_symbolfiledwarf 117 #include "SymbolFileDWARFProperties.inc" 118 119 enum { 120 #define LLDB_PROPERTIES_symbolfiledwarf 121 #include "SymbolFileDWARFPropertiesEnum.inc" 122 }; 123 124 class PluginProperties : public Properties { 125 public: 126 static ConstString GetSettingName() { 127 return SymbolFileDWARF::GetPluginNameStatic(); 128 } 129 130 PluginProperties() { 131 m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName()); 132 m_collection_sp->Initialize(g_symbolfiledwarf_properties); 133 } 134 135 FileSpecList GetSymLinkPaths() { 136 const OptionValueFileSpecList *option_value = 137 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList( 138 nullptr, true, ePropertySymLinkPaths); 139 assert(option_value); 140 return option_value->GetCurrentValue(); 141 } 142 143 bool IgnoreFileIndexes() const { 144 return m_collection_sp->GetPropertyAtIndexAsBoolean( 145 nullptr, ePropertyIgnoreIndexes, false); 146 } 147 }; 148 149 typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP; 150 151 static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() { 152 static const auto g_settings_sp(std::make_shared<PluginProperties>()); 153 return g_settings_sp; 154 } 155 156 } // anonymous namespace end 157 158 FileSpecList SymbolFileDWARF::GetSymlinkPaths() { 159 return GetGlobalPluginProperties()->GetSymLinkPaths(); 160 } 161 162 void SymbolFileDWARF::Initialize() { 163 LogChannelDWARF::Initialize(); 164 PluginManager::RegisterPlugin(GetPluginNameStatic(), 165 GetPluginDescriptionStatic(), CreateInstance, 166 DebuggerInitialize); 167 } 168 169 void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) { 170 if (!PluginManager::GetSettingForSymbolFilePlugin( 171 debugger, PluginProperties::GetSettingName())) { 172 const bool is_global_setting = true; 173 PluginManager::CreateSettingForSymbolFilePlugin( 174 debugger, GetGlobalPluginProperties()->GetValueProperties(), 175 ConstString("Properties for the dwarf symbol-file plug-in."), 176 is_global_setting); 177 } 178 } 179 180 void SymbolFileDWARF::Terminate() { 181 PluginManager::UnregisterPlugin(CreateInstance); 182 LogChannelDWARF::Terminate(); 183 } 184 185 lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() { 186 static ConstString g_name("dwarf"); 187 return g_name; 188 } 189 190 const char *SymbolFileDWARF::GetPluginDescriptionStatic() { 191 return "DWARF and DWARF3 debug symbol file reader."; 192 } 193 194 SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) { 195 return new SymbolFileDWARF(obj_file, 196 /*dwo_section_list*/ nullptr); 197 } 198 199 TypeList &SymbolFileDWARF::GetTypeList() { 200 // This method can be called without going through the symbol vendor so we 201 // need to lock the module. 202 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 203 if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile()) 204 return debug_map_symfile->GetTypeList(); 205 return SymbolFile::GetTypeList(); 206 } 207 void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset, 208 dw_offset_t max_die_offset, uint32_t type_mask, 209 TypeSet &type_set) { 210 if (die) { 211 const dw_offset_t die_offset = die.GetOffset(); 212 213 if (die_offset >= max_die_offset) 214 return; 215 216 if (die_offset >= min_die_offset) { 217 const dw_tag_t tag = die.Tag(); 218 219 bool add_type = false; 220 221 switch (tag) { 222 case DW_TAG_array_type: 223 add_type = (type_mask & eTypeClassArray) != 0; 224 break; 225 case DW_TAG_unspecified_type: 226 case DW_TAG_base_type: 227 add_type = (type_mask & eTypeClassBuiltin) != 0; 228 break; 229 case DW_TAG_class_type: 230 add_type = (type_mask & eTypeClassClass) != 0; 231 break; 232 case DW_TAG_structure_type: 233 add_type = (type_mask & eTypeClassStruct) != 0; 234 break; 235 case DW_TAG_union_type: 236 add_type = (type_mask & eTypeClassUnion) != 0; 237 break; 238 case DW_TAG_enumeration_type: 239 add_type = (type_mask & eTypeClassEnumeration) != 0; 240 break; 241 case DW_TAG_subroutine_type: 242 case DW_TAG_subprogram: 243 case DW_TAG_inlined_subroutine: 244 add_type = (type_mask & eTypeClassFunction) != 0; 245 break; 246 case DW_TAG_pointer_type: 247 add_type = (type_mask & eTypeClassPointer) != 0; 248 break; 249 case DW_TAG_rvalue_reference_type: 250 case DW_TAG_reference_type: 251 add_type = (type_mask & eTypeClassReference) != 0; 252 break; 253 case DW_TAG_typedef: 254 add_type = (type_mask & eTypeClassTypedef) != 0; 255 break; 256 case DW_TAG_ptr_to_member_type: 257 add_type = (type_mask & eTypeClassMemberPointer) != 0; 258 break; 259 } 260 261 if (add_type) { 262 const bool assert_not_being_parsed = true; 263 Type *type = ResolveTypeUID(die, assert_not_being_parsed); 264 if (type) { 265 if (type_set.find(type) == type_set.end()) 266 type_set.insert(type); 267 } 268 } 269 } 270 271 for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid(); 272 child_die = child_die.GetSibling()) { 273 GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set); 274 } 275 } 276 } 277 278 size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope, 279 TypeClass type_mask, TypeList &type_list) 280 281 { 282 ASSERT_MODULE_LOCK(this); 283 TypeSet type_set; 284 285 CompileUnit *comp_unit = nullptr; 286 DWARFUnit *dwarf_cu = nullptr; 287 if (sc_scope) 288 comp_unit = sc_scope->CalculateSymbolContextCompileUnit(); 289 290 if (comp_unit) { 291 dwarf_cu = GetDWARFCompileUnit(comp_unit); 292 if (dwarf_cu == nullptr) 293 return 0; 294 GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(), 295 dwarf_cu->GetNextUnitOffset(), type_mask, type_set); 296 } else { 297 DWARFDebugInfo *info = DebugInfo(); 298 if (info) { 299 const size_t num_cus = info->GetNumUnits(); 300 for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) { 301 dwarf_cu = info->GetUnitAtIndex(cu_idx); 302 if (dwarf_cu) { 303 GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set); 304 } 305 } 306 } 307 } 308 309 std::set<CompilerType> compiler_type_set; 310 size_t num_types_added = 0; 311 for (Type *type : type_set) { 312 CompilerType compiler_type = type->GetForwardCompilerType(); 313 if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) { 314 compiler_type_set.insert(compiler_type); 315 type_list.Insert(type->shared_from_this()); 316 ++num_types_added; 317 } 318 } 319 return num_types_added; 320 } 321 322 // Gets the first parent that is a lexical block, function or inlined 323 // subroutine, or compile unit. 324 DWARFDIE 325 SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) { 326 DWARFDIE die; 327 for (die = child_die.GetParent(); die; die = die.GetParent()) { 328 dw_tag_t tag = die.Tag(); 329 330 switch (tag) { 331 case DW_TAG_compile_unit: 332 case DW_TAG_partial_unit: 333 case DW_TAG_subprogram: 334 case DW_TAG_inlined_subroutine: 335 case DW_TAG_lexical_block: 336 return die; 337 } 338 } 339 return DWARFDIE(); 340 } 341 342 SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile, 343 SectionList *dwo_section_list) 344 : SymbolFile(objfile), 345 UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to 346 // when this class parses .o files to 347 // contain the .o file index/ID 348 m_debug_map_module_wp(), m_debug_map_symfile(nullptr), 349 m_context(objfile->GetModule()->GetSectionList(), dwo_section_list), 350 m_data_debug_loc(), m_abbr(), m_info(), m_fetched_external_modules(false), 351 m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), 352 m_unique_ast_type_map() {} 353 354 SymbolFileDWARF::~SymbolFileDWARF() {} 355 356 static ConstString GetDWARFMachOSegmentName() { 357 static ConstString g_dwarf_section_name("__DWARF"); 358 return g_dwarf_section_name; 359 } 360 361 UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() { 362 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 363 if (debug_map_symfile) 364 return debug_map_symfile->GetUniqueDWARFASTTypeMap(); 365 else 366 return m_unique_ast_type_map; 367 } 368 369 TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) { 370 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 371 TypeSystem *type_system; 372 if (debug_map_symfile) { 373 type_system = debug_map_symfile->GetTypeSystemForLanguage(language); 374 } else { 375 type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language); 376 if (type_system) 377 type_system->SetSymbolFile(this); 378 } 379 return type_system; 380 } 381 382 void SymbolFileDWARF::InitializeObject() { 383 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); 384 385 if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) { 386 DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc; 387 LoadSectionData(eSectionTypeDWARFAppleNames, apple_names); 388 LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces); 389 LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types); 390 LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc); 391 392 m_index = AppleDWARFIndex::Create( 393 *GetObjectFile()->GetModule(), apple_names, apple_namespaces, 394 apple_types, apple_objc, m_context.getOrLoadStrData()); 395 396 if (m_index) 397 return; 398 399 DWARFDataExtractor debug_names; 400 LoadSectionData(eSectionTypeDWARFDebugNames, debug_names); 401 if (debug_names.GetByteSize() > 0) { 402 llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or = 403 DebugNamesDWARFIndex::Create( 404 *GetObjectFile()->GetModule(), debug_names, 405 m_context.getOrLoadStrData(), DebugInfo()); 406 if (index_or) { 407 m_index = std::move(*index_or); 408 return; 409 } 410 LLDB_LOG_ERROR(log, index_or.takeError(), 411 "Unable to read .debug_names data: {0}"); 412 } 413 } 414 415 m_index = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(), 416 DebugInfo()); 417 } 418 419 bool SymbolFileDWARF::SupportedVersion(uint16_t version) { 420 return version >= 2 && version <= 5; 421 } 422 423 uint32_t SymbolFileDWARF::CalculateAbilities() { 424 uint32_t abilities = 0; 425 if (m_obj_file != nullptr) { 426 const Section *section = nullptr; 427 const SectionList *section_list = m_obj_file->GetSectionList(); 428 if (section_list == nullptr) 429 return 0; 430 431 uint64_t debug_abbrev_file_size = 0; 432 uint64_t debug_info_file_size = 0; 433 uint64_t debug_line_file_size = 0; 434 435 section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get(); 436 437 if (section) 438 section_list = §ion->GetChildren(); 439 440 section = 441 section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get(); 442 if (section != nullptr) { 443 debug_info_file_size = section->GetFileSize(); 444 445 section = 446 section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true) 447 .get(); 448 if (section) 449 debug_abbrev_file_size = section->GetFileSize(); 450 451 DWARFDebugAbbrev *abbrev = DebugAbbrev(); 452 if (abbrev) { 453 std::set<dw_form_t> invalid_forms; 454 abbrev->GetUnsupportedForms(invalid_forms); 455 if (!invalid_forms.empty()) { 456 StreamString error; 457 error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : ""); 458 for (auto form : invalid_forms) 459 error.Printf(" %#x", form); 460 m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str()); 461 return 0; 462 } 463 } 464 465 section = 466 section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true) 467 .get(); 468 if (section) 469 debug_line_file_size = section->GetFileSize(); 470 } else { 471 const char *symfile_dir_cstr = 472 m_obj_file->GetFileSpec().GetDirectory().GetCString(); 473 if (symfile_dir_cstr) { 474 if (strcasestr(symfile_dir_cstr, ".dsym")) { 475 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo) { 476 // We have a dSYM file that didn't have a any debug info. If the 477 // string table has a size of 1, then it was made from an 478 // executable with no debug info, or from an executable that was 479 // stripped. 480 section = 481 section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true) 482 .get(); 483 if (section && section->GetFileSize() == 1) { 484 m_obj_file->GetModule()->ReportWarning( 485 "empty dSYM file detected, dSYM was created with an " 486 "executable with no debug info."); 487 } 488 } 489 } 490 } 491 } 492 493 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0) 494 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | 495 LocalVariables | VariableTypes; 496 497 if (debug_line_file_size > 0) 498 abilities |= LineTables; 499 } 500 return abilities; 501 } 502 503 const DWARFDataExtractor & 504 SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type, 505 DWARFDataSegment &data_segment) { 506 llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] { 507 this->LoadSectionData(sect_type, std::ref(data_segment.m_data)); 508 }); 509 return data_segment.m_data; 510 } 511 512 void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type, 513 DWARFDataExtractor &data) { 514 ModuleSP module_sp(m_obj_file->GetModule()); 515 const SectionList *section_list = module_sp->GetSectionList(); 516 if (!section_list) 517 return; 518 519 SectionSP section_sp(section_list->FindSectionByType(sect_type, true)); 520 if (!section_sp) 521 return; 522 523 data.Clear(); 524 m_obj_file->ReadSectionData(section_sp.get(), data); 525 } 526 527 const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() { 528 const DWARFDataExtractor &debugLocData = get_debug_loc_data(); 529 if (debugLocData.GetByteSize() > 0) 530 return debugLocData; 531 return get_debug_loclists_data(); 532 } 533 534 const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() { 535 return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc); 536 } 537 538 const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() { 539 return GetCachedSectionData(eSectionTypeDWARFDebugLocLists, 540 m_data_debug_loclists); 541 } 542 543 DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() { 544 if (m_abbr) 545 return m_abbr.get(); 546 547 const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData(); 548 if (debug_abbrev_data.GetByteSize() == 0) 549 return nullptr; 550 551 auto abbr = llvm::make_unique<DWARFDebugAbbrev>(); 552 llvm::Error error = abbr->parse(debug_abbrev_data); 553 if (error) { 554 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); 555 LLDB_LOG_ERROR(log, std::move(error), 556 "Unable to read .debug_abbrev section: {0}"); 557 return nullptr; 558 } 559 560 m_abbr = std::move(abbr); 561 return m_abbr.get(); 562 } 563 564 const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const { 565 return m_abbr.get(); 566 } 567 568 DWARFDebugInfo *SymbolFileDWARF::DebugInfo() { 569 if (m_info == nullptr) { 570 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 571 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION, 572 static_cast<void *>(this)); 573 if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0) 574 m_info = llvm::make_unique<DWARFDebugInfo>(*this, m_context); 575 } 576 return m_info.get(); 577 } 578 579 const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const { 580 return m_info.get(); 581 } 582 583 DWARFUnit * 584 SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) { 585 if (!comp_unit) 586 return nullptr; 587 588 DWARFDebugInfo *info = DebugInfo(); 589 if (info) { 590 // The compile unit ID is the index of the DWARF unit. 591 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID()); 592 if (dwarf_cu && dwarf_cu->GetUserData() == nullptr) 593 dwarf_cu->SetUserData(comp_unit); 594 return dwarf_cu; 595 } 596 return nullptr; 597 } 598 599 DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() { 600 if (!m_ranges) { 601 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 602 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION, 603 static_cast<void *>(this)); 604 605 if (m_context.getOrLoadRangesData().GetByteSize() > 0) 606 m_ranges.reset(new DWARFDebugRanges()); 607 608 if (m_ranges) 609 m_ranges->Extract(m_context); 610 } 611 return m_ranges.get(); 612 } 613 614 DWARFDebugRngLists *SymbolFileDWARF::GetDebugRngLists() { 615 if (!m_rnglists) { 616 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 617 Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION, 618 static_cast<void *>(this)); 619 620 if (m_context.getOrLoadRngListsData().GetByteSize() > 0) 621 m_rnglists.reset(new DWARFDebugRngLists()); 622 623 if (m_rnglists) 624 m_rnglists->Extract(m_context); 625 } 626 return m_rnglists.get(); 627 } 628 629 lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) { 630 CompUnitSP cu_sp; 631 CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData(); 632 if (comp_unit) { 633 // We already parsed this compile unit, had out a shared pointer to it 634 cu_sp = comp_unit->shared_from_this(); 635 } else { 636 if (&dwarf_cu.GetSymbolFileDWARF() != this) { 637 return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu); 638 } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) { 639 // Let the debug map create the compile unit 640 cu_sp = m_debug_map_symfile->GetCompileUnit(this); 641 dwarf_cu.SetUserData(cu_sp.get()); 642 } else { 643 ModuleSP module_sp(m_obj_file->GetModule()); 644 if (module_sp) { 645 const DWARFDIE cu_die = dwarf_cu.DIE(); 646 if (cu_die) { 647 FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle()); 648 if (cu_file_spec) { 649 // If we have a full path to the compile unit, we don't need to 650 // resolve the file. This can be expensive e.g. when the source 651 // files are NFS mounted. 652 cu_file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory()); 653 654 std::string remapped_file; 655 if (module_sp->RemapSourceFile(cu_file_spec.GetPath(), 656 remapped_file)) 657 cu_file_spec.SetFile(remapped_file, FileSpec::Style::native); 658 } 659 660 LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF( 661 cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0)); 662 663 bool is_optimized = dwarf_cu.GetIsOptimized(); 664 BuildCuTranslationTable(); 665 cu_sp = std::make_shared<CompileUnit>( 666 module_sp, &dwarf_cu, cu_file_spec, 667 *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language, 668 is_optimized ? eLazyBoolYes : eLazyBoolNo); 669 670 dwarf_cu.SetUserData(cu_sp.get()); 671 672 SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp); 673 } 674 } 675 } 676 } 677 return cu_sp; 678 } 679 680 void SymbolFileDWARF::BuildCuTranslationTable() { 681 if (!m_lldb_cu_to_dwarf_unit.empty()) 682 return; 683 684 DWARFDebugInfo *info = DebugInfo(); 685 if (!info) 686 return; 687 688 if (!info->ContainsTypeUnits()) { 689 // We can use a 1-to-1 mapping. No need to build a translation table. 690 return; 691 } 692 for (uint32_t i = 0, num = info->GetNumUnits(); i < num; ++i) { 693 if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info->GetUnitAtIndex(i))) { 694 cu->SetID(m_lldb_cu_to_dwarf_unit.size()); 695 m_lldb_cu_to_dwarf_unit.push_back(i); 696 } 697 } 698 } 699 700 llvm::Optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) { 701 BuildCuTranslationTable(); 702 if (m_lldb_cu_to_dwarf_unit.empty()) 703 return cu_idx; 704 if (cu_idx >= m_lldb_cu_to_dwarf_unit.size()) 705 return llvm::None; 706 return m_lldb_cu_to_dwarf_unit[cu_idx]; 707 } 708 709 uint32_t SymbolFileDWARF::CalculateNumCompileUnits() { 710 DWARFDebugInfo *info = DebugInfo(); 711 if (!info) 712 return 0; 713 BuildCuTranslationTable(); 714 return m_lldb_cu_to_dwarf_unit.empty() ? info->GetNumUnits() 715 : m_lldb_cu_to_dwarf_unit.size(); 716 } 717 718 CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) { 719 ASSERT_MODULE_LOCK(this); 720 DWARFDebugInfo *info = DebugInfo(); 721 if (!info) 722 return {}; 723 724 if (llvm::Optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) { 725 if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>( 726 info->GetUnitAtIndex(*dwarf_idx))) 727 return ParseCompileUnit(*dwarf_cu); 728 } 729 return {}; 730 } 731 732 Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit, 733 const DWARFDIE &die) { 734 ASSERT_MODULE_LOCK(this); 735 if (die.IsValid()) { 736 TypeSystem *type_system = 737 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType()); 738 739 if (type_system) { 740 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 741 if (dwarf_ast) 742 return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die); 743 } 744 } 745 return nullptr; 746 } 747 748 bool SymbolFileDWARF::FixupAddress(Address &addr) { 749 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 750 if (debug_map_symfile) { 751 return debug_map_symfile->LinkOSOAddress(addr); 752 } 753 // This is a normal DWARF file, no address fixups need to happen 754 return true; 755 } 756 lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) { 757 ASSERT_MODULE_LOCK(this); 758 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 759 if (dwarf_cu) 760 return dwarf_cu->GetLanguageType(); 761 else 762 return eLanguageTypeUnknown; 763 } 764 765 size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) { 766 ASSERT_MODULE_LOCK(this); 767 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 768 if (!dwarf_cu) 769 return 0; 770 771 size_t functions_added = 0; 772 std::vector<DWARFDIE> function_dies; 773 dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies); 774 for (const DWARFDIE &die : function_dies) { 775 if (comp_unit.FindFunctionByUID(die.GetID())) 776 continue; 777 if (ParseFunction(comp_unit, die)) 778 ++functions_added; 779 } 780 // FixupTypes(); 781 return functions_added; 782 } 783 784 bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit, 785 FileSpecList &support_files) { 786 ASSERT_MODULE_LOCK(this); 787 if (DWARFUnit *unit = GetDWARFCompileUnit(&comp_unit)) { 788 const dw_offset_t stmt_list = unit->GetLineTableOffset(); 789 if (stmt_list != DW_INVALID_OFFSET) { 790 // All file indexes in DWARF are one based and a file of index zero is 791 // supposed to be the compile unit itself. 792 support_files.Append(comp_unit); 793 return DWARFDebugLine::ParseSupportFiles(comp_unit.GetModule(), 794 m_context.getOrLoadLineData(), 795 stmt_list, support_files, unit); 796 } 797 } 798 return false; 799 } 800 801 FileSpec SymbolFileDWARF::GetFile(DWARFUnit &unit, size_t file_idx) { 802 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit)) { 803 if (CompileUnit *lldb_cu = GetCompUnitForDWARFCompUnit(*dwarf_cu)) 804 return lldb_cu->GetSupportFiles().GetFileSpecAtIndex(file_idx); 805 return FileSpec(); 806 } 807 808 auto &tu = llvm::cast<DWARFTypeUnit>(unit); 809 return GetTypeUnitSupportFiles(tu).GetFileSpecAtIndex(file_idx); 810 } 811 812 const FileSpecList & 813 SymbolFileDWARF::GetTypeUnitSupportFiles(DWARFTypeUnit &tu) { 814 static FileSpecList empty_list; 815 816 dw_offset_t offset = tu.GetLineTableOffset(); 817 if (offset == DW_INVALID_OFFSET || 818 offset == llvm::DenseMapInfo<dw_offset_t>::getEmptyKey() || 819 offset == llvm::DenseMapInfo<dw_offset_t>::getTombstoneKey()) 820 return empty_list; 821 822 // Many type units can share a line table, so parse the support file list 823 // once, and cache it based on the offset field. 824 auto iter_bool = m_type_unit_support_files.try_emplace(offset); 825 FileSpecList &list = iter_bool.first->second; 826 if (iter_bool.second) { 827 list.EmplaceBack(); 828 DWARFDebugLine::ParseSupportFiles(GetObjectFile()->GetModule(), 829 m_context.getOrLoadLineData(), offset, 830 list, &tu); 831 } 832 return list; 833 } 834 835 bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) { 836 ASSERT_MODULE_LOCK(this); 837 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 838 if (dwarf_cu) 839 return dwarf_cu->GetIsOptimized(); 840 return false; 841 } 842 843 bool SymbolFileDWARF::ParseImportedModules( 844 const lldb_private::SymbolContext &sc, 845 std::vector<SourceModule> &imported_modules) { 846 ASSERT_MODULE_LOCK(this); 847 assert(sc.comp_unit); 848 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit); 849 if (!dwarf_cu) 850 return false; 851 if (!ClangModulesDeclVendor::LanguageSupportsClangModules( 852 sc.comp_unit->GetLanguage())) 853 return false; 854 UpdateExternalModuleListIfNeeded(); 855 856 const DWARFDIE die = dwarf_cu->DIE(); 857 if (!die) 858 return false; 859 860 for (DWARFDIE child_die = die.GetFirstChild(); child_die; 861 child_die = child_die.GetSibling()) { 862 if (child_die.Tag() != DW_TAG_imported_declaration) 863 continue; 864 865 DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import); 866 if (module_die.Tag() != DW_TAG_module) 867 continue; 868 869 if (const char *name = 870 module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) { 871 SourceModule module; 872 module.path.push_back(ConstString(name)); 873 874 DWARFDIE parent_die = module_die; 875 while ((parent_die = parent_die.GetParent())) { 876 if (parent_die.Tag() != DW_TAG_module) 877 break; 878 if (const char *name = 879 parent_die.GetAttributeValueAsString(DW_AT_name, nullptr)) 880 module.path.push_back(ConstString(name)); 881 } 882 std::reverse(module.path.begin(), module.path.end()); 883 if (const char *include_path = module_die.GetAttributeValueAsString( 884 DW_AT_LLVM_include_path, nullptr)) 885 module.search_path = ConstString(include_path); 886 if (const char *sysroot = module_die.GetAttributeValueAsString( 887 DW_AT_LLVM_isysroot, nullptr)) 888 module.sysroot = ConstString(sysroot); 889 imported_modules.push_back(module); 890 } 891 } 892 return true; 893 } 894 895 struct ParseDWARFLineTableCallbackInfo { 896 LineTable *line_table; 897 std::unique_ptr<LineSequence> sequence_up; 898 lldb::addr_t addr_mask; 899 }; 900 901 // ParseStatementTableCallback 902 static void ParseDWARFLineTableCallback(dw_offset_t offset, 903 const DWARFDebugLine::State &state, 904 void *userData) { 905 if (state.row == DWARFDebugLine::State::StartParsingLineTable) { 906 // Just started parsing the line table 907 } else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) { 908 // Done parsing line table, nothing to do for the cleanup 909 } else { 910 ParseDWARFLineTableCallbackInfo *info = 911 (ParseDWARFLineTableCallbackInfo *)userData; 912 LineTable *line_table = info->line_table; 913 914 // If this is our first time here, we need to create a sequence container. 915 if (!info->sequence_up) { 916 info->sequence_up.reset(line_table->CreateLineSequenceContainer()); 917 assert(info->sequence_up.get()); 918 } 919 line_table->AppendLineEntryToSequence( 920 info->sequence_up.get(), state.address & info->addr_mask, state.line, 921 state.column, state.file, state.is_stmt, state.basic_block, 922 state.prologue_end, state.epilogue_begin, state.end_sequence); 923 if (state.end_sequence) { 924 // First, put the current sequence into the line table. 925 line_table->InsertSequence(info->sequence_up.get()); 926 // Then, empty it to prepare for the next sequence. 927 info->sequence_up->Clear(); 928 } 929 } 930 } 931 932 bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) { 933 ASSERT_MODULE_LOCK(this); 934 if (comp_unit.GetLineTable() != nullptr) 935 return true; 936 937 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 938 if (dwarf_cu) { 939 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly(); 940 if (dwarf_cu_die) { 941 const dw_offset_t cu_line_offset = 942 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list, 943 DW_INVALID_OFFSET); 944 if (cu_line_offset != DW_INVALID_OFFSET) { 945 std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit)); 946 if (line_table_up) { 947 ParseDWARFLineTableCallbackInfo info; 948 info.line_table = line_table_up.get(); 949 950 /* 951 * MIPS: 952 * The SymbolContext may not have a valid target, thus we may not be 953 * able 954 * to call Address::GetOpcodeLoadAddress() which would clear the bit 955 * #0 956 * for MIPS. Use ArchSpec to clear the bit #0. 957 */ 958 switch (GetObjectFile()->GetArchitecture().GetMachine()) { 959 case llvm::Triple::mips: 960 case llvm::Triple::mipsel: 961 case llvm::Triple::mips64: 962 case llvm::Triple::mips64el: 963 info.addr_mask = ~((lldb::addr_t)1); 964 break; 965 default: 966 info.addr_mask = ~((lldb::addr_t)0); 967 break; 968 } 969 970 lldb::offset_t offset = cu_line_offset; 971 DWARFDebugLine::ParseStatementTable( 972 m_context.getOrLoadLineData(), &offset, 973 ParseDWARFLineTableCallback, &info, dwarf_cu); 974 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 975 if (debug_map_symfile) { 976 // We have an object file that has a line table with addresses that 977 // are not linked. We need to link the line table and convert the 978 // addresses that are relative to the .o file into addresses for 979 // the main executable. 980 comp_unit.SetLineTable( 981 debug_map_symfile->LinkOSOLineTable(this, line_table_up.get())); 982 } else { 983 comp_unit.SetLineTable(line_table_up.release()); 984 return true; 985 } 986 } 987 } 988 } 989 } 990 return false; 991 } 992 993 lldb_private::DebugMacrosSP 994 SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) { 995 auto iter = m_debug_macros_map.find(*offset); 996 if (iter != m_debug_macros_map.end()) 997 return iter->second; 998 999 const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData(); 1000 if (debug_macro_data.GetByteSize() == 0) 1001 return DebugMacrosSP(); 1002 1003 lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros()); 1004 m_debug_macros_map[*offset] = debug_macros_sp; 1005 1006 const DWARFDebugMacroHeader &header = 1007 DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset); 1008 DWARFDebugMacroEntry::ReadMacroEntries( 1009 debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(), 1010 offset, this, debug_macros_sp); 1011 1012 return debug_macros_sp; 1013 } 1014 1015 bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) { 1016 ASSERT_MODULE_LOCK(this); 1017 1018 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 1019 if (dwarf_cu == nullptr) 1020 return false; 1021 1022 const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly(); 1023 if (!dwarf_cu_die) 1024 return false; 1025 1026 lldb::offset_t sect_offset = 1027 dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET); 1028 if (sect_offset == DW_INVALID_OFFSET) 1029 sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros, 1030 DW_INVALID_OFFSET); 1031 if (sect_offset == DW_INVALID_OFFSET) 1032 return false; 1033 1034 comp_unit.SetDebugMacros(ParseDebugMacros(§_offset)); 1035 1036 return true; 1037 } 1038 1039 size_t SymbolFileDWARF::ParseBlocksRecursive( 1040 lldb_private::CompileUnit &comp_unit, Block *parent_block, 1041 const DWARFDIE &orig_die, addr_t subprogram_low_pc, uint32_t depth) { 1042 size_t blocks_added = 0; 1043 DWARFDIE die = orig_die; 1044 while (die) { 1045 dw_tag_t tag = die.Tag(); 1046 1047 switch (tag) { 1048 case DW_TAG_inlined_subroutine: 1049 case DW_TAG_subprogram: 1050 case DW_TAG_lexical_block: { 1051 Block *block = nullptr; 1052 if (tag == DW_TAG_subprogram) { 1053 // Skip any DW_TAG_subprogram DIEs that are inside of a normal or 1054 // inlined functions. These will be parsed on their own as separate 1055 // entities. 1056 1057 if (depth > 0) 1058 break; 1059 1060 block = parent_block; 1061 } else { 1062 BlockSP block_sp(new Block(die.GetID())); 1063 parent_block->AddChild(block_sp); 1064 block = block_sp.get(); 1065 } 1066 DWARFRangeList ranges; 1067 const char *name = nullptr; 1068 const char *mangled_name = nullptr; 1069 1070 int decl_file = 0; 1071 int decl_line = 0; 1072 int decl_column = 0; 1073 int call_file = 0; 1074 int call_line = 0; 1075 int call_column = 0; 1076 if (die.GetDIENamesAndRanges(name, mangled_name, ranges, decl_file, 1077 decl_line, decl_column, call_file, call_line, 1078 call_column, nullptr)) { 1079 if (tag == DW_TAG_subprogram) { 1080 assert(subprogram_low_pc == LLDB_INVALID_ADDRESS); 1081 subprogram_low_pc = ranges.GetMinRangeBase(0); 1082 } else if (tag == DW_TAG_inlined_subroutine) { 1083 // We get called here for inlined subroutines in two ways. The first 1084 // time is when we are making the Function object for this inlined 1085 // concrete instance. Since we're creating a top level block at 1086 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we 1087 // need to adjust the containing address. The second time is when we 1088 // are parsing the blocks inside the function that contains the 1089 // inlined concrete instance. Since these will be blocks inside the 1090 // containing "real" function the offset will be for that function. 1091 if (subprogram_low_pc == LLDB_INVALID_ADDRESS) { 1092 subprogram_low_pc = ranges.GetMinRangeBase(0); 1093 } 1094 } 1095 1096 const size_t num_ranges = ranges.GetSize(); 1097 for (size_t i = 0; i < num_ranges; ++i) { 1098 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i); 1099 const addr_t range_base = range.GetRangeBase(); 1100 if (range_base >= subprogram_low_pc) 1101 block->AddRange(Block::Range(range_base - subprogram_low_pc, 1102 range.GetByteSize())); 1103 else { 1104 GetObjectFile()->GetModule()->ReportError( 1105 "0x%8.8" PRIx64 ": adding range [0x%" PRIx64 "-0x%" PRIx64 1106 ") which has a base that is less than the function's low PC " 1107 "0x%" PRIx64 ". Please file a bug and attach the file at the " 1108 "start of this error message", 1109 block->GetID(), range_base, range.GetRangeEnd(), 1110 subprogram_low_pc); 1111 } 1112 } 1113 block->FinalizeRanges(); 1114 1115 if (tag != DW_TAG_subprogram && 1116 (name != nullptr || mangled_name != nullptr)) { 1117 std::unique_ptr<Declaration> decl_up; 1118 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 1119 decl_up.reset(new Declaration( 1120 comp_unit.GetSupportFiles().GetFileSpecAtIndex(decl_file), 1121 decl_line, decl_column)); 1122 1123 std::unique_ptr<Declaration> call_up; 1124 if (call_file != 0 || call_line != 0 || call_column != 0) 1125 call_up.reset(new Declaration( 1126 comp_unit.GetSupportFiles().GetFileSpecAtIndex(call_file), 1127 call_line, call_column)); 1128 1129 block->SetInlinedFunctionInfo(name, mangled_name, decl_up.get(), 1130 call_up.get()); 1131 } 1132 1133 ++blocks_added; 1134 1135 if (die.HasChildren()) { 1136 blocks_added += 1137 ParseBlocksRecursive(comp_unit, block, die.GetFirstChild(), 1138 subprogram_low_pc, depth + 1); 1139 } 1140 } 1141 } break; 1142 default: 1143 break; 1144 } 1145 1146 // Only parse siblings of the block if we are not at depth zero. A depth of 1147 // zero indicates we are currently parsing the top level DW_TAG_subprogram 1148 // DIE 1149 1150 if (depth == 0) 1151 die.Clear(); 1152 else 1153 die = die.GetSibling(); 1154 } 1155 return blocks_added; 1156 } 1157 1158 bool SymbolFileDWARF::ClassOrStructIsVirtual(const DWARFDIE &parent_die) { 1159 if (parent_die) { 1160 for (DWARFDIE die = parent_die.GetFirstChild(); die; 1161 die = die.GetSibling()) { 1162 dw_tag_t tag = die.Tag(); 1163 bool check_virtuality = false; 1164 switch (tag) { 1165 case DW_TAG_inheritance: 1166 case DW_TAG_subprogram: 1167 check_virtuality = true; 1168 break; 1169 default: 1170 break; 1171 } 1172 if (check_virtuality) { 1173 if (die.GetAttributeValueAsUnsigned(DW_AT_virtuality, 0) != 0) 1174 return true; 1175 } 1176 } 1177 } 1178 return false; 1179 } 1180 1181 void SymbolFileDWARF::ParseDeclsForContext(CompilerDeclContext decl_ctx) { 1182 TypeSystem *type_system = decl_ctx.GetTypeSystem(); 1183 DWARFASTParser *ast_parser = type_system->GetDWARFParser(); 1184 std::vector<DWARFDIE> decl_ctx_die_list = 1185 ast_parser->GetDIEForDeclContext(decl_ctx); 1186 1187 for (DWARFDIE decl_ctx_die : decl_ctx_die_list) 1188 for (DWARFDIE decl = decl_ctx_die.GetFirstChild(); decl; 1189 decl = decl.GetSibling()) 1190 ast_parser->GetDeclForUIDFromDWARF(decl); 1191 } 1192 1193 user_id_t SymbolFileDWARF::GetUID(DIERef ref) { 1194 if (GetDebugMapSymfile()) 1195 return GetID() | ref.die_offset(); 1196 1197 return user_id_t(GetDwoNum().getValueOr(0x7fffffff)) << 32 | 1198 ref.die_offset() | 1199 (lldb::user_id_t(ref.section() == DIERef::Section::DebugTypes) << 63); 1200 } 1201 1202 llvm::Optional<SymbolFileDWARF::DecodedUID> 1203 SymbolFileDWARF::DecodeUID(lldb::user_id_t uid) { 1204 // This method can be called without going through the symbol vendor so we 1205 // need to lock the module. 1206 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1207 // Anytime we get a "lldb::user_id_t" from an lldb_private::SymbolFile API we 1208 // must make sure we use the correct DWARF file when resolving things. On 1209 // MacOSX, when using SymbolFileDWARFDebugMap, we will use multiple 1210 // SymbolFileDWARF classes, one for each .o file. We can often end up with 1211 // references to other DWARF objects and we must be ready to receive a 1212 // "lldb::user_id_t" that specifies a DIE from another SymbolFileDWARF 1213 // instance. 1214 if (SymbolFileDWARFDebugMap *debug_map = GetDebugMapSymfile()) { 1215 SymbolFileDWARF *dwarf = debug_map->GetSymbolFileByOSOIndex( 1216 debug_map->GetOSOIndexFromUserID(uid)); 1217 return DecodedUID{ 1218 *dwarf, {llvm::None, DIERef::Section::DebugInfo, dw_offset_t(uid)}}; 1219 } 1220 dw_offset_t die_offset = uid; 1221 if (die_offset == DW_INVALID_OFFSET) 1222 return llvm::None; 1223 1224 DIERef::Section section = 1225 uid >> 63 ? DIERef::Section::DebugTypes : DIERef::Section::DebugInfo; 1226 1227 llvm::Optional<uint32_t> dwo_num = uid >> 32 & 0x7fffffff; 1228 if (*dwo_num == 0x7fffffff) 1229 dwo_num = llvm::None; 1230 1231 return DecodedUID{*this, {dwo_num, section, die_offset}}; 1232 } 1233 1234 DWARFDIE 1235 SymbolFileDWARF::GetDIE(lldb::user_id_t uid) { 1236 // This method can be called without going through the symbol vendor so we 1237 // need to lock the module. 1238 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1239 1240 llvm::Optional<DecodedUID> decoded = DecodeUID(uid); 1241 1242 if (decoded) 1243 return decoded->dwarf.GetDIE(decoded->ref); 1244 1245 return DWARFDIE(); 1246 } 1247 1248 CompilerDecl SymbolFileDWARF::GetDeclForUID(lldb::user_id_t type_uid) { 1249 // This method can be called without going through the symbol vendor so we 1250 // need to lock the module. 1251 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1252 // Anytime we have a lldb::user_id_t, we must get the DIE by calling 1253 // SymbolFileDWARF::GetDIE(). See comments inside the 1254 // SymbolFileDWARF::GetDIE() for details. 1255 if (DWARFDIE die = GetDIE(type_uid)) 1256 return die.GetDecl(); 1257 return CompilerDecl(); 1258 } 1259 1260 CompilerDeclContext 1261 SymbolFileDWARF::GetDeclContextForUID(lldb::user_id_t type_uid) { 1262 // This method can be called without going through the symbol vendor so we 1263 // need to lock the module. 1264 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1265 // Anytime we have a lldb::user_id_t, we must get the DIE by calling 1266 // SymbolFileDWARF::GetDIE(). See comments inside the 1267 // SymbolFileDWARF::GetDIE() for details. 1268 if (DWARFDIE die = GetDIE(type_uid)) 1269 return die.GetDeclContext(); 1270 return CompilerDeclContext(); 1271 } 1272 1273 CompilerDeclContext 1274 SymbolFileDWARF::GetDeclContextContainingUID(lldb::user_id_t type_uid) { 1275 // This method can be called without going through the symbol vendor so we 1276 // need to lock the module. 1277 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1278 // Anytime we have a lldb::user_id_t, we must get the DIE by calling 1279 // SymbolFileDWARF::GetDIE(). See comments inside the 1280 // SymbolFileDWARF::GetDIE() for details. 1281 if (DWARFDIE die = GetDIE(type_uid)) 1282 return die.GetContainingDeclContext(); 1283 return CompilerDeclContext(); 1284 } 1285 1286 Type *SymbolFileDWARF::ResolveTypeUID(lldb::user_id_t type_uid) { 1287 // This method can be called without going through the symbol vendor so we 1288 // need to lock the module. 1289 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1290 // Anytime we have a lldb::user_id_t, we must get the DIE by calling 1291 // SymbolFileDWARF::GetDIE(). See comments inside the 1292 // SymbolFileDWARF::GetDIE() for details. 1293 if (DWARFDIE type_die = GetDIE(type_uid)) 1294 return type_die.ResolveType(); 1295 else 1296 return nullptr; 1297 } 1298 1299 llvm::Optional<SymbolFile::ArrayInfo> 1300 SymbolFileDWARF::GetDynamicArrayInfoForUID( 1301 lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { 1302 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1303 if (DWARFDIE type_die = GetDIE(type_uid)) 1304 return DWARFASTParser::ParseChildArrayInfo(type_die, exe_ctx); 1305 else 1306 return llvm::None; 1307 } 1308 1309 Type *SymbolFileDWARF::ResolveTypeUID(const DIERef &die_ref) { 1310 return ResolveType(GetDIE(die_ref), true); 1311 } 1312 1313 Type *SymbolFileDWARF::ResolveTypeUID(const DWARFDIE &die, 1314 bool assert_not_being_parsed) { 1315 if (die) { 1316 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 1317 if (log) 1318 GetObjectFile()->GetModule()->LogMessage( 1319 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'", 1320 die.GetOffset(), die.GetTagAsCString(), die.GetName()); 1321 1322 // We might be coming in in the middle of a type tree (a class within a 1323 // class, an enum within a class), so parse any needed parent DIEs before 1324 // we get to this one... 1325 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(die); 1326 if (decl_ctx_die) { 1327 if (log) { 1328 switch (decl_ctx_die.Tag()) { 1329 case DW_TAG_structure_type: 1330 case DW_TAG_union_type: 1331 case DW_TAG_class_type: { 1332 // Get the type, which could be a forward declaration 1333 if (log) 1334 GetObjectFile()->GetModule()->LogMessage( 1335 log, "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' " 1336 "resolve parent forward type for 0x%8.8x", 1337 die.GetOffset(), die.GetTagAsCString(), die.GetName(), 1338 decl_ctx_die.GetOffset()); 1339 } break; 1340 1341 default: 1342 break; 1343 } 1344 } 1345 } 1346 return ResolveType(die); 1347 } 1348 return nullptr; 1349 } 1350 1351 // This function is used when SymbolFileDWARFDebugMap owns a bunch of 1352 // SymbolFileDWARF objects to detect if this DWARF file is the one that can 1353 // resolve a compiler_type. 1354 bool SymbolFileDWARF::HasForwardDeclForClangType( 1355 const CompilerType &compiler_type) { 1356 CompilerType compiler_type_no_qualifiers = 1357 ClangUtil::RemoveFastQualifiers(compiler_type); 1358 if (GetForwardDeclClangTypeToDie().count( 1359 compiler_type_no_qualifiers.GetOpaqueQualType())) { 1360 return true; 1361 } 1362 TypeSystem *type_system = compiler_type.GetTypeSystem(); 1363 1364 ClangASTContext *clang_type_system = 1365 llvm::dyn_cast_or_null<ClangASTContext>(type_system); 1366 if (!clang_type_system) 1367 return false; 1368 DWARFASTParserClang *ast_parser = 1369 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser()); 1370 return ast_parser->GetClangASTImporter().CanImport(compiler_type); 1371 } 1372 1373 bool SymbolFileDWARF::CompleteType(CompilerType &compiler_type) { 1374 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1375 1376 ClangASTContext *clang_type_system = 1377 llvm::dyn_cast_or_null<ClangASTContext>(compiler_type.GetTypeSystem()); 1378 if (clang_type_system) { 1379 DWARFASTParserClang *ast_parser = 1380 static_cast<DWARFASTParserClang *>(clang_type_system->GetDWARFParser()); 1381 if (ast_parser && 1382 ast_parser->GetClangASTImporter().CanImport(compiler_type)) 1383 return ast_parser->GetClangASTImporter().CompleteType(compiler_type); 1384 } 1385 1386 // We have a struct/union/class/enum that needs to be fully resolved. 1387 CompilerType compiler_type_no_qualifiers = 1388 ClangUtil::RemoveFastQualifiers(compiler_type); 1389 auto die_it = GetForwardDeclClangTypeToDie().find( 1390 compiler_type_no_qualifiers.GetOpaqueQualType()); 1391 if (die_it == GetForwardDeclClangTypeToDie().end()) { 1392 // We have already resolved this type... 1393 return true; 1394 } 1395 1396 DWARFDIE dwarf_die = GetDIE(die_it->getSecond()); 1397 if (dwarf_die) { 1398 // Once we start resolving this type, remove it from the forward 1399 // declaration map in case anyone child members or other types require this 1400 // type to get resolved. The type will get resolved when all of the calls 1401 // to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition are done. 1402 GetForwardDeclClangTypeToDie().erase(die_it); 1403 1404 Type *type = GetDIEToType().lookup(dwarf_die.GetDIE()); 1405 1406 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | 1407 DWARF_LOG_TYPE_COMPLETION)); 1408 if (log) 1409 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace( 1410 log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...", 1411 dwarf_die.GetID(), dwarf_die.GetTagAsCString(), 1412 type->GetName().AsCString()); 1413 assert(compiler_type); 1414 DWARFASTParser *dwarf_ast = dwarf_die.GetDWARFParser(); 1415 if (dwarf_ast) 1416 return dwarf_ast->CompleteTypeFromDWARF(dwarf_die, type, compiler_type); 1417 } 1418 return false; 1419 } 1420 1421 Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die, 1422 bool assert_not_being_parsed, 1423 bool resolve_function_context) { 1424 if (die) { 1425 Type *type = GetTypeForDIE(die, resolve_function_context).get(); 1426 1427 if (assert_not_being_parsed) { 1428 if (type != DIE_IS_BEING_PARSED) 1429 return type; 1430 1431 GetObjectFile()->GetModule()->ReportError( 1432 "Parsing a die that is being parsed die: 0x%8.8x: %s %s", 1433 die.GetOffset(), die.GetTagAsCString(), die.GetName()); 1434 1435 } else 1436 return type; 1437 } 1438 return nullptr; 1439 } 1440 1441 CompileUnit * 1442 SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) { 1443 // Check if the symbol vendor already knows about this compile unit? 1444 if (dwarf_cu.GetUserData() == nullptr) { 1445 // The symbol vendor doesn't know about this compile unit, we need to parse 1446 // and add it to the symbol vendor object. 1447 return ParseCompileUnit(dwarf_cu).get(); 1448 } 1449 return (CompileUnit *)dwarf_cu.GetUserData(); 1450 } 1451 1452 size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name, 1453 DIEArray &method_die_offsets) { 1454 method_die_offsets.clear(); 1455 m_index->GetObjCMethods(class_name, method_die_offsets); 1456 return method_die_offsets.size(); 1457 } 1458 1459 bool SymbolFileDWARF::GetFunction(const DWARFDIE &die, SymbolContext &sc) { 1460 sc.Clear(false); 1461 1462 if (die && llvm::isa<DWARFCompileUnit>(die.GetCU())) { 1463 // Check if the symbol vendor already knows about this compile unit? 1464 sc.comp_unit = 1465 GetCompUnitForDWARFCompUnit(llvm::cast<DWARFCompileUnit>(*die.GetCU())); 1466 1467 sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get(); 1468 if (sc.function == nullptr) 1469 sc.function = ParseFunction(*sc.comp_unit, die); 1470 1471 if (sc.function) { 1472 sc.module_sp = sc.function->CalculateSymbolContextModule(); 1473 return true; 1474 } 1475 } 1476 1477 return false; 1478 } 1479 1480 lldb::ModuleSP SymbolFileDWARF::GetDWOModule(ConstString name) { 1481 UpdateExternalModuleListIfNeeded(); 1482 const auto &pos = m_external_type_modules.find(name); 1483 if (pos != m_external_type_modules.end()) 1484 return pos->second; 1485 else 1486 return lldb::ModuleSP(); 1487 } 1488 1489 DWARFDIE 1490 SymbolFileDWARF::GetDIE(const DIERef &die_ref) { 1491 if (die_ref.dwo_num()) { 1492 return DebugInfo() 1493 ->GetUnitAtIndex(*die_ref.dwo_num()) 1494 ->GetDwoSymbolFile() 1495 ->GetDIE(die_ref); 1496 } 1497 1498 1499 DWARFDebugInfo *debug_info = DebugInfo(); 1500 if (debug_info) 1501 return debug_info->GetDIE(die_ref); 1502 else 1503 return DWARFDIE(); 1504 } 1505 1506 std::unique_ptr<SymbolFileDWARFDwo> 1507 SymbolFileDWARF::GetDwoSymbolFileForCompileUnit( 1508 DWARFUnit &unit, const DWARFDebugInfoEntry &cu_die) { 1509 // If we are using a dSYM file, we never want the standard DWO files since 1510 // the -gmodules support uses the same DWO machanism to specify full debug 1511 // info files for modules. 1512 if (GetDebugMapSymfile()) 1513 return nullptr; 1514 1515 DWARFCompileUnit *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(&unit); 1516 // Only compile units can be split into two parts. 1517 if (!dwarf_cu) 1518 return nullptr; 1519 1520 const char *dwo_name = 1521 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_GNU_dwo_name, nullptr); 1522 if (!dwo_name) 1523 return nullptr; 1524 1525 SymbolFileDWARFDwp *dwp_symfile = GetDwpSymbolFile(); 1526 if (dwp_symfile) { 1527 uint64_t dwo_id = 1528 cu_die.GetAttributeValueAsUnsigned(dwarf_cu, DW_AT_GNU_dwo_id, 0); 1529 std::unique_ptr<SymbolFileDWARFDwo> dwo_symfile = 1530 dwp_symfile->GetSymbolFileForDwoId(*dwarf_cu, dwo_id); 1531 if (dwo_symfile) 1532 return dwo_symfile; 1533 } 1534 1535 FileSpec dwo_file(dwo_name); 1536 FileSystem::Instance().Resolve(dwo_file); 1537 if (dwo_file.IsRelative()) { 1538 const char *comp_dir = 1539 cu_die.GetAttributeValueAsString(dwarf_cu, DW_AT_comp_dir, nullptr); 1540 if (!comp_dir) 1541 return nullptr; 1542 1543 dwo_file.SetFile(comp_dir, FileSpec::Style::native); 1544 FileSystem::Instance().Resolve(dwo_file); 1545 dwo_file.AppendPathComponent(dwo_name); 1546 } 1547 1548 if (!FileSystem::Instance().Exists(dwo_file)) 1549 return nullptr; 1550 1551 const lldb::offset_t file_offset = 0; 1552 DataBufferSP dwo_file_data_sp; 1553 lldb::offset_t dwo_file_data_offset = 0; 1554 ObjectFileSP dwo_obj_file = ObjectFile::FindPlugin( 1555 GetObjectFile()->GetModule(), &dwo_file, file_offset, 1556 FileSystem::Instance().GetByteSize(dwo_file), dwo_file_data_sp, 1557 dwo_file_data_offset); 1558 if (dwo_obj_file == nullptr) 1559 return nullptr; 1560 1561 return llvm::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu); 1562 } 1563 1564 void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() { 1565 if (m_fetched_external_modules) 1566 return; 1567 m_fetched_external_modules = true; 1568 1569 DWARFDebugInfo *debug_info = DebugInfo(); 1570 1571 const uint32_t num_compile_units = GetNumCompileUnits(); 1572 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) { 1573 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx); 1574 1575 const DWARFBaseDIE die = dwarf_cu->GetUnitDIEOnly(); 1576 if (die && !die.HasChildren()) { 1577 const char *name = die.GetAttributeValueAsString(DW_AT_name, nullptr); 1578 1579 if (name) { 1580 ConstString const_name(name); 1581 if (m_external_type_modules.find(const_name) == 1582 m_external_type_modules.end()) { 1583 ModuleSP module_sp; 1584 const char *dwo_path = 1585 die.GetAttributeValueAsString(DW_AT_GNU_dwo_name, nullptr); 1586 if (dwo_path) { 1587 ModuleSpec dwo_module_spec; 1588 dwo_module_spec.GetFileSpec().SetFile(dwo_path, 1589 FileSpec::Style::native); 1590 if (dwo_module_spec.GetFileSpec().IsRelative()) { 1591 const char *comp_dir = 1592 die.GetAttributeValueAsString(DW_AT_comp_dir, nullptr); 1593 if (comp_dir) { 1594 dwo_module_spec.GetFileSpec().SetFile(comp_dir, 1595 FileSpec::Style::native); 1596 FileSystem::Instance().Resolve(dwo_module_spec.GetFileSpec()); 1597 dwo_module_spec.GetFileSpec().AppendPathComponent(dwo_path); 1598 } 1599 } 1600 dwo_module_spec.GetArchitecture() = 1601 m_obj_file->GetModule()->GetArchitecture(); 1602 1603 // When LLDB loads "external" modules it looks at the presence of 1604 // DW_AT_GNU_dwo_name. However, when the already created module 1605 // (corresponding to .dwo itself) is being processed, it will see 1606 // the presence of DW_AT_GNU_dwo_name (which contains the name of 1607 // dwo file) and will try to call ModuleList::GetSharedModule 1608 // again. In some cases (i.e. for empty files) Clang 4.0 generates 1609 // a *.dwo file which has DW_AT_GNU_dwo_name, but no 1610 // DW_AT_comp_dir. In this case the method 1611 // ModuleList::GetSharedModule will fail and the warning will be 1612 // printed. However, as one can notice in this case we don't 1613 // actually need to try to load the already loaded module 1614 // (corresponding to .dwo) so we simply skip it. 1615 if (m_obj_file->GetFileSpec().GetFileNameExtension() == ".dwo" && 1616 llvm::StringRef(m_obj_file->GetFileSpec().GetPath()) 1617 .endswith(dwo_module_spec.GetFileSpec().GetPath())) { 1618 continue; 1619 } 1620 1621 Status error = ModuleList::GetSharedModule( 1622 dwo_module_spec, module_sp, nullptr, nullptr, nullptr); 1623 if (!module_sp) { 1624 GetObjectFile()->GetModule()->ReportWarning( 1625 "0x%8.8x: unable to locate module needed for external types: " 1626 "%s\nerror: %s\nDebugging will be degraded due to missing " 1627 "types. Rebuilding your project will regenerate the needed " 1628 "module files.", 1629 die.GetOffset(), 1630 dwo_module_spec.GetFileSpec().GetPath().c_str(), 1631 error.AsCString("unknown error")); 1632 } 1633 } 1634 m_external_type_modules[const_name] = module_sp; 1635 } 1636 } 1637 } 1638 } 1639 } 1640 1641 SymbolFileDWARF::GlobalVariableMap &SymbolFileDWARF::GetGlobalAranges() { 1642 if (!m_global_aranges_up) { 1643 m_global_aranges_up.reset(new GlobalVariableMap()); 1644 1645 ModuleSP module_sp = GetObjectFile()->GetModule(); 1646 if (module_sp) { 1647 const size_t num_cus = module_sp->GetNumCompileUnits(); 1648 for (size_t i = 0; i < num_cus; ++i) { 1649 CompUnitSP cu_sp = module_sp->GetCompileUnitAtIndex(i); 1650 if (cu_sp) { 1651 VariableListSP globals_sp = cu_sp->GetVariableList(true); 1652 if (globals_sp) { 1653 const size_t num_globals = globals_sp->GetSize(); 1654 for (size_t g = 0; g < num_globals; ++g) { 1655 VariableSP var_sp = globals_sp->GetVariableAtIndex(g); 1656 if (var_sp && !var_sp->GetLocationIsConstantValueData()) { 1657 const DWARFExpression &location = var_sp->LocationExpression(); 1658 Value location_result; 1659 Status error; 1660 if (location.Evaluate(nullptr, LLDB_INVALID_ADDRESS, nullptr, 1661 nullptr, location_result, &error)) { 1662 if (location_result.GetValueType() == 1663 Value::eValueTypeFileAddress) { 1664 lldb::addr_t file_addr = 1665 location_result.GetScalar().ULongLong(); 1666 lldb::addr_t byte_size = 1; 1667 if (var_sp->GetType()) 1668 byte_size = 1669 var_sp->GetType()->GetByteSize().getValueOr(0); 1670 m_global_aranges_up->Append(GlobalVariableMap::Entry( 1671 file_addr, byte_size, var_sp.get())); 1672 } 1673 } 1674 } 1675 } 1676 } 1677 } 1678 } 1679 } 1680 m_global_aranges_up->Sort(); 1681 } 1682 return *m_global_aranges_up; 1683 } 1684 1685 uint32_t SymbolFileDWARF::ResolveSymbolContext(const Address &so_addr, 1686 SymbolContextItem resolve_scope, 1687 SymbolContext &sc) { 1688 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 1689 Timer scoped_timer(func_cat, 1690 "SymbolFileDWARF::" 1691 "ResolveSymbolContext (so_addr = { " 1692 "section = %p, offset = 0x%" PRIx64 1693 " }, resolve_scope = 0x%8.8x)", 1694 static_cast<void *>(so_addr.GetSection().get()), 1695 so_addr.GetOffset(), resolve_scope); 1696 uint32_t resolved = 0; 1697 if (resolve_scope & 1698 (eSymbolContextCompUnit | eSymbolContextFunction | eSymbolContextBlock | 1699 eSymbolContextLineEntry | eSymbolContextVariable)) { 1700 lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); 1701 1702 DWARFDebugInfo *debug_info = DebugInfo(); 1703 if (debug_info) { 1704 llvm::Expected<DWARFDebugAranges &> aranges = 1705 debug_info->GetCompileUnitAranges(); 1706 if (!aranges) { 1707 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); 1708 LLDB_LOG_ERROR(log, aranges.takeError(), 1709 "SymbolFileDWARF::ResolveSymbolContext failed to get cu " 1710 "aranges. {0}"); 1711 return 0; 1712 } 1713 1714 const dw_offset_t cu_offset = aranges->FindAddress(file_vm_addr); 1715 if (cu_offset == DW_INVALID_OFFSET) { 1716 // Global variables are not in the compile unit address ranges. The 1717 // only way to currently find global variables is to iterate over the 1718 // .debug_pubnames or the __apple_names table and find all items in 1719 // there that point to DW_TAG_variable DIEs and then find the address 1720 // that matches. 1721 if (resolve_scope & eSymbolContextVariable) { 1722 GlobalVariableMap &map = GetGlobalAranges(); 1723 const GlobalVariableMap::Entry *entry = 1724 map.FindEntryThatContains(file_vm_addr); 1725 if (entry && entry->data) { 1726 Variable *variable = entry->data; 1727 SymbolContextScope *scc = variable->GetSymbolContextScope(); 1728 if (scc) { 1729 scc->CalculateSymbolContext(&sc); 1730 sc.variable = variable; 1731 } 1732 return sc.GetResolvedMask(); 1733 } 1734 } 1735 } else { 1736 uint32_t cu_idx = DW_INVALID_INDEX; 1737 if (auto *dwarf_cu = llvm::dyn_cast_or_null<DWARFCompileUnit>( 1738 debug_info->GetUnitAtOffset(DIERef::Section::DebugInfo, 1739 cu_offset, &cu_idx))) { 1740 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu); 1741 if (sc.comp_unit) { 1742 resolved |= eSymbolContextCompUnit; 1743 1744 bool force_check_line_table = false; 1745 if (resolve_scope & 1746 (eSymbolContextFunction | eSymbolContextBlock)) { 1747 DWARFDIE function_die = dwarf_cu->LookupAddress(file_vm_addr); 1748 DWARFDIE block_die; 1749 if (function_die) { 1750 sc.function = 1751 sc.comp_unit->FindFunctionByUID(function_die.GetID()).get(); 1752 if (sc.function == nullptr) 1753 sc.function = ParseFunction(*sc.comp_unit, function_die); 1754 1755 if (sc.function && (resolve_scope & eSymbolContextBlock)) 1756 block_die = function_die.LookupDeepestBlock(file_vm_addr); 1757 } else { 1758 // We might have had a compile unit that had discontiguous 1759 // address ranges where the gaps are symbols that don't have 1760 // any debug info. Discontiguous compile unit address ranges 1761 // should only happen when there aren't other functions from 1762 // other compile units in these gaps. This helps keep the size 1763 // of the aranges down. 1764 force_check_line_table = true; 1765 } 1766 1767 if (sc.function != nullptr) { 1768 resolved |= eSymbolContextFunction; 1769 1770 if (resolve_scope & eSymbolContextBlock) { 1771 Block &block = sc.function->GetBlock(true); 1772 1773 if (block_die) 1774 sc.block = block.FindBlockByID(block_die.GetID()); 1775 else 1776 sc.block = block.FindBlockByID(function_die.GetID()); 1777 if (sc.block) 1778 resolved |= eSymbolContextBlock; 1779 } 1780 } 1781 } 1782 1783 if ((resolve_scope & eSymbolContextLineEntry) || 1784 force_check_line_table) { 1785 LineTable *line_table = sc.comp_unit->GetLineTable(); 1786 if (line_table != nullptr) { 1787 // And address that makes it into this function should be in 1788 // terms of this debug file if there is no debug map, or it 1789 // will be an address in the .o file which needs to be fixed up 1790 // to be in terms of the debug map executable. Either way, 1791 // calling FixupAddress() will work for us. 1792 Address exe_so_addr(so_addr); 1793 if (FixupAddress(exe_so_addr)) { 1794 if (line_table->FindLineEntryByAddress(exe_so_addr, 1795 sc.line_entry)) { 1796 resolved |= eSymbolContextLineEntry; 1797 } 1798 } 1799 } 1800 } 1801 1802 if (force_check_line_table && 1803 !(resolved & eSymbolContextLineEntry)) { 1804 // We might have had a compile unit that had discontiguous 1805 // address ranges where the gaps are symbols that don't have any 1806 // debug info. Discontiguous compile unit address ranges should 1807 // only happen when there aren't other functions from other 1808 // compile units in these gaps. This helps keep the size of the 1809 // aranges down. 1810 sc.comp_unit = nullptr; 1811 resolved &= ~eSymbolContextCompUnit; 1812 } 1813 } else { 1814 GetObjectFile()->GetModule()->ReportWarning( 1815 "0x%8.8x: compile unit %u failed to create a valid " 1816 "lldb_private::CompileUnit class.", 1817 cu_offset, cu_idx); 1818 } 1819 } 1820 } 1821 } 1822 } 1823 return resolved; 1824 } 1825 1826 uint32_t SymbolFileDWARF::ResolveSymbolContext(const FileSpec &file_spec, 1827 uint32_t line, 1828 bool check_inlines, 1829 SymbolContextItem resolve_scope, 1830 SymbolContextList &sc_list) { 1831 const uint32_t prev_size = sc_list.GetSize(); 1832 if (resolve_scope & eSymbolContextCompUnit) { 1833 for (uint32_t cu_idx = 0, num_cus = GetNumCompileUnits(); cu_idx < num_cus; 1834 ++cu_idx) { 1835 CompileUnit *dc_cu = ParseCompileUnitAtIndex(cu_idx).get(); 1836 if (!dc_cu) 1837 continue; 1838 1839 const bool full_match = (bool)file_spec.GetDirectory(); 1840 bool file_spec_matches_cu_file_spec = 1841 FileSpec::Equal(file_spec, *dc_cu, full_match); 1842 if (check_inlines || file_spec_matches_cu_file_spec) { 1843 SymbolContext sc(m_obj_file->GetModule()); 1844 sc.comp_unit = dc_cu; 1845 uint32_t file_idx = UINT32_MAX; 1846 1847 // If we are looking for inline functions only and we don't find it 1848 // in the support files, we are done. 1849 if (check_inlines) { 1850 file_idx = 1851 sc.comp_unit->GetSupportFiles().FindFileIndex(1, file_spec, true); 1852 if (file_idx == UINT32_MAX) 1853 continue; 1854 } 1855 1856 if (line != 0) { 1857 LineTable *line_table = sc.comp_unit->GetLineTable(); 1858 1859 if (line_table != nullptr && line != 0) { 1860 // We will have already looked up the file index if we are 1861 // searching for inline entries. 1862 if (!check_inlines) 1863 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex( 1864 1, file_spec, true); 1865 1866 if (file_idx != UINT32_MAX) { 1867 uint32_t found_line; 1868 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex( 1869 0, file_idx, line, false, &sc.line_entry); 1870 found_line = sc.line_entry.line; 1871 1872 while (line_idx != UINT32_MAX) { 1873 sc.function = nullptr; 1874 sc.block = nullptr; 1875 if (resolve_scope & 1876 (eSymbolContextFunction | eSymbolContextBlock)) { 1877 const lldb::addr_t file_vm_addr = 1878 sc.line_entry.range.GetBaseAddress().GetFileAddress(); 1879 if (file_vm_addr != LLDB_INVALID_ADDRESS) { 1880 DWARFDIE function_die = 1881 GetDWARFCompileUnit(dc_cu)->LookupAddress(file_vm_addr); 1882 DWARFDIE block_die; 1883 if (function_die) { 1884 sc.function = 1885 sc.comp_unit->FindFunctionByUID(function_die.GetID()) 1886 .get(); 1887 if (sc.function == nullptr) 1888 sc.function = 1889 ParseFunction(*sc.comp_unit, function_die); 1890 1891 if (sc.function && (resolve_scope & eSymbolContextBlock)) 1892 block_die = 1893 function_die.LookupDeepestBlock(file_vm_addr); 1894 } 1895 1896 if (sc.function != nullptr) { 1897 Block &block = sc.function->GetBlock(true); 1898 1899 if (block_die) 1900 sc.block = block.FindBlockByID(block_die.GetID()); 1901 else if (function_die) 1902 sc.block = block.FindBlockByID(function_die.GetID()); 1903 } 1904 } 1905 } 1906 1907 sc_list.Append(sc); 1908 line_idx = line_table->FindLineEntryIndexByFileIndex( 1909 line_idx + 1, file_idx, found_line, true, &sc.line_entry); 1910 } 1911 } 1912 } else if (file_spec_matches_cu_file_spec && !check_inlines) { 1913 // only append the context if we aren't looking for inline call 1914 // sites by file and line and if the file spec matches that of 1915 // the compile unit 1916 sc_list.Append(sc); 1917 } 1918 } else if (file_spec_matches_cu_file_spec && !check_inlines) { 1919 // only append the context if we aren't looking for inline call 1920 // sites by file and line and if the file spec matches that of 1921 // the compile unit 1922 sc_list.Append(sc); 1923 } 1924 1925 if (!check_inlines) 1926 break; 1927 } 1928 } 1929 } 1930 return sc_list.GetSize() - prev_size; 1931 } 1932 1933 void SymbolFileDWARF::PreloadSymbols() { 1934 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); 1935 m_index->Preload(); 1936 } 1937 1938 std::recursive_mutex &SymbolFileDWARF::GetModuleMutex() const { 1939 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock()); 1940 if (module_sp) 1941 return module_sp->GetMutex(); 1942 return GetObjectFile()->GetModule()->GetMutex(); 1943 } 1944 1945 bool SymbolFileDWARF::DeclContextMatchesThisSymbolFile( 1946 const lldb_private::CompilerDeclContext *decl_ctx) { 1947 if (decl_ctx == nullptr || !decl_ctx->IsValid()) { 1948 // Invalid namespace decl which means we aren't matching only things in 1949 // this symbol file, so return true to indicate it matches this symbol 1950 // file. 1951 return true; 1952 } 1953 1954 TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); 1955 TypeSystem *type_system = GetTypeSystemForLanguage( 1956 decl_ctx_type_system->GetMinimumLanguage(nullptr)); 1957 if (decl_ctx_type_system == type_system) 1958 return true; // The type systems match, return true 1959 1960 // The namespace AST was valid, and it does not match... 1961 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 1962 1963 if (log) 1964 GetObjectFile()->GetModule()->LogMessage( 1965 log, "Valid namespace does not match symbol file"); 1966 1967 return false; 1968 } 1969 1970 uint32_t SymbolFileDWARF::FindGlobalVariables( 1971 ConstString name, const CompilerDeclContext *parent_decl_ctx, 1972 uint32_t max_matches, VariableList &variables) { 1973 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 1974 1975 if (log) 1976 GetObjectFile()->GetModule()->LogMessage( 1977 log, 1978 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", " 1979 "parent_decl_ctx=%p, max_matches=%u, variables)", 1980 name.GetCString(), static_cast<const void *>(parent_decl_ctx), 1981 max_matches); 1982 1983 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 1984 return 0; 1985 1986 DWARFDebugInfo *info = DebugInfo(); 1987 if (info == nullptr) 1988 return 0; 1989 1990 // Remember how many variables are in the list before we search. 1991 const uint32_t original_size = variables.GetSize(); 1992 1993 llvm::StringRef basename; 1994 llvm::StringRef context; 1995 bool name_is_mangled = (bool)Mangled(name); 1996 1997 if (!CPlusPlusLanguage::ExtractContextAndIdentifier(name.GetCString(), 1998 context, basename)) 1999 basename = name.GetStringRef(); 2000 2001 DIEArray die_offsets; 2002 m_index->GetGlobalVariables(ConstString(basename), die_offsets); 2003 const size_t num_die_matches = die_offsets.size(); 2004 if (num_die_matches) { 2005 SymbolContext sc; 2006 sc.module_sp = m_obj_file->GetModule(); 2007 assert(sc.module_sp); 2008 2009 // Loop invariant: Variables up to this index have been checked for context 2010 // matches. 2011 uint32_t pruned_idx = original_size; 2012 2013 bool done = false; 2014 for (size_t i = 0; i < num_die_matches && !done; ++i) { 2015 const DIERef &die_ref = die_offsets[i]; 2016 DWARFDIE die = GetDIE(die_ref); 2017 2018 if (die) { 2019 switch (die.Tag()) { 2020 default: 2021 case DW_TAG_subprogram: 2022 case DW_TAG_inlined_subroutine: 2023 case DW_TAG_try_block: 2024 case DW_TAG_catch_block: 2025 break; 2026 2027 case DW_TAG_variable: { 2028 auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()); 2029 if (!dwarf_cu) 2030 continue; 2031 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu); 2032 2033 if (parent_decl_ctx) { 2034 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 2035 if (dwarf_ast) { 2036 CompilerDeclContext actual_parent_decl_ctx = 2037 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die); 2038 if (!actual_parent_decl_ctx || 2039 actual_parent_decl_ctx != *parent_decl_ctx) 2040 continue; 2041 } 2042 } 2043 2044 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, 2045 &variables); 2046 while (pruned_idx < variables.GetSize()) { 2047 VariableSP var_sp = variables.GetVariableAtIndex(pruned_idx); 2048 if (name_is_mangled || 2049 var_sp->GetName().GetStringRef().contains(name.GetStringRef())) 2050 ++pruned_idx; 2051 else 2052 variables.RemoveVariableAtIndex(pruned_idx); 2053 } 2054 2055 if (variables.GetSize() - original_size >= max_matches) 2056 done = true; 2057 } break; 2058 } 2059 } else { 2060 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef()); 2061 } 2062 } 2063 } 2064 2065 // Return the number of variable that were appended to the list 2066 const uint32_t num_matches = variables.GetSize() - original_size; 2067 if (log && num_matches > 0) { 2068 GetObjectFile()->GetModule()->LogMessage( 2069 log, 2070 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", " 2071 "parent_decl_ctx=%p, max_matches=%u, variables) => %u", 2072 name.GetCString(), static_cast<const void *>(parent_decl_ctx), 2073 max_matches, num_matches); 2074 } 2075 return num_matches; 2076 } 2077 2078 uint32_t SymbolFileDWARF::FindGlobalVariables(const RegularExpression ®ex, 2079 uint32_t max_matches, 2080 VariableList &variables) { 2081 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2082 2083 if (log) { 2084 GetObjectFile()->GetModule()->LogMessage( 2085 log, 2086 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", " 2087 "max_matches=%u, variables)", 2088 regex.GetText().str().c_str(), max_matches); 2089 } 2090 2091 DWARFDebugInfo *info = DebugInfo(); 2092 if (info == nullptr) 2093 return 0; 2094 2095 // Remember how many variables are in the list before we search. 2096 const uint32_t original_size = variables.GetSize(); 2097 2098 DIEArray die_offsets; 2099 m_index->GetGlobalVariables(regex, die_offsets); 2100 2101 SymbolContext sc; 2102 sc.module_sp = m_obj_file->GetModule(); 2103 assert(sc.module_sp); 2104 2105 const size_t num_matches = die_offsets.size(); 2106 if (num_matches) { 2107 for (size_t i = 0; i < num_matches; ++i) { 2108 const DIERef &die_ref = die_offsets[i]; 2109 DWARFDIE die = GetDIE(die_ref); 2110 2111 if (die) { 2112 DWARFCompileUnit *dwarf_cu = 2113 llvm::dyn_cast<DWARFCompileUnit>(die.GetCU()); 2114 if (!dwarf_cu) 2115 continue; 2116 sc.comp_unit = GetCompUnitForDWARFCompUnit(*dwarf_cu); 2117 2118 ParseVariables(sc, die, LLDB_INVALID_ADDRESS, false, false, &variables); 2119 2120 if (variables.GetSize() - original_size >= max_matches) 2121 break; 2122 } else 2123 m_index->ReportInvalidDIERef(die_ref, regex.GetText()); 2124 } 2125 } 2126 2127 // Return the number of variable that were appended to the list 2128 return variables.GetSize() - original_size; 2129 } 2130 2131 bool SymbolFileDWARF::ResolveFunction(const DWARFDIE &orig_die, 2132 bool include_inlines, 2133 SymbolContextList &sc_list) { 2134 SymbolContext sc; 2135 2136 if (!orig_die) 2137 return false; 2138 2139 // If we were passed a die that is not a function, just return false... 2140 if (!(orig_die.Tag() == DW_TAG_subprogram || 2141 (include_inlines && orig_die.Tag() == DW_TAG_inlined_subroutine))) 2142 return false; 2143 2144 DWARFDIE die = orig_die; 2145 DWARFDIE inlined_die; 2146 if (die.Tag() == DW_TAG_inlined_subroutine) { 2147 inlined_die = die; 2148 2149 while (true) { 2150 die = die.GetParent(); 2151 2152 if (die) { 2153 if (die.Tag() == DW_TAG_subprogram) 2154 break; 2155 } else 2156 break; 2157 } 2158 } 2159 assert(die && die.Tag() == DW_TAG_subprogram); 2160 if (GetFunction(die, sc)) { 2161 Address addr; 2162 // Parse all blocks if needed 2163 if (inlined_die) { 2164 Block &function_block = sc.function->GetBlock(true); 2165 sc.block = function_block.FindBlockByID(inlined_die.GetID()); 2166 if (sc.block == nullptr) 2167 sc.block = function_block.FindBlockByID(inlined_die.GetOffset()); 2168 if (sc.block == nullptr || !sc.block->GetStartAddress(addr)) 2169 addr.Clear(); 2170 } else { 2171 sc.block = nullptr; 2172 addr = sc.function->GetAddressRange().GetBaseAddress(); 2173 } 2174 2175 if (addr.IsValid()) { 2176 sc_list.Append(sc); 2177 return true; 2178 } 2179 } 2180 2181 return false; 2182 } 2183 2184 bool SymbolFileDWARF::DIEInDeclContext(const CompilerDeclContext *decl_ctx, 2185 const DWARFDIE &die) { 2186 // If we have no parent decl context to match this DIE matches, and if the 2187 // parent decl context isn't valid, we aren't trying to look for any 2188 // particular decl context so any die matches. 2189 if (decl_ctx == nullptr || !decl_ctx->IsValid()) 2190 return true; 2191 2192 if (die) { 2193 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 2194 if (dwarf_ast) { 2195 CompilerDeclContext actual_decl_ctx = 2196 dwarf_ast->GetDeclContextContainingUIDFromDWARF(die); 2197 if (actual_decl_ctx) 2198 return decl_ctx->IsContainedInLookup(actual_decl_ctx); 2199 } 2200 } 2201 return false; 2202 } 2203 2204 uint32_t SymbolFileDWARF::FindFunctions( 2205 ConstString name, const CompilerDeclContext *parent_decl_ctx, 2206 FunctionNameType name_type_mask, bool include_inlines, bool append, 2207 SymbolContextList &sc_list) { 2208 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 2209 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (name = '%s')", 2210 name.AsCString()); 2211 2212 // eFunctionNameTypeAuto should be pre-resolved by a call to 2213 // Module::LookupInfo::LookupInfo() 2214 assert((name_type_mask & eFunctionNameTypeAuto) == 0); 2215 2216 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2217 2218 if (log) { 2219 GetObjectFile()->GetModule()->LogMessage( 2220 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", " 2221 "name_type_mask=0x%x, append=%u, sc_list)", 2222 name.GetCString(), name_type_mask, append); 2223 } 2224 2225 // If we aren't appending the results to this list, then clear the list 2226 if (!append) 2227 sc_list.Clear(); 2228 2229 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2230 return 0; 2231 2232 // If name is empty then we won't find anything. 2233 if (name.IsEmpty()) 2234 return 0; 2235 2236 // Remember how many sc_list are in the list before we search in case we are 2237 // appending the results to a variable list. 2238 2239 const uint32_t original_size = sc_list.GetSize(); 2240 2241 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies; 2242 DIEArray offsets; 2243 CompilerDeclContext empty_decl_ctx; 2244 if (!parent_decl_ctx) 2245 parent_decl_ctx = &empty_decl_ctx; 2246 2247 std::vector<DWARFDIE> dies; 2248 m_index->GetFunctions(name, *this, *parent_decl_ctx, name_type_mask, dies); 2249 for (const DWARFDIE &die: dies) { 2250 if (resolved_dies.insert(die.GetDIE()).second) 2251 ResolveFunction(die, include_inlines, sc_list); 2252 } 2253 2254 // Return the number of variable that were appended to the list 2255 const uint32_t num_matches = sc_list.GetSize() - original_size; 2256 2257 if (log && num_matches > 0) { 2258 GetObjectFile()->GetModule()->LogMessage( 2259 log, "SymbolFileDWARF::FindFunctions (name=\"%s\", " 2260 "name_type_mask=0x%x, include_inlines=%d, append=%u, sc_list) => " 2261 "%u", 2262 name.GetCString(), name_type_mask, include_inlines, append, 2263 num_matches); 2264 } 2265 return num_matches; 2266 } 2267 2268 uint32_t SymbolFileDWARF::FindFunctions(const RegularExpression ®ex, 2269 bool include_inlines, bool append, 2270 SymbolContextList &sc_list) { 2271 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 2272 Timer scoped_timer(func_cat, "SymbolFileDWARF::FindFunctions (regex = '%s')", 2273 regex.GetText().str().c_str()); 2274 2275 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2276 2277 if (log) { 2278 GetObjectFile()->GetModule()->LogMessage( 2279 log, 2280 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)", 2281 regex.GetText().str().c_str(), append); 2282 } 2283 2284 // If we aren't appending the results to this list, then clear the list 2285 if (!append) 2286 sc_list.Clear(); 2287 2288 DWARFDebugInfo *info = DebugInfo(); 2289 if (!info) 2290 return 0; 2291 2292 // Remember how many sc_list are in the list before we search in case we are 2293 // appending the results to a variable list. 2294 uint32_t original_size = sc_list.GetSize(); 2295 2296 DIEArray offsets; 2297 m_index->GetFunctions(regex, offsets); 2298 2299 llvm::DenseSet<const DWARFDebugInfoEntry *> resolved_dies; 2300 for (DIERef ref : offsets) { 2301 DWARFDIE die = info->GetDIE(ref); 2302 if (!die) { 2303 m_index->ReportInvalidDIERef(ref, regex.GetText()); 2304 continue; 2305 } 2306 if (resolved_dies.insert(die.GetDIE()).second) 2307 ResolveFunction(die, include_inlines, sc_list); 2308 } 2309 2310 // Return the number of variable that were appended to the list 2311 return sc_list.GetSize() - original_size; 2312 } 2313 2314 void SymbolFileDWARF::GetMangledNamesForFunction( 2315 const std::string &scope_qualified_name, 2316 std::vector<ConstString> &mangled_names) { 2317 DWARFDebugInfo *info = DebugInfo(); 2318 uint32_t num_comp_units = 0; 2319 if (info) 2320 num_comp_units = info->GetNumUnits(); 2321 2322 for (uint32_t i = 0; i < num_comp_units; i++) { 2323 DWARFUnit *cu = info->GetUnitAtIndex(i); 2324 if (cu == nullptr) 2325 continue; 2326 2327 SymbolFileDWARFDwo *dwo = cu->GetDwoSymbolFile(); 2328 if (dwo) 2329 dwo->GetMangledNamesForFunction(scope_qualified_name, mangled_names); 2330 } 2331 2332 for (lldb::user_id_t uid : 2333 m_function_scope_qualified_name_map.lookup(scope_qualified_name)) { 2334 DWARFDIE die = GetDIE(uid); 2335 mangled_names.push_back(ConstString(die.GetMangledName())); 2336 } 2337 } 2338 2339 uint32_t SymbolFileDWARF::FindTypes( 2340 ConstString name, const CompilerDeclContext *parent_decl_ctx, 2341 bool append, uint32_t max_matches, 2342 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 2343 TypeMap &types) { 2344 // If we aren't appending the results to this list, then clear the list 2345 if (!append) 2346 types.Clear(); 2347 2348 // Make sure we haven't already searched this SymbolFile before... 2349 if (searched_symbol_files.count(this)) 2350 return 0; 2351 else 2352 searched_symbol_files.insert(this); 2353 2354 DWARFDebugInfo *info = DebugInfo(); 2355 if (info == nullptr) 2356 return 0; 2357 2358 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2359 2360 if (log) { 2361 if (parent_decl_ctx) 2362 GetObjectFile()->GetModule()->LogMessage( 2363 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = " 2364 "%p (\"%s\"), append=%u, max_matches=%u, type_list)", 2365 name.GetCString(), static_cast<const void *>(parent_decl_ctx), 2366 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches); 2367 else 2368 GetObjectFile()->GetModule()->LogMessage( 2369 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx = " 2370 "NULL, append=%u, max_matches=%u, type_list)", 2371 name.GetCString(), append, max_matches); 2372 } 2373 2374 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2375 return 0; 2376 2377 DIEArray die_offsets; 2378 m_index->GetTypes(name, die_offsets); 2379 const size_t num_die_matches = die_offsets.size(); 2380 2381 if (num_die_matches) { 2382 const uint32_t initial_types_size = types.GetSize(); 2383 for (size_t i = 0; i < num_die_matches; ++i) { 2384 const DIERef &die_ref = die_offsets[i]; 2385 DWARFDIE die = GetDIE(die_ref); 2386 2387 if (die) { 2388 if (!DIEInDeclContext(parent_decl_ctx, die)) 2389 continue; // The containing decl contexts don't match 2390 2391 Type *matching_type = ResolveType(die, true, true); 2392 if (matching_type) { 2393 // We found a type pointer, now find the shared pointer form our type 2394 // list 2395 types.InsertUnique(matching_type->shared_from_this()); 2396 if (types.GetSize() >= max_matches) 2397 break; 2398 } 2399 } else { 2400 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef()); 2401 } 2402 } 2403 const uint32_t num_matches = types.GetSize() - initial_types_size; 2404 if (log && num_matches) { 2405 if (parent_decl_ctx) { 2406 GetObjectFile()->GetModule()->LogMessage( 2407 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx " 2408 "= %p (\"%s\"), append=%u, max_matches=%u, type_list) => %u", 2409 name.GetCString(), static_cast<const void *>(parent_decl_ctx), 2410 parent_decl_ctx->GetName().AsCString("<NULL>"), append, max_matches, 2411 num_matches); 2412 } else { 2413 GetObjectFile()->GetModule()->LogMessage( 2414 log, "SymbolFileDWARF::FindTypes (sc, name=\"%s\", parent_decl_ctx " 2415 "= NULL, append=%u, max_matches=%u, type_list) => %u", 2416 name.GetCString(), append, max_matches, num_matches); 2417 } 2418 } 2419 return num_matches; 2420 } else { 2421 UpdateExternalModuleListIfNeeded(); 2422 2423 for (const auto &pair : m_external_type_modules) { 2424 ModuleSP external_module_sp = pair.second; 2425 if (external_module_sp) { 2426 SymbolVendor *sym_vendor = external_module_sp->GetSymbolVendor(); 2427 if (sym_vendor) { 2428 const uint32_t num_external_matches = 2429 sym_vendor->FindTypes(name, parent_decl_ctx, append, max_matches, 2430 searched_symbol_files, types); 2431 if (num_external_matches) 2432 return num_external_matches; 2433 } 2434 } 2435 } 2436 } 2437 2438 return 0; 2439 } 2440 2441 size_t SymbolFileDWARF::FindTypes(const std::vector<CompilerContext> &context, 2442 bool append, TypeMap &types) { 2443 if (!append) 2444 types.Clear(); 2445 2446 if (context.empty()) 2447 return 0; 2448 2449 ConstString name = context.back().name; 2450 2451 if (!name) 2452 return 0; 2453 2454 DIEArray die_offsets; 2455 m_index->GetTypes(name, die_offsets); 2456 const size_t num_die_matches = die_offsets.size(); 2457 2458 if (num_die_matches) { 2459 size_t num_matches = 0; 2460 for (size_t i = 0; i < num_die_matches; ++i) { 2461 const DIERef &die_ref = die_offsets[i]; 2462 DWARFDIE die = GetDIE(die_ref); 2463 2464 if (die) { 2465 std::vector<CompilerContext> die_context; 2466 die.GetDeclContext(die_context); 2467 if (die_context != context) 2468 continue; 2469 2470 Type *matching_type = ResolveType(die, true, true); 2471 if (matching_type) { 2472 // We found a type pointer, now find the shared pointer form our type 2473 // list 2474 types.InsertUnique(matching_type->shared_from_this()); 2475 ++num_matches; 2476 } 2477 } else { 2478 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef()); 2479 } 2480 } 2481 return num_matches; 2482 } 2483 return 0; 2484 } 2485 2486 CompilerDeclContext 2487 SymbolFileDWARF::FindNamespace(ConstString name, 2488 const CompilerDeclContext *parent_decl_ctx) { 2489 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS)); 2490 2491 if (log) { 2492 GetObjectFile()->GetModule()->LogMessage( 2493 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")", 2494 name.GetCString()); 2495 } 2496 2497 CompilerDeclContext namespace_decl_ctx; 2498 2499 if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) 2500 return namespace_decl_ctx; 2501 2502 DWARFDebugInfo *info = DebugInfo(); 2503 if (info) { 2504 DIEArray die_offsets; 2505 m_index->GetNamespaces(name, die_offsets); 2506 const size_t num_matches = die_offsets.size(); 2507 if (num_matches) { 2508 for (size_t i = 0; i < num_matches; ++i) { 2509 const DIERef &die_ref = die_offsets[i]; 2510 DWARFDIE die = GetDIE(die_ref); 2511 2512 if (die) { 2513 if (!DIEInDeclContext(parent_decl_ctx, die)) 2514 continue; // The containing decl contexts don't match 2515 2516 DWARFASTParser *dwarf_ast = die.GetDWARFParser(); 2517 if (dwarf_ast) { 2518 namespace_decl_ctx = dwarf_ast->GetDeclContextForUIDFromDWARF(die); 2519 if (namespace_decl_ctx) 2520 break; 2521 } 2522 } else { 2523 m_index->ReportInvalidDIERef(die_ref, name.GetStringRef()); 2524 } 2525 } 2526 } 2527 } 2528 if (log && namespace_decl_ctx) { 2529 GetObjectFile()->GetModule()->LogMessage( 2530 log, "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => " 2531 "CompilerDeclContext(%p/%p) \"%s\"", 2532 name.GetCString(), 2533 static_cast<const void *>(namespace_decl_ctx.GetTypeSystem()), 2534 static_cast<const void *>(namespace_decl_ctx.GetOpaqueDeclContext()), 2535 namespace_decl_ctx.GetName().AsCString("<NULL>")); 2536 } 2537 2538 return namespace_decl_ctx; 2539 } 2540 2541 TypeSP SymbolFileDWARF::GetTypeForDIE(const DWARFDIE &die, 2542 bool resolve_function_context) { 2543 TypeSP type_sp; 2544 if (die) { 2545 Type *type_ptr = GetDIEToType().lookup(die.GetDIE()); 2546 if (type_ptr == nullptr) { 2547 SymbolContextScope *scope; 2548 if (auto *dwarf_cu = llvm::dyn_cast<DWARFCompileUnit>(die.GetCU())) 2549 scope = GetCompUnitForDWARFCompUnit(*dwarf_cu); 2550 else 2551 scope = GetObjectFile()->GetModule().get(); 2552 assert(scope); 2553 SymbolContext sc(scope); 2554 const DWARFDebugInfoEntry *parent_die = die.GetParent().GetDIE(); 2555 while (parent_die != nullptr) { 2556 if (parent_die->Tag() == DW_TAG_subprogram) 2557 break; 2558 parent_die = parent_die->GetParent(); 2559 } 2560 SymbolContext sc_backup = sc; 2561 if (resolve_function_context && parent_die != nullptr && 2562 !GetFunction(DWARFDIE(die.GetCU(), parent_die), sc)) 2563 sc = sc_backup; 2564 2565 type_sp = ParseType(sc, die, nullptr); 2566 } else if (type_ptr != DIE_IS_BEING_PARSED) { 2567 // Grab the existing type from the master types lists 2568 type_sp = type_ptr->shared_from_this(); 2569 } 2570 } 2571 return type_sp; 2572 } 2573 2574 DWARFDIE 2575 SymbolFileDWARF::GetDeclContextDIEContainingDIE(const DWARFDIE &orig_die) { 2576 if (orig_die) { 2577 DWARFDIE die = orig_die; 2578 2579 while (die) { 2580 // If this is the original DIE that we are searching for a declaration 2581 // for, then don't look in the cache as we don't want our own decl 2582 // context to be our decl context... 2583 if (orig_die != die) { 2584 switch (die.Tag()) { 2585 case DW_TAG_compile_unit: 2586 case DW_TAG_partial_unit: 2587 case DW_TAG_namespace: 2588 case DW_TAG_structure_type: 2589 case DW_TAG_union_type: 2590 case DW_TAG_class_type: 2591 case DW_TAG_lexical_block: 2592 case DW_TAG_subprogram: 2593 return die; 2594 case DW_TAG_inlined_subroutine: { 2595 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin); 2596 if (abs_die) { 2597 return abs_die; 2598 } 2599 break; 2600 } 2601 default: 2602 break; 2603 } 2604 } 2605 2606 DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification); 2607 if (spec_die) { 2608 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(spec_die); 2609 if (decl_ctx_die) 2610 return decl_ctx_die; 2611 } 2612 2613 DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin); 2614 if (abs_die) { 2615 DWARFDIE decl_ctx_die = GetDeclContextDIEContainingDIE(abs_die); 2616 if (decl_ctx_die) 2617 return decl_ctx_die; 2618 } 2619 2620 die = die.GetParent(); 2621 } 2622 } 2623 return DWARFDIE(); 2624 } 2625 2626 Symbol * 2627 SymbolFileDWARF::GetObjCClassSymbol(ConstString objc_class_name) { 2628 Symbol *objc_class_symbol = nullptr; 2629 if (m_obj_file) { 2630 Symtab *symtab = m_obj_file->GetSymtab(); 2631 if (symtab) { 2632 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType( 2633 objc_class_name, eSymbolTypeObjCClass, Symtab::eDebugNo, 2634 Symtab::eVisibilityAny); 2635 } 2636 } 2637 return objc_class_symbol; 2638 } 2639 2640 // Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If 2641 // they don't then we can end up looking through all class types for a complete 2642 // type and never find the full definition. We need to know if this attribute 2643 // is supported, so we determine this here and cache th result. We also need to 2644 // worry about the debug map 2645 // DWARF file 2646 // if we are doing darwin DWARF in .o file debugging. 2647 bool SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type( 2648 DWARFUnit *cu) { 2649 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate) { 2650 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo; 2651 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type()) 2652 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 2653 else { 2654 DWARFDebugInfo *debug_info = DebugInfo(); 2655 const uint32_t num_compile_units = GetNumCompileUnits(); 2656 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) { 2657 DWARFUnit *dwarf_cu = debug_info->GetUnitAtIndex(cu_idx); 2658 if (dwarf_cu != cu && 2659 dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type()) { 2660 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes; 2661 break; 2662 } 2663 } 2664 } 2665 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && 2666 GetDebugMapSymfile()) 2667 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type(this); 2668 } 2669 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes; 2670 } 2671 2672 // This function can be used when a DIE is found that is a forward declaration 2673 // DIE and we want to try and find a type that has the complete definition. 2674 TypeSP SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE( 2675 const DWARFDIE &die, ConstString type_name, 2676 bool must_be_implementation) { 2677 2678 TypeSP type_sp; 2679 2680 if (!type_name || (must_be_implementation && !GetObjCClassSymbol(type_name))) 2681 return type_sp; 2682 2683 DIEArray die_offsets; 2684 m_index->GetCompleteObjCClass(type_name, must_be_implementation, die_offsets); 2685 2686 const size_t num_matches = die_offsets.size(); 2687 2688 if (num_matches) { 2689 for (size_t i = 0; i < num_matches; ++i) { 2690 const DIERef &die_ref = die_offsets[i]; 2691 DWARFDIE type_die = GetDIE(die_ref); 2692 2693 if (type_die) { 2694 bool try_resolving_type = false; 2695 2696 // Don't try and resolve the DIE we are looking for with the DIE 2697 // itself! 2698 if (type_die != die) { 2699 switch (type_die.Tag()) { 2700 case DW_TAG_class_type: 2701 case DW_TAG_structure_type: 2702 try_resolving_type = true; 2703 break; 2704 default: 2705 break; 2706 } 2707 } 2708 2709 if (try_resolving_type) { 2710 if (must_be_implementation && 2711 type_die.Supports_DW_AT_APPLE_objc_complete_type()) 2712 try_resolving_type = type_die.GetAttributeValueAsUnsigned( 2713 DW_AT_APPLE_objc_complete_type, 0); 2714 2715 if (try_resolving_type) { 2716 Type *resolved_type = ResolveType(type_die, false, true); 2717 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) { 2718 DEBUG_PRINTF("resolved 0x%8.8" PRIx64 " from %s to 0x%8.8" PRIx64 2719 " (cu 0x%8.8" PRIx64 ")\n", 2720 die.GetID(), 2721 m_obj_file->GetFileSpec().GetFilename().AsCString( 2722 "<Unknown>"), 2723 type_die.GetID(), type_cu->GetID()); 2724 2725 if (die) 2726 GetDIEToType()[die.GetDIE()] = resolved_type; 2727 type_sp = resolved_type->shared_from_this(); 2728 break; 2729 } 2730 } 2731 } 2732 } else { 2733 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef()); 2734 } 2735 } 2736 } 2737 return type_sp; 2738 } 2739 2740 // This function helps to ensure that the declaration contexts match for two 2741 // different DIEs. Often times debug information will refer to a forward 2742 // declaration of a type (the equivalent of "struct my_struct;". There will 2743 // often be a declaration of that type elsewhere that has the full definition. 2744 // When we go looking for the full type "my_struct", we will find one or more 2745 // matches in the accelerator tables and we will then need to make sure the 2746 // type was in the same declaration context as the original DIE. This function 2747 // can efficiently compare two DIEs and will return true when the declaration 2748 // context matches, and false when they don't. 2749 bool SymbolFileDWARF::DIEDeclContextsMatch(const DWARFDIE &die1, 2750 const DWARFDIE &die2) { 2751 if (die1 == die2) 2752 return true; 2753 2754 std::vector<DWARFDIE> decl_ctx_1; 2755 std::vector<DWARFDIE> decl_ctx_2; 2756 // The declaration DIE stack is a stack of the declaration context DIEs all 2757 // the way back to the compile unit. If a type "T" is declared inside a class 2758 // "B", and class "B" is declared inside a class "A" and class "A" is in a 2759 // namespace "lldb", and the namespace is in a compile unit, there will be a 2760 // stack of DIEs: 2761 // 2762 // [0] DW_TAG_class_type for "B" 2763 // [1] DW_TAG_class_type for "A" 2764 // [2] DW_TAG_namespace for "lldb" 2765 // [3] DW_TAG_compile_unit or DW_TAG_partial_unit for the source file. 2766 // 2767 // We grab both contexts and make sure that everything matches all the way 2768 // back to the compiler unit. 2769 2770 // First lets grab the decl contexts for both DIEs 2771 decl_ctx_1 = die1.GetDeclContextDIEs(); 2772 decl_ctx_2 = die2.GetDeclContextDIEs(); 2773 // Make sure the context arrays have the same size, otherwise we are done 2774 const size_t count1 = decl_ctx_1.size(); 2775 const size_t count2 = decl_ctx_2.size(); 2776 if (count1 != count2) 2777 return false; 2778 2779 // Make sure the DW_TAG values match all the way back up the compile unit. If 2780 // they don't, then we are done. 2781 DWARFDIE decl_ctx_die1; 2782 DWARFDIE decl_ctx_die2; 2783 size_t i; 2784 for (i = 0; i < count1; i++) { 2785 decl_ctx_die1 = decl_ctx_1[i]; 2786 decl_ctx_die2 = decl_ctx_2[i]; 2787 if (decl_ctx_die1.Tag() != decl_ctx_die2.Tag()) 2788 return false; 2789 } 2790 #ifndef NDEBUG 2791 2792 // Make sure the top item in the decl context die array is always 2793 // DW_TAG_compile_unit or DW_TAG_partial_unit. If it isn't then 2794 // something went wrong in the DWARFDIE::GetDeclContextDIEs() 2795 // function. 2796 dw_tag_t cu_tag = decl_ctx_1[count1 - 1].Tag(); 2797 UNUSED_IF_ASSERT_DISABLED(cu_tag); 2798 assert(cu_tag == DW_TAG_compile_unit || cu_tag == DW_TAG_partial_unit); 2799 2800 #endif 2801 // Always skip the compile unit when comparing by only iterating up to "count 2802 // - 1". Here we compare the names as we go. 2803 for (i = 0; i < count1 - 1; i++) { 2804 decl_ctx_die1 = decl_ctx_1[i]; 2805 decl_ctx_die2 = decl_ctx_2[i]; 2806 const char *name1 = decl_ctx_die1.GetName(); 2807 const char *name2 = decl_ctx_die2.GetName(); 2808 // If the string was from a DW_FORM_strp, then the pointer will often be 2809 // the same! 2810 if (name1 == name2) 2811 continue; 2812 2813 // Name pointers are not equal, so only compare the strings if both are not 2814 // NULL. 2815 if (name1 && name2) { 2816 // If the strings don't compare, we are done... 2817 if (strcmp(name1, name2) != 0) 2818 return false; 2819 } else { 2820 // One name was NULL while the other wasn't 2821 return false; 2822 } 2823 } 2824 // We made it through all of the checks and the declaration contexts are 2825 // equal. 2826 return true; 2827 } 2828 2829 TypeSP SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext( 2830 const DWARFDeclContext &dwarf_decl_ctx) { 2831 TypeSP type_sp; 2832 2833 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize(); 2834 if (dwarf_decl_ctx_count > 0) { 2835 const ConstString type_name(dwarf_decl_ctx[0].name); 2836 const dw_tag_t tag = dwarf_decl_ctx[0].tag; 2837 2838 if (type_name) { 2839 Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION | 2840 DWARF_LOG_LOOKUPS)); 2841 if (log) { 2842 GetObjectFile()->GetModule()->LogMessage( 2843 log, "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%" 2844 "s, qualified-name='%s')", 2845 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 2846 dwarf_decl_ctx.GetQualifiedName()); 2847 } 2848 2849 DIEArray die_offsets; 2850 m_index->GetTypes(dwarf_decl_ctx, die_offsets); 2851 const size_t num_matches = die_offsets.size(); 2852 2853 // Get the type system that we are looking to find a type for. We will 2854 // use this to ensure any matches we find are in a language that this 2855 // type system supports 2856 const LanguageType language = dwarf_decl_ctx.GetLanguage(); 2857 TypeSystem *type_system = (language == eLanguageTypeUnknown) 2858 ? nullptr 2859 : GetTypeSystemForLanguage(language); 2860 2861 if (num_matches) { 2862 for (size_t i = 0; i < num_matches; ++i) { 2863 const DIERef &die_ref = die_offsets[i]; 2864 DWARFDIE type_die = GetDIE(die_ref); 2865 2866 if (type_die) { 2867 // Make sure type_die's langauge matches the type system we are 2868 // looking for. We don't want to find a "Foo" type from Java if we 2869 // are looking for a "Foo" type for C, C++, ObjC, or ObjC++. 2870 if (type_system && 2871 !type_system->SupportsLanguage(type_die.GetLanguage())) 2872 continue; 2873 bool try_resolving_type = false; 2874 2875 // Don't try and resolve the DIE we are looking for with the DIE 2876 // itself! 2877 const dw_tag_t type_tag = type_die.Tag(); 2878 // Make sure the tags match 2879 if (type_tag == tag) { 2880 // The tags match, lets try resolving this type 2881 try_resolving_type = true; 2882 } else { 2883 // The tags don't match, but we need to watch our for a forward 2884 // declaration for a struct and ("struct foo") ends up being a 2885 // class ("class foo { ... };") or vice versa. 2886 switch (type_tag) { 2887 case DW_TAG_class_type: 2888 // We had a "class foo", see if we ended up with a "struct foo 2889 // { ... };" 2890 try_resolving_type = (tag == DW_TAG_structure_type); 2891 break; 2892 case DW_TAG_structure_type: 2893 // We had a "struct foo", see if we ended up with a "class foo 2894 // { ... };" 2895 try_resolving_type = (tag == DW_TAG_class_type); 2896 break; 2897 default: 2898 // Tags don't match, don't event try to resolve using this type 2899 // whose name matches.... 2900 break; 2901 } 2902 } 2903 2904 if (try_resolving_type) { 2905 DWARFDeclContext type_dwarf_decl_ctx; 2906 type_die.GetDWARFDeclContext(type_dwarf_decl_ctx); 2907 2908 if (log) { 2909 GetObjectFile()->GetModule()->LogMessage( 2910 log, "SymbolFileDWARF::" 2911 "FindDefinitionTypeForDWARFDeclContext(tag=%s, " 2912 "qualified-name='%s') trying die=0x%8.8x (%s)", 2913 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 2914 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(), 2915 type_dwarf_decl_ctx.GetQualifiedName()); 2916 } 2917 2918 // Make sure the decl contexts match all the way up 2919 if (dwarf_decl_ctx == type_dwarf_decl_ctx) { 2920 Type *resolved_type = ResolveType(type_die, false); 2921 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED) { 2922 type_sp = resolved_type->shared_from_this(); 2923 break; 2924 } 2925 } 2926 } else { 2927 if (log) { 2928 std::string qualified_name; 2929 type_die.GetQualifiedName(qualified_name); 2930 GetObjectFile()->GetModule()->LogMessage( 2931 log, "SymbolFileDWARF::" 2932 "FindDefinitionTypeForDWARFDeclContext(tag=%s, " 2933 "qualified-name='%s') ignoring die=0x%8.8x (%s)", 2934 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag), 2935 dwarf_decl_ctx.GetQualifiedName(), type_die.GetOffset(), 2936 qualified_name.c_str()); 2937 } 2938 } 2939 } else { 2940 m_index->ReportInvalidDIERef(die_ref, type_name.GetStringRef()); 2941 } 2942 } 2943 } 2944 } 2945 } 2946 return type_sp; 2947 } 2948 2949 TypeSP SymbolFileDWARF::ParseType(const SymbolContext &sc, const DWARFDIE &die, 2950 bool *type_is_new_ptr) { 2951 if (!die) 2952 return {}; 2953 2954 TypeSystem *type_system = 2955 GetTypeSystemForLanguage(die.GetCU()->GetLanguageType()); 2956 if (!type_system) 2957 return {}; 2958 2959 DWARFASTParser *dwarf_ast = type_system->GetDWARFParser(); 2960 if (!dwarf_ast) 2961 return {}; 2962 2963 Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO); 2964 TypeSP type_sp = dwarf_ast->ParseTypeFromDWARF(sc, die, log, type_is_new_ptr); 2965 if (type_sp) { 2966 GetTypeList().Insert(type_sp); 2967 2968 if (die.Tag() == DW_TAG_subprogram) { 2969 std::string scope_qualified_name(GetDeclContextForUID(die.GetID()) 2970 .GetScopeQualifiedName() 2971 .AsCString("")); 2972 if (scope_qualified_name.size()) { 2973 m_function_scope_qualified_name_map[scope_qualified_name].insert( 2974 die.GetID()); 2975 } 2976 } 2977 } 2978 2979 return type_sp; 2980 } 2981 2982 size_t SymbolFileDWARF::ParseTypes(const SymbolContext &sc, 2983 const DWARFDIE &orig_die, 2984 bool parse_siblings, bool parse_children) { 2985 size_t types_added = 0; 2986 DWARFDIE die = orig_die; 2987 while (die) { 2988 bool type_is_new = false; 2989 if (ParseType(sc, die, &type_is_new).get()) { 2990 if (type_is_new) 2991 ++types_added; 2992 } 2993 2994 if (parse_children && die.HasChildren()) { 2995 if (die.Tag() == DW_TAG_subprogram) { 2996 SymbolContext child_sc(sc); 2997 child_sc.function = sc.comp_unit->FindFunctionByUID(die.GetID()).get(); 2998 types_added += ParseTypes(child_sc, die.GetFirstChild(), true, true); 2999 } else 3000 types_added += ParseTypes(sc, die.GetFirstChild(), true, true); 3001 } 3002 3003 if (parse_siblings) 3004 die = die.GetSibling(); 3005 else 3006 die.Clear(); 3007 } 3008 return types_added; 3009 } 3010 3011 size_t SymbolFileDWARF::ParseBlocksRecursive(Function &func) { 3012 ASSERT_MODULE_LOCK(this); 3013 CompileUnit *comp_unit = func.GetCompileUnit(); 3014 lldbassert(comp_unit); 3015 3016 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(comp_unit); 3017 if (!dwarf_cu) 3018 return 0; 3019 3020 size_t functions_added = 0; 3021 const dw_offset_t function_die_offset = func.GetID(); 3022 DWARFDIE function_die = dwarf_cu->GetDIE(function_die_offset); 3023 if (function_die) { 3024 ParseBlocksRecursive(*comp_unit, &func.GetBlock(false), function_die, 3025 LLDB_INVALID_ADDRESS, 0); 3026 } 3027 3028 return functions_added; 3029 } 3030 3031 size_t SymbolFileDWARF::ParseTypes(CompileUnit &comp_unit) { 3032 ASSERT_MODULE_LOCK(this); 3033 size_t types_added = 0; 3034 DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); 3035 if (dwarf_cu) { 3036 DWARFDIE dwarf_cu_die = dwarf_cu->DIE(); 3037 if (dwarf_cu_die && dwarf_cu_die.HasChildren()) { 3038 SymbolContext sc; 3039 sc.comp_unit = &comp_unit; 3040 types_added = ParseTypes(sc, dwarf_cu_die.GetFirstChild(), true, true); 3041 } 3042 } 3043 3044 return types_added; 3045 } 3046 3047 size_t SymbolFileDWARF::ParseVariablesForContext(const SymbolContext &sc) { 3048 ASSERT_MODULE_LOCK(this); 3049 if (sc.comp_unit != nullptr) { 3050 DWARFDebugInfo *info = DebugInfo(); 3051 if (info == nullptr) 3052 return 0; 3053 3054 if (sc.function) { 3055 DWARFDIE function_die = GetDIE(sc.function->GetID()); 3056 3057 const dw_addr_t func_lo_pc = function_die.GetAttributeValueAsAddress( 3058 DW_AT_low_pc, LLDB_INVALID_ADDRESS); 3059 if (func_lo_pc != LLDB_INVALID_ADDRESS) { 3060 const size_t num_variables = ParseVariables( 3061 sc, function_die.GetFirstChild(), func_lo_pc, true, true); 3062 3063 // Let all blocks know they have parse all their variables 3064 sc.function->GetBlock(false).SetDidParseVariables(true, true); 3065 return num_variables; 3066 } 3067 } else if (sc.comp_unit) { 3068 DWARFUnit *dwarf_cu = info->GetUnitAtIndex(sc.comp_unit->GetID()); 3069 3070 if (dwarf_cu == nullptr) 3071 return 0; 3072 3073 uint32_t vars_added = 0; 3074 VariableListSP variables(sc.comp_unit->GetVariableList(false)); 3075 3076 if (variables.get() == nullptr) { 3077 variables = std::make_shared<VariableList>(); 3078 sc.comp_unit->SetVariableList(variables); 3079 3080 DIEArray die_offsets; 3081 m_index->GetGlobalVariables(dwarf_cu->GetNonSkeletonUnit(), 3082 die_offsets); 3083 const size_t num_matches = die_offsets.size(); 3084 if (num_matches) { 3085 for (size_t i = 0; i < num_matches; ++i) { 3086 const DIERef &die_ref = die_offsets[i]; 3087 DWARFDIE die = GetDIE(die_ref); 3088 if (die) { 3089 VariableSP var_sp( 3090 ParseVariableDIE(sc, die, LLDB_INVALID_ADDRESS)); 3091 if (var_sp) { 3092 variables->AddVariableIfUnique(var_sp); 3093 ++vars_added; 3094 } 3095 } else 3096 m_index->ReportInvalidDIERef(die_ref, ""); 3097 } 3098 } 3099 } 3100 return vars_added; 3101 } 3102 } 3103 return 0; 3104 } 3105 3106 VariableSP SymbolFileDWARF::ParseVariableDIE(const SymbolContext &sc, 3107 const DWARFDIE &die, 3108 const lldb::addr_t func_low_pc) { 3109 if (die.GetDWARF() != this) 3110 return die.GetDWARF()->ParseVariableDIE(sc, die, func_low_pc); 3111 3112 VariableSP var_sp; 3113 if (!die) 3114 return var_sp; 3115 3116 var_sp = GetDIEToVariable()[die.GetDIE()]; 3117 if (var_sp) 3118 return var_sp; // Already been parsed! 3119 3120 const dw_tag_t tag = die.Tag(); 3121 ModuleSP module = GetObjectFile()->GetModule(); 3122 3123 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) || 3124 (tag == DW_TAG_formal_parameter && sc.function)) { 3125 DWARFAttributes attributes; 3126 const size_t num_attributes = die.GetAttributes(attributes); 3127 DWARFDIE spec_die; 3128 if (num_attributes > 0) { 3129 const char *name = nullptr; 3130 const char *mangled = nullptr; 3131 Declaration decl; 3132 uint32_t i; 3133 DWARFFormValue type_die_form; 3134 DWARFExpression location; 3135 bool is_external = false; 3136 bool is_artificial = false; 3137 bool location_is_const_value_data = false; 3138 bool has_explicit_location = false; 3139 DWARFFormValue const_value; 3140 Variable::RangeList scope_ranges; 3141 // AccessType accessibility = eAccessNone; 3142 3143 for (i = 0; i < num_attributes; ++i) { 3144 dw_attr_t attr = attributes.AttributeAtIndex(i); 3145 DWARFFormValue form_value; 3146 3147 if (attributes.ExtractFormValueAtIndex(i, form_value)) { 3148 switch (attr) { 3149 case DW_AT_decl_file: 3150 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex( 3151 form_value.Unsigned())); 3152 break; 3153 case DW_AT_decl_line: 3154 decl.SetLine(form_value.Unsigned()); 3155 break; 3156 case DW_AT_decl_column: 3157 decl.SetColumn(form_value.Unsigned()); 3158 break; 3159 case DW_AT_name: 3160 name = form_value.AsCString(); 3161 break; 3162 case DW_AT_linkage_name: 3163 case DW_AT_MIPS_linkage_name: 3164 mangled = form_value.AsCString(); 3165 break; 3166 case DW_AT_type: 3167 type_die_form = form_value; 3168 break; 3169 case DW_AT_external: 3170 is_external = form_value.Boolean(); 3171 break; 3172 case DW_AT_const_value: 3173 // If we have already found a DW_AT_location attribute, ignore this 3174 // attribute. 3175 if (!has_explicit_location) { 3176 location_is_const_value_data = true; 3177 // The constant value will be either a block, a data value or a 3178 // string. 3179 auto debug_info_data = die.GetData(); 3180 if (DWARFFormValue::IsBlockForm(form_value.Form())) { 3181 // Retrieve the value as a block expression. 3182 uint32_t block_offset = 3183 form_value.BlockData() - debug_info_data.GetDataStart(); 3184 uint32_t block_length = form_value.Unsigned(); 3185 location = DWARFExpression(module, debug_info_data, die.GetCU(), 3186 block_offset, block_length); 3187 } else if (DWARFFormValue::IsDataForm(form_value.Form())) { 3188 // Retrieve the value as a data expression. 3189 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 3190 if (auto data_length = form_value.GetFixedSize()) 3191 location = 3192 DWARFExpression(module, debug_info_data, die.GetCU(), 3193 data_offset, *data_length); 3194 else { 3195 const uint8_t *data_pointer = form_value.BlockData(); 3196 if (data_pointer) { 3197 form_value.Unsigned(); 3198 } else if (DWARFFormValue::IsDataForm(form_value.Form())) { 3199 // we need to get the byte size of the type later after we 3200 // create the variable 3201 const_value = form_value; 3202 } 3203 } 3204 } else { 3205 // Retrieve the value as a string expression. 3206 if (form_value.Form() == DW_FORM_strp) { 3207 uint32_t data_offset = attributes.DIEOffsetAtIndex(i); 3208 if (auto data_length = form_value.GetFixedSize()) 3209 location = 3210 DWARFExpression(module, debug_info_data, die.GetCU(), 3211 data_offset, *data_length); 3212 } else { 3213 const char *str = form_value.AsCString(); 3214 uint32_t string_offset = 3215 str - (const char *)debug_info_data.GetDataStart(); 3216 uint32_t string_length = strlen(str) + 1; 3217 location = 3218 DWARFExpression(module, debug_info_data, die.GetCU(), 3219 string_offset, string_length); 3220 } 3221 } 3222 } 3223 break; 3224 case DW_AT_location: { 3225 location_is_const_value_data = false; 3226 has_explicit_location = true; 3227 if (DWARFFormValue::IsBlockForm(form_value.Form())) { 3228 auto data = die.GetData(); 3229 3230 uint32_t block_offset = 3231 form_value.BlockData() - data.GetDataStart(); 3232 uint32_t block_length = form_value.Unsigned(); 3233 location = DWARFExpression(module, data, die.GetCU(), 3234 block_offset, block_length); 3235 } else { 3236 const DWARFDataExtractor &debug_loc_data = DebugLocData(); 3237 const dw_offset_t debug_loc_offset = form_value.Unsigned(); 3238 3239 size_t loc_list_length = DWARFExpression::LocationListSize( 3240 die.GetCU(), debug_loc_data, debug_loc_offset); 3241 if (loc_list_length > 0) { 3242 location = DWARFExpression(module, debug_loc_data, die.GetCU(), 3243 debug_loc_offset, loc_list_length); 3244 assert(func_low_pc != LLDB_INVALID_ADDRESS); 3245 location.SetLocationListSlide( 3246 func_low_pc - 3247 attributes.CompileUnitAtIndex(i)->GetBaseAddress()); 3248 } 3249 } 3250 } break; 3251 case DW_AT_specification: 3252 spec_die = form_value.Reference(); 3253 break; 3254 case DW_AT_start_scope: 3255 // TODO: Implement this. 3256 break; 3257 case DW_AT_artificial: 3258 is_artificial = form_value.Boolean(); 3259 break; 3260 case DW_AT_accessibility: 3261 break; // accessibility = 3262 // DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 3263 case DW_AT_declaration: 3264 case DW_AT_description: 3265 case DW_AT_endianity: 3266 case DW_AT_segment: 3267 case DW_AT_visibility: 3268 default: 3269 case DW_AT_abstract_origin: 3270 case DW_AT_sibling: 3271 break; 3272 } 3273 } 3274 } 3275 3276 const DWARFDIE parent_context_die = GetDeclContextDIEContainingDIE(die); 3277 const dw_tag_t parent_tag = die.GetParent().Tag(); 3278 bool is_static_member = 3279 (parent_tag == DW_TAG_compile_unit || 3280 parent_tag == DW_TAG_partial_unit) && 3281 (parent_context_die.Tag() == DW_TAG_class_type || 3282 parent_context_die.Tag() == DW_TAG_structure_type); 3283 3284 ValueType scope = eValueTypeInvalid; 3285 3286 const DWARFDIE sc_parent_die = GetParentSymbolContextDIE(die); 3287 SymbolContextScope *symbol_context_scope = nullptr; 3288 3289 bool has_explicit_mangled = mangled != nullptr; 3290 if (!mangled) { 3291 // LLDB relies on the mangled name (DW_TAG_linkage_name or 3292 // DW_AT_MIPS_linkage_name) to generate fully qualified names 3293 // of global variables with commands like "frame var j". For 3294 // example, if j were an int variable holding a value 4 and 3295 // declared in a namespace B which in turn is contained in a 3296 // namespace A, the command "frame var j" returns 3297 // "(int) A::B::j = 4". 3298 // If the compiler does not emit a linkage name, we should be 3299 // able to generate a fully qualified name from the 3300 // declaration context. 3301 if ((parent_tag == DW_TAG_compile_unit || 3302 parent_tag == DW_TAG_partial_unit) && 3303 Language::LanguageIsCPlusPlus(die.GetLanguage())) { 3304 DWARFDeclContext decl_ctx; 3305 3306 die.GetDWARFDeclContext(decl_ctx); 3307 mangled = decl_ctx.GetQualifiedNameAsConstString().GetCString(); 3308 } 3309 } 3310 3311 if (tag == DW_TAG_formal_parameter) 3312 scope = eValueTypeVariableArgument; 3313 else { 3314 // DWARF doesn't specify if a DW_TAG_variable is a local, global 3315 // or static variable, so we have to do a little digging: 3316 // 1) DW_AT_linkage_name implies static lifetime (but may be missing) 3317 // 2) An empty DW_AT_location is an (optimized-out) static lifetime var. 3318 // 3) DW_AT_location containing a DW_OP_addr implies static lifetime. 3319 // Clang likes to combine small global variables into the same symbol 3320 // with locations like: DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus 3321 // so we need to look through the whole expression. 3322 bool is_static_lifetime = 3323 has_explicit_mangled || 3324 (has_explicit_location && !location.IsValid()); 3325 // Check if the location has a DW_OP_addr with any address value... 3326 lldb::addr_t location_DW_OP_addr = LLDB_INVALID_ADDRESS; 3327 if (!location_is_const_value_data) { 3328 bool op_error = false; 3329 location_DW_OP_addr = location.GetLocation_DW_OP_addr(0, op_error); 3330 if (op_error) { 3331 StreamString strm; 3332 location.DumpLocationForAddress(&strm, eDescriptionLevelFull, 0, 0, 3333 nullptr); 3334 GetObjectFile()->GetModule()->ReportError( 3335 "0x%8.8x: %s has an invalid location: %s", die.GetOffset(), 3336 die.GetTagAsCString(), strm.GetData()); 3337 } 3338 if (location_DW_OP_addr != LLDB_INVALID_ADDRESS) 3339 is_static_lifetime = true; 3340 } 3341 SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile(); 3342 if (debug_map_symfile) 3343 // Set the module of the expression to the linked module 3344 // instead of the oject file so the relocated address can be 3345 // found there. 3346 location.SetModule(debug_map_symfile->GetObjectFile()->GetModule()); 3347 3348 if (is_static_lifetime) { 3349 if (is_external) 3350 scope = eValueTypeVariableGlobal; 3351 else 3352 scope = eValueTypeVariableStatic; 3353 3354 if (debug_map_symfile) { 3355 // When leaving the DWARF in the .o files on darwin, when we have a 3356 // global variable that wasn't initialized, the .o file might not 3357 // have allocated a virtual address for the global variable. In 3358 // this case it will have created a symbol for the global variable 3359 // that is undefined/data and external and the value will be the 3360 // byte size of the variable. When we do the address map in 3361 // SymbolFileDWARFDebugMap we rely on having an address, we need to 3362 // do some magic here so we can get the correct address for our 3363 // global variable. The address for all of these entries will be 3364 // zero, and there will be an undefined symbol in this object file, 3365 // and the executable will have a matching symbol with a good 3366 // address. So here we dig up the correct address and replace it in 3367 // the location for the variable, and set the variable's symbol 3368 // context scope to be that of the main executable so the file 3369 // address will resolve correctly. 3370 bool linked_oso_file_addr = false; 3371 if (is_external && location_DW_OP_addr == 0) { 3372 // we have a possible uninitialized extern global 3373 ConstString const_name(mangled ? mangled : name); 3374 ObjectFile *debug_map_objfile = 3375 debug_map_symfile->GetObjectFile(); 3376 if (debug_map_objfile) { 3377 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab(); 3378 if (debug_map_symtab) { 3379 Symbol *exe_symbol = 3380 debug_map_symtab->FindFirstSymbolWithNameAndType( 3381 const_name, eSymbolTypeData, Symtab::eDebugYes, 3382 Symtab::eVisibilityExtern); 3383 if (exe_symbol) { 3384 if (exe_symbol->ValueIsAddress()) { 3385 const addr_t exe_file_addr = 3386 exe_symbol->GetAddressRef().GetFileAddress(); 3387 if (exe_file_addr != LLDB_INVALID_ADDRESS) { 3388 if (location.Update_DW_OP_addr(exe_file_addr)) { 3389 linked_oso_file_addr = true; 3390 symbol_context_scope = exe_symbol; 3391 } 3392 } 3393 } 3394 } 3395 } 3396 } 3397 } 3398 3399 if (!linked_oso_file_addr) { 3400 // The DW_OP_addr is not zero, but it contains a .o file address 3401 // which needs to be linked up correctly. 3402 const lldb::addr_t exe_file_addr = 3403 debug_map_symfile->LinkOSOFileAddress(this, 3404 location_DW_OP_addr); 3405 if (exe_file_addr != LLDB_INVALID_ADDRESS) { 3406 // Update the file address for this variable 3407 location.Update_DW_OP_addr(exe_file_addr); 3408 } else { 3409 // Variable didn't make it into the final executable 3410 return var_sp; 3411 } 3412 } 3413 } 3414 } else { 3415 if (location_is_const_value_data) 3416 scope = eValueTypeVariableStatic; 3417 else { 3418 scope = eValueTypeVariableLocal; 3419 if (debug_map_symfile) { 3420 // We need to check for TLS addresses that we need to fixup 3421 if (location.ContainsThreadLocalStorage()) { 3422 location.LinkThreadLocalStorage( 3423 debug_map_symfile->GetObjectFile()->GetModule(), 3424 [this, debug_map_symfile]( 3425 lldb::addr_t unlinked_file_addr) -> lldb::addr_t { 3426 return debug_map_symfile->LinkOSOFileAddress( 3427 this, unlinked_file_addr); 3428 }); 3429 scope = eValueTypeVariableThreadLocal; 3430 } 3431 } 3432 } 3433 } 3434 } 3435 3436 if (symbol_context_scope == nullptr) { 3437 switch (parent_tag) { 3438 case DW_TAG_subprogram: 3439 case DW_TAG_inlined_subroutine: 3440 case DW_TAG_lexical_block: 3441 if (sc.function) { 3442 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID( 3443 sc_parent_die.GetID()); 3444 if (symbol_context_scope == nullptr) 3445 symbol_context_scope = sc.function; 3446 } 3447 break; 3448 3449 default: 3450 symbol_context_scope = sc.comp_unit; 3451 break; 3452 } 3453 } 3454 3455 if (symbol_context_scope) { 3456 SymbolFileTypeSP type_sp( 3457 new SymbolFileType(*this, GetUID(type_die_form.Reference()))); 3458 3459 if (const_value.Form() && type_sp && type_sp->GetType()) 3460 location.UpdateValue(const_value.Unsigned(), 3461 type_sp->GetType()->GetByteSize().getValueOr(0), 3462 die.GetCU()->GetAddressByteSize()); 3463 3464 var_sp = std::make_shared<Variable>( 3465 die.GetID(), name, mangled, type_sp, scope, symbol_context_scope, 3466 scope_ranges, &decl, location, is_external, is_artificial, 3467 is_static_member); 3468 3469 var_sp->SetLocationIsConstantValueData(location_is_const_value_data); 3470 } else { 3471 // Not ready to parse this variable yet. It might be a global or static 3472 // variable that is in a function scope and the function in the symbol 3473 // context wasn't filled in yet 3474 return var_sp; 3475 } 3476 } 3477 // Cache var_sp even if NULL (the variable was just a specification or was 3478 // missing vital information to be able to be displayed in the debugger 3479 // (missing location due to optimization, etc)) so we don't re-parse this 3480 // DIE over and over later... 3481 GetDIEToVariable()[die.GetDIE()] = var_sp; 3482 if (spec_die) 3483 GetDIEToVariable()[spec_die.GetDIE()] = var_sp; 3484 } 3485 return var_sp; 3486 } 3487 3488 DWARFDIE 3489 SymbolFileDWARF::FindBlockContainingSpecification( 3490 const DIERef &func_die_ref, dw_offset_t spec_block_die_offset) { 3491 // Give the concrete function die specified by "func_die_offset", find the 3492 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 3493 // to "spec_block_die_offset" 3494 return FindBlockContainingSpecification(DebugInfo()->GetDIE(func_die_ref), 3495 spec_block_die_offset); 3496 } 3497 3498 DWARFDIE 3499 SymbolFileDWARF::FindBlockContainingSpecification( 3500 const DWARFDIE &die, dw_offset_t spec_block_die_offset) { 3501 if (die) { 3502 switch (die.Tag()) { 3503 case DW_TAG_subprogram: 3504 case DW_TAG_inlined_subroutine: 3505 case DW_TAG_lexical_block: { 3506 if (die.GetReferencedDIE(DW_AT_specification).GetOffset() == 3507 spec_block_die_offset) 3508 return die; 3509 3510 if (die.GetReferencedDIE(DW_AT_abstract_origin).GetOffset() == 3511 spec_block_die_offset) 3512 return die; 3513 } break; 3514 } 3515 3516 // Give the concrete function die specified by "func_die_offset", find the 3517 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points 3518 // to "spec_block_die_offset" 3519 for (DWARFDIE child_die = die.GetFirstChild(); child_die; 3520 child_die = child_die.GetSibling()) { 3521 DWARFDIE result_die = 3522 FindBlockContainingSpecification(child_die, spec_block_die_offset); 3523 if (result_die) 3524 return result_die; 3525 } 3526 } 3527 3528 return DWARFDIE(); 3529 } 3530 3531 size_t SymbolFileDWARF::ParseVariables(const SymbolContext &sc, 3532 const DWARFDIE &orig_die, 3533 const lldb::addr_t func_low_pc, 3534 bool parse_siblings, bool parse_children, 3535 VariableList *cc_variable_list) { 3536 if (!orig_die) 3537 return 0; 3538 3539 VariableListSP variable_list_sp; 3540 3541 size_t vars_added = 0; 3542 DWARFDIE die = orig_die; 3543 while (die) { 3544 dw_tag_t tag = die.Tag(); 3545 3546 // Check to see if we have already parsed this variable or constant? 3547 VariableSP var_sp = GetDIEToVariable()[die.GetDIE()]; 3548 if (var_sp) { 3549 if (cc_variable_list) 3550 cc_variable_list->AddVariableIfUnique(var_sp); 3551 } else { 3552 // We haven't already parsed it, lets do that now. 3553 if ((tag == DW_TAG_variable) || (tag == DW_TAG_constant) || 3554 (tag == DW_TAG_formal_parameter && sc.function)) { 3555 if (variable_list_sp.get() == nullptr) { 3556 DWARFDIE sc_parent_die = GetParentSymbolContextDIE(orig_die); 3557 dw_tag_t parent_tag = sc_parent_die.Tag(); 3558 switch (parent_tag) { 3559 case DW_TAG_compile_unit: 3560 case DW_TAG_partial_unit: 3561 if (sc.comp_unit != nullptr) { 3562 variable_list_sp = sc.comp_unit->GetVariableList(false); 3563 if (variable_list_sp.get() == nullptr) { 3564 variable_list_sp = std::make_shared<VariableList>(); 3565 } 3566 } else { 3567 GetObjectFile()->GetModule()->ReportError( 3568 "parent 0x%8.8" PRIx64 " %s with no valid compile unit in " 3569 "symbol context for 0x%8.8" PRIx64 3570 " %s.\n", 3571 sc_parent_die.GetID(), sc_parent_die.GetTagAsCString(), 3572 orig_die.GetID(), orig_die.GetTagAsCString()); 3573 } 3574 break; 3575 3576 case DW_TAG_subprogram: 3577 case DW_TAG_inlined_subroutine: 3578 case DW_TAG_lexical_block: 3579 if (sc.function != nullptr) { 3580 // Check to see if we already have parsed the variables for the 3581 // given scope 3582 3583 Block *block = sc.function->GetBlock(true).FindBlockByID( 3584 sc_parent_die.GetID()); 3585 if (block == nullptr) { 3586 // This must be a specification or abstract origin with a 3587 // concrete block counterpart in the current function. We need 3588 // to find the concrete block so we can correctly add the 3589 // variable to it 3590 const DWARFDIE concrete_block_die = 3591 FindBlockContainingSpecification( 3592 GetDIE(sc.function->GetID()), 3593 sc_parent_die.GetOffset()); 3594 if (concrete_block_die) 3595 block = sc.function->GetBlock(true).FindBlockByID( 3596 concrete_block_die.GetID()); 3597 } 3598 3599 if (block != nullptr) { 3600 const bool can_create = false; 3601 variable_list_sp = block->GetBlockVariableList(can_create); 3602 if (variable_list_sp.get() == nullptr) { 3603 variable_list_sp = std::make_shared<VariableList>(); 3604 block->SetVariableList(variable_list_sp); 3605 } 3606 } 3607 } 3608 break; 3609 3610 default: 3611 GetObjectFile()->GetModule()->ReportError( 3612 "didn't find appropriate parent DIE for variable list for " 3613 "0x%8.8" PRIx64 " %s.\n", 3614 orig_die.GetID(), orig_die.GetTagAsCString()); 3615 break; 3616 } 3617 } 3618 3619 if (variable_list_sp) { 3620 VariableSP var_sp(ParseVariableDIE(sc, die, func_low_pc)); 3621 if (var_sp) { 3622 variable_list_sp->AddVariableIfUnique(var_sp); 3623 if (cc_variable_list) 3624 cc_variable_list->AddVariableIfUnique(var_sp); 3625 ++vars_added; 3626 } 3627 } 3628 } 3629 } 3630 3631 bool skip_children = (sc.function == nullptr && tag == DW_TAG_subprogram); 3632 3633 if (!skip_children && parse_children && die.HasChildren()) { 3634 vars_added += ParseVariables(sc, die.GetFirstChild(), func_low_pc, true, 3635 true, cc_variable_list); 3636 } 3637 3638 if (parse_siblings) 3639 die = die.GetSibling(); 3640 else 3641 die.Clear(); 3642 } 3643 return vars_added; 3644 } 3645 3646 /// Collect call graph edges present in a function DIE. 3647 static std::vector<lldb_private::CallEdge> 3648 CollectCallEdges(DWARFDIE function_die) { 3649 // Check if the function has a supported call site-related attribute. 3650 // TODO: In the future it may be worthwhile to support call_all_source_calls. 3651 uint64_t has_call_edges = 3652 function_die.GetAttributeValueAsUnsigned(DW_AT_call_all_calls, 0); 3653 if (!has_call_edges) 3654 return {}; 3655 3656 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 3657 LLDB_LOG(log, "CollectCallEdges: Found call site info in {0}", 3658 function_die.GetPubname()); 3659 3660 // Scan the DIE for TAG_call_site entries. 3661 // TODO: A recursive scan of all blocks in the subprogram is needed in order 3662 // to be DWARF5-compliant. This may need to be done lazily to be performant. 3663 // For now, assume that all entries are nested directly under the subprogram 3664 // (this is the kind of DWARF LLVM produces) and parse them eagerly. 3665 std::vector<CallEdge> call_edges; 3666 for (DWARFDIE child = function_die.GetFirstChild(); child.IsValid(); 3667 child = child.GetSibling()) { 3668 if (child.Tag() != DW_TAG_call_site) 3669 continue; 3670 3671 // Extract DW_AT_call_origin (the call target's DIE). 3672 DWARFDIE call_origin = child.GetReferencedDIE(DW_AT_call_origin); 3673 if (!call_origin.IsValid()) { 3674 LLDB_LOG(log, "CollectCallEdges: Invalid call origin in {0}", 3675 function_die.GetPubname()); 3676 continue; 3677 } 3678 3679 // Extract DW_AT_call_return_pc (the PC the call returns to) if it's 3680 // available. It should only ever be unavailable for tail call edges, in 3681 // which case use LLDB_INVALID_ADDRESS. 3682 addr_t return_pc = child.GetAttributeValueAsAddress(DW_AT_call_return_pc, 3683 LLDB_INVALID_ADDRESS); 3684 3685 LLDB_LOG(log, "CollectCallEdges: Found call origin: {0} (retn-PC: {1:x})", 3686 call_origin.GetPubname(), return_pc); 3687 call_edges.emplace_back(call_origin.GetMangledName(), return_pc); 3688 } 3689 return call_edges; 3690 } 3691 3692 std::vector<lldb_private::CallEdge> 3693 SymbolFileDWARF::ParseCallEdgesInFunction(UserID func_id) { 3694 DWARFDIE func_die = GetDIE(func_id.GetID()); 3695 if (func_die.IsValid()) 3696 return CollectCallEdges(func_die); 3697 return {}; 3698 } 3699 3700 // PluginInterface protocol 3701 ConstString SymbolFileDWARF::GetPluginName() { return GetPluginNameStatic(); } 3702 3703 uint32_t SymbolFileDWARF::GetPluginVersion() { return 1; } 3704 3705 void SymbolFileDWARF::Dump(lldb_private::Stream &s) { 3706 SymbolFile::Dump(s); 3707 m_index->Dump(s); 3708 } 3709 3710 void SymbolFileDWARF::DumpClangAST(Stream &s) { 3711 TypeSystem *ts = GetTypeSystemForLanguage(eLanguageTypeC_plus_plus); 3712 ClangASTContext *clang = llvm::dyn_cast_or_null<ClangASTContext>(ts); 3713 if (!clang) 3714 return; 3715 clang->Dump(s); 3716 } 3717 3718 SymbolFileDWARFDebugMap *SymbolFileDWARF::GetDebugMapSymfile() { 3719 if (m_debug_map_symfile == nullptr && !m_debug_map_module_wp.expired()) { 3720 lldb::ModuleSP module_sp(m_debug_map_module_wp.lock()); 3721 if (module_sp) { 3722 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor(); 3723 if (sym_vendor) 3724 m_debug_map_symfile = 3725 (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile(); 3726 } 3727 } 3728 return m_debug_map_symfile; 3729 } 3730 3731 DWARFExpression::LocationListFormat 3732 SymbolFileDWARF::GetLocationListFormat() const { 3733 if (m_data_debug_loclists.m_data.GetByteSize() > 0) 3734 return DWARFExpression::LocLists; 3735 return DWARFExpression::RegularLocationList; 3736 } 3737 3738 SymbolFileDWARFDwp *SymbolFileDWARF::GetDwpSymbolFile() { 3739 llvm::call_once(m_dwp_symfile_once_flag, [this]() { 3740 ModuleSpec module_spec; 3741 module_spec.GetFileSpec() = m_obj_file->GetFileSpec(); 3742 module_spec.GetSymbolFileSpec() = 3743 FileSpec(m_obj_file->GetFileSpec().GetPath() + ".dwp"); 3744 3745 FileSpecList search_paths = Target::GetDefaultDebugFileSearchPaths(); 3746 FileSpec dwp_filespec = 3747 Symbols::LocateExecutableSymbolFile(module_spec, search_paths); 3748 if (FileSystem::Instance().Exists(dwp_filespec)) { 3749 m_dwp_symfile = SymbolFileDWARFDwp::Create(GetObjectFile()->GetModule(), 3750 dwp_filespec); 3751 } 3752 }); 3753 return m_dwp_symfile.get(); 3754 } 3755