From 6c04b19357dee90980e0bca182f314628e509e38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:14:21 +0100 Subject: [PATCH] Analyze core dumps on test failure --- ci/tests/run-core-tests.sh | 6 +--- ci/tests/run-qt-tests.sh | 6 +--- ci/tests/run-rpc-tests.sh | 6 +--- ci/tests/run-tests.sh | 64 +++++++++++++++++++++++++++++++++++++ ci/tests/show-core-dumps.sh | 62 +++++++++++++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 15 deletions(-) create mode 100755 ci/tests/run-tests.sh create mode 100755 ci/tests/show-core-dumps.sh diff --git a/ci/tests/run-core-tests.sh b/ci/tests/run-core-tests.sh index a3e02b70bf..31dd324a3f 100755 --- a/ci/tests/run-core-tests.sh +++ b/ci/tests/run-core-tests.sh @@ -1,8 +1,4 @@ #!/bin/bash set -euo pipefail -source "$(dirname "$BASH_SOURCE")/common.sh" - -BUILD_DIR=${1-${PWD}} - -${BUILD_DIR}/core_test$(get_exec_extension) \ No newline at end of file +$(dirname "$BASH_SOURCE")/run-tests.sh core_test \ No newline at end of file diff --git a/ci/tests/run-qt-tests.sh b/ci/tests/run-qt-tests.sh index 8faa28d43a..c53b77544c 100755 --- a/ci/tests/run-qt-tests.sh +++ b/ci/tests/run-qt-tests.sh @@ -1,10 +1,6 @@ #!/bin/bash set -euo pipefail -source "$(dirname "$BASH_SOURCE")/common.sh" - -BUILD_DIR=${1-${PWD}} - # Alpine doesn't offer an xvfb xvfb_run_() { @@ -20,4 +16,4 @@ xvfb_run_() return ${res} } -xvfb_run_ ${BUILD_DIR}/qt_test$(get_exec_extension) \ No newline at end of file +xvfb_run_ $(dirname "$BASH_SOURCE")/run-tests.sh qt_test \ No newline at end of file diff --git a/ci/tests/run-rpc-tests.sh b/ci/tests/run-rpc-tests.sh index 6b64607d95..91dba35e71 100755 --- a/ci/tests/run-rpc-tests.sh +++ b/ci/tests/run-rpc-tests.sh @@ -1,8 +1,4 @@ #!/bin/bash set -euo pipefail -source "$(dirname "$BASH_SOURCE")/common.sh" - -BUILD_DIR=${1-${PWD}} - -${BUILD_DIR}/rpc_test$(get_exec_extension) \ No newline at end of file +$(dirname "$BASH_SOURCE")/run-tests.sh rpc_test \ No newline at end of file diff --git a/ci/tests/run-tests.sh b/ci/tests/run-tests.sh new file mode 100755 index 0000000000..7ac5594e3c --- /dev/null +++ b/ci/tests/run-tests.sh @@ -0,0 +1,64 @@ +#!/bin/bash +set -uo pipefail + +source "$(dirname "$BASH_SOURCE")/common.sh" + +target=$1 +if [ -z "${target-}" ]; then + echo "Target not specified" + exit 1 +fi + +echo "Running tests for target: ${target}" + +# Enable core dumps +DEFAULT_COREDUMP_DIR="/cores" +case "$(uname -s)" in + Linux*) + # Ensure directory exists and is writable for core dumps + sudo mkdir -p "${DEFAULT_COREDUMP_DIR}" + sudo chmod a+w "${DEFAULT_COREDUMP_DIR}" + # Enable core dumps + ulimit -c unlimited + echo "${DEFAULT_COREDUMP_DIR}/core-%e.%p" | sudo tee /proc/sys/kernel/core_pattern + export COREDUMP_DIR=${DEFAULT_COREDUMP_DIR} + + echo "Core dumps enabled (Linux)" + ;; + Darwin*) + # Ensure directory exists and is writable for core dumps + sudo mkdir -p "${DEFAULT_COREDUMP_DIR}" + sudo chmod a+w "${DEFAULT_COREDUMP_DIR}" + # Enable core dumps + ulimit -c unlimited + # By default, macOS writes core dumps to /cores + export COREDUMP_DIR=${DEFAULT_COREDUMP_DIR} + + echo "Core dumps enabled (macOS)" + ;; + CYGWIN*|MINGW32*|MSYS*|MINGW*) + # TODO: Support core dumps on Windows + echo "Core dumps not supported on Windows" + ;; + *) + echo "Unknown OS" + exit 1 + ;; +esac + +# Run the test +executable=./${target}$(get_exec_extension) +"${executable}" +status=$? + +if [ $status -ne 0 ]; then + echo "::error::Test failed: ${target}" + + # Show core dumps + export EXECUTABLE=${executable} + "$(dirname "$BASH_SOURCE")/show-core-dumps.sh" + + exit $status +else + exit 0 +fi \ No newline at end of file diff --git a/ci/tests/show-core-dumps.sh b/ci/tests/show-core-dumps.sh new file mode 100755 index 0000000000..e8409e83a4 --- /dev/null +++ b/ci/tests/show-core-dumps.sh @@ -0,0 +1,62 @@ +#!/bin/bash +set -uo pipefail + +echo "Analyzing core dumps..." + +if [ -z "${COREDUMP_DIR-}" ]; then + echo "COREDUMP_DIR environment variable is not set." + exit 1 +fi + +if [ -z "${EXECUTABLE-}" ]; then + echo "EXECUTABLE environment variable is not set." + exit 1 +fi + +echo "Core dump location: ${COREDUMP_DIR}" +echo "Executable: ${EXECUTABLE}" + +analyze_core_dump() { + local core_dump=$1 + local executable=$2 + + case "$(uname)" in + Darwin) + # macOS, use lldb + echo "Using lldb for analysis..." + lldb "${executable}" -c "$core_dump" --batch -o "thread backtrace all" -o "quit" + ;; + Linux) + # Linux, use gdb + echo "Using gdb for analysis..." + gdb -quiet -batch -ex "thread apply all bt full" -ex "quit" "${executable}" "$core_dump" + ;; + *) + echo "Unsupported OS." + return 1 + ;; + esac + + # Remove the analyzed core dump file + echo "Removing analyzed core dump: $core_dump" + rm "$core_dump" +} + +# List core dump files +echo "::group::Core dump files" +ls -al "${COREDUMP_DIR}" +echo "::endgroup::" + +# Use a glob pattern to match core dumps +shopt -s nullglob +core_dumps=("${COREDUMP_DIR}"/core*) + +if [ ${#core_dumps[@]} -gt 0 ]; then + for core_dump in "${core_dumps[@]}"; do + echo "::group::Analyzing core dump: $core_dump" + analyze_core_dump "$core_dump" "${EXECUTABLE}" + echo "::endgroup::" + done +else + echo "No core dump file found." +fi