2 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3 * Copyright (c) 2016-2017 The strace developers.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "number_set.h"
34 struct number_set *read_set;
35 struct number_set *write_set;
36 struct number_set *signal_set;
38 static struct number_set *abbrev_set;
39 static struct number_set *inject_set;
40 static struct number_set *raw_set;
41 static struct number_set *trace_set;
42 static struct number_set *verbose_set;
45 sigstr_to_uint(const char *s)
49 if (*s >= '0' && *s <= '9')
50 return string_to_uint_upto(s, 255);
52 if (strncasecmp(s, "SIG", 3) == 0)
55 for (i = 0; i <= 255; ++i) {
56 const char *name = signame(i);
58 if (strncasecmp(name, "SIG", 3) != 0)
63 if (strcasecmp(name, s) != 0)
73 find_errno_by_name(const char *name)
77 for (i = 1; i < nerrnos; ++i) {
78 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
86 parse_inject_token(const char *const token, struct inject_opts *const fopts,
87 const bool fault_tokens_only)
92 if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
100 intval = string_to_uint_ex(val, &end, 0xffff, "+");
104 fopts->first = intval;
110 intval = string_to_uint_upto(val, 0xffff);
113 fopts->step = intval;
122 } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
123 if (fopts->data.flags & INJECT_F_RETVAL)
125 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
127 intval = find_errno_by_name(val);
130 fopts->data.rval = -intval;
131 fopts->data.flags |= INJECT_F_RETVAL;
132 } else if (!fault_tokens_only
133 && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
134 if (fopts->data.flags & INJECT_F_RETVAL)
136 intval = string_to_uint(val);
139 fopts->data.rval = intval;
140 fopts->data.flags |= INJECT_F_RETVAL;
141 } else if (!fault_tokens_only
142 && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
143 if (fopts->data.flags & INJECT_F_SIGNAL)
145 intval = sigstr_to_uint(val);
146 if (intval < 1 || intval > NSIG_BYTES * 8)
148 fopts->data.signo = intval;
149 fopts->data.flags |= INJECT_F_SIGNAL;
158 parse_inject_expression(const char *const s, char **buf,
159 struct inject_opts *const fopts,
160 const bool fault_tokens_only)
162 char *saveptr = NULL;
167 for (token = strtok_r(*buf, ":", &saveptr); token;
168 token = strtok_r(NULL, ":", &saveptr)) {
171 else if (!parse_inject_token(token, fopts, fault_tokens_only))
184 qualify_read(const char *const str)
187 read_set = alloc_number_set_array(1);
188 qualify_tokens(str, read_set, string_to_uint, "descriptor");
192 qualify_write(const char *const str)
195 write_set = alloc_number_set_array(1);
196 qualify_tokens(str, write_set, string_to_uint, "descriptor");
200 qualify_signals(const char *const str)
203 signal_set = alloc_number_set_array(1);
204 qualify_tokens(str, signal_set, sigstr_to_uint, "signal");
208 qualify_trace(const char *const str)
211 trace_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
212 qualify_syscall_tokens(str, trace_set, "system call");
216 qualify_abbrev(const char *const str)
219 abbrev_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
220 qualify_syscall_tokens(str, abbrev_set, "system call");
224 qualify_verbose(const char *const str)
227 verbose_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
228 qualify_syscall_tokens(str, verbose_set, "system call");
232 qualify_raw(const char *const str)
235 raw_set = alloc_number_set_array(SUPPORTED_PERSONALITIES);
236 qualify_syscall_tokens(str, raw_set, "system call");
240 qualify_inject_common(const char *const str,
241 const bool fault_tokens_only,
242 const char *const description)
244 struct inject_opts opts = {
249 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
251 error_msg_and_die("invalid %s '%s'", description, str);
254 /* If neither of retval, error, or signal is specified, then ... */
255 if (!opts.data.flags) {
256 if (fault_tokens_only) {
257 /* in fault= syntax the default error code is ENOSYS. */
258 opts.data.rval = -ENOSYS;
259 opts.data.flags |= INJECT_F_RETVAL;
261 /* in inject= syntax this is not allowed. */
262 error_msg_and_die("invalid %s '%s'", description, str);
266 struct number_set *tmp_set =
267 alloc_number_set_array(SUPPORTED_PERSONALITIES);
268 qualify_syscall_tokens(name, tmp_set, description);
273 * Initialize inject_vec accourding to tmp_set.
274 * Merge tmp_set into inject_set.
277 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
278 if (number_set_array_is_empty(tmp_set, p))
283 alloc_number_set_array(SUPPORTED_PERSONALITIES);
285 if (!inject_vec[p]) {
286 inject_vec[p] = xcalloc(nsyscall_vec[p],
287 sizeof(*inject_vec[p]));
291 for (i = 0; i < nsyscall_vec[p]; ++i) {
292 if (is_number_in_set_array(i, tmp_set, p)) {
293 add_number_to_set_array(i, inject_set, p);
294 inject_vec[p][i] = opts;
299 free_number_set_array(tmp_set, SUPPORTED_PERSONALITIES);
303 qualify_fault(const char *const str)
305 qualify_inject_common(str, true, "fault argument");
309 qualify_inject(const char *const str)
311 qualify_inject_common(str, false, "inject argument");
314 static const struct qual_options {
316 void (*qualify)(const char *);
318 { "trace", qualify_trace },
319 { "t", qualify_trace },
320 { "abbrev", qualify_abbrev },
321 { "a", qualify_abbrev },
322 { "verbose", qualify_verbose },
323 { "v", qualify_verbose },
324 { "raw", qualify_raw },
325 { "x", qualify_raw },
326 { "signal", qualify_signals },
327 { "signals", qualify_signals },
328 { "s", qualify_signals },
329 { "read", qualify_read },
330 { "reads", qualify_read },
331 { "r", qualify_read },
332 { "write", qualify_write },
333 { "writes", qualify_write },
334 { "w", qualify_write },
335 { "fault", qualify_fault },
336 { "inject", qualify_inject },
340 qualify(const char *str)
342 const struct qual_options *opt = qual_options;
345 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
346 const char *name = qual_options[i].name;
347 const size_t len = strlen(name);
348 const char *val = str_strip_prefix_len(str, name, len);
350 if (val == str || *val != '=')
353 opt = &qual_options[i];
361 qual_flags(const unsigned int scno)
363 return (is_number_in_set_array(scno, trace_set, current_personality)
365 | (is_number_in_set_array(scno, abbrev_set, current_personality)
367 | (is_number_in_set_array(scno, verbose_set, current_personality)
369 | (is_number_in_set_array(scno, raw_set, current_personality)
371 | (is_number_in_set_array(scno, inject_set, current_personality)