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