xref: /freebsd/sbin/savecore/tests/livedump_test.sh (revision 73465bb47bb37e6efdc61827e260a070dda0a0dc)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 Mark Johnston <markj@FreeBSD.org>
5#
6
7atf_test_case livedump_kldstat
8livedump_kldstat_head()
9{
10	atf_set "descr" "Test livedump integrity"
11	atf_set "require.progs" kgdb
12	atf_set "require.user" root
13}
14livedump_kldstat_body()
15{
16	atf_check savecore -L .
17
18	kernel=$(sysctl -n kern.bootfile)
19
20	if ! [ -f /usr/lib/debug/${kernel}.debug ]; then
21		atf_skip "No debug symbols for the running kernel"
22	fi
23
24	# Implement kldstat using gdb script.
25	cat >./kldstat.gdb <<'__EOF__'
26printf "Id Refs Address                Size Name\n"
27set $_lf = linker_files.tqh_first
28while ($_lf)
29    printf "%2d %4d %p %8x %s\n", $_lf->id, $_lf->refs, $_lf->address, $_lf->size, $_lf->filename
30    set $_lf = $_lf->link.tqe_next
31end
32__EOF__
33
34	# Ignore stderr since kgdb prints some warnings about inaccessible
35	# source files.
36	#
37	# Use a script to source the main gdb script, otherwise kgdb prints
38	# a bunch of line noise that is annoying to filter out.
39	echo "source ./kldstat.gdb" > ./script.gdb
40	atf_check -o save:out -e ignore \
41	    kgdb -q ${kernel} ./livecore.0 < ./script.gdb
42
43	# Get rid of gunk printed by kgdb.
44	sed -i '' -n -e 's/^(kgdb) //' -e '/^Id Refs /,$p' out
45
46	# The output of kgdb should match the output of kldstat.
47	atf_check -o save:kldstat kldstat
48	atf_check diff kldstat out
49}
50
51atf_init_test_cases()
52{
53	atf_add_test_case livedump_kldstat
54}
55