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/lttv.h>
78 #include <lttv/filter.h>
79 #include <ltt/trace.h>
81 #include <ltt/facility.h>
86 * @fn LttvSimpleExpression* lttv_simple_expression_new()
88 * Constructor for LttvSimpleExpression
89 * @return pointer to new LttvSimpleExpression
92 lttv_simple_expression_new() {
94 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
96 se
->field
= LTTV_FILTER_UNDEFINED
;
104 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
106 * Parse through filtering field hierarchy as specified
107 * by user. This function compares each value to
108 * predetermined quarks
109 * @param fp The field path list
110 * @param se current simple expression
111 * @return success/failure of operation
114 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
118 if(fp
->len
< 2) return FALSE
;
119 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
122 * Parse through the specified
125 * Take note however that the
126 * 'event' subfields might change
127 * depending on values specified
128 * in core.xml file. Hence, if
129 * none of the subfields in the
130 * array match the hardcoded
131 * subfields, it will be considered
134 if(!g_strcasecmp(f
->str
,"trace") ) {
139 g_string_free(f
,TRUE
);
140 f
=g_ptr_array_remove_index(fp
,0);
141 if(!g_strcasecmp(f
->str
,"name")) {
142 se
->field
= LTTV_FILTER_TRACE_NAME
;
144 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
146 * FIXME: not yet implemented !
148 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
153 g_string_free(f
,TRUE
);
154 f
=g_ptr_array_remove_index(fp
,0);
155 if(!g_strcasecmp(f
->str
,"name")) {
156 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
158 } else if(!g_strcasecmp(f
->str
,"state") ) {
163 * state.creation_time
164 * state.insertion_time
166 * state.execution_mode
167 * state.execution_submode
168 * state.process_status
171 g_string_free(f
,TRUE
);
172 f
=g_ptr_array_remove_index(fp
,0);
173 if(!g_strcasecmp(f
->str
,"pid") ) {
174 se
->field
= LTTV_FILTER_STATE_PID
;
176 else if(!g_strcasecmp(f
->str
,"ppid") ) {
177 se
->field
= LTTV_FILTER_STATE_PPID
;
179 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
180 se
->field
= LTTV_FILTER_STATE_CT
;
182 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
183 se
->field
= LTTV_FILTER_STATE_IT
;
185 else if(!g_strcasecmp(f
->str
,"process_name") ) {
186 se
->field
= LTTV_FILTER_STATE_P_NAME
;
188 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
189 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
191 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
192 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
194 else if(!g_strcasecmp(f
->str
,"process_status") ) {
195 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
197 else if(!g_strcasecmp(f
->str
,"cpu") ) {
198 se
->field
= LTTV_FILTER_STATE_CPU
;
200 } else if(!g_strcasecmp(f
->str
,"event") ) {
208 g_string_free(f
,TRUE
);
209 f
=g_ptr_array_remove_index(fp
,0);
210 if(!g_strcasecmp(f
->str
,"name") ) {
211 se
->field
= LTTV_FILTER_EVENT_NAME
;
213 else if(!g_strcasecmp(f
->str
,"facility") ) {
214 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
216 else if(!g_strcasecmp(f
->str
,"category") ) {
218 * FIXME: Category not yet functional in lttv
220 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
222 else if(!g_strcasecmp(f
->str
,"time") ) {
223 se
->field
= LTTV_FILTER_EVENT_TIME
;
225 else if(!g_strcasecmp(f
->str
,"tsc") ) {
226 se
->field
= LTTV_FILTER_EVENT_TSC
;
228 else { /* core.xml specified options */
229 se
->field
= LTTV_FILTER_EVENT_FIELD
;
232 g_warning("Unrecognized field in filter string");
235 /* free memory for last string */
236 g_string_free(f
,TRUE
);
238 /* array should be empty */
239 g_assert(fp
->len
== 0);
241 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
242 g_warning("The specified field was not recognized !");
249 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
251 * Sets the function pointer for the current
253 * @param se current simple expression
254 * @param op current operator
255 * @return success/failure of operation
258 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
264 case LTTV_FILTER_TRACE_NAME
:
265 case LTTV_FILTER_TRACEFILE_NAME
:
266 case LTTV_FILTER_STATE_P_NAME
:
267 case LTTV_FILTER_EVENT_NAME
:
268 case LTTV_FILTER_EVENT_FACILITY
:
269 case LTTV_FILTER_STATE_EX_MODE
:
270 case LTTV_FILTER_STATE_EX_SUBMODE
:
271 case LTTV_FILTER_STATE_P_STATUS
:
274 se
->op
= lttv_apply_op_eq_quark
;
277 se
->op
= lttv_apply_op_ne_quark
;
280 g_warning("Error encountered in operator assignment = or != expected");
287 case LTTV_FILTER_EVENT_TSC
:
290 se
->op
= lttv_apply_op_eq_uint64
;
293 se
->op
= lttv_apply_op_ne_uint64
;
296 se
->op
= lttv_apply_op_lt_uint64
;
299 se
->op
= lttv_apply_op_le_uint64
;
302 se
->op
= lttv_apply_op_gt_uint64
;
305 se
->op
= lttv_apply_op_ge_uint64
;
308 g_warning("Error encountered in operator assignment");
315 case LTTV_FILTER_STATE_CPU
:
316 case LTTV_FILTER_STATE_PID
:
317 case LTTV_FILTER_STATE_PPID
:
320 se
->op
= lttv_apply_op_eq_uint
;
323 se
->op
= lttv_apply_op_ne_uint
;
326 se
->op
= lttv_apply_op_lt_uint
;
329 se
->op
= lttv_apply_op_le_uint
;
332 se
->op
= lttv_apply_op_gt_uint
;
335 se
->op
= lttv_apply_op_ge_uint
;
338 g_warning("Error encountered in operator assignment");
345 * Entered as string, converted to enum
347 * can only be compared with 'equal' or 'not equal' operators
349 * unsigned int of 16 bits are used here since enums
350 * should not go over 2^16-1 values
354 // case LTTV_FIELD_EQ:
355 // se->op = lttv_apply_op_eq_uint16;
357 // case LTTV_FIELD_NE:
358 // se->op = lttv_apply_op_ne_uint16;
361 // g_warning("Error encountered in operator assignment = or != expected");
368 case LTTV_FILTER_STATE_CT
:
369 case LTTV_FILTER_STATE_IT
:
370 case LTTV_FILTER_EVENT_TIME
:
373 se
->op
= lttv_apply_op_eq_ltttime
;
376 se
->op
= lttv_apply_op_ne_ltttime
;
379 se
->op
= lttv_apply_op_lt_ltttime
;
382 se
->op
= lttv_apply_op_le_ltttime
;
385 se
->op
= lttv_apply_op_gt_ltttime
;
388 se
->op
= lttv_apply_op_ge_ltttime
;
391 g_warning("Error encountered in operator assignment");
396 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
405 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
407 * Assign the value field to the current LttvSimpleExpression
408 * @param se pointer to the current LttvSimpleExpression
409 * @param value string value for simple expression
412 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
415 gboolean is_double
= FALSE
;
416 LttTime t
= ltt_time_zero
;
422 * entered as strings, converted to Quarks
424 case LTTV_FILTER_TRACE_NAME
:
425 case LTTV_FILTER_TRACEFILE_NAME
:
426 case LTTV_FILTER_STATE_P_NAME
:
427 case LTTV_FILTER_EVENT_NAME
:
428 case LTTV_FILTER_EVENT_FACILITY
:
429 case LTTV_FILTER_STATE_EX_MODE
:
430 case LTTV_FILTER_STATE_EX_SUBMODE
:
431 case LTTV_FILTER_STATE_P_STATUS
:
432 // se->value.v_string = value;
433 se
->value
.v_uint32
= g_quark_from_string(value
);
437 * integer -- supposed to be uint64
439 case LTTV_FILTER_EVENT_TSC
:
440 se
->value
.v_uint64
= atoi(value
);
446 case LTTV_FILTER_STATE_PID
:
447 case LTTV_FILTER_STATE_PPID
:
448 case LTTV_FILTER_STATE_CPU
:
449 se
->value
.v_uint
= atoi(value
);
455 case LTTV_FILTER_STATE_CT
:
456 case LTTV_FILTER_STATE_IT
:
457 case LTTV_FILTER_EVENT_TIME
:
458 //se->value.v_double = atof(value);
460 * parsing logic could be optimised,
461 * but as for now, simpler this way
463 v
= g_string_new("");
464 for(i
=0;i
<strlen(value
);i
++) {
465 if(value
[i
] == '.') {
466 /* cannot specify number with more than one '.' */
467 if(is_double
) return FALSE
;
468 else is_double
= TRUE
;
469 t
.tv_sec
= atoi(v
->str
);
470 g_string_free(v
,TRUE
);
471 v
= g_string_new("");
472 } else g_string_append_c(v
,value
[i
]);
474 /* number can be integer or double */
475 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
476 else t
.tv_sec
= atoi(v
->str
);
478 g_string_free(v
,TRUE
);
480 se
->value
.v_ltttime
= t
;
484 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
494 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
496 * Disallocate memory for the current
498 * @param se pointer to the current LttvSimpleExpression
501 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
503 // g_free(se->value);
504 // switch(se->field) {
505 // case LTTV_FILTER_TRACE_NAME:
506 // case LTTV_FILTER_TRACEFILE_NAME:
507 // case LTTV_FILTER_STATE_P_NAME:
508 // case LTTV_FILTER_EVENT_NAME:
509 // g_free(se->value.v_string);
517 * @fn gint lttv_struct_type(gint)
519 * Finds the structure type depending
520 * on the fields in parameters
521 * @params ft Field of the current structure
522 * @return LttvStructType enum or -1 for error
525 lttv_struct_type(gint ft
) {
528 case LTTV_FILTER_TRACE_NAME
:
529 return LTTV_FILTER_TRACE
;
531 case LTTV_FILTER_TRACEFILE_NAME
:
532 return LTTV_FILTER_TRACEFILE
;
534 case LTTV_FILTER_STATE_PID
:
535 case LTTV_FILTER_STATE_PPID
:
536 case LTTV_FILTER_STATE_CT
:
537 case LTTV_FILTER_STATE_IT
:
538 case LTTV_FILTER_STATE_P_NAME
:
539 case LTTV_FILTER_STATE_EX_MODE
:
540 case LTTV_FILTER_STATE_EX_SUBMODE
:
541 case LTTV_FILTER_STATE_P_STATUS
:
542 case LTTV_FILTER_STATE_CPU
:
543 return LTTV_FILTER_STATE
;
545 case LTTV_FILTER_EVENT_NAME
:
546 case LTTV_FILTER_EVENT_FACILITY
:
547 case LTTV_FILTER_EVENT_CATEGORY
:
548 case LTTV_FILTER_EVENT_TIME
:
549 case LTTV_FILTER_EVENT_TSC
:
550 case LTTV_FILTER_EVENT_FIELD
:
551 return LTTV_FILTER_EVENT
;
559 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
561 * Applies the 'equal' operator to the
562 * specified structure and value
563 * @param v1 left member of comparison
564 * @param v2 right member of comparison
565 * @return success/failure of operation
567 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
569 guint
* r
= (guint
*) v1
;
570 return (*r
== v2
.v_uint
);
575 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
577 * Applies the 'equal' operator to the
578 * specified structure and value
579 * @param v1 left member of comparison
580 * @param v2 right member of comparison
581 * @return success/failure of operation
583 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
585 guint64
* r
= (guint64
*) v1
;
586 return (*r
== v2
.v_uint64
);
591 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
593 * Applies the 'equal' operator to the
594 * specified structure and value
595 * @param v1 left member of comparison
596 * @param v2 right member of comparison
597 * @return success/failure of operation
599 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
600 guint32
* r
= (guint32
*) v1
;
601 return (*r
== v2
.v_uint32
);
605 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
607 * Applies the 'equal' operator to the
608 * specified structure and value
609 * @param v1 left member of comparison
610 * @param v2 right member of comparison
611 * @return success/failure of operation
613 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
614 guint16
* r
= (guint16
*) v1
;
615 return (*r
== v2
.v_uint16
);
619 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
621 * Applies the 'equal' operator to the
622 * specified structure and value
623 * @param v1 left member of comparison
624 * @param v2 right member of comparison
625 * @return success/failure of operation
627 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
628 double* r
= (double*) v1
;
629 return (*r
== v2
.v_double
);
633 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
635 * Applies the 'equal' operator to the
636 * specified structure and value
637 * @param v1 left member of comparison
638 * @param v2 right member of comparison
639 * @return success/failure of operation
641 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
642 char* r
= (char*) v1
;
643 return (!g_strcasecmp(r
,v2
.v_string
));
647 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
649 * Applies the 'equal' operator to the
650 * specified structure and value
651 * @param v1 left member of comparison
652 * @param v2 right member of comparison
653 * @return success/failure of operation
655 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
656 GQuark
* r
= (GQuark
*) v1
;
657 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
658 return (*r
== v2
.v_uint32
);
662 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
664 * Applies the 'equal' operator to the
665 * specified structure and value
666 * @param v1 left member of comparison
667 * @param v2 right member of comparison
668 * @return success/failure of operation
670 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
671 LttTime
* r
= (LttTime
*) v1
;
672 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
676 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
678 * Applies the 'not equal' operator to the
679 * specified structure and value
680 * @param v1 left member of comparison
681 * @param v2 right member of comparison
682 * @return success/failure of operation
684 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
685 guint
* r
= (guint
*) v1
;
686 return (*r
!= v2
.v_uint
);
690 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
692 * Applies the 'not equal' operator to the
693 * specified structure and value
694 * @param v1 left member of comparison
695 * @param v2 right member of comparison
696 * @return success/failure of operation
698 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
699 guint64
* r
= (guint64
*) v1
;
700 return (*r
!= v2
.v_uint64
);
704 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
706 * Applies the 'not equal' operator to the
707 * specified structure and value
708 * @param v1 left member of comparison
709 * @param v2 right member of comparison
710 * @return success/failure of operation
712 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
713 guint32
* r
= (guint32
*) v1
;
714 return (*r
!= v2
.v_uint32
);
718 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
720 * Applies the 'not equal' operator to the
721 * specified structure and value
722 * @param v1 left member of comparison
723 * @param v2 right member of comparison
724 * @return success/failure of operation
726 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
727 guint16
* r
= (guint16
*) v1
;
728 return (*r
!= v2
.v_uint16
);
732 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
734 * Applies the 'not equal' operator to the
735 * specified structure and value
736 * @param v1 left member of comparison
737 * @param v2 right member of comparison
738 * @return success/failure of operation
740 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
741 double* r
= (double*) v1
;
742 return (*r
!= v2
.v_double
);
746 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
748 * Applies the 'not equal' operator to the
749 * specified structure and value
750 * @param v1 left member of comparison
751 * @param v2 right member of comparison
752 * @return success/failure of operation
754 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
755 char* r
= (char*) v1
;
756 return (g_strcasecmp(r
,v2
.v_string
));
760 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
762 * Applies the 'not equal' operator to the
763 * specified structure and value
764 * @param v1 left member of comparison
765 * @param v2 right member of comparison
766 * @return success/failure of operation
768 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
769 GQuark
* r
= (GQuark
*) v1
;
770 return (*r
!= v2
.v_uint32
);
775 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
777 * Applies the 'not equal' operator to the
778 * specified structure and value
779 * @param v1 left member of comparison
780 * @param v2 right member of comparison
781 * @return success/failure of operation
783 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
784 LttTime
* r
= (LttTime
*) v1
;
785 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
789 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
791 * Applies the 'lower than' operator to the
792 * specified structure and value
793 * @param v1 left member of comparison
794 * @param v2 right member of comparison
795 * @return success/failure of operation
797 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
798 guint
* r
= (guint
*) v1
;
799 return (*r
< v2
.v_uint
);
803 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
805 * Applies the 'lower than' operator to the
806 * specified structure and value
807 * @param v1 left member of comparison
808 * @param v2 right member of comparison
809 * @return success/failure of operation
811 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
812 guint64
* r
= (guint64
*) v1
;
813 return (*r
< v2
.v_uint64
);
817 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
819 * Applies the 'lower than' operator to the
820 * specified structure and value
821 * @param v1 left member of comparison
822 * @param v2 right member of comparison
823 * @return success/failure of operation
825 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
826 guint32
* r
= (guint32
*) v1
;
827 return (*r
< v2
.v_uint32
);
831 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
833 * Applies the 'lower than' operator to the
834 * specified structure and value
835 * @param v1 left member of comparison
836 * @param v2 right member of comparison
837 * @return success/failure of operation
839 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
840 guint16
* r
= (guint16
*) v1
;
841 return (*r
< v2
.v_uint16
);
845 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
847 * Applies the 'lower than' operator to the
848 * specified structure and value
849 * @param v1 left member of comparison
850 * @param v2 right member of comparison
851 * @return success/failure of operation
853 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
854 double* r
= (double*) v1
;
855 return (*r
< v2
.v_double
);
859 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
861 * Applies the 'lower than' operator to the
862 * specified structure and value
863 * @param v1 left member of comparison
864 * @param v2 right member of comparison
865 * @return success/failure of operation
867 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
868 LttTime
* r
= (LttTime
*) v1
;
869 // 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)));
870 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
874 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
876 * Applies the 'lower or equal' operator to the
877 * specified structure and value
878 * @param v1 left member of comparison
879 * @param v2 right member of comparison
880 * @return success/failure of operation
882 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
883 guint
* r
= (guint
*) v1
;
884 return (*r
<= v2
.v_uint
);
888 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
890 * Applies the 'lower or equal' operator to the
891 * specified structure and value
892 * @param v1 left member of comparison
893 * @param v2 right member of comparison
894 * @return success/failure of operation
896 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
897 guint64
* r
= (guint64
*) v1
;
898 return (*r
<= v2
.v_uint64
);
902 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
904 * Applies the 'lower or equal' operator to the
905 * specified structure and value
906 * @param v1 left member of comparison
907 * @param v2 right member of comparison
908 * @return success/failure of operation
910 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
911 guint32
* r
= (guint32
*) v1
;
912 return (*r
<= v2
.v_uint32
);
916 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
918 * Applies the 'lower or equal' operator to the
919 * specified structure and value
920 * @param v1 left member of comparison
921 * @param v2 right member of comparison
922 * @return success/failure of operation
924 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
925 guint16
* r
= (guint16
*) v1
;
926 return (*r
<= v2
.v_uint16
);
930 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
932 * Applies the 'lower or equal' operator to the
933 * specified structure and value
934 * @param v1 left member of comparison
935 * @param v2 right member of comparison
936 * @return success/failure of operation
938 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
939 double* r
= (double*) v1
;
940 return (*r
<= v2
.v_double
);
944 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
946 * Applies the 'lower or equal' operator to the
947 * specified structure and value
948 * @param v1 left member of comparison
949 * @param v2 right member of comparison
950 * @return success/failure of operation
952 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
953 LttTime
* r
= (LttTime
*) v1
;
954 // 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)));
955 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
960 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
962 * Applies the 'greater than' operator to the
963 * specified structure and value
964 * @param v1 left member of comparison
965 * @param v2 right member of comparison
966 * @return success/failure of operation
968 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
969 guint
* r
= (guint
*) v1
;
970 return (*r
> v2
.v_uint
);
974 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
976 * Applies the 'greater than' operator to the
977 * specified structure and value
978 * @param v1 left member of comparison
979 * @param v2 right member of comparison
980 * @return success/failure of operation
982 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
983 guint64
* r
= (guint64
*) v1
;
984 return (*r
> v2
.v_uint64
);
988 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
990 * Applies the 'greater than' operator to the
991 * specified structure and value
992 * @param v1 left member of comparison
993 * @param v2 right member of comparison
994 * @return success/failure of operation
996 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
997 guint32
* r
= (guint32
*) v1
;
998 return (*r
> v2
.v_uint32
);
1002 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1004 * Applies the 'greater than' operator to the
1005 * specified structure and value
1006 * @param v1 left member of comparison
1007 * @param v2 right member of comparison
1008 * @return success/failure of operation
1010 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1011 guint16
* r
= (guint16
*) v1
;
1012 return (*r
> v2
.v_uint16
);
1016 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1018 * Applies the 'greater than' operator to the
1019 * specified structure and value
1020 * @param v1 left member of comparison
1021 * @param v2 right member of comparison
1022 * @return success/failure of operation
1024 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1025 double* r
= (double*) v1
;
1026 return (*r
> v2
.v_double
);
1030 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1032 * Applies the 'greater than' operator to the
1033 * specified structure and value
1034 * @param v1 left member of comparison
1035 * @param v2 right member of comparison
1036 * @return success/failure of operation
1038 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1039 LttTime
* r
= (LttTime
*) v1
;
1040 // 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)));
1041 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1045 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1047 * Applies the 'greater or equal' operator to the
1048 * specified structure and value
1049 * @param v1 left member of comparison
1050 * @param v2 right member of comparison
1051 * @return success/failure of operation
1053 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1054 guint
* r
= (guint
*) v1
;
1055 return (*r
>= v2
.v_uint
);
1059 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1061 * Applies the 'greater or equal' operator to the
1062 * specified structure and value
1063 * @param v1 left member of comparison
1064 * @param v2 right member of comparison
1065 * @return success/failure of operation
1067 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1068 guint64
* r
= (guint64
*) v1
;
1069 return (*r
>= v2
.v_uint64
);
1073 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1075 * Applies the 'greater or equal' operator to the
1076 * specified structure and value
1077 * @param v1 left member of comparison
1078 * @param v2 right member of comparison
1079 * @return success/failure of operation
1081 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1082 guint32
* r
= (guint32
*) v1
;
1083 return (*r
>= v2
.v_uint32
);
1087 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1089 * Applies the 'greater or equal' operator to the
1090 * specified structure and value
1091 * @param v1 left member of comparison
1092 * @param v2 right member of comparison
1093 * @return success/failure of operation
1095 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1096 guint16
* r
= (guint16
*) v1
;
1097 return (*r
>= v2
.v_uint16
);
1101 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1103 * Applies the 'greater or equal' operator to the
1104 * specified structure and value
1105 * @param v1 left member of comparison
1106 * @param v2 right member of comparison
1107 * @return success/failure of operation
1109 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1110 double* r
= (double*) v1
;
1111 return (*r
>= v2
.v_double
);
1115 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1117 * Applies the 'greater or equal' operator to the
1118 * specified structure and value
1119 * @param v1 left member of comparison
1120 * @param v2 right member of comparison
1121 * @return success/failure of operation
1123 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1124 LttTime
* r
= (LttTime
*) v1
;
1125 // 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)));
1126 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1132 * Makes a copy of the current filter tree
1133 * @param tree pointer to the current tree
1134 * @return new copy of the filter tree
1137 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1139 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1141 newtree
->node
= tree
->node
;
1143 newtree
->left
= tree
->left
;
1144 if(newtree
->left
== LTTV_TREE_NODE
) {
1145 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1146 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1147 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1148 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1149 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1150 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1151 /* FIXME: special case for string copy ! */
1152 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1155 newtree
->right
= tree
->right
;
1156 if(newtree
->right
== LTTV_TREE_NODE
) {
1157 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1158 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1159 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1160 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1161 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1162 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1163 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1171 * Makes a copy of the current filter
1172 * @param filter pointer to the current filter
1173 * @return new copy of the filter
1176 lttv_filter_clone(const LttvFilter
* filter
) {
1178 if(!filter
) return NULL
;
1180 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1182 strcpy(newfilter
->expression
,filter
->expression
);
1184 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1192 * @fn LttvFilter* lttv_filter_new()
1194 * Creates a new LttvFilter
1195 * @return the current LttvFilter or NULL if error
1200 LttvFilter
* filter
= g_new(LttvFilter
,1);
1201 filter
->expression
= NULL
;
1202 filter
->head
= NULL
;
1209 * @fn gboolean lttv_filter_update(LttvFilter*)
1211 * Updates the current LttvFilter by building
1212 * its tree based upon the expression string
1213 * @param filter pointer to the current LttvFilter
1214 * @return Failure/Success of operation
1217 lttv_filter_update(LttvFilter
* filter
) {
1219 // g_print("filter::lttv_filter_new()\n"); /* debug */
1221 if(filter
->expression
== NULL
) return FALSE
;
1225 p_nesting
=0, /* parenthesis nesting value */
1230 *tree
= lttv_filter_tree_new(), /* main tree */
1231 *subtree
= NULL
, /* buffer for subtrees */
1232 *t1
, /* buffer #1 */
1233 *t2
, /* buffer #2 */
1234 *t3
; /* buffer #3 */
1238 * If the tree already exists,
1239 * destroy it and build a new one
1241 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1242 filter
->head
= NULL
; /* will be assigned at the end */
1246 * each element of the list
1247 * is a sub tree created
1248 * by the use of parenthesis in the
1249 * global expression. The final tree
1250 * will be the one left at the root of
1253 GPtrArray
*tree_stack
= g_ptr_array_new();
1254 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1256 /* temporary values */
1257 GString
*a_field_component
= g_string_new("");
1258 GPtrArray
*a_field_path
= g_ptr_array_new();
1260 /* simple expression buffer */
1261 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1264 * Parse entire expression and construct
1265 * the binary tree. There are two steps
1266 * in browsing that string
1267 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1268 * 2. finding simple expressions
1269 * - field path ( separated by dots )
1270 * - op ( >, <, =, >=, <=, !=)
1271 * - value ( integer, string ... )
1272 * To spare computing time, the whole
1273 * string is parsed in this loop for a
1274 * O(n) complexity order.
1276 * When encountering logical op &,|,^
1277 * 1. parse the last value if any
1278 * 2. create a new tree
1279 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1280 * 4. concatenate this tree with the current tree on top of the stack
1281 * When encountering math ops >,>=,<,<=,=,!=
1282 * 1. add to op to the simple expression
1283 * 2. concatenate last field component to field path
1284 * When encountering concatening ops .
1285 * 1. concatenate last field component to field path
1286 * When encountering opening parenthesis (,{,[
1287 * 1. create a new subtree on top of tree stack
1288 * When encountering closing parenthesis ),},]
1289 * 1. add the expression on right child of the current tree
1290 * 2. the subtree is completed, allocate a new subtree
1291 * 3. pop the tree value from the tree stack
1295 struct timeval starttime
;
1296 struct timeval endtime
;
1297 gettimeofday(&starttime
, NULL
);
1300 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1302 // g_print("%c\n ",filter->expression[i]);
1304 switch(filter
->expression
[i
]) {
1310 /* get current tree in tree stack */
1311 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1313 /* get current node at absolute right */
1314 while(t1
->right
!= LTTV_TREE_IDLE
) {
1315 g_assert(t1
->right
== LTTV_TREE_NODE
);
1318 t2
= lttv_filter_tree_new();
1319 t2
->node
= LTTV_LOGICAL_AND
;
1320 t1
->right
= LTTV_TREE_NODE
;
1322 if(not) { /* add not operator to tree */
1323 t3
= lttv_filter_tree_new();
1324 t3
->node
= LTTV_LOGICAL_NOT
;
1325 t2
->left
= LTTV_TREE_NODE
;
1330 if(subtree
!= NULL
) { /* append subtree to current tree */
1331 t2
->left
= LTTV_TREE_NODE
;
1332 t2
->l_child
.t
= subtree
;
1334 } else { /* append a simple expression */
1335 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1336 a_field_component
= g_string_new("");
1337 t2
->left
= LTTV_TREE_LEAF
;
1338 t2
->l_child
.leaf
= a_simple_expression
;
1339 a_simple_expression
= lttv_simple_expression_new();
1345 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1346 while(t1
->right
!= LTTV_TREE_IDLE
) {
1347 g_assert(t1
->right
== LTTV_TREE_NODE
);
1350 t2
= lttv_filter_tree_new();
1351 t2
->node
= LTTV_LOGICAL_OR
;
1352 t1
->right
= LTTV_TREE_NODE
;
1354 if(not) { // add not operator to tree
1355 t3
= lttv_filter_tree_new();
1356 t3
->node
= LTTV_LOGICAL_NOT
;
1357 t2
->left
= LTTV_TREE_NODE
;
1362 if(subtree
!= NULL
) { /* append subtree to current tree */
1363 t2
->left
= LTTV_TREE_NODE
;
1364 t2
->l_child
.t
= subtree
;
1366 } else { /* append a simple expression */
1367 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1368 a_field_component
= g_string_new("");
1369 t2
->left
= LTTV_TREE_LEAF
;
1370 t2
->l_child
.leaf
= a_simple_expression
;
1371 a_simple_expression
= lttv_simple_expression_new();
1377 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1378 while(t1
->right
!= LTTV_TREE_IDLE
) {
1379 g_assert(t1
->right
== LTTV_TREE_NODE
);
1382 t2
= lttv_filter_tree_new();
1383 t2
->node
= LTTV_LOGICAL_XOR
;
1384 t1
->right
= LTTV_TREE_NODE
;
1386 if(not) { // add not operator to tree
1387 t3
= lttv_filter_tree_new();
1388 t3
->node
= LTTV_LOGICAL_NOT
;
1389 t2
->left
= LTTV_TREE_NODE
;
1394 if(subtree
!= NULL
) { /* append subtree to current tree */
1395 t2
->left
= LTTV_TREE_NODE
;
1396 t2
->l_child
.t
= subtree
;
1398 } else { /* append a simple expression */
1399 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1400 a_field_component
= g_string_new("");
1401 t2
->left
= LTTV_TREE_LEAF
;
1402 t2
->l_child
.leaf
= a_simple_expression
;
1403 a_simple_expression
= lttv_simple_expression_new();
1407 case '!': /* not, or not equal (math op) */
1409 if(filter
->expression
[i
+1] == '=') { /* != */
1410 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1411 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1412 a_field_component
= g_string_new("");
1413 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1420 case '(': /* start of parenthesis */
1424 p_nesting
++; /* incrementing parenthesis nesting value */
1425 t1
= lttv_filter_tree_new();
1426 if(not) { /* add not operator to tree */
1427 t3
= lttv_filter_tree_new();
1428 t3
->node
= LTTV_LOGICAL_NOT
;
1429 t1
->right
= LTTV_TREE_NODE
;
1433 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1436 case ')': /* end of parenthesis */
1440 p_nesting
--; /* decrementing parenthesis nesting value */
1441 if(p_nesting
<0 || tree_stack
->len
<2) {
1442 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1443 is not valid due to parenthesis incorrect use",filter
->expression
);
1447 /* there must at least be the root tree left in the array */
1448 g_assert(tree_stack
->len
>0);
1450 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1451 while(t1
->right
!= LTTV_TREE_IDLE
) {
1454 if(not) { // add not operator to tree
1456 t3
= lttv_filter_tree_new();
1457 t3
->node
= LTTV_LOGICAL_NOT
;
1458 t1
->right
= LTTV_TREE_NODE
;
1463 if(subtree
!= NULL
) { /* append subtree to current tree */
1464 t1
->right
= LTTV_TREE_NODE
;
1465 t1
->r_child
.t
= subtree
;
1466 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1467 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1468 } else { /* assign subtree as current tree */
1469 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1470 a_field_component
= g_string_new("");
1471 t1
->right
= LTTV_TREE_LEAF
;
1472 t1
->r_child
.leaf
= a_simple_expression
;
1473 a_simple_expression
= lttv_simple_expression_new();
1474 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1479 * mathematic operators
1481 case '<': /* lower, lower or equal */
1483 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1484 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1485 a_field_component
= g_string_new("");
1486 if(filter
->expression
[i
+1] == '=') { /* <= */
1488 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1489 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1492 case '>': /* higher, higher or equal */
1494 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1495 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1496 a_field_component
= g_string_new("");
1497 if(filter
->expression
[i
+1] == '=') { /* >= */
1499 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1500 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1503 case '=': /* equal */
1505 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1506 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1507 a_field_component
= g_string_new("");
1508 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1512 * Field concatening caracter
1517 * divide field expression into elements
1518 * in a_field_path array.
1520 * A dot can also be present in double values
1522 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1523 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1524 a_field_component
= g_string_new("");
1527 case ' ': /* ignore */
1528 case '\n': /* ignore */
1530 default: /* concatening current string */
1531 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1536 * Preliminary check to see
1537 * if tree was constructed correctly
1540 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1541 is not valid due to parenthesis incorrect use",filter
->expression
);
1545 if(tree_stack
->len
!= 1) /* only root tree should remain */
1549 * processing last element of expression
1551 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1552 while(t1
->right
!= LTTV_TREE_IDLE
) {
1553 g_assert(t1
->right
== LTTV_TREE_NODE
);
1556 if(not) { // add not operator to tree
1557 t3
= lttv_filter_tree_new();
1558 t3
->node
= LTTV_LOGICAL_NOT
;
1559 t1
->right
= LTTV_TREE_NODE
;
1564 if(subtree
!= NULL
) { /* add the subtree */
1565 t1
->right
= LTTV_TREE_NODE
;
1566 t1
->r_child
.t
= subtree
;
1568 } else { /* add a leaf */
1569 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1570 a_field_component
= NULL
;
1571 t1
->right
= LTTV_TREE_LEAF
;
1572 t1
->r_child
.leaf
= a_simple_expression
;
1573 a_simple_expression
= NULL
;
1577 /* free the pointer array */
1578 g_assert(a_field_path
->len
== 0);
1579 g_ptr_array_free(a_field_path
,TRUE
);
1581 /* free the tree stack -- but keep the root tree */
1582 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1583 g_ptr_array_free(tree_stack
,TRUE
);
1585 /* free the field buffer if allocated */
1586 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1588 /* free the simple expression buffer if allocated */
1589 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1591 g_assert(filter
->head
!= NULL
); /* tree should exist */
1592 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1595 gettimeofday(&endtime
, NULL
);
1597 /* Calcul du temps de l'algorithme */
1598 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1599 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1600 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1601 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1605 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1606 lttv_print_tree(filter
->head
,0) ;
1607 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1615 * @fn void lttv_filter_destroy(LttvFilter*)
1617 * Destroy the current LttvFilter
1618 * @param filter pointer to the current LttvFilter
1621 lttv_filter_destroy(LttvFilter
* filter
) {
1625 if(filter
->expression
)
1626 g_free(filter
->expression
);
1628 lttv_filter_tree_destroy(filter
->head
);
1634 * @fn LttvFilterTree* lttv_filter_tree_new()
1636 * Assign a new tree for the current expression
1638 * @return pointer of LttvFilterTree
1641 lttv_filter_tree_new() {
1642 LttvFilterTree
* tree
;
1644 tree
= g_new(LttvFilterTree
,1);
1645 tree
->node
= 0; //g_new(lttv_expression,1);
1646 tree
->left
= LTTV_TREE_IDLE
;
1647 tree
->right
= LTTV_TREE_IDLE
;
1648 tree
->r_child
.t
= NULL
;
1649 tree
->l_child
.t
= NULL
;
1655 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1657 * Append a new expression to the expression
1658 * defined in the current filter
1659 * @param filter pointer to the current LttvFilter
1660 * @param expression string that must be appended
1661 * @return Success/Failure of operation
1664 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1666 if(expression
== NULL
) return FALSE
;
1667 if(filter
== NULL
) return FALSE
;
1668 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1670 GString
* s
= g_string_new("");
1671 if(filter
->expression
!= NULL
) {
1672 g_string_append(s
,filter
->expression
);
1673 g_string_append_c(s
,'&');
1675 g_string_append(s
,expression
);
1677 g_free(filter
->expression
);
1678 filter
->expression
= g_string_free(s
,FALSE
);
1680 /* TRUE if construction of tree proceeded without errors */
1681 return lttv_filter_update(filter
);
1686 * @fn void lttv_filter_clear_expression(LttvFilter*)
1688 * Clear the filter expression from the
1689 * current filter and sets its pointer to NULL
1690 * @param filter pointer to the current LttvFilter
1693 lttv_filter_clear_expression(LttvFilter
* filter
) {
1695 if(filter
->expression
!= NULL
) {
1696 g_free(filter
->expression
);
1697 filter
->expression
= NULL
;
1703 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1705 * Destroys the tree and his sub-trees
1706 * @param tree Tree which must be destroyed
1709 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1711 if(tree
== NULL
) return;
1713 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1714 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1716 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1717 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1719 // g_free(tree->node);
1724 * Global parsing function for the current
1726 * @param t pointer to the current LttvFilterTree
1727 * @param event current LttEvent, NULL if not used
1728 * @param tracefile current LttTracefile, NULL if not used
1729 * @param trace current LttTrace, NULL if not used
1730 * @param state current LttvProcessState, NULL if not used
1731 * @param context current LttvTracefileContext, NULL if not used
1732 * @return response of filter
1735 lttv_filter_tree_parse(
1736 const LttvFilterTree
* t
,
1737 const LttEvent
* event
,
1738 const LttTracefile
* tracefile
,
1739 const LttTrace
* trace
,
1740 const LttvTracefileContext
* context
1745 * Each tree is parsed in inorder.
1746 * This way, it's possible to apply the left filter of the
1747 * tree, then decide whether or not the right branch should
1748 * be parsed depending on the linking logical operator
1750 * Each node consists in a
1751 * 1. logical operator
1752 * 2. left child ( node or simple expression )
1753 * 3. right child ( node or simple expression )
1755 * When the child is a simple expression, we must
1756 * before all determine if the expression refers to
1757 * a structure which is whithin observation ( not NULL ).
1758 * -If so, the expression is evaluated.
1759 * -If not, the result is set to TRUE since this particular
1760 * operation does not interfere with the lttv structure
1762 * The result of each simple expression will directly
1763 * affect the next branch. This way, depending on
1764 * the linking logical operator, the parser will decide
1765 * to explore or not the next branch.
1767 * -If result of left branch is 0 / FALSE
1768 * then don't explore right branch and return 0;
1769 * -If result of left branch is 1 / TRUE then explore
1771 * -If result of left branch is 1 / TRUE
1772 * then don't explore right branch and return 1;
1773 * -If result of left branch is 0 / FALSE then explore
1775 * -Result of left branch will not affect exploration of
1779 gboolean lresult
= FALSE
, rresult
= FALSE
;
1781 LttvProcessState
* state
;
1783 guint cpu
= ltt_tracefile_num(context
->tf
);
1784 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1785 state
= ts
->running_process
[cpu
];
1790 if(t
->left
== LTTV_TREE_NODE
) {
1791 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1793 else if(t
->left
== LTTV_TREE_LEAF
) {
1794 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1798 * Parse linking operator
1799 * make a cutoff if possible
1801 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1802 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1805 * Parse right branch
1807 if(t
->right
== LTTV_TREE_NODE
) {
1808 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1810 else if(t
->right
== LTTV_TREE_LEAF
) {
1811 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1816 * Apply and return the
1817 * logical link between the
1821 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1822 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1823 case LTTV_LOGICAL_NOT
:
1824 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1825 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1826 case 0: return (rresult
);
1829 * This case should never be
1830 * parsed, if so, this subtree
1839 * This function parses a particular branch of the tree
1840 * @param se pointer to the current LttvSimpleExpression
1841 * @param event current LttEvent, NULL if not used
1842 * @param tracefile current LttTracefile, NULL if not used
1843 * @param trace current LttTrace, NULL if not used
1844 * @param state current LttvProcessState, NULL if not used
1845 * @param context current LttvTracefileContext, NULL if not used
1846 * @return response of filter
1849 lttv_filter_tree_parse_branch(
1850 const LttvSimpleExpression
* se
,
1851 const LttEvent
* event
,
1852 const LttTracefile
* tracefile
,
1853 const LttTrace
* trace
,
1854 const LttvProcessState
* state
,
1855 const LttvTracefileContext
* context
) {
1860 case LTTV_FILTER_TRACE_NAME
:
1861 if(trace
== NULL
) return TRUE
;
1863 GQuark quark
= ltt_trace_name(trace
);
1864 return se
->op((gpointer
)&quark
,v
);
1867 case LTTV_FILTER_TRACEFILE_NAME
:
1868 if(tracefile
== NULL
) return TRUE
;
1870 GQuark quark
= ltt_tracefile_name(tracefile
);
1871 return se
->op((gpointer
)&quark
,v
);
1874 case LTTV_FILTER_STATE_PID
:
1875 if(state
== NULL
) return TRUE
;
1876 else return se
->op((gpointer
)&state
->pid
,v
);
1878 case LTTV_FILTER_STATE_PPID
:
1879 if(state
== NULL
) return TRUE
;
1880 else return se
->op((gpointer
)&state
->ppid
,v
);
1882 case LTTV_FILTER_STATE_CT
:
1883 if(state
== NULL
) return TRUE
;
1885 return se
->op((gpointer
)&state
->creation_time
,v
);
1888 case LTTV_FILTER_STATE_IT
:
1889 if(state
== NULL
) return TRUE
;
1891 return se
->op((gpointer
)&state
->insertion_time
,v
);
1894 case LTTV_FILTER_STATE_P_NAME
:
1896 * All 'unnamed' for the moment
1898 if(state
== NULL
) return TRUE
;
1900 GQuark quark
= state
->name
;
1901 return se
->op((gpointer
)&quark
,v
);
1904 case LTTV_FILTER_STATE_EX_MODE
:
1905 if(state
== NULL
) return TRUE
;
1906 else return se
->op((gpointer
)&state
->state
->t
,v
);
1908 case LTTV_FILTER_STATE_EX_SUBMODE
:
1909 if(state
== NULL
) return TRUE
;
1910 else return se
->op((gpointer
)&state
->state
->n
,v
);
1912 case LTTV_FILTER_STATE_P_STATUS
:
1913 if(state
== NULL
) return TRUE
;
1914 else return se
->op((gpointer
)&state
->state
->s
,v
);
1916 case LTTV_FILTER_STATE_CPU
:
1917 if(context
== NULL
) return TRUE
;
1919 /* FIXME: not sure of that one Mathieu : fixed.*/
1920 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1921 // return se->op((gpointer)&quark,v);
1922 if(state
== NULL
) return TRUE
;
1923 else return se
->op((gpointer
)&state
->cpu
,v
);
1926 case LTTV_FILTER_EVENT_NAME
:
1927 if(event
== NULL
) return TRUE
;
1930 et
= ltt_event_eventtype(event
);
1931 GQuark quark
= ltt_eventtype_name(et
);
1932 return se
->op((gpointer
)&quark
,v
);
1935 case LTTV_FILTER_EVENT_FACILITY
:
1936 if(event
== NULL
) return TRUE
;
1939 fac
= ltt_event_facility(event
);
1940 GQuark quark
= ltt_facility_name(fac
);
1941 return se
->op((gpointer
)&quark
,v
);
1944 case LTTV_FILTER_EVENT_CATEGORY
:
1946 * TODO: Not yet implemented
1950 case LTTV_FILTER_EVENT_TIME
:
1951 if(event
== NULL
) return TRUE
;
1953 LttTime time
= ltt_event_time(event
);
1954 return se
->op((gpointer
)&time
,v
);
1957 case LTTV_FILTER_EVENT_TSC
:
1958 if(event
== NULL
) return TRUE
;
1960 LttCycleCount count
= ltt_event_cycle_count(event
);
1961 return se
->op((gpointer
)&count
,v
);
1964 case LTTV_FILTER_EVENT_FIELD
:
1966 * TODO: Use the offset to
1967 * find the dynamic field
1968 * in the event struct
1973 * This case should never be
1974 * parsed, if so, the whole
1975 * filtering is cancelled
1977 g_warning("Error while parsing the filter tree");
1981 /* should never get here */
1989 * Debug function. Prints tree memory allocation.
1990 * @param t the pointer to the current LttvFilterTree
1993 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1995 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1996 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1997 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1999 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2000 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"));
2001 if(t
->left
== LTTV_TREE_LEAF
) {
2002 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2003 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2004 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2006 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"));
2007 if(t
->right
== LTTV_TREE_LEAF
) {
2008 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2009 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2010 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2013 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2014 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2018 * @fn static void module_init()
2020 * Initializes the filter module and specific values
2022 static void module_init()
2028 * Destroys the filter module and specific values
2030 static void module_destroy()
2036 LTTV_MODULE("filter", "Filters traceset and events", \
2037 "Filters traceset and events specifically to user input", \
2038 module_init
, module_destroy
)