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)
38 * | |->target_pid (target PID of the event)
43 * | |->"sub-field name"
45 * | |->"leaf-field name" (field type)
47 * | |->name (String, converted to GQuark)
49 * | |->name (String, converted to GQuark)
53 * |->creation_time (LttTime)
54 * |->insertion_time (LttTime)
55 * |->process_name (String, converted to GQuark)
56 * |->thread_brand (String, converted to GQuark)
57 * |->execution_mode (LttvExecutionMode)
58 * |->execution_submode (LttvExecutionSubmode)
59 * |->process_status (LttvProcessStatus)
66 * - refine switch of expression in multiple uses functions
67 * - remove the idle expressions in the tree
80 #include <lttv/lttv.h>
81 #include <lttv/filter.h>
82 #include <ltt/trace.h>
84 #include <ltt/facility.h>
89 * @fn LttvSimpleExpression* lttv_simple_expression_new()
91 * Constructor for LttvSimpleExpression
92 * @return pointer to new LttvSimpleExpression
95 lttv_simple_expression_new() {
97 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
99 se
->field
= LTTV_FILTER_UNDEFINED
;
107 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
109 * Parse through filtering field hierarchy as specified
110 * by user. This function compares each value to
111 * predetermined quarks
112 * @param fp The field path list
113 * @param se current simple expression
114 * @return success/failure of operation
117 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
121 if(fp
->len
< 2) return FALSE
;
122 g_assert((f
=g_ptr_array_remove_index(fp
,0)));
125 * Parse through the specified
128 * Take note however that the
129 * 'event' subfields might change
130 * depending on values specified
131 * in core.xml file. Hence, if
132 * none of the subfields in the
133 * array match the hardcoded
134 * subfields, it will be considered
137 if(!g_strcasecmp(f
->str
,"trace") ) {
142 g_string_free(f
,TRUE
);
143 f
=g_ptr_array_remove_index(fp
,0);
144 if(!g_strcasecmp(f
->str
,"name")) {
145 se
->field
= LTTV_FILTER_TRACE_NAME
;
147 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
149 * FIXME: not yet implemented !
151 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
156 g_string_free(f
,TRUE
);
157 f
=g_ptr_array_remove_index(fp
,0);
158 if(!g_strcasecmp(f
->str
,"name")) {
159 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
161 } else if(!g_strcasecmp(f
->str
,"state") ) {
166 * state.creation_time
167 * state.insertion_time
170 * state.execution_mode
171 * state.execution_submode
172 * state.process_status
175 g_string_free(f
,TRUE
);
176 f
=g_ptr_array_remove_index(fp
,0);
177 if(!g_strcasecmp(f
->str
,"pid") ) {
178 se
->field
= LTTV_FILTER_STATE_PID
;
180 else if(!g_strcasecmp(f
->str
,"ppid") ) {
181 se
->field
= LTTV_FILTER_STATE_PPID
;
183 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
184 se
->field
= LTTV_FILTER_STATE_CT
;
186 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
187 se
->field
= LTTV_FILTER_STATE_IT
;
189 else if(!g_strcasecmp(f
->str
,"process_name") ) {
190 se
->field
= LTTV_FILTER_STATE_P_NAME
;
192 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
193 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
195 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
196 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
198 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
199 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
201 else if(!g_strcasecmp(f
->str
,"process_status") ) {
202 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
204 else if(!g_strcasecmp(f
->str
,"cpu") ) {
205 se
->field
= LTTV_FILTER_STATE_CPU
;
207 } else if(!g_strcasecmp(f
->str
,"event") ) {
217 g_string_free(f
,TRUE
);
218 f
=g_ptr_array_remove_index(fp
,0);
219 if(!g_strcasecmp(f
->str
,"name") ) {
220 se
->field
= LTTV_FILTER_EVENT_NAME
;
222 else if(!g_strcasecmp(f
->str
,"facility") ) {
223 se
->field
= LTTV_FILTER_EVENT_FACILITY
;
225 else if(!g_strcasecmp(f
->str
,"category") ) {
227 * FIXME: Category not yet functional in lttv
229 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
231 else if(!g_strcasecmp(f
->str
,"time") ) {
232 se
->field
= LTTV_FILTER_EVENT_TIME
;
234 else if(!g_strcasecmp(f
->str
,"tsc") ) {
235 se
->field
= LTTV_FILTER_EVENT_TSC
;
237 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
238 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
240 else if(!g_strcasecmp(f
->str
,"field") ) {
241 se
->field
= LTTV_FILTER_EVENT_FIELD
;
242 g_string_free(f
,TRUE
);
243 f
=g_ptr_array_remove_index(fp
,0);
246 g_string_free(f
,TRUE
);
247 f
=g_ptr_array_remove_index(fp
,0);
248 g_warning("Unknown event filter subtype %s", f
->str
);
251 g_string_free(f
,TRUE
);
252 f
=g_ptr_array_remove_index(fp
,0);
254 g_warning("Unrecognized field in filter string");
257 /* free memory for last string */
258 g_string_free(f
,TRUE
);
260 /* array should be empty */
261 g_assert(fp
->len
== 0);
263 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
264 g_warning("The specified field was not recognized !");
271 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
273 * Sets the function pointer for the current
275 * @param se current simple expression
276 * @param op current operator
277 * @return success/failure of operation
280 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
286 case LTTV_FILTER_TRACE_NAME
:
287 case LTTV_FILTER_TRACEFILE_NAME
:
288 case LTTV_FILTER_STATE_P_NAME
:
289 case LTTV_FILTER_STATE_T_BRAND
:
290 case LTTV_FILTER_EVENT_NAME
:
291 case LTTV_FILTER_EVENT_FACILITY
:
292 case LTTV_FILTER_STATE_EX_MODE
:
293 case LTTV_FILTER_STATE_EX_SUBMODE
:
294 case LTTV_FILTER_STATE_P_STATUS
:
297 se
->op
= lttv_apply_op_eq_quark
;
300 se
->op
= lttv_apply_op_ne_quark
;
303 g_warning("Error encountered in operator assignment = or != expected");
310 case LTTV_FILTER_EVENT_TSC
:
313 se
->op
= lttv_apply_op_eq_uint64
;
316 se
->op
= lttv_apply_op_ne_uint64
;
319 se
->op
= lttv_apply_op_lt_uint64
;
322 se
->op
= lttv_apply_op_le_uint64
;
325 se
->op
= lttv_apply_op_gt_uint64
;
328 se
->op
= lttv_apply_op_ge_uint64
;
331 g_warning("Error encountered in operator assignment");
338 case LTTV_FILTER_STATE_CPU
:
339 case LTTV_FILTER_STATE_PID
:
340 case LTTV_FILTER_STATE_PPID
:
341 case LTTV_FILTER_EVENT_TARGET_PID
:
344 se
->op
= lttv_apply_op_eq_uint
;
347 se
->op
= lttv_apply_op_ne_uint
;
350 se
->op
= lttv_apply_op_lt_uint
;
353 se
->op
= lttv_apply_op_le_uint
;
356 se
->op
= lttv_apply_op_gt_uint
;
359 se
->op
= lttv_apply_op_ge_uint
;
362 g_warning("Error encountered in operator assignment");
369 * Entered as string, converted to enum
371 * can only be compared with 'equal' or 'not equal' operators
373 * unsigned int of 16 bits are used here since enums
374 * should not go over 2^16-1 values
378 // case LTTV_FIELD_EQ:
379 // se->op = lttv_apply_op_eq_uint16;
381 // case LTTV_FIELD_NE:
382 // se->op = lttv_apply_op_ne_uint16;
385 // g_warning("Error encountered in operator assignment = or != expected");
392 case LTTV_FILTER_STATE_CT
:
393 case LTTV_FILTER_STATE_IT
:
394 case LTTV_FILTER_EVENT_TIME
:
397 se
->op
= lttv_apply_op_eq_ltttime
;
400 se
->op
= lttv_apply_op_ne_ltttime
;
403 se
->op
= lttv_apply_op_lt_ltttime
;
406 se
->op
= lttv_apply_op_le_ltttime
;
409 se
->op
= lttv_apply_op_gt_ltttime
;
412 se
->op
= lttv_apply_op_ge_ltttime
;
415 g_warning("Error encountered in operator assignment");
420 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
429 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
431 * Assign the value field to the current LttvSimpleExpression
432 * @param se pointer to the current LttvSimpleExpression
433 * @param value string value for simple expression
436 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
439 gboolean is_double
= FALSE
;
440 LttTime t
= ltt_time_zero
;
447 * entered as strings, converted to Quarks
449 case LTTV_FILTER_TRACE_NAME
:
450 case LTTV_FILTER_TRACEFILE_NAME
:
451 case LTTV_FILTER_STATE_P_NAME
:
452 case LTTV_FILTER_STATE_T_BRAND
:
453 case LTTV_FILTER_EVENT_NAME
:
454 case LTTV_FILTER_EVENT_FACILITY
:
455 case LTTV_FILTER_STATE_EX_MODE
:
456 case LTTV_FILTER_STATE_EX_SUBMODE
:
457 case LTTV_FILTER_STATE_P_STATUS
:
458 // se->value.v_string = value;
459 se
->value
.v_quark
= g_quark_from_string(value
);
463 * integer -- supposed to be uint64
465 case LTTV_FILTER_EVENT_TSC
:
466 se
->value
.v_uint64
= atoi(value
);
472 case LTTV_FILTER_STATE_PID
:
473 case LTTV_FILTER_STATE_PPID
:
474 case LTTV_FILTER_STATE_CPU
:
475 case LTTV_FILTER_EVENT_TARGET_PID
:
476 se
->value
.v_uint
= atoi(value
);
482 case LTTV_FILTER_STATE_CT
:
483 case LTTV_FILTER_STATE_IT
:
484 case LTTV_FILTER_EVENT_TIME
:
485 //se->value.v_double = atof(value);
487 * parsing logic could be optimised,
488 * but as for now, simpler this way
490 v
= g_string_new("");
491 string_len
= strlen(value
);
492 for(i
=0;i
<string_len
;i
++) {
493 if(value
[i
] == '.') {
494 /* cannot specify number with more than one '.' */
495 if(is_double
) return FALSE
;
496 else is_double
= TRUE
;
497 t
.tv_sec
= atoi(v
->str
);
498 g_string_free(v
,TRUE
);
499 v
= g_string_new("");
500 } else v
= g_string_append_c(v
,value
[i
]);
502 /* number can be integer or double */
503 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
505 t
.tv_sec
= atoi(v
->str
);
509 g_string_free(v
,TRUE
);
511 se
->value
.v_ltttime
= t
;
515 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
525 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
527 * Disallocate memory for the current
529 * @param se pointer to the current LttvSimpleExpression
532 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
534 // g_free(se->value);
535 // switch(se->field) {
536 // case LTTV_FILTER_TRACE_NAME:
537 // case LTTV_FILTER_TRACEFILE_NAME:
538 // case LTTV_FILTER_STATE_P_NAME:
539 // case LTTV_FILTER_EVENT_NAME:
540 // g_free(se->value.v_string);
548 * @fn gint lttv_struct_type(gint)
550 * Finds the structure type depending
551 * on the fields in parameters
552 * @params ft Field of the current structure
553 * @return LttvStructType enum or -1 for error
556 lttv_struct_type(gint ft
) {
559 case LTTV_FILTER_TRACE_NAME
:
560 return LTTV_FILTER_TRACE
;
562 case LTTV_FILTER_TRACEFILE_NAME
:
563 return LTTV_FILTER_TRACEFILE
;
565 case LTTV_FILTER_STATE_PID
:
566 case LTTV_FILTER_STATE_PPID
:
567 case LTTV_FILTER_STATE_CT
:
568 case LTTV_FILTER_STATE_IT
:
569 case LTTV_FILTER_STATE_P_NAME
:
570 case LTTV_FILTER_STATE_T_BRAND
:
571 case LTTV_FILTER_STATE_EX_MODE
:
572 case LTTV_FILTER_STATE_EX_SUBMODE
:
573 case LTTV_FILTER_STATE_P_STATUS
:
574 case LTTV_FILTER_STATE_CPU
:
575 return LTTV_FILTER_STATE
;
577 case LTTV_FILTER_EVENT_NAME
:
578 case LTTV_FILTER_EVENT_FACILITY
:
579 case LTTV_FILTER_EVENT_CATEGORY
:
580 case LTTV_FILTER_EVENT_TIME
:
581 case LTTV_FILTER_EVENT_TSC
:
582 case LTTV_FILTER_EVENT_TARGET_PID
:
583 case LTTV_FILTER_EVENT_FIELD
:
584 return LTTV_FILTER_EVENT
;
592 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
594 * Applies the 'equal' operator to the
595 * specified structure and value
596 * @param v1 left member of comparison
597 * @param v2 right member of comparison
598 * @return success/failure of operation
600 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
602 guint
* r
= (guint
*) v1
;
603 return (*r
== v2
.v_uint
);
608 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
610 * Applies the 'equal' operator to the
611 * specified structure and value
612 * @param v1 left member of comparison
613 * @param v2 right member of comparison
614 * @return success/failure of operation
616 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
618 guint64
* r
= (guint64
*) v1
;
619 return (*r
== v2
.v_uint64
);
624 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
626 * Applies the 'equal' operator to the
627 * specified structure and value
628 * @param v1 left member of comparison
629 * @param v2 right member of comparison
630 * @return success/failure of operation
632 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
633 guint32
* r
= (guint32
*) v1
;
634 return (*r
== v2
.v_uint32
);
638 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
640 * Applies the 'equal' operator to the
641 * specified structure and value
642 * @param v1 left member of comparison
643 * @param v2 right member of comparison
644 * @return success/failure of operation
646 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
647 guint16
* r
= (guint16
*) v1
;
648 return (*r
== v2
.v_uint16
);
652 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
654 * Applies the 'equal' operator to the
655 * specified structure and value
656 * @param v1 left member of comparison
657 * @param v2 right member of comparison
658 * @return success/failure of operation
660 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
661 double* r
= (double*) v1
;
662 return (*r
== v2
.v_double
);
666 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
668 * Applies the 'equal' operator to the
669 * specified structure and value
670 * @param v1 left member of comparison
671 * @param v2 right member of comparison
672 * @return success/failure of operation
674 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
675 char* r
= (char*) v1
;
676 return (!g_strcasecmp(r
,v2
.v_string
));
680 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
682 * Applies the 'equal' operator to the
683 * specified structure and value
684 * @param v1 left member of comparison
685 * @param v2 right member of comparison
686 * @return success/failure of operation
688 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
689 GQuark
* r
= (GQuark
*) v1
;
690 return (*r
== v2
.v_quark
);
694 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
696 * Applies the 'equal' operator to the
697 * specified structure and value
698 * @param v1 left member of comparison
699 * @param v2 right member of comparison
700 * @return success/failure of operation
702 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
703 LttTime
* r
= (LttTime
*) v1
;
704 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
708 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
710 * Applies the 'not equal' operator to the
711 * specified structure and value
712 * @param v1 left member of comparison
713 * @param v2 right member of comparison
714 * @return success/failure of operation
716 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
717 guint
* r
= (guint
*) v1
;
718 return (*r
!= v2
.v_uint
);
722 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
724 * Applies the 'not equal' operator to the
725 * specified structure and value
726 * @param v1 left member of comparison
727 * @param v2 right member of comparison
728 * @return success/failure of operation
730 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
731 guint64
* r
= (guint64
*) v1
;
732 return (*r
!= v2
.v_uint64
);
736 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
738 * Applies the 'not equal' operator to the
739 * specified structure and value
740 * @param v1 left member of comparison
741 * @param v2 right member of comparison
742 * @return success/failure of operation
744 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
745 guint32
* r
= (guint32
*) v1
;
746 return (*r
!= v2
.v_uint32
);
750 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
752 * Applies the 'not equal' operator to the
753 * specified structure and value
754 * @param v1 left member of comparison
755 * @param v2 right member of comparison
756 * @return success/failure of operation
758 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
759 guint16
* r
= (guint16
*) v1
;
760 return (*r
!= v2
.v_uint16
);
764 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
766 * Applies the 'not equal' operator to the
767 * specified structure and value
768 * @param v1 left member of comparison
769 * @param v2 right member of comparison
770 * @return success/failure of operation
772 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
773 double* r
= (double*) v1
;
774 return (*r
!= v2
.v_double
);
778 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
780 * Applies the 'not equal' operator to the
781 * specified structure and value
782 * @param v1 left member of comparison
783 * @param v2 right member of comparison
784 * @return success/failure of operation
786 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
787 char* r
= (char*) v1
;
788 return (g_strcasecmp(r
,v2
.v_string
));
792 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
794 * Applies the 'not equal' operator to the
795 * specified structure and value
796 * @param v1 left member of comparison
797 * @param v2 right member of comparison
798 * @return success/failure of operation
800 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
801 GQuark
* r
= (GQuark
*) v1
;
802 return (*r
!= v2
.v_quark
);
807 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
809 * Applies the 'not equal' operator to the
810 * specified structure and value
811 * @param v1 left member of comparison
812 * @param v2 right member of comparison
813 * @return success/failure of operation
815 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
816 LttTime
* r
= (LttTime
*) v1
;
817 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
821 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
823 * Applies the 'lower than' operator to the
824 * specified structure and value
825 * @param v1 left member of comparison
826 * @param v2 right member of comparison
827 * @return success/failure of operation
829 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
830 guint
* r
= (guint
*) v1
;
831 return (*r
< v2
.v_uint
);
835 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
837 * Applies the 'lower than' operator to the
838 * specified structure and value
839 * @param v1 left member of comparison
840 * @param v2 right member of comparison
841 * @return success/failure of operation
843 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
844 guint64
* r
= (guint64
*) v1
;
845 return (*r
< v2
.v_uint64
);
849 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
851 * Applies the 'lower than' operator to the
852 * specified structure and value
853 * @param v1 left member of comparison
854 * @param v2 right member of comparison
855 * @return success/failure of operation
857 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
858 guint32
* r
= (guint32
*) v1
;
859 return (*r
< v2
.v_uint32
);
863 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
865 * Applies the 'lower than' operator to the
866 * specified structure and value
867 * @param v1 left member of comparison
868 * @param v2 right member of comparison
869 * @return success/failure of operation
871 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
872 guint16
* r
= (guint16
*) v1
;
873 return (*r
< v2
.v_uint16
);
877 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
879 * Applies the 'lower than' operator to the
880 * specified structure and value
881 * @param v1 left member of comparison
882 * @param v2 right member of comparison
883 * @return success/failure of operation
885 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
886 double* r
= (double*) v1
;
887 return (*r
< v2
.v_double
);
891 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
893 * Applies the 'lower than' operator to the
894 * specified structure and value
895 * @param v1 left member of comparison
896 * @param v2 right member of comparison
897 * @return success/failure of operation
899 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
900 LttTime
* r
= (LttTime
*) v1
;
901 // 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)));
902 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
906 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
908 * Applies the 'lower or equal' operator to the
909 * specified structure and value
910 * @param v1 left member of comparison
911 * @param v2 right member of comparison
912 * @return success/failure of operation
914 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
915 guint
* r
= (guint
*) v1
;
916 return (*r
<= v2
.v_uint
);
920 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
922 * Applies the 'lower or equal' operator to the
923 * specified structure and value
924 * @param v1 left member of comparison
925 * @param v2 right member of comparison
926 * @return success/failure of operation
928 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
929 guint64
* r
= (guint64
*) v1
;
930 return (*r
<= v2
.v_uint64
);
934 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
936 * Applies the 'lower or equal' operator to the
937 * specified structure and value
938 * @param v1 left member of comparison
939 * @param v2 right member of comparison
940 * @return success/failure of operation
942 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
943 guint32
* r
= (guint32
*) v1
;
944 return (*r
<= v2
.v_uint32
);
948 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
950 * Applies the 'lower or equal' operator to the
951 * specified structure and value
952 * @param v1 left member of comparison
953 * @param v2 right member of comparison
954 * @return success/failure of operation
956 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
957 guint16
* r
= (guint16
*) v1
;
958 return (*r
<= v2
.v_uint16
);
962 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
964 * Applies the 'lower or equal' operator to the
965 * specified structure and value
966 * @param v1 left member of comparison
967 * @param v2 right member of comparison
968 * @return success/failure of operation
970 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
971 double* r
= (double*) v1
;
972 return (*r
<= v2
.v_double
);
976 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
978 * Applies the 'lower or equal' operator to the
979 * specified structure and value
980 * @param v1 left member of comparison
981 * @param v2 right member of comparison
982 * @return success/failure of operation
984 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
985 LttTime
* r
= (LttTime
*) v1
;
986 // 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)));
987 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
992 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
994 * Applies the 'greater than' operator to the
995 * specified structure and value
996 * @param v1 left member of comparison
997 * @param v2 right member of comparison
998 * @return success/failure of operation
1000 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1001 guint
* r
= (guint
*) v1
;
1002 return (*r
> v2
.v_uint
);
1006 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1008 * Applies the 'greater than' operator to the
1009 * specified structure and value
1010 * @param v1 left member of comparison
1011 * @param v2 right member of comparison
1012 * @return success/failure of operation
1014 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1015 guint64
* r
= (guint64
*) v1
;
1016 return (*r
> v2
.v_uint64
);
1020 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1022 * Applies the 'greater than' operator to the
1023 * specified structure and value
1024 * @param v1 left member of comparison
1025 * @param v2 right member of comparison
1026 * @return success/failure of operation
1028 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1029 guint32
* r
= (guint32
*) v1
;
1030 return (*r
> v2
.v_uint32
);
1034 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1036 * Applies the 'greater than' operator to the
1037 * specified structure and value
1038 * @param v1 left member of comparison
1039 * @param v2 right member of comparison
1040 * @return success/failure of operation
1042 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1043 guint16
* r
= (guint16
*) v1
;
1044 return (*r
> v2
.v_uint16
);
1048 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1050 * Applies the 'greater than' operator to the
1051 * specified structure and value
1052 * @param v1 left member of comparison
1053 * @param v2 right member of comparison
1054 * @return success/failure of operation
1056 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1057 double* r
= (double*) v1
;
1058 return (*r
> v2
.v_double
);
1062 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1064 * Applies the 'greater than' operator to the
1065 * specified structure and value
1066 * @param v1 left member of comparison
1067 * @param v2 right member of comparison
1068 * @return success/failure of operation
1070 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1071 LttTime
* r
= (LttTime
*) v1
;
1072 // 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)));
1073 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1077 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1079 * Applies the 'greater or equal' operator to the
1080 * specified structure and value
1081 * @param v1 left member of comparison
1082 * @param v2 right member of comparison
1083 * @return success/failure of operation
1085 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1086 guint
* r
= (guint
*) v1
;
1087 return (*r
>= v2
.v_uint
);
1091 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1093 * Applies the 'greater or equal' operator to the
1094 * specified structure and value
1095 * @param v1 left member of comparison
1096 * @param v2 right member of comparison
1097 * @return success/failure of operation
1099 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1100 guint64
* r
= (guint64
*) v1
;
1101 return (*r
>= v2
.v_uint64
);
1105 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1107 * Applies the 'greater or equal' operator to the
1108 * specified structure and value
1109 * @param v1 left member of comparison
1110 * @param v2 right member of comparison
1111 * @return success/failure of operation
1113 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1114 guint32
* r
= (guint32
*) v1
;
1115 return (*r
>= v2
.v_uint32
);
1119 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1121 * Applies the 'greater or equal' operator to the
1122 * specified structure and value
1123 * @param v1 left member of comparison
1124 * @param v2 right member of comparison
1125 * @return success/failure of operation
1127 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1128 guint16
* r
= (guint16
*) v1
;
1129 return (*r
>= v2
.v_uint16
);
1133 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1135 * Applies the 'greater or equal' operator to the
1136 * specified structure and value
1137 * @param v1 left member of comparison
1138 * @param v2 right member of comparison
1139 * @return success/failure of operation
1141 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1142 double* r
= (double*) v1
;
1143 return (*r
>= v2
.v_double
);
1147 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1149 * Applies the 'greater or equal' operator to the
1150 * specified structure and value
1151 * @param v1 left member of comparison
1152 * @param v2 right member of comparison
1153 * @return success/failure of operation
1155 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1156 LttTime
* r
= (LttTime
*) v1
;
1157 // 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)));
1158 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1164 * Makes a copy of the current filter tree
1165 * @param tree pointer to the current tree
1166 * @return new copy of the filter tree
1169 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1171 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1173 newtree
->node
= tree
->node
;
1175 newtree
->left
= tree
->left
;
1176 if(newtree
->left
== LTTV_TREE_NODE
) {
1177 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1178 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1179 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1180 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1181 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1182 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1183 /* FIXME: special case for string copy ! */
1184 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1187 newtree
->right
= tree
->right
;
1188 if(newtree
->right
== LTTV_TREE_NODE
) {
1189 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1190 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1191 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1192 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1193 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1194 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1195 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1203 * Makes a copy of the current filter
1204 * @param filter pointer to the current filter
1205 * @return new copy of the filter
1208 lttv_filter_clone(const LttvFilter
* filter
) {
1210 if(!filter
) return NULL
;
1212 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1214 strcpy(newfilter
->expression
,filter
->expression
);
1216 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1224 * @fn LttvFilter* lttv_filter_new()
1226 * Creates a new LttvFilter
1227 * @return the current LttvFilter or NULL if error
1232 LttvFilter
* filter
= g_new(LttvFilter
,1);
1233 filter
->expression
= NULL
;
1234 filter
->head
= NULL
;
1241 * @fn gboolean lttv_filter_update(LttvFilter*)
1243 * Updates the current LttvFilter by building
1244 * its tree based upon the expression string
1245 * @param filter pointer to the current LttvFilter
1246 * @return Failure/Success of operation
1249 lttv_filter_update(LttvFilter
* filter
) {
1251 // g_print("filter::lttv_filter_new()\n"); /* debug */
1253 if(filter
->expression
== NULL
) return FALSE
;
1257 p_nesting
=0, /* parenthesis nesting value */
1259 guint expression_len
;
1263 *tree
= lttv_filter_tree_new(), /* main tree */
1264 *subtree
= NULL
, /* buffer for subtrees */
1265 *t1
, /* buffer #1 */
1266 *t2
, /* buffer #2 */
1267 *t3
; /* buffer #3 */
1271 * If the tree already exists,
1272 * destroy it and build a new one
1274 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1275 filter
->head
= NULL
; /* will be assigned at the end */
1279 * each element of the list
1280 * is a sub tree created
1281 * by the use of parenthesis in the
1282 * global expression. The final tree
1283 * will be the one left at the root of
1286 GPtrArray
*tree_stack
= g_ptr_array_new();
1287 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1289 /* temporary values */
1290 GString
*a_field_component
= g_string_new("");
1291 GString
*a_string_spaces
= g_string_new("");
1292 GPtrArray
*a_field_path
= g_ptr_array_new();
1294 /* simple expression buffer */
1295 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1297 gint nest_quotes
= 0;
1300 * Parse entire expression and construct
1301 * the binary tree. There are two steps
1302 * in browsing that string
1303 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1304 * 2. finding simple expressions
1305 * - field path ( separated by dots )
1306 * - op ( >, <, =, >=, <=, !=)
1307 * - value ( integer, string ... )
1308 * To spare computing time, the whole
1309 * string is parsed in this loop for a
1310 * O(n) complexity order.
1312 * When encountering logical op &,|,^
1313 * 1. parse the last value if any
1314 * 2. create a new tree
1315 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1316 * 4. concatenate this tree with the current tree on top of the stack
1317 * When encountering math ops >,>=,<,<=,=,!=
1318 * 1. add to op to the simple expression
1319 * 2. concatenate last field component to field path
1320 * When encountering concatening ops .
1321 * 1. concatenate last field component to field path
1322 * When encountering opening parenthesis (,{,[
1323 * 1. create a new subtree on top of tree stack
1324 * When encountering closing parenthesis ),},]
1325 * 1. add the expression on right child of the current tree
1326 * 2. the subtree is completed, allocate a new subtree
1327 * 3. pop the tree value from the tree stack
1331 struct timeval starttime
;
1332 struct timeval endtime
;
1333 gettimeofday(&starttime
, NULL
);
1336 expression_len
= strlen(filter
->expression
);
1337 for(i
=0;i
<expression_len
;i
++) {
1339 // g_print("%c\n ",filter->expression[i]);
1341 switch(filter
->expression
[i
]) {
1343 if(filter
->expression
[i
+1] == '\"') {
1352 if(a_string_spaces
->len
!= 0) {
1353 a_field_component
= g_string_append(
1354 a_field_component
, a_string_spaces
->str
);
1355 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1357 a_field_component
= g_string_append_c(a_field_component
,
1358 filter
->expression
[i
]);
1362 switch(filter
->expression
[i
]) {
1368 /* get current tree in tree stack */
1369 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1371 /* get current node at absolute right */
1372 while(t1
->right
!= LTTV_TREE_IDLE
) {
1373 g_assert(t1
->right
== LTTV_TREE_NODE
);
1376 t2
= lttv_filter_tree_new();
1377 t2
->node
= LTTV_LOGICAL_AND
;
1378 t1
->right
= LTTV_TREE_NODE
;
1380 if(not) { /* add not operator to tree */
1381 t3
= lttv_filter_tree_new();
1382 t3
->node
= LTTV_LOGICAL_NOT
;
1383 t2
->left
= LTTV_TREE_NODE
;
1388 if(subtree
!= NULL
) { /* append subtree to current tree */
1389 t2
->left
= LTTV_TREE_NODE
;
1390 t2
->l_child
.t
= subtree
;
1392 } else { /* append a simple expression */
1393 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1394 a_field_component
= g_string_new("");
1395 g_string_free(a_string_spaces
, TRUE
);
1396 a_string_spaces
= g_string_new("");
1397 t2
->left
= LTTV_TREE_LEAF
;
1398 t2
->l_child
.leaf
= a_simple_expression
;
1399 a_simple_expression
= lttv_simple_expression_new();
1405 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1406 while(t1
->right
!= LTTV_TREE_IDLE
) {
1407 g_assert(t1
->right
== LTTV_TREE_NODE
);
1410 t2
= lttv_filter_tree_new();
1411 t2
->node
= LTTV_LOGICAL_OR
;
1412 t1
->right
= LTTV_TREE_NODE
;
1414 if(not) { // add not operator to tree
1415 t3
= lttv_filter_tree_new();
1416 t3
->node
= LTTV_LOGICAL_NOT
;
1417 t2
->left
= LTTV_TREE_NODE
;
1422 if(subtree
!= NULL
) { /* append subtree to current tree */
1423 t2
->left
= LTTV_TREE_NODE
;
1424 t2
->l_child
.t
= subtree
;
1426 } else { /* append a simple expression */
1427 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1428 a_field_component
= g_string_new("");
1429 g_string_free(a_string_spaces
, TRUE
);
1430 a_string_spaces
= g_string_new("");
1431 t2
->left
= LTTV_TREE_LEAF
;
1432 t2
->l_child
.leaf
= a_simple_expression
;
1433 a_simple_expression
= lttv_simple_expression_new();
1439 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1440 while(t1
->right
!= LTTV_TREE_IDLE
) {
1441 g_assert(t1
->right
== LTTV_TREE_NODE
);
1444 t2
= lttv_filter_tree_new();
1445 t2
->node
= LTTV_LOGICAL_XOR
;
1446 t1
->right
= LTTV_TREE_NODE
;
1448 if(not) { // add not operator to tree
1449 t3
= lttv_filter_tree_new();
1450 t3
->node
= LTTV_LOGICAL_NOT
;
1451 t2
->left
= LTTV_TREE_NODE
;
1456 if(subtree
!= NULL
) { /* append subtree to current tree */
1457 t2
->left
= LTTV_TREE_NODE
;
1458 t2
->l_child
.t
= subtree
;
1460 } else { /* append a simple expression */
1461 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1462 a_field_component
= g_string_new("");
1463 g_string_free(a_string_spaces
, TRUE
);
1464 a_string_spaces
= g_string_new("");
1465 t2
->left
= LTTV_TREE_LEAF
;
1466 t2
->l_child
.leaf
= a_simple_expression
;
1467 a_simple_expression
= lttv_simple_expression_new();
1471 case '!': /* not, or not equal (math op) */
1473 if(filter
->expression
[i
+1] == '=') { /* != */
1474 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1475 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1476 a_field_component
= g_string_new("");
1477 g_string_free(a_string_spaces
, TRUE
);
1478 a_string_spaces
= g_string_new("");
1479 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1486 case '(': /* start of parenthesis */
1490 p_nesting
++; /* incrementing parenthesis nesting value */
1491 t1
= lttv_filter_tree_new();
1492 if(not) { /* add not operator to tree */
1493 t3
= lttv_filter_tree_new();
1494 t3
->node
= LTTV_LOGICAL_NOT
;
1495 t1
->right
= LTTV_TREE_NODE
;
1499 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1502 case ')': /* end of parenthesis */
1506 p_nesting
--; /* decrementing parenthesis nesting value */
1507 if(p_nesting
<0 || tree_stack
->len
<2) {
1508 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1509 is not valid due to parenthesis incorrect use",filter
->expression
);
1513 /* there must at least be the root tree left in the array */
1514 g_assert(tree_stack
->len
>0);
1516 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1517 while(t1
->right
!= LTTV_TREE_IDLE
) {
1520 if(not) { // add not operator to tree
1522 t3
= lttv_filter_tree_new();
1523 t3
->node
= LTTV_LOGICAL_NOT
;
1524 t1
->right
= LTTV_TREE_NODE
;
1529 if(subtree
!= NULL
) { /* append subtree to current tree */
1530 t1
->right
= LTTV_TREE_NODE
;
1531 t1
->r_child
.t
= subtree
;
1532 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1533 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1534 } else { /* assign subtree as current tree */
1535 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1536 a_field_component
= g_string_new("");
1537 g_string_free(a_string_spaces
, TRUE
);
1538 a_string_spaces
= g_string_new("");
1539 t1
->right
= LTTV_TREE_LEAF
;
1540 t1
->r_child
.leaf
= a_simple_expression
;
1541 a_simple_expression
= lttv_simple_expression_new();
1542 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1547 * mathematic operators
1549 case '<': /* lower, lower or equal */
1551 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1552 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1553 a_field_component
= g_string_new("");
1554 g_string_free(a_string_spaces
, TRUE
);
1555 a_string_spaces
= g_string_new("");
1556 if(filter
->expression
[i
+1] == '=') { /* <= */
1558 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1559 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1562 case '>': /* higher, higher or equal */
1564 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1565 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1566 a_field_component
= g_string_new("");
1567 g_string_free(a_string_spaces
, TRUE
);
1568 a_string_spaces
= g_string_new("");
1569 if(filter
->expression
[i
+1] == '=') { /* >= */
1571 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1572 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1575 case '=': /* equal */
1577 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1578 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1579 a_field_component
= g_string_new("");
1580 g_string_free(a_string_spaces
, TRUE
);
1581 a_string_spaces
= g_string_new("");
1582 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1586 * Field concatening caracter
1591 * divide field expression into elements
1592 * in a_field_path array.
1594 * A dot can also be present in double values
1596 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1597 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1598 a_field_component
= g_string_new("");
1599 g_string_free(a_string_spaces
, TRUE
);
1600 a_string_spaces
= g_string_new("");
1602 /* Operator found, we are in the value field */
1603 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1606 case ' ': /* keep spaces that are within a field component */
1607 if(a_field_component
->len
== 0) break; /* ignore */
1609 a_string_spaces
= g_string_append_c(a_string_spaces
,
1610 filter
->expression
[i
]);
1612 case '\n': /* ignore */
1615 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1617 default: /* concatening current string */
1618 if(a_string_spaces
->len
!= 0) {
1619 a_field_component
= g_string_append(
1620 a_field_component
, a_string_spaces
->str
);
1621 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1623 a_field_component
= g_string_append_c(a_field_component
,
1624 filter
->expression
[i
]);
1629 * Preliminary check to see
1630 * if tree was constructed correctly
1633 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1634 is not valid due to parenthesis incorrect use",filter
->expression
);
1638 if(tree_stack
->len
!= 1) /* only root tree should remain */
1642 * processing last element of expression
1644 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1645 while(t1
->right
!= LTTV_TREE_IDLE
) {
1646 g_assert(t1
->right
== LTTV_TREE_NODE
);
1649 if(not) { // add not operator to tree
1650 t3
= lttv_filter_tree_new();
1651 t3
->node
= LTTV_LOGICAL_NOT
;
1652 t1
->right
= LTTV_TREE_NODE
;
1657 if(subtree
!= NULL
) { /* add the subtree */
1658 t1
->right
= LTTV_TREE_NODE
;
1659 t1
->r_child
.t
= subtree
;
1661 } else { /* add a leaf */
1662 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1663 a_field_component
= NULL
;
1664 g_string_free(a_string_spaces
, TRUE
);
1665 a_string_spaces
= NULL
;
1666 t1
->right
= LTTV_TREE_LEAF
;
1667 t1
->r_child
.leaf
= a_simple_expression
;
1668 a_simple_expression
= NULL
;
1672 /* free the pointer array */
1673 g_assert(a_field_path
->len
== 0);
1674 g_ptr_array_free(a_field_path
,TRUE
);
1676 /* free the tree stack -- but keep the root tree */
1677 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1678 g_ptr_array_free(tree_stack
,TRUE
);
1680 /* free the field buffer if allocated */
1681 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1682 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1684 /* free the simple expression buffer if allocated */
1685 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1687 g_assert(filter
->head
!= NULL
); /* tree should exist */
1688 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1691 gettimeofday(&endtime
, NULL
);
1693 /* Calcul du temps de l'algorithme */
1694 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1695 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1696 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1697 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1701 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1702 lttv_print_tree(filter
->head
,0) ;
1703 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1711 * @fn void lttv_filter_destroy(LttvFilter*)
1713 * Destroy the current LttvFilter
1714 * @param filter pointer to the current LttvFilter
1717 lttv_filter_destroy(LttvFilter
* filter
) {
1721 if(filter
->expression
)
1722 g_free(filter
->expression
);
1724 lttv_filter_tree_destroy(filter
->head
);
1730 * @fn LttvFilterTree* lttv_filter_tree_new()
1732 * Assign a new tree for the current expression
1734 * @return pointer of LttvFilterTree
1737 lttv_filter_tree_new() {
1738 LttvFilterTree
* tree
;
1740 tree
= g_new(LttvFilterTree
,1);
1741 tree
->node
= 0; //g_new(lttv_expression,1);
1742 tree
->left
= LTTV_TREE_IDLE
;
1743 tree
->right
= LTTV_TREE_IDLE
;
1744 tree
->r_child
.t
= NULL
;
1745 tree
->l_child
.t
= NULL
;
1751 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1753 * Append a new expression to the expression
1754 * defined in the current filter
1755 * @param filter pointer to the current LttvFilter
1756 * @param expression string that must be appended
1757 * @return Success/Failure of operation
1760 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1762 if(expression
== NULL
) return FALSE
;
1763 if(filter
== NULL
) return FALSE
;
1764 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1766 GString
* s
= g_string_new("");
1767 if(filter
->expression
!= NULL
) {
1768 s
= g_string_append(s
,filter
->expression
);
1769 s
= g_string_append_c(s
,'&');
1771 s
= g_string_append(s
,expression
);
1773 g_free(filter
->expression
);
1774 filter
->expression
= g_string_free(s
,FALSE
);
1776 /* TRUE if construction of tree proceeded without errors */
1777 return lttv_filter_update(filter
);
1782 * @fn void lttv_filter_clear_expression(LttvFilter*)
1784 * Clear the filter expression from the
1785 * current filter and sets its pointer to NULL
1786 * @param filter pointer to the current LttvFilter
1789 lttv_filter_clear_expression(LttvFilter
* filter
) {
1791 if(filter
->expression
!= NULL
) {
1792 g_free(filter
->expression
);
1793 filter
->expression
= NULL
;
1799 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1801 * Destroys the tree and his sub-trees
1802 * @param tree Tree which must be destroyed
1805 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1807 if(tree
== NULL
) return;
1809 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1810 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1812 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1813 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1815 // g_free(tree->node);
1820 * Global parsing function for the current
1822 * @param t pointer to the current LttvFilterTree
1823 * @param event current LttEvent, NULL if not used
1824 * @param tracefile current LttTracefile, NULL if not used
1825 * @param trace current LttTrace, NULL if not used
1826 * @param state current LttvProcessState, NULL if not used
1827 * @param context current LttvTracefileContext, NULL if not used
1828 * @return response of filter
1831 lttv_filter_tree_parse(
1832 const LttvFilterTree
* t
,
1833 const LttEvent
* event
,
1834 const LttTracefile
* tracefile
,
1835 const LttTrace
* trace
,
1836 const LttvTracefileContext
* context
1841 * Each tree is parsed in inorder.
1842 * This way, it's possible to apply the left filter of the
1843 * tree, then decide whether or not the right branch should
1844 * be parsed depending on the linking logical operator
1846 * Each node consists in a
1847 * 1. logical operator
1848 * 2. left child ( node or simple expression )
1849 * 3. right child ( node or simple expression )
1851 * When the child is a simple expression, we must
1852 * before all determine if the expression refers to
1853 * a structure which is whithin observation ( not NULL ).
1854 * -If so, the expression is evaluated.
1855 * -If not, the result is set to TRUE since this particular
1856 * operation does not interfere with the lttv structure
1858 * The result of each simple expression will directly
1859 * affect the next branch. This way, depending on
1860 * the linking logical operator, the parser will decide
1861 * to explore or not the next branch.
1863 * -If result of left branch is 0 / FALSE
1864 * then don't explore right branch and return 0;
1865 * -If result of left branch is 1 / TRUE then explore
1867 * -If result of left branch is 1 / TRUE
1868 * then don't explore right branch and return 1;
1869 * -If result of left branch is 0 / FALSE then explore
1871 * -Result of left branch will not affect exploration of
1875 gboolean lresult
= FALSE
, rresult
= FALSE
;
1877 LttvProcessState
* state
;
1879 LttvTraceState
*ts
= (LttvTraceState
*)context
->t_context
;
1880 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1881 guint cpu
= tfs
->cpu
;
1882 state
= ts
->running_process
[cpu
];
1887 if(t
->left
== LTTV_TREE_NODE
) {
1888 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
);
1890 else if(t
->left
== LTTV_TREE_LEAF
) {
1891 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1895 * Parse linking operator
1896 * make a cutoff if possible
1898 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1899 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1902 * Parse right branch
1904 if(t
->right
== LTTV_TREE_NODE
) {
1905 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
);
1907 else if(t
->right
== LTTV_TREE_LEAF
) {
1908 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1913 * Apply and return the
1914 * logical link between the
1918 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1919 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1920 case LTTV_LOGICAL_NOT
:
1921 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1922 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1923 case 0: return (rresult
);
1926 * This case should never be
1927 * parsed, if so, this subtree
1936 * This function parses a particular branch of the tree
1937 * @param se pointer to the current LttvSimpleExpression
1938 * @param event current LttEvent, NULL if not used
1939 * @param tracefile current LttTracefile, NULL if not used
1940 * @param trace current LttTrace, NULL if not used
1941 * @param state current LttvProcessState, NULL if not used
1942 * @param context current LttvTracefileContext, NULL if not used
1943 * @return response of filter
1946 lttv_filter_tree_parse_branch(
1947 const LttvSimpleExpression
* se
,
1948 const LttEvent
* event
,
1949 const LttTracefile
* tracefile
,
1950 const LttTrace
* trace
,
1951 const LttvProcessState
* state
,
1952 const LttvTracefileContext
* context
) {
1957 case LTTV_FILTER_TRACE_NAME
:
1958 if(trace
== NULL
) return TRUE
;
1960 GQuark quark
= ltt_trace_name(trace
);
1961 return se
->op((gpointer
)&quark
,v
);
1964 case LTTV_FILTER_TRACEFILE_NAME
:
1965 if(tracefile
== NULL
) return TRUE
;
1967 GQuark quark
= ltt_tracefile_name(tracefile
);
1968 return se
->op((gpointer
)&quark
,v
);
1971 case LTTV_FILTER_STATE_PID
:
1972 if(state
== NULL
) return TRUE
;
1973 else return se
->op((gpointer
)&state
->pid
,v
);
1975 case LTTV_FILTER_STATE_PPID
:
1976 if(state
== NULL
) return TRUE
;
1977 else return se
->op((gpointer
)&state
->ppid
,v
);
1979 case LTTV_FILTER_STATE_CT
:
1980 if(state
== NULL
) return TRUE
;
1982 return se
->op((gpointer
)&state
->creation_time
,v
);
1985 case LTTV_FILTER_STATE_IT
:
1986 if(state
== NULL
) return TRUE
;
1988 return se
->op((gpointer
)&state
->insertion_time
,v
);
1991 case LTTV_FILTER_STATE_P_NAME
:
1992 if(state
== NULL
) return TRUE
;
1994 GQuark quark
= state
->name
;
1995 return se
->op((gpointer
)&quark
,v
);
1998 case LTTV_FILTER_STATE_T_BRAND
:
1999 if(state
== NULL
) return TRUE
;
2001 GQuark quark
= state
->brand
;
2002 return se
->op((gpointer
)&quark
,v
);
2005 case LTTV_FILTER_STATE_EX_MODE
:
2006 if(state
== NULL
) return TRUE
;
2007 else return se
->op((gpointer
)&state
->state
->t
,v
);
2009 case LTTV_FILTER_STATE_EX_SUBMODE
:
2010 if(state
== NULL
) return TRUE
;
2011 else return se
->op((gpointer
)&state
->state
->n
,v
);
2013 case LTTV_FILTER_STATE_P_STATUS
:
2014 if(state
== NULL
) return TRUE
;
2015 else return se
->op((gpointer
)&state
->state
->s
,v
);
2017 case LTTV_FILTER_STATE_CPU
:
2018 if(context
== NULL
) return TRUE
;
2020 if(state
== NULL
) return TRUE
;
2021 else return se
->op((gpointer
)&state
->cpu
,v
);
2024 case LTTV_FILTER_EVENT_NAME
:
2025 if(event
== NULL
) return TRUE
;
2028 et
= ltt_event_eventtype(event
);
2029 GQuark quark
= ltt_eventtype_name(et
);
2030 return se
->op((gpointer
)&quark
,v
);
2033 case LTTV_FILTER_EVENT_FACILITY
:
2034 if(event
== NULL
) return TRUE
;
2037 fac
= ltt_event_facility(event
);
2038 GQuark quark
= ltt_facility_name(fac
);
2039 return se
->op((gpointer
)&quark
,v
);
2042 case LTTV_FILTER_EVENT_CATEGORY
:
2044 * TODO: Not yet implemented
2048 case LTTV_FILTER_EVENT_TIME
:
2049 if(event
== NULL
) return TRUE
;
2051 LttTime time
= ltt_event_time(event
);
2052 return se
->op((gpointer
)&time
,v
);
2055 case LTTV_FILTER_EVENT_TSC
:
2056 if(event
== NULL
) return TRUE
;
2058 LttCycleCount count
= ltt_event_cycle_count(event
);
2059 return se
->op((gpointer
)&count
,v
);
2062 case LTTV_FILTER_EVENT_TARGET_PID
:
2063 if(context
== NULL
) return TRUE
;
2066 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2067 return se
->op((gpointer
)&target_pid
,v
);
2070 case LTTV_FILTER_EVENT_FIELD
:
2072 * TODO: Use the offset to
2073 * find the dynamic field
2074 * in the event struct
2079 * This case should never be
2080 * parsed, if so, the whole
2081 * filtering is cancelled
2083 g_warning("Error while parsing the filter tree");
2087 /* should never get here */
2095 * Debug function. Prints tree memory allocation.
2096 * @param t the pointer to the current LttvFilterTree
2099 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2101 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2102 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2103 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2105 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2106 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"));
2107 if(t
->left
== LTTV_TREE_LEAF
) {
2108 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2109 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2110 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2112 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"));
2113 if(t
->right
== LTTV_TREE_LEAF
) {
2114 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2115 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2116 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2119 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2120 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2124 * @fn static void module_init()
2126 * Initializes the filter module and specific values
2128 static void module_init()
2134 * Destroys the filter module and specific values
2136 static void module_destroy()
2142 LTTV_MODULE("filter", "Filters traceset and events", \
2143 "Filters traceset and events specifically to user input", \
2144 module_init
, module_destroy
)