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"
58 /* Order must be the same as enum filter_op_ids above */
59 static struct filter_op filter_ops[] = {
70 { OP_NONE, "OP_NONE", 0 },
71 { OP_OPEN_PAREN, "(", 0 },
77 FILT_ERR_UNBALANCED_PAREN,
78 FILT_ERR_TOO_MANY_OPERANDS,
79 FILT_ERR_OPERAND_TOO_LONG,
80 FILT_ERR_FIELD_NOT_FOUND,
81 FILT_ERR_ILLEGAL_FIELD_OP,
82 FILT_ERR_ILLEGAL_INTVAL,
83 FILT_ERR_BAD_SUBSYS_FILTER,
84 FILT_ERR_TOO_MANY_PREDS,
85 FILT_ERR_MISSING_FIELD,
86 FILT_ERR_INVALID_FILTER,
87 FILT_ERR_IP_FIELD_ONLY,
90 static char *err_text[] = {
97 "Illegal operation for field type",
98 "Illegal integer value",
99 "Couldn't find or set field in one of a subsystem's events",
100 "Too many terms in predicate expression",
101 "Missing field name and/or value",
102 "Meaningless filter expression",
103 "Only 'ip' field is supported for function trace",
108 struct list_head list;
114 struct list_head list;
117 struct filter_parse_state {
118 struct filter_op *ops;
119 struct list_head opstack;
120 struct list_head postfix;
131 char string[MAX_FILTER_STR_VAL];
138 struct filter_pred **preds;
142 #define DEFINE_COMPARISON_PRED(type) \
143 static int filter_pred_##type(struct filter_pred *pred, void *event) \
145 type *addr = (type *)(event + pred->offset); \
146 type val = (type)pred->val; \
149 switch (pred->op) { \
151 match = (*addr < val); \
154 match = (*addr <= val); \
157 match = (*addr > val); \
160 match = (*addr >= val); \
163 match = (*addr & val); \
172 #define DEFINE_EQUALITY_PRED(size) \
173 static int filter_pred_##size(struct filter_pred *pred, void *event) \
175 u##size *addr = (u##size *)(event + pred->offset); \
176 u##size val = (u##size)pred->val; \
179 match = (val == *addr) ^ pred->not; \
184 DEFINE_COMPARISON_PRED(s64);
185 DEFINE_COMPARISON_PRED(u64);
186 DEFINE_COMPARISON_PRED(s32);
187 DEFINE_COMPARISON_PRED(u32);
188 DEFINE_COMPARISON_PRED(s16);
189 DEFINE_COMPARISON_PRED(u16);
190 DEFINE_COMPARISON_PRED(s8);
191 DEFINE_COMPARISON_PRED(u8);
193 DEFINE_EQUALITY_PRED(64);
194 DEFINE_EQUALITY_PRED(32);
195 DEFINE_EQUALITY_PRED(16);
196 DEFINE_EQUALITY_PRED(8);
198 /* Filter predicate for fixed sized arrays of characters */
199 static int filter_pred_string(struct filter_pred *pred, void *event)
201 char *addr = (char *)(event + pred->offset);
204 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
206 match = cmp ^ pred->not;
211 /* Filter predicate for char * pointers */
212 static int filter_pred_pchar(struct filter_pred *pred, void *event)
214 char **addr = (char **)(event + pred->offset);
216 int len = strlen(*addr) + 1; /* including tailing '\0' */
218 cmp = pred->regex.match(*addr, &pred->regex, len);
220 match = cmp ^ pred->not;
226 * Filter predicate for dynamic sized arrays of characters.
227 * These are implemented through a list of strings at the end
229 * Also each of these strings have a field in the entry which
230 * contains its offset from the beginning of the entry.
231 * We have then first to get this field, dereference it
232 * and add it to the address of the entry, and at last we have
233 * the address of the string.
235 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)
256 * regex_match_foo - Basic regex callbacks
258 * @str: the string to be searched
259 * @r: the regex structure containing the pattern string
260 * @len: the length of the string to be searched (including '\0')
263 * - @str might not be NULL-terminated if it's of type DYN_STRING
267 static int regex_match_full(char *str, struct regex *r, int len)
269 if (strncmp(str, r->pattern, len) == 0)
274 static int regex_match_front(char *str, struct regex *r, int len)
276 if (strncmp(str, r->pattern, r->len) == 0)
281 static int regex_match_middle(char *str, struct regex *r, int len)
283 if (strnstr(str, r->pattern, len))
288 static int regex_match_end(char *str, struct regex *r, int len)
290 int strlen = len - 1;
292 if (strlen >= r->len &&
293 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
299 * filter_parse_regex - parse a basic regex
300 * @buff: the raw regex
301 * @len: length of the regex
302 * @search: will point to the beginning of the string to compare
303 * @not: tell whether the match will have to be inverted
305 * This passes in a buffer containing a regex and this function will
306 * set search to point to the search part of the buffer and
307 * return the type of search it is (see enum above).
308 * This does modify buff.
311 * search returns the pointer to use for comparison.
312 * not returns 1 if buff started with a '!'
315 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
317 int type = MATCH_FULL;
320 if (buff[0] == '!') {
329 for (i = 0; i < len; i++) {
330 if (buff[i] == '*') {
333 type = MATCH_END_ONLY;
335 if (type == MATCH_END_ONLY)
336 type = MATCH_MIDDLE_ONLY;
338 type = MATCH_FRONT_ONLY;
348 static void filter_build_regex(struct filter_pred *pred)
350 struct regex *r = &pred->regex;
352 enum regex_type type = MATCH_FULL;
355 if (pred->op == OP_GLOB) {
356 type = filter_parse_regex(r->pattern, r->len, &search, ¬);
357 r->len = strlen(search);
358 memmove(r->pattern, search, r->len+1);
363 r->match = regex_match_full;
365 case MATCH_FRONT_ONLY:
366 r->match = regex_match_front;
368 case MATCH_MIDDLE_ONLY:
369 r->match = regex_match_middle;
372 r->match = regex_match_end;
385 static struct filter_pred *
386 get_pred_parent(struct filter_pred *pred, struct filter_pred *preds,
387 int index, enum move_type *move)
389 if (pred->parent & FILTER_PRED_IS_RIGHT)
390 *move = MOVE_UP_FROM_RIGHT;
392 *move = MOVE_UP_FROM_LEFT;
393 pred = &preds[pred->parent & ~FILTER_PRED_IS_RIGHT];
404 typedef int (*filter_pred_walkcb_t) (enum move_type move,
405 struct filter_pred *pred,
406 int *err, void *data);
408 static int walk_pred_tree(struct filter_pred *preds,
409 struct filter_pred *root,
410 filter_pred_walkcb_t cb, void *data)
412 struct filter_pred *pred = root;
413 enum move_type move = MOVE_DOWN;
422 ret = cb(move, pred, &err, data);
423 if (ret == WALK_PRED_ABORT)
425 if (ret == WALK_PRED_PARENT)
430 if (pred->left != FILTER_PRED_INVALID) {
431 pred = &preds[pred->left];
435 case MOVE_UP_FROM_LEFT:
436 pred = &preds[pred->right];
439 case MOVE_UP_FROM_RIGHT:
443 pred = get_pred_parent(pred, preds,
456 * A series of AND or ORs where found together. Instead of
457 * climbing up and down the tree branches, an array of the
458 * ops were made in order of checks. We can just move across
459 * the array and short circuit if needed.
461 static int process_ops(struct filter_pred *preds,
462 struct filter_pred *op, void *rec)
464 struct filter_pred *pred;
470 * Micro-optimization: We set type to true if op
471 * is an OR and false otherwise (AND). Then we
472 * just need to test if the match is equal to
473 * the type, and if it is, we can short circuit the
474 * rest of the checks:
476 * if ((match && op->op == OP_OR) ||
477 * (!match && op->op == OP_AND))
480 type = op->op == OP_OR;
482 for (i = 0; i < op->val; i++) {
483 pred = &preds[op->ops[i]];
484 if (!WARN_ON_ONCE(!pred->fn))
485 match = pred->fn(pred, rec);
492 struct filter_match_preds_data {
493 struct filter_pred *preds;
498 static int filter_match_preds_cb(enum move_type move, struct filter_pred *pred,
499 int *err, void *data)
501 struct filter_match_preds_data *d = data;
506 /* only AND and OR have children */
507 if (pred->left != FILTER_PRED_INVALID) {
508 /* If ops is set, then it was folded. */
510 return WALK_PRED_DEFAULT;
511 /* We can treat folded ops as a leaf node */
512 d->match = process_ops(d->preds, pred, d->rec);
514 if (!WARN_ON_ONCE(!pred->fn))
515 d->match = pred->fn(pred, d->rec);
518 return WALK_PRED_PARENT;
519 case MOVE_UP_FROM_LEFT:
521 * Check for short circuits.
523 * Optimization: !!match == (pred->op == OP_OR)
525 * if ((match && pred->op == OP_OR) ||
526 * (!match && pred->op == OP_AND))
528 if (!!d->match == (pred->op == OP_OR))
529 return WALK_PRED_PARENT;
531 case MOVE_UP_FROM_RIGHT:
535 return WALK_PRED_DEFAULT;
538 /* return 1 if event matches, 0 otherwise (discard) */
539 int filter_match_preds(struct event_filter *filter, void *rec)
541 struct filter_pred *preds;
542 struct filter_pred *root;
543 struct filter_match_preds_data data = {
544 /* match is currently meaningless */
550 /* no filter is considered a match */
554 n_preds = filter->n_preds;
559 * n_preds, root and filter->preds are protect with preemption disabled.
561 root = rcu_dereference_sched(filter->root);
565 data.preds = preds = rcu_dereference_sched(filter->preds);
566 ret = walk_pred_tree(preds, root, filter_match_preds_cb, &data);
570 EXPORT_SYMBOL_GPL(filter_match_preds);
572 static void parse_error(struct filter_parse_state *ps, int err, int pos)
575 ps->lasterr_pos = pos;
578 static void remove_filter_string(struct event_filter *filter)
583 kfree(filter->filter_string);
584 filter->filter_string = NULL;
587 static int replace_filter_string(struct event_filter *filter,
590 kfree(filter->filter_string);
591 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
592 if (!filter->filter_string)
598 static int append_filter_string(struct event_filter *filter,
602 char *new_filter_string;
604 BUG_ON(!filter->filter_string);
605 newlen = strlen(filter->filter_string) + strlen(string) + 1;
606 new_filter_string = kmalloc(newlen, GFP_KERNEL);
607 if (!new_filter_string)
610 strcpy(new_filter_string, filter->filter_string);
611 strcat(new_filter_string, string);
612 kfree(filter->filter_string);
613 filter->filter_string = new_filter_string;
618 static void append_filter_err(struct filter_parse_state *ps,
619 struct event_filter *filter)
621 int pos = ps->lasterr_pos;
624 buf = (char *)__get_free_page(GFP_TEMPORARY);
628 append_filter_string(filter, "\n");
629 memset(buf, ' ', PAGE_SIZE);
630 if (pos > PAGE_SIZE - 128)
633 pbuf = &buf[pos] + 1;
635 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
636 append_filter_string(filter, buf);
637 free_page((unsigned long) buf);
640 static inline struct event_filter *event_filter(struct ftrace_event_file *file)
642 if (file->event_call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
643 return file->event_call->filter;
648 /* caller must hold event_mutex */
649 void print_event_filter(struct ftrace_event_file *file, struct trace_seq *s)
651 struct event_filter *filter = event_filter(file);
653 if (filter && filter->filter_string)
654 trace_seq_printf(s, "%s\n", filter->filter_string);
656 trace_seq_puts(s, "none\n");
659 void print_subsystem_event_filter(struct event_subsystem *system,
662 struct event_filter *filter;
664 mutex_lock(&event_mutex);
665 filter = system->filter;
666 if (filter && filter->filter_string)
667 trace_seq_printf(s, "%s\n", filter->filter_string);
669 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
670 mutex_unlock(&event_mutex);
673 static int __alloc_pred_stack(struct pred_stack *stack, int n_preds)
675 stack->preds = kcalloc(n_preds + 1, sizeof(*stack->preds), GFP_KERNEL);
678 stack->index = n_preds;
682 static void __free_pred_stack(struct pred_stack *stack)
688 static int __push_pred_stack(struct pred_stack *stack,
689 struct filter_pred *pred)
691 int index = stack->index;
693 if (WARN_ON(index == 0))
696 stack->preds[--index] = pred;
697 stack->index = index;
701 static struct filter_pred *
702 __pop_pred_stack(struct pred_stack *stack)
704 struct filter_pred *pred;
705 int index = stack->index;
707 pred = stack->preds[index++];
711 stack->index = index;
715 static int filter_set_pred(struct event_filter *filter,
717 struct pred_stack *stack,
718 struct filter_pred *src)
720 struct filter_pred *dest = &filter->preds[idx];
721 struct filter_pred *left;
722 struct filter_pred *right;
727 if (dest->op == OP_OR || dest->op == OP_AND) {
728 right = __pop_pred_stack(stack);
729 left = __pop_pred_stack(stack);
733 * If both children can be folded
734 * and they are the same op as this op or a leaf,
735 * then this op can be folded.
737 if (left->index & FILTER_PRED_FOLD &&
738 (left->op == dest->op ||
739 left->left == FILTER_PRED_INVALID) &&
740 right->index & FILTER_PRED_FOLD &&
741 (right->op == dest->op ||
742 right->left == FILTER_PRED_INVALID))
743 dest->index |= FILTER_PRED_FOLD;
745 dest->left = left->index & ~FILTER_PRED_FOLD;
746 dest->right = right->index & ~FILTER_PRED_FOLD;
747 left->parent = dest->index & ~FILTER_PRED_FOLD;
748 right->parent = dest->index | FILTER_PRED_IS_RIGHT;
751 * Make dest->left invalid to be used as a quick
752 * way to know this is a leaf node.
754 dest->left = FILTER_PRED_INVALID;
756 /* All leafs allow folding the parent ops. */
757 dest->index |= FILTER_PRED_FOLD;
760 return __push_pred_stack(stack, dest);
763 static void __free_preds(struct event_filter *filter)
768 for (i = 0; i < filter->n_preds; i++)
769 kfree(filter->preds[i].ops);
770 kfree(filter->preds);
771 filter->preds = NULL;
777 static void call_filter_disable(struct ftrace_event_call *call)
779 call->flags &= ~TRACE_EVENT_FL_FILTERED;
782 static void filter_disable(struct ftrace_event_file *file)
784 struct ftrace_event_call *call = file->event_call;
786 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
787 call_filter_disable(call);
789 file->flags &= ~FTRACE_EVENT_FL_FILTERED;
792 static void __free_filter(struct event_filter *filter)
797 __free_preds(filter);
798 kfree(filter->filter_string);
802 void destroy_call_preds(struct ftrace_event_call *call)
804 __free_filter(call->filter);
808 static void destroy_file_preds(struct ftrace_event_file *file)
810 __free_filter(file->filter);
815 * Called when destroying the ftrace_event_file.
816 * The file is being freed, so we do not need to worry about
817 * the file being currently used. This is for module code removing
818 * the tracepoints from within it.
820 void destroy_preds(struct ftrace_event_file *file)
822 if (file->event_call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
823 destroy_call_preds(file->event_call);
825 destroy_file_preds(file);
828 static struct event_filter *__alloc_filter(void)
830 struct event_filter *filter;
832 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
836 static int __alloc_preds(struct event_filter *filter, int n_preds)
838 struct filter_pred *pred;
842 __free_preds(filter);
844 filter->preds = kcalloc(n_preds, sizeof(*filter->preds), GFP_KERNEL);
849 filter->a_preds = n_preds;
852 for (i = 0; i < n_preds; i++) {
853 pred = &filter->preds[i];
854 pred->fn = filter_pred_none;
860 static inline void __remove_filter(struct ftrace_event_file *file)
862 struct ftrace_event_call *call = file->event_call;
864 filter_disable(file);
865 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
866 remove_filter_string(call->filter);
868 remove_filter_string(file->filter);
871 static void filter_free_subsystem_preds(struct event_subsystem *system,
872 struct trace_array *tr)
874 struct ftrace_event_file *file;
875 struct ftrace_event_call *call;
877 list_for_each_entry(file, &tr->events, list) {
878 call = file->event_call;
879 if (strcmp(call->class->system, system->name) != 0)
882 __remove_filter(file);
886 static inline void __free_subsystem_filter(struct ftrace_event_file *file)
888 struct ftrace_event_call *call = file->event_call;
890 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) {
891 __free_filter(call->filter);
894 __free_filter(file->filter);
899 static void filter_free_subsystem_filters(struct event_subsystem *system,
900 struct trace_array *tr)
902 struct ftrace_event_file *file;
903 struct ftrace_event_call *call;
905 list_for_each_entry(file, &tr->events, list) {
906 call = file->event_call;
907 if (strcmp(call->class->system, system->name) != 0)
909 __free_subsystem_filter(file);
913 static int filter_add_pred(struct filter_parse_state *ps,
914 struct event_filter *filter,
915 struct filter_pred *pred,
916 struct pred_stack *stack)
920 if (WARN_ON(filter->n_preds == filter->a_preds)) {
921 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
925 err = filter_set_pred(filter, filter->n_preds, stack, pred);
934 int filter_assign_type(const char *type)
936 if (strstr(type, "__data_loc") && strstr(type, "char"))
937 return FILTER_DYN_STRING;
939 if (strchr(type, '[') && strstr(type, "char"))
940 return FILTER_STATIC_STRING;
945 static bool is_function_field(struct ftrace_event_field *field)
947 return field->filter_type == FILTER_TRACE_FN;
950 static bool is_string_field(struct ftrace_event_field *field)
952 return field->filter_type == FILTER_DYN_STRING ||
953 field->filter_type == FILTER_STATIC_STRING ||
954 field->filter_type == FILTER_PTR_STRING;
957 static int is_legal_op(struct ftrace_event_field *field, int op)
959 if (is_string_field(field) &&
960 (op != OP_EQ && op != OP_NE && op != OP_GLOB))
962 if (!is_string_field(field) && op == OP_GLOB)
968 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
971 filter_pred_fn_t fn = NULL;
973 switch (field_size) {
975 if (op == OP_EQ || op == OP_NE)
977 else if (field_is_signed)
978 fn = filter_pred_s64;
980 fn = filter_pred_u64;
983 if (op == OP_EQ || op == OP_NE)
985 else if (field_is_signed)
986 fn = filter_pred_s32;
988 fn = filter_pred_u32;
991 if (op == OP_EQ || op == OP_NE)
993 else if (field_is_signed)
994 fn = filter_pred_s16;
996 fn = filter_pred_u16;
999 if (op == OP_EQ || op == OP_NE)
1001 else if (field_is_signed)
1002 fn = filter_pred_s8;
1004 fn = filter_pred_u8;
1011 static int init_pred(struct filter_parse_state *ps,
1012 struct ftrace_event_field *field,
1013 struct filter_pred *pred)
1016 filter_pred_fn_t fn = filter_pred_none;
1017 unsigned long long val;
1020 pred->offset = field->offset;
1022 if (!is_legal_op(field, pred->op)) {
1023 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
1027 if (is_string_field(field)) {
1028 filter_build_regex(pred);
1030 if (field->filter_type == FILTER_STATIC_STRING) {
1031 fn = filter_pred_string;
1032 pred->regex.field_len = field->size;
1033 } else if (field->filter_type == FILTER_DYN_STRING)
1034 fn = filter_pred_strloc;
1036 fn = filter_pred_pchar;
1037 } else if (is_function_field(field)) {
1038 if (strcmp(field->name, "ip")) {
1039 parse_error(ps, FILT_ERR_IP_FIELD_ONLY, 0);
1043 if (field->is_signed)
1044 ret = kstrtoll(pred->regex.pattern, 0, &val);
1046 ret = kstrtoull(pred->regex.pattern, 0, &val);
1048 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
1053 fn = select_comparison_fn(pred->op, field->size,
1056 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1061 if (pred->op == OP_NE)
1068 static void parse_init(struct filter_parse_state *ps,
1069 struct filter_op *ops,
1072 memset(ps, '\0', sizeof(*ps));
1074 ps->infix.string = infix_string;
1075 ps->infix.cnt = strlen(infix_string);
1078 INIT_LIST_HEAD(&ps->opstack);
1079 INIT_LIST_HEAD(&ps->postfix);
1082 static char infix_next(struct filter_parse_state *ps)
1086 return ps->infix.string[ps->infix.tail++];
1089 static char infix_peek(struct filter_parse_state *ps)
1091 if (ps->infix.tail == strlen(ps->infix.string))
1094 return ps->infix.string[ps->infix.tail];
1097 static void infix_advance(struct filter_parse_state *ps)
1103 static inline int is_precedence_lower(struct filter_parse_state *ps,
1106 return ps->ops[a].precedence < ps->ops[b].precedence;
1109 static inline int is_op_char(struct filter_parse_state *ps, char c)
1113 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1114 if (ps->ops[i].string[0] == c)
1121 static int infix_get_op(struct filter_parse_state *ps, char firstc)
1123 char nextc = infix_peek(ps);
1131 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1132 if (!strcmp(opstr, ps->ops[i].string)) {
1134 return ps->ops[i].id;
1140 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
1141 if (!strcmp(opstr, ps->ops[i].string))
1142 return ps->ops[i].id;
1148 static inline void clear_operand_string(struct filter_parse_state *ps)
1150 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
1151 ps->operand.tail = 0;
1154 static inline int append_operand_char(struct filter_parse_state *ps, char c)
1156 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
1159 ps->operand.string[ps->operand.tail++] = c;
1164 static int filter_opstack_push(struct filter_parse_state *ps, int op)
1166 struct opstack_op *opstack_op;
1168 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
1172 opstack_op->op = op;
1173 list_add(&opstack_op->list, &ps->opstack);
1178 static int filter_opstack_empty(struct filter_parse_state *ps)
1180 return list_empty(&ps->opstack);
1183 static int filter_opstack_top(struct filter_parse_state *ps)
1185 struct opstack_op *opstack_op;
1187 if (filter_opstack_empty(ps))
1190 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1192 return opstack_op->op;
1195 static int filter_opstack_pop(struct filter_parse_state *ps)
1197 struct opstack_op *opstack_op;
1200 if (filter_opstack_empty(ps))
1203 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
1204 op = opstack_op->op;
1205 list_del(&opstack_op->list);
1212 static void filter_opstack_clear(struct filter_parse_state *ps)
1214 while (!filter_opstack_empty(ps))
1215 filter_opstack_pop(ps);
1218 static char *curr_operand(struct filter_parse_state *ps)
1220 return ps->operand.string;
1223 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
1225 struct postfix_elt *elt;
1227 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1232 elt->operand = kstrdup(operand, GFP_KERNEL);
1233 if (!elt->operand) {
1238 list_add_tail(&elt->list, &ps->postfix);
1243 static int postfix_append_op(struct filter_parse_state *ps, int op)
1245 struct postfix_elt *elt;
1247 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
1252 elt->operand = NULL;
1254 list_add_tail(&elt->list, &ps->postfix);
1259 static void postfix_clear(struct filter_parse_state *ps)
1261 struct postfix_elt *elt;
1263 while (!list_empty(&ps->postfix)) {
1264 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
1265 list_del(&elt->list);
1266 kfree(elt->operand);
1271 static int filter_parse(struct filter_parse_state *ps)
1277 while ((ch = infix_next(ps))) {
1289 if (is_op_char(ps, ch)) {
1290 op = infix_get_op(ps, ch);
1291 if (op == OP_NONE) {
1292 parse_error(ps, FILT_ERR_INVALID_OP, 0);
1296 if (strlen(curr_operand(ps))) {
1297 postfix_append_operand(ps, curr_operand(ps));
1298 clear_operand_string(ps);
1301 while (!filter_opstack_empty(ps)) {
1302 top_op = filter_opstack_top(ps);
1303 if (!is_precedence_lower(ps, top_op, op)) {
1304 top_op = filter_opstack_pop(ps);
1305 postfix_append_op(ps, top_op);
1311 filter_opstack_push(ps, op);
1316 filter_opstack_push(ps, OP_OPEN_PAREN);
1321 if (strlen(curr_operand(ps))) {
1322 postfix_append_operand(ps, curr_operand(ps));
1323 clear_operand_string(ps);
1326 top_op = filter_opstack_pop(ps);
1327 while (top_op != OP_NONE) {
1328 if (top_op == OP_OPEN_PAREN)
1330 postfix_append_op(ps, top_op);
1331 top_op = filter_opstack_pop(ps);
1333 if (top_op == OP_NONE) {
1334 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1340 if (append_operand_char(ps, ch)) {
1341 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1346 if (strlen(curr_operand(ps)))
1347 postfix_append_operand(ps, curr_operand(ps));
1349 while (!filter_opstack_empty(ps)) {
1350 top_op = filter_opstack_pop(ps);
1351 if (top_op == OP_NONE)
1353 if (top_op == OP_OPEN_PAREN) {
1354 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1357 postfix_append_op(ps, top_op);
1363 static struct filter_pred *create_pred(struct filter_parse_state *ps,
1364 struct ftrace_event_call *call,
1365 int op, char *operand1, char *operand2)
1367 struct ftrace_event_field *field;
1368 static struct filter_pred pred;
1370 memset(&pred, 0, sizeof(pred));
1373 if (op == OP_AND || op == OP_OR)
1376 if (!operand1 || !operand2) {
1377 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1381 field = trace_find_event_field(call, operand1);
1383 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
1387 strcpy(pred.regex.pattern, operand2);
1388 pred.regex.len = strlen(pred.regex.pattern);
1390 return init_pred(ps, field, &pred) ? NULL : &pred;
1393 static int check_preds(struct filter_parse_state *ps)
1395 int n_normal_preds = 0, n_logical_preds = 0;
1396 struct postfix_elt *elt;
1398 list_for_each_entry(elt, &ps->postfix, list) {
1399 if (elt->op == OP_NONE)
1402 if (elt->op == OP_AND || elt->op == OP_OR) {
1409 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1410 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1417 static int count_preds(struct filter_parse_state *ps)
1419 struct postfix_elt *elt;
1422 list_for_each_entry(elt, &ps->postfix, list) {
1423 if (elt->op == OP_NONE)
1431 struct check_pred_data {
1436 static int check_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1437 int *err, void *data)
1439 struct check_pred_data *d = data;
1441 if (WARN_ON(d->count++ > d->max)) {
1443 return WALK_PRED_ABORT;
1445 return WALK_PRED_DEFAULT;
1449 * The tree is walked at filtering of an event. If the tree is not correctly
1450 * built, it may cause an infinite loop. Check here that the tree does
1453 static int check_pred_tree(struct event_filter *filter,
1454 struct filter_pred *root)
1456 struct check_pred_data data = {
1458 * The max that we can hit a node is three times.
1459 * Once going down, once coming up from left, and
1460 * once coming up from right. This is more than enough
1461 * since leafs are only hit a single time.
1463 .max = 3 * filter->n_preds,
1467 return walk_pred_tree(filter->preds, root,
1468 check_pred_tree_cb, &data);
1471 static int count_leafs_cb(enum move_type move, struct filter_pred *pred,
1472 int *err, void *data)
1476 if ((move == MOVE_DOWN) &&
1477 (pred->left == FILTER_PRED_INVALID))
1480 return WALK_PRED_DEFAULT;
1483 static int count_leafs(struct filter_pred *preds, struct filter_pred *root)
1487 ret = walk_pred_tree(preds, root, count_leafs_cb, &count);
1492 struct fold_pred_data {
1493 struct filter_pred *root;
1498 static int fold_pred_cb(enum move_type move, struct filter_pred *pred,
1499 int *err, void *data)
1501 struct fold_pred_data *d = data;
1502 struct filter_pred *root = d->root;
1504 if (move != MOVE_DOWN)
1505 return WALK_PRED_DEFAULT;
1506 if (pred->left != FILTER_PRED_INVALID)
1507 return WALK_PRED_DEFAULT;
1509 if (WARN_ON(d->count == d->children)) {
1511 return WALK_PRED_ABORT;
1514 pred->index &= ~FILTER_PRED_FOLD;
1515 root->ops[d->count++] = pred->index;
1516 return WALK_PRED_DEFAULT;
1519 static int fold_pred(struct filter_pred *preds, struct filter_pred *root)
1521 struct fold_pred_data data = {
1527 /* No need to keep the fold flag */
1528 root->index &= ~FILTER_PRED_FOLD;
1530 /* If the root is a leaf then do nothing */
1531 if (root->left == FILTER_PRED_INVALID)
1534 /* count the children */
1535 children = count_leafs(preds, &preds[root->left]);
1536 children += count_leafs(preds, &preds[root->right]);
1538 root->ops = kcalloc(children, sizeof(*root->ops), GFP_KERNEL);
1542 root->val = children;
1543 data.children = children;
1544 return walk_pred_tree(preds, root, fold_pred_cb, &data);
1547 static int fold_pred_tree_cb(enum move_type move, struct filter_pred *pred,
1548 int *err, void *data)
1550 struct filter_pred *preds = data;
1552 if (move != MOVE_DOWN)
1553 return WALK_PRED_DEFAULT;
1554 if (!(pred->index & FILTER_PRED_FOLD))
1555 return WALK_PRED_DEFAULT;
1557 *err = fold_pred(preds, pred);
1559 return WALK_PRED_ABORT;
1561 /* eveyrhing below is folded, continue with parent */
1562 return WALK_PRED_PARENT;
1566 * To optimize the processing of the ops, if we have several "ors" or
1567 * "ands" together, we can put them in an array and process them all
1568 * together speeding up the filter logic.
1570 static int fold_pred_tree(struct event_filter *filter,
1571 struct filter_pred *root)
1573 return walk_pred_tree(filter->preds, root, fold_pred_tree_cb,
1577 static int replace_preds(struct ftrace_event_call *call,
1578 struct event_filter *filter,
1579 struct filter_parse_state *ps,
1580 char *filter_string,
1583 char *operand1 = NULL, *operand2 = NULL;
1584 struct filter_pred *pred;
1585 struct filter_pred *root;
1586 struct postfix_elt *elt;
1587 struct pred_stack stack = { }; /* init to NULL */
1591 n_preds = count_preds(ps);
1592 if (n_preds >= MAX_FILTER_PRED) {
1593 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1597 err = check_preds(ps);
1602 err = __alloc_pred_stack(&stack, n_preds);
1605 err = __alloc_preds(filter, n_preds);
1611 list_for_each_entry(elt, &ps->postfix, list) {
1612 if (elt->op == OP_NONE) {
1614 operand1 = elt->operand;
1616 operand2 = elt->operand;
1618 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1625 if (WARN_ON(n_preds++ == MAX_FILTER_PRED)) {
1626 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1631 pred = create_pred(ps, call, elt->op, operand1, operand2);
1638 err = filter_add_pred(ps, filter, pred, &stack);
1643 operand1 = operand2 = NULL;
1647 /* We should have one item left on the stack */
1648 pred = __pop_pred_stack(&stack);
1651 /* This item is where we start from in matching */
1653 /* Make sure the stack is empty */
1654 pred = __pop_pred_stack(&stack);
1655 if (WARN_ON(pred)) {
1657 filter->root = NULL;
1660 err = check_pred_tree(filter, root);
1664 /* Optimize the tree */
1665 err = fold_pred_tree(filter, root);
1669 /* We don't set root until we know it works */
1671 filter->root = root;
1676 __free_pred_stack(&stack);
1680 static inline void event_set_filtered_flag(struct ftrace_event_file *file)
1682 struct ftrace_event_call *call = file->event_call;
1684 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1685 call->flags |= TRACE_EVENT_FL_FILTERED;
1687 file->flags |= FTRACE_EVENT_FL_FILTERED;
1690 static inline void event_set_filter(struct ftrace_event_file *file,
1691 struct event_filter *filter)
1693 struct ftrace_event_call *call = file->event_call;
1695 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1696 rcu_assign_pointer(call->filter, filter);
1698 rcu_assign_pointer(file->filter, filter);
1701 static inline void event_clear_filter(struct ftrace_event_file *file)
1703 struct ftrace_event_call *call = file->event_call;
1705 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1706 RCU_INIT_POINTER(call->filter, NULL);
1708 RCU_INIT_POINTER(file->filter, NULL);
1712 event_set_no_set_filter_flag(struct ftrace_event_file *file)
1714 struct ftrace_event_call *call = file->event_call;
1716 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1717 call->flags |= TRACE_EVENT_FL_NO_SET_FILTER;
1719 file->flags |= FTRACE_EVENT_FL_NO_SET_FILTER;
1723 event_clear_no_set_filter_flag(struct ftrace_event_file *file)
1725 struct ftrace_event_call *call = file->event_call;
1727 if (call->flags & TRACE_EVENT_FL_USE_CALL_FILTER)
1728 call->flags &= ~TRACE_EVENT_FL_NO_SET_FILTER;
1730 file->flags &= ~FTRACE_EVENT_FL_NO_SET_FILTER;
1734 event_no_set_filter_flag(struct ftrace_event_file *file)
1736 struct ftrace_event_call *call = file->event_call;
1738 if (file->flags & FTRACE_EVENT_FL_NO_SET_FILTER)
1741 if ((call->flags & TRACE_EVENT_FL_USE_CALL_FILTER) &&
1742 (call->flags & TRACE_EVENT_FL_NO_SET_FILTER))
1748 struct filter_list {
1749 struct list_head list;
1750 struct event_filter *filter;
1753 static int replace_system_preds(struct event_subsystem *system,
1754 struct trace_array *tr,
1755 struct filter_parse_state *ps,
1756 char *filter_string)
1758 struct ftrace_event_file *file;
1759 struct ftrace_event_call *call;
1760 struct filter_list *filter_item;
1761 struct filter_list *tmp;
1762 LIST_HEAD(filter_list);
1766 list_for_each_entry(file, &tr->events, list) {
1767 call = file->event_call;
1768 if (strcmp(call->class->system, system->name) != 0)
1772 * Try to see if the filter can be applied
1773 * (filter arg is ignored on dry_run)
1775 err = replace_preds(call, NULL, ps, filter_string, true);
1777 event_set_no_set_filter_flag(file);
1779 event_clear_no_set_filter_flag(file);
1782 list_for_each_entry(file, &tr->events, list) {
1783 struct event_filter *filter;
1785 call = file->event_call;
1787 if (strcmp(call->class->system, system->name) != 0)
1790 if (event_no_set_filter_flag(file))
1793 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1797 list_add_tail(&filter_item->list, &filter_list);
1799 filter_item->filter = __alloc_filter();
1800 if (!filter_item->filter)
1802 filter = filter_item->filter;
1804 /* Can only fail on no memory */
1805 err = replace_filter_string(filter, filter_string);
1809 err = replace_preds(call, filter, ps, filter_string, false);
1811 filter_disable(file);
1812 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1813 append_filter_err(ps, filter);
1815 event_set_filtered_flag(file);
1817 * Regardless of if this returned an error, we still
1818 * replace the filter for the call.
1820 filter = event_filter(file);
1821 event_set_filter(file, filter_item->filter);
1822 filter_item->filter = filter;
1831 * The calls can still be using the old filters.
1832 * Do a synchronize_sched() to ensure all calls are
1833 * done with them before we free them.
1835 synchronize_sched();
1836 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1837 __free_filter(filter_item->filter);
1838 list_del(&filter_item->list);
1843 /* No call succeeded */
1844 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1845 list_del(&filter_item->list);
1848 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1851 /* If any call succeeded, we still need to sync */
1853 synchronize_sched();
1854 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1855 __free_filter(filter_item->filter);
1856 list_del(&filter_item->list);
1862 static int create_filter_start(char *filter_str, bool set_str,
1863 struct filter_parse_state **psp,
1864 struct event_filter **filterp)
1866 struct event_filter *filter;
1867 struct filter_parse_state *ps = NULL;
1870 WARN_ON_ONCE(*psp || *filterp);
1872 /* allocate everything, and if any fails, free all and fail */
1873 filter = __alloc_filter();
1874 if (filter && set_str)
1875 err = replace_filter_string(filter, filter_str);
1877 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1879 if (!filter || !ps || err) {
1881 __free_filter(filter);
1885 /* we're committed to creating a new filter */
1889 parse_init(ps, filter_ops, filter_str);
1890 err = filter_parse(ps);
1892 append_filter_err(ps, filter);
1896 static void create_filter_finish(struct filter_parse_state *ps)
1899 filter_opstack_clear(ps);
1906 * create_filter - create a filter for a ftrace_event_call
1907 * @call: ftrace_event_call to create a filter for
1908 * @filter_str: filter string
1909 * @set_str: remember @filter_str and enable detailed error in filter
1910 * @filterp: out param for created filter (always updated on return)
1912 * Creates a filter for @call with @filter_str. If @set_str is %true,
1913 * @filter_str is copied and recorded in the new filter.
1915 * On success, returns 0 and *@filterp points to the new filter. On
1916 * failure, returns -errno and *@filterp may point to %NULL or to a new
1917 * filter. In the latter case, the returned filter contains error
1918 * information if @set_str is %true and the caller is responsible for
1921 static int create_filter(struct ftrace_event_call *call,
1922 char *filter_str, bool set_str,
1923 struct event_filter **filterp)
1925 struct event_filter *filter = NULL;
1926 struct filter_parse_state *ps = NULL;
1929 err = create_filter_start(filter_str, set_str, &ps, &filter);
1931 err = replace_preds(call, filter, ps, filter_str, false);
1933 append_filter_err(ps, filter);
1935 create_filter_finish(ps);
1942 * create_system_filter - create a filter for an event_subsystem
1943 * @system: event_subsystem to create a filter for
1944 * @filter_str: filter string
1945 * @filterp: out param for created filter (always updated on return)
1947 * Identical to create_filter() except that it creates a subsystem filter
1948 * and always remembers @filter_str.
1950 static int create_system_filter(struct event_subsystem *system,
1951 struct trace_array *tr,
1952 char *filter_str, struct event_filter **filterp)
1954 struct event_filter *filter = NULL;
1955 struct filter_parse_state *ps = NULL;
1958 err = create_filter_start(filter_str, true, &ps, &filter);
1960 err = replace_system_preds(system, tr, ps, filter_str);
1962 /* System filters just show a default message */
1963 kfree(filter->filter_string);
1964 filter->filter_string = NULL;
1966 append_filter_err(ps, filter);
1969 create_filter_finish(ps);
1975 /* caller must hold event_mutex */
1976 int apply_event_filter(struct ftrace_event_file *file, char *filter_string)
1978 struct ftrace_event_call *call = file->event_call;
1979 struct event_filter *filter;
1982 if (!strcmp(strstrip(filter_string), "0")) {
1983 filter_disable(file);
1984 filter = event_filter(file);
1989 event_clear_filter(file);
1991 /* Make sure the filter is not being used */
1992 synchronize_sched();
1993 __free_filter(filter);
1998 err = create_filter(call, filter_string, true, &filter);
2001 * Always swap the call filter with the new filter
2002 * even if there was an error. If there was an error
2003 * in the filter, we disable the filter and show the error
2007 struct event_filter *tmp;
2009 tmp = event_filter(file);
2011 event_set_filtered_flag(file);
2013 filter_disable(file);
2015 event_set_filter(file, filter);
2018 /* Make sure the call is done with the filter */
2019 synchronize_sched();
2027 int apply_subsystem_event_filter(struct ftrace_subsystem_dir *dir,
2028 char *filter_string)
2030 struct event_subsystem *system = dir->subsystem;
2031 struct trace_array *tr = dir->tr;
2032 struct event_filter *filter;
2035 mutex_lock(&event_mutex);
2037 /* Make sure the system still has events */
2038 if (!dir->nr_events) {
2043 if (!strcmp(strstrip(filter_string), "0")) {
2044 filter_free_subsystem_preds(system, tr);
2045 remove_filter_string(system->filter);
2046 filter = system->filter;
2047 system->filter = NULL;
2048 /* Ensure all filters are no longer used */
2049 synchronize_sched();
2050 filter_free_subsystem_filters(system, tr);
2051 __free_filter(filter);
2055 err = create_system_filter(system, tr, filter_string, &filter);
2058 * No event actually uses the system filter
2059 * we can free it without synchronize_sched().
2061 __free_filter(system->filter);
2062 system->filter = filter;
2065 mutex_unlock(&event_mutex);
2070 #ifdef CONFIG_PERF_EVENTS
2072 void ftrace_profile_free_filter(struct perf_event *event)
2074 struct event_filter *filter = event->filter;
2076 event->filter = NULL;
2077 __free_filter(filter);
2080 struct function_filter_data {
2081 struct ftrace_ops *ops;
2086 #ifdef CONFIG_FUNCTION_TRACER
2088 ftrace_function_filter_re(char *buf, int len, int *count)
2090 char *str, *sep, **re;
2092 str = kstrndup(buf, len, GFP_KERNEL);
2097 * The argv_split function takes white space
2098 * as a separator, so convert ',' into spaces.
2100 while ((sep = strchr(str, ',')))
2103 re = argv_split(GFP_KERNEL, str, count);
2108 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2109 int reset, char *re, int len)
2114 ret = ftrace_set_filter(ops, re, len, reset);
2116 ret = ftrace_set_notrace(ops, re, len, reset);
2121 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2122 struct function_filter_data *data)
2124 int i, re_cnt, ret = -EINVAL;
2128 reset = filter ? &data->first_filter : &data->first_notrace;
2131 * The 'ip' field could have multiple filters set, separated
2132 * either by space or comma. We first cut the filter and apply
2133 * all pieces separatelly.
2135 re = ftrace_function_filter_re(buf, len, &re_cnt);
2139 for (i = 0; i < re_cnt; i++) {
2140 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2141 re[i], strlen(re[i]));
2153 static int ftrace_function_check_pred(struct filter_pred *pred, int leaf)
2155 struct ftrace_event_field *field = pred->field;
2159 * Check the leaf predicate for function trace, verify:
2160 * - only '==' and '!=' is used
2161 * - the 'ip' field is used
2163 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2166 if (strcmp(field->name, "ip"))
2170 * Check the non leaf predicate for function trace, verify:
2171 * - only '||' is used
2173 if (pred->op != OP_OR)
2180 static int ftrace_function_set_filter_cb(enum move_type move,
2181 struct filter_pred *pred,
2182 int *err, void *data)
2184 /* Checking the node is valid for function trace. */
2185 if ((move != MOVE_DOWN) ||
2186 (pred->left != FILTER_PRED_INVALID)) {
2187 *err = ftrace_function_check_pred(pred, 0);
2189 *err = ftrace_function_check_pred(pred, 1);
2191 return WALK_PRED_ABORT;
2193 *err = __ftrace_function_set_filter(pred->op == OP_EQ,
2194 pred->regex.pattern,
2199 return (*err) ? WALK_PRED_ABORT : WALK_PRED_DEFAULT;
2202 static int ftrace_function_set_filter(struct perf_event *event,
2203 struct event_filter *filter)
2205 struct function_filter_data data = {
2208 .ops = &event->ftrace_ops,
2211 return walk_pred_tree(filter->preds, filter->root,
2212 ftrace_function_set_filter_cb, &data);
2215 static int ftrace_function_set_filter(struct perf_event *event,
2216 struct event_filter *filter)
2220 #endif /* CONFIG_FUNCTION_TRACER */
2222 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2226 struct event_filter *filter;
2227 struct ftrace_event_call *call;
2229 mutex_lock(&event_mutex);
2231 call = event->tp_event;
2241 err = create_filter(call, filter_str, false, &filter);
2245 if (ftrace_event_is_function(call))
2246 err = ftrace_function_set_filter(event, filter);
2248 event->filter = filter;
2251 if (err || ftrace_event_is_function(call))
2252 __free_filter(filter);
2255 mutex_unlock(&event_mutex);
2260 #endif /* CONFIG_PERF_EVENTS */
2262 #ifdef CONFIG_FTRACE_STARTUP_TEST
2264 #include <linux/types.h>
2265 #include <linux/tracepoint.h>
2267 #define CREATE_TRACE_POINTS
2268 #include "trace_events_filter_test.h"
2270 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2273 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2274 .e = ve, .f = vf, .g = vg, .h = vh }, \
2276 .not_visited = nvisit, \
2281 static struct test_filter_data_t {
2283 struct ftrace_raw_ftrace_test_filter rec;
2286 } test_filter_data[] = {
2287 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2288 "e == 1 && f == 1 && g == 1 && h == 1"
2289 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2290 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2291 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2293 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2294 "e == 1 || f == 1 || g == 1 || h == 1"
2295 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2296 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2297 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2299 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2300 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2301 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2302 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2303 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2304 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2306 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2307 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2308 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2309 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2310 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2312 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2313 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2314 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2315 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2316 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2318 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2319 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2320 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2321 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2322 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2324 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2325 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2326 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2327 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2328 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2330 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2331 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2332 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2333 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2334 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2342 #define DATA_CNT (sizeof(test_filter_data)/sizeof(struct test_filter_data_t))
2344 static int test_pred_visited;
2346 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2348 struct ftrace_event_field *field = pred->field;
2350 test_pred_visited = 1;
2351 printk(KERN_INFO "\npred visited %s\n", field->name);
2355 static int test_walk_pred_cb(enum move_type move, struct filter_pred *pred,
2356 int *err, void *data)
2358 char *fields = data;
2360 if ((move == MOVE_DOWN) &&
2361 (pred->left == FILTER_PRED_INVALID)) {
2362 struct ftrace_event_field *field = pred->field;
2365 WARN(1, "all leafs should have field defined");
2366 return WALK_PRED_DEFAULT;
2368 if (!strchr(fields, *field->name))
2369 return WALK_PRED_DEFAULT;
2372 pred->fn = test_pred_visited_fn;
2374 return WALK_PRED_DEFAULT;
2377 static __init int ftrace_test_event_filter(void)
2381 printk(KERN_INFO "Testing ftrace filter: ");
2383 for (i = 0; i < DATA_CNT; i++) {
2384 struct event_filter *filter = NULL;
2385 struct test_filter_data_t *d = &test_filter_data[i];
2388 err = create_filter(&event_ftrace_test_filter, d->filter,
2392 "Failed to get filter for '%s', err %d\n",
2394 __free_filter(filter);
2399 * The preemption disabling is not really needed for self
2400 * tests, but the rcu dereference will complain without it.
2403 if (*d->not_visited)
2404 walk_pred_tree(filter->preds, filter->root,
2408 test_pred_visited = 0;
2409 err = filter_match_preds(filter, &d->rec);
2412 __free_filter(filter);
2414 if (test_pred_visited) {
2416 "Failed, unwanted pred visited for filter %s\n",
2421 if (err != d->match) {
2423 "Failed to match filter '%s', expected %d\n",
2424 d->filter, d->match);
2430 printk(KERN_CONT "OK\n");
2435 late_initcall(ftrace_test_event_filter);
2437 #endif /* CONFIG_FTRACE_STARTUP_TEST */