首先要说明的是:
linux 下 比较方便可以得到 崩溃时的调用栈,win下 比较难办
1. linux 获取调用栈
代码奉上:
1 #include <execinfo.h> //在头文件"execinfo.h"中声明了三个函数用于获取当前线程的函数调用堆栈 2 #include <fcntl.h> 3 #include <sys/resource.h> 4 5 #include <signal.h> 6 static int _core_dump_signals[] = { 7 SIGABRT, SIGFPE, SIGILL, SIGQUIT, SIGSEGV, 8 SIGTRAP, SIGSYS, SIGBUS, SIGXCPU, SIGXFSZ 9 #ifdef SIGEMT 10 ,SIGEMT 11 #endif 12 }; 13 //安装崩溃时//打印当前线程的函数调用堆栈 14 void installTrace() 15 { 16 struct sigaction act; 17 size_t i; 18 memset(&act, 0, sizeof(act)); 19 20 act.sa_handler = &CLog::LogTrace; 21 sigfillset(&act.sa_mask); 22 23 for (i = 0; i < (sizeof(_core_dump_signals) / sizeof((_core_dump_signals)[0])); i++) 24 sigaction(_core_dump_signals[i], &act, NULL); 25 /* unblock all the signals, because if the current process is 26 * spawned in the previous signal handler, all the signals are 27 * blocked. In order to make it sense of signals, we should 28 * unblock them. Certainly, you should call this function as 29 * early as possible. :) */ 30 sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL); 31 32 //sigemptyset(&myAction.sa_mask); 33 //myAction.sa_flags = SA_RESTART | SA_SIGINFO; 34 //LOG(LOGLV_WARN, "Log::installTrace!!! "); 35 } 36 //#include <sys/wait.h> 37 ////等待fok的子进程信号,防止僵尸进程 38 //void fok_chld_wait(int signo) { 39 // pid_t pid; 40 // int stat; 41 // while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) { 42 // printf("child %d exit ", pid); 43 // } 44 // return; 45 //} 46 ////设置等待fok的子进程信号 47 //int set_fok_chld_wait_signal() { 48 // struct sigaction act, oact; 49 // act.sa_handler = fok_chld_wait; 50 // sigemptyset(&act.sa_mask); 51 // act.sa_flags = 0; 52 // 53 // if (sigaction(SIGCHLD, &act, &oact) < 0) { 54 // return -1; 55 // } 56 // return 0; 57 //} 58 //重新启动进程 59 static void _restart(void) 60 { 61 char buf[4096]; 62 char exe[512]; 63 char **argv; 64 int fd, len; 65 size_t argv_size, i; 66 char *ptr, *ptr_end; 67 struct rlimit limit; 68 69 /* find the file executable. */ 70 len = readlink("/proc/self/exe", exe, sizeof(exe) - 1); 71 if (len == -1 || len == sizeof(exe) - 1) 72 _exit(EXIT_FAILURE); 73 exe[len] = '