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");
349 case LTTV_FILTER_EVENT_TSC
:
352 se
->op
= lttv_apply_op_eq_uint64
;
355 se
->op
= lttv_apply_op_ne_uint64
;
358 se
->op
= lttv_apply_op_lt_uint64
;
361 se
->op
= lttv_apply_op_le_uint64
;
364 se
->op
= lttv_apply_op_gt_uint64
;
367 se
->op
= lttv_apply_op_ge_uint64
;
370 g_warning("Error encountered in operator assignment");
377 case LTTV_FILTER_STATE_CPU
:
378 case LTTV_FILTER_STATE_PID
:
379 case LTTV_FILTER_STATE_PPID
:
380 case LTTV_FILTER_EVENT_TARGET_PID
:
383 se
->op
= lttv_apply_op_eq_uint
;
386 se
->op
= lttv_apply_op_ne_uint
;
389 se
->op
= lttv_apply_op_lt_uint
;
392 se
->op
= lttv_apply_op_le_uint
;
395 se
->op
= lttv_apply_op_gt_uint
;
398 se
->op
= lttv_apply_op_ge_uint
;
401 g_warning("Error encountered in operator assignment");
408 * Entered as string, converted to enum
410 * can only be compared with 'equal' or 'not equal' operators
412 * unsigned int of 16 bits are used here since enums
413 * should not go over 2^16-1 values
417 // case LTTV_FIELD_EQ:
418 // se->op = lttv_apply_op_eq_uint16;
420 // case LTTV_FIELD_NE:
421 // se->op = lttv_apply_op_ne_uint16;
424 // g_warning("Error encountered in operator assignment = or != expected");
431 case LTTV_FILTER_STATE_CT
:
432 case LTTV_FILTER_STATE_IT
:
433 case LTTV_FILTER_EVENT_TIME
:
436 se
->op
= lttv_apply_op_eq_ltttime
;
439 se
->op
= lttv_apply_op_ne_ltttime
;
442 se
->op
= lttv_apply_op_lt_ltttime
;
445 se
->op
= lttv_apply_op_le_ltttime
;
448 se
->op
= lttv_apply_op_gt_ltttime
;
451 se
->op
= lttv_apply_op_ge_ltttime
;
454 g_warning("Error encountered in operator assignment");
459 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
468 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
470 * Assign the value field to the current LttvSimpleExpression
471 * @param se pointer to the current LttvSimpleExpression
472 * @param value string value for simple expression
475 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
478 gboolean is_double
= FALSE
;
479 LttTime t
= ltt_time_zero
;
486 * entered as strings, converted to Quarks
488 case LTTV_FILTER_TRACE_NAME
:
489 case LTTV_FILTER_TRACEFILE_NAME
:
490 case LTTV_FILTER_STATE_P_NAME
:
491 case LTTV_FILTER_STATE_T_BRAND
:
492 case LTTV_FILTER_EVENT_SUBNAME
:
493 case LTTV_FILTER_STATE_EX_MODE
:
494 case LTTV_FILTER_STATE_EX_SUBMODE
:
495 case LTTV_FILTER_STATE_P_STATUS
:
496 // se->value.v_string = value;
497 se
->value
.v_quark
= g_quark_from_string(value
);
503 case LTTV_FILTER_EVENT_NAME
:
506 char *end
= strchr(value
, '.');
510 se
->value
.v_quarks
.q
[0] = g_quark_from_string(value
);
511 se
->value
.v_quarks
.q
[1] = g_quark_from_string(end
);
513 se
->value
.v_quarks
.q
[0] = (GQuark
)0;
514 se
->value
.v_quarks
.q
[1] = g_quark_from_string(value
);
520 * integer -- supposed to be uint64
522 case LTTV_FILTER_EVENT_TSC
:
523 se
->value
.v_uint64
= atoi(value
);
529 case LTTV_FILTER_STATE_PID
:
530 case LTTV_FILTER_STATE_PPID
:
531 case LTTV_FILTER_STATE_CPU
:
532 case LTTV_FILTER_EVENT_TARGET_PID
:
533 se
->value
.v_uint
= atoi(value
);
539 case LTTV_FILTER_STATE_CT
:
540 case LTTV_FILTER_STATE_IT
:
541 case LTTV_FILTER_EVENT_TIME
:
542 //se->value.v_double = atof(value);
544 * parsing logic could be optimised,
545 * but as for now, simpler this way
547 v
= g_string_new("");
548 string_len
= strlen(value
);
549 for(i
=0;i
<string_len
;i
++) {
550 if(value
[i
] == '.') {
551 /* cannot specify number with more than one '.' */
552 if(is_double
) return FALSE
;
553 else is_double
= TRUE
;
554 t
.tv_sec
= atoi(v
->str
);
555 g_string_free(v
,TRUE
);
556 v
= g_string_new("");
557 } else v
= g_string_append_c(v
,value
[i
]);
559 /* number can be integer or double */
560 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
562 t
.tv_sec
= atoi(v
->str
);
566 g_string_free(v
,TRUE
);
568 se
->value
.v_ltttime
= t
;
572 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
582 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
584 * Disallocate memory for the current
586 * @param se pointer to the current LttvSimpleExpression
589 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
591 // g_free(se->value);
592 // switch(se->field) {
593 // case LTTV_FILTER_TRACE_NAME:
594 // case LTTV_FILTER_TRACEFILE_NAME:
595 // case LTTV_FILTER_STATE_P_NAME:
596 // case LTTV_FILTER_EVENT_NAME:
597 // g_free(se->value.v_string);
605 * @fn gint lttv_struct_type(gint)
607 * Finds the structure type depending
608 * on the fields in parameters
609 * @params ft Field of the current structure
610 * @return LttvStructType enum or -1 for error
613 lttv_struct_type(gint ft
) {
616 case LTTV_FILTER_TRACE_NAME
:
617 return LTTV_FILTER_TRACE
;
619 case LTTV_FILTER_TRACEFILE_NAME
:
620 return LTTV_FILTER_TRACEFILE
;
622 case LTTV_FILTER_STATE_PID
:
623 case LTTV_FILTER_STATE_PPID
:
624 case LTTV_FILTER_STATE_CT
:
625 case LTTV_FILTER_STATE_IT
:
626 case LTTV_FILTER_STATE_P_NAME
:
627 case LTTV_FILTER_STATE_T_BRAND
:
628 case LTTV_FILTER_STATE_EX_MODE
:
629 case LTTV_FILTER_STATE_EX_SUBMODE
:
630 case LTTV_FILTER_STATE_P_STATUS
:
631 case LTTV_FILTER_STATE_CPU
:
632 return LTTV_FILTER_STATE
;
634 case LTTV_FILTER_EVENT_NAME
:
635 case LTTV_FILTER_EVENT_SUBNAME
:
636 case LTTV_FILTER_EVENT_CATEGORY
:
637 case LTTV_FILTER_EVENT_TIME
:
638 case LTTV_FILTER_EVENT_TSC
:
639 case LTTV_FILTER_EVENT_TARGET_PID
:
640 case LTTV_FILTER_EVENT_FIELD
:
641 return LTTV_FILTER_EVENT
;
649 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
651 * Applies the 'equal' operator to the
652 * specified structure and value
653 * @param v1 left member of comparison
654 * @param v2 right member of comparison
655 * @return success/failure of operation
657 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
) {
659 guint
* r
= (guint
*) v1
;
660 return (*r
== v2
.v_uint
);
665 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
667 * Applies the 'equal' operator to the
668 * specified structure and value
669 * @param v1 left member of comparison
670 * @param v2 right member of comparison
671 * @return success/failure of operation
673 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
675 guint64
* r
= (guint64
*) v1
;
676 return (*r
== v2
.v_uint64
);
681 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
683 * Applies the 'equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
689 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
690 guint32
* r
= (guint32
*) v1
;
691 return (*r
== v2
.v_uint32
);
695 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
697 * Applies the 'equal' operator to the
698 * specified structure and value
699 * @param v1 left member of comparison
700 * @param v2 right member of comparison
701 * @return success/failure of operation
703 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
704 guint16
* r
= (guint16
*) v1
;
705 return (*r
== v2
.v_uint16
);
709 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
711 * Applies the 'equal' operator to the
712 * specified structure and value
713 * @param v1 left member of comparison
714 * @param v2 right member of comparison
715 * @return success/failure of operation
717 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
718 double* r
= (double*) v1
;
719 return (*r
== v2
.v_double
);
723 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
725 * Applies the 'equal' operator to the
726 * specified structure and value
727 * @param v1 left member of comparison
728 * @param v2 right member of comparison
729 * @return success/failure of operation
731 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
732 char* r
= (char*) v1
;
733 return (!g_strcasecmp(r
,v2
.v_string
));
737 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
739 * Applies the 'equal' operator to the
740 * specified structure and value
741 * @param v1 left member of comparison
742 * @param v2 right member of comparison
743 * @return success/failure of operation
745 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
746 GQuark
* r
= (GQuark
*) v1
;
747 return (*r
== v2
.v_quark
);
751 * @fn gboolean lttv_apply_op_eq_quarks(gpointer,LttvFieldValue)
753 * Applies the 'equal' operator to the
754 * specified structure and value
755 * @param v1 left member of comparison
756 * @param v2 right member of comparison
757 * @return success/failure of operation
759 gboolean
lttv_apply_op_eq_quarks(const gpointer v1
, LttvFieldValue v2
) {
760 GQuark
*r1
= (GQuark
*) v1
;
762 if (likely(*r1
!= (GQuark
)0) && *r1
!= v2
.v_quarks
.q
[0])
764 if (*r2
!= v2
.v_quarks
.q
[1])
770 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
772 * Applies the 'equal' operator to the
773 * specified structure and value
774 * @param v1 left member of comparison
775 * @param v2 right member of comparison
776 * @return success/failure of operation
778 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
779 LttTime
* r
= (LttTime
*) v1
;
780 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
784 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
786 * Applies the 'not equal' operator to the
787 * specified structure and value
788 * @param v1 left member of comparison
789 * @param v2 right member of comparison
790 * @return success/failure of operation
792 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
) {
793 guint
* r
= (guint
*) v1
;
794 return (*r
!= v2
.v_uint
);
798 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
800 * Applies the 'not equal' operator to the
801 * specified structure and value
802 * @param v1 left member of comparison
803 * @param v2 right member of comparison
804 * @return success/failure of operation
806 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
807 guint64
* r
= (guint64
*) v1
;
808 return (*r
!= v2
.v_uint64
);
812 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
814 * Applies the 'not equal' operator to the
815 * specified structure and value
816 * @param v1 left member of comparison
817 * @param v2 right member of comparison
818 * @return success/failure of operation
820 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
821 guint32
* r
= (guint32
*) v1
;
822 return (*r
!= v2
.v_uint32
);
826 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
828 * Applies the 'not equal' operator to the
829 * specified structure and value
830 * @param v1 left member of comparison
831 * @param v2 right member of comparison
832 * @return success/failure of operation
834 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
835 guint16
* r
= (guint16
*) v1
;
836 return (*r
!= v2
.v_uint16
);
840 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
842 * Applies the 'not equal' operator to the
843 * specified structure and value
844 * @param v1 left member of comparison
845 * @param v2 right member of comparison
846 * @return success/failure of operation
848 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
849 double* r
= (double*) v1
;
850 return (*r
!= v2
.v_double
);
854 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
856 * Applies the 'not equal' operator to the
857 * specified structure and value
858 * @param v1 left member of comparison
859 * @param v2 right member of comparison
860 * @return success/failure of operation
862 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
863 char* r
= (char*) v1
;
864 return (g_strcasecmp(r
,v2
.v_string
));
868 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
870 * Applies the 'not equal' operator to the
871 * specified structure and value
872 * @param v1 left member of comparison
873 * @param v2 right member of comparison
874 * @return success/failure of operation
876 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
877 GQuark
* r
= (GQuark
*) v1
;
878 return (*r
!= v2
.v_quark
);
882 * @fn gboolean lttv_apply_op_ne_quarks(gpointer,LttvFieldValue)
884 * Applies the 'equal' operator to the
885 * specified structure and value
886 * @param v1 left member of comparison
887 * @param v2 right member of comparison
888 * @return success/failure of operation
890 gboolean
lttv_apply_op_ne_quarks(const gpointer v1
, LttvFieldValue v2
) {
891 GQuark
*r1
= (GQuark
*) v1
;
893 if ((*r1
== (GQuark
)0 || *r1
== v2
.v_quarks
.q
[0]) && *r2
== v2
.v_quarks
.q
[1])
900 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
902 * Applies the 'not equal' operator to the
903 * specified structure and value
904 * @param v1 left member of comparison
905 * @param v2 right member of comparison
906 * @return success/failure of operation
908 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
909 LttTime
* r
= (LttTime
*) v1
;
910 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
914 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
916 * Applies the 'lower than' operator to the
917 * specified structure and value
918 * @param v1 left member of comparison
919 * @param v2 right member of comparison
920 * @return success/failure of operation
922 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
) {
923 guint
* r
= (guint
*) v1
;
924 return (*r
< v2
.v_uint
);
928 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
930 * Applies the 'lower than' operator to the
931 * specified structure and value
932 * @param v1 left member of comparison
933 * @param v2 right member of comparison
934 * @return success/failure of operation
936 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
937 guint64
* r
= (guint64
*) v1
;
938 return (*r
< v2
.v_uint64
);
942 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
944 * Applies the 'lower than' operator to the
945 * specified structure and value
946 * @param v1 left member of comparison
947 * @param v2 right member of comparison
948 * @return success/failure of operation
950 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
951 guint32
* r
= (guint32
*) v1
;
952 return (*r
< v2
.v_uint32
);
956 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
958 * Applies the 'lower than' operator to the
959 * specified structure and value
960 * @param v1 left member of comparison
961 * @param v2 right member of comparison
962 * @return success/failure of operation
964 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
965 guint16
* r
= (guint16
*) v1
;
966 return (*r
< v2
.v_uint16
);
970 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
972 * Applies the 'lower than' operator to the
973 * specified structure and value
974 * @param v1 left member of comparison
975 * @param v2 right member of comparison
976 * @return success/failure of operation
978 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
979 double* r
= (double*) v1
;
980 return (*r
< v2
.v_double
);
984 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
986 * Applies the 'lower than' operator to the
987 * specified structure and value
988 * @param v1 left member of comparison
989 * @param v2 right member of comparison
990 * @return success/failure of operation
992 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
993 LttTime
* r
= (LttTime
*) v1
;
994 // 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)));
995 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
999 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
1001 * Applies the 'lower or equal' operator to the
1002 * specified structure and value
1003 * @param v1 left member of comparison
1004 * @param v2 right member of comparison
1005 * @return success/failure of operation
1007 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
) {
1008 guint
* r
= (guint
*) v1
;
1009 return (*r
<= v2
.v_uint
);
1013 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
1015 * Applies the 'lower or equal' operator to the
1016 * specified structure and value
1017 * @param v1 left member of comparison
1018 * @param v2 right member of comparison
1019 * @return success/failure of operation
1021 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
1022 guint64
* r
= (guint64
*) v1
;
1023 return (*r
<= v2
.v_uint64
);
1027 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
1029 * Applies the 'lower or equal' operator to the
1030 * specified structure and value
1031 * @param v1 left member of comparison
1032 * @param v2 right member of comparison
1033 * @return success/failure of operation
1035 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
1036 guint32
* r
= (guint32
*) v1
;
1037 return (*r
<= v2
.v_uint32
);
1041 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
1043 * Applies the 'lower or equal' operator to the
1044 * specified structure and value
1045 * @param v1 left member of comparison
1046 * @param v2 right member of comparison
1047 * @return success/failure of operation
1049 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
1050 guint16
* r
= (guint16
*) v1
;
1051 return (*r
<= v2
.v_uint16
);
1055 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
1057 * Applies the 'lower or equal' operator to the
1058 * specified structure and value
1059 * @param v1 left member of comparison
1060 * @param v2 right member of comparison
1061 * @return success/failure of operation
1063 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
1064 double* r
= (double*) v1
;
1065 return (*r
<= v2
.v_double
);
1069 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
1071 * Applies the 'lower or equal' operator to the
1072 * specified structure and value
1073 * @param v1 left member of comparison
1074 * @param v2 right member of comparison
1075 * @return success/failure of operation
1077 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1078 LttTime
* r
= (LttTime
*) v1
;
1079 // 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)));
1080 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1085 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1087 * Applies the 'greater than' operator to the
1088 * specified structure and value
1089 * @param v1 left member of comparison
1090 * @param v2 right member of comparison
1091 * @return success/failure of operation
1093 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
) {
1094 guint
* r
= (guint
*) v1
;
1095 return (*r
> v2
.v_uint
);
1099 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1101 * Applies the 'greater than' operator to the
1102 * specified structure and value
1103 * @param v1 left member of comparison
1104 * @param v2 right member of comparison
1105 * @return success/failure of operation
1107 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
1108 guint64
* r
= (guint64
*) v1
;
1109 return (*r
> v2
.v_uint64
);
1113 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1115 * Applies the 'greater than' operator to the
1116 * specified structure and value
1117 * @param v1 left member of comparison
1118 * @param v2 right member of comparison
1119 * @return success/failure of operation
1121 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
1122 guint32
* r
= (guint32
*) v1
;
1123 return (*r
> v2
.v_uint32
);
1127 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1129 * Applies the 'greater than' operator to the
1130 * specified structure and value
1131 * @param v1 left member of comparison
1132 * @param v2 right member of comparison
1133 * @return success/failure of operation
1135 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
1136 guint16
* r
= (guint16
*) v1
;
1137 return (*r
> v2
.v_uint16
);
1141 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1143 * Applies the 'greater than' operator to the
1144 * specified structure and value
1145 * @param v1 left member of comparison
1146 * @param v2 right member of comparison
1147 * @return success/failure of operation
1149 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
1150 double* r
= (double*) v1
;
1151 return (*r
> v2
.v_double
);
1155 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1157 * Applies the 'greater than' operator to the
1158 * specified structure and value
1159 * @param v1 left member of comparison
1160 * @param v2 right member of comparison
1161 * @return success/failure of operation
1163 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1164 LttTime
* r
= (LttTime
*) v1
;
1165 // 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)));
1166 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1170 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1172 * Applies the 'greater or equal' operator to the
1173 * specified structure and value
1174 * @param v1 left member of comparison
1175 * @param v2 right member of comparison
1176 * @return success/failure of operation
1178 gboolean
lttv_apply_op_ge_uint(const gpointer v1
, LttvFieldValue v2
) {
1179 guint
* r
= (guint
*) v1
;
1180 return (*r
>= v2
.v_uint
);
1184 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1186 * Applies the 'greater or equal' operator to the
1187 * specified structure and value
1188 * @param v1 left member of comparison
1189 * @param v2 right member of comparison
1190 * @return success/failure of operation
1192 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
1193 guint64
* r
= (guint64
*) v1
;
1194 return (*r
>= v2
.v_uint64
);
1198 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1200 * Applies the 'greater or equal' operator to the
1201 * specified structure and value
1202 * @param v1 left member of comparison
1203 * @param v2 right member of comparison
1204 * @return success/failure of operation
1206 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
1207 guint32
* r
= (guint32
*) v1
;
1208 return (*r
>= v2
.v_uint32
);
1212 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1214 * Applies the 'greater or equal' operator to the
1215 * specified structure and value
1216 * @param v1 left member of comparison
1217 * @param v2 right member of comparison
1218 * @return success/failure of operation
1220 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
1221 guint16
* r
= (guint16
*) v1
;
1222 return (*r
>= v2
.v_uint16
);
1226 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1228 * Applies the 'greater or equal' operator to the
1229 * specified structure and value
1230 * @param v1 left member of comparison
1231 * @param v2 right member of comparison
1232 * @return success/failure of operation
1234 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
1235 double* r
= (double*) v1
;
1236 return (*r
>= v2
.v_double
);
1240 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1242 * Applies the 'greater or equal' operator to the
1243 * specified structure and value
1244 * @param v1 left member of comparison
1245 * @param v2 right member of comparison
1246 * @return success/failure of operation
1248 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
1249 LttTime
* r
= (LttTime
*) v1
;
1250 // 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)));
1251 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1257 * Makes a copy of the current filter tree
1258 * @param tree pointer to the current tree
1259 * @return new copy of the filter tree
1262 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1264 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1266 newtree
->node
= tree
->node
;
1268 newtree
->left
= tree
->left
;
1269 if(newtree
->left
== LTTV_TREE_NODE
) {
1270 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1271 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1272 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1273 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1274 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1275 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1276 /* FIXME: special case for string copy ! */
1277 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1280 newtree
->right
= tree
->right
;
1281 if(newtree
->right
== LTTV_TREE_NODE
) {
1282 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1283 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1284 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1285 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1286 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1287 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1288 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1296 * Makes a copy of the current filter
1297 * @param filter pointer to the current filter
1298 * @return new copy of the filter
1301 lttv_filter_clone(const LttvFilter
* filter
) {
1303 if(!filter
) return NULL
;
1305 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1307 strcpy(newfilter
->expression
,filter
->expression
);
1309 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1317 * @fn LttvFilter* lttv_filter_new()
1319 * Creates a new LttvFilter
1320 * @return the current LttvFilter or NULL if error
1325 LttvFilter
* filter
= g_new(LttvFilter
,1);
1326 filter
->expression
= NULL
;
1327 filter
->head
= NULL
;
1334 * @fn gboolean lttv_filter_update(LttvFilter*)
1336 * Updates the current LttvFilter by building
1337 * its tree based upon the expression string
1338 * @param filter pointer to the current LttvFilter
1339 * @return Failure/Success of operation
1342 lttv_filter_update(LttvFilter
* filter
) {
1344 // g_print("filter::lttv_filter_new()\n"); /* debug */
1346 if(filter
->expression
== NULL
) return FALSE
;
1350 p_nesting
=0, /* parenthesis nesting value */
1352 guint expression_len
;
1356 *tree
= lttv_filter_tree_new(), /* main tree */
1357 *subtree
= NULL
, /* buffer for subtrees */
1358 *t1
, /* buffer #1 */
1359 *t2
, /* buffer #2 */
1360 *t3
; /* buffer #3 */
1364 * If the tree already exists,
1365 * destroy it and build a new one
1367 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1368 filter
->head
= NULL
; /* will be assigned at the end */
1372 * each element of the list
1373 * is a sub tree created
1374 * by the use of parenthesis in the
1375 * global expression. The final tree
1376 * will be the one left at the root of
1379 GPtrArray
*tree_stack
= g_ptr_array_new();
1380 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1382 /* temporary values */
1383 GString
*a_field_component
= g_string_new("");
1384 GString
*a_string_spaces
= g_string_new("");
1385 GPtrArray
*a_field_path
= g_ptr_array_new();
1387 /* simple expression buffer */
1388 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1390 gint nest_quotes
= 0;
1393 * Parse entire expression and construct
1394 * the binary tree. There are two steps
1395 * in browsing that string
1396 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1397 * 2. finding simple expressions
1398 * - field path ( separated by dots )
1399 * - op ( >, <, =, >=, <=, !=)
1400 * - value ( integer, string ... )
1401 * To spare computing time, the whole
1402 * string is parsed in this loop for a
1403 * O(n) complexity order.
1405 * When encountering logical op &,|,^
1406 * 1. parse the last value if any
1407 * 2. create a new tree
1408 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1409 * 4. concatenate this tree with the current tree on top of the stack
1410 * When encountering math ops >,>=,<,<=,=,!=
1411 * 1. add to op to the simple expression
1412 * 2. concatenate last field component to field path
1413 * When encountering concatening ops .
1414 * 1. concatenate last field component to field path
1415 * When encountering opening parenthesis (,{,[
1416 * 1. create a new subtree on top of tree stack
1417 * When encountering closing parenthesis ),},]
1418 * 1. add the expression on right child of the current tree
1419 * 2. the subtree is completed, allocate a new subtree
1420 * 3. pop the tree value from the tree stack
1424 struct timeval starttime
;
1425 struct timeval endtime
;
1426 gettimeofday(&starttime
, NULL
);
1429 expression_len
= strlen(filter
->expression
);
1430 for(i
=0;i
<expression_len
;i
++) {
1432 // g_print("%c\n ",filter->expression[i]);
1434 switch(filter
->expression
[i
]) {
1436 if(filter
->expression
[i
+1] == '\"') {
1445 if(a_string_spaces
->len
!= 0) {
1446 a_field_component
= g_string_append(
1447 a_field_component
, a_string_spaces
->str
);
1448 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1450 a_field_component
= g_string_append_c(a_field_component
,
1451 filter
->expression
[i
]);
1455 switch(filter
->expression
[i
]) {
1461 /* get current tree in tree stack */
1462 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1464 /* get current node at absolute right */
1465 while(t1
->right
!= LTTV_TREE_IDLE
) {
1466 g_assert(t1
->right
== LTTV_TREE_NODE
);
1469 t2
= lttv_filter_tree_new();
1470 t2
->node
= LTTV_LOGICAL_AND
;
1471 t1
->right
= LTTV_TREE_NODE
;
1473 if(not) { /* add not operator to tree */
1474 t3
= lttv_filter_tree_new();
1475 t3
->node
= LTTV_LOGICAL_NOT
;
1476 t2
->left
= LTTV_TREE_NODE
;
1481 if(subtree
!= NULL
) { /* append subtree to current tree */
1482 t2
->left
= LTTV_TREE_NODE
;
1483 t2
->l_child
.t
= subtree
;
1485 } else { /* append a simple expression */
1486 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1487 a_field_component
= g_string_new("");
1488 g_string_free(a_string_spaces
, TRUE
);
1489 a_string_spaces
= g_string_new("");
1490 t2
->left
= LTTV_TREE_LEAF
;
1491 t2
->l_child
.leaf
= a_simple_expression
;
1492 a_simple_expression
= lttv_simple_expression_new();
1498 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1499 while(t1
->right
!= LTTV_TREE_IDLE
) {
1500 g_assert(t1
->right
== LTTV_TREE_NODE
);
1503 t2
= lttv_filter_tree_new();
1504 t2
->node
= LTTV_LOGICAL_OR
;
1505 t1
->right
= LTTV_TREE_NODE
;
1507 if(not) { // add not operator to tree
1508 t3
= lttv_filter_tree_new();
1509 t3
->node
= LTTV_LOGICAL_NOT
;
1510 t2
->left
= LTTV_TREE_NODE
;
1515 if(subtree
!= NULL
) { /* append subtree to current tree */
1516 t2
->left
= LTTV_TREE_NODE
;
1517 t2
->l_child
.t
= subtree
;
1519 } else { /* append a simple expression */
1520 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1521 a_field_component
= g_string_new("");
1522 g_string_free(a_string_spaces
, TRUE
);
1523 a_string_spaces
= g_string_new("");
1524 t2
->left
= LTTV_TREE_LEAF
;
1525 t2
->l_child
.leaf
= a_simple_expression
;
1526 a_simple_expression
= lttv_simple_expression_new();
1532 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1533 while(t1
->right
!= LTTV_TREE_IDLE
) {
1534 g_assert(t1
->right
== LTTV_TREE_NODE
);
1537 t2
= lttv_filter_tree_new();
1538 t2
->node
= LTTV_LOGICAL_XOR
;
1539 t1
->right
= LTTV_TREE_NODE
;
1541 if(not) { // add not operator to tree
1542 t3
= lttv_filter_tree_new();
1543 t3
->node
= LTTV_LOGICAL_NOT
;
1544 t2
->left
= LTTV_TREE_NODE
;
1549 if(subtree
!= NULL
) { /* append subtree to current tree */
1550 t2
->left
= LTTV_TREE_NODE
;
1551 t2
->l_child
.t
= subtree
;
1553 } else { /* append a simple expression */
1554 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1555 a_field_component
= g_string_new("");
1556 g_string_free(a_string_spaces
, TRUE
);
1557 a_string_spaces
= g_string_new("");
1558 t2
->left
= LTTV_TREE_LEAF
;
1559 t2
->l_child
.leaf
= a_simple_expression
;
1560 a_simple_expression
= lttv_simple_expression_new();
1564 case '!': /* not, or not equal (math op) */
1566 if(filter
->expression
[i
+1] == '=') { /* != */
1567 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1568 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1569 a_field_component
= g_string_new("");
1570 g_string_free(a_string_spaces
, TRUE
);
1571 a_string_spaces
= g_string_new("");
1572 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1579 case '(': /* start of parenthesis */
1583 p_nesting
++; /* incrementing parenthesis nesting value */
1584 t1
= lttv_filter_tree_new();
1585 if(not) { /* add not operator to tree */
1586 t3
= lttv_filter_tree_new();
1587 t3
->node
= LTTV_LOGICAL_NOT
;
1588 t1
->right
= LTTV_TREE_NODE
;
1592 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1595 case ')': /* end of parenthesis */
1599 p_nesting
--; /* decrementing parenthesis nesting value */
1600 if(p_nesting
<0 || tree_stack
->len
<2) {
1601 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1602 is not valid due to parenthesis incorrect use",filter
->expression
);
1606 /* there must at least be the root tree left in the array */
1607 g_assert(tree_stack
->len
>0);
1609 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1610 while(t1
->right
!= LTTV_TREE_IDLE
) {
1613 if(not) { // add not operator to tree
1615 t3
= lttv_filter_tree_new();
1616 t3
->node
= LTTV_LOGICAL_NOT
;
1617 t1
->right
= LTTV_TREE_NODE
;
1622 if(subtree
!= NULL
) { /* append subtree to current tree */
1623 t1
->right
= LTTV_TREE_NODE
;
1624 t1
->r_child
.t
= subtree
;
1625 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1626 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1627 } else { /* assign subtree as current tree */
1628 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1629 a_field_component
= g_string_new("");
1630 g_string_free(a_string_spaces
, TRUE
);
1631 a_string_spaces
= g_string_new("");
1632 t1
->right
= LTTV_TREE_LEAF
;
1633 t1
->r_child
.leaf
= a_simple_expression
;
1634 a_simple_expression
= lttv_simple_expression_new();
1635 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1640 * mathematic operators
1642 case '<': /* lower, lower or equal */
1644 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1645 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1646 a_field_component
= g_string_new("");
1647 g_string_free(a_string_spaces
, TRUE
);
1648 a_string_spaces
= g_string_new("");
1649 if(filter
->expression
[i
+1] == '=') { /* <= */
1651 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1652 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1655 case '>': /* higher, higher or equal */
1657 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1658 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1659 a_field_component
= g_string_new("");
1660 g_string_free(a_string_spaces
, TRUE
);
1661 a_string_spaces
= g_string_new("");
1662 if(filter
->expression
[i
+1] == '=') { /* >= */
1664 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1665 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1668 case '=': /* equal */
1670 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1671 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1672 a_field_component
= g_string_new("");
1673 g_string_free(a_string_spaces
, TRUE
);
1674 a_string_spaces
= g_string_new("");
1675 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1679 * Field concatening caracter
1684 * divide field expression into elements
1685 * in a_field_path array.
1687 * A dot can also be present in double values
1689 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1690 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1691 a_field_component
= g_string_new("");
1692 g_string_free(a_string_spaces
, TRUE
);
1693 a_string_spaces
= g_string_new("");
1695 /* Operator found, we are in the value field */
1696 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1699 case ' ': /* keep spaces that are within a field component */
1700 if(a_field_component
->len
== 0) break; /* ignore */
1702 a_string_spaces
= g_string_append_c(a_string_spaces
,
1703 filter
->expression
[i
]);
1705 case '\n': /* ignore */
1708 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1710 default: /* concatening current string */
1711 if(a_string_spaces
->len
!= 0) {
1712 a_field_component
= g_string_append(
1713 a_field_component
, a_string_spaces
->str
);
1714 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1716 a_field_component
= g_string_append_c(a_field_component
,
1717 filter
->expression
[i
]);
1722 * Preliminary check to see
1723 * if tree was constructed correctly
1726 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1727 is not valid due to parenthesis incorrect use",filter
->expression
);
1731 if(tree_stack
->len
!= 1) /* only root tree should remain */
1735 * processing last element of expression
1737 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1738 while(t1
->right
!= LTTV_TREE_IDLE
) {
1739 g_assert(t1
->right
== LTTV_TREE_NODE
);
1742 if(not) { // add not operator to tree
1743 t3
= lttv_filter_tree_new();
1744 t3
->node
= LTTV_LOGICAL_NOT
;
1745 t1
->right
= LTTV_TREE_NODE
;
1750 if(subtree
!= NULL
) { /* add the subtree */
1751 t1
->right
= LTTV_TREE_NODE
;
1752 t1
->r_child
.t
= subtree
;
1754 } else { /* add a leaf */
1755 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1756 a_field_component
= NULL
;
1757 g_string_free(a_string_spaces
, TRUE
);
1758 a_string_spaces
= NULL
;
1759 t1
->right
= LTTV_TREE_LEAF
;
1760 t1
->r_child
.leaf
= a_simple_expression
;
1761 a_simple_expression
= NULL
;
1765 /* free the pointer array */
1766 g_assert(a_field_path
->len
== 0);
1767 g_ptr_array_free(a_field_path
,TRUE
);
1769 /* free the tree stack -- but keep the root tree */
1770 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1771 g_ptr_array_free(tree_stack
,TRUE
);
1773 /* free the field buffer if allocated */
1774 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1775 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1777 /* free the simple expression buffer if allocated */
1778 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1780 g_assert(filter
->head
!= NULL
); /* tree should exist */
1781 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1784 gettimeofday(&endtime
, NULL
);
1786 /* Calcul du temps de l'algorithme */
1787 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1788 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1789 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1790 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1794 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1795 lttv_print_tree(filter
->head
,0) ;
1796 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1804 * @fn void lttv_filter_destroy(LttvFilter*)
1806 * Destroy the current LttvFilter
1807 * @param filter pointer to the current LttvFilter
1810 lttv_filter_destroy(LttvFilter
* filter
) {
1814 if(filter
->expression
)
1815 g_free(filter
->expression
);
1817 lttv_filter_tree_destroy(filter
->head
);
1823 * @fn LttvFilterTree* lttv_filter_tree_new()
1825 * Assign a new tree for the current expression
1827 * @return pointer of LttvFilterTree
1830 lttv_filter_tree_new() {
1831 LttvFilterTree
* tree
;
1833 tree
= g_new(LttvFilterTree
,1);
1834 tree
->node
= 0; //g_new(lttv_expression,1);
1835 tree
->left
= LTTV_TREE_IDLE
;
1836 tree
->right
= LTTV_TREE_IDLE
;
1837 tree
->r_child
.t
= NULL
;
1838 tree
->l_child
.t
= NULL
;
1844 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1846 * Append a new expression to the expression
1847 * defined in the current filter
1848 * @param filter pointer to the current LttvFilter
1849 * @param expression string that must be appended
1850 * @return Success/Failure of operation
1853 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1855 if(expression
== NULL
) return FALSE
;
1856 if(filter
== NULL
) return FALSE
;
1857 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1859 GString
* s
= g_string_new("");
1860 if(filter
->expression
!= NULL
) {
1861 s
= g_string_append(s
,filter
->expression
);
1862 s
= g_string_append_c(s
,'&');
1864 s
= g_string_append(s
,expression
);
1866 g_free(filter
->expression
);
1867 filter
->expression
= g_string_free(s
,FALSE
);
1869 /* TRUE if construction of tree proceeded without errors */
1870 return lttv_filter_update(filter
);
1875 * @fn void lttv_filter_clear_expression(LttvFilter*)
1877 * Clear the filter expression from the
1878 * current filter and sets its pointer to NULL
1879 * @param filter pointer to the current LttvFilter
1882 lttv_filter_clear_expression(LttvFilter
* filter
) {
1884 if(filter
->expression
!= NULL
) {
1885 g_free(filter
->expression
);
1886 filter
->expression
= NULL
;
1892 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1894 * Destroys the tree and his sub-trees
1895 * @param tree Tree which must be destroyed
1898 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1900 if(tree
== NULL
) return;
1902 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1903 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1905 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1906 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1908 // g_free(tree->node);
1913 * Global parsing function for the current
1915 * @param t pointer to the current LttvFilterTree
1916 * @param event current LttEvent, NULL if not used
1917 * @param tracefile current LttTracefile, NULL if not used
1918 * @param trace current LttTrace, NULL if not used
1919 * @param state current LttvProcessState, NULL if not used
1920 * @param context current LttvTracefileContext, NULL if not used
1921 * @return response of filter
1924 lttv_filter_tree_parse(
1925 const LttvFilterTree
* t
,
1926 const LttEvent
* event
,
1927 const LttTracefile
* tracefile
,
1928 const LttTrace
* trace
,
1929 const LttvTracefileContext
* context
,
1930 const LttvProcessState
* state
,
1931 const LttvTraceContext
* tc
1936 * Each tree is parsed in inorder.
1937 * This way, it's possible to apply the left filter of the
1938 * tree, then decide whether or not the right branch should
1939 * be parsed depending on the linking logical operator
1941 * Each node consists in a
1942 * 1. logical operator
1943 * 2. left child ( node or simple expression )
1944 * 3. right child ( node or simple expression )
1946 * When the child is a simple expression, we must
1947 * before all determine if the expression refers to
1948 * a structure which is whithin observation ( not NULL ).
1949 * -If so, the expression is evaluated.
1950 * -If not, the result is set to TRUE since this particular
1951 * operation does not interfere with the lttv structure
1953 * The result of each simple expression will directly
1954 * affect the next branch. This way, depending on
1955 * the linking logical operator, the parser will decide
1956 * to explore or not the next branch.
1958 * -If result of left branch is 0 / FALSE
1959 * then don't explore right branch and return 0;
1960 * -If result of left branch is 1 / TRUE then explore
1962 * -If result of left branch is 1 / TRUE
1963 * then don't explore right branch and return 1;
1964 * -If result of left branch is 0 / FALSE then explore
1966 * -Result of left branch will not affect exploration of
1970 gboolean lresult
= FALSE
, rresult
= FALSE
;
1972 LttvTraceState
*ts
= NULL
;
1973 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
1975 ts
= (LttvTraceState
*)tc
;
1977 ts
= (LttvTraceState
*)context
->t_context
;
1980 guint cpu
= tfs
->cpu
;
1982 state
= ts
->running_process
[cpu
];
1988 if(t
->left
== LTTV_TREE_NODE
) {
1989 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
1991 else if(t
->left
== LTTV_TREE_LEAF
) {
1992 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1996 * Parse linking operator
1997 * make a cutoff if possible
1999 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
2000 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
2003 * Parse right branch
2005 if(t
->right
== LTTV_TREE_NODE
) {
2006 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
2008 else if(t
->right
== LTTV_TREE_LEAF
) {
2009 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
2014 * Apply and return the
2015 * logical link between the
2019 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
2020 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
2021 case LTTV_LOGICAL_NOT
:
2022 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
2023 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
2024 case 0: return (rresult
);
2027 * This case should never be
2028 * parsed, if so, this subtree
2037 * This function parses a particular branch of the tree
2038 * @param se pointer to the current LttvSimpleExpression
2039 * @param event current LttEvent, NULL if not used
2040 * @param tracefile current LttTracefile, NULL if not used
2041 * @param trace current LttTrace, NULL if not used
2042 * @param state current LttvProcessState, NULL if not used
2043 * @param context current LttvTracefileContext, NULL if not used
2044 * @return response of filter
2047 lttv_filter_tree_parse_branch(
2048 const LttvSimpleExpression
* se
,
2049 const LttEvent
* event
,
2050 const LttTracefile
* tracefile
,
2051 const LttTrace
* trace
,
2052 const LttvProcessState
* state
,
2053 const LttvTracefileContext
* context
) {
2058 case LTTV_FILTER_TRACE_NAME
:
2059 if(trace
== NULL
) return TRUE
;
2061 GQuark quark
= ltt_trace_name(trace
);
2062 return se
->op((gpointer
)&quark
,v
);
2065 case LTTV_FILTER_TRACEFILE_NAME
:
2066 if(tracefile
== NULL
) return TRUE
;
2068 GQuark quark
= ltt_tracefile_name(tracefile
);
2069 return se
->op((gpointer
)&quark
,v
);
2072 case LTTV_FILTER_STATE_PID
:
2073 if(state
== NULL
) return TRUE
;
2074 else return se
->op((gpointer
)&state
->pid
,v
);
2076 case LTTV_FILTER_STATE_PPID
:
2077 if(state
== NULL
) return TRUE
;
2078 else return se
->op((gpointer
)&state
->ppid
,v
);
2080 case LTTV_FILTER_STATE_CT
:
2081 if(state
== NULL
) return TRUE
;
2083 return se
->op((gpointer
)&state
->creation_time
,v
);
2086 case LTTV_FILTER_STATE_IT
:
2087 if(state
== NULL
) return TRUE
;
2089 return se
->op((gpointer
)&state
->insertion_time
,v
);
2092 case LTTV_FILTER_STATE_P_NAME
:
2093 if(state
== NULL
) return TRUE
;
2095 GQuark quark
= state
->name
;
2096 return se
->op((gpointer
)&quark
,v
);
2099 case LTTV_FILTER_STATE_T_BRAND
:
2100 if(state
== NULL
) return TRUE
;
2102 GQuark quark
= state
->brand
;
2103 return se
->op((gpointer
)&quark
,v
);
2106 case LTTV_FILTER_STATE_EX_MODE
:
2107 if(state
== NULL
) return TRUE
;
2108 else return se
->op((gpointer
)&state
->state
->t
,v
);
2110 case LTTV_FILTER_STATE_EX_SUBMODE
:
2111 if(state
== NULL
) return TRUE
;
2112 else return se
->op((gpointer
)&state
->state
->n
,v
);
2114 case LTTV_FILTER_STATE_P_STATUS
:
2115 if(state
== NULL
) return TRUE
;
2116 else return se
->op((gpointer
)&state
->state
->s
,v
);
2118 case LTTV_FILTER_STATE_CPU
:
2119 if(state
== NULL
) return TRUE
;
2121 return se
->op((gpointer
)&state
->cpu
,v
);
2124 case LTTV_FILTER_EVENT_NAME
:
2125 if(event
== NULL
) return TRUE
;
2127 struct marker_info
*info
;
2129 LttTracefile
*tf
= context
->tf
;
2130 qtuple
[0] = ltt_tracefile_name(tracefile
);
2131 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2132 g_assert(info
!= NULL
);
2133 qtuple
[1] = info
->name
;
2134 return se
->op((gpointer
)qtuple
,v
);
2137 case LTTV_FILTER_EVENT_SUBNAME
:
2138 if(event
== NULL
) return TRUE
;
2140 struct marker_info
*info
;
2141 LttTracefile
*tf
= context
->tf
;
2142 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2143 g_assert(info
!= NULL
);
2144 GQuark quark
= info
->name
;
2145 return se
->op((gpointer
)&quark
,v
);
2148 case LTTV_FILTER_EVENT_CATEGORY
:
2150 * TODO: Not yet implemented
2154 case LTTV_FILTER_EVENT_TIME
:
2155 if(event
== NULL
) return TRUE
;
2157 LttTime time
= ltt_event_time(event
);
2158 return se
->op((gpointer
)&time
,v
);
2161 case LTTV_FILTER_EVENT_TSC
:
2162 if(event
== NULL
) return TRUE
;
2164 LttCycleCount count
= ltt_event_cycle_count(event
);
2165 return se
->op((gpointer
)&count
,v
);
2168 case LTTV_FILTER_EVENT_TARGET_PID
:
2169 if(context
== NULL
) return TRUE
;
2172 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2173 return se
->op((gpointer
)&target_pid
,v
);
2176 case LTTV_FILTER_EVENT_FIELD
:
2178 * TODO: Use the offset to
2179 * find the dynamic field
2180 * in the event struct
2185 * This case should never be
2186 * parsed, if so, the whole
2187 * filtering is cancelled
2189 g_warning("Error while parsing the filter tree");
2193 /* should never get here */
2201 * Debug function. Prints tree memory allocation.
2202 * @param t the pointer to the current LttvFilterTree
2205 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
2207 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2208 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2209 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2211 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2212 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"));
2213 if(t
->left
== LTTV_TREE_LEAF
) {
2214 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2215 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2216 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2218 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"));
2219 if(t
->right
== LTTV_TREE_LEAF
) {
2220 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2221 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2222 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2225 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2226 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2230 * @fn static void module_init()
2232 * Initializes the filter module and specific values
2234 static void module_init()
2240 * Destroys the filter module and specific values
2242 static void module_destroy()
2248 LTTV_MODULE("filter", "Filters traceset and events", \
2249 "Filters traceset and events specifically to user input", \
2250 module_init
, module_destroy
)