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