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
269 llvm::MutableArrayRef<method_t> methods, Process &process) {
270 std::vector<lldb::addr_t> str_addresses;
271 str_addresses.reserve(2 * methods.size());
272 for (auto &method : methods)
273 str_addresses.push_back(method.m_name_ptr);
274 for (auto &method : methods)
275 str_addresses.push_back(method.m_types_ptr);
276
277 llvm::SmallVector<std::optional<std::string>> read_result =
278 process.ReadCStringsFromMemory(str_addresses);
279 auto names = llvm::MutableArrayRef(read_result).take_front(methods.size());
280 auto types = llvm::MutableArrayRef(read_result).take_back(methods.size());
281
282 for (auto [name_str, type_str, method] : llvm::zip(names, types, methods)) {
283 if (name_str)
284 method.m_name = std::move(*name_str);
285 if (type_str)
286 method.m_types = std::move(*type_str);
287 }
288}
289
290llvm::SmallVector<ClassDescriptorV2::method_t, 0>
291ClassDescriptorV2::ReadMethods(llvm::ArrayRef<lldb::addr_t> addresses,
292 lldb::addr_t relative_string_base_addr,
293 bool is_small, bool has_direct_sel,
294 bool has_relative_types) const {
295 lldb_private::Process *process = m_runtime.GetProcess();
296 if (!process)
297 return {};
298
299 const size_t size = method_t::GetSize(process, is_small);
300 const size_t num_methods = addresses.size();
301
302 llvm::SmallVector<uint8_t, 0> buffer(num_methods * size, 0);
303
304 llvm::SmallVector<Range<addr_t, size_t>> mem_ranges =
305 llvm::to_vector(llvm::map_range(llvm::seq(num_methods), [&](size_t idx) {
306 return Range<addr_t, size_t>(addresses[idx], size);
307 }));
308
309 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> read_results =
310 process->ReadMemoryRanges(mem_ranges, buffer);
311
312 llvm::SmallVector<method_t, 0> methods;
313 methods.reserve(num_methods);
314 for (auto [addr, memory] : llvm::zip(addresses, read_results)) {
315 // Ignore partial reads.
316 if (memory.size() != size)
317 continue;
318
319 DataExtractor extractor(memory.data(), size, process->GetByteOrder(),
320 process->GetAddressByteSize());
321 methods.push_back(method_t());
322 methods.back().Read(extractor, process, addr, relative_string_base_addr,
323 is_small, has_direct_sel, has_relative_types);
324 }
325
326 method_t::ReadNames(methods, *process);
327 return methods;
328}
329
331 Process *process, lldb::addr_t addr,
332 lldb::addr_t relative_string_base_addr,
333 bool is_small, bool has_direct_sel,
334 bool has_relative_types) {
335 lldb::offset_t cursor = 0;
336
337 if (is_small) {
338 uint32_t nameref_offset = extractor.GetU32_unchecked(&cursor);
339 uint32_t types_offset = extractor.GetU32_unchecked(&cursor);
340 uint32_t imp_offset = extractor.GetU32_unchecked(&cursor);
341
342 m_name_ptr = addr + nameref_offset;
343
345 if (!has_direct_sel) {
346 // The SEL offset points to a SELRef. We need to dereference twice.
348 if (error.Fail())
349 return false;
350 } else if (relative_string_base_addr != LLDB_INVALID_ADDRESS) {
351 m_name_ptr = relative_string_base_addr + nameref_offset;
352 }
353 if (has_relative_types)
354 m_types_ptr = relative_string_base_addr + types_offset;
355 else
356 m_types_ptr = addr + 4 + types_offset;
357 m_imp_ptr = addr + 8 + imp_offset;
358 } else {
359 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
360 m_types_ptr = extractor.GetAddress_unchecked(&cursor);
361 m_imp_ptr = extractor.GetAddress_unchecked(&cursor);
362 }
363
364 return true;
365}
366
368 size_t size = sizeof(uint32_t) // uint32_t entsize;
369 + sizeof(uint32_t); // uint32_t count;
370
371 DataBufferHeap buffer(size, '\0');
373
374 process->ReadMemory(addr, buffer.GetBytes(), size, error);
375 if (error.Fail()) {
376 return false;
377 }
378
379 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
380 process->GetAddressByteSize());
381
382 lldb::offset_t cursor = 0;
383
384 m_entsize = extractor.GetU32_unchecked(&cursor);
385 m_count = extractor.GetU32_unchecked(&cursor);
386 m_first_ptr = addr + cursor;
387
388 return true;
389}
390
392 size_t size = GetSize(process);
393
394 DataBufferHeap buffer(size, '\0');
396
397 process->ReadMemory(addr, buffer.GetBytes(), size, error);
398 if (error.Fail()) {
399 return false;
400 }
401
402 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
403 process->GetAddressByteSize());
404
405 lldb::offset_t cursor = 0;
406
407 m_offset_ptr = extractor.GetAddress_unchecked(&cursor);
408 m_name_ptr = extractor.GetAddress_unchecked(&cursor);
409 m_type_ptr = extractor.GetAddress_unchecked(&cursor);
410 m_alignment = extractor.GetU32_unchecked(&cursor);
411 m_size = extractor.GetU32_unchecked(&cursor);
412
414 if (error.Fail()) {
415 return false;
416 }
417
419 return !error.Fail();
420}
421
423 lldb::addr_t addr) {
424 Log *log = GetLog(LLDBLog::Types);
425 size_t size = sizeof(uint64_t); // m_image_index : 16
426 // m_list_offset : 48
427
428 DataBufferHeap buffer(size, '\0');
430
431 process->ReadMemory(addr, buffer.GetBytes(), size, error);
432 // FIXME: Propagate this error up
433 if (error.Fail()) {
434 LLDB_LOG(log, "Failed to read relative_list_entry_t at address {0:x}",
435 addr);
436 return false;
437 }
438
439 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
440 process->GetAddressByteSize());
441 lldb::offset_t cursor = 0;
442 uint64_t raw_entry = extractor.GetU64_unchecked(&cursor);
443 m_image_index = raw_entry & 0xFFFF;
444 m_list_offset = llvm::SignExtend64<48>(raw_entry >> 16);
445 return true;
446}
447
449 lldb::addr_t addr) {
450 Log *log = GetLog(LLDBLog::Types);
451 size_t size = sizeof(uint32_t) // m_entsize
452 + sizeof(uint32_t); // m_count
453
454 DataBufferHeap buffer(size, '\0');
456
457 // FIXME: Propagate this error up
458 process->ReadMemory(addr, buffer.GetBytes(), size, error);
459 if (error.Fail()) {
460 LLDB_LOG(log, "Failed to read relative_list_list_t at address 0x" PRIx64,
461 addr);
462 return false;
463 }
464
465 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
466 process->GetAddressByteSize());
467 lldb::offset_t cursor = 0;
468 m_entsize = extractor.GetU32_unchecked(&cursor);
469 m_count = extractor.GetU32_unchecked(&cursor);
470 m_first_ptr = addr + cursor;
471 return true;
472}
473
474std::optional<ClassDescriptorV2::method_list_t>
476 lldb::addr_t method_list_ptr) const {
477 Log *log = GetLog(LLDBLog::Types);
479 if (!method_list.Read(process, method_list_ptr))
480 return std::nullopt;
481
482 const size_t method_size = method_t::GetSize(process, method_list.m_is_small);
483 if (method_list.m_entsize != method_size) {
484 LLDB_LOG(log,
485 "method_list_t at address 0x" PRIx64 " has an entsize of " PRIu16
486 " but method size should be " PRIu64,
487 method_list_ptr, method_list.m_entsize, method_size);
488 return std::nullopt;
489 }
490
491 return method_list;
492}
493
495 std::function<bool(const char *, const char *)> const &instance_method_func,
496 ClassDescriptorV2::method_list_t &method_list) const {
497 auto idx_to_method_addr = [&](uint32_t idx) {
498 return method_list.m_first_ptr + (idx * method_list.m_entsize);
499 };
500 llvm::SmallVector<addr_t> addresses = llvm::to_vector(llvm::map_range(
501 llvm::seq<uint32_t>(method_list.m_count), idx_to_method_addr));
502
503 llvm::SmallVector<method_t, 0> methods =
504 ReadMethods(addresses, m_runtime.GetRelativeSelectorBaseAddr(),
505 method_list.m_is_small, method_list.m_has_direct_selector,
506 method_list.m_has_relative_types);
507
508 for (const auto &method : methods)
509 if (instance_method_func(method.m_name.c_str(), method.m_types.c_str()))
510 break;
511 return true;
512}
513
514// The relevant data structures:
515// - relative_list_list_t
516// - uint32_t count
517// - uint32_t entsize
518// - Followed by <count> number of relative_list_entry_t of size <entsize>
519//
520// - relative_list_entry_t
521// - uint64_t image_index : 16
522// - int64_t list_offset : 48
523// - Note: The above 2 fit into 8 bytes always
524//
525// image_index corresponds to an image in the shared cache
526// list_offset is used to calculate the address of the method_list_t we want
528 std::function<bool(const char *, const char *)> const &instance_method_func,
529 lldb::addr_t relative_method_list_ptr) const {
530 lldb_private::Process *process = m_runtime.GetProcess();
531 auto relative_method_lists = std::make_unique<relative_list_list_t>();
532
533 // 1. Process the count and entsize of the relative_list_list_t
534 if (!relative_method_lists->Read(process, relative_method_list_ptr))
535 return false;
536
537 auto entry = std::make_unique<relative_list_entry_t>();
538 for (uint32_t i = 0; i < relative_method_lists->m_count; i++) {
539 // 2. Extract the image index and the list offset from the
540 // relative_list_entry_t
541 const lldb::addr_t entry_addr = relative_method_lists->m_first_ptr +
542 (i * relative_method_lists->m_entsize);
543 if (!entry->Read(process, entry_addr))
544 return false;
545
546 // 3. Calculate the pointer to the method_list_t from the
547 // relative_list_entry_t
548 const lldb::addr_t method_list_addr = entry_addr + entry->m_list_offset;
549
550 // 4. Get the method_list_t from the pointer
551 std::optional<method_list_t> method_list =
552 GetMethodList(process, method_list_addr);
553 if (!method_list)
554 return false;
555
556 // 5. Cache the result so we don't need to reconstruct it later.
557 m_image_to_method_lists[entry->m_image_index].emplace_back(*method_list);
558
559 // 6. If the relevant image is loaded, add the methods to the Decl
560 if (!m_runtime.IsSharedCacheImageLoaded(entry->m_image_index))
561 continue;
562
563 if (!ProcessMethodList(instance_method_func, *method_list))
564 return false;
565 }
566
567 // We need to keep track of the last time we updated so we can re-update the
568 // type information in the future
569 m_last_version_updated = m_runtime.GetSharedCacheImageHeaderVersion();
570
571 return true;
572}
573
575 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
576 std::function<bool(const char *, const char *)> const &instance_method_func,
577 std::function<bool(const char *, const char *)> const &class_method_func,
578 std::function<bool(const char *, const char *, lldb::addr_t,
579 uint64_t)> const &ivar_func) const {
580 lldb_private::Process *process = m_runtime.GetProcess();
581
582 std::unique_ptr<objc_class_t> objc_class;
583 std::unique_ptr<class_ro_t> class_ro;
584 std::unique_ptr<class_rw_t> class_rw;
585
586 if (!Read_objc_class(process, objc_class))
587 return false;
588 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
589 return false;
590
591 static ConstString NSObject_name("NSObject");
592
593 if (m_name != NSObject_name && superclass_func)
594 superclass_func(objc_class->m_superclass);
595
596 if (instance_method_func) {
597 // This is a relative list of lists
598 if (class_ro->m_baseMethods_ptr & 1) {
599 if (!ProcessRelativeMethodLists(instance_method_func,
600 class_ro->m_baseMethods_ptr ^ 1))
601 return false;
602 } else {
603 std::optional<method_list_t> base_method_list =
604 GetMethodList(process, class_ro->m_baseMethods_ptr);
605 if (base_method_list &&
606 !ProcessMethodList(instance_method_func, *base_method_list))
607 return false;
608 }
609 }
610
611 if (class_method_func) {
613
614 // We don't care about the metaclass's superclass, or its class methods.
615 // Its instance methods are our class methods.
616
617 if (metaclass) {
618 metaclass->Describe(
619 std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
620 class_method_func,
621 std::function<bool(const char *, const char *)>(nullptr),
622 std::function<bool(const char *, const char *, lldb::addr_t,
623 uint64_t)>(nullptr));
624 }
625 }
626
627 if (ivar_func) {
628 if (class_ro->m_ivars_ptr != 0) {
629 ivar_list_t ivar_list;
630 if (!ivar_list.Read(process, class_ro->m_ivars_ptr))
631 return false;
632
633 if (ivar_list.m_entsize != ivar_t::GetSize(process))
634 return false;
635
636 ivar_t ivar;
637
638 for (uint32_t i = 0, e = ivar_list.m_count; i < e; ++i) {
639 ivar.Read(process, ivar_list.m_first_ptr + (i * ivar_list.m_entsize));
640
641 if (ivar_func(ivar.m_name.c_str(), ivar.m_type.c_str(),
642 ivar.m_offset_ptr, ivar.m_size))
643 break;
644 }
645 }
646 }
647
648 return true;
649}
650
652 if (!m_name) {
653 lldb_private::Process *process = m_runtime.GetProcess();
654
655 if (process) {
656 std::unique_ptr<objc_class_t> objc_class;
657 std::unique_ptr<class_ro_t> class_ro;
658 std::unique_ptr<class_rw_t> class_rw;
659
660 if (!Read_objc_class(process, objc_class))
661 return m_name;
662 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
663 return m_name;
664
665 m_name = ConstString(class_ro->m_name.c_str());
666 }
667 }
668 return m_name;
669}
670
672 lldb_private::Process *process = m_runtime.GetProcess();
673
674 if (!process)
676
677 std::unique_ptr<objc_class_t> objc_class;
678
679 if (!Read_objc_class(process, objc_class))
681
682 return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
683 objc_class->m_superclass);
684}
685
687 lldb_private::Process *process = m_runtime.GetProcess();
688
689 if (!process)
691
692 std::unique_ptr<objc_class_t> objc_class;
693
694 if (!Read_objc_class(process, objc_class))
696
697 lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
698
700 new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
701}
702
704 lldb_private::Process *process = m_runtime.GetProcess();
705
706 if (process) {
707 std::unique_ptr<objc_class_t> objc_class;
708 std::unique_ptr<class_ro_t> class_ro;
709 std::unique_ptr<class_rw_t> class_rw;
710
711 if (!Read_objc_class(process, objc_class))
712 return 0;
713 if (!Read_class_row(process, *objc_class, class_ro, class_rw))
714 return 0;
715
716 return class_ro->m_instanceSize;
717 }
718
719 return 0;
720}
721
722// From the ObjC runtime.
723static uint8_t IS_SWIFT_STABLE = 1U << 1;
724
726 std::unique_ptr<objc_class_t> objc_class;
727 if (auto *process = m_runtime.GetProcess())
728 if (Read_objc_class(process, objc_class))
729 if (objc_class->m_flags & IS_SWIFT_STABLE)
731
733}
734
736
738
743
745 ClassDescriptorV2 &descriptor) {
746 if (m_filled)
747 return;
748 std::lock_guard<std::recursive_mutex> guard(m_mutex);
749 Log *log = GetLog(LLDBLog::Types);
750 LLDB_LOGV(log, "class_name = {0}", descriptor.GetClassName());
751 m_filled = true;
752 ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
753 runtime.GetEncodingToType());
754 Process *process(runtime.GetProcess());
755 if (!encoding_to_type_sp)
756 return;
757 descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
758 encoding_to_type_sp,
759 log](const char *name,
760 const char *type,
761 lldb::addr_t offset_ptr,
762 uint64_t size) -> bool {
763 const bool for_expression = false;
764 const bool stop_loop = false;
765 LLDB_LOGV(log, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = {3}",
766 name, type, offset_ptr, size);
767 CompilerType ivar_type =
768 encoding_to_type_sp->RealizeType(type, for_expression);
769 if (ivar_type) {
770 LLDB_LOGV(log,
771 "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = "
772 "{3}, type_size = {4}",
773 name, type, offset_ptr, size,
774 expectedToOptional(ivar_type.GetByteSize(nullptr)).value_or(0));
775 Scalar offset_scalar;
777 const int offset_ptr_size = 4;
778 const bool is_signed = false;
779 size_t read = process->ReadScalarIntegerFromMemory(
780 offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
781 if (error.Success() && 4 == read) {
782 LLDB_LOGV(log, "offset_ptr = {0:x} --> {1}", offset_ptr,
783 offset_scalar.SInt());
784 m_ivars.push_back(
785 {ConstString(name), ivar_type, size, offset_scalar.SInt()});
786 } else
787 LLDB_LOGV(log, "offset_ptr = {0:x} --> read fail, read = %{1}",
788 offset_ptr, read);
789 }
790 return stop_loop;
791 });
792}
793
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:2492
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:2216
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:1930
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3716
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:2335
llvm::SmallVector< std::optional< std::string > > ReadCStringsFromMemory(llvm::ArrayRef< lldb::addr_t > addresses)
Definition Process.cpp:2141
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2357
uint32_t GetAddressByteSize() const
Definition Process.cpp:3720
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:1975
const lldb::ABISP & GetABI()
Definition Process.cpp:1481
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)
static void ReadNames(llvm::MutableArrayRef< method_t > methods, Process &process)
Fill in m_name and m_types efficiently by batching read requests.
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)