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