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