1d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#!/bin/sh
2d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#
3d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# Check strace options syntax.
4d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#
5d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes# Copyright (c) 2016-2018 The strace developers.
7d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# All rights reserved.
8d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#
9d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# Redistribution and use in source and binary forms, with or without
10d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# modification, are permitted provided that the following conditions
11d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# are met:
12d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# 1. Redistributions of source code must retain the above copyright
13d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#    notice, this list of conditions and the following disclaimer.
14d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# 2. Redistributions in binary form must reproduce the above copyright
15d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#    notice, this list of conditions and the following disclaimer in the
16d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#    documentation and/or other materials provided with the distribution.
17d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# 3. The name of the author may not be used to endorse or promote products
18d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#    derived from this software without specific prior written permission.
19d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes#
20d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
3177c3ff8e0644f1e120e4b2ebc7222150b0446f3bElliott Hughes. "${srcdir=.}/syntax.sh"
32d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
33d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Invalid process id: '0'" -p 0
34d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Invalid process id: '-42'" -p -42
35d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Invalid process id: '$$.'" -p $$.
36d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Invalid process id: 'a'" -p 1,a
37d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Syscall 'chdir' for -b isn't supported" -b chdir
38d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_e "Syscall 'chdir' for -b isn't supported" -b execve -b chdir
39d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
40b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughescheck_e_using_grep 'exec: File *name too long' "$(printf '%4096s' ' ')"
41d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
42b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughesff_name="$(printf '%4084s' ' ')"
43b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughescheck_e_using_grep "$ff_name: File *name too long" -ff -o "$ff_name" true
4439bac055674d23770b9a724221b728e443196ea7Elliott Hughes
45d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h 'must have PROG [ARGS] or -p PID'
46d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h 'PROG [ARGS] must be specified with -D' -D -p $$
47d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h '-c and -C are mutually exclusive' -c -C true
48d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h '-c and -C are mutually exclusive' -C -c true
49d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h '(-c or -C) and -ff are mutually exclusive' -c -ff true
50d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h '(-c or -C) and -ff are mutually exclusive' -C -ff true
51d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h '-w must be given with (-c or -C)' -w true
52d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h 'piping the output and -ff are mutually exclusive' -o '|' -ff true
53d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h 'piping the output and -ff are mutually exclusive' -o '!' -ff true
54d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h "invalid -a argument: '-42'" -a -42
55d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h "invalid -O argument: '-42'" -O -42
56d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h "invalid -s argument: '-42'" -s -42
57dc75b01004a0588c1eb3bc26d7248a6e473b2cddElliott Hughescheck_h "invalid -s argument: '1073741824'" -s 1073741824
58d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughescheck_h "invalid -I argument: '5'" -I 5
59d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
60b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes../zeroargc "$STRACE_EXE" /bin/true 2> "$LOG" &&
61dc75b01004a0588c1eb3bc26d7248a6e473b2cddElliott Hughes	dump_log_and_fail_with \
62dc75b01004a0588c1eb3bc26d7248a6e473b2cddElliott Hughes		'zeroargc strace failed to handle the error properly'
63bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughescat > "$EXP" << __EOF__
64b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: must have PROG [ARGS] or -p PID
65b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott HughesTry '$STRACE_EXE -h' for more information.
66bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes__EOF__
67bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughesdiff -u -- "$EXP" "$LOG" > /dev/null || {
68bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	cat > "$EXP" <<- '__EOF__'
69bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	strace: must have PROG [ARGS] or -p PID
70bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	Try 'strace -h' for more information.
71bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	__EOF__
72bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes	match_diff "$LOG" "$EXP" \
73bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes		"zeroargc $STRACE $args output mismatch"
74bbf97dc20c4ee0e36b4628fe03e0e7db5a11d469Elliott Hughes}
75dc75b01004a0588c1eb3bc26d7248a6e473b2cddElliott Hughes
76d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesif [ -n "${UID-}" ]; then
77d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	if [ "${UID-}" = 0 ]; then
78d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		umsg="Cannot find user ':nosuchuser:'"
79d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	else
80d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		umsg='You must be root to use the -u option'
81d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	fi
82d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
83d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	check_e "$umsg" -u :nosuchuser: true
84d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
85d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	for c in i r t T y; do
86d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		check_e "-$c has no effect with -c
87b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $umsg" -u :nosuchuser: -c -$c true
88d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	done
89d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		check_e "-i has no effect with -c
90b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: -r has no effect with -c
91b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: -t has no effect with -c
92b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: -T has no effect with -c
93b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: -y has no effect with -c
94b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $umsg" -u :nosuchuser: -cirtTy true
95d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
96d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	check_e "-tt has no effect with -r
97b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: $umsg" -u :nosuchuser: -r -tt true
98d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesfi
99d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
100d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesargs='-p 2147483647'
101d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes$STRACE $args 2> "$LOG" &&
102d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	dump_log_and_fail_with \
103d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		"strace $args failed to handle the error properly"
104d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
105d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesfor cmd in PTRACE_SEIZE PTRACE_ATTACH; do
106d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	cat > "$EXP" << __EOF__
107b755614143ce6aab5265ed32c1bb6c8f748e7898Elliott Hughes$STRACE_EXE: attach: ptrace($cmd, 2147483647): No such process
108d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes__EOF__
109d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	diff -- "$EXP" "$LOG" ||
110d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		continue
111d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	args=
112d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	break
113d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughesdone
114d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes
115d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes[ -z "$args" ] ||
116d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes	dump_log_and_fail_with \
117d35df493b4e7684c50d2d2fa032ee3a7ac228009Elliott Hughes		"strace $args failed to print expected diagnostics"
118