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