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)
42 * | |->"sub-field name"
44 * | |->"leaf-field name" (field type)
46 * | |->name (String, converted to GQuark)
48 * | |->name (String, converted to GQuark)
52 * |->creation_time (LttTime)
53 * |->insertion_time (LttTime)
54 * |->process_name (String, converted to GQuark)
55 * |->execution_mode (LttvExecutionMode)
56 * |->execution_submode (LttvExecutionSubmode)
57 * |->process_status (LttvProcessStatus)
64 * - refine switch of expression in multiple uses functions
65 * - remove the idle expressions in the tree
78 #include <lttv/lttv.h>
79 #include <lttv/filter.h>
80 #include <ltt/trace.h>
82 #include <ltt/facility.h>
87 * @fn LttvSimpleExpression* lttv_simple_expression_new()
89 * Constructor for LttvSimpleExpression
90 * @return pointer to new LttvSimpleExpression
93 lttv_simple_expression_new() {
95 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
97 se
->field
= LTTV_FILTER_UNDEFINED
;
105 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
107 * Parse through filtering field hierarchy as specified
108 * by user. This function compares each value to
109 * predetermined quarks
110 * @param fp The field path list
111 * @param se current simple expression
112 * @return success/failure of operation
115 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
119 if(fp
->len
< 2) return FALSE
;
120 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
123 * Parse through the specified
126 * Take note however that the
127 * 'event' subfields might change
128 * depending on values specified
129 * in core.xml file. Hence, if
130 * none of the subfields in the
131 * array match the hardcoded
132 * subfields, it will be considered
135 if(!g_strcasecmp(f
->str
,"trace") ) {
140 g_string_free(f
,TRUE
);
141 f
=g_ptr_array_remove_index(fp
,0);
142 if(!g_strcasecmp(f
->str
,"name")) {
143 se
->field
= LTTV_FILTER_TRACE_NAME
;
145 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
147 * FIXME: not yet implemented !
149 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
154 g_string_free(f
,TRUE
);
155 f
=g_ptr_array_remove_index(fp
,0);
156 if(!g_strcasecmp(f
->str
,"name")) {
157 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
159 } else if(!g_strcasecmp(f
->str
,"state") ) {
164 * state.creation_time
165 * state.insertion_time
167 * state.execution_mode
168 * state.execution_submode
169 * state.process_status
172 g_string_free(f
,TRUE
);
173 f
=g_ptr_array_remove_index(fp
,0);
174 if(!g_strcasecmp(f
->str
,"pid") ) {
175 se
->field
= LTTV_FILTER_STATE_PID
;
177 else if(!g_strcasecmp(f
->str
,"ppid") ) {
178 se
->field
= LTTV_FILTER_STATE_PPID
;
180 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
181 se
->field
= LTTV_FILTER_STATE_CT
;
183 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
184 se
->field
= LTTV_FILTER_STATE_IT
;
186 else if(!g_strcasecmp(f
->str
,"process_name") ) {
187 se
->field
= LTTV_FILTER_STATE_P_NAME
;
189 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
190 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
192 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
193 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
195 else if(!g_strcasecmp(f
->str
,"process_status") ) {
196 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
198 else if(!g_strcasecmp(f
->str
,"cpu") ) {
199 se
->field
= LTTV_FILTER_STATE_CPU
;
201 } else if(!g_strcasecmp(f
->str
,"event") ) {
210 g_string_free(f
,TRUE
);
211 f
=g_ptr_array_remove_index(fp
,0);
212 if(!g_strcasecmp(f
->str
,"name") ) {
213 se
->field
= LTTV_FILTER_EVENT_NAME
;
215 else if(!g_strcasecmp(f
->str
,"facility") ) {
216 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
218 else if(!g_strcasecmp(f
->str
,"category") ) {
220 * FIXME: Category not yet functional in lttv
222 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
224 else if(!g_strcasecmp(f
->str
,"time") ) {
225 se
->field
= LTTV_FILTER_EVENT_TIME
;
227 else if(!g_strcasecmp(f
->str
,"tsc") ) {
228 se
->field
= LTTV_FILTER_EVENT_TSC
;
230 else if(!g_strcasecmp(f
->str
,"field") ) {
231 se
->field
= LTTV_FILTER_EVENT_FIELD
;
232 g_string_free(f
,TRUE
);
233 f
=g_ptr_array_remove_index(fp
,0);
236 g_string_free(f
,TRUE
);
237 f
=g_ptr_array_remove_index(fp
,0);
238 g_warning("Unknown event filter subtype %s", f
->str
);
241 g_string_free(f
,TRUE
);
242 f
=g_ptr_array_remove_index(fp
,0);
244 g_warning("Unrecognized field in filter string");
247 /* free memory for last string */
248 g_string_free(f
,TRUE
);
250 /* array should be empty */
251 g_assert(fp
->len
== 0);
253 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
254 g_warning("The specified field was not recognized !");
261 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
263 * Sets the function pointer for the current
265 * @param se current simple expression
266 * @param op current operator
267 * @return success/failure of operation
270 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
276 case LTTV_FILTER_TRACE_NAME
:
277 case LTTV_FILTER_TRACEFILE_NAME
:
278 case LTTV_FILTER_STATE_P_NAME
:
279 case LTTV_FILTER_EVENT_NAME
:
280 case LTTV_FILTER_EVENT_FACILITY
:
281 case LTTV_FILTER_STATE_EX_MODE
:
282 case LTTV_FILTER_STATE_EX_SUBMODE
:
283 case LTTV_FILTER_STATE_P_STATUS
:
286 se
->op
= lttv_apply_op_eq_quark
;
289 se
->op
= lttv_apply_op_ne_quark
;
292 g_warning("Error encountered in operator assignment = or != expected");
299 case LTTV_FILTER_EVENT_TSC
:
302 se
->op
= lttv_apply_op_eq_uint64
;
305 se
->op
= lttv_apply_op_ne_uint64
;
308 se
->op
= lttv_apply_op_lt_uint64
;
311 se
->op
= lttv_apply_op_le_uint64
;
314 se
->op
= lttv_apply_op_gt_uint64
;
317 se
->op
= lttv_apply_op_ge_uint64
;
320 g_warning("Error encountered in operator assignment");
327 case LTTV_FILTER_STATE_CPU
:
328 case LTTV_FILTER_STATE_PID
:
329 case LTTV_FILTER_STATE_PPID
:
332 se
->op
= lttv_apply_op_eq_uint
;
335 se
->op
= lttv_apply_op_ne_uint
;
338 se
->op
= lttv_apply_op_lt_uint
;
341 se
->op
= lttv_apply_op_le_uint
;
344 se
->op
= lttv_apply_op_gt_uint
;
347 se
->op
= lttv_apply_op_ge_uint
;
350 g_warning("Error encountered in operator assignment");
357 * Entered as string, converted to enum
359 * can only be compared with 'equal' or 'not equal' operators
361 * unsigned int of 16 bits are used here since enums
362 * should not go over 2^16-1 values
366 // case LTTV_FIELD_EQ:
367 // se->op = lttv_apply_op_eq_uint16;
369 // case LTTV_FIELD_NE:
370 // se->op = lttv_apply_op_ne_uint16;
373 // g_warning("Error encountered in operator assignment = or != expected");
380 case LTTV_FILTER_STATE_CT
:
381 case LTTV_FILTER_STATE_IT
:
382 case LTTV_FILTER_EVENT_TIME
:
385 se
->op
= lttv_apply_op_eq_ltttime
;
388 se
->op
= lttv_apply_op_ne_ltttime
;
391 se
->op
= lttv_apply_op_lt_ltttime
;
394 se
->op
= lttv_apply_op_le_ltttime
;
397 se
->op
= lttv_apply_op_gt_ltttime
;
400 se
->op
= lttv_apply_op_ge_ltttime
;
403 g_warning("Error encountered in operator assignment");
408 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
417 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
419 * Assign the value field to the current LttvSimpleExpression
420 * @param se pointer to the current LttvSimpleExpression
421 * @param value string value for simple expression
424 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
427 gboolean is_double
= FALSE
;
428 LttTime t
= ltt_time_zero
;
434 * entered as strings, converted to Quarks
436 case LTTV_FILTER_TRACE_NAME
:
437 case LTTV_FILTER_TRACEFILE_NAME
:
438 case LTTV_FILTER_STATE_P_NAME
:
439 case LTTV_FILTER_EVENT_NAME
:
440 case LTTV_FILTER_EVENT_FACILITY
:
441 case LTTV_FILTER_STATE_EX_MODE
:
442 case LTTV_FILTER_STATE_EX_SUBMODE
:
443 case LTTV_FILTER_STATE_P_STATUS
:
444 // se->value.v_string = value;
445 se
->value
.v_quark
= g_quark_from_string(value
);
449 * integer -- supposed to be uint64
451 case LTTV_FILTER_EVENT_TSC
:
452 se
->value
.v_uint64
= atoi(value
);
458 case LTTV_FILTER_STATE_PID
:
459 case LTTV_FILTER_STATE_PPID
:
460 case LTTV_FILTER_STATE_CPU
:
461 se
->value
.v_uint
= atoi(value
);
467 case LTTV_FILTER_STATE_CT
:
468 case LTTV_FILTER_STATE_IT
:
469 case LTTV_FILTER_EVENT_TIME
:
470 //se->value.v_double = atof(value);
472 * parsing logic could be optimised,
473 * but as for now, simpler this way
475 v
= g_string_new("");
476 for(i
=0;i
<strlen(value
);i
++) {
477 if(value
[i
] == '.') {
478 /* cannot specify number with more than one '.' */
479 if(is_double
) return FALSE
;
480 else is_double
= TRUE
;
481 t
.tv_sec
= atoi(v
->str
);
482 g_string_free(v
,TRUE
);
483 v
= g_string_new("");
484 } else g_string_append_c(v
,value
[i
]);
486 /* number can be integer or double */
487 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
488 else t
.tv_sec
= atoi(v
->str
);
490 g_string_free(v
,TRUE
);
492 se
->value
.v_ltttime
= t
;
496 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
506 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
508 * Disallocate memory for the current
510 * @param se pointer to the current LttvSimpleExpression
513 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
515 // g_free(se->value);
516 // switch(se->field) {
517 // case LTTV_FILTER_TRACE_NAME:
518 // case LTTV_FILTER_TRACEFILE_NAME:
519 // case LTTV_FILTER_STATE_P_NAME:
520 // case LTTV_FILTER_EVENT_NAME:
521 // g_free(se->value.v_string);
529 * @fn gint lttv_struct_type(gint)
531 * Finds the structure type depending
532 * on the fields in parameters
533 * @params ft Field of the current structure
534 * @return LttvStructType enum or -1 for error
537 lttv_struct_type(gint ft
) {
540 case LTTV_FILTER_TRACE_NAME
:
541 return LTTV_FILTER_TRACE
;
543 case LTTV_FILTER_TRACEFILE_NAME
:
544 return LTTV_FILTER_TRACEFILE
;
546 case LTTV_FILTER_STATE_PID
:
547 case LTTV_FILTER_STATE_PPID
:
548 case LTTV_FILTER_STATE_CT
:
549 case LTTV_FILTER_STATE_IT
:
550 case LTTV_FILTER_STATE_P_NAME
:
551 case LTTV_FILTER_STATE_EX_MODE
:
552 case LTTV_FILTER_STATE_EX_SUBMODE
:
553 case LTTV_FILTER_STATE_P_STATUS
:
554 case LTTV_FILTER_STATE_CPU
:
555 return LTTV_FILTER_STATE
;
557 case LTTV_FILTER_EVENT_NAME
:
558 case LTTV_FILTER_EVENT_FACILITY
:
559 case LTTV_FILTER_EVENT_CATEGORY
:
560 case LTTV_FILTER_EVENT_TIME
:
561 case LTTV_FILTER_EVENT_TSC
:
562 case LTTV_FILTER_EVENT_FIELD
:
563 return LTTV_FILTER_EVENT
;
571 * @fn gboolean lttv_apply_op_eq_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
581 guint
* r
= (guint
*) v1
;
582 return (*r
== v2
.v_uint
);
587 * @fn gboolean lttv_apply_op_eq_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
597 guint64
* r
= (guint64
*) v1
;
598 return (*r
== v2
.v_uint64
);
603 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
605 * Applies the 'equal' operator to the
606 * specified structure and value
607 * @param v1 left member of comparison
608 * @param v2 right member of comparison
609 * @return success/failure of operation
611 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
612 guint32
* r
= (guint32
*) v1
;
613 return (*r
== v2
.v_uint32
);
617 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
619 * Applies the 'equal' operator to the
620 * specified structure and value
621 * @param v1 left member of comparison
622 * @param v2 right member of comparison
623 * @return success/failure of operation
625 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
626 guint16
* r
= (guint16
*) v1
;
627 return (*r
== v2
.v_uint16
);
631 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
633 * Applies the 'equal' operator to the
634 * specified structure and value
635 * @param v1 left member of comparison
636 * @param v2 right member of comparison
637 * @return success/failure of operation
639 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
640 double* r
= (double*) v1
;
641 return (*r
== v2
.v_double
);
645 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
647 * Applies the 'equal' operator to the
648 * specified structure and value
649 * @param v1 left member of comparison
650 * @param v2 right member of comparison
651 * @return success/failure of operation
653 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
654 char* r
= (char*) v1
;
655 return (!g_strcasecmp(r
,v2
.v_string
));
659 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
661 * Applies the 'equal' operator to the
662 * specified structure and value
663 * @param v1 left member of comparison
664 * @param v2 right member of comparison
665 * @return success/failure of operation
667 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
668 GQuark
* r
= (GQuark
*) v1
;
669 return (*r
== v2
.v_quark
);
673 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
675 * Applies the 'equal' operator to the
676 * specified structure and value
677 * @param v1 left member of comparison
678 * @param v2 right member of comparison
679 * @return success/failure of operation
681 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
682 LttTime
* r
= (LttTime
*) v1
;
683 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
687 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
689 * Applies the 'not equal' operator to the
690 * specified structure and value
691 * @param v1 left member of comparison
692 * @param v2 right member of comparison
693 * @return success/failure of operation
695 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
696 guint
* r
= (guint
*) v1
;
697 return (*r
!= v2
.v_uint
);
701 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
703 * Applies the 'not equal' operator to the
704 * specified structure and value
705 * @param v1 left member of comparison
706 * @param v2 right member of comparison
707 * @return success/failure of operation
709 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
710 guint64
* r
= (guint64
*) v1
;
711 return (*r
!= v2
.v_uint64
);
715 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
717 * Applies the 'not equal' operator to the
718 * specified structure and value
719 * @param v1 left member of comparison
720 * @param v2 right member of comparison
721 * @return success/failure of operation
723 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
724 guint32
* r
= (guint32
*) v1
;
725 return (*r
!= v2
.v_uint32
);
729 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
731 * Applies the 'not equal' operator to the
732 * specified structure and value
733 * @param v1 left member of comparison
734 * @param v2 right member of comparison
735 * @return success/failure of operation
737 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
738 guint16
* r
= (guint16
*) v1
;
739 return (*r
!= v2
.v_uint16
);
743 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
745 * Applies the 'not equal' operator to the
746 * specified structure and value
747 * @param v1 left member of comparison
748 * @param v2 right member of comparison
749 * @return success/failure of operation
751 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
752 double* r
= (double*) v1
;
753 return (*r
!= v2
.v_double
);
757 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
759 * Applies the 'not equal' operator to the
760 * specified structure and value
761 * @param v1 left member of comparison
762 * @param v2 right member of comparison
763 * @return success/failure of operation
765 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
766 char* r
= (char*) v1
;
767 return (g_strcasecmp(r
,v2
.v_string
));
771 * @fn gboolean lttv_apply_op_ne_quark(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_quark(const gpointer v1
, LttvFieldValue v2
) {
780 GQuark
* r
= (GQuark
*) v1
;
781 return (*r
!= v2
.v_quark
);
786 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
788 * Applies the 'not equal' operator to the
789 * specified structure and value
790 * @param v1 left member of comparison
791 * @param v2 right member of comparison
792 * @return success/failure of operation
794 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
795 LttTime
* r
= (LttTime
*) v1
;
796 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
800 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
802 * Applies the 'lower than' operator to the
803 * specified structure and value
804 * @param v1 left member of comparison
805 * @param v2 right member of comparison
806 * @return success/failure of operation
808 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
809 guint
* r
= (guint
*) v1
;
810 return (*r
< v2
.v_uint
);
814 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
816 * Applies the 'lower than' operator to the
817 * specified structure and value
818 * @param v1 left member of comparison
819 * @param v2 right member of comparison
820 * @return success/failure of operation
822 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
823 guint64
* r
= (guint64
*) v1
;
824 return (*r
< v2
.v_uint64
);
828 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
830 * Applies the 'lower than' operator to the
831 * specified structure and value
832 * @param v1 left member of comparison
833 * @param v2 right member of comparison
834 * @return success/failure of operation
836 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
837 guint32
* r
= (guint32
*) v1
;
838 return (*r
< v2
.v_uint32
);
842 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
844 * Applies the 'lower than' operator to the
845 * specified structure and value
846 * @param v1 left member of comparison
847 * @param v2 right member of comparison
848 * @return success/failure of operation
850 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
851 guint16
* r
= (guint16
*) v1
;
852 return (*r
< v2
.v_uint16
);
856 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
858 * Applies the 'lower than' operator to the
859 * specified structure and value
860 * @param v1 left member of comparison
861 * @param v2 right member of comparison
862 * @return success/failure of operation
864 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
865 double* r
= (double*) v1
;
866 return (*r
< v2
.v_double
);
870 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
872 * Applies the 'lower than' 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_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
879 LttTime
* r
= (LttTime
*) v1
;
880 // 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)));
881 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
885 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
887 * Applies the 'lower or equal' operator to the
888 * specified structure and value
889 * @param v1 left member of comparison
890 * @param v2 right member of comparison
891 * @return success/failure of operation
893 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
894 guint
* r
= (guint
*) v1
;
895 return (*r
<= v2
.v_uint
);
899 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
901 * Applies the 'lower or equal' operator to the
902 * specified structure and value
903 * @param v1 left member of comparison
904 * @param v2 right member of comparison
905 * @return success/failure of operation
907 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
908 guint64
* r
= (guint64
*) v1
;
909 return (*r
<= v2
.v_uint64
);
913 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
915 * Applies the 'lower or equal' operator to the
916 * specified structure and value
917 * @param v1 left member of comparison
918 * @param v2 right member of comparison
919 * @return success/failure of operation
921 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
922 guint32
* r
= (guint32
*) v1
;
923 return (*r
<= v2
.v_uint32
);
927 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
929 * Applies the 'lower or equal' operator to the
930 * specified structure and value
931 * @param v1 left member of comparison
932 * @param v2 right member of comparison
933 * @return success/failure of operation
935 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
936 guint16
* r
= (guint16
*) v1
;
937 return (*r
<= v2
.v_uint16
);
941 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
943 * Applies the 'lower or equal' operator to the
944 * specified structure and value
945 * @param v1 left member of comparison
946 * @param v2 right member of comparison
947 * @return success/failure of operation
949 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
950 double* r
= (double*) v1
;
951 return (*r
<= v2
.v_double
);
955 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
957 * Applies the 'lower or equal' operator to the
958 * specified structure and value
959 * @param v1 left member of comparison
960 * @param v2 right member of comparison
961 * @return success/failure of operation
963 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
964 LttTime
* r
= (LttTime
*) v1
;
965 // 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)));
966 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
971 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
973 * Applies the 'greater than' operator to the
974 * specified structure and value
975 * @param v1 left member of comparison
976 * @param v2 right member of comparison
977 * @return success/failure of operation
979 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
980 guint
* r
= (guint
*) v1
;
981 return (*r
> v2
.v_uint
);
985 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
987 * Applies the 'greater than' operator to the
988 * specified structure and value
989 * @param v1 left member of comparison
990 * @param v2 right member of comparison
991 * @return success/failure of operation
993 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
994 guint64
* r
= (guint64
*) v1
;
995 return (*r
> v2
.v_uint64
);
999 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1001 * Applies the 'greater than' operator to the
1002 * specified structure and value
1003 * @param v1 left member of comparison
1004 * @param v2 right member of comparison
1005 * @return success/failure of operation
1007 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1008 guint32
* r
= (guint32
*) v1
;
1009 return (*r
> v2
.v_uint32
);
1013 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1015 * Applies the 'greater than' operator to the
1016 * specified structure and value
1017 * @param v1 left member of comparison
1018 * @param v2 right member of comparison
1019 * @return success/failure of operation
1021 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1022 guint16
* r
= (guint16
*) v1
;
1023 return (*r
> v2
.v_uint16
);
1027 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1029 * Applies the 'greater than' operator to the
1030 * specified structure and value
1031 * @param v1 left member of comparison
1032 * @param v2 right member of comparison
1033 * @return success/failure of operation
1035 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1036 double* r
= (double*) v1
;
1037 return (*r
> v2
.v_double
);
1041 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1043 * Applies the 'greater than' 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_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1050 LttTime
* r
= (LttTime
*) v1
;
1051 // 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)));
1052 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1056 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1058 * Applies the 'greater or equal' operator to the
1059 * specified structure and value
1060 * @param v1 left member of comparison
1061 * @param v2 right member of comparison
1062 * @return success/failure of operation
1064 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1065 guint
* r
= (guint
*) v1
;
1066 return (*r
>= v2
.v_uint
);
1070 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1072 * Applies the 'greater or equal' operator to the
1073 * specified structure and value
1074 * @param v1 left member of comparison
1075 * @param v2 right member of comparison
1076 * @return success/failure of operation
1078 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1079 guint64
* r
= (guint64
*) v1
;
1080 return (*r
>= v2
.v_uint64
);
1084 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1086 * Applies the 'greater or equal' operator to the
1087 * specified structure and value
1088 * @param v1 left member of comparison
1089 * @param v2 right member of comparison
1090 * @return success/failure of operation
1092 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1093 guint32
* r
= (guint32
*) v1
;
1094 return (*r
>= v2
.v_uint32
);
1098 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1100 * Applies the 'greater or equal' operator to the
1101 * specified structure and value
1102 * @param v1 left member of comparison
1103 * @param v2 right member of comparison
1104 * @return success/failure of operation
1106 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1107 guint16
* r
= (guint16
*) v1
;
1108 return (*r
>= v2
.v_uint16
);
1112 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1114 * Applies the 'greater or equal' operator to the
1115 * specified structure and value
1116 * @param v1 left member of comparison
1117 * @param v2 right member of comparison
1118 * @return success/failure of operation
1120 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1121 double* r
= (double*) v1
;
1122 return (*r
>= v2
.v_double
);
1126 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1128 * Applies the 'greater or equal' operator to the
1129 * specified structure and value
1130 * @param v1 left member of comparison
1131 * @param v2 right member of comparison
1132 * @return success/failure of operation
1134 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1135 LttTime
* r
= (LttTime
*) v1
;
1136 // 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)));
1137 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1143 * Makes a copy of the current filter tree
1144 * @param tree pointer to the current tree
1145 * @return new copy of the filter tree
1148 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1150 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1152 newtree
->node
= tree
->node
;
1154 newtree
->left
= tree
->left
;
1155 if(newtree
->left
== LTTV_TREE_NODE
) {
1156 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1157 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1158 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1159 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1160 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1161 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1162 /* FIXME: special case for string copy ! */
1163 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1166 newtree
->right
= tree
->right
;
1167 if(newtree
->right
== LTTV_TREE_NODE
) {
1168 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1169 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1170 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1171 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1172 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1173 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1174 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1182 * Makes a copy of the current filter
1183 * @param filter pointer to the current filter
1184 * @return new copy of the filter
1187 lttv_filter_clone(const LttvFilter
* filter
) {
1189 if(!filter
) return NULL
;
1191 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1193 strcpy(newfilter
->expression
,filter
->expression
);
1195 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1203 * @fn LttvFilter* lttv_filter_new()
1205 * Creates a new LttvFilter
1206 * @return the current LttvFilter or NULL if error
1211 LttvFilter
* filter
= g_new(LttvFilter
,1);
1212 filter
->expression
= NULL
;
1213 filter
->head
= NULL
;
1220 * @fn gboolean lttv_filter_update(LttvFilter*)
1222 * Updates the current LttvFilter by building
1223 * its tree based upon the expression string
1224 * @param filter pointer to the current LttvFilter
1225 * @return Failure/Success of operation
1228 lttv_filter_update(LttvFilter
* filter
) {
1230 // g_print("filter::lttv_filter_new()\n"); /* debug */
1232 if(filter
->expression
== NULL
) return FALSE
;
1236 p_nesting
=0, /* parenthesis nesting value */
1241 *tree
= lttv_filter_tree_new(), /* main tree */
1242 *subtree
= NULL
, /* buffer for subtrees */
1243 *t1
, /* buffer #1 */
1244 *t2
, /* buffer #2 */
1245 *t3
; /* buffer #3 */
1249 * If the tree already exists,
1250 * destroy it and build a new one
1252 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1253 filter
->head
= NULL
; /* will be assigned at the end */
1257 * each element of the list
1258 * is a sub tree created
1259 * by the use of parenthesis in the
1260 * global expression. The final tree
1261 * will be the one left at the root of
1264 GPtrArray
*tree_stack
= g_ptr_array_new();
1265 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1267 /* temporary values */
1268 GString
*a_field_component
= g_string_new("");
1269 GString
*a_string_spaces
= g_string_new("");
1270 GPtrArray
*a_field_path
= g_ptr_array_new();
1272 /* simple expression buffer */
1273 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1276 * Parse entire expression and construct
1277 * the binary tree. There are two steps
1278 * in browsing that string
1279 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1280 * 2. finding simple expressions
1281 * - field path ( separated by dots )
1282 * - op ( >, <, =, >=, <=, !=)
1283 * - value ( integer, string ... )
1284 * To spare computing time, the whole
1285 * string is parsed in this loop for a
1286 * O(n) complexity order.
1288 * When encountering logical op &,|,^
1289 * 1. parse the last value if any
1290 * 2. create a new tree
1291 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1292 * 4. concatenate this tree with the current tree on top of the stack
1293 * When encountering math ops >,>=,<,<=,=,!=
1294 * 1. add to op to the simple expression
1295 * 2. concatenate last field component to field path
1296 * When encountering concatening ops .
1297 * 1. concatenate last field component to field path
1298 * When encountering opening parenthesis (,{,[
1299 * 1. create a new subtree on top of tree stack
1300 * When encountering closing parenthesis ),},]
1301 * 1. add the expression on right child of the current tree
1302 * 2. the subtree is completed, allocate a new subtree
1303 * 3. pop the tree value from the tree stack
1307 struct timeval starttime
;
1308 struct timeval endtime
;
1309 gettimeofday(&starttime
, NULL
);
1312 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1314 // g_print("%c\n ",filter->expression[i]);
1316 switch(filter
->expression
[i
]) {
1322 /* get current tree in tree stack */
1323 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1325 /* get current node at absolute right */
1326 while(t1
->right
!= LTTV_TREE_IDLE
) {
1327 g_assert(t1
->right
== LTTV_TREE_NODE
);
1330 t2
= lttv_filter_tree_new();
1331 t2
->node
= LTTV_LOGICAL_AND
;
1332 t1
->right
= LTTV_TREE_NODE
;
1334 if(not) { /* add not operator to tree */
1335 t3
= lttv_filter_tree_new();
1336 t3
->node
= LTTV_LOGICAL_NOT
;
1337 t2
->left
= LTTV_TREE_NODE
;
1342 if(subtree
!= NULL
) { /* append subtree to current tree */
1343 t2
->left
= LTTV_TREE_NODE
;
1344 t2
->l_child
.t
= subtree
;
1346 } else { /* append a simple expression */
1347 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1348 a_field_component
= g_string_new("");
1349 g_string_free(a_string_spaces
, TRUE
);
1350 a_string_spaces
= g_string_new("");
1351 t2
->left
= LTTV_TREE_LEAF
;
1352 t2
->l_child
.leaf
= a_simple_expression
;
1353 a_simple_expression
= lttv_simple_expression_new();
1359 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1360 while(t1
->right
!= LTTV_TREE_IDLE
) {
1361 g_assert(t1
->right
== LTTV_TREE_NODE
);
1364 t2
= lttv_filter_tree_new();
1365 t2
->node
= LTTV_LOGICAL_OR
;
1366 t1
->right
= LTTV_TREE_NODE
;
1368 if(not) { // add not operator to tree
1369 t3
= lttv_filter_tree_new();
1370 t3
->node
= LTTV_LOGICAL_NOT
;
1371 t2
->left
= LTTV_TREE_NODE
;
1376 if(subtree
!= NULL
) { /* append subtree to current tree */
1377 t2
->left
= LTTV_TREE_NODE
;
1378 t2
->l_child
.t
= subtree
;
1380 } else { /* append a simple expression */
1381 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1382 a_field_component
= g_string_new("");
1383 g_string_free(a_string_spaces
, TRUE
);
1384 a_string_spaces
= g_string_new("");
1385 t2
->left
= LTTV_TREE_LEAF
;
1386 t2
->l_child
.leaf
= a_simple_expression
;
1387 a_simple_expression
= lttv_simple_expression_new();
1393 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1394 while(t1
->right
!= LTTV_TREE_IDLE
) {
1395 g_assert(t1
->right
== LTTV_TREE_NODE
);
1398 t2
= lttv_filter_tree_new();
1399 t2
->node
= LTTV_LOGICAL_XOR
;
1400 t1
->right
= LTTV_TREE_NODE
;
1402 if(not) { // add not operator to tree
1403 t3
= lttv_filter_tree_new();
1404 t3
->node
= LTTV_LOGICAL_NOT
;
1405 t2
->left
= LTTV_TREE_NODE
;
1410 if(subtree
!= NULL
) { /* append subtree to current tree */
1411 t2
->left
= LTTV_TREE_NODE
;
1412 t2
->l_child
.t
= subtree
;
1414 } else { /* append a simple expression */
1415 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1416 a_field_component
= g_string_new("");
1417 g_string_free(a_string_spaces
, TRUE
);
1418 a_string_spaces
= g_string_new("");
1419 t2
->left
= LTTV_TREE_LEAF
;
1420 t2
->l_child
.leaf
= a_simple_expression
;
1421 a_simple_expression
= lttv_simple_expression_new();
1425 case '!': /* not, or not equal (math op) */
1427 if(filter
->expression
[i
+1] == '=') { /* != */
1428 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1429 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1430 a_field_component
= g_string_new("");
1431 g_string_free(a_string_spaces
, TRUE
);
1432 a_string_spaces
= g_string_new("");
1433 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1440 case '(': /* start of parenthesis */
1444 p_nesting
++; /* incrementing parenthesis nesting value */
1445 t1
= lttv_filter_tree_new();
1446 if(not) { /* add not operator to tree */
1447 t3
= lttv_filter_tree_new();
1448 t3
->node
= LTTV_LOGICAL_NOT
;
1449 t1
->right
= LTTV_TREE_NODE
;
1453 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1456 case ')': /* end of parenthesis */
1460 p_nesting
--; /* decrementing parenthesis nesting value */
1461 if(p_nesting
<0 || tree_stack
->len
<2) {
1462 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1463 is not valid due to parenthesis incorrect use",filter
->expression
);
1467 /* there must at least be the root tree left in the array */
1468 g_assert(tree_stack
->len
>0);
1470 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1471 while(t1
->right
!= LTTV_TREE_IDLE
) {
1474 if(not) { // add not operator to tree
1476 t3
= lttv_filter_tree_new();
1477 t3
->node
= LTTV_LOGICAL_NOT
;
1478 t1
->right
= LTTV_TREE_NODE
;
1483 if(subtree
!= NULL
) { /* append subtree to current tree */
1484 t1
->right
= LTTV_TREE_NODE
;
1485 t1
->r_child
.t
= subtree
;
1486 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1487 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1488 } else { /* assign subtree as current tree */
1489 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1490 a_field_component
= g_string_new("");
1491 g_string_free(a_string_spaces
, TRUE
);
1492 a_string_spaces
= g_string_new("");
1493 t1
->right
= LTTV_TREE_LEAF
;
1494 t1
->r_child
.leaf
= a_simple_expression
;
1495 a_simple_expression
= lttv_simple_expression_new();
1496 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1501 * mathematic operators
1503 case '<': /* lower, lower or 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 g_string_free(a_string_spaces
, TRUE
);
1509 a_string_spaces
= g_string_new("");
1510 if(filter
->expression
[i
+1] == '=') { /* <= */
1512 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1513 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1516 case '>': /* higher, higher or equal */
1518 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1519 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1520 a_field_component
= g_string_new("");
1521 g_string_free(a_string_spaces
, TRUE
);
1522 a_string_spaces
= g_string_new("");
1523 if(filter
->expression
[i
+1] == '=') { /* >= */
1525 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1526 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1529 case '=': /* equal */
1531 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1532 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1533 a_field_component
= g_string_new("");
1534 g_string_free(a_string_spaces
, TRUE
);
1535 a_string_spaces
= g_string_new("");
1536 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1540 * Field concatening caracter
1545 * divide field expression into elements
1546 * in a_field_path array.
1548 * A dot can also be present in double values
1550 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1551 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1552 a_field_component
= g_string_new("");
1553 g_string_free(a_string_spaces
, TRUE
);
1554 a_string_spaces
= g_string_new("");
1557 case ' ': /* keep spaces that are within a field component */
1558 if(a_field_component
->len
== 0) break; /* ignore */
1560 a_string_spaces
= g_string_append_c(a_string_spaces
,
1561 filter
->expression
[i
]);
1563 case '\n': /* ignore */
1565 default: /* concatening current string */
1566 if(a_string_spaces
->len
!= 0) {
1567 g_string_append(a_field_component
, a_string_spaces
->str
);
1568 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1570 a_field_component
= g_string_append_c(a_field_component
,
1571 filter
->expression
[i
]);
1576 * Preliminary check to see
1577 * if tree was constructed correctly
1580 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1581 is not valid due to parenthesis incorrect use",filter
->expression
);
1585 if(tree_stack
->len
!= 1) /* only root tree should remain */
1589 * processing last element of expression
1591 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1592 while(t1
->right
!= LTTV_TREE_IDLE
) {
1593 g_assert(t1
->right
== LTTV_TREE_NODE
);
1596 if(not) { // add not operator to tree
1597 t3
= lttv_filter_tree_new();
1598 t3
->node
= LTTV_LOGICAL_NOT
;
1599 t1
->right
= LTTV_TREE_NODE
;
1604 if(subtree
!= NULL
) { /* add the subtree */
1605 t1
->right
= LTTV_TREE_NODE
;
1606 t1
->r_child
.t
= subtree
;
1608 } else { /* add a leaf */
1609 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1610 a_field_component
= NULL
;
1611 g_string_free(a_string_spaces
, TRUE
);
1612 a_string_spaces
= NULL
;
1613 t1
->right
= LTTV_TREE_LEAF
;
1614 t1
->r_child
.leaf
= a_simple_expression
;
1615 a_simple_expression
= NULL
;
1619 /* free the pointer array */
1620 g_assert(a_field_path
->len
== 0);
1621 g_ptr_array_free(a_field_path
,TRUE
);
1623 /* free the tree stack -- but keep the root tree */
1624 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1625 g_ptr_array_free(tree_stack
,TRUE
);
1627 /* free the field buffer if allocated */
1628 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1629 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1631 /* free the simple expression buffer if allocated */
1632 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1634 g_assert(filter
->head
!= NULL
); /* tree should exist */
1635 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1638 gettimeofday(&endtime
, NULL
);
1640 /* Calcul du temps de l'algorithme */
1641 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1642 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1643 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1644 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1648 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1649 lttv_print_tree(filter
->head
,0) ;
1650 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1658 * @fn void lttv_filter_destroy(LttvFilter*)
1660 * Destroy the current LttvFilter
1661 * @param filter pointer to the current LttvFilter
1664 lttv_filter_destroy(LttvFilter
* filter
) {
1668 if(filter
->expression
)
1669 g_free(filter
->expression
);
1671 lttv_filter_tree_destroy(filter
->head
);
1677 * @fn LttvFilterTree* lttv_filter_tree_new()
1679 * Assign a new tree for the current expression
1681 * @return pointer of LttvFilterTree
1684 lttv_filter_tree_new() {
1685 LttvFilterTree
* tree
;
1687 tree
= g_new(LttvFilterTree
,1);
1688 tree
->node
= 0; //g_new(lttv_expression,1);
1689 tree
->left
= LTTV_TREE_IDLE
;
1690 tree
->right
= LTTV_TREE_IDLE
;
1691 tree
->r_child
.t
= NULL
;
1692 tree
->l_child
.t
= NULL
;
1698 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1700 * Append a new expression to the expression
1701 * defined in the current filter
1702 * @param filter pointer to the current LttvFilter
1703 * @param expression string that must be appended
1704 * @return Success/Failure of operation
1707 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1709 if(expression
== NULL
) return FALSE
;
1710 if(filter
== NULL
) return FALSE
;
1711 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1713 GString
* s
= g_string_new("");
1714 if(filter
->expression
!= NULL
) {
1715 g_string_append(s
,filter
->expression
);
1716 g_string_append_c(s
,'&');
1718 g_string_append(s
,expression
);
1720 g_free(filter
->expression
);
1721 filter
->expression
= g_string_free(s
,FALSE
);
1723 /* TRUE if construction of tree proceeded without errors */
1724 return lttv_filter_update(filter
);
1729 * @fn void lttv_filter_clear_expression(LttvFilter*)
1731 * Clear the filter expression from the
1732 * current filter and sets its pointer to NULL
1733 * @param filter pointer to the current LttvFilter
1736 lttv_filter_clear_expression(LttvFilter
* filter
) {
1738 if(filter
->expression
!= NULL
) {
1739 g_free(filter
->expression
);
1740 filter
->expression
= NULL
;
1746 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1748 * Destroys the tree and his sub-trees
1749 * @param tree Tree which must be destroyed
1752 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1754 if(tree
== NULL
) return;
1756 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1757 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1759 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1760 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1762 // g_free(tree->node);
1767 * Global parsing function for the current
1769 * @param t pointer to the current LttvFilterTree
1770 * @param event current LttEvent, NULL if not used
1771 * @param tracefile current LttTracefile, NULL if not used
1772 * @param trace current LttTrace, NULL if not used
1773 * @param state current LttvProcessState, NULL if not used
1774 * @param context current LttvTracefileContext, NULL if not used
1775 * @return response of filter
1778 lttv_filter_tree_parse(
1779 const LttvFilterTree
* t
,
1780 const LttEvent
* event
,
1781 const LttTracefile
* tracefile
,
1782 const LttTrace
* trace
,
1783 const LttvTracefileContext
* context
1788 * Each tree is parsed in inorder.
1789 * This way, it's possible to apply the left filter of the
1790 * tree, then decide whether or not the right branch should
1791 * be parsed depending on the linking logical operator
1793 * Each node consists in a
1794 * 1. logical operator
1795 * 2. left child ( node or simple expression )
1796 * 3. right child ( node or simple expression )
1798 * When the child is a simple expression, we must
1799 * before all determine if the expression refers to
1800 * a structure which is whithin observation ( not NULL ).
1801 * -If so, the expression is evaluated.
1802 * -If not, the result is set to TRUE since this particular
1803 * operation does not interfere with the lttv structure
1805 * The result of each simple expression will directly
1806 * affect the next branch. This way, depending on
1807 * the linking logical operator, the parser will decide
1808 * to explore or not the next branch.
1810 * -If result of left branch is 0 / FALSE
1811 * then don't explore right branch and return 0;
1812 * -If result of left branch is 1 / TRUE then explore
1814 * -If result of left branch is 1 / TRUE
1815 * then don't explore right branch and return 1;
1816 * -If result of left branch is 0 / FALSE then explore
1818 * -Result of left branch will not affect exploration of
1822 gboolean lresult
= FALSE
, rresult
= FALSE
;
1824 LttvProcessState
* state
;
1826 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1827 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1828 guint cpu
= tfs
->cpu
;
1829 state
= ts
->running_process
[cpu
];
1834 if(t
->left
== LTTV_TREE_NODE
) {
1835 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1837 else if(t
->left
== LTTV_TREE_LEAF
) {
1838 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1842 * Parse linking operator
1843 * make a cutoff if possible
1845 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1846 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1849 * Parse right branch
1851 if(t
->right
== LTTV_TREE_NODE
) {
1852 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1854 else if(t
->right
== LTTV_TREE_LEAF
) {
1855 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1860 * Apply and return the
1861 * logical link between the
1865 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1866 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1867 case LTTV_LOGICAL_NOT
:
1868 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1869 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1870 case 0: return (rresult
);
1873 * This case should never be
1874 * parsed, if so, this subtree
1883 * This function parses a particular branch of the tree
1884 * @param se pointer to the current LttvSimpleExpression
1885 * @param event current LttEvent, NULL if not used
1886 * @param tracefile current LttTracefile, NULL if not used
1887 * @param trace current LttTrace, NULL if not used
1888 * @param state current LttvProcessState, NULL if not used
1889 * @param context current LttvTracefileContext, NULL if not used
1890 * @return response of filter
1893 lttv_filter_tree_parse_branch(
1894 const LttvSimpleExpression
* se
,
1895 const LttEvent
* event
,
1896 const LttTracefile
* tracefile
,
1897 const LttTrace
* trace
,
1898 const LttvProcessState
* state
,
1899 const LttvTracefileContext
* context
) {
1904 case LTTV_FILTER_TRACE_NAME
:
1905 if(trace
== NULL
) return TRUE
;
1907 GQuark quark
= ltt_trace_name(trace
);
1908 return se
->op((gpointer
)&quark
,v
);
1911 case LTTV_FILTER_TRACEFILE_NAME
:
1912 if(tracefile
== NULL
) return TRUE
;
1914 GQuark quark
= ltt_tracefile_name(tracefile
);
1915 return se
->op((gpointer
)&quark
,v
);
1918 case LTTV_FILTER_STATE_PID
:
1919 if(state
== NULL
) return TRUE
;
1920 else return se
->op((gpointer
)&state
->pid
,v
);
1922 case LTTV_FILTER_STATE_PPID
:
1923 if(state
== NULL
) return TRUE
;
1924 else return se
->op((gpointer
)&state
->ppid
,v
);
1926 case LTTV_FILTER_STATE_CT
:
1927 if(state
== NULL
) return TRUE
;
1929 return se
->op((gpointer
)&state
->creation_time
,v
);
1932 case LTTV_FILTER_STATE_IT
:
1933 if(state
== NULL
) return TRUE
;
1935 return se
->op((gpointer
)&state
->insertion_time
,v
);
1938 case LTTV_FILTER_STATE_P_NAME
:
1940 * All 'unnamed' for the moment
1942 if(state
== NULL
) return TRUE
;
1944 GQuark quark
= state
->name
;
1945 return se
->op((gpointer
)&quark
,v
);
1948 case LTTV_FILTER_STATE_EX_MODE
:
1949 if(state
== NULL
) return TRUE
;
1950 else return se
->op((gpointer
)&state
->state
->t
,v
);
1952 case LTTV_FILTER_STATE_EX_SUBMODE
:
1953 if(state
== NULL
) return TRUE
;
1954 else return se
->op((gpointer
)&state
->state
->n
,v
);
1956 case LTTV_FILTER_STATE_P_STATUS
:
1957 if(state
== NULL
) return TRUE
;
1958 else return se
->op((gpointer
)&state
->state
->s
,v
);
1960 case LTTV_FILTER_STATE_CPU
:
1961 if(context
== NULL
) return TRUE
;
1963 if(state
== NULL
) return TRUE
;
1964 else return se
->op((gpointer
)&state
->cpu
,v
);
1967 case LTTV_FILTER_EVENT_NAME
:
1968 if(event
== NULL
) return TRUE
;
1971 et
= ltt_event_eventtype(event
);
1972 GQuark quark
= ltt_eventtype_name(et
);
1973 return se
->op((gpointer
)&quark
,v
);
1976 case LTTV_FILTER_EVENT_FACILITY
:
1977 if(event
== NULL
) return TRUE
;
1980 fac
= ltt_event_facility(event
);
1981 GQuark quark
= ltt_facility_name(fac
);
1982 return se
->op((gpointer
)&quark
,v
);
1985 case LTTV_FILTER_EVENT_CATEGORY
:
1987 * TODO: Not yet implemented
1991 case LTTV_FILTER_EVENT_TIME
:
1992 if(event
== NULL
) return TRUE
;
1994 LttTime time
= ltt_event_time(event
);
1995 return se
->op((gpointer
)&time
,v
);
1998 case LTTV_FILTER_EVENT_TSC
:
1999 if(event
== NULL
) return TRUE
;
2001 LttCycleCount count
= ltt_event_cycle_count(event
);
2002 return se
->op((gpointer
)&count
,v
);
2005 case LTTV_FILTER_EVENT_FIELD
:
2007 * TODO: Use the offset to
2008 * find the dynamic field
2009 * in the event struct
2014 * This case should never be
2015 * parsed, if so, the whole
2016 * filtering is cancelled
2018 g_warning("Error while parsing the filter tree");
2022 /* should never get here */
2030 * Debug function. Prints tree memory allocation.
2031 * @param t the pointer to the current LttvFilterTree
2034 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2036 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2037 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2038 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2040 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2041 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"));
2042 if(t
->left
== LTTV_TREE_LEAF
) {
2043 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2044 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2045 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2047 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"));
2048 if(t
->right
== LTTV_TREE_LEAF
) {
2049 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2050 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2051 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2054 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2055 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2059 * @fn static void module_init()
2061 * Initializes the filter module and specific values
2063 static void module_init()
2069 * Destroys the filter module and specific values
2071 static void module_destroy()
2077 LTTV_MODULE("filter", "Filters traceset and events", \
2078 "Filters traceset and events specifically to user input", \
2079 module_init
, module_destroy
)