Print name of the calling function

Use:

#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__);
}

And, getting fancy, the name of the caller of the calling function:

#include <execinfo.h>

void *addr[3];
int nframes = backtrace(addr, sizeof(addr) / sizeof(*addr));
char **syms = backtrace_symbols(addr, nframes);
NSLog(@"%s: caller of caller: %s", __func__, syms[2]);

Thanks to H2CO3.

http://stackoverflow.com/questions/16122870/print-name-of-the-caller-of-the-calling-function

Leave a Reply

Your email address will not be published. Required fields are marked *