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