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