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