Last active
December 22, 2015 21:19
-
-
Save vietor/6532884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <unistd.h> | |
#include <execinfo.h> | |
static void signal_dump(int sig) | |
{ | |
FILE* fp; | |
time_t curTime; | |
struct tm* pTm, xTm; | |
void* array[20]; | |
char **strings, filename[1024]; | |
size_t i, size; | |
struct sigaction act, oact; | |
time(&curTime); | |
pTm = localtime_r(&curTime, &xTm); | |
sprintf(filename, "%04d-%02d-%02d-stackdump.log", pTm->tm_year + 1900, pTm->tm_mon + 1, pTm->tm_mday); | |
fp = fopen(filename, "a"); | |
if(fp) { | |
fprintf(fp, "%02d:%02d:%02d PID[%d] Exception: %s\nStack trace:\n", pTm->tm_hour, pTm->tm_min, pTm->tm_sec, (int)getpid(), strsignal(sig)); | |
size = backtrace(array, sizeof(array) / sizeof(array[0])); | |
strings = backtrace_symbols(array, size); | |
for(i = 0; i < size; i++) { | |
fprintf(fp, "%s\n", strings[i]); | |
} | |
fclose(fp); | |
} | |
memset(&act, 0, sizeof(act)); | |
act.sa_handler = SIG_DFL; | |
act.sa_flags = 0; | |
sigaction(sig, &act, &oact); | |
raise(sig); | |
} | |
void register_signal_dump() | |
{ | |
size_t i; | |
struct sigaction act, oact; | |
static int signals[] = {SIGSEGV, SIGABRT, SIGBUS, SIGFPE, SIGSTKFLT}; | |
static int ignore_signals[] = {SIGPIPE}; | |
for(i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) { | |
memset(&act, 0, sizeof(act)); | |
act.sa_handler = signal_dump; | |
act.sa_flags = 0; | |
sigaction(signals[i], &act, &oact); | |
} | |
for(i = 0; i < sizeof(ignore_signals) / sizeof(ignore_signals[0]); i++) { | |
memset(&act, 0, sizeof(act)); | |
act.sa_handler = SIG_IGN; | |
act.sa_flags = 0; | |
sigaction(ignore_signals[i], &act, &oact); | |
} | |
} | |
/* | |
cat 2013-09-12-stackdump.log| grep "\[0x"| awk -F[ '{print $2}'| awk -F] '{print $1}' | xargs addr2line $1 -e demo | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment