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"
50 static struct filter_op filter_ops[] = {
59 { OP_NONE, "OP_NONE", 0 },
60 { OP_OPEN_PAREN, "(", 0 },
66 FILT_ERR_UNBALANCED_PAREN,
67 FILT_ERR_TOO_MANY_OPERANDS,
68 FILT_ERR_OPERAND_TOO_LONG,
69 FILT_ERR_FIELD_NOT_FOUND,
70 FILT_ERR_ILLEGAL_FIELD_OP,
71 FILT_ERR_ILLEGAL_INTVAL,
72 FILT_ERR_BAD_SUBSYS_FILTER,
73 FILT_ERR_TOO_MANY_PREDS,
74 FILT_ERR_MISSING_FIELD,
75 FILT_ERR_INVALID_FILTER,
78 static char *err_text[] = {
85 "Illegal operation for field type",
86 "Illegal integer value",
87 "Couldn't find or set field in one of a subsystem's events",
88 "Too many terms in predicate expression",
89 "Missing field name and/or value",
90 "Meaningless filter expression",
95 struct list_head list;
101 struct list_head list;
104 struct filter_parse_state {
105 struct filter_op *ops;
106 struct list_head opstack;
107 struct list_head postfix;
118 char string[MAX_FILTER_STR_VAL];
124 DEFINE_COMPARISON_PRED(s64);
125 DEFINE_COMPARISON_PRED(u64);
126 DEFINE_COMPARISON_PRED(s32);
127 DEFINE_COMPARISON_PRED(u32);
128 DEFINE_COMPARISON_PRED(s16);
129 DEFINE_COMPARISON_PRED(u16);
130 DEFINE_COMPARISON_PRED(s8);
131 DEFINE_COMPARISON_PRED(u8);
133 DEFINE_EQUALITY_PRED(64);
134 DEFINE_EQUALITY_PRED(32);
135 DEFINE_EQUALITY_PRED(16);
136 DEFINE_EQUALITY_PRED(8);
138 static int filter_pred_and(struct filter_pred *pred __attribute((unused)),
139 void *event __attribute((unused)),
145 static int filter_pred_or(struct filter_pred *pred __attribute((unused)),
146 void *event __attribute((unused)),
152 /* Filter predicate for fixed sized arrays of characters */
153 static int filter_pred_string(struct filter_pred *pred, void *event,
156 char *addr = (char *)(event + pred->offset);
159 cmp = strncmp(addr, pred->str_val, pred->str_len);
161 match = (!cmp) ^ pred->not;
166 /* Filter predicate for char * pointers */
167 static int filter_pred_pchar(struct filter_pred *pred, void *event,
170 char **addr = (char **)(event + pred->offset);
173 cmp = strncmp(*addr, pred->str_val, pred->str_len);
175 match = (!cmp) ^ pred->not;
181 * Filter predicate for dynamic sized arrays of characters.
182 * These are implemented through a list of strings at the end
184 * Also each of these strings have a field in the entry which
185 * contains its offset from the beginning of the entry.
186 * We have then first to get this field, dereference it
187 * and add it to the address of the entry, and at last we have
188 * the address of the string.
190 static int filter_pred_strloc(struct filter_pred *pred, void *event,
193 u32 str_item = *(u32 *)(event + pred->offset);
194 int str_loc = str_item & 0xffff;
195 int str_len = str_item >> 16;
196 char *addr = (char *)(event + str_loc);
199 cmp = strncmp(addr, pred->str_val, str_len);
201 match = (!cmp) ^ pred->not;
206 static int filter_pred_none(struct filter_pred *pred, void *event,
212 /* return 1 if event matches, 0 otherwise (discard) */
213 int filter_match_preds(struct ftrace_event_call *call, void *rec)
215 struct event_filter *filter = call->filter;
216 int match, top = 0, val1 = 0, val2 = 0;
217 int stack[MAX_FILTER_PRED];
218 struct filter_pred *pred;
221 for (i = 0; i < filter->n_preds; i++) {
222 pred = filter->preds[i];
224 match = pred->fn(pred, rec, val1, val2);
225 stack[top++] = match;
228 if (pred->pop_n > top) {
234 match = pred->fn(pred, rec, val1, val2);
235 stack[top++] = match;
240 EXPORT_SYMBOL_GPL(filter_match_preds);
242 static void parse_error(struct filter_parse_state *ps, int err, int pos)
245 ps->lasterr_pos = pos;
248 static void remove_filter_string(struct event_filter *filter)
250 kfree(filter->filter_string);
251 filter->filter_string = NULL;
254 static int replace_filter_string(struct event_filter *filter,
257 kfree(filter->filter_string);
258 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
259 if (!filter->filter_string)
265 static int append_filter_string(struct event_filter *filter,
269 char *new_filter_string;
271 BUG_ON(!filter->filter_string);
272 newlen = strlen(filter->filter_string) + strlen(string) + 1;
273 new_filter_string = kmalloc(newlen, GFP_KERNEL);
274 if (!new_filter_string)
277 strcpy(new_filter_string, filter->filter_string);
278 strcat(new_filter_string, string);
279 kfree(filter->filter_string);
280 filter->filter_string = new_filter_string;
285 static void append_filter_err(struct filter_parse_state *ps,
286 struct event_filter *filter)
288 int pos = ps->lasterr_pos;
291 buf = (char *)__get_free_page(GFP_TEMPORARY);
295 append_filter_string(filter, "\n");
296 memset(buf, ' ', PAGE_SIZE);
297 if (pos > PAGE_SIZE - 128)
300 pbuf = &buf[pos] + 1;
302 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
303 append_filter_string(filter, buf);
304 free_page((unsigned long) buf);
307 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
309 struct event_filter *filter = call->filter;
311 mutex_lock(&event_mutex);
312 if (filter && filter->filter_string)
313 trace_seq_printf(s, "%s\n", filter->filter_string);
315 trace_seq_printf(s, "none\n");
316 mutex_unlock(&event_mutex);
319 void print_subsystem_event_filter(struct event_subsystem *system,
322 struct event_filter *filter = system->filter;
324 mutex_lock(&event_mutex);
325 if (filter && filter->filter_string)
326 trace_seq_printf(s, "%s\n", filter->filter_string);
328 trace_seq_printf(s, "none\n");
329 mutex_unlock(&event_mutex);
332 static struct ftrace_event_field *
333 find_event_field(struct ftrace_event_call *call, char *name)
335 struct ftrace_event_field *field;
337 list_for_each_entry(field, &call->fields, link) {
338 if (!strcmp(field->name, name))
345 static void filter_free_pred(struct filter_pred *pred)
350 kfree(pred->field_name);
354 static void filter_clear_pred(struct filter_pred *pred)
356 kfree(pred->field_name);
357 pred->field_name = NULL;
361 static int filter_set_pred(struct filter_pred *dest,
362 struct filter_pred *src,
366 if (src->field_name) {
367 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
368 if (!dest->field_name)
376 static void filter_disable_preds(struct ftrace_event_call *call)
378 struct event_filter *filter = call->filter;
381 call->filter_active = 0;
384 for (i = 0; i < MAX_FILTER_PRED; i++)
385 filter->preds[i]->fn = filter_pred_none;
388 void destroy_preds(struct ftrace_event_call *call)
390 struct event_filter *filter = call->filter;
396 for (i = 0; i < MAX_FILTER_PRED; i++) {
397 if (filter->preds[i])
398 filter_free_pred(filter->preds[i]);
400 kfree(filter->preds);
401 kfree(filter->filter_string);
406 static int init_preds(struct ftrace_event_call *call)
408 struct event_filter *filter;
409 struct filter_pred *pred;
412 filter = call->filter = kzalloc(sizeof(*filter), GFP_KERNEL);
418 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
422 for (i = 0; i < MAX_FILTER_PRED; i++) {
423 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
426 pred->fn = filter_pred_none;
427 filter->preds[i] = pred;
438 static int init_subsystem_preds(struct event_subsystem *system)
440 struct ftrace_event_call *call;
443 list_for_each_entry(call, &ftrace_events, list) {
444 if (!call->define_fields)
447 if (strcmp(call->system, system->name) != 0)
451 err = init_preds(call);
462 FILTER_INIT_NO_RESET,
463 FILTER_SKIP_NO_RESET,
466 static void filter_free_subsystem_preds(struct event_subsystem *system,
469 struct ftrace_event_call *call;
471 list_for_each_entry(call, &ftrace_events, list) {
472 if (!call->define_fields)
475 if (strcmp(call->system, system->name) != 0)
478 if (flag == FILTER_INIT_NO_RESET) {
479 call->filter->no_reset = false;
483 if (flag == FILTER_SKIP_NO_RESET && call->filter->no_reset)
486 filter_disable_preds(call);
487 remove_filter_string(call->filter);
491 static int filter_add_pred_fn(struct filter_parse_state *ps,
492 struct ftrace_event_call *call,
493 struct filter_pred *pred,
496 struct event_filter *filter = call->filter;
499 if (filter->n_preds == MAX_FILTER_PRED) {
500 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
504 idx = filter->n_preds;
505 filter_clear_pred(filter->preds[idx]);
506 err = filter_set_pred(filter->preds[idx], pred, fn);
511 call->filter_active = 1;
516 int filter_assign_type(const char *type)
518 if (strstr(type, "__data_loc") && strstr(type, "char"))
519 return FILTER_DYN_STRING;
521 if (strchr(type, '[') && strstr(type, "char"))
522 return FILTER_STATIC_STRING;
527 static bool is_string_field(struct ftrace_event_field *field)
529 return field->filter_type == FILTER_DYN_STRING ||
530 field->filter_type == FILTER_STATIC_STRING ||
531 field->filter_type == FILTER_PTR_STRING;
534 static int is_legal_op(struct ftrace_event_field *field, int op)
536 if (is_string_field(field) && (op != OP_EQ && op != OP_NE))
542 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
545 filter_pred_fn_t fn = NULL;
547 switch (field_size) {
549 if (op == OP_EQ || op == OP_NE)
551 else if (field_is_signed)
552 fn = filter_pred_s64;
554 fn = filter_pred_u64;
557 if (op == OP_EQ || op == OP_NE)
559 else if (field_is_signed)
560 fn = filter_pred_s32;
562 fn = filter_pred_u32;
565 if (op == OP_EQ || op == OP_NE)
567 else if (field_is_signed)
568 fn = filter_pred_s16;
570 fn = filter_pred_u16;
573 if (op == OP_EQ || op == OP_NE)
575 else if (field_is_signed)
585 static int filter_add_pred(struct filter_parse_state *ps,
586 struct ftrace_event_call *call,
587 struct filter_pred *pred,
590 struct ftrace_event_field *field;
592 unsigned long long val;
595 pred->fn = filter_pred_none;
597 if (pred->op == OP_AND) {
599 fn = filter_pred_and;
601 } else if (pred->op == OP_OR) {
607 field = find_event_field(call, pred->field_name);
609 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
613 pred->offset = field->offset;
615 if (!is_legal_op(field, pred->op)) {
616 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
620 if (is_string_field(field)) {
621 pred->str_len = field->size;
623 if (field->filter_type == FILTER_STATIC_STRING)
624 fn = filter_pred_string;
625 else if (field->filter_type == FILTER_DYN_STRING)
626 fn = filter_pred_strloc;
628 fn = filter_pred_pchar;
629 pred->str_len = strlen(pred->str_val);
632 if (field->is_signed)
633 ret = strict_strtoll(pred->str_val, 0, &val);
635 ret = strict_strtoull(pred->str_val, 0, &val);
637 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
642 fn = select_comparison_fn(pred->op, field->size,
645 parse_error(ps, FILT_ERR_INVALID_OP, 0);
650 if (pred->op == OP_NE)
655 return filter_add_pred_fn(ps, call, pred, fn);
659 static int filter_add_subsystem_pred(struct filter_parse_state *ps,
660 struct event_subsystem *system,
661 struct filter_pred *pred,
665 struct ftrace_event_call *call;
669 list_for_each_entry(call, &ftrace_events, list) {
671 if (!call->define_fields)
674 if (strcmp(call->system, system->name))
677 if (call->filter->no_reset)
680 err = filter_add_pred(ps, call, pred, dry_run);
682 call->filter->no_reset = true;
687 replace_filter_string(call->filter, filter_string);
691 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
697 static void parse_init(struct filter_parse_state *ps,
698 struct filter_op *ops,
701 memset(ps, '\0', sizeof(*ps));
703 ps->infix.string = infix_string;
704 ps->infix.cnt = strlen(infix_string);
707 INIT_LIST_HEAD(&ps->opstack);
708 INIT_LIST_HEAD(&ps->postfix);
711 static char infix_next(struct filter_parse_state *ps)
715 return ps->infix.string[ps->infix.tail++];
718 static char infix_peek(struct filter_parse_state *ps)
720 if (ps->infix.tail == strlen(ps->infix.string))
723 return ps->infix.string[ps->infix.tail];
726 static void infix_advance(struct filter_parse_state *ps)
732 static inline int is_precedence_lower(struct filter_parse_state *ps,
735 return ps->ops[a].precedence < ps->ops[b].precedence;
738 static inline int is_op_char(struct filter_parse_state *ps, char c)
742 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
743 if (ps->ops[i].string[0] == c)
750 static int infix_get_op(struct filter_parse_state *ps, char firstc)
752 char nextc = infix_peek(ps);
760 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
761 if (!strcmp(opstr, ps->ops[i].string)) {
763 return ps->ops[i].id;
769 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
770 if (!strcmp(opstr, ps->ops[i].string))
771 return ps->ops[i].id;
777 static inline void clear_operand_string(struct filter_parse_state *ps)
779 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
780 ps->operand.tail = 0;
783 static inline int append_operand_char(struct filter_parse_state *ps, char c)
785 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
788 ps->operand.string[ps->operand.tail++] = c;
793 static int filter_opstack_push(struct filter_parse_state *ps, int op)
795 struct opstack_op *opstack_op;
797 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
802 list_add(&opstack_op->list, &ps->opstack);
807 static int filter_opstack_empty(struct filter_parse_state *ps)
809 return list_empty(&ps->opstack);
812 static int filter_opstack_top(struct filter_parse_state *ps)
814 struct opstack_op *opstack_op;
816 if (filter_opstack_empty(ps))
819 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
821 return opstack_op->op;
824 static int filter_opstack_pop(struct filter_parse_state *ps)
826 struct opstack_op *opstack_op;
829 if (filter_opstack_empty(ps))
832 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
834 list_del(&opstack_op->list);
841 static void filter_opstack_clear(struct filter_parse_state *ps)
843 while (!filter_opstack_empty(ps))
844 filter_opstack_pop(ps);
847 static char *curr_operand(struct filter_parse_state *ps)
849 return ps->operand.string;
852 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
854 struct postfix_elt *elt;
856 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
861 elt->operand = kstrdup(operand, GFP_KERNEL);
867 list_add_tail(&elt->list, &ps->postfix);
872 static int postfix_append_op(struct filter_parse_state *ps, int op)
874 struct postfix_elt *elt;
876 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
883 list_add_tail(&elt->list, &ps->postfix);
888 static void postfix_clear(struct filter_parse_state *ps)
890 struct postfix_elt *elt;
892 while (!list_empty(&ps->postfix)) {
893 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
895 list_del(&elt->list);
899 static int filter_parse(struct filter_parse_state *ps)
905 while ((ch = infix_next(ps))) {
917 if (is_op_char(ps, ch)) {
918 op = infix_get_op(ps, ch);
920 parse_error(ps, FILT_ERR_INVALID_OP, 0);
924 if (strlen(curr_operand(ps))) {
925 postfix_append_operand(ps, curr_operand(ps));
926 clear_operand_string(ps);
929 while (!filter_opstack_empty(ps)) {
930 top_op = filter_opstack_top(ps);
931 if (!is_precedence_lower(ps, top_op, op)) {
932 top_op = filter_opstack_pop(ps);
933 postfix_append_op(ps, top_op);
939 filter_opstack_push(ps, op);
944 filter_opstack_push(ps, OP_OPEN_PAREN);
949 if (strlen(curr_operand(ps))) {
950 postfix_append_operand(ps, curr_operand(ps));
951 clear_operand_string(ps);
954 top_op = filter_opstack_pop(ps);
955 while (top_op != OP_NONE) {
956 if (top_op == OP_OPEN_PAREN)
958 postfix_append_op(ps, top_op);
959 top_op = filter_opstack_pop(ps);
961 if (top_op == OP_NONE) {
962 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
968 if (append_operand_char(ps, ch)) {
969 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
974 if (strlen(curr_operand(ps)))
975 postfix_append_operand(ps, curr_operand(ps));
977 while (!filter_opstack_empty(ps)) {
978 top_op = filter_opstack_pop(ps);
979 if (top_op == OP_NONE)
981 if (top_op == OP_OPEN_PAREN) {
982 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
985 postfix_append_op(ps, top_op);
991 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
993 struct filter_pred *pred;
995 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
999 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1000 if (!pred->field_name) {
1005 strcpy(pred->str_val, operand2);
1006 pred->str_len = strlen(operand2);
1013 static struct filter_pred *create_logical_pred(int op)
1015 struct filter_pred *pred;
1017 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1026 static int check_preds(struct filter_parse_state *ps)
1028 int n_normal_preds = 0, n_logical_preds = 0;
1029 struct postfix_elt *elt;
1031 list_for_each_entry(elt, &ps->postfix, list) {
1032 if (elt->op == OP_NONE)
1035 if (elt->op == OP_AND || elt->op == OP_OR) {
1042 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1043 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1050 static int replace_preds(struct event_subsystem *system,
1051 struct ftrace_event_call *call,
1052 struct filter_parse_state *ps,
1053 char *filter_string,
1056 char *operand1 = NULL, *operand2 = NULL;
1057 struct filter_pred *pred;
1058 struct postfix_elt *elt;
1062 err = check_preds(ps);
1066 list_for_each_entry(elt, &ps->postfix, list) {
1067 if (elt->op == OP_NONE) {
1069 operand1 = elt->operand;
1071 operand2 = elt->operand;
1073 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1079 if (n_preds++ == MAX_FILTER_PRED) {
1080 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1084 if (elt->op == OP_AND || elt->op == OP_OR) {
1085 pred = create_logical_pred(elt->op);
1089 if (!operand1 || !operand2) {
1090 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1094 pred = create_pred(elt->op, operand1, operand2);
1099 err = filter_add_pred(ps, call, pred, false);
1101 err = filter_add_subsystem_pred(ps, system, pred,
1102 filter_string, dry_run);
1103 filter_free_pred(pred);
1107 operand1 = operand2 = NULL;
1113 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1117 struct filter_parse_state *ps;
1119 mutex_lock(&event_mutex);
1121 err = init_preds(call);
1125 if (!strcmp(strstrip(filter_string), "0")) {
1126 filter_disable_preds(call);
1127 remove_filter_string(call->filter);
1128 mutex_unlock(&event_mutex);
1133 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1137 filter_disable_preds(call);
1138 replace_filter_string(call->filter, filter_string);
1140 parse_init(ps, filter_ops, filter_string);
1141 err = filter_parse(ps);
1143 append_filter_err(ps, call->filter);
1147 err = replace_preds(NULL, call, ps, filter_string, false);
1149 append_filter_err(ps, call->filter);
1152 filter_opstack_clear(ps);
1156 mutex_unlock(&event_mutex);
1161 int apply_subsystem_event_filter(struct event_subsystem *system,
1162 char *filter_string)
1166 struct filter_parse_state *ps;
1168 mutex_lock(&event_mutex);
1170 err = init_subsystem_preds(system);
1174 if (!strcmp(strstrip(filter_string), "0")) {
1175 filter_free_subsystem_preds(system, FILTER_DISABLE_ALL);
1176 remove_filter_string(system->filter);
1177 mutex_unlock(&event_mutex);
1182 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1186 replace_filter_string(system->filter, filter_string);
1188 parse_init(ps, filter_ops, filter_string);
1189 err = filter_parse(ps);
1191 append_filter_err(ps, system->filter);
1195 filter_free_subsystem_preds(system, FILTER_INIT_NO_RESET);
1197 /* try to see the filter can be applied to which events */
1198 err = replace_preds(system, NULL, ps, filter_string, true);
1200 append_filter_err(ps, system->filter);
1204 filter_free_subsystem_preds(system, FILTER_SKIP_NO_RESET);
1206 /* really apply the filter to the events */
1207 err = replace_preds(system, NULL, ps, filter_string, false);
1209 append_filter_err(ps, system->filter);
1210 filter_free_subsystem_preds(system, 2);
1214 filter_opstack_clear(ps);
1218 mutex_unlock(&event_mutex);