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 * |->thread_brand (String, converted to GQuark)
56 * |->execution_mode (LttvExecutionMode)
57 * |->execution_submode (LttvExecutionSubmode)
58 * |->process_status (LttvProcessStatus)
65 * - refine switch of expression in multiple uses functions
66 * - remove the idle expressions in the tree
79 #include <lttv/lttv.h>
80 #include <lttv/filter.h>
81 #include <ltt/trace.h>
83 #include <ltt/facility.h>
88 * @fn LttvSimpleExpression* lttv_simple_expression_new()
90 * Constructor for LttvSimpleExpression
91 * @return pointer to new LttvSimpleExpression
94 lttv_simple_expression_new() {
96 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
98 se
->field
= LTTV_FILTER_UNDEFINED
;
106 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
108 * Parse through filtering field hierarchy as specified
109 * by user. This function compares each value to
110 * predetermined quarks
111 * @param fp The field path list
112 * @param se current simple expression
113 * @return success/failure of operation
116 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
120 if(fp
->len
< 2) return FALSE
;
121 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
124 * Parse through the specified
127 * Take note however that the
128 * 'event' subfields might change
129 * depending on values specified
130 * in core.xml file. Hence, if
131 * none of the subfields in the
132 * array match the hardcoded
133 * subfields, it will be considered
136 if(!g_strcasecmp(f
->str
,"trace") ) {
141 g_string_free(f
,TRUE
);
142 f
=g_ptr_array_remove_index(fp
,0);
143 if(!g_strcasecmp(f
->str
,"name")) {
144 se
->field
= LTTV_FILTER_TRACE_NAME
;
146 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
148 * FIXME: not yet implemented !
150 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
155 g_string_free(f
,TRUE
);
156 f
=g_ptr_array_remove_index(fp
,0);
157 if(!g_strcasecmp(f
->str
,"name")) {
158 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
160 } else if(!g_strcasecmp(f
->str
,"state") ) {
165 * state.creation_time
166 * state.insertion_time
169 * state.execution_mode
170 * state.execution_submode
171 * state.process_status
174 g_string_free(f
,TRUE
);
175 f
=g_ptr_array_remove_index(fp
,0);
176 if(!g_strcasecmp(f
->str
,"pid") ) {
177 se
->field
= LTTV_FILTER_STATE_PID
;
179 else if(!g_strcasecmp(f
->str
,"ppid") ) {
180 se
->field
= LTTV_FILTER_STATE_PPID
;
182 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
183 se
->field
= LTTV_FILTER_STATE_CT
;
185 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
186 se
->field
= LTTV_FILTER_STATE_IT
;
188 else if(!g_strcasecmp(f
->str
,"process_name") ) {
189 se
->field
= LTTV_FILTER_STATE_P_NAME
;
191 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
192 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
194 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
195 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
197 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
198 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
200 else if(!g_strcasecmp(f
->str
,"process_status") ) {
201 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
203 else if(!g_strcasecmp(f
->str
,"cpu") ) {
204 se
->field
= LTTV_FILTER_STATE_CPU
;
206 } else if(!g_strcasecmp(f
->str
,"event") ) {
215 g_string_free(f
,TRUE
);
216 f
=g_ptr_array_remove_index(fp
,0);
217 if(!g_strcasecmp(f
->str
,"name") ) {
218 se
->field
= LTTV_FILTER_EVENT_NAME
;
220 else if(!g_strcasecmp(f
->str
,"facility") ) {
221 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
223 else if(!g_strcasecmp(f
->str
,"category") ) {
225 * FIXME: Category not yet functional in lttv
227 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
229 else if(!g_strcasecmp(f
->str
,"time") ) {
230 se
->field
= LTTV_FILTER_EVENT_TIME
;
232 else if(!g_strcasecmp(f
->str
,"tsc") ) {
233 se
->field
= LTTV_FILTER_EVENT_TSC
;
235 else if(!g_strcasecmp(f
->str
,"field") ) {
236 se
->field
= LTTV_FILTER_EVENT_FIELD
;
237 g_string_free(f
,TRUE
);
238 f
=g_ptr_array_remove_index(fp
,0);
241 g_string_free(f
,TRUE
);
242 f
=g_ptr_array_remove_index(fp
,0);
243 g_warning("Unknown event filter subtype %s", f
->str
);
246 g_string_free(f
,TRUE
);
247 f
=g_ptr_array_remove_index(fp
,0);
249 g_warning("Unrecognized field in filter string");
252 /* free memory for last string */
253 g_string_free(f
,TRUE
);
255 /* array should be empty */
256 g_assert(fp
->len
== 0);
258 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
259 g_warning("The specified field was not recognized !");
266 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
268 * Sets the function pointer for the current
270 * @param se current simple expression
271 * @param op current operator
272 * @return success/failure of operation
275 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
281 case LTTV_FILTER_TRACE_NAME
:
282 case LTTV_FILTER_TRACEFILE_NAME
:
283 case LTTV_FILTER_STATE_P_NAME
:
284 case LTTV_FILTER_STATE_T_BRAND
:
285 case LTTV_FILTER_EVENT_NAME
:
286 case LTTV_FILTER_EVENT_FACILITY
:
287 case LTTV_FILTER_STATE_EX_MODE
:
288 case LTTV_FILTER_STATE_EX_SUBMODE
:
289 case LTTV_FILTER_STATE_P_STATUS
:
292 se
->op
= lttv_apply_op_eq_quark
;
295 se
->op
= lttv_apply_op_ne_quark
;
298 g_warning("Error encountered in operator assignment = or != expected");
305 case LTTV_FILTER_EVENT_TSC
:
308 se
->op
= lttv_apply_op_eq_uint64
;
311 se
->op
= lttv_apply_op_ne_uint64
;
314 se
->op
= lttv_apply_op_lt_uint64
;
317 se
->op
= lttv_apply_op_le_uint64
;
320 se
->op
= lttv_apply_op_gt_uint64
;
323 se
->op
= lttv_apply_op_ge_uint64
;
326 g_warning("Error encountered in operator assignment");
333 case LTTV_FILTER_STATE_CPU
:
334 case LTTV_FILTER_STATE_PID
:
335 case LTTV_FILTER_STATE_PPID
:
338 se
->op
= lttv_apply_op_eq_uint
;
341 se
->op
= lttv_apply_op_ne_uint
;
344 se
->op
= lttv_apply_op_lt_uint
;
347 se
->op
= lttv_apply_op_le_uint
;
350 se
->op
= lttv_apply_op_gt_uint
;
353 se
->op
= lttv_apply_op_ge_uint
;
356 g_warning("Error encountered in operator assignment");
363 * Entered as string, converted to enum
365 * can only be compared with 'equal' or 'not equal' operators
367 * unsigned int of 16 bits are used here since enums
368 * should not go over 2^16-1 values
372 // case LTTV_FIELD_EQ:
373 // se->op = lttv_apply_op_eq_uint16;
375 // case LTTV_FIELD_NE:
376 // se->op = lttv_apply_op_ne_uint16;
379 // g_warning("Error encountered in operator assignment = or != expected");
386 case LTTV_FILTER_STATE_CT
:
387 case LTTV_FILTER_STATE_IT
:
388 case LTTV_FILTER_EVENT_TIME
:
391 se
->op
= lttv_apply_op_eq_ltttime
;
394 se
->op
= lttv_apply_op_ne_ltttime
;
397 se
->op
= lttv_apply_op_lt_ltttime
;
400 se
->op
= lttv_apply_op_le_ltttime
;
403 se
->op
= lttv_apply_op_gt_ltttime
;
406 se
->op
= lttv_apply_op_ge_ltttime
;
409 g_warning("Error encountered in operator assignment");
414 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
423 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
425 * Assign the value field to the current LttvSimpleExpression
426 * @param se pointer to the current LttvSimpleExpression
427 * @param value string value for simple expression
430 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
433 gboolean is_double
= FALSE
;
434 LttTime t
= ltt_time_zero
;
440 * entered as strings, converted to Quarks
442 case LTTV_FILTER_TRACE_NAME
:
443 case LTTV_FILTER_TRACEFILE_NAME
:
444 case LTTV_FILTER_STATE_P_NAME
:
445 case LTTV_FILTER_STATE_T_BRAND
:
446 case LTTV_FILTER_EVENT_NAME
:
447 case LTTV_FILTER_EVENT_FACILITY
:
448 case LTTV_FILTER_STATE_EX_MODE
:
449 case LTTV_FILTER_STATE_EX_SUBMODE
:
450 case LTTV_FILTER_STATE_P_STATUS
:
451 // se->value.v_string = value;
452 se
->value
.v_quark
= g_quark_from_string(value
);
456 * integer -- supposed to be uint64
458 case LTTV_FILTER_EVENT_TSC
:
459 se
->value
.v_uint64
= atoi(value
);
465 case LTTV_FILTER_STATE_PID
:
466 case LTTV_FILTER_STATE_PPID
:
467 case LTTV_FILTER_STATE_CPU
:
468 se
->value
.v_uint
= atoi(value
);
474 case LTTV_FILTER_STATE_CT
:
475 case LTTV_FILTER_STATE_IT
:
476 case LTTV_FILTER_EVENT_TIME
:
477 //se->value.v_double = atof(value);
479 * parsing logic could be optimised,
480 * but as for now, simpler this way
482 v
= g_string_new("");
483 for(i
=0;i
<strlen(value
);i
++) {
484 if(value
[i
] == '.') {
485 /* cannot specify number with more than one '.' */
486 if(is_double
) return FALSE
;
487 else is_double
= TRUE
;
488 t
.tv_sec
= atoi(v
->str
);
489 g_string_free(v
,TRUE
);
490 v
= g_string_new("");
491 } else v
= g_string_append_c(v
,value
[i
]);
493 /* number can be integer or double */
494 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
495 else t
.tv_sec
= atoi(v
->str
);
497 g_string_free(v
,TRUE
);
499 se
->value
.v_ltttime
= t
;
503 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
513 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
515 * Disallocate memory for the current
517 * @param se pointer to the current LttvSimpleExpression
520 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
522 // g_free(se->value);
523 // switch(se->field) {
524 // case LTTV_FILTER_TRACE_NAME:
525 // case LTTV_FILTER_TRACEFILE_NAME:
526 // case LTTV_FILTER_STATE_P_NAME:
527 // case LTTV_FILTER_EVENT_NAME:
528 // g_free(se->value.v_string);
536 * @fn gint lttv_struct_type(gint)
538 * Finds the structure type depending
539 * on the fields in parameters
540 * @params ft Field of the current structure
541 * @return LttvStructType enum or -1 for error
544 lttv_struct_type(gint ft
) {
547 case LTTV_FILTER_TRACE_NAME
:
548 return LTTV_FILTER_TRACE
;
550 case LTTV_FILTER_TRACEFILE_NAME
:
551 return LTTV_FILTER_TRACEFILE
;
553 case LTTV_FILTER_STATE_PID
:
554 case LTTV_FILTER_STATE_PPID
:
555 case LTTV_FILTER_STATE_CT
:
556 case LTTV_FILTER_STATE_IT
:
557 case LTTV_FILTER_STATE_P_NAME
:
558 case LTTV_FILTER_STATE_T_BRAND
:
559 case LTTV_FILTER_STATE_EX_MODE
:
560 case LTTV_FILTER_STATE_EX_SUBMODE
:
561 case LTTV_FILTER_STATE_P_STATUS
:
562 case LTTV_FILTER_STATE_CPU
:
563 return LTTV_FILTER_STATE
;
565 case LTTV_FILTER_EVENT_NAME
:
566 case LTTV_FILTER_EVENT_FACILITY
:
567 case LTTV_FILTER_EVENT_CATEGORY
:
568 case LTTV_FILTER_EVENT_TIME
:
569 case LTTV_FILTER_EVENT_TSC
:
570 case LTTV_FILTER_EVENT_FIELD
:
571 return LTTV_FILTER_EVENT
;
579 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
581 * Applies the 'equal' operator to the
582 * specified structure and value
583 * @param v1 left member of comparison
584 * @param v2 right member of comparison
585 * @return success/failure of operation
587 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
589 guint
* r
= (guint
*) v1
;
590 return (*r
== v2
.v_uint
);
595 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
597 * Applies the 'equal' operator to the
598 * specified structure and value
599 * @param v1 left member of comparison
600 * @param v2 right member of comparison
601 * @return success/failure of operation
603 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
605 guint64
* r
= (guint64
*) v1
;
606 return (*r
== v2
.v_uint64
);
611 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
613 * Applies the 'equal' operator to the
614 * specified structure and value
615 * @param v1 left member of comparison
616 * @param v2 right member of comparison
617 * @return success/failure of operation
619 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
620 guint32
* r
= (guint32
*) v1
;
621 return (*r
== v2
.v_uint32
);
625 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
627 * Applies the 'equal' operator to the
628 * specified structure and value
629 * @param v1 left member of comparison
630 * @param v2 right member of comparison
631 * @return success/failure of operation
633 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
634 guint16
* r
= (guint16
*) v1
;
635 return (*r
== v2
.v_uint16
);
639 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
641 * Applies the 'equal' operator to the
642 * specified structure and value
643 * @param v1 left member of comparison
644 * @param v2 right member of comparison
645 * @return success/failure of operation
647 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
648 double* r
= (double*) v1
;
649 return (*r
== v2
.v_double
);
653 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
655 * Applies the 'equal' operator to the
656 * specified structure and value
657 * @param v1 left member of comparison
658 * @param v2 right member of comparison
659 * @return success/failure of operation
661 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
662 char* r
= (char*) v1
;
663 return (!g_strcasecmp(r
,v2
.v_string
));
667 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
669 * Applies the 'equal' operator to the
670 * specified structure and value
671 * @param v1 left member of comparison
672 * @param v2 right member of comparison
673 * @return success/failure of operation
675 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
676 GQuark
* r
= (GQuark
*) v1
;
677 return (*r
== v2
.v_quark
);
681 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
683 * Applies the 'equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
689 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
690 LttTime
* r
= (LttTime
*) v1
;
691 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
695 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
697 * Applies the 'not equal' operator to the
698 * specified structure and value
699 * @param v1 left member of comparison
700 * @param v2 right member of comparison
701 * @return success/failure of operation
703 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
704 guint
* r
= (guint
*) v1
;
705 return (*r
!= v2
.v_uint
);
709 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
711 * Applies the 'not equal' operator to the
712 * specified structure and value
713 * @param v1 left member of comparison
714 * @param v2 right member of comparison
715 * @return success/failure of operation
717 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
718 guint64
* r
= (guint64
*) v1
;
719 return (*r
!= v2
.v_uint64
);
723 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
725 * Applies the 'not equal' operator to the
726 * specified structure and value
727 * @param v1 left member of comparison
728 * @param v2 right member of comparison
729 * @return success/failure of operation
731 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
732 guint32
* r
= (guint32
*) v1
;
733 return (*r
!= v2
.v_uint32
);
737 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
739 * Applies the 'not equal' operator to the
740 * specified structure and value
741 * @param v1 left member of comparison
742 * @param v2 right member of comparison
743 * @return success/failure of operation
745 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
746 guint16
* r
= (guint16
*) v1
;
747 return (*r
!= v2
.v_uint16
);
751 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
753 * Applies the 'not equal' operator to the
754 * specified structure and value
755 * @param v1 left member of comparison
756 * @param v2 right member of comparison
757 * @return success/failure of operation
759 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
760 double* r
= (double*) v1
;
761 return (*r
!= v2
.v_double
);
765 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
767 * Applies the 'not equal' operator to the
768 * specified structure and value
769 * @param v1 left member of comparison
770 * @param v2 right member of comparison
771 * @return success/failure of operation
773 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
774 char* r
= (char*) v1
;
775 return (g_strcasecmp(r
,v2
.v_string
));
779 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
781 * Applies the 'not equal' operator to the
782 * specified structure and value
783 * @param v1 left member of comparison
784 * @param v2 right member of comparison
785 * @return success/failure of operation
787 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
788 GQuark
* r
= (GQuark
*) v1
;
789 return (*r
!= v2
.v_quark
);
794 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
796 * Applies the 'not equal' operator to the
797 * specified structure and value
798 * @param v1 left member of comparison
799 * @param v2 right member of comparison
800 * @return success/failure of operation
802 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
803 LttTime
* r
= (LttTime
*) v1
;
804 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
808 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
810 * Applies the 'lower than' operator to the
811 * specified structure and value
812 * @param v1 left member of comparison
813 * @param v2 right member of comparison
814 * @return success/failure of operation
816 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
817 guint
* r
= (guint
*) v1
;
818 return (*r
< v2
.v_uint
);
822 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
824 * Applies the 'lower than' operator to the
825 * specified structure and value
826 * @param v1 left member of comparison
827 * @param v2 right member of comparison
828 * @return success/failure of operation
830 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
831 guint64
* r
= (guint64
*) v1
;
832 return (*r
< v2
.v_uint64
);
836 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
838 * Applies the 'lower than' operator to the
839 * specified structure and value
840 * @param v1 left member of comparison
841 * @param v2 right member of comparison
842 * @return success/failure of operation
844 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
845 guint32
* r
= (guint32
*) v1
;
846 return (*r
< v2
.v_uint32
);
850 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
852 * Applies the 'lower than' operator to the
853 * specified structure and value
854 * @param v1 left member of comparison
855 * @param v2 right member of comparison
856 * @return success/failure of operation
858 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
859 guint16
* r
= (guint16
*) v1
;
860 return (*r
< v2
.v_uint16
);
864 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
866 * Applies the 'lower than' operator to the
867 * specified structure and value
868 * @param v1 left member of comparison
869 * @param v2 right member of comparison
870 * @return success/failure of operation
872 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
873 double* r
= (double*) v1
;
874 return (*r
< v2
.v_double
);
878 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
880 * Applies the 'lower than' operator to the
881 * specified structure and value
882 * @param v1 left member of comparison
883 * @param v2 right member of comparison
884 * @return success/failure of operation
886 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
887 LttTime
* r
= (LttTime
*) v1
;
888 // 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)));
889 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
893 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
895 * Applies the 'lower or equal' operator to the
896 * specified structure and value
897 * @param v1 left member of comparison
898 * @param v2 right member of comparison
899 * @return success/failure of operation
901 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
902 guint
* r
= (guint
*) v1
;
903 return (*r
<= v2
.v_uint
);
907 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
909 * Applies the 'lower or equal' operator to the
910 * specified structure and value
911 * @param v1 left member of comparison
912 * @param v2 right member of comparison
913 * @return success/failure of operation
915 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
916 guint64
* r
= (guint64
*) v1
;
917 return (*r
<= v2
.v_uint64
);
921 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
923 * Applies the 'lower or equal' operator to the
924 * specified structure and value
925 * @param v1 left member of comparison
926 * @param v2 right member of comparison
927 * @return success/failure of operation
929 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
930 guint32
* r
= (guint32
*) v1
;
931 return (*r
<= v2
.v_uint32
);
935 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
937 * Applies the 'lower or equal' operator to the
938 * specified structure and value
939 * @param v1 left member of comparison
940 * @param v2 right member of comparison
941 * @return success/failure of operation
943 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
944 guint16
* r
= (guint16
*) v1
;
945 return (*r
<= v2
.v_uint16
);
949 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
951 * Applies the 'lower or equal' operator to the
952 * specified structure and value
953 * @param v1 left member of comparison
954 * @param v2 right member of comparison
955 * @return success/failure of operation
957 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
958 double* r
= (double*) v1
;
959 return (*r
<= v2
.v_double
);
963 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
965 * Applies the 'lower or equal' operator to the
966 * specified structure and value
967 * @param v1 left member of comparison
968 * @param v2 right member of comparison
969 * @return success/failure of operation
971 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
972 LttTime
* r
= (LttTime
*) v1
;
973 // 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)));
974 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
979 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
981 * Applies the 'greater than' operator to the
982 * specified structure and value
983 * @param v1 left member of comparison
984 * @param v2 right member of comparison
985 * @return success/failure of operation
987 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
988 guint
* r
= (guint
*) v1
;
989 return (*r
> v2
.v_uint
);
993 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
995 * Applies the 'greater than' operator to the
996 * specified structure and value
997 * @param v1 left member of comparison
998 * @param v2 right member of comparison
999 * @return success/failure of operation
1001 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1002 guint64
* r
= (guint64
*) v1
;
1003 return (*r
> v2
.v_uint64
);
1007 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1009 * Applies the 'greater than' operator to the
1010 * specified structure and value
1011 * @param v1 left member of comparison
1012 * @param v2 right member of comparison
1013 * @return success/failure of operation
1015 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1016 guint32
* r
= (guint32
*) v1
;
1017 return (*r
> v2
.v_uint32
);
1021 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1023 * Applies the 'greater than' operator to the
1024 * specified structure and value
1025 * @param v1 left member of comparison
1026 * @param v2 right member of comparison
1027 * @return success/failure of operation
1029 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1030 guint16
* r
= (guint16
*) v1
;
1031 return (*r
> v2
.v_uint16
);
1035 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1037 * Applies the 'greater than' operator to the
1038 * specified structure and value
1039 * @param v1 left member of comparison
1040 * @param v2 right member of comparison
1041 * @return success/failure of operation
1043 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1044 double* r
= (double*) v1
;
1045 return (*r
> v2
.v_double
);
1049 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1051 * Applies the 'greater than' operator to the
1052 * specified structure and value
1053 * @param v1 left member of comparison
1054 * @param v2 right member of comparison
1055 * @return success/failure of operation
1057 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1058 LttTime
* r
= (LttTime
*) v1
;
1059 // 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)));
1060 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1064 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1066 * Applies the 'greater or equal' operator to the
1067 * specified structure and value
1068 * @param v1 left member of comparison
1069 * @param v2 right member of comparison
1070 * @return success/failure of operation
1072 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1073 guint
* r
= (guint
*) v1
;
1074 return (*r
>= v2
.v_uint
);
1078 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1080 * Applies the 'greater or equal' operator to the
1081 * specified structure and value
1082 * @param v1 left member of comparison
1083 * @param v2 right member of comparison
1084 * @return success/failure of operation
1086 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1087 guint64
* r
= (guint64
*) v1
;
1088 return (*r
>= v2
.v_uint64
);
1092 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1094 * Applies the 'greater or equal' operator to the
1095 * specified structure and value
1096 * @param v1 left member of comparison
1097 * @param v2 right member of comparison
1098 * @return success/failure of operation
1100 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1101 guint32
* r
= (guint32
*) v1
;
1102 return (*r
>= v2
.v_uint32
);
1106 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1108 * Applies the 'greater or equal' operator to the
1109 * specified structure and value
1110 * @param v1 left member of comparison
1111 * @param v2 right member of comparison
1112 * @return success/failure of operation
1114 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1115 guint16
* r
= (guint16
*) v1
;
1116 return (*r
>= v2
.v_uint16
);
1120 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1122 * Applies the 'greater or equal' operator to the
1123 * specified structure and value
1124 * @param v1 left member of comparison
1125 * @param v2 right member of comparison
1126 * @return success/failure of operation
1128 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1129 double* r
= (double*) v1
;
1130 return (*r
>= v2
.v_double
);
1134 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1136 * Applies the 'greater or equal' operator to the
1137 * specified structure and value
1138 * @param v1 left member of comparison
1139 * @param v2 right member of comparison
1140 * @return success/failure of operation
1142 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1143 LttTime
* r
= (LttTime
*) v1
;
1144 // 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)));
1145 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1151 * Makes a copy of the current filter tree
1152 * @param tree pointer to the current tree
1153 * @return new copy of the filter tree
1156 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1158 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1160 newtree
->node
= tree
->node
;
1162 newtree
->left
= tree
->left
;
1163 if(newtree
->left
== LTTV_TREE_NODE
) {
1164 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1165 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1166 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1167 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1168 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1169 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1170 /* FIXME: special case for string copy ! */
1171 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1174 newtree
->right
= tree
->right
;
1175 if(newtree
->right
== LTTV_TREE_NODE
) {
1176 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1177 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1178 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1179 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1180 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1181 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1182 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1190 * Makes a copy of the current filter
1191 * @param filter pointer to the current filter
1192 * @return new copy of the filter
1195 lttv_filter_clone(const LttvFilter
* filter
) {
1197 if(!filter
) return NULL
;
1199 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1201 strcpy(newfilter
->expression
,filter
->expression
);
1203 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1211 * @fn LttvFilter* lttv_filter_new()
1213 * Creates a new LttvFilter
1214 * @return the current LttvFilter or NULL if error
1219 LttvFilter
* filter
= g_new(LttvFilter
,1);
1220 filter
->expression
= NULL
;
1221 filter
->head
= NULL
;
1228 * @fn gboolean lttv_filter_update(LttvFilter*)
1230 * Updates the current LttvFilter by building
1231 * its tree based upon the expression string
1232 * @param filter pointer to the current LttvFilter
1233 * @return Failure/Success of operation
1236 lttv_filter_update(LttvFilter
* filter
) {
1238 // g_print("filter::lttv_filter_new()\n"); /* debug */
1240 if(filter
->expression
== NULL
) return FALSE
;
1244 p_nesting
=0, /* parenthesis nesting value */
1249 *tree
= lttv_filter_tree_new(), /* main tree */
1250 *subtree
= NULL
, /* buffer for subtrees */
1251 *t1
, /* buffer #1 */
1252 *t2
, /* buffer #2 */
1253 *t3
; /* buffer #3 */
1257 * If the tree already exists,
1258 * destroy it and build a new one
1260 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1261 filter
->head
= NULL
; /* will be assigned at the end */
1265 * each element of the list
1266 * is a sub tree created
1267 * by the use of parenthesis in the
1268 * global expression. The final tree
1269 * will be the one left at the root of
1272 GPtrArray
*tree_stack
= g_ptr_array_new();
1273 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1275 /* temporary values */
1276 GString
*a_field_component
= g_string_new("");
1277 GString
*a_string_spaces
= g_string_new("");
1278 GPtrArray
*a_field_path
= g_ptr_array_new();
1280 /* simple expression buffer */
1281 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1284 * Parse entire expression and construct
1285 * the binary tree. There are two steps
1286 * in browsing that string
1287 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1288 * 2. finding simple expressions
1289 * - field path ( separated by dots )
1290 * - op ( >, <, =, >=, <=, !=)
1291 * - value ( integer, string ... )
1292 * To spare computing time, the whole
1293 * string is parsed in this loop for a
1294 * O(n) complexity order.
1296 * When encountering logical op &,|,^
1297 * 1. parse the last value if any
1298 * 2. create a new tree
1299 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1300 * 4. concatenate this tree with the current tree on top of the stack
1301 * When encountering math ops >,>=,<,<=,=,!=
1302 * 1. add to op to the simple expression
1303 * 2. concatenate last field component to field path
1304 * When encountering concatening ops .
1305 * 1. concatenate last field component to field path
1306 * When encountering opening parenthesis (,{,[
1307 * 1. create a new subtree on top of tree stack
1308 * When encountering closing parenthesis ),},]
1309 * 1. add the expression on right child of the current tree
1310 * 2. the subtree is completed, allocate a new subtree
1311 * 3. pop the tree value from the tree stack
1315 struct timeval starttime
;
1316 struct timeval endtime
;
1317 gettimeofday(&starttime
, NULL
);
1320 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1322 // g_print("%c\n ",filter->expression[i]);
1324 switch(filter
->expression
[i
]) {
1330 /* get current tree in tree stack */
1331 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1333 /* get current node at absolute right */
1334 while(t1
->right
!= LTTV_TREE_IDLE
) {
1335 g_assert(t1
->right
== LTTV_TREE_NODE
);
1338 t2
= lttv_filter_tree_new();
1339 t2
->node
= LTTV_LOGICAL_AND
;
1340 t1
->right
= LTTV_TREE_NODE
;
1342 if(not) { /* add not operator to tree */
1343 t3
= lttv_filter_tree_new();
1344 t3
->node
= LTTV_LOGICAL_NOT
;
1345 t2
->left
= LTTV_TREE_NODE
;
1350 if(subtree
!= NULL
) { /* append subtree to current tree */
1351 t2
->left
= LTTV_TREE_NODE
;
1352 t2
->l_child
.t
= subtree
;
1354 } else { /* append a simple expression */
1355 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1356 a_field_component
= g_string_new("");
1357 g_string_free(a_string_spaces
, TRUE
);
1358 a_string_spaces
= g_string_new("");
1359 t2
->left
= LTTV_TREE_LEAF
;
1360 t2
->l_child
.leaf
= a_simple_expression
;
1361 a_simple_expression
= lttv_simple_expression_new();
1367 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1368 while(t1
->right
!= LTTV_TREE_IDLE
) {
1369 g_assert(t1
->right
== LTTV_TREE_NODE
);
1372 t2
= lttv_filter_tree_new();
1373 t2
->node
= LTTV_LOGICAL_OR
;
1374 t1
->right
= LTTV_TREE_NODE
;
1376 if(not) { // add not operator to tree
1377 t3
= lttv_filter_tree_new();
1378 t3
->node
= LTTV_LOGICAL_NOT
;
1379 t2
->left
= LTTV_TREE_NODE
;
1384 if(subtree
!= NULL
) { /* append subtree to current tree */
1385 t2
->left
= LTTV_TREE_NODE
;
1386 t2
->l_child
.t
= subtree
;
1388 } else { /* append a simple expression */
1389 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1390 a_field_component
= g_string_new("");
1391 g_string_free(a_string_spaces
, TRUE
);
1392 a_string_spaces
= g_string_new("");
1393 t2
->left
= LTTV_TREE_LEAF
;
1394 t2
->l_child
.leaf
= a_simple_expression
;
1395 a_simple_expression
= lttv_simple_expression_new();
1401 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1402 while(t1
->right
!= LTTV_TREE_IDLE
) {
1403 g_assert(t1
->right
== LTTV_TREE_NODE
);
1406 t2
= lttv_filter_tree_new();
1407 t2
->node
= LTTV_LOGICAL_XOR
;
1408 t1
->right
= LTTV_TREE_NODE
;
1410 if(not) { // add not operator to tree
1411 t3
= lttv_filter_tree_new();
1412 t3
->node
= LTTV_LOGICAL_NOT
;
1413 t2
->left
= LTTV_TREE_NODE
;
1418 if(subtree
!= NULL
) { /* append subtree to current tree */
1419 t2
->left
= LTTV_TREE_NODE
;
1420 t2
->l_child
.t
= subtree
;
1422 } else { /* append a simple expression */
1423 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1424 a_field_component
= g_string_new("");
1425 g_string_free(a_string_spaces
, TRUE
);
1426 a_string_spaces
= g_string_new("");
1427 t2
->left
= LTTV_TREE_LEAF
;
1428 t2
->l_child
.leaf
= a_simple_expression
;
1429 a_simple_expression
= lttv_simple_expression_new();
1433 case '!': /* not, or not equal (math op) */
1435 if(filter
->expression
[i
+1] == '=') { /* != */
1436 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1437 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1438 a_field_component
= g_string_new("");
1439 g_string_free(a_string_spaces
, TRUE
);
1440 a_string_spaces
= g_string_new("");
1441 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1448 case '(': /* start of parenthesis */
1452 p_nesting
++; /* incrementing parenthesis nesting value */
1453 t1
= lttv_filter_tree_new();
1454 if(not) { /* add not operator to tree */
1455 t3
= lttv_filter_tree_new();
1456 t3
->node
= LTTV_LOGICAL_NOT
;
1457 t1
->right
= LTTV_TREE_NODE
;
1461 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1464 case ')': /* end of parenthesis */
1468 p_nesting
--; /* decrementing parenthesis nesting value */
1469 if(p_nesting
<0 || tree_stack
->len
<2) {
1470 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1471 is not valid due to parenthesis incorrect use",filter
->expression
);
1475 /* there must at least be the root tree left in the array */
1476 g_assert(tree_stack
->len
>0);
1478 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1479 while(t1
->right
!= LTTV_TREE_IDLE
) {
1482 if(not) { // add not operator to tree
1484 t3
= lttv_filter_tree_new();
1485 t3
->node
= LTTV_LOGICAL_NOT
;
1486 t1
->right
= LTTV_TREE_NODE
;
1491 if(subtree
!= NULL
) { /* append subtree to current tree */
1492 t1
->right
= LTTV_TREE_NODE
;
1493 t1
->r_child
.t
= subtree
;
1494 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1495 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1496 } else { /* assign subtree as current tree */
1497 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1498 a_field_component
= g_string_new("");
1499 g_string_free(a_string_spaces
, TRUE
);
1500 a_string_spaces
= g_string_new("");
1501 t1
->right
= LTTV_TREE_LEAF
;
1502 t1
->r_child
.leaf
= a_simple_expression
;
1503 a_simple_expression
= lttv_simple_expression_new();
1504 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1509 * mathematic operators
1511 case '<': /* lower, lower or equal */
1513 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1514 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1515 a_field_component
= g_string_new("");
1516 g_string_free(a_string_spaces
, TRUE
);
1517 a_string_spaces
= g_string_new("");
1518 if(filter
->expression
[i
+1] == '=') { /* <= */
1520 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1521 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1524 case '>': /* higher, higher or equal */
1526 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1527 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1528 a_field_component
= g_string_new("");
1529 g_string_free(a_string_spaces
, TRUE
);
1530 a_string_spaces
= g_string_new("");
1531 if(filter
->expression
[i
+1] == '=') { /* >= */
1533 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1534 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1537 case '=': /* equal */
1539 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1540 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1541 a_field_component
= g_string_new("");
1542 g_string_free(a_string_spaces
, TRUE
);
1543 a_string_spaces
= g_string_new("");
1544 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1548 * Field concatening caracter
1553 * divide field expression into elements
1554 * in a_field_path array.
1556 * A dot can also be present in double values
1558 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1559 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1560 a_field_component
= g_string_new("");
1561 g_string_free(a_string_spaces
, TRUE
);
1562 a_string_spaces
= g_string_new("");
1565 case ' ': /* keep spaces that are within a field component */
1566 if(a_field_component
->len
== 0) break; /* ignore */
1568 a_string_spaces
= g_string_append_c(a_string_spaces
,
1569 filter
->expression
[i
]);
1571 case '\n': /* ignore */
1573 default: /* concatening current string */
1574 if(a_string_spaces
->len
!= 0) {
1575 a_field_component
= g_string_append(
1576 a_field_component
, a_string_spaces
->str
);
1577 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1579 a_field_component
= g_string_append_c(a_field_component
,
1580 filter
->expression
[i
]);
1585 * Preliminary check to see
1586 * if tree was constructed correctly
1589 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1590 is not valid due to parenthesis incorrect use",filter
->expression
);
1594 if(tree_stack
->len
!= 1) /* only root tree should remain */
1598 * processing last element of expression
1600 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1601 while(t1
->right
!= LTTV_TREE_IDLE
) {
1602 g_assert(t1
->right
== LTTV_TREE_NODE
);
1605 if(not) { // add not operator to tree
1606 t3
= lttv_filter_tree_new();
1607 t3
->node
= LTTV_LOGICAL_NOT
;
1608 t1
->right
= LTTV_TREE_NODE
;
1613 if(subtree
!= NULL
) { /* add the subtree */
1614 t1
->right
= LTTV_TREE_NODE
;
1615 t1
->r_child
.t
= subtree
;
1617 } else { /* add a leaf */
1618 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1619 a_field_component
= NULL
;
1620 g_string_free(a_string_spaces
, TRUE
);
1621 a_string_spaces
= NULL
;
1622 t1
->right
= LTTV_TREE_LEAF
;
1623 t1
->r_child
.leaf
= a_simple_expression
;
1624 a_simple_expression
= NULL
;
1628 /* free the pointer array */
1629 g_assert(a_field_path
->len
== 0);
1630 g_ptr_array_free(a_field_path
,TRUE
);
1632 /* free the tree stack -- but keep the root tree */
1633 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1634 g_ptr_array_free(tree_stack
,TRUE
);
1636 /* free the field buffer if allocated */
1637 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1638 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1640 /* free the simple expression buffer if allocated */
1641 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1643 g_assert(filter
->head
!= NULL
); /* tree should exist */
1644 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1647 gettimeofday(&endtime
, NULL
);
1649 /* Calcul du temps de l'algorithme */
1650 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1651 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1652 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1653 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1657 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1658 lttv_print_tree(filter
->head
,0) ;
1659 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1667 * @fn void lttv_filter_destroy(LttvFilter*)
1669 * Destroy the current LttvFilter
1670 * @param filter pointer to the current LttvFilter
1673 lttv_filter_destroy(LttvFilter
* filter
) {
1677 if(filter
->expression
)
1678 g_free(filter
->expression
);
1680 lttv_filter_tree_destroy(filter
->head
);
1686 * @fn LttvFilterTree* lttv_filter_tree_new()
1688 * Assign a new tree for the current expression
1690 * @return pointer of LttvFilterTree
1693 lttv_filter_tree_new() {
1694 LttvFilterTree
* tree
;
1696 tree
= g_new(LttvFilterTree
,1);
1697 tree
->node
= 0; //g_new(lttv_expression,1);
1698 tree
->left
= LTTV_TREE_IDLE
;
1699 tree
->right
= LTTV_TREE_IDLE
;
1700 tree
->r_child
.t
= NULL
;
1701 tree
->l_child
.t
= NULL
;
1707 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1709 * Append a new expression to the expression
1710 * defined in the current filter
1711 * @param filter pointer to the current LttvFilter
1712 * @param expression string that must be appended
1713 * @return Success/Failure of operation
1716 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1718 if(expression
== NULL
) return FALSE
;
1719 if(filter
== NULL
) return FALSE
;
1720 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1722 GString
* s
= g_string_new("");
1723 if(filter
->expression
!= NULL
) {
1724 s
= g_string_append(s
,filter
->expression
);
1725 s
= g_string_append_c(s
,'&');
1727 s
= g_string_append(s
,expression
);
1729 g_free(filter
->expression
);
1730 filter
->expression
= g_string_free(s
,FALSE
);
1732 /* TRUE if construction of tree proceeded without errors */
1733 return lttv_filter_update(filter
);
1738 * @fn void lttv_filter_clear_expression(LttvFilter*)
1740 * Clear the filter expression from the
1741 * current filter and sets its pointer to NULL
1742 * @param filter pointer to the current LttvFilter
1745 lttv_filter_clear_expression(LttvFilter
* filter
) {
1747 if(filter
->expression
!= NULL
) {
1748 g_free(filter
->expression
);
1749 filter
->expression
= NULL
;
1755 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1757 * Destroys the tree and his sub-trees
1758 * @param tree Tree which must be destroyed
1761 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1763 if(tree
== NULL
) return;
1765 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1766 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1768 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1769 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1771 // g_free(tree->node);
1776 * Global parsing function for the current
1778 * @param t pointer to the current LttvFilterTree
1779 * @param event current LttEvent, NULL if not used
1780 * @param tracefile current LttTracefile, NULL if not used
1781 * @param trace current LttTrace, NULL if not used
1782 * @param state current LttvProcessState, NULL if not used
1783 * @param context current LttvTracefileContext, NULL if not used
1784 * @return response of filter
1787 lttv_filter_tree_parse(
1788 const LttvFilterTree
* t
,
1789 const LttEvent
* event
,
1790 const LttTracefile
* tracefile
,
1791 const LttTrace
* trace
,
1792 const LttvTracefileContext
* context
1797 * Each tree is parsed in inorder.
1798 * This way, it's possible to apply the left filter of the
1799 * tree, then decide whether or not the right branch should
1800 * be parsed depending on the linking logical operator
1802 * Each node consists in a
1803 * 1. logical operator
1804 * 2. left child ( node or simple expression )
1805 * 3. right child ( node or simple expression )
1807 * When the child is a simple expression, we must
1808 * before all determine if the expression refers to
1809 * a structure which is whithin observation ( not NULL ).
1810 * -If so, the expression is evaluated.
1811 * -If not, the result is set to TRUE since this particular
1812 * operation does not interfere with the lttv structure
1814 * The result of each simple expression will directly
1815 * affect the next branch. This way, depending on
1816 * the linking logical operator, the parser will decide
1817 * to explore or not the next branch.
1819 * -If result of left branch is 0 / FALSE
1820 * then don't explore right branch and return 0;
1821 * -If result of left branch is 1 / TRUE then explore
1823 * -If result of left branch is 1 / TRUE
1824 * then don't explore right branch and return 1;
1825 * -If result of left branch is 0 / FALSE then explore
1827 * -Result of left branch will not affect exploration of
1831 gboolean lresult
= FALSE
, rresult
= FALSE
;
1833 LttvProcessState
* state
;
1835 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1836 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1837 guint cpu
= tfs
->cpu
;
1838 state
= ts
->running_process
[cpu
];
1843 if(t
->left
== LTTV_TREE_NODE
) {
1844 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1846 else if(t
->left
== LTTV_TREE_LEAF
) {
1847 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1851 * Parse linking operator
1852 * make a cutoff if possible
1854 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1855 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1858 * Parse right branch
1860 if(t
->right
== LTTV_TREE_NODE
) {
1861 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1863 else if(t
->right
== LTTV_TREE_LEAF
) {
1864 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1869 * Apply and return the
1870 * logical link between the
1874 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1875 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1876 case LTTV_LOGICAL_NOT
:
1877 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1878 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1879 case 0: return (rresult
);
1882 * This case should never be
1883 * parsed, if so, this subtree
1892 * This function parses a particular branch of the tree
1893 * @param se pointer to the current LttvSimpleExpression
1894 * @param event current LttEvent, NULL if not used
1895 * @param tracefile current LttTracefile, NULL if not used
1896 * @param trace current LttTrace, NULL if not used
1897 * @param state current LttvProcessState, NULL if not used
1898 * @param context current LttvTracefileContext, NULL if not used
1899 * @return response of filter
1902 lttv_filter_tree_parse_branch(
1903 const LttvSimpleExpression
* se
,
1904 const LttEvent
* event
,
1905 const LttTracefile
* tracefile
,
1906 const LttTrace
* trace
,
1907 const LttvProcessState
* state
,
1908 const LttvTracefileContext
* context
) {
1913 case LTTV_FILTER_TRACE_NAME
:
1914 if(trace
== NULL
) return TRUE
;
1916 GQuark quark
= ltt_trace_name(trace
);
1917 return se
->op((gpointer
)&quark
,v
);
1920 case LTTV_FILTER_TRACEFILE_NAME
:
1921 if(tracefile
== NULL
) return TRUE
;
1923 GQuark quark
= ltt_tracefile_name(tracefile
);
1924 return se
->op((gpointer
)&quark
,v
);
1927 case LTTV_FILTER_STATE_PID
:
1928 if(state
== NULL
) return TRUE
;
1929 else return se
->op((gpointer
)&state
->pid
,v
);
1931 case LTTV_FILTER_STATE_PPID
:
1932 if(state
== NULL
) return TRUE
;
1933 else return se
->op((gpointer
)&state
->ppid
,v
);
1935 case LTTV_FILTER_STATE_CT
:
1936 if(state
== NULL
) return TRUE
;
1938 return se
->op((gpointer
)&state
->creation_time
,v
);
1941 case LTTV_FILTER_STATE_IT
:
1942 if(state
== NULL
) return TRUE
;
1944 return se
->op((gpointer
)&state
->insertion_time
,v
);
1947 case LTTV_FILTER_STATE_P_NAME
:
1948 if(state
== NULL
) return TRUE
;
1950 GQuark quark
= state
->name
;
1951 return se
->op((gpointer
)&quark
,v
);
1954 case LTTV_FILTER_STATE_T_BRAND
:
1955 if(state
== NULL
) return TRUE
;
1957 GQuark quark
= state
->brand
;
1958 return se
->op((gpointer
)&quark
,v
);
1961 case LTTV_FILTER_STATE_EX_MODE
:
1962 if(state
== NULL
) return TRUE
;
1963 else return se
->op((gpointer
)&state
->state
->t
,v
);
1965 case LTTV_FILTER_STATE_EX_SUBMODE
:
1966 if(state
== NULL
) return TRUE
;
1967 else return se
->op((gpointer
)&state
->state
->n
,v
);
1969 case LTTV_FILTER_STATE_P_STATUS
:
1970 if(state
== NULL
) return TRUE
;
1971 else return se
->op((gpointer
)&state
->state
->s
,v
);
1973 case LTTV_FILTER_STATE_CPU
:
1974 if(context
== NULL
) return TRUE
;
1976 if(state
== NULL
) return TRUE
;
1977 else return se
->op((gpointer
)&state
->cpu
,v
);
1980 case LTTV_FILTER_EVENT_NAME
:
1981 if(event
== NULL
) return TRUE
;
1984 et
= ltt_event_eventtype(event
);
1985 GQuark quark
= ltt_eventtype_name(et
);
1986 return se
->op((gpointer
)&quark
,v
);
1989 case LTTV_FILTER_EVENT_FACILITY
:
1990 if(event
== NULL
) return TRUE
;
1993 fac
= ltt_event_facility(event
);
1994 GQuark quark
= ltt_facility_name(fac
);
1995 return se
->op((gpointer
)&quark
,v
);
1998 case LTTV_FILTER_EVENT_CATEGORY
:
2000 * TODO: Not yet implemented
2004 case LTTV_FILTER_EVENT_TIME
:
2005 if(event
== NULL
) return TRUE
;
2007 LttTime time
= ltt_event_time(event
);
2008 return se
->op((gpointer
)&time
,v
);
2011 case LTTV_FILTER_EVENT_TSC
:
2012 if(event
== NULL
) return TRUE
;
2014 LttCycleCount count
= ltt_event_cycle_count(event
);
2015 return se
->op((gpointer
)&count
,v
);
2018 case LTTV_FILTER_EVENT_FIELD
:
2020 * TODO: Use the offset to
2021 * find the dynamic field
2022 * in the event struct
2027 * This case should never be
2028 * parsed, if so, the whole
2029 * filtering is cancelled
2031 g_warning("Error while parsing the filter tree");
2035 /* should never get here */
2043 * Debug function. Prints tree memory allocation.
2044 * @param t the pointer to the current LttvFilterTree
2047 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2049 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2050 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2051 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2053 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2054 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"));
2055 if(t
->left
== LTTV_TREE_LEAF
) {
2056 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2057 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2058 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2060 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"));
2061 if(t
->right
== LTTV_TREE_LEAF
) {
2062 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2063 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2064 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2067 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2068 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2072 * @fn static void module_init()
2074 * Initializes the filter module and specific values
2076 static void module_init()
2082 * Destroys the filter module and specific values
2084 static void module_destroy()
2090 LTTV_MODULE("filter", "Filters traceset and events", \
2091 "Filters traceset and events specifically to user input", \
2092 module_init
, module_destroy
)