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: Tony Becker <email@hidden>
- Date: Sun, 9 Mar 2008 17:45:39 -0400
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...
On Mar 9, 2008, at 3:18 PM, Stuart Malin wrote:
Thanks Jon and Shem. I spaced out the existence of _cmd and was
unaware of __func__
As I best understand their distinction:
__func__ is generated at compile time (a C-string)
_cmd is part of the Obj-C runtime (a SEL)
Since I am writing a new method, I can accomplish my goal by passing
either _cmd or __func__.
BUT, just out of intellectual curiosity (and possible future need),
what if I have existing callers and can't modify their code to pass
either of these -- can I still get the identity of the invoking
method in my method?
Of course, that would be objc_msgSend. So... the one before that?
Which leads back to the stack.... and method signatures and frame
lengths, and ... I think I'm over my head.
Is there something else simple that I've missed? Or there is
documentation somewhere on these mechanics that I should be reading?
On Mar 8, 2008, at 11:14 PM, Sherm Pendley wrote:
On Sun, Mar 9, 2008 at 4:44 AM, Stuart Malin <email@hidden>
wrote:
I'd like to have a method determine the name of the method that
invoked it -- as an NSString.
For example
- (void) method1 {
[someObject method2];
}
- (void) method2 {
// here, I'd like to be able to find the name of the caller
// in this example, that would be "method1"
}
Seems to me the simplest thing to do would be to pass _cmd as an
argument, much like the caller of an IBAction method passes self:
- (void) method1 {
[someObject method2:_cmd];
}
- (void) method2: (SEL)callerSel {
NSLog(@"Caller's selector: %@", NSStringFromSelector(callerSel));
}
I have a suspicion that this is either trivial, and I'm just missing
the obvious, or its far from trivial and requires accessing the
stack and some
symbol table.
I think maybe you missed the existence of _cmd. Both self (this
object) and _cmd (this selector) are passed as implicit arguments
to every Objective-C method. Knowing that, forwarding them to
another method does indeed become rather trivial. :-)
On Mar 9, 2008, at 6:01 AM, Jonathan Dann wrote:
I think maybe you missed the existence of _cmd. Both self (this
object) and
_cmd (this selector) are passed as implicit arguments to every
Objective-C
method.
You can also call __func__ from within a method call, I use this
often,
NSLog(@"%p %s",self,__func__); // Thanks James Bucanek
_______________________________________________
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
_______________________________________________
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