2014-10-22 102 views

回答

3

This is the code I wrote to do it

以下步骤用于:

  1. NSProxy
  2. 继承调酒allocWithZone:目标类创建一个代理类,并与代理类
  3. 登录包裹返回的对象消息forwardInvocation:代理类

#import <objc/runtime.h> 

@interface XLCProxy : NSProxy 

+ (id)proxyWithObject:(id)obj; 

@end 

@implementation XLCProxy 
{ 
    id _obj; 
} 

+ (void)load 
{ 
    { 
     Class cls = NSClassFromString(@"IDESourceCodeDocument"); 
     id metacls = object_getClass(cls); 
     IMP imp = class_getMethodImplementation(metacls, @selector(allocWithZone:)); 
     IMP newimp = imp_implementationWithBlock(^id(id me, SEL cmd, NSZone *zone) { 
      id obj = ((id (*)(id,SEL,NSZone*))(imp))(me, cmd, zone); 
      return [XLCProxy proxyWithObject:obj]; 
     }); 
     BOOL success = class_addMethod(metacls, @selector(allocWithZone:), newimp, [[NSString stringWithFormat:@"@@:%s", @encode(NSZone*)] UTF8String]); 
     if (!success) { 
      NSLog(@"Add method failed"); 
     } 
    } 
} 

+ (id)proxyWithObject:(id)obj 
{ 
    XLCProxy *proxy = [self alloc]; 
    proxy->_obj = obj; 
    return proxy; 
} 

- (void)forwardInvocation:(NSInvocation *)invocation 
{ 
    const char *selname = sel_getName([invocation selector]); 

    [invocation setTarget:_obj]; 
    [invocation invoke]; 

    if ([@(selname) hasPrefix:@"init"] && [[invocation methodSignature] methodReturnType][0] == '@') { 
     const void * ret; 
     [invocation getReturnValue:&ret]; 
     ret = CFBridgingRetain([XLCProxy proxyWithObject:_obj]); 
     [invocation setReturnValue:&ret]; 
    } 


    NSLog(@"%@ %s", [_obj class], selname); 
// if ([[invocation methodSignature] methodReturnType][0] == '@') { 
//  NSObject __unsafe_unretained * obj; 
//  [invocation getReturnValue:&obj]; 
//  NSLog(@"%@", obj); 
// } 
} 

-(NSMethodSignature *)methodSignatureForSelector:(SEL)sel 
{ 
    return [_obj methodSignatureForSelector:sel]; 
} 

- (Class)class 
{ 
    return [_obj class]; 
} 

@end 
2

您可以使用DTrace。这里是一行:

sudo dtrace -q -n 'objc$target:YourClass::entry { printf("%c[%s %s]\n", probefunc[0], probemod, probefunc + 1); }' -p <the PID of the target process> 
相关问题