Skip to content

Instantly share code, notes, and snippets.

@markd2
Created February 21, 2013 14:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save markd2/5005201 to your computer and use it in GitHub Desktop.
Save markd2/5005201 to your computer and use it in GitHub Desktop.
On-demand Objective-C message tracing
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -framework Foundation -o array array.m
void observeObject (id objectToWatch) {
// Just a stub to give DTrace something to hook in to.
}
int main (void) {
@autoreleasepool {
NSArray *thing1 = @[ @1, @2, @3, @4];
observeObject (thing1);
[thing1 enumerateObjectsUsingBlock: ^(id thing, NSUInteger index, BOOL *whoa) {
NSLog (@"Saw %@", thing);
}];
observeObject (nil);
NSLog (@"it's got %ld elements", thing1.count);
}
return 0;
} // main
pid$target::objc_msgSend:entry
/arg0 == 0/
{
@stacks[ustack(5)] = count();
}
END
{
trunc (@stacks, 20);
}
pid$target::objc_msgSend:entry
/arg0 == 0/
{
ustack ();
}
/* run with
* # dtrace -qs watch-array.d -c ./array
*/
int64_t observedObject;
pid$target::observeObject:entry
{
printf ("observing object: %p\n", arg0);
observedObject = arg0;
}
objc$target:::entry
/arg0 == observedObject/
{
printf("%s %s\n", probemod, probefunc);
/* uncomment to print stack:
* ustack();
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment