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 '!'",
111 enum filter_op_ids op
;
112 struct list_head list
;
116 enum filter_op_ids op
;
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_LT_##type(struct filter_pred *pred, void *event) \
150 type *addr = (type *)(event + pred->offset); \
151 type val = (type)pred->val; \
152 int match = (*addr < val); \
153 return !!match == !pred->not; \
155 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
157 type *addr = (type *)(event + pred->offset); \
158 type val = (type)pred->val; \
159 int match = (*addr <= val); \
160 return !!match == !pred->not; \
162 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
164 type *addr = (type *)(event + pred->offset); \
165 type val = (type)pred->val; \
166 int match = (*addr > val); \
167 return !!match == !pred->not; \
169 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
171 type *addr = (type *)(event + pred->offset); \
172 type val = (type)pred->val; \
173 int match = (*addr >= val); \
174 return !!match == !pred->not; \
176 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
178 type *addr = (type *)(event + pred->offset); \
179 type val = (type)pred->val; \
180 int match = !!(*addr & val); \
181 return match == !pred->not; \
183 static const filter_pred_fn_t pred_funcs_##type[] = { \
184 filter_pred_LT_##type, \
185 filter_pred_LE_##type, \
186 filter_pred_GT_##type, \
187 filter_pred_GE_##type, \
188 filter_pred_BAND_##type, \
191 #define PRED_FUNC_START OP_LT
193 #define DEFINE_EQUALITY_PRED(size) \
194 static int filter_pred_##size(struct filter_pred *pred, void *event) \
196 u##size *addr = (u##size *)(event + pred->offset); \
197 u##size val = (u##size)pred->val; \
200 match = (val == *addr) ^ pred->not; \
205 DEFINE_COMPARISON_PRED(s64
);
206 DEFINE_COMPARISON_PRED(u64
);
207 DEFINE_COMPARISON_PRED(s32
);
208 DEFINE_COMPARISON_PRED(u32
);
209 DEFINE_COMPARISON_PRED(s16
);
210 DEFINE_COMPARISON_PRED(u16
);
211 DEFINE_COMPARISON_PRED(s8
);
212 DEFINE_COMPARISON_PRED(u8
);
214 DEFINE_EQUALITY_PRED(64);
215 DEFINE_EQUALITY_PRED(32);
216 DEFINE_EQUALITY_PRED(16);
217 DEFINE_EQUALITY_PRED(8);
219 /* Filter predicate for fixed sized arrays of characters */
220 static int filter_pred_string(struct filter_pred
*pred
, void *event
)
222 char *addr
= (char *)(event
+ pred
->offset
);
225 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
227 match
= cmp
^ pred
->not;
232 /* Filter predicate for char * pointers */
233 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
)
235 char **addr
= (char **)(event
+ pred
->offset
);
237 int len
= strlen(*addr
) + 1; /* including tailing '\0' */
239 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, len
);
241 match
= cmp
^ pred
->not;
247 * Filter predicate for dynamic sized arrays of characters.
248 * These are implemented through a list of strings at the end
250 * Also each of these strings have a field in the entry which
251 * contains its offset from the beginning of the entry.
252 * We have then first to get this field, dereference it
253 * and add it to the address of the entry, and at last we have
254 * the address of the string.
256 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
)
258 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
259 int str_loc
= str_item
& 0xffff;
260 int str_len
= str_item
>> 16;
261 char *addr
= (char *)(event
+ str_loc
);
264 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
266 match
= cmp
^ pred
->not;
271 /* Filter predicate for CPUs. */
272 static int filter_pred_cpu(struct filter_pred
*pred
, void *event
)
277 cpu
= raw_smp_processor_id();
300 return !!match
== !pred
->not;
303 /* Filter predicate for COMM. */
304 static int filter_pred_comm(struct filter_pred
*pred
, void *event
)
308 cmp
= pred
->regex
.match(current
->comm
, &pred
->regex
,
309 pred
->regex
.field_len
);
310 match
= cmp
^ pred
->not;
315 static int filter_pred_none(struct filter_pred
*pred
, void *event
)
321 * regex_match_foo - Basic regex callbacks
323 * @str: the string to be searched
324 * @r: the regex structure containing the pattern string
325 * @len: the length of the string to be searched (including '\0')
328 * - @str might not be NULL-terminated if it's of type DYN_STRING
332 static int regex_match_full(char *str
, struct regex
*r
, int len
)
334 if (strncmp(str
, r
->pattern
, len
) == 0)
339 static int regex_match_front(char *str
, struct regex
*r
, int len
)
344 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
349 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
351 if (strnstr(str
, r
->pattern
, len
))
356 static int regex_match_end(char *str
, struct regex
*r
, int len
)
358 int strlen
= len
- 1;
360 if (strlen
>= r
->len
&&
361 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
366 static int regex_match_glob(char *str
, struct regex
*r
, int len __maybe_unused
)
368 if (glob_match(r
->pattern
, str
))
373 * filter_parse_regex - parse a basic regex
374 * @buff: the raw regex
375 * @len: length of the regex
376 * @search: will point to the beginning of the string to compare
377 * @not: tell whether the match will have to be inverted
379 * This passes in a buffer containing a regex and this function will
380 * set search to point to the search part of the buffer and
381 * return the type of search it is (see enum above).
382 * This does modify buff.
385 * search returns the pointer to use for comparison.
386 * not returns 1 if buff started with a '!'
389 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
391 int type
= MATCH_FULL
;
394 if (buff
[0] == '!') {
403 for (i
= 0; i
< len
; i
++) {
404 if (buff
[i
] == '*') {
406 type
= MATCH_END_ONLY
;
407 } else if (i
== len
- 1) {
408 if (type
== MATCH_END_ONLY
)
409 type
= MATCH_MIDDLE_ONLY
;
411 type
= MATCH_FRONT_ONLY
;
414 } else { /* pattern continues, use full glob */
417 } else if (strchr("[?\\", buff
[i
])) {
427 static void filter_build_regex(struct filter_pred
*pred
)
429 struct regex
*r
= &pred
->regex
;
431 enum regex_type type
= MATCH_FULL
;
434 if (pred
->op
== OP_GLOB
) {
435 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
436 r
->len
= strlen(search
);
437 memmove(r
->pattern
, search
, r
->len
+1);
442 r
->match
= regex_match_full
;
444 case MATCH_FRONT_ONLY
:
445 r
->match
= regex_match_front
;
447 case MATCH_MIDDLE_ONLY
:
448 r
->match
= regex_match_middle
;
451 r
->match
= regex_match_end
;
454 r
->match
= regex_match_glob
;
467 static struct filter_pred
*
468 get_pred_parent(struct filter_pred
*pred
, struct filter_pred
*preds
,
469 int index
, enum move_type
*move
)
471 if (pred
->parent
& FILTER_PRED_IS_RIGHT
)
472 *move
= MOVE_UP_FROM_RIGHT
;
474 *move
= MOVE_UP_FROM_LEFT
;
475 pred
= &preds
[pred
->parent
& ~FILTER_PRED_IS_RIGHT
];
486 typedef int (*filter_pred_walkcb_t
) (enum move_type move
,
487 struct filter_pred
*pred
,
488 int *err
, void *data
);
490 static int walk_pred_tree(struct filter_pred
*preds
,
491 struct filter_pred
*root
,
492 filter_pred_walkcb_t cb
, void *data
)
494 struct filter_pred
*pred
= root
;
495 enum move_type move
= MOVE_DOWN
;
504 ret
= cb(move
, pred
, &err
, data
);
505 if (ret
== WALK_PRED_ABORT
)
507 if (ret
== WALK_PRED_PARENT
)
512 if (pred
->left
!= FILTER_PRED_INVALID
) {
513 pred
= &preds
[pred
->left
];
517 case MOVE_UP_FROM_LEFT
:
518 pred
= &preds
[pred
->right
];
521 case MOVE_UP_FROM_RIGHT
:
525 pred
= get_pred_parent(pred
, preds
,
538 * A series of AND or ORs where found together. Instead of
539 * climbing up and down the tree branches, an array of the
540 * ops were made in order of checks. We can just move across
541 * the array and short circuit if needed.
543 static int process_ops(struct filter_pred
*preds
,
544 struct filter_pred
*op
, void *rec
)
546 struct filter_pred
*pred
;
552 * Micro-optimization: We set type to true if op
553 * is an OR and false otherwise (AND). Then we
554 * just need to test if the match is equal to
555 * the type, and if it is, we can short circuit the
556 * rest of the checks:
558 * if ((match && op->op == OP_OR) ||
559 * (!match && op->op == OP_AND))
562 type
= op
->op
== OP_OR
;
564 for (i
= 0; i
< op
->val
; i
++) {
565 pred
= &preds
[op
->ops
[i
]];
566 if (!WARN_ON_ONCE(!pred
->fn
))
567 match
= pred
->fn(pred
, rec
);
571 /* If not of not match is equal to not of not, then it is a match */
572 return !!match
== !op
->not;
575 struct filter_match_preds_data
{
576 struct filter_pred
*preds
;
581 static int filter_match_preds_cb(enum move_type move
, struct filter_pred
*pred
,
582 int *err
, void *data
)
584 struct filter_match_preds_data
*d
= data
;
589 /* only AND and OR have children */
590 if (pred
->left
!= FILTER_PRED_INVALID
) {
591 /* If ops is set, then it was folded. */
593 return WALK_PRED_DEFAULT
;
594 /* We can treat folded ops as a leaf node */
595 d
->match
= process_ops(d
->preds
, pred
, d
->rec
);
597 if (!WARN_ON_ONCE(!pred
->fn
))
598 d
->match
= pred
->fn(pred
, d
->rec
);
601 return WALK_PRED_PARENT
;
602 case MOVE_UP_FROM_LEFT
:
604 * Check for short circuits.
606 * Optimization: !!match == (pred->op == OP_OR)
608 * if ((match && pred->op == OP_OR) ||
609 * (!match && pred->op == OP_AND))
611 if (!!d
->match
== (pred
->op
== OP_OR
))
612 return WALK_PRED_PARENT
;
614 case MOVE_UP_FROM_RIGHT
:
618 return WALK_PRED_DEFAULT
;
621 /* return 1 if event matches, 0 otherwise (discard) */
622 int filter_match_preds(struct event_filter
*filter
, void *rec
)
624 struct filter_pred
*preds
;
625 struct filter_pred
*root
;
626 struct filter_match_preds_data data
= {
627 /* match is currently meaningless */
633 /* no filter is considered a match */
637 n_preds
= filter
->n_preds
;
642 * n_preds, root and filter->preds are protect with preemption disabled.
644 root
= rcu_dereference_sched(filter
->root
);
648 data
.preds
= preds
= rcu_dereference_sched(filter
->preds
);
649 ret
= walk_pred_tree(preds
, root
, filter_match_preds_cb
, &data
);
653 EXPORT_SYMBOL_GPL(filter_match_preds
);
655 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
658 ps
->lasterr_pos
= pos
;
661 static void remove_filter_string(struct event_filter
*filter
)
666 kfree(filter
->filter_string
);
667 filter
->filter_string
= NULL
;
670 static int replace_filter_string(struct event_filter
*filter
,
673 kfree(filter
->filter_string
);
674 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
675 if (!filter
->filter_string
)
681 static int append_filter_string(struct event_filter
*filter
,
685 char *new_filter_string
;
687 BUG_ON(!filter
->filter_string
);
688 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
689 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
690 if (!new_filter_string
)
693 strcpy(new_filter_string
, filter
->filter_string
);
694 strcat(new_filter_string
, string
);
695 kfree(filter
->filter_string
);
696 filter
->filter_string
= new_filter_string
;
701 static void append_filter_err(struct filter_parse_state
*ps
,
702 struct event_filter
*filter
)
704 int pos
= ps
->lasterr_pos
;
707 buf
= (char *)__get_free_page(GFP_KERNEL
);
711 append_filter_string(filter
, "\n");
712 memset(buf
, ' ', PAGE_SIZE
);
713 if (pos
> PAGE_SIZE
- 128)
716 pbuf
= &buf
[pos
] + 1;
718 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
719 append_filter_string(filter
, buf
);
720 free_page((unsigned long) buf
);
723 static inline struct event_filter
*event_filter(struct trace_event_file
*file
)
728 /* caller must hold event_mutex */
729 void print_event_filter(struct trace_event_file
*file
, struct trace_seq
*s
)
731 struct event_filter
*filter
= event_filter(file
);
733 if (filter
&& filter
->filter_string
)
734 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
736 trace_seq_puts(s
, "none\n");
739 void print_subsystem_event_filter(struct event_subsystem
*system
,
742 struct event_filter
*filter
;
744 mutex_lock(&event_mutex
);
745 filter
= system
->filter
;
746 if (filter
&& filter
->filter_string
)
747 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
749 trace_seq_puts(s
, DEFAULT_SYS_FILTER_MESSAGE
"\n");
750 mutex_unlock(&event_mutex
);
753 static int __alloc_pred_stack(struct pred_stack
*stack
, int n_preds
)
755 stack
->preds
= kcalloc(n_preds
+ 1, sizeof(*stack
->preds
), GFP_KERNEL
);
758 stack
->index
= n_preds
;
762 static void __free_pred_stack(struct pred_stack
*stack
)
768 static int __push_pred_stack(struct pred_stack
*stack
,
769 struct filter_pred
*pred
)
771 int index
= stack
->index
;
773 if (WARN_ON(index
== 0))
776 stack
->preds
[--index
] = pred
;
777 stack
->index
= index
;
781 static struct filter_pred
*
782 __pop_pred_stack(struct pred_stack
*stack
)
784 struct filter_pred
*pred
;
785 int index
= stack
->index
;
787 pred
= stack
->preds
[index
++];
791 stack
->index
= index
;
795 static int filter_set_pred(struct event_filter
*filter
,
797 struct pred_stack
*stack
,
798 struct filter_pred
*src
)
800 struct filter_pred
*dest
= &filter
->preds
[idx
];
801 struct filter_pred
*left
;
802 struct filter_pred
*right
;
807 if (dest
->op
== OP_OR
|| dest
->op
== OP_AND
) {
808 right
= __pop_pred_stack(stack
);
809 left
= __pop_pred_stack(stack
);
813 * If both children can be folded
814 * and they are the same op as this op or a leaf,
815 * then this op can be folded.
817 if (left
->index
& FILTER_PRED_FOLD
&&
818 ((left
->op
== dest
->op
&& !left
->not) ||
819 left
->left
== FILTER_PRED_INVALID
) &&
820 right
->index
& FILTER_PRED_FOLD
&&
821 ((right
->op
== dest
->op
&& !right
->not) ||
822 right
->left
== FILTER_PRED_INVALID
))
823 dest
->index
|= FILTER_PRED_FOLD
;
825 dest
->left
= left
->index
& ~FILTER_PRED_FOLD
;
826 dest
->right
= right
->index
& ~FILTER_PRED_FOLD
;
827 left
->parent
= dest
->index
& ~FILTER_PRED_FOLD
;
828 right
->parent
= dest
->index
| FILTER_PRED_IS_RIGHT
;
831 * Make dest->left invalid to be used as a quick
832 * way to know this is a leaf node.
834 dest
->left
= FILTER_PRED_INVALID
;
836 /* All leafs allow folding the parent ops. */
837 dest
->index
|= FILTER_PRED_FOLD
;
840 return __push_pred_stack(stack
, dest
);
843 static void __free_preds(struct event_filter
*filter
)
848 for (i
= 0; i
< filter
->n_preds
; i
++)
849 kfree(filter
->preds
[i
].ops
);
850 kfree(filter
->preds
);
851 filter
->preds
= NULL
;
857 static void filter_disable(struct trace_event_file
*file
)
859 unsigned long old_flags
= file
->flags
;
861 file
->flags
&= ~EVENT_FILE_FL_FILTERED
;
863 if (old_flags
!= file
->flags
)
864 trace_buffered_event_disable();
867 static void __free_filter(struct event_filter
*filter
)
872 __free_preds(filter
);
873 kfree(filter
->filter_string
);
877 void free_event_filter(struct event_filter
*filter
)
879 __free_filter(filter
);
882 static struct event_filter
*__alloc_filter(void)
884 struct event_filter
*filter
;
886 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
890 static int __alloc_preds(struct event_filter
*filter
, int n_preds
)
892 struct filter_pred
*pred
;
896 __free_preds(filter
);
898 filter
->preds
= kcalloc(n_preds
, sizeof(*filter
->preds
), GFP_KERNEL
);
903 filter
->a_preds
= n_preds
;
906 for (i
= 0; i
< n_preds
; i
++) {
907 pred
= &filter
->preds
[i
];
908 pred
->fn
= filter_pred_none
;
914 static inline void __remove_filter(struct trace_event_file
*file
)
916 filter_disable(file
);
917 remove_filter_string(file
->filter
);
920 static void filter_free_subsystem_preds(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 __remove_filter(file
);
932 static inline void __free_subsystem_filter(struct trace_event_file
*file
)
934 __free_filter(file
->filter
);
938 static void filter_free_subsystem_filters(struct trace_subsystem_dir
*dir
,
939 struct trace_array
*tr
)
941 struct trace_event_file
*file
;
943 list_for_each_entry(file
, &tr
->events
, list
) {
944 if (file
->system
!= dir
)
946 __free_subsystem_filter(file
);
950 static int filter_add_pred(struct filter_parse_state
*ps
,
951 struct event_filter
*filter
,
952 struct filter_pred
*pred
,
953 struct pred_stack
*stack
)
957 if (WARN_ON(filter
->n_preds
== filter
->a_preds
)) {
958 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
962 err
= filter_set_pred(filter
, filter
->n_preds
, stack
, pred
);
971 int filter_assign_type(const char *type
)
973 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
974 return FILTER_DYN_STRING
;
976 if (strchr(type
, '[') && strstr(type
, "char"))
977 return FILTER_STATIC_STRING
;
982 static bool is_legal_op(struct ftrace_event_field
*field
, enum filter_op_ids op
)
984 if (is_string_field(field
) &&
985 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
987 if (!is_string_field(field
) && op
== OP_GLOB
)
993 static filter_pred_fn_t
select_comparison_fn(enum filter_op_ids op
,
994 int field_size
, int field_is_signed
)
996 filter_pred_fn_t fn
= NULL
;
998 switch (field_size
) {
1000 if (op
== OP_EQ
|| op
== OP_NE
)
1001 fn
= filter_pred_64
;
1002 else if (field_is_signed
)
1003 fn
= pred_funcs_s64
[op
- PRED_FUNC_START
];
1005 fn
= pred_funcs_u64
[op
- PRED_FUNC_START
];
1008 if (op
== OP_EQ
|| op
== OP_NE
)
1009 fn
= filter_pred_32
;
1010 else if (field_is_signed
)
1011 fn
= pred_funcs_s32
[op
- PRED_FUNC_START
];
1013 fn
= pred_funcs_u32
[op
- PRED_FUNC_START
];
1016 if (op
== OP_EQ
|| op
== OP_NE
)
1017 fn
= filter_pred_16
;
1018 else if (field_is_signed
)
1019 fn
= pred_funcs_s16
[op
- PRED_FUNC_START
];
1021 fn
= pred_funcs_u16
[op
- PRED_FUNC_START
];
1024 if (op
== OP_EQ
|| op
== OP_NE
)
1026 else if (field_is_signed
)
1027 fn
= pred_funcs_s8
[op
- PRED_FUNC_START
];
1029 fn
= pred_funcs_u8
[op
- PRED_FUNC_START
];
1036 static int init_pred(struct filter_parse_state
*ps
,
1037 struct ftrace_event_field
*field
,
1038 struct filter_pred
*pred
)
1041 filter_pred_fn_t fn
= filter_pred_none
;
1042 unsigned long long val
;
1045 pred
->offset
= field
->offset
;
1047 if (!is_legal_op(field
, pred
->op
)) {
1048 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
1052 if (field
->filter_type
== FILTER_COMM
) {
1053 filter_build_regex(pred
);
1054 fn
= filter_pred_comm
;
1055 pred
->regex
.field_len
= TASK_COMM_LEN
;
1056 } else if (is_string_field(field
)) {
1057 filter_build_regex(pred
);
1059 if (field
->filter_type
== FILTER_STATIC_STRING
) {
1060 fn
= filter_pred_string
;
1061 pred
->regex
.field_len
= field
->size
;
1062 } else if (field
->filter_type
== FILTER_DYN_STRING
)
1063 fn
= filter_pred_strloc
;
1065 fn
= filter_pred_pchar
;
1066 } else if (is_function_field(field
)) {
1067 if (strcmp(field
->name
, "ip")) {
1068 parse_error(ps
, FILT_ERR_IP_FIELD_ONLY
, 0);
1072 if (field
->is_signed
)
1073 ret
= kstrtoll(pred
->regex
.pattern
, 0, &val
);
1075 ret
= kstrtoull(pred
->regex
.pattern
, 0, &val
);
1077 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
1082 if (field
->filter_type
== FILTER_CPU
)
1083 fn
= filter_pred_cpu
;
1085 fn
= select_comparison_fn(pred
->op
, field
->size
,
1088 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1093 if (pred
->op
== OP_NE
)
1100 static void parse_init(struct filter_parse_state
*ps
,
1101 struct filter_op
*ops
,
1104 memset(ps
, '\0', sizeof(*ps
));
1106 ps
->infix
.string
= infix_string
;
1107 ps
->infix
.cnt
= strlen(infix_string
);
1110 INIT_LIST_HEAD(&ps
->opstack
);
1111 INIT_LIST_HEAD(&ps
->postfix
);
1114 static char infix_next(struct filter_parse_state
*ps
)
1121 return ps
->infix
.string
[ps
->infix
.tail
++];
1124 static char infix_peek(struct filter_parse_state
*ps
)
1126 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
1129 return ps
->infix
.string
[ps
->infix
.tail
];
1132 static void infix_advance(struct filter_parse_state
*ps
)
1141 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
1144 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
1147 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
1151 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1152 if (ps
->ops
[i
].string
[0] == c
)
1159 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
1161 char nextc
= infix_peek(ps
);
1169 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1170 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
1172 return ps
->ops
[i
].id
;
1178 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
1179 if (!strcmp(opstr
, ps
->ops
[i
].string
))
1180 return ps
->ops
[i
].id
;
1186 static inline void clear_operand_string(struct filter_parse_state
*ps
)
1188 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
1189 ps
->operand
.tail
= 0;
1192 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
1194 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
1197 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
1202 static int filter_opstack_push(struct filter_parse_state
*ps
,
1203 enum filter_op_ids op
)
1205 struct opstack_op
*opstack_op
;
1207 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
1211 opstack_op
->op
= op
;
1212 list_add(&opstack_op
->list
, &ps
->opstack
);
1217 static int filter_opstack_empty(struct filter_parse_state
*ps
)
1219 return list_empty(&ps
->opstack
);
1222 static int filter_opstack_top(struct filter_parse_state
*ps
)
1224 struct opstack_op
*opstack_op
;
1226 if (filter_opstack_empty(ps
))
1229 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1231 return opstack_op
->op
;
1234 static int filter_opstack_pop(struct filter_parse_state
*ps
)
1236 struct opstack_op
*opstack_op
;
1237 enum filter_op_ids op
;
1239 if (filter_opstack_empty(ps
))
1242 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
1243 op
= opstack_op
->op
;
1244 list_del(&opstack_op
->list
);
1251 static void filter_opstack_clear(struct filter_parse_state
*ps
)
1253 while (!filter_opstack_empty(ps
))
1254 filter_opstack_pop(ps
);
1257 static char *curr_operand(struct filter_parse_state
*ps
)
1259 return ps
->operand
.string
;
1262 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
1264 struct postfix_elt
*elt
;
1266 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1271 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1272 if (!elt
->operand
) {
1277 list_add_tail(&elt
->list
, &ps
->postfix
);
1282 static int postfix_append_op(struct filter_parse_state
*ps
, enum filter_op_ids op
)
1284 struct postfix_elt
*elt
;
1286 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1291 elt
->operand
= NULL
;
1293 list_add_tail(&elt
->list
, &ps
->postfix
);
1298 static void postfix_clear(struct filter_parse_state
*ps
)
1300 struct postfix_elt
*elt
;
1302 while (!list_empty(&ps
->postfix
)) {
1303 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1304 list_del(&elt
->list
);
1305 kfree(elt
->operand
);
1310 static int filter_parse(struct filter_parse_state
*ps
)
1312 enum filter_op_ids op
, top_op
;
1316 while ((ch
= infix_next(ps
))) {
1328 if (is_op_char(ps
, ch
)) {
1329 op
= infix_get_op(ps
, ch
);
1330 if (op
== OP_NONE
) {
1331 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1335 if (strlen(curr_operand(ps
))) {
1336 postfix_append_operand(ps
, curr_operand(ps
));
1337 clear_operand_string(ps
);
1340 while (!filter_opstack_empty(ps
)) {
1341 top_op
= filter_opstack_top(ps
);
1342 if (!is_precedence_lower(ps
, top_op
, op
)) {
1343 top_op
= filter_opstack_pop(ps
);
1344 postfix_append_op(ps
, top_op
);
1350 filter_opstack_push(ps
, op
);
1355 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1360 if (strlen(curr_operand(ps
))) {
1361 postfix_append_operand(ps
, curr_operand(ps
));
1362 clear_operand_string(ps
);
1365 top_op
= filter_opstack_pop(ps
);
1366 while (top_op
!= OP_NONE
) {
1367 if (top_op
== OP_OPEN_PAREN
)
1369 postfix_append_op(ps
, top_op
);
1370 top_op
= filter_opstack_pop(ps
);
1372 if (top_op
== OP_NONE
) {
1373 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1379 if (append_operand_char(ps
, ch
)) {
1380 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1385 if (strlen(curr_operand(ps
)))
1386 postfix_append_operand(ps
, curr_operand(ps
));
1388 while (!filter_opstack_empty(ps
)) {
1389 top_op
= filter_opstack_pop(ps
);
1390 if (top_op
== OP_NONE
)
1392 if (top_op
== OP_OPEN_PAREN
) {
1393 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1396 postfix_append_op(ps
, top_op
);
1402 static struct filter_pred
*create_pred(struct filter_parse_state
*ps
,
1403 struct trace_event_call
*call
,
1404 enum filter_op_ids op
,
1405 char *operand1
, char *operand2
)
1407 struct ftrace_event_field
*field
;
1408 static struct filter_pred pred
;
1410 memset(&pred
, 0, sizeof(pred
));
1413 if (op
== OP_AND
|| op
== OP_OR
)
1416 if (!operand1
|| !operand2
) {
1417 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1421 field
= trace_find_event_field(call
, operand1
);
1423 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
1427 strcpy(pred
.regex
.pattern
, operand2
);
1428 pred
.regex
.len
= strlen(pred
.regex
.pattern
);
1430 return init_pred(ps
, field
, &pred
) ? NULL
: &pred
;
1433 static int check_preds(struct filter_parse_state
*ps
)
1435 int n_normal_preds
= 0, n_logical_preds
= 0;
1436 struct postfix_elt
*elt
;
1439 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1440 if (elt
->op
== OP_NONE
) {
1445 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1450 if (elt
->op
!= OP_NOT
)
1453 /* all ops should have operands */
1458 if (cnt
!= 1 || !n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1459 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1466 static int count_preds(struct filter_parse_state
*ps
)
1468 struct postfix_elt
*elt
;
1471 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1472 if (elt
->op
== OP_NONE
)
1480 struct check_pred_data
{
1485 static int check_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1486 int *err
, void *data
)
1488 struct check_pred_data
*d
= data
;
1490 if (WARN_ON(d
->count
++ > d
->max
)) {
1492 return WALK_PRED_ABORT
;
1494 return WALK_PRED_DEFAULT
;
1498 * The tree is walked at filtering of an event. If the tree is not correctly
1499 * built, it may cause an infinite loop. Check here that the tree does
1502 static int check_pred_tree(struct event_filter
*filter
,
1503 struct filter_pred
*root
)
1505 struct check_pred_data data
= {
1507 * The max that we can hit a node is three times.
1508 * Once going down, once coming up from left, and
1509 * once coming up from right. This is more than enough
1510 * since leafs are only hit a single time.
1512 .max
= 3 * filter
->n_preds
,
1516 return walk_pred_tree(filter
->preds
, root
,
1517 check_pred_tree_cb
, &data
);
1520 static int count_leafs_cb(enum move_type move
, struct filter_pred
*pred
,
1521 int *err
, void *data
)
1525 if ((move
== MOVE_DOWN
) &&
1526 (pred
->left
== FILTER_PRED_INVALID
))
1529 return WALK_PRED_DEFAULT
;
1532 static int count_leafs(struct filter_pred
*preds
, struct filter_pred
*root
)
1536 ret
= walk_pred_tree(preds
, root
, count_leafs_cb
, &count
);
1541 struct fold_pred_data
{
1542 struct filter_pred
*root
;
1547 static int fold_pred_cb(enum move_type move
, struct filter_pred
*pred
,
1548 int *err
, void *data
)
1550 struct fold_pred_data
*d
= data
;
1551 struct filter_pred
*root
= d
->root
;
1553 if (move
!= MOVE_DOWN
)
1554 return WALK_PRED_DEFAULT
;
1555 if (pred
->left
!= FILTER_PRED_INVALID
)
1556 return WALK_PRED_DEFAULT
;
1558 if (WARN_ON(d
->count
== d
->children
)) {
1560 return WALK_PRED_ABORT
;
1563 pred
->index
&= ~FILTER_PRED_FOLD
;
1564 root
->ops
[d
->count
++] = pred
->index
;
1565 return WALK_PRED_DEFAULT
;
1568 static int fold_pred(struct filter_pred
*preds
, struct filter_pred
*root
)
1570 struct fold_pred_data data
= {
1576 /* No need to keep the fold flag */
1577 root
->index
&= ~FILTER_PRED_FOLD
;
1579 /* If the root is a leaf then do nothing */
1580 if (root
->left
== FILTER_PRED_INVALID
)
1583 /* count the children */
1584 children
= count_leafs(preds
, &preds
[root
->left
]);
1585 children
+= count_leafs(preds
, &preds
[root
->right
]);
1587 root
->ops
= kcalloc(children
, sizeof(*root
->ops
), GFP_KERNEL
);
1591 root
->val
= children
;
1592 data
.children
= children
;
1593 return walk_pred_tree(preds
, root
, fold_pred_cb
, &data
);
1596 static int fold_pred_tree_cb(enum move_type move
, struct filter_pred
*pred
,
1597 int *err
, void *data
)
1599 struct filter_pred
*preds
= data
;
1601 if (move
!= MOVE_DOWN
)
1602 return WALK_PRED_DEFAULT
;
1603 if (!(pred
->index
& FILTER_PRED_FOLD
))
1604 return WALK_PRED_DEFAULT
;
1606 *err
= fold_pred(preds
, pred
);
1608 return WALK_PRED_ABORT
;
1610 /* eveyrhing below is folded, continue with parent */
1611 return WALK_PRED_PARENT
;
1615 * To optimize the processing of the ops, if we have several "ors" or
1616 * "ands" together, we can put them in an array and process them all
1617 * together speeding up the filter logic.
1619 static int fold_pred_tree(struct event_filter
*filter
,
1620 struct filter_pred
*root
)
1622 return walk_pred_tree(filter
->preds
, root
, fold_pred_tree_cb
,
1626 static int replace_preds(struct trace_event_call
*call
,
1627 struct event_filter
*filter
,
1628 struct filter_parse_state
*ps
,
1631 char *operand1
= NULL
, *operand2
= NULL
;
1632 struct filter_pred
*pred
;
1633 struct filter_pred
*root
;
1634 struct postfix_elt
*elt
;
1635 struct pred_stack stack
= { }; /* init to NULL */
1639 n_preds
= count_preds(ps
);
1640 if (n_preds
>= MAX_FILTER_PRED
) {
1641 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1645 err
= check_preds(ps
);
1650 err
= __alloc_pred_stack(&stack
, n_preds
);
1653 err
= __alloc_preds(filter
, n_preds
);
1659 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1660 if (elt
->op
== OP_NONE
) {
1662 operand1
= elt
->operand
;
1664 operand2
= elt
->operand
;
1666 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1673 if (elt
->op
== OP_NOT
) {
1674 if (!n_preds
|| operand1
|| operand2
) {
1675 parse_error(ps
, FILT_ERR_ILLEGAL_NOT_OP
, 0);
1680 filter
->preds
[n_preds
- 1].not ^= 1;
1684 if (WARN_ON(n_preds
++ == MAX_FILTER_PRED
)) {
1685 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1690 pred
= create_pred(ps
, call
, elt
->op
, operand1
, operand2
);
1697 err
= filter_add_pred(ps
, filter
, pred
, &stack
);
1702 operand1
= operand2
= NULL
;
1706 /* We should have one item left on the stack */
1707 pred
= __pop_pred_stack(&stack
);
1710 /* This item is where we start from in matching */
1712 /* Make sure the stack is empty */
1713 pred
= __pop_pred_stack(&stack
);
1714 if (WARN_ON(pred
)) {
1716 filter
->root
= NULL
;
1719 err
= check_pred_tree(filter
, root
);
1723 /* Optimize the tree */
1724 err
= fold_pred_tree(filter
, root
);
1728 /* We don't set root until we know it works */
1730 filter
->root
= root
;
1735 __free_pred_stack(&stack
);
1739 static inline void event_set_filtered_flag(struct trace_event_file
*file
)
1741 unsigned long old_flags
= file
->flags
;
1743 file
->flags
|= EVENT_FILE_FL_FILTERED
;
1745 if (old_flags
!= file
->flags
)
1746 trace_buffered_event_enable();
1749 static inline void event_set_filter(struct trace_event_file
*file
,
1750 struct event_filter
*filter
)
1752 rcu_assign_pointer(file
->filter
, filter
);
1755 static inline void event_clear_filter(struct trace_event_file
*file
)
1757 RCU_INIT_POINTER(file
->filter
, NULL
);
1761 event_set_no_set_filter_flag(struct trace_event_file
*file
)
1763 file
->flags
|= EVENT_FILE_FL_NO_SET_FILTER
;
1767 event_clear_no_set_filter_flag(struct trace_event_file
*file
)
1769 file
->flags
&= ~EVENT_FILE_FL_NO_SET_FILTER
;
1773 event_no_set_filter_flag(struct trace_event_file
*file
)
1775 if (file
->flags
& EVENT_FILE_FL_NO_SET_FILTER
)
1781 struct filter_list
{
1782 struct list_head list
;
1783 struct event_filter
*filter
;
1786 static int replace_system_preds(struct trace_subsystem_dir
*dir
,
1787 struct trace_array
*tr
,
1788 struct filter_parse_state
*ps
,
1789 char *filter_string
)
1791 struct trace_event_file
*file
;
1792 struct filter_list
*filter_item
;
1793 struct filter_list
*tmp
;
1794 LIST_HEAD(filter_list
);
1798 list_for_each_entry(file
, &tr
->events
, list
) {
1799 if (file
->system
!= dir
)
1803 * Try to see if the filter can be applied
1804 * (filter arg is ignored on dry_run)
1806 err
= replace_preds(file
->event_call
, NULL
, ps
, true);
1808 event_set_no_set_filter_flag(file
);
1810 event_clear_no_set_filter_flag(file
);
1813 list_for_each_entry(file
, &tr
->events
, list
) {
1814 struct event_filter
*filter
;
1816 if (file
->system
!= dir
)
1819 if (event_no_set_filter_flag(file
))
1822 filter_item
= kzalloc(sizeof(*filter_item
), GFP_KERNEL
);
1826 list_add_tail(&filter_item
->list
, &filter_list
);
1828 filter_item
->filter
= __alloc_filter();
1829 if (!filter_item
->filter
)
1831 filter
= filter_item
->filter
;
1833 /* Can only fail on no memory */
1834 err
= replace_filter_string(filter
, filter_string
);
1838 err
= replace_preds(file
->event_call
, filter
, ps
, false);
1840 filter_disable(file
);
1841 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1842 append_filter_err(ps
, filter
);
1844 event_set_filtered_flag(file
);
1846 * Regardless of if this returned an error, we still
1847 * replace the filter for the call.
1849 filter
= event_filter(file
);
1850 event_set_filter(file
, filter_item
->filter
);
1851 filter_item
->filter
= filter
;
1860 * The calls can still be using the old filters.
1861 * Do a synchronize_sched() to ensure all calls are
1862 * done with them before we free them.
1864 synchronize_sched();
1865 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1866 __free_filter(filter_item
->filter
);
1867 list_del(&filter_item
->list
);
1872 /* No call succeeded */
1873 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1874 list_del(&filter_item
->list
);
1877 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1880 /* If any call succeeded, we still need to sync */
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
);
1891 static int create_filter_start(char *filter_str
, bool set_str
,
1892 struct filter_parse_state
**psp
,
1893 struct event_filter
**filterp
)
1895 struct event_filter
*filter
;
1896 struct filter_parse_state
*ps
= NULL
;
1899 WARN_ON_ONCE(*psp
|| *filterp
);
1901 /* allocate everything, and if any fails, free all and fail */
1902 filter
= __alloc_filter();
1903 if (filter
&& set_str
)
1904 err
= replace_filter_string(filter
, filter_str
);
1906 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1908 if (!filter
|| !ps
|| err
) {
1910 __free_filter(filter
);
1914 /* we're committed to creating a new filter */
1918 parse_init(ps
, filter_ops
, filter_str
);
1919 err
= filter_parse(ps
);
1921 append_filter_err(ps
, filter
);
1925 static void create_filter_finish(struct filter_parse_state
*ps
)
1928 filter_opstack_clear(ps
);
1935 * create_filter - create a filter for a trace_event_call
1936 * @call: trace_event_call to create a filter for
1937 * @filter_str: filter string
1938 * @set_str: remember @filter_str and enable detailed error in filter
1939 * @filterp: out param for created filter (always updated on return)
1941 * Creates a filter for @call with @filter_str. If @set_str is %true,
1942 * @filter_str is copied and recorded in the new filter.
1944 * On success, returns 0 and *@filterp points to the new filter. On
1945 * failure, returns -errno and *@filterp may point to %NULL or to a new
1946 * filter. In the latter case, the returned filter contains error
1947 * information if @set_str is %true and the caller is responsible for
1950 static int create_filter(struct trace_event_call
*call
,
1951 char *filter_str
, bool set_str
,
1952 struct event_filter
**filterp
)
1954 struct event_filter
*filter
= NULL
;
1955 struct filter_parse_state
*ps
= NULL
;
1958 err
= create_filter_start(filter_str
, set_str
, &ps
, &filter
);
1960 err
= replace_preds(call
, filter
, ps
, false);
1962 append_filter_err(ps
, filter
);
1964 if (err
&& !set_str
) {
1965 free_event_filter(filter
);
1968 create_filter_finish(ps
);
1974 int create_event_filter(struct trace_event_call
*call
,
1975 char *filter_str
, bool set_str
,
1976 struct event_filter
**filterp
)
1978 return create_filter(call
, filter_str
, set_str
, filterp
);
1982 * create_system_filter - create a filter for an event_subsystem
1983 * @system: event_subsystem to create a filter for
1984 * @filter_str: filter string
1985 * @filterp: out param for created filter (always updated on return)
1987 * Identical to create_filter() except that it creates a subsystem filter
1988 * and always remembers @filter_str.
1990 static int create_system_filter(struct trace_subsystem_dir
*dir
,
1991 struct trace_array
*tr
,
1992 char *filter_str
, struct event_filter
**filterp
)
1994 struct event_filter
*filter
= NULL
;
1995 struct filter_parse_state
*ps
= NULL
;
1998 err
= create_filter_start(filter_str
, true, &ps
, &filter
);
2000 err
= replace_system_preds(dir
, tr
, ps
, filter_str
);
2002 /* System filters just show a default message */
2003 kfree(filter
->filter_string
);
2004 filter
->filter_string
= NULL
;
2006 append_filter_err(ps
, filter
);
2009 create_filter_finish(ps
);
2015 /* caller must hold event_mutex */
2016 int apply_event_filter(struct trace_event_file
*file
, char *filter_string
)
2018 struct trace_event_call
*call
= file
->event_call
;
2019 struct event_filter
*filter
;
2022 if (!strcmp(strstrip(filter_string
), "0")) {
2023 filter_disable(file
);
2024 filter
= event_filter(file
);
2029 event_clear_filter(file
);
2031 /* Make sure the filter is not being used */
2032 synchronize_sched();
2033 __free_filter(filter
);
2038 err
= create_filter(call
, filter_string
, true, &filter
);
2041 * Always swap the call filter with the new filter
2042 * even if there was an error. If there was an error
2043 * in the filter, we disable the filter and show the error
2047 struct event_filter
*tmp
;
2049 tmp
= event_filter(file
);
2051 event_set_filtered_flag(file
);
2053 filter_disable(file
);
2055 event_set_filter(file
, filter
);
2058 /* Make sure the call is done with the filter */
2059 synchronize_sched();
2067 int apply_subsystem_event_filter(struct trace_subsystem_dir
*dir
,
2068 char *filter_string
)
2070 struct event_subsystem
*system
= dir
->subsystem
;
2071 struct trace_array
*tr
= dir
->tr
;
2072 struct event_filter
*filter
;
2075 mutex_lock(&event_mutex
);
2077 /* Make sure the system still has events */
2078 if (!dir
->nr_events
) {
2083 if (!strcmp(strstrip(filter_string
), "0")) {
2084 filter_free_subsystem_preds(dir
, tr
);
2085 remove_filter_string(system
->filter
);
2086 filter
= system
->filter
;
2087 system
->filter
= NULL
;
2088 /* Ensure all filters are no longer used */
2089 synchronize_sched();
2090 filter_free_subsystem_filters(dir
, tr
);
2091 __free_filter(filter
);
2095 err
= create_system_filter(dir
, tr
, filter_string
, &filter
);
2098 * No event actually uses the system filter
2099 * we can free it without synchronize_sched().
2101 __free_filter(system
->filter
);
2102 system
->filter
= filter
;
2105 mutex_unlock(&event_mutex
);
2110 #ifdef CONFIG_PERF_EVENTS
2112 void ftrace_profile_free_filter(struct perf_event
*event
)
2114 struct event_filter
*filter
= event
->filter
;
2116 event
->filter
= NULL
;
2117 __free_filter(filter
);
2120 struct function_filter_data
{
2121 struct ftrace_ops
*ops
;
2126 #ifdef CONFIG_FUNCTION_TRACER
2128 ftrace_function_filter_re(char *buf
, int len
, int *count
)
2132 str
= kstrndup(buf
, len
, GFP_KERNEL
);
2137 * The argv_split function takes white space
2138 * as a separator, so convert ',' into spaces.
2140 strreplace(str
, ',', ' ');
2142 re
= argv_split(GFP_KERNEL
, str
, count
);
2147 static int ftrace_function_set_regexp(struct ftrace_ops
*ops
, int filter
,
2148 int reset
, char *re
, int len
)
2153 ret
= ftrace_set_filter(ops
, re
, len
, reset
);
2155 ret
= ftrace_set_notrace(ops
, re
, len
, reset
);
2160 static int __ftrace_function_set_filter(int filter
, char *buf
, int len
,
2161 struct function_filter_data
*data
)
2163 int i
, re_cnt
, ret
= -EINVAL
;
2167 reset
= filter
? &data
->first_filter
: &data
->first_notrace
;
2170 * The 'ip' field could have multiple filters set, separated
2171 * either by space or comma. We first cut the filter and apply
2172 * all pieces separatelly.
2174 re
= ftrace_function_filter_re(buf
, len
, &re_cnt
);
2178 for (i
= 0; i
< re_cnt
; i
++) {
2179 ret
= ftrace_function_set_regexp(data
->ops
, filter
, *reset
,
2180 re
[i
], strlen(re
[i
]));
2192 static int ftrace_function_check_pred(struct filter_pred
*pred
, int leaf
)
2194 struct ftrace_event_field
*field
= pred
->field
;
2198 * Check the leaf predicate for function trace, verify:
2199 * - only '==' and '!=' is used
2200 * - the 'ip' field is used
2202 if ((pred
->op
!= OP_EQ
) && (pred
->op
!= OP_NE
))
2205 if (strcmp(field
->name
, "ip"))
2209 * Check the non leaf predicate for function trace, verify:
2210 * - only '||' is used
2212 if (pred
->op
!= OP_OR
)
2219 static int ftrace_function_set_filter_cb(enum move_type move
,
2220 struct filter_pred
*pred
,
2221 int *err
, void *data
)
2223 /* Checking the node is valid for function trace. */
2224 if ((move
!= MOVE_DOWN
) ||
2225 (pred
->left
!= FILTER_PRED_INVALID
)) {
2226 *err
= ftrace_function_check_pred(pred
, 0);
2228 *err
= ftrace_function_check_pred(pred
, 1);
2230 return WALK_PRED_ABORT
;
2232 *err
= __ftrace_function_set_filter(pred
->op
== OP_EQ
,
2233 pred
->regex
.pattern
,
2238 return (*err
) ? WALK_PRED_ABORT
: WALK_PRED_DEFAULT
;
2241 static int ftrace_function_set_filter(struct perf_event
*event
,
2242 struct event_filter
*filter
)
2244 struct function_filter_data data
= {
2247 .ops
= &event
->ftrace_ops
,
2250 return walk_pred_tree(filter
->preds
, filter
->root
,
2251 ftrace_function_set_filter_cb
, &data
);
2254 static int ftrace_function_set_filter(struct perf_event
*event
,
2255 struct event_filter
*filter
)
2259 #endif /* CONFIG_FUNCTION_TRACER */
2261 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
2265 struct event_filter
*filter
;
2266 struct trace_event_call
*call
;
2268 mutex_lock(&event_mutex
);
2270 call
= event
->tp_event
;
2280 err
= create_filter(call
, filter_str
, false, &filter
);
2284 if (ftrace_event_is_function(call
))
2285 err
= ftrace_function_set_filter(event
, filter
);
2287 event
->filter
= filter
;
2290 if (err
|| ftrace_event_is_function(call
))
2291 __free_filter(filter
);
2294 mutex_unlock(&event_mutex
);
2299 #endif /* CONFIG_PERF_EVENTS */
2301 #ifdef CONFIG_FTRACE_STARTUP_TEST
2303 #include <linux/types.h>
2304 #include <linux/tracepoint.h>
2306 #define CREATE_TRACE_POINTS
2307 #include "trace_events_filter_test.h"
2309 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2312 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2313 .e = ve, .f = vf, .g = vg, .h = vh }, \
2315 .not_visited = nvisit, \
2320 static struct test_filter_data_t
{
2322 struct trace_event_raw_ftrace_test_filter rec
;
2325 } test_filter_data
[] = {
2326 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2327 "e == 1 && f == 1 && g == 1 && h == 1"
2328 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2329 DATA_REC(NO
, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2330 DATA_REC(NO
, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2332 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2333 "e == 1 || f == 1 || g == 1 || h == 1"
2334 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2335 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2336 DATA_REC(YES
, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2338 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2339 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2340 DATA_REC(NO
, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2341 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2342 DATA_REC(YES
, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2343 DATA_REC(NO
, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2345 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2346 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2347 DATA_REC(YES
, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2348 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2349 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2351 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2352 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2353 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2354 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2355 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2357 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2358 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2359 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2360 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2361 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2363 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2364 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2365 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2366 DATA_REC(NO
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2367 DATA_REC(NO
, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2369 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2370 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2371 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2372 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2373 DATA_REC(YES
, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2381 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2383 static int test_pred_visited
;
2385 static int test_pred_visited_fn(struct filter_pred
*pred
, void *event
)
2387 struct ftrace_event_field
*field
= pred
->field
;
2389 test_pred_visited
= 1;
2390 printk(KERN_INFO
"\npred visited %s\n", field
->name
);
2394 static int test_walk_pred_cb(enum move_type move
, struct filter_pred
*pred
,
2395 int *err
, void *data
)
2397 char *fields
= data
;
2399 if ((move
== MOVE_DOWN
) &&
2400 (pred
->left
== FILTER_PRED_INVALID
)) {
2401 struct ftrace_event_field
*field
= pred
->field
;
2404 WARN(1, "all leafs should have field defined");
2405 return WALK_PRED_DEFAULT
;
2407 if (!strchr(fields
, *field
->name
))
2408 return WALK_PRED_DEFAULT
;
2411 pred
->fn
= test_pred_visited_fn
;
2413 return WALK_PRED_DEFAULT
;
2416 static __init
int ftrace_test_event_filter(void)
2420 printk(KERN_INFO
"Testing ftrace filter: ");
2422 for (i
= 0; i
< DATA_CNT
; i
++) {
2423 struct event_filter
*filter
= NULL
;
2424 struct test_filter_data_t
*d
= &test_filter_data
[i
];
2427 err
= create_filter(&event_ftrace_test_filter
, d
->filter
,
2431 "Failed to get filter for '%s', err %d\n",
2433 __free_filter(filter
);
2438 * The preemption disabling is not really needed for self
2439 * tests, but the rcu dereference will complain without it.
2442 if (*d
->not_visited
)
2443 walk_pred_tree(filter
->preds
, filter
->root
,
2447 test_pred_visited
= 0;
2448 err
= filter_match_preds(filter
, &d
->rec
);
2451 __free_filter(filter
);
2453 if (test_pred_visited
) {
2455 "Failed, unwanted pred visited for filter %s\n",
2460 if (err
!= d
->match
) {
2462 "Failed to match filter '%s', expected %d\n",
2463 d
->filter
, d
->match
);
2469 printk(KERN_CONT
"OK\n");
2474 late_initcall(ftrace_test_event_filter
);
2476 #endif /* CONFIG_FTRACE_STARTUP_TEST */