]> granicus.if.org Git - strace/blob - tests/options-syntax.test
Implement -e trace=/regex option
[strace] / tests / options-syntax.test
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
32 check_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
42 check_exit_status_and_stderr_using_grep()
43 {
44         $STRACE "$@" 2> "$LOG" &&
45                 dump_log_and_fail_with \
46                         "strace $* failed to handle the error properly"
47         match_grep "$LOG" "$EXP" ||
48                 dump_log_and_fail_with \
49                         "strace $* failed to print expected diagnostics"
50 }
51
52 strace_exp="${STRACE##* }"
53
54 check_e()
55 {
56         local pattern="$1"; shift
57         cat > "$EXP" << __EOF__
58 $strace_exp: $pattern
59 __EOF__
60         check_exit_status_and_stderr "$@"
61 }
62
63 check_e_using_grep()
64 {
65         local pattern="$1"; shift
66         cat > "$EXP" << __EOF__
67 $strace_exp: $pattern
68 __EOF__
69         check_exit_status_and_stderr_using_grep "$@"
70 }
71
72 check_h()
73 {
74         local pattern="$1"; shift
75         cat > "$EXP" << __EOF__
76 $strace_exp: $pattern
77 Try '$strace_exp -h' for more information.
78 __EOF__
79         check_exit_status_and_stderr "$@"
80 }
81
82 check_e "Invalid process id: '0'" -p 0
83 check_e "Invalid process id: '-42'" -p -42
84 check_e "Invalid process id: '$$.'" -p $$.
85 check_e "Invalid process id: 'a'" -p 1,a
86 check_e "Syscall 'chdir' for -b isn't supported" -b chdir
87 check_e "Syscall 'chdir' for -b isn't supported" -b execve -b chdir
88
89 check_e "invalid system call '-1'" -e-1
90 check_e "invalid system call '-2'" -e -2
91 check_e "invalid system call '-3'" -etrace=-3
92 check_e "invalid system call '-4'" -e trace=-4
93 check_e "invalid system call '-5'" -e trace=1,-5
94 check_e "invalid system call '/non_syscall'" -e trace=/non_syscall
95 check_e "invalid system call '2147483647'" -e 2147483647
96 check_e "invalid system call '2147483648'" -e 2147483648
97 check_e "invalid system call '4294967295'" -e 4294967295
98 check_e "invalid system call '4294967296'" -e 4294967296
99
100 check_e "invalid descriptor '-1'" -eread=-1
101 check_e "invalid descriptor '-42'" -ewrite=-42
102 check_e "invalid descriptor '2147483648'" -eread=2147483648
103 check_e "invalid descriptor '4294967296'" -ewrite=4294967296
104 check_e "invalid descriptor 'foo'" -eread=foo
105 check_e "invalid descriptor ''" -ewrite=
106 check_e "invalid descriptor ','" -eread=,
107 check_e "invalid descriptor '!'" -ewrite='!'
108 check_e "invalid descriptor '!'" -eread='0,!'
109 check_e "invalid descriptor '!,'" -ewrite='!,'
110
111 check_e_using_grep 'regcomp: \+id: [[:alpha:]].+' -e trace='/+id'
112 check_e_using_grep 'regcomp: \*id: [[:alpha:]].+' -e trace='/*id'
113 check_e_using_grep 'regcomp: \{id: [[:alpha:]].+' -e trace='/{id'
114 check_e_using_grep 'regcomp: \(id: [[:alpha:]].+' -e trace='/(id'
115 check_e_using_grep 'regcomp: \[id: [[:alpha:]].+' -e trace='/[id'
116
117 check_h 'must have PROG [ARGS] or -p PID'
118 check_h 'PROG [ARGS] must be specified with -D' -D -p $$
119 check_h '-c and -C are mutually exclusive' -c -C true
120 check_h '-c and -C are mutually exclusive' -C -c true
121 check_h '(-c or -C) and -ff are mutually exclusive' -c -ff true
122 check_h '(-c or -C) and -ff are mutually exclusive' -C -ff true
123 check_h '-w must be given with (-c or -C)' -w true
124 check_h 'piping the output and -ff are mutually exclusive' -o '|' -ff true
125 check_h 'piping the output and -ff are mutually exclusive' -o '!' -ff true
126 check_h "invalid -a argument: '-42'" -a -42
127 check_h "invalid -O argument: '-42'" -O -42
128 check_h "invalid -s argument: '-42'" -s -42
129 check_h "invalid -I argument: '5'" -I 5
130
131 if [ -n "${UID-}" ]; then
132         if [ "${UID-}" = 0 ]; then
133                 umsg="Cannot find user ':nosuchuser:'"
134         else
135                 umsg='You must be root to use the -u option'
136         fi
137
138         check_e "$umsg" -u :nosuchuser: true
139
140         for c in i r t T y; do
141                 check_e "-$c has no effect with -c
142 $strace_exp: $umsg" -u :nosuchuser: -c -$c true
143         done
144                 check_e "-i has no effect with -c
145 $strace_exp: -r has no effect with -c
146 $strace_exp: -t has no effect with -c
147 $strace_exp: -T has no effect with -c
148 $strace_exp: -y has no effect with -c
149 $strace_exp: $umsg" -u :nosuchuser: -cirtTy true
150
151         check_e "-tt has no effect with -r
152 $strace_exp: $umsg" -u :nosuchuser: -r -tt true
153 fi
154
155 args='-p 2147483647'
156 $STRACE $args 2> "$LOG" &&
157         dump_log_and_fail_with \
158                 "strace $args failed to handle the error properly"
159
160 for cmd in PTRACE_SEIZE PTRACE_ATTACH; do
161         cat > "$EXP" << __EOF__
162 $strace_exp: attach: ptrace($cmd, 2147483647): No such process
163 __EOF__
164         diff -- "$EXP" "$LOG" ||
165                 continue
166         args=
167         break
168 done
169
170 [ -z "$args" ] ||
171         dump_log_and_fail_with \
172                 "strace $args failed to print expected diagnostics"