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