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)
42 * | |->"sub-field name"
44 * | |->"leaf-field name" (field type)
46 * | |->name (String, converted to GQuark)
48 * | |->name (String, converted to GQuark)
52 * |->creation_time (LttTime)
53 * |->insertion_time (LttTime)
54 * |->process_name (String, converted to GQuark)
55 * |->execution_mode (LttvExecutionMode)
56 * |->execution_submode (LttvExecutionSubmode)
57 * |->process_status (LttvProcessStatus)
64 * - refine switch of expression in multiple uses functions
65 * - remove the idle expressions in the tree
78 #include <lttv/lttv.h>
79 #include <lttv/filter.h>
80 #include <ltt/trace.h>
82 #include <ltt/facility.h>
87 * @fn LttvSimpleExpression* lttv_simple_expression_new()
89 * Constructor for LttvSimpleExpression
90 * @return pointer to new LttvSimpleExpression
93 lttv_simple_expression_new() {
95 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
97 se
->field
= LTTV_FILTER_UNDEFINED
;
105 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
107 * Parse through filtering field hierarchy as specified
108 * by user. This function compares each value to
109 * predetermined quarks
110 * @param fp The field path list
111 * @param se current simple expression
112 * @return success/failure of operation
115 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
119 if(fp
->len
< 2) return FALSE
;
120 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
123 * Parse through the specified
126 * Take note however that the
127 * 'event' subfields might change
128 * depending on values specified
129 * in core.xml file. Hence, if
130 * none of the subfields in the
131 * array match the hardcoded
132 * subfields, it will be considered
135 if(!g_strcasecmp(f
->str
,"trace") ) {
140 g_string_free(f
,TRUE
);
141 f
=g_ptr_array_remove_index(fp
,0);
142 if(!g_strcasecmp(f
->str
,"name")) {
143 se
->field
= LTTV_FILTER_TRACE_NAME
;
145 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
147 * FIXME: not yet implemented !
149 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
154 g_string_free(f
,TRUE
);
155 f
=g_ptr_array_remove_index(fp
,0);
156 if(!g_strcasecmp(f
->str
,"name")) {
157 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
159 } else if(!g_strcasecmp(f
->str
,"state") ) {
164 * state.creation_time
165 * state.insertion_time
167 * state.execution_mode
168 * state.execution_submode
169 * state.process_status
172 g_string_free(f
,TRUE
);
173 f
=g_ptr_array_remove_index(fp
,0);
174 if(!g_strcasecmp(f
->str
,"pid") ) {
175 se
->field
= LTTV_FILTER_STATE_PID
;
177 else if(!g_strcasecmp(f
->str
,"ppid") ) {
178 se
->field
= LTTV_FILTER_STATE_PPID
;
180 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
181 se
->field
= LTTV_FILTER_STATE_CT
;
183 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
184 se
->field
= LTTV_FILTER_STATE_IT
;
186 else if(!g_strcasecmp(f
->str
,"process_name") ) {
187 se
->field
= LTTV_FILTER_STATE_P_NAME
;
189 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
190 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
192 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
193 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
195 else if(!g_strcasecmp(f
->str
,"process_status") ) {
196 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
198 else if(!g_strcasecmp(f
->str
,"cpu") ) {
199 se
->field
= LTTV_FILTER_STATE_CPU
;
201 } else if(!g_strcasecmp(f
->str
,"event") ) {
210 g_string_free(f
,TRUE
);
211 f
=g_ptr_array_remove_index(fp
,0);
212 if(!g_strcasecmp(f
->str
,"name") ) {
213 se
->field
= LTTV_FILTER_EVENT_NAME
;
215 else if(!g_strcasecmp(f
->str
,"facility") ) {
216 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
218 else if(!g_strcasecmp(f
->str
,"category") ) {
220 * FIXME: Category not yet functional in lttv
222 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
224 else if(!g_strcasecmp(f
->str
,"time") ) {
225 se
->field
= LTTV_FILTER_EVENT_TIME
;
227 else if(!g_strcasecmp(f
->str
,"tsc") ) {
228 se
->field
= LTTV_FILTER_EVENT_TSC
;
230 else if(!g_strcasecmp(f
->str
,"field") ) {
231 se
->field
= LTTV_FILTER_EVENT_FIELD
;
234 g_warning("Unknown event filter subtype %s", f
->str
);
237 g_string_free(f
,TRUE
);
238 f
=g_ptr_array_remove_index(fp
,0);
240 g_warning("Unrecognized field in filter string");
243 /* free memory for last string */
244 g_string_free(f
,TRUE
);
246 /* array should be empty */
247 g_assert(fp
->len
== 0);
249 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
250 g_warning("The specified field was not recognized !");
257 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
259 * Sets the function pointer for the current
261 * @param se current simple expression
262 * @param op current operator
263 * @return success/failure of operation
266 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
272 case LTTV_FILTER_TRACE_NAME
:
273 case LTTV_FILTER_TRACEFILE_NAME
:
274 case LTTV_FILTER_STATE_P_NAME
:
275 case LTTV_FILTER_EVENT_NAME
:
276 case LTTV_FILTER_EVENT_FACILITY
:
277 case LTTV_FILTER_STATE_EX_MODE
:
278 case LTTV_FILTER_STATE_EX_SUBMODE
:
279 case LTTV_FILTER_STATE_P_STATUS
:
282 se
->op
= lttv_apply_op_eq_quark
;
285 se
->op
= lttv_apply_op_ne_quark
;
288 g_warning("Error encountered in operator assignment = or != expected");
295 case LTTV_FILTER_EVENT_TSC
:
298 se
->op
= lttv_apply_op_eq_uint64
;
301 se
->op
= lttv_apply_op_ne_uint64
;
304 se
->op
= lttv_apply_op_lt_uint64
;
307 se
->op
= lttv_apply_op_le_uint64
;
310 se
->op
= lttv_apply_op_gt_uint64
;
313 se
->op
= lttv_apply_op_ge_uint64
;
316 g_warning("Error encountered in operator assignment");
323 case LTTV_FILTER_STATE_CPU
:
324 case LTTV_FILTER_STATE_PID
:
325 case LTTV_FILTER_STATE_PPID
:
328 se
->op
= lttv_apply_op_eq_uint
;
331 se
->op
= lttv_apply_op_ne_uint
;
334 se
->op
= lttv_apply_op_lt_uint
;
337 se
->op
= lttv_apply_op_le_uint
;
340 se
->op
= lttv_apply_op_gt_uint
;
343 se
->op
= lttv_apply_op_ge_uint
;
346 g_warning("Error encountered in operator assignment");
353 * Entered as string, converted to enum
355 * can only be compared with 'equal' or 'not equal' operators
357 * unsigned int of 16 bits are used here since enums
358 * should not go over 2^16-1 values
362 // case LTTV_FIELD_EQ:
363 // se->op = lttv_apply_op_eq_uint16;
365 // case LTTV_FIELD_NE:
366 // se->op = lttv_apply_op_ne_uint16;
369 // g_warning("Error encountered in operator assignment = or != expected");
376 case LTTV_FILTER_STATE_CT
:
377 case LTTV_FILTER_STATE_IT
:
378 case LTTV_FILTER_EVENT_TIME
:
381 se
->op
= lttv_apply_op_eq_ltttime
;
384 se
->op
= lttv_apply_op_ne_ltttime
;
387 se
->op
= lttv_apply_op_lt_ltttime
;
390 se
->op
= lttv_apply_op_le_ltttime
;
393 se
->op
= lttv_apply_op_gt_ltttime
;
396 se
->op
= lttv_apply_op_ge_ltttime
;
399 g_warning("Error encountered in operator assignment");
404 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
413 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
415 * Assign the value field to the current LttvSimpleExpression
416 * @param se pointer to the current LttvSimpleExpression
417 * @param value string value for simple expression
420 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
423 gboolean is_double
= FALSE
;
424 LttTime t
= ltt_time_zero
;
430 * entered as strings, converted to Quarks
432 case LTTV_FILTER_TRACE_NAME
:
433 case LTTV_FILTER_TRACEFILE_NAME
:
434 case LTTV_FILTER_STATE_P_NAME
:
435 case LTTV_FILTER_EVENT_NAME
:
436 case LTTV_FILTER_EVENT_FACILITY
:
437 case LTTV_FILTER_STATE_EX_MODE
:
438 case LTTV_FILTER_STATE_EX_SUBMODE
:
439 case LTTV_FILTER_STATE_P_STATUS
:
440 // se->value.v_string = value;
441 se
->value
.v_quark
= g_quark_from_string(value
);
445 * integer -- supposed to be uint64
447 case LTTV_FILTER_EVENT_TSC
:
448 se
->value
.v_uint64
= atoi(value
);
454 case LTTV_FILTER_STATE_PID
:
455 case LTTV_FILTER_STATE_PPID
:
456 case LTTV_FILTER_STATE_CPU
:
457 se
->value
.v_uint
= atoi(value
);
463 case LTTV_FILTER_STATE_CT
:
464 case LTTV_FILTER_STATE_IT
:
465 case LTTV_FILTER_EVENT_TIME
:
466 //se->value.v_double = atof(value);
468 * parsing logic could be optimised,
469 * but as for now, simpler this way
471 v
= g_string_new("");
472 for(i
=0;i
<strlen(value
);i
++) {
473 if(value
[i
] == '.') {
474 /* cannot specify number with more than one '.' */
475 if(is_double
) return FALSE
;
476 else is_double
= TRUE
;
477 t
.tv_sec
= atoi(v
->str
);
478 g_string_free(v
,TRUE
);
479 v
= g_string_new("");
480 } else g_string_append_c(v
,value
[i
]);
482 /* number can be integer or double */
483 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
484 else t
.tv_sec
= atoi(v
->str
);
486 g_string_free(v
,TRUE
);
488 se
->value
.v_ltttime
= t
;
492 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
502 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
504 * Disallocate memory for the current
506 * @param se pointer to the current LttvSimpleExpression
509 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
511 // g_free(se->value);
512 // switch(se->field) {
513 // case LTTV_FILTER_TRACE_NAME:
514 // case LTTV_FILTER_TRACEFILE_NAME:
515 // case LTTV_FILTER_STATE_P_NAME:
516 // case LTTV_FILTER_EVENT_NAME:
517 // g_free(se->value.v_string);
525 * @fn gint lttv_struct_type(gint)
527 * Finds the structure type depending
528 * on the fields in parameters
529 * @params ft Field of the current structure
530 * @return LttvStructType enum or -1 for error
533 lttv_struct_type(gint ft
) {
536 case LTTV_FILTER_TRACE_NAME
:
537 return LTTV_FILTER_TRACE
;
539 case LTTV_FILTER_TRACEFILE_NAME
:
540 return LTTV_FILTER_TRACEFILE
;
542 case LTTV_FILTER_STATE_PID
:
543 case LTTV_FILTER_STATE_PPID
:
544 case LTTV_FILTER_STATE_CT
:
545 case LTTV_FILTER_STATE_IT
:
546 case LTTV_FILTER_STATE_P_NAME
:
547 case LTTV_FILTER_STATE_EX_MODE
:
548 case LTTV_FILTER_STATE_EX_SUBMODE
:
549 case LTTV_FILTER_STATE_P_STATUS
:
550 case LTTV_FILTER_STATE_CPU
:
551 return LTTV_FILTER_STATE
;
553 case LTTV_FILTER_EVENT_NAME
:
554 case LTTV_FILTER_EVENT_FACILITY
:
555 case LTTV_FILTER_EVENT_CATEGORY
:
556 case LTTV_FILTER_EVENT_TIME
:
557 case LTTV_FILTER_EVENT_TSC
:
558 case LTTV_FILTER_EVENT_FIELD
:
559 return LTTV_FILTER_EVENT
;
567 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
569 * Applies the 'equal' operator to the
570 * specified structure and value
571 * @param v1 left member of comparison
572 * @param v2 right member of comparison
573 * @return success/failure of operation
575 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
577 guint
* r
= (guint
*) v1
;
578 return (*r
== v2
.v_uint
);
583 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
585 * Applies the 'equal' operator to the
586 * specified structure and value
587 * @param v1 left member of comparison
588 * @param v2 right member of comparison
589 * @return success/failure of operation
591 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
593 guint64
* r
= (guint64
*) v1
;
594 return (*r
== v2
.v_uint64
);
599 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
601 * Applies the 'equal' operator to the
602 * specified structure and value
603 * @param v1 left member of comparison
604 * @param v2 right member of comparison
605 * @return success/failure of operation
607 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
608 guint32
* r
= (guint32
*) v1
;
609 return (*r
== v2
.v_uint32
);
613 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
615 * Applies the 'equal' operator to the
616 * specified structure and value
617 * @param v1 left member of comparison
618 * @param v2 right member of comparison
619 * @return success/failure of operation
621 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
622 guint16
* r
= (guint16
*) v1
;
623 return (*r
== v2
.v_uint16
);
627 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
629 * Applies the 'equal' operator to the
630 * specified structure and value
631 * @param v1 left member of comparison
632 * @param v2 right member of comparison
633 * @return success/failure of operation
635 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
636 double* r
= (double*) v1
;
637 return (*r
== v2
.v_double
);
641 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
643 * Applies the 'equal' operator to the
644 * specified structure and value
645 * @param v1 left member of comparison
646 * @param v2 right member of comparison
647 * @return success/failure of operation
649 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
650 char* r
= (char*) v1
;
651 return (!g_strcasecmp(r
,v2
.v_string
));
655 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
657 * Applies the 'equal' operator to the
658 * specified structure and value
659 * @param v1 left member of comparison
660 * @param v2 right member of comparison
661 * @return success/failure of operation
663 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
664 GQuark
* r
= (GQuark
*) v1
;
665 return (*r
== v2
.v_quark
);
669 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
671 * Applies the 'equal' operator to the
672 * specified structure and value
673 * @param v1 left member of comparison
674 * @param v2 right member of comparison
675 * @return success/failure of operation
677 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
678 LttTime
* r
= (LttTime
*) v1
;
679 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
683 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
685 * Applies the 'not equal' operator to the
686 * specified structure and value
687 * @param v1 left member of comparison
688 * @param v2 right member of comparison
689 * @return success/failure of operation
691 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
692 guint
* r
= (guint
*) v1
;
693 return (*r
!= v2
.v_uint
);
697 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
699 * Applies the 'not equal' operator to the
700 * specified structure and value
701 * @param v1 left member of comparison
702 * @param v2 right member of comparison
703 * @return success/failure of operation
705 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
706 guint64
* r
= (guint64
*) v1
;
707 return (*r
!= v2
.v_uint64
);
711 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
713 * Applies the 'not equal' operator to the
714 * specified structure and value
715 * @param v1 left member of comparison
716 * @param v2 right member of comparison
717 * @return success/failure of operation
719 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
720 guint32
* r
= (guint32
*) v1
;
721 return (*r
!= v2
.v_uint32
);
725 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
727 * Applies the 'not equal' operator to the
728 * specified structure and value
729 * @param v1 left member of comparison
730 * @param v2 right member of comparison
731 * @return success/failure of operation
733 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
734 guint16
* r
= (guint16
*) v1
;
735 return (*r
!= v2
.v_uint16
);
739 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
741 * Applies the 'not equal' operator to the
742 * specified structure and value
743 * @param v1 left member of comparison
744 * @param v2 right member of comparison
745 * @return success/failure of operation
747 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
748 double* r
= (double*) v1
;
749 return (*r
!= v2
.v_double
);
753 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
755 * Applies the 'not equal' operator to the
756 * specified structure and value
757 * @param v1 left member of comparison
758 * @param v2 right member of comparison
759 * @return success/failure of operation
761 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
762 char* r
= (char*) v1
;
763 return (g_strcasecmp(r
,v2
.v_string
));
767 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
769 * Applies the 'not equal' operator to the
770 * specified structure and value
771 * @param v1 left member of comparison
772 * @param v2 right member of comparison
773 * @return success/failure of operation
775 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
776 GQuark
* r
= (GQuark
*) v1
;
777 return (*r
!= v2
.v_quark
);
782 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
784 * Applies the 'not equal' operator to the
785 * specified structure and value
786 * @param v1 left member of comparison
787 * @param v2 right member of comparison
788 * @return success/failure of operation
790 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
791 LttTime
* r
= (LttTime
*) v1
;
792 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
796 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
798 * Applies the 'lower than' operator to the
799 * specified structure and value
800 * @param v1 left member of comparison
801 * @param v2 right member of comparison
802 * @return success/failure of operation
804 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
805 guint
* r
= (guint
*) v1
;
806 return (*r
< v2
.v_uint
);
810 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
812 * Applies the 'lower than' operator to the
813 * specified structure and value
814 * @param v1 left member of comparison
815 * @param v2 right member of comparison
816 * @return success/failure of operation
818 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
819 guint64
* r
= (guint64
*) v1
;
820 return (*r
< v2
.v_uint64
);
824 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
826 * Applies the 'lower than' operator to the
827 * specified structure and value
828 * @param v1 left member of comparison
829 * @param v2 right member of comparison
830 * @return success/failure of operation
832 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
833 guint32
* r
= (guint32
*) v1
;
834 return (*r
< v2
.v_uint32
);
838 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
840 * Applies the 'lower than' operator to the
841 * specified structure and value
842 * @param v1 left member of comparison
843 * @param v2 right member of comparison
844 * @return success/failure of operation
846 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
847 guint16
* r
= (guint16
*) v1
;
848 return (*r
< v2
.v_uint16
);
852 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
854 * Applies the 'lower than' operator to the
855 * specified structure and value
856 * @param v1 left member of comparison
857 * @param v2 right member of comparison
858 * @return success/failure of operation
860 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
861 double* r
= (double*) v1
;
862 return (*r
< v2
.v_double
);
866 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
868 * Applies the 'lower than' operator to the
869 * specified structure and value
870 * @param v1 left member of comparison
871 * @param v2 right member of comparison
872 * @return success/failure of operation
874 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
875 LttTime
* r
= (LttTime
*) v1
;
876 // 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)));
877 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
881 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
883 * Applies the 'lower or equal' operator to the
884 * specified structure and value
885 * @param v1 left member of comparison
886 * @param v2 right member of comparison
887 * @return success/failure of operation
889 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
890 guint
* r
= (guint
*) v1
;
891 return (*r
<= v2
.v_uint
);
895 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
897 * Applies the 'lower or equal' operator to the
898 * specified structure and value
899 * @param v1 left member of comparison
900 * @param v2 right member of comparison
901 * @return success/failure of operation
903 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
904 guint64
* r
= (guint64
*) v1
;
905 return (*r
<= v2
.v_uint64
);
909 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
911 * Applies the 'lower or equal' operator to the
912 * specified structure and value
913 * @param v1 left member of comparison
914 * @param v2 right member of comparison
915 * @return success/failure of operation
917 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
918 guint32
* r
= (guint32
*) v1
;
919 return (*r
<= v2
.v_uint32
);
923 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
925 * Applies the 'lower or equal' operator to the
926 * specified structure and value
927 * @param v1 left member of comparison
928 * @param v2 right member of comparison
929 * @return success/failure of operation
931 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
932 guint16
* r
= (guint16
*) v1
;
933 return (*r
<= v2
.v_uint16
);
937 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
939 * Applies the 'lower or equal' operator to the
940 * specified structure and value
941 * @param v1 left member of comparison
942 * @param v2 right member of comparison
943 * @return success/failure of operation
945 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
946 double* r
= (double*) v1
;
947 return (*r
<= v2
.v_double
);
951 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
953 * Applies the 'lower or equal' operator to the
954 * specified structure and value
955 * @param v1 left member of comparison
956 * @param v2 right member of comparison
957 * @return success/failure of operation
959 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
960 LttTime
* r
= (LttTime
*) v1
;
961 // 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)));
962 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
967 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
969 * Applies the 'greater than' operator to the
970 * specified structure and value
971 * @param v1 left member of comparison
972 * @param v2 right member of comparison
973 * @return success/failure of operation
975 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
976 guint
* r
= (guint
*) v1
;
977 return (*r
> v2
.v_uint
);
981 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
983 * Applies the 'greater than' operator to the
984 * specified structure and value
985 * @param v1 left member of comparison
986 * @param v2 right member of comparison
987 * @return success/failure of operation
989 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
990 guint64
* r
= (guint64
*) v1
;
991 return (*r
> v2
.v_uint64
);
995 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
997 * Applies the 'greater than' operator to the
998 * specified structure and value
999 * @param v1 left member of comparison
1000 * @param v2 right member of comparison
1001 * @return success/failure of operation
1003 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1004 guint32
* r
= (guint32
*) v1
;
1005 return (*r
> v2
.v_uint32
);
1009 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1011 * Applies the 'greater than' operator to the
1012 * specified structure and value
1013 * @param v1 left member of comparison
1014 * @param v2 right member of comparison
1015 * @return success/failure of operation
1017 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1018 guint16
* r
= (guint16
*) v1
;
1019 return (*r
> v2
.v_uint16
);
1023 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1025 * Applies the 'greater than' operator to the
1026 * specified structure and value
1027 * @param v1 left member of comparison
1028 * @param v2 right member of comparison
1029 * @return success/failure of operation
1031 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1032 double* r
= (double*) v1
;
1033 return (*r
> v2
.v_double
);
1037 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1039 * Applies the 'greater than' operator to the
1040 * specified structure and value
1041 * @param v1 left member of comparison
1042 * @param v2 right member of comparison
1043 * @return success/failure of operation
1045 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1046 LttTime
* r
= (LttTime
*) v1
;
1047 // 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)));
1048 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1052 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1054 * Applies the 'greater or equal' operator to the
1055 * specified structure and value
1056 * @param v1 left member of comparison
1057 * @param v2 right member of comparison
1058 * @return success/failure of operation
1060 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1061 guint
* r
= (guint
*) v1
;
1062 return (*r
>= v2
.v_uint
);
1066 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1068 * Applies the 'greater or equal' operator to the
1069 * specified structure and value
1070 * @param v1 left member of comparison
1071 * @param v2 right member of comparison
1072 * @return success/failure of operation
1074 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1075 guint64
* r
= (guint64
*) v1
;
1076 return (*r
>= v2
.v_uint64
);
1080 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1082 * Applies the 'greater or equal' operator to the
1083 * specified structure and value
1084 * @param v1 left member of comparison
1085 * @param v2 right member of comparison
1086 * @return success/failure of operation
1088 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1089 guint32
* r
= (guint32
*) v1
;
1090 return (*r
>= v2
.v_uint32
);
1094 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1096 * Applies the 'greater or equal' operator to the
1097 * specified structure and value
1098 * @param v1 left member of comparison
1099 * @param v2 right member of comparison
1100 * @return success/failure of operation
1102 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1103 guint16
* r
= (guint16
*) v1
;
1104 return (*r
>= v2
.v_uint16
);
1108 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1110 * Applies the 'greater or equal' operator to the
1111 * specified structure and value
1112 * @param v1 left member of comparison
1113 * @param v2 right member of comparison
1114 * @return success/failure of operation
1116 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1117 double* r
= (double*) v1
;
1118 return (*r
>= v2
.v_double
);
1122 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1124 * Applies the 'greater or equal' operator to the
1125 * specified structure and value
1126 * @param v1 left member of comparison
1127 * @param v2 right member of comparison
1128 * @return success/failure of operation
1130 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1131 LttTime
* r
= (LttTime
*) v1
;
1132 // 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)));
1133 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1139 * Makes a copy of the current filter tree
1140 * @param tree pointer to the current tree
1141 * @return new copy of the filter tree
1144 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1146 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1148 newtree
->node
= tree
->node
;
1150 newtree
->left
= tree
->left
;
1151 if(newtree
->left
== LTTV_TREE_NODE
) {
1152 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1153 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1154 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1155 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1156 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1157 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1158 /* FIXME: special case for string copy ! */
1159 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1162 newtree
->right
= tree
->right
;
1163 if(newtree
->right
== LTTV_TREE_NODE
) {
1164 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1165 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1166 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1167 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1168 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1169 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1170 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1178 * Makes a copy of the current filter
1179 * @param filter pointer to the current filter
1180 * @return new copy of the filter
1183 lttv_filter_clone(const LttvFilter
* filter
) {
1185 if(!filter
) return NULL
;
1187 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1189 strcpy(newfilter
->expression
,filter
->expression
);
1191 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1199 * @fn LttvFilter* lttv_filter_new()
1201 * Creates a new LttvFilter
1202 * @return the current LttvFilter or NULL if error
1207 LttvFilter
* filter
= g_new(LttvFilter
,1);
1208 filter
->expression
= NULL
;
1209 filter
->head
= NULL
;
1216 * @fn gboolean lttv_filter_update(LttvFilter*)
1218 * Updates the current LttvFilter by building
1219 * its tree based upon the expression string
1220 * @param filter pointer to the current LttvFilter
1221 * @return Failure/Success of operation
1224 lttv_filter_update(LttvFilter
* filter
) {
1226 // g_print("filter::lttv_filter_new()\n"); /* debug */
1228 if(filter
->expression
== NULL
) return FALSE
;
1232 p_nesting
=0, /* parenthesis nesting value */
1237 *tree
= lttv_filter_tree_new(), /* main tree */
1238 *subtree
= NULL
, /* buffer for subtrees */
1239 *t1
, /* buffer #1 */
1240 *t2
, /* buffer #2 */
1241 *t3
; /* buffer #3 */
1245 * If the tree already exists,
1246 * destroy it and build a new one
1248 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1249 filter
->head
= NULL
; /* will be assigned at the end */
1253 * each element of the list
1254 * is a sub tree created
1255 * by the use of parenthesis in the
1256 * global expression. The final tree
1257 * will be the one left at the root of
1260 GPtrArray
*tree_stack
= g_ptr_array_new();
1261 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1263 /* temporary values */
1264 GString
*a_field_component
= g_string_new("");
1265 GString
*a_string_spaces
= g_string_new("");
1266 GPtrArray
*a_field_path
= g_ptr_array_new();
1268 /* simple expression buffer */
1269 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1272 * Parse entire expression and construct
1273 * the binary tree. There are two steps
1274 * in browsing that string
1275 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1276 * 2. finding simple expressions
1277 * - field path ( separated by dots )
1278 * - op ( >, <, =, >=, <=, !=)
1279 * - value ( integer, string ... )
1280 * To spare computing time, the whole
1281 * string is parsed in this loop for a
1282 * O(n) complexity order.
1284 * When encountering logical op &,|,^
1285 * 1. parse the last value if any
1286 * 2. create a new tree
1287 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1288 * 4. concatenate this tree with the current tree on top of the stack
1289 * When encountering math ops >,>=,<,<=,=,!=
1290 * 1. add to op to the simple expression
1291 * 2. concatenate last field component to field path
1292 * When encountering concatening ops .
1293 * 1. concatenate last field component to field path
1294 * When encountering opening parenthesis (,{,[
1295 * 1. create a new subtree on top of tree stack
1296 * When encountering closing parenthesis ),},]
1297 * 1. add the expression on right child of the current tree
1298 * 2. the subtree is completed, allocate a new subtree
1299 * 3. pop the tree value from the tree stack
1303 struct timeval starttime
;
1304 struct timeval endtime
;
1305 gettimeofday(&starttime
, NULL
);
1308 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1310 // g_print("%c\n ",filter->expression[i]);
1312 switch(filter
->expression
[i
]) {
1318 /* get current tree in tree stack */
1319 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1321 /* get current node at absolute right */
1322 while(t1
->right
!= LTTV_TREE_IDLE
) {
1323 g_assert(t1
->right
== LTTV_TREE_NODE
);
1326 t2
= lttv_filter_tree_new();
1327 t2
->node
= LTTV_LOGICAL_AND
;
1328 t1
->right
= LTTV_TREE_NODE
;
1330 if(not) { /* add not operator to tree */
1331 t3
= lttv_filter_tree_new();
1332 t3
->node
= LTTV_LOGICAL_NOT
;
1333 t2
->left
= LTTV_TREE_NODE
;
1338 if(subtree
!= NULL
) { /* append subtree to current tree */
1339 t2
->left
= LTTV_TREE_NODE
;
1340 t2
->l_child
.t
= subtree
;
1342 } else { /* append a simple expression */
1343 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1344 a_field_component
= g_string_new("");
1345 g_string_free(a_string_spaces
, TRUE
);
1346 a_string_spaces
= g_string_new("");
1347 t2
->left
= LTTV_TREE_LEAF
;
1348 t2
->l_child
.leaf
= a_simple_expression
;
1349 a_simple_expression
= lttv_simple_expression_new();
1355 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1356 while(t1
->right
!= LTTV_TREE_IDLE
) {
1357 g_assert(t1
->right
== LTTV_TREE_NODE
);
1360 t2
= lttv_filter_tree_new();
1361 t2
->node
= LTTV_LOGICAL_OR
;
1362 t1
->right
= LTTV_TREE_NODE
;
1364 if(not) { // add not operator to tree
1365 t3
= lttv_filter_tree_new();
1366 t3
->node
= LTTV_LOGICAL_NOT
;
1367 t2
->left
= LTTV_TREE_NODE
;
1372 if(subtree
!= NULL
) { /* append subtree to current tree */
1373 t2
->left
= LTTV_TREE_NODE
;
1374 t2
->l_child
.t
= subtree
;
1376 } else { /* append a simple expression */
1377 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1378 a_field_component
= g_string_new("");
1379 g_string_free(a_string_spaces
, TRUE
);
1380 a_string_spaces
= g_string_new("");
1381 t2
->left
= LTTV_TREE_LEAF
;
1382 t2
->l_child
.leaf
= a_simple_expression
;
1383 a_simple_expression
= lttv_simple_expression_new();
1389 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1390 while(t1
->right
!= LTTV_TREE_IDLE
) {
1391 g_assert(t1
->right
== LTTV_TREE_NODE
);
1394 t2
= lttv_filter_tree_new();
1395 t2
->node
= LTTV_LOGICAL_XOR
;
1396 t1
->right
= LTTV_TREE_NODE
;
1398 if(not) { // add not operator to tree
1399 t3
= lttv_filter_tree_new();
1400 t3
->node
= LTTV_LOGICAL_NOT
;
1401 t2
->left
= LTTV_TREE_NODE
;
1406 if(subtree
!= NULL
) { /* append subtree to current tree */
1407 t2
->left
= LTTV_TREE_NODE
;
1408 t2
->l_child
.t
= subtree
;
1410 } else { /* append a simple expression */
1411 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1412 a_field_component
= g_string_new("");
1413 g_string_free(a_string_spaces
, TRUE
);
1414 a_string_spaces
= g_string_new("");
1415 t2
->left
= LTTV_TREE_LEAF
;
1416 t2
->l_child
.leaf
= a_simple_expression
;
1417 a_simple_expression
= lttv_simple_expression_new();
1421 case '!': /* not, or not equal (math op) */
1423 if(filter
->expression
[i
+1] == '=') { /* != */
1424 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1425 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1426 a_field_component
= g_string_new("");
1427 g_string_free(a_string_spaces
, TRUE
);
1428 a_string_spaces
= g_string_new("");
1429 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1436 case '(': /* start of parenthesis */
1440 p_nesting
++; /* incrementing parenthesis nesting value */
1441 t1
= lttv_filter_tree_new();
1442 if(not) { /* add not operator to tree */
1443 t3
= lttv_filter_tree_new();
1444 t3
->node
= LTTV_LOGICAL_NOT
;
1445 t1
->right
= LTTV_TREE_NODE
;
1449 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1452 case ')': /* end of parenthesis */
1456 p_nesting
--; /* decrementing parenthesis nesting value */
1457 if(p_nesting
<0 || tree_stack
->len
<2) {
1458 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1459 is not valid due to parenthesis incorrect use",filter
->expression
);
1463 /* there must at least be the root tree left in the array */
1464 g_assert(tree_stack
->len
>0);
1466 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1467 while(t1
->right
!= LTTV_TREE_IDLE
) {
1470 if(not) { // add not operator to tree
1472 t3
= lttv_filter_tree_new();
1473 t3
->node
= LTTV_LOGICAL_NOT
;
1474 t1
->right
= LTTV_TREE_NODE
;
1479 if(subtree
!= NULL
) { /* append subtree to current tree */
1480 t1
->right
= LTTV_TREE_NODE
;
1481 t1
->r_child
.t
= subtree
;
1482 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1483 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1484 } else { /* assign subtree as current tree */
1485 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1486 a_field_component
= g_string_new("");
1487 g_string_free(a_string_spaces
, TRUE
);
1488 a_string_spaces
= g_string_new("");
1489 t1
->right
= LTTV_TREE_LEAF
;
1490 t1
->r_child
.leaf
= a_simple_expression
;
1491 a_simple_expression
= lttv_simple_expression_new();
1492 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1497 * mathematic operators
1499 case '<': /* lower, lower or equal */
1501 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1502 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1503 a_field_component
= g_string_new("");
1504 g_string_free(a_string_spaces
, TRUE
);
1505 a_string_spaces
= g_string_new("");
1506 if(filter
->expression
[i
+1] == '=') { /* <= */
1508 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1509 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1512 case '>': /* higher, higher or equal */
1514 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1515 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1516 a_field_component
= g_string_new("");
1517 g_string_free(a_string_spaces
, TRUE
);
1518 a_string_spaces
= g_string_new("");
1519 if(filter
->expression
[i
+1] == '=') { /* >= */
1521 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1522 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1525 case '=': /* equal */
1527 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1528 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1529 a_field_component
= g_string_new("");
1530 g_string_free(a_string_spaces
, TRUE
);
1531 a_string_spaces
= g_string_new("");
1532 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1536 * Field concatening caracter
1541 * divide field expression into elements
1542 * in a_field_path array.
1544 * A dot can also be present in double values
1546 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1547 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1548 a_field_component
= g_string_new("");
1549 g_string_free(a_string_spaces
, TRUE
);
1550 a_string_spaces
= g_string_new("");
1553 case ' ': /* keep spaces that are within a field component */
1554 if(a_field_component
->len
== 0) break; /* ignore */
1556 a_string_spaces
= g_string_append_c(a_string_spaces
,
1557 filter
->expression
[i
]);
1559 case '\n': /* ignore */
1561 default: /* concatening current string */
1562 if(a_string_spaces
->len
!= 0) {
1563 g_string_append(a_field_component
, a_string_spaces
->str
);
1564 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1566 a_field_component
= g_string_append_c(a_field_component
,
1567 filter
->expression
[i
]);
1572 * Preliminary check to see
1573 * if tree was constructed correctly
1576 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1577 is not valid due to parenthesis incorrect use",filter
->expression
);
1581 if(tree_stack
->len
!= 1) /* only root tree should remain */
1585 * processing last element of expression
1587 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1588 while(t1
->right
!= LTTV_TREE_IDLE
) {
1589 g_assert(t1
->right
== LTTV_TREE_NODE
);
1592 if(not) { // add not operator to tree
1593 t3
= lttv_filter_tree_new();
1594 t3
->node
= LTTV_LOGICAL_NOT
;
1595 t1
->right
= LTTV_TREE_NODE
;
1600 if(subtree
!= NULL
) { /* add the subtree */
1601 t1
->right
= LTTV_TREE_NODE
;
1602 t1
->r_child
.t
= subtree
;
1604 } else { /* add a leaf */
1605 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1606 a_field_component
= NULL
;
1607 g_string_free(a_string_spaces
, TRUE
);
1608 a_string_spaces
= NULL
;
1609 t1
->right
= LTTV_TREE_LEAF
;
1610 t1
->r_child
.leaf
= a_simple_expression
;
1611 a_simple_expression
= NULL
;
1615 /* free the pointer array */
1616 g_assert(a_field_path
->len
== 0);
1617 g_ptr_array_free(a_field_path
,TRUE
);
1619 /* free the tree stack -- but keep the root tree */
1620 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1621 g_ptr_array_free(tree_stack
,TRUE
);
1623 /* free the field buffer if allocated */
1624 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1625 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1627 /* free the simple expression buffer if allocated */
1628 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1630 g_assert(filter
->head
!= NULL
); /* tree should exist */
1631 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1634 gettimeofday(&endtime
, NULL
);
1636 /* Calcul du temps de l'algorithme */
1637 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1638 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1639 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1640 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1644 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1645 lttv_print_tree(filter
->head
,0) ;
1646 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1654 * @fn void lttv_filter_destroy(LttvFilter*)
1656 * Destroy the current LttvFilter
1657 * @param filter pointer to the current LttvFilter
1660 lttv_filter_destroy(LttvFilter
* filter
) {
1664 if(filter
->expression
)
1665 g_free(filter
->expression
);
1667 lttv_filter_tree_destroy(filter
->head
);
1673 * @fn LttvFilterTree* lttv_filter_tree_new()
1675 * Assign a new tree for the current expression
1677 * @return pointer of LttvFilterTree
1680 lttv_filter_tree_new() {
1681 LttvFilterTree
* tree
;
1683 tree
= g_new(LttvFilterTree
,1);
1684 tree
->node
= 0; //g_new(lttv_expression,1);
1685 tree
->left
= LTTV_TREE_IDLE
;
1686 tree
->right
= LTTV_TREE_IDLE
;
1687 tree
->r_child
.t
= NULL
;
1688 tree
->l_child
.t
= NULL
;
1694 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1696 * Append a new expression to the expression
1697 * defined in the current filter
1698 * @param filter pointer to the current LttvFilter
1699 * @param expression string that must be appended
1700 * @return Success/Failure of operation
1703 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1705 if(expression
== NULL
) return FALSE
;
1706 if(filter
== NULL
) return FALSE
;
1707 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1709 GString
* s
= g_string_new("");
1710 if(filter
->expression
!= NULL
) {
1711 g_string_append(s
,filter
->expression
);
1712 g_string_append_c(s
,'&');
1714 g_string_append(s
,expression
);
1716 g_free(filter
->expression
);
1717 filter
->expression
= g_string_free(s
,FALSE
);
1719 /* TRUE if construction of tree proceeded without errors */
1720 return lttv_filter_update(filter
);
1725 * @fn void lttv_filter_clear_expression(LttvFilter*)
1727 * Clear the filter expression from the
1728 * current filter and sets its pointer to NULL
1729 * @param filter pointer to the current LttvFilter
1732 lttv_filter_clear_expression(LttvFilter
* filter
) {
1734 if(filter
->expression
!= NULL
) {
1735 g_free(filter
->expression
);
1736 filter
->expression
= NULL
;
1742 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1744 * Destroys the tree and his sub-trees
1745 * @param tree Tree which must be destroyed
1748 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1750 if(tree
== NULL
) return;
1752 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1753 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1755 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1756 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1758 // g_free(tree->node);
1763 * Global parsing function for the current
1765 * @param t pointer to the current LttvFilterTree
1766 * @param event current LttEvent, NULL if not used
1767 * @param tracefile current LttTracefile, NULL if not used
1768 * @param trace current LttTrace, NULL if not used
1769 * @param state current LttvProcessState, NULL if not used
1770 * @param context current LttvTracefileContext, NULL if not used
1771 * @return response of filter
1774 lttv_filter_tree_parse(
1775 const LttvFilterTree
* t
,
1776 const LttEvent
* event
,
1777 const LttTracefile
* tracefile
,
1778 const LttTrace
* trace
,
1779 const LttvTracefileContext
* context
1784 * Each tree is parsed in inorder.
1785 * This way, it's possible to apply the left filter of the
1786 * tree, then decide whether or not the right branch should
1787 * be parsed depending on the linking logical operator
1789 * Each node consists in a
1790 * 1. logical operator
1791 * 2. left child ( node or simple expression )
1792 * 3. right child ( node or simple expression )
1794 * When the child is a simple expression, we must
1795 * before all determine if the expression refers to
1796 * a structure which is whithin observation ( not NULL ).
1797 * -If so, the expression is evaluated.
1798 * -If not, the result is set to TRUE since this particular
1799 * operation does not interfere with the lttv structure
1801 * The result of each simple expression will directly
1802 * affect the next branch. This way, depending on
1803 * the linking logical operator, the parser will decide
1804 * to explore or not the next branch.
1806 * -If result of left branch is 0 / FALSE
1807 * then don't explore right branch and return 0;
1808 * -If result of left branch is 1 / TRUE then explore
1810 * -If result of left branch is 1 / TRUE
1811 * then don't explore right branch and return 1;
1812 * -If result of left branch is 0 / FALSE then explore
1814 * -Result of left branch will not affect exploration of
1818 gboolean lresult
= FALSE
, rresult
= FALSE
;
1820 LttvProcessState
* state
;
1822 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1823 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1824 guint cpu
= tfs
->cpu
;
1825 state
= ts
->running_process
[cpu
];
1830 if(t
->left
== LTTV_TREE_NODE
) {
1831 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1833 else if(t
->left
== LTTV_TREE_LEAF
) {
1834 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1838 * Parse linking operator
1839 * make a cutoff if possible
1841 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1842 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1845 * Parse right branch
1847 if(t
->right
== LTTV_TREE_NODE
) {
1848 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1850 else if(t
->right
== LTTV_TREE_LEAF
) {
1851 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1856 * Apply and return the
1857 * logical link between the
1861 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1862 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1863 case LTTV_LOGICAL_NOT
:
1864 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1865 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1866 case 0: return (rresult
);
1869 * This case should never be
1870 * parsed, if so, this subtree
1879 * This function parses a particular branch of the tree
1880 * @param se pointer to the current LttvSimpleExpression
1881 * @param event current LttEvent, NULL if not used
1882 * @param tracefile current LttTracefile, NULL if not used
1883 * @param trace current LttTrace, NULL if not used
1884 * @param state current LttvProcessState, NULL if not used
1885 * @param context current LttvTracefileContext, NULL if not used
1886 * @return response of filter
1889 lttv_filter_tree_parse_branch(
1890 const LttvSimpleExpression
* se
,
1891 const LttEvent
* event
,
1892 const LttTracefile
* tracefile
,
1893 const LttTrace
* trace
,
1894 const LttvProcessState
* state
,
1895 const LttvTracefileContext
* context
) {
1900 case LTTV_FILTER_TRACE_NAME
:
1901 if(trace
== NULL
) return TRUE
;
1903 GQuark quark
= ltt_trace_name(trace
);
1904 return se
->op((gpointer
)&quark
,v
);
1907 case LTTV_FILTER_TRACEFILE_NAME
:
1908 if(tracefile
== NULL
) return TRUE
;
1910 GQuark quark
= ltt_tracefile_name(tracefile
);
1911 return se
->op((gpointer
)&quark
,v
);
1914 case LTTV_FILTER_STATE_PID
:
1915 if(state
== NULL
) return TRUE
;
1916 else return se
->op((gpointer
)&state
->pid
,v
);
1918 case LTTV_FILTER_STATE_PPID
:
1919 if(state
== NULL
) return TRUE
;
1920 else return se
->op((gpointer
)&state
->ppid
,v
);
1922 case LTTV_FILTER_STATE_CT
:
1923 if(state
== NULL
) return TRUE
;
1925 return se
->op((gpointer
)&state
->creation_time
,v
);
1928 case LTTV_FILTER_STATE_IT
:
1929 if(state
== NULL
) return TRUE
;
1931 return se
->op((gpointer
)&state
->insertion_time
,v
);
1934 case LTTV_FILTER_STATE_P_NAME
:
1936 * All 'unnamed' for the moment
1938 if(state
== NULL
) return TRUE
;
1940 GQuark quark
= state
->name
;
1941 return se
->op((gpointer
)&quark
,v
);
1944 case LTTV_FILTER_STATE_EX_MODE
:
1945 if(state
== NULL
) return TRUE
;
1946 else return se
->op((gpointer
)&state
->state
->t
,v
);
1948 case LTTV_FILTER_STATE_EX_SUBMODE
:
1949 if(state
== NULL
) return TRUE
;
1950 else return se
->op((gpointer
)&state
->state
->n
,v
);
1952 case LTTV_FILTER_STATE_P_STATUS
:
1953 if(state
== NULL
) return TRUE
;
1954 else return se
->op((gpointer
)&state
->state
->s
,v
);
1956 case LTTV_FILTER_STATE_CPU
:
1957 if(context
== NULL
) return TRUE
;
1959 if(state
== NULL
) return TRUE
;
1960 else return se
->op((gpointer
)&state
->cpu
,v
);
1963 case LTTV_FILTER_EVENT_NAME
:
1964 if(event
== NULL
) return TRUE
;
1967 et
= ltt_event_eventtype(event
);
1968 GQuark quark
= ltt_eventtype_name(et
);
1969 return se
->op((gpointer
)&quark
,v
);
1972 case LTTV_FILTER_EVENT_FACILITY
:
1973 if(event
== NULL
) return TRUE
;
1976 fac
= ltt_event_facility(event
);
1977 GQuark quark
= ltt_facility_name(fac
);
1978 return se
->op((gpointer
)&quark
,v
);
1981 case LTTV_FILTER_EVENT_CATEGORY
:
1983 * TODO: Not yet implemented
1987 case LTTV_FILTER_EVENT_TIME
:
1988 if(event
== NULL
) return TRUE
;
1990 LttTime time
= ltt_event_time(event
);
1991 return se
->op((gpointer
)&time
,v
);
1994 case LTTV_FILTER_EVENT_TSC
:
1995 if(event
== NULL
) return TRUE
;
1997 LttCycleCount count
= ltt_event_cycle_count(event
);
1998 return se
->op((gpointer
)&count
,v
);
2001 case LTTV_FILTER_EVENT_FIELD
:
2003 * TODO: Use the offset to
2004 * find the dynamic field
2005 * in the event struct
2010 * This case should never be
2011 * parsed, if so, the whole
2012 * filtering is cancelled
2014 g_warning("Error while parsing the filter tree");
2018 /* should never get here */
2026 * Debug function. Prints tree memory allocation.
2027 * @param t the pointer to the current LttvFilterTree
2030 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2032 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2033 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2034 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2036 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2037 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"));
2038 if(t
->left
== LTTV_TREE_LEAF
) {
2039 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2040 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2041 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2043 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"));
2044 if(t
->right
== LTTV_TREE_LEAF
) {
2045 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2046 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2047 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2050 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2051 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2055 * @fn static void module_init()
2057 * Initializes the filter module and specific values
2059 static void module_init()
2065 * Destroys the filter module and specific values
2067 static void module_destroy()
2073 LTTV_MODULE("filter", "Filters traceset and events", \
2074 "Filters traceset and events specifically to user input", \
2075 module_init
, module_destroy
)