1 //===-- AppleObjCClassDescriptorV2.cpp -----------------------------*- C++
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "AppleObjCClassDescriptorV2.h"
12 #include "lldb/Expression/FunctionCaller.h"
13 #include "lldb/Utility/Log.h"
16 using namespace lldb_private;
18 bool ClassDescriptorV2::Read_objc_class(
19 Process *process, std::unique_ptr<objc_class_t> &objc_class) const {
20 objc_class.reset(new objc_class_t);
22 bool ret = objc_class->Read(process, m_objc_class_ptr);
30 static lldb::addr_t GetClassDataMask(Process *process) {
31 switch (process->GetAddressByteSize()) {
35 return 0x00007ffffffffff8UL;
40 return LLDB_INVALID_ADDRESS;
43 bool ClassDescriptorV2::objc_class_t::Read(Process *process,
45 size_t ptr_size = process->GetAddressByteSize();
47 size_t objc_class_size = ptr_size // uintptr_t isa;
48 + ptr_size // Class superclass;
49 + ptr_size // void *cache;
50 + ptr_size // IMP *vtable;
51 + ptr_size; // uintptr_t data_NEVER_USE;
53 DataBufferHeap objc_class_buf(objc_class_size, '\0');
56 process->ReadMemory(addr, objc_class_buf.GetBytes(), objc_class_size, error);
61 DataExtractor extractor(objc_class_buf.GetBytes(), objc_class_size,
62 process->GetByteOrder(),
63 process->GetAddressByteSize());
65 lldb::offset_t cursor = 0;
67 m_isa = extractor.GetAddress_unchecked(&cursor); // uintptr_t isa;
68 m_superclass = extractor.GetAddress_unchecked(&cursor); // Class superclass;
69 m_cache_ptr = extractor.GetAddress_unchecked(&cursor); // void *cache;
70 m_vtable_ptr = extractor.GetAddress_unchecked(&cursor); // IMP *vtable;
71 lldb::addr_t data_NEVER_USE =
72 extractor.GetAddress_unchecked(&cursor); // uintptr_t data_NEVER_USE;
74 m_flags = (uint8_t)(data_NEVER_USE & (lldb::addr_t)3);
75 m_data_ptr = data_NEVER_USE & GetClassDataMask(process);
80 bool ClassDescriptorV2::class_rw_t::Read(Process *process, lldb::addr_t addr) {
81 size_t ptr_size = process->GetAddressByteSize();
83 size_t size = sizeof(uint32_t) // uint32_t flags;
84 + sizeof(uint32_t) // uint32_t version;
85 + ptr_size // const class_ro_t *ro;
86 + ptr_size // union { method_list_t **method_lists;
87 // method_list_t *method_list; };
88 + ptr_size // struct chained_property_list *properties;
89 + ptr_size // const protocol_list_t **protocols;
90 + ptr_size // Class firstSubclass;
91 + ptr_size; // Class nextSiblingClass;
93 DataBufferHeap buffer(size, '\0');
96 process->ReadMemory(addr, buffer.GetBytes(), size, error);
101 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
102 process->GetAddressByteSize());
104 lldb::offset_t cursor = 0;
106 m_flags = extractor.GetU32_unchecked(&cursor);
107 m_version = extractor.GetU32_unchecked(&cursor);
108 m_ro_ptr = extractor.GetAddress_unchecked(&cursor);
109 m_method_list_ptr = extractor.GetAddress_unchecked(&cursor);
110 m_properties_ptr = extractor.GetAddress_unchecked(&cursor);
111 m_firstSubclass = extractor.GetAddress_unchecked(&cursor);
112 m_nextSiblingClass = extractor.GetAddress_unchecked(&cursor);
117 bool ClassDescriptorV2::class_ro_t::Read(Process *process, lldb::addr_t addr) {
118 size_t ptr_size = process->GetAddressByteSize();
120 size_t size = sizeof(uint32_t) // uint32_t flags;
121 + sizeof(uint32_t) // uint32_t instanceStart;
122 + sizeof(uint32_t) // uint32_t instanceSize;
123 + (ptr_size == 8 ? sizeof(uint32_t)
124 : 0) // uint32_t reserved; // __LP64__ only
125 + ptr_size // const uint8_t *ivarLayout;
126 + ptr_size // const char *name;
127 + ptr_size // const method_list_t *baseMethods;
128 + ptr_size // const protocol_list_t *baseProtocols;
129 + ptr_size // const ivar_list_t *ivars;
130 + ptr_size // const uint8_t *weakIvarLayout;
131 + ptr_size; // const property_list_t *baseProperties;
133 DataBufferHeap buffer(size, '\0');
136 process->ReadMemory(addr, buffer.GetBytes(), size, error);
141 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
142 process->GetAddressByteSize());
144 lldb::offset_t cursor = 0;
146 m_flags = extractor.GetU32_unchecked(&cursor);
147 m_instanceStart = extractor.GetU32_unchecked(&cursor);
148 m_instanceSize = extractor.GetU32_unchecked(&cursor);
150 m_reserved = extractor.GetU32_unchecked(&cursor);
153 m_ivarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
154 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
155 m_baseMethods_ptr = extractor.GetAddress_unchecked(&cursor);
156 m_baseProtocols_ptr = extractor.GetAddress_unchecked(&cursor);
157 m_ivars_ptr = extractor.GetAddress_unchecked(&cursor);
158 m_weakIvarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
159 m_baseProperties_ptr = extractor.GetAddress_unchecked(&cursor);
161 DataBufferHeap name_buf(1024, '\0');
163 process->ReadCStringFromMemory(m_name_ptr, (char *)name_buf.GetBytes(),
164 name_buf.GetByteSize(), error);
170 m_name.assign((char *)name_buf.GetBytes());
175 bool ClassDescriptorV2::Read_class_row(
176 Process *process, const objc_class_t &objc_class,
177 std::unique_ptr<class_ro_t> &class_ro,
178 std::unique_ptr<class_rw_t> &class_rw) const {
183 uint32_t class_row_t_flags = process->ReadUnsignedIntegerFromMemory(
184 objc_class.m_data_ptr, sizeof(uint32_t), 0, error);
185 if (!error.Success())
188 if (class_row_t_flags & RW_REALIZED) {
189 class_rw.reset(new class_rw_t);
191 if (!class_rw->Read(process, objc_class.m_data_ptr)) {
196 class_ro.reset(new class_ro_t);
198 if (!class_ro->Read(process, class_rw->m_ro_ptr)) {
204 class_ro.reset(new class_ro_t);
206 if (!class_ro->Read(process, objc_class.m_data_ptr)) {
215 bool ClassDescriptorV2::method_list_t::Read(Process *process,
217 size_t size = sizeof(uint32_t) // uint32_t entsize_NEVER_USE;
218 + sizeof(uint32_t); // uint32_t count;
220 DataBufferHeap buffer(size, '\0');
223 process->ReadMemory(addr, buffer.GetBytes(), size, error);
228 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
229 process->GetAddressByteSize());
231 lldb::offset_t cursor = 0;
233 m_entsize = extractor.GetU32_unchecked(&cursor) & ~(uint32_t)3;
234 m_count = extractor.GetU32_unchecked(&cursor);
235 m_first_ptr = addr + cursor;
240 bool ClassDescriptorV2::method_t::Read(Process *process, lldb::addr_t addr) {
241 size_t size = GetSize(process);
243 DataBufferHeap buffer(size, '\0');
246 process->ReadMemory(addr, buffer.GetBytes(), size, error);
251 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
252 process->GetAddressByteSize());
254 lldb::offset_t cursor = 0;
256 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
257 m_types_ptr = extractor.GetAddress_unchecked(&cursor);
258 m_imp_ptr = extractor.GetAddress_unchecked(&cursor);
260 process->ReadCStringFromMemory(m_name_ptr, m_name, error);
265 process->ReadCStringFromMemory(m_types_ptr, m_types, error);
266 return !error.Fail();
269 bool ClassDescriptorV2::ivar_list_t::Read(Process *process, lldb::addr_t addr) {
270 size_t size = sizeof(uint32_t) // uint32_t entsize;
271 + sizeof(uint32_t); // uint32_t count;
273 DataBufferHeap buffer(size, '\0');
276 process->ReadMemory(addr, buffer.GetBytes(), size, error);
281 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
282 process->GetAddressByteSize());
284 lldb::offset_t cursor = 0;
286 m_entsize = extractor.GetU32_unchecked(&cursor);
287 m_count = extractor.GetU32_unchecked(&cursor);
288 m_first_ptr = addr + cursor;
293 bool ClassDescriptorV2::ivar_t::Read(Process *process, lldb::addr_t addr) {
294 size_t size = GetSize(process);
296 DataBufferHeap buffer(size, '\0');
299 process->ReadMemory(addr, buffer.GetBytes(), size, error);
304 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
305 process->GetAddressByteSize());
307 lldb::offset_t cursor = 0;
309 m_offset_ptr = extractor.GetAddress_unchecked(&cursor);
310 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
311 m_type_ptr = extractor.GetAddress_unchecked(&cursor);
312 m_alignment = extractor.GetU32_unchecked(&cursor);
313 m_size = extractor.GetU32_unchecked(&cursor);
315 process->ReadCStringFromMemory(m_name_ptr, m_name, error);
320 process->ReadCStringFromMemory(m_type_ptr, m_type, error);
321 return !error.Fail();
324 bool ClassDescriptorV2::Describe(
325 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
326 std::function<bool(const char *, const char *)> const &instance_method_func,
327 std::function<bool(const char *, const char *)> const &class_method_func,
328 std::function<bool(const char *, const char *, lldb::addr_t,
329 uint64_t)> const &ivar_func) const {
330 lldb_private::Process *process = m_runtime.GetProcess();
332 std::unique_ptr<objc_class_t> objc_class;
333 std::unique_ptr<class_ro_t> class_ro;
334 std::unique_ptr<class_rw_t> class_rw;
336 if (!Read_objc_class(process, objc_class))
338 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
341 static ConstString NSObject_name("NSObject");
343 if (m_name != NSObject_name && superclass_func)
344 superclass_func(objc_class->m_superclass);
346 if (instance_method_func) {
347 std::unique_ptr<method_list_t> base_method_list;
349 base_method_list.reset(new method_list_t);
350 if (!base_method_list->Read(process, class_ro->m_baseMethods_ptr))
353 if (base_method_list->m_entsize != method_t::GetSize(process))
356 std::unique_ptr<method_t> method;
357 method.reset(new method_t);
359 for (uint32_t i = 0, e = base_method_list->m_count; i < e; ++i) {
360 method->Read(process, base_method_list->m_first_ptr +
361 (i * base_method_list->m_entsize));
363 if (instance_method_func(method->m_name.c_str(), method->m_types.c_str()))
368 if (class_method_func) {
369 AppleObjCRuntime::ClassDescriptorSP metaclass(GetMetaclass());
371 // We don't care about the metaclass's superclass, or its class methods.
372 // Its instance methods are our class methods.
376 std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
378 std::function<bool(const char *, const char *)>(nullptr),
379 std::function<bool(const char *, const char *, lldb::addr_t,
380 uint64_t)>(nullptr));
385 if (class_ro->m_ivars_ptr != 0) {
386 ivar_list_t ivar_list;
387 if (!ivar_list.Read(process, class_ro->m_ivars_ptr))
390 if (ivar_list.m_entsize != ivar_t::GetSize(process))
395 for (uint32_t i = 0, e = ivar_list.m_count; i < e; ++i) {
396 ivar.Read(process, ivar_list.m_first_ptr + (i * ivar_list.m_entsize));
398 if (ivar_func(ivar.m_name.c_str(), ivar.m_type.c_str(),
399 ivar.m_offset_ptr, ivar.m_size))
408 ConstString ClassDescriptorV2::GetClassName() {
410 lldb_private::Process *process = m_runtime.GetProcess();
413 std::unique_ptr<objc_class_t> objc_class;
414 std::unique_ptr<class_ro_t> class_ro;
415 std::unique_ptr<class_rw_t> class_rw;
417 if (!Read_objc_class(process, objc_class))
419 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
422 m_name = ConstString(class_ro->m_name.c_str());
428 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetSuperclass() {
429 lldb_private::Process *process = m_runtime.GetProcess();
432 return ObjCLanguageRuntime::ClassDescriptorSP();
434 std::unique_ptr<objc_class_t> objc_class;
436 if (!Read_objc_class(process, objc_class))
437 return ObjCLanguageRuntime::ClassDescriptorSP();
439 return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
440 objc_class->m_superclass);
443 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetMetaclass() const {
444 lldb_private::Process *process = m_runtime.GetProcess();
447 return ObjCLanguageRuntime::ClassDescriptorSP();
449 std::unique_ptr<objc_class_t> objc_class;
451 if (!Read_objc_class(process, objc_class))
452 return ObjCLanguageRuntime::ClassDescriptorSP();
454 lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
456 return ObjCLanguageRuntime::ClassDescriptorSP(
457 new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
460 uint64_t ClassDescriptorV2::GetInstanceSize() {
461 lldb_private::Process *process = m_runtime.GetProcess();
464 std::unique_ptr<objc_class_t> objc_class;
465 std::unique_ptr<class_ro_t> class_ro;
466 std::unique_ptr<class_rw_t> class_rw;
468 if (!Read_objc_class(process, objc_class))
470 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
473 return class_ro->m_instanceSize;
479 ClassDescriptorV2::iVarsStorage::iVarsStorage()
480 : m_filled(false), m_ivars(), m_mutex() {}
482 size_t ClassDescriptorV2::iVarsStorage::size() { return m_ivars.size(); }
484 ClassDescriptorV2::iVarDescriptor &ClassDescriptorV2::iVarsStorage::
485 operator[](size_t idx) {
489 void ClassDescriptorV2::iVarsStorage::fill(AppleObjCRuntimeV2 &runtime,
490 ClassDescriptorV2 &descriptor) {
493 std::lock_guard<std::recursive_mutex> guard(m_mutex);
494 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
495 LLDB_LOGV(log, "class_name = {0}", descriptor.GetClassName());
497 ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
498 runtime.GetEncodingToType());
499 Process *process(runtime.GetProcess());
500 if (!encoding_to_type_sp)
502 descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
504 log](const char *name,
506 lldb::addr_t offset_ptr,
507 uint64_t size) -> bool {
508 const bool for_expression = false;
509 const bool stop_loop = false;
510 LLDB_LOGV(log, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = {3}",
511 name, type, offset_ptr, size);
512 CompilerType ivar_type =
513 encoding_to_type_sp->RealizeType(type, for_expression);
516 "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = "
517 "{3}, type_size = {4}",
518 name, type, offset_ptr, size,
519 ivar_type.GetByteSize(nullptr).getValueOr(0));
520 Scalar offset_scalar;
522 const int offset_ptr_size = 4;
523 const bool is_signed = false;
524 size_t read = process->ReadScalarIntegerFromMemory(
525 offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
526 if (error.Success() && 4 == read) {
527 LLDB_LOGV(log, "offset_ptr = {0:x} --> {1}", offset_ptr,
528 offset_scalar.SInt());
530 {ConstString(name), ivar_type, size, offset_scalar.SInt()});
532 LLDB_LOGV(log, "offset_ptr = {0:x} --> read fail, read = %{1}",
539 void ClassDescriptorV2::GetIVarInformation() {
540 m_ivars_storage.fill(m_runtime, *this);