filter guint
[lttv.git] / ltt / branches / poly / lttv / lttv / filter.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa
3 *
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;
7 *
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.
12 *
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,
16 * MA 02111-1307, USA.
17 */
18
19 /*! \file lttv/lttv/filter.c
20 * \brief Defines the core filter of application
21 *
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.
26 *
27 * Fields specified in a simple expression can take following
28 * values
29 *
30 * \verbatim
31 * LttvTracefileContext{}
32 * |->event\
33 * | |->name (String, converted to GQuark)
34 * | |->category (String, not yet implemented)
35 * | |->time (LttTime)
36 * | |->tsc (LttCycleCount --> uint64)
37 * | |->fields
38 * | |->"event name"
39 * | |->"field name"
40 * | |->"sub-field name"
41 * | |->...
42 * | |->"leaf-field name" (field type)
43 * |->tracefile
44 * | |->name (String, converted to GQuark)
45 * |->trace
46 * | |->name (String, converted to GQuark)
47 * |->state
48 * |->pid (guint)
49 * |->ppid (guint)
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)
56 * |->cpu (guint)
57 * \endverbatim
58 */
59
60 /*
61 * \todo
62 * - refine switch of expression in multiple uses functions
63 * - remove the idle expressions in the tree
64 */
65
66 #ifdef HAVE_CONFIG_H
67 #include <config.h>
68 #endif
69
70 //#define TEST
71 #ifdef TEST
72 #include <time.h>
73 #include <sys/time.h>
74 #endif
75
76 #include <lttv/filter.h>
77 #include <ltt/trace.h>
78 #include <ltt/type.h>
79 #include <stdlib.h>
80 #include <string.h>
81
82 /**
83 * @fn LttvSimpleExpression* lttv_simple_expression_new()
84 *
85 * Constructor for LttvSimpleExpression
86 * @return pointer to new LttvSimpleExpression
87 */
88 LttvSimpleExpression*
89 lttv_simple_expression_new() {
90
91 LttvSimpleExpression* se = g_new(LttvSimpleExpression,1);
92
93 se->field = LTTV_FILTER_UNDEFINED;
94 se->op = NULL;
95 se->offset = 0;
96
97 return se;
98 }
99
100 /**
101 * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*)
102 *
103 * Parse through filtering field hierarchy as specified
104 * by user. This function compares each value to
105 * predetermined quarks
106 * @param fp The field path list
107 * @param se current simple expression
108 * @return success/failure of operation
109 */
110 gboolean
111 lttv_simple_expression_assign_field(GPtrArray* fp, LttvSimpleExpression* se) {
112
113 GString* f = NULL;
114
115 if(fp->len < 2) return FALSE;
116 g_assert((f=g_ptr_array_remove_index(fp,0)));
117
118 /*
119 * Parse through the specified
120 * hardcoded fields.
121 *
122 * Take note however that the
123 * 'event' subfields might change
124 * depending on values specified
125 * in core.xml file. Hence, if
126 * none of the subfields in the
127 * array match the hardcoded
128 * subfields, it will be considered
129 * as a dynamic field
130 */
131 if(!g_strcasecmp(f->str,"trace") ) {
132 /*
133 * Possible values:
134 * trace.name
135 */
136 g_string_free(f,TRUE);
137 f=g_ptr_array_remove_index(fp,0);
138 if(!g_strcasecmp(f->str,"name")) {
139 se->field = LTTV_FILTER_TRACE_NAME;
140 }
141 } else if(!g_strcasecmp(f->str,"traceset") ) {
142 /*
143 * FIXME: not yet implemented !
144 */
145 } else if(!g_strcasecmp(f->str,"tracefile") ) {
146 /*
147 * Possible values:
148 * tracefile.name
149 */
150 g_string_free(f,TRUE);
151 f=g_ptr_array_remove_index(fp,0);
152 if(!g_strcasecmp(f->str,"name")) {
153 se->field = LTTV_FILTER_TRACEFILE_NAME;
154 }
155 } else if(!g_strcasecmp(f->str,"state") ) {
156 /*
157 * Possible values:
158 * state.pid
159 * state.ppid
160 * state.creation_time
161 * state.insertion_time
162 * state.process_name
163 * state.execution_mode
164 * state.execution_submode
165 * state.process_status
166 * state.cpu
167 */
168 g_string_free(f,TRUE);
169 f=g_ptr_array_remove_index(fp,0);
170 if(!g_strcasecmp(f->str,"pid") ) {
171 se->field = LTTV_FILTER_STATE_PID;
172 }
173 else if(!g_strcasecmp(f->str,"ppid") ) {
174 se->field = LTTV_FILTER_STATE_PPID;
175 }
176 else if(!g_strcasecmp(f->str,"creation_time") ) {
177 se->field = LTTV_FILTER_STATE_CT;
178 }
179 else if(!g_strcasecmp(f->str,"insertion_time") ) {
180 se->field = LTTV_FILTER_STATE_IT;
181 }
182 else if(!g_strcasecmp(f->str,"process_name") ) {
183 se->field = LTTV_FILTER_STATE_P_NAME;
184 }
185 else if(!g_strcasecmp(f->str,"execution_mode") ) {
186 se->field = LTTV_FILTER_STATE_EX_MODE;
187 }
188 else if(!g_strcasecmp(f->str,"execution_submode") ) {
189 se->field = LTTV_FILTER_STATE_EX_SUBMODE;
190 }
191 else if(!g_strcasecmp(f->str,"process_status") ) {
192 se->field = LTTV_FILTER_STATE_P_STATUS;
193 }
194 else if(!g_strcasecmp(f->str,"cpu") ) {
195 se->field = LTTV_FILTER_STATE_CPU;
196 }
197 } else if(!g_strcasecmp(f->str,"event") ) {
198 /*
199 * Possible values:
200 * event.name
201 * event.category
202 * event.time
203 * event.tsc
204 */
205 g_string_free(f,TRUE);
206 f=g_ptr_array_remove_index(fp,0);
207 if(!g_strcasecmp(f->str,"name") ) {
208 se->field = LTTV_FILTER_EVENT_NAME;
209 }
210 else if(!g_strcasecmp(f->str,"category") ) {
211 /*
212 * FIXME: Category not yet functional in lttv
213 */
214 se->field = LTTV_FILTER_EVENT_CATEGORY;
215 }
216 else if(!g_strcasecmp(f->str,"time") ) {
217 se->field = LTTV_FILTER_EVENT_TIME;
218 }
219 else if(!g_strcasecmp(f->str,"tsc") ) {
220 se->field = LTTV_FILTER_EVENT_TSC;
221 }
222 else { /* core.xml specified options */
223 se->field = LTTV_FILTER_EVENT_FIELD;
224 }
225 } else {
226 g_warning("Unrecognized field in filter string");
227 }
228
229 /* free memory for last string */
230 g_string_free(f,TRUE);
231
232 /* array should be empty */
233 g_assert(fp->len == 0);
234
235 if(se->field == LTTV_FILTER_UNDEFINED) {
236 g_warning("The specified field was not recognized !");
237 return FALSE;
238 }
239 return TRUE;
240 }
241
242 /**
243 * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp)
244 *
245 * Sets the function pointer for the current
246 * Simple Expression
247 * @param se current simple expression
248 * @param op current operator
249 * @return success/failure of operation
250 */
251 gboolean
252 lttv_simple_expression_assign_operator(LttvSimpleExpression* se, LttvExpressionOp op) {
253
254 switch(se->field) {
255 /*
256 * string
257 */
258 case LTTV_FILTER_TRACE_NAME:
259 case LTTV_FILTER_TRACEFILE_NAME:
260 case LTTV_FILTER_STATE_P_NAME:
261 case LTTV_FILTER_EVENT_NAME:
262 case LTTV_FILTER_STATE_EX_MODE:
263 case LTTV_FILTER_STATE_EX_SUBMODE:
264 case LTTV_FILTER_STATE_P_STATUS:
265 switch(op) {
266 case LTTV_FIELD_EQ:
267 se->op = lttv_apply_op_eq_quark;
268 break;
269 case LTTV_FIELD_NE:
270 se->op = lttv_apply_op_ne_quark;
271 break;
272 default:
273 g_warning("Error encountered in operator assignment = or != expected");
274 return FALSE;
275 }
276 break;
277 /*
278 * integer
279 */
280 case LTTV_FILTER_EVENT_TSC:
281 switch(op) {
282 case LTTV_FIELD_EQ:
283 se->op = lttv_apply_op_eq_uint64;
284 break;
285 case LTTV_FIELD_NE:
286 se->op = lttv_apply_op_ne_uint64;
287 break;
288 case LTTV_FIELD_LT:
289 se->op = lttv_apply_op_lt_uint64;
290 break;
291 case LTTV_FIELD_LE:
292 se->op = lttv_apply_op_le_uint64;
293 break;
294 case LTTV_FIELD_GT:
295 se->op = lttv_apply_op_gt_uint64;
296 break;
297 case LTTV_FIELD_GE:
298 se->op = lttv_apply_op_ge_uint64;
299 break;
300 default:
301 g_warning("Error encountered in operator assignment");
302 return FALSE;
303 }
304 break;
305 /*
306 * unsigned integers
307 */
308 case LTTV_FILTER_STATE_CPU:
309 case LTTV_FILTER_STATE_PID:
310 case LTTV_FILTER_STATE_PPID:
311 switch(op) {
312 case LTTV_FIELD_EQ:
313 se->op = lttv_apply_op_eq_uint;
314 break;
315 case LTTV_FIELD_NE:
316 se->op = lttv_apply_op_ne_uint;
317 break;
318 case LTTV_FIELD_LT:
319 se->op = lttv_apply_op_lt_uint;
320 break;
321 case LTTV_FIELD_LE:
322 se->op = lttv_apply_op_le_uint;
323 break;
324 case LTTV_FIELD_GT:
325 se->op = lttv_apply_op_gt_uint;
326 break;
327 case LTTV_FIELD_GE:
328 se->op = lttv_apply_op_ge_uint;
329 break;
330 default:
331 g_warning("Error encountered in operator assignment");
332 return FALSE;
333 }
334 break;
335
336 /*
337 * Enums
338 * Entered as string, converted to enum
339 *
340 * can only be compared with 'equal' or 'not equal' operators
341 *
342 * unsigned int of 16 bits are used here since enums
343 * should not go over 2^16-1 values
344 */
345 // case /*NOTHING*/:
346 // switch(op) {
347 // case LTTV_FIELD_EQ:
348 // se->op = lttv_apply_op_eq_uint16;
349 // break;
350 // case LTTV_FIELD_NE:
351 // se->op = lttv_apply_op_ne_uint16;
352 // break;
353 // default:
354 // g_warning("Error encountered in operator assignment = or != expected");
355 // return FALSE;
356 // }
357 // break;
358 /*
359 * Ltttime
360 */
361 case LTTV_FILTER_STATE_CT:
362 case LTTV_FILTER_STATE_IT:
363 case LTTV_FILTER_EVENT_TIME:
364 switch(op) {
365 case LTTV_FIELD_EQ:
366 se->op = lttv_apply_op_eq_ltttime;
367 break;
368 case LTTV_FIELD_NE:
369 se->op = lttv_apply_op_ne_ltttime;
370 break;
371 case LTTV_FIELD_LT:
372 se->op = lttv_apply_op_lt_ltttime;
373 break;
374 case LTTV_FIELD_LE:
375 se->op = lttv_apply_op_le_ltttime;
376 break;
377 case LTTV_FIELD_GT:
378 se->op = lttv_apply_op_gt_ltttime;
379 break;
380 case LTTV_FIELD_GE:
381 se->op = lttv_apply_op_ge_ltttime;
382 break;
383 default:
384 g_warning("Error encountered in operator assignment");
385 return FALSE;
386 }
387 break;
388 default:
389 g_warning("Error encountered in operator assignation ! Field type:%i",se->field);
390 return FALSE;
391 }
392
393 return TRUE;
394
395 }
396
397 /**
398 * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*)
399 *
400 * Assign the value field to the current LttvSimpleExpression
401 * @param se pointer to the current LttvSimpleExpression
402 * @param value string value for simple expression
403 */
404 gboolean
405 lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value) {
406
407 unsigned i;
408 gboolean is_double = FALSE;
409 LttTime t = ltt_time_zero;
410 GString* v;
411
412 switch(se->field) {
413 /*
414 * Strings
415 * entered as strings, converted to Quarks
416 */
417 case LTTV_FILTER_TRACE_NAME:
418 case LTTV_FILTER_TRACEFILE_NAME:
419 case LTTV_FILTER_STATE_P_NAME:
420 case LTTV_FILTER_EVENT_NAME:
421 case LTTV_FILTER_STATE_EX_MODE:
422 case LTTV_FILTER_STATE_EX_SUBMODE:
423 case LTTV_FILTER_STATE_P_STATUS:
424 // se->value.v_string = value;
425 se->value.v_uint32 = g_quark_from_string(value);
426 g_free(value);
427 break;
428 /*
429 * integer -- supposed to be uint64
430 */
431 case LTTV_FILTER_EVENT_TSC:
432 se->value.v_uint64 = atoi(value);
433 g_free(value);
434 break;
435 /*
436 * unsigned integers
437 */
438 case LTTV_FILTER_STATE_PID:
439 case LTTV_FILTER_STATE_PPID:
440 case LTTV_FILTER_STATE_CPU:
441 se->value.v_uint = atoi(value);
442 g_free(value);
443 break;
444 /*
445 * LttTime
446 */
447 case LTTV_FILTER_STATE_CT:
448 case LTTV_FILTER_STATE_IT:
449 case LTTV_FILTER_EVENT_TIME:
450 //se->value.v_double = atof(value);
451 /*
452 * parsing logic could be optimised,
453 * but as for now, simpler this way
454 */
455 v = g_string_new("");
456 for(i=0;i<strlen(value);i++) {
457 if(value[i] == '.') {
458 /* cannot specify number with more than one '.' */
459 if(is_double) return FALSE;
460 else is_double = TRUE;
461 t.tv_sec = atoi(v->str);
462 g_string_free(v,TRUE);
463 v = g_string_new("");
464 } else g_string_append_c(v,value[i]);
465 }
466 /* number can be integer or double */
467 if(is_double) t.tv_nsec = atoi(v->str);
468 else t.tv_sec = atoi(v->str);
469
470 g_string_free(v,TRUE);
471
472 se->value.v_ltttime = t;
473 g_free(value);
474 break;
475 default:
476 g_warning("Error encountered in value assignation ! Field type = %i",se->field);
477 g_free(value);
478 return FALSE;
479 }
480
481 return TRUE;
482
483 }
484
485 /**
486 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
487 *
488 * Disallocate memory for the current
489 * simple expression
490 * @param se pointer to the current LttvSimpleExpression
491 */
492 void
493 lttv_simple_expression_destroy(LttvSimpleExpression* se) {
494
495 // g_free(se->value);
496 // switch(se->field) {
497 // case LTTV_FILTER_TRACE_NAME:
498 // case LTTV_FILTER_TRACEFILE_NAME:
499 // case LTTV_FILTER_STATE_P_NAME:
500 // case LTTV_FILTER_EVENT_NAME:
501 // g_free(se->value.v_string);
502 // break;
503 // }
504 g_free(se);
505
506 }
507
508 /**
509 * @fn gint lttv_struct_type(gint)
510 *
511 * Finds the structure type depending
512 * on the fields in parameters
513 * @params ft Field of the current structure
514 * @return LttvStructType enum or -1 for error
515 */
516 gint
517 lttv_struct_type(gint ft) {
518
519 switch(ft) {
520 case LTTV_FILTER_TRACE_NAME:
521 return LTTV_FILTER_TRACE;
522 break;
523 case LTTV_FILTER_TRACEFILE_NAME:
524 return LTTV_FILTER_TRACEFILE;
525 break;
526 case LTTV_FILTER_STATE_PID:
527 case LTTV_FILTER_STATE_PPID:
528 case LTTV_FILTER_STATE_CT:
529 case LTTV_FILTER_STATE_IT:
530 case LTTV_FILTER_STATE_P_NAME:
531 case LTTV_FILTER_STATE_EX_MODE:
532 case LTTV_FILTER_STATE_EX_SUBMODE:
533 case LTTV_FILTER_STATE_P_STATUS:
534 case LTTV_FILTER_STATE_CPU:
535 return LTTV_FILTER_STATE;
536 break;
537 case LTTV_FILTER_EVENT_NAME:
538 case LTTV_FILTER_EVENT_CATEGORY:
539 case LTTV_FILTER_EVENT_TIME:
540 case LTTV_FILTER_EVENT_TSC:
541 case LTTV_FILTER_EVENT_FIELD:
542 return LTTV_FILTER_EVENT;
543 break;
544 default:
545 return -1;
546 }
547 }
548
549 /**
550 * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue)
551 *
552 * Applies the 'equal' operator to the
553 * specified structure and value
554 * @param v1 left member of comparison
555 * @param v2 right member of comparison
556 * @return success/failure of operation
557 */
558 gboolean lttv_apply_op_eq_uint(const gpointer v1, LttvFieldValue v2) {
559
560 guint* r = (guint*) v1;
561 return (*r == v2.v_uint);
562
563 }
564
565 /**
566 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
567 *
568 * Applies the 'equal' operator to the
569 * specified structure and value
570 * @param v1 left member of comparison
571 * @param v2 right member of comparison
572 * @return success/failure of operation
573 */
574 gboolean lttv_apply_op_eq_uint64(const gpointer v1, LttvFieldValue v2) {
575
576 guint64* r = (guint64*) v1;
577 return (*r == v2.v_uint64);
578
579 }
580
581 /**
582 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
583 *
584 * Applies the 'equal' operator to the
585 * specified structure and value
586 * @param v1 left member of comparison
587 * @param v2 right member of comparison
588 * @return success/failure of operation
589 */
590 gboolean lttv_apply_op_eq_uint32(const gpointer v1, LttvFieldValue v2) {
591 guint32* r = (guint32*) v1;
592 return (*r == v2.v_uint32);
593 }
594
595 /**
596 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
597 *
598 * Applies the 'equal' operator to the
599 * specified structure and value
600 * @param v1 left member of comparison
601 * @param v2 right member of comparison
602 * @return success/failure of operation
603 */
604 gboolean lttv_apply_op_eq_uint16(const gpointer v1, LttvFieldValue v2) {
605 guint16* r = (guint16*) v1;
606 return (*r == v2.v_uint16);
607 }
608
609 /**
610 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
611 *
612 * Applies the 'equal' operator to the
613 * specified structure and value
614 * @param v1 left member of comparison
615 * @param v2 right member of comparison
616 * @return success/failure of operation
617 */
618 gboolean lttv_apply_op_eq_double(const gpointer v1, LttvFieldValue v2) {
619 double* r = (double*) v1;
620 return (*r == v2.v_double);
621 }
622
623 /**
624 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
625 *
626 * Applies the 'equal' operator to the
627 * specified structure and value
628 * @param v1 left member of comparison
629 * @param v2 right member of comparison
630 * @return success/failure of operation
631 */
632 gboolean lttv_apply_op_eq_string(const gpointer v1, LttvFieldValue v2) {
633 char* r = (char*) v1;
634 return (!g_strcasecmp(r,v2.v_string));
635 }
636
637 /**
638 * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue)
639 *
640 * Applies the 'equal' operator to the
641 * specified structure and value
642 * @param v1 left member of comparison
643 * @param v2 right member of comparison
644 * @return success/failure of operation
645 */
646 gboolean lttv_apply_op_eq_quark(const gpointer v1, LttvFieldValue v2) {
647 GQuark* r = (GQuark*) v1;
648 // g_print("v1:%i v2:%i\n",*r,v2.v_uint32);
649 return (*r == v2.v_uint32);
650 }
651
652 /**
653 * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue)
654 *
655 * Applies the 'equal' operator to the
656 * specified structure and value
657 * @param v1 left member of comparison
658 * @param v2 right member of comparison
659 * @return success/failure of operation
660 */
661 gboolean lttv_apply_op_eq_ltttime(const gpointer v1, LttvFieldValue v2) {
662 LttTime* r = (LttTime*) v1;
663 return ltt_time_compare(*r, v2.v_ltttime)==0?1:0;
664 }
665
666 /**
667 * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue)
668 *
669 * Applies the 'not equal' operator to the
670 * specified structure and value
671 * @param v1 left member of comparison
672 * @param v2 right member of comparison
673 * @return success/failure of operation
674 */
675 gboolean lttv_apply_op_ne_uint(const gpointer v1, LttvFieldValue v2) {
676 guint* r = (guint*) v1;
677 return (*r != v2.v_uint);
678 }
679
680 /**
681 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
682 *
683 * Applies the 'not equal' operator to the
684 * specified structure and value
685 * @param v1 left member of comparison
686 * @param v2 right member of comparison
687 * @return success/failure of operation
688 */
689 gboolean lttv_apply_op_ne_uint64(const gpointer v1, LttvFieldValue v2) {
690 guint64* r = (guint64*) v1;
691 return (*r != v2.v_uint64);
692 }
693
694 /**
695 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
696 *
697 * Applies the 'not equal' operator to the
698 * specified structure and value
699 * @param v1 left member of comparison
700 * @param v2 right member of comparison
701 * @return success/failure of operation
702 */
703 gboolean lttv_apply_op_ne_uint32(const gpointer v1, LttvFieldValue v2) {
704 guint32* r = (guint32*) v1;
705 return (*r != v2.v_uint32);
706 }
707
708 /**
709 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
710 *
711 * Applies the 'not equal' operator to the
712 * specified structure and value
713 * @param v1 left member of comparison
714 * @param v2 right member of comparison
715 * @return success/failure of operation
716 */
717 gboolean lttv_apply_op_ne_uint16(const gpointer v1, LttvFieldValue v2) {
718 guint16* r = (guint16*) v1;
719 return (*r != v2.v_uint16);
720 }
721
722 /**
723 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
724 *
725 * Applies the 'not equal' operator to the
726 * specified structure and value
727 * @param v1 left member of comparison
728 * @param v2 right member of comparison
729 * @return success/failure of operation
730 */
731 gboolean lttv_apply_op_ne_double(const gpointer v1, LttvFieldValue v2) {
732 double* r = (double*) v1;
733 return (*r != v2.v_double);
734 }
735
736 /**
737 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
738 *
739 * Applies the 'not equal' operator to the
740 * specified structure and value
741 * @param v1 left member of comparison
742 * @param v2 right member of comparison
743 * @return success/failure of operation
744 */
745 gboolean lttv_apply_op_ne_string(const gpointer v1, LttvFieldValue v2) {
746 char* r = (char*) v1;
747 return (g_strcasecmp(r,v2.v_string));
748 }
749
750 /**
751 * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue)
752 *
753 * Applies the 'not equal' operator to the
754 * specified structure and value
755 * @param v1 left member of comparison
756 * @param v2 right member of comparison
757 * @return success/failure of operation
758 */
759 gboolean lttv_apply_op_ne_quark(const gpointer v1, LttvFieldValue v2) {
760 GQuark* r = (GQuark*) v1;
761 return (*r != v2.v_uint32);
762 }
763
764
765 /**
766 * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue)
767 *
768 * Applies the 'not equal' operator to the
769 * specified structure and value
770 * @param v1 left member of comparison
771 * @param v2 right member of comparison
772 * @return success/failure of operation
773 */
774 gboolean lttv_apply_op_ne_ltttime(const gpointer v1, LttvFieldValue v2) {
775 LttTime* r = (LttTime*) v1;
776 return ltt_time_compare(*r, v2.v_ltttime)!=0?1:0;
777 }
778
779 /**
780 * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue)
781 *
782 * Applies the 'lower than' operator to the
783 * specified structure and value
784 * @param v1 left member of comparison
785 * @param v2 right member of comparison
786 * @return success/failure of operation
787 */
788 gboolean lttv_apply_op_lt_uint(const gpointer v1, LttvFieldValue v2) {
789 guint* r = (guint*) v1;
790 return (*r < v2.v_uint);
791 }
792
793 /**
794 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
795 *
796 * Applies the 'lower than' operator to the
797 * specified structure and value
798 * @param v1 left member of comparison
799 * @param v2 right member of comparison
800 * @return success/failure of operation
801 */
802 gboolean lttv_apply_op_lt_uint64(const gpointer v1, LttvFieldValue v2) {
803 guint64* r = (guint64*) v1;
804 return (*r < v2.v_uint64);
805 }
806
807 /**
808 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
809 *
810 * Applies the 'lower than' operator to the
811 * specified structure and value
812 * @param v1 left member of comparison
813 * @param v2 right member of comparison
814 * @return success/failure of operation
815 */
816 gboolean lttv_apply_op_lt_uint32(const gpointer v1, LttvFieldValue v2) {
817 guint32* r = (guint32*) v1;
818 return (*r < v2.v_uint32);
819 }
820
821 /**
822 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
823 *
824 * Applies the 'lower than' operator to the
825 * specified structure and value
826 * @param v1 left member of comparison
827 * @param v2 right member of comparison
828 * @return success/failure of operation
829 */
830 gboolean lttv_apply_op_lt_uint16(const gpointer v1, LttvFieldValue v2) {
831 guint16* r = (guint16*) v1;
832 return (*r < v2.v_uint16);
833 }
834
835 /**
836 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
837 *
838 * Applies the 'lower than' operator to the
839 * specified structure and value
840 * @param v1 left member of comparison
841 * @param v2 right member of comparison
842 * @return success/failure of operation
843 */
844 gboolean lttv_apply_op_lt_double(const gpointer v1, LttvFieldValue v2) {
845 double* r = (double*) v1;
846 return (*r < v2.v_double);
847 }
848
849 /**
850 * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue)
851 *
852 * Applies the 'lower than' operator to the
853 * specified structure and value
854 * @param v1 left member of comparison
855 * @param v2 right member of comparison
856 * @return success/failure of operation
857 */
858 gboolean lttv_apply_op_lt_ltttime(const gpointer v1, LttvFieldValue v2) {
859 LttTime* r = (LttTime*) v1;
860 // 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)));
861 return ltt_time_compare(*r, v2.v_ltttime)==-1?1:0;
862 }
863
864 /**
865 * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue)
866 *
867 * Applies the 'lower or equal' operator to the
868 * specified structure and value
869 * @param v1 left member of comparison
870 * @param v2 right member of comparison
871 * @return success/failure of operation
872 */
873 gboolean lttv_apply_op_le_uint(const gpointer v1, LttvFieldValue v2) {
874 guint* r = (guint*) v1;
875 return (*r <= v2.v_uint);
876 }
877
878 /**
879 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
880 *
881 * Applies the 'lower or equal' operator to the
882 * specified structure and value
883 * @param v1 left member of comparison
884 * @param v2 right member of comparison
885 * @return success/failure of operation
886 */
887 gboolean lttv_apply_op_le_uint64(const gpointer v1, LttvFieldValue v2) {
888 guint64* r = (guint64*) v1;
889 return (*r <= v2.v_uint64);
890 }
891
892 /**
893 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
894 *
895 * Applies the 'lower or equal' operator to the
896 * specified structure and value
897 * @param v1 left member of comparison
898 * @param v2 right member of comparison
899 * @return success/failure of operation
900 */
901 gboolean lttv_apply_op_le_uint32(const gpointer v1, LttvFieldValue v2) {
902 guint32* r = (guint32*) v1;
903 return (*r <= v2.v_uint32);
904 }
905
906 /**
907 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
908 *
909 * Applies the 'lower or equal' operator to the
910 * specified structure and value
911 * @param v1 left member of comparison
912 * @param v2 right member of comparison
913 * @return success/failure of operation
914 */
915 gboolean lttv_apply_op_le_uint16(const gpointer v1, LttvFieldValue v2) {
916 guint16* r = (guint16*) v1;
917 return (*r <= v2.v_uint16);
918 }
919
920 /**
921 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
922 *
923 * Applies the 'lower or equal' operator to the
924 * specified structure and value
925 * @param v1 left member of comparison
926 * @param v2 right member of comparison
927 * @return success/failure of operation
928 */
929 gboolean lttv_apply_op_le_double(const gpointer v1, LttvFieldValue v2) {
930 double* r = (double*) v1;
931 return (*r <= v2.v_double);
932 }
933
934 /**
935 * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue)
936 *
937 * Applies the 'lower or equal' operator to the
938 * specified structure and value
939 * @param v1 left member of comparison
940 * @param v2 right member of comparison
941 * @return success/failure of operation
942 */
943 gboolean lttv_apply_op_le_ltttime(const gpointer v1, LttvFieldValue v2) {
944 LttTime* r = (LttTime*) v1;
945 // 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)));
946 return ltt_time_compare(*r, v2.v_ltttime)<1?1:0;
947 }
948
949
950 /**
951 * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue)
952 *
953 * Applies the 'greater than' operator to the
954 * specified structure and value
955 * @param v1 left member of comparison
956 * @param v2 right member of comparison
957 * @return success/failure of operation
958 */
959 gboolean lttv_apply_op_gt_uint(const gpointer v1, LttvFieldValue v2) {
960 guint* r = (guint*) v1;
961 return (*r > v2.v_uint);
962 }
963
964 /**
965 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
966 *
967 * Applies the 'greater than' operator to the
968 * specified structure and value
969 * @param v1 left member of comparison
970 * @param v2 right member of comparison
971 * @return success/failure of operation
972 */
973 gboolean lttv_apply_op_gt_uint64(const gpointer v1, LttvFieldValue v2) {
974 guint64* r = (guint64*) v1;
975 return (*r > v2.v_uint64);
976 }
977
978 /**
979 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
980 *
981 * Applies the 'greater than' operator to the
982 * specified structure and value
983 * @param v1 left member of comparison
984 * @param v2 right member of comparison
985 * @return success/failure of operation
986 */
987 gboolean lttv_apply_op_gt_uint32(const gpointer v1, LttvFieldValue v2) {
988 guint32* r = (guint32*) v1;
989 return (*r > v2.v_uint32);
990 }
991
992 /**
993 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
994 *
995 * Applies the 'greater than' operator to the
996 * specified structure and value
997 * @param v1 left member of comparison
998 * @param v2 right member of comparison
999 * @return success/failure of operation
1000 */
1001 gboolean lttv_apply_op_gt_uint16(const gpointer v1, LttvFieldValue v2) {
1002 guint16* r = (guint16*) v1;
1003 return (*r > v2.v_uint16);
1004 }
1005
1006 /**
1007 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
1008 *
1009 * Applies the 'greater than' operator to the
1010 * specified structure and value
1011 * @param v1 left member of comparison
1012 * @param v2 right member of comparison
1013 * @return success/failure of operation
1014 */
1015 gboolean lttv_apply_op_gt_double(const gpointer v1, LttvFieldValue v2) {
1016 double* r = (double*) v1;
1017 return (*r > v2.v_double);
1018 }
1019
1020 /**
1021 * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue)
1022 *
1023 * Applies the 'greater than' operator to the
1024 * specified structure and value
1025 * @param v1 left member of comparison
1026 * @param v2 right member of comparison
1027 * @return success/failure of operation
1028 */
1029 gboolean lttv_apply_op_gt_ltttime(const gpointer v1, LttvFieldValue v2) {
1030 LttTime* r = (LttTime*) v1;
1031 // 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)));
1032 return ltt_time_compare(*r, v2.v_ltttime)==1?1:0;
1033 }
1034
1035 /**
1036 * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue)
1037 *
1038 * Applies the 'greater or equal' operator to the
1039 * specified structure and value
1040 * @param v1 left member of comparison
1041 * @param v2 right member of comparison
1042 * @return success/failure of operation
1043 */
1044 gboolean lttv_apply_op_ge_uint(const gpointer v1, LttvFieldValue v2) {
1045 guint* r = (guint*) v1;
1046 return (*r >= v2.v_uint);
1047 }
1048
1049 /**
1050 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
1051 *
1052 * Applies the 'greater or equal' operator to the
1053 * specified structure and value
1054 * @param v1 left member of comparison
1055 * @param v2 right member of comparison
1056 * @return success/failure of operation
1057 */
1058 gboolean lttv_apply_op_ge_uint64(const gpointer v1, LttvFieldValue v2) {
1059 guint64* r = (guint64*) v1;
1060 return (*r >= v2.v_uint64);
1061 }
1062
1063 /**
1064 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
1065 *
1066 * Applies the 'greater or equal' operator to the
1067 * specified structure and value
1068 * @param v1 left member of comparison
1069 * @param v2 right member of comparison
1070 * @return success/failure of operation
1071 */
1072 gboolean lttv_apply_op_ge_uint32(const gpointer v1, LttvFieldValue v2) {
1073 guint32* r = (guint32*) v1;
1074 return (*r >= v2.v_uint32);
1075 }
1076
1077 /**
1078 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
1079 *
1080 * Applies the 'greater or equal' operator to the
1081 * specified structure and value
1082 * @param v1 left member of comparison
1083 * @param v2 right member of comparison
1084 * @return success/failure of operation
1085 */
1086 gboolean lttv_apply_op_ge_uint16(const gpointer v1, LttvFieldValue v2) {
1087 guint16* r = (guint16*) v1;
1088 return (*r >= v2.v_uint16);
1089 }
1090
1091 /**
1092 * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue)
1093 *
1094 * Applies the 'greater or equal' operator to the
1095 * specified structure and value
1096 * @param v1 left member of comparison
1097 * @param v2 right member of comparison
1098 * @return success/failure of operation
1099 */
1100 gboolean lttv_apply_op_ge_double(const gpointer v1, LttvFieldValue v2) {
1101 double* r = (double*) v1;
1102 return (*r >= v2.v_double);
1103 }
1104
1105 /**
1106 * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue)
1107 *
1108 * Applies the 'greater or equal' operator to the
1109 * specified structure and value
1110 * @param v1 left member of comparison
1111 * @param v2 right member of comparison
1112 * @return success/failure of operation
1113 */
1114 gboolean lttv_apply_op_ge_ltttime(const gpointer v1, LttvFieldValue v2) {
1115 LttTime* r = (LttTime*) v1;
1116 // 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)));
1117 return ltt_time_compare(*r, v2.v_ltttime)>-1?1:0;
1118 }
1119
1120
1121
1122 /**
1123 * Makes a copy of the current filter tree
1124 * @param tree pointer to the current tree
1125 * @return new copy of the filter tree
1126 */
1127 LttvFilterTree*
1128 lttv_filter_tree_clone(const LttvFilterTree* tree) {
1129
1130 LttvFilterTree* newtree = lttv_filter_tree_new();
1131
1132 newtree->node = tree->node;
1133
1134 newtree->left = tree->left;
1135 if(newtree->left == LTTV_TREE_NODE) {
1136 newtree->l_child.t = lttv_filter_tree_clone(tree->l_child.t);
1137 } else if(newtree->left == LTTV_TREE_LEAF) {
1138 newtree->l_child.leaf = lttv_simple_expression_new();
1139 newtree->l_child.leaf->field = tree->l_child.leaf->field;
1140 newtree->l_child.leaf->offset = tree->l_child.leaf->offset;
1141 newtree->l_child.leaf->op = tree->l_child.leaf->op;
1142 /* FIXME: special case for string copy ! */
1143 newtree->l_child.leaf->value = tree->l_child.leaf->value;
1144 }
1145
1146 newtree->right = tree->right;
1147 if(newtree->right == LTTV_TREE_NODE) {
1148 newtree->r_child.t = lttv_filter_tree_clone(tree->r_child.t);
1149 } else if(newtree->right == LTTV_TREE_LEAF) {
1150 newtree->r_child.leaf = lttv_simple_expression_new();
1151 newtree->r_child.leaf->field = tree->r_child.leaf->field;
1152 newtree->r_child.leaf->offset = tree->r_child.leaf->offset;
1153 newtree->r_child.leaf->op = tree->r_child.leaf->op;
1154 newtree->r_child.leaf->value = tree->r_child.leaf->value;
1155 }
1156
1157 return newtree;
1158
1159 }
1160
1161 /**
1162 * Makes a copy of the current filter
1163 * @param filter pointer to the current filter
1164 * @return new copy of the filter
1165 */
1166 LttvFilter*
1167 lttv_filter_clone(const LttvFilter* filter) {
1168
1169 if(!filter) return NULL;
1170
1171 LttvFilter* newfilter = g_new(LttvFilter,1);
1172
1173 strcpy(newfilter->expression,filter->expression);
1174
1175 newfilter->head = lttv_filter_tree_clone(filter->head);
1176
1177 return newfilter;
1178
1179 }
1180
1181
1182 /**
1183 * @fn LttvFilter* lttv_filter_new()
1184 *
1185 * Creates a new LttvFilter
1186 * @return the current LttvFilter or NULL if error
1187 */
1188 LttvFilter*
1189 lttv_filter_new() {
1190
1191 LttvFilter* filter = g_new(LttvFilter,1);
1192 filter->expression = NULL;
1193 filter->head = NULL;
1194
1195 return filter;
1196
1197 }
1198
1199 /**
1200 * @fn gboolean lttv_filter_update(LttvFilter*)
1201 *
1202 * Updates the current LttvFilter by building
1203 * its tree based upon the expression string
1204 * @param filter pointer to the current LttvFilter
1205 * @return Failure/Success of operation
1206 */
1207 gboolean
1208 lttv_filter_update(LttvFilter* filter) {
1209
1210 // g_print("filter::lttv_filter_new()\n"); /* debug */
1211
1212 if(filter->expression == NULL) return FALSE;
1213
1214 int
1215 i,
1216 p_nesting=0, /* parenthesis nesting value */
1217 not=0;
1218
1219 /* trees */
1220 LttvFilterTree
1221 *tree = lttv_filter_tree_new(), /* main tree */
1222 *subtree = NULL, /* buffer for subtrees */
1223 *t1, /* buffer #1 */
1224 *t2, /* buffer #2 */
1225 *t3; /* buffer #3 */
1226
1227 /*
1228 * the filter
1229 * If the tree already exists,
1230 * destroy it and build a new one
1231 */
1232 if(filter->head != NULL) lttv_filter_tree_destroy(filter->head);
1233 filter->head = NULL; /* will be assigned at the end */
1234
1235 /*
1236 * Tree Stack
1237 * each element of the list
1238 * is a sub tree created
1239 * by the use of parenthesis in the
1240 * global expression. The final tree
1241 * will be the one left at the root of
1242 * the list
1243 */
1244 GPtrArray *tree_stack = g_ptr_array_new();
1245 g_ptr_array_add( tree_stack,(gpointer) tree );
1246
1247 /* temporary values */
1248 GString *a_field_component = g_string_new("");
1249 GPtrArray *a_field_path = g_ptr_array_new();
1250
1251 /* simple expression buffer */
1252 LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new();
1253
1254 /*
1255 * Parse entire expression and construct
1256 * the binary tree. There are two steps
1257 * in browsing that string
1258 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
1259 * 2. finding simple expressions
1260 * - field path ( separated by dots )
1261 * - op ( >, <, =, >=, <=, !=)
1262 * - value ( integer, string ... )
1263 * To spare computing time, the whole
1264 * string is parsed in this loop for a
1265 * O(n) complexity order.
1266 *
1267 * When encountering logical op &,|,^
1268 * 1. parse the last value if any
1269 * 2. create a new tree
1270 * 3. add the expression (simple exp, or exp (subtree)) to the tree
1271 * 4. concatenate this tree with the current tree on top of the stack
1272 * When encountering math ops >,>=,<,<=,=,!=
1273 * 1. add to op to the simple expression
1274 * 2. concatenate last field component to field path
1275 * When encountering concatening ops .
1276 * 1. concatenate last field component to field path
1277 * When encountering opening parenthesis (,{,[
1278 * 1. create a new subtree on top of tree stack
1279 * When encountering closing parenthesis ),},]
1280 * 1. add the expression on right child of the current tree
1281 * 2. the subtree is completed, allocate a new subtree
1282 * 3. pop the tree value from the tree stack
1283 */
1284
1285 #ifdef TEST
1286 struct timeval starttime;
1287 struct timeval endtime;
1288 gettimeofday(&starttime, NULL);
1289 #endif
1290
1291 for(i=0;i<strlen(filter->expression);i++) {
1292 // debug
1293 // g_print("%c\n ",filter->expression[i]);
1294
1295 switch(filter->expression[i]) {
1296 /*
1297 * logical operators
1298 */
1299 case '&': /* and */
1300
1301 /* get current tree in tree stack */
1302 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1303
1304 /* get current node at absolute right */
1305 while(t1->right != LTTV_TREE_IDLE) {
1306 g_assert(t1->right == LTTV_TREE_NODE);
1307 t1 = t1->r_child.t;
1308 }
1309 t2 = lttv_filter_tree_new();
1310 t2->node = LTTV_LOGICAL_AND;
1311 t1->right = LTTV_TREE_NODE;
1312 t1->r_child.t = t2;
1313 if(not) { /* add not operator to tree */
1314 t3 = lttv_filter_tree_new();
1315 t3->node = LTTV_LOGICAL_NOT;
1316 t2->left = LTTV_TREE_NODE;
1317 t2->l_child.t = t3;
1318 t2 = t3;
1319 not = 0;
1320 }
1321 if(subtree != NULL) { /* append subtree to current tree */
1322 t2->left = LTTV_TREE_NODE;
1323 t2->l_child.t = subtree;
1324 subtree = NULL;
1325 } else { /* append a simple expression */
1326 lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
1327 a_field_component = g_string_new("");
1328 t2->left = LTTV_TREE_LEAF;
1329 t2->l_child.leaf = a_simple_expression;
1330 a_simple_expression = lttv_simple_expression_new();
1331 }
1332 break;
1333
1334 case '|': /* or */
1335
1336 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1337 while(t1->right != LTTV_TREE_IDLE) {
1338 g_assert(t1->right == LTTV_TREE_NODE);
1339 t1 = t1->r_child.t;
1340 }
1341 t2 = lttv_filter_tree_new();
1342 t2->node = LTTV_LOGICAL_OR;
1343 t1->right = LTTV_TREE_NODE;
1344 t1->r_child.t = t2;
1345 if(not) { // add not operator to tree
1346 t3 = lttv_filter_tree_new();
1347 t3->node = LTTV_LOGICAL_NOT;
1348 t2->left = LTTV_TREE_NODE;
1349 t2->l_child.t = t3;
1350 t2 = t3;
1351 not = 0;
1352 }
1353 if(subtree != NULL) { /* append subtree to current tree */
1354 t2->left = LTTV_TREE_NODE;
1355 t2->l_child.t = subtree;
1356 subtree = NULL;
1357 } else { /* append a simple expression */
1358 lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
1359 a_field_component = g_string_new("");
1360 t2->left = LTTV_TREE_LEAF;
1361 t2->l_child.leaf = a_simple_expression;
1362 a_simple_expression = lttv_simple_expression_new();
1363 }
1364 break;
1365
1366 case '^': /* xor */
1367
1368 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1369 while(t1->right != LTTV_TREE_IDLE) {
1370 g_assert(t1->right == LTTV_TREE_NODE);
1371 t1 = t1->r_child.t;
1372 }
1373 t2 = lttv_filter_tree_new();
1374 t2->node = LTTV_LOGICAL_XOR;
1375 t1->right = LTTV_TREE_NODE;
1376 t1->r_child.t = t2;
1377 if(not) { // add not operator to tree
1378 t3 = lttv_filter_tree_new();
1379 t3->node = LTTV_LOGICAL_NOT;
1380 t2->left = LTTV_TREE_NODE;
1381 t2->l_child.t = t3;
1382 t2 = t3;
1383 not = 0;
1384 }
1385 if(subtree != NULL) { /* append subtree to current tree */
1386 t2->left = LTTV_TREE_NODE;
1387 t2->l_child.t = subtree;
1388 subtree = NULL;
1389 } else { /* append a simple expression */
1390 lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
1391 a_field_component = g_string_new("");
1392 t2->left = LTTV_TREE_LEAF;
1393 t2->l_child.leaf = a_simple_expression;
1394 a_simple_expression = lttv_simple_expression_new();
1395 }
1396 break;
1397
1398 case '!': /* not, or not equal (math op) */
1399
1400 if(filter->expression[i+1] == '=') { /* != */
1401 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1402 lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
1403 a_field_component = g_string_new("");
1404 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_NE);
1405 i++;
1406 } else { /* ! */
1407 not=1;
1408 }
1409 break;
1410
1411 case '(': /* start of parenthesis */
1412 case '[':
1413 case '{':
1414
1415 p_nesting++; /* incrementing parenthesis nesting value */
1416 t1 = lttv_filter_tree_new();
1417 if(not) { /* add not operator to tree */
1418 t3 = lttv_filter_tree_new();
1419 t3->node = LTTV_LOGICAL_NOT;
1420 t1->right = LTTV_TREE_NODE;
1421 t1->r_child.t = t3;
1422 not = 0;
1423 }
1424 g_ptr_array_add( tree_stack,(gpointer) t1 );
1425 break;
1426
1427 case ')': /* end of parenthesis */
1428 case ']':
1429 case '}':
1430
1431 p_nesting--; /* decrementing parenthesis nesting value */
1432 if(p_nesting<0 || tree_stack->len<2) {
1433 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1434 is not valid due to parenthesis incorrect use",filter->expression);
1435 return FALSE;
1436 }
1437
1438 /* there must at least be the root tree left in the array */
1439 g_assert(tree_stack->len>0);
1440
1441 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1442 while(t1->right != LTTV_TREE_IDLE) {
1443 t1 = t1->r_child.t;
1444 }
1445 if(not) { // add not operator to tree
1446 g_print("ici");
1447 t3 = lttv_filter_tree_new();
1448 t3->node = LTTV_LOGICAL_NOT;
1449 t1->right = LTTV_TREE_NODE;
1450 t1->r_child.t = t3;
1451 t1 = t3;
1452 not = 0;
1453 }
1454 if(subtree != NULL) { /* append subtree to current tree */
1455 t1->right = LTTV_TREE_NODE;
1456 t1->r_child.t = subtree;
1457 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
1458 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
1459 } else { /* assign subtree as current tree */
1460 lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
1461 a_field_component = g_string_new("");
1462 t1->right = LTTV_TREE_LEAF;
1463 t1->r_child.leaf = a_simple_expression;
1464 a_simple_expression = lttv_simple_expression_new();
1465 subtree = g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
1466 }
1467 break;
1468
1469 /*
1470 * mathematic operators
1471 */
1472 case '<': /* lower, lower or equal */
1473
1474 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1475 lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
1476 a_field_component = g_string_new("");
1477 if(filter->expression[i+1] == '=') { /* <= */
1478 i++;
1479 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LE);
1480 } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LT);
1481 break;
1482
1483 case '>': /* higher, higher or equal */
1484
1485 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1486 lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
1487 a_field_component = g_string_new("");
1488 if(filter->expression[i+1] == '=') { /* >= */
1489 i++;
1490 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GE);
1491 } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GT);
1492 break;
1493
1494 case '=': /* equal */
1495
1496 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1497 lttv_simple_expression_assign_field(a_field_path,a_simple_expression);
1498 a_field_component = g_string_new("");
1499 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_EQ);
1500 break;
1501
1502 /*
1503 * Field concatening caracter
1504 */
1505 case '.': /* dot */
1506
1507 /*
1508 * divide field expression into elements
1509 * in a_field_path array.
1510 *
1511 * A dot can also be present in double values
1512 */
1513 if(a_simple_expression->field == LTTV_FILTER_UNDEFINED) {
1514 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1515 a_field_component = g_string_new("");
1516 }
1517 break;
1518 case ' ': /* ignore */
1519 case '\n': /* ignore */
1520 break;
1521 default: /* concatening current string */
1522 g_string_append_c(a_field_component,filter->expression[i]);
1523 }
1524 }
1525
1526 /*
1527 * Preliminary check to see
1528 * if tree was constructed correctly
1529 */
1530 if( p_nesting>0 ) {
1531 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1532 is not valid due to parenthesis incorrect use",filter->expression);
1533 return FALSE;
1534 }
1535
1536 if(tree_stack->len != 1) /* only root tree should remain */
1537 return FALSE;
1538
1539 /*
1540 * processing last element of expression
1541 */
1542 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1543 while(t1->right != LTTV_TREE_IDLE) {
1544 g_assert(t1->right == LTTV_TREE_NODE);
1545 t1 = t1->r_child.t;
1546 }
1547 if(not) { // add not operator to tree
1548 t3 = lttv_filter_tree_new();
1549 t3->node = LTTV_LOGICAL_NOT;
1550 t1->right = LTTV_TREE_NODE;
1551 t1->r_child.t = t3;
1552 t1 = t3;
1553 not = 0;
1554 }
1555 if(subtree != NULL) { /* add the subtree */
1556 t1->right = LTTV_TREE_NODE;
1557 t1->r_child.t = subtree;
1558 subtree = NULL;
1559 } else { /* add a leaf */
1560 lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE));
1561 a_field_component = NULL;
1562 t1->right = LTTV_TREE_LEAF;
1563 t1->r_child.leaf = a_simple_expression;
1564 a_simple_expression = NULL;
1565 }
1566
1567
1568 /* free the pointer array */
1569 g_assert(a_field_path->len == 0);
1570 g_ptr_array_free(a_field_path,TRUE);
1571
1572 /* free the tree stack -- but keep the root tree */
1573 filter->head = g_ptr_array_remove_index(tree_stack,0);
1574 g_ptr_array_free(tree_stack,TRUE);
1575
1576 /* free the field buffer if allocated */
1577 if(a_field_component != NULL) g_string_free(a_field_component,TRUE);
1578
1579 /* free the simple expression buffer if allocated */
1580 if(a_simple_expression != NULL) lttv_simple_expression_destroy(a_simple_expression);
1581
1582 g_assert(filter->head != NULL); /* tree should exist */
1583 g_assert(subtree == NULL); /* remaining subtree should be included in main tree */
1584
1585 #ifdef TEST
1586 gettimeofday(&endtime, NULL);
1587
1588 /* Calcul du temps de l'algorithme */
1589 double time1 = starttime.tv_sec + (starttime.tv_usec/1000000.0);
1590 double time2 = endtime.tv_sec + (endtime.tv_usec/1000000.0);
1591 // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression));
1592 g_print("%.10f %i\n",(time2-time1)*1000,strlen(filter->expression));
1593 #endif
1594
1595 /* debug */
1596 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1597 lttv_print_tree(filter->head,0) ;
1598 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1599
1600 /* success */
1601 return TRUE;
1602
1603 }
1604
1605 /**
1606 * @fn void lttv_filter_destroy(LttvFilter*)
1607 *
1608 * Destroy the current LttvFilter
1609 * @param filter pointer to the current LttvFilter
1610 */
1611 void
1612 lttv_filter_destroy(LttvFilter* filter) {
1613
1614 if(!filter) return;
1615
1616 if(filter->expression)
1617 g_free(filter->expression);
1618 if(filter->head)
1619 lttv_filter_tree_destroy(filter->head);
1620 g_free(filter);
1621
1622 }
1623
1624 /**
1625 * @fn LttvFilterTree* lttv_filter_tree_new()
1626 *
1627 * Assign a new tree for the current expression
1628 * or sub expression
1629 * @return pointer of LttvFilterTree
1630 */
1631 LttvFilterTree*
1632 lttv_filter_tree_new() {
1633 LttvFilterTree* tree;
1634
1635 tree = g_new(LttvFilterTree,1);
1636 tree->node = 0; //g_new(lttv_expression,1);
1637 tree->left = LTTV_TREE_IDLE;
1638 tree->right = LTTV_TREE_IDLE;
1639 tree->r_child.t = NULL;
1640 tree->l_child.t = NULL;
1641
1642 return tree;
1643 }
1644
1645 /**
1646 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1647 *
1648 * Append a new expression to the expression
1649 * defined in the current filter
1650 * @param filter pointer to the current LttvFilter
1651 * @param expression string that must be appended
1652 * @return Success/Failure of operation
1653 */
1654 gboolean
1655 lttv_filter_append_expression(LttvFilter* filter, const char *expression) {
1656
1657 if(expression == NULL) return FALSE;
1658 if(filter == NULL) return FALSE;
1659 if(expression[0] == '\0') return FALSE; /* Empty expression */
1660
1661 GString* s = g_string_new("");
1662 if(filter->expression != NULL) {
1663 g_string_append(s,filter->expression);
1664 g_string_append_c(s,'&');
1665 }
1666 g_string_append(s,expression);
1667
1668 g_free(filter->expression);
1669 filter->expression = g_string_free(s,FALSE);
1670
1671 /* TRUE if construction of tree proceeded without errors */
1672 return lttv_filter_update(filter);
1673
1674 }
1675
1676 /**
1677 * @fn void lttv_filter_clear_expression(LttvFilter*)
1678 *
1679 * Clear the filter expression from the
1680 * current filter and sets its pointer to NULL
1681 * @param filter pointer to the current LttvFilter
1682 */
1683 void
1684 lttv_filter_clear_expression(LttvFilter* filter) {
1685
1686 if(filter->expression != NULL) {
1687 g_free(filter->expression);
1688 filter->expression = NULL;
1689 }
1690
1691 }
1692
1693 /**
1694 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1695 *
1696 * Destroys the tree and his sub-trees
1697 * @param tree Tree which must be destroyed
1698 */
1699 void
1700 lttv_filter_tree_destroy(LttvFilterTree* tree) {
1701
1702 if(tree == NULL) return;
1703
1704 if(tree->left == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->l_child.leaf);
1705 else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t);
1706
1707 if(tree->right == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->r_child.leaf);
1708 else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t);
1709
1710 // g_free(tree->node);
1711 g_free(tree);
1712 }
1713
1714 /**
1715 * Global parsing function for the current
1716 * LttvFilterTree
1717 * @param t pointer to the current LttvFilterTree
1718 * @param event current LttEvent, NULL if not used
1719 * @param tracefile current LttTracefile, NULL if not used
1720 * @param trace current LttTrace, NULL if not used
1721 * @param state current LttvProcessState, NULL if not used
1722 * @param context current LttvTracefileContext, NULL if not used
1723 * @return response of filter
1724 */
1725 gboolean
1726 lttv_filter_tree_parse(
1727 const LttvFilterTree* t,
1728 const LttEvent* event,
1729 const LttTracefile* tracefile,
1730 const LttTrace* trace,
1731 const LttvTracefileContext* context
1732 /*,...*/)
1733 {
1734
1735 /*
1736 * Each tree is parsed in inorder.
1737 * This way, it's possible to apply the left filter of the
1738 * tree, then decide whether or not the right branch should
1739 * be parsed depending on the linking logical operator
1740 *
1741 * Each node consists in a
1742 * 1. logical operator
1743 * 2. left child ( node or simple expression )
1744 * 3. right child ( node or simple expression )
1745 *
1746 * When the child is a simple expression, we must
1747 * before all determine if the expression refers to
1748 * a structure which is whithin observation ( not NULL ).
1749 * -If so, the expression is evaluated.
1750 * -If not, the result is set to TRUE since this particular
1751 * operation does not interfere with the lttv structure
1752 *
1753 * The result of each simple expression will directly
1754 * affect the next branch. This way, depending on
1755 * the linking logical operator, the parser will decide
1756 * to explore or not the next branch.
1757 * 1. AND OPERATOR
1758 * -If result of left branch is 0 / FALSE
1759 * then don't explore right branch and return 0;
1760 * -If result of left branch is 1 / TRUE then explore
1761 * 2. OR OPERATOR
1762 * -If result of left branch is 1 / TRUE
1763 * then don't explore right branch and return 1;
1764 * -If result of left branch is 0 / FALSE then explore
1765 * 3. XOR OPERATOR
1766 * -Result of left branch will not affect exploration of
1767 * right branch
1768 */
1769
1770 gboolean lresult = FALSE, rresult = FALSE;
1771
1772 LttvProcessState* state;
1773
1774 guint cpu = ltt_tracefile_num(context->tf);
1775 LttvTraceState *ts = (LttvTraceState*)context->t_context;
1776 state = ts->running_process[cpu];
1777
1778 /*
1779 * Parse left branch
1780 */
1781 if(t->left == LTTV_TREE_NODE) {
1782 lresult = lttv_filter_tree_parse(t->l_child.t,event,tracefile,trace,context);
1783 }
1784 else if(t->left == LTTV_TREE_LEAF) {
1785 lresult = lttv_filter_tree_parse_branch(t->l_child.leaf,event,tracefile,trace,state,context);
1786 }
1787
1788 /*
1789 * Parse linking operator
1790 * make a cutoff if possible
1791 */
1792 if((t->node & LTTV_LOGICAL_OR) && lresult == TRUE) return TRUE;
1793 if((t->node & LTTV_LOGICAL_AND) && lresult == FALSE) return FALSE;
1794
1795 /*
1796 * Parse right branch
1797 */
1798 if(t->right == LTTV_TREE_NODE) {
1799 rresult = lttv_filter_tree_parse(t->r_child.t,event,tracefile,trace,context);
1800 }
1801 else if(t->right == LTTV_TREE_LEAF) {
1802 rresult = lttv_filter_tree_parse_branch(t->r_child.leaf,event,tracefile,trace,state,context);
1803 }
1804
1805
1806 /*
1807 * Apply and return the
1808 * logical link between the
1809 * two operation
1810 */
1811 switch(t->node) {
1812 case LTTV_LOGICAL_OR: return (lresult | rresult);
1813 case LTTV_LOGICAL_AND: return (lresult & rresult);
1814 case LTTV_LOGICAL_NOT:
1815 return (t->left==LTTV_TREE_LEAF)?!lresult:((t->right==LTTV_TREE_LEAF)?!rresult:TRUE);
1816 case LTTV_LOGICAL_XOR: return (lresult ^ rresult);
1817 case 0: return (rresult);
1818 default:
1819 /*
1820 * This case should never be
1821 * parsed, if so, this subtree
1822 * is cancelled !
1823 */
1824 return TRUE;
1825 }
1826
1827 }
1828
1829 /**
1830 * This function parses a particular branch of the tree
1831 * @param se pointer to the current LttvSimpleExpression
1832 * @param event current LttEvent, NULL if not used
1833 * @param tracefile current LttTracefile, NULL if not used
1834 * @param trace current LttTrace, NULL if not used
1835 * @param state current LttvProcessState, NULL if not used
1836 * @param context current LttvTracefileContext, NULL if not used
1837 * @return response of filter
1838 */
1839 gboolean
1840 lttv_filter_tree_parse_branch(
1841 const LttvSimpleExpression* se,
1842 const LttEvent* event,
1843 const LttTracefile* tracefile,
1844 const LttTrace* trace,
1845 const LttvProcessState* state,
1846 const LttvTracefileContext* context) {
1847
1848 LttvFieldValue v;
1849 v = se->value;
1850 switch(se->field) {
1851 case LTTV_FILTER_TRACE_NAME:
1852 if(trace == NULL) return TRUE;
1853 else {
1854 GQuark quark = ltt_trace_name(trace);
1855 return se->op((gpointer)&quark,v);
1856 }
1857 break;
1858 case LTTV_FILTER_TRACEFILE_NAME:
1859 if(tracefile == NULL) return TRUE;
1860 else {
1861 GQuark quark = ltt_tracefile_name(tracefile);
1862 return se->op((gpointer)&quark,v);
1863 }
1864 break;
1865 case LTTV_FILTER_STATE_PID:
1866 if(state == NULL) return TRUE;
1867 else return se->op((gpointer)&state->pid,v);
1868 break;
1869 case LTTV_FILTER_STATE_PPID:
1870 if(state == NULL) return TRUE;
1871 else return se->op((gpointer)&state->ppid,v);
1872 break;
1873 case LTTV_FILTER_STATE_CT:
1874 if(state == NULL) return TRUE;
1875 else {
1876 return se->op((gpointer)&state->creation_time,v);
1877 }
1878 break;
1879 case LTTV_FILTER_STATE_IT:
1880 if(state == NULL) return TRUE;
1881 else {
1882 return se->op((gpointer)&state->insertion_time,v);
1883 }
1884 break;
1885 case LTTV_FILTER_STATE_P_NAME:
1886 /*
1887 * All 'unnamed' for the moment
1888 */
1889 if(state == NULL) return TRUE;
1890 else {
1891 GQuark quark = state->name;
1892 return se->op((gpointer)&quark,v);
1893 }
1894 break;
1895 case LTTV_FILTER_STATE_EX_MODE:
1896 if(state == NULL) return TRUE;
1897 else return se->op((gpointer)&state->state->t,v);
1898 break;
1899 case LTTV_FILTER_STATE_EX_SUBMODE:
1900 if(state == NULL) return TRUE;
1901 else return se->op((gpointer)&state->state->n,v);
1902 break;
1903 case LTTV_FILTER_STATE_P_STATUS:
1904 if(state == NULL) return TRUE;
1905 else return se->op((gpointer)&state->state->s,v);
1906 break;
1907 case LTTV_FILTER_STATE_CPU:
1908 if(context == NULL) return TRUE;
1909 else {
1910 /* FIXME: not sure of that one Mathieu : fixed.*/
1911 // GQuark quark = ((LttvTracefileState*)context)->cpu_name;
1912 // return se->op((gpointer)&quark,v);
1913 if(state == NULL) return TRUE;
1914 else return se->op((gpointer)&state->cpu,v);
1915 }
1916 break;
1917 case LTTV_FILTER_EVENT_NAME:
1918 if(event == NULL) return TRUE;
1919 else {
1920 LttEventType* et;
1921 et = ltt_event_eventtype(event);
1922 GQuark quark = ltt_eventtype_name(et);
1923 return se->op((gpointer)&quark,v);
1924 }
1925 break;
1926
1927 case LTTV_FILTER_EVENT_CATEGORY:
1928 /*
1929 * TODO: Not yet implemented
1930 */
1931 return TRUE;
1932 break;
1933 case LTTV_FILTER_EVENT_TIME:
1934 if(event == NULL) return TRUE;
1935 else {
1936 LttTime time = ltt_event_time(event);
1937 return se->op((gpointer)&time,v);
1938 }
1939 break;
1940 case LTTV_FILTER_EVENT_TSC:
1941 if(event == NULL) return TRUE;
1942 else {
1943 LttCycleCount count = ltt_event_cycle_count(event);
1944 return se->op((gpointer)&count,v);
1945 }
1946 break;
1947 case LTTV_FILTER_EVENT_FIELD:
1948 /*
1949 * TODO: Use the offset to
1950 * find the dynamic field
1951 * in the event struct
1952 */
1953 return TRUE;
1954 default:
1955 /*
1956 * This case should never be
1957 * parsed, if so, the whole
1958 * filtering is cancelled
1959 */
1960 g_warning("Error while parsing the filter tree");
1961 return TRUE;
1962 }
1963
1964 /* should never get here */
1965 return TRUE;
1966
1967 }
1968
1969
1970
1971 /**
1972 * Debug function. Prints tree memory allocation.
1973 * @param t the pointer to the current LttvFilterTree
1974 */
1975 void
1976 lttv_print_tree(const LttvFilterTree* t, const int count) {
1977
1978 g_print("node:%p lchild:%p rchild:%p depth:%i\n",t, //t->l_child.t,t->r_child.t);
1979 (t->left==LTTV_TREE_NODE)?t->l_child.t:NULL,
1980 (t->right==LTTV_TREE_NODE)?t->r_child.t:NULL,
1981 count);
1982 g_print("logic operator: %s\n",(t->node&1)?"OR":((t->node&2)?"AND":((t->node&4)?"NOT":((t->node&8)?"XOR":"IDLE"))));
1983 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"));
1984 if(t->left == LTTV_TREE_LEAF) {
1985 g_print("| |-> field type number: %i\n",t->l_child.leaf->field);
1986 g_print("| |-> offset is: %i\n",t->l_child.leaf->offset);
1987 g_print("| |-> operator function is: %p\n",t->l_child.leaf->op);
1988 }
1989 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"));
1990 if(t->right == LTTV_TREE_LEAF) {
1991 g_print("| |-> field type number: %i\n",t->r_child.leaf->field);
1992 g_print("| |-> offset is: %i\n",t->r_child.leaf->offset);
1993 g_print("| |-> operator function is: %p\n",t->r_child.leaf->op);
1994 }
1995
1996 if(t->left == LTTV_TREE_NODE) lttv_print_tree(t->l_child.t,count+1);
1997 if(t->right == LTTV_TREE_NODE) lttv_print_tree(t->r_child.t,count+1);
1998 }
1999
2000 /**
2001 * @fn static void module_init()
2002 *
2003 * Initializes the filter module and specific values
2004 */
2005 static void module_init()
2006 {
2007
2008 }
2009
2010 /**
2011 * Destroys the filter module and specific values
2012 */
2013 static void module_destroy()
2014 {
2015
2016 }
2017
2018
2019 LTTV_MODULE("filter", "Filters traceset and events", \
2020 "Filters traceset and events specifically to user input", \
2021 module_init, module_destroy)
2022
2023
2024
This page took 0.077626 seconds and 4 git commands to generate.