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/lttv.h>
78 #include <lttv/filter.h>
79 #include <ltt/trace.h>
81 #include <ltt/facility.h>
86 * @fn LttvSimpleExpression* lttv_simple_expression_new()
88 * Constructor for LttvSimpleExpression
89 * @return pointer to new LttvSimpleExpression
92 lttv_simple_expression_new() {
94 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
96 se
->field
= LTTV_FILTER_UNDEFINED
;
104 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
106 * Parse through filtering field hierarchy as specified
107 * by user. This function compares each value to
108 * predetermined quarks
109 * @param fp The field path list
110 * @param se current simple expression
111 * @return success/failure of operation
114 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
118 if(fp
->len
< 2) return FALSE
;
119 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
122 * Parse through the specified
125 * Take note however that the
126 * 'event' subfields might change
127 * depending on values specified
128 * in core.xml file. Hence, if
129 * none of the subfields in the
130 * array match the hardcoded
131 * subfields, it will be considered
134 if(!g_strcasecmp(f
->str
,"trace") ) {
139 g_string_free(f
,TRUE
);
140 f
=g_ptr_array_remove_index(fp
,0);
141 if(!g_strcasecmp(f
->str
,"name")) {
142 se
->field
= LTTV_FILTER_TRACE_NAME
;
144 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
146 * FIXME: not yet implemented !
148 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
153 g_string_free(f
,TRUE
);
154 f
=g_ptr_array_remove_index(fp
,0);
155 if(!g_strcasecmp(f
->str
,"name")) {
156 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
158 } else if(!g_strcasecmp(f
->str
,"state") ) {
163 * state.creation_time
164 * state.insertion_time
166 * state.execution_mode
167 * state.execution_submode
168 * state.process_status
171 g_string_free(f
,TRUE
);
172 f
=g_ptr_array_remove_index(fp
,0);
173 if(!g_strcasecmp(f
->str
,"pid") ) {
174 se
->field
= LTTV_FILTER_STATE_PID
;
176 else if(!g_strcasecmp(f
->str
,"ppid") ) {
177 se
->field
= LTTV_FILTER_STATE_PPID
;
179 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
180 se
->field
= LTTV_FILTER_STATE_CT
;
182 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
183 se
->field
= LTTV_FILTER_STATE_IT
;
185 else if(!g_strcasecmp(f
->str
,"process_name") ) {
186 se
->field
= LTTV_FILTER_STATE_P_NAME
;
188 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
189 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
191 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
192 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
194 else if(!g_strcasecmp(f
->str
,"process_status") ) {
195 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
197 else if(!g_strcasecmp(f
->str
,"cpu") ) {
198 se
->field
= LTTV_FILTER_STATE_CPU
;
200 } else if(!g_strcasecmp(f
->str
,"event") ) {
208 g_string_free(f
,TRUE
);
209 f
=g_ptr_array_remove_index(fp
,0);
210 if(!g_strcasecmp(f
->str
,"name") ) {
211 se
->field
= LTTV_FILTER_EVENT_NAME
;
213 else if(!g_strcasecmp(f
->str
,"facility") ) {
214 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
216 else if(!g_strcasecmp(f
->str
,"category") ) {
218 * FIXME: Category not yet functional in lttv
220 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
222 else if(!g_strcasecmp(f
->str
,"time") ) {
223 se
->field
= LTTV_FILTER_EVENT_TIME
;
225 else if(!g_strcasecmp(f
->str
,"tsc") ) {
226 se
->field
= LTTV_FILTER_EVENT_TSC
;
228 else { /* core.xml specified options */
229 se
->field
= LTTV_FILTER_EVENT_FIELD
;
232 g_string_free(f
,TRUE
);
233 f
=g_ptr_array_remove_index(fp
,0);
235 g_warning("Unrecognized field in filter string");
238 /* free memory for last string */
239 g_string_free(f
,TRUE
);
241 /* array should be empty */
242 g_assert(fp
->len
== 0);
244 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
245 g_warning("The specified field was not recognized !");
252 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
254 * Sets the function pointer for the current
256 * @param se current simple expression
257 * @param op current operator
258 * @return success/failure of operation
261 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
267 case LTTV_FILTER_TRACE_NAME
:
268 case LTTV_FILTER_TRACEFILE_NAME
:
269 case LTTV_FILTER_STATE_P_NAME
:
270 case LTTV_FILTER_EVENT_NAME
:
271 case LTTV_FILTER_EVENT_FACILITY
:
272 case LTTV_FILTER_STATE_EX_MODE
:
273 case LTTV_FILTER_STATE_EX_SUBMODE
:
274 case LTTV_FILTER_STATE_P_STATUS
:
277 se
->op
= lttv_apply_op_eq_quark
;
280 se
->op
= lttv_apply_op_ne_quark
;
283 g_warning("Error encountered in operator assignment = or != expected");
290 case LTTV_FILTER_EVENT_TSC
:
293 se
->op
= lttv_apply_op_eq_uint64
;
296 se
->op
= lttv_apply_op_ne_uint64
;
299 se
->op
= lttv_apply_op_lt_uint64
;
302 se
->op
= lttv_apply_op_le_uint64
;
305 se
->op
= lttv_apply_op_gt_uint64
;
308 se
->op
= lttv_apply_op_ge_uint64
;
311 g_warning("Error encountered in operator assignment");
318 case LTTV_FILTER_STATE_CPU
:
319 case LTTV_FILTER_STATE_PID
:
320 case LTTV_FILTER_STATE_PPID
:
323 se
->op
= lttv_apply_op_eq_uint
;
326 se
->op
= lttv_apply_op_ne_uint
;
329 se
->op
= lttv_apply_op_lt_uint
;
332 se
->op
= lttv_apply_op_le_uint
;
335 se
->op
= lttv_apply_op_gt_uint
;
338 se
->op
= lttv_apply_op_ge_uint
;
341 g_warning("Error encountered in operator assignment");
348 * Entered as string, converted to enum
350 * can only be compared with 'equal' or 'not equal' operators
352 * unsigned int of 16 bits are used here since enums
353 * should not go over 2^16-1 values
357 // case LTTV_FIELD_EQ:
358 // se->op = lttv_apply_op_eq_uint16;
360 // case LTTV_FIELD_NE:
361 // se->op = lttv_apply_op_ne_uint16;
364 // g_warning("Error encountered in operator assignment = or != expected");
371 case LTTV_FILTER_STATE_CT
:
372 case LTTV_FILTER_STATE_IT
:
373 case LTTV_FILTER_EVENT_TIME
:
376 se
->op
= lttv_apply_op_eq_ltttime
;
379 se
->op
= lttv_apply_op_ne_ltttime
;
382 se
->op
= lttv_apply_op_lt_ltttime
;
385 se
->op
= lttv_apply_op_le_ltttime
;
388 se
->op
= lttv_apply_op_gt_ltttime
;
391 se
->op
= lttv_apply_op_ge_ltttime
;
394 g_warning("Error encountered in operator assignment");
399 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
408 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
410 * Assign the value field to the current LttvSimpleExpression
411 * @param se pointer to the current LttvSimpleExpression
412 * @param value string value for simple expression
415 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
418 gboolean is_double
= FALSE
;
419 LttTime t
= ltt_time_zero
;
425 * entered as strings, converted to Quarks
427 case LTTV_FILTER_TRACE_NAME
:
428 case LTTV_FILTER_TRACEFILE_NAME
:
429 case LTTV_FILTER_STATE_P_NAME
:
430 case LTTV_FILTER_EVENT_NAME
:
431 case LTTV_FILTER_EVENT_FACILITY
:
432 case LTTV_FILTER_STATE_EX_MODE
:
433 case LTTV_FILTER_STATE_EX_SUBMODE
:
434 case LTTV_FILTER_STATE_P_STATUS
:
435 // se->value.v_string = value;
436 se
->value
.v_uint32
= g_quark_from_string(value
);
440 * integer -- supposed to be uint64
442 case LTTV_FILTER_EVENT_TSC
:
443 se
->value
.v_uint64
= atoi(value
);
449 case LTTV_FILTER_STATE_PID
:
450 case LTTV_FILTER_STATE_PPID
:
451 case LTTV_FILTER_STATE_CPU
:
452 se
->value
.v_uint
= atoi(value
);
458 case LTTV_FILTER_STATE_CT
:
459 case LTTV_FILTER_STATE_IT
:
460 case LTTV_FILTER_EVENT_TIME
:
461 //se->value.v_double = atof(value);
463 * parsing logic could be optimised,
464 * but as for now, simpler this way
466 v
= g_string_new("");
467 for(i
=0;i
<strlen(value
);i
++) {
468 if(value
[i
] == '.') {
469 /* cannot specify number with more than one '.' */
470 if(is_double
) return FALSE
;
471 else is_double
= TRUE
;
472 t
.tv_sec
= atoi(v
->str
);
473 g_string_free(v
,TRUE
);
474 v
= g_string_new("");
475 } else g_string_append_c(v
,value
[i
]);
477 /* number can be integer or double */
478 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
479 else t
.tv_sec
= atoi(v
->str
);
481 g_string_free(v
,TRUE
);
483 se
->value
.v_ltttime
= t
;
487 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
497 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
499 * Disallocate memory for the current
501 * @param se pointer to the current LttvSimpleExpression
504 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
506 // g_free(se->value);
507 // switch(se->field) {
508 // case LTTV_FILTER_TRACE_NAME:
509 // case LTTV_FILTER_TRACEFILE_NAME:
510 // case LTTV_FILTER_STATE_P_NAME:
511 // case LTTV_FILTER_EVENT_NAME:
512 // g_free(se->value.v_string);
520 * @fn gint lttv_struct_type(gint)
522 * Finds the structure type depending
523 * on the fields in parameters
524 * @params ft Field of the current structure
525 * @return LttvStructType enum or -1 for error
528 lttv_struct_type(gint ft
) {
531 case LTTV_FILTER_TRACE_NAME
:
532 return LTTV_FILTER_TRACE
;
534 case LTTV_FILTER_TRACEFILE_NAME
:
535 return LTTV_FILTER_TRACEFILE
;
537 case LTTV_FILTER_STATE_PID
:
538 case LTTV_FILTER_STATE_PPID
:
539 case LTTV_FILTER_STATE_CT
:
540 case LTTV_FILTER_STATE_IT
:
541 case LTTV_FILTER_STATE_P_NAME
:
542 case LTTV_FILTER_STATE_EX_MODE
:
543 case LTTV_FILTER_STATE_EX_SUBMODE
:
544 case LTTV_FILTER_STATE_P_STATUS
:
545 case LTTV_FILTER_STATE_CPU
:
546 return LTTV_FILTER_STATE
;
548 case LTTV_FILTER_EVENT_NAME
:
549 case LTTV_FILTER_EVENT_FACILITY
:
550 case LTTV_FILTER_EVENT_CATEGORY
:
551 case LTTV_FILTER_EVENT_TIME
:
552 case LTTV_FILTER_EVENT_TSC
:
553 case LTTV_FILTER_EVENT_FIELD
:
554 return LTTV_FILTER_EVENT
;
562 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
564 * Applies the 'equal' operator to the
565 * specified structure and value
566 * @param v1 left member of comparison
567 * @param v2 right member of comparison
568 * @return success/failure of operation
570 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
572 guint
* r
= (guint
*) v1
;
573 return (*r
== v2
.v_uint
);
578 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
580 * Applies the 'equal' operator to the
581 * specified structure and value
582 * @param v1 left member of comparison
583 * @param v2 right member of comparison
584 * @return success/failure of operation
586 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
588 guint64
* r
= (guint64
*) v1
;
589 return (*r
== v2
.v_uint64
);
594 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
596 * Applies the 'equal' operator to the
597 * specified structure and value
598 * @param v1 left member of comparison
599 * @param v2 right member of comparison
600 * @return success/failure of operation
602 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
603 guint32
* r
= (guint32
*) v1
;
604 return (*r
== v2
.v_uint32
);
608 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
610 * Applies the 'equal' operator to the
611 * specified structure and value
612 * @param v1 left member of comparison
613 * @param v2 right member of comparison
614 * @return success/failure of operation
616 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
617 guint16
* r
= (guint16
*) v1
;
618 return (*r
== v2
.v_uint16
);
622 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
624 * Applies the 'equal' operator to the
625 * specified structure and value
626 * @param v1 left member of comparison
627 * @param v2 right member of comparison
628 * @return success/failure of operation
630 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
631 double* r
= (double*) v1
;
632 return (*r
== v2
.v_double
);
636 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
638 * Applies the 'equal' operator to the
639 * specified structure and value
640 * @param v1 left member of comparison
641 * @param v2 right member of comparison
642 * @return success/failure of operation
644 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
645 char* r
= (char*) v1
;
646 return (!g_strcasecmp(r
,v2
.v_string
));
650 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
652 * Applies the 'equal' operator to the
653 * specified structure and value
654 * @param v1 left member of comparison
655 * @param v2 right member of comparison
656 * @return success/failure of operation
658 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
659 GQuark
* r
= (GQuark
*) v1
;
660 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
661 return (*r
== v2
.v_uint32
);
665 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
667 * Applies the 'equal' operator to the
668 * specified structure and value
669 * @param v1 left member of comparison
670 * @param v2 right member of comparison
671 * @return success/failure of operation
673 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
674 LttTime
* r
= (LttTime
*) v1
;
675 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
679 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
681 * Applies the 'not equal' operator to the
682 * specified structure and value
683 * @param v1 left member of comparison
684 * @param v2 right member of comparison
685 * @return success/failure of operation
687 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
688 guint
* r
= (guint
*) v1
;
689 return (*r
!= v2
.v_uint
);
693 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
695 * Applies the 'not equal' operator to the
696 * specified structure and value
697 * @param v1 left member of comparison
698 * @param v2 right member of comparison
699 * @return success/failure of operation
701 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
702 guint64
* r
= (guint64
*) v1
;
703 return (*r
!= v2
.v_uint64
);
707 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
709 * Applies the 'not equal' operator to the
710 * specified structure and value
711 * @param v1 left member of comparison
712 * @param v2 right member of comparison
713 * @return success/failure of operation
715 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
716 guint32
* r
= (guint32
*) v1
;
717 return (*r
!= v2
.v_uint32
);
721 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
723 * Applies the 'not equal' operator to the
724 * specified structure and value
725 * @param v1 left member of comparison
726 * @param v2 right member of comparison
727 * @return success/failure of operation
729 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
730 guint16
* r
= (guint16
*) v1
;
731 return (*r
!= v2
.v_uint16
);
735 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
737 * Applies the 'not equal' operator to the
738 * specified structure and value
739 * @param v1 left member of comparison
740 * @param v2 right member of comparison
741 * @return success/failure of operation
743 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
744 double* r
= (double*) v1
;
745 return (*r
!= v2
.v_double
);
749 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
751 * Applies the 'not equal' operator to the
752 * specified structure and value
753 * @param v1 left member of comparison
754 * @param v2 right member of comparison
755 * @return success/failure of operation
757 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
758 char* r
= (char*) v1
;
759 return (g_strcasecmp(r
,v2
.v_string
));
763 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
765 * Applies the 'not equal' operator to the
766 * specified structure and value
767 * @param v1 left member of comparison
768 * @param v2 right member of comparison
769 * @return success/failure of operation
771 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
772 GQuark
* r
= (GQuark
*) v1
;
773 return (*r
!= v2
.v_uint32
);
778 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
780 * Applies the 'not equal' operator to the
781 * specified structure and value
782 * @param v1 left member of comparison
783 * @param v2 right member of comparison
784 * @return success/failure of operation
786 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
787 LttTime
* r
= (LttTime
*) v1
;
788 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
792 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
794 * Applies the 'lower than' operator to the
795 * specified structure and value
796 * @param v1 left member of comparison
797 * @param v2 right member of comparison
798 * @return success/failure of operation
800 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
801 guint
* r
= (guint
*) v1
;
802 return (*r
< v2
.v_uint
);
806 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
808 * Applies the 'lower than' operator to the
809 * specified structure and value
810 * @param v1 left member of comparison
811 * @param v2 right member of comparison
812 * @return success/failure of operation
814 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
815 guint64
* r
= (guint64
*) v1
;
816 return (*r
< v2
.v_uint64
);
820 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
822 * Applies the 'lower than' operator to the
823 * specified structure and value
824 * @param v1 left member of comparison
825 * @param v2 right member of comparison
826 * @return success/failure of operation
828 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
829 guint32
* r
= (guint32
*) v1
;
830 return (*r
< v2
.v_uint32
);
834 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
836 * Applies the 'lower than' operator to the
837 * specified structure and value
838 * @param v1 left member of comparison
839 * @param v2 right member of comparison
840 * @return success/failure of operation
842 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
843 guint16
* r
= (guint16
*) v1
;
844 return (*r
< v2
.v_uint16
);
848 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
850 * Applies the 'lower than' operator to the
851 * specified structure and value
852 * @param v1 left member of comparison
853 * @param v2 right member of comparison
854 * @return success/failure of operation
856 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
857 double* r
= (double*) v1
;
858 return (*r
< v2
.v_double
);
862 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
864 * Applies the 'lower than' operator to the
865 * specified structure and value
866 * @param v1 left member of comparison
867 * @param v2 right member of comparison
868 * @return success/failure of operation
870 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
871 LttTime
* r
= (LttTime
*) v1
;
872 // 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)));
873 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
877 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
879 * Applies the 'lower or equal' operator to the
880 * specified structure and value
881 * @param v1 left member of comparison
882 * @param v2 right member of comparison
883 * @return success/failure of operation
885 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
886 guint
* r
= (guint
*) v1
;
887 return (*r
<= v2
.v_uint
);
891 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
893 * Applies the 'lower or equal' operator to the
894 * specified structure and value
895 * @param v1 left member of comparison
896 * @param v2 right member of comparison
897 * @return success/failure of operation
899 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
900 guint64
* r
= (guint64
*) v1
;
901 return (*r
<= v2
.v_uint64
);
905 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
907 * Applies the 'lower or equal' operator to the
908 * specified structure and value
909 * @param v1 left member of comparison
910 * @param v2 right member of comparison
911 * @return success/failure of operation
913 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
914 guint32
* r
= (guint32
*) v1
;
915 return (*r
<= v2
.v_uint32
);
919 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
921 * Applies the 'lower or equal' operator to the
922 * specified structure and value
923 * @param v1 left member of comparison
924 * @param v2 right member of comparison
925 * @return success/failure of operation
927 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
928 guint16
* r
= (guint16
*) v1
;
929 return (*r
<= v2
.v_uint16
);
933 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
935 * Applies the 'lower or equal' operator to the
936 * specified structure and value
937 * @param v1 left member of comparison
938 * @param v2 right member of comparison
939 * @return success/failure of operation
941 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
942 double* r
= (double*) v1
;
943 return (*r
<= v2
.v_double
);
947 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
949 * Applies the 'lower or equal' operator to the
950 * specified structure and value
951 * @param v1 left member of comparison
952 * @param v2 right member of comparison
953 * @return success/failure of operation
955 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
956 LttTime
* r
= (LttTime
*) v1
;
957 // 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)));
958 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
963 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
965 * Applies the 'greater than' operator to the
966 * specified structure and value
967 * @param v1 left member of comparison
968 * @param v2 right member of comparison
969 * @return success/failure of operation
971 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
972 guint
* r
= (guint
*) v1
;
973 return (*r
> v2
.v_uint
);
977 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
979 * Applies the 'greater than' operator to the
980 * specified structure and value
981 * @param v1 left member of comparison
982 * @param v2 right member of comparison
983 * @return success/failure of operation
985 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
986 guint64
* r
= (guint64
*) v1
;
987 return (*r
> v2
.v_uint64
);
991 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
993 * Applies the 'greater than' operator to the
994 * specified structure and value
995 * @param v1 left member of comparison
996 * @param v2 right member of comparison
997 * @return success/failure of operation
999 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1000 guint32
* r
= (guint32
*) v1
;
1001 return (*r
> v2
.v_uint32
);
1005 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1007 * Applies the 'greater than' operator to the
1008 * specified structure and value
1009 * @param v1 left member of comparison
1010 * @param v2 right member of comparison
1011 * @return success/failure of operation
1013 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1014 guint16
* r
= (guint16
*) v1
;
1015 return (*r
> v2
.v_uint16
);
1019 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1021 * Applies the 'greater than' operator to the
1022 * specified structure and value
1023 * @param v1 left member of comparison
1024 * @param v2 right member of comparison
1025 * @return success/failure of operation
1027 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1028 double* r
= (double*) v1
;
1029 return (*r
> v2
.v_double
);
1033 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1035 * Applies the 'greater than' operator to the
1036 * specified structure and value
1037 * @param v1 left member of comparison
1038 * @param v2 right member of comparison
1039 * @return success/failure of operation
1041 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1042 LttTime
* r
= (LttTime
*) v1
;
1043 // 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)));
1044 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1048 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1050 * Applies the 'greater or equal' operator to the
1051 * specified structure and value
1052 * @param v1 left member of comparison
1053 * @param v2 right member of comparison
1054 * @return success/failure of operation
1056 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1057 guint
* r
= (guint
*) v1
;
1058 return (*r
>= v2
.v_uint
);
1062 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1064 * Applies the 'greater or equal' operator to the
1065 * specified structure and value
1066 * @param v1 left member of comparison
1067 * @param v2 right member of comparison
1068 * @return success/failure of operation
1070 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1071 guint64
* r
= (guint64
*) v1
;
1072 return (*r
>= v2
.v_uint64
);
1076 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1078 * Applies the 'greater or equal' operator to the
1079 * specified structure and value
1080 * @param v1 left member of comparison
1081 * @param v2 right member of comparison
1082 * @return success/failure of operation
1084 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1085 guint32
* r
= (guint32
*) v1
;
1086 return (*r
>= v2
.v_uint32
);
1090 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1092 * Applies the 'greater or equal' operator to the
1093 * specified structure and value
1094 * @param v1 left member of comparison
1095 * @param v2 right member of comparison
1096 * @return success/failure of operation
1098 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1099 guint16
* r
= (guint16
*) v1
;
1100 return (*r
>= v2
.v_uint16
);
1104 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1106 * Applies the 'greater or equal' operator to the
1107 * specified structure and value
1108 * @param v1 left member of comparison
1109 * @param v2 right member of comparison
1110 * @return success/failure of operation
1112 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1113 double* r
= (double*) v1
;
1114 return (*r
>= v2
.v_double
);
1118 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1120 * Applies the 'greater or equal' operator to the
1121 * specified structure and value
1122 * @param v1 left member of comparison
1123 * @param v2 right member of comparison
1124 * @return success/failure of operation
1126 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1127 LttTime
* r
= (LttTime
*) v1
;
1128 // 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)));
1129 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1135 * Makes a copy of the current filter tree
1136 * @param tree pointer to the current tree
1137 * @return new copy of the filter tree
1140 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1142 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1144 newtree
->node
= tree
->node
;
1146 newtree
->left
= tree
->left
;
1147 if(newtree
->left
== LTTV_TREE_NODE
) {
1148 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1149 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1150 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1151 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1152 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1153 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1154 /* FIXME: special case for string copy ! */
1155 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1158 newtree
->right
= tree
->right
;
1159 if(newtree
->right
== LTTV_TREE_NODE
) {
1160 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1161 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1162 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1163 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1164 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1165 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1166 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1174 * Makes a copy of the current filter
1175 * @param filter pointer to the current filter
1176 * @return new copy of the filter
1179 lttv_filter_clone(const LttvFilter
* filter
) {
1181 if(!filter
) return NULL
;
1183 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1185 strcpy(newfilter
->expression
,filter
->expression
);
1187 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1195 * @fn LttvFilter* lttv_filter_new()
1197 * Creates a new LttvFilter
1198 * @return the current LttvFilter or NULL if error
1203 LttvFilter
* filter
= g_new(LttvFilter
,1);
1204 filter
->expression
= NULL
;
1205 filter
->head
= NULL
;
1212 * @fn gboolean lttv_filter_update(LttvFilter*)
1214 * Updates the current LttvFilter by building
1215 * its tree based upon the expression string
1216 * @param filter pointer to the current LttvFilter
1217 * @return Failure/Success of operation
1220 lttv_filter_update(LttvFilter
* filter
) {
1222 // g_print("filter::lttv_filter_new()\n"); /* debug */
1224 if(filter
->expression
== NULL
) return FALSE
;
1228 p_nesting
=0, /* parenthesis nesting value */
1233 *tree
= lttv_filter_tree_new(), /* main tree */
1234 *subtree
= NULL
, /* buffer for subtrees */
1235 *t1
, /* buffer #1 */
1236 *t2
, /* buffer #2 */
1237 *t3
; /* buffer #3 */
1241 * If the tree already exists,
1242 * destroy it and build a new one
1244 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1245 filter
->head
= NULL
; /* will be assigned at the end */
1249 * each element of the list
1250 * is a sub tree created
1251 * by the use of parenthesis in the
1252 * global expression. The final tree
1253 * will be the one left at the root of
1256 GPtrArray
*tree_stack
= g_ptr_array_new();
1257 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1259 /* temporary values */
1260 GString
*a_field_component
= g_string_new("");
1261 GString
*a_string_spaces
= g_string_new("");
1262 GPtrArray
*a_field_path
= g_ptr_array_new();
1264 /* simple expression buffer */
1265 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1268 * Parse entire expression and construct
1269 * the binary tree. There are two steps
1270 * in browsing that string
1271 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1272 * 2. finding simple expressions
1273 * - field path ( separated by dots )
1274 * - op ( >, <, =, >=, <=, !=)
1275 * - value ( integer, string ... )
1276 * To spare computing time, the whole
1277 * string is parsed in this loop for a
1278 * O(n) complexity order.
1280 * When encountering logical op &,|,^
1281 * 1. parse the last value if any
1282 * 2. create a new tree
1283 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1284 * 4. concatenate this tree with the current tree on top of the stack
1285 * When encountering math ops >,>=,<,<=,=,!=
1286 * 1. add to op to the simple expression
1287 * 2. concatenate last field component to field path
1288 * When encountering concatening ops .
1289 * 1. concatenate last field component to field path
1290 * When encountering opening parenthesis (,{,[
1291 * 1. create a new subtree on top of tree stack
1292 * When encountering closing parenthesis ),},]
1293 * 1. add the expression on right child of the current tree
1294 * 2. the subtree is completed, allocate a new subtree
1295 * 3. pop the tree value from the tree stack
1299 struct timeval starttime
;
1300 struct timeval endtime
;
1301 gettimeofday(&starttime
, NULL
);
1304 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1306 // g_print("%c\n ",filter->expression[i]);
1308 switch(filter
->expression
[i
]) {
1314 /* get current tree in tree stack */
1315 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1317 /* get current node at absolute right */
1318 while(t1
->right
!= LTTV_TREE_IDLE
) {
1319 g_assert(t1
->right
== LTTV_TREE_NODE
);
1322 t2
= lttv_filter_tree_new();
1323 t2
->node
= LTTV_LOGICAL_AND
;
1324 t1
->right
= LTTV_TREE_NODE
;
1326 if(not) { /* add not operator to tree */
1327 t3
= lttv_filter_tree_new();
1328 t3
->node
= LTTV_LOGICAL_NOT
;
1329 t2
->left
= LTTV_TREE_NODE
;
1334 if(subtree
!= NULL
) { /* append subtree to current tree */
1335 t2
->left
= LTTV_TREE_NODE
;
1336 t2
->l_child
.t
= subtree
;
1338 } else { /* append a simple expression */
1339 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1340 a_field_component
= g_string_new("");
1341 g_string_free(a_string_spaces
, TRUE
);
1342 a_string_spaces
= g_string_new("");
1343 t2
->left
= LTTV_TREE_LEAF
;
1344 t2
->l_child
.leaf
= a_simple_expression
;
1345 a_simple_expression
= lttv_simple_expression_new();
1351 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1352 while(t1
->right
!= LTTV_TREE_IDLE
) {
1353 g_assert(t1
->right
== LTTV_TREE_NODE
);
1356 t2
= lttv_filter_tree_new();
1357 t2
->node
= LTTV_LOGICAL_OR
;
1358 t1
->right
= LTTV_TREE_NODE
;
1360 if(not) { // add not operator to tree
1361 t3
= lttv_filter_tree_new();
1362 t3
->node
= LTTV_LOGICAL_NOT
;
1363 t2
->left
= LTTV_TREE_NODE
;
1368 if(subtree
!= NULL
) { /* append subtree to current tree */
1369 t2
->left
= LTTV_TREE_NODE
;
1370 t2
->l_child
.t
= subtree
;
1372 } else { /* append a simple expression */
1373 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1374 a_field_component
= g_string_new("");
1375 g_string_free(a_string_spaces
, TRUE
);
1376 a_string_spaces
= g_string_new("");
1377 t2
->left
= LTTV_TREE_LEAF
;
1378 t2
->l_child
.leaf
= a_simple_expression
;
1379 a_simple_expression
= lttv_simple_expression_new();
1385 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1386 while(t1
->right
!= LTTV_TREE_IDLE
) {
1387 g_assert(t1
->right
== LTTV_TREE_NODE
);
1390 t2
= lttv_filter_tree_new();
1391 t2
->node
= LTTV_LOGICAL_XOR
;
1392 t1
->right
= LTTV_TREE_NODE
;
1394 if(not) { // add not operator to tree
1395 t3
= lttv_filter_tree_new();
1396 t3
->node
= LTTV_LOGICAL_NOT
;
1397 t2
->left
= LTTV_TREE_NODE
;
1402 if(subtree
!= NULL
) { /* append subtree to current tree */
1403 t2
->left
= LTTV_TREE_NODE
;
1404 t2
->l_child
.t
= subtree
;
1406 } else { /* append a simple expression */
1407 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1408 a_field_component
= g_string_new("");
1409 g_string_free(a_string_spaces
, TRUE
);
1410 a_string_spaces
= g_string_new("");
1411 t2
->left
= LTTV_TREE_LEAF
;
1412 t2
->l_child
.leaf
= a_simple_expression
;
1413 a_simple_expression
= lttv_simple_expression_new();
1417 case '!': /* not, or not equal (math op) */
1419 if(filter
->expression
[i
+1] == '=') { /* != */
1420 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1421 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1422 a_field_component
= g_string_new("");
1423 g_string_free(a_string_spaces
, TRUE
);
1424 a_string_spaces
= g_string_new("");
1425 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1432 case '(': /* start of parenthesis */
1436 p_nesting
++; /* incrementing parenthesis nesting value */
1437 t1
= lttv_filter_tree_new();
1438 if(not) { /* add not operator to tree */
1439 t3
= lttv_filter_tree_new();
1440 t3
->node
= LTTV_LOGICAL_NOT
;
1441 t1
->right
= LTTV_TREE_NODE
;
1445 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1448 case ')': /* end of parenthesis */
1452 p_nesting
--; /* decrementing parenthesis nesting value */
1453 if(p_nesting
<0 || tree_stack
->len
<2) {
1454 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1455 is not valid due to parenthesis incorrect use",filter
->expression
);
1459 /* there must at least be the root tree left in the array */
1460 g_assert(tree_stack
->len
>0);
1462 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1463 while(t1
->right
!= LTTV_TREE_IDLE
) {
1466 if(not) { // add not operator to tree
1468 t3
= lttv_filter_tree_new();
1469 t3
->node
= LTTV_LOGICAL_NOT
;
1470 t1
->right
= LTTV_TREE_NODE
;
1475 if(subtree
!= NULL
) { /* append subtree to current tree */
1476 t1
->right
= LTTV_TREE_NODE
;
1477 t1
->r_child
.t
= subtree
;
1478 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1479 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1480 } else { /* assign subtree as current tree */
1481 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1482 a_field_component
= g_string_new("");
1483 g_string_free(a_string_spaces
, TRUE
);
1484 a_string_spaces
= g_string_new("");
1485 t1
->right
= LTTV_TREE_LEAF
;
1486 t1
->r_child
.leaf
= a_simple_expression
;
1487 a_simple_expression
= lttv_simple_expression_new();
1488 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1493 * mathematic operators
1495 case '<': /* lower, lower or equal */
1497 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1498 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1499 a_field_component
= g_string_new("");
1500 g_string_free(a_string_spaces
, TRUE
);
1501 a_string_spaces
= g_string_new("");
1502 if(filter
->expression
[i
+1] == '=') { /* <= */
1504 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1505 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1508 case '>': /* higher, higher or equal */
1510 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1511 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1512 a_field_component
= g_string_new("");
1513 g_string_free(a_string_spaces
, TRUE
);
1514 a_string_spaces
= g_string_new("");
1515 if(filter
->expression
[i
+1] == '=') { /* >= */
1517 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1518 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1521 case '=': /* equal */
1523 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1524 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1525 a_field_component
= g_string_new("");
1526 g_string_free(a_string_spaces
, TRUE
);
1527 a_string_spaces
= g_string_new("");
1528 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1532 * Field concatening caracter
1537 * divide field expression into elements
1538 * in a_field_path array.
1540 * A dot can also be present in double values
1542 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1543 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1544 a_field_component
= g_string_new("");
1545 g_string_free(a_string_spaces
, TRUE
);
1546 a_string_spaces
= g_string_new("");
1549 case ' ': /* keep spaces that are within a field component */
1550 if(a_field_component
->len
== 0) break; /* ignore */
1552 a_string_spaces
= g_string_append_c(a_string_spaces
,
1553 filter
->expression
[i
]);
1555 case '\n': /* ignore */
1557 default: /* concatening current string */
1558 if(a_string_spaces
->len
!= 0) {
1559 g_string_append(a_field_component
, a_string_spaces
->str
);
1560 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1562 a_field_component
= g_string_append_c(a_field_component
,
1563 filter
->expression
[i
]);
1568 * Preliminary check to see
1569 * if tree was constructed correctly
1572 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1573 is not valid due to parenthesis incorrect use",filter
->expression
);
1577 if(tree_stack
->len
!= 1) /* only root tree should remain */
1581 * processing last element of expression
1583 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1584 while(t1
->right
!= LTTV_TREE_IDLE
) {
1585 g_assert(t1
->right
== LTTV_TREE_NODE
);
1588 if(not) { // add not operator to tree
1589 t3
= lttv_filter_tree_new();
1590 t3
->node
= LTTV_LOGICAL_NOT
;
1591 t1
->right
= LTTV_TREE_NODE
;
1596 if(subtree
!= NULL
) { /* add the subtree */
1597 t1
->right
= LTTV_TREE_NODE
;
1598 t1
->r_child
.t
= subtree
;
1600 } else { /* add a leaf */
1601 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1602 a_field_component
= NULL
;
1603 g_string_free(a_string_spaces
, TRUE
);
1604 a_string_spaces
= NULL
;
1605 t1
->right
= LTTV_TREE_LEAF
;
1606 t1
->r_child
.leaf
= a_simple_expression
;
1607 a_simple_expression
= NULL
;
1611 /* free the pointer array */
1612 g_assert(a_field_path
->len
== 0);
1613 g_ptr_array_free(a_field_path
,TRUE
);
1615 /* free the tree stack -- but keep the root tree */
1616 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1617 g_ptr_array_free(tree_stack
,TRUE
);
1619 /* free the field buffer if allocated */
1620 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1621 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1623 /* free the simple expression buffer if allocated */
1624 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1626 g_assert(filter
->head
!= NULL
); /* tree should exist */
1627 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1630 gettimeofday(&endtime
, NULL
);
1632 /* Calcul du temps de l'algorithme */
1633 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1634 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1635 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1636 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1640 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1641 lttv_print_tree(filter
->head
,0) ;
1642 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1650 * @fn void lttv_filter_destroy(LttvFilter*)
1652 * Destroy the current LttvFilter
1653 * @param filter pointer to the current LttvFilter
1656 lttv_filter_destroy(LttvFilter
* filter
) {
1660 if(filter
->expression
)
1661 g_free(filter
->expression
);
1663 lttv_filter_tree_destroy(filter
->head
);
1669 * @fn LttvFilterTree* lttv_filter_tree_new()
1671 * Assign a new tree for the current expression
1673 * @return pointer of LttvFilterTree
1676 lttv_filter_tree_new() {
1677 LttvFilterTree
* tree
;
1679 tree
= g_new(LttvFilterTree
,1);
1680 tree
->node
= 0; //g_new(lttv_expression,1);
1681 tree
->left
= LTTV_TREE_IDLE
;
1682 tree
->right
= LTTV_TREE_IDLE
;
1683 tree
->r_child
.t
= NULL
;
1684 tree
->l_child
.t
= NULL
;
1690 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1692 * Append a new expression to the expression
1693 * defined in the current filter
1694 * @param filter pointer to the current LttvFilter
1695 * @param expression string that must be appended
1696 * @return Success/Failure of operation
1699 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1701 if(expression
== NULL
) return FALSE
;
1702 if(filter
== NULL
) return FALSE
;
1703 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1705 GString
* s
= g_string_new("");
1706 if(filter
->expression
!= NULL
) {
1707 g_string_append(s
,filter
->expression
);
1708 g_string_append_c(s
,'&');
1710 g_string_append(s
,expression
);
1712 g_free(filter
->expression
);
1713 filter
->expression
= g_string_free(s
,FALSE
);
1715 /* TRUE if construction of tree proceeded without errors */
1716 return lttv_filter_update(filter
);
1721 * @fn void lttv_filter_clear_expression(LttvFilter*)
1723 * Clear the filter expression from the
1724 * current filter and sets its pointer to NULL
1725 * @param filter pointer to the current LttvFilter
1728 lttv_filter_clear_expression(LttvFilter
* filter
) {
1730 if(filter
->expression
!= NULL
) {
1731 g_free(filter
->expression
);
1732 filter
->expression
= NULL
;
1738 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1740 * Destroys the tree and his sub-trees
1741 * @param tree Tree which must be destroyed
1744 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1746 if(tree
== NULL
) return;
1748 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1749 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1751 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1752 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1754 // g_free(tree->node);
1759 * Global parsing function for the current
1761 * @param t pointer to the current LttvFilterTree
1762 * @param event current LttEvent, NULL if not used
1763 * @param tracefile current LttTracefile, NULL if not used
1764 * @param trace current LttTrace, NULL if not used
1765 * @param state current LttvProcessState, NULL if not used
1766 * @param context current LttvTracefileContext, NULL if not used
1767 * @return response of filter
1770 lttv_filter_tree_parse(
1771 const LttvFilterTree
* t
,
1772 const LttEvent
* event
,
1773 const LttTracefile
* tracefile
,
1774 const LttTrace
* trace
,
1775 const LttvTracefileContext
* context
1780 * Each tree is parsed in inorder.
1781 * This way, it's possible to apply the left filter of the
1782 * tree, then decide whether or not the right branch should
1783 * be parsed depending on the linking logical operator
1785 * Each node consists in a
1786 * 1. logical operator
1787 * 2. left child ( node or simple expression )
1788 * 3. right child ( node or simple expression )
1790 * When the child is a simple expression, we must
1791 * before all determine if the expression refers to
1792 * a structure which is whithin observation ( not NULL ).
1793 * -If so, the expression is evaluated.
1794 * -If not, the result is set to TRUE since this particular
1795 * operation does not interfere with the lttv structure
1797 * The result of each simple expression will directly
1798 * affect the next branch. This way, depending on
1799 * the linking logical operator, the parser will decide
1800 * to explore or not the next branch.
1802 * -If result of left branch is 0 / FALSE
1803 * then don't explore right branch and return 0;
1804 * -If result of left branch is 1 / TRUE then explore
1806 * -If result of left branch is 1 / TRUE
1807 * then don't explore right branch and return 1;
1808 * -If result of left branch is 0 / FALSE then explore
1810 * -Result of left branch will not affect exploration of
1814 gboolean lresult
= FALSE
, rresult
= FALSE
;
1816 LttvProcessState
* state
;
1818 guint cpu
= ltt_tracefile_num(context
->tf
);
1819 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1820 state
= ts
->running_process
[cpu
];
1825 if(t
->left
== LTTV_TREE_NODE
) {
1826 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1828 else if(t
->left
== LTTV_TREE_LEAF
) {
1829 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1833 * Parse linking operator
1834 * make a cutoff if possible
1836 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1837 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1840 * Parse right branch
1842 if(t
->right
== LTTV_TREE_NODE
) {
1843 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1845 else if(t
->right
== LTTV_TREE_LEAF
) {
1846 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1851 * Apply and return the
1852 * logical link between the
1856 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1857 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1858 case LTTV_LOGICAL_NOT
:
1859 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1860 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1861 case 0: return (rresult
);
1864 * This case should never be
1865 * parsed, if so, this subtree
1874 * This function parses a particular branch of the tree
1875 * @param se pointer to the current LttvSimpleExpression
1876 * @param event current LttEvent, NULL if not used
1877 * @param tracefile current LttTracefile, NULL if not used
1878 * @param trace current LttTrace, NULL if not used
1879 * @param state current LttvProcessState, NULL if not used
1880 * @param context current LttvTracefileContext, NULL if not used
1881 * @return response of filter
1884 lttv_filter_tree_parse_branch(
1885 const LttvSimpleExpression
* se
,
1886 const LttEvent
* event
,
1887 const LttTracefile
* tracefile
,
1888 const LttTrace
* trace
,
1889 const LttvProcessState
* state
,
1890 const LttvTracefileContext
* context
) {
1895 case LTTV_FILTER_TRACE_NAME
:
1896 if(trace
== NULL
) return TRUE
;
1898 GQuark quark
= ltt_trace_name(trace
);
1899 return se
->op((gpointer
)&quark
,v
);
1902 case LTTV_FILTER_TRACEFILE_NAME
:
1903 if(tracefile
== NULL
) return TRUE
;
1905 GQuark quark
= ltt_tracefile_name(tracefile
);
1906 return se
->op((gpointer
)&quark
,v
);
1909 case LTTV_FILTER_STATE_PID
:
1910 if(state
== NULL
) return TRUE
;
1911 else return se
->op((gpointer
)&state
->pid
,v
);
1913 case LTTV_FILTER_STATE_PPID
:
1914 if(state
== NULL
) return TRUE
;
1915 else return se
->op((gpointer
)&state
->ppid
,v
);
1917 case LTTV_FILTER_STATE_CT
:
1918 if(state
== NULL
) return TRUE
;
1920 return se
->op((gpointer
)&state
->creation_time
,v
);
1923 case LTTV_FILTER_STATE_IT
:
1924 if(state
== NULL
) return TRUE
;
1926 return se
->op((gpointer
)&state
->insertion_time
,v
);
1929 case LTTV_FILTER_STATE_P_NAME
:
1931 * All 'unnamed' for the moment
1933 if(state
== NULL
) return TRUE
;
1935 GQuark quark
= state
->name
;
1936 return se
->op((gpointer
)&quark
,v
);
1939 case LTTV_FILTER_STATE_EX_MODE
:
1940 if(state
== NULL
) return TRUE
;
1941 else return se
->op((gpointer
)&state
->state
->t
,v
);
1943 case LTTV_FILTER_STATE_EX_SUBMODE
:
1944 if(state
== NULL
) return TRUE
;
1945 else return se
->op((gpointer
)&state
->state
->n
,v
);
1947 case LTTV_FILTER_STATE_P_STATUS
:
1948 if(state
== NULL
) return TRUE
;
1949 else return se
->op((gpointer
)&state
->state
->s
,v
);
1951 case LTTV_FILTER_STATE_CPU
:
1952 if(context
== NULL
) return TRUE
;
1954 /* FIXME: not sure of that one Mathieu : fixed.*/
1955 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1956 // return se->op((gpointer)&quark,v);
1957 if(state
== NULL
) return TRUE
;
1958 else return se
->op((gpointer
)&state
->cpu
,v
);
1961 case LTTV_FILTER_EVENT_NAME
:
1962 if(event
== NULL
) return TRUE
;
1965 et
= ltt_event_eventtype(event
);
1966 GQuark quark
= ltt_eventtype_name(et
);
1967 return se
->op((gpointer
)&quark
,v
);
1970 case LTTV_FILTER_EVENT_FACILITY
:
1971 if(event
== NULL
) return TRUE
;
1974 fac
= ltt_event_facility(event
);
1975 GQuark quark
= ltt_facility_name(fac
);
1976 return se
->op((gpointer
)&quark
,v
);
1979 case LTTV_FILTER_EVENT_CATEGORY
:
1981 * TODO: Not yet implemented
1985 case LTTV_FILTER_EVENT_TIME
:
1986 if(event
== NULL
) return TRUE
;
1988 LttTime time
= ltt_event_time(event
);
1989 return se
->op((gpointer
)&time
,v
);
1992 case LTTV_FILTER_EVENT_TSC
:
1993 if(event
== NULL
) return TRUE
;
1995 LttCycleCount count
= ltt_event_cycle_count(event
);
1996 return se
->op((gpointer
)&count
,v
);
1999 case LTTV_FILTER_EVENT_FIELD
:
2001 * TODO: Use the offset to
2002 * find the dynamic field
2003 * in the event struct
2008 * This case should never be
2009 * parsed, if so, the whole
2010 * filtering is cancelled
2012 g_warning("Error while parsing the filter tree");
2016 /* should never get here */
2024 * Debug function. Prints tree memory allocation.
2025 * @param t the pointer to the current LttvFilterTree
2028 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2030 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2031 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2032 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2034 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2035 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"));
2036 if(t
->left
== LTTV_TREE_LEAF
) {
2037 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2038 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2039 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2041 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"));
2042 if(t
->right
== LTTV_TREE_LEAF
) {
2043 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2044 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2045 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2048 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2049 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2053 * @fn static void module_init()
2055 * Initializes the filter module and specific values
2057 static void module_init()
2063 * Destroys the filter module and specific values
2065 static void module_destroy()
2071 LTTV_MODULE("filter", "Filters traceset and events", \
2072 "Filters traceset and events specifically to user input", \
2073 module_init
, module_destroy
)