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 90b57cec5SDimitry Andric #ifndef liblldb_SymbolFile_h_ 100b57cec5SDimitry Andric #define liblldb_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" 210b57cec5SDimitry Andric #include "lldb/lldb-private.h" 220b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 239dba64beSDimitry Andric #include "llvm/Support/Errc.h" 240b57cec5SDimitry Andric 250b57cec5SDimitry Andric #include <mutex> 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric #if defined(LLDB_CONFIGURATION_DEBUG) 280b57cec5SDimitry Andric #define ASSERT_MODULE_LOCK(expr) (expr->AssertModuleLock()) 290b57cec5SDimitry Andric #else 300b57cec5SDimitry Andric #define ASSERT_MODULE_LOCK(expr) ((void)0) 310b57cec5SDimitry Andric #endif 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric namespace lldb_private { 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric class SymbolFile : public PluginInterface { 36*480093f4SDimitry Andric /// LLVM RTTI support. 37*480093f4SDimitry Andric static char ID; 38*480093f4SDimitry Andric 390b57cec5SDimitry Andric public: 40*480093f4SDimitry Andric /// LLVM RTTI support. 41*480093f4SDimitry Andric /// \{ 42*480093f4SDimitry Andric virtual bool isA(const void *ClassID) const { return ClassID == &ID; } 43*480093f4SDimitry Andric static bool classof(const SymbolFile *obj) { return obj->isA(&ID); } 44*480093f4SDimitry Andric /// \} 45*480093f4SDimitry Andric 460b57cec5SDimitry Andric // Symbol file ability bits. 470b57cec5SDimitry Andric // 480b57cec5SDimitry Andric // Each symbol file can claim to support one or more symbol file abilities. 490b57cec5SDimitry Andric // These get returned from SymbolFile::GetAbilities(). These help us to 500b57cec5SDimitry Andric // determine which plug-in will be best to load the debug information found 510b57cec5SDimitry Andric // in files. 520b57cec5SDimitry Andric enum Abilities { 530b57cec5SDimitry Andric CompileUnits = (1u << 0), 540b57cec5SDimitry Andric LineTables = (1u << 1), 550b57cec5SDimitry Andric Functions = (1u << 2), 560b57cec5SDimitry Andric Blocks = (1u << 3), 570b57cec5SDimitry Andric GlobalVariables = (1u << 4), 580b57cec5SDimitry Andric LocalVariables = (1u << 5), 590b57cec5SDimitry Andric VariableTypes = (1u << 6), 600b57cec5SDimitry Andric kAllAbilities = ((1u << 7) - 1u) 610b57cec5SDimitry Andric }; 620b57cec5SDimitry Andric 639dba64beSDimitry Andric static SymbolFile *FindPlugin(lldb::ObjectFileSP objfile_sp); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric // Constructors and Destructors 669dba64beSDimitry Andric SymbolFile(lldb::ObjectFileSP objfile_sp) 679dba64beSDimitry Andric : m_objfile_sp(std::move(objfile_sp)), m_abilities(0), 689dba64beSDimitry Andric m_calculated_abilities(false) {} 690b57cec5SDimitry Andric 700b57cec5SDimitry Andric ~SymbolFile() override {} 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /// Get a mask of what this symbol file supports for the object file 730b57cec5SDimitry Andric /// that it was constructed with. 740b57cec5SDimitry Andric /// 750b57cec5SDimitry Andric /// Each symbol file gets to respond with a mask of abilities that 760b57cec5SDimitry Andric /// it supports for each object file. This happens when we are 770b57cec5SDimitry Andric /// trying to figure out which symbol file plug-in will get used 780b57cec5SDimitry Andric /// for a given object file. The plug-in that responds with the 790b57cec5SDimitry Andric /// best mix of "SymbolFile::Abilities" bits set, will get chosen to 800b57cec5SDimitry Andric /// be the symbol file parser. This allows each plug-in to check for 810b57cec5SDimitry Andric /// sections that contain data a symbol file plug-in would need. For 820b57cec5SDimitry Andric /// example the DWARF plug-in requires DWARF sections in a file that 830b57cec5SDimitry Andric /// contain debug information. If the DWARF plug-in doesn't find 840b57cec5SDimitry Andric /// these sections, it won't respond with many ability bits set, and 850b57cec5SDimitry Andric /// we will probably fall back to the symbol table SymbolFile plug-in 860b57cec5SDimitry Andric /// which uses any information in the symbol table. Also, plug-ins 870b57cec5SDimitry Andric /// might check for some specific symbols in a symbol table in the 880b57cec5SDimitry Andric /// case where the symbol table contains debug information (STABS 890b57cec5SDimitry Andric /// and COFF). Not a lot of work should happen in these functions 900b57cec5SDimitry Andric /// as the plug-in might not get selected due to another plug-in 910b57cec5SDimitry Andric /// having more abilities. Any initialization work should be saved 920b57cec5SDimitry Andric /// for "void SymbolFile::InitializeObject()" which will get called 930b57cec5SDimitry Andric /// on the SymbolFile object with the best set of abilities. 940b57cec5SDimitry Andric /// 950b57cec5SDimitry Andric /// \return 960b57cec5SDimitry Andric /// A uint32_t mask containing bits from the SymbolFile::Abilities 970b57cec5SDimitry Andric /// enumeration. Any bits that are set represent an ability that 980b57cec5SDimitry Andric /// this symbol plug-in can parse from the object file. 990b57cec5SDimitry Andric uint32_t GetAbilities() { 1000b57cec5SDimitry Andric if (!m_calculated_abilities) { 1010b57cec5SDimitry Andric m_abilities = CalculateAbilities(); 1020b57cec5SDimitry Andric m_calculated_abilities = true; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric return m_abilities; 1060b57cec5SDimitry Andric } 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric virtual uint32_t CalculateAbilities() = 0; 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Symbols file subclasses should override this to return the Module that 1110b57cec5SDimitry Andric /// owns the TypeSystem that this symbol file modifies type information in. 1120b57cec5SDimitry Andric virtual std::recursive_mutex &GetModuleMutex() const; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric /// Initialize the SymbolFile object. 1150b57cec5SDimitry Andric /// 1160b57cec5SDimitry Andric /// The SymbolFile object with the best set of abilities (detected 1170b57cec5SDimitry Andric /// in "uint32_t SymbolFile::GetAbilities()) will have this function 1180b57cec5SDimitry Andric /// called if it is chosen to parse an object file. More complete 1190b57cec5SDimitry Andric /// initialization can happen in this function which will get called 1200b57cec5SDimitry Andric /// prior to any other functions in the SymbolFile protocol. 1210b57cec5SDimitry Andric virtual void InitializeObject() {} 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // Compile Unit function calls 1240b57cec5SDimitry Andric // Approach 1 - iterator 1259dba64beSDimitry Andric uint32_t GetNumCompileUnits(); 1269dba64beSDimitry Andric lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx); 1279dba64beSDimitry Andric 1289dba64beSDimitry Andric Symtab *GetSymtab(); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric virtual lldb::LanguageType ParseLanguage(CompileUnit &comp_unit) = 0; 1310b57cec5SDimitry Andric virtual size_t ParseFunctions(CompileUnit &comp_unit) = 0; 1320b57cec5SDimitry Andric virtual bool ParseLineTable(CompileUnit &comp_unit) = 0; 1330b57cec5SDimitry Andric virtual bool ParseDebugMacros(CompileUnit &comp_unit) = 0; 134*480093f4SDimitry Andric 135*480093f4SDimitry Andric /// Apply a lambda to each external lldb::Module referenced by this 136*480093f4SDimitry Andric /// \p comp_unit. Recursively also descends into the referenced external 137*480093f4SDimitry Andric /// modules of any encountered compilation unit. 138*480093f4SDimitry Andric /// 139*480093f4SDimitry Andric /// \param comp_unit 140*480093f4SDimitry Andric /// When this SymbolFile consists of multiple auxilliary 141*480093f4SDimitry Andric /// SymbolFiles, for example, a Darwin debug map that references 142*480093f4SDimitry Andric /// multiple .o files, comp_unit helps choose the auxilliary 143*480093f4SDimitry Andric /// file. In most other cases comp_unit's symbol file is 144*480093f4SDimitry Andric /// identiacal with *this. 145*480093f4SDimitry Andric /// 146*480093f4SDimitry Andric /// \param[in] lambda 147*480093f4SDimitry Andric /// The lambda that should be applied to every function. The lambda can 148*480093f4SDimitry Andric /// return true if the iteration should be aborted earlier. 149*480093f4SDimitry Andric /// 150*480093f4SDimitry Andric /// \param visited_symbol_files 151*480093f4SDimitry Andric /// A set of SymbolFiles that were already visited to avoid 152*480093f4SDimitry Andric /// visiting one file more than once. 153*480093f4SDimitry Andric /// 154*480093f4SDimitry Andric /// \return 155*480093f4SDimitry Andric /// If the lambda early-exited, this function returns true to 156*480093f4SDimitry Andric /// propagate the early exit. 157*480093f4SDimitry Andric virtual bool ForEachExternalModule( 158*480093f4SDimitry Andric lldb_private::CompileUnit &comp_unit, 159*480093f4SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files, 160*480093f4SDimitry Andric llvm::function_ref<bool(Module &)> lambda) { 161*480093f4SDimitry Andric return false; 162*480093f4SDimitry Andric } 1630b57cec5SDimitry Andric virtual bool ParseSupportFiles(CompileUnit &comp_unit, 1640b57cec5SDimitry Andric FileSpecList &support_files) = 0; 1650b57cec5SDimitry Andric virtual size_t ParseTypes(CompileUnit &comp_unit) = 0; 1660b57cec5SDimitry Andric virtual bool ParseIsOptimized(CompileUnit &comp_unit) { return false; } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric virtual bool 1690b57cec5SDimitry Andric ParseImportedModules(const SymbolContext &sc, 1700b57cec5SDimitry Andric std::vector<SourceModule> &imported_modules) = 0; 1710b57cec5SDimitry Andric virtual size_t ParseBlocksRecursive(Function &func) = 0; 1720b57cec5SDimitry Andric virtual size_t ParseVariablesForContext(const SymbolContext &sc) = 0; 1730b57cec5SDimitry Andric virtual Type *ResolveTypeUID(lldb::user_id_t type_uid) = 0; 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric /// The characteristics of an array type. 1770b57cec5SDimitry Andric struct ArrayInfo { 1780b57cec5SDimitry Andric int64_t first_index = 0; 1790b57cec5SDimitry Andric llvm::SmallVector<uint64_t, 1> element_orders; 1800b57cec5SDimitry Andric uint32_t byte_stride = 0; 1810b57cec5SDimitry Andric uint32_t bit_stride = 0; 1820b57cec5SDimitry Andric }; 1830b57cec5SDimitry Andric /// If \c type_uid points to an array type, return its characteristics. 1840b57cec5SDimitry Andric /// To support variable-length array types, this function takes an 1850b57cec5SDimitry Andric /// optional \p ExtecutionContext. If \c exe_ctx is non-null, the 1860b57cec5SDimitry Andric /// dynamic characteristics for that context are returned. 1870b57cec5SDimitry Andric virtual llvm::Optional<ArrayInfo> 1880b57cec5SDimitry Andric GetDynamicArrayInfoForUID(lldb::user_id_t type_uid, 1890b57cec5SDimitry Andric const lldb_private::ExecutionContext *exe_ctx) = 0; 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric virtual bool CompleteType(CompilerType &compiler_type) = 0; 1920b57cec5SDimitry Andric virtual void ParseDeclsForContext(CompilerDeclContext decl_ctx) {} 1930b57cec5SDimitry Andric virtual CompilerDecl GetDeclForUID(lldb::user_id_t uid) { 1940b57cec5SDimitry Andric return CompilerDecl(); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric virtual CompilerDeclContext GetDeclContextForUID(lldb::user_id_t uid) { 1970b57cec5SDimitry Andric return CompilerDeclContext(); 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric virtual CompilerDeclContext GetDeclContextContainingUID(lldb::user_id_t uid) { 2000b57cec5SDimitry Andric return CompilerDeclContext(); 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric virtual uint32_t ResolveSymbolContext(const Address &so_addr, 2030b57cec5SDimitry Andric lldb::SymbolContextItem resolve_scope, 2040b57cec5SDimitry Andric SymbolContext &sc) = 0; 2050b57cec5SDimitry Andric virtual uint32_t ResolveSymbolContext(const FileSpec &file_spec, 2060b57cec5SDimitry Andric uint32_t line, bool check_inlines, 2070b57cec5SDimitry Andric lldb::SymbolContextItem resolve_scope, 2080b57cec5SDimitry Andric SymbolContextList &sc_list); 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric virtual void DumpClangAST(Stream &s) {} 2119dba64beSDimitry Andric virtual void 2120b57cec5SDimitry Andric FindGlobalVariables(ConstString name, 2130b57cec5SDimitry Andric const CompilerDeclContext *parent_decl_ctx, 2140b57cec5SDimitry Andric uint32_t max_matches, VariableList &variables); 2159dba64beSDimitry Andric virtual void FindGlobalVariables(const RegularExpression ®ex, 2160b57cec5SDimitry Andric uint32_t max_matches, 2170b57cec5SDimitry Andric VariableList &variables); 2189dba64beSDimitry Andric virtual void FindFunctions(ConstString name, 2190b57cec5SDimitry Andric const CompilerDeclContext *parent_decl_ctx, 2200b57cec5SDimitry Andric lldb::FunctionNameType name_type_mask, 2219dba64beSDimitry Andric bool include_inlines, SymbolContextList &sc_list); 2229dba64beSDimitry Andric virtual void FindFunctions(const RegularExpression ®ex, 2239dba64beSDimitry Andric bool include_inlines, SymbolContextList &sc_list); 2249dba64beSDimitry Andric virtual void 2250b57cec5SDimitry Andric FindTypes(ConstString name, const CompilerDeclContext *parent_decl_ctx, 2269dba64beSDimitry Andric uint32_t max_matches, 2270b57cec5SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 2280b57cec5SDimitry Andric TypeMap &types); 2299dba64beSDimitry Andric 2309dba64beSDimitry Andric /// Find types specified by a CompilerContextPattern. 231*480093f4SDimitry Andric /// \param languages 232*480093f4SDimitry Andric /// Only return results in these languages. 233*480093f4SDimitry Andric /// \param searched_symbol_files 234*480093f4SDimitry Andric /// Prevents one file from being visited multiple times. 235*480093f4SDimitry Andric virtual void 236*480093f4SDimitry Andric FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 237*480093f4SDimitry Andric llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 238*480093f4SDimitry Andric TypeMap &types); 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric virtual void 2410b57cec5SDimitry Andric GetMangledNamesForFunction(const std::string &scope_qualified_name, 2420b57cec5SDimitry Andric std::vector<ConstString> &mangled_names); 2439dba64beSDimitry Andric 2449dba64beSDimitry Andric virtual void GetTypes(lldb_private::SymbolContextScope *sc_scope, 2450b57cec5SDimitry Andric lldb::TypeClass type_mask, 2460b57cec5SDimitry Andric lldb_private::TypeList &type_list) = 0; 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric virtual void PreloadSymbols(); 2490b57cec5SDimitry Andric 2509dba64beSDimitry Andric virtual llvm::Expected<lldb_private::TypeSystem &> 2510b57cec5SDimitry Andric GetTypeSystemForLanguage(lldb::LanguageType language); 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric virtual CompilerDeclContext 2540b57cec5SDimitry Andric FindNamespace(ConstString name, 2550b57cec5SDimitry Andric const CompilerDeclContext *parent_decl_ctx) { 2560b57cec5SDimitry Andric return CompilerDeclContext(); 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric 2599dba64beSDimitry Andric ObjectFile *GetObjectFile() { return m_objfile_sp.get(); } 2609dba64beSDimitry Andric const ObjectFile *GetObjectFile() const { return m_objfile_sp.get(); } 2619dba64beSDimitry Andric ObjectFile *GetMainObjectFile(); 2620b57cec5SDimitry Andric 263*480093f4SDimitry Andric virtual std::vector<std::unique_ptr<CallEdge>> 264*480093f4SDimitry Andric ParseCallEdgesInFunction(UserID func_id) { 2650b57cec5SDimitry Andric return {}; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric virtual void AddSymbols(Symtab &symtab) {} 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric /// Notify the SymbolFile that the file addresses in the Sections 2710b57cec5SDimitry Andric /// for this module have been changed. 2729dba64beSDimitry Andric virtual void SectionFileAddressesChanged(); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric struct RegisterInfoResolver { 2750b57cec5SDimitry Andric virtual ~RegisterInfoResolver(); // anchor 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric virtual const RegisterInfo *ResolveName(llvm::StringRef name) const = 0; 2780b57cec5SDimitry Andric virtual const RegisterInfo *ResolveNumber(lldb::RegisterKind kind, 2790b57cec5SDimitry Andric uint32_t number) const = 0; 2800b57cec5SDimitry Andric }; 2810b57cec5SDimitry Andric virtual lldb::UnwindPlanSP 2820b57cec5SDimitry Andric GetUnwindPlan(const Address &address, const RegisterInfoResolver &resolver) { 2830b57cec5SDimitry Andric return nullptr; 2840b57cec5SDimitry Andric } 2850b57cec5SDimitry Andric 2869dba64beSDimitry Andric /// Return the number of stack bytes taken up by the parameters to this 2879dba64beSDimitry Andric /// function. 2889dba64beSDimitry Andric virtual llvm::Expected<lldb::addr_t> GetParameterStackSize(Symbol &symbol) { 2899dba64beSDimitry Andric return llvm::createStringError(make_error_code(llvm::errc::not_supported), 2909dba64beSDimitry Andric "Operation not supported."); 2919dba64beSDimitry Andric } 2929dba64beSDimitry Andric 2939dba64beSDimitry Andric virtual void Dump(Stream &s); 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric protected: 2960b57cec5SDimitry Andric void AssertModuleLock(); 2979dba64beSDimitry Andric virtual uint32_t CalculateNumCompileUnits() = 0; 2989dba64beSDimitry Andric virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx) = 0; 2999dba64beSDimitry Andric virtual TypeList &GetTypeList() { return m_type_list; } 3000b57cec5SDimitry Andric 3019dba64beSDimitry Andric void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp); 3029dba64beSDimitry Andric 3039dba64beSDimitry Andric lldb::ObjectFileSP m_objfile_sp; // Keep a reference to the object file in 3049dba64beSDimitry Andric // case it isn't the same as the module 3059dba64beSDimitry Andric // object file (debug symbols in a separate 3069dba64beSDimitry Andric // file) 3079dba64beSDimitry Andric llvm::Optional<std::vector<lldb::CompUnitSP>> m_compile_units; 3089dba64beSDimitry Andric TypeList m_type_list; 3099dba64beSDimitry Andric Symtab *m_symtab = nullptr; 3100b57cec5SDimitry Andric uint32_t m_abilities; 3110b57cec5SDimitry Andric bool m_calculated_abilities; 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric private: 3140b57cec5SDimitry Andric DISALLOW_COPY_AND_ASSIGN(SymbolFile); 3150b57cec5SDimitry Andric }; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric } // namespace lldb_private 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric #endif // liblldb_SymbolFile_h_ 320