2 * trace_events_filter - generic event filtering
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.
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.
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.
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/perf_event.h>
27 #include "trace_output.h"
50 static struct filter_op filter_ops[] = {
60 { OP_NONE, "OP_NONE", 0 },
61 { OP_OPEN_PAREN, "(", 0 },
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,
79 static char *err_text[] = {
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",
96 struct list_head list;
102 struct list_head list;
105 struct filter_parse_state {
106 struct filter_op *ops;
107 struct list_head opstack;
108 struct list_head postfix;
119 char string[MAX_FILTER_STR_VAL];
125 #define DEFINE_COMPARISON_PRED(type) \
126 static int filter_pred_##type(struct filter_pred *pred, void *event, \
127 int val1, int val2) \
129 type *addr = (type *)(event + pred->offset); \
130 type val = (type)pred->val; \
133 switch (pred->op) { \
135 match = (*addr < val); \
138 match = (*addr <= val); \
141 match = (*addr > val); \
144 match = (*addr >= val); \
153 #define DEFINE_EQUALITY_PRED(size) \
154 static int filter_pred_##size(struct filter_pred *pred, void *event, \
155 int val1, int val2) \
157 u##size *addr = (u##size *)(event + pred->offset); \
158 u##size val = (u##size)pred->val; \
161 match = (val == *addr) ^ pred->not; \
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);
175 DEFINE_EQUALITY_PRED(64);
176 DEFINE_EQUALITY_PRED(32);
177 DEFINE_EQUALITY_PRED(16);
178 DEFINE_EQUALITY_PRED(8);
180 static int filter_pred_and(struct filter_pred *pred __attribute((unused)),
181 void *event __attribute((unused)),
187 static int filter_pred_or(struct filter_pred *pred __attribute((unused)),
188 void *event __attribute((unused)),
194 /* Filter predicate for fixed sized arrays of characters */
195 static int filter_pred_string(struct filter_pred *pred, void *event,
198 char *addr = (char *)(event + pred->offset);
201 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
203 match = cmp ^ pred->not;
208 /* Filter predicate for char * pointers */
209 static int filter_pred_pchar(struct filter_pred *pred, void *event,
212 char **addr = (char **)(event + pred->offset);
215 cmp = pred->regex.match(*addr, &pred->regex, pred->regex.field_len);
217 match = cmp ^ pred->not;
223 * Filter predicate for dynamic sized arrays of characters.
224 * These are implemented through a list of strings at the end
226 * Also each of these strings have a field in the entry which
227 * contains its offset from the beginning of the entry.
228 * We have then first to get this field, dereference it
229 * and add it to the address of the entry, and at last we have
230 * the address of the string.
232 static int filter_pred_strloc(struct filter_pred *pred, void *event,
235 u32 str_item = *(u32 *)(event + pred->offset);
236 int str_loc = str_item & 0xffff;
237 int str_len = str_item >> 16;
238 char *addr = (char *)(event + str_loc);
241 cmp = pred->regex.match(addr, &pred->regex, str_len);
243 match = cmp ^ pred->not;
248 static int filter_pred_none(struct filter_pred *pred, void *event,
254 /* Basic regex callbacks */
255 static int regex_match_full(char *str, struct regex *r, int len)
257 if (strncmp(str, r->pattern, len) == 0)
262 static int regex_match_front(char *str, struct regex *r, int len)
264 if (strncmp(str, r->pattern, r->len) == 0)
269 static int regex_match_middle(char *str, struct regex *r, int len)
271 if (strnstr(str, r->pattern, len))
276 static int regex_match_end(char *str, struct regex *r, int len)
278 int strlen = len - 1;
280 if (strlen >= r->len &&
281 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
287 * filter_parse_regex - parse a basic regex
288 * @buff: the raw regex
289 * @len: length of the regex
290 * @search: will point to the beginning of the string to compare
291 * @not: tell whether the match will have to be inverted
293 * This passes in a buffer containing a regex and this function will
294 * set search to point to the search part of the buffer and
295 * return the type of search it is (see enum above).
296 * This does modify buff.
299 * search returns the pointer to use for comparison.
300 * not returns 1 if buff started with a '!'
303 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
305 int type = MATCH_FULL;
308 if (buff[0] == '!') {
317 for (i = 0; i < len; i++) {
318 if (buff[i] == '*') {
321 type = MATCH_END_ONLY;
323 if (type == MATCH_END_ONLY)
324 type = MATCH_MIDDLE_ONLY;
326 type = MATCH_FRONT_ONLY;
336 static void filter_build_regex(struct filter_pred *pred)
338 struct regex *r = &pred->regex;
340 enum regex_type type = MATCH_FULL;
343 if (pred->op == OP_GLOB) {
344 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
345 r->len = strlen(search);
346 memmove(r->pattern, search, r->len+1);
351 r->match = regex_match_full;
353 case MATCH_FRONT_ONLY:
354 r->match = regex_match_front;
356 case MATCH_MIDDLE_ONLY:
357 r->match = regex_match_middle;
360 r->match = regex_match_end;
367 /* return 1 if event matches, 0 otherwise (discard) */
368 int filter_match_preds(struct event_filter *filter, void *rec)
370 int match, top = 0, val1 = 0, val2 = 0;
371 int stack[MAX_FILTER_PRED];
372 struct filter_pred *pred;
375 for (i = 0; i < filter->n_preds; i++) {
376 pred = filter->preds[i];
378 match = pred->fn(pred, rec, val1, val2);
379 stack[top++] = match;
382 if (pred->pop_n > top) {
388 match = pred->fn(pred, rec, val1, val2);
389 stack[top++] = match;
394 EXPORT_SYMBOL_GPL(filter_match_preds);
396 static void parse_error(struct filter_parse_state *ps, int err, int pos)
399 ps->lasterr_pos = pos;
402 static void remove_filter_string(struct event_filter *filter)
404 kfree(filter->filter_string);
405 filter->filter_string = NULL;
408 static int replace_filter_string(struct event_filter *filter,
411 kfree(filter->filter_string);
412 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
413 if (!filter->filter_string)
419 static int append_filter_string(struct event_filter *filter,
423 char *new_filter_string;
425 BUG_ON(!filter->filter_string);
426 newlen = strlen(filter->filter_string) + strlen(string) + 1;
427 new_filter_string = kmalloc(newlen, GFP_KERNEL);
428 if (!new_filter_string)
431 strcpy(new_filter_string, filter->filter_string);
432 strcat(new_filter_string, string);
433 kfree(filter->filter_string);
434 filter->filter_string = new_filter_string;
439 static void append_filter_err(struct filter_parse_state *ps,
440 struct event_filter *filter)
442 int pos = ps->lasterr_pos;
445 buf = (char *)__get_free_page(GFP_TEMPORARY);
449 append_filter_string(filter, "\n");
450 memset(buf, ' ', PAGE_SIZE);
451 if (pos > PAGE_SIZE - 128)
454 pbuf = &buf[pos] + 1;
456 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
457 append_filter_string(filter, buf);
458 free_page((unsigned long) buf);
461 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
463 struct event_filter *filter = call->filter;
465 mutex_lock(&event_mutex);
466 if (filter && filter->filter_string)
467 trace_seq_printf(s, "%s\n", filter->filter_string);
469 trace_seq_printf(s, "none\n");
470 mutex_unlock(&event_mutex);
473 void print_subsystem_event_filter(struct event_subsystem *system,
476 struct event_filter *filter = system->filter;
478 mutex_lock(&event_mutex);
479 if (filter && filter->filter_string)
480 trace_seq_printf(s, "%s\n", filter->filter_string);
482 trace_seq_printf(s, "none\n");
483 mutex_unlock(&event_mutex);
486 static struct ftrace_event_field *
487 find_event_field(struct ftrace_event_call *call, char *name)
489 struct ftrace_event_field *field;
491 list_for_each_entry(field, &call->fields, link) {
492 if (!strcmp(field->name, name))
499 static void filter_free_pred(struct filter_pred *pred)
504 kfree(pred->field_name);
508 static void filter_clear_pred(struct filter_pred *pred)
510 kfree(pred->field_name);
511 pred->field_name = NULL;
515 static int filter_set_pred(struct filter_pred *dest,
516 struct filter_pred *src,
520 if (src->field_name) {
521 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
522 if (!dest->field_name)
530 static void filter_disable_preds(struct ftrace_event_call *call)
532 struct event_filter *filter = call->filter;
535 call->filter_active = 0;
538 for (i = 0; i < MAX_FILTER_PRED; i++)
539 filter->preds[i]->fn = filter_pred_none;
542 static void __free_preds(struct event_filter *filter)
549 for (i = 0; i < MAX_FILTER_PRED; i++) {
550 if (filter->preds[i])
551 filter_free_pred(filter->preds[i]);
553 kfree(filter->preds);
554 kfree(filter->filter_string);
558 void destroy_preds(struct ftrace_event_call *call)
560 __free_preds(call->filter);
562 call->filter_active = 0;
565 static struct event_filter *__alloc_preds(void)
567 struct event_filter *filter;
568 struct filter_pred *pred;
571 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
573 return ERR_PTR(-ENOMEM);
577 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
581 for (i = 0; i < MAX_FILTER_PRED; i++) {
582 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
585 pred->fn = filter_pred_none;
586 filter->preds[i] = pred;
592 __free_preds(filter);
593 return ERR_PTR(-ENOMEM);
596 static int init_preds(struct ftrace_event_call *call)
601 call->filter_active = 0;
602 call->filter = __alloc_preds();
603 if (IS_ERR(call->filter))
604 return PTR_ERR(call->filter);
609 static int init_subsystem_preds(struct event_subsystem *system)
611 struct ftrace_event_call *call;
614 list_for_each_entry(call, &ftrace_events, list) {
615 if (!call->define_fields)
618 if (strcmp(call->system, system->name) != 0)
621 err = init_preds(call);
629 static void filter_free_subsystem_preds(struct event_subsystem *system)
631 struct ftrace_event_call *call;
633 list_for_each_entry(call, &ftrace_events, list) {
634 if (!call->define_fields)
637 if (strcmp(call->system, system->name) != 0)
640 filter_disable_preds(call);
641 remove_filter_string(call->filter);
645 static int filter_add_pred_fn(struct filter_parse_state *ps,
646 struct ftrace_event_call *call,
647 struct event_filter *filter,
648 struct filter_pred *pred,
653 if (filter->n_preds == MAX_FILTER_PRED) {
654 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
658 idx = filter->n_preds;
659 filter_clear_pred(filter->preds[idx]);
660 err = filter_set_pred(filter->preds[idx], pred, fn);
669 int filter_assign_type(const char *type)
671 if (strstr(type, "__data_loc") && strstr(type, "char"))
672 return FILTER_DYN_STRING;
674 if (strchr(type, '[') && strstr(type, "char"))
675 return FILTER_STATIC_STRING;
680 static bool is_string_field(struct ftrace_event_field *field)
682 return field->filter_type == FILTER_DYN_STRING ||
683 field->filter_type == FILTER_STATIC_STRING ||
684 field->filter_type == FILTER_PTR_STRING;
687 static int is_legal_op(struct ftrace_event_field *field, int op)
689 if (is_string_field(field) &&
690 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
692 if (!is_string_field(field) && op == OP_GLOB)
698 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
701 filter_pred_fn_t fn = NULL;
703 switch (field_size) {
705 if (op == OP_EQ || op == OP_NE)
707 else if (field_is_signed)
708 fn = filter_pred_s64;
710 fn = filter_pred_u64;
713 if (op == OP_EQ || op == OP_NE)
715 else if (field_is_signed)
716 fn = filter_pred_s32;
718 fn = filter_pred_u32;
721 if (op == OP_EQ || op == OP_NE)
723 else if (field_is_signed)
724 fn = filter_pred_s16;
726 fn = filter_pred_u16;
729 if (op == OP_EQ || op == OP_NE)
731 else if (field_is_signed)
741 static int filter_add_pred(struct filter_parse_state *ps,
742 struct ftrace_event_call *call,
743 struct event_filter *filter,
744 struct filter_pred *pred,
747 struct ftrace_event_field *field;
749 unsigned long long val;
752 pred->fn = filter_pred_none;
754 if (pred->op == OP_AND) {
756 fn = filter_pred_and;
758 } else if (pred->op == OP_OR) {
764 field = find_event_field(call, pred->field_name);
766 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
770 pred->offset = field->offset;
772 if (!is_legal_op(field, pred->op)) {
773 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
777 if (is_string_field(field)) {
778 filter_build_regex(pred);
780 if (field->filter_type == FILTER_STATIC_STRING) {
781 fn = filter_pred_string;
782 pred->regex.field_len = field->size;
783 } else if (field->filter_type == FILTER_DYN_STRING)
784 fn = filter_pred_strloc;
786 fn = filter_pred_pchar;
787 pred->regex.field_len = strlen(pred->regex.pattern);
790 if (field->is_signed)
791 ret = strict_strtoll(pred->regex.pattern, 0, &val);
793 ret = strict_strtoull(pred->regex.pattern, 0, &val);
795 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
800 fn = select_comparison_fn(pred->op, field->size,
803 parse_error(ps, FILT_ERR_INVALID_OP, 0);
808 if (pred->op == OP_NE)
813 return filter_add_pred_fn(ps, call, filter, pred, fn);
817 static void parse_init(struct filter_parse_state *ps,
818 struct filter_op *ops,
821 memset(ps, '\0', sizeof(*ps));
823 ps->infix.string = infix_string;
824 ps->infix.cnt = strlen(infix_string);
827 INIT_LIST_HEAD(&ps->opstack);
828 INIT_LIST_HEAD(&ps->postfix);
831 static char infix_next(struct filter_parse_state *ps)
835 return ps->infix.string[ps->infix.tail++];
838 static char infix_peek(struct filter_parse_state *ps)
840 if (ps->infix.tail == strlen(ps->infix.string))
843 return ps->infix.string[ps->infix.tail];
846 static void infix_advance(struct filter_parse_state *ps)
852 static inline int is_precedence_lower(struct filter_parse_state *ps,
855 return ps->ops[a].precedence < ps->ops[b].precedence;
858 static inline int is_op_char(struct filter_parse_state *ps, char c)
862 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
863 if (ps->ops[i].string[0] == c)
870 static int infix_get_op(struct filter_parse_state *ps, char firstc)
872 char nextc = infix_peek(ps);
880 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
881 if (!strcmp(opstr, ps->ops[i].string)) {
883 return ps->ops[i].id;
889 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
890 if (!strcmp(opstr, ps->ops[i].string))
891 return ps->ops[i].id;
897 static inline void clear_operand_string(struct filter_parse_state *ps)
899 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
900 ps->operand.tail = 0;
903 static inline int append_operand_char(struct filter_parse_state *ps, char c)
905 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
908 ps->operand.string[ps->operand.tail++] = c;
913 static int filter_opstack_push(struct filter_parse_state *ps, int op)
915 struct opstack_op *opstack_op;
917 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
922 list_add(&opstack_op->list, &ps->opstack);
927 static int filter_opstack_empty(struct filter_parse_state *ps)
929 return list_empty(&ps->opstack);
932 static int filter_opstack_top(struct filter_parse_state *ps)
934 struct opstack_op *opstack_op;
936 if (filter_opstack_empty(ps))
939 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
941 return opstack_op->op;
944 static int filter_opstack_pop(struct filter_parse_state *ps)
946 struct opstack_op *opstack_op;
949 if (filter_opstack_empty(ps))
952 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
954 list_del(&opstack_op->list);
961 static void filter_opstack_clear(struct filter_parse_state *ps)
963 while (!filter_opstack_empty(ps))
964 filter_opstack_pop(ps);
967 static char *curr_operand(struct filter_parse_state *ps)
969 return ps->operand.string;
972 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
974 struct postfix_elt *elt;
976 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
981 elt->operand = kstrdup(operand, GFP_KERNEL);
987 list_add_tail(&elt->list, &ps->postfix);
992 static int postfix_append_op(struct filter_parse_state *ps, int op)
994 struct postfix_elt *elt;
996 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1001 elt->operand = NULL;
1003 list_add_tail(&elt->list, &ps->postfix);
1008 static void postfix_clear(struct filter_parse_state *ps)
1010 struct postfix_elt *elt;
1012 while (!list_empty(&ps->postfix)) {
1013 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1014 list_del(&elt->list);
1015 kfree(elt->operand);
1020 static int filter_parse(struct filter_parse_state *ps)
1026 while ((ch = infix_next(ps))) {
1038 if (is_op_char(ps, ch)) {
1039 op = infix_get_op(ps, ch);
1040 if (op == OP_NONE) {
1041 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1045 if (strlen(curr_operand(ps))) {
1046 postfix_append_operand(ps, curr_operand(ps));
1047 clear_operand_string(ps);
1050 while (!filter_opstack_empty(ps)) {
1051 top_op = filter_opstack_top(ps);
1052 if (!is_precedence_lower(ps, top_op, op)) {
1053 top_op = filter_opstack_pop(ps);
1054 postfix_append_op(ps, top_op);
1060 filter_opstack_push(ps, op);
1065 filter_opstack_push(ps, OP_OPEN_PAREN);
1070 if (strlen(curr_operand(ps))) {
1071 postfix_append_operand(ps, curr_operand(ps));
1072 clear_operand_string(ps);
1075 top_op = filter_opstack_pop(ps);
1076 while (top_op != OP_NONE) {
1077 if (top_op == OP_OPEN_PAREN)
1079 postfix_append_op(ps, top_op);
1080 top_op = filter_opstack_pop(ps);
1082 if (top_op == OP_NONE) {
1083 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1089 if (append_operand_char(ps, ch)) {
1090 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1095 if (strlen(curr_operand(ps)))
1096 postfix_append_operand(ps, curr_operand(ps));
1098 while (!filter_opstack_empty(ps)) {
1099 top_op = filter_opstack_pop(ps);
1100 if (top_op == OP_NONE)
1102 if (top_op == OP_OPEN_PAREN) {
1103 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1106 postfix_append_op(ps, top_op);
1112 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1114 struct filter_pred *pred;
1116 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1120 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1121 if (!pred->field_name) {
1126 strcpy(pred->regex.pattern, operand2);
1127 pred->regex.len = strlen(pred->regex.pattern);
1134 static struct filter_pred *create_logical_pred(int op)
1136 struct filter_pred *pred;
1138 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1147 static int check_preds(struct filter_parse_state *ps)
1149 int n_normal_preds = 0, n_logical_preds = 0;
1150 struct postfix_elt *elt;
1152 list_for_each_entry(elt, &ps->postfix, list) {
1153 if (elt->op == OP_NONE)
1156 if (elt->op == OP_AND || elt->op == OP_OR) {
1163 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1164 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1171 static int replace_preds(struct ftrace_event_call *call,
1172 struct event_filter *filter,
1173 struct filter_parse_state *ps,
1174 char *filter_string,
1177 char *operand1 = NULL, *operand2 = NULL;
1178 struct filter_pred *pred;
1179 struct postfix_elt *elt;
1183 err = check_preds(ps);
1187 list_for_each_entry(elt, &ps->postfix, list) {
1188 if (elt->op == OP_NONE) {
1190 operand1 = elt->operand;
1192 operand2 = elt->operand;
1194 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1200 if (n_preds++ == MAX_FILTER_PRED) {
1201 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1205 if (elt->op == OP_AND || elt->op == OP_OR) {
1206 pred = create_logical_pred(elt->op);
1210 if (!operand1 || !operand2) {
1211 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1215 pred = create_pred(elt->op, operand1, operand2);
1219 err = filter_add_pred(ps, call, filter, pred, dry_run);
1220 filter_free_pred(pred);
1224 operand1 = operand2 = NULL;
1230 static int replace_system_preds(struct event_subsystem *system,
1231 struct filter_parse_state *ps,
1232 char *filter_string)
1234 struct ftrace_event_call *call;
1238 list_for_each_entry(call, &ftrace_events, list) {
1239 struct event_filter *filter = call->filter;
1241 if (!call->define_fields)
1244 if (strcmp(call->system, system->name) != 0)
1247 /* try to see if the filter can be applied */
1248 err = replace_preds(call, filter, ps, filter_string, true);
1252 /* really apply the filter */
1253 filter_disable_preds(call);
1254 err = replace_preds(call, filter, ps, filter_string, false);
1256 filter_disable_preds(call);
1258 call->filter_active = 1;
1259 replace_filter_string(filter, filter_string);
1265 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1271 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1274 struct filter_parse_state *ps;
1276 mutex_lock(&event_mutex);
1278 err = init_preds(call);
1282 if (!strcmp(strstrip(filter_string), "0")) {
1283 filter_disable_preds(call);
1284 remove_filter_string(call->filter);
1289 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1293 filter_disable_preds(call);
1294 replace_filter_string(call->filter, filter_string);
1296 parse_init(ps, filter_ops, filter_string);
1297 err = filter_parse(ps);
1299 append_filter_err(ps, call->filter);
1303 err = replace_preds(call, call->filter, ps, filter_string, false);
1305 append_filter_err(ps, call->filter);
1307 call->filter_active = 1;
1309 filter_opstack_clear(ps);
1313 mutex_unlock(&event_mutex);
1318 int apply_subsystem_event_filter(struct event_subsystem *system,
1319 char *filter_string)
1322 struct filter_parse_state *ps;
1324 mutex_lock(&event_mutex);
1326 err = init_subsystem_preds(system);
1330 if (!strcmp(strstrip(filter_string), "0")) {
1331 filter_free_subsystem_preds(system);
1332 remove_filter_string(system->filter);
1337 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1341 replace_filter_string(system->filter, filter_string);
1343 parse_init(ps, filter_ops, filter_string);
1344 err = filter_parse(ps);
1346 append_filter_err(ps, system->filter);
1350 err = replace_system_preds(system, ps, filter_string);
1352 append_filter_err(ps, system->filter);
1355 filter_opstack_clear(ps);
1359 mutex_unlock(&event_mutex);
1364 #ifdef CONFIG_EVENT_PROFILE
1366 void ftrace_profile_free_filter(struct perf_event *event)
1368 struct event_filter *filter = event->filter;
1370 event->filter = NULL;
1371 __free_preds(filter);
1374 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1378 struct event_filter *filter;
1379 struct filter_parse_state *ps;
1380 struct ftrace_event_call *call = NULL;
1382 mutex_lock(&event_mutex);
1384 list_for_each_entry(call, &ftrace_events, list) {
1385 if (call->id == event_id)
1397 filter = __alloc_preds();
1398 if (IS_ERR(filter)) {
1399 err = PTR_ERR(filter);
1404 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1408 parse_init(ps, filter_ops, filter_str);
1409 err = filter_parse(ps);
1413 err = replace_preds(call, filter, ps, filter_str, false);
1415 event->filter = filter;
1418 filter_opstack_clear(ps);
1424 __free_preds(filter);
1427 mutex_unlock(&event_mutex);
1432 #endif /* CONFIG_EVENT_PROFILE */