From 6f43797df839c189545d87b5c9ccbf45403cf631 Mon Sep 17 00:00:00 2001
From: Nazir Bilal Yavuz <byavuz81@gmail.com>
Date: Mon, 11 Nov 2024 17:27:08 +0300
Subject: [PATCH v2 3/3] Collect core files on NetBSD and OpenBSD

---
 .cirrus.tasks.yml               | 17 ++++++++++++++++-
 src/tools/ci/cores_backtrace.sh |  6 ++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml
index f338af902aa..a1500b42e4c 100644
--- a/.cirrus.tasks.yml
+++ b/.cirrus.tasks.yml
@@ -210,7 +210,7 @@ task:
         build/tmp_install/usr/local/pgsql/bin/pg_ctl -D build/runningcheck stop || true
       EOF
     <<: *on_failure_meson
-    cores_script: src/tools/ci/cores_backtrace.sh freebsd /tmp/cores
+    cores_script: src/tools/ci/cores_backtrace.sh bsd /tmp/cores
 
 
 task:
@@ -227,11 +227,13 @@ task:
     CCACHE_DIR: /tmp/ccache_dir
 
     PATH: /usr/sbin:$PATH
+    CORE_DUMP_DIR: /var/crash
 
   matrix:
     - name: NetBSD - 10 - Meson
       only_if: $CIRRUS_CHANGE_MESSAGE !=~ '.*\nci-os-only:.*' || $CIRRUS_CHANGE_MESSAGE =~ '.*\nci-os-only:[^\n]*netbsd.*'
       env:
+        OS_NAME: netbsd
         IMAGE_FAMILY: pg-ci-netbsd-postgres
         INCLUDE_DIRS: -Dextra_include_dirs=/usr/pkg/include -Dextra_lib_dirs=/usr/pkg/lib
         # initdb fails with: 'invalid locale settings' error on NetBSD.
@@ -245,11 +247,14 @@ task:
     - name: OpenBSD - 7 - Meson
       only_if: $CIRRUS_CHANGE_MESSAGE !=~ '.*\nci-os-only:.*' || $CIRRUS_CHANGE_MESSAGE =~ '.*\nci-os-only:[^\n]*openbsd.*'
       env:
+        OS_NAME: openbsd
         IMAGE_FAMILY: pg-ci-openbsd-postgres
         INCLUDE_DIRS: -Dextra_include_dirs=/usr/local/include -Dextra_lib_dirs=/usr/local/lib
         UUID: -Duuid=e2fs
       setup_additional_packages_script: |
         #pkg_add -I ...
+      # Always core dump to ${CORE_DUMP_DIR}
+      set_core_dump_script: sysctl -w kern.nosuidcoredump=2
       <<: *openbsd_task_template
 
   sysinfo_script: |
@@ -267,6 +272,10 @@ task:
     chown -R postgres:users /home/postgres
     mkdir -p ${CCACHE_DIR}
     chown -R postgres:users ${CCACHE_DIR}
+  setup_core_files_script: |
+    mkdir -p ${CORE_DUMP_DIR}
+    chmod -R 770 ${CORE_DUMP_DIR}
+    chown -R postgres:users ${CORE_DUMP_DIR}
 
   # -Duuid=bsd is not set since 'bsd' uuid option
   # is not working on NetBSD & OpenBSD. See
@@ -288,6 +297,7 @@ task:
 
   test_world_script: |
     su postgres <<-EOF
+      ulimit -c unlimited
       # Otherwise tests will fail on OpenBSD, due to the lack of enough processes.
       ulimit -p 256
       meson test $MTEST_ARGS --num-processes ${TEST_JOBS}
@@ -295,6 +305,11 @@ task:
 
   on_failure:
     <<: *on_failure_meson
+    cores_script: |
+      # Although OSes are forced to core dump inside ${CORE_DUMP_DIR}, they may
+      # not obey this. So, move core files to the ${CORE_DUMP_DIR} directory.
+      find build/ -maxdepth 1 -type f -name '*.core' -exec mv '{}' ${CORE_DUMP_DIR} \;
+      src/tools/ci/cores_backtrace.sh ${OS_NAME} ${CORE_DUMP_DIR}
 
 
 # configure feature flags, shared between the task running the linux tests and
diff --git a/src/tools/ci/cores_backtrace.sh b/src/tools/ci/cores_backtrace.sh
index 28d3cecfc67..4b174a5cf56 100755
--- a/src/tools/ci/cores_backtrace.sh
+++ b/src/tools/ci/cores_backtrace.sh
@@ -9,7 +9,7 @@ os=$1
 directory=$2
 
 case $os in
-    freebsd|linux|macos)
+    freebsd|netbsd|openbsd|linux|macos)
     ;;
     *)
         echo "unsupported operating system ${os}"
@@ -26,7 +26,7 @@ for corefile in $(find "$directory" -type f) ; do
         echo -e '\n\n'
     fi
 
-    if [ "$os" = 'macos' ]; then
+    if [ "$os" = 'macos' ] || [ "$os" = 'openbsd' ]; then
         lldb -c $corefile --batch -o 'thread backtrace all' -o 'quit'
     else
         auxv=$(gdb --quiet --core ${corefile} --batch -ex 'info auxv' 2>/dev/null)
@@ -37,6 +37,8 @@ for corefile in $(find "$directory" -type f) ; do
 
         if [ "$os" = 'freebsd' ]; then
             binary=$(echo "$auxv" | grep AT_EXECPATH | perl -pe "s/^.*\"(.*)\"\$/\$1/g")
+        elif [ "$os" = 'netbsd' ]; then
+            binary=$(echo "$auxv" | grep AT_SUN_EXECNAME | perl -pe "s/^.*\"(.*)\"\$/\$1/g")
         elif [ "$os" = 'linux' ]; then
             binary=$(echo "$auxv" | grep AT_EXECFN | perl -pe "s/^.*\"(.*)\"\$/\$1/g")
         else
-- 
2.45.2

