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 * Applies the 'equal' operator to the
377 * specified structure and value
378 * @param v1 left member of comparison
379 * @param v2 right member of comparison
380 * @return success/failure of operation
381 */
382 gboolean lttv_apply_op_eq_uint64(gpointer v1, char* v2) {
383
384 guint64* r = (guint64*) v1;
385 guint64 l = atoi(v2);
386 return (*r == l);
387
388 }
389
390 /**
391 * Applies the 'equal' operator to the
392 * specified structure and value
393 * @param v1 left member of comparison
394 * @param v2 right member of comparison
395 * @return success/failure of operation
396 */
397 gboolean lttv_apply_op_eq_uint32(gpointer v1, char* v2) {
398 guint32* r = (guint32*) v1;
399 guint32 l = atoi(v2);
400 return (*r == l);
401 }
402
403 /**
404 * Applies the 'equal' operator to the
405 * specified structure and value
406 * @param v1 left member of comparison
407 * @param v2 right member of comparison
408 * @return success/failure of operation
409 */
410 gboolean lttv_apply_op_eq_uint16(gpointer v1, char* v2) {
411 guint16* r = (guint16*) v1;
412 guint16 l = atoi(v2);
413 return (*r == l);
414 }
415
416 /**
417 * Applies the 'equal' operator to the
418 * specified structure and value
419 * @param v1 left member of comparison
420 * @param v2 right member of comparison
421 * @return success/failure of operation
422 */
423 gboolean lttv_apply_op_eq_double(gpointer v1, char* v2) {
424 double* r = (double*) v1;
425 double l = atof(v2);
426 return (*r == l);
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_string(gpointer v1, char* v2) {
437 char* r = (char*) v1;
438 return (g_strcasecmp(r,v2));
439 }
440
441 /**
442 * Applies the 'not equal' operator to the
443 * specified structure and value
444 * @param v1 left member of comparison
445 * @param v2 right member of comparison
446 * @return success/failure of operation
447 */
448 gboolean lttv_apply_op_ne_uint64(gpointer v1, char* v2) {
449 guint64* r = (guint64*) v1;
450 guint64 l = atoi(v2);
451 return (*r != l);
452 }
453
454 /**
455 * Applies the 'not equal' operator to the
456 * specified structure and value
457 * @param v1 left member of comparison
458 * @param v2 right member of comparison
459 * @return success/failure of operation
460 */
461 gboolean lttv_apply_op_ne_uint32(gpointer v1, char* v2) {
462 guint32* r = (guint32*) v1;
463 guint32 l = atoi(v2);
464 return (*r != l);
465 }
466
467 /**
468 * Applies the 'not equal' operator to the
469 * specified structure and value
470 * @param v1 left member of comparison
471 * @param v2 right member of comparison
472 * @return success/failure of operation
473 */
474 gboolean lttv_apply_op_ne_uint16(gpointer v1, char* v2) {
475 guint16* r = (guint16*) v1;
476 guint16 l = atoi(v2);
477 return (*r != l);
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_double(gpointer v1, char* v2) {
488 double* r = (double*) v1;
489 double l = atof(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_string(gpointer v1, char* v2) {
501 char* r = (char*) v1;
502 return (!g_strcasecmp(r,v2));
503 }
504
505 /**
506 * Applies the 'lower than' operator to the
507 * specified structure and value
508 * @param v1 left member of comparison
509 * @param v2 right member of comparison
510 * @return success/failure of operation
511 */
512 gboolean lttv_apply_op_lt_uint64(gpointer v1, char* v2) {
513 guint64* r = (guint64*) v1;
514 guint64 l = atoi(v2);
515 return (*r < l);
516 }
517
518 /**
519 * Applies the 'lower than' operator to the
520 * specified structure and value
521 * @param v1 left member of comparison
522 * @param v2 right member of comparison
523 * @return success/failure of operation
524 */
525 gboolean lttv_apply_op_lt_uint32(gpointer v1, char* v2) {
526 guint32* r = (guint32*) v1;
527 guint32 l = atoi(v2);
528 return (*r < l);
529 }
530
531 /**
532 * Applies the 'lower than' operator to the
533 * specified structure and value
534 * @param v1 left member of comparison
535 * @param v2 right member of comparison
536 * @return success/failure of operation
537 */
538 gboolean lttv_apply_op_lt_uint16(gpointer v1, char* v2) {
539 guint16* r = (guint16*) v1;
540 guint16 l = atoi(v2);
541 return (*r < l);
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_double(gpointer v1, char* v2) {
552 double* r = (double*) v1;
553 double l = atof(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_le_uint64(gpointer v1, char* v2) {
565 guint64* r = (guint64*) v1;
566 guint64 l = atoi(v2);
567 return (*r <= l);
568 }
569
570 /**
571 * Applies the 'lower or equal' operator to the
572 * specified structure and value
573 * @param v1 left member of comparison
574 * @param v2 right member of comparison
575 * @return success/failure of operation
576 */
577 gboolean lttv_apply_op_le_uint32(gpointer v1, char* v2) {
578 guint32* r = (guint32*) v1;
579 guint32 l = atoi(v2);
580 return (*r <= l);
581 }
582
583 /**
584 * Applies the 'lower or equal' 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_le_uint16(gpointer v1, char* v2) {
591 guint16* r = (guint16*) v1;
592 guint16 l = atoi(v2);
593 return (*r <= l);
594 }
595
596 /**
597 * Applies the 'lower or equal' 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_double(gpointer v1, char* v2) {
604 double* r = (double*) v1;
605 double l = atof(v2);
606 return (*r <= l);
607 }
608
609 /**
610 * Applies the 'greater than' 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_gt_uint64(gpointer v1, char* v2) {
617 guint64* r = (guint64*) v1;
618 guint64 l = atoi(v2);
619 return (*r > l);
620 }
621
622 /**
623 * Applies the 'greater than' 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_gt_uint32(gpointer v1, char* v2) {
630 guint32* r = (guint32*) v1;
631 guint32 l = atoi(v2);
632 return (*r > l);
633 }
634
635 /**
636 * Applies the 'greater than' 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_gt_uint16(gpointer v1, char* v2) {
643 guint16* r = (guint16*) v1;
644 guint16 l = atoi(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_double(gpointer v1, char* v2) {
656 double* r = (double*) v1;
657 double l = atof(v2);
658 return (*r > l);
659 }
660
661 /**
662 * Applies the 'greater or equal' 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_ge_uint64(gpointer v1, char* v2) {
669 guint64* r = (guint64*) v1;
670 guint64 l = atoi(v2);
671 return (*r >= l);
672 }
673
674 /**
675 * Applies the 'greater or equal' operator to the
676 * specified structure and value
677 * @param v1 left member of comparison
678 * @param v2 right member of comparison
679 * @return success/failure of operation
680 */
681 gboolean lttv_apply_op_ge_uint32(gpointer v1, char* v2) {
682 guint32* r = (guint32*) v1;
683 guint32 l = atoi(v2);
684 return (*r >= l);
685 }
686
687 /**
688 * Applies the 'greater or equal' 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_ge_uint16(gpointer v1, char* v2) {
695 guint16* r = (guint16*) v1;
696 guint16 l = atoi(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_double(gpointer v1, char* v2) {
708 double* r = (double*) v1;
709 double l = atof(v2);
710 return (*r >= l);
711 }
712
713
714 /**
715 * Makes a copy of the current filter tree
716 * @param tree pointer to the current tree
717 * @return new copy of the filter tree
718 */
719 LttvFilterTree*
720 lttv_filter_tree_clone(LttvFilterTree* tree) {
721
722
723
724 }
725
726 /**
727 * Makes a copy of the current filter
728 * @param filter pointer to the current filter
729 * @return new copy of the filter
730 */
731 LttvFilter*
732 lttv_filter_clone(LttvFilter* filter) {
733
734
735 LttvFilter* newfilter = g_new(LttvFilter,1);
736
737 // newfilter->expression = g_new(char,1)
738 strcpy(newfilter->expression,filter->expression);
739
740 newfilter->head = lttv_filter_tree_clone(filter->head);
741
742 return newfilter;
743
744 }
745
746
747 /**
748 * Creates a new lttv_filter
749 * @param expression filtering options string
750 * @param t pointer to the current LttvTrace
751 * @return the current lttv_filter or NULL if error
752 */
753 LttvFilter*
754 lttv_filter_new(char *expression, LttvTraceState *tcs) {
755
756 g_print("filter::lttv_filter_new()\n"); /* debug */
757
758 unsigned
759 i,
760 p_nesting=0, /* parenthesis nesting value */
761 b=0; /* current breakpoint in expression string */
762
763 /* trees */
764 LttvFilterTree
765 *tree = lttv_filter_tree_new(), /* main tree */
766 *subtree = NULL, /* buffer for subtrees */
767 *t1, /* buffer #1 */
768 *t2; /* buffer #2 */
769
770 /*
771 * Tree Stack
772 * each element of the list
773 * is a sub tree created
774 * by the use of parenthesis in the
775 * global expression. The final tree
776 * will be the one left at the root of
777 * the list
778 */
779 GPtrArray *tree_stack = g_ptr_array_new();
780 g_ptr_array_add( tree_stack,(gpointer) tree );
781
782 /* temporary values */
783 GString *a_field_component = g_string_new("");
784 GPtrArray *a_field_path = NULL;
785
786 LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new();
787
788 /*
789 * Parse entire expression and construct
790 * the binary tree. There are two steps
791 * in browsing that string
792 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
793 * 2. finding simple expressions
794 * - field path ( separated by dots )
795 * - op ( >, <, =, >=, <=, !=)
796 * - value ( integer, string ... )
797 * To spare computing time, the whole
798 * string is parsed in this loop for a
799 * O(n) complexity order.
800 *
801 * When encountering logical op &,|,^
802 * 1. parse the last value if any
803 * 2. create a new tree
804 * 3. add the expression (simple exp, or exp (subtree)) to the tree
805 * 4. concatenate this tree with the current tree on top of the stack
806 * When encountering math ops >,>=,<,<=,=,!=
807 * 1. add to op to the simple expression
808 * 2. concatenate last field component to field path
809 * When encountering concatening ops .
810 * 1. concatenate last field component to field path
811 * When encountering opening parenthesis (,{,[
812 * 1. create a new subtree on top of tree stack
813 * When encountering closing parenthesis ),},]
814 * 1. add the expression on right child of the current tree
815 * 2. the subtree is completed, allocate a new subtree
816 * 3. pop the tree value from the tree stack
817 */
818
819 a_field_path = g_ptr_array_new();
820 g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */
821
822
823 for(i=0;i<strlen(expression);i++) {
824 // g_print("%s\n",a_field_component->str);
825 g_print("%c ",expression[i]);
826 // g_print("switch:%c -->subtree:%p\n",expression[i],subtree);
827 switch(expression[i]) {
828 /*
829 * logical operators
830 */
831 case '&': /* and */
832 t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1);
833 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
834 t2 = lttv_filter_tree_new();
835 t2->node = LTTV_LOGICAL_AND;
836 if(subtree != NULL) { /* append subtree to current tree */
837 t2->left = LTTV_TREE_NODE;
838 t2->l_child.t = subtree;
839 subtree = NULL;
840 t1->right = LTTV_TREE_NODE;
841 t1->r_child.t = t2;
842 } else { /* append a simple expression */
843 a_simple_expression->value = a_field_component->str;
844 a_field_component = g_string_new("");
845 t2->left = LTTV_TREE_LEAF;
846 t2->l_child.leaf = a_simple_expression;
847 a_simple_expression = lttv_simple_expression_new();
848 t1->right = LTTV_TREE_NODE;
849 t1->r_child.t = t2;
850 }
851
852 break;
853 case '|': /* or */
854 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
855 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
856 t2 = lttv_filter_tree_new();
857 t2->node = LTTV_LOGICAL_OR;
858 if(subtree != NULL) { /* append subtree to current tree */
859 t2->left = LTTV_TREE_NODE;
860 t2->l_child.t = subtree;
861 subtree = NULL;
862 t1->right = LTTV_TREE_NODE;
863 t1->r_child.t = t2;
864 } else { /* append a simple expression */
865 a_simple_expression->value = a_field_component->str;
866 a_field_component = g_string_new("");
867 t2->left = LTTV_TREE_LEAF;
868 t2->l_child.leaf = a_simple_expression;
869 a_simple_expression = lttv_simple_expression_new();
870 t1->right = LTTV_TREE_NODE;
871 t1->r_child.t = t2;
872 }
873 break;
874 case '^': /* xor */
875 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
876 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
877 t2 = lttv_filter_tree_new();
878 t2->node = LTTV_LOGICAL_XOR;
879 if(subtree != NULL) { /* append subtree to current tree */
880 t2->left = LTTV_TREE_NODE;
881 t2->l_child.t = subtree;
882 subtree = NULL;
883 t1->right = LTTV_TREE_NODE;
884 t1->r_child.t = t2;
885 } else { /* append a simple expression */
886 a_simple_expression->value = a_field_component->str;
887 a_field_component = g_string_new("");
888 t2->left = LTTV_TREE_LEAF;
889 t2->l_child.leaf = a_simple_expression;
890 a_simple_expression = lttv_simple_expression_new();
891 t1->right = LTTV_TREE_NODE;
892 t1->r_child.t = t2;
893 }
894 break;
895 case '!': /* not, or not equal (math op) */
896 if(expression[i+1] == '=') { /* != */
897 assign_operator(a_simple_expression,LTTV_FIELD_NE);
898 i++;
899 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
900 parse_field_path(a_field_path,a_simple_expression);
901 a_field_component = g_string_new("");
902 } else { /* ! */
903 // g_print("%s\n",a_field_component);
904 // a_field_component = g_string_new("");
905 t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1);
906 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
907 t2 = lttv_filter_tree_new();
908 t2->node = LTTV_LOGICAL_NOT;
909 t1->right = LTTV_TREE_NODE;
910 t1->r_child.t = t2;
911 }
912 break;
913 case '(': /* start of parenthesis */
914 case '[':
915 case '{':
916 p_nesting++; /* incrementing parenthesis nesting value */
917 t1 = lttv_filter_tree_new();
918 g_ptr_array_add( tree_stack,(gpointer) t1 );
919 break;
920 case ')': /* end of parenthesis */
921 case ']':
922 case '}':
923 p_nesting--; /* decrementing parenthesis nesting value */
924 if(p_nesting<0 || tree_stack->len<2) {
925 g_warning("Wrong filtering options, the string\n\"%s\"\n\
926 is not valid due to parenthesis incorrect use",expression);
927 return NULL;
928 }
929
930 g_assert(tree_stack->len>0);
931 if(subtree != NULL) { /* append subtree to current tree */
932 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
933 while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) {
934 g_assert(t1!=NULL && t1->r_child.t != NULL);
935 t1 = t1->r_child.t;
936 }
937 t1->right = LTTV_TREE_NODE;
938 t1->r_child.t = subtree;
939 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
940 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
941 } else { /* assign subtree as current tree */
942 a_simple_expression->value = a_field_component->str;
943 a_field_component = g_string_new("");
944 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
945 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
946 t1->right = LTTV_TREE_LEAF;
947 t1->r_child.leaf = a_simple_expression;
948 a_simple_expression = lttv_simple_expression_new();
949 subtree = g_ptr_array_index(tree_stack,tree_stack->len-1);
950 g_assert(subtree != NULL);
951 g_ptr_array_remove_index(tree_stack,tree_stack->len-1);
952 }
953 break;
954
955 /*
956 * mathematic operators
957 */
958 case '<': /* lower, lower or equal */
959 if(expression[i+1] == '=') { /* <= */
960 i++;
961 assign_operator(a_simple_expression,LTTV_FIELD_LE);
962 } else assign_operator(a_simple_expression,LTTV_FIELD_LT);
963 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
964 parse_field_path(a_field_path,a_simple_expression);
965 a_field_component = g_string_new("");
966 break;
967 case '>': /* higher, higher or equal */
968 if(expression[i+1] == '=') { /* >= */
969 i++;
970 assign_operator(a_simple_expression,LTTV_FIELD_GE);
971 } else assign_operator(a_simple_expression,LTTV_FIELD_GT);
972 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
973 parse_field_path(a_field_path,a_simple_expression);
974 a_field_component = g_string_new("");
975 break;
976 case '=': /* equal */
977 assign_operator(a_simple_expression,LTTV_FIELD_EQ);
978 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
979 parse_field_path(a_field_path,a_simple_expression);
980 a_field_component = g_string_new("");
981 break;
982 /*
983 * Field concatening caracter
984 */
985 case '.': /* dot */
986 /*
987 * divide field expression into elements
988 * in a_field_path array.
989 */
990 if(a_simple_expression->op != NULL) {
991 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
992 a_field_component = g_string_new("");
993 }
994 break;
995 default: /* concatening current string */
996 g_string_append_c(a_field_component,expression[i]);
997 }
998 }
999
1000 g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2);
1001 g_print("stack size: %i\n",tree_stack->len);
1002
1003 /*
1004 * Preliminary check to see
1005 * if tree was constructed correctly
1006 */
1007 if( p_nesting>0 ) {
1008 g_warning("Wrong filtering options, the string\n\"%s\"\n\
1009 is not valid due to parenthesis incorrect use",expression);
1010 return NULL;
1011 }
1012
1013 if(tree_stack->len != 1) /* only root tree should remain */
1014 return NULL;
1015
1016 /* processing last element of expression */
1017 t1 = g_ptr_array_index(tree_stack,tree_stack->len-1);
1018 while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t;
1019 if(subtree != NULL) { /* add the subtree */
1020 t1->right = LTTV_TREE_NODE;
1021 t1->r_child.t = subtree;
1022 subtree = NULL;
1023 } else { /* add a leaf */
1024 a_simple_expression->value = a_field_component->str;
1025 a_field_component = g_string_new("");
1026 t1->right = LTTV_TREE_LEAF;
1027 t1->r_child.leaf = a_simple_expression;
1028 /*
1029 * FIXME: is it really necessary to reallocate
1030 * LttvSimpleExpression at this point ??
1031 */
1032 a_simple_expression = lttv_simple_expression_new();
1033 }
1034
1035 g_assert(tree != NULL);
1036 g_assert(subtree == NULL);
1037
1038 lttv_filter_tracefile(tree,NULL);
1039
1040 return tree;
1041
1042 }
1043
1044 void
1045 lttv_filter_destroy(LttvFilter* filter) {
1046
1047 }
1048
1049 /**
1050 * Assign a new tree for the current expression
1051 * or sub expression
1052 * @return pointer of LttvFilterTree
1053 */
1054 LttvFilterTree* lttv_filter_tree_new() {
1055 LttvFilterTree* tree;
1056
1057 tree = g_new(LttvFilter,1);
1058 tree->node = 0; //g_new(lttv_expression,1);
1059 // tree->node->type = LTTV_UNDEFINED_EXPRESSION;
1060 tree->left = LTTV_TREE_IDLE;
1061 tree->right = LTTV_TREE_IDLE;
1062
1063 return tree;
1064 }
1065
1066 /**
1067 * Destroys the tree and his sub-trees
1068 * @param tree Tree which must be destroyed
1069 */
1070 void lttv_filter_tree_destroy(LttvFilterTree* tree) {
1071
1072 if(tree == NULL) return;
1073
1074 if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf);
1075 else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t);
1076
1077 if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf);
1078 else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t);
1079
1080 g_free(tree->node);
1081 g_free(tree);
1082 }
1083
1084
1085 /**
1086 * Apply the filter to a specific trace
1087 * @param filter the current filter applied
1088 * @param tracefile the trace to apply the filter to
1089 * @return success/failure of operation
1090 */
1091 gboolean
1092 lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) {
1093
1094 LttvFilterTree* t = filter->head;
1095
1096 /*
1097 * Each tree is parsed in inorder.
1098 * This way, it's possible to apply the left filter of the
1099 * tree, then decide whether or not the right branch should
1100 * be parsed depending on the linking logical operator
1101 *
1102 * As for the filtering structure, since we are trying
1103 * to remove elements from the trace, it might be better
1104 * managing an array of all items to be removed ..
1105 */
1106
1107 g_print("node:%p lchild:%p rchild:%p\n",t,t->l_child.t,t->r_child.t);
1108 g_print("node type%i\n",t->node);
1109 if(t->left == LTTV_TREE_NODE) lttv_filter_tracefile(t->l_child.t,NULL);
1110 else if(t->left == LTTV_TREE_LEAF) {
1111 g_assert(t->l_child.leaf->value != NULL);
1112 g_print("%p: left is qqch %i %s\n",t,t->l_child.leaf->op,t->l_child.leaf->value);
1113 }
1114 if(t->right == LTTV_TREE_NODE) lttv_filter_tracefile(t->r_child.t,NULL);
1115 else if(t->right == LTTV_TREE_LEAF) {
1116 g_assert(t->r_child.leaf->value != NULL);
1117 g_print("%p: right is qqch %i %s\n",t,t->r_child.leaf->op,t->r_child.leaf->value);
1118 }
1119
1120 /* test */
1121 /* int i, nb;
1122 char *f_name, *e_name;
1123
1124 char* field = "cpu";
1125
1126 LttvTraceHook h;
1127
1128 LttEventType *et;
1129
1130 LttType *t;
1131
1132 GString *fe_name = g_string_new("");
1133
1134 nb = ltt_trace_eventtype_number(tcs->parent.t);
1135 g_print("NB:%i\n",nb);
1136 for(i = 0 ; i < nb ; i++) {
1137 et = ltt_trace_eventtype_get(tcs->parent.t, i);
1138 e_name = ltt_eventtype_name(et);
1139 f_name = ltt_facility_name(ltt_eventtype_facility(et));
1140 g_string_printf(fe_name, "%s.%s", f_name, e_name);
1141 g_print("facility:%s and event:%s\n",f_name,e_name);
1142 }
1143 */
1144 }
1145
1146 gboolean
1147 lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) {
1148
1149 }
1150
1151 /**
1152 * Apply the filter to a specific event
1153 * @param filter the current filter applied
1154 * @param event the event to apply the filter to
1155 * @return success/failure of operation
1156 */
1157 gboolean
1158 lttv_filter_event(LttvFilter *filter, LttEvent *event) {
1159
1160 }
1161
1162 /**
1163 * Initializes the filter module and specific values
1164 */
1165 static void module_init()
1166 {
1167
1168 /*
1169 * Quarks initialization
1170 * for hardcoded filtering options
1171 *
1172 * TODO: traceset has no yet been defined
1173 */
1174
1175 /* top fields */
1176 // LTTV_FILTER_EVENT = g_quark_from_string("event");
1177 // LTTV_FILTER_TRACE = g_quark_from_string("trace");
1178 // LTTV_FILTER_TRACESET = g_quark_from_string("traceset");
1179 // LTTV_FILTER_STATE = g_quark_from_string("state");
1180 // LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile");
1181
1182 /* event.name, tracefile.name, trace.name */
1183 // LTTV_FILTER_NAME = g_quark_from_string("name");
1184
1185 /* event sub fields */
1186 // LTTV_FILTER_CATEGORY = g_quark_from_string("category");
1187 // LTTV_FILTER_TIME = g_quark_from_string("time");
1188 // LTTV_FILTER_TSC = g_quark_from_string("tsc");
1189
1190 /* state sub fields */
1191 // LTTV_FILTER_PID = g_quark_from_string("pid");
1192 // LTTV_FILTER_PPID = g_quark_from_string("ppid");
1193 // LTTV_FILTER_C_TIME = g_quark_from_string("creation_time");
1194 // LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time");
1195 // LTTV_FILTER_P_NAME = g_quark_from_string("process_name");
1196 // LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode");
1197 // LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode");
1198 // LTTV_FILTER_P_STATUS = g_quark_from_string("process_status");
1199 // LTTV_FILTER_CPU = g_quark_from_string("cpu");
1200
1201 }
1202
1203 /**
1204 * Destroys the filter module and specific values
1205 */
1206 static void module_destroy()
1207 {
1208 }
1209
1210
1211 LTTV_MODULE("filter", "Filters traceset and events", \
1212 "Filters traceset and events specifically to user input", \
1213 module_init, module_destroy)
1214
1215
1216
This page took 0.054072 seconds and 4 git commands to generate.