Path: blob/main/contrib/llvm-project/lldb/source/Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h
39642 views
//===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H9#define LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H1011#include <functional>12#include <map>13#include <memory>14#include <optional>15#include <unordered_set>1617#include "llvm/Support/Casting.h"1819#include "lldb/Breakpoint/BreakpointPrecondition.h"20#include "lldb/Core/PluginInterface.h"21#include "lldb/Symbol/CompilerType.h"22#include "lldb/Symbol/Type.h"23#include "lldb/Target/LanguageRuntime.h"24#include "lldb/Utility/ConstString.h"25#include "lldb/Utility/ThreadSafeDenseMap.h"26#include "lldb/lldb-enumerations.h"27#include "lldb/lldb-private.h"2829class CommandObjectObjC_ClassTable_Dump;3031namespace lldb_private {3233class TypeSystemClang;34class UtilityFunction;3536class ObjCLanguageRuntime : public LanguageRuntime {37public:38enum class ObjCRuntimeVersions {39eObjC_VersionUnknown = 0,40eAppleObjC_V1 = 1,41eAppleObjC_V2 = 2,42eGNUstep_libobjc2 = 3,43};4445typedef lldb::addr_t ObjCISA;4647class ClassDescriptor;48typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;4950// the information that we want to support retrieving from an ObjC class this51// needs to be pure virtual since there are at least 2 different52// implementations of the runtime, and more might come53class ClassDescriptor {54public:55ClassDescriptor() : m_type_wp() {}5657virtual ~ClassDescriptor() = default;5859virtual ConstString GetClassName() = 0;6061virtual ClassDescriptorSP GetSuperclass() = 0;6263virtual ClassDescriptorSP GetMetaclass() const = 0;6465// virtual if any implementation has some other version-specific rules but66// for the known v1/v2 this is all that needs to be done67virtual bool IsKVO() {68if (m_is_kvo == eLazyBoolCalculate) {69const char *class_name = GetClassName().AsCString();70if (class_name && *class_name)71m_is_kvo =72(LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);73}74return (m_is_kvo == eLazyBoolYes);75}7677// virtual if any implementation has some other version-specific rules but78// for the known v1/v2 this is all that needs to be done79virtual bool IsCFType() {80if (m_is_cf == eLazyBoolCalculate) {81const char *class_name = GetClassName().AsCString();82if (class_name && *class_name)83m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||84strcmp(class_name, "NSCFType") == 0);85}86return (m_is_cf == eLazyBoolYes);87}8889/// Determine whether this class is implemented in Swift.90virtual lldb::LanguageType GetImplementationLanguage() const {91return lldb::eLanguageTypeObjC;92}9394virtual bool IsValid() = 0;9596/// There are two routines in the ObjC runtime that tagged pointer clients97/// can call to get the value from their tagged pointer, one that retrieves98/// it as an unsigned value and one a signed value. These two99/// GetTaggedPointerInfo methods mirror those two ObjC runtime calls.100/// @{101virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,102uint64_t *value_bits = nullptr,103uint64_t *payload = nullptr) = 0;104105virtual bool GetTaggedPointerInfoSigned(uint64_t *info_bits = nullptr,106int64_t *value_bits = nullptr,107uint64_t *payload = nullptr) = 0;108/// @}109110virtual uint64_t GetInstanceSize() = 0;111112// use to implement version-specific additional constraints on pointers113virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {114return true;115}116117virtual ObjCISA GetISA() = 0;118119// This should return true iff the interface could be completed120virtual bool121Describe(std::function<void(ObjCISA)> const &superclass_func,122std::function<bool(const char *, const char *)> const123&instance_method_func,124std::function<bool(const char *, const char *)> const125&class_method_func,126std::function<bool(const char *, const char *, lldb::addr_t,127uint64_t)> const &ivar_func) const {128return false;129}130131lldb::TypeSP GetType() { return m_type_wp.lock(); }132133void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }134135struct iVarDescriptor {136ConstString m_name;137CompilerType m_type;138uint64_t m_size;139int32_t m_offset;140};141142virtual size_t GetNumIVars() { return 0; }143144virtual iVarDescriptor GetIVarAtIndex(size_t idx) {145return iVarDescriptor();146}147148protected:149bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,150bool allow_NULLs = false, bool allow_tagged = false,151bool check_version_specific = false) const;152153private:154LazyBool m_is_kvo = eLazyBoolCalculate;155LazyBool m_is_cf = eLazyBoolCalculate;156lldb::TypeWP m_type_wp;157};158159class EncodingToType {160public:161virtual ~EncodingToType();162163virtual CompilerType RealizeType(TypeSystemClang &ast_ctx, const char *name,164bool for_expression) = 0;165virtual CompilerType RealizeType(const char *name, bool for_expression);166167protected:168std::shared_ptr<TypeSystemClang> m_scratch_ast_ctx_sp;169};170171class ObjCExceptionPrecondition : public BreakpointPrecondition {172public:173ObjCExceptionPrecondition();174175~ObjCExceptionPrecondition() override = default;176177bool EvaluatePrecondition(StoppointCallbackContext &context) override;178void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;179Status ConfigurePrecondition(Args &args) override;180181protected:182void AddClassName(const char *class_name);183184private:185std::unordered_set<std::string> m_class_names;186};187188static lldb::BreakpointPreconditionSP189GetBreakpointExceptionPrecondition(lldb::LanguageType language,190bool throw_bp);191192class TaggedPointerVendor {193public:194virtual ~TaggedPointerVendor() = default;195196virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0;197198virtual ObjCLanguageRuntime::ClassDescriptorSP199GetClassDescriptor(lldb::addr_t ptr) = 0;200201protected:202TaggedPointerVendor() = default;203204private:205TaggedPointerVendor(const TaggedPointerVendor &) = delete;206const TaggedPointerVendor &operator=(const TaggedPointerVendor &) = delete;207};208209~ObjCLanguageRuntime() override;210211static char ID;212213bool isA(const void *ClassID) const override {214return ClassID == &ID || LanguageRuntime::isA(ClassID);215}216217static bool classof(const LanguageRuntime *runtime) {218return runtime->isA(&ID);219}220221static ObjCLanguageRuntime *Get(Process &process) {222return llvm::cast_or_null<ObjCLanguageRuntime>(223process.GetLanguageRuntime(lldb::eLanguageTypeObjC));224}225226virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }227228typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;229230virtual EncodingToTypeSP GetEncodingToType();231232virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value);233234ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value);235236virtual ClassDescriptorSP237GetClassDescriptorFromClassName(ConstString class_name);238239virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa);240241ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa);242243lldb::LanguageType GetLanguageType() const override {244return lldb::eLanguageTypeObjC;245}246247virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;248249virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;250251virtual bool HasReadObjCLibrary() = 0;252253// These two methods actually use different caches. The only time we'll254// cache a sel_str is if we found a "selector specific stub" for the selector255// and conversely we only add to the SEL cache if we saw a regular dispatch.256lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel);257lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr,258llvm::StringRef sel_str);259260void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,261lldb::addr_t impl_addr);262263void AddToMethodCache(lldb::addr_t class_addr, llvm::StringRef sel_str,264lldb::addr_t impl_addr);265266TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr);267268void AddToClassNameCache(lldb::addr_t class_addr, const char *name,269lldb::TypeSP type_sp);270271void AddToClassNameCache(lldb::addr_t class_addr,272const TypeAndOrName &class_or_type_name);273274lldb::TypeSP LookupInCompleteClassCache(ConstString &name);275276std::optional<CompilerType> GetRuntimeType(CompilerType base_type) override;277278virtual llvm::Expected<std::unique_ptr<UtilityFunction>>279CreateObjectChecker(std::string name, ExecutionContext &exe_ctx) = 0;280281virtual ObjCRuntimeVersions GetRuntimeVersion() const {282return ObjCRuntimeVersions::eObjC_VersionUnknown;283}284285bool IsValidISA(ObjCISA isa) {286UpdateISAToDescriptorMap();287return m_isa_to_descriptor.count(isa) > 0;288}289290virtual void UpdateISAToDescriptorMapIfNeeded() = 0;291292void UpdateISAToDescriptorMap() {293if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) {294UpdateISAToDescriptorMapIfNeeded();295}296}297298virtual ObjCISA GetISA(ConstString name);299300virtual ObjCISA GetParentClass(ObjCISA isa);301302// Finds the byte offset of the child_type ivar in parent_type. If it can't303// find the offset, returns LLDB_INVALID_IVAR_OFFSET.304305virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,306const char *ivar_name);307308bool HasNewLiteralsAndIndexing() {309if (m_has_new_literals_and_indexing == eLazyBoolCalculate) {310if (CalculateHasNewLiteralsAndIndexing())311m_has_new_literals_and_indexing = eLazyBoolYes;312else313m_has_new_literals_and_indexing = eLazyBoolNo;314}315316return (m_has_new_literals_and_indexing == eLazyBoolYes);317}318319void SymbolsDidLoad(const ModuleList &module_list) override {320m_negative_complete_class_cache.clear();321}322323std::optional<uint64_t>324GetTypeBitSize(const CompilerType &compiler_type) override;325326/// Check whether the name is "self" or "_cmd" and should show up in327/// "frame variable".328bool IsAllowedRuntimeValue(ConstString name) override;329330protected:331// Classes that inherit from ObjCLanguageRuntime can see and modify these332ObjCLanguageRuntime(Process *process);333334virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }335336bool ISAIsCached(ObjCISA isa) const {337return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();338}339340bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {341if (isa != 0) {342m_isa_to_descriptor[isa] = descriptor_sp;343return true;344}345return false;346}347348bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,349const char *class_name);350351bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,352uint32_t class_name_hash) {353if (isa != 0) {354m_isa_to_descriptor[isa] = descriptor_sp;355m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));356return true;357}358return false;359}360361private:362// We keep two maps of <Class,Selector>->Implementation so we don't have363// to call the resolver function over and over.364// The first comes from regular obj_msgSend type dispatch, and maps the365// class + uniqued SEL value to an implementation.366// The second comes from the "selector-specific stubs", which are always367// of the form _objc_msgSend$SelectorName, so we don't know the uniqued368// selector, only the string name.369370// FIXME: We need to watch for the loading of Protocols, and flush the cache371// for any372// class that we see so changed.373374struct ClassAndSel {375ClassAndSel() = default;376377ClassAndSel(lldb::addr_t in_class_addr, lldb::addr_t in_sel_addr)378: class_addr(in_class_addr), sel_addr(in_sel_addr) {}379380bool operator==(const ClassAndSel &rhs) {381if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)382return true;383else384return false;385}386387bool operator<(const ClassAndSel &rhs) const {388if (class_addr < rhs.class_addr)389return true;390else if (class_addr > rhs.class_addr)391return false;392else {393if (sel_addr < rhs.sel_addr)394return true;395else396return false;397}398}399400lldb::addr_t class_addr = LLDB_INVALID_ADDRESS;401lldb::addr_t sel_addr = LLDB_INVALID_ADDRESS;402};403404struct ClassAndSelStr {405ClassAndSelStr() = default;406407ClassAndSelStr(lldb::addr_t in_class_addr, llvm::StringRef in_sel_name)408: class_addr(in_class_addr), sel_name(in_sel_name) {}409410bool operator==(const ClassAndSelStr &rhs) {411return class_addr == rhs.class_addr && sel_name == rhs.sel_name;412}413414bool operator<(const ClassAndSelStr &rhs) const {415if (class_addr < rhs.class_addr)416return true;417else if (class_addr > rhs.class_addr)418return false;419else420return ConstString::Compare(sel_name, rhs.sel_name);421}422423lldb::addr_t class_addr = LLDB_INVALID_ADDRESS;424ConstString sel_name;425};426427typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;428typedef std::map<ClassAndSelStr, lldb::addr_t> MsgImplStrMap;429typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;430typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;431typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;432typedef HashToISAMap::iterator HashToISAIterator;433typedef ThreadSafeDenseMap<void *, uint64_t> TypeSizeCache;434435MsgImplMap m_impl_cache;436MsgImplStrMap m_impl_str_cache;437LazyBool m_has_new_literals_and_indexing;438ISAToDescriptorMap m_isa_to_descriptor;439HashToISAMap m_hash_to_isa_map;440TypeSizeCache m_type_size_cache;441442protected:443uint32_t m_isa_to_descriptor_stop_id;444445typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;446CompleteClassMap m_complete_class_cache;447448struct ConstStringSetHelpers {449size_t operator()(ConstString arg) const // for hashing450{451return (size_t)arg.GetCString();452}453bool operator()(ConstString arg1,454ConstString arg2) const // for equality455{456return arg1.operator==(arg2);457}458};459typedef std::unordered_set<ConstString, ConstStringSetHelpers,460ConstStringSetHelpers>461CompleteClassSet;462CompleteClassSet m_negative_complete_class_cache;463464ISAToDescriptorIterator GetDescriptorIterator(ConstString name);465466friend class ::CommandObjectObjC_ClassTable_Dump;467468std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>469GetDescriptorIteratorPair(bool update_if_needed = true);470471void ReadObjCLibraryIfNeeded(const ModuleList &module_list);472473ObjCLanguageRuntime(const ObjCLanguageRuntime &) = delete;474const ObjCLanguageRuntime &operator=(const ObjCLanguageRuntime &) = delete;475};476477} // namespace lldb_private478479#endif // LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_OBJCLANGUAGERUNTIME_H480481482