Printing thearguments of obj-c function in DTrace/Instruments (was NSNotifications & background apps?)
Printing thearguments of obj-c function in DTrace/Instruments (was NSNotifications & background apps?)
- Subject: Printing thearguments of obj-c function in DTrace/Instruments (was NSNotifications & background apps?)
- From: dreamcat7 <email@hidden>
- Date: Mon, 16 Jun 2008 10:08:32 +0100
Hi,
I often see in instruments many recurring obj-c method calls. But
never able to peek at the data passed into them.
Wouldn't it be nice to see the arguments printed out for the obj-c
method calls ?
I was wondering if we can adapt the following method. This example
first is for logging NSNotifications.
On 15 Jun 2008, at 11:18, dreamcat7 wrote:
So if you put name = nil as the notification name you are
registering for then it will catch *all* notification from the
notification centre. Each notification can then be appended to an
NSArray by the notificationHook fn, along with timestamp and any
other relevant data (e.g. to know the application that sent it or a
'threaded view').
- (void)myNotificationHook:(NSNotification*)aNotification
{
[self.nsNotificationArray addObject:aNotification];
[self.timestampFornsNotificationArray addObject:[NSCalendarDate
date]];
}
And then watch the array with KVC/binding and all the notification
data can be printed to NSTableView or textual log. This can be
simply achieved with [object description] to print the NSString
representation of the @"name "@"object" and @"userInfo" parameters
of the NSNotification.
To send stuff over to Instruments 'a d-trace hook can be added into
your program'...
As explained in the following article
http://docs.sun.com/app/docs/doc/817-6223/chp-usdt?a=view
We can convert to 'c' strings and pass into the DTRACE_PROBE_ macro.
Macro will simply call a 'd' function declared in 'd' language file.
I am haven't tried this code, but it should work if Sun's example is
correct.
NSNotificationLogging.m:
- (void)notificationHook:(NSNotification*)aNotification
{
// Don't need these anymore
// [self.nsNotificationArray addObject:aNotification];
// [self.timestampFornsNotificationArray addObject:[NSCalendarDate
date]];
char* cstr_notificationName = [[aNotification name]
cStringUsingEncoding:NSASCIIStringEncoding];
char* cstr_notificationSender = [[[aNotification object] description]
cStringUsingEncoding:NSASCIIStringEncoding];
char* cstr_notificationUserInfo = [[[aNotification userInfo]
description] cStringUsingEncoding:NSASCIIStringEncoding];
DTRACE_PROBE3(receive_cstrs, query__receive3, cstr_notificationName,
cstr_notificationSender, cstr_notificationUserInfo);
dealloc(cstr_notificationName);
dealloc(cstr_notificationSender);
dealloc(cstr_notificationUserInfo);
}
receive_cstrs_trace.d:
provider receive_cstrs {
probe query__receive1(string);
probe query__receive2(string, string);
probe query__receive3(string, string, string);
probe query__receive4(string, string, string, string);
probe query__respond();
};
then Sun says to have this .d language file compiled and linked to the
executable we are analysing.
So for a file named "receive_cstrs.d"...
dtrace -G -32 -s receive_cstrs.d myObjCFramework.o / myObjCApp.o ...
cc -o myObjCApp receive_cstrs.o myObjCFramework.o / myObjCApp.o ...
Im not sure how to load a '.d' probe file into Instruments. We need it
to know about our probe definitions in receive_cstrs_trace.d.
Perhaps it is simply enough to create a new probe with the same name
and then use the predicate rules to specify the function name, number
of arguments, etc.
Here is a tutorial that deals very well with this part.
http://cocoasamurai.blogspot.com/2008/05/dtrace-for-cocoa-developers.html
...then probe function query__receive3(string, string, string)
function should appear in Instruments to have three arguments.
arg[0] , arg[1] and arg[2] respectively.
The args[0-9] can be chosen to print straight out to the 'Detail
View' ( one of the 3 'panes' on Instrument's main screen ).
So for what i originally wanted that output would be equivalent to the
Meow Notifications logging tool.
But alongside all the other Instruments output.
To get a printout of the arguments of any arbitrary obj-c method on
the stack. So it will appear in the Instruments' Details Panel:
We might override objc_MsgSend() by insterting a shim function. Print
the [-NSObject description] of each argument to the
DTRACE_PROBE_n_(...) macro like above.
But any obj-c method we call in objc_MsgSend() will re-enter back into
this function again. So we need a way to stop unwanted recursion of [-
NSObject description] and [-NSString cStringUsingEncoding:]. (And
also any other obj-c method those two method are calling). But i think
this can be possible.
And must also find a reasonable way of linking our shim function (and
the .d language file) to whichever App(s) we want to Trace.
Other people have tried overriding the objc_MsgSend(). So here is an
article about it.
http://www.dribin.org/dave/blog/archives/2006/04/22/tracing_objc/
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden