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