views:

125

answers:

4

How do I put a "hook" on an object so I can see what messages are being sent to it? (ie do an NSLog() every time a message is sent to an object).

I think recall seeing this done before but I forget how. I am thinking it might help me track down why part of my code is not working.

A: 

You could use a NSProxy object and override the forwardInvocation: method.

teabot
+4  A: 

The best way to do this is with dtrace or an instruments script. Using dtrace you can do the following:

Write the following script as objc-calls.d

#pragma D option quiet
objc$target:::entry
{
   printf("%s %s\n", probemod, probefunc);
}

Then run the app using the script:

setenv DYLD_SHARED_REGION avoid
sudo dtrace -s objc-calls.d -c /Path/To/Your/App/Binary

You can also build a custom Instrument using a similiar dtrace probe.

Louis Gerbarg
I have bookmarked your help, its helpful, but the other one provides a quicker/easier solution. Thanks.
Jacob
+4  A: 

You can also use objective-c forwarding. Basically you can create a proxy object that logs the methods then forwards the call to the original. See my blog post for more details.

@interface LoggerProxy : NSObject
{
    id original;
}

- (id)initWithOriginal:(id) value;

@end
@implementation LoggerProxy

- (id) initWithOriginal:(id)value
{
    if (self = [super init]) {
        original = value;
    }
    return self;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
    NSMethodSignature *sig = [super methodSignatureForSelector:sel];
    if(!sig)
    {
        sig = [original methodSignatureForSelector:sel];
    }
    return sig;
}

- (void)forwardInvocation:(NSInvocation *)inv
{
    NSLog(@"[%@ %@] %@ %@", original, inv,[inv methodSignature],
         NSStringFromSelector([inv selector]));
    [inv invokeWithTarget:original];
}

@end
mfazekas
+1  A: 

Following up on Louis Gerbarg's comment from last year, it's also useful to be able to easily filter by class name.

Try the following D script:

 #!/usr/sbin/dtrace -s
 #pragma D option quiet
 objc$target:::entry
 /strstr(probemod,$$1) != NULL/
 {
     printf("%s %s\n", probemod, probefunc);
 }

Save it, chmod a+x objc-calls.d, and then do sudo objc-calls.d -c /Your/Binary NSObject to see just the invocations related to NSObject (and its categories).

smokris