Commit | Line | Data |
---|---|---|
7ce7e424 TZ |
1 | /* |
2 | * trace_events_filter - generic event filtering | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | |
19 | */ | |
20 | ||
7ce7e424 TZ |
21 | #include <linux/module.h> |
22 | #include <linux/ctype.h> | |
ac1adc55 | 23 | #include <linux/mutex.h> |
6fb2915d | 24 | #include <linux/perf_event.h> |
7ce7e424 TZ |
25 | |
26 | #include "trace.h" | |
4bda2d51 | 27 | #include "trace_output.h" |
7ce7e424 | 28 | |
8b372562 | 29 | enum filter_op_ids |
7ce7e424 | 30 | { |
8b372562 TZ |
31 | OP_OR, |
32 | OP_AND, | |
b0f1a59a | 33 | OP_GLOB, |
8b372562 TZ |
34 | OP_NE, |
35 | OP_EQ, | |
36 | OP_LT, | |
37 | OP_LE, | |
38 | OP_GT, | |
39 | OP_GE, | |
40 | OP_NONE, | |
41 | OP_OPEN_PAREN, | |
42 | }; | |
43 | ||
44 | struct filter_op { | |
45 | int id; | |
46 | char *string; | |
47 | int precedence; | |
48 | }; | |
49 | ||
50 | static struct filter_op filter_ops[] = { | |
b0f1a59a LZ |
51 | { OP_OR, "||", 1 }, |
52 | { OP_AND, "&&", 2 }, | |
53 | { OP_GLOB, "~", 4 }, | |
54 | { OP_NE, "!=", 4 }, | |
55 | { OP_EQ, "==", 4 }, | |
56 | { OP_LT, "<", 5 }, | |
57 | { OP_LE, "<=", 5 }, | |
58 | { OP_GT, ">", 5 }, | |
59 | { OP_GE, ">=", 5 }, | |
60 | { OP_NONE, "OP_NONE", 0 }, | |
61 | { OP_OPEN_PAREN, "(", 0 }, | |
8b372562 TZ |
62 | }; |
63 | ||
64 | enum { | |
65 | FILT_ERR_NONE, | |
66 | FILT_ERR_INVALID_OP, | |
67 | FILT_ERR_UNBALANCED_PAREN, | |
68 | FILT_ERR_TOO_MANY_OPERANDS, | |
69 | FILT_ERR_OPERAND_TOO_LONG, | |
70 | FILT_ERR_FIELD_NOT_FOUND, | |
71 | FILT_ERR_ILLEGAL_FIELD_OP, | |
72 | FILT_ERR_ILLEGAL_INTVAL, | |
73 | FILT_ERR_BAD_SUBSYS_FILTER, | |
74 | FILT_ERR_TOO_MANY_PREDS, | |
75 | FILT_ERR_MISSING_FIELD, | |
76 | FILT_ERR_INVALID_FILTER, | |
77 | }; | |
78 | ||
79 | static char *err_text[] = { | |
80 | "No error", | |
81 | "Invalid operator", | |
82 | "Unbalanced parens", | |
83 | "Too many operands", | |
84 | "Operand too long", | |
85 | "Field not found", | |
86 | "Illegal operation for field type", | |
87 | "Illegal integer value", | |
88 | "Couldn't find or set field in one of a subsystem's events", | |
89 | "Too many terms in predicate expression", | |
90 | "Missing field name and/or value", | |
91 | "Meaningless filter expression", | |
92 | }; | |
93 | ||
94 | struct opstack_op { | |
95 | int op; | |
96 | struct list_head list; | |
97 | }; | |
98 | ||
99 | struct postfix_elt { | |
100 | int op; | |
101 | char *operand; | |
102 | struct list_head list; | |
103 | }; | |
104 | ||
105 | struct filter_parse_state { | |
106 | struct filter_op *ops; | |
107 | struct list_head opstack; | |
108 | struct list_head postfix; | |
109 | int lasterr; | |
110 | int lasterr_pos; | |
111 | ||
112 | struct { | |
113 | char *string; | |
114 | unsigned int cnt; | |
115 | unsigned int tail; | |
116 | } infix; | |
117 | ||
118 | struct { | |
119 | char string[MAX_FILTER_STR_VAL]; | |
120 | int pos; | |
121 | unsigned int tail; | |
122 | } operand; | |
123 | }; | |
124 | ||
197e2eab LZ |
125 | #define DEFINE_COMPARISON_PRED(type) \ |
126 | static int filter_pred_##type(struct filter_pred *pred, void *event, \ | |
127 | int val1, int val2) \ | |
128 | { \ | |
129 | type *addr = (type *)(event + pred->offset); \ | |
130 | type val = (type)pred->val; \ | |
131 | int match = 0; \ | |
132 | \ | |
133 | switch (pred->op) { \ | |
134 | case OP_LT: \ | |
135 | match = (*addr < val); \ | |
136 | break; \ | |
137 | case OP_LE: \ | |
138 | match = (*addr <= val); \ | |
139 | break; \ | |
140 | case OP_GT: \ | |
141 | match = (*addr > val); \ | |
142 | break; \ | |
143 | case OP_GE: \ | |
144 | match = (*addr >= val); \ | |
145 | break; \ | |
146 | default: \ | |
147 | break; \ | |
148 | } \ | |
149 | \ | |
150 | return match; \ | |
151 | } | |
152 | ||
153 | #define DEFINE_EQUALITY_PRED(size) \ | |
154 | static int filter_pred_##size(struct filter_pred *pred, void *event, \ | |
155 | int val1, int val2) \ | |
156 | { \ | |
157 | u##size *addr = (u##size *)(event + pred->offset); \ | |
158 | u##size val = (u##size)pred->val; \ | |
159 | int match; \ | |
160 | \ | |
161 | match = (val == *addr) ^ pred->not; \ | |
162 | \ | |
163 | return match; \ | |
164 | } | |
165 | ||
8b372562 TZ |
166 | DEFINE_COMPARISON_PRED(s64); |
167 | DEFINE_COMPARISON_PRED(u64); | |
168 | DEFINE_COMPARISON_PRED(s32); | |
169 | DEFINE_COMPARISON_PRED(u32); | |
170 | DEFINE_COMPARISON_PRED(s16); | |
171 | DEFINE_COMPARISON_PRED(u16); | |
172 | DEFINE_COMPARISON_PRED(s8); | |
173 | DEFINE_COMPARISON_PRED(u8); | |
174 | ||
175 | DEFINE_EQUALITY_PRED(64); | |
176 | DEFINE_EQUALITY_PRED(32); | |
177 | DEFINE_EQUALITY_PRED(16); | |
178 | DEFINE_EQUALITY_PRED(8); | |
179 | ||
180 | static int filter_pred_and(struct filter_pred *pred __attribute((unused)), | |
181 | void *event __attribute((unused)), | |
182 | int val1, int val2) | |
7ce7e424 | 183 | { |
8b372562 | 184 | return val1 && val2; |
7ce7e424 TZ |
185 | } |
186 | ||
8b372562 TZ |
187 | static int filter_pred_or(struct filter_pred *pred __attribute((unused)), |
188 | void *event __attribute((unused)), | |
189 | int val1, int val2) | |
7ce7e424 | 190 | { |
8b372562 | 191 | return val1 || val2; |
7ce7e424 TZ |
192 | } |
193 | ||
e8808c10 | 194 | /* Filter predicate for fixed sized arrays of characters */ |
8b372562 TZ |
195 | static int filter_pred_string(struct filter_pred *pred, void *event, |
196 | int val1, int val2) | |
7ce7e424 TZ |
197 | { |
198 | char *addr = (char *)(event + pred->offset); | |
199 | int cmp, match; | |
200 | ||
1889d209 | 201 | cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len); |
7ce7e424 | 202 | |
1889d209 | 203 | match = cmp ^ pred->not; |
7ce7e424 TZ |
204 | |
205 | return match; | |
206 | } | |
207 | ||
87a342f5 LZ |
208 | /* Filter predicate for char * pointers */ |
209 | static int filter_pred_pchar(struct filter_pred *pred, void *event, | |
210 | int val1, int val2) | |
211 | { | |
212 | char **addr = (char **)(event + pred->offset); | |
213 | int cmp, match; | |
16da27a8 | 214 | int len = strlen(*addr) + 1; /* including tailing '\0' */ |
87a342f5 | 215 | |
16da27a8 | 216 | cmp = pred->regex.match(*addr, &pred->regex, len); |
87a342f5 | 217 | |
1889d209 | 218 | match = cmp ^ pred->not; |
87a342f5 LZ |
219 | |
220 | return match; | |
221 | } | |
222 | ||
e8808c10 FW |
223 | /* |
224 | * Filter predicate for dynamic sized arrays of characters. | |
225 | * These are implemented through a list of strings at the end | |
226 | * of the entry. | |
227 | * Also each of these strings have a field in the entry which | |
228 | * contains its offset from the beginning of the entry. | |
229 | * We have then first to get this field, dereference it | |
230 | * and add it to the address of the entry, and at last we have | |
231 | * the address of the string. | |
232 | */ | |
233 | static int filter_pred_strloc(struct filter_pred *pred, void *event, | |
234 | int val1, int val2) | |
235 | { | |
7d536cb3 LZ |
236 | u32 str_item = *(u32 *)(event + pred->offset); |
237 | int str_loc = str_item & 0xffff; | |
238 | int str_len = str_item >> 16; | |
e8808c10 FW |
239 | char *addr = (char *)(event + str_loc); |
240 | int cmp, match; | |
241 | ||
1889d209 | 242 | cmp = pred->regex.match(addr, &pred->regex, str_len); |
e8808c10 | 243 | |
1889d209 | 244 | match = cmp ^ pred->not; |
e8808c10 FW |
245 | |
246 | return match; | |
247 | } | |
248 | ||
8b372562 TZ |
249 | static int filter_pred_none(struct filter_pred *pred, void *event, |
250 | int val1, int val2) | |
0a19e53c TZ |
251 | { |
252 | return 0; | |
253 | } | |
254 | ||
d1303dd1 LZ |
255 | /* |
256 | * regex_match_foo - Basic regex callbacks | |
257 | * | |
258 | * @str: the string to be searched | |
259 | * @r: the regex structure containing the pattern string | |
260 | * @len: the length of the string to be searched (including '\0') | |
261 | * | |
262 | * Note: | |
263 | * - @str might not be NULL-terminated if it's of type DYN_STRING | |
264 | * or STATIC_STRING | |
265 | */ | |
266 | ||
1889d209 FW |
267 | static int regex_match_full(char *str, struct regex *r, int len) |
268 | { | |
269 | if (strncmp(str, r->pattern, len) == 0) | |
270 | return 1; | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static int regex_match_front(char *str, struct regex *r, int len) | |
275 | { | |
285caad4 | 276 | if (strncmp(str, r->pattern, r->len) == 0) |
1889d209 FW |
277 | return 1; |
278 | return 0; | |
279 | } | |
280 | ||
281 | static int regex_match_middle(char *str, struct regex *r, int len) | |
282 | { | |
b2af211f | 283 | if (strnstr(str, r->pattern, len)) |
1889d209 FW |
284 | return 1; |
285 | return 0; | |
286 | } | |
287 | ||
288 | static int regex_match_end(char *str, struct regex *r, int len) | |
289 | { | |
a3291c14 | 290 | int strlen = len - 1; |
1889d209 | 291 | |
a3291c14 LZ |
292 | if (strlen >= r->len && |
293 | memcmp(str + strlen - r->len, r->pattern, r->len) == 0) | |
1889d209 FW |
294 | return 1; |
295 | return 0; | |
296 | } | |
297 | ||
3f6fe06d FW |
298 | /** |
299 | * filter_parse_regex - parse a basic regex | |
300 | * @buff: the raw regex | |
301 | * @len: length of the regex | |
302 | * @search: will point to the beginning of the string to compare | |
303 | * @not: tell whether the match will have to be inverted | |
304 | * | |
305 | * This passes in a buffer containing a regex and this function will | |
1889d209 FW |
306 | * set search to point to the search part of the buffer and |
307 | * return the type of search it is (see enum above). | |
308 | * This does modify buff. | |
309 | * | |
310 | * Returns enum type. | |
311 | * search returns the pointer to use for comparison. | |
312 | * not returns 1 if buff started with a '!' | |
313 | * 0 otherwise. | |
314 | */ | |
3f6fe06d | 315 | enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) |
1889d209 FW |
316 | { |
317 | int type = MATCH_FULL; | |
318 | int i; | |
319 | ||
320 | if (buff[0] == '!') { | |
321 | *not = 1; | |
322 | buff++; | |
323 | len--; | |
324 | } else | |
325 | *not = 0; | |
326 | ||
327 | *search = buff; | |
328 | ||
329 | for (i = 0; i < len; i++) { | |
330 | if (buff[i] == '*') { | |
331 | if (!i) { | |
332 | *search = buff + 1; | |
333 | type = MATCH_END_ONLY; | |
334 | } else { | |
335 | if (type == MATCH_END_ONLY) | |
336 | type = MATCH_MIDDLE_ONLY; | |
337 | else | |
338 | type = MATCH_FRONT_ONLY; | |
339 | buff[i] = 0; | |
340 | break; | |
341 | } | |
342 | } | |
343 | } | |
344 | ||
345 | return type; | |
346 | } | |
347 | ||
b0f1a59a | 348 | static void filter_build_regex(struct filter_pred *pred) |
1889d209 FW |
349 | { |
350 | struct regex *r = &pred->regex; | |
b0f1a59a LZ |
351 | char *search; |
352 | enum regex_type type = MATCH_FULL; | |
353 | int not = 0; | |
354 | ||
355 | if (pred->op == OP_GLOB) { | |
356 | type = filter_parse_regex(r->pattern, r->len, &search, ¬); | |
357 | r->len = strlen(search); | |
358 | memmove(r->pattern, search, r->len+1); | |
359 | } | |
1889d209 FW |
360 | |
361 | switch (type) { | |
362 | case MATCH_FULL: | |
363 | r->match = regex_match_full; | |
364 | break; | |
365 | case MATCH_FRONT_ONLY: | |
366 | r->match = regex_match_front; | |
367 | break; | |
368 | case MATCH_MIDDLE_ONLY: | |
369 | r->match = regex_match_middle; | |
370 | break; | |
371 | case MATCH_END_ONLY: | |
372 | r->match = regex_match_end; | |
373 | break; | |
374 | } | |
375 | ||
376 | pred->not ^= not; | |
1889d209 FW |
377 | } |
378 | ||
7ce7e424 | 379 | /* return 1 if event matches, 0 otherwise (discard) */ |
6fb2915d | 380 | int filter_match_preds(struct event_filter *filter, void *rec) |
7ce7e424 | 381 | { |
8b372562 TZ |
382 | int match, top = 0, val1 = 0, val2 = 0; |
383 | int stack[MAX_FILTER_PRED]; | |
7ce7e424 | 384 | struct filter_pred *pred; |
8b372562 | 385 | int i; |
7ce7e424 | 386 | |
30e673b2 TZ |
387 | for (i = 0; i < filter->n_preds; i++) { |
388 | pred = filter->preds[i]; | |
8b372562 TZ |
389 | if (!pred->pop_n) { |
390 | match = pred->fn(pred, rec, val1, val2); | |
391 | stack[top++] = match; | |
0a19e53c | 392 | continue; |
8b372562 TZ |
393 | } |
394 | if (pred->pop_n > top) { | |
395 | WARN_ON_ONCE(1); | |
396 | return 0; | |
397 | } | |
398 | val1 = stack[--top]; | |
399 | val2 = stack[--top]; | |
400 | match = pred->fn(pred, rec, val1, val2); | |
401 | stack[top++] = match; | |
7ce7e424 TZ |
402 | } |
403 | ||
8b372562 | 404 | return stack[--top]; |
7ce7e424 | 405 | } |
17c873ec | 406 | EXPORT_SYMBOL_GPL(filter_match_preds); |
7ce7e424 | 407 | |
8b372562 | 408 | static void parse_error(struct filter_parse_state *ps, int err, int pos) |
7ce7e424 | 409 | { |
8b372562 TZ |
410 | ps->lasterr = err; |
411 | ps->lasterr_pos = pos; | |
412 | } | |
7ce7e424 | 413 | |
8b372562 TZ |
414 | static void remove_filter_string(struct event_filter *filter) |
415 | { | |
416 | kfree(filter->filter_string); | |
417 | filter->filter_string = NULL; | |
418 | } | |
419 | ||
420 | static int replace_filter_string(struct event_filter *filter, | |
421 | char *filter_string) | |
422 | { | |
423 | kfree(filter->filter_string); | |
424 | filter->filter_string = kstrdup(filter_string, GFP_KERNEL); | |
425 | if (!filter->filter_string) | |
426 | return -ENOMEM; | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | static int append_filter_string(struct event_filter *filter, | |
432 | char *string) | |
433 | { | |
434 | int newlen; | |
435 | char *new_filter_string; | |
436 | ||
437 | BUG_ON(!filter->filter_string); | |
438 | newlen = strlen(filter->filter_string) + strlen(string) + 1; | |
439 | new_filter_string = kmalloc(newlen, GFP_KERNEL); | |
440 | if (!new_filter_string) | |
441 | return -ENOMEM; | |
442 | ||
443 | strcpy(new_filter_string, filter->filter_string); | |
444 | strcat(new_filter_string, string); | |
445 | kfree(filter->filter_string); | |
446 | filter->filter_string = new_filter_string; | |
447 | ||
448 | return 0; | |
449 | } | |
450 | ||
451 | static void append_filter_err(struct filter_parse_state *ps, | |
452 | struct event_filter *filter) | |
453 | { | |
454 | int pos = ps->lasterr_pos; | |
455 | char *buf, *pbuf; | |
456 | ||
457 | buf = (char *)__get_free_page(GFP_TEMPORARY); | |
458 | if (!buf) | |
4bda2d51 | 459 | return; |
7ce7e424 | 460 | |
8b372562 TZ |
461 | append_filter_string(filter, "\n"); |
462 | memset(buf, ' ', PAGE_SIZE); | |
463 | if (pos > PAGE_SIZE - 128) | |
464 | pos = 0; | |
465 | buf[pos] = '^'; | |
466 | pbuf = &buf[pos] + 1; | |
467 | ||
468 | sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]); | |
469 | append_filter_string(filter, buf); | |
470 | free_page((unsigned long) buf); | |
7ce7e424 TZ |
471 | } |
472 | ||
8b372562 | 473 | void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s) |
ac1adc55 | 474 | { |
8b372562 TZ |
475 | struct event_filter *filter = call->filter; |
476 | ||
00e95830 | 477 | mutex_lock(&event_mutex); |
8e254c1d | 478 | if (filter && filter->filter_string) |
8b372562 TZ |
479 | trace_seq_printf(s, "%s\n", filter->filter_string); |
480 | else | |
481 | trace_seq_printf(s, "none\n"); | |
00e95830 | 482 | mutex_unlock(&event_mutex); |
ac1adc55 TZ |
483 | } |
484 | ||
8b372562 | 485 | void print_subsystem_event_filter(struct event_subsystem *system, |
ac1adc55 TZ |
486 | struct trace_seq *s) |
487 | { | |
8b372562 TZ |
488 | struct event_filter *filter = system->filter; |
489 | ||
00e95830 | 490 | mutex_lock(&event_mutex); |
8e254c1d | 491 | if (filter && filter->filter_string) |
8b372562 TZ |
492 | trace_seq_printf(s, "%s\n", filter->filter_string); |
493 | else | |
494 | trace_seq_printf(s, "none\n"); | |
00e95830 | 495 | mutex_unlock(&event_mutex); |
ac1adc55 TZ |
496 | } |
497 | ||
7ce7e424 TZ |
498 | static struct ftrace_event_field * |
499 | find_event_field(struct ftrace_event_call *call, char *name) | |
500 | { | |
1fc2d5c1 | 501 | struct ftrace_event_field *field; |
7ce7e424 | 502 | |
1fc2d5c1 | 503 | list_for_each_entry(field, &call->fields, link) { |
7ce7e424 TZ |
504 | if (!strcmp(field->name, name)) |
505 | return field; | |
506 | } | |
507 | ||
508 | return NULL; | |
509 | } | |
510 | ||
8b372562 | 511 | static void filter_free_pred(struct filter_pred *pred) |
7ce7e424 TZ |
512 | { |
513 | if (!pred) | |
514 | return; | |
515 | ||
516 | kfree(pred->field_name); | |
7ce7e424 TZ |
517 | kfree(pred); |
518 | } | |
519 | ||
0a19e53c TZ |
520 | static void filter_clear_pred(struct filter_pred *pred) |
521 | { | |
522 | kfree(pred->field_name); | |
523 | pred->field_name = NULL; | |
1889d209 | 524 | pred->regex.len = 0; |
0a19e53c TZ |
525 | } |
526 | ||
527 | static int filter_set_pred(struct filter_pred *dest, | |
528 | struct filter_pred *src, | |
529 | filter_pred_fn_t fn) | |
530 | { | |
531 | *dest = *src; | |
8b372562 TZ |
532 | if (src->field_name) { |
533 | dest->field_name = kstrdup(src->field_name, GFP_KERNEL); | |
534 | if (!dest->field_name) | |
535 | return -ENOMEM; | |
536 | } | |
0a19e53c TZ |
537 | dest->fn = fn; |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
8b372562 | 542 | static void filter_disable_preds(struct ftrace_event_call *call) |
7ce7e424 | 543 | { |
30e673b2 | 544 | struct event_filter *filter = call->filter; |
7ce7e424 TZ |
545 | int i; |
546 | ||
30e673b2 TZ |
547 | call->filter_active = 0; |
548 | filter->n_preds = 0; | |
0a19e53c TZ |
549 | |
550 | for (i = 0; i < MAX_FILTER_PRED; i++) | |
30e673b2 | 551 | filter->preds[i]->fn = filter_pred_none; |
0a19e53c TZ |
552 | } |
553 | ||
6fb2915d | 554 | static void __free_preds(struct event_filter *filter) |
2df75e41 | 555 | { |
2df75e41 LZ |
556 | int i; |
557 | ||
8e254c1d LZ |
558 | if (!filter) |
559 | return; | |
560 | ||
2df75e41 LZ |
561 | for (i = 0; i < MAX_FILTER_PRED; i++) { |
562 | if (filter->preds[i]) | |
563 | filter_free_pred(filter->preds[i]); | |
564 | } | |
565 | kfree(filter->preds); | |
57be8887 | 566 | kfree(filter->filter_string); |
2df75e41 | 567 | kfree(filter); |
6fb2915d LZ |
568 | } |
569 | ||
570 | void destroy_preds(struct ftrace_event_call *call) | |
571 | { | |
572 | __free_preds(call->filter); | |
2df75e41 | 573 | call->filter = NULL; |
6fb2915d | 574 | call->filter_active = 0; |
2df75e41 LZ |
575 | } |
576 | ||
6fb2915d | 577 | static struct event_filter *__alloc_preds(void) |
0a19e53c | 578 | { |
30e673b2 | 579 | struct event_filter *filter; |
0a19e53c TZ |
580 | struct filter_pred *pred; |
581 | int i; | |
582 | ||
6fb2915d LZ |
583 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); |
584 | if (!filter) | |
585 | return ERR_PTR(-ENOMEM); | |
0a19e53c | 586 | |
30e673b2 TZ |
587 | filter->n_preds = 0; |
588 | ||
589 | filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL); | |
590 | if (!filter->preds) | |
591 | goto oom; | |
592 | ||
0a19e53c TZ |
593 | for (i = 0; i < MAX_FILTER_PRED; i++) { |
594 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); | |
595 | if (!pred) | |
596 | goto oom; | |
597 | pred->fn = filter_pred_none; | |
30e673b2 | 598 | filter->preds[i] = pred; |
0a19e53c TZ |
599 | } |
600 | ||
6fb2915d | 601 | return filter; |
0a19e53c TZ |
602 | |
603 | oom: | |
6fb2915d LZ |
604 | __free_preds(filter); |
605 | return ERR_PTR(-ENOMEM); | |
606 | } | |
607 | ||
608 | static int init_preds(struct ftrace_event_call *call) | |
609 | { | |
610 | if (call->filter) | |
611 | return 0; | |
612 | ||
613 | call->filter_active = 0; | |
614 | call->filter = __alloc_preds(); | |
615 | if (IS_ERR(call->filter)) | |
616 | return PTR_ERR(call->filter); | |
0a19e53c | 617 | |
6fb2915d | 618 | return 0; |
7ce7e424 | 619 | } |
8e254c1d LZ |
620 | |
621 | static int init_subsystem_preds(struct event_subsystem *system) | |
622 | { | |
623 | struct ftrace_event_call *call; | |
624 | int err; | |
625 | ||
626 | list_for_each_entry(call, &ftrace_events, list) { | |
627 | if (!call->define_fields) | |
628 | continue; | |
629 | ||
630 | if (strcmp(call->system, system->name) != 0) | |
631 | continue; | |
632 | ||
c58b4321 LZ |
633 | err = init_preds(call); |
634 | if (err) | |
635 | return err; | |
8e254c1d LZ |
636 | } |
637 | ||
638 | return 0; | |
639 | } | |
7ce7e424 | 640 | |
fce29d15 | 641 | static void filter_free_subsystem_preds(struct event_subsystem *system) |
cfb180f3 | 642 | { |
a59fd602 | 643 | struct ftrace_event_call *call; |
cfb180f3 | 644 | |
a59fd602 | 645 | list_for_each_entry(call, &ftrace_events, list) { |
e1112b4d | 646 | if (!call->define_fields) |
cfb180f3 TZ |
647 | continue; |
648 | ||
8e254c1d LZ |
649 | if (strcmp(call->system, system->name) != 0) |
650 | continue; | |
651 | ||
8e254c1d LZ |
652 | filter_disable_preds(call); |
653 | remove_filter_string(call->filter); | |
cfb180f3 TZ |
654 | } |
655 | } | |
656 | ||
8b372562 TZ |
657 | static int filter_add_pred_fn(struct filter_parse_state *ps, |
658 | struct ftrace_event_call *call, | |
6fb2915d | 659 | struct event_filter *filter, |
ac1adc55 TZ |
660 | struct filter_pred *pred, |
661 | filter_pred_fn_t fn) | |
7ce7e424 | 662 | { |
0a19e53c | 663 | int idx, err; |
7ce7e424 | 664 | |
8b372562 TZ |
665 | if (filter->n_preds == MAX_FILTER_PRED) { |
666 | parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); | |
0a19e53c | 667 | return -ENOSPC; |
8b372562 | 668 | } |
7ce7e424 | 669 | |
30e673b2 TZ |
670 | idx = filter->n_preds; |
671 | filter_clear_pred(filter->preds[idx]); | |
672 | err = filter_set_pred(filter->preds[idx], pred, fn); | |
0a19e53c TZ |
673 | if (err) |
674 | return err; | |
675 | ||
30e673b2 | 676 | filter->n_preds++; |
7ce7e424 | 677 | |
0a19e53c | 678 | return 0; |
7ce7e424 TZ |
679 | } |
680 | ||
aa38e9fc | 681 | int filter_assign_type(const char *type) |
7ce7e424 | 682 | { |
7fcb7c47 LZ |
683 | if (strstr(type, "__data_loc") && strstr(type, "char")) |
684 | return FILTER_DYN_STRING; | |
685 | ||
7ce7e424 | 686 | if (strchr(type, '[') && strstr(type, "char")) |
e8808c10 FW |
687 | return FILTER_STATIC_STRING; |
688 | ||
aa38e9fc LZ |
689 | return FILTER_OTHER; |
690 | } | |
691 | ||
692 | static bool is_string_field(struct ftrace_event_field *field) | |
693 | { | |
694 | return field->filter_type == FILTER_DYN_STRING || | |
87a342f5 LZ |
695 | field->filter_type == FILTER_STATIC_STRING || |
696 | field->filter_type == FILTER_PTR_STRING; | |
7ce7e424 TZ |
697 | } |
698 | ||
8b372562 TZ |
699 | static int is_legal_op(struct ftrace_event_field *field, int op) |
700 | { | |
b0f1a59a LZ |
701 | if (is_string_field(field) && |
702 | (op != OP_EQ && op != OP_NE && op != OP_GLOB)) | |
703 | return 0; | |
704 | if (!is_string_field(field) && op == OP_GLOB) | |
8b372562 TZ |
705 | return 0; |
706 | ||
707 | return 1; | |
708 | } | |
709 | ||
710 | static filter_pred_fn_t select_comparison_fn(int op, int field_size, | |
711 | int field_is_signed) | |
712 | { | |
713 | filter_pred_fn_t fn = NULL; | |
714 | ||
715 | switch (field_size) { | |
716 | case 8: | |
717 | if (op == OP_EQ || op == OP_NE) | |
718 | fn = filter_pred_64; | |
719 | else if (field_is_signed) | |
720 | fn = filter_pred_s64; | |
721 | else | |
722 | fn = filter_pred_u64; | |
723 | break; | |
724 | case 4: | |
725 | if (op == OP_EQ || op == OP_NE) | |
726 | fn = filter_pred_32; | |
727 | else if (field_is_signed) | |
728 | fn = filter_pred_s32; | |
729 | else | |
730 | fn = filter_pred_u32; | |
731 | break; | |
732 | case 2: | |
733 | if (op == OP_EQ || op == OP_NE) | |
734 | fn = filter_pred_16; | |
735 | else if (field_is_signed) | |
736 | fn = filter_pred_s16; | |
737 | else | |
738 | fn = filter_pred_u16; | |
739 | break; | |
740 | case 1: | |
741 | if (op == OP_EQ || op == OP_NE) | |
742 | fn = filter_pred_8; | |
743 | else if (field_is_signed) | |
744 | fn = filter_pred_s8; | |
745 | else | |
746 | fn = filter_pred_u8; | |
747 | break; | |
748 | } | |
749 | ||
750 | return fn; | |
751 | } | |
752 | ||
753 | static int filter_add_pred(struct filter_parse_state *ps, | |
754 | struct ftrace_event_call *call, | |
6fb2915d | 755 | struct event_filter *filter, |
1f9963cb LZ |
756 | struct filter_pred *pred, |
757 | bool dry_run) | |
7ce7e424 TZ |
758 | { |
759 | struct ftrace_event_field *field; | |
0a19e53c | 760 | filter_pred_fn_t fn; |
f66578a7 | 761 | unsigned long long val; |
5e4904cb | 762 | int ret; |
7ce7e424 | 763 | |
8b372562 TZ |
764 | pred->fn = filter_pred_none; |
765 | ||
766 | if (pred->op == OP_AND) { | |
767 | pred->pop_n = 2; | |
1f9963cb LZ |
768 | fn = filter_pred_and; |
769 | goto add_pred_fn; | |
8b372562 TZ |
770 | } else if (pred->op == OP_OR) { |
771 | pred->pop_n = 2; | |
1f9963cb LZ |
772 | fn = filter_pred_or; |
773 | goto add_pred_fn; | |
8b372562 TZ |
774 | } |
775 | ||
7ce7e424 | 776 | field = find_event_field(call, pred->field_name); |
8b372562 TZ |
777 | if (!field) { |
778 | parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0); | |
7ce7e424 | 779 | return -EINVAL; |
8b372562 | 780 | } |
7ce7e424 TZ |
781 | |
782 | pred->offset = field->offset; | |
783 | ||
8b372562 TZ |
784 | if (!is_legal_op(field, pred->op)) { |
785 | parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0); | |
786 | return -EINVAL; | |
787 | } | |
788 | ||
aa38e9fc | 789 | if (is_string_field(field)) { |
b0f1a59a | 790 | filter_build_regex(pred); |
87a342f5 | 791 | |
1889d209 | 792 | if (field->filter_type == FILTER_STATIC_STRING) { |
e8808c10 | 793 | fn = filter_pred_string; |
1889d209 FW |
794 | pred->regex.field_len = field->size; |
795 | } else if (field->filter_type == FILTER_DYN_STRING) | |
b0f1a59a | 796 | fn = filter_pred_strloc; |
16da27a8 | 797 | else |
87a342f5 | 798 | fn = filter_pred_pchar; |
9f58a159 | 799 | } else { |
5e4904cb | 800 | if (field->is_signed) |
1889d209 | 801 | ret = strict_strtoll(pred->regex.pattern, 0, &val); |
5e4904cb | 802 | else |
1889d209 | 803 | ret = strict_strtoull(pred->regex.pattern, 0, &val); |
5e4904cb | 804 | if (ret) { |
8b372562 | 805 | parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0); |
9f58a159 | 806 | return -EINVAL; |
8b372562 | 807 | } |
f66578a7 | 808 | pred->val = val; |
7ce7e424 | 809 | |
1f9963cb LZ |
810 | fn = select_comparison_fn(pred->op, field->size, |
811 | field->is_signed); | |
812 | if (!fn) { | |
813 | parse_error(ps, FILT_ERR_INVALID_OP, 0); | |
814 | return -EINVAL; | |
815 | } | |
7ce7e424 TZ |
816 | } |
817 | ||
8b372562 TZ |
818 | if (pred->op == OP_NE) |
819 | pred->not = 1; | |
ac1adc55 | 820 | |
1f9963cb LZ |
821 | add_pred_fn: |
822 | if (!dry_run) | |
6fb2915d | 823 | return filter_add_pred_fn(ps, call, filter, pred, fn); |
1f9963cb | 824 | return 0; |
cfb180f3 TZ |
825 | } |
826 | ||
8b372562 TZ |
827 | static void parse_init(struct filter_parse_state *ps, |
828 | struct filter_op *ops, | |
829 | char *infix_string) | |
830 | { | |
831 | memset(ps, '\0', sizeof(*ps)); | |
832 | ||
833 | ps->infix.string = infix_string; | |
834 | ps->infix.cnt = strlen(infix_string); | |
835 | ps->ops = ops; | |
836 | ||
837 | INIT_LIST_HEAD(&ps->opstack); | |
838 | INIT_LIST_HEAD(&ps->postfix); | |
839 | } | |
840 | ||
841 | static char infix_next(struct filter_parse_state *ps) | |
842 | { | |
843 | ps->infix.cnt--; | |
844 | ||
845 | return ps->infix.string[ps->infix.tail++]; | |
846 | } | |
847 | ||
848 | static char infix_peek(struct filter_parse_state *ps) | |
849 | { | |
850 | if (ps->infix.tail == strlen(ps->infix.string)) | |
851 | return 0; | |
852 | ||
853 | return ps->infix.string[ps->infix.tail]; | |
854 | } | |
855 | ||
856 | static void infix_advance(struct filter_parse_state *ps) | |
857 | { | |
858 | ps->infix.cnt--; | |
859 | ps->infix.tail++; | |
860 | } | |
861 | ||
862 | static inline int is_precedence_lower(struct filter_parse_state *ps, | |
863 | int a, int b) | |
864 | { | |
865 | return ps->ops[a].precedence < ps->ops[b].precedence; | |
866 | } | |
867 | ||
868 | static inline int is_op_char(struct filter_parse_state *ps, char c) | |
869 | { | |
870 | int i; | |
871 | ||
872 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { | |
873 | if (ps->ops[i].string[0] == c) | |
874 | return 1; | |
875 | } | |
c4cff064 | 876 | |
0a19e53c | 877 | return 0; |
cfb180f3 TZ |
878 | } |
879 | ||
8b372562 TZ |
880 | static int infix_get_op(struct filter_parse_state *ps, char firstc) |
881 | { | |
882 | char nextc = infix_peek(ps); | |
883 | char opstr[3]; | |
884 | int i; | |
885 | ||
886 | opstr[0] = firstc; | |
887 | opstr[1] = nextc; | |
888 | opstr[2] = '\0'; | |
889 | ||
890 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { | |
891 | if (!strcmp(opstr, ps->ops[i].string)) { | |
892 | infix_advance(ps); | |
893 | return ps->ops[i].id; | |
7ce7e424 | 894 | } |
8b372562 TZ |
895 | } |
896 | ||
897 | opstr[1] = '\0'; | |
898 | ||
899 | for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) { | |
900 | if (!strcmp(opstr, ps->ops[i].string)) | |
901 | return ps->ops[i].id; | |
902 | } | |
903 | ||
904 | return OP_NONE; | |
905 | } | |
906 | ||
907 | static inline void clear_operand_string(struct filter_parse_state *ps) | |
908 | { | |
909 | memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL); | |
910 | ps->operand.tail = 0; | |
911 | } | |
912 | ||
913 | static inline int append_operand_char(struct filter_parse_state *ps, char c) | |
914 | { | |
5872144f | 915 | if (ps->operand.tail == MAX_FILTER_STR_VAL - 1) |
8b372562 TZ |
916 | return -EINVAL; |
917 | ||
918 | ps->operand.string[ps->operand.tail++] = c; | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
923 | static int filter_opstack_push(struct filter_parse_state *ps, int op) | |
924 | { | |
925 | struct opstack_op *opstack_op; | |
926 | ||
927 | opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL); | |
928 | if (!opstack_op) | |
929 | return -ENOMEM; | |
930 | ||
931 | opstack_op->op = op; | |
932 | list_add(&opstack_op->list, &ps->opstack); | |
933 | ||
934 | return 0; | |
935 | } | |
936 | ||
937 | static int filter_opstack_empty(struct filter_parse_state *ps) | |
938 | { | |
939 | return list_empty(&ps->opstack); | |
940 | } | |
941 | ||
942 | static int filter_opstack_top(struct filter_parse_state *ps) | |
943 | { | |
944 | struct opstack_op *opstack_op; | |
945 | ||
946 | if (filter_opstack_empty(ps)) | |
947 | return OP_NONE; | |
948 | ||
949 | opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list); | |
950 | ||
951 | return opstack_op->op; | |
952 | } | |
953 | ||
954 | static int filter_opstack_pop(struct filter_parse_state *ps) | |
955 | { | |
956 | struct opstack_op *opstack_op; | |
957 | int op; | |
958 | ||
959 | if (filter_opstack_empty(ps)) | |
960 | return OP_NONE; | |
961 | ||
962 | opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list); | |
963 | op = opstack_op->op; | |
964 | list_del(&opstack_op->list); | |
965 | ||
966 | kfree(opstack_op); | |
967 | ||
968 | return op; | |
969 | } | |
970 | ||
971 | static void filter_opstack_clear(struct filter_parse_state *ps) | |
972 | { | |
973 | while (!filter_opstack_empty(ps)) | |
974 | filter_opstack_pop(ps); | |
975 | } | |
976 | ||
977 | static char *curr_operand(struct filter_parse_state *ps) | |
978 | { | |
979 | return ps->operand.string; | |
980 | } | |
981 | ||
982 | static int postfix_append_operand(struct filter_parse_state *ps, char *operand) | |
983 | { | |
984 | struct postfix_elt *elt; | |
985 | ||
986 | elt = kmalloc(sizeof(*elt), GFP_KERNEL); | |
987 | if (!elt) | |
988 | return -ENOMEM; | |
989 | ||
990 | elt->op = OP_NONE; | |
991 | elt->operand = kstrdup(operand, GFP_KERNEL); | |
992 | if (!elt->operand) { | |
993 | kfree(elt); | |
994 | return -ENOMEM; | |
995 | } | |
996 | ||
997 | list_add_tail(&elt->list, &ps->postfix); | |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | ||
1002 | static int postfix_append_op(struct filter_parse_state *ps, int op) | |
1003 | { | |
1004 | struct postfix_elt *elt; | |
1005 | ||
1006 | elt = kmalloc(sizeof(*elt), GFP_KERNEL); | |
1007 | if (!elt) | |
1008 | return -ENOMEM; | |
1009 | ||
1010 | elt->op = op; | |
1011 | elt->operand = NULL; | |
1012 | ||
1013 | list_add_tail(&elt->list, &ps->postfix); | |
1014 | ||
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | static void postfix_clear(struct filter_parse_state *ps) | |
1019 | { | |
1020 | struct postfix_elt *elt; | |
1021 | ||
1022 | while (!list_empty(&ps->postfix)) { | |
1023 | elt = list_first_entry(&ps->postfix, struct postfix_elt, list); | |
8b372562 | 1024 | list_del(&elt->list); |
8ad80731 LZ |
1025 | kfree(elt->operand); |
1026 | kfree(elt); | |
8b372562 TZ |
1027 | } |
1028 | } | |
1029 | ||
1030 | static int filter_parse(struct filter_parse_state *ps) | |
1031 | { | |
5928c3cc | 1032 | int in_string = 0; |
8b372562 TZ |
1033 | int op, top_op; |
1034 | char ch; | |
1035 | ||
1036 | while ((ch = infix_next(ps))) { | |
5928c3cc FW |
1037 | if (ch == '"') { |
1038 | in_string ^= 1; | |
1039 | continue; | |
1040 | } | |
1041 | ||
1042 | if (in_string) | |
1043 | goto parse_operand; | |
1044 | ||
8b372562 TZ |
1045 | if (isspace(ch)) |
1046 | continue; | |
1047 | ||
1048 | if (is_op_char(ps, ch)) { | |
1049 | op = infix_get_op(ps, ch); | |
1050 | if (op == OP_NONE) { | |
1051 | parse_error(ps, FILT_ERR_INVALID_OP, 0); | |
7ce7e424 TZ |
1052 | return -EINVAL; |
1053 | } | |
8b372562 TZ |
1054 | |
1055 | if (strlen(curr_operand(ps))) { | |
1056 | postfix_append_operand(ps, curr_operand(ps)); | |
1057 | clear_operand_string(ps); | |
1058 | } | |
1059 | ||
1060 | while (!filter_opstack_empty(ps)) { | |
1061 | top_op = filter_opstack_top(ps); | |
1062 | if (!is_precedence_lower(ps, top_op, op)) { | |
1063 | top_op = filter_opstack_pop(ps); | |
1064 | postfix_append_op(ps, top_op); | |
1065 | continue; | |
1066 | } | |
1067 | break; | |
1068 | } | |
1069 | ||
1070 | filter_opstack_push(ps, op); | |
7ce7e424 TZ |
1071 | continue; |
1072 | } | |
8b372562 TZ |
1073 | |
1074 | if (ch == '(') { | |
1075 | filter_opstack_push(ps, OP_OPEN_PAREN); | |
1076 | continue; | |
1077 | } | |
1078 | ||
1079 | if (ch == ')') { | |
1080 | if (strlen(curr_operand(ps))) { | |
1081 | postfix_append_operand(ps, curr_operand(ps)); | |
1082 | clear_operand_string(ps); | |
1083 | } | |
1084 | ||
1085 | top_op = filter_opstack_pop(ps); | |
1086 | while (top_op != OP_NONE) { | |
1087 | if (top_op == OP_OPEN_PAREN) | |
1088 | break; | |
1089 | postfix_append_op(ps, top_op); | |
1090 | top_op = filter_opstack_pop(ps); | |
1091 | } | |
1092 | if (top_op == OP_NONE) { | |
1093 | parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0); | |
1094 | return -EINVAL; | |
7ce7e424 | 1095 | } |
7ce7e424 TZ |
1096 | continue; |
1097 | } | |
5928c3cc | 1098 | parse_operand: |
8b372562 TZ |
1099 | if (append_operand_char(ps, ch)) { |
1100 | parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0); | |
1101 | return -EINVAL; | |
1102 | } | |
1103 | } | |
1104 | ||
1105 | if (strlen(curr_operand(ps))) | |
1106 | postfix_append_operand(ps, curr_operand(ps)); | |
1107 | ||
1108 | while (!filter_opstack_empty(ps)) { | |
1109 | top_op = filter_opstack_pop(ps); | |
1110 | if (top_op == OP_NONE) | |
1111 | break; | |
1112 | if (top_op == OP_OPEN_PAREN) { | |
1113 | parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0); | |
1114 | return -EINVAL; | |
1115 | } | |
1116 | postfix_append_op(ps, top_op); | |
1117 | } | |
1118 | ||
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | static struct filter_pred *create_pred(int op, char *operand1, char *operand2) | |
1123 | { | |
1124 | struct filter_pred *pred; | |
1125 | ||
1126 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); | |
1127 | if (!pred) | |
1128 | return NULL; | |
1129 | ||
1130 | pred->field_name = kstrdup(operand1, GFP_KERNEL); | |
1131 | if (!pred->field_name) { | |
1132 | kfree(pred); | |
1133 | return NULL; | |
1134 | } | |
1135 | ||
1889d209 FW |
1136 | strcpy(pred->regex.pattern, operand2); |
1137 | pred->regex.len = strlen(pred->regex.pattern); | |
8b372562 TZ |
1138 | |
1139 | pred->op = op; | |
1140 | ||
1141 | return pred; | |
1142 | } | |
1143 | ||
1144 | static struct filter_pred *create_logical_pred(int op) | |
1145 | { | |
1146 | struct filter_pred *pred; | |
1147 | ||
1148 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); | |
1149 | if (!pred) | |
1150 | return NULL; | |
1151 | ||
1152 | pred->op = op; | |
1153 | ||
1154 | return pred; | |
1155 | } | |
1156 | ||
1157 | static int check_preds(struct filter_parse_state *ps) | |
1158 | { | |
1159 | int n_normal_preds = 0, n_logical_preds = 0; | |
1160 | struct postfix_elt *elt; | |
1161 | ||
1162 | list_for_each_entry(elt, &ps->postfix, list) { | |
1163 | if (elt->op == OP_NONE) | |
1164 | continue; | |
1165 | ||
1166 | if (elt->op == OP_AND || elt->op == OP_OR) { | |
1167 | n_logical_preds++; | |
1168 | continue; | |
7ce7e424 | 1169 | } |
8b372562 | 1170 | n_normal_preds++; |
7ce7e424 TZ |
1171 | } |
1172 | ||
8b372562 TZ |
1173 | if (!n_normal_preds || n_logical_preds >= n_normal_preds) { |
1174 | parse_error(ps, FILT_ERR_INVALID_FILTER, 0); | |
bcabd91c LZ |
1175 | return -EINVAL; |
1176 | } | |
1177 | ||
8b372562 TZ |
1178 | return 0; |
1179 | } | |
f66578a7 | 1180 | |
fce29d15 | 1181 | static int replace_preds(struct ftrace_event_call *call, |
6fb2915d | 1182 | struct event_filter *filter, |
8b372562 | 1183 | struct filter_parse_state *ps, |
1f9963cb LZ |
1184 | char *filter_string, |
1185 | bool dry_run) | |
8b372562 TZ |
1186 | { |
1187 | char *operand1 = NULL, *operand2 = NULL; | |
1188 | struct filter_pred *pred; | |
1189 | struct postfix_elt *elt; | |
1190 | int err; | |
1f9963cb | 1191 | int n_preds = 0; |
8b372562 TZ |
1192 | |
1193 | err = check_preds(ps); | |
1194 | if (err) | |
1195 | return err; | |
1196 | ||
1197 | list_for_each_entry(elt, &ps->postfix, list) { | |
1198 | if (elt->op == OP_NONE) { | |
1199 | if (!operand1) | |
1200 | operand1 = elt->operand; | |
1201 | else if (!operand2) | |
1202 | operand2 = elt->operand; | |
1203 | else { | |
1204 | parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0); | |
1205 | return -EINVAL; | |
1206 | } | |
1207 | continue; | |
1208 | } | |
1209 | ||
1f9963cb LZ |
1210 | if (n_preds++ == MAX_FILTER_PRED) { |
1211 | parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0); | |
1212 | return -ENOSPC; | |
1213 | } | |
1214 | ||
8b372562 TZ |
1215 | if (elt->op == OP_AND || elt->op == OP_OR) { |
1216 | pred = create_logical_pred(elt->op); | |
1f9963cb | 1217 | goto add_pred; |
8b372562 TZ |
1218 | } |
1219 | ||
1220 | if (!operand1 || !operand2) { | |
1221 | parse_error(ps, FILT_ERR_MISSING_FIELD, 0); | |
1222 | return -EINVAL; | |
1223 | } | |
1224 | ||
1225 | pred = create_pred(elt->op, operand1, operand2); | |
1f9963cb | 1226 | add_pred: |
fb82ad71 TZ |
1227 | if (!pred) |
1228 | return -ENOMEM; | |
6fb2915d | 1229 | err = filter_add_pred(ps, call, filter, pred, dry_run); |
c5cb1836 | 1230 | filter_free_pred(pred); |
8b372562 TZ |
1231 | if (err) |
1232 | return err; | |
1233 | ||
1234 | operand1 = operand2 = NULL; | |
1235 | } | |
7ce7e424 | 1236 | |
7ce7e424 TZ |
1237 | return 0; |
1238 | } | |
1239 | ||
fce29d15 LZ |
1240 | static int replace_system_preds(struct event_subsystem *system, |
1241 | struct filter_parse_state *ps, | |
1242 | char *filter_string) | |
1243 | { | |
1244 | struct ftrace_event_call *call; | |
fce29d15 | 1245 | bool fail = true; |
a66abe7f | 1246 | int err; |
fce29d15 LZ |
1247 | |
1248 | list_for_each_entry(call, &ftrace_events, list) { | |
3ed67776 | 1249 | struct event_filter *filter = call->filter; |
fce29d15 LZ |
1250 | |
1251 | if (!call->define_fields) | |
1252 | continue; | |
1253 | ||
1254 | if (strcmp(call->system, system->name) != 0) | |
1255 | continue; | |
1256 | ||
1257 | /* try to see if the filter can be applied */ | |
6fb2915d | 1258 | err = replace_preds(call, filter, ps, filter_string, true); |
fce29d15 LZ |
1259 | if (err) |
1260 | continue; | |
1261 | ||
1262 | /* really apply the filter */ | |
1263 | filter_disable_preds(call); | |
6fb2915d | 1264 | err = replace_preds(call, filter, ps, filter_string, false); |
fce29d15 LZ |
1265 | if (err) |
1266 | filter_disable_preds(call); | |
6fb2915d LZ |
1267 | else { |
1268 | call->filter_active = 1; | |
1269 | replace_filter_string(filter, filter_string); | |
1270 | } | |
fce29d15 LZ |
1271 | fail = false; |
1272 | } | |
1273 | ||
1274 | if (fail) { | |
1275 | parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0); | |
a66abe7f | 1276 | return -EINVAL; |
fce29d15 LZ |
1277 | } |
1278 | return 0; | |
1279 | } | |
1280 | ||
8b372562 TZ |
1281 | int apply_event_filter(struct ftrace_event_call *call, char *filter_string) |
1282 | { | |
1283 | int err; | |
8b372562 TZ |
1284 | struct filter_parse_state *ps; |
1285 | ||
00e95830 | 1286 | mutex_lock(&event_mutex); |
8b372562 | 1287 | |
8e254c1d LZ |
1288 | err = init_preds(call); |
1289 | if (err) | |
1290 | goto out_unlock; | |
1291 | ||
8b372562 TZ |
1292 | if (!strcmp(strstrip(filter_string), "0")) { |
1293 | filter_disable_preds(call); | |
1294 | remove_filter_string(call->filter); | |
a66abe7f | 1295 | goto out_unlock; |
8b372562 TZ |
1296 | } |
1297 | ||
8cd995b6 | 1298 | err = -ENOMEM; |
8b372562 TZ |
1299 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); |
1300 | if (!ps) | |
8cd995b6 | 1301 | goto out_unlock; |
8b372562 TZ |
1302 | |
1303 | filter_disable_preds(call); | |
1304 | replace_filter_string(call->filter, filter_string); | |
1305 | ||
1306 | parse_init(ps, filter_ops, filter_string); | |
1307 | err = filter_parse(ps); | |
1308 | if (err) { | |
1309 | append_filter_err(ps, call->filter); | |
1310 | goto out; | |
1311 | } | |
1312 | ||
6fb2915d | 1313 | err = replace_preds(call, call->filter, ps, filter_string, false); |
8b372562 TZ |
1314 | if (err) |
1315 | append_filter_err(ps, call->filter); | |
6fb2915d LZ |
1316 | else |
1317 | call->filter_active = 1; | |
8b372562 TZ |
1318 | out: |
1319 | filter_opstack_clear(ps); | |
1320 | postfix_clear(ps); | |
1321 | kfree(ps); | |
8cd995b6 | 1322 | out_unlock: |
00e95830 | 1323 | mutex_unlock(&event_mutex); |
8b372562 TZ |
1324 | |
1325 | return err; | |
1326 | } | |
1327 | ||
1328 | int apply_subsystem_event_filter(struct event_subsystem *system, | |
1329 | char *filter_string) | |
1330 | { | |
1331 | int err; | |
8b372562 TZ |
1332 | struct filter_parse_state *ps; |
1333 | ||
00e95830 | 1334 | mutex_lock(&event_mutex); |
8b372562 | 1335 | |
8e254c1d LZ |
1336 | err = init_subsystem_preds(system); |
1337 | if (err) | |
1338 | goto out_unlock; | |
1339 | ||
8b372562 | 1340 | if (!strcmp(strstrip(filter_string), "0")) { |
fce29d15 | 1341 | filter_free_subsystem_preds(system); |
8b372562 | 1342 | remove_filter_string(system->filter); |
a66abe7f | 1343 | goto out_unlock; |
8b372562 TZ |
1344 | } |
1345 | ||
8cd995b6 | 1346 | err = -ENOMEM; |
8b372562 TZ |
1347 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); |
1348 | if (!ps) | |
8cd995b6 | 1349 | goto out_unlock; |
8b372562 | 1350 | |
8b372562 TZ |
1351 | replace_filter_string(system->filter, filter_string); |
1352 | ||
1353 | parse_init(ps, filter_ops, filter_string); | |
1354 | err = filter_parse(ps); | |
1355 | if (err) { | |
1356 | append_filter_err(ps, system->filter); | |
1357 | goto out; | |
1358 | } | |
1359 | ||
fce29d15 LZ |
1360 | err = replace_system_preds(system, ps, filter_string); |
1361 | if (err) | |
8b372562 TZ |
1362 | append_filter_err(ps, system->filter); |
1363 | ||
1364 | out: | |
1365 | filter_opstack_clear(ps); | |
1366 | postfix_clear(ps); | |
1367 | kfree(ps); | |
8cd995b6 | 1368 | out_unlock: |
00e95830 | 1369 | mutex_unlock(&event_mutex); |
8b372562 TZ |
1370 | |
1371 | return err; | |
1372 | } | |
7ce7e424 | 1373 | |
6fb2915d LZ |
1374 | #ifdef CONFIG_EVENT_PROFILE |
1375 | ||
1376 | void ftrace_profile_free_filter(struct perf_event *event) | |
1377 | { | |
1378 | struct event_filter *filter = event->filter; | |
1379 | ||
1380 | event->filter = NULL; | |
1381 | __free_preds(filter); | |
1382 | } | |
1383 | ||
1384 | int ftrace_profile_set_filter(struct perf_event *event, int event_id, | |
1385 | char *filter_str) | |
1386 | { | |
1387 | int err; | |
1388 | struct event_filter *filter; | |
1389 | struct filter_parse_state *ps; | |
1390 | struct ftrace_event_call *call = NULL; | |
1391 | ||
1392 | mutex_lock(&event_mutex); | |
1393 | ||
1394 | list_for_each_entry(call, &ftrace_events, list) { | |
1395 | if (call->id == event_id) | |
1396 | break; | |
1397 | } | |
a66abe7f IM |
1398 | |
1399 | err = -EINVAL; | |
6fb2915d | 1400 | if (!call) |
a66abe7f | 1401 | goto out_unlock; |
6fb2915d | 1402 | |
a66abe7f | 1403 | err = -EEXIST; |
6fb2915d | 1404 | if (event->filter) |
a66abe7f | 1405 | goto out_unlock; |
6fb2915d LZ |
1406 | |
1407 | filter = __alloc_preds(); | |
a66abe7f IM |
1408 | if (IS_ERR(filter)) { |
1409 | err = PTR_ERR(filter); | |
1410 | goto out_unlock; | |
1411 | } | |
6fb2915d LZ |
1412 | |
1413 | err = -ENOMEM; | |
1414 | ps = kzalloc(sizeof(*ps), GFP_KERNEL); | |
1415 | if (!ps) | |
1416 | goto free_preds; | |
1417 | ||
1418 | parse_init(ps, filter_ops, filter_str); | |
1419 | err = filter_parse(ps); | |
1420 | if (err) | |
1421 | goto free_ps; | |
1422 | ||
1423 | err = replace_preds(call, filter, ps, filter_str, false); | |
1424 | if (!err) | |
1425 | event->filter = filter; | |
1426 | ||
1427 | free_ps: | |
1428 | filter_opstack_clear(ps); | |
1429 | postfix_clear(ps); | |
1430 | kfree(ps); | |
1431 | ||
1432 | free_preds: | |
1433 | if (err) | |
1434 | __free_preds(filter); | |
1435 | ||
a66abe7f | 1436 | out_unlock: |
6fb2915d LZ |
1437 | mutex_unlock(&event_mutex); |
1438 | ||
1439 | return err; | |
1440 | } | |
1441 | ||
1442 | #endif /* CONFIG_EVENT_PROFILE */ | |
1443 |