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) (channel.event)
34 * | |->subname (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)
46 * |->channel (or tracefile)
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 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
145 * Parse through the specified
148 * Take note however that the
149 * 'event' subfields might change
150 * depending on values specified
151 * in core.xml file. Hence, if
152 * none of the subfields in the
153 * array match the hardcoded
154 * subfields, it will be considered
158 if(!g_strcasecmp(f
->str
,"trace") ) {
163 g_string_free(f
,TRUE
);
164 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
165 if(!g_strcasecmp(f
->str
,"name")) {
166 se
->field
= LTTV_FILTER_TRACE_NAME
;
168 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
170 * FIXME: not yet implemented !
172 } else if(!g_strcasecmp(f
->str
,"tracefile")
173 || !g_strcasecmp(f
->str
,"channel") ) {
179 g_string_free(f
,TRUE
);
180 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
181 if(!g_strcasecmp(f
->str
,"name")) {
182 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
184 } else if(!g_strcasecmp(f
->str
,"state") ) {
189 * state.creation_time
190 * state.insertion_time
193 * state.execution_mode
194 * state.execution_submode
195 * state.process_status
198 g_string_free(f
,TRUE
);
199 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
200 if(!g_strcasecmp(f
->str
,"pid") ) {
201 se
->field
= LTTV_FILTER_STATE_PID
;
203 else if(!g_strcasecmp(f
->str
,"ppid") ) {
204 se
->field
= LTTV_FILTER_STATE_PPID
;
206 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
207 se
->field
= LTTV_FILTER_STATE_CT
;
209 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
210 se
->field
= LTTV_FILTER_STATE_IT
;
212 else if(!g_strcasecmp(f
->str
,"process_name") ) {
213 se
->field
= LTTV_FILTER_STATE_P_NAME
;
215 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
216 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
218 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
219 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
221 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
222 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
224 else if(!g_strcasecmp(f
->str
,"process_status") ) {
225 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
227 else if(!g_strcasecmp(f
->str
,"cpu") ) {
228 se
->field
= LTTV_FILTER_STATE_CPU
;
230 } else if(!g_strcasecmp(f
->str
,"event") ) {
241 g_string_free(f
,TRUE
);
242 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
244 if(!g_strcasecmp(f
->str
,"name") ) {
245 se
->field
= LTTV_FILTER_EVENT_NAME
;
247 else if(!g_strcasecmp(f
->str
,"subname") ) {
248 se
->field
= LTTV_FILTER_EVENT_SUBNAME
;
250 else if(!g_strcasecmp(f
->str
,"category") ) {
252 * FIXME: Category not yet functional in lttv
254 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
256 else if(!g_strcasecmp(f
->str
,"time") ) {
257 se
->field
= LTTV_FILTER_EVENT_TIME
;
259 else if(!g_strcasecmp(f
->str
,"tsc") ) {
260 se
->field
= LTTV_FILTER_EVENT_TSC
;
262 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
263 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
265 else if(!g_strcasecmp(f
->str
,"field") ) {
266 se
->field
= LTTV_FILTER_EVENT_FIELD
;
267 g_string_free(f
,TRUE
);
268 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
271 //g_string_free(f,TRUE);
272 //f=ltt_g_ptr_array_remove_index_slow(fp,0);
273 g_warning("Unknown event filter subtype %s", f
->str
);
276 g_string_free(f
,TRUE
);
277 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
279 g_warning("Unrecognized field in filter string");
282 /* free memory for last string */
283 g_string_free(f
,TRUE
);
285 /* array should be empty */
286 g_assert(fp
->len
== 0);
288 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
289 g_warning("The specified field was not recognized !");
296 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
298 * Sets the function pointer for the current
300 * @param se current simple expression
301 * @param op current operator
302 * @return success/failure of operation
305 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
311 case LTTV_FILTER_TRACE_NAME
:
312 case LTTV_FILTER_TRACEFILE_NAME
:
313 case LTTV_FILTER_STATE_P_NAME
:
314 case LTTV_FILTER_STATE_T_BRAND
:
315 case LTTV_FILTER_EVENT_SUBNAME
:
316 case LTTV_FILTER_STATE_EX_MODE
:
317 case LTTV_FILTER_STATE_EX_SUBMODE
:
318 case LTTV_FILTER_STATE_P_STATUS
:
321 se
->op
= lttv_apply_op_eq_quark
;
324 se
->op
= lttv_apply_op_ne_quark
;
327 g_warning("Error encountered in operator assignment = or != expected");
334 case LTTV_FILTER_EVENT_NAME
:
337 se
->op
= lttv_apply_op_eq_quarks
;
340 se
->op
= lttv_apply_op_ne_quarks
;
343 g_warning("Error encountered in operator assignment = or != expected");
350 case LTTV_FILTER_EVENT_TSC
:
353 se
->op
= lttv_apply_op_eq_uint64
;
356 se
->op
= lttv_apply_op_ne_uint64
;
359 se
->op
= lttv_apply_op_lt_uint64
;
362 se
->op
= lttv_apply_op_le_uint64
;
365 se
->op
= lttv_apply_op_gt_uint64
;
368 se
->op
= lttv_apply_op_ge_uint64
;
371 g_warning("Error encountered in operator assignment");
378 case LTTV_FILTER_STATE_CPU
:
379 case LTTV_FILTER_STATE_PID
:
380 case LTTV_FILTER_STATE_PPID
:
381 case LTTV_FILTER_EVENT_TARGET_PID
:
384 se
->op
= lttv_apply_op_eq_uint
;
387 se
->op
= lttv_apply_op_ne_uint
;
390 se
->op
= lttv_apply_op_lt_uint
;
393 se
->op
= lttv_apply_op_le_uint
;
396 se
->op
= lttv_apply_op_gt_uint
;
399 se
->op
= lttv_apply_op_ge_uint
;
402 g_warning("Error encountered in operator assignment");
409 * Entered as string, converted to enum
411 * can only be compared with 'equal' or 'not equal' operators
413 * unsigned int of 16 bits are used here since enums
414 * should not go over 2^16-1 values
418 // case LTTV_FIELD_EQ:
419 // se->op = lttv_apply_op_eq_uint16;
421 // case LTTV_FIELD_NE:
422 // se->op = lttv_apply_op_ne_uint16;
425 // g_warning("Error encountered in operator assignment = or != expected");
432 case LTTV_FILTER_STATE_CT
:
433 case LTTV_FILTER_STATE_IT
:
434 case LTTV_FILTER_EVENT_TIME
:
437 se
->op
= lttv_apply_op_eq_ltttime
;
440 se
->op
= lttv_apply_op_ne_ltttime
;
443 se
->op
= lttv_apply_op_lt_ltttime
;
446 se
->op
= lttv_apply_op_le_ltttime
;
449 se
->op
= lttv_apply_op_gt_ltttime
;
452 se
->op
= lttv_apply_op_ge_ltttime
;
455 g_warning("Error encountered in operator assignment");
460 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
469 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
471 * Assign the value field to the current LttvSimpleExpression
472 * @param se pointer to the current LttvSimpleExpression
473 * @param value string value for simple expression
476 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
479 gboolean is_double
= FALSE
;
480 LttTime t
= ltt_time_zero
;
487 * entered as strings, converted to Quarks
489 case LTTV_FILTER_TRACE_NAME
:
490 case LTTV_FILTER_TRACEFILE_NAME
:
491 case LTTV_FILTER_STATE_P_NAME
:
492 case LTTV_FILTER_STATE_T_BRAND
:
493 case LTTV_FILTER_EVENT_SUBNAME
:
494 case LTTV_FILTER_STATE_EX_MODE
:
495 case LTTV_FILTER_STATE_EX_SUBMODE
:
496 case LTTV_FILTER_STATE_P_STATUS
:
497 // se->value.v_string = value;
498 se
->value
.v_quark
= g_quark_from_string(value
);
504 case LTTV_FILTER_EVENT_NAME
:
507 char *end
= strchr(value
, '.');
511 se
->value
.v_quarks
.q
[0] = g_quark_from_string(value
);
512 se
->value
.v_quarks
.q
[1] = g_quark_from_string(end
);
514 se
->value
.v_quarks
.q
[0] = (GQuark
)0;
515 se
->value
.v_quarks
.q
[1] = g_quark_from_string(value
);
521 * integer -- supposed to be uint64
523 case LTTV_FILTER_EVENT_TSC
:
524 se
->value
.v_uint64
= atoi(value
);
530 case LTTV_FILTER_STATE_PID
:
531 case LTTV_FILTER_STATE_PPID
:
532 case LTTV_FILTER_STATE_CPU
:
533 case LTTV_FILTER_EVENT_TARGET_PID
:
534 se
->value
.v_uint
= atoi(value
);
540 case LTTV_FILTER_STATE_CT
:
541 case LTTV_FILTER_STATE_IT
:
542 case LTTV_FILTER_EVENT_TIME
:
543 //se->value.v_double = atof(value);
545 * parsing logic could be optimised,
546 * but as for now, simpler this way
548 v
= g_string_new("");
549 string_len
= strlen(value
);
550 for(i
=0;i
<string_len
;i
++) {
551 if(value
[i
] == '.') {
552 /* cannot specify number with more than one '.' */
553 if(is_double
) return FALSE
;
554 else is_double
= TRUE
;
555 t
.tv_sec
= atoi(v
->str
);
556 g_string_free(v
,TRUE
);
557 v
= g_string_new("");
558 } else v
= g_string_append_c(v
,value
[i
]);
560 /* number can be integer or double */
561 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
563 t
.tv_sec
= atoi(v
->str
);
567 g_string_free(v
,TRUE
);
569 se
->value
.v_ltttime
= t
;
573 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
583 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
585 * Disallocate memory for the current
587 * @param se pointer to the current LttvSimpleExpression
590 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
592 // g_free(se->value);
593 // switch(se->field) {
594 // case LTTV_FILTER_TRACE_NAME:
595 // case LTTV_FILTER_TRACEFILE_NAME:
596 // case LTTV_FILTER_STATE_P_NAME:
597 // case LTTV_FILTER_EVENT_NAME:
598 // g_free(se->value.v_string);
606 * @fn gint lttv_struct_type(gint)
608 * Finds the structure type depending
609 * on the fields in parameters
610 * @params ft Field of the current structure
611 * @return LttvStructType enum or -1 for error
614 lttv_struct_type(gint ft
) {
617 case LTTV_FILTER_TRACE_NAME
:
618 return LTTV_FILTER_TRACE
;
620 case LTTV_FILTER_TRACEFILE_NAME
:
621 return LTTV_FILTER_TRACEFILE
;
623 case LTTV_FILTER_STATE_PID
:
624 case LTTV_FILTER_STATE_PPID
:
625 case LTTV_FILTER_STATE_CT
:
626 case LTTV_FILTER_STATE_IT
:
627 case LTTV_FILTER_STATE_P_NAME
:
628 case LTTV_FILTER_STATE_T_BRAND
:
629 case LTTV_FILTER_STATE_EX_MODE
:
630 case LTTV_FILTER_STATE_EX_SUBMODE
:
631 case LTTV_FILTER_STATE_P_STATUS
:
632 case LTTV_FILTER_STATE_CPU
:
633 return LTTV_FILTER_STATE
;
635 case LTTV_FILTER_EVENT_NAME
:
636 case LTTV_FILTER_EVENT_SUBNAME
:
637 case LTTV_FILTER_EVENT_CATEGORY
:
638 case LTTV_FILTER_EVENT_TIME
:
639 case LTTV_FILTER_EVENT_TSC
:
640 case LTTV_FILTER_EVENT_TARGET_PID
:
641 case LTTV_FILTER_EVENT_FIELD
:
642 return LTTV_FILTER_EVENT
;
650 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
652 * Applies the 'equal' operator to the
653 * specified structure and value
654 * @param v1 left member of comparison
655 * @param v2 right member of comparison
656 * @return success/failure of operation
658 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
660 guint
* r
= (guint
*) v1
;
661 return (*r
== v2
.v_uint
);
666 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
668 * Applies the 'equal' operator to the
669 * specified structure and value
670 * @param v1 left member of comparison
671 * @param v2 right member of comparison
672 * @return success/failure of operation
674 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
676 guint64
* r
= (guint64
*) v1
;
677 return (*r
== v2
.v_uint64
);
682 * @fn gboolean lttv_apply_op_eq_uint32(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_uint32(const gpointer v1
, LttvFieldValue v2
) {
691 guint32
* r
= (guint32
*) v1
;
692 return (*r
== v2
.v_uint32
);
696 * @fn gboolean lttv_apply_op_eq_uint16(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_uint16(const gpointer v1
, LttvFieldValue v2
) {
705 guint16
* r
= (guint16
*) v1
;
706 return (*r
== v2
.v_uint16
);
710 * @fn gboolean lttv_apply_op_eq_double(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_double(const gpointer v1
, LttvFieldValue v2
) {
719 double* r
= (double*) v1
;
720 return (*r
== v2
.v_double
);
724 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
726 * Applies the '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_eq_string(const gpointer v1
, LttvFieldValue v2
) {
733 char* r
= (char*) v1
;
734 return (!g_strcasecmp(r
,v2
.v_string
));
738 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
740 * Applies the '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_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
747 GQuark
* r
= (GQuark
*) v1
;
748 return (*r
== v2
.v_quark
);
752 * @fn gboolean lttv_apply_op_eq_quarks(gpointer,LttvFieldValue)
754 * Applies the '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_eq_quarks(const gpointer v1
, LttvFieldValue v2
) {
761 GQuark
*r1
= (GQuark
*) v1
;
763 if (likely(*r1
!= (GQuark
)0) && *r1
!= v2
.v_quarks
.q
[0])
765 if (*r2
!= v2
.v_quarks
.q
[1])
771 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
773 * Applies the 'equal' operator to the
774 * specified structure and value
775 * @param v1 left member of comparison
776 * @param v2 right member of comparison
777 * @return success/failure of operation
779 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
780 LttTime
* r
= (LttTime
*) v1
;
781 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
785 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
787 * Applies the 'not equal' operator to the
788 * specified structure and value
789 * @param v1 left member of comparison
790 * @param v2 right member of comparison
791 * @return success/failure of operation
793 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
794 guint
* r
= (guint
*) v1
;
795 return (*r
!= v2
.v_uint
);
799 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
801 * Applies the 'not equal' operator to the
802 * specified structure and value
803 * @param v1 left member of comparison
804 * @param v2 right member of comparison
805 * @return success/failure of operation
807 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
808 guint64
* r
= (guint64
*) v1
;
809 return (*r
!= v2
.v_uint64
);
813 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
815 * Applies the 'not equal' operator to the
816 * specified structure and value
817 * @param v1 left member of comparison
818 * @param v2 right member of comparison
819 * @return success/failure of operation
821 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
822 guint32
* r
= (guint32
*) v1
;
823 return (*r
!= v2
.v_uint32
);
827 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
829 * Applies the 'not equal' operator to the
830 * specified structure and value
831 * @param v1 left member of comparison
832 * @param v2 right member of comparison
833 * @return success/failure of operation
835 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
836 guint16
* r
= (guint16
*) v1
;
837 return (*r
!= v2
.v_uint16
);
841 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
843 * Applies the 'not equal' operator to the
844 * specified structure and value
845 * @param v1 left member of comparison
846 * @param v2 right member of comparison
847 * @return success/failure of operation
849 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
850 double* r
= (double*) v1
;
851 return (*r
!= v2
.v_double
);
855 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
857 * Applies the 'not equal' operator to the
858 * specified structure and value
859 * @param v1 left member of comparison
860 * @param v2 right member of comparison
861 * @return success/failure of operation
863 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
864 char* r
= (char*) v1
;
865 return (g_strcasecmp(r
,v2
.v_string
));
869 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
871 * Applies the 'not equal' operator to the
872 * specified structure and value
873 * @param v1 left member of comparison
874 * @param v2 right member of comparison
875 * @return success/failure of operation
877 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
878 GQuark
* r
= (GQuark
*) v1
;
879 return (*r
!= v2
.v_quark
);
883 * @fn gboolean lttv_apply_op_ne_quarks(gpointer,LttvFieldValue)
885 * Applies the 'equal' operator to the
886 * specified structure and value
887 * @param v1 left member of comparison
888 * @param v2 right member of comparison
889 * @return success/failure of operation
891 gboolean
lttv_apply_op_ne_quarks(const gpointer v1
, LttvFieldValue v2
) {
892 GQuark
*r1
= (GQuark
*) v1
;
894 if ((*r1
== (GQuark
)0 || *r1
== v2
.v_quarks
.q
[0]) && *r2
== v2
.v_quarks
.q
[1])
901 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
903 * Applies the 'not equal' operator to the
904 * specified structure and value
905 * @param v1 left member of comparison
906 * @param v2 right member of comparison
907 * @return success/failure of operation
909 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
910 LttTime
* r
= (LttTime
*) v1
;
911 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
915 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
917 * Applies the 'lower than' operator to the
918 * specified structure and value
919 * @param v1 left member of comparison
920 * @param v2 right member of comparison
921 * @return success/failure of operation
923 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
924 guint
* r
= (guint
*) v1
;
925 return (*r
< v2
.v_uint
);
929 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
931 * Applies the 'lower than' operator to the
932 * specified structure and value
933 * @param v1 left member of comparison
934 * @param v2 right member of comparison
935 * @return success/failure of operation
937 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
938 guint64
* r
= (guint64
*) v1
;
939 return (*r
< v2
.v_uint64
);
943 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
945 * Applies the 'lower than' operator to the
946 * specified structure and value
947 * @param v1 left member of comparison
948 * @param v2 right member of comparison
949 * @return success/failure of operation
951 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
952 guint32
* r
= (guint32
*) v1
;
953 return (*r
< v2
.v_uint32
);
957 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
959 * Applies the 'lower than' operator to the
960 * specified structure and value
961 * @param v1 left member of comparison
962 * @param v2 right member of comparison
963 * @return success/failure of operation
965 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
966 guint16
* r
= (guint16
*) v1
;
967 return (*r
< v2
.v_uint16
);
971 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
973 * Applies the 'lower than' operator to the
974 * specified structure and value
975 * @param v1 left member of comparison
976 * @param v2 right member of comparison
977 * @return success/failure of operation
979 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
980 double* r
= (double*) v1
;
981 return (*r
< v2
.v_double
);
985 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
987 * Applies the 'lower than' operator to the
988 * specified structure and value
989 * @param v1 left member of comparison
990 * @param v2 right member of comparison
991 * @return success/failure of operation
993 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
994 LttTime
* r
= (LttTime
*) v1
;
995 // 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)));
996 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
1000 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
1002 * Applies the 'lower or equal' operator to the
1003 * specified structure and value
1004 * @param v1 left member of comparison
1005 * @param v2 right member of comparison
1006 * @return success/failure of operation
1008 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
1009 guint
* r
= (guint
*) v1
;
1010 return (*r
<= v2
.v_uint
);
1014 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
1016 * Applies the 'lower or equal' operator to the
1017 * specified structure and value
1018 * @param v1 left member of comparison
1019 * @param v2 right member of comparison
1020 * @return success/failure of operation
1022 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
1023 guint64
* r
= (guint64
*) v1
;
1024 return (*r
<= v2
.v_uint64
);
1028 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
1030 * Applies the 'lower or equal' operator to the
1031 * specified structure and value
1032 * @param v1 left member of comparison
1033 * @param v2 right member of comparison
1034 * @return success/failure of operation
1036 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
1037 guint32
* r
= (guint32
*) v1
;
1038 return (*r
<= v2
.v_uint32
);
1042 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
1044 * Applies the 'lower or equal' operator to the
1045 * specified structure and value
1046 * @param v1 left member of comparison
1047 * @param v2 right member of comparison
1048 * @return success/failure of operation
1050 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
1051 guint16
* r
= (guint16
*) v1
;
1052 return (*r
<= v2
.v_uint16
);
1056 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
1058 * Applies the 'lower or equal' operator to the
1059 * specified structure and value
1060 * @param v1 left member of comparison
1061 * @param v2 right member of comparison
1062 * @return success/failure of operation
1064 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
1065 double* r
= (double*) v1
;
1066 return (*r
<= v2
.v_double
);
1070 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
1072 * Applies the 'lower or equal' operator to the
1073 * specified structure and value
1074 * @param v1 left member of comparison
1075 * @param v2 right member of comparison
1076 * @return success/failure of operation
1078 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1079 LttTime
* r
= (LttTime
*) v1
;
1080 // 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)));
1081 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1086 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1088 * Applies the 'greater than' operator to the
1089 * specified structure and value
1090 * @param v1 left member of comparison
1091 * @param v2 right member of comparison
1092 * @return success/failure of operation
1094 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1095 guint
* r
= (guint
*) v1
;
1096 return (*r
> v2
.v_uint
);
1100 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1102 * Applies the 'greater than' operator to the
1103 * specified structure and value
1104 * @param v1 left member of comparison
1105 * @param v2 right member of comparison
1106 * @return success/failure of operation
1108 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1109 guint64
* r
= (guint64
*) v1
;
1110 return (*r
> v2
.v_uint64
);
1114 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1116 * Applies the 'greater than' operator to the
1117 * specified structure and value
1118 * @param v1 left member of comparison
1119 * @param v2 right member of comparison
1120 * @return success/failure of operation
1122 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1123 guint32
* r
= (guint32
*) v1
;
1124 return (*r
> v2
.v_uint32
);
1128 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1130 * Applies the 'greater than' operator to the
1131 * specified structure and value
1132 * @param v1 left member of comparison
1133 * @param v2 right member of comparison
1134 * @return success/failure of operation
1136 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1137 guint16
* r
= (guint16
*) v1
;
1138 return (*r
> v2
.v_uint16
);
1142 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1144 * Applies the 'greater than' operator to the
1145 * specified structure and value
1146 * @param v1 left member of comparison
1147 * @param v2 right member of comparison
1148 * @return success/failure of operation
1150 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1151 double* r
= (double*) v1
;
1152 return (*r
> v2
.v_double
);
1156 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1158 * Applies the 'greater than' operator to the
1159 * specified structure and value
1160 * @param v1 left member of comparison
1161 * @param v2 right member of comparison
1162 * @return success/failure of operation
1164 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1165 LttTime
* r
= (LttTime
*) v1
;
1166 // 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)));
1167 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1171 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1173 * Applies the 'greater or equal' operator to the
1174 * specified structure and value
1175 * @param v1 left member of comparison
1176 * @param v2 right member of comparison
1177 * @return success/failure of operation
1179 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1180 guint
* r
= (guint
*) v1
;
1181 return (*r
>= v2
.v_uint
);
1185 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1187 * Applies the 'greater or equal' operator to the
1188 * specified structure and value
1189 * @param v1 left member of comparison
1190 * @param v2 right member of comparison
1191 * @return success/failure of operation
1193 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1194 guint64
* r
= (guint64
*) v1
;
1195 return (*r
>= v2
.v_uint64
);
1199 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1201 * Applies the 'greater or equal' operator to the
1202 * specified structure and value
1203 * @param v1 left member of comparison
1204 * @param v2 right member of comparison
1205 * @return success/failure of operation
1207 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1208 guint32
* r
= (guint32
*) v1
;
1209 return (*r
>= v2
.v_uint32
);
1213 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1215 * Applies the 'greater or equal' operator to the
1216 * specified structure and value
1217 * @param v1 left member of comparison
1218 * @param v2 right member of comparison
1219 * @return success/failure of operation
1221 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1222 guint16
* r
= (guint16
*) v1
;
1223 return (*r
>= v2
.v_uint16
);
1227 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1229 * Applies the 'greater or equal' operator to the
1230 * specified structure and value
1231 * @param v1 left member of comparison
1232 * @param v2 right member of comparison
1233 * @return success/failure of operation
1235 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1236 double* r
= (double*) v1
;
1237 return (*r
>= v2
.v_double
);
1241 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1243 * Applies the 'greater or equal' operator to the
1244 * specified structure and value
1245 * @param v1 left member of comparison
1246 * @param v2 right member of comparison
1247 * @return success/failure of operation
1249 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1250 LttTime
* r
= (LttTime
*) v1
;
1251 // 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)));
1252 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1258 * Makes a copy of the current filter tree
1259 * @param tree pointer to the current tree
1260 * @return new copy of the filter tree
1263 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1265 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1267 newtree
->node
= tree
->node
;
1269 newtree
->left
= tree
->left
;
1270 if(newtree
->left
== LTTV_TREE_NODE
) {
1271 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1272 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1273 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1274 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1275 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1276 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1277 /* FIXME: special case for string copy ! */
1278 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1281 newtree
->right
= tree
->right
;
1282 if(newtree
->right
== LTTV_TREE_NODE
) {
1283 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1284 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1285 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1286 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1287 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1288 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1289 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1297 * Makes a copy of the current filter
1298 * @param filter pointer to the current filter
1299 * @return new copy of the filter
1302 lttv_filter_clone(const LttvFilter
* filter
) {
1304 if(!filter
) return NULL
;
1306 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1308 strcpy(newfilter
->expression
,filter
->expression
);
1310 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1318 * @fn LttvFilter* lttv_filter_new()
1320 * Creates a new LttvFilter
1321 * @return the current LttvFilter or NULL if error
1326 LttvFilter
* filter
= g_new(LttvFilter
,1);
1327 filter
->expression
= NULL
;
1328 filter
->head
= NULL
;
1335 * @fn gboolean lttv_filter_update(LttvFilter*)
1337 * Updates the current LttvFilter by building
1338 * its tree based upon the expression string
1339 * @param filter pointer to the current LttvFilter
1340 * @return Failure/Success of operation
1343 lttv_filter_update(LttvFilter
* filter
) {
1345 // g_print("filter::lttv_filter_new()\n"); /* debug */
1347 if(filter
->expression
== NULL
) return FALSE
;
1351 p_nesting
=0, /* parenthesis nesting value */
1353 guint expression_len
;
1357 *tree
= lttv_filter_tree_new(), /* main tree */
1358 *subtree
= NULL
, /* buffer for subtrees */
1359 *t1
, /* buffer #1 */
1360 *t2
, /* buffer #2 */
1361 *t3
; /* buffer #3 */
1365 * If the tree already exists,
1366 * destroy it and build a new one
1368 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1369 filter
->head
= NULL
; /* will be assigned at the end */
1373 * each element of the list
1374 * is a sub tree created
1375 * by the use of parenthesis in the
1376 * global expression. The final tree
1377 * will be the one left at the root of
1380 GPtrArray
*tree_stack
= g_ptr_array_new();
1381 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1383 /* temporary values */
1384 GString
*a_field_component
= g_string_new("");
1385 GString
*a_string_spaces
= g_string_new("");
1386 GPtrArray
*a_field_path
= g_ptr_array_new();
1388 /* simple expression buffer */
1389 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1391 gint nest_quotes
= 0;
1394 * Parse entire expression and construct
1395 * the binary tree. There are two steps
1396 * in browsing that string
1397 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1398 * 2. finding simple expressions
1399 * - field path ( separated by dots )
1400 * - op ( >, <, =, >=, <=, !=)
1401 * - value ( integer, string ... )
1402 * To spare computing time, the whole
1403 * string is parsed in this loop for a
1404 * O(n) complexity order.
1406 * When encountering logical op &,|,^
1407 * 1. parse the last value if any
1408 * 2. create a new tree
1409 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1410 * 4. concatenate this tree with the current tree on top of the stack
1411 * When encountering math ops >,>=,<,<=,=,!=
1412 * 1. add to op to the simple expression
1413 * 2. concatenate last field component to field path
1414 * When encountering concatening ops .
1415 * 1. concatenate last field component to field path
1416 * When encountering opening parenthesis (,{,[
1417 * 1. create a new subtree on top of tree stack
1418 * When encountering closing parenthesis ),},]
1419 * 1. add the expression on right child of the current tree
1420 * 2. the subtree is completed, allocate a new subtree
1421 * 3. pop the tree value from the tree stack
1425 struct timeval starttime
;
1426 struct timeval endtime
;
1427 gettimeofday(&starttime
, NULL
);
1430 expression_len
= strlen(filter
->expression
);
1431 for(i
=0;i
<expression_len
;i
++) {
1433 // g_print("%c\n ",filter->expression[i]);
1435 switch(filter
->expression
[i
]) {
1437 if(filter
->expression
[i
+1] == '\"') {
1446 if(a_string_spaces
->len
!= 0) {
1447 a_field_component
= g_string_append(
1448 a_field_component
, a_string_spaces
->str
);
1449 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1451 a_field_component
= g_string_append_c(a_field_component
,
1452 filter
->expression
[i
]);
1456 switch(filter
->expression
[i
]) {
1462 /* get current tree in tree stack */
1463 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1465 /* get current node at absolute right */
1466 while(t1
->right
!= LTTV_TREE_IDLE
) {
1467 g_assert(t1
->right
== LTTV_TREE_NODE
);
1470 t2
= lttv_filter_tree_new();
1471 t2
->node
= LTTV_LOGICAL_AND
;
1472 t1
->right
= LTTV_TREE_NODE
;
1474 if(not) { /* add not operator to tree */
1475 t3
= lttv_filter_tree_new();
1476 t3
->node
= LTTV_LOGICAL_NOT
;
1477 t2
->left
= LTTV_TREE_NODE
;
1482 if(subtree
!= NULL
) { /* append subtree to current tree */
1483 t2
->left
= LTTV_TREE_NODE
;
1484 t2
->l_child
.t
= subtree
;
1486 } else { /* append a simple expression */
1487 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1488 a_field_component
= g_string_new("");
1489 g_string_free(a_string_spaces
, TRUE
);
1490 a_string_spaces
= g_string_new("");
1491 t2
->left
= LTTV_TREE_LEAF
;
1492 t2
->l_child
.leaf
= a_simple_expression
;
1493 a_simple_expression
= lttv_simple_expression_new();
1499 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1500 while(t1
->right
!= LTTV_TREE_IDLE
) {
1501 g_assert(t1
->right
== LTTV_TREE_NODE
);
1504 t2
= lttv_filter_tree_new();
1505 t2
->node
= LTTV_LOGICAL_OR
;
1506 t1
->right
= LTTV_TREE_NODE
;
1508 if(not) { // add not operator to tree
1509 t3
= lttv_filter_tree_new();
1510 t3
->node
= LTTV_LOGICAL_NOT
;
1511 t2
->left
= LTTV_TREE_NODE
;
1516 if(subtree
!= NULL
) { /* append subtree to current tree */
1517 t2
->left
= LTTV_TREE_NODE
;
1518 t2
->l_child
.t
= subtree
;
1520 } else { /* append a simple expression */
1521 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1522 a_field_component
= g_string_new("");
1523 g_string_free(a_string_spaces
, TRUE
);
1524 a_string_spaces
= g_string_new("");
1525 t2
->left
= LTTV_TREE_LEAF
;
1526 t2
->l_child
.leaf
= a_simple_expression
;
1527 a_simple_expression
= lttv_simple_expression_new();
1533 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1534 while(t1
->right
!= LTTV_TREE_IDLE
) {
1535 g_assert(t1
->right
== LTTV_TREE_NODE
);
1538 t2
= lttv_filter_tree_new();
1539 t2
->node
= LTTV_LOGICAL_XOR
;
1540 t1
->right
= LTTV_TREE_NODE
;
1542 if(not) { // add not operator to tree
1543 t3
= lttv_filter_tree_new();
1544 t3
->node
= LTTV_LOGICAL_NOT
;
1545 t2
->left
= LTTV_TREE_NODE
;
1550 if(subtree
!= NULL
) { /* append subtree to current tree */
1551 t2
->left
= LTTV_TREE_NODE
;
1552 t2
->l_child
.t
= subtree
;
1554 } else { /* append a simple expression */
1555 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1556 a_field_component
= g_string_new("");
1557 g_string_free(a_string_spaces
, TRUE
);
1558 a_string_spaces
= g_string_new("");
1559 t2
->left
= LTTV_TREE_LEAF
;
1560 t2
->l_child
.leaf
= a_simple_expression
;
1561 a_simple_expression
= lttv_simple_expression_new();
1565 case '!': /* not, or not equal (math op) */
1567 if(filter
->expression
[i
+1] == '=') { /* != */
1568 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1569 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1570 a_field_component
= g_string_new("");
1571 g_string_free(a_string_spaces
, TRUE
);
1572 a_string_spaces
= g_string_new("");
1573 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1580 case '(': /* start of parenthesis */
1584 p_nesting
++; /* incrementing parenthesis nesting value */
1585 t1
= lttv_filter_tree_new();
1586 if(not) { /* add not operator to tree */
1587 t3
= lttv_filter_tree_new();
1588 t3
->node
= LTTV_LOGICAL_NOT
;
1589 t1
->right
= LTTV_TREE_NODE
;
1593 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1596 case ')': /* end of parenthesis */
1600 p_nesting
--; /* decrementing parenthesis nesting value */
1601 if(p_nesting
<0 || tree_stack
->len
<2) {
1602 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1603 is not valid due to parenthesis incorrect use",filter
->expression
);
1607 /* there must at least be the root tree left in the array */
1608 g_assert(tree_stack
->len
>0);
1610 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1611 while(t1
->right
!= LTTV_TREE_IDLE
) {
1614 if(not) { // add not operator to tree
1616 t3
= lttv_filter_tree_new();
1617 t3
->node
= LTTV_LOGICAL_NOT
;
1618 t1
->right
= LTTV_TREE_NODE
;
1623 if(subtree
!= NULL
) { /* append subtree to current tree */
1624 t1
->right
= LTTV_TREE_NODE
;
1625 t1
->r_child
.t
= subtree
;
1626 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1627 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1628 } else { /* assign subtree as current tree */
1629 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1630 a_field_component
= g_string_new("");
1631 g_string_free(a_string_spaces
, TRUE
);
1632 a_string_spaces
= g_string_new("");
1633 t1
->right
= LTTV_TREE_LEAF
;
1634 t1
->r_child
.leaf
= a_simple_expression
;
1635 a_simple_expression
= lttv_simple_expression_new();
1636 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1641 * mathematic operators
1643 case '<': /* lower, lower or equal */
1645 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1646 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1647 a_field_component
= g_string_new("");
1648 g_string_free(a_string_spaces
, TRUE
);
1649 a_string_spaces
= g_string_new("");
1650 if(filter
->expression
[i
+1] == '=') { /* <= */
1652 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1653 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1656 case '>': /* higher, higher or equal */
1658 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1659 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1660 a_field_component
= g_string_new("");
1661 g_string_free(a_string_spaces
, TRUE
);
1662 a_string_spaces
= g_string_new("");
1663 if(filter
->expression
[i
+1] == '=') { /* >= */
1665 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1666 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1669 case '=': /* equal */
1671 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1672 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1673 a_field_component
= g_string_new("");
1674 g_string_free(a_string_spaces
, TRUE
);
1675 a_string_spaces
= g_string_new("");
1676 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1680 * Field concatening caracter
1685 * divide field expression into elements
1686 * in a_field_path array.
1688 * A dot can also be present in double values
1690 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1691 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1692 a_field_component
= g_string_new("");
1693 g_string_free(a_string_spaces
, TRUE
);
1694 a_string_spaces
= g_string_new("");
1696 /* Operator found, we are in the value field */
1697 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1700 case ' ': /* keep spaces that are within a field component */
1701 if(a_field_component
->len
== 0) break; /* ignore */
1703 a_string_spaces
= g_string_append_c(a_string_spaces
,
1704 filter
->expression
[i
]);
1706 case '\n': /* ignore */
1709 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1711 default: /* concatening current string */
1712 if(a_string_spaces
->len
!= 0) {
1713 a_field_component
= g_string_append(
1714 a_field_component
, a_string_spaces
->str
);
1715 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1717 a_field_component
= g_string_append_c(a_field_component
,
1718 filter
->expression
[i
]);
1723 * Preliminary check to see
1724 * if tree was constructed correctly
1727 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1728 is not valid due to parenthesis incorrect use",filter
->expression
);
1732 if(tree_stack
->len
!= 1) /* only root tree should remain */
1736 * processing last element of expression
1738 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1739 while(t1
->right
!= LTTV_TREE_IDLE
) {
1740 g_assert(t1
->right
== LTTV_TREE_NODE
);
1743 if(not) { // add not operator to tree
1744 t3
= lttv_filter_tree_new();
1745 t3
->node
= LTTV_LOGICAL_NOT
;
1746 t1
->right
= LTTV_TREE_NODE
;
1751 if(subtree
!= NULL
) { /* add the subtree */
1752 t1
->right
= LTTV_TREE_NODE
;
1753 t1
->r_child
.t
= subtree
;
1755 } else { /* add a leaf */
1756 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1757 a_field_component
= NULL
;
1758 g_string_free(a_string_spaces
, TRUE
);
1759 a_string_spaces
= NULL
;
1760 t1
->right
= LTTV_TREE_LEAF
;
1761 t1
->r_child
.leaf
= a_simple_expression
;
1762 a_simple_expression
= NULL
;
1766 /* free the pointer array */
1767 g_assert(a_field_path
->len
== 0);
1768 g_ptr_array_free(a_field_path
,TRUE
);
1770 /* free the tree stack -- but keep the root tree */
1771 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1772 g_ptr_array_free(tree_stack
,TRUE
);
1774 /* free the field buffer if allocated */
1775 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1776 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1778 /* free the simple expression buffer if allocated */
1779 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1781 g_assert(filter
->head
!= NULL
); /* tree should exist */
1782 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1785 gettimeofday(&endtime
, NULL
);
1787 /* Calcul du temps de l'algorithme */
1788 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1789 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1790 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1791 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1795 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1796 lttv_print_tree(filter
->head
,0) ;
1797 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1805 * @fn void lttv_filter_destroy(LttvFilter*)
1807 * Destroy the current LttvFilter
1808 * @param filter pointer to the current LttvFilter
1811 lttv_filter_destroy(LttvFilter
* filter
) {
1815 if(filter
->expression
)
1816 g_free(filter
->expression
);
1818 lttv_filter_tree_destroy(filter
->head
);
1824 * @fn LttvFilterTree* lttv_filter_tree_new()
1826 * Assign a new tree for the current expression
1828 * @return pointer of LttvFilterTree
1831 lttv_filter_tree_new() {
1832 LttvFilterTree
* tree
;
1834 tree
= g_new(LttvFilterTree
,1);
1835 tree
->node
= 0; //g_new(lttv_expression,1);
1836 tree
->left
= LTTV_TREE_IDLE
;
1837 tree
->right
= LTTV_TREE_IDLE
;
1838 tree
->r_child
.t
= NULL
;
1839 tree
->l_child
.t
= NULL
;
1845 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1847 * Append a new expression to the expression
1848 * defined in the current filter
1849 * @param filter pointer to the current LttvFilter
1850 * @param expression string that must be appended
1851 * @return Success/Failure of operation
1854 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1856 if(expression
== NULL
) return FALSE
;
1857 if(filter
== NULL
) return FALSE
;
1858 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1860 GString
* s
= g_string_new("");
1861 if(filter
->expression
!= NULL
) {
1862 s
= g_string_append(s
,filter
->expression
);
1863 s
= g_string_append_c(s
,'&');
1865 s
= g_string_append(s
,expression
);
1867 g_free(filter
->expression
);
1868 filter
->expression
= g_string_free(s
,FALSE
);
1870 /* TRUE if construction of tree proceeded without errors */
1871 return lttv_filter_update(filter
);
1876 * @fn void lttv_filter_clear_expression(LttvFilter*)
1878 * Clear the filter expression from the
1879 * current filter and sets its pointer to NULL
1880 * @param filter pointer to the current LttvFilter
1883 lttv_filter_clear_expression(LttvFilter
* filter
) {
1885 if(filter
->expression
!= NULL
) {
1886 g_free(filter
->expression
);
1887 filter
->expression
= NULL
;
1893 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1895 * Destroys the tree and his sub-trees
1896 * @param tree Tree which must be destroyed
1899 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1901 if(tree
== NULL
) return;
1903 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1904 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1906 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1907 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1909 // g_free(tree->node);
1914 * Global parsing function for the current
1916 * @param t pointer to the current LttvFilterTree
1917 * @param event current LttEvent, NULL if not used
1918 * @param tracefile current LttTracefile, NULL if not used
1919 * @param trace current LttTrace, NULL if not used
1920 * @param state current LttvProcessState, NULL if not used
1921 * @param context current LttvTracefileContext, NULL if not used
1922 * @return response of filter
1925 lttv_filter_tree_parse(
1926 const LttvFilterTree
* t
,
1927 const LttEvent
* event
,
1928 const LttTracefile
* tracefile
,
1929 const LttTrace
* trace
,
1930 const LttvTracefileContext
* context
,
1931 const LttvProcessState
* state
,
1932 const LttvTraceContext
* tc
1937 * Each tree is parsed in inorder.
1938 * This way, it's possible to apply the left filter of the
1939 * tree, then decide whether or not the right branch should
1940 * be parsed depending on the linking logical operator
1942 * Each node consists in a
1943 * 1. logical operator
1944 * 2. left child ( node or simple expression )
1945 * 3. right child ( node or simple expression )
1947 * When the child is a simple expression, we must
1948 * before all determine if the expression refers to
1949 * a structure which is whithin observation ( not NULL ).
1950 * -If so, the expression is evaluated.
1951 * -If not, the result is set to TRUE since this particular
1952 * operation does not interfere with the lttv structure
1954 * The result of each simple expression will directly
1955 * affect the next branch. This way, depending on
1956 * the linking logical operator, the parser will decide
1957 * to explore or not the next branch.
1959 * -If result of left branch is 0 / FALSE
1960 * then don't explore right branch and return 0;
1961 * -If result of left branch is 1 / TRUE then explore
1963 * -If result of left branch is 1 / TRUE
1964 * then don't explore right branch and return 1;
1965 * -If result of left branch is 0 / FALSE then explore
1967 * -Result of left branch will not affect exploration of
1971 gboolean lresult
= FALSE
, rresult
= FALSE
;
1973 LttvTraceState
*ts
= NULL
;
1974 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1976 ts
= (LttvTraceState
*)tc
;
1978 ts
= (LttvTraceState
*)context
->t_context
;
1981 guint cpu
= tfs
->cpu
;
1983 state
= ts
->running_process
[cpu
];
1989 if(t
->left
== LTTV_TREE_NODE
) {
1990 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1992 else if(t
->left
== LTTV_TREE_LEAF
) {
1993 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1997 * Parse linking operator
1998 * make a cutoff if possible
2000 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
2001 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
2004 * Parse right branch
2006 if(t
->right
== LTTV_TREE_NODE
) {
2007 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
2009 else if(t
->right
== LTTV_TREE_LEAF
) {
2010 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
2015 * Apply and return the
2016 * logical link between the
2020 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
2021 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
2022 case LTTV_LOGICAL_NOT
:
2023 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
2024 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
2025 case 0: return (rresult
);
2028 * This case should never be
2029 * parsed, if so, this subtree
2038 * This function parses a particular branch of the tree
2039 * @param se pointer to the current LttvSimpleExpression
2040 * @param event current LttEvent, NULL if not used
2041 * @param tracefile current LttTracefile, NULL if not used
2042 * @param trace current LttTrace, NULL if not used
2043 * @param state current LttvProcessState, NULL if not used
2044 * @param context current LttvTracefileContext, NULL if not used
2045 * @return response of filter
2048 lttv_filter_tree_parse_branch(
2049 const LttvSimpleExpression
* se
,
2050 const LttEvent
* event
,
2051 const LttTracefile
* tracefile
,
2052 const LttTrace
* trace
,
2053 const LttvProcessState
* state
,
2054 const LttvTracefileContext
* context
) {
2059 case LTTV_FILTER_TRACE_NAME
:
2060 if(trace
== NULL
) return TRUE
;
2062 GQuark quark
= ltt_trace_name(trace
);
2063 return se
->op((gpointer
)&quark
,v
);
2066 case LTTV_FILTER_TRACEFILE_NAME
:
2067 if(tracefile
== NULL
) return TRUE
;
2069 GQuark quark
= ltt_tracefile_name(tracefile
);
2070 return se
->op((gpointer
)&quark
,v
);
2073 case LTTV_FILTER_STATE_PID
:
2074 if(state
== NULL
) return TRUE
;
2075 else return se
->op((gpointer
)&state
->pid
,v
);
2077 case LTTV_FILTER_STATE_PPID
:
2078 if(state
== NULL
) return TRUE
;
2079 else return se
->op((gpointer
)&state
->ppid
,v
);
2081 case LTTV_FILTER_STATE_CT
:
2082 if(state
== NULL
) return TRUE
;
2084 return se
->op((gpointer
)&state
->creation_time
,v
);
2087 case LTTV_FILTER_STATE_IT
:
2088 if(state
== NULL
) return TRUE
;
2090 return se
->op((gpointer
)&state
->insertion_time
,v
);
2093 case LTTV_FILTER_STATE_P_NAME
:
2094 if(state
== NULL
) return TRUE
;
2096 GQuark quark
= state
->name
;
2097 return se
->op((gpointer
)&quark
,v
);
2100 case LTTV_FILTER_STATE_T_BRAND
:
2101 if(state
== NULL
) return TRUE
;
2103 GQuark quark
= state
->brand
;
2104 return se
->op((gpointer
)&quark
,v
);
2107 case LTTV_FILTER_STATE_EX_MODE
:
2108 if(state
== NULL
) return TRUE
;
2109 else return se
->op((gpointer
)&state
->state
->t
,v
);
2111 case LTTV_FILTER_STATE_EX_SUBMODE
:
2112 if(state
== NULL
) return TRUE
;
2113 else return se
->op((gpointer
)&state
->state
->n
,v
);
2115 case LTTV_FILTER_STATE_P_STATUS
:
2116 if(state
== NULL
) return TRUE
;
2117 else return se
->op((gpointer
)&state
->state
->s
,v
);
2119 case LTTV_FILTER_STATE_CPU
:
2120 if(state
== NULL
) return TRUE
;
2122 return se
->op((gpointer
)&state
->cpu
,v
);
2125 case LTTV_FILTER_EVENT_NAME
:
2126 if(event
== NULL
) return TRUE
;
2128 struct marker_info
*info
;
2130 LttTracefile
*tf
= context
->tf
;
2131 qtuple
[0] = ltt_tracefile_name(tracefile
);
2132 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2133 g_assert(info
!= NULL
);
2134 qtuple
[1] = info
->name
;
2135 return se
->op((gpointer
)qtuple
,v
);
2138 case LTTV_FILTER_EVENT_SUBNAME
:
2139 if(event
== NULL
) return TRUE
;
2141 struct marker_info
*info
;
2142 LttTracefile
*tf
= context
->tf
;
2143 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2144 g_assert(info
!= NULL
);
2145 GQuark quark
= info
->name
;
2146 return se
->op((gpointer
)&quark
,v
);
2149 case LTTV_FILTER_EVENT_CATEGORY
:
2151 * TODO: Not yet implemented
2155 case LTTV_FILTER_EVENT_TIME
:
2156 if(event
== NULL
) return TRUE
;
2158 LttTime time
= ltt_event_time(event
);
2159 return se
->op((gpointer
)&time
,v
);
2162 case LTTV_FILTER_EVENT_TSC
:
2163 if(event
== NULL
) return TRUE
;
2165 LttCycleCount count
= ltt_event_cycle_count(event
);
2166 return se
->op((gpointer
)&count
,v
);
2169 case LTTV_FILTER_EVENT_TARGET_PID
:
2170 if(context
== NULL
) return TRUE
;
2173 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2174 return se
->op((gpointer
)&target_pid
,v
);
2177 case LTTV_FILTER_EVENT_FIELD
:
2179 * TODO: Use the offset to
2180 * find the dynamic field
2181 * in the event struct
2186 * This case should never be
2187 * parsed, if so, the whole
2188 * filtering is cancelled
2190 g_warning("Error while parsing the filter tree");
2194 /* should never get here */
2202 * Debug function. Prints tree memory allocation.
2203 * @param t the pointer to the current LttvFilterTree
2206 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2208 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2209 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2210 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2212 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2213 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"));
2214 if(t
->left
== LTTV_TREE_LEAF
) {
2215 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2216 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2217 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2219 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"));
2220 if(t
->right
== LTTV_TREE_LEAF
) {
2221 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2222 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2223 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2226 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2227 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2231 * @fn static void module_init()
2233 * Initializes the filter module and specific values
2235 static void module_init()
2241 * Destroys the filter module and specific values
2243 static void module_destroy()
2249 LTTV_MODULE("filter", "Filters traceset and events", \
2250 "Filters traceset and events specifically to user input", \
2251 module_init
, module_destroy
)