xref: /freebsd/cddl/usr.sbin/dtrace/tests/tools/dtest.sh (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1#!/bin/sh
2
3# $FreeBSD$
4
5usage()
6{
7    cat >&2 <<__EOF__
8A harness for test cases in the DTrace test suite.
9
10usage: $(basename $0) <testfile>
11__EOF__
12    exit 1
13}
14
15gettag()
16{
17    local tag
18
19    tag=$(basename $1)
20    tag=${tag#*.}
21    tag=${tag%%[a-z.]*}
22    echo $tag
23}
24
25runtest()
26{
27    local dflags exe exstatus pid retval status
28
29    exstatus=0
30    retval=0
31
32    case $TFILE in
33    drp.DTRACEDROP_*.d|err.*.d|tst.*.d)
34        case $TFILE in
35        drp.DTRACEDROP_*.d)
36            dflags="-x droptags"
37            tag=$(gettag "$TFILE")
38            ;;
39        err.D_*.d)
40            exstatus=1
41            dflags="-x errtags"
42            tag=$(gettag "$TFILE")
43            ;;
44        err.*.d)
45            exstatus=1
46            ;;
47        esac
48
49        exe=${TFILE%.*}.exe
50        if [ -f "$exe" -a -x "$exe" ]; then
51            ./$exe &
52            pid=$!
53            dflags="$dflags ${pid}"
54        fi
55
56        dtrace -C -s "${TFILE}" $dflags >$STDOUT 2>$STDERR
57        status=$?
58
59        if [ $status -ne $exstatus ]; then
60            ERRMSG="dtrace exited with status ${status}, expected ${exstatus}"
61            retval=1
62        elif [ -n "${tag}" ] && ! grep -Fq " [${tag}] " ${STDERR}; then
63            ERRMSG="dtrace's error output did not contain expected tag ${tag}"
64            retval=1
65        fi
66
67        if [ -n "$pid" ]; then
68            kill -0 $pid >/dev/null 2>&1 && kill -9 $pid >/dev/null 2>&1
69            wait
70        fi
71        ;;
72    err.*.ksh|tst.*.ksh)
73        expr "$TFILE" : 'err.*' >/dev/null && exstatus=1
74
75        tst=$TFILE ksh "$TFILE" /usr/sbin/dtrace >$STDOUT 2>$STDERR
76        status=$?
77
78        if [ $status -ne $exstatus ]; then
79            ERRMSG="script exited with status ${status}, expected ${exstatus}"
80            retval=1
81        fi
82        ;;
83    *)
84        ERRMSG="unexpected test file name $TFILE"
85        retval=1
86        ;;
87    esac
88
89    if [ $retval -eq 0 ] && \
90        head -n 1 $STDOUT | grep -q -E '^#!/.*ksh$'; then
91        ksh $STDOUT
92        retval=$?
93    fi
94
95    return $retval
96}
97
98[ $# -eq 1 ] || usage
99
100readonly STDERR=$(mktemp)
101readonly STDOUT=$(mktemp)
102readonly TFILE=$(basename $1)
103readonly EXOUT=${TFILE}.out
104
105kldstat -q -m dtrace_test || kldload dtrace_test
106cd $(dirname $1)
107runtest
108RESULT=$?
109
110if [ $RESULT -eq 0 -a -f $EXOUT -a -r $EXOUT ] && \
111   ! cmp $STDOUT $EXOUT >/dev/null 2>&1; then
112    ERRMSG="test output mismatch"
113    RESULT=1
114fi
115
116if [ $RESULT -ne 0 ]; then
117    echo "test $TFILE failed: $ERRMSG" >&2
118    if [ $(stat -f '%z' $STDOUT) -gt 0 ]; then
119        cat >&2 <<__EOF__
120test stdout:
121--
122$(cat $STDOUT)
123--
124__EOF__
125    fi
126    if [ $(stat -f '%z' $STDERR) -gt 0 ]; then
127        cat >&2 <<__EOF__
128test stderr:
129--
130$(cat $STDERR)
131--
132__EOF__
133    fi
134fi
135
136rm -f $STDERR $STDOUT
137exit $RESULT
138