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