From 161edef2a8ebbf3ea86a8c4ca2cfb83a14eb01e5 Mon Sep 17 00:00:00 2001 From: Akkadius Date: Mon, 23 Sep 2019 00:47:57 -0500 Subject: [PATCH 1/2] WIP POC --- common/crash.cpp | 39 +++++++++++++++++++++++++++++++++++- common/profanity_manager.cpp | 3 +++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/common/crash.cpp b/common/crash.cpp index 522d5f171..2c3cf44f9 100644 --- a/common/crash.cpp +++ b/common/crash.cpp @@ -111,7 +111,44 @@ void set_exception_handler() { SetUnhandledExceptionFilter(windows_exception_handler); } #else + +#include +#include +#include +#include + +void print_trace() +{ + auto uid = geteuid (); + + std::cout << "running as user id " << uid << std::endl; + + 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) { + dup2(2, 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); + } + + abort(); /* If gdb failed to start */ + } + else { + waitpid(child_pid, NULL, 0); + } + exit(1); +} + // crash is off or an unhandled platform -void set_exception_handler() { +void set_exception_handler() +{ + signal(SIGSEGV, reinterpret_cast(print_trace)); } #endif diff --git a/common/profanity_manager.cpp b/common/profanity_manager.cpp index 70817576f..57a1bffde 100644 --- a/common/profanity_manager.cpp +++ b/common/profanity_manager.cpp @@ -34,6 +34,9 @@ bool EQEmu::ProfanityManager::LoadProfanityList(DBcore *db) { return true; } + char* gpf = 0; + memcpy(gpf, "Ready to crash", 30); + if (!load_database_entries(db)) return false; From 118c6b563640cd91498fc8e0b0175dbb1363242a Mon Sep 17 00:00:00 2001 From: Akkadius Date: Mon, 23 Sep 2019 01:56:03 -0500 Subject: [PATCH 2/2] Add refinements to linux crash handler --- common/crash.cpp | 22 +++++++++++++++++++--- common/profanity_manager.cpp | 3 --- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/common/crash.cpp b/common/crash.cpp index 2c3cf44f9..155bfcf2a 100644 --- a/common/crash.cpp +++ b/common/crash.cpp @@ -116,12 +116,13 @@ void set_exception_handler() { #include #include #include +#include void print_trace() { - auto uid = geteuid (); + auto uid = geteuid(); - std::cout << "running as user id " << uid << std::endl; + std::string temp_output_file = "/tmp/dump-output"; char pid_buf[30]; sprintf(pid_buf, "%d", getpid()); @@ -129,7 +130,9 @@ void print_trace() name_buf[readlink("/proc/self/exe", name_buf, 511)] = 0; int child_pid = fork(); if (!child_pid) { - dup2(2, 1); // redirect output to stderr + 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); @@ -138,17 +141,30 @@ void print_trace() 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() { + 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 diff --git a/common/profanity_manager.cpp b/common/profanity_manager.cpp index 57a1bffde..70817576f 100644 --- a/common/profanity_manager.cpp +++ b/common/profanity_manager.cpp @@ -34,9 +34,6 @@ bool EQEmu::ProfanityManager::LoadProfanityList(DBcore *db) { return true; } - char* gpf = 0; - memcpy(gpf, "Ready to crash", 30); - if (!load_database_entries(db)) return false;