Re: How to get the name of a method at runtime?
Re: How to get the name of a method at runtime?
- Subject: Re: How to get the name of a method at runtime?
- From: Stuart Malin <email@hidden>
- Date: Sun, 9 Mar 2008 13:29:54 -1000
Interesting approach, Tony.
However, __builtin_return_address isn't an object, so
stringWithFormat throws an exception when given the %@ token. It
should be %u (or %U ?? - I'm not sure of the difference).
So, just so I could step through the code in the debugger, I wrote:
int pid = [[NSProcessInfo processInfo] processIdentifier];
unsigned long retAddr = (unsigned long)__builtin_return_address(0);
NSString *str = [NSString stringWithFormat:@"/usr/bin/atos -p %d %
u", pid, retAddr];
which does work.
Mike & b.bum -- thanks for the warning -- my code isn't fragile
because it doesn't depend on such functionality. The way I am
handling my functionality is by having the caller pass in _cmd. This
further investigation was just for my edification to better
understand method invocation mechanics. I hope the interest
stimulated was useful to others.
On Mar 9, 2008, at 11:45 AM, Tony Becker wrote:
If you're on Leopard, there is a new backtrace(3) call.
If you're on Tiger, it's a little more complex...
You can use the compiler function
(long)__builtin_return_address(0)
to find the address of your caller. Alas, there is no
__builtin_return_symbol()
Something like the following will print out the calling function.
NSString *str = [NSString stringWithFormat:@"/usr/bin/atos -p %d
%@",
[[NSProcessInfo processInfo] processIdentifier],
__builtin_return_address(0)];
FILE *file = popen( [str UTF8String], "r" );
if( file ) {
char buffer[512];
size_t length;
while( (length = fread( buffer, 1, sizeof( buffer ),
file )) ) {
fwrite( buffer, 1, length, stderr );
}
pclose( file );
}
Note that is is REALLY heavy, but it does work...
_______________________________________________
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