Objective-C print out call stack in your own method

Max
1 min readOct 25, 2016

Sometimes it’s useful to know which other method calls your method and it’s easy to do so in Objective C.

#include <execinfo.h>

void *addr[2];
int nframes = backtrace(addr, sizeof(addr)/sizeof(*addr));
if (nframes > 1) {
char **syms = backtrace_symbols(addr, nframes);
NSLog(@"%s: caller: %s", __func__, syms[1]);
free(syms);
} else {
NSLog(@"%s: *** Failed to generate backtrace.", __func__);
}

You can change the size of the array to print deeper levels.

From http://stackoverflow.com/questions/4046833/print-the-name-of-the-calling-function-to-the-debug-log

--

--