1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-20 06:53:52 +00:00

Changed crashcatcher's uname system info retrieval and cleaned indentation

This commit is contained in:
pvdk 2014-01-04 21:46:38 +01:00
parent 356b53bd22
commit 92234cf783

View file

@ -6,6 +6,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/ucontext.h> #include <sys/ucontext.h>
#include <sys/utsname.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
@ -42,78 +43,78 @@ static char altstack[SIGSTKSZ];
static struct { static struct {
int signum; int signum;
pid_t pid; pid_t pid;
int has_siginfo; int has_siginfo;
siginfo_t siginfo; siginfo_t siginfo;
char buf[1024]; char buf[1024];
} crash_info; } crash_info;
static const struct { static const struct {
const char *name; const char *name;
int signum; int signum;
} signals[] = { } signals[] = {
{ "Segmentation fault", SIGSEGV }, { "Segmentation fault", SIGSEGV },
{ "Illegal instruction", SIGILL }, { "Illegal instruction", SIGILL },
{ "FPU exception", SIGFPE }, { "FPU exception", SIGFPE },
{ "System BUS error", SIGBUS }, { "System BUS error", SIGBUS },
{ NULL, 0 } { NULL, 0 }
}; };
static const struct { static const struct {
int code; int code;
const char *name; const char *name;
} sigill_codes[] = { } sigill_codes[] = {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
{ ILL_ILLOPC, "Illegal opcode" }, { ILL_ILLOPC, "Illegal opcode" },
{ ILL_ILLOPN, "Illegal operand" }, { ILL_ILLOPN, "Illegal operand" },
{ ILL_ILLADR, "Illegal addressing mode" }, { ILL_ILLADR, "Illegal addressing mode" },
{ ILL_ILLTRP, "Illegal trap" }, { ILL_ILLTRP, "Illegal trap" },
{ ILL_PRVOPC, "Privileged opcode" }, { ILL_PRVOPC, "Privileged opcode" },
{ ILL_PRVREG, "Privileged register" }, { ILL_PRVREG, "Privileged register" },
{ ILL_COPROC, "Coprocessor error" }, { ILL_COPROC, "Coprocessor error" },
{ ILL_BADSTK, "Internal stack error" }, { ILL_BADSTK, "Internal stack error" },
#endif #endif
{ 0, NULL } { 0, NULL }
}; };
static const struct { static const struct {
int code; int code;
const char *name; const char *name;
} sigfpe_codes[] = { } sigfpe_codes[] = {
{ FPE_INTDIV, "Integer divide by zero" }, { FPE_INTDIV, "Integer divide by zero" },
{ FPE_INTOVF, "Integer overflow" }, { FPE_INTOVF, "Integer overflow" },
{ FPE_FLTDIV, "Floating point divide by zero" }, { FPE_FLTDIV, "Floating point divide by zero" },
{ FPE_FLTOVF, "Floating point overflow" }, { FPE_FLTOVF, "Floating point overflow" },
{ FPE_FLTUND, "Floating point underflow" }, { FPE_FLTUND, "Floating point underflow" },
{ FPE_FLTRES, "Floating point inexact result" }, { FPE_FLTRES, "Floating point inexact result" },
{ FPE_FLTINV, "Floating point invalid operation" }, { FPE_FLTINV, "Floating point invalid operation" },
{ FPE_FLTSUB, "Subscript out of range" }, { FPE_FLTSUB, "Subscript out of range" },
{ 0, NULL } { 0, NULL }
}; };
static const struct { static const struct {
int code; int code;
const char *name; const char *name;
} sigsegv_codes[] = { } sigsegv_codes[] = {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
{ SEGV_MAPERR, "Address not mapped to object" }, { SEGV_MAPERR, "Address not mapped to object" },
{ SEGV_ACCERR, "Invalid permissions for mapped object" }, { SEGV_ACCERR, "Invalid permissions for mapped object" },
#endif #endif
{ 0, NULL } { 0, NULL }
}; };
static const struct { static const struct {
int code; int code;
const char *name; const char *name;
} sigbus_codes[] = { } sigbus_codes[] = {
#ifndef __FreeBSD__ #ifndef __FreeBSD__
{ BUS_ADRALN, "Invalid address alignment" }, { BUS_ADRALN, "Invalid address alignment" },
{ BUS_ADRERR, "Non-existent physical address" }, { BUS_ADRERR, "Non-existent physical address" },
{ BUS_OBJERR, "Object specific hardware error" }, { BUS_OBJERR, "Object specific hardware error" },
#endif #endif
{ 0, NULL } { 0, NULL }
}; };
static int (*cc_user_info)(char*, char*); static int (*cc_user_info)(char*, char*);
@ -121,314 +122,318 @@ static int (*cc_user_info)(char*, char*);
static void gdb_info(pid_t pid) static void gdb_info(pid_t pid)
{ {
char respfile[64]; char respfile[64];
char cmd_buf[128]; char cmd_buf[128];
FILE *f; FILE *f;
int fd; int fd;
/* Create a temp file to put gdb commands into */ /* Create a temp file to put gdb commands into */
strcpy(respfile, "gdb-respfile-XXXXXX"); strcpy(respfile, "gdb-respfile-XXXXXX");
if((fd=mkstemp(respfile)) >= 0 && (f=fdopen(fd, "w")) != NULL) if((fd=mkstemp(respfile)) >= 0 && (f=fdopen(fd, "w")) != NULL)
{ {
fprintf(f, "attach %d\n" fprintf(f, "attach %d\n"
"shell echo \"\"\n" "shell echo \"\"\n"
"shell echo \"* Loaded Libraries\"\n" "shell echo \"* Loaded Libraries\"\n"
"info sharedlibrary\n" "info sharedlibrary\n"
"shell echo \"\"\n" "shell echo \"\"\n"
"shell echo \"* Threads\"\n" "shell echo \"* Threads\"\n"
"info threads\n" "info threads\n"
"shell echo \"\"\n" "shell echo \"\"\n"
"shell echo \"* FPU Status\"\n" "shell echo \"* FPU Status\"\n"
"info float\n" "info float\n"
"shell echo \"\"\n" "shell echo \"\"\n"
"shell echo \"* Registers\"\n" "shell echo \"* Registers\"\n"
"info registers\n" "info registers\n"
"shell echo \"\"\n" "shell echo \"\"\n"
"shell echo \"* Backtrace\"\n" "shell echo \"* Backtrace\"\n"
"thread apply all backtrace full\n" "thread apply all backtrace full\n"
"detach\n" "detach\n"
"quit\n", pid); "quit\n", pid);
fclose(f); fclose(f);
/* Run gdb and print process info. */ /* Run gdb and print process info. */
snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile); snprintf(cmd_buf, sizeof(cmd_buf), "gdb --quiet --batch --command=%s", respfile);
printf("Executing: %s\n", cmd_buf); printf("Executing: %s\n", cmd_buf);
fflush(stdout); fflush(stdout);
system(cmd_buf); system(cmd_buf);
/* Clean up */ /* Clean up */
remove(respfile); remove(respfile);
} }
else else
{ {
/* Error creating temp file */ /* Error creating temp file */
if(fd >= 0) if(fd >= 0)
{ {
close(fd); close(fd);
remove(respfile); remove(respfile);
} }
printf("!!! Could not create gdb command file\n"); printf("!!! Could not create gdb command file\n");
} }
fflush(stdout); fflush(stdout);
} }
static void sys_info(void) static void sys_info(void)
{ {
#ifdef __unix__ #ifdef __unix__
system("echo \"System: `uname -a`\""); struct utsname info;
putchar('\n'); if(!uname(&info))
fflush(stdout); printf("!!! Failed to get system information\n");
else
printf("System: %s %s %s %s %s\n",
info.sysname, info.nodename, info.release, info.version, info.machine);
fflush(stdout);
#endif #endif
} }
static size_t safe_write(int fd, const void *buf, size_t len) static size_t safe_write(int fd, const void *buf, size_t len)
{ {
size_t ret = 0; size_t ret = 0;
while(ret < len) while(ret < len)
{ {
ssize_t rem; ssize_t rem;
if((rem=write(fd, (const char*)buf+ret, len-ret)) == -1) if((rem=write(fd, (const char*)buf+ret, len-ret)) == -1)
{ {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
break; break;
} }
ret += rem; ret += rem;
} }
return ret; return ret;
} }
static void crash_catcher(int signum, siginfo_t *siginfo, void *context) static void crash_catcher(int signum, siginfo_t *siginfo, void *context)
{ {
//ucontext_t *ucontext = (ucontext_t*)context; //ucontext_t *ucontext = (ucontext_t*)context;
pid_t dbg_pid; pid_t dbg_pid;
int fd[2]; int fd[2];
/* Make sure the effective uid is the real uid */ /* Make sure the effective uid is the real uid */
if(getuid() != geteuid()) if(getuid() != geteuid())
{ {
raise(signum); raise(signum);
return; return;
} }
safe_write(STDERR_FILENO, fatal_err, sizeof(fatal_err)-1); safe_write(STDERR_FILENO, fatal_err, sizeof(fatal_err)-1);
if(pipe(fd) == -1) if(pipe(fd) == -1)
{ {
safe_write(STDERR_FILENO, pipe_err, sizeof(pipe_err)-1); safe_write(STDERR_FILENO, pipe_err, sizeof(pipe_err)-1);
raise(signum); raise(signum);
return; return;
} }
crash_info.signum = signum; crash_info.signum = signum;
crash_info.pid = getpid(); crash_info.pid = getpid();
crash_info.has_siginfo = !!siginfo; crash_info.has_siginfo = !!siginfo;
if(siginfo) if(siginfo)
crash_info.siginfo = *siginfo; crash_info.siginfo = *siginfo;
if(cc_user_info) if(cc_user_info)
cc_user_info(crash_info.buf, crash_info.buf+sizeof(crash_info.buf)); cc_user_info(crash_info.buf, crash_info.buf+sizeof(crash_info.buf));
/* Fork off to start a crash handler */ /* Fork off to start a crash handler */
switch((dbg_pid=fork())) switch((dbg_pid=fork()))
{ {
/* Error */ /* Error */
case -1: case -1:
safe_write(STDERR_FILENO, fork_err, sizeof(fork_err)-1); safe_write(STDERR_FILENO, fork_err, sizeof(fork_err)-1);
raise(signum); raise(signum);
return; return;
case 0: case 0:
dup2(fd[0], STDIN_FILENO); dup2(fd[0], STDIN_FILENO);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
execl(argv0, argv0, crash_switch, NULL); execl(argv0, argv0, crash_switch, NULL);
safe_write(STDERR_FILENO, exec_err, sizeof(exec_err)-1); safe_write(STDERR_FILENO, exec_err, sizeof(exec_err)-1);
_exit(1); _exit(1);
default: default:
#ifdef __linux__ #ifdef __linux__
prctl(PR_SET_PTRACER, dbg_pid, 0, 0, 0); prctl(PR_SET_PTRACER, dbg_pid, 0, 0, 0);
#endif #endif
safe_write(fd[1], &crash_info, sizeof(crash_info)); safe_write(fd[1], &crash_info, sizeof(crash_info));
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
/* Wait; we'll be killed when gdb is done */ /* Wait; we'll be killed when gdb is done */
do { do {
int status; int status;
if(waitpid(dbg_pid, &status, 0) == dbg_pid && if(waitpid(dbg_pid, &status, 0) == dbg_pid &&
(WIFEXITED(status) || WIFSIGNALED(status))) (WIFEXITED(status) || WIFSIGNALED(status)))
{ {
/* The debug process died before it could kill us */ /* The debug process died before it could kill us */
raise(signum); raise(signum);
break; break;
} }
} while(1); } while(1);
} }
} }
static void crash_handler(const char *logfile) static void crash_handler(const char *logfile)
{ {
const char *sigdesc = ""; const char *sigdesc = "";
int i; int i;
if(fread(&crash_info, sizeof(crash_info), 1, stdin) != 1) if(fread(&crash_info, sizeof(crash_info), 1, stdin) != 1)
{ {
fprintf(stderr, "!!! Failed to retrieve info from crashed process\n"); fprintf(stderr, "!!! Failed to retrieve info from crashed process\n");
exit(1); exit(1);
} }
/* Get the signal description */ /* Get the signal description */
for(i = 0;signals[i].name;++i) for(i = 0;signals[i].name;++i)
{ {
if(signals[i].signum == crash_info.signum) if(signals[i].signum == crash_info.signum)
{ {
sigdesc = signals[i].name; sigdesc = signals[i].name;
break; break;
} }
} }
if(crash_info.has_siginfo) if(crash_info.has_siginfo)
{ {
switch(crash_info.signum) switch(crash_info.signum)
{ {
case SIGSEGV: case SIGSEGV:
for(i = 0;sigsegv_codes[i].name;++i) for(i = 0;sigsegv_codes[i].name;++i)
{ {
if(sigsegv_codes[i].code == crash_info.siginfo.si_code) if(sigsegv_codes[i].code == crash_info.siginfo.si_code)
{ {
sigdesc = sigsegv_codes[i].name; sigdesc = sigsegv_codes[i].name;
break; break;
} }
} }
break; break;
case SIGFPE: case SIGFPE:
for(i = 0;sigfpe_codes[i].name;++i) for(i = 0;sigfpe_codes[i].name;++i)
{ {
if(sigfpe_codes[i].code == crash_info.siginfo.si_code) if(sigfpe_codes[i].code == crash_info.siginfo.si_code)
{ {
sigdesc = sigfpe_codes[i].name; sigdesc = sigfpe_codes[i].name;
break; break;
} }
} }
break; break;
case SIGILL: case SIGILL:
for(i = 0;sigill_codes[i].name;++i) for(i = 0;sigill_codes[i].name;++i)
{ {
if(sigill_codes[i].code == crash_info.siginfo.si_code) if(sigill_codes[i].code == crash_info.siginfo.si_code)
{ {
sigdesc = sigill_codes[i].name; sigdesc = sigill_codes[i].name;
break; break;
} }
} }
break; break;
case SIGBUS: case SIGBUS:
for(i = 0;sigbus_codes[i].name;++i) for(i = 0;sigbus_codes[i].name;++i)
{ {
if(sigbus_codes[i].code == crash_info.siginfo.si_code) if(sigbus_codes[i].code == crash_info.siginfo.si_code)
{ {
sigdesc = sigbus_codes[i].name; sigdesc = sigbus_codes[i].name;
break; break;
} }
} }
break; break;
} }
} }
fprintf(stderr, "%s (signal %i)\n", sigdesc, crash_info.signum); fprintf(stderr, "%s (signal %i)\n", sigdesc, crash_info.signum);
if(crash_info.has_siginfo) if(crash_info.has_siginfo)
fprintf(stderr, "Address: %p\n", crash_info.siginfo.si_addr); fprintf(stderr, "Address: %p\n", crash_info.siginfo.si_addr);
fputc('\n', stderr); fputc('\n', stderr);
if(logfile) if(logfile)
{ {
/* Create crash log file and redirect shell output to it */ /* Create crash log file and redirect shell output to it */
if(freopen(logfile, "wa", stdout) != stdout) if(freopen(logfile, "wa", stdout) != stdout)
{ {
fprintf(stderr, "!!! Could not create %s following signal\n", logfile); fprintf(stderr, "!!! Could not create %s following signal\n", logfile);
exit(1); exit(1);
} }
fprintf(stderr, "Generating %s and killing process %d, please wait... ", logfile, crash_info.pid); fprintf(stderr, "Generating %s and killing process %d, please wait... ", logfile, crash_info.pid);
printf("*** Fatal Error ***\n" printf("*** Fatal Error ***\n"
"%s (signal %i)\n", sigdesc, crash_info.signum); "%s (signal %i)\n", sigdesc, crash_info.signum);
if(crash_info.has_siginfo) if(crash_info.has_siginfo)
printf("Address: %p\n", crash_info.siginfo.si_addr); printf("Address: %p\n", crash_info.siginfo.si_addr);
fputc('\n', stdout); fputc('\n', stdout);
fflush(stdout); fflush(stdout);
} }
sys_info(); sys_info();
crash_info.buf[sizeof(crash_info.buf)-1] = '\0'; crash_info.buf[sizeof(crash_info.buf)-1] = '\0';
printf("%s\n", crash_info.buf); printf("%s\n", crash_info.buf);
fflush(stdout); fflush(stdout);
if(crash_info.pid > 0) if(crash_info.pid > 0)
{ {
gdb_info(crash_info.pid); gdb_info(crash_info.pid);
kill(crash_info.pid, SIGKILL); kill(crash_info.pid, SIGKILL);
} }
if(logfile) if(logfile)
{ {
char cwd[MAXPATHLEN]; char cwd[MAXPATHLEN];
getcwd(cwd, MAXPATHLEN); getcwd(cwd, MAXPATHLEN);
std::string message = "OpenMW has encountered a fatal error.\nCrash log saved to '" + std::string(cwd) + "/" + std::string(logfile) + "'.\n Please report this to https://bugs.openmw.org !"; std::string message = "OpenMW has encountered a fatal error.\nCrash log saved to '" + std::string(cwd) + "/" + std::string(logfile) + "'.\n Please report this to https://bugs.openmw.org !";
SDL_ShowSimpleMessageBox(0, "Fatal Error", message.c_str(), NULL); SDL_ShowSimpleMessageBox(0, "Fatal Error", message.c_str(), NULL);
} }
exit(0); exit(0);
} }
int cc_install_handlers(int argc, char **argv, int num_signals, int *signals, const char *logfile, int (*user_info)(char*, char*)) int cc_install_handlers(int argc, char **argv, int num_signals, int *signals, const char *logfile, int (*user_info)(char*, char*))
{ {
struct sigaction sa; struct sigaction sa;
stack_t altss; stack_t altss;
int retval; int retval;
if(argc == 2 && strcmp(argv[1], crash_switch) == 0) if(argc == 2 && strcmp(argv[1], crash_switch) == 0)
crash_handler(logfile); crash_handler(logfile);
cc_user_info = user_info; cc_user_info = user_info;
if(argv[0][0] == '/') if(argv[0][0] == '/')
snprintf(argv0, sizeof(argv0), "%s", argv[0]); snprintf(argv0, sizeof(argv0), "%s", argv[0]);
else else
{ {
getcwd(argv0, sizeof(argv0)); getcwd(argv0, sizeof(argv0));
retval = strlen(argv0); retval = strlen(argv0);
snprintf(argv0+retval, sizeof(argv0)-retval, "/%s", argv[0]); snprintf(argv0+retval, sizeof(argv0)-retval, "/%s", argv[0]);
} }
/* Set an alternate signal stack so SIGSEGVs caused by stack overflows /* Set an alternate signal stack so SIGSEGVs caused by stack overflows
* still run */ * still run */
altss.ss_sp = altstack; altss.ss_sp = altstack;
altss.ss_flags = 0; altss.ss_flags = 0;
altss.ss_size = sizeof(altstack); altss.ss_size = sizeof(altstack);
sigaltstack(&altss, NULL); sigaltstack(&altss, NULL);
memset(&sa, 0, sizeof(sa)); memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = crash_catcher; sa.sa_sigaction = crash_catcher;
sa.sa_flags = SA_RESETHAND | SA_NODEFER | SA_SIGINFO | SA_ONSTACK; sa.sa_flags = SA_RESETHAND | SA_NODEFER | SA_SIGINFO | SA_ONSTACK;
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
retval = 0; retval = 0;
while(num_signals--) while(num_signals--)
{ {
if((*signals != SIGSEGV && *signals != SIGILL && *signals != SIGFPE && *signals != SIGABRT && if((*signals != SIGSEGV && *signals != SIGILL && *signals != SIGFPE && *signals != SIGABRT &&
*signals != SIGBUS) || sigaction(*signals, &sa, NULL) == -1) *signals != SIGBUS) || sigaction(*signals, &sa, NULL) == -1)
{ {
*signals = 0; *signals = 0;
retval = -1; retval = -1;
} }
++signals; ++signals;
} }
return retval; return retval;
} }