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"
51 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 DEFINE_COMPARISON_PRED(type) \
127 static int filter_pred_##type(struct filter_pred *pred, void *event, \
128 int val1, int val2) \
130 type *addr = (type *)(event + pred->offset); \
131 type val = (type)pred->val; \
134 switch (pred->op) { \
136 match = (*addr < val); \
139 match = (*addr <= val); \
142 match = (*addr > val); \
145 match = (*addr >= val); \
154 #define DEFINE_EQUALITY_PRED(size) \
155 static int filter_pred_##size(struct filter_pred *pred, void *event, \
156 int val1, int val2) \
158 u##size *addr = (u##size *)(event + pred->offset); \
159 u##size val = (u##size)pred->val; \
162 match = (val == *addr) ^ pred->not; \
167 DEFINE_COMPARISON_PRED(s64
);
168 DEFINE_COMPARISON_PRED(u64
);
169 DEFINE_COMPARISON_PRED(s32
);
170 DEFINE_COMPARISON_PRED(u32
);
171 DEFINE_COMPARISON_PRED(s16
);
172 DEFINE_COMPARISON_PRED(u16
);
173 DEFINE_COMPARISON_PRED(s8
);
174 DEFINE_COMPARISON_PRED(u8
);
176 DEFINE_EQUALITY_PRED(64);
177 DEFINE_EQUALITY_PRED(32);
178 DEFINE_EQUALITY_PRED(16);
179 DEFINE_EQUALITY_PRED(8);
181 static int filter_pred_and(struct filter_pred
*pred
__attribute((unused
)),
182 void *event
__attribute((unused
)),
188 static int filter_pred_or(struct filter_pred
*pred
__attribute((unused
)),
189 void *event
__attribute((unused
)),
195 /* Filter predicate for fixed sized arrays of characters */
196 static int filter_pred_string(struct filter_pred
*pred
, void *event
,
199 char *addr
= (char *)(event
+ pred
->offset
);
202 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
204 match
= cmp
^ pred
->not;
209 /* Filter predicate for char * pointers */
210 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
,
213 char **addr
= (char **)(event
+ pred
->offset
);
215 int len
= strlen(*addr
) + 1; /* including tailing '\0' */
217 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, len
);
219 match
= cmp
^ pred
->not;
225 * Filter predicate for dynamic sized arrays of characters.
226 * These are implemented through a list of strings at the end
228 * Also each of these strings have a field in the entry which
229 * contains its offset from the beginning of the entry.
230 * We have then first to get this field, dereference it
231 * and add it to the address of the entry, and at last we have
232 * the address of the string.
234 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
,
237 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
238 int str_loc
= str_item
& 0xffff;
239 int str_len
= str_item
>> 16;
240 char *addr
= (char *)(event
+ str_loc
);
243 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
245 match
= cmp
^ pred
->not;
250 static int filter_pred_none(struct filter_pred
*pred
, void *event
,
257 * regex_match_foo - Basic regex callbacks
259 * @str: the string to be searched
260 * @r: the regex structure containing the pattern string
261 * @len: the length of the string to be searched (including '\0')
264 * - @str might not be NULL-terminated if it's of type DYN_STRING
268 static int regex_match_full(char *str
, struct regex
*r
, int len
)
270 if (strncmp(str
, r
->pattern
, len
) == 0)
275 static int regex_match_front(char *str
, struct regex
*r
, int len
)
277 if (strncmp(str
, r
->pattern
, r
->len
) == 0)
282 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
284 if (strnstr(str
, r
->pattern
, len
))
289 static int regex_match_end(char *str
, struct regex
*r
, int len
)
291 int strlen
= len
- 1;
293 if (strlen
>= r
->len
&&
294 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
300 * filter_parse_regex - parse a basic regex
301 * @buff: the raw regex
302 * @len: length of the regex
303 * @search: will point to the beginning of the string to compare
304 * @not: tell whether the match will have to be inverted
306 * This passes in a buffer containing a regex and this function will
307 * set search to point to the search part of the buffer and
308 * return the type of search it is (see enum above).
309 * This does modify buff.
312 * search returns the pointer to use for comparison.
313 * not returns 1 if buff started with a '!'
316 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
318 int type
= MATCH_FULL
;
321 if (buff
[0] == '!') {
330 for (i
= 0; i
< len
; i
++) {
331 if (buff
[i
] == '*') {
334 type
= MATCH_END_ONLY
;
336 if (type
== MATCH_END_ONLY
)
337 type
= MATCH_MIDDLE_ONLY
;
339 type
= MATCH_FRONT_ONLY
;
349 static void filter_build_regex(struct filter_pred
*pred
)
351 struct regex
*r
= &pred
->regex
;
353 enum regex_type type
= MATCH_FULL
;
356 if (pred
->op
== OP_GLOB
) {
357 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
358 r
->len
= strlen(search
);
359 memmove(r
->pattern
, search
, r
->len
+1);
364 r
->match
= regex_match_full
;
366 case MATCH_FRONT_ONLY
:
367 r
->match
= regex_match_front
;
369 case MATCH_MIDDLE_ONLY
:
370 r
->match
= regex_match_middle
;
373 r
->match
= regex_match_end
;
380 /* return 1 if event matches, 0 otherwise (discard) */
381 int filter_match_preds(struct event_filter
*filter
, void *rec
)
383 int match
, top
= 0, val1
= 0, val2
= 0;
384 int stack
[MAX_FILTER_PRED
];
385 struct filter_pred
*pred
;
388 for (i
= 0; i
< filter
->n_preds
; i
++) {
389 pred
= filter
->preds
[i
];
391 match
= pred
->fn(pred
, rec
, val1
, val2
);
392 stack
[top
++] = match
;
395 if (pred
->pop_n
> top
) {
401 match
= pred
->fn(pred
, rec
, val1
, val2
);
402 stack
[top
++] = match
;
407 EXPORT_SYMBOL_GPL(filter_match_preds
);
409 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
412 ps
->lasterr_pos
= pos
;
415 static void remove_filter_string(struct event_filter
*filter
)
417 kfree(filter
->filter_string
);
418 filter
->filter_string
= NULL
;
421 static int replace_filter_string(struct event_filter
*filter
,
424 kfree(filter
->filter_string
);
425 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
426 if (!filter
->filter_string
)
432 static int append_filter_string(struct event_filter
*filter
,
436 char *new_filter_string
;
438 BUG_ON(!filter
->filter_string
);
439 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
440 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
441 if (!new_filter_string
)
444 strcpy(new_filter_string
, filter
->filter_string
);
445 strcat(new_filter_string
, string
);
446 kfree(filter
->filter_string
);
447 filter
->filter_string
= new_filter_string
;
452 static void append_filter_err(struct filter_parse_state
*ps
,
453 struct event_filter
*filter
)
455 int pos
= ps
->lasterr_pos
;
458 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
462 append_filter_string(filter
, "\n");
463 memset(buf
, ' ', PAGE_SIZE
);
464 if (pos
> PAGE_SIZE
- 128)
467 pbuf
= &buf
[pos
] + 1;
469 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
470 append_filter_string(filter
, buf
);
471 free_page((unsigned long) buf
);
474 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
476 struct event_filter
*filter
= call
->filter
;
478 mutex_lock(&event_mutex
);
479 if (filter
&& filter
->filter_string
)
480 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
482 trace_seq_printf(s
, "none\n");
483 mutex_unlock(&event_mutex
);
486 void print_subsystem_event_filter(struct event_subsystem
*system
,
489 struct event_filter
*filter
= system
->filter
;
491 mutex_lock(&event_mutex
);
492 if (filter
&& filter
->filter_string
)
493 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
495 trace_seq_printf(s
, "none\n");
496 mutex_unlock(&event_mutex
);
499 static struct ftrace_event_field
*
500 find_event_field(struct ftrace_event_call
*call
, char *name
)
502 struct ftrace_event_field
*field
;
503 struct list_head
*head
;
505 head
= trace_get_fields(call
);
506 list_for_each_entry(field
, head
, link
) {
507 if (!strcmp(field
->name
, name
))
514 static void filter_free_pred(struct filter_pred
*pred
)
519 kfree(pred
->field_name
);
523 static void filter_clear_pred(struct filter_pred
*pred
)
525 kfree(pred
->field_name
);
526 pred
->field_name
= NULL
;
530 static int filter_set_pred(struct filter_pred
*dest
,
531 struct filter_pred
*src
,
535 if (src
->field_name
) {
536 dest
->field_name
= kstrdup(src
->field_name
, GFP_KERNEL
);
537 if (!dest
->field_name
)
545 static void filter_disable_preds(struct ftrace_event_call
*call
)
547 struct event_filter
*filter
= call
->filter
;
550 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
553 for (i
= 0; i
< MAX_FILTER_PRED
; i
++)
554 filter
->preds
[i
]->fn
= filter_pred_none
;
557 static void __free_preds(struct event_filter
*filter
)
564 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
565 if (filter
->preds
[i
])
566 filter_free_pred(filter
->preds
[i
]);
568 kfree(filter
->preds
);
569 kfree(filter
->filter_string
);
573 void destroy_preds(struct ftrace_event_call
*call
)
575 __free_preds(call
->filter
);
577 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
580 static struct event_filter
*__alloc_preds(void)
582 struct event_filter
*filter
;
583 struct filter_pred
*pred
;
586 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
588 return ERR_PTR(-ENOMEM
);
592 filter
->preds
= kzalloc(MAX_FILTER_PRED
* sizeof(pred
), GFP_KERNEL
);
596 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
597 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
600 pred
->fn
= filter_pred_none
;
601 filter
->preds
[i
] = pred
;
607 __free_preds(filter
);
608 return ERR_PTR(-ENOMEM
);
611 static int init_preds(struct ftrace_event_call
*call
)
616 call
->flags
&= ~TRACE_EVENT_FL_FILTERED
;
617 call
->filter
= __alloc_preds();
618 if (IS_ERR(call
->filter
))
619 return PTR_ERR(call
->filter
);
624 static int init_subsystem_preds(struct event_subsystem
*system
)
626 struct ftrace_event_call
*call
;
629 list_for_each_entry(call
, &ftrace_events
, list
) {
630 if (!call
->class || !call
->class->define_fields
)
633 if (strcmp(call
->class->system
, system
->name
) != 0)
636 err
= init_preds(call
);
644 static void filter_free_subsystem_preds(struct event_subsystem
*system
)
646 struct ftrace_event_call
*call
;
648 list_for_each_entry(call
, &ftrace_events
, list
) {
649 if (!call
->class || !call
->class->define_fields
)
652 if (strcmp(call
->class->system
, system
->name
) != 0)
655 filter_disable_preds(call
);
656 remove_filter_string(call
->filter
);
660 static int filter_add_pred_fn(struct filter_parse_state
*ps
,
661 struct ftrace_event_call
*call
,
662 struct event_filter
*filter
,
663 struct filter_pred
*pred
,
668 if (filter
->n_preds
== MAX_FILTER_PRED
) {
669 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
673 idx
= filter
->n_preds
;
674 filter_clear_pred(filter
->preds
[idx
]);
675 err
= filter_set_pred(filter
->preds
[idx
], pred
, fn
);
684 int filter_assign_type(const char *type
)
686 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
687 return FILTER_DYN_STRING
;
689 if (strchr(type
, '[') && strstr(type
, "char"))
690 return FILTER_STATIC_STRING
;
695 static bool is_string_field(struct ftrace_event_field
*field
)
697 return field
->filter_type
== FILTER_DYN_STRING
||
698 field
->filter_type
== FILTER_STATIC_STRING
||
699 field
->filter_type
== FILTER_PTR_STRING
;
702 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
704 if (is_string_field(field
) &&
705 (op
!= OP_EQ
&& op
!= OP_NE
&& op
!= OP_GLOB
))
707 if (!is_string_field(field
) && op
== OP_GLOB
)
713 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
716 filter_pred_fn_t fn
= NULL
;
718 switch (field_size
) {
720 if (op
== OP_EQ
|| op
== OP_NE
)
722 else if (field_is_signed
)
723 fn
= filter_pred_s64
;
725 fn
= filter_pred_u64
;
728 if (op
== OP_EQ
|| op
== OP_NE
)
730 else if (field_is_signed
)
731 fn
= filter_pred_s32
;
733 fn
= filter_pred_u32
;
736 if (op
== OP_EQ
|| op
== OP_NE
)
738 else if (field_is_signed
)
739 fn
= filter_pred_s16
;
741 fn
= filter_pred_u16
;
744 if (op
== OP_EQ
|| op
== OP_NE
)
746 else if (field_is_signed
)
756 static int filter_add_pred(struct filter_parse_state
*ps
,
757 struct ftrace_event_call
*call
,
758 struct event_filter
*filter
,
759 struct filter_pred
*pred
,
762 struct ftrace_event_field
*field
;
764 unsigned long long val
;
767 pred
->fn
= filter_pred_none
;
769 if (pred
->op
== OP_AND
) {
771 fn
= filter_pred_and
;
773 } else if (pred
->op
== OP_OR
) {
779 field
= find_event_field(call
, pred
->field_name
);
781 parse_error(ps
, FILT_ERR_FIELD_NOT_FOUND
, 0);
785 pred
->offset
= field
->offset
;
787 if (!is_legal_op(field
, pred
->op
)) {
788 parse_error(ps
, FILT_ERR_ILLEGAL_FIELD_OP
, 0);
792 if (is_string_field(field
)) {
793 filter_build_regex(pred
);
795 if (field
->filter_type
== FILTER_STATIC_STRING
) {
796 fn
= filter_pred_string
;
797 pred
->regex
.field_len
= field
->size
;
798 } else if (field
->filter_type
== FILTER_DYN_STRING
)
799 fn
= filter_pred_strloc
;
801 fn
= filter_pred_pchar
;
803 if (field
->is_signed
)
804 ret
= strict_strtoll(pred
->regex
.pattern
, 0, &val
);
806 ret
= strict_strtoull(pred
->regex
.pattern
, 0, &val
);
808 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
813 fn
= select_comparison_fn(pred
->op
, field
->size
,
816 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
821 if (pred
->op
== OP_NE
)
826 return filter_add_pred_fn(ps
, call
, filter
, pred
, fn
);
830 static void parse_init(struct filter_parse_state
*ps
,
831 struct filter_op
*ops
,
834 memset(ps
, '\0', sizeof(*ps
));
836 ps
->infix
.string
= infix_string
;
837 ps
->infix
.cnt
= strlen(infix_string
);
840 INIT_LIST_HEAD(&ps
->opstack
);
841 INIT_LIST_HEAD(&ps
->postfix
);
844 static char infix_next(struct filter_parse_state
*ps
)
848 return ps
->infix
.string
[ps
->infix
.tail
++];
851 static char infix_peek(struct filter_parse_state
*ps
)
853 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
856 return ps
->infix
.string
[ps
->infix
.tail
];
859 static void infix_advance(struct filter_parse_state
*ps
)
865 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
868 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
871 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
875 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
876 if (ps
->ops
[i
].string
[0] == c
)
883 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
885 char nextc
= infix_peek(ps
);
893 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
894 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
896 return ps
->ops
[i
].id
;
902 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
903 if (!strcmp(opstr
, ps
->ops
[i
].string
))
904 return ps
->ops
[i
].id
;
910 static inline void clear_operand_string(struct filter_parse_state
*ps
)
912 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
913 ps
->operand
.tail
= 0;
916 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
918 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
921 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
926 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
928 struct opstack_op
*opstack_op
;
930 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
935 list_add(&opstack_op
->list
, &ps
->opstack
);
940 static int filter_opstack_empty(struct filter_parse_state
*ps
)
942 return list_empty(&ps
->opstack
);
945 static int filter_opstack_top(struct filter_parse_state
*ps
)
947 struct opstack_op
*opstack_op
;
949 if (filter_opstack_empty(ps
))
952 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
954 return opstack_op
->op
;
957 static int filter_opstack_pop(struct filter_parse_state
*ps
)
959 struct opstack_op
*opstack_op
;
962 if (filter_opstack_empty(ps
))
965 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
967 list_del(&opstack_op
->list
);
974 static void filter_opstack_clear(struct filter_parse_state
*ps
)
976 while (!filter_opstack_empty(ps
))
977 filter_opstack_pop(ps
);
980 static char *curr_operand(struct filter_parse_state
*ps
)
982 return ps
->operand
.string
;
985 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
987 struct postfix_elt
*elt
;
989 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
994 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1000 list_add_tail(&elt
->list
, &ps
->postfix
);
1005 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1007 struct postfix_elt
*elt
;
1009 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1014 elt
->operand
= NULL
;
1016 list_add_tail(&elt
->list
, &ps
->postfix
);
1021 static void postfix_clear(struct filter_parse_state
*ps
)
1023 struct postfix_elt
*elt
;
1025 while (!list_empty(&ps
->postfix
)) {
1026 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1027 list_del(&elt
->list
);
1028 kfree(elt
->operand
);
1033 static int filter_parse(struct filter_parse_state
*ps
)
1039 while ((ch
= infix_next(ps
))) {
1051 if (is_op_char(ps
, ch
)) {
1052 op
= infix_get_op(ps
, ch
);
1053 if (op
== OP_NONE
) {
1054 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1058 if (strlen(curr_operand(ps
))) {
1059 postfix_append_operand(ps
, curr_operand(ps
));
1060 clear_operand_string(ps
);
1063 while (!filter_opstack_empty(ps
)) {
1064 top_op
= filter_opstack_top(ps
);
1065 if (!is_precedence_lower(ps
, top_op
, op
)) {
1066 top_op
= filter_opstack_pop(ps
);
1067 postfix_append_op(ps
, top_op
);
1073 filter_opstack_push(ps
, op
);
1078 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1083 if (strlen(curr_operand(ps
))) {
1084 postfix_append_operand(ps
, curr_operand(ps
));
1085 clear_operand_string(ps
);
1088 top_op
= filter_opstack_pop(ps
);
1089 while (top_op
!= OP_NONE
) {
1090 if (top_op
== OP_OPEN_PAREN
)
1092 postfix_append_op(ps
, top_op
);
1093 top_op
= filter_opstack_pop(ps
);
1095 if (top_op
== OP_NONE
) {
1096 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1102 if (append_operand_char(ps
, ch
)) {
1103 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1108 if (strlen(curr_operand(ps
)))
1109 postfix_append_operand(ps
, curr_operand(ps
));
1111 while (!filter_opstack_empty(ps
)) {
1112 top_op
= filter_opstack_pop(ps
);
1113 if (top_op
== OP_NONE
)
1115 if (top_op
== OP_OPEN_PAREN
) {
1116 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1119 postfix_append_op(ps
, top_op
);
1125 static struct filter_pred
*create_pred(int op
, char *operand1
, char *operand2
)
1127 struct filter_pred
*pred
;
1129 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1133 pred
->field_name
= kstrdup(operand1
, GFP_KERNEL
);
1134 if (!pred
->field_name
) {
1139 strcpy(pred
->regex
.pattern
, operand2
);
1140 pred
->regex
.len
= strlen(pred
->regex
.pattern
);
1147 static struct filter_pred
*create_logical_pred(int op
)
1149 struct filter_pred
*pred
;
1151 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1160 static int check_preds(struct filter_parse_state
*ps
)
1162 int n_normal_preds
= 0, n_logical_preds
= 0;
1163 struct postfix_elt
*elt
;
1165 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1166 if (elt
->op
== OP_NONE
)
1169 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1176 if (!n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1177 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1184 static int replace_preds(struct ftrace_event_call
*call
,
1185 struct event_filter
*filter
,
1186 struct filter_parse_state
*ps
,
1187 char *filter_string
,
1190 char *operand1
= NULL
, *operand2
= NULL
;
1191 struct filter_pred
*pred
;
1192 struct postfix_elt
*elt
;
1196 err
= check_preds(ps
);
1200 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1201 if (elt
->op
== OP_NONE
) {
1203 operand1
= elt
->operand
;
1205 operand2
= elt
->operand
;
1207 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1213 if (n_preds
++ == MAX_FILTER_PRED
) {
1214 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1218 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1219 pred
= create_logical_pred(elt
->op
);
1223 if (!operand1
|| !operand2
) {
1224 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1228 pred
= create_pred(elt
->op
, operand1
, operand2
);
1232 err
= filter_add_pred(ps
, call
, filter
, pred
, dry_run
);
1233 filter_free_pred(pred
);
1237 operand1
= operand2
= NULL
;
1243 static int replace_system_preds(struct event_subsystem
*system
,
1244 struct filter_parse_state
*ps
,
1245 char *filter_string
)
1247 struct ftrace_event_call
*call
;
1251 list_for_each_entry(call
, &ftrace_events
, list
) {
1252 struct event_filter
*filter
= call
->filter
;
1254 if (!call
->class || !call
->class->define_fields
)
1257 if (strcmp(call
->class->system
, system
->name
) != 0)
1260 /* try to see if the filter can be applied */
1261 err
= replace_preds(call
, filter
, ps
, filter_string
, true);
1265 /* really apply the filter */
1266 filter_disable_preds(call
);
1267 err
= replace_preds(call
, filter
, ps
, filter_string
, false);
1269 filter_disable_preds(call
);
1271 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1272 replace_filter_string(filter
, filter_string
);
1278 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1284 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1287 struct filter_parse_state
*ps
;
1289 mutex_lock(&event_mutex
);
1291 err
= init_preds(call
);
1295 if (!strcmp(strstrip(filter_string
), "0")) {
1296 filter_disable_preds(call
);
1297 remove_filter_string(call
->filter
);
1302 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1306 filter_disable_preds(call
);
1307 replace_filter_string(call
->filter
, filter_string
);
1309 parse_init(ps
, filter_ops
, filter_string
);
1310 err
= filter_parse(ps
);
1312 append_filter_err(ps
, call
->filter
);
1316 err
= replace_preds(call
, call
->filter
, ps
, filter_string
, false);
1318 append_filter_err(ps
, call
->filter
);
1320 call
->flags
|= TRACE_EVENT_FL_FILTERED
;
1322 filter_opstack_clear(ps
);
1326 mutex_unlock(&event_mutex
);
1331 int apply_subsystem_event_filter(struct event_subsystem
*system
,
1332 char *filter_string
)
1335 struct filter_parse_state
*ps
;
1337 mutex_lock(&event_mutex
);
1339 err
= init_subsystem_preds(system
);
1343 /* Make sure the system still has events */
1344 if (!system
->nr_events
) {
1349 if (!strcmp(strstrip(filter_string
), "0")) {
1350 filter_free_subsystem_preds(system
);
1351 remove_filter_string(system
->filter
);
1356 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1360 replace_filter_string(system
->filter
, filter_string
);
1362 parse_init(ps
, filter_ops
, filter_string
);
1363 err
= filter_parse(ps
);
1365 append_filter_err(ps
, system
->filter
);
1369 err
= replace_system_preds(system
, ps
, filter_string
);
1371 append_filter_err(ps
, system
->filter
);
1374 filter_opstack_clear(ps
);
1378 mutex_unlock(&event_mutex
);
1383 #ifdef CONFIG_PERF_EVENTS
1385 void ftrace_profile_free_filter(struct perf_event
*event
)
1387 struct event_filter
*filter
= event
->filter
;
1389 event
->filter
= NULL
;
1390 __free_preds(filter
);
1393 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
1397 struct event_filter
*filter
;
1398 struct filter_parse_state
*ps
;
1399 struct ftrace_event_call
*call
= NULL
;
1401 mutex_lock(&event_mutex
);
1403 list_for_each_entry(call
, &ftrace_events
, list
) {
1404 if (call
->event
.type
== event_id
)
1409 if (&call
->list
== &ftrace_events
)
1416 filter
= __alloc_preds();
1417 if (IS_ERR(filter
)) {
1418 err
= PTR_ERR(filter
);
1423 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1427 parse_init(ps
, filter_ops
, filter_str
);
1428 err
= filter_parse(ps
);
1432 err
= replace_preds(call
, filter
, ps
, filter_str
, false);
1434 event
->filter
= filter
;
1437 filter_opstack_clear(ps
);
1443 __free_preds(filter
);
1446 mutex_unlock(&event_mutex
);
1451 #endif /* CONFIG_PERF_EVENTS */