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