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/debugfs.h>
22 #include <linux/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/ctype.h>
25 #include <linux/mutex.h>
28 #include "trace_output.h"
30 static DEFINE_MUTEX(filter_mutex
);
52 static struct filter_op filter_ops
[] = {
61 { OP_NONE
, "OP_NONE", 0 },
62 { OP_OPEN_PAREN
, "(", 0 },
68 FILT_ERR_UNBALANCED_PAREN
,
69 FILT_ERR_TOO_MANY_OPERANDS
,
70 FILT_ERR_OPERAND_TOO_LONG
,
71 FILT_ERR_FIELD_NOT_FOUND
,
72 FILT_ERR_ILLEGAL_FIELD_OP
,
73 FILT_ERR_ILLEGAL_INTVAL
,
74 FILT_ERR_BAD_SUBSYS_FILTER
,
75 FILT_ERR_TOO_MANY_PREDS
,
76 FILT_ERR_MISSING_FIELD
,
77 FILT_ERR_INVALID_FILTER
,
80 static char *err_text
[] = {
87 "Illegal operation for field type",
88 "Illegal integer value",
89 "Couldn't find or set field in one of a subsystem's events",
90 "Too many terms in predicate expression",
91 "Missing field name and/or value",
92 "Meaningless filter expression",
97 struct list_head list
;
103 struct list_head list
;
106 struct filter_parse_state
{
107 struct filter_op
*ops
;
108 struct list_head opstack
;
109 struct list_head postfix
;
120 char string
[MAX_FILTER_STR_VAL
];
126 DEFINE_COMPARISON_PRED(s64
);
127 DEFINE_COMPARISON_PRED(u64
);
128 DEFINE_COMPARISON_PRED(s32
);
129 DEFINE_COMPARISON_PRED(u32
);
130 DEFINE_COMPARISON_PRED(s16
);
131 DEFINE_COMPARISON_PRED(u16
);
132 DEFINE_COMPARISON_PRED(s8
);
133 DEFINE_COMPARISON_PRED(u8
);
135 DEFINE_EQUALITY_PRED(64);
136 DEFINE_EQUALITY_PRED(32);
137 DEFINE_EQUALITY_PRED(16);
138 DEFINE_EQUALITY_PRED(8);
140 static int filter_pred_and(struct filter_pred
*pred
__attribute((unused
)),
141 void *event
__attribute((unused
)),
147 static int filter_pred_or(struct filter_pred
*pred
__attribute((unused
)),
148 void *event
__attribute((unused
)),
154 static int filter_pred_string(struct filter_pred
*pred
, void *event
,
157 char *addr
= (char *)(event
+ pred
->offset
);
160 cmp
= strncmp(addr
, pred
->str_val
, pred
->str_len
);
162 match
= (!cmp
) ^ pred
->not;
167 static int filter_pred_none(struct filter_pred
*pred
, void *event
,
173 /* return 1 if event matches, 0 otherwise (discard) */
174 int filter_match_preds(struct ftrace_event_call
*call
, void *rec
)
176 struct event_filter
*filter
= call
->filter
;
177 int match
, top
= 0, val1
= 0, val2
= 0;
178 int stack
[MAX_FILTER_PRED
];
179 struct filter_pred
*pred
;
182 for (i
= 0; i
< filter
->n_preds
; i
++) {
183 pred
= filter
->preds
[i
];
185 match
= pred
->fn(pred
, rec
, val1
, val2
);
186 stack
[top
++] = match
;
189 if (pred
->pop_n
> top
) {
195 match
= pred
->fn(pred
, rec
, val1
, val2
);
196 stack
[top
++] = match
;
201 EXPORT_SYMBOL_GPL(filter_match_preds
);
203 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
206 ps
->lasterr_pos
= pos
;
209 static void remove_filter_string(struct event_filter
*filter
)
211 kfree(filter
->filter_string
);
212 filter
->filter_string
= NULL
;
215 static int replace_filter_string(struct event_filter
*filter
,
218 kfree(filter
->filter_string
);
219 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
220 if (!filter
->filter_string
)
226 static int append_filter_string(struct event_filter
*filter
,
230 char *new_filter_string
;
232 BUG_ON(!filter
->filter_string
);
233 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
234 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
235 if (!new_filter_string
)
238 strcpy(new_filter_string
, filter
->filter_string
);
239 strcat(new_filter_string
, string
);
240 kfree(filter
->filter_string
);
241 filter
->filter_string
= new_filter_string
;
246 static void append_filter_err(struct filter_parse_state
*ps
,
247 struct event_filter
*filter
)
249 int pos
= ps
->lasterr_pos
;
252 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
256 append_filter_string(filter
, "\n");
257 memset(buf
, ' ', PAGE_SIZE
);
258 if (pos
> PAGE_SIZE
- 128)
261 pbuf
= &buf
[pos
] + 1;
263 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
264 append_filter_string(filter
, buf
);
265 free_page((unsigned long) buf
);
268 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
270 struct event_filter
*filter
= call
->filter
;
272 mutex_lock(&filter_mutex
);
273 if (filter
->filter_string
)
274 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
276 trace_seq_printf(s
, "none\n");
277 mutex_unlock(&filter_mutex
);
280 void print_subsystem_event_filter(struct event_subsystem
*system
,
283 struct event_filter
*filter
= system
->filter
;
285 mutex_lock(&filter_mutex
);
286 if (filter
->filter_string
)
287 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
289 trace_seq_printf(s
, "none\n");
290 mutex_unlock(&filter_mutex
);
293 static struct ftrace_event_field
*
294 find_event_field(struct ftrace_event_call
*call
, char *name
)
296 struct ftrace_event_field
*field
;
298 list_for_each_entry(field
, &call
->fields
, link
) {
299 if (!strcmp(field
->name
, name
))
306 static void filter_free_pred(struct filter_pred
*pred
)
311 kfree(pred
->field_name
);
315 static void filter_clear_pred(struct filter_pred
*pred
)
317 kfree(pred
->field_name
);
318 pred
->field_name
= NULL
;
322 static int filter_set_pred(struct filter_pred
*dest
,
323 struct filter_pred
*src
,
327 if (src
->field_name
) {
328 dest
->field_name
= kstrdup(src
->field_name
, GFP_KERNEL
);
329 if (!dest
->field_name
)
337 static void filter_disable_preds(struct ftrace_event_call
*call
)
339 struct event_filter
*filter
= call
->filter
;
342 call
->filter_active
= 0;
345 for (i
= 0; i
< MAX_FILTER_PRED
; i
++)
346 filter
->preds
[i
]->fn
= filter_pred_none
;
349 int init_preds(struct ftrace_event_call
*call
)
351 struct event_filter
*filter
;
352 struct filter_pred
*pred
;
355 filter
= call
->filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
359 call
->filter_active
= 0;
362 filter
->preds
= kzalloc(MAX_FILTER_PRED
* sizeof(pred
), GFP_KERNEL
);
366 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
367 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
370 pred
->fn
= filter_pred_none
;
371 filter
->preds
[i
] = pred
;
377 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
378 if (filter
->preds
[i
])
379 filter_free_pred(filter
->preds
[i
]);
381 kfree(filter
->preds
);
387 EXPORT_SYMBOL_GPL(init_preds
);
389 static void filter_free_subsystem_preds(struct event_subsystem
*system
)
391 struct event_filter
*filter
= system
->filter
;
392 struct ftrace_event_call
*call
;
395 if (filter
->n_preds
) {
396 for (i
= 0; i
< filter
->n_preds
; i
++)
397 filter_free_pred(filter
->preds
[i
]);
398 kfree(filter
->preds
);
399 filter
->preds
= NULL
;
403 list_for_each_entry(call
, &ftrace_events
, list
) {
404 if (!call
->define_fields
)
407 if (!strcmp(call
->system
, system
->name
)) {
408 filter_disable_preds(call
);
409 remove_filter_string(call
->filter
);
414 static int filter_add_pred_fn(struct filter_parse_state
*ps
,
415 struct ftrace_event_call
*call
,
416 struct filter_pred
*pred
,
419 struct event_filter
*filter
= call
->filter
;
422 if (filter
->n_preds
== MAX_FILTER_PRED
) {
423 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
427 idx
= filter
->n_preds
;
428 filter_clear_pred(filter
->preds
[idx
]);
429 err
= filter_set_pred(filter
->preds
[idx
], pred
, fn
);
434 call
->filter_active
= 1;
439 static int is_string_field(const char *type
)
441 if (strchr(type
, '[') && strstr(type
, "char"))
447 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
449 if (is_string_field(field
->type
) && (op
!= OP_EQ
&& op
!= OP_NE
))
455 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
458 filter_pred_fn_t fn
= NULL
;
460 switch (field_size
) {
462 if (op
== OP_EQ
|| op
== OP_NE
)
464 else if (field_is_signed
)
465 fn
= filter_pred_s64
;
467 fn
= filter_pred_u64
;
470 if (op
== OP_EQ
|| op
== OP_NE
)
472 else if (field_is_signed
)
473 fn
= filter_pred_s32
;
475 fn
= filter_pred_u32
;
478 if (op
== OP_EQ
|| op
== OP_NE
)
480 else if (field_is_signed
)
481 fn
= filter_pred_s16
;
483 fn
= filter_pred_u16
;
486 if (op
== OP_EQ
|| op
== OP_NE
)
488 else if (field_is_signed
)
498 static int filter_add_pred(struct filter_parse_state
*ps
,
499 struct ftrace_event_call
*call
,
500 struct filter_pred
*pred
)
502 struct ftrace_event_field
*field
;
504 unsigned long long val
;
506 pred
->fn
= filter_pred_none
;
508 if (pred
->op
== OP_AND
) {
510 return filter_add_pred_fn(ps
, call
, pred
, filter_pred_and
);
511 } else if (pred
->op
== OP_OR
) {
513 return filter_add_pred_fn(ps
, call
, pred
, filter_pred_or
);
516 field
= find_event_field(call
, pred
->field_name
);
518 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
522 pred
->offset
= field
->offset
;
524 if (!is_legal_op(field
, pred
->op
)) {
525 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
529 if (is_string_field(field
->type
)) {
530 fn
= filter_pred_string
;
531 pred
->str_len
= field
->size
;
532 if (pred
->op
== OP_NE
)
534 return filter_add_pred_fn(ps
, call
, pred
, fn
);
536 if (strict_strtoull(pred
->str_val
, 0, &val
)) {
537 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
543 fn
= select_comparison_fn(pred
->op
, field
->size
, field
->is_signed
);
545 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
549 if (pred
->op
== OP_NE
)
552 return filter_add_pred_fn(ps
, call
, pred
, fn
);
555 static int filter_add_subsystem_pred(struct filter_parse_state
*ps
,
556 struct event_subsystem
*system
,
557 struct filter_pred
*pred
,
560 struct event_filter
*filter
= system
->filter
;
561 struct ftrace_event_call
*call
;
563 if (!filter
->preds
) {
564 filter
->preds
= kzalloc(MAX_FILTER_PRED
* sizeof(pred
),
571 if (filter
->n_preds
== MAX_FILTER_PRED
) {
572 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
576 filter
->preds
[filter
->n_preds
] = pred
;
579 list_for_each_entry(call
, &ftrace_events
, list
) {
582 if (!call
->define_fields
)
585 if (strcmp(call
->system
, system
->name
))
588 err
= filter_add_pred(ps
, call
, pred
);
590 filter_free_subsystem_preds(system
);
591 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
594 replace_filter_string(call
->filter
, filter_string
);
600 static void parse_init(struct filter_parse_state
*ps
,
601 struct filter_op
*ops
,
604 memset(ps
, '\0', sizeof(*ps
));
606 ps
->infix
.string
= infix_string
;
607 ps
->infix
.cnt
= strlen(infix_string
);
610 INIT_LIST_HEAD(&ps
->opstack
);
611 INIT_LIST_HEAD(&ps
->postfix
);
614 static char infix_next(struct filter_parse_state
*ps
)
618 return ps
->infix
.string
[ps
->infix
.tail
++];
621 static char infix_peek(struct filter_parse_state
*ps
)
623 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
626 return ps
->infix
.string
[ps
->infix
.tail
];
629 static void infix_advance(struct filter_parse_state
*ps
)
635 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
638 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
641 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
645 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
646 if (ps
->ops
[i
].string
[0] == c
)
653 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
655 char nextc
= infix_peek(ps
);
663 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
664 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
666 return ps
->ops
[i
].id
;
672 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
673 if (!strcmp(opstr
, ps
->ops
[i
].string
))
674 return ps
->ops
[i
].id
;
680 static inline void clear_operand_string(struct filter_parse_state
*ps
)
682 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
683 ps
->operand
.tail
= 0;
686 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
688 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
)
691 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
696 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
698 struct opstack_op
*opstack_op
;
700 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
705 list_add(&opstack_op
->list
, &ps
->opstack
);
710 static int filter_opstack_empty(struct filter_parse_state
*ps
)
712 return list_empty(&ps
->opstack
);
715 static int filter_opstack_top(struct filter_parse_state
*ps
)
717 struct opstack_op
*opstack_op
;
719 if (filter_opstack_empty(ps
))
722 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
724 return opstack_op
->op
;
727 static int filter_opstack_pop(struct filter_parse_state
*ps
)
729 struct opstack_op
*opstack_op
;
732 if (filter_opstack_empty(ps
))
735 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
737 list_del(&opstack_op
->list
);
744 static void filter_opstack_clear(struct filter_parse_state
*ps
)
746 while (!filter_opstack_empty(ps
))
747 filter_opstack_pop(ps
);
750 static char *curr_operand(struct filter_parse_state
*ps
)
752 return ps
->operand
.string
;
755 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
757 struct postfix_elt
*elt
;
759 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
764 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
770 list_add_tail(&elt
->list
, &ps
->postfix
);
775 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
777 struct postfix_elt
*elt
;
779 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
786 list_add_tail(&elt
->list
, &ps
->postfix
);
791 static void postfix_clear(struct filter_parse_state
*ps
)
793 struct postfix_elt
*elt
;
795 while (!list_empty(&ps
->postfix
)) {
796 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
798 list_del(&elt
->list
);
802 static int filter_parse(struct filter_parse_state
*ps
)
807 while ((ch
= infix_next(ps
))) {
811 if (is_op_char(ps
, ch
)) {
812 op
= infix_get_op(ps
, ch
);
814 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
818 if (strlen(curr_operand(ps
))) {
819 postfix_append_operand(ps
, curr_operand(ps
));
820 clear_operand_string(ps
);
823 while (!filter_opstack_empty(ps
)) {
824 top_op
= filter_opstack_top(ps
);
825 if (!is_precedence_lower(ps
, top_op
, op
)) {
826 top_op
= filter_opstack_pop(ps
);
827 postfix_append_op(ps
, top_op
);
833 filter_opstack_push(ps
, op
);
838 filter_opstack_push(ps
, OP_OPEN_PAREN
);
843 if (strlen(curr_operand(ps
))) {
844 postfix_append_operand(ps
, curr_operand(ps
));
845 clear_operand_string(ps
);
848 top_op
= filter_opstack_pop(ps
);
849 while (top_op
!= OP_NONE
) {
850 if (top_op
== OP_OPEN_PAREN
)
852 postfix_append_op(ps
, top_op
);
853 top_op
= filter_opstack_pop(ps
);
855 if (top_op
== OP_NONE
) {
856 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
861 if (append_operand_char(ps
, ch
)) {
862 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
867 if (strlen(curr_operand(ps
)))
868 postfix_append_operand(ps
, curr_operand(ps
));
870 while (!filter_opstack_empty(ps
)) {
871 top_op
= filter_opstack_pop(ps
);
872 if (top_op
== OP_NONE
)
874 if (top_op
== OP_OPEN_PAREN
) {
875 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
878 postfix_append_op(ps
, top_op
);
884 static struct filter_pred
*create_pred(int op
, char *operand1
, char *operand2
)
886 struct filter_pred
*pred
;
888 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
892 pred
->field_name
= kstrdup(operand1
, GFP_KERNEL
);
893 if (!pred
->field_name
) {
898 strcpy(pred
->str_val
, operand2
);
899 pred
->str_len
= strlen(operand2
);
906 static struct filter_pred
*create_logical_pred(int op
)
908 struct filter_pred
*pred
;
910 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
919 static int check_preds(struct filter_parse_state
*ps
)
921 int n_normal_preds
= 0, n_logical_preds
= 0;
922 struct postfix_elt
*elt
;
924 list_for_each_entry(elt
, &ps
->postfix
, list
) {
925 if (elt
->op
== OP_NONE
)
928 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
935 if (!n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
936 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
943 static int replace_preds(struct event_subsystem
*system
,
944 struct ftrace_event_call
*call
,
945 struct filter_parse_state
*ps
,
948 char *operand1
= NULL
, *operand2
= NULL
;
949 struct filter_pred
*pred
;
950 struct postfix_elt
*elt
;
953 err
= check_preds(ps
);
957 list_for_each_entry(elt
, &ps
->postfix
, list
) {
958 if (elt
->op
== OP_NONE
) {
960 operand1
= elt
->operand
;
962 operand2
= elt
->operand
;
964 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
970 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
971 pred
= create_logical_pred(elt
->op
);
973 err
= filter_add_pred(ps
, call
, pred
);
974 filter_free_pred(pred
);
976 err
= filter_add_subsystem_pred(ps
, system
,
977 pred
, filter_string
);
981 operand1
= operand2
= NULL
;
985 if (!operand1
|| !operand2
) {
986 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
990 pred
= create_pred(elt
->op
, operand1
, operand2
);
992 err
= filter_add_pred(ps
, call
, pred
);
993 filter_free_pred(pred
);
995 err
= filter_add_subsystem_pred(ps
, system
, pred
,
1000 operand1
= operand2
= NULL
;
1006 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1010 struct filter_parse_state
*ps
;
1012 mutex_lock(&filter_mutex
);
1014 if (!strcmp(strstrip(filter_string
), "0")) {
1015 filter_disable_preds(call
);
1016 remove_filter_string(call
->filter
);
1017 mutex_unlock(&filter_mutex
);
1021 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1025 filter_disable_preds(call
);
1026 replace_filter_string(call
->filter
, filter_string
);
1028 parse_init(ps
, filter_ops
, filter_string
);
1029 err
= filter_parse(ps
);
1031 append_filter_err(ps
, call
->filter
);
1035 err
= replace_preds(NULL
, call
, ps
, filter_string
);
1037 append_filter_err(ps
, call
->filter
);
1040 filter_opstack_clear(ps
);
1044 mutex_unlock(&filter_mutex
);
1049 int apply_subsystem_event_filter(struct event_subsystem
*system
,
1050 char *filter_string
)
1054 struct filter_parse_state
*ps
;
1056 mutex_lock(&filter_mutex
);
1058 if (!strcmp(strstrip(filter_string
), "0")) {
1059 filter_free_subsystem_preds(system
);
1060 remove_filter_string(system
->filter
);
1061 mutex_unlock(&filter_mutex
);
1065 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1069 filter_free_subsystem_preds(system
);
1070 replace_filter_string(system
->filter
, filter_string
);
1072 parse_init(ps
, filter_ops
, filter_string
);
1073 err
= filter_parse(ps
);
1075 append_filter_err(ps
, system
->filter
);
1079 err
= replace_preds(system
, NULL
, ps
, filter_string
);
1081 append_filter_err(ps
, system
->filter
);
1084 filter_opstack_clear(ps
);
1088 mutex_unlock(&filter_mutex
);