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