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