2 * trace_events_filter - generic event filtering
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
21 #include <linux/debugfs.h>
22 #include <linux/uaccess.h>
23 #include <linux/module.h>
24 #include <linux/ctype.h>
25 #include <linux/mutex.h>
28 #include "trace_output.h"
30 static DEFINE_MUTEX(filter_mutex);
52 static struct filter_op filter_ops[] = {
61 { OP_NONE, "OP_NONE", 0 },
62 { OP_OPEN_PAREN, "(", 0 },
68 FILT_ERR_UNBALANCED_PAREN,
69 FILT_ERR_TOO_MANY_OPERANDS,
70 FILT_ERR_OPERAND_TOO_LONG,
71 FILT_ERR_FIELD_NOT_FOUND,
72 FILT_ERR_ILLEGAL_FIELD_OP,
73 FILT_ERR_ILLEGAL_INTVAL,
74 FILT_ERR_BAD_SUBSYS_FILTER,
75 FILT_ERR_TOO_MANY_PREDS,
76 FILT_ERR_MISSING_FIELD,
77 FILT_ERR_INVALID_FILTER,
80 static char *err_text[] = {
87 "Illegal operation for field type",
88 "Illegal integer value",
89 "Couldn't find or set field in one of a subsystem's events",
90 "Too many terms in predicate expression",
91 "Missing field name and/or value",
92 "Meaningless filter expression",
97 struct list_head list;
103 struct list_head list;
106 struct filter_parse_state {
107 struct filter_op *ops;
108 struct list_head opstack;
109 struct list_head postfix;
120 char string[MAX_FILTER_STR_VAL];
126 DEFINE_COMPARISON_PRED(s64);
127 DEFINE_COMPARISON_PRED(u64);
128 DEFINE_COMPARISON_PRED(s32);
129 DEFINE_COMPARISON_PRED(u32);
130 DEFINE_COMPARISON_PRED(s16);
131 DEFINE_COMPARISON_PRED(u16);
132 DEFINE_COMPARISON_PRED(s8);
133 DEFINE_COMPARISON_PRED(u8);
135 DEFINE_EQUALITY_PRED(64);
136 DEFINE_EQUALITY_PRED(32);
137 DEFINE_EQUALITY_PRED(16);
138 DEFINE_EQUALITY_PRED(8);
140 static int filter_pred_and(struct filter_pred *pred __attribute((unused)),
141 void *event __attribute((unused)),
147 static int filter_pred_or(struct filter_pred *pred __attribute((unused)),
148 void *event __attribute((unused)),
154 /* Filter predicate for fixed sized arrays of characters */
155 static int filter_pred_string(struct filter_pred *pred, void *event,
158 char *addr = (char *)(event + pred->offset);
161 cmp = strncmp(addr, pred->str_val, pred->str_len);
163 match = (!cmp) ^ pred->not;
169 * Filter predicate for dynamic sized arrays of characters.
170 * These are implemented through a list of strings at the end
172 * Also each of these strings have a field in the entry which
173 * contains its offset from the beginning of the entry.
174 * We have then first to get this field, dereference it
175 * and add it to the address of the entry, and at last we have
176 * the address of the string.
178 static int filter_pred_strloc(struct filter_pred *pred, void *event,
181 unsigned short str_loc = *(unsigned short *)(event + pred->offset);
182 char *addr = (char *)(event + str_loc);
185 cmp = strncmp(addr, pred->str_val, pred->str_len);
187 match = (!cmp) ^ pred->not;
192 static int filter_pred_none(struct filter_pred *pred, void *event,
198 /* return 1 if event matches, 0 otherwise (discard) */
199 int filter_match_preds(struct ftrace_event_call *call, void *rec)
201 struct event_filter *filter = call->filter;
202 int match, top = 0, val1 = 0, val2 = 0;
203 int stack[MAX_FILTER_PRED];
204 struct filter_pred *pred;
207 for (i = 0; i < filter->n_preds; i++) {
208 pred = filter->preds[i];
210 match = pred->fn(pred, rec, val1, val2);
211 stack[top++] = match;
214 if (pred->pop_n > top) {
220 match = pred->fn(pred, rec, val1, val2);
221 stack[top++] = match;
226 EXPORT_SYMBOL_GPL(filter_match_preds);
228 static void parse_error(struct filter_parse_state *ps, int err, int pos)
231 ps->lasterr_pos = pos;
234 static void remove_filter_string(struct event_filter *filter)
236 kfree(filter->filter_string);
237 filter->filter_string = NULL;
240 static int replace_filter_string(struct event_filter *filter,
243 kfree(filter->filter_string);
244 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
245 if (!filter->filter_string)
251 static int append_filter_string(struct event_filter *filter,
255 char *new_filter_string;
257 BUG_ON(!filter->filter_string);
258 newlen = strlen(filter->filter_string) + strlen(string) + 1;
259 new_filter_string = kmalloc(newlen, GFP_KERNEL);
260 if (!new_filter_string)
263 strcpy(new_filter_string, filter->filter_string);
264 strcat(new_filter_string, string);
265 kfree(filter->filter_string);
266 filter->filter_string = new_filter_string;
271 static void append_filter_err(struct filter_parse_state *ps,
272 struct event_filter *filter)
274 int pos = ps->lasterr_pos;
277 buf = (char *)__get_free_page(GFP_TEMPORARY);
281 append_filter_string(filter, "\n");
282 memset(buf, ' ', PAGE_SIZE);
283 if (pos > PAGE_SIZE - 128)
286 pbuf = &buf[pos] + 1;
288 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
289 append_filter_string(filter, buf);
290 free_page((unsigned long) buf);
293 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
295 struct event_filter *filter = call->filter;
297 mutex_lock(&filter_mutex);
298 if (filter->filter_string)
299 trace_seq_printf(s, "%s\n", filter->filter_string);
301 trace_seq_printf(s, "none\n");
302 mutex_unlock(&filter_mutex);
305 void print_subsystem_event_filter(struct event_subsystem *system,
308 struct event_filter *filter = system->filter;
310 mutex_lock(&filter_mutex);
311 if (filter->filter_string)
312 trace_seq_printf(s, "%s\n", filter->filter_string);
314 trace_seq_printf(s, "none\n");
315 mutex_unlock(&filter_mutex);
318 static struct ftrace_event_field *
319 find_event_field(struct ftrace_event_call *call, char *name)
321 struct ftrace_event_field *field;
323 list_for_each_entry(field, &call->fields, link) {
324 if (!strcmp(field->name, name))
331 static void filter_free_pred(struct filter_pred *pred)
336 kfree(pred->field_name);
340 static void filter_clear_pred(struct filter_pred *pred)
342 kfree(pred->field_name);
343 pred->field_name = NULL;
347 static int filter_set_pred(struct filter_pred *dest,
348 struct filter_pred *src,
352 if (src->field_name) {
353 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
354 if (!dest->field_name)
362 static void filter_disable_preds(struct ftrace_event_call *call)
364 struct event_filter *filter = call->filter;
367 call->filter_active = 0;
370 for (i = 0; i < MAX_FILTER_PRED; i++)
371 filter->preds[i]->fn = filter_pred_none;
374 void destroy_preds(struct ftrace_event_call *call)
376 struct event_filter *filter = call->filter;
379 for (i = 0; i < MAX_FILTER_PRED; i++) {
380 if (filter->preds[i])
381 filter_free_pred(filter->preds[i]);
383 kfree(filter->preds);
384 kfree(filter->filter_string);
389 int init_preds(struct ftrace_event_call *call)
391 struct event_filter *filter;
392 struct filter_pred *pred;
395 filter = call->filter = kzalloc(sizeof(*filter), GFP_KERNEL);
399 call->filter_active = 0;
402 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
406 for (i = 0; i < MAX_FILTER_PRED; i++) {
407 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
410 pred->fn = filter_pred_none;
411 filter->preds[i] = pred;
421 EXPORT_SYMBOL_GPL(init_preds);
423 static void filter_free_subsystem_preds(struct event_subsystem *system)
425 struct event_filter *filter = system->filter;
426 struct ftrace_event_call *call;
429 if (filter->n_preds) {
430 for (i = 0; i < filter->n_preds; i++)
431 filter_free_pred(filter->preds[i]);
432 kfree(filter->preds);
433 filter->preds = NULL;
437 mutex_lock(&event_mutex);
438 list_for_each_entry(call, &ftrace_events, list) {
439 if (!call->define_fields)
442 if (!strcmp(call->system, system->name)) {
443 filter_disable_preds(call);
444 remove_filter_string(call->filter);
447 mutex_unlock(&event_mutex);
450 static int filter_add_pred_fn(struct filter_parse_state *ps,
451 struct ftrace_event_call *call,
452 struct filter_pred *pred,
455 struct event_filter *filter = call->filter;
458 if (filter->n_preds == MAX_FILTER_PRED) {
459 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
463 idx = filter->n_preds;
464 filter_clear_pred(filter->preds[idx]);
465 err = filter_set_pred(filter->preds[idx], pred, fn);
470 call->filter_active = 1;
476 FILTER_STATIC_STRING = 1,
480 static int is_string_field(const char *type)
482 if (strstr(type, "__data_loc") && strstr(type, "char"))
483 return FILTER_DYN_STRING;
485 if (strchr(type, '[') && strstr(type, "char"))
486 return FILTER_STATIC_STRING;
491 static int is_legal_op(struct ftrace_event_field *field, int op)
493 if (is_string_field(field->type) && (op != OP_EQ && op != OP_NE))
499 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
502 filter_pred_fn_t fn = NULL;
504 switch (field_size) {
506 if (op == OP_EQ || op == OP_NE)
508 else if (field_is_signed)
509 fn = filter_pred_s64;
511 fn = filter_pred_u64;
514 if (op == OP_EQ || op == OP_NE)
516 else if (field_is_signed)
517 fn = filter_pred_s32;
519 fn = filter_pred_u32;
522 if (op == OP_EQ || op == OP_NE)
524 else if (field_is_signed)
525 fn = filter_pred_s16;
527 fn = filter_pred_u16;
530 if (op == OP_EQ || op == OP_NE)
532 else if (field_is_signed)
542 static int filter_add_pred(struct filter_parse_state *ps,
543 struct ftrace_event_call *call,
544 struct filter_pred *pred)
546 struct ftrace_event_field *field;
548 unsigned long long val;
552 pred->fn = filter_pred_none;
554 if (pred->op == OP_AND) {
556 return filter_add_pred_fn(ps, call, pred, filter_pred_and);
557 } else if (pred->op == OP_OR) {
559 return filter_add_pred_fn(ps, call, pred, filter_pred_or);
562 field = find_event_field(call, pred->field_name);
564 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
568 pred->offset = field->offset;
570 if (!is_legal_op(field, pred->op)) {
571 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
575 string_type = is_string_field(field->type);
577 if (string_type == FILTER_STATIC_STRING)
578 fn = filter_pred_string;
580 fn = filter_pred_strloc;
581 pred->str_len = field->size;
582 if (pred->op == OP_NE)
584 return filter_add_pred_fn(ps, call, pred, fn);
586 if (field->is_signed)
587 ret = strict_strtoll(pred->str_val, 0, &val);
589 ret = strict_strtoull(pred->str_val, 0, &val);
591 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
597 fn = select_comparison_fn(pred->op, field->size, field->is_signed);
599 parse_error(ps, FILT_ERR_INVALID_OP, 0);
603 if (pred->op == OP_NE)
606 return filter_add_pred_fn(ps, call, pred, fn);
609 static int filter_add_subsystem_pred(struct filter_parse_state *ps,
610 struct event_subsystem *system,
611 struct filter_pred *pred,
614 struct event_filter *filter = system->filter;
615 struct ftrace_event_call *call;
618 if (!filter->preds) {
619 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred),
626 if (filter->n_preds == MAX_FILTER_PRED) {
627 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
631 filter->preds[filter->n_preds] = pred;
634 mutex_lock(&event_mutex);
635 list_for_each_entry(call, &ftrace_events, list) {
637 if (!call->define_fields)
640 if (strcmp(call->system, system->name))
643 err = filter_add_pred(ps, call, pred);
645 mutex_unlock(&event_mutex);
646 filter_free_subsystem_preds(system);
647 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
650 replace_filter_string(call->filter, filter_string);
652 mutex_unlock(&event_mutex);
657 static void parse_init(struct filter_parse_state *ps,
658 struct filter_op *ops,
661 memset(ps, '\0', sizeof(*ps));
663 ps->infix.string = infix_string;
664 ps->infix.cnt = strlen(infix_string);
667 INIT_LIST_HEAD(&ps->opstack);
668 INIT_LIST_HEAD(&ps->postfix);
671 static char infix_next(struct filter_parse_state *ps)
675 return ps->infix.string[ps->infix.tail++];
678 static char infix_peek(struct filter_parse_state *ps)
680 if (ps->infix.tail == strlen(ps->infix.string))
683 return ps->infix.string[ps->infix.tail];
686 static void infix_advance(struct filter_parse_state *ps)
692 static inline int is_precedence_lower(struct filter_parse_state *ps,
695 return ps->ops[a].precedence < ps->ops[b].precedence;
698 static inline int is_op_char(struct filter_parse_state *ps, char c)
702 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
703 if (ps->ops[i].string[0] == c)
710 static int infix_get_op(struct filter_parse_state *ps, char firstc)
712 char nextc = infix_peek(ps);
720 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
721 if (!strcmp(opstr, ps->ops[i].string)) {
723 return ps->ops[i].id;
729 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
730 if (!strcmp(opstr, ps->ops[i].string))
731 return ps->ops[i].id;
737 static inline void clear_operand_string(struct filter_parse_state *ps)
739 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
740 ps->operand.tail = 0;
743 static inline int append_operand_char(struct filter_parse_state *ps, char c)
745 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
748 ps->operand.string[ps->operand.tail++] = c;
753 static int filter_opstack_push(struct filter_parse_state *ps, int op)
755 struct opstack_op *opstack_op;
757 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
762 list_add(&opstack_op->list, &ps->opstack);
767 static int filter_opstack_empty(struct filter_parse_state *ps)
769 return list_empty(&ps->opstack);
772 static int filter_opstack_top(struct filter_parse_state *ps)
774 struct opstack_op *opstack_op;
776 if (filter_opstack_empty(ps))
779 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
781 return opstack_op->op;
784 static int filter_opstack_pop(struct filter_parse_state *ps)
786 struct opstack_op *opstack_op;
789 if (filter_opstack_empty(ps))
792 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
794 list_del(&opstack_op->list);
801 static void filter_opstack_clear(struct filter_parse_state *ps)
803 while (!filter_opstack_empty(ps))
804 filter_opstack_pop(ps);
807 static char *curr_operand(struct filter_parse_state *ps)
809 return ps->operand.string;
812 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
814 struct postfix_elt *elt;
816 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
821 elt->operand = kstrdup(operand, GFP_KERNEL);
827 list_add_tail(&elt->list, &ps->postfix);
832 static int postfix_append_op(struct filter_parse_state *ps, int op)
834 struct postfix_elt *elt;
836 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
843 list_add_tail(&elt->list, &ps->postfix);
848 static void postfix_clear(struct filter_parse_state *ps)
850 struct postfix_elt *elt;
852 while (!list_empty(&ps->postfix)) {
853 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
855 list_del(&elt->list);
859 static int filter_parse(struct filter_parse_state *ps)
865 while ((ch = infix_next(ps))) {
877 if (is_op_char(ps, ch)) {
878 op = infix_get_op(ps, ch);
880 parse_error(ps, FILT_ERR_INVALID_OP, 0);
884 if (strlen(curr_operand(ps))) {
885 postfix_append_operand(ps, curr_operand(ps));
886 clear_operand_string(ps);
889 while (!filter_opstack_empty(ps)) {
890 top_op = filter_opstack_top(ps);
891 if (!is_precedence_lower(ps, top_op, op)) {
892 top_op = filter_opstack_pop(ps);
893 postfix_append_op(ps, top_op);
899 filter_opstack_push(ps, op);
904 filter_opstack_push(ps, OP_OPEN_PAREN);
909 if (strlen(curr_operand(ps))) {
910 postfix_append_operand(ps, curr_operand(ps));
911 clear_operand_string(ps);
914 top_op = filter_opstack_pop(ps);
915 while (top_op != OP_NONE) {
916 if (top_op == OP_OPEN_PAREN)
918 postfix_append_op(ps, top_op);
919 top_op = filter_opstack_pop(ps);
921 if (top_op == OP_NONE) {
922 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
928 if (append_operand_char(ps, ch)) {
929 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
934 if (strlen(curr_operand(ps)))
935 postfix_append_operand(ps, curr_operand(ps));
937 while (!filter_opstack_empty(ps)) {
938 top_op = filter_opstack_pop(ps);
939 if (top_op == OP_NONE)
941 if (top_op == OP_OPEN_PAREN) {
942 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
945 postfix_append_op(ps, top_op);
951 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
953 struct filter_pred *pred;
955 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
959 pred->field_name = kstrdup(operand1, GFP_KERNEL);
960 if (!pred->field_name) {
965 strcpy(pred->str_val, operand2);
966 pred->str_len = strlen(operand2);
973 static struct filter_pred *create_logical_pred(int op)
975 struct filter_pred *pred;
977 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
986 static int check_preds(struct filter_parse_state *ps)
988 int n_normal_preds = 0, n_logical_preds = 0;
989 struct postfix_elt *elt;
991 list_for_each_entry(elt, &ps->postfix, list) {
992 if (elt->op == OP_NONE)
995 if (elt->op == OP_AND || elt->op == OP_OR) {
1002 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1003 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1010 static int replace_preds(struct event_subsystem *system,
1011 struct ftrace_event_call *call,
1012 struct filter_parse_state *ps,
1013 char *filter_string)
1015 char *operand1 = NULL, *operand2 = NULL;
1016 struct filter_pred *pred;
1017 struct postfix_elt *elt;
1020 err = check_preds(ps);
1024 list_for_each_entry(elt, &ps->postfix, list) {
1025 if (elt->op == OP_NONE) {
1027 operand1 = elt->operand;
1029 operand2 = elt->operand;
1031 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1037 if (elt->op == OP_AND || elt->op == OP_OR) {
1038 pred = create_logical_pred(elt->op);
1040 err = filter_add_pred(ps, call, pred);
1041 filter_free_pred(pred);
1043 err = filter_add_subsystem_pred(ps, system,
1044 pred, filter_string);
1048 operand1 = operand2 = NULL;
1052 if (!operand1 || !operand2) {
1053 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1057 pred = create_pred(elt->op, operand1, operand2);
1059 err = filter_add_pred(ps, call, pred);
1060 filter_free_pred(pred);
1062 err = filter_add_subsystem_pred(ps, system, pred,
1067 operand1 = operand2 = NULL;
1073 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1077 struct filter_parse_state *ps;
1079 mutex_lock(&filter_mutex);
1081 if (!strcmp(strstrip(filter_string), "0")) {
1082 filter_disable_preds(call);
1083 remove_filter_string(call->filter);
1084 mutex_unlock(&filter_mutex);
1089 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1093 filter_disable_preds(call);
1094 replace_filter_string(call->filter, filter_string);
1096 parse_init(ps, filter_ops, filter_string);
1097 err = filter_parse(ps);
1099 append_filter_err(ps, call->filter);
1103 err = replace_preds(NULL, call, ps, filter_string);
1105 append_filter_err(ps, call->filter);
1108 filter_opstack_clear(ps);
1112 mutex_unlock(&filter_mutex);
1117 int apply_subsystem_event_filter(struct event_subsystem *system,
1118 char *filter_string)
1122 struct filter_parse_state *ps;
1124 mutex_lock(&filter_mutex);
1126 if (!strcmp(strstrip(filter_string), "0")) {
1127 filter_free_subsystem_preds(system);
1128 remove_filter_string(system->filter);
1129 mutex_unlock(&filter_mutex);
1134 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1138 filter_free_subsystem_preds(system);
1139 replace_filter_string(system->filter, filter_string);
1141 parse_init(ps, filter_ops, filter_string);
1142 err = filter_parse(ps);
1144 append_filter_err(ps, system->filter);
1148 err = replace_preds(system, NULL, ps, filter_string);
1150 append_filter_err(ps, system->filter);
1153 filter_opstack_clear(ps);
1157 mutex_unlock(&filter_mutex);