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