1 //===-- SymbolFile.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_SYMBOL_SYMBOLFILE_H 10 #define LLDB_SYMBOL_SYMBOLFILE_H 11 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/ModuleList.h" 14 #include "lldb/Core/PluginInterface.h" 15 #include "lldb/Core/SourceLocationSpec.h" 16 #include "lldb/Symbol/CompilerDecl.h" 17 #include "lldb/Symbol/CompilerDeclContext.h" 18 #include "lldb/Symbol/CompilerType.h" 19 #include "lldb/Symbol/Function.h" 20 #include "lldb/Symbol/SourceModule.h" 21 #include "lldb/Symbol/Type.h" 22 #include "lldb/Symbol/TypeList.h" 23 #include "lldb/Symbol/TypeSystem.h" 24 #include "lldb/Target/Statistics.h" 25 #include "lldb/Utility/XcodeSDK.h" 26 #include "lldb/lldb-private.h" 27 #include "llvm/ADT/DenseSet.h" 28 #include "llvm/Support/Errc.h" 29 30 #include <mutex> 31 #include <optional> 32 33 #if defined(LLDB_CONFIGURATION_DEBUG) 34 #define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock()) 35 #else 36 #define ASSERT_MODULE_LOCK(expr) ((void)0) 37 #endif 38 39 namespace lldb_private { 40 41 /// Provides public interface for all SymbolFiles. Any protected 42 /// virtual members should go into SymbolFileCommon; most SymbolFile 43 /// implementations should inherit from SymbolFileCommon to override 44 /// the behaviors except SymbolFileOnDemand which inherits 45 /// public interfaces from SymbolFile and forward to underlying concrete 46 /// SymbolFile implementation. 47 class SymbolFile : public PluginInterface { 48 /// LLVM RTTI support. 49 static char ID; 50 51 public: 52 /// LLVM RTTI support. 53 /// \{ 54 virtual bool isA(const void *ClassID) const { return ClassID == &ID; } 55 static bool classof(const SymbolFile *obj) { return obj->isA(&ID); } 56 /// \} 57 58 // Symbol file ability bits. 59 // 60 // Each symbol file can claim to support one or more symbol file abilities. 61 // These get returned from SymbolFile::GetAbilities(). These help us to 62 // determine which plug-in will be best to load the debug information found 63 // in files. 64 enum Abilities { 65 CompileUnits = (1u << 0), 66 LineTables = (1u << 1), 67 Functions = (1u << 2), 68 Blocks = (1u << 3), 69 GlobalVariables = (1u << 4), 70 LocalVariables = (1u << 5), 71 VariableTypes = (1u << 6), 72 kAllAbilities = ((1u << 7) - 1u) 73 }; 74 75 static SymbolFile *FindPlugin(lldb::ObjectFileSP objfile_sp); 76 77 // Constructors and Destructors 78 SymbolFile() = default; 79 80 ~SymbolFile() override = default; 81 82 /// SymbolFileOnDemand class overrides this to return the underlying 83 /// backing SymbolFile implementation that loads on-demand. 84 virtual SymbolFile *GetBackingSymbolFile() { return this; } 85 86 /// Get a mask of what this symbol file supports for the object file 87 /// that it was constructed with. 88 /// 89 /// Each symbol file gets to respond with a mask of abilities that 90 /// it supports for each object file. This happens when we are 91 /// trying to figure out which symbol file plug-in will get used 92 /// for a given object file. The plug-in that responds with the 93 /// best mix of "SymbolFile::Abilities" bits set, will get chosen to 94 /// be the symbol file parser. This allows each plug-in to check for 95 /// sections that contain data a symbol file plug-in would need. For 96 /// example the DWARF plug-in requires DWARF sections in a file that 97 /// contain debug information. If the DWARF plug-in doesn't find 98 /// these sections, it won't respond with many ability bits set, and 99 /// we will probably fall back to the symbol table SymbolFile plug-in 100 /// which uses any information in the symbol table. Also, plug-ins 101 /// might check for some specific symbols in a symbol table in the 102 /// case where the symbol table contains debug information (STABS 103 /// and COFF). Not a lot of work should happen in these functions 104 /// as the plug-in might not get selected due to another plug-in 105 /// having more abilities. Any initialization work should be saved 106 /// for "void SymbolFile::InitializeObject()" which will get called 107 /// on the SymbolFile object with the best set of abilities. 108 /// 109 /// \return 110 /// A uint32_t mask containing bits from the SymbolFile::Abilities 111 /// enumeration. Any bits that are set represent an ability that 112 /// this symbol plug-in can parse from the object file. 113 virtual uint32_t GetAbilities() = 0; 114 virtual uint32_t CalculateAbilities() = 0; 115 116 /// Symbols file subclasses should override this to return the Module that 117 /// owns the TypeSystem that this symbol file modifies type information in. 118 virtual std::recursive_mutex &GetModuleMutex() const; 119 120 /// Initialize the SymbolFile object. 121 /// 122 /// The SymbolFile object with the best set of abilities (detected 123 /// in "uint32_t SymbolFile::GetAbilities()) will have this function 124 /// called if it is chosen to parse an object file. More complete 125 /// initialization can happen in this function which will get called 126 /// prior to any other functions in the SymbolFile protocol. 127 virtual void InitializeObject() {} 128 129 /// Whether debug info will be loaded or not. 130 /// 131 /// It will be true for most implementations except SymbolFileOnDemand. 132 virtual bool GetLoadDebugInfoEnabled() { return true; } 133 134 /// Specify debug info should be loaded. 135 /// 136 /// It will be no-op for most implementations except SymbolFileOnDemand. 137 virtual void SetLoadDebugInfoEnabled() {} 138 139 // Compile Unit function calls 140 // Approach 1 - iterator 141 virtual uint32_t GetNumCompileUnits() = 0; 142 virtual lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx) = 0; 143 144 virtual Symtab *GetSymtab() = 0; 145 146 virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0; 147 /// Return the Xcode SDK comp_unit was compiled against. 148 virtual XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) { return {}; } 149 virtual size_t ParseFunctions(CompileUnit &comp_unit) = 0; 150 virtual bool ParseLineTable(CompileUnit &comp_unit) = 0; 151 virtual bool ParseDebugMacros(CompileUnit &comp_unit) = 0; 152 153 /// Apply a lambda to each external lldb::Module referenced by this 154 /// \p comp_unit. Recursively also descends into the referenced external 155 /// modules of any encountered compilation unit. 156 /// 157 /// This function can be used to traverse Clang -gmodules debug 158 /// information, which is stored in DWARF files separate from the 159 /// object files. 160 /// 161 /// \param comp_unit 162 /// When this SymbolFile consists of multiple auxilliary 163 /// SymbolFiles, for example, a Darwin debug map that references 164 /// multiple .o files, comp_unit helps choose the auxilliary 165 /// file. In most other cases comp_unit's symbol file is 166 /// identical with *this. 167 /// 168 /// \param[in] lambda 169 /// The lambda that should be applied to every function. The lambda can 170 /// return true if the iteration should be aborted earlier. 171 /// 172 /// \param visited_symbol_files 173 /// A set of SymbolFiles that were already visited to avoid 174 /// visiting one file more than once. 175 /// 176 /// \return 177 /// If the lambda early-exited, this function returns true to 178 /// propagate the early exit. 179 virtual bool ForEachExternalModule( 180 lldb_private::CompileUnit &comp_unit, 181 llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files, 182 llvm::function_ref<bool(Module &)> lambda) { 183 return false; 184 } 185 virtual bool ParseSupportFiles(CompileUnit &comp_unit, 186 FileSpecList &support_files) = 0; 187 virtual size_t ParseTypes(CompileUnit &comp_unit) = 0; 188 virtual bool ParseIsOptimized(CompileUnit &comp_unit) { return false; } 189 190 virtual bool 191 ParseImportedModules(const SymbolContext &sc, 192 std::vector<SourceModule> &imported_modules) = 0; 193 virtual size_t ParseBlocksRecursive(Function &func) = 0; 194 virtual size_t ParseVariablesForContext(const SymbolContext &sc) = 0; 195 virtual Type *ResolveTypeUID(lldb::user_id_t type_uid) = 0; 196 197 /// The characteristics of an array type. 198 struct ArrayInfo { 199 int64_t first_index = 0; 200 llvm::SmallVector<uint64_t, 1> element_orders; 201 uint32_t byte_stride = 0; 202 uint32_t bit_stride = 0; 203 }; 204 /// If \c type_uid points to an array type, return its characteristics. 205 /// To support variable-length array types, this function takes an 206 /// optional \p ExecutionContext. If \c exe_ctx is non-null, the 207 /// dynamic characteristics for that context are returned. 208 virtual std::optional<ArrayInfo> 209 GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, 210 const lldb_private::ExecutionContext *exe_ctx) = 0; 211 212 virtual bool CompleteType(CompilerType &compiler_type) = 0; 213 virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {} 214 virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { 215 return CompilerDecl(); 216 } 217 virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) { 218 return CompilerDeclContext(); 219 } 220 virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) { 221 return CompilerDeclContext(); 222 } 223 virtual uint32_t ResolveSymbolContext(const Address &so_addr, 224 lldb::SymbolContextItem resolve_scope, 225 SymbolContext &sc) = 0; 226 227 /// Get an error that describes why variables might be missing for a given 228 /// symbol context. 229 /// 230 /// If there is an error in the debug information that prevents variables from 231 /// being fetched, this error will get filled in. If there is no debug 232 /// informaiton, no error should be returned. But if there is debug 233 /// information and something prevents the variables from being available a 234 /// valid error should be returned. Valid cases include: 235 /// - compiler option that removes variables (-gline-tables-only) 236 /// - missing external files 237 /// - .dwo files in fission are not accessible or missing 238 /// - .o files on darwin when not using dSYM files that are not accessible 239 /// or missing 240 /// - mismatched exteral files 241 /// - .dwo files in fission where the DWO ID doesn't match 242 /// - .o files on darwin when modification timestamp doesn't match 243 /// - corrupted debug info 244 /// 245 /// \param[in] frame 246 /// The stack frame to use as a basis for the context to check. The frame 247 /// address can be used if there is not debug info due to it not being able 248 /// to be loaded, or if there is a debug info context, like a compile unit, 249 /// or function, it can be used to track down more information on why 250 /// variables are missing. 251 /// 252 /// \returns 253 /// An error specifying why there should have been debug info with variable 254 /// information but the variables were not able to be resolved. 255 Status GetFrameVariableError(StackFrame &frame) { 256 Status err = CalculateFrameVariableError(frame); 257 if (err.Fail()) 258 SetDebugInfoHadFrameVariableErrors(); 259 return err; 260 } 261 262 /// Subclasses will override this function to for GetFrameVariableError(). 263 /// 264 /// This allows GetFrameVariableError() to set the member variable 265 /// m_debug_info_had_variable_errors correctly without users having to do it 266 /// manually which is error prone. 267 virtual Status CalculateFrameVariableError(StackFrame &frame) { 268 return Status(); 269 } 270 virtual uint32_t 271 ResolveSymbolContext(const SourceLocationSpec &src_location_spec, 272 lldb::SymbolContextItem resolve_scope, 273 SymbolContextList &sc_list); 274 275 virtual void DumpClangAST(Stream &s) {} 276 virtual void FindGlobalVariables(ConstString name, 277 const CompilerDeclContext &parent_decl_ctx, 278 uint32_t max_matches, 279 VariableList &variables); 280 virtual void FindGlobalVariables(const RegularExpression ®ex, 281 uint32_t max_matches, 282 VariableList &variables); 283 virtual void FindFunctions(const Module::LookupInfo &lookup_info, 284 const CompilerDeclContext &parent_decl_ctx, 285 bool include_inlines, SymbolContextList &sc_list); 286 virtual void FindFunctions(const RegularExpression ®ex, 287 bool include_inlines, SymbolContextList &sc_list); 288 virtual void 289 FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx, 290 uint32_t max_matches, 291 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 292 TypeMap &types); 293 294 /// Find types specified by a CompilerContextPattern. 295 /// \param languages 296 /// Only return results in these languages. 297 /// \param searched_symbol_files 298 /// Prevents one file from being visited multiple times. 299 virtual void 300 FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 301 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 302 TypeMap &types); 303 304 virtual void 305 GetMangledNamesForFunction(const std::string &scope_qualified_name, 306 std::vector<ConstString> &mangled_names); 307 308 virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope, 309 lldb::TypeClass type_mask, 310 lldb_private::TypeList &type_list) = 0; 311 312 virtual void PreloadSymbols(); 313 314 virtual llvm::Expected<lldb::TypeSystemSP> 315 GetTypeSystemForLanguage(lldb::LanguageType language) = 0; 316 317 virtual CompilerDeclContext 318 FindNamespace(ConstString name, const CompilerDeclContext &parent_decl_ctx) { 319 return CompilerDeclContext(); 320 } 321 322 virtual ObjectFile *GetObjectFile() = 0; 323 virtual const ObjectFile *GetObjectFile() const = 0; 324 virtual ObjectFile *GetMainObjectFile() = 0; 325 326 virtual std::vector<std::unique_ptr<CallEdge>> 327 ParseCallEdgesInFunction(UserID func_id) { 328 return {}; 329 } 330 331 virtual void AddSymbols(Symtab &symtab) {} 332 333 /// Notify the SymbolFile that the file addresses in the Sections 334 /// for this module have been changed. 335 virtual void SectionFileAddressesChanged() = 0; 336 337 struct RegisterInfoResolver { 338 virtual ~RegisterInfoResolver(); // anchor 339 340 virtual const RegisterInfo *ResolveName(llvm::StringRef name) const = 0; 341 virtual const RegisterInfo *ResolveNumber(lldb::RegisterKind kind, 342 uint32_t number) const = 0; 343 }; 344 virtual lldb::UnwindPlanSP 345 GetUnwindPlan(const Address &address, const RegisterInfoResolver &resolver) { 346 return nullptr; 347 } 348 349 /// Return the number of stack bytes taken up by the parameters to this 350 /// function. 351 virtual llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) { 352 return llvm::createStringError(make_error_code(llvm::errc::not_supported), 353 "Operation not supported."); 354 } 355 356 virtual void Dump(Stream &s) = 0; 357 358 /// Metrics gathering functions 359 360 /// Return the size in bytes of all debug information in the symbol file. 361 /// 362 /// If the debug information is contained in sections of an ObjectFile, then 363 /// this call should add the size of all sections that contain debug 364 /// information. Symbols the symbol tables are not considered debug 365 /// information for this call to make it easy and quick for this number to be 366 /// calculated. If the symbol file is all debug information, the size of the 367 /// entire file should be returned. The default implementation of this 368 /// function will iterate over all sections in a module and add up their 369 /// debug info only section byte sizes. 370 virtual uint64_t GetDebugInfoSize() = 0; 371 372 /// Return the time taken to parse the debug information. 373 /// 374 /// \returns 0.0 if no information has been parsed or if there is 375 /// no computational cost to parsing the debug information. 376 virtual StatsDuration::Duration GetDebugInfoParseTime() { return {}; } 377 378 /// Return the time it took to index the debug information in the object 379 /// file. 380 /// 381 /// \returns 0.0 if the file doesn't need to be indexed or if it 382 /// hasn't been indexed yet, or a valid duration if it has. 383 virtual StatsDuration::Duration GetDebugInfoIndexTime() { return {}; } 384 385 /// Get the additional modules that this symbol file uses to parse debug info. 386 /// 387 /// Some debug info is stored in stand alone object files that are represented 388 /// by unique modules that will show up in the statistics module list. Return 389 /// a list of modules that are not in the target module list that this symbol 390 /// file is currently using so that they can be tracked and assoicated with 391 /// the module in the statistics. 392 virtual ModuleList GetDebugInfoModules() { return ModuleList(); } 393 394 /// Accessors for the bool that indicates if the debug info index was loaded 395 /// from, or saved to the module index cache. 396 /// 397 /// In statistics it is handy to know if a module's debug info was loaded from 398 /// or saved to the cache. When the debug info index is loaded from the cache 399 /// startup times can be faster. When the cache is enabled and the debug info 400 /// index is saved to the cache, debug sessions can be slower. These accessors 401 /// can be accessed by the statistics and emitted to help track these costs. 402 /// \{ 403 virtual bool GetDebugInfoIndexWasLoadedFromCache() const = 0; 404 virtual void SetDebugInfoIndexWasLoadedFromCache() = 0; 405 virtual bool GetDebugInfoIndexWasSavedToCache() const = 0; 406 virtual void SetDebugInfoIndexWasSavedToCache() = 0; 407 /// \} 408 409 /// Accessors for the bool that indicates if there was debug info, but errors 410 /// stopped variables from being able to be displayed correctly. See 411 /// GetFrameVariableError() for details on what are considered errors. 412 virtual bool GetDebugInfoHadFrameVariableErrors() const = 0; 413 virtual void SetDebugInfoHadFrameVariableErrors() = 0; 414 415 virtual lldb::TypeSP 416 MakeType(lldb::user_id_t uid, ConstString name, 417 std::optional<uint64_t> byte_size, SymbolContextScope *context, 418 lldb::user_id_t encoding_uid, 419 Type::EncodingDataType encoding_uid_type, const Declaration &decl, 420 const CompilerType &compiler_qual_type, 421 Type::ResolveState compiler_type_resolve_state, 422 uint32_t opaque_payload = 0) = 0; 423 424 virtual lldb::TypeSP CopyType(const lldb::TypeSP &other_type) = 0; 425 426 protected: 427 void AssertModuleLock(); 428 429 private: 430 SymbolFile(const SymbolFile &) = delete; 431 const SymbolFile &operator=(const SymbolFile &) = delete; 432 }; 433 434 /// Containing protected virtual methods for child classes to override. 435 /// Most actual SymbolFile implementations should inherit from this class. 436 class SymbolFileCommon : public SymbolFile { 437 /// LLVM RTTI support. 438 static char ID; 439 440 public: 441 /// LLVM RTTI support. 442 /// \{ 443 bool isA(const void *ClassID) const override { 444 return ClassID == &ID || SymbolFile::isA(ClassID); 445 } 446 static bool classof(const SymbolFileCommon *obj) { return obj->isA(&ID); } 447 /// \} 448 449 // Constructors and Destructors 450 SymbolFileCommon(lldb::ObjectFileSP objfile_sp) 451 : m_objfile_sp(std::move(objfile_sp)) {} 452 453 ~SymbolFileCommon() override = default; 454 455 uint32_t GetAbilities() override { 456 if (!m_calculated_abilities) { 457 m_abilities = CalculateAbilities(); 458 m_calculated_abilities = true; 459 } 460 return m_abilities; 461 } 462 463 Symtab *GetSymtab() override; 464 465 ObjectFile *GetObjectFile() override { return m_objfile_sp.get(); } 466 const ObjectFile *GetObjectFile() const override { 467 return m_objfile_sp.get(); 468 } 469 ObjectFile *GetMainObjectFile() override; 470 471 /// Notify the SymbolFile that the file addresses in the Sections 472 /// for this module have been changed. 473 void SectionFileAddressesChanged() override; 474 475 // Compile Unit function calls 476 // Approach 1 - iterator 477 uint32_t GetNumCompileUnits() override; 478 lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx) override; 479 480 llvm::Expected<lldb::TypeSystemSP> 481 GetTypeSystemForLanguage(lldb::LanguageType language) override; 482 483 void Dump(Stream &s) override; 484 485 uint64_t GetDebugInfoSize() override; 486 487 bool GetDebugInfoIndexWasLoadedFromCache() const override { 488 return m_index_was_loaded_from_cache; 489 } 490 void SetDebugInfoIndexWasLoadedFromCache() override { 491 m_index_was_loaded_from_cache = true; 492 } 493 bool GetDebugInfoIndexWasSavedToCache() const override { 494 return m_index_was_saved_to_cache; 495 } 496 void SetDebugInfoIndexWasSavedToCache() override { 497 m_index_was_saved_to_cache = true; 498 } 499 bool GetDebugInfoHadFrameVariableErrors() const override { 500 return m_debug_info_had_variable_errors; 501 } 502 void SetDebugInfoHadFrameVariableErrors() override { 503 m_debug_info_had_variable_errors = true; 504 } 505 506 /// This function is used to create types that belong to a SymbolFile. The 507 /// symbol file will own a strong reference to the type in an internal type 508 /// list. 509 lldb::TypeSP MakeType(lldb::user_id_t uid, ConstString name, 510 std::optional<uint64_t> byte_size, 511 SymbolContextScope *context, 512 lldb::user_id_t encoding_uid, 513 Type::EncodingDataType encoding_uid_type, 514 const Declaration &decl, 515 const CompilerType &compiler_qual_type, 516 Type::ResolveState compiler_type_resolve_state, 517 uint32_t opaque_payload = 0) override { 518 lldb::TypeSP type_sp (new Type( 519 uid, this, name, byte_size, context, encoding_uid, 520 encoding_uid_type, decl, compiler_qual_type, 521 compiler_type_resolve_state, opaque_payload)); 522 m_type_list.Insert(type_sp); 523 return type_sp; 524 } 525 526 lldb::TypeSP CopyType(const lldb::TypeSP &other_type) override { 527 // Make sure the real symbol file matches when copying types. 528 if (GetBackingSymbolFile() != other_type->GetSymbolFile()) 529 return lldb::TypeSP(); 530 lldb::TypeSP type_sp(new Type(*other_type)); 531 m_type_list.Insert(type_sp); 532 return type_sp; 533 } 534 535 protected: 536 virtual uint32_t CalculateNumCompileUnits() = 0; 537 virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0; 538 virtual TypeList &GetTypeList() { return m_type_list; } 539 void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp); 540 541 lldb::ObjectFileSP m_objfile_sp; // Keep a reference to the object file in 542 // case it isn't the same as the module 543 // object file (debug symbols in a separate 544 // file) 545 std::optional<std::vector<lldb::CompUnitSP>> m_compile_units; 546 TypeList m_type_list; 547 uint32_t m_abilities = 0; 548 bool m_calculated_abilities = false; 549 bool m_index_was_loaded_from_cache = false; 550 bool m_index_was_saved_to_cache = false; 551 /// Set to true if any variable feteching errors have been found when calling 552 /// GetFrameVariableError(). This will be emitted in the "statistics dump" 553 /// information for a module. 554 bool m_debug_info_had_variable_errors = false; 555 556 private: 557 SymbolFileCommon(const SymbolFileCommon &) = delete; 558 const SymbolFileCommon &operator=(const SymbolFileCommon &) = delete; 559 560 /// Do not use m_symtab directly, as it may be freed. Use GetSymtab() 561 /// to access it instead. 562 Symtab *m_symtab = nullptr; 563 }; 564 565 } // namespace lldb_private 566 567 #endif // LLDB_SYMBOL_SYMBOLFILE_H 568