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 * Constructor for LttvSimpleExpression
86 * @return pointer to new LttvSimpleExpression
87 */
88 LttvSimpleExpression*
89 lttv_simple_expression_new() {
90
91 LttvSimpleExpression* se = g_new(LttvSimpleExpression,1);
92
93 se->field = LTTV_FILTER_UNDEFINED;
94 se->op = NULL;
95 se->offset = 0;
96 se->value = NULL;
97
98 return se;
99 }
100 /**
101 * add a node to the current tree
102 * FIXME: Might be used to lower coding in lttv_filter_new switch expression
103 * @param stack the tree stack
104 * @param subtree the subtree if available (pointer or NULL)
105 * @param op the logical operator that will form the node
106 */
107 void
108 lttv_filter_tree_add_node(GPtrArray* stack, LttvFilterTree* subtree, LttvLogicalOp op) {
109
110 LttvFilterTree* t1 = NULL;
111 LttvFilterTree* t2 = NULL;
112
113 t1 = (LttvFilterTree*)g_ptr_array_index(stack,stack->len-1);
114 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
115 t2 = lttv_filter_tree_new();
116 t2->node = op;
117 if(subtree != NULL) {
118 t2->left = LTTV_TREE_NODE;
119 t2->l_child.t = subtree;
120 subtree = NULL;
121 t1->right = LTTV_TREE_NODE;
122 t1->r_child.t = t2;
123 } else {
124 // a_simple_expression->value = a_field_component->str;
125 // a_field_component = g_string_new("");
126 t2->left = LTTV_TREE_LEAF;
127 // t2->l_child.leaf = a_simple_expression;
128 // a_simple_expression = g_new(lttv_simple_expression,1);
129 t1->right = LTTV_TREE_NODE;
130 t1->r_child.t = t2;
131 }
132
133 }
134
135 /**
136 * Parse through filtering field hierarchy as specified
137 * by user. This function compares each value to
138 * predetermined quarks
139 * @param fp The field path list
140 * @param se current simple expression
141 * @return success/failure of operation
142 */
143 gboolean
144 parse_field_path(GPtrArray* fp, LttvSimpleExpression* se) {
145
146 GString* f = NULL;
147 if(fp->len < 2) return FALSE;
148 g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data;
149
150 /*
151 * Parse through the specified
152 * hardcoded fields.
153 *
154 * Take note however that the
155 * 'event' subfields might change
156 * depending on values specified
157 * in core.xml file. Hence, if
158 * none of the subfields in the
159 * array match the hardcoded
160 * subfields, it will be considered
161 * as a dynamic field
162 */
163 if(g_strcasecmp(f->str,"trace") ) {
164 /*
165 * Possible values:
166 * trace.name
167 */
168 f=g_ptr_array_index(fp,1);
169 if(g_strcasecmp(f->str,"name")) {
170 se->field = LTTV_FILTER_TRACE_NAME;
171 }
172 else return FALSE;
173 } else if(g_strcasecmp(f->str,"traceset") ) {
174 /*
175 * FIXME: not yet implemented !
176 */
177 } else if(g_strcasecmp(f->str,"tracefile") ) {
178 /*
179 * Possible values:
180 * tracefile.name
181 */
182 f=g_ptr_array_index(fp,1);
183 if(g_strcasecmp(f->str,"name")) {
184 se->field = LTTV_FILTER_TRACEFILE_NAME;
185 }
186 else return FALSE;
187 } else if(g_strcasecmp(f->str,"state") ) {
188 /*
189 * Possible values:
190 * state.pid
191 * state.ppid
192 * state.creation_time
193 * state.insertion_time
194 * state.process_name
195 * state.execution_mode
196 * state.execution_submode
197 * state.process_status
198 * state.cpu
199 */
200 f=g_ptr_array_index(fp,1);
201 if(g_strcasecmp(f->str,"pid") ) {
202 se->field = LTTV_FILTER_STATE_PID;
203 }
204 else if(g_strcasecmp(f->str,"ppid") ) {
205 se->field = LTTV_FILTER_STATE_PPID;
206 }
207 else if(g_strcasecmp(f->str,"creation_time") ) {
208 se->field = LTTV_FILTER_STATE_CT;
209 }
210 else if(g_strcasecmp(f->str,"insertion_time") ) {
211 se->field = LTTV_FILTER_STATE_IT;
212 }
213 else if(g_strcasecmp(f->str,"process_name") ) {
214 se->field = LTTV_FILTER_STATE_P_NAME;
215 }
216 else if(g_strcasecmp(f->str,"execution_mode") ) {
217 se->field = LTTV_FILTER_STATE_EX_MODE;
218 }
219 else if(g_strcasecmp(f->str,"execution_submode") ) {
220 se->field = LTTV_FILTER_STATE_EX_SUBMODE;
221 }
222 else if(g_strcasecmp(f->str,"process_status") ) {
223 se->field = LTTV_FILTER_STATE_P_STATUS;
224 }
225 else if(g_strcasecmp(f->str,"cpu") ) {
226 se->field = LTTV_FILTER_STATE_CPU;
227 }
228 else return FALSE;
229 } else if(g_strcasecmp(f->str,"event") ) {
230 /*
231 * Possible values:
232 * event.name
233 * event.category
234 * event.time
235 * event.tsc
236 */
237 f=g_ptr_array_index(fp,1);
238 if(g_strcasecmp(f->str,"name") ) {
239 se->field = LTTV_FILTER_EVENT_NAME;
240 }
241 else if(g_strcasecmp(f->str,"category") ) {
242 /*
243 * FIXME: Category not yet functional in lttv
244 */
245 se->field = LTTV_FILTER_EVENT_CATEGORY;
246 }
247 else if(g_strcasecmp(f->str,"time") ) {
248 se->field = LTTV_FILTER_EVENT_TIME;
249 // offset = &((LttEvent*)NULL)->event_time);
250 }
251 else if(g_strcasecmp(f->str,"tsc") ) {
252 se->field = LTTV_FILTER_EVENT_TSC;
253 // offset = &((LttEvent*)NULL)->event_cycle_count);
254 }
255 else { /* core.xml specified options */
256 se->field = LTTV_FILTER_EVENT_FIELD;
257 //se->offset = (...);
258 }
259 } else {
260 g_warning("Unrecognized field in filter string");
261 return FALSE;
262 }
263
264 /* free the pointer array */
265 g_ptr_array_free(fp,FALSE);
266
267 return TRUE;
268 }
269
270 /**
271 * Sets the function pointer for the current
272 * Simple Expression
273 * @param se current simple expression
274 * @return success/failure of operation
275 */
276 gboolean assign_operator(LttvSimpleExpression* se, LttvExpressionOp op) {
277
278 switch(se->field) {
279 /* char */
280 case LTTV_FILTER_TRACE_NAME:
281 case LTTV_FILTER_TRACEFILE_NAME:
282 case LTTV_FILTER_STATE_P_NAME:
283 case LTTV_FILTER_EVENT_NAME:
284 switch(op) {
285 case LTTV_FIELD_EQ:
286 se->op = lttv_apply_op_eq_string;
287 break;
288 case LTTV_FIELD_NE:
289 se->op = lttv_apply_op_eq_string;
290 break;
291 default:
292 g_warning("Error encountered in operator assignment");
293 return FALSE;
294 }
295 break;
296 case LTTV_FILTER_STATE_PID:
297 case LTTV_FILTER_STATE_PPID:
298 case LTTV_FILTER_STATE_EX_MODE:
299 case LTTV_FILTER_STATE_EX_SUBMODE:
300 case LTTV_FILTER_STATE_P_STATUS:
301 switch(op) {
302 case LTTV_FIELD_EQ:
303 se->op = lttv_apply_op_eq_uint64;
304 break;
305 case LTTV_FIELD_NE:
306 se->op = lttv_apply_op_ne_uint64;
307 break;
308 case LTTV_FIELD_LT:
309 se->op = lttv_apply_op_lt_uint64;
310 break;
311 case LTTV_FIELD_LE:
312 se->op = lttv_apply_op_le_uint64;
313 break;
314 case LTTV_FIELD_GT:
315 se->op = lttv_apply_op_gt_uint64;
316 break;
317 case LTTV_FIELD_GE:
318 se->op = lttv_apply_op_ge_uint64;
319 break;
320 default:
321 g_warning("Error encountered in operator assignment");
322 return FALSE;
323 }
324 break;
325 case LTTV_FILTER_STATE_CT:
326 case LTTV_FILTER_STATE_IT:
327 case LTTV_FILTER_EVENT_TIME:
328 case LTTV_FILTER_EVENT_TSC:
329 switch(op) {
330 case LTTV_FIELD_EQ:
331 se->op = lttv_apply_op_eq_double;
332 break;
333 case LTTV_FIELD_NE:
334 se->op = lttv_apply_op_ne_double;
335 break;
336 case LTTV_FIELD_LT:
337 se->op = lttv_apply_op_lt_double;
338 break;
339 case LTTV_FIELD_LE:
340 se->op = lttv_apply_op_le_double;
341 break;
342 case LTTV_FIELD_GT:
343 se->op = lttv_apply_op_gt_double;
344 break;
345 case LTTV_FIELD_GE:
346 se->op = lttv_apply_op_ge_double;
347 break;
348 default:
349 g_warning("Error encountered in operator assignment");
350 return FALSE;
351 }
352 break;
353 default:
354 g_warning("Error encountered in operator assignment");
355 return FALSE;
356 }
357
358 }
359
360 /**
361 * Add an filtering option to the current tree
362 * @param expression Current expression to parse
363 * @return success/failure of operation
364 */
365 gboolean
366 parse_simple_expression(GString* expression) {
367
368 unsigned i;
369
370
371
372
373 }
374
375 /**
376 * Append a new expression to the expression
377 * defined in the current filter
378 * @param filter pointer to the current LttvFilter
379 * @param expression string that must be appended
380 */
381 void lttv_filter_append_expression(LttvFilter* filter, char *expression) {
382
383 if(expression == NULL) return;
384 if(filter == NULL) {
385 filter = lttv_filter_new();
386 filter->expression = expression;
387 } else if(filter->expression == NULL) {
388 filter->expression = expression;
389 } else {
390 filter->expression = g_strconcat(filter->expression,"&",expression);
391 }
392
393 lttv_filter_update(filter);
394
395 }
396
397 void lttv_filter_clear_expression(LttvFilter* filter) {
398
399 if(filter->expression != NULL) {
400 g_free(filter->expression);
401 filter->expression = NULL;
402 }
403
404 }
405
406 /**
407 * Applies the 'equal' operator to the
408 * specified structure and value
409 * @param v1 left member of comparison
410 * @param v2 right member of comparison
411 * @return success/failure of operation
412 */
413 gboolean lttv_apply_op_eq_uint64(gpointer v1, char* v2) {
414
415 guint64* r = (guint64*) v1;
416 guint64 l = atoi(v2);
417 return (*r == l);
418
419 }
420
421 /**
422 * Applies the 'equal' operator to the
423 * specified structure and value
424 * @param v1 left member of comparison
425 * @param v2 right member of comparison
426 * @return success/failure of operation
427 */
428 gboolean lttv_apply_op_eq_uint32(gpointer v1, char* v2) {
429 guint32* r = (guint32*) v1;
430 guint32 l = atoi(v2);
431 return (*r == l);
432 }
433
434 /**
435 * Applies the 'equal' operator to the
436 * specified structure and value
437 * @param v1 left member of comparison
438 * @param v2 right member of comparison
439 * @return success/failure of operation
440 */
441 gboolean lttv_apply_op_eq_uint16(gpointer v1, char* v2) {
442 guint16* r = (guint16*) v1;
443 guint16 l = atoi(v2);
444 return (*r == l);
445 }
446
447 /**
448 * Applies the 'equal' operator to the
449 * specified structure and value
450 * @param v1 left member of comparison
451 * @param v2 right member of comparison
452 * @return success/failure of operation
453 */
454 gboolean lttv_apply_op_eq_double(gpointer v1, char* v2) {
455 double* r = (double*) v1;
456 double l = atof(v2);
457 return (*r == l);
458 }
459
460 /**
461 * Applies the 'equal' operator to the
462 * specified structure and value
463 * @param v1 left member of comparison
464 * @param v2 right member of comparison
465 * @return success/failure of operation
466 */
467 gboolean lttv_apply_op_eq_string(gpointer v1, char* v2) {
468 char* r = (char*) v1;
469 return (g_strcasecmp(r,v2));
470 }
471
472 /**
473 * Applies the 'not equal' operator to the
474 * specified structure and value
475 * @param v1 left member of comparison
476 * @param v2 right member of comparison
477 * @return success/failure of operation
478 */
479 gboolean lttv_apply_op_ne_uint64(gpointer v1, char* v2) {
480 guint64* r = (guint64*) v1;
481 guint64 l = atoi(v2);
482 return (*r != l);
483 }
484
485 /**
486 * Applies the 'not equal' operator to the
487 * specified structure and value
488 * @param v1 left member of comparison
489 * @param v2 right member of comparison
490 * @return success/failure of operation
491 */
492 gboolean lttv_apply_op_ne_uint32(gpointer v1, char* v2) {
493 guint32* r = (guint32*) v1;
494 guint32 l = atoi(v2);
495 return (*r != l);
496 }
497
498 /**
499 * Applies the 'not equal' operator to the
500 * specified structure and value
501 * @param v1 left member of comparison
502 * @param v2 right member of comparison
503 * @return success/failure of operation
504 */
505 gboolean lttv_apply_op_ne_uint16(gpointer v1, char* v2) {
506 guint16* r = (guint16*) v1;
507 guint16 l = atoi(v2);
508 return (*r != l);
509 }
510
511 /**
512 * Applies the 'not equal' operator to the
513 * specified structure and value
514 * @param v1 left member of comparison
515 * @param v2 right member of comparison
516 * @return success/failure of operation
517 */
518 gboolean lttv_apply_op_ne_double(gpointer v1, char* v2) {
519 double* r = (double*) v1;
520 double l = atof(v2);
521 return (*r != l);
522 }
523
524 /**
525 * Applies the 'not 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_ne_string(gpointer v1, char* v2) {
532 char* r = (char*) v1;
533 return (!g_strcasecmp(r,v2));
534 }
535
536 /**
537 * Applies the 'lower than' operator to the
538 * specified structure and value
539 * @param v1 left member of comparison
540 * @param v2 right member of comparison
541 * @return success/failure of operation
542 */
543 gboolean lttv_apply_op_lt_uint64(gpointer v1, char* v2) {
544 guint64* r = (guint64*) v1;
545 guint64 l = atoi(v2);
546 return (*r < l);
547 }
548
549 /**
550 * Applies the 'lower than' operator to the
551 * specified structure and value
552 * @param v1 left member of comparison
553 * @param v2 right member of comparison
554 * @return success/failure of operation
555 */
556 gboolean lttv_apply_op_lt_uint32(gpointer v1, char* v2) {
557 guint32* r = (guint32*) v1;
558 guint32 l = atoi(v2);
559 return (*r < l);
560 }
561
562 /**
563 * Applies the 'lower than' operator to the
564 * specified structure and value
565 * @param v1 left member of comparison
566 * @param v2 right member of comparison
567 * @return success/failure of operation
568 */
569 gboolean lttv_apply_op_lt_uint16(gpointer v1, char* v2) {
570 guint16* r = (guint16*) v1;
571 guint16 l = atoi(v2);
572 return (*r < l);
573 }
574
575 /**
576 * Applies the 'lower than' operator to the
577 * specified structure and value
578 * @param v1 left member of comparison
579 * @param v2 right member of comparison
580 * @return success/failure of operation
581 */
582 gboolean lttv_apply_op_lt_double(gpointer v1, char* v2) {
583 double* r = (double*) v1;
584 double l = atof(v2);
585 return (*r < l);
586 }
587
588 /**
589 * Applies the 'lower than' operator to the
590 * specified structure and value
591 * @param v1 left member of comparison
592 * @param v2 right member of comparison
593 * @return success/failure of operation
594 */
595 gboolean lttv_apply_op_le_uint64(gpointer v1, char* v2) {
596 guint64* r = (guint64*) v1;
597 guint64 l = atoi(v2);
598 return (*r <= l);
599 }
600
601 /**
602 * Applies the 'lower or equal' operator to the
603 * specified structure and value
604 * @param v1 left member of comparison
605 * @param v2 right member of comparison
606 * @return success/failure of operation
607 */
608 gboolean lttv_apply_op_le_uint32(gpointer v1, char* v2) {
609 guint32* r = (guint32*) v1;
610 guint32 l = atoi(v2);
611 return (*r <= l);
612 }
613
614 /**
615 * Applies the 'lower or equal' operator to the
616 * specified structure and value
617 * @param v1 left member of comparison
618 * @param v2 right member of comparison
619 * @return success/failure of operation
620 */
621 gboolean lttv_apply_op_le_uint16(gpointer v1, char* v2) {
622 guint16* r = (guint16*) v1;
623 guint16 l = atoi(v2);
624 return (*r <= l);
625 }
626
627 /**
628 * Applies the 'lower or equal' operator to the
629 * specified structure and value
630 * @param v1 left member of comparison
631 * @param v2 right member of comparison
632 * @return success/failure of operation
633 */
634 gboolean lttv_apply_op_le_double(gpointer v1, char* v2) {
635 double* r = (double*) v1;
636 double l = atof(v2);
637 return (*r <= l);
638 }
639
640 /**
641 * Applies the 'greater than' operator to the
642 * specified structure and value
643 * @param v1 left member of comparison
644 * @param v2 right member of comparison
645 * @return success/failure of operation
646 */
647 gboolean lttv_apply_op_gt_uint64(gpointer v1, char* v2) {
648 guint64* r = (guint64*) v1;
649 guint64 l = atoi(v2);
650 return (*r > l);
651 }
652
653 /**
654 * Applies the 'greater than' operator to the
655 * specified structure and value
656 * @param v1 left member of comparison
657 * @param v2 right member of comparison
658 * @return success/failure of operation
659 */
660 gboolean lttv_apply_op_gt_uint32(gpointer v1, char* v2) {
661 guint32* r = (guint32*) v1;
662 guint32 l = atoi(v2);
663 return (*r > l);
664 }
665
666 /**
667 * Applies the 'greater than' operator to the
668 * specified structure and value
669 * @param v1 left member of comparison
670 * @param v2 right member of comparison
671 * @return success/failure of operation
672 */
673 gboolean lttv_apply_op_gt_uint16(gpointer v1, char* v2) {
674 guint16* r = (guint16*) v1;
675 guint16 l = atoi(v2);
676 return (*r > l);
677 }
678
679 /**
680 * Applies the 'greater 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_gt_double(gpointer v1, char* v2) {
687 double* r = (double*) v1;
688 double l = atof(v2);
689 return (*r > l);
690 }
691
692 /**
693 * Applies the 'greater or equal' operator to the
694 * specified structure and value
695 * @param v1 left member of comparison
696 * @param v2 right member of comparison
697 * @return success/failure of operation
698 */
699 gboolean lttv_apply_op_ge_uint64(gpointer v1, char* v2) {
700 guint64* r = (guint64*) v1;
701 guint64 l = atoi(v2);
702 return (*r >= l);
703 }
704
705 /**
706 * Applies the 'greater or equal' operator to the
707 * specified structure and value
708 * @param v1 left member of comparison
709 * @param v2 right member of comparison
710 * @return success/failure of operation
711 */
712 gboolean lttv_apply_op_ge_uint32(gpointer v1, char* v2) {
713 guint32* r = (guint32*) v1;
714 guint32 l = atoi(v2);
715 return (*r >= l);
716 }
717
718 /**
719 * Applies the 'greater or equal' operator to the
720 * specified structure and value
721 * @param v1 left member of comparison
722 * @param v2 right member of comparison
723 * @return success/failure of operation
724 */
725 gboolean lttv_apply_op_ge_uint16(gpointer v1, char* v2) {
726 guint16* r = (guint16*) v1;
727 guint16 l = atoi(v2);
728 return (*r >= l);
729 }
730
731 /**
732 * Applies the 'greater or equal' operator to the
733 * specified structure and value
734 * @param v1 left member of comparison
735 * @param v2 right member of comparison
736 * @return success/failure of operation
737 */
738 gboolean lttv_apply_op_ge_double(gpointer v1, char* v2) {
739 double* r = (double*) v1;
740 double l = atof(v2);
741 return (*r >= l);
742 }
743
744
745 /**
746 * Makes a copy of the current filter tree
747 * @param tree pointer to the current tree
748 * @return new copy of the filter tree
749 */
750 LttvFilterTree*
751 lttv_filter_tree_clone(LttvFilterTree* tree) {
752
753
754
755 }
756
757 /**
758 * Makes a copy of the current filter
759 * @param filter pointer to the current filter
760 * @return new copy of the filter
761 */
762 LttvFilter*
763 lttv_filter_clone(LttvFilter* filter) {
764
765
766 LttvFilter* newfilter = g_new(LttvFilter,1);
767
768 // newfilter->expression = g_new(char,1)
769 strcpy(newfilter->expression,filter->expression);
770
771 newfilter->head = lttv_filter_tree_clone(filter->head);
772
773 return newfilter;
774
775 }
776
777
778 /**
779 * Creates a new lttv_filter
780 * @param expression filtering options string
781 * @param t pointer to the current LttvTrace
782 * @return the current lttv_filter or NULL if error
783 */
784 LttvFilter*
785 lttv_filter_new() {
786
787 LttvFilter* filter = g_new(LttvFilter,1);
788 filter->expression = NULL;
789 filter->head = NULL;
790
791 }
792
793 gboolean
794 lttv_filter_update(LttvFilter* filter) {
795
796 g_print("filter::lttv_filter_new()\n"); /* debug */
797
798 if(filter->expression == NULL) return FALSE;
799
800 unsigned
801 i,
802 p_nesting=0, /* parenthesis nesting value */
803 b=0; /* current breakpoint in expression string */
804
805 /* trees */
806 LttvFilterTree
807 *tree = lttv_filter_tree_new(), /* main tree */
808 *subtree = NULL, /* buffer for subtrees */
809 *t1, /* buffer #1 */
810 *t2; /* buffer #2 */
811
812 /*
813 * the filter
814 * If the tree already exists,
815 * destroy it and build a new one
816 */
817 if(filter->head != NULL) lttv_filter_tree_destroy(filter->head);
818 filter->head = tree;
819
820 /*
821 * Tree Stack
822 * each element of the list
823 * is a sub tree created
824 * by the use of parenthesis in the
825 * global expression. The final tree
826 * will be the one left at the root of
827 * the list
828 */
829 GPtrArray *tree_stack = g_ptr_array_new();
830 g_ptr_array_add( tree_stack,(gpointer) tree );
831
832 /* temporary values */
833 GString *a_field_component = g_string_new("");
834 GPtrArray *a_field_path = NULL;
835
836 LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new();
837
838 /*
839 * Parse entire expression and construct
840 * the binary tree. There are two steps
841 * in browsing that string
842 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
843 * 2. finding simple expressions
844 * - field path ( separated by dots )
845 * - op ( >, <, =, >=, <=, !=)
846 * - value ( integer, string ... )
847 * To spare computing time, the whole
848 * string is parsed in this loop for a
849 * O(n) complexity order.
850 *
851 * When encountering logical op &,|,^
852 * 1. parse the last value if any
853 * 2. create a new tree
854 * 3. add the expression (simple exp, or exp (subtree)) to the tree
855 * 4. concatenate this tree with the current tree on top of the stack
856 * When encountering math ops >,>=,<,<=,=,!=
857 * 1. add to op to the simple expression
858 * 2. concatenate last field component to field path
859 * When encountering concatening ops .
860 * 1. concatenate last field component to field path
861 * When encountering opening parenthesis (,{,[
862 * 1. create a new subtree on top of tree stack
863 * When encountering closing parenthesis ),},]
864 * 1. add the expression on right child of the current tree
865 * 2. the subtree is completed, allocate a new subtree
866 * 3. pop the tree value from the tree stack
867 */
868
869 a_field_path = g_ptr_array_new();
870 g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */
871
872
873 for(i=0;i<strlen(filter->expression);i++) {
874 // debug
875 g_print("%c ",filter->expression[i]);
876 switch(filter->expression[i]) {
877 /*
878 * logical operators
879 */
880 case '&': /* and */
881 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
882 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
883 t2 = lttv_filter_tree_new();
884 t2->node = LTTV_LOGICAL_AND;
885 if(subtree != NULL) { /* append subtree to current tree */
886 t2->left = LTTV_TREE_NODE;
887 t2->l_child.t = subtree;
888 subtree = NULL;
889 t1->right = LTTV_TREE_NODE;
890 t1->r_child.t = t2;
891 } else { /* append a simple expression */
892 a_simple_expression->value = a_field_component->str;
893 a_field_component = g_string_new("");
894 t2->left = LTTV_TREE_LEAF;
895 t2->l_child.leaf = a_simple_expression;
896 a_simple_expression = lttv_simple_expression_new();
897 t1->right = LTTV_TREE_NODE;
898 t1->r_child.t = t2;
899 }
900
901 break;
902 case '|': /* or */
903 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
904 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
905 t2 = lttv_filter_tree_new();
906 t2->node = LTTV_LOGICAL_OR;
907 if(subtree != NULL) { /* append subtree to current tree */
908 t2->left = LTTV_TREE_NODE;
909 t2->l_child.t = subtree;
910 subtree = NULL;
911 t1->right = LTTV_TREE_NODE;
912 t1->r_child.t = t2;
913 } else { /* append a simple expression */
914 a_simple_expression->value = a_field_component->str;
915 a_field_component = g_string_new("");
916 t2->left = LTTV_TREE_LEAF;
917 t2->l_child.leaf = a_simple_expression;
918 a_simple_expression = lttv_simple_expression_new();
919 t1->right = LTTV_TREE_NODE;
920 t1->r_child.t = t2;
921 }
922 break;
923 case '^': /* xor */
924 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
925 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
926 t2 = lttv_filter_tree_new();
927 t2->node = LTTV_LOGICAL_XOR;
928 if(subtree != NULL) { /* append subtree to current tree */
929 t2->left = LTTV_TREE_NODE;
930 t2->l_child.t = subtree;
931 subtree = NULL;
932 t1->right = LTTV_TREE_NODE;
933 t1->r_child.t = t2;
934 } else { /* append a simple expression */
935 a_simple_expression->value = a_field_component->str;
936 a_field_component = g_string_new("");
937 t2->left = LTTV_TREE_LEAF;
938 t2->l_child.leaf = a_simple_expression;
939 a_simple_expression = lttv_simple_expression_new();
940 t1->right = LTTV_TREE_NODE;
941 t1->r_child.t = t2;
942 }
943 break;
944 case '!': /* not, or not equal (math op) */
945 if(filter->expression[i+1] == '=') { /* != */
946 assign_operator(a_simple_expression,LTTV_FIELD_NE);
947 i++;
948 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
949 parse_field_path(a_field_path,a_simple_expression);
950 a_field_component = g_string_new("");
951 } else { /* ! */
952 // g_print("%s\n",a_field_component);
953 // a_field_component = g_string_new("");
954 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
955 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
956 t2 = lttv_filter_tree_new();
957 t2->node = LTTV_LOGICAL_NOT;
958 t1->right = LTTV_TREE_NODE;
959 t1->r_child.t = t2;
960 }
961 break;
962 case '(': /* start of parenthesis */
963 case '[':
964 case '{':
965 p_nesting++; /* incrementing parenthesis nesting value */
966 t1 = lttv_filter_tree_new();
967 g_ptr_array_add( tree_stack,(gpointer) t1 );
968 break;
969 case ')': /* end of parenthesis */
970 case ']':
971 case '}':
972 p_nesting--; /* decrementing parenthesis nesting value */
973 if(p_nesting<0 || tree_stack->len<2) {
974 g_warning("Wrong filtering options, the string\n\"%s\"\n\
975 is not valid due to parenthesis incorrect use",filter->expression);
976 return FALSE;
977 }
978
979 g_assert(tree_stack->len>0);
980 if(subtree != NULL) { /* append subtree to current tree */
981 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
982 while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) {
983 g_assert(t1!=NULL && t1->r_child.t != NULL);
984 t1 = t1->r_child.t;
985 }
986 t1->right = LTTV_TREE_NODE;
987 t1->r_child.t = subtree;
988 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
989 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
990 } else { /* assign subtree as current tree */
991 a_simple_expression->value = a_field_component->str;
992 a_field_component = g_string_new("");
993 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
994 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
995 t1->right = LTTV_TREE_LEAF;
996 t1->r_child.leaf = a_simple_expression;
997 a_simple_expression = lttv_simple_expression_new();
998 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
999 g_assert(subtree != NULL);
1000 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
1001 }
1002 break;
1003
1004 /*
1005 * mathematic operators
1006 */
1007 case '<': /* lower, lower or equal */
1008 if(filter->expression[i+1] == '=') { /* <= */
1009 i++;
1010 assign_operator(a_simple_expression,LTTV_FIELD_LE);
1011 } else assign_operator(a_simple_expression,LTTV_FIELD_LT);
1012 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1013 parse_field_path(a_field_path,a_simple_expression);
1014 a_field_component = g_string_new("");
1015 break;
1016 case '>': /* higher, higher or equal */
1017 if(filter->expression[i+1] == '=') { /* >= */
1018 i++;
1019 assign_operator(a_simple_expression,LTTV_FIELD_GE);
1020 } else assign_operator(a_simple_expression,LTTV_FIELD_GT);
1021 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1022 parse_field_path(a_field_path,a_simple_expression);
1023 a_field_component = g_string_new("");
1024 break;
1025 case '=': /* equal */
1026 assign_operator(a_simple_expression,LTTV_FIELD_EQ);
1027 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1028 parse_field_path(a_field_path,a_simple_expression);
1029 a_field_component = g_string_new("");
1030 break;
1031 /*
1032 * Field concatening caracter
1033 */
1034 case '.': /* dot */
1035 /*
1036 * divide field expression into elements
1037 * in a_field_path array.
1038 */
1039 if(a_simple_expression->op != NULL) {
1040 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
1041 a_field_component = g_string_new("");
1042 }
1043 break;
1044 default: /* concatening current string */
1045 g_string_append_c(a_field_component,filter->expression[i]);
1046 }
1047 }
1048
1049 g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2);
1050 g_print("stack size: %i\n",tree_stack->len);
1051
1052 /*
1053 * Preliminary check to see
1054 * if tree was constructed correctly
1055 */
1056 if( p_nesting>0 ) {
1057 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1058 is not valid due to parenthesis incorrect use",filter->expression);
1059 return FALSE;
1060 }
1061
1062 if(tree_stack->len != 1) /* only root tree should remain */
1063 return FALSE;
1064
1065 /* processing last element of expression */
1066 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1067 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1068 if(subtree != NULL) { /* add the subtree */
1069 t1->right = LTTV_TREE_NODE;
1070 t1->r_child.t = subtree;
1071 subtree = NULL;
1072 } else { /* add a leaf */
1073 a_simple_expression->value = a_field_component->str;
1074 a_field_component = g_string_new("");
1075 t1->right = LTTV_TREE_LEAF;
1076 t1->r_child.leaf = a_simple_expression;
1077 /*
1078 * FIXME: is it really necessary to reallocate
1079 * LttvSimpleExpression at this point ??
1080 */
1081 a_simple_expression = lttv_simple_expression_new();
1082 }
1083
1084 g_assert(tree != NULL);
1085 g_assert(subtree == NULL);
1086
1087 return TRUE;
1088 // return filter;
1089
1090 }
1091
1092 void
1093 lttv_filter_destroy(LttvFilter* filter) {
1094
1095 g_free(filter->expression);
1096 lttv_filter_tree_destroy(filter->head);
1097 g_free(filter);
1098
1099 }
1100
1101 /**
1102 * Assign a new tree for the current expression
1103 * or sub expression
1104 * @return pointer of LttvFilterTree
1105 */
1106 LttvFilterTree*
1107 lttv_filter_tree_new() {
1108 LttvFilterTree* tree;
1109
1110 tree = g_new(LttvFilter,1);
1111 tree->node = 0; //g_new(lttv_expression,1);
1112 tree->left = LTTV_TREE_IDLE;
1113 tree->right = LTTV_TREE_IDLE;
1114
1115 return tree;
1116 }
1117
1118 /**
1119 * Destroys the tree and his sub-trees
1120 * @param tree Tree which must be destroyed
1121 */
1122 void
1123 lttv_filter_tree_destroy(LttvFilterTree* tree) {
1124
1125 if(tree == NULL) return;
1126
1127 if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf);
1128 else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t);
1129
1130 if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf);
1131 else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t);
1132
1133 g_free(tree->node);
1134 g_free(tree);
1135 }
1136
1137
1138 /**
1139 * Apply the filter to a specific trace
1140 * @param filter the current filter applied
1141 * @param tracefile the trace to apply the filter to
1142 * @return success/failure of operation
1143 */
1144 gboolean
1145 lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) {
1146
1147 LttvFilterTree* t = filter->head;
1148
1149 /*
1150 * Each tree is parsed in inorder.
1151 * This way, it's possible to apply the left filter of the
1152 * tree, then decide whether or not the right branch should
1153 * be parsed depending on the linking logical operator
1154 *
1155 * As for the filtering structure, since we are trying
1156 * to remove elements from the trace, it might be better
1157 * managing an array of all items to be removed ..
1158 */
1159
1160 /////////////////////////////////////////////////////////////////////////////
1161 // TEST //
1162 /////////////////////////////////////////////////////////////////////////////
1163 g_print("node:%p lchild:%p rchild:%p\n",t,t->l_child.t,t->r_child.t);
1164 g_print("node type%i\n",t->node);
1165 if(t->left == LTTV_TREE_NODE) lttv_filter_tracefile(t->l_child.t,NULL);
1166 else if(t->left == LTTV_TREE_LEAF) {
1167 g_assert(t->l_child.leaf->value != NULL);
1168 g_print("%p: left is qqch %i %s\n",t,t->l_child.leaf->op,t->l_child.leaf->value);
1169 }
1170 if(t->right == LTTV_TREE_NODE) lttv_filter_tracefile(t->r_child.t,NULL);
1171 else if(t->right == LTTV_TREE_LEAF) {
1172 g_assert(t->r_child.leaf->value != NULL);
1173 g_print("%p: right is qqch %i %s\n",t,t->r_child.leaf->op,t->r_child.leaf->value);
1174 }
1175 /////////////////////////////////////////////////////////////////////////////
1176
1177
1178
1179
1180 /* test */
1181 /* int i, nb;
1182 char *f_name, *e_name;
1183
1184 char* field = "cpu";
1185
1186 LttvTraceHook h;
1187
1188 LttEventType *et;
1189
1190 LttType *t;
1191
1192 GString *fe_name = g_string_new("");
1193
1194 nb = ltt_trace_eventtype_number(tcs->parent.t);
1195 g_print("NB:%i\n",nb);
1196 for(i = 0 ; i < nb ; i++) {
1197 et = ltt_trace_eventtype_get(tcs->parent.t, i);
1198 e_name = ltt_eventtype_name(et);
1199 f_name = ltt_facility_name(ltt_eventtype_facility(et));
1200 g_string_printf(fe_name, "%s.%s", f_name, e_name);
1201 g_print("facility:%s and event:%s\n",f_name,e_name);
1202 }
1203 */
1204 }
1205
1206 gboolean
1207 lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) {
1208
1209 }
1210
1211 /**
1212 * Apply the filter to a specific event
1213 * @param filter the current filter applied
1214 * @param event the event to apply the filter to
1215 * @return success/failure of operation
1216 */
1217 gboolean
1218 lttv_filter_event(LttvFilter *filter, LttEvent *event) {
1219
1220 }
1221
1222 /**
1223 * Initializes the filter module and specific values
1224 */
1225 static void module_init()
1226 {
1227
1228 /*
1229 * Quarks initialization
1230 * for hardcoded filtering options
1231 *
1232 * TODO: traceset has no yet been defined
1233 */
1234
1235 /* top fields */
1236 // LTTV_FILTER_EVENT = g_quark_from_string("event");
1237 // LTTV_FILTER_TRACE = g_quark_from_string("trace");
1238 // LTTV_FILTER_TRACESET = g_quark_from_string("traceset");
1239 // LTTV_FILTER_STATE = g_quark_from_string("state");
1240 // LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile");
1241
1242 /* event.name, tracefile.name, trace.name */
1243 // LTTV_FILTER_NAME = g_quark_from_string("name");
1244
1245 /* event sub fields */
1246 // LTTV_FILTER_CATEGORY = g_quark_from_string("category");
1247 // LTTV_FILTER_TIME = g_quark_from_string("time");
1248 // LTTV_FILTER_TSC = g_quark_from_string("tsc");
1249
1250 /* state sub fields */
1251 // LTTV_FILTER_PID = g_quark_from_string("pid");
1252 // LTTV_FILTER_PPID = g_quark_from_string("ppid");
1253 // LTTV_FILTER_C_TIME = g_quark_from_string("creation_time");
1254 // LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time");
1255 // LTTV_FILTER_P_NAME = g_quark_from_string("process_name");
1256 // LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode");
1257 // LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode");
1258 // LTTV_FILTER_P_STATUS = g_quark_from_string("process_status");
1259 // LTTV_FILTER_CPU = g_quark_from_string("cpu");
1260
1261 }
1262
1263 /**
1264 * Destroys the filter module and specific values
1265 */
1266 static void module_destroy()
1267 {
1268 }
1269
1270
1271 LTTV_MODULE("filter", "Filters traceset and events", \
1272 "Filters traceset and events specifically to user input", \
1273 module_init, module_destroy)
1274
1275
1276
This page took 0.054479 seconds and 4 git commands to generate.