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