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
);
494 /* If not of not match is equal to not of not, then it is a match */
495 return !!match
== !op
->not;
498 struct filter_match_preds_data
{
499 struct filter_pred
*preds
;
504 static int filter_match_preds_cb(enum move_type move
, struct filter_pred
*pred
,
505 int *err
, void *data
)
507 struct filter_match_preds_data
*d
= data
;
512 /* only AND and OR have children */
513 if (pred
->left
!= FILTER_PRED_INVALID
) {
514 /* If ops is set, then it was folded. */
516 return WALK_PRED_DEFAULT
;
517 /* We can treat folded ops as a leaf node */
518 d
->match
= process_ops(d
->preds
, pred
, d
->rec
);
520 if (!WARN_ON_ONCE(!pred
->fn
))
521 d
->match
= pred
->fn(pred
, d
->rec
);
524 return WALK_PRED_PARENT
;
525 case MOVE_UP_FROM_LEFT
:
527 * Check for short circuits.
529 * Optimization: !!match == (pred->op == OP_OR)
531 * if ((match && pred->op == OP_OR) ||
532 * (!match && pred->op == OP_AND))
534 if (!!d
->match
== (pred
->op
== OP_OR
))
535 return WALK_PRED_PARENT
;
537 case MOVE_UP_FROM_RIGHT
:
541 return WALK_PRED_DEFAULT
;
544 /* return 1 if event matches, 0 otherwise (discard) */
545 int filter_match_preds(struct event_filter
*filter
, void *rec
)
547 struct filter_pred
*preds
;
548 struct filter_pred
*root
;
549 struct filter_match_preds_data data
= {
550 /* match is currently meaningless */
556 /* no filter is considered a match */
560 n_preds
= filter
->n_preds
;
565 * n_preds, root and filter->preds are protect with preemption disabled.
567 root
= rcu_dereference_sched(filter
->root
);
571 data
.preds
= preds
= rcu_dereference_sched(filter
->preds
);
572 ret
= walk_pred_tree(preds
, root
, filter_match_preds_cb
, &data
);
576 EXPORT_SYMBOL_GPL(filter_match_preds
);
578 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
581 ps
->lasterr_pos
= pos
;
584 static void remove_filter_string(struct event_filter
*filter
)
589 kfree(filter
->filter_string
);
590 filter
->filter_string
= NULL
;
593 static int replace_filter_string(struct event_filter
*filter
,
596 kfree(filter
->filter_string
);
597 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
598 if (!filter
->filter_string
)
604 static int append_filter_string(struct event_filter
*filter
,
608 char *new_filter_string
;
610 BUG_ON(!filter
->filter_string
);
611 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
612 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
613 if (!new_filter_string
)
616 strcpy(new_filter_string
, filter
->filter_string
);
617 strcat(new_filter_string
, string
);
618 kfree(filter
->filter_string
);
619 filter
->filter_string
= new_filter_string
;
624 static void append_filter_err(struct filter_parse_state
*ps
,
625 struct event_filter
*filter
)
627 int pos
= ps
->lasterr_pos
;
630 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
634 append_filter_string(filter
, "\n");
635 memset(buf
, ' ', PAGE_SIZE
);
636 if (pos
> PAGE_SIZE
- 128)
639 pbuf
= &buf
[pos
] + 1;
641 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
642 append_filter_string(filter
, buf
);
643 free_page((unsigned long) buf
);
646 static inline struct event_filter
*event_filter(struct ftrace_event_file
*file
)
648 if (file
->event_call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
649 return file
->event_call
->filter
;
654 /* caller must hold event_mutex */
655 void print_event_filter(struct ftrace_event_file
*file
, struct trace_seq
*s
)
657 struct event_filter
*filter
= event_filter(file
);
659 if (filter
&& filter
->filter_string
)
660 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
662 trace_seq_puts(s
, "none\n");
665 void print_subsystem_event_filter(struct event_subsystem
*system
,
668 struct event_filter
*filter
;
670 mutex_lock(&event_mutex
);
671 filter
= system
->filter
;
672 if (filter
&& filter
->filter_string
)
673 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
675 trace_seq_puts(s
, DEFAULT_SYS_FILTER_MESSAGE
"\n");
676 mutex_unlock(&event_mutex
);
679 static int __alloc_pred_stack(struct pred_stack
*stack
, int n_preds
)
681 stack
->preds
= kcalloc(n_preds
+ 1, sizeof(*stack
->preds
), GFP_KERNEL
);
684 stack
->index
= n_preds
;
688 static void __free_pred_stack(struct pred_stack
*stack
)
694 static int __push_pred_stack(struct pred_stack
*stack
,
695 struct filter_pred
*pred
)
697 int index
= stack
->index
;
699 if (WARN_ON(index
== 0))
702 stack
->preds
[--index
] = pred
;
703 stack
->index
= index
;
707 static struct filter_pred
*
708 __pop_pred_stack(struct pred_stack
*stack
)
710 struct filter_pred
*pred
;
711 int index
= stack
->index
;
713 pred
= stack
->preds
[index
++];
717 stack
->index
= index
;
721 static int filter_set_pred(struct event_filter
*filter
,
723 struct pred_stack
*stack
,
724 struct filter_pred
*src
)
726 struct filter_pred
*dest
= &filter
->preds
[idx
];
727 struct filter_pred
*left
;
728 struct filter_pred
*right
;
733 if (dest
->op
== OP_OR
|| dest
->op
== OP_AND
) {
734 right
= __pop_pred_stack(stack
);
735 left
= __pop_pred_stack(stack
);
739 * If both children can be folded
740 * and they are the same op as this op or a leaf,
741 * then this op can be folded.
743 if (left
->index
& FILTER_PRED_FOLD
&&
744 ((left
->op
== dest
->op
&& !left
->not) ||
745 left
->left
== FILTER_PRED_INVALID
) &&
746 right
->index
& FILTER_PRED_FOLD
&&
747 ((right
->op
== dest
->op
&& !right
->not) ||
748 right
->left
== FILTER_PRED_INVALID
))
749 dest
->index
|= FILTER_PRED_FOLD
;
751 dest
->left
= left
->index
& ~FILTER_PRED_FOLD
;
752 dest
->right
= right
->index
& ~FILTER_PRED_FOLD
;
753 left
->parent
= dest
->index
& ~FILTER_PRED_FOLD
;
754 right
->parent
= dest
->index
| FILTER_PRED_IS_RIGHT
;
757 * Make dest->left invalid to be used as a quick
758 * way to know this is a leaf node.
760 dest
->left
= FILTER_PRED_INVALID
;
762 /* All leafs allow folding the parent ops. */
763 dest
->index
|= FILTER_PRED_FOLD
;
766 return __push_pred_stack(stack
, dest
);
769 static void __free_preds(struct event_filter
*filter
)
774 for (i
= 0; i
< filter
->n_preds
; i
++)
775 kfree(filter
->preds
[i
].ops
);
776 kfree(filter
->preds
);
777 filter
->preds
= NULL
;
783 static void filter_disable(struct ftrace_event_file
*file
)
785 struct ftrace_event_call
*call
= file
->event_call
;
787 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
788 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
790 file
->flags
&= ~FTRACE_EVENT_FL_FILTERED
;
793 static void __free_filter(struct event_filter
*filter
)
798 __free_preds(filter
);
799 kfree(filter
->filter_string
);
803 void free_event_filter(struct event_filter
*filter
)
805 __free_filter(filter
);
808 static struct event_filter
*__alloc_filter(void)
810 struct event_filter
*filter
;
812 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
816 static int __alloc_preds(struct event_filter
*filter
, int n_preds
)
818 struct filter_pred
*pred
;
822 __free_preds(filter
);
824 filter
->preds
= kcalloc(n_preds
, sizeof(*filter
->preds
), GFP_KERNEL
);
829 filter
->a_preds
= n_preds
;
832 for (i
= 0; i
< n_preds
; i
++) {
833 pred
= &filter
->preds
[i
];
834 pred
->fn
= filter_pred_none
;
840 static inline void __remove_filter(struct ftrace_event_file
*file
)
842 struct ftrace_event_call
*call
= file
->event_call
;
844 filter_disable(file
);
845 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
846 remove_filter_string(call
->filter
);
848 remove_filter_string(file
->filter
);
851 static void filter_free_subsystem_preds(struct ftrace_subsystem_dir
*dir
,
852 struct trace_array
*tr
)
854 struct ftrace_event_file
*file
;
856 list_for_each_entry(file
, &tr
->events
, list
) {
857 if (file
->system
!= dir
)
859 __remove_filter(file
);
863 static inline void __free_subsystem_filter(struct ftrace_event_file
*file
)
865 struct ftrace_event_call
*call
= file
->event_call
;
867 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
) {
868 __free_filter(call
->filter
);
871 __free_filter(file
->filter
);
876 static void filter_free_subsystem_filters(struct ftrace_subsystem_dir
*dir
,
877 struct trace_array
*tr
)
879 struct ftrace_event_file
*file
;
881 list_for_each_entry(file
, &tr
->events
, list
) {
882 if (file
->system
!= dir
)
884 __free_subsystem_filter(file
);
888 static int filter_add_pred(struct filter_parse_state
*ps
,
889 struct event_filter
*filter
,
890 struct filter_pred
*pred
,
891 struct pred_stack
*stack
)
895 if (WARN_ON(filter
->n_preds
== filter
->a_preds
)) {
896 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
900 err
= filter_set_pred(filter
, filter
->n_preds
, stack
, pred
);
909 int filter_assign_type(const char *type
)
911 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
912 return FILTER_DYN_STRING
;
914 if (strchr(type
, '[') && strstr(type
, "char"))
915 return FILTER_STATIC_STRING
;
920 static bool is_function_field(struct ftrace_event_field
*field
)
922 return field
->filter_type
== FILTER_TRACE_FN
;
925 static bool is_string_field(struct ftrace_event_field
*field
)
927 return field
->filter_type
== FILTER_DYN_STRING
||
928 field
->filter_type
== FILTER_STATIC_STRING
||
929 field
->filter_type
== FILTER_PTR_STRING
;
932 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
934 if (is_string_field(field
) &&
935 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
937 if (!is_string_field(field
) && op
== OP_GLOB
)
943 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
946 filter_pred_fn_t fn
= NULL
;
948 switch (field_size
) {
950 if (op
== OP_EQ
|| op
== OP_NE
)
952 else if (field_is_signed
)
953 fn
= filter_pred_s64
;
955 fn
= filter_pred_u64
;
958 if (op
== OP_EQ
|| op
== OP_NE
)
960 else if (field_is_signed
)
961 fn
= filter_pred_s32
;
963 fn
= filter_pred_u32
;
966 if (op
== OP_EQ
|| op
== OP_NE
)
968 else if (field_is_signed
)
969 fn
= filter_pred_s16
;
971 fn
= filter_pred_u16
;
974 if (op
== OP_EQ
|| op
== OP_NE
)
976 else if (field_is_signed
)
986 static int init_pred(struct filter_parse_state
*ps
,
987 struct ftrace_event_field
*field
,
988 struct filter_pred
*pred
)
991 filter_pred_fn_t fn
= filter_pred_none
;
992 unsigned long long val
;
995 pred
->offset
= field
->offset
;
997 if (!is_legal_op(field
, pred
->op
)) {
998 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
1002 if (is_string_field(field
)) {
1003 filter_build_regex(pred
);
1005 if (field
->filter_type
== FILTER_STATIC_STRING
) {
1006 fn
= filter_pred_string
;
1007 pred
->regex
.field_len
= field
->size
;
1008 } else if (field
->filter_type
== FILTER_DYN_STRING
)
1009 fn
= filter_pred_strloc
;
1011 fn
= filter_pred_pchar
;
1012 } else if (is_function_field(field
)) {
1013 if (strcmp(field
->name
, "ip")) {
1014 parse_error(ps
, FILT_ERR_IP_FIELD_ONLY
, 0);
1018 if (field
->is_signed
)
1019 ret
= kstrtoll(pred
->regex
.pattern
, 0, &val
);
1021 ret
= kstrtoull(pred
->regex
.pattern
, 0, &val
);
1023 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
1028 fn
= select_comparison_fn(pred
->op
, field
->size
,
1031 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1036 if (pred
->op
== OP_NE
)
1043 static void parse_init(struct filter_parse_state
*ps
,
1044 struct filter_op
*ops
,
1047 memset(ps
, '\0', sizeof(*ps
));
1049 ps
->infix
.string
= infix_string
;
1050 ps
->infix
.cnt
= strlen(infix_string
);
1053 INIT_LIST_HEAD(&ps
->opstack
);
1054 INIT_LIST_HEAD(&ps
->postfix
);
1057 static char infix_next(struct filter_parse_state
*ps
)
1061 return ps
->infix
.string
[ps
->infix
.tail
++];
1064 static char infix_peek(struct filter_parse_state
*ps
)
1066 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
1069 return ps
->infix
.string
[ps
->infix
.tail
];
1072 static void infix_advance(struct filter_parse_state
*ps
)
1078 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
1081 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
1084 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
1088 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1089 if (ps
->ops
[i
].string
[0] == c
)
1096 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
1098 char nextc
= infix_peek(ps
);
1106 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1107 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
1109 return ps
->ops
[i
].id
;
1115 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1116 if (!strcmp(opstr
, ps
->ops
[i
].string
))
1117 return ps
->ops
[i
].id
;
1123 static inline void clear_operand_string(struct filter_parse_state
*ps
)
1125 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
1126 ps
->operand
.tail
= 0;
1129 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
1131 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
1134 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
1139 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
1141 struct opstack_op
*opstack_op
;
1143 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
1147 opstack_op
->op
= op
;
1148 list_add(&opstack_op
->list
, &ps
->opstack
);
1153 static int filter_opstack_empty(struct filter_parse_state
*ps
)
1155 return list_empty(&ps
->opstack
);
1158 static int filter_opstack_top(struct filter_parse_state
*ps
)
1160 struct opstack_op
*opstack_op
;
1162 if (filter_opstack_empty(ps
))
1165 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1167 return opstack_op
->op
;
1170 static int filter_opstack_pop(struct filter_parse_state
*ps
)
1172 struct opstack_op
*opstack_op
;
1175 if (filter_opstack_empty(ps
))
1178 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1179 op
= opstack_op
->op
;
1180 list_del(&opstack_op
->list
);
1187 static void filter_opstack_clear(struct filter_parse_state
*ps
)
1189 while (!filter_opstack_empty(ps
))
1190 filter_opstack_pop(ps
);
1193 static char *curr_operand(struct filter_parse_state
*ps
)
1195 return ps
->operand
.string
;
1198 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
1200 struct postfix_elt
*elt
;
1202 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1207 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1208 if (!elt
->operand
) {
1213 list_add_tail(&elt
->list
, &ps
->postfix
);
1218 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1220 struct postfix_elt
*elt
;
1222 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1227 elt
->operand
= NULL
;
1229 list_add_tail(&elt
->list
, &ps
->postfix
);
1234 static void postfix_clear(struct filter_parse_state
*ps
)
1236 struct postfix_elt
*elt
;
1238 while (!list_empty(&ps
->postfix
)) {
1239 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1240 list_del(&elt
->list
);
1241 kfree(elt
->operand
);
1246 static int filter_parse(struct filter_parse_state
*ps
)
1252 while ((ch
= infix_next(ps
))) {
1264 if (is_op_char(ps
, ch
)) {
1265 op
= infix_get_op(ps
, ch
);
1266 if (op
== OP_NONE
) {
1267 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1271 if (strlen(curr_operand(ps
))) {
1272 postfix_append_operand(ps
, curr_operand(ps
));
1273 clear_operand_string(ps
);
1276 while (!filter_opstack_empty(ps
)) {
1277 top_op
= filter_opstack_top(ps
);
1278 if (!is_precedence_lower(ps
, top_op
, op
)) {
1279 top_op
= filter_opstack_pop(ps
);
1280 postfix_append_op(ps
, top_op
);
1286 filter_opstack_push(ps
, op
);
1291 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1296 if (strlen(curr_operand(ps
))) {
1297 postfix_append_operand(ps
, curr_operand(ps
));
1298 clear_operand_string(ps
);
1301 top_op
= filter_opstack_pop(ps
);
1302 while (top_op
!= OP_NONE
) {
1303 if (top_op
== OP_OPEN_PAREN
)
1305 postfix_append_op(ps
, top_op
);
1306 top_op
= filter_opstack_pop(ps
);
1308 if (top_op
== OP_NONE
) {
1309 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1315 if (append_operand_char(ps
, ch
)) {
1316 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1321 if (strlen(curr_operand(ps
)))
1322 postfix_append_operand(ps
, curr_operand(ps
));
1324 while (!filter_opstack_empty(ps
)) {
1325 top_op
= filter_opstack_pop(ps
);
1326 if (top_op
== OP_NONE
)
1328 if (top_op
== OP_OPEN_PAREN
) {
1329 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1332 postfix_append_op(ps
, top_op
);
1338 static struct filter_pred
*create_pred(struct filter_parse_state
*ps
,
1339 struct ftrace_event_call
*call
,
1340 int op
, char *operand1
, char *operand2
)
1342 struct ftrace_event_field
*field
;
1343 static struct filter_pred pred
;
1345 memset(&pred
, 0, sizeof(pred
));
1348 if (op
== OP_AND
|| op
== OP_OR
)
1351 if (!operand1
|| !operand2
) {
1352 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1356 field
= trace_find_event_field(call
, operand1
);
1358 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
1362 strcpy(pred
.regex
.pattern
, operand2
);
1363 pred
.regex
.len
= strlen(pred
.regex
.pattern
);
1365 return init_pred(ps
, field
, &pred
) ? NULL
: &pred
;
1368 static int check_preds(struct filter_parse_state
*ps
)
1370 int n_normal_preds
= 0, n_logical_preds
= 0;
1371 struct postfix_elt
*elt
;
1374 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1375 if (elt
->op
== OP_NONE
) {
1380 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1385 if (elt
->op
!= OP_NOT
)
1388 WARN_ON_ONCE(cnt
< 0);
1391 if (cnt
!= 1 || !n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1392 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1399 static int count_preds(struct filter_parse_state
*ps
)
1401 struct postfix_elt
*elt
;
1404 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1405 if (elt
->op
== OP_NONE
)
1413 struct check_pred_data
{
1418 static int check_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1419 int *err
, void *data
)
1421 struct check_pred_data
*d
= data
;
1423 if (WARN_ON(d
->count
++ > d
->max
)) {
1425 return WALK_PRED_ABORT
;
1427 return WALK_PRED_DEFAULT
;
1431 * The tree is walked at filtering of an event. If the tree is not correctly
1432 * built, it may cause an infinite loop. Check here that the tree does
1435 static int check_pred_tree(struct event_filter
*filter
,
1436 struct filter_pred
*root
)
1438 struct check_pred_data data
= {
1440 * The max that we can hit a node is three times.
1441 * Once going down, once coming up from left, and
1442 * once coming up from right. This is more than enough
1443 * since leafs are only hit a single time.
1445 .max
= 3 * filter
->n_preds
,
1449 return walk_pred_tree(filter
->preds
, root
,
1450 check_pred_tree_cb
, &data
);
1453 static int count_leafs_cb(enum move_type move
, struct filter_pred
*pred
,
1454 int *err
, void *data
)
1458 if ((move
== MOVE_DOWN
) &&
1459 (pred
->left
== FILTER_PRED_INVALID
))
1462 return WALK_PRED_DEFAULT
;
1465 static int count_leafs(struct filter_pred
*preds
, struct filter_pred
*root
)
1469 ret
= walk_pred_tree(preds
, root
, count_leafs_cb
, &count
);
1474 struct fold_pred_data
{
1475 struct filter_pred
*root
;
1480 static int fold_pred_cb(enum move_type move
, struct filter_pred
*pred
,
1481 int *err
, void *data
)
1483 struct fold_pred_data
*d
= data
;
1484 struct filter_pred
*root
= d
->root
;
1486 if (move
!= MOVE_DOWN
)
1487 return WALK_PRED_DEFAULT
;
1488 if (pred
->left
!= FILTER_PRED_INVALID
)
1489 return WALK_PRED_DEFAULT
;
1491 if (WARN_ON(d
->count
== d
->children
)) {
1493 return WALK_PRED_ABORT
;
1496 pred
->index
&= ~FILTER_PRED_FOLD
;
1497 root
->ops
[d
->count
++] = pred
->index
;
1498 return WALK_PRED_DEFAULT
;
1501 static int fold_pred(struct filter_pred
*preds
, struct filter_pred
*root
)
1503 struct fold_pred_data data
= {
1509 /* No need to keep the fold flag */
1510 root
->index
&= ~FILTER_PRED_FOLD
;
1512 /* If the root is a leaf then do nothing */
1513 if (root
->left
== FILTER_PRED_INVALID
)
1516 /* count the children */
1517 children
= count_leafs(preds
, &preds
[root
->left
]);
1518 children
+= count_leafs(preds
, &preds
[root
->right
]);
1520 root
->ops
= kcalloc(children
, sizeof(*root
->ops
), GFP_KERNEL
);
1524 root
->val
= children
;
1525 data
.children
= children
;
1526 return walk_pred_tree(preds
, root
, fold_pred_cb
, &data
);
1529 static int fold_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1530 int *err
, void *data
)
1532 struct filter_pred
*preds
= data
;
1534 if (move
!= MOVE_DOWN
)
1535 return WALK_PRED_DEFAULT
;
1536 if (!(pred
->index
& FILTER_PRED_FOLD
))
1537 return WALK_PRED_DEFAULT
;
1539 *err
= fold_pred(preds
, pred
);
1541 return WALK_PRED_ABORT
;
1543 /* eveyrhing below is folded, continue with parent */
1544 return WALK_PRED_PARENT
;
1548 * To optimize the processing of the ops, if we have several "ors" or
1549 * "ands" together, we can put them in an array and process them all
1550 * together speeding up the filter logic.
1552 static int fold_pred_tree(struct event_filter
*filter
,
1553 struct filter_pred
*root
)
1555 return walk_pred_tree(filter
->preds
, root
, fold_pred_tree_cb
,
1559 static int replace_preds(struct ftrace_event_call
*call
,
1560 struct event_filter
*filter
,
1561 struct filter_parse_state
*ps
,
1564 char *operand1
= NULL
, *operand2
= NULL
;
1565 struct filter_pred
*pred
;
1566 struct filter_pred
*root
;
1567 struct postfix_elt
*elt
;
1568 struct pred_stack stack
= { }; /* init to NULL */
1572 n_preds
= count_preds(ps
);
1573 if (n_preds
>= MAX_FILTER_PRED
) {
1574 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1578 err
= check_preds(ps
);
1583 err
= __alloc_pred_stack(&stack
, n_preds
);
1586 err
= __alloc_preds(filter
, n_preds
);
1592 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1593 if (elt
->op
== OP_NONE
) {
1595 operand1
= elt
->operand
;
1597 operand2
= elt
->operand
;
1599 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1606 if (elt
->op
== OP_NOT
) {
1607 if (!n_preds
|| operand1
|| operand2
) {
1608 parse_error(ps
, FILT_ERR_ILLEGAL_NOT_OP
, 0);
1613 filter
->preds
[n_preds
- 1].not ^= 1;
1617 if (WARN_ON(n_preds
++ == MAX_FILTER_PRED
)) {
1618 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1623 pred
= create_pred(ps
, call
, elt
->op
, operand1
, operand2
);
1630 err
= filter_add_pred(ps
, filter
, pred
, &stack
);
1635 operand1
= operand2
= NULL
;
1639 /* We should have one item left on the stack */
1640 pred
= __pop_pred_stack(&stack
);
1643 /* This item is where we start from in matching */
1645 /* Make sure the stack is empty */
1646 pred
= __pop_pred_stack(&stack
);
1647 if (WARN_ON(pred
)) {
1649 filter
->root
= NULL
;
1652 err
= check_pred_tree(filter
, root
);
1656 /* Optimize the tree */
1657 err
= fold_pred_tree(filter
, root
);
1661 /* We don't set root until we know it works */
1663 filter
->root
= root
;
1668 __free_pred_stack(&stack
);
1672 static inline void event_set_filtered_flag(struct ftrace_event_file
*file
)
1674 struct ftrace_event_call
*call
= file
->event_call
;
1676 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1677 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1679 file
->flags
|= FTRACE_EVENT_FL_FILTERED
;
1682 static inline void event_set_filter(struct ftrace_event_file
*file
,
1683 struct event_filter
*filter
)
1685 struct ftrace_event_call
*call
= file
->event_call
;
1687 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1688 rcu_assign_pointer(call
->filter
, filter
);
1690 rcu_assign_pointer(file
->filter
, filter
);
1693 static inline void event_clear_filter(struct ftrace_event_file
*file
)
1695 struct ftrace_event_call
*call
= file
->event_call
;
1697 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1698 RCU_INIT_POINTER(call
->filter
, NULL
);
1700 RCU_INIT_POINTER(file
->filter
, NULL
);
1704 event_set_no_set_filter_flag(struct ftrace_event_file
*file
)
1706 struct ftrace_event_call
*call
= file
->event_call
;
1708 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1709 call
->flags
|= TRACE_EVENT_FL_NO_SET_FILTER
;
1711 file
->flags
|= FTRACE_EVENT_FL_NO_SET_FILTER
;
1715 event_clear_no_set_filter_flag(struct ftrace_event_file
*file
)
1717 struct ftrace_event_call
*call
= file
->event_call
;
1719 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1720 call
->flags
&= ~TRACE_EVENT_FL_NO_SET_FILTER
;
1722 file
->flags
&= ~FTRACE_EVENT_FL_NO_SET_FILTER
;
1726 event_no_set_filter_flag(struct ftrace_event_file
*file
)
1728 struct ftrace_event_call
*call
= file
->event_call
;
1730 if (file
->flags
& FTRACE_EVENT_FL_NO_SET_FILTER
)
1733 if ((call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
) &&
1734 (call
->flags
& TRACE_EVENT_FL_NO_SET_FILTER
))
1740 struct filter_list
{
1741 struct list_head list
;
1742 struct event_filter
*filter
;
1745 static int replace_system_preds(struct ftrace_subsystem_dir
*dir
,
1746 struct trace_array
*tr
,
1747 struct filter_parse_state
*ps
,
1748 char *filter_string
)
1750 struct ftrace_event_file
*file
;
1751 struct filter_list
*filter_item
;
1752 struct filter_list
*tmp
;
1753 LIST_HEAD(filter_list
);
1757 list_for_each_entry(file
, &tr
->events
, list
) {
1758 if (file
->system
!= dir
)
1762 * Try to see if the filter can be applied
1763 * (filter arg is ignored on dry_run)
1765 err
= replace_preds(file
->event_call
, NULL
, ps
, true);
1767 event_set_no_set_filter_flag(file
);
1769 event_clear_no_set_filter_flag(file
);
1772 list_for_each_entry(file
, &tr
->events
, list
) {
1773 struct event_filter
*filter
;
1775 if (file
->system
!= dir
)
1778 if (event_no_set_filter_flag(file
))
1781 filter_item
= kzalloc(sizeof(*filter_item
), GFP_KERNEL
);
1785 list_add_tail(&filter_item
->list
, &filter_list
);
1787 filter_item
->filter
= __alloc_filter();
1788 if (!filter_item
->filter
)
1790 filter
= filter_item
->filter
;
1792 /* Can only fail on no memory */
1793 err
= replace_filter_string(filter
, filter_string
);
1797 err
= replace_preds(file
->event_call
, filter
, ps
, false);
1799 filter_disable(file
);
1800 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1801 append_filter_err(ps
, filter
);
1803 event_set_filtered_flag(file
);
1805 * Regardless of if this returned an error, we still
1806 * replace the filter for the call.
1808 filter
= event_filter(file
);
1809 event_set_filter(file
, filter_item
->filter
);
1810 filter_item
->filter
= filter
;
1819 * The calls can still be using the old filters.
1820 * Do a synchronize_sched() to ensure all calls are
1821 * done with them before we free them.
1823 synchronize_sched();
1824 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1825 __free_filter(filter_item
->filter
);
1826 list_del(&filter_item
->list
);
1831 /* No call succeeded */
1832 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1833 list_del(&filter_item
->list
);
1836 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1839 /* If any call succeeded, we still need to sync */
1841 synchronize_sched();
1842 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1843 __free_filter(filter_item
->filter
);
1844 list_del(&filter_item
->list
);
1850 static int create_filter_start(char *filter_str
, bool set_str
,
1851 struct filter_parse_state
**psp
,
1852 struct event_filter
**filterp
)
1854 struct event_filter
*filter
;
1855 struct filter_parse_state
*ps
= NULL
;
1858 WARN_ON_ONCE(*psp
|| *filterp
);
1860 /* allocate everything, and if any fails, free all and fail */
1861 filter
= __alloc_filter();
1862 if (filter
&& set_str
)
1863 err
= replace_filter_string(filter
, filter_str
);
1865 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1867 if (!filter
|| !ps
|| err
) {
1869 __free_filter(filter
);
1873 /* we're committed to creating a new filter */
1877 parse_init(ps
, filter_ops
, filter_str
);
1878 err
= filter_parse(ps
);
1880 append_filter_err(ps
, filter
);
1884 static void create_filter_finish(struct filter_parse_state
*ps
)
1887 filter_opstack_clear(ps
);
1894 * create_filter - create a filter for a ftrace_event_call
1895 * @call: ftrace_event_call to create a filter for
1896 * @filter_str: filter string
1897 * @set_str: remember @filter_str and enable detailed error in filter
1898 * @filterp: out param for created filter (always updated on return)
1900 * Creates a filter for @call with @filter_str. If @set_str is %true,
1901 * @filter_str is copied and recorded in the new filter.
1903 * On success, returns 0 and *@filterp points to the new filter. On
1904 * failure, returns -errno and *@filterp may point to %NULL or to a new
1905 * filter. In the latter case, the returned filter contains error
1906 * information if @set_str is %true and the caller is responsible for
1909 static int create_filter(struct ftrace_event_call
*call
,
1910 char *filter_str
, bool set_str
,
1911 struct event_filter
**filterp
)
1913 struct event_filter
*filter
= NULL
;
1914 struct filter_parse_state
*ps
= NULL
;
1917 err
= create_filter_start(filter_str
, set_str
, &ps
, &filter
);
1919 err
= replace_preds(call
, filter
, ps
, false);
1921 append_filter_err(ps
, filter
);
1923 create_filter_finish(ps
);
1929 int create_event_filter(struct ftrace_event_call
*call
,
1930 char *filter_str
, bool set_str
,
1931 struct event_filter
**filterp
)
1933 return create_filter(call
, filter_str
, set_str
, filterp
);
1937 * create_system_filter - create a filter for an event_subsystem
1938 * @system: event_subsystem to create a filter for
1939 * @filter_str: filter string
1940 * @filterp: out param for created filter (always updated on return)
1942 * Identical to create_filter() except that it creates a subsystem filter
1943 * and always remembers @filter_str.
1945 static int create_system_filter(struct ftrace_subsystem_dir
*dir
,
1946 struct trace_array
*tr
,
1947 char *filter_str
, struct event_filter
**filterp
)
1949 struct event_filter
*filter
= NULL
;
1950 struct filter_parse_state
*ps
= NULL
;
1953 err
= create_filter_start(filter_str
, true, &ps
, &filter
);
1955 err
= replace_system_preds(dir
, tr
, ps
, filter_str
);
1957 /* System filters just show a default message */
1958 kfree(filter
->filter_string
);
1959 filter
->filter_string
= NULL
;
1961 append_filter_err(ps
, filter
);
1964 create_filter_finish(ps
);
1970 /* caller must hold event_mutex */
1971 int apply_event_filter(struct ftrace_event_file
*file
, char *filter_string
)
1973 struct ftrace_event_call
*call
= file
->event_call
;
1974 struct event_filter
*filter
;
1977 if (!strcmp(strstrip(filter_string
), "0")) {
1978 filter_disable(file
);
1979 filter
= event_filter(file
);
1984 event_clear_filter(file
);
1986 /* Make sure the filter is not being used */
1987 synchronize_sched();
1988 __free_filter(filter
);
1993 err
= create_filter(call
, filter_string
, true, &filter
);
1996 * Always swap the call filter with the new filter
1997 * even if there was an error. If there was an error
1998 * in the filter, we disable the filter and show the error
2002 struct event_filter
*tmp
;
2004 tmp
= event_filter(file
);
2006 event_set_filtered_flag(file
);
2008 filter_disable(file
);
2010 event_set_filter(file
, filter
);
2013 /* Make sure the call is done with the filter */
2014 synchronize_sched();
2022 int apply_subsystem_event_filter(struct ftrace_subsystem_dir
*dir
,
2023 char *filter_string
)
2025 struct event_subsystem
*system
= dir
->subsystem
;
2026 struct trace_array
*tr
= dir
->tr
;
2027 struct event_filter
*filter
;
2030 mutex_lock(&event_mutex
);
2032 /* Make sure the system still has events */
2033 if (!dir
->nr_events
) {
2038 if (!strcmp(strstrip(filter_string
), "0")) {
2039 filter_free_subsystem_preds(dir
, tr
);
2040 remove_filter_string(system
->filter
);
2041 filter
= system
->filter
;
2042 system
->filter
= NULL
;
2043 /* Ensure all filters are no longer used */
2044 synchronize_sched();
2045 filter_free_subsystem_filters(dir
, tr
);
2046 __free_filter(filter
);
2050 err
= create_system_filter(dir
, tr
, filter_string
, &filter
);
2053 * No event actually uses the system filter
2054 * we can free it without synchronize_sched().
2056 __free_filter(system
->filter
);
2057 system
->filter
= filter
;
2060 mutex_unlock(&event_mutex
);
2065 #ifdef CONFIG_PERF_EVENTS
2067 void ftrace_profile_free_filter(struct perf_event
*event
)
2069 struct event_filter
*filter
= event
->filter
;
2071 event
->filter
= NULL
;
2072 __free_filter(filter
);
2075 struct function_filter_data
{
2076 struct ftrace_ops
*ops
;
2081 #ifdef CONFIG_FUNCTION_TRACER
2083 ftrace_function_filter_re(char *buf
, int len
, int *count
)
2087 str
= kstrndup(buf
, len
, GFP_KERNEL
);
2092 * The argv_split function takes white space
2093 * as a separator, so convert ',' into spaces.
2095 strreplace(str
, ',', ' ');
2097 re
= argv_split(GFP_KERNEL
, str
, count
);
2102 static int ftrace_function_set_regexp(struct ftrace_ops
*ops
, int filter
,
2103 int reset
, char *re
, int len
)
2108 ret
= ftrace_set_filter(ops
, re
, len
, reset
);
2110 ret
= ftrace_set_notrace(ops
, re
, len
, reset
);
2115 static int __ftrace_function_set_filter(int filter
, char *buf
, int len
,
2116 struct function_filter_data
*data
)
2118 int i
, re_cnt
, ret
= -EINVAL
;
2122 reset
= filter
? &data
->first_filter
: &data
->first_notrace
;
2125 * The 'ip' field could have multiple filters set, separated
2126 * either by space or comma. We first cut the filter and apply
2127 * all pieces separatelly.
2129 re
= ftrace_function_filter_re(buf
, len
, &re_cnt
);
2133 for (i
= 0; i
< re_cnt
; i
++) {
2134 ret
= ftrace_function_set_regexp(data
->ops
, filter
, *reset
,
2135 re
[i
], strlen(re
[i
]));
2147 static int ftrace_function_check_pred(struct filter_pred
*pred
, int leaf
)
2149 struct ftrace_event_field
*field
= pred
->field
;
2153 * Check the leaf predicate for function trace, verify:
2154 * - only '==' and '!=' is used
2155 * - the 'ip' field is used
2157 if ((pred
->op
!= OP_EQ
) && (pred
->op
!= OP_NE
))
2160 if (strcmp(field
->name
, "ip"))
2164 * Check the non leaf predicate for function trace, verify:
2165 * - only '||' is used
2167 if (pred
->op
!= OP_OR
)
2174 static int ftrace_function_set_filter_cb(enum move_type move
,
2175 struct filter_pred
*pred
,
2176 int *err
, void *data
)
2178 /* Checking the node is valid for function trace. */
2179 if ((move
!= MOVE_DOWN
) ||
2180 (pred
->left
!= FILTER_PRED_INVALID
)) {
2181 *err
= ftrace_function_check_pred(pred
, 0);
2183 *err
= ftrace_function_check_pred(pred
, 1);
2185 return WALK_PRED_ABORT
;
2187 *err
= __ftrace_function_set_filter(pred
->op
== OP_EQ
,
2188 pred
->regex
.pattern
,
2193 return (*err
) ? WALK_PRED_ABORT
: WALK_PRED_DEFAULT
;
2196 static int ftrace_function_set_filter(struct perf_event
*event
,
2197 struct event_filter
*filter
)
2199 struct function_filter_data data
= {
2202 .ops
= &event
->ftrace_ops
,
2205 return walk_pred_tree(filter
->preds
, filter
->root
,
2206 ftrace_function_set_filter_cb
, &data
);
2209 static int ftrace_function_set_filter(struct perf_event
*event
,
2210 struct event_filter
*filter
)
2214 #endif /* CONFIG_FUNCTION_TRACER */
2216 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
2220 struct event_filter
*filter
;
2221 struct ftrace_event_call
*call
;
2223 mutex_lock(&event_mutex
);
2225 call
= event
->tp_event
;
2235 err
= create_filter(call
, filter_str
, false, &filter
);
2239 if (ftrace_event_is_function(call
))
2240 err
= ftrace_function_set_filter(event
, filter
);
2242 event
->filter
= filter
;
2245 if (err
|| ftrace_event_is_function(call
))
2246 __free_filter(filter
);
2249 mutex_unlock(&event_mutex
);
2254 #endif /* CONFIG_PERF_EVENTS */
2256 #ifdef CONFIG_FTRACE_STARTUP_TEST
2258 #include <linux/types.h>
2259 #include <linux/tracepoint.h>
2261 #define CREATE_TRACE_POINTS
2262 #include "trace_events_filter_test.h"
2264 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2267 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2268 .e = ve, .f = vf, .g = vg, .h = vh }, \
2270 .not_visited = nvisit, \
2275 static struct test_filter_data_t
{
2277 struct ftrace_raw_ftrace_test_filter rec
;
2280 } test_filter_data
[] = {
2281 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2282 "e == 1 && f == 1 && g == 1 && h == 1"
2283 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2284 DATA_REC(NO
, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2285 DATA_REC(NO
, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2287 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2288 "e == 1 || f == 1 || g == 1 || h == 1"
2289 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2290 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2291 DATA_REC(YES
, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2293 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2294 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2295 DATA_REC(NO
, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2296 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2297 DATA_REC(YES
, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2298 DATA_REC(NO
, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2300 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2301 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2302 DATA_REC(YES
, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2303 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2304 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2306 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2307 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2308 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2309 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2310 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2312 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2313 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2314 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2315 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2316 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2318 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2319 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2320 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2321 DATA_REC(NO
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2322 DATA_REC(NO
, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2324 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2325 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2326 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2327 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2328 DATA_REC(YES
, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2336 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2338 static int test_pred_visited
;
2340 static int test_pred_visited_fn(struct filter_pred
*pred
, void *event
)
2342 struct ftrace_event_field
*field
= pred
->field
;
2344 test_pred_visited
= 1;
2345 printk(KERN_INFO
"\npred visited %s\n", field
->name
);
2349 static int test_walk_pred_cb(enum move_type move
, struct filter_pred
*pred
,
2350 int *err
, void *data
)
2352 char *fields
= data
;
2354 if ((move
== MOVE_DOWN
) &&
2355 (pred
->left
== FILTER_PRED_INVALID
)) {
2356 struct ftrace_event_field
*field
= pred
->field
;
2359 WARN(1, "all leafs should have field defined");
2360 return WALK_PRED_DEFAULT
;
2362 if (!strchr(fields
, *field
->name
))
2363 return WALK_PRED_DEFAULT
;
2366 pred
->fn
= test_pred_visited_fn
;
2368 return WALK_PRED_DEFAULT
;
2371 static __init
int ftrace_test_event_filter(void)
2375 printk(KERN_INFO
"Testing ftrace filter: ");
2377 for (i
= 0; i
< DATA_CNT
; i
++) {
2378 struct event_filter
*filter
= NULL
;
2379 struct test_filter_data_t
*d
= &test_filter_data
[i
];
2382 err
= create_filter(&event_ftrace_test_filter
, d
->filter
,
2386 "Failed to get filter for '%s', err %d\n",
2388 __free_filter(filter
);
2393 * The preemption disabling is not really needed for self
2394 * tests, but the rcu dereference will complain without it.
2397 if (*d
->not_visited
)
2398 walk_pred_tree(filter
->preds
, filter
->root
,
2402 test_pred_visited
= 0;
2403 err
= filter_match_preds(filter
, &d
->rec
);
2406 __free_filter(filter
);
2408 if (test_pred_visited
) {
2410 "Failed, unwanted pred visited for filter %s\n",
2415 if (err
!= d
->match
) {
2417 "Failed to match filter '%s', expected %d\n",
2418 d
->filter
, d
->match
);
2424 printk(KERN_CONT
"OK\n");
2429 late_initcall(ftrace_test_event_filter
);
2431 #endif /* CONFIG_FTRACE_STARTUP_TEST */