gcc -g -rdynamic main.c -o test
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <execinfo.h>
#include <signal.h>
void dump(int signo)
{
void *buffer[100] = {0};
size_t size;
char **strings = NULL;
int i = 0;
size = backtrace(buffer, 100);
fprintf(stderr, "Obtained %zd stack frames.nm\n", size);
strings = backtrace_symbols(buffer, size);
if (strings == NULL)
{
perror("backtrace_symbols.");
exit(EXIT_FAILURE);
}
for (i = 0; i < size; i++)
{
fprintf(stderr, "%d: %s\n", i, strings[i]);
}
free(strings);
strings = NULL;
exit(0);
}
void fun_namal1(void)
{
printf("fun_namal1\r\n");
}
void fun_namal2(void)
{
*((volatile char *)0x0) = 0x9999;
}
void func_c()
{
fun_namal1();
fun_namal2();
}
void func_b()
{
func_c();
}
void func_a()
{
func_b();
}
int main(int argc, const char *argv[])
{
printf("main\r\n");
if (signal(SIGSEGV, dump) == SIG_ERR)
perror("can't catch SIGSEGV");
func_a();
return 0;
}