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");
306 * 32 bits unsigned integers
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_uint32
;
316 se
->op
= lttv_apply_op_ne_uint32
;
319 se
->op
= lttv_apply_op_lt_uint32
;
322 se
->op
= lttv_apply_op_le_uint32
;
325 se
->op
= lttv_apply_op_gt_uint32
;
328 se
->op
= lttv_apply_op_ge_uint32
;
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_uint32
= 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_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
560 guint64
* r
= (guint64
*) v1
;
561 return (*r
== v2
.v_uint64
);
566 * @fn gboolean lttv_apply_op_eq_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
575 guint32
* r
= (guint32
*) v1
;
576 return (*r
== v2
.v_uint32
);
580 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
582 * Applies the 'equal' operator to the
583 * specified structure and value
584 * @param v1 left member of comparison
585 * @param v2 right member of comparison
586 * @return success/failure of operation
588 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
589 guint16
* r
= (guint16
*) v1
;
590 return (*r
== v2
.v_uint16
);
594 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
596 * Applies the 'equal' operator to the
597 * specified structure and value
598 * @param v1 left member of comparison
599 * @param v2 right member of comparison
600 * @return success/failure of operation
602 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
603 double* r
= (double*) v1
;
604 return (*r
== v2
.v_double
);
608 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
610 * Applies the 'equal' operator to the
611 * specified structure and value
612 * @param v1 left member of comparison
613 * @param v2 right member of comparison
614 * @return success/failure of operation
616 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
617 char* r
= (char*) v1
;
618 return (!g_strcasecmp(r
,v2
.v_string
));
622 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
624 * Applies the 'equal' operator to the
625 * specified structure and value
626 * @param v1 left member of comparison
627 * @param v2 right member of comparison
628 * @return success/failure of operation
630 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
631 GQuark
* r
= (GQuark
*) v1
;
632 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
633 return (*r
== v2
.v_uint32
);
637 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
639 * Applies the 'equal' operator to the
640 * specified structure and value
641 * @param v1 left member of comparison
642 * @param v2 right member of comparison
643 * @return success/failure of operation
645 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
646 LttTime
* r
= (LttTime
*) v1
;
647 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
652 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
654 * Applies the 'not equal' operator to the
655 * specified structure and value
656 * @param v1 left member of comparison
657 * @param v2 right member of comparison
658 * @return success/failure of operation
660 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
661 guint64
* r
= (guint64
*) v1
;
662 return (*r
!= v2
.v_uint64
);
666 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
668 * Applies the 'not equal' operator to the
669 * specified structure and value
670 * @param v1 left member of comparison
671 * @param v2 right member of comparison
672 * @return success/failure of operation
674 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
675 guint32
* r
= (guint32
*) v1
;
676 return (*r
!= v2
.v_uint32
);
680 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
682 * Applies the 'not equal' operator to the
683 * specified structure and value
684 * @param v1 left member of comparison
685 * @param v2 right member of comparison
686 * @return success/failure of operation
688 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
689 guint16
* r
= (guint16
*) v1
;
690 return (*r
!= v2
.v_uint16
);
694 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
696 * Applies the 'not equal' operator to the
697 * specified structure and value
698 * @param v1 left member of comparison
699 * @param v2 right member of comparison
700 * @return success/failure of operation
702 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
703 double* r
= (double*) v1
;
704 return (*r
!= v2
.v_double
);
708 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
710 * Applies the 'not equal' operator to the
711 * specified structure and value
712 * @param v1 left member of comparison
713 * @param v2 right member of comparison
714 * @return success/failure of operation
716 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
717 char* r
= (char*) v1
;
718 return (g_strcasecmp(r
,v2
.v_string
));
722 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
724 * Applies the 'not equal' operator to the
725 * specified structure and value
726 * @param v1 left member of comparison
727 * @param v2 right member of comparison
728 * @return success/failure of operation
730 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
731 GQuark
* r
= (GQuark
*) v1
;
732 return (*r
!= v2
.v_uint32
);
737 * @fn gboolean lttv_apply_op_ne_ltttime(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_ltttime(const gpointer v1
, LttvFieldValue v2
) {
746 LttTime
* r
= (LttTime
*) v1
;
747 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
752 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
754 * Applies the 'lower than' operator to the
755 * specified structure and value
756 * @param v1 left member of comparison
757 * @param v2 right member of comparison
758 * @return success/failure of operation
760 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
761 guint64
* r
= (guint64
*) v1
;
762 return (*r
< v2
.v_uint64
);
766 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
768 * Applies the 'lower than' 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_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
775 guint32
* r
= (guint32
*) v1
;
776 return (*r
< v2
.v_uint32
);
780 * @fn gboolean lttv_apply_op_lt_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
789 guint16
* r
= (guint16
*) v1
;
790 return (*r
< v2
.v_uint16
);
794 * @fn gboolean lttv_apply_op_lt_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
803 double* r
= (double*) v1
;
804 return (*r
< v2
.v_double
);
808 * @fn gboolean lttv_apply_op_lt_ltttime(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_ltttime(const gpointer v1
, LttvFieldValue v2
) {
817 LttTime
* r
= (LttTime
*) v1
;
818 // 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)));
819 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
824 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
826 * Applies the 'lower or equal' operator to the
827 * specified structure and value
828 * @param v1 left member of comparison
829 * @param v2 right member of comparison
830 * @return success/failure of operation
832 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
833 guint64
* r
= (guint64
*) v1
;
834 return (*r
<= v2
.v_uint64
);
838 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
840 * Applies the 'lower or equal' operator to the
841 * specified structure and value
842 * @param v1 left member of comparison
843 * @param v2 right member of comparison
844 * @return success/failure of operation
846 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
847 guint32
* r
= (guint32
*) v1
;
848 return (*r
<= v2
.v_uint32
);
852 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
854 * Applies the 'lower or equal' operator to the
855 * specified structure and value
856 * @param v1 left member of comparison
857 * @param v2 right member of comparison
858 * @return success/failure of operation
860 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
861 guint16
* r
= (guint16
*) v1
;
862 return (*r
<= v2
.v_uint16
);
866 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
868 * Applies the 'lower or equal' operator to the
869 * specified structure and value
870 * @param v1 left member of comparison
871 * @param v2 right member of comparison
872 * @return success/failure of operation
874 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
875 double* r
= (double*) v1
;
876 return (*r
<= v2
.v_double
);
880 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
882 * Applies the 'lower or equal' operator to the
883 * specified structure and value
884 * @param v1 left member of comparison
885 * @param v2 right member of comparison
886 * @return success/failure of operation
888 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
889 LttTime
* r
= (LttTime
*) v1
;
890 // 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)));
891 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
896 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
898 * Applies the 'greater than' operator to the
899 * specified structure and value
900 * @param v1 left member of comparison
901 * @param v2 right member of comparison
902 * @return success/failure of operation
904 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
905 guint64
* r
= (guint64
*) v1
;
906 return (*r
> v2
.v_uint64
);
910 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
912 * Applies the 'greater than' operator to the
913 * specified structure and value
914 * @param v1 left member of comparison
915 * @param v2 right member of comparison
916 * @return success/failure of operation
918 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
919 guint32
* r
= (guint32
*) v1
;
920 return (*r
> v2
.v_uint32
);
924 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
926 * Applies the 'greater than' operator to the
927 * specified structure and value
928 * @param v1 left member of comparison
929 * @param v2 right member of comparison
930 * @return success/failure of operation
932 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
933 guint16
* r
= (guint16
*) v1
;
934 return (*r
> v2
.v_uint16
);
938 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
940 * Applies the 'greater than' operator to the
941 * specified structure and value
942 * @param v1 left member of comparison
943 * @param v2 right member of comparison
944 * @return success/failure of operation
946 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
947 double* r
= (double*) v1
;
948 return (*r
> v2
.v_double
);
952 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
954 * Applies the 'greater than' operator to the
955 * specified structure and value
956 * @param v1 left member of comparison
957 * @param v2 right member of comparison
958 * @return success/failure of operation
960 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
961 LttTime
* r
= (LttTime
*) v1
;
962 // 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)));
963 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
968 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
970 * Applies the 'greater or equal' operator to the
971 * specified structure and value
972 * @param v1 left member of comparison
973 * @param v2 right member of comparison
974 * @return success/failure of operation
976 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
977 guint64
* r
= (guint64
*) v1
;
978 return (*r
>= v2
.v_uint64
);
982 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
984 * Applies the 'greater or equal' operator to the
985 * specified structure and value
986 * @param v1 left member of comparison
987 * @param v2 right member of comparison
988 * @return success/failure of operation
990 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
991 guint32
* r
= (guint32
*) v1
;
992 return (*r
>= v2
.v_uint32
);
996 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
998 * Applies the 'greater or equal' operator to the
999 * specified structure and value
1000 * @param v1 left member of comparison
1001 * @param v2 right member of comparison
1002 * @return success/failure of operation
1004 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1005 guint16
* r
= (guint16
*) v1
;
1006 return (*r
>= v2
.v_uint16
);
1010 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1012 * Applies the 'greater or equal' operator to the
1013 * specified structure and value
1014 * @param v1 left member of comparison
1015 * @param v2 right member of comparison
1016 * @return success/failure of operation
1018 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1019 double* r
= (double*) v1
;
1020 return (*r
>= v2
.v_double
);
1024 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1026 * Applies the 'greater or equal' operator to the
1027 * specified structure and value
1028 * @param v1 left member of comparison
1029 * @param v2 right member of comparison
1030 * @return success/failure of operation
1032 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1033 LttTime
* r
= (LttTime
*) v1
;
1034 // 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)));
1035 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1041 * Makes a copy of the current filter tree
1042 * @param tree pointer to the current tree
1043 * @return new copy of the filter tree
1046 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1048 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1050 newtree
->node
= tree
->node
;
1052 newtree
->left
= tree
->left
;
1053 if(newtree
->left
== LTTV_TREE_NODE
) {
1054 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1055 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1056 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1057 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1058 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1059 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1060 /* FIXME: special case for string copy ! */
1061 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1064 newtree
->right
= tree
->right
;
1065 if(newtree
->right
== LTTV_TREE_NODE
) {
1066 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1067 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1068 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1069 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1070 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1071 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1072 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1080 * Makes a copy of the current filter
1081 * @param filter pointer to the current filter
1082 * @return new copy of the filter
1085 lttv_filter_clone(const LttvFilter
* filter
) {
1087 if(!filter
) return NULL
;
1089 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1091 strcpy(newfilter
->expression
,filter
->expression
);
1093 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1101 * @fn LttvFilter* lttv_filter_new()
1103 * Creates a new LttvFilter
1104 * @return the current LttvFilter or NULL if error
1109 LttvFilter
* filter
= g_new(LttvFilter
,1);
1110 filter
->expression
= NULL
;
1111 filter
->head
= NULL
;
1118 * @fn gboolean lttv_filter_update(LttvFilter*)
1120 * Updates the current LttvFilter by building
1121 * its tree based upon the expression string
1122 * @param filter pointer to the current LttvFilter
1123 * @return Failure/Success of operation
1126 lttv_filter_update(LttvFilter
* filter
) {
1128 // g_print("filter::lttv_filter_new()\n"); /* debug */
1130 if(filter
->expression
== NULL
) return FALSE
;
1134 p_nesting
=0, /* parenthesis nesting value */
1139 *tree
= lttv_filter_tree_new(), /* main tree */
1140 *subtree
= NULL
, /* buffer for subtrees */
1141 *t1
, /* buffer #1 */
1142 *t2
, /* buffer #2 */
1143 *t3
; /* buffer #3 */
1147 * If the tree already exists,
1148 * destroy it and build a new one
1150 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1151 filter
->head
= NULL
; /* will be assigned at the end */
1155 * each element of the list
1156 * is a sub tree created
1157 * by the use of parenthesis in the
1158 * global expression. The final tree
1159 * will be the one left at the root of
1162 GPtrArray
*tree_stack
= g_ptr_array_new();
1163 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1165 /* temporary values */
1166 GString
*a_field_component
= g_string_new("");
1167 GPtrArray
*a_field_path
= g_ptr_array_new();
1169 /* simple expression buffer */
1170 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1173 * Parse entire expression and construct
1174 * the binary tree. There are two steps
1175 * in browsing that string
1176 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1177 * 2. finding simple expressions
1178 * - field path ( separated by dots )
1179 * - op ( >, <, =, >=, <=, !=)
1180 * - value ( integer, string ... )
1181 * To spare computing time, the whole
1182 * string is parsed in this loop for a
1183 * O(n) complexity order.
1185 * When encountering logical op &,|,^
1186 * 1. parse the last value if any
1187 * 2. create a new tree
1188 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1189 * 4. concatenate this tree with the current tree on top of the stack
1190 * When encountering math ops >,>=,<,<=,=,!=
1191 * 1. add to op to the simple expression
1192 * 2. concatenate last field component to field path
1193 * When encountering concatening ops .
1194 * 1. concatenate last field component to field path
1195 * When encountering opening parenthesis (,{,[
1196 * 1. create a new subtree on top of tree stack
1197 * When encountering closing parenthesis ),},]
1198 * 1. add the expression on right child of the current tree
1199 * 2. the subtree is completed, allocate a new subtree
1200 * 3. pop the tree value from the tree stack
1204 struct timeval starttime
;
1205 struct timeval endtime
;
1206 gettimeofday(&starttime
, NULL
);
1209 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1211 // g_print("%c\n ",filter->expression[i]);
1213 switch(filter
->expression
[i
]) {
1219 /* get current tree in tree stack */
1220 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1222 /* get current node at absolute right */
1223 while(t1
->right
!= LTTV_TREE_IDLE
) {
1224 g_assert(t1
->right
== LTTV_TREE_NODE
);
1227 t2
= lttv_filter_tree_new();
1228 t2
->node
= LTTV_LOGICAL_AND
;
1229 t1
->right
= LTTV_TREE_NODE
;
1231 if(not) { /* add not operator to tree */
1232 t3
= lttv_filter_tree_new();
1233 t3
->node
= LTTV_LOGICAL_NOT
;
1234 t2
->left
= LTTV_TREE_NODE
;
1239 if(subtree
!= NULL
) { /* append subtree to current tree */
1240 t2
->left
= LTTV_TREE_NODE
;
1241 t2
->l_child
.t
= subtree
;
1243 } else { /* append a simple expression */
1244 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1245 a_field_component
= g_string_new("");
1246 t2
->left
= LTTV_TREE_LEAF
;
1247 t2
->l_child
.leaf
= a_simple_expression
;
1248 a_simple_expression
= lttv_simple_expression_new();
1254 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1255 while(t1
->right
!= LTTV_TREE_IDLE
) {
1256 g_assert(t1
->right
== LTTV_TREE_NODE
);
1259 t2
= lttv_filter_tree_new();
1260 t2
->node
= LTTV_LOGICAL_OR
;
1261 t1
->right
= LTTV_TREE_NODE
;
1263 if(not) { // add not operator to tree
1264 t3
= lttv_filter_tree_new();
1265 t3
->node
= LTTV_LOGICAL_NOT
;
1266 t2
->left
= LTTV_TREE_NODE
;
1271 if(subtree
!= NULL
) { /* append subtree to current tree */
1272 t2
->left
= LTTV_TREE_NODE
;
1273 t2
->l_child
.t
= subtree
;
1275 } else { /* append a simple expression */
1276 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1277 a_field_component
= g_string_new("");
1278 t2
->left
= LTTV_TREE_LEAF
;
1279 t2
->l_child
.leaf
= a_simple_expression
;
1280 a_simple_expression
= lttv_simple_expression_new();
1286 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1287 while(t1
->right
!= LTTV_TREE_IDLE
) {
1288 g_assert(t1
->right
== LTTV_TREE_NODE
);
1291 t2
= lttv_filter_tree_new();
1292 t2
->node
= LTTV_LOGICAL_XOR
;
1293 t1
->right
= LTTV_TREE_NODE
;
1295 if(not) { // add not operator to tree
1296 t3
= lttv_filter_tree_new();
1297 t3
->node
= LTTV_LOGICAL_NOT
;
1298 t2
->left
= LTTV_TREE_NODE
;
1303 if(subtree
!= NULL
) { /* append subtree to current tree */
1304 t2
->left
= LTTV_TREE_NODE
;
1305 t2
->l_child
.t
= subtree
;
1307 } else { /* append a simple expression */
1308 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1309 a_field_component
= g_string_new("");
1310 t2
->left
= LTTV_TREE_LEAF
;
1311 t2
->l_child
.leaf
= a_simple_expression
;
1312 a_simple_expression
= lttv_simple_expression_new();
1316 case '!': /* not, or not equal (math op) */
1318 if(filter
->expression
[i
+1] == '=') { /* != */
1319 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1320 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1321 a_field_component
= g_string_new("");
1322 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1329 case '(': /* start of parenthesis */
1333 p_nesting
++; /* incrementing parenthesis nesting value */
1334 t1
= lttv_filter_tree_new();
1335 if(not) { /* add not operator to tree */
1336 t3
= lttv_filter_tree_new();
1337 t3
->node
= LTTV_LOGICAL_NOT
;
1338 t1
->right
= LTTV_TREE_NODE
;
1342 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1345 case ')': /* end of parenthesis */
1349 p_nesting
--; /* decrementing parenthesis nesting value */
1350 if(p_nesting
<0 || tree_stack
->len
<2) {
1351 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1352 is not valid due to parenthesis incorrect use",filter
->expression
);
1356 /* there must at least be the root tree left in the array */
1357 g_assert(tree_stack
->len
>0);
1359 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1360 while(t1
->right
!= LTTV_TREE_IDLE
) {
1363 if(not) { // add not operator to tree
1365 t3
= lttv_filter_tree_new();
1366 t3
->node
= LTTV_LOGICAL_NOT
;
1367 t1
->right
= LTTV_TREE_NODE
;
1372 if(subtree
!= NULL
) { /* append subtree to current tree */
1373 t1
->right
= LTTV_TREE_NODE
;
1374 t1
->r_child
.t
= subtree
;
1375 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1376 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1377 } else { /* assign subtree as current tree */
1378 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1379 a_field_component
= g_string_new("");
1380 t1
->right
= LTTV_TREE_LEAF
;
1381 t1
->r_child
.leaf
= a_simple_expression
;
1382 a_simple_expression
= lttv_simple_expression_new();
1383 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1388 * mathematic operators
1390 case '<': /* lower, lower or equal */
1392 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1393 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1394 a_field_component
= g_string_new("");
1395 if(filter
->expression
[i
+1] == '=') { /* <= */
1397 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1398 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1401 case '>': /* higher, higher or equal */
1403 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1404 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1405 a_field_component
= g_string_new("");
1406 if(filter
->expression
[i
+1] == '=') { /* >= */
1408 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1409 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1412 case '=': /* equal */
1414 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1415 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1416 a_field_component
= g_string_new("");
1417 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1421 * Field concatening caracter
1426 * divide field expression into elements
1427 * in a_field_path array.
1429 * A dot can also be present in double values
1431 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1432 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1433 a_field_component
= g_string_new("");
1436 case ' ': /* ignore */
1437 case '\n': /* ignore */
1439 default: /* concatening current string */
1440 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1445 * Preliminary check to see
1446 * if tree was constructed correctly
1449 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1450 is not valid due to parenthesis incorrect use",filter
->expression
);
1454 if(tree_stack
->len
!= 1) /* only root tree should remain */
1458 * processing last element of expression
1460 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1461 while(t1
->right
!= LTTV_TREE_IDLE
) {
1462 g_assert(t1
->right
== LTTV_TREE_NODE
);
1465 if(not) { // add not operator to tree
1466 t3
= lttv_filter_tree_new();
1467 t3
->node
= LTTV_LOGICAL_NOT
;
1468 t1
->right
= LTTV_TREE_NODE
;
1473 if(subtree
!= NULL
) { /* add the subtree */
1474 t1
->right
= LTTV_TREE_NODE
;
1475 t1
->r_child
.t
= subtree
;
1477 } else { /* add a leaf */
1478 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1479 a_field_component
= NULL
;
1480 t1
->right
= LTTV_TREE_LEAF
;
1481 t1
->r_child
.leaf
= a_simple_expression
;
1482 a_simple_expression
= NULL
;
1486 /* free the pointer array */
1487 g_assert(a_field_path
->len
== 0);
1488 g_ptr_array_free(a_field_path
,TRUE
);
1490 /* free the tree stack -- but keep the root tree */
1491 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1492 g_ptr_array_free(tree_stack
,TRUE
);
1494 /* free the field buffer if allocated */
1495 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1497 /* free the simple expression buffer if allocated */
1498 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1500 g_assert(filter
->head
!= NULL
); /* tree should exist */
1501 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1504 gettimeofday(&endtime
, NULL
);
1506 /* Calcul du temps de l'algorithme */
1507 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1508 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1509 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1510 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1514 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1515 lttv_print_tree(filter
->head
,0) ;
1516 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1524 * @fn void lttv_filter_destroy(LttvFilter*)
1526 * Destroy the current LttvFilter
1527 * @param filter pointer to the current LttvFilter
1530 lttv_filter_destroy(LttvFilter
* filter
) {
1534 if(filter
->expression
)
1535 g_free(filter
->expression
);
1537 lttv_filter_tree_destroy(filter
->head
);
1543 * @fn LttvFilterTree* lttv_filter_tree_new()
1545 * Assign a new tree for the current expression
1547 * @return pointer of LttvFilterTree
1550 lttv_filter_tree_new() {
1551 LttvFilterTree
* tree
;
1553 tree
= g_new(LttvFilterTree
,1);
1554 tree
->node
= 0; //g_new(lttv_expression,1);
1555 tree
->left
= LTTV_TREE_IDLE
;
1556 tree
->right
= LTTV_TREE_IDLE
;
1557 tree
->r_child
.t
= NULL
;
1558 tree
->l_child
.t
= NULL
;
1564 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1566 * Append a new expression to the expression
1567 * defined in the current filter
1568 * @param filter pointer to the current LttvFilter
1569 * @param expression string that must be appended
1570 * @return Success/Failure of operation
1573 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1575 if(expression
== NULL
) return FALSE
;
1576 if(filter
== NULL
) return FALSE
;
1577 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1579 GString
* s
= g_string_new("");
1580 if(filter
->expression
!= NULL
) {
1581 g_string_append(s
,filter
->expression
);
1582 g_string_append_c(s
,'&');
1584 g_string_append(s
,expression
);
1586 g_free(filter
->expression
);
1587 filter
->expression
= g_string_free(s
,FALSE
);
1589 /* TRUE if construction of tree proceeded without errors */
1590 return lttv_filter_update(filter
);
1595 * @fn void lttv_filter_clear_expression(LttvFilter*)
1597 * Clear the filter expression from the
1598 * current filter and sets its pointer to NULL
1599 * @param filter pointer to the current LttvFilter
1602 lttv_filter_clear_expression(LttvFilter
* filter
) {
1604 if(filter
->expression
!= NULL
) {
1605 g_free(filter
->expression
);
1606 filter
->expression
= NULL
;
1612 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1614 * Destroys the tree and his sub-trees
1615 * @param tree Tree which must be destroyed
1618 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1620 if(tree
== NULL
) return;
1622 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1623 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1625 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1626 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1628 // g_free(tree->node);
1633 * Global parsing function for the current
1635 * @param t pointer to the current LttvFilterTree
1636 * @param event current LttEvent, NULL if not used
1637 * @param tracefile current LttTracefile, NULL if not used
1638 * @param trace current LttTrace, NULL if not used
1639 * @param state current LttvProcessState, NULL if not used
1640 * @param context current LttvTracefileContext, NULL if not used
1641 * @return response of filter
1644 lttv_filter_tree_parse(
1645 const LttvFilterTree
* t
,
1646 const LttEvent
* event
,
1647 const LttTracefile
* tracefile
,
1648 const LttTrace
* trace
,
1649 const LttvTracefileContext
* context
1654 * Each tree is parsed in inorder.
1655 * This way, it's possible to apply the left filter of the
1656 * tree, then decide whether or not the right branch should
1657 * be parsed depending on the linking logical operator
1659 * Each node consists in a
1660 * 1. logical operator
1661 * 2. left child ( node or simple expression )
1662 * 3. right child ( node or simple expression )
1664 * When the child is a simple expression, we must
1665 * before all determine if the expression refers to
1666 * a structure which is whithin observation ( not NULL ).
1667 * -If so, the expression is evaluated.
1668 * -If not, the result is set to TRUE since this particular
1669 * operation does not interfere with the lttv structure
1671 * The result of each simple expression will directly
1672 * affect the next branch. This way, depending on
1673 * the linking logical operator, the parser will decide
1674 * to explore or not the next branch.
1676 * -If result of left branch is 0 / FALSE
1677 * then don't explore right branch and return 0;
1678 * -If result of left branch is 1 / TRUE then explore
1680 * -If result of left branch is 1 / TRUE
1681 * then don't explore right branch and return 1;
1682 * -If result of left branch is 0 / FALSE then explore
1684 * -Result of left branch will not affect exploration of
1688 gboolean lresult
= FALSE
, rresult
= FALSE
;
1690 LttvProcessState
* state
;
1692 guint cpu
= ltt_tracefile_num(context
->tf
);
1693 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1694 state
= ts
->running_process
[cpu
];
1699 if(t
->left
== LTTV_TREE_NODE
) {
1700 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1702 else if(t
->left
== LTTV_TREE_LEAF
) {
1703 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1707 * Parse linking operator
1708 * make a cutoff if possible
1710 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1711 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1714 * Parse right branch
1716 if(t
->right
== LTTV_TREE_NODE
) {
1717 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1719 else if(t
->right
== LTTV_TREE_LEAF
) {
1720 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1725 * Apply and return the
1726 * logical link between the
1730 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1731 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1732 case LTTV_LOGICAL_NOT
:
1733 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1734 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1735 case 0: return (rresult
);
1738 * This case should never be
1739 * parsed, if so, this subtree
1748 * This function parses a particular branch of the tree
1749 * @param se pointer to the current LttvSimpleExpression
1750 * @param event current LttEvent, NULL if not used
1751 * @param tracefile current LttTracefile, NULL if not used
1752 * @param trace current LttTrace, NULL if not used
1753 * @param state current LttvProcessState, NULL if not used
1754 * @param context current LttvTracefileContext, NULL if not used
1755 * @return response of filter
1758 lttv_filter_tree_parse_branch(
1759 const LttvSimpleExpression
* se
,
1760 const LttEvent
* event
,
1761 const LttTracefile
* tracefile
,
1762 const LttTrace
* trace
,
1763 const LttvProcessState
* state
,
1764 const LttvTracefileContext
* context
) {
1769 case LTTV_FILTER_TRACE_NAME
:
1770 if(trace
== NULL
) return TRUE
;
1772 GQuark quark
= ltt_trace_name(trace
);
1773 return se
->op((gpointer
)&quark
,v
);
1776 case LTTV_FILTER_TRACEFILE_NAME
:
1777 if(tracefile
== NULL
) return TRUE
;
1779 GQuark quark
= ltt_tracefile_name(tracefile
);
1780 return se
->op((gpointer
)&quark
,v
);
1783 case LTTV_FILTER_STATE_PID
:
1784 if(state
== NULL
) return TRUE
;
1785 else return se
->op((gpointer
)&state
->pid
,v
);
1787 case LTTV_FILTER_STATE_PPID
:
1788 if(state
== NULL
) return TRUE
;
1789 else return se
->op((gpointer
)&state
->ppid
,v
);
1791 case LTTV_FILTER_STATE_CT
:
1792 if(state
== NULL
) return TRUE
;
1794 return se
->op((gpointer
)&state
->creation_time
,v
);
1797 case LTTV_FILTER_STATE_IT
:
1798 if(state
== NULL
) return TRUE
;
1800 return se
->op((gpointer
)&state
->insertion_time
,v
);
1803 case LTTV_FILTER_STATE_P_NAME
:
1805 * All 'unnamed' for the moment
1807 if(state
== NULL
) return TRUE
;
1809 GQuark quark
= state
->name
;
1810 return se
->op((gpointer
)&quark
,v
);
1813 case LTTV_FILTER_STATE_EX_MODE
:
1814 if(state
== NULL
) return TRUE
;
1815 else return se
->op((gpointer
)&state
->state
->t
,v
);
1817 case LTTV_FILTER_STATE_EX_SUBMODE
:
1818 if(state
== NULL
) return TRUE
;
1819 else return se
->op((gpointer
)&state
->state
->n
,v
);
1821 case LTTV_FILTER_STATE_P_STATUS
:
1822 if(state
== NULL
) return TRUE
;
1823 else return se
->op((gpointer
)&state
->state
->s
,v
);
1825 case LTTV_FILTER_STATE_CPU
:
1826 if(context
== NULL
) return TRUE
;
1828 /* FIXME: not sure of that one Mathieu : fixed.*/
1829 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1830 // return se->op((gpointer)&quark,v);
1831 if(state
== NULL
) return TRUE
;
1832 else return se
->op((gpointer
)&state
->cpu
,v
);
1835 case LTTV_FILTER_EVENT_NAME
:
1836 if(event
== NULL
) return TRUE
;
1839 et
= ltt_event_eventtype(event
);
1840 GQuark quark
= ltt_eventtype_name(et
);
1841 return se
->op((gpointer
)&quark
,v
);
1845 case LTTV_FILTER_EVENT_CATEGORY
:
1847 * TODO: Not yet implemented
1851 case LTTV_FILTER_EVENT_TIME
:
1852 if(event
== NULL
) return TRUE
;
1854 LttTime time
= ltt_event_time(event
);
1855 return se
->op((gpointer
)&time
,v
);
1858 case LTTV_FILTER_EVENT_TSC
:
1859 if(event
== NULL
) return TRUE
;
1861 LttCycleCount count
= ltt_event_cycle_count(event
);
1862 return se
->op((gpointer
)&count
,v
);
1865 case LTTV_FILTER_EVENT_FIELD
:
1867 * TODO: Use the offset to
1868 * find the dynamic field
1869 * in the event struct
1874 * This case should never be
1875 * parsed, if so, the whole
1876 * filtering is cancelled
1878 g_warning("Error while parsing the filter tree");
1882 /* should never get here */
1890 * Debug function. Prints tree memory allocation.
1891 * @param t the pointer to the current LttvFilterTree
1894 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1896 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1897 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1898 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1900 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1901 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"));
1902 if(t
->left
== LTTV_TREE_LEAF
) {
1903 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1904 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1905 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1907 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"));
1908 if(t
->right
== LTTV_TREE_LEAF
) {
1909 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1910 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1911 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1914 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1915 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
1919 * @fn static void module_init()
1921 * Initializes the filter module and specific values
1923 static void module_init()
1929 * Destroys the filter module and specific values
1931 static void module_destroy()
1937 LTTV_MODULE("filter", "Filters traceset and events", \
1938 "Filters traceset and events specifically to user input", \
1939 module_init
, module_destroy
)