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
,
256 * regex_match_foo - Basic regex callbacks
258 * @str: the string to be searched
259 * @r: the regex structure containing the pattern string
260 * @len: the length of the string to be searched (including '\0')
263 * - @str might not be NULL-terminated if it's of type DYN_STRING
267 static int regex_match_full(char *str
, struct regex
*r
, int len
)
269 if (strncmp(str
, r
->pattern
, len
) == 0)
274 static int regex_match_front(char *str
, struct regex
*r
, int len
)
276 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
281 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
283 if (strnstr(str
, r
->pattern
, len
))
288 static int regex_match_end(char *str
, struct regex
*r
, int len
)
290 int strlen
= len
- 1;
292 if (strlen
>= r
->len
&&
293 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
299 * filter_parse_regex - parse a basic regex
300 * @buff: the raw regex
301 * @len: length of the regex
302 * @search: will point to the beginning of the string to compare
303 * @not: tell whether the match will have to be inverted
305 * This passes in a buffer containing a regex and this function will
306 * set search to point to the search part of the buffer and
307 * return the type of search it is (see enum above).
308 * This does modify buff.
311 * search returns the pointer to use for comparison.
312 * not returns 1 if buff started with a '!'
315 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
317 int type
= MATCH_FULL
;
320 if (buff
[0] == '!') {
329 for (i
= 0; i
< len
; i
++) {
330 if (buff
[i
] == '*') {
333 type
= MATCH_END_ONLY
;
335 if (type
== MATCH_END_ONLY
)
336 type
= MATCH_MIDDLE_ONLY
;
338 type
= MATCH_FRONT_ONLY
;
348 static void filter_build_regex(struct filter_pred
*pred
)
350 struct regex
*r
= &pred
->regex
;
352 enum regex_type type
= MATCH_FULL
;
355 if (pred
->op
== OP_GLOB
) {
356 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
357 r
->len
= strlen(search
);
358 memmove(r
->pattern
, search
, r
->len
+1);
363 r
->match
= regex_match_full
;
365 case MATCH_FRONT_ONLY
:
366 r
->match
= regex_match_front
;
368 case MATCH_MIDDLE_ONLY
:
369 r
->match
= regex_match_middle
;
372 r
->match
= regex_match_end
;
379 /* return 1 if event matches, 0 otherwise (discard) */
380 int filter_match_preds(struct event_filter
*filter
, void *rec
)
382 int match
, top
= 0, val1
= 0, val2
= 0;
383 int stack
[MAX_FILTER_PRED
];
384 struct filter_pred
*pred
;
387 for (i
= 0; i
< filter
->n_preds
; i
++) {
388 pred
= filter
->preds
[i
];
390 match
= pred
->fn(pred
, rec
, val1
, val2
);
391 stack
[top
++] = match
;
394 if (pred
->pop_n
> top
) {
400 match
= pred
->fn(pred
, rec
, val1
, val2
);
401 stack
[top
++] = match
;
406 EXPORT_SYMBOL_GPL(filter_match_preds
);
408 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
411 ps
->lasterr_pos
= pos
;
414 static void remove_filter_string(struct event_filter
*filter
)
416 kfree(filter
->filter_string
);
417 filter
->filter_string
= NULL
;
420 static int replace_filter_string(struct event_filter
*filter
,
423 kfree(filter
->filter_string
);
424 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
425 if (!filter
->filter_string
)
431 static int append_filter_string(struct event_filter
*filter
,
435 char *new_filter_string
;
437 BUG_ON(!filter
->filter_string
);
438 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
439 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
440 if (!new_filter_string
)
443 strcpy(new_filter_string
, filter
->filter_string
);
444 strcat(new_filter_string
, string
);
445 kfree(filter
->filter_string
);
446 filter
->filter_string
= new_filter_string
;
451 static void append_filter_err(struct filter_parse_state
*ps
,
452 struct event_filter
*filter
)
454 int pos
= ps
->lasterr_pos
;
457 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
461 append_filter_string(filter
, "\n");
462 memset(buf
, ' ', PAGE_SIZE
);
463 if (pos
> PAGE_SIZE
- 128)
466 pbuf
= &buf
[pos
] + 1;
468 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
469 append_filter_string(filter
, buf
);
470 free_page((unsigned long) buf
);
473 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
475 struct event_filter
*filter
= call
->filter
;
477 mutex_lock(&event_mutex
);
478 if (filter
&& filter
->filter_string
)
479 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
481 trace_seq_printf(s
, "none\n");
482 mutex_unlock(&event_mutex
);
485 void print_subsystem_event_filter(struct event_subsystem
*system
,
488 struct event_filter
*filter
= system
->filter
;
490 mutex_lock(&event_mutex
);
491 if (filter
&& filter
->filter_string
)
492 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
494 trace_seq_printf(s
, "none\n");
495 mutex_unlock(&event_mutex
);
498 static struct ftrace_event_field
*
499 find_event_field(struct ftrace_event_call
*call
, char *name
)
501 struct ftrace_event_field
*field
;
503 list_for_each_entry(field
, &call
->fields
, link
) {
504 if (!strcmp(field
->name
, name
))
511 static void filter_free_pred(struct filter_pred
*pred
)
516 kfree(pred
->field_name
);
520 static void filter_clear_pred(struct filter_pred
*pred
)
522 kfree(pred
->field_name
);
523 pred
->field_name
= NULL
;
527 static int filter_set_pred(struct filter_pred
*dest
,
528 struct filter_pred
*src
,
532 if (src
->field_name
) {
533 dest
->field_name
= kstrdup(src
->field_name
, GFP_KERNEL
);
534 if (!dest
->field_name
)
542 static void filter_disable_preds(struct ftrace_event_call
*call
)
544 struct event_filter
*filter
= call
->filter
;
547 call
->filter_active
= 0;
550 for (i
= 0; i
< MAX_FILTER_PRED
; i
++)
551 filter
->preds
[i
]->fn
= filter_pred_none
;
554 static void __free_preds(struct event_filter
*filter
)
561 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
562 if (filter
->preds
[i
])
563 filter_free_pred(filter
->preds
[i
]);
565 kfree(filter
->preds
);
566 kfree(filter
->filter_string
);
570 void destroy_preds(struct ftrace_event_call
*call
)
572 __free_preds(call
->filter
);
574 call
->filter_active
= 0;
577 static struct event_filter
*__alloc_preds(void)
579 struct event_filter
*filter
;
580 struct filter_pred
*pred
;
583 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
585 return ERR_PTR(-ENOMEM
);
589 filter
->preds
= kzalloc(MAX_FILTER_PRED
* sizeof(pred
), GFP_KERNEL
);
593 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
594 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
597 pred
->fn
= filter_pred_none
;
598 filter
->preds
[i
] = pred
;
604 __free_preds(filter
);
605 return ERR_PTR(-ENOMEM
);
608 static int init_preds(struct ftrace_event_call
*call
)
613 call
->filter_active
= 0;
614 call
->filter
= __alloc_preds();
615 if (IS_ERR(call
->filter
))
616 return PTR_ERR(call
->filter
);
621 static int init_subsystem_preds(struct event_subsystem
*system
)
623 struct ftrace_event_call
*call
;
626 list_for_each_entry(call
, &ftrace_events
, list
) {
627 if (!call
->define_fields
)
630 if (strcmp(call
->system
, system
->name
) != 0)
633 err
= init_preds(call
);
641 static void filter_free_subsystem_preds(struct event_subsystem
*system
)
643 struct ftrace_event_call
*call
;
645 list_for_each_entry(call
, &ftrace_events
, list
) {
646 if (!call
->define_fields
)
649 if (strcmp(call
->system
, system
->name
) != 0)
652 filter_disable_preds(call
);
653 remove_filter_string(call
->filter
);
657 static int filter_add_pred_fn(struct filter_parse_state
*ps
,
658 struct ftrace_event_call
*call
,
659 struct event_filter
*filter
,
660 struct filter_pred
*pred
,
665 if (filter
->n_preds
== MAX_FILTER_PRED
) {
666 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
670 idx
= filter
->n_preds
;
671 filter_clear_pred(filter
->preds
[idx
]);
672 err
= filter_set_pred(filter
->preds
[idx
], pred
, fn
);
681 int filter_assign_type(const char *type
)
683 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
684 return FILTER_DYN_STRING
;
686 if (strchr(type
, '[') && strstr(type
, "char"))
687 return FILTER_STATIC_STRING
;
692 static bool is_string_field(struct ftrace_event_field
*field
)
694 return field
->filter_type
== FILTER_DYN_STRING
||
695 field
->filter_type
== FILTER_STATIC_STRING
||
696 field
->filter_type
== FILTER_PTR_STRING
;
699 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
701 if (is_string_field(field
) &&
702 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
704 if (!is_string_field(field
) && op
== OP_GLOB
)
710 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
713 filter_pred_fn_t fn
= NULL
;
715 switch (field_size
) {
717 if (op
== OP_EQ
|| op
== OP_NE
)
719 else if (field_is_signed
)
720 fn
= filter_pred_s64
;
722 fn
= filter_pred_u64
;
725 if (op
== OP_EQ
|| op
== OP_NE
)
727 else if (field_is_signed
)
728 fn
= filter_pred_s32
;
730 fn
= filter_pred_u32
;
733 if (op
== OP_EQ
|| op
== OP_NE
)
735 else if (field_is_signed
)
736 fn
= filter_pred_s16
;
738 fn
= filter_pred_u16
;
741 if (op
== OP_EQ
|| op
== OP_NE
)
743 else if (field_is_signed
)
753 static int filter_add_pred(struct filter_parse_state
*ps
,
754 struct ftrace_event_call
*call
,
755 struct event_filter
*filter
,
756 struct filter_pred
*pred
,
759 struct ftrace_event_field
*field
;
761 unsigned long long val
;
764 pred
->fn
= filter_pred_none
;
766 if (pred
->op
== OP_AND
) {
768 fn
= filter_pred_and
;
770 } else if (pred
->op
== OP_OR
) {
776 field
= find_event_field(call
, pred
->field_name
);
778 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
782 pred
->offset
= field
->offset
;
784 if (!is_legal_op(field
, pred
->op
)) {
785 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
789 if (is_string_field(field
)) {
790 filter_build_regex(pred
);
792 if (field
->filter_type
== FILTER_STATIC_STRING
) {
793 fn
= filter_pred_string
;
794 pred
->regex
.field_len
= field
->size
;
795 } else if (field
->filter_type
== FILTER_DYN_STRING
)
796 fn
= filter_pred_strloc
;
798 fn
= filter_pred_pchar
;
800 if (field
->is_signed
)
801 ret
= strict_strtoll(pred
->regex
.pattern
, 0, &val
);
803 ret
= strict_strtoull(pred
->regex
.pattern
, 0, &val
);
805 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
810 fn
= select_comparison_fn(pred
->op
, field
->size
,
813 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
818 if (pred
->op
== OP_NE
)
823 return filter_add_pred_fn(ps
, call
, filter
, pred
, fn
);
827 static void parse_init(struct filter_parse_state
*ps
,
828 struct filter_op
*ops
,
831 memset(ps
, '\0', sizeof(*ps
));
833 ps
->infix
.string
= infix_string
;
834 ps
->infix
.cnt
= strlen(infix_string
);
837 INIT_LIST_HEAD(&ps
->opstack
);
838 INIT_LIST_HEAD(&ps
->postfix
);
841 static char infix_next(struct filter_parse_state
*ps
)
845 return ps
->infix
.string
[ps
->infix
.tail
++];
848 static char infix_peek(struct filter_parse_state
*ps
)
850 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
853 return ps
->infix
.string
[ps
->infix
.tail
];
856 static void infix_advance(struct filter_parse_state
*ps
)
862 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
865 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
868 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
872 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
873 if (ps
->ops
[i
].string
[0] == c
)
880 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
882 char nextc
= infix_peek(ps
);
890 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
891 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
893 return ps
->ops
[i
].id
;
899 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
900 if (!strcmp(opstr
, ps
->ops
[i
].string
))
901 return ps
->ops
[i
].id
;
907 static inline void clear_operand_string(struct filter_parse_state
*ps
)
909 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
910 ps
->operand
.tail
= 0;
913 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
915 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
918 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
923 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
925 struct opstack_op
*opstack_op
;
927 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
932 list_add(&opstack_op
->list
, &ps
->opstack
);
937 static int filter_opstack_empty(struct filter_parse_state
*ps
)
939 return list_empty(&ps
->opstack
);
942 static int filter_opstack_top(struct filter_parse_state
*ps
)
944 struct opstack_op
*opstack_op
;
946 if (filter_opstack_empty(ps
))
949 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
951 return opstack_op
->op
;
954 static int filter_opstack_pop(struct filter_parse_state
*ps
)
956 struct opstack_op
*opstack_op
;
959 if (filter_opstack_empty(ps
))
962 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
964 list_del(&opstack_op
->list
);
971 static void filter_opstack_clear(struct filter_parse_state
*ps
)
973 while (!filter_opstack_empty(ps
))
974 filter_opstack_pop(ps
);
977 static char *curr_operand(struct filter_parse_state
*ps
)
979 return ps
->operand
.string
;
982 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
984 struct postfix_elt
*elt
;
986 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
991 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
997 list_add_tail(&elt
->list
, &ps
->postfix
);
1002 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1004 struct postfix_elt
*elt
;
1006 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1011 elt
->operand
= NULL
;
1013 list_add_tail(&elt
->list
, &ps
->postfix
);
1018 static void postfix_clear(struct filter_parse_state
*ps
)
1020 struct postfix_elt
*elt
;
1022 while (!list_empty(&ps
->postfix
)) {
1023 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1024 list_del(&elt
->list
);
1025 kfree(elt
->operand
);
1030 static int filter_parse(struct filter_parse_state
*ps
)
1036 while ((ch
= infix_next(ps
))) {
1048 if (is_op_char(ps
, ch
)) {
1049 op
= infix_get_op(ps
, ch
);
1050 if (op
== OP_NONE
) {
1051 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1055 if (strlen(curr_operand(ps
))) {
1056 postfix_append_operand(ps
, curr_operand(ps
));
1057 clear_operand_string(ps
);
1060 while (!filter_opstack_empty(ps
)) {
1061 top_op
= filter_opstack_top(ps
);
1062 if (!is_precedence_lower(ps
, top_op
, op
)) {
1063 top_op
= filter_opstack_pop(ps
);
1064 postfix_append_op(ps
, top_op
);
1070 filter_opstack_push(ps
, op
);
1075 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1080 if (strlen(curr_operand(ps
))) {
1081 postfix_append_operand(ps
, curr_operand(ps
));
1082 clear_operand_string(ps
);
1085 top_op
= filter_opstack_pop(ps
);
1086 while (top_op
!= OP_NONE
) {
1087 if (top_op
== OP_OPEN_PAREN
)
1089 postfix_append_op(ps
, top_op
);
1090 top_op
= filter_opstack_pop(ps
);
1092 if (top_op
== OP_NONE
) {
1093 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1099 if (append_operand_char(ps
, ch
)) {
1100 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1105 if (strlen(curr_operand(ps
)))
1106 postfix_append_operand(ps
, curr_operand(ps
));
1108 while (!filter_opstack_empty(ps
)) {
1109 top_op
= filter_opstack_pop(ps
);
1110 if (top_op
== OP_NONE
)
1112 if (top_op
== OP_OPEN_PAREN
) {
1113 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1116 postfix_append_op(ps
, top_op
);
1122 static struct filter_pred
*create_pred(int op
, char *operand1
, char *operand2
)
1124 struct filter_pred
*pred
;
1126 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1130 pred
->field_name
= kstrdup(operand1
, GFP_KERNEL
);
1131 if (!pred
->field_name
) {
1136 strcpy(pred
->regex
.pattern
, operand2
);
1137 pred
->regex
.len
= strlen(pred
->regex
.pattern
);
1144 static struct filter_pred
*create_logical_pred(int op
)
1146 struct filter_pred
*pred
;
1148 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1157 static int check_preds(struct filter_parse_state
*ps
)
1159 int n_normal_preds
= 0, n_logical_preds
= 0;
1160 struct postfix_elt
*elt
;
1162 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1163 if (elt
->op
== OP_NONE
)
1166 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1173 if (!n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1174 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1181 static int replace_preds(struct ftrace_event_call
*call
,
1182 struct event_filter
*filter
,
1183 struct filter_parse_state
*ps
,
1184 char *filter_string
,
1187 char *operand1
= NULL
, *operand2
= NULL
;
1188 struct filter_pred
*pred
;
1189 struct postfix_elt
*elt
;
1193 err
= check_preds(ps
);
1197 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1198 if (elt
->op
== OP_NONE
) {
1200 operand1
= elt
->operand
;
1202 operand2
= elt
->operand
;
1204 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1210 if (n_preds
++ == MAX_FILTER_PRED
) {
1211 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1215 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1216 pred
= create_logical_pred(elt
->op
);
1220 if (!operand1
|| !operand2
) {
1221 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1225 pred
= create_pred(elt
->op
, operand1
, operand2
);
1229 err
= filter_add_pred(ps
, call
, filter
, pred
, dry_run
);
1230 filter_free_pred(pred
);
1234 operand1
= operand2
= NULL
;
1240 static int replace_system_preds(struct event_subsystem
*system
,
1241 struct filter_parse_state
*ps
,
1242 char *filter_string
)
1244 struct ftrace_event_call
*call
;
1248 list_for_each_entry(call
, &ftrace_events
, list
) {
1249 struct event_filter
*filter
= call
->filter
;
1251 if (!call
->define_fields
)
1254 if (strcmp(call
->system
, system
->name
) != 0)
1257 /* try to see if the filter can be applied */
1258 err
= replace_preds(call
, filter
, ps
, filter_string
, true);
1262 /* really apply the filter */
1263 filter_disable_preds(call
);
1264 err
= replace_preds(call
, filter
, ps
, filter_string
, false);
1266 filter_disable_preds(call
);
1268 call
->filter_active
= 1;
1269 replace_filter_string(filter
, filter_string
);
1275 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1281 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1284 struct filter_parse_state
*ps
;
1286 mutex_lock(&event_mutex
);
1288 err
= init_preds(call
);
1292 if (!strcmp(strstrip(filter_string
), "0")) {
1293 filter_disable_preds(call
);
1294 remove_filter_string(call
->filter
);
1299 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1303 filter_disable_preds(call
);
1304 replace_filter_string(call
->filter
, filter_string
);
1306 parse_init(ps
, filter_ops
, filter_string
);
1307 err
= filter_parse(ps
);
1309 append_filter_err(ps
, call
->filter
);
1313 err
= replace_preds(call
, call
->filter
, ps
, filter_string
, false);
1315 append_filter_err(ps
, call
->filter
);
1317 call
->filter_active
= 1;
1319 filter_opstack_clear(ps
);
1323 mutex_unlock(&event_mutex
);
1328 int apply_subsystem_event_filter(struct event_subsystem
*system
,
1329 char *filter_string
)
1332 struct filter_parse_state
*ps
;
1334 mutex_lock(&event_mutex
);
1336 err
= init_subsystem_preds(system
);
1340 if (!strcmp(strstrip(filter_string
), "0")) {
1341 filter_free_subsystem_preds(system
);
1342 remove_filter_string(system
->filter
);
1347 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1351 replace_filter_string(system
->filter
, filter_string
);
1353 parse_init(ps
, filter_ops
, filter_string
);
1354 err
= filter_parse(ps
);
1356 append_filter_err(ps
, system
->filter
);
1360 err
= replace_system_preds(system
, ps
, filter_string
);
1362 append_filter_err(ps
, system
->filter
);
1365 filter_opstack_clear(ps
);
1369 mutex_unlock(&event_mutex
);
1374 #ifdef CONFIG_EVENT_PROFILE
1376 void ftrace_profile_free_filter(struct perf_event
*event
)
1378 struct event_filter
*filter
= event
->filter
;
1380 event
->filter
= NULL
;
1381 __free_preds(filter
);
1384 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
1388 struct event_filter
*filter
;
1389 struct filter_parse_state
*ps
;
1390 struct ftrace_event_call
*call
= NULL
;
1392 mutex_lock(&event_mutex
);
1394 list_for_each_entry(call
, &ftrace_events
, list
) {
1395 if (call
->id
== event_id
)
1407 filter
= __alloc_preds();
1408 if (IS_ERR(filter
)) {
1409 err
= PTR_ERR(filter
);
1414 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1418 parse_init(ps
, filter_ops
, filter_str
);
1419 err
= filter_parse(ps
);
1423 err
= replace_preds(call
, filter
, ps
, filter_str
, false);
1425 event
->filter
= filter
;
1428 filter_opstack_clear(ps
);
1434 __free_preds(filter
);
1437 mutex_unlock(&event_mutex
);
1442 #endif /* CONFIG_EVENT_PROFILE */