there is a glibc backtrace function. An example call is shown on the manual page:
#define SIZE 100 void myfunc3(void) { int j, nptrs; void *buffer[100]; char **strings; nptrs = backtrace(buffer, SIZE); printf("backtrace() returned %d addresses\n", nptrs); /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) would produce similar output to the following: */ strings = backtrace_symbols(buffer, nptrs); if (strings == NULL) { perror("backtrace_symbols"); exit(EXIT_FAILURE); } for (j = 0; j < nptrs; j++) printf("%s\n", strings[j]); free(strings); }
See the man page for more context.
itโs hard to determine if this really works from a signal handler, since posix lists only a few return functions that are guaranteed to work. Remember: a signal handler can be called while the rest of your process is right in the middle of a malloc call.
I suppose this usually works, but it may fail from time to time. This may be enough for debugging.
source share