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