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_
18 #include <unordered_set>
20 // Other libraries and framework includes
21 #include "llvm/Support/Casting.h"
24 #include "lldb/Core/PluginInterface.h"
25 #include "lldb/Core/ThreadSafeDenseMap.h"
26 #include "lldb/Symbol/CompilerType.h"
27 #include "lldb/Symbol/DeclVendor.h"
28 #include "lldb/Symbol/Type.h"
29 #include "lldb/Target/LanguageRuntime.h"
30 #include "lldb/lldb-private.h"
32 class CommandObjectObjC_ClassTable_Dump;
34 namespace lldb_private {
36 class UtilityFunction;
38 class ObjCLanguageRuntime : public LanguageRuntime {
40 enum class ObjCRuntimeVersions {
41 eObjC_VersionUnknown = 0,
46 typedef lldb::addr_t ObjCISA;
48 class ClassDescriptor;
49 typedef std::shared_ptr<ClassDescriptor> ClassDescriptorSP;
51 // the information that we want to support retrieving from an ObjC class
52 // this needs to be pure virtual since there are at least 2 different
54 // of the runtime, and more might come
55 class ClassDescriptor {
58 : m_is_kvo(eLazyBoolCalculate), m_is_cf(eLazyBoolCalculate),
61 virtual ~ClassDescriptor() = default;
63 virtual ConstString GetClassName() = 0;
65 virtual ClassDescriptorSP GetSuperclass() = 0;
67 virtual ClassDescriptorSP GetMetaclass() const = 0;
69 // virtual if any implementation has some other version-specific rules
70 // but for the known v1/v2 this is all that needs to be done
71 virtual bool IsKVO() {
72 if (m_is_kvo == eLazyBoolCalculate) {
73 const char *class_name = GetClassName().AsCString();
74 if (class_name && *class_name)
76 (LazyBool)(strstr(class_name, "NSKVONotifying_") == class_name);
78 return (m_is_kvo == eLazyBoolYes);
81 // virtual if any implementation has some other version-specific rules
82 // but for the known v1/v2 this is all that needs to be done
83 virtual bool IsCFType() {
84 if (m_is_cf == eLazyBoolCalculate) {
85 const char *class_name = GetClassName().AsCString();
86 if (class_name && *class_name)
87 m_is_cf = (LazyBool)(strcmp(class_name, "__NSCFType") == 0 ||
88 strcmp(class_name, "NSCFType") == 0);
90 return (m_is_cf == eLazyBoolYes);
93 virtual bool IsValid() = 0;
95 virtual bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
96 uint64_t *value_bits = nullptr,
97 uint64_t *payload = nullptr) = 0;
99 virtual uint64_t GetInstanceSize() = 0;
101 // use to implement version-specific additional constraints on pointers
102 virtual bool CheckPointer(lldb::addr_t value, uint32_t ptr_size) const {
106 virtual ObjCISA GetISA() = 0;
108 // This should return true iff the interface could be completed
110 Describe(std::function<void(ObjCISA)> const &superclass_func,
111 std::function<bool(const char *, const char *)> const
112 &instance_method_func,
113 std::function<bool(const char *, const char *)> const
115 std::function<bool(const char *, const char *, lldb::addr_t,
116 uint64_t)> const &ivar_func) const {
120 lldb::TypeSP GetType() { return m_type_wp.lock(); }
122 void SetType(const lldb::TypeSP &type_sp) { m_type_wp = type_sp; }
124 struct iVarDescriptor {
131 virtual size_t GetNumIVars() { return 0; }
133 virtual iVarDescriptor GetIVarAtIndex(size_t idx) {
134 return iVarDescriptor();
138 bool IsPointerValid(lldb::addr_t value, uint32_t ptr_size,
139 bool allow_NULLs = false, bool allow_tagged = false,
140 bool check_version_specific = false) const;
145 lldb::TypeWP m_type_wp;
148 class EncodingToType {
150 virtual ~EncodingToType();
152 virtual CompilerType RealizeType(ClangASTContext &ast_ctx, const char *name,
153 bool for_expression);
154 virtual CompilerType RealizeType(const char *name, bool for_expression);
156 virtual CompilerType RealizeType(clang::ASTContext &ast_ctx,
157 const char *name, bool for_expression) = 0;
160 std::unique_ptr<ClangASTContext> m_scratch_ast_ctx_ap;
163 class ObjCExceptionPrecondition : public Breakpoint::BreakpointPrecondition {
165 ObjCExceptionPrecondition();
167 ~ObjCExceptionPrecondition() override = default;
169 bool EvaluatePrecondition(StoppointCallbackContext &context) override;
170 void GetDescription(Stream &stream, lldb::DescriptionLevel level) override;
171 Error ConfigurePrecondition(Args &args) override;
174 void AddClassName(const char *class_name);
177 std::unordered_set<std::string> m_class_names;
180 class TaggedPointerVendor {
182 virtual ~TaggedPointerVendor() = default;
184 virtual bool IsPossibleTaggedPointer(lldb::addr_t ptr) = 0;
186 virtual ObjCLanguageRuntime::ClassDescriptorSP
187 GetClassDescriptor(lldb::addr_t ptr) = 0;
190 TaggedPointerVendor() = default;
193 DISALLOW_COPY_AND_ASSIGN(TaggedPointerVendor);
196 ~ObjCLanguageRuntime() override;
198 virtual TaggedPointerVendor *GetTaggedPointerVendor() { return nullptr; }
200 typedef std::shared_ptr<EncodingToType> EncodingToTypeSP;
202 virtual EncodingToTypeSP GetEncodingToType();
204 virtual ClassDescriptorSP GetClassDescriptor(ValueObject &in_value);
206 ClassDescriptorSP GetNonKVOClassDescriptor(ValueObject &in_value);
208 virtual ClassDescriptorSP
209 GetClassDescriptorFromClassName(const ConstString &class_name);
211 virtual ClassDescriptorSP GetClassDescriptorFromISA(ObjCISA isa);
213 ClassDescriptorSP GetNonKVOClassDescriptor(ObjCISA isa);
215 lldb::LanguageType GetLanguageType() const override {
216 return lldb::eLanguageTypeObjC;
219 virtual bool IsModuleObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
221 virtual bool ReadObjCLibrary(const lldb::ModuleSP &module_sp) = 0;
223 virtual bool HasReadObjCLibrary() = 0;
225 virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread,
226 bool stop_others) = 0;
228 lldb::addr_t LookupInMethodCache(lldb::addr_t class_addr, lldb::addr_t sel);
230 void AddToMethodCache(lldb::addr_t class_addr, lldb::addr_t sel,
231 lldb::addr_t impl_addr);
233 TypeAndOrName LookupInClassNameCache(lldb::addr_t class_addr);
235 void AddToClassNameCache(lldb::addr_t class_addr, const char *name,
236 lldb::TypeSP type_sp);
238 void AddToClassNameCache(lldb::addr_t class_addr,
239 const TypeAndOrName &class_or_type_name);
241 lldb::TypeSP LookupInCompleteClassCache(ConstString &name);
243 virtual UtilityFunction *CreateObjectChecker(const char *) = 0;
245 virtual ObjCRuntimeVersions GetRuntimeVersion() const {
246 return ObjCRuntimeVersions::eObjC_VersionUnknown;
249 bool IsValidISA(ObjCISA isa) {
250 UpdateISAToDescriptorMap();
251 return m_isa_to_descriptor.count(isa) > 0;
254 virtual void UpdateISAToDescriptorMapIfNeeded() = 0;
256 void UpdateISAToDescriptorMap() {
257 if (m_process && m_process->GetStopID() != m_isa_to_descriptor_stop_id) {
258 UpdateISAToDescriptorMapIfNeeded();
262 virtual ObjCISA GetISA(const ConstString &name);
264 virtual ConstString GetActualTypeName(ObjCISA isa);
266 virtual ObjCISA GetParentClass(ObjCISA isa);
268 virtual DeclVendor *GetDeclVendor() { return nullptr; }
270 // Finds the byte offset of the child_type ivar in parent_type. If it can't
272 // offset, returns LLDB_INVALID_IVAR_OFFSET.
274 virtual size_t GetByteOffsetForIvar(CompilerType &parent_qual_type,
275 const char *ivar_name);
277 // Given the name of an Objective-C runtime symbol (e.g., ivar offset symbol),
278 // try to determine from the runtime what the value of that symbol would be.
279 // Useful when the underlying binary is stripped.
280 virtual lldb::addr_t LookupRuntimeSymbol(const ConstString &name) {
281 return LLDB_INVALID_ADDRESS;
284 bool HasNewLiteralsAndIndexing() {
285 if (m_has_new_literals_and_indexing == eLazyBoolCalculate) {
286 if (CalculateHasNewLiteralsAndIndexing())
287 m_has_new_literals_and_indexing = eLazyBoolYes;
289 m_has_new_literals_and_indexing = eLazyBoolNo;
292 return (m_has_new_literals_and_indexing == eLazyBoolYes);
295 virtual void SymbolsDidLoad(const ModuleList &module_list) {
296 m_negative_complete_class_cache.clear();
299 bool GetTypeBitSize(const CompilerType &compiler_type,
300 uint64_t &size) override;
303 //------------------------------------------------------------------
304 // Classes that inherit from ObjCLanguageRuntime can see and modify these
305 //------------------------------------------------------------------
306 ObjCLanguageRuntime(Process *process);
308 virtual bool CalculateHasNewLiteralsAndIndexing() { return false; }
310 bool ISAIsCached(ObjCISA isa) const {
311 return m_isa_to_descriptor.find(isa) != m_isa_to_descriptor.end();
314 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp) {
316 m_isa_to_descriptor[isa] = descriptor_sp;
322 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
323 const char *class_name);
325 bool AddClass(ObjCISA isa, const ClassDescriptorSP &descriptor_sp,
326 uint32_t class_name_hash) {
328 m_isa_to_descriptor[isa] = descriptor_sp;
329 m_hash_to_isa_map.insert(std::make_pair(class_name_hash, isa));
336 // We keep a map of <Class,Selector>->Implementation so we don't have to call
338 // function over and over.
340 // FIXME: We need to watch for the loading of Protocols, and flush the cache
342 // class that we see so changed.
346 sel_addr = LLDB_INVALID_ADDRESS;
347 class_addr = LLDB_INVALID_ADDRESS;
350 ClassAndSel(lldb::addr_t in_sel_addr, lldb::addr_t in_class_addr)
351 : class_addr(in_class_addr), sel_addr(in_sel_addr) {}
353 bool operator==(const ClassAndSel &rhs) {
354 if (class_addr == rhs.class_addr && sel_addr == rhs.sel_addr)
360 bool operator<(const ClassAndSel &rhs) const {
361 if (class_addr < rhs.class_addr)
363 else if (class_addr > rhs.class_addr)
366 if (sel_addr < rhs.sel_addr)
373 lldb::addr_t class_addr;
374 lldb::addr_t sel_addr;
377 typedef std::map<ClassAndSel, lldb::addr_t> MsgImplMap;
378 typedef std::map<ObjCISA, ClassDescriptorSP> ISAToDescriptorMap;
379 typedef std::multimap<uint32_t, ObjCISA> HashToISAMap;
380 typedef ISAToDescriptorMap::iterator ISAToDescriptorIterator;
381 typedef HashToISAMap::iterator HashToISAIterator;
382 typedef ThreadSafeDenseMap<void *, uint64_t> TypeSizeCache;
384 MsgImplMap m_impl_cache;
385 LazyBool m_has_new_literals_and_indexing;
386 ISAToDescriptorMap m_isa_to_descriptor;
387 HashToISAMap m_hash_to_isa_map;
388 TypeSizeCache m_type_size_cache;
391 uint32_t m_isa_to_descriptor_stop_id;
393 typedef std::map<ConstString, lldb::TypeWP> CompleteClassMap;
394 CompleteClassMap m_complete_class_cache;
396 struct ConstStringSetHelpers {
397 size_t operator()(const ConstString &arg) const // for hashing
399 return (size_t)arg.GetCString();
401 bool operator()(const ConstString &arg1,
402 const ConstString &arg2) const // for equality
404 return arg1.operator==(arg2);
407 typedef std::unordered_set<ConstString, ConstStringSetHelpers,
408 ConstStringSetHelpers>
410 CompleteClassSet m_negative_complete_class_cache;
412 ISAToDescriptorIterator GetDescriptorIterator(const ConstString &name);
414 friend class ::CommandObjectObjC_ClassTable_Dump;
416 std::pair<ISAToDescriptorIterator, ISAToDescriptorIterator>
417 GetDescriptorIteratorPair(bool update_if_needed = true);
419 void ReadObjCLibraryIfNeeded(const ModuleList &module_list);
421 DISALLOW_COPY_AND_ASSIGN(ObjCLanguageRuntime);
424 } // namespace lldb_private
426 #endif // liblldb_ObjCLanguageRuntime_h_