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 llvm::SmallVector<std::optional<std::string>> strs =
387 process->ReadCStringsFromMemory({result.m_name_ptr, result.m_type_ptr});
388
389 if (!strs[0])
390 return llvm::createStringErrorV(
391 "failed to read ivar_t::m_name_str at address {0:x}",
392 result.m_name_ptr);
393 if (!strs[1])
394 return llvm::createStringErrorV(
395 "failed to read ivar_t::m_type_str at address {0:x}",
396 result.m_type_ptr);
397
398 result.m_name = std::move(*strs[0]);
399 result.m_type = std::move(*strs[1]);
400 return result;
401}
402
403llvm::Expected<llvm::SmallVector<ClassDescriptorV2::relative_list_entry_t>>
405 llvm::ArrayRef<lldb::addr_t> addrs) {
406 size_t size = sizeof(uint64_t); // m_image_index : 16
407 // m_list_offset : 48
408
409 llvm::SmallVector<std::optional<uint64_t>> raw_entries =
410 process.ReadUnsignedIntegersFromMemory(addrs, size);
411
412 llvm::SmallVector<relative_list_entry_t> results;
413 results.reserve(addrs.size());
414 for (auto [addr, maybe_raw] : llvm::zip(addrs, raw_entries)) {
415 if (!maybe_raw)
416 return llvm::createStringErrorV(
417 "Failed to read relative_list_entry_t at address {0:x}", addr);
418 uint64_t raw = *maybe_raw;
419 uint16_t image_index = raw & 0xFFFF;
420 int64_t list_offset = llvm::SignExtend64<48>(raw >> 16);
421 results.push_back(relative_list_entry_t{image_index, list_offset});
422 }
423 return results;
424}
425
426llvm::Expected<ClassDescriptorV2::relative_list_list_t>
428 lldb::addr_t addr) {
429 size_t size = sizeof(uint32_t) // m_entsize
430 + sizeof(uint32_t); // m_count
431
432 DataBufferHeap buffer(size, '\0');
434
435 process->ReadMemory(addr, buffer.GetBytes(), size, error);
436 if (error.Fail())
437 return llvm::joinErrors(
438 error.takeError(),
439 llvm::createStringErrorV(
440 "Failed to read relative_list_list_t at address {0:x}", addr));
441
442 DataExtractor extractor(buffer.GetBytes(), size, process->GetByteOrder(),
443 process->GetAddressByteSize());
444 lldb::offset_t cursor = 0;
445 uint32_t entsize = extractor.GetU32_unchecked(&cursor);
446 uint32_t count = extractor.GetU32_unchecked(&cursor);
447 lldb::addr_t first_ptr = addr + cursor;
448 return relative_list_list_t{entsize, count, first_ptr};
449}
450
451llvm::Expected<ClassDescriptorV2::method_list_t>
453 lldb::addr_t method_list_ptr) {
454 auto method_list =
455 ClassDescriptorV2::method_list_t::Read(process, method_list_ptr);
456 if (!method_list)
457 return method_list.takeError();
458
459 const size_t method_size =
460 method_t::GetSize(process, method_list->m_is_small);
461 if (method_list->m_entsize != method_size)
462 return llvm::createStringErrorV(
463 "method_list_t at address {0:x} has an entsize of {1:x}"
464 " but method size should be {2:x}",
465 method_list_ptr, method_list->m_entsize, method_size);
466
467 return *method_list;
468}
469
471 std::function<bool(const char *, const char *)> const &instance_method_func,
472 ClassDescriptorV2::method_list_t &method_list) const {
473 auto idx_to_method_addr = [&](uint32_t idx) {
474 return method_list.m_first_ptr + (idx * method_list.m_entsize);
475 };
476 llvm::SmallVector<addr_t> addresses = llvm::to_vector(llvm::map_range(
477 llvm::seq<uint32_t>(method_list.m_count), idx_to_method_addr));
478
479 llvm::SmallVector<method_t, 0> methods =
480 ReadMethods(addresses, m_runtime.GetRelativeSelectorBaseAddr(),
481 method_list.m_is_small, method_list.m_has_direct_selector,
482 method_list.m_has_relative_types);
483
484 for (const auto &method : methods)
485 if (instance_method_func(method.m_name.c_str(), method.m_types.c_str()))
486 break;
487}
488
489// The relevant data structures:
490// - relative_list_list_t
491// - uint32_t count
492// - uint32_t entsize
493// - Followed by <count> number of relative_list_entry_t of size <entsize>
494//
495// - relative_list_entry_t
496// - uint64_t image_index : 16
497// - int64_t list_offset : 48
498// - Note: The above 2 fit into 8 bytes always
499//
500// image_index corresponds to an image in the shared cache
501// list_offset is used to calculate the address of the method_list_t we want
503 std::function<bool(const char *, const char *)> const &instance_method_func,
504 lldb::addr_t relative_method_list_ptr) const {
505 lldb_private::Process *process = m_runtime.GetProcess();
506
507 // 1. Process the count and entsize of the relative_list_list_t
508 auto relative_method_lists =
509 relative_list_list_t::Read(process, relative_method_list_ptr);
510 if (!relative_method_lists)
511 return relative_method_lists.takeError();
512
513 // 2. Compute the address of every relative_list_entry_t and read them all in
514 // a single batched memory read.
515 auto to_entry_addr = [&](uint64_t idx) {
516 return relative_method_lists->m_first_ptr +
517 (idx * relative_method_lists->m_entsize);
518 };
519 auto entry_addrs = llvm::to_vector(llvm::map_range(
520 llvm::seq<uint64_t>(relative_method_lists->m_count), to_entry_addr));
521
522 auto entries = ReadRelativeListEntries(*process, entry_addrs);
523 if (!entries)
524 return entries.takeError();
525
526 for (auto [entry_addr, entry] : llvm::zip(entry_addrs, *entries)) {
527 // 3. Calculate the pointer to the method_list_t from the
528 // relative_list_entry_t
529 const lldb::addr_t method_list_addr = entry_addr + entry.m_list_offset;
530
531 // 4. Get the method_list_t from the pointer
532 llvm::Expected<method_list_t> method_list =
533 GetMethodList(process, method_list_addr);
534 if (!method_list)
535 return method_list.takeError();
536
537 // 5. If the relevant image is loaded, add the methods to the Decl
538 if (!m_runtime.IsSharedCacheImageLoaded(entry.m_image_index))
539 continue;
540
541 ProcessMethodList(instance_method_func, *method_list);
542 }
543
544 return llvm::Error::success();
545}
546
548 std::function<void(ObjCLanguageRuntime::ObjCISA)> const &superclass_func,
549 std::function<bool(const char *, const char *)> const &instance_method_func,
550 std::function<bool(const char *, const char *)> const &class_method_func,
551 std::function<bool(const char *, const char *, lldb::addr_t,
552 uint64_t)> const &ivar_func) const {
553 lldb_private::Process *process = m_runtime.GetProcess();
554
555 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
556 if (!objc_class) {
557 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
558 return false;
559 }
560 auto class_ro = Read_class_row(process, *objc_class);
561 if (!class_ro) {
562 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), class_ro.takeError(), "{0}");
563 return false;
564 }
565
566 static ConstString NSObject_name("NSObject");
567
568 if (m_name != NSObject_name && superclass_func)
569 superclass_func(objc_class->m_superclass);
570
571 if (instance_method_func) {
572 // This is a relative list of lists
573 if (class_ro->m_baseMethods_ptr & 1) {
574 if (llvm::Error err = ProcessRelativeMethodLists(
575 instance_method_func, class_ro->m_baseMethods_ptr ^ 1)) {
576 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), std::move(err), "{0}");
577 return false;
578 }
579 } else {
580 llvm::Expected<method_list_t> base_method_list =
581 GetMethodList(process, class_ro->m_baseMethods_ptr);
582 if (base_method_list)
583 ProcessMethodList(instance_method_func, *base_method_list);
584 else
585 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), base_method_list.takeError(),
586 "{0}");
587 }
588 }
589
590 if (class_method_func) {
591 std::unique_ptr<ClassDescriptor> metaclass = GetMetaclass();
592
593 // We don't care about the metaclass's superclass, or its class methods.
594 // Its instance methods are our class methods.
595
596 if (metaclass) {
597 metaclass->Describe(
598 std::function<void(ObjCLanguageRuntime::ObjCISA)>(nullptr),
599 class_method_func,
600 std::function<bool(const char *, const char *)>(nullptr),
601 std::function<bool(const char *, const char *, lldb::addr_t,
602 uint64_t)>(nullptr));
603 }
604 }
605
606 if (ivar_func) {
607 if (class_ro->m_ivars_ptr != 0) {
608 auto ivar_list = ivar_list_t::Read(process, class_ro->m_ivars_ptr);
609 if (!ivar_list) {
610 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), ivar_list.takeError(), "{0}");
611 return false;
612 }
613
614 if (ivar_list->m_entsize != ivar_t::GetSize(process))
615 return false;
616
617 for (uint32_t i = 0, e = ivar_list->m_count; i < e; ++i) {
618 auto ivar = ivar_t::Read(process, ivar_list->m_first_ptr +
619 (i * ivar_list->m_entsize));
620 if (!ivar) {
621 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), ivar.takeError(), "{0}");
622 continue;
623 }
624
625 if (ivar_func(ivar->m_name.c_str(), ivar->m_type.c_str(),
626 ivar->m_offset_ptr, ivar->m_size))
627 break;
628 }
629 }
630 }
631
632 return true;
633}
634
636 if (!m_name) {
637 lldb_private::Process *process = m_runtime.GetProcess();
638
639 if (process) {
640 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
641 if (!objc_class) {
642 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
643 return m_name;
644 }
645 auto class_ro = Read_class_row(process, *objc_class);
646 if (!class_ro) {
647 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), class_ro.takeError(), "{0}");
648 return m_name;
649 }
650
651 m_name = ConstString(class_ro->m_name);
652 }
653 }
654 return m_name;
655}
656
658 lldb_private::Process *process = m_runtime.GetProcess();
659
660 if (!process)
662
663 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
664 if (!objc_class) {
665 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
667 }
668
669 return m_runtime.ObjCLanguageRuntime::GetClassDescriptorFromISA(
670 objc_class->m_superclass);
671}
672
673std::unique_ptr<ObjCLanguageRuntime::ClassDescriptor>
675 lldb_private::Process *process = m_runtime.GetProcess();
676
677 if (!process)
678 return nullptr;
679
680 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
681 if (!objc_class) {
682 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
683 return nullptr;
684 }
685
686 lldb::addr_t candidate_isa = m_runtime.GetPointerISA(objc_class->m_isa);
687
688 return std::unique_ptr<ClassDescriptor>(
689 new ClassDescriptorV2(m_runtime, candidate_isa, nullptr));
690}
691
693 lldb_private::Process *process = m_runtime.GetProcess();
694
695 if (process) {
696 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
697 if (!objc_class) {
698 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
699 return 0;
700 }
701 auto class_ro = Read_class_row(process, *objc_class);
702 if (!class_ro) {
703 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), class_ro.takeError(), "{0}");
704 return 0;
705 }
706
707 return class_ro->m_instanceSize;
708 }
709
710 return 0;
711}
712
713// From the ObjC runtime.
714static uint8_t IS_SWIFT_STABLE = 1U << 1;
715
717 if (auto *process = m_runtime.GetProcess()) {
718 auto objc_class = objc_class_t::Read(process, m_objc_class_ptr);
719 if (objc_class) {
720 if (objc_class->m_flags & IS_SWIFT_STABLE)
722 } else {
723 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), objc_class.takeError(), "{0}");
724 }
725 }
727}
728
730
732
737
739 ClassDescriptorV2 &descriptor) {
740 if (m_filled)
741 return;
742 std::lock_guard<std::recursive_mutex> guard(m_mutex);
743 Log *log = GetLog(LLDBLog::Types);
744 LLDB_LOG_VERBOSE(log, "class_name = {0}", descriptor.GetClassName());
745 m_filled = true;
746 ObjCLanguageRuntime::EncodingToTypeSP encoding_to_type_sp(
747 runtime.GetEncodingToType());
748 Process *process(runtime.GetProcess());
749 if (!encoding_to_type_sp)
750 return;
751 descriptor.Describe(nullptr, nullptr, nullptr, [this, process,
752 encoding_to_type_sp,
753 log](const char *name,
754 const char *type,
755 lldb::addr_t offset_ptr,
756 uint64_t size) -> bool {
757 const bool for_expression = false;
758 const bool stop_loop = false;
760 log, "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = {3}", name,
761 type, offset_ptr, size);
762 CompilerType ivar_type =
763 encoding_to_type_sp->RealizeType(type, for_expression);
764 if (ivar_type) {
766 log,
767 "name = {0}, encoding = {1}, offset_ptr = {2:x}, size = "
768 "{3}, type_size = {4}",
769 name, type, offset_ptr, size,
770 expectedToOptional(ivar_type.GetByteSize(nullptr)).value_or(0));
771 Scalar offset_scalar;
773 const int offset_ptr_size = 4;
774 const bool is_signed = false;
775 size_t read = process->ReadScalarIntegerFromMemory(
776 offset_ptr, offset_ptr_size, is_signed, offset_scalar, error);
777 if (error.Success() && 4 == read) {
778 LLDB_LOG_VERBOSE(log, "offset_ptr = {0:x} --> {1}", offset_ptr,
779 offset_scalar.SInt());
780 m_ivars.push_back(
781 {ConstString(name), ivar_type, size, offset_scalar.SInt()});
782 } else
783 LLDB_LOG_VERBOSE(log, "offset_ptr = {0:x} --> read fail, read = %{1}",
784 offset_ptr, read);
785 }
786 return stop_loop;
787 });
788}
789
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
static llvm::Expected< llvm::SmallVector< ClassDescriptorV2::relative_list_entry_t > > ReadRelativeListEntries(Process &process, llvm::ArrayRef< lldb::addr_t > addrs)
std::unique_ptr< ClassDescriptor > GetMetaclass() const override
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
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)
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
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:2649
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:2328
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:2029
llvm::SmallVector< std::optional< uint64_t > > ReadUnsignedIntegersFromMemory(llvm::ArrayRef< lldb::addr_t > addresses, unsigned byte_size)
Use Process::ReadMemoryRanges to efficiently read multiple unsigned integers from memory at once.
Definition Process.cpp:2459
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3912
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:2447
llvm::SmallVector< std::optional< std::string > > ReadCStringsFromMemory(llvm::ArrayRef< lldb::addr_t > addresses)
Definition Process.cpp:2253
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2508
uint32_t GetAddressByteSize() const
Definition Process.cpp:3916
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:2074
const lldb::ABISP & GetABI()
Definition Process.cpp:1483
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_list_t > Read(Process *process, lldb::addr_t addr)