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.
33 typedef unsigned int number_slot_t;
34 #define BITS_PER_SLOT (sizeof(number_slot_t) * 8)
42 struct number_set read_set;
43 struct number_set write_set;
44 struct number_set signal_set;
46 static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
47 static struct number_set inject_set[SUPPORTED_PERSONALITIES];
48 static struct number_set raw_set[SUPPORTED_PERSONALITIES];
49 static struct number_set trace_set[SUPPORTED_PERSONALITIES];
50 static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
53 number_setbit(const unsigned int i, number_slot_t *const vec)
55 vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT);
59 number_isset(const unsigned int i, const number_slot_t *const vec)
61 return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT));
65 reallocate_number_set(struct number_set *const set, const unsigned int new_nslots)
67 if (new_nslots <= set->nslots)
69 set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec));
70 memset(set->vec + set->nslots, 0,
71 sizeof(*set->vec) * (new_nslots - set->nslots));
72 set->nslots = new_nslots;
76 add_number_to_set(const unsigned int number, struct number_set *const set)
78 reallocate_number_set(set, number / BITS_PER_SLOT + 1);
79 number_setbit(number, set->vec);
83 is_number_in_set(const unsigned int number, const struct number_set *const set)
85 return ((number / BITS_PER_SLOT < set->nslots)
86 && number_isset(number, set->vec)) ^ set->not;
89 typedef int (*string_to_uint_func)(const char *);
92 * Add numbers to SET according to STR specification.
95 qualify_tokens(const char *const str, struct number_set *const set,
96 string_to_uint_func func, const char *const name)
100 memset(set->vec, 0, sizeof(*set->vec) * set->nslots);
104 * Each leading ! character means inversion
105 * of the remaining specification.
110 set->not = !set->not;
114 if (strcmp(s, "none") == 0) {
116 * No numbers are added to the set.
117 * Subsequent is_number_in_set invocations will return set->not.
120 } else if (strcmp(s, "all") == 0) {
122 goto handle_inversion;
126 * Split the string into comma separated tokens.
127 * For each token, find out the corresponding number
128 * by calling FUNC, and add that number to the set.
129 * The absence of tokens or a negative answer
130 * from FUNC is a fatal error.
132 char *copy = xstrdup(s);
133 char *saveptr = NULL;
137 for (token = strtok_r(copy, ",", &saveptr); token;
138 token = strtok_r(NULL, ",", &saveptr)) {
139 number = func(token);
141 error_msg_and_die("invalid %s '%s'", name, token);
144 add_number_to_set(number, set);
150 error_msg_and_die("invalid %s '%s'", name, str);
155 sigstr_to_uint(const char *s)
159 if (*s >= '0' && *s <= '9')
160 return string_to_uint_upto(s, 255);
162 if (strncasecmp(s, "SIG", 3) == 0)
165 for (i = 0; i <= 255; ++i) {
166 const char *name = signame(i);
168 if (strncasecmp(name, "SIG", 3) != 0)
173 if (strcasecmp(name, s) != 0)
183 qualify_syscall_number(const char *s, struct number_set *set)
185 int n = string_to_uint(s);
192 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
193 if ((unsigned) n >= nsyscall_vec[p]) {
196 add_number_to_set(n, &set[p]);
204 regerror_msg_and_die(int errcode, const regex_t *preg,
205 const char *str, const char *pattern)
209 regerror(errcode, preg, buf, sizeof(buf));
210 error_msg_and_die("%s: %s: %s", str, pattern, buf);
214 qualify_syscall_regex(const char *s, struct number_set *set)
219 if ((rc = regcomp(&preg, s, REG_EXTENDED | REG_NOSUB)) != 0)
220 regerror_msg_and_die(rc, &preg, "regcomp", s);
224 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
227 for (i = 0; i < nsyscall_vec[p]; ++i) {
228 if (!sysent_vec[p][i].sys_name)
230 rc = regexec(&preg, sysent_vec[p][i].sys_name,
232 if (rc == REG_NOMATCH)
235 regerror_msg_and_die(rc, &preg, "regexec", s);
236 add_number_to_set(i, &set[p]);
246 lookup_class(const char *s)
248 static const struct {
251 } syscall_class[] = {
252 { "desc", TRACE_DESC },
253 { "file", TRACE_FILE },
254 { "memory", TRACE_MEMORY },
255 { "process", TRACE_PROCESS },
256 { "signal", TRACE_SIGNAL },
257 { "ipc", TRACE_IPC },
258 { "network", TRACE_NETWORK },
259 { "%desc", TRACE_DESC },
260 { "%file", TRACE_FILE },
261 { "%memory", TRACE_MEMORY },
262 { "%process", TRACE_PROCESS },
263 { "%signal", TRACE_SIGNAL },
264 { "%ipc", TRACE_IPC },
265 { "%network", TRACE_NETWORK },
266 { "%stat", TRACE_STAT },
267 { "%lstat", TRACE_LSTAT },
268 { "%fstat", TRACE_FSTAT },
269 { "%%stat", TRACE_STAT_LIKE },
270 { "%statfs", TRACE_STATFS },
271 { "%fstatfs", TRACE_FSTATFS },
272 { "%%statfs", TRACE_STATFS_LIKE },
276 for (i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
277 if (strcmp(s, syscall_class[i].name) == 0) {
278 return syscall_class[i].value;
286 qualify_syscall_class(const char *s, struct number_set *set)
288 const unsigned int n = lookup_class(s);
293 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
296 for (i = 0; i < nsyscall_vec[p]; ++i) {
297 if (!sysent_vec[p][i].sys_name
298 || (sysent_vec[p][i].sys_flags & n) != n) {
301 add_number_to_set(i, &set[p]);
309 qualify_syscall_name(const char *s, struct number_set *set)
314 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
317 for (i = 0; i < nsyscall_vec[p]; ++i) {
318 if (!sysent_vec[p][i].sys_name
319 || strcmp(s, sysent_vec[p][i].sys_name)) {
322 add_number_to_set(i, &set[p]);
331 qualify_syscall(const char *token, struct number_set *set)
333 bool ignore_fail = false;
335 while (*token == '?') {
339 if (*token >= '0' && *token <= '9')
340 return qualify_syscall_number(token, set) || ignore_fail;
342 return qualify_syscall_regex(token + 1, set) || ignore_fail;
343 return qualify_syscall_class(token, set)
344 || qualify_syscall_name(token, set)
349 * Add syscall numbers to SETs for each supported personality
350 * according to STR specification.
353 qualify_syscall_tokens(const char *const str, struct number_set *const set,
354 const char *const name)
356 /* Clear all sets. */
358 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
360 memset(set[p].vec, 0,
361 sizeof(*set[p].vec) * set[p].nslots);
366 * Each leading ! character means inversion
367 * of the remaining specification.
372 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
373 set[p].not = !set[p].not;
378 if (strcmp(s, "none") == 0) {
380 * No syscall numbers are added to sets.
381 * Subsequent is_number_in_set invocations
382 * will return set[p]->not.
385 } else if (strcmp(s, "all") == 0) {
387 goto handle_inversion;
391 * Split the string into comma separated tokens.
392 * For each token, call qualify_syscall that will take care
393 * if adding appropriate syscall numbers to sets.
394 * The absence of tokens or a negative return code
395 * from qualify_syscall is a fatal error.
397 char *copy = xstrdup(s);
398 char *saveptr = NULL;
402 for (token = strtok_r(copy, ",", &saveptr); token;
403 token = strtok_r(NULL, ",", &saveptr)) {
404 done = qualify_syscall(token, set);
406 error_msg_and_die("invalid %s '%s'", name, token);
413 error_msg_and_die("invalid %s '%s'", name, str);
418 find_errno_by_name(const char *name)
422 for (i = 1; i < nerrnos; ++i) {
423 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
431 parse_inject_token(const char *const token, struct inject_opts *const fopts,
432 const bool fault_tokens_only)
437 if ((val = STR_STRIP_PREFIX(token, "when=")) != token) {
445 intval = string_to_uint_ex(val, &end, 0xffff, "+");
449 fopts->first = intval;
455 intval = string_to_uint_upto(val, 0xffff);
458 fopts->step = intval;
467 } else if ((val = STR_STRIP_PREFIX(token, "error=")) != token) {
468 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
470 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
472 intval = find_errno_by_name(val);
475 fopts->rval = -intval;
476 } else if (!fault_tokens_only
477 && (val = STR_STRIP_PREFIX(token, "retval=")) != token) {
478 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
480 intval = string_to_uint(val);
483 fopts->rval = intval;
484 } else if (!fault_tokens_only
485 && (val = STR_STRIP_PREFIX(token, "signal=")) != token) {
486 intval = sigstr_to_uint(val);
487 if (intval < 1 || intval > NSIG_BYTES * 8)
489 fopts->signo = intval;
498 parse_inject_expression(const char *const s, char **buf,
499 struct inject_opts *const fopts,
500 const bool fault_tokens_only)
502 char *saveptr = NULL;
507 for (token = strtok_r(*buf, ":", &saveptr); token;
508 token = strtok_r(NULL, ":", &saveptr)) {
511 else if (!parse_inject_token(token, fopts, fault_tokens_only))
524 qualify_read(const char *const str)
526 qualify_tokens(str, &read_set, string_to_uint, "descriptor");
530 qualify_write(const char *const str)
532 qualify_tokens(str, &write_set, string_to_uint, "descriptor");
536 qualify_signals(const char *const str)
538 qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
542 qualify_trace(const char *const str)
544 qualify_syscall_tokens(str, trace_set, "system call");
548 qualify_abbrev(const char *const str)
550 qualify_syscall_tokens(str, abbrev_set, "system call");
554 qualify_verbose(const char *const str)
556 qualify_syscall_tokens(str, verbose_set, "system call");
560 qualify_raw(const char *const str)
562 qualify_syscall_tokens(str, raw_set, "system call");
566 qualify_inject_common(const char *const str,
567 const bool fault_tokens_only,
568 const char *const description)
570 struct inject_opts opts = {
573 .rval = INJECT_OPTS_RVAL_DEFAULT,
577 char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
579 error_msg_and_die("invalid %s '%s'", description, str);
582 /* If neither of retval, error, or signal is specified, then ... */
583 if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
584 if (fault_tokens_only) {
585 /* in fault= syntax the default error code is ENOSYS. */
588 /* in inject= syntax this is not allowed. */
589 error_msg_and_die("invalid %s '%s'", description, str);
593 struct number_set tmp_set[SUPPORTED_PERSONALITIES];
594 memset(tmp_set, 0, sizeof(tmp_set));
595 qualify_syscall_tokens(name, tmp_set, description);
600 * Initialize inject_vec accourding to tmp_set.
601 * Merge tmp_set into inject_set.
604 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
605 if (!tmp_set[p].nslots && !tmp_set[p].not) {
609 if (!inject_vec[p]) {
610 inject_vec[p] = xcalloc(nsyscall_vec[p],
611 sizeof(*inject_vec[p]));
615 for (i = 0; i < nsyscall_vec[p]; ++i) {
616 if (is_number_in_set(i, &tmp_set[p])) {
617 add_number_to_set(i, &inject_set[p]);
618 inject_vec[p][i] = opts;
622 free(tmp_set[p].vec);
627 qualify_fault(const char *const str)
629 qualify_inject_common(str, true, "fault argument");
633 qualify_inject(const char *const str)
635 qualify_inject_common(str, false, "inject argument");
638 static const struct qual_options {
640 void (*qualify)(const char *);
642 { "trace", qualify_trace },
643 { "t", qualify_trace },
644 { "abbrev", qualify_abbrev },
645 { "a", qualify_abbrev },
646 { "verbose", qualify_verbose },
647 { "v", qualify_verbose },
648 { "raw", qualify_raw },
649 { "x", qualify_raw },
650 { "signal", qualify_signals },
651 { "signals", qualify_signals },
652 { "s", qualify_signals },
653 { "read", qualify_read },
654 { "reads", qualify_read },
655 { "r", qualify_read },
656 { "write", qualify_write },
657 { "writes", qualify_write },
658 { "w", qualify_write },
659 { "fault", qualify_fault },
660 { "inject", qualify_inject },
664 qualify(const char *str)
666 const struct qual_options *opt = qual_options;
669 for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
670 const char *name = qual_options[i].name;
671 const size_t len = strlen(name);
672 const char *val = str_strip_prefix_len(str, name, len);
674 if (val == str || *val != '=')
677 opt = &qual_options[i];
685 qual_flags(const unsigned int scno)
687 return (is_number_in_set(scno, &trace_set[current_personality])
689 | (is_number_in_set(scno, &abbrev_set[current_personality])
691 | (is_number_in_set(scno, &verbose_set[current_personality])
693 | (is_number_in_set(scno, &raw_set[current_personality])
695 | (is_number_in_set(scno, &inject_set[current_personality])