From 43da1b8d1fdaaeb2977c0f936889194d3cf46f17 Mon Sep 17 00:00:00 2001 From: The412Banner Date: Fri, 2 Oct 2026 19:28:49 -0400 Subject: [PATCH] Diagnostics: SIGSEGV logger preload for x86 guest programs Writes the fault address, the instruction pointer and a backtrace with module offsets to SEGVLOG_FILE; built by its own small workflow. --- .github/workflows/build-diag.yml | 21 +++++++++ tools/diag/segvlog.c | 73 ++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 .github/workflows/build-diag.yml create mode 100644 tools/diag/segvlog.c diff --git a/.github/workflows/build-diag.yml b/.github/workflows/build-diag.yml new file mode 100644 index 000000000..7361c10ee --- /dev/null +++ b/.github/workflows/build-diag.yml @@ -0,0 +1,21 @@ +name: Build diagnostics + +on: + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + - name: Build + run: | + sudo apt-get update -qq && sudo apt-get install -y -qq gcc-multilib >/dev/null + mkdir -p out + gcc -m32 -O1 -fPIC -shared -fno-omit-frame-pointer -o out/libsegvlog-i386.so tools/diag/segvlog.c -ldl + gcc -O1 -fPIC -shared -fno-omit-frame-pointer -o out/libsegvlog-x86_64.so tools/diag/segvlog.c -ldl + file out/* + - uses: actions/upload-artifact@v4 + with: + name: diag + path: out/ diff --git a/tools/diag/segvlog.c b/tools/diag/segvlog.c new file mode 100644 index 000000000..f616d6a19 --- /dev/null +++ b/tools/diag/segvlog.c @@ -0,0 +1,73 @@ +// Diagnostic: on SIGSEGV/SIGBUS/SIGILL, write the faulting address, the instruction pointer and a +// frame-pointer backtrace (module+offset, symbol) to $SEGVLOG_FILE, then die as before. Later +// SIGSEGV handlers (the game's breakpad) are refused so this one sees the fault first. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +static int (*real_sigaction)(int, const struct sigaction *, struct sigaction *); +static int out = -1; + +static void line(const char *tag, void *pc) { + char buf[512]; + Dl_info di; + if (pc && dladdr(pc, &di) && di.dli_fname) + snprintf(buf, sizeof buf, "%s %p %s+0x%lx %s\n", tag, pc, di.dli_fname, + (unsigned long)((char *)pc - (char *)di.dli_fbase), di.dli_sname ? di.dli_sname : "?"); + else + snprintf(buf, sizeof buf, "%s %p ?\n", tag, pc); + if (out >= 0) write(out, buf, strlen(buf)); +} + +static void handler(int sig, siginfo_t *si, void *ctx) { + ucontext_t *uc = ctx; + char buf[160]; +#if defined(__i386__) + void *pc = (void *)uc->uc_mcontext.gregs[REG_EIP]; + void **fp = (void **)uc->uc_mcontext.gregs[REG_EBP]; +#else + void *pc = (void *)uc->uc_mcontext.gregs[REG_RIP]; + void **fp = (void **)uc->uc_mcontext.gregs[REG_RBP]; +#endif + snprintf(buf, sizeof buf, "=== pid %d signal %d code %d fault addr %p\n", getpid(), sig, si->si_code, si->si_addr); + if (out >= 0) write(out, buf, strlen(buf)); + line("pc", pc); + for (int i = 0; i < 24 && fp && ((unsigned long)fp & 3) == 0; i++) { + void *ret = fp[1]; + if (!ret) break; + line(" at", ret); + void **next = (void **)fp[0]; + if (next <= fp) break; + fp = next; + } + signal(sig, SIG_DFL); +} + +int sigaction(int sig, const struct sigaction *act, struct sigaction *old) { + if (!real_sigaction) real_sigaction = dlsym(RTLD_NEXT, "sigaction"); + if (act && (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL) && act->sa_sigaction != handler) { + if (old) real_sigaction(sig, NULL, old); + return 0; + } + return real_sigaction(sig, act, old); +} + +__attribute__((constructor)) static void init(void) { + const char *f = getenv("SEGVLOG_FILE"); + if (!f) return; + out = open(f, O_WRONLY | O_CREAT | O_APPEND, 0644); + struct sigaction sa; + memset(&sa, 0, sizeof sa); + sa.sa_sigaction = handler; + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; + real_sigaction = dlsym(RTLD_NEXT, "sigaction"); + real_sigaction(SIGSEGV, &sa, NULL); + real_sigaction(SIGBUS, &sa, NULL); + real_sigaction(SIGILL, &sa, NULL); +}