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 * |->execution_mode (LttvExecutionMode)
57 * |->execution_submode (LttvExecutionSubmode)
58 * |->process_status (LttvProcessStatus)
65 * - refine switch of expression in multiple uses functions
66 * - remove the idle expressions in the tree
79 #include <lttv/lttv.h>
80 #include <lttv/filter.h>
81 #include <ltt/trace.h>
86 * @fn LttvSimpleExpression* lttv_simple_expression_new()
88 * Constructor for LttvSimpleExpression
89 * @return pointer to new LttvSimpleExpression
91 LttvSimpleExpression
* lttv_simple_expression_new()
94 LttvSimpleExpression
* se
= g_new(LttvSimpleExpression
,1);
96 se
->field
= LTTV_FILTER_UNDEFINED
;
104 * Keeps the array order.
106 static inline gpointer
ltt_g_ptr_array_remove_index_slow(GPtrArray
*fp
,
115 ptr
= g_ptr_array_index(fp
, index
);
116 for (i
= index
; i
< fp
->len
- 1; i
++) {
117 g_ptr_array_index(fp
, i
) = g_ptr_array_index(fp
, i
+ 1);
119 g_ptr_array_remove_index(fp
, fp
->len
- 1);
124 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
126 * Parse through filtering field hierarchy as specified
127 * by user. This function compares each value to
128 * predetermined quarks
129 * @param fp The field path list
130 * @param se current simple expression
131 * @return success/failure of operation
133 gboolean
lttv_simple_expression_assign_field(GPtrArray
* fp
,
134 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_ascii_strcasecmp(f
->str
,"trace") ) {
163 g_string_free(f
,TRUE
);
164 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
165 if(!g_ascii_strcasecmp(f
->str
,"name")) {
166 se
->field
= LTTV_FILTER_TRACE_NAME
;
168 } else if(!g_ascii_strcasecmp(f
->str
,"traceset") ) {
170 * FIXME: not yet implemented !
172 } else if(!g_ascii_strcasecmp(f
->str
,"tracefile")
173 || !g_ascii_strcasecmp(f
->str
,"channel") ) {
179 g_string_free(f
,TRUE
);
180 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
181 if(!g_ascii_strcasecmp(f
->str
,"name")) {
182 se
->field
= LTTV_FILTER_TRACEFILE_NAME
;
184 } else if(!g_ascii_strcasecmp(f
->str
,"state") ) {
189 * state.creation_time
190 * state.insertion_time
192 * state.execution_mode
193 * state.execution_submode
194 * state.process_status
197 g_string_free(f
,TRUE
);
198 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
199 if(!g_ascii_strcasecmp(f
->str
,"pid") ) {
200 se
->field
= LTTV_FILTER_STATE_PID
;
202 else if(!g_ascii_strcasecmp(f
->str
,"ppid") ) {
203 se
->field
= LTTV_FILTER_STATE_PPID
;
205 else if(!g_ascii_strcasecmp(f
->str
,"creation_time") ) {
206 se
->field
= LTTV_FILTER_STATE_CT
;
208 else if(!g_ascii_strcasecmp(f
->str
,"insertion_time") ) {
209 se
->field
= LTTV_FILTER_STATE_IT
;
211 else if(!g_ascii_strcasecmp(f
->str
,"process_name") ) {
212 se
->field
= LTTV_FILTER_STATE_P_NAME
;
214 else if(!g_ascii_strcasecmp(f
->str
,"execution_mode") ) {
215 se
->field
= LTTV_FILTER_STATE_EX_MODE
;
217 else if(!g_ascii_strcasecmp(f
->str
,"execution_submode") ) {
218 se
->field
= LTTV_FILTER_STATE_EX_SUBMODE
;
220 else if(!g_ascii_strcasecmp(f
->str
,"process_status") ) {
221 se
->field
= LTTV_FILTER_STATE_P_STATUS
;
223 else if(!g_ascii_strcasecmp(f
->str
,"cpu") ) {
224 se
->field
= LTTV_FILTER_STATE_CPU
;
226 } else if(!g_ascii_strcasecmp(f
->str
,"event") ) {
237 g_string_free(f
,TRUE
);
238 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
240 if(!g_ascii_strcasecmp(f
->str
,"name") ) {
241 se
->field
= LTTV_FILTER_EVENT_NAME
;
243 else if(!g_ascii_strcasecmp(f
->str
,"subname") ) {
244 se
->field
= LTTV_FILTER_EVENT_SUBNAME
;
246 else if(!g_ascii_strcasecmp(f
->str
,"category") ) {
248 * FIXME: Category not yet functional in lttv
250 se
->field
= LTTV_FILTER_EVENT_CATEGORY
;
252 else if(!g_ascii_strcasecmp(f
->str
,"time") ) {
253 se
->field
= LTTV_FILTER_EVENT_TIME
;
255 else if(!g_ascii_strcasecmp(f
->str
,"tsc") ) {
256 se
->field
= LTTV_FILTER_EVENT_TSC
;
258 else if(!g_ascii_strcasecmp(f
->str
,"target_pid") ) {
259 se
->field
= LTTV_FILTER_EVENT_TARGET_PID
;
261 else if(!g_ascii_strcasecmp(f
->str
,"field") ) {
262 se
->field
= LTTV_FILTER_EVENT_FIELD
;
263 g_string_free(f
,TRUE
);
264 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
267 //g_string_free(f,TRUE);
268 //f=ltt_g_ptr_array_remove_index_slow(fp,0);
269 g_warning("Unknown event filter subtype %s", f
->str
);
272 g_string_free(f
,TRUE
);
273 f
=ltt_g_ptr_array_remove_index_slow(fp
,0);
275 g_warning("Unrecognized field in filter string");
278 /* free memory for last string */
279 g_string_free(f
,TRUE
);
281 /* array should be empty */
282 g_assert(fp
->len
== 0);
284 if(se
->field
== LTTV_FILTER_UNDEFINED
) {
285 g_warning("The specified field was not recognized !");
292 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
294 * Sets the function pointer for the current
296 * @param se current simple expression
297 * @param op current operator
298 * @return success/failure of operation
300 gboolean
lttv_simple_expression_assign_operator(LttvSimpleExpression
* se
,
308 case LTTV_FILTER_TRACE_NAME
:
309 case LTTV_FILTER_TRACEFILE_NAME
:
310 case LTTV_FILTER_STATE_P_NAME
:
311 case LTTV_FILTER_EVENT_SUBNAME
:
312 case LTTV_FILTER_STATE_EX_MODE
:
313 case LTTV_FILTER_STATE_EX_SUBMODE
:
314 case LTTV_FILTER_STATE_P_STATUS
:
317 se
->op
= lttv_apply_op_eq_quark
;
320 se
->op
= lttv_apply_op_ne_quark
;
323 g_warning("Error encountered in operator assignment = or != expected");
330 case LTTV_FILTER_EVENT_NAME
:
333 se
->op
= lttv_apply_op_eq_quarks
;
336 se
->op
= lttv_apply_op_ne_quarks
;
339 g_warning("Error encountered in operator assignment = or != expected");
346 case LTTV_FILTER_EVENT_TSC
:
349 se
->op
= lttv_apply_op_eq_uint64
;
352 se
->op
= lttv_apply_op_ne_uint64
;
355 se
->op
= lttv_apply_op_lt_uint64
;
358 se
->op
= lttv_apply_op_le_uint64
;
361 se
->op
= lttv_apply_op_gt_uint64
;
364 se
->op
= lttv_apply_op_ge_uint64
;
367 g_warning("Error encountered in operator assignment");
374 case LTTV_FILTER_STATE_CPU
:
375 case LTTV_FILTER_STATE_PID
:
376 case LTTV_FILTER_STATE_PPID
:
377 case LTTV_FILTER_EVENT_TARGET_PID
:
380 se
->op
= lttv_apply_op_eq_uint
;
383 se
->op
= lttv_apply_op_ne_uint
;
386 se
->op
= lttv_apply_op_lt_uint
;
389 se
->op
= lttv_apply_op_le_uint
;
392 se
->op
= lttv_apply_op_gt_uint
;
395 se
->op
= lttv_apply_op_ge_uint
;
398 g_warning("Error encountered in operator assignment");
405 * Entered as string, converted to enum
407 * can only be compared with 'equal' or 'not equal' operators
409 * unsigned int of 16 bits are used here since enums
410 * should not go over 2^16-1 values
414 // case LTTV_FIELD_EQ:
415 // se->op = lttv_apply_op_eq_uint16;
417 // case LTTV_FIELD_NE:
418 // se->op = lttv_apply_op_ne_uint16;
421 // g_warning("Error encountered in operator assignment = or != expected");
428 case LTTV_FILTER_STATE_CT
:
429 case LTTV_FILTER_STATE_IT
:
430 case LTTV_FILTER_EVENT_TIME
:
433 se
->op
= lttv_apply_op_eq_ltttime
;
436 se
->op
= lttv_apply_op_ne_ltttime
;
439 se
->op
= lttv_apply_op_lt_ltttime
;
442 se
->op
= lttv_apply_op_le_ltttime
;
445 se
->op
= lttv_apply_op_gt_ltttime
;
448 se
->op
= lttv_apply_op_ge_ltttime
;
451 g_warning("Error encountered in operator assignment");
456 g_warning("Error encountered in operator assignation ! Field type:%i",se
->field
);
465 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
467 * Assign the value field to the current LttvSimpleExpression
468 * @param se pointer to the current LttvSimpleExpression
469 * @param value string value for simple expression
471 gboolean
lttv_simple_expression_assign_value(LttvSimpleExpression
* se
,
476 gboolean is_double
= FALSE
;
477 LttTime t
= ltt_time_zero
;
484 * entered as strings, converted to Quarks
486 case LTTV_FILTER_TRACE_NAME
:
487 case LTTV_FILTER_TRACEFILE_NAME
:
488 case LTTV_FILTER_STATE_P_NAME
:
489 case LTTV_FILTER_EVENT_SUBNAME
:
490 case LTTV_FILTER_STATE_EX_MODE
:
491 case LTTV_FILTER_STATE_EX_SUBMODE
:
492 case LTTV_FILTER_STATE_P_STATUS
:
493 // se->value.v_string = value;
494 se
->value
.v_quark
= g_quark_from_string(value
);
500 case LTTV_FILTER_EVENT_NAME
:
503 char *end
= strchr(value
, '.');
507 se
->value
.v_quarks
.q
[0] = g_quark_from_string(value
);
508 se
->value
.v_quarks
.q
[1] = g_quark_from_string(end
);
510 se
->value
.v_quarks
.q
[0] = (GQuark
)0;
511 se
->value
.v_quarks
.q
[1] = g_quark_from_string(value
);
517 * integer -- supposed to be uint64
519 case LTTV_FILTER_EVENT_TSC
:
520 se
->value
.v_uint64
= atoi(value
);
526 case LTTV_FILTER_STATE_PID
:
527 case LTTV_FILTER_STATE_PPID
:
528 case LTTV_FILTER_STATE_CPU
:
529 case LTTV_FILTER_EVENT_TARGET_PID
:
530 se
->value
.v_uint
= atoi(value
);
536 case LTTV_FILTER_STATE_CT
:
537 case LTTV_FILTER_STATE_IT
:
538 case LTTV_FILTER_EVENT_TIME
:
539 //se->value.v_double = atof(value);
541 * parsing logic could be optimised,
542 * but as for now, simpler this way
544 v
= g_string_new("");
545 string_len
= strlen(value
);
546 for(i
=0;i
<string_len
;i
++) {
547 if(value
[i
] == '.') {
548 /* cannot specify number with more than one '.' */
549 if(is_double
) return FALSE
;
550 else is_double
= TRUE
;
551 t
.tv_sec
= atoi(v
->str
);
552 g_string_free(v
,TRUE
);
553 v
= g_string_new("");
554 } else v
= g_string_append_c(v
,value
[i
]);
556 /* number can be integer or double */
557 if(is_double
) t
.tv_nsec
= atoi(v
->str
);
559 t
.tv_sec
= atoi(v
->str
);
563 g_string_free(v
,TRUE
);
565 se
->value
.v_ltttime
= t
;
569 g_warning("Error encountered in value assignation ! Field type = %i",se
->field
);
579 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
581 * Disallocate memory for the current
583 * @param se pointer to the current LttvSimpleExpression
585 void lttv_simple_expression_destroy(LttvSimpleExpression
* se
)
588 // g_free(se->value);
589 // switch(se->field) {
590 // case LTTV_FILTER_TRACE_NAME:
591 // case LTTV_FILTER_TRACEFILE_NAME:
592 // case LTTV_FILTER_STATE_P_NAME:
593 // case LTTV_FILTER_EVENT_NAME:
594 // g_free(se->value.v_string);
602 * @fn gint lttv_struct_type(gint)
604 * Finds the structure type depending
605 * on the fields in parameters
606 * @params ft Field of the current structure
607 * @return LttvStructType enum or -1 for error
609 gint
lttv_struct_type(gint ft
)
613 case LTTV_FILTER_TRACE_NAME
:
614 return LTTV_FILTER_TRACE
;
616 case LTTV_FILTER_TRACEFILE_NAME
:
617 return LTTV_FILTER_TRACEFILE
;
619 case LTTV_FILTER_STATE_PID
:
620 case LTTV_FILTER_STATE_PPID
:
621 case LTTV_FILTER_STATE_CT
:
622 case LTTV_FILTER_STATE_IT
:
623 case LTTV_FILTER_STATE_P_NAME
:
624 case LTTV_FILTER_STATE_EX_MODE
:
625 case LTTV_FILTER_STATE_EX_SUBMODE
:
626 case LTTV_FILTER_STATE_P_STATUS
:
627 case LTTV_FILTER_STATE_CPU
:
628 return LTTV_FILTER_STATE
;
630 case LTTV_FILTER_EVENT_NAME
:
631 case LTTV_FILTER_EVENT_SUBNAME
:
632 case LTTV_FILTER_EVENT_CATEGORY
:
633 case LTTV_FILTER_EVENT_TIME
:
634 case LTTV_FILTER_EVENT_TSC
:
635 case LTTV_FILTER_EVENT_TARGET_PID
:
636 case LTTV_FILTER_EVENT_FIELD
:
637 return LTTV_FILTER_EVENT
;
645 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
647 * Applies the 'equal' operator to the
648 * specified structure and value
649 * @param v1 left member of comparison
650 * @param v2 right member of comparison
651 * @return success/failure of operation
653 gboolean
lttv_apply_op_eq_uint(const gpointer v1
, LttvFieldValue v2
)
655 guint
* r
= (guint
*) v1
;
656 return (*r
== v2
.v_uint
);
660 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
662 * Applies the 'equal' operator to the
663 * specified structure and value
664 * @param v1 left member of comparison
665 * @param v2 right member of comparison
666 * @return success/failure of operation
668 gboolean
lttv_apply_op_eq_uint64(const gpointer v1
, LttvFieldValue v2
)
670 guint64
* r
= (guint64
*) v1
;
671 return (*r
== v2
.v_uint64
);
675 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
677 * Applies the 'equal' operator to the
678 * specified structure and value
679 * @param v1 left member of comparison
680 * @param v2 right member of comparison
681 * @return success/failure of operation
683 gboolean
lttv_apply_op_eq_uint32(const gpointer v1
, LttvFieldValue v2
)
685 guint32
* r
= (guint32
*) v1
;
686 return (*r
== v2
.v_uint32
);
690 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
692 * Applies the 'equal' operator to the
693 * specified structure and value
694 * @param v1 left member of comparison
695 * @param v2 right member of comparison
696 * @return success/failure of operation
698 gboolean
lttv_apply_op_eq_uint16(const gpointer v1
, LttvFieldValue v2
)
700 guint16
* r
= (guint16
*) v1
;
701 return (*r
== v2
.v_uint16
);
705 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
707 * Applies the 'equal' operator to the
708 * specified structure and value
709 * @param v1 left member of comparison
710 * @param v2 right member of comparison
711 * @return success/failure of operation
713 gboolean
lttv_apply_op_eq_double(const gpointer v1
, LttvFieldValue v2
)
715 double* r
= (double*) v1
;
716 return (*r
== v2
.v_double
);
720 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
722 * Applies the 'equal' operator to the
723 * specified structure and value
724 * @param v1 left member of comparison
725 * @param v2 right member of comparison
726 * @return success/failure of operation
728 gboolean
lttv_apply_op_eq_string(const gpointer v1
, LttvFieldValue v2
)
730 char* r
= (char*) v1
;
731 return (!g_ascii_strcasecmp(r
,v2
.v_string
));
735 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
737 * Applies the 'equal' operator to the
738 * specified structure and value
739 * @param v1 left member of comparison
740 * @param v2 right member of comparison
741 * @return success/failure of operation
743 gboolean
lttv_apply_op_eq_quark(const gpointer v1
, LttvFieldValue v2
)
745 GQuark
* r
= (GQuark
*) v1
;
746 return (*r
== v2
.v_quark
);
750 * @fn gboolean lttv_apply_op_eq_quarks(gpointer,LttvFieldValue)
752 * Applies the 'equal' operator to the
753 * specified structure and value
754 * @param v1 left member of comparison
755 * @param v2 right member of comparison
756 * @return success/failure of operation
758 gboolean
lttv_apply_op_eq_quarks(const gpointer v1
, LttvFieldValue v2
)
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
)
780 LttTime
* r
= (LttTime
*) v1
;
781 return ltt_time_compare(*r
, v2
.v_ltttime
)==0?1:0;
785 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
787 * Applies the 'not equal' operator to the
788 * specified structure and value
789 * @param v1 left member of comparison
790 * @param v2 right member of comparison
791 * @return success/failure of operation
793 gboolean
lttv_apply_op_ne_uint(const gpointer v1
, LttvFieldValue v2
)
795 guint
* r
= (guint
*) v1
;
796 return (*r
!= v2
.v_uint
);
800 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
802 * Applies the 'not equal' operator to the
803 * specified structure and value
804 * @param v1 left member of comparison
805 * @param v2 right member of comparison
806 * @return success/failure of operation
808 gboolean
lttv_apply_op_ne_uint64(const gpointer v1
, LttvFieldValue v2
)
810 guint64
* r
= (guint64
*) v1
;
811 return (*r
!= v2
.v_uint64
);
815 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
817 * Applies the 'not 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_ne_uint32(const gpointer v1
, LttvFieldValue v2
)
825 guint32
* r
= (guint32
*) v1
;
826 return (*r
!= v2
.v_uint32
);
830 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
832 * Applies the 'not equal' operator to the
833 * specified structure and value
834 * @param v1 left member of comparison
835 * @param v2 right member of comparison
836 * @return success/failure of operation
838 gboolean
lttv_apply_op_ne_uint16(const gpointer v1
, LttvFieldValue v2
)
840 guint16
* r
= (guint16
*) v1
;
841 return (*r
!= v2
.v_uint16
);
845 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
847 * Applies the 'not equal' operator to the
848 * specified structure and value
849 * @param v1 left member of comparison
850 * @param v2 right member of comparison
851 * @return success/failure of operation
853 gboolean
lttv_apply_op_ne_double(const gpointer v1
, LttvFieldValue v2
)
855 double* r
= (double*) v1
;
856 return (*r
!= v2
.v_double
);
860 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
862 * Applies the 'not equal' operator to the
863 * specified structure and value
864 * @param v1 left member of comparison
865 * @param v2 right member of comparison
866 * @return success/failure of operation
868 gboolean
lttv_apply_op_ne_string(const gpointer v1
, LttvFieldValue v2
)
870 char* r
= (char*) v1
;
871 return (g_ascii_strcasecmp(r
,v2
.v_string
));
875 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
877 * Applies the 'not equal' operator to the
878 * specified structure and value
879 * @param v1 left member of comparison
880 * @param v2 right member of comparison
881 * @return success/failure of operation
883 gboolean
lttv_apply_op_ne_quark(const gpointer v1
, LttvFieldValue v2
)
885 GQuark
* r
= (GQuark
*) v1
;
886 return (*r
!= v2
.v_quark
);
890 * @fn gboolean lttv_apply_op_ne_quarks(gpointer,LttvFieldValue)
892 * Applies the 'equal' operator to the
893 * specified structure and value
894 * @param v1 left member of comparison
895 * @param v2 right member of comparison
896 * @return success/failure of operation
898 gboolean
lttv_apply_op_ne_quarks(const gpointer v1
, LttvFieldValue v2
)
900 GQuark
*r1
= (GQuark
*) v1
;
902 if ((*r1
== (GQuark
)0 || *r1
== v2
.v_quarks
.q
[0]) && *r2
== v2
.v_quarks
.q
[1])
909 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
911 * Applies the 'not equal' operator to the
912 * specified structure and value
913 * @param v1 left member of comparison
914 * @param v2 right member of comparison
915 * @return success/failure of operation
917 gboolean
lttv_apply_op_ne_ltttime(const gpointer v1
, LttvFieldValue v2
)
919 LttTime
* r
= (LttTime
*) v1
;
920 return ltt_time_compare(*r
, v2
.v_ltttime
)!=0?1:0;
924 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
926 * Applies the 'lower than' operator to the
927 * specified structure and value
928 * @param v1 left member of comparison
929 * @param v2 right member of comparison
930 * @return success/failure of operation
932 gboolean
lttv_apply_op_lt_uint(const gpointer v1
, LttvFieldValue v2
)
934 guint
* r
= (guint
*) v1
;
935 return (*r
< v2
.v_uint
);
939 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
941 * Applies the 'lower than' operator to the
942 * specified structure and value
943 * @param v1 left member of comparison
944 * @param v2 right member of comparison
945 * @return success/failure of operation
947 gboolean
lttv_apply_op_lt_uint64(const gpointer v1
, LttvFieldValue v2
)
949 guint64
* r
= (guint64
*) v1
;
950 return (*r
< v2
.v_uint64
);
954 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
956 * Applies the 'lower than' operator to the
957 * specified structure and value
958 * @param v1 left member of comparison
959 * @param v2 right member of comparison
960 * @return success/failure of operation
962 gboolean
lttv_apply_op_lt_uint32(const gpointer v1
, LttvFieldValue v2
)
964 guint32
* r
= (guint32
*) v1
;
965 return (*r
< v2
.v_uint32
);
969 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
971 * Applies the 'lower than' operator to the
972 * specified structure and value
973 * @param v1 left member of comparison
974 * @param v2 right member of comparison
975 * @return success/failure of operation
977 gboolean
lttv_apply_op_lt_uint16(const gpointer v1
, LttvFieldValue v2
)
979 guint16
* r
= (guint16
*) v1
;
980 return (*r
< v2
.v_uint16
);
984 * @fn gboolean lttv_apply_op_lt_double(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_double(const gpointer v1
, LttvFieldValue v2
)
994 double* r
= (double*) v1
;
995 return (*r
< v2
.v_double
);
999 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
1001 * Applies the 'lower than' 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_lt_ltttime(const gpointer v1
, LttvFieldValue v2
)
1009 LttTime
* r
= (LttTime
*) v1
;
1010 // 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)));
1011 return ltt_time_compare(*r
, v2
.v_ltttime
)==-1?1:0;
1015 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
1017 * Applies the 'lower or equal' operator to the
1018 * specified structure and value
1019 * @param v1 left member of comparison
1020 * @param v2 right member of comparison
1021 * @return success/failure of operation
1023 gboolean
lttv_apply_op_le_uint(const gpointer v1
, LttvFieldValue v2
)
1025 guint
* r
= (guint
*) v1
;
1026 return (*r
<= v2
.v_uint
);
1030 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
1032 * Applies the 'lower or equal' operator to the
1033 * specified structure and value
1034 * @param v1 left member of comparison
1035 * @param v2 right member of comparison
1036 * @return success/failure of operation
1038 gboolean
lttv_apply_op_le_uint64(const gpointer v1
, LttvFieldValue v2
)
1040 guint64
* r
= (guint64
*) v1
;
1041 return (*r
<= v2
.v_uint64
);
1045 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
1047 * Applies the 'lower or equal' operator to the
1048 * specified structure and value
1049 * @param v1 left member of comparison
1050 * @param v2 right member of comparison
1051 * @return success/failure of operation
1053 gboolean
lttv_apply_op_le_uint32(const gpointer v1
, LttvFieldValue v2
)
1055 guint32
* r
= (guint32
*) v1
;
1056 return (*r
<= v2
.v_uint32
);
1060 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
1062 * Applies the 'lower or equal' operator to the
1063 * specified structure and value
1064 * @param v1 left member of comparison
1065 * @param v2 right member of comparison
1066 * @return success/failure of operation
1068 gboolean
lttv_apply_op_le_uint16(const gpointer v1
, LttvFieldValue v2
)
1070 guint16
* r
= (guint16
*) v1
;
1071 return (*r
<= v2
.v_uint16
);
1075 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
1077 * Applies the 'lower or equal' operator to the
1078 * specified structure and value
1079 * @param v1 left member of comparison
1080 * @param v2 right member of comparison
1081 * @return success/failure of operation
1083 gboolean
lttv_apply_op_le_double(const gpointer v1
, LttvFieldValue v2
)
1085 double* r
= (double*) v1
;
1086 return (*r
<= v2
.v_double
);
1090 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
1092 * Applies the 'lower or equal' operator to the
1093 * specified structure and value
1094 * @param v1 left member of comparison
1095 * @param v2 right member of comparison
1096 * @return success/failure of operation
1098 gboolean
lttv_apply_op_le_ltttime(const gpointer v1
, LttvFieldValue v2
)
1100 LttTime
* r
= (LttTime
*) v1
;
1101 // 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)));
1102 return ltt_time_compare(*r
, v2
.v_ltttime
)<1?1:0;
1107 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
1109 * Applies the 'greater than' operator to the
1110 * specified structure and value
1111 * @param v1 left member of comparison
1112 * @param v2 right member of comparison
1113 * @return success/failure of operation
1115 gboolean
lttv_apply_op_gt_uint(const gpointer v1
, LttvFieldValue v2
)
1117 guint
* r
= (guint
*) v1
;
1118 return (*r
> v2
.v_uint
);
1122 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
1124 * Applies the 'greater than' operator to the
1125 * specified structure and value
1126 * @param v1 left member of comparison
1127 * @param v2 right member of comparison
1128 * @return success/failure of operation
1130 gboolean
lttv_apply_op_gt_uint64(const gpointer v1
, LttvFieldValue v2
)
1132 guint64
* r
= (guint64
*) v1
;
1133 return (*r
> v2
.v_uint64
);
1137 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
1139 * Applies the 'greater than' operator to the
1140 * specified structure and value
1141 * @param v1 left member of comparison
1142 * @param v2 right member of comparison
1143 * @return success/failure of operation
1145 gboolean
lttv_apply_op_gt_uint32(const gpointer v1
, LttvFieldValue v2
)
1147 guint32
* r
= (guint32
*) v1
;
1148 return (*r
> v2
.v_uint32
);
1152 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
1154 * Applies the 'greater than' operator to the
1155 * specified structure and value
1156 * @param v1 left member of comparison
1157 * @param v2 right member of comparison
1158 * @return success/failure of operation
1160 gboolean
lttv_apply_op_gt_uint16(const gpointer v1
, LttvFieldValue v2
)
1162 guint16
* r
= (guint16
*) v1
;
1163 return (*r
> v2
.v_uint16
);
1167 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1169 * Applies the 'greater than' operator to the
1170 * specified structure and value
1171 * @param v1 left member of comparison
1172 * @param v2 right member of comparison
1173 * @return success/failure of operation
1175 gboolean
lttv_apply_op_gt_double(const gpointer v1
, LttvFieldValue v2
)
1177 double* r
= (double*) v1
;
1178 return (*r
> v2
.v_double
);
1182 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1184 * Applies the 'greater than' operator to the
1185 * specified structure and value
1186 * @param v1 left member of comparison
1187 * @param v2 right member of comparison
1188 * @return success/failure of operation
1190 gboolean
lttv_apply_op_gt_ltttime(const gpointer v1
, LttvFieldValue v2
)
1192 LttTime
* r
= (LttTime
*) v1
;
1193 // 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)));
1194 return ltt_time_compare(*r
, v2
.v_ltttime
)==1?1:0;
1198 * @fn gboolean lttv_apply_op_ge_uint(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_uint(const gpointer v1
, LttvFieldValue v2
)
1208 guint
* r
= (guint
*) v1
;
1209 return (*r
>= v2
.v_uint
);
1213 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1215 * Applies the 'greater or equal' operator to the
1216 * specified structure and value
1217 * @param v1 left member of comparison
1218 * @param v2 right member of comparison
1219 * @return success/failure of operation
1221 gboolean
lttv_apply_op_ge_uint64(const gpointer v1
, LttvFieldValue v2
)
1223 guint64
* r
= (guint64
*) v1
;
1224 return (*r
>= v2
.v_uint64
);
1228 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1230 * Applies the 'greater or equal' operator to the
1231 * specified structure and value
1232 * @param v1 left member of comparison
1233 * @param v2 right member of comparison
1234 * @return success/failure of operation
1236 gboolean
lttv_apply_op_ge_uint32(const gpointer v1
, LttvFieldValue v2
)
1238 guint32
* r
= (guint32
*) v1
;
1239 return (*r
>= v2
.v_uint32
);
1243 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1245 * Applies the 'greater or equal' operator to the
1246 * specified structure and value
1247 * @param v1 left member of comparison
1248 * @param v2 right member of comparison
1249 * @return success/failure of operation
1251 gboolean
lttv_apply_op_ge_uint16(const gpointer v1
, LttvFieldValue v2
)
1253 guint16
* r
= (guint16
*) v1
;
1254 return (*r
>= v2
.v_uint16
);
1258 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1260 * Applies the 'greater or equal' operator to the
1261 * specified structure and value
1262 * @param v1 left member of comparison
1263 * @param v2 right member of comparison
1264 * @return success/failure of operation
1266 gboolean
lttv_apply_op_ge_double(const gpointer v1
, LttvFieldValue v2
)
1268 double* r
= (double*) v1
;
1269 return (*r
>= v2
.v_double
);
1273 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1275 * Applies the 'greater or equal' operator to the
1276 * specified structure and value
1277 * @param v1 left member of comparison
1278 * @param v2 right member of comparison
1279 * @return success/failure of operation
1281 gboolean
lttv_apply_op_ge_ltttime(const gpointer v1
, LttvFieldValue v2
)
1283 LttTime
* r
= (LttTime
*) v1
;
1284 // 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)));
1285 return ltt_time_compare(*r
, v2
.v_ltttime
)>-1?1:0;
1291 * Makes a copy of the current filter tree
1292 * @param tree pointer to the current tree
1293 * @return new copy of the filter tree
1295 LttvFilterTree
* lttv_filter_tree_clone(const LttvFilterTree
* tree
)
1298 LttvFilterTree
* newtree
= lttv_filter_tree_new();
1300 newtree
->node
= tree
->node
;
1302 newtree
->left
= tree
->left
;
1303 if(newtree
->left
== LTTV_TREE_NODE
) {
1304 newtree
->l_child
.t
= lttv_filter_tree_clone(tree
->l_child
.t
);
1305 } else if(newtree
->left
== LTTV_TREE_LEAF
) {
1306 newtree
->l_child
.leaf
= lttv_simple_expression_new();
1307 newtree
->l_child
.leaf
->field
= tree
->l_child
.leaf
->field
;
1308 newtree
->l_child
.leaf
->offset
= tree
->l_child
.leaf
->offset
;
1309 newtree
->l_child
.leaf
->op
= tree
->l_child
.leaf
->op
;
1310 /* FIXME: special case for string copy ! */
1311 newtree
->l_child
.leaf
->value
= tree
->l_child
.leaf
->value
;
1314 newtree
->right
= tree
->right
;
1315 if(newtree
->right
== LTTV_TREE_NODE
) {
1316 newtree
->r_child
.t
= lttv_filter_tree_clone(tree
->r_child
.t
);
1317 } else if(newtree
->right
== LTTV_TREE_LEAF
) {
1318 newtree
->r_child
.leaf
= lttv_simple_expression_new();
1319 newtree
->r_child
.leaf
->field
= tree
->r_child
.leaf
->field
;
1320 newtree
->r_child
.leaf
->offset
= tree
->r_child
.leaf
->offset
;
1321 newtree
->r_child
.leaf
->op
= tree
->r_child
.leaf
->op
;
1322 newtree
->r_child
.leaf
->value
= tree
->r_child
.leaf
->value
;
1330 * Makes a copy of the current filter
1331 * @param filter pointer to the current filter
1332 * @return new copy of the filter
1334 LttvFilter
* lttv_filter_clone(const LttvFilter
* filter
)
1337 if(!filter
) return NULL
;
1339 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
1341 strcpy(newfilter
->expression
,filter
->expression
);
1343 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
1351 * @fn LttvFilter* lttv_filter_new()
1353 * Creates a new LttvFilter
1354 * @return the current LttvFilter or NULL if error
1356 LttvFilter
* lttv_filter_new()
1359 LttvFilter
* filter
= g_new(LttvFilter
,1);
1360 filter
->expression
= NULL
;
1361 filter
->head
= NULL
;
1368 * @fn gboolean lttv_filter_update(LttvFilter*)
1370 * Updates the current LttvFilter by building
1371 * its tree based upon the expression string
1372 * @param filter pointer to the current LttvFilter
1373 * @return Failure/Success of operation
1375 gboolean
lttv_filter_update(LttvFilter
* filter
)
1378 // g_print("filter::lttv_filter_new()\n"); /* debug */
1380 if(filter
->expression
== NULL
) return FALSE
;
1384 p_nesting
=0, /* parenthesis nesting value */
1386 guint expression_len
;
1390 *tree
= lttv_filter_tree_new(), /* main tree */
1391 *subtree
= NULL
, /* buffer for subtrees */
1392 *t1
, /* buffer #1 */
1393 *t2
, /* buffer #2 */
1394 *t3
; /* buffer #3 */
1398 * If the tree already exists,
1399 * destroy it and build a new one
1401 if(filter
->head
!= NULL
) lttv_filter_tree_destroy(filter
->head
);
1402 filter
->head
= NULL
; /* will be assigned at the end */
1406 * each element of the list
1407 * is a sub tree created
1408 * by the use of parenthesis in the
1409 * global expression. The final tree
1410 * will be the one left at the root of
1413 GPtrArray
*tree_stack
= g_ptr_array_new();
1414 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
1416 /* temporary values */
1417 GString
*a_field_component
= g_string_new("");
1418 GString
*a_string_spaces
= g_string_new("");
1419 GPtrArray
*a_field_path
= g_ptr_array_new();
1421 /* simple expression buffer */
1422 LttvSimpleExpression
* a_simple_expression
= lttv_simple_expression_new();
1424 gint nest_quotes
= 0;
1427 * Parse entire expression and construct
1428 * the binary tree. There are two steps
1429 * in browsing that string
1430 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1431 * 2. finding simple expressions
1432 * - field path ( separated by dots )
1433 * - op ( >, <, =, >=, <=, !=)
1434 * - value ( integer, string ... )
1435 * To spare computing time, the whole
1436 * string is parsed in this loop for a
1437 * O(n) complexity order.
1439 * When encountering logical op &,|,^
1440 * 1. parse the last value if any
1441 * 2. create a new tree
1442 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1443 * 4. concatenate this tree with the current tree on top of the stack
1444 * When encountering math ops >,>=,<,<=,=,!=
1445 * 1. add to op to the simple expression
1446 * 2. concatenate last field component to field path
1447 * When encountering concatening ops .
1448 * 1. concatenate last field component to field path
1449 * When encountering opening parenthesis (,{,[
1450 * 1. create a new subtree on top of tree stack
1451 * When encountering closing parenthesis ),},]
1452 * 1. add the expression on right child of the current tree
1453 * 2. the subtree is completed, allocate a new subtree
1454 * 3. pop the tree value from the tree stack
1458 struct timeval starttime
;
1459 struct timeval endtime
;
1460 gettimeofday(&starttime
, NULL
);
1463 expression_len
= strlen(filter
->expression
);
1464 for(i
=0;i
<expression_len
;i
++) {
1466 // g_print("%c\n ",filter->expression[i]);
1468 switch(filter
->expression
[i
]) {
1470 if(filter
->expression
[i
+1] == '\"') {
1479 if(a_string_spaces
->len
!= 0) {
1480 a_field_component
= g_string_append(
1481 a_field_component
, a_string_spaces
->str
);
1482 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1484 a_field_component
= g_string_append_c(a_field_component
,
1485 filter
->expression
[i
]);
1489 switch(filter
->expression
[i
]) {
1495 /* get current tree in tree stack */
1496 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1498 /* get current node at absolute right */
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_AND
;
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
,
1521 g_string_free(a_field_component
,FALSE
));
1522 a_field_component
= g_string_new("");
1523 g_string_free(a_string_spaces
, TRUE
);
1524 a_string_spaces
= g_string_new("");
1525 t2
->left
= LTTV_TREE_LEAF
;
1526 t2
->l_child
.leaf
= a_simple_expression
;
1527 a_simple_expression
= lttv_simple_expression_new();
1533 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1534 while(t1
->right
!= LTTV_TREE_IDLE
) {
1535 g_assert(t1
->right
== LTTV_TREE_NODE
);
1538 t2
= lttv_filter_tree_new();
1539 t2
->node
= LTTV_LOGICAL_OR
;
1540 t1
->right
= LTTV_TREE_NODE
;
1542 if(not) { // add not operator to tree
1543 t3
= lttv_filter_tree_new();
1544 t3
->node
= LTTV_LOGICAL_NOT
;
1545 t2
->left
= LTTV_TREE_NODE
;
1550 if(subtree
!= NULL
) { /* append subtree to current tree */
1551 t2
->left
= LTTV_TREE_NODE
;
1552 t2
->l_child
.t
= subtree
;
1554 } else { /* append a simple expression */
1555 lttv_simple_expression_assign_value(a_simple_expression
,
1556 g_string_free(a_field_component
,FALSE
));
1557 a_field_component
= g_string_new("");
1558 g_string_free(a_string_spaces
, TRUE
);
1559 a_string_spaces
= g_string_new("");
1560 t2
->left
= LTTV_TREE_LEAF
;
1561 t2
->l_child
.leaf
= a_simple_expression
;
1562 a_simple_expression
= lttv_simple_expression_new();
1568 t1
= (LttvFilterTree
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1569 while(t1
->right
!= LTTV_TREE_IDLE
) {
1570 g_assert(t1
->right
== LTTV_TREE_NODE
);
1573 t2
= lttv_filter_tree_new();
1574 t2
->node
= LTTV_LOGICAL_XOR
;
1575 t1
->right
= LTTV_TREE_NODE
;
1577 if(not) { // add not operator to tree
1578 t3
= lttv_filter_tree_new();
1579 t3
->node
= LTTV_LOGICAL_NOT
;
1580 t2
->left
= LTTV_TREE_NODE
;
1585 if(subtree
!= NULL
) { /* append subtree to current tree */
1586 t2
->left
= LTTV_TREE_NODE
;
1587 t2
->l_child
.t
= subtree
;
1589 } else { /* append a simple expression */
1590 lttv_simple_expression_assign_value(a_simple_expression
,
1591 g_string_free(a_field_component
,FALSE
));
1592 a_field_component
= g_string_new("");
1593 g_string_free(a_string_spaces
, TRUE
);
1594 a_string_spaces
= g_string_new("");
1595 t2
->left
= LTTV_TREE_LEAF
;
1596 t2
->l_child
.leaf
= a_simple_expression
;
1597 a_simple_expression
= lttv_simple_expression_new();
1601 case '!': /* not, or not equal (math op) */
1603 if(filter
->expression
[i
+1] == '=') { /* != */
1604 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1605 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1606 a_field_component
= g_string_new("");
1607 g_string_free(a_string_spaces
, TRUE
);
1608 a_string_spaces
= g_string_new("");
1609 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_NE
);
1616 case '(': /* start of parenthesis */
1620 p_nesting
++; /* incrementing parenthesis nesting value */
1621 t1
= lttv_filter_tree_new();
1622 if(not) { /* add not operator to tree */
1623 t3
= lttv_filter_tree_new();
1624 t3
->node
= LTTV_LOGICAL_NOT
;
1625 t1
->right
= LTTV_TREE_NODE
;
1629 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
1632 case ')': /* end of parenthesis */
1636 p_nesting
--; /* decrementing parenthesis nesting value */
1637 if(p_nesting
<0 || tree_stack
->len
<2) {
1638 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1639 is not valid due to parenthesis incorrect use",filter
->expression
);
1643 /* there must at least be the root tree left in the array */
1644 g_assert(tree_stack
->len
>0);
1646 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1647 while(t1
->right
!= LTTV_TREE_IDLE
) {
1650 if(not) { // add not operator to tree
1652 t3
= lttv_filter_tree_new();
1653 t3
->node
= LTTV_LOGICAL_NOT
;
1654 t1
->right
= LTTV_TREE_NODE
;
1659 if(subtree
!= NULL
) { /* append subtree to current tree */
1660 t1
->right
= LTTV_TREE_NODE
;
1661 t1
->r_child
.t
= subtree
;
1662 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1663 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1664 } else { /* assign subtree as current tree */
1665 lttv_simple_expression_assign_value(a_simple_expression
,
1666 g_string_free(a_field_component
,FALSE
));
1667 a_field_component
= g_string_new("");
1668 g_string_free(a_string_spaces
, TRUE
);
1669 a_string_spaces
= g_string_new("");
1670 t1
->right
= LTTV_TREE_LEAF
;
1671 t1
->r_child
.leaf
= a_simple_expression
;
1672 a_simple_expression
= lttv_simple_expression_new();
1673 subtree
= g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
1678 * mathematic operators
1680 case '<': /* lower, lower or equal */
1682 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1683 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1684 a_field_component
= g_string_new("");
1685 g_string_free(a_string_spaces
, TRUE
);
1686 a_string_spaces
= g_string_new("");
1687 if(filter
->expression
[i
+1] == '=') { /* <= */
1689 lttv_simple_expression_assign_operator(a_simple_expression
,
1691 } else lttv_simple_expression_assign_operator(a_simple_expression
,
1695 case '>': /* higher, higher or equal */
1697 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1698 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1699 a_field_component
= g_string_new("");
1700 g_string_free(a_string_spaces
, TRUE
);
1701 a_string_spaces
= g_string_new("");
1702 if(filter
->expression
[i
+1] == '=') { /* >= */
1704 lttv_simple_expression_assign_operator(a_simple_expression
,
1706 } else lttv_simple_expression_assign_operator(a_simple_expression
,
1710 case '=': /* equal */
1712 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1713 lttv_simple_expression_assign_field(a_field_path
,a_simple_expression
);
1714 a_field_component
= g_string_new("");
1715 g_string_free(a_string_spaces
, TRUE
);
1716 a_string_spaces
= g_string_new("");
1717 lttv_simple_expression_assign_operator(a_simple_expression
,LTTV_FIELD_EQ
);
1721 * Field concatening caracter
1726 * divide field expression into elements
1727 * in a_field_path array.
1729 * A dot can also be present in double values
1731 if(a_simple_expression
->field
== LTTV_FILTER_UNDEFINED
) {
1732 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
1733 a_field_component
= g_string_new("");
1734 g_string_free(a_string_spaces
, TRUE
);
1735 a_string_spaces
= g_string_new("");
1737 /* Operator found, we are in the value field */
1738 g_string_append_c(a_field_component
, filter
->expression
[i
]);
1741 case ' ': /* keep spaces that are within a field component */
1742 if(a_field_component
->len
== 0) break; /* ignore */
1744 a_string_spaces
= g_string_append_c(a_string_spaces
,
1745 filter
->expression
[i
]);
1747 case '\n': /* ignore */
1750 nest_quotes
?(nest_quotes
=0):(nest_quotes
=1);
1752 default: /* concatening current string */
1753 if(a_string_spaces
->len
!= 0) {
1754 a_field_component
= g_string_append(
1755 a_field_component
, a_string_spaces
->str
);
1756 a_string_spaces
= g_string_set_size(a_string_spaces
, 0);
1758 a_field_component
= g_string_append_c(a_field_component
,
1759 filter
->expression
[i
]);
1764 * Preliminary check to see
1765 * if tree was constructed correctly
1768 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1769 is not valid due to parenthesis incorrect use",filter
->expression
);
1773 if(tree_stack
->len
!= 1) /* only root tree should remain */
1777 * processing last element of expression
1779 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
1780 while(t1
->right
!= LTTV_TREE_IDLE
) {
1781 g_assert(t1
->right
== LTTV_TREE_NODE
);
1784 if(not) { // add not operator to tree
1785 t3
= lttv_filter_tree_new();
1786 t3
->node
= LTTV_LOGICAL_NOT
;
1787 t1
->right
= LTTV_TREE_NODE
;
1792 if(subtree
!= NULL
) { /* add the subtree */
1793 t1
->right
= LTTV_TREE_NODE
;
1794 t1
->r_child
.t
= subtree
;
1796 } else { /* add a leaf */
1797 lttv_simple_expression_assign_value(a_simple_expression
,g_string_free(a_field_component
,FALSE
));
1798 a_field_component
= NULL
;
1799 g_string_free(a_string_spaces
, TRUE
);
1800 a_string_spaces
= NULL
;
1801 t1
->right
= LTTV_TREE_LEAF
;
1802 t1
->r_child
.leaf
= a_simple_expression
;
1803 a_simple_expression
= NULL
;
1807 /* free the pointer array */
1808 g_assert(a_field_path
->len
== 0);
1809 g_ptr_array_free(a_field_path
,TRUE
);
1811 /* free the tree stack -- but keep the root tree */
1812 filter
->head
= ltt_g_ptr_array_remove_index_slow(tree_stack
,0);
1813 g_ptr_array_free(tree_stack
,TRUE
);
1815 /* free the field buffer if allocated */
1816 if(a_field_component
!= NULL
) g_string_free(a_field_component
,TRUE
);
1817 if(a_string_spaces
!= NULL
) g_string_free(a_string_spaces
, TRUE
);
1819 /* free the simple expression buffer if allocated */
1820 if(a_simple_expression
!= NULL
) lttv_simple_expression_destroy(a_simple_expression
);
1822 g_assert(filter
->head
!= NULL
); /* tree should exist */
1823 g_assert(subtree
== NULL
); /* remaining subtree should be included in main tree */
1826 gettimeofday(&endtime
, NULL
);
1828 /* Calcul du temps de l'algorithme */
1829 double time1
= starttime
.tv_sec
+ (starttime
.tv_usec
/1000000.0);
1830 double time2
= endtime
.tv_sec
+ (endtime
.tv_usec
/1000000.0);
1831 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1832 g_print("%.10f %i\n",(time2
-time1
)*1000,strlen(filter
->expression
));
1836 g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1837 lttv_print_tree(filter
->head
,0) ;
1838 g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1846 * @fn void lttv_filter_destroy(LttvFilter*)
1848 * Destroy the current LttvFilter
1849 * @param filter pointer to the current LttvFilter
1851 void lttv_filter_destroy(LttvFilter
* filter
)
1856 if(filter
->expression
)
1857 g_free(filter
->expression
);
1859 lttv_filter_tree_destroy(filter
->head
);
1865 * @fn LttvFilterTree* lttv_filter_tree_new()
1867 * Assign a new tree for the current expression
1869 * @return pointer of LttvFilterTree
1871 LttvFilterTree
* lttv_filter_tree_new()
1873 LttvFilterTree
* tree
;
1875 tree
= g_new(LttvFilterTree
,1);
1876 tree
->node
= 0; //g_new(lttv_expression,1);
1877 tree
->left
= LTTV_TREE_IDLE
;
1878 tree
->right
= LTTV_TREE_IDLE
;
1879 tree
->r_child
.t
= NULL
;
1880 tree
->l_child
.t
= NULL
;
1886 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1888 * Append a new expression to the expression
1889 * defined in the current filter
1890 * @param filter pointer to the current LttvFilter
1891 * @param expression string that must be appended
1892 * @return Success/Failure of operation
1894 gboolean
lttv_filter_append_expression(LttvFilter
* filter
,
1895 const char *expression
)
1897 if(expression
== NULL
) return FALSE
;
1898 if(filter
== NULL
) return FALSE
;
1899 if(expression
[0] == '\0') return FALSE
; /* Empty expression */
1901 GString
* s
= g_string_new("");
1902 if(filter
->expression
!= NULL
) {
1903 s
= g_string_append(s
,filter
->expression
);
1904 s
= g_string_append_c(s
,'&');
1906 s
= g_string_append(s
,expression
);
1908 g_free(filter
->expression
);
1909 filter
->expression
= g_string_free(s
,FALSE
);
1911 /* TRUE if construction of tree proceeded without errors */
1912 return lttv_filter_update(filter
);
1917 * @fn void lttv_filter_clear_expression(LttvFilter*)
1919 * Clear the filter expression from the
1920 * current filter and sets its pointer to NULL
1921 * @param filter pointer to the current LttvFilter
1923 void lttv_filter_clear_expression(LttvFilter
* filter
)
1925 if(filter
->expression
!= NULL
) {
1926 g_free(filter
->expression
);
1927 filter
->expression
= NULL
;
1933 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1935 * Destroys the tree and his sub-trees
1936 * @param tree Tree which must be destroyed
1938 void lttv_filter_tree_destroy(LttvFilterTree
* tree
)
1940 if(tree
== NULL
) return;
1942 if(tree
->left
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->l_child
.leaf
);
1943 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
1945 if(tree
->right
== LTTV_TREE_LEAF
) lttv_simple_expression_destroy(tree
->r_child
.leaf
);
1946 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
1948 // g_free(tree->node);
1953 * Global parsing function for the current
1955 * @param t pointer to the current LttvFilterTree
1956 * @param event current LttEvent, NULL if not used
1957 * @param tracefile current LttTracefile, NULL if not used
1958 * @param trace current LttTrace, NULL if not used
1959 * @param state current LttvProcessState, NULL if not used
1960 * @param context current LttvTracefileContext, NULL if not used
1961 * @return response of filter
1963 gboolean
lttv_filter_tree_parse(
1964 const LttvFilterTree
* t
,
1965 const LttEvent
* event
,
1966 const LttTracefile
* tracefile
,
1967 const LttTrace
* trace
,
1968 const LttvTracefileContext
* context
,
1969 const LttvProcessState
* state
,
1970 const LttvTraceContext
* tc
1975 * Each tree is parsed in inorder.
1976 * This way, it's possible to apply the left filter of the
1977 * tree, then decide whether or not the right branch should
1978 * be parsed depending on the linking logical operator
1980 * Each node consists in a
1981 * 1. logical operator
1982 * 2. left child ( node or simple expression )
1983 * 3. right child ( node or simple expression )
1985 * When the child is a simple expression, we must
1986 * before all determine if the expression refers to
1987 * a structure which is whithin observation ( not NULL ).
1988 * -If so, the expression is evaluated.
1989 * -If not, the result is set to TRUE since this particular
1990 * operation does not interfere with the lttv structure
1992 * The result of each simple expression will directly
1993 * affect the next branch. This way, depending on
1994 * the linking logical operator, the parser will decide
1995 * to explore or not the next branch.
1997 * -If result of left branch is 0 / FALSE
1998 * then don't explore right branch and return 0;
1999 * -If result of left branch is 1 / TRUE then explore
2001 * -If result of left branch is 1 / TRUE
2002 * then don't explore right branch and return 1;
2003 * -If result of left branch is 0 / FALSE then explore
2005 * -Result of left branch will not affect exploration of
2009 gboolean lresult
= FALSE
, rresult
= FALSE
;
2011 LttvTraceState
*ts
= NULL
;
2012 LttvTracefileState
*tfs
= (LttvTracefileState
*)context
;
2014 ts
= (LttvTraceState
*)tc
;
2016 ts
= (LttvTraceState
*)context
->t_context
;
2019 guint cpu
= tfs
->cpu
;
2021 state
= ts
->running_process
[cpu
];
2027 if(t
->left
== LTTV_TREE_NODE
) {
2028 lresult
= lttv_filter_tree_parse(t
->l_child
.t
,event
,
2029 tracefile
,trace
,context
,NULL
,NULL
);
2031 else if(t
->left
== LTTV_TREE_LEAF
) {
2032 lresult
= lttv_filter_tree_parse_branch(t
->l_child
.leaf
,event
,
2033 tracefile
,trace
,state
,context
);
2037 * Parse linking operator
2038 * make a cutoff if possible
2040 if((t
->node
& LTTV_LOGICAL_OR
) && lresult
== TRUE
) return TRUE
;
2041 if((t
->node
& LTTV_LOGICAL_AND
) && lresult
== FALSE
) return FALSE
;
2044 * Parse right branch
2046 if(t
->right
== LTTV_TREE_NODE
) {
2047 rresult
= lttv_filter_tree_parse(t
->r_child
.t
,event
,tracefile
,trace
,context
,NULL
,NULL
);
2049 else if(t
->right
== LTTV_TREE_LEAF
) {
2050 rresult
= lttv_filter_tree_parse_branch(t
->r_child
.leaf
,event
,tracefile
,trace
,state
,context
);
2055 * Apply and return the
2056 * logical link between the
2060 case LTTV_LOGICAL_OR
: return (lresult
| rresult
);
2061 case LTTV_LOGICAL_AND
: return (lresult
& rresult
);
2062 case LTTV_LOGICAL_NOT
:
2063 return (t
->left
==LTTV_TREE_LEAF
)?!lresult
:((t
->right
==LTTV_TREE_LEAF
)?!rresult
:TRUE
);
2064 case LTTV_LOGICAL_XOR
: return (lresult
^ rresult
);
2065 case 0: return (rresult
);
2068 * This case should never be
2069 * parsed, if so, this subtree
2078 * This function parses a particular branch of the tree
2079 * @param se pointer to the current LttvSimpleExpression
2080 * @param event current LttEvent, NULL if not used
2081 * @param tracefile current LttTracefile, NULL if not used
2082 * @param trace current LttTrace, NULL if not used
2083 * @param state current LttvProcessState, NULL if not used
2084 * @param context current LttvTracefileContext, NULL if not used
2085 * @return response of filter
2087 gboolean
lttv_filter_tree_parse_branch(
2088 const LttvSimpleExpression
* se
,
2089 const LttEvent
* event
,
2090 const LttTracefile
* tracefile
,
2091 const LttTrace
* trace
,
2092 const LttvProcessState
* state
,
2093 const LttvTracefileContext
* context
) {
2098 case LTTV_FILTER_TRACE_NAME
:
2099 if(trace
== NULL
) return TRUE
;
2101 GQuark quark
= ltt_trace_name(trace
);
2102 return se
->op((gpointer
)&quark
,v
);
2105 case LTTV_FILTER_TRACEFILE_NAME
:
2106 if(tracefile
== NULL
) return TRUE
;
2108 GQuark quark
= ltt_tracefile_name(tracefile
);
2109 return se
->op((gpointer
)&quark
,v
);
2112 case LTTV_FILTER_STATE_PID
:
2113 if(state
== NULL
) return TRUE
;
2114 else return se
->op((gpointer
)&state
->pid
,v
);
2116 case LTTV_FILTER_STATE_PPID
:
2117 if(state
== NULL
) return TRUE
;
2118 else return se
->op((gpointer
)&state
->ppid
,v
);
2120 case LTTV_FILTER_STATE_CT
:
2121 if(state
== NULL
) return TRUE
;
2123 return se
->op((gpointer
)&state
->creation_time
,v
);
2126 case LTTV_FILTER_STATE_IT
:
2127 if(state
== NULL
) return TRUE
;
2129 return se
->op((gpointer
)&state
->insertion_time
,v
);
2132 case LTTV_FILTER_STATE_P_NAME
:
2133 if(state
== NULL
) return TRUE
;
2135 GQuark quark
= state
->name
;
2136 return se
->op((gpointer
)&quark
,v
);
2139 case LTTV_FILTER_STATE_EX_MODE
:
2140 if(state
== NULL
) return TRUE
;
2141 else return se
->op((gpointer
)&state
->state
->t
,v
);
2143 case LTTV_FILTER_STATE_EX_SUBMODE
:
2144 if(state
== NULL
) return TRUE
;
2145 else return se
->op((gpointer
)&state
->state
->n
,v
);
2147 case LTTV_FILTER_STATE_P_STATUS
:
2148 if(state
== NULL
) return TRUE
;
2149 else return se
->op((gpointer
)&state
->state
->s
,v
);
2151 case LTTV_FILTER_STATE_CPU
:
2152 if(state
== NULL
) return TRUE
;
2154 return se
->op((gpointer
)&state
->cpu
,v
);
2157 case LTTV_FILTER_EVENT_NAME
:
2158 if(event
== NULL
) return TRUE
;
2160 struct marker_info
*info
;
2162 LttTracefile
*tf
= context
->tf
;
2163 qtuple
[0] = ltt_tracefile_name(tracefile
);
2164 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2165 g_assert(info
!= NULL
);
2166 qtuple
[1] = info
->name
;
2167 return se
->op((gpointer
)qtuple
,v
);
2170 case LTTV_FILTER_EVENT_SUBNAME
:
2171 if(event
== NULL
) return TRUE
;
2173 struct marker_info
*info
;
2174 LttTracefile
*tf
= context
->tf
;
2175 info
= marker_get_info_from_id(tf
->mdata
, event
->event_id
);
2176 g_assert(info
!= NULL
);
2177 GQuark quark
= info
->name
;
2178 return se
->op((gpointer
)&quark
,v
);
2181 case LTTV_FILTER_EVENT_CATEGORY
:
2183 * TODO: Not yet implemented
2187 case LTTV_FILTER_EVENT_TIME
:
2188 if(event
== NULL
) return TRUE
;
2190 LttTime time
= ltt_event_time(event
);
2191 return se
->op((gpointer
)&time
,v
);
2194 case LTTV_FILTER_EVENT_TSC
:
2195 if(event
== NULL
) return TRUE
;
2197 LttCycleCount count
= ltt_event_cycle_count(event
);
2198 return se
->op((gpointer
)&count
,v
);
2202 case LTTV_FILTER_EVENT_TARGET_PID
:
2203 if(context
== NULL
) return TRUE
;
2206 lttv_state_get_target_pid((LttvTracefileState
*)context
);
2207 return se
->op((gpointer
)&target_pid
,v
);
2211 case LTTV_FILTER_EVENT_FIELD
:
2213 * TODO: Use the offset to
2214 * find the dynamic field
2215 * in the event struct
2220 * This case should never be
2221 * parsed, if so, the whole
2222 * filtering is cancelled
2224 g_warning("Error while parsing the filter tree");
2228 /* should never get here */
2236 * Debug function. Prints tree memory allocation.
2237 * @param t the pointer to the current LttvFilterTree
2239 void lttv_print_tree(const LttvFilterTree
* t
, const int count
)
2241 g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t
, //t->l_child.t,t->r_child.t);
2242 (t
->left
==LTTV_TREE_NODE
)?t
->l_child
.t
:NULL
,
2243 (t
->right
==LTTV_TREE_NODE
)?t
->r_child
.t
:NULL
,
2245 g_debug("logic operator: %s\n",(t
->node
&1)?"OR":((t
->node
&2)?"AND":((t
->node
&4)?"NOT":((t
->node
&8)?"XOR":"IDLE"))));
2246 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"));
2247 if(t
->left
== LTTV_TREE_LEAF
) {
2248 g_debug("| |-> field type number: %i\n",t
->l_child
.leaf
->field
);
2249 g_debug("| |-> offset is: %i\n",t
->l_child
.leaf
->offset
);
2250 g_debug("| |-> operator function is: %p\n",t
->l_child
.leaf
->op
);
2252 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"));
2253 if(t
->right
== LTTV_TREE_LEAF
) {
2254 g_debug("| |-> field type number: %i\n",t
->r_child
.leaf
->field
);
2255 g_debug("| |-> offset is: %i\n",t
->r_child
.leaf
->offset
);
2256 g_debug("| |-> operator function is: %p\n",t
->r_child
.leaf
->op
);
2259 if(t
->left
== LTTV_TREE_NODE
) lttv_print_tree(t
->l_child
.t
,count
+1);
2260 if(t
->right
== LTTV_TREE_NODE
) lttv_print_tree(t
->r_child
.t
,count
+1);
2264 * @fn static void module_init()
2266 * Initializes the filter module and specific values
2268 static void module_init()
2274 * Destroys the filter module and specific values
2276 static void module_destroy()
2282 LTTV_MODULE("filter", "Filters traceset and events", \
2283 "Filters traceset and events specifically to user input", \
2284 module_init
, module_destroy
)