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>
26 #include "trace_output.h"
48 static struct filter_op filter_ops
[] = {
57 { OP_NONE
, "OP_NONE", 0 },
58 { OP_OPEN_PAREN
, "(", 0 },
64 FILT_ERR_UNBALANCED_PAREN
,
65 FILT_ERR_TOO_MANY_OPERANDS
,
66 FILT_ERR_OPERAND_TOO_LONG
,
67 FILT_ERR_FIELD_NOT_FOUND
,
68 FILT_ERR_ILLEGAL_FIELD_OP
,
69 FILT_ERR_ILLEGAL_INTVAL
,
70 FILT_ERR_BAD_SUBSYS_FILTER
,
71 FILT_ERR_TOO_MANY_PREDS
,
72 FILT_ERR_MISSING_FIELD
,
73 FILT_ERR_INVALID_FILTER
,
76 static char *err_text
[] = {
83 "Illegal operation for field type",
84 "Illegal integer value",
85 "Couldn't find or set field in one of a subsystem's events",
86 "Too many terms in predicate expression",
87 "Missing field name and/or value",
88 "Meaningless filter expression",
93 struct list_head list
;
99 struct list_head list
;
102 struct filter_parse_state
{
103 struct filter_op
*ops
;
104 struct list_head opstack
;
105 struct list_head postfix
;
116 char string
[MAX_FILTER_STR_VAL
];
122 #define DEFINE_COMPARISON_PRED(type) \
123 static int filter_pred_##type(struct filter_pred *pred, void *event, \
124 int val1, int val2) \
126 type *addr = (type *)(event + pred->offset); \
127 type val = (type)pred->val; \
130 switch (pred->op) { \
132 match = (*addr < val); \
135 match = (*addr <= val); \
138 match = (*addr > val); \
141 match = (*addr >= val); \
150 #define DEFINE_EQUALITY_PRED(size) \
151 static int filter_pred_##size(struct filter_pred *pred, void *event, \
152 int val1, int val2) \
154 u##size *addr = (u##size *)(event + pred->offset); \
155 u##size val = (u##size)pred->val; \
158 match = (val == *addr) ^ pred->not; \
163 DEFINE_COMPARISON_PRED(s64
);
164 DEFINE_COMPARISON_PRED(u64
);
165 DEFINE_COMPARISON_PRED(s32
);
166 DEFINE_COMPARISON_PRED(u32
);
167 DEFINE_COMPARISON_PRED(s16
);
168 DEFINE_COMPARISON_PRED(u16
);
169 DEFINE_COMPARISON_PRED(s8
);
170 DEFINE_COMPARISON_PRED(u8
);
172 DEFINE_EQUALITY_PRED(64);
173 DEFINE_EQUALITY_PRED(32);
174 DEFINE_EQUALITY_PRED(16);
175 DEFINE_EQUALITY_PRED(8);
177 static int filter_pred_and(struct filter_pred
*pred
__attribute((unused
)),
178 void *event
__attribute((unused
)),
184 static int filter_pred_or(struct filter_pred
*pred
__attribute((unused
)),
185 void *event
__attribute((unused
)),
191 /* Filter predicate for fixed sized arrays of characters */
192 static int filter_pred_string(struct filter_pred
*pred
, void *event
,
195 char *addr
= (char *)(event
+ pred
->offset
);
198 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
200 match
= cmp
^ pred
->not;
205 /* Filter predicate for char * pointers */
206 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
,
209 char **addr
= (char **)(event
+ pred
->offset
);
212 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, pred
->regex
.field_len
);
214 match
= cmp
^ pred
->not;
220 * Filter predicate for dynamic sized arrays of characters.
221 * These are implemented through a list of strings at the end
223 * Also each of these strings have a field in the entry which
224 * contains its offset from the beginning of the entry.
225 * We have then first to get this field, dereference it
226 * and add it to the address of the entry, and at last we have
227 * the address of the string.
229 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
,
232 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
233 int str_loc
= str_item
& 0xffff;
234 int str_len
= str_item
>> 16;
235 char *addr
= (char *)(event
+ str_loc
);
238 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
240 match
= cmp
^ pred
->not;
245 static int filter_pred_none(struct filter_pred
*pred
, void *event
,
251 /* Basic regex callbacks */
252 static int regex_match_full(char *str
, struct regex
*r
, int len
)
254 if (strncmp(str
, r
->pattern
, len
) == 0)
259 static int regex_match_front(char *str
, struct regex
*r
, int len
)
261 if (strncmp(str
, r
->pattern
, len
) == 0)
266 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
268 if (strstr(str
, r
->pattern
))
273 static int regex_match_end(char *str
, struct regex
*r
, int len
)
275 char *ptr
= strstr(str
, r
->pattern
);
277 if (ptr
&& (ptr
[r
->len
] == 0))
283 * filter_parse_regex - parse a basic regex
284 * @buff: the raw regex
285 * @len: length of the regex
286 * @search: will point to the beginning of the string to compare
287 * @not: tell whether the match will have to be inverted
289 * This passes in a buffer containing a regex and this function will
290 * set search to point to the search part of the buffer and
291 * return the type of search it is (see enum above).
292 * This does modify buff.
295 * search returns the pointer to use for comparison.
296 * not returns 1 if buff started with a '!'
299 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
301 int type
= MATCH_FULL
;
304 if (buff
[0] == '!') {
313 for (i
= 0; i
< len
; i
++) {
314 if (buff
[i
] == '*') {
317 type
= MATCH_END_ONLY
;
319 if (type
== MATCH_END_ONLY
)
320 type
= MATCH_MIDDLE_ONLY
;
322 type
= MATCH_FRONT_ONLY
;
332 static int filter_build_regex(struct filter_pred
*pred
)
334 struct regex
*r
= &pred
->regex
;
336 enum regex_type type
;
339 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, ¬);
340 dup
= kstrdup(search
, GFP_KERNEL
);
344 strcpy(r
->pattern
, dup
);
347 r
->len
= strlen(r
->pattern
);
351 r
->match
= regex_match_full
;
353 case MATCH_FRONT_ONLY
:
354 r
->match
= regex_match_front
;
356 case MATCH_MIDDLE_ONLY
:
357 r
->match
= regex_match_middle
;
360 r
->match
= regex_match_end
;
369 /* return 1 if event matches, 0 otherwise (discard) */
370 int filter_match_preds(struct ftrace_event_call
*call
, void *rec
)
372 struct event_filter
*filter
= call
->filter
;
373 int match
, top
= 0, val1
= 0, val2
= 0;
374 int stack
[MAX_FILTER_PRED
];
375 struct filter_pred
*pred
;
378 for (i
= 0; i
< filter
->n_preds
; i
++) {
379 pred
= filter
->preds
[i
];
381 match
= pred
->fn(pred
, rec
, val1
, val2
);
382 stack
[top
++] = match
;
385 if (pred
->pop_n
> top
) {
391 match
= pred
->fn(pred
, rec
, val1
, val2
);
392 stack
[top
++] = match
;
397 EXPORT_SYMBOL_GPL(filter_match_preds
);
399 static void parse_error(struct filter_parse_state
*ps
, int err
, int pos
)
402 ps
->lasterr_pos
= pos
;
405 static void remove_filter_string(struct event_filter
*filter
)
407 kfree(filter
->filter_string
);
408 filter
->filter_string
= NULL
;
411 static int replace_filter_string(struct event_filter
*filter
,
414 kfree(filter
->filter_string
);
415 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
416 if (!filter
->filter_string
)
422 static int append_filter_string(struct event_filter
*filter
,
426 char *new_filter_string
;
428 BUG_ON(!filter
->filter_string
);
429 newlen
= strlen(filter
->filter_string
) + strlen(string
) + 1;
430 new_filter_string
= kmalloc(newlen
, GFP_KERNEL
);
431 if (!new_filter_string
)
434 strcpy(new_filter_string
, filter
->filter_string
);
435 strcat(new_filter_string
, string
);
436 kfree(filter
->filter_string
);
437 filter
->filter_string
= new_filter_string
;
442 static void append_filter_err(struct filter_parse_state
*ps
,
443 struct event_filter
*filter
)
445 int pos
= ps
->lasterr_pos
;
448 buf
= (char *)__get_free_page(GFP_TEMPORARY
);
452 append_filter_string(filter
, "\n");
453 memset(buf
, ' ', PAGE_SIZE
);
454 if (pos
> PAGE_SIZE
- 128)
457 pbuf
= &buf
[pos
] + 1;
459 sprintf(pbuf
, "\nparse_error: %s\n", err_text
[ps
->lasterr
]);
460 append_filter_string(filter
, buf
);
461 free_page((unsigned long) buf
);
464 void print_event_filter(struct ftrace_event_call
*call
, struct trace_seq
*s
)
466 struct event_filter
*filter
= call
->filter
;
468 mutex_lock(&event_mutex
);
469 if (filter
&& filter
->filter_string
)
470 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
472 trace_seq_printf(s
, "none\n");
473 mutex_unlock(&event_mutex
);
476 void print_subsystem_event_filter(struct event_subsystem
*system
,
479 struct event_filter
*filter
= system
->filter
;
481 mutex_lock(&event_mutex
);
482 if (filter
&& filter
->filter_string
)
483 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
485 trace_seq_printf(s
, "none\n");
486 mutex_unlock(&event_mutex
);
489 static struct ftrace_event_field
*
490 find_event_field(struct ftrace_event_call
*call
, char *name
)
492 struct ftrace_event_field
*field
;
494 list_for_each_entry(field
, &call
->fields
, link
) {
495 if (!strcmp(field
->name
, name
))
502 static void filter_free_pred(struct filter_pred
*pred
)
507 kfree(pred
->field_name
);
511 static void filter_clear_pred(struct filter_pred
*pred
)
513 kfree(pred
->field_name
);
514 pred
->field_name
= NULL
;
518 static int filter_set_pred(struct filter_pred
*dest
,
519 struct filter_pred
*src
,
523 if (src
->field_name
) {
524 dest
->field_name
= kstrdup(src
->field_name
, GFP_KERNEL
);
525 if (!dest
->field_name
)
533 static void filter_disable_preds(struct ftrace_event_call
*call
)
535 struct event_filter
*filter
= call
->filter
;
538 call
->filter_active
= 0;
541 for (i
= 0; i
< MAX_FILTER_PRED
; i
++)
542 filter
->preds
[i
]->fn
= filter_pred_none
;
545 void destroy_preds(struct ftrace_event_call
*call
)
547 struct event_filter
*filter
= call
->filter
;
553 for (i
= 0; i
< MAX_FILTER_PRED
; i
++) {
554 if (filter
->preds
[i
])
555 filter_free_pred(filter
->preds
[i
]);
557 kfree(filter
->preds
);
558 kfree(filter
->filter_string
);
563 static int init_preds(struct ftrace_event_call
*call
)
565 struct event_filter
*filter
;
566 struct filter_pred
*pred
;
572 filter
= call
->filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
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
;
598 static int init_subsystem_preds(struct event_subsystem
*system
)
600 struct ftrace_event_call
*call
;
603 list_for_each_entry(call
, &ftrace_events
, list
) {
604 if (!call
->define_fields
)
607 if (strcmp(call
->system
, system
->name
) != 0)
610 err
= init_preds(call
);
620 FILTER_INIT_NO_RESET
,
621 FILTER_SKIP_NO_RESET
,
624 static void filter_free_subsystem_preds(struct event_subsystem
*system
,
627 struct ftrace_event_call
*call
;
629 list_for_each_entry(call
, &ftrace_events
, list
) {
630 if (!call
->define_fields
)
633 if (strcmp(call
->system
, system
->name
) != 0)
636 if (flag
== FILTER_INIT_NO_RESET
) {
637 call
->filter
->no_reset
= false;
641 if (flag
== FILTER_SKIP_NO_RESET
&& call
->filter
->no_reset
)
644 filter_disable_preds(call
);
645 remove_filter_string(call
->filter
);
649 static int filter_add_pred_fn(struct filter_parse_state
*ps
,
650 struct ftrace_event_call
*call
,
651 struct filter_pred
*pred
,
654 struct event_filter
*filter
= call
->filter
;
657 if (filter
->n_preds
== MAX_FILTER_PRED
) {
658 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
662 idx
= filter
->n_preds
;
663 filter_clear_pred(filter
->preds
[idx
]);
664 err
= filter_set_pred(filter
->preds
[idx
], pred
, fn
);
669 call
->filter_active
= 1;
674 int filter_assign_type(const char *type
)
676 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
677 return FILTER_DYN_STRING
;
679 if (strchr(type
, '[') && strstr(type
, "char"))
680 return FILTER_STATIC_STRING
;
685 static bool is_string_field(struct ftrace_event_field
*field
)
687 return field
->filter_type
== FILTER_DYN_STRING
||
688 field
->filter_type
== FILTER_STATIC_STRING
||
689 field
->filter_type
== FILTER_PTR_STRING
;
692 static int is_legal_op(struct ftrace_event_field
*field
, int op
)
694 if (is_string_field(field
) && (op
!= OP_EQ
&& op
!= OP_NE
))
700 static filter_pred_fn_t
select_comparison_fn(int op
, int field_size
,
703 filter_pred_fn_t fn
= NULL
;
705 switch (field_size
) {
707 if (op
== OP_EQ
|| op
== OP_NE
)
709 else if (field_is_signed
)
710 fn
= filter_pred_s64
;
712 fn
= filter_pred_u64
;
715 if (op
== OP_EQ
|| op
== OP_NE
)
717 else if (field_is_signed
)
718 fn
= filter_pred_s32
;
720 fn
= filter_pred_u32
;
723 if (op
== OP_EQ
|| op
== OP_NE
)
725 else if (field_is_signed
)
726 fn
= filter_pred_s16
;
728 fn
= filter_pred_u16
;
731 if (op
== OP_EQ
|| op
== OP_NE
)
733 else if (field_is_signed
)
743 static int filter_add_pred(struct filter_parse_state
*ps
,
744 struct ftrace_event_call
*call
,
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 ret
= filter_build_regex(pred
);
783 if (field
->filter_type
== FILTER_STATIC_STRING
) {
784 fn
= filter_pred_string
;
785 pred
->regex
.field_len
= field
->size
;
786 } else if (field
->filter_type
== FILTER_DYN_STRING
)
787 fn
= filter_pred_strloc
;
789 fn
= filter_pred_pchar
;
790 pred
->regex
.field_len
= strlen(pred
->regex
.pattern
);
793 if (field
->is_signed
)
794 ret
= strict_strtoll(pred
->regex
.pattern
, 0, &val
);
796 ret
= strict_strtoull(pred
->regex
.pattern
, 0, &val
);
798 parse_error(ps
, FILT_ERR_ILLEGAL_INTVAL
, 0);
803 fn
= select_comparison_fn(pred
->op
, field
->size
,
806 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
811 if (pred
->op
== OP_NE
)
816 return filter_add_pred_fn(ps
, call
, pred
, fn
);
820 static int filter_add_subsystem_pred(struct filter_parse_state
*ps
,
821 struct event_subsystem
*system
,
822 struct filter_pred
*pred
,
826 struct ftrace_event_call
*call
;
830 list_for_each_entry(call
, &ftrace_events
, list
) {
832 if (!call
->define_fields
)
835 if (strcmp(call
->system
, system
->name
))
838 if (call
->filter
->no_reset
)
841 err
= filter_add_pred(ps
, call
, pred
, dry_run
);
843 call
->filter
->no_reset
= true;
848 replace_filter_string(call
->filter
, filter_string
);
852 parse_error(ps
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
858 static void parse_init(struct filter_parse_state
*ps
,
859 struct filter_op
*ops
,
862 memset(ps
, '\0', sizeof(*ps
));
864 ps
->infix
.string
= infix_string
;
865 ps
->infix
.cnt
= strlen(infix_string
);
868 INIT_LIST_HEAD(&ps
->opstack
);
869 INIT_LIST_HEAD(&ps
->postfix
);
872 static char infix_next(struct filter_parse_state
*ps
)
876 return ps
->infix
.string
[ps
->infix
.tail
++];
879 static char infix_peek(struct filter_parse_state
*ps
)
881 if (ps
->infix
.tail
== strlen(ps
->infix
.string
))
884 return ps
->infix
.string
[ps
->infix
.tail
];
887 static void infix_advance(struct filter_parse_state
*ps
)
893 static inline int is_precedence_lower(struct filter_parse_state
*ps
,
896 return ps
->ops
[a
].precedence
< ps
->ops
[b
].precedence
;
899 static inline int is_op_char(struct filter_parse_state
*ps
, char c
)
903 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
904 if (ps
->ops
[i
].string
[0] == c
)
911 static int infix_get_op(struct filter_parse_state
*ps
, char firstc
)
913 char nextc
= infix_peek(ps
);
921 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
922 if (!strcmp(opstr
, ps
->ops
[i
].string
)) {
924 return ps
->ops
[i
].id
;
930 for (i
= 0; strcmp(ps
->ops
[i
].string
, "OP_NONE"); i
++) {
931 if (!strcmp(opstr
, ps
->ops
[i
].string
))
932 return ps
->ops
[i
].id
;
938 static inline void clear_operand_string(struct filter_parse_state
*ps
)
940 memset(ps
->operand
.string
, '\0', MAX_FILTER_STR_VAL
);
941 ps
->operand
.tail
= 0;
944 static inline int append_operand_char(struct filter_parse_state
*ps
, char c
)
946 if (ps
->operand
.tail
== MAX_FILTER_STR_VAL
- 1)
949 ps
->operand
.string
[ps
->operand
.tail
++] = c
;
954 static int filter_opstack_push(struct filter_parse_state
*ps
, int op
)
956 struct opstack_op
*opstack_op
;
958 opstack_op
= kmalloc(sizeof(*opstack_op
), GFP_KERNEL
);
963 list_add(&opstack_op
->list
, &ps
->opstack
);
968 static int filter_opstack_empty(struct filter_parse_state
*ps
)
970 return list_empty(&ps
->opstack
);
973 static int filter_opstack_top(struct filter_parse_state
*ps
)
975 struct opstack_op
*opstack_op
;
977 if (filter_opstack_empty(ps
))
980 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
982 return opstack_op
->op
;
985 static int filter_opstack_pop(struct filter_parse_state
*ps
)
987 struct opstack_op
*opstack_op
;
990 if (filter_opstack_empty(ps
))
993 opstack_op
= list_first_entry(&ps
->opstack
, struct opstack_op
, list
);
995 list_del(&opstack_op
->list
);
1002 static void filter_opstack_clear(struct filter_parse_state
*ps
)
1004 while (!filter_opstack_empty(ps
))
1005 filter_opstack_pop(ps
);
1008 static char *curr_operand(struct filter_parse_state
*ps
)
1010 return ps
->operand
.string
;
1013 static int postfix_append_operand(struct filter_parse_state
*ps
, char *operand
)
1015 struct postfix_elt
*elt
;
1017 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1022 elt
->operand
= kstrdup(operand
, GFP_KERNEL
);
1023 if (!elt
->operand
) {
1028 list_add_tail(&elt
->list
, &ps
->postfix
);
1033 static int postfix_append_op(struct filter_parse_state
*ps
, int op
)
1035 struct postfix_elt
*elt
;
1037 elt
= kmalloc(sizeof(*elt
), GFP_KERNEL
);
1042 elt
->operand
= NULL
;
1044 list_add_tail(&elt
->list
, &ps
->postfix
);
1049 static void postfix_clear(struct filter_parse_state
*ps
)
1051 struct postfix_elt
*elt
;
1053 while (!list_empty(&ps
->postfix
)) {
1054 elt
= list_first_entry(&ps
->postfix
, struct postfix_elt
, list
);
1055 kfree(elt
->operand
);
1056 list_del(&elt
->list
);
1060 static int filter_parse(struct filter_parse_state
*ps
)
1066 while ((ch
= infix_next(ps
))) {
1078 if (is_op_char(ps
, ch
)) {
1079 op
= infix_get_op(ps
, ch
);
1080 if (op
== OP_NONE
) {
1081 parse_error(ps
, FILT_ERR_INVALID_OP
, 0);
1085 if (strlen(curr_operand(ps
))) {
1086 postfix_append_operand(ps
, curr_operand(ps
));
1087 clear_operand_string(ps
);
1090 while (!filter_opstack_empty(ps
)) {
1091 top_op
= filter_opstack_top(ps
);
1092 if (!is_precedence_lower(ps
, top_op
, op
)) {
1093 top_op
= filter_opstack_pop(ps
);
1094 postfix_append_op(ps
, top_op
);
1100 filter_opstack_push(ps
, op
);
1105 filter_opstack_push(ps
, OP_OPEN_PAREN
);
1110 if (strlen(curr_operand(ps
))) {
1111 postfix_append_operand(ps
, curr_operand(ps
));
1112 clear_operand_string(ps
);
1115 top_op
= filter_opstack_pop(ps
);
1116 while (top_op
!= OP_NONE
) {
1117 if (top_op
== OP_OPEN_PAREN
)
1119 postfix_append_op(ps
, top_op
);
1120 top_op
= filter_opstack_pop(ps
);
1122 if (top_op
== OP_NONE
) {
1123 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1129 if (append_operand_char(ps
, ch
)) {
1130 parse_error(ps
, FILT_ERR_OPERAND_TOO_LONG
, 0);
1135 if (strlen(curr_operand(ps
)))
1136 postfix_append_operand(ps
, curr_operand(ps
));
1138 while (!filter_opstack_empty(ps
)) {
1139 top_op
= filter_opstack_pop(ps
);
1140 if (top_op
== OP_NONE
)
1142 if (top_op
== OP_OPEN_PAREN
) {
1143 parse_error(ps
, FILT_ERR_UNBALANCED_PAREN
, 0);
1146 postfix_append_op(ps
, top_op
);
1152 static struct filter_pred
*create_pred(int op
, char *operand1
, char *operand2
)
1154 struct filter_pred
*pred
;
1156 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1160 pred
->field_name
= kstrdup(operand1
, GFP_KERNEL
);
1161 if (!pred
->field_name
) {
1166 strcpy(pred
->regex
.pattern
, operand2
);
1167 pred
->regex
.len
= strlen(pred
->regex
.pattern
);
1174 static struct filter_pred
*create_logical_pred(int op
)
1176 struct filter_pred
*pred
;
1178 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1187 static int check_preds(struct filter_parse_state
*ps
)
1189 int n_normal_preds
= 0, n_logical_preds
= 0;
1190 struct postfix_elt
*elt
;
1192 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1193 if (elt
->op
== OP_NONE
)
1196 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1203 if (!n_normal_preds
|| n_logical_preds
>= n_normal_preds
) {
1204 parse_error(ps
, FILT_ERR_INVALID_FILTER
, 0);
1211 static int replace_preds(struct event_subsystem
*system
,
1212 struct ftrace_event_call
*call
,
1213 struct filter_parse_state
*ps
,
1214 char *filter_string
,
1217 char *operand1
= NULL
, *operand2
= NULL
;
1218 struct filter_pred
*pred
;
1219 struct postfix_elt
*elt
;
1223 err
= check_preds(ps
);
1227 list_for_each_entry(elt
, &ps
->postfix
, list
) {
1228 if (elt
->op
== OP_NONE
) {
1230 operand1
= elt
->operand
;
1232 operand2
= elt
->operand
;
1234 parse_error(ps
, FILT_ERR_TOO_MANY_OPERANDS
, 0);
1240 if (n_preds
++ == MAX_FILTER_PRED
) {
1241 parse_error(ps
, FILT_ERR_TOO_MANY_PREDS
, 0);
1245 if (elt
->op
== OP_AND
|| elt
->op
== OP_OR
) {
1246 pred
= create_logical_pred(elt
->op
);
1250 if (!operand1
|| !operand2
) {
1251 parse_error(ps
, FILT_ERR_MISSING_FIELD
, 0);
1255 pred
= create_pred(elt
->op
, operand1
, operand2
);
1260 err
= filter_add_pred(ps
, call
, pred
, false);
1262 err
= filter_add_subsystem_pred(ps
, system
, pred
,
1263 filter_string
, dry_run
);
1264 filter_free_pred(pred
);
1268 operand1
= operand2
= NULL
;
1274 int apply_event_filter(struct ftrace_event_call
*call
, char *filter_string
)
1278 struct filter_parse_state
*ps
;
1280 mutex_lock(&event_mutex
);
1282 err
= init_preds(call
);
1286 if (!strcmp(strstrip(filter_string
), "0")) {
1287 filter_disable_preds(call
);
1288 remove_filter_string(call
->filter
);
1289 mutex_unlock(&event_mutex
);
1294 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1298 filter_disable_preds(call
);
1299 replace_filter_string(call
->filter
, filter_string
);
1301 parse_init(ps
, filter_ops
, filter_string
);
1302 err
= filter_parse(ps
);
1304 append_filter_err(ps
, call
->filter
);
1308 err
= replace_preds(NULL
, call
, ps
, filter_string
, false);
1310 append_filter_err(ps
, call
->filter
);
1313 filter_opstack_clear(ps
);
1317 mutex_unlock(&event_mutex
);
1322 int apply_subsystem_event_filter(struct event_subsystem
*system
,
1323 char *filter_string
)
1327 struct filter_parse_state
*ps
;
1329 mutex_lock(&event_mutex
);
1331 err
= init_subsystem_preds(system
);
1335 if (!strcmp(strstrip(filter_string
), "0")) {
1336 filter_free_subsystem_preds(system
, FILTER_DISABLE_ALL
);
1337 remove_filter_string(system
->filter
);
1338 mutex_unlock(&event_mutex
);
1343 ps
= kzalloc(sizeof(*ps
), GFP_KERNEL
);
1347 replace_filter_string(system
->filter
, filter_string
);
1349 parse_init(ps
, filter_ops
, filter_string
);
1350 err
= filter_parse(ps
);
1352 append_filter_err(ps
, system
->filter
);
1356 filter_free_subsystem_preds(system
, FILTER_INIT_NO_RESET
);
1358 /* try to see the filter can be applied to which events */
1359 err
= replace_preds(system
, NULL
, ps
, filter_string
, true);
1361 append_filter_err(ps
, system
->filter
);
1365 filter_free_subsystem_preds(system
, FILTER_SKIP_NO_RESET
);
1367 /* really apply the filter to the events */
1368 err
= replace_preds(system
, NULL
, ps
, filter_string
, false);
1370 append_filter_err(ps
, system
->filter
);
1371 filter_free_subsystem_preds(system
, 2);
1375 filter_opstack_clear(ps
);
1379 mutex_unlock(&event_mutex
);