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