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