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