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>
25 #include <linux/slab.h>
28 #include "trace_output.h"
51 static struct filter_op filter_ops[] = {
61 { OP_NONE, "OP_NONE", 0 },
62 { OP_OPEN_PAREN, "(", 0 },
68 FILT_ERR_UNBALANCED_PAREN,
69 FILT_ERR_TOO_MANY_OPERANDS,
70 FILT_ERR_OPERAND_TOO_LONG,
71 FILT_ERR_FIELD_NOT_FOUND,
72 FILT_ERR_ILLEGAL_FIELD_OP,
73 FILT_ERR_ILLEGAL_INTVAL,
74 FILT_ERR_BAD_SUBSYS_FILTER,
75 FILT_ERR_TOO_MANY_PREDS,
76 FILT_ERR_MISSING_FIELD,
77 FILT_ERR_INVALID_FILTER,
80 static char *err_text[] = {
87 "Illegal operation for field type",
88 "Illegal integer value",
89 "Couldn't find or set field in one of a subsystem's events",
90 "Too many terms in predicate expression",
91 "Missing field name and/or value",
92 "Meaningless filter expression",
97 struct list_head list;
103 struct list_head list;
106 struct filter_parse_state {
107 struct filter_op *ops;
108 struct list_head opstack;
109 struct list_head postfix;
120 char string[MAX_FILTER_STR_VAL];
126 #define DEFINE_COMPARISON_PRED(type) \
127 static int filter_pred_##type(struct filter_pred *pred, void *event) \
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) \
156 u##size *addr = (u##size *)(event + pred->offset); \
157 u##size val = (u##size)pred->val; \
160 match = (val == *addr) ^ pred->not; \
165 DEFINE_COMPARISON_PRED(s64);
166 DEFINE_COMPARISON_PRED(u64);
167 DEFINE_COMPARISON_PRED(s32);
168 DEFINE_COMPARISON_PRED(u32);
169 DEFINE_COMPARISON_PRED(s16);
170 DEFINE_COMPARISON_PRED(u16);
171 DEFINE_COMPARISON_PRED(s8);
172 DEFINE_COMPARISON_PRED(u8);
174 DEFINE_EQUALITY_PRED(64);
175 DEFINE_EQUALITY_PRED(32);
176 DEFINE_EQUALITY_PRED(16);
177 DEFINE_EQUALITY_PRED(8);
179 /* Filter predicate for fixed sized arrays of characters */
180 static int filter_pred_string(struct filter_pred *pred, void *event)
182 char *addr = (char *)(event + pred->offset);
185 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
187 match = cmp ^ pred->not;
192 /* Filter predicate for char * pointers */
193 static int filter_pred_pchar(struct filter_pred *pred, void *event)
195 char **addr = (char **)(event + pred->offset);
197 int len = strlen(*addr) + 1; /* including tailing '\0' */
199 cmp = pred->regex.match(*addr, &pred->regex, len);
201 match = cmp ^ pred->not;
207 * Filter predicate for dynamic sized arrays of characters.
208 * These are implemented through a list of strings at the end
210 * Also each of these strings have a field in the entry which
211 * contains its offset from the beginning of the entry.
212 * We have then first to get this field, dereference it
213 * and add it to the address of the entry, and at last we have
214 * the address of the string.
216 static int filter_pred_strloc(struct filter_pred *pred, void *event)
218 u32 str_item = *(u32 *)(event + pred->offset);
219 int str_loc = str_item & 0xffff;
220 int str_len = str_item >> 16;
221 char *addr = (char *)(event + str_loc);
224 cmp = pred->regex.match(addr, &pred->regex, str_len);
226 match = cmp ^ pred->not;
231 static int filter_pred_none(struct filter_pred *pred, void *event)
237 * regex_match_foo - Basic regex callbacks
239 * @str: the string to be searched
240 * @r: the regex structure containing the pattern string
241 * @len: the length of the string to be searched (including '\0')
244 * - @str might not be NULL-terminated if it's of type DYN_STRING
248 static int regex_match_full(char *str, struct regex *r, int len)
250 if (strncmp(str, r->pattern, len) == 0)
255 static int regex_match_front(char *str, struct regex *r, int len)
257 if (strncmp(str, r->pattern, r->len) == 0)
262 static int regex_match_middle(char *str, struct regex *r, int len)
264 if (strnstr(str, r->pattern, len))
269 static int regex_match_end(char *str, struct regex *r, int len)
271 int strlen = len - 1;
273 if (strlen >= r->len &&
274 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
280 * filter_parse_regex - parse a basic regex
281 * @buff: the raw regex
282 * @len: length of the regex
283 * @search: will point to the beginning of the string to compare
284 * @not: tell whether the match will have to be inverted
286 * This passes in a buffer containing a regex and this function will
287 * set search to point to the search part of the buffer and
288 * return the type of search it is (see enum above).
289 * This does modify buff.
292 * search returns the pointer to use for comparison.
293 * not returns 1 if buff started with a '!'
296 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
298 int type = MATCH_FULL;
301 if (buff[0] == '!') {
310 for (i = 0; i < len; i++) {
311 if (buff[i] == '*') {
314 type = MATCH_END_ONLY;
316 if (type == MATCH_END_ONLY)
317 type = MATCH_MIDDLE_ONLY;
319 type = MATCH_FRONT_ONLY;
329 static void filter_build_regex(struct filter_pred *pred)
331 struct regex *r = &pred->regex;
333 enum regex_type type = MATCH_FULL;
336 if (pred->op == OP_GLOB) {
337 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
338 r->len = strlen(search);
339 memmove(r->pattern, search, r->len+1);
344 r->match = regex_match_full;
346 case MATCH_FRONT_ONLY:
347 r->match = regex_match_front;
349 case MATCH_MIDDLE_ONLY:
350 r->match = regex_match_middle;
353 r->match = regex_match_end;
360 /* return 1 if event matches, 0 otherwise (discard) */
361 int filter_match_preds(struct event_filter *filter, void *rec)
363 int match = -1, top = 0, val1 = 0, val2 = 0;
364 int stack[MAX_FILTER_PRED];
365 struct filter_pred **preds;
366 struct filter_pred *pred;
367 int n_preds = ACCESS_ONCE(filter->n_preds);
370 /* no filter is considered a match */
375 * n_preds and filter->preds is protect with preemption disabled.
377 preds = rcu_dereference_sched(filter->preds);
379 for (i = 0; i < n_preds; i++) {
382 match = pred->fn(pred, rec);
383 stack[top++] = match;
386 if (pred->pop_n > top) {
394 match = val1 && val2;
397 match = val1 || val2;
400 WARN_ONCE(1, "filter op is not AND or OR");
402 stack[top++] = match;
407 EXPORT_SYMBOL_GPL(filter_match_preds);
409 static void parse_error(struct filter_parse_state *ps, int err, int pos)
412 ps->lasterr_pos = pos;
415 static void remove_filter_string(struct event_filter *filter)
417 kfree(filter->filter_string);
418 filter->filter_string = NULL;
421 static int replace_filter_string(struct event_filter *filter,
424 kfree(filter->filter_string);
425 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
426 if (!filter->filter_string)
432 static int append_filter_string(struct event_filter *filter,
436 char *new_filter_string;
438 BUG_ON(!filter->filter_string);
439 newlen = strlen(filter->filter_string) + strlen(string) + 1;
440 new_filter_string = kmalloc(newlen, GFP_KERNEL);
441 if (!new_filter_string)
444 strcpy(new_filter_string, filter->filter_string);
445 strcat(new_filter_string, string);
446 kfree(filter->filter_string);
447 filter->filter_string = new_filter_string;
452 static void append_filter_err(struct filter_parse_state *ps,
453 struct event_filter *filter)
455 int pos = ps->lasterr_pos;
458 buf = (char *)__get_free_page(GFP_TEMPORARY);
462 append_filter_string(filter, "\n");
463 memset(buf, ' ', PAGE_SIZE);
464 if (pos > PAGE_SIZE - 128)
467 pbuf = &buf[pos] + 1;
469 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
470 append_filter_string(filter, buf);
471 free_page((unsigned long) buf);
474 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
476 struct event_filter *filter = call->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 void print_subsystem_event_filter(struct event_subsystem *system,
489 struct event_filter *filter = system->filter;
491 mutex_lock(&event_mutex);
492 if (filter && filter->filter_string)
493 trace_seq_printf(s, "%s\n", filter->filter_string);
495 trace_seq_printf(s, "none\n");
496 mutex_unlock(&event_mutex);
499 static struct ftrace_event_field *
500 __find_event_field(struct list_head *head, char *name)
502 struct ftrace_event_field *field;
504 list_for_each_entry(field, head, link) {
505 if (!strcmp(field->name, name))
512 static struct ftrace_event_field *
513 find_event_field(struct ftrace_event_call *call, char *name)
515 struct ftrace_event_field *field;
516 struct list_head *head;
518 field = __find_event_field(&ftrace_common_fields, name);
522 head = trace_get_fields(call);
523 return __find_event_field(head, name);
526 static void filter_free_pred(struct filter_pred *pred)
531 kfree(pred->field_name);
535 static void filter_clear_pred(struct filter_pred *pred)
537 kfree(pred->field_name);
538 pred->field_name = NULL;
542 static int filter_set_pred(struct filter_pred *dest,
543 struct filter_pred *src,
547 if (src->field_name) {
548 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
549 if (!dest->field_name)
557 static void __free_preds(struct event_filter *filter)
562 for (i = 0; i < filter->a_preds; i++) {
563 if (filter->preds[i])
564 filter_free_pred(filter->preds[i]);
566 kfree(filter->preds);
567 filter->preds = NULL;
573 static void filter_disable_preds(struct ftrace_event_call *call)
575 struct event_filter *filter = call->filter;
578 call->flags &= ~TRACE_EVENT_FL_FILTERED;
580 for (i = 0; i < filter->n_preds; i++)
581 filter->preds[i]->fn = filter_pred_none;
586 static void __free_filter(struct event_filter *filter)
591 __free_preds(filter);
592 kfree(filter->filter_string);
596 void destroy_preds(struct ftrace_event_call *call)
598 __free_filter(call->filter);
600 call->flags &= ~TRACE_EVENT_FL_FILTERED;
603 static struct event_filter *__alloc_filter(void)
605 struct event_filter *filter;
607 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
609 return ERR_PTR(-ENOMEM);
616 static int __alloc_preds(struct event_filter *filter, int n_preds)
618 struct filter_pred *pred;
622 if (filter->a_preds < n_preds) {
623 /* We need to reallocate */
626 * It is possible that the filter is currently
627 * being used. We need to zero out the number
628 * of preds, wait on preemption and then free
632 __free_preds(filter);
636 if (!filter->preds) {
638 kzalloc(sizeof(*filter->preds) * n_preds, GFP_KERNEL);
639 filter->a_preds = n_preds;
644 if (WARN_ON(filter->a_preds < n_preds))
647 for (i = 0; i < n_preds; i++) {
648 pred = filter->preds[i];
650 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
653 pred->fn = filter_pred_none;
654 filter->preds[i] = pred;
659 __free_preds(filter);
663 static int init_filter(struct ftrace_event_call *call)
668 call->flags &= ~TRACE_EVENT_FL_FILTERED;
669 call->filter = __alloc_filter();
670 if (IS_ERR(call->filter))
671 return PTR_ERR(call->filter);
676 static int init_subsystem_preds(struct event_subsystem *system)
678 struct ftrace_event_call *call;
681 list_for_each_entry(call, &ftrace_events, list) {
682 if (strcmp(call->class->system, system->name) != 0)
685 err = init_filter(call);
693 static void filter_free_subsystem_preds(struct event_subsystem *system)
695 struct ftrace_event_call *call;
697 list_for_each_entry(call, &ftrace_events, list) {
698 if (strcmp(call->class->system, system->name) != 0)
701 filter_disable_preds(call);
702 remove_filter_string(call->filter);
706 static int filter_add_pred_fn(struct filter_parse_state *ps,
707 struct ftrace_event_call *call,
708 struct event_filter *filter,
709 struct filter_pred *pred,
714 if (WARN_ON(filter->n_preds == filter->a_preds)) {
715 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
719 idx = filter->n_preds;
720 filter_clear_pred(filter->preds[idx]);
721 err = filter_set_pred(filter->preds[idx], pred, fn);
730 int filter_assign_type(const char *type)
732 if (strstr(type, "__data_loc") && strstr(type, "char"))
733 return FILTER_DYN_STRING;
735 if (strchr(type, '[') && strstr(type, "char"))
736 return FILTER_STATIC_STRING;
741 static bool is_string_field(struct ftrace_event_field *field)
743 return field->filter_type == FILTER_DYN_STRING ||
744 field->filter_type == FILTER_STATIC_STRING ||
745 field->filter_type == FILTER_PTR_STRING;
748 static int is_legal_op(struct ftrace_event_field *field, int op)
750 if (is_string_field(field) &&
751 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
753 if (!is_string_field(field) && op == OP_GLOB)
759 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
762 filter_pred_fn_t fn = NULL;
764 switch (field_size) {
766 if (op == OP_EQ || op == OP_NE)
768 else if (field_is_signed)
769 fn = filter_pred_s64;
771 fn = filter_pred_u64;
774 if (op == OP_EQ || op == OP_NE)
776 else if (field_is_signed)
777 fn = filter_pred_s32;
779 fn = filter_pred_u32;
782 if (op == OP_EQ || op == OP_NE)
784 else if (field_is_signed)
785 fn = filter_pred_s16;
787 fn = filter_pred_u16;
790 if (op == OP_EQ || op == OP_NE)
792 else if (field_is_signed)
802 static int filter_add_pred(struct filter_parse_state *ps,
803 struct ftrace_event_call *call,
804 struct event_filter *filter,
805 struct filter_pred *pred,
808 struct ftrace_event_field *field;
810 unsigned long long val;
813 fn = pred->fn = filter_pred_none;
815 if (pred->op == OP_AND) {
818 } else if (pred->op == OP_OR) {
823 field = find_event_field(call, pred->field_name);
825 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
829 pred->offset = field->offset;
831 if (!is_legal_op(field, pred->op)) {
832 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
836 if (is_string_field(field)) {
837 filter_build_regex(pred);
839 if (field->filter_type == FILTER_STATIC_STRING) {
840 fn = filter_pred_string;
841 pred->regex.field_len = field->size;
842 } else if (field->filter_type == FILTER_DYN_STRING)
843 fn = filter_pred_strloc;
845 fn = filter_pred_pchar;
847 if (field->is_signed)
848 ret = strict_strtoll(pred->regex.pattern, 0, &val);
850 ret = strict_strtoull(pred->regex.pattern, 0, &val);
852 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
857 fn = select_comparison_fn(pred->op, field->size,
860 parse_error(ps, FILT_ERR_INVALID_OP, 0);
865 if (pred->op == OP_NE)
870 return filter_add_pred_fn(ps, call, filter, pred, fn);
874 static void parse_init(struct filter_parse_state *ps,
875 struct filter_op *ops,
878 memset(ps, '\0', sizeof(*ps));
880 ps->infix.string = infix_string;
881 ps->infix.cnt = strlen(infix_string);
884 INIT_LIST_HEAD(&ps->opstack);
885 INIT_LIST_HEAD(&ps->postfix);
888 static char infix_next(struct filter_parse_state *ps)
892 return ps->infix.string[ps->infix.tail++];
895 static char infix_peek(struct filter_parse_state *ps)
897 if (ps->infix.tail == strlen(ps->infix.string))
900 return ps->infix.string[ps->infix.tail];
903 static void infix_advance(struct filter_parse_state *ps)
909 static inline int is_precedence_lower(struct filter_parse_state *ps,
912 return ps->ops[a].precedence < ps->ops[b].precedence;
915 static inline int is_op_char(struct filter_parse_state *ps, char c)
919 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
920 if (ps->ops[i].string[0] == c)
927 static int infix_get_op(struct filter_parse_state *ps, char firstc)
929 char nextc = infix_peek(ps);
937 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
938 if (!strcmp(opstr, ps->ops[i].string)) {
940 return ps->ops[i].id;
946 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
947 if (!strcmp(opstr, ps->ops[i].string))
948 return ps->ops[i].id;
954 static inline void clear_operand_string(struct filter_parse_state *ps)
956 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
957 ps->operand.tail = 0;
960 static inline int append_operand_char(struct filter_parse_state *ps, char c)
962 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
965 ps->operand.string[ps->operand.tail++] = c;
970 static int filter_opstack_push(struct filter_parse_state *ps, int op)
972 struct opstack_op *opstack_op;
974 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
979 list_add(&opstack_op->list, &ps->opstack);
984 static int filter_opstack_empty(struct filter_parse_state *ps)
986 return list_empty(&ps->opstack);
989 static int filter_opstack_top(struct filter_parse_state *ps)
991 struct opstack_op *opstack_op;
993 if (filter_opstack_empty(ps))
996 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
998 return opstack_op->op;
1001 static int filter_opstack_pop(struct filter_parse_state *ps)
1003 struct opstack_op *opstack_op;
1006 if (filter_opstack_empty(ps))
1009 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1010 op = opstack_op->op;
1011 list_del(&opstack_op->list);
1018 static void filter_opstack_clear(struct filter_parse_state *ps)
1020 while (!filter_opstack_empty(ps))
1021 filter_opstack_pop(ps);
1024 static char *curr_operand(struct filter_parse_state *ps)
1026 return ps->operand.string;
1029 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1031 struct postfix_elt *elt;
1033 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1038 elt->operand = kstrdup(operand, GFP_KERNEL);
1039 if (!elt->operand) {
1044 list_add_tail(&elt->list, &ps->postfix);
1049 static int postfix_append_op(struct filter_parse_state *ps, int op)
1051 struct postfix_elt *elt;
1053 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1058 elt->operand = NULL;
1060 list_add_tail(&elt->list, &ps->postfix);
1065 static void postfix_clear(struct filter_parse_state *ps)
1067 struct postfix_elt *elt;
1069 while (!list_empty(&ps->postfix)) {
1070 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1071 list_del(&elt->list);
1072 kfree(elt->operand);
1077 static int filter_parse(struct filter_parse_state *ps)
1083 while ((ch = infix_next(ps))) {
1095 if (is_op_char(ps, ch)) {
1096 op = infix_get_op(ps, ch);
1097 if (op == OP_NONE) {
1098 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1102 if (strlen(curr_operand(ps))) {
1103 postfix_append_operand(ps, curr_operand(ps));
1104 clear_operand_string(ps);
1107 while (!filter_opstack_empty(ps)) {
1108 top_op = filter_opstack_top(ps);
1109 if (!is_precedence_lower(ps, top_op, op)) {
1110 top_op = filter_opstack_pop(ps);
1111 postfix_append_op(ps, top_op);
1117 filter_opstack_push(ps, op);
1122 filter_opstack_push(ps, OP_OPEN_PAREN);
1127 if (strlen(curr_operand(ps))) {
1128 postfix_append_operand(ps, curr_operand(ps));
1129 clear_operand_string(ps);
1132 top_op = filter_opstack_pop(ps);
1133 while (top_op != OP_NONE) {
1134 if (top_op == OP_OPEN_PAREN)
1136 postfix_append_op(ps, top_op);
1137 top_op = filter_opstack_pop(ps);
1139 if (top_op == OP_NONE) {
1140 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1146 if (append_operand_char(ps, ch)) {
1147 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1152 if (strlen(curr_operand(ps)))
1153 postfix_append_operand(ps, curr_operand(ps));
1155 while (!filter_opstack_empty(ps)) {
1156 top_op = filter_opstack_pop(ps);
1157 if (top_op == OP_NONE)
1159 if (top_op == OP_OPEN_PAREN) {
1160 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1163 postfix_append_op(ps, top_op);
1169 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1171 struct filter_pred *pred;
1173 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1177 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1178 if (!pred->field_name) {
1183 strcpy(pred->regex.pattern, operand2);
1184 pred->regex.len = strlen(pred->regex.pattern);
1191 static struct filter_pred *create_logical_pred(int op)
1193 struct filter_pred *pred;
1195 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1204 static int check_preds(struct filter_parse_state *ps)
1206 int n_normal_preds = 0, n_logical_preds = 0;
1207 struct postfix_elt *elt;
1209 list_for_each_entry(elt, &ps->postfix, list) {
1210 if (elt->op == OP_NONE)
1213 if (elt->op == OP_AND || elt->op == OP_OR) {
1220 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1221 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1228 static int count_preds(struct filter_parse_state *ps)
1230 struct postfix_elt *elt;
1233 list_for_each_entry(elt, &ps->postfix, list) {
1234 if (elt->op == OP_NONE)
1242 static int replace_preds(struct ftrace_event_call *call,
1243 struct event_filter *filter,
1244 struct filter_parse_state *ps,
1245 char *filter_string,
1248 char *operand1 = NULL, *operand2 = NULL;
1249 struct filter_pred *pred;
1250 struct postfix_elt *elt;
1254 n_preds = count_preds(ps);
1255 if (n_preds >= MAX_FILTER_PRED) {
1256 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1260 err = check_preds(ps);
1265 err = __alloc_preds(filter, n_preds);
1271 list_for_each_entry(elt, &ps->postfix, list) {
1272 if (elt->op == OP_NONE) {
1274 operand1 = elt->operand;
1276 operand2 = elt->operand;
1278 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1284 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1285 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1289 if (elt->op == OP_AND || elt->op == OP_OR) {
1290 pred = create_logical_pred(elt->op);
1294 if (!operand1 || !operand2) {
1295 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1299 pred = create_pred(elt->op, operand1, operand2);
1303 err = filter_add_pred(ps, call, filter, pred, dry_run);
1304 filter_free_pred(pred);
1308 operand1 = operand2 = NULL;
1314 static int replace_system_preds(struct event_subsystem *system,
1315 struct filter_parse_state *ps,
1316 char *filter_string)
1318 struct ftrace_event_call *call;
1322 list_for_each_entry(call, &ftrace_events, list) {
1323 struct event_filter *filter = call->filter;
1325 if (strcmp(call->class->system, system->name) != 0)
1328 /* try to see if the filter can be applied */
1329 err = replace_preds(call, filter, ps, filter_string, true);
1333 /* really apply the filter */
1334 filter_disable_preds(call);
1335 err = replace_preds(call, filter, ps, filter_string, false);
1337 filter_disable_preds(call);
1339 call->flags |= TRACE_EVENT_FL_FILTERED;
1340 replace_filter_string(filter, filter_string);
1346 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1352 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1355 struct filter_parse_state *ps;
1357 mutex_lock(&event_mutex);
1359 err = init_filter(call);
1363 if (!strcmp(strstrip(filter_string), "0")) {
1364 filter_disable_preds(call);
1365 remove_filter_string(call->filter);
1370 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1374 filter_disable_preds(call);
1375 replace_filter_string(call->filter, filter_string);
1377 parse_init(ps, filter_ops, filter_string);
1378 err = filter_parse(ps);
1380 append_filter_err(ps, call->filter);
1384 err = replace_preds(call, call->filter, ps, filter_string, false);
1386 append_filter_err(ps, call->filter);
1388 call->flags |= TRACE_EVENT_FL_FILTERED;
1390 filter_opstack_clear(ps);
1394 mutex_unlock(&event_mutex);
1399 int apply_subsystem_event_filter(struct event_subsystem *system,
1400 char *filter_string)
1403 struct filter_parse_state *ps;
1405 mutex_lock(&event_mutex);
1407 err = init_subsystem_preds(system);
1411 if (!strcmp(strstrip(filter_string), "0")) {
1412 filter_free_subsystem_preds(system);
1413 remove_filter_string(system->filter);
1418 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1422 replace_filter_string(system->filter, filter_string);
1424 parse_init(ps, filter_ops, filter_string);
1425 err = filter_parse(ps);
1427 append_filter_err(ps, system->filter);
1431 err = replace_system_preds(system, ps, filter_string);
1433 append_filter_err(ps, system->filter);
1436 filter_opstack_clear(ps);
1440 mutex_unlock(&event_mutex);
1445 #ifdef CONFIG_PERF_EVENTS
1447 void ftrace_profile_free_filter(struct perf_event *event)
1449 struct event_filter *filter = event->filter;
1451 event->filter = NULL;
1452 __free_filter(filter);
1455 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
1459 struct event_filter *filter;
1460 struct filter_parse_state *ps;
1461 struct ftrace_event_call *call = NULL;
1463 mutex_lock(&event_mutex);
1465 list_for_each_entry(call, &ftrace_events, list) {
1466 if (call->event.type == event_id)
1471 if (&call->list == &ftrace_events)
1478 filter = __alloc_filter();
1479 if (IS_ERR(filter)) {
1480 err = PTR_ERR(filter);
1485 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1489 parse_init(ps, filter_ops, filter_str);
1490 err = filter_parse(ps);
1494 err = replace_preds(call, filter, ps, filter_str, false);
1496 event->filter = filter;
1499 filter_opstack_clear(ps);
1505 __free_filter(filter);
1508 mutex_unlock(&event_mutex);
1513 #endif /* CONFIG_PERF_EVENTS */