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