I'm surprised why everybody says it is not possible. It is possible on Linux for non-static functions.
I know at least two ways to achieve this.
There are GNU functions for backtrace printing: backtrace()
and backtrace_symbols()
(See man
). In your case you don't need backtrace()
as you already have function pointer, you just pass it to backtrace_symbols()
.
Example (working code):
#include <stdio.h>
#include <execinfo.h>
void foo(void) {
printf("foo\n");
}
int main(int argc, char *argv[]) {
void *funptr = &foo;
backtrace_symbols_fd(&funptr, 1, 1);
return 0;
}
Compile with gcc test.c -rdynamic
Output: ./a.out(foo+0x0)[0x8048634]
It gives you binary name, function name, pointer offset from function start and pointer value so you can parse it.
Another way is to use dladdr()
(another extension), I guess print_backtrace()
uses dladdr()
. dladdr()
returns Dl_info
structure that has function name in dli_sname
field. I don't provide code example here but it is obvious - see man dladdr
for details.
NB! Both approaches require function to be non-static!
Well, there is one more way - use debug information using libdwarf
but it would require unstripped binary and not very easy to do so I don't recommend it.