facebook / chisel

Chisel is a collection of LLDB commands to assist debugging iOS apps.
MIT License
9.13k stars 803 forks source link

Add evalfile command #248

Open longv2go opened 6 years ago

longv2go commented 6 years ago

evalfile command can evaluate a multiline source code from a file. You can also supply a argument after the file parameter.

// /path/to/test.m

@import ObjectiveC.runtime;

NSMutableArray *result = (id)[NSMutableArray array];
unsigned int count;
objc_property_t *props = (objc_property_t *)class_copyPropertyList((Class)$arg0, &count);
for (int i = 0; i < count; i++) {
    char *name = (char *)property_getName(props[i]);
    [result addObject:(id)[NSString stringWithUTF8String:name]];
}
RETURN(result);
lldb> p/x (char*)NSClassFromString(@"UIView")
(char *) $35 = 0x00000001119a7ff8
lldb> evalfile  /path/to/test.m 0x00000001119a7ff8
<__NSArrayM 0x608000057040>(
hash,
superclass,
description,
debugDescription,
hash,
superclass,
description,
......
)
kastiglione commented 6 years ago

Thanks for the pull request. I did something similar recently, but without the arguments. Have you tried wrapping the code in a function? Or alternatively, a class method or a category? For example:

@import ObjectiveC.runtime;

@implementation NSObject (ObjectProperties)

+ (NSArray *)propertyNames {
    NSMutableArray *result = (id)[NSMutableArray array];
    unsigned int count;
    objc_property_t *props = (objc_property_t *)class_copyPropertyList(self, &count);
    for (int i = 0; i < count; i++) {
        char *name = (char *)property_getName(props[i]);
        [result addObject:(id)[NSString stringWithUTF8String:name]];
    }
    return result;
}

@end

And then from lldb:

lldb> evalfile /path/to/test.m
lldb> po [UIView propertyNames]