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 * | |->category (String, not yet implemented)
36 * | |->tsc (LttCycleCount --> uint64)
40 * | |->"sub-field name"
42 * | |->"leaf-field name" (field type)
44 * | |->name (String, converted to GQuark)
46 * | |->name (String, converted to GQuark)
50 * |->creation_time (LttTime)
51 * |->insertion_time (LttTime)
52 * |->process_name (String, converted to GQuark)
53 * |->execution_mode (LttvExecutionMode)
54 * |->execution_submode (LttvExecutionSubmode)
55 * |->process_status (LttvProcessStatus)
62 * - refine switch of expression in multiple uses functions
63 * - remove the idle expressions in the tree
64 * - replace string for enum value for corresponding value
73 #include <lttv/filter.h>
76 * @fn LttvSimpleExpression* lttv_simple_expression_new()
78 * Constructor for LttvSimpleExpression
79 * @return pointer to new LttvSimpleExpression
82 lttv_simple_expression_new() {
84 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
86 se
->field
= LTTV_FILTER_UNDEFINED
;
94 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
96 * Parse through filtering field hierarchy as specified
97 * by user. This function compares each value to
98 * predetermined quarks
99 * @param fp The field path list
100 * @param se current simple expression
101 * @return success/failure of operation
104 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
108 if(fp
->len
< 2) return FALSE
;
109 g_assert(f
=g_ptr_array_remove_index(fp
,0));
112 * Parse through the specified
115 * Take note however that the
116 * 'event' subfields might change
117 * depending on values specified
118 * in core.xml file. Hence, if
119 * none of the subfields in the
120 * array match the hardcoded
121 * subfields, it will be considered
124 if(!g_strcasecmp(f
->str
,"trace") ) {
129 g_string_free(f
,TRUE
);
130 f
=g_ptr_array_remove_index(fp
,0);
131 if(!g_strcasecmp(f
->str
,"name")) {
132 se
->field
= LTTV_FILTER_TRACE_NAME
;
134 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
136 * FIXME: not yet implemented !
138 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
143 g_string_free(f
,TRUE
);
144 f
=g_ptr_array_remove_index(fp
,0);
145 if(!g_strcasecmp(f
->str
,"name")) {
146 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
148 } else if(!g_strcasecmp(f
->str
,"state") ) {
153 * state.creation_time
154 * state.insertion_time
156 * state.execution_mode
157 * state.execution_submode
158 * state.process_status
161 g_string_free(f
,TRUE
);
162 f
=g_ptr_array_remove_index(fp
,0);
163 if(!g_strcasecmp(f
->str
,"pid") ) {
164 se
->field
= LTTV_FILTER_STATE_PID
;
166 else if(!g_strcasecmp(f
->str
,"ppid") ) {
167 se
->field
= LTTV_FILTER_STATE_PPID
;
169 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
170 se
->field
= LTTV_FILTER_STATE_CT
;
172 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
173 se
->field
= LTTV_FILTER_STATE_IT
;
175 else if(!g_strcasecmp(f
->str
,"process_name") ) {
176 se
->field
= LTTV_FILTER_STATE_P_NAME
;
178 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
179 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
181 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
182 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
184 else if(!g_strcasecmp(f
->str
,"process_status") ) {
185 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
187 else if(!g_strcasecmp(f
->str
,"cpu") ) {
188 se
->field
= LTTV_FILTER_STATE_CPU
;
190 } else if(!g_strcasecmp(f
->str
,"event") ) {
198 g_string_free(f
,TRUE
);
199 f
=g_ptr_array_remove_index(fp
,0);
200 if(!g_strcasecmp(f
->str
,"name") ) {
201 se
->field
= LTTV_FILTER_EVENT_NAME
;
203 else if(!g_strcasecmp(f
->str
,"category") ) {
205 * FIXME: Category not yet functional in lttv
207 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
209 else if(!g_strcasecmp(f
->str
,"time") ) {
210 se
->field
= LTTV_FILTER_EVENT_TIME
;
212 else if(!g_strcasecmp(f
->str
,"tsc") ) {
213 se
->field
= LTTV_FILTER_EVENT_TSC
;
215 else { /* core.xml specified options */
216 se
->field
= LTTV_FILTER_EVENT_FIELD
;
219 g_warning("Unrecognized field in filter string");
222 /* free memory for last string */
223 g_string_free(f
,TRUE
);
225 /* array should be empty */
226 g_assert(fp
->len
== 0);
228 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
229 g_warning("The specified field was not recognized !");
236 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
238 * Sets the function pointer for the current
240 * @param se current simple expression
241 * @param op current operator
242 * @return success/failure of operation
245 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
251 case LTTV_FILTER_TRACE_NAME
:
252 case LTTV_FILTER_TRACEFILE_NAME
:
253 case LTTV_FILTER_STATE_P_NAME
:
254 case LTTV_FILTER_EVENT_NAME
:
255 case LTTV_FILTER_STATE_CPU
:
256 case LTTV_FILTER_STATE_EX_MODE
:
257 case LTTV_FILTER_STATE_EX_SUBMODE
:
258 case LTTV_FILTER_STATE_P_STATUS
:
261 se
->op
= lttv_apply_op_eq_quark
;
264 se
->op
= lttv_apply_op_ne_quark
;
267 g_warning("Error encountered in operator assignment = or != expected");
274 case LTTV_FILTER_STATE_PID
:
275 case LTTV_FILTER_STATE_PPID
:
276 case LTTV_FILTER_EVENT_TSC
:
279 se
->op
= lttv_apply_op_eq_uint64
;
282 se
->op
= lttv_apply_op_ne_uint64
;
285 se
->op
= lttv_apply_op_lt_uint64
;
288 se
->op
= lttv_apply_op_le_uint64
;
291 se
->op
= lttv_apply_op_gt_uint64
;
294 se
->op
= lttv_apply_op_ge_uint64
;
297 g_warning("Error encountered in operator assignment");
303 * Entered as string, converted to enum
305 * can only be compared with 'equal' or 'not equal' operators
307 * unsigned int of 16 bits are used here since enums
308 * should not go over 2^16-1 values
312 // case LTTV_FIELD_EQ:
313 // se->op = lttv_apply_op_eq_uint16;
315 // case LTTV_FIELD_NE:
316 // se->op = lttv_apply_op_ne_uint16;
319 // g_warning("Error encountered in operator assignment = or != expected");
326 case LTTV_FILTER_STATE_CT
:
327 case LTTV_FILTER_STATE_IT
:
328 case LTTV_FILTER_EVENT_TIME
:
331 se
->op
= lttv_apply_op_eq_ltttime
;
334 se
->op
= lttv_apply_op_ne_ltttime
;
337 se
->op
= lttv_apply_op_lt_ltttime
;
340 se
->op
= lttv_apply_op_le_ltttime
;
343 se
->op
= lttv_apply_op_gt_ltttime
;
346 se
->op
= lttv_apply_op_ge_ltttime
;
349 g_warning("Error encountered in operator assignment");
354 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
363 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
365 * Assign the value field to the current LttvSimpleExpression
366 * @param se pointer to the current LttvSimpleExpression
367 * @param value string value for simple expression
370 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
373 gboolean is_double
= FALSE
;
374 LttTime t
= ltt_time_zero
;
381 * entered as strings, converted to Quarks
383 case LTTV_FILTER_TRACE_NAME
:
384 case LTTV_FILTER_TRACEFILE_NAME
:
385 case LTTV_FILTER_STATE_P_NAME
:
386 case LTTV_FILTER_EVENT_NAME
:
387 case LTTV_FILTER_STATE_CPU
:
388 case LTTV_FILTER_STATE_EX_MODE
:
389 case LTTV_FILTER_STATE_EX_SUBMODE
:
390 case LTTV_FILTER_STATE_P_STATUS
:
391 // se->value.v_string = value;
392 se
->value
.v_uint32
= g_quark_from_string(value
);
396 * integer -- supposed to be uint64
398 case LTTV_FILTER_STATE_PID
:
399 case LTTV_FILTER_STATE_PPID
:
400 case LTTV_FILTER_EVENT_TSC
:
401 se
->value
.v_uint64
= atoi(value
);
407 case LTTV_FILTER_STATE_CT
:
408 case LTTV_FILTER_STATE_IT
:
409 case LTTV_FILTER_EVENT_TIME
:
410 //se->value.v_double = atof(value);
412 * parsing logic could be optimised,
413 * but as for now, simpler this way
415 v
= g_string_new("");
416 for(i
=0;i
<strlen(value
);i
++) {
417 if(value
[i
] == '.') {
418 /* cannot specify number with more than one '.' */
419 if(is_double
) return FALSE
;
420 else is_double
= TRUE
;
422 g_string_free(v
,TRUE
);
423 v
= g_string_new("");
424 } else g_string_append_c(v
,value
[i
]);
426 /* number can be integer or double */
427 if(is_double
) t
.tv_nsec
= atoi(v
);
428 else t
.tv_sec
= atoi(v
);
430 g_string_free(v
,TRUE
);
432 se
->value
.v_ltttime
= t
;
436 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
446 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
448 * Disallocate memory for the current
450 * @param se pointer to the current LttvSimpleExpression
453 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
455 // g_free(se->value);
456 // switch(se->field) {
457 // case LTTV_FILTER_TRACE_NAME:
458 // case LTTV_FILTER_TRACEFILE_NAME:
459 // case LTTV_FILTER_STATE_P_NAME:
460 // case LTTV_FILTER_EVENT_NAME:
461 // g_free(se->value.v_string);
469 * @fn gint lttv_struct_type(gint)
471 * Finds the structure type depending
472 * on the fields in parameters
473 * @params ft Field of the current structure
474 * @return LttvStructType enum or -1 for error
477 lttv_struct_type(gint ft
) {
480 case LTTV_FILTER_TRACE_NAME
:
481 return LTTV_FILTER_TRACE
;
483 case LTTV_FILTER_TRACEFILE_NAME
:
484 return LTTV_FILTER_TRACEFILE
;
486 case LTTV_FILTER_STATE_PID
:
487 case LTTV_FILTER_STATE_PPID
:
488 case LTTV_FILTER_STATE_CT
:
489 case LTTV_FILTER_STATE_IT
:
490 case LTTV_FILTER_STATE_P_NAME
:
491 case LTTV_FILTER_STATE_EX_MODE
:
492 case LTTV_FILTER_STATE_EX_SUBMODE
:
493 case LTTV_FILTER_STATE_P_STATUS
:
494 case LTTV_FILTER_STATE_CPU
:
495 return LTTV_FILTER_STATE
;
497 case LTTV_FILTER_EVENT_NAME
:
498 case LTTV_FILTER_EVENT_CATEGORY
:
499 case LTTV_FILTER_EVENT_TIME
:
500 case LTTV_FILTER_EVENT_TSC
:
501 case LTTV_FILTER_EVENT_FIELD
:
502 return LTTV_FILTER_EVENT
;
510 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
512 * Applies the 'equal' operator to the
513 * specified structure and value
514 * @param v1 left member of comparison
515 * @param v2 right member of comparison
516 * @return success/failure of operation
518 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
520 guint64
* r
= (guint64
*) v1
;
521 return (*r
== v2
.v_uint64
);
526 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
528 * Applies the 'equal' operator to the
529 * specified structure and value
530 * @param v1 left member of comparison
531 * @param v2 right member of comparison
532 * @return success/failure of operation
534 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
535 guint32
* r
= (guint32
*) v1
;
536 return (*r
== v2
.v_uint32
);
540 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
542 * Applies the 'equal' operator to the
543 * specified structure and value
544 * @param v1 left member of comparison
545 * @param v2 right member of comparison
546 * @return success/failure of operation
548 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
549 guint16
* r
= (guint16
*) v1
;
550 return (*r
== v2
.v_uint16
);
554 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
556 * Applies the 'equal' operator to the
557 * specified structure and value
558 * @param v1 left member of comparison
559 * @param v2 right member of comparison
560 * @return success/failure of operation
562 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
563 double* r
= (double*) v1
;
564 return (*r
== v2
.v_double
);
568 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
570 * Applies the 'equal' operator to the
571 * specified structure and value
572 * @param v1 left member of comparison
573 * @param v2 right member of comparison
574 * @return success/failure of operation
576 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
577 char* r
= (char*) v1
;
578 return (!g_strcasecmp(r
,v2
.v_string
));
582 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
584 * Applies the 'equal' operator to the
585 * specified structure and value
586 * @param v1 left member of comparison
587 * @param v2 right member of comparison
588 * @return success/failure of operation
590 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
591 GQuark
* r
= (GQuark
*) v1
;
592 g_print("v1:%i v2:%i\n",*r
,v2
.v_uint32
);
593 return (*r
== v2
.v_uint32
);
597 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
599 * Applies the 'equal' operator to the
600 * specified structure and value
601 * @param v1 left member of comparison
602 * @param v2 right member of comparison
603 * @return success/failure of operation
605 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
606 LttTime
* r
= (LttTime
*) v1
;
607 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
612 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
614 * Applies the 'not equal' operator to the
615 * specified structure and value
616 * @param v1 left member of comparison
617 * @param v2 right member of comparison
618 * @return success/failure of operation
620 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
621 guint64
* r
= (guint64
*) v1
;
622 return (*r
!= v2
.v_uint64
);
626 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
628 * Applies the 'not equal' operator to the
629 * specified structure and value
630 * @param v1 left member of comparison
631 * @param v2 right member of comparison
632 * @return success/failure of operation
634 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
635 guint32
* r
= (guint32
*) v1
;
636 return (*r
!= v2
.v_uint32
);
640 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
642 * Applies the 'not equal' operator to the
643 * specified structure and value
644 * @param v1 left member of comparison
645 * @param v2 right member of comparison
646 * @return success/failure of operation
648 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
649 guint16
* r
= (guint16
*) v1
;
650 return (*r
!= v2
.v_uint16
);
654 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
656 * Applies the 'not equal' operator to the
657 * specified structure and value
658 * @param v1 left member of comparison
659 * @param v2 right member of comparison
660 * @return success/failure of operation
662 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
663 double* r
= (double*) v1
;
664 return (*r
!= v2
.v_double
);
668 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
670 * Applies the 'not equal' operator to the
671 * specified structure and value
672 * @param v1 left member of comparison
673 * @param v2 right member of comparison
674 * @return success/failure of operation
676 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
677 char* r
= (char*) v1
;
678 return (g_strcasecmp(r
,v2
.v_string
));
682 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
684 * Applies the 'not equal' operator to the
685 * specified structure and value
686 * @param v1 left member of comparison
687 * @param v2 right member of comparison
688 * @return success/failure of operation
690 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
691 GQuark
* r
= (GQuark
*) v1
;
692 return (*r
!= v2
.v_uint32
);
697 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
699 * Applies the 'not equal' operator to the
700 * specified structure and value
701 * @param v1 left member of comparison
702 * @param v2 right member of comparison
703 * @return success/failure of operation
705 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
706 LttTime
* r
= (LttTime
*) v1
;
707 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
712 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
714 * Applies the 'lower than' operator to the
715 * specified structure and value
716 * @param v1 left member of comparison
717 * @param v2 right member of comparison
718 * @return success/failure of operation
720 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
721 guint64
* r
= (guint64
*) v1
;
722 return (*r
< v2
.v_uint64
);
726 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
728 * Applies the 'lower than' operator to the
729 * specified structure and value
730 * @param v1 left member of comparison
731 * @param v2 right member of comparison
732 * @return success/failure of operation
734 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
735 guint32
* r
= (guint32
*) v1
;
736 return (*r
< v2
.v_uint32
);
740 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
742 * Applies the 'lower than' operator to the
743 * specified structure and value
744 * @param v1 left member of comparison
745 * @param v2 right member of comparison
746 * @return success/failure of operation
748 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
749 guint16
* r
= (guint16
*) v1
;
750 return (*r
< v2
.v_uint16
);
754 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
756 * Applies the 'lower than' operator to the
757 * specified structure and value
758 * @param v1 left member of comparison
759 * @param v2 right member of comparison
760 * @return success/failure of operation
762 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
763 double* r
= (double*) v1
;
764 return (*r
< v2
.v_double
);
768 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
770 * Applies the 'lower than' operator to the
771 * specified structure and value
772 * @param v1 left member of comparison
773 * @param v2 right member of comparison
774 * @return success/failure of operation
776 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
777 LttTime
* r
= (LttTime
*) v1
;
778 // 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)));
779 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
784 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
786 * Applies the 'lower or 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_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
793 guint64
* r
= (guint64
*) v1
;
794 return (*r
<= v2
.v_uint64
);
798 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
800 * Applies the 'lower or equal' operator to the
801 * specified structure and value
802 * @param v1 left member of comparison
803 * @param v2 right member of comparison
804 * @return success/failure of operation
806 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
807 guint32
* r
= (guint32
*) v1
;
808 return (*r
<= v2
.v_uint32
);
812 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
814 * Applies the 'lower or equal' operator to the
815 * specified structure and value
816 * @param v1 left member of comparison
817 * @param v2 right member of comparison
818 * @return success/failure of operation
820 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
821 guint16
* r
= (guint16
*) v1
;
822 return (*r
<= v2
.v_uint16
);
826 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
828 * Applies the 'lower or equal' operator to the
829 * specified structure and value
830 * @param v1 left member of comparison
831 * @param v2 right member of comparison
832 * @return success/failure of operation
834 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
835 double* r
= (double*) v1
;
836 return (*r
<= v2
.v_double
);
840 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
842 * Applies the 'lower or equal' operator to the
843 * specified structure and value
844 * @param v1 left member of comparison
845 * @param v2 right member of comparison
846 * @return success/failure of operation
848 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
849 LttTime
* r
= (LttTime
*) v1
;
850 // 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)));
851 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
856 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
858 * Applies the 'greater 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_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
865 guint64
* r
= (guint64
*) v1
;
866 return (*r
> v2
.v_uint64
);
870 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
872 * Applies the 'greater 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_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
879 guint32
* r
= (guint32
*) v1
;
880 return (*r
> v2
.v_uint32
);
884 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
886 * Applies the 'greater 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_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
893 guint16
* r
= (guint16
*) v1
;
894 return (*r
> v2
.v_uint16
);
898 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
900 * Applies the 'greater than' 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_gt_double(const gpointer v1
, LttvFieldValue v2
) {
907 double* r
= (double*) v1
;
908 return (*r
> v2
.v_double
);
912 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
914 * Applies the 'greater than' 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_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
921 LttTime
* r
= (LttTime
*) v1
;
922 // 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)));
923 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
928 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
930 * Applies the 'greater or equal' operator to the
931 * specified structure and value
932 * @param v1 left member of comparison
933 * @param v2 right member of comparison
934 * @return success/failure of operation
936 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
937 guint64
* r
= (guint64
*) v1
;
938 return (*r
>= v2
.v_uint64
);
942 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
944 * Applies the 'greater or equal' operator to the
945 * specified structure and value
946 * @param v1 left member of comparison
947 * @param v2 right member of comparison
948 * @return success/failure of operation
950 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
951 guint32
* r
= (guint32
*) v1
;
952 return (*r
>= v2
.v_uint32
);
956 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
958 * Applies the 'greater or equal' operator to the
959 * specified structure and value
960 * @param v1 left member of comparison
961 * @param v2 right member of comparison
962 * @return success/failure of operation
964 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
965 guint16
* r
= (guint16
*) v1
;
966 return (*r
>= v2
.v_uint16
);
970 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
972 * Applies the 'greater or equal' operator to the
973 * specified structure and value
974 * @param v1 left member of comparison
975 * @param v2 right member of comparison
976 * @return success/failure of operation
978 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
979 double* r
= (double*) v1
;
980 return (*r
>= v2
.v_double
);
984 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
986 * Applies the 'greater or equal' 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_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
993 LttTime
* r
= (LttTime
*) v1
;
994 // 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)));
995 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1001 * Makes a copy of the current filter tree
1002 * @param tree pointer to the current tree
1003 * @return new copy of the filter tree
1006 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1008 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1010 newtree
->node
= tree
->node
;
1012 newtree
->left
= tree
->left
;
1013 if(newtree
->left
== LTTV_TREE_NODE
) {
1014 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1015 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1016 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1017 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1018 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1019 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1020 /* FIXME: special case for string copy ! */
1021 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1024 newtree
->right
= tree
->right
;
1025 if(newtree
->right
== LTTV_TREE_NODE
) {
1026 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1027 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1028 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1029 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1030 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1031 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1032 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1040 * Makes a copy of the current filter
1041 * @param filter pointer to the current filter
1042 * @return new copy of the filter
1045 lttv_filter_clone(const LttvFilter
* filter
) {
1047 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1049 strcpy(newfilter
->expression
,filter
->expression
);
1051 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1059 * @fn LttvFilter* lttv_filter_new()
1061 * Creates a new LttvFilter
1062 * @return the current LttvFilter or NULL if error
1067 LttvFilter
* filter
= g_new(LttvFilter
,1);
1068 filter
->expression
= NULL
;
1069 filter
->head
= NULL
;
1076 * @fn gboolean lttv_filter_update(LttvFilter*)
1078 * Updates the current LttvFilter by building
1079 * its tree based upon the expression string
1080 * @param filter pointer to the current LttvFilter
1081 * @return Failure/Success of operation
1084 lttv_filter_update(LttvFilter
* filter
) {
1086 // g_print("filter::lttv_filter_new()\n"); /* debug */
1088 if(filter
->expression
== NULL
) return FALSE
;
1092 p_nesting
=0; /* parenthesis nesting value */
1096 *tree
= lttv_filter_tree_new(), /* main tree */
1097 *subtree
= NULL
, /* buffer for subtrees */
1098 *t1
, /* buffer #1 */
1099 *t2
; /* buffer #2 */
1103 * If the tree already exists,
1104 * destroy it and build a new one
1106 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1107 filter
->head
= NULL
; /* will be assigned at the end */
1111 * each element of the list
1112 * is a sub tree created
1113 * by the use of parenthesis in the
1114 * global expression. The final tree
1115 * will be the one left at the root of
1118 GPtrArray
*tree_stack
= g_ptr_array_new();
1119 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1121 /* temporary values */
1122 GString
*a_field_component
= g_string_new("");
1123 GPtrArray
*a_field_path
= g_ptr_array_new();
1125 /* simple expression buffer */
1126 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1129 * Parse entire expression and construct
1130 * the binary tree. There are two steps
1131 * in browsing that string
1132 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1133 * 2. finding simple expressions
1134 * - field path ( separated by dots )
1135 * - op ( >, <, =, >=, <=, !=)
1136 * - value ( integer, string ... )
1137 * To spare computing time, the whole
1138 * string is parsed in this loop for a
1139 * O(n) complexity order.
1141 * When encountering logical op &,|,^
1142 * 1. parse the last value if any
1143 * 2. create a new tree
1144 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1145 * 4. concatenate this tree with the current tree on top of the stack
1146 * When encountering math ops >,>=,<,<=,=,!=
1147 * 1. add to op to the simple expression
1148 * 2. concatenate last field component to field path
1149 * When encountering concatening ops .
1150 * 1. concatenate last field component to field path
1151 * When encountering opening parenthesis (,{,[
1152 * 1. create a new subtree on top of tree stack
1153 * When encountering closing parenthesis ),},]
1154 * 1. add the expression on right child of the current tree
1155 * 2. the subtree is completed, allocate a new subtree
1156 * 3. pop the tree value from the tree stack
1160 struct timeval starttime
;
1161 struct timeval endtime
;
1162 gettimeofday(&starttime
, NULL
);
1165 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1167 //g_print("%c ",filter->expression[i]);
1169 switch(filter
->expression
[i
]) {
1175 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1176 while(t1
->right
!= LTTV_TREE_IDLE
) {
1177 g_assert(t1
->right
== LTTV_TREE_NODE
);
1180 t2
= lttv_filter_tree_new();
1181 t2
->node
= LTTV_LOGICAL_AND
;
1182 if(subtree
!= NULL
) { /* append subtree to current tree */
1183 t2
->left
= LTTV_TREE_NODE
;
1184 t2
->l_child
.t
= subtree
;
1186 t1
->right
= LTTV_TREE_NODE
;
1188 } else { /* append a simple expression */
1189 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1190 a_field_component
= g_string_new("");
1191 t2
->left
= LTTV_TREE_LEAF
;
1192 t2
->l_child
.leaf
= a_simple_expression
;
1193 a_simple_expression
= lttv_simple_expression_new();
1194 t1
->right
= LTTV_TREE_NODE
;
1201 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1202 while(t1
->right
!= LTTV_TREE_IDLE
) {
1203 g_assert(t1
->right
== LTTV_TREE_NODE
);
1206 t2
= lttv_filter_tree_new();
1207 t2
->node
= LTTV_LOGICAL_OR
;
1208 if(subtree
!= NULL
) { /* append subtree to current tree */
1209 t2
->left
= LTTV_TREE_NODE
;
1210 t2
->l_child
.t
= subtree
;
1212 t1
->right
= LTTV_TREE_NODE
;
1214 } else { /* append a simple expression */
1215 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1216 a_field_component
= g_string_new("");
1217 t2
->left
= LTTV_TREE_LEAF
;
1218 t2
->l_child
.leaf
= a_simple_expression
;
1219 a_simple_expression
= lttv_simple_expression_new();
1220 t1
->right
= LTTV_TREE_NODE
;
1227 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1228 while(t1
->right
!= LTTV_TREE_IDLE
) {
1229 g_assert(t1
->right
== LTTV_TREE_NODE
);
1232 t2
= lttv_filter_tree_new();
1233 t2
->node
= LTTV_LOGICAL_XOR
;
1234 if(subtree
!= NULL
) { /* append subtree to current tree */
1235 t2
->left
= LTTV_TREE_NODE
;
1236 t2
->l_child
.t
= subtree
;
1238 t1
->right
= LTTV_TREE_NODE
;
1240 } else { /* append a simple expression */
1241 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1242 a_field_component
= g_string_new("");
1243 t2
->left
= LTTV_TREE_LEAF
;
1244 t2
->l_child
.leaf
= a_simple_expression
;
1245 a_simple_expression
= lttv_simple_expression_new();
1246 t1
->right
= LTTV_TREE_NODE
;
1251 case '!': /* not, or not equal (math op) */
1253 if(filter
->expression
[i
+1] == '=') { /* != */
1254 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1255 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1256 a_field_component
= g_string_new("");
1257 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1260 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1261 while(t1
->right
!= LTTV_TREE_IDLE
) {
1262 g_assert(t1
->right
== LTTV_TREE_NODE
);
1265 t2
= lttv_filter_tree_new();
1266 t2
->node
= LTTV_LOGICAL_NOT
;
1267 t1
->right
= LTTV_TREE_NODE
;
1272 case '(': /* start of parenthesis */
1276 p_nesting
++; /* incrementing parenthesis nesting value */
1277 t1
= lttv_filter_tree_new();
1278 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1281 case ')': /* end of parenthesis */
1285 p_nesting
--; /* decrementing parenthesis nesting value */
1286 if(p_nesting
<0 || tree_stack
->len
<2) {
1287 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1288 is not valid due to parenthesis incorrect use",filter
->expression
);
1292 /* there must at least be the root tree left in the array */
1293 g_assert(tree_stack
->len
>0);
1295 if(subtree
!= NULL
) { /* append subtree to current tree */
1296 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1297 while(t1
->right
!= LTTV_TREE_IDLE
) {
1298 g_assert(t1
->right
== LTTV_TREE_NODE
);
1301 t1
->right
= LTTV_TREE_NODE
;
1302 t1
->r_child
.t
= subtree
;
1303 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1304 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1305 } else { /* assign subtree as current tree */
1306 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1307 a_field_component
= g_string_new("");
1308 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1309 while(t1
->right
!= LTTV_TREE_IDLE
) {
1310 g_assert(t1
->right
== LTTV_TREE_NODE
);
1311 g_assert(t1
->r_child
.t
!= NULL
);
1314 t1
->right
= LTTV_TREE_LEAF
;
1315 t1
->r_child
.leaf
= a_simple_expression
;
1316 a_simple_expression
= lttv_simple_expression_new();
1317 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1322 * mathematic operators
1324 case '<': /* lower, lower or equal */
1326 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1327 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1328 a_field_component
= g_string_new("");
1329 if(filter
->expression
[i
+1] == '=') { /* <= */
1331 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1332 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1335 case '>': /* higher, higher or equal */
1337 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1338 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1339 a_field_component
= g_string_new("");
1340 if(filter
->expression
[i
+1] == '=') { /* >= */
1342 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1343 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1346 case '=': /* equal */
1348 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1349 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1350 a_field_component
= g_string_new("");
1351 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1355 * Field concatening caracter
1360 * divide field expression into elements
1361 * in a_field_path array.
1363 * A dot can also be present in double values
1365 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1366 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1367 a_field_component
= g_string_new("");
1370 case ' ': /* ignore */
1371 case '\n': /* ignore */
1373 default: /* concatening current string */
1374 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1379 * Preliminary check to see
1380 * if tree was constructed correctly
1383 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1384 is not valid due to parenthesis incorrect use",filter
->expression
);
1388 if(tree_stack
->len
!= 1) /* only root tree should remain */
1391 /* processing last element of expression */
1392 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1393 while(t1
->right
!= LTTV_TREE_IDLE
) {
1394 g_assert(t1
->right
== LTTV_TREE_NODE
);
1397 if(subtree
!= NULL
) { /* add the subtree */
1398 t1
->right
= LTTV_TREE_NODE
;
1399 t1
->r_child
.t
= subtree
;
1401 } else { /* add a leaf */
1402 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1403 a_field_component
= NULL
;
1404 t1
->right
= LTTV_TREE_LEAF
;
1405 t1
->r_child
.leaf
= a_simple_expression
;
1406 a_simple_expression
= NULL
;
1410 /* free the pointer array */
1411 g_assert(a_field_path
->len
== 0);
1412 g_ptr_array_free(a_field_path
,TRUE
);
1414 /* free the tree stack -- but keep the root tree */
1415 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1416 g_ptr_array_free(tree_stack
,TRUE
);
1418 /* free the field buffer if allocated */
1419 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1421 /* free the simple expression buffer if allocated */
1422 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1424 g_assert(filter
->head
!= NULL
); /* tree should exist */
1425 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1428 gettimeofday(&endtime
, NULL
);
1430 /* Calcul du temps de l'algorithme */
1431 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1432 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1433 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1434 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1438 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1439 lttv_print_tree(filter
->head
) ;
1440 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1448 * @fn void lttv_filter_destroy(LttvFilter*)
1450 * Destroy the current LttvFilter
1451 * @param filter pointer to the current LttvFilter
1454 lttv_filter_destroy(LttvFilter
* filter
) {
1456 g_free(filter
->expression
);
1457 lttv_filter_tree_destroy(filter
->head
);
1463 * @fn LttvFilterTree* lttv_filter_tree_new()
1465 * Assign a new tree for the current expression
1467 * @return pointer of LttvFilterTree
1470 lttv_filter_tree_new() {
1471 LttvFilterTree
* tree
;
1473 tree
= g_new(LttvFilterTree
,1);
1474 tree
->node
= 0; //g_new(lttv_expression,1);
1475 tree
->left
= LTTV_TREE_IDLE
;
1476 tree
->right
= LTTV_TREE_IDLE
;
1477 tree
->r_child
.t
= NULL
;
1478 tree
->l_child
.t
= NULL
;
1484 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1486 * Append a new expression to the expression
1487 * defined in the current filter
1488 * @param filter pointer to the current LttvFilter
1489 * @param expression string that must be appended
1490 * @return Success/Failure of operation
1493 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1495 if(expression
== NULL
) return FALSE
;
1496 if(filter
== NULL
) return FALSE
;
1498 GString
* s
= g_string_new("");
1499 if(filter
->expression
!= NULL
) {
1500 g_string_append(s
,filter
->expression
);
1501 g_string_append_c(s
,'&');
1503 g_string_append(s
,expression
);
1505 g_free(filter
->expression
);
1506 filter
->expression
= g_string_free(s
,FALSE
);
1508 /* TRUE if construction of tree proceeded without errors */
1509 return lttv_filter_update(filter
);
1514 * @fn void lttv_filter_clear_expression(LttvFilter*)
1516 * Clear the filter expression from the
1517 * current filter and sets its pointer to NULL
1518 * @param filter pointer to the current LttvFilter
1521 lttv_filter_clear_expression(LttvFilter
* filter
) {
1523 if(filter
->expression
!= NULL
) {
1524 g_free(filter
->expression
);
1525 filter
->expression
= NULL
;
1531 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1533 * Destroys the tree and his sub-trees
1534 * @param tree Tree which must be destroyed
1537 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1539 if(tree
== NULL
) return;
1541 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1542 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1544 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1545 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1547 // g_free(tree->node);
1552 * Global parsing function for the current
1554 * @param t pointer to the current LttvFilterTree
1555 * @param event current LttEvent, NULL if not used
1556 * @param tracefile current LttTracefile, NULL if not used
1557 * @param trace current LttTrace, NULL if not used
1558 * @param state current LttvProcessState, NULL if not used
1559 * @param context current LttvTracefileContext, NULL if not used
1560 * @return response of filter
1563 lttv_filter_tree_parse(
1564 const LttvFilterTree
* t
,
1565 const LttEvent
* event
,
1566 const LttTracefile
* tracefile
,
1567 const LttTrace
* trace
,
1568 const LttvProcessState
* state
,
1569 const LttvTracefileContext
* context
1574 * Each tree is parsed in inorder.
1575 * This way, it's possible to apply the left filter of the
1576 * tree, then decide whether or not the right branch should
1577 * be parsed depending on the linking logical operator
1579 * Each node consists in a
1580 * 1. logical operator
1581 * 2. left child ( node or simple expression )
1582 * 3. right child ( node or simple expression )
1584 * When the child is a simple expression, we must
1585 * before all determine if the expression refers to
1586 * a structure which is whithin observation ( not NULL ).
1587 * -If so, the expression is evaluated.
1588 * -If not, the result is set to TRUE since this particular
1589 * operation does not interfere with the lttv structure
1591 * The result of each simple expression will directly
1592 * affect the next branch. This way, depending on
1593 * the linking logical operator, the parser will decide
1594 * to explore or not the next branch.
1596 * -If result of left branch is 0 / FALSE
1597 * then don't explore right branch and return 0;
1598 * -If result of left branch is 1 / TRUE then explore
1600 * -If result of left branch is 1 / TRUE
1601 * then don't explore right branch and return 1;
1602 * -If result of left branch is 0 / FALSE then explore
1604 * -Result of left branch will not affect exploration of
1608 gboolean lresult
= FALSE
, rresult
= FALSE
;
1613 if(t
->left
== LTTV_TREE_NODE
) {
1614 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,state
,context
);
1616 else if(t
->left
== LTTV_TREE_LEAF
) {
1617 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1621 * Parse linking operator
1622 * make a cutoff if possible
1624 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1625 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1628 * Parse right branch
1630 if(t
->right
== LTTV_TREE_NODE
) {
1631 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,state
,context
);
1633 else if(t
->right
== LTTV_TREE_LEAF
) {
1634 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1638 * Apply and return the
1639 * logical link between the
1643 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1644 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1645 case LTTV_LOGICAL_NOT
: return (!rresult
);
1646 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1647 case 0: return (rresult
);
1650 * This case should never be
1651 * parsed, if so, this subtree
1660 * This function parses a particular branch of the tree
1661 * @param se pointer to the current LttvSimpleExpression
1662 * @param event current LttEvent, NULL if not used
1663 * @param tracefile current LttTracefile, NULL if not used
1664 * @param trace current LttTrace, NULL if not used
1665 * @param state current LttvProcessState, NULL if not used
1666 * @param context current LttvTracefileContext, NULL if not used
1667 * @return response of filter
1670 lttv_filter_tree_parse_branch(
1671 const LttvSimpleExpression
* se
,
1672 const LttEvent
* event
,
1673 const LttTracefile
* tracefile
,
1674 const LttTrace
* trace
,
1675 const LttvProcessState
* state
,
1676 const LttvTracefileContext
* context
) {
1681 case LTTV_FILTER_TRACE_NAME
:
1682 if(trace
== NULL
) return TRUE
;
1684 char* trace_name
= ltt_trace_name(trace
);
1685 GQuark quark
= g_quark_from_string(trace_name
);
1686 return se
->op((gpointer
)&quark
,v
);
1689 case LTTV_FILTER_TRACEFILE_NAME
:
1690 if(tracefile
== NULL
) return TRUE
;
1692 char* tracefile_name
= ltt_tracefile_name(tracefile
);
1693 GQuark quark
= g_quark_from_string(tracefile_name
);
1694 return se
->op((gpointer
)&quark
,v
);
1697 case LTTV_FILTER_STATE_PID
:
1698 if(state
== NULL
) return TRUE
;
1699 else return se
->op((gpointer
)&state
->pid
,v
);
1701 case LTTV_FILTER_STATE_PPID
:
1702 if(state
== NULL
) return TRUE
;
1703 else return se
->op((gpointer
)&state
->ppid
,v
);
1705 case LTTV_FILTER_STATE_CT
:
1706 if(state
== NULL
) return TRUE
;
1708 return se
->op((gpointer
)&state
->creation_time
,v
);
1711 case LTTV_FILTER_STATE_IT
:
1712 if(state
== NULL
) return TRUE
;
1714 return se
->op((gpointer
)&state
->insertion_time
,v
);
1717 case LTTV_FILTER_STATE_P_NAME
:
1719 * All 'unnamed' for the moment
1721 if(state
== NULL
) return TRUE
;
1723 GQuark quark
= state
->name
;
1724 return se
->op((gpointer
)&quark
,v
);
1727 case LTTV_FILTER_STATE_EX_MODE
:
1728 if(state
== NULL
) return TRUE
;
1729 else return se
->op((gpointer
)&state
->state
->t
,v
);
1731 case LTTV_FILTER_STATE_EX_SUBMODE
:
1732 if(state
== NULL
) return TRUE
;
1733 else return se
->op((gpointer
)&state
->state
->n
,v
);
1735 case LTTV_FILTER_STATE_P_STATUS
:
1736 if(state
== NULL
) return TRUE
;
1737 else return se
->op((gpointer
)&state
->state
->s
,v
);
1739 case LTTV_FILTER_STATE_CPU
:
1740 if(context
== NULL
) return TRUE
;
1742 /* FIXME: not sure of that one */
1743 GQuark quark
= ((LttvTracefileState
*)context
)->cpu_name
;
1744 return se
->op((gpointer
)&quark
,v
);
1747 case LTTV_FILTER_EVENT_NAME
:
1748 if(event
== NULL
) return TRUE
;
1751 et
= ltt_event_eventtype(event
);
1752 char* event_name
= ltt_eventtype_name(et
);
1753 GQuark quark
= g_quark_from_string(event_name
);
1754 return se
->op((gpointer
)&quark
,v
);
1758 case LTTV_FILTER_EVENT_CATEGORY
:
1760 * TODO: Not yet implemented
1764 case LTTV_FILTER_EVENT_TIME
:
1765 if(event
== NULL
) return TRUE
;
1767 LttTime time
= ltt_event_time(event
);
1768 return se
->op((gpointer
)&time
,v
);
1771 case LTTV_FILTER_EVENT_TSC
:
1772 if(event
== NULL
) return TRUE
;
1774 LttCycleCount count
= ltt_event_cycle_count(event
);
1775 return se
->op((gpointer
)&count
,v
);
1778 case LTTV_FILTER_EVENT_FIELD
:
1780 * TODO: Use the offset to
1781 * find the dynamic field
1782 * in the event struct
1787 * This case should never be
1788 * parsed, if so, the whole
1789 * filtering is cancelled
1791 g_warning("Error while parsing the filter tree");
1795 /* should never get here */
1803 * Debug function. Prints tree memory allocation.
1804 * @param t the pointer to the current LttvFilterTree
1807 lttv_print_tree(const LttvFilterTree
* t
) {
1809 g_print("node:%p lchild:%p rchild:%p\n",t
, //t->l_child.t,t->r_child.t);
1810 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1811 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
);
1812 g_print("node type: %i / [left] %i / [right] %i\n",t
->node
,t
->left
,t
->right
);
1813 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
);
1814 else if(t
->left
== LTTV_TREE_LEAF
) {
1815 g_print("%p: left is %i %p value\n",t
,t
->l_child
.leaf
->field
,t
->l_child
.leaf
->op
);
1817 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
);
1818 else if(t
->right
== LTTV_TREE_LEAF
) {
1819 g_print("%p: right is %i %p value\n",t
,t
->r_child
.leaf
->field
,t
->r_child
.leaf
->op
);
1825 * @fn static void module_init()
1827 * Initializes the filter module and specific values
1829 static void module_init()
1835 * Destroys the filter module and specific values
1837 static void module_destroy()
1843 LTTV_MODULE("filter", "Filters traceset and events", \
1844 "Filters traceset and events specifically to user input", \
1845 module_init
, module_destroy
)