From 396eeff350aa89210f6f678353d1cf34963216e8 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Fri, 10 Apr 2026 18:28:33 +0200 Subject: [PATCH] cmake: add SDLTEST_GDB option to run tests under gdb debugger printing stacktraces --- test/CMakeLists.txt | 6 ++++++ test/unix/gdbcmds.txt | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/unix/gdbcmds.txt diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a77c5aa685..81caee0d74 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -54,6 +54,12 @@ if(WIN32) else() set_property(TARGET sdlprocdump PROPERTY EXCLUDE_FROM_ALL "1") endif() +else() + option(SDLTEST_GDB "Run tests using headless gdb" OFF) + find_program(GDB_BIN NAMES "gdb") + if(SDLTEST_GDB AND GDB_BIN) + set(CMAKE_TEST_LAUNCHER "${GDB_BIN};-batch;-nx;-x;${CMAKE_CURRENT_SOURCE_DIR}/unix/gdbcmds.txt;--args") + endif() endif() if(EMSCRIPTEN) diff --git a/test/unix/gdbcmds.txt b/test/unix/gdbcmds.txt new file mode 100644 index 0000000000..c7a8ff9f08 --- /dev/null +++ b/test/unix/gdbcmds.txt @@ -0,0 +1,36 @@ +# Script to run a headless gdb printing a stacktrace on a fatal signal +# Use it as: `gdb -batch -nx -x gdbcmds.txt --args $PROGRAM_AND_ARGS` +# Configure CMake with -DSDLTEST_GDB=ON to use with within CTest. + +set pagination off +set confirm off +set verbose off + +# Only stop on real crash signals +handle SIGSEGV stop print nopass +handle SIGABRT stop print nopass +handle SIGILL stop print nopass +handle SIGFPE stop print nopass +handle SIGBUS stop print nopass + +# Ignore SIGPIPE and SIGTERM signals +# (SIGTERM is used by testthread) +handle SIGPIPE nostop noprint pass +handle SIGTERM nostop noprint pass + +define hook-stop + if !$_isvoid($_siginfo) + printf "\n=== Crash detected (signal %d) ===\n", $_siginfo.si_signo + bt full + quit 1 + end +end + +run + +# Normal exit path +if !$_isvoid($_exitcode) + quit $_exitcode +end + +quit 0