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.
30 #include "number_set.h"
35 qualify_syscall_number(const char *s, struct number_set *set)
37 int n = string_to_uint(s);
43 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
44 if ((unsigned) n >= nsyscall_vec[p])
46 add_number_to_set_array(n, set, p);
54 regerror_msg_and_die(int errcode, const regex_t *preg,
55 const char *str, const char *pattern)
59 regerror(errcode, preg, buf, sizeof(buf));
60 error_msg_and_die("%s: %s: %s", str, pattern, buf);
64 qualify_syscall_regex(const char *s, struct number_set *set)
69 if ((rc = regcomp(&preg, s, REG_EXTENDED | REG_NOSUB)) != 0)
70 regerror_msg_and_die(rc, &preg, "regcomp", s);
74 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
75 for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
76 if (!sysent_vec[p][i].sys_name)
78 rc = regexec(&preg, sysent_vec[p][i].sys_name,
80 if (rc == REG_NOMATCH)
83 regerror_msg_and_die(rc, &preg, "regexec", s);
84 add_number_to_set_array(i, set, p);
94 lookup_class(const char *s)
100 { "%desc", TRACE_DESC },
101 { "%file", TRACE_FILE },
102 { "%memory", TRACE_MEMORY },
103 { "%process", TRACE_PROCESS },
104 { "%signal", TRACE_SIGNAL },
105 { "%ipc", TRACE_IPC },
106 { "%network", TRACE_NETWORK },
107 { "%stat", TRACE_STAT },
108 { "%lstat", TRACE_LSTAT },
109 { "%fstat", TRACE_FSTAT },
110 { "%%stat", TRACE_STAT_LIKE },
111 { "%statfs", TRACE_STATFS },
112 { "%fstatfs", TRACE_FSTATFS },
113 { "%%statfs", TRACE_STATFS_LIKE },
114 { "%pure", TRACE_PURE },
115 /* legacy class names */
116 { "desc", TRACE_DESC },
117 { "file", TRACE_FILE },
118 { "memory", TRACE_MEMORY },
119 { "process", TRACE_PROCESS },
120 { "signal", TRACE_SIGNAL },
121 { "ipc", TRACE_IPC },
122 { "network", TRACE_NETWORK },
125 for (unsigned int i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
126 if (strcmp(s, syscall_class[i].name) == 0)
127 return syscall_class[i].value;
134 qualify_syscall_class(const char *s, struct number_set *set)
136 const unsigned int n = lookup_class(s);
140 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
141 for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
142 if (sysent_vec[p][i].sys_name &&
143 (sysent_vec[p][i].sys_flags & n) == n)
144 add_number_to_set_array(i, set, p);
152 scno_by_name(const char *s, unsigned int p, kernel_long_t start)
154 if (p >= SUPPORTED_PERSONALITIES)
157 for (kernel_ulong_t i = start; i < nsyscall_vec[p]; ++i) {
158 if (sysent_vec[p][i].sys_name &&
159 strcmp(s, sysent_vec[p][i].sys_name) == 0)
167 qualify_syscall_name(const char *s, struct number_set *set)
171 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
172 for (kernel_long_t scno = 0;
173 (scno = scno_by_name(s, p, scno)) >= 0;
175 add_number_to_set_array(scno, set, p);
184 qualify_syscall(const char *token, struct number_set *set)
186 bool ignore_fail = false;
188 while (*token == '?') {
192 if (*token >= '0' && *token <= '9')
193 return qualify_syscall_number(token, set) || ignore_fail;
195 return qualify_syscall_regex(token + 1, set) || ignore_fail;
196 return qualify_syscall_class(token, set)
197 || qualify_syscall_name(token, set)
202 * Add syscall numbers to SETs for each supported personality
203 * according to STR specification.
206 qualify_syscall_tokens(const char *const str, struct number_set *const set)
208 /* Clear all sets. */
209 clear_number_set_array(set, SUPPORTED_PERSONALITIES);
212 * Each leading ! character means inversion
213 * of the remaining specification.
217 invert_number_set_array(set, SUPPORTED_PERSONALITIES);
221 if (strcmp(s, "none") == 0) {
223 * No syscall numbers are added to sets.
224 * Subsequent is_number_in_set* invocations
225 * will return set[p]->not.
228 } else if (strcmp(s, "all") == 0) {
229 /* "all" == "!none" */
230 invert_number_set_array(set, SUPPORTED_PERSONALITIES);
235 * Split the string into comma separated tokens.
236 * For each token, call qualify_syscall that will take care
237 * if adding appropriate syscall numbers to sets.
238 * The absence of tokens or a negative return code
239 * from qualify_syscall is a fatal error.
241 char *copy = xstrdup(s);
242 char *saveptr = NULL;
245 for (const char *token = strtok_r(copy, ",", &saveptr);
246 token; token = strtok_r(NULL, ",", &saveptr)) {
247 done = qualify_syscall(token, set);
249 error_msg_and_die("invalid system call '%s'", token);
255 error_msg_and_die("invalid system call '%s'", str);
259 * Add numbers to SET according to STR specification.
262 qualify_tokens(const char *const str, struct number_set *const set,
263 string_to_uint_func func, const char *const name)
266 clear_number_set_array(set, 1);
269 * Each leading ! character means inversion
270 * of the remaining specification.
274 invert_number_set_array(set, 1);
278 if (strcmp(s, "none") == 0) {
280 * No numbers are added to the set.
281 * Subsequent is_number_in_set* invocations
282 * will return set->not.
285 } else if (strcmp(s, "all") == 0) {
286 /* "all" == "!none" */
287 invert_number_set_array(set, 1);
292 * Split the string into comma separated tokens.
293 * For each token, find out the corresponding number
294 * by calling FUNC, and add that number to the set.
295 * The absence of tokens or a negative answer
296 * from FUNC is a fatal error.
298 char *copy = xstrdup(s);
299 char *saveptr = NULL;
302 for (const char *token = strtok_r(copy, ",", &saveptr);
303 token; token = strtok_r(NULL, ",", &saveptr)) {
304 number = func(token);
306 error_msg_and_die("invalid %s '%s'", name, token);
308 add_number_to_set(number, set);
314 error_msg_and_die("invalid %s '%s'", name, str);