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 if(!filter
) return NULL
;
1048 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1050 strcpy(newfilter
->expression
,filter
->expression
);
1052 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1060 * @fn LttvFilter* lttv_filter_new()
1062 * Creates a new LttvFilter
1063 * @return the current LttvFilter or NULL if error
1068 LttvFilter
* filter
= g_new(LttvFilter
,1);
1069 filter
->expression
= NULL
;
1070 filter
->head
= NULL
;
1077 * @fn gboolean lttv_filter_update(LttvFilter*)
1079 * Updates the current LttvFilter by building
1080 * its tree based upon the expression string
1081 * @param filter pointer to the current LttvFilter
1082 * @return Failure/Success of operation
1085 lttv_filter_update(LttvFilter
* filter
) {
1087 // g_print("filter::lttv_filter_new()\n"); /* debug */
1089 if(filter
->expression
== NULL
) return FALSE
;
1093 p_nesting
=0, /* parenthesis nesting value */
1098 *tree
= lttv_filter_tree_new(), /* main tree */
1099 *subtree
= NULL
, /* buffer for subtrees */
1100 *t1
, /* buffer #1 */
1101 *t2
, /* buffer #2 */
1102 *t3
; /* buffer #3 */
1106 * If the tree already exists,
1107 * destroy it and build a new one
1109 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1110 filter
->head
= NULL
; /* will be assigned at the end */
1114 * each element of the list
1115 * is a sub tree created
1116 * by the use of parenthesis in the
1117 * global expression. The final tree
1118 * will be the one left at the root of
1121 GPtrArray
*tree_stack
= g_ptr_array_new();
1122 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1124 /* temporary values */
1125 GString
*a_field_component
= g_string_new("");
1126 GPtrArray
*a_field_path
= g_ptr_array_new();
1128 /* simple expression buffer */
1129 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1132 * Parse entire expression and construct
1133 * the binary tree. There are two steps
1134 * in browsing that string
1135 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1136 * 2. finding simple expressions
1137 * - field path ( separated by dots )
1138 * - op ( >, <, =, >=, <=, !=)
1139 * - value ( integer, string ... )
1140 * To spare computing time, the whole
1141 * string is parsed in this loop for a
1142 * O(n) complexity order.
1144 * When encountering logical op &,|,^
1145 * 1. parse the last value if any
1146 * 2. create a new tree
1147 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1148 * 4. concatenate this tree with the current tree on top of the stack
1149 * When encountering math ops >,>=,<,<=,=,!=
1150 * 1. add to op to the simple expression
1151 * 2. concatenate last field component to field path
1152 * When encountering concatening ops .
1153 * 1. concatenate last field component to field path
1154 * When encountering opening parenthesis (,{,[
1155 * 1. create a new subtree on top of tree stack
1156 * When encountering closing parenthesis ),},]
1157 * 1. add the expression on right child of the current tree
1158 * 2. the subtree is completed, allocate a new subtree
1159 * 3. pop the tree value from the tree stack
1163 struct timeval starttime
;
1164 struct timeval endtime
;
1165 gettimeofday(&starttime
, NULL
);
1168 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1170 // g_print("%c\n ",filter->expression[i]);
1172 switch(filter
->expression
[i
]) {
1178 /* get current tree in tree stack */
1179 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1181 /* get current node at absolute right */
1182 while(t1
->right
!= LTTV_TREE_IDLE
) {
1183 g_assert(t1
->right
== LTTV_TREE_NODE
);
1186 t2
= lttv_filter_tree_new();
1187 t2
->node
= LTTV_LOGICAL_AND
;
1188 t1
->right
= LTTV_TREE_NODE
;
1190 if(not) { /* add not operator to tree */
1191 t3
= lttv_filter_tree_new();
1192 t3
->node
= LTTV_LOGICAL_NOT
;
1193 t2
->left
= LTTV_TREE_NODE
;
1198 if(subtree
!= NULL
) { /* append subtree to current tree */
1199 t2
->left
= LTTV_TREE_NODE
;
1200 t2
->l_child
.t
= subtree
;
1202 } else { /* append a simple expression */
1203 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1204 a_field_component
= g_string_new("");
1205 t2
->left
= LTTV_TREE_LEAF
;
1206 t2
->l_child
.leaf
= a_simple_expression
;
1207 a_simple_expression
= lttv_simple_expression_new();
1213 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1214 while(t1
->right
!= LTTV_TREE_IDLE
) {
1215 g_assert(t1
->right
== LTTV_TREE_NODE
);
1218 t2
= lttv_filter_tree_new();
1219 t2
->node
= LTTV_LOGICAL_OR
;
1220 t1
->right
= LTTV_TREE_NODE
;
1222 if(not) { // add not operator to tree
1223 t3
= lttv_filter_tree_new();
1224 t3
->node
= LTTV_LOGICAL_NOT
;
1225 t2
->left
= LTTV_TREE_NODE
;
1230 if(subtree
!= NULL
) { /* append subtree to current tree */
1231 t2
->left
= LTTV_TREE_NODE
;
1232 t2
->l_child
.t
= subtree
;
1234 } else { /* append a simple expression */
1235 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1236 a_field_component
= g_string_new("");
1237 t2
->left
= LTTV_TREE_LEAF
;
1238 t2
->l_child
.leaf
= a_simple_expression
;
1239 a_simple_expression
= lttv_simple_expression_new();
1245 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1246 while(t1
->right
!= LTTV_TREE_IDLE
) {
1247 g_assert(t1
->right
== LTTV_TREE_NODE
);
1250 t2
= lttv_filter_tree_new();
1251 t2
->node
= LTTV_LOGICAL_XOR
;
1252 t1
->right
= LTTV_TREE_NODE
;
1254 if(not) { // add not operator to tree
1255 t3
= lttv_filter_tree_new();
1256 t3
->node
= LTTV_LOGICAL_NOT
;
1257 t2
->left
= LTTV_TREE_NODE
;
1262 if(subtree
!= NULL
) { /* append subtree to current tree */
1263 t2
->left
= LTTV_TREE_NODE
;
1264 t2
->l_child
.t
= subtree
;
1266 } else { /* append a simple expression */
1267 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1268 a_field_component
= g_string_new("");
1269 t2
->left
= LTTV_TREE_LEAF
;
1270 t2
->l_child
.leaf
= a_simple_expression
;
1271 a_simple_expression
= lttv_simple_expression_new();
1275 case '!': /* not, or not equal (math op) */
1277 if(filter
->expression
[i
+1] == '=') { /* != */
1278 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1279 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1280 a_field_component
= g_string_new("");
1281 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1288 case '(': /* start of parenthesis */
1292 p_nesting
++; /* incrementing parenthesis nesting value */
1293 t1
= lttv_filter_tree_new();
1294 if(not) { /* add not operator to tree */
1295 t3
= lttv_filter_tree_new();
1296 t3
->node
= LTTV_LOGICAL_NOT
;
1297 t1
->right
= LTTV_TREE_NODE
;
1301 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1304 case ')': /* end of parenthesis */
1308 p_nesting
--; /* decrementing parenthesis nesting value */
1309 if(p_nesting
<0 || tree_stack
->len
<2) {
1310 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1311 is not valid due to parenthesis incorrect use",filter
->expression
);
1315 /* there must at least be the root tree left in the array */
1316 g_assert(tree_stack
->len
>0);
1318 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1319 while(t1
->right
!= LTTV_TREE_IDLE
) {
1322 if(not) { // add not operator to tree
1324 t3
= lttv_filter_tree_new();
1325 t3
->node
= LTTV_LOGICAL_NOT
;
1326 t1
->right
= LTTV_TREE_NODE
;
1331 if(subtree
!= NULL
) { /* append subtree to current tree */
1332 t1
->right
= LTTV_TREE_NODE
;
1333 t1
->r_child
.t
= subtree
;
1334 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1335 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1336 } else { /* assign subtree as current tree */
1337 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1338 a_field_component
= g_string_new("");
1339 t1
->right
= LTTV_TREE_LEAF
;
1340 t1
->r_child
.leaf
= a_simple_expression
;
1341 a_simple_expression
= lttv_simple_expression_new();
1342 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1347 * mathematic operators
1349 case '<': /* lower, lower or equal */
1351 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1352 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1353 a_field_component
= g_string_new("");
1354 if(filter
->expression
[i
+1] == '=') { /* <= */
1356 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1357 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1360 case '>': /* higher, higher or equal */
1362 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1363 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1364 a_field_component
= g_string_new("");
1365 if(filter
->expression
[i
+1] == '=') { /* >= */
1367 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1368 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1371 case '=': /* equal */
1373 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1374 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1375 a_field_component
= g_string_new("");
1376 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1380 * Field concatening caracter
1385 * divide field expression into elements
1386 * in a_field_path array.
1388 * A dot can also be present in double values
1390 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1391 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1392 a_field_component
= g_string_new("");
1395 case ' ': /* ignore */
1396 case '\n': /* ignore */
1398 default: /* concatening current string */
1399 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1404 * Preliminary check to see
1405 * if tree was constructed correctly
1408 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1409 is not valid due to parenthesis incorrect use",filter
->expression
);
1413 if(tree_stack
->len
!= 1) /* only root tree should remain */
1417 * processing last element of expression
1419 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1420 while(t1
->right
!= LTTV_TREE_IDLE
) {
1421 g_assert(t1
->right
== LTTV_TREE_NODE
);
1424 if(not) { // add not operator to tree
1425 t3
= lttv_filter_tree_new();
1426 t3
->node
= LTTV_LOGICAL_NOT
;
1427 t1
->right
= LTTV_TREE_NODE
;
1432 if(subtree
!= NULL
) { /* add the subtree */
1433 t1
->right
= LTTV_TREE_NODE
;
1434 t1
->r_child
.t
= subtree
;
1436 } else { /* add a leaf */
1437 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1438 a_field_component
= NULL
;
1439 t1
->right
= LTTV_TREE_LEAF
;
1440 t1
->r_child
.leaf
= a_simple_expression
;
1441 a_simple_expression
= NULL
;
1445 /* free the pointer array */
1446 g_assert(a_field_path
->len
== 0);
1447 g_ptr_array_free(a_field_path
,TRUE
);
1449 /* free the tree stack -- but keep the root tree */
1450 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1451 g_ptr_array_free(tree_stack
,TRUE
);
1453 /* free the field buffer if allocated */
1454 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1456 /* free the simple expression buffer if allocated */
1457 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1459 g_assert(filter
->head
!= NULL
); /* tree should exist */
1460 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1463 gettimeofday(&endtime
, NULL
);
1465 /* Calcul du temps de l'algorithme */
1466 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1467 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1468 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1469 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1473 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1474 lttv_print_tree(filter
->head
,0) ;
1475 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1483 * @fn void lttv_filter_destroy(LttvFilter*)
1485 * Destroy the current LttvFilter
1486 * @param filter pointer to the current LttvFilter
1489 lttv_filter_destroy(LttvFilter
* filter
) {
1493 if(filter
->expression
)
1494 g_free(filter
->expression
);
1496 lttv_filter_tree_destroy(filter
->head
);
1502 * @fn LttvFilterTree* lttv_filter_tree_new()
1504 * Assign a new tree for the current expression
1506 * @return pointer of LttvFilterTree
1509 lttv_filter_tree_new() {
1510 LttvFilterTree
* tree
;
1512 tree
= g_new(LttvFilterTree
,1);
1513 tree
->node
= 0; //g_new(lttv_expression,1);
1514 tree
->left
= LTTV_TREE_IDLE
;
1515 tree
->right
= LTTV_TREE_IDLE
;
1516 tree
->r_child
.t
= NULL
;
1517 tree
->l_child
.t
= NULL
;
1523 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1525 * Append a new expression to the expression
1526 * defined in the current filter
1527 * @param filter pointer to the current LttvFilter
1528 * @param expression string that must be appended
1529 * @return Success/Failure of operation
1532 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1534 if(expression
== NULL
) return FALSE
;
1535 if(filter
== NULL
) return FALSE
;
1537 GString
* s
= g_string_new("");
1538 if(filter
->expression
!= NULL
) {
1539 g_string_append(s
,filter
->expression
);
1540 g_string_append_c(s
,'&');
1542 g_string_append(s
,expression
);
1544 g_free(filter
->expression
);
1545 filter
->expression
= g_string_free(s
,FALSE
);
1547 /* TRUE if construction of tree proceeded without errors */
1548 return lttv_filter_update(filter
);
1553 * @fn void lttv_filter_clear_expression(LttvFilter*)
1555 * Clear the filter expression from the
1556 * current filter and sets its pointer to NULL
1557 * @param filter pointer to the current LttvFilter
1560 lttv_filter_clear_expression(LttvFilter
* filter
) {
1562 if(filter
->expression
!= NULL
) {
1563 g_free(filter
->expression
);
1564 filter
->expression
= NULL
;
1570 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1572 * Destroys the tree and his sub-trees
1573 * @param tree Tree which must be destroyed
1576 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1578 if(tree
== NULL
) return;
1580 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1581 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1583 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1584 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1586 // g_free(tree->node);
1591 * Global parsing function for the current
1593 * @param t pointer to the current LttvFilterTree
1594 * @param event current LttEvent, NULL if not used
1595 * @param tracefile current LttTracefile, NULL if not used
1596 * @param trace current LttTrace, NULL if not used
1597 * @param state current LttvProcessState, NULL if not used
1598 * @param context current LttvTracefileContext, NULL if not used
1599 * @return response of filter
1602 lttv_filter_tree_parse(
1603 const LttvFilterTree
* t
,
1604 const LttEvent
* event
,
1605 const LttTracefile
* tracefile
,
1606 const LttTrace
* trace
,
1607 const LttvProcessState
* state
,
1608 const LttvTracefileContext
* context
1613 * Each tree is parsed in inorder.
1614 * This way, it's possible to apply the left filter of the
1615 * tree, then decide whether or not the right branch should
1616 * be parsed depending on the linking logical operator
1618 * Each node consists in a
1619 * 1. logical operator
1620 * 2. left child ( node or simple expression )
1621 * 3. right child ( node or simple expression )
1623 * When the child is a simple expression, we must
1624 * before all determine if the expression refers to
1625 * a structure which is whithin observation ( not NULL ).
1626 * -If so, the expression is evaluated.
1627 * -If not, the result is set to TRUE since this particular
1628 * operation does not interfere with the lttv structure
1630 * The result of each simple expression will directly
1631 * affect the next branch. This way, depending on
1632 * the linking logical operator, the parser will decide
1633 * to explore or not the next branch.
1635 * -If result of left branch is 0 / FALSE
1636 * then don't explore right branch and return 0;
1637 * -If result of left branch is 1 / TRUE then explore
1639 * -If result of left branch is 1 / TRUE
1640 * then don't explore right branch and return 1;
1641 * -If result of left branch is 0 / FALSE then explore
1643 * -Result of left branch will not affect exploration of
1647 gboolean lresult
= FALSE
, rresult
= FALSE
;
1652 if(t
->left
== LTTV_TREE_NODE
) {
1653 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,state
,context
);
1655 else if(t
->left
== LTTV_TREE_LEAF
) {
1656 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1660 * Parse linking operator
1661 * make a cutoff if possible
1663 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1664 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1667 * Parse right branch
1669 if(t
->right
== LTTV_TREE_NODE
) {
1670 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,state
,context
);
1672 else if(t
->right
== LTTV_TREE_LEAF
) {
1673 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1678 * Apply and return the
1679 * logical link between the
1683 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1684 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1685 case LTTV_LOGICAL_NOT
:
1686 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1687 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1688 case 0: return (rresult
);
1691 * This case should never be
1692 * parsed, if so, this subtree
1701 * This function parses a particular branch of the tree
1702 * @param se pointer to the current LttvSimpleExpression
1703 * @param event current LttEvent, NULL if not used
1704 * @param tracefile current LttTracefile, NULL if not used
1705 * @param trace current LttTrace, NULL if not used
1706 * @param state current LttvProcessState, NULL if not used
1707 * @param context current LttvTracefileContext, NULL if not used
1708 * @return response of filter
1711 lttv_filter_tree_parse_branch(
1712 const LttvSimpleExpression
* se
,
1713 const LttEvent
* event
,
1714 const LttTracefile
* tracefile
,
1715 const LttTrace
* trace
,
1716 const LttvProcessState
* state
,
1717 const LttvTracefileContext
* context
) {
1722 case LTTV_FILTER_TRACE_NAME
:
1723 if(trace
== NULL
) return TRUE
;
1725 char* trace_name
= ltt_trace_name(trace
);
1726 GQuark quark
= g_quark_from_string(trace_name
);
1727 return se
->op((gpointer
)&quark
,v
);
1730 case LTTV_FILTER_TRACEFILE_NAME
:
1731 if(tracefile
== NULL
) return TRUE
;
1733 char* tracefile_name
= ltt_tracefile_name(tracefile
);
1734 GQuark quark
= g_quark_from_string(tracefile_name
);
1735 return se
->op((gpointer
)&quark
,v
);
1738 case LTTV_FILTER_STATE_PID
:
1739 if(state
== NULL
) return TRUE
;
1740 else return se
->op((gpointer
)&state
->pid
,v
);
1742 case LTTV_FILTER_STATE_PPID
:
1743 if(state
== NULL
) return TRUE
;
1744 else return se
->op((gpointer
)&state
->ppid
,v
);
1746 case LTTV_FILTER_STATE_CT
:
1747 if(state
== NULL
) return TRUE
;
1749 return se
->op((gpointer
)&state
->creation_time
,v
);
1752 case LTTV_FILTER_STATE_IT
:
1753 if(state
== NULL
) return TRUE
;
1755 return se
->op((gpointer
)&state
->insertion_time
,v
);
1758 case LTTV_FILTER_STATE_P_NAME
:
1760 * All 'unnamed' for the moment
1762 if(state
== NULL
) return TRUE
;
1764 GQuark quark
= state
->name
;
1765 return se
->op((gpointer
)&quark
,v
);
1768 case LTTV_FILTER_STATE_EX_MODE
:
1769 if(state
== NULL
) return TRUE
;
1770 else return se
->op((gpointer
)&state
->state
->t
,v
);
1772 case LTTV_FILTER_STATE_EX_SUBMODE
:
1773 if(state
== NULL
) return TRUE
;
1774 else return se
->op((gpointer
)&state
->state
->n
,v
);
1776 case LTTV_FILTER_STATE_P_STATUS
:
1777 if(state
== NULL
) return TRUE
;
1778 else return se
->op((gpointer
)&state
->state
->s
,v
);
1780 case LTTV_FILTER_STATE_CPU
:
1781 if(context
== NULL
) return TRUE
;
1783 /* FIXME: not sure of that one */
1784 GQuark quark
= ((LttvTracefileState
*)context
)->cpu_name
;
1785 return se
->op((gpointer
)&quark
,v
);
1788 case LTTV_FILTER_EVENT_NAME
:
1789 if(event
== NULL
) return TRUE
;
1792 et
= ltt_event_eventtype(event
);
1793 char* event_name
= ltt_eventtype_name(et
);
1794 GQuark quark
= g_quark_from_string(event_name
);
1795 return se
->op((gpointer
)&quark
,v
);
1799 case LTTV_FILTER_EVENT_CATEGORY
:
1801 * TODO: Not yet implemented
1805 case LTTV_FILTER_EVENT_TIME
:
1806 if(event
== NULL
) return TRUE
;
1808 LttTime time
= ltt_event_time(event
);
1809 return se
->op((gpointer
)&time
,v
);
1812 case LTTV_FILTER_EVENT_TSC
:
1813 if(event
== NULL
) return TRUE
;
1815 LttCycleCount count
= ltt_event_cycle_count(event
);
1816 return se
->op((gpointer
)&count
,v
);
1819 case LTTV_FILTER_EVENT_FIELD
:
1821 * TODO: Use the offset to
1822 * find the dynamic field
1823 * in the event struct
1828 * This case should never be
1829 * parsed, if so, the whole
1830 * filtering is cancelled
1832 g_warning("Error while parsing the filter tree");
1836 /* should never get here */
1844 * Debug function. Prints tree memory allocation.
1845 * @param t the pointer to the current LttvFilterTree
1848 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1850 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1851 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1852 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1854 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1855 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"));
1856 if(t
->left
== LTTV_TREE_LEAF
) {
1857 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1858 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1859 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1861 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"));
1862 if(t
->right
== LTTV_TREE_LEAF
) {
1863 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1864 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1865 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1868 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1869 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
1873 * @fn static void module_init()
1875 * Initializes the filter module and specific values
1877 static void module_init()
1883 * Destroys the filter module and specific values
1885 static void module_destroy()
1891 LTTV_MODULE("filter", "Filters traceset and events", \
1892 "Filters traceset and events specifically to user input", \
1893 module_init
, module_destroy
)