1 //===-- ObjCLanguageRuntime.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_ObjCLanguageRuntime_h_
11 #define liblldb_ObjCLanguageRuntime_h_
16 #include <unordered_set>
18 #include "llvm/Support/Casting.h"
20 #include "lldb/Core/PluginInterface.h"
21 #include "lldb/Core/ThreadSafeDenseMap.h"
22 #include "lldb/Symbol/CompilerType.h"
23 #include "lldb/Symbol/DeclVendor.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Target/LanguageRuntime.h"
26 #include "lldb/lldb-private.h"
28 class CommandObjectObjC_ClassTable_Dump;
30 namespace lldb_private {
32 class UtilityFunction;
34 class ObjCLanguageRuntime : public LanguageRuntime {
36 enum class ObjCRuntimeVersions {
37 eObjC_VersionUnknown = 0,
42 typedef lldb::addr_t ObjCISA;
44 class ClassDescriptor;
45 typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
47 // the information that we want to support retrieving from an ObjC class this
48 // needs to be pure virtual since there are at least 2 different
49 // implementations of the runtime, and more might come
50 class ClassDescriptor {
53 : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate),
56 virtual ~ClassDescriptor() = default;
58 virtual ConstString GetClassName() = 0;
60 virtual ClassDescriptorSP GetSuperclass() = 0;
62 virtual ClassDescriptorSP GetMetaclass() const = 0;
64 // virtual if any implementation has some other version-specific rules but
65 // for the known v1/v2 this is all that needs to be done
66 virtual bool IsKVO() {
67 if (m_is_kvo == eLazyBoolCalculate) {
68 const char *class_name = GetClassName().AsCString();
69 if (class_name && *class_name)
71 (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
73 return (m_is_kvo == eLazyBoolYes);
76 // virtual if any implementation has some other version-specific rules but
77 // for the known v1/v2 this is all that needs to be done
78 virtual bool IsCFType() {
79 if (m_is_cf == eLazyBoolCalculate) {
80 const char *class_name = GetClassName().AsCString();
81 if (class_name && *class_name)
82 m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
83 strcmp(class_name, "NSCFType") == 0);
85 return (m_is_cf == eLazyBoolYes);
88 virtual bool IsValid() = 0;
90 virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
91 uint64_t *value_bits = nullptr,
92 uint64_t *payload = nullptr) = 0;
94 virtual uint64_t GetInstanceSize() = 0;
96 // use to implement version-specific additional constraints on pointers
97 virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
101 virtual ObjCISA GetISA() = 0;
103 // This should return true iff the interface could be completed
105 Describe(std::function<void(ObjCISA)> const &superclass_func,
106 std::function<bool(const char *, const char *)> const
107 &instance_method_func,
108 std::function<bool(const char *, const char *)> const
110 std::function<bool(const char *, const char *, lldb::addr_t,
111 uint64_t)> const &ivar_func) const {
115 lldb::TypeSP GetType() { return m_type_wp.lock(); }
117 void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
119 struct iVarDescriptor {
126 virtual size_t GetNumIVars() { return 0; }
128 virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
129 return iVarDescriptor();
133 bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
134 bool allow_NULLs = false, bool allow_tagged = false,
135 bool check_version_specific = false) const;
140 lldb::TypeWP m_type_wp;
143 class EncodingToType {
145 virtual ~EncodingToType();
147 virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name,
148 bool for_expression);
149 virtual CompilerType RealizeType(const char *name, bool for_expression);
151 virtual CompilerType RealizeType(clang::ASTContext &ast_ctx,
152 const char *name, bool for_expression) = 0;
155 std::unique_ptr<ClangASTContext> m_scratch_ast_ctx_ap;
158 class ObjCExceptionPrecondition : public Breakpoint::BreakpointPrecondition {
160 ObjCExceptionPrecondition();
162 ~ObjCExceptionPrecondition() override = default;
164 bool EvaluatePrecondition(StoppointCallbackContext &context) override;
165 void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
166 Status ConfigurePrecondition(Args &args) override;
169 void AddClassName(const char *class_name);
172 std::unordered_set<std::string> m_class_names;
175 class TaggedPointerVendor {
177 virtual ~TaggedPointerVendor() = default;
179 virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0;
181 virtual ObjCLanguageRuntime::ClassDescriptorSP
182 GetClassDescriptor(lldb::addr_t ptr) = 0;
185 TaggedPointerVendor() = default;
188 DISALLOW_COPY_AND_ASSIGN(TaggedPointerVendor);
191 ~ObjCLanguageRuntime() override;
193 virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
195 typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
197 virtual EncodingToTypeSP GetEncodingToType();
199 virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value);
201 ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value);
203 virtual ClassDescriptorSP
204 GetClassDescriptorFromClassName(const ConstString &class_name);
206 virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa);
208 ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa);
210 lldb::LanguageType GetLanguageType() const override {
211 return lldb::eLanguageTypeObjC;
214 virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
216 virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
218 virtual bool HasReadObjCLibrary() = 0;
220 virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
221 bool stop_others) = 0;
223 lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel);
225 void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
226 lldb::addr_t impl_addr);
228 TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr);
230 void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
231 lldb::TypeSP type_sp);
233 void AddToClassNameCache(lldb::addr_t class_addr,
234 const TypeAndOrName &class_or_type_name);
236 lldb::TypeSP LookupInCompleteClassCache(ConstString &name);
238 virtual UtilityFunction *CreateObjectChecker(const char *) = 0;
240 virtual ObjCRuntimeVersions GetRuntimeVersion() const {
241 return ObjCRuntimeVersions::eObjC_VersionUnknown;
244 bool IsValidISA(ObjCISA isa) {
245 UpdateISAToDescriptorMap();
246 return m_isa_to_descriptor.count(isa) > 0;
249 virtual void UpdateISAToDescriptorMapIfNeeded() = 0;
251 void UpdateISAToDescriptorMap() {
252 if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) {
253 UpdateISAToDescriptorMapIfNeeded();
257 virtual ObjCISA GetISA(const ConstString &name);
259 virtual ConstString GetActualTypeName(ObjCISA isa);
261 virtual ObjCISA GetParentClass(ObjCISA isa);
263 virtual DeclVendor *GetDeclVendor() { return nullptr; }
265 // Finds the byte offset of the child_type ivar in parent_type. If it can't
266 // find the offset, returns LLDB_INVALID_IVAR_OFFSET.
268 virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
269 const char *ivar_name);
271 // Given the name of an Objective-C runtime symbol (e.g., ivar offset
272 // symbol), try to determine from the runtime what the value of that symbol
273 // would be. Useful when the underlying binary is stripped.
274 virtual lldb::addr_t LookupRuntimeSymbol(const ConstString &name) {
275 return LLDB_INVALID_ADDRESS;
278 bool HasNewLiteralsAndIndexing() {
279 if (m_has_new_literals_and_indexing == eLazyBoolCalculate) {
280 if (CalculateHasNewLiteralsAndIndexing())
281 m_has_new_literals_and_indexing = eLazyBoolYes;
283 m_has_new_literals_and_indexing = eLazyBoolNo;
286 return (m_has_new_literals_and_indexing == eLazyBoolYes);
289 virtual void SymbolsDidLoad(const ModuleList &module_list) {
290 m_negative_complete_class_cache.clear();
293 bool GetTypeBitSize(const CompilerType &compiler_type,
294 uint64_t &size) override;
297 //------------------------------------------------------------------
298 // Classes that inherit from ObjCLanguageRuntime can see and modify these
299 //------------------------------------------------------------------
300 ObjCLanguageRuntime(Process *process);
302 virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
304 bool ISAIsCached(ObjCISA isa) const {
305 return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
308 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
310 m_isa_to_descriptor[isa] = descriptor_sp;
316 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
317 const char *class_name);
319 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
320 uint32_t class_name_hash) {
322 m_isa_to_descriptor[isa] = descriptor_sp;
323 m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));
330 // We keep a map of <Class,Selector>->Implementation so we don't have to call
331 // the resolver function over and over.
333 // FIXME: We need to watch for the loading of Protocols, and flush the cache
335 // class that we see so changed.
339 sel_addr = LLDB_INVALID_ADDRESS;
340 class_addr = LLDB_INVALID_ADDRESS;
343 ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr)
344 : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
346 bool operator==(const ClassAndSel &rhs) {
347 if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
353 bool operator<(const ClassAndSel &rhs) const {
354 if (class_addr < rhs.class_addr)
356 else if (class_addr > rhs.class_addr)
359 if (sel_addr < rhs.sel_addr)
366 lldb::addr_t class_addr;
367 lldb::addr_t sel_addr;
370 typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
371 typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
372 typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;
373 typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
374 typedef HashToISAMap::iterator HashToISAIterator;
375 typedef ThreadSafeDenseMap<void *, uint64_t> TypeSizeCache;
377 MsgImplMap m_impl_cache;
378 LazyBool m_has_new_literals_and_indexing;
379 ISAToDescriptorMap m_isa_to_descriptor;
380 HashToISAMap m_hash_to_isa_map;
381 TypeSizeCache m_type_size_cache;
384 uint32_t m_isa_to_descriptor_stop_id;
386 typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
387 CompleteClassMap m_complete_class_cache;
389 struct ConstStringSetHelpers {
390 size_t operator()(const ConstString &arg) const // for hashing
392 return (size_t)arg.GetCString();
394 bool operator()(const ConstString &arg1,
395 const ConstString &arg2) const // for equality
397 return arg1.operator==(arg2);
400 typedef std::unordered_set<ConstString, ConstStringSetHelpers,
401 ConstStringSetHelpers>
403 CompleteClassSet m_negative_complete_class_cache;
405 ISAToDescriptorIterator GetDescriptorIterator(const ConstString &name);
407 friend class ::CommandObjectObjC_ClassTable_Dump;
409 std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
410 GetDescriptorIteratorPair(bool update_if_needed = true);
412 void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
414 DISALLOW_COPY_AND_ASSIGN(ObjCLanguageRuntime);
417 } // namespace lldb_private
419 #endif // liblldb_ObjCLanguageRuntime_h_