git: 73465bb47bb3 - main - savecore: Add a livedump regression test

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Mon, 25 Nov 2024 21:13:01 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=73465bb47bb37e6efdc61827e260a070dda0a0dc

commit 73465bb47bb37e6efdc61827e260a070dda0a0dc
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2024-11-25 21:12:12 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2024-11-25 21:12:51 +0000

    savecore: Add a livedump regression test
    
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D47715
---
 etc/mtree/BSD.tests.dist             |  2 ++
 sbin/savecore/Makefile               |  3 ++
 sbin/savecore/tests/Makefile         |  3 ++
 sbin/savecore/tests/livedump_test.sh | 54 ++++++++++++++++++++++++++++++++++++
 4 files changed, 62 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index beae521789c0..e9ba0f689678 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -505,6 +505,8 @@
         ..
         route
         ..
+        savecore
+        ..
         sysctl
         ..
     ..
diff --git a/sbin/savecore/Makefile b/sbin/savecore/Makefile
index 8d22ab21b6da..7e194a62deb1 100644
--- a/sbin/savecore/Makefile
+++ b/sbin/savecore/Makefile
@@ -18,4 +18,7 @@ CFLAGS+=	-DWITH_CASPER
 LIBADD+=	casper cap_fileargs cap_syslog
 .endif
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
 .include <bsd.prog.mk>
diff --git a/sbin/savecore/tests/Makefile b/sbin/savecore/tests/Makefile
new file mode 100644
index 000000000000..b58e79f518ef
--- /dev/null
+++ b/sbin/savecore/tests/Makefile
@@ -0,0 +1,3 @@
+ATF_TESTS_SH=	livedump_test
+
+.include <bsd.test.mk>
diff --git a/sbin/savecore/tests/livedump_test.sh b/sbin/savecore/tests/livedump_test.sh
new file mode 100644
index 000000000000..42c726bfad4e
--- /dev/null
+++ b/sbin/savecore/tests/livedump_test.sh
@@ -0,0 +1,54 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2024 Mark Johnston <markj@FreeBSD.org>
+#
+
+atf_test_case livedump_kldstat
+livedump_kldstat_head()
+{
+	atf_set "descr" "Test livedump integrity"
+	atf_set "require.progs" kgdb
+	atf_set "require.user" root
+}
+livedump_kldstat_body()
+{
+	atf_check savecore -L .
+
+	kernel=$(sysctl -n kern.bootfile)
+
+	if ! [ -f /usr/lib/debug/${kernel}.debug ]; then
+		atf_skip "No debug symbols for the running kernel"
+	fi
+
+	# Implement kldstat using gdb script.
+	cat >./kldstat.gdb <<'__EOF__'
+printf "Id Refs Address                Size Name\n"
+set $_lf = linker_files.tqh_first
+while ($_lf)
+    printf "%2d %4d %p %8x %s\n", $_lf->id, $_lf->refs, $_lf->address, $_lf->size, $_lf->filename
+    set $_lf = $_lf->link.tqe_next
+end
+__EOF__
+
+	# Ignore stderr since kgdb prints some warnings about inaccessible
+	# source files.
+	#
+	# Use a script to source the main gdb script, otherwise kgdb prints
+	# a bunch of line noise that is annoying to filter out.
+	echo "source ./kldstat.gdb" > ./script.gdb
+	atf_check -o save:out -e ignore \
+	    kgdb -q ${kernel} ./livecore.0 < ./script.gdb
+
+	# Get rid of gunk printed by kgdb.
+	sed -i '' -n -e 's/^(kgdb) //' -e '/^Id Refs /,$p' out
+
+	# The output of kgdb should match the output of kldstat.
+	atf_check -o save:kldstat kldstat
+	atf_check diff kldstat out
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case livedump_kldstat
+}