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"
57 static struct filter_op filter_ops
[] = {
67 { OP_NONE
, "OP_NONE", 0 },
68 { OP_OPEN_PAREN
, "(", 0 },
74 FILT_ERR_UNBALANCED_PAREN
,
75 FILT_ERR_TOO_MANY_OPERANDS
,
76 FILT_ERR_OPERAND_TOO_LONG
,
77 FILT_ERR_FIELD_NOT_FOUND
,
78 FILT_ERR_ILLEGAL_FIELD_OP
,
79 FILT_ERR_ILLEGAL_INTVAL
,
80 FILT_ERR_BAD_SUBSYS_FILTER
,
81 FILT_ERR_TOO_MANY_PREDS
,
82 FILT_ERR_MISSING_FIELD
,
83 FILT_ERR_INVALID_FILTER
,
84 FILT_ERR_IP_FIELD_ONLY
,
87 static char *err_text
[] = {
94 "Illegal operation for field type",
95 "Illegal integer value",
96 "Couldn't find or set field in one of a subsystem's events",
97 "Too many terms in predicate expression",
98 "Missing field name and/or value",
99 "Meaningless filter expression",
100 "Only 'ip' field is supported for function trace",
105 struct list_head list
;
111 struct list_head list
;
114 struct filter_parse_state
{
115 struct filter_op
*ops
;
116 struct list_head opstack
;
117 struct list_head postfix
;
128 char string
[MAX_FILTER_STR_VAL
];
135 struct filter_pred
**preds
;
139 #define DEFINE_COMPARISON_PRED(type) \
140 static int filter_pred_##type(struct filter_pred *pred, void *event) \
142 type *addr = (type *)(event + pred->offset); \
143 type val = (type)pred->val; \
146 switch (pred->op) { \
148 match = (*addr < val); \
151 match = (*addr <= val); \
154 match = (*addr > val); \
157 match = (*addr >= val); \
166 #define DEFINE_EQUALITY_PRED(size) \
167 static int filter_pred_##size(struct filter_pred *pred, void *event) \
169 u##size *addr = (u##size *)(event + pred->offset); \
170 u##size val = (u##size)pred->val; \
173 match = (val == *addr) ^ pred->not; \
178 DEFINE_COMPARISON_PRED(s64
);
179 DEFINE_COMPARISON_PRED(u64
);
180 DEFINE_COMPARISON_PRED(s32
);
181 DEFINE_COMPARISON_PRED(u32
);
182 DEFINE_COMPARISON_PRED(s16
);
183 DEFINE_COMPARISON_PRED(u16
);
184 DEFINE_COMPARISON_PRED(s8
);
185 DEFINE_COMPARISON_PRED(u8
);
187 DEFINE_EQUALITY_PRED(64);
188 DEFINE_EQUALITY_PRED(32);
189 DEFINE_EQUALITY_PRED(16);
190 DEFINE_EQUALITY_PRED(8);
192 /* Filter predicate for fixed sized arrays of characters */
193 static int filter_pred_string(struct filter_pred
*pred
, void *event
)
195 char *addr
= (char *)(event
+ pred
->offset
);
198 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
200 match
= cmp
^ pred
->not;
205 /* Filter predicate for char * pointers */
206 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
)
208 char **addr
= (char **)(event
+ pred
->offset
);
210 int len
= strlen(*addr
) + 1; /* including tailing '\0' */
212 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, len
);
214 match
= cmp
^ pred
->not;
220 * Filter predicate for dynamic sized arrays of characters.
221 * These are implemented through a list of strings at the end
223 * Also each of these strings have a field in the entry which
224 * contains its offset from the beginning of the entry.
225 * We have then first to get this field, dereference it
226 * and add it to the address of the entry, and at last we have
227 * the address of the string.
229 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
)
231 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
232 int str_loc
= str_item
& 0xffff;
233 int str_len
= str_item
>> 16;
234 char *addr
= (char *)(event
+ str_loc
);
237 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
239 match
= cmp
^ pred
->not;
244 static int filter_pred_none(struct filter_pred
*pred
, void *event
)
250 * regex_match_foo - Basic regex callbacks
252 * @str: the string to be searched
253 * @r: the regex structure containing the pattern string
254 * @len: the length of the string to be searched (including '\0')
257 * - @str might not be NULL-terminated if it's of type DYN_STRING
261 static int regex_match_full(char *str
, struct regex
*r
, int len
)
263 if (strncmp(str
, r
->pattern
, len
) == 0)
268 static int regex_match_front(char *str
, struct regex
*r
, int len
)
270 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
275 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
277 if (strnstr(str
, r
->pattern
, len
))
282 static int regex_match_end(char *str
, struct regex
*r
, int len
)
284 int strlen
= len
- 1;
286 if (strlen
>= r
->len
&&
287 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
293 * filter_parse_regex - parse a basic regex
294 * @buff: the raw regex
295 * @len: length of the regex
296 * @search: will point to the beginning of the string to compare
297 * @not: tell whether the match will have to be inverted
299 * This passes in a buffer containing a regex and this function will
300 * set search to point to the search part of the buffer and
301 * return the type of search it is (see enum above).
302 * This does modify buff.
305 * search returns the pointer to use for comparison.
306 * not returns 1 if buff started with a '!'
309 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
311 int type
= MATCH_FULL
;
314 if (buff
[0] == '!') {
323 for (i
= 0; i
< len
; i
++) {
324 if (buff
[i
] == '*') {
327 type
= MATCH_END_ONLY
;
329 if (type
== MATCH_END_ONLY
)
330 type
= MATCH_MIDDLE_ONLY
;
332 type
= MATCH_FRONT_ONLY
;
342 static void filter_build_regex(struct filter_pred
*pred
)
344 struct regex
*r
= &pred
->regex
;
346 enum regex_type type
= MATCH_FULL
;
349 if (pred
->op
== OP_GLOB
) {
350 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
351 r
->len
= strlen(search
);
352 memmove(r
->pattern
, search
, r
->len
+1);
357 r
->match
= regex_match_full
;
359 case MATCH_FRONT_ONLY
:
360 r
->match
= regex_match_front
;
362 case MATCH_MIDDLE_ONLY
:
363 r
->match
= regex_match_middle
;
366 r
->match
= regex_match_end
;
379 static struct filter_pred
*
380 get_pred_parent(struct filter_pred
*pred
, struct filter_pred
*preds
,
381 int index
, enum move_type
*move
)
383 if (pred
->parent
& FILTER_PRED_IS_RIGHT
)
384 *move
= MOVE_UP_FROM_RIGHT
;
386 *move
= MOVE_UP_FROM_LEFT
;
387 pred
= &preds
[pred
->parent
& ~FILTER_PRED_IS_RIGHT
];
398 typedef int (*filter_pred_walkcb_t
) (enum move_type move
,
399 struct filter_pred
*pred
,
400 int *err
, void *data
);
402 static int walk_pred_tree(struct filter_pred
*preds
,
403 struct filter_pred
*root
,
404 filter_pred_walkcb_t cb
, void *data
)
406 struct filter_pred
*pred
= root
;
407 enum move_type move
= MOVE_DOWN
;
416 ret
= cb(move
, pred
, &err
, data
);
417 if (ret
== WALK_PRED_ABORT
)
419 if (ret
== WALK_PRED_PARENT
)
424 if (pred
->left
!= FILTER_PRED_INVALID
) {
425 pred
= &preds
[pred
->left
];
429 case MOVE_UP_FROM_LEFT
:
430 pred
= &preds
[pred
->right
];
433 case MOVE_UP_FROM_RIGHT
:
437 pred
= get_pred_parent(pred
, preds
,
450 * A series of AND or ORs where found together. Instead of
451 * climbing up and down the tree branches, an array of the
452 * ops were made in order of checks. We can just move across
453 * the array and short circuit if needed.
455 static int process_ops(struct filter_pred
*preds
,
456 struct filter_pred
*op
, void *rec
)
458 struct filter_pred
*pred
;
464 * Micro-optimization: We set type to true if op
465 * is an OR and false otherwise (AND). Then we
466 * just need to test if the match is equal to
467 * the type, and if it is, we can short circuit the
468 * rest of the checks:
470 * if ((match && op->op == OP_OR) ||
471 * (!match && op->op == OP_AND))
474 type
= op
->op
== OP_OR
;
476 for (i
= 0; i
< op
->val
; i
++) {
477 pred
= &preds
[op
->ops
[i
]];
478 if (!WARN_ON_ONCE(!pred
->fn
))
479 match
= pred
->fn(pred
, rec
);
486 struct filter_match_preds_data
{
487 struct filter_pred
*preds
;
492 static int filter_match_preds_cb(enum move_type move
, struct filter_pred
*pred
,
493 int *err
, void *data
)
495 struct filter_match_preds_data
*d
= data
;
500 /* only AND and OR have children */
501 if (pred
->left
!= FILTER_PRED_INVALID
) {
502 /* If ops is set, then it was folded. */
504 return WALK_PRED_DEFAULT
;
505 /* We can treat folded ops as a leaf node */
506 d
->match
= process_ops(d
->preds
, pred
, d
->rec
);
508 if (!WARN_ON_ONCE(!pred
->fn
))
509 d
->match
= pred
->fn(pred
, d
->rec
);
512 return WALK_PRED_PARENT
;
513 case MOVE_UP_FROM_LEFT
:
515 * Check for short circuits.
517 * Optimization: !!match == (pred->op == OP_OR)
519 * if ((match && pred->op == OP_OR) ||
520 * (!match && pred->op == OP_AND))
522 if (!!d
->match
== (pred
->op
== OP_OR
))
523 return WALK_PRED_PARENT
;
525 case MOVE_UP_FROM_RIGHT
:
529 return WALK_PRED_DEFAULT
;
532 /* return 1 if event matches, 0 otherwise (discard) */
533 int filter_match_preds(struct event_filter
*filter
, void *rec
)
535 struct filter_pred
*preds
;
536 struct filter_pred
*root
;
537 struct filter_match_preds_data data
= {
538 /* match is currently meaningless */
544 /* no filter is considered a match */
548 n_preds
= filter
->n_preds
;
553 * n_preds, root and filter->preds are protect with preemption disabled.
555 root
= rcu_dereference_sched(filter
->root
);
559 data
.preds
= preds
= rcu_dereference_sched(filter
->preds
);
560 ret
= walk_pred_tree(preds
, root
, filter_match_preds_cb
, &data
);
564 EXPORT_SYMBOL_GPL(filter_match_preds
);
566 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
569 ps
->lasterr_pos
= pos
;
572 static void remove_filter_string(struct event_filter
*filter
)
577 kfree(filter
->filter_string
);
578 filter
->filter_string
= NULL
;
581 static int replace_filter_string(struct event_filter
*filter
,
584 kfree(filter
->filter_string
);
585 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
586 if (!filter
->filter_string
)
592 static int append_filter_string(struct event_filter
*filter
,
596 char *new_filter_string
;
598 BUG_ON(!filter
->filter_string
);
599 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
600 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
601 if (!new_filter_string
)
604 strcpy(new_filter_string
, filter
->filter_string
);
605 strcat(new_filter_string
, string
);
606 kfree(filter
->filter_string
);
607 filter
->filter_string
= new_filter_string
;
612 static void append_filter_err(struct filter_parse_state
*ps
,
613 struct event_filter
*filter
)
615 int pos
= ps
->lasterr_pos
;
618 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
622 append_filter_string(filter
, "\n");
623 memset(buf
, ' ', PAGE_SIZE
);
624 if (pos
> PAGE_SIZE
- 128)
627 pbuf
= &buf
[pos
] + 1;
629 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
630 append_filter_string(filter
, buf
);
631 free_page((unsigned long) buf
);
634 /* caller must hold event_mutex */
635 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
637 struct event_filter
*filter
= call
->filter
;
639 if (filter
&& filter
->filter_string
)
640 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
642 trace_seq_printf(s
, "none\n");
645 void print_subsystem_event_filter(struct event_subsystem
*system
,
648 struct event_filter
*filter
;
650 mutex_lock(&event_mutex
);
651 filter
= system
->filter
;
652 if (filter
&& filter
->filter_string
)
653 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
655 trace_seq_printf(s
, DEFAULT_SYS_FILTER_MESSAGE
"\n");
656 mutex_unlock(&event_mutex
);
659 static int __alloc_pred_stack(struct pred_stack
*stack
, int n_preds
)
661 stack
->preds
= kcalloc(n_preds
+ 1, sizeof(*stack
->preds
), GFP_KERNEL
);
664 stack
->index
= n_preds
;
668 static void __free_pred_stack(struct pred_stack
*stack
)
674 static int __push_pred_stack(struct pred_stack
*stack
,
675 struct filter_pred
*pred
)
677 int index
= stack
->index
;
679 if (WARN_ON(index
== 0))
682 stack
->preds
[--index
] = pred
;
683 stack
->index
= index
;
687 static struct filter_pred
*
688 __pop_pred_stack(struct pred_stack
*stack
)
690 struct filter_pred
*pred
;
691 int index
= stack
->index
;
693 pred
= stack
->preds
[index
++];
697 stack
->index
= index
;
701 static int filter_set_pred(struct event_filter
*filter
,
703 struct pred_stack
*stack
,
704 struct filter_pred
*src
)
706 struct filter_pred
*dest
= &filter
->preds
[idx
];
707 struct filter_pred
*left
;
708 struct filter_pred
*right
;
713 if (dest
->op
== OP_OR
|| dest
->op
== OP_AND
) {
714 right
= __pop_pred_stack(stack
);
715 left
= __pop_pred_stack(stack
);
719 * If both children can be folded
720 * and they are the same op as this op or a leaf,
721 * then this op can be folded.
723 if (left
->index
& FILTER_PRED_FOLD
&&
724 (left
->op
== dest
->op
||
725 left
->left
== FILTER_PRED_INVALID
) &&
726 right
->index
& FILTER_PRED_FOLD
&&
727 (right
->op
== dest
->op
||
728 right
->left
== FILTER_PRED_INVALID
))
729 dest
->index
|= FILTER_PRED_FOLD
;
731 dest
->left
= left
->index
& ~FILTER_PRED_FOLD
;
732 dest
->right
= right
->index
& ~FILTER_PRED_FOLD
;
733 left
->parent
= dest
->index
& ~FILTER_PRED_FOLD
;
734 right
->parent
= dest
->index
| FILTER_PRED_IS_RIGHT
;
737 * Make dest->left invalid to be used as a quick
738 * way to know this is a leaf node.
740 dest
->left
= FILTER_PRED_INVALID
;
742 /* All leafs allow folding the parent ops. */
743 dest
->index
|= FILTER_PRED_FOLD
;
746 return __push_pred_stack(stack
, dest
);
749 static void __free_preds(struct event_filter
*filter
)
754 for (i
= 0; i
< filter
->n_preds
; i
++)
755 kfree(filter
->preds
[i
].ops
);
756 kfree(filter
->preds
);
757 filter
->preds
= NULL
;
763 static void filter_disable(struct ftrace_event_call
*call
)
765 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
768 static void __free_filter(struct event_filter
*filter
)
773 __free_preds(filter
);
774 kfree(filter
->filter_string
);
779 * Called when destroying the ftrace_event_call.
780 * The call is being freed, so we do not need to worry about
781 * the call being currently used. This is for module code removing
782 * the tracepoints from within it.
784 void destroy_preds(struct ftrace_event_call
*call
)
786 __free_filter(call
->filter
);
790 static struct event_filter
*__alloc_filter(void)
792 struct event_filter
*filter
;
794 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
798 static int __alloc_preds(struct event_filter
*filter
, int n_preds
)
800 struct filter_pred
*pred
;
804 __free_preds(filter
);
806 filter
->preds
= kcalloc(n_preds
, sizeof(*filter
->preds
), GFP_KERNEL
);
811 filter
->a_preds
= n_preds
;
814 for (i
= 0; i
< n_preds
; i
++) {
815 pred
= &filter
->preds
[i
];
816 pred
->fn
= filter_pred_none
;
822 static void filter_free_subsystem_preds(struct event_subsystem
*system
)
824 struct ftrace_event_call
*call
;
826 list_for_each_entry(call
, &ftrace_events
, list
) {
827 if (strcmp(call
->class->system
, system
->name
) != 0)
830 filter_disable(call
);
831 remove_filter_string(call
->filter
);
835 static void filter_free_subsystem_filters(struct event_subsystem
*system
)
837 struct ftrace_event_call
*call
;
839 list_for_each_entry(call
, &ftrace_events
, list
) {
840 if (strcmp(call
->class->system
, system
->name
) != 0)
842 __free_filter(call
->filter
);
847 static int filter_add_pred(struct filter_parse_state
*ps
,
848 struct event_filter
*filter
,
849 struct filter_pred
*pred
,
850 struct pred_stack
*stack
)
854 if (WARN_ON(filter
->n_preds
== filter
->a_preds
)) {
855 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
859 err
= filter_set_pred(filter
, filter
->n_preds
, stack
, pred
);
868 int filter_assign_type(const char *type
)
870 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
871 return FILTER_DYN_STRING
;
873 if (strchr(type
, '[') && strstr(type
, "char"))
874 return FILTER_STATIC_STRING
;
879 static bool is_function_field(struct ftrace_event_field
*field
)
881 return field
->filter_type
== FILTER_TRACE_FN
;
884 static bool is_string_field(struct ftrace_event_field
*field
)
886 return field
->filter_type
== FILTER_DYN_STRING
||
887 field
->filter_type
== FILTER_STATIC_STRING
||
888 field
->filter_type
== FILTER_PTR_STRING
;
891 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
893 if (is_string_field(field
) &&
894 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
896 if (!is_string_field(field
) && op
== OP_GLOB
)
902 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
905 filter_pred_fn_t fn
= NULL
;
907 switch (field_size
) {
909 if (op
== OP_EQ
|| op
== OP_NE
)
911 else if (field_is_signed
)
912 fn
= filter_pred_s64
;
914 fn
= filter_pred_u64
;
917 if (op
== OP_EQ
|| op
== OP_NE
)
919 else if (field_is_signed
)
920 fn
= filter_pred_s32
;
922 fn
= filter_pred_u32
;
925 if (op
== OP_EQ
|| op
== OP_NE
)
927 else if (field_is_signed
)
928 fn
= filter_pred_s16
;
930 fn
= filter_pred_u16
;
933 if (op
== OP_EQ
|| op
== OP_NE
)
935 else if (field_is_signed
)
945 static int init_pred(struct filter_parse_state
*ps
,
946 struct ftrace_event_field
*field
,
947 struct filter_pred
*pred
)
950 filter_pred_fn_t fn
= filter_pred_none
;
951 unsigned long long val
;
954 pred
->offset
= field
->offset
;
956 if (!is_legal_op(field
, pred
->op
)) {
957 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
961 if (is_string_field(field
)) {
962 filter_build_regex(pred
);
964 if (field
->filter_type
== FILTER_STATIC_STRING
) {
965 fn
= filter_pred_string
;
966 pred
->regex
.field_len
= field
->size
;
967 } else if (field
->filter_type
== FILTER_DYN_STRING
)
968 fn
= filter_pred_strloc
;
970 fn
= filter_pred_pchar
;
971 } else if (is_function_field(field
)) {
972 if (strcmp(field
->name
, "ip")) {
973 parse_error(ps
, FILT_ERR_IP_FIELD_ONLY
, 0);
977 if (field
->is_signed
)
978 ret
= kstrtoll(pred
->regex
.pattern
, 0, &val
);
980 ret
= kstrtoull(pred
->regex
.pattern
, 0, &val
);
982 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
987 fn
= select_comparison_fn(pred
->op
, field
->size
,
990 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
995 if (pred
->op
== OP_NE
)
1002 static void parse_init(struct filter_parse_state
*ps
,
1003 struct filter_op
*ops
,
1006 memset(ps
, '\0', sizeof(*ps
));
1008 ps
->infix
.string
= infix_string
;
1009 ps
->infix
.cnt
= strlen(infix_string
);
1012 INIT_LIST_HEAD(&ps
->opstack
);
1013 INIT_LIST_HEAD(&ps
->postfix
);
1016 static char infix_next(struct filter_parse_state
*ps
)
1023 return ps
->infix
.string
[ps
->infix
.tail
++];
1026 static char infix_peek(struct filter_parse_state
*ps
)
1028 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
1031 return ps
->infix
.string
[ps
->infix
.tail
];
1034 static void infix_advance(struct filter_parse_state
*ps
)
1043 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
1046 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
1049 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
1053 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1054 if (ps
->ops
[i
].string
[0] == c
)
1061 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
1063 char nextc
= infix_peek(ps
);
1071 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1072 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
1074 return ps
->ops
[i
].id
;
1080 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1081 if (!strcmp(opstr
, ps
->ops
[i
].string
))
1082 return ps
->ops
[i
].id
;
1088 static inline void clear_operand_string(struct filter_parse_state
*ps
)
1090 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
1091 ps
->operand
.tail
= 0;
1094 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
1096 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
1099 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
1104 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
1106 struct opstack_op
*opstack_op
;
1108 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
1112 opstack_op
->op
= op
;
1113 list_add(&opstack_op
->list
, &ps
->opstack
);
1118 static int filter_opstack_empty(struct filter_parse_state
*ps
)
1120 return list_empty(&ps
->opstack
);
1123 static int filter_opstack_top(struct filter_parse_state
*ps
)
1125 struct opstack_op
*opstack_op
;
1127 if (filter_opstack_empty(ps
))
1130 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1132 return opstack_op
->op
;
1135 static int filter_opstack_pop(struct filter_parse_state
*ps
)
1137 struct opstack_op
*opstack_op
;
1140 if (filter_opstack_empty(ps
))
1143 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1144 op
= opstack_op
->op
;
1145 list_del(&opstack_op
->list
);
1152 static void filter_opstack_clear(struct filter_parse_state
*ps
)
1154 while (!filter_opstack_empty(ps
))
1155 filter_opstack_pop(ps
);
1158 static char *curr_operand(struct filter_parse_state
*ps
)
1160 return ps
->operand
.string
;
1163 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
1165 struct postfix_elt
*elt
;
1167 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1172 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1173 if (!elt
->operand
) {
1178 list_add_tail(&elt
->list
, &ps
->postfix
);
1183 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1185 struct postfix_elt
*elt
;
1187 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1192 elt
->operand
= NULL
;
1194 list_add_tail(&elt
->list
, &ps
->postfix
);
1199 static void postfix_clear(struct filter_parse_state
*ps
)
1201 struct postfix_elt
*elt
;
1203 while (!list_empty(&ps
->postfix
)) {
1204 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1205 list_del(&elt
->list
);
1206 kfree(elt
->operand
);
1211 static int filter_parse(struct filter_parse_state
*ps
)
1217 while ((ch
= infix_next(ps
))) {
1229 if (is_op_char(ps
, ch
)) {
1230 op
= infix_get_op(ps
, ch
);
1231 if (op
== OP_NONE
) {
1232 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1236 if (strlen(curr_operand(ps
))) {
1237 postfix_append_operand(ps
, curr_operand(ps
));
1238 clear_operand_string(ps
);
1241 while (!filter_opstack_empty(ps
)) {
1242 top_op
= filter_opstack_top(ps
);
1243 if (!is_precedence_lower(ps
, top_op
, op
)) {
1244 top_op
= filter_opstack_pop(ps
);
1245 postfix_append_op(ps
, top_op
);
1251 filter_opstack_push(ps
, op
);
1256 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1261 if (strlen(curr_operand(ps
))) {
1262 postfix_append_operand(ps
, curr_operand(ps
));
1263 clear_operand_string(ps
);
1266 top_op
= filter_opstack_pop(ps
);
1267 while (top_op
!= OP_NONE
) {
1268 if (top_op
== OP_OPEN_PAREN
)
1270 postfix_append_op(ps
, top_op
);
1271 top_op
= filter_opstack_pop(ps
);
1273 if (top_op
== OP_NONE
) {
1274 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1280 if (append_operand_char(ps
, ch
)) {
1281 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1286 if (strlen(curr_operand(ps
)))
1287 postfix_append_operand(ps
, curr_operand(ps
));
1289 while (!filter_opstack_empty(ps
)) {
1290 top_op
= filter_opstack_pop(ps
);
1291 if (top_op
== OP_NONE
)
1293 if (top_op
== OP_OPEN_PAREN
) {
1294 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1297 postfix_append_op(ps
, top_op
);
1303 static struct filter_pred
*create_pred(struct filter_parse_state
*ps
,
1304 struct ftrace_event_call
*call
,
1305 int op
, char *operand1
, char *operand2
)
1307 struct ftrace_event_field
*field
;
1308 static struct filter_pred pred
;
1310 memset(&pred
, 0, sizeof(pred
));
1313 if (op
== OP_AND
|| op
== OP_OR
)
1316 if (!operand1
|| !operand2
) {
1317 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1321 field
= trace_find_event_field(call
, operand1
);
1323 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
1327 strcpy(pred
.regex
.pattern
, operand2
);
1328 pred
.regex
.len
= strlen(pred
.regex
.pattern
);
1330 return init_pred(ps
, field
, &pred
) ? NULL
: &pred
;
1333 static int check_preds(struct filter_parse_state
*ps
)
1335 int n_normal_preds
= 0, n_logical_preds
= 0;
1336 struct postfix_elt
*elt
;
1339 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1340 if (elt
->op
== OP_NONE
) {
1346 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1351 /* all ops should have operands */
1356 if (cnt
!= 1 || !n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1357 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1364 static int count_preds(struct filter_parse_state
*ps
)
1366 struct postfix_elt
*elt
;
1369 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1370 if (elt
->op
== OP_NONE
)
1378 struct check_pred_data
{
1383 static int check_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1384 int *err
, void *data
)
1386 struct check_pred_data
*d
= data
;
1388 if (WARN_ON(d
->count
++ > d
->max
)) {
1390 return WALK_PRED_ABORT
;
1392 return WALK_PRED_DEFAULT
;
1396 * The tree is walked at filtering of an event. If the tree is not correctly
1397 * built, it may cause an infinite loop. Check here that the tree does
1400 static int check_pred_tree(struct event_filter
*filter
,
1401 struct filter_pred
*root
)
1403 struct check_pred_data data
= {
1405 * The max that we can hit a node is three times.
1406 * Once going down, once coming up from left, and
1407 * once coming up from right. This is more than enough
1408 * since leafs are only hit a single time.
1410 .max
= 3 * filter
->n_preds
,
1414 return walk_pred_tree(filter
->preds
, root
,
1415 check_pred_tree_cb
, &data
);
1418 static int count_leafs_cb(enum move_type move
, struct filter_pred
*pred
,
1419 int *err
, void *data
)
1423 if ((move
== MOVE_DOWN
) &&
1424 (pred
->left
== FILTER_PRED_INVALID
))
1427 return WALK_PRED_DEFAULT
;
1430 static int count_leafs(struct filter_pred
*preds
, struct filter_pred
*root
)
1434 ret
= walk_pred_tree(preds
, root
, count_leafs_cb
, &count
);
1439 struct fold_pred_data
{
1440 struct filter_pred
*root
;
1445 static int fold_pred_cb(enum move_type move
, struct filter_pred
*pred
,
1446 int *err
, void *data
)
1448 struct fold_pred_data
*d
= data
;
1449 struct filter_pred
*root
= d
->root
;
1451 if (move
!= MOVE_DOWN
)
1452 return WALK_PRED_DEFAULT
;
1453 if (pred
->left
!= FILTER_PRED_INVALID
)
1454 return WALK_PRED_DEFAULT
;
1456 if (WARN_ON(d
->count
== d
->children
)) {
1458 return WALK_PRED_ABORT
;
1461 pred
->index
&= ~FILTER_PRED_FOLD
;
1462 root
->ops
[d
->count
++] = pred
->index
;
1463 return WALK_PRED_DEFAULT
;
1466 static int fold_pred(struct filter_pred
*preds
, struct filter_pred
*root
)
1468 struct fold_pred_data data
= {
1474 /* No need to keep the fold flag */
1475 root
->index
&= ~FILTER_PRED_FOLD
;
1477 /* If the root is a leaf then do nothing */
1478 if (root
->left
== FILTER_PRED_INVALID
)
1481 /* count the children */
1482 children
= count_leafs(preds
, &preds
[root
->left
]);
1483 children
+= count_leafs(preds
, &preds
[root
->right
]);
1485 root
->ops
= kcalloc(children
, sizeof(*root
->ops
), GFP_KERNEL
);
1489 root
->val
= children
;
1490 data
.children
= children
;
1491 return walk_pred_tree(preds
, root
, fold_pred_cb
, &data
);
1494 static int fold_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1495 int *err
, void *data
)
1497 struct filter_pred
*preds
= data
;
1499 if (move
!= MOVE_DOWN
)
1500 return WALK_PRED_DEFAULT
;
1501 if (!(pred
->index
& FILTER_PRED_FOLD
))
1502 return WALK_PRED_DEFAULT
;
1504 *err
= fold_pred(preds
, pred
);
1506 return WALK_PRED_ABORT
;
1508 /* eveyrhing below is folded, continue with parent */
1509 return WALK_PRED_PARENT
;
1513 * To optimize the processing of the ops, if we have several "ors" or
1514 * "ands" together, we can put them in an array and process them all
1515 * together speeding up the filter logic.
1517 static int fold_pred_tree(struct event_filter
*filter
,
1518 struct filter_pred
*root
)
1520 return walk_pred_tree(filter
->preds
, root
, fold_pred_tree_cb
,
1524 static int replace_preds(struct ftrace_event_call
*call
,
1525 struct event_filter
*filter
,
1526 struct filter_parse_state
*ps
,
1527 char *filter_string
,
1530 char *operand1
= NULL
, *operand2
= NULL
;
1531 struct filter_pred
*pred
;
1532 struct filter_pred
*root
;
1533 struct postfix_elt
*elt
;
1534 struct pred_stack stack
= { }; /* init to NULL */
1538 n_preds
= count_preds(ps
);
1539 if (n_preds
>= MAX_FILTER_PRED
) {
1540 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1544 err
= check_preds(ps
);
1549 err
= __alloc_pred_stack(&stack
, n_preds
);
1552 err
= __alloc_preds(filter
, n_preds
);
1558 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1559 if (elt
->op
== OP_NONE
) {
1561 operand1
= elt
->operand
;
1563 operand2
= elt
->operand
;
1565 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1572 if (WARN_ON(n_preds
++ == MAX_FILTER_PRED
)) {
1573 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1578 pred
= create_pred(ps
, call
, elt
->op
, operand1
, operand2
);
1585 err
= filter_add_pred(ps
, filter
, pred
, &stack
);
1590 operand1
= operand2
= NULL
;
1594 /* We should have one item left on the stack */
1595 pred
= __pop_pred_stack(&stack
);
1598 /* This item is where we start from in matching */
1600 /* Make sure the stack is empty */
1601 pred
= __pop_pred_stack(&stack
);
1602 if (WARN_ON(pred
)) {
1604 filter
->root
= NULL
;
1607 err
= check_pred_tree(filter
, root
);
1611 /* Optimize the tree */
1612 err
= fold_pred_tree(filter
, root
);
1616 /* We don't set root until we know it works */
1618 filter
->root
= root
;
1623 __free_pred_stack(&stack
);
1627 struct filter_list
{
1628 struct list_head list
;
1629 struct event_filter
*filter
;
1632 static int replace_system_preds(struct event_subsystem
*system
,
1633 struct filter_parse_state
*ps
,
1634 char *filter_string
)
1636 struct ftrace_event_call
*call
;
1637 struct filter_list
*filter_item
;
1638 struct filter_list
*tmp
;
1639 LIST_HEAD(filter_list
);
1643 list_for_each_entry(call
, &ftrace_events
, list
) {
1645 if (strcmp(call
->class->system
, system
->name
) != 0)
1649 * Try to see if the filter can be applied
1650 * (filter arg is ignored on dry_run)
1652 err
= replace_preds(call
, NULL
, ps
, filter_string
, true);
1654 call
->flags
|= TRACE_EVENT_FL_NO_SET_FILTER
;
1656 call
->flags
&= ~TRACE_EVENT_FL_NO_SET_FILTER
;
1659 list_for_each_entry(call
, &ftrace_events
, list
) {
1660 struct event_filter
*filter
;
1662 if (strcmp(call
->class->system
, system
->name
) != 0)
1665 if (call
->flags
& TRACE_EVENT_FL_NO_SET_FILTER
)
1668 filter_item
= kzalloc(sizeof(*filter_item
), GFP_KERNEL
);
1672 list_add_tail(&filter_item
->list
, &filter_list
);
1674 filter_item
->filter
= __alloc_filter();
1675 if (!filter_item
->filter
)
1677 filter
= filter_item
->filter
;
1679 /* Can only fail on no memory */
1680 err
= replace_filter_string(filter
, filter_string
);
1684 err
= replace_preds(call
, filter
, ps
, filter_string
, false);
1686 filter_disable(call
);
1687 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1688 append_filter_err(ps
, filter
);
1690 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1692 * Regardless of if this returned an error, we still
1693 * replace the filter for the call.
1695 filter
= call
->filter
;
1696 rcu_assign_pointer(call
->filter
, filter_item
->filter
);
1697 filter_item
->filter
= filter
;
1706 * The calls can still be using the old filters.
1707 * Do a synchronize_sched() to ensure all calls are
1708 * done with them before we free them.
1710 synchronize_sched();
1711 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1712 __free_filter(filter_item
->filter
);
1713 list_del(&filter_item
->list
);
1718 /* No call succeeded */
1719 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1720 list_del(&filter_item
->list
);
1723 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1726 /* If any call succeeded, we still need to sync */
1728 synchronize_sched();
1729 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1730 __free_filter(filter_item
->filter
);
1731 list_del(&filter_item
->list
);
1737 static int create_filter_start(char *filter_str
, bool set_str
,
1738 struct filter_parse_state
**psp
,
1739 struct event_filter
**filterp
)
1741 struct event_filter
*filter
;
1742 struct filter_parse_state
*ps
= NULL
;
1745 WARN_ON_ONCE(*psp
|| *filterp
);
1747 /* allocate everything, and if any fails, free all and fail */
1748 filter
= __alloc_filter();
1749 if (filter
&& set_str
)
1750 err
= replace_filter_string(filter
, filter_str
);
1752 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1754 if (!filter
|| !ps
|| err
) {
1756 __free_filter(filter
);
1760 /* we're committed to creating a new filter */
1764 parse_init(ps
, filter_ops
, filter_str
);
1765 err
= filter_parse(ps
);
1767 append_filter_err(ps
, filter
);
1771 static void create_filter_finish(struct filter_parse_state
*ps
)
1774 filter_opstack_clear(ps
);
1781 * create_filter - create a filter for a ftrace_event_call
1782 * @call: ftrace_event_call to create a filter for
1783 * @filter_str: filter string
1784 * @set_str: remember @filter_str and enable detailed error in filter
1785 * @filterp: out param for created filter (always updated on return)
1787 * Creates a filter for @call with @filter_str. If @set_str is %true,
1788 * @filter_str is copied and recorded in the new filter.
1790 * On success, returns 0 and *@filterp points to the new filter. On
1791 * failure, returns -errno and *@filterp may point to %NULL or to a new
1792 * filter. In the latter case, the returned filter contains error
1793 * information if @set_str is %true and the caller is responsible for
1796 static int create_filter(struct ftrace_event_call
*call
,
1797 char *filter_str
, bool set_str
,
1798 struct event_filter
**filterp
)
1800 struct event_filter
*filter
= NULL
;
1801 struct filter_parse_state
*ps
= NULL
;
1804 err
= create_filter_start(filter_str
, set_str
, &ps
, &filter
);
1806 err
= replace_preds(call
, filter
, ps
, filter_str
, false);
1808 append_filter_err(ps
, filter
);
1810 create_filter_finish(ps
);
1817 * create_system_filter - create a filter for an event_subsystem
1818 * @system: event_subsystem to create a filter for
1819 * @filter_str: filter string
1820 * @filterp: out param for created filter (always updated on return)
1822 * Identical to create_filter() except that it creates a subsystem filter
1823 * and always remembers @filter_str.
1825 static int create_system_filter(struct event_subsystem
*system
,
1826 char *filter_str
, struct event_filter
**filterp
)
1828 struct event_filter
*filter
= NULL
;
1829 struct filter_parse_state
*ps
= NULL
;
1832 err
= create_filter_start(filter_str
, true, &ps
, &filter
);
1834 err
= replace_system_preds(system
, ps
, filter_str
);
1836 /* System filters just show a default message */
1837 kfree(filter
->filter_string
);
1838 filter
->filter_string
= NULL
;
1840 append_filter_err(ps
, filter
);
1843 create_filter_finish(ps
);
1849 /* caller must hold event_mutex */
1850 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1852 struct event_filter
*filter
;
1855 if (!strcmp(strstrip(filter_string
), "0")) {
1856 filter_disable(call
);
1857 filter
= call
->filter
;
1860 RCU_INIT_POINTER(call
->filter
, NULL
);
1861 /* Make sure the filter is not being used */
1862 synchronize_sched();
1863 __free_filter(filter
);
1867 err
= create_filter(call
, filter_string
, true, &filter
);
1870 * Always swap the call filter with the new filter
1871 * even if there was an error. If there was an error
1872 * in the filter, we disable the filter and show the error
1876 struct event_filter
*tmp
= call
->filter
;
1879 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1881 filter_disable(call
);
1883 rcu_assign_pointer(call
->filter
, filter
);
1886 /* Make sure the call is done with the filter */
1887 synchronize_sched();
1895 int apply_subsystem_event_filter(struct ftrace_subsystem_dir
*dir
,
1896 char *filter_string
)
1898 struct event_subsystem
*system
= dir
->subsystem
;
1899 struct event_filter
*filter
;
1902 mutex_lock(&event_mutex
);
1904 /* Make sure the system still has events */
1905 if (!dir
->nr_events
) {
1910 if (!strcmp(strstrip(filter_string
), "0")) {
1911 filter_free_subsystem_preds(system
);
1912 remove_filter_string(system
->filter
);
1913 filter
= system
->filter
;
1914 system
->filter
= NULL
;
1915 /* Ensure all filters are no longer used */
1916 synchronize_sched();
1917 filter_free_subsystem_filters(system
);
1918 __free_filter(filter
);
1922 err
= create_system_filter(system
, filter_string
, &filter
);
1925 * No event actually uses the system filter
1926 * we can free it without synchronize_sched().
1928 __free_filter(system
->filter
);
1929 system
->filter
= filter
;
1932 mutex_unlock(&event_mutex
);
1937 #ifdef CONFIG_PERF_EVENTS
1939 void ftrace_profile_free_filter(struct perf_event
*event
)
1941 struct event_filter
*filter
= event
->filter
;
1943 event
->filter
= NULL
;
1944 __free_filter(filter
);
1947 struct function_filter_data
{
1948 struct ftrace_ops
*ops
;
1953 #ifdef CONFIG_FUNCTION_TRACER
1955 ftrace_function_filter_re(char *buf
, int len
, int *count
)
1957 char *str
, *sep
, **re
;
1959 str
= kstrndup(buf
, len
, GFP_KERNEL
);
1964 * The argv_split function takes white space
1965 * as a separator, so convert ',' into spaces.
1967 while ((sep
= strchr(str
, ',')))
1970 re
= argv_split(GFP_KERNEL
, str
, count
);
1975 static int ftrace_function_set_regexp(struct ftrace_ops
*ops
, int filter
,
1976 int reset
, char *re
, int len
)
1981 ret
= ftrace_set_filter(ops
, re
, len
, reset
);
1983 ret
= ftrace_set_notrace(ops
, re
, len
, reset
);
1988 static int __ftrace_function_set_filter(int filter
, char *buf
, int len
,
1989 struct function_filter_data
*data
)
1991 int i
, re_cnt
, ret
= -EINVAL
;
1995 reset
= filter
? &data
->first_filter
: &data
->first_notrace
;
1998 * The 'ip' field could have multiple filters set, separated
1999 * either by space or comma. We first cut the filter and apply
2000 * all pieces separatelly.
2002 re
= ftrace_function_filter_re(buf
, len
, &re_cnt
);
2006 for (i
= 0; i
< re_cnt
; i
++) {
2007 ret
= ftrace_function_set_regexp(data
->ops
, filter
, *reset
,
2008 re
[i
], strlen(re
[i
]));
2020 static int ftrace_function_check_pred(struct filter_pred
*pred
, int leaf
)
2022 struct ftrace_event_field
*field
= pred
->field
;
2026 * Check the leaf predicate for function trace, verify:
2027 * - only '==' and '!=' is used
2028 * - the 'ip' field is used
2030 if ((pred
->op
!= OP_EQ
) && (pred
->op
!= OP_NE
))
2033 if (strcmp(field
->name
, "ip"))
2037 * Check the non leaf predicate for function trace, verify:
2038 * - only '||' is used
2040 if (pred
->op
!= OP_OR
)
2047 static int ftrace_function_set_filter_cb(enum move_type move
,
2048 struct filter_pred
*pred
,
2049 int *err
, void *data
)
2051 /* Checking the node is valid for function trace. */
2052 if ((move
!= MOVE_DOWN
) ||
2053 (pred
->left
!= FILTER_PRED_INVALID
)) {
2054 *err
= ftrace_function_check_pred(pred
, 0);
2056 *err
= ftrace_function_check_pred(pred
, 1);
2058 return WALK_PRED_ABORT
;
2060 *err
= __ftrace_function_set_filter(pred
->op
== OP_EQ
,
2061 pred
->regex
.pattern
,
2066 return (*err
) ? WALK_PRED_ABORT
: WALK_PRED_DEFAULT
;
2069 static int ftrace_function_set_filter(struct perf_event
*event
,
2070 struct event_filter
*filter
)
2072 struct function_filter_data data
= {
2075 .ops
= &event
->ftrace_ops
,
2078 return walk_pred_tree(filter
->preds
, filter
->root
,
2079 ftrace_function_set_filter_cb
, &data
);
2082 static int ftrace_function_set_filter(struct perf_event
*event
,
2083 struct event_filter
*filter
)
2087 #endif /* CONFIG_FUNCTION_TRACER */
2089 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
2093 struct event_filter
*filter
;
2094 struct ftrace_event_call
*call
;
2096 mutex_lock(&event_mutex
);
2098 call
= event
->tp_event
;
2108 err
= create_filter(call
, filter_str
, false, &filter
);
2112 if (ftrace_event_is_function(call
))
2113 err
= ftrace_function_set_filter(event
, filter
);
2115 event
->filter
= filter
;
2118 if (err
|| ftrace_event_is_function(call
))
2119 __free_filter(filter
);
2122 mutex_unlock(&event_mutex
);
2127 #endif /* CONFIG_PERF_EVENTS */
2129 #ifdef CONFIG_FTRACE_STARTUP_TEST
2131 #include <linux/types.h>
2132 #include <linux/tracepoint.h>
2134 #define CREATE_TRACE_POINTS
2135 #include "trace_events_filter_test.h"
2137 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2140 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2141 .e = ve, .f = vf, .g = vg, .h = vh }, \
2143 .not_visited = nvisit, \
2148 static struct test_filter_data_t
{
2150 struct ftrace_raw_ftrace_test_filter rec
;
2153 } test_filter_data
[] = {
2154 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2155 "e == 1 && f == 1 && g == 1 && h == 1"
2156 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2157 DATA_REC(NO
, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2158 DATA_REC(NO
, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2160 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2161 "e == 1 || f == 1 || g == 1 || h == 1"
2162 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2163 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2164 DATA_REC(YES
, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2166 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2167 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2168 DATA_REC(NO
, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2169 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2170 DATA_REC(YES
, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2171 DATA_REC(NO
, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2173 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2174 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2175 DATA_REC(YES
, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2176 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2177 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2179 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2180 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2181 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2182 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2183 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2185 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2186 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2187 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2188 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2189 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2191 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2192 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2193 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2194 DATA_REC(NO
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2195 DATA_REC(NO
, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2197 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2198 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2199 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2200 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2201 DATA_REC(YES
, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2209 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2211 static int test_pred_visited
;
2213 static int test_pred_visited_fn(struct filter_pred
*pred
, void *event
)
2215 struct ftrace_event_field
*field
= pred
->field
;
2217 test_pred_visited
= 1;
2218 printk(KERN_INFO
"\npred visited %s\n", field
->name
);
2222 static int test_walk_pred_cb(enum move_type move
, struct filter_pred
*pred
,
2223 int *err
, void *data
)
2225 char *fields
= data
;
2227 if ((move
== MOVE_DOWN
) &&
2228 (pred
->left
== FILTER_PRED_INVALID
)) {
2229 struct ftrace_event_field
*field
= pred
->field
;
2232 WARN(1, "all leafs should have field defined");
2233 return WALK_PRED_DEFAULT
;
2235 if (!strchr(fields
, *field
->name
))
2236 return WALK_PRED_DEFAULT
;
2239 pred
->fn
= test_pred_visited_fn
;
2241 return WALK_PRED_DEFAULT
;
2244 static __init
int ftrace_test_event_filter(void)
2248 printk(KERN_INFO
"Testing ftrace filter: ");
2250 for (i
= 0; i
< DATA_CNT
; i
++) {
2251 struct event_filter
*filter
= NULL
;
2252 struct test_filter_data_t
*d
= &test_filter_data
[i
];
2255 err
= create_filter(&event_ftrace_test_filter
, d
->filter
,
2259 "Failed to get filter for '%s', err %d\n",
2261 __free_filter(filter
);
2266 * The preemption disabling is not really needed for self
2267 * tests, but the rcu dereference will complain without it.
2270 if (*d
->not_visited
)
2271 walk_pred_tree(filter
->preds
, filter
->root
,
2275 test_pred_visited
= 0;
2276 err
= filter_match_preds(filter
, &d
->rec
);
2279 __free_filter(filter
);
2281 if (test_pred_visited
) {
2283 "Failed, unwanted pred visited for filter %s\n",
2288 if (err
!= d
->match
) {
2290 "Failed to match filter '%s', expected %d\n",
2291 d
->filter
, d
->match
);
2297 printk(KERN_CONT
"OK\n");
2302 late_initcall(ftrace_test_event_filter
);
2304 #endif /* CONFIG_FTRACE_STARTUP_TEST */