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
) {
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
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") ) {
186 * state.creation_time
187 * state.insertion_time
190 * state.execution_mode
191 * state.execution_submode
192 * state.process_status
195 g_string_free(f
,TRUE
);
196 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
197 if(!g_strcasecmp(f
->str
,"pid") ) {
198 se
->field
= LTTV_FILTER_STATE_PID
;
200 else if(!g_strcasecmp(f
->str
,"ppid") ) {
201 se
->field
= LTTV_FILTER_STATE_PPID
;
203 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
204 se
->field
= LTTV_FILTER_STATE_CT
;
206 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
207 se
->field
= LTTV_FILTER_STATE_IT
;
209 else if(!g_strcasecmp(f
->str
,"process_name") ) {
210 se
->field
= LTTV_FILTER_STATE_P_NAME
;
212 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
213 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
215 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
216 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
218 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
219 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
221 else if(!g_strcasecmp(f
->str
,"process_status") ) {
222 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
224 else if(!g_strcasecmp(f
->str
,"cpu") ) {
225 se
->field
= LTTV_FILTER_STATE_CPU
;
227 } else if(!g_strcasecmp(f
->str
,"event") ) {
237 g_string_free(f
,TRUE
);
238 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
240 if(!g_strcasecmp(f
->str
,"name") ) {
241 se
->field
= LTTV_FILTER_EVENT_NAME
;
243 else if(!g_strcasecmp(f
->str
,"category") ) {
245 * FIXME: Category not yet functional in lttv
247 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
249 else if(!g_strcasecmp(f
->str
,"time") ) {
250 se
->field
= LTTV_FILTER_EVENT_TIME
;
252 else if(!g_strcasecmp(f
->str
,"tsc") ) {
253 se
->field
= LTTV_FILTER_EVENT_TSC
;
255 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
256 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
258 else if(!g_strcasecmp(f
->str
,"field") ) {
259 se
->field
= LTTV_FILTER_EVENT_FIELD
;
260 g_string_free(f
,TRUE
);
261 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
264 //g_string_free(f,TRUE);
265 //f=ltt_g_ptr_array_remove_index_slow(fp,0);
266 g_warning("Unknown event filter subtype %s", f
->str
);
269 g_string_free(f
,TRUE
);
270 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
272 g_warning("Unrecognized field in filter string");
275 /* free memory for last string */
276 g_string_free(f
,TRUE
);
278 /* array should be empty */
279 g_assert(fp
->len
== 0);
281 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
282 g_warning("The specified field was not recognized !");
289 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
291 * Sets the function pointer for the current
293 * @param se current simple expression
294 * @param op current operator
295 * @return success/failure of operation
298 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
304 case LTTV_FILTER_TRACE_NAME
:
305 case LTTV_FILTER_TRACEFILE_NAME
:
306 case LTTV_FILTER_STATE_P_NAME
:
307 case LTTV_FILTER_STATE_T_BRAND
:
308 case LTTV_FILTER_EVENT_NAME
:
309 case LTTV_FILTER_STATE_EX_MODE
:
310 case LTTV_FILTER_STATE_EX_SUBMODE
:
311 case LTTV_FILTER_STATE_P_STATUS
:
314 se
->op
= lttv_apply_op_eq_quark
;
317 se
->op
= lttv_apply_op_ne_quark
;
320 g_warning("Error encountered in operator assignment = or != expected");
327 case LTTV_FILTER_EVENT_TSC
:
330 se
->op
= lttv_apply_op_eq_uint64
;
333 se
->op
= lttv_apply_op_ne_uint64
;
336 se
->op
= lttv_apply_op_lt_uint64
;
339 se
->op
= lttv_apply_op_le_uint64
;
342 se
->op
= lttv_apply_op_gt_uint64
;
345 se
->op
= lttv_apply_op_ge_uint64
;
348 g_warning("Error encountered in operator assignment");
355 case LTTV_FILTER_STATE_CPU
:
356 case LTTV_FILTER_STATE_PID
:
357 case LTTV_FILTER_STATE_PPID
:
358 case LTTV_FILTER_EVENT_TARGET_PID
:
361 se
->op
= lttv_apply_op_eq_uint
;
364 se
->op
= lttv_apply_op_ne_uint
;
367 se
->op
= lttv_apply_op_lt_uint
;
370 se
->op
= lttv_apply_op_le_uint
;
373 se
->op
= lttv_apply_op_gt_uint
;
376 se
->op
= lttv_apply_op_ge_uint
;
379 g_warning("Error encountered in operator assignment");
386 * Entered as string, converted to enum
388 * can only be compared with 'equal' or 'not equal' operators
390 * unsigned int of 16 bits are used here since enums
391 * should not go over 2^16-1 values
395 // case LTTV_FIELD_EQ:
396 // se->op = lttv_apply_op_eq_uint16;
398 // case LTTV_FIELD_NE:
399 // se->op = lttv_apply_op_ne_uint16;
402 // g_warning("Error encountered in operator assignment = or != expected");
409 case LTTV_FILTER_STATE_CT
:
410 case LTTV_FILTER_STATE_IT
:
411 case LTTV_FILTER_EVENT_TIME
:
414 se
->op
= lttv_apply_op_eq_ltttime
;
417 se
->op
= lttv_apply_op_ne_ltttime
;
420 se
->op
= lttv_apply_op_lt_ltttime
;
423 se
->op
= lttv_apply_op_le_ltttime
;
426 se
->op
= lttv_apply_op_gt_ltttime
;
429 se
->op
= lttv_apply_op_ge_ltttime
;
432 g_warning("Error encountered in operator assignment");
437 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
446 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
448 * Assign the value field to the current LttvSimpleExpression
449 * @param se pointer to the current LttvSimpleExpression
450 * @param value string value for simple expression
453 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
456 gboolean is_double
= FALSE
;
457 LttTime t
= ltt_time_zero
;
464 * entered as strings, converted to Quarks
466 case LTTV_FILTER_TRACE_NAME
:
467 case LTTV_FILTER_TRACEFILE_NAME
:
468 case LTTV_FILTER_STATE_P_NAME
:
469 case LTTV_FILTER_STATE_T_BRAND
:
470 case LTTV_FILTER_EVENT_NAME
:
471 case LTTV_FILTER_STATE_EX_MODE
:
472 case LTTV_FILTER_STATE_EX_SUBMODE
:
473 case LTTV_FILTER_STATE_P_STATUS
:
474 // se->value.v_string = value;
475 se
->value
.v_quark
= g_quark_from_string(value
);
479 * integer -- supposed to be uint64
481 case LTTV_FILTER_EVENT_TSC
:
482 se
->value
.v_uint64
= atoi(value
);
488 case LTTV_FILTER_STATE_PID
:
489 case LTTV_FILTER_STATE_PPID
:
490 case LTTV_FILTER_STATE_CPU
:
491 case LTTV_FILTER_EVENT_TARGET_PID
:
492 se
->value
.v_uint
= atoi(value
);
498 case LTTV_FILTER_STATE_CT
:
499 case LTTV_FILTER_STATE_IT
:
500 case LTTV_FILTER_EVENT_TIME
:
501 //se->value.v_double = atof(value);
503 * parsing logic could be optimised,
504 * but as for now, simpler this way
506 v
= g_string_new("");
507 string_len
= strlen(value
);
508 for(i
=0;i
<string_len
;i
++) {
509 if(value
[i
] == '.') {
510 /* cannot specify number with more than one '.' */
511 if(is_double
) return FALSE
;
512 else is_double
= TRUE
;
513 t
.tv_sec
= atoi(v
->str
);
514 g_string_free(v
,TRUE
);
515 v
= g_string_new("");
516 } else v
= g_string_append_c(v
,value
[i
]);
518 /* number can be integer or double */
519 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
521 t
.tv_sec
= atoi(v
->str
);
525 g_string_free(v
,TRUE
);
527 se
->value
.v_ltttime
= t
;
531 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
541 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
543 * Disallocate memory for the current
545 * @param se pointer to the current LttvSimpleExpression
548 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
550 // g_free(se->value);
551 // switch(se->field) {
552 // case LTTV_FILTER_TRACE_NAME:
553 // case LTTV_FILTER_TRACEFILE_NAME:
554 // case LTTV_FILTER_STATE_P_NAME:
555 // case LTTV_FILTER_EVENT_NAME:
556 // g_free(se->value.v_string);
564 * @fn gint lttv_struct_type(gint)
566 * Finds the structure type depending
567 * on the fields in parameters
568 * @params ft Field of the current structure
569 * @return LttvStructType enum or -1 for error
572 lttv_struct_type(gint ft
) {
575 case LTTV_FILTER_TRACE_NAME
:
576 return LTTV_FILTER_TRACE
;
578 case LTTV_FILTER_TRACEFILE_NAME
:
579 return LTTV_FILTER_TRACEFILE
;
581 case LTTV_FILTER_STATE_PID
:
582 case LTTV_FILTER_STATE_PPID
:
583 case LTTV_FILTER_STATE_CT
:
584 case LTTV_FILTER_STATE_IT
:
585 case LTTV_FILTER_STATE_P_NAME
:
586 case LTTV_FILTER_STATE_T_BRAND
:
587 case LTTV_FILTER_STATE_EX_MODE
:
588 case LTTV_FILTER_STATE_EX_SUBMODE
:
589 case LTTV_FILTER_STATE_P_STATUS
:
590 case LTTV_FILTER_STATE_CPU
:
591 return LTTV_FILTER_STATE
;
593 case LTTV_FILTER_EVENT_NAME
:
594 case LTTV_FILTER_EVENT_CATEGORY
:
595 case LTTV_FILTER_EVENT_TIME
:
596 case LTTV_FILTER_EVENT_TSC
:
597 case LTTV_FILTER_EVENT_TARGET_PID
:
598 case LTTV_FILTER_EVENT_FIELD
:
599 return LTTV_FILTER_EVENT
;
607 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
609 * Applies the 'equal' operator to the
610 * specified structure and value
611 * @param v1 left member of comparison
612 * @param v2 right member of comparison
613 * @return success/failure of operation
615 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
617 guint
* r
= (guint
*) v1
;
618 return (*r
== v2
.v_uint
);
623 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
625 * Applies the 'equal' operator to the
626 * specified structure and value
627 * @param v1 left member of comparison
628 * @param v2 right member of comparison
629 * @return success/failure of operation
631 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
633 guint64
* r
= (guint64
*) v1
;
634 return (*r
== v2
.v_uint64
);
639 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
641 * Applies the 'equal' operator to the
642 * specified structure and value
643 * @param v1 left member of comparison
644 * @param v2 right member of comparison
645 * @return success/failure of operation
647 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
648 guint32
* r
= (guint32
*) v1
;
649 return (*r
== v2
.v_uint32
);
653 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
655 * Applies the 'equal' operator to the
656 * specified structure and value
657 * @param v1 left member of comparison
658 * @param v2 right member of comparison
659 * @return success/failure of operation
661 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
662 guint16
* r
= (guint16
*) v1
;
663 return (*r
== v2
.v_uint16
);
667 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
669 * Applies the 'equal' operator to the
670 * specified structure and value
671 * @param v1 left member of comparison
672 * @param v2 right member of comparison
673 * @return success/failure of operation
675 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
676 double* r
= (double*) v1
;
677 return (*r
== v2
.v_double
);
681 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
683 * Applies the 'equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
689 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
690 char* r
= (char*) v1
;
691 return (!g_strcasecmp(r
,v2
.v_string
));
695 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
697 * Applies the 'equal' operator to the
698 * specified structure and value
699 * @param v1 left member of comparison
700 * @param v2 right member of comparison
701 * @return success/failure of operation
703 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
704 GQuark
* r
= (GQuark
*) v1
;
705 return (*r
== v2
.v_quark
);
709 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
711 * Applies the 'equal' operator to the
712 * specified structure and value
713 * @param v1 left member of comparison
714 * @param v2 right member of comparison
715 * @return success/failure of operation
717 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
718 LttTime
* r
= (LttTime
*) v1
;
719 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
723 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
725 * Applies the 'not equal' operator to the
726 * specified structure and value
727 * @param v1 left member of comparison
728 * @param v2 right member of comparison
729 * @return success/failure of operation
731 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
732 guint
* r
= (guint
*) v1
;
733 return (*r
!= v2
.v_uint
);
737 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
739 * Applies the 'not equal' operator to the
740 * specified structure and value
741 * @param v1 left member of comparison
742 * @param v2 right member of comparison
743 * @return success/failure of operation
745 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
746 guint64
* r
= (guint64
*) v1
;
747 return (*r
!= v2
.v_uint64
);
751 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
753 * Applies the 'not equal' operator to the
754 * specified structure and value
755 * @param v1 left member of comparison
756 * @param v2 right member of comparison
757 * @return success/failure of operation
759 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
760 guint32
* r
= (guint32
*) v1
;
761 return (*r
!= v2
.v_uint32
);
765 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
767 * Applies the 'not equal' operator to the
768 * specified structure and value
769 * @param v1 left member of comparison
770 * @param v2 right member of comparison
771 * @return success/failure of operation
773 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
774 guint16
* r
= (guint16
*) v1
;
775 return (*r
!= v2
.v_uint16
);
779 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
781 * Applies the 'not equal' operator to the
782 * specified structure and value
783 * @param v1 left member of comparison
784 * @param v2 right member of comparison
785 * @return success/failure of operation
787 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
788 double* r
= (double*) v1
;
789 return (*r
!= v2
.v_double
);
793 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
795 * Applies the 'not equal' operator to the
796 * specified structure and value
797 * @param v1 left member of comparison
798 * @param v2 right member of comparison
799 * @return success/failure of operation
801 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
802 char* r
= (char*) v1
;
803 return (g_strcasecmp(r
,v2
.v_string
));
807 * @fn gboolean lttv_apply_op_ne_quark(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_quark(const gpointer v1
, LttvFieldValue v2
) {
816 GQuark
* r
= (GQuark
*) v1
;
817 return (*r
!= v2
.v_quark
);
822 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
824 * Applies the 'not equal' operator to the
825 * specified structure and value
826 * @param v1 left member of comparison
827 * @param v2 right member of comparison
828 * @return success/failure of operation
830 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
831 LttTime
* r
= (LttTime
*) v1
;
832 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
836 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
838 * Applies the 'lower than' operator to the
839 * specified structure and value
840 * @param v1 left member of comparison
841 * @param v2 right member of comparison
842 * @return success/failure of operation
844 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
845 guint
* r
= (guint
*) v1
;
846 return (*r
< v2
.v_uint
);
850 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
852 * Applies the 'lower than' operator to the
853 * specified structure and value
854 * @param v1 left member of comparison
855 * @param v2 right member of comparison
856 * @return success/failure of operation
858 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
859 guint64
* r
= (guint64
*) v1
;
860 return (*r
< v2
.v_uint64
);
864 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
866 * Applies the 'lower than' operator to the
867 * specified structure and value
868 * @param v1 left member of comparison
869 * @param v2 right member of comparison
870 * @return success/failure of operation
872 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
873 guint32
* r
= (guint32
*) v1
;
874 return (*r
< v2
.v_uint32
);
878 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
880 * Applies the 'lower than' operator to the
881 * specified structure and value
882 * @param v1 left member of comparison
883 * @param v2 right member of comparison
884 * @return success/failure of operation
886 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
887 guint16
* r
= (guint16
*) v1
;
888 return (*r
< v2
.v_uint16
);
892 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
894 * Applies the 'lower than' operator to the
895 * specified structure and value
896 * @param v1 left member of comparison
897 * @param v2 right member of comparison
898 * @return success/failure of operation
900 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
901 double* r
= (double*) v1
;
902 return (*r
< v2
.v_double
);
906 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
908 * Applies the 'lower than' 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_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
915 LttTime
* r
= (LttTime
*) v1
;
916 // 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)));
917 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
921 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
923 * Applies the 'lower or equal' operator to the
924 * specified structure and value
925 * @param v1 left member of comparison
926 * @param v2 right member of comparison
927 * @return success/failure of operation
929 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
930 guint
* r
= (guint
*) v1
;
931 return (*r
<= v2
.v_uint
);
935 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
937 * Applies the 'lower or equal' operator to the
938 * specified structure and value
939 * @param v1 left member of comparison
940 * @param v2 right member of comparison
941 * @return success/failure of operation
943 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
944 guint64
* r
= (guint64
*) v1
;
945 return (*r
<= v2
.v_uint64
);
949 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
951 * Applies the 'lower or equal' operator to the
952 * specified structure and value
953 * @param v1 left member of comparison
954 * @param v2 right member of comparison
955 * @return success/failure of operation
957 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
958 guint32
* r
= (guint32
*) v1
;
959 return (*r
<= v2
.v_uint32
);
963 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
965 * Applies the 'lower or equal' operator to the
966 * specified structure and value
967 * @param v1 left member of comparison
968 * @param v2 right member of comparison
969 * @return success/failure of operation
971 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
972 guint16
* r
= (guint16
*) v1
;
973 return (*r
<= v2
.v_uint16
);
977 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
979 * Applies the 'lower or equal' operator to the
980 * specified structure and value
981 * @param v1 left member of comparison
982 * @param v2 right member of comparison
983 * @return success/failure of operation
985 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
986 double* r
= (double*) v1
;
987 return (*r
<= v2
.v_double
);
991 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
993 * Applies the 'lower or equal' operator to the
994 * specified structure and value
995 * @param v1 left member of comparison
996 * @param v2 right member of comparison
997 * @return success/failure of operation
999 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1000 LttTime
* r
= (LttTime
*) v1
;
1001 // 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)));
1002 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1007 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1009 * Applies the 'greater than' operator to the
1010 * specified structure and value
1011 * @param v1 left member of comparison
1012 * @param v2 right member of comparison
1013 * @return success/failure of operation
1015 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1016 guint
* r
= (guint
*) v1
;
1017 return (*r
> v2
.v_uint
);
1021 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1023 * Applies the 'greater than' operator to the
1024 * specified structure and value
1025 * @param v1 left member of comparison
1026 * @param v2 right member of comparison
1027 * @return success/failure of operation
1029 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1030 guint64
* r
= (guint64
*) v1
;
1031 return (*r
> v2
.v_uint64
);
1035 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1037 * Applies the 'greater than' operator to the
1038 * specified structure and value
1039 * @param v1 left member of comparison
1040 * @param v2 right member of comparison
1041 * @return success/failure of operation
1043 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1044 guint32
* r
= (guint32
*) v1
;
1045 return (*r
> v2
.v_uint32
);
1049 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1051 * Applies the 'greater than' operator to the
1052 * specified structure and value
1053 * @param v1 left member of comparison
1054 * @param v2 right member of comparison
1055 * @return success/failure of operation
1057 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1058 guint16
* r
= (guint16
*) v1
;
1059 return (*r
> v2
.v_uint16
);
1063 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1065 * Applies the 'greater than' operator to the
1066 * specified structure and value
1067 * @param v1 left member of comparison
1068 * @param v2 right member of comparison
1069 * @return success/failure of operation
1071 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1072 double* r
= (double*) v1
;
1073 return (*r
> v2
.v_double
);
1077 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1079 * Applies the 'greater than' 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_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1086 LttTime
* r
= (LttTime
*) v1
;
1087 // 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)));
1088 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1092 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1094 * Applies the 'greater or equal' operator to the
1095 * specified structure and value
1096 * @param v1 left member of comparison
1097 * @param v2 right member of comparison
1098 * @return success/failure of operation
1100 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1101 guint
* r
= (guint
*) v1
;
1102 return (*r
>= v2
.v_uint
);
1106 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1108 * Applies the 'greater or equal' operator to the
1109 * specified structure and value
1110 * @param v1 left member of comparison
1111 * @param v2 right member of comparison
1112 * @return success/failure of operation
1114 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1115 guint64
* r
= (guint64
*) v1
;
1116 return (*r
>= v2
.v_uint64
);
1120 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1122 * Applies the 'greater or equal' operator to the
1123 * specified structure and value
1124 * @param v1 left member of comparison
1125 * @param v2 right member of comparison
1126 * @return success/failure of operation
1128 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1129 guint32
* r
= (guint32
*) v1
;
1130 return (*r
>= v2
.v_uint32
);
1134 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1136 * Applies the 'greater or equal' operator to the
1137 * specified structure and value
1138 * @param v1 left member of comparison
1139 * @param v2 right member of comparison
1140 * @return success/failure of operation
1142 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1143 guint16
* r
= (guint16
*) v1
;
1144 return (*r
>= v2
.v_uint16
);
1148 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1150 * Applies the 'greater or equal' operator to the
1151 * specified structure and value
1152 * @param v1 left member of comparison
1153 * @param v2 right member of comparison
1154 * @return success/failure of operation
1156 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1157 double* r
= (double*) v1
;
1158 return (*r
>= v2
.v_double
);
1162 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1164 * Applies the 'greater or equal' operator to the
1165 * specified structure and value
1166 * @param v1 left member of comparison
1167 * @param v2 right member of comparison
1168 * @return success/failure of operation
1170 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1171 LttTime
* r
= (LttTime
*) v1
;
1172 // 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)));
1173 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1179 * Makes a copy of the current filter tree
1180 * @param tree pointer to the current tree
1181 * @return new copy of the filter tree
1184 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1186 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1188 newtree
->node
= tree
->node
;
1190 newtree
->left
= tree
->left
;
1191 if(newtree
->left
== LTTV_TREE_NODE
) {
1192 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1193 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1194 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1195 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1196 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1197 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1198 /* FIXME: special case for string copy ! */
1199 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1202 newtree
->right
= tree
->right
;
1203 if(newtree
->right
== LTTV_TREE_NODE
) {
1204 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1205 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1206 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1207 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1208 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1209 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1210 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1218 * Makes a copy of the current filter
1219 * @param filter pointer to the current filter
1220 * @return new copy of the filter
1223 lttv_filter_clone(const LttvFilter
* filter
) {
1225 if(!filter
) return NULL
;
1227 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1229 strcpy(newfilter
->expression
,filter
->expression
);
1231 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1239 * @fn LttvFilter* lttv_filter_new()
1241 * Creates a new LttvFilter
1242 * @return the current LttvFilter or NULL if error
1247 LttvFilter
* filter
= g_new(LttvFilter
,1);
1248 filter
->expression
= NULL
;
1249 filter
->head
= NULL
;
1256 * @fn gboolean lttv_filter_update(LttvFilter*)
1258 * Updates the current LttvFilter by building
1259 * its tree based upon the expression string
1260 * @param filter pointer to the current LttvFilter
1261 * @return Failure/Success of operation
1264 lttv_filter_update(LttvFilter
* filter
) {
1266 // g_print("filter::lttv_filter_new()\n"); /* debug */
1268 if(filter
->expression
== NULL
) return FALSE
;
1272 p_nesting
=0, /* parenthesis nesting value */
1274 guint expression_len
;
1278 *tree
= lttv_filter_tree_new(), /* main tree */
1279 *subtree
= NULL
, /* buffer for subtrees */
1280 *t1
, /* buffer #1 */
1281 *t2
, /* buffer #2 */
1282 *t3
; /* buffer #3 */
1286 * If the tree already exists,
1287 * destroy it and build a new one
1289 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1290 filter
->head
= NULL
; /* will be assigned at the end */
1294 * each element of the list
1295 * is a sub tree created
1296 * by the use of parenthesis in the
1297 * global expression. The final tree
1298 * will be the one left at the root of
1301 GPtrArray
*tree_stack
= g_ptr_array_new();
1302 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1304 /* temporary values */
1305 GString
*a_field_component
= g_string_new("");
1306 GString
*a_string_spaces
= g_string_new("");
1307 GPtrArray
*a_field_path
= g_ptr_array_new();
1309 /* simple expression buffer */
1310 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1312 gint nest_quotes
= 0;
1315 * Parse entire expression and construct
1316 * the binary tree. There are two steps
1317 * in browsing that string
1318 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1319 * 2. finding simple expressions
1320 * - field path ( separated by dots )
1321 * - op ( >, <, =, >=, <=, !=)
1322 * - value ( integer, string ... )
1323 * To spare computing time, the whole
1324 * string is parsed in this loop for a
1325 * O(n) complexity order.
1327 * When encountering logical op &,|,^
1328 * 1. parse the last value if any
1329 * 2. create a new tree
1330 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1331 * 4. concatenate this tree with the current tree on top of the stack
1332 * When encountering math ops >,>=,<,<=,=,!=
1333 * 1. add to op to the simple expression
1334 * 2. concatenate last field component to field path
1335 * When encountering concatening ops .
1336 * 1. concatenate last field component to field path
1337 * When encountering opening parenthesis (,{,[
1338 * 1. create a new subtree on top of tree stack
1339 * When encountering closing parenthesis ),},]
1340 * 1. add the expression on right child of the current tree
1341 * 2. the subtree is completed, allocate a new subtree
1342 * 3. pop the tree value from the tree stack
1346 struct timeval starttime
;
1347 struct timeval endtime
;
1348 gettimeofday(&starttime
, NULL
);
1351 expression_len
= strlen(filter
->expression
);
1352 for(i
=0;i
<expression_len
;i
++) {
1354 // g_print("%c\n ",filter->expression[i]);
1356 switch(filter
->expression
[i
]) {
1358 if(filter
->expression
[i
+1] == '\"') {
1367 if(a_string_spaces
->len
!= 0) {
1368 a_field_component
= g_string_append(
1369 a_field_component
, a_string_spaces
->str
);
1370 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1372 a_field_component
= g_string_append_c(a_field_component
,
1373 filter
->expression
[i
]);
1377 switch(filter
->expression
[i
]) {
1383 /* get current tree in tree stack */
1384 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1386 /* get current node at absolute right */
1387 while(t1
->right
!= LTTV_TREE_IDLE
) {
1388 g_assert(t1
->right
== LTTV_TREE_NODE
);
1391 t2
= lttv_filter_tree_new();
1392 t2
->node
= LTTV_LOGICAL_AND
;
1393 t1
->right
= LTTV_TREE_NODE
;
1395 if(not) { /* add not operator to tree */
1396 t3
= lttv_filter_tree_new();
1397 t3
->node
= LTTV_LOGICAL_NOT
;
1398 t2
->left
= LTTV_TREE_NODE
;
1403 if(subtree
!= NULL
) { /* append subtree to current tree */
1404 t2
->left
= LTTV_TREE_NODE
;
1405 t2
->l_child
.t
= subtree
;
1407 } else { /* append a simple expression */
1408 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1409 a_field_component
= g_string_new("");
1410 g_string_free(a_string_spaces
, TRUE
);
1411 a_string_spaces
= g_string_new("");
1412 t2
->left
= LTTV_TREE_LEAF
;
1413 t2
->l_child
.leaf
= a_simple_expression
;
1414 a_simple_expression
= lttv_simple_expression_new();
1420 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1421 while(t1
->right
!= LTTV_TREE_IDLE
) {
1422 g_assert(t1
->right
== LTTV_TREE_NODE
);
1425 t2
= lttv_filter_tree_new();
1426 t2
->node
= LTTV_LOGICAL_OR
;
1427 t1
->right
= LTTV_TREE_NODE
;
1429 if(not) { // add not operator to tree
1430 t3
= lttv_filter_tree_new();
1431 t3
->node
= LTTV_LOGICAL_NOT
;
1432 t2
->left
= LTTV_TREE_NODE
;
1437 if(subtree
!= NULL
) { /* append subtree to current tree */
1438 t2
->left
= LTTV_TREE_NODE
;
1439 t2
->l_child
.t
= subtree
;
1441 } else { /* append a simple expression */
1442 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1443 a_field_component
= g_string_new("");
1444 g_string_free(a_string_spaces
, TRUE
);
1445 a_string_spaces
= g_string_new("");
1446 t2
->left
= LTTV_TREE_LEAF
;
1447 t2
->l_child
.leaf
= a_simple_expression
;
1448 a_simple_expression
= lttv_simple_expression_new();
1454 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1455 while(t1
->right
!= LTTV_TREE_IDLE
) {
1456 g_assert(t1
->right
== LTTV_TREE_NODE
);
1459 t2
= lttv_filter_tree_new();
1460 t2
->node
= LTTV_LOGICAL_XOR
;
1461 t1
->right
= LTTV_TREE_NODE
;
1463 if(not) { // add not operator to tree
1464 t3
= lttv_filter_tree_new();
1465 t3
->node
= LTTV_LOGICAL_NOT
;
1466 t2
->left
= LTTV_TREE_NODE
;
1471 if(subtree
!= NULL
) { /* append subtree to current tree */
1472 t2
->left
= LTTV_TREE_NODE
;
1473 t2
->l_child
.t
= subtree
;
1475 } else { /* append a simple expression */
1476 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1477 a_field_component
= g_string_new("");
1478 g_string_free(a_string_spaces
, TRUE
);
1479 a_string_spaces
= g_string_new("");
1480 t2
->left
= LTTV_TREE_LEAF
;
1481 t2
->l_child
.leaf
= a_simple_expression
;
1482 a_simple_expression
= lttv_simple_expression_new();
1486 case '!': /* not, or not equal (math op) */
1488 if(filter
->expression
[i
+1] == '=') { /* != */
1489 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1490 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1491 a_field_component
= g_string_new("");
1492 g_string_free(a_string_spaces
, TRUE
);
1493 a_string_spaces
= g_string_new("");
1494 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1501 case '(': /* start of parenthesis */
1505 p_nesting
++; /* incrementing parenthesis nesting value */
1506 t1
= lttv_filter_tree_new();
1507 if(not) { /* add not operator to tree */
1508 t3
= lttv_filter_tree_new();
1509 t3
->node
= LTTV_LOGICAL_NOT
;
1510 t1
->right
= LTTV_TREE_NODE
;
1514 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1517 case ')': /* end of parenthesis */
1521 p_nesting
--; /* decrementing parenthesis nesting value */
1522 if(p_nesting
<0 || tree_stack
->len
<2) {
1523 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1524 is not valid due to parenthesis incorrect use",filter
->expression
);
1528 /* there must at least be the root tree left in the array */
1529 g_assert(tree_stack
->len
>0);
1531 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1532 while(t1
->right
!= LTTV_TREE_IDLE
) {
1535 if(not) { // add not operator to tree
1537 t3
= lttv_filter_tree_new();
1538 t3
->node
= LTTV_LOGICAL_NOT
;
1539 t1
->right
= LTTV_TREE_NODE
;
1544 if(subtree
!= NULL
) { /* append subtree to current tree */
1545 t1
->right
= LTTV_TREE_NODE
;
1546 t1
->r_child
.t
= subtree
;
1547 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1548 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1549 } else { /* assign subtree as current tree */
1550 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1551 a_field_component
= g_string_new("");
1552 g_string_free(a_string_spaces
, TRUE
);
1553 a_string_spaces
= g_string_new("");
1554 t1
->right
= LTTV_TREE_LEAF
;
1555 t1
->r_child
.leaf
= a_simple_expression
;
1556 a_simple_expression
= lttv_simple_expression_new();
1557 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1562 * mathematic operators
1564 case '<': /* lower, lower or equal */
1566 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1567 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1568 a_field_component
= g_string_new("");
1569 g_string_free(a_string_spaces
, TRUE
);
1570 a_string_spaces
= g_string_new("");
1571 if(filter
->expression
[i
+1] == '=') { /* <= */
1573 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1574 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1577 case '>': /* higher, higher or equal */
1579 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1580 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1581 a_field_component
= g_string_new("");
1582 g_string_free(a_string_spaces
, TRUE
);
1583 a_string_spaces
= g_string_new("");
1584 if(filter
->expression
[i
+1] == '=') { /* >= */
1586 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1587 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1590 case '=': /* equal */
1592 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1593 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1594 a_field_component
= g_string_new("");
1595 g_string_free(a_string_spaces
, TRUE
);
1596 a_string_spaces
= g_string_new("");
1597 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1601 * Field concatening caracter
1606 * divide field expression into elements
1607 * in a_field_path array.
1609 * A dot can also be present in double values
1611 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1612 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1613 a_field_component
= g_string_new("");
1614 g_string_free(a_string_spaces
, TRUE
);
1615 a_string_spaces
= g_string_new("");
1617 /* Operator found, we are in the value field */
1618 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1621 case ' ': /* keep spaces that are within a field component */
1622 if(a_field_component
->len
== 0) break; /* ignore */
1624 a_string_spaces
= g_string_append_c(a_string_spaces
,
1625 filter
->expression
[i
]);
1627 case '\n': /* ignore */
1630 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1632 default: /* concatening current string */
1633 if(a_string_spaces
->len
!= 0) {
1634 a_field_component
= g_string_append(
1635 a_field_component
, a_string_spaces
->str
);
1636 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1638 a_field_component
= g_string_append_c(a_field_component
,
1639 filter
->expression
[i
]);
1644 * Preliminary check to see
1645 * if tree was constructed correctly
1648 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1649 is not valid due to parenthesis incorrect use",filter
->expression
);
1653 if(tree_stack
->len
!= 1) /* only root tree should remain */
1657 * processing last element of expression
1659 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1660 while(t1
->right
!= LTTV_TREE_IDLE
) {
1661 g_assert(t1
->right
== LTTV_TREE_NODE
);
1664 if(not) { // add not operator to tree
1665 t3
= lttv_filter_tree_new();
1666 t3
->node
= LTTV_LOGICAL_NOT
;
1667 t1
->right
= LTTV_TREE_NODE
;
1672 if(subtree
!= NULL
) { /* add the subtree */
1673 t1
->right
= LTTV_TREE_NODE
;
1674 t1
->r_child
.t
= subtree
;
1676 } else { /* add a leaf */
1677 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1678 a_field_component
= NULL
;
1679 g_string_free(a_string_spaces
, TRUE
);
1680 a_string_spaces
= NULL
;
1681 t1
->right
= LTTV_TREE_LEAF
;
1682 t1
->r_child
.leaf
= a_simple_expression
;
1683 a_simple_expression
= NULL
;
1687 /* free the pointer array */
1688 g_assert(a_field_path
->len
== 0);
1689 g_ptr_array_free(a_field_path
,TRUE
);
1691 /* free the tree stack -- but keep the root tree */
1692 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1693 g_ptr_array_free(tree_stack
,TRUE
);
1695 /* free the field buffer if allocated */
1696 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1697 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1699 /* free the simple expression buffer if allocated */
1700 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1702 g_assert(filter
->head
!= NULL
); /* tree should exist */
1703 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1706 gettimeofday(&endtime
, NULL
);
1708 /* Calcul du temps de l'algorithme */
1709 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1710 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1711 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1712 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1716 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1717 lttv_print_tree(filter
->head
,0) ;
1718 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1726 * @fn void lttv_filter_destroy(LttvFilter*)
1728 * Destroy the current LttvFilter
1729 * @param filter pointer to the current LttvFilter
1732 lttv_filter_destroy(LttvFilter
* filter
) {
1736 if(filter
->expression
)
1737 g_free(filter
->expression
);
1739 lttv_filter_tree_destroy(filter
->head
);
1745 * @fn LttvFilterTree* lttv_filter_tree_new()
1747 * Assign a new tree for the current expression
1749 * @return pointer of LttvFilterTree
1752 lttv_filter_tree_new() {
1753 LttvFilterTree
* tree
;
1755 tree
= g_new(LttvFilterTree
,1);
1756 tree
->node
= 0; //g_new(lttv_expression,1);
1757 tree
->left
= LTTV_TREE_IDLE
;
1758 tree
->right
= LTTV_TREE_IDLE
;
1759 tree
->r_child
.t
= NULL
;
1760 tree
->l_child
.t
= NULL
;
1766 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1768 * Append a new expression to the expression
1769 * defined in the current filter
1770 * @param filter pointer to the current LttvFilter
1771 * @param expression string that must be appended
1772 * @return Success/Failure of operation
1775 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1777 if(expression
== NULL
) return FALSE
;
1778 if(filter
== NULL
) return FALSE
;
1779 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1781 GString
* s
= g_string_new("");
1782 if(filter
->expression
!= NULL
) {
1783 s
= g_string_append(s
,filter
->expression
);
1784 s
= g_string_append_c(s
,'&');
1786 s
= g_string_append(s
,expression
);
1788 g_free(filter
->expression
);
1789 filter
->expression
= g_string_free(s
,FALSE
);
1791 /* TRUE if construction of tree proceeded without errors */
1792 return lttv_filter_update(filter
);
1797 * @fn void lttv_filter_clear_expression(LttvFilter*)
1799 * Clear the filter expression from the
1800 * current filter and sets its pointer to NULL
1801 * @param filter pointer to the current LttvFilter
1804 lttv_filter_clear_expression(LttvFilter
* filter
) {
1806 if(filter
->expression
!= NULL
) {
1807 g_free(filter
->expression
);
1808 filter
->expression
= NULL
;
1814 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1816 * Destroys the tree and his sub-trees
1817 * @param tree Tree which must be destroyed
1820 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1822 if(tree
== NULL
) return;
1824 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1825 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1827 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1828 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1830 // g_free(tree->node);
1835 * Global parsing function for the current
1837 * @param t pointer to the current LttvFilterTree
1838 * @param event current LttEvent, NULL if not used
1839 * @param tracefile current LttTracefile, NULL if not used
1840 * @param trace current LttTrace, NULL if not used
1841 * @param state current LttvProcessState, NULL if not used
1842 * @param context current LttvTracefileContext, NULL if not used
1843 * @return response of filter
1846 lttv_filter_tree_parse(
1847 const LttvFilterTree
* t
,
1848 const LttEvent
* event
,
1849 const LttTracefile
* tracefile
,
1850 const LttTrace
* trace
,
1851 const LttvTracefileContext
* context
,
1852 const LttvProcessState
* state
,
1853 const LttvTraceContext
* tc
1858 * Each tree is parsed in inorder.
1859 * This way, it's possible to apply the left filter of the
1860 * tree, then decide whether or not the right branch should
1861 * be parsed depending on the linking logical operator
1863 * Each node consists in a
1864 * 1. logical operator
1865 * 2. left child ( node or simple expression )
1866 * 3. right child ( node or simple expression )
1868 * When the child is a simple expression, we must
1869 * before all determine if the expression refers to
1870 * a structure which is whithin observation ( not NULL ).
1871 * -If so, the expression is evaluated.
1872 * -If not, the result is set to TRUE since this particular
1873 * operation does not interfere with the lttv structure
1875 * The result of each simple expression will directly
1876 * affect the next branch. This way, depending on
1877 * the linking logical operator, the parser will decide
1878 * to explore or not the next branch.
1880 * -If result of left branch is 0 / FALSE
1881 * then don't explore right branch and return 0;
1882 * -If result of left branch is 1 / TRUE then explore
1884 * -If result of left branch is 1 / TRUE
1885 * then don't explore right branch and return 1;
1886 * -If result of left branch is 0 / FALSE then explore
1888 * -Result of left branch will not affect exploration of
1892 gboolean lresult
= FALSE
, rresult
= FALSE
;
1895 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1897 ts
= (LttvTraceState
*)tc
;
1899 ts
= (LttvTraceState
*)context
->t_context
;
1902 guint cpu
= tfs
->cpu
;
1904 state
= ts
->running_process
[cpu
];
1910 if(t
->left
== LTTV_TREE_NODE
) {
1911 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1913 else if(t
->left
== LTTV_TREE_LEAF
) {
1914 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1918 * Parse linking operator
1919 * make a cutoff if possible
1921 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1922 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1925 * Parse right branch
1927 if(t
->right
== LTTV_TREE_NODE
) {
1928 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1930 else if(t
->right
== LTTV_TREE_LEAF
) {
1931 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1936 * Apply and return the
1937 * logical link between the
1941 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1942 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1943 case LTTV_LOGICAL_NOT
:
1944 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1945 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1946 case 0: return (rresult
);
1949 * This case should never be
1950 * parsed, if so, this subtree
1959 * This function parses a particular branch of the tree
1960 * @param se pointer to the current LttvSimpleExpression
1961 * @param event current LttEvent, NULL if not used
1962 * @param tracefile current LttTracefile, NULL if not used
1963 * @param trace current LttTrace, NULL if not used
1964 * @param state current LttvProcessState, NULL if not used
1965 * @param context current LttvTracefileContext, NULL if not used
1966 * @return response of filter
1969 lttv_filter_tree_parse_branch(
1970 const LttvSimpleExpression
* se
,
1971 const LttEvent
* event
,
1972 const LttTracefile
* tracefile
,
1973 const LttTrace
* trace
,
1974 const LttvProcessState
* state
,
1975 const LttvTracefileContext
* context
) {
1980 case LTTV_FILTER_TRACE_NAME
:
1981 if(trace
== NULL
) return TRUE
;
1983 GQuark quark
= ltt_trace_name(trace
);
1984 return se
->op((gpointer
)&quark
,v
);
1987 case LTTV_FILTER_TRACEFILE_NAME
:
1988 if(tracefile
== NULL
) return TRUE
;
1990 GQuark quark
= ltt_tracefile_name(tracefile
);
1991 return se
->op((gpointer
)&quark
,v
);
1994 case LTTV_FILTER_STATE_PID
:
1995 if(state
== NULL
) return TRUE
;
1996 else return se
->op((gpointer
)&state
->pid
,v
);
1998 case LTTV_FILTER_STATE_PPID
:
1999 if(state
== NULL
) return TRUE
;
2000 else return se
->op((gpointer
)&state
->ppid
,v
);
2002 case LTTV_FILTER_STATE_CT
:
2003 if(state
== NULL
) return TRUE
;
2005 return se
->op((gpointer
)&state
->creation_time
,v
);
2008 case LTTV_FILTER_STATE_IT
:
2009 if(state
== NULL
) return TRUE
;
2011 return se
->op((gpointer
)&state
->insertion_time
,v
);
2014 case LTTV_FILTER_STATE_P_NAME
:
2015 if(state
== NULL
) return TRUE
;
2017 GQuark quark
= state
->name
;
2018 return se
->op((gpointer
)&quark
,v
);
2021 case LTTV_FILTER_STATE_T_BRAND
:
2022 if(state
== NULL
) return TRUE
;
2024 GQuark quark
= state
->brand
;
2025 return se
->op((gpointer
)&quark
,v
);
2028 case LTTV_FILTER_STATE_EX_MODE
:
2029 if(state
== NULL
) return TRUE
;
2030 else return se
->op((gpointer
)&state
->state
->t
,v
);
2032 case LTTV_FILTER_STATE_EX_SUBMODE
:
2033 if(state
== NULL
) return TRUE
;
2034 else return se
->op((gpointer
)&state
->state
->n
,v
);
2036 case LTTV_FILTER_STATE_P_STATUS
:
2037 if(state
== NULL
) return TRUE
;
2038 else return se
->op((gpointer
)&state
->state
->s
,v
);
2040 case LTTV_FILTER_STATE_CPU
:
2041 if(state
== NULL
) return TRUE
;
2043 return se
->op((gpointer
)&state
->cpu
,v
);
2046 case LTTV_FILTER_EVENT_NAME
:
2047 if(event
== NULL
) return TRUE
;
2049 struct marker_info
*info
;
2050 LttTracefile
*tf
= context
->tf
;
2051 info
= marker_get_info_from_id(tf
->mdata
, 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
)