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