#include "common.h" void dump_backtrace(void) { int j, nptrs; void *buffer[32]; char **strings; nptrs = backtrace(buffer, ARRAY_SIZE(buffer) ); printf("backtrace() returned %d addresses:\n", nptrs); strings = backtrace_symbols(buffer, nptrs); if (NULL == strings) { perror("backtrace_symbols"); exit(-1); } for (j = 0; j < nptrs; j++) { printf(" %s\n", strings[j]); } free(strings); }
#include "common.h" void test() { *((int*)0x04) = 0; } void siginfo_handler(int signum, siginfo_t * siginfo, void * context) { LOG("Signal Number: %d\n", signum); LOG("Address: %p\n", siginfo->si_addr); dump_backtrace(); exit(-1); } int main() { int ret = 0; struct sigaction act; act.sa_flags = SA_SIGINFO; act.sa_sigaction = siginfo_handler; sigemptyset(&act.sa_mask); ret = sigaction(SIGSEGV, &act, NULL); if(0 != ret) { perror("sigaction() failed"); } test(); return 0; }