for C:
(1)正解:
mipsel-linux-gcc backt.c -g -rdynamic -fexceptions
原因:
-fexceptions
Enable exception handling. Generates extra code needed to propagate exceptions. For some targets, this implies GCC generates frame unwind information for all functions, which can produce significant data size overhead, although it does not affect execution.If you do not specify this option,GCC enables it by default for languages like C++ that normally require exception handling, and disables it for languages like C that do not normally require it. However, you may need to enable this option when compiling C code that needs to interoperate properly with exception handlers written in C++. You may also wish to disable this option if you are compiling older C++ programs that don't use exception handling.
(2)
mipsel-linux-gcc backt.c -g -rdynamic -funwind-tables
(3)
mipsel-linux-gcc backt.c -g -rdynamic -fasynchronous-unwind-tables
for C++:
mipsel-linux-g++ backt.c -g -rdynamic
以下是backt.c的实现:
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
void print_bt()
{
void *array[10];
size_t size;
char **strings;
size_t i;
size = backtrace (array, 10);
strings = backtrace_symbols (array, size);
printf ("*****Obtained %zd stack frames.*****\n", size);
for (i = 0; i < size; i++)
printf ("%s\n", strings[i]);
free (strings);
}
//段错误处理函数
void dump(int signo)
{
printf("[*****Program received signal SIGSEGV, Segmentation fault.id=%d*****]\n", signo);
print_bt();
exit(0);
}
void fun3()
{
print_bt();
}
void fun2()
{
fun3();
}
void fun1()
{
fun2();
}
int main(int argc, char *argv[])
{
signal(SIGSEGV, &dump);
signal(SIGABRT, &dump);
fun1();
return 0;
}
参考文献:
http://sourceware.org/ml/libc-ports/2006-04/msg00009.html
http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html