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