2 * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <sys/types.h>
28 #include "event-parse.h"
29 #include "event-utils.h"
33 static struct format_field comm = {
38 struct event_list *next;
39 struct event_format *event;
42 #define MAX_ERR_STR_SIZE 256
44 static void show_error(char **error_str, const char *fmt, ...)
46 unsigned long long index;
56 input = pevent_get_input_buf();
57 index = pevent_get_input_buf_ptr();
58 len = input ? strlen(input) : 0;
60 error = malloc_or_die(MAX_ERR_STR_SIZE + (len*2) + 3);
65 for (i = 1; i < len && i < index; i++)
68 error[len + i + 1] = '\n';
73 vsnprintf(error + len, MAX_ERR_STR_SIZE, fmt, ap);
79 static void free_token(char *token)
81 pevent_free_token(token);
84 static enum event_type read_token(char **tok)
91 type = pevent_read_token(&token);
92 } while (type == EVENT_NEWLINE || type == EVENT_SPACE);
94 /* If token is = or ! check to see if the next char is ~ */
96 (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&
97 pevent_peek_char() == '~') {
99 *tok = malloc_or_die(3);
100 sprintf(*tok, "%c%c", *token, '~');
102 /* Now remove the '~' from the buffer */
103 pevent_read_token(&token);
111 static int filter_cmp(const void *a, const void *b)
113 const struct filter_type *ea = a;
114 const struct filter_type *eb = b;
116 if (ea->event_id < eb->event_id)
119 if (ea->event_id > eb->event_id)
125 static struct filter_type *
126 find_filter_type(struct event_filter *filter, int id)
128 struct filter_type *filter_type;
129 struct filter_type key;
133 filter_type = bsearch(&key, filter->event_filters,
135 sizeof(*filter->event_filters),
141 static struct filter_type *
142 add_filter_type(struct event_filter *filter, int id)
144 struct filter_type *filter_type;
147 filter_type = find_filter_type(filter, id);
151 filter->event_filters = realloc(filter->event_filters,
152 sizeof(*filter->event_filters) *
153 (filter->filters + 1));
154 if (!filter->event_filters)
155 die("Could not allocate filter");
157 for (i = 0; i < filter->filters; i++) {
158 if (filter->event_filters[i].event_id > id)
162 if (i < filter->filters)
163 memmove(&filter->event_filters[i+1],
164 &filter->event_filters[i],
165 sizeof(*filter->event_filters) *
166 (filter->filters - i));
168 filter_type = &filter->event_filters[i];
169 filter_type->event_id = id;
170 filter_type->event = pevent_find_event(filter->pevent, id);
171 filter_type->filter = NULL;
179 * pevent_filter_alloc - create a new event filter
180 * @pevent: The pevent that this filter is associated with
182 struct event_filter *pevent_filter_alloc(struct pevent *pevent)
184 struct event_filter *filter;
186 filter = malloc_or_die(sizeof(*filter));
187 memset(filter, 0, sizeof(*filter));
188 filter->pevent = pevent;
194 static struct filter_arg *allocate_arg(void)
196 struct filter_arg *arg;
198 arg = malloc_or_die(sizeof(*arg));
199 memset(arg, 0, sizeof(*arg));
204 static void free_arg(struct filter_arg *arg)
210 case FILTER_ARG_NONE:
211 case FILTER_ARG_BOOLEAN:
215 free_arg(arg->num.left);
216 free_arg(arg->num.right);
220 free_arg(arg->exp.left);
221 free_arg(arg->exp.right);
226 regfree(&arg->str.reg);
227 free(arg->str.buffer);
230 case FILTER_ARG_VALUE:
231 if (arg->value.type == FILTER_STRING ||
232 arg->value.type == FILTER_CHAR)
233 free(arg->value.str);
237 free_arg(arg->op.left);
238 free_arg(arg->op.right);
246 static void add_event(struct event_list **events,
247 struct event_format *event)
249 struct event_list *list;
251 list = malloc_or_die(sizeof(*list));
252 list->next = *events;
257 static int event_match(struct event_format *event,
258 regex_t *sreg, regex_t *ereg)
261 return !regexec(sreg, event->system, 0, NULL, 0) &&
262 !regexec(ereg, event->name, 0, NULL, 0);
265 return !regexec(ereg, event->system, 0, NULL, 0) ||
266 !regexec(ereg, event->name, 0, NULL, 0);
270 find_event(struct pevent *pevent, struct event_list **events,
271 char *sys_name, char *event_name)
273 struct event_format *event;
282 /* if no name is given, then swap sys and name */
283 event_name = sys_name;
287 reg = malloc_or_die(strlen(event_name) + 3);
288 sprintf(reg, "^%s$", event_name);
290 ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);
297 reg = malloc_or_die(strlen(sys_name) + 3);
298 sprintf(reg, "^%s$", sys_name);
299 ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);
307 for (i = 0; i < pevent->nr_events; i++) {
308 event = pevent->events[i];
309 if (event_match(event, sys_name ? &sreg : NULL, &ereg)) {
311 add_event(events, event);
325 static void free_events(struct event_list *events)
327 struct event_list *event;
331 events = events->next;
336 static struct filter_arg *
337 create_arg_item(struct event_format *event, const char *token,
338 enum event_type type, char **error_str)
340 struct format_field *field;
341 struct filter_arg *arg;
343 arg = allocate_arg();
349 arg->type = FILTER_ARG_VALUE;
351 type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR;
352 arg->value.str = strdup(token);
354 die("malloc string");
357 /* if it is a number, then convert it */
358 if (isdigit(token[0])) {
359 arg->type = FILTER_ARG_VALUE;
360 arg->value.type = FILTER_NUMBER;
361 arg->value.val = strtoull(token, NULL, 0);
364 /* Consider this a field */
365 field = pevent_find_any_field(event, token);
367 if (strcmp(token, COMM) != 0) {
368 /* not a field, Make it false */
369 arg->type = FILTER_ARG_BOOLEAN;
370 arg->boolean.value = FILTER_FALSE;
373 /* If token is 'COMM' then it is special */
376 arg->type = FILTER_ARG_FIELD;
377 arg->field.field = field;
381 show_error(error_str, "expected a value but found %s",
388 static struct filter_arg *
389 create_arg_op(enum filter_op_type btype)
391 struct filter_arg *arg;
393 arg = allocate_arg();
394 arg->type = FILTER_ARG_OP;
395 arg->op.type = btype;
400 static struct filter_arg *
401 create_arg_exp(enum filter_exp_type etype)
403 struct filter_arg *arg;
405 arg = allocate_arg();
406 arg->type = FILTER_ARG_EXP;
407 arg->op.type = etype;
412 static struct filter_arg *
413 create_arg_cmp(enum filter_exp_type etype)
415 struct filter_arg *arg;
417 arg = allocate_arg();
418 /* Use NUM and change if necessary */
419 arg->type = FILTER_ARG_NUM;
420 arg->op.type = etype;
425 static int add_right(struct filter_arg *op, struct filter_arg *arg,
428 struct filter_arg *left;
450 * The arg must be num, str, or field
453 case FILTER_ARG_VALUE:
454 case FILTER_ARG_FIELD:
457 show_error(error_str,
463 * Depending on the type, we may need to
464 * convert this to a string or regex.
466 switch (arg->value.type) {
469 * A char should be converted to number if
470 * the string is 1 byte, and the compare
473 if (strlen(arg->value.str) == 1 &&
474 op->num.type != FILTER_CMP_REGEX &&
475 op->num.type != FILTER_CMP_NOT_REGEX) {
476 arg->value.type = FILTER_NUMBER;
482 /* convert op to a string arg */
483 op_type = op->num.type;
485 str = arg->value.str;
487 /* reset the op for the new field */
488 memset(op, 0, sizeof(*op));
491 * If left arg was a field not found then
492 * NULL the entire op.
494 if (left->type == FILTER_ARG_BOOLEAN) {
497 op->type = FILTER_ARG_BOOLEAN;
498 op->boolean.value = FILTER_FALSE;
502 /* Left arg must be a field */
503 if (left->type != FILTER_ARG_FIELD) {
504 show_error(error_str,
505 "Illegal lvalue for string comparison");
509 /* Make sure this is a valid string compare */
512 op_type = FILTER_CMP_MATCH;
515 op_type = FILTER_CMP_NOT_MATCH;
518 case FILTER_CMP_REGEX:
519 case FILTER_CMP_NOT_REGEX:
520 ret = regcomp(&op->str.reg, str, REG_ICASE|REG_NOSUB);
522 show_error(error_str,
523 "RegEx '%s' did not compute",
529 show_error(error_str,
530 "Illegal comparison for string");
534 op->type = FILTER_ARG_STR;
535 op->str.type = op_type;
536 op->str.field = left->field.field;
537 op->str.val = strdup(str);
539 die("malloc string");
541 * Need a buffer to copy data for tests
543 op->str.buffer = malloc_or_die(op->str.field->size + 1);
544 /* Null terminate this buffer */
545 op->str.buffer[op->str.field->size] = 0;
547 /* We no longer have left or right args */
556 switch (op->num.type) {
557 case FILTER_CMP_REGEX:
558 case FILTER_CMP_NOT_REGEX:
559 show_error(error_str,
560 "Op not allowed with integers");
567 /* numeric compare */
581 show_error(error_str,
586 static struct filter_arg *
587 rotate_op_right(struct filter_arg *a, struct filter_arg *b)
589 struct filter_arg *arg;
596 static int add_left(struct filter_arg *op, struct filter_arg *arg)
600 if (arg->type == FILTER_ARG_OP)
601 arg = rotate_op_right(arg, op);
609 if (arg->type == FILTER_ARG_OP)
610 arg = rotate_op_right(arg, op);
612 /* left arg of compares must be a field */
613 if (arg->type != FILTER_ARG_FIELD &&
614 arg->type != FILTER_ARG_BOOLEAN)
632 static enum op_type process_op(const char *token,
633 enum filter_op_type *btype,
634 enum filter_cmp_type *ctype,
635 enum filter_exp_type *etype)
637 *btype = FILTER_OP_NOT;
638 *etype = FILTER_EXP_NONE;
639 *ctype = FILTER_CMP_NONE;
641 if (strcmp(token, "&&") == 0)
642 *btype = FILTER_OP_AND;
643 else if (strcmp(token, "||") == 0)
644 *btype = FILTER_OP_OR;
645 else if (strcmp(token, "!") == 0)
648 if (*btype != FILTER_OP_NOT)
651 /* Check for value expressions */
652 if (strcmp(token, "+") == 0) {
653 *etype = FILTER_EXP_ADD;
654 } else if (strcmp(token, "-") == 0) {
655 *etype = FILTER_EXP_SUB;
656 } else if (strcmp(token, "*") == 0) {
657 *etype = FILTER_EXP_MUL;
658 } else if (strcmp(token, "/") == 0) {
659 *etype = FILTER_EXP_DIV;
660 } else if (strcmp(token, "%") == 0) {
661 *etype = FILTER_EXP_MOD;
662 } else if (strcmp(token, ">>") == 0) {
663 *etype = FILTER_EXP_RSHIFT;
664 } else if (strcmp(token, "<<") == 0) {
665 *etype = FILTER_EXP_LSHIFT;
666 } else if (strcmp(token, "&") == 0) {
667 *etype = FILTER_EXP_AND;
668 } else if (strcmp(token, "|") == 0) {
669 *etype = FILTER_EXP_OR;
670 } else if (strcmp(token, "^") == 0) {
671 *etype = FILTER_EXP_XOR;
672 } else if (strcmp(token, "~") == 0)
673 *etype = FILTER_EXP_NOT;
675 if (*etype != FILTER_EXP_NONE)
678 /* Check for compares */
679 if (strcmp(token, "==") == 0)
680 *ctype = FILTER_CMP_EQ;
681 else if (strcmp(token, "!=") == 0)
682 *ctype = FILTER_CMP_NE;
683 else if (strcmp(token, "<") == 0)
684 *ctype = FILTER_CMP_LT;
685 else if (strcmp(token, ">") == 0)
686 *ctype = FILTER_CMP_GT;
687 else if (strcmp(token, "<=") == 0)
688 *ctype = FILTER_CMP_LE;
689 else if (strcmp(token, ">=") == 0)
690 *ctype = FILTER_CMP_GE;
691 else if (strcmp(token, "=~") == 0)
692 *ctype = FILTER_CMP_REGEX;
693 else if (strcmp(token, "!~") == 0)
694 *ctype = FILTER_CMP_NOT_REGEX;
701 static int check_op_done(struct filter_arg *arg)
705 return arg->exp.right != NULL;
708 return arg->op.right != NULL;
711 return arg->num.right != NULL;
714 /* A string conversion is always done */
717 case FILTER_ARG_BOOLEAN:
718 /* field not found, is ok */
732 void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,
733 struct filter_arg *arg)
735 struct filter_arg *other_child;
736 struct filter_arg **ptr;
738 if (parent->type != FILTER_ARG_OP &&
739 arg->type != FILTER_ARG_OP)
740 die("can not reparent other than OP");
742 /* Get the sibling */
743 if (old_child->op.right == arg) {
744 ptr = &old_child->op.right;
745 other_child = old_child->op.left;
746 } else if (old_child->op.left == arg) {
747 ptr = &old_child->op.left;
748 other_child = old_child->op.right;
750 die("Error in reparent op, find other child");
752 /* Detach arg from old_child */
756 if (parent == old_child) {
757 free_arg(other_child);
759 /* Free arg without recussion */
764 if (parent->op.right == old_child)
765 ptr = &parent->op.right;
766 else if (parent->op.left == old_child)
767 ptr = &parent->op.left;
769 die("Error in reparent op");
775 enum filter_vals test_arg(struct filter_arg *parent, struct filter_arg *arg)
777 enum filter_vals lval, rval;
782 case FILTER_ARG_BOOLEAN:
783 return FILTER_VAL_FALSE + arg->boolean.value;
787 case FILTER_ARG_VALUE:
788 case FILTER_ARG_FIELD:
789 return FILTER_VAL_NORM;
792 lval = test_arg(arg, arg->exp.left);
793 if (lval != FILTER_VAL_NORM)
795 rval = test_arg(arg, arg->exp.right);
796 if (rval != FILTER_VAL_NORM)
798 return FILTER_VAL_NORM;
801 lval = test_arg(arg, arg->num.left);
802 if (lval != FILTER_VAL_NORM)
804 rval = test_arg(arg, arg->num.right);
805 if (rval != FILTER_VAL_NORM)
807 return FILTER_VAL_NORM;
810 if (arg->op.type != FILTER_OP_NOT) {
811 lval = test_arg(arg, arg->op.left);
813 case FILTER_VAL_NORM:
815 case FILTER_VAL_TRUE:
816 if (arg->op.type == FILTER_OP_OR)
817 return FILTER_VAL_TRUE;
818 rval = test_arg(arg, arg->op.right);
819 if (rval != FILTER_VAL_NORM)
822 reparent_op_arg(parent, arg, arg->op.right);
823 return FILTER_VAL_NORM;
825 case FILTER_VAL_FALSE:
826 if (arg->op.type == FILTER_OP_AND)
827 return FILTER_VAL_FALSE;
828 rval = test_arg(arg, arg->op.right);
829 if (rval != FILTER_VAL_NORM)
832 reparent_op_arg(parent, arg, arg->op.right);
833 return FILTER_VAL_NORM;
837 rval = test_arg(arg, arg->op.right);
839 case FILTER_VAL_NORM:
841 case FILTER_VAL_TRUE:
842 if (arg->op.type == FILTER_OP_OR)
843 return FILTER_VAL_TRUE;
844 if (arg->op.type == FILTER_OP_NOT)
845 return FILTER_VAL_FALSE;
847 reparent_op_arg(parent, arg, arg->op.left);
848 return FILTER_VAL_NORM;
850 case FILTER_VAL_FALSE:
851 if (arg->op.type == FILTER_OP_AND)
852 return FILTER_VAL_FALSE;
853 if (arg->op.type == FILTER_OP_NOT)
854 return FILTER_VAL_TRUE;
856 reparent_op_arg(parent, arg, arg->op.left);
857 return FILTER_VAL_NORM;
860 return FILTER_VAL_NORM;
862 die("bad arg in filter tree");
864 return FILTER_VAL_NORM;
867 /* Remove any unknown event fields */
868 static struct filter_arg *collapse_tree(struct filter_arg *arg)
870 enum filter_vals ret;
872 ret = test_arg(arg, arg);
874 case FILTER_VAL_NORM:
877 case FILTER_VAL_TRUE:
878 case FILTER_VAL_FALSE:
880 arg = allocate_arg();
881 arg->type = FILTER_ARG_BOOLEAN;
882 arg->boolean.value = ret == FILTER_VAL_TRUE;
889 process_filter(struct event_format *event, struct filter_arg **parg,
890 char **error_str, int not)
892 enum event_type type;
894 struct filter_arg *current_op = NULL;
895 struct filter_arg *current_exp = NULL;
896 struct filter_arg *left_item = NULL;
897 struct filter_arg *arg = NULL;
898 enum op_type op_type;
899 enum filter_op_type btype;
900 enum filter_exp_type etype;
901 enum filter_cmp_type ctype;
908 type = read_token(&token);
913 arg = create_arg_item(event, token, type, error_str);
918 else if (current_exp) {
919 ret = add_right(current_exp, arg, error_str);
923 /* Not's only one one expression */
939 show_error(error_str,
940 "Illegal token ','");
946 show_error(error_str,
947 "Open paren can not come after item");
951 show_error(error_str,
952 "Open paren can not come after expression");
956 ret = process_filter(event, &arg, error_str, 0);
959 show_error(error_str,
960 "Unbalanced number of '('");
965 /* A not wants just one expression */
974 ret = add_right(current_op, arg, error_str);
982 if (!current_op && !current_exp)
985 /* Make sure everything is finished at this level */
986 if (current_exp && !check_op_done(current_exp))
988 if (current_op && !check_op_done(current_op))
1000 op_type = process_op(token, &btype, &ctype, &etype);
1002 /* All expect a left arg except for NOT */
1005 /* Logic ops need a left expression */
1006 if (!current_exp && !current_op)
1010 /* logic only processes ops and exp */
1020 show_error(error_str,
1021 "Unknown op token %s", token);
1028 arg = create_arg_op(btype);
1030 ret = add_left(arg, current_op);
1032 ret = add_left(arg, current_exp);
1038 arg = create_arg_op(btype);
1040 ret = add_right(current_op, arg, error_str);
1044 ret = process_filter(event, &arg, error_str, 1);
1047 ret = add_right(current_exp, arg, error_str);
1054 if (op_type == OP_EXP)
1055 arg = create_arg_exp(etype);
1057 arg = create_arg_cmp(ctype);
1060 ret = add_right(current_op, arg, error_str);
1063 ret = add_left(arg, left_item);
1082 } while (type != EVENT_NONE);
1084 if (!current_op && !current_exp)
1088 current_op = current_exp;
1090 current_op = collapse_tree(current_op);
1097 show_error(error_str, "Syntax error");
1099 free_arg(current_op);
1100 free_arg(current_exp);
1107 process_event(struct event_format *event, const char *filter_str,
1108 struct filter_arg **parg, char **error_str)
1112 pevent_buffer_init(filter_str, strlen(filter_str));
1114 ret = process_filter(event, parg, error_str, 0);
1116 show_error(error_str,
1117 "Unbalanced number of ')'");
1123 /* If parg is NULL, then make it into FALSE */
1125 *parg = allocate_arg();
1126 (*parg)->type = FILTER_ARG_BOOLEAN;
1127 (*parg)->boolean.value = FILTER_FALSE;
1133 static int filter_event(struct event_filter *filter,
1134 struct event_format *event,
1135 const char *filter_str, char **error_str)
1137 struct filter_type *filter_type;
1138 struct filter_arg *arg;
1142 ret = process_event(event, filter_str, &arg, error_str);
1147 /* just add a TRUE arg */
1148 arg = allocate_arg();
1149 arg->type = FILTER_ARG_BOOLEAN;
1150 arg->boolean.value = FILTER_TRUE;
1153 filter_type = add_filter_type(filter, event->id);
1154 if (filter_type->filter)
1155 free_arg(filter_type->filter);
1156 filter_type->filter = arg;
1162 * pevent_filter_add_filter_str - add a new filter
1163 * @filter: the event filter to add to
1164 * @filter_str: the filter string that contains the filter
1165 * @error_str: string containing reason for failed filter
1167 * Returns 0 if the filter was successfully added
1168 * -1 if there was an error.
1170 * On error, if @error_str points to a string pointer,
1171 * it is set to the reason that the filter failed.
1172 * This string must be freed with "free".
1174 int pevent_filter_add_filter_str(struct event_filter *filter,
1175 const char *filter_str,
1178 struct pevent *pevent = filter->pevent;
1179 struct event_list *event;
1180 struct event_list *events = NULL;
1181 const char *filter_start;
1182 const char *next_event;
1184 char *event_name = NULL;
1185 char *sys_name = NULL;
1191 /* clear buffer to reset show error */
1192 pevent_buffer_init("", 0);
1197 filter_start = strchr(filter_str, ':');
1199 len = filter_start - filter_str;
1201 len = strlen(filter_str);
1205 next_event = strchr(filter_str, ',');
1207 (!filter_start || next_event < filter_start))
1208 len = next_event - filter_str;
1209 else if (filter_start)
1210 len = filter_start - filter_str;
1212 len = strlen(filter_str);
1214 this_event = malloc_or_die(len + 1);
1215 memcpy(this_event, filter_str, len);
1216 this_event[len] = 0;
1221 filter_str = next_event;
1223 sys_name = strtok_r(this_event, "/", &sp);
1224 event_name = strtok_r(NULL, "/", &sp);
1227 show_error(error_str, "No filter found");
1228 /* This can only happen when events is NULL, but still */
1229 free_events(events);
1234 /* Find this event */
1235 ret = find_event(pevent, &events, strim(sys_name), strim(event_name));
1238 show_error(error_str,
1239 "No event found under '%s.%s'",
1240 sys_name, event_name);
1242 show_error(error_str,
1243 "No event found under '%s'",
1245 free_events(events);
1250 } while (filter_str);
1256 /* filter starts here */
1257 for (event = events; event; event = event->next) {
1258 ret = filter_event(filter, event->event, filter_start,
1260 /* Failures are returned if a parse error happened */
1264 if (ret >= 0 && pevent->test_filters) {
1266 test = pevent_filter_make_string(filter, event->event->id);
1267 printf(" '%s: %s'\n", event->event->name, test);
1272 free_events(events);
1274 if (rtn >= 0 && pevent->test_filters)
1280 static void free_filter_type(struct filter_type *filter_type)
1282 free_arg(filter_type->filter);
1286 * pevent_filter_remove_event - remove a filter for an event
1287 * @filter: the event filter to remove from
1288 * @event_id: the event to remove a filter for
1290 * Removes the filter saved for an event defined by @event_id
1293 * Returns 1: if an event was removed
1294 * 0: if the event was not found
1296 int pevent_filter_remove_event(struct event_filter *filter,
1299 struct filter_type *filter_type;
1302 if (!filter->filters)
1305 filter_type = find_filter_type(filter, event_id);
1310 free_filter_type(filter_type);
1312 /* The filter_type points into the event_filters array */
1313 len = (unsigned long)(filter->event_filters + filter->filters) -
1314 (unsigned long)(filter_type + 1);
1316 memmove(filter_type, filter_type + 1, len);
1319 memset(&filter->event_filters[filter->filters], 0,
1320 sizeof(*filter_type));
1326 * pevent_filter_reset - clear all filters in a filter
1327 * @filter: the event filter to reset
1329 * Removes all filters from a filter and resets it.
1331 void pevent_filter_reset(struct event_filter *filter)
1335 for (i = 0; i < filter->filters; i++)
1336 free_filter_type(&filter->event_filters[i]);
1338 free(filter->event_filters);
1339 filter->filters = 0;
1340 filter->event_filters = NULL;
1343 void pevent_filter_free(struct event_filter *filter)
1345 pevent_unref(filter->pevent);
1347 pevent_filter_reset(filter);
1352 static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg);
1354 static int copy_filter_type(struct event_filter *filter,
1355 struct event_filter *source,
1356 struct filter_type *filter_type)
1358 struct filter_arg *arg;
1359 struct event_format *event;
1364 /* Can't assume that the pevent's are the same */
1365 sys = filter_type->event->system;
1366 name = filter_type->event->name;
1367 event = pevent_find_event_by_name(filter->pevent, sys, name);
1371 str = arg_to_str(source, filter_type->filter);
1375 if (strcmp(str, "TRUE") == 0 || strcmp(str, "FALSE") == 0) {
1376 /* Add trivial event */
1377 arg = allocate_arg();
1378 arg->type = FILTER_ARG_BOOLEAN;
1379 if (strcmp(str, "TRUE") == 0)
1380 arg->boolean.value = 1;
1382 arg->boolean.value = 0;
1384 filter_type = add_filter_type(filter, event->id);
1385 filter_type->filter = arg;
1391 filter_event(filter, event, str, NULL);
1398 * pevent_filter_copy - copy a filter using another filter
1399 * @dest - the filter to copy to
1400 * @source - the filter to copy from
1402 * Returns 0 on success and -1 if not all filters were copied
1404 int pevent_filter_copy(struct event_filter *dest, struct event_filter *source)
1409 pevent_filter_reset(dest);
1411 for (i = 0; i < source->filters; i++) {
1412 if (copy_filter_type(dest, source, &source->event_filters[i]))
1420 * pevent_update_trivial - update the trivial filters with the given filter
1421 * @dest - the filter to update
1422 * @source - the filter as the source of the update
1423 * @type - the type of trivial filter to update.
1425 * Scan dest for trivial events matching @type to replace with the source.
1427 * Returns 0 on success and -1 if there was a problem updating, but
1428 * events may have still been updated on error.
1430 int pevent_update_trivial(struct event_filter *dest, struct event_filter *source,
1431 enum filter_trivial_type type)
1433 struct pevent *src_pevent;
1434 struct pevent *dest_pevent;
1435 struct event_format *event;
1436 struct filter_type *filter_type;
1437 struct filter_arg *arg;
1441 src_pevent = source->pevent;
1442 dest_pevent = dest->pevent;
1444 /* Do nothing if either of the filters has nothing to filter */
1445 if (!dest->filters || !source->filters)
1448 for (i = 0; i < dest->filters; i++) {
1449 filter_type = &dest->event_filters[i];
1450 arg = filter_type->filter;
1451 if (arg->type != FILTER_ARG_BOOLEAN)
1453 if ((arg->boolean.value && type == FILTER_TRIVIAL_FALSE) ||
1454 (!arg->boolean.value && type == FILTER_TRIVIAL_TRUE))
1457 event = filter_type->event;
1459 if (src_pevent != dest_pevent) {
1461 event = pevent_find_event_by_name(src_pevent,
1468 str = pevent_filter_make_string(source, event->id);
1472 /* Don't bother if the filter is trivial too */
1473 if (strcmp(str, "TRUE") != 0 && strcmp(str, "FALSE") != 0)
1474 filter_event(dest, event, str, NULL);
1481 * pevent_filter_clear_trivial - clear TRUE and FALSE filters
1482 * @filter: the filter to remove trivial filters from
1483 * @type: remove only true, false, or both
1485 * Removes filters that only contain a TRUE or FALES boolean arg.
1487 void pevent_filter_clear_trivial(struct event_filter *filter,
1488 enum filter_trivial_type type)
1490 struct filter_type *filter_type;
1495 if (!filter->filters)
1499 * Two steps, first get all ids with trivial filters.
1500 * then remove those ids.
1502 for (i = 0; i < filter->filters; i++) {
1503 filter_type = &filter->event_filters[i];
1504 if (filter_type->filter->type != FILTER_ARG_BOOLEAN)
1507 case FILTER_TRIVIAL_FALSE:
1508 if (filter_type->filter->boolean.value)
1510 case FILTER_TRIVIAL_TRUE:
1511 if (!filter_type->filter->boolean.value)
1517 ids = realloc(ids, sizeof(*ids) * (count + 1));
1519 die("Can't allocate ids");
1520 ids[count++] = filter_type->event_id;
1526 for (i = 0; i < count; i++)
1527 pevent_filter_remove_event(filter, ids[i]);
1533 * pevent_filter_event_has_trivial - return true event contains trivial filter
1534 * @filter: the filter with the information
1535 * @event_id: the id of the event to test
1536 * @type: trivial type to test for (TRUE, FALSE, EITHER)
1538 * Returns 1 if the event contains a matching trivial type
1541 int pevent_filter_event_has_trivial(struct event_filter *filter,
1543 enum filter_trivial_type type)
1545 struct filter_type *filter_type;
1547 if (!filter->filters)
1550 filter_type = find_filter_type(filter, event_id);
1555 if (filter_type->filter->type != FILTER_ARG_BOOLEAN)
1559 case FILTER_TRIVIAL_FALSE:
1560 return !filter_type->filter->boolean.value;
1562 case FILTER_TRIVIAL_TRUE:
1563 return filter_type->filter->boolean.value;
1569 static int test_filter(struct event_format *event,
1570 struct filter_arg *arg, struct pevent_record *record);
1573 get_comm(struct event_format *event, struct pevent_record *record)
1578 pid = pevent_data_pid(event->pevent, record);
1579 comm = pevent_data_comm_from_pid(event->pevent, pid);
1583 static unsigned long long
1584 get_value(struct event_format *event,
1585 struct format_field *field, struct pevent_record *record)
1587 unsigned long long val;
1589 /* Handle our dummy "comm" field */
1590 if (field == &comm) {
1593 name = get_comm(event, record);
1594 return (unsigned long)name;
1597 pevent_read_number_field(field, record->data, &val);
1599 if (!(field->flags & FIELD_IS_SIGNED))
1602 switch (field->size) {
1610 return (long long)val;
1615 static unsigned long long
1616 get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record);
1618 static unsigned long long
1619 get_exp_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record)
1621 unsigned long long lval, rval;
1623 lval = get_arg_value(event, arg->exp.left, record);
1624 rval = get_arg_value(event, arg->exp.right, record);
1626 switch (arg->exp.type) {
1627 case FILTER_EXP_ADD:
1630 case FILTER_EXP_SUB:
1633 case FILTER_EXP_MUL:
1636 case FILTER_EXP_DIV:
1639 case FILTER_EXP_MOD:
1642 case FILTER_EXP_RSHIFT:
1643 return lval >> rval;
1645 case FILTER_EXP_LSHIFT:
1646 return lval << rval;
1648 case FILTER_EXP_AND:
1654 case FILTER_EXP_XOR:
1657 case FILTER_EXP_NOT:
1659 die("error in exp");
1664 static unsigned long long
1665 get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record)
1667 switch (arg->type) {
1668 case FILTER_ARG_FIELD:
1669 return get_value(event, arg->field.field, record);
1671 case FILTER_ARG_VALUE:
1672 if (arg->value.type != FILTER_NUMBER)
1673 die("must have number field!");
1674 return arg->value.val;
1676 case FILTER_ARG_EXP:
1677 return get_exp_value(event, arg, record);
1680 die("oops in filter");
1685 static int test_num(struct event_format *event,
1686 struct filter_arg *arg, struct pevent_record *record)
1688 unsigned long long lval, rval;
1690 lval = get_arg_value(event, arg->num.left, record);
1691 rval = get_arg_value(event, arg->num.right, record);
1693 switch (arg->num.type) {
1695 return lval == rval;
1698 return lval != rval;
1707 return lval >= rval;
1710 return lval <= rval;
1718 static const char *get_field_str(struct filter_arg *arg, struct pevent_record *record)
1720 struct event_format *event;
1721 struct pevent *pevent;
1722 unsigned long long addr;
1723 const char *val = NULL;
1726 /* If the field is not a string convert it */
1727 if (arg->str.field->flags & FIELD_IS_STRING) {
1728 val = record->data + arg->str.field->offset;
1731 * We need to copy the data since we can't be sure the field
1732 * is null terminated.
1734 if (*(val + arg->str.field->size - 1)) {
1736 memcpy(arg->str.buffer, val, arg->str.field->size);
1737 /* the buffer is already NULL terminated */
1738 val = arg->str.buffer;
1742 event = arg->str.field->event;
1743 pevent = event->pevent;
1744 addr = get_value(event, arg->str.field, record);
1746 if (arg->str.field->flags & (FIELD_IS_POINTER | FIELD_IS_LONG))
1747 /* convert to a kernel symbol */
1748 val = pevent_find_function(pevent, addr);
1751 /* just use the hex of the string name */
1752 snprintf(hex, 64, "0x%llx", addr);
1760 static int test_str(struct event_format *event,
1761 struct filter_arg *arg, struct pevent_record *record)
1765 if (arg->str.field == &comm)
1766 val = get_comm(event, record);
1768 val = get_field_str(arg, record);
1770 switch (arg->str.type) {
1771 case FILTER_CMP_MATCH:
1772 return strcmp(val, arg->str.val) == 0;
1774 case FILTER_CMP_NOT_MATCH:
1775 return strcmp(val, arg->str.val) != 0;
1777 case FILTER_CMP_REGEX:
1778 /* Returns zero on match */
1779 return !regexec(&arg->str.reg, val, 0, NULL, 0);
1781 case FILTER_CMP_NOT_REGEX:
1782 return regexec(&arg->str.reg, val, 0, NULL, 0);
1790 static int test_op(struct event_format *event,
1791 struct filter_arg *arg, struct pevent_record *record)
1793 switch (arg->op.type) {
1795 return test_filter(event, arg->op.left, record) &&
1796 test_filter(event, arg->op.right, record);
1799 return test_filter(event, arg->op.left, record) ||
1800 test_filter(event, arg->op.right, record);
1803 return !test_filter(event, arg->op.right, record);
1811 static int test_filter(struct event_format *event,
1812 struct filter_arg *arg, struct pevent_record *record)
1814 switch (arg->type) {
1815 case FILTER_ARG_BOOLEAN:
1817 return arg->boolean.value;
1820 return test_op(event, arg, record);
1822 case FILTER_ARG_NUM:
1823 return test_num(event, arg, record);
1825 case FILTER_ARG_STR:
1826 return test_str(event, arg, record);
1828 case FILTER_ARG_EXP:
1829 case FILTER_ARG_VALUE:
1830 case FILTER_ARG_FIELD:
1832 * Expressions, fields and values evaluate
1833 * to true if they return non zero
1835 return !!get_arg_value(event, arg, record);
1845 * pevent_event_filtered - return true if event has filter
1846 * @filter: filter struct with filter information
1847 * @event_id: event id to test if filter exists
1849 * Returns 1 if filter found for @event_id
1852 int pevent_event_filtered(struct event_filter *filter,
1855 struct filter_type *filter_type;
1857 if (!filter->filters)
1860 filter_type = find_filter_type(filter, event_id);
1862 return filter_type ? 1 : 0;
1866 * pevent_filter_match - test if a record matches a filter
1867 * @filter: filter struct with filter information
1868 * @record: the record to test against the filter
1871 * 1 - filter found for event and @record matches
1872 * 0 - filter found for event and @record does not match
1873 * -1 - no filter found for @record's event
1874 * -2 - if no filters exist
1876 int pevent_filter_match(struct event_filter *filter,
1877 struct pevent_record *record)
1879 struct pevent *pevent = filter->pevent;
1880 struct filter_type *filter_type;
1883 if (!filter->filters)
1886 event_id = pevent_data_type(pevent, record);
1888 filter_type = find_filter_type(filter, event_id);
1891 return FILTER_NOEXIST;
1893 return test_filter(filter_type->event, filter_type->filter, record) ?
1894 FILTER_MATCH : FILTER_MISS;
1897 static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)
1908 switch (arg->op.type) {
1916 left = arg_to_str(filter, arg->op.left);
1917 right = arg_to_str(filter, arg->op.right);
1918 if (!left || !right)
1921 /* Try to consolidate boolean values */
1922 if (strcmp(left, "TRUE") == 0)
1924 else if (strcmp(left, "FALSE") == 0)
1927 if (strcmp(right, "TRUE") == 0)
1929 else if (strcmp(right, "FALSE") == 0)
1932 if (left_val >= 0) {
1933 if ((arg->op.type == FILTER_OP_AND && !left_val) ||
1934 (arg->op.type == FILTER_OP_OR && left_val)) {
1935 /* Just return left value */
1940 if (right_val >= 0) {
1941 /* just evaluate this. */
1943 switch (arg->op.type) {
1945 val = left_val && right_val;
1948 val = left_val || right_val;
1953 str = malloc_or_die(6);
1955 strcpy(str, "TRUE");
1957 strcpy(str, "FALSE");
1961 if (right_val >= 0) {
1962 if ((arg->op.type == FILTER_OP_AND && !right_val) ||
1963 (arg->op.type == FILTER_OP_OR && right_val)) {
1964 /* Just return right value */
1969 /* The right value is meaningless */
1975 len = strlen(left) + strlen(right) + strlen(op) + 10;
1976 str = malloc_or_die(len);
1977 snprintf(str, len, "(%s) %s (%s)",
1983 right = arg_to_str(filter, arg->op.right);
1987 /* See if we can consolidate */
1988 if (strcmp(right, "TRUE") == 0)
1990 else if (strcmp(right, "FALSE") == 0)
1992 if (right_val >= 0) {
1993 /* just return the opposite */
1994 str = malloc_or_die(6);
1996 strcpy(str, "FALSE");
1998 strcpy(str, "TRUE");
2001 len = strlen(right) + strlen(op) + 3;
2002 str = malloc_or_die(len);
2003 snprintf(str, len, "%s(%s)", op, right);
2015 static char *val_to_str(struct event_filter *filter, struct filter_arg *arg)
2019 str = malloc_or_die(30);
2021 snprintf(str, 30, "%lld", arg->value.val);
2026 static char *field_to_str(struct event_filter *filter, struct filter_arg *arg)
2028 return strdup(arg->field.field->name);
2031 static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)
2039 lstr = arg_to_str(filter, arg->exp.left);
2040 rstr = arg_to_str(filter, arg->exp.right);
2044 switch (arg->exp.type) {
2045 case FILTER_EXP_ADD:
2048 case FILTER_EXP_SUB:
2051 case FILTER_EXP_MUL:
2054 case FILTER_EXP_DIV:
2057 case FILTER_EXP_MOD:
2060 case FILTER_EXP_RSHIFT:
2063 case FILTER_EXP_LSHIFT:
2066 case FILTER_EXP_AND:
2072 case FILTER_EXP_XOR:
2079 len = strlen(op) + strlen(lstr) + strlen(rstr) + 4;
2080 str = malloc_or_die(len);
2081 snprintf(str, len, "%s %s %s", lstr, op, rstr);
2089 static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)
2097 lstr = arg_to_str(filter, arg->num.left);
2098 rstr = arg_to_str(filter, arg->num.right);
2102 switch (arg->num.type) {
2126 len = strlen(lstr) + strlen(op) + strlen(rstr) + 4;
2127 str = malloc_or_die(len);
2128 sprintf(str, "%s %s %s", lstr, op, rstr);
2143 static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)
2149 switch (arg->str.type) {
2150 case FILTER_CMP_MATCH:
2153 case FILTER_CMP_NOT_MATCH:
2157 case FILTER_CMP_REGEX:
2161 case FILTER_CMP_NOT_REGEX:
2165 len = strlen(arg->str.field->name) + strlen(op) +
2166 strlen(arg->str.val) + 6;
2167 str = malloc_or_die(len);
2168 snprintf(str, len, "%s %s \"%s\"",
2169 arg->str.field->name,
2180 static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)
2184 switch (arg->type) {
2185 case FILTER_ARG_BOOLEAN:
2186 str = malloc_or_die(6);
2187 if (arg->boolean.value)
2188 strcpy(str, "TRUE");
2190 strcpy(str, "FALSE");
2194 return op_to_str(filter, arg);
2196 case FILTER_ARG_NUM:
2197 return num_to_str(filter, arg);
2199 case FILTER_ARG_STR:
2200 return str_to_str(filter, arg);
2202 case FILTER_ARG_VALUE:
2203 return val_to_str(filter, arg);
2205 case FILTER_ARG_FIELD:
2206 return field_to_str(filter, arg);
2208 case FILTER_ARG_EXP:
2209 return exp_to_str(filter, arg);
2219 * pevent_filter_make_string - return a string showing the filter
2220 * @filter: filter struct with filter information
2221 * @event_id: the event id to return the filter string with
2223 * Returns a string that displays the filter contents.
2224 * This string must be freed with free(str).
2225 * NULL is returned if no filter is found.
2228 pevent_filter_make_string(struct event_filter *filter, int event_id)
2230 struct filter_type *filter_type;
2232 if (!filter->filters)
2235 filter_type = find_filter_type(filter, event_id);
2240 return arg_to_str(filter, filter_type->filter);
2244 * pevent_filter_compare - compare two filters and return if they are the same
2245 * @filter1: Filter to compare with @filter2
2246 * @filter2: Filter to compare with @filter1
2249 * 1 if the two filters hold the same content.
2252 int pevent_filter_compare(struct event_filter *filter1, struct event_filter *filter2)
2254 struct filter_type *filter_type1;
2255 struct filter_type *filter_type2;
2260 /* Do the easy checks first */
2261 if (filter1->filters != filter2->filters)
2263 if (!filter1->filters && !filter2->filters)
2267 * Now take a look at each of the events to see if they have the same
2270 for (i = 0; i < filter1->filters; i++) {
2271 filter_type1 = &filter1->event_filters[i];
2272 filter_type2 = find_filter_type(filter2, filter_type1->event_id);
2275 if (filter_type1->filter->type != filter_type2->filter->type)
2277 switch (filter_type1->filter->type) {
2278 case FILTER_TRIVIAL_FALSE:
2279 case FILTER_TRIVIAL_TRUE:
2280 /* trivial types just need the type compared */
2285 /* The best way to compare complex filters is with strings */
2286 str1 = arg_to_str(filter1, filter_type1->filter);
2287 str2 = arg_to_str(filter2, filter_type2->filter);
2289 result = strcmp(str1, str2) != 0;
2291 /* bail out if allocation fails */
2300 if (i < filter1->filters)