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 * |->thread_brand (String, converted to GQuark)
56 * |->execution_mode (LttvExecutionMode)
57 * |->execution_submode (LttvExecutionSubmode)
58 * |->process_status (LttvProcessStatus)
65 * - refine switch of expression in multiple uses functions
66 * - remove the idle expressions in the tree
79 #include <lttv/lttv.h>
80 #include <lttv/filter.h>
81 #include <ltt/trace.h>
83 #include <ltt/facility.h>
88 * @fn LttvSimpleExpression* lttv_simple_expression_new()
90 * Constructor for LttvSimpleExpression
91 * @return pointer to new LttvSimpleExpression
94 lttv_simple_expression_new() {
96 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
98 se
->field
= LTTV_FILTER_UNDEFINED
;
106 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
108 * Parse through filtering field hierarchy as specified
109 * by user. This function compares each value to
110 * predetermined quarks
111 * @param fp The field path list
112 * @param se current simple expression
113 * @return success/failure of operation
116 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
120 if(fp
->len
< 2) return FALSE
;
121 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
124 * Parse through the specified
127 * Take note however that the
128 * 'event' subfields might change
129 * depending on values specified
130 * in core.xml file. Hence, if
131 * none of the subfields in the
132 * array match the hardcoded
133 * subfields, it will be considered
136 if(!g_strcasecmp(f
->str
,"trace") ) {
141 g_string_free(f
,TRUE
);
142 f
=g_ptr_array_remove_index(fp
,0);
143 if(!g_strcasecmp(f
->str
,"name")) {
144 se
->field
= LTTV_FILTER_TRACE_NAME
;
146 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
148 * FIXME: not yet implemented !
150 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
155 g_string_free(f
,TRUE
);
156 f
=g_ptr_array_remove_index(fp
,0);
157 if(!g_strcasecmp(f
->str
,"name")) {
158 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
160 } else if(!g_strcasecmp(f
->str
,"state") ) {
165 * state.creation_time
166 * state.insertion_time
169 * state.execution_mode
170 * state.execution_submode
171 * state.process_status
174 g_string_free(f
,TRUE
);
175 f
=g_ptr_array_remove_index(fp
,0);
176 if(!g_strcasecmp(f
->str
,"pid") ) {
177 se
->field
= LTTV_FILTER_STATE_PID
;
179 else if(!g_strcasecmp(f
->str
,"ppid") ) {
180 se
->field
= LTTV_FILTER_STATE_PPID
;
182 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
183 se
->field
= LTTV_FILTER_STATE_CT
;
185 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
186 se
->field
= LTTV_FILTER_STATE_IT
;
188 else if(!g_strcasecmp(f
->str
,"process_name") ) {
189 se
->field
= LTTV_FILTER_STATE_P_NAME
;
191 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
192 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
194 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
195 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
197 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
198 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
200 else if(!g_strcasecmp(f
->str
,"process_status") ) {
201 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
203 else if(!g_strcasecmp(f
->str
,"cpu") ) {
204 se
->field
= LTTV_FILTER_STATE_CPU
;
206 } else if(!g_strcasecmp(f
->str
,"event") ) {
215 g_string_free(f
,TRUE
);
216 f
=g_ptr_array_remove_index(fp
,0);
217 if(!g_strcasecmp(f
->str
,"name") ) {
218 se
->field
= LTTV_FILTER_EVENT_NAME
;
220 else if(!g_strcasecmp(f
->str
,"facility") ) {
221 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
223 else if(!g_strcasecmp(f
->str
,"category") ) {
225 * FIXME: Category not yet functional in lttv
227 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
229 else if(!g_strcasecmp(f
->str
,"time") ) {
230 se
->field
= LTTV_FILTER_EVENT_TIME
;
232 else if(!g_strcasecmp(f
->str
,"tsc") ) {
233 se
->field
= LTTV_FILTER_EVENT_TSC
;
235 else if(!g_strcasecmp(f
->str
,"field") ) {
236 se
->field
= LTTV_FILTER_EVENT_FIELD
;
237 g_string_free(f
,TRUE
);
238 f
=g_ptr_array_remove_index(fp
,0);
241 g_string_free(f
,TRUE
);
242 f
=g_ptr_array_remove_index(fp
,0);
243 g_warning("Unknown event filter subtype %s", f
->str
);
246 g_string_free(f
,TRUE
);
247 f
=g_ptr_array_remove_index(fp
,0);
249 g_warning("Unrecognized field in filter string");
252 /* free memory for last string */
253 g_string_free(f
,TRUE
);
255 /* array should be empty */
256 g_assert(fp
->len
== 0);
258 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
259 g_warning("The specified field was not recognized !");
266 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
268 * Sets the function pointer for the current
270 * @param se current simple expression
271 * @param op current operator
272 * @return success/failure of operation
275 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
281 case LTTV_FILTER_TRACE_NAME
:
282 case LTTV_FILTER_TRACEFILE_NAME
:
283 case LTTV_FILTER_STATE_P_NAME
:
284 case LTTV_FILTER_STATE_T_BRAND
:
285 case LTTV_FILTER_EVENT_NAME
:
286 case LTTV_FILTER_EVENT_FACILITY
:
287 case LTTV_FILTER_STATE_EX_MODE
:
288 case LTTV_FILTER_STATE_EX_SUBMODE
:
289 case LTTV_FILTER_STATE_P_STATUS
:
292 se
->op
= lttv_apply_op_eq_quark
;
295 se
->op
= lttv_apply_op_ne_quark
;
298 g_warning("Error encountered in operator assignment = or != expected");
305 case LTTV_FILTER_EVENT_TSC
:
308 se
->op
= lttv_apply_op_eq_uint64
;
311 se
->op
= lttv_apply_op_ne_uint64
;
314 se
->op
= lttv_apply_op_lt_uint64
;
317 se
->op
= lttv_apply_op_le_uint64
;
320 se
->op
= lttv_apply_op_gt_uint64
;
323 se
->op
= lttv_apply_op_ge_uint64
;
326 g_warning("Error encountered in operator assignment");
333 case LTTV_FILTER_STATE_CPU
:
334 case LTTV_FILTER_STATE_PID
:
335 case LTTV_FILTER_STATE_PPID
:
338 se
->op
= lttv_apply_op_eq_uint
;
341 se
->op
= lttv_apply_op_ne_uint
;
344 se
->op
= lttv_apply_op_lt_uint
;
347 se
->op
= lttv_apply_op_le_uint
;
350 se
->op
= lttv_apply_op_gt_uint
;
353 se
->op
= lttv_apply_op_ge_uint
;
356 g_warning("Error encountered in operator assignment");
363 * Entered as string, converted to enum
365 * can only be compared with 'equal' or 'not equal' operators
367 * unsigned int of 16 bits are used here since enums
368 * should not go over 2^16-1 values
372 // case LTTV_FIELD_EQ:
373 // se->op = lttv_apply_op_eq_uint16;
375 // case LTTV_FIELD_NE:
376 // se->op = lttv_apply_op_ne_uint16;
379 // g_warning("Error encountered in operator assignment = or != expected");
386 case LTTV_FILTER_STATE_CT
:
387 case LTTV_FILTER_STATE_IT
:
388 case LTTV_FILTER_EVENT_TIME
:
391 se
->op
= lttv_apply_op_eq_ltttime
;
394 se
->op
= lttv_apply_op_ne_ltttime
;
397 se
->op
= lttv_apply_op_lt_ltttime
;
400 se
->op
= lttv_apply_op_le_ltttime
;
403 se
->op
= lttv_apply_op_gt_ltttime
;
406 se
->op
= lttv_apply_op_ge_ltttime
;
409 g_warning("Error encountered in operator assignment");
414 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
423 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
425 * Assign the value field to the current LttvSimpleExpression
426 * @param se pointer to the current LttvSimpleExpression
427 * @param value string value for simple expression
430 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
433 gboolean is_double
= FALSE
;
434 LttTime t
= ltt_time_zero
;
441 * entered as strings, converted to Quarks
443 case LTTV_FILTER_TRACE_NAME
:
444 case LTTV_FILTER_TRACEFILE_NAME
:
445 case LTTV_FILTER_STATE_P_NAME
:
446 case LTTV_FILTER_STATE_T_BRAND
:
447 case LTTV_FILTER_EVENT_NAME
:
448 case LTTV_FILTER_EVENT_FACILITY
:
449 case LTTV_FILTER_STATE_EX_MODE
:
450 case LTTV_FILTER_STATE_EX_SUBMODE
:
451 case LTTV_FILTER_STATE_P_STATUS
:
452 // se->value.v_string = value;
453 se
->value
.v_quark
= g_quark_from_string(value
);
457 * integer -- supposed to be uint64
459 case LTTV_FILTER_EVENT_TSC
:
460 se
->value
.v_uint64
= atoi(value
);
466 case LTTV_FILTER_STATE_PID
:
467 case LTTV_FILTER_STATE_PPID
:
468 case LTTV_FILTER_STATE_CPU
:
469 se
->value
.v_uint
= atoi(value
);
475 case LTTV_FILTER_STATE_CT
:
476 case LTTV_FILTER_STATE_IT
:
477 case LTTV_FILTER_EVENT_TIME
:
478 //se->value.v_double = atof(value);
480 * parsing logic could be optimised,
481 * but as for now, simpler this way
483 v
= g_string_new("");
484 string_len
= strlen(value
);
485 for(i
=0;i
<string_len
;i
++) {
486 if(value
[i
] == '.') {
487 /* cannot specify number with more than one '.' */
488 if(is_double
) return FALSE
;
489 else is_double
= TRUE
;
490 t
.tv_sec
= atoi(v
->str
);
491 g_string_free(v
,TRUE
);
492 v
= g_string_new("");
493 } else v
= g_string_append_c(v
,value
[i
]);
495 /* number can be integer or double */
496 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
498 t
.tv_sec
= atoi(v
->str
);
502 g_string_free(v
,TRUE
);
504 se
->value
.v_ltttime
= t
;
505 //g_error("Filter TEST ltttime : %u, %u.", t.tv_sec, t.tv_nsec);
509 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
519 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
521 * Disallocate memory for the current
523 * @param se pointer to the current LttvSimpleExpression
526 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
528 // g_free(se->value);
529 // switch(se->field) {
530 // case LTTV_FILTER_TRACE_NAME:
531 // case LTTV_FILTER_TRACEFILE_NAME:
532 // case LTTV_FILTER_STATE_P_NAME:
533 // case LTTV_FILTER_EVENT_NAME:
534 // g_free(se->value.v_string);
542 * @fn gint lttv_struct_type(gint)
544 * Finds the structure type depending
545 * on the fields in parameters
546 * @params ft Field of the current structure
547 * @return LttvStructType enum or -1 for error
550 lttv_struct_type(gint ft
) {
553 case LTTV_FILTER_TRACE_NAME
:
554 return LTTV_FILTER_TRACE
;
556 case LTTV_FILTER_TRACEFILE_NAME
:
557 return LTTV_FILTER_TRACEFILE
;
559 case LTTV_FILTER_STATE_PID
:
560 case LTTV_FILTER_STATE_PPID
:
561 case LTTV_FILTER_STATE_CT
:
562 case LTTV_FILTER_STATE_IT
:
563 case LTTV_FILTER_STATE_P_NAME
:
564 case LTTV_FILTER_STATE_T_BRAND
:
565 case LTTV_FILTER_STATE_EX_MODE
:
566 case LTTV_FILTER_STATE_EX_SUBMODE
:
567 case LTTV_FILTER_STATE_P_STATUS
:
568 case LTTV_FILTER_STATE_CPU
:
569 return LTTV_FILTER_STATE
;
571 case LTTV_FILTER_EVENT_NAME
:
572 case LTTV_FILTER_EVENT_FACILITY
:
573 case LTTV_FILTER_EVENT_CATEGORY
:
574 case LTTV_FILTER_EVENT_TIME
:
575 case LTTV_FILTER_EVENT_TSC
:
576 case LTTV_FILTER_EVENT_FIELD
:
577 return LTTV_FILTER_EVENT
;
585 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
587 * Applies the 'equal' operator to the
588 * specified structure and value
589 * @param v1 left member of comparison
590 * @param v2 right member of comparison
591 * @return success/failure of operation
593 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
595 guint
* r
= (guint
*) v1
;
596 return (*r
== v2
.v_uint
);
601 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
603 * Applies the 'equal' operator to the
604 * specified structure and value
605 * @param v1 left member of comparison
606 * @param v2 right member of comparison
607 * @return success/failure of operation
609 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
611 guint64
* r
= (guint64
*) v1
;
612 return (*r
== v2
.v_uint64
);
617 * @fn gboolean lttv_apply_op_eq_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
626 guint32
* r
= (guint32
*) v1
;
627 return (*r
== v2
.v_uint32
);
631 * @fn gboolean lttv_apply_op_eq_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
640 guint16
* r
= (guint16
*) v1
;
641 return (*r
== v2
.v_uint16
);
645 * @fn gboolean lttv_apply_op_eq_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
654 double* r
= (double*) v1
;
655 return (*r
== v2
.v_double
);
659 * @fn gboolean lttv_apply_op_eq_string(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_string(const gpointer v1
, LttvFieldValue v2
) {
668 char* r
= (char*) v1
;
669 return (!g_strcasecmp(r
,v2
.v_string
));
673 * @fn gboolean lttv_apply_op_eq_quark(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_quark(const gpointer v1
, LttvFieldValue v2
) {
682 GQuark
* r
= (GQuark
*) v1
;
683 return (*r
== v2
.v_quark
);
687 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
689 * Applies the '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_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
696 LttTime
* r
= (LttTime
*) v1
;
697 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
701 * @fn gboolean lttv_apply_op_ne_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
710 guint
* r
= (guint
*) v1
;
711 return (*r
!= v2
.v_uint
);
715 * @fn gboolean lttv_apply_op_ne_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
724 guint64
* r
= (guint64
*) v1
;
725 return (*r
!= v2
.v_uint64
);
729 * @fn gboolean lttv_apply_op_ne_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
738 guint32
* r
= (guint32
*) v1
;
739 return (*r
!= v2
.v_uint32
);
743 * @fn gboolean lttv_apply_op_ne_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
752 guint16
* r
= (guint16
*) v1
;
753 return (*r
!= v2
.v_uint16
);
757 * @fn gboolean lttv_apply_op_ne_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
766 double* r
= (double*) v1
;
767 return (*r
!= v2
.v_double
);
771 * @fn gboolean lttv_apply_op_ne_string(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_string(const gpointer v1
, LttvFieldValue v2
) {
780 char* r
= (char*) v1
;
781 return (g_strcasecmp(r
,v2
.v_string
));
785 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
787 * Applies the 'not equal' operator to the
788 * specified structure and value
789 * @param v1 left member of comparison
790 * @param v2 right member of comparison
791 * @return success/failure of operation
793 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
794 GQuark
* r
= (GQuark
*) v1
;
795 return (*r
!= v2
.v_quark
);
800 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
802 * Applies the 'not equal' 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_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
809 LttTime
* r
= (LttTime
*) v1
;
810 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
814 * @fn gboolean lttv_apply_op_lt_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
823 guint
* r
= (guint
*) v1
;
824 return (*r
< v2
.v_uint
);
828 * @fn gboolean lttv_apply_op_lt_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
837 guint64
* r
= (guint64
*) v1
;
838 return (*r
< v2
.v_uint64
);
842 * @fn gboolean lttv_apply_op_lt_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
851 guint32
* r
= (guint32
*) v1
;
852 return (*r
< v2
.v_uint32
);
856 * @fn gboolean lttv_apply_op_lt_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
865 guint16
* r
= (guint16
*) v1
;
866 return (*r
< v2
.v_uint16
);
870 * @fn gboolean lttv_apply_op_lt_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
879 double* r
= (double*) v1
;
880 return (*r
< v2
.v_double
);
884 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
886 * Applies the 'lower than' operator to the
887 * specified structure and value
888 * @param v1 left member of comparison
889 * @param v2 right member of comparison
890 * @return success/failure of operation
892 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
893 LttTime
* r
= (LttTime
*) v1
;
894 // 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)));
895 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
899 * @fn gboolean lttv_apply_op_le_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
908 guint
* r
= (guint
*) v1
;
909 return (*r
<= v2
.v_uint
);
913 * @fn gboolean lttv_apply_op_le_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
922 guint64
* r
= (guint64
*) v1
;
923 return (*r
<= v2
.v_uint64
);
927 * @fn gboolean lttv_apply_op_le_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
936 guint32
* r
= (guint32
*) v1
;
937 return (*r
<= v2
.v_uint32
);
941 * @fn gboolean lttv_apply_op_le_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
950 guint16
* r
= (guint16
*) v1
;
951 return (*r
<= v2
.v_uint16
);
955 * @fn gboolean lttv_apply_op_le_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
964 double* r
= (double*) v1
;
965 return (*r
<= v2
.v_double
);
969 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
971 * Applies the 'lower or equal' operator to the
972 * specified structure and value
973 * @param v1 left member of comparison
974 * @param v2 right member of comparison
975 * @return success/failure of operation
977 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
978 LttTime
* r
= (LttTime
*) v1
;
979 // 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)));
980 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
985 * @fn gboolean lttv_apply_op_gt_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
994 guint
* r
= (guint
*) v1
;
995 return (*r
> v2
.v_uint
);
999 * @fn gboolean lttv_apply_op_gt_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
1008 guint64
* r
= (guint64
*) v1
;
1009 return (*r
> v2
.v_uint64
);
1013 * @fn gboolean lttv_apply_op_gt_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
1022 guint32
* r
= (guint32
*) v1
;
1023 return (*r
> v2
.v_uint32
);
1027 * @fn gboolean lttv_apply_op_gt_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
1036 guint16
* r
= (guint16
*) v1
;
1037 return (*r
> v2
.v_uint16
);
1041 * @fn gboolean lttv_apply_op_gt_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
1050 double* r
= (double*) v1
;
1051 return (*r
> v2
.v_double
);
1055 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1057 * Applies the 'greater than' operator to the
1058 * specified structure and value
1059 * @param v1 left member of comparison
1060 * @param v2 right member of comparison
1061 * @return success/failure of operation
1063 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1064 LttTime
* r
= (LttTime
*) v1
;
1065 // 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)));
1066 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1070 * @fn gboolean lttv_apply_op_ge_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
1079 guint
* r
= (guint
*) v1
;
1080 return (*r
>= v2
.v_uint
);
1084 * @fn gboolean lttv_apply_op_ge_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
1093 guint64
* r
= (guint64
*) v1
;
1094 return (*r
>= v2
.v_uint64
);
1098 * @fn gboolean lttv_apply_op_ge_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
1107 guint32
* r
= (guint32
*) v1
;
1108 return (*r
>= v2
.v_uint32
);
1112 * @fn gboolean lttv_apply_op_ge_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
1121 guint16
* r
= (guint16
*) v1
;
1122 return (*r
>= v2
.v_uint16
);
1126 * @fn gboolean lttv_apply_op_ge_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
1135 double* r
= (double*) v1
;
1136 return (*r
>= v2
.v_double
);
1140 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1142 * Applies the 'greater or equal' operator to the
1143 * specified structure and value
1144 * @param v1 left member of comparison
1145 * @param v2 right member of comparison
1146 * @return success/failure of operation
1148 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1149 LttTime
* r
= (LttTime
*) v1
;
1150 // 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)));
1151 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1157 * Makes a copy of the current filter tree
1158 * @param tree pointer to the current tree
1159 * @return new copy of the filter tree
1162 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1164 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1166 newtree
->node
= tree
->node
;
1168 newtree
->left
= tree
->left
;
1169 if(newtree
->left
== LTTV_TREE_NODE
) {
1170 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1171 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1172 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1173 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1174 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1175 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1176 /* FIXME: special case for string copy ! */
1177 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1180 newtree
->right
= tree
->right
;
1181 if(newtree
->right
== LTTV_TREE_NODE
) {
1182 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1183 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1184 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1185 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1186 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1187 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1188 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1196 * Makes a copy of the current filter
1197 * @param filter pointer to the current filter
1198 * @return new copy of the filter
1201 lttv_filter_clone(const LttvFilter
* filter
) {
1203 if(!filter
) return NULL
;
1205 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1207 strcpy(newfilter
->expression
,filter
->expression
);
1209 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1217 * @fn LttvFilter* lttv_filter_new()
1219 * Creates a new LttvFilter
1220 * @return the current LttvFilter or NULL if error
1225 LttvFilter
* filter
= g_new(LttvFilter
,1);
1226 filter
->expression
= NULL
;
1227 filter
->head
= NULL
;
1234 * @fn gboolean lttv_filter_update(LttvFilter*)
1236 * Updates the current LttvFilter by building
1237 * its tree based upon the expression string
1238 * @param filter pointer to the current LttvFilter
1239 * @return Failure/Success of operation
1242 lttv_filter_update(LttvFilter
* filter
) {
1244 // g_print("filter::lttv_filter_new()\n"); /* debug */
1246 if(filter
->expression
== NULL
) return FALSE
;
1250 p_nesting
=0, /* parenthesis nesting value */
1252 guint expression_len
;
1256 *tree
= lttv_filter_tree_new(), /* main tree */
1257 *subtree
= NULL
, /* buffer for subtrees */
1258 *t1
, /* buffer #1 */
1259 *t2
, /* buffer #2 */
1260 *t3
; /* buffer #3 */
1264 * If the tree already exists,
1265 * destroy it and build a new one
1267 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1268 filter
->head
= NULL
; /* will be assigned at the end */
1272 * each element of the list
1273 * is a sub tree created
1274 * by the use of parenthesis in the
1275 * global expression. The final tree
1276 * will be the one left at the root of
1279 GPtrArray
*tree_stack
= g_ptr_array_new();
1280 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1282 /* temporary values */
1283 GString
*a_field_component
= g_string_new("");
1284 GString
*a_string_spaces
= g_string_new("");
1285 GPtrArray
*a_field_path
= g_ptr_array_new();
1287 /* simple expression buffer */
1288 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1290 gint nest_quotes
= 0;
1293 * Parse entire expression and construct
1294 * the binary tree. There are two steps
1295 * in browsing that string
1296 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1297 * 2. finding simple expressions
1298 * - field path ( separated by dots )
1299 * - op ( >, <, =, >=, <=, !=)
1300 * - value ( integer, string ... )
1301 * To spare computing time, the whole
1302 * string is parsed in this loop for a
1303 * O(n) complexity order.
1305 * When encountering logical op &,|,^
1306 * 1. parse the last value if any
1307 * 2. create a new tree
1308 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1309 * 4. concatenate this tree with the current tree on top of the stack
1310 * When encountering math ops >,>=,<,<=,=,!=
1311 * 1. add to op to the simple expression
1312 * 2. concatenate last field component to field path
1313 * When encountering concatening ops .
1314 * 1. concatenate last field component to field path
1315 * When encountering opening parenthesis (,{,[
1316 * 1. create a new subtree on top of tree stack
1317 * When encountering closing parenthesis ),},]
1318 * 1. add the expression on right child of the current tree
1319 * 2. the subtree is completed, allocate a new subtree
1320 * 3. pop the tree value from the tree stack
1324 struct timeval starttime
;
1325 struct timeval endtime
;
1326 gettimeofday(&starttime
, NULL
);
1329 expression_len
= strlen(filter
->expression
);
1330 for(i
=0;i
<expression_len
;i
++) {
1332 // g_print("%c\n ",filter->expression[i]);
1334 switch(filter
->expression
[i
]) {
1336 if(filter
->expression
[i
+1] == '\"') {
1345 if(a_string_spaces
->len
!= 0) {
1346 a_field_component
= g_string_append(
1347 a_field_component
, a_string_spaces
->str
);
1348 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1350 a_field_component
= g_string_append_c(a_field_component
,
1351 filter
->expression
[i
]);
1355 switch(filter
->expression
[i
]) {
1361 /* get current tree in tree stack */
1362 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1364 /* get current node at absolute right */
1365 while(t1
->right
!= LTTV_TREE_IDLE
) {
1366 g_assert(t1
->right
== LTTV_TREE_NODE
);
1369 t2
= lttv_filter_tree_new();
1370 t2
->node
= LTTV_LOGICAL_AND
;
1371 t1
->right
= LTTV_TREE_NODE
;
1373 if(not) { /* add not operator to tree */
1374 t3
= lttv_filter_tree_new();
1375 t3
->node
= LTTV_LOGICAL_NOT
;
1376 t2
->left
= LTTV_TREE_NODE
;
1381 if(subtree
!= NULL
) { /* append subtree to current tree */
1382 t2
->left
= LTTV_TREE_NODE
;
1383 t2
->l_child
.t
= subtree
;
1385 } else { /* append a simple expression */
1386 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1387 a_field_component
= g_string_new("");
1388 g_string_free(a_string_spaces
, TRUE
);
1389 a_string_spaces
= g_string_new("");
1390 t2
->left
= LTTV_TREE_LEAF
;
1391 t2
->l_child
.leaf
= a_simple_expression
;
1392 a_simple_expression
= lttv_simple_expression_new();
1398 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1399 while(t1
->right
!= LTTV_TREE_IDLE
) {
1400 g_assert(t1
->right
== LTTV_TREE_NODE
);
1403 t2
= lttv_filter_tree_new();
1404 t2
->node
= LTTV_LOGICAL_OR
;
1405 t1
->right
= LTTV_TREE_NODE
;
1407 if(not) { // add not operator to tree
1408 t3
= lttv_filter_tree_new();
1409 t3
->node
= LTTV_LOGICAL_NOT
;
1410 t2
->left
= LTTV_TREE_NODE
;
1415 if(subtree
!= NULL
) { /* append subtree to current tree */
1416 t2
->left
= LTTV_TREE_NODE
;
1417 t2
->l_child
.t
= subtree
;
1419 } else { /* append a simple expression */
1420 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1421 a_field_component
= g_string_new("");
1422 g_string_free(a_string_spaces
, TRUE
);
1423 a_string_spaces
= g_string_new("");
1424 t2
->left
= LTTV_TREE_LEAF
;
1425 t2
->l_child
.leaf
= a_simple_expression
;
1426 a_simple_expression
= lttv_simple_expression_new();
1432 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1433 while(t1
->right
!= LTTV_TREE_IDLE
) {
1434 g_assert(t1
->right
== LTTV_TREE_NODE
);
1437 t2
= lttv_filter_tree_new();
1438 t2
->node
= LTTV_LOGICAL_XOR
;
1439 t1
->right
= LTTV_TREE_NODE
;
1441 if(not) { // add not operator to tree
1442 t3
= lttv_filter_tree_new();
1443 t3
->node
= LTTV_LOGICAL_NOT
;
1444 t2
->left
= LTTV_TREE_NODE
;
1449 if(subtree
!= NULL
) { /* append subtree to current tree */
1450 t2
->left
= LTTV_TREE_NODE
;
1451 t2
->l_child
.t
= subtree
;
1453 } else { /* append a simple expression */
1454 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1455 a_field_component
= g_string_new("");
1456 g_string_free(a_string_spaces
, TRUE
);
1457 a_string_spaces
= g_string_new("");
1458 t2
->left
= LTTV_TREE_LEAF
;
1459 t2
->l_child
.leaf
= a_simple_expression
;
1460 a_simple_expression
= lttv_simple_expression_new();
1464 case '!': /* not, or not equal (math op) */
1466 if(filter
->expression
[i
+1] == '=') { /* != */
1467 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1468 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1469 a_field_component
= g_string_new("");
1470 g_string_free(a_string_spaces
, TRUE
);
1471 a_string_spaces
= g_string_new("");
1472 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1479 case '(': /* start of parenthesis */
1483 p_nesting
++; /* incrementing parenthesis nesting value */
1484 t1
= lttv_filter_tree_new();
1485 if(not) { /* add not operator to tree */
1486 t3
= lttv_filter_tree_new();
1487 t3
->node
= LTTV_LOGICAL_NOT
;
1488 t1
->right
= LTTV_TREE_NODE
;
1492 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1495 case ')': /* end of parenthesis */
1499 p_nesting
--; /* decrementing parenthesis nesting value */
1500 if(p_nesting
<0 || tree_stack
->len
<2) {
1501 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1502 is not valid due to parenthesis incorrect use",filter
->expression
);
1506 /* there must at least be the root tree left in the array */
1507 g_assert(tree_stack
->len
>0);
1509 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1510 while(t1
->right
!= LTTV_TREE_IDLE
) {
1513 if(not) { // add not operator to tree
1515 t3
= lttv_filter_tree_new();
1516 t3
->node
= LTTV_LOGICAL_NOT
;
1517 t1
->right
= LTTV_TREE_NODE
;
1522 if(subtree
!= NULL
) { /* append subtree to current tree */
1523 t1
->right
= LTTV_TREE_NODE
;
1524 t1
->r_child
.t
= subtree
;
1525 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1526 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1527 } else { /* assign subtree as current tree */
1528 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1529 a_field_component
= g_string_new("");
1530 g_string_free(a_string_spaces
, TRUE
);
1531 a_string_spaces
= g_string_new("");
1532 t1
->right
= LTTV_TREE_LEAF
;
1533 t1
->r_child
.leaf
= a_simple_expression
;
1534 a_simple_expression
= lttv_simple_expression_new();
1535 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1540 * mathematic operators
1542 case '<': /* lower, lower or equal */
1544 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1545 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1546 a_field_component
= g_string_new("");
1547 g_string_free(a_string_spaces
, TRUE
);
1548 a_string_spaces
= g_string_new("");
1549 if(filter
->expression
[i
+1] == '=') { /* <= */
1551 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1552 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1555 case '>': /* higher, higher or equal */
1557 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1558 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1559 a_field_component
= g_string_new("");
1560 g_string_free(a_string_spaces
, TRUE
);
1561 a_string_spaces
= g_string_new("");
1562 if(filter
->expression
[i
+1] == '=') { /* >= */
1564 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1565 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1568 case '=': /* equal */
1570 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1571 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1572 a_field_component
= g_string_new("");
1573 g_string_free(a_string_spaces
, TRUE
);
1574 a_string_spaces
= g_string_new("");
1575 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1579 * Field concatening caracter
1584 * divide field expression into elements
1585 * in a_field_path array.
1587 * A dot can also be present in double values
1589 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1590 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1591 a_field_component
= g_string_new("");
1592 g_string_free(a_string_spaces
, TRUE
);
1593 a_string_spaces
= g_string_new("");
1596 case ' ': /* keep spaces that are within a field component */
1597 if(a_field_component
->len
== 0) break; /* ignore */
1599 a_string_spaces
= g_string_append_c(a_string_spaces
,
1600 filter
->expression
[i
]);
1602 case '\n': /* ignore */
1605 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1607 default: /* concatening current string */
1608 if(a_string_spaces
->len
!= 0) {
1609 a_field_component
= g_string_append(
1610 a_field_component
, a_string_spaces
->str
);
1611 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1613 a_field_component
= g_string_append_c(a_field_component
,
1614 filter
->expression
[i
]);
1619 * Preliminary check to see
1620 * if tree was constructed correctly
1623 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1624 is not valid due to parenthesis incorrect use",filter
->expression
);
1628 if(tree_stack
->len
!= 1) /* only root tree should remain */
1632 * processing last element of expression
1634 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1635 while(t1
->right
!= LTTV_TREE_IDLE
) {
1636 g_assert(t1
->right
== LTTV_TREE_NODE
);
1639 if(not) { // add not operator to tree
1640 t3
= lttv_filter_tree_new();
1641 t3
->node
= LTTV_LOGICAL_NOT
;
1642 t1
->right
= LTTV_TREE_NODE
;
1647 if(subtree
!= NULL
) { /* add the subtree */
1648 t1
->right
= LTTV_TREE_NODE
;
1649 t1
->r_child
.t
= subtree
;
1651 } else { /* add a leaf */
1652 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1653 a_field_component
= NULL
;
1654 g_string_free(a_string_spaces
, TRUE
);
1655 a_string_spaces
= NULL
;
1656 t1
->right
= LTTV_TREE_LEAF
;
1657 t1
->r_child
.leaf
= a_simple_expression
;
1658 a_simple_expression
= NULL
;
1662 /* free the pointer array */
1663 g_assert(a_field_path
->len
== 0);
1664 g_ptr_array_free(a_field_path
,TRUE
);
1666 /* free the tree stack -- but keep the root tree */
1667 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1668 g_ptr_array_free(tree_stack
,TRUE
);
1670 /* free the field buffer if allocated */
1671 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1672 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1674 /* free the simple expression buffer if allocated */
1675 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1677 g_assert(filter
->head
!= NULL
); /* tree should exist */
1678 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1681 gettimeofday(&endtime
, NULL
);
1683 /* Calcul du temps de l'algorithme */
1684 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1685 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1686 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1687 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1691 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1692 lttv_print_tree(filter
->head
,0) ;
1693 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1701 * @fn void lttv_filter_destroy(LttvFilter*)
1703 * Destroy the current LttvFilter
1704 * @param filter pointer to the current LttvFilter
1707 lttv_filter_destroy(LttvFilter
* filter
) {
1711 if(filter
->expression
)
1712 g_free(filter
->expression
);
1714 lttv_filter_tree_destroy(filter
->head
);
1720 * @fn LttvFilterTree* lttv_filter_tree_new()
1722 * Assign a new tree for the current expression
1724 * @return pointer of LttvFilterTree
1727 lttv_filter_tree_new() {
1728 LttvFilterTree
* tree
;
1730 tree
= g_new(LttvFilterTree
,1);
1731 tree
->node
= 0; //g_new(lttv_expression,1);
1732 tree
->left
= LTTV_TREE_IDLE
;
1733 tree
->right
= LTTV_TREE_IDLE
;
1734 tree
->r_child
.t
= NULL
;
1735 tree
->l_child
.t
= NULL
;
1741 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1743 * Append a new expression to the expression
1744 * defined in the current filter
1745 * @param filter pointer to the current LttvFilter
1746 * @param expression string that must be appended
1747 * @return Success/Failure of operation
1750 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1752 if(expression
== NULL
) return FALSE
;
1753 if(filter
== NULL
) return FALSE
;
1754 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1756 GString
* s
= g_string_new("");
1757 if(filter
->expression
!= NULL
) {
1758 s
= g_string_append(s
,filter
->expression
);
1759 s
= g_string_append_c(s
,'&');
1761 s
= g_string_append(s
,expression
);
1763 g_free(filter
->expression
);
1764 filter
->expression
= g_string_free(s
,FALSE
);
1766 /* TRUE if construction of tree proceeded without errors */
1767 return lttv_filter_update(filter
);
1772 * @fn void lttv_filter_clear_expression(LttvFilter*)
1774 * Clear the filter expression from the
1775 * current filter and sets its pointer to NULL
1776 * @param filter pointer to the current LttvFilter
1779 lttv_filter_clear_expression(LttvFilter
* filter
) {
1781 if(filter
->expression
!= NULL
) {
1782 g_free(filter
->expression
);
1783 filter
->expression
= NULL
;
1789 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1791 * Destroys the tree and his sub-trees
1792 * @param tree Tree which must be destroyed
1795 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1797 if(tree
== NULL
) return;
1799 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1800 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1802 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1803 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1805 // g_free(tree->node);
1810 * Global parsing function for the current
1812 * @param t pointer to the current LttvFilterTree
1813 * @param event current LttEvent, NULL if not used
1814 * @param tracefile current LttTracefile, NULL if not used
1815 * @param trace current LttTrace, NULL if not used
1816 * @param state current LttvProcessState, NULL if not used
1817 * @param context current LttvTracefileContext, NULL if not used
1818 * @return response of filter
1821 lttv_filter_tree_parse(
1822 const LttvFilterTree
* t
,
1823 const LttEvent
* event
,
1824 const LttTracefile
* tracefile
,
1825 const LttTrace
* trace
,
1826 const LttvTracefileContext
* context
1831 * Each tree is parsed in inorder.
1832 * This way, it's possible to apply the left filter of the
1833 * tree, then decide whether or not the right branch should
1834 * be parsed depending on the linking logical operator
1836 * Each node consists in a
1837 * 1. logical operator
1838 * 2. left child ( node or simple expression )
1839 * 3. right child ( node or simple expression )
1841 * When the child is a simple expression, we must
1842 * before all determine if the expression refers to
1843 * a structure which is whithin observation ( not NULL ).
1844 * -If so, the expression is evaluated.
1845 * -If not, the result is set to TRUE since this particular
1846 * operation does not interfere with the lttv structure
1848 * The result of each simple expression will directly
1849 * affect the next branch. This way, depending on
1850 * the linking logical operator, the parser will decide
1851 * to explore or not the next branch.
1853 * -If result of left branch is 0 / FALSE
1854 * then don't explore right branch and return 0;
1855 * -If result of left branch is 1 / TRUE then explore
1857 * -If result of left branch is 1 / TRUE
1858 * then don't explore right branch and return 1;
1859 * -If result of left branch is 0 / FALSE then explore
1861 * -Result of left branch will not affect exploration of
1865 gboolean lresult
= FALSE
, rresult
= FALSE
;
1867 LttvProcessState
* state
;
1869 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1870 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1871 guint cpu
= tfs
->cpu
;
1872 state
= ts
->running_process
[cpu
];
1877 if(t
->left
== LTTV_TREE_NODE
) {
1878 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1880 else if(t
->left
== LTTV_TREE_LEAF
) {
1881 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1885 * Parse linking operator
1886 * make a cutoff if possible
1888 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1889 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1892 * Parse right branch
1894 if(t
->right
== LTTV_TREE_NODE
) {
1895 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1897 else if(t
->right
== LTTV_TREE_LEAF
) {
1898 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1903 * Apply and return the
1904 * logical link between the
1908 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1909 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1910 case LTTV_LOGICAL_NOT
:
1911 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1912 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1913 case 0: return (rresult
);
1916 * This case should never be
1917 * parsed, if so, this subtree
1926 * This function parses a particular branch of the tree
1927 * @param se pointer to the current LttvSimpleExpression
1928 * @param event current LttEvent, NULL if not used
1929 * @param tracefile current LttTracefile, NULL if not used
1930 * @param trace current LttTrace, NULL if not used
1931 * @param state current LttvProcessState, NULL if not used
1932 * @param context current LttvTracefileContext, NULL if not used
1933 * @return response of filter
1936 lttv_filter_tree_parse_branch(
1937 const LttvSimpleExpression
* se
,
1938 const LttEvent
* event
,
1939 const LttTracefile
* tracefile
,
1940 const LttTrace
* trace
,
1941 const LttvProcessState
* state
,
1942 const LttvTracefileContext
* context
) {
1947 case LTTV_FILTER_TRACE_NAME
:
1948 if(trace
== NULL
) return TRUE
;
1950 GQuark quark
= ltt_trace_name(trace
);
1951 return se
->op((gpointer
)&quark
,v
);
1954 case LTTV_FILTER_TRACEFILE_NAME
:
1955 if(tracefile
== NULL
) return TRUE
;
1957 GQuark quark
= ltt_tracefile_name(tracefile
);
1958 return se
->op((gpointer
)&quark
,v
);
1961 case LTTV_FILTER_STATE_PID
:
1962 if(state
== NULL
) return TRUE
;
1963 else return se
->op((gpointer
)&state
->pid
,v
);
1965 case LTTV_FILTER_STATE_PPID
:
1966 if(state
== NULL
) return TRUE
;
1967 else return se
->op((gpointer
)&state
->ppid
,v
);
1969 case LTTV_FILTER_STATE_CT
:
1970 if(state
== NULL
) return TRUE
;
1972 return se
->op((gpointer
)&state
->creation_time
,v
);
1975 case LTTV_FILTER_STATE_IT
:
1976 if(state
== NULL
) return TRUE
;
1978 return se
->op((gpointer
)&state
->insertion_time
,v
);
1981 case LTTV_FILTER_STATE_P_NAME
:
1982 if(state
== NULL
) return TRUE
;
1984 GQuark quark
= state
->name
;
1985 return se
->op((gpointer
)&quark
,v
);
1988 case LTTV_FILTER_STATE_T_BRAND
:
1989 if(state
== NULL
) return TRUE
;
1991 GQuark quark
= state
->brand
;
1992 return se
->op((gpointer
)&quark
,v
);
1995 case LTTV_FILTER_STATE_EX_MODE
:
1996 if(state
== NULL
) return TRUE
;
1997 else return se
->op((gpointer
)&state
->state
->t
,v
);
1999 case LTTV_FILTER_STATE_EX_SUBMODE
:
2000 if(state
== NULL
) return TRUE
;
2001 else return se
->op((gpointer
)&state
->state
->n
,v
);
2003 case LTTV_FILTER_STATE_P_STATUS
:
2004 if(state
== NULL
) return TRUE
;
2005 else return se
->op((gpointer
)&state
->state
->s
,v
);
2007 case LTTV_FILTER_STATE_CPU
:
2008 if(context
== NULL
) return TRUE
;
2010 if(state
== NULL
) return TRUE
;
2011 else return se
->op((gpointer
)&state
->cpu
,v
);
2014 case LTTV_FILTER_EVENT_NAME
:
2015 if(event
== NULL
) return TRUE
;
2018 et
= ltt_event_eventtype(event
);
2019 GQuark quark
= ltt_eventtype_name(et
);
2020 return se
->op((gpointer
)&quark
,v
);
2023 case LTTV_FILTER_EVENT_FACILITY
:
2024 if(event
== NULL
) return TRUE
;
2027 fac
= ltt_event_facility(event
);
2028 GQuark quark
= ltt_facility_name(fac
);
2029 return se
->op((gpointer
)&quark
,v
);
2032 case LTTV_FILTER_EVENT_CATEGORY
:
2034 * TODO: Not yet implemented
2038 case LTTV_FILTER_EVENT_TIME
:
2039 if(event
== NULL
) return TRUE
;
2041 LttTime time
= ltt_event_time(event
);
2042 return se
->op((gpointer
)&time
,v
);
2045 case LTTV_FILTER_EVENT_TSC
:
2046 if(event
== NULL
) return TRUE
;
2048 LttCycleCount count
= ltt_event_cycle_count(event
);
2049 return se
->op((gpointer
)&count
,v
);
2052 case LTTV_FILTER_EVENT_FIELD
:
2054 * TODO: Use the offset to
2055 * find the dynamic field
2056 * in the event struct
2061 * This case should never be
2062 * parsed, if so, the whole
2063 * filtering is cancelled
2065 g_warning("Error while parsing the filter tree");
2069 /* should never get here */
2077 * Debug function. Prints tree memory allocation.
2078 * @param t the pointer to the current LttvFilterTree
2081 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2083 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2084 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2085 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2087 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2088 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"));
2089 if(t
->left
== LTTV_TREE_LEAF
) {
2090 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2091 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2092 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2094 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"));
2095 if(t
->right
== LTTV_TREE_LEAF
) {
2096 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2097 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2098 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2101 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2102 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2106 * @fn static void module_init()
2108 * Initializes the filter module and specific values
2110 static void module_init()
2116 * Destroys the filter module and specific values
2118 static void module_destroy()
2124 LTTV_MODULE("filter", "Filters traceset and events", \
2125 "Filters traceset and events specifically to user input", \
2126 module_init
, module_destroy
)