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_add_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 assignment ! Bad field type ...");
384 return FALSE;
385 }
386
387 return TRUE;
388
389 }
390
391 /**
392 * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*)
393 *
394 * Desallocate memory for the current
395 * simple expression
396 * @param se pointer to the current LttvSimpleExpression
397 */
398 void
399 lttv_simple_expression_destroy(LttvSimpleExpression* se) {
400
401 g_free(se->value);
402 g_free(se);
403
404 }
405
406 /**
407 * @fn gint lttv_struct_type(gint)
408 *
409 * Finds the structure type depending
410 * on the fields in parameters
411 * @params ft Field of the current structure
412 * @return LttvStructType enum or -1 for error
413 */
414 gint
415 lttv_struct_type(gint ft) {
416
417 switch(ft) {
418 case LTTV_FILTER_TRACE_NAME:
419 return LTTV_FILTER_TRACE;
420 break;
421 case LTTV_FILTER_TRACEFILE_NAME:
422 return LTTV_FILTER_TRACEFILE;
423 break;
424 case LTTV_FILTER_STATE_PID:
425 case LTTV_FILTER_STATE_PPID:
426 case LTTV_FILTER_STATE_CT:
427 case LTTV_FILTER_STATE_IT:
428 case LTTV_FILTER_STATE_P_NAME:
429 case LTTV_FILTER_STATE_EX_MODE:
430 case LTTV_FILTER_STATE_EX_SUBMODE:
431 case LTTV_FILTER_STATE_P_STATUS:
432 case LTTV_FILTER_STATE_CPU:
433 return LTTV_FILTER_STATE;
434 break;
435 case LTTV_FILTER_EVENT_NAME:
436 case LTTV_FILTER_EVENT_CATEGORY:
437 case LTTV_FILTER_EVENT_TIME:
438 case LTTV_FILTER_EVENT_TSC:
439 case LTTV_FILTER_EVENT_FIELD:
440 return LTTV_FILTER_EVENT;
441 break;
442 default:
443 return -1;
444 }
445 }
446
447 /**
448 * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue)
449 *
450 * Applies the 'equal' operator to the
451 * specified structure and value
452 * @param v1 left member of comparison
453 * @param v2 right member of comparison
454 * @return success/failure of operation
455 */
456 gboolean lttv_apply_op_eq_uint64(gpointer v1, char* v2) {
457
458 guint64* r = (guint64*) v1;
459 guint64 l = atoi(v2);
460 return (*r == l);
461
462 }
463
464 /**
465 * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue)
466 *
467 * Applies the 'equal' operator to the
468 * specified structure and value
469 * @param v1 left member of comparison
470 * @param v2 right member of comparison
471 * @return success/failure of operation
472 */
473 gboolean lttv_apply_op_eq_uint32(gpointer v1, char* v2) {
474 guint32* r = (guint32*) v1;
475 guint32 l = atoi(v2);
476 return (*r == l);
477 }
478
479 /**
480 * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue)
481 *
482 * Applies the 'equal' operator to the
483 * specified structure and value
484 * @param v1 left member of comparison
485 * @param v2 right member of comparison
486 * @return success/failure of operation
487 */
488 gboolean lttv_apply_op_eq_uint16(gpointer v1, char* v2) {
489 guint16* r = (guint16*) v1;
490 guint16 l = atoi(v2);
491 return (*r == l);
492 }
493
494 /**
495 * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue)
496 *
497 * Applies the 'equal' operator to the
498 * specified structure and value
499 * @param v1 left member of comparison
500 * @param v2 right member of comparison
501 * @return success/failure of operation
502 */
503 gboolean lttv_apply_op_eq_double(gpointer v1, char* v2) {
504 double* r = (double*) v1;
505 double l = atof(v2);
506 return (*r == l);
507 }
508
509 /**
510 * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue)
511 *
512 * Applies the 'equal' operator to the
513 * specified structure and value
514 * @param v1 left member of comparison
515 * @param v2 right member of comparison
516 * @return success/failure of operation
517 */
518 gboolean lttv_apply_op_eq_string(gpointer v1, char* v2) {
519 char* r = (char*) v1;
520 return (!g_strcasecmp(r,v2));
521 }
522
523 /**
524 * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue)
525 *
526 * Applies the 'not equal' operator to the
527 * specified structure and value
528 * @param v1 left member of comparison
529 * @param v2 right member of comparison
530 * @return success/failure of operation
531 */
532 gboolean lttv_apply_op_ne_uint64(gpointer v1, char* v2) {
533 guint64* r = (guint64*) v1;
534 guint64 l = atoi(v2);
535 return (*r != l);
536 }
537
538 /**
539 * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue)
540 *
541 * Applies the 'not equal' operator to the
542 * specified structure and value
543 * @param v1 left member of comparison
544 * @param v2 right member of comparison
545 * @return success/failure of operation
546 */
547 gboolean lttv_apply_op_ne_uint32(gpointer v1, char* v2) {
548 guint32* r = (guint32*) v1;
549 guint32 l = atoi(v2);
550 return (*r != l);
551 }
552
553 /**
554 * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue)
555 *
556 * Applies the 'not equal' operator to the
557 * specified structure and value
558 * @param v1 left member of comparison
559 * @param v2 right member of comparison
560 * @return success/failure of operation
561 */
562 gboolean lttv_apply_op_ne_uint16(gpointer v1, char* v2) {
563 guint16* r = (guint16*) v1;
564 guint16 l = atoi(v2);
565 return (*r != l);
566 }
567
568 /**
569 * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue)
570 *
571 * Applies the 'not 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 */
577 gboolean lttv_apply_op_ne_double(gpointer v1, char* v2) {
578 double* r = (double*) v1;
579 double l = atof(v2);
580 return (*r != l);
581 }
582
583 /**
584 * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue)
585 *
586 * Applies the 'not 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 */
592 gboolean lttv_apply_op_ne_string(gpointer v1, char* v2) {
593 char* r = (char*) v1;
594 return (g_strcasecmp(r,v2));
595 }
596
597 /**
598 * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue)
599 *
600 * Applies the 'lower than' operator to the
601 * specified structure and value
602 * @param v1 left member of comparison
603 * @param v2 right member of comparison
604 * @return success/failure of operation
605 */
606 gboolean lttv_apply_op_lt_uint64(gpointer v1, char* v2) {
607 guint64* r = (guint64*) v1;
608 guint64 l = atoi(v2);
609 return (*r < l);
610 }
611
612 /**
613 * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue)
614 *
615 * Applies the 'lower than' 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_lt_uint32(gpointer v1, char* v2) {
622 guint32* r = (guint32*) v1;
623 guint32 l = atoi(v2);
624 return (*r < l);
625 }
626
627 /**
628 * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue)
629 *
630 * Applies the 'lower than' operator to the
631 * specified structure and value
632 * @param v1 left member of comparison
633 * @param v2 right member of comparison
634 * @return success/failure of operation
635 */
636 gboolean lttv_apply_op_lt_uint16(gpointer v1, char* v2) {
637 guint16* r = (guint16*) v1;
638 guint16 l = atoi(v2);
639 return (*r < l);
640 }
641
642 /**
643 * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue)
644 *
645 * Applies the 'lower than' operator to the
646 * specified structure and value
647 * @param v1 left member of comparison
648 * @param v2 right member of comparison
649 * @return success/failure of operation
650 */
651 gboolean lttv_apply_op_lt_double(gpointer v1, char* v2) {
652 double* r = (double*) v1;
653 double l = atof(v2);
654 return (*r < l);
655 }
656
657 /**
658 * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue)
659 *
660 * Applies the 'lower or equal' operator to the
661 * specified structure and value
662 * @param v1 left member of comparison
663 * @param v2 right member of comparison
664 * @return success/failure of operation
665 */
666 gboolean lttv_apply_op_le_uint64(gpointer v1, char* v2) {
667 guint64* r = (guint64*) v1;
668 guint64 l = atoi(v2);
669 return (*r <= l);
670 }
671
672 /**
673 * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue)
674 *
675 * Applies the 'lower or equal' operator to the
676 * specified structure and value
677 * @param v1 left member of comparison
678 * @param v2 right member of comparison
679 * @return success/failure of operation
680 */
681 gboolean lttv_apply_op_le_uint32(gpointer v1, char* v2) {
682 guint32* r = (guint32*) v1;
683 guint32 l = atoi(v2);
684 return (*r <= l);
685 }
686
687 /**
688 * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue)
689 *
690 * Applies the 'lower or equal' operator to the
691 * specified structure and value
692 * @param v1 left member of comparison
693 * @param v2 right member of comparison
694 * @return success/failure of operation
695 */
696 gboolean lttv_apply_op_le_uint16(gpointer v1, char* v2) {
697 guint16* r = (guint16*) v1;
698 guint16 l = atoi(v2);
699 return (*r <= l);
700 }
701
702 /**
703 * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue)
704 *
705 * Applies the 'lower or equal' operator to the
706 * specified structure and value
707 * @param v1 left member of comparison
708 * @param v2 right member of comparison
709 * @return success/failure of operation
710 */
711 gboolean lttv_apply_op_le_double(gpointer v1, char* v2) {
712 double* r = (double*) v1;
713 double l = atof(v2);
714 return (*r <= l);
715 }
716
717 /**
718 * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue)
719 *
720 * Applies the 'greater than' operator to the
721 * specified structure and value
722 * @param v1 left member of comparison
723 * @param v2 right member of comparison
724 * @return success/failure of operation
725 */
726 gboolean lttv_apply_op_gt_uint64(gpointer v1, char* v2) {
727 guint64* r = (guint64*) v1;
728 guint64 l = atoi(v2);
729 return (*r > l);
730 }
731
732 /**
733 * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue)
734 *
735 * Applies the 'greater than' operator to the
736 * specified structure and value
737 * @param v1 left member of comparison
738 * @param v2 right member of comparison
739 * @return success/failure of operation
740 */
741 gboolean lttv_apply_op_gt_uint32(gpointer v1, char* v2) {
742 guint32* r = (guint32*) v1;
743 guint32 l = atoi(v2);
744 return (*r > l);
745 }
746
747 /**
748 * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue)
749 *
750 * Applies the 'greater than' operator to the
751 * specified structure and value
752 * @param v1 left member of comparison
753 * @param v2 right member of comparison
754 * @return success/failure of operation
755 */
756 gboolean lttv_apply_op_gt_uint16(gpointer v1, char* v2) {
757 guint16* r = (guint16*) v1;
758 guint16 l = atoi(v2);
759 return (*r > l);
760 }
761
762 /**
763 * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue)
764 *
765 * Applies the 'greater than' operator to the
766 * specified structure and value
767 * @param v1 left member of comparison
768 * @param v2 right member of comparison
769 * @return success/failure of operation
770 */
771 gboolean lttv_apply_op_gt_double(gpointer v1, char* v2) {
772 double* r = (double*) v1;
773 double l = atof(v2);
774 return (*r > l);
775 }
776
777 /**
778 * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue)
779 *
780 * Applies the 'greater or equal' operator to the
781 * specified structure and value
782 * @param v1 left member of comparison
783 * @param v2 right member of comparison
784 * @return success/failure of operation
785 */
786 gboolean lttv_apply_op_ge_uint64(gpointer v1, char* v2) {
787 guint64* r = (guint64*) v1;
788 guint64 l = atoi(v2);
789 return (*r >= l);
790 }
791
792 /**
793 * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue)
794 *
795 * Applies the 'greater or equal' operator to the
796 * specified structure and value
797 * @param v1 left member of comparison
798 * @param v2 right member of comparison
799 * @return success/failure of operation
800 */
801 gboolean lttv_apply_op_ge_uint32(gpointer v1, char* v2) {
802 guint32* r = (guint32*) v1;
803 guint32 l = atoi(v2);
804 return (*r >= l);
805 }
806
807 /**
808 * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue)
809 *
810 * Applies the 'greater or equal' operator to the
811 * specified structure and value
812 * @param v1 left member of comparison
813 * @param v2 right member of comparison
814 * @return success/failure of operation
815 */
816 gboolean lttv_apply_op_ge_uint16(gpointer v1, char* v2) {
817 guint16* r = (guint16*) v1;
818 guint16 l = atoi(v2);
819 return (*r >= l);
820 }
821
822 /**
823 * @fn gboolean lttv_apply_op_ge_double(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_double(gpointer v1, char* v2) {
832 double* r = (double*) v1;
833 double l = atof(v2);
834 return (*r >= l);
835 }
836
837
838 /**
839 * @fn LttvFilterTree* lttv_filter_tree_clone(LttvFilterTree*)
840 *
841 * Makes a copy of the current filter tree
842 * @param tree pointer to the current tree
843 * @return new copy of the filter tree
844 */
845 LttvFilterTree*
846 lttv_filter_tree_clone(LttvFilterTree* tree) {
847
848 LttvFilterTree* newtree = lttv_filter_tree_new();
849
850 newtree->node = tree->node;
851
852 newtree->left = tree->left;
853 if(newtree->left == LTTV_TREE_NODE) {
854 newtree->l_child.t = lttv_filter_tree_clone(tree->l_child.t);
855 } else if(newtree->left == LTTV_TREE_LEAF) {
856 newtree->l_child.leaf = lttv_simple_expression_new();
857 newtree->l_child.leaf->field = tree->l_child.leaf->field;
858 newtree->l_child.leaf->offset = tree->l_child.leaf->offset;
859 newtree->l_child.leaf->op = tree->l_child.leaf->op;
860 newtree->l_child.leaf->value = g_strconcat(tree->l_child.leaf->value);
861 }
862
863 newtree->right = tree->right;
864 if(newtree->right == LTTV_TREE_NODE) {
865 newtree->r_child.t = lttv_filter_tree_clone(tree->r_child.t);
866 } else if(newtree->right == LTTV_TREE_LEAF) {
867 newtree->r_child.leaf = lttv_simple_expression_new();
868 newtree->r_child.leaf->field = tree->r_child.leaf->field;
869 newtree->r_child.leaf->offset = tree->r_child.leaf->offset;
870 newtree->r_child.leaf->op = tree->r_child.leaf->op;
871 newtree->r_child.leaf->value = g_strconcat(tree->r_child.leaf->value);
872 }
873
874 return newtree;
875
876 }
877
878 /**
879 * @fn LttvFilter* lttv_filter_clone(LttvFilter*)
880 *
881 * Makes a copy of the current filter
882 * @param filter pointer to the current filter
883 * @return new copy of the filter
884 */
885 LttvFilter*
886 lttv_filter_clone(LttvFilter* filter) {
887
888
889 LttvFilter* newfilter = g_new(LttvFilter,1);
890
891 // newfilter->expression = g_new(char,1)
892 strcpy(newfilter->expression,filter->expression);
893
894 newfilter->head = lttv_filter_tree_clone(filter->head);
895
896 return newfilter;
897
898 }
899
900
901 /**
902 * @fn LttvFilter* lttv_filter_new()
903 *
904 * Creates a new lttv_filter
905 * @param expression filtering options string
906 * @param t pointer to the current LttvTrace
907 * @return the current lttv_filter or NULL if error
908 */
909 LttvFilter*
910 lttv_filter_new() {
911
912 LttvFilter* filter = g_new(LttvFilter,1);
913 filter->expression = NULL;
914 filter->head = NULL;
915
916 }
917
918 /**
919 * @fn gboolean lttv_filter_update(LttvFilter*)
920 *
921 * Updates the current LttvFilter by building
922 * its tree based upon the expression string
923 * @param filter pointer to the current LttvFilter
924 * @return Failure/Success of operation
925 */
926 gboolean
927 lttv_filter_update(LttvFilter* filter) {
928
929 g_print("filter::lttv_filter_new()\n"); /* debug */
930
931 if(filter->expression == NULL) return FALSE;
932
933 unsigned
934 i,
935 p_nesting=0; /* parenthesis nesting value */
936
937 /* trees */
938 LttvFilterTree
939 *tree = lttv_filter_tree_new(), /* main tree */
940 *subtree = NULL, /* buffer for subtrees */
941 *t1, /* buffer #1 */
942 *t2; /* buffer #2 */
943
944 /*
945 * the filter
946 * If the tree already exists,
947 * destroy it and build a new one
948 */
949 if(filter->head != NULL) lttv_filter_tree_destroy(filter->head);
950 filter->head = tree;
951
952 /*
953 * Tree Stack
954 * each element of the list
955 * is a sub tree created
956 * by the use of parenthesis in the
957 * global expression. The final tree
958 * will be the one left at the root of
959 * the list
960 */
961 GPtrArray *tree_stack = g_ptr_array_new();
962 g_ptr_array_add( tree_stack,(gpointer) tree );
963
964 /* temporary values */
965 GString *a_field_component = g_string_new("");
966 GPtrArray *a_field_path = g_ptr_array_new();
967
968 /* simple expression buffer */
969 LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new();
970
971 /*
972 * Parse entire expression and construct
973 * the binary tree. There are two steps
974 * in browsing that string
975 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
976 * 2. finding simple expressions
977 * - field path ( separated by dots )
978 * - op ( >, <, =, >=, <=, !=)
979 * - value ( integer, string ... )
980 * To spare computing time, the whole
981 * string is parsed in this loop for a
982 * O(n) complexity order.
983 *
984 * When encountering logical op &,|,^
985 * 1. parse the last value if any
986 * 2. create a new tree
987 * 3. add the expression (simple exp, or exp (subtree)) to the tree
988 * 4. concatenate this tree with the current tree on top of the stack
989 * When encountering math ops >,>=,<,<=,=,!=
990 * 1. add to op to the simple expression
991 * 2. concatenate last field component to field path
992 * When encountering concatening ops .
993 * 1. concatenate last field component to field path
994 * When encountering opening parenthesis (,{,[
995 * 1. create a new subtree on top of tree stack
996 * When encountering closing parenthesis ),},]
997 * 1. add the expression on right child of the current tree
998 * 2. the subtree is completed, allocate a new subtree
999 * 3. pop the tree value from the tree stack
1000 */
1001
1002 g_print("expression: %s\n",filter->expression);
1003 g_print("strlen(expression): %i\n",strlen(filter->expression));
1004 for(i=0;i<strlen(filter->expression);i++) {
1005 // debug
1006 g_print("%c ",filter->expression[i]);
1007 switch(filter->expression[i]) {
1008 /*
1009 * logical operators
1010 */
1011 case '&': /* and */
1012
1013 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1014 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1015 t2 = lttv_filter_tree_new();
1016 t2->node = LTTV_LOGICAL_AND;
1017 if(subtree != NULL) { /* append subtree to current tree */
1018 t2->left = LTTV_TREE_NODE;
1019 t2->l_child.t = subtree;
1020 subtree = NULL;
1021 t1->right = LTTV_TREE_NODE;
1022 t1->r_child.t = t2;
1023 } else { /* append a simple expression */
1024 a_simple_expression->value = g_string_free(a_field_component,FALSE);
1025 a_field_component = g_string_new("");
1026 t2->left = LTTV_TREE_LEAF;
1027 t2->l_child.leaf = a_simple_expression;
1028 a_simple_expression = lttv_simple_expression_new();
1029 t1->right = LTTV_TREE_NODE;
1030 t1->r_child.t = t2;
1031 }
1032 break;
1033
1034 case '|': /* or */
1035
1036 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1037 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1038 t2 = lttv_filter_tree_new();
1039 t2->node = LTTV_LOGICAL_OR;
1040 if(subtree != NULL) { /* append subtree to current tree */
1041 t2->left = LTTV_TREE_NODE;
1042 t2->l_child.t = subtree;
1043 subtree = NULL;
1044 t1->right = LTTV_TREE_NODE;
1045 t1->r_child.t = t2;
1046 } else { /* append a simple expression */
1047 a_simple_expression->value = g_string_free(a_field_component,FALSE);
1048 a_field_component = g_string_new("");
1049 t2->left = LTTV_TREE_LEAF;
1050 t2->l_child.leaf = a_simple_expression;
1051 a_simple_expression = lttv_simple_expression_new();
1052 t1->right = LTTV_TREE_NODE;
1053 t1->r_child.t = t2;
1054 }
1055 break;
1056
1057 case '^': /* xor */
1058
1059 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1060 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1061 t2 = lttv_filter_tree_new();
1062 t2->node = LTTV_LOGICAL_XOR;
1063 if(subtree != NULL) { /* append subtree to current tree */
1064 t2->left = LTTV_TREE_NODE;
1065 t2->l_child.t = subtree;
1066 subtree = NULL;
1067 t1->right = LTTV_TREE_NODE;
1068 t1->r_child.t = t2;
1069 } else { /* append a simple expression */
1070 a_simple_expression->value = g_string_free(a_field_component,FALSE);
1071 a_field_component = g_string_new("");
1072 t2->left = LTTV_TREE_LEAF;
1073 t2->l_child.leaf = a_simple_expression;
1074 a_simple_expression = lttv_simple_expression_new();
1075 t1->right = LTTV_TREE_NODE;
1076 t1->r_child.t = t2;
1077 }
1078 break;
1079
1080 case '!': /* not, or not equal (math op) */
1081
1082 if(filter->expression[i+1] == '=') { /* != */
1083 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1084 lttv_simple_expression_add_field(a_field_path,a_simple_expression);
1085 a_field_component = g_string_new("");
1086 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_NE);
1087 i++;
1088 } else { /* ! */
1089 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
1090 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1091 t2 = lttv_filter_tree_new();
1092 t2->node = LTTV_LOGICAL_NOT;
1093 t1->right = LTTV_TREE_NODE;
1094 t1->r_child.t = t2;
1095 }
1096 break;
1097
1098 case '(': /* start of parenthesis */
1099 case '[':
1100 case '{':
1101
1102 p_nesting++; /* incrementing parenthesis nesting value */
1103 t1 = lttv_filter_tree_new();
1104 g_ptr_array_add( tree_stack,(gpointer) t1 );
1105 break;
1106
1107 case ')': /* end of parenthesis */
1108 case ']':
1109 case '}':
1110
1111 p_nesting--; /* decrementing parenthesis nesting value */
1112 if(p_nesting<0 || tree_stack->len<2) {
1113 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1114 is not valid due to parenthesis incorrect use",filter->expression);
1115 return FALSE;
1116 }
1117
1118 /* there must at least be the root tree left in the array */
1119 g_assert(tree_stack->len>0);
1120
1121 if(subtree != NULL) { /* append subtree to current tree */
1122 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1123 while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) {
1124 g_assert(t1!=NULL && t1->r_child.t != NULL);
1125 t1 = t1->r_child.t;
1126 }
1127 t1->right = LTTV_TREE_NODE;
1128 t1->r_child.t = subtree;
1129 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
1130 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
1131 } else { /* assign subtree as current tree */
1132 a_simple_expression->value = g_string_free(a_field_component,FALSE);
1133 a_field_component = g_string_new("");
1134 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1135 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1136 t1->right = LTTV_TREE_LEAF;
1137 t1->r_child.leaf = a_simple_expression;
1138 a_simple_expression = lttv_simple_expression_new();
1139 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
1140 g_assert(subtree != NULL);
1141 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
1142 }
1143 break;
1144
1145 /*
1146 * mathematic operators
1147 */
1148 case '<': /* lower, lower or equal */
1149
1150 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1151 lttv_simple_expression_add_field(a_field_path,a_simple_expression);
1152 a_field_component = g_string_new("");
1153 if(filter->expression[i+1] == '=') { /* <= */
1154 i++;
1155 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LE);
1156 } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LT);
1157 break;
1158
1159 case '>': /* higher, higher or equal */
1160
1161 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1162 lttv_simple_expression_add_field(a_field_path,a_simple_expression);
1163 a_field_component = g_string_new("");
1164 if(filter->expression[i+1] == '=') { /* >= */
1165 i++;
1166 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GE);
1167 } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GT);
1168 break;
1169
1170 case '=': /* equal */
1171
1172 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1173 lttv_simple_expression_add_field(a_field_path,a_simple_expression);
1174 a_field_component = g_string_new("");
1175 lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_EQ);
1176 break;
1177
1178 /*
1179 * Field concatening caracter
1180 */
1181 case '.': /* dot */
1182
1183 /*
1184 * divide field expression into elements
1185 * in a_field_path array.
1186 */
1187 /* FIXME: check for double values */
1188 // if(a_simple_expression->op == NULL) {
1189 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1190 a_field_component = g_string_new("");
1191 // }
1192 break;
1193 // case ' ':
1194 // case '\n':
1195 // ignore
1196 default: /* concatening current string */
1197 g_string_append_c(a_field_component,filter->expression[i]);
1198 }
1199 }
1200
1201 g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2);
1202 g_print("stack size: %i\n",tree_stack->len);
1203
1204 /*
1205 * Preliminary check to see
1206 * if tree was constructed correctly
1207 */
1208 if( p_nesting>0 ) {
1209 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1210 is not valid due to parenthesis incorrect use",filter->expression);
1211 return FALSE;
1212 }
1213
1214 if(tree_stack->len != 1) /* only root tree should remain */
1215 return FALSE;
1216
1217 /* processing last element of expression */
1218 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1219 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1220 if(subtree != NULL) { /* add the subtree */
1221 g_print("there right!\n");
1222 t1->right = LTTV_TREE_NODE;
1223 t1->r_child.t = subtree;
1224 subtree = NULL;
1225 } else { /* add a leaf */
1226 a_simple_expression->value = g_string_free(a_field_component,FALSE);
1227 a_field_component = NULL;
1228 t1->right = LTTV_TREE_LEAF;
1229 t1->r_child.leaf = a_simple_expression;
1230 a_simple_expression = NULL;
1231 }
1232
1233
1234 /* free the pointer array */
1235 g_assert(a_field_path->len == 0);
1236 g_ptr_array_free(a_field_path,TRUE);
1237
1238 /* free the tree stack -- but keep the root tree */
1239 g_ptr_array_free(tree_stack,FALSE);
1240
1241 /* free the field buffer if allocated */
1242 if(a_field_component != NULL) g_string_free(a_field_component,TRUE);
1243
1244 /* free the simple expression buffer if allocated */
1245 if(a_simple_expression != NULL) lttv_simple_expression_destroy(a_simple_expression);
1246
1247 g_assert(tree != NULL); /* tree should exist */
1248 g_assert(subtree == NULL); /* remaining subtree should be included in main tree */
1249
1250 /* debug */
1251 g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n");
1252 lttv_print_tree(filter->head) ;
1253 g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n");
1254
1255 /* success */
1256 return TRUE;
1257
1258 }
1259
1260 /**
1261 * @fn void lttv_filter_destroy(LttvFilter*)
1262 *
1263 * Destroy the current LttvFilter
1264 * @param filter pointer to the current LttvFilter
1265 */
1266 void
1267 lttv_filter_destroy(LttvFilter* filter) {
1268
1269 g_free(filter->expression);
1270 lttv_filter_tree_destroy(filter->head);
1271 g_free(filter);
1272
1273 }
1274
1275 /**
1276 * LttvFilterTree* lttv_filter_tree_new()
1277 *
1278 * Assign a new tree for the current expression
1279 * or sub expression
1280 * @return pointer of LttvFilterTree
1281 */
1282 LttvFilterTree*
1283 lttv_filter_tree_new() {
1284 LttvFilterTree* tree;
1285
1286 tree = g_new(LttvFilter,1);
1287 tree->node = 0; //g_new(lttv_expression,1);
1288 tree->left = LTTV_TREE_IDLE;
1289 tree->right = LTTV_TREE_IDLE;
1290
1291 return tree;
1292 }
1293
1294 /**
1295 * @fn void lttv_filter_append_expression(LttvFilter*,char*)
1296 *
1297 * Append a new expression to the expression
1298 * defined in the current filter
1299 * @param filter pointer to the current LttvFilter
1300 * @param expression string that must be appended
1301 * @return Success/Failure of operation
1302 */
1303 gboolean lttv_filter_append_expression(LttvFilter* filter, char *expression) {
1304
1305 if(expression == NULL) return FALSE;
1306 if(filter == NULL) {
1307 filter = lttv_filter_new();
1308 filter->expression = expression;
1309 } else if(filter->expression == NULL) {
1310 filter->expression = expression;
1311 } else {
1312 filter->expression = g_strconcat(filter->expression,"&",expression);
1313 }
1314
1315 return lttv_filter_update(filter);
1316
1317 }
1318
1319 /**
1320 * @fn void lttv_filter_clear_expression(LttvFilter*)
1321 *
1322 * Clear the filter expression from the
1323 * current filter and sets its pointer to NULL
1324 * @param filter pointer to the current LttvFilter
1325 */
1326 void lttv_filter_clear_expression(LttvFilter* filter) {
1327
1328 if(filter->expression != NULL) {
1329 g_free(filter->expression);
1330 filter->expression = NULL;
1331 }
1332
1333 }
1334
1335 /**
1336 * @fn void lttv_filter_tree_destroy(LttvFilterTree*)
1337 *
1338 * Destroys the tree and his sub-trees
1339 * @param tree Tree which must be destroyed
1340 */
1341 void
1342 lttv_filter_tree_destroy(LttvFilterTree* tree) {
1343
1344 if(tree == NULL) return;
1345
1346 if(tree->left == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->l_child.leaf);
1347 else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t);
1348
1349 if(tree->right == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->r_child.leaf);
1350 else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t);
1351
1352 g_free(tree->node);
1353 g_free(tree);
1354 }
1355
1356 /**
1357 * gboolean lttv_filter_tree_parse(LttvFilterTree*,LttEvent,LttTracefile,LttTrace,LttvProcessState)
1358 *
1359 * Global parsing function for the current
1360 * LttvFilterTree
1361 * @param tree pointer to the current LttvFilterTree
1362 * @param event current LttEvent, NULL if not used
1363 * @param tracefile current LttTracefile, NULL if not used
1364 * @param trace current LttTrace, NULL if not used
1365 * @param state current LttvProcessState, NULL if not used
1366 */
1367 gboolean
1368 lttv_filter_tree_parse(
1369 LttvFilterTree* t,
1370 LttEvent* event,
1371 LttTracefile* tracefile,
1372 LttTrace* trace,
1373 LttvProcessState* state
1374 /*,...*/)
1375 {
1376
1377 /*
1378 * Each tree is parsed in inorder.
1379 * This way, it's possible to apply the left filter of the
1380 * tree, then decide whether or not the right branch should
1381 * be parsed depending on the linking logical operator
1382 *
1383 * Each node consists in a
1384 * 1. logical operator
1385 * 2. left child ( node or simple expression )
1386 * 3. right child ( node or simple expression )
1387 *
1388 * When the child is a simple expression, we must
1389 * before all determine if the expression refers to
1390 * a structure which is whithin observation ( not NULL ).
1391 * -If so, the expression is evaluated.
1392 * -If not, the result is set to TRUE since this particular
1393 * operation does not interfere with the lttv structure
1394 *
1395 * The result of each simple expression will directly
1396 * affect the next branch. This way, depending on
1397 * the linking logical operator, the parser will decide
1398 * to explore or not the next branch.
1399 * 1. AND OPERATOR
1400 * -If result of left branch is 0 / FALSE
1401 * then don't explore right branch and return 0;
1402 * -If result of left branch is 1 / TRUE then explore
1403 * 2. OR OPERATOR
1404 * -If result of left branch is 1 / TRUE
1405 * then don't explore right branch and return 1;
1406 * -If result of left branch is 0 / FALSE then explore
1407 * 3. XOR OPERATOR
1408 * -Result of left branch will not affect exploration of
1409 * right branch
1410 */
1411 g_print("filter::lttv_parse_tree(...)\n");
1412
1413 gboolean lresult = FALSE, rresult = FALSE;
1414
1415 /*
1416 * Parse left branch
1417 */
1418 if(t->left == LTTV_TREE_NODE) lresult = lttv_filter_tree_parse(t->l_child.t,event,tracefile,trace,state);
1419 else if(t->left == LTTV_TREE_LEAF) {
1420 //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);
1421 char* v;
1422 g_assert(v = t->l_child.leaf->value);
1423 switch(t->l_child.leaf->field) {
1424
1425 case LTTV_FILTER_TRACE_NAME:
1426 if(trace == NULL) lresult = TRUE;
1427 else lresult = t->l_child.leaf->op((gpointer)ltt_trace_name(trace),v);
1428 break;
1429 case LTTV_FILTER_TRACEFILE_NAME:
1430 if(tracefile == NULL) lresult = TRUE;
1431 else lresult = t->l_child.leaf->op((gpointer)ltt_tracefile_name(tracefile),v);
1432 break;
1433 case LTTV_FILTER_STATE_PID:
1434 if(state == NULL) lresult = TRUE;
1435 else lresult = t->l_child.leaf->op((gpointer)&state->pid,v);
1436 break;
1437 case LTTV_FILTER_STATE_PPID:
1438 if(state == NULL) lresult = TRUE;
1439 else lresult = t->l_child.leaf->op((gpointer)&state->ppid,v);
1440 break;
1441 case LTTV_FILTER_STATE_CT:
1442 if(state == NULL) lresult = TRUE;
1443 else {
1444 double val = ltt_time_to_double(state->creation_time);
1445 lresult = t->l_child.leaf->op((gpointer)&val,v);
1446 }
1447 break;
1448 case LTTV_FILTER_STATE_IT:
1449 if(state == NULL) lresult = TRUE;
1450 else {
1451 double val = ltt_time_to_double(state->insertion_time);
1452 lresult = t->l_child.leaf->op((gpointer)&val,v);
1453 }
1454 break;
1455 case LTTV_FILTER_STATE_P_NAME:
1456 /*
1457 * FIXME: Yet to be done ( I think ? )
1458 */
1459 lresult = TRUE;
1460 break;
1461 case LTTV_FILTER_STATE_EX_MODE:
1462 if(state == NULL) lresult = TRUE;
1463 else lresult = t->l_child.leaf->op((gpointer)&state->state->t,v);
1464 break;
1465 case LTTV_FILTER_STATE_EX_SUBMODE:
1466 if(state == NULL) lresult = TRUE;
1467 else lresult = t->l_child.leaf->op((gpointer)&state->state->n,v);
1468 break;
1469 case LTTV_FILTER_STATE_P_STATUS:
1470 if(state == NULL) lresult = TRUE;
1471 else lresult = t->l_child.leaf->op((gpointer)&state->state->s,v);
1472 break;
1473 case LTTV_FILTER_STATE_CPU:
1474 /*
1475 * FIXME: What is the comparison value ?
1476 */
1477 lresult = TRUE;
1478 break;
1479 case LTTV_FILTER_EVENT_NAME:
1480 if(event == NULL) lresult = TRUE;
1481 else lresult = t->l_child.leaf->op((gpointer)ltt_event_eventtype(event),v);
1482 break;
1483
1484 case LTTV_FILTER_EVENT_CATEGORY:
1485 /*
1486 * FIXME: Not yet implemented
1487 */
1488 lresult = TRUE;
1489 break;
1490 case LTTV_FILTER_EVENT_TIME:
1491 // if(event == NULL) lresult = TRUE;
1492 // else {
1493 // double val = ltt_time_to_double(event->event_time);
1494 // lresult = t->l_child.leaf->op((gpointer)&val,v);
1495 // }
1496 lresult = TRUE;
1497 break;
1498 case LTTV_FILTER_EVENT_TSC:
1499 // if(event == NULL) lresult = TRUE;
1500 // else {
1501 // double val = ltt_time_to_double(event->event_time);
1502 // lresult = t->l_child.leaf->op((gpointer)&val,v);
1503 // }
1504 /*
1505 * FIXME: Where is event.tsc
1506 */
1507 lresult = TRUE;
1508 break;
1509 case LTTV_FILTER_EVENT_FIELD:
1510 /*
1511 * TODO: Use the offset to
1512 * find the dynamic field
1513 * in the event struct
1514 */
1515 lresult = TRUE;
1516 default:
1517 /*
1518 * This case should never be
1519 * parsed, if so, the whole
1520 * filtering is cancelled
1521 */
1522 g_warning("Error while parsing the filter tree");
1523 return TRUE;
1524 }
1525 }
1526
1527 /*
1528 * Parse linking operator
1529 * make a cutoff if possible
1530 */
1531 if((t->node & LTTV_LOGICAL_OR) && lresult == TRUE) return TRUE;
1532 if((t->node & LTTV_LOGICAL_AND) && lresult == FALSE) return FALSE;
1533
1534 /*
1535 * Parse right branch
1536 */
1537 if(t->right == LTTV_TREE_NODE) rresult = lttv_filter_tree_parse(t->r_child.t,event,tracefile,trace,state);
1538 else if(t->right == LTTV_TREE_LEAF) {
1539 //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);
1540 char* v;
1541 g_assert(v = t->r_child.leaf->value);
1542 switch(t->r_child.leaf->field) {
1543
1544 case LTTV_FILTER_TRACE_NAME:
1545 if(trace == NULL) rresult = TRUE;
1546 else rresult = t->r_child.leaf->op((gpointer)ltt_trace_name(trace),v);
1547 break;
1548 case LTTV_FILTER_TRACEFILE_NAME:
1549 if(tracefile == NULL) rresult = TRUE;
1550 else rresult = t->r_child.leaf->op((gpointer)ltt_tracefile_name(tracefile),v);
1551 break;
1552 case LTTV_FILTER_STATE_PID:
1553 if(state == NULL) rresult = TRUE;
1554 else rresult = t->r_child.leaf->op((gpointer)&state->pid,v);
1555 break;
1556 case LTTV_FILTER_STATE_PPID:
1557 if(state == NULL) rresult = TRUE;
1558 else rresult = t->r_child.leaf->op((gpointer)&state->ppid,v);
1559 break;
1560 case LTTV_FILTER_STATE_CT:
1561 if(state == NULL) rresult = TRUE;
1562 else {
1563 double val = ltt_time_to_double(state->creation_time);
1564 rresult = t->r_child.leaf->op((gpointer)&val,v);
1565 }
1566 break;
1567 case LTTV_FILTER_STATE_IT:
1568 if(state == NULL) rresult = TRUE;
1569 else {
1570 double val = ltt_time_to_double(state->insertion_time);
1571 rresult = t->r_child.leaf->op((gpointer)&val,v);
1572 }
1573 break;
1574 case LTTV_FILTER_STATE_P_NAME:
1575 /*
1576 * FIXME: Yet to be done ( I think ? )
1577 */
1578 rresult = TRUE;
1579 break;
1580 case LTTV_FILTER_STATE_EX_MODE:
1581 if(state == NULL) rresult = TRUE;
1582 else rresult = t->r_child.leaf->op((gpointer)&state->state->t,v);
1583 break;
1584 case LTTV_FILTER_STATE_EX_SUBMODE:
1585 if(state == NULL) rresult = TRUE;
1586 else rresult = t->r_child.leaf->op((gpointer)&state->state->n,v);
1587 break;
1588 case LTTV_FILTER_STATE_P_STATUS:
1589 if(state == NULL) rresult = TRUE;
1590 else rresult = t->r_child.leaf->op((gpointer)&state->state->s,v);
1591 break;
1592 case LTTV_FILTER_STATE_CPU:
1593 /*
1594 * FIXME: What is the comparison value ?
1595 */
1596 rresult = TRUE;
1597 break;
1598 case LTTV_FILTER_EVENT_NAME:
1599 if(event == NULL) rresult = TRUE;
1600 else rresult = t->r_child.leaf->op((gpointer)ltt_event_eventtype(event),v);
1601 break;
1602
1603 case LTTV_FILTER_EVENT_CATEGORY:
1604 /*
1605 * FIXME: Not yet implemented
1606 */
1607 rresult = TRUE;
1608 break;
1609 case LTTV_FILTER_EVENT_TIME:
1610 // if(event == NULL) rresult = TRUE;
1611 // else {
1612 // double val = ltt_time_to_double(event->event_time);
1613 // rresult = t->r_child.leaf->op((gpointer)&val,v);
1614 // }
1615 rresult = TRUE;
1616 break;
1617 case LTTV_FILTER_EVENT_TSC:
1618 // if(event == NULL) rresult = TRUE;
1619 // else {
1620 // double val = ltt_time_to_double(event->event_time);
1621 // rresult = t->r_child.leaf->op((gpointer)&val,v);
1622 // }
1623 /*
1624 * FIXME: Where is event.tsc
1625 */
1626 rresult = TRUE;
1627 break;
1628 case LTTV_FILTER_EVENT_FIELD:
1629 /*
1630 * TODO: Use the offset to
1631 * find the dynamic field
1632 * in the event struct
1633 */
1634 rresult = TRUE;
1635 default:
1636 /*
1637 * This case should never be
1638 * parsed, if so, this subtree
1639 * is cancelled !
1640 */
1641 g_warning("Error while parsing the filter tree");
1642 return TRUE;
1643 }
1644 }
1645
1646 /*
1647 * Apply and return the
1648 * logical link between the
1649 * two operation
1650 */
1651 switch(t->node) {
1652 case LTTV_LOGICAL_OR: return (lresult | rresult);
1653 case LTTV_LOGICAL_AND: return (lresult & rresult);
1654 case LTTV_LOGICAL_NOT: return (!rresult);
1655 case LTTV_LOGICAL_XOR: return (lresult ^ rresult);
1656 default:
1657 /*
1658 * This case should never be
1659 * parsed, if so, this subtree
1660 * is cancelled !
1661 */
1662 return TRUE;
1663 }
1664
1665 }
1666
1667 /**
1668 * @fn void lttv_print_tree(LttvFilterTree*)
1669 *
1670 * Debug
1671 * @param t the pointer to the current LttvFilterTree
1672 */
1673 void
1674 lttv_print_tree(LttvFilterTree* t) {
1675
1676 g_print("node:%p lchild:%p rchild:%p\n",t, //t->l_child.t,t->r_child.t);
1677 (t->left==LTTV_TREE_NODE)?t->l_child.t:NULL,
1678 (t->right==LTTV_TREE_NODE)?t->r_child.t:NULL);
1679 g_print("node type: %i / [left] %i / [right] %i\n",t->node,t->left,t->right);
1680 if(t->left == LTTV_TREE_NODE) lttv_print_tree(t->l_child.t);
1681 else if(t->left == LTTV_TREE_LEAF) {
1682 // g_assert(t->l_child.leaf->value != NULL);
1683 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);
1684 }
1685 g_print("1\n");
1686 if(t->right == LTTV_TREE_NODE) lttv_print_tree(t->r_child.t);
1687 else if(t->right == LTTV_TREE_LEAF) {
1688 fprintf(stderr,"leaf!\n");
1689 // g_assert(t->r_child.leaf->value != NULL);
1690 fprintf(stderr,"%p: right is %i %p %s\n",t,t->r_child.leaf->field,t->r_child.leaf->op,t->r_child.leaf->value);
1691 }
1692
1693 }
1694
1695 /**
1696 * gboolean lttv_filter_tracefile(LttvFilter*, LttTracefile*)
1697 *
1698 * Apply the filter to a specific trace
1699 * @param filter the current filter applied
1700 * @param tracefile the trace to apply the filter to
1701 * @return success/failure of operation
1702 */
1703 gboolean
1704 lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) {
1705
1706 return lttv_filter_tree_parse(filter->head,NULL,tracefile,NULL,NULL);
1707
1708 }
1709
1710 /**
1711 * @fn gboolean lttv_filter_tracestate(LttvFilter*,LttvTraceState*)
1712 *
1713 * Parse the current tracestate
1714 * @param filter pointer to the current LttvFilter
1715 * @param tracestate pointer to the current tracestate
1716 */
1717 gboolean
1718 lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) {
1719
1720 }
1721
1722 /**
1723 * @fn gboolean lttv_filter_event(LttvFilter*,LttEvent*)
1724 *
1725 * Apply the filter to a specific event
1726 * @param filter the current filter applied
1727 * @param event the event to apply the filter to
1728 * @return success/failure of operation
1729 */
1730 gboolean
1731 lttv_filter_event(LttvFilter *filter, LttEvent *event) {
1732
1733 }
1734
1735 /**
1736 * @fn static void module_init()
1737 *
1738 * Initializes the filter module and specific values
1739 */
1740 static void module_init()
1741 {
1742
1743 }
1744
1745 /**
1746 * Destroys the filter module and specific values
1747 */
1748 static void module_destroy()
1749 {
1750
1751 }
1752
1753
1754 LTTV_MODULE("filter", "Filters traceset and events", \
1755 "Filters traceset and events specifically to user input", \
1756 module_init, module_destroy)
1757
1758
1759
This page took 0.064967 seconds and 5 git commands to generate.