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"
30 #define DEFAULT_SYS_FILTER_MESSAGE \
31 "### global filter ###\n" \
32 "# Use this to set filters for multiple events.\n" \
33 "# Only events with the given fields will be affected.\n" \
34 "# If no events are modified, an error message will be displayed here"
58 /* Order must be the same as enum filter_op_ids above */
59 static struct filter_op filter_ops[] = {
70 { OP_NONE, "OP_NONE", 0 },
71 { OP_OPEN_PAREN, "(", 0 },
77 FILT_ERR_UNBALANCED_PAREN,
78 FILT_ERR_TOO_MANY_OPERANDS,
79 FILT_ERR_OPERAND_TOO_LONG,
80 FILT_ERR_FIELD_NOT_FOUND,
81 FILT_ERR_ILLEGAL_FIELD_OP,
82 FILT_ERR_ILLEGAL_INTVAL,
83 FILT_ERR_BAD_SUBSYS_FILTER,
84 FILT_ERR_TOO_MANY_PREDS,
85 FILT_ERR_MISSING_FIELD,
86 FILT_ERR_INVALID_FILTER,
87 FILT_ERR_IP_FIELD_ONLY,
90 static char *err_text[] = {
97 "Illegal operation for field type",
98 "Illegal integer value",
99 "Couldn't find or set field in one of a subsystem's events",
100 "Too many terms in predicate expression",
101 "Missing field name and/or value",
102 "Meaningless filter expression",
103 "Only 'ip' field is supported for function trace",
108 struct list_head list;
114 struct list_head list;
117 struct filter_parse_state {
118 struct filter_op *ops;
119 struct list_head opstack;
120 struct list_head postfix;
131 char string[MAX_FILTER_STR_VAL];
138 struct filter_pred **preds;
142 #define DEFINE_COMPARISON_PRED(type) \
143 static int filter_pred_##type(struct filter_pred *pred, void *event) \
145 type *addr = (type *)(event + pred->offset); \
146 type val = (type)pred->val; \
149 switch (pred->op) { \
151 match = (*addr < val); \
154 match = (*addr <= val); \
157 match = (*addr > val); \
160 match = (*addr >= val); \
163 match = (*addr & val); \
172 #define DEFINE_EQUALITY_PRED(size) \
173 static int filter_pred_##size(struct filter_pred *pred, void *event) \
175 u##size *addr = (u##size *)(event + pred->offset); \
176 u##size val = (u##size)pred->val; \
179 match = (val == *addr) ^ pred->not; \
184 DEFINE_COMPARISON_PRED(s64);
185 DEFINE_COMPARISON_PRED(u64);
186 DEFINE_COMPARISON_PRED(s32);
187 DEFINE_COMPARISON_PRED(u32);
188 DEFINE_COMPARISON_PRED(s16);
189 DEFINE_COMPARISON_PRED(u16);
190 DEFINE_COMPARISON_PRED(s8);
191 DEFINE_COMPARISON_PRED(u8);
193 DEFINE_EQUALITY_PRED(64);
194 DEFINE_EQUALITY_PRED(32);
195 DEFINE_EQUALITY_PRED(16);
196 DEFINE_EQUALITY_PRED(8);
198 /* Filter predicate for fixed sized arrays of characters */
199 static int filter_pred_string(struct filter_pred *pred, void *event)
201 char *addr = (char *)(event + pred->offset);
204 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
206 match = cmp ^ pred->not;
211 /* Filter predicate for char * pointers */
212 static int filter_pred_pchar(struct filter_pred *pred, void *event)
214 char **addr = (char **)(event + pred->offset);
216 int len = strlen(*addr) + 1; /* including tailing '\0' */
218 cmp = pred->regex.match(*addr, &pred->regex, len);
220 match = cmp ^ pred->not;
226 * Filter predicate for dynamic sized arrays of characters.
227 * These are implemented through a list of strings at the end
229 * Also each of these strings have a field in the entry which
230 * contains its offset from the beginning of the entry.
231 * We have then first to get this field, dereference it
232 * and add it to the address of the entry, and at last we have
233 * the address of the string.
235 static int filter_pred_strloc(struct filter_pred *pred, void *event)
237 u32 str_item = *(u32 *)(event + pred->offset);
238 int str_loc = str_item & 0xffff;
239 int str_len = str_item >> 16;
240 char *addr = (char *)(event + str_loc);
243 cmp = pred->regex.match(addr, &pred->regex, str_len);
245 match = cmp ^ pred->not;
250 static int filter_pred_none(struct filter_pred *pred, void *event)
256 * regex_match_foo - Basic regex callbacks
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')
263 * - @str might not be NULL-terminated if it's of type DYN_STRING
267 static int regex_match_full(char *str, struct regex *r, int len)
269 if (strncmp(str, r->pattern, len) == 0)
274 static int regex_match_front(char *str, struct regex *r, int len)
276 if (strncmp(str, r->pattern, r->len) == 0)
281 static int regex_match_middle(char *str, struct regex *r, int len)
283 if (strnstr(str, r->pattern, len))
288 static int regex_match_end(char *str, struct regex *r, int len)
290 int strlen = len - 1;
292 if (strlen >= r->len &&
293 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
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
305 * This passes in a buffer containing a regex and this function will
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.
311 * search returns the pointer to use for comparison.
312 * not returns 1 if buff started with a '!'
315 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
317 int type = MATCH_FULL;
320 if (buff[0] == '!') {
329 for (i = 0; i < len; i++) {
330 if (buff[i] == '*') {
333 type = MATCH_END_ONLY;
335 if (type == MATCH_END_ONLY)
336 type = MATCH_MIDDLE_ONLY;
338 type = MATCH_FRONT_ONLY;
348 static void filter_build_regex(struct filter_pred *pred)
350 struct regex *r = &pred->regex;
352 enum regex_type type = MATCH_FULL;
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);
363 r->match = regex_match_full;
365 case MATCH_FRONT_ONLY:
366 r->match = regex_match_front;
368 case MATCH_MIDDLE_ONLY:
369 r->match = regex_match_middle;
372 r->match = regex_match_end;
385 static struct filter_pred *
386 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
387 int index, enum move_type *move)
389 if (pred->parent & FILTER_PRED_IS_RIGHT)
390 *move = MOVE_UP_FROM_RIGHT;
392 *move = MOVE_UP_FROM_LEFT;
393 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
404 typedef int (*filter_pred_walkcb_t) (enum move_type move,
405 struct filter_pred *pred,
406 int *err, void *data);
408 static int walk_pred_tree(struct filter_pred *preds,
409 struct filter_pred *root,
410 filter_pred_walkcb_t cb, void *data)
412 struct filter_pred *pred = root;
413 enum move_type move = MOVE_DOWN;
422 ret = cb(move, pred, &err, data);
423 if (ret == WALK_PRED_ABORT)
425 if (ret == WALK_PRED_PARENT)
430 if (pred->left != FILTER_PRED_INVALID) {
431 pred = &preds[pred->left];
435 case MOVE_UP_FROM_LEFT:
436 pred = &preds[pred->right];
439 case MOVE_UP_FROM_RIGHT:
443 pred = get_pred_parent(pred, preds,
456 * A series of AND or ORs where found together. Instead of
457 * climbing up and down the tree branches, an array of the
458 * ops were made in order of checks. We can just move across
459 * the array and short circuit if needed.
461 static int process_ops(struct filter_pred *preds,
462 struct filter_pred *op, void *rec)
464 struct filter_pred *pred;
470 * Micro-optimization: We set type to true if op
471 * is an OR and false otherwise (AND). Then we
472 * just need to test if the match is equal to
473 * the type, and if it is, we can short circuit the
474 * rest of the checks:
476 * if ((match && op->op == OP_OR) ||
477 * (!match && op->op == OP_AND))
480 type = op->op == OP_OR;
482 for (i = 0; i < op->val; i++) {
483 pred = &preds[op->ops[i]];
484 if (!WARN_ON_ONCE(!pred->fn))
485 match = pred->fn(pred, rec);
492 struct filter_match_preds_data {
493 struct filter_pred *preds;
498 static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
499 int *err, void *data)
501 struct filter_match_preds_data *d = data;
506 /* only AND and OR have children */
507 if (pred->left != FILTER_PRED_INVALID) {
508 /* If ops is set, then it was folded. */
510 return WALK_PRED_DEFAULT;
511 /* We can treat folded ops as a leaf node */
512 d->match = process_ops(d->preds, pred, d->rec);
514 if (!WARN_ON_ONCE(!pred->fn))
515 d->match = pred->fn(pred, d->rec);
518 return WALK_PRED_PARENT;
519 case MOVE_UP_FROM_LEFT:
521 * Check for short circuits.
523 * Optimization: !!match == (pred->op == OP_OR)
525 * if ((match && pred->op == OP_OR) ||
526 * (!match && pred->op == OP_AND))
528 if (!!d->match == (pred->op == OP_OR))
529 return WALK_PRED_PARENT;
531 case MOVE_UP_FROM_RIGHT:
535 return WALK_PRED_DEFAULT;
538 /* return 1 if event matches, 0 otherwise (discard) */
539 int filter_match_preds(struct event_filter *filter, void *rec)
541 struct filter_pred *preds;
542 struct filter_pred *root;
543 struct filter_match_preds_data data = {
544 /* match is currently meaningless */
550 /* no filter is considered a match */
554 n_preds = filter->n_preds;
559 * n_preds, root and filter->preds are protect with preemption disabled.
561 root = rcu_dereference_sched(filter->root);
565 data.preds = preds = rcu_dereference_sched(filter->preds);
566 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
570 EXPORT_SYMBOL_GPL(filter_match_preds);
572 static void parse_error(struct filter_parse_state *ps, int err, int pos)
575 ps->lasterr_pos = pos;
578 static void remove_filter_string(struct event_filter *filter)
583 kfree(filter->filter_string);
584 filter->filter_string = NULL;
587 static int replace_filter_string(struct event_filter *filter,
590 kfree(filter->filter_string);
591 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
592 if (!filter->filter_string)
598 static int append_filter_string(struct event_filter *filter,
602 char *new_filter_string;
604 BUG_ON(!filter->filter_string);
605 newlen = strlen(filter->filter_string) + strlen(string) + 1;
606 new_filter_string = kmalloc(newlen, GFP_KERNEL);
607 if (!new_filter_string)
610 strcpy(new_filter_string, filter->filter_string);
611 strcat(new_filter_string, string);
612 kfree(filter->filter_string);
613 filter->filter_string = new_filter_string;
618 static void append_filter_err(struct filter_parse_state *ps,
619 struct event_filter *filter)
621 int pos = ps->lasterr_pos;
624 buf = (char *)__get_free_page(GFP_TEMPORARY);
628 append_filter_string(filter, "\n");
629 memset(buf, ' ', PAGE_SIZE);
630 if (pos > PAGE_SIZE - 128)
633 pbuf = &buf[pos] + 1;
635 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
636 append_filter_string(filter, buf);
637 free_page((unsigned long) buf);
640 static inline struct event_filter *event_filter(struct ftrace_event_file *file)
642 if (file->event_call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
643 return file->event_call->filter;
648 /* caller must hold event_mutex */
649 void print_event_filter(struct ftrace_event_file *file, struct trace_seq *s)
651 struct event_filter *filter = event_filter(file);
653 if (filter && filter->filter_string)
654 trace_seq_printf(s, "%s\n", filter->filter_string);
656 trace_seq_puts(s, "none\n");
659 void print_subsystem_event_filter(struct event_subsystem *system,
662 struct event_filter *filter;
664 mutex_lock(&event_mutex);
665 filter = system->filter;
666 if (filter && filter->filter_string)
667 trace_seq_printf(s, "%s\n", filter->filter_string);
669 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
670 mutex_unlock(&event_mutex);
673 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
675 stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
678 stack->index = n_preds;
682 static void __free_pred_stack(struct pred_stack *stack)
688 static int __push_pred_stack(struct pred_stack *stack,
689 struct filter_pred *pred)
691 int index = stack->index;
693 if (WARN_ON(index == 0))
696 stack->preds[--index] = pred;
697 stack->index = index;
701 static struct filter_pred *
702 __pop_pred_stack(struct pred_stack *stack)
704 struct filter_pred *pred;
705 int index = stack->index;
707 pred = stack->preds[index++];
711 stack->index = index;
715 static int filter_set_pred(struct event_filter *filter,
717 struct pred_stack *stack,
718 struct filter_pred *src)
720 struct filter_pred *dest = &filter->preds[idx];
721 struct filter_pred *left;
722 struct filter_pred *right;
727 if (dest->op == OP_OR || dest->op == OP_AND) {
728 right = __pop_pred_stack(stack);
729 left = __pop_pred_stack(stack);
733 * If both children can be folded
734 * and they are the same op as this op or a leaf,
735 * then this op can be folded.
737 if (left->index & FILTER_PRED_FOLD &&
738 (left->op == dest->op ||
739 left->left == FILTER_PRED_INVALID) &&
740 right->index & FILTER_PRED_FOLD &&
741 (right->op == dest->op ||
742 right->left == FILTER_PRED_INVALID))
743 dest->index |= FILTER_PRED_FOLD;
745 dest->left = left->index & ~FILTER_PRED_FOLD;
746 dest->right = right->index & ~FILTER_PRED_FOLD;
747 left->parent = dest->index & ~FILTER_PRED_FOLD;
748 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
751 * Make dest->left invalid to be used as a quick
752 * way to know this is a leaf node.
754 dest->left = FILTER_PRED_INVALID;
756 /* All leafs allow folding the parent ops. */
757 dest->index |= FILTER_PRED_FOLD;
760 return __push_pred_stack(stack, dest);
763 static void __free_preds(struct event_filter *filter)
768 for (i = 0; i < filter->n_preds; i++)
769 kfree(filter->preds[i].ops);
770 kfree(filter->preds);
771 filter->preds = NULL;
777 static void filter_disable(struct ftrace_event_file *file)
779 struct ftrace_event_call *call = file->event_call;
781 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
782 call->flags &= ~TRACE_EVENT_FL_FILTERED;
784 file->flags &= ~FTRACE_EVENT_FL_FILTERED;
787 static void __free_filter(struct event_filter *filter)
792 __free_preds(filter);
793 kfree(filter->filter_string);
797 void free_event_filter(struct event_filter *filter)
799 __free_filter(filter);
802 static struct event_filter *__alloc_filter(void)
804 struct event_filter *filter;
806 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
810 static int __alloc_preds(struct event_filter *filter, int n_preds)
812 struct filter_pred *pred;
816 __free_preds(filter);
818 filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
823 filter->a_preds = n_preds;
826 for (i = 0; i < n_preds; i++) {
827 pred = &filter->preds[i];
828 pred->fn = filter_pred_none;
834 static inline void __remove_filter(struct ftrace_event_file *file)
836 struct ftrace_event_call *call = file->event_call;
838 filter_disable(file);
839 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
840 remove_filter_string(call->filter);
842 remove_filter_string(file->filter);
845 static void filter_free_subsystem_preds(struct ftrace_subsystem_dir *dir,
846 struct trace_array *tr)
848 struct ftrace_event_file *file;
850 list_for_each_entry(file, &tr->events, list) {
851 if (file->system != dir)
853 __remove_filter(file);
857 static inline void __free_subsystem_filter(struct ftrace_event_file *file)
859 struct ftrace_event_call *call = file->event_call;
861 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) {
862 __free_filter(call->filter);
865 __free_filter(file->filter);
870 static void filter_free_subsystem_filters(struct ftrace_subsystem_dir *dir,
871 struct trace_array *tr)
873 struct ftrace_event_file *file;
875 list_for_each_entry(file, &tr->events, list) {
876 if (file->system != dir)
878 __free_subsystem_filter(file);
882 static int filter_add_pred(struct filter_parse_state *ps,
883 struct event_filter *filter,
884 struct filter_pred *pred,
885 struct pred_stack *stack)
889 if (WARN_ON(filter->n_preds == filter->a_preds)) {
890 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
894 err = filter_set_pred(filter, filter->n_preds, stack, pred);
903 int filter_assign_type(const char *type)
905 if (strstr(type, "__data_loc") && strstr(type, "char"))
906 return FILTER_DYN_STRING;
908 if (strchr(type, '[') && strstr(type, "char"))
909 return FILTER_STATIC_STRING;
914 static bool is_function_field(struct ftrace_event_field *field)
916 return field->filter_type == FILTER_TRACE_FN;
919 static bool is_string_field(struct ftrace_event_field *field)
921 return field->filter_type == FILTER_DYN_STRING ||
922 field->filter_type == FILTER_STATIC_STRING ||
923 field->filter_type == FILTER_PTR_STRING;
926 static int is_legal_op(struct ftrace_event_field *field, int op)
928 if (is_string_field(field) &&
929 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
931 if (!is_string_field(field) && op == OP_GLOB)
937 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
940 filter_pred_fn_t fn = NULL;
942 switch (field_size) {
944 if (op == OP_EQ || op == OP_NE)
946 else if (field_is_signed)
947 fn = filter_pred_s64;
949 fn = filter_pred_u64;
952 if (op == OP_EQ || op == OP_NE)
954 else if (field_is_signed)
955 fn = filter_pred_s32;
957 fn = filter_pred_u32;
960 if (op == OP_EQ || op == OP_NE)
962 else if (field_is_signed)
963 fn = filter_pred_s16;
965 fn = filter_pred_u16;
968 if (op == OP_EQ || op == OP_NE)
970 else if (field_is_signed)
980 static int init_pred(struct filter_parse_state *ps,
981 struct ftrace_event_field *field,
982 struct filter_pred *pred)
985 filter_pred_fn_t fn = filter_pred_none;
986 unsigned long long val;
989 pred->offset = field->offset;
991 if (!is_legal_op(field, pred->op)) {
992 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
996 if (is_string_field(field)) {
997 filter_build_regex(pred);
999 if (field->filter_type == FILTER_STATIC_STRING) {
1000 fn = filter_pred_string;
1001 pred->regex.field_len = field->size;
1002 } else if (field->filter_type == FILTER_DYN_STRING)
1003 fn = filter_pred_strloc;
1005 fn = filter_pred_pchar;
1006 } else if (is_function_field(field)) {
1007 if (strcmp(field->name, "ip")) {
1008 parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
1012 if (field->is_signed)
1013 ret = kstrtoll(pred->regex.pattern, 0, &val);
1015 ret = kstrtoull(pred->regex.pattern, 0, &val);
1017 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
1022 fn = select_comparison_fn(pred->op, field->size,
1025 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1030 if (pred->op == OP_NE)
1037 static void parse_init(struct filter_parse_state *ps,
1038 struct filter_op *ops,
1041 memset(ps, '\0', sizeof(*ps));
1043 ps->infix.string = infix_string;
1044 ps->infix.cnt = strlen(infix_string);
1047 INIT_LIST_HEAD(&ps->opstack);
1048 INIT_LIST_HEAD(&ps->postfix);
1051 static char infix_next(struct filter_parse_state *ps)
1058 return ps->infix.string[ps->infix.tail++];
1061 static char infix_peek(struct filter_parse_state *ps)
1063 if (ps->infix.tail == strlen(ps->infix.string))
1066 return ps->infix.string[ps->infix.tail];
1069 static void infix_advance(struct filter_parse_state *ps)
1078 static inline int is_precedence_lower(struct filter_parse_state *ps,
1081 return ps->ops[a].precedence < ps->ops[b].precedence;
1084 static inline int is_op_char(struct filter_parse_state *ps, char c)
1088 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1089 if (ps->ops[i].string[0] == c)
1096 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1098 char nextc = infix_peek(ps);
1106 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1107 if (!strcmp(opstr, ps->ops[i].string)) {
1109 return ps->ops[i].id;
1115 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1116 if (!strcmp(opstr, ps->ops[i].string))
1117 return ps->ops[i].id;
1123 static inline void clear_operand_string(struct filter_parse_state *ps)
1125 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1126 ps->operand.tail = 0;
1129 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1131 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1134 ps->operand.string[ps->operand.tail++] = c;
1139 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1141 struct opstack_op *opstack_op;
1143 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1147 opstack_op->op = op;
1148 list_add(&opstack_op->list, &ps->opstack);
1153 static int filter_opstack_empty(struct filter_parse_state *ps)
1155 return list_empty(&ps->opstack);
1158 static int filter_opstack_top(struct filter_parse_state *ps)
1160 struct opstack_op *opstack_op;
1162 if (filter_opstack_empty(ps))
1165 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1167 return opstack_op->op;
1170 static int filter_opstack_pop(struct filter_parse_state *ps)
1172 struct opstack_op *opstack_op;
1175 if (filter_opstack_empty(ps))
1178 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1179 op = opstack_op->op;
1180 list_del(&opstack_op->list);
1187 static void filter_opstack_clear(struct filter_parse_state *ps)
1189 while (!filter_opstack_empty(ps))
1190 filter_opstack_pop(ps);
1193 static char *curr_operand(struct filter_parse_state *ps)
1195 return ps->operand.string;
1198 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1200 struct postfix_elt *elt;
1202 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1207 elt->operand = kstrdup(operand, GFP_KERNEL);
1208 if (!elt->operand) {
1213 list_add_tail(&elt->list, &ps->postfix);
1218 static int postfix_append_op(struct filter_parse_state *ps, int op)
1220 struct postfix_elt *elt;
1222 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1227 elt->operand = NULL;
1229 list_add_tail(&elt->list, &ps->postfix);
1234 static void postfix_clear(struct filter_parse_state *ps)
1236 struct postfix_elt *elt;
1238 while (!list_empty(&ps->postfix)) {
1239 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1240 list_del(&elt->list);
1241 kfree(elt->operand);
1246 static int filter_parse(struct filter_parse_state *ps)
1252 while ((ch = infix_next(ps))) {
1264 if (is_op_char(ps, ch)) {
1265 op = infix_get_op(ps, ch);
1266 if (op == OP_NONE) {
1267 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1271 if (strlen(curr_operand(ps))) {
1272 postfix_append_operand(ps, curr_operand(ps));
1273 clear_operand_string(ps);
1276 while (!filter_opstack_empty(ps)) {
1277 top_op = filter_opstack_top(ps);
1278 if (!is_precedence_lower(ps, top_op, op)) {
1279 top_op = filter_opstack_pop(ps);
1280 postfix_append_op(ps, top_op);
1286 filter_opstack_push(ps, op);
1291 filter_opstack_push(ps, OP_OPEN_PAREN);
1296 if (strlen(curr_operand(ps))) {
1297 postfix_append_operand(ps, curr_operand(ps));
1298 clear_operand_string(ps);
1301 top_op = filter_opstack_pop(ps);
1302 while (top_op != OP_NONE) {
1303 if (top_op == OP_OPEN_PAREN)
1305 postfix_append_op(ps, top_op);
1306 top_op = filter_opstack_pop(ps);
1308 if (top_op == OP_NONE) {
1309 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1315 if (append_operand_char(ps, ch)) {
1316 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1321 if (strlen(curr_operand(ps)))
1322 postfix_append_operand(ps, curr_operand(ps));
1324 while (!filter_opstack_empty(ps)) {
1325 top_op = filter_opstack_pop(ps);
1326 if (top_op == OP_NONE)
1328 if (top_op == OP_OPEN_PAREN) {
1329 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1332 postfix_append_op(ps, top_op);
1338 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1339 struct ftrace_event_call *call,
1340 int op, char *operand1, char *operand2)
1342 struct ftrace_event_field *field;
1343 static struct filter_pred pred;
1345 memset(&pred, 0, sizeof(pred));
1348 if (op == OP_AND || op == OP_OR)
1351 if (!operand1 || !operand2) {
1352 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1356 field = trace_find_event_field(call, operand1);
1358 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1362 strcpy(pred.regex.pattern, operand2);
1363 pred.regex.len = strlen(pred.regex.pattern);
1365 return init_pred(ps, field, &pred) ? NULL : &pred;
1368 static int check_preds(struct filter_parse_state *ps)
1370 int n_normal_preds = 0, n_logical_preds = 0;
1371 struct postfix_elt *elt;
1374 list_for_each_entry(elt, &ps->postfix, list) {
1375 if (elt->op == OP_NONE) {
1381 if (elt->op == OP_AND || elt->op == OP_OR) {
1386 /* all ops should have operands */
1391 if (cnt != 1 || !n_normal_preds || n_logical_preds >= n_normal_preds) {
1392 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1399 static int count_preds(struct filter_parse_state *ps)
1401 struct postfix_elt *elt;
1404 list_for_each_entry(elt, &ps->postfix, list) {
1405 if (elt->op == OP_NONE)
1413 struct check_pred_data {
1418 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1419 int *err, void *data)
1421 struct check_pred_data *d = data;
1423 if (WARN_ON(d->count++ > d->max)) {
1425 return WALK_PRED_ABORT;
1427 return WALK_PRED_DEFAULT;
1431 * The tree is walked at filtering of an event. If the tree is not correctly
1432 * built, it may cause an infinite loop. Check here that the tree does
1435 static int check_pred_tree(struct event_filter *filter,
1436 struct filter_pred *root)
1438 struct check_pred_data data = {
1440 * The max that we can hit a node is three times.
1441 * Once going down, once coming up from left, and
1442 * once coming up from right. This is more than enough
1443 * since leafs are only hit a single time.
1445 .max = 3 * filter->n_preds,
1449 return walk_pred_tree(filter->preds, root,
1450 check_pred_tree_cb, &data);
1453 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1454 int *err, void *data)
1458 if ((move == MOVE_DOWN) &&
1459 (pred->left == FILTER_PRED_INVALID))
1462 return WALK_PRED_DEFAULT;
1465 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1469 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1474 struct fold_pred_data {
1475 struct filter_pred *root;
1480 static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1481 int *err, void *data)
1483 struct fold_pred_data *d = data;
1484 struct filter_pred *root = d->root;
1486 if (move != MOVE_DOWN)
1487 return WALK_PRED_DEFAULT;
1488 if (pred->left != FILTER_PRED_INVALID)
1489 return WALK_PRED_DEFAULT;
1491 if (WARN_ON(d->count == d->children)) {
1493 return WALK_PRED_ABORT;
1496 pred->index &= ~FILTER_PRED_FOLD;
1497 root->ops[d->count++] = pred->index;
1498 return WALK_PRED_DEFAULT;
1501 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1503 struct fold_pred_data data = {
1509 /* No need to keep the fold flag */
1510 root->index &= ~FILTER_PRED_FOLD;
1512 /* If the root is a leaf then do nothing */
1513 if (root->left == FILTER_PRED_INVALID)
1516 /* count the children */
1517 children = count_leafs(preds, &preds[root->left]);
1518 children += count_leafs(preds, &preds[root->right]);
1520 root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
1524 root->val = children;
1525 data.children = children;
1526 return walk_pred_tree(preds, root, fold_pred_cb, &data);
1529 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1530 int *err, void *data)
1532 struct filter_pred *preds = data;
1534 if (move != MOVE_DOWN)
1535 return WALK_PRED_DEFAULT;
1536 if (!(pred->index & FILTER_PRED_FOLD))
1537 return WALK_PRED_DEFAULT;
1539 *err = fold_pred(preds, pred);
1541 return WALK_PRED_ABORT;
1543 /* eveyrhing below is folded, continue with parent */
1544 return WALK_PRED_PARENT;
1548 * To optimize the processing of the ops, if we have several "ors" or
1549 * "ands" together, we can put them in an array and process them all
1550 * together speeding up the filter logic.
1552 static int fold_pred_tree(struct event_filter *filter,
1553 struct filter_pred *root)
1555 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1559 static int replace_preds(struct ftrace_event_call *call,
1560 struct event_filter *filter,
1561 struct filter_parse_state *ps,
1564 char *operand1 = NULL, *operand2 = NULL;
1565 struct filter_pred *pred;
1566 struct filter_pred *root;
1567 struct postfix_elt *elt;
1568 struct pred_stack stack = { }; /* init to NULL */
1572 n_preds = count_preds(ps);
1573 if (n_preds >= MAX_FILTER_PRED) {
1574 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1578 err = check_preds(ps);
1583 err = __alloc_pred_stack(&stack, n_preds);
1586 err = __alloc_preds(filter, n_preds);
1592 list_for_each_entry(elt, &ps->postfix, list) {
1593 if (elt->op == OP_NONE) {
1595 operand1 = elt->operand;
1597 operand2 = elt->operand;
1599 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1606 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1607 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1612 pred = create_pred(ps, call, elt->op, operand1, operand2);
1619 err = filter_add_pred(ps, filter, pred, &stack);
1624 operand1 = operand2 = NULL;
1628 /* We should have one item left on the stack */
1629 pred = __pop_pred_stack(&stack);
1632 /* This item is where we start from in matching */
1634 /* Make sure the stack is empty */
1635 pred = __pop_pred_stack(&stack);
1636 if (WARN_ON(pred)) {
1638 filter->root = NULL;
1641 err = check_pred_tree(filter, root);
1645 /* Optimize the tree */
1646 err = fold_pred_tree(filter, root);
1650 /* We don't set root until we know it works */
1652 filter->root = root;
1657 __free_pred_stack(&stack);
1661 static inline void event_set_filtered_flag(struct ftrace_event_file *file)
1663 struct ftrace_event_call *call = file->event_call;
1665 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1666 call->flags |= TRACE_EVENT_FL_FILTERED;
1668 file->flags |= FTRACE_EVENT_FL_FILTERED;
1671 static inline void event_set_filter(struct ftrace_event_file *file,
1672 struct event_filter *filter)
1674 struct ftrace_event_call *call = file->event_call;
1676 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1677 rcu_assign_pointer(call->filter, filter);
1679 rcu_assign_pointer(file->filter, filter);
1682 static inline void event_clear_filter(struct ftrace_event_file *file)
1684 struct ftrace_event_call *call = file->event_call;
1686 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1687 RCU_INIT_POINTER(call->filter, NULL);
1689 RCU_INIT_POINTER(file->filter, NULL);
1693 event_set_no_set_filter_flag(struct ftrace_event_file *file)
1695 struct ftrace_event_call *call = file->event_call;
1697 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1698 call->flags |= TRACE_EVENT_FL_NO_SET_FILTER;
1700 file->flags |= FTRACE_EVENT_FL_NO_SET_FILTER;
1704 event_clear_no_set_filter_flag(struct ftrace_event_file *file)
1706 struct ftrace_event_call *call = file->event_call;
1708 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1709 call->flags &= ~TRACE_EVENT_FL_NO_SET_FILTER;
1711 file->flags &= ~FTRACE_EVENT_FL_NO_SET_FILTER;
1715 event_no_set_filter_flag(struct ftrace_event_file *file)
1717 struct ftrace_event_call *call = file->event_call;
1719 if (file->flags & FTRACE_EVENT_FL_NO_SET_FILTER)
1722 if ((call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) &&
1723 (call->flags & TRACE_EVENT_FL_NO_SET_FILTER))
1729 struct filter_list {
1730 struct list_head list;
1731 struct event_filter *filter;
1734 static int replace_system_preds(struct ftrace_subsystem_dir *dir,
1735 struct trace_array *tr,
1736 struct filter_parse_state *ps,
1737 char *filter_string)
1739 struct ftrace_event_file *file;
1740 struct filter_list *filter_item;
1741 struct filter_list *tmp;
1742 LIST_HEAD(filter_list);
1746 list_for_each_entry(file, &tr->events, list) {
1747 if (file->system != dir)
1751 * Try to see if the filter can be applied
1752 * (filter arg is ignored on dry_run)
1754 err = replace_preds(file->event_call, NULL, ps, true);
1756 event_set_no_set_filter_flag(file);
1758 event_clear_no_set_filter_flag(file);
1761 list_for_each_entry(file, &tr->events, list) {
1762 struct event_filter *filter;
1764 if (file->system != dir)
1767 if (event_no_set_filter_flag(file))
1770 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1774 list_add_tail(&filter_item->list, &filter_list);
1776 filter_item->filter = __alloc_filter();
1777 if (!filter_item->filter)
1779 filter = filter_item->filter;
1781 /* Can only fail on no memory */
1782 err = replace_filter_string(filter, filter_string);
1786 err = replace_preds(file->event_call, filter, ps, false);
1788 filter_disable(file);
1789 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1790 append_filter_err(ps, filter);
1792 event_set_filtered_flag(file);
1794 * Regardless of if this returned an error, we still
1795 * replace the filter for the call.
1797 filter = event_filter(file);
1798 event_set_filter(file, filter_item->filter);
1799 filter_item->filter = filter;
1808 * The calls can still be using the old filters.
1809 * Do a synchronize_sched() to ensure all calls are
1810 * done with them before we free them.
1812 synchronize_sched();
1813 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1814 __free_filter(filter_item->filter);
1815 list_del(&filter_item->list);
1820 /* No call succeeded */
1821 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1822 list_del(&filter_item->list);
1825 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1828 /* If any call succeeded, we still need to sync */
1830 synchronize_sched();
1831 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1832 __free_filter(filter_item->filter);
1833 list_del(&filter_item->list);
1839 static int create_filter_start(char *filter_str, bool set_str,
1840 struct filter_parse_state **psp,
1841 struct event_filter **filterp)
1843 struct event_filter *filter;
1844 struct filter_parse_state *ps = NULL;
1847 WARN_ON_ONCE(*psp || *filterp);
1849 /* allocate everything, and if any fails, free all and fail */
1850 filter = __alloc_filter();
1851 if (filter && set_str)
1852 err = replace_filter_string(filter, filter_str);
1854 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1856 if (!filter || !ps || err) {
1858 __free_filter(filter);
1862 /* we're committed to creating a new filter */
1866 parse_init(ps, filter_ops, filter_str);
1867 err = filter_parse(ps);
1869 append_filter_err(ps, filter);
1873 static void create_filter_finish(struct filter_parse_state *ps)
1876 filter_opstack_clear(ps);
1883 * create_filter - create a filter for a ftrace_event_call
1884 * @call: ftrace_event_call to create a filter for
1885 * @filter_str: filter string
1886 * @set_str: remember @filter_str and enable detailed error in filter
1887 * @filterp: out param for created filter (always updated on return)
1889 * Creates a filter for @call with @filter_str. If @set_str is %true,
1890 * @filter_str is copied and recorded in the new filter.
1892 * On success, returns 0 and *@filterp points to the new filter. On
1893 * failure, returns -errno and *@filterp may point to %NULL or to a new
1894 * filter. In the latter case, the returned filter contains error
1895 * information if @set_str is %true and the caller is responsible for
1898 static int create_filter(struct ftrace_event_call *call,
1899 char *filter_str, bool set_str,
1900 struct event_filter **filterp)
1902 struct event_filter *filter = NULL;
1903 struct filter_parse_state *ps = NULL;
1906 err = create_filter_start(filter_str, set_str, &ps, &filter);
1908 err = replace_preds(call, filter, ps, false);
1910 append_filter_err(ps, filter);
1912 create_filter_finish(ps);
1918 int create_event_filter(struct ftrace_event_call *call,
1919 char *filter_str, bool set_str,
1920 struct event_filter **filterp)
1922 return create_filter(call, filter_str, set_str, filterp);
1926 * create_system_filter - create a filter for an event_subsystem
1927 * @system: event_subsystem to create a filter for
1928 * @filter_str: filter string
1929 * @filterp: out param for created filter (always updated on return)
1931 * Identical to create_filter() except that it creates a subsystem filter
1932 * and always remembers @filter_str.
1934 static int create_system_filter(struct ftrace_subsystem_dir *dir,
1935 struct trace_array *tr,
1936 char *filter_str, struct event_filter **filterp)
1938 struct event_filter *filter = NULL;
1939 struct filter_parse_state *ps = NULL;
1942 err = create_filter_start(filter_str, true, &ps, &filter);
1944 err = replace_system_preds(dir, tr, ps, filter_str);
1946 /* System filters just show a default message */
1947 kfree(filter->filter_string);
1948 filter->filter_string = NULL;
1950 append_filter_err(ps, filter);
1953 create_filter_finish(ps);
1959 /* caller must hold event_mutex */
1960 int apply_event_filter(struct ftrace_event_file *file, char *filter_string)
1962 struct ftrace_event_call *call = file->event_call;
1963 struct event_filter *filter;
1966 if (!strcmp(strstrip(filter_string), "0")) {
1967 filter_disable(file);
1968 filter = event_filter(file);
1973 event_clear_filter(file);
1975 /* Make sure the filter is not being used */
1976 synchronize_sched();
1977 __free_filter(filter);
1982 err = create_filter(call, filter_string, true, &filter);
1985 * Always swap the call filter with the new filter
1986 * even if there was an error. If there was an error
1987 * in the filter, we disable the filter and show the error
1991 struct event_filter *tmp;
1993 tmp = event_filter(file);
1995 event_set_filtered_flag(file);
1997 filter_disable(file);
1999 event_set_filter(file, filter);
2002 /* Make sure the call is done with the filter */
2003 synchronize_sched();
2011 int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
2012 char *filter_string)
2014 struct event_subsystem *system = dir->subsystem;
2015 struct trace_array *tr = dir->tr;
2016 struct event_filter *filter;
2019 mutex_lock(&event_mutex);
2021 /* Make sure the system still has events */
2022 if (!dir->nr_events) {
2027 if (!strcmp(strstrip(filter_string), "0")) {
2028 filter_free_subsystem_preds(dir, tr);
2029 remove_filter_string(system->filter);
2030 filter = system->filter;
2031 system->filter = NULL;
2032 /* Ensure all filters are no longer used */
2033 synchronize_sched();
2034 filter_free_subsystem_filters(dir, tr);
2035 __free_filter(filter);
2039 err = create_system_filter(dir, tr, filter_string, &filter);
2042 * No event actually uses the system filter
2043 * we can free it without synchronize_sched().
2045 __free_filter(system->filter);
2046 system->filter = filter;
2049 mutex_unlock(&event_mutex);
2054 #ifdef CONFIG_PERF_EVENTS
2056 void ftrace_profile_free_filter(struct perf_event *event)
2058 struct event_filter *filter = event->filter;
2060 event->filter = NULL;
2061 __free_filter(filter);
2064 struct function_filter_data {
2065 struct ftrace_ops *ops;
2070 #ifdef CONFIG_FUNCTION_TRACER
2072 ftrace_function_filter_re(char *buf, int len, int *count)
2074 char *str, *sep, **re;
2076 str = kstrndup(buf, len, GFP_KERNEL);
2081 * The argv_split function takes white space
2082 * as a separator, so convert ',' into spaces.
2084 while ((sep = strchr(str, ',')))
2087 re = argv_split(GFP_KERNEL, str, count);
2092 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2093 int reset, char *re, int len)
2098 ret = ftrace_set_filter(ops, re, len, reset);
2100 ret = ftrace_set_notrace(ops, re, len, reset);
2105 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2106 struct function_filter_data *data)
2108 int i, re_cnt, ret = -EINVAL;
2112 reset = filter ? &data->first_filter : &data->first_notrace;
2115 * The 'ip' field could have multiple filters set, separated
2116 * either by space or comma. We first cut the filter and apply
2117 * all pieces separatelly.
2119 re = ftrace_function_filter_re(buf, len, &re_cnt);
2123 for (i = 0; i < re_cnt; i++) {
2124 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2125 re[i], strlen(re[i]));
2137 static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
2139 struct ftrace_event_field *field = pred->field;
2143 * Check the leaf predicate for function trace, verify:
2144 * - only '==' and '!=' is used
2145 * - the 'ip' field is used
2147 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2150 if (strcmp(field->name, "ip"))
2154 * Check the non leaf predicate for function trace, verify:
2155 * - only '||' is used
2157 if (pred->op != OP_OR)
2164 static int ftrace_function_set_filter_cb(enum move_type move,
2165 struct filter_pred *pred,
2166 int *err, void *data)
2168 /* Checking the node is valid for function trace. */
2169 if ((move != MOVE_DOWN) ||
2170 (pred->left != FILTER_PRED_INVALID)) {
2171 *err = ftrace_function_check_pred(pred, 0);
2173 *err = ftrace_function_check_pred(pred, 1);
2175 return WALK_PRED_ABORT;
2177 *err = __ftrace_function_set_filter(pred->op == OP_EQ,
2178 pred->regex.pattern,
2183 return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
2186 static int ftrace_function_set_filter(struct perf_event *event,
2187 struct event_filter *filter)
2189 struct function_filter_data data = {
2192 .ops = &event->ftrace_ops,
2195 return walk_pred_tree(filter->preds, filter->root,
2196 ftrace_function_set_filter_cb, &data);
2199 static int ftrace_function_set_filter(struct perf_event *event,
2200 struct event_filter *filter)
2204 #endif /* CONFIG_FUNCTION_TRACER */
2206 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2210 struct event_filter *filter;
2211 struct ftrace_event_call *call;
2213 mutex_lock(&event_mutex);
2215 call = event->tp_event;
2225 err = create_filter(call, filter_str, false, &filter);
2229 if (ftrace_event_is_function(call))
2230 err = ftrace_function_set_filter(event, filter);
2232 event->filter = filter;
2235 if (err || ftrace_event_is_function(call))
2236 __free_filter(filter);
2239 mutex_unlock(&event_mutex);
2244 #endif /* CONFIG_PERF_EVENTS */
2246 #ifdef CONFIG_FTRACE_STARTUP_TEST
2248 #include <linux/types.h>
2249 #include <linux/tracepoint.h>
2251 #define CREATE_TRACE_POINTS
2252 #include "trace_events_filter_test.h"
2254 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2257 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2258 .e = ve, .f = vf, .g = vg, .h = vh }, \
2260 .not_visited = nvisit, \
2265 static struct test_filter_data_t {
2267 struct ftrace_raw_ftrace_test_filter rec;
2270 } test_filter_data[] = {
2271 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2272 "e == 1 && f == 1 && g == 1 && h == 1"
2273 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2274 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2275 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2277 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2278 "e == 1 || f == 1 || g == 1 || h == 1"
2279 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2280 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2281 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2283 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2284 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2285 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2286 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2287 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2288 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2290 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2291 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2292 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2293 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2294 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2296 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2297 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2298 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2299 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2300 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2302 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2303 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2304 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2305 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2306 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2308 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2309 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2310 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2311 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2312 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2314 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2315 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2316 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2317 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2318 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2326 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2328 static int test_pred_visited;
2330 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2332 struct ftrace_event_field *field = pred->field;
2334 test_pred_visited = 1;
2335 printk(KERN_INFO "\npred visited %s\n", field->name);
2339 static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2340 int *err, void *data)
2342 char *fields = data;
2344 if ((move == MOVE_DOWN) &&
2345 (pred->left == FILTER_PRED_INVALID)) {
2346 struct ftrace_event_field *field = pred->field;
2349 WARN(1, "all leafs should have field defined");
2350 return WALK_PRED_DEFAULT;
2352 if (!strchr(fields, *field->name))
2353 return WALK_PRED_DEFAULT;
2356 pred->fn = test_pred_visited_fn;
2358 return WALK_PRED_DEFAULT;
2361 static __init int ftrace_test_event_filter(void)
2365 printk(KERN_INFO "Testing ftrace filter: ");
2367 for (i = 0; i < DATA_CNT; i++) {
2368 struct event_filter *filter = NULL;
2369 struct test_filter_data_t *d = &test_filter_data[i];
2372 err = create_filter(&event_ftrace_test_filter, d->filter,
2376 "Failed to get filter for '%s', err %d\n",
2378 __free_filter(filter);
2383 * The preemption disabling is not really needed for self
2384 * tests, but the rcu dereference will complain without it.
2387 if (*d->not_visited)
2388 walk_pred_tree(filter->preds, filter->root,
2392 test_pred_visited = 0;
2393 err = filter_match_preds(filter, &d->rec);
2396 __free_filter(filter);
2398 if (test_pred_visited) {
2400 "Failed, unwanted pred visited for filter %s\n",
2405 if (err != d->match) {
2407 "Failed to match filter '%s', expected %d\n",
2408 d->filter, d->match);
2414 printk(KERN_CONT "OK\n");
2419 late_initcall(ftrace_test_event_filter);
2421 #endif /* CONFIG_FTRACE_STARTUP_TEST */