diff --git a/common/crash.cpp b/common/crash.cpp index 522d5f171..155bfcf2a 100644 --- a/common/crash.cpp +++ b/common/crash.cpp @@ -111,7 +111,60 @@ void set_exception_handler() { SetUnhandledExceptionFilter(windows_exception_handler); } #else + +#include +#include +#include +#include +#include + +void print_trace() +{ + auto uid = geteuid(); + + std::string temp_output_file = "/tmp/dump-output"; + + char pid_buf[30]; + sprintf(pid_buf, "%d", getpid()); + char name_buf[512]; + name_buf[readlink("/proc/self/exe", name_buf, 511)] = 0; + int child_pid = fork(); + if (!child_pid) { + int fd = open(temp_output_file.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); + + dup2(fd, 1); // redirect output to stderr + fprintf(stdout, "stack trace for %s pid=%s\n", name_buf, pid_buf); + if (uid == 0) { + execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL); + } + else { + execlp("sudo", "gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL); + } + + close(fd); + + abort(); /* If gdb failed to start */ + } + else { + waitpid(child_pid, NULL, 0); + } + + std::ifstream input(temp_output_file); + for (std::string line; getline(input, line);) { + LogCrash("{}", line); + } + + std::remove(temp_output_file.c_str()); + + exit(1); +} + // crash is off or an unhandled platform -void set_exception_handler() { +void set_exception_handler() +{ + signal(SIGABRT, reinterpret_cast(print_trace)); + signal(SIGFPE, reinterpret_cast(print_trace)); + signal(SIGFPE, reinterpret_cast(print_trace)); + signal(SIGSEGV, reinterpret_cast(print_trace)); } #endif