1 //===-- AppleObjCClassDescriptorV2.h ----------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_APPLEOBJCRUNTIME_APPLEOBJCCLASSDESCRIPTORV2_H
10 #define LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_APPLEOBJCRUNTIME_APPLEOBJCCLASSDESCRIPTORV2_H
14 #include "AppleObjCRuntimeV2.h"
15 #include "lldb/lldb-private.h"
17 #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
19 namespace lldb_private {
21 class ClassDescriptorV2 : public ObjCLanguageRuntime::ClassDescriptor {
23 friend class lldb_private::AppleObjCRuntimeV2;
25 ~ClassDescriptorV2() override = default;
27 ConstString GetClassName() override;
29 ObjCLanguageRuntime::ClassDescriptorSP GetSuperclass() override;
31 ObjCLanguageRuntime::ClassDescriptorSP GetMetaclass() const override;
33 bool IsValid() override {
34 return true; // any Objective-C v2 runtime class descriptor we vend is valid
37 // a custom descriptor is used for tagged pointers
38 bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
39 uint64_t *value_bits = nullptr,
40 uint64_t *payload = nullptr) override {
44 uint64_t GetInstanceSize() override;
46 ObjCLanguageRuntime::ObjCISA GetISA() override { return m_objc_class_ptr; }
49 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
50 std::function<bool(const char *, const char *)> const
51 &instance_method_func,
52 std::function<bool(const char *, const char *)> const &class_method_func,
53 std::function<bool(const char *, const char *, lldb::addr_t,
54 uint64_t)> const &ivar_func) const override;
56 size_t GetNumIVars() override {
58 return m_ivars_storage.size();
61 iVarDescriptor GetIVarAtIndex(size_t idx) override {
62 if (idx >= GetNumIVars())
63 return iVarDescriptor();
64 return m_ivars_storage[idx];
68 void GetIVarInformation();
71 static const uint32_t RW_REALIZED = (1 << 31);
74 ObjCLanguageRuntime::ObjCISA m_isa; // The class's metaclass.
75 ObjCLanguageRuntime::ObjCISA m_superclass;
76 lldb::addr_t m_cache_ptr;
77 lldb::addr_t m_vtable_ptr;
78 lldb::addr_t m_data_ptr;
82 : m_isa(0), m_superclass(0), m_cache_ptr(0), m_vtable_ptr(0),
83 m_data_ptr(0), m_flags(0) {}
94 bool Read(Process *process, lldb::addr_t addr);
99 uint32_t m_instanceStart;
100 uint32_t m_instanceSize;
103 lldb::addr_t m_ivarLayout_ptr;
104 lldb::addr_t m_name_ptr;
105 lldb::addr_t m_baseMethods_ptr;
106 lldb::addr_t m_baseProtocols_ptr;
107 lldb::addr_t m_ivars_ptr;
109 lldb::addr_t m_weakIvarLayout_ptr;
110 lldb::addr_t m_baseProperties_ptr;
114 bool Read(Process *process, lldb::addr_t addr);
121 lldb::addr_t m_ro_ptr;
123 lldb::addr_t m_method_list_ptr;
124 lldb::addr_t m_method_lists_ptr;
126 lldb::addr_t m_properties_ptr;
127 lldb::addr_t m_protocols_ptr;
129 ObjCLanguageRuntime::ObjCISA m_firstSubclass;
130 ObjCLanguageRuntime::ObjCISA m_nextSiblingClass;
132 bool Read(Process *process, lldb::addr_t addr);
135 struct method_list_t {
138 bool m_has_direct_selector;
140 lldb::addr_t m_first_ptr;
142 bool Read(Process *process, lldb::addr_t addr);
146 lldb::addr_t m_name_ptr;
147 lldb::addr_t m_types_ptr;
148 lldb::addr_t m_imp_ptr;
153 static size_t GetSize(Process *process, bool is_small) {
156 field_size = 4; // uint32_t relative indirect fields
158 field_size = process->GetAddressByteSize();
160 return field_size // SEL name;
161 + field_size // const char *types;
162 + field_size; // IMP imp;
165 bool Read(Process *process, lldb::addr_t addr, bool, bool);
171 lldb::addr_t m_first_ptr;
173 bool Read(Process *process, lldb::addr_t addr);
177 lldb::addr_t m_offset_ptr;
178 lldb::addr_t m_name_ptr;
179 lldb::addr_t m_type_ptr;
180 uint32_t m_alignment;
186 static size_t GetSize(Process *process) {
187 size_t ptr_size = process->GetAddressByteSize();
189 return ptr_size // uintptr_t *offset;
190 + ptr_size // const char *name;
191 + ptr_size // const char *type;
192 + sizeof(uint32_t) // uint32_t alignment;
193 + sizeof(uint32_t); // uint32_t size;
196 bool Read(Process *process, lldb::addr_t addr);
205 iVarDescriptor &operator[](size_t idx);
207 void fill(AppleObjCRuntimeV2 &runtime, ClassDescriptorV2 &descriptor);
211 std::vector<iVarDescriptor> m_ivars;
212 std::recursive_mutex m_mutex;
215 // The constructor should only be invoked by the runtime as it builds its
217 // or populates them. A ClassDescriptorV2 should only ever exist in a cache.
218 ClassDescriptorV2(AppleObjCRuntimeV2 &runtime,
219 ObjCLanguageRuntime::ObjCISA isa, const char *name)
220 : m_runtime(runtime), m_objc_class_ptr(isa), m_name(name),
223 bool Read_objc_class(Process *process,
224 std::unique_ptr<objc_class_t> &objc_class) const;
226 bool Read_class_row(Process *process, const objc_class_t &objc_class,
227 std::unique_ptr<class_ro_t> &class_ro,
228 std::unique_ptr<class_rw_t> &class_rw) const;
231 &m_runtime; // The runtime, so we can read information lazily.
232 lldb::addr_t m_objc_class_ptr; // The address of the objc_class_t. (I.e.,
233 // objects of this class type have this as
235 ConstString m_name; // May be NULL
236 iVarsStorage m_ivars_storage;
239 // tagged pointer descriptor
240 class ClassDescriptorV2Tagged : public ObjCLanguageRuntime::ClassDescriptor {
242 ClassDescriptorV2Tagged(ConstString class_name, uint64_t payload) {
250 m_info_bits = (m_payload & 0xF0ULL) >> 4;
251 m_value_bits = (m_payload & ~0x0000000000000000FFULL) >> 8;
254 ClassDescriptorV2Tagged(
255 ObjCLanguageRuntime::ClassDescriptorSP actual_class_sp,
257 if (!actual_class_sp) {
261 m_name = actual_class_sp->GetClassName();
268 m_info_bits = (m_payload & 0x0FULL);
269 m_value_bits = (m_payload & ~0x0FULL) >> 4;
272 ~ClassDescriptorV2Tagged() override = default;
274 ConstString GetClassName() override { return m_name; }
276 ObjCLanguageRuntime::ClassDescriptorSP GetSuperclass() override {
277 // tagged pointers can represent a class that has a superclass, but since
278 // that information is not
279 // stored in the object itself, we would have to query the runtime to
280 // discover the hierarchy
281 // for the time being, we skip this step in the interest of static discovery
282 return ObjCLanguageRuntime::ClassDescriptorSP();
285 ObjCLanguageRuntime::ClassDescriptorSP GetMetaclass() const override {
286 return ObjCLanguageRuntime::ClassDescriptorSP();
289 bool IsValid() override { return m_valid; }
291 bool IsKVO() override {
292 return false; // tagged pointers are not KVO'ed
295 bool IsCFType() override {
296 return false; // tagged pointers are not CF objects
299 bool GetTaggedPointerInfo(uint64_t *info_bits = nullptr,
300 uint64_t *value_bits = nullptr,
301 uint64_t *payload = nullptr) override {
303 *info_bits = GetInfoBits();
305 *value_bits = GetValueBits();
307 *payload = GetPayload();
311 uint64_t GetInstanceSize() override {
312 return (IsValid() ? m_pointer_size : 0);
315 ObjCLanguageRuntime::ObjCISA GetISA() override {
316 return 0; // tagged pointers have no ISA
319 // these calls are not part of any formal tagged pointers specification
320 virtual uint64_t GetValueBits() { return (IsValid() ? m_value_bits : 0); }
322 virtual uint64_t GetInfoBits() { return (IsValid() ? m_info_bits : 0); }
324 virtual uint64_t GetPayload() { return (IsValid() ? m_payload : 0); }
328 uint8_t m_pointer_size;
330 uint64_t m_info_bits;
331 uint64_t m_value_bits;
335 } // namespace lldb_private
337 #endif // LLDB_SOURCE_PLUGINS_LANGUAGERUNTIME_OBJC_APPLEOBJCRUNTIME_APPLEOBJCCLASSDESCRIPTORV2_H