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