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 /* legacy class names */
115 { "desc", TRACE_DESC },
116 { "file", TRACE_FILE },
117 { "memory", TRACE_MEMORY },
118 { "process", TRACE_PROCESS },
119 { "signal", TRACE_SIGNAL },
120 { "ipc", TRACE_IPC },
121 { "network", TRACE_NETWORK },
124 for (unsigned int i = 0; i < ARRAY_SIZE(syscall_class); ++i) {
125 if (strcmp(s, syscall_class[i].name) == 0)
126 return syscall_class[i].value;
133 qualify_syscall_class(const char *s, struct number_set *set)
135 const unsigned int n = lookup_class(s);
139 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
140 for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
141 if (sysent_vec[p][i].sys_name &&
142 (sysent_vec[p][i].sys_flags & n) == n)
143 add_number_to_set_array(i, set, p);
151 qualify_syscall_name(const char *s, struct number_set *set)
155 for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
156 for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
157 if (sysent_vec[p][i].sys_name &&
158 strcmp(s, sysent_vec[p][i].sys_name) == 0) {
159 add_number_to_set_array(i, set, p);
169 qualify_syscall(const char *token, struct number_set *set)
171 bool ignore_fail = false;
173 while (*token == '?') {
177 if (*token >= '0' && *token <= '9')
178 return qualify_syscall_number(token, set) || ignore_fail;
180 return qualify_syscall_regex(token + 1, set) || ignore_fail;
181 return qualify_syscall_class(token, set)
182 || qualify_syscall_name(token, set)
187 * Add syscall numbers to SETs for each supported personality
188 * according to STR specification.
191 qualify_syscall_tokens(const char *const str, struct number_set *const set)
193 /* Clear all sets. */
194 clear_number_set_array(set, SUPPORTED_PERSONALITIES);
197 * Each leading ! character means inversion
198 * of the remaining specification.
202 invert_number_set_array(set, SUPPORTED_PERSONALITIES);
206 if (strcmp(s, "none") == 0) {
208 * No syscall numbers are added to sets.
209 * Subsequent is_number_in_set* invocations
210 * will return set[p]->not.
213 } else if (strcmp(s, "all") == 0) {
214 /* "all" == "!none" */
215 invert_number_set_array(set, SUPPORTED_PERSONALITIES);
220 * Split the string into comma separated tokens.
221 * For each token, call qualify_syscall that will take care
222 * if adding appropriate syscall numbers to sets.
223 * The absence of tokens or a negative return code
224 * from qualify_syscall is a fatal error.
226 char *copy = xstrdup(s);
227 char *saveptr = NULL;
230 for (const char *token = strtok_r(copy, ",", &saveptr);
231 token; token = strtok_r(NULL, ",", &saveptr)) {
232 done = qualify_syscall(token, set);
234 error_msg_and_die("invalid system call '%s'", token);
240 error_msg_and_die("invalid system call '%s'", str);
244 * Add numbers to SET according to STR specification.
247 qualify_tokens(const char *const str, struct number_set *const set,
248 string_to_uint_func func, const char *const name)
251 clear_number_set_array(set, 1);
254 * Each leading ! character means inversion
255 * of the remaining specification.
259 invert_number_set_array(set, 1);
263 if (strcmp(s, "none") == 0) {
265 * No numbers are added to the set.
266 * Subsequent is_number_in_set* invocations
267 * will return set->not.
270 } else if (strcmp(s, "all") == 0) {
271 /* "all" == "!none" */
272 invert_number_set_array(set, 1);
277 * Split the string into comma separated tokens.
278 * For each token, find out the corresponding number
279 * by calling FUNC, and add that number to the set.
280 * The absence of tokens or a negative answer
281 * from FUNC is a fatal error.
283 char *copy = xstrdup(s);
284 char *saveptr = NULL;
287 for (const char *token = strtok_r(copy, ",", &saveptr);
288 token; token = strtok_r(NULL, ",", &saveptr)) {
289 number = func(token);
291 error_msg_and_die("invalid %s '%s'", name, token);
293 add_number_to_set(number, set);
299 error_msg_and_die("invalid %s '%s'", name, str);