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>
87 * @fn LttvSimpleExpression* lttv_simple_expression_new()
89 * Constructor for LttvSimpleExpression
90 * @return pointer to new LttvSimpleExpression
93 lttv_simple_expression_new() {
95 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
97 se
->field
= LTTV_FILTER_UNDEFINED
;
105 * Keeps the array order.
107 static inline gpointer
ltt_g_ptr_array_remove_index_slow(GPtrArray
*fp
,
116 ptr
= g_ptr_array_index(fp
, index
);
117 for (i
= index
; i
< fp
->len
- 1; i
++) {
118 g_ptr_array_index(fp
, i
) = g_ptr_array_index(fp
, i
+ 1);
120 g_ptr_array_remove_index(fp
, fp
->len
- 1);
125 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
127 * Parse through filtering field hierarchy as specified
128 * by user. This function compares each value to
129 * predetermined quarks
130 * @param fp The field path list
131 * @param se current simple expression
132 * @return success/failure of operation
135 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
138 printf("array len : %d\n", fp
->len
);
139 if(fp
->len
< 2) return FALSE
;
140 g_assert((f
=ltt_g_ptr_array_remove_index_slow(fp
,0)));
144 * Parse through the specified
147 * Take note however that the
148 * 'event' subfields might change
149 * depending on values specified
150 * in core.xml file. Hence, if
151 * none of the subfields in the
152 * array match the hardcoded
153 * subfields, it will be considered
156 printf("DBG1: %s\n", f
->str
);
157 if(!g_strcasecmp(f
->str
,"trace") ) {
162 g_string_free(f
,TRUE
);
163 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
164 if(!g_strcasecmp(f
->str
,"name")) {
165 se
->field
= LTTV_FILTER_TRACE_NAME
;
167 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
169 * FIXME: not yet implemented !
171 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
176 g_string_free(f
,TRUE
);
177 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
178 if(!g_strcasecmp(f
->str
,"name")) {
179 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
181 } else if(!g_strcasecmp(f
->str
,"state") ) {
182 printf("DBG2: %s\n", f
->str
);
187 * state.creation_time
188 * state.insertion_time
191 * state.execution_mode
192 * state.execution_submode
193 * state.process_status
196 g_string_free(f
,TRUE
);
197 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
198 if(!g_strcasecmp(f
->str
,"pid") ) {
199 se
->field
= LTTV_FILTER_STATE_PID
;
201 else if(!g_strcasecmp(f
->str
,"ppid") ) {
202 se
->field
= LTTV_FILTER_STATE_PPID
;
204 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
205 se
->field
= LTTV_FILTER_STATE_CT
;
207 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
208 se
->field
= LTTV_FILTER_STATE_IT
;
210 else if(!g_strcasecmp(f
->str
,"process_name") ) {
211 se
->field
= LTTV_FILTER_STATE_P_NAME
;
213 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
214 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
216 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
217 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
219 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
220 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
222 else if(!g_strcasecmp(f
->str
,"process_status") ) {
223 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
225 else if(!g_strcasecmp(f
->str
,"cpu") ) {
226 se
->field
= LTTV_FILTER_STATE_CPU
;
228 } else if(!g_strcasecmp(f
->str
,"event") ) {
238 g_string_free(f
,TRUE
);
239 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
240 printf("DBG: %s\n", f
->str
);
241 if(!g_strcasecmp(f
->str
,"name") ) {
242 se
->field
= LTTV_FILTER_EVENT_NAME
;
244 else if(!g_strcasecmp(f
->str
,"category") ) {
246 * FIXME: Category not yet functional in lttv
248 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
250 else if(!g_strcasecmp(f
->str
,"time") ) {
251 se
->field
= LTTV_FILTER_EVENT_TIME
;
253 else if(!g_strcasecmp(f
->str
,"tsc") ) {
254 se
->field
= LTTV_FILTER_EVENT_TSC
;
256 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
257 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
259 else if(!g_strcasecmp(f
->str
,"field") ) {
260 se
->field
= LTTV_FILTER_EVENT_FIELD
;
261 g_string_free(f
,TRUE
);
262 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
265 //g_string_free(f,TRUE);
266 //f=ltt_g_ptr_array_remove_index_slow(fp,0);
267 g_warning("Unknown event filter subtype %s", f
->str
);
270 g_string_free(f
,TRUE
);
271 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
273 g_warning("Unrecognized field in filter string");
276 /* free memory for last string */
277 g_string_free(f
,TRUE
);
279 /* array should be empty */
280 g_assert(fp
->len
== 0);
282 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
283 g_warning("The specified field was not recognized !");
290 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
292 * Sets the function pointer for the current
294 * @param se current simple expression
295 * @param op current operator
296 * @return success/failure of operation
299 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
305 case LTTV_FILTER_TRACE_NAME
:
306 case LTTV_FILTER_TRACEFILE_NAME
:
307 case LTTV_FILTER_STATE_P_NAME
:
308 case LTTV_FILTER_STATE_T_BRAND
:
309 case LTTV_FILTER_EVENT_NAME
:
310 case LTTV_FILTER_STATE_EX_MODE
:
311 case LTTV_FILTER_STATE_EX_SUBMODE
:
312 case LTTV_FILTER_STATE_P_STATUS
:
315 se
->op
= lttv_apply_op_eq_quark
;
318 se
->op
= lttv_apply_op_ne_quark
;
321 g_warning("Error encountered in operator assignment = or != expected");
328 case LTTV_FILTER_EVENT_TSC
:
331 se
->op
= lttv_apply_op_eq_uint64
;
334 se
->op
= lttv_apply_op_ne_uint64
;
337 se
->op
= lttv_apply_op_lt_uint64
;
340 se
->op
= lttv_apply_op_le_uint64
;
343 se
->op
= lttv_apply_op_gt_uint64
;
346 se
->op
= lttv_apply_op_ge_uint64
;
349 g_warning("Error encountered in operator assignment");
356 case LTTV_FILTER_STATE_CPU
:
357 case LTTV_FILTER_STATE_PID
:
358 case LTTV_FILTER_STATE_PPID
:
359 case LTTV_FILTER_EVENT_TARGET_PID
:
362 se
->op
= lttv_apply_op_eq_uint
;
365 se
->op
= lttv_apply_op_ne_uint
;
368 se
->op
= lttv_apply_op_lt_uint
;
371 se
->op
= lttv_apply_op_le_uint
;
374 se
->op
= lttv_apply_op_gt_uint
;
377 se
->op
= lttv_apply_op_ge_uint
;
380 g_warning("Error encountered in operator assignment");
387 * Entered as string, converted to enum
389 * can only be compared with 'equal' or 'not equal' operators
391 * unsigned int of 16 bits are used here since enums
392 * should not go over 2^16-1 values
396 // case LTTV_FIELD_EQ:
397 // se->op = lttv_apply_op_eq_uint16;
399 // case LTTV_FIELD_NE:
400 // se->op = lttv_apply_op_ne_uint16;
403 // g_warning("Error encountered in operator assignment = or != expected");
410 case LTTV_FILTER_STATE_CT
:
411 case LTTV_FILTER_STATE_IT
:
412 case LTTV_FILTER_EVENT_TIME
:
415 se
->op
= lttv_apply_op_eq_ltttime
;
418 se
->op
= lttv_apply_op_ne_ltttime
;
421 se
->op
= lttv_apply_op_lt_ltttime
;
424 se
->op
= lttv_apply_op_le_ltttime
;
427 se
->op
= lttv_apply_op_gt_ltttime
;
430 se
->op
= lttv_apply_op_ge_ltttime
;
433 g_warning("Error encountered in operator assignment");
438 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
447 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
449 * Assign the value field to the current LttvSimpleExpression
450 * @param se pointer to the current LttvSimpleExpression
451 * @param value string value for simple expression
454 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
457 gboolean is_double
= FALSE
;
458 LttTime t
= ltt_time_zero
;
465 * entered as strings, converted to Quarks
467 case LTTV_FILTER_TRACE_NAME
:
468 case LTTV_FILTER_TRACEFILE_NAME
:
469 case LTTV_FILTER_STATE_P_NAME
:
470 case LTTV_FILTER_STATE_T_BRAND
:
471 case LTTV_FILTER_EVENT_NAME
:
472 case LTTV_FILTER_STATE_EX_MODE
:
473 case LTTV_FILTER_STATE_EX_SUBMODE
:
474 case LTTV_FILTER_STATE_P_STATUS
:
475 // se->value.v_string = value;
476 se
->value
.v_quark
= g_quark_from_string(value
);
480 * integer -- supposed to be uint64
482 case LTTV_FILTER_EVENT_TSC
:
483 se
->value
.v_uint64
= atoi(value
);
489 case LTTV_FILTER_STATE_PID
:
490 case LTTV_FILTER_STATE_PPID
:
491 case LTTV_FILTER_STATE_CPU
:
492 case LTTV_FILTER_EVENT_TARGET_PID
:
493 se
->value
.v_uint
= atoi(value
);
499 case LTTV_FILTER_STATE_CT
:
500 case LTTV_FILTER_STATE_IT
:
501 case LTTV_FILTER_EVENT_TIME
:
502 //se->value.v_double = atof(value);
504 * parsing logic could be optimised,
505 * but as for now, simpler this way
507 v
= g_string_new("");
508 string_len
= strlen(value
);
509 for(i
=0;i
<string_len
;i
++) {
510 if(value
[i
] == '.') {
511 /* cannot specify number with more than one '.' */
512 if(is_double
) return FALSE
;
513 else is_double
= TRUE
;
514 t
.tv_sec
= atoi(v
->str
);
515 g_string_free(v
,TRUE
);
516 v
= g_string_new("");
517 } else v
= g_string_append_c(v
,value
[i
]);
519 /* number can be integer or double */
520 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
522 t
.tv_sec
= atoi(v
->str
);
526 g_string_free(v
,TRUE
);
528 se
->value
.v_ltttime
= t
;
532 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
542 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
544 * Disallocate memory for the current
546 * @param se pointer to the current LttvSimpleExpression
549 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
551 // g_free(se->value);
552 // switch(se->field) {
553 // case LTTV_FILTER_TRACE_NAME:
554 // case LTTV_FILTER_TRACEFILE_NAME:
555 // case LTTV_FILTER_STATE_P_NAME:
556 // case LTTV_FILTER_EVENT_NAME:
557 // g_free(se->value.v_string);
565 * @fn gint lttv_struct_type(gint)
567 * Finds the structure type depending
568 * on the fields in parameters
569 * @params ft Field of the current structure
570 * @return LttvStructType enum or -1 for error
573 lttv_struct_type(gint ft
) {
576 case LTTV_FILTER_TRACE_NAME
:
577 return LTTV_FILTER_TRACE
;
579 case LTTV_FILTER_TRACEFILE_NAME
:
580 return LTTV_FILTER_TRACEFILE
;
582 case LTTV_FILTER_STATE_PID
:
583 case LTTV_FILTER_STATE_PPID
:
584 case LTTV_FILTER_STATE_CT
:
585 case LTTV_FILTER_STATE_IT
:
586 case LTTV_FILTER_STATE_P_NAME
:
587 case LTTV_FILTER_STATE_T_BRAND
:
588 case LTTV_FILTER_STATE_EX_MODE
:
589 case LTTV_FILTER_STATE_EX_SUBMODE
:
590 case LTTV_FILTER_STATE_P_STATUS
:
591 case LTTV_FILTER_STATE_CPU
:
592 return LTTV_FILTER_STATE
;
594 case LTTV_FILTER_EVENT_NAME
:
595 case LTTV_FILTER_EVENT_CATEGORY
:
596 case LTTV_FILTER_EVENT_TIME
:
597 case LTTV_FILTER_EVENT_TSC
:
598 case LTTV_FILTER_EVENT_TARGET_PID
:
599 case LTTV_FILTER_EVENT_FIELD
:
600 return LTTV_FILTER_EVENT
;
608 * @fn gboolean lttv_apply_op_eq_uint(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_uint(const gpointer v1
, LttvFieldValue v2
) {
618 guint
* r
= (guint
*) v1
;
619 return (*r
== v2
.v_uint
);
624 * @fn gboolean lttv_apply_op_eq_uint64(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_uint64(const gpointer v1
, LttvFieldValue v2
) {
634 guint64
* r
= (guint64
*) v1
;
635 return (*r
== v2
.v_uint64
);
640 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
642 * Applies the 'equal' operator to the
643 * specified structure and value
644 * @param v1 left member of comparison
645 * @param v2 right member of comparison
646 * @return success/failure of operation
648 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
649 guint32
* r
= (guint32
*) v1
;
650 return (*r
== v2
.v_uint32
);
654 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
656 * Applies the 'equal' operator to the
657 * specified structure and value
658 * @param v1 left member of comparison
659 * @param v2 right member of comparison
660 * @return success/failure of operation
662 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
663 guint16
* r
= (guint16
*) v1
;
664 return (*r
== v2
.v_uint16
);
668 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
670 * Applies the 'equal' operator to the
671 * specified structure and value
672 * @param v1 left member of comparison
673 * @param v2 right member of comparison
674 * @return success/failure of operation
676 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
677 double* r
= (double*) v1
;
678 return (*r
== v2
.v_double
);
682 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
684 * Applies the 'equal' operator to the
685 * specified structure and value
686 * @param v1 left member of comparison
687 * @param v2 right member of comparison
688 * @return success/failure of operation
690 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
691 char* r
= (char*) v1
;
692 return (!g_strcasecmp(r
,v2
.v_string
));
696 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
698 * Applies the 'equal' operator to the
699 * specified structure and value
700 * @param v1 left member of comparison
701 * @param v2 right member of comparison
702 * @return success/failure of operation
704 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
705 GQuark
* r
= (GQuark
*) v1
;
706 return (*r
== v2
.v_quark
);
710 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
712 * Applies the 'equal' operator to the
713 * specified structure and value
714 * @param v1 left member of comparison
715 * @param v2 right member of comparison
716 * @return success/failure of operation
718 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
719 LttTime
* r
= (LttTime
*) v1
;
720 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
724 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
726 * Applies the 'not equal' operator to the
727 * specified structure and value
728 * @param v1 left member of comparison
729 * @param v2 right member of comparison
730 * @return success/failure of operation
732 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
733 guint
* r
= (guint
*) v1
;
734 return (*r
!= v2
.v_uint
);
738 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
740 * Applies the 'not equal' operator to the
741 * specified structure and value
742 * @param v1 left member of comparison
743 * @param v2 right member of comparison
744 * @return success/failure of operation
746 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
747 guint64
* r
= (guint64
*) v1
;
748 return (*r
!= v2
.v_uint64
);
752 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
754 * Applies the 'not equal' operator to the
755 * specified structure and value
756 * @param v1 left member of comparison
757 * @param v2 right member of comparison
758 * @return success/failure of operation
760 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
761 guint32
* r
= (guint32
*) v1
;
762 return (*r
!= v2
.v_uint32
);
766 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
768 * Applies the 'not equal' operator to the
769 * specified structure and value
770 * @param v1 left member of comparison
771 * @param v2 right member of comparison
772 * @return success/failure of operation
774 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
775 guint16
* r
= (guint16
*) v1
;
776 return (*r
!= v2
.v_uint16
);
780 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
782 * Applies the 'not equal' operator to the
783 * specified structure and value
784 * @param v1 left member of comparison
785 * @param v2 right member of comparison
786 * @return success/failure of operation
788 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
789 double* r
= (double*) v1
;
790 return (*r
!= v2
.v_double
);
794 * @fn gboolean lttv_apply_op_ne_string(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_string(const gpointer v1
, LttvFieldValue v2
) {
803 char* r
= (char*) v1
;
804 return (g_strcasecmp(r
,v2
.v_string
));
808 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
810 * Applies the 'not equal' 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_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
817 GQuark
* r
= (GQuark
*) v1
;
818 return (*r
!= v2
.v_quark
);
823 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
825 * Applies the 'not equal' operator to the
826 * specified structure and value
827 * @param v1 left member of comparison
828 * @param v2 right member of comparison
829 * @return success/failure of operation
831 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
832 LttTime
* r
= (LttTime
*) v1
;
833 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
837 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
839 * Applies the 'lower than' operator to the
840 * specified structure and value
841 * @param v1 left member of comparison
842 * @param v2 right member of comparison
843 * @return success/failure of operation
845 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
846 guint
* r
= (guint
*) v1
;
847 return (*r
< v2
.v_uint
);
851 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
853 * Applies the 'lower than' operator to the
854 * specified structure and value
855 * @param v1 left member of comparison
856 * @param v2 right member of comparison
857 * @return success/failure of operation
859 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
860 guint64
* r
= (guint64
*) v1
;
861 return (*r
< v2
.v_uint64
);
865 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
867 * Applies the 'lower than' operator to the
868 * specified structure and value
869 * @param v1 left member of comparison
870 * @param v2 right member of comparison
871 * @return success/failure of operation
873 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
874 guint32
* r
= (guint32
*) v1
;
875 return (*r
< v2
.v_uint32
);
879 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
881 * Applies the 'lower than' operator to the
882 * specified structure and value
883 * @param v1 left member of comparison
884 * @param v2 right member of comparison
885 * @return success/failure of operation
887 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
888 guint16
* r
= (guint16
*) v1
;
889 return (*r
< v2
.v_uint16
);
893 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
895 * Applies the 'lower than' 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_lt_double(const gpointer v1
, LttvFieldValue v2
) {
902 double* r
= (double*) v1
;
903 return (*r
< v2
.v_double
);
907 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
909 * Applies the 'lower than' 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_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
916 LttTime
* r
= (LttTime
*) v1
;
917 // 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)));
918 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
922 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
924 * Applies the 'lower or equal' operator to the
925 * specified structure and value
926 * @param v1 left member of comparison
927 * @param v2 right member of comparison
928 * @return success/failure of operation
930 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
931 guint
* r
= (guint
*) v1
;
932 return (*r
<= v2
.v_uint
);
936 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
938 * Applies the 'lower or equal' operator to the
939 * specified structure and value
940 * @param v1 left member of comparison
941 * @param v2 right member of comparison
942 * @return success/failure of operation
944 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
945 guint64
* r
= (guint64
*) v1
;
946 return (*r
<= v2
.v_uint64
);
950 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
952 * Applies the 'lower or equal' operator to the
953 * specified structure and value
954 * @param v1 left member of comparison
955 * @param v2 right member of comparison
956 * @return success/failure of operation
958 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
959 guint32
* r
= (guint32
*) v1
;
960 return (*r
<= v2
.v_uint32
);
964 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
966 * Applies the 'lower or equal' operator to the
967 * specified structure and value
968 * @param v1 left member of comparison
969 * @param v2 right member of comparison
970 * @return success/failure of operation
972 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
973 guint16
* r
= (guint16
*) v1
;
974 return (*r
<= v2
.v_uint16
);
978 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
980 * Applies the 'lower or equal' operator to the
981 * specified structure and value
982 * @param v1 left member of comparison
983 * @param v2 right member of comparison
984 * @return success/failure of operation
986 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
987 double* r
= (double*) v1
;
988 return (*r
<= v2
.v_double
);
992 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
994 * Applies the 'lower or equal' 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_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1001 LttTime
* r
= (LttTime
*) v1
;
1002 // 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)));
1003 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1008 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1010 * Applies the 'greater than' operator to the
1011 * specified structure and value
1012 * @param v1 left member of comparison
1013 * @param v2 right member of comparison
1014 * @return success/failure of operation
1016 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1017 guint
* r
= (guint
*) v1
;
1018 return (*r
> v2
.v_uint
);
1022 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1024 * Applies the 'greater than' operator to the
1025 * specified structure and value
1026 * @param v1 left member of comparison
1027 * @param v2 right member of comparison
1028 * @return success/failure of operation
1030 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1031 guint64
* r
= (guint64
*) v1
;
1032 return (*r
> v2
.v_uint64
);
1036 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1038 * Applies the 'greater than' operator to the
1039 * specified structure and value
1040 * @param v1 left member of comparison
1041 * @param v2 right member of comparison
1042 * @return success/failure of operation
1044 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1045 guint32
* r
= (guint32
*) v1
;
1046 return (*r
> v2
.v_uint32
);
1050 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1052 * Applies the 'greater than' operator to the
1053 * specified structure and value
1054 * @param v1 left member of comparison
1055 * @param v2 right member of comparison
1056 * @return success/failure of operation
1058 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1059 guint16
* r
= (guint16
*) v1
;
1060 return (*r
> v2
.v_uint16
);
1064 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1066 * Applies the 'greater than' 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_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1073 double* r
= (double*) v1
;
1074 return (*r
> v2
.v_double
);
1078 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1080 * Applies the 'greater than' 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_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1087 LttTime
* r
= (LttTime
*) v1
;
1088 // 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)));
1089 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1093 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1095 * Applies the 'greater or equal' operator to the
1096 * specified structure and value
1097 * @param v1 left member of comparison
1098 * @param v2 right member of comparison
1099 * @return success/failure of operation
1101 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1102 guint
* r
= (guint
*) v1
;
1103 return (*r
>= v2
.v_uint
);
1107 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1109 * Applies the 'greater or equal' operator to the
1110 * specified structure and value
1111 * @param v1 left member of comparison
1112 * @param v2 right member of comparison
1113 * @return success/failure of operation
1115 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1116 guint64
* r
= (guint64
*) v1
;
1117 return (*r
>= v2
.v_uint64
);
1121 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1123 * Applies the 'greater or equal' operator to the
1124 * specified structure and value
1125 * @param v1 left member of comparison
1126 * @param v2 right member of comparison
1127 * @return success/failure of operation
1129 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1130 guint32
* r
= (guint32
*) v1
;
1131 return (*r
>= v2
.v_uint32
);
1135 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1137 * Applies the 'greater or equal' operator to the
1138 * specified structure and value
1139 * @param v1 left member of comparison
1140 * @param v2 right member of comparison
1141 * @return success/failure of operation
1143 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1144 guint16
* r
= (guint16
*) v1
;
1145 return (*r
>= v2
.v_uint16
);
1149 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1151 * Applies the 'greater or equal' operator to the
1152 * specified structure and value
1153 * @param v1 left member of comparison
1154 * @param v2 right member of comparison
1155 * @return success/failure of operation
1157 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1158 double* r
= (double*) v1
;
1159 return (*r
>= v2
.v_double
);
1163 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1165 * Applies the 'greater or equal' operator to the
1166 * specified structure and value
1167 * @param v1 left member of comparison
1168 * @param v2 right member of comparison
1169 * @return success/failure of operation
1171 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1172 LttTime
* r
= (LttTime
*) v1
;
1173 // 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)));
1174 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1180 * Makes a copy of the current filter tree
1181 * @param tree pointer to the current tree
1182 * @return new copy of the filter tree
1185 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1187 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1189 newtree
->node
= tree
->node
;
1191 newtree
->left
= tree
->left
;
1192 if(newtree
->left
== LTTV_TREE_NODE
) {
1193 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1194 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1195 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1196 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1197 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1198 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1199 /* FIXME: special case for string copy ! */
1200 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1203 newtree
->right
= tree
->right
;
1204 if(newtree
->right
== LTTV_TREE_NODE
) {
1205 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1206 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1207 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1208 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1209 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1210 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1211 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1219 * Makes a copy of the current filter
1220 * @param filter pointer to the current filter
1221 * @return new copy of the filter
1224 lttv_filter_clone(const LttvFilter
* filter
) {
1226 if(!filter
) return NULL
;
1228 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1230 strcpy(newfilter
->expression
,filter
->expression
);
1232 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1240 * @fn LttvFilter* lttv_filter_new()
1242 * Creates a new LttvFilter
1243 * @return the current LttvFilter or NULL if error
1248 LttvFilter
* filter
= g_new(LttvFilter
,1);
1249 filter
->expression
= NULL
;
1250 filter
->head
= NULL
;
1257 * @fn gboolean lttv_filter_update(LttvFilter*)
1259 * Updates the current LttvFilter by building
1260 * its tree based upon the expression string
1261 * @param filter pointer to the current LttvFilter
1262 * @return Failure/Success of operation
1265 lttv_filter_update(LttvFilter
* filter
) {
1267 // g_print("filter::lttv_filter_new()\n"); /* debug */
1269 if(filter
->expression
== NULL
) return FALSE
;
1273 p_nesting
=0, /* parenthesis nesting value */
1275 guint expression_len
;
1279 *tree
= lttv_filter_tree_new(), /* main tree */
1280 *subtree
= NULL
, /* buffer for subtrees */
1281 *t1
, /* buffer #1 */
1282 *t2
, /* buffer #2 */
1283 *t3
; /* buffer #3 */
1287 * If the tree already exists,
1288 * destroy it and build a new one
1290 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1291 filter
->head
= NULL
; /* will be assigned at the end */
1295 * each element of the list
1296 * is a sub tree created
1297 * by the use of parenthesis in the
1298 * global expression. The final tree
1299 * will be the one left at the root of
1302 GPtrArray
*tree_stack
= g_ptr_array_new();
1303 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1305 /* temporary values */
1306 GString
*a_field_component
= g_string_new("");
1307 GString
*a_string_spaces
= g_string_new("");
1308 GPtrArray
*a_field_path
= g_ptr_array_new();
1310 /* simple expression buffer */
1311 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1313 gint nest_quotes
= 0;
1316 * Parse entire expression and construct
1317 * the binary tree. There are two steps
1318 * in browsing that string
1319 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1320 * 2. finding simple expressions
1321 * - field path ( separated by dots )
1322 * - op ( >, <, =, >=, <=, !=)
1323 * - value ( integer, string ... )
1324 * To spare computing time, the whole
1325 * string is parsed in this loop for a
1326 * O(n) complexity order.
1328 * When encountering logical op &,|,^
1329 * 1. parse the last value if any
1330 * 2. create a new tree
1331 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1332 * 4. concatenate this tree with the current tree on top of the stack
1333 * When encountering math ops >,>=,<,<=,=,!=
1334 * 1. add to op to the simple expression
1335 * 2. concatenate last field component to field path
1336 * When encountering concatening ops .
1337 * 1. concatenate last field component to field path
1338 * When encountering opening parenthesis (,{,[
1339 * 1. create a new subtree on top of tree stack
1340 * When encountering closing parenthesis ),},]
1341 * 1. add the expression on right child of the current tree
1342 * 2. the subtree is completed, allocate a new subtree
1343 * 3. pop the tree value from the tree stack
1347 struct timeval starttime
;
1348 struct timeval endtime
;
1349 gettimeofday(&starttime
, NULL
);
1352 expression_len
= strlen(filter
->expression
);
1353 for(i
=0;i
<expression_len
;i
++) {
1355 // g_print("%c\n ",filter->expression[i]);
1357 switch(filter
->expression
[i
]) {
1359 if(filter
->expression
[i
+1] == '\"') {
1368 if(a_string_spaces
->len
!= 0) {
1369 a_field_component
= g_string_append(
1370 a_field_component
, a_string_spaces
->str
);
1371 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1373 a_field_component
= g_string_append_c(a_field_component
,
1374 filter
->expression
[i
]);
1378 switch(filter
->expression
[i
]) {
1384 /* get current tree in tree stack */
1385 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1387 /* get current node at absolute right */
1388 while(t1
->right
!= LTTV_TREE_IDLE
) {
1389 g_assert(t1
->right
== LTTV_TREE_NODE
);
1392 t2
= lttv_filter_tree_new();
1393 t2
->node
= LTTV_LOGICAL_AND
;
1394 t1
->right
= LTTV_TREE_NODE
;
1396 if(not) { /* add not operator to tree */
1397 t3
= lttv_filter_tree_new();
1398 t3
->node
= LTTV_LOGICAL_NOT
;
1399 t2
->left
= LTTV_TREE_NODE
;
1404 if(subtree
!= NULL
) { /* append subtree to current tree */
1405 t2
->left
= LTTV_TREE_NODE
;
1406 t2
->l_child
.t
= subtree
;
1408 } else { /* append a simple expression */
1409 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1410 a_field_component
= g_string_new("");
1411 g_string_free(a_string_spaces
, TRUE
);
1412 a_string_spaces
= g_string_new("");
1413 t2
->left
= LTTV_TREE_LEAF
;
1414 t2
->l_child
.leaf
= a_simple_expression
;
1415 a_simple_expression
= lttv_simple_expression_new();
1421 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1422 while(t1
->right
!= LTTV_TREE_IDLE
) {
1423 g_assert(t1
->right
== LTTV_TREE_NODE
);
1426 t2
= lttv_filter_tree_new();
1427 t2
->node
= LTTV_LOGICAL_OR
;
1428 t1
->right
= LTTV_TREE_NODE
;
1430 if(not) { // add not operator to tree
1431 t3
= lttv_filter_tree_new();
1432 t3
->node
= LTTV_LOGICAL_NOT
;
1433 t2
->left
= LTTV_TREE_NODE
;
1438 if(subtree
!= NULL
) { /* append subtree to current tree */
1439 t2
->left
= LTTV_TREE_NODE
;
1440 t2
->l_child
.t
= subtree
;
1442 } else { /* append a simple expression */
1443 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1444 a_field_component
= g_string_new("");
1445 g_string_free(a_string_spaces
, TRUE
);
1446 a_string_spaces
= g_string_new("");
1447 t2
->left
= LTTV_TREE_LEAF
;
1448 t2
->l_child
.leaf
= a_simple_expression
;
1449 a_simple_expression
= lttv_simple_expression_new();
1455 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1456 while(t1
->right
!= LTTV_TREE_IDLE
) {
1457 g_assert(t1
->right
== LTTV_TREE_NODE
);
1460 t2
= lttv_filter_tree_new();
1461 t2
->node
= LTTV_LOGICAL_XOR
;
1462 t1
->right
= LTTV_TREE_NODE
;
1464 if(not) { // add not operator to tree
1465 t3
= lttv_filter_tree_new();
1466 t3
->node
= LTTV_LOGICAL_NOT
;
1467 t2
->left
= LTTV_TREE_NODE
;
1472 if(subtree
!= NULL
) { /* append subtree to current tree */
1473 t2
->left
= LTTV_TREE_NODE
;
1474 t2
->l_child
.t
= subtree
;
1476 } else { /* append a simple expression */
1477 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1478 a_field_component
= g_string_new("");
1479 g_string_free(a_string_spaces
, TRUE
);
1480 a_string_spaces
= g_string_new("");
1481 t2
->left
= LTTV_TREE_LEAF
;
1482 t2
->l_child
.leaf
= a_simple_expression
;
1483 a_simple_expression
= lttv_simple_expression_new();
1487 case '!': /* not, or not equal (math op) */
1489 if(filter
->expression
[i
+1] == '=') { /* != */
1490 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1491 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1492 a_field_component
= g_string_new("");
1493 g_string_free(a_string_spaces
, TRUE
);
1494 a_string_spaces
= g_string_new("");
1495 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1502 case '(': /* start of parenthesis */
1506 p_nesting
++; /* incrementing parenthesis nesting value */
1507 t1
= lttv_filter_tree_new();
1508 if(not) { /* add not operator to tree */
1509 t3
= lttv_filter_tree_new();
1510 t3
->node
= LTTV_LOGICAL_NOT
;
1511 t1
->right
= LTTV_TREE_NODE
;
1515 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1518 case ')': /* end of parenthesis */
1522 p_nesting
--; /* decrementing parenthesis nesting value */
1523 if(p_nesting
<0 || tree_stack
->len
<2) {
1524 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1525 is not valid due to parenthesis incorrect use",filter
->expression
);
1529 /* there must at least be the root tree left in the array */
1530 g_assert(tree_stack
->len
>0);
1532 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1533 while(t1
->right
!= LTTV_TREE_IDLE
) {
1536 if(not) { // add not operator to tree
1538 t3
= lttv_filter_tree_new();
1539 t3
->node
= LTTV_LOGICAL_NOT
;
1540 t1
->right
= LTTV_TREE_NODE
;
1545 if(subtree
!= NULL
) { /* append subtree to current tree */
1546 t1
->right
= LTTV_TREE_NODE
;
1547 t1
->r_child
.t
= subtree
;
1548 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1549 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1550 } else { /* assign subtree as current tree */
1551 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1552 a_field_component
= g_string_new("");
1553 g_string_free(a_string_spaces
, TRUE
);
1554 a_string_spaces
= g_string_new("");
1555 t1
->right
= LTTV_TREE_LEAF
;
1556 t1
->r_child
.leaf
= a_simple_expression
;
1557 a_simple_expression
= lttv_simple_expression_new();
1558 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1563 * mathematic operators
1565 case '<': /* lower, lower or equal */
1567 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1568 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1569 a_field_component
= g_string_new("");
1570 g_string_free(a_string_spaces
, TRUE
);
1571 a_string_spaces
= g_string_new("");
1572 if(filter
->expression
[i
+1] == '=') { /* <= */
1574 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1575 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1578 case '>': /* higher, higher or equal */
1580 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1581 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1582 a_field_component
= g_string_new("");
1583 g_string_free(a_string_spaces
, TRUE
);
1584 a_string_spaces
= g_string_new("");
1585 if(filter
->expression
[i
+1] == '=') { /* >= */
1587 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1588 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1591 case '=': /* equal */
1593 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1594 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1595 a_field_component
= g_string_new("");
1596 g_string_free(a_string_spaces
, TRUE
);
1597 a_string_spaces
= g_string_new("");
1598 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1602 * Field concatening caracter
1607 * divide field expression into elements
1608 * in a_field_path array.
1610 * A dot can also be present in double values
1612 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1613 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1614 a_field_component
= g_string_new("");
1615 g_string_free(a_string_spaces
, TRUE
);
1616 a_string_spaces
= g_string_new("");
1618 /* Operator found, we are in the value field */
1619 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1622 case ' ': /* keep spaces that are within a field component */
1623 if(a_field_component
->len
== 0) break; /* ignore */
1625 a_string_spaces
= g_string_append_c(a_string_spaces
,
1626 filter
->expression
[i
]);
1628 case '\n': /* ignore */
1631 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1633 default: /* concatening current string */
1634 if(a_string_spaces
->len
!= 0) {
1635 a_field_component
= g_string_append(
1636 a_field_component
, a_string_spaces
->str
);
1637 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1639 a_field_component
= g_string_append_c(a_field_component
,
1640 filter
->expression
[i
]);
1645 * Preliminary check to see
1646 * if tree was constructed correctly
1649 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1650 is not valid due to parenthesis incorrect use",filter
->expression
);
1654 if(tree_stack
->len
!= 1) /* only root tree should remain */
1658 * processing last element of expression
1660 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1661 while(t1
->right
!= LTTV_TREE_IDLE
) {
1662 g_assert(t1
->right
== LTTV_TREE_NODE
);
1665 if(not) { // add not operator to tree
1666 t3
= lttv_filter_tree_new();
1667 t3
->node
= LTTV_LOGICAL_NOT
;
1668 t1
->right
= LTTV_TREE_NODE
;
1673 if(subtree
!= NULL
) { /* add the subtree */
1674 t1
->right
= LTTV_TREE_NODE
;
1675 t1
->r_child
.t
= subtree
;
1677 } else { /* add a leaf */
1678 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1679 a_field_component
= NULL
;
1680 g_string_free(a_string_spaces
, TRUE
);
1681 a_string_spaces
= NULL
;
1682 t1
->right
= LTTV_TREE_LEAF
;
1683 t1
->r_child
.leaf
= a_simple_expression
;
1684 a_simple_expression
= NULL
;
1688 /* free the pointer array */
1689 g_assert(a_field_path
->len
== 0);
1690 g_ptr_array_free(a_field_path
,TRUE
);
1692 /* free the tree stack -- but keep the root tree */
1693 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1694 g_ptr_array_free(tree_stack
,TRUE
);
1696 /* free the field buffer if allocated */
1697 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1698 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1700 /* free the simple expression buffer if allocated */
1701 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1703 g_assert(filter
->head
!= NULL
); /* tree should exist */
1704 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1707 gettimeofday(&endtime
, NULL
);
1709 /* Calcul du temps de l'algorithme */
1710 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1711 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1712 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1713 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1717 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1718 lttv_print_tree(filter
->head
,0) ;
1719 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1727 * @fn void lttv_filter_destroy(LttvFilter*)
1729 * Destroy the current LttvFilter
1730 * @param filter pointer to the current LttvFilter
1733 lttv_filter_destroy(LttvFilter
* filter
) {
1737 if(filter
->expression
)
1738 g_free(filter
->expression
);
1740 lttv_filter_tree_destroy(filter
->head
);
1746 * @fn LttvFilterTree* lttv_filter_tree_new()
1748 * Assign a new tree for the current expression
1750 * @return pointer of LttvFilterTree
1753 lttv_filter_tree_new() {
1754 LttvFilterTree
* tree
;
1756 tree
= g_new(LttvFilterTree
,1);
1757 tree
->node
= 0; //g_new(lttv_expression,1);
1758 tree
->left
= LTTV_TREE_IDLE
;
1759 tree
->right
= LTTV_TREE_IDLE
;
1760 tree
->r_child
.t
= NULL
;
1761 tree
->l_child
.t
= NULL
;
1767 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1769 * Append a new expression to the expression
1770 * defined in the current filter
1771 * @param filter pointer to the current LttvFilter
1772 * @param expression string that must be appended
1773 * @return Success/Failure of operation
1776 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1778 if(expression
== NULL
) return FALSE
;
1779 if(filter
== NULL
) return FALSE
;
1780 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1782 GString
* s
= g_string_new("");
1783 if(filter
->expression
!= NULL
) {
1784 s
= g_string_append(s
,filter
->expression
);
1785 s
= g_string_append_c(s
,'&');
1787 s
= g_string_append(s
,expression
);
1789 g_free(filter
->expression
);
1790 filter
->expression
= g_string_free(s
,FALSE
);
1792 /* TRUE if construction of tree proceeded without errors */
1793 return lttv_filter_update(filter
);
1798 * @fn void lttv_filter_clear_expression(LttvFilter*)
1800 * Clear the filter expression from the
1801 * current filter and sets its pointer to NULL
1802 * @param filter pointer to the current LttvFilter
1805 lttv_filter_clear_expression(LttvFilter
* filter
) {
1807 if(filter
->expression
!= NULL
) {
1808 g_free(filter
->expression
);
1809 filter
->expression
= NULL
;
1815 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1817 * Destroys the tree and his sub-trees
1818 * @param tree Tree which must be destroyed
1821 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1823 if(tree
== NULL
) return;
1825 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1826 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1828 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1829 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1831 // g_free(tree->node);
1836 * Global parsing function for the current
1838 * @param t pointer to the current LttvFilterTree
1839 * @param event current LttEvent, NULL if not used
1840 * @param tracefile current LttTracefile, NULL if not used
1841 * @param trace current LttTrace, NULL if not used
1842 * @param state current LttvProcessState, NULL if not used
1843 * @param context current LttvTracefileContext, NULL if not used
1844 * @return response of filter
1847 lttv_filter_tree_parse(
1848 const LttvFilterTree
* t
,
1849 const LttEvent
* event
,
1850 const LttTracefile
* tracefile
,
1851 const LttTrace
* trace
,
1852 const LttvTracefileContext
* context
,
1853 const LttvProcessState
* state
,
1854 const LttvTraceContext
* tc
1859 * Each tree is parsed in inorder.
1860 * This way, it's possible to apply the left filter of the
1861 * tree, then decide whether or not the right branch should
1862 * be parsed depending on the linking logical operator
1864 * Each node consists in a
1865 * 1. logical operator
1866 * 2. left child ( node or simple expression )
1867 * 3. right child ( node or simple expression )
1869 * When the child is a simple expression, we must
1870 * before all determine if the expression refers to
1871 * a structure which is whithin observation ( not NULL ).
1872 * -If so, the expression is evaluated.
1873 * -If not, the result is set to TRUE since this particular
1874 * operation does not interfere with the lttv structure
1876 * The result of each simple expression will directly
1877 * affect the next branch. This way, depending on
1878 * the linking logical operator, the parser will decide
1879 * to explore or not the next branch.
1881 * -If result of left branch is 0 / FALSE
1882 * then don't explore right branch and return 0;
1883 * -If result of left branch is 1 / TRUE then explore
1885 * -If result of left branch is 1 / TRUE
1886 * then don't explore right branch and return 1;
1887 * -If result of left branch is 0 / FALSE then explore
1889 * -Result of left branch will not affect exploration of
1893 gboolean lresult
= FALSE
, rresult
= FALSE
;
1896 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1898 ts
= (LttvTraceState
*)tc
;
1900 ts
= (LttvTraceState
*)context
->t_context
;
1903 guint cpu
= tfs
->cpu
;
1905 state
= ts
->running_process
[cpu
];
1911 if(t
->left
== LTTV_TREE_NODE
) {
1912 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1914 else if(t
->left
== LTTV_TREE_LEAF
) {
1915 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1919 * Parse linking operator
1920 * make a cutoff if possible
1922 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1923 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1926 * Parse right branch
1928 if(t
->right
== LTTV_TREE_NODE
) {
1929 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1931 else if(t
->right
== LTTV_TREE_LEAF
) {
1932 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1937 * Apply and return the
1938 * logical link between the
1942 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1943 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1944 case LTTV_LOGICAL_NOT
:
1945 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1946 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1947 case 0: return (rresult
);
1950 * This case should never be
1951 * parsed, if so, this subtree
1960 * This function parses a particular branch of the tree
1961 * @param se pointer to the current LttvSimpleExpression
1962 * @param event current LttEvent, NULL if not used
1963 * @param tracefile current LttTracefile, NULL if not used
1964 * @param trace current LttTrace, NULL if not used
1965 * @param state current LttvProcessState, NULL if not used
1966 * @param context current LttvTracefileContext, NULL if not used
1967 * @return response of filter
1970 lttv_filter_tree_parse_branch(
1971 const LttvSimpleExpression
* se
,
1972 const LttEvent
* event
,
1973 const LttTracefile
* tracefile
,
1974 const LttTrace
* trace
,
1975 const LttvProcessState
* state
,
1976 const LttvTracefileContext
* context
) {
1981 case LTTV_FILTER_TRACE_NAME
:
1982 if(trace
== NULL
) return TRUE
;
1984 GQuark quark
= ltt_trace_name(trace
);
1985 return se
->op((gpointer
)&quark
,v
);
1988 case LTTV_FILTER_TRACEFILE_NAME
:
1989 if(tracefile
== NULL
) return TRUE
;
1991 GQuark quark
= ltt_tracefile_name(tracefile
);
1992 return se
->op((gpointer
)&quark
,v
);
1995 case LTTV_FILTER_STATE_PID
:
1996 if(state
== NULL
) return TRUE
;
1997 else return se
->op((gpointer
)&state
->pid
,v
);
1999 case LTTV_FILTER_STATE_PPID
:
2000 if(state
== NULL
) return TRUE
;
2001 else return se
->op((gpointer
)&state
->ppid
,v
);
2003 case LTTV_FILTER_STATE_CT
:
2004 if(state
== NULL
) return TRUE
;
2006 return se
->op((gpointer
)&state
->creation_time
,v
);
2009 case LTTV_FILTER_STATE_IT
:
2010 if(state
== NULL
) return TRUE
;
2012 return se
->op((gpointer
)&state
->insertion_time
,v
);
2015 case LTTV_FILTER_STATE_P_NAME
:
2016 if(state
== NULL
) return TRUE
;
2018 GQuark quark
= state
->name
;
2019 return se
->op((gpointer
)&quark
,v
);
2022 case LTTV_FILTER_STATE_T_BRAND
:
2023 if(state
== NULL
) return TRUE
;
2025 GQuark quark
= state
->brand
;
2026 return se
->op((gpointer
)&quark
,v
);
2029 case LTTV_FILTER_STATE_EX_MODE
:
2030 if(state
== NULL
) return TRUE
;
2031 else return se
->op((gpointer
)&state
->state
->t
,v
);
2033 case LTTV_FILTER_STATE_EX_SUBMODE
:
2034 if(state
== NULL
) return TRUE
;
2035 else return se
->op((gpointer
)&state
->state
->n
,v
);
2037 case LTTV_FILTER_STATE_P_STATUS
:
2038 if(state
== NULL
) return TRUE
;
2039 else return se
->op((gpointer
)&state
->state
->s
,v
);
2041 case LTTV_FILTER_STATE_CPU
:
2042 if(state
== NULL
) return TRUE
;
2044 return se
->op((gpointer
)&state
->cpu
,v
);
2047 case LTTV_FILTER_EVENT_NAME
:
2048 if(event
== NULL
) return TRUE
;
2050 struct marker_info
*info
;
2051 info
= marker_get_info_from_id((LttTrace
*)trace
, event
->event_id
);
2052 g_assert(info
!= NULL
);
2053 GQuark quark
= info
->name
;
2054 return se
->op((gpointer
)&quark
,v
);
2057 case LTTV_FILTER_EVENT_CATEGORY
:
2059 * TODO: Not yet implemented
2063 case LTTV_FILTER_EVENT_TIME
:
2064 if(event
== NULL
) return TRUE
;
2066 LttTime time
= ltt_event_time(event
);
2067 return se
->op((gpointer
)&time
,v
);
2070 case LTTV_FILTER_EVENT_TSC
:
2071 if(event
== NULL
) return TRUE
;
2073 LttCycleCount count
= ltt_event_cycle_count(event
);
2074 return se
->op((gpointer
)&count
,v
);
2077 case LTTV_FILTER_EVENT_TARGET_PID
:
2078 if(context
== NULL
) return TRUE
;
2081 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2082 return se
->op((gpointer
)&target_pid
,v
);
2085 case LTTV_FILTER_EVENT_FIELD
:
2087 * TODO: Use the offset to
2088 * find the dynamic field
2089 * in the event struct
2094 * This case should never be
2095 * parsed, if so, the whole
2096 * filtering is cancelled
2098 g_warning("Error while parsing the filter tree");
2102 /* should never get here */
2110 * Debug function. Prints tree memory allocation.
2111 * @param t the pointer to the current LttvFilterTree
2114 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2116 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2117 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2118 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2120 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2121 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"));
2122 if(t
->left
== LTTV_TREE_LEAF
) {
2123 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2124 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2125 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2127 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"));
2128 if(t
->right
== LTTV_TREE_LEAF
) {
2129 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2130 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2131 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2134 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2135 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2139 * @fn static void module_init()
2141 * Initializes the filter module and specific values
2143 static void module_init()
2149 * Destroys the filter module and specific values
2151 static void module_destroy()
2157 LTTV_MODULE("filter", "Filters traceset and events", \
2158 "Filters traceset and events specifically to user input", \
2159 module_init
, module_destroy
)