remove the build INITRAMFS
[qi-kernel.git] / kernel / trace / trace_events_filter.c
blob98a6cc5c64edec869176962836de406097266cab
1 /*
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>
27 #include "trace.h"
28 #include "trace_output.h"
30 enum filter_op_ids
32 OP_OR,
33 OP_AND,
34 OP_NE,
35 OP_EQ,
36 OP_LT,
37 OP_LE,
38 OP_GT,
39 OP_GE,
40 OP_NONE,
41 OP_OPEN_PAREN,
44 struct filter_op {
45 int id;
46 char *string;
47 int precedence;
50 static struct filter_op filter_ops[] = {
51 { OP_OR, "||", 1 },
52 { OP_AND, "&&", 2 },
53 { OP_NE, "!=", 4 },
54 { OP_EQ, "==", 4 },
55 { OP_LT, "<", 5 },
56 { OP_LE, "<=", 5 },
57 { OP_GT, ">", 5 },
58 { OP_GE, ">=", 5 },
59 { OP_NONE, "OP_NONE", 0 },
60 { OP_OPEN_PAREN, "(", 0 },
63 enum {
64 FILT_ERR_NONE,
65 FILT_ERR_INVALID_OP,
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[] = {
79 "No error",
80 "Invalid operator",
81 "Unbalanced parens",
82 "Too many operands",
83 "Operand too long",
84 "Field not found",
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",
93 struct opstack_op {
94 int op;
95 struct list_head list;
98 struct postfix_elt {
99 int op;
100 char *operand;
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;
108 int lasterr;
109 int lasterr_pos;
111 struct {
112 char *string;
113 unsigned int cnt;
114 unsigned int tail;
115 } infix;
117 struct {
118 char string[MAX_FILTER_STR_VAL];
119 int pos;
120 unsigned int tail;
121 } operand;
124 #define DEFINE_COMPARISON_PRED(type) \
125 static int filter_pred_##type(struct filter_pred *pred, void *event, \
126 int val1, int val2) \
128 type *addr = (type *)(event + pred->offset); \
129 type val = (type)pred->val; \
130 int match = 0; \
132 switch (pred->op) { \
133 case OP_LT: \
134 match = (*addr < val); \
135 break; \
136 case OP_LE: \
137 match = (*addr <= val); \
138 break; \
139 case OP_GT: \
140 match = (*addr > val); \
141 break; \
142 case OP_GE: \
143 match = (*addr >= val); \
144 break; \
145 default: \
146 break; \
149 return match; \
152 #define DEFINE_EQUALITY_PRED(size) \
153 static int filter_pred_##size(struct filter_pred *pred, void *event, \
154 int val1, int val2) \
156 u##size *addr = (u##size *)(event + pred->offset); \
157 u##size val = (u##size)pred->val; \
158 int match; \
160 match = (val == *addr) ^ pred->not; \
162 return match; \
165 DEFINE_COMPARISON_PRED(s64);
166 DEFINE_COMPARISON_PRED(u64);
167 DEFINE_COMPARISON_PRED(s32);
168 DEFINE_COMPARISON_PRED(u32);
169 DEFINE_COMPARISON_PRED(s16);
170 DEFINE_COMPARISON_PRED(u16);
171 DEFINE_COMPARISON_PRED(s8);
172 DEFINE_COMPARISON_PRED(u8);
174 DEFINE_EQUALITY_PRED(64);
175 DEFINE_EQUALITY_PRED(32);
176 DEFINE_EQUALITY_PRED(16);
177 DEFINE_EQUALITY_PRED(8);
179 static int filter_pred_and(struct filter_pred *pred __attribute((unused)),
180 void *event __attribute((unused)),
181 int val1, int val2)
183 return val1 && val2;
186 static int filter_pred_or(struct filter_pred *pred __attribute((unused)),
187 void *event __attribute((unused)),
188 int val1, int val2)
190 return val1 || val2;
193 /* Filter predicate for fixed sized arrays of characters */
194 static int filter_pred_string(struct filter_pred *pred, void *event,
195 int val1, int val2)
197 char *addr = (char *)(event + pred->offset);
198 int cmp, match;
200 cmp = strncmp(addr, pred->str_val, pred->str_len);
202 match = (!cmp) ^ pred->not;
204 return match;
207 /* Filter predicate for char * pointers */
208 static int filter_pred_pchar(struct filter_pred *pred, void *event,
209 int val1, int val2)
211 char **addr = (char **)(event + pred->offset);
212 int cmp, match;
214 cmp = strncmp(*addr, pred->str_val, pred->str_len);
216 match = (!cmp) ^ pred->not;
218 return match;
222 * Filter predicate for dynamic sized arrays of characters.
223 * These are implemented through a list of strings at the end
224 * of the entry.
225 * Also each of these strings have a field in the entry which
226 * contains its offset from the beginning of the entry.
227 * We have then first to get this field, dereference it
228 * and add it to the address of the entry, and at last we have
229 * the address of the string.
231 static int filter_pred_strloc(struct filter_pred *pred, void *event,
232 int val1, int val2)
234 u32 str_item = *(u32 *)(event + pred->offset);
235 int str_loc = str_item & 0xffff;
236 int str_len = str_item >> 16;
237 char *addr = (char *)(event + str_loc);
238 int cmp, match;
240 cmp = strncmp(addr, pred->str_val, str_len);
242 match = (!cmp) ^ pred->not;
244 return match;
247 static int filter_pred_none(struct filter_pred *pred, void *event,
248 int val1, int val2)
250 return 0;
253 /* return 1 if event matches, 0 otherwise (discard) */
254 int filter_match_preds(struct ftrace_event_call *call, void *rec)
256 struct event_filter *filter = call->filter;
257 int match, top = 0, val1 = 0, val2 = 0;
258 int stack[MAX_FILTER_PRED];
259 struct filter_pred *pred;
260 int i;
262 for (i = 0; i < filter->n_preds; i++) {
263 pred = filter->preds[i];
264 if (!pred->pop_n) {
265 match = pred->fn(pred, rec, val1, val2);
266 stack[top++] = match;
267 continue;
269 if (pred->pop_n > top) {
270 WARN_ON_ONCE(1);
271 return 0;
273 val1 = stack[--top];
274 val2 = stack[--top];
275 match = pred->fn(pred, rec, val1, val2);
276 stack[top++] = match;
279 return stack[--top];
281 EXPORT_SYMBOL_GPL(filter_match_preds);
283 static void parse_error(struct filter_parse_state *ps, int err, int pos)
285 ps->lasterr = err;
286 ps->lasterr_pos = pos;
289 static void remove_filter_string(struct event_filter *filter)
291 kfree(filter->filter_string);
292 filter->filter_string = NULL;
295 static int replace_filter_string(struct event_filter *filter,
296 char *filter_string)
298 kfree(filter->filter_string);
299 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
300 if (!filter->filter_string)
301 return -ENOMEM;
303 return 0;
306 static int append_filter_string(struct event_filter *filter,
307 char *string)
309 int newlen;
310 char *new_filter_string;
312 BUG_ON(!filter->filter_string);
313 newlen = strlen(filter->filter_string) + strlen(string) + 1;
314 new_filter_string = kmalloc(newlen, GFP_KERNEL);
315 if (!new_filter_string)
316 return -ENOMEM;
318 strcpy(new_filter_string, filter->filter_string);
319 strcat(new_filter_string, string);
320 kfree(filter->filter_string);
321 filter->filter_string = new_filter_string;
323 return 0;
326 static void append_filter_err(struct filter_parse_state *ps,
327 struct event_filter *filter)
329 int pos = ps->lasterr_pos;
330 char *buf, *pbuf;
332 buf = (char *)__get_free_page(GFP_TEMPORARY);
333 if (!buf)
334 return;
336 append_filter_string(filter, "\n");
337 memset(buf, ' ', PAGE_SIZE);
338 if (pos > PAGE_SIZE - 128)
339 pos = 0;
340 buf[pos] = '^';
341 pbuf = &buf[pos] + 1;
343 sprintf(pbuf, "\nparse_error: %s\n", err_text[ps->lasterr]);
344 append_filter_string(filter, buf);
345 free_page((unsigned long) buf);
348 void print_event_filter(struct ftrace_event_call *call, struct trace_seq *s)
350 struct event_filter *filter = call->filter;
352 mutex_lock(&event_mutex);
353 if (filter && filter->filter_string)
354 trace_seq_printf(s, "%s\n", filter->filter_string);
355 else
356 trace_seq_printf(s, "none\n");
357 mutex_unlock(&event_mutex);
360 void print_subsystem_event_filter(struct event_subsystem *system,
361 struct trace_seq *s)
363 struct event_filter *filter = system->filter;
365 mutex_lock(&event_mutex);
366 if (filter && filter->filter_string)
367 trace_seq_printf(s, "%s\n", filter->filter_string);
368 else
369 trace_seq_printf(s, "none\n");
370 mutex_unlock(&event_mutex);
373 static struct ftrace_event_field *
374 find_event_field(struct ftrace_event_call *call, char *name)
376 struct ftrace_event_field *field;
378 list_for_each_entry(field, &call->fields, link) {
379 if (!strcmp(field->name, name))
380 return field;
383 return NULL;
386 static void filter_free_pred(struct filter_pred *pred)
388 if (!pred)
389 return;
391 kfree(pred->field_name);
392 kfree(pred);
395 static void filter_clear_pred(struct filter_pred *pred)
397 kfree(pred->field_name);
398 pred->field_name = NULL;
399 pred->str_len = 0;
402 static int filter_set_pred(struct filter_pred *dest,
403 struct filter_pred *src,
404 filter_pred_fn_t fn)
406 *dest = *src;
407 if (src->field_name) {
408 dest->field_name = kstrdup(src->field_name, GFP_KERNEL);
409 if (!dest->field_name)
410 return -ENOMEM;
412 dest->fn = fn;
414 return 0;
417 static void filter_disable_preds(struct ftrace_event_call *call)
419 struct event_filter *filter = call->filter;
420 int i;
422 call->filter_active = 0;
423 filter->n_preds = 0;
425 for (i = 0; i < MAX_FILTER_PRED; i++)
426 filter->preds[i]->fn = filter_pred_none;
429 void destroy_preds(struct ftrace_event_call *call)
431 struct event_filter *filter = call->filter;
432 int i;
434 if (!filter)
435 return;
437 for (i = 0; i < MAX_FILTER_PRED; i++) {
438 if (filter->preds[i])
439 filter_free_pred(filter->preds[i]);
441 kfree(filter->preds);
442 kfree(filter->filter_string);
443 kfree(filter);
444 call->filter = NULL;
447 static int init_preds(struct ftrace_event_call *call)
449 struct event_filter *filter;
450 struct filter_pred *pred;
451 int i;
453 if (call->filter)
454 return 0;
456 filter = call->filter = kzalloc(sizeof(*filter), GFP_KERNEL);
457 if (!call->filter)
458 return -ENOMEM;
460 filter->n_preds = 0;
462 filter->preds = kzalloc(MAX_FILTER_PRED * sizeof(pred), GFP_KERNEL);
463 if (!filter->preds)
464 goto oom;
466 for (i = 0; i < MAX_FILTER_PRED; i++) {
467 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
468 if (!pred)
469 goto oom;
470 pred->fn = filter_pred_none;
471 filter->preds[i] = pred;
474 return 0;
476 oom:
477 destroy_preds(call);
479 return -ENOMEM;
482 static int init_subsystem_preds(struct event_subsystem *system)
484 struct ftrace_event_call *call;
485 int err;
487 list_for_each_entry(call, &ftrace_events, list) {
488 if (!call->define_fields)
489 continue;
491 if (strcmp(call->system, system->name) != 0)
492 continue;
494 err = init_preds(call);
495 if (err)
496 return err;
499 return 0;
502 enum {
503 FILTER_DISABLE_ALL,
504 FILTER_INIT_NO_RESET,
505 FILTER_SKIP_NO_RESET,
508 static void filter_free_subsystem_preds(struct event_subsystem *system,
509 int flag)
511 struct ftrace_event_call *call;
513 list_for_each_entry(call, &ftrace_events, list) {
514 if (!call->define_fields)
515 continue;
517 if (strcmp(call->system, system->name) != 0)
518 continue;
520 if (flag == FILTER_INIT_NO_RESET) {
521 call->filter->no_reset = false;
522 continue;
525 if (flag == FILTER_SKIP_NO_RESET && call->filter->no_reset)
526 continue;
528 filter_disable_preds(call);
529 remove_filter_string(call->filter);
533 static int filter_add_pred_fn(struct filter_parse_state *ps,
534 struct ftrace_event_call *call,
535 struct filter_pred *pred,
536 filter_pred_fn_t fn)
538 struct event_filter *filter = call->filter;
539 int idx, err;
541 if (filter->n_preds == MAX_FILTER_PRED) {
542 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
543 return -ENOSPC;
546 idx = filter->n_preds;
547 filter_clear_pred(filter->preds[idx]);
548 err = filter_set_pred(filter->preds[idx], pred, fn);
549 if (err)
550 return err;
552 filter->n_preds++;
553 call->filter_active = 1;
555 return 0;
558 int filter_assign_type(const char *type)
560 if (strstr(type, "__data_loc") && strstr(type, "char"))
561 return FILTER_DYN_STRING;
563 if (strchr(type, '[') && strstr(type, "char"))
564 return FILTER_STATIC_STRING;
566 return FILTER_OTHER;
569 static bool is_string_field(struct ftrace_event_field *field)
571 return field->filter_type == FILTER_DYN_STRING ||
572 field->filter_type == FILTER_STATIC_STRING ||
573 field->filter_type == FILTER_PTR_STRING;
576 static int is_legal_op(struct ftrace_event_field *field, int op)
578 if (is_string_field(field) && (op != OP_EQ && op != OP_NE))
579 return 0;
581 return 1;
584 static filter_pred_fn_t select_comparison_fn(int op, int field_size,
585 int field_is_signed)
587 filter_pred_fn_t fn = NULL;
589 switch (field_size) {
590 case 8:
591 if (op == OP_EQ || op == OP_NE)
592 fn = filter_pred_64;
593 else if (field_is_signed)
594 fn = filter_pred_s64;
595 else
596 fn = filter_pred_u64;
597 break;
598 case 4:
599 if (op == OP_EQ || op == OP_NE)
600 fn = filter_pred_32;
601 else if (field_is_signed)
602 fn = filter_pred_s32;
603 else
604 fn = filter_pred_u32;
605 break;
606 case 2:
607 if (op == OP_EQ || op == OP_NE)
608 fn = filter_pred_16;
609 else if (field_is_signed)
610 fn = filter_pred_s16;
611 else
612 fn = filter_pred_u16;
613 break;
614 case 1:
615 if (op == OP_EQ || op == OP_NE)
616 fn = filter_pred_8;
617 else if (field_is_signed)
618 fn = filter_pred_s8;
619 else
620 fn = filter_pred_u8;
621 break;
624 return fn;
627 static int filter_add_pred(struct filter_parse_state *ps,
628 struct ftrace_event_call *call,
629 struct filter_pred *pred,
630 bool dry_run)
632 struct ftrace_event_field *field;
633 filter_pred_fn_t fn;
634 unsigned long long val;
635 int ret;
637 pred->fn = filter_pred_none;
639 if (pred->op == OP_AND) {
640 pred->pop_n = 2;
641 fn = filter_pred_and;
642 goto add_pred_fn;
643 } else if (pred->op == OP_OR) {
644 pred->pop_n = 2;
645 fn = filter_pred_or;
646 goto add_pred_fn;
649 field = find_event_field(call, pred->field_name);
650 if (!field) {
651 parse_error(ps, FILT_ERR_FIELD_NOT_FOUND, 0);
652 return -EINVAL;
655 pred->offset = field->offset;
657 if (!is_legal_op(field, pred->op)) {
658 parse_error(ps, FILT_ERR_ILLEGAL_FIELD_OP, 0);
659 return -EINVAL;
662 if (is_string_field(field)) {
663 pred->str_len = field->size;
665 if (field->filter_type == FILTER_STATIC_STRING)
666 fn = filter_pred_string;
667 else if (field->filter_type == FILTER_DYN_STRING)
668 fn = filter_pred_strloc;
669 else {
670 fn = filter_pred_pchar;
671 pred->str_len = strlen(pred->str_val);
673 } else {
674 if (field->is_signed)
675 ret = strict_strtoll(pred->str_val, 0, &val);
676 else
677 ret = strict_strtoull(pred->str_val, 0, &val);
678 if (ret) {
679 parse_error(ps, FILT_ERR_ILLEGAL_INTVAL, 0);
680 return -EINVAL;
682 pred->val = val;
684 fn = select_comparison_fn(pred->op, field->size,
685 field->is_signed);
686 if (!fn) {
687 parse_error(ps, FILT_ERR_INVALID_OP, 0);
688 return -EINVAL;
692 if (pred->op == OP_NE)
693 pred->not = 1;
695 add_pred_fn:
696 if (!dry_run)
697 return filter_add_pred_fn(ps, call, pred, fn);
698 return 0;
701 static int filter_add_subsystem_pred(struct filter_parse_state *ps,
702 struct event_subsystem *system,
703 struct filter_pred *pred,
704 char *filter_string,
705 bool dry_run)
707 struct ftrace_event_call *call;
708 int err = 0;
709 bool fail = true;
711 list_for_each_entry(call, &ftrace_events, list) {
713 if (!call->define_fields)
714 continue;
716 if (strcmp(call->system, system->name))
717 continue;
719 if (call->filter->no_reset)
720 continue;
722 err = filter_add_pred(ps, call, pred, dry_run);
723 if (err)
724 call->filter->no_reset = true;
725 else
726 fail = false;
728 if (!dry_run)
729 replace_filter_string(call->filter, filter_string);
732 if (fail) {
733 parse_error(ps, FILT_ERR_BAD_SUBSYS_FILTER, 0);
734 return err;
736 return 0;
739 static void parse_init(struct filter_parse_state *ps,
740 struct filter_op *ops,
741 char *infix_string)
743 memset(ps, '\0', sizeof(*ps));
745 ps->infix.string = infix_string;
746 ps->infix.cnt = strlen(infix_string);
747 ps->ops = ops;
749 INIT_LIST_HEAD(&ps->opstack);
750 INIT_LIST_HEAD(&ps->postfix);
753 static char infix_next(struct filter_parse_state *ps)
755 ps->infix.cnt--;
757 return ps->infix.string[ps->infix.tail++];
760 static char infix_peek(struct filter_parse_state *ps)
762 if (ps->infix.tail == strlen(ps->infix.string))
763 return 0;
765 return ps->infix.string[ps->infix.tail];
768 static void infix_advance(struct filter_parse_state *ps)
770 ps->infix.cnt--;
771 ps->infix.tail++;
774 static inline int is_precedence_lower(struct filter_parse_state *ps,
775 int a, int b)
777 return ps->ops[a].precedence < ps->ops[b].precedence;
780 static inline int is_op_char(struct filter_parse_state *ps, char c)
782 int i;
784 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
785 if (ps->ops[i].string[0] == c)
786 return 1;
789 return 0;
792 static int infix_get_op(struct filter_parse_state *ps, char firstc)
794 char nextc = infix_peek(ps);
795 char opstr[3];
796 int i;
798 opstr[0] = firstc;
799 opstr[1] = nextc;
800 opstr[2] = '\0';
802 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
803 if (!strcmp(opstr, ps->ops[i].string)) {
804 infix_advance(ps);
805 return ps->ops[i].id;
809 opstr[1] = '\0';
811 for (i = 0; strcmp(ps->ops[i].string, "OP_NONE"); i++) {
812 if (!strcmp(opstr, ps->ops[i].string))
813 return ps->ops[i].id;
816 return OP_NONE;
819 static inline void clear_operand_string(struct filter_parse_state *ps)
821 memset(ps->operand.string, '\0', MAX_FILTER_STR_VAL);
822 ps->operand.tail = 0;
825 static inline int append_operand_char(struct filter_parse_state *ps, char c)
827 if (ps->operand.tail == MAX_FILTER_STR_VAL - 1)
828 return -EINVAL;
830 ps->operand.string[ps->operand.tail++] = c;
832 return 0;
835 static int filter_opstack_push(struct filter_parse_state *ps, int op)
837 struct opstack_op *opstack_op;
839 opstack_op = kmalloc(sizeof(*opstack_op), GFP_KERNEL);
840 if (!opstack_op)
841 return -ENOMEM;
843 opstack_op->op = op;
844 list_add(&opstack_op->list, &ps->opstack);
846 return 0;
849 static int filter_opstack_empty(struct filter_parse_state *ps)
851 return list_empty(&ps->opstack);
854 static int filter_opstack_top(struct filter_parse_state *ps)
856 struct opstack_op *opstack_op;
858 if (filter_opstack_empty(ps))
859 return OP_NONE;
861 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
863 return opstack_op->op;
866 static int filter_opstack_pop(struct filter_parse_state *ps)
868 struct opstack_op *opstack_op;
869 int op;
871 if (filter_opstack_empty(ps))
872 return OP_NONE;
874 opstack_op = list_first_entry(&ps->opstack, struct opstack_op, list);
875 op = opstack_op->op;
876 list_del(&opstack_op->list);
878 kfree(opstack_op);
880 return op;
883 static void filter_opstack_clear(struct filter_parse_state *ps)
885 while (!filter_opstack_empty(ps))
886 filter_opstack_pop(ps);
889 static char *curr_operand(struct filter_parse_state *ps)
891 return ps->operand.string;
894 static int postfix_append_operand(struct filter_parse_state *ps, char *operand)
896 struct postfix_elt *elt;
898 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
899 if (!elt)
900 return -ENOMEM;
902 elt->op = OP_NONE;
903 elt->operand = kstrdup(operand, GFP_KERNEL);
904 if (!elt->operand) {
905 kfree(elt);
906 return -ENOMEM;
909 list_add_tail(&elt->list, &ps->postfix);
911 return 0;
914 static int postfix_append_op(struct filter_parse_state *ps, int op)
916 struct postfix_elt *elt;
918 elt = kmalloc(sizeof(*elt), GFP_KERNEL);
919 if (!elt)
920 return -ENOMEM;
922 elt->op = op;
923 elt->operand = NULL;
925 list_add_tail(&elt->list, &ps->postfix);
927 return 0;
930 static void postfix_clear(struct filter_parse_state *ps)
932 struct postfix_elt *elt;
934 while (!list_empty(&ps->postfix)) {
935 elt = list_first_entry(&ps->postfix, struct postfix_elt, list);
936 list_del(&elt->list);
937 kfree(elt->operand);
938 kfree(elt);
942 static int filter_parse(struct filter_parse_state *ps)
944 int in_string = 0;
945 int op, top_op;
946 char ch;
948 while ((ch = infix_next(ps))) {
949 if (ch == '"') {
950 in_string ^= 1;
951 continue;
954 if (in_string)
955 goto parse_operand;
957 if (isspace(ch))
958 continue;
960 if (is_op_char(ps, ch)) {
961 op = infix_get_op(ps, ch);
962 if (op == OP_NONE) {
963 parse_error(ps, FILT_ERR_INVALID_OP, 0);
964 return -EINVAL;
967 if (strlen(curr_operand(ps))) {
968 postfix_append_operand(ps, curr_operand(ps));
969 clear_operand_string(ps);
972 while (!filter_opstack_empty(ps)) {
973 top_op = filter_opstack_top(ps);
974 if (!is_precedence_lower(ps, top_op, op)) {
975 top_op = filter_opstack_pop(ps);
976 postfix_append_op(ps, top_op);
977 continue;
979 break;
982 filter_opstack_push(ps, op);
983 continue;
986 if (ch == '(') {
987 filter_opstack_push(ps, OP_OPEN_PAREN);
988 continue;
991 if (ch == ')') {
992 if (strlen(curr_operand(ps))) {
993 postfix_append_operand(ps, curr_operand(ps));
994 clear_operand_string(ps);
997 top_op = filter_opstack_pop(ps);
998 while (top_op != OP_NONE) {
999 if (top_op == OP_OPEN_PAREN)
1000 break;
1001 postfix_append_op(ps, top_op);
1002 top_op = filter_opstack_pop(ps);
1004 if (top_op == OP_NONE) {
1005 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1006 return -EINVAL;
1008 continue;
1010 parse_operand:
1011 if (append_operand_char(ps, ch)) {
1012 parse_error(ps, FILT_ERR_OPERAND_TOO_LONG, 0);
1013 return -EINVAL;
1017 if (strlen(curr_operand(ps)))
1018 postfix_append_operand(ps, curr_operand(ps));
1020 while (!filter_opstack_empty(ps)) {
1021 top_op = filter_opstack_pop(ps);
1022 if (top_op == OP_NONE)
1023 break;
1024 if (top_op == OP_OPEN_PAREN) {
1025 parse_error(ps, FILT_ERR_UNBALANCED_PAREN, 0);
1026 return -EINVAL;
1028 postfix_append_op(ps, top_op);
1031 return 0;
1034 static struct filter_pred *create_pred(int op, char *operand1, char *operand2)
1036 struct filter_pred *pred;
1038 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1039 if (!pred)
1040 return NULL;
1042 pred->field_name = kstrdup(operand1, GFP_KERNEL);
1043 if (!pred->field_name) {
1044 kfree(pred);
1045 return NULL;
1048 strcpy(pred->str_val, operand2);
1049 pred->str_len = strlen(operand2);
1051 pred->op = op;
1053 return pred;
1056 static struct filter_pred *create_logical_pred(int op)
1058 struct filter_pred *pred;
1060 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1061 if (!pred)
1062 return NULL;
1064 pred->op = op;
1066 return pred;
1069 static int check_preds(struct filter_parse_state *ps)
1071 int n_normal_preds = 0, n_logical_preds = 0;
1072 struct postfix_elt *elt;
1074 list_for_each_entry(elt, &ps->postfix, list) {
1075 if (elt->op == OP_NONE)
1076 continue;
1078 if (elt->op == OP_AND || elt->op == OP_OR) {
1079 n_logical_preds++;
1080 continue;
1082 n_normal_preds++;
1085 if (!n_normal_preds || n_logical_preds >= n_normal_preds) {
1086 parse_error(ps, FILT_ERR_INVALID_FILTER, 0);
1087 return -EINVAL;
1090 return 0;
1093 static int replace_preds(struct event_subsystem *system,
1094 struct ftrace_event_call *call,
1095 struct filter_parse_state *ps,
1096 char *filter_string,
1097 bool dry_run)
1099 char *operand1 = NULL, *operand2 = NULL;
1100 struct filter_pred *pred;
1101 struct postfix_elt *elt;
1102 int err;
1103 int n_preds = 0;
1105 err = check_preds(ps);
1106 if (err)
1107 return err;
1109 list_for_each_entry(elt, &ps->postfix, list) {
1110 if (elt->op == OP_NONE) {
1111 if (!operand1)
1112 operand1 = elt->operand;
1113 else if (!operand2)
1114 operand2 = elt->operand;
1115 else {
1116 parse_error(ps, FILT_ERR_TOO_MANY_OPERANDS, 0);
1117 return -EINVAL;
1119 continue;
1122 if (n_preds++ == MAX_FILTER_PRED) {
1123 parse_error(ps, FILT_ERR_TOO_MANY_PREDS, 0);
1124 return -ENOSPC;
1127 if (elt->op == OP_AND || elt->op == OP_OR) {
1128 pred = create_logical_pred(elt->op);
1129 goto add_pred;
1132 if (!operand1 || !operand2) {
1133 parse_error(ps, FILT_ERR_MISSING_FIELD, 0);
1134 return -EINVAL;
1137 pred = create_pred(elt->op, operand1, operand2);
1138 add_pred:
1139 if (!pred)
1140 return -ENOMEM;
1141 if (call)
1142 err = filter_add_pred(ps, call, pred, false);
1143 else
1144 err = filter_add_subsystem_pred(ps, system, pred,
1145 filter_string, dry_run);
1146 filter_free_pred(pred);
1147 if (err)
1148 return err;
1150 operand1 = operand2 = NULL;
1153 return 0;
1156 int apply_event_filter(struct ftrace_event_call *call, char *filter_string)
1158 int err;
1160 struct filter_parse_state *ps;
1162 mutex_lock(&event_mutex);
1164 err = init_preds(call);
1165 if (err)
1166 goto out_unlock;
1168 if (!strcmp(strstrip(filter_string), "0")) {
1169 filter_disable_preds(call);
1170 remove_filter_string(call->filter);
1171 mutex_unlock(&event_mutex);
1172 return 0;
1175 err = -ENOMEM;
1176 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1177 if (!ps)
1178 goto out_unlock;
1180 filter_disable_preds(call);
1181 replace_filter_string(call->filter, filter_string);
1183 parse_init(ps, filter_ops, filter_string);
1184 err = filter_parse(ps);
1185 if (err) {
1186 append_filter_err(ps, call->filter);
1187 goto out;
1190 err = replace_preds(NULL, call, ps, filter_string, false);
1191 if (err)
1192 append_filter_err(ps, call->filter);
1194 out:
1195 filter_opstack_clear(ps);
1196 postfix_clear(ps);
1197 kfree(ps);
1198 out_unlock:
1199 mutex_unlock(&event_mutex);
1201 return err;
1204 int apply_subsystem_event_filter(struct event_subsystem *system,
1205 char *filter_string)
1207 int err;
1209 struct filter_parse_state *ps;
1211 mutex_lock(&event_mutex);
1213 err = init_subsystem_preds(system);
1214 if (err)
1215 goto out_unlock;
1217 if (!strcmp(strstrip(filter_string), "0")) {
1218 filter_free_subsystem_preds(system, FILTER_DISABLE_ALL);
1219 remove_filter_string(system->filter);
1220 mutex_unlock(&event_mutex);
1221 return 0;
1224 err = -ENOMEM;
1225 ps = kzalloc(sizeof(*ps), GFP_KERNEL);
1226 if (!ps)
1227 goto out_unlock;
1229 replace_filter_string(system->filter, filter_string);
1231 parse_init(ps, filter_ops, filter_string);
1232 err = filter_parse(ps);
1233 if (err) {
1234 append_filter_err(ps, system->filter);
1235 goto out;
1238 filter_free_subsystem_preds(system, FILTER_INIT_NO_RESET);
1240 /* try to see the filter can be applied to which events */
1241 err = replace_preds(system, NULL, ps, filter_string, true);
1242 if (err) {
1243 append_filter_err(ps, system->filter);
1244 goto out;
1247 filter_free_subsystem_preds(system, FILTER_SKIP_NO_RESET);
1249 /* really apply the filter to the events */
1250 err = replace_preds(system, NULL, ps, filter_string, false);
1251 if (err) {
1252 append_filter_err(ps, system->filter);
1253 filter_free_subsystem_preds(system, 2);
1256 out:
1257 filter_opstack_clear(ps);
1258 postfix_clear(ps);
1259 kfree(ps);
1260 out_unlock:
1261 mutex_unlock(&event_mutex);
1263 return err;