3 # Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org>
4 # Copyright (c) 2011-2017 The strace developers.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 3. The name of the author may not be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 warn_() { printf >&2 '%s\n' "$*"; }
35 fail_() { warn_ "$ME_: failed test: $*"; exit 1; }
36 skip_() { warn_ "$ME_: skipped test: $*"; exit 77; }
37 framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; }
38 framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; }
42 type "$@" > /dev/null 2>&1 ||
43 framework_skip_ "$* is not available"
46 dump_log_and_fail_with()
60 if [ $rc -eq 77 ]; then
61 skip_ "$args exited with code 77"
63 fail_ "$args failed with code $rc"
69 run_prog_skip_if_failed()
72 "$@" || framework_skip_ "$args failed with code $?"
81 if [ $rc -eq 77 ]; then
84 fail_ "$* failed with code $rc"
91 > "$LOG" || fail_ "failed to write $LOG"
93 $STRACE -o "$LOG" "$@" ||
94 dump_log_and_fail_with "$STRACE $args failed with code $?"
99 rm -f -- "$LOG".[0-9]*
100 run_strace -ff -tt "$@"
101 "$srcdir"/../strace-log-merge "$LOG" > "$LOG" ||
102 dump_log_and_fail_with 'strace-log-merge failed with code $?'
103 rm -f -- "$LOG".[0-9]*
111 local program="$1"; shift
112 if grep '^@include[[:space:]]' < "$program" > /dev/null; then
113 gawk '@include "/dev/null"' < /dev/null ||
114 framework_skip_ 'gawk does not support @include'
118 # Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
119 # Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk.
120 # If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE.
123 local output program error
124 if [ $# -eq 0 ]; then
129 if [ $# -eq 0 ]; then
130 program="$srcdir/$NAME.awk"
134 if [ $# -eq 0 ]; then
135 error="$STRACE $args output mismatch"
140 check_gawk "$program"
142 AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || {
148 # Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
149 # Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
150 # If it does, dump the difference and fail with ERROR_MESSAGE.
153 local output expected error
154 if [ $# -eq 0 ]; then
159 if [ $# -eq 0 ]; then
160 expected="$srcdir/$NAME.expected"
164 if [ $# -eq 0 ]; then
165 error="$STRACE $args output mismatch"
172 diff -u -- "$expected" "$output" ||
176 # Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
177 # Check whether all patterns listed in FILE_WITH_PATTERNS
178 # match FILE_TO_CHECK using egrep.
179 # If at least one of these patterns does not match,
180 # dump both files and fail with ERROR_MESSAGE.
183 local output patterns error pattern cnt failed=
184 if [ $# -eq 0 ]; then
189 if [ $# -eq 0 ]; then
190 patterns="$srcdir/$NAME.expected"
194 if [ $# -eq 0 ]; then
195 error="$STRACE $args output mismatch"
204 while read -r pattern; do
205 LC_ALL=C grep -E -x -e "$pattern" < "$output" > /dev/null || {
206 test -n "$failed" || {
207 echo 'Failed patterns of expected output:'
210 printf '#%d: %s\n' "$cnt" "$pattern"
214 test -z "$failed" || {
215 echo 'Actual output:'
221 # Usage: run_strace_match_diff [args to run_strace]
222 run_strace_match_diff()
225 [ -n "$args" -a -z "${args##*-e trace=*}" ] ||
226 set -- -e trace="$NAME" "$@"
228 run_strace "$@" $args > "$EXP"
229 match_diff "$LOG" "$EXP"
232 # Print kernel version code.
233 # usage: kernel_version_code $(uname -r)
234 kernel_version_code()
240 v1="${1%%[!0-9]*}" && [ -n "$v1" ] || v1=0
241 v2="${2%%[!0-9]*}" && [ -n "$v2" ] || v2=0
242 v3="${3%%[!0-9]*}" && [ -n "$v3" ] || v3=0
243 echo "$(($v1 * 65536 + $v2 * 256 + $v3))"
247 # Usage: require_min_kernel_version_or_skip 3.0
248 require_min_kernel_version_or_skip()
251 uname_r="$(uname -r)"
253 [ "$(kernel_version_code "$uname_r")" -ge \
254 "$(kernel_version_code "$1")" ] ||
255 skip_ "the kernel release $uname_r is not $1 or newer"
258 # Usage: grep_pid_status $pid GREP-OPTIONS...
263 cat < "/proc/$pid/status" | grep "$@"
266 # Subtracts one program set from another.
267 # If an optional regular expression is specified, the lines in the minuend file
268 # that match this regular expression are elso excluded from the output.
270 # Usage: prog_set_subtract minuend_file subtrahend_file [subtrahend_regexp]
277 pat="$re|$(sed 's/[[:space:]].*//' < "$sub" | tr -s '\n' '|')"
278 grep -E -v -x -e "$pat" < "$min"
281 # Usage: test_pure_prog_set [--expfile FILE] COMMON_ARGS < tests_file
282 # stdin should consist of lines in "test_name strace_args..." format.
289 while [ -n "$1" ]; do
302 while read -r t prog_args; do {
303 # skip lines beginning with "#" symbol
304 [ "${t###}" = "$t" ] || continue
306 try_run_prog "../$t" || continue
307 run_strace $prog_args "$@" "../$t" > "$expfile"
308 match_diff "$LOG" "$expfile"
312 # Run strace against list of programs put in "$NAME.in" and then against the
313 # rest of pure_executables.list with the expectation of empty output in the
316 # Usage: source this file after init.sh and call:
317 # test_trace_expr subtrahend_regexp strace_args
319 # $NAME: test name, used for "$NAME.in" file containing list of tests
320 # for positive trace expression match;
321 # $srcdir: used to find pure_executables.list and "$NAME.in" files.
323 # negative.list: File containing list of tests for negative match.
326 local subtrahend_regexp
327 subtrahend_regexp="$1"; shift
328 test_pure_prog_set "$@" < "$srcdir/$NAME.in"
329 prog_set_subtract "$srcdir/pure_executables.list" "$srcdir/$NAME.in" \
330 "$subtrahend_regexp" > negative.list
331 test_pure_prog_set --expfile /dev/null -qq -esignal=none "$@" \
339 *.gen.test) NAME="${ME_%.gen.test}" ;;
340 *.test) NAME="${ME_%.test}" ;;
344 if [ -n "$NAME" ]; then
352 *) srcdir="../$srcdir" ;;
355 [ -n "${STRACE-}" ] || {
357 case "${LOG_COMPILER-} ${LOG_FLAGS-}" in
358 *--suppressions=*--error-exitcode=*--tool=*)
359 # add valgrind command prefix
360 STRACE="${LOG_COMPILER-} ${LOG_FLAGS-} $STRACE"
365 trap 'dump_log_and_fail_with "time limit ($TIMEOUT_DURATION) exceeded"' XCPU
367 [ -n "${STRACE-}" ] ||
371 : "${TIMEOUT_DURATION:=300}"
372 : "${SLEEP_A_BIT:=sleep 1}"
374 [ -z "${VERBOSE-}" ] ||