Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out who called a method?

Example: When my method -fooBar gets called, I want it to log in the console which other method of which other class called it.

Right now, I only know how to log the method name of fooBar itself and it's class, with this:

_cmd

[self class]

Is this possible to figure out?

like image 663
Another Registered User Avatar asked Nov 25 '09 00:11

Another Registered User


People also ask

How do you find out who called a method in Java?

Sometimes it is nice to access the caller of a method. Doing this is relatively simple, you can use the getStackTrace() method on the current thread.

How do you find out who called a function in Python?

use sys. exc_info to get Traceback frame ( tb_frame ). from tb_frame get last caller's frame using f_back . from last caller's frame get the code object that was being executed in that frame.

How do you find out the caller function in JavaScript?

To find out the caller function, name a non-standard the function. caller property in JavaScript. The Function object is replaced by the name of the function of which you need to find out the parent function name. If the function Welcome was invoked by the top-level code, the value of Welcome.

What is the caller of the function?

A caller is a function that calls another function; a callee is a function that was called. The currently-executing function is a callee, but not a caller.


2 Answers

In fully optimized code, there is no 100% surefire way to determine the caller to a certain method. The compiler may employ a tail call optimization whereas the compiler effectively re-uses the caller's stack frame for the callee.

To see an example of this, set a breakpoint on any given method using gdb and look at the backtrace. Note that you don't see objc_msgSend() before every method call. That is because objc_msgSend() does a tail call to each method's implementation.

While you could compile your application non-optimized, you would need non-optimized versions of all of the system libraries to avoid just this one problem.

And this is just but one problem; in effect, you are asking "how do I re-invent CrashTracer or gdb?". A very hard problem upon which careers are made. Unless you want "debugging tools" to be your career, I would recommend against going down this road.

What question are you really trying to answer?

like image 153
bbum Avatar answered Nov 03 '22 23:11

bbum


How about this:

NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];

NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString  componentsSeparatedByCharactersInSet:separatorSet]];
[array removeObject:@""];

NSLog(@"Class caller = %@", [array objectAtIndex:3]);
NSLog(@"Method caller = %@", [array objectAtIndex:4]);

Credits to the original author, intropedro.

like image 33
Maxim Chetrusca Avatar answered Nov 03 '22 23:11

Maxim Chetrusca