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