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