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.
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"
30 #define DEFAULT_SYS_FILTER_MESSAGE \
31 "### global filter ###\n" \
32 "# Use this to set filters for multiple events.\n" \
33 "# Only events with the given fields will be affected.\n" \
34 "# If no events are modified, an error message will be displayed here"
59 /* Order must be the same as enum filter_op_ids above */
60 static struct filter_op filter_ops[] = {
72 { OP_NONE, "OP_NONE", 0 },
73 { OP_OPEN_PAREN, "(", 0 },
79 FILT_ERR_UNBALANCED_PAREN,
80 FILT_ERR_TOO_MANY_OPERANDS,
81 FILT_ERR_OPERAND_TOO_LONG,
82 FILT_ERR_FIELD_NOT_FOUND,
83 FILT_ERR_ILLEGAL_FIELD_OP,
84 FILT_ERR_ILLEGAL_INTVAL,
85 FILT_ERR_BAD_SUBSYS_FILTER,
86 FILT_ERR_TOO_MANY_PREDS,
87 FILT_ERR_MISSING_FIELD,
88 FILT_ERR_INVALID_FILTER,
89 FILT_ERR_IP_FIELD_ONLY,
90 FILT_ERR_ILLEGAL_NOT_OP,
93 static char *err_text[] = {
100 "Illegal operation for field type",
101 "Illegal integer value",
102 "Couldn't find or set field in one of a subsystem's events",
103 "Too many terms in predicate expression",
104 "Missing field name and/or value",
105 "Meaningless filter expression",
106 "Only 'ip' field is supported for function trace",
107 "Illegal use of '!'",
111 enum filter_op_ids op;
112 struct list_head list;
116 enum filter_op_ids op;
118 struct list_head list;
121 struct filter_parse_state {
122 struct filter_op *ops;
123 struct list_head opstack;
124 struct list_head postfix;
135 char string[MAX_FILTER_STR_VAL];
142 struct filter_pred **preds;
146 /* If not of not match is equal to not of not, then it is a match */
147 #define DEFINE_COMPARISON_PRED(type) \
148 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
150 type *addr = (type *)(event + pred->offset); \
151 type val = (type)pred->val; \
152 int match = (*addr < val); \
153 return !!match == !pred->not; \
155 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
157 type *addr = (type *)(event + pred->offset); \
158 type val = (type)pred->val; \
159 int match = (*addr <= val); \
160 return !!match == !pred->not; \
162 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
164 type *addr = (type *)(event + pred->offset); \
165 type val = (type)pred->val; \
166 int match = (*addr > val); \
167 return !!match == !pred->not; \
169 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
171 type *addr = (type *)(event + pred->offset); \
172 type val = (type)pred->val; \
173 int match = (*addr >= val); \
174 return !!match == !pred->not; \
176 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
178 type *addr = (type *)(event + pred->offset); \
179 type val = (type)pred->val; \
180 int match = !!(*addr & val); \
181 return match == !pred->not; \
183 static const filter_pred_fn_t pred_funcs_##type[] = { \
184 filter_pred_LT_##type, \
185 filter_pred_LE_##type, \
186 filter_pred_GT_##type, \
187 filter_pred_GE_##type, \
188 filter_pred_BAND_##type, \
191 #define PRED_FUNC_START OP_LT
193 #define DEFINE_EQUALITY_PRED(size) \
194 static int filter_pred_##size(struct filter_pred *pred, void *event) \
196 u##size *addr = (u##size *)(event + pred->offset); \
197 u##size val = (u##size)pred->val; \
200 match = (val == *addr) ^ pred->not; \
205 DEFINE_COMPARISON_PRED(s64);
206 DEFINE_COMPARISON_PRED(u64);
207 DEFINE_COMPARISON_PRED(s32);
208 DEFINE_COMPARISON_PRED(u32);
209 DEFINE_COMPARISON_PRED(s16);
210 DEFINE_COMPARISON_PRED(u16);
211 DEFINE_COMPARISON_PRED(s8);
212 DEFINE_COMPARISON_PRED(u8);
214 DEFINE_EQUALITY_PRED(64);
215 DEFINE_EQUALITY_PRED(32);
216 DEFINE_EQUALITY_PRED(16);
217 DEFINE_EQUALITY_PRED(8);
219 /* Filter predicate for fixed sized arrays of characters */
220 static int filter_pred_string(struct filter_pred *pred, void *event)
222 char *addr = (char *)(event + pred->offset);
225 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
227 match = cmp ^ pred->not;
232 /* Filter predicate for char * pointers */
233 static int filter_pred_pchar(struct filter_pred *pred, void *event)
235 char **addr = (char **)(event + pred->offset);
237 int len = strlen(*addr) + 1; /* including tailing '\0' */
239 cmp = pred->regex.match(*addr, &pred->regex, len);
241 match = cmp ^ pred->not;
247 * Filter predicate for dynamic sized arrays of characters.
248 * These are implemented through a list of strings at the end
250 * Also each of these strings have a field in the entry which
251 * contains its offset from the beginning of the entry.
252 * We have then first to get this field, dereference it
253 * and add it to the address of the entry, and at last we have
254 * the address of the string.
256 static int filter_pred_strloc(struct filter_pred *pred, void *event)
258 u32 str_item = *(u32 *)(event + pred->offset);
259 int str_loc = str_item & 0xffff;
260 int str_len = str_item >> 16;
261 char *addr = (char *)(event + str_loc);
264 cmp = pred->regex.match(addr, &pred->regex, str_len);
266 match = cmp ^ pred->not;
271 /* Filter predicate for CPUs. */
272 static int filter_pred_cpu(struct filter_pred *pred, void *event)
277 cpu = raw_smp_processor_id();
300 return !!match == !pred->not;
303 /* Filter predicate for COMM. */
304 static int filter_pred_comm(struct filter_pred *pred, void *event)
308 cmp = pred->regex.match(current->comm, &pred->regex,
309 pred->regex.field_len);
310 match = cmp ^ pred->not;
315 static int filter_pred_none(struct filter_pred *pred, void *event)
321 * regex_match_foo - Basic regex callbacks
323 * @str: the string to be searched
324 * @r: the regex structure containing the pattern string
325 * @len: the length of the string to be searched (including '\0')
328 * - @str might not be NULL-terminated if it's of type DYN_STRING
332 static int regex_match_full(char *str, struct regex *r, int len)
334 if (strncmp(str, r->pattern, len) == 0)
339 static int regex_match_front(char *str, struct regex *r, int len)
341 if (strncmp(str, r->pattern, r->len) == 0)
346 static int regex_match_middle(char *str, struct regex *r, int len)
348 if (strnstr(str, r->pattern, len))
353 static int regex_match_end(char *str, struct regex *r, int len)
355 int strlen = len - 1;
357 if (strlen >= r->len &&
358 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
363 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
365 if (glob_match(r->pattern, str))
370 * filter_parse_regex - parse a basic regex
371 * @buff: the raw regex
372 * @len: length of the regex
373 * @search: will point to the beginning of the string to compare
374 * @not: tell whether the match will have to be inverted
376 * This passes in a buffer containing a regex and this function will
377 * set search to point to the search part of the buffer and
378 * return the type of search it is (see enum above).
379 * This does modify buff.
382 * search returns the pointer to use for comparison.
383 * not returns 1 if buff started with a '!'
386 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
388 int type = MATCH_FULL;
391 if (buff[0] == '!') {
400 for (i = 0; i < len; i++) {
401 if (buff[i] == '*') {
404 type = MATCH_END_ONLY;
405 } else if (i == len - 1) {
406 if (type == MATCH_END_ONLY)
407 type = MATCH_MIDDLE_ONLY;
409 type = MATCH_FRONT_ONLY;
412 } else { /* pattern continues, use full glob */
416 } else if (strchr("[?\\", buff[i])) {
425 static void filter_build_regex(struct filter_pred *pred)
427 struct regex *r = &pred->regex;
429 enum regex_type type = MATCH_FULL;
432 if (pred->op == OP_GLOB) {
433 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
434 r->len = strlen(search);
435 memmove(r->pattern, search, r->len+1);
440 r->match = regex_match_full;
442 case MATCH_FRONT_ONLY:
443 r->match = regex_match_front;
445 case MATCH_MIDDLE_ONLY:
446 r->match = regex_match_middle;
449 r->match = regex_match_end;
452 r->match = regex_match_glob;
465 static struct filter_pred *
466 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
467 int index, enum move_type *move)
469 if (pred->parent & FILTER_PRED_IS_RIGHT)
470 *move = MOVE_UP_FROM_RIGHT;
472 *move = MOVE_UP_FROM_LEFT;
473 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
484 typedef int (*filter_pred_walkcb_t) (enum move_type move,
485 struct filter_pred *pred,
486 int *err, void *data);
488 static int walk_pred_tree(struct filter_pred *preds,
489 struct filter_pred *root,
490 filter_pred_walkcb_t cb, void *data)
492 struct filter_pred *pred = root;
493 enum move_type move = MOVE_DOWN;
502 ret = cb(move, pred, &err, data);
503 if (ret == WALK_PRED_ABORT)
505 if (ret == WALK_PRED_PARENT)
510 if (pred->left != FILTER_PRED_INVALID) {
511 pred = &preds[pred->left];
515 case MOVE_UP_FROM_LEFT:
516 pred = &preds[pred->right];
519 case MOVE_UP_FROM_RIGHT:
523 pred = get_pred_parent(pred, preds,
536 * A series of AND or ORs where found together. Instead of
537 * climbing up and down the tree branches, an array of the
538 * ops were made in order of checks. We can just move across
539 * the array and short circuit if needed.
541 static int process_ops(struct filter_pred *preds,
542 struct filter_pred *op, void *rec)
544 struct filter_pred *pred;
550 * Micro-optimization: We set type to true if op
551 * is an OR and false otherwise (AND). Then we
552 * just need to test if the match is equal to
553 * the type, and if it is, we can short circuit the
554 * rest of the checks:
556 * if ((match && op->op == OP_OR) ||
557 * (!match && op->op == OP_AND))
560 type = op->op == OP_OR;
562 for (i = 0; i < op->val; i++) {
563 pred = &preds[op->ops[i]];
564 if (!WARN_ON_ONCE(!pred->fn))
565 match = pred->fn(pred, rec);
569 /* If not of not match is equal to not of not, then it is a match */
570 return !!match == !op->not;
573 struct filter_match_preds_data {
574 struct filter_pred *preds;
579 static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
580 int *err, void *data)
582 struct filter_match_preds_data *d = data;
587 /* only AND and OR have children */
588 if (pred->left != FILTER_PRED_INVALID) {
589 /* If ops is set, then it was folded. */
591 return WALK_PRED_DEFAULT;
592 /* We can treat folded ops as a leaf node */
593 d->match = process_ops(d->preds, pred, d->rec);
595 if (!WARN_ON_ONCE(!pred->fn))
596 d->match = pred->fn(pred, d->rec);
599 return WALK_PRED_PARENT;
600 case MOVE_UP_FROM_LEFT:
602 * Check for short circuits.
604 * Optimization: !!match == (pred->op == OP_OR)
606 * if ((match && pred->op == OP_OR) ||
607 * (!match && pred->op == OP_AND))
609 if (!!d->match == (pred->op == OP_OR))
610 return WALK_PRED_PARENT;
612 case MOVE_UP_FROM_RIGHT:
616 return WALK_PRED_DEFAULT;
619 /* return 1 if event matches, 0 otherwise (discard) */
620 int filter_match_preds(struct event_filter *filter, void *rec)
622 struct filter_pred *preds;
623 struct filter_pred *root;
624 struct filter_match_preds_data data = {
625 /* match is currently meaningless */
631 /* no filter is considered a match */
635 n_preds = filter->n_preds;
640 * n_preds, root and filter->preds are protect with preemption disabled.
642 root = rcu_dereference_sched(filter->root);
646 data.preds = preds = rcu_dereference_sched(filter->preds);
647 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
651 EXPORT_SYMBOL_GPL(filter_match_preds);
653 static void parse_error(struct filter_parse_state *ps, int err, int pos)
656 ps->lasterr_pos = pos;
659 static void remove_filter_string(struct event_filter *filter)
664 kfree(filter->filter_string);
665 filter->filter_string = NULL;
668 static int replace_filter_string(struct event_filter *filter,
671 kfree(filter->filter_string);
672 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
673 if (!filter->filter_string)
679 static int append_filter_string(struct event_filter *filter,
683 char *new_filter_string;
685 BUG_ON(!filter->filter_string);
686 newlen = strlen(filter->filter_string) + strlen(string) + 1;
687 new_filter_string = kmalloc(newlen, GFP_KERNEL);
688 if (!new_filter_string)
691 strcpy(new_filter_string, filter->filter_string);
692 strcat(new_filter_string, string);
693 kfree(filter->filter_string);
694 filter->filter_string = new_filter_string;
699 static void append_filter_err(struct filter_parse_state *ps,
700 struct event_filter *filter)
702 int pos = ps->lasterr_pos;
705 buf = (char *)__get_free_page(GFP_TEMPORARY);
709 append_filter_string(filter, "\n");
710 memset(buf, ' ', PAGE_SIZE);
711 if (pos > PAGE_SIZE - 128)
714 pbuf = &buf[pos] + 1;
716 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
717 append_filter_string(filter, buf);
718 free_page((unsigned long) buf);
721 static inline struct event_filter *event_filter(struct trace_event_file *file)
726 /* caller must hold event_mutex */
727 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
729 struct event_filter *filter = event_filter(file);
731 if (filter && filter->filter_string)
732 trace_seq_printf(s, "%s\n", filter->filter_string);
734 trace_seq_puts(s, "none\n");
737 void print_subsystem_event_filter(struct event_subsystem *system,
740 struct event_filter *filter;
742 mutex_lock(&event_mutex);
743 filter = system->filter;
744 if (filter && filter->filter_string)
745 trace_seq_printf(s, "%s\n", filter->filter_string);
747 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
748 mutex_unlock(&event_mutex);
751 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
753 stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
756 stack->index = n_preds;
760 static void __free_pred_stack(struct pred_stack *stack)
766 static int __push_pred_stack(struct pred_stack *stack,
767 struct filter_pred *pred)
769 int index = stack->index;
771 if (WARN_ON(index == 0))
774 stack->preds[--index] = pred;
775 stack->index = index;
779 static struct filter_pred *
780 __pop_pred_stack(struct pred_stack *stack)
782 struct filter_pred *pred;
783 int index = stack->index;
785 pred = stack->preds[index++];
789 stack->index = index;
793 static int filter_set_pred(struct event_filter *filter,
795 struct pred_stack *stack,
796 struct filter_pred *src)
798 struct filter_pred *dest = &filter->preds[idx];
799 struct filter_pred *left;
800 struct filter_pred *right;
805 if (dest->op == OP_OR || dest->op == OP_AND) {
806 right = __pop_pred_stack(stack);
807 left = __pop_pred_stack(stack);
811 * If both children can be folded
812 * and they are the same op as this op or a leaf,
813 * then this op can be folded.
815 if (left->index & FILTER_PRED_FOLD &&
816 ((left->op == dest->op && !left->not) ||
817 left->left == FILTER_PRED_INVALID) &&
818 right->index & FILTER_PRED_FOLD &&
819 ((right->op == dest->op && !right->not) ||
820 right->left == FILTER_PRED_INVALID))
821 dest->index |= FILTER_PRED_FOLD;
823 dest->left = left->index & ~FILTER_PRED_FOLD;
824 dest->right = right->index & ~FILTER_PRED_FOLD;
825 left->parent = dest->index & ~FILTER_PRED_FOLD;
826 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
829 * Make dest->left invalid to be used as a quick
830 * way to know this is a leaf node.
832 dest->left = FILTER_PRED_INVALID;
834 /* All leafs allow folding the parent ops. */
835 dest->index |= FILTER_PRED_FOLD;
838 return __push_pred_stack(stack, dest);
841 static void __free_preds(struct event_filter *filter)
846 for (i = 0; i < filter->n_preds; i++)
847 kfree(filter->preds[i].ops);
848 kfree(filter->preds);
849 filter->preds = NULL;
855 static void filter_disable(struct trace_event_file *file)
857 unsigned long old_flags = file->flags;
859 file->flags &= ~EVENT_FILE_FL_FILTERED;
861 if (old_flags != file->flags)
862 trace_buffered_event_disable();
865 static void __free_filter(struct event_filter *filter)
870 __free_preds(filter);
871 kfree(filter->filter_string);
875 void free_event_filter(struct event_filter *filter)
877 __free_filter(filter);
880 static struct event_filter *__alloc_filter(void)
882 struct event_filter *filter;
884 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
888 static int __alloc_preds(struct event_filter *filter, int n_preds)
890 struct filter_pred *pred;
894 __free_preds(filter);
896 filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
901 filter->a_preds = n_preds;
904 for (i = 0; i < n_preds; i++) {
905 pred = &filter->preds[i];
906 pred->fn = filter_pred_none;
912 static inline void __remove_filter(struct trace_event_file *file)
914 filter_disable(file);
915 remove_filter_string(file->filter);
918 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
919 struct trace_array *tr)
921 struct trace_event_file *file;
923 list_for_each_entry(file, &tr->events, list) {
924 if (file->system != dir)
926 __remove_filter(file);
930 static inline void __free_subsystem_filter(struct trace_event_file *file)
932 __free_filter(file->filter);
936 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
937 struct trace_array *tr)
939 struct trace_event_file *file;
941 list_for_each_entry(file, &tr->events, list) {
942 if (file->system != dir)
944 __free_subsystem_filter(file);
948 static int filter_add_pred(struct filter_parse_state *ps,
949 struct event_filter *filter,
950 struct filter_pred *pred,
951 struct pred_stack *stack)
955 if (WARN_ON(filter->n_preds == filter->a_preds)) {
956 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
960 err = filter_set_pred(filter, filter->n_preds, stack, pred);
969 int filter_assign_type(const char *type)
971 if (strstr(type, "__data_loc") && strstr(type, "char"))
972 return FILTER_DYN_STRING;
974 if (strchr(type, '[') && strstr(type, "char"))
975 return FILTER_STATIC_STRING;
980 static bool is_legal_op(struct ftrace_event_field *field, enum filter_op_ids op)
982 if (is_string_field(field) &&
983 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
985 if (!is_string_field(field) && op == OP_GLOB)
991 static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
992 int field_size, int field_is_signed)
994 filter_pred_fn_t fn = NULL;
996 switch (field_size) {
998 if (op == OP_EQ || op == OP_NE)
1000 else if (field_is_signed)
1001 fn = pred_funcs_s64[op - PRED_FUNC_START];
1003 fn = pred_funcs_u64[op - PRED_FUNC_START];
1006 if (op == OP_EQ || op == OP_NE)
1007 fn = filter_pred_32;
1008 else if (field_is_signed)
1009 fn = pred_funcs_s32[op - PRED_FUNC_START];
1011 fn = pred_funcs_u32[op - PRED_FUNC_START];
1014 if (op == OP_EQ || op == OP_NE)
1015 fn = filter_pred_16;
1016 else if (field_is_signed)
1017 fn = pred_funcs_s16[op - PRED_FUNC_START];
1019 fn = pred_funcs_u16[op - PRED_FUNC_START];
1022 if (op == OP_EQ || op == OP_NE)
1024 else if (field_is_signed)
1025 fn = pred_funcs_s8[op - PRED_FUNC_START];
1027 fn = pred_funcs_u8[op - PRED_FUNC_START];
1034 static int init_pred(struct filter_parse_state *ps,
1035 struct ftrace_event_field *field,
1036 struct filter_pred *pred)
1039 filter_pred_fn_t fn = filter_pred_none;
1040 unsigned long long val;
1043 pred->offset = field->offset;
1045 if (!is_legal_op(field, pred->op)) {
1046 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
1050 if (field->filter_type == FILTER_COMM) {
1051 filter_build_regex(pred);
1052 fn = filter_pred_comm;
1053 pred->regex.field_len = TASK_COMM_LEN;
1054 } else if (is_string_field(field)) {
1055 filter_build_regex(pred);
1057 if (field->filter_type == FILTER_STATIC_STRING) {
1058 fn = filter_pred_string;
1059 pred->regex.field_len = field->size;
1060 } else if (field->filter_type == FILTER_DYN_STRING)
1061 fn = filter_pred_strloc;
1063 fn = filter_pred_pchar;
1064 } else if (is_function_field(field)) {
1065 if (strcmp(field->name, "ip")) {
1066 parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
1070 if (field->is_signed)
1071 ret = kstrtoll(pred->regex.pattern, 0, &val);
1073 ret = kstrtoull(pred->regex.pattern, 0, &val);
1075 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
1080 if (field->filter_type == FILTER_CPU)
1081 fn = filter_pred_cpu;
1083 fn = select_comparison_fn(pred->op, field->size,
1086 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1091 if (pred->op == OP_NE)
1098 static void parse_init(struct filter_parse_state *ps,
1099 struct filter_op *ops,
1102 memset(ps, '\0', sizeof(*ps));
1104 ps->infix.string = infix_string;
1105 ps->infix.cnt = strlen(infix_string);
1108 INIT_LIST_HEAD(&ps->opstack);
1109 INIT_LIST_HEAD(&ps->postfix);
1112 static char infix_next(struct filter_parse_state *ps)
1119 return ps->infix.string[ps->infix.tail++];
1122 static char infix_peek(struct filter_parse_state *ps)
1124 if (ps->infix.tail == strlen(ps->infix.string))
1127 return ps->infix.string[ps->infix.tail];
1130 static void infix_advance(struct filter_parse_state *ps)
1139 static inline int is_precedence_lower(struct filter_parse_state *ps,
1142 return ps->ops[a].precedence < ps->ops[b].precedence;
1145 static inline int is_op_char(struct filter_parse_state *ps, char c)
1149 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1150 if (ps->ops[i].string[0] == c)
1157 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1159 char nextc = infix_peek(ps);
1167 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1168 if (!strcmp(opstr, ps->ops[i].string)) {
1170 return ps->ops[i].id;
1176 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1177 if (!strcmp(opstr, ps->ops[i].string))
1178 return ps->ops[i].id;
1184 static inline void clear_operand_string(struct filter_parse_state *ps)
1186 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1187 ps->operand.tail = 0;
1190 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1192 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1195 ps->operand.string[ps->operand.tail++] = c;
1200 static int filter_opstack_push(struct filter_parse_state *ps,
1201 enum filter_op_ids op)
1203 struct opstack_op *opstack_op;
1205 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1209 opstack_op->op = op;
1210 list_add(&opstack_op->list, &ps->opstack);
1215 static int filter_opstack_empty(struct filter_parse_state *ps)
1217 return list_empty(&ps->opstack);
1220 static int filter_opstack_top(struct filter_parse_state *ps)
1222 struct opstack_op *opstack_op;
1224 if (filter_opstack_empty(ps))
1227 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1229 return opstack_op->op;
1232 static int filter_opstack_pop(struct filter_parse_state *ps)
1234 struct opstack_op *opstack_op;
1235 enum filter_op_ids op;
1237 if (filter_opstack_empty(ps))
1240 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1241 op = opstack_op->op;
1242 list_del(&opstack_op->list);
1249 static void filter_opstack_clear(struct filter_parse_state *ps)
1251 while (!filter_opstack_empty(ps))
1252 filter_opstack_pop(ps);
1255 static char *curr_operand(struct filter_parse_state *ps)
1257 return ps->operand.string;
1260 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1262 struct postfix_elt *elt;
1264 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1269 elt->operand = kstrdup(operand, GFP_KERNEL);
1270 if (!elt->operand) {
1275 list_add_tail(&elt->list, &ps->postfix);
1280 static int postfix_append_op(struct filter_parse_state *ps, enum filter_op_ids op)
1282 struct postfix_elt *elt;
1284 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1289 elt->operand = NULL;
1291 list_add_tail(&elt->list, &ps->postfix);
1296 static void postfix_clear(struct filter_parse_state *ps)
1298 struct postfix_elt *elt;
1300 while (!list_empty(&ps->postfix)) {
1301 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1302 list_del(&elt->list);
1303 kfree(elt->operand);
1308 static int filter_parse(struct filter_parse_state *ps)
1310 enum filter_op_ids op, top_op;
1314 while ((ch = infix_next(ps))) {
1326 if (is_op_char(ps, ch)) {
1327 op = infix_get_op(ps, ch);
1328 if (op == OP_NONE) {
1329 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1333 if (strlen(curr_operand(ps))) {
1334 postfix_append_operand(ps, curr_operand(ps));
1335 clear_operand_string(ps);
1338 while (!filter_opstack_empty(ps)) {
1339 top_op = filter_opstack_top(ps);
1340 if (!is_precedence_lower(ps, top_op, op)) {
1341 top_op = filter_opstack_pop(ps);
1342 postfix_append_op(ps, top_op);
1348 filter_opstack_push(ps, op);
1353 filter_opstack_push(ps, OP_OPEN_PAREN);
1358 if (strlen(curr_operand(ps))) {
1359 postfix_append_operand(ps, curr_operand(ps));
1360 clear_operand_string(ps);
1363 top_op = filter_opstack_pop(ps);
1364 while (top_op != OP_NONE) {
1365 if (top_op == OP_OPEN_PAREN)
1367 postfix_append_op(ps, top_op);
1368 top_op = filter_opstack_pop(ps);
1370 if (top_op == OP_NONE) {
1371 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1377 if (append_operand_char(ps, ch)) {
1378 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1383 if (strlen(curr_operand(ps)))
1384 postfix_append_operand(ps, curr_operand(ps));
1386 while (!filter_opstack_empty(ps)) {
1387 top_op = filter_opstack_pop(ps);
1388 if (top_op == OP_NONE)
1390 if (top_op == OP_OPEN_PAREN) {
1391 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1394 postfix_append_op(ps, top_op);
1400 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1401 struct trace_event_call *call,
1402 enum filter_op_ids op,
1403 char *operand1, char *operand2)
1405 struct ftrace_event_field *field;
1406 static struct filter_pred pred;
1408 memset(&pred, 0, sizeof(pred));
1411 if (op == OP_AND || op == OP_OR)
1414 if (!operand1 || !operand2) {
1415 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1419 field = trace_find_event_field(call, operand1);
1421 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1425 strcpy(pred.regex.pattern, operand2);
1426 pred.regex.len = strlen(pred.regex.pattern);
1428 return init_pred(ps, field, &pred) ? NULL : &pred;
1431 static int check_preds(struct filter_parse_state *ps)
1433 int n_normal_preds = 0, n_logical_preds = 0;
1434 struct postfix_elt *elt;
1437 list_for_each_entry(elt, &ps->postfix, list) {
1438 if (elt->op == OP_NONE) {
1443 if (elt->op == OP_AND || elt->op == OP_OR) {
1448 if (elt->op != OP_NOT)
1451 /* all ops should have operands */
1456 if (cnt != 1 || !n_normal_preds || n_logical_preds >= n_normal_preds) {
1457 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1464 static int count_preds(struct filter_parse_state *ps)
1466 struct postfix_elt *elt;
1469 list_for_each_entry(elt, &ps->postfix, list) {
1470 if (elt->op == OP_NONE)
1478 struct check_pred_data {
1483 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1484 int *err, void *data)
1486 struct check_pred_data *d = data;
1488 if (WARN_ON(d->count++ > d->max)) {
1490 return WALK_PRED_ABORT;
1492 return WALK_PRED_DEFAULT;
1496 * The tree is walked at filtering of an event. If the tree is not correctly
1497 * built, it may cause an infinite loop. Check here that the tree does
1500 static int check_pred_tree(struct event_filter *filter,
1501 struct filter_pred *root)
1503 struct check_pred_data data = {
1505 * The max that we can hit a node is three times.
1506 * Once going down, once coming up from left, and
1507 * once coming up from right. This is more than enough
1508 * since leafs are only hit a single time.
1510 .max = 3 * filter->n_preds,
1514 return walk_pred_tree(filter->preds, root,
1515 check_pred_tree_cb, &data);
1518 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1519 int *err, void *data)
1523 if ((move == MOVE_DOWN) &&
1524 (pred->left == FILTER_PRED_INVALID))
1527 return WALK_PRED_DEFAULT;
1530 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1534 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1539 struct fold_pred_data {
1540 struct filter_pred *root;
1545 static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1546 int *err, void *data)
1548 struct fold_pred_data *d = data;
1549 struct filter_pred *root = d->root;
1551 if (move != MOVE_DOWN)
1552 return WALK_PRED_DEFAULT;
1553 if (pred->left != FILTER_PRED_INVALID)
1554 return WALK_PRED_DEFAULT;
1556 if (WARN_ON(d->count == d->children)) {
1558 return WALK_PRED_ABORT;
1561 pred->index &= ~FILTER_PRED_FOLD;
1562 root->ops[d->count++] = pred->index;
1563 return WALK_PRED_DEFAULT;
1566 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1568 struct fold_pred_data data = {
1574 /* No need to keep the fold flag */
1575 root->index &= ~FILTER_PRED_FOLD;
1577 /* If the root is a leaf then do nothing */
1578 if (root->left == FILTER_PRED_INVALID)
1581 /* count the children */
1582 children = count_leafs(preds, &preds[root->left]);
1583 children += count_leafs(preds, &preds[root->right]);
1585 root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
1589 root->val = children;
1590 data.children = children;
1591 return walk_pred_tree(preds, root, fold_pred_cb, &data);
1594 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1595 int *err, void *data)
1597 struct filter_pred *preds = data;
1599 if (move != MOVE_DOWN)
1600 return WALK_PRED_DEFAULT;
1601 if (!(pred->index & FILTER_PRED_FOLD))
1602 return WALK_PRED_DEFAULT;
1604 *err = fold_pred(preds, pred);
1606 return WALK_PRED_ABORT;
1608 /* eveyrhing below is folded, continue with parent */
1609 return WALK_PRED_PARENT;
1613 * To optimize the processing of the ops, if we have several "ors" or
1614 * "ands" together, we can put them in an array and process them all
1615 * together speeding up the filter logic.
1617 static int fold_pred_tree(struct event_filter *filter,
1618 struct filter_pred *root)
1620 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1624 static int replace_preds(struct trace_event_call *call,
1625 struct event_filter *filter,
1626 struct filter_parse_state *ps,
1629 char *operand1 = NULL, *operand2 = NULL;
1630 struct filter_pred *pred;
1631 struct filter_pred *root;
1632 struct postfix_elt *elt;
1633 struct pred_stack stack = { }; /* init to NULL */
1637 n_preds = count_preds(ps);
1638 if (n_preds >= MAX_FILTER_PRED) {
1639 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1643 err = check_preds(ps);
1648 err = __alloc_pred_stack(&stack, n_preds);
1651 err = __alloc_preds(filter, n_preds);
1657 list_for_each_entry(elt, &ps->postfix, list) {
1658 if (elt->op == OP_NONE) {
1660 operand1 = elt->operand;
1662 operand2 = elt->operand;
1664 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1671 if (elt->op == OP_NOT) {
1672 if (!n_preds || operand1 || operand2) {
1673 parse_error(ps, FILT_ERR_ILLEGAL_NOT_OP, 0);
1678 filter->preds[n_preds - 1].not ^= 1;
1682 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1683 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1688 pred = create_pred(ps, call, elt->op, operand1, operand2);
1695 err = filter_add_pred(ps, filter, pred, &stack);
1700 operand1 = operand2 = NULL;
1704 /* We should have one item left on the stack */
1705 pred = __pop_pred_stack(&stack);
1708 /* This item is where we start from in matching */
1710 /* Make sure the stack is empty */
1711 pred = __pop_pred_stack(&stack);
1712 if (WARN_ON(pred)) {
1714 filter->root = NULL;
1717 err = check_pred_tree(filter, root);
1721 /* Optimize the tree */
1722 err = fold_pred_tree(filter, root);
1726 /* We don't set root until we know it works */
1728 filter->root = root;
1733 __free_pred_stack(&stack);
1737 static inline void event_set_filtered_flag(struct trace_event_file *file)
1739 unsigned long old_flags = file->flags;
1741 file->flags |= EVENT_FILE_FL_FILTERED;
1743 if (old_flags != file->flags)
1744 trace_buffered_event_enable();
1747 static inline void event_set_filter(struct trace_event_file *file,
1748 struct event_filter *filter)
1750 rcu_assign_pointer(file->filter, filter);
1753 static inline void event_clear_filter(struct trace_event_file *file)
1755 RCU_INIT_POINTER(file->filter, NULL);
1759 event_set_no_set_filter_flag(struct trace_event_file *file)
1761 file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
1765 event_clear_no_set_filter_flag(struct trace_event_file *file)
1767 file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
1771 event_no_set_filter_flag(struct trace_event_file *file)
1773 if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
1779 struct filter_list {
1780 struct list_head list;
1781 struct event_filter *filter;
1784 static int replace_system_preds(struct trace_subsystem_dir *dir,
1785 struct trace_array *tr,
1786 struct filter_parse_state *ps,
1787 char *filter_string)
1789 struct trace_event_file *file;
1790 struct filter_list *filter_item;
1791 struct filter_list *tmp;
1792 LIST_HEAD(filter_list);
1796 list_for_each_entry(file, &tr->events, list) {
1797 if (file->system != dir)
1801 * Try to see if the filter can be applied
1802 * (filter arg is ignored on dry_run)
1804 err = replace_preds(file->event_call, NULL, ps, true);
1806 event_set_no_set_filter_flag(file);
1808 event_clear_no_set_filter_flag(file);
1811 list_for_each_entry(file, &tr->events, list) {
1812 struct event_filter *filter;
1814 if (file->system != dir)
1817 if (event_no_set_filter_flag(file))
1820 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1824 list_add_tail(&filter_item->list, &filter_list);
1826 filter_item->filter = __alloc_filter();
1827 if (!filter_item->filter)
1829 filter = filter_item->filter;
1831 /* Can only fail on no memory */
1832 err = replace_filter_string(filter, filter_string);
1836 err = replace_preds(file->event_call, filter, ps, false);
1838 filter_disable(file);
1839 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1840 append_filter_err(ps, filter);
1842 event_set_filtered_flag(file);
1844 * Regardless of if this returned an error, we still
1845 * replace the filter for the call.
1847 filter = event_filter(file);
1848 event_set_filter(file, filter_item->filter);
1849 filter_item->filter = filter;
1858 * The calls can still be using the old filters.
1859 * Do a synchronize_sched() to ensure all calls are
1860 * done with them before we free them.
1862 synchronize_sched();
1863 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1864 __free_filter(filter_item->filter);
1865 list_del(&filter_item->list);
1870 /* No call succeeded */
1871 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1872 list_del(&filter_item->list);
1875 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1878 /* If any call succeeded, we still need to sync */
1880 synchronize_sched();
1881 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1882 __free_filter(filter_item->filter);
1883 list_del(&filter_item->list);
1889 static int create_filter_start(char *filter_str, bool set_str,
1890 struct filter_parse_state **psp,
1891 struct event_filter **filterp)
1893 struct event_filter *filter;
1894 struct filter_parse_state *ps = NULL;
1897 WARN_ON_ONCE(*psp || *filterp);
1899 /* allocate everything, and if any fails, free all and fail */
1900 filter = __alloc_filter();
1901 if (filter && set_str)
1902 err = replace_filter_string(filter, filter_str);
1904 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1906 if (!filter || !ps || err) {
1908 __free_filter(filter);
1912 /* we're committed to creating a new filter */
1916 parse_init(ps, filter_ops, filter_str);
1917 err = filter_parse(ps);
1919 append_filter_err(ps, filter);
1923 static void create_filter_finish(struct filter_parse_state *ps)
1926 filter_opstack_clear(ps);
1933 * create_filter - create a filter for a trace_event_call
1934 * @call: trace_event_call to create a filter for
1935 * @filter_str: filter string
1936 * @set_str: remember @filter_str and enable detailed error in filter
1937 * @filterp: out param for created filter (always updated on return)
1939 * Creates a filter for @call with @filter_str. If @set_str is %true,
1940 * @filter_str is copied and recorded in the new filter.
1942 * On success, returns 0 and *@filterp points to the new filter. On
1943 * failure, returns -errno and *@filterp may point to %NULL or to a new
1944 * filter. In the latter case, the returned filter contains error
1945 * information if @set_str is %true and the caller is responsible for
1948 static int create_filter(struct trace_event_call *call,
1949 char *filter_str, bool set_str,
1950 struct event_filter **filterp)
1952 struct event_filter *filter = NULL;
1953 struct filter_parse_state *ps = NULL;
1956 err = create_filter_start(filter_str, set_str, &ps, &filter);
1958 err = replace_preds(call, filter, ps, false);
1960 append_filter_err(ps, filter);
1962 create_filter_finish(ps);
1968 int create_event_filter(struct trace_event_call *call,
1969 char *filter_str, bool set_str,
1970 struct event_filter **filterp)
1972 return create_filter(call, filter_str, set_str, filterp);
1976 * create_system_filter - create a filter for an event_subsystem
1977 * @system: event_subsystem to create a filter for
1978 * @filter_str: filter string
1979 * @filterp: out param for created filter (always updated on return)
1981 * Identical to create_filter() except that it creates a subsystem filter
1982 * and always remembers @filter_str.
1984 static int create_system_filter(struct trace_subsystem_dir *dir,
1985 struct trace_array *tr,
1986 char *filter_str, struct event_filter **filterp)
1988 struct event_filter *filter = NULL;
1989 struct filter_parse_state *ps = NULL;
1992 err = create_filter_start(filter_str, true, &ps, &filter);
1994 err = replace_system_preds(dir, tr, ps, filter_str);
1996 /* System filters just show a default message */
1997 kfree(filter->filter_string);
1998 filter->filter_string = NULL;
2000 append_filter_err(ps, filter);
2003 create_filter_finish(ps);
2009 /* caller must hold event_mutex */
2010 int apply_event_filter(struct trace_event_file *file, char *filter_string)
2012 struct trace_event_call *call = file->event_call;
2013 struct event_filter *filter;
2016 if (!strcmp(strstrip(filter_string), "0")) {
2017 filter_disable(file);
2018 filter = event_filter(file);
2023 event_clear_filter(file);
2025 /* Make sure the filter is not being used */
2026 synchronize_sched();
2027 __free_filter(filter);
2032 err = create_filter(call, filter_string, true, &filter);
2035 * Always swap the call filter with the new filter
2036 * even if there was an error. If there was an error
2037 * in the filter, we disable the filter and show the error
2041 struct event_filter *tmp;
2043 tmp = event_filter(file);
2045 event_set_filtered_flag(file);
2047 filter_disable(file);
2049 event_set_filter(file, filter);
2052 /* Make sure the call is done with the filter */
2053 synchronize_sched();
2061 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2062 char *filter_string)
2064 struct event_subsystem *system = dir->subsystem;
2065 struct trace_array *tr = dir->tr;
2066 struct event_filter *filter;
2069 mutex_lock(&event_mutex);
2071 /* Make sure the system still has events */
2072 if (!dir->nr_events) {
2077 if (!strcmp(strstrip(filter_string), "0")) {
2078 filter_free_subsystem_preds(dir, tr);
2079 remove_filter_string(system->filter);
2080 filter = system->filter;
2081 system->filter = NULL;
2082 /* Ensure all filters are no longer used */
2083 synchronize_sched();
2084 filter_free_subsystem_filters(dir, tr);
2085 __free_filter(filter);
2089 err = create_system_filter(dir, tr, filter_string, &filter);
2092 * No event actually uses the system filter
2093 * we can free it without synchronize_sched().
2095 __free_filter(system->filter);
2096 system->filter = filter;
2099 mutex_unlock(&event_mutex);
2104 #ifdef CONFIG_PERF_EVENTS
2106 void ftrace_profile_free_filter(struct perf_event *event)
2108 struct event_filter *filter = event->filter;
2110 event->filter = NULL;
2111 __free_filter(filter);
2114 struct function_filter_data {
2115 struct ftrace_ops *ops;
2120 #ifdef CONFIG_FUNCTION_TRACER
2122 ftrace_function_filter_re(char *buf, int len, int *count)
2126 str = kstrndup(buf, len, GFP_KERNEL);
2131 * The argv_split function takes white space
2132 * as a separator, so convert ',' into spaces.
2134 strreplace(str, ',', ' ');
2136 re = argv_split(GFP_KERNEL, str, count);
2141 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2142 int reset, char *re, int len)
2147 ret = ftrace_set_filter(ops, re, len, reset);
2149 ret = ftrace_set_notrace(ops, re, len, reset);
2154 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2155 struct function_filter_data *data)
2157 int i, re_cnt, ret = -EINVAL;
2161 reset = filter ? &data->first_filter : &data->first_notrace;
2164 * The 'ip' field could have multiple filters set, separated
2165 * either by space or comma. We first cut the filter and apply
2166 * all pieces separatelly.
2168 re = ftrace_function_filter_re(buf, len, &re_cnt);
2172 for (i = 0; i < re_cnt; i++) {
2173 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2174 re[i], strlen(re[i]));
2186 static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
2188 struct ftrace_event_field *field = pred->field;
2192 * Check the leaf predicate for function trace, verify:
2193 * - only '==' and '!=' is used
2194 * - the 'ip' field is used
2196 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2199 if (strcmp(field->name, "ip"))
2203 * Check the non leaf predicate for function trace, verify:
2204 * - only '||' is used
2206 if (pred->op != OP_OR)
2213 static int ftrace_function_set_filter_cb(enum move_type move,
2214 struct filter_pred *pred,
2215 int *err, void *data)
2217 /* Checking the node is valid for function trace. */
2218 if ((move != MOVE_DOWN) ||
2219 (pred->left != FILTER_PRED_INVALID)) {
2220 *err = ftrace_function_check_pred(pred, 0);
2222 *err = ftrace_function_check_pred(pred, 1);
2224 return WALK_PRED_ABORT;
2226 *err = __ftrace_function_set_filter(pred->op == OP_EQ,
2227 pred->regex.pattern,
2232 return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
2235 static int ftrace_function_set_filter(struct perf_event *event,
2236 struct event_filter *filter)
2238 struct function_filter_data data = {
2241 .ops = &event->ftrace_ops,
2244 return walk_pred_tree(filter->preds, filter->root,
2245 ftrace_function_set_filter_cb, &data);
2248 static int ftrace_function_set_filter(struct perf_event *event,
2249 struct event_filter *filter)
2253 #endif /* CONFIG_FUNCTION_TRACER */
2255 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2259 struct event_filter *filter;
2260 struct trace_event_call *call;
2262 mutex_lock(&event_mutex);
2264 call = event->tp_event;
2274 err = create_filter(call, filter_str, false, &filter);
2278 if (ftrace_event_is_function(call))
2279 err = ftrace_function_set_filter(event, filter);
2281 event->filter = filter;
2284 if (err || ftrace_event_is_function(call))
2285 __free_filter(filter);
2288 mutex_unlock(&event_mutex);
2293 #endif /* CONFIG_PERF_EVENTS */
2295 #ifdef CONFIG_FTRACE_STARTUP_TEST
2297 #include <linux/types.h>
2298 #include <linux/tracepoint.h>
2300 #define CREATE_TRACE_POINTS
2301 #include "trace_events_filter_test.h"
2303 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2306 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2307 .e = ve, .f = vf, .g = vg, .h = vh }, \
2309 .not_visited = nvisit, \
2314 static struct test_filter_data_t {
2316 struct trace_event_raw_ftrace_test_filter rec;
2319 } test_filter_data[] = {
2320 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2321 "e == 1 && f == 1 && g == 1 && h == 1"
2322 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2323 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2324 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2326 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2327 "e == 1 || f == 1 || g == 1 || h == 1"
2328 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2329 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2330 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2332 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2333 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2334 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2335 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2336 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2337 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2339 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2340 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2341 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2342 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2343 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2345 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2346 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2347 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2348 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2349 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2351 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2352 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2353 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2354 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2355 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2357 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2358 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2359 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2360 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2361 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2363 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2364 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2365 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2366 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2367 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2375 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2377 static int test_pred_visited;
2379 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2381 struct ftrace_event_field *field = pred->field;
2383 test_pred_visited = 1;
2384 printk(KERN_INFO "\npred visited %s\n", field->name);
2388 static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2389 int *err, void *data)
2391 char *fields = data;
2393 if ((move == MOVE_DOWN) &&
2394 (pred->left == FILTER_PRED_INVALID)) {
2395 struct ftrace_event_field *field = pred->field;
2398 WARN(1, "all leafs should have field defined");
2399 return WALK_PRED_DEFAULT;
2401 if (!strchr(fields, *field->name))
2402 return WALK_PRED_DEFAULT;
2405 pred->fn = test_pred_visited_fn;
2407 return WALK_PRED_DEFAULT;
2410 static __init int ftrace_test_event_filter(void)
2414 printk(KERN_INFO "Testing ftrace filter: ");
2416 for (i = 0; i < DATA_CNT; i++) {
2417 struct event_filter *filter = NULL;
2418 struct test_filter_data_t *d = &test_filter_data[i];
2421 err = create_filter(&event_ftrace_test_filter, d->filter,
2425 "Failed to get filter for '%s', err %d\n",
2427 __free_filter(filter);
2432 * The preemption disabling is not really needed for self
2433 * tests, but the rcu dereference will complain without it.
2436 if (*d->not_visited)
2437 walk_pred_tree(filter->preds, filter->root,
2441 test_pred_visited = 0;
2442 err = filter_match_preds(filter, &d->rec);
2445 __free_filter(filter);
2447 if (test_pred_visited) {
2449 "Failed, unwanted pred visited for filter %s\n",
2454 if (err != d->match) {
2456 "Failed to match filter '%s', expected %d\n",
2457 d->filter, d->match);
2463 printk(KERN_CONT "OK\n");
2468 late_initcall(ftrace_test_event_filter);
2470 #endif /* CONFIG_FTRACE_STARTUP_TEST */