1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 /*! \file lttv/lttv/filter.c
20 * \brief Defines the core filter of application
22 * consist in AND, OR and NOT nested expressions, forming a tree with
23 * simple relations as leaves. The simple relations test if a field
24 * in an event is equal, not equal, smaller, smaller or equal, larger, or
25 * larger or equal to a specified value.
27 * Fields specified in a simple expression can take following
31 * LttvTracefileContext{}
33 * | |->name (String, converted to GQuark)
34 * | |->category (String, not yet implemented)
36 * | |->tsc (LttCycleCount --> uint64)
40 * | |->"sub-field name"
42 * | |->"leaf-field name" (field type)
44 * | |->name (String, converted to GQuark)
46 * | |->name (String, converted to GQuark)
50 * |->creation_time (LttTime)
51 * |->insertion_time (LttTime)
52 * |->process_name (String, converted to GQuark)
53 * |->execution_mode (LttvExecutionMode)
54 * |->execution_submode (LttvExecutionSubmode)
55 * |->process_status (LttvProcessStatus)
62 * - refine switch of expression in multiple uses functions
63 * - remove the idle expressions in the tree
76 #include <lttv/filter.h>
77 #include <ltt/trace.h>
83 * @fn LttvSimpleExpression* lttv_simple_expression_new()
85 * Constructor for LttvSimpleExpression
86 * @return pointer to new LttvSimpleExpression
89 lttv_simple_expression_new() {
91 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
93 se
->field
= LTTV_FILTER_UNDEFINED
;
101 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
103 * Parse through filtering field hierarchy as specified
104 * by user. This function compares each value to
105 * predetermined quarks
106 * @param fp The field path list
107 * @param se current simple expression
108 * @return success/failure of operation
111 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
115 if(fp
->len
< 2) return FALSE
;
116 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
119 * Parse through the specified
122 * Take note however that the
123 * 'event' subfields might change
124 * depending on values specified
125 * in core.xml file. Hence, if
126 * none of the subfields in the
127 * array match the hardcoded
128 * subfields, it will be considered
131 if(!g_strcasecmp(f
->str
,"trace") ) {
136 g_string_free(f
,TRUE
);
137 f
=g_ptr_array_remove_index(fp
,0);
138 if(!g_strcasecmp(f
->str
,"name")) {
139 se
->field
= LTTV_FILTER_TRACE_NAME
;
141 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
143 * FIXME: not yet implemented !
145 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
150 g_string_free(f
,TRUE
);
151 f
=g_ptr_array_remove_index(fp
,0);
152 if(!g_strcasecmp(f
->str
,"name")) {
153 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
155 } else if(!g_strcasecmp(f
->str
,"state") ) {
160 * state.creation_time
161 * state.insertion_time
163 * state.execution_mode
164 * state.execution_submode
165 * state.process_status
168 g_string_free(f
,TRUE
);
169 f
=g_ptr_array_remove_index(fp
,0);
170 if(!g_strcasecmp(f
->str
,"pid") ) {
171 se
->field
= LTTV_FILTER_STATE_PID
;
173 else if(!g_strcasecmp(f
->str
,"ppid") ) {
174 se
->field
= LTTV_FILTER_STATE_PPID
;
176 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
177 se
->field
= LTTV_FILTER_STATE_CT
;
179 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
180 se
->field
= LTTV_FILTER_STATE_IT
;
182 else if(!g_strcasecmp(f
->str
,"process_name") ) {
183 se
->field
= LTTV_FILTER_STATE_P_NAME
;
185 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
186 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
188 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
189 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
191 else if(!g_strcasecmp(f
->str
,"process_status") ) {
192 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
194 else if(!g_strcasecmp(f
->str
,"cpu") ) {
195 se
->field
= LTTV_FILTER_STATE_CPU
;
197 } else if(!g_strcasecmp(f
->str
,"event") ) {
205 g_string_free(f
,TRUE
);
206 f
=g_ptr_array_remove_index(fp
,0);
207 if(!g_strcasecmp(f
->str
,"name") ) {
208 se
->field
= LTTV_FILTER_EVENT_NAME
;
210 else if(!g_strcasecmp(f
->str
,"category") ) {
212 * FIXME: Category not yet functional in lttv
214 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
216 else if(!g_strcasecmp(f
->str
,"time") ) {
217 se
->field
= LTTV_FILTER_EVENT_TIME
;
219 else if(!g_strcasecmp(f
->str
,"tsc") ) {
220 se
->field
= LTTV_FILTER_EVENT_TSC
;
222 else { /* core.xml specified options */
223 se
->field
= LTTV_FILTER_EVENT_FIELD
;
226 g_warning("Unrecognized field in filter string");
229 /* free memory for last string */
230 g_string_free(f
,TRUE
);
232 /* array should be empty */
233 g_assert(fp
->len
== 0);
235 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
236 g_warning("The specified field was not recognized !");
243 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
245 * Sets the function pointer for the current
247 * @param se current simple expression
248 * @param op current operator
249 * @return success/failure of operation
252 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
258 case LTTV_FILTER_TRACE_NAME
:
259 case LTTV_FILTER_TRACEFILE_NAME
:
260 case LTTV_FILTER_STATE_P_NAME
:
261 case LTTV_FILTER_EVENT_NAME
:
262 case LTTV_FILTER_STATE_EX_MODE
:
263 case LTTV_FILTER_STATE_EX_SUBMODE
:
264 case LTTV_FILTER_STATE_P_STATUS
:
267 se
->op
= lttv_apply_op_eq_quark
;
270 se
->op
= lttv_apply_op_ne_quark
;
273 g_warning("Error encountered in operator assignment = or != expected");
280 case LTTV_FILTER_EVENT_TSC
:
283 se
->op
= lttv_apply_op_eq_uint64
;
286 se
->op
= lttv_apply_op_ne_uint64
;
289 se
->op
= lttv_apply_op_lt_uint64
;
292 se
->op
= lttv_apply_op_le_uint64
;
295 se
->op
= lttv_apply_op_gt_uint64
;
298 se
->op
= lttv_apply_op_ge_uint64
;
301 g_warning("Error encountered in operator assignment");
308 case LTTV_FILTER_STATE_CPU
:
309 case LTTV_FILTER_STATE_PID
:
310 case LTTV_FILTER_STATE_PPID
:
313 se
->op
= lttv_apply_op_eq_uint
;
316 se
->op
= lttv_apply_op_ne_uint
;
319 se
->op
= lttv_apply_op_lt_uint
;
322 se
->op
= lttv_apply_op_le_uint
;
325 se
->op
= lttv_apply_op_gt_uint
;
328 se
->op
= lttv_apply_op_ge_uint
;
331 g_warning("Error encountered in operator assignment");
338 * Entered as string, converted to enum
340 * can only be compared with 'equal' or 'not equal' operators
342 * unsigned int of 16 bits are used here since enums
343 * should not go over 2^16-1 values
347 // case LTTV_FIELD_EQ:
348 // se->op = lttv_apply_op_eq_uint16;
350 // case LTTV_FIELD_NE:
351 // se->op = lttv_apply_op_ne_uint16;
354 // g_warning("Error encountered in operator assignment = or != expected");
361 case LTTV_FILTER_STATE_CT
:
362 case LTTV_FILTER_STATE_IT
:
363 case LTTV_FILTER_EVENT_TIME
:
366 se
->op
= lttv_apply_op_eq_ltttime
;
369 se
->op
= lttv_apply_op_ne_ltttime
;
372 se
->op
= lttv_apply_op_lt_ltttime
;
375 se
->op
= lttv_apply_op_le_ltttime
;
378 se
->op
= lttv_apply_op_gt_ltttime
;
381 se
->op
= lttv_apply_op_ge_ltttime
;
384 g_warning("Error encountered in operator assignment");
389 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
398 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
400 * Assign the value field to the current LttvSimpleExpression
401 * @param se pointer to the current LttvSimpleExpression
402 * @param value string value for simple expression
405 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
408 gboolean is_double
= FALSE
;
409 LttTime t
= ltt_time_zero
;
415 * entered as strings, converted to Quarks
417 case LTTV_FILTER_TRACE_NAME
:
418 case LTTV_FILTER_TRACEFILE_NAME
:
419 case LTTV_FILTER_STATE_P_NAME
:
420 case LTTV_FILTER_EVENT_NAME
:
421 case LTTV_FILTER_STATE_EX_MODE
:
422 case LTTV_FILTER_STATE_EX_SUBMODE
:
423 case LTTV_FILTER_STATE_P_STATUS
:
424 // se->value.v_string = value;
425 se
->value
.v_uint32
= g_quark_from_string(value
);
429 * integer -- supposed to be uint64
431 case LTTV_FILTER_EVENT_TSC
:
432 se
->value
.v_uint64
= atoi(value
);
438 case LTTV_FILTER_STATE_PID
:
439 case LTTV_FILTER_STATE_PPID
:
440 case LTTV_FILTER_STATE_CPU
:
441 se
->value
.v_uint
= atoi(value
);
447 case LTTV_FILTER_STATE_CT
:
448 case LTTV_FILTER_STATE_IT
:
449 case LTTV_FILTER_EVENT_TIME
:
450 //se->value.v_double = atof(value);
452 * parsing logic could be optimised,
453 * but as for now, simpler this way
455 v
= g_string_new("");
456 for(i
=0;i
<strlen(value
);i
++) {
457 if(value
[i
] == '.') {
458 /* cannot specify number with more than one '.' */
459 if(is_double
) return FALSE
;
460 else is_double
= TRUE
;
461 t
.tv_sec
= atoi(v
->str
);
462 g_string_free(v
,TRUE
);
463 v
= g_string_new("");
464 } else g_string_append_c(v
,value
[i
]);
466 /* number can be integer or double */
467 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
468 else t
.tv_sec
= atoi(v
->str
);
470 g_string_free(v
,TRUE
);
472 se
->value
.v_ltttime
= t
;
476 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
486 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
488 * Disallocate memory for the current
490 * @param se pointer to the current LttvSimpleExpression
493 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
495 // g_free(se->value);
496 // switch(se->field) {
497 // case LTTV_FILTER_TRACE_NAME:
498 // case LTTV_FILTER_TRACEFILE_NAME:
499 // case LTTV_FILTER_STATE_P_NAME:
500 // case LTTV_FILTER_EVENT_NAME:
501 // g_free(se->value.v_string);
509 * @fn gint lttv_struct_type(gint)
511 * Finds the structure type depending
512 * on the fields in parameters
513 * @params ft Field of the current structure
514 * @return LttvStructType enum or -1 for error
517 lttv_struct_type(gint ft
) {
520 case LTTV_FILTER_TRACE_NAME
:
521 return LTTV_FILTER_TRACE
;
523 case LTTV_FILTER_TRACEFILE_NAME
:
524 return LTTV_FILTER_TRACEFILE
;
526 case LTTV_FILTER_STATE_PID
:
527 case LTTV_FILTER_STATE_PPID
:
528 case LTTV_FILTER_STATE_CT
:
529 case LTTV_FILTER_STATE_IT
:
530 case LTTV_FILTER_STATE_P_NAME
:
531 case LTTV_FILTER_STATE_EX_MODE
:
532 case LTTV_FILTER_STATE_EX_SUBMODE
:
533 case LTTV_FILTER_STATE_P_STATUS
:
534 case LTTV_FILTER_STATE_CPU
:
535 return LTTV_FILTER_STATE
;
537 case LTTV_FILTER_EVENT_NAME
:
538 case LTTV_FILTER_EVENT_CATEGORY
:
539 case LTTV_FILTER_EVENT_TIME
:
540 case LTTV_FILTER_EVENT_TSC
:
541 case LTTV_FILTER_EVENT_FIELD
:
542 return LTTV_FILTER_EVENT
;
550 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
552 * Applies the 'equal' operator to the
553 * specified structure and value
554 * @param v1 left member of comparison
555 * @param v2 right member of comparison
556 * @return success/failure of operation
558 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
560 guint
* r
= (guint
*) v1
;
561 return (*r
== v2
.v_uint
);
566 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
568 * Applies the 'equal' operator to the
569 * specified structure and value
570 * @param v1 left member of comparison
571 * @param v2 right member of comparison
572 * @return success/failure of operation
574 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
576 guint64
* r
= (guint64
*) v1
;
577 return (*r
== v2
.v_uint64
);
582 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
584 * Applies the 'equal' operator to the
585 * specified structure and value
586 * @param v1 left member of comparison
587 * @param v2 right member of comparison
588 * @return success/failure of operation
590 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
591 guint32
* r
= (guint32
*) v1
;
592 return (*r
== v2
.v_uint32
);
596 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
598 * Applies the 'equal' operator to the
599 * specified structure and value
600 * @param v1 left member of comparison
601 * @param v2 right member of comparison
602 * @return success/failure of operation
604 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
605 guint16
* r
= (guint16
*) v1
;
606 return (*r
== v2
.v_uint16
);
610 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
612 * Applies the 'equal' operator to the
613 * specified structure and value
614 * @param v1 left member of comparison
615 * @param v2 right member of comparison
616 * @return success/failure of operation
618 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
619 double* r
= (double*) v1
;
620 return (*r
== v2
.v_double
);
624 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
626 * Applies the 'equal' operator to the
627 * specified structure and value
628 * @param v1 left member of comparison
629 * @param v2 right member of comparison
630 * @return success/failure of operation
632 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
633 char* r
= (char*) v1
;
634 return (!g_strcasecmp(r
,v2
.v_string
));
638 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
640 * Applies the 'equal' operator to the
641 * specified structure and value
642 * @param v1 left member of comparison
643 * @param v2 right member of comparison
644 * @return success/failure of operation
646 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
647 GQuark
* r
= (GQuark
*) v1
;
648 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
649 return (*r
== v2
.v_uint32
);
653 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
655 * Applies the 'equal' operator to the
656 * specified structure and value
657 * @param v1 left member of comparison
658 * @param v2 right member of comparison
659 * @return success/failure of operation
661 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
662 LttTime
* r
= (LttTime
*) v1
;
663 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
667 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
669 * Applies the 'not equal' operator to the
670 * specified structure and value
671 * @param v1 left member of comparison
672 * @param v2 right member of comparison
673 * @return success/failure of operation
675 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
676 guint
* r
= (guint
*) v1
;
677 return (*r
!= v2
.v_uint
);
681 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
683 * Applies the 'not equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
689 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
690 guint64
* r
= (guint64
*) v1
;
691 return (*r
!= v2
.v_uint64
);
695 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
697 * Applies the 'not equal' operator to the
698 * specified structure and value
699 * @param v1 left member of comparison
700 * @param v2 right member of comparison
701 * @return success/failure of operation
703 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
704 guint32
* r
= (guint32
*) v1
;
705 return (*r
!= v2
.v_uint32
);
709 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
711 * Applies the 'not equal' operator to the
712 * specified structure and value
713 * @param v1 left member of comparison
714 * @param v2 right member of comparison
715 * @return success/failure of operation
717 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
718 guint16
* r
= (guint16
*) v1
;
719 return (*r
!= v2
.v_uint16
);
723 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
725 * Applies the 'not equal' operator to the
726 * specified structure and value
727 * @param v1 left member of comparison
728 * @param v2 right member of comparison
729 * @return success/failure of operation
731 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
732 double* r
= (double*) v1
;
733 return (*r
!= v2
.v_double
);
737 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
739 * Applies the 'not equal' operator to the
740 * specified structure and value
741 * @param v1 left member of comparison
742 * @param v2 right member of comparison
743 * @return success/failure of operation
745 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
746 char* r
= (char*) v1
;
747 return (g_strcasecmp(r
,v2
.v_string
));
751 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
753 * Applies the 'not equal' operator to the
754 * specified structure and value
755 * @param v1 left member of comparison
756 * @param v2 right member of comparison
757 * @return success/failure of operation
759 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
760 GQuark
* r
= (GQuark
*) v1
;
761 return (*r
!= v2
.v_uint32
);
766 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
768 * Applies the 'not equal' operator to the
769 * specified structure and value
770 * @param v1 left member of comparison
771 * @param v2 right member of comparison
772 * @return success/failure of operation
774 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
775 LttTime
* r
= (LttTime
*) v1
;
776 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
780 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
782 * Applies the 'lower than' operator to the
783 * specified structure and value
784 * @param v1 left member of comparison
785 * @param v2 right member of comparison
786 * @return success/failure of operation
788 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
789 guint
* r
= (guint
*) v1
;
790 return (*r
< v2
.v_uint
);
794 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
796 * Applies the 'lower than' operator to the
797 * specified structure and value
798 * @param v1 left member of comparison
799 * @param v2 right member of comparison
800 * @return success/failure of operation
802 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
803 guint64
* r
= (guint64
*) v1
;
804 return (*r
< v2
.v_uint64
);
808 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
810 * Applies the 'lower than' operator to the
811 * specified structure and value
812 * @param v1 left member of comparison
813 * @param v2 right member of comparison
814 * @return success/failure of operation
816 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
817 guint32
* r
= (guint32
*) v1
;
818 return (*r
< v2
.v_uint32
);
822 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
824 * Applies the 'lower than' operator to the
825 * specified structure and value
826 * @param v1 left member of comparison
827 * @param v2 right member of comparison
828 * @return success/failure of operation
830 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
831 guint16
* r
= (guint16
*) v1
;
832 return (*r
< v2
.v_uint16
);
836 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
838 * Applies the 'lower than' operator to the
839 * specified structure and value
840 * @param v1 left member of comparison
841 * @param v2 right member of comparison
842 * @return success/failure of operation
844 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
845 double* r
= (double*) v1
;
846 return (*r
< v2
.v_double
);
850 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
852 * Applies the 'lower than' operator to the
853 * specified structure and value
854 * @param v1 left member of comparison
855 * @param v2 right member of comparison
856 * @return success/failure of operation
858 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
859 LttTime
* r
= (LttTime
*) v1
;
860 // return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec < v2.v_ltttime.tv_nsec)));
861 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
865 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
867 * Applies the 'lower or equal' operator to the
868 * specified structure and value
869 * @param v1 left member of comparison
870 * @param v2 right member of comparison
871 * @return success/failure of operation
873 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
874 guint
* r
= (guint
*) v1
;
875 return (*r
<= v2
.v_uint
);
879 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
881 * Applies the 'lower or equal' operator to the
882 * specified structure and value
883 * @param v1 left member of comparison
884 * @param v2 right member of comparison
885 * @return success/failure of operation
887 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
888 guint64
* r
= (guint64
*) v1
;
889 return (*r
<= v2
.v_uint64
);
893 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
895 * Applies the 'lower or equal' operator to the
896 * specified structure and value
897 * @param v1 left member of comparison
898 * @param v2 right member of comparison
899 * @return success/failure of operation
901 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
902 guint32
* r
= (guint32
*) v1
;
903 return (*r
<= v2
.v_uint32
);
907 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
909 * Applies the 'lower or equal' operator to the
910 * specified structure and value
911 * @param v1 left member of comparison
912 * @param v2 right member of comparison
913 * @return success/failure of operation
915 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
916 guint16
* r
= (guint16
*) v1
;
917 return (*r
<= v2
.v_uint16
);
921 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
923 * Applies the 'lower or equal' operator to the
924 * specified structure and value
925 * @param v1 left member of comparison
926 * @param v2 right member of comparison
927 * @return success/failure of operation
929 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
930 double* r
= (double*) v1
;
931 return (*r
<= v2
.v_double
);
935 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
937 * Applies the 'lower or equal' operator to the
938 * specified structure and value
939 * @param v1 left member of comparison
940 * @param v2 right member of comparison
941 * @return success/failure of operation
943 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
944 LttTime
* r
= (LttTime
*) v1
;
945 // return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec <= v2.v_ltttime.tv_nsec)));
946 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
951 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
953 * Applies the 'greater than' operator to the
954 * specified structure and value
955 * @param v1 left member of comparison
956 * @param v2 right member of comparison
957 * @return success/failure of operation
959 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
960 guint
* r
= (guint
*) v1
;
961 return (*r
> v2
.v_uint
);
965 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
967 * Applies the 'greater than' operator to the
968 * specified structure and value
969 * @param v1 left member of comparison
970 * @param v2 right member of comparison
971 * @return success/failure of operation
973 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
974 guint64
* r
= (guint64
*) v1
;
975 return (*r
> v2
.v_uint64
);
979 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
981 * Applies the 'greater than' operator to the
982 * specified structure and value
983 * @param v1 left member of comparison
984 * @param v2 right member of comparison
985 * @return success/failure of operation
987 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
988 guint32
* r
= (guint32
*) v1
;
989 return (*r
> v2
.v_uint32
);
993 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
995 * Applies the 'greater than' operator to the
996 * specified structure and value
997 * @param v1 left member of comparison
998 * @param v2 right member of comparison
999 * @return success/failure of operation
1001 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1002 guint16
* r
= (guint16
*) v1
;
1003 return (*r
> v2
.v_uint16
);
1007 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1009 * Applies the 'greater than' operator to the
1010 * specified structure and value
1011 * @param v1 left member of comparison
1012 * @param v2 right member of comparison
1013 * @return success/failure of operation
1015 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1016 double* r
= (double*) v1
;
1017 return (*r
> v2
.v_double
);
1021 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1023 * Applies the 'greater than' operator to the
1024 * specified structure and value
1025 * @param v1 left member of comparison
1026 * @param v2 right member of comparison
1027 * @return success/failure of operation
1029 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1030 LttTime
* r
= (LttTime
*) v1
;
1031 // return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec > v2.v_ltttime.tv_nsec)));
1032 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1036 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1038 * Applies the 'greater or equal' operator to the
1039 * specified structure and value
1040 * @param v1 left member of comparison
1041 * @param v2 right member of comparison
1042 * @return success/failure of operation
1044 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1045 guint
* r
= (guint
*) v1
;
1046 return (*r
>= v2
.v_uint
);
1050 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1052 * Applies the 'greater or equal' operator to the
1053 * specified structure and value
1054 * @param v1 left member of comparison
1055 * @param v2 right member of comparison
1056 * @return success/failure of operation
1058 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1059 guint64
* r
= (guint64
*) v1
;
1060 return (*r
>= v2
.v_uint64
);
1064 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1066 * Applies the 'greater or equal' operator to the
1067 * specified structure and value
1068 * @param v1 left member of comparison
1069 * @param v2 right member of comparison
1070 * @return success/failure of operation
1072 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1073 guint32
* r
= (guint32
*) v1
;
1074 return (*r
>= v2
.v_uint32
);
1078 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1080 * Applies the 'greater or equal' operator to the
1081 * specified structure and value
1082 * @param v1 left member of comparison
1083 * @param v2 right member of comparison
1084 * @return success/failure of operation
1086 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1087 guint16
* r
= (guint16
*) v1
;
1088 return (*r
>= v2
.v_uint16
);
1092 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1094 * Applies the 'greater or equal' operator to the
1095 * specified structure and value
1096 * @param v1 left member of comparison
1097 * @param v2 right member of comparison
1098 * @return success/failure of operation
1100 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1101 double* r
= (double*) v1
;
1102 return (*r
>= v2
.v_double
);
1106 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1108 * Applies the 'greater or equal' operator to the
1109 * specified structure and value
1110 * @param v1 left member of comparison
1111 * @param v2 right member of comparison
1112 * @return success/failure of operation
1114 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1115 LttTime
* r
= (LttTime
*) v1
;
1116 // return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec >= v2.v_ltttime.tv_nsec)));
1117 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1123 * Makes a copy of the current filter tree
1124 * @param tree pointer to the current tree
1125 * @return new copy of the filter tree
1128 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1130 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1132 newtree
->node
= tree
->node
;
1134 newtree
->left
= tree
->left
;
1135 if(newtree
->left
== LTTV_TREE_NODE
) {
1136 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1137 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1138 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1139 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1140 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1141 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1142 /* FIXME: special case for string copy ! */
1143 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1146 newtree
->right
= tree
->right
;
1147 if(newtree
->right
== LTTV_TREE_NODE
) {
1148 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1149 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1150 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1151 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1152 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1153 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1154 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1162 * Makes a copy of the current filter
1163 * @param filter pointer to the current filter
1164 * @return new copy of the filter
1167 lttv_filter_clone(const LttvFilter
* filter
) {
1169 if(!filter
) return NULL
;
1171 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1173 strcpy(newfilter
->expression
,filter
->expression
);
1175 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1183 * @fn LttvFilter* lttv_filter_new()
1185 * Creates a new LttvFilter
1186 * @return the current LttvFilter or NULL if error
1191 LttvFilter
* filter
= g_new(LttvFilter
,1);
1192 filter
->expression
= NULL
;
1193 filter
->head
= NULL
;
1200 * @fn gboolean lttv_filter_update(LttvFilter*)
1202 * Updates the current LttvFilter by building
1203 * its tree based upon the expression string
1204 * @param filter pointer to the current LttvFilter
1205 * @return Failure/Success of operation
1208 lttv_filter_update(LttvFilter
* filter
) {
1210 // g_print("filter::lttv_filter_new()\n"); /* debug */
1212 if(filter
->expression
== NULL
) return FALSE
;
1216 p_nesting
=0, /* parenthesis nesting value */
1221 *tree
= lttv_filter_tree_new(), /* main tree */
1222 *subtree
= NULL
, /* buffer for subtrees */
1223 *t1
, /* buffer #1 */
1224 *t2
, /* buffer #2 */
1225 *t3
; /* buffer #3 */
1229 * If the tree already exists,
1230 * destroy it and build a new one
1232 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1233 filter
->head
= NULL
; /* will be assigned at the end */
1237 * each element of the list
1238 * is a sub tree created
1239 * by the use of parenthesis in the
1240 * global expression. The final tree
1241 * will be the one left at the root of
1244 GPtrArray
*tree_stack
= g_ptr_array_new();
1245 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1247 /* temporary values */
1248 GString
*a_field_component
= g_string_new("");
1249 GPtrArray
*a_field_path
= g_ptr_array_new();
1251 /* simple expression buffer */
1252 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1255 * Parse entire expression and construct
1256 * the binary tree. There are two steps
1257 * in browsing that string
1258 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1259 * 2. finding simple expressions
1260 * - field path ( separated by dots )
1261 * - op ( >, <, =, >=, <=, !=)
1262 * - value ( integer, string ... )
1263 * To spare computing time, the whole
1264 * string is parsed in this loop for a
1265 * O(n) complexity order.
1267 * When encountering logical op &,|,^
1268 * 1. parse the last value if any
1269 * 2. create a new tree
1270 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1271 * 4. concatenate this tree with the current tree on top of the stack
1272 * When encountering math ops >,>=,<,<=,=,!=
1273 * 1. add to op to the simple expression
1274 * 2. concatenate last field component to field path
1275 * When encountering concatening ops .
1276 * 1. concatenate last field component to field path
1277 * When encountering opening parenthesis (,{,[
1278 * 1. create a new subtree on top of tree stack
1279 * When encountering closing parenthesis ),},]
1280 * 1. add the expression on right child of the current tree
1281 * 2. the subtree is completed, allocate a new subtree
1282 * 3. pop the tree value from the tree stack
1286 struct timeval starttime
;
1287 struct timeval endtime
;
1288 gettimeofday(&starttime
, NULL
);
1291 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1293 // g_print("%c\n ",filter->expression[i]);
1295 switch(filter
->expression
[i
]) {
1301 /* get current tree in tree stack */
1302 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1304 /* get current node at absolute right */
1305 while(t1
->right
!= LTTV_TREE_IDLE
) {
1306 g_assert(t1
->right
== LTTV_TREE_NODE
);
1309 t2
= lttv_filter_tree_new();
1310 t2
->node
= LTTV_LOGICAL_AND
;
1311 t1
->right
= LTTV_TREE_NODE
;
1313 if(not) { /* add not operator to tree */
1314 t3
= lttv_filter_tree_new();
1315 t3
->node
= LTTV_LOGICAL_NOT
;
1316 t2
->left
= LTTV_TREE_NODE
;
1321 if(subtree
!= NULL
) { /* append subtree to current tree */
1322 t2
->left
= LTTV_TREE_NODE
;
1323 t2
->l_child
.t
= subtree
;
1325 } else { /* append a simple expression */
1326 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1327 a_field_component
= g_string_new("");
1328 t2
->left
= LTTV_TREE_LEAF
;
1329 t2
->l_child
.leaf
= a_simple_expression
;
1330 a_simple_expression
= lttv_simple_expression_new();
1336 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1337 while(t1
->right
!= LTTV_TREE_IDLE
) {
1338 g_assert(t1
->right
== LTTV_TREE_NODE
);
1341 t2
= lttv_filter_tree_new();
1342 t2
->node
= LTTV_LOGICAL_OR
;
1343 t1
->right
= LTTV_TREE_NODE
;
1345 if(not) { // add not operator to tree
1346 t3
= lttv_filter_tree_new();
1347 t3
->node
= LTTV_LOGICAL_NOT
;
1348 t2
->left
= LTTV_TREE_NODE
;
1353 if(subtree
!= NULL
) { /* append subtree to current tree */
1354 t2
->left
= LTTV_TREE_NODE
;
1355 t2
->l_child
.t
= subtree
;
1357 } else { /* append a simple expression */
1358 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1359 a_field_component
= g_string_new("");
1360 t2
->left
= LTTV_TREE_LEAF
;
1361 t2
->l_child
.leaf
= a_simple_expression
;
1362 a_simple_expression
= lttv_simple_expression_new();
1368 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1369 while(t1
->right
!= LTTV_TREE_IDLE
) {
1370 g_assert(t1
->right
== LTTV_TREE_NODE
);
1373 t2
= lttv_filter_tree_new();
1374 t2
->node
= LTTV_LOGICAL_XOR
;
1375 t1
->right
= LTTV_TREE_NODE
;
1377 if(not) { // add not operator to tree
1378 t3
= lttv_filter_tree_new();
1379 t3
->node
= LTTV_LOGICAL_NOT
;
1380 t2
->left
= LTTV_TREE_NODE
;
1385 if(subtree
!= NULL
) { /* append subtree to current tree */
1386 t2
->left
= LTTV_TREE_NODE
;
1387 t2
->l_child
.t
= subtree
;
1389 } else { /* append a simple expression */
1390 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1391 a_field_component
= g_string_new("");
1392 t2
->left
= LTTV_TREE_LEAF
;
1393 t2
->l_child
.leaf
= a_simple_expression
;
1394 a_simple_expression
= lttv_simple_expression_new();
1398 case '!': /* not, or not equal (math op) */
1400 if(filter
->expression
[i
+1] == '=') { /* != */
1401 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1402 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1403 a_field_component
= g_string_new("");
1404 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1411 case '(': /* start of parenthesis */
1415 p_nesting
++; /* incrementing parenthesis nesting value */
1416 t1
= lttv_filter_tree_new();
1417 if(not) { /* add not operator to tree */
1418 t3
= lttv_filter_tree_new();
1419 t3
->node
= LTTV_LOGICAL_NOT
;
1420 t1
->right
= LTTV_TREE_NODE
;
1424 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1427 case ')': /* end of parenthesis */
1431 p_nesting
--; /* decrementing parenthesis nesting value */
1432 if(p_nesting
<0 || tree_stack
->len
<2) {
1433 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1434 is not valid due to parenthesis incorrect use",filter
->expression
);
1438 /* there must at least be the root tree left in the array */
1439 g_assert(tree_stack
->len
>0);
1441 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1442 while(t1
->right
!= LTTV_TREE_IDLE
) {
1445 if(not) { // add not operator to tree
1447 t3
= lttv_filter_tree_new();
1448 t3
->node
= LTTV_LOGICAL_NOT
;
1449 t1
->right
= LTTV_TREE_NODE
;
1454 if(subtree
!= NULL
) { /* append subtree to current tree */
1455 t1
->right
= LTTV_TREE_NODE
;
1456 t1
->r_child
.t
= subtree
;
1457 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1458 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1459 } else { /* assign subtree as current tree */
1460 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1461 a_field_component
= g_string_new("");
1462 t1
->right
= LTTV_TREE_LEAF
;
1463 t1
->r_child
.leaf
= a_simple_expression
;
1464 a_simple_expression
= lttv_simple_expression_new();
1465 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1470 * mathematic operators
1472 case '<': /* lower, lower or equal */
1474 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1475 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1476 a_field_component
= g_string_new("");
1477 if(filter
->expression
[i
+1] == '=') { /* <= */
1479 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1480 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1483 case '>': /* higher, higher or equal */
1485 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1486 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1487 a_field_component
= g_string_new("");
1488 if(filter
->expression
[i
+1] == '=') { /* >= */
1490 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1491 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1494 case '=': /* equal */
1496 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1497 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1498 a_field_component
= g_string_new("");
1499 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1503 * Field concatening caracter
1508 * divide field expression into elements
1509 * in a_field_path array.
1511 * A dot can also be present in double values
1513 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1514 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1515 a_field_component
= g_string_new("");
1518 case ' ': /* ignore */
1519 case '\n': /* ignore */
1521 default: /* concatening current string */
1522 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1527 * Preliminary check to see
1528 * if tree was constructed correctly
1531 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1532 is not valid due to parenthesis incorrect use",filter
->expression
);
1536 if(tree_stack
->len
!= 1) /* only root tree should remain */
1540 * processing last element of expression
1542 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1543 while(t1
->right
!= LTTV_TREE_IDLE
) {
1544 g_assert(t1
->right
== LTTV_TREE_NODE
);
1547 if(not) { // add not operator to tree
1548 t3
= lttv_filter_tree_new();
1549 t3
->node
= LTTV_LOGICAL_NOT
;
1550 t1
->right
= LTTV_TREE_NODE
;
1555 if(subtree
!= NULL
) { /* add the subtree */
1556 t1
->right
= LTTV_TREE_NODE
;
1557 t1
->r_child
.t
= subtree
;
1559 } else { /* add a leaf */
1560 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1561 a_field_component
= NULL
;
1562 t1
->right
= LTTV_TREE_LEAF
;
1563 t1
->r_child
.leaf
= a_simple_expression
;
1564 a_simple_expression
= NULL
;
1568 /* free the pointer array */
1569 g_assert(a_field_path
->len
== 0);
1570 g_ptr_array_free(a_field_path
,TRUE
);
1572 /* free the tree stack -- but keep the root tree */
1573 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1574 g_ptr_array_free(tree_stack
,TRUE
);
1576 /* free the field buffer if allocated */
1577 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1579 /* free the simple expression buffer if allocated */
1580 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1582 g_assert(filter
->head
!= NULL
); /* tree should exist */
1583 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1586 gettimeofday(&endtime
, NULL
);
1588 /* Calcul du temps de l'algorithme */
1589 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1590 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1591 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1592 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1596 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1597 lttv_print_tree(filter
->head
,0) ;
1598 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1606 * @fn void lttv_filter_destroy(LttvFilter*)
1608 * Destroy the current LttvFilter
1609 * @param filter pointer to the current LttvFilter
1612 lttv_filter_destroy(LttvFilter
* filter
) {
1616 if(filter
->expression
)
1617 g_free(filter
->expression
);
1619 lttv_filter_tree_destroy(filter
->head
);
1625 * @fn LttvFilterTree* lttv_filter_tree_new()
1627 * Assign a new tree for the current expression
1629 * @return pointer of LttvFilterTree
1632 lttv_filter_tree_new() {
1633 LttvFilterTree
* tree
;
1635 tree
= g_new(LttvFilterTree
,1);
1636 tree
->node
= 0; //g_new(lttv_expression,1);
1637 tree
->left
= LTTV_TREE_IDLE
;
1638 tree
->right
= LTTV_TREE_IDLE
;
1639 tree
->r_child
.t
= NULL
;
1640 tree
->l_child
.t
= NULL
;
1646 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1648 * Append a new expression to the expression
1649 * defined in the current filter
1650 * @param filter pointer to the current LttvFilter
1651 * @param expression string that must be appended
1652 * @return Success/Failure of operation
1655 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1657 if(expression
== NULL
) return FALSE
;
1658 if(filter
== NULL
) return FALSE
;
1659 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1661 GString
* s
= g_string_new("");
1662 if(filter
->expression
!= NULL
) {
1663 g_string_append(s
,filter
->expression
);
1664 g_string_append_c(s
,'&');
1666 g_string_append(s
,expression
);
1668 g_free(filter
->expression
);
1669 filter
->expression
= g_string_free(s
,FALSE
);
1671 /* TRUE if construction of tree proceeded without errors */
1672 return lttv_filter_update(filter
);
1677 * @fn void lttv_filter_clear_expression(LttvFilter*)
1679 * Clear the filter expression from the
1680 * current filter and sets its pointer to NULL
1681 * @param filter pointer to the current LttvFilter
1684 lttv_filter_clear_expression(LttvFilter
* filter
) {
1686 if(filter
->expression
!= NULL
) {
1687 g_free(filter
->expression
);
1688 filter
->expression
= NULL
;
1694 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1696 * Destroys the tree and his sub-trees
1697 * @param tree Tree which must be destroyed
1700 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1702 if(tree
== NULL
) return;
1704 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1705 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1707 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1708 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1710 // g_free(tree->node);
1715 * Global parsing function for the current
1717 * @param t pointer to the current LttvFilterTree
1718 * @param event current LttEvent, NULL if not used
1719 * @param tracefile current LttTracefile, NULL if not used
1720 * @param trace current LttTrace, NULL if not used
1721 * @param state current LttvProcessState, NULL if not used
1722 * @param context current LttvTracefileContext, NULL if not used
1723 * @return response of filter
1726 lttv_filter_tree_parse(
1727 const LttvFilterTree
* t
,
1728 const LttEvent
* event
,
1729 const LttTracefile
* tracefile
,
1730 const LttTrace
* trace
,
1731 const LttvTracefileContext
* context
1736 * Each tree is parsed in inorder.
1737 * This way, it's possible to apply the left filter of the
1738 * tree, then decide whether or not the right branch should
1739 * be parsed depending on the linking logical operator
1741 * Each node consists in a
1742 * 1. logical operator
1743 * 2. left child ( node or simple expression )
1744 * 3. right child ( node or simple expression )
1746 * When the child is a simple expression, we must
1747 * before all determine if the expression refers to
1748 * a structure which is whithin observation ( not NULL ).
1749 * -If so, the expression is evaluated.
1750 * -If not, the result is set to TRUE since this particular
1751 * operation does not interfere with the lttv structure
1753 * The result of each simple expression will directly
1754 * affect the next branch. This way, depending on
1755 * the linking logical operator, the parser will decide
1756 * to explore or not the next branch.
1758 * -If result of left branch is 0 / FALSE
1759 * then don't explore right branch and return 0;
1760 * -If result of left branch is 1 / TRUE then explore
1762 * -If result of left branch is 1 / TRUE
1763 * then don't explore right branch and return 1;
1764 * -If result of left branch is 0 / FALSE then explore
1766 * -Result of left branch will not affect exploration of
1770 gboolean lresult
= FALSE
, rresult
= FALSE
;
1772 LttvProcessState
* state
;
1774 guint cpu
= ltt_tracefile_num(context
->tf
);
1775 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1776 state
= ts
->running_process
[cpu
];
1781 if(t
->left
== LTTV_TREE_NODE
) {
1782 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1784 else if(t
->left
== LTTV_TREE_LEAF
) {
1785 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1789 * Parse linking operator
1790 * make a cutoff if possible
1792 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1793 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1796 * Parse right branch
1798 if(t
->right
== LTTV_TREE_NODE
) {
1799 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1801 else if(t
->right
== LTTV_TREE_LEAF
) {
1802 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1807 * Apply and return the
1808 * logical link between the
1812 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1813 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1814 case LTTV_LOGICAL_NOT
:
1815 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1816 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1817 case 0: return (rresult
);
1820 * This case should never be
1821 * parsed, if so, this subtree
1830 * This function parses a particular branch of the tree
1831 * @param se pointer to the current LttvSimpleExpression
1832 * @param event current LttEvent, NULL if not used
1833 * @param tracefile current LttTracefile, NULL if not used
1834 * @param trace current LttTrace, NULL if not used
1835 * @param state current LttvProcessState, NULL if not used
1836 * @param context current LttvTracefileContext, NULL if not used
1837 * @return response of filter
1840 lttv_filter_tree_parse_branch(
1841 const LttvSimpleExpression
* se
,
1842 const LttEvent
* event
,
1843 const LttTracefile
* tracefile
,
1844 const LttTrace
* trace
,
1845 const LttvProcessState
* state
,
1846 const LttvTracefileContext
* context
) {
1851 case LTTV_FILTER_TRACE_NAME
:
1852 if(trace
== NULL
) return TRUE
;
1854 GQuark quark
= ltt_trace_name(trace
);
1855 return se
->op((gpointer
)&quark
,v
);
1858 case LTTV_FILTER_TRACEFILE_NAME
:
1859 if(tracefile
== NULL
) return TRUE
;
1861 GQuark quark
= ltt_tracefile_name(tracefile
);
1862 return se
->op((gpointer
)&quark
,v
);
1865 case LTTV_FILTER_STATE_PID
:
1866 if(state
== NULL
) return TRUE
;
1867 else return se
->op((gpointer
)&state
->pid
,v
);
1869 case LTTV_FILTER_STATE_PPID
:
1870 if(state
== NULL
) return TRUE
;
1871 else return se
->op((gpointer
)&state
->ppid
,v
);
1873 case LTTV_FILTER_STATE_CT
:
1874 if(state
== NULL
) return TRUE
;
1876 return se
->op((gpointer
)&state
->creation_time
,v
);
1879 case LTTV_FILTER_STATE_IT
:
1880 if(state
== NULL
) return TRUE
;
1882 return se
->op((gpointer
)&state
->insertion_time
,v
);
1885 case LTTV_FILTER_STATE_P_NAME
:
1887 * All 'unnamed' for the moment
1889 if(state
== NULL
) return TRUE
;
1891 GQuark quark
= state
->name
;
1892 return se
->op((gpointer
)&quark
,v
);
1895 case LTTV_FILTER_STATE_EX_MODE
:
1896 if(state
== NULL
) return TRUE
;
1897 else return se
->op((gpointer
)&state
->state
->t
,v
);
1899 case LTTV_FILTER_STATE_EX_SUBMODE
:
1900 if(state
== NULL
) return TRUE
;
1901 else return se
->op((gpointer
)&state
->state
->n
,v
);
1903 case LTTV_FILTER_STATE_P_STATUS
:
1904 if(state
== NULL
) return TRUE
;
1905 else return se
->op((gpointer
)&state
->state
->s
,v
);
1907 case LTTV_FILTER_STATE_CPU
:
1908 if(context
== NULL
) return TRUE
;
1910 /* FIXME: not sure of that one Mathieu : fixed.*/
1911 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1912 // return se->op((gpointer)&quark,v);
1913 if(state
== NULL
) return TRUE
;
1914 else return se
->op((gpointer
)&state
->cpu
,v
);
1917 case LTTV_FILTER_EVENT_NAME
:
1918 if(event
== NULL
) return TRUE
;
1921 et
= ltt_event_eventtype(event
);
1922 GQuark quark
= ltt_eventtype_name(et
);
1923 return se
->op((gpointer
)&quark
,v
);
1927 case LTTV_FILTER_EVENT_CATEGORY
:
1929 * TODO: Not yet implemented
1933 case LTTV_FILTER_EVENT_TIME
:
1934 if(event
== NULL
) return TRUE
;
1936 LttTime time
= ltt_event_time(event
);
1937 return se
->op((gpointer
)&time
,v
);
1940 case LTTV_FILTER_EVENT_TSC
:
1941 if(event
== NULL
) return TRUE
;
1943 LttCycleCount count
= ltt_event_cycle_count(event
);
1944 return se
->op((gpointer
)&count
,v
);
1947 case LTTV_FILTER_EVENT_FIELD
:
1949 * TODO: Use the offset to
1950 * find the dynamic field
1951 * in the event struct
1956 * This case should never be
1957 * parsed, if so, the whole
1958 * filtering is cancelled
1960 g_warning("Error while parsing the filter tree");
1964 /* should never get here */
1972 * Debug function. Prints tree memory allocation.
1973 * @param t the pointer to the current LttvFilterTree
1976 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1978 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1979 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1980 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1982 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1983 g_print("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1984 if(t
->left
== LTTV_TREE_LEAF
) {
1985 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1986 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1987 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1989 g_print("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1990 if(t
->right
== LTTV_TREE_LEAF
) {
1991 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1992 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1993 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1996 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1997 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2001 * @fn static void module_init()
2003 * Initializes the filter module and specific values
2005 static void module_init()
2011 * Destroys the filter module and specific values
2013 static void module_destroy()
2019 LTTV_MODULE("filter", "Filters traceset and events", \
2020 "Filters traceset and events specifically to user input", \
2021 module_init
, module_destroy
)