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