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>
27 #include "trace_output.h"
50 static struct filter_op filter_ops
[] = {
60 { OP_NONE
, "OP_NONE", 0 },
61 { OP_OPEN_PAREN
, "(", 0 },
67 FILT_ERR_UNBALANCED_PAREN
,
68 FILT_ERR_TOO_MANY_OPERANDS
,
69 FILT_ERR_OPERAND_TOO_LONG
,
70 FILT_ERR_FIELD_NOT_FOUND
,
71 FILT_ERR_ILLEGAL_FIELD_OP
,
72 FILT_ERR_ILLEGAL_INTVAL
,
73 FILT_ERR_BAD_SUBSYS_FILTER
,
74 FILT_ERR_TOO_MANY_PREDS
,
75 FILT_ERR_MISSING_FIELD
,
76 FILT_ERR_INVALID_FILTER
,
79 static char *err_text
[] = {
86 "Illegal operation for field type",
87 "Illegal integer value",
88 "Couldn't find or set field in one of a subsystem's events",
89 "Too many terms in predicate expression",
90 "Missing field name and/or value",
91 "Meaningless filter expression",
96 struct list_head list
;
102 struct list_head list
;
105 struct filter_parse_state
{
106 struct filter_op
*ops
;
107 struct list_head opstack
;
108 struct list_head postfix
;
119 char string
[MAX_FILTER_STR_VAL
];
125 #define DEFINE_COMPARISON_PRED(type) \
126 static int filter_pred_##type(struct filter_pred *pred, void *event, \
127 int val1, int val2) \
129 type *addr = (type *)(event + pred->offset); \
130 type val = (type)pred->val; \
133 switch (pred->op) { \
135 match = (*addr < val); \
138 match = (*addr <= val); \
141 match = (*addr > val); \
144 match = (*addr >= val); \
153 #define DEFINE_EQUALITY_PRED(size) \
154 static int filter_pred_##size(struct filter_pred *pred, void *event, \
155 int val1, int val2) \
157 u##size *addr = (u##size *)(event + pred->offset); \
158 u##size val = (u##size)pred->val; \
161 match = (val == *addr) ^ pred->not; \
166 DEFINE_COMPARISON_PRED(s64
);
167 DEFINE_COMPARISON_PRED(u64
);
168 DEFINE_COMPARISON_PRED(s32
);
169 DEFINE_COMPARISON_PRED(u32
);
170 DEFINE_COMPARISON_PRED(s16
);
171 DEFINE_COMPARISON_PRED(u16
);
172 DEFINE_COMPARISON_PRED(s8
);
173 DEFINE_COMPARISON_PRED(u8
);
175 DEFINE_EQUALITY_PRED(64);
176 DEFINE_EQUALITY_PRED(32);
177 DEFINE_EQUALITY_PRED(16);
178 DEFINE_EQUALITY_PRED(8);
180 static int filter_pred_and(struct filter_pred
*pred
__attribute((unused
)),
181 void *event
__attribute((unused
)),
187 static int filter_pred_or(struct filter_pred
*pred
__attribute((unused
)),
188 void *event
__attribute((unused
)),
194 /* Filter predicate for fixed sized arrays of characters */
195 static int filter_pred_string(struct filter_pred
*pred
, void *event
,
198 char *addr
= (char *)(event
+ pred
->offset
);
201 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
203 match
= cmp
^ pred
->not;
208 /* Filter predicate for char * pointers */
209 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
,
212 char **addr
= (char **)(event
+ pred
->offset
);
214 int len
= strlen(*addr
) + 1; /* including tailing '\0' */
216 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, len
);
218 match
= cmp
^ pred
->not;
224 * Filter predicate for dynamic sized arrays of characters.
225 * These are implemented through a list of strings at the end
227 * Also each of these strings have a field in the entry which
228 * contains its offset from the beginning of the entry.
229 * We have then first to get this field, dereference it
230 * and add it to the address of the entry, and at last we have
231 * the address of the string.
233 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
,
236 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
237 int str_loc
= str_item
& 0xffff;
238 int str_len
= str_item
>> 16;
239 char *addr
= (char *)(event
+ str_loc
);
242 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
244 match
= cmp
^ pred
->not;
249 static int filter_pred_none(struct filter_pred
*pred
, void *event
,
255 /* Basic regex callbacks */
256 static int regex_match_full(char *str
, struct regex
*r
, int len
)
258 if (strncmp(str
, r
->pattern
, len
) == 0)
263 static int regex_match_front(char *str
, struct regex
*r
, int len
)
265 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
270 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
272 if (strnstr(str
, r
->pattern
, len
))
277 static int regex_match_end(char *str
, struct regex
*r
, int len
)
279 int strlen
= len
- 1;
281 if (strlen
>= r
->len
&&
282 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
288 * filter_parse_regex - parse a basic regex
289 * @buff: the raw regex
290 * @len: length of the regex
291 * @search: will point to the beginning of the string to compare
292 * @not: tell whether the match will have to be inverted
294 * This passes in a buffer containing a regex and this function will
295 * set search to point to the search part of the buffer and
296 * return the type of search it is (see enum above).
297 * This does modify buff.
300 * search returns the pointer to use for comparison.
301 * not returns 1 if buff started with a '!'
304 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
306 int type
= MATCH_FULL
;
309 if (buff
[0] == '!') {
318 for (i
= 0; i
< len
; i
++) {
319 if (buff
[i
] == '*') {
322 type
= MATCH_END_ONLY
;
324 if (type
== MATCH_END_ONLY
)
325 type
= MATCH_MIDDLE_ONLY
;
327 type
= MATCH_FRONT_ONLY
;
337 static void filter_build_regex(struct filter_pred
*pred
)
339 struct regex
*r
= &pred
->regex
;
341 enum regex_type type
= MATCH_FULL
;
344 if (pred
->op
== OP_GLOB
) {
345 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
346 r
->len
= strlen(search
);
347 memmove(r
->pattern
, search
, r
->len
+1);
352 r
->match
= regex_match_full
;
354 case MATCH_FRONT_ONLY
:
355 r
->match
= regex_match_front
;
357 case MATCH_MIDDLE_ONLY
:
358 r
->match
= regex_match_middle
;
361 r
->match
= regex_match_end
;
368 /* return 1 if event matches, 0 otherwise (discard) */
369 int filter_match_preds(struct event_filter
*filter
, void *rec
)
371 int match
, top
= 0, val1
= 0, val2
= 0;
372 int stack
[MAX_FILTER_PRED
];
373 struct filter_pred
*pred
;
376 for (i
= 0; i
< filter
->n_preds
; i
++) {
377 pred
= filter
->preds
[i
];
379 match
= pred
->fn(pred
, rec
, val1
, val2
);
380 stack
[top
++] = match
;
383 if (pred
->pop_n
> top
) {
389 match
= pred
->fn(pred
, rec
, val1
, val2
);
390 stack
[top
++] = match
;
395 EXPORT_SYMBOL_GPL(filter_match_preds
);
397 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
400 ps
->lasterr_pos
= pos
;
403 static void remove_filter_string(struct event_filter
*filter
)
405 kfree(filter
->filter_string
);
406 filter
->filter_string
= NULL
;
409 static int replace_filter_string(struct event_filter
*filter
,
412 kfree(filter
->filter_string
);
413 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
414 if (!filter
->filter_string
)
420 static int append_filter_string(struct event_filter
*filter
,
424 char *new_filter_string
;
426 BUG_ON(!filter
->filter_string
);
427 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
428 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
429 if (!new_filter_string
)
432 strcpy(new_filter_string
, filter
->filter_string
);
433 strcat(new_filter_string
, string
);
434 kfree(filter
->filter_string
);
435 filter
->filter_string
= new_filter_string
;
440 static void append_filter_err(struct filter_parse_state
*ps
,
441 struct event_filter
*filter
)
443 int pos
= ps
->lasterr_pos
;
446 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
450 append_filter_string(filter
, "\n");
451 memset(buf
, ' ', PAGE_SIZE
);
452 if (pos
> PAGE_SIZE
- 128)
455 pbuf
= &buf
[pos
] + 1;
457 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
458 append_filter_string(filter
, buf
);
459 free_page((unsigned long) buf
);
462 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
464 struct event_filter
*filter
= call
->filter
;
466 mutex_lock(&event_mutex
);
467 if (filter
&& filter
->filter_string
)
468 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
470 trace_seq_printf(s
, "none\n");
471 mutex_unlock(&event_mutex
);
474 void print_subsystem_event_filter(struct event_subsystem
*system
,
477 struct event_filter
*filter
= system
->filter
;
479 mutex_lock(&event_mutex
);
480 if (filter
&& filter
->filter_string
)
481 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
483 trace_seq_printf(s
, "none\n");
484 mutex_unlock(&event_mutex
);
487 static struct ftrace_event_field
*
488 find_event_field(struct ftrace_event_call
*call
, char *name
)
490 struct ftrace_event_field
*field
;
492 list_for_each_entry(field
, &call
->fields
, link
) {
493 if (!strcmp(field
->name
, name
))
500 static void filter_free_pred(struct filter_pred
*pred
)
505 kfree(pred
->field_name
);
509 static void filter_clear_pred(struct filter_pred
*pred
)
511 kfree(pred
->field_name
);
512 pred
->field_name
= NULL
;
516 static int filter_set_pred(struct filter_pred
*dest
,
517 struct filter_pred
*src
,
521 if (src
->field_name
) {
522 dest
->field_name
= kstrdup(src
->field_name
, GFP_KERNEL
);
523 if (!dest
->field_name
)
531 static void filter_disable_preds(struct ftrace_event_call
*call
)
533 struct event_filter
*filter
= call
->filter
;
536 call
->filter_active
= 0;
539 for (i
= 0; i
< MAX_FILTER_PRED
; i
++)
540 filter
->preds
[i
]->fn
= filter_pred_none
;
543 static void __free_preds(struct event_filter
*filter
)
550 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
551 if (filter
->preds
[i
])
552 filter_free_pred(filter
->preds
[i
]);
554 kfree(filter
->preds
);
555 kfree(filter
->filter_string
);
559 void destroy_preds(struct ftrace_event_call
*call
)
561 __free_preds(call
->filter
);
563 call
->filter_active
= 0;
566 static struct event_filter
*__alloc_preds(void)
568 struct event_filter
*filter
;
569 struct filter_pred
*pred
;
572 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
574 return ERR_PTR(-ENOMEM
);
578 filter
->preds
= kzalloc(MAX_FILTER_PRED
* sizeof(pred
), GFP_KERNEL
);
582 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
583 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
586 pred
->fn
= filter_pred_none
;
587 filter
->preds
[i
] = pred
;
593 __free_preds(filter
);
594 return ERR_PTR(-ENOMEM
);
597 static int init_preds(struct ftrace_event_call
*call
)
602 call
->filter_active
= 0;
603 call
->filter
= __alloc_preds();
604 if (IS_ERR(call
->filter
))
605 return PTR_ERR(call
->filter
);
610 static int init_subsystem_preds(struct event_subsystem
*system
)
612 struct ftrace_event_call
*call
;
615 list_for_each_entry(call
, &ftrace_events
, list
) {
616 if (!call
->define_fields
)
619 if (strcmp(call
->system
, system
->name
) != 0)
622 err
= init_preds(call
);
630 static void filter_free_subsystem_preds(struct event_subsystem
*system
)
632 struct ftrace_event_call
*call
;
634 list_for_each_entry(call
, &ftrace_events
, list
) {
635 if (!call
->define_fields
)
638 if (strcmp(call
->system
, system
->name
) != 0)
641 filter_disable_preds(call
);
642 remove_filter_string(call
->filter
);
646 static int filter_add_pred_fn(struct filter_parse_state
*ps
,
647 struct ftrace_event_call
*call
,
648 struct event_filter
*filter
,
649 struct filter_pred
*pred
,
654 if (filter
->n_preds
== MAX_FILTER_PRED
) {
655 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
659 idx
= filter
->n_preds
;
660 filter_clear_pred(filter
->preds
[idx
]);
661 err
= filter_set_pred(filter
->preds
[idx
], pred
, fn
);
670 int filter_assign_type(const char *type
)
672 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
673 return FILTER_DYN_STRING
;
675 if (strchr(type
, '[') && strstr(type
, "char"))
676 return FILTER_STATIC_STRING
;
681 static bool is_string_field(struct ftrace_event_field
*field
)
683 return field
->filter_type
== FILTER_DYN_STRING
||
684 field
->filter_type
== FILTER_STATIC_STRING
||
685 field
->filter_type
== FILTER_PTR_STRING
;
688 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
690 if (is_string_field(field
) &&
691 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
693 if (!is_string_field(field
) && op
== OP_GLOB
)
699 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
702 filter_pred_fn_t fn
= NULL
;
704 switch (field_size
) {
706 if (op
== OP_EQ
|| op
== OP_NE
)
708 else if (field_is_signed
)
709 fn
= filter_pred_s64
;
711 fn
= filter_pred_u64
;
714 if (op
== OP_EQ
|| op
== OP_NE
)
716 else if (field_is_signed
)
717 fn
= filter_pred_s32
;
719 fn
= filter_pred_u32
;
722 if (op
== OP_EQ
|| op
== OP_NE
)
724 else if (field_is_signed
)
725 fn
= filter_pred_s16
;
727 fn
= filter_pred_u16
;
730 if (op
== OP_EQ
|| op
== OP_NE
)
732 else if (field_is_signed
)
742 static int filter_add_pred(struct filter_parse_state
*ps
,
743 struct ftrace_event_call
*call
,
744 struct event_filter
*filter
,
745 struct filter_pred
*pred
,
748 struct ftrace_event_field
*field
;
750 unsigned long long val
;
753 pred
->fn
= filter_pred_none
;
755 if (pred
->op
== OP_AND
) {
757 fn
= filter_pred_and
;
759 } else if (pred
->op
== OP_OR
) {
765 field
= find_event_field(call
, pred
->field_name
);
767 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
771 pred
->offset
= field
->offset
;
773 if (!is_legal_op(field
, pred
->op
)) {
774 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
778 if (is_string_field(field
)) {
779 filter_build_regex(pred
);
781 if (field
->filter_type
== FILTER_STATIC_STRING
) {
782 fn
= filter_pred_string
;
783 pred
->regex
.field_len
= field
->size
;
784 } else if (field
->filter_type
== FILTER_DYN_STRING
)
785 fn
= filter_pred_strloc
;
787 fn
= filter_pred_pchar
;
789 if (field
->is_signed
)
790 ret
= strict_strtoll(pred
->regex
.pattern
, 0, &val
);
792 ret
= strict_strtoull(pred
->regex
.pattern
, 0, &val
);
794 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
799 fn
= select_comparison_fn(pred
->op
, field
->size
,
802 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
807 if (pred
->op
== OP_NE
)
812 return filter_add_pred_fn(ps
, call
, filter
, pred
, fn
);
816 static void parse_init(struct filter_parse_state
*ps
,
817 struct filter_op
*ops
,
820 memset(ps
, '\0', sizeof(*ps
));
822 ps
->infix
.string
= infix_string
;
823 ps
->infix
.cnt
= strlen(infix_string
);
826 INIT_LIST_HEAD(&ps
->opstack
);
827 INIT_LIST_HEAD(&ps
->postfix
);
830 static char infix_next(struct filter_parse_state
*ps
)
834 return ps
->infix
.string
[ps
->infix
.tail
++];
837 static char infix_peek(struct filter_parse_state
*ps
)
839 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
842 return ps
->infix
.string
[ps
->infix
.tail
];
845 static void infix_advance(struct filter_parse_state
*ps
)
851 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
854 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
857 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
861 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
862 if (ps
->ops
[i
].string
[0] == c
)
869 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
871 char nextc
= infix_peek(ps
);
879 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
880 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
882 return ps
->ops
[i
].id
;
888 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
889 if (!strcmp(opstr
, ps
->ops
[i
].string
))
890 return ps
->ops
[i
].id
;
896 static inline void clear_operand_string(struct filter_parse_state
*ps
)
898 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
899 ps
->operand
.tail
= 0;
902 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
904 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
907 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
912 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
914 struct opstack_op
*opstack_op
;
916 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
921 list_add(&opstack_op
->list
, &ps
->opstack
);
926 static int filter_opstack_empty(struct filter_parse_state
*ps
)
928 return list_empty(&ps
->opstack
);
931 static int filter_opstack_top(struct filter_parse_state
*ps
)
933 struct opstack_op
*opstack_op
;
935 if (filter_opstack_empty(ps
))
938 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
940 return opstack_op
->op
;
943 static int filter_opstack_pop(struct filter_parse_state
*ps
)
945 struct opstack_op
*opstack_op
;
948 if (filter_opstack_empty(ps
))
951 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
953 list_del(&opstack_op
->list
);
960 static void filter_opstack_clear(struct filter_parse_state
*ps
)
962 while (!filter_opstack_empty(ps
))
963 filter_opstack_pop(ps
);
966 static char *curr_operand(struct filter_parse_state
*ps
)
968 return ps
->operand
.string
;
971 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
973 struct postfix_elt
*elt
;
975 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
980 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
986 list_add_tail(&elt
->list
, &ps
->postfix
);
991 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
993 struct postfix_elt
*elt
;
995 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1000 elt
->operand
= NULL
;
1002 list_add_tail(&elt
->list
, &ps
->postfix
);
1007 static void postfix_clear(struct filter_parse_state
*ps
)
1009 struct postfix_elt
*elt
;
1011 while (!list_empty(&ps
->postfix
)) {
1012 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1013 list_del(&elt
->list
);
1014 kfree(elt
->operand
);
1019 static int filter_parse(struct filter_parse_state
*ps
)
1025 while ((ch
= infix_next(ps
))) {
1037 if (is_op_char(ps
, ch
)) {
1038 op
= infix_get_op(ps
, ch
);
1039 if (op
== OP_NONE
) {
1040 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1044 if (strlen(curr_operand(ps
))) {
1045 postfix_append_operand(ps
, curr_operand(ps
));
1046 clear_operand_string(ps
);
1049 while (!filter_opstack_empty(ps
)) {
1050 top_op
= filter_opstack_top(ps
);
1051 if (!is_precedence_lower(ps
, top_op
, op
)) {
1052 top_op
= filter_opstack_pop(ps
);
1053 postfix_append_op(ps
, top_op
);
1059 filter_opstack_push(ps
, op
);
1064 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1069 if (strlen(curr_operand(ps
))) {
1070 postfix_append_operand(ps
, curr_operand(ps
));
1071 clear_operand_string(ps
);
1074 top_op
= filter_opstack_pop(ps
);
1075 while (top_op
!= OP_NONE
) {
1076 if (top_op
== OP_OPEN_PAREN
)
1078 postfix_append_op(ps
, top_op
);
1079 top_op
= filter_opstack_pop(ps
);
1081 if (top_op
== OP_NONE
) {
1082 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1088 if (append_operand_char(ps
, ch
)) {
1089 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1094 if (strlen(curr_operand(ps
)))
1095 postfix_append_operand(ps
, curr_operand(ps
));
1097 while (!filter_opstack_empty(ps
)) {
1098 top_op
= filter_opstack_pop(ps
);
1099 if (top_op
== OP_NONE
)
1101 if (top_op
== OP_OPEN_PAREN
) {
1102 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1105 postfix_append_op(ps
, top_op
);
1111 static struct filter_pred
*create_pred(int op
, char *operand1
, char *operand2
)
1113 struct filter_pred
*pred
;
1115 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1119 pred
->field_name
= kstrdup(operand1
, GFP_KERNEL
);
1120 if (!pred
->field_name
) {
1125 strcpy(pred
->regex
.pattern
, operand2
);
1126 pred
->regex
.len
= strlen(pred
->regex
.pattern
);
1133 static struct filter_pred
*create_logical_pred(int op
)
1135 struct filter_pred
*pred
;
1137 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1146 static int check_preds(struct filter_parse_state
*ps
)
1148 int n_normal_preds
= 0, n_logical_preds
= 0;
1149 struct postfix_elt
*elt
;
1151 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1152 if (elt
->op
== OP_NONE
)
1155 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1162 if (!n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1163 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1170 static int replace_preds(struct ftrace_event_call
*call
,
1171 struct event_filter
*filter
,
1172 struct filter_parse_state
*ps
,
1173 char *filter_string
,
1176 char *operand1
= NULL
, *operand2
= NULL
;
1177 struct filter_pred
*pred
;
1178 struct postfix_elt
*elt
;
1182 err
= check_preds(ps
);
1186 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1187 if (elt
->op
== OP_NONE
) {
1189 operand1
= elt
->operand
;
1191 operand2
= elt
->operand
;
1193 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1199 if (n_preds
++ == MAX_FILTER_PRED
) {
1200 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1204 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1205 pred
= create_logical_pred(elt
->op
);
1209 if (!operand1
|| !operand2
) {
1210 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1214 pred
= create_pred(elt
->op
, operand1
, operand2
);
1218 err
= filter_add_pred(ps
, call
, filter
, pred
, dry_run
);
1219 filter_free_pred(pred
);
1223 operand1
= operand2
= NULL
;
1229 static int replace_system_preds(struct event_subsystem
*system
,
1230 struct filter_parse_state
*ps
,
1231 char *filter_string
)
1233 struct ftrace_event_call
*call
;
1237 list_for_each_entry(call
, &ftrace_events
, list
) {
1238 struct event_filter
*filter
= call
->filter
;
1240 if (!call
->define_fields
)
1243 if (strcmp(call
->system
, system
->name
) != 0)
1246 /* try to see if the filter can be applied */
1247 err
= replace_preds(call
, filter
, ps
, filter_string
, true);
1251 /* really apply the filter */
1252 filter_disable_preds(call
);
1253 err
= replace_preds(call
, filter
, ps
, filter_string
, false);
1255 filter_disable_preds(call
);
1257 call
->filter_active
= 1;
1258 replace_filter_string(filter
, filter_string
);
1264 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1270 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1273 struct filter_parse_state
*ps
;
1275 mutex_lock(&event_mutex
);
1277 err
= init_preds(call
);
1281 if (!strcmp(strstrip(filter_string
), "0")) {
1282 filter_disable_preds(call
);
1283 remove_filter_string(call
->filter
);
1288 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1292 filter_disable_preds(call
);
1293 replace_filter_string(call
->filter
, filter_string
);
1295 parse_init(ps
, filter_ops
, filter_string
);
1296 err
= filter_parse(ps
);
1298 append_filter_err(ps
, call
->filter
);
1302 err
= replace_preds(call
, call
->filter
, ps
, filter_string
, false);
1304 append_filter_err(ps
, call
->filter
);
1306 call
->filter_active
= 1;
1308 filter_opstack_clear(ps
);
1312 mutex_unlock(&event_mutex
);
1317 int apply_subsystem_event_filter(struct event_subsystem
*system
,
1318 char *filter_string
)
1321 struct filter_parse_state
*ps
;
1323 mutex_lock(&event_mutex
);
1325 err
= init_subsystem_preds(system
);
1329 if (!strcmp(strstrip(filter_string
), "0")) {
1330 filter_free_subsystem_preds(system
);
1331 remove_filter_string(system
->filter
);
1336 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1340 replace_filter_string(system
->filter
, filter_string
);
1342 parse_init(ps
, filter_ops
, filter_string
);
1343 err
= filter_parse(ps
);
1345 append_filter_err(ps
, system
->filter
);
1349 err
= replace_system_preds(system
, ps
, filter_string
);
1351 append_filter_err(ps
, system
->filter
);
1354 filter_opstack_clear(ps
);
1358 mutex_unlock(&event_mutex
);
1363 #ifdef CONFIG_EVENT_PROFILE
1365 void ftrace_profile_free_filter(struct perf_event
*event
)
1367 struct event_filter
*filter
= event
->filter
;
1369 event
->filter
= NULL
;
1370 __free_preds(filter
);
1373 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
1377 struct event_filter
*filter
;
1378 struct filter_parse_state
*ps
;
1379 struct ftrace_event_call
*call
= NULL
;
1381 mutex_lock(&event_mutex
);
1383 list_for_each_entry(call
, &ftrace_events
, list
) {
1384 if (call
->id
== event_id
)
1396 filter
= __alloc_preds();
1397 if (IS_ERR(filter
)) {
1398 err
= PTR_ERR(filter
);
1403 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1407 parse_init(ps
, filter_ops
, filter_str
);
1408 err
= filter_parse(ps
);
1412 err
= replace_preds(call
, filter
, ps
, filter_str
, false);
1414 event
->filter
= filter
;
1417 filter_opstack_clear(ps
);
1423 __free_preds(filter
);
1426 mutex_unlock(&event_mutex
);
1431 #endif /* CONFIG_EVENT_PROFILE */