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 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")
172 || !g_strcasecmp(f
->str
,"channel") ) {
178 g_string_free(f
,TRUE
);
179 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
180 if(!g_strcasecmp(f
->str
,"name")) {
181 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
183 } else if(!g_strcasecmp(f
->str
,"state") ) {
188 * state.creation_time
189 * state.insertion_time
192 * state.execution_mode
193 * state.execution_submode
194 * state.process_status
197 g_string_free(f
,TRUE
);
198 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
199 if(!g_strcasecmp(f
->str
,"pid") ) {
200 se
->field
= LTTV_FILTER_STATE_PID
;
202 else if(!g_strcasecmp(f
->str
,"ppid") ) {
203 se
->field
= LTTV_FILTER_STATE_PPID
;
205 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
206 se
->field
= LTTV_FILTER_STATE_CT
;
208 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
209 se
->field
= LTTV_FILTER_STATE_IT
;
211 else if(!g_strcasecmp(f
->str
,"process_name") ) {
212 se
->field
= LTTV_FILTER_STATE_P_NAME
;
214 else if(!g_strcasecmp(f
->str
,"thread_brand") ) {
215 se
->field
= LTTV_FILTER_STATE_T_BRAND
;
217 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
218 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
220 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
221 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
223 else if(!g_strcasecmp(f
->str
,"process_status") ) {
224 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
226 else if(!g_strcasecmp(f
->str
,"cpu") ) {
227 se
->field
= LTTV_FILTER_STATE_CPU
;
229 } else if(!g_strcasecmp(f
->str
,"event") ) {
240 g_string_free(f
,TRUE
);
241 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
243 if(!g_strcasecmp(f
->str
,"name") ) {
244 se
->field
= LTTV_FILTER_EVENT_NAME
;
246 else if(!g_strcasecmp(f
->str
,"subname") ) {
247 se
->field
= LTTV_FILTER_EVENT_SUBNAME
;
249 else if(!g_strcasecmp(f
->str
,"category") ) {
251 * FIXME: Category not yet functional in lttv
253 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
255 else if(!g_strcasecmp(f
->str
,"time") ) {
256 se
->field
= LTTV_FILTER_EVENT_TIME
;
258 else if(!g_strcasecmp(f
->str
,"tsc") ) {
259 se
->field
= LTTV_FILTER_EVENT_TSC
;
261 else if(!g_strcasecmp(f
->str
,"target_pid") ) {
262 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
264 else if(!g_strcasecmp(f
->str
,"field") ) {
265 se
->field
= LTTV_FILTER_EVENT_FIELD
;
266 g_string_free(f
,TRUE
);
267 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
270 //g_string_free(f,TRUE);
271 //f=ltt_g_ptr_array_remove_index_slow(fp,0);
272 g_warning("Unknown event filter subtype %s", f
->str
);
275 g_string_free(f
,TRUE
);
276 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
278 g_warning("Unrecognized field in filter string");
281 /* free memory for last string */
282 g_string_free(f
,TRUE
);
284 /* array should be empty */
285 g_assert(fp
->len
== 0);
287 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
288 g_warning("The specified field was not recognized !");
295 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
297 * Sets the function pointer for the current
299 * @param se current simple expression
300 * @param op current operator
301 * @return success/failure of operation
304 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
310 case LTTV_FILTER_TRACE_NAME
:
311 case LTTV_FILTER_TRACEFILE_NAME
:
312 case LTTV_FILTER_STATE_P_NAME
:
313 case LTTV_FILTER_STATE_T_BRAND
:
314 case LTTV_FILTER_EVENT_SUBNAME
:
315 case LTTV_FILTER_STATE_EX_MODE
:
316 case LTTV_FILTER_STATE_EX_SUBMODE
:
317 case LTTV_FILTER_STATE_P_STATUS
:
320 se
->op
= lttv_apply_op_eq_quark
;
323 se
->op
= lttv_apply_op_ne_quark
;
326 g_warning("Error encountered in operator assignment = or != expected");
333 case LTTV_FILTER_EVENT_NAME
:
336 se
->op
= lttv_apply_op_eq_quarks
;
339 se
->op
= lttv_apply_op_ne_quarks
;
342 g_warning("Error encountered in operator assignment = or != expected");
348 case LTTV_FILTER_EVENT_TSC
:
351 se
->op
= lttv_apply_op_eq_uint64
;
354 se
->op
= lttv_apply_op_ne_uint64
;
357 se
->op
= lttv_apply_op_lt_uint64
;
360 se
->op
= lttv_apply_op_le_uint64
;
363 se
->op
= lttv_apply_op_gt_uint64
;
366 se
->op
= lttv_apply_op_ge_uint64
;
369 g_warning("Error encountered in operator assignment");
376 case LTTV_FILTER_STATE_CPU
:
377 case LTTV_FILTER_STATE_PID
:
378 case LTTV_FILTER_STATE_PPID
:
379 case LTTV_FILTER_EVENT_TARGET_PID
:
382 se
->op
= lttv_apply_op_eq_uint
;
385 se
->op
= lttv_apply_op_ne_uint
;
388 se
->op
= lttv_apply_op_lt_uint
;
391 se
->op
= lttv_apply_op_le_uint
;
394 se
->op
= lttv_apply_op_gt_uint
;
397 se
->op
= lttv_apply_op_ge_uint
;
400 g_warning("Error encountered in operator assignment");
407 * Entered as string, converted to enum
409 * can only be compared with 'equal' or 'not equal' operators
411 * unsigned int of 16 bits are used here since enums
412 * should not go over 2^16-1 values
416 // case LTTV_FIELD_EQ:
417 // se->op = lttv_apply_op_eq_uint16;
419 // case LTTV_FIELD_NE:
420 // se->op = lttv_apply_op_ne_uint16;
423 // g_warning("Error encountered in operator assignment = or != expected");
430 case LTTV_FILTER_STATE_CT
:
431 case LTTV_FILTER_STATE_IT
:
432 case LTTV_FILTER_EVENT_TIME
:
435 se
->op
= lttv_apply_op_eq_ltttime
;
438 se
->op
= lttv_apply_op_ne_ltttime
;
441 se
->op
= lttv_apply_op_lt_ltttime
;
444 se
->op
= lttv_apply_op_le_ltttime
;
447 se
->op
= lttv_apply_op_gt_ltttime
;
450 se
->op
= lttv_apply_op_ge_ltttime
;
453 g_warning("Error encountered in operator assignment");
458 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
467 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
469 * Assign the value field to the current LttvSimpleExpression
470 * @param se pointer to the current LttvSimpleExpression
471 * @param value string value for simple expression
474 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
477 gboolean is_double
= FALSE
;
478 LttTime t
= ltt_time_zero
;
485 * entered as strings, converted to Quarks
487 case LTTV_FILTER_TRACE_NAME
:
488 case LTTV_FILTER_TRACEFILE_NAME
:
489 case LTTV_FILTER_STATE_P_NAME
:
490 case LTTV_FILTER_STATE_T_BRAND
:
491 case LTTV_FILTER_EVENT_SUBNAME
:
492 case LTTV_FILTER_STATE_EX_MODE
:
493 case LTTV_FILTER_STATE_EX_SUBMODE
:
494 case LTTV_FILTER_STATE_P_STATUS
:
495 // se->value.v_string = value;
496 se
->value
.v_quark
= g_quark_from_string(value
);
502 case LTTV_FILTER_EVENT_NAME
:
505 char *end
= strchr(value
, '.');
509 se
->value
.v_quarks
.q
[0] = g_quark_from_string(value
);
510 se
->value
.v_quarks
.q
[1] = g_quark_from_string(end
);
512 se
->value
.v_quarks
.q
[0] = (GQuark
)0;
513 se
->value
.v_quarks
.q
[1] = g_quark_from_string(value
);
519 * integer -- supposed to be uint64
521 case LTTV_FILTER_EVENT_TSC
:
522 se
->value
.v_uint64
= atoi(value
);
528 case LTTV_FILTER_STATE_PID
:
529 case LTTV_FILTER_STATE_PPID
:
530 case LTTV_FILTER_STATE_CPU
:
531 case LTTV_FILTER_EVENT_TARGET_PID
:
532 se
->value
.v_uint
= atoi(value
);
538 case LTTV_FILTER_STATE_CT
:
539 case LTTV_FILTER_STATE_IT
:
540 case LTTV_FILTER_EVENT_TIME
:
541 //se->value.v_double = atof(value);
543 * parsing logic could be optimised,
544 * but as for now, simpler this way
546 v
= g_string_new("");
547 string_len
= strlen(value
);
548 for(i
=0;i
<string_len
;i
++) {
549 if(value
[i
] == '.') {
550 /* cannot specify number with more than one '.' */
551 if(is_double
) return FALSE
;
552 else is_double
= TRUE
;
553 t
.tv_sec
= atoi(v
->str
);
554 g_string_free(v
,TRUE
);
555 v
= g_string_new("");
556 } else v
= g_string_append_c(v
,value
[i
]);
558 /* number can be integer or double */
559 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
561 t
.tv_sec
= atoi(v
->str
);
565 g_string_free(v
,TRUE
);
567 se
->value
.v_ltttime
= t
;
571 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
581 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
583 * Disallocate memory for the current
585 * @param se pointer to the current LttvSimpleExpression
588 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
590 // g_free(se->value);
591 // switch(se->field) {
592 // case LTTV_FILTER_TRACE_NAME:
593 // case LTTV_FILTER_TRACEFILE_NAME:
594 // case LTTV_FILTER_STATE_P_NAME:
595 // case LTTV_FILTER_EVENT_NAME:
596 // g_free(se->value.v_string);
604 * @fn gint lttv_struct_type(gint)
606 * Finds the structure type depending
607 * on the fields in parameters
608 * @params ft Field of the current structure
609 * @return LttvStructType enum or -1 for error
612 lttv_struct_type(gint ft
) {
615 case LTTV_FILTER_TRACE_NAME
:
616 return LTTV_FILTER_TRACE
;
618 case LTTV_FILTER_TRACEFILE_NAME
:
619 return LTTV_FILTER_TRACEFILE
;
621 case LTTV_FILTER_STATE_PID
:
622 case LTTV_FILTER_STATE_PPID
:
623 case LTTV_FILTER_STATE_CT
:
624 case LTTV_FILTER_STATE_IT
:
625 case LTTV_FILTER_STATE_P_NAME
:
626 case LTTV_FILTER_STATE_T_BRAND
:
627 case LTTV_FILTER_STATE_EX_MODE
:
628 case LTTV_FILTER_STATE_EX_SUBMODE
:
629 case LTTV_FILTER_STATE_P_STATUS
:
630 case LTTV_FILTER_STATE_CPU
:
631 return LTTV_FILTER_STATE
;
633 case LTTV_FILTER_EVENT_NAME
:
634 case LTTV_FILTER_EVENT_SUBNAME
:
635 case LTTV_FILTER_EVENT_CATEGORY
:
636 case LTTV_FILTER_EVENT_TIME
:
637 case LTTV_FILTER_EVENT_TSC
:
638 case LTTV_FILTER_EVENT_TARGET_PID
:
639 case LTTV_FILTER_EVENT_FIELD
:
640 return LTTV_FILTER_EVENT
;
648 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
650 * Applies the 'equal' operator to the
651 * specified structure and value
652 * @param v1 left member of comparison
653 * @param v2 right member of comparison
654 * @return success/failure of operation
656 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
658 guint
* r
= (guint
*) v1
;
659 return (*r
== v2
.v_uint
);
664 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
666 * Applies the 'equal' operator to the
667 * specified structure and value
668 * @param v1 left member of comparison
669 * @param v2 right member of comparison
670 * @return success/failure of operation
672 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
674 guint64
* r
= (guint64
*) v1
;
675 return (*r
== v2
.v_uint64
);
680 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
682 * Applies the 'equal' operator to the
683 * specified structure and value
684 * @param v1 left member of comparison
685 * @param v2 right member of comparison
686 * @return success/failure of operation
688 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
689 guint32
* r
= (guint32
*) v1
;
690 return (*r
== v2
.v_uint32
);
694 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
696 * Applies the 'equal' operator to the
697 * specified structure and value
698 * @param v1 left member of comparison
699 * @param v2 right member of comparison
700 * @return success/failure of operation
702 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
703 guint16
* r
= (guint16
*) v1
;
704 return (*r
== v2
.v_uint16
);
708 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
710 * Applies the 'equal' operator to the
711 * specified structure and value
712 * @param v1 left member of comparison
713 * @param v2 right member of comparison
714 * @return success/failure of operation
716 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
717 double* r
= (double*) v1
;
718 return (*r
== v2
.v_double
);
722 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
724 * Applies the 'equal' operator to the
725 * specified structure and value
726 * @param v1 left member of comparison
727 * @param v2 right member of comparison
728 * @return success/failure of operation
730 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
731 char* r
= (char*) v1
;
732 return (!g_strcasecmp(r
,v2
.v_string
));
736 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
738 * Applies the 'equal' operator to the
739 * specified structure and value
740 * @param v1 left member of comparison
741 * @param v2 right member of comparison
742 * @return success/failure of operation
744 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
745 GQuark
* r
= (GQuark
*) v1
;
746 return (*r
== v2
.v_quark
);
750 * @fn gboolean lttv_apply_op_eq_quarks(gpointer,LttvFieldValue)
752 * Applies the 'equal' operator to the
753 * specified structure and value
754 * @param v1 left member of comparison
755 * @param v2 right member of comparison
756 * @return success/failure of operation
758 gboolean
lttv_apply_op_eq_quarks(const gpointer v1
, LttvFieldValue v2
) {
759 GQuark
*r1
= (GQuark
*) v1
;
761 if (likely(*r1
!= (GQuark
)0) && *r1
!= v2
.v_quarks
.q
[0])
763 if (*r2
!= v2
.v_quarks
.q
[1])
769 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
771 * Applies the 'equal' operator to the
772 * specified structure and value
773 * @param v1 left member of comparison
774 * @param v2 right member of comparison
775 * @return success/failure of operation
777 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
778 LttTime
* r
= (LttTime
*) v1
;
779 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
783 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
785 * Applies the 'not equal' operator to the
786 * specified structure and value
787 * @param v1 left member of comparison
788 * @param v2 right member of comparison
789 * @return success/failure of operation
791 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
792 guint
* r
= (guint
*) v1
;
793 return (*r
!= v2
.v_uint
);
797 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
799 * Applies the 'not equal' operator to the
800 * specified structure and value
801 * @param v1 left member of comparison
802 * @param v2 right member of comparison
803 * @return success/failure of operation
805 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
806 guint64
* r
= (guint64
*) v1
;
807 return (*r
!= v2
.v_uint64
);
811 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
813 * Applies the 'not equal' operator to the
814 * specified structure and value
815 * @param v1 left member of comparison
816 * @param v2 right member of comparison
817 * @return success/failure of operation
819 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
820 guint32
* r
= (guint32
*) v1
;
821 return (*r
!= v2
.v_uint32
);
825 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
827 * Applies the 'not equal' operator to the
828 * specified structure and value
829 * @param v1 left member of comparison
830 * @param v2 right member of comparison
831 * @return success/failure of operation
833 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
834 guint16
* r
= (guint16
*) v1
;
835 return (*r
!= v2
.v_uint16
);
839 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
841 * Applies the 'not equal' operator to the
842 * specified structure and value
843 * @param v1 left member of comparison
844 * @param v2 right member of comparison
845 * @return success/failure of operation
847 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
848 double* r
= (double*) v1
;
849 return (*r
!= v2
.v_double
);
853 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
855 * Applies the 'not equal' operator to the
856 * specified structure and value
857 * @param v1 left member of comparison
858 * @param v2 right member of comparison
859 * @return success/failure of operation
861 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
862 char* r
= (char*) v1
;
863 return (g_strcasecmp(r
,v2
.v_string
));
867 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
869 * Applies the 'not equal' operator to the
870 * specified structure and value
871 * @param v1 left member of comparison
872 * @param v2 right member of comparison
873 * @return success/failure of operation
875 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
876 GQuark
* r
= (GQuark
*) v1
;
877 return (*r
!= v2
.v_quark
);
881 * @fn gboolean lttv_apply_op_ne_quarks(gpointer,LttvFieldValue)
883 * Applies the 'equal' operator to the
884 * specified structure and value
885 * @param v1 left member of comparison
886 * @param v2 right member of comparison
887 * @return success/failure of operation
889 gboolean
lttv_apply_op_ne_quarks(const gpointer v1
, LttvFieldValue v2
) {
890 GQuark
*r1
= (GQuark
*) v1
;
892 if ((*r1
== (GQuark
)0 || *r1
== v2
.v_quarks
.q
[0]) && *r2
== v2
.v_quarks
.q
[1])
899 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
901 * Applies the 'not equal' operator to the
902 * specified structure and value
903 * @param v1 left member of comparison
904 * @param v2 right member of comparison
905 * @return success/failure of operation
907 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
908 LttTime
* r
= (LttTime
*) v1
;
909 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
913 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
915 * Applies the 'lower than' operator to the
916 * specified structure and value
917 * @param v1 left member of comparison
918 * @param v2 right member of comparison
919 * @return success/failure of operation
921 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
922 guint
* r
= (guint
*) v1
;
923 return (*r
< v2
.v_uint
);
927 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
929 * Applies the 'lower than' operator to the
930 * specified structure and value
931 * @param v1 left member of comparison
932 * @param v2 right member of comparison
933 * @return success/failure of operation
935 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
936 guint64
* r
= (guint64
*) v1
;
937 return (*r
< v2
.v_uint64
);
941 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
943 * Applies the 'lower than' operator to the
944 * specified structure and value
945 * @param v1 left member of comparison
946 * @param v2 right member of comparison
947 * @return success/failure of operation
949 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
950 guint32
* r
= (guint32
*) v1
;
951 return (*r
< v2
.v_uint32
);
955 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
957 * Applies the 'lower than' operator to the
958 * specified structure and value
959 * @param v1 left member of comparison
960 * @param v2 right member of comparison
961 * @return success/failure of operation
963 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
964 guint16
* r
= (guint16
*) v1
;
965 return (*r
< v2
.v_uint16
);
969 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
971 * Applies the 'lower than' operator to the
972 * specified structure and value
973 * @param v1 left member of comparison
974 * @param v2 right member of comparison
975 * @return success/failure of operation
977 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
978 double* r
= (double*) v1
;
979 return (*r
< v2
.v_double
);
983 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
985 * Applies the 'lower than' operator to the
986 * specified structure and value
987 * @param v1 left member of comparison
988 * @param v2 right member of comparison
989 * @return success/failure of operation
991 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
992 LttTime
* r
= (LttTime
*) v1
;
993 // 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)));
994 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
998 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
1000 * Applies the 'lower or equal' operator to the
1001 * specified structure and value
1002 * @param v1 left member of comparison
1003 * @param v2 right member of comparison
1004 * @return success/failure of operation
1006 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
1007 guint
* r
= (guint
*) v1
;
1008 return (*r
<= v2
.v_uint
);
1012 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
1014 * Applies the 'lower or equal' operator to the
1015 * specified structure and value
1016 * @param v1 left member of comparison
1017 * @param v2 right member of comparison
1018 * @return success/failure of operation
1020 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
1021 guint64
* r
= (guint64
*) v1
;
1022 return (*r
<= v2
.v_uint64
);
1026 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
1028 * Applies the 'lower or equal' operator to the
1029 * specified structure and value
1030 * @param v1 left member of comparison
1031 * @param v2 right member of comparison
1032 * @return success/failure of operation
1034 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
1035 guint32
* r
= (guint32
*) v1
;
1036 return (*r
<= v2
.v_uint32
);
1040 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
1042 * Applies the 'lower or equal' operator to the
1043 * specified structure and value
1044 * @param v1 left member of comparison
1045 * @param v2 right member of comparison
1046 * @return success/failure of operation
1048 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
1049 guint16
* r
= (guint16
*) v1
;
1050 return (*r
<= v2
.v_uint16
);
1054 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
1056 * Applies the 'lower or equal' operator to the
1057 * specified structure and value
1058 * @param v1 left member of comparison
1059 * @param v2 right member of comparison
1060 * @return success/failure of operation
1062 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
1063 double* r
= (double*) v1
;
1064 return (*r
<= v2
.v_double
);
1068 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
1070 * Applies the 'lower or equal' operator to the
1071 * specified structure and value
1072 * @param v1 left member of comparison
1073 * @param v2 right member of comparison
1074 * @return success/failure of operation
1076 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1077 LttTime
* r
= (LttTime
*) v1
;
1078 // 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)));
1079 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1084 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1086 * Applies the 'greater than' operator to the
1087 * specified structure and value
1088 * @param v1 left member of comparison
1089 * @param v2 right member of comparison
1090 * @return success/failure of operation
1092 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1093 guint
* r
= (guint
*) v1
;
1094 return (*r
> v2
.v_uint
);
1098 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1100 * Applies the 'greater than' operator to the
1101 * specified structure and value
1102 * @param v1 left member of comparison
1103 * @param v2 right member of comparison
1104 * @return success/failure of operation
1106 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1107 guint64
* r
= (guint64
*) v1
;
1108 return (*r
> v2
.v_uint64
);
1112 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1114 * Applies the 'greater than' operator to the
1115 * specified structure and value
1116 * @param v1 left member of comparison
1117 * @param v2 right member of comparison
1118 * @return success/failure of operation
1120 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1121 guint32
* r
= (guint32
*) v1
;
1122 return (*r
> v2
.v_uint32
);
1126 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1128 * Applies the 'greater than' operator to the
1129 * specified structure and value
1130 * @param v1 left member of comparison
1131 * @param v2 right member of comparison
1132 * @return success/failure of operation
1134 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1135 guint16
* r
= (guint16
*) v1
;
1136 return (*r
> v2
.v_uint16
);
1140 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1142 * Applies the 'greater than' operator to the
1143 * specified structure and value
1144 * @param v1 left member of comparison
1145 * @param v2 right member of comparison
1146 * @return success/failure of operation
1148 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1149 double* r
= (double*) v1
;
1150 return (*r
> v2
.v_double
);
1154 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1156 * Applies the 'greater than' operator to the
1157 * specified structure and value
1158 * @param v1 left member of comparison
1159 * @param v2 right member of comparison
1160 * @return success/failure of operation
1162 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1163 LttTime
* r
= (LttTime
*) v1
;
1164 // 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)));
1165 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1169 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1171 * Applies the 'greater or equal' operator to the
1172 * specified structure and value
1173 * @param v1 left member of comparison
1174 * @param v2 right member of comparison
1175 * @return success/failure of operation
1177 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1178 guint
* r
= (guint
*) v1
;
1179 return (*r
>= v2
.v_uint
);
1183 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1185 * Applies the 'greater or equal' operator to the
1186 * specified structure and value
1187 * @param v1 left member of comparison
1188 * @param v2 right member of comparison
1189 * @return success/failure of operation
1191 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1192 guint64
* r
= (guint64
*) v1
;
1193 return (*r
>= v2
.v_uint64
);
1197 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1199 * Applies the 'greater or equal' operator to the
1200 * specified structure and value
1201 * @param v1 left member of comparison
1202 * @param v2 right member of comparison
1203 * @return success/failure of operation
1205 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1206 guint32
* r
= (guint32
*) v1
;
1207 return (*r
>= v2
.v_uint32
);
1211 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1213 * Applies the 'greater or equal' operator to the
1214 * specified structure and value
1215 * @param v1 left member of comparison
1216 * @param v2 right member of comparison
1217 * @return success/failure of operation
1219 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1220 guint16
* r
= (guint16
*) v1
;
1221 return (*r
>= v2
.v_uint16
);
1225 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1227 * Applies the 'greater or equal' operator to the
1228 * specified structure and value
1229 * @param v1 left member of comparison
1230 * @param v2 right member of comparison
1231 * @return success/failure of operation
1233 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1234 double* r
= (double*) v1
;
1235 return (*r
>= v2
.v_double
);
1239 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1241 * Applies the 'greater or equal' operator to the
1242 * specified structure and value
1243 * @param v1 left member of comparison
1244 * @param v2 right member of comparison
1245 * @return success/failure of operation
1247 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1248 LttTime
* r
= (LttTime
*) v1
;
1249 // 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)));
1250 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1256 * Makes a copy of the current filter tree
1257 * @param tree pointer to the current tree
1258 * @return new copy of the filter tree
1261 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1263 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1265 newtree
->node
= tree
->node
;
1267 newtree
->left
= tree
->left
;
1268 if(newtree
->left
== LTTV_TREE_NODE
) {
1269 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1270 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1271 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1272 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1273 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1274 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1275 /* FIXME: special case for string copy ! */
1276 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1279 newtree
->right
= tree
->right
;
1280 if(newtree
->right
== LTTV_TREE_NODE
) {
1281 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1282 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1283 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1284 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1285 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1286 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1287 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1295 * Makes a copy of the current filter
1296 * @param filter pointer to the current filter
1297 * @return new copy of the filter
1300 lttv_filter_clone(const LttvFilter
* filter
) {
1302 if(!filter
) return NULL
;
1304 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1306 strcpy(newfilter
->expression
,filter
->expression
);
1308 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1316 * @fn LttvFilter* lttv_filter_new()
1318 * Creates a new LttvFilter
1319 * @return the current LttvFilter or NULL if error
1324 LttvFilter
* filter
= g_new(LttvFilter
,1);
1325 filter
->expression
= NULL
;
1326 filter
->head
= NULL
;
1333 * @fn gboolean lttv_filter_update(LttvFilter*)
1335 * Updates the current LttvFilter by building
1336 * its tree based upon the expression string
1337 * @param filter pointer to the current LttvFilter
1338 * @return Failure/Success of operation
1341 lttv_filter_update(LttvFilter
* filter
) {
1343 // g_print("filter::lttv_filter_new()\n"); /* debug */
1345 if(filter
->expression
== NULL
) return FALSE
;
1349 p_nesting
=0, /* parenthesis nesting value */
1351 guint expression_len
;
1355 *tree
= lttv_filter_tree_new(), /* main tree */
1356 *subtree
= NULL
, /* buffer for subtrees */
1357 *t1
, /* buffer #1 */
1358 *t2
, /* buffer #2 */
1359 *t3
; /* buffer #3 */
1363 * If the tree already exists,
1364 * destroy it and build a new one
1366 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1367 filter
->head
= NULL
; /* will be assigned at the end */
1371 * each element of the list
1372 * is a sub tree created
1373 * by the use of parenthesis in the
1374 * global expression. The final tree
1375 * will be the one left at the root of
1378 GPtrArray
*tree_stack
= g_ptr_array_new();
1379 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1381 /* temporary values */
1382 GString
*a_field_component
= g_string_new("");
1383 GString
*a_string_spaces
= g_string_new("");
1384 GPtrArray
*a_field_path
= g_ptr_array_new();
1386 /* simple expression buffer */
1387 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1389 gint nest_quotes
= 0;
1392 * Parse entire expression and construct
1393 * the binary tree. There are two steps
1394 * in browsing that string
1395 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1396 * 2. finding simple expressions
1397 * - field path ( separated by dots )
1398 * - op ( >, <, =, >=, <=, !=)
1399 * - value ( integer, string ... )
1400 * To spare computing time, the whole
1401 * string is parsed in this loop for a
1402 * O(n) complexity order.
1404 * When encountering logical op &,|,^
1405 * 1. parse the last value if any
1406 * 2. create a new tree
1407 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1408 * 4. concatenate this tree with the current tree on top of the stack
1409 * When encountering math ops >,>=,<,<=,=,!=
1410 * 1. add to op to the simple expression
1411 * 2. concatenate last field component to field path
1412 * When encountering concatening ops .
1413 * 1. concatenate last field component to field path
1414 * When encountering opening parenthesis (,{,[
1415 * 1. create a new subtree on top of tree stack
1416 * When encountering closing parenthesis ),},]
1417 * 1. add the expression on right child of the current tree
1418 * 2. the subtree is completed, allocate a new subtree
1419 * 3. pop the tree value from the tree stack
1423 struct timeval starttime
;
1424 struct timeval endtime
;
1425 gettimeofday(&starttime
, NULL
);
1428 expression_len
= strlen(filter
->expression
);
1429 for(i
=0;i
<expression_len
;i
++) {
1431 // g_print("%c\n ",filter->expression[i]);
1433 switch(filter
->expression
[i
]) {
1435 if(filter
->expression
[i
+1] == '\"') {
1444 if(a_string_spaces
->len
!= 0) {
1445 a_field_component
= g_string_append(
1446 a_field_component
, a_string_spaces
->str
);
1447 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1449 a_field_component
= g_string_append_c(a_field_component
,
1450 filter
->expression
[i
]);
1454 switch(filter
->expression
[i
]) {
1460 /* get current tree in tree stack */
1461 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1463 /* get current node at absolute right */
1464 while(t1
->right
!= LTTV_TREE_IDLE
) {
1465 g_assert(t1
->right
== LTTV_TREE_NODE
);
1468 t2
= lttv_filter_tree_new();
1469 t2
->node
= LTTV_LOGICAL_AND
;
1470 t1
->right
= LTTV_TREE_NODE
;
1472 if(not) { /* add not operator to tree */
1473 t3
= lttv_filter_tree_new();
1474 t3
->node
= LTTV_LOGICAL_NOT
;
1475 t2
->left
= LTTV_TREE_NODE
;
1480 if(subtree
!= NULL
) { /* append subtree to current tree */
1481 t2
->left
= LTTV_TREE_NODE
;
1482 t2
->l_child
.t
= subtree
;
1484 } else { /* append a simple expression */
1485 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1486 a_field_component
= g_string_new("");
1487 g_string_free(a_string_spaces
, TRUE
);
1488 a_string_spaces
= g_string_new("");
1489 t2
->left
= LTTV_TREE_LEAF
;
1490 t2
->l_child
.leaf
= a_simple_expression
;
1491 a_simple_expression
= lttv_simple_expression_new();
1497 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1498 while(t1
->right
!= LTTV_TREE_IDLE
) {
1499 g_assert(t1
->right
== LTTV_TREE_NODE
);
1502 t2
= lttv_filter_tree_new();
1503 t2
->node
= LTTV_LOGICAL_OR
;
1504 t1
->right
= LTTV_TREE_NODE
;
1506 if(not) { // add not operator to tree
1507 t3
= lttv_filter_tree_new();
1508 t3
->node
= LTTV_LOGICAL_NOT
;
1509 t2
->left
= LTTV_TREE_NODE
;
1514 if(subtree
!= NULL
) { /* append subtree to current tree */
1515 t2
->left
= LTTV_TREE_NODE
;
1516 t2
->l_child
.t
= subtree
;
1518 } else { /* append a simple expression */
1519 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1520 a_field_component
= g_string_new("");
1521 g_string_free(a_string_spaces
, TRUE
);
1522 a_string_spaces
= g_string_new("");
1523 t2
->left
= LTTV_TREE_LEAF
;
1524 t2
->l_child
.leaf
= a_simple_expression
;
1525 a_simple_expression
= lttv_simple_expression_new();
1531 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1532 while(t1
->right
!= LTTV_TREE_IDLE
) {
1533 g_assert(t1
->right
== LTTV_TREE_NODE
);
1536 t2
= lttv_filter_tree_new();
1537 t2
->node
= LTTV_LOGICAL_XOR
;
1538 t1
->right
= LTTV_TREE_NODE
;
1540 if(not) { // add not operator to tree
1541 t3
= lttv_filter_tree_new();
1542 t3
->node
= LTTV_LOGICAL_NOT
;
1543 t2
->left
= LTTV_TREE_NODE
;
1548 if(subtree
!= NULL
) { /* append subtree to current tree */
1549 t2
->left
= LTTV_TREE_NODE
;
1550 t2
->l_child
.t
= subtree
;
1552 } else { /* append a simple expression */
1553 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1554 a_field_component
= g_string_new("");
1555 g_string_free(a_string_spaces
, TRUE
);
1556 a_string_spaces
= g_string_new("");
1557 t2
->left
= LTTV_TREE_LEAF
;
1558 t2
->l_child
.leaf
= a_simple_expression
;
1559 a_simple_expression
= lttv_simple_expression_new();
1563 case '!': /* not, or not equal (math op) */
1565 if(filter
->expression
[i
+1] == '=') { /* != */
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 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1578 case '(': /* start of parenthesis */
1582 p_nesting
++; /* incrementing parenthesis nesting value */
1583 t1
= lttv_filter_tree_new();
1584 if(not) { /* add not operator to tree */
1585 t3
= lttv_filter_tree_new();
1586 t3
->node
= LTTV_LOGICAL_NOT
;
1587 t1
->right
= LTTV_TREE_NODE
;
1591 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1594 case ')': /* end of parenthesis */
1598 p_nesting
--; /* decrementing parenthesis nesting value */
1599 if(p_nesting
<0 || tree_stack
->len
<2) {
1600 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1601 is not valid due to parenthesis incorrect use",filter
->expression
);
1605 /* there must at least be the root tree left in the array */
1606 g_assert(tree_stack
->len
>0);
1608 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1609 while(t1
->right
!= LTTV_TREE_IDLE
) {
1612 if(not) { // add not operator to tree
1614 t3
= lttv_filter_tree_new();
1615 t3
->node
= LTTV_LOGICAL_NOT
;
1616 t1
->right
= LTTV_TREE_NODE
;
1621 if(subtree
!= NULL
) { /* append subtree to current tree */
1622 t1
->right
= LTTV_TREE_NODE
;
1623 t1
->r_child
.t
= subtree
;
1624 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1625 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1626 } else { /* assign subtree as current tree */
1627 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1628 a_field_component
= g_string_new("");
1629 g_string_free(a_string_spaces
, TRUE
);
1630 a_string_spaces
= g_string_new("");
1631 t1
->right
= LTTV_TREE_LEAF
;
1632 t1
->r_child
.leaf
= a_simple_expression
;
1633 a_simple_expression
= lttv_simple_expression_new();
1634 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1639 * mathematic operators
1641 case '<': /* lower, lower or equal */
1643 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1644 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1645 a_field_component
= g_string_new("");
1646 g_string_free(a_string_spaces
, TRUE
);
1647 a_string_spaces
= g_string_new("");
1648 if(filter
->expression
[i
+1] == '=') { /* <= */
1650 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1651 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1654 case '>': /* higher, higher or equal */
1656 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1657 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1658 a_field_component
= g_string_new("");
1659 g_string_free(a_string_spaces
, TRUE
);
1660 a_string_spaces
= g_string_new("");
1661 if(filter
->expression
[i
+1] == '=') { /* >= */
1663 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1664 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1667 case '=': /* equal */
1669 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1670 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1671 a_field_component
= g_string_new("");
1672 g_string_free(a_string_spaces
, TRUE
);
1673 a_string_spaces
= g_string_new("");
1674 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1678 * Field concatening caracter
1683 * divide field expression into elements
1684 * in a_field_path array.
1686 * A dot can also be present in double values
1688 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1689 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1690 a_field_component
= g_string_new("");
1691 g_string_free(a_string_spaces
, TRUE
);
1692 a_string_spaces
= g_string_new("");
1694 /* Operator found, we are in the value field */
1695 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1698 case ' ': /* keep spaces that are within a field component */
1699 if(a_field_component
->len
== 0) break; /* ignore */
1701 a_string_spaces
= g_string_append_c(a_string_spaces
,
1702 filter
->expression
[i
]);
1704 case '\n': /* ignore */
1707 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1709 default: /* concatening current string */
1710 if(a_string_spaces
->len
!= 0) {
1711 a_field_component
= g_string_append(
1712 a_field_component
, a_string_spaces
->str
);
1713 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1715 a_field_component
= g_string_append_c(a_field_component
,
1716 filter
->expression
[i
]);
1721 * Preliminary check to see
1722 * if tree was constructed correctly
1725 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1726 is not valid due to parenthesis incorrect use",filter
->expression
);
1730 if(tree_stack
->len
!= 1) /* only root tree should remain */
1734 * processing last element of expression
1736 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1737 while(t1
->right
!= LTTV_TREE_IDLE
) {
1738 g_assert(t1
->right
== LTTV_TREE_NODE
);
1741 if(not) { // add not operator to tree
1742 t3
= lttv_filter_tree_new();
1743 t3
->node
= LTTV_LOGICAL_NOT
;
1744 t1
->right
= LTTV_TREE_NODE
;
1749 if(subtree
!= NULL
) { /* add the subtree */
1750 t1
->right
= LTTV_TREE_NODE
;
1751 t1
->r_child
.t
= subtree
;
1753 } else { /* add a leaf */
1754 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1755 a_field_component
= NULL
;
1756 g_string_free(a_string_spaces
, TRUE
);
1757 a_string_spaces
= NULL
;
1758 t1
->right
= LTTV_TREE_LEAF
;
1759 t1
->r_child
.leaf
= a_simple_expression
;
1760 a_simple_expression
= NULL
;
1764 /* free the pointer array */
1765 g_assert(a_field_path
->len
== 0);
1766 g_ptr_array_free(a_field_path
,TRUE
);
1768 /* free the tree stack -- but keep the root tree */
1769 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1770 g_ptr_array_free(tree_stack
,TRUE
);
1772 /* free the field buffer if allocated */
1773 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1774 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1776 /* free the simple expression buffer if allocated */
1777 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1779 g_assert(filter
->head
!= NULL
); /* tree should exist */
1780 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1783 gettimeofday(&endtime
, NULL
);
1785 /* Calcul du temps de l'algorithme */
1786 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1787 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1788 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1789 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1793 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1794 lttv_print_tree(filter
->head
,0) ;
1795 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1803 * @fn void lttv_filter_destroy(LttvFilter*)
1805 * Destroy the current LttvFilter
1806 * @param filter pointer to the current LttvFilter
1809 lttv_filter_destroy(LttvFilter
* filter
) {
1813 if(filter
->expression
)
1814 g_free(filter
->expression
);
1816 lttv_filter_tree_destroy(filter
->head
);
1822 * @fn LttvFilterTree* lttv_filter_tree_new()
1824 * Assign a new tree for the current expression
1826 * @return pointer of LttvFilterTree
1829 lttv_filter_tree_new() {
1830 LttvFilterTree
* tree
;
1832 tree
= g_new(LttvFilterTree
,1);
1833 tree
->node
= 0; //g_new(lttv_expression,1);
1834 tree
->left
= LTTV_TREE_IDLE
;
1835 tree
->right
= LTTV_TREE_IDLE
;
1836 tree
->r_child
.t
= NULL
;
1837 tree
->l_child
.t
= NULL
;
1843 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1845 * Append a new expression to the expression
1846 * defined in the current filter
1847 * @param filter pointer to the current LttvFilter
1848 * @param expression string that must be appended
1849 * @return Success/Failure of operation
1852 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1854 if(expression
== NULL
) return FALSE
;
1855 if(filter
== NULL
) return FALSE
;
1856 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1858 GString
* s
= g_string_new("");
1859 if(filter
->expression
!= NULL
) {
1860 s
= g_string_append(s
,filter
->expression
);
1861 s
= g_string_append_c(s
,'&');
1863 s
= g_string_append(s
,expression
);
1865 g_free(filter
->expression
);
1866 filter
->expression
= g_string_free(s
,FALSE
);
1868 /* TRUE if construction of tree proceeded without errors */
1869 return lttv_filter_update(filter
);
1874 * @fn void lttv_filter_clear_expression(LttvFilter*)
1876 * Clear the filter expression from the
1877 * current filter and sets its pointer to NULL
1878 * @param filter pointer to the current LttvFilter
1881 lttv_filter_clear_expression(LttvFilter
* filter
) {
1883 if(filter
->expression
!= NULL
) {
1884 g_free(filter
->expression
);
1885 filter
->expression
= NULL
;
1891 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1893 * Destroys the tree and his sub-trees
1894 * @param tree Tree which must be destroyed
1897 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1899 if(tree
== NULL
) return;
1901 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1902 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1904 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1905 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1907 // g_free(tree->node);
1912 * Global parsing function for the current
1914 * @param t pointer to the current LttvFilterTree
1915 * @param event current LttEvent, NULL if not used
1916 * @param tracefile current LttTracefile, NULL if not used
1917 * @param trace current LttTrace, NULL if not used
1918 * @param state current LttvProcessState, NULL if not used
1919 * @param context current LttvTracefileContext, NULL if not used
1920 * @return response of filter
1923 lttv_filter_tree_parse(
1924 const LttvFilterTree
* t
,
1925 const LttEvent
* event
,
1926 const LttTracefile
* tracefile
,
1927 const LttTrace
* trace
,
1928 const LttvTracefileContext
* context
,
1929 const LttvProcessState
* state
,
1930 const LttvTraceContext
* tc
1935 * Each tree is parsed in inorder.
1936 * This way, it's possible to apply the left filter of the
1937 * tree, then decide whether or not the right branch should
1938 * be parsed depending on the linking logical operator
1940 * Each node consists in a
1941 * 1. logical operator
1942 * 2. left child ( node or simple expression )
1943 * 3. right child ( node or simple expression )
1945 * When the child is a simple expression, we must
1946 * before all determine if the expression refers to
1947 * a structure which is whithin observation ( not NULL ).
1948 * -If so, the expression is evaluated.
1949 * -If not, the result is set to TRUE since this particular
1950 * operation does not interfere with the lttv structure
1952 * The result of each simple expression will directly
1953 * affect the next branch. This way, depending on
1954 * the linking logical operator, the parser will decide
1955 * to explore or not the next branch.
1957 * -If result of left branch is 0 / FALSE
1958 * then don't explore right branch and return 0;
1959 * -If result of left branch is 1 / TRUE then explore
1961 * -If result of left branch is 1 / TRUE
1962 * then don't explore right branch and return 1;
1963 * -If result of left branch is 0 / FALSE then explore
1965 * -Result of left branch will not affect exploration of
1969 gboolean lresult
= FALSE
, rresult
= FALSE
;
1971 LttvTraceState
*ts
= NULL
;
1972 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1974 ts
= (LttvTraceState
*)tc
;
1976 ts
= (LttvTraceState
*)context
->t_context
;
1979 guint cpu
= tfs
->cpu
;
1981 state
= ts
->running_process
[cpu
];
1987 if(t
->left
== LTTV_TREE_NODE
) {
1988 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1990 else if(t
->left
== LTTV_TREE_LEAF
) {
1991 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1995 * Parse linking operator
1996 * make a cutoff if possible
1998 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1999 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
2002 * Parse right branch
2004 if(t
->right
== LTTV_TREE_NODE
) {
2005 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
2007 else if(t
->right
== LTTV_TREE_LEAF
) {
2008 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
2013 * Apply and return the
2014 * logical link between the
2018 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
2019 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
2020 case LTTV_LOGICAL_NOT
:
2021 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
2022 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
2023 case 0: return (rresult
);
2026 * This case should never be
2027 * parsed, if so, this subtree
2036 * This function parses a particular branch of the tree
2037 * @param se pointer to the current LttvSimpleExpression
2038 * @param event current LttEvent, NULL if not used
2039 * @param tracefile current LttTracefile, NULL if not used
2040 * @param trace current LttTrace, NULL if not used
2041 * @param state current LttvProcessState, NULL if not used
2042 * @param context current LttvTracefileContext, NULL if not used
2043 * @return response of filter
2046 lttv_filter_tree_parse_branch(
2047 const LttvSimpleExpression
* se
,
2048 const LttEvent
* event
,
2049 const LttTracefile
* tracefile
,
2050 const LttTrace
* trace
,
2051 const LttvProcessState
* state
,
2052 const LttvTracefileContext
* context
) {
2057 case LTTV_FILTER_TRACE_NAME
:
2058 if(trace
== NULL
) return TRUE
;
2060 GQuark quark
= ltt_trace_name(trace
);
2061 return se
->op((gpointer
)&quark
,v
);
2064 case LTTV_FILTER_TRACEFILE_NAME
:
2065 if(tracefile
== NULL
) return TRUE
;
2067 GQuark quark
= ltt_tracefile_name(tracefile
);
2068 return se
->op((gpointer
)&quark
,v
);
2071 case LTTV_FILTER_STATE_PID
:
2072 if(state
== NULL
) return TRUE
;
2073 else return se
->op((gpointer
)&state
->pid
,v
);
2075 case LTTV_FILTER_STATE_PPID
:
2076 if(state
== NULL
) return TRUE
;
2077 else return se
->op((gpointer
)&state
->ppid
,v
);
2079 case LTTV_FILTER_STATE_CT
:
2080 if(state
== NULL
) return TRUE
;
2082 return se
->op((gpointer
)&state
->creation_time
,v
);
2085 case LTTV_FILTER_STATE_IT
:
2086 if(state
== NULL
) return TRUE
;
2088 return se
->op((gpointer
)&state
->insertion_time
,v
);
2091 case LTTV_FILTER_STATE_P_NAME
:
2092 if(state
== NULL
) return TRUE
;
2094 GQuark quark
= state
->name
;
2095 return se
->op((gpointer
)&quark
,v
);
2098 case LTTV_FILTER_STATE_T_BRAND
:
2099 if(state
== NULL
) return TRUE
;
2101 GQuark quark
= state
->brand
;
2102 return se
->op((gpointer
)&quark
,v
);
2105 case LTTV_FILTER_STATE_EX_MODE
:
2106 if(state
== NULL
) return TRUE
;
2107 else return se
->op((gpointer
)&state
->state
->t
,v
);
2109 case LTTV_FILTER_STATE_EX_SUBMODE
:
2110 if(state
== NULL
) return TRUE
;
2111 else return se
->op((gpointer
)&state
->state
->n
,v
);
2113 case LTTV_FILTER_STATE_P_STATUS
:
2114 if(state
== NULL
) return TRUE
;
2115 else return se
->op((gpointer
)&state
->state
->s
,v
);
2117 case LTTV_FILTER_STATE_CPU
:
2118 if(state
== NULL
) return TRUE
;
2120 return se
->op((gpointer
)&state
->cpu
,v
);
2123 case LTTV_FILTER_EVENT_NAME
:
2124 if(event
== NULL
) return TRUE
;
2126 struct marker_info
*info
;
2128 LttTracefile
*tf
= context
->tf
;
2129 qtuple
[0] = ltt_tracefile_name(tracefile
);
2130 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2131 g_assert(info
!= NULL
);
2132 qtuple
[1] = info
->name
;
2133 return se
->op((gpointer
)qtuple
,v
);
2136 case LTTV_FILTER_EVENT_SUBNAME
:
2137 if(event
== NULL
) return TRUE
;
2139 struct marker_info
*info
;
2140 LttTracefile
*tf
= context
->tf
;
2141 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2142 g_assert(info
!= NULL
);
2143 GQuark quark
= info
->name
;
2144 return se
->op((gpointer
)&quark
,v
);
2147 case LTTV_FILTER_EVENT_CATEGORY
:
2149 * TODO: Not yet implemented
2153 case LTTV_FILTER_EVENT_TIME
:
2154 if(event
== NULL
) return TRUE
;
2156 LttTime time
= ltt_event_time(event
);
2157 return se
->op((gpointer
)&time
,v
);
2160 case LTTV_FILTER_EVENT_TSC
:
2161 if(event
== NULL
) return TRUE
;
2163 LttCycleCount count
= ltt_event_cycle_count(event
);
2164 return se
->op((gpointer
)&count
,v
);
2167 case LTTV_FILTER_EVENT_TARGET_PID
:
2168 if(context
== NULL
) return TRUE
;
2171 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2172 return se
->op((gpointer
)&target_pid
,v
);
2175 case LTTV_FILTER_EVENT_FIELD
:
2177 * TODO: Use the offset to
2178 * find the dynamic field
2179 * in the event struct
2184 * This case should never be
2185 * parsed, if so, the whole
2186 * filtering is cancelled
2188 g_warning("Error while parsing the filter tree");
2192 /* should never get here */
2200 * Debug function. Prints tree memory allocation.
2201 * @param t the pointer to the current LttvFilterTree
2204 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2206 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2207 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2208 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2210 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2211 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"));
2212 if(t
->left
== LTTV_TREE_LEAF
) {
2213 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2214 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2215 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2217 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"));
2218 if(t
->right
== LTTV_TREE_LEAF
) {
2219 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2220 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2221 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2224 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2225 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2229 * @fn static void module_init()
2231 * Initializes the filter module and specific values
2233 static void module_init()
2239 * Destroys the filter module and specific values
2241 static void module_destroy()
2247 LTTV_MODULE("filter", "Filters traceset and events", \
2248 "Filters traceset and events specifically to user input", \
2249 module_init
, module_destroy
)