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