1#!/usr/bin/ksh
2#
3#
4# This file and its contents are supplied under the terms of the
5# Common Development and Distribution License ("CDDL"), version 1.0.
6# You may only use this file in accordance with the terms of version
7# 1.0 of the CDDL.
8#
9# A full copy of the text of the CDDL should have accompanied this
10# source.  A copy of the CDDL is also available via the Internet at
11# http://www.illumos.org/license/CDDL.
12#
13
14#
15# Copyright 2021 Oxide Computer Company
16#
17
18#
19# The goal of this test suite is to test certain aspects of core dump
20# generation and different core contents being specified. The
21# 'dumper.32' and 'dumper.64' programs are designed to be told what to
22# set a core content and path to, after which point we use both gcore
23# and the kernel to generate a core dump for the file and verify that it
24# has what we expect. The verification is done by the secmapper program.
25#
26
27unalias -a
28set -o pipefail
29
30core_arg0="$(basename $0)"
31core_dir="$(dirname $0)"
32core_dumper32="$core_dir/dumper.32"
33core_dumper64="$core_dir/dumper.64"
34core_checker="$core_dir/secmapper"
35
36core_tmpdir="/tmp/coretest.$$"
37core_exit=0
38
39#
40# This array describes the different types of core contents that we're
41# going to try and generate and check against.
42#
43core_contents="none
44ctf
45debug
46symtab
47ctf+debug+symtab
48anon+data+ctf+debug+symtab
49default
50default-ctf-debug-symtab
51default+debug
52default-symtab"
53
54warn()
55{
56	typeset msg="$*"
57	echo "TEST FAILED: $msg" >&2
58	core_exit=1
59}
60
61core_dump_one()
62{
63	typeset prog="$1"
64	typeset pbase=$(basename $prog)
65	typeset cont="$2"
66	typeset kpath="$3"
67	typeset gpath="$4"
68	typeset pid=
69
70	$prog "$cont" "$kpath" &
71	pid=$!
72	if (( $? != 0 )); then
73		warn "failed to spawn $core_dumper32: $cont $kpath"
74		return 1
75	fi
76
77	#
78	# This is racy, but probably should be a reasonable amount of
79	# time for dumper to be ready.
80	#
81	for ((i = 0; i < 10; i++)) {
82		if pstack $pid | grep -q 'fsigsuspend'; then
83			break
84		fi
85	}
86
87	if ! gcore -o "$gpath" -c "$cont" $pid >/dev/null; then
88		warn "failed to gcore $pid: $prog $cont $kpath"
89	fi
90
91	kill -ABRT $pid
92	fg %1
93
94	#
95	# Since we have the pid, go through and check this now.
96	#
97	if $core_checker $core_tmpdir/*.kernel.$c.$pid $c; then
98		printf "TEST PASSED: kernel %s %s\n" "$pbase" "$c"
99	else
100		warn "checker failed for kernel $c"
101	fi
102
103	if $core_checker $core_tmpdir/*.gcore.$c.$pid $c; then
104		printf "TEST PASSED: gcore %s %s\n" "$pbase" "$c"
105	else
106		warn "checker failed for gcore of $c"
107	fi
108}
109
110if [[ ! -x "$core_dumper32" || ! -x "$core_dumper64" || \
111     ! -f "$core_checker" ]]; then
112	warn "missing expected files"
113	exit $core_exit
114fi
115
116if ! mkdir "$core_tmpdir"; then
117	warn "failed to create temporary directory: $core_tmpdir"
118	exit $core_exit
119fi
120
121for c in $core_contents; do
122	kpattern="$core_tmpdir/%f.kernel.$c.%p"
123	gpattern="$core_tmpdir/%f.gcore.$c"
124
125	core_dump_one "$core_dumper32" "$c" "$kpattern" "$gpattern"
126	core_dump_one "$core_dumper64" "$c" "$kpattern" "$gpattern"
127
128done
129
130if (( core_exit == 0 )); then
131	printf "All tests passed successfully\n"
132fi
133
134rm -rf $core_tmpdir
135exit $core_exit
136