From 611216286e463edd463d3aa1efe091c099daed8a Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 11 Apr 2019 11:34:22 +1000 Subject: [PATCH] [c-host] added initial crash handler stub --- c-host/src/app.c | 4 ++ common/CMakeLists.txt | 8 +-- common/include/common/crash.h | 22 ++++++++ common/src/crash.linux.c | 97 +++++++++++++++++++++++++++++++++++ common/src/crash.windows.c | 26 ++++++++++ common/src/debug.c | 0 6 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 common/include/common/crash.h create mode 100644 common/src/crash.linux.c create mode 100644 common/src/crash.windows.c delete mode 100644 common/src/debug.c diff --git a/c-host/src/app.c b/c-host/src/app.c index 990693ad..77ebfd1a 100644 --- a/c-host/src/app.c +++ b/c-host/src/app.c @@ -23,6 +23,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "common/debug.h" #include "common/locking.h" #include "common/KVMFR.h" +#include "common/crash.h" #include #include @@ -257,6 +258,9 @@ static bool captureRestart() int app_main() { + if (!installCrashHandler()) + DEBUG_WARN("Failed to install the crash handler"); + unsigned int shmemSize = os_shmemSize(); uint8_t * shmemMap = NULL; int exitcode = 0; diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 45f31490..ef35fa83 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -5,9 +5,11 @@ include_directories( ${PROJECT_SOURCE_DIR}/include ) -add_library(lg_common STATIC - src/debug.c -) +if(WIN32) + add_library(lg_common STATIC src/crash.windows.c) +else() + add_library(lg_common STATIC src/crash.linux.c) +endif() #target_link_libraries(lg_common #) diff --git a/common/include/common/crash.h b/common/include/common/crash.h new file mode 100644 index 00000000..9abc1b45 --- /dev/null +++ b/common/include/common/crash.h @@ -0,0 +1,22 @@ +/* +KVMGFX Client - A KVM Client for VGA Passthrough +Copyright (C) 2017-2019 Geoffrey McRae +https://looking-glass.hostfission.com + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include + +bool installCrashHandler(); \ No newline at end of file diff --git a/common/src/crash.linux.c b/common/src/crash.linux.c new file mode 100644 index 00000000..03d02056 --- /dev/null +++ b/common/src/crash.linux.c @@ -0,0 +1,97 @@ +/* +KVMGFX Client - A KVM Client for VGA Passthrough +Copyright (C) 2017-2019 Geoffrey McRae +https://looking-glass.hostfission.com + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "common/crash.h" +#include "common/debug.h" + +#include +#include +#include +#include +#include +#include +#include + +/* + Large portions of this code comes from @jschmier @ https://stackoverflow.com/a/1925461/637874 +*/ + +/* This structure mirrors the one found in /usr/include/asm/ucontext.h */ +typedef struct _sig_ucontext +{ + unsigned long uc_flags; + struct ucontext *uc_link; + stack_t uc_stack; + struct sigcontext uc_mcontext; + sigset_t uc_sigmask; +} +sig_ucontext_t; + +static void crit_err_hdlr(int sig_num, siginfo_t * info, void * ucontext) +{ + void * array[50]; + void * caller_address; + char ** messages; + int size, i; + sig_ucontext_t * uc; + + uc = (sig_ucontext_t *)ucontext; + + /* Get the address at the time the signal was raised */ + #if defined(__i386__) // gcc specific + caller_address = (void *) uc->uc_mcontext.eip; // EIP: x86 specific + #elif defined(__x86_64__) // gcc specific + caller_address = (void *) uc->uc_mcontext.rip; // RIP: x86_64 specific + #else + #error Unsupported architecture. // TODO: Add support for other arch. + #endif + + DEBUG_ERROR("signal %d (%s), address is %p from %p", sig_num, strsignal(sig_num), info->si_addr, (void *)caller_address); + + size = backtrace(array, 50); + + /* overwrite sigaction with caller's address */ + array[1] = caller_address; + + messages = backtrace_symbols(array, size); + + /* skip first stack frame (points here) */ + for (i = 1; i < size && messages != NULL; ++i) + DEBUG_ERROR("[bt]: (%d) %s", i, messages[i]); + + free(messages); + + exit(EXIT_FAILURE); +} + +bool installCrashHandler() +{ + struct sigaction sigact; + + sigact.sa_sigaction = crit_err_hdlr; + sigact.sa_flags = SA_RESTART | SA_SIGINFO; + + if (sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL) != 0) + { + DEBUG_ERROR("Error setting signal handler for %d (%s)", SIGSEGV, strsignal(SIGSEGV)); + return false; + } + + return true; +} \ No newline at end of file diff --git a/common/src/crash.windows.c b/common/src/crash.windows.c new file mode 100644 index 00000000..7d04dc18 --- /dev/null +++ b/common/src/crash.windows.c @@ -0,0 +1,26 @@ +/* +KVMGFX Client - A KVM Client for VGA Passthrough +Copyright (C) 2017-2019 Geoffrey McRae +https://looking-glass.hostfission.com + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 2 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program; if not, write to the Free Software Foundation, Inc., 59 Temple +Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "common/crash.h" + +bool installCrashHandler() +{ + //TODO + return true; +} \ No newline at end of file diff --git a/common/src/debug.c b/common/src/debug.c deleted file mode 100644 index e69de29b..00000000