#include <execinfo.h> #include <iostream> #include <signal.h> void map_dump(void) { char buff[64] = {0x00}; sprintf(buff, "cat /proc/%d/maps", getpid()); system((const char *)buff); } // 打印堆栈信息 void trace_dump(void) { int j, nptrs; void *buffer[100]; char **strings; #define SIZE 1024 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); } void signal_handler(int signo) { map_dump(); printf("\n=========>>>catch signal %d <<<=========\n", signo); printf("Dump stack start...\n"); trace_dump(); printf("Dump stack end...\n"); signal(signo, SIG_DFL); /* 恢复信号默认处理 */ raise(signo); /* 重新发送信号 */ } void trigger_crash() { int *p = nullptr; *p = 0; } int main(int argc, char *argv[]) { signal(SIGSEGV, signal_handler); signal(SIGABRT, signal_handler); signal(SIGINT, signal_handler); trigger_crash(); // done: return 0; }
标签:nptrs,dump,错误,buffer,backtrace,void,跟踪,strings From: https://www.cnblogs.com/jiyong3998/p/17775567.html