1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 /*! \file lttv/lttv/filter.c
20 * \brief Defines the core filter of application
22 * consist in AND, OR and NOT nested expressions, forming a tree with
23 * simple relations as leaves. The simple relations test if a field
24 * in an event is equal, not equal, smaller, smaller or equal, larger, or
25 * larger or equal to a specified value.
27 * Fields specified in a simple expression can take following
31 * LttvTracefileContext{}
33 * | |->name (String, converted to GQuark)
34 * | |->category (String, not yet implemented)
36 * | |->tsc (LttCycleCount --> uint64)
40 * | |->"sub-field name"
42 * | |->"leaf-field name" (field type)
44 * | |->name (String, converted to GQuark)
46 * | |->name (String, converted to GQuark)
50 * |->creation_time (LttTime)
51 * |->insertion_time (LttTime)
52 * |->process_name (String, converted to GQuark)
53 * |->execution_mode (LttvExecutionMode)
54 * |->execution_submode (LttvExecutionSubmode)
55 * |->process_status (LttvProcessStatus)
62 * - refine switch of expression in multiple uses functions
63 * - remove the idle expressions in the tree
76 #include <lttv/filter.h>
79 * @fn LttvSimpleExpression* lttv_simple_expression_new()
81 * Constructor for LttvSimpleExpression
82 * @return pointer to new LttvSimpleExpression
85 lttv_simple_expression_new() {
87 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
89 se
->field
= LTTV_FILTER_UNDEFINED
;
97 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
99 * Parse through filtering field hierarchy as specified
100 * by user. This function compares each value to
101 * predetermined quarks
102 * @param fp The field path list
103 * @param se current simple expression
104 * @return success/failure of operation
107 lttv_simple_expression_assign_field(GPtrArray
* fp
, LttvSimpleExpression
* se
) {
111 if(fp
->len
< 2) return FALSE
;
112 g_assert(f
=g_ptr_array_remove_index(fp
,0));
115 * Parse through the specified
118 * Take note however that the
119 * 'event' subfields might change
120 * depending on values specified
121 * in core.xml file. Hence, if
122 * none of the subfields in the
123 * array match the hardcoded
124 * subfields, it will be considered
127 if(!g_strcasecmp(f
->str
,"trace") ) {
132 g_string_free(f
,TRUE
);
133 f
=g_ptr_array_remove_index(fp
,0);
134 if(!g_strcasecmp(f
->str
,"name")) {
135 se
->field
= LTTV_FILTER_TRACE_NAME
;
137 } else if(!g_strcasecmp(f
->str
,"traceset") ) {
139 * FIXME: not yet implemented !
141 } else if(!g_strcasecmp(f
->str
,"tracefile") ) {
146 g_string_free(f
,TRUE
);
147 f
=g_ptr_array_remove_index(fp
,0);
148 if(!g_strcasecmp(f
->str
,"name")) {
149 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
151 } else if(!g_strcasecmp(f
->str
,"state") ) {
156 * state.creation_time
157 * state.insertion_time
159 * state.execution_mode
160 * state.execution_submode
161 * state.process_status
164 g_string_free(f
,TRUE
);
165 f
=g_ptr_array_remove_index(fp
,0);
166 if(!g_strcasecmp(f
->str
,"pid") ) {
167 se
->field
= LTTV_FILTER_STATE_PID
;
169 else if(!g_strcasecmp(f
->str
,"ppid") ) {
170 se
->field
= LTTV_FILTER_STATE_PPID
;
172 else if(!g_strcasecmp(f
->str
,"creation_time") ) {
173 se
->field
= LTTV_FILTER_STATE_CT
;
175 else if(!g_strcasecmp(f
->str
,"insertion_time") ) {
176 se
->field
= LTTV_FILTER_STATE_IT
;
178 else if(!g_strcasecmp(f
->str
,"process_name") ) {
179 se
->field
= LTTV_FILTER_STATE_P_NAME
;
181 else if(!g_strcasecmp(f
->str
,"execution_mode") ) {
182 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
184 else if(!g_strcasecmp(f
->str
,"execution_submode") ) {
185 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
187 else if(!g_strcasecmp(f
->str
,"process_status") ) {
188 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
190 else if(!g_strcasecmp(f
->str
,"cpu") ) {
191 se
->field
= LTTV_FILTER_STATE_CPU
;
193 } else if(!g_strcasecmp(f
->str
,"event") ) {
201 g_string_free(f
,TRUE
);
202 f
=g_ptr_array_remove_index(fp
,0);
203 if(!g_strcasecmp(f
->str
,"name") ) {
204 se
->field
= LTTV_FILTER_EVENT_NAME
;
206 else if(!g_strcasecmp(f
->str
,"category") ) {
208 * FIXME: Category not yet functional in lttv
210 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
212 else if(!g_strcasecmp(f
->str
,"time") ) {
213 se
->field
= LTTV_FILTER_EVENT_TIME
;
215 else if(!g_strcasecmp(f
->str
,"tsc") ) {
216 se
->field
= LTTV_FILTER_EVENT_TSC
;
218 else { /* core.xml specified options */
219 se
->field
= LTTV_FILTER_EVENT_FIELD
;
222 g_warning("Unrecognized field in filter string");
225 /* free memory for last string */
226 g_string_free(f
,TRUE
);
228 /* array should be empty */
229 g_assert(fp
->len
== 0);
231 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
232 g_warning("The specified field was not recognized !");
239 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
241 * Sets the function pointer for the current
243 * @param se current simple expression
244 * @param op current operator
245 * @return success/failure of operation
248 lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
, LttvExpressionOp op
) {
254 case LTTV_FILTER_TRACE_NAME
:
255 case LTTV_FILTER_TRACEFILE_NAME
:
256 case LTTV_FILTER_STATE_P_NAME
:
257 case LTTV_FILTER_EVENT_NAME
:
258 case LTTV_FILTER_STATE_CPU
:
259 case LTTV_FILTER_STATE_EX_MODE
:
260 case LTTV_FILTER_STATE_EX_SUBMODE
:
261 case LTTV_FILTER_STATE_P_STATUS
:
264 se
->op
= lttv_apply_op_eq_quark
;
267 se
->op
= lttv_apply_op_ne_quark
;
270 g_warning("Error encountered in operator assignment = or != expected");
277 case LTTV_FILTER_STATE_PID
:
278 case LTTV_FILTER_STATE_PPID
:
279 case LTTV_FILTER_EVENT_TSC
:
282 se
->op
= lttv_apply_op_eq_uint64
;
285 se
->op
= lttv_apply_op_ne_uint64
;
288 se
->op
= lttv_apply_op_lt_uint64
;
291 se
->op
= lttv_apply_op_le_uint64
;
294 se
->op
= lttv_apply_op_gt_uint64
;
297 se
->op
= lttv_apply_op_ge_uint64
;
300 g_warning("Error encountered in operator assignment");
306 * Entered as string, converted to enum
308 * can only be compared with 'equal' or 'not equal' operators
310 * unsigned int of 16 bits are used here since enums
311 * should not go over 2^16-1 values
315 // case LTTV_FIELD_EQ:
316 // se->op = lttv_apply_op_eq_uint16;
318 // case LTTV_FIELD_NE:
319 // se->op = lttv_apply_op_ne_uint16;
322 // g_warning("Error encountered in operator assignment = or != expected");
329 case LTTV_FILTER_STATE_CT
:
330 case LTTV_FILTER_STATE_IT
:
331 case LTTV_FILTER_EVENT_TIME
:
334 se
->op
= lttv_apply_op_eq_ltttime
;
337 se
->op
= lttv_apply_op_ne_ltttime
;
340 se
->op
= lttv_apply_op_lt_ltttime
;
343 se
->op
= lttv_apply_op_le_ltttime
;
346 se
->op
= lttv_apply_op_gt_ltttime
;
349 se
->op
= lttv_apply_op_ge_ltttime
;
352 g_warning("Error encountered in operator assignment");
357 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
366 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
368 * Assign the value field to the current LttvSimpleExpression
369 * @param se pointer to the current LttvSimpleExpression
370 * @param value string value for simple expression
373 lttv_simple_expression_assign_value(LttvSimpleExpression
* se
, char* value
) {
376 gboolean is_double
= FALSE
;
377 LttTime t
= ltt_time_zero
;
384 * entered as strings, converted to Quarks
386 case LTTV_FILTER_TRACE_NAME
:
387 case LTTV_FILTER_TRACEFILE_NAME
:
388 case LTTV_FILTER_STATE_P_NAME
:
389 case LTTV_FILTER_EVENT_NAME
:
390 case LTTV_FILTER_STATE_CPU
:
391 case LTTV_FILTER_STATE_EX_MODE
:
392 case LTTV_FILTER_STATE_EX_SUBMODE
:
393 case LTTV_FILTER_STATE_P_STATUS
:
394 // se->value.v_string = value;
395 se
->value
.v_uint32
= g_quark_from_string(value
);
399 * integer -- supposed to be uint64
401 case LTTV_FILTER_STATE_PID
:
402 case LTTV_FILTER_STATE_PPID
:
403 case LTTV_FILTER_EVENT_TSC
:
404 se
->value
.v_uint64
= atoi(value
);
410 case LTTV_FILTER_STATE_CT
:
411 case LTTV_FILTER_STATE_IT
:
412 case LTTV_FILTER_EVENT_TIME
:
413 //se->value.v_double = atof(value);
415 * parsing logic could be optimised,
416 * but as for now, simpler this way
418 v
= g_string_new("");
419 for(i
=0;i
<strlen(value
);i
++) {
420 if(value
[i
] == '.') {
421 /* cannot specify number with more than one '.' */
422 if(is_double
) return FALSE
;
423 else is_double
= TRUE
;
425 g_string_free(v
,TRUE
);
426 v
= g_string_new("");
427 } else g_string_append_c(v
,value
[i
]);
429 /* number can be integer or double */
430 if(is_double
) t
.tv_nsec
= atoi(v
);
431 else t
.tv_sec
= atoi(v
);
433 g_string_free(v
,TRUE
);
435 se
->value
.v_ltttime
= t
;
439 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
449 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
451 * Disallocate memory for the current
453 * @param se pointer to the current LttvSimpleExpression
456 lttv_simple_expression_destroy(LttvSimpleExpression
* se
) {
458 // g_free(se->value);
459 // switch(se->field) {
460 // case LTTV_FILTER_TRACE_NAME:
461 // case LTTV_FILTER_TRACEFILE_NAME:
462 // case LTTV_FILTER_STATE_P_NAME:
463 // case LTTV_FILTER_EVENT_NAME:
464 // g_free(se->value.v_string);
472 * @fn gint lttv_struct_type(gint)
474 * Finds the structure type depending
475 * on the fields in parameters
476 * @params ft Field of the current structure
477 * @return LttvStructType enum or -1 for error
480 lttv_struct_type(gint ft
) {
483 case LTTV_FILTER_TRACE_NAME
:
484 return LTTV_FILTER_TRACE
;
486 case LTTV_FILTER_TRACEFILE_NAME
:
487 return LTTV_FILTER_TRACEFILE
;
489 case LTTV_FILTER_STATE_PID
:
490 case LTTV_FILTER_STATE_PPID
:
491 case LTTV_FILTER_STATE_CT
:
492 case LTTV_FILTER_STATE_IT
:
493 case LTTV_FILTER_STATE_P_NAME
:
494 case LTTV_FILTER_STATE_EX_MODE
:
495 case LTTV_FILTER_STATE_EX_SUBMODE
:
496 case LTTV_FILTER_STATE_P_STATUS
:
497 case LTTV_FILTER_STATE_CPU
:
498 return LTTV_FILTER_STATE
;
500 case LTTV_FILTER_EVENT_NAME
:
501 case LTTV_FILTER_EVENT_CATEGORY
:
502 case LTTV_FILTER_EVENT_TIME
:
503 case LTTV_FILTER_EVENT_TSC
:
504 case LTTV_FILTER_EVENT_FIELD
:
505 return LTTV_FILTER_EVENT
;
513 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
515 * Applies the 'equal' operator to the
516 * specified structure and value
517 * @param v1 left member of comparison
518 * @param v2 right member of comparison
519 * @return success/failure of operation
521 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
) {
523 guint64
* r
= (guint64
*) v1
;
524 return (*r
== v2
.v_uint64
);
529 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
531 * Applies the 'equal' operator to the
532 * specified structure and value
533 * @param v1 left member of comparison
534 * @param v2 right member of comparison
535 * @return success/failure of operation
537 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
) {
538 guint32
* r
= (guint32
*) v1
;
539 return (*r
== v2
.v_uint32
);
543 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
545 * Applies the 'equal' operator to the
546 * specified structure and value
547 * @param v1 left member of comparison
548 * @param v2 right member of comparison
549 * @return success/failure of operation
551 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
) {
552 guint16
* r
= (guint16
*) v1
;
553 return (*r
== v2
.v_uint16
);
557 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
559 * Applies the 'equal' operator to the
560 * specified structure and value
561 * @param v1 left member of comparison
562 * @param v2 right member of comparison
563 * @return success/failure of operation
565 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
) {
566 double* r
= (double*) v1
;
567 return (*r
== v2
.v_double
);
571 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
573 * Applies the 'equal' operator to the
574 * specified structure and value
575 * @param v1 left member of comparison
576 * @param v2 right member of comparison
577 * @return success/failure of operation
579 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
) {
580 char* r
= (char*) v1
;
581 return (!g_strcasecmp(r
,v2
.v_string
));
585 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
587 * Applies the 'equal' operator to the
588 * specified structure and value
589 * @param v1 left member of comparison
590 * @param v2 right member of comparison
591 * @return success/failure of operation
593 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
) {
594 GQuark
* r
= (GQuark
*) v1
;
595 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
596 return (*r
== v2
.v_uint32
);
600 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
602 * Applies the 'equal' operator to the
603 * specified structure and value
604 * @param v1 left member of comparison
605 * @param v2 right member of comparison
606 * @return success/failure of operation
608 gboolean
lttv_apply_op_eq_ltttime(const gpointer v1
, LttvFieldValue v2
) {
609 LttTime
* r
= (LttTime
*) v1
;
610 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
615 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
617 * Applies the 'not equal' operator to the
618 * specified structure and value
619 * @param v1 left member of comparison
620 * @param v2 right member of comparison
621 * @return success/failure of operation
623 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
) {
624 guint64
* r
= (guint64
*) v1
;
625 return (*r
!= v2
.v_uint64
);
629 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
631 * Applies the 'not equal' operator to the
632 * specified structure and value
633 * @param v1 left member of comparison
634 * @param v2 right member of comparison
635 * @return success/failure of operation
637 gboolean
lttv_apply_op_ne_uint32(const gpointer v1
, LttvFieldValue v2
) {
638 guint32
* r
= (guint32
*) v1
;
639 return (*r
!= v2
.v_uint32
);
643 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
645 * Applies the 'not equal' operator to the
646 * specified structure and value
647 * @param v1 left member of comparison
648 * @param v2 right member of comparison
649 * @return success/failure of operation
651 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
) {
652 guint16
* r
= (guint16
*) v1
;
653 return (*r
!= v2
.v_uint16
);
657 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
659 * Applies the 'not equal' operator to the
660 * specified structure and value
661 * @param v1 left member of comparison
662 * @param v2 right member of comparison
663 * @return success/failure of operation
665 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
) {
666 double* r
= (double*) v1
;
667 return (*r
!= v2
.v_double
);
671 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
673 * Applies the 'not equal' operator to the
674 * specified structure and value
675 * @param v1 left member of comparison
676 * @param v2 right member of comparison
677 * @return success/failure of operation
679 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
) {
680 char* r
= (char*) v1
;
681 return (g_strcasecmp(r
,v2
.v_string
));
685 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
687 * Applies the 'not equal' operator to the
688 * specified structure and value
689 * @param v1 left member of comparison
690 * @param v2 right member of comparison
691 * @return success/failure of operation
693 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
) {
694 GQuark
* r
= (GQuark
*) v1
;
695 return (*r
!= v2
.v_uint32
);
700 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
702 * Applies the 'not equal' operator to the
703 * specified structure and value
704 * @param v1 left member of comparison
705 * @param v2 right member of comparison
706 * @return success/failure of operation
708 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
) {
709 LttTime
* r
= (LttTime
*) v1
;
710 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
715 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
717 * Applies the 'lower than' operator to the
718 * specified structure and value
719 * @param v1 left member of comparison
720 * @param v2 right member of comparison
721 * @return success/failure of operation
723 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
) {
724 guint64
* r
= (guint64
*) v1
;
725 return (*r
< v2
.v_uint64
);
729 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
731 * Applies the 'lower than' operator to the
732 * specified structure and value
733 * @param v1 left member of comparison
734 * @param v2 right member of comparison
735 * @return success/failure of operation
737 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
) {
738 guint32
* r
= (guint32
*) v1
;
739 return (*r
< v2
.v_uint32
);
743 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
745 * Applies the 'lower than' operator to the
746 * specified structure and value
747 * @param v1 left member of comparison
748 * @param v2 right member of comparison
749 * @return success/failure of operation
751 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
) {
752 guint16
* r
= (guint16
*) v1
;
753 return (*r
< v2
.v_uint16
);
757 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
759 * Applies the 'lower than' operator to the
760 * specified structure and value
761 * @param v1 left member of comparison
762 * @param v2 right member of comparison
763 * @return success/failure of operation
765 gboolean
lttv_apply_op_lt_double(const gpointer v1
, LttvFieldValue v2
) {
766 double* r
= (double*) v1
;
767 return (*r
< v2
.v_double
);
771 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
773 * Applies the 'lower than' operator to the
774 * specified structure and value
775 * @param v1 left member of comparison
776 * @param v2 right member of comparison
777 * @return success/failure of operation
779 gboolean
lttv_apply_op_lt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
780 LttTime
* r
= (LttTime
*) v1
;
781 // 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)));
782 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
787 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
789 * Applies the 'lower or equal' operator to the
790 * specified structure and value
791 * @param v1 left member of comparison
792 * @param v2 right member of comparison
793 * @return success/failure of operation
795 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
) {
796 guint64
* r
= (guint64
*) v1
;
797 return (*r
<= v2
.v_uint64
);
801 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
803 * Applies the 'lower or equal' operator to the
804 * specified structure and value
805 * @param v1 left member of comparison
806 * @param v2 right member of comparison
807 * @return success/failure of operation
809 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
) {
810 guint32
* r
= (guint32
*) v1
;
811 return (*r
<= v2
.v_uint32
);
815 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
817 * Applies the 'lower or equal' operator to the
818 * specified structure and value
819 * @param v1 left member of comparison
820 * @param v2 right member of comparison
821 * @return success/failure of operation
823 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
) {
824 guint16
* r
= (guint16
*) v1
;
825 return (*r
<= v2
.v_uint16
);
829 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
831 * Applies the 'lower or equal' operator to the
832 * specified structure and value
833 * @param v1 left member of comparison
834 * @param v2 right member of comparison
835 * @return success/failure of operation
837 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
) {
838 double* r
= (double*) v1
;
839 return (*r
<= v2
.v_double
);
843 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
845 * Applies the 'lower or equal' operator to the
846 * specified structure and value
847 * @param v1 left member of comparison
848 * @param v2 right member of comparison
849 * @return success/failure of operation
851 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
) {
852 LttTime
* r
= (LttTime
*) v1
;
853 // 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)));
854 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
859 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
861 * Applies the 'greater than' operator to the
862 * specified structure and value
863 * @param v1 left member of comparison
864 * @param v2 right member of comparison
865 * @return success/failure of operation
867 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
) {
868 guint64
* r
= (guint64
*) v1
;
869 return (*r
> v2
.v_uint64
);
873 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
875 * Applies the 'greater than' operator to the
876 * specified structure and value
877 * @param v1 left member of comparison
878 * @param v2 right member of comparison
879 * @return success/failure of operation
881 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
) {
882 guint32
* r
= (guint32
*) v1
;
883 return (*r
> v2
.v_uint32
);
887 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
889 * Applies the 'greater than' operator to the
890 * specified structure and value
891 * @param v1 left member of comparison
892 * @param v2 right member of comparison
893 * @return success/failure of operation
895 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
) {
896 guint16
* r
= (guint16
*) v1
;
897 return (*r
> v2
.v_uint16
);
901 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
903 * Applies the 'greater than' operator to the
904 * specified structure and value
905 * @param v1 left member of comparison
906 * @param v2 right member of comparison
907 * @return success/failure of operation
909 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
) {
910 double* r
= (double*) v1
;
911 return (*r
> v2
.v_double
);
915 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
917 * Applies the 'greater than' operator to the
918 * specified structure and value
919 * @param v1 left member of comparison
920 * @param v2 right member of comparison
921 * @return success/failure of operation
923 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
) {
924 LttTime
* r
= (LttTime
*) v1
;
925 // 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)));
926 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
931 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
933 * Applies the 'greater or equal' operator to the
934 * specified structure and value
935 * @param v1 left member of comparison
936 * @param v2 right member of comparison
937 * @return success/failure of operation
939 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
) {
940 guint64
* r
= (guint64
*) v1
;
941 return (*r
>= v2
.v_uint64
);
945 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
947 * Applies the 'greater or equal' operator to the
948 * specified structure and value
949 * @param v1 left member of comparison
950 * @param v2 right member of comparison
951 * @return success/failure of operation
953 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
) {
954 guint32
* r
= (guint32
*) v1
;
955 return (*r
>= v2
.v_uint32
);
959 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
961 * Applies the 'greater or equal' operator to the
962 * specified structure and value
963 * @param v1 left member of comparison
964 * @param v2 right member of comparison
965 * @return success/failure of operation
967 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
) {
968 guint16
* r
= (guint16
*) v1
;
969 return (*r
>= v2
.v_uint16
);
973 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
975 * Applies the 'greater or equal' operator to the
976 * specified structure and value
977 * @param v1 left member of comparison
978 * @param v2 right member of comparison
979 * @return success/failure of operation
981 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
) {
982 double* r
= (double*) v1
;
983 return (*r
>= v2
.v_double
);
987 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
989 * Applies the 'greater or equal' operator to the
990 * specified structure and value
991 * @param v1 left member of comparison
992 * @param v2 right member of comparison
993 * @return success/failure of operation
995 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
) {
996 LttTime
* r
= (LttTime
*) v1
;
997 // 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)));
998 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1004 * Makes a copy of the current filter tree
1005 * @param tree pointer to the current tree
1006 * @return new copy of the filter tree
1009 lttv_filter_tree_clone(const LttvFilterTree
* tree
) {
1011 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1013 newtree
->node
= tree
->node
;
1015 newtree
->left
= tree
->left
;
1016 if(newtree
->left
== LTTV_TREE_NODE
) {
1017 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1018 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1019 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1020 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1021 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1022 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1023 /* FIXME: special case for string copy ! */
1024 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1027 newtree
->right
= tree
->right
;
1028 if(newtree
->right
== LTTV_TREE_NODE
) {
1029 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1030 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1031 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1032 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1033 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1034 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1035 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1043 * Makes a copy of the current filter
1044 * @param filter pointer to the current filter
1045 * @return new copy of the filter
1048 lttv_filter_clone(const LttvFilter
* filter
) {
1050 if(!filter
) return NULL
;
1052 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1054 strcpy(newfilter
->expression
,filter
->expression
);
1056 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1064 * @fn LttvFilter* lttv_filter_new()
1066 * Creates a new LttvFilter
1067 * @return the current LttvFilter or NULL if error
1072 LttvFilter
* filter
= g_new(LttvFilter
,1);
1073 filter
->expression
= NULL
;
1074 filter
->head
= NULL
;
1081 * @fn gboolean lttv_filter_update(LttvFilter*)
1083 * Updates the current LttvFilter by building
1084 * its tree based upon the expression string
1085 * @param filter pointer to the current LttvFilter
1086 * @return Failure/Success of operation
1089 lttv_filter_update(LttvFilter
* filter
) {
1091 // g_print("filter::lttv_filter_new()\n"); /* debug */
1093 if(filter
->expression
== NULL
) return FALSE
;
1097 p_nesting
=0, /* parenthesis nesting value */
1102 *tree
= lttv_filter_tree_new(), /* main tree */
1103 *subtree
= NULL
, /* buffer for subtrees */
1104 *t1
, /* buffer #1 */
1105 *t2
, /* buffer #2 */
1106 *t3
; /* buffer #3 */
1110 * If the tree already exists,
1111 * destroy it and build a new one
1113 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1114 filter
->head
= NULL
; /* will be assigned at the end */
1118 * each element of the list
1119 * is a sub tree created
1120 * by the use of parenthesis in the
1121 * global expression. The final tree
1122 * will be the one left at the root of
1125 GPtrArray
*tree_stack
= g_ptr_array_new();
1126 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1128 /* temporary values */
1129 GString
*a_field_component
= g_string_new("");
1130 GPtrArray
*a_field_path
= g_ptr_array_new();
1132 /* simple expression buffer */
1133 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1136 * Parse entire expression and construct
1137 * the binary tree. There are two steps
1138 * in browsing that string
1139 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1140 * 2. finding simple expressions
1141 * - field path ( separated by dots )
1142 * - op ( >, <, =, >=, <=, !=)
1143 * - value ( integer, string ... )
1144 * To spare computing time, the whole
1145 * string is parsed in this loop for a
1146 * O(n) complexity order.
1148 * When encountering logical op &,|,^
1149 * 1. parse the last value if any
1150 * 2. create a new tree
1151 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1152 * 4. concatenate this tree with the current tree on top of the stack
1153 * When encountering math ops >,>=,<,<=,=,!=
1154 * 1. add to op to the simple expression
1155 * 2. concatenate last field component to field path
1156 * When encountering concatening ops .
1157 * 1. concatenate last field component to field path
1158 * When encountering opening parenthesis (,{,[
1159 * 1. create a new subtree on top of tree stack
1160 * When encountering closing parenthesis ),},]
1161 * 1. add the expression on right child of the current tree
1162 * 2. the subtree is completed, allocate a new subtree
1163 * 3. pop the tree value from the tree stack
1167 struct timeval starttime
;
1168 struct timeval endtime
;
1169 gettimeofday(&starttime
, NULL
);
1172 for(i
=0;i
<strlen(filter
->expression
);i
++) {
1174 // g_print("%c\n ",filter->expression[i]);
1176 switch(filter
->expression
[i
]) {
1182 /* get current tree in tree stack */
1183 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1185 /* get current node at absolute right */
1186 while(t1
->right
!= LTTV_TREE_IDLE
) {
1187 g_assert(t1
->right
== LTTV_TREE_NODE
);
1190 t2
= lttv_filter_tree_new();
1191 t2
->node
= LTTV_LOGICAL_AND
;
1192 t1
->right
= LTTV_TREE_NODE
;
1194 if(not) { /* add not operator to tree */
1195 t3
= lttv_filter_tree_new();
1196 t3
->node
= LTTV_LOGICAL_NOT
;
1197 t2
->left
= LTTV_TREE_NODE
;
1202 if(subtree
!= NULL
) { /* append subtree to current tree */
1203 t2
->left
= LTTV_TREE_NODE
;
1204 t2
->l_child
.t
= subtree
;
1206 } else { /* append a simple expression */
1207 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1208 a_field_component
= g_string_new("");
1209 t2
->left
= LTTV_TREE_LEAF
;
1210 t2
->l_child
.leaf
= a_simple_expression
;
1211 a_simple_expression
= lttv_simple_expression_new();
1217 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1218 while(t1
->right
!= LTTV_TREE_IDLE
) {
1219 g_assert(t1
->right
== LTTV_TREE_NODE
);
1222 t2
= lttv_filter_tree_new();
1223 t2
->node
= LTTV_LOGICAL_OR
;
1224 t1
->right
= LTTV_TREE_NODE
;
1226 if(not) { // add not operator to tree
1227 t3
= lttv_filter_tree_new();
1228 t3
->node
= LTTV_LOGICAL_NOT
;
1229 t2
->left
= LTTV_TREE_NODE
;
1234 if(subtree
!= NULL
) { /* append subtree to current tree */
1235 t2
->left
= LTTV_TREE_NODE
;
1236 t2
->l_child
.t
= subtree
;
1238 } else { /* append a simple expression */
1239 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1240 a_field_component
= g_string_new("");
1241 t2
->left
= LTTV_TREE_LEAF
;
1242 t2
->l_child
.leaf
= a_simple_expression
;
1243 a_simple_expression
= lttv_simple_expression_new();
1249 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1250 while(t1
->right
!= LTTV_TREE_IDLE
) {
1251 g_assert(t1
->right
== LTTV_TREE_NODE
);
1254 t2
= lttv_filter_tree_new();
1255 t2
->node
= LTTV_LOGICAL_XOR
;
1256 t1
->right
= LTTV_TREE_NODE
;
1258 if(not) { // add not operator to tree
1259 t3
= lttv_filter_tree_new();
1260 t3
->node
= LTTV_LOGICAL_NOT
;
1261 t2
->left
= LTTV_TREE_NODE
;
1266 if(subtree
!= NULL
) { /* append subtree to current tree */
1267 t2
->left
= LTTV_TREE_NODE
;
1268 t2
->l_child
.t
= subtree
;
1270 } else { /* append a simple expression */
1271 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1272 a_field_component
= g_string_new("");
1273 t2
->left
= LTTV_TREE_LEAF
;
1274 t2
->l_child
.leaf
= a_simple_expression
;
1275 a_simple_expression
= lttv_simple_expression_new();
1279 case '!': /* not, or not equal (math op) */
1281 if(filter
->expression
[i
+1] == '=') { /* != */
1282 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1283 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1284 a_field_component
= g_string_new("");
1285 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1292 case '(': /* start of parenthesis */
1296 p_nesting
++; /* incrementing parenthesis nesting value */
1297 t1
= lttv_filter_tree_new();
1298 if(not) { /* add not operator to tree */
1299 t3
= lttv_filter_tree_new();
1300 t3
->node
= LTTV_LOGICAL_NOT
;
1301 t1
->right
= LTTV_TREE_NODE
;
1305 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1308 case ')': /* end of parenthesis */
1312 p_nesting
--; /* decrementing parenthesis nesting value */
1313 if(p_nesting
<0 || tree_stack
->len
<2) {
1314 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1315 is not valid due to parenthesis incorrect use",filter
->expression
);
1319 /* there must at least be the root tree left in the array */
1320 g_assert(tree_stack
->len
>0);
1322 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1323 while(t1
->right
!= LTTV_TREE_IDLE
) {
1326 if(not) { // add not operator to tree
1328 t3
= lttv_filter_tree_new();
1329 t3
->node
= LTTV_LOGICAL_NOT
;
1330 t1
->right
= LTTV_TREE_NODE
;
1335 if(subtree
!= NULL
) { /* append subtree to current tree */
1336 t1
->right
= LTTV_TREE_NODE
;
1337 t1
->r_child
.t
= subtree
;
1338 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1339 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1340 } else { /* assign subtree as current tree */
1341 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1342 a_field_component
= g_string_new("");
1343 t1
->right
= LTTV_TREE_LEAF
;
1344 t1
->r_child
.leaf
= a_simple_expression
;
1345 a_simple_expression
= lttv_simple_expression_new();
1346 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1351 * mathematic operators
1353 case '<': /* lower, lower or equal */
1355 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1356 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1357 a_field_component
= g_string_new("");
1358 if(filter
->expression
[i
+1] == '=') { /* <= */
1360 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LE
);
1361 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_LT
);
1364 case '>': /* higher, higher or equal */
1366 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1367 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1368 a_field_component
= g_string_new("");
1369 if(filter
->expression
[i
+1] == '=') { /* >= */
1371 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GE
);
1372 } else lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_GT
);
1375 case '=': /* equal */
1377 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1378 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1379 a_field_component
= g_string_new("");
1380 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1384 * Field concatening caracter
1389 * divide field expression into elements
1390 * in a_field_path array.
1392 * A dot can also be present in double values
1394 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1395 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1396 a_field_component
= g_string_new("");
1399 case ' ': /* ignore */
1400 case '\n': /* ignore */
1402 default: /* concatening current string */
1403 g_string_append_c(a_field_component
,filter
->expression
[i
]);
1408 * Preliminary check to see
1409 * if tree was constructed correctly
1412 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1413 is not valid due to parenthesis incorrect use",filter
->expression
);
1417 if(tree_stack
->len
!= 1) /* only root tree should remain */
1421 * processing last element of expression
1423 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1424 while(t1
->right
!= LTTV_TREE_IDLE
) {
1425 g_assert(t1
->right
== LTTV_TREE_NODE
);
1428 if(not) { // add not operator to tree
1429 t3
= lttv_filter_tree_new();
1430 t3
->node
= LTTV_LOGICAL_NOT
;
1431 t1
->right
= LTTV_TREE_NODE
;
1436 if(subtree
!= NULL
) { /* add the subtree */
1437 t1
->right
= LTTV_TREE_NODE
;
1438 t1
->r_child
.t
= subtree
;
1440 } else { /* add a leaf */
1441 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1442 a_field_component
= NULL
;
1443 t1
->right
= LTTV_TREE_LEAF
;
1444 t1
->r_child
.leaf
= a_simple_expression
;
1445 a_simple_expression
= NULL
;
1449 /* free the pointer array */
1450 g_assert(a_field_path
->len
== 0);
1451 g_ptr_array_free(a_field_path
,TRUE
);
1453 /* free the tree stack -- but keep the root tree */
1454 filter
->head
= g_ptr_array_remove_index(tree_stack
,0);
1455 g_ptr_array_free(tree_stack
,TRUE
);
1457 /* free the field buffer if allocated */
1458 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1460 /* free the simple expression buffer if allocated */
1461 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1463 g_assert(filter
->head
!= NULL
); /* tree should exist */
1464 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1467 gettimeofday(&endtime
, NULL
);
1469 /* Calcul du temps de l'algorithme */
1470 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1471 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1472 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1473 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1477 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1478 lttv_print_tree(filter
->head
,0) ;
1479 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1487 * @fn void lttv_filter_destroy(LttvFilter*)
1489 * Destroy the current LttvFilter
1490 * @param filter pointer to the current LttvFilter
1493 lttv_filter_destroy(LttvFilter
* filter
) {
1497 if(filter
->expression
)
1498 g_free(filter
->expression
);
1500 lttv_filter_tree_destroy(filter
->head
);
1506 * @fn LttvFilterTree* lttv_filter_tree_new()
1508 * Assign a new tree for the current expression
1510 * @return pointer of LttvFilterTree
1513 lttv_filter_tree_new() {
1514 LttvFilterTree
* tree
;
1516 tree
= g_new(LttvFilterTree
,1);
1517 tree
->node
= 0; //g_new(lttv_expression,1);
1518 tree
->left
= LTTV_TREE_IDLE
;
1519 tree
->right
= LTTV_TREE_IDLE
;
1520 tree
->r_child
.t
= NULL
;
1521 tree
->l_child
.t
= NULL
;
1527 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1529 * Append a new expression to the expression
1530 * defined in the current filter
1531 * @param filter pointer to the current LttvFilter
1532 * @param expression string that must be appended
1533 * @return Success/Failure of operation
1536 lttv_filter_append_expression(LttvFilter
* filter
, const char *expression
) {
1538 if(expression
== NULL
) return FALSE
;
1539 if(filter
== NULL
) return FALSE
;
1541 GString
* s
= g_string_new("");
1542 if(filter
->expression
!= NULL
) {
1543 g_string_append(s
,filter
->expression
);
1544 g_string_append_c(s
,'&');
1546 g_string_append(s
,expression
);
1548 g_free(filter
->expression
);
1549 filter
->expression
= g_string_free(s
,FALSE
);
1551 /* TRUE if construction of tree proceeded without errors */
1552 return lttv_filter_update(filter
);
1557 * @fn void lttv_filter_clear_expression(LttvFilter*)
1559 * Clear the filter expression from the
1560 * current filter and sets its pointer to NULL
1561 * @param filter pointer to the current LttvFilter
1564 lttv_filter_clear_expression(LttvFilter
* filter
) {
1566 if(filter
->expression
!= NULL
) {
1567 g_free(filter
->expression
);
1568 filter
->expression
= NULL
;
1574 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1576 * Destroys the tree and his sub-trees
1577 * @param tree Tree which must be destroyed
1580 lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
1582 if(tree
== NULL
) return;
1584 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1585 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1587 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1588 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1590 // g_free(tree->node);
1595 * Global parsing function for the current
1597 * @param t pointer to the current LttvFilterTree
1598 * @param event current LttEvent, NULL if not used
1599 * @param tracefile current LttTracefile, NULL if not used
1600 * @param trace current LttTrace, NULL if not used
1601 * @param state current LttvProcessState, NULL if not used
1602 * @param context current LttvTracefileContext, NULL if not used
1603 * @return response of filter
1606 lttv_filter_tree_parse(
1607 const LttvFilterTree
* t
,
1608 const LttEvent
* event
,
1609 const LttTracefile
* tracefile
,
1610 const LttTrace
* trace
,
1611 const LttvProcessState
* state
,
1612 const LttvTracefileContext
* context
1617 * Each tree is parsed in inorder.
1618 * This way, it's possible to apply the left filter of the
1619 * tree, then decide whether or not the right branch should
1620 * be parsed depending on the linking logical operator
1622 * Each node consists in a
1623 * 1. logical operator
1624 * 2. left child ( node or simple expression )
1625 * 3. right child ( node or simple expression )
1627 * When the child is a simple expression, we must
1628 * before all determine if the expression refers to
1629 * a structure which is whithin observation ( not NULL ).
1630 * -If so, the expression is evaluated.
1631 * -If not, the result is set to TRUE since this particular
1632 * operation does not interfere with the lttv structure
1634 * The result of each simple expression will directly
1635 * affect the next branch. This way, depending on
1636 * the linking logical operator, the parser will decide
1637 * to explore or not the next branch.
1639 * -If result of left branch is 0 / FALSE
1640 * then don't explore right branch and return 0;
1641 * -If result of left branch is 1 / TRUE then explore
1643 * -If result of left branch is 1 / TRUE
1644 * then don't explore right branch and return 1;
1645 * -If result of left branch is 0 / FALSE then explore
1647 * -Result of left branch will not affect exploration of
1651 gboolean lresult
= FALSE
, rresult
= FALSE
;
1656 if(t
->left
== LTTV_TREE_NODE
) {
1657 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,tracefile
,trace
,state
,context
);
1659 else if(t
->left
== LTTV_TREE_LEAF
) {
1660 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1664 * Parse linking operator
1665 * make a cutoff if possible
1667 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
1668 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
1671 * Parse right branch
1673 if(t
->right
== LTTV_TREE_NODE
) {
1674 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,state
,context
);
1676 else if(t
->right
== LTTV_TREE_LEAF
) {
1677 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
1682 * Apply and return the
1683 * logical link between the
1687 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
1688 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
1689 case LTTV_LOGICAL_NOT
:
1690 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
1691 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
1692 case 0: return (rresult
);
1695 * This case should never be
1696 * parsed, if so, this subtree
1705 * This function parses a particular branch of the tree
1706 * @param se pointer to the current LttvSimpleExpression
1707 * @param event current LttEvent, NULL if not used
1708 * @param tracefile current LttTracefile, NULL if not used
1709 * @param trace current LttTrace, NULL if not used
1710 * @param state current LttvProcessState, NULL if not used
1711 * @param context current LttvTracefileContext, NULL if not used
1712 * @return response of filter
1715 lttv_filter_tree_parse_branch(
1716 const LttvSimpleExpression
* se
,
1717 const LttEvent
* event
,
1718 const LttTracefile
* tracefile
,
1719 const LttTrace
* trace
,
1720 const LttvProcessState
* state
,
1721 const LttvTracefileContext
* context
) {
1726 case LTTV_FILTER_TRACE_NAME
:
1727 if(trace
== NULL
) return TRUE
;
1729 char* trace_name
= ltt_trace_name(trace
);
1730 GQuark quark
= g_quark_from_string(trace_name
);
1731 return se
->op((gpointer
)&quark
,v
);
1734 case LTTV_FILTER_TRACEFILE_NAME
:
1735 if(tracefile
== NULL
) return TRUE
;
1737 char* tracefile_name
= ltt_tracefile_name(tracefile
);
1738 GQuark quark
= g_quark_from_string(tracefile_name
);
1739 return se
->op((gpointer
)&quark
,v
);
1742 case LTTV_FILTER_STATE_PID
:
1743 if(state
== NULL
) return TRUE
;
1744 else return se
->op((gpointer
)&state
->pid
,v
);
1746 case LTTV_FILTER_STATE_PPID
:
1747 if(state
== NULL
) return TRUE
;
1748 else return se
->op((gpointer
)&state
->ppid
,v
);
1750 case LTTV_FILTER_STATE_CT
:
1751 if(state
== NULL
) return TRUE
;
1753 return se
->op((gpointer
)&state
->creation_time
,v
);
1756 case LTTV_FILTER_STATE_IT
:
1757 if(state
== NULL
) return TRUE
;
1759 return se
->op((gpointer
)&state
->insertion_time
,v
);
1762 case LTTV_FILTER_STATE_P_NAME
:
1764 * All 'unnamed' for the moment
1766 if(state
== NULL
) return TRUE
;
1768 GQuark quark
= state
->name
;
1769 return se
->op((gpointer
)&quark
,v
);
1772 case LTTV_FILTER_STATE_EX_MODE
:
1773 if(state
== NULL
) return TRUE
;
1774 else return se
->op((gpointer
)&state
->state
->t
,v
);
1776 case LTTV_FILTER_STATE_EX_SUBMODE
:
1777 if(state
== NULL
) return TRUE
;
1778 else return se
->op((gpointer
)&state
->state
->n
,v
);
1780 case LTTV_FILTER_STATE_P_STATUS
:
1781 if(state
== NULL
) return TRUE
;
1782 else return se
->op((gpointer
)&state
->state
->s
,v
);
1784 case LTTV_FILTER_STATE_CPU
:
1785 if(context
== NULL
) return TRUE
;
1787 /* FIXME: not sure of that one */
1788 GQuark quark
= ((LttvTracefileState
*)context
)->cpu_name
;
1789 return se
->op((gpointer
)&quark
,v
);
1792 case LTTV_FILTER_EVENT_NAME
:
1793 if(event
== NULL
) return TRUE
;
1796 et
= ltt_event_eventtype(event
);
1797 char* event_name
= ltt_eventtype_name(et
);
1798 GQuark quark
= g_quark_from_string(event_name
);
1799 return se
->op((gpointer
)&quark
,v
);
1803 case LTTV_FILTER_EVENT_CATEGORY
:
1805 * TODO: Not yet implemented
1809 case LTTV_FILTER_EVENT_TIME
:
1810 if(event
== NULL
) return TRUE
;
1812 LttTime time
= ltt_event_time(event
);
1813 return se
->op((gpointer
)&time
,v
);
1816 case LTTV_FILTER_EVENT_TSC
:
1817 if(event
== NULL
) return TRUE
;
1819 LttCycleCount count
= ltt_event_cycle_count(event
);
1820 return se
->op((gpointer
)&count
,v
);
1823 case LTTV_FILTER_EVENT_FIELD
:
1825 * TODO: Use the offset to
1826 * find the dynamic field
1827 * in the event struct
1832 * This case should never be
1833 * parsed, if so, the whole
1834 * filtering is cancelled
1836 g_warning("Error while parsing the filter tree");
1840 /* should never get here */
1848 * Debug function. Prints tree memory allocation.
1849 * @param t the pointer to the current LttvFilterTree
1852 lttv_print_tree(const LttvFilterTree
* t
, const int count
) {
1854 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
1855 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
1856 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
1858 g_print("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
1859 g_print("|-> left branch %p is a %s\n",t
->l_child
.t
,(t
->left
==LTTV_TREE_NODE
)?"NODE":((t
->left
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1860 if(t
->left
== LTTV_TREE_LEAF
) {
1861 g_print("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
1862 g_print("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
1863 g_print("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
1865 g_print("|-> right branch %p is a %s\n",t
->r_child
.t
,(t
->right
==LTTV_TREE_NODE
)?"NODE":((t
->right
==LTTV_TREE_LEAF
)?"LEAF":"IDLE"));
1866 if(t
->right
== LTTV_TREE_LEAF
) {
1867 g_print("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
1868 g_print("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
1869 g_print("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
1872 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
1873 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
1877 * @fn static void module_init()
1879 * Initializes the filter module and specific values
1881 static void module_init()
1887 * Destroys the filter module and specific values
1889 static void module_destroy()
1895 LTTV_MODULE("filter", "Filters traceset and events", \
1896 "Filters traceset and events specifically to user input", \
1897 module_init
, module_destroy
)