1 //===-- AppleObjCClassDescriptorV2.cpp -----------------------------*- C++
2 //-*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #include "AppleObjCClassDescriptorV2.h"
12
13 #include "lldb/Expression/FunctionCaller.h"
14 #include "lldb/Utility/Log.h"
15
16 using namespace lldb;
17 using namespace lldb_private;
18
Read_objc_class(Process * process,std::unique_ptr<objc_class_t> & objc_class) const19 bool ClassDescriptorV2::Read_objc_class(
20 Process *process, std::unique_ptr<objc_class_t> &objc_class) const {
21 objc_class.reset(new objc_class_t);
22
23 bool ret = objc_class->Read(process, m_objc_class_ptr);
24
25 if (!ret)
26 objc_class.reset();
27
28 return ret;
29 }
30
GetClassDataMask(Process * process)31 static lldb::addr_t GetClassDataMask(Process *process) {
32 switch (process->GetAddressByteSize()) {
33 case 4:
34 return 0xfffffffcUL;
35 case 8:
36 return 0x00007ffffffffff8UL;
37 default:
38 break;
39 }
40
41 return LLDB_INVALID_ADDRESS;
42 }
43
Read(Process * process,lldb::addr_t addr)44 bool ClassDescriptorV2::objc_class_t::Read(Process *process,
45 lldb::addr_t addr) {
46 size_t ptr_size = process->GetAddressByteSize();
47
48 size_t objc_class_size = ptr_size // uintptr_t isa;
49 + ptr_size // Class superclass;
50 + ptr_size // void *cache;
51 + ptr_size // IMP *vtable;
52 + ptr_size; // uintptr_t data_NEVER_USE;
53
54 DataBufferHeap objc_class_buf(objc_class_size, '\0');
55 Status error;
56
57 process->ReadMemory(addr, objc_class_buf.GetBytes(), objc_class_size, error);
58 if (error.Fail()) {
59 return false;
60 }
61
62 DataExtractor extractor(objc_class_buf.GetBytes(), objc_class_size,
63 process->GetByteOrder(),
64 process->GetAddressByteSize());
65
66 lldb::offset_t cursor = 0;
67
68 m_isa = extractor.GetAddress_unchecked(&cursor); // uintptr_t isa;
69 m_superclass = extractor.GetAddress_unchecked(&cursor); // Class superclass;
70 m_cache_ptr = extractor.GetAddress_unchecked(&cursor); // void *cache;
71 m_vtable_ptr = extractor.GetAddress_unchecked(&cursor); // IMP *vtable;
72 lldb::addr_t data_NEVER_USE =
73 extractor.GetAddress_unchecked(&cursor); // uintptr_t data_NEVER_USE;
74
75 m_flags = (uint8_t)(data_NEVER_USE & (lldb::addr_t)3);
76 m_data_ptr = data_NEVER_USE & GetClassDataMask(process);
77
78 return true;
79 }
80
Read(Process * process,lldb::addr_t addr)81 bool ClassDescriptorV2::class_rw_t::Read(Process *process, lldb::addr_t addr) {
82 size_t ptr_size = process->GetAddressByteSize();
83
84 size_t size = sizeof(uint32_t) // uint32_t flags;
85 + sizeof(uint32_t) // uint32_t version;
86 + ptr_size // const class_ro_t *ro;
87 + ptr_size // union { method_list_t **method_lists;
88 // method_list_t *method_list; };
89 + ptr_size // struct chained_property_list *properties;
90 + ptr_size // const protocol_list_t **protocols;
91 + ptr_size // Class firstSubclass;
92 + ptr_size; // Class nextSiblingClass;
93
94 DataBufferHeap buffer(size, '\0');
95 Status error;
96
97 process->ReadMemory(addr, buffer.GetBytes(), size, error);
98 if (error.Fail()) {
99 return false;
100 }
101
102 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
103 process->GetAddressByteSize());
104
105 lldb::offset_t cursor = 0;
106
107 m_flags = extractor.GetU32_unchecked(&cursor);
108 m_version = extractor.GetU32_unchecked(&cursor);
109 m_ro_ptr = extractor.GetAddress_unchecked(&cursor);
110 m_method_list_ptr = extractor.GetAddress_unchecked(&cursor);
111 m_properties_ptr = extractor.GetAddress_unchecked(&cursor);
112 m_firstSubclass = extractor.GetAddress_unchecked(&cursor);
113 m_nextSiblingClass = extractor.GetAddress_unchecked(&cursor);
114
115 return true;
116 }
117
Read(Process * process,lldb::addr_t addr)118 bool ClassDescriptorV2::class_ro_t::Read(Process *process, lldb::addr_t addr) {
119 size_t ptr_size = process->GetAddressByteSize();
120
121 size_t size = sizeof(uint32_t) // uint32_t flags;
122 + sizeof(uint32_t) // uint32_t instanceStart;
123 + sizeof(uint32_t) // uint32_t instanceSize;
124 + (ptr_size == 8 ? sizeof(uint32_t)
125 : 0) // uint32_t reserved; // __LP64__ only
126 + ptr_size // const uint8_t *ivarLayout;
127 + ptr_size // const char *name;
128 + ptr_size // const method_list_t *baseMethods;
129 + ptr_size // const protocol_list_t *baseProtocols;
130 + ptr_size // const ivar_list_t *ivars;
131 + ptr_size // const uint8_t *weakIvarLayout;
132 + ptr_size; // const property_list_t *baseProperties;
133
134 DataBufferHeap buffer(size, '\0');
135 Status error;
136
137 process->ReadMemory(addr, buffer.GetBytes(), size, error);
138 if (error.Fail()) {
139 return false;
140 }
141
142 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
143 process->GetAddressByteSize());
144
145 lldb::offset_t cursor = 0;
146
147 m_flags = extractor.GetU32_unchecked(&cursor);
148 m_instanceStart = extractor.GetU32_unchecked(&cursor);
149 m_instanceSize = extractor.GetU32_unchecked(&cursor);
150 if (ptr_size == 8)
151 m_reserved = extractor.GetU32_unchecked(&cursor);
152 else
153 m_reserved = 0;
154 m_ivarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
155 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
156 m_baseMethods_ptr = extractor.GetAddress_unchecked(&cursor);
157 m_baseProtocols_ptr = extractor.GetAddress_unchecked(&cursor);
158 m_ivars_ptr = extractor.GetAddress_unchecked(&cursor);
159 m_weakIvarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
160 m_baseProperties_ptr = extractor.GetAddress_unchecked(&cursor);
161
162 DataBufferHeap name_buf(1024, '\0');
163
164 process->ReadCStringFromMemory(m_name_ptr, (char *)name_buf.GetBytes(),
165 name_buf.GetByteSize(), error);
166
167 if (error.Fail()) {
168 return false;
169 }
170
171 m_name.assign((char *)name_buf.GetBytes());
172
173 return true;
174 }
175
Read_class_row(Process * process,const objc_class_t & objc_class,std::unique_ptr<class_ro_t> & class_ro,std::unique_ptr<class_rw_t> & class_rw) const176 bool ClassDescriptorV2::Read_class_row(
177 Process *process, const objc_class_t &objc_class,
178 std::unique_ptr<class_ro_t> &class_ro,
179 std::unique_ptr<class_rw_t> &class_rw) const {
180 class_ro.reset();
181 class_rw.reset();
182
183 Status error;
184 uint32_t class_row_t_flags = process->ReadUnsignedIntegerFromMemory(
185 objc_class.m_data_ptr, sizeof(uint32_t), 0, error);
186 if (!error.Success())
187 return false;
188
189 if (class_row_t_flags & RW_REALIZED) {
190 class_rw.reset(new class_rw_t);
191
192 if (!class_rw->Read(process, objc_class.m_data_ptr)) {
193 class_rw.reset();
194 return false;
195 }
196
197 class_ro.reset(new class_ro_t);
198
199 if (!class_ro->Read(process, class_rw->m_ro_ptr)) {
200 class_rw.reset();
201 class_ro.reset();
202 return false;
203 }
204 } else {
205 class_ro.reset(new class_ro_t);
206
207 if (!class_ro->Read(process, objc_class.m_data_ptr)) {
208 class_ro.reset();
209 return false;
210 }
211 }
212
213 return true;
214 }
215
Read(Process * process,lldb::addr_t addr)216 bool ClassDescriptorV2::method_list_t::Read(Process *process,
217 lldb::addr_t addr) {
218 size_t size = sizeof(uint32_t) // uint32_t entsize_NEVER_USE;
219 + sizeof(uint32_t); // uint32_t count;
220
221 DataBufferHeap buffer(size, '\0');
222 Status error;
223
224 process->ReadMemory(addr, buffer.GetBytes(), size, error);
225 if (error.Fail()) {
226 return false;
227 }
228
229 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
230 process->GetAddressByteSize());
231
232 lldb::offset_t cursor = 0;
233
234 m_entsize = extractor.GetU32_unchecked(&cursor) & ~(uint32_t)3;
235 m_count = extractor.GetU32_unchecked(&cursor);
236 m_first_ptr = addr + cursor;
237
238 return true;
239 }
240
Read(Process * process,lldb::addr_t addr)241 bool ClassDescriptorV2::method_t::Read(Process *process, lldb::addr_t addr) {
242 size_t size = GetSize(process);
243
244 DataBufferHeap buffer(size, '\0');
245 Status error;
246
247 process->ReadMemory(addr, buffer.GetBytes(), size, error);
248 if (error.Fail()) {
249 return false;
250 }
251
252 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
253 process->GetAddressByteSize());
254
255 lldb::offset_t cursor = 0;
256
257 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
258 m_types_ptr = extractor.GetAddress_unchecked(&cursor);
259 m_imp_ptr = extractor.GetAddress_unchecked(&cursor);
260
261 process->ReadCStringFromMemory(m_name_ptr, m_name, error);
262 if (error.Fail()) {
263 return false;
264 }
265
266 process->ReadCStringFromMemory(m_types_ptr, m_types, error);
267 return !error.Fail();
268 }
269
Read(Process * process,lldb::addr_t addr)270 bool ClassDescriptorV2::ivar_list_t::Read(Process *process, lldb::addr_t addr) {
271 size_t size = sizeof(uint32_t) // uint32_t entsize;
272 + sizeof(uint32_t); // uint32_t count;
273
274 DataBufferHeap buffer(size, '\0');
275 Status error;
276
277 process->ReadMemory(addr, buffer.GetBytes(), size, error);
278 if (error.Fail()) {
279 return false;
280 }
281
282 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
283 process->GetAddressByteSize());
284
285 lldb::offset_t cursor = 0;
286
287 m_entsize = extractor.GetU32_unchecked(&cursor);
288 m_count = extractor.GetU32_unchecked(&cursor);
289 m_first_ptr = addr + cursor;
290
291 return true;
292 }
293
Read(Process * process,lldb::addr_t addr)294 bool ClassDescriptorV2::ivar_t::Read(Process *process, lldb::addr_t addr) {
295 size_t size = GetSize(process);
296
297 DataBufferHeap buffer(size, '\0');
298 Status error;
299
300 process->ReadMemory(addr, buffer.GetBytes(), size, error);
301 if (error.Fail()) {
302 return false;
303 }
304
305 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
306 process->GetAddressByteSize());
307
308 lldb::offset_t cursor = 0;
309
310 m_offset_ptr = extractor.GetAddress_unchecked(&cursor);
311 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
312 m_type_ptr = extractor.GetAddress_unchecked(&cursor);
313 m_alignment = extractor.GetU32_unchecked(&cursor);
314 m_size = extractor.GetU32_unchecked(&cursor);
315
316 process->ReadCStringFromMemory(m_name_ptr, m_name, error);
317 if (error.Fail()) {
318 return false;
319 }
320
321 process->ReadCStringFromMemory(m_type_ptr, m_type, error);
322 return !error.Fail();
323 }
324
Describe(std::function<void (ObjCLanguageRuntime::ObjCISA)> const & superclass_func,std::function<bool (const char *,const char *)> const & instance_method_func,std::function<bool (const char *,const char *)> const & class_method_func,std::function<bool (const char *,const char *,lldb::addr_t,uint64_t)> const & ivar_func) const325 bool ClassDescriptorV2::Describe(
326 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
327 std::function<bool(const char *, const char *)> const &instance_method_func,
328 std::function<bool(const char *, const char *)> const &class_method_func,
329 std::function<bool(const char *, const char *, lldb::addr_t,
330 uint64_t)> const &ivar_func) const {
331 lldb_private::Process *process = m_runtime.GetProcess();
332
333 std::unique_ptr<objc_class_t> objc_class;
334 std::unique_ptr<class_ro_t> class_ro;
335 std::unique_ptr<class_rw_t> class_rw;
336
337 if (!Read_objc_class(process, objc_class))
338 return 0;
339 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
340 return 0;
341
342 static ConstString NSObject_name("NSObject");
343
344 if (m_name != NSObject_name && superclass_func)
345 superclass_func(objc_class->m_superclass);
346
347 if (instance_method_func) {
348 std::unique_ptr<method_list_t> base_method_list;
349
350 base_method_list.reset(new method_list_t);
351 if (!base_method_list->Read(process, class_ro->m_baseMethods_ptr))
352 return false;
353
354 if (base_method_list->m_entsize != method_t::GetSize(process))
355 return false;
356
357 std::unique_ptr<method_t> method;
358 method.reset(new method_t);
359
360 for (uint32_t i = 0, e = base_method_list->m_count; i < e; ++i) {
361 method->Read(process, base_method_list->m_first_ptr +
362 (i * base_method_list->m_entsize));
363
364 if (instance_method_func(method->m_name.c_str(), method->m_types.c_str()))
365 break;
366 }
367 }
368
369 if (class_method_func) {
370 AppleObjCRuntime::ClassDescriptorSP metaclass(GetMetaclass());
371
372 // We don't care about the metaclass's superclass, or its class methods.
373 // Its instance methods are our class methods.
374
375 if (metaclass) {
376 metaclass->Describe(
377 std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
378 class_method_func,
379 std::function<bool(const char *, const char *)>(nullptr),
380 std::function<bool(const char *, const char *, lldb::addr_t,
381 uint64_t)>(nullptr));
382 }
383 }
384
385 if (ivar_func) {
386 if (class_ro->m_ivars_ptr != 0) {
387 ivar_list_t ivar_list;
388 if (!ivar_list.Read(process, class_ro->m_ivars_ptr))
389 return false;
390
391 if (ivar_list.m_entsize != ivar_t::GetSize(process))
392 return false;
393
394 ivar_t ivar;
395
396 for (uint32_t i = 0, e = ivar_list.m_count; i < e; ++i) {
397 ivar.Read(process, ivar_list.m_first_ptr + (i * ivar_list.m_entsize));
398
399 if (ivar_func(ivar.m_name.c_str(), ivar.m_type.c_str(),
400 ivar.m_offset_ptr, ivar.m_size))
401 break;
402 }
403 }
404 }
405
406 return true;
407 }
408
GetClassName()409 ConstString ClassDescriptorV2::GetClassName() {
410 if (!m_name) {
411 lldb_private::Process *process = m_runtime.GetProcess();
412
413 if (process) {
414 std::unique_ptr<objc_class_t> objc_class;
415 std::unique_ptr<class_ro_t> class_ro;
416 std::unique_ptr<class_rw_t> class_rw;
417
418 if (!Read_objc_class(process, objc_class))
419 return m_name;
420 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
421 return m_name;
422
423 m_name = ConstString(class_ro->m_name.c_str());
424 }
425 }
426 return m_name;
427 }
428
GetSuperclass()429 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetSuperclass() {
430 lldb_private::Process *process = m_runtime.GetProcess();
431
432 if (!process)
433 return ObjCLanguageRuntime::ClassDescriptorSP();
434
435 std::unique_ptr<objc_class_t> objc_class;
436
437 if (!Read_objc_class(process, objc_class))
438 return ObjCLanguageRuntime::ClassDescriptorSP();
439
440 return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
441 objc_class->m_superclass);
442 }
443
GetMetaclass() const444 ObjCLanguageRuntime::ClassDescriptorSP ClassDescriptorV2::GetMetaclass() const {
445 lldb_private::Process *process = m_runtime.GetProcess();
446
447 if (!process)
448 return ObjCLanguageRuntime::ClassDescriptorSP();
449
450 std::unique_ptr<objc_class_t> objc_class;
451
452 if (!Read_objc_class(process, objc_class))
453 return ObjCLanguageRuntime::ClassDescriptorSP();
454
455 lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
456
457 return ObjCLanguageRuntime::ClassDescriptorSP(
458 new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
459 }
460
GetInstanceSize()461 uint64_t ClassDescriptorV2::GetInstanceSize() {
462 lldb_private::Process *process = m_runtime.GetProcess();
463
464 if (process) {
465 std::unique_ptr<objc_class_t> objc_class;
466 std::unique_ptr<class_ro_t> class_ro;
467 std::unique_ptr<class_rw_t> class_rw;
468
469 if (!Read_objc_class(process, objc_class))
470 return 0;
471 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
472 return 0;
473
474 return class_ro->m_instanceSize;
475 }
476
477 return 0;
478 }
479
iVarsStorage()480 ClassDescriptorV2::iVarsStorage::iVarsStorage()
481 : m_filled(false), m_ivars(), m_mutex() {}
482
size()483 size_t ClassDescriptorV2::iVarsStorage::size() { return m_ivars.size(); }
484
485 ClassDescriptorV2::iVarDescriptor &ClassDescriptorV2::iVarsStorage::
operator [](size_t idx)486 operator[](size_t idx) {
487 return m_ivars[idx];
488 }
489
fill(AppleObjCRuntimeV2 & runtime,ClassDescriptorV2 & descriptor)490 void ClassDescriptorV2::iVarsStorage::fill(AppleObjCRuntimeV2 &runtime,
491 ClassDescriptorV2 &descriptor) {
492 if (m_filled)
493 return;
494 std::lock_guard<std::recursive_mutex> guard(m_mutex);
495 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
496 LLDB_LOGV(log, "class_name = {0}", descriptor.GetClassName());
497 m_filled = true;
498 ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
499 runtime.GetEncodingToType());
500 Process *process(runtime.GetProcess());
501 if (!encoding_to_type_sp)
502 return;
503 descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
504 encoding_to_type_sp,
505 log](const char *name,
506 const char *type,
507 lldb::addr_t offset_ptr,
508 uint64_t size) -> bool {
509 const bool for_expression = false;
510 const bool stop_loop = false;
511 LLDB_LOGV(log, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = {3}",
512 name, type, offset_ptr, size);
513 CompilerType ivar_type =
514 encoding_to_type_sp->RealizeType(type, for_expression);
515 if (ivar_type) {
516 LLDB_LOGV(log,
517 "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = "
518 "{3}, type_size = {4}",
519 name, type, offset_ptr, size,
520 ivar_type.GetByteSize(nullptr).getValueOr(0));
521 Scalar offset_scalar;
522 Status error;
523 const int offset_ptr_size = 4;
524 const bool is_signed = false;
525 size_t read = process->ReadScalarIntegerFromMemory(
526 offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
527 if (error.Success() && 4 == read) {
528 LLDB_LOGV(log, "offset_ptr = {0:x} --> {1}", offset_ptr,
529 offset_scalar.SInt());
530 m_ivars.push_back(
531 {ConstString(name), ivar_type, size, offset_scalar.SInt()});
532 } else
533 LLDB_LOGV(log, "offset_ptr = {0:x} --> read fail, read = %{1}",
534 offset_ptr, read);
535 }
536 return stop_loop;
537 });
538 }
539
GetIVarInformation()540 void ClassDescriptorV2::GetIVarInformation() {
541 m_ivars_storage.fill(m_runtime, *this);
542 }
543