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)
38 * | |->target_pid (target PID of the event)
43 * | |->"sub-field name"
45 * | |->"leaf-field name" (field type)
47 * | |->name (String, converted to GQuark)
49 * | |->name (String, converted to GQuark)
53 * |->creation_time (LttTime)
54 * |->insertion_time (LttTime)
55 * |->process_name (String, converted to GQuark)
56 * |->thread_brand (String, converted to GQuark)
57 * |->execution_mode (LttvExecutionMode)
58 * |->execution_submode (LttvExecutionSubmode)
59 * |->process_status (LttvProcessStatus)
66 * - refine switch of expression in multiple uses functions
67 * - remove the idle expressions in the tree
80 #include <lttv/lttv.h>
81 #include <lttv/filter.h>
82 #include <ltt/trace.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
168 * state.execution_mode
169 * state.execution_submode
170 * state.process_status
173 g_string_free(f
,TRUE
);
174 f
=g_ptr_array_remove_index(fp
,0);
175 if(!g_strcasecmp(f
->str
,"pid") ) {
176 se
->field
= LTTV_FILTER_STATE_PID
;
178 else if(!g_strcasecmp(f
->str
,"ppid") ) {
179 se
->field
= LTTV_FILTER_STATE_PPID
;
181 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
182 se
->field
= LTTV_FILTER_STATE_CT
;
184 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
185 se
->field
= LTTV_FILTER_STATE_IT
;
187 else if(!g_strcasecmp(f
->str
,"process_name") ) {
188 se
->field
= LTTV_FILTER_STATE_P_NAME
;
190 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
191 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
193 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
194 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
196 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
197 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
199 else if(!g_strcasecmp(f
->str
,"process_status") ) {
200 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
202 else if(!g_strcasecmp(f
->str
,"cpu") ) {
203 se
->field
= LTTV_FILTER_STATE_CPU
;
205 } 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
,"category") ) {
222 * FIXME: Category not yet functional in lttv
224 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
226 else if(!g_strcasecmp(f
->str
,"time") ) {
227 se
->field
= LTTV_FILTER_EVENT_TIME
;
229 else if(!g_strcasecmp(f
->str
,"tsc") ) {
230 se
->field
= LTTV_FILTER_EVENT_TSC
;
232 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
233 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
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_STATE_EX_MODE
:
287 case LTTV_FILTER_STATE_EX_SUBMODE
:
288 case LTTV_FILTER_STATE_P_STATUS
:
291 se
->op
= lttv_apply_op_eq_quark
;
294 se
->op
= lttv_apply_op_ne_quark
;
297 g_warning("Error encountered in operator assignment = or != expected");
304 case LTTV_FILTER_EVENT_TSC
:
307 se
->op
= lttv_apply_op_eq_uint64
;
310 se
->op
= lttv_apply_op_ne_uint64
;
313 se
->op
= lttv_apply_op_lt_uint64
;
316 se
->op
= lttv_apply_op_le_uint64
;
319 se
->op
= lttv_apply_op_gt_uint64
;
322 se
->op
= lttv_apply_op_ge_uint64
;
325 g_warning("Error encountered in operator assignment");
332 case LTTV_FILTER_STATE_CPU
:
333 case LTTV_FILTER_STATE_PID
:
334 case LTTV_FILTER_STATE_PPID
:
335 case LTTV_FILTER_EVENT_TARGET_PID
:
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_STATE_EX_MODE
:
449 case LTTV_FILTER_STATE_EX_SUBMODE
:
450 case LTTV_FILTER_STATE_P_STATUS
:
451 // se->value.v_string = value;
452 se
->value
.v_quark
= g_quark_from_string(value
);
456 * integer -- supposed to be uint64
458 case LTTV_FILTER_EVENT_TSC
:
459 se
->value
.v_uint64
= atoi(value
);
465 case LTTV_FILTER_STATE_PID
:
466 case LTTV_FILTER_STATE_PPID
:
467 case LTTV_FILTER_STATE_CPU
:
468 case LTTV_FILTER_EVENT_TARGET_PID
:
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
;
508 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
518 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
520 * Disallocate memory for the current
522 * @param se pointer to the current LttvSimpleExpression
525 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
527 // g_free(se->value);
528 // switch(se->field) {
529 // case LTTV_FILTER_TRACE_NAME:
530 // case LTTV_FILTER_TRACEFILE_NAME:
531 // case LTTV_FILTER_STATE_P_NAME:
532 // case LTTV_FILTER_EVENT_NAME:
533 // g_free(se->value.v_string);
541 * @fn gint lttv_struct_type(gint)
543 * Finds the structure type depending
544 * on the fields in parameters
545 * @params ft Field of the current structure
546 * @return LttvStructType enum or -1 for error
549 lttv_struct_type(gint ft
) {
552 case LTTV_FILTER_TRACE_NAME
:
553 return LTTV_FILTER_TRACE
;
555 case LTTV_FILTER_TRACEFILE_NAME
:
556 return LTTV_FILTER_TRACEFILE
;
558 case LTTV_FILTER_STATE_PID
:
559 case LTTV_FILTER_STATE_PPID
:
560 case LTTV_FILTER_STATE_CT
:
561 case LTTV_FILTER_STATE_IT
:
562 case LTTV_FILTER_STATE_P_NAME
:
563 case LTTV_FILTER_STATE_T_BRAND
:
564 case LTTV_FILTER_STATE_EX_MODE
:
565 case LTTV_FILTER_STATE_EX_SUBMODE
:
566 case LTTV_FILTER_STATE_P_STATUS
:
567 case LTTV_FILTER_STATE_CPU
:
568 return LTTV_FILTER_STATE
;
570 case LTTV_FILTER_EVENT_NAME
:
571 case LTTV_FILTER_EVENT_CATEGORY
:
572 case LTTV_FILTER_EVENT_TIME
:
573 case LTTV_FILTER_EVENT_TSC
:
574 case LTTV_FILTER_EVENT_TARGET_PID
:
575 case LTTV_FILTER_EVENT_FIELD
:
576 return LTTV_FILTER_EVENT
;
584 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
586 * Applies the 'equal' operator to the
587 * specified structure and value
588 * @param v1 left member of comparison
589 * @param v2 right member of comparison
590 * @return success/failure of operation
592 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
594 guint
* r
= (guint
*) v1
;
595 return (*r
== v2
.v_uint
);
600 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
602 * Applies the 'equal' operator to the
603 * specified structure and value
604 * @param v1 left member of comparison
605 * @param v2 right member of comparison
606 * @return success/failure of operation
608 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
610 guint64
* r
= (guint64
*) v1
;
611 return (*r
== v2
.v_uint64
);
616 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
618 * Applies the 'equal' operator to the
619 * specified structure and value
620 * @param v1 left member of comparison
621 * @param v2 right member of comparison
622 * @return success/failure of operation
624 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
625 guint32
* r
= (guint32
*) v1
;
626 return (*r
== v2
.v_uint32
);
630 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
632 * Applies the 'equal' operator to the
633 * specified structure and value
634 * @param v1 left member of comparison
635 * @param v2 right member of comparison
636 * @return success/failure of operation
638 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
639 guint16
* r
= (guint16
*) v1
;
640 return (*r
== v2
.v_uint16
);
644 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
646 * Applies the 'equal' operator to the
647 * specified structure and value
648 * @param v1 left member of comparison
649 * @param v2 right member of comparison
650 * @return success/failure of operation
652 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
653 double* r
= (double*) v1
;
654 return (*r
== v2
.v_double
);
658 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
660 * Applies the 'equal' operator to the
661 * specified structure and value
662 * @param v1 left member of comparison
663 * @param v2 right member of comparison
664 * @return success/failure of operation
666 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
667 char* r
= (char*) v1
;
668 return (!g_strcasecmp(r
,v2
.v_string
));
672 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
674 * Applies the 'equal' operator to the
675 * specified structure and value
676 * @param v1 left member of comparison
677 * @param v2 right member of comparison
678 * @return success/failure of operation
680 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
681 GQuark
* r
= (GQuark
*) v1
;
682 return (*r
== v2
.v_quark
);
686 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
688 * Applies the 'equal' operator to the
689 * specified structure and value
690 * @param v1 left member of comparison
691 * @param v2 right member of comparison
692 * @return success/failure of operation
694 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
695 LttTime
* r
= (LttTime
*) v1
;
696 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
700 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
702 * Applies the 'not equal' operator to the
703 * specified structure and value
704 * @param v1 left member of comparison
705 * @param v2 right member of comparison
706 * @return success/failure of operation
708 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
709 guint
* r
= (guint
*) v1
;
710 return (*r
!= v2
.v_uint
);
714 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
716 * Applies the 'not equal' operator to the
717 * specified structure and value
718 * @param v1 left member of comparison
719 * @param v2 right member of comparison
720 * @return success/failure of operation
722 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
723 guint64
* r
= (guint64
*) v1
;
724 return (*r
!= v2
.v_uint64
);
728 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
730 * Applies the 'not equal' operator to the
731 * specified structure and value
732 * @param v1 left member of comparison
733 * @param v2 right member of comparison
734 * @return success/failure of operation
736 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
737 guint32
* r
= (guint32
*) v1
;
738 return (*r
!= v2
.v_uint32
);
742 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
744 * Applies the 'not equal' operator to the
745 * specified structure and value
746 * @param v1 left member of comparison
747 * @param v2 right member of comparison
748 * @return success/failure of operation
750 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
751 guint16
* r
= (guint16
*) v1
;
752 return (*r
!= v2
.v_uint16
);
756 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
758 * Applies the 'not equal' operator to the
759 * specified structure and value
760 * @param v1 left member of comparison
761 * @param v2 right member of comparison
762 * @return success/failure of operation
764 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
765 double* r
= (double*) v1
;
766 return (*r
!= v2
.v_double
);
770 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
772 * Applies the 'not equal' operator to the
773 * specified structure and value
774 * @param v1 left member of comparison
775 * @param v2 right member of comparison
776 * @return success/failure of operation
778 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
779 char* r
= (char*) v1
;
780 return (g_strcasecmp(r
,v2
.v_string
));
784 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
786 * Applies the 'not equal' operator to the
787 * specified structure and value
788 * @param v1 left member of comparison
789 * @param v2 right member of comparison
790 * @return success/failure of operation
792 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
793 GQuark
* r
= (GQuark
*) v1
;
794 return (*r
!= v2
.v_quark
);
799 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
801 * Applies the 'not equal' operator to the
802 * specified structure and value
803 * @param v1 left member of comparison
804 * @param v2 right member of comparison
805 * @return success/failure of operation
807 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
808 LttTime
* r
= (LttTime
*) v1
;
809 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
813 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
815 * Applies the 'lower than' operator to the
816 * specified structure and value
817 * @param v1 left member of comparison
818 * @param v2 right member of comparison
819 * @return success/failure of operation
821 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
822 guint
* r
= (guint
*) v1
;
823 return (*r
< v2
.v_uint
);
827 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
829 * Applies the 'lower than' operator to the
830 * specified structure and value
831 * @param v1 left member of comparison
832 * @param v2 right member of comparison
833 * @return success/failure of operation
835 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
836 guint64
* r
= (guint64
*) v1
;
837 return (*r
< v2
.v_uint64
);
841 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
843 * Applies the 'lower than' operator to the
844 * specified structure and value
845 * @param v1 left member of comparison
846 * @param v2 right member of comparison
847 * @return success/failure of operation
849 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
850 guint32
* r
= (guint32
*) v1
;
851 return (*r
< v2
.v_uint32
);
855 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
857 * Applies the 'lower than' operator to the
858 * specified structure and value
859 * @param v1 left member of comparison
860 * @param v2 right member of comparison
861 * @return success/failure of operation
863 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
864 guint16
* r
= (guint16
*) v1
;
865 return (*r
< v2
.v_uint16
);
869 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
871 * Applies the 'lower than' operator to the
872 * specified structure and value
873 * @param v1 left member of comparison
874 * @param v2 right member of comparison
875 * @return success/failure of operation
877 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
878 double* r
= (double*) v1
;
879 return (*r
< v2
.v_double
);
883 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
885 * Applies the 'lower than' operator to the
886 * specified structure and value
887 * @param v1 left member of comparison
888 * @param v2 right member of comparison
889 * @return success/failure of operation
891 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
892 LttTime
* r
= (LttTime
*) v1
;
893 // 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)));
894 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
898 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
900 * Applies the 'lower or equal' operator to the
901 * specified structure and value
902 * @param v1 left member of comparison
903 * @param v2 right member of comparison
904 * @return success/failure of operation
906 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
907 guint
* r
= (guint
*) v1
;
908 return (*r
<= v2
.v_uint
);
912 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
914 * Applies the 'lower or equal' operator to the
915 * specified structure and value
916 * @param v1 left member of comparison
917 * @param v2 right member of comparison
918 * @return success/failure of operation
920 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
921 guint64
* r
= (guint64
*) v1
;
922 return (*r
<= v2
.v_uint64
);
926 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
928 * Applies the 'lower or equal' operator to the
929 * specified structure and value
930 * @param v1 left member of comparison
931 * @param v2 right member of comparison
932 * @return success/failure of operation
934 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
935 guint32
* r
= (guint32
*) v1
;
936 return (*r
<= v2
.v_uint32
);
940 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
942 * Applies the 'lower or equal' operator to the
943 * specified structure and value
944 * @param v1 left member of comparison
945 * @param v2 right member of comparison
946 * @return success/failure of operation
948 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
949 guint16
* r
= (guint16
*) v1
;
950 return (*r
<= v2
.v_uint16
);
954 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
956 * Applies the 'lower or equal' operator to the
957 * specified structure and value
958 * @param v1 left member of comparison
959 * @param v2 right member of comparison
960 * @return success/failure of operation
962 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
963 double* r
= (double*) v1
;
964 return (*r
<= v2
.v_double
);
968 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
970 * Applies the 'lower or equal' operator to the
971 * specified structure and value
972 * @param v1 left member of comparison
973 * @param v2 right member of comparison
974 * @return success/failure of operation
976 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
977 LttTime
* r
= (LttTime
*) v1
;
978 // 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)));
979 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
984 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
986 * Applies the 'greater than' operator to the
987 * specified structure and value
988 * @param v1 left member of comparison
989 * @param v2 right member of comparison
990 * @return success/failure of operation
992 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
993 guint
* r
= (guint
*) v1
;
994 return (*r
> v2
.v_uint
);
998 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1000 * Applies the 'greater than' operator to the
1001 * specified structure and value
1002 * @param v1 left member of comparison
1003 * @param v2 right member of comparison
1004 * @return success/failure of operation
1006 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1007 guint64
* r
= (guint64
*) v1
;
1008 return (*r
> v2
.v_uint64
);
1012 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1014 * Applies the 'greater than' operator to the
1015 * specified structure and value
1016 * @param v1 left member of comparison
1017 * @param v2 right member of comparison
1018 * @return success/failure of operation
1020 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1021 guint32
* r
= (guint32
*) v1
;
1022 return (*r
> v2
.v_uint32
);
1026 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1028 * Applies the 'greater than' operator to the
1029 * specified structure and value
1030 * @param v1 left member of comparison
1031 * @param v2 right member of comparison
1032 * @return success/failure of operation
1034 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1035 guint16
* r
= (guint16
*) v1
;
1036 return (*r
> v2
.v_uint16
);
1040 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1042 * Applies the 'greater than' operator to the
1043 * specified structure and value
1044 * @param v1 left member of comparison
1045 * @param v2 right member of comparison
1046 * @return success/failure of operation
1048 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1049 double* r
= (double*) v1
;
1050 return (*r
> v2
.v_double
);
1054 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1056 * Applies the 'greater than' operator to the
1057 * specified structure and value
1058 * @param v1 left member of comparison
1059 * @param v2 right member of comparison
1060 * @return success/failure of operation
1062 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1063 LttTime
* r
= (LttTime
*) v1
;
1064 // 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)));
1065 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1069 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1071 * Applies the 'greater or equal' operator to the
1072 * specified structure and value
1073 * @param v1 left member of comparison
1074 * @param v2 right member of comparison
1075 * @return success/failure of operation
1077 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1078 guint
* r
= (guint
*) v1
;
1079 return (*r
>= v2
.v_uint
);
1083 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1085 * Applies the 'greater or equal' operator to the
1086 * specified structure and value
1087 * @param v1 left member of comparison
1088 * @param v2 right member of comparison
1089 * @return success/failure of operation
1091 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1092 guint64
* r
= (guint64
*) v1
;
1093 return (*r
>= v2
.v_uint64
);
1097 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1099 * Applies the 'greater or equal' operator to the
1100 * specified structure and value
1101 * @param v1 left member of comparison
1102 * @param v2 right member of comparison
1103 * @return success/failure of operation
1105 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1106 guint32
* r
= (guint32
*) v1
;
1107 return (*r
>= v2
.v_uint32
);
1111 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1113 * Applies the 'greater or equal' operator to the
1114 * specified structure and value
1115 * @param v1 left member of comparison
1116 * @param v2 right member of comparison
1117 * @return success/failure of operation
1119 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1120 guint16
* r
= (guint16
*) v1
;
1121 return (*r
>= v2
.v_uint16
);
1125 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1127 * Applies the 'greater or equal' operator to the
1128 * specified structure and value
1129 * @param v1 left member of comparison
1130 * @param v2 right member of comparison
1131 * @return success/failure of operation
1133 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1134 double* r
= (double*) v1
;
1135 return (*r
>= v2
.v_double
);
1139 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1141 * Applies the 'greater or equal' operator to the
1142 * specified structure and value
1143 * @param v1 left member of comparison
1144 * @param v2 right member of comparison
1145 * @return success/failure of operation
1147 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1148 LttTime
* r
= (LttTime
*) v1
;
1149 // 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)));
1150 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1156 * Makes a copy of the current filter tree
1157 * @param tree pointer to the current tree
1158 * @return new copy of the filter tree
1161 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1163 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1165 newtree
->node
= tree
->node
;
1167 newtree
->left
= tree
->left
;
1168 if(newtree
->left
== LTTV_TREE_NODE
) {
1169 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1170 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1171 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1172 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1173 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1174 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1175 /* FIXME: special case for string copy ! */
1176 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1179 newtree
->right
= tree
->right
;
1180 if(newtree
->right
== LTTV_TREE_NODE
) {
1181 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1182 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1183 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1184 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1185 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1186 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1187 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1195 * Makes a copy of the current filter
1196 * @param filter pointer to the current filter
1197 * @return new copy of the filter
1200 lttv_filter_clone(const LttvFilter
* filter
) {
1202 if(!filter
) return NULL
;
1204 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1206 strcpy(newfilter
->expression
,filter
->expression
);
1208 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1216 * @fn LttvFilter* lttv_filter_new()
1218 * Creates a new LttvFilter
1219 * @return the current LttvFilter or NULL if error
1224 LttvFilter
* filter
= g_new(LttvFilter
,1);
1225 filter
->expression
= NULL
;
1226 filter
->head
= NULL
;
1233 * @fn gboolean lttv_filter_update(LttvFilter*)
1235 * Updates the current LttvFilter by building
1236 * its tree based upon the expression string
1237 * @param filter pointer to the current LttvFilter
1238 * @return Failure/Success of operation
1241 lttv_filter_update(LttvFilter
* filter
) {
1243 // g_print("filter::lttv_filter_new()\n"); /* debug */
1245 if(filter
->expression
== NULL
) return FALSE
;
1249 p_nesting
=0, /* parenthesis nesting value */
1251 guint expression_len
;
1255 *tree
= lttv_filter_tree_new(), /* main tree */
1256 *subtree
= NULL
, /* buffer for subtrees */
1257 *t1
, /* buffer #1 */
1258 *t2
, /* buffer #2 */
1259 *t3
; /* buffer #3 */
1263 * If the tree already exists,
1264 * destroy it and build a new one
1266 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1267 filter
->head
= NULL
; /* will be assigned at the end */
1271 * each element of the list
1272 * is a sub tree created
1273 * by the use of parenthesis in the
1274 * global expression. The final tree
1275 * will be the one left at the root of
1278 GPtrArray
*tree_stack
= g_ptr_array_new();
1279 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1281 /* temporary values */
1282 GString
*a_field_component
= g_string_new("");
1283 GString
*a_string_spaces
= g_string_new("");
1284 GPtrArray
*a_field_path
= g_ptr_array_new();
1286 /* simple expression buffer */
1287 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1289 gint nest_quotes
= 0;
1292 * Parse entire expression and construct
1293 * the binary tree. There are two steps
1294 * in browsing that string
1295 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1296 * 2. finding simple expressions
1297 * - field path ( separated by dots )
1298 * - op ( >, <, =, >=, <=, !=)
1299 * - value ( integer, string ... )
1300 * To spare computing time, the whole
1301 * string is parsed in this loop for a
1302 * O(n) complexity order.
1304 * When encountering logical op &,|,^
1305 * 1. parse the last value if any
1306 * 2. create a new tree
1307 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1308 * 4. concatenate this tree with the current tree on top of the stack
1309 * When encountering math ops >,>=,<,<=,=,!=
1310 * 1. add to op to the simple expression
1311 * 2. concatenate last field component to field path
1312 * When encountering concatening ops .
1313 * 1. concatenate last field component to field path
1314 * When encountering opening parenthesis (,{,[
1315 * 1. create a new subtree on top of tree stack
1316 * When encountering closing parenthesis ),},]
1317 * 1. add the expression on right child of the current tree
1318 * 2. the subtree is completed, allocate a new subtree
1319 * 3. pop the tree value from the tree stack
1323 struct timeval starttime
;
1324 struct timeval endtime
;
1325 gettimeofday(&starttime
, NULL
);
1328 expression_len
= strlen(filter
->expression
);
1329 for(i
=0;i
<expression_len
;i
++) {
1331 // g_print("%c\n ",filter->expression[i]);
1333 switch(filter
->expression
[i
]) {
1335 if(filter
->expression
[i
+1] == '\"') {
1344 if(a_string_spaces
->len
!= 0) {
1345 a_field_component
= g_string_append(
1346 a_field_component
, a_string_spaces
->str
);
1347 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1349 a_field_component
= g_string_append_c(a_field_component
,
1350 filter
->expression
[i
]);
1354 switch(filter
->expression
[i
]) {
1360 /* get current tree in tree stack */
1361 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1363 /* get current node at absolute right */
1364 while(t1
->right
!= LTTV_TREE_IDLE
) {
1365 g_assert(t1
->right
== LTTV_TREE_NODE
);
1368 t2
= lttv_filter_tree_new();
1369 t2
->node
= LTTV_LOGICAL_AND
;
1370 t1
->right
= LTTV_TREE_NODE
;
1372 if(not) { /* add not operator to tree */
1373 t3
= lttv_filter_tree_new();
1374 t3
->node
= LTTV_LOGICAL_NOT
;
1375 t2
->left
= LTTV_TREE_NODE
;
1380 if(subtree
!= NULL
) { /* append subtree to current tree */
1381 t2
->left
= LTTV_TREE_NODE
;
1382 t2
->l_child
.t
= subtree
;
1384 } else { /* append a simple expression */
1385 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1386 a_field_component
= g_string_new("");
1387 g_string_free(a_string_spaces
, TRUE
);
1388 a_string_spaces
= g_string_new("");
1389 t2
->left
= LTTV_TREE_LEAF
;
1390 t2
->l_child
.leaf
= a_simple_expression
;
1391 a_simple_expression
= lttv_simple_expression_new();
1397 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1398 while(t1
->right
!= LTTV_TREE_IDLE
) {
1399 g_assert(t1
->right
== LTTV_TREE_NODE
);
1402 t2
= lttv_filter_tree_new();
1403 t2
->node
= LTTV_LOGICAL_OR
;
1404 t1
->right
= LTTV_TREE_NODE
;
1406 if(not) { // add not operator to tree
1407 t3
= lttv_filter_tree_new();
1408 t3
->node
= LTTV_LOGICAL_NOT
;
1409 t2
->left
= LTTV_TREE_NODE
;
1414 if(subtree
!= NULL
) { /* append subtree to current tree */
1415 t2
->left
= LTTV_TREE_NODE
;
1416 t2
->l_child
.t
= subtree
;
1418 } else { /* append a simple expression */
1419 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1420 a_field_component
= g_string_new("");
1421 g_string_free(a_string_spaces
, TRUE
);
1422 a_string_spaces
= g_string_new("");
1423 t2
->left
= LTTV_TREE_LEAF
;
1424 t2
->l_child
.leaf
= a_simple_expression
;
1425 a_simple_expression
= lttv_simple_expression_new();
1431 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1432 while(t1
->right
!= LTTV_TREE_IDLE
) {
1433 g_assert(t1
->right
== LTTV_TREE_NODE
);
1436 t2
= lttv_filter_tree_new();
1437 t2
->node
= LTTV_LOGICAL_XOR
;
1438 t1
->right
= LTTV_TREE_NODE
;
1440 if(not) { // add not operator to tree
1441 t3
= lttv_filter_tree_new();
1442 t3
->node
= LTTV_LOGICAL_NOT
;
1443 t2
->left
= LTTV_TREE_NODE
;
1448 if(subtree
!= NULL
) { /* append subtree to current tree */
1449 t2
->left
= LTTV_TREE_NODE
;
1450 t2
->l_child
.t
= subtree
;
1452 } else { /* append a simple expression */
1453 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1454 a_field_component
= g_string_new("");
1455 g_string_free(a_string_spaces
, TRUE
);
1456 a_string_spaces
= g_string_new("");
1457 t2
->left
= LTTV_TREE_LEAF
;
1458 t2
->l_child
.leaf
= a_simple_expression
;
1459 a_simple_expression
= lttv_simple_expression_new();
1463 case '!': /* not, or not equal (math op) */
1465 if(filter
->expression
[i
+1] == '=') { /* != */
1466 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1467 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1468 a_field_component
= g_string_new("");
1469 g_string_free(a_string_spaces
, TRUE
);
1470 a_string_spaces
= g_string_new("");
1471 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1478 case '(': /* start of parenthesis */
1482 p_nesting
++; /* incrementing parenthesis nesting value */
1483 t1
= lttv_filter_tree_new();
1484 if(not) { /* add not operator to tree */
1485 t3
= lttv_filter_tree_new();
1486 t3
->node
= LTTV_LOGICAL_NOT
;
1487 t1
->right
= LTTV_TREE_NODE
;
1491 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1494 case ')': /* end of parenthesis */
1498 p_nesting
--; /* decrementing parenthesis nesting value */
1499 if(p_nesting
<0 || tree_stack
->len
<2) {
1500 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1501 is not valid due to parenthesis incorrect use",filter
->expression
);
1505 /* there must at least be the root tree left in the array */
1506 g_assert(tree_stack
->len
>0);
1508 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1509 while(t1
->right
!= LTTV_TREE_IDLE
) {
1512 if(not) { // add not operator to tree
1514 t3
= lttv_filter_tree_new();
1515 t3
->node
= LTTV_LOGICAL_NOT
;
1516 t1
->right
= LTTV_TREE_NODE
;
1521 if(subtree
!= NULL
) { /* append subtree to current tree */
1522 t1
->right
= LTTV_TREE_NODE
;
1523 t1
->r_child
.t
= subtree
;
1524 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1525 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1526 } else { /* assign subtree as current tree */
1527 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1528 a_field_component
= g_string_new("");
1529 g_string_free(a_string_spaces
, TRUE
);
1530 a_string_spaces
= g_string_new("");
1531 t1
->right
= LTTV_TREE_LEAF
;
1532 t1
->r_child
.leaf
= a_simple_expression
;
1533 a_simple_expression
= lttv_simple_expression_new();
1534 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1539 * mathematic operators
1541 case '<': /* lower, lower or equal */
1543 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1544 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1545 a_field_component
= g_string_new("");
1546 g_string_free(a_string_spaces
, TRUE
);
1547 a_string_spaces
= g_string_new("");
1548 if(filter
->expression
[i
+1] == '=') { /* <= */
1550 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1551 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1554 case '>': /* higher, higher or equal */
1556 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1557 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1558 a_field_component
= g_string_new("");
1559 g_string_free(a_string_spaces
, TRUE
);
1560 a_string_spaces
= g_string_new("");
1561 if(filter
->expression
[i
+1] == '=') { /* >= */
1563 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1564 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1567 case '=': /* equal */
1569 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1570 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1571 a_field_component
= g_string_new("");
1572 g_string_free(a_string_spaces
, TRUE
);
1573 a_string_spaces
= g_string_new("");
1574 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1578 * Field concatening caracter
1583 * divide field expression into elements
1584 * in a_field_path array.
1586 * A dot can also be present in double values
1588 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1589 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1590 a_field_component
= g_string_new("");
1591 g_string_free(a_string_spaces
, TRUE
);
1592 a_string_spaces
= g_string_new("");
1594 /* Operator found, we are in the value field */
1595 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1598 case ' ': /* keep spaces that are within a field component */
1599 if(a_field_component
->len
== 0) break; /* ignore */
1601 a_string_spaces
= g_string_append_c(a_string_spaces
,
1602 filter
->expression
[i
]);
1604 case '\n': /* ignore */
1607 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1609 default: /* concatening current string */
1610 if(a_string_spaces
->len
!= 0) {
1611 a_field_component
= g_string_append(
1612 a_field_component
, a_string_spaces
->str
);
1613 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1615 a_field_component
= g_string_append_c(a_field_component
,
1616 filter
->expression
[i
]);
1621 * Preliminary check to see
1622 * if tree was constructed correctly
1625 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1626 is not valid due to parenthesis incorrect use",filter
->expression
);
1630 if(tree_stack
->len
!= 1) /* only root tree should remain */
1634 * processing last element of expression
1636 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1637 while(t1
->right
!= LTTV_TREE_IDLE
) {
1638 g_assert(t1
->right
== LTTV_TREE_NODE
);
1641 if(not) { // add not operator to tree
1642 t3
= lttv_filter_tree_new();
1643 t3
->node
= LTTV_LOGICAL_NOT
;
1644 t1
->right
= LTTV_TREE_NODE
;
1649 if(subtree
!= NULL
) { /* add the subtree */
1650 t1
->right
= LTTV_TREE_NODE
;
1651 t1
->r_child
.t
= subtree
;
1653 } else { /* add a leaf */
1654 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1655 a_field_component
= NULL
;
1656 g_string_free(a_string_spaces
, TRUE
);
1657 a_string_spaces
= NULL
;
1658 t1
->right
= LTTV_TREE_LEAF
;
1659 t1
->r_child
.leaf
= a_simple_expression
;
1660 a_simple_expression
= NULL
;
1664 /* free the pointer array */
1665 g_assert(a_field_path
->len
== 0);
1666 g_ptr_array_free(a_field_path
,TRUE
);
1668 /* free the tree stack -- but keep the root tree */
1669 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1670 g_ptr_array_free(tree_stack
,TRUE
);
1672 /* free the field buffer if allocated */
1673 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1674 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1676 /* free the simple expression buffer if allocated */
1677 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1679 g_assert(filter
->head
!= NULL
); /* tree should exist */
1680 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1683 gettimeofday(&endtime
, NULL
);
1685 /* Calcul du temps de l'algorithme */
1686 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1687 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1688 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1689 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1693 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1694 lttv_print_tree(filter
->head
,0) ;
1695 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1703 * @fn void lttv_filter_destroy(LttvFilter*)
1705 * Destroy the current LttvFilter
1706 * @param filter pointer to the current LttvFilter
1709 lttv_filter_destroy(LttvFilter
* filter
) {
1713 if(filter
->expression
)
1714 g_free(filter
->expression
);
1716 lttv_filter_tree_destroy(filter
->head
);
1722 * @fn LttvFilterTree* lttv_filter_tree_new()
1724 * Assign a new tree for the current expression
1726 * @return pointer of LttvFilterTree
1729 lttv_filter_tree_new() {
1730 LttvFilterTree
* tree
;
1732 tree
= g_new(LttvFilterTree
,1);
1733 tree
->node
= 0; //g_new(lttv_expression,1);
1734 tree
->left
= LTTV_TREE_IDLE
;
1735 tree
->right
= LTTV_TREE_IDLE
;
1736 tree
->r_child
.t
= NULL
;
1737 tree
->l_child
.t
= NULL
;
1743 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1745 * Append a new expression to the expression
1746 * defined in the current filter
1747 * @param filter pointer to the current LttvFilter
1748 * @param expression string that must be appended
1749 * @return Success/Failure of operation
1752 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1754 if(expression
== NULL
) return FALSE
;
1755 if(filter
== NULL
) return FALSE
;
1756 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1758 GString
* s
= g_string_new("");
1759 if(filter
->expression
!= NULL
) {
1760 s
= g_string_append(s
,filter
->expression
);
1761 s
= g_string_append_c(s
,'&');
1763 s
= g_string_append(s
,expression
);
1765 g_free(filter
->expression
);
1766 filter
->expression
= g_string_free(s
,FALSE
);
1768 /* TRUE if construction of tree proceeded without errors */
1769 return lttv_filter_update(filter
);
1774 * @fn void lttv_filter_clear_expression(LttvFilter*)
1776 * Clear the filter expression from the
1777 * current filter and sets its pointer to NULL
1778 * @param filter pointer to the current LttvFilter
1781 lttv_filter_clear_expression(LttvFilter
* filter
) {
1783 if(filter
->expression
!= NULL
) {
1784 g_free(filter
->expression
);
1785 filter
->expression
= NULL
;
1791 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1793 * Destroys the tree and his sub-trees
1794 * @param tree Tree which must be destroyed
1797 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1799 if(tree
== NULL
) return;
1801 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1802 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1804 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1805 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1807 // g_free(tree->node);
1812 * Global parsing function for the current
1814 * @param t pointer to the current LttvFilterTree
1815 * @param event current LttEvent, NULL if not used
1816 * @param tracefile current LttTracefile, NULL if not used
1817 * @param trace current LttTrace, NULL if not used
1818 * @param state current LttvProcessState, NULL if not used
1819 * @param context current LttvTracefileContext, NULL if not used
1820 * @return response of filter
1823 lttv_filter_tree_parse(
1824 const LttvFilterTree
* t
,
1825 const LttEvent
* event
,
1826 const LttTracefile
* tracefile
,
1827 const LttTrace
* trace
,
1828 const LttvTracefileContext
* context
,
1829 const LttvProcessState
* state
,
1830 const LttvTraceContext
* tc
1835 * Each tree is parsed in inorder.
1836 * This way, it's possible to apply the left filter of the
1837 * tree, then decide whether or not the right branch should
1838 * be parsed depending on the linking logical operator
1840 * Each node consists in a
1841 * 1. logical operator
1842 * 2. left child ( node or simple expression )
1843 * 3. right child ( node or simple expression )
1845 * When the child is a simple expression, we must
1846 * before all determine if the expression refers to
1847 * a structure which is whithin observation ( not NULL ).
1848 * -If so, the expression is evaluated.
1849 * -If not, the result is set to TRUE since this particular
1850 * operation does not interfere with the lttv structure
1852 * The result of each simple expression will directly
1853 * affect the next branch. This way, depending on
1854 * the linking logical operator, the parser will decide
1855 * to explore or not the next branch.
1857 * -If result of left branch is 0 / FALSE
1858 * then don't explore right branch and return 0;
1859 * -If result of left branch is 1 / TRUE then explore
1861 * -If result of left branch is 1 / TRUE
1862 * then don't explore right branch and return 1;
1863 * -If result of left branch is 0 / FALSE then explore
1865 * -Result of left branch will not affect exploration of
1869 gboolean lresult
= FALSE
, rresult
= FALSE
;
1872 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1874 ts
= (LttvTraceState
*)tc
;
1876 ts
= (LttvTraceState
*)context
->t_context
;
1879 guint cpu
= tfs
->cpu
;
1881 state
= ts
->running_process
[cpu
];
1887 if(t
->left
== LTTV_TREE_NODE
) {
1888 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1890 else if(t
->left
== LTTV_TREE_LEAF
) {
1891 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1895 * Parse linking operator
1896 * make a cutoff if possible
1898 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1899 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1902 * Parse right branch
1904 if(t
->right
== LTTV_TREE_NODE
) {
1905 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1907 else if(t
->right
== LTTV_TREE_LEAF
) {
1908 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1913 * Apply and return the
1914 * logical link between the
1918 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1919 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1920 case LTTV_LOGICAL_NOT
:
1921 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1922 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1923 case 0: return (rresult
);
1926 * This case should never be
1927 * parsed, if so, this subtree
1936 * This function parses a particular branch of the tree
1937 * @param se pointer to the current LttvSimpleExpression
1938 * @param event current LttEvent, NULL if not used
1939 * @param tracefile current LttTracefile, NULL if not used
1940 * @param trace current LttTrace, NULL if not used
1941 * @param state current LttvProcessState, NULL if not used
1942 * @param context current LttvTracefileContext, NULL if not used
1943 * @return response of filter
1946 lttv_filter_tree_parse_branch(
1947 const LttvSimpleExpression
* se
,
1948 const LttEvent
* event
,
1949 const LttTracefile
* tracefile
,
1950 const LttTrace
* trace
,
1951 const LttvProcessState
* state
,
1952 const LttvTracefileContext
* context
) {
1957 case LTTV_FILTER_TRACE_NAME
:
1958 if(trace
== NULL
) return TRUE
;
1960 GQuark quark
= ltt_trace_name(trace
);
1961 return se
->op((gpointer
)&quark
,v
);
1964 case LTTV_FILTER_TRACEFILE_NAME
:
1965 if(tracefile
== NULL
) return TRUE
;
1967 GQuark quark
= ltt_tracefile_name(tracefile
);
1968 return se
->op((gpointer
)&quark
,v
);
1971 case LTTV_FILTER_STATE_PID
:
1972 if(state
== NULL
) return TRUE
;
1973 else return se
->op((gpointer
)&state
->pid
,v
);
1975 case LTTV_FILTER_STATE_PPID
:
1976 if(state
== NULL
) return TRUE
;
1977 else return se
->op((gpointer
)&state
->ppid
,v
);
1979 case LTTV_FILTER_STATE_CT
:
1980 if(state
== NULL
) return TRUE
;
1982 return se
->op((gpointer
)&state
->creation_time
,v
);
1985 case LTTV_FILTER_STATE_IT
:
1986 if(state
== NULL
) return TRUE
;
1988 return se
->op((gpointer
)&state
->insertion_time
,v
);
1991 case LTTV_FILTER_STATE_P_NAME
:
1992 if(state
== NULL
) return TRUE
;
1994 GQuark quark
= state
->name
;
1995 return se
->op((gpointer
)&quark
,v
);
1998 case LTTV_FILTER_STATE_T_BRAND
:
1999 if(state
== NULL
) return TRUE
;
2001 GQuark quark
= state
->brand
;
2002 return se
->op((gpointer
)&quark
,v
);
2005 case LTTV_FILTER_STATE_EX_MODE
:
2006 if(state
== NULL
) return TRUE
;
2007 else return se
->op((gpointer
)&state
->state
->t
,v
);
2009 case LTTV_FILTER_STATE_EX_SUBMODE
:
2010 if(state
== NULL
) return TRUE
;
2011 else return se
->op((gpointer
)&state
->state
->n
,v
);
2013 case LTTV_FILTER_STATE_P_STATUS
:
2014 if(state
== NULL
) return TRUE
;
2015 else return se
->op((gpointer
)&state
->state
->s
,v
);
2017 case LTTV_FILTER_STATE_CPU
:
2018 if(state
== NULL
) return TRUE
;
2020 return se
->op((gpointer
)&state
->cpu
,v
);
2023 case LTTV_FILTER_EVENT_NAME
:
2024 if(event
== NULL
) return TRUE
;
2026 struct marker_info
*info
;
2027 info
= marker_get_info_from_id((LttTrace
*)trace
, event
->event_id
);
2028 g_assert(info
!= NULL
);
2029 GQuark quark
= info
->name
;
2030 return se
->op((gpointer
)&quark
,v
);
2033 case LTTV_FILTER_EVENT_CATEGORY
:
2035 * TODO: Not yet implemented
2039 case LTTV_FILTER_EVENT_TIME
:
2040 if(event
== NULL
) return TRUE
;
2042 LttTime time
= ltt_event_time(event
);
2043 return se
->op((gpointer
)&time
,v
);
2046 case LTTV_FILTER_EVENT_TSC
:
2047 if(event
== NULL
) return TRUE
;
2049 LttCycleCount count
= ltt_event_cycle_count(event
);
2050 return se
->op((gpointer
)&count
,v
);
2053 case LTTV_FILTER_EVENT_TARGET_PID
:
2054 if(context
== NULL
) return TRUE
;
2057 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2058 return se
->op((gpointer
)&target_pid
,v
);
2061 case LTTV_FILTER_EVENT_FIELD
:
2063 * TODO: Use the offset to
2064 * find the dynamic field
2065 * in the event struct
2070 * This case should never be
2071 * parsed, if so, the whole
2072 * filtering is cancelled
2074 g_warning("Error while parsing the filter tree");
2078 /* should never get here */
2086 * Debug function. Prints tree memory allocation.
2087 * @param t the pointer to the current LttvFilterTree
2090 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2092 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2093 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2094 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2096 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2097 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"));
2098 if(t
->left
== LTTV_TREE_LEAF
) {
2099 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2100 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2101 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2103 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"));
2104 if(t
->right
== LTTV_TREE_LEAF
) {
2105 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2106 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2107 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2110 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2111 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2115 * @fn static void module_init()
2117 * Initializes the filter module and specific values
2119 static void module_init()
2125 * Destroys the filter module and specific values
2127 static void module_destroy()
2133 LTTV_MODULE("filter", "Filters traceset and events", \
2134 "Filters traceset and events specifically to user input", \
2135 module_init
, module_destroy
)