10b57cec5SDimitry Andric //===-- SymbolFile.h --------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 9*5ffd83dbSDimitry Andric #ifndef LLDB_SYMBOL_SYMBOLFILE_H 10*5ffd83dbSDimitry Andric #define LLDB_SYMBOL_SYMBOLFILE_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "lldb/Core/PluginInterface.h" 130b57cec5SDimitry Andric #include "lldb/Symbol/CompilerDecl.h" 140b57cec5SDimitry Andric #include "lldb/Symbol/CompilerDeclContext.h" 150b57cec5SDimitry Andric #include "lldb/Symbol/CompilerType.h" 160b57cec5SDimitry Andric #include "lldb/Symbol/Function.h" 170b57cec5SDimitry Andric #include "lldb/Symbol/SourceModule.h" 180b57cec5SDimitry Andric #include "lldb/Symbol/Type.h" 199dba64beSDimitry Andric #include "lldb/Symbol/TypeList.h" 209dba64beSDimitry Andric #include "lldb/Symbol/TypeSystem.h" 21*5ffd83dbSDimitry Andric #include "lldb/Utility/XcodeSDK.h" 220b57cec5SDimitry Andric #include "lldb/lldb-private.h" 230b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 249dba64beSDimitry Andric #include "llvm/Support/Errc.h" 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #include <mutex> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric #if defined(LLDB_CONFIGURATION_DEBUG) 290b57cec5SDimitry Andric #define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock()) 300b57cec5SDimitry Andric #else 310b57cec5SDimitry Andric #define ASSERT_MODULE_LOCK(expr) ((void)0) 320b57cec5SDimitry Andric #endif 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric namespace lldb_private { 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric class SymbolFile : public PluginInterface { 37480093f4SDimitry Andric /// LLVM RTTI support. 38480093f4SDimitry Andric static char ID; 39480093f4SDimitry Andric 400b57cec5SDimitry Andric public: 41480093f4SDimitry Andric /// LLVM RTTI support. 42480093f4SDimitry Andric /// \{ 43480093f4SDimitry Andric virtual bool isA(const void *ClassID) const { return ClassID == &ID; } 44480093f4SDimitry Andric static bool classof(const SymbolFile *obj) { return obj->isA(&ID); } 45480093f4SDimitry Andric /// \} 46480093f4SDimitry Andric 470b57cec5SDimitry Andric // Symbol file ability bits. 480b57cec5SDimitry Andric // 490b57cec5SDimitry Andric // Each symbol file can claim to support one or more symbol file abilities. 500b57cec5SDimitry Andric // These get returned from SymbolFile::GetAbilities(). These help us to 510b57cec5SDimitry Andric // determine which plug-in will be best to load the debug information found 520b57cec5SDimitry Andric // in files. 530b57cec5SDimitry Andric enum Abilities { 540b57cec5SDimitry Andric CompileUnits = (1u << 0), 550b57cec5SDimitry Andric LineTables = (1u << 1), 560b57cec5SDimitry Andric Functions = (1u << 2), 570b57cec5SDimitry Andric Blocks = (1u << 3), 580b57cec5SDimitry Andric GlobalVariables = (1u << 4), 590b57cec5SDimitry Andric LocalVariables = (1u << 5), 600b57cec5SDimitry Andric VariableTypes = (1u << 6), 610b57cec5SDimitry Andric kAllAbilities = ((1u << 7) - 1u) 620b57cec5SDimitry Andric }; 630b57cec5SDimitry Andric 649dba64beSDimitry Andric static SymbolFile *FindPlugin(lldb::ObjectFileSP objfile_sp); 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Constructors and Destructors 679dba64beSDimitry Andric SymbolFile(lldb::ObjectFileSP objfile_sp) 689dba64beSDimitry Andric : m_objfile_sp(std::move(objfile_sp)), m_abilities(0), 699dba64beSDimitry Andric m_calculated_abilities(false) {} 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric ~SymbolFile() override {} 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric /// Get a mask of what this symbol file supports for the object file 740b57cec5SDimitry Andric /// that it was constructed with. 750b57cec5SDimitry Andric /// 760b57cec5SDimitry Andric /// Each symbol file gets to respond with a mask of abilities that 770b57cec5SDimitry Andric /// it supports for each object file. This happens when we are 780b57cec5SDimitry Andric /// trying to figure out which symbol file plug-in will get used 790b57cec5SDimitry Andric /// for a given object file. The plug-in that responds with the 800b57cec5SDimitry Andric /// best mix of "SymbolFile::Abilities" bits set, will get chosen to 810b57cec5SDimitry Andric /// be the symbol file parser. This allows each plug-in to check for 820b57cec5SDimitry Andric /// sections that contain data a symbol file plug-in would need. For 830b57cec5SDimitry Andric /// example the DWARF plug-in requires DWARF sections in a file that 840b57cec5SDimitry Andric /// contain debug information. If the DWARF plug-in doesn't find 850b57cec5SDimitry Andric /// these sections, it won't respond with many ability bits set, and 860b57cec5SDimitry Andric /// we will probably fall back to the symbol table SymbolFile plug-in 870b57cec5SDimitry Andric /// which uses any information in the symbol table. Also, plug-ins 880b57cec5SDimitry Andric /// might check for some specific symbols in a symbol table in the 890b57cec5SDimitry Andric /// case where the symbol table contains debug information (STABS 900b57cec5SDimitry Andric /// and COFF). Not a lot of work should happen in these functions 910b57cec5SDimitry Andric /// as the plug-in might not get selected due to another plug-in 920b57cec5SDimitry Andric /// having more abilities. Any initialization work should be saved 930b57cec5SDimitry Andric /// for "void SymbolFile::InitializeObject()" which will get called 940b57cec5SDimitry Andric /// on the SymbolFile object with the best set of abilities. 950b57cec5SDimitry Andric /// 960b57cec5SDimitry Andric /// \return 970b57cec5SDimitry Andric /// A uint32_t mask containing bits from the SymbolFile::Abilities 980b57cec5SDimitry Andric /// enumeration. Any bits that are set represent an ability that 990b57cec5SDimitry Andric /// this symbol plug-in can parse from the object file. 1000b57cec5SDimitry Andric uint32_t GetAbilities() { 1010b57cec5SDimitry Andric if (!m_calculated_abilities) { 1020b57cec5SDimitry Andric m_abilities = CalculateAbilities(); 1030b57cec5SDimitry Andric m_calculated_abilities = true; 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric return m_abilities; 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric virtual uint32_t CalculateAbilities() = 0; 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric /// Symbols file subclasses should override this to return the Module that 1120b57cec5SDimitry Andric /// owns the TypeSystem that this symbol file modifies type information in. 1130b57cec5SDimitry Andric virtual std::recursive_mutex &GetModuleMutex() const; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric /// Initialize the SymbolFile object. 1160b57cec5SDimitry Andric /// 1170b57cec5SDimitry Andric /// The SymbolFile object with the best set of abilities (detected 1180b57cec5SDimitry Andric /// in "uint32_t SymbolFile::GetAbilities()) will have this function 1190b57cec5SDimitry Andric /// called if it is chosen to parse an object file. More complete 1200b57cec5SDimitry Andric /// initialization can happen in this function which will get called 1210b57cec5SDimitry Andric /// prior to any other functions in the SymbolFile protocol. 1220b57cec5SDimitry Andric virtual void InitializeObject() {} 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric // Compile Unit function calls 1250b57cec5SDimitry Andric // Approach 1 - iterator 1269dba64beSDimitry Andric uint32_t GetNumCompileUnits(); 1279dba64beSDimitry Andric lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx); 1289dba64beSDimitry Andric 1299dba64beSDimitry Andric Symtab *GetSymtab(); 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0; 132*5ffd83dbSDimitry Andric /// Return the Xcode SDK comp_unit was compiled against. 133*5ffd83dbSDimitry Andric virtual XcodeSDK ParseXcodeSDK(CompileUnit &comp_unit) { return {}; } 1340b57cec5SDimitry Andric virtual size_t ParseFunctions(CompileUnit &comp_unit) = 0; 1350b57cec5SDimitry Andric virtual bool ParseLineTable(CompileUnit &comp_unit) = 0; 1360b57cec5SDimitry Andric virtual bool ParseDebugMacros(CompileUnit &comp_unit) = 0; 137480093f4SDimitry Andric 138480093f4SDimitry Andric /// Apply a lambda to each external lldb::Module referenced by this 139480093f4SDimitry Andric /// \p comp_unit. Recursively also descends into the referenced external 140480093f4SDimitry Andric /// modules of any encountered compilation unit. 141480093f4SDimitry Andric /// 142*5ffd83dbSDimitry Andric /// This function can be used to traverse Clang -gmodules debug 143*5ffd83dbSDimitry Andric /// information, which is stored in DWARF files separate from the 144*5ffd83dbSDimitry Andric /// object files. 145*5ffd83dbSDimitry Andric /// 146480093f4SDimitry Andric /// \param comp_unit 147480093f4SDimitry Andric /// When this SymbolFile consists of multiple auxilliary 148480093f4SDimitry Andric /// SymbolFiles, for example, a Darwin debug map that references 149480093f4SDimitry Andric /// multiple .o files, comp_unit helps choose the auxilliary 150480093f4SDimitry Andric /// file. In most other cases comp_unit's symbol file is 151*5ffd83dbSDimitry Andric /// identical with *this. 152480093f4SDimitry Andric /// 153480093f4SDimitry Andric /// \param[in] lambda 154480093f4SDimitry Andric /// The lambda that should be applied to every function. The lambda can 155480093f4SDimitry Andric /// return true if the iteration should be aborted earlier. 156480093f4SDimitry Andric /// 157480093f4SDimitry Andric /// \param visited_symbol_files 158480093f4SDimitry Andric /// A set of SymbolFiles that were already visited to avoid 159480093f4SDimitry Andric /// visiting one file more than once. 160480093f4SDimitry Andric /// 161480093f4SDimitry Andric /// \return 162480093f4SDimitry Andric /// If the lambda early-exited, this function returns true to 163480093f4SDimitry Andric /// propagate the early exit. 164480093f4SDimitry Andric virtual bool ForEachExternalModule( 165480093f4SDimitry Andric lldb_private::CompileUnit &comp_unit, 166480093f4SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files, 167480093f4SDimitry Andric llvm::function_ref<bool(Module &)> lambda) { 168480093f4SDimitry Andric return false; 169480093f4SDimitry Andric } 1700b57cec5SDimitry Andric virtual bool ParseSupportFiles(CompileUnit &comp_unit, 1710b57cec5SDimitry Andric FileSpecList &support_files) = 0; 1720b57cec5SDimitry Andric virtual size_t ParseTypes(CompileUnit &comp_unit) = 0; 1730b57cec5SDimitry Andric virtual bool ParseIsOptimized(CompileUnit &comp_unit) { return false; } 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric virtual bool 1760b57cec5SDimitry Andric ParseImportedModules(const SymbolContext &sc, 1770b57cec5SDimitry Andric std::vector<SourceModule> &imported_modules) = 0; 1780b57cec5SDimitry Andric virtual size_t ParseBlocksRecursive(Function &func) = 0; 1790b57cec5SDimitry Andric virtual size_t ParseVariablesForContext(const SymbolContext &sc) = 0; 1800b57cec5SDimitry Andric virtual Type *ResolveTypeUID(lldb::user_id_t type_uid) = 0; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric /// The characteristics of an array type. 1840b57cec5SDimitry Andric struct ArrayInfo { 1850b57cec5SDimitry Andric int64_t first_index = 0; 1860b57cec5SDimitry Andric llvm::SmallVector<uint64_t, 1> element_orders; 1870b57cec5SDimitry Andric uint32_t byte_stride = 0; 1880b57cec5SDimitry Andric uint32_t bit_stride = 0; 1890b57cec5SDimitry Andric }; 1900b57cec5SDimitry Andric /// If \c type_uid points to an array type, return its characteristics. 1910b57cec5SDimitry Andric /// To support variable-length array types, this function takes an 192*5ffd83dbSDimitry Andric /// optional \p ExecutionContext. If \c exe_ctx is non-null, the 1930b57cec5SDimitry Andric /// dynamic characteristics for that context are returned. 1940b57cec5SDimitry Andric virtual llvm::Optional<ArrayInfo> 1950b57cec5SDimitry Andric GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, 1960b57cec5SDimitry Andric const lldb_private::ExecutionContext *exe_ctx) = 0; 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric virtual bool CompleteType(CompilerType &compiler_type) = 0; 1990b57cec5SDimitry Andric virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {} 2000b57cec5SDimitry Andric virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { 2010b57cec5SDimitry Andric return CompilerDecl(); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) { 2040b57cec5SDimitry Andric return CompilerDeclContext(); 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) { 2070b57cec5SDimitry Andric return CompilerDeclContext(); 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric virtual uint32_t ResolveSymbolContext(const Address &so_addr, 2100b57cec5SDimitry Andric lldb::SymbolContextItem resolve_scope, 2110b57cec5SDimitry Andric SymbolContext &sc) = 0; 2120b57cec5SDimitry Andric virtual uint32_t ResolveSymbolContext(const FileSpec &file_spec, 2130b57cec5SDimitry Andric uint32_t line, bool check_inlines, 2140b57cec5SDimitry Andric lldb::SymbolContextItem resolve_scope, 2150b57cec5SDimitry Andric SymbolContextList &sc_list); 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric virtual void DumpClangAST(Stream &s) {} 218*5ffd83dbSDimitry Andric virtual void FindGlobalVariables(ConstString name, 219*5ffd83dbSDimitry Andric const CompilerDeclContext &parent_decl_ctx, 220*5ffd83dbSDimitry Andric uint32_t max_matches, 221*5ffd83dbSDimitry Andric VariableList &variables); 2229dba64beSDimitry Andric virtual void FindGlobalVariables(const RegularExpression ®ex, 2230b57cec5SDimitry Andric uint32_t max_matches, 2240b57cec5SDimitry Andric VariableList &variables); 2259dba64beSDimitry Andric virtual void FindFunctions(ConstString name, 226*5ffd83dbSDimitry Andric const CompilerDeclContext &parent_decl_ctx, 2270b57cec5SDimitry Andric lldb::FunctionNameType name_type_mask, 2289dba64beSDimitry Andric bool include_inlines, SymbolContextList &sc_list); 2299dba64beSDimitry Andric virtual void FindFunctions(const RegularExpression ®ex, 2309dba64beSDimitry Andric bool include_inlines, SymbolContextList &sc_list); 2319dba64beSDimitry Andric virtual void 232*5ffd83dbSDimitry Andric FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx, 2339dba64beSDimitry Andric uint32_t max_matches, 2340b57cec5SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 2350b57cec5SDimitry Andric TypeMap &types); 2369dba64beSDimitry Andric 2379dba64beSDimitry Andric /// Find types specified by a CompilerContextPattern. 238480093f4SDimitry Andric /// \param languages 239480093f4SDimitry Andric /// Only return results in these languages. 240480093f4SDimitry Andric /// \param searched_symbol_files 241480093f4SDimitry Andric /// Prevents one file from being visited multiple times. 242480093f4SDimitry Andric virtual void 243480093f4SDimitry Andric FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 244480093f4SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 245480093f4SDimitry Andric TypeMap &types); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric virtual void 2480b57cec5SDimitry Andric GetMangledNamesForFunction(const std::string &scope_qualified_name, 2490b57cec5SDimitry Andric std::vector<ConstString> &mangled_names); 2509dba64beSDimitry Andric 2519dba64beSDimitry Andric virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope, 2520b57cec5SDimitry Andric lldb::TypeClass type_mask, 2530b57cec5SDimitry Andric lldb_private::TypeList &type_list) = 0; 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric virtual void PreloadSymbols(); 2560b57cec5SDimitry Andric 2579dba64beSDimitry Andric virtual llvm::Expected<lldb_private::TypeSystem &> 2580b57cec5SDimitry Andric GetTypeSystemForLanguage(lldb::LanguageType language); 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric virtual CompilerDeclContext 261*5ffd83dbSDimitry Andric FindNamespace(ConstString name, const CompilerDeclContext &parent_decl_ctx) { 2620b57cec5SDimitry Andric return CompilerDeclContext(); 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2659dba64beSDimitry Andric ObjectFile *GetObjectFile() { return m_objfile_sp.get(); } 2669dba64beSDimitry Andric const ObjectFile *GetObjectFile() const { return m_objfile_sp.get(); } 2679dba64beSDimitry Andric ObjectFile *GetMainObjectFile(); 2680b57cec5SDimitry Andric 269480093f4SDimitry Andric virtual std::vector<std::unique_ptr<CallEdge>> 270480093f4SDimitry Andric ParseCallEdgesInFunction(UserID func_id) { 2710b57cec5SDimitry Andric return {}; 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric virtual void AddSymbols(Symtab &symtab) {} 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric /// Notify the SymbolFile that the file addresses in the Sections 2770b57cec5SDimitry Andric /// for this module have been changed. 2789dba64beSDimitry Andric virtual void SectionFileAddressesChanged(); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric struct RegisterInfoResolver { 2810b57cec5SDimitry Andric virtual ~RegisterInfoResolver(); // anchor 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric virtual const RegisterInfo *ResolveName(llvm::StringRef name) const = 0; 2840b57cec5SDimitry Andric virtual const RegisterInfo *ResolveNumber(lldb::RegisterKind kind, 2850b57cec5SDimitry Andric uint32_t number) const = 0; 2860b57cec5SDimitry Andric }; 2870b57cec5SDimitry Andric virtual lldb::UnwindPlanSP 2880b57cec5SDimitry Andric GetUnwindPlan(const Address &address, const RegisterInfoResolver &resolver) { 2890b57cec5SDimitry Andric return nullptr; 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2929dba64beSDimitry Andric /// Return the number of stack bytes taken up by the parameters to this 2939dba64beSDimitry Andric /// function. 2949dba64beSDimitry Andric virtual llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) { 2959dba64beSDimitry Andric return llvm::createStringError(make_error_code(llvm::errc::not_supported), 2969dba64beSDimitry Andric "Operation not supported."); 2979dba64beSDimitry Andric } 2989dba64beSDimitry Andric 2999dba64beSDimitry Andric virtual void Dump(Stream &s); 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric protected: 3020b57cec5SDimitry Andric void AssertModuleLock(); 3039dba64beSDimitry Andric virtual uint32_t CalculateNumCompileUnits() = 0; 3049dba64beSDimitry Andric virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0; 3059dba64beSDimitry Andric virtual TypeList &GetTypeList() { return m_type_list; } 3060b57cec5SDimitry Andric 3079dba64beSDimitry Andric void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp); 3089dba64beSDimitry Andric 3099dba64beSDimitry Andric lldb::ObjectFileSP m_objfile_sp; // Keep a reference to the object file in 3109dba64beSDimitry Andric // case it isn't the same as the module 3119dba64beSDimitry Andric // object file (debug symbols in a separate 3129dba64beSDimitry Andric // file) 3139dba64beSDimitry Andric llvm::Optional<std::vector<lldb::CompUnitSP>> m_compile_units; 3149dba64beSDimitry Andric TypeList m_type_list; 3159dba64beSDimitry Andric Symtab *m_symtab = nullptr; 3160b57cec5SDimitry Andric uint32_t m_abilities; 3170b57cec5SDimitry Andric bool m_calculated_abilities; 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric private: 320*5ffd83dbSDimitry Andric SymbolFile(const SymbolFile &) = delete; 321*5ffd83dbSDimitry Andric const SymbolFile &operator=(const SymbolFile &) = delete; 3220b57cec5SDimitry Andric }; 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric } // namespace lldb_private 3250b57cec5SDimitry Andric 326*5ffd83dbSDimitry Andric #endif // LLDB_SYMBOL_SYMBOLFILE_H 327