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