1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 /*! \file lttv/lttv/filter.c
20 * \brief Defines the core filter of application
22 * consist in AND, OR and NOT nested expressions, forming a tree with
23 * simple relations as leaves. The simple relations test if a field
24 * in an event is equal, not equal, smaller, smaller or equal, larger, or
25 * larger or equal to a specified value.
27 * Fields specified in a simple expression can take following
31 * LttvTracefileContext{}
33 * | |->name (String, converted to GQuark)
34 * | |->facility (String, converted to GQuark)
35 * | |->category (String, not yet implemented)
37 * | |->tsc (LttCycleCount --> uint64)
41 * | |->"sub-field name"
43 * | |->"leaf-field name" (field type)
45 * | |->name (String, converted to GQuark)
47 * | |->name (String, converted to GQuark)
51 * |->creation_time (LttTime)
52 * |->insertion_time (LttTime)
53 * |->process_name (String, converted to GQuark)
54 * |->execution_mode (LttvExecutionMode)
55 * |->execution_submode (LttvExecutionSubmode)
56 * |->process_status (LttvProcessStatus)
63 * - refine switch of expression in multiple uses functions
64 * - remove the idle expressions in the tree
77 #include <lttv/filter.h>
78 #include <ltt/trace.h>
80 #include <ltt/facility.h>
85 * @fn LttvSimpleExpression* lttv_simple_expression_new()
87 * Constructor for LttvSimpleExpression
88 * @return pointer to new LttvSimpleExpression
91 lttv_simple_expression_new() {
93 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
95 se
->field
= LTTV_FILTER_UNDEFINED
;
103 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
105 * Parse through filtering field hierarchy as specified
106 * by user. This function compares each value to
107 * predetermined quarks
108 * @param fp The field path list
109 * @param se current simple expression
110 * @return success/failure of operation
113 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
117 if(fp
->len
< 2) return FALSE
;
118 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
121 * Parse through the specified
124 * Take note however that the
125 * 'event' subfields might change
126 * depending on values specified
127 * in core.xml file. Hence, if
128 * none of the subfields in the
129 * array match the hardcoded
130 * subfields, it will be considered
133 if(!g_strcasecmp(f
->str
,"trace") ) {
138 g_string_free(f
,TRUE
);
139 f
=g_ptr_array_remove_index(fp
,0);
140 if(!g_strcasecmp(f
->str
,"name")) {
141 se
->field
= LTTV_FILTER_TRACE_NAME
;
143 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
145 * FIXME: not yet implemented !
147 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
152 g_string_free(f
,TRUE
);
153 f
=g_ptr_array_remove_index(fp
,0);
154 if(!g_strcasecmp(f
->str
,"name")) {
155 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
157 } else if(!g_strcasecmp(f
->str
,"state") ) {
162 * state.creation_time
163 * state.insertion_time
165 * state.execution_mode
166 * state.execution_submode
167 * state.process_status
170 g_string_free(f
,TRUE
);
171 f
=g_ptr_array_remove_index(fp
,0);
172 if(!g_strcasecmp(f
->str
,"pid") ) {
173 se
->field
= LTTV_FILTER_STATE_PID
;
175 else if(!g_strcasecmp(f
->str
,"ppid") ) {
176 se
->field
= LTTV_FILTER_STATE_PPID
;
178 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
179 se
->field
= LTTV_FILTER_STATE_CT
;
181 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
182 se
->field
= LTTV_FILTER_STATE_IT
;
184 else if(!g_strcasecmp(f
->str
,"process_name") ) {
185 se
->field
= LTTV_FILTER_STATE_P_NAME
;
187 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
188 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
190 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
191 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
193 else if(!g_strcasecmp(f
->str
,"process_status") ) {
194 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
196 else if(!g_strcasecmp(f
->str
,"cpu") ) {
197 se
->field
= LTTV_FILTER_STATE_CPU
;
199 } else if(!g_strcasecmp(f
->str
,"event") ) {
207 g_string_free(f
,TRUE
);
208 f
=g_ptr_array_remove_index(fp
,0);
209 if(!g_strcasecmp(f
->str
,"name") ) {
210 se
->field
= LTTV_FILTER_EVENT_NAME
;
212 else if(!g_strcasecmp(f
->str
,"facility") ) {
213 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
215 else if(!g_strcasecmp(f
->str
,"category") ) {
217 * FIXME: Category not yet functional in lttv
219 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
221 else if(!g_strcasecmp(f
->str
,"time") ) {
222 se
->field
= LTTV_FILTER_EVENT_TIME
;
224 else if(!g_strcasecmp(f
->str
,"tsc") ) {
225 se
->field
= LTTV_FILTER_EVENT_TSC
;
227 else { /* core.xml specified options */
228 se
->field
= LTTV_FILTER_EVENT_FIELD
;
231 g_warning("Unrecognized field in filter string");
234 /* free memory for last string */
235 g_string_free(f
,TRUE
);
237 /* array should be empty */
238 g_assert(fp
->len
== 0);
240 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
241 g_warning("The specified field was not recognized !");
248 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
250 * Sets the function pointer for the current
252 * @param se current simple expression
253 * @param op current operator
254 * @return success/failure of operation
257 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
263 case LTTV_FILTER_TRACE_NAME
:
264 case LTTV_FILTER_TRACEFILE_NAME
:
265 case LTTV_FILTER_STATE_P_NAME
:
266 case LTTV_FILTER_EVENT_NAME
:
267 case LTTV_FILTER_EVENT_FACILITY
:
268 case LTTV_FILTER_STATE_EX_MODE
:
269 case LTTV_FILTER_STATE_EX_SUBMODE
:
270 case LTTV_FILTER_STATE_P_STATUS
:
273 se
->op
= lttv_apply_op_eq_quark
;
276 se
->op
= lttv_apply_op_ne_quark
;
279 g_warning("Error encountered in operator assignment = or != expected");
286 case LTTV_FILTER_EVENT_TSC
:
289 se
->op
= lttv_apply_op_eq_uint64
;
292 se
->op
= lttv_apply_op_ne_uint64
;
295 se
->op
= lttv_apply_op_lt_uint64
;
298 se
->op
= lttv_apply_op_le_uint64
;
301 se
->op
= lttv_apply_op_gt_uint64
;
304 se
->op
= lttv_apply_op_ge_uint64
;
307 g_warning("Error encountered in operator assignment");
314 case LTTV_FILTER_STATE_CPU
:
315 case LTTV_FILTER_STATE_PID
:
316 case LTTV_FILTER_STATE_PPID
:
319 se
->op
= lttv_apply_op_eq_uint
;
322 se
->op
= lttv_apply_op_ne_uint
;
325 se
->op
= lttv_apply_op_lt_uint
;
328 se
->op
= lttv_apply_op_le_uint
;
331 se
->op
= lttv_apply_op_gt_uint
;
334 se
->op
= lttv_apply_op_ge_uint
;
337 g_warning("Error encountered in operator assignment");
344 * Entered as string, converted to enum
346 * can only be compared with 'equal' or 'not equal' operators
348 * unsigned int of 16 bits are used here since enums
349 * should not go over 2^16-1 values
353 // case LTTV_FIELD_EQ:
354 // se->op = lttv_apply_op_eq_uint16;
356 // case LTTV_FIELD_NE:
357 // se->op = lttv_apply_op_ne_uint16;
360 // g_warning("Error encountered in operator assignment = or != expected");
367 case LTTV_FILTER_STATE_CT
:
368 case LTTV_FILTER_STATE_IT
:
369 case LTTV_FILTER_EVENT_TIME
:
372 se
->op
= lttv_apply_op_eq_ltttime
;
375 se
->op
= lttv_apply_op_ne_ltttime
;
378 se
->op
= lttv_apply_op_lt_ltttime
;
381 se
->op
= lttv_apply_op_le_ltttime
;
384 se
->op
= lttv_apply_op_gt_ltttime
;
387 se
->op
= lttv_apply_op_ge_ltttime
;
390 g_warning("Error encountered in operator assignment");
395 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
404 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
406 * Assign the value field to the current LttvSimpleExpression
407 * @param se pointer to the current LttvSimpleExpression
408 * @param value string value for simple expression
411 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
414 gboolean is_double
= FALSE
;
415 LttTime t
= ltt_time_zero
;
421 * entered as strings, converted to Quarks
423 case LTTV_FILTER_TRACE_NAME
:
424 case LTTV_FILTER_TRACEFILE_NAME
:
425 case LTTV_FILTER_STATE_P_NAME
:
426 case LTTV_FILTER_EVENT_NAME
:
427 case LTTV_FILTER_EVENT_FACILITY
:
428 case LTTV_FILTER_STATE_EX_MODE
:
429 case LTTV_FILTER_STATE_EX_SUBMODE
:
430 case LTTV_FILTER_STATE_P_STATUS
:
431 // se->value.v_string = value;
432 se
->value
.v_uint32
= g_quark_from_string(value
);
436 * integer -- supposed to be uint64
438 case LTTV_FILTER_EVENT_TSC
:
439 se
->value
.v_uint64
= atoi(value
);
445 case LTTV_FILTER_STATE_PID
:
446 case LTTV_FILTER_STATE_PPID
:
447 case LTTV_FILTER_STATE_CPU
:
448 se
->value
.v_uint
= atoi(value
);
454 case LTTV_FILTER_STATE_CT
:
455 case LTTV_FILTER_STATE_IT
:
456 case LTTV_FILTER_EVENT_TIME
:
457 //se->value.v_double = atof(value);
459 * parsing logic could be optimised,
460 * but as for now, simpler this way
462 v
= g_string_new("");
463 for(i
=0;i
<strlen(value
);i
++) {
464 if(value
[i
] == '.') {
465 /* cannot specify number with more than one '.' */
466 if(is_double
) return FALSE
;
467 else is_double
= TRUE
;
468 t
.tv_sec
= atoi(v
->str
);
469 g_string_free(v
,TRUE
);
470 v
= g_string_new("");
471 } else g_string_append_c(v
,value
[i
]);
473 /* number can be integer or double */
474 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
475 else t
.tv_sec
= atoi(v
->str
);
477 g_string_free(v
,TRUE
);
479 se
->value
.v_ltttime
= t
;
483 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
493 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
495 * Disallocate memory for the current
497 * @param se pointer to the current LttvSimpleExpression
500 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
502 // g_free(se->value);
503 // switch(se->field) {
504 // case LTTV_FILTER_TRACE_NAME:
505 // case LTTV_FILTER_TRACEFILE_NAME:
506 // case LTTV_FILTER_STATE_P_NAME:
507 // case LTTV_FILTER_EVENT_NAME:
508 // g_free(se->value.v_string);
516 * @fn gint lttv_struct_type(gint)
518 * Finds the structure type depending
519 * on the fields in parameters
520 * @params ft Field of the current structure
521 * @return LttvStructType enum or -1 for error
524 lttv_struct_type(gint ft
) {
527 case LTTV_FILTER_TRACE_NAME
:
528 return LTTV_FILTER_TRACE
;
530 case LTTV_FILTER_TRACEFILE_NAME
:
531 return LTTV_FILTER_TRACEFILE
;
533 case LTTV_FILTER_STATE_PID
:
534 case LTTV_FILTER_STATE_PPID
:
535 case LTTV_FILTER_STATE_CT
:
536 case LTTV_FILTER_STATE_IT
:
537 case LTTV_FILTER_STATE_P_NAME
:
538 case LTTV_FILTER_STATE_EX_MODE
:
539 case LTTV_FILTER_STATE_EX_SUBMODE
:
540 case LTTV_FILTER_STATE_P_STATUS
:
541 case LTTV_FILTER_STATE_CPU
:
542 return LTTV_FILTER_STATE
;
544 case LTTV_FILTER_EVENT_NAME
:
545 case LTTV_FILTER_EVENT_FACILITY
:
546 case LTTV_FILTER_EVENT_CATEGORY
:
547 case LTTV_FILTER_EVENT_TIME
:
548 case LTTV_FILTER_EVENT_TSC
:
549 case LTTV_FILTER_EVENT_FIELD
:
550 return LTTV_FILTER_EVENT
;
558 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
560 * Applies the 'equal' operator to the
561 * specified structure and value
562 * @param v1 left member of comparison
563 * @param v2 right member of comparison
564 * @return success/failure of operation
566 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
568 guint
* r
= (guint
*) v1
;
569 return (*r
== v2
.v_uint
);
574 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
576 * Applies the 'equal' operator to the
577 * specified structure and value
578 * @param v1 left member of comparison
579 * @param v2 right member of comparison
580 * @return success/failure of operation
582 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
584 guint64
* r
= (guint64
*) v1
;
585 return (*r
== v2
.v_uint64
);
590 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
592 * Applies the 'equal' operator to the
593 * specified structure and value
594 * @param v1 left member of comparison
595 * @param v2 right member of comparison
596 * @return success/failure of operation
598 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
599 guint32
* r
= (guint32
*) v1
;
600 return (*r
== v2
.v_uint32
);
604 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
606 * Applies the 'equal' operator to the
607 * specified structure and value
608 * @param v1 left member of comparison
609 * @param v2 right member of comparison
610 * @return success/failure of operation
612 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
613 guint16
* r
= (guint16
*) v1
;
614 return (*r
== v2
.v_uint16
);
618 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
620 * Applies the 'equal' operator to the
621 * specified structure and value
622 * @param v1 left member of comparison
623 * @param v2 right member of comparison
624 * @return success/failure of operation
626 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
627 double* r
= (double*) v1
;
628 return (*r
== v2
.v_double
);
632 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
634 * Applies the 'equal' operator to the
635 * specified structure and value
636 * @param v1 left member of comparison
637 * @param v2 right member of comparison
638 * @return success/failure of operation
640 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
641 char* r
= (char*) v1
;
642 return (!g_strcasecmp(r
,v2
.v_string
));
646 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
648 * Applies the 'equal' operator to the
649 * specified structure and value
650 * @param v1 left member of comparison
651 * @param v2 right member of comparison
652 * @return success/failure of operation
654 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
655 GQuark
* r
= (GQuark
*) v1
;
656 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
657 return (*r
== v2
.v_uint32
);
661 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
663 * Applies the 'equal' operator to the
664 * specified structure and value
665 * @param v1 left member of comparison
666 * @param v2 right member of comparison
667 * @return success/failure of operation
669 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
670 LttTime
* r
= (LttTime
*) v1
;
671 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
675 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
677 * Applies the 'not equal' operator to the
678 * specified structure and value
679 * @param v1 left member of comparison
680 * @param v2 right member of comparison
681 * @return success/failure of operation
683 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
684 guint
* r
= (guint
*) v1
;
685 return (*r
!= v2
.v_uint
);
689 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
691 * Applies the 'not equal' operator to the
692 * specified structure and value
693 * @param v1 left member of comparison
694 * @param v2 right member of comparison
695 * @return success/failure of operation
697 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
698 guint64
* r
= (guint64
*) v1
;
699 return (*r
!= v2
.v_uint64
);
703 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
705 * Applies the 'not equal' operator to the
706 * specified structure and value
707 * @param v1 left member of comparison
708 * @param v2 right member of comparison
709 * @return success/failure of operation
711 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
712 guint32
* r
= (guint32
*) v1
;
713 return (*r
!= v2
.v_uint32
);
717 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
719 * Applies the 'not equal' operator to the
720 * specified structure and value
721 * @param v1 left member of comparison
722 * @param v2 right member of comparison
723 * @return success/failure of operation
725 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
726 guint16
* r
= (guint16
*) v1
;
727 return (*r
!= v2
.v_uint16
);
731 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
733 * Applies the 'not equal' operator to the
734 * specified structure and value
735 * @param v1 left member of comparison
736 * @param v2 right member of comparison
737 * @return success/failure of operation
739 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
740 double* r
= (double*) v1
;
741 return (*r
!= v2
.v_double
);
745 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
747 * Applies the 'not equal' operator to the
748 * specified structure and value
749 * @param v1 left member of comparison
750 * @param v2 right member of comparison
751 * @return success/failure of operation
753 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
754 char* r
= (char*) v1
;
755 return (g_strcasecmp(r
,v2
.v_string
));
759 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
761 * Applies the 'not equal' operator to the
762 * specified structure and value
763 * @param v1 left member of comparison
764 * @param v2 right member of comparison
765 * @return success/failure of operation
767 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
768 GQuark
* r
= (GQuark
*) v1
;
769 return (*r
!= v2
.v_uint32
);
774 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
776 * Applies the 'not equal' operator to the
777 * specified structure and value
778 * @param v1 left member of comparison
779 * @param v2 right member of comparison
780 * @return success/failure of operation
782 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
783 LttTime
* r
= (LttTime
*) v1
;
784 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
788 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
790 * Applies the 'lower than' operator to the
791 * specified structure and value
792 * @param v1 left member of comparison
793 * @param v2 right member of comparison
794 * @return success/failure of operation
796 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
797 guint
* r
= (guint
*) v1
;
798 return (*r
< v2
.v_uint
);
802 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
804 * Applies the 'lower than' operator to the
805 * specified structure and value
806 * @param v1 left member of comparison
807 * @param v2 right member of comparison
808 * @return success/failure of operation
810 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
811 guint64
* r
= (guint64
*) v1
;
812 return (*r
< v2
.v_uint64
);
816 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
818 * Applies the 'lower than' operator to the
819 * specified structure and value
820 * @param v1 left member of comparison
821 * @param v2 right member of comparison
822 * @return success/failure of operation
824 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
825 guint32
* r
= (guint32
*) v1
;
826 return (*r
< v2
.v_uint32
);
830 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
832 * Applies the 'lower than' operator to the
833 * specified structure and value
834 * @param v1 left member of comparison
835 * @param v2 right member of comparison
836 * @return success/failure of operation
838 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
839 guint16
* r
= (guint16
*) v1
;
840 return (*r
< v2
.v_uint16
);
844 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
846 * Applies the 'lower than' operator to the
847 * specified structure and value
848 * @param v1 left member of comparison
849 * @param v2 right member of comparison
850 * @return success/failure of operation
852 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
853 double* r
= (double*) v1
;
854 return (*r
< v2
.v_double
);
858 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
860 * Applies the 'lower than' operator to the
861 * specified structure and value
862 * @param v1 left member of comparison
863 * @param v2 right member of comparison
864 * @return success/failure of operation
866 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
867 LttTime
* r
= (LttTime
*) v1
;
868 // 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)));
869 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
873 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
875 * Applies the 'lower or equal' operator to the
876 * specified structure and value
877 * @param v1 left member of comparison
878 * @param v2 right member of comparison
879 * @return success/failure of operation
881 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
882 guint
* r
= (guint
*) v1
;
883 return (*r
<= v2
.v_uint
);
887 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
889 * Applies the 'lower or equal' operator to the
890 * specified structure and value
891 * @param v1 left member of comparison
892 * @param v2 right member of comparison
893 * @return success/failure of operation
895 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
896 guint64
* r
= (guint64
*) v1
;
897 return (*r
<= v2
.v_uint64
);
901 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
903 * Applies the 'lower or equal' operator to the
904 * specified structure and value
905 * @param v1 left member of comparison
906 * @param v2 right member of comparison
907 * @return success/failure of operation
909 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
910 guint32
* r
= (guint32
*) v1
;
911 return (*r
<= v2
.v_uint32
);
915 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
917 * Applies the 'lower or equal' operator to the
918 * specified structure and value
919 * @param v1 left member of comparison
920 * @param v2 right member of comparison
921 * @return success/failure of operation
923 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
924 guint16
* r
= (guint16
*) v1
;
925 return (*r
<= v2
.v_uint16
);
929 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
931 * Applies the 'lower or equal' operator to the
932 * specified structure and value
933 * @param v1 left member of comparison
934 * @param v2 right member of comparison
935 * @return success/failure of operation
937 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
938 double* r
= (double*) v1
;
939 return (*r
<= v2
.v_double
);
943 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
945 * Applies the 'lower or equal' operator to the
946 * specified structure and value
947 * @param v1 left member of comparison
948 * @param v2 right member of comparison
949 * @return success/failure of operation
951 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
952 LttTime
* r
= (LttTime
*) v1
;
953 // 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)));
954 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
959 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
961 * Applies the 'greater than' operator to the
962 * specified structure and value
963 * @param v1 left member of comparison
964 * @param v2 right member of comparison
965 * @return success/failure of operation
967 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
968 guint
* r
= (guint
*) v1
;
969 return (*r
> v2
.v_uint
);
973 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
975 * Applies the 'greater than' operator to the
976 * specified structure and value
977 * @param v1 left member of comparison
978 * @param v2 right member of comparison
979 * @return success/failure of operation
981 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
982 guint64
* r
= (guint64
*) v1
;
983 return (*r
> v2
.v_uint64
);
987 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
989 * Applies the 'greater than' operator to the
990 * specified structure and value
991 * @param v1 left member of comparison
992 * @param v2 right member of comparison
993 * @return success/failure of operation
995 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
996 guint32
* r
= (guint32
*) v1
;
997 return (*r
> v2
.v_uint32
);
1001 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1003 * Applies the 'greater than' operator to the
1004 * specified structure and value
1005 * @param v1 left member of comparison
1006 * @param v2 right member of comparison
1007 * @return success/failure of operation
1009 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1010 guint16
* r
= (guint16
*) v1
;
1011 return (*r
> v2
.v_uint16
);
1015 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1017 * Applies the 'greater than' operator to the
1018 * specified structure and value
1019 * @param v1 left member of comparison
1020 * @param v2 right member of comparison
1021 * @return success/failure of operation
1023 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1024 double* r
= (double*) v1
;
1025 return (*r
> v2
.v_double
);
1029 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1031 * Applies the 'greater than' operator to the
1032 * specified structure and value
1033 * @param v1 left member of comparison
1034 * @param v2 right member of comparison
1035 * @return success/failure of operation
1037 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1038 LttTime
* r
= (LttTime
*) v1
;
1039 // 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)));
1040 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1044 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1046 * Applies the 'greater or equal' operator to the
1047 * specified structure and value
1048 * @param v1 left member of comparison
1049 * @param v2 right member of comparison
1050 * @return success/failure of operation
1052 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1053 guint
* r
= (guint
*) v1
;
1054 return (*r
>= v2
.v_uint
);
1058 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1060 * Applies the 'greater or equal' operator to the
1061 * specified structure and value
1062 * @param v1 left member of comparison
1063 * @param v2 right member of comparison
1064 * @return success/failure of operation
1066 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1067 guint64
* r
= (guint64
*) v1
;
1068 return (*r
>= v2
.v_uint64
);
1072 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1074 * Applies the 'greater or equal' operator to the
1075 * specified structure and value
1076 * @param v1 left member of comparison
1077 * @param v2 right member of comparison
1078 * @return success/failure of operation
1080 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1081 guint32
* r
= (guint32
*) v1
;
1082 return (*r
>= v2
.v_uint32
);
1086 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1088 * Applies the 'greater or equal' operator to the
1089 * specified structure and value
1090 * @param v1 left member of comparison
1091 * @param v2 right member of comparison
1092 * @return success/failure of operation
1094 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1095 guint16
* r
= (guint16
*) v1
;
1096 return (*r
>= v2
.v_uint16
);
1100 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1102 * Applies the 'greater or equal' operator to the
1103 * specified structure and value
1104 * @param v1 left member of comparison
1105 * @param v2 right member of comparison
1106 * @return success/failure of operation
1108 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1109 double* r
= (double*) v1
;
1110 return (*r
>= v2
.v_double
);
1114 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1116 * Applies the 'greater or equal' operator to the
1117 * specified structure and value
1118 * @param v1 left member of comparison
1119 * @param v2 right member of comparison
1120 * @return success/failure of operation
1122 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1123 LttTime
* r
= (LttTime
*) v1
;
1124 // 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)));
1125 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1131 * Makes a copy of the current filter tree
1132 * @param tree pointer to the current tree
1133 * @return new copy of the filter tree
1136 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1138 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1140 newtree
->node
= tree
->node
;
1142 newtree
->left
= tree
->left
;
1143 if(newtree
->left
== LTTV_TREE_NODE
) {
1144 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1145 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1146 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1147 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1148 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1149 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1150 /* FIXME: special case for string copy ! */
1151 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1154 newtree
->right
= tree
->right
;
1155 if(newtree
->right
== LTTV_TREE_NODE
) {
1156 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1157 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1158 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1159 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1160 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1161 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1162 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1170 * Makes a copy of the current filter
1171 * @param filter pointer to the current filter
1172 * @return new copy of the filter
1175 lttv_filter_clone(const LttvFilter
* filter
) {
1177 if(!filter
) return NULL
;
1179 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1181 strcpy(newfilter
->expression
,filter
->expression
);
1183 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1191 * @fn LttvFilter* lttv_filter_new()
1193 * Creates a new LttvFilter
1194 * @return the current LttvFilter or NULL if error
1199 LttvFilter
* filter
= g_new(LttvFilter
,1);
1200 filter
->expression
= NULL
;
1201 filter
->head
= NULL
;
1208 * @fn gboolean lttv_filter_update(LttvFilter*)
1210 * Updates the current LttvFilter by building
1211 * its tree based upon the expression string
1212 * @param filter pointer to the current LttvFilter
1213 * @return Failure/Success of operation
1216 lttv_filter_update(LttvFilter
* filter
) {
1218 // g_print("filter::lttv_filter_new()\n"); /* debug */
1220 if(filter
->expression
== NULL
) return FALSE
;
1224 p_nesting
=0, /* parenthesis nesting value */
1229 *tree
= lttv_filter_tree_new(), /* main tree */
1230 *subtree
= NULL
, /* buffer for subtrees */
1231 *t1
, /* buffer #1 */
1232 *t2
, /* buffer #2 */
1233 *t3
; /* buffer #3 */
1237 * If the tree already exists,
1238 * destroy it and build a new one
1240 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1241 filter
->head
= NULL
; /* will be assigned at the end */
1245 * each element of the list
1246 * is a sub tree created
1247 * by the use of parenthesis in the
1248 * global expression. The final tree
1249 * will be the one left at the root of
1252 GPtrArray
*tree_stack
= g_ptr_array_new();
1253 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1255 /* temporary values */
1256 GString
*a_field_component
= g_string_new("");
1257 GPtrArray
*a_field_path
= g_ptr_array_new();
1259 /* simple expression buffer */
1260 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1263 * Parse entire expression and construct
1264 * the binary tree. There are two steps
1265 * in browsing that string
1266 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1267 * 2. finding simple expressions
1268 * - field path ( separated by dots )
1269 * - op ( >, <, =, >=, <=, !=)
1270 * - value ( integer, string ... )
1271 * To spare computing time, the whole
1272 * string is parsed in this loop for a
1273 * O(n) complexity order.
1275 * When encountering logical op &,|,^
1276 * 1. parse the last value if any
1277 * 2. create a new tree
1278 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1279 * 4. concatenate this tree with the current tree on top of the stack
1280 * When encountering math ops >,>=,<,<=,=,!=
1281 * 1. add to op to the simple expression
1282 * 2. concatenate last field component to field path
1283 * When encountering concatening ops .
1284 * 1. concatenate last field component to field path
1285 * When encountering opening parenthesis (,{,[
1286 * 1. create a new subtree on top of tree stack
1287 * When encountering closing parenthesis ),},]
1288 * 1. add the expression on right child of the current tree
1289 * 2. the subtree is completed, allocate a new subtree
1290 * 3. pop the tree value from the tree stack
1294 struct timeval starttime
;
1295 struct timeval endtime
;
1296 gettimeofday(&starttime
, NULL
);
1299 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1301 // g_print("%c\n ",filter->expression[i]);
1303 switch(filter
->expression
[i
]) {
1309 /* get current tree in tree stack */
1310 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1312 /* get current node at absolute right */
1313 while(t1
->right
!= LTTV_TREE_IDLE
) {
1314 g_assert(t1
->right
== LTTV_TREE_NODE
);
1317 t2
= lttv_filter_tree_new();
1318 t2
->node
= LTTV_LOGICAL_AND
;
1319 t1
->right
= LTTV_TREE_NODE
;
1321 if(not) { /* add not operator to tree */
1322 t3
= lttv_filter_tree_new();
1323 t3
->node
= LTTV_LOGICAL_NOT
;
1324 t2
->left
= LTTV_TREE_NODE
;
1329 if(subtree
!= NULL
) { /* append subtree to current tree */
1330 t2
->left
= LTTV_TREE_NODE
;
1331 t2
->l_child
.t
= subtree
;
1333 } else { /* append a simple expression */
1334 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1335 a_field_component
= g_string_new("");
1336 t2
->left
= LTTV_TREE_LEAF
;
1337 t2
->l_child
.leaf
= a_simple_expression
;
1338 a_simple_expression
= lttv_simple_expression_new();
1344 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1345 while(t1
->right
!= LTTV_TREE_IDLE
) {
1346 g_assert(t1
->right
== LTTV_TREE_NODE
);
1349 t2
= lttv_filter_tree_new();
1350 t2
->node
= LTTV_LOGICAL_OR
;
1351 t1
->right
= LTTV_TREE_NODE
;
1353 if(not) { // add not operator to tree
1354 t3
= lttv_filter_tree_new();
1355 t3
->node
= LTTV_LOGICAL_NOT
;
1356 t2
->left
= LTTV_TREE_NODE
;
1361 if(subtree
!= NULL
) { /* append subtree to current tree */
1362 t2
->left
= LTTV_TREE_NODE
;
1363 t2
->l_child
.t
= subtree
;
1365 } else { /* append a simple expression */
1366 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1367 a_field_component
= g_string_new("");
1368 t2
->left
= LTTV_TREE_LEAF
;
1369 t2
->l_child
.leaf
= a_simple_expression
;
1370 a_simple_expression
= lttv_simple_expression_new();
1376 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1377 while(t1
->right
!= LTTV_TREE_IDLE
) {
1378 g_assert(t1
->right
== LTTV_TREE_NODE
);
1381 t2
= lttv_filter_tree_new();
1382 t2
->node
= LTTV_LOGICAL_XOR
;
1383 t1
->right
= LTTV_TREE_NODE
;
1385 if(not) { // add not operator to tree
1386 t3
= lttv_filter_tree_new();
1387 t3
->node
= LTTV_LOGICAL_NOT
;
1388 t2
->left
= LTTV_TREE_NODE
;
1393 if(subtree
!= NULL
) { /* append subtree to current tree */
1394 t2
->left
= LTTV_TREE_NODE
;
1395 t2
->l_child
.t
= subtree
;
1397 } else { /* append a simple expression */
1398 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1399 a_field_component
= g_string_new("");
1400 t2
->left
= LTTV_TREE_LEAF
;
1401 t2
->l_child
.leaf
= a_simple_expression
;
1402 a_simple_expression
= lttv_simple_expression_new();
1406 case '!': /* not, or not equal (math op) */
1408 if(filter
->expression
[i
+1] == '=') { /* != */
1409 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1410 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1411 a_field_component
= g_string_new("");
1412 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1419 case '(': /* start of parenthesis */
1423 p_nesting
++; /* incrementing parenthesis nesting value */
1424 t1
= lttv_filter_tree_new();
1425 if(not) { /* add not operator to tree */
1426 t3
= lttv_filter_tree_new();
1427 t3
->node
= LTTV_LOGICAL_NOT
;
1428 t1
->right
= LTTV_TREE_NODE
;
1432 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1435 case ')': /* end of parenthesis */
1439 p_nesting
--; /* decrementing parenthesis nesting value */
1440 if(p_nesting
<0 || tree_stack
->len
<2) {
1441 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1442 is not valid due to parenthesis incorrect use",filter
->expression
);
1446 /* there must at least be the root tree left in the array */
1447 g_assert(tree_stack
->len
>0);
1449 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1450 while(t1
->right
!= LTTV_TREE_IDLE
) {
1453 if(not) { // add not operator to tree
1455 t3
= lttv_filter_tree_new();
1456 t3
->node
= LTTV_LOGICAL_NOT
;
1457 t1
->right
= LTTV_TREE_NODE
;
1462 if(subtree
!= NULL
) { /* append subtree to current tree */
1463 t1
->right
= LTTV_TREE_NODE
;
1464 t1
->r_child
.t
= subtree
;
1465 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1466 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1467 } else { /* assign subtree as current tree */
1468 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1469 a_field_component
= g_string_new("");
1470 t1
->right
= LTTV_TREE_LEAF
;
1471 t1
->r_child
.leaf
= a_simple_expression
;
1472 a_simple_expression
= lttv_simple_expression_new();
1473 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1478 * mathematic operators
1480 case '<': /* lower, lower or equal */
1482 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1483 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1484 a_field_component
= g_string_new("");
1485 if(filter
->expression
[i
+1] == '=') { /* <= */
1487 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1488 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1491 case '>': /* higher, higher or equal */
1493 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1494 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1495 a_field_component
= g_string_new("");
1496 if(filter
->expression
[i
+1] == '=') { /* >= */
1498 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1499 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1502 case '=': /* equal */
1504 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1505 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1506 a_field_component
= g_string_new("");
1507 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1511 * Field concatening caracter
1516 * divide field expression into elements
1517 * in a_field_path array.
1519 * A dot can also be present in double values
1521 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1522 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1523 a_field_component
= g_string_new("");
1526 case ' ': /* ignore */
1527 case '\n': /* ignore */
1529 default: /* concatening current string */
1530 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1535 * Preliminary check to see
1536 * if tree was constructed correctly
1539 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1540 is not valid due to parenthesis incorrect use",filter
->expression
);
1544 if(tree_stack
->len
!= 1) /* only root tree should remain */
1548 * processing last element of expression
1550 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1551 while(t1
->right
!= LTTV_TREE_IDLE
) {
1552 g_assert(t1
->right
== LTTV_TREE_NODE
);
1555 if(not) { // add not operator to tree
1556 t3
= lttv_filter_tree_new();
1557 t3
->node
= LTTV_LOGICAL_NOT
;
1558 t1
->right
= LTTV_TREE_NODE
;
1563 if(subtree
!= NULL
) { /* add the subtree */
1564 t1
->right
= LTTV_TREE_NODE
;
1565 t1
->r_child
.t
= subtree
;
1567 } else { /* add a leaf */
1568 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1569 a_field_component
= NULL
;
1570 t1
->right
= LTTV_TREE_LEAF
;
1571 t1
->r_child
.leaf
= a_simple_expression
;
1572 a_simple_expression
= NULL
;
1576 /* free the pointer array */
1577 g_assert(a_field_path
->len
== 0);
1578 g_ptr_array_free(a_field_path
,TRUE
);
1580 /* free the tree stack -- but keep the root tree */
1581 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1582 g_ptr_array_free(tree_stack
,TRUE
);
1584 /* free the field buffer if allocated */
1585 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1587 /* free the simple expression buffer if allocated */
1588 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1590 g_assert(filter
->head
!= NULL
); /* tree should exist */
1591 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1594 gettimeofday(&endtime
, NULL
);
1596 /* Calcul du temps de l'algorithme */
1597 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1598 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1599 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1600 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1604 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1605 lttv_print_tree(filter
->head
,0) ;
1606 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1614 * @fn void lttv_filter_destroy(LttvFilter*)
1616 * Destroy the current LttvFilter
1617 * @param filter pointer to the current LttvFilter
1620 lttv_filter_destroy(LttvFilter
* filter
) {
1624 if(filter
->expression
)
1625 g_free(filter
->expression
);
1627 lttv_filter_tree_destroy(filter
->head
);
1633 * @fn LttvFilterTree* lttv_filter_tree_new()
1635 * Assign a new tree for the current expression
1637 * @return pointer of LttvFilterTree
1640 lttv_filter_tree_new() {
1641 LttvFilterTree
* tree
;
1643 tree
= g_new(LttvFilterTree
,1);
1644 tree
->node
= 0; //g_new(lttv_expression,1);
1645 tree
->left
= LTTV_TREE_IDLE
;
1646 tree
->right
= LTTV_TREE_IDLE
;
1647 tree
->r_child
.t
= NULL
;
1648 tree
->l_child
.t
= NULL
;
1654 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1656 * Append a new expression to the expression
1657 * defined in the current filter
1658 * @param filter pointer to the current LttvFilter
1659 * @param expression string that must be appended
1660 * @return Success/Failure of operation
1663 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1665 if(expression
== NULL
) return FALSE
;
1666 if(filter
== NULL
) return FALSE
;
1667 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1669 GString
* s
= g_string_new("");
1670 if(filter
->expression
!= NULL
) {
1671 g_string_append(s
,filter
->expression
);
1672 g_string_append_c(s
,'&');
1674 g_string_append(s
,expression
);
1676 g_free(filter
->expression
);
1677 filter
->expression
= g_string_free(s
,FALSE
);
1679 /* TRUE if construction of tree proceeded without errors */
1680 return lttv_filter_update(filter
);
1685 * @fn void lttv_filter_clear_expression(LttvFilter*)
1687 * Clear the filter expression from the
1688 * current filter and sets its pointer to NULL
1689 * @param filter pointer to the current LttvFilter
1692 lttv_filter_clear_expression(LttvFilter
* filter
) {
1694 if(filter
->expression
!= NULL
) {
1695 g_free(filter
->expression
);
1696 filter
->expression
= NULL
;
1702 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1704 * Destroys the tree and his sub-trees
1705 * @param tree Tree which must be destroyed
1708 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1710 if(tree
== NULL
) return;
1712 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1713 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1715 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1716 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1718 // g_free(tree->node);
1723 * Global parsing function for the current
1725 * @param t pointer to the current LttvFilterTree
1726 * @param event current LttEvent, NULL if not used
1727 * @param tracefile current LttTracefile, NULL if not used
1728 * @param trace current LttTrace, NULL if not used
1729 * @param state current LttvProcessState, NULL if not used
1730 * @param context current LttvTracefileContext, NULL if not used
1731 * @return response of filter
1734 lttv_filter_tree_parse(
1735 const LttvFilterTree
* t
,
1736 const LttEvent
* event
,
1737 const LttTracefile
* tracefile
,
1738 const LttTrace
* trace
,
1739 const LttvTracefileContext
* context
1744 * Each tree is parsed in inorder.
1745 * This way, it's possible to apply the left filter of the
1746 * tree, then decide whether or not the right branch should
1747 * be parsed depending on the linking logical operator
1749 * Each node consists in a
1750 * 1. logical operator
1751 * 2. left child ( node or simple expression )
1752 * 3. right child ( node or simple expression )
1754 * When the child is a simple expression, we must
1755 * before all determine if the expression refers to
1756 * a structure which is whithin observation ( not NULL ).
1757 * -If so, the expression is evaluated.
1758 * -If not, the result is set to TRUE since this particular
1759 * operation does not interfere with the lttv structure
1761 * The result of each simple expression will directly
1762 * affect the next branch. This way, depending on
1763 * the linking logical operator, the parser will decide
1764 * to explore or not the next branch.
1766 * -If result of left branch is 0 / FALSE
1767 * then don't explore right branch and return 0;
1768 * -If result of left branch is 1 / TRUE then explore
1770 * -If result of left branch is 1 / TRUE
1771 * then don't explore right branch and return 1;
1772 * -If result of left branch is 0 / FALSE then explore
1774 * -Result of left branch will not affect exploration of
1778 gboolean lresult
= FALSE
, rresult
= FALSE
;
1780 LttvProcessState
* state
;
1782 guint cpu
= ltt_tracefile_num(context
->tf
);
1783 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1784 state
= ts
->running_process
[cpu
];
1789 if(t
->left
== LTTV_TREE_NODE
) {
1790 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1792 else if(t
->left
== LTTV_TREE_LEAF
) {
1793 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1797 * Parse linking operator
1798 * make a cutoff if possible
1800 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1801 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1804 * Parse right branch
1806 if(t
->right
== LTTV_TREE_NODE
) {
1807 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1809 else if(t
->right
== LTTV_TREE_LEAF
) {
1810 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1815 * Apply and return the
1816 * logical link between the
1820 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1821 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1822 case LTTV_LOGICAL_NOT
:
1823 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1824 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1825 case 0: return (rresult
);
1828 * This case should never be
1829 * parsed, if so, this subtree
1838 * This function parses a particular branch of the tree
1839 * @param se pointer to the current LttvSimpleExpression
1840 * @param event current LttEvent, NULL if not used
1841 * @param tracefile current LttTracefile, NULL if not used
1842 * @param trace current LttTrace, NULL if not used
1843 * @param state current LttvProcessState, NULL if not used
1844 * @param context current LttvTracefileContext, NULL if not used
1845 * @return response of filter
1848 lttv_filter_tree_parse_branch(
1849 const LttvSimpleExpression
* se
,
1850 const LttEvent
* event
,
1851 const LttTracefile
* tracefile
,
1852 const LttTrace
* trace
,
1853 const LttvProcessState
* state
,
1854 const LttvTracefileContext
* context
) {
1859 case LTTV_FILTER_TRACE_NAME
:
1860 if(trace
== NULL
) return TRUE
;
1862 GQuark quark
= ltt_trace_name(trace
);
1863 return se
->op((gpointer
)&quark
,v
);
1866 case LTTV_FILTER_TRACEFILE_NAME
:
1867 if(tracefile
== NULL
) return TRUE
;
1869 GQuark quark
= ltt_tracefile_name(tracefile
);
1870 return se
->op((gpointer
)&quark
,v
);
1873 case LTTV_FILTER_STATE_PID
:
1874 if(state
== NULL
) return TRUE
;
1875 else return se
->op((gpointer
)&state
->pid
,v
);
1877 case LTTV_FILTER_STATE_PPID
:
1878 if(state
== NULL
) return TRUE
;
1879 else return se
->op((gpointer
)&state
->ppid
,v
);
1881 case LTTV_FILTER_STATE_CT
:
1882 if(state
== NULL
) return TRUE
;
1884 return se
->op((gpointer
)&state
->creation_time
,v
);
1887 case LTTV_FILTER_STATE_IT
:
1888 if(state
== NULL
) return TRUE
;
1890 return se
->op((gpointer
)&state
->insertion_time
,v
);
1893 case LTTV_FILTER_STATE_P_NAME
:
1895 * All 'unnamed' for the moment
1897 if(state
== NULL
) return TRUE
;
1899 GQuark quark
= state
->name
;
1900 return se
->op((gpointer
)&quark
,v
);
1903 case LTTV_FILTER_STATE_EX_MODE
:
1904 if(state
== NULL
) return TRUE
;
1905 else return se
->op((gpointer
)&state
->state
->t
,v
);
1907 case LTTV_FILTER_STATE_EX_SUBMODE
:
1908 if(state
== NULL
) return TRUE
;
1909 else return se
->op((gpointer
)&state
->state
->n
,v
);
1911 case LTTV_FILTER_STATE_P_STATUS
:
1912 if(state
== NULL
) return TRUE
;
1913 else return se
->op((gpointer
)&state
->state
->s
,v
);
1915 case LTTV_FILTER_STATE_CPU
:
1916 if(context
== NULL
) return TRUE
;
1918 /* FIXME: not sure of that one Mathieu : fixed.*/
1919 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1920 // return se->op((gpointer)&quark,v);
1921 if(state
== NULL
) return TRUE
;
1922 else return se
->op((gpointer
)&state
->cpu
,v
);
1925 case LTTV_FILTER_EVENT_NAME
:
1926 if(event
== NULL
) return TRUE
;
1929 et
= ltt_event_eventtype(event
);
1930 GQuark quark
= ltt_eventtype_name(et
);
1931 return se
->op((gpointer
)&quark
,v
);
1934 case LTTV_FILTER_EVENT_FACILITY
:
1935 if(event
== NULL
) return TRUE
;
1938 fac
= ltt_event_facility(event
);
1939 GQuark quark
= ltt_facility_name(fac
);
1940 return se
->op((gpointer
)&quark
,v
);
1943 case LTTV_FILTER_EVENT_CATEGORY
:
1945 * TODO: Not yet implemented
1949 case LTTV_FILTER_EVENT_TIME
:
1950 if(event
== NULL
) return TRUE
;
1952 LttTime time
= ltt_event_time(event
);
1953 return se
->op((gpointer
)&time
,v
);
1956 case LTTV_FILTER_EVENT_TSC
:
1957 if(event
== NULL
) return TRUE
;
1959 LttCycleCount count
= ltt_event_cycle_count(event
);
1960 return se
->op((gpointer
)&count
,v
);
1963 case LTTV_FILTER_EVENT_FIELD
:
1965 * TODO: Use the offset to
1966 * find the dynamic field
1967 * in the event struct
1972 * This case should never be
1973 * parsed, if so, the whole
1974 * filtering is cancelled
1976 g_warning("Error while parsing the filter tree");
1980 /* should never get here */
1988 * Debug function. Prints tree memory allocation.
1989 * @param t the pointer to the current LttvFilterTree
1992 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1994 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1995 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1996 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1998 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1999 g_debug("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
2000 if(t
->left
== LTTV_TREE_LEAF
) {
2001 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2002 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2003 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2005 g_debug("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
2006 if(t
->right
== LTTV_TREE_LEAF
) {
2007 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2008 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2009 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2012 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2013 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2017 * @fn static void module_init()
2019 * Initializes the filter module and specific values
2021 static void module_init()
2027 * Destroys the filter module and specific values
2029 static void module_destroy()
2035 LTTV_MODULE("filter", "Filters traceset and events", \
2036 "Filters traceset and events specifically to user input", \
2037 module_init
, module_destroy
)