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 /* Filter predicate for CPUs. */
256 static int filter_pred_cpu(struct filter_pred
*pred
, void *event
)
261 cpu
= raw_smp_processor_id();
284 return !!match
== !pred
->not;
287 /* Filter predicate for COMM. */
288 static int filter_pred_comm(struct filter_pred
*pred
, void *event
)
292 cmp
= pred
->regex
.match(current
->comm
, &pred
->regex
,
293 pred
->regex
.field_len
);
294 match
= cmp
^ pred
->not;
299 static int filter_pred_none(struct filter_pred
*pred
, void *event
)
305 * regex_match_foo - Basic regex callbacks
307 * @str: the string to be searched
308 * @r: the regex structure containing the pattern string
309 * @len: the length of the string to be searched (including '\0')
312 * - @str might not be NULL-terminated if it's of type DYN_STRING
316 static int regex_match_full(char *str
, struct regex
*r
, int len
)
318 if (strncmp(str
, r
->pattern
, len
) == 0)
323 static int regex_match_front(char *str
, struct regex
*r
, int len
)
325 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
330 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
332 if (strnstr(str
, r
->pattern
, len
))
337 static int regex_match_end(char *str
, struct regex
*r
, int len
)
339 int strlen
= len
- 1;
341 if (strlen
>= r
->len
&&
342 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
348 * filter_parse_regex - parse a basic regex
349 * @buff: the raw regex
350 * @len: length of the regex
351 * @search: will point to the beginning of the string to compare
352 * @not: tell whether the match will have to be inverted
354 * This passes in a buffer containing a regex and this function will
355 * set search to point to the search part of the buffer and
356 * return the type of search it is (see enum above).
357 * This does modify buff.
360 * search returns the pointer to use for comparison.
361 * not returns 1 if buff started with a '!'
364 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
366 int type
= MATCH_FULL
;
369 if (buff
[0] == '!') {
378 for (i
= 0; i
< len
; i
++) {
379 if (buff
[i
] == '*') {
382 type
= MATCH_END_ONLY
;
384 if (type
== MATCH_END_ONLY
)
385 type
= MATCH_MIDDLE_ONLY
;
387 type
= MATCH_FRONT_ONLY
;
397 static void filter_build_regex(struct filter_pred
*pred
)
399 struct regex
*r
= &pred
->regex
;
401 enum regex_type type
= MATCH_FULL
;
404 if (pred
->op
== OP_GLOB
) {
405 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
406 r
->len
= strlen(search
);
407 memmove(r
->pattern
, search
, r
->len
+1);
412 r
->match
= regex_match_full
;
414 case MATCH_FRONT_ONLY
:
415 r
->match
= regex_match_front
;
417 case MATCH_MIDDLE_ONLY
:
418 r
->match
= regex_match_middle
;
421 r
->match
= regex_match_end
;
434 static struct filter_pred
*
435 get_pred_parent(struct filter_pred
*pred
, struct filter_pred
*preds
,
436 int index
, enum move_type
*move
)
438 if (pred
->parent
& FILTER_PRED_IS_RIGHT
)
439 *move
= MOVE_UP_FROM_RIGHT
;
441 *move
= MOVE_UP_FROM_LEFT
;
442 pred
= &preds
[pred
->parent
& ~FILTER_PRED_IS_RIGHT
];
453 typedef int (*filter_pred_walkcb_t
) (enum move_type move
,
454 struct filter_pred
*pred
,
455 int *err
, void *data
);
457 static int walk_pred_tree(struct filter_pred
*preds
,
458 struct filter_pred
*root
,
459 filter_pred_walkcb_t cb
, void *data
)
461 struct filter_pred
*pred
= root
;
462 enum move_type move
= MOVE_DOWN
;
471 ret
= cb(move
, pred
, &err
, data
);
472 if (ret
== WALK_PRED_ABORT
)
474 if (ret
== WALK_PRED_PARENT
)
479 if (pred
->left
!= FILTER_PRED_INVALID
) {
480 pred
= &preds
[pred
->left
];
484 case MOVE_UP_FROM_LEFT
:
485 pred
= &preds
[pred
->right
];
488 case MOVE_UP_FROM_RIGHT
:
492 pred
= get_pred_parent(pred
, preds
,
505 * A series of AND or ORs where found together. Instead of
506 * climbing up and down the tree branches, an array of the
507 * ops were made in order of checks. We can just move across
508 * the array and short circuit if needed.
510 static int process_ops(struct filter_pred
*preds
,
511 struct filter_pred
*op
, void *rec
)
513 struct filter_pred
*pred
;
519 * Micro-optimization: We set type to true if op
520 * is an OR and false otherwise (AND). Then we
521 * just need to test if the match is equal to
522 * the type, and if it is, we can short circuit the
523 * rest of the checks:
525 * if ((match && op->op == OP_OR) ||
526 * (!match && op->op == OP_AND))
529 type
= op
->op
== OP_OR
;
531 for (i
= 0; i
< op
->val
; i
++) {
532 pred
= &preds
[op
->ops
[i
]];
533 if (!WARN_ON_ONCE(!pred
->fn
))
534 match
= pred
->fn(pred
, rec
);
538 /* If not of not match is equal to not of not, then it is a match */
539 return !!match
== !op
->not;
542 struct filter_match_preds_data
{
543 struct filter_pred
*preds
;
548 static int filter_match_preds_cb(enum move_type move
, struct filter_pred
*pred
,
549 int *err
, void *data
)
551 struct filter_match_preds_data
*d
= data
;
556 /* only AND and OR have children */
557 if (pred
->left
!= FILTER_PRED_INVALID
) {
558 /* If ops is set, then it was folded. */
560 return WALK_PRED_DEFAULT
;
561 /* We can treat folded ops as a leaf node */
562 d
->match
= process_ops(d
->preds
, pred
, d
->rec
);
564 if (!WARN_ON_ONCE(!pred
->fn
))
565 d
->match
= pred
->fn(pred
, d
->rec
);
568 return WALK_PRED_PARENT
;
569 case MOVE_UP_FROM_LEFT
:
571 * Check for short circuits.
573 * Optimization: !!match == (pred->op == OP_OR)
575 * if ((match && pred->op == OP_OR) ||
576 * (!match && pred->op == OP_AND))
578 if (!!d
->match
== (pred
->op
== OP_OR
))
579 return WALK_PRED_PARENT
;
581 case MOVE_UP_FROM_RIGHT
:
585 return WALK_PRED_DEFAULT
;
588 /* return 1 if event matches, 0 otherwise (discard) */
589 int filter_match_preds(struct event_filter
*filter
, void *rec
)
591 struct filter_pred
*preds
;
592 struct filter_pred
*root
;
593 struct filter_match_preds_data data
= {
594 /* match is currently meaningless */
600 /* no filter is considered a match */
604 n_preds
= filter
->n_preds
;
609 * n_preds, root and filter->preds are protect with preemption disabled.
611 root
= rcu_dereference_sched(filter
->root
);
615 data
.preds
= preds
= rcu_dereference_sched(filter
->preds
);
616 ret
= walk_pred_tree(preds
, root
, filter_match_preds_cb
, &data
);
620 EXPORT_SYMBOL_GPL(filter_match_preds
);
622 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
625 ps
->lasterr_pos
= pos
;
628 static void remove_filter_string(struct event_filter
*filter
)
633 kfree(filter
->filter_string
);
634 filter
->filter_string
= NULL
;
637 static int replace_filter_string(struct event_filter
*filter
,
640 kfree(filter
->filter_string
);
641 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
642 if (!filter
->filter_string
)
648 static int append_filter_string(struct event_filter
*filter
,
652 char *new_filter_string
;
654 BUG_ON(!filter
->filter_string
);
655 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
656 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
657 if (!new_filter_string
)
660 strcpy(new_filter_string
, filter
->filter_string
);
661 strcat(new_filter_string
, string
);
662 kfree(filter
->filter_string
);
663 filter
->filter_string
= new_filter_string
;
668 static void append_filter_err(struct filter_parse_state
*ps
,
669 struct event_filter
*filter
)
671 int pos
= ps
->lasterr_pos
;
674 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
678 append_filter_string(filter
, "\n");
679 memset(buf
, ' ', PAGE_SIZE
);
680 if (pos
> PAGE_SIZE
- 128)
683 pbuf
= &buf
[pos
] + 1;
685 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
686 append_filter_string(filter
, buf
);
687 free_page((unsigned long) buf
);
690 static inline struct event_filter
*event_filter(struct trace_event_file
*file
)
692 if (file
->event_call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
693 return file
->event_call
->filter
;
698 /* caller must hold event_mutex */
699 void print_event_filter(struct trace_event_file
*file
, struct trace_seq
*s
)
701 struct event_filter
*filter
= event_filter(file
);
703 if (filter
&& filter
->filter_string
)
704 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
706 trace_seq_puts(s
, "none\n");
709 void print_subsystem_event_filter(struct event_subsystem
*system
,
712 struct event_filter
*filter
;
714 mutex_lock(&event_mutex
);
715 filter
= system
->filter
;
716 if (filter
&& filter
->filter_string
)
717 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
719 trace_seq_puts(s
, DEFAULT_SYS_FILTER_MESSAGE
"\n");
720 mutex_unlock(&event_mutex
);
723 static int __alloc_pred_stack(struct pred_stack
*stack
, int n_preds
)
725 stack
->preds
= kcalloc(n_preds
+ 1, sizeof(*stack
->preds
), GFP_KERNEL
);
728 stack
->index
= n_preds
;
732 static void __free_pred_stack(struct pred_stack
*stack
)
738 static int __push_pred_stack(struct pred_stack
*stack
,
739 struct filter_pred
*pred
)
741 int index
= stack
->index
;
743 if (WARN_ON(index
== 0))
746 stack
->preds
[--index
] = pred
;
747 stack
->index
= index
;
751 static struct filter_pred
*
752 __pop_pred_stack(struct pred_stack
*stack
)
754 struct filter_pred
*pred
;
755 int index
= stack
->index
;
757 pred
= stack
->preds
[index
++];
761 stack
->index
= index
;
765 static int filter_set_pred(struct event_filter
*filter
,
767 struct pred_stack
*stack
,
768 struct filter_pred
*src
)
770 struct filter_pred
*dest
= &filter
->preds
[idx
];
771 struct filter_pred
*left
;
772 struct filter_pred
*right
;
777 if (dest
->op
== OP_OR
|| dest
->op
== OP_AND
) {
778 right
= __pop_pred_stack(stack
);
779 left
= __pop_pred_stack(stack
);
783 * If both children can be folded
784 * and they are the same op as this op or a leaf,
785 * then this op can be folded.
787 if (left
->index
& FILTER_PRED_FOLD
&&
788 ((left
->op
== dest
->op
&& !left
->not) ||
789 left
->left
== FILTER_PRED_INVALID
) &&
790 right
->index
& FILTER_PRED_FOLD
&&
791 ((right
->op
== dest
->op
&& !right
->not) ||
792 right
->left
== FILTER_PRED_INVALID
))
793 dest
->index
|= FILTER_PRED_FOLD
;
795 dest
->left
= left
->index
& ~FILTER_PRED_FOLD
;
796 dest
->right
= right
->index
& ~FILTER_PRED_FOLD
;
797 left
->parent
= dest
->index
& ~FILTER_PRED_FOLD
;
798 right
->parent
= dest
->index
| FILTER_PRED_IS_RIGHT
;
801 * Make dest->left invalid to be used as a quick
802 * way to know this is a leaf node.
804 dest
->left
= FILTER_PRED_INVALID
;
806 /* All leafs allow folding the parent ops. */
807 dest
->index
|= FILTER_PRED_FOLD
;
810 return __push_pred_stack(stack
, dest
);
813 static void __free_preds(struct event_filter
*filter
)
818 for (i
= 0; i
< filter
->n_preds
; i
++)
819 kfree(filter
->preds
[i
].ops
);
820 kfree(filter
->preds
);
821 filter
->preds
= NULL
;
827 static void filter_disable(struct trace_event_file
*file
)
829 struct trace_event_call
*call
= file
->event_call
;
831 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
832 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
834 file
->flags
&= ~EVENT_FILE_FL_FILTERED
;
837 static void __free_filter(struct event_filter
*filter
)
842 __free_preds(filter
);
843 kfree(filter
->filter_string
);
847 void free_event_filter(struct event_filter
*filter
)
849 __free_filter(filter
);
852 static struct event_filter
*__alloc_filter(void)
854 struct event_filter
*filter
;
856 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
860 static int __alloc_preds(struct event_filter
*filter
, int n_preds
)
862 struct filter_pred
*pred
;
866 __free_preds(filter
);
868 filter
->preds
= kcalloc(n_preds
, sizeof(*filter
->preds
), GFP_KERNEL
);
873 filter
->a_preds
= n_preds
;
876 for (i
= 0; i
< n_preds
; i
++) {
877 pred
= &filter
->preds
[i
];
878 pred
->fn
= filter_pred_none
;
884 static inline void __remove_filter(struct trace_event_file
*file
)
886 struct trace_event_call
*call
= file
->event_call
;
888 filter_disable(file
);
889 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
890 remove_filter_string(call
->filter
);
892 remove_filter_string(file
->filter
);
895 static void filter_free_subsystem_preds(struct trace_subsystem_dir
*dir
,
896 struct trace_array
*tr
)
898 struct trace_event_file
*file
;
900 list_for_each_entry(file
, &tr
->events
, list
) {
901 if (file
->system
!= dir
)
903 __remove_filter(file
);
907 static inline void __free_subsystem_filter(struct trace_event_file
*file
)
909 struct trace_event_call
*call
= file
->event_call
;
911 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
) {
912 __free_filter(call
->filter
);
915 __free_filter(file
->filter
);
920 static void filter_free_subsystem_filters(struct trace_subsystem_dir
*dir
,
921 struct trace_array
*tr
)
923 struct trace_event_file
*file
;
925 list_for_each_entry(file
, &tr
->events
, list
) {
926 if (file
->system
!= dir
)
928 __free_subsystem_filter(file
);
932 static int filter_add_pred(struct filter_parse_state
*ps
,
933 struct event_filter
*filter
,
934 struct filter_pred
*pred
,
935 struct pred_stack
*stack
)
939 if (WARN_ON(filter
->n_preds
== filter
->a_preds
)) {
940 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
944 err
= filter_set_pred(filter
, filter
->n_preds
, stack
, pred
);
953 int filter_assign_type(const char *type
)
955 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
956 return FILTER_DYN_STRING
;
958 if (strchr(type
, '[') && strstr(type
, "char"))
959 return FILTER_STATIC_STRING
;
964 static bool is_function_field(struct ftrace_event_field
*field
)
966 return field
->filter_type
== FILTER_TRACE_FN
;
969 static bool is_string_field(struct ftrace_event_field
*field
)
971 return field
->filter_type
== FILTER_DYN_STRING
||
972 field
->filter_type
== FILTER_STATIC_STRING
||
973 field
->filter_type
== FILTER_PTR_STRING
;
976 static bool is_legal_op(struct ftrace_event_field
*field
, int op
)
978 if (is_string_field(field
) &&
979 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
981 if (!is_string_field(field
) && op
== OP_GLOB
)
987 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
990 filter_pred_fn_t fn
= NULL
;
992 switch (field_size
) {
994 if (op
== OP_EQ
|| op
== OP_NE
)
996 else if (field_is_signed
)
997 fn
= filter_pred_s64
;
999 fn
= filter_pred_u64
;
1002 if (op
== OP_EQ
|| op
== OP_NE
)
1003 fn
= filter_pred_32
;
1004 else if (field_is_signed
)
1005 fn
= filter_pred_s32
;
1007 fn
= filter_pred_u32
;
1010 if (op
== OP_EQ
|| op
== OP_NE
)
1011 fn
= filter_pred_16
;
1012 else if (field_is_signed
)
1013 fn
= filter_pred_s16
;
1015 fn
= filter_pred_u16
;
1018 if (op
== OP_EQ
|| op
== OP_NE
)
1020 else if (field_is_signed
)
1021 fn
= filter_pred_s8
;
1023 fn
= filter_pred_u8
;
1030 static int init_pred(struct filter_parse_state
*ps
,
1031 struct ftrace_event_field
*field
,
1032 struct filter_pred
*pred
)
1035 filter_pred_fn_t fn
= filter_pred_none
;
1036 unsigned long long val
;
1039 pred
->offset
= field
->offset
;
1041 if (!is_legal_op(field
, pred
->op
)) {
1042 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
1046 if (field
->filter_type
== FILTER_COMM
) {
1047 filter_build_regex(pred
);
1048 fn
= filter_pred_comm
;
1049 pred
->regex
.field_len
= TASK_COMM_LEN
;
1050 } else if (is_string_field(field
)) {
1051 filter_build_regex(pred
);
1053 if (field
->filter_type
== FILTER_STATIC_STRING
) {
1054 fn
= filter_pred_string
;
1055 pred
->regex
.field_len
= field
->size
;
1056 } else if (field
->filter_type
== FILTER_DYN_STRING
)
1057 fn
= filter_pred_strloc
;
1059 fn
= filter_pred_pchar
;
1060 } else if (is_function_field(field
)) {
1061 if (strcmp(field
->name
, "ip")) {
1062 parse_error(ps
, FILT_ERR_IP_FIELD_ONLY
, 0);
1066 if (field
->is_signed
)
1067 ret
= kstrtoll(pred
->regex
.pattern
, 0, &val
);
1069 ret
= kstrtoull(pred
->regex
.pattern
, 0, &val
);
1071 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
1076 if (field
->filter_type
== FILTER_CPU
)
1077 fn
= filter_pred_cpu
;
1079 fn
= select_comparison_fn(pred
->op
, field
->size
,
1082 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1087 if (pred
->op
== OP_NE
)
1094 static void parse_init(struct filter_parse_state
*ps
,
1095 struct filter_op
*ops
,
1098 memset(ps
, '\0', sizeof(*ps
));
1100 ps
->infix
.string
= infix_string
;
1101 ps
->infix
.cnt
= strlen(infix_string
);
1104 INIT_LIST_HEAD(&ps
->opstack
);
1105 INIT_LIST_HEAD(&ps
->postfix
);
1108 static char infix_next(struct filter_parse_state
*ps
)
1115 return ps
->infix
.string
[ps
->infix
.tail
++];
1118 static char infix_peek(struct filter_parse_state
*ps
)
1120 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
1123 return ps
->infix
.string
[ps
->infix
.tail
];
1126 static void infix_advance(struct filter_parse_state
*ps
)
1135 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
1138 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
1141 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
1145 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1146 if (ps
->ops
[i
].string
[0] == c
)
1153 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
1155 char nextc
= infix_peek(ps
);
1163 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1164 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
1166 return ps
->ops
[i
].id
;
1172 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1173 if (!strcmp(opstr
, ps
->ops
[i
].string
))
1174 return ps
->ops
[i
].id
;
1180 static inline void clear_operand_string(struct filter_parse_state
*ps
)
1182 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
1183 ps
->operand
.tail
= 0;
1186 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
1188 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
1191 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
1196 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
1198 struct opstack_op
*opstack_op
;
1200 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
1204 opstack_op
->op
= op
;
1205 list_add(&opstack_op
->list
, &ps
->opstack
);
1210 static int filter_opstack_empty(struct filter_parse_state
*ps
)
1212 return list_empty(&ps
->opstack
);
1215 static int filter_opstack_top(struct filter_parse_state
*ps
)
1217 struct opstack_op
*opstack_op
;
1219 if (filter_opstack_empty(ps
))
1222 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1224 return opstack_op
->op
;
1227 static int filter_opstack_pop(struct filter_parse_state
*ps
)
1229 struct opstack_op
*opstack_op
;
1232 if (filter_opstack_empty(ps
))
1235 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1236 op
= opstack_op
->op
;
1237 list_del(&opstack_op
->list
);
1244 static void filter_opstack_clear(struct filter_parse_state
*ps
)
1246 while (!filter_opstack_empty(ps
))
1247 filter_opstack_pop(ps
);
1250 static char *curr_operand(struct filter_parse_state
*ps
)
1252 return ps
->operand
.string
;
1255 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
1257 struct postfix_elt
*elt
;
1259 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1264 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1265 if (!elt
->operand
) {
1270 list_add_tail(&elt
->list
, &ps
->postfix
);
1275 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1277 struct postfix_elt
*elt
;
1279 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1284 elt
->operand
= NULL
;
1286 list_add_tail(&elt
->list
, &ps
->postfix
);
1291 static void postfix_clear(struct filter_parse_state
*ps
)
1293 struct postfix_elt
*elt
;
1295 while (!list_empty(&ps
->postfix
)) {
1296 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1297 list_del(&elt
->list
);
1298 kfree(elt
->operand
);
1303 static int filter_parse(struct filter_parse_state
*ps
)
1309 while ((ch
= infix_next(ps
))) {
1321 if (is_op_char(ps
, ch
)) {
1322 op
= infix_get_op(ps
, ch
);
1323 if (op
== OP_NONE
) {
1324 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1328 if (strlen(curr_operand(ps
))) {
1329 postfix_append_operand(ps
, curr_operand(ps
));
1330 clear_operand_string(ps
);
1333 while (!filter_opstack_empty(ps
)) {
1334 top_op
= filter_opstack_top(ps
);
1335 if (!is_precedence_lower(ps
, top_op
, op
)) {
1336 top_op
= filter_opstack_pop(ps
);
1337 postfix_append_op(ps
, top_op
);
1343 filter_opstack_push(ps
, op
);
1348 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1353 if (strlen(curr_operand(ps
))) {
1354 postfix_append_operand(ps
, curr_operand(ps
));
1355 clear_operand_string(ps
);
1358 top_op
= filter_opstack_pop(ps
);
1359 while (top_op
!= OP_NONE
) {
1360 if (top_op
== OP_OPEN_PAREN
)
1362 postfix_append_op(ps
, top_op
);
1363 top_op
= filter_opstack_pop(ps
);
1365 if (top_op
== OP_NONE
) {
1366 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1372 if (append_operand_char(ps
, ch
)) {
1373 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1378 if (strlen(curr_operand(ps
)))
1379 postfix_append_operand(ps
, curr_operand(ps
));
1381 while (!filter_opstack_empty(ps
)) {
1382 top_op
= filter_opstack_pop(ps
);
1383 if (top_op
== OP_NONE
)
1385 if (top_op
== OP_OPEN_PAREN
) {
1386 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1389 postfix_append_op(ps
, top_op
);
1395 static struct filter_pred
*create_pred(struct filter_parse_state
*ps
,
1396 struct trace_event_call
*call
,
1397 int op
, char *operand1
, char *operand2
)
1399 struct ftrace_event_field
*field
;
1400 static struct filter_pred pred
;
1402 memset(&pred
, 0, sizeof(pred
));
1405 if (op
== OP_AND
|| op
== OP_OR
)
1408 if (!operand1
|| !operand2
) {
1409 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1413 field
= trace_find_event_field(call
, operand1
);
1415 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
1419 strcpy(pred
.regex
.pattern
, operand2
);
1420 pred
.regex
.len
= strlen(pred
.regex
.pattern
);
1422 return init_pred(ps
, field
, &pred
) ? NULL
: &pred
;
1425 static int check_preds(struct filter_parse_state
*ps
)
1427 int n_normal_preds
= 0, n_logical_preds
= 0;
1428 struct postfix_elt
*elt
;
1431 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1432 if (elt
->op
== OP_NONE
) {
1437 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1442 if (elt
->op
!= OP_NOT
)
1445 /* all ops should have operands */
1450 if (cnt
!= 1 || !n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1451 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1458 static int count_preds(struct filter_parse_state
*ps
)
1460 struct postfix_elt
*elt
;
1463 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1464 if (elt
->op
== OP_NONE
)
1472 struct check_pred_data
{
1477 static int check_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1478 int *err
, void *data
)
1480 struct check_pred_data
*d
= data
;
1482 if (WARN_ON(d
->count
++ > d
->max
)) {
1484 return WALK_PRED_ABORT
;
1486 return WALK_PRED_DEFAULT
;
1490 * The tree is walked at filtering of an event. If the tree is not correctly
1491 * built, it may cause an infinite loop. Check here that the tree does
1494 static int check_pred_tree(struct event_filter
*filter
,
1495 struct filter_pred
*root
)
1497 struct check_pred_data data
= {
1499 * The max that we can hit a node is three times.
1500 * Once going down, once coming up from left, and
1501 * once coming up from right. This is more than enough
1502 * since leafs are only hit a single time.
1504 .max
= 3 * filter
->n_preds
,
1508 return walk_pred_tree(filter
->preds
, root
,
1509 check_pred_tree_cb
, &data
);
1512 static int count_leafs_cb(enum move_type move
, struct filter_pred
*pred
,
1513 int *err
, void *data
)
1517 if ((move
== MOVE_DOWN
) &&
1518 (pred
->left
== FILTER_PRED_INVALID
))
1521 return WALK_PRED_DEFAULT
;
1524 static int count_leafs(struct filter_pred
*preds
, struct filter_pred
*root
)
1528 ret
= walk_pred_tree(preds
, root
, count_leafs_cb
, &count
);
1533 struct fold_pred_data
{
1534 struct filter_pred
*root
;
1539 static int fold_pred_cb(enum move_type move
, struct filter_pred
*pred
,
1540 int *err
, void *data
)
1542 struct fold_pred_data
*d
= data
;
1543 struct filter_pred
*root
= d
->root
;
1545 if (move
!= MOVE_DOWN
)
1546 return WALK_PRED_DEFAULT
;
1547 if (pred
->left
!= FILTER_PRED_INVALID
)
1548 return WALK_PRED_DEFAULT
;
1550 if (WARN_ON(d
->count
== d
->children
)) {
1552 return WALK_PRED_ABORT
;
1555 pred
->index
&= ~FILTER_PRED_FOLD
;
1556 root
->ops
[d
->count
++] = pred
->index
;
1557 return WALK_PRED_DEFAULT
;
1560 static int fold_pred(struct filter_pred
*preds
, struct filter_pred
*root
)
1562 struct fold_pred_data data
= {
1568 /* No need to keep the fold flag */
1569 root
->index
&= ~FILTER_PRED_FOLD
;
1571 /* If the root is a leaf then do nothing */
1572 if (root
->left
== FILTER_PRED_INVALID
)
1575 /* count the children */
1576 children
= count_leafs(preds
, &preds
[root
->left
]);
1577 children
+= count_leafs(preds
, &preds
[root
->right
]);
1579 root
->ops
= kcalloc(children
, sizeof(*root
->ops
), GFP_KERNEL
);
1583 root
->val
= children
;
1584 data
.children
= children
;
1585 return walk_pred_tree(preds
, root
, fold_pred_cb
, &data
);
1588 static int fold_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1589 int *err
, void *data
)
1591 struct filter_pred
*preds
= data
;
1593 if (move
!= MOVE_DOWN
)
1594 return WALK_PRED_DEFAULT
;
1595 if (!(pred
->index
& FILTER_PRED_FOLD
))
1596 return WALK_PRED_DEFAULT
;
1598 *err
= fold_pred(preds
, pred
);
1600 return WALK_PRED_ABORT
;
1602 /* eveyrhing below is folded, continue with parent */
1603 return WALK_PRED_PARENT
;
1607 * To optimize the processing of the ops, if we have several "ors" or
1608 * "ands" together, we can put them in an array and process them all
1609 * together speeding up the filter logic.
1611 static int fold_pred_tree(struct event_filter
*filter
,
1612 struct filter_pred
*root
)
1614 return walk_pred_tree(filter
->preds
, root
, fold_pred_tree_cb
,
1618 static int replace_preds(struct trace_event_call
*call
,
1619 struct event_filter
*filter
,
1620 struct filter_parse_state
*ps
,
1623 char *operand1
= NULL
, *operand2
= NULL
;
1624 struct filter_pred
*pred
;
1625 struct filter_pred
*root
;
1626 struct postfix_elt
*elt
;
1627 struct pred_stack stack
= { }; /* init to NULL */
1631 n_preds
= count_preds(ps
);
1632 if (n_preds
>= MAX_FILTER_PRED
) {
1633 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1637 err
= check_preds(ps
);
1642 err
= __alloc_pred_stack(&stack
, n_preds
);
1645 err
= __alloc_preds(filter
, n_preds
);
1651 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1652 if (elt
->op
== OP_NONE
) {
1654 operand1
= elt
->operand
;
1656 operand2
= elt
->operand
;
1658 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1665 if (elt
->op
== OP_NOT
) {
1666 if (!n_preds
|| operand1
|| operand2
) {
1667 parse_error(ps
, FILT_ERR_ILLEGAL_NOT_OP
, 0);
1672 filter
->preds
[n_preds
- 1].not ^= 1;
1676 if (WARN_ON(n_preds
++ == MAX_FILTER_PRED
)) {
1677 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1682 pred
= create_pred(ps
, call
, elt
->op
, operand1
, operand2
);
1689 err
= filter_add_pred(ps
, filter
, pred
, &stack
);
1694 operand1
= operand2
= NULL
;
1698 /* We should have one item left on the stack */
1699 pred
= __pop_pred_stack(&stack
);
1702 /* This item is where we start from in matching */
1704 /* Make sure the stack is empty */
1705 pred
= __pop_pred_stack(&stack
);
1706 if (WARN_ON(pred
)) {
1708 filter
->root
= NULL
;
1711 err
= check_pred_tree(filter
, root
);
1715 /* Optimize the tree */
1716 err
= fold_pred_tree(filter
, root
);
1720 /* We don't set root until we know it works */
1722 filter
->root
= root
;
1727 __free_pred_stack(&stack
);
1731 static inline void event_set_filtered_flag(struct trace_event_file
*file
)
1733 struct trace_event_call
*call
= file
->event_call
;
1735 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1736 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1738 file
->flags
|= EVENT_FILE_FL_FILTERED
;
1741 static inline void event_set_filter(struct trace_event_file
*file
,
1742 struct event_filter
*filter
)
1744 struct trace_event_call
*call
= file
->event_call
;
1746 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1747 rcu_assign_pointer(call
->filter
, filter
);
1749 rcu_assign_pointer(file
->filter
, filter
);
1752 static inline void event_clear_filter(struct trace_event_file
*file
)
1754 struct trace_event_call
*call
= file
->event_call
;
1756 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1757 RCU_INIT_POINTER(call
->filter
, NULL
);
1759 RCU_INIT_POINTER(file
->filter
, NULL
);
1763 event_set_no_set_filter_flag(struct trace_event_file
*file
)
1765 struct trace_event_call
*call
= file
->event_call
;
1767 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1768 call
->flags
|= TRACE_EVENT_FL_NO_SET_FILTER
;
1770 file
->flags
|= EVENT_FILE_FL_NO_SET_FILTER
;
1774 event_clear_no_set_filter_flag(struct trace_event_file
*file
)
1776 struct trace_event_call
*call
= file
->event_call
;
1778 if (call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
)
1779 call
->flags
&= ~TRACE_EVENT_FL_NO_SET_FILTER
;
1781 file
->flags
&= ~EVENT_FILE_FL_NO_SET_FILTER
;
1785 event_no_set_filter_flag(struct trace_event_file
*file
)
1787 struct trace_event_call
*call
= file
->event_call
;
1789 if (file
->flags
& EVENT_FILE_FL_NO_SET_FILTER
)
1792 if ((call
->flags
& TRACE_EVENT_FL_USE_CALL_FILTER
) &&
1793 (call
->flags
& TRACE_EVENT_FL_NO_SET_FILTER
))
1799 struct filter_list
{
1800 struct list_head list
;
1801 struct event_filter
*filter
;
1804 static int replace_system_preds(struct trace_subsystem_dir
*dir
,
1805 struct trace_array
*tr
,
1806 struct filter_parse_state
*ps
,
1807 char *filter_string
)
1809 struct trace_event_file
*file
;
1810 struct filter_list
*filter_item
;
1811 struct filter_list
*tmp
;
1812 LIST_HEAD(filter_list
);
1816 list_for_each_entry(file
, &tr
->events
, list
) {
1817 if (file
->system
!= dir
)
1821 * Try to see if the filter can be applied
1822 * (filter arg is ignored on dry_run)
1824 err
= replace_preds(file
->event_call
, NULL
, ps
, true);
1826 event_set_no_set_filter_flag(file
);
1828 event_clear_no_set_filter_flag(file
);
1831 list_for_each_entry(file
, &tr
->events
, list
) {
1832 struct event_filter
*filter
;
1834 if (file
->system
!= dir
)
1837 if (event_no_set_filter_flag(file
))
1840 filter_item
= kzalloc(sizeof(*filter_item
), GFP_KERNEL
);
1844 list_add_tail(&filter_item
->list
, &filter_list
);
1846 filter_item
->filter
= __alloc_filter();
1847 if (!filter_item
->filter
)
1849 filter
= filter_item
->filter
;
1851 /* Can only fail on no memory */
1852 err
= replace_filter_string(filter
, filter_string
);
1856 err
= replace_preds(file
->event_call
, filter
, ps
, false);
1858 filter_disable(file
);
1859 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1860 append_filter_err(ps
, filter
);
1862 event_set_filtered_flag(file
);
1864 * Regardless of if this returned an error, we still
1865 * replace the filter for the call.
1867 filter
= event_filter(file
);
1868 event_set_filter(file
, filter_item
->filter
);
1869 filter_item
->filter
= filter
;
1878 * The calls can still be using the old filters.
1879 * Do a synchronize_sched() to ensure all calls are
1880 * done with them before we free them.
1882 synchronize_sched();
1883 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1884 __free_filter(filter_item
->filter
);
1885 list_del(&filter_item
->list
);
1890 /* No call succeeded */
1891 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1892 list_del(&filter_item
->list
);
1895 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1898 /* If any call succeeded, we still need to sync */
1900 synchronize_sched();
1901 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1902 __free_filter(filter_item
->filter
);
1903 list_del(&filter_item
->list
);
1909 static int create_filter_start(char *filter_str
, bool set_str
,
1910 struct filter_parse_state
**psp
,
1911 struct event_filter
**filterp
)
1913 struct event_filter
*filter
;
1914 struct filter_parse_state
*ps
= NULL
;
1917 WARN_ON_ONCE(*psp
|| *filterp
);
1919 /* allocate everything, and if any fails, free all and fail */
1920 filter
= __alloc_filter();
1921 if (filter
&& set_str
)
1922 err
= replace_filter_string(filter
, filter_str
);
1924 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1926 if (!filter
|| !ps
|| err
) {
1928 __free_filter(filter
);
1932 /* we're committed to creating a new filter */
1936 parse_init(ps
, filter_ops
, filter_str
);
1937 err
= filter_parse(ps
);
1939 append_filter_err(ps
, filter
);
1943 static void create_filter_finish(struct filter_parse_state
*ps
)
1946 filter_opstack_clear(ps
);
1953 * create_filter - create a filter for a trace_event_call
1954 * @call: trace_event_call to create a filter for
1955 * @filter_str: filter string
1956 * @set_str: remember @filter_str and enable detailed error in filter
1957 * @filterp: out param for created filter (always updated on return)
1959 * Creates a filter for @call with @filter_str. If @set_str is %true,
1960 * @filter_str is copied and recorded in the new filter.
1962 * On success, returns 0 and *@filterp points to the new filter. On
1963 * failure, returns -errno and *@filterp may point to %NULL or to a new
1964 * filter. In the latter case, the returned filter contains error
1965 * information if @set_str is %true and the caller is responsible for
1968 static int create_filter(struct trace_event_call
*call
,
1969 char *filter_str
, bool set_str
,
1970 struct event_filter
**filterp
)
1972 struct event_filter
*filter
= NULL
;
1973 struct filter_parse_state
*ps
= NULL
;
1976 err
= create_filter_start(filter_str
, set_str
, &ps
, &filter
);
1978 err
= replace_preds(call
, filter
, ps
, false);
1980 append_filter_err(ps
, filter
);
1982 create_filter_finish(ps
);
1988 int create_event_filter(struct trace_event_call
*call
,
1989 char *filter_str
, bool set_str
,
1990 struct event_filter
**filterp
)
1992 return create_filter(call
, filter_str
, set_str
, filterp
);
1996 * create_system_filter - create a filter for an event_subsystem
1997 * @system: event_subsystem to create a filter for
1998 * @filter_str: filter string
1999 * @filterp: out param for created filter (always updated on return)
2001 * Identical to create_filter() except that it creates a subsystem filter
2002 * and always remembers @filter_str.
2004 static int create_system_filter(struct trace_subsystem_dir
*dir
,
2005 struct trace_array
*tr
,
2006 char *filter_str
, struct event_filter
**filterp
)
2008 struct event_filter
*filter
= NULL
;
2009 struct filter_parse_state
*ps
= NULL
;
2012 err
= create_filter_start(filter_str
, true, &ps
, &filter
);
2014 err
= replace_system_preds(dir
, tr
, ps
, filter_str
);
2016 /* System filters just show a default message */
2017 kfree(filter
->filter_string
);
2018 filter
->filter_string
= NULL
;
2020 append_filter_err(ps
, filter
);
2023 create_filter_finish(ps
);
2029 /* caller must hold event_mutex */
2030 int apply_event_filter(struct trace_event_file
*file
, char *filter_string
)
2032 struct trace_event_call
*call
= file
->event_call
;
2033 struct event_filter
*filter
;
2036 if (!strcmp(strstrip(filter_string
), "0")) {
2037 filter_disable(file
);
2038 filter
= event_filter(file
);
2043 event_clear_filter(file
);
2045 /* Make sure the filter is not being used */
2046 synchronize_sched();
2047 __free_filter(filter
);
2052 err
= create_filter(call
, filter_string
, true, &filter
);
2055 * Always swap the call filter with the new filter
2056 * even if there was an error. If there was an error
2057 * in the filter, we disable the filter and show the error
2061 struct event_filter
*tmp
;
2063 tmp
= event_filter(file
);
2065 event_set_filtered_flag(file
);
2067 filter_disable(file
);
2069 event_set_filter(file
, filter
);
2072 /* Make sure the call is done with the filter */
2073 synchronize_sched();
2081 int apply_subsystem_event_filter(struct trace_subsystem_dir
*dir
,
2082 char *filter_string
)
2084 struct event_subsystem
*system
= dir
->subsystem
;
2085 struct trace_array
*tr
= dir
->tr
;
2086 struct event_filter
*filter
;
2089 mutex_lock(&event_mutex
);
2091 /* Make sure the system still has events */
2092 if (!dir
->nr_events
) {
2097 if (!strcmp(strstrip(filter_string
), "0")) {
2098 filter_free_subsystem_preds(dir
, tr
);
2099 remove_filter_string(system
->filter
);
2100 filter
= system
->filter
;
2101 system
->filter
= NULL
;
2102 /* Ensure all filters are no longer used */
2103 synchronize_sched();
2104 filter_free_subsystem_filters(dir
, tr
);
2105 __free_filter(filter
);
2109 err
= create_system_filter(dir
, tr
, filter_string
, &filter
);
2112 * No event actually uses the system filter
2113 * we can free it without synchronize_sched().
2115 __free_filter(system
->filter
);
2116 system
->filter
= filter
;
2119 mutex_unlock(&event_mutex
);
2124 #ifdef CONFIG_PERF_EVENTS
2126 void ftrace_profile_free_filter(struct perf_event
*event
)
2128 struct event_filter
*filter
= event
->filter
;
2130 event
->filter
= NULL
;
2131 __free_filter(filter
);
2134 struct function_filter_data
{
2135 struct ftrace_ops
*ops
;
2140 #ifdef CONFIG_FUNCTION_TRACER
2142 ftrace_function_filter_re(char *buf
, int len
, int *count
)
2146 str
= kstrndup(buf
, len
, GFP_KERNEL
);
2151 * The argv_split function takes white space
2152 * as a separator, so convert ',' into spaces.
2154 strreplace(str
, ',', ' ');
2156 re
= argv_split(GFP_KERNEL
, str
, count
);
2161 static int ftrace_function_set_regexp(struct ftrace_ops
*ops
, int filter
,
2162 int reset
, char *re
, int len
)
2167 ret
= ftrace_set_filter(ops
, re
, len
, reset
);
2169 ret
= ftrace_set_notrace(ops
, re
, len
, reset
);
2174 static int __ftrace_function_set_filter(int filter
, char *buf
, int len
,
2175 struct function_filter_data
*data
)
2177 int i
, re_cnt
, ret
= -EINVAL
;
2181 reset
= filter
? &data
->first_filter
: &data
->first_notrace
;
2184 * The 'ip' field could have multiple filters set, separated
2185 * either by space or comma. We first cut the filter and apply
2186 * all pieces separatelly.
2188 re
= ftrace_function_filter_re(buf
, len
, &re_cnt
);
2192 for (i
= 0; i
< re_cnt
; i
++) {
2193 ret
= ftrace_function_set_regexp(data
->ops
, filter
, *reset
,
2194 re
[i
], strlen(re
[i
]));
2206 static int ftrace_function_check_pred(struct filter_pred
*pred
, int leaf
)
2208 struct ftrace_event_field
*field
= pred
->field
;
2212 * Check the leaf predicate for function trace, verify:
2213 * - only '==' and '!=' is used
2214 * - the 'ip' field is used
2216 if ((pred
->op
!= OP_EQ
) && (pred
->op
!= OP_NE
))
2219 if (strcmp(field
->name
, "ip"))
2223 * Check the non leaf predicate for function trace, verify:
2224 * - only '||' is used
2226 if (pred
->op
!= OP_OR
)
2233 static int ftrace_function_set_filter_cb(enum move_type move
,
2234 struct filter_pred
*pred
,
2235 int *err
, void *data
)
2237 /* Checking the node is valid for function trace. */
2238 if ((move
!= MOVE_DOWN
) ||
2239 (pred
->left
!= FILTER_PRED_INVALID
)) {
2240 *err
= ftrace_function_check_pred(pred
, 0);
2242 *err
= ftrace_function_check_pred(pred
, 1);
2244 return WALK_PRED_ABORT
;
2246 *err
= __ftrace_function_set_filter(pred
->op
== OP_EQ
,
2247 pred
->regex
.pattern
,
2252 return (*err
) ? WALK_PRED_ABORT
: WALK_PRED_DEFAULT
;
2255 static int ftrace_function_set_filter(struct perf_event
*event
,
2256 struct event_filter
*filter
)
2258 struct function_filter_data data
= {
2261 .ops
= &event
->ftrace_ops
,
2264 return walk_pred_tree(filter
->preds
, filter
->root
,
2265 ftrace_function_set_filter_cb
, &data
);
2268 static int ftrace_function_set_filter(struct perf_event
*event
,
2269 struct event_filter
*filter
)
2273 #endif /* CONFIG_FUNCTION_TRACER */
2275 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
2279 struct event_filter
*filter
;
2280 struct trace_event_call
*call
;
2282 mutex_lock(&event_mutex
);
2284 call
= event
->tp_event
;
2294 err
= create_filter(call
, filter_str
, false, &filter
);
2298 if (ftrace_event_is_function(call
))
2299 err
= ftrace_function_set_filter(event
, filter
);
2301 event
->filter
= filter
;
2304 if (err
|| ftrace_event_is_function(call
))
2305 __free_filter(filter
);
2308 mutex_unlock(&event_mutex
);
2313 #endif /* CONFIG_PERF_EVENTS */
2315 #ifdef CONFIG_FTRACE_STARTUP_TEST
2317 #include <linux/types.h>
2318 #include <linux/tracepoint.h>
2320 #define CREATE_TRACE_POINTS
2321 #include "trace_events_filter_test.h"
2323 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2326 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2327 .e = ve, .f = vf, .g = vg, .h = vh }, \
2329 .not_visited = nvisit, \
2334 static struct test_filter_data_t
{
2336 struct trace_event_raw_ftrace_test_filter rec
;
2339 } test_filter_data
[] = {
2340 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2341 "e == 1 && f == 1 && g == 1 && h == 1"
2342 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2343 DATA_REC(NO
, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2344 DATA_REC(NO
, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2346 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2347 "e == 1 || f == 1 || g == 1 || h == 1"
2348 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2349 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2350 DATA_REC(YES
, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2352 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2353 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2354 DATA_REC(NO
, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2355 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2356 DATA_REC(YES
, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2357 DATA_REC(NO
, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2359 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2360 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2361 DATA_REC(YES
, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2362 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2363 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2365 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2366 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2367 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2368 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2369 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2371 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2372 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2373 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2374 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2375 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2377 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2378 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2379 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2380 DATA_REC(NO
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2381 DATA_REC(NO
, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2383 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2384 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2385 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2386 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2387 DATA_REC(YES
, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2395 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2397 static int test_pred_visited
;
2399 static int test_pred_visited_fn(struct filter_pred
*pred
, void *event
)
2401 struct ftrace_event_field
*field
= pred
->field
;
2403 test_pred_visited
= 1;
2404 printk(KERN_INFO
"\npred visited %s\n", field
->name
);
2408 static int test_walk_pred_cb(enum move_type move
, struct filter_pred
*pred
,
2409 int *err
, void *data
)
2411 char *fields
= data
;
2413 if ((move
== MOVE_DOWN
) &&
2414 (pred
->left
== FILTER_PRED_INVALID
)) {
2415 struct ftrace_event_field
*field
= pred
->field
;
2418 WARN(1, "all leafs should have field defined");
2419 return WALK_PRED_DEFAULT
;
2421 if (!strchr(fields
, *field
->name
))
2422 return WALK_PRED_DEFAULT
;
2425 pred
->fn
= test_pred_visited_fn
;
2427 return WALK_PRED_DEFAULT
;
2430 static __init
int ftrace_test_event_filter(void)
2434 printk(KERN_INFO
"Testing ftrace filter: ");
2436 for (i
= 0; i
< DATA_CNT
; i
++) {
2437 struct event_filter
*filter
= NULL
;
2438 struct test_filter_data_t
*d
= &test_filter_data
[i
];
2441 err
= create_filter(&event_ftrace_test_filter
, d
->filter
,
2445 "Failed to get filter for '%s', err %d\n",
2447 __free_filter(filter
);
2452 * The preemption disabling is not really needed for self
2453 * tests, but the rcu dereference will complain without it.
2456 if (*d
->not_visited
)
2457 walk_pred_tree(filter
->preds
, filter
->root
,
2461 test_pred_visited
= 0;
2462 err
= filter_match_preds(filter
, &d
->rec
);
2465 __free_filter(filter
);
2467 if (test_pred_visited
) {
2469 "Failed, unwanted pred visited for filter %s\n",
2474 if (err
!= d
->match
) {
2476 "Failed to match filter '%s', expected %d\n",
2477 d
->filter
, d
->match
);
2483 printk(KERN_CONT
"OK\n");
2488 late_initcall(ftrace_test_event_filter
);
2490 #endif /* CONFIG_FTRACE_STARTUP_TEST */