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