Get class_rw_t

After obtaining the memory address of class_datA_bits_t, we need to obtain the data in it. We see the source code like this:

LLDB print: LLDB print: LLDB print: LLDB print

P $2->data(); p *($3) = class_rw_t; ($2 and $3 are both variable names generated by the LLDB print.) .

Now we need to get the methods, properties, protocols and so forth that are cached in this class. So let’s see what’s in class_rw_t.

Look at this, it seems that there is nothing we want, then find a way, down to find:

This is what we need. To do this, we will add attributes and methods to SHPerson:

@interface SHPerson : NSObject
 @property (nonatomic, copy) NSString *name;
 @property (nonatomic) NSInteger age;
 @end
 @implementation SHPerson
 - (void)setNickname:(NSString *)name {
 }
 
+ (instancetype)person {
    return [[SHPerson alloc] init];
}
Copy the code

Get property_list_t

Run again and get class_rw_t again:

Property_array_t = $4; property_array_t = $4; Property_list_t = property_list_t property_list_t = property_list_t

There is nothing in the source, but it inherits from entsize_list_tt.

So there’s a get method that gets the element, so get calls getOrEnd, and it turns out that the implementation of getOrEnd is to get the element by memory translation

At this point, we try to get our property in the LLDB print using the get method, but because of the LLDB print, we have to run it again, and we know that the PTR type is property_list_t. When printing to property_array_t, You can force the PTR to be converted to property_list_t directly, or p *$6, otherwise you can’t print the relevant stuff in the LLDB using the GET method.

With LLDB printing, there really is something about attributes.

Get method_t

Methods () returns a method_array_t, gets method_list_t, which also inherits from entsize_list_tt, but prints a method_t with nothing in it:

Take a look at the structure of method_t:

So you can see that there’s a structure big, and there’s SEL name, const char *types, MethodListIMP IMP in big.

Look further down:

Try printing these three methods to see the effect:

Next, print out all the methods to see if they match the method we declared. In addition, I won’t print out the protocol methods here. In fact, it can be printed out, if you are interested in studying it.

The.cxx_destruct method was originally used for C++ object destruct, and ARC borrowed this method to insert code for automatic memory freeing.

Also, the + (InstanceType)person we declared was not found, indicating that the class method does not exist on the class object!

So where is the class method stored, in fact, the class method stored in the metaclass, the structure of the class and metaclass is the same, but the storage content is different, interested in can refer to the steps above, to obtain the metaclass stored in the class method.