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 * | |->facility (String, converted to GQuark)
35 * | |->category (String, not yet implemented)
37 * | |->tsc (LttCycleCount --> uint64)
41 * | |->"sub-field name"
43 * | |->"leaf-field name" (field type)
45 * | |->name (String, converted to GQuark)
47 * | |->name (String, converted to GQuark)
51 * |->creation_time (LttTime)
52 * |->insertion_time (LttTime)
53 * |->process_name (String, converted to GQuark)
54 * |->execution_mode (LttvExecutionMode)
55 * |->execution_submode (LttvExecutionSubmode)
56 * |->process_status (LttvProcessStatus)
63 * - refine switch of expression in multiple uses functions
64 * - remove the idle expressions in the tree
77 #include <lttv/filter.h>
78 #include <ltt/trace.h>
84 * @fn LttvSimpleExpression* lttv_simple_expression_new()
86 * Constructor for LttvSimpleExpression
87 * @return pointer to new LttvSimpleExpression
90 lttv_simple_expression_new() {
92 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
94 se
->field
= LTTV_FILTER_UNDEFINED
;
102 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
104 * Parse through filtering field hierarchy as specified
105 * by user. This function compares each value to
106 * predetermined quarks
107 * @param fp The field path list
108 * @param se current simple expression
109 * @return success/failure of operation
112 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
116 if(fp
->len
< 2) return FALSE
;
117 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
120 * Parse through the specified
123 * Take note however that the
124 * 'event' subfields might change
125 * depending on values specified
126 * in core.xml file. Hence, if
127 * none of the subfields in the
128 * array match the hardcoded
129 * subfields, it will be considered
132 if(!g_strcasecmp(f
->str
,"trace") ) {
137 g_string_free(f
,TRUE
);
138 f
=g_ptr_array_remove_index(fp
,0);
139 if(!g_strcasecmp(f
->str
,"name")) {
140 se
->field
= LTTV_FILTER_TRACE_NAME
;
142 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
144 * FIXME: not yet implemented !
146 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
151 g_string_free(f
,TRUE
);
152 f
=g_ptr_array_remove_index(fp
,0);
153 if(!g_strcasecmp(f
->str
,"name")) {
154 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
156 } else if(!g_strcasecmp(f
->str
,"state") ) {
161 * state.creation_time
162 * state.insertion_time
164 * state.execution_mode
165 * state.execution_submode
166 * state.process_status
169 g_string_free(f
,TRUE
);
170 f
=g_ptr_array_remove_index(fp
,0);
171 if(!g_strcasecmp(f
->str
,"pid") ) {
172 se
->field
= LTTV_FILTER_STATE_PID
;
174 else if(!g_strcasecmp(f
->str
,"ppid") ) {
175 se
->field
= LTTV_FILTER_STATE_PPID
;
177 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
178 se
->field
= LTTV_FILTER_STATE_CT
;
180 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
181 se
->field
= LTTV_FILTER_STATE_IT
;
183 else if(!g_strcasecmp(f
->str
,"process_name") ) {
184 se
->field
= LTTV_FILTER_STATE_P_NAME
;
186 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
187 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
189 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
190 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
192 else if(!g_strcasecmp(f
->str
,"process_status") ) {
193 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
195 else if(!g_strcasecmp(f
->str
,"cpu") ) {
196 se
->field
= LTTV_FILTER_STATE_CPU
;
198 } else if(!g_strcasecmp(f
->str
,"event") ) {
206 g_string_free(f
,TRUE
);
207 f
=g_ptr_array_remove_index(fp
,0);
208 if(!g_strcasecmp(f
->str
,"name") ) {
209 se
->field
= LTTV_FILTER_EVENT_NAME
;
211 else if(!g_strcasecmp(f
->str
,"facility") ) {
212 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
214 else if(!g_strcasecmp(f
->str
,"category") ) {
216 * FIXME: Category not yet functional in lttv
218 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
220 else if(!g_strcasecmp(f
->str
,"time") ) {
221 se
->field
= LTTV_FILTER_EVENT_TIME
;
223 else if(!g_strcasecmp(f
->str
,"tsc") ) {
224 se
->field
= LTTV_FILTER_EVENT_TSC
;
226 else { /* core.xml specified options */
227 se
->field
= LTTV_FILTER_EVENT_FIELD
;
230 g_warning("Unrecognized field in filter string");
233 /* free memory for last string */
234 g_string_free(f
,TRUE
);
236 /* array should be empty */
237 g_assert(fp
->len
== 0);
239 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
240 g_warning("The specified field was not recognized !");
247 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
249 * Sets the function pointer for the current
251 * @param se current simple expression
252 * @param op current operator
253 * @return success/failure of operation
256 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
262 case LTTV_FILTER_TRACE_NAME
:
263 case LTTV_FILTER_TRACEFILE_NAME
:
264 case LTTV_FILTER_STATE_P_NAME
:
265 case LTTV_FILTER_EVENT_NAME
:
266 case LTTV_FILTER_STATE_EX_MODE
:
267 case LTTV_FILTER_STATE_EX_SUBMODE
:
268 case LTTV_FILTER_STATE_P_STATUS
:
271 se
->op
= lttv_apply_op_eq_quark
;
274 se
->op
= lttv_apply_op_ne_quark
;
277 g_warning("Error encountered in operator assignment = or != expected");
284 case LTTV_FILTER_EVENT_TSC
:
287 se
->op
= lttv_apply_op_eq_uint64
;
290 se
->op
= lttv_apply_op_ne_uint64
;
293 se
->op
= lttv_apply_op_lt_uint64
;
296 se
->op
= lttv_apply_op_le_uint64
;
299 se
->op
= lttv_apply_op_gt_uint64
;
302 se
->op
= lttv_apply_op_ge_uint64
;
305 g_warning("Error encountered in operator assignment");
312 case LTTV_FILTER_STATE_CPU
:
313 case LTTV_FILTER_STATE_PID
:
314 case LTTV_FILTER_STATE_PPID
:
317 se
->op
= lttv_apply_op_eq_uint
;
320 se
->op
= lttv_apply_op_ne_uint
;
323 se
->op
= lttv_apply_op_lt_uint
;
326 se
->op
= lttv_apply_op_le_uint
;
329 se
->op
= lttv_apply_op_gt_uint
;
332 se
->op
= lttv_apply_op_ge_uint
;
335 g_warning("Error encountered in operator assignment");
342 * Entered as string, converted to enum
344 * can only be compared with 'equal' or 'not equal' operators
346 * unsigned int of 16 bits are used here since enums
347 * should not go over 2^16-1 values
351 // case LTTV_FIELD_EQ:
352 // se->op = lttv_apply_op_eq_uint16;
354 // case LTTV_FIELD_NE:
355 // se->op = lttv_apply_op_ne_uint16;
358 // g_warning("Error encountered in operator assignment = or != expected");
365 case LTTV_FILTER_STATE_CT
:
366 case LTTV_FILTER_STATE_IT
:
367 case LTTV_FILTER_EVENT_TIME
:
370 se
->op
= lttv_apply_op_eq_ltttime
;
373 se
->op
= lttv_apply_op_ne_ltttime
;
376 se
->op
= lttv_apply_op_lt_ltttime
;
379 se
->op
= lttv_apply_op_le_ltttime
;
382 se
->op
= lttv_apply_op_gt_ltttime
;
385 se
->op
= lttv_apply_op_ge_ltttime
;
388 g_warning("Error encountered in operator assignment");
393 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
402 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
404 * Assign the value field to the current LttvSimpleExpression
405 * @param se pointer to the current LttvSimpleExpression
406 * @param value string value for simple expression
409 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
412 gboolean is_double
= FALSE
;
413 LttTime t
= ltt_time_zero
;
419 * entered as strings, converted to Quarks
421 case LTTV_FILTER_TRACE_NAME
:
422 case LTTV_FILTER_TRACEFILE_NAME
:
423 case LTTV_FILTER_STATE_P_NAME
:
424 case LTTV_FILTER_EVENT_NAME
:
425 case LTTV_FILTER_STATE_EX_MODE
:
426 case LTTV_FILTER_STATE_EX_SUBMODE
:
427 case LTTV_FILTER_STATE_P_STATUS
:
428 // se->value.v_string = value;
429 se
->value
.v_uint32
= g_quark_from_string(value
);
433 * integer -- supposed to be uint64
435 case LTTV_FILTER_EVENT_TSC
:
436 se
->value
.v_uint64
= atoi(value
);
442 case LTTV_FILTER_STATE_PID
:
443 case LTTV_FILTER_STATE_PPID
:
444 case LTTV_FILTER_STATE_CPU
:
445 se
->value
.v_uint
= atoi(value
);
451 case LTTV_FILTER_STATE_CT
:
452 case LTTV_FILTER_STATE_IT
:
453 case LTTV_FILTER_EVENT_TIME
:
454 //se->value.v_double = atof(value);
456 * parsing logic could be optimised,
457 * but as for now, simpler this way
459 v
= g_string_new("");
460 for(i
=0;i
<strlen(value
);i
++) {
461 if(value
[i
] == '.') {
462 /* cannot specify number with more than one '.' */
463 if(is_double
) return FALSE
;
464 else is_double
= TRUE
;
465 t
.tv_sec
= atoi(v
->str
);
466 g_string_free(v
,TRUE
);
467 v
= g_string_new("");
468 } else g_string_append_c(v
,value
[i
]);
470 /* number can be integer or double */
471 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
472 else t
.tv_sec
= atoi(v
->str
);
474 g_string_free(v
,TRUE
);
476 se
->value
.v_ltttime
= t
;
480 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
490 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
492 * Disallocate memory for the current
494 * @param se pointer to the current LttvSimpleExpression
497 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
499 // g_free(se->value);
500 // switch(se->field) {
501 // case LTTV_FILTER_TRACE_NAME:
502 // case LTTV_FILTER_TRACEFILE_NAME:
503 // case LTTV_FILTER_STATE_P_NAME:
504 // case LTTV_FILTER_EVENT_NAME:
505 // g_free(se->value.v_string);
513 * @fn gint lttv_struct_type(gint)
515 * Finds the structure type depending
516 * on the fields in parameters
517 * @params ft Field of the current structure
518 * @return LttvStructType enum or -1 for error
521 lttv_struct_type(gint ft
) {
524 case LTTV_FILTER_TRACE_NAME
:
525 return LTTV_FILTER_TRACE
;
527 case LTTV_FILTER_TRACEFILE_NAME
:
528 return LTTV_FILTER_TRACEFILE
;
530 case LTTV_FILTER_STATE_PID
:
531 case LTTV_FILTER_STATE_PPID
:
532 case LTTV_FILTER_STATE_CT
:
533 case LTTV_FILTER_STATE_IT
:
534 case LTTV_FILTER_STATE_P_NAME
:
535 case LTTV_FILTER_STATE_EX_MODE
:
536 case LTTV_FILTER_STATE_EX_SUBMODE
:
537 case LTTV_FILTER_STATE_P_STATUS
:
538 case LTTV_FILTER_STATE_CPU
:
539 return LTTV_FILTER_STATE
;
541 case LTTV_FILTER_EVENT_NAME
:
542 case LTTV_FILTER_EVENT_FACILITY
:
543 case LTTV_FILTER_EVENT_CATEGORY
:
544 case LTTV_FILTER_EVENT_TIME
:
545 case LTTV_FILTER_EVENT_TSC
:
546 case LTTV_FILTER_EVENT_FIELD
:
547 return LTTV_FILTER_EVENT
;
555 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
557 * Applies the 'equal' operator to the
558 * specified structure and value
559 * @param v1 left member of comparison
560 * @param v2 right member of comparison
561 * @return success/failure of operation
563 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
565 guint
* r
= (guint
*) v1
;
566 return (*r
== v2
.v_uint
);
571 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
573 * Applies the 'equal' operator to the
574 * specified structure and value
575 * @param v1 left member of comparison
576 * @param v2 right member of comparison
577 * @return success/failure of operation
579 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
581 guint64
* r
= (guint64
*) v1
;
582 return (*r
== v2
.v_uint64
);
587 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
589 * Applies the 'equal' operator to the
590 * specified structure and value
591 * @param v1 left member of comparison
592 * @param v2 right member of comparison
593 * @return success/failure of operation
595 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
596 guint32
* r
= (guint32
*) v1
;
597 return (*r
== v2
.v_uint32
);
601 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
603 * Applies the 'equal' operator to the
604 * specified structure and value
605 * @param v1 left member of comparison
606 * @param v2 right member of comparison
607 * @return success/failure of operation
609 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
610 guint16
* r
= (guint16
*) v1
;
611 return (*r
== v2
.v_uint16
);
615 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
617 * Applies the 'equal' operator to the
618 * specified structure and value
619 * @param v1 left member of comparison
620 * @param v2 right member of comparison
621 * @return success/failure of operation
623 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
624 double* r
= (double*) v1
;
625 return (*r
== v2
.v_double
);
629 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
631 * Applies the 'equal' operator to the
632 * specified structure and value
633 * @param v1 left member of comparison
634 * @param v2 right member of comparison
635 * @return success/failure of operation
637 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
638 char* r
= (char*) v1
;
639 return (!g_strcasecmp(r
,v2
.v_string
));
643 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
645 * Applies the 'equal' operator to the
646 * specified structure and value
647 * @param v1 left member of comparison
648 * @param v2 right member of comparison
649 * @return success/failure of operation
651 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
652 GQuark
* r
= (GQuark
*) v1
;
653 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
654 return (*r
== v2
.v_uint32
);
658 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
660 * Applies the 'equal' operator to the
661 * specified structure and value
662 * @param v1 left member of comparison
663 * @param v2 right member of comparison
664 * @return success/failure of operation
666 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
667 LttTime
* r
= (LttTime
*) v1
;
668 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
672 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
674 * Applies the 'not equal' operator to the
675 * specified structure and value
676 * @param v1 left member of comparison
677 * @param v2 right member of comparison
678 * @return success/failure of operation
680 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
681 guint
* r
= (guint
*) v1
;
682 return (*r
!= v2
.v_uint
);
686 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
688 * Applies the 'not equal' operator to the
689 * specified structure and value
690 * @param v1 left member of comparison
691 * @param v2 right member of comparison
692 * @return success/failure of operation
694 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
695 guint64
* r
= (guint64
*) v1
;
696 return (*r
!= v2
.v_uint64
);
700 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
702 * Applies the 'not equal' operator to the
703 * specified structure and value
704 * @param v1 left member of comparison
705 * @param v2 right member of comparison
706 * @return success/failure of operation
708 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
709 guint32
* r
= (guint32
*) v1
;
710 return (*r
!= v2
.v_uint32
);
714 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
716 * Applies the 'not equal' operator to the
717 * specified structure and value
718 * @param v1 left member of comparison
719 * @param v2 right member of comparison
720 * @return success/failure of operation
722 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
723 guint16
* r
= (guint16
*) v1
;
724 return (*r
!= v2
.v_uint16
);
728 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
730 * Applies the 'not equal' operator to the
731 * specified structure and value
732 * @param v1 left member of comparison
733 * @param v2 right member of comparison
734 * @return success/failure of operation
736 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
737 double* r
= (double*) v1
;
738 return (*r
!= v2
.v_double
);
742 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
744 * Applies the 'not equal' operator to the
745 * specified structure and value
746 * @param v1 left member of comparison
747 * @param v2 right member of comparison
748 * @return success/failure of operation
750 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
751 char* r
= (char*) v1
;
752 return (g_strcasecmp(r
,v2
.v_string
));
756 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
758 * Applies the 'not equal' operator to the
759 * specified structure and value
760 * @param v1 left member of comparison
761 * @param v2 right member of comparison
762 * @return success/failure of operation
764 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
765 GQuark
* r
= (GQuark
*) v1
;
766 return (*r
!= v2
.v_uint32
);
771 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
773 * Applies the 'not equal' operator to the
774 * specified structure and value
775 * @param v1 left member of comparison
776 * @param v2 right member of comparison
777 * @return success/failure of operation
779 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
780 LttTime
* r
= (LttTime
*) v1
;
781 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
785 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
787 * Applies the 'lower than' operator to the
788 * specified structure and value
789 * @param v1 left member of comparison
790 * @param v2 right member of comparison
791 * @return success/failure of operation
793 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
794 guint
* r
= (guint
*) v1
;
795 return (*r
< v2
.v_uint
);
799 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
801 * Applies the 'lower than' operator to the
802 * specified structure and value
803 * @param v1 left member of comparison
804 * @param v2 right member of comparison
805 * @return success/failure of operation
807 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
808 guint64
* r
= (guint64
*) v1
;
809 return (*r
< v2
.v_uint64
);
813 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
815 * Applies the 'lower than' operator to the
816 * specified structure and value
817 * @param v1 left member of comparison
818 * @param v2 right member of comparison
819 * @return success/failure of operation
821 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
822 guint32
* r
= (guint32
*) v1
;
823 return (*r
< v2
.v_uint32
);
827 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
829 * Applies the 'lower than' operator to the
830 * specified structure and value
831 * @param v1 left member of comparison
832 * @param v2 right member of comparison
833 * @return success/failure of operation
835 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
836 guint16
* r
= (guint16
*) v1
;
837 return (*r
< v2
.v_uint16
);
841 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
843 * Applies the 'lower than' operator to the
844 * specified structure and value
845 * @param v1 left member of comparison
846 * @param v2 right member of comparison
847 * @return success/failure of operation
849 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
850 double* r
= (double*) v1
;
851 return (*r
< v2
.v_double
);
855 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
857 * Applies the 'lower than' operator to the
858 * specified structure and value
859 * @param v1 left member of comparison
860 * @param v2 right member of comparison
861 * @return success/failure of operation
863 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
864 LttTime
* r
= (LttTime
*) v1
;
865 // 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)));
866 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
870 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
872 * Applies the 'lower or equal' operator to the
873 * specified structure and value
874 * @param v1 left member of comparison
875 * @param v2 right member of comparison
876 * @return success/failure of operation
878 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
879 guint
* r
= (guint
*) v1
;
880 return (*r
<= v2
.v_uint
);
884 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
886 * Applies the 'lower or equal' operator to the
887 * specified structure and value
888 * @param v1 left member of comparison
889 * @param v2 right member of comparison
890 * @return success/failure of operation
892 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
893 guint64
* r
= (guint64
*) v1
;
894 return (*r
<= v2
.v_uint64
);
898 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
900 * Applies the 'lower or equal' operator to the
901 * specified structure and value
902 * @param v1 left member of comparison
903 * @param v2 right member of comparison
904 * @return success/failure of operation
906 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
907 guint32
* r
= (guint32
*) v1
;
908 return (*r
<= v2
.v_uint32
);
912 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
914 * Applies the 'lower or equal' operator to the
915 * specified structure and value
916 * @param v1 left member of comparison
917 * @param v2 right member of comparison
918 * @return success/failure of operation
920 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
921 guint16
* r
= (guint16
*) v1
;
922 return (*r
<= v2
.v_uint16
);
926 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
928 * Applies the 'lower or equal' operator to the
929 * specified structure and value
930 * @param v1 left member of comparison
931 * @param v2 right member of comparison
932 * @return success/failure of operation
934 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
935 double* r
= (double*) v1
;
936 return (*r
<= v2
.v_double
);
940 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
942 * Applies the 'lower or equal' operator to the
943 * specified structure and value
944 * @param v1 left member of comparison
945 * @param v2 right member of comparison
946 * @return success/failure of operation
948 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
949 LttTime
* r
= (LttTime
*) v1
;
950 // 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)));
951 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
956 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
958 * Applies the 'greater than' operator to the
959 * specified structure and value
960 * @param v1 left member of comparison
961 * @param v2 right member of comparison
962 * @return success/failure of operation
964 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
965 guint
* r
= (guint
*) v1
;
966 return (*r
> v2
.v_uint
);
970 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
972 * Applies the 'greater than' operator to the
973 * specified structure and value
974 * @param v1 left member of comparison
975 * @param v2 right member of comparison
976 * @return success/failure of operation
978 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
979 guint64
* r
= (guint64
*) v1
;
980 return (*r
> v2
.v_uint64
);
984 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
986 * Applies the 'greater than' operator to the
987 * specified structure and value
988 * @param v1 left member of comparison
989 * @param v2 right member of comparison
990 * @return success/failure of operation
992 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
993 guint32
* r
= (guint32
*) v1
;
994 return (*r
> v2
.v_uint32
);
998 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1000 * Applies the 'greater than' operator to the
1001 * specified structure and value
1002 * @param v1 left member of comparison
1003 * @param v2 right member of comparison
1004 * @return success/failure of operation
1006 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1007 guint16
* r
= (guint16
*) v1
;
1008 return (*r
> v2
.v_uint16
);
1012 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1014 * Applies the 'greater than' operator to the
1015 * specified structure and value
1016 * @param v1 left member of comparison
1017 * @param v2 right member of comparison
1018 * @return success/failure of operation
1020 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1021 double* r
= (double*) v1
;
1022 return (*r
> v2
.v_double
);
1026 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1028 * Applies the 'greater than' operator to the
1029 * specified structure and value
1030 * @param v1 left member of comparison
1031 * @param v2 right member of comparison
1032 * @return success/failure of operation
1034 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1035 LttTime
* r
= (LttTime
*) v1
;
1036 // 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)));
1037 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1041 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1043 * Applies the 'greater or equal' operator to the
1044 * specified structure and value
1045 * @param v1 left member of comparison
1046 * @param v2 right member of comparison
1047 * @return success/failure of operation
1049 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1050 guint
* r
= (guint
*) v1
;
1051 return (*r
>= v2
.v_uint
);
1055 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1057 * Applies the 'greater or equal' operator to the
1058 * specified structure and value
1059 * @param v1 left member of comparison
1060 * @param v2 right member of comparison
1061 * @return success/failure of operation
1063 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1064 guint64
* r
= (guint64
*) v1
;
1065 return (*r
>= v2
.v_uint64
);
1069 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1071 * Applies the 'greater or equal' operator to the
1072 * specified structure and value
1073 * @param v1 left member of comparison
1074 * @param v2 right member of comparison
1075 * @return success/failure of operation
1077 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1078 guint32
* r
= (guint32
*) v1
;
1079 return (*r
>= v2
.v_uint32
);
1083 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1085 * Applies the 'greater or equal' operator to the
1086 * specified structure and value
1087 * @param v1 left member of comparison
1088 * @param v2 right member of comparison
1089 * @return success/failure of operation
1091 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1092 guint16
* r
= (guint16
*) v1
;
1093 return (*r
>= v2
.v_uint16
);
1097 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1099 * Applies the 'greater or equal' operator to the
1100 * specified structure and value
1101 * @param v1 left member of comparison
1102 * @param v2 right member of comparison
1103 * @return success/failure of operation
1105 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1106 double* r
= (double*) v1
;
1107 return (*r
>= v2
.v_double
);
1111 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1113 * Applies the 'greater or equal' operator to the
1114 * specified structure and value
1115 * @param v1 left member of comparison
1116 * @param v2 right member of comparison
1117 * @return success/failure of operation
1119 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1120 LttTime
* r
= (LttTime
*) v1
;
1121 // 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)));
1122 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1128 * Makes a copy of the current filter tree
1129 * @param tree pointer to the current tree
1130 * @return new copy of the filter tree
1133 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1135 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1137 newtree
->node
= tree
->node
;
1139 newtree
->left
= tree
->left
;
1140 if(newtree
->left
== LTTV_TREE_NODE
) {
1141 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1142 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1143 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1144 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1145 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1146 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1147 /* FIXME: special case for string copy ! */
1148 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1151 newtree
->right
= tree
->right
;
1152 if(newtree
->right
== LTTV_TREE_NODE
) {
1153 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1154 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1155 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1156 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1157 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1158 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1159 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1167 * Makes a copy of the current filter
1168 * @param filter pointer to the current filter
1169 * @return new copy of the filter
1172 lttv_filter_clone(const LttvFilter
* filter
) {
1174 if(!filter
) return NULL
;
1176 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1178 strcpy(newfilter
->expression
,filter
->expression
);
1180 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1188 * @fn LttvFilter* lttv_filter_new()
1190 * Creates a new LttvFilter
1191 * @return the current LttvFilter or NULL if error
1196 LttvFilter
* filter
= g_new(LttvFilter
,1);
1197 filter
->expression
= NULL
;
1198 filter
->head
= NULL
;
1205 * @fn gboolean lttv_filter_update(LttvFilter*)
1207 * Updates the current LttvFilter by building
1208 * its tree based upon the expression string
1209 * @param filter pointer to the current LttvFilter
1210 * @return Failure/Success of operation
1213 lttv_filter_update(LttvFilter
* filter
) {
1215 // g_print("filter::lttv_filter_new()\n"); /* debug */
1217 if(filter
->expression
== NULL
) return FALSE
;
1221 p_nesting
=0, /* parenthesis nesting value */
1226 *tree
= lttv_filter_tree_new(), /* main tree */
1227 *subtree
= NULL
, /* buffer for subtrees */
1228 *t1
, /* buffer #1 */
1229 *t2
, /* buffer #2 */
1230 *t3
; /* buffer #3 */
1234 * If the tree already exists,
1235 * destroy it and build a new one
1237 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1238 filter
->head
= NULL
; /* will be assigned at the end */
1242 * each element of the list
1243 * is a sub tree created
1244 * by the use of parenthesis in the
1245 * global expression. The final tree
1246 * will be the one left at the root of
1249 GPtrArray
*tree_stack
= g_ptr_array_new();
1250 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1252 /* temporary values */
1253 GString
*a_field_component
= g_string_new("");
1254 GPtrArray
*a_field_path
= g_ptr_array_new();
1256 /* simple expression buffer */
1257 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1260 * Parse entire expression and construct
1261 * the binary tree. There are two steps
1262 * in browsing that string
1263 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1264 * 2. finding simple expressions
1265 * - field path ( separated by dots )
1266 * - op ( >, <, =, >=, <=, !=)
1267 * - value ( integer, string ... )
1268 * To spare computing time, the whole
1269 * string is parsed in this loop for a
1270 * O(n) complexity order.
1272 * When encountering logical op &,|,^
1273 * 1. parse the last value if any
1274 * 2. create a new tree
1275 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1276 * 4. concatenate this tree with the current tree on top of the stack
1277 * When encountering math ops >,>=,<,<=,=,!=
1278 * 1. add to op to the simple expression
1279 * 2. concatenate last field component to field path
1280 * When encountering concatening ops .
1281 * 1. concatenate last field component to field path
1282 * When encountering opening parenthesis (,{,[
1283 * 1. create a new subtree on top of tree stack
1284 * When encountering closing parenthesis ),},]
1285 * 1. add the expression on right child of the current tree
1286 * 2. the subtree is completed, allocate a new subtree
1287 * 3. pop the tree value from the tree stack
1291 struct timeval starttime
;
1292 struct timeval endtime
;
1293 gettimeofday(&starttime
, NULL
);
1296 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1298 // g_print("%c\n ",filter->expression[i]);
1300 switch(filter
->expression
[i
]) {
1306 /* get current tree in tree stack */
1307 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1309 /* get current node at absolute right */
1310 while(t1
->right
!= LTTV_TREE_IDLE
) {
1311 g_assert(t1
->right
== LTTV_TREE_NODE
);
1314 t2
= lttv_filter_tree_new();
1315 t2
->node
= LTTV_LOGICAL_AND
;
1316 t1
->right
= LTTV_TREE_NODE
;
1318 if(not) { /* add not operator to tree */
1319 t3
= lttv_filter_tree_new();
1320 t3
->node
= LTTV_LOGICAL_NOT
;
1321 t2
->left
= LTTV_TREE_NODE
;
1326 if(subtree
!= NULL
) { /* append subtree to current tree */
1327 t2
->left
= LTTV_TREE_NODE
;
1328 t2
->l_child
.t
= subtree
;
1330 } else { /* append a simple expression */
1331 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1332 a_field_component
= g_string_new("");
1333 t2
->left
= LTTV_TREE_LEAF
;
1334 t2
->l_child
.leaf
= a_simple_expression
;
1335 a_simple_expression
= lttv_simple_expression_new();
1341 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1342 while(t1
->right
!= LTTV_TREE_IDLE
) {
1343 g_assert(t1
->right
== LTTV_TREE_NODE
);
1346 t2
= lttv_filter_tree_new();
1347 t2
->node
= LTTV_LOGICAL_OR
;
1348 t1
->right
= LTTV_TREE_NODE
;
1350 if(not) { // add not operator to tree
1351 t3
= lttv_filter_tree_new();
1352 t3
->node
= LTTV_LOGICAL_NOT
;
1353 t2
->left
= LTTV_TREE_NODE
;
1358 if(subtree
!= NULL
) { /* append subtree to current tree */
1359 t2
->left
= LTTV_TREE_NODE
;
1360 t2
->l_child
.t
= subtree
;
1362 } else { /* append a simple expression */
1363 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1364 a_field_component
= g_string_new("");
1365 t2
->left
= LTTV_TREE_LEAF
;
1366 t2
->l_child
.leaf
= a_simple_expression
;
1367 a_simple_expression
= lttv_simple_expression_new();
1373 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1374 while(t1
->right
!= LTTV_TREE_IDLE
) {
1375 g_assert(t1
->right
== LTTV_TREE_NODE
);
1378 t2
= lttv_filter_tree_new();
1379 t2
->node
= LTTV_LOGICAL_XOR
;
1380 t1
->right
= LTTV_TREE_NODE
;
1382 if(not) { // add not operator to tree
1383 t3
= lttv_filter_tree_new();
1384 t3
->node
= LTTV_LOGICAL_NOT
;
1385 t2
->left
= LTTV_TREE_NODE
;
1390 if(subtree
!= NULL
) { /* append subtree to current tree */
1391 t2
->left
= LTTV_TREE_NODE
;
1392 t2
->l_child
.t
= subtree
;
1394 } else { /* append a simple expression */
1395 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1396 a_field_component
= g_string_new("");
1397 t2
->left
= LTTV_TREE_LEAF
;
1398 t2
->l_child
.leaf
= a_simple_expression
;
1399 a_simple_expression
= lttv_simple_expression_new();
1403 case '!': /* not, or not equal (math op) */
1405 if(filter
->expression
[i
+1] == '=') { /* != */
1406 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1407 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1408 a_field_component
= g_string_new("");
1409 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1416 case '(': /* start of parenthesis */
1420 p_nesting
++; /* incrementing parenthesis nesting value */
1421 t1
= lttv_filter_tree_new();
1422 if(not) { /* add not operator to tree */
1423 t3
= lttv_filter_tree_new();
1424 t3
->node
= LTTV_LOGICAL_NOT
;
1425 t1
->right
= LTTV_TREE_NODE
;
1429 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1432 case ')': /* end of parenthesis */
1436 p_nesting
--; /* decrementing parenthesis nesting value */
1437 if(p_nesting
<0 || tree_stack
->len
<2) {
1438 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1439 is not valid due to parenthesis incorrect use",filter
->expression
);
1443 /* there must at least be the root tree left in the array */
1444 g_assert(tree_stack
->len
>0);
1446 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1447 while(t1
->right
!= LTTV_TREE_IDLE
) {
1450 if(not) { // add not operator to tree
1452 t3
= lttv_filter_tree_new();
1453 t3
->node
= LTTV_LOGICAL_NOT
;
1454 t1
->right
= LTTV_TREE_NODE
;
1459 if(subtree
!= NULL
) { /* append subtree to current tree */
1460 t1
->right
= LTTV_TREE_NODE
;
1461 t1
->r_child
.t
= subtree
;
1462 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1463 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1464 } else { /* assign subtree as current tree */
1465 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1466 a_field_component
= g_string_new("");
1467 t1
->right
= LTTV_TREE_LEAF
;
1468 t1
->r_child
.leaf
= a_simple_expression
;
1469 a_simple_expression
= lttv_simple_expression_new();
1470 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1475 * mathematic operators
1477 case '<': /* lower, lower or equal */
1479 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1480 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1481 a_field_component
= g_string_new("");
1482 if(filter
->expression
[i
+1] == '=') { /* <= */
1484 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1485 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1488 case '>': /* higher, higher or equal */
1490 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1491 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1492 a_field_component
= g_string_new("");
1493 if(filter
->expression
[i
+1] == '=') { /* >= */
1495 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1496 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1499 case '=': /* equal */
1501 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1502 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1503 a_field_component
= g_string_new("");
1504 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1508 * Field concatening caracter
1513 * divide field expression into elements
1514 * in a_field_path array.
1516 * A dot can also be present in double values
1518 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1519 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1520 a_field_component
= g_string_new("");
1523 case ' ': /* ignore */
1524 case '\n': /* ignore */
1526 default: /* concatening current string */
1527 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1532 * Preliminary check to see
1533 * if tree was constructed correctly
1536 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1537 is not valid due to parenthesis incorrect use",filter
->expression
);
1541 if(tree_stack
->len
!= 1) /* only root tree should remain */
1545 * processing last element of expression
1547 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1548 while(t1
->right
!= LTTV_TREE_IDLE
) {
1549 g_assert(t1
->right
== LTTV_TREE_NODE
);
1552 if(not) { // add not operator to tree
1553 t3
= lttv_filter_tree_new();
1554 t3
->node
= LTTV_LOGICAL_NOT
;
1555 t1
->right
= LTTV_TREE_NODE
;
1560 if(subtree
!= NULL
) { /* add the subtree */
1561 t1
->right
= LTTV_TREE_NODE
;
1562 t1
->r_child
.t
= subtree
;
1564 } else { /* add a leaf */
1565 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1566 a_field_component
= NULL
;
1567 t1
->right
= LTTV_TREE_LEAF
;
1568 t1
->r_child
.leaf
= a_simple_expression
;
1569 a_simple_expression
= NULL
;
1573 /* free the pointer array */
1574 g_assert(a_field_path
->len
== 0);
1575 g_ptr_array_free(a_field_path
,TRUE
);
1577 /* free the tree stack -- but keep the root tree */
1578 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1579 g_ptr_array_free(tree_stack
,TRUE
);
1581 /* free the field buffer if allocated */
1582 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1584 /* free the simple expression buffer if allocated */
1585 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1587 g_assert(filter
->head
!= NULL
); /* tree should exist */
1588 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1591 gettimeofday(&endtime
, NULL
);
1593 /* Calcul du temps de l'algorithme */
1594 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1595 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1596 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1597 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1601 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1602 lttv_print_tree(filter
->head
,0) ;
1603 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1611 * @fn void lttv_filter_destroy(LttvFilter*)
1613 * Destroy the current LttvFilter
1614 * @param filter pointer to the current LttvFilter
1617 lttv_filter_destroy(LttvFilter
* filter
) {
1621 if(filter
->expression
)
1622 g_free(filter
->expression
);
1624 lttv_filter_tree_destroy(filter
->head
);
1630 * @fn LttvFilterTree* lttv_filter_tree_new()
1632 * Assign a new tree for the current expression
1634 * @return pointer of LttvFilterTree
1637 lttv_filter_tree_new() {
1638 LttvFilterTree
* tree
;
1640 tree
= g_new(LttvFilterTree
,1);
1641 tree
->node
= 0; //g_new(lttv_expression,1);
1642 tree
->left
= LTTV_TREE_IDLE
;
1643 tree
->right
= LTTV_TREE_IDLE
;
1644 tree
->r_child
.t
= NULL
;
1645 tree
->l_child
.t
= NULL
;
1651 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1653 * Append a new expression to the expression
1654 * defined in the current filter
1655 * @param filter pointer to the current LttvFilter
1656 * @param expression string that must be appended
1657 * @return Success/Failure of operation
1660 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1662 if(expression
== NULL
) return FALSE
;
1663 if(filter
== NULL
) return FALSE
;
1664 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1666 GString
* s
= g_string_new("");
1667 if(filter
->expression
!= NULL
) {
1668 g_string_append(s
,filter
->expression
);
1669 g_string_append_c(s
,'&');
1671 g_string_append(s
,expression
);
1673 g_free(filter
->expression
);
1674 filter
->expression
= g_string_free(s
,FALSE
);
1676 /* TRUE if construction of tree proceeded without errors */
1677 return lttv_filter_update(filter
);
1682 * @fn void lttv_filter_clear_expression(LttvFilter*)
1684 * Clear the filter expression from the
1685 * current filter and sets its pointer to NULL
1686 * @param filter pointer to the current LttvFilter
1689 lttv_filter_clear_expression(LttvFilter
* filter
) {
1691 if(filter
->expression
!= NULL
) {
1692 g_free(filter
->expression
);
1693 filter
->expression
= NULL
;
1699 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1701 * Destroys the tree and his sub-trees
1702 * @param tree Tree which must be destroyed
1705 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1707 if(tree
== NULL
) return;
1709 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1710 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1712 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1713 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1715 // g_free(tree->node);
1720 * Global parsing function for the current
1722 * @param t pointer to the current LttvFilterTree
1723 * @param event current LttEvent, NULL if not used
1724 * @param tracefile current LttTracefile, NULL if not used
1725 * @param trace current LttTrace, NULL if not used
1726 * @param state current LttvProcessState, NULL if not used
1727 * @param context current LttvTracefileContext, NULL if not used
1728 * @return response of filter
1731 lttv_filter_tree_parse(
1732 const LttvFilterTree
* t
,
1733 const LttEvent
* event
,
1734 const LttTracefile
* tracefile
,
1735 const LttTrace
* trace
,
1736 const LttvTracefileContext
* context
1741 * Each tree is parsed in inorder.
1742 * This way, it's possible to apply the left filter of the
1743 * tree, then decide whether or not the right branch should
1744 * be parsed depending on the linking logical operator
1746 * Each node consists in a
1747 * 1. logical operator
1748 * 2. left child ( node or simple expression )
1749 * 3. right child ( node or simple expression )
1751 * When the child is a simple expression, we must
1752 * before all determine if the expression refers to
1753 * a structure which is whithin observation ( not NULL ).
1754 * -If so, the expression is evaluated.
1755 * -If not, the result is set to TRUE since this particular
1756 * operation does not interfere with the lttv structure
1758 * The result of each simple expression will directly
1759 * affect the next branch. This way, depending on
1760 * the linking logical operator, the parser will decide
1761 * to explore or not the next branch.
1763 * -If result of left branch is 0 / FALSE
1764 * then don't explore right branch and return 0;
1765 * -If result of left branch is 1 / TRUE then explore
1767 * -If result of left branch is 1 / TRUE
1768 * then don't explore right branch and return 1;
1769 * -If result of left branch is 0 / FALSE then explore
1771 * -Result of left branch will not affect exploration of
1775 gboolean lresult
= FALSE
, rresult
= FALSE
;
1777 LttvProcessState
* state
;
1779 guint cpu
= ltt_tracefile_num(context
->tf
);
1780 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1781 state
= ts
->running_process
[cpu
];
1786 if(t
->left
== LTTV_TREE_NODE
) {
1787 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1789 else if(t
->left
== LTTV_TREE_LEAF
) {
1790 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1794 * Parse linking operator
1795 * make a cutoff if possible
1797 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1798 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1801 * Parse right branch
1803 if(t
->right
== LTTV_TREE_NODE
) {
1804 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1806 else if(t
->right
== LTTV_TREE_LEAF
) {
1807 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1812 * Apply and return the
1813 * logical link between the
1817 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1818 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1819 case LTTV_LOGICAL_NOT
:
1820 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1821 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1822 case 0: return (rresult
);
1825 * This case should never be
1826 * parsed, if so, this subtree
1835 * This function parses a particular branch of the tree
1836 * @param se pointer to the current LttvSimpleExpression
1837 * @param event current LttEvent, NULL if not used
1838 * @param tracefile current LttTracefile, NULL if not used
1839 * @param trace current LttTrace, NULL if not used
1840 * @param state current LttvProcessState, NULL if not used
1841 * @param context current LttvTracefileContext, NULL if not used
1842 * @return response of filter
1845 lttv_filter_tree_parse_branch(
1846 const LttvSimpleExpression
* se
,
1847 const LttEvent
* event
,
1848 const LttTracefile
* tracefile
,
1849 const LttTrace
* trace
,
1850 const LttvProcessState
* state
,
1851 const LttvTracefileContext
* context
) {
1856 case LTTV_FILTER_TRACE_NAME
:
1857 if(trace
== NULL
) return TRUE
;
1859 GQuark quark
= ltt_trace_name(trace
);
1860 return se
->op((gpointer
)&quark
,v
);
1863 case LTTV_FILTER_TRACEFILE_NAME
:
1864 if(tracefile
== NULL
) return TRUE
;
1866 GQuark quark
= ltt_tracefile_name(tracefile
);
1867 return se
->op((gpointer
)&quark
,v
);
1870 case LTTV_FILTER_STATE_PID
:
1871 if(state
== NULL
) return TRUE
;
1872 else return se
->op((gpointer
)&state
->pid
,v
);
1874 case LTTV_FILTER_STATE_PPID
:
1875 if(state
== NULL
) return TRUE
;
1876 else return se
->op((gpointer
)&state
->ppid
,v
);
1878 case LTTV_FILTER_STATE_CT
:
1879 if(state
== NULL
) return TRUE
;
1881 return se
->op((gpointer
)&state
->creation_time
,v
);
1884 case LTTV_FILTER_STATE_IT
:
1885 if(state
== NULL
) return TRUE
;
1887 return se
->op((gpointer
)&state
->insertion_time
,v
);
1890 case LTTV_FILTER_STATE_P_NAME
:
1892 * All 'unnamed' for the moment
1894 if(state
== NULL
) return TRUE
;
1896 GQuark quark
= state
->name
;
1897 return se
->op((gpointer
)&quark
,v
);
1900 case LTTV_FILTER_STATE_EX_MODE
:
1901 if(state
== NULL
) return TRUE
;
1902 else return se
->op((gpointer
)&state
->state
->t
,v
);
1904 case LTTV_FILTER_STATE_EX_SUBMODE
:
1905 if(state
== NULL
) return TRUE
;
1906 else return se
->op((gpointer
)&state
->state
->n
,v
);
1908 case LTTV_FILTER_STATE_P_STATUS
:
1909 if(state
== NULL
) return TRUE
;
1910 else return se
->op((gpointer
)&state
->state
->s
,v
);
1912 case LTTV_FILTER_STATE_CPU
:
1913 if(context
== NULL
) return TRUE
;
1915 /* FIXME: not sure of that one Mathieu : fixed.*/
1916 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1917 // return se->op((gpointer)&quark,v);
1918 if(state
== NULL
) return TRUE
;
1919 else return se
->op((gpointer
)&state
->cpu
,v
);
1922 case LTTV_FILTER_EVENT_NAME
:
1923 if(event
== NULL
) return TRUE
;
1926 et
= ltt_event_eventtype(event
);
1927 GQuark quark
= ltt_eventtype_name(et
);
1928 return se
->op((gpointer
)&quark
,v
);
1931 case LTTV_FILTER_EVENT_FACILITY
:
1932 if(event
== NULL
) return TRUE
;
1935 fac
= ltt_event_facility(event
);
1936 GQuark quark
= ltt_facility_name(fac
);
1937 return se
->op((gpointer
)&quark
,v
);
1940 case LTTV_FILTER_EVENT_CATEGORY
:
1942 * TODO: Not yet implemented
1946 case LTTV_FILTER_EVENT_TIME
:
1947 if(event
== NULL
) return TRUE
;
1949 LttTime time
= ltt_event_time(event
);
1950 return se
->op((gpointer
)&time
,v
);
1953 case LTTV_FILTER_EVENT_TSC
:
1954 if(event
== NULL
) return TRUE
;
1956 LttCycleCount count
= ltt_event_cycle_count(event
);
1957 return se
->op((gpointer
)&count
,v
);
1960 case LTTV_FILTER_EVENT_FIELD
:
1962 * TODO: Use the offset to
1963 * find the dynamic field
1964 * in the event struct
1969 * This case should never be
1970 * parsed, if so, the whole
1971 * filtering is cancelled
1973 g_warning("Error while parsing the filter tree");
1977 /* should never get here */
1985 * Debug function. Prints tree memory allocation.
1986 * @param t the pointer to the current LttvFilterTree
1989 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1991 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1992 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1993 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1995 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1996 g_debug("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1997 if(t
->left
== LTTV_TREE_LEAF
) {
1998 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1999 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2000 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2002 g_debug("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
2003 if(t
->right
== LTTV_TREE_LEAF
) {
2004 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2005 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2006 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2009 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2010 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2014 * @fn static void module_init()
2016 * Initializes the filter module and specific values
2018 static void module_init()
2024 * Destroys the filter module and specific values
2026 static void module_destroy()
2032 LTTV_MODULE("filter", "Filters traceset and events", \
2033 "Filters traceset and events specifically to user input", \
2034 module_init
, module_destroy
)