3 # Copyright (c) 2011-2016 Dmitry V. Levin <ldv@altlinux.org>
4 # Copyright (c) 2011-2018 The strace developers.
7 # SPDX-License-Identifier: GPL-2.0-or-later
15 warn_() { printf >&2 '%s\n' "$*"; }
16 fail_() { warn_ "$ME_: failed test: $*"; exit 1; }
17 skip_() { warn_ "$ME_: skipped test: $*"; exit 77; }
18 framework_failure_() { warn_ "$ME_: framework failure: $*"; exit 99; }
19 framework_skip_() { warn_ "$ME_: framework skip: $*"; exit 77; }
23 type "$@" > /dev/null 2>&1 ||
24 framework_skip_ "$* is not available"
27 dump_log_and_fail_with()
41 if [ $rc -eq 77 ]; then
42 skip_ "$args exited with code 77"
44 fail_ "$args failed with code $rc"
50 run_prog_skip_if_failed()
53 "$@" || framework_skip_ "$args failed with code $?"
62 if [ $rc -eq 77 ]; then
65 fail_ "$* failed with code $rc"
72 > "$LOG" || fail_ "failed to write $LOG"
74 $STRACE -o "$LOG" "$@" ||
75 dump_log_and_fail_with "$STRACE $args failed with code $?"
80 rm -f -- "$LOG".[0-9]*
81 run_strace -ff -tt "$@"
82 "$srcdir"/../strace-log-merge "$LOG" > "$LOG" ||
83 dump_log_and_fail_with 'strace-log-merge failed with code $?'
84 rm -f -- "$LOG".[0-9]*
92 local program="$1"; shift
93 if grep '^@include[[:space:]]' < "$program" > /dev/null; then
94 gawk '@include "/dev/null"' < /dev/null ||
95 framework_skip_ 'gawk does not support @include'
99 # Usage: [FILE_TO_CHECK [AWK_PROGRAM [ERROR_MESSAGE [EXTRA_AWK_OPTIONS...]]]]
100 # Check whether AWK_PROGRAM matches FILE_TO_CHECK using gawk.
101 # If it doesn't, dump FILE_TO_CHECK and fail with ERROR_MESSAGE.
104 local output program error
105 if [ $# -eq 0 ]; then
110 if [ $# -eq 0 ]; then
111 program="$srcdir/$NAME.awk"
115 if [ $# -eq 0 ]; then
116 error="$STRACE $args output mismatch"
121 check_gawk "$program"
123 AWKPATH="$srcdir" gawk -f "$program" "$@" < "$output" || {
129 # Usage: [FILE_TO_CHECK [FILE_TO_COMPATE_WITH [ERROR_MESSAGE]]]
130 # Check whether FILE_TO_CHECK differs from FILE_TO_COMPATE_WITH.
131 # If it does, dump the difference and fail with ERROR_MESSAGE.
134 local output expected error
135 if [ $# -eq 0 ]; then
140 if [ $# -eq 0 ]; then
141 expected="$srcdir/$NAME.expected"
145 if [ $# -eq 0 ]; then
146 error="$STRACE $args output mismatch"
153 diff -u -- "$expected" "$output" ||
157 # Usage: [FILE_TO_CHECK [FILE_WITH_PATTERNS [ERROR_MESSAGE]]]
158 # Check whether all patterns listed in FILE_WITH_PATTERNS
159 # match FILE_TO_CHECK using egrep.
160 # If at least one of these patterns does not match,
161 # dump both files and fail with ERROR_MESSAGE.
164 local output patterns error pattern cnt failed=
165 if [ $# -eq 0 ]; then
170 if [ $# -eq 0 ]; then
171 patterns="$srcdir/$NAME.expected"
175 if [ $# -eq 0 ]; then
176 error="$STRACE $args output mismatch"
185 while read -r pattern; do
186 LC_ALL=C grep -E -x -e "$pattern" < "$output" > /dev/null || {
187 test -n "$failed" || {
188 echo 'Failed patterns of expected output:'
191 printf '#%d: %s\n' "$cnt" "$pattern"
195 test -z "$failed" || {
196 echo 'Actual output:'
202 # Usage: run_strace_match_diff [args to run_strace]
203 run_strace_match_diff()
206 [ -n "$args" -a -z "${args##*-e trace=*}" ] ||
207 set -- -e trace="$NAME" "$@"
209 run_strace "$@" $args > "$EXP"
210 match_diff "$LOG" "$EXP"
213 # Usage: run_strace_match_grep [args to run_strace]
214 run_strace_match_grep()
217 [ -n "$args" -a -z "${args##*-e trace=*}" ] ||
218 set -- -e trace="$NAME" "$@"
220 run_strace "$@" $args > "$EXP"
221 match_grep "$LOG" "$EXP"
224 # Print kernel version code.
225 # usage: kernel_version_code $(uname -r)
226 kernel_version_code()
232 v1="${1%%[!0-9]*}" && [ -n "$v1" ] || v1=0
233 v2="${2%%[!0-9]*}" && [ -n "$v2" ] || v2=0
234 v3="${3%%[!0-9]*}" && [ -n "$v3" ] || v3=0
235 echo "$(($v1 * 65536 + $v2 * 256 + $v3))"
239 # Usage: require_min_kernel_version_or_skip 3.0
240 require_min_kernel_version_or_skip()
243 uname_r="$(uname -r)"
245 [ "$(kernel_version_code "$uname_r")" -ge \
246 "$(kernel_version_code "$1")" ] ||
247 skip_ "the kernel release $uname_r is not $1 or newer"
250 # Usage: grep_pid_status $pid GREP-OPTIONS...
255 cat < "/proc/$pid/status" | grep "$@"
258 # Subtracts one program set from another.
259 # If an optional regular expression is specified, the lines in the minuend file
260 # that match this regular expression are elso excluded from the output.
262 # Usage: prog_set_subtract minuend_file subtrahend_file [subtrahend_regexp]
269 pat="$re|$(sed 's/[[:space:]].*//' < "$sub" | tr -s '\n' '|')"
270 grep -E -v -x -e "$pat" < "$min"
273 # Usage: test_pure_prog_set [--expfile FILE] COMMON_ARGS < tests_file
274 # stdin should consist of lines in "test_name strace_args..." format.
281 while [ -n "$1" ]; do
294 while read -r t prog_args; do {
295 # skip lines beginning with "#" symbol
296 [ "${t###}" = "$t" ] || continue
298 try_run_prog "../$t" || continue
299 run_strace $prog_args "$@" "../$t" > "$expfile"
300 match_diff "$LOG" "$expfile"
304 # Run strace against list of programs put in "$NAME.in" and then against the
305 # rest of pure_executables.list with the expectation of empty output in the
308 # Usage: source this file after init.sh and call:
309 # test_trace_expr subtrahend_regexp strace_args
311 # $NAME: test name, used for "$NAME.in" file containing list of tests
312 # for positive trace expression match;
313 # $srcdir: used to find pure_executables.list and "$NAME.in" files.
315 # negative.list: File containing list of tests for negative match.
318 local subtrahend_regexp
319 subtrahend_regexp="$1"; shift
320 test_pure_prog_set "$@" < "$srcdir/$NAME.in"
321 prog_set_subtract "$srcdir/pure_executables.list" "$srcdir/$NAME.in" \
322 "$subtrahend_regexp" > negative.list
323 test_pure_prog_set --expfile /dev/null -qq -esignal=none "$@" \
331 *.gen.test) NAME="${ME_%.gen.test}" ;;
332 *.test) NAME="${ME_%.test}" ;;
337 if [ -n "$NAME" ]; then
345 *) srcdir="../$srcdir" ;;
348 [ -n "${STRACE-}" ] || {
350 case "${LOG_COMPILER-} ${LOG_FLAGS-}" in
351 *--suppressions=*--error-exitcode=*--tool=*)
353 # add valgrind command prefix
354 STRACE="${LOG_COMPILER-} ${LOG_FLAGS-} $STRACE"
359 trap 'dump_log_and_fail_with "time limit ($TIMEOUT_DURATION) exceeded"' XCPU
361 : "${STRACE:=../strace}"
364 # Export $STRACE_EXE to check_PROGRAMS.
365 : "${STRACE_EXE:=$STRACE}"
368 : "${TIMEOUT_DURATION:=600}"
369 : "${SLEEP_A_BIT:=sleep 1}"
371 [ -z "${VERBOSE-}" ] ||