结合文章http://blog.csdn.net/zahuopuboss/article/details/46519697
与信号处理
在程序出现段错的时候,打印函数调用链
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void trace(int signo)
{
int j, nptrs;
#define SIZE 100
void *buffer[100];
char **strings;
printf("signo: %d\n", signo);
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);
if (SIGSEGV == signo || SIGQUIT == signo) {
exit(0);