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