LLDB mainline
AppleObjCClassDescriptorV2.cpp
Go to the documentation of this file.
1//===-- AppleObjCClassDescriptorV2.cpp ------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
10
12#include "lldb/Target/ABI.h"
15#include "lldb/Utility/Log.h"
17#include "llvm/ADT/Sequence.h"
18
19using namespace lldb;
20using namespace lldb_private;
21
23 Process *process, std::unique_ptr<objc_class_t> &objc_class) const {
24 objc_class = std::make_unique<objc_class_t>();
25
26 bool ret = objc_class->Read(process, m_objc_class_ptr);
27
28 if (!ret)
29 objc_class.reset();
30
31 return ret;
32}
33
35 switch (process->GetAddressByteSize()) {
36 case 4:
37 return 0xfffffffcUL;
38 case 8:
39 return 0x00007ffffffffff8UL;
40 default:
41 break;
42 }
43
45}
46
48 lldb::addr_t addr) {
49 size_t ptr_size = process->GetAddressByteSize();
50
51 size_t objc_class_size = ptr_size // uintptr_t isa;
52 + ptr_size // Class superclass;
53 + ptr_size // void *cache;
54 + ptr_size // IMP *vtable;
55 + ptr_size; // uintptr_t data_NEVER_USE;
56
57 DataBufferHeap objc_class_buf(objc_class_size, '\0');
59
60 process->ReadMemory(addr, objc_class_buf.GetBytes(), objc_class_size, error);
61 if (error.Fail()) {
62 return false;
63 }
64
65 DataExtractor extractor(objc_class_buf.GetBytes(), objc_class_size,
66 process->GetByteOrder(),
67 process->GetAddressByteSize());
68
69 lldb::offset_t cursor = 0;
70
71 m_isa = extractor.GetAddress_unchecked(&cursor); // uintptr_t isa;
72 m_superclass = extractor.GetAddress_unchecked(&cursor); // Class superclass;
73 m_cache_ptr = extractor.GetAddress_unchecked(&cursor); // void *cache;
74 m_vtable_ptr = extractor.GetAddress_unchecked(&cursor); // IMP *vtable;
75 lldb::addr_t data_NEVER_USE =
76 extractor.GetAddress_unchecked(&cursor); // uintptr_t data_NEVER_USE;
77
78 m_flags = (uint8_t)(data_NEVER_USE & (lldb::addr_t)3);
79 m_data_ptr = data_NEVER_USE & GetClassDataMask(process);
80
81 if (ABISP abi_sp = process->GetABI()) {
82 m_isa = abi_sp->FixCodeAddress(m_isa);
83 m_superclass = abi_sp->FixCodeAddress(m_superclass);
84 m_data_ptr = abi_sp->FixCodeAddress(m_data_ptr);
85 }
86 return true;
87}
88
90 size_t ptr_size = process->GetAddressByteSize();
91
92 size_t size = sizeof(uint32_t) // uint32_t flags;
93 + sizeof(uint32_t) // uint32_t version;
94 + ptr_size // const class_ro_t *ro;
95 + ptr_size // union { method_list_t **method_lists;
96 // method_list_t *method_list; };
97 + ptr_size // struct chained_property_list *properties;
98 + ptr_size // const protocol_list_t **protocols;
99 + ptr_size // Class firstSubclass;
100 + ptr_size; // Class nextSiblingClass;
101
102 DataBufferHeap buffer(size, '\0');
104
105 process->ReadMemory(addr, buffer.GetBytes(), size, error);
106 if (error.Fail()) {
107 return false;
108 }
109
110 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
111 process->GetAddressByteSize());
112
113 lldb::offset_t cursor = 0;
114 m_flags = extractor.GetU32_unchecked(&cursor);
115 m_version = extractor.GetU32_unchecked(&cursor);
116 m_ro_ptr = extractor.GetAddress_unchecked(&cursor);
117 if (ABISP abi_sp = process->GetABI())
118 m_ro_ptr = abi_sp->FixCodeAddress(m_ro_ptr);
119 m_method_list_ptr = extractor.GetAddress_unchecked(&cursor);
120 m_properties_ptr = extractor.GetAddress_unchecked(&cursor);
121
122 if (m_ro_ptr & 1) {
123 DataBufferHeap buffer(ptr_size, '\0');
124 process->ReadMemory(m_ro_ptr ^ 1, buffer.GetBytes(), ptr_size, error);
125 if (error.Fail())
126 return false;
127 DataExtractor extractor(buffer.GetBytes(), ptr_size,
128 process->GetByteOrder(),
129 process->GetAddressByteSize());
130 lldb::offset_t cursor = 0;
131 m_ro_ptr = extractor.GetAddress_unchecked(&cursor);
132 if (ABISP abi_sp = process->GetABI())
133 m_ro_ptr = abi_sp->FixCodeAddress(m_ro_ptr);
134 }
135
136 return true;
137}
138
140 size_t ptr_size = process->GetAddressByteSize();
141
142 size_t size = sizeof(uint32_t) // uint32_t flags;
143 + sizeof(uint32_t) // uint32_t instanceStart;
144 + sizeof(uint32_t) // uint32_t instanceSize;
145 + (ptr_size == 8 ? sizeof(uint32_t)
146 : 0) // uint32_t reserved; // __LP64__ only
147 + ptr_size // const uint8_t *ivarLayout;
148 + ptr_size // const char *name;
149 + ptr_size // const method_list_t *baseMethods;
150 + ptr_size // const protocol_list_t *baseProtocols;
151 + ptr_size // const ivar_list_t *ivars;
152 + ptr_size // const uint8_t *weakIvarLayout;
153 + ptr_size; // const property_list_t *baseProperties;
154
155 DataBufferHeap buffer(size, '\0');
157
158 process->ReadMemory(addr, buffer.GetBytes(), size, error);
159 if (error.Fail()) {
160 return false;
161 }
162
163 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
164 process->GetAddressByteSize());
165
166 lldb::offset_t cursor = 0;
167
168 m_flags = extractor.GetU32_unchecked(&cursor);
169 m_instanceStart = extractor.GetU32_unchecked(&cursor);
170 m_instanceSize = extractor.GetU32_unchecked(&cursor);
171 if (ptr_size == 8)
172 m_reserved = extractor.GetU32_unchecked(&cursor);
173 else
174 m_reserved = 0;
175 m_ivarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
176 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
177 m_baseMethods_ptr = extractor.GetAddress_unchecked(&cursor);
178 m_baseProtocols_ptr = extractor.GetAddress_unchecked(&cursor);
179 m_ivars_ptr = extractor.GetAddress_unchecked(&cursor);
180 m_weakIvarLayout_ptr = extractor.GetAddress_unchecked(&cursor);
181 m_baseProperties_ptr = extractor.GetAddress_unchecked(&cursor);
182
183 DataBufferHeap name_buf(1024, '\0');
184
185 process->ReadCStringFromMemory(m_name_ptr, (char *)name_buf.GetBytes(),
186 name_buf.GetByteSize(), error);
187
188 if (error.Fail()) {
189 return false;
190 }
191
192 m_name.assign((char *)name_buf.GetBytes());
193
194 return true;
195}
196
198 Process *process, const objc_class_t &objc_class,
199 std::unique_ptr<class_ro_t> &class_ro,
200 std::unique_ptr<class_rw_t> &class_rw) const {
201 class_ro.reset();
202 class_rw.reset();
203
205 uint32_t class_row_t_flags = process->ReadUnsignedIntegerFromMemory(
206 objc_class.m_data_ptr, sizeof(uint32_t), 0, error);
207 if (!error.Success())
208 return false;
209
210 if (class_row_t_flags & RW_REALIZED) {
211 class_rw = std::make_unique<class_rw_t>();
212
213 if (!class_rw->Read(process, objc_class.m_data_ptr)) {
214 class_rw.reset();
215 return false;
216 }
217
218 class_ro = std::make_unique<class_ro_t>();
219
220 if (!class_ro->Read(process, class_rw->m_ro_ptr)) {
221 class_rw.reset();
222 class_ro.reset();
223 return false;
224 }
225 } else {
226 class_ro = std::make_unique<class_ro_t>();
227
228 if (!class_ro->Read(process, objc_class.m_data_ptr)) {
229 class_ro.reset();
230 return false;
231 }
232 }
233
234 return true;
235}
236
238 lldb::addr_t addr) {
239 size_t size = sizeof(uint32_t) // uint32_t entsize_NEVER_USE;
240 + sizeof(uint32_t); // uint32_t count;
241
242 DataBufferHeap buffer(size, '\0');
244
245 if (ABISP abi_sp = process->GetABI())
246 addr = abi_sp->FixCodeAddress(addr);
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 uint32_t entsize = extractor.GetU32_unchecked(&cursor);
258 m_is_small = (entsize & 0x80000000) != 0;
259 m_has_direct_selector = (entsize & 0x40000000) != 0;
260 m_has_relative_types = (entsize & 0x20000000) != 0;
261 m_entsize = entsize & 0xfffc;
262 m_count = extractor.GetU32_unchecked(&cursor);
263 m_first_ptr = addr + cursor;
264
265 return true;
266}
267
268llvm::SmallVector<ClassDescriptorV2::method_t, 0>
269ClassDescriptorV2::ReadMethods(llvm::ArrayRef<lldb::addr_t> addresses,
270 lldb::addr_t relative_string_base_addr,
271 bool is_small, bool has_direct_sel,
272 bool has_relative_types) const {
273 lldb_private::Process *process = m_runtime.GetProcess();
274 if (!process)
275 return {};
276
277 const size_t size = method_t::GetSize(process, is_small);
278 const size_t num_methods = addresses.size();
279
280 llvm::SmallVector<uint8_t, 0> buffer(num_methods * size, 0);
281
282 llvm::SmallVector<Range<addr_t, size_t>> mem_ranges =
283 llvm::to_vector(llvm::map_range(llvm::seq(num_methods), [&](size_t idx) {
284 return Range<addr_t, size_t>(addresses[idx], size);
285 }));
286
287 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> read_results =
288 process->ReadMemoryRanges(mem_ranges, buffer);
289
290 llvm::SmallVector<method_t, 0> methods;
291 methods.reserve(num_methods);
292 for (auto [addr, memory] : llvm::zip(addresses, read_results)) {
293 // Ignore partial reads.
294 if (memory.size() != size)
295 continue;
296
297 DataExtractor extractor(memory.data(), size, process->GetByteOrder(),
298 process->GetAddressByteSize());
299 methods.push_back(method_t());
300 methods.back().Read(extractor, process, addr, relative_string_base_addr,
301 is_small, has_direct_sel, has_relative_types);
302 }
303
304 return methods;
305}
306
308 Process *process, lldb::addr_t addr,
309 lldb::addr_t relative_string_base_addr,
310 bool is_small, bool has_direct_sel,
311 bool has_relative_types) {
312 lldb::offset_t cursor = 0;
313
314 if (is_small) {
315 uint32_t nameref_offset = extractor.GetU32_unchecked(&cursor);
316 uint32_t types_offset = extractor.GetU32_unchecked(&cursor);
317 uint32_t imp_offset = extractor.GetU32_unchecked(&cursor);
318
319 m_name_ptr = addr + nameref_offset;
320
322 if (!has_direct_sel) {
323 // The SEL offset points to a SELRef. We need to dereference twice.
325 if (error.Fail())
326 return false;
327 } else if (relative_string_base_addr != LLDB_INVALID_ADDRESS) {
328 m_name_ptr = relative_string_base_addr + nameref_offset;
329 }
330 if (has_relative_types)
331 m_types_ptr = relative_string_base_addr + types_offset;
332 else
333 m_types_ptr = addr + 4 + types_offset;
334 m_imp_ptr = addr + 8 + imp_offset;
335 } else {
336 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
337 m_types_ptr = extractor.GetAddress_unchecked(&cursor);
338 m_imp_ptr = extractor.GetAddress_unchecked(&cursor);
339 }
340
343 if (error.Fail())
344 return false;
345
347 return error.Success();
348}
349
351 size_t size = sizeof(uint32_t) // uint32_t entsize;
352 + sizeof(uint32_t); // uint32_t count;
353
354 DataBufferHeap buffer(size, '\0');
356
357 process->ReadMemory(addr, buffer.GetBytes(), size, error);
358 if (error.Fail()) {
359 return false;
360 }
361
362 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
363 process->GetAddressByteSize());
364
365 lldb::offset_t cursor = 0;
366
367 m_entsize = extractor.GetU32_unchecked(&cursor);
368 m_count = extractor.GetU32_unchecked(&cursor);
369 m_first_ptr = addr + cursor;
370
371 return true;
372}
373
375 size_t size = GetSize(process);
376
377 DataBufferHeap buffer(size, '\0');
379
380 process->ReadMemory(addr, buffer.GetBytes(), size, error);
381 if (error.Fail()) {
382 return false;
383 }
384
385 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
386 process->GetAddressByteSize());
387
388 lldb::offset_t cursor = 0;
389
390 m_offset_ptr = extractor.GetAddress_unchecked(&cursor);
391 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
392 m_type_ptr = extractor.GetAddress_unchecked(&cursor);
393 m_alignment = extractor.GetU32_unchecked(&cursor);
394 m_size = extractor.GetU32_unchecked(&cursor);
395
397 if (error.Fail()) {
398 return false;
399 }
400
402 return !error.Fail();
403}
404
406 lldb::addr_t addr) {
407 Log *log = GetLog(LLDBLog::Types);
408 size_t size = sizeof(uint64_t); // m_image_index : 16
409 // m_list_offset : 48
410
411 DataBufferHeap buffer(size, '\0');
413
414 process->ReadMemory(addr, buffer.GetBytes(), size, error);
415 // FIXME: Propagate this error up
416 if (error.Fail()) {
417 LLDB_LOG(log, "Failed to read relative_list_entry_t at address {0:x}",
418 addr);
419 return false;
420 }
421
422 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
423 process->GetAddressByteSize());
424 lldb::offset_t cursor = 0;
425 uint64_t raw_entry = extractor.GetU64_unchecked(&cursor);
426 m_image_index = raw_entry & 0xFFFF;
427 m_list_offset = llvm::SignExtend64<48>(raw_entry >> 16);
428 return true;
429}
430
432 lldb::addr_t addr) {
433 Log *log = GetLog(LLDBLog::Types);
434 size_t size = sizeof(uint32_t) // m_entsize
435 + sizeof(uint32_t); // m_count
436
437 DataBufferHeap buffer(size, '\0');
439
440 // FIXME: Propagate this error up
441 process->ReadMemory(addr, buffer.GetBytes(), size, error);
442 if (error.Fail()) {
443 LLDB_LOG(log, "Failed to read relative_list_list_t at address 0x" PRIx64,
444 addr);
445 return false;
446 }
447
448 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
449 process->GetAddressByteSize());
450 lldb::offset_t cursor = 0;
451 m_entsize = extractor.GetU32_unchecked(&cursor);
452 m_count = extractor.GetU32_unchecked(&cursor);
453 m_first_ptr = addr + cursor;
454 return true;
455}
456
457std::optional<ClassDescriptorV2::method_list_t>
459 lldb::addr_t method_list_ptr) const {
460 Log *log = GetLog(LLDBLog::Types);
462 if (!method_list.Read(process, method_list_ptr))
463 return std::nullopt;
464
465 const size_t method_size = method_t::GetSize(process, method_list.m_is_small);
466 if (method_list.m_entsize != method_size) {
467 LLDB_LOG(log,
468 "method_list_t at address 0x" PRIx64 " has an entsize of " PRIu16
469 " but method size should be " PRIu64,
470 method_list_ptr, method_list.m_entsize, method_size);
471 return std::nullopt;
472 }
473
474 return method_list;
475}
476
478 std::function<bool(const char *, const char *)> const &instance_method_func,
479 ClassDescriptorV2::method_list_t &method_list) const {
480 auto idx_to_method_addr = [&](uint32_t idx) {
481 return method_list.m_first_ptr + (idx * method_list.m_entsize);
482 };
483 llvm::SmallVector<addr_t> addresses = llvm::to_vector(llvm::map_range(
484 llvm::seq<uint32_t>(method_list.m_count), idx_to_method_addr));
485
486 llvm::SmallVector<method_t, 0> methods =
487 ReadMethods(addresses, m_runtime.GetRelativeSelectorBaseAddr(),
488 method_list.m_is_small, method_list.m_has_direct_selector,
489 method_list.m_has_relative_types);
490
491 for (const auto &method : methods)
492 if (instance_method_func(method.m_name.c_str(), method.m_types.c_str()))
493 break;
494 return true;
495}
496
497// The relevant data structures:
498// - relative_list_list_t
499// - uint32_t count
500// - uint32_t entsize
501// - Followed by <count> number of relative_list_entry_t of size <entsize>
502//
503// - relative_list_entry_t
504// - uint64_t image_index : 16
505// - int64_t list_offset : 48
506// - Note: The above 2 fit into 8 bytes always
507//
508// image_index corresponds to an image in the shared cache
509// list_offset is used to calculate the address of the method_list_t we want
511 std::function<bool(const char *, const char *)> const &instance_method_func,
512 lldb::addr_t relative_method_list_ptr) const {
513 lldb_private::Process *process = m_runtime.GetProcess();
514 auto relative_method_lists = std::make_unique<relative_list_list_t>();
515
516 // 1. Process the count and entsize of the relative_list_list_t
517 if (!relative_method_lists->Read(process, relative_method_list_ptr))
518 return false;
519
520 auto entry = std::make_unique<relative_list_entry_t>();
521 for (uint32_t i = 0; i < relative_method_lists->m_count; i++) {
522 // 2. Extract the image index and the list offset from the
523 // relative_list_entry_t
524 const lldb::addr_t entry_addr = relative_method_lists->m_first_ptr +
525 (i * relative_method_lists->m_entsize);
526 if (!entry->Read(process, entry_addr))
527 return false;
528
529 // 3. Calculate the pointer to the method_list_t from the
530 // relative_list_entry_t
531 const lldb::addr_t method_list_addr = entry_addr + entry->m_list_offset;
532
533 // 4. Get the method_list_t from the pointer
534 std::optional<method_list_t> method_list =
535 GetMethodList(process, method_list_addr);
536 if (!method_list)
537 return false;
538
539 // 5. Cache the result so we don't need to reconstruct it later.
540 m_image_to_method_lists[entry->m_image_index].emplace_back(*method_list);
541
542 // 6. If the relevant image is loaded, add the methods to the Decl
543 if (!m_runtime.IsSharedCacheImageLoaded(entry->m_image_index))
544 continue;
545
546 if (!ProcessMethodList(instance_method_func, *method_list))
547 return false;
548 }
549
550 // We need to keep track of the last time we updated so we can re-update the
551 // type information in the future
552 m_last_version_updated = m_runtime.GetSharedCacheImageHeaderVersion();
553
554 return true;
555}
556
558 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
559 std::function<bool(const char *, const char *)> const &instance_method_func,
560 std::function<bool(const char *, const char *)> const &class_method_func,
561 std::function<bool(const char *, const char *, lldb::addr_t,
562 uint64_t)> const &ivar_func) const {
563 lldb_private::Process *process = m_runtime.GetProcess();
564
565 std::unique_ptr<objc_class_t> objc_class;
566 std::unique_ptr<class_ro_t> class_ro;
567 std::unique_ptr<class_rw_t> class_rw;
568
569 if (!Read_objc_class(process, objc_class))
570 return false;
571 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
572 return false;
573
574 static ConstString NSObject_name("NSObject");
575
576 if (m_name != NSObject_name && superclass_func)
577 superclass_func(objc_class->m_superclass);
578
579 if (instance_method_func) {
580 // This is a relative list of lists
581 if (class_ro->m_baseMethods_ptr & 1) {
582 if (!ProcessRelativeMethodLists(instance_method_func,
583 class_ro->m_baseMethods_ptr ^ 1))
584 return false;
585 } else {
586 std::optional<method_list_t> base_method_list =
587 GetMethodList(process, class_ro->m_baseMethods_ptr);
588 if (base_method_list &&
589 !ProcessMethodList(instance_method_func, *base_method_list))
590 return false;
591 }
592 }
593
594 if (class_method_func) {
596
597 // We don't care about the metaclass's superclass, or its class methods.
598 // Its instance methods are our class methods.
599
600 if (metaclass) {
601 metaclass->Describe(
602 std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
603 class_method_func,
604 std::function<bool(const char *, const char *)>(nullptr),
605 std::function<bool(const char *, const char *, lldb::addr_t,
606 uint64_t)>(nullptr));
607 }
608 }
609
610 if (ivar_func) {
611 if (class_ro->m_ivars_ptr != 0) {
612 ivar_list_t ivar_list;
613 if (!ivar_list.Read(process, class_ro->m_ivars_ptr))
614 return false;
615
616 if (ivar_list.m_entsize != ivar_t::GetSize(process))
617 return false;
618
619 ivar_t ivar;
620
621 for (uint32_t i = 0, e = ivar_list.m_count; i < e; ++i) {
622 ivar.Read(process, ivar_list.m_first_ptr + (i * ivar_list.m_entsize));
623
624 if (ivar_func(ivar.m_name.c_str(), ivar.m_type.c_str(),
625 ivar.m_offset_ptr, ivar.m_size))
626 break;
627 }
628 }
629 }
630
631 return true;
632}
633
635 if (!m_name) {
636 lldb_private::Process *process = m_runtime.GetProcess();
637
638 if (process) {
639 std::unique_ptr<objc_class_t> objc_class;
640 std::unique_ptr<class_ro_t> class_ro;
641 std::unique_ptr<class_rw_t> class_rw;
642
643 if (!Read_objc_class(process, objc_class))
644 return m_name;
645 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
646 return m_name;
647
648 m_name = ConstString(class_ro->m_name.c_str());
649 }
650 }
651 return m_name;
652}
653
655 lldb_private::Process *process = m_runtime.GetProcess();
656
657 if (!process)
659
660 std::unique_ptr<objc_class_t> objc_class;
661
662 if (!Read_objc_class(process, objc_class))
664
665 return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
666 objc_class->m_superclass);
667}
668
670 lldb_private::Process *process = m_runtime.GetProcess();
671
672 if (!process)
674
675 std::unique_ptr<objc_class_t> objc_class;
676
677 if (!Read_objc_class(process, objc_class))
679
680 lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
681
683 new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
684}
685
687 lldb_private::Process *process = m_runtime.GetProcess();
688
689 if (process) {
690 std::unique_ptr<objc_class_t> objc_class;
691 std::unique_ptr<class_ro_t> class_ro;
692 std::unique_ptr<class_rw_t> class_rw;
693
694 if (!Read_objc_class(process, objc_class))
695 return 0;
696 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
697 return 0;
698
699 return class_ro->m_instanceSize;
700 }
701
702 return 0;
703}
704
705// From the ObjC runtime.
706static uint8_t IS_SWIFT_STABLE = 1U << 1;
707
709 std::unique_ptr<objc_class_t> objc_class;
710 if (auto *process = m_runtime.GetProcess())
711 if (Read_objc_class(process, objc_class))
712 if (objc_class->m_flags & IS_SWIFT_STABLE)
714
716}
717
719
721
726
728 ClassDescriptorV2 &descriptor) {
729 if (m_filled)
730 return;
731 std::lock_guard<std::recursive_mutex> guard(m_mutex);
732 Log *log = GetLog(LLDBLog::Types);
733 LLDB_LOGV(log, "class_name = {0}", descriptor.GetClassName());
734 m_filled = true;
735 ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
736 runtime.GetEncodingToType());
737 Process *process(runtime.GetProcess());
738 if (!encoding_to_type_sp)
739 return;
740 descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
741 encoding_to_type_sp,
742 log](const char *name,
743 const char *type,
744 lldb::addr_t offset_ptr,
745 uint64_t size) -> bool {
746 const bool for_expression = false;
747 const bool stop_loop = false;
748 LLDB_LOGV(log, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = {3}",
749 name, type, offset_ptr, size);
750 CompilerType ivar_type =
751 encoding_to_type_sp->RealizeType(type, for_expression);
752 if (ivar_type) {
753 LLDB_LOGV(log,
754 "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = "
755 "{3}, type_size = {4}",
756 name, type, offset_ptr, size,
757 expectedToOptional(ivar_type.GetByteSize(nullptr)).value_or(0));
758 Scalar offset_scalar;
760 const int offset_ptr_size = 4;
761 const bool is_signed = false;
762 size_t read = process->ReadScalarIntegerFromMemory(
763 offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
764 if (error.Success() && 4 == read) {
765 LLDB_LOGV(log, "offset_ptr = {0:x} --> {1}", offset_ptr,
766 offset_scalar.SInt());
767 m_ivars.push_back(
768 {ConstString(name), ivar_type, size, offset_scalar.SInt()});
769 } else
770 LLDB_LOGV(log, "offset_ptr = {0:x} --> read fail, read = %{1}",
771 offset_ptr, read);
772 }
773 return stop_loop;
774 });
775}
776
static lldb::addr_t GetClassDataMask(Process *process)
static uint8_t IS_SWIFT_STABLE
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGV(log,...)
Definition Log.h:383
EncodingToTypeSP GetEncodingToType() override
void fill(AppleObjCRuntimeV2 &runtime, ClassDescriptorV2 &descriptor)
ObjCLanguageRuntime::ClassDescriptorSP GetSuperclass() override
llvm::SmallVector< method_t, 0 > ReadMethods(llvm::ArrayRef< lldb::addr_t > addresses, lldb::addr_t relative_string_base_addr, bool is_small, bool has_direct_sel, bool has_relative_types) const
bool 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) const override
friend class lldb_private::AppleObjCRuntimeV2
bool 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) const
bool ProcessRelativeMethodLists(std::function< bool(const char *, const char *)> const &instance_method_func, lldb::addr_t relative_method_list_ptr) const
ClassDescriptorV2(AppleObjCRuntimeV2 &runtime, ObjCLanguageRuntime::ObjCISA isa, const char *name)
bool ProcessMethodList(std::function< bool(const char *, const char *)> const &instance_method_func, method_list_t &method_list) const
bool Read_objc_class(Process *process, std::unique_ptr< objc_class_t > &objc_class) const
std::optional< uint64_t > m_last_version_updated
std::map< uint16_t, std::vector< method_list_t > > m_image_to_method_lists
ObjCLanguageRuntime::ClassDescriptorSP GetMetaclass() const override
lldb::LanguageType GetImplementationLanguage() const override
Determine whether this class is implemented in Swift.
std::optional< method_list_t > GetMethodList(Process *process, lldb::addr_t method_list_ptr) const
Generic representation of a type in a programming language.
llvm::Expected< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
A uniqued constant string class.
Definition ConstString.h:40
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An data extractor class.
virtual uint32_t GetU32_unchecked(lldb::offset_t *offset_ptr) const
uint64_t GetAddress_unchecked(lldb::offset_t *offset_ptr) const
virtual uint64_t GetU64_unchecked(lldb::offset_t *offset_ptr) const
std::shared_ptr< ClassDescriptor > ClassDescriptorSP
std::shared_ptr< EncodingToType > EncodingToTypeSP
A plug-in interface definition class for debugging a process.
Definition Process.h:354
size_t ReadScalarIntegerFromMemory(lldb::addr_t addr, uint32_t byte_size, bool is_signed, Scalar &scalar, Status &error)
Definition Process.cpp:2436
size_t ReadCStringFromMemory(lldb::addr_t vm_addr, char *cstr, size_t cstr_max_len, Status &error)
Read a NULL terminated C string from memory.
Definition Process.cpp:2160
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:1928
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3660
uint64_t ReadUnsignedIntegerFromMemory(lldb::addr_t load_addr, size_t byte_size, uint64_t fail_value, Status &error)
Reads an unsigned integer of the specified byte size from process memory.
Definition Process.cpp:2279
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2301
uint32_t GetAddressByteSize() const
Definition Process.cpp:3664
virtual llvm::SmallVector< llvm::MutableArrayRef< uint8_t > > ReadMemoryRanges(llvm::ArrayRef< Range< lldb::addr_t, size_t > > ranges, llvm::MutableArrayRef< uint8_t > buffer)
Read from multiple memory ranges and write the results into buffer.
Definition Process.cpp:1973
const lldb::ABISP & GetABI()
Definition Process.cpp:1479
Process * GetProcess()
Definition Runtime.h:22
int SInt(int fail_value=0) const
Definition Scalar.cpp:349
An error handling class.
Definition Status.h:118
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::ABI > ABISP
uint64_t offset_t
Definition lldb-types.h:85
LanguageType
Programming language type.
@ eLanguageTypeSwift
Swift.
@ eLanguageTypeObjC
Objective-C.
uint64_t addr_t
Definition lldb-types.h:80
bool Read(Process *process, lldb::addr_t addr)
bool Read(Process *process, lldb::addr_t addr)
bool Read(Process *process, lldb::addr_t addr)
bool Read(Process *process, lldb::addr_t addr)
static size_t GetSize(Process *process, bool is_small)
bool Read(DataExtractor &extractor, Process *process, lldb::addr_t addr, lldb::addr_t relative_string_base_addr, bool is_small, bool has_direct_sel, bool has_relative_types)
bool Read(Process *process, lldb::addr_t addr)
uint16_t m_image_index
int64_t m_list_offset
bool Read(Process *process, lldb::addr_t addr)