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"
59 /* Order must be the same as enum filter_op_ids above */
60 static struct filter_op filter_ops[] = {
72 { OP_NONE, "OP_NONE", 0 },
73 { OP_OPEN_PAREN, "(", 0 },
79 FILT_ERR_UNBALANCED_PAREN,
80 FILT_ERR_TOO_MANY_OPERANDS,
81 FILT_ERR_OPERAND_TOO_LONG,
82 FILT_ERR_FIELD_NOT_FOUND,
83 FILT_ERR_ILLEGAL_FIELD_OP,
84 FILT_ERR_ILLEGAL_INTVAL,
85 FILT_ERR_BAD_SUBSYS_FILTER,
86 FILT_ERR_TOO_MANY_PREDS,
87 FILT_ERR_MISSING_FIELD,
88 FILT_ERR_INVALID_FILTER,
89 FILT_ERR_IP_FIELD_ONLY,
90 FILT_ERR_ILLEGAL_NOT_OP,
93 static char *err_text[] = {
100 "Illegal operation for field type",
101 "Illegal integer value",
102 "Couldn't find or set field in one of a subsystem's events",
103 "Too many terms in predicate expression",
104 "Missing field name and/or value",
105 "Meaningless filter expression",
106 "Only 'ip' field is supported for function trace",
107 "Illegal use of '!'",
112 struct list_head list;
118 struct list_head list;
121 struct filter_parse_state {
122 struct filter_op *ops;
123 struct list_head opstack;
124 struct list_head postfix;
135 char string[MAX_FILTER_STR_VAL];
142 struct filter_pred **preds;
146 /* If not of not match is equal to not of not, then it is a match */
147 #define DEFINE_COMPARISON_PRED(type) \
148 static int filter_pred_##type(struct filter_pred *pred, void *event) \
150 type *addr = (type *)(event + pred->offset); \
151 type val = (type)pred->val; \
154 switch (pred->op) { \
156 match = (*addr < val); \
159 match = (*addr <= val); \
162 match = (*addr > val); \
165 match = (*addr >= val); \
168 match = (*addr & val); \
174 return !!match == !pred->not; \
177 #define DEFINE_EQUALITY_PRED(size) \
178 static int filter_pred_##size(struct filter_pred *pred, void *event) \
180 u##size *addr = (u##size *)(event + pred->offset); \
181 u##size val = (u##size)pred->val; \
184 match = (val == *addr) ^ pred->not; \
189 DEFINE_COMPARISON_PRED(s64);
190 DEFINE_COMPARISON_PRED(u64);
191 DEFINE_COMPARISON_PRED(s32);
192 DEFINE_COMPARISON_PRED(u32);
193 DEFINE_COMPARISON_PRED(s16);
194 DEFINE_COMPARISON_PRED(u16);
195 DEFINE_COMPARISON_PRED(s8);
196 DEFINE_COMPARISON_PRED(u8);
198 DEFINE_EQUALITY_PRED(64);
199 DEFINE_EQUALITY_PRED(32);
200 DEFINE_EQUALITY_PRED(16);
201 DEFINE_EQUALITY_PRED(8);
203 /* Filter predicate for fixed sized arrays of characters */
204 static int filter_pred_string(struct filter_pred *pred, void *event)
206 char *addr = (char *)(event + pred->offset);
209 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
211 match = cmp ^ pred->not;
216 /* Filter predicate for char * pointers */
217 static int filter_pred_pchar(struct filter_pred *pred, void *event)
219 char **addr = (char **)(event + pred->offset);
221 int len = strlen(*addr) + 1; /* including tailing '\0' */
223 cmp = pred->regex.match(*addr, &pred->regex, len);
225 match = cmp ^ pred->not;
231 * Filter predicate for dynamic sized arrays of characters.
232 * These are implemented through a list of strings at the end
234 * Also each of these strings have a field in the entry which
235 * contains its offset from the beginning of the entry.
236 * We have then first to get this field, dereference it
237 * and add it to the address of the entry, and at last we have
238 * the address of the string.
240 static int filter_pred_strloc(struct filter_pred *pred, void *event)
242 u32 str_item = *(u32 *)(event + pred->offset);
243 int str_loc = str_item & 0xffff;
244 int str_len = str_item >> 16;
245 char *addr = (char *)(event + str_loc);
248 cmp = pred->regex.match(addr, &pred->regex, str_len);
250 match = cmp ^ pred->not;
255 static int filter_pred_none(struct filter_pred *pred, void *event)
261 * regex_match_foo - Basic regex callbacks
263 * @str: the string to be searched
264 * @r: the regex structure containing the pattern string
265 * @len: the length of the string to be searched (including '\0')
268 * - @str might not be NULL-terminated if it's of type DYN_STRING
272 static int regex_match_full(char *str, struct regex *r, int len)
274 if (strncmp(str, r->pattern, len) == 0)
279 static int regex_match_front(char *str, struct regex *r, int len)
281 if (strncmp(str, r->pattern, r->len) == 0)
286 static int regex_match_middle(char *str, struct regex *r, int len)
288 if (strnstr(str, r->pattern, len))
293 static int regex_match_end(char *str, struct regex *r, int len)
295 int strlen = len - 1;
297 if (strlen >= r->len &&
298 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
304 * filter_parse_regex - parse a basic regex
305 * @buff: the raw regex
306 * @len: length of the regex
307 * @search: will point to the beginning of the string to compare
308 * @not: tell whether the match will have to be inverted
310 * This passes in a buffer containing a regex and this function will
311 * set search to point to the search part of the buffer and
312 * return the type of search it is (see enum above).
313 * This does modify buff.
316 * search returns the pointer to use for comparison.
317 * not returns 1 if buff started with a '!'
320 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
322 int type = MATCH_FULL;
325 if (buff[0] == '!') {
334 for (i = 0; i < len; i++) {
335 if (buff[i] == '*') {
338 type = MATCH_END_ONLY;
340 if (type == MATCH_END_ONLY)
341 type = MATCH_MIDDLE_ONLY;
343 type = MATCH_FRONT_ONLY;
353 static void filter_build_regex(struct filter_pred *pred)
355 struct regex *r = &pred->regex;
357 enum regex_type type = MATCH_FULL;
360 if (pred->op == OP_GLOB) {
361 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
362 r->len = strlen(search);
363 memmove(r->pattern, search, r->len+1);
368 r->match = regex_match_full;
370 case MATCH_FRONT_ONLY:
371 r->match = regex_match_front;
373 case MATCH_MIDDLE_ONLY:
374 r->match = regex_match_middle;
377 r->match = regex_match_end;
390 static struct filter_pred *
391 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
392 int index, enum move_type *move)
394 if (pred->parent & FILTER_PRED_IS_RIGHT)
395 *move = MOVE_UP_FROM_RIGHT;
397 *move = MOVE_UP_FROM_LEFT;
398 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
409 typedef int (*filter_pred_walkcb_t) (enum move_type move,
410 struct filter_pred *pred,
411 int *err, void *data);
413 static int walk_pred_tree(struct filter_pred *preds,
414 struct filter_pred *root,
415 filter_pred_walkcb_t cb, void *data)
417 struct filter_pred *pred = root;
418 enum move_type move = MOVE_DOWN;
427 ret = cb(move, pred, &err, data);
428 if (ret == WALK_PRED_ABORT)
430 if (ret == WALK_PRED_PARENT)
435 if (pred->left != FILTER_PRED_INVALID) {
436 pred = &preds[pred->left];
440 case MOVE_UP_FROM_LEFT:
441 pred = &preds[pred->right];
444 case MOVE_UP_FROM_RIGHT:
448 pred = get_pred_parent(pred, preds,
461 * A series of AND or ORs where found together. Instead of
462 * climbing up and down the tree branches, an array of the
463 * ops were made in order of checks. We can just move across
464 * the array and short circuit if needed.
466 static int process_ops(struct filter_pred *preds,
467 struct filter_pred *op, void *rec)
469 struct filter_pred *pred;
475 * Micro-optimization: We set type to true if op
476 * is an OR and false otherwise (AND). Then we
477 * just need to test if the match is equal to
478 * the type, and if it is, we can short circuit the
479 * rest of the checks:
481 * if ((match && op->op == OP_OR) ||
482 * (!match && op->op == OP_AND))
485 type = op->op == OP_OR;
487 for (i = 0; i < op->val; i++) {
488 pred = &preds[op->ops[i]];
489 if (!WARN_ON_ONCE(!pred->fn))
490 match = pred->fn(pred, rec);
497 struct filter_match_preds_data {
498 struct filter_pred *preds;
503 static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
504 int *err, void *data)
506 struct filter_match_preds_data *d = data;
511 /* only AND and OR have children */
512 if (pred->left != FILTER_PRED_INVALID) {
513 /* If ops is set, then it was folded. */
515 return WALK_PRED_DEFAULT;
516 /* We can treat folded ops as a leaf node */
517 d->match = process_ops(d->preds, pred, d->rec);
519 if (!WARN_ON_ONCE(!pred->fn))
520 d->match = pred->fn(pred, d->rec);
523 return WALK_PRED_PARENT;
524 case MOVE_UP_FROM_LEFT:
526 * Check for short circuits.
528 * Optimization: !!match == (pred->op == OP_OR)
530 * if ((match && pred->op == OP_OR) ||
531 * (!match && pred->op == OP_AND))
533 if (!!d->match == (pred->op == OP_OR))
534 return WALK_PRED_PARENT;
536 case MOVE_UP_FROM_RIGHT:
540 return WALK_PRED_DEFAULT;
543 /* return 1 if event matches, 0 otherwise (discard) */
544 int filter_match_preds(struct event_filter *filter, void *rec)
546 struct filter_pred *preds;
547 struct filter_pred *root;
548 struct filter_match_preds_data data = {
549 /* match is currently meaningless */
555 /* no filter is considered a match */
559 n_preds = filter->n_preds;
564 * n_preds, root and filter->preds are protect with preemption disabled.
566 root = rcu_dereference_sched(filter->root);
570 data.preds = preds = rcu_dereference_sched(filter->preds);
571 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
575 EXPORT_SYMBOL_GPL(filter_match_preds);
577 static void parse_error(struct filter_parse_state *ps, int err, int pos)
580 ps->lasterr_pos = pos;
583 static void remove_filter_string(struct event_filter *filter)
588 kfree(filter->filter_string);
589 filter->filter_string = NULL;
592 static int replace_filter_string(struct event_filter *filter,
595 kfree(filter->filter_string);
596 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
597 if (!filter->filter_string)
603 static int append_filter_string(struct event_filter *filter,
607 char *new_filter_string;
609 BUG_ON(!filter->filter_string);
610 newlen = strlen(filter->filter_string) + strlen(string) + 1;
611 new_filter_string = kmalloc(newlen, GFP_KERNEL);
612 if (!new_filter_string)
615 strcpy(new_filter_string, filter->filter_string);
616 strcat(new_filter_string, string);
617 kfree(filter->filter_string);
618 filter->filter_string = new_filter_string;
623 static void append_filter_err(struct filter_parse_state *ps,
624 struct event_filter *filter)
626 int pos = ps->lasterr_pos;
629 buf = (char *)__get_free_page(GFP_TEMPORARY);
633 append_filter_string(filter, "\n");
634 memset(buf, ' ', PAGE_SIZE);
635 if (pos > PAGE_SIZE - 128)
638 pbuf = &buf[pos] + 1;
640 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
641 append_filter_string(filter, buf);
642 free_page((unsigned long) buf);
645 static inline struct event_filter *event_filter(struct ftrace_event_file *file)
647 if (file->event_call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
648 return file->event_call->filter;
653 /* caller must hold event_mutex */
654 void print_event_filter(struct ftrace_event_file *file, struct trace_seq *s)
656 struct event_filter *filter = event_filter(file);
658 if (filter && filter->filter_string)
659 trace_seq_printf(s, "%s\n", filter->filter_string);
661 trace_seq_puts(s, "none\n");
664 void print_subsystem_event_filter(struct event_subsystem *system,
667 struct event_filter *filter;
669 mutex_lock(&event_mutex);
670 filter = system->filter;
671 if (filter && filter->filter_string)
672 trace_seq_printf(s, "%s\n", filter->filter_string);
674 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
675 mutex_unlock(&event_mutex);
678 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
680 stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
683 stack->index = n_preds;
687 static void __free_pred_stack(struct pred_stack *stack)
693 static int __push_pred_stack(struct pred_stack *stack,
694 struct filter_pred *pred)
696 int index = stack->index;
698 if (WARN_ON(index == 0))
701 stack->preds[--index] = pred;
702 stack->index = index;
706 static struct filter_pred *
707 __pop_pred_stack(struct pred_stack *stack)
709 struct filter_pred *pred;
710 int index = stack->index;
712 pred = stack->preds[index++];
716 stack->index = index;
720 static int filter_set_pred(struct event_filter *filter,
722 struct pred_stack *stack,
723 struct filter_pred *src)
725 struct filter_pred *dest = &filter->preds[idx];
726 struct filter_pred *left;
727 struct filter_pred *right;
732 if (dest->op == OP_OR || dest->op == OP_AND) {
733 right = __pop_pred_stack(stack);
734 left = __pop_pred_stack(stack);
738 * If both children can be folded
739 * and they are the same op as this op or a leaf,
740 * then this op can be folded.
742 if (left->index & FILTER_PRED_FOLD &&
743 (left->op == dest->op ||
744 left->left == FILTER_PRED_INVALID) &&
745 right->index & FILTER_PRED_FOLD &&
746 (right->op == dest->op ||
747 right->left == FILTER_PRED_INVALID))
748 dest->index |= FILTER_PRED_FOLD;
750 dest->left = left->index & ~FILTER_PRED_FOLD;
751 dest->right = right->index & ~FILTER_PRED_FOLD;
752 left->parent = dest->index & ~FILTER_PRED_FOLD;
753 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
756 * Make dest->left invalid to be used as a quick
757 * way to know this is a leaf node.
759 dest->left = FILTER_PRED_INVALID;
761 /* All leafs allow folding the parent ops. */
762 dest->index |= FILTER_PRED_FOLD;
765 return __push_pred_stack(stack, dest);
768 static void __free_preds(struct event_filter *filter)
773 for (i = 0; i < filter->n_preds; i++)
774 kfree(filter->preds[i].ops);
775 kfree(filter->preds);
776 filter->preds = NULL;
782 static void filter_disable(struct ftrace_event_file *file)
784 struct ftrace_event_call *call = file->event_call;
786 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
787 call->flags &= ~TRACE_EVENT_FL_FILTERED;
789 file->flags &= ~FTRACE_EVENT_FL_FILTERED;
792 static void __free_filter(struct event_filter *filter)
797 __free_preds(filter);
798 kfree(filter->filter_string);
802 void free_event_filter(struct event_filter *filter)
804 __free_filter(filter);
807 static struct event_filter *__alloc_filter(void)
809 struct event_filter *filter;
811 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
815 static int __alloc_preds(struct event_filter *filter, int n_preds)
817 struct filter_pred *pred;
821 __free_preds(filter);
823 filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
828 filter->a_preds = n_preds;
831 for (i = 0; i < n_preds; i++) {
832 pred = &filter->preds[i];
833 pred->fn = filter_pred_none;
839 static inline void __remove_filter(struct ftrace_event_file *file)
841 struct ftrace_event_call *call = file->event_call;
843 filter_disable(file);
844 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
845 remove_filter_string(call->filter);
847 remove_filter_string(file->filter);
850 static void filter_free_subsystem_preds(struct ftrace_subsystem_dir *dir,
851 struct trace_array *tr)
853 struct ftrace_event_file *file;
855 list_for_each_entry(file, &tr->events, list) {
856 if (file->system != dir)
858 __remove_filter(file);
862 static inline void __free_subsystem_filter(struct ftrace_event_file *file)
864 struct ftrace_event_call *call = file->event_call;
866 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) {
867 __free_filter(call->filter);
870 __free_filter(file->filter);
875 static void filter_free_subsystem_filters(struct ftrace_subsystem_dir *dir,
876 struct trace_array *tr)
878 struct ftrace_event_file *file;
880 list_for_each_entry(file, &tr->events, list) {
881 if (file->system != dir)
883 __free_subsystem_filter(file);
887 static int filter_add_pred(struct filter_parse_state *ps,
888 struct event_filter *filter,
889 struct filter_pred *pred,
890 struct pred_stack *stack)
894 if (WARN_ON(filter->n_preds == filter->a_preds)) {
895 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
899 err = filter_set_pred(filter, filter->n_preds, stack, pred);
908 int filter_assign_type(const char *type)
910 if (strstr(type, "__data_loc") && strstr(type, "char"))
911 return FILTER_DYN_STRING;
913 if (strchr(type, '[') && strstr(type, "char"))
914 return FILTER_STATIC_STRING;
919 static bool is_function_field(struct ftrace_event_field *field)
921 return field->filter_type == FILTER_TRACE_FN;
924 static bool is_string_field(struct ftrace_event_field *field)
926 return field->filter_type == FILTER_DYN_STRING ||
927 field->filter_type == FILTER_STATIC_STRING ||
928 field->filter_type == FILTER_PTR_STRING;
931 static int is_legal_op(struct ftrace_event_field *field, int op)
933 if (is_string_field(field) &&
934 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
936 if (!is_string_field(field) && op == OP_GLOB)
942 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
945 filter_pred_fn_t fn = NULL;
947 switch (field_size) {
949 if (op == OP_EQ || op == OP_NE)
951 else if (field_is_signed)
952 fn = filter_pred_s64;
954 fn = filter_pred_u64;
957 if (op == OP_EQ || op == OP_NE)
959 else if (field_is_signed)
960 fn = filter_pred_s32;
962 fn = filter_pred_u32;
965 if (op == OP_EQ || op == OP_NE)
967 else if (field_is_signed)
968 fn = filter_pred_s16;
970 fn = filter_pred_u16;
973 if (op == OP_EQ || op == OP_NE)
975 else if (field_is_signed)
985 static int init_pred(struct filter_parse_state *ps,
986 struct ftrace_event_field *field,
987 struct filter_pred *pred)
990 filter_pred_fn_t fn = filter_pred_none;
991 unsigned long long val;
994 pred->offset = field->offset;
996 if (!is_legal_op(field, pred->op)) {
997 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
1001 if (is_string_field(field)) {
1002 filter_build_regex(pred);
1004 if (field->filter_type == FILTER_STATIC_STRING) {
1005 fn = filter_pred_string;
1006 pred->regex.field_len = field->size;
1007 } else if (field->filter_type == FILTER_DYN_STRING)
1008 fn = filter_pred_strloc;
1010 fn = filter_pred_pchar;
1011 } else if (is_function_field(field)) {
1012 if (strcmp(field->name, "ip")) {
1013 parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
1017 if (field->is_signed)
1018 ret = kstrtoll(pred->regex.pattern, 0, &val);
1020 ret = kstrtoull(pred->regex.pattern, 0, &val);
1022 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
1027 fn = select_comparison_fn(pred->op, field->size,
1030 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1035 if (pred->op == OP_NE)
1042 static void parse_init(struct filter_parse_state *ps,
1043 struct filter_op *ops,
1046 memset(ps, '\0', sizeof(*ps));
1048 ps->infix.string = infix_string;
1049 ps->infix.cnt = strlen(infix_string);
1052 INIT_LIST_HEAD(&ps->opstack);
1053 INIT_LIST_HEAD(&ps->postfix);
1056 static char infix_next(struct filter_parse_state *ps)
1060 return ps->infix.string[ps->infix.tail++];
1063 static char infix_peek(struct filter_parse_state *ps)
1065 if (ps->infix.tail == strlen(ps->infix.string))
1068 return ps->infix.string[ps->infix.tail];
1071 static void infix_advance(struct filter_parse_state *ps)
1077 static inline int is_precedence_lower(struct filter_parse_state *ps,
1080 return ps->ops[a].precedence < ps->ops[b].precedence;
1083 static inline int is_op_char(struct filter_parse_state *ps, char c)
1087 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1088 if (ps->ops[i].string[0] == c)
1095 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1097 char nextc = infix_peek(ps);
1105 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1106 if (!strcmp(opstr, ps->ops[i].string)) {
1108 return ps->ops[i].id;
1114 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1115 if (!strcmp(opstr, ps->ops[i].string))
1116 return ps->ops[i].id;
1122 static inline void clear_operand_string(struct filter_parse_state *ps)
1124 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1125 ps->operand.tail = 0;
1128 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1130 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1133 ps->operand.string[ps->operand.tail++] = c;
1138 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1140 struct opstack_op *opstack_op;
1142 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1146 opstack_op->op = op;
1147 list_add(&opstack_op->list, &ps->opstack);
1152 static int filter_opstack_empty(struct filter_parse_state *ps)
1154 return list_empty(&ps->opstack);
1157 static int filter_opstack_top(struct filter_parse_state *ps)
1159 struct opstack_op *opstack_op;
1161 if (filter_opstack_empty(ps))
1164 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1166 return opstack_op->op;
1169 static int filter_opstack_pop(struct filter_parse_state *ps)
1171 struct opstack_op *opstack_op;
1174 if (filter_opstack_empty(ps))
1177 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1178 op = opstack_op->op;
1179 list_del(&opstack_op->list);
1186 static void filter_opstack_clear(struct filter_parse_state *ps)
1188 while (!filter_opstack_empty(ps))
1189 filter_opstack_pop(ps);
1192 static char *curr_operand(struct filter_parse_state *ps)
1194 return ps->operand.string;
1197 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1199 struct postfix_elt *elt;
1201 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1206 elt->operand = kstrdup(operand, GFP_KERNEL);
1207 if (!elt->operand) {
1212 list_add_tail(&elt->list, &ps->postfix);
1217 static int postfix_append_op(struct filter_parse_state *ps, int op)
1219 struct postfix_elt *elt;
1221 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1226 elt->operand = NULL;
1228 list_add_tail(&elt->list, &ps->postfix);
1233 static void postfix_clear(struct filter_parse_state *ps)
1235 struct postfix_elt *elt;
1237 while (!list_empty(&ps->postfix)) {
1238 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1239 list_del(&elt->list);
1240 kfree(elt->operand);
1245 static int filter_parse(struct filter_parse_state *ps)
1251 while ((ch = infix_next(ps))) {
1263 if (is_op_char(ps, ch)) {
1264 op = infix_get_op(ps, ch);
1265 if (op == OP_NONE) {
1266 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1270 if (strlen(curr_operand(ps))) {
1271 postfix_append_operand(ps, curr_operand(ps));
1272 clear_operand_string(ps);
1275 while (!filter_opstack_empty(ps)) {
1276 top_op = filter_opstack_top(ps);
1277 if (!is_precedence_lower(ps, top_op, op)) {
1278 top_op = filter_opstack_pop(ps);
1279 postfix_append_op(ps, top_op);
1285 filter_opstack_push(ps, op);
1290 filter_opstack_push(ps, OP_OPEN_PAREN);
1295 if (strlen(curr_operand(ps))) {
1296 postfix_append_operand(ps, curr_operand(ps));
1297 clear_operand_string(ps);
1300 top_op = filter_opstack_pop(ps);
1301 while (top_op != OP_NONE) {
1302 if (top_op == OP_OPEN_PAREN)
1304 postfix_append_op(ps, top_op);
1305 top_op = filter_opstack_pop(ps);
1307 if (top_op == OP_NONE) {
1308 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1314 if (append_operand_char(ps, ch)) {
1315 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1320 if (strlen(curr_operand(ps)))
1321 postfix_append_operand(ps, curr_operand(ps));
1323 while (!filter_opstack_empty(ps)) {
1324 top_op = filter_opstack_pop(ps);
1325 if (top_op == OP_NONE)
1327 if (top_op == OP_OPEN_PAREN) {
1328 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1331 postfix_append_op(ps, top_op);
1337 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1338 struct ftrace_event_call *call,
1339 int op, char *operand1, char *operand2)
1341 struct ftrace_event_field *field;
1342 static struct filter_pred pred;
1344 memset(&pred, 0, sizeof(pred));
1347 if (op == OP_AND || op == OP_OR)
1350 if (!operand1 || !operand2) {
1351 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1355 field = trace_find_event_field(call, operand1);
1357 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1361 strcpy(pred.regex.pattern, operand2);
1362 pred.regex.len = strlen(pred.regex.pattern);
1364 return init_pred(ps, field, &pred) ? NULL : &pred;
1367 static int check_preds(struct filter_parse_state *ps)
1369 int n_normal_preds = 0, n_logical_preds = 0;
1370 struct postfix_elt *elt;
1372 list_for_each_entry(elt, &ps->postfix, list) {
1373 if (elt->op == OP_NONE)
1376 if (elt->op == OP_AND || elt->op == OP_OR) {
1383 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1384 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1391 static int count_preds(struct filter_parse_state *ps)
1393 struct postfix_elt *elt;
1396 list_for_each_entry(elt, &ps->postfix, list) {
1397 if (elt->op == OP_NONE)
1405 struct check_pred_data {
1410 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1411 int *err, void *data)
1413 struct check_pred_data *d = data;
1415 if (WARN_ON(d->count++ > d->max)) {
1417 return WALK_PRED_ABORT;
1419 return WALK_PRED_DEFAULT;
1423 * The tree is walked at filtering of an event. If the tree is not correctly
1424 * built, it may cause an infinite loop. Check here that the tree does
1427 static int check_pred_tree(struct event_filter *filter,
1428 struct filter_pred *root)
1430 struct check_pred_data data = {
1432 * The max that we can hit a node is three times.
1433 * Once going down, once coming up from left, and
1434 * once coming up from right. This is more than enough
1435 * since leafs are only hit a single time.
1437 .max = 3 * filter->n_preds,
1441 return walk_pred_tree(filter->preds, root,
1442 check_pred_tree_cb, &data);
1445 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1446 int *err, void *data)
1450 if ((move == MOVE_DOWN) &&
1451 (pred->left == FILTER_PRED_INVALID))
1454 return WALK_PRED_DEFAULT;
1457 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1461 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1466 struct fold_pred_data {
1467 struct filter_pred *root;
1472 static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1473 int *err, void *data)
1475 struct fold_pred_data *d = data;
1476 struct filter_pred *root = d->root;
1478 if (move != MOVE_DOWN)
1479 return WALK_PRED_DEFAULT;
1480 if (pred->left != FILTER_PRED_INVALID)
1481 return WALK_PRED_DEFAULT;
1483 if (WARN_ON(d->count == d->children)) {
1485 return WALK_PRED_ABORT;
1488 pred->index &= ~FILTER_PRED_FOLD;
1489 root->ops[d->count++] = pred->index;
1490 return WALK_PRED_DEFAULT;
1493 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1495 struct fold_pred_data data = {
1501 /* No need to keep the fold flag */
1502 root->index &= ~FILTER_PRED_FOLD;
1504 /* If the root is a leaf then do nothing */
1505 if (root->left == FILTER_PRED_INVALID)
1508 /* count the children */
1509 children = count_leafs(preds, &preds[root->left]);
1510 children += count_leafs(preds, &preds[root->right]);
1512 root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
1516 root->val = children;
1517 data.children = children;
1518 return walk_pred_tree(preds, root, fold_pred_cb, &data);
1521 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1522 int *err, void *data)
1524 struct filter_pred *preds = data;
1526 if (move != MOVE_DOWN)
1527 return WALK_PRED_DEFAULT;
1528 if (!(pred->index & FILTER_PRED_FOLD))
1529 return WALK_PRED_DEFAULT;
1531 *err = fold_pred(preds, pred);
1533 return WALK_PRED_ABORT;
1535 /* eveyrhing below is folded, continue with parent */
1536 return WALK_PRED_PARENT;
1540 * To optimize the processing of the ops, if we have several "ors" or
1541 * "ands" together, we can put them in an array and process them all
1542 * together speeding up the filter logic.
1544 static int fold_pred_tree(struct event_filter *filter,
1545 struct filter_pred *root)
1547 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1551 static int replace_preds(struct ftrace_event_call *call,
1552 struct event_filter *filter,
1553 struct filter_parse_state *ps,
1556 char *operand1 = NULL, *operand2 = NULL;
1557 struct filter_pred *pred;
1558 struct filter_pred *root;
1559 struct postfix_elt *elt;
1560 struct pred_stack stack = { }; /* init to NULL */
1564 n_preds = count_preds(ps);
1565 if (n_preds >= MAX_FILTER_PRED) {
1566 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1570 err = check_preds(ps);
1575 err = __alloc_pred_stack(&stack, n_preds);
1578 err = __alloc_preds(filter, n_preds);
1584 list_for_each_entry(elt, &ps->postfix, list) {
1585 if (elt->op == OP_NONE) {
1587 operand1 = elt->operand;
1589 operand2 = elt->operand;
1591 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1598 if (elt->op == OP_NOT) {
1599 if (!n_preds || operand1 || operand2) {
1600 parse_error(ps, FILT_ERR_ILLEGAL_NOT_OP, 0);
1605 filter->preds[n_preds - 1].not ^= 1;
1609 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1610 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1615 pred = create_pred(ps, call, elt->op, operand1, operand2);
1622 err = filter_add_pred(ps, filter, pred, &stack);
1627 operand1 = operand2 = NULL;
1631 /* We should have one item left on the stack */
1632 pred = __pop_pred_stack(&stack);
1635 /* This item is where we start from in matching */
1637 /* Make sure the stack is empty */
1638 pred = __pop_pred_stack(&stack);
1639 if (WARN_ON(pred)) {
1641 filter->root = NULL;
1644 err = check_pred_tree(filter, root);
1648 /* Optimize the tree */
1649 err = fold_pred_tree(filter, root);
1653 /* We don't set root until we know it works */
1655 filter->root = root;
1660 __free_pred_stack(&stack);
1664 static inline void event_set_filtered_flag(struct ftrace_event_file *file)
1666 struct ftrace_event_call *call = file->event_call;
1668 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1669 call->flags |= TRACE_EVENT_FL_FILTERED;
1671 file->flags |= FTRACE_EVENT_FL_FILTERED;
1674 static inline void event_set_filter(struct ftrace_event_file *file,
1675 struct event_filter *filter)
1677 struct ftrace_event_call *call = file->event_call;
1679 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1680 rcu_assign_pointer(call->filter, filter);
1682 rcu_assign_pointer(file->filter, filter);
1685 static inline void event_clear_filter(struct ftrace_event_file *file)
1687 struct ftrace_event_call *call = file->event_call;
1689 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1690 RCU_INIT_POINTER(call->filter, NULL);
1692 RCU_INIT_POINTER(file->filter, NULL);
1696 event_set_no_set_filter_flag(struct ftrace_event_file *file)
1698 struct ftrace_event_call *call = file->event_call;
1700 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1701 call->flags |= TRACE_EVENT_FL_NO_SET_FILTER;
1703 file->flags |= FTRACE_EVENT_FL_NO_SET_FILTER;
1707 event_clear_no_set_filter_flag(struct ftrace_event_file *file)
1709 struct ftrace_event_call *call = file->event_call;
1711 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1712 call->flags &= ~TRACE_EVENT_FL_NO_SET_FILTER;
1714 file->flags &= ~FTRACE_EVENT_FL_NO_SET_FILTER;
1718 event_no_set_filter_flag(struct ftrace_event_file *file)
1720 struct ftrace_event_call *call = file->event_call;
1722 if (file->flags & FTRACE_EVENT_FL_NO_SET_FILTER)
1725 if ((call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) &&
1726 (call->flags & TRACE_EVENT_FL_NO_SET_FILTER))
1732 struct filter_list {
1733 struct list_head list;
1734 struct event_filter *filter;
1737 static int replace_system_preds(struct ftrace_subsystem_dir *dir,
1738 struct trace_array *tr,
1739 struct filter_parse_state *ps,
1740 char *filter_string)
1742 struct ftrace_event_file *file;
1743 struct filter_list *filter_item;
1744 struct filter_list *tmp;
1745 LIST_HEAD(filter_list);
1749 list_for_each_entry(file, &tr->events, list) {
1750 if (file->system != dir)
1754 * Try to see if the filter can be applied
1755 * (filter arg is ignored on dry_run)
1757 err = replace_preds(file->event_call, NULL, ps, true);
1759 event_set_no_set_filter_flag(file);
1761 event_clear_no_set_filter_flag(file);
1764 list_for_each_entry(file, &tr->events, list) {
1765 struct event_filter *filter;
1767 if (file->system != dir)
1770 if (event_no_set_filter_flag(file))
1773 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1777 list_add_tail(&filter_item->list, &filter_list);
1779 filter_item->filter = __alloc_filter();
1780 if (!filter_item->filter)
1782 filter = filter_item->filter;
1784 /* Can only fail on no memory */
1785 err = replace_filter_string(filter, filter_string);
1789 err = replace_preds(file->event_call, filter, ps, false);
1791 filter_disable(file);
1792 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1793 append_filter_err(ps, filter);
1795 event_set_filtered_flag(file);
1797 * Regardless of if this returned an error, we still
1798 * replace the filter for the call.
1800 filter = event_filter(file);
1801 event_set_filter(file, filter_item->filter);
1802 filter_item->filter = filter;
1811 * The calls can still be using the old filters.
1812 * Do a synchronize_sched() to ensure all calls are
1813 * done with them before we free them.
1815 synchronize_sched();
1816 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1817 __free_filter(filter_item->filter);
1818 list_del(&filter_item->list);
1823 /* No call succeeded */
1824 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1825 list_del(&filter_item->list);
1828 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1831 /* If any call succeeded, we still need to sync */
1833 synchronize_sched();
1834 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1835 __free_filter(filter_item->filter);
1836 list_del(&filter_item->list);
1842 static int create_filter_start(char *filter_str, bool set_str,
1843 struct filter_parse_state **psp,
1844 struct event_filter **filterp)
1846 struct event_filter *filter;
1847 struct filter_parse_state *ps = NULL;
1850 WARN_ON_ONCE(*psp || *filterp);
1852 /* allocate everything, and if any fails, free all and fail */
1853 filter = __alloc_filter();
1854 if (filter && set_str)
1855 err = replace_filter_string(filter, filter_str);
1857 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1859 if (!filter || !ps || err) {
1861 __free_filter(filter);
1865 /* we're committed to creating a new filter */
1869 parse_init(ps, filter_ops, filter_str);
1870 err = filter_parse(ps);
1872 append_filter_err(ps, filter);
1876 static void create_filter_finish(struct filter_parse_state *ps)
1879 filter_opstack_clear(ps);
1886 * create_filter - create a filter for a ftrace_event_call
1887 * @call: ftrace_event_call to create a filter for
1888 * @filter_str: filter string
1889 * @set_str: remember @filter_str and enable detailed error in filter
1890 * @filterp: out param for created filter (always updated on return)
1892 * Creates a filter for @call with @filter_str. If @set_str is %true,
1893 * @filter_str is copied and recorded in the new filter.
1895 * On success, returns 0 and *@filterp points to the new filter. On
1896 * failure, returns -errno and *@filterp may point to %NULL or to a new
1897 * filter. In the latter case, the returned filter contains error
1898 * information if @set_str is %true and the caller is responsible for
1901 static int create_filter(struct ftrace_event_call *call,
1902 char *filter_str, bool set_str,
1903 struct event_filter **filterp)
1905 struct event_filter *filter = NULL;
1906 struct filter_parse_state *ps = NULL;
1909 err = create_filter_start(filter_str, set_str, &ps, &filter);
1911 err = replace_preds(call, filter, ps, false);
1913 append_filter_err(ps, filter);
1915 create_filter_finish(ps);
1921 int create_event_filter(struct ftrace_event_call *call,
1922 char *filter_str, bool set_str,
1923 struct event_filter **filterp)
1925 return create_filter(call, filter_str, set_str, filterp);
1929 * create_system_filter - create a filter for an event_subsystem
1930 * @system: event_subsystem to create a filter for
1931 * @filter_str: filter string
1932 * @filterp: out param for created filter (always updated on return)
1934 * Identical to create_filter() except that it creates a subsystem filter
1935 * and always remembers @filter_str.
1937 static int create_system_filter(struct ftrace_subsystem_dir *dir,
1938 struct trace_array *tr,
1939 char *filter_str, struct event_filter **filterp)
1941 struct event_filter *filter = NULL;
1942 struct filter_parse_state *ps = NULL;
1945 err = create_filter_start(filter_str, true, &ps, &filter);
1947 err = replace_system_preds(dir, tr, ps, filter_str);
1949 /* System filters just show a default message */
1950 kfree(filter->filter_string);
1951 filter->filter_string = NULL;
1953 append_filter_err(ps, filter);
1956 create_filter_finish(ps);
1962 /* caller must hold event_mutex */
1963 int apply_event_filter(struct ftrace_event_file *file, char *filter_string)
1965 struct ftrace_event_call *call = file->event_call;
1966 struct event_filter *filter;
1969 if (!strcmp(strstrip(filter_string), "0")) {
1970 filter_disable(file);
1971 filter = event_filter(file);
1976 event_clear_filter(file);
1978 /* Make sure the filter is not being used */
1979 synchronize_sched();
1980 __free_filter(filter);
1985 err = create_filter(call, filter_string, true, &filter);
1988 * Always swap the call filter with the new filter
1989 * even if there was an error. If there was an error
1990 * in the filter, we disable the filter and show the error
1994 struct event_filter *tmp;
1996 tmp = event_filter(file);
1998 event_set_filtered_flag(file);
2000 filter_disable(file);
2002 event_set_filter(file, filter);
2005 /* Make sure the call is done with the filter */
2006 synchronize_sched();
2014 int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
2015 char *filter_string)
2017 struct event_subsystem *system = dir->subsystem;
2018 struct trace_array *tr = dir->tr;
2019 struct event_filter *filter;
2022 mutex_lock(&event_mutex);
2024 /* Make sure the system still has events */
2025 if (!dir->nr_events) {
2030 if (!strcmp(strstrip(filter_string), "0")) {
2031 filter_free_subsystem_preds(dir, tr);
2032 remove_filter_string(system->filter);
2033 filter = system->filter;
2034 system->filter = NULL;
2035 /* Ensure all filters are no longer used */
2036 synchronize_sched();
2037 filter_free_subsystem_filters(dir, tr);
2038 __free_filter(filter);
2042 err = create_system_filter(dir, tr, filter_string, &filter);
2045 * No event actually uses the system filter
2046 * we can free it without synchronize_sched().
2048 __free_filter(system->filter);
2049 system->filter = filter;
2052 mutex_unlock(&event_mutex);
2057 #ifdef CONFIG_PERF_EVENTS
2059 void ftrace_profile_free_filter(struct perf_event *event)
2061 struct event_filter *filter = event->filter;
2063 event->filter = NULL;
2064 __free_filter(filter);
2067 struct function_filter_data {
2068 struct ftrace_ops *ops;
2073 #ifdef CONFIG_FUNCTION_TRACER
2075 ftrace_function_filter_re(char *buf, int len, int *count)
2077 char *str, *sep, **re;
2079 str = kstrndup(buf, len, GFP_KERNEL);
2084 * The argv_split function takes white space
2085 * as a separator, so convert ',' into spaces.
2087 while ((sep = strchr(str, ',')))
2090 re = argv_split(GFP_KERNEL, str, count);
2095 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2096 int reset, char *re, int len)
2101 ret = ftrace_set_filter(ops, re, len, reset);
2103 ret = ftrace_set_notrace(ops, re, len, reset);
2108 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2109 struct function_filter_data *data)
2111 int i, re_cnt, ret = -EINVAL;
2115 reset = filter ? &data->first_filter : &data->first_notrace;
2118 * The 'ip' field could have multiple filters set, separated
2119 * either by space or comma. We first cut the filter and apply
2120 * all pieces separatelly.
2122 re = ftrace_function_filter_re(buf, len, &re_cnt);
2126 for (i = 0; i < re_cnt; i++) {
2127 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2128 re[i], strlen(re[i]));
2140 static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
2142 struct ftrace_event_field *field = pred->field;
2146 * Check the leaf predicate for function trace, verify:
2147 * - only '==' and '!=' is used
2148 * - the 'ip' field is used
2150 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2153 if (strcmp(field->name, "ip"))
2157 * Check the non leaf predicate for function trace, verify:
2158 * - only '||' is used
2160 if (pred->op != OP_OR)
2167 static int ftrace_function_set_filter_cb(enum move_type move,
2168 struct filter_pred *pred,
2169 int *err, void *data)
2171 /* Checking the node is valid for function trace. */
2172 if ((move != MOVE_DOWN) ||
2173 (pred->left != FILTER_PRED_INVALID)) {
2174 *err = ftrace_function_check_pred(pred, 0);
2176 *err = ftrace_function_check_pred(pred, 1);
2178 return WALK_PRED_ABORT;
2180 *err = __ftrace_function_set_filter(pred->op == OP_EQ,
2181 pred->regex.pattern,
2186 return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
2189 static int ftrace_function_set_filter(struct perf_event *event,
2190 struct event_filter *filter)
2192 struct function_filter_data data = {
2195 .ops = &event->ftrace_ops,
2198 return walk_pred_tree(filter->preds, filter->root,
2199 ftrace_function_set_filter_cb, &data);
2202 static int ftrace_function_set_filter(struct perf_event *event,
2203 struct event_filter *filter)
2207 #endif /* CONFIG_FUNCTION_TRACER */
2209 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2213 struct event_filter *filter;
2214 struct ftrace_event_call *call;
2216 mutex_lock(&event_mutex);
2218 call = event->tp_event;
2228 err = create_filter(call, filter_str, false, &filter);
2232 if (ftrace_event_is_function(call))
2233 err = ftrace_function_set_filter(event, filter);
2235 event->filter = filter;
2238 if (err || ftrace_event_is_function(call))
2239 __free_filter(filter);
2242 mutex_unlock(&event_mutex);
2247 #endif /* CONFIG_PERF_EVENTS */
2249 #ifdef CONFIG_FTRACE_STARTUP_TEST
2251 #include <linux/types.h>
2252 #include <linux/tracepoint.h>
2254 #define CREATE_TRACE_POINTS
2255 #include "trace_events_filter_test.h"
2257 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2260 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2261 .e = ve, .f = vf, .g = vg, .h = vh }, \
2263 .not_visited = nvisit, \
2268 static struct test_filter_data_t {
2270 struct ftrace_raw_ftrace_test_filter rec;
2273 } test_filter_data[] = {
2274 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2275 "e == 1 && f == 1 && g == 1 && h == 1"
2276 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2277 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2278 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2280 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2281 "e == 1 || f == 1 || g == 1 || h == 1"
2282 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2283 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2284 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2286 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2287 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2288 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2289 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2290 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2291 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2293 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2294 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2295 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2296 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2297 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2299 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2300 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2301 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2302 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2303 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2305 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2306 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2307 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2308 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2309 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2311 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2312 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2313 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2314 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2315 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2317 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2318 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2319 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2320 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2321 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2329 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2331 static int test_pred_visited;
2333 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2335 struct ftrace_event_field *field = pred->field;
2337 test_pred_visited = 1;
2338 printk(KERN_INFO "\npred visited %s\n", field->name);
2342 static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2343 int *err, void *data)
2345 char *fields = data;
2347 if ((move == MOVE_DOWN) &&
2348 (pred->left == FILTER_PRED_INVALID)) {
2349 struct ftrace_event_field *field = pred->field;
2352 WARN(1, "all leafs should have field defined");
2353 return WALK_PRED_DEFAULT;
2355 if (!strchr(fields, *field->name))
2356 return WALK_PRED_DEFAULT;
2359 pred->fn = test_pred_visited_fn;
2361 return WALK_PRED_DEFAULT;
2364 static __init int ftrace_test_event_filter(void)
2368 printk(KERN_INFO "Testing ftrace filter: ");
2370 for (i = 0; i < DATA_CNT; i++) {
2371 struct event_filter *filter = NULL;
2372 struct test_filter_data_t *d = &test_filter_data[i];
2375 err = create_filter(&event_ftrace_test_filter, d->filter,
2379 "Failed to get filter for '%s', err %d\n",
2381 __free_filter(filter);
2386 * The preemption disabling is not really needed for self
2387 * tests, but the rcu dereference will complain without it.
2390 if (*d->not_visited)
2391 walk_pred_tree(filter->preds, filter->root,
2395 test_pred_visited = 0;
2396 err = filter_match_preds(filter, &d->rec);
2399 __free_filter(filter);
2401 if (test_pred_visited) {
2403 "Failed, unwanted pred visited for filter %s\n",
2408 if (err != d->match) {
2410 "Failed to match filter '%s', expected %d\n",
2411 d->filter, d->match);
2417 printk(KERN_CONT "OK\n");
2422 late_initcall(ftrace_test_event_filter);
2424 #endif /* CONFIG_FTRACE_STARTUP_TEST */