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