How to find out who called a method?

9 Solutions Collect From Internet About “How to find out who called a method?”

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?

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.

It’s not possible in the general case without actually walking the stack. There’s not even a guarantee that another object send the message that called the method. For example, it could be called from a block in a signal handler.

See backtrace(3).

User the below method

Pass index for which you want to display method and pass -1 if you want to display full stack of method

+(void) methodAtIndex:(int)index{
    void* callstack[128];
    int frames = backtrace(callstack, 128);
    char** strs = backtrace_symbols(callstack, frames);

    if (index == -1) {
        for (int i = 0; i < frames; ++i) {
            printf("%s\n", strs[i]);
    else {
        if (index < frames) {
            printf("%s\n", strs[index]);


This information can be obtained using DTrace.

Make a macro that adds the __FUNCTION__ to the function name to the function call. This macro will then call your function with an extra parameter of a char* to the target function.

NSLog(@"Show stack trace: %@", [NSThread callStackSymbols]);

I was trying to catch who, how and when changes window’s size and did some handwork:

- (void)logWindowWidth:(NSString *)whoCalls {
   NSLog(@"%@", whoCalls);
   NSLog(@"self.window.size.width %f", self.window.size.width);

-(void)someMethod {
  [self logWindowWidth:@"someMethod - before"];
  [self logWindowWidth:@"someMethod - after"];

-(void)anotherMethod {
  [self logWindowWidth:@"anotherMethod - before"];
  [self logWindowWidth:@"anotherMethod - after"];