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