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
72 #include <lttv/filter.h>
75 * @fn LttvSimpleExpression* lttv_simple_expression_new()
77 * Constructor for LttvSimpleExpression
78 * @return pointer to new LttvSimpleExpression
81 lttv_simple_expression_new() {
83 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
85 se
->field
= LTTV_FILTER_UNDEFINED
;
93 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
95 * Parse through filtering field hierarchy as specified
96 * by user. This function compares each value to
97 * predetermined quarks
98 * @param fp The field path list
99 * @param se current simple expression
100 * @return success/failure of operation
103 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
107 if(fp
->len
< 2) return FALSE
;
108 g_assert(f
=g_ptr_array_remove_index(fp
,0));
111 * Parse through the specified
114 * Take note however that the
115 * 'event' subfields might change
116 * depending on values specified
117 * in core.xml file. Hence, if
118 * none of the subfields in the
119 * array match the hardcoded
120 * subfields, it will be considered
123 if(!g_strcasecmp(f
->str
,"trace") ) {
128 g_string_free(f
,TRUE
);
129 f
=g_ptr_array_remove_index(fp
,0);
130 if(!g_strcasecmp(f
->str
,"name")) {
131 se
->field
= LTTV_FILTER_TRACE_NAME
;
133 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
135 * FIXME: not yet implemented !
137 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
142 g_string_free(f
,TRUE
);
143 f
=g_ptr_array_remove_index(fp
,0);
144 if(!g_strcasecmp(f
->str
,"name")) {
145 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
147 } else if(!g_strcasecmp(f
->str
,"state") ) {
152 * state.creation_time
153 * state.insertion_time
155 * state.execution_mode
156 * state.execution_submode
157 * state.process_status
160 g_string_free(f
,TRUE
);
161 f
=g_ptr_array_remove_index(fp
,0);
162 if(!g_strcasecmp(f
->str
,"pid") ) {
163 se
->field
= LTTV_FILTER_STATE_PID
;
165 else if(!g_strcasecmp(f
->str
,"ppid") ) {
166 se
->field
= LTTV_FILTER_STATE_PPID
;
168 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
169 se
->field
= LTTV_FILTER_STATE_CT
;
171 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
172 se
->field
= LTTV_FILTER_STATE_IT
;
174 else if(!g_strcasecmp(f
->str
,"process_name") ) {
175 se
->field
= LTTV_FILTER_STATE_P_NAME
;
177 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
178 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
180 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
181 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
183 else if(!g_strcasecmp(f
->str
,"process_status") ) {
184 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
186 else if(!g_strcasecmp(f
->str
,"cpu") ) {
187 se
->field
= LTTV_FILTER_STATE_CPU
;
189 } else if(!g_strcasecmp(f
->str
,"event") ) {
197 g_string_free(f
,TRUE
);
198 f
=g_ptr_array_remove_index(fp
,0);
199 if(!g_strcasecmp(f
->str
,"name") ) {
200 se
->field
= LTTV_FILTER_EVENT_NAME
;
202 else if(!g_strcasecmp(f
->str
,"category") ) {
204 * FIXME: Category not yet functional in lttv
206 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
208 else if(!g_strcasecmp(f
->str
,"time") ) {
209 se
->field
= LTTV_FILTER_EVENT_TIME
;
211 else if(!g_strcasecmp(f
->str
,"tsc") ) {
212 se
->field
= LTTV_FILTER_EVENT_TSC
;
214 else { /* core.xml specified options */
215 se
->field
= LTTV_FILTER_EVENT_FIELD
;
218 g_warning("Unrecognized field in filter string");
221 /* free memory for last string */
222 g_string_free(f
,TRUE
);
224 /* array should be empty */
225 g_assert(fp
->len
== 0);
227 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
228 g_warning("The specified field was not recognized !");
235 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
237 * Sets the function pointer for the current
239 * @param se current simple expression
240 * @param op current operator
241 * @return success/failure of operation
244 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
250 case LTTV_FILTER_TRACE_NAME
:
251 case LTTV_FILTER_TRACEFILE_NAME
:
252 case LTTV_FILTER_STATE_P_NAME
:
253 case LTTV_FILTER_EVENT_NAME
:
254 case LTTV_FILTER_STATE_CPU
:
255 case LTTV_FILTER_STATE_EX_MODE
:
256 case LTTV_FILTER_STATE_EX_SUBMODE
:
257 case LTTV_FILTER_STATE_P_STATUS
:
260 se
->op
= lttv_apply_op_eq_quark
;
263 se
->op
= lttv_apply_op_ne_quark
;
266 g_warning("Error encountered in operator assignment = or != expected");
273 case LTTV_FILTER_STATE_PID
:
274 case LTTV_FILTER_STATE_PPID
:
275 case LTTV_FILTER_EVENT_TSC
:
278 se
->op
= lttv_apply_op_eq_uint64
;
281 se
->op
= lttv_apply_op_ne_uint64
;
284 se
->op
= lttv_apply_op_lt_uint64
;
287 se
->op
= lttv_apply_op_le_uint64
;
290 se
->op
= lttv_apply_op_gt_uint64
;
293 se
->op
= lttv_apply_op_ge_uint64
;
296 g_warning("Error encountered in operator assignment");
302 * Entered as string, converted to enum
304 * can only be compared with 'equal' or 'not equal' operators
306 * unsigned int of 16 bits are used here since enums
307 * should not go over 2^16-1 values
311 // case LTTV_FIELD_EQ:
312 // se->op = lttv_apply_op_eq_uint16;
314 // case LTTV_FIELD_NE:
315 // se->op = lttv_apply_op_ne_uint16;
318 // g_warning("Error encountered in operator assignment = or != expected");
325 case LTTV_FILTER_STATE_CT
:
326 case LTTV_FILTER_STATE_IT
:
327 case LTTV_FILTER_EVENT_TIME
:
330 se
->op
= lttv_apply_op_eq_ltttime
;
333 se
->op
= lttv_apply_op_ne_ltttime
;
336 se
->op
= lttv_apply_op_lt_ltttime
;
339 se
->op
= lttv_apply_op_le_ltttime
;
342 se
->op
= lttv_apply_op_gt_ltttime
;
345 se
->op
= lttv_apply_op_ge_ltttime
;
348 g_warning("Error encountered in operator assignment");
353 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
362 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
364 * Assign the value field to the current LttvSimpleExpression
365 * @param se pointer to the current LttvSimpleExpression
366 * @param value string value for simple expression
369 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
372 gboolean is_double
= FALSE
;
373 LttTime t
= ltt_time_zero
;
380 * entered as strings, converted to Quarks
382 case LTTV_FILTER_TRACE_NAME
:
383 case LTTV_FILTER_TRACEFILE_NAME
:
384 case LTTV_FILTER_STATE_P_NAME
:
385 case LTTV_FILTER_EVENT_NAME
:
386 case LTTV_FILTER_STATE_CPU
:
387 case LTTV_FILTER_STATE_EX_MODE
:
388 case LTTV_FILTER_STATE_EX_SUBMODE
:
389 case LTTV_FILTER_STATE_P_STATUS
:
390 // se->value.v_string = value;
391 se
->value
.v_uint32
= g_quark_from_string(value
);
395 * integer -- supposed to be uint64
397 case LTTV_FILTER_STATE_PID
:
398 case LTTV_FILTER_STATE_PPID
:
399 case LTTV_FILTER_EVENT_TSC
:
400 se
->value
.v_uint64
= atoi(value
);
406 case LTTV_FILTER_STATE_CT
:
407 case LTTV_FILTER_STATE_IT
:
408 case LTTV_FILTER_EVENT_TIME
:
409 //se->value.v_double = atof(value);
411 * parsing logic could be optimised,
412 * but as for now, simpler this way
414 v
= g_string_new("");
415 for(i
=0;i
<strlen(value
);i
++) {
416 if(value
[i
] == '.') {
417 /* cannot specify number with more than one '.' */
418 if(is_double
) return FALSE
;
419 else is_double
= TRUE
;
421 g_string_free(v
,TRUE
);
422 v
= g_string_new("");
423 } else g_string_append_c(v
,value
[i
]);
425 /* number can be integer or double */
426 if(is_double
) t
.tv_nsec
= atoi(v
);
427 else t
.tv_sec
= atoi(v
);
429 g_string_free(v
,TRUE
);
431 se
->value
.v_ltttime
= t
;
435 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
445 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
447 * Disallocate memory for the current
449 * @param se pointer to the current LttvSimpleExpression
452 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
454 // g_free(se->value);
455 // switch(se->field) {
456 // case LTTV_FILTER_TRACE_NAME:
457 // case LTTV_FILTER_TRACEFILE_NAME:
458 // case LTTV_FILTER_STATE_P_NAME:
459 // case LTTV_FILTER_EVENT_NAME:
460 // g_free(se->value.v_string);
468 * @fn gint lttv_struct_type(gint)
470 * Finds the structure type depending
471 * on the fields in parameters
472 * @params ft Field of the current structure
473 * @return LttvStructType enum or -1 for error
476 lttv_struct_type(gint ft
) {
479 case LTTV_FILTER_TRACE_NAME
:
480 return LTTV_FILTER_TRACE
;
482 case LTTV_FILTER_TRACEFILE_NAME
:
483 return LTTV_FILTER_TRACEFILE
;
485 case LTTV_FILTER_STATE_PID
:
486 case LTTV_FILTER_STATE_PPID
:
487 case LTTV_FILTER_STATE_CT
:
488 case LTTV_FILTER_STATE_IT
:
489 case LTTV_FILTER_STATE_P_NAME
:
490 case LTTV_FILTER_STATE_EX_MODE
:
491 case LTTV_FILTER_STATE_EX_SUBMODE
:
492 case LTTV_FILTER_STATE_P_STATUS
:
493 case LTTV_FILTER_STATE_CPU
:
494 return LTTV_FILTER_STATE
;
496 case LTTV_FILTER_EVENT_NAME
:
497 case LTTV_FILTER_EVENT_CATEGORY
:
498 case LTTV_FILTER_EVENT_TIME
:
499 case LTTV_FILTER_EVENT_TSC
:
500 case LTTV_FILTER_EVENT_FIELD
:
501 return LTTV_FILTER_EVENT
;
509 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
511 * Applies the 'equal' operator to the
512 * specified structure and value
513 * @param v1 left member of comparison
514 * @param v2 right member of comparison
515 * @return success/failure of operation
517 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
519 guint64
* r
= (guint64
*) v1
;
520 return (*r
== v2
.v_uint64
);
525 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
527 * Applies the 'equal' operator to the
528 * specified structure and value
529 * @param v1 left member of comparison
530 * @param v2 right member of comparison
531 * @return success/failure of operation
533 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
534 guint32
* r
= (guint32
*) v1
;
535 return (*r
== v2
.v_uint32
);
539 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
541 * Applies the 'equal' operator to the
542 * specified structure and value
543 * @param v1 left member of comparison
544 * @param v2 right member of comparison
545 * @return success/failure of operation
547 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
548 guint16
* r
= (guint16
*) v1
;
549 return (*r
== v2
.v_uint16
);
553 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
555 * Applies the 'equal' operator to the
556 * specified structure and value
557 * @param v1 left member of comparison
558 * @param v2 right member of comparison
559 * @return success/failure of operation
561 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
562 double* r
= (double*) v1
;
563 return (*r
== v2
.v_double
);
567 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
569 * Applies the 'equal' operator to the
570 * specified structure and value
571 * @param v1 left member of comparison
572 * @param v2 right member of comparison
573 * @return success/failure of operation
575 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
576 char* r
= (char*) v1
;
577 return (!g_strcasecmp(r
,v2
.v_string
));
581 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
583 * Applies the 'equal' operator to the
584 * specified structure and value
585 * @param v1 left member of comparison
586 * @param v2 right member of comparison
587 * @return success/failure of operation
589 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
590 GQuark
* r
= (GQuark
*) v1
;
591 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
592 return (*r
== v2
.v_uint32
);
596 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
598 * Applies the 'equal' operator to the
599 * specified structure and value
600 * @param v1 left member of comparison
601 * @param v2 right member of comparison
602 * @return success/failure of operation
604 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
605 LttTime
* r
= (LttTime
*) v1
;
606 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
611 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
613 * Applies the 'not equal' operator to the
614 * specified structure and value
615 * @param v1 left member of comparison
616 * @param v2 right member of comparison
617 * @return success/failure of operation
619 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
620 guint64
* r
= (guint64
*) v1
;
621 return (*r
!= v2
.v_uint64
);
625 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
627 * Applies the 'not equal' operator to the
628 * specified structure and value
629 * @param v1 left member of comparison
630 * @param v2 right member of comparison
631 * @return success/failure of operation
633 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
634 guint32
* r
= (guint32
*) v1
;
635 return (*r
!= v2
.v_uint32
);
639 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
641 * Applies the 'not equal' operator to the
642 * specified structure and value
643 * @param v1 left member of comparison
644 * @param v2 right member of comparison
645 * @return success/failure of operation
647 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
648 guint16
* r
= (guint16
*) v1
;
649 return (*r
!= v2
.v_uint16
);
653 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
655 * Applies the 'not equal' operator to the
656 * specified structure and value
657 * @param v1 left member of comparison
658 * @param v2 right member of comparison
659 * @return success/failure of operation
661 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
662 double* r
= (double*) v1
;
663 return (*r
!= v2
.v_double
);
667 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
669 * Applies the 'not equal' operator to the
670 * specified structure and value
671 * @param v1 left member of comparison
672 * @param v2 right member of comparison
673 * @return success/failure of operation
675 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
676 char* r
= (char*) v1
;
677 return (g_strcasecmp(r
,v2
.v_string
));
681 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
683 * Applies the 'not equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
689 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
690 GQuark
* r
= (GQuark
*) v1
;
691 return (*r
!= v2
.v_uint32
);
696 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
698 * Applies the 'not equal' operator to the
699 * specified structure and value
700 * @param v1 left member of comparison
701 * @param v2 right member of comparison
702 * @return success/failure of operation
704 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
705 LttTime
* r
= (LttTime
*) v1
;
706 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
711 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
713 * Applies the 'lower than' operator to the
714 * specified structure and value
715 * @param v1 left member of comparison
716 * @param v2 right member of comparison
717 * @return success/failure of operation
719 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
720 guint64
* r
= (guint64
*) v1
;
721 return (*r
< v2
.v_uint64
);
725 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
727 * Applies the 'lower than' operator to the
728 * specified structure and value
729 * @param v1 left member of comparison
730 * @param v2 right member of comparison
731 * @return success/failure of operation
733 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
734 guint32
* r
= (guint32
*) v1
;
735 return (*r
< v2
.v_uint32
);
739 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
741 * Applies the 'lower than' operator to the
742 * specified structure and value
743 * @param v1 left member of comparison
744 * @param v2 right member of comparison
745 * @return success/failure of operation
747 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
748 guint16
* r
= (guint16
*) v1
;
749 return (*r
< v2
.v_uint16
);
753 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
755 * Applies the 'lower than' operator to the
756 * specified structure and value
757 * @param v1 left member of comparison
758 * @param v2 right member of comparison
759 * @return success/failure of operation
761 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
762 double* r
= (double*) v1
;
763 return (*r
< v2
.v_double
);
767 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
769 * Applies the 'lower than' operator to the
770 * specified structure and value
771 * @param v1 left member of comparison
772 * @param v2 right member of comparison
773 * @return success/failure of operation
775 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
776 LttTime
* r
= (LttTime
*) v1
;
777 // 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)));
778 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
783 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
785 * Applies the 'lower or equal' operator to the
786 * specified structure and value
787 * @param v1 left member of comparison
788 * @param v2 right member of comparison
789 * @return success/failure of operation
791 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
792 guint64
* r
= (guint64
*) v1
;
793 return (*r
<= v2
.v_uint64
);
797 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
799 * Applies the 'lower or equal' operator to the
800 * specified structure and value
801 * @param v1 left member of comparison
802 * @param v2 right member of comparison
803 * @return success/failure of operation
805 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
806 guint32
* r
= (guint32
*) v1
;
807 return (*r
<= v2
.v_uint32
);
811 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
813 * Applies the 'lower or equal' operator to the
814 * specified structure and value
815 * @param v1 left member of comparison
816 * @param v2 right member of comparison
817 * @return success/failure of operation
819 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
820 guint16
* r
= (guint16
*) v1
;
821 return (*r
<= v2
.v_uint16
);
825 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
827 * Applies the 'lower or equal' operator to the
828 * specified structure and value
829 * @param v1 left member of comparison
830 * @param v2 right member of comparison
831 * @return success/failure of operation
833 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
834 double* r
= (double*) v1
;
835 return (*r
<= v2
.v_double
);
839 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
841 * Applies the 'lower or equal' operator to the
842 * specified structure and value
843 * @param v1 left member of comparison
844 * @param v2 right member of comparison
845 * @return success/failure of operation
847 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
848 LttTime
* r
= (LttTime
*) v1
;
849 // 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)));
850 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
855 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
857 * Applies the 'greater 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_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
864 guint64
* r
= (guint64
*) v1
;
865 return (*r
> v2
.v_uint64
);
869 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
871 * Applies the 'greater 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_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
878 guint32
* r
= (guint32
*) v1
;
879 return (*r
> v2
.v_uint32
);
883 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
885 * Applies the 'greater 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_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
892 guint16
* r
= (guint16
*) v1
;
893 return (*r
> v2
.v_uint16
);
897 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
899 * Applies the 'greater than' operator to the
900 * specified structure and value
901 * @param v1 left member of comparison
902 * @param v2 right member of comparison
903 * @return success/failure of operation
905 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
906 double* r
= (double*) v1
;
907 return (*r
> v2
.v_double
);
911 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
913 * Applies the 'greater than' operator to the
914 * specified structure and value
915 * @param v1 left member of comparison
916 * @param v2 right member of comparison
917 * @return success/failure of operation
919 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
920 LttTime
* r
= (LttTime
*) v1
;
921 // 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)));
922 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
927 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
929 * Applies the 'greater or equal' operator to the
930 * specified structure and value
931 * @param v1 left member of comparison
932 * @param v2 right member of comparison
933 * @return success/failure of operation
935 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
936 guint64
* r
= (guint64
*) v1
;
937 return (*r
>= v2
.v_uint64
);
941 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
943 * Applies the 'greater or equal' operator to the
944 * specified structure and value
945 * @param v1 left member of comparison
946 * @param v2 right member of comparison
947 * @return success/failure of operation
949 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
950 guint32
* r
= (guint32
*) v1
;
951 return (*r
>= v2
.v_uint32
);
955 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
957 * Applies the 'greater or equal' operator to the
958 * specified structure and value
959 * @param v1 left member of comparison
960 * @param v2 right member of comparison
961 * @return success/failure of operation
963 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
964 guint16
* r
= (guint16
*) v1
;
965 return (*r
>= v2
.v_uint16
);
969 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
971 * Applies the 'greater or equal' operator to the
972 * specified structure and value
973 * @param v1 left member of comparison
974 * @param v2 right member of comparison
975 * @return success/failure of operation
977 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
978 double* r
= (double*) v1
;
979 return (*r
>= v2
.v_double
);
983 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
985 * Applies the 'greater or equal' operator to the
986 * specified structure and value
987 * @param v1 left member of comparison
988 * @param v2 right member of comparison
989 * @return success/failure of operation
991 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
992 LttTime
* r
= (LttTime
*) v1
;
993 // 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)));
994 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1000 * Makes a copy of the current filter tree
1001 * @param tree pointer to the current tree
1002 * @return new copy of the filter tree
1005 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1007 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1009 newtree
->node
= tree
->node
;
1011 newtree
->left
= tree
->left
;
1012 if(newtree
->left
== LTTV_TREE_NODE
) {
1013 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1014 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1015 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1016 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1017 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1018 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1019 /* FIXME: special case for string copy ! */
1020 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1023 newtree
->right
= tree
->right
;
1024 if(newtree
->right
== LTTV_TREE_NODE
) {
1025 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1026 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1027 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1028 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1029 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1030 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1031 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1039 * Makes a copy of the current filter
1040 * @param filter pointer to the current filter
1041 * @return new copy of the filter
1044 lttv_filter_clone(const LttvFilter
* filter
) {
1046 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1048 strcpy(newfilter
->expression
,filter
->expression
);
1050 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1058 * @fn LttvFilter* lttv_filter_new()
1060 * Creates a new LttvFilter
1061 * @return the current LttvFilter or NULL if error
1066 LttvFilter
* filter
= g_new(LttvFilter
,1);
1067 filter
->expression
= NULL
;
1068 filter
->head
= NULL
;
1075 * @fn gboolean lttv_filter_update(LttvFilter*)
1077 * Updates the current LttvFilter by building
1078 * its tree based upon the expression string
1079 * @param filter pointer to the current LttvFilter
1080 * @return Failure/Success of operation
1083 lttv_filter_update(LttvFilter
* filter
) {
1085 // g_print("filter::lttv_filter_new()\n"); /* debug */
1087 if(filter
->expression
== NULL
) return FALSE
;
1091 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 */
1100 *t3
; /* buffer #3 */
1104 * If the tree already exists,
1105 * destroy it and build a new one
1107 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1108 filter
->head
= NULL
; /* will be assigned at the end */
1112 * each element of the list
1113 * is a sub tree created
1114 * by the use of parenthesis in the
1115 * global expression. The final tree
1116 * will be the one left at the root of
1119 GPtrArray
*tree_stack
= g_ptr_array_new();
1120 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1122 /* temporary values */
1123 GString
*a_field_component
= g_string_new("");
1124 GPtrArray
*a_field_path
= g_ptr_array_new();
1126 /* simple expression buffer */
1127 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1130 * Parse entire expression and construct
1131 * the binary tree. There are two steps
1132 * in browsing that string
1133 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1134 * 2. finding simple expressions
1135 * - field path ( separated by dots )
1136 * - op ( >, <, =, >=, <=, !=)
1137 * - value ( integer, string ... )
1138 * To spare computing time, the whole
1139 * string is parsed in this loop for a
1140 * O(n) complexity order.
1142 * When encountering logical op &,|,^
1143 * 1. parse the last value if any
1144 * 2. create a new tree
1145 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1146 * 4. concatenate this tree with the current tree on top of the stack
1147 * When encountering math ops >,>=,<,<=,=,!=
1148 * 1. add to op to the simple expression
1149 * 2. concatenate last field component to field path
1150 * When encountering concatening ops .
1151 * 1. concatenate last field component to field path
1152 * When encountering opening parenthesis (,{,[
1153 * 1. create a new subtree on top of tree stack
1154 * When encountering closing parenthesis ),},]
1155 * 1. add the expression on right child of the current tree
1156 * 2. the subtree is completed, allocate a new subtree
1157 * 3. pop the tree value from the tree stack
1161 struct timeval starttime
;
1162 struct timeval endtime
;
1163 gettimeofday(&starttime
, NULL
);
1166 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1168 // g_print("%c\n ",filter->expression[i]);
1170 switch(filter
->expression
[i
]) {
1176 /* get current tree in tree stack */
1177 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1179 /* get current node at absolute right */
1180 while(t1
->right
!= LTTV_TREE_IDLE
) {
1181 g_assert(t1
->right
== LTTV_TREE_NODE
);
1184 t2
= lttv_filter_tree_new();
1185 t2
->node
= LTTV_LOGICAL_AND
;
1186 t1
->right
= LTTV_TREE_NODE
;
1188 if(not) { /* add not operator to tree */
1189 t3
= lttv_filter_tree_new();
1190 t3
->node
= LTTV_LOGICAL_NOT
;
1191 t2
->left
= LTTV_TREE_NODE
;
1196 if(subtree
!= NULL
) { /* append subtree to current tree */
1197 t2
->left
= LTTV_TREE_NODE
;
1198 t2
->l_child
.t
= subtree
;
1200 } else { /* append a simple expression */
1201 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1202 a_field_component
= g_string_new("");
1203 t2
->left
= LTTV_TREE_LEAF
;
1204 t2
->l_child
.leaf
= a_simple_expression
;
1205 a_simple_expression
= lttv_simple_expression_new();
1211 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1212 while(t1
->right
!= LTTV_TREE_IDLE
) {
1213 g_assert(t1
->right
== LTTV_TREE_NODE
);
1216 t2
= lttv_filter_tree_new();
1217 t2
->node
= LTTV_LOGICAL_OR
;
1218 t1
->right
= LTTV_TREE_NODE
;
1220 if(not) { // add not operator to tree
1221 t3
= lttv_filter_tree_new();
1222 t3
->node
= LTTV_LOGICAL_NOT
;
1223 t2
->left
= LTTV_TREE_NODE
;
1228 if(subtree
!= NULL
) { /* append subtree to current tree */
1229 t2
->left
= LTTV_TREE_NODE
;
1230 t2
->l_child
.t
= subtree
;
1232 } else { /* append a simple expression */
1233 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1234 a_field_component
= g_string_new("");
1235 t2
->left
= LTTV_TREE_LEAF
;
1236 t2
->l_child
.leaf
= a_simple_expression
;
1237 a_simple_expression
= lttv_simple_expression_new();
1243 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1244 while(t1
->right
!= LTTV_TREE_IDLE
) {
1245 g_assert(t1
->right
== LTTV_TREE_NODE
);
1248 t2
= lttv_filter_tree_new();
1249 t2
->node
= LTTV_LOGICAL_XOR
;
1250 t1
->right
= LTTV_TREE_NODE
;
1252 if(not) { // add not operator to tree
1253 t3
= lttv_filter_tree_new();
1254 t3
->node
= LTTV_LOGICAL_NOT
;
1255 t2
->left
= LTTV_TREE_NODE
;
1260 if(subtree
!= NULL
) { /* append subtree to current tree */
1261 t2
->left
= LTTV_TREE_NODE
;
1262 t2
->l_child
.t
= subtree
;
1264 } else { /* append a simple expression */
1265 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1266 a_field_component
= g_string_new("");
1267 t2
->left
= LTTV_TREE_LEAF
;
1268 t2
->l_child
.leaf
= a_simple_expression
;
1269 a_simple_expression
= lttv_simple_expression_new();
1273 case '!': /* not, or not equal (math op) */
1275 if(filter
->expression
[i
+1] == '=') { /* != */
1276 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1277 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1278 a_field_component
= g_string_new("");
1279 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1286 case '(': /* start of parenthesis */
1290 p_nesting
++; /* incrementing parenthesis nesting value */
1291 t1
= lttv_filter_tree_new();
1292 if(not) { /* add not operator to tree */
1293 t3
= lttv_filter_tree_new();
1294 t3
->node
= LTTV_LOGICAL_NOT
;
1295 t1
->right
= LTTV_TREE_NODE
;
1299 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1302 case ')': /* end of parenthesis */
1306 p_nesting
--; /* decrementing parenthesis nesting value */
1307 if(p_nesting
<0 || tree_stack
->len
<2) {
1308 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1309 is not valid due to parenthesis incorrect use",filter
->expression
);
1313 /* there must at least be the root tree left in the array */
1314 g_assert(tree_stack
->len
>0);
1316 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1317 while(t1
->right
!= LTTV_TREE_IDLE
) {
1320 if(not) { // add not operator to tree
1322 t3
= lttv_filter_tree_new();
1323 t3
->node
= LTTV_LOGICAL_NOT
;
1324 t1
->right
= LTTV_TREE_NODE
;
1329 if(subtree
!= NULL
) { /* append subtree to current tree */
1330 t1
->right
= LTTV_TREE_NODE
;
1331 t1
->r_child
.t
= subtree
;
1332 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1333 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1334 } else { /* assign subtree as current tree */
1335 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1336 a_field_component
= g_string_new("");
1337 t1
->right
= LTTV_TREE_LEAF
;
1338 t1
->r_child
.leaf
= a_simple_expression
;
1339 a_simple_expression
= lttv_simple_expression_new();
1340 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1345 * mathematic operators
1347 case '<': /* lower, lower or equal */
1349 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1350 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1351 a_field_component
= g_string_new("");
1352 if(filter
->expression
[i
+1] == '=') { /* <= */
1354 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1355 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1358 case '>': /* higher, higher or equal */
1360 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1361 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1362 a_field_component
= g_string_new("");
1363 if(filter
->expression
[i
+1] == '=') { /* >= */
1365 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1366 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1369 case '=': /* equal */
1371 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1372 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1373 a_field_component
= g_string_new("");
1374 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1378 * Field concatening caracter
1383 * divide field expression into elements
1384 * in a_field_path array.
1386 * A dot can also be present in double values
1388 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1389 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1390 a_field_component
= g_string_new("");
1393 case ' ': /* ignore */
1394 case '\n': /* ignore */
1396 default: /* concatening current string */
1397 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1402 * Preliminary check to see
1403 * if tree was constructed correctly
1406 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1407 is not valid due to parenthesis incorrect use",filter
->expression
);
1411 if(tree_stack
->len
!= 1) /* only root tree should remain */
1415 * processing last element of expression
1417 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1418 while(t1
->right
!= LTTV_TREE_IDLE
) {
1419 g_assert(t1
->right
== LTTV_TREE_NODE
);
1422 if(not) { // add not operator to tree
1423 t3
= lttv_filter_tree_new();
1424 t3
->node
= LTTV_LOGICAL_NOT
;
1425 t1
->right
= LTTV_TREE_NODE
;
1430 if(subtree
!= NULL
) { /* add the subtree */
1431 t1
->right
= LTTV_TREE_NODE
;
1432 t1
->r_child
.t
= subtree
;
1434 } else { /* add a leaf */
1435 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1436 a_field_component
= NULL
;
1437 t1
->right
= LTTV_TREE_LEAF
;
1438 t1
->r_child
.leaf
= a_simple_expression
;
1439 a_simple_expression
= NULL
;
1443 /* free the pointer array */
1444 g_assert(a_field_path
->len
== 0);
1445 g_ptr_array_free(a_field_path
,TRUE
);
1447 /* free the tree stack -- but keep the root tree */
1448 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1449 g_ptr_array_free(tree_stack
,TRUE
);
1451 /* free the field buffer if allocated */
1452 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1454 /* free the simple expression buffer if allocated */
1455 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1457 g_assert(filter
->head
!= NULL
); /* tree should exist */
1458 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1461 gettimeofday(&endtime
, NULL
);
1463 /* Calcul du temps de l'algorithme */
1464 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1465 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1466 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1467 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1471 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1472 lttv_print_tree(filter
->head
,0) ;
1473 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1481 * @fn void lttv_filter_destroy(LttvFilter*)
1483 * Destroy the current LttvFilter
1484 * @param filter pointer to the current LttvFilter
1487 lttv_filter_destroy(LttvFilter
* filter
) {
1489 g_free(filter
->expression
);
1490 lttv_filter_tree_destroy(filter
->head
);
1496 * @fn LttvFilterTree* lttv_filter_tree_new()
1498 * Assign a new tree for the current expression
1500 * @return pointer of LttvFilterTree
1503 lttv_filter_tree_new() {
1504 LttvFilterTree
* tree
;
1506 tree
= g_new(LttvFilterTree
,1);
1507 tree
->node
= 0; //g_new(lttv_expression,1);
1508 tree
->left
= LTTV_TREE_IDLE
;
1509 tree
->right
= LTTV_TREE_IDLE
;
1510 tree
->r_child
.t
= NULL
;
1511 tree
->l_child
.t
= NULL
;
1517 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1519 * Append a new expression to the expression
1520 * defined in the current filter
1521 * @param filter pointer to the current LttvFilter
1522 * @param expression string that must be appended
1523 * @return Success/Failure of operation
1526 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1528 if(expression
== NULL
) return FALSE
;
1529 if(filter
== NULL
) return FALSE
;
1531 GString
* s
= g_string_new("");
1532 if(filter
->expression
!= NULL
) {
1533 g_string_append(s
,filter
->expression
);
1534 g_string_append_c(s
,'&');
1536 g_string_append(s
,expression
);
1538 g_free(filter
->expression
);
1539 filter
->expression
= g_string_free(s
,FALSE
);
1541 /* TRUE if construction of tree proceeded without errors */
1542 return lttv_filter_update(filter
);
1547 * @fn void lttv_filter_clear_expression(LttvFilter*)
1549 * Clear the filter expression from the
1550 * current filter and sets its pointer to NULL
1551 * @param filter pointer to the current LttvFilter
1554 lttv_filter_clear_expression(LttvFilter
* filter
) {
1556 if(filter
->expression
!= NULL
) {
1557 g_free(filter
->expression
);
1558 filter
->expression
= NULL
;
1564 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1566 * Destroys the tree and his sub-trees
1567 * @param tree Tree which must be destroyed
1570 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1572 if(tree
== NULL
) return;
1574 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1575 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1577 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1578 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1580 // g_free(tree->node);
1585 * Global parsing function for the current
1587 * @param t pointer to the current LttvFilterTree
1588 * @param event current LttEvent, NULL if not used
1589 * @param tracefile current LttTracefile, NULL if not used
1590 * @param trace current LttTrace, NULL if not used
1591 * @param state current LttvProcessState, NULL if not used
1592 * @param context current LttvTracefileContext, NULL if not used
1593 * @return response of filter
1596 lttv_filter_tree_parse(
1597 const LttvFilterTree
* t
,
1598 const LttEvent
* event
,
1599 const LttTracefile
* tracefile
,
1600 const LttTrace
* trace
,
1601 const LttvProcessState
* state
,
1602 const LttvTracefileContext
* context
1607 * Each tree is parsed in inorder.
1608 * This way, it's possible to apply the left filter of the
1609 * tree, then decide whether or not the right branch should
1610 * be parsed depending on the linking logical operator
1612 * Each node consists in a
1613 * 1. logical operator
1614 * 2. left child ( node or simple expression )
1615 * 3. right child ( node or simple expression )
1617 * When the child is a simple expression, we must
1618 * before all determine if the expression refers to
1619 * a structure which is whithin observation ( not NULL ).
1620 * -If so, the expression is evaluated.
1621 * -If not, the result is set to TRUE since this particular
1622 * operation does not interfere with the lttv structure
1624 * The result of each simple expression will directly
1625 * affect the next branch. This way, depending on
1626 * the linking logical operator, the parser will decide
1627 * to explore or not the next branch.
1629 * -If result of left branch is 0 / FALSE
1630 * then don't explore right branch and return 0;
1631 * -If result of left branch is 1 / TRUE then explore
1633 * -If result of left branch is 1 / TRUE
1634 * then don't explore right branch and return 1;
1635 * -If result of left branch is 0 / FALSE then explore
1637 * -Result of left branch will not affect exploration of
1641 gboolean lresult
= FALSE
, rresult
= FALSE
;
1646 if(t
->left
== LTTV_TREE_NODE
) {
1647 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,state
,context
);
1649 else if(t
->left
== LTTV_TREE_LEAF
) {
1650 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1654 * Parse linking operator
1655 * make a cutoff if possible
1657 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1658 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1661 * Parse right branch
1663 if(t
->right
== LTTV_TREE_NODE
) {
1664 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,state
,context
);
1666 else if(t
->right
== LTTV_TREE_LEAF
) {
1667 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1672 * Apply and return the
1673 * logical link between the
1677 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1678 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1679 case LTTV_LOGICAL_NOT
:
1680 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1681 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1682 case 0: return (rresult
);
1685 * This case should never be
1686 * parsed, if so, this subtree
1695 * This function parses a particular branch of the tree
1696 * @param se pointer to the current LttvSimpleExpression
1697 * @param event current LttEvent, NULL if not used
1698 * @param tracefile current LttTracefile, NULL if not used
1699 * @param trace current LttTrace, NULL if not used
1700 * @param state current LttvProcessState, NULL if not used
1701 * @param context current LttvTracefileContext, NULL if not used
1702 * @return response of filter
1705 lttv_filter_tree_parse_branch(
1706 const LttvSimpleExpression
* se
,
1707 const LttEvent
* event
,
1708 const LttTracefile
* tracefile
,
1709 const LttTrace
* trace
,
1710 const LttvProcessState
* state
,
1711 const LttvTracefileContext
* context
) {
1716 case LTTV_FILTER_TRACE_NAME
:
1717 if(trace
== NULL
) return TRUE
;
1719 char* trace_name
= ltt_trace_name(trace
);
1720 GQuark quark
= g_quark_from_string(trace_name
);
1721 return se
->op((gpointer
)&quark
,v
);
1724 case LTTV_FILTER_TRACEFILE_NAME
:
1725 if(tracefile
== NULL
) return TRUE
;
1727 char* tracefile_name
= ltt_tracefile_name(tracefile
);
1728 GQuark quark
= g_quark_from_string(tracefile_name
);
1729 return se
->op((gpointer
)&quark
,v
);
1732 case LTTV_FILTER_STATE_PID
:
1733 if(state
== NULL
) return TRUE
;
1734 else return se
->op((gpointer
)&state
->pid
,v
);
1736 case LTTV_FILTER_STATE_PPID
:
1737 if(state
== NULL
) return TRUE
;
1738 else return se
->op((gpointer
)&state
->ppid
,v
);
1740 case LTTV_FILTER_STATE_CT
:
1741 if(state
== NULL
) return TRUE
;
1743 return se
->op((gpointer
)&state
->creation_time
,v
);
1746 case LTTV_FILTER_STATE_IT
:
1747 if(state
== NULL
) return TRUE
;
1749 return se
->op((gpointer
)&state
->insertion_time
,v
);
1752 case LTTV_FILTER_STATE_P_NAME
:
1754 * All 'unnamed' for the moment
1756 if(state
== NULL
) return TRUE
;
1758 GQuark quark
= state
->name
;
1759 return se
->op((gpointer
)&quark
,v
);
1762 case LTTV_FILTER_STATE_EX_MODE
:
1763 if(state
== NULL
) return TRUE
;
1764 else return se
->op((gpointer
)&state
->state
->t
,v
);
1766 case LTTV_FILTER_STATE_EX_SUBMODE
:
1767 if(state
== NULL
) return TRUE
;
1768 else return se
->op((gpointer
)&state
->state
->n
,v
);
1770 case LTTV_FILTER_STATE_P_STATUS
:
1771 if(state
== NULL
) return TRUE
;
1772 else return se
->op((gpointer
)&state
->state
->s
,v
);
1774 case LTTV_FILTER_STATE_CPU
:
1775 if(context
== NULL
) return TRUE
;
1777 /* FIXME: not sure of that one */
1778 GQuark quark
= ((LttvTracefileState
*)context
)->cpu_name
;
1779 return se
->op((gpointer
)&quark
,v
);
1782 case LTTV_FILTER_EVENT_NAME
:
1783 if(event
== NULL
) return TRUE
;
1786 et
= ltt_event_eventtype(event
);
1787 char* event_name
= ltt_eventtype_name(et
);
1788 GQuark quark
= g_quark_from_string(event_name
);
1789 return se
->op((gpointer
)&quark
,v
);
1793 case LTTV_FILTER_EVENT_CATEGORY
:
1795 * TODO: Not yet implemented
1799 case LTTV_FILTER_EVENT_TIME
:
1800 if(event
== NULL
) return TRUE
;
1802 LttTime time
= ltt_event_time(event
);
1803 return se
->op((gpointer
)&time
,v
);
1806 case LTTV_FILTER_EVENT_TSC
:
1807 if(event
== NULL
) return TRUE
;
1809 LttCycleCount count
= ltt_event_cycle_count(event
);
1810 return se
->op((gpointer
)&count
,v
);
1813 case LTTV_FILTER_EVENT_FIELD
:
1815 * TODO: Use the offset to
1816 * find the dynamic field
1817 * in the event struct
1822 * This case should never be
1823 * parsed, if so, the whole
1824 * filtering is cancelled
1826 g_warning("Error while parsing the filter tree");
1830 /* should never get here */
1838 * Debug function. Prints tree memory allocation.
1839 * @param t the pointer to the current LttvFilterTree
1842 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1844 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1845 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1846 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1848 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1849 g_print("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1850 if(t
->left
== LTTV_TREE_LEAF
) {
1851 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1852 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1853 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1855 g_print("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1856 if(t
->right
== LTTV_TREE_LEAF
) {
1857 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1858 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1859 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1862 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1863 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
1867 * @fn static void module_init()
1869 * Initializes the filter module and specific values
1871 static void module_init()
1877 * Destroys the filter module and specific values
1879 static void module_destroy()
1885 LTTV_MODULE("filter", "Filters traceset and events", \
1886 "Filters traceset and events specifically to user input", \
1887 module_init
, module_destroy
)