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