brintos

brintos / llvm-project-archived public Read only

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