Index: lldb/trunk/include/lldb/Target/ObjCLanguageRuntime.h =================================================================== --- lldb/trunk/include/lldb/Target/ObjCLanguageRuntime.h +++ lldb/trunk/include/lldb/Target/ObjCLanguageRuntime.h @@ -1,429 +0,0 @@ -//===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// - -#ifndef liblldb_ObjCLanguageRuntime_h_ -#define liblldb_ObjCLanguageRuntime_h_ - -#include -#include -#include -#include - -#include "llvm/Support/Casting.h" - -#include "lldb/Breakpoint/BreakpointPrecondition.h" -#include "lldb/Core/PluginInterface.h" -#include "lldb/Core/ThreadSafeDenseMap.h" -#include "lldb/Symbol/CompilerType.h" -#include "lldb/Symbol/Type.h" -#include "lldb/Target/LanguageRuntime.h" -#include "lldb/lldb-private.h" - -class CommandObjectObjC_ClassTable_Dump; - -namespace lldb_private { - -class UtilityFunction; - -class ObjCLanguageRuntime : public LanguageRuntime { -public: - enum class ObjCRuntimeVersions { - eObjC_VersionUnknown = 0, - eAppleObjC_V1 = 1, - eAppleObjC_V2 = 2 - }; - - typedef lldb::addr_t ObjCISA; - - class ClassDescriptor; - typedef std::shared_ptr ClassDescriptorSP; - - // the information that we want to support retrieving from an ObjC class this - // needs to be pure virtual since there are at least 2 different - // implementations of the runtime, and more might come - class ClassDescriptor { - public: - ClassDescriptor() - : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate), - m_type_wp() {} - - virtual ~ClassDescriptor() = default; - - virtual ConstString GetClassName() = 0; - - virtual ClassDescriptorSP GetSuperclass() = 0; - - virtual ClassDescriptorSP GetMetaclass() const = 0; - - // virtual if any implementation has some other version-specific rules but - // for the known v1/v2 this is all that needs to be done - virtual bool IsKVO() { - if (m_is_kvo == eLazyBoolCalculate) { - const char *class_name = GetClassName().AsCString(); - if (class_name && *class_name) - m_is_kvo = - (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name); - } - return (m_is_kvo == eLazyBoolYes); - } - - // virtual if any implementation has some other version-specific rules but - // for the known v1/v2 this is all that needs to be done - virtual bool IsCFType() { - if (m_is_cf == eLazyBoolCalculate) { - const char *class_name = GetClassName().AsCString(); - if (class_name && *class_name) - m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 || - strcmp(class_name, "NSCFType") == 0); - } - return (m_is_cf == eLazyBoolYes); - } - - virtual bool IsValid() = 0; - - virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr, - uint64_t *value_bits = nullptr, - uint64_t *payload = nullptr) = 0; - - virtual uint64_t GetInstanceSize() = 0; - - // use to implement version-specific additional constraints on pointers - virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const { - return true; - } - - virtual ObjCISA GetISA() = 0; - - // This should return true iff the interface could be completed - virtual bool - Describe(std::function const &superclass_func, - std::function const - &instance_method_func, - std::function const - &class_method_func, - std::function const &ivar_func) const { - return false; - } - - lldb::TypeSP GetType() { return m_type_wp.lock(); } - - void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; } - - struct iVarDescriptor { - ConstString m_name; - CompilerType m_type; - uint64_t m_size; - int32_t m_offset; - }; - - virtual size_t GetNumIVars() { return 0; } - - virtual iVarDescriptor GetIVarAtIndex(size_t idx) { - return iVarDescriptor(); - } - - protected: - bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size, - bool allow_NULLs = false, bool allow_tagged = false, - bool check_version_specific = false) const; - - private: - LazyBool m_is_kvo; - LazyBool m_is_cf; - lldb::TypeWP m_type_wp; - }; - - class EncodingToType { - public: - virtual ~EncodingToType(); - - virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name, - bool for_expression); - virtual CompilerType RealizeType(const char *name, bool for_expression); - - virtual CompilerType RealizeType(clang::ASTContext &ast_ctx, - const char *name, bool for_expression) = 0; - - protected: - std::unique_ptr m_scratch_ast_ctx_up; - }; - - class ObjCExceptionPrecondition : public BreakpointPrecondition { - public: - ObjCExceptionPrecondition(); - - ~ObjCExceptionPrecondition() override = default; - - bool EvaluatePrecondition(StoppointCallbackContext &context) override; - void GetDescription(Stream &stream, lldb::DescriptionLevel level) override; - Status ConfigurePrecondition(Args &args) override; - - protected: - void AddClassName(const char *class_name); - - private: - std::unordered_set m_class_names; - }; - - static lldb::BreakpointPreconditionSP - GetBreakpointExceptionPrecondition(lldb::LanguageType language, - bool throw_bp); - - class TaggedPointerVendor { - public: - virtual ~TaggedPointerVendor() = default; - - virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0; - - virtual ObjCLanguageRuntime::ClassDescriptorSP - GetClassDescriptor(lldb::addr_t ptr) = 0; - - protected: - TaggedPointerVendor() = default; - - private: - DISALLOW_COPY_AND_ASSIGN(TaggedPointerVendor); - }; - - ~ObjCLanguageRuntime() override; - - static char ID; - - bool isA(const void *ClassID) const override { - return ClassID == &ID || LanguageRuntime::isA(ClassID); - } - - static bool classof(const LanguageRuntime *runtime) { - return runtime->isA(&ID); - } - - static ObjCLanguageRuntime *Get(Process &process) { - return llvm::cast_or_null( - process.GetLanguageRuntime(lldb::eLanguageTypeObjC)); - } - - virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; } - - typedef std::shared_ptr EncodingToTypeSP; - - virtual EncodingToTypeSP GetEncodingToType(); - - virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value); - - ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value); - - virtual ClassDescriptorSP - GetClassDescriptorFromClassName(ConstString class_name); - - virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa); - - ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa); - - lldb::LanguageType GetLanguageType() const override { - return lldb::eLanguageTypeObjC; - } - - virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0; - - virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0; - - virtual bool HasReadObjCLibrary() = 0; - - lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel); - - void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel, - lldb::addr_t impl_addr); - - TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr); - - void AddToClassNameCache(lldb::addr_t class_addr, const char *name, - lldb::TypeSP type_sp); - - void AddToClassNameCache(lldb::addr_t class_addr, - const TypeAndOrName &class_or_type_name); - - lldb::TypeSP LookupInCompleteClassCache(ConstString &name); - - llvm::Optional GetRuntimeType(CompilerType base_type) override; - - virtual UtilityFunction *CreateObjectChecker(const char *) = 0; - - virtual ObjCRuntimeVersions GetRuntimeVersion() const { - return ObjCRuntimeVersions::eObjC_VersionUnknown; - } - - bool IsValidISA(ObjCISA isa) { - UpdateISAToDescriptorMap(); - return m_isa_to_descriptor.count(isa) > 0; - } - - virtual void UpdateISAToDescriptorMapIfNeeded() = 0; - - void UpdateISAToDescriptorMap() { - if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) { - UpdateISAToDescriptorMapIfNeeded(); - } - } - - virtual ObjCISA GetISA(ConstString name); - - virtual ConstString GetActualTypeName(ObjCISA isa); - - virtual ObjCISA GetParentClass(ObjCISA isa); - - // Finds the byte offset of the child_type ivar in parent_type. If it can't - // find the offset, returns LLDB_INVALID_IVAR_OFFSET. - - virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type, - const char *ivar_name); - - bool HasNewLiteralsAndIndexing() { - if (m_has_new_literals_and_indexing == eLazyBoolCalculate) { - if (CalculateHasNewLiteralsAndIndexing()) - m_has_new_literals_and_indexing = eLazyBoolYes; - else - m_has_new_literals_and_indexing = eLazyBoolNo; - } - - return (m_has_new_literals_and_indexing == eLazyBoolYes); - } - - void SymbolsDidLoad(const ModuleList &module_list) override { - m_negative_complete_class_cache.clear(); - } - - bool GetTypeBitSize(const CompilerType &compiler_type, - uint64_t &size) override; - - /// Check whether the name is "self" or "_cmd" and should show up in - /// "frame variable". - bool IsWhitelistedRuntimeValue(ConstString name) override; - -protected: - // Classes that inherit from ObjCLanguageRuntime can see and modify these - ObjCLanguageRuntime(Process *process); - - virtual bool CalculateHasNewLiteralsAndIndexing() { return false; } - - bool ISAIsCached(ObjCISA isa) const { - return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end(); - } - - bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) { - if (isa != 0) { - m_isa_to_descriptor[isa] = descriptor_sp; - return true; - } - return false; - } - - bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp, - const char *class_name); - - bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp, - uint32_t class_name_hash) { - if (isa != 0) { - m_isa_to_descriptor[isa] = descriptor_sp; - m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa)); - return true; - } - return false; - } - -private: - // We keep a map of ->Implementation so we don't have to call - // the resolver function over and over. - - // FIXME: We need to watch for the loading of Protocols, and flush the cache - // for any - // class that we see so changed. - - struct ClassAndSel { - ClassAndSel() { - sel_addr = LLDB_INVALID_ADDRESS; - class_addr = LLDB_INVALID_ADDRESS; - } - - ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr) - : class_addr(in_class_addr), sel_addr(in_sel_addr) {} - - bool operator==(const ClassAndSel &rhs) { - if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr) - return true; - else - return false; - } - - bool operator<(const ClassAndSel &rhs) const { - if (class_addr < rhs.class_addr) - return true; - else if (class_addr > rhs.class_addr) - return false; - else { - if (sel_addr < rhs.sel_addr) - return true; - else - return false; - } - } - - lldb::addr_t class_addr; - lldb::addr_t sel_addr; - }; - - typedef std::map MsgImplMap; - typedef std::map ISAToDescriptorMap; - typedef std::multimap HashToISAMap; - typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator; - typedef HashToISAMap::iterator HashToISAIterator; - typedef ThreadSafeDenseMap TypeSizeCache; - - MsgImplMap m_impl_cache; - LazyBool m_has_new_literals_and_indexing; - ISAToDescriptorMap m_isa_to_descriptor; - HashToISAMap m_hash_to_isa_map; - TypeSizeCache m_type_size_cache; - -protected: - uint32_t m_isa_to_descriptor_stop_id; - - typedef std::map CompleteClassMap; - CompleteClassMap m_complete_class_cache; - - struct ConstStringSetHelpers { - size_t operator()(ConstString arg) const // for hashing - { - return (size_t)arg.GetCString(); - } - bool operator()(ConstString arg1, - ConstString arg2) const // for equality - { - return arg1.operator==(arg2); - } - }; - typedef std::unordered_set - CompleteClassSet; - CompleteClassSet m_negative_complete_class_cache; - - ISAToDescriptorIterator GetDescriptorIterator(ConstString name); - - friend class ::CommandObjectObjC_ClassTable_Dump; - - std::pair - GetDescriptorIteratorPair(bool update_if_needed = true); - - void ReadObjCLibraryIfNeeded(const ModuleList &module_list); - - DISALLOW_COPY_AND_ASSIGN(ObjCLanguageRuntime); -}; - -} // namespace lldb_private - -#endif // liblldb_ObjCLanguageRuntime_h_ Index: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp =================================================================== --- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp +++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp @@ -20,7 +20,6 @@ #include "lldb/Symbol/Function.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/ABI.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" @@ -32,6 +31,8 @@ #include "lldb/Utility/Log.h" #include "lldb/Utility/State.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN #ifdef ENABLE_DEBUG_PRINTF #include Index: lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp =================================================================== --- lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp +++ lldb/trunk/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp @@ -16,7 +16,6 @@ #include "lldb/Symbol/Function.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/ABI.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" @@ -30,6 +29,8 @@ #include "DynamicLoaderDarwin.h" #include "DynamicLoaderMacOSXDYLD.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + //#define ENABLE_DEBUG_PRINTF // COMMENT THIS LINE OUT PRIOR TO CHECKIN #ifdef ENABLE_DEBUG_PRINTF #include Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp @@ -20,12 +20,13 @@ #include "lldb/Symbol/SymbolFile.h" #include "lldb/Symbol/SymbolVendor.h" #include "lldb/Symbol/TaggedASTType.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Target.h" #include "lldb/Utility/Log.h" #include "clang/AST/ASTContext.h" #include "clang/AST/RecordLayout.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include #include Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp @@ -33,7 +33,6 @@ #include "lldb/Symbol/Variable.h" #include "lldb/Symbol/VariableList.h" #include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/StackFrame.h" @@ -53,6 +52,7 @@ #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" #include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionParser.cpp @@ -78,7 +78,6 @@ #include "lldb/Symbol/SymbolVendor.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Target/ThreadPlanCallFunction.h" @@ -90,6 +89,8 @@ #include "lldb/Utility/StreamString.h" #include "lldb/Utility/StringList.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include #include Index: lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp =================================================================== --- lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp +++ lldb/trunk/source/Plugins/ExpressionParser/Clang/IRDynamicChecks.cpp @@ -18,13 +18,14 @@ #include "lldb/Expression/UtilityFunction.h" #include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/Log.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + using namespace llvm; using namespace lldb_private; Index: lldb/trunk/source/Plugins/Language/ObjC/CF.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/CF.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/CF.cpp @@ -14,7 +14,6 @@ #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" @@ -22,6 +21,8 @@ #include "lldb/Utility/Status.h" #include "lldb/Utility/Stream.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + using namespace lldb; using namespace lldb_private; using namespace lldb_private::formatters; Index: lldb/trunk/source/Plugins/Language/ObjC/Cocoa.h =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/Cocoa.h +++ lldb/trunk/source/Plugins/Language/ObjC/Cocoa.h @@ -13,9 +13,10 @@ #include "lldb/Core/ValueObject.h" #include "lldb/DataFormatters/TypeSummary.h" #include "lldb/DataFormatters/TypeSynthetic.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Utility/Stream.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { namespace formatters { bool NSIndexSetSummaryProvider(ValueObject &valobj, Stream &stream, Index: lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/Cocoa.cpp @@ -17,7 +17,6 @@ #include "lldb/Host/Time.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/ProcessStructReader.h" #include "lldb/Target/Target.h" Index: lldb/trunk/source/Plugins/Language/ObjC/NSArray.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSArray.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSArray.cpp @@ -11,13 +11,13 @@ #include "Cocoa.h" #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h" + #include "lldb/Core/ValueObject.h" #include "lldb/Core/ValueObjectConstResult.h" #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Expression/FunctionCaller.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/Endian.h" Index: lldb/trunk/source/Plugins/Language/ObjC/NSDictionary.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSDictionary.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSDictionary.cpp @@ -19,7 +19,6 @@ #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/StackFrame.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" Index: lldb/trunk/source/Plugins/Language/ObjC/NSError.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSError.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSError.cpp @@ -14,7 +14,6 @@ #include "lldb/Core/ValueObjectConstResult.h" #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/ProcessStructReader.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" @@ -23,6 +22,7 @@ #include "lldb/Utility/Stream.h" #include "Plugins/Language/ObjC/NSString.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Plugins/Language/ObjC/NSException.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSException.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSException.cpp @@ -14,7 +14,6 @@ #include "lldb/Core/ValueObjectConstResult.h" #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/ProcessStructReader.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" @@ -23,6 +22,7 @@ #include "lldb/Utility/Stream.h" #include "Plugins/Language/ObjC/NSString.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" using namespace lldb; using namespace lldb_private; Index: lldb/trunk/source/Plugins/Language/ObjC/NSIndexPath.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSIndexPath.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSIndexPath.cpp @@ -13,10 +13,10 @@ #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/DataFormatters/TypeSynthetic.h" #include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" using namespace lldb; using namespace lldb_private; using namespace lldb_private::formatters; Index: lldb/trunk/source/Plugins/Language/ObjC/NSSet.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSSet.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/NSSet.cpp @@ -14,7 +14,6 @@ #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/Endian.h" Index: lldb/trunk/source/Plugins/Language/ObjC/NSString.h =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/NSString.h +++ lldb/trunk/source/Plugins/Language/ObjC/NSString.h @@ -12,9 +12,10 @@ #include "lldb/Core/ValueObject.h" #include "lldb/DataFormatters/TypeSummary.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Utility/Stream.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { namespace formatters { bool NSStringSummaryProvider(ValueObject &valobj, Stream &stream, Index: lldb/trunk/source/Plugins/Language/ObjC/ObjCLanguage.cpp =================================================================== --- lldb/trunk/source/Plugins/Language/ObjC/ObjCLanguage.cpp +++ lldb/trunk/source/Plugins/Language/ObjC/ObjCLanguage.cpp @@ -16,13 +16,14 @@ #include "lldb/DataFormatters/FormattersHelpers.h" #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/CompilerType.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Target.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/StreamString.h" #include "llvm/Support/Threading.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include "CF.h" #include "Cocoa.h" #include "CoreMedia.h" Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCClassDescriptorV2.h @@ -12,9 +12,10 @@ #include #include "AppleObjCRuntimeV2.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { class ClassDescriptorV2 : public ObjCLanguageRuntime::ClassDescriptor { Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.h @@ -11,9 +11,10 @@ #include "lldb/Symbol/ClangASTContext.h" #include "lldb/Symbol/DeclVendor.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { class AppleObjCExternalASTSource; Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp @@ -9,10 +9,10 @@ #include "AppleObjCDeclVendor.h" #include "Plugins/ExpressionParser/Clang/ASTDumper.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" #include "lldb/Core/Module.h" #include "lldb/Symbol/ClangExternalASTSourceCommon.h" #include "lldb/Symbol/ClangUtil.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/Log.h" @@ -20,6 +20,7 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/DeclObjC.h" + using namespace lldb_private; class lldb_private::AppleObjCExternalASTSource Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.h @@ -14,9 +14,10 @@ #include "AppleObjCTrampolineHandler.h" #include "AppleThreadPlanStepThroughObjCTrampoline.h" #include "lldb/Target/LanguageRuntime.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { class AppleObjCRuntime : public lldb_private::ObjCLanguageRuntime { Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h @@ -10,9 +10,10 @@ #define liblldb_AppleObjCRuntimeV1_h_ #include "AppleObjCRuntime.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_private { class AppleObjCRuntimeV1 : public AppleObjCRuntime { Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h @@ -14,9 +14,10 @@ #include #include "AppleObjCRuntime.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + class RemoteNXMapTable; namespace lldb_private { Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp @@ -42,7 +42,6 @@ #include "lldb/Symbol/VariableList.h" #include "lldb/Target/ABI.h" #include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Platform.h" #include "lldb/Target/Process.h" #include "lldb/Target/RegisterContext.h" @@ -66,6 +65,8 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/DeclObjC.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include using namespace lldb; Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp @@ -23,7 +23,6 @@ #include "lldb/Symbol/Symbol.h" #include "lldb/Target/ABI.h" #include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/RegisterContext.h" #include "lldb/Target/Target.h" @@ -35,6 +34,8 @@ #include "llvm/ADT/STLExtras.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include using namespace lldb; Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTypeEncodingParser.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTypeEncodingParser.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTypeEncodingParser.h @@ -11,9 +11,10 @@ #include "clang/AST/ASTContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/lldb-private.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + namespace lldb_utility { class StringLexer; } Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleThreadPlanStepThroughObjCTrampoline.cpp @@ -13,13 +13,14 @@ #include "lldb/Expression/FunctionCaller.h" #include "lldb/Expression/UtilityFunction.h" #include "lldb/Target/ExecutionContext.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/Thread.h" #include "lldb/Target/ThreadPlanRunToAddress.h" #include "lldb/Target/ThreadPlanStepOut.h" #include "lldb/Utility/Log.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" + #include using namespace lldb; Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/CMakeLists.txt =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/CMakeLists.txt +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/CMakeLists.txt @@ -1 +1,10 @@ +add_lldb_library(lldbPluginObjCRuntime PLUGIN + ObjCLanguageRuntime.cpp + + LINK_LIBS + lldbCore + lldbSymbol + lldbTarget + lldbUtility +) add_subdirectory(AppleObjCRuntime) Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h @@ -0,0 +1,429 @@ +//===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef liblldb_ObjCLanguageRuntime_h_ +#define liblldb_ObjCLanguageRuntime_h_ + +#include +#include +#include +#include + +#include "llvm/Support/Casting.h" + +#include "lldb/Breakpoint/BreakpointPrecondition.h" +#include "lldb/Core/PluginInterface.h" +#include "lldb/Core/ThreadSafeDenseMap.h" +#include "lldb/Symbol/CompilerType.h" +#include "lldb/Symbol/Type.h" +#include "lldb/Target/LanguageRuntime.h" +#include "lldb/lldb-private.h" + +class CommandObjectObjC_ClassTable_Dump; + +namespace lldb_private { + +class UtilityFunction; + +class ObjCLanguageRuntime : public LanguageRuntime { +public: + enum class ObjCRuntimeVersions { + eObjC_VersionUnknown = 0, + eAppleObjC_V1 = 1, + eAppleObjC_V2 = 2 + }; + + typedef lldb::addr_t ObjCISA; + + class ClassDescriptor; + typedef std::shared_ptr ClassDescriptorSP; + + // the information that we want to support retrieving from an ObjC class this + // needs to be pure virtual since there are at least 2 different + // implementations of the runtime, and more might come + class ClassDescriptor { + public: + ClassDescriptor() + : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate), + m_type_wp() {} + + virtual ~ClassDescriptor() = default; + + virtual ConstString GetClassName() = 0; + + virtual ClassDescriptorSP GetSuperclass() = 0; + + virtual ClassDescriptorSP GetMetaclass() const = 0; + + // virtual if any implementation has some other version-specific rules but + // for the known v1/v2 this is all that needs to be done + virtual bool IsKVO() { + if (m_is_kvo == eLazyBoolCalculate) { + const char *class_name = GetClassName().AsCString(); + if (class_name && *class_name) + m_is_kvo = + (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name); + } + return (m_is_kvo == eLazyBoolYes); + } + + // virtual if any implementation has some other version-specific rules but + // for the known v1/v2 this is all that needs to be done + virtual bool IsCFType() { + if (m_is_cf == eLazyBoolCalculate) { + const char *class_name = GetClassName().AsCString(); + if (class_name && *class_name) + m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 || + strcmp(class_name, "NSCFType") == 0); + } + return (m_is_cf == eLazyBoolYes); + } + + virtual bool IsValid() = 0; + + virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr, + uint64_t *value_bits = nullptr, + uint64_t *payload = nullptr) = 0; + + virtual uint64_t GetInstanceSize() = 0; + + // use to implement version-specific additional constraints on pointers + virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const { + return true; + } + + virtual ObjCISA GetISA() = 0; + + // This should return true iff the interface could be completed + virtual bool + Describe(std::function const &superclass_func, + std::function const + &instance_method_func, + std::function const + &class_method_func, + std::function const &ivar_func) const { + return false; + } + + lldb::TypeSP GetType() { return m_type_wp.lock(); } + + void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; } + + struct iVarDescriptor { + ConstString m_name; + CompilerType m_type; + uint64_t m_size; + int32_t m_offset; + }; + + virtual size_t GetNumIVars() { return 0; } + + virtual iVarDescriptor GetIVarAtIndex(size_t idx) { + return iVarDescriptor(); + } + + protected: + bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size, + bool allow_NULLs = false, bool allow_tagged = false, + bool check_version_specific = false) const; + + private: + LazyBool m_is_kvo; + LazyBool m_is_cf; + lldb::TypeWP m_type_wp; + }; + + class EncodingToType { + public: + virtual ~EncodingToType(); + + virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name, + bool for_expression); + virtual CompilerType RealizeType(const char *name, bool for_expression); + + virtual CompilerType RealizeType(clang::ASTContext &ast_ctx, + const char *name, bool for_expression) = 0; + + protected: + std::unique_ptr m_scratch_ast_ctx_up; + }; + + class ObjCExceptionPrecondition : public BreakpointPrecondition { + public: + ObjCExceptionPrecondition(); + + ~ObjCExceptionPrecondition() override = default; + + bool EvaluatePrecondition(StoppointCallbackContext &context) override; + void GetDescription(Stream &stream, lldb::DescriptionLevel level) override; + Status ConfigurePrecondition(Args &args) override; + + protected: + void AddClassName(const char *class_name); + + private: + std::unordered_set m_class_names; + }; + + static lldb::BreakpointPreconditionSP + GetBreakpointExceptionPrecondition(lldb::LanguageType language, + bool throw_bp); + + class TaggedPointerVendor { + public: + virtual ~TaggedPointerVendor() = default; + + virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0; + + virtual ObjCLanguageRuntime::ClassDescriptorSP + GetClassDescriptor(lldb::addr_t ptr) = 0; + + protected: + TaggedPointerVendor() = default; + + private: + DISALLOW_COPY_AND_ASSIGN(TaggedPointerVendor); + }; + + ~ObjCLanguageRuntime() override; + + static char ID; + + bool isA(const void *ClassID) const override { + return ClassID == &ID || LanguageRuntime::isA(ClassID); + } + + static bool classof(const LanguageRuntime *runtime) { + return runtime->isA(&ID); + } + + static ObjCLanguageRuntime *Get(Process &process) { + return llvm::cast_or_null( + process.GetLanguageRuntime(lldb::eLanguageTypeObjC)); + } + + virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; } + + typedef std::shared_ptr EncodingToTypeSP; + + virtual EncodingToTypeSP GetEncodingToType(); + + virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value); + + ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value); + + virtual ClassDescriptorSP + GetClassDescriptorFromClassName(ConstString class_name); + + virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa); + + ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa); + + lldb::LanguageType GetLanguageType() const override { + return lldb::eLanguageTypeObjC; + } + + virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0; + + virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0; + + virtual bool HasReadObjCLibrary() = 0; + + lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel); + + void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel, + lldb::addr_t impl_addr); + + TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr); + + void AddToClassNameCache(lldb::addr_t class_addr, const char *name, + lldb::TypeSP type_sp); + + void AddToClassNameCache(lldb::addr_t class_addr, + const TypeAndOrName &class_or_type_name); + + lldb::TypeSP LookupInCompleteClassCache(ConstString &name); + + llvm::Optional GetRuntimeType(CompilerType base_type) override; + + virtual UtilityFunction *CreateObjectChecker(const char *) = 0; + + virtual ObjCRuntimeVersions GetRuntimeVersion() const { + return ObjCRuntimeVersions::eObjC_VersionUnknown; + } + + bool IsValidISA(ObjCISA isa) { + UpdateISAToDescriptorMap(); + return m_isa_to_descriptor.count(isa) > 0; + } + + virtual void UpdateISAToDescriptorMapIfNeeded() = 0; + + void UpdateISAToDescriptorMap() { + if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) { + UpdateISAToDescriptorMapIfNeeded(); + } + } + + virtual ObjCISA GetISA(ConstString name); + + virtual ConstString GetActualTypeName(ObjCISA isa); + + virtual ObjCISA GetParentClass(ObjCISA isa); + + // Finds the byte offset of the child_type ivar in parent_type. If it can't + // find the offset, returns LLDB_INVALID_IVAR_OFFSET. + + virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type, + const char *ivar_name); + + bool HasNewLiteralsAndIndexing() { + if (m_has_new_literals_and_indexing == eLazyBoolCalculate) { + if (CalculateHasNewLiteralsAndIndexing()) + m_has_new_literals_and_indexing = eLazyBoolYes; + else + m_has_new_literals_and_indexing = eLazyBoolNo; + } + + return (m_has_new_literals_and_indexing == eLazyBoolYes); + } + + void SymbolsDidLoad(const ModuleList &module_list) override { + m_negative_complete_class_cache.clear(); + } + + bool GetTypeBitSize(const CompilerType &compiler_type, + uint64_t &size) override; + + /// Check whether the name is "self" or "_cmd" and should show up in + /// "frame variable". + bool IsWhitelistedRuntimeValue(ConstString name) override; + +protected: + // Classes that inherit from ObjCLanguageRuntime can see and modify these + ObjCLanguageRuntime(Process *process); + + virtual bool CalculateHasNewLiteralsAndIndexing() { return false; } + + bool ISAIsCached(ObjCISA isa) const { + return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end(); + } + + bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) { + if (isa != 0) { + m_isa_to_descriptor[isa] = descriptor_sp; + return true; + } + return false; + } + + bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp, + const char *class_name); + + bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp, + uint32_t class_name_hash) { + if (isa != 0) { + m_isa_to_descriptor[isa] = descriptor_sp; + m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa)); + return true; + } + return false; + } + +private: + // We keep a map of ->Implementation so we don't have to call + // the resolver function over and over. + + // FIXME: We need to watch for the loading of Protocols, and flush the cache + // for any + // class that we see so changed. + + struct ClassAndSel { + ClassAndSel() { + sel_addr = LLDB_INVALID_ADDRESS; + class_addr = LLDB_INVALID_ADDRESS; + } + + ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr) + : class_addr(in_class_addr), sel_addr(in_sel_addr) {} + + bool operator==(const ClassAndSel &rhs) { + if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr) + return true; + else + return false; + } + + bool operator<(const ClassAndSel &rhs) const { + if (class_addr < rhs.class_addr) + return true; + else if (class_addr > rhs.class_addr) + return false; + else { + if (sel_addr < rhs.sel_addr) + return true; + else + return false; + } + } + + lldb::addr_t class_addr; + lldb::addr_t sel_addr; + }; + + typedef std::map MsgImplMap; + typedef std::map ISAToDescriptorMap; + typedef std::multimap HashToISAMap; + typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator; + typedef HashToISAMap::iterator HashToISAIterator; + typedef ThreadSafeDenseMap TypeSizeCache; + + MsgImplMap m_impl_cache; + LazyBool m_has_new_literals_and_indexing; + ISAToDescriptorMap m_isa_to_descriptor; + HashToISAMap m_hash_to_isa_map; + TypeSizeCache m_type_size_cache; + +protected: + uint32_t m_isa_to_descriptor_stop_id; + + typedef std::map CompleteClassMap; + CompleteClassMap m_complete_class_cache; + + struct ConstStringSetHelpers { + size_t operator()(ConstString arg) const // for hashing + { + return (size_t)arg.GetCString(); + } + bool operator()(ConstString arg1, + ConstString arg2) const // for equality + { + return arg1.operator==(arg2); + } + }; + typedef std::unordered_set + CompleteClassSet; + CompleteClassSet m_negative_complete_class_cache; + + ISAToDescriptorIterator GetDescriptorIterator(ConstString name); + + friend class ::CommandObjectObjC_ClassTable_Dump; + + std::pair + GetDescriptorIteratorPair(bool update_if_needed = true); + + void ReadObjCLibraryIfNeeded(const ModuleList &module_list); + + DISALLOW_COPY_AND_ASSIGN(ObjCLanguageRuntime); +}; + +} // namespace lldb_private + +#endif // liblldb_ObjCLanguageRuntime_h_ Index: lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.cpp =================================================================== --- lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.cpp +++ lldb/trunk/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.cpp @@ -0,0 +1,436 @@ +//===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#include "clang/AST/Type.h" + +#include "ObjCLanguageRuntime.h" + +#include "lldb/Core/MappedHash.h" +#include "lldb/Core/Module.h" +#include "lldb/Core/PluginManager.h" +#include "lldb/Core/ValueObject.h" +#include "lldb/Symbol/ClangASTContext.h" +#include "lldb/Symbol/SymbolContext.h" +#include "lldb/Symbol/SymbolFile.h" +#include "lldb/Symbol/Type.h" +#include "lldb/Symbol/TypeList.h" +#include "lldb/Symbol/Variable.h" +#include "lldb/Target/Target.h" +#include "lldb/Utility/Log.h" +#include "lldb/Utility/Timer.h" + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/DJB.h" + +using namespace lldb; +using namespace lldb_private; + +char ObjCLanguageRuntime::ID = 0; + +// Destructor +ObjCLanguageRuntime::~ObjCLanguageRuntime() {} + +ObjCLanguageRuntime::ObjCLanguageRuntime(Process *process) + : LanguageRuntime(process), m_impl_cache(), + m_has_new_literals_and_indexing(eLazyBoolCalculate), + m_isa_to_descriptor(), m_hash_to_isa_map(), m_type_size_cache(), + m_isa_to_descriptor_stop_id(UINT32_MAX), m_complete_class_cache(), + m_negative_complete_class_cache() {} + +bool ObjCLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) { + static ConstString g_self = ConstString("self"); + static ConstString g_cmd = ConstString("_cmd"); + return name == g_self || name == g_cmd; +} + +bool ObjCLanguageRuntime::AddClass(ObjCISA isa, + const ClassDescriptorSP &descriptor_sp, + const char *class_name) { + if (isa != 0) { + m_isa_to_descriptor[isa] = descriptor_sp; + // class_name is assumed to be valid + m_hash_to_isa_map.insert(std::make_pair(llvm::djbHash(class_name), isa)); + return true; + } + return false; +} + +void ObjCLanguageRuntime::AddToMethodCache(lldb::addr_t class_addr, + lldb::addr_t selector, + lldb::addr_t impl_addr) { + Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); + if (log) { + log->Printf("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 + " implementation 0x%" PRIx64 ".", + class_addr, selector, impl_addr); + } + m_impl_cache.insert(std::pair( + ClassAndSel(class_addr, selector), impl_addr)); +} + +lldb::addr_t ObjCLanguageRuntime::LookupInMethodCache(lldb::addr_t class_addr, + lldb::addr_t selector) { + MsgImplMap::iterator pos, end = m_impl_cache.end(); + pos = m_impl_cache.find(ClassAndSel(class_addr, selector)); + if (pos != end) + return (*pos).second; + return LLDB_INVALID_ADDRESS; +} + +lldb::TypeSP +ObjCLanguageRuntime::LookupInCompleteClassCache(ConstString &name) { + CompleteClassMap::iterator complete_class_iter = + m_complete_class_cache.find(name); + + if (complete_class_iter != m_complete_class_cache.end()) { + // Check the weak pointer to make sure the type hasn't been unloaded + TypeSP complete_type_sp(complete_class_iter->second.lock()); + + if (complete_type_sp) + return complete_type_sp; + else + m_complete_class_cache.erase(name); + } + + if (m_negative_complete_class_cache.count(name) > 0) + return TypeSP(); + + const ModuleList &modules = m_process->GetTarget().GetImages(); + + SymbolContextList sc_list; + const size_t matching_symbols = + modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list); + + if (matching_symbols) { + SymbolContext sc; + + sc_list.GetContextAtIndex(0, sc); + + ModuleSP module_sp(sc.module_sp); + + if (!module_sp) + return TypeSP(); + + const bool exact_match = true; + const uint32_t max_matches = UINT32_MAX; + TypeList types; + + llvm::DenseSet searched_symbol_files; + const uint32_t num_types = module_sp->FindTypes( + name, exact_match, max_matches, searched_symbol_files, types); + + if (num_types) { + uint32_t i; + for (i = 0; i < num_types; ++i) { + TypeSP type_sp(types.GetTypeAtIndex(i)); + + if (ClangASTContext::IsObjCObjectOrInterfaceType( + type_sp->GetForwardCompilerType())) { + if (type_sp->IsCompleteObjCClass()) { + m_complete_class_cache[name] = type_sp; + return type_sp; + } + } + } + } + } + m_negative_complete_class_cache.insert(name); + return TypeSP(); +} + +size_t ObjCLanguageRuntime::GetByteOffsetForIvar(CompilerType &parent_qual_type, + const char *ivar_name) { + return LLDB_INVALID_IVAR_OFFSET; +} + +bool ObjCLanguageRuntime::ClassDescriptor::IsPointerValid( + lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged, + bool check_version_specific) const { + if (!value) + return allow_NULLs; + if ((value % 2) == 1 && allow_tagged) + return true; + if ((value % ptr_size) == 0) + return (check_version_specific ? CheckPointer(value, ptr_size) : true); + else + return false; +} + +ObjCLanguageRuntime::ObjCISA +ObjCLanguageRuntime::GetISA(ConstString name) { + ISAToDescriptorIterator pos = GetDescriptorIterator(name); + if (pos != m_isa_to_descriptor.end()) + return pos->first; + return 0; +} + +ObjCLanguageRuntime::ISAToDescriptorIterator +ObjCLanguageRuntime::GetDescriptorIterator(ConstString name) { + ISAToDescriptorIterator end = m_isa_to_descriptor.end(); + + if (name) { + UpdateISAToDescriptorMap(); + if (m_hash_to_isa_map.empty()) { + // No name hashes were provided, we need to just linearly power through + // the names and find a match + for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); + pos != end; ++pos) { + if (pos->second->GetClassName() == name) + return pos; + } + } else { + // Name hashes were provided, so use them to efficiently lookup name to + // isa/descriptor + const uint32_t name_hash = llvm::djbHash(name.GetStringRef()); + std::pair range = + m_hash_to_isa_map.equal_range(name_hash); + for (HashToISAIterator range_pos = range.first; range_pos != range.second; + ++range_pos) { + ISAToDescriptorIterator pos = + m_isa_to_descriptor.find(range_pos->second); + if (pos != m_isa_to_descriptor.end()) { + if (pos->second->GetClassName() == name) + return pos; + } + } + } + } + return end; +} + +std::pair +ObjCLanguageRuntime::GetDescriptorIteratorPair(bool update_if_needed) { + if (update_if_needed) + UpdateISAToDescriptorMapIfNeeded(); + + return std::pair( + m_isa_to_descriptor.begin(), m_isa_to_descriptor.end()); +} + +ObjCLanguageRuntime::ObjCISA +ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) { + ClassDescriptorSP objc_class_sp(GetClassDescriptorFromISA(isa)); + if (objc_class_sp) { + ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass()); + if (objc_super_class_sp) + return objc_super_class_sp->GetISA(); + } + return 0; +} + +ConstString +ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) { + ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(isa)); + if (objc_class_sp) + return objc_class_sp->GetClassName(); + return ConstString(); +} + +ObjCLanguageRuntime::ClassDescriptorSP +ObjCLanguageRuntime::GetClassDescriptorFromClassName( + ConstString class_name) { + ISAToDescriptorIterator pos = GetDescriptorIterator(class_name); + if (pos != m_isa_to_descriptor.end()) + return pos->second; + return ClassDescriptorSP(); +} + +ObjCLanguageRuntime::ClassDescriptorSP +ObjCLanguageRuntime::GetClassDescriptor(ValueObject &valobj) { + ClassDescriptorSP objc_class_sp; + // if we get an invalid VO (which might still happen when playing around with + // pointers returned by the expression parser, don't consider this a valid + // ObjC object) + if (valobj.GetCompilerType().IsValid()) { + addr_t isa_pointer = valobj.GetPointerValue(); + if (isa_pointer != LLDB_INVALID_ADDRESS) { + ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); + + Process *process = exe_ctx.GetProcessPtr(); + if (process) { + Status error; + ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); + if (isa != LLDB_INVALID_ADDRESS) + objc_class_sp = GetClassDescriptorFromISA(isa); + } + } + } + return objc_class_sp; +} + +ObjCLanguageRuntime::ClassDescriptorSP +ObjCLanguageRuntime::GetNonKVOClassDescriptor(ValueObject &valobj) { + ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp( + GetClassDescriptor(valobj)); + if (objc_class_sp) { + if (!objc_class_sp->IsKVO()) + return objc_class_sp; + + ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); + if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) + return non_kvo_objc_class_sp; + } + return ClassDescriptorSP(); +} + +ObjCLanguageRuntime::ClassDescriptorSP +ObjCLanguageRuntime::GetClassDescriptorFromISA(ObjCISA isa) { + if (isa) { + UpdateISAToDescriptorMap(); + ObjCLanguageRuntime::ISAToDescriptorIterator pos = + m_isa_to_descriptor.find(isa); + if (pos != m_isa_to_descriptor.end()) + return pos->second; + } + return ClassDescriptorSP(); +} + +ObjCLanguageRuntime::ClassDescriptorSP +ObjCLanguageRuntime::GetNonKVOClassDescriptor(ObjCISA isa) { + if (isa) { + ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa); + if (objc_class_sp && objc_class_sp->IsValid()) { + if (!objc_class_sp->IsKVO()) + return objc_class_sp; + + ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); + if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) + return non_kvo_objc_class_sp; + } + } + return ClassDescriptorSP(); +} + +CompilerType +ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name, + bool for_expression) { + if (m_scratch_ast_ctx_up) + return RealizeType(*m_scratch_ast_ctx_up, name, for_expression); + return CompilerType(); +} + +CompilerType ObjCLanguageRuntime::EncodingToType::RealizeType( + ClangASTContext &ast_ctx, const char *name, bool for_expression) { + clang::ASTContext *clang_ast = ast_ctx.getASTContext(); + if (!clang_ast) + return CompilerType(); + return RealizeType(*clang_ast, name, for_expression); +} + +ObjCLanguageRuntime::EncodingToType::~EncodingToType() {} + +ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() { + return nullptr; +} + +bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type, + uint64_t &size) { + void *opaque_ptr = compiler_type.GetOpaqueQualType(); + size = m_type_size_cache.Lookup(opaque_ptr); + // an ObjC object will at least have an ISA, so 0 is definitely not OK + if (size > 0) + return true; + + ClassDescriptorSP class_descriptor_sp = + GetClassDescriptorFromClassName(compiler_type.GetTypeName()); + if (!class_descriptor_sp) + return false; + + int32_t max_offset = INT32_MIN; + uint64_t sizeof_max = 0; + bool found = false; + + for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) { + const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx); + int32_t cur_offset = ivar.m_offset; + if (cur_offset > max_offset) { + max_offset = cur_offset; + sizeof_max = ivar.m_size; + found = true; + } + } + + size = 8 * (max_offset + sizeof_max); + if (found) + m_type_size_cache.Insert(opaque_ptr, size); + + return found; +} + +lldb::BreakpointPreconditionSP +ObjCLanguageRuntime::GetBreakpointExceptionPrecondition(LanguageType language, + bool throw_bp) { + if (language != eLanguageTypeObjC) + return lldb::BreakpointPreconditionSP(); + if (!throw_bp) + return lldb::BreakpointPreconditionSP(); + BreakpointPreconditionSP precondition_sp( + new ObjCLanguageRuntime::ObjCExceptionPrecondition()); + return precondition_sp; +} + +// Exception breakpoint Precondition class for ObjC: +void ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName( + const char *class_name) { + m_class_names.insert(class_name); +} + +ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() {} + +bool ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition( + StoppointCallbackContext &context) { + return true; +} + +void ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription( + Stream &stream, lldb::DescriptionLevel level) {} + +Status ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition( + Args &args) { + Status error; + if (args.GetArgumentCount() > 0) + error.SetErrorString( + "The ObjC Exception breakpoint doesn't support extra options."); + return error; +} + +llvm::Optional +ObjCLanguageRuntime::GetRuntimeType(CompilerType base_type) { + CompilerType class_type; + bool is_pointer_type = false; + + if (ClangASTContext::IsObjCObjectPointerType(base_type, &class_type)) + is_pointer_type = true; + else if (ClangASTContext::IsObjCObjectOrInterfaceType(base_type)) + class_type = base_type; + else + return llvm::None; + + if (!class_type) + return llvm::None; + + ConstString class_name(class_type.GetConstTypeName()); + if (!class_name) + return llvm::None; + + TypeSP complete_objc_class_type_sp = LookupInCompleteClassCache(class_name); + if (!complete_objc_class_type_sp) + return llvm::None; + + CompilerType complete_class( + complete_objc_class_type_sp->GetFullCompilerType()); + if (complete_class.GetCompleteType()) { + if (is_pointer_type) + return complete_class.GetPointerType(); + else + return complete_class; + } + + return llvm::None; +} Index: lldb/trunk/source/Symbol/CMakeLists.txt =================================================================== --- lldb/trunk/source/Symbol/CMakeLists.txt +++ lldb/trunk/source/Symbol/CMakeLists.txt @@ -59,6 +59,7 @@ lldbPluginSymbolFileDWARF lldbPluginSymbolFilePDB lldbPluginObjCLanguage + lldbPluginObjCRuntime LINK_COMPONENTS Support Index: lldb/trunk/source/Symbol/ClangASTContext.cpp =================================================================== --- lldb/trunk/source/Symbol/ClangASTContext.cpp +++ lldb/trunk/source/Symbol/ClangASTContext.cpp @@ -86,7 +86,6 @@ #include "lldb/Symbol/VerifyDecl.h" #include "lldb/Target/ExecutionContext.h" #include "lldb/Target/Language.h" -#include "lldb/Target/ObjCLanguageRuntime.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" #include "lldb/Utility/DataExtractor.h" @@ -95,6 +94,7 @@ #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Scalar.h" +#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" #include "Plugins/SymbolFile/PDB/PDBASTParser.h" Index: lldb/trunk/source/Target/CMakeLists.txt =================================================================== --- lldb/trunk/source/Target/CMakeLists.txt +++ lldb/trunk/source/Target/CMakeLists.txt @@ -10,7 +10,6 @@ Memory.cpp MemoryHistory.cpp ModuleCache.cpp - ObjCLanguageRuntime.cpp OperatingSystem.cpp PathMappingList.cpp Platform.cpp Index: lldb/trunk/source/Target/ObjCLanguageRuntime.cpp =================================================================== --- lldb/trunk/source/Target/ObjCLanguageRuntime.cpp +++ lldb/trunk/source/Target/ObjCLanguageRuntime.cpp @@ -1,435 +0,0 @@ -//===-- ObjCLanguageRuntime.cpp ---------------------------------*- C++ -*-===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -#include "clang/AST/Type.h" - -#include "lldb/Core/MappedHash.h" -#include "lldb/Core/Module.h" -#include "lldb/Core/PluginManager.h" -#include "lldb/Core/ValueObject.h" -#include "lldb/Symbol/ClangASTContext.h" -#include "lldb/Symbol/SymbolContext.h" -#include "lldb/Symbol/SymbolFile.h" -#include "lldb/Symbol/Type.h" -#include "lldb/Symbol/TypeList.h" -#include "lldb/Symbol/Variable.h" -#include "lldb/Target/ObjCLanguageRuntime.h" -#include "lldb/Target/Target.h" -#include "lldb/Utility/Log.h" -#include "lldb/Utility/Timer.h" - -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/DJB.h" - -using namespace lldb; -using namespace lldb_private; - -char ObjCLanguageRuntime::ID = 0; - -// Destructor -ObjCLanguageRuntime::~ObjCLanguageRuntime() {} - -ObjCLanguageRuntime::ObjCLanguageRuntime(Process *process) - : LanguageRuntime(process), m_impl_cache(), - m_has_new_literals_and_indexing(eLazyBoolCalculate), - m_isa_to_descriptor(), m_hash_to_isa_map(), m_type_size_cache(), - m_isa_to_descriptor_stop_id(UINT32_MAX), m_complete_class_cache(), - m_negative_complete_class_cache() {} - -bool ObjCLanguageRuntime::IsWhitelistedRuntimeValue(ConstString name) { - static ConstString g_self = ConstString("self"); - static ConstString g_cmd = ConstString("_cmd"); - return name == g_self || name == g_cmd; -} - -bool ObjCLanguageRuntime::AddClass(ObjCISA isa, - const ClassDescriptorSP &descriptor_sp, - const char *class_name) { - if (isa != 0) { - m_isa_to_descriptor[isa] = descriptor_sp; - // class_name is assumed to be valid - m_hash_to_isa_map.insert(std::make_pair(llvm::djbHash(class_name), isa)); - return true; - } - return false; -} - -void ObjCLanguageRuntime::AddToMethodCache(lldb::addr_t class_addr, - lldb::addr_t selector, - lldb::addr_t impl_addr) { - Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); - if (log) { - log->Printf("Caching: class 0x%" PRIx64 " selector 0x%" PRIx64 - " implementation 0x%" PRIx64 ".", - class_addr, selector, impl_addr); - } - m_impl_cache.insert(std::pair( - ClassAndSel(class_addr, selector), impl_addr)); -} - -lldb::addr_t ObjCLanguageRuntime::LookupInMethodCache(lldb::addr_t class_addr, - lldb::addr_t selector) { - MsgImplMap::iterator pos, end = m_impl_cache.end(); - pos = m_impl_cache.find(ClassAndSel(class_addr, selector)); - if (pos != end) - return (*pos).second; - return LLDB_INVALID_ADDRESS; -} - -lldb::TypeSP -ObjCLanguageRuntime::LookupInCompleteClassCache(ConstString &name) { - CompleteClassMap::iterator complete_class_iter = - m_complete_class_cache.find(name); - - if (complete_class_iter != m_complete_class_cache.end()) { - // Check the weak pointer to make sure the type hasn't been unloaded - TypeSP complete_type_sp(complete_class_iter->second.lock()); - - if (complete_type_sp) - return complete_type_sp; - else - m_complete_class_cache.erase(name); - } - - if (m_negative_complete_class_cache.count(name) > 0) - return TypeSP(); - - const ModuleList &modules = m_process->GetTarget().GetImages(); - - SymbolContextList sc_list; - const size_t matching_symbols = - modules.FindSymbolsWithNameAndType(name, eSymbolTypeObjCClass, sc_list); - - if (matching_symbols) { - SymbolContext sc; - - sc_list.GetContextAtIndex(0, sc); - - ModuleSP module_sp(sc.module_sp); - - if (!module_sp) - return TypeSP(); - - const bool exact_match = true; - const uint32_t max_matches = UINT32_MAX; - TypeList types; - - llvm::DenseSet searched_symbol_files; - const uint32_t num_types = module_sp->FindTypes( - name, exact_match, max_matches, searched_symbol_files, types); - - if (num_types) { - uint32_t i; - for (i = 0; i < num_types; ++i) { - TypeSP type_sp(types.GetTypeAtIndex(i)); - - if (ClangASTContext::IsObjCObjectOrInterfaceType( - type_sp->GetForwardCompilerType())) { - if (type_sp->IsCompleteObjCClass()) { - m_complete_class_cache[name] = type_sp; - return type_sp; - } - } - } - } - } - m_negative_complete_class_cache.insert(name); - return TypeSP(); -} - -size_t ObjCLanguageRuntime::GetByteOffsetForIvar(CompilerType &parent_qual_type, - const char *ivar_name) { - return LLDB_INVALID_IVAR_OFFSET; -} - -bool ObjCLanguageRuntime::ClassDescriptor::IsPointerValid( - lldb::addr_t value, uint32_t ptr_size, bool allow_NULLs, bool allow_tagged, - bool check_version_specific) const { - if (!value) - return allow_NULLs; - if ((value % 2) == 1 && allow_tagged) - return true; - if ((value % ptr_size) == 0) - return (check_version_specific ? CheckPointer(value, ptr_size) : true); - else - return false; -} - -ObjCLanguageRuntime::ObjCISA -ObjCLanguageRuntime::GetISA(ConstString name) { - ISAToDescriptorIterator pos = GetDescriptorIterator(name); - if (pos != m_isa_to_descriptor.end()) - return pos->first; - return 0; -} - -ObjCLanguageRuntime::ISAToDescriptorIterator -ObjCLanguageRuntime::GetDescriptorIterator(ConstString name) { - ISAToDescriptorIterator end = m_isa_to_descriptor.end(); - - if (name) { - UpdateISAToDescriptorMap(); - if (m_hash_to_isa_map.empty()) { - // No name hashes were provided, we need to just linearly power through - // the names and find a match - for (ISAToDescriptorIterator pos = m_isa_to_descriptor.begin(); - pos != end; ++pos) { - if (pos->second->GetClassName() == name) - return pos; - } - } else { - // Name hashes were provided, so use them to efficiently lookup name to - // isa/descriptor - const uint32_t name_hash = llvm::djbHash(name.GetStringRef()); - std::pair range = - m_hash_to_isa_map.equal_range(name_hash); - for (HashToISAIterator range_pos = range.first; range_pos != range.second; - ++range_pos) { - ISAToDescriptorIterator pos = - m_isa_to_descriptor.find(range_pos->second); - if (pos != m_isa_to_descriptor.end()) { - if (pos->second->GetClassName() == name) - return pos; - } - } - } - } - return end; -} - -std::pair -ObjCLanguageRuntime::GetDescriptorIteratorPair(bool update_if_needed) { - if (update_if_needed) - UpdateISAToDescriptorMapIfNeeded(); - - return std::pair( - m_isa_to_descriptor.begin(), m_isa_to_descriptor.end()); -} - -ObjCLanguageRuntime::ObjCISA -ObjCLanguageRuntime::GetParentClass(ObjCLanguageRuntime::ObjCISA isa) { - ClassDescriptorSP objc_class_sp(GetClassDescriptorFromISA(isa)); - if (objc_class_sp) { - ClassDescriptorSP objc_super_class_sp(objc_class_sp->GetSuperclass()); - if (objc_super_class_sp) - return objc_super_class_sp->GetISA(); - } - return 0; -} - -ConstString -ObjCLanguageRuntime::GetActualTypeName(ObjCLanguageRuntime::ObjCISA isa) { - ClassDescriptorSP objc_class_sp(GetNonKVOClassDescriptor(isa)); - if (objc_class_sp) - return objc_class_sp->GetClassName(); - return ConstString(); -} - -ObjCLanguageRuntime::ClassDescriptorSP -ObjCLanguageRuntime::GetClassDescriptorFromClassName( - ConstString class_name) { - ISAToDescriptorIterator pos = GetDescriptorIterator(class_name); - if (pos != m_isa_to_descriptor.end()) - return pos->second; - return ClassDescriptorSP(); -} - -ObjCLanguageRuntime::ClassDescriptorSP -ObjCLanguageRuntime::GetClassDescriptor(ValueObject &valobj) { - ClassDescriptorSP objc_class_sp; - // if we get an invalid VO (which might still happen when playing around with - // pointers returned by the expression parser, don't consider this a valid - // ObjC object) - if (valobj.GetCompilerType().IsValid()) { - addr_t isa_pointer = valobj.GetPointerValue(); - if (isa_pointer != LLDB_INVALID_ADDRESS) { - ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); - - Process *process = exe_ctx.GetProcessPtr(); - if (process) { - Status error; - ObjCISA isa = process->ReadPointerFromMemory(isa_pointer, error); - if (isa != LLDB_INVALID_ADDRESS) - objc_class_sp = GetClassDescriptorFromISA(isa); - } - } - } - return objc_class_sp; -} - -ObjCLanguageRuntime::ClassDescriptorSP -ObjCLanguageRuntime::GetNonKVOClassDescriptor(ValueObject &valobj) { - ObjCLanguageRuntime::ClassDescriptorSP objc_class_sp( - GetClassDescriptor(valobj)); - if (objc_class_sp) { - if (!objc_class_sp->IsKVO()) - return objc_class_sp; - - ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); - if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) - return non_kvo_objc_class_sp; - } - return ClassDescriptorSP(); -} - -ObjCLanguageRuntime::ClassDescriptorSP -ObjCLanguageRuntime::GetClassDescriptorFromISA(ObjCISA isa) { - if (isa) { - UpdateISAToDescriptorMap(); - ObjCLanguageRuntime::ISAToDescriptorIterator pos = - m_isa_to_descriptor.find(isa); - if (pos != m_isa_to_descriptor.end()) - return pos->second; - } - return ClassDescriptorSP(); -} - -ObjCLanguageRuntime::ClassDescriptorSP -ObjCLanguageRuntime::GetNonKVOClassDescriptor(ObjCISA isa) { - if (isa) { - ClassDescriptorSP objc_class_sp = GetClassDescriptorFromISA(isa); - if (objc_class_sp && objc_class_sp->IsValid()) { - if (!objc_class_sp->IsKVO()) - return objc_class_sp; - - ClassDescriptorSP non_kvo_objc_class_sp(objc_class_sp->GetSuperclass()); - if (non_kvo_objc_class_sp && non_kvo_objc_class_sp->IsValid()) - return non_kvo_objc_class_sp; - } - } - return ClassDescriptorSP(); -} - -CompilerType -ObjCLanguageRuntime::EncodingToType::RealizeType(const char *name, - bool for_expression) { - if (m_scratch_ast_ctx_up) - return RealizeType(*m_scratch_ast_ctx_up, name, for_expression); - return CompilerType(); -} - -CompilerType ObjCLanguageRuntime::EncodingToType::RealizeType( - ClangASTContext &ast_ctx, const char *name, bool for_expression) { - clang::ASTContext *clang_ast = ast_ctx.getASTContext(); - if (!clang_ast) - return CompilerType(); - return RealizeType(*clang_ast, name, for_expression); -} - -ObjCLanguageRuntime::EncodingToType::~EncodingToType() {} - -ObjCLanguageRuntime::EncodingToTypeSP ObjCLanguageRuntime::GetEncodingToType() { - return nullptr; -} - -bool ObjCLanguageRuntime::GetTypeBitSize(const CompilerType &compiler_type, - uint64_t &size) { - void *opaque_ptr = compiler_type.GetOpaqueQualType(); - size = m_type_size_cache.Lookup(opaque_ptr); - // an ObjC object will at least have an ISA, so 0 is definitely not OK - if (size > 0) - return true; - - ClassDescriptorSP class_descriptor_sp = - GetClassDescriptorFromClassName(compiler_type.GetTypeName()); - if (!class_descriptor_sp) - return false; - - int32_t max_offset = INT32_MIN; - uint64_t sizeof_max = 0; - bool found = false; - - for (size_t idx = 0; idx < class_descriptor_sp->GetNumIVars(); idx++) { - const auto &ivar = class_descriptor_sp->GetIVarAtIndex(idx); - int32_t cur_offset = ivar.m_offset; - if (cur_offset > max_offset) { - max_offset = cur_offset; - sizeof_max = ivar.m_size; - found = true; - } - } - - size = 8 * (max_offset + sizeof_max); - if (found) - m_type_size_cache.Insert(opaque_ptr, size); - - return found; -} - -lldb::BreakpointPreconditionSP -ObjCLanguageRuntime::GetBreakpointExceptionPrecondition(LanguageType language, - bool throw_bp) { - if (language != eLanguageTypeObjC) - return lldb::BreakpointPreconditionSP(); - if (!throw_bp) - return lldb::BreakpointPreconditionSP(); - BreakpointPreconditionSP precondition_sp( - new ObjCLanguageRuntime::ObjCExceptionPrecondition()); - return precondition_sp; -} - -// Exception breakpoint Precondition class for ObjC: -void ObjCLanguageRuntime::ObjCExceptionPrecondition::AddClassName( - const char *class_name) { - m_class_names.insert(class_name); -} - -ObjCLanguageRuntime::ObjCExceptionPrecondition::ObjCExceptionPrecondition() {} - -bool ObjCLanguageRuntime::ObjCExceptionPrecondition::EvaluatePrecondition( - StoppointCallbackContext &context) { - return true; -} - -void ObjCLanguageRuntime::ObjCExceptionPrecondition::GetDescription( - Stream &stream, lldb::DescriptionLevel level) {} - -Status ObjCLanguageRuntime::ObjCExceptionPrecondition::ConfigurePrecondition( - Args &args) { - Status error; - if (args.GetArgumentCount() > 0) - error.SetErrorString( - "The ObjC Exception breakpoint doesn't support extra options."); - return error; -} - -llvm::Optional -ObjCLanguageRuntime::GetRuntimeType(CompilerType base_type) { - CompilerType class_type; - bool is_pointer_type = false; - - if (ClangASTContext::IsObjCObjectPointerType(base_type, &class_type)) - is_pointer_type = true; - else if (ClangASTContext::IsObjCObjectOrInterfaceType(base_type)) - class_type = base_type; - else - return llvm::None; - - if (!class_type) - return llvm::None; - - ConstString class_name(class_type.GetConstTypeName()); - if (!class_name) - return llvm::None; - - TypeSP complete_objc_class_type_sp = LookupInCompleteClassCache(class_name); - if (!complete_objc_class_type_sp) - return llvm::None; - - CompilerType complete_class( - complete_objc_class_type_sp->GetFullCompilerType()); - if (complete_class.GetCompleteType()) { - if (is_pointer_type) - return complete_class.GetPointerType(); - else - return complete_class; - } - - return llvm::None; -}