1 // SPDX-License-Identifier: LGPL-2.1
3 * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
11 #include <sys/types.h>
13 #include "event-parse.h"
14 #include "event-parse-local.h"
15 #include "event-utils.h"
20 static struct tep_format_field comm
= {
24 static struct tep_format_field cpu
= {
29 struct event_list
*next
;
30 struct tep_event_format
*event
;
33 static void show_error(char *error_buf
, const char *fmt
, ...)
35 unsigned long long index
;
41 input
= tep_get_input_buf();
42 index
= tep_get_input_buf_ptr();
43 len
= input
? strlen(input
) : 0;
46 strcpy(error_buf
, input
);
47 error_buf
[len
] = '\n';
48 for (i
= 1; i
< len
&& i
< index
; i
++)
49 error_buf
[len
+i
] = ' ';
50 error_buf
[len
+ i
] = '^';
51 error_buf
[len
+ i
+ 1] = '\n';
56 vsnprintf(error_buf
+ len
, TEP_FILTER_ERROR_BUFSZ
- len
, fmt
, ap
);
60 static void free_token(char *token
)
62 tep_free_token(token
);
65 static enum tep_event_type
read_token(char **tok
)
67 enum tep_event_type type
;
72 type
= tep_read_token(&token
);
73 } while (type
== TEP_EVENT_NEWLINE
|| type
== TEP_EVENT_SPACE
);
75 /* If token is = or ! check to see if the next char is ~ */
77 (strcmp(token
, "=") == 0 || strcmp(token
, "!") == 0) &&
78 tep_peek_char() == '~') {
83 return TEP_EVENT_ERROR
;
85 sprintf(*tok
, "%c%c", *token
, '~');
87 /* Now remove the '~' from the buffer */
88 tep_read_token(&token
);
96 static int filter_cmp(const void *a
, const void *b
)
98 const struct tep_filter_type
*ea
= a
;
99 const struct tep_filter_type
*eb
= b
;
101 if (ea
->event_id
< eb
->event_id
)
104 if (ea
->event_id
> eb
->event_id
)
110 static struct tep_filter_type
*
111 find_filter_type(struct tep_event_filter
*filter
, int id
)
113 struct tep_filter_type
*filter_type
;
114 struct tep_filter_type key
;
118 filter_type
= bsearch(&key
, filter
->event_filters
,
120 sizeof(*filter
->event_filters
),
126 static struct tep_filter_type
*
127 add_filter_type(struct tep_event_filter
*filter
, int id
)
129 struct tep_filter_type
*filter_type
;
132 filter_type
= find_filter_type(filter
, id
);
136 filter_type
= realloc(filter
->event_filters
,
137 sizeof(*filter
->event_filters
) *
138 (filter
->filters
+ 1));
142 filter
->event_filters
= filter_type
;
144 for (i
= 0; i
< filter
->filters
; i
++) {
145 if (filter
->event_filters
[i
].event_id
> id
)
149 if (i
< filter
->filters
)
150 memmove(&filter
->event_filters
[i
+1],
151 &filter
->event_filters
[i
],
152 sizeof(*filter
->event_filters
) *
153 (filter
->filters
- i
));
155 filter_type
= &filter
->event_filters
[i
];
156 filter_type
->event_id
= id
;
157 filter_type
->event
= tep_find_event(filter
->pevent
, id
);
158 filter_type
->filter
= NULL
;
166 * tep_filter_alloc - create a new event filter
167 * @pevent: The pevent that this filter is associated with
169 struct tep_event_filter
*tep_filter_alloc(struct tep_handle
*pevent
)
171 struct tep_event_filter
*filter
;
173 filter
= malloc(sizeof(*filter
));
177 memset(filter
, 0, sizeof(*filter
));
178 filter
->pevent
= pevent
;
184 static struct tep_filter_arg
*allocate_arg(void)
186 return calloc(1, sizeof(struct tep_filter_arg
));
189 static void free_arg(struct tep_filter_arg
*arg
)
195 case TEP_FILTER_ARG_NONE
:
196 case TEP_FILTER_ARG_BOOLEAN
:
199 case TEP_FILTER_ARG_NUM
:
200 free_arg(arg
->num
.left
);
201 free_arg(arg
->num
.right
);
204 case TEP_FILTER_ARG_EXP
:
205 free_arg(arg
->exp
.left
);
206 free_arg(arg
->exp
.right
);
209 case TEP_FILTER_ARG_STR
:
211 regfree(&arg
->str
.reg
);
212 free(arg
->str
.buffer
);
215 case TEP_FILTER_ARG_VALUE
:
216 if (arg
->value
.type
== TEP_FILTER_STRING
||
217 arg
->value
.type
== TEP_FILTER_CHAR
)
218 free(arg
->value
.str
);
221 case TEP_FILTER_ARG_OP
:
222 free_arg(arg
->op
.left
);
223 free_arg(arg
->op
.right
);
231 static int add_event(struct event_list
**events
,
232 struct tep_event_format
*event
)
234 struct event_list
*list
;
236 list
= malloc(sizeof(*list
));
240 list
->next
= *events
;
246 static int event_match(struct tep_event_format
*event
,
247 regex_t
*sreg
, regex_t
*ereg
)
250 return !regexec(sreg
, event
->system
, 0, NULL
, 0) &&
251 !regexec(ereg
, event
->name
, 0, NULL
, 0);
254 return !regexec(ereg
, event
->system
, 0, NULL
, 0) ||
255 !regexec(ereg
, event
->name
, 0, NULL
, 0);
258 static enum tep_errno
259 find_event(struct tep_handle
*pevent
, struct event_list
**events
,
260 char *sys_name
, char *event_name
)
262 struct tep_event_format
*event
;
272 /* if no name is given, then swap sys and name */
273 event_name
= sys_name
;
277 ret
= asprintf(®
, "^%s$", event_name
);
279 return TEP_ERRNO__MEM_ALLOC_FAILED
;
281 ret
= regcomp(&ereg
, reg
, REG_ICASE
|REG_NOSUB
);
285 return TEP_ERRNO__INVALID_EVENT_NAME
;
288 ret
= asprintf(®
, "^%s$", sys_name
);
291 return TEP_ERRNO__MEM_ALLOC_FAILED
;
294 ret
= regcomp(&sreg
, reg
, REG_ICASE
|REG_NOSUB
);
298 return TEP_ERRNO__INVALID_EVENT_NAME
;
302 for (i
= 0; i
< pevent
->nr_events
; i
++) {
303 event
= pevent
->events
[i
];
304 if (event_match(event
, sys_name
? &sreg
: NULL
, &ereg
)) {
306 if (add_event(events
, event
) < 0) {
318 return TEP_ERRNO__EVENT_NOT_FOUND
;
320 return TEP_ERRNO__MEM_ALLOC_FAILED
;
325 static void free_events(struct event_list
*events
)
327 struct event_list
*event
;
331 events
= events
->next
;
336 static enum tep_errno
337 create_arg_item(struct tep_event_format
*event
, const char *token
,
338 enum tep_event_type type
, struct tep_filter_arg
**parg
, char *error_str
)
340 struct tep_format_field
*field
;
341 struct tep_filter_arg
*arg
;
343 arg
= allocate_arg();
345 show_error(error_str
, "failed to allocate filter arg");
346 return TEP_ERRNO__MEM_ALLOC_FAILED
;
351 case TEP_EVENT_SQUOTE
:
352 case TEP_EVENT_DQUOTE
:
353 arg
->type
= TEP_FILTER_ARG_VALUE
;
355 type
== TEP_EVENT_DQUOTE
? TEP_FILTER_STRING
: TEP_FILTER_CHAR
;
356 arg
->value
.str
= strdup(token
);
357 if (!arg
->value
.str
) {
359 show_error(error_str
, "failed to allocate string filter arg");
360 return TEP_ERRNO__MEM_ALLOC_FAILED
;
364 /* if it is a number, then convert it */
365 if (isdigit(token
[0])) {
366 arg
->type
= TEP_FILTER_ARG_VALUE
;
367 arg
->value
.type
= TEP_FILTER_NUMBER
;
368 arg
->value
.val
= strtoull(token
, NULL
, 0);
371 /* Consider this a field */
372 field
= tep_find_any_field(event
, token
);
374 /* If token is 'COMM' or 'CPU' then it is special */
375 if (strcmp(token
, COMM
) == 0) {
377 } else if (strcmp(token
, CPU
) == 0) {
380 /* not a field, Make it false */
381 arg
->type
= TEP_FILTER_ARG_BOOLEAN
;
382 arg
->boolean
.value
= TEP_FILTER_FALSE
;
386 arg
->type
= TEP_FILTER_ARG_FIELD
;
387 arg
->field
.field
= field
;
391 show_error(error_str
, "expected a value but found %s", token
);
392 return TEP_ERRNO__UNEXPECTED_TYPE
;
398 static struct tep_filter_arg
*
399 create_arg_op(enum tep_filter_op_type btype
)
401 struct tep_filter_arg
*arg
;
403 arg
= allocate_arg();
407 arg
->type
= TEP_FILTER_ARG_OP
;
408 arg
->op
.type
= btype
;
413 static struct tep_filter_arg
*
414 create_arg_exp(enum tep_filter_exp_type etype
)
416 struct tep_filter_arg
*arg
;
418 arg
= allocate_arg();
422 arg
->type
= TEP_FILTER_ARG_EXP
;
423 arg
->exp
.type
= etype
;
428 static struct tep_filter_arg
*
429 create_arg_cmp(enum tep_filter_cmp_type ctype
)
431 struct tep_filter_arg
*arg
;
433 arg
= allocate_arg();
437 /* Use NUM and change if necessary */
438 arg
->type
= TEP_FILTER_ARG_NUM
;
439 arg
->num
.type
= ctype
;
444 static enum tep_errno
445 add_right(struct tep_filter_arg
*op
, struct tep_filter_arg
*arg
, char *error_str
)
447 struct tep_filter_arg
*left
;
453 case TEP_FILTER_ARG_EXP
:
459 case TEP_FILTER_ARG_OP
:
465 case TEP_FILTER_ARG_NUM
:
469 * The arg must be num, str, or field
472 case TEP_FILTER_ARG_VALUE
:
473 case TEP_FILTER_ARG_FIELD
:
476 show_error(error_str
, "Illegal rvalue");
477 return TEP_ERRNO__ILLEGAL_RVALUE
;
481 * Depending on the type, we may need to
482 * convert this to a string or regex.
484 switch (arg
->value
.type
) {
485 case TEP_FILTER_CHAR
:
487 * A char should be converted to number if
488 * the string is 1 byte, and the compare
491 if (strlen(arg
->value
.str
) == 1 &&
492 op
->num
.type
!= TEP_FILTER_CMP_REGEX
&&
493 op
->num
.type
!= TEP_FILTER_CMP_NOT_REGEX
) {
494 arg
->value
.type
= TEP_FILTER_NUMBER
;
498 case TEP_FILTER_STRING
:
500 /* convert op to a string arg */
501 op_type
= op
->num
.type
;
503 str
= arg
->value
.str
;
505 /* reset the op for the new field */
506 memset(op
, 0, sizeof(*op
));
509 * If left arg was a field not found then
510 * NULL the entire op.
512 if (left
->type
== TEP_FILTER_ARG_BOOLEAN
) {
515 op
->type
= TEP_FILTER_ARG_BOOLEAN
;
516 op
->boolean
.value
= TEP_FILTER_FALSE
;
520 /* Left arg must be a field */
521 if (left
->type
!= TEP_FILTER_ARG_FIELD
) {
522 show_error(error_str
,
523 "Illegal lvalue for string comparison");
524 return TEP_ERRNO__ILLEGAL_LVALUE
;
527 /* Make sure this is a valid string compare */
529 case TEP_FILTER_CMP_EQ
:
530 op_type
= TEP_FILTER_CMP_MATCH
;
532 case TEP_FILTER_CMP_NE
:
533 op_type
= TEP_FILTER_CMP_NOT_MATCH
;
536 case TEP_FILTER_CMP_REGEX
:
537 case TEP_FILTER_CMP_NOT_REGEX
:
538 ret
= regcomp(&op
->str
.reg
, str
, REG_ICASE
|REG_NOSUB
);
540 show_error(error_str
,
541 "RegEx '%s' did not compute",
543 return TEP_ERRNO__INVALID_REGEX
;
547 show_error(error_str
,
548 "Illegal comparison for string");
549 return TEP_ERRNO__ILLEGAL_STRING_CMP
;
552 op
->type
= TEP_FILTER_ARG_STR
;
553 op
->str
.type
= op_type
;
554 op
->str
.field
= left
->field
.field
;
555 op
->str
.val
= strdup(str
);
557 show_error(error_str
, "Failed to allocate string filter");
558 return TEP_ERRNO__MEM_ALLOC_FAILED
;
561 * Need a buffer to copy data for tests
563 op
->str
.buffer
= malloc(op
->str
.field
->size
+ 1);
564 if (!op
->str
.buffer
) {
565 show_error(error_str
, "Failed to allocate string filter");
566 return TEP_ERRNO__MEM_ALLOC_FAILED
;
568 /* Null terminate this buffer */
569 op
->str
.buffer
[op
->str
.field
->size
] = 0;
571 /* We no longer have left or right args */
577 case TEP_FILTER_NUMBER
:
580 switch (op
->num
.type
) {
581 case TEP_FILTER_CMP_REGEX
:
582 case TEP_FILTER_CMP_NOT_REGEX
:
583 show_error(error_str
,
584 "Op not allowed with integers");
585 return TEP_ERRNO__ILLEGAL_INTEGER_CMP
;
591 /* numeric compare */
605 show_error(error_str
, "Syntax error");
606 return TEP_ERRNO__SYNTAX_ERROR
;
609 static struct tep_filter_arg
*
610 rotate_op_right(struct tep_filter_arg
*a
, struct tep_filter_arg
*b
)
612 struct tep_filter_arg
*arg
;
619 static enum tep_errno
add_left(struct tep_filter_arg
*op
, struct tep_filter_arg
*arg
)
622 case TEP_FILTER_ARG_EXP
:
623 if (arg
->type
== TEP_FILTER_ARG_OP
)
624 arg
= rotate_op_right(arg
, op
);
628 case TEP_FILTER_ARG_OP
:
631 case TEP_FILTER_ARG_NUM
:
632 if (arg
->type
== TEP_FILTER_ARG_OP
)
633 arg
= rotate_op_right(arg
, op
);
635 /* left arg of compares must be a field */
636 if (arg
->type
!= TEP_FILTER_ARG_FIELD
&&
637 arg
->type
!= TEP_FILTER_ARG_BOOLEAN
)
638 return TEP_ERRNO__INVALID_ARG_TYPE
;
642 return TEP_ERRNO__INVALID_ARG_TYPE
;
655 static enum op_type
process_op(const char *token
,
656 enum tep_filter_op_type
*btype
,
657 enum tep_filter_cmp_type
*ctype
,
658 enum tep_filter_exp_type
*etype
)
660 *btype
= TEP_FILTER_OP_NOT
;
661 *etype
= TEP_FILTER_EXP_NONE
;
662 *ctype
= TEP_FILTER_CMP_NONE
;
664 if (strcmp(token
, "&&") == 0)
665 *btype
= TEP_FILTER_OP_AND
;
666 else if (strcmp(token
, "||") == 0)
667 *btype
= TEP_FILTER_OP_OR
;
668 else if (strcmp(token
, "!") == 0)
671 if (*btype
!= TEP_FILTER_OP_NOT
)
674 /* Check for value expressions */
675 if (strcmp(token
, "+") == 0) {
676 *etype
= TEP_FILTER_EXP_ADD
;
677 } else if (strcmp(token
, "-") == 0) {
678 *etype
= TEP_FILTER_EXP_SUB
;
679 } else if (strcmp(token
, "*") == 0) {
680 *etype
= TEP_FILTER_EXP_MUL
;
681 } else if (strcmp(token
, "/") == 0) {
682 *etype
= TEP_FILTER_EXP_DIV
;
683 } else if (strcmp(token
, "%") == 0) {
684 *etype
= TEP_FILTER_EXP_MOD
;
685 } else if (strcmp(token
, ">>") == 0) {
686 *etype
= TEP_FILTER_EXP_RSHIFT
;
687 } else if (strcmp(token
, "<<") == 0) {
688 *etype
= TEP_FILTER_EXP_LSHIFT
;
689 } else if (strcmp(token
, "&") == 0) {
690 *etype
= TEP_FILTER_EXP_AND
;
691 } else if (strcmp(token
, "|") == 0) {
692 *etype
= TEP_FILTER_EXP_OR
;
693 } else if (strcmp(token
, "^") == 0) {
694 *etype
= TEP_FILTER_EXP_XOR
;
695 } else if (strcmp(token
, "~") == 0)
696 *etype
= TEP_FILTER_EXP_NOT
;
698 if (*etype
!= TEP_FILTER_EXP_NONE
)
701 /* Check for compares */
702 if (strcmp(token
, "==") == 0)
703 *ctype
= TEP_FILTER_CMP_EQ
;
704 else if (strcmp(token
, "!=") == 0)
705 *ctype
= TEP_FILTER_CMP_NE
;
706 else if (strcmp(token
, "<") == 0)
707 *ctype
= TEP_FILTER_CMP_LT
;
708 else if (strcmp(token
, ">") == 0)
709 *ctype
= TEP_FILTER_CMP_GT
;
710 else if (strcmp(token
, "<=") == 0)
711 *ctype
= TEP_FILTER_CMP_LE
;
712 else if (strcmp(token
, ">=") == 0)
713 *ctype
= TEP_FILTER_CMP_GE
;
714 else if (strcmp(token
, "=~") == 0)
715 *ctype
= TEP_FILTER_CMP_REGEX
;
716 else if (strcmp(token
, "!~") == 0)
717 *ctype
= TEP_FILTER_CMP_NOT_REGEX
;
724 static int check_op_done(struct tep_filter_arg
*arg
)
727 case TEP_FILTER_ARG_EXP
:
728 return arg
->exp
.right
!= NULL
;
730 case TEP_FILTER_ARG_OP
:
731 return arg
->op
.right
!= NULL
;
733 case TEP_FILTER_ARG_NUM
:
734 return arg
->num
.right
!= NULL
;
736 case TEP_FILTER_ARG_STR
:
737 /* A string conversion is always done */
740 case TEP_FILTER_ARG_BOOLEAN
:
741 /* field not found, is ok */
755 static enum tep_errno
756 reparent_op_arg(struct tep_filter_arg
*parent
, struct tep_filter_arg
*old_child
,
757 struct tep_filter_arg
*arg
, char *error_str
)
759 struct tep_filter_arg
*other_child
;
760 struct tep_filter_arg
**ptr
;
762 if (parent
->type
!= TEP_FILTER_ARG_OP
&&
763 arg
->type
!= TEP_FILTER_ARG_OP
) {
764 show_error(error_str
, "can not reparent other than OP");
765 return TEP_ERRNO__REPARENT_NOT_OP
;
768 /* Get the sibling */
769 if (old_child
->op
.right
== arg
) {
770 ptr
= &old_child
->op
.right
;
771 other_child
= old_child
->op
.left
;
772 } else if (old_child
->op
.left
== arg
) {
773 ptr
= &old_child
->op
.left
;
774 other_child
= old_child
->op
.right
;
776 show_error(error_str
, "Error in reparent op, find other child");
777 return TEP_ERRNO__REPARENT_FAILED
;
780 /* Detach arg from old_child */
784 if (parent
== old_child
) {
785 free_arg(other_child
);
787 /* Free arg without recussion */
792 if (parent
->op
.right
== old_child
)
793 ptr
= &parent
->op
.right
;
794 else if (parent
->op
.left
== old_child
)
795 ptr
= &parent
->op
.left
;
797 show_error(error_str
, "Error in reparent op");
798 return TEP_ERRNO__REPARENT_FAILED
;
807 /* Returns either filter_vals (success) or tep_errno (failfure) */
808 static int test_arg(struct tep_filter_arg
*parent
, struct tep_filter_arg
*arg
,
816 case TEP_FILTER_ARG_BOOLEAN
:
817 return FILTER_VAL_FALSE
+ arg
->boolean
.value
;
820 case TEP_FILTER_ARG_STR
:
821 case TEP_FILTER_ARG_VALUE
:
822 case TEP_FILTER_ARG_FIELD
:
823 return FILTER_VAL_NORM
;
825 case TEP_FILTER_ARG_EXP
:
826 lval
= test_arg(arg
, arg
->exp
.left
, error_str
);
827 if (lval
!= FILTER_VAL_NORM
)
829 rval
= test_arg(arg
, arg
->exp
.right
, error_str
);
830 if (rval
!= FILTER_VAL_NORM
)
832 return FILTER_VAL_NORM
;
834 case TEP_FILTER_ARG_NUM
:
835 lval
= test_arg(arg
, arg
->num
.left
, error_str
);
836 if (lval
!= FILTER_VAL_NORM
)
838 rval
= test_arg(arg
, arg
->num
.right
, error_str
);
839 if (rval
!= FILTER_VAL_NORM
)
841 return FILTER_VAL_NORM
;
843 case TEP_FILTER_ARG_OP
:
844 if (arg
->op
.type
!= TEP_FILTER_OP_NOT
) {
845 lval
= test_arg(arg
, arg
->op
.left
, error_str
);
847 case FILTER_VAL_NORM
:
849 case FILTER_VAL_TRUE
:
850 if (arg
->op
.type
== TEP_FILTER_OP_OR
)
851 return FILTER_VAL_TRUE
;
852 rval
= test_arg(arg
, arg
->op
.right
, error_str
);
853 if (rval
!= FILTER_VAL_NORM
)
856 return reparent_op_arg(parent
, arg
, arg
->op
.right
,
859 case FILTER_VAL_FALSE
:
860 if (arg
->op
.type
== TEP_FILTER_OP_AND
)
861 return FILTER_VAL_FALSE
;
862 rval
= test_arg(arg
, arg
->op
.right
, error_str
);
863 if (rval
!= FILTER_VAL_NORM
)
866 return reparent_op_arg(parent
, arg
, arg
->op
.right
,
874 rval
= test_arg(arg
, arg
->op
.right
, error_str
);
876 case FILTER_VAL_NORM
:
880 case FILTER_VAL_TRUE
:
881 if (arg
->op
.type
== TEP_FILTER_OP_OR
)
882 return FILTER_VAL_TRUE
;
883 if (arg
->op
.type
== TEP_FILTER_OP_NOT
)
884 return FILTER_VAL_FALSE
;
886 return reparent_op_arg(parent
, arg
, arg
->op
.left
,
889 case FILTER_VAL_FALSE
:
890 if (arg
->op
.type
== TEP_FILTER_OP_AND
)
891 return FILTER_VAL_FALSE
;
892 if (arg
->op
.type
== TEP_FILTER_OP_NOT
)
893 return FILTER_VAL_TRUE
;
895 return reparent_op_arg(parent
, arg
, arg
->op
.left
,
901 show_error(error_str
, "bad arg in filter tree");
902 return TEP_ERRNO__BAD_FILTER_ARG
;
904 return FILTER_VAL_NORM
;
907 /* Remove any unknown event fields */
908 static int collapse_tree(struct tep_filter_arg
*arg
,
909 struct tep_filter_arg
**arg_collapsed
, char *error_str
)
913 ret
= test_arg(arg
, arg
, error_str
);
915 case FILTER_VAL_NORM
:
918 case FILTER_VAL_TRUE
:
919 case FILTER_VAL_FALSE
:
921 arg
= allocate_arg();
923 arg
->type
= TEP_FILTER_ARG_BOOLEAN
;
924 arg
->boolean
.value
= ret
== FILTER_VAL_TRUE
;
926 show_error(error_str
, "Failed to allocate filter arg");
927 ret
= TEP_ERRNO__MEM_ALLOC_FAILED
;
932 /* test_arg() already set the error_str */
938 *arg_collapsed
= arg
;
942 static enum tep_errno
943 process_filter(struct tep_event_format
*event
, struct tep_filter_arg
**parg
,
944 char *error_str
, int not)
946 enum tep_event_type type
;
948 struct tep_filter_arg
*current_op
= NULL
;
949 struct tep_filter_arg
*current_exp
= NULL
;
950 struct tep_filter_arg
*left_item
= NULL
;
951 struct tep_filter_arg
*arg
= NULL
;
952 enum op_type op_type
;
953 enum tep_filter_op_type btype
;
954 enum tep_filter_exp_type etype
;
955 enum tep_filter_cmp_type ctype
;
962 type
= read_token(&token
);
964 case TEP_EVENT_SQUOTE
:
965 case TEP_EVENT_DQUOTE
:
967 ret
= create_arg_item(event
, token
, type
, &arg
, error_str
);
972 else if (current_exp
) {
973 ret
= add_right(current_exp
, arg
, error_str
);
977 /* Not's only one one expression */
991 case TEP_EVENT_DELIM
:
993 show_error(error_str
, "Illegal token ','");
994 ret
= TEP_ERRNO__ILLEGAL_TOKEN
;
1000 show_error(error_str
,
1001 "Open paren can not come after item");
1002 ret
= TEP_ERRNO__INVALID_PAREN
;
1006 show_error(error_str
,
1007 "Open paren can not come after expression");
1008 ret
= TEP_ERRNO__INVALID_PAREN
;
1012 ret
= process_filter(event
, &arg
, error_str
, 0);
1013 if (ret
!= TEP_ERRNO__UNBALANCED_PAREN
) {
1015 show_error(error_str
,
1016 "Unbalanced number of '('");
1017 ret
= TEP_ERRNO__UNBALANCED_PAREN
;
1023 /* A not wants just one expression */
1032 ret
= add_right(current_op
, arg
, error_str
);
1040 if (!current_op
&& !current_exp
)
1043 /* Make sure everything is finished at this level */
1044 if (current_exp
&& !check_op_done(current_exp
))
1046 if (current_op
&& !check_op_done(current_op
))
1052 *parg
= current_exp
;
1054 return TEP_ERRNO__UNBALANCED_PAREN
;
1059 op_type
= process_op(token
, &btype
, &ctype
, &etype
);
1061 /* All expect a left arg except for NOT */
1064 /* Logic ops need a left expression */
1065 if (!current_exp
&& !current_op
)
1069 /* logic only processes ops and exp */
1079 show_error(error_str
,
1080 "Unknown op token %s", token
);
1081 ret
= TEP_ERRNO__UNKNOWN_TOKEN
;
1088 arg
= create_arg_op(btype
);
1092 ret
= add_left(arg
, current_op
);
1094 ret
= add_left(arg
, current_exp
);
1100 arg
= create_arg_op(btype
);
1104 ret
= add_right(current_op
, arg
, error_str
);
1108 ret
= process_filter(event
, &arg
, error_str
, 1);
1111 ret
= add_right(current_exp
, arg
, error_str
);
1118 if (op_type
== OP_EXP
)
1119 arg
= create_arg_exp(etype
);
1121 arg
= create_arg_cmp(ctype
);
1126 ret
= add_right(current_op
, arg
, error_str
);
1129 ret
= add_left(arg
, left_item
);
1143 case TEP_EVENT_NONE
:
1145 case TEP_EVENT_ERROR
:
1150 } while (type
!= TEP_EVENT_NONE
);
1152 if (!current_op
&& !current_exp
)
1156 current_op
= current_exp
;
1158 ret
= collapse_tree(current_op
, parg
, error_str
);
1159 /* collapse_tree() may free current_op, and updates parg accordingly */
1168 show_error(error_str
, "failed to allocate filter arg");
1169 ret
= TEP_ERRNO__MEM_ALLOC_FAILED
;
1172 show_error(error_str
, "Syntax error");
1173 ret
= TEP_ERRNO__SYNTAX_ERROR
;
1175 free_arg(current_op
);
1176 free_arg(current_exp
);
1182 static enum tep_errno
1183 process_event(struct tep_event_format
*event
, const char *filter_str
,
1184 struct tep_filter_arg
**parg
, char *error_str
)
1188 tep_buffer_init(filter_str
, strlen(filter_str
));
1190 ret
= process_filter(event
, parg
, error_str
, 0);
1194 /* If parg is NULL, then make it into FALSE */
1196 *parg
= allocate_arg();
1198 return TEP_ERRNO__MEM_ALLOC_FAILED
;
1200 (*parg
)->type
= TEP_FILTER_ARG_BOOLEAN
;
1201 (*parg
)->boolean
.value
= TEP_FILTER_FALSE
;
1207 static enum tep_errno
1208 filter_event(struct tep_event_filter
*filter
, struct tep_event_format
*event
,
1209 const char *filter_str
, char *error_str
)
1211 struct tep_filter_type
*filter_type
;
1212 struct tep_filter_arg
*arg
;
1216 ret
= process_event(event
, filter_str
, &arg
, error_str
);
1221 /* just add a TRUE arg */
1222 arg
= allocate_arg();
1224 return TEP_ERRNO__MEM_ALLOC_FAILED
;
1226 arg
->type
= TEP_FILTER_ARG_BOOLEAN
;
1227 arg
->boolean
.value
= TEP_FILTER_TRUE
;
1230 filter_type
= add_filter_type(filter
, event
->id
);
1231 if (filter_type
== NULL
)
1232 return TEP_ERRNO__MEM_ALLOC_FAILED
;
1234 if (filter_type
->filter
)
1235 free_arg(filter_type
->filter
);
1236 filter_type
->filter
= arg
;
1241 static void filter_init_error_buf(struct tep_event_filter
*filter
)
1243 /* clear buffer to reset show error */
1244 tep_buffer_init("", 0);
1245 filter
->error_buffer
[0] = '\0';
1249 * tep_filter_add_filter_str - add a new filter
1250 * @filter: the event filter to add to
1251 * @filter_str: the filter string that contains the filter
1253 * Returns 0 if the filter was successfully added or a
1254 * negative error code. Use tep_filter_strerror() to see
1255 * actual error message in case of error.
1257 enum tep_errno
tep_filter_add_filter_str(struct tep_event_filter
*filter
,
1258 const char *filter_str
)
1260 struct tep_handle
*pevent
= filter
->pevent
;
1261 struct event_list
*event
;
1262 struct event_list
*events
= NULL
;
1263 const char *filter_start
;
1264 const char *next_event
;
1266 char *event_name
= NULL
;
1267 char *sys_name
= NULL
;
1269 enum tep_errno rtn
= 0; /* TEP_ERRNO__SUCCESS */
1273 filter_init_error_buf(filter
);
1275 filter_start
= strchr(filter_str
, ':');
1277 len
= filter_start
- filter_str
;
1279 len
= strlen(filter_str
);
1282 next_event
= strchr(filter_str
, ',');
1284 (!filter_start
|| next_event
< filter_start
))
1285 len
= next_event
- filter_str
;
1286 else if (filter_start
)
1287 len
= filter_start
- filter_str
;
1289 len
= strlen(filter_str
);
1291 this_event
= malloc(len
+ 1);
1292 if (this_event
== NULL
) {
1293 /* This can only happen when events is NULL, but still */
1294 free_events(events
);
1295 return TEP_ERRNO__MEM_ALLOC_FAILED
;
1297 memcpy(this_event
, filter_str
, len
);
1298 this_event
[len
] = 0;
1303 filter_str
= next_event
;
1305 sys_name
= strtok_r(this_event
, "/", &sp
);
1306 event_name
= strtok_r(NULL
, "/", &sp
);
1309 /* This can only happen when events is NULL, but still */
1310 free_events(events
);
1312 return TEP_ERRNO__FILTER_NOT_FOUND
;
1315 /* Find this event */
1316 ret
= find_event(pevent
, &events
, strim(sys_name
), strim(event_name
));
1318 free_events(events
);
1323 } while (filter_str
);
1329 /* filter starts here */
1330 for (event
= events
; event
; event
= event
->next
) {
1331 ret
= filter_event(filter
, event
->event
, filter_start
,
1332 filter
->error_buffer
);
1333 /* Failures are returned if a parse error happened */
1337 if (ret
>= 0 && pevent
->test_filters
) {
1339 test
= tep_filter_make_string(filter
, event
->event
->id
);
1341 printf(" '%s: %s'\n", event
->event
->name
, test
);
1347 free_events(events
);
1349 if (rtn
>= 0 && pevent
->test_filters
)
1355 static void free_filter_type(struct tep_filter_type
*filter_type
)
1357 free_arg(filter_type
->filter
);
1361 * tep_filter_strerror - fill error message in a buffer
1362 * @filter: the event filter contains error
1363 * @err: the error code
1364 * @buf: the buffer to be filled in
1365 * @buflen: the size of the buffer
1367 * Returns 0 if message was filled successfully, -1 if error
1369 int tep_filter_strerror(struct tep_event_filter
*filter
, enum tep_errno err
,
1370 char *buf
, size_t buflen
)
1372 if (err
<= __TEP_ERRNO__START
|| err
>= __TEP_ERRNO__END
)
1375 if (strlen(filter
->error_buffer
) > 0) {
1376 size_t len
= snprintf(buf
, buflen
, "%s", filter
->error_buffer
);
1383 return tep_strerror(filter
->pevent
, err
, buf
, buflen
);
1387 * tep_filter_remove_event - remove a filter for an event
1388 * @filter: the event filter to remove from
1389 * @event_id: the event to remove a filter for
1391 * Removes the filter saved for an event defined by @event_id
1394 * Returns 1: if an event was removed
1395 * 0: if the event was not found
1397 int tep_filter_remove_event(struct tep_event_filter
*filter
,
1400 struct tep_filter_type
*filter_type
;
1403 if (!filter
->filters
)
1406 filter_type
= find_filter_type(filter
, event_id
);
1411 free_filter_type(filter_type
);
1413 /* The filter_type points into the event_filters array */
1414 len
= (unsigned long)(filter
->event_filters
+ filter
->filters
) -
1415 (unsigned long)(filter_type
+ 1);
1417 memmove(filter_type
, filter_type
+ 1, len
);
1420 memset(&filter
->event_filters
[filter
->filters
], 0,
1421 sizeof(*filter_type
));
1427 * tep_filter_reset - clear all filters in a filter
1428 * @filter: the event filter to reset
1430 * Removes all filters from a filter and resets it.
1432 void tep_filter_reset(struct tep_event_filter
*filter
)
1436 for (i
= 0; i
< filter
->filters
; i
++)
1437 free_filter_type(&filter
->event_filters
[i
]);
1439 free(filter
->event_filters
);
1440 filter
->filters
= 0;
1441 filter
->event_filters
= NULL
;
1444 void tep_filter_free(struct tep_event_filter
*filter
)
1446 tep_unref(filter
->pevent
);
1448 tep_filter_reset(filter
);
1453 static char *arg_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
);
1455 static int copy_filter_type(struct tep_event_filter
*filter
,
1456 struct tep_event_filter
*source
,
1457 struct tep_filter_type
*filter_type
)
1459 struct tep_filter_arg
*arg
;
1460 struct tep_event_format
*event
;
1465 /* Can't assume that the pevent's are the same */
1466 sys
= filter_type
->event
->system
;
1467 name
= filter_type
->event
->name
;
1468 event
= tep_find_event_by_name(filter
->pevent
, sys
, name
);
1472 str
= arg_to_str(source
, filter_type
->filter
);
1476 if (strcmp(str
, "TRUE") == 0 || strcmp(str
, "FALSE") == 0) {
1477 /* Add trivial event */
1478 arg
= allocate_arg();
1482 arg
->type
= TEP_FILTER_ARG_BOOLEAN
;
1483 if (strcmp(str
, "TRUE") == 0)
1484 arg
->boolean
.value
= 1;
1486 arg
->boolean
.value
= 0;
1488 filter_type
= add_filter_type(filter
, event
->id
);
1489 if (filter_type
== NULL
)
1492 filter_type
->filter
= arg
;
1498 filter_event(filter
, event
, str
, NULL
);
1505 * tep_filter_copy - copy a filter using another filter
1506 * @dest - the filter to copy to
1507 * @source - the filter to copy from
1509 * Returns 0 on success and -1 if not all filters were copied
1511 int tep_filter_copy(struct tep_event_filter
*dest
, struct tep_event_filter
*source
)
1516 tep_filter_reset(dest
);
1518 for (i
= 0; i
< source
->filters
; i
++) {
1519 if (copy_filter_type(dest
, source
, &source
->event_filters
[i
]))
1527 * tep_update_trivial - update the trivial filters with the given filter
1528 * @dest - the filter to update
1529 * @source - the filter as the source of the update
1530 * @type - the type of trivial filter to update.
1532 * Scan dest for trivial events matching @type to replace with the source.
1534 * Returns 0 on success and -1 if there was a problem updating, but
1535 * events may have still been updated on error.
1537 int tep_update_trivial(struct tep_event_filter
*dest
, struct tep_event_filter
*source
,
1538 enum tep_filter_trivial_type type
)
1540 struct tep_handle
*src_pevent
;
1541 struct tep_handle
*dest_pevent
;
1542 struct tep_event_format
*event
;
1543 struct tep_filter_type
*filter_type
;
1544 struct tep_filter_arg
*arg
;
1548 src_pevent
= source
->pevent
;
1549 dest_pevent
= dest
->pevent
;
1551 /* Do nothing if either of the filters has nothing to filter */
1552 if (!dest
->filters
|| !source
->filters
)
1555 for (i
= 0; i
< dest
->filters
; i
++) {
1556 filter_type
= &dest
->event_filters
[i
];
1557 arg
= filter_type
->filter
;
1558 if (arg
->type
!= TEP_FILTER_ARG_BOOLEAN
)
1560 if ((arg
->boolean
.value
&& type
== TEP_FILTER_TRIVIAL_FALSE
) ||
1561 (!arg
->boolean
.value
&& type
== TEP_FILTER_TRIVIAL_TRUE
))
1564 event
= filter_type
->event
;
1566 if (src_pevent
!= dest_pevent
) {
1568 event
= tep_find_event_by_name(src_pevent
,
1575 str
= tep_filter_make_string(source
, event
->id
);
1579 /* Don't bother if the filter is trivial too */
1580 if (strcmp(str
, "TRUE") != 0 && strcmp(str
, "FALSE") != 0)
1581 filter_event(dest
, event
, str
, NULL
);
1588 * tep_filter_clear_trivial - clear TRUE and FALSE filters
1589 * @filter: the filter to remove trivial filters from
1590 * @type: remove only true, false, or both
1592 * Removes filters that only contain a TRUE or FALES boolean arg.
1594 * Returns 0 on success and -1 if there was a problem.
1596 int tep_filter_clear_trivial(struct tep_event_filter
*filter
,
1597 enum tep_filter_trivial_type type
)
1599 struct tep_filter_type
*filter_type
;
1604 if (!filter
->filters
)
1608 * Two steps, first get all ids with trivial filters.
1609 * then remove those ids.
1611 for (i
= 0; i
< filter
->filters
; i
++) {
1614 filter_type
= &filter
->event_filters
[i
];
1615 if (filter_type
->filter
->type
!= TEP_FILTER_ARG_BOOLEAN
)
1618 case TEP_FILTER_TRIVIAL_FALSE
:
1619 if (filter_type
->filter
->boolean
.value
)
1622 case TEP_FILTER_TRIVIAL_TRUE
:
1623 if (!filter_type
->filter
->boolean
.value
)
1629 new_ids
= realloc(ids
, sizeof(*ids
) * (count
+ 1));
1636 ids
[count
++] = filter_type
->event_id
;
1642 for (i
= 0; i
< count
; i
++)
1643 tep_filter_remove_event(filter
, ids
[i
]);
1650 * tep_filter_event_has_trivial - return true event contains trivial filter
1651 * @filter: the filter with the information
1652 * @event_id: the id of the event to test
1653 * @type: trivial type to test for (TRUE, FALSE, EITHER)
1655 * Returns 1 if the event contains a matching trivial type
1658 int tep_filter_event_has_trivial(struct tep_event_filter
*filter
,
1660 enum tep_filter_trivial_type type
)
1662 struct tep_filter_type
*filter_type
;
1664 if (!filter
->filters
)
1667 filter_type
= find_filter_type(filter
, event_id
);
1672 if (filter_type
->filter
->type
!= TEP_FILTER_ARG_BOOLEAN
)
1676 case TEP_FILTER_TRIVIAL_FALSE
:
1677 return !filter_type
->filter
->boolean
.value
;
1679 case TEP_FILTER_TRIVIAL_TRUE
:
1680 return filter_type
->filter
->boolean
.value
;
1686 static int test_filter(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1687 struct tep_record
*record
, enum tep_errno
*err
);
1690 get_comm(struct tep_event_format
*event
, struct tep_record
*record
)
1695 pid
= tep_data_pid(event
->pevent
, record
);
1696 comm
= tep_data_comm_from_pid(event
->pevent
, pid
);
1700 static unsigned long long
1701 get_value(struct tep_event_format
*event
,
1702 struct tep_format_field
*field
, struct tep_record
*record
)
1704 unsigned long long val
;
1706 /* Handle our dummy "comm" field */
1707 if (field
== &comm
) {
1710 name
= get_comm(event
, record
);
1711 return (unsigned long)name
;
1714 /* Handle our dummy "cpu" field */
1718 tep_read_number_field(field
, record
->data
, &val
);
1720 if (!(field
->flags
& TEP_FIELD_IS_SIGNED
))
1723 switch (field
->size
) {
1731 return (long long)val
;
1736 static unsigned long long
1737 get_arg_value(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1738 struct tep_record
*record
, enum tep_errno
*err
);
1740 static unsigned long long
1741 get_exp_value(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1742 struct tep_record
*record
, enum tep_errno
*err
)
1744 unsigned long long lval
, rval
;
1746 lval
= get_arg_value(event
, arg
->exp
.left
, record
, err
);
1747 rval
= get_arg_value(event
, arg
->exp
.right
, record
, err
);
1751 * There was an error, no need to process anymore.
1756 switch (arg
->exp
.type
) {
1757 case TEP_FILTER_EXP_ADD
:
1760 case TEP_FILTER_EXP_SUB
:
1763 case TEP_FILTER_EXP_MUL
:
1766 case TEP_FILTER_EXP_DIV
:
1769 case TEP_FILTER_EXP_MOD
:
1772 case TEP_FILTER_EXP_RSHIFT
:
1773 return lval
>> rval
;
1775 case TEP_FILTER_EXP_LSHIFT
:
1776 return lval
<< rval
;
1778 case TEP_FILTER_EXP_AND
:
1781 case TEP_FILTER_EXP_OR
:
1784 case TEP_FILTER_EXP_XOR
:
1787 case TEP_FILTER_EXP_NOT
:
1790 *err
= TEP_ERRNO__INVALID_EXP_TYPE
;
1795 static unsigned long long
1796 get_arg_value(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1797 struct tep_record
*record
, enum tep_errno
*err
)
1799 switch (arg
->type
) {
1800 case TEP_FILTER_ARG_FIELD
:
1801 return get_value(event
, arg
->field
.field
, record
);
1803 case TEP_FILTER_ARG_VALUE
:
1804 if (arg
->value
.type
!= TEP_FILTER_NUMBER
) {
1806 *err
= TEP_ERRNO__NOT_A_NUMBER
;
1808 return arg
->value
.val
;
1810 case TEP_FILTER_ARG_EXP
:
1811 return get_exp_value(event
, arg
, record
, err
);
1815 *err
= TEP_ERRNO__INVALID_ARG_TYPE
;
1820 static int test_num(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1821 struct tep_record
*record
, enum tep_errno
*err
)
1823 unsigned long long lval
, rval
;
1825 lval
= get_arg_value(event
, arg
->num
.left
, record
, err
);
1826 rval
= get_arg_value(event
, arg
->num
.right
, record
, err
);
1830 * There was an error, no need to process anymore.
1835 switch (arg
->num
.type
) {
1836 case TEP_FILTER_CMP_EQ
:
1837 return lval
== rval
;
1839 case TEP_FILTER_CMP_NE
:
1840 return lval
!= rval
;
1842 case TEP_FILTER_CMP_GT
:
1845 case TEP_FILTER_CMP_LT
:
1848 case TEP_FILTER_CMP_GE
:
1849 return lval
>= rval
;
1851 case TEP_FILTER_CMP_LE
:
1852 return lval
<= rval
;
1856 *err
= TEP_ERRNO__ILLEGAL_INTEGER_CMP
;
1861 static const char *get_field_str(struct tep_filter_arg
*arg
, struct tep_record
*record
)
1863 struct tep_event_format
*event
;
1864 struct tep_handle
*pevent
;
1865 unsigned long long addr
;
1866 const char *val
= NULL
;
1870 /* If the field is not a string convert it */
1871 if (arg
->str
.field
->flags
& TEP_FIELD_IS_STRING
) {
1872 val
= record
->data
+ arg
->str
.field
->offset
;
1873 size
= arg
->str
.field
->size
;
1875 if (arg
->str
.field
->flags
& TEP_FIELD_IS_DYNAMIC
) {
1876 addr
= *(unsigned int *)val
;
1877 val
= record
->data
+ (addr
& 0xffff);
1882 * We need to copy the data since we can't be sure the field
1883 * is null terminated.
1885 if (*(val
+ size
- 1)) {
1887 memcpy(arg
->str
.buffer
, val
, arg
->str
.field
->size
);
1888 /* the buffer is already NULL terminated */
1889 val
= arg
->str
.buffer
;
1893 event
= arg
->str
.field
->event
;
1894 pevent
= event
->pevent
;
1895 addr
= get_value(event
, arg
->str
.field
, record
);
1897 if (arg
->str
.field
->flags
& (TEP_FIELD_IS_POINTER
| TEP_FIELD_IS_LONG
))
1898 /* convert to a kernel symbol */
1899 val
= tep_find_function(pevent
, addr
);
1902 /* just use the hex of the string name */
1903 snprintf(hex
, 64, "0x%llx", addr
);
1911 static int test_str(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1912 struct tep_record
*record
, enum tep_errno
*err
)
1916 if (arg
->str
.field
== &comm
)
1917 val
= get_comm(event
, record
);
1919 val
= get_field_str(arg
, record
);
1921 switch (arg
->str
.type
) {
1922 case TEP_FILTER_CMP_MATCH
:
1923 return strcmp(val
, arg
->str
.val
) == 0;
1925 case TEP_FILTER_CMP_NOT_MATCH
:
1926 return strcmp(val
, arg
->str
.val
) != 0;
1928 case TEP_FILTER_CMP_REGEX
:
1929 /* Returns zero on match */
1930 return !regexec(&arg
->str
.reg
, val
, 0, NULL
, 0);
1932 case TEP_FILTER_CMP_NOT_REGEX
:
1933 return regexec(&arg
->str
.reg
, val
, 0, NULL
, 0);
1937 *err
= TEP_ERRNO__ILLEGAL_STRING_CMP
;
1942 static int test_op(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1943 struct tep_record
*record
, enum tep_errno
*err
)
1945 switch (arg
->op
.type
) {
1946 case TEP_FILTER_OP_AND
:
1947 return test_filter(event
, arg
->op
.left
, record
, err
) &&
1948 test_filter(event
, arg
->op
.right
, record
, err
);
1950 case TEP_FILTER_OP_OR
:
1951 return test_filter(event
, arg
->op
.left
, record
, err
) ||
1952 test_filter(event
, arg
->op
.right
, record
, err
);
1954 case TEP_FILTER_OP_NOT
:
1955 return !test_filter(event
, arg
->op
.right
, record
, err
);
1959 *err
= TEP_ERRNO__INVALID_OP_TYPE
;
1964 static int test_filter(struct tep_event_format
*event
, struct tep_filter_arg
*arg
,
1965 struct tep_record
*record
, enum tep_errno
*err
)
1969 * There was an error, no need to process anymore.
1974 switch (arg
->type
) {
1975 case TEP_FILTER_ARG_BOOLEAN
:
1977 return arg
->boolean
.value
;
1979 case TEP_FILTER_ARG_OP
:
1980 return test_op(event
, arg
, record
, err
);
1982 case TEP_FILTER_ARG_NUM
:
1983 return test_num(event
, arg
, record
, err
);
1985 case TEP_FILTER_ARG_STR
:
1986 return test_str(event
, arg
, record
, err
);
1988 case TEP_FILTER_ARG_EXP
:
1989 case TEP_FILTER_ARG_VALUE
:
1990 case TEP_FILTER_ARG_FIELD
:
1992 * Expressions, fields and values evaluate
1993 * to true if they return non zero
1995 return !!get_arg_value(event
, arg
, record
, err
);
1999 *err
= TEP_ERRNO__INVALID_ARG_TYPE
;
2005 * tep_event_filtered - return true if event has filter
2006 * @filter: filter struct with filter information
2007 * @event_id: event id to test if filter exists
2009 * Returns 1 if filter found for @event_id
2012 int tep_event_filtered(struct tep_event_filter
*filter
, int event_id
)
2014 struct tep_filter_type
*filter_type
;
2016 if (!filter
->filters
)
2019 filter_type
= find_filter_type(filter
, event_id
);
2021 return filter_type
? 1 : 0;
2025 * tep_filter_match - test if a record matches a filter
2026 * @filter: filter struct with filter information
2027 * @record: the record to test against the filter
2029 * Returns: match result or error code (prefixed with TEP_ERRNO__)
2030 * FILTER_MATCH - filter found for event and @record matches
2031 * FILTER_MISS - filter found for event and @record does not match
2032 * FILTER_NOT_FOUND - no filter found for @record's event
2033 * NO_FILTER - if no filters exist
2034 * otherwise - error occurred during test
2036 enum tep_errno
tep_filter_match(struct tep_event_filter
*filter
,
2037 struct tep_record
*record
)
2039 struct tep_handle
*pevent
= filter
->pevent
;
2040 struct tep_filter_type
*filter_type
;
2043 enum tep_errno err
= 0;
2045 filter_init_error_buf(filter
);
2047 if (!filter
->filters
)
2048 return TEP_ERRNO__NO_FILTER
;
2050 event_id
= tep_data_type(pevent
, record
);
2052 filter_type
= find_filter_type(filter
, event_id
);
2054 return TEP_ERRNO__FILTER_NOT_FOUND
;
2056 ret
= test_filter(filter_type
->event
, filter_type
->filter
, record
, &err
);
2060 return ret
? TEP_ERRNO__FILTER_MATCH
: TEP_ERRNO__FILTER_MISS
;
2063 static char *op_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2073 switch (arg
->op
.type
) {
2074 case TEP_FILTER_OP_AND
:
2077 case TEP_FILTER_OP_OR
:
2081 left
= arg_to_str(filter
, arg
->op
.left
);
2082 right
= arg_to_str(filter
, arg
->op
.right
);
2083 if (!left
|| !right
)
2086 /* Try to consolidate boolean values */
2087 if (strcmp(left
, "TRUE") == 0)
2089 else if (strcmp(left
, "FALSE") == 0)
2092 if (strcmp(right
, "TRUE") == 0)
2094 else if (strcmp(right
, "FALSE") == 0)
2097 if (left_val
>= 0) {
2098 if ((arg
->op
.type
== TEP_FILTER_OP_AND
&& !left_val
) ||
2099 (arg
->op
.type
== TEP_FILTER_OP_OR
&& left_val
)) {
2100 /* Just return left value */
2105 if (right_val
>= 0) {
2106 /* just evaluate this. */
2108 switch (arg
->op
.type
) {
2109 case TEP_FILTER_OP_AND
:
2110 val
= left_val
&& right_val
;
2112 case TEP_FILTER_OP_OR
:
2113 val
= left_val
|| right_val
;
2118 asprintf(&str
, val
? "TRUE" : "FALSE");
2122 if (right_val
>= 0) {
2123 if ((arg
->op
.type
== TEP_FILTER_OP_AND
&& !right_val
) ||
2124 (arg
->op
.type
== TEP_FILTER_OP_OR
&& right_val
)) {
2125 /* Just return right value */
2130 /* The right value is meaningless */
2136 asprintf(&str
, "(%s) %s (%s)", left
, op
, right
);
2139 case TEP_FILTER_OP_NOT
:
2141 right
= arg_to_str(filter
, arg
->op
.right
);
2145 /* See if we can consolidate */
2146 if (strcmp(right
, "TRUE") == 0)
2148 else if (strcmp(right
, "FALSE") == 0)
2150 if (right_val
>= 0) {
2151 /* just return the opposite */
2152 asprintf(&str
, right_val
? "FALSE" : "TRUE");
2155 asprintf(&str
, "%s(%s)", op
, right
);
2167 static char *val_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2171 asprintf(&str
, "%lld", arg
->value
.val
);
2176 static char *field_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2178 return strdup(arg
->field
.field
->name
);
2181 static char *exp_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2188 lstr
= arg_to_str(filter
, arg
->exp
.left
);
2189 rstr
= arg_to_str(filter
, arg
->exp
.right
);
2193 switch (arg
->exp
.type
) {
2194 case TEP_FILTER_EXP_ADD
:
2197 case TEP_FILTER_EXP_SUB
:
2200 case TEP_FILTER_EXP_MUL
:
2203 case TEP_FILTER_EXP_DIV
:
2206 case TEP_FILTER_EXP_MOD
:
2209 case TEP_FILTER_EXP_RSHIFT
:
2212 case TEP_FILTER_EXP_LSHIFT
:
2215 case TEP_FILTER_EXP_AND
:
2218 case TEP_FILTER_EXP_OR
:
2221 case TEP_FILTER_EXP_XOR
:
2225 op
= "[ERROR IN EXPRESSION TYPE]";
2229 asprintf(&str
, "%s %s %s", lstr
, op
, rstr
);
2237 static char *num_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2244 lstr
= arg_to_str(filter
, arg
->num
.left
);
2245 rstr
= arg_to_str(filter
, arg
->num
.right
);
2249 switch (arg
->num
.type
) {
2250 case TEP_FILTER_CMP_EQ
:
2253 case TEP_FILTER_CMP_NE
:
2257 case TEP_FILTER_CMP_GT
:
2261 case TEP_FILTER_CMP_LT
:
2265 case TEP_FILTER_CMP_GE
:
2269 case TEP_FILTER_CMP_LE
:
2273 asprintf(&str
, "%s %s %s", lstr
, op
, rstr
);
2287 static char *str_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2292 switch (arg
->str
.type
) {
2293 case TEP_FILTER_CMP_MATCH
:
2296 case TEP_FILTER_CMP_NOT_MATCH
:
2300 case TEP_FILTER_CMP_REGEX
:
2304 case TEP_FILTER_CMP_NOT_REGEX
:
2308 asprintf(&str
, "%s %s \"%s\"",
2309 arg
->str
.field
->name
, op
, arg
->str
.val
);
2319 static char *arg_to_str(struct tep_event_filter
*filter
, struct tep_filter_arg
*arg
)
2323 switch (arg
->type
) {
2324 case TEP_FILTER_ARG_BOOLEAN
:
2325 asprintf(&str
, arg
->boolean
.value
? "TRUE" : "FALSE");
2328 case TEP_FILTER_ARG_OP
:
2329 return op_to_str(filter
, arg
);
2331 case TEP_FILTER_ARG_NUM
:
2332 return num_to_str(filter
, arg
);
2334 case TEP_FILTER_ARG_STR
:
2335 return str_to_str(filter
, arg
);
2337 case TEP_FILTER_ARG_VALUE
:
2338 return val_to_str(filter
, arg
);
2340 case TEP_FILTER_ARG_FIELD
:
2341 return field_to_str(filter
, arg
);
2343 case TEP_FILTER_ARG_EXP
:
2344 return exp_to_str(filter
, arg
);
2354 * tep_filter_make_string - return a string showing the filter
2355 * @filter: filter struct with filter information
2356 * @event_id: the event id to return the filter string with
2358 * Returns a string that displays the filter contents.
2359 * This string must be freed with free(str).
2360 * NULL is returned if no filter is found or allocation failed.
2363 tep_filter_make_string(struct tep_event_filter
*filter
, int event_id
)
2365 struct tep_filter_type
*filter_type
;
2367 if (!filter
->filters
)
2370 filter_type
= find_filter_type(filter
, event_id
);
2375 return arg_to_str(filter
, filter_type
->filter
);
2379 * tep_filter_compare - compare two filters and return if they are the same
2380 * @filter1: Filter to compare with @filter2
2381 * @filter2: Filter to compare with @filter1
2384 * 1 if the two filters hold the same content.
2387 int tep_filter_compare(struct tep_event_filter
*filter1
, struct tep_event_filter
*filter2
)
2389 struct tep_filter_type
*filter_type1
;
2390 struct tep_filter_type
*filter_type2
;
2395 /* Do the easy checks first */
2396 if (filter1
->filters
!= filter2
->filters
)
2398 if (!filter1
->filters
&& !filter2
->filters
)
2402 * Now take a look at each of the events to see if they have the same
2405 for (i
= 0; i
< filter1
->filters
; i
++) {
2406 filter_type1
= &filter1
->event_filters
[i
];
2407 filter_type2
= find_filter_type(filter2
, filter_type1
->event_id
);
2410 if (filter_type1
->filter
->type
!= filter_type2
->filter
->type
)
2412 switch (filter_type1
->filter
->type
) {
2413 case TEP_FILTER_TRIVIAL_FALSE
:
2414 case TEP_FILTER_TRIVIAL_TRUE
:
2415 /* trivial types just need the type compared */
2420 /* The best way to compare complex filters is with strings */
2421 str1
= arg_to_str(filter1
, filter_type1
->filter
);
2422 str2
= arg_to_str(filter2
, filter_type2
->filter
);
2424 result
= strcmp(str1
, str2
) != 0;
2426 /* bail out if allocation fails */
2435 if (i
< filter1
->filters
)