]> granicus.if.org Git - strace/blob - qualify.c
Implement -e trace=%clock option
[strace] / qualify.c
1 /*
2  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "defs.h"
29 #include "nsig.h"
30
31 typedef unsigned int number_slot_t;
32 #define BITS_PER_SLOT (sizeof(number_slot_t) * 8)
33
34 struct number_set {
35         number_slot_t *vec;
36         unsigned int nslots;
37         bool not;
38 };
39
40 struct number_set read_set;
41 struct number_set write_set;
42 struct number_set signal_set;
43
44 static struct number_set abbrev_set[SUPPORTED_PERSONALITIES];
45 static struct number_set inject_set[SUPPORTED_PERSONALITIES];
46 static struct number_set raw_set[SUPPORTED_PERSONALITIES];
47 static struct number_set trace_set[SUPPORTED_PERSONALITIES];
48 static struct number_set verbose_set[SUPPORTED_PERSONALITIES];
49
50 static void
51 number_setbit(const unsigned int i, number_slot_t *const vec)
52 {
53         vec[i / BITS_PER_SLOT] |= (number_slot_t) 1 << (i % BITS_PER_SLOT);
54 }
55
56 static bool
57 number_isset(const unsigned int i, const number_slot_t *const vec)
58 {
59         return vec[i / BITS_PER_SLOT] & ((number_slot_t) 1 << (i % BITS_PER_SLOT));
60 }
61
62 static void
63 reallocate_number_set(struct number_set *const set, const unsigned int new_nslots)
64 {
65         if (new_nslots <= set->nslots)
66                 return;
67         set->vec = xreallocarray(set->vec, new_nslots, sizeof(*set->vec));
68         memset(set->vec + set->nslots, 0,
69                sizeof(*set->vec) * (new_nslots - set->nslots));
70         set->nslots = new_nslots;
71 }
72
73 static void
74 add_number_to_set(const unsigned int number, struct number_set *const set)
75 {
76         reallocate_number_set(set, number / BITS_PER_SLOT + 1);
77         number_setbit(number, set->vec);
78 }
79
80 bool
81 is_number_in_set(const unsigned int number, const struct number_set *const set)
82 {
83         return ((number / BITS_PER_SLOT < set->nslots)
84                 && number_isset(number, set->vec)) ^ set->not;
85 }
86
87 typedef int (*string_to_uint_func)(const char *);
88
89 /*
90  * Add numbers to SET according to STR specification.
91  */
92 static void
93 qualify_tokens(const char *const str, struct number_set *const set,
94                string_to_uint_func func, const char *const name)
95 {
96         /* Clear the set. */
97         if (set->nslots)
98                 memset(set->vec, 0, sizeof(*set->vec) * set->nslots);
99         set->not = false;
100
101         /*
102          * Each leading ! character means inversion
103          * of the remaining specification.
104          */
105         const char *s = str;
106 handle_inversion:
107         while (*s == '!') {
108                 set->not = !set->not;
109                 ++s;
110         }
111
112         if (strcmp(s, "none") == 0) {
113                 /*
114                  * No numbers are added to the set.
115                  * Subsequent is_number_in_set invocations will return set->not.
116                  */
117                 return;
118         } else if (strcmp(s, "all") == 0) {
119                 s = "!none";
120                 goto handle_inversion;
121         }
122
123         /*
124          * Split the string into comma separated tokens.
125          * For each token, find out the corresponding number
126          * by calling FUNC, and add that number to the set.
127          * The absence of tokens or a negative answer
128          * from FUNC is a fatal error.
129          */
130         char *copy = xstrdup(s);
131         char *saveptr = NULL;
132         const char *token;
133         int number = -1;
134
135         for (token = strtok_r(copy, ",", &saveptr); token;
136              token = strtok_r(NULL, ",", &saveptr)) {
137                 number = func(token);
138                 if (number < 0) {
139                         error_msg_and_die("invalid %s '%s'", name, token);
140                 }
141
142                 add_number_to_set(number, set);
143         }
144
145         free(copy);
146
147         if (number < 0) {
148                 error_msg_and_die("invalid %s '%s'", name, str);
149         }
150 }
151
152 static int
153 sigstr_to_uint(const char *s)
154 {
155         int i;
156
157         if (*s >= '0' && *s <= '9')
158                 return string_to_uint_upto(s, 255);
159
160         if (strncasecmp(s, "SIG", 3) == 0)
161                 s += 3;
162
163         for (i = 0; i <= 255; ++i) {
164                 const char *name = signame(i);
165
166                 if (strncasecmp(name, "SIG", 3) != 0)
167                         continue;
168
169                 name += 3;
170
171                 if (strcasecmp(name, s) != 0)
172                         continue;
173
174                 return i;
175         }
176
177         return -1;
178 }
179
180 static bool
181 qualify_syscall_number(const char *s, struct number_set *set)
182 {
183         int n = string_to_uint(s);
184         if (n < 0)
185                 return false;
186
187         unsigned int p;
188         bool done = false;
189
190         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
191                 if ((unsigned) n >= nsyscall_vec[p]) {
192                         continue;
193                 }
194                 add_number_to_set(n, &set[p]);
195                 done = true;
196         }
197
198         return done;
199 }
200
201 static unsigned int
202 lookup_class(const char *s)
203 {
204         static const struct {
205                 const char *name;
206                 unsigned int value;
207         } syscall_class[] = {
208                 { "desc",       TRACE_DESC      },
209                 { "file",       TRACE_FILE      },
210                 { "memory",     TRACE_MEMORY    },
211                 { "process",    TRACE_PROCESS   },
212                 { "signal",     TRACE_SIGNAL    },
213                 { "ipc",        TRACE_IPC       },
214                 { "network",    TRACE_NETWORK   },
215                 { "%desc",      TRACE_DESC      },
216                 { "%file",      TRACE_FILE      },
217                 { "%memory",    TRACE_MEMORY    },
218                 { "%process",   TRACE_PROCESS   },
219                 { "%signal",    TRACE_SIGNAL    },
220                 { "%ipc",       TRACE_IPC       },
221                 { "%network",   TRACE_NETWORK   },
222                 { "%sched",     TRACE_SCHED     },
223                 { "%clock",     TRACE_CLOCK     },
224         };
225
226         unsigned int i;
227         for (i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
228                 if (strcmp(s, syscall_class[i].name) == 0) {
229                         return syscall_class[i].value;
230                 }
231         }
232
233         return 0;
234 }
235
236 static bool
237 qualify_syscall_class(const char *s, struct number_set *set)
238 {
239         const unsigned int n = lookup_class(s);
240         if (!n)
241                 return false;
242
243         unsigned int p;
244         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
245                 unsigned int i;
246
247                 for (i = 0; i < nsyscall_vec[p]; ++i) {
248                         if (!sysent_vec[p][i].sys_name
249                             || (sysent_vec[p][i].sys_flags & n) != n) {
250                                 continue;
251                         }
252                         add_number_to_set(i, &set[p]);
253                 }
254         }
255
256         return true;
257 }
258
259 static bool
260 qualify_syscall_name(const char *s, struct number_set *set)
261 {
262         unsigned int p;
263         bool found = false;
264
265         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
266                 unsigned int i;
267
268                 for (i = 0; i < nsyscall_vec[p]; ++i) {
269                         if (!sysent_vec[p][i].sys_name
270                             || strcmp(s, sysent_vec[p][i].sys_name)) {
271                                 continue;
272                         }
273                         add_number_to_set(i, &set[p]);
274                         found = true;
275                 }
276         }
277
278         return found;
279 }
280
281 static bool
282 qualify_syscall(const char *token, struct number_set *set)
283 {
284         if (*token >= '0' && *token <= '9')
285                 return qualify_syscall_number(token, set);
286         return qualify_syscall_class(token, set)
287                || qualify_syscall_name(token, set);
288 }
289
290 /*
291  * Add syscall numbers to SETs for each supported personality
292  * according to STR specification.
293  */
294 static void
295 qualify_syscall_tokens(const char *const str, struct number_set *const set,
296                        const char *const name)
297 {
298         /* Clear all sets. */
299         unsigned int p;
300         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
301                 if (set[p].nslots)
302                         memset(set[p].vec, 0,
303                                sizeof(*set[p].vec) * set[p].nslots);
304                 set[p].not = false;
305         }
306
307         /*
308          * Each leading ! character means inversion
309          * of the remaining specification.
310          */
311         const char *s = str;
312 handle_inversion:
313         while (*s == '!') {
314                 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
315                         set[p].not = !set[p].not;
316                 }
317                 ++s;
318         }
319
320         if (strcmp(s, "none") == 0) {
321                 /*
322                  * No syscall numbers are added to sets.
323                  * Subsequent is_number_in_set invocations
324                  * will return set[p]->not.
325                  */
326                 return;
327         } else if (strcmp(s, "all") == 0) {
328                 s = "!none";
329                 goto handle_inversion;
330         }
331
332         /*
333          * Split the string into comma separated tokens.
334          * For each token, call qualify_syscall that will take care
335          * if adding appropriate syscall numbers to sets.
336          * The absence of tokens or a negative return code
337          * from qualify_syscall is a fatal error.
338          */
339         char *copy = xstrdup(s);
340         char *saveptr = NULL;
341         const char *token;
342         bool done = false;
343
344         for (token = strtok_r(copy, ",", &saveptr); token;
345              token = strtok_r(NULL, ",", &saveptr)) {
346                 done = qualify_syscall(token, set);
347                 if (!done) {
348                         error_msg_and_die("invalid %s '%s'", name, token);
349                 }
350         }
351
352         free(copy);
353
354         if (!done) {
355                 error_msg_and_die("invalid %s '%s'", name, str);
356         }
357 }
358
359 /*
360  * Returns NULL if STR does not start with PREFIX,
361  * or a pointer to the first char in STR after PREFIX.
362  */
363 static const char *
364 strip_prefix(const char *prefix, const char *str)
365 {
366         size_t len = strlen(prefix);
367
368         return strncmp(prefix, str, len) ? NULL : str + len;
369 }
370
371 static int
372 find_errno_by_name(const char *name)
373 {
374         unsigned int i;
375
376         for (i = 1; i < nerrnos; ++i) {
377                 if (errnoent[i] && (strcasecmp(name, errnoent[i]) == 0))
378                         return i;
379         }
380
381         return -1;
382 }
383
384 static bool
385 parse_inject_token(const char *const token, struct inject_opts *const fopts,
386                    const bool fault_tokens_only)
387 {
388         const char *val;
389         int intval;
390
391         if ((val = strip_prefix("when=", token))) {
392                 /*
393                  *      == 1+1
394                  * F    == F+0
395                  * F+   == F+1
396                  * F+S
397                  */
398                 char *end;
399                 intval = string_to_uint_ex(val, &end, 0xffff, "+");
400                 if (intval < 1)
401                         return false;
402
403                 fopts->first = intval;
404
405                 if (*end) {
406                         val = end + 1;
407                         if (*val) {
408                                 /* F+S */
409                                 intval = string_to_uint_upto(val, 0xffff);
410                                 if (intval < 1)
411                                         return false;
412                                 fopts->step = intval;
413                         } else {
414                                 /* F+ == F+1 */
415                                 fopts->step = 1;
416                         }
417                 } else {
418                         /* F == F+0 */
419                         fopts->step = 0;
420                 }
421         } else if ((val = strip_prefix("error=", token))) {
422                 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
423                         return false;
424                 intval = string_to_uint_upto(val, MAX_ERRNO_VALUE);
425                 if (intval < 0)
426                         intval = find_errno_by_name(val);
427                 if (intval < 1)
428                         return false;
429                 fopts->rval = -intval;
430         } else if (!fault_tokens_only && (val = strip_prefix("retval=", token))) {
431                 if (fopts->rval != INJECT_OPTS_RVAL_DEFAULT)
432                         return false;
433                 intval = string_to_uint(val);
434                 if (intval < 0)
435                         return false;
436                 fopts->rval = intval;
437         } else if (!fault_tokens_only && (val = strip_prefix("signal=", token))) {
438                 intval = sigstr_to_uint(val);
439                 if (intval < 1 || intval > NSIG_BYTES * 8)
440                         return false;
441                 fopts->signo = intval;
442         } else {
443                 return false;
444         }
445
446         return true;
447 }
448
449 static char *
450 parse_inject_expression(const char *const s, char **buf,
451                         struct inject_opts *const fopts,
452                         const bool fault_tokens_only)
453 {
454         char *saveptr = NULL;
455         char *name = NULL;
456         char *token;
457
458         *buf = xstrdup(s);
459         for (token = strtok_r(*buf, ":", &saveptr); token;
460              token = strtok_r(NULL, ":", &saveptr)) {
461                 if (!name)
462                         name = token;
463                 else if (!parse_inject_token(token, fopts, fault_tokens_only))
464                         goto parse_error;
465         }
466
467         if (name)
468                 return name;
469
470 parse_error:
471         free(*buf);
472         return *buf = NULL;
473 }
474
475 static void
476 qualify_read(const char *const str)
477 {
478         qualify_tokens(str, &read_set, string_to_uint, "descriptor");
479 }
480
481 static void
482 qualify_write(const char *const str)
483 {
484         qualify_tokens(str, &write_set, string_to_uint, "descriptor");
485 }
486
487 static void
488 qualify_signals(const char *const str)
489 {
490         qualify_tokens(str, &signal_set, sigstr_to_uint, "signal");
491 }
492
493 static void
494 qualify_trace(const char *const str)
495 {
496         qualify_syscall_tokens(str, trace_set, "system call");
497 }
498
499 static void
500 qualify_abbrev(const char *const str)
501 {
502         qualify_syscall_tokens(str, abbrev_set, "system call");
503 }
504
505 static void
506 qualify_verbose(const char *const str)
507 {
508         qualify_syscall_tokens(str, verbose_set, "system call");
509 }
510
511 static void
512 qualify_raw(const char *const str)
513 {
514         qualify_syscall_tokens(str, raw_set, "system call");
515 }
516
517 static void
518 qualify_inject_common(const char *const str,
519                       const bool fault_tokens_only,
520                       const char *const description)
521 {
522         struct inject_opts opts = {
523                 .first = 1,
524                 .step = 1,
525                 .rval = INJECT_OPTS_RVAL_DEFAULT,
526                 .signo = 0
527         };
528         char *buf = NULL;
529         char *name = parse_inject_expression(str, &buf, &opts, fault_tokens_only);
530         if (!name) {
531                 error_msg_and_die("invalid %s '%s'", description, str);
532         }
533
534         /* If neither of retval, error, or signal is specified, then ... */
535         if (opts.rval == INJECT_OPTS_RVAL_DEFAULT && !opts.signo) {
536                 if (fault_tokens_only) {
537                         /* in fault= syntax the default error code is ENOSYS. */
538                         opts.rval = -ENOSYS;
539                 } else {
540                         /* in inject= syntax this is not allowed. */
541                         error_msg_and_die("invalid %s '%s'", description, str);
542                 }
543         }
544
545         struct number_set tmp_set[SUPPORTED_PERSONALITIES];
546         memset(tmp_set, 0, sizeof(tmp_set));
547         qualify_syscall_tokens(name, tmp_set, description);
548
549         free(buf);
550
551         /*
552          * Initialize inject_vec accourding to tmp_set.
553          * Merge tmp_set into inject_set.
554          */
555         unsigned int p;
556         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
557                 if (!tmp_set[p].nslots && !tmp_set[p].not) {
558                         continue;
559                 }
560
561                 if (!inject_vec[p]) {
562                         inject_vec[p] = xcalloc(nsyscall_vec[p],
563                                                sizeof(*inject_vec[p]));
564                 }
565
566                 unsigned int i;
567                 for (i = 0; i < nsyscall_vec[p]; ++i) {
568                         if (is_number_in_set(i, &tmp_set[p])) {
569                                 add_number_to_set(i, &inject_set[p]);
570                                 inject_vec[p][i] = opts;
571                         }
572                 }
573
574                 free(tmp_set[p].vec);
575         }
576 }
577
578 static void
579 qualify_fault(const char *const str)
580 {
581         qualify_inject_common(str, true, "fault argument");
582 }
583
584 static void
585 qualify_inject(const char *const str)
586 {
587         qualify_inject_common(str, false, "inject argument");
588 }
589
590 static const struct qual_options {
591         const char *name;
592         void (*qualify)(const char *);
593 } qual_options[] = {
594         { "trace",      qualify_trace   },
595         { "t",          qualify_trace   },
596         { "abbrev",     qualify_abbrev  },
597         { "a",          qualify_abbrev  },
598         { "verbose",    qualify_verbose },
599         { "v",          qualify_verbose },
600         { "raw",        qualify_raw     },
601         { "x",          qualify_raw     },
602         { "signal",     qualify_signals },
603         { "signals",    qualify_signals },
604         { "s",          qualify_signals },
605         { "read",       qualify_read    },
606         { "reads",      qualify_read    },
607         { "r",          qualify_read    },
608         { "write",      qualify_write   },
609         { "writes",     qualify_write   },
610         { "w",          qualify_write   },
611         { "fault",      qualify_fault   },
612         { "inject",     qualify_inject  },
613 };
614
615 void
616 qualify(const char *str)
617 {
618         const struct qual_options *opt = qual_options;
619         unsigned int i;
620
621         for (i = 0; i < ARRAY_SIZE(qual_options); ++i) {
622                 const char *p = qual_options[i].name;
623                 unsigned int len = strlen(p);
624
625                 if (strncmp(str, p, len) || str[len] != '=')
626                         continue;
627
628                 opt = &qual_options[i];
629                 str += len + 1;
630                 break;
631         }
632
633         opt->qualify(str);
634 }
635
636 unsigned int
637 qual_flags(const unsigned int scno)
638 {
639         return  (is_number_in_set(scno, &trace_set[current_personality])
640                    ? QUAL_TRACE : 0)
641                 | (is_number_in_set(scno, &abbrev_set[current_personality])
642                    ? QUAL_ABBREV : 0)
643                 | (is_number_in_set(scno, &verbose_set[current_personality])
644                    ? QUAL_VERBOSE : 0)
645                 | (is_number_in_set(scno, &raw_set[current_personality])
646                    ? QUAL_RAW : 0)
647                 | (is_number_in_set(scno, &inject_set[current_personality])
648                    ? QUAL_INJECT : 0);
649 }