1 //===-- SymbolFileDWARF.h --------------------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H 10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H 11 12 #include <list> 13 #include <map> 14 #include <mutex> 15 #include <unordered_map> 16 #include <vector> 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/SetVector.h" 20 #include "llvm/Support/Threading.h" 21 22 #include "lldb/Core/UniqueCStringMap.h" 23 #include "lldb/Core/dwarf.h" 24 #include "lldb/Expression/DWARFExpressionList.h" 25 #include "lldb/Symbol/DebugMacros.h" 26 #include "lldb/Symbol/SymbolContext.h" 27 #include "lldb/Symbol/SymbolFile.h" 28 #include "lldb/Target/Statistics.h" 29 #include "lldb/Utility/ConstString.h" 30 #include "lldb/Utility/Flags.h" 31 #include "lldb/Utility/RangeMap.h" 32 #include "lldb/lldb-private.h" 33 34 #include "DWARFContext.h" 35 #include "DWARFDataExtractor.h" 36 #include "DWARFDefines.h" 37 #include "DWARFIndex.h" 38 #include "UniqueDWARFASTType.h" 39 40 // Forward Declarations for this DWARF plugin 41 class DebugMapModule; 42 class DWARFAbbreviationDeclaration; 43 class DWARFAbbreviationDeclarationSet; 44 class DWARFCompileUnit; 45 class DWARFDebugAbbrev; 46 class DWARFDebugAranges; 47 class DWARFDebugInfo; 48 class DWARFDebugInfoEntry; 49 class DWARFDebugLine; 50 class DWARFDebugRanges; 51 class DWARFDeclContext; 52 class DWARFFormValue; 53 class DWARFTypeUnit; 54 class SymbolFileDWARFDebugMap; 55 class SymbolFileDWARFDwo; 56 class SymbolFileDWARFDwp; 57 58 #define DIE_IS_BEING_PARSED ((lldb_private::Type *)1) 59 60 class SymbolFileDWARF : public lldb_private::SymbolFileCommon, 61 public lldb_private::UserID { 62 /// LLVM RTTI support. 63 static char ID; 64 65 public: 66 /// LLVM RTTI support. 67 /// \{ 68 bool isA(const void *ClassID) const override { 69 return ClassID == &ID || SymbolFileCommon::isA(ClassID); 70 } 71 static bool classof(const SymbolFile *obj) { return obj->isA(&ID); } 72 /// \} 73 74 friend class SymbolFileDWARFDebugMap; 75 friend class SymbolFileDWARFDwo; 76 friend class DebugMapModule; 77 friend class DWARFCompileUnit; 78 friend class DWARFDIE; 79 friend class DWARFASTParserClang; 80 81 // Static Functions 82 static void Initialize(); 83 84 static void Terminate(); 85 86 static void DebuggerInitialize(lldb_private::Debugger &debugger); 87 88 static llvm::StringRef GetPluginNameStatic() { return "dwarf"; } 89 90 static llvm::StringRef GetPluginDescriptionStatic(); 91 92 static lldb_private::SymbolFile * 93 CreateInstance(lldb::ObjectFileSP objfile_sp); 94 95 // Constructors and Destructors 96 97 SymbolFileDWARF(lldb::ObjectFileSP objfile_sp, 98 lldb_private::SectionList *dwo_section_list); 99 100 ~SymbolFileDWARF() override; 101 102 uint32_t CalculateAbilities() override; 103 104 void InitializeObject() override; 105 106 // Compile Unit function calls 107 108 lldb::LanguageType 109 ParseLanguage(lldb_private::CompileUnit &comp_unit) override; 110 111 lldb_private::XcodeSDK 112 ParseXcodeSDK(lldb_private::CompileUnit &comp_unit) override; 113 114 size_t ParseFunctions(lldb_private::CompileUnit &comp_unit) override; 115 116 bool ParseLineTable(lldb_private::CompileUnit &comp_unit) override; 117 118 bool ParseDebugMacros(lldb_private::CompileUnit &comp_unit) override; 119 120 bool ForEachExternalModule( 121 lldb_private::CompileUnit &, llvm::DenseSet<lldb_private::SymbolFile *> &, 122 llvm::function_ref<bool(lldb_private::Module &)>) override; 123 124 bool ParseSupportFiles(lldb_private::CompileUnit &comp_unit, 125 lldb_private::FileSpecList &support_files) override; 126 127 bool ParseIsOptimized(lldb_private::CompileUnit &comp_unit) override; 128 129 size_t ParseTypes(lldb_private::CompileUnit &comp_unit) override; 130 131 bool ParseImportedModules( 132 const lldb_private::SymbolContext &sc, 133 std::vector<lldb_private::SourceModule> &imported_modules) override; 134 135 size_t ParseBlocksRecursive(lldb_private::Function &func) override; 136 137 size_t 138 ParseVariablesForContext(const lldb_private::SymbolContext &sc) override; 139 140 lldb_private::Type *ResolveTypeUID(lldb::user_id_t type_uid) override; 141 llvm::Optional<ArrayInfo> GetDynamicArrayInfoForUID( 142 lldb::user_id_t type_uid, 143 const lldb_private::ExecutionContext *exe_ctx) override; 144 145 bool CompleteType(lldb_private::CompilerType &compiler_type) override; 146 147 lldb_private::Type *ResolveType(const DWARFDIE &die, 148 bool assert_not_being_parsed = true, 149 bool resolve_function_context = false); 150 151 lldb_private::CompilerDecl GetDeclForUID(lldb::user_id_t uid) override; 152 153 lldb_private::CompilerDeclContext 154 GetDeclContextForUID(lldb::user_id_t uid) override; 155 156 lldb_private::CompilerDeclContext 157 GetDeclContextContainingUID(lldb::user_id_t uid) override; 158 159 void 160 ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) override; 161 162 uint32_t ResolveSymbolContext(const lldb_private::Address &so_addr, 163 lldb::SymbolContextItem resolve_scope, 164 lldb_private::SymbolContext &sc) override; 165 166 uint32_t ResolveSymbolContext( 167 const lldb_private::SourceLocationSpec &src_location_spec, 168 lldb::SymbolContextItem resolve_scope, 169 lldb_private::SymbolContextList &sc_list) override; 170 171 void 172 FindGlobalVariables(lldb_private::ConstString name, 173 const lldb_private::CompilerDeclContext &parent_decl_ctx, 174 uint32_t max_matches, 175 lldb_private::VariableList &variables) override; 176 177 void FindGlobalVariables(const lldb_private::RegularExpression ®ex, 178 uint32_t max_matches, 179 lldb_private::VariableList &variables) override; 180 181 void FindFunctions(lldb_private::ConstString name, 182 const lldb_private::CompilerDeclContext &parent_decl_ctx, 183 lldb::FunctionNameType name_type_mask, 184 bool include_inlines, 185 lldb_private::SymbolContextList &sc_list) override; 186 187 void FindFunctions(const lldb_private::RegularExpression ®ex, 188 bool include_inlines, 189 lldb_private::SymbolContextList &sc_list) override; 190 191 void GetMangledNamesForFunction( 192 const std::string &scope_qualified_name, 193 std::vector<lldb_private::ConstString> &mangled_names) override; 194 195 void 196 FindTypes(lldb_private::ConstString name, 197 const lldb_private::CompilerDeclContext &parent_decl_ctx, 198 uint32_t max_matches, 199 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 200 lldb_private::TypeMap &types) override; 201 202 void FindTypes(llvm::ArrayRef<lldb_private::CompilerContext> pattern, 203 lldb_private::LanguageSet languages, 204 llvm::DenseSet<SymbolFile *> &searched_symbol_files, 205 lldb_private::TypeMap &types) override; 206 207 void GetTypes(lldb_private::SymbolContextScope *sc_scope, 208 lldb::TypeClass type_mask, 209 lldb_private::TypeList &type_list) override; 210 211 llvm::Expected<lldb_private::TypeSystem &> 212 GetTypeSystemForLanguage(lldb::LanguageType language) override; 213 214 lldb_private::CompilerDeclContext FindNamespace( 215 lldb_private::ConstString name, 216 const lldb_private::CompilerDeclContext &parent_decl_ctx) override; 217 218 void PreloadSymbols() override; 219 220 std::recursive_mutex &GetModuleMutex() const override; 221 222 // PluginInterface protocol 223 llvm::StringRef GetPluginName() override { return GetPluginNameStatic(); } 224 225 DWARFDebugAbbrev *DebugAbbrev(); 226 227 DWARFDebugInfo &DebugInfo(); 228 229 DWARFDebugRanges *GetDebugRanges(); 230 231 static bool SupportedVersion(uint16_t version); 232 233 DWARFDIE 234 GetDeclContextDIEContainingDIE(const DWARFDIE &die); 235 236 bool 237 HasForwardDeclForClangType(const lldb_private::CompilerType &compiler_type); 238 239 lldb_private::CompileUnit * 240 GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu); 241 242 virtual void GetObjCMethods(lldb_private::ConstString class_name, 243 llvm::function_ref<bool(DWARFDIE die)> callback); 244 245 bool Supports_DW_AT_APPLE_objc_complete_type(DWARFUnit *cu); 246 247 lldb_private::DebugMacrosSP ParseDebugMacros(lldb::offset_t *offset); 248 249 static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die); 250 251 lldb::ModuleSP GetExternalModule(lldb_private::ConstString name); 252 253 typedef std::map<lldb_private::ConstString, lldb::ModuleSP> 254 ExternalTypeModuleMap; 255 256 /// Return the list of Clang modules imported by this SymbolFile. 257 const ExternalTypeModuleMap &getExternalTypeModules() const { 258 return m_external_type_modules; 259 } 260 261 virtual DWARFDIE GetDIE(const DIERef &die_ref); 262 263 DWARFDIE GetDIE(lldb::user_id_t uid); 264 265 lldb::user_id_t GetUID(const DWARFBaseDIE &die) { 266 return GetUID(die.GetDIERef()); 267 } 268 269 lldb::user_id_t GetUID(const llvm::Optional<DIERef> &ref) { 270 return ref ? GetUID(*ref) : LLDB_INVALID_UID; 271 } 272 273 lldb::user_id_t GetUID(DIERef ref); 274 275 std::shared_ptr<SymbolFileDWARFDwo> 276 GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu, 277 const DWARFDebugInfoEntry &cu_die); 278 279 virtual llvm::Optional<uint32_t> GetDwoNum() { return llvm::None; } 280 281 /// If this is a DWARF object with a single CU, return its DW_AT_dwo_id. 282 llvm::Optional<uint64_t> GetDWOId(); 283 284 static bool 285 DIEInDeclContext(const lldb_private::CompilerDeclContext &parent_decl_ctx, 286 const DWARFDIE &die); 287 288 std::vector<std::unique_ptr<lldb_private::CallEdge>> 289 ParseCallEdgesInFunction(UserID func_id) override; 290 291 void Dump(lldb_private::Stream &s) override; 292 293 void DumpClangAST(lldb_private::Stream &s) override; 294 295 lldb_private::DWARFContext &GetDWARFContext() { return m_context; } 296 297 const std::shared_ptr<SymbolFileDWARFDwo> &GetDwpSymbolFile(); 298 299 lldb_private::FileSpec GetFile(DWARFUnit &unit, size_t file_idx); 300 301 static llvm::Expected<lldb_private::TypeSystem &> 302 GetTypeSystem(DWARFUnit &unit); 303 304 static DWARFASTParser *GetDWARFParser(DWARFUnit &unit); 305 306 // CompilerDecl related functions 307 308 static lldb_private::CompilerDecl GetDecl(const DWARFDIE &die); 309 310 static lldb_private::CompilerDeclContext GetDeclContext(const DWARFDIE &die); 311 312 static lldb_private::CompilerDeclContext 313 GetContainingDeclContext(const DWARFDIE &die); 314 315 static DWARFDeclContext GetDWARFDeclContext(const DWARFDIE &die); 316 317 static lldb::LanguageType LanguageTypeFromDWARF(uint64_t val); 318 319 static lldb::LanguageType GetLanguage(DWARFUnit &unit); 320 /// Same as GetLanguage() but reports all C++ versions as C++ (no version). 321 static lldb::LanguageType GetLanguageFamily(DWARFUnit &unit); 322 323 lldb_private::StatsDuration::Duration GetDebugInfoParseTime() override { 324 return m_parse_time; 325 } 326 lldb_private::StatsDuration::Duration GetDebugInfoIndexTime() override; 327 328 lldb_private::StatsDuration &GetDebugInfoParseTimeRef() { 329 return m_parse_time; 330 } 331 332 protected: 333 typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb_private::Type *> 334 DIEToTypePtr; 335 typedef llvm::DenseMap<const DWARFDebugInfoEntry *, lldb::VariableSP> 336 DIEToVariableSP; 337 typedef llvm::DenseMap<const DWARFDebugInfoEntry *, 338 lldb::opaque_compiler_type_t> 339 DIEToClangType; 340 typedef llvm::DenseMap<lldb::opaque_compiler_type_t, DIERef> ClangTypeToDIE; 341 342 SymbolFileDWARF(const SymbolFileDWARF &) = delete; 343 const SymbolFileDWARF &operator=(const SymbolFileDWARF &) = delete; 344 345 virtual void LoadSectionData(lldb::SectionType sect_type, 346 lldb_private::DWARFDataExtractor &data); 347 348 bool DeclContextMatchesThisSymbolFile( 349 const lldb_private::CompilerDeclContext &decl_ctx); 350 351 uint32_t CalculateNumCompileUnits() override; 352 353 lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t index) override; 354 355 lldb_private::TypeList &GetTypeList() override; 356 357 lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu); 358 359 virtual DWARFCompileUnit * 360 GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit); 361 362 DWARFUnit *GetNextUnparsedDWARFCompileUnit(DWARFUnit *prev_cu); 363 364 bool GetFunction(const DWARFDIE &die, lldb_private::SymbolContext &sc); 365 366 lldb_private::Function *ParseFunction(lldb_private::CompileUnit &comp_unit, 367 const DWARFDIE &die); 368 369 size_t ParseBlocksRecursive(lldb_private::CompileUnit &comp_unit, 370 lldb_private::Block *parent_block, 371 const DWARFDIE &die, 372 lldb::addr_t subprogram_low_pc, uint32_t depth); 373 374 size_t ParseTypes(const lldb_private::SymbolContext &sc, const DWARFDIE &die, 375 bool parse_siblings, bool parse_children); 376 377 lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc, 378 const DWARFDIE &die, bool *type_is_new); 379 380 bool ParseSupportFiles(DWARFUnit &dwarf_cu, const lldb::ModuleSP &module, 381 lldb_private::FileSpecList &support_files); 382 383 lldb_private::Type *ResolveTypeUID(const DWARFDIE &die, 384 bool assert_not_being_parsed); 385 386 lldb_private::Type *ResolveTypeUID(const DIERef &die_ref); 387 388 lldb::VariableSP ParseVariableDIE(const lldb_private::SymbolContext &sc, 389 const DWARFDIE &die, 390 const lldb::addr_t func_low_pc); 391 lldb::VariableSP ParseVariableDIECached(const lldb_private::SymbolContext &sc, 392 const DWARFDIE &die); 393 394 void 395 ParseAndAppendGlobalVariable(const lldb_private::SymbolContext &sc, 396 const DWARFDIE &die, 397 lldb_private::VariableList &cc_variable_list); 398 399 size_t ParseVariablesInFunctionContext(const lldb_private::SymbolContext &sc, 400 const DWARFDIE &die, 401 const lldb::addr_t func_low_pc); 402 403 size_t ParseVariablesInFunctionContextRecursive( 404 const lldb_private::SymbolContext &sc, const DWARFDIE &die, 405 lldb::addr_t func_low_pc, DIEArray &accumulator); 406 407 size_t PopulateBlockVariableList(lldb_private::VariableList &variable_list, 408 const lldb_private::SymbolContext &sc, 409 llvm::ArrayRef<DIERef> variable_dies, 410 lldb::addr_t func_low_pc); 411 412 DIEArray MergeBlockAbstractParameters(const DWARFDIE &block_die, 413 DIEArray &&variable_dies); 414 415 bool ClassOrStructIsVirtual(const DWARFDIE &die); 416 417 // Given a die_offset, figure out the symbol context representing that die. 418 bool ResolveFunction(const DWARFDIE &die, bool include_inlines, 419 lldb_private::SymbolContextList &sc_list); 420 421 /// Resolve functions and (possibly) blocks for the given file address and a 422 /// compile unit. The compile unit comes from the sc argument and it must be 423 /// set. The results of the lookup (if any) are written back to the symbol 424 /// context. 425 void ResolveFunctionAndBlock(lldb::addr_t file_vm_addr, bool lookup_block, 426 lldb_private::SymbolContext &sc); 427 428 virtual lldb::TypeSP 429 FindDefinitionTypeForDWARFDeclContext(const DWARFDeclContext &die_decl_ctx); 430 431 virtual lldb::TypeSP 432 FindCompleteObjCDefinitionTypeForDIE(const DWARFDIE &die, 433 lldb_private::ConstString type_name, 434 bool must_be_implementation); 435 436 lldb_private::Symbol * 437 GetObjCClassSymbol(lldb_private::ConstString objc_class_name); 438 439 lldb::TypeSP GetTypeForDIE(const DWARFDIE &die, 440 bool resolve_function_context = false); 441 442 void SetDebugMapModule(const lldb::ModuleSP &module_sp) { 443 m_debug_map_module_wp = module_sp; 444 } 445 446 SymbolFileDWARFDebugMap *GetDebugMapSymfile(); 447 448 DWARFDIE 449 FindBlockContainingSpecification(const DIERef &func_die_ref, 450 dw_offset_t spec_block_die_offset); 451 452 DWARFDIE 453 FindBlockContainingSpecification(const DWARFDIE &die, 454 dw_offset_t spec_block_die_offset); 455 456 virtual UniqueDWARFASTTypeMap &GetUniqueDWARFASTTypeMap(); 457 458 bool DIEDeclContextsMatch(const DWARFDIE &die1, const DWARFDIE &die2); 459 460 bool ClassContainsSelector(const DWARFDIE &class_die, 461 lldb_private::ConstString selector); 462 463 /// Parse call site entries (DW_TAG_call_site), including any nested call site 464 /// parameters (DW_TAG_call_site_parameter). 465 std::vector<std::unique_ptr<lldb_private::CallEdge>> 466 CollectCallEdges(lldb::ModuleSP module, DWARFDIE function_die); 467 468 /// If this symbol file is linked to by a debug map (see 469 /// SymbolFileDWARFDebugMap), and \p file_addr is a file address relative to 470 /// an object file, adjust \p file_addr so that it is relative to the main 471 /// binary. Returns the adjusted address, or \p file_addr if no adjustment is 472 /// needed, on success and LLDB_INVALID_ADDRESS otherwise. 473 lldb::addr_t FixupAddress(lldb::addr_t file_addr); 474 475 bool FixupAddress(lldb_private::Address &addr); 476 477 typedef llvm::SetVector<lldb_private::Type *> TypeSet; 478 479 void GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset, 480 dw_offset_t max_die_offset, uint32_t type_mask, 481 TypeSet &type_set); 482 483 typedef lldb_private::RangeDataVector<lldb::addr_t, lldb::addr_t, 484 lldb_private::Variable *> 485 GlobalVariableMap; 486 487 GlobalVariableMap &GetGlobalAranges(); 488 489 void UpdateExternalModuleListIfNeeded(); 490 491 virtual DIEToTypePtr &GetDIEToType() { return m_die_to_type; } 492 493 virtual DIEToVariableSP &GetDIEToVariable() { return m_die_to_variable_sp; } 494 495 virtual DIEToClangType &GetForwardDeclDieToClangType() { 496 return m_forward_decl_die_to_clang_type; 497 } 498 499 virtual ClangTypeToDIE &GetForwardDeclClangTypeToDie() { 500 return m_forward_decl_clang_type_to_die; 501 } 502 503 void BuildCuTranslationTable(); 504 llvm::Optional<uint32_t> GetDWARFUnitIndex(uint32_t cu_idx); 505 506 struct DecodedUID { 507 SymbolFileDWARF &dwarf; 508 DIERef ref; 509 }; 510 llvm::Optional<DecodedUID> DecodeUID(lldb::user_id_t uid); 511 512 void FindDwpSymbolFile(); 513 514 const lldb_private::FileSpecList &GetTypeUnitSupportFiles(DWARFTypeUnit &tu); 515 516 void InitializeFirstCodeAddressRecursive( 517 const lldb_private::SectionList §ion_list); 518 519 void InitializeFirstCodeAddress(); 520 521 lldb::ModuleWP m_debug_map_module_wp; 522 SymbolFileDWARFDebugMap *m_debug_map_symfile; 523 524 llvm::once_flag m_dwp_symfile_once_flag; 525 std::shared_ptr<SymbolFileDWARFDwo> m_dwp_symfile; 526 527 lldb_private::DWARFContext m_context; 528 529 llvm::once_flag m_info_once_flag; 530 std::unique_ptr<DWARFDebugInfo> m_info; 531 532 std::unique_ptr<DWARFDebugAbbrev> m_abbr; 533 std::unique_ptr<GlobalVariableMap> m_global_aranges_up; 534 535 typedef std::unordered_map<lldb::offset_t, lldb_private::DebugMacrosSP> 536 DebugMacrosMap; 537 DebugMacrosMap m_debug_macros_map; 538 539 ExternalTypeModuleMap m_external_type_modules; 540 std::unique_ptr<lldb_private::DWARFIndex> m_index; 541 bool m_fetched_external_modules : 1; 542 lldb_private::LazyBool m_supports_DW_AT_APPLE_objc_complete_type; 543 544 typedef std::set<DIERef> DIERefSet; 545 typedef llvm::StringMap<DIERefSet> NameToOffsetMap; 546 NameToOffsetMap m_function_scope_qualified_name_map; 547 std::unique_ptr<DWARFDebugRanges> m_ranges; 548 UniqueDWARFASTTypeMap m_unique_ast_type_map; 549 DIEToTypePtr m_die_to_type; 550 DIEToVariableSP m_die_to_variable_sp; 551 DIEToClangType m_forward_decl_die_to_clang_type; 552 ClangTypeToDIE m_forward_decl_clang_type_to_die; 553 llvm::DenseMap<dw_offset_t, lldb_private::FileSpecList> 554 m_type_unit_support_files; 555 std::vector<uint32_t> m_lldb_cu_to_dwarf_unit; 556 /// DWARF does not provide a good way for traditional (concatenating) linkers 557 /// to invalidate debug info describing dead-stripped code. These linkers will 558 /// keep the debug info but resolve any addresses referring to such code as 559 /// zero (BFD) or a small positive integer (zero + relocation addend -- GOLD). 560 /// Try to filter out this debug info by comparing it to the lowest code 561 /// address in the module. 562 lldb::addr_t m_first_code_address = LLDB_INVALID_ADDRESS; 563 lldb_private::StatsDuration m_parse_time; 564 std::atomic_flag m_dwo_warning_issued = ATOMIC_FLAG_INIT; 565 }; 566 567 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_SYMBOLFILEDWARF_H 568