1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 /*! \file lttv/lttv/filter.c
20 * \brief Defines the core filter of application
22 * consist in AND, OR and NOT nested expressions, forming a tree with
23 * simple relations as leaves. The simple relations test if a field
24 * in an event is equal, not equal, smaller, smaller or equal, larger, or
25 * larger or equal to a specified value.
27 * Fields specified in a simple expression can take following
31 * LttvTracefileContext{}
33 * | |->name (String, converted to GQuark)
34 * | |->category (String, not yet implemented)
36 * | |->tsc (LttCycleCount --> uint64)
40 * | |->"sub-field name"
42 * | |->"leaf-field name" (field type)
44 * | |->name (String, converted to GQuark)
46 * | |->name (String, converted to GQuark)
50 * |->creation_time (LttTime)
51 * |->insertion_time (LttTime)
52 * |->process_name (String, converted to GQuark)
53 * |->execution_mode (LttvExecutionMode)
54 * |->execution_submode (LttvExecutionSubmode)
55 * |->process_status (LttvProcessStatus)
62 * - refine switch of expression in multiple uses functions
63 * - remove the idle expressions in the tree
76 #include <lttv/filter.h>
77 #include <ltt/trace.h>
83 * @fn LttvSimpleExpression* lttv_simple_expression_new()
85 * Constructor for LttvSimpleExpression
86 * @return pointer to new LttvSimpleExpression
89 lttv_simple_expression_new() {
91 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
93 se
->field
= LTTV_FILTER_UNDEFINED
;
101 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
103 * Parse through filtering field hierarchy as specified
104 * by user. This function compares each value to
105 * predetermined quarks
106 * @param fp The field path list
107 * @param se current simple expression
108 * @return success/failure of operation
111 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
115 if(fp
->len
< 2) return FALSE
;
116 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
119 * Parse through the specified
122 * Take note however that the
123 * 'event' subfields might change
124 * depending on values specified
125 * in core.xml file. Hence, if
126 * none of the subfields in the
127 * array match the hardcoded
128 * subfields, it will be considered
131 if(!g_strcasecmp(f
->str
,"trace") ) {
136 g_string_free(f
,TRUE
);
137 f
=g_ptr_array_remove_index(fp
,0);
138 if(!g_strcasecmp(f
->str
,"name")) {
139 se
->field
= LTTV_FILTER_TRACE_NAME
;
141 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
143 * FIXME: not yet implemented !
145 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
150 g_string_free(f
,TRUE
);
151 f
=g_ptr_array_remove_index(fp
,0);
152 if(!g_strcasecmp(f
->str
,"name")) {
153 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
155 } else if(!g_strcasecmp(f
->str
,"state") ) {
160 * state.creation_time
161 * state.insertion_time
163 * state.execution_mode
164 * state.execution_submode
165 * state.process_status
168 g_string_free(f
,TRUE
);
169 f
=g_ptr_array_remove_index(fp
,0);
170 if(!g_strcasecmp(f
->str
,"pid") ) {
171 se
->field
= LTTV_FILTER_STATE_PID
;
173 else if(!g_strcasecmp(f
->str
,"ppid") ) {
174 se
->field
= LTTV_FILTER_STATE_PPID
;
176 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
177 se
->field
= LTTV_FILTER_STATE_CT
;
179 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
180 se
->field
= LTTV_FILTER_STATE_IT
;
182 else if(!g_strcasecmp(f
->str
,"process_name") ) {
183 se
->field
= LTTV_FILTER_STATE_P_NAME
;
185 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
186 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
188 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
189 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
191 else if(!g_strcasecmp(f
->str
,"process_status") ) {
192 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
194 else if(!g_strcasecmp(f
->str
,"cpu") ) {
195 se
->field
= LTTV_FILTER_STATE_CPU
;
197 } else if(!g_strcasecmp(f
->str
,"event") ) {
205 g_string_free(f
,TRUE
);
206 f
=g_ptr_array_remove_index(fp
,0);
207 if(!g_strcasecmp(f
->str
,"name") ) {
208 se
->field
= LTTV_FILTER_EVENT_NAME
;
210 else if(!g_strcasecmp(f
->str
,"category") ) {
212 * FIXME: Category not yet functional in lttv
214 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
216 else if(!g_strcasecmp(f
->str
,"time") ) {
217 se
->field
= LTTV_FILTER_EVENT_TIME
;
219 else if(!g_strcasecmp(f
->str
,"tsc") ) {
220 se
->field
= LTTV_FILTER_EVENT_TSC
;
222 else { /* core.xml specified options */
223 se
->field
= LTTV_FILTER_EVENT_FIELD
;
226 g_warning("Unrecognized field in filter string");
229 /* free memory for last string */
230 g_string_free(f
,TRUE
);
232 /* array should be empty */
233 g_assert(fp
->len
== 0);
235 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
236 g_warning("The specified field was not recognized !");
243 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
245 * Sets the function pointer for the current
247 * @param se current simple expression
248 * @param op current operator
249 * @return success/failure of operation
252 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
258 case LTTV_FILTER_TRACE_NAME
:
259 case LTTV_FILTER_TRACEFILE_NAME
:
260 case LTTV_FILTER_STATE_P_NAME
:
261 case LTTV_FILTER_EVENT_NAME
:
262 case LTTV_FILTER_STATE_CPU
:
263 case LTTV_FILTER_STATE_EX_MODE
:
264 case LTTV_FILTER_STATE_EX_SUBMODE
:
265 case LTTV_FILTER_STATE_P_STATUS
:
268 se
->op
= lttv_apply_op_eq_quark
;
271 se
->op
= lttv_apply_op_ne_quark
;
274 g_warning("Error encountered in operator assignment = or != expected");
281 case LTTV_FILTER_STATE_PID
:
282 case LTTV_FILTER_STATE_PPID
:
283 case LTTV_FILTER_EVENT_TSC
:
286 se
->op
= lttv_apply_op_eq_uint64
;
289 se
->op
= lttv_apply_op_ne_uint64
;
292 se
->op
= lttv_apply_op_lt_uint64
;
295 se
->op
= lttv_apply_op_le_uint64
;
298 se
->op
= lttv_apply_op_gt_uint64
;
301 se
->op
= lttv_apply_op_ge_uint64
;
304 g_warning("Error encountered in operator assignment");
310 * Entered as string, converted to enum
312 * can only be compared with 'equal' or 'not equal' operators
314 * unsigned int of 16 bits are used here since enums
315 * should not go over 2^16-1 values
319 // case LTTV_FIELD_EQ:
320 // se->op = lttv_apply_op_eq_uint16;
322 // case LTTV_FIELD_NE:
323 // se->op = lttv_apply_op_ne_uint16;
326 // g_warning("Error encountered in operator assignment = or != expected");
333 case LTTV_FILTER_STATE_CT
:
334 case LTTV_FILTER_STATE_IT
:
335 case LTTV_FILTER_EVENT_TIME
:
338 se
->op
= lttv_apply_op_eq_ltttime
;
341 se
->op
= lttv_apply_op_ne_ltttime
;
344 se
->op
= lttv_apply_op_lt_ltttime
;
347 se
->op
= lttv_apply_op_le_ltttime
;
350 se
->op
= lttv_apply_op_gt_ltttime
;
353 se
->op
= lttv_apply_op_ge_ltttime
;
356 g_warning("Error encountered in operator assignment");
361 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
370 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
372 * Assign the value field to the current LttvSimpleExpression
373 * @param se pointer to the current LttvSimpleExpression
374 * @param value string value for simple expression
377 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
380 gboolean is_double
= FALSE
;
381 LttTime t
= ltt_time_zero
;
387 * entered as strings, converted to Quarks
389 case LTTV_FILTER_TRACE_NAME
:
390 case LTTV_FILTER_TRACEFILE_NAME
:
391 case LTTV_FILTER_STATE_P_NAME
:
392 case LTTV_FILTER_EVENT_NAME
:
393 case LTTV_FILTER_STATE_CPU
:
394 case LTTV_FILTER_STATE_EX_MODE
:
395 case LTTV_FILTER_STATE_EX_SUBMODE
:
396 case LTTV_FILTER_STATE_P_STATUS
:
397 // se->value.v_string = value;
398 se
->value
.v_uint32
= g_quark_from_string(value
);
402 * integer -- supposed to be uint64
404 case LTTV_FILTER_STATE_PID
:
405 case LTTV_FILTER_STATE_PPID
:
406 case LTTV_FILTER_EVENT_TSC
:
407 se
->value
.v_uint64
= atoi(value
);
413 case LTTV_FILTER_STATE_CT
:
414 case LTTV_FILTER_STATE_IT
:
415 case LTTV_FILTER_EVENT_TIME
:
416 //se->value.v_double = atof(value);
418 * parsing logic could be optimised,
419 * but as for now, simpler this way
421 v
= g_string_new("");
422 for(i
=0;i
<strlen(value
);i
++) {
423 if(value
[i
] == '.') {
424 /* cannot specify number with more than one '.' */
425 if(is_double
) return FALSE
;
426 else is_double
= TRUE
;
427 t
.tv_sec
= atoi(v
->str
);
428 g_string_free(v
,TRUE
);
429 v
= g_string_new("");
430 } else g_string_append_c(v
,value
[i
]);
432 /* number can be integer or double */
433 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
434 else t
.tv_sec
= atoi(v
->str
);
436 g_string_free(v
,TRUE
);
438 se
->value
.v_ltttime
= t
;
442 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
452 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
454 * Disallocate memory for the current
456 * @param se pointer to the current LttvSimpleExpression
459 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
461 // g_free(se->value);
462 // switch(se->field) {
463 // case LTTV_FILTER_TRACE_NAME:
464 // case LTTV_FILTER_TRACEFILE_NAME:
465 // case LTTV_FILTER_STATE_P_NAME:
466 // case LTTV_FILTER_EVENT_NAME:
467 // g_free(se->value.v_string);
475 * @fn gint lttv_struct_type(gint)
477 * Finds the structure type depending
478 * on the fields in parameters
479 * @params ft Field of the current structure
480 * @return LttvStructType enum or -1 for error
483 lttv_struct_type(gint ft
) {
486 case LTTV_FILTER_TRACE_NAME
:
487 return LTTV_FILTER_TRACE
;
489 case LTTV_FILTER_TRACEFILE_NAME
:
490 return LTTV_FILTER_TRACEFILE
;
492 case LTTV_FILTER_STATE_PID
:
493 case LTTV_FILTER_STATE_PPID
:
494 case LTTV_FILTER_STATE_CT
:
495 case LTTV_FILTER_STATE_IT
:
496 case LTTV_FILTER_STATE_P_NAME
:
497 case LTTV_FILTER_STATE_EX_MODE
:
498 case LTTV_FILTER_STATE_EX_SUBMODE
:
499 case LTTV_FILTER_STATE_P_STATUS
:
500 case LTTV_FILTER_STATE_CPU
:
501 return LTTV_FILTER_STATE
;
503 case LTTV_FILTER_EVENT_NAME
:
504 case LTTV_FILTER_EVENT_CATEGORY
:
505 case LTTV_FILTER_EVENT_TIME
:
506 case LTTV_FILTER_EVENT_TSC
:
507 case LTTV_FILTER_EVENT_FIELD
:
508 return LTTV_FILTER_EVENT
;
516 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
518 * Applies the 'equal' operator to the
519 * specified structure and value
520 * @param v1 left member of comparison
521 * @param v2 right member of comparison
522 * @return success/failure of operation
524 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
526 guint64
* r
= (guint64
*) v1
;
527 return (*r
== v2
.v_uint64
);
532 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
534 * Applies the 'equal' operator to the
535 * specified structure and value
536 * @param v1 left member of comparison
537 * @param v2 right member of comparison
538 * @return success/failure of operation
540 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
541 guint32
* r
= (guint32
*) v1
;
542 return (*r
== v2
.v_uint32
);
546 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
548 * Applies the 'equal' operator to the
549 * specified structure and value
550 * @param v1 left member of comparison
551 * @param v2 right member of comparison
552 * @return success/failure of operation
554 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
555 guint16
* r
= (guint16
*) v1
;
556 return (*r
== v2
.v_uint16
);
560 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
562 * Applies the 'equal' operator to the
563 * specified structure and value
564 * @param v1 left member of comparison
565 * @param v2 right member of comparison
566 * @return success/failure of operation
568 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
569 double* r
= (double*) v1
;
570 return (*r
== v2
.v_double
);
574 * @fn gboolean lttv_apply_op_eq_string(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_string(const gpointer v1
, LttvFieldValue v2
) {
583 char* r
= (char*) v1
;
584 return (!g_strcasecmp(r
,v2
.v_string
));
588 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
590 * Applies the 'equal' operator to the
591 * specified structure and value
592 * @param v1 left member of comparison
593 * @param v2 right member of comparison
594 * @return success/failure of operation
596 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
597 GQuark
* r
= (GQuark
*) v1
;
598 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
599 return (*r
== v2
.v_uint32
);
603 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
605 * Applies the 'equal' operator to the
606 * specified structure and value
607 * @param v1 left member of comparison
608 * @param v2 right member of comparison
609 * @return success/failure of operation
611 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
612 LttTime
* r
= (LttTime
*) v1
;
613 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
618 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
620 * Applies the 'not 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_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
627 guint64
* r
= (guint64
*) v1
;
628 return (*r
!= v2
.v_uint64
);
632 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
634 * Applies the 'not 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_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
641 guint32
* r
= (guint32
*) v1
;
642 return (*r
!= v2
.v_uint32
);
646 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
648 * Applies the 'not 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_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
655 guint16
* r
= (guint16
*) v1
;
656 return (*r
!= v2
.v_uint16
);
660 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
662 * Applies the 'not equal' operator to the
663 * specified structure and value
664 * @param v1 left member of comparison
665 * @param v2 right member of comparison
666 * @return success/failure of operation
668 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
669 double* r
= (double*) v1
;
670 return (*r
!= v2
.v_double
);
674 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
676 * Applies the 'not equal' operator to the
677 * specified structure and value
678 * @param v1 left member of comparison
679 * @param v2 right member of comparison
680 * @return success/failure of operation
682 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
683 char* r
= (char*) v1
;
684 return (g_strcasecmp(r
,v2
.v_string
));
688 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
690 * Applies the 'not equal' operator to the
691 * specified structure and value
692 * @param v1 left member of comparison
693 * @param v2 right member of comparison
694 * @return success/failure of operation
696 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
697 GQuark
* r
= (GQuark
*) v1
;
698 return (*r
!= v2
.v_uint32
);
703 * @fn gboolean lttv_apply_op_ne_ltttime(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_ltttime(const gpointer v1
, LttvFieldValue v2
) {
712 LttTime
* r
= (LttTime
*) v1
;
713 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
718 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
720 * Applies the 'lower than' operator to the
721 * specified structure and value
722 * @param v1 left member of comparison
723 * @param v2 right member of comparison
724 * @return success/failure of operation
726 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
727 guint64
* r
= (guint64
*) v1
;
728 return (*r
< v2
.v_uint64
);
732 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
734 * Applies the 'lower than' operator to the
735 * specified structure and value
736 * @param v1 left member of comparison
737 * @param v2 right member of comparison
738 * @return success/failure of operation
740 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
741 guint32
* r
= (guint32
*) v1
;
742 return (*r
< v2
.v_uint32
);
746 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
748 * Applies the 'lower than' operator to the
749 * specified structure and value
750 * @param v1 left member of comparison
751 * @param v2 right member of comparison
752 * @return success/failure of operation
754 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
755 guint16
* r
= (guint16
*) v1
;
756 return (*r
< v2
.v_uint16
);
760 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
762 * Applies the 'lower than' operator to the
763 * specified structure and value
764 * @param v1 left member of comparison
765 * @param v2 right member of comparison
766 * @return success/failure of operation
768 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
769 double* r
= (double*) v1
;
770 return (*r
< v2
.v_double
);
774 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
776 * Applies the 'lower than' 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_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
783 LttTime
* r
= (LttTime
*) v1
;
784 // 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)));
785 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
790 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
792 * Applies the 'lower or equal' operator to the
793 * specified structure and value
794 * @param v1 left member of comparison
795 * @param v2 right member of comparison
796 * @return success/failure of operation
798 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
799 guint64
* r
= (guint64
*) v1
;
800 return (*r
<= v2
.v_uint64
);
804 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
806 * Applies the 'lower or equal' operator to the
807 * specified structure and value
808 * @param v1 left member of comparison
809 * @param v2 right member of comparison
810 * @return success/failure of operation
812 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
813 guint32
* r
= (guint32
*) v1
;
814 return (*r
<= v2
.v_uint32
);
818 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
820 * Applies the 'lower or equal' operator to the
821 * specified structure and value
822 * @param v1 left member of comparison
823 * @param v2 right member of comparison
824 * @return success/failure of operation
826 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
827 guint16
* r
= (guint16
*) v1
;
828 return (*r
<= v2
.v_uint16
);
832 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
834 * Applies the 'lower or equal' operator to the
835 * specified structure and value
836 * @param v1 left member of comparison
837 * @param v2 right member of comparison
838 * @return success/failure of operation
840 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
841 double* r
= (double*) v1
;
842 return (*r
<= v2
.v_double
);
846 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
848 * Applies the 'lower or equal' operator to the
849 * specified structure and value
850 * @param v1 left member of comparison
851 * @param v2 right member of comparison
852 * @return success/failure of operation
854 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
855 LttTime
* r
= (LttTime
*) v1
;
856 // 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)));
857 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
862 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
864 * Applies the 'greater 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_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
871 guint64
* r
= (guint64
*) v1
;
872 return (*r
> v2
.v_uint64
);
876 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
878 * Applies the 'greater than' operator to the
879 * specified structure and value
880 * @param v1 left member of comparison
881 * @param v2 right member of comparison
882 * @return success/failure of operation
884 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
885 guint32
* r
= (guint32
*) v1
;
886 return (*r
> v2
.v_uint32
);
890 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
892 * Applies the 'greater than' operator to the
893 * specified structure and value
894 * @param v1 left member of comparison
895 * @param v2 right member of comparison
896 * @return success/failure of operation
898 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
899 guint16
* r
= (guint16
*) v1
;
900 return (*r
> v2
.v_uint16
);
904 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
906 * Applies the 'greater than' operator to the
907 * specified structure and value
908 * @param v1 left member of comparison
909 * @param v2 right member of comparison
910 * @return success/failure of operation
912 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
913 double* r
= (double*) v1
;
914 return (*r
> v2
.v_double
);
918 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
920 * Applies the 'greater than' operator to the
921 * specified structure and value
922 * @param v1 left member of comparison
923 * @param v2 right member of comparison
924 * @return success/failure of operation
926 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
927 LttTime
* r
= (LttTime
*) v1
;
928 // 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)));
929 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
934 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
936 * Applies the 'greater or equal' operator to the
937 * specified structure and value
938 * @param v1 left member of comparison
939 * @param v2 right member of comparison
940 * @return success/failure of operation
942 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
943 guint64
* r
= (guint64
*) v1
;
944 return (*r
>= v2
.v_uint64
);
948 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
950 * Applies the 'greater or equal' operator to the
951 * specified structure and value
952 * @param v1 left member of comparison
953 * @param v2 right member of comparison
954 * @return success/failure of operation
956 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
957 guint32
* r
= (guint32
*) v1
;
958 return (*r
>= v2
.v_uint32
);
962 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
964 * Applies the 'greater or equal' operator to the
965 * specified structure and value
966 * @param v1 left member of comparison
967 * @param v2 right member of comparison
968 * @return success/failure of operation
970 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
971 guint16
* r
= (guint16
*) v1
;
972 return (*r
>= v2
.v_uint16
);
976 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
978 * Applies the 'greater or equal' operator to the
979 * specified structure and value
980 * @param v1 left member of comparison
981 * @param v2 right member of comparison
982 * @return success/failure of operation
984 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
985 double* r
= (double*) v1
;
986 return (*r
>= v2
.v_double
);
990 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
992 * Applies the 'greater or equal' operator to the
993 * specified structure and value
994 * @param v1 left member of comparison
995 * @param v2 right member of comparison
996 * @return success/failure of operation
998 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
999 LttTime
* r
= (LttTime
*) v1
;
1000 // 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)));
1001 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1007 * Makes a copy of the current filter tree
1008 * @param tree pointer to the current tree
1009 * @return new copy of the filter tree
1012 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1014 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1016 newtree
->node
= tree
->node
;
1018 newtree
->left
= tree
->left
;
1019 if(newtree
->left
== LTTV_TREE_NODE
) {
1020 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1021 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1022 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1023 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1024 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1025 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1026 /* FIXME: special case for string copy ! */
1027 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1030 newtree
->right
= tree
->right
;
1031 if(newtree
->right
== LTTV_TREE_NODE
) {
1032 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1033 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1034 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1035 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1036 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1037 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1038 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1046 * Makes a copy of the current filter
1047 * @param filter pointer to the current filter
1048 * @return new copy of the filter
1051 lttv_filter_clone(const LttvFilter
* filter
) {
1053 if(!filter
) return NULL
;
1055 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1057 strcpy(newfilter
->expression
,filter
->expression
);
1059 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1067 * @fn LttvFilter* lttv_filter_new()
1069 * Creates a new LttvFilter
1070 * @return the current LttvFilter or NULL if error
1075 LttvFilter
* filter
= g_new(LttvFilter
,1);
1076 filter
->expression
= NULL
;
1077 filter
->head
= NULL
;
1084 * @fn gboolean lttv_filter_update(LttvFilter*)
1086 * Updates the current LttvFilter by building
1087 * its tree based upon the expression string
1088 * @param filter pointer to the current LttvFilter
1089 * @return Failure/Success of operation
1092 lttv_filter_update(LttvFilter
* filter
) {
1094 // g_print("filter::lttv_filter_new()\n"); /* debug */
1096 if(filter
->expression
== NULL
) return FALSE
;
1100 p_nesting
=0, /* parenthesis nesting value */
1105 *tree
= lttv_filter_tree_new(), /* main tree */
1106 *subtree
= NULL
, /* buffer for subtrees */
1107 *t1
, /* buffer #1 */
1108 *t2
, /* buffer #2 */
1109 *t3
; /* buffer #3 */
1113 * If the tree already exists,
1114 * destroy it and build a new one
1116 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1117 filter
->head
= NULL
; /* will be assigned at the end */
1121 * each element of the list
1122 * is a sub tree created
1123 * by the use of parenthesis in the
1124 * global expression. The final tree
1125 * will be the one left at the root of
1128 GPtrArray
*tree_stack
= g_ptr_array_new();
1129 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1131 /* temporary values */
1132 GString
*a_field_component
= g_string_new("");
1133 GPtrArray
*a_field_path
= g_ptr_array_new();
1135 /* simple expression buffer */
1136 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1139 * Parse entire expression and construct
1140 * the binary tree. There are two steps
1141 * in browsing that string
1142 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1143 * 2. finding simple expressions
1144 * - field path ( separated by dots )
1145 * - op ( >, <, =, >=, <=, !=)
1146 * - value ( integer, string ... )
1147 * To spare computing time, the whole
1148 * string is parsed in this loop for a
1149 * O(n) complexity order.
1151 * When encountering logical op &,|,^
1152 * 1. parse the last value if any
1153 * 2. create a new tree
1154 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1155 * 4. concatenate this tree with the current tree on top of the stack
1156 * When encountering math ops >,>=,<,<=,=,!=
1157 * 1. add to op to the simple expression
1158 * 2. concatenate last field component to field path
1159 * When encountering concatening ops .
1160 * 1. concatenate last field component to field path
1161 * When encountering opening parenthesis (,{,[
1162 * 1. create a new subtree on top of tree stack
1163 * When encountering closing parenthesis ),},]
1164 * 1. add the expression on right child of the current tree
1165 * 2. the subtree is completed, allocate a new subtree
1166 * 3. pop the tree value from the tree stack
1170 struct timeval starttime
;
1171 struct timeval endtime
;
1172 gettimeofday(&starttime
, NULL
);
1175 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1177 // g_print("%c\n ",filter->expression[i]);
1179 switch(filter
->expression
[i
]) {
1185 /* get current tree in tree stack */
1186 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1188 /* get current node at absolute right */
1189 while(t1
->right
!= LTTV_TREE_IDLE
) {
1190 g_assert(t1
->right
== LTTV_TREE_NODE
);
1193 t2
= lttv_filter_tree_new();
1194 t2
->node
= LTTV_LOGICAL_AND
;
1195 t1
->right
= LTTV_TREE_NODE
;
1197 if(not) { /* add not operator to tree */
1198 t3
= lttv_filter_tree_new();
1199 t3
->node
= LTTV_LOGICAL_NOT
;
1200 t2
->left
= LTTV_TREE_NODE
;
1205 if(subtree
!= NULL
) { /* append subtree to current tree */
1206 t2
->left
= LTTV_TREE_NODE
;
1207 t2
->l_child
.t
= subtree
;
1209 } else { /* append a simple expression */
1210 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1211 a_field_component
= g_string_new("");
1212 t2
->left
= LTTV_TREE_LEAF
;
1213 t2
->l_child
.leaf
= a_simple_expression
;
1214 a_simple_expression
= lttv_simple_expression_new();
1220 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1221 while(t1
->right
!= LTTV_TREE_IDLE
) {
1222 g_assert(t1
->right
== LTTV_TREE_NODE
);
1225 t2
= lttv_filter_tree_new();
1226 t2
->node
= LTTV_LOGICAL_OR
;
1227 t1
->right
= LTTV_TREE_NODE
;
1229 if(not) { // add not operator to tree
1230 t3
= lttv_filter_tree_new();
1231 t3
->node
= LTTV_LOGICAL_NOT
;
1232 t2
->left
= LTTV_TREE_NODE
;
1237 if(subtree
!= NULL
) { /* append subtree to current tree */
1238 t2
->left
= LTTV_TREE_NODE
;
1239 t2
->l_child
.t
= subtree
;
1241 } else { /* append a simple expression */
1242 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1243 a_field_component
= g_string_new("");
1244 t2
->left
= LTTV_TREE_LEAF
;
1245 t2
->l_child
.leaf
= a_simple_expression
;
1246 a_simple_expression
= lttv_simple_expression_new();
1252 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1253 while(t1
->right
!= LTTV_TREE_IDLE
) {
1254 g_assert(t1
->right
== LTTV_TREE_NODE
);
1257 t2
= lttv_filter_tree_new();
1258 t2
->node
= LTTV_LOGICAL_XOR
;
1259 t1
->right
= LTTV_TREE_NODE
;
1261 if(not) { // add not operator to tree
1262 t3
= lttv_filter_tree_new();
1263 t3
->node
= LTTV_LOGICAL_NOT
;
1264 t2
->left
= LTTV_TREE_NODE
;
1269 if(subtree
!= NULL
) { /* append subtree to current tree */
1270 t2
->left
= LTTV_TREE_NODE
;
1271 t2
->l_child
.t
= subtree
;
1273 } else { /* append a simple expression */
1274 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1275 a_field_component
= g_string_new("");
1276 t2
->left
= LTTV_TREE_LEAF
;
1277 t2
->l_child
.leaf
= a_simple_expression
;
1278 a_simple_expression
= lttv_simple_expression_new();
1282 case '!': /* not, or not equal (math op) */
1284 if(filter
->expression
[i
+1] == '=') { /* != */
1285 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1286 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1287 a_field_component
= g_string_new("");
1288 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1295 case '(': /* start of parenthesis */
1299 p_nesting
++; /* incrementing parenthesis nesting value */
1300 t1
= lttv_filter_tree_new();
1301 if(not) { /* add not operator to tree */
1302 t3
= lttv_filter_tree_new();
1303 t3
->node
= LTTV_LOGICAL_NOT
;
1304 t1
->right
= LTTV_TREE_NODE
;
1308 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1311 case ')': /* end of parenthesis */
1315 p_nesting
--; /* decrementing parenthesis nesting value */
1316 if(p_nesting
<0 || tree_stack
->len
<2) {
1317 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1318 is not valid due to parenthesis incorrect use",filter
->expression
);
1322 /* there must at least be the root tree left in the array */
1323 g_assert(tree_stack
->len
>0);
1325 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1326 while(t1
->right
!= LTTV_TREE_IDLE
) {
1329 if(not) { // add not operator to tree
1331 t3
= lttv_filter_tree_new();
1332 t3
->node
= LTTV_LOGICAL_NOT
;
1333 t1
->right
= LTTV_TREE_NODE
;
1338 if(subtree
!= NULL
) { /* append subtree to current tree */
1339 t1
->right
= LTTV_TREE_NODE
;
1340 t1
->r_child
.t
= subtree
;
1341 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1342 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1343 } else { /* assign subtree as current tree */
1344 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1345 a_field_component
= g_string_new("");
1346 t1
->right
= LTTV_TREE_LEAF
;
1347 t1
->r_child
.leaf
= a_simple_expression
;
1348 a_simple_expression
= lttv_simple_expression_new();
1349 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1354 * mathematic operators
1356 case '<': /* lower, lower or equal */
1358 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1359 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1360 a_field_component
= g_string_new("");
1361 if(filter
->expression
[i
+1] == '=') { /* <= */
1363 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1364 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1367 case '>': /* higher, higher or equal */
1369 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1370 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1371 a_field_component
= g_string_new("");
1372 if(filter
->expression
[i
+1] == '=') { /* >= */
1374 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1375 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1378 case '=': /* equal */
1380 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1381 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1382 a_field_component
= g_string_new("");
1383 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1387 * Field concatening caracter
1392 * divide field expression into elements
1393 * in a_field_path array.
1395 * A dot can also be present in double values
1397 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1398 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1399 a_field_component
= g_string_new("");
1402 case ' ': /* ignore */
1403 case '\n': /* ignore */
1405 default: /* concatening current string */
1406 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1411 * Preliminary check to see
1412 * if tree was constructed correctly
1415 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1416 is not valid due to parenthesis incorrect use",filter
->expression
);
1420 if(tree_stack
->len
!= 1) /* only root tree should remain */
1424 * processing last element of expression
1426 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1427 while(t1
->right
!= LTTV_TREE_IDLE
) {
1428 g_assert(t1
->right
== LTTV_TREE_NODE
);
1431 if(not) { // add not operator to tree
1432 t3
= lttv_filter_tree_new();
1433 t3
->node
= LTTV_LOGICAL_NOT
;
1434 t1
->right
= LTTV_TREE_NODE
;
1439 if(subtree
!= NULL
) { /* add the subtree */
1440 t1
->right
= LTTV_TREE_NODE
;
1441 t1
->r_child
.t
= subtree
;
1443 } else { /* add a leaf */
1444 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1445 a_field_component
= NULL
;
1446 t1
->right
= LTTV_TREE_LEAF
;
1447 t1
->r_child
.leaf
= a_simple_expression
;
1448 a_simple_expression
= NULL
;
1452 /* free the pointer array */
1453 g_assert(a_field_path
->len
== 0);
1454 g_ptr_array_free(a_field_path
,TRUE
);
1456 /* free the tree stack -- but keep the root tree */
1457 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1458 g_ptr_array_free(tree_stack
,TRUE
);
1460 /* free the field buffer if allocated */
1461 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1463 /* free the simple expression buffer if allocated */
1464 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1466 g_assert(filter
->head
!= NULL
); /* tree should exist */
1467 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1470 gettimeofday(&endtime
, NULL
);
1472 /* Calcul du temps de l'algorithme */
1473 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1474 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1475 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1476 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1480 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1481 lttv_print_tree(filter
->head
,0) ;
1482 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1490 * @fn void lttv_filter_destroy(LttvFilter*)
1492 * Destroy the current LttvFilter
1493 * @param filter pointer to the current LttvFilter
1496 lttv_filter_destroy(LttvFilter
* filter
) {
1500 if(filter
->expression
)
1501 g_free(filter
->expression
);
1503 lttv_filter_tree_destroy(filter
->head
);
1509 * @fn LttvFilterTree* lttv_filter_tree_new()
1511 * Assign a new tree for the current expression
1513 * @return pointer of LttvFilterTree
1516 lttv_filter_tree_new() {
1517 LttvFilterTree
* tree
;
1519 tree
= g_new(LttvFilterTree
,1);
1520 tree
->node
= 0; //g_new(lttv_expression,1);
1521 tree
->left
= LTTV_TREE_IDLE
;
1522 tree
->right
= LTTV_TREE_IDLE
;
1523 tree
->r_child
.t
= NULL
;
1524 tree
->l_child
.t
= NULL
;
1530 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1532 * Append a new expression to the expression
1533 * defined in the current filter
1534 * @param filter pointer to the current LttvFilter
1535 * @param expression string that must be appended
1536 * @return Success/Failure of operation
1539 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1541 if(expression
== NULL
) return FALSE
;
1542 if(filter
== NULL
) return FALSE
;
1543 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1545 GString
* s
= g_string_new("");
1546 if(filter
->expression
!= NULL
) {
1547 g_string_append(s
,filter
->expression
);
1548 g_string_append_c(s
,'&');
1550 g_string_append(s
,expression
);
1552 g_free(filter
->expression
);
1553 filter
->expression
= g_string_free(s
,FALSE
);
1555 /* TRUE if construction of tree proceeded without errors */
1556 return lttv_filter_update(filter
);
1561 * @fn void lttv_filter_clear_expression(LttvFilter*)
1563 * Clear the filter expression from the
1564 * current filter and sets its pointer to NULL
1565 * @param filter pointer to the current LttvFilter
1568 lttv_filter_clear_expression(LttvFilter
* filter
) {
1570 if(filter
->expression
!= NULL
) {
1571 g_free(filter
->expression
);
1572 filter
->expression
= NULL
;
1578 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1580 * Destroys the tree and his sub-trees
1581 * @param tree Tree which must be destroyed
1584 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1586 if(tree
== NULL
) return;
1588 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1589 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1591 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1592 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1594 // g_free(tree->node);
1599 * Global parsing function for the current
1601 * @param t pointer to the current LttvFilterTree
1602 * @param event current LttEvent, NULL if not used
1603 * @param tracefile current LttTracefile, NULL if not used
1604 * @param trace current LttTrace, NULL if not used
1605 * @param state current LttvProcessState, NULL if not used
1606 * @param context current LttvTracefileContext, NULL if not used
1607 * @return response of filter
1610 lttv_filter_tree_parse(
1611 const LttvFilterTree
* t
,
1612 const LttEvent
* event
,
1613 const LttTracefile
* tracefile
,
1614 const LttTrace
* trace
,
1615 const LttvProcessState
* state
,
1616 const LttvTracefileContext
* context
1621 * Each tree is parsed in inorder.
1622 * This way, it's possible to apply the left filter of the
1623 * tree, then decide whether or not the right branch should
1624 * be parsed depending on the linking logical operator
1626 * Each node consists in a
1627 * 1. logical operator
1628 * 2. left child ( node or simple expression )
1629 * 3. right child ( node or simple expression )
1631 * When the child is a simple expression, we must
1632 * before all determine if the expression refers to
1633 * a structure which is whithin observation ( not NULL ).
1634 * -If so, the expression is evaluated.
1635 * -If not, the result is set to TRUE since this particular
1636 * operation does not interfere with the lttv structure
1638 * The result of each simple expression will directly
1639 * affect the next branch. This way, depending on
1640 * the linking logical operator, the parser will decide
1641 * to explore or not the next branch.
1643 * -If result of left branch is 0 / FALSE
1644 * then don't explore right branch and return 0;
1645 * -If result of left branch is 1 / TRUE then explore
1647 * -If result of left branch is 1 / TRUE
1648 * then don't explore right branch and return 1;
1649 * -If result of left branch is 0 / FALSE then explore
1651 * -Result of left branch will not affect exploration of
1655 gboolean lresult
= FALSE
, rresult
= FALSE
;
1660 if(t
->left
== LTTV_TREE_NODE
) {
1661 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,state
,context
);
1663 else if(t
->left
== LTTV_TREE_LEAF
) {
1664 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1668 * Parse linking operator
1669 * make a cutoff if possible
1671 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1672 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1675 * Parse right branch
1677 if(t
->right
== LTTV_TREE_NODE
) {
1678 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,state
,context
);
1680 else if(t
->right
== LTTV_TREE_LEAF
) {
1681 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1686 * Apply and return the
1687 * logical link between the
1691 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1692 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1693 case LTTV_LOGICAL_NOT
:
1694 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1695 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1696 case 0: return (rresult
);
1699 * This case should never be
1700 * parsed, if so, this subtree
1709 * This function parses a particular branch of the tree
1710 * @param se pointer to the current LttvSimpleExpression
1711 * @param event current LttEvent, NULL if not used
1712 * @param tracefile current LttTracefile, NULL if not used
1713 * @param trace current LttTrace, NULL if not used
1714 * @param state current LttvProcessState, NULL if not used
1715 * @param context current LttvTracefileContext, NULL if not used
1716 * @return response of filter
1719 lttv_filter_tree_parse_branch(
1720 const LttvSimpleExpression
* se
,
1721 const LttEvent
* event
,
1722 const LttTracefile
* tracefile
,
1723 const LttTrace
* trace
,
1724 const LttvProcessState
* state
,
1725 const LttvTracefileContext
* context
) {
1730 case LTTV_FILTER_TRACE_NAME
:
1731 if(trace
== NULL
) return TRUE
;
1733 GQuark quark
= ltt_trace_name(trace
);
1734 return se
->op((gpointer
)&quark
,v
);
1737 case LTTV_FILTER_TRACEFILE_NAME
:
1738 if(tracefile
== NULL
) return TRUE
;
1740 GQuark quark
= ltt_tracefile_name(tracefile
);
1741 return se
->op((gpointer
)&quark
,v
);
1744 case LTTV_FILTER_STATE_PID
:
1745 if(state
== NULL
) return TRUE
;
1746 else return se
->op((gpointer
)&state
->pid
,v
);
1748 case LTTV_FILTER_STATE_PPID
:
1749 if(state
== NULL
) return TRUE
;
1750 else return se
->op((gpointer
)&state
->ppid
,v
);
1752 case LTTV_FILTER_STATE_CT
:
1753 if(state
== NULL
) return TRUE
;
1755 return se
->op((gpointer
)&state
->creation_time
,v
);
1758 case LTTV_FILTER_STATE_IT
:
1759 if(state
== NULL
) return TRUE
;
1761 return se
->op((gpointer
)&state
->insertion_time
,v
);
1764 case LTTV_FILTER_STATE_P_NAME
:
1766 * All 'unnamed' for the moment
1768 if(state
== NULL
) return TRUE
;
1770 GQuark quark
= state
->name
;
1771 return se
->op((gpointer
)&quark
,v
);
1774 case LTTV_FILTER_STATE_EX_MODE
:
1775 if(state
== NULL
) return TRUE
;
1776 else return se
->op((gpointer
)&state
->state
->t
,v
);
1778 case LTTV_FILTER_STATE_EX_SUBMODE
:
1779 if(state
== NULL
) return TRUE
;
1780 else return se
->op((gpointer
)&state
->state
->n
,v
);
1782 case LTTV_FILTER_STATE_P_STATUS
:
1783 if(state
== NULL
) return TRUE
;
1784 else return se
->op((gpointer
)&state
->state
->s
,v
);
1786 case LTTV_FILTER_STATE_CPU
:
1787 if(context
== NULL
) return TRUE
;
1789 /* FIXME: not sure of that one Mathieu : fixed.*/
1790 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1791 // return se->op((gpointer)&quark,v);
1792 if(state
== NULL
) return TRUE
;
1793 else return se
->op((gpointer
)&state
->cpu
,v
);
1796 case LTTV_FILTER_EVENT_NAME
:
1797 if(event
== NULL
) return TRUE
;
1800 et
= ltt_event_eventtype(event
);
1801 GQuark quark
= ltt_eventtype_name(et
);
1802 return se
->op((gpointer
)&quark
,v
);
1806 case LTTV_FILTER_EVENT_CATEGORY
:
1808 * TODO: Not yet implemented
1812 case LTTV_FILTER_EVENT_TIME
:
1813 if(event
== NULL
) return TRUE
;
1815 LttTime time
= ltt_event_time(event
);
1816 return se
->op((gpointer
)&time
,v
);
1819 case LTTV_FILTER_EVENT_TSC
:
1820 if(event
== NULL
) return TRUE
;
1822 LttCycleCount count
= ltt_event_cycle_count(event
);
1823 return se
->op((gpointer
)&count
,v
);
1826 case LTTV_FILTER_EVENT_FIELD
:
1828 * TODO: Use the offset to
1829 * find the dynamic field
1830 * in the event struct
1835 * This case should never be
1836 * parsed, if so, the whole
1837 * filtering is cancelled
1839 g_warning("Error while parsing the filter tree");
1843 /* should never get here */
1851 * Debug function. Prints tree memory allocation.
1852 * @param t the pointer to the current LttvFilterTree
1855 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1857 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1858 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1859 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1861 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1862 g_print("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1863 if(t
->left
== LTTV_TREE_LEAF
) {
1864 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1865 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1866 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1868 g_print("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1869 if(t
->right
== LTTV_TREE_LEAF
) {
1870 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1871 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1872 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1875 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1876 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
1880 * @fn static void module_init()
1882 * Initializes the filter module and specific values
1884 static void module_init()
1890 * Destroys the filter module and specific values
1892 static void module_destroy()
1898 LTTV_MODULE("filter", "Filters traceset and events", \
1899 "Filters traceset and events specifically to user input", \
1900 module_init
, module_destroy
)