OC底层原理06-isa流程图

2020-09-12  本文已影响0人  Gomu_iOS

一、回顾

上一期OC底层原理05-isa结构分析中,我们在GomuPerson的实例对象person的地址中,拿到第一位地址中的isa,进行逆向推导isa&mask得到GomuPerson

//: 第一步,拿到`person`的内存地址
(lldb) x/4gx person
//: 或者
(lldb) p person
(GomuPerson *) $31 = 0x0000000100632a50
(lldb) x/4gx 0x0000000100632a50
//: 两种方式都能拿到`person`的地址

//: 下面的`0x001d800100002465 `就是我们需要的`isa`
0x100632a50: 0x001d800100002465 0x0000000000000000
0x100632a60: 0x0000000000000000 0x0000000000000000

//: 用`isa` & `mask`
(lldb) p/x 0x001d800100002465 & 0x00007ffffffffff8ULL
//: 得到了`GomuPerson `的16进制地址
(unsigned long long) $29 = 0x0000000100002460
//: 得到`GomuPerson `
(lldb) po 0x0000000100002460
GomuPerson

二、isa流程探索

既然我们用x/4gx打印person对象的地址,找到isa,从而拿到GomuPerson,那我们用x/4gx打印GomuPerson的地址,它会有isa吗?而它的isa又会拿到什么?

2.1 类的isa的指向

//: 拿到`GomuPerson `的内存地址
(lldb) x/4gx 0x0000000100002460
0x100002460: 0x0000000100002438 0x0000000100334140
0x100002470: 0x0000000100632a80 0x0002803400000003

//: `isa` & `mask`
(lldb) p/x 0x0000000100002438 & 0x00007ffffffffff8ULL
(unsigned long long) $32 = 0x0000000100002438

//: 又得到了`GomuPerson`
(lldb) po 0x0000000100002438
GomuPerson

2.2 元类的isa的指向

//: 拿到`GomuPerson `元类的内存地址
(lldb) x/4gx 0x0000000100002438
0x100002438: 0x00000001003340f0 0x00000001003340f0
0x100002448: 0x00000001010045e0 0x0005e03500000007

//: `isa` & `mask`
(lldb) p/x 0x00000001003340f0 & 0x00007ffffffffff8ULL
(unsigned long long) $34 = 0x00000001003340f0

//: 得到`NSObject `
(lldb) po 0x00000001003340f0
NSObject

2.3 NSObject的isa的指向

//: 拿到`NSObject `的内存地址
(lldb) x/4gx 0x00000001003340f0
0x1003340f0: 0x00000001003340f0 0x0000000100334140
0x100334100: 0x0000000101059460 0x0005e03100000007

//: `isa` & `mask`
(lldb) p/x 0x00000001003340f0 & 0x00007ffffffffff8ULL
(unsigned long long) $36 = 0x00000001003340f0

//: 又得到`NSObject `
(lldb) po 0x00000001003340f0
NSObject

2.4 这里的NSObject和系统初始化的NSObject的区别

//: 获取系统初始化的`NSObject `的地址
(lldb) p/x NSObject.class
(Class) $43 = 0x0000000100334140 NSObject

//: 拿到系统初始化的`NSObject `的`isa`
(lldb) x/4gx 0x0000000100334140
0x100334140: 0x00000001003340f0 0x0000000000000000
0x100334150: 0x0000000100632d90 0x0001801000000003

2.5 打印对象地址验证流程

// 实例对象
GomuPerson *person = [GomuPerson alloc];
// 类
Class class = object_getClass(person);
// 元类
Class metaClass = object_getClass(class);
// 根元类
Class rootMetaClass = object_getClass(metaClass);
// 根根元类
Class rootRootMetaClass = object_getClass(rootMetaClass);
NSLog(@"\n%p 实例对象\n%p 类\n%p 元类\n%p 根元类\n%p 根根元类",person,class,metaClass,rootMetaClass,rootRootMetaClass);

//: 输出
0x10066fe20 实例对象
0x100002470 类
0x100002448 元类
0x1003340f0 根元类
0x1003340f0 根根元类

2.6 isa流程图

9491599893819_.pic_hd.jpg

三、继承流程图

四、将isa流程图,和继承流程图结合得出

isa流程图.png
上一篇 下一篇

猜你喜欢

热点阅读