1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais
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;
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.
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,
24 simple expr [ op expr ]
26 read_simple_expression
27 read_field_path [ rel value ]
30 read_field_component [. field path]
39 path(component...) -> field
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.
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
59 #include <lttv/filter.h>
65 LTTV_FILTER_TRACEFILE,
78 LTTV_FILTER_EX_SUBMODE,
84 lttv_simple_expression_new() {
88 * add a node to the current tree
89 * @param stack the tree stack
90 * @param subtree the subtree if available (pointer or NULL)
91 * @param op the logical operator that will form the node
94 lttv_filter_tree_add_node(GPtrArray
* stack
, LttvFilter
* subtree
, LttvLogicalOp op
) {
96 LttvFilter
* t1
= NULL
;
97 LttvFilter
* t2
= NULL
;
99 t1
= (LttvFilter
*)g_ptr_array_index(stack
,stack
->len
-1);
100 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
101 t2
= lttv_filter_tree_new();
103 if(subtree
!= NULL
) {
104 t2
->left
= LTTV_TREE_NODE
;
105 t2
->l_child
.t
= subtree
;
107 t1
->right
= LTTV_TREE_NODE
;
110 // a_simple_expression->value = a_field_component->str;
111 // a_field_component = g_string_new("");
112 t2
->left
= LTTV_TREE_LEAF
;
113 // t2->l_child.leaf = a_simple_expression;
114 // a_simple_expression = g_new(lttv_simple_expression,1);
115 t1
->right
= LTTV_TREE_NODE
;
122 * Parse through filtering field hierarchy as specified
123 * by user. This function compares each value to
124 * predetermined quarks
125 * @param fp The field path list
126 * @return success/failure of operation
129 parse_field_path(GPtrArray
* fp
) {
132 if(fp
->len
< 2) return FALSE
;
133 g_assert(f
=g_ptr_array_index(fp
,0)); //list_first(fp)->data;
135 if(g_quark_try_string(f
->str
) == LTTV_FILTER_EVENT
) {
136 f
=g_ptr_array_index(fp
,1);
137 if(g_quark_try_string(f
->str
) == LTTV_FILTER_NAME
) {}
138 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_CATEGORY
) {}
139 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TIME
) {
140 // offset = &((LttEvent*)NULL)->event_time);
142 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TSC
) {
143 // offset = &((LttEvent*)NULL)->event_cycle_count);
145 else { /* core.xml specified options */
148 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACEFILE
) {
149 f
=g_ptr_array_index(fp
,1);
150 if(g_quark_try_string(f
->str
) == LTTV_FILTER_NAME
) {}
152 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACE
) {
153 f
=g_ptr_array_index(fp
,1);
154 if(g_quark_try_string(f
->str
) == LTTV_FILTER_NAME
) {}
157 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_STATE
) {
158 f
=g_ptr_array_index(fp
,1);
159 if(g_quark_try_string(f
->str
) == LTTV_FILTER_PID
) {}
160 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_PPID
) {}
161 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_C_TIME
) {}
162 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_I_TIME
) {}
163 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_P_NAME
) {}
164 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_EX_MODE
) {}
165 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_EX_SUBMODE
) {}
166 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_P_STATUS
) {}
167 else if(g_quark_try_string(f
->str
) == LTTV_FILTER_CPU
) {}
171 g_warning("Unrecognized field in filter string");
178 * Add an filtering option to the current tree
179 * @param expression Current expression to parse
180 * @return success/failure of operation
183 parse_simple_expression(GString
* expression
) {
193 * Applies the 'equal' operator to the
194 * specified structure and value
195 * @return success/failure of operation
197 gboolean
lttv_apply_op_eq() {
202 * Applies the 'not equal' operator to the
203 * specified structure and value
204 * @return success/failure of operation
206 gboolean
lttv_apply_op_ne() {
211 * Applies the 'lower than' operator to the
212 * specified structure and value
213 * @return success/failure of operation
215 gboolean
lttv_apply_op_lt() {
220 * Applies the 'lower or equal' operator to the
221 * specified structure and value
222 * @return success/failure of operation
224 gboolean
lttv_apply_op_le() {
229 * Applies the 'greater than' operator to the
230 * specified structure and value
231 * @return success/failure of operation
233 gboolean
lttv_apply_op_gt() {
238 * Applies the 'greater or equal' operator to the
239 * specified structure and value
240 * @return success/failure of operation
242 gboolean
lttv_apply_op_ge() {
249 * Makes a copy of the current filter tree
250 * @param tree pointer to the current tree
251 * @return new copy of the filter tree
254 lttv_filter_tree_clone(LttvFilterTree
* tree
) {
261 * Makes a copy of the current filter
262 * @param filter pointer to the current filter
263 * @return new copy of the filter
266 lttv_filter_clone(LttvFilter
* filter
) {
269 LttvFilter
* newfilter
= g_new(LttvFilter
,1);
271 // newfilter->expression = g_new(char,1)
272 strcpy(newfilter
->expression
,filter
->expression
);
274 newfilter
->head
= lttv_filter_tree_clone(filter
->head
);
282 * Creates a new lttv_filter
283 * @param expression filtering options string
284 * @param t pointer to the current LttvTrace
285 * @return the current lttv_filter or NULL if error
288 lttv_filter_new(char *expression
, LttvTraceState
*tcs
) {
290 g_print("filter::lttv_filter_new()\n"); /* debug */
294 p_nesting
=0, /* parenthesis nesting value */
295 b
=0; /* current breakpoint in expression string */
299 *tree
= lttv_filter_tree_new(), /* main tree */
300 *subtree
= NULL
, /* buffer for subtrees */
306 * each element of the list
307 * is a sub tree created
308 * by the use of parenthesis in the
309 * global expression. The final tree
310 * will be the one left at the root of
313 GPtrArray
*tree_stack
= g_ptr_array_new();
314 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
316 /* temporary values */
317 GString
*a_field_component
= g_string_new("");
318 GPtrArray
*a_field_path
= NULL
;
320 LttvSimpleExpression
* a_simple_expression
= g_new(LttvSimpleExpression
,1);
323 * Parse entire expression and construct
324 * the binary tree. There are two steps
325 * in browsing that string
326 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
327 * 2. finding simple expressions
328 * - field path ( separated by dots )
329 * - op ( >, <, =, >=, <=, !=)
330 * - value ( integer, string ... )
331 * To spare computing time, the whole
332 * string is parsed in this loop for a
333 * O(n) complexity order.
335 * When encountering logical op &,|,^
336 * 1. parse the last value if any
337 * 2. create a new tree
338 * 3. add the expression (simple exp, or exp (subtree)) to the tree
339 * 4. concatenate this tree with the current tree on top of the stack
340 * When encountering math ops >,>=,<,<=,=,!=
341 * 1. add to op to the simple expression
342 * 2. concatenate last field component to field path
343 * When encountering concatening ops .
344 * 1. concatenate last field component to field path
345 * When encountering opening parenthesis (,{,[
346 * 1. create a new subtree on top of tree stack
347 * When encountering closing parenthesis ),},]
348 * 1. add the expression on right child of the current tree
349 * 2. the subtree is completed, allocate a new subtree
350 * 3. pop the tree value from the tree stack
353 a_field_path
= g_ptr_array_new();
354 g_ptr_array_set_size(a_field_path
,2); /* by default, recording 2 field expressions */
357 for(i
=0;i
<strlen(expression
);i
++) {
358 // g_print("%s\n",a_field_component->str);
359 g_print("%c ",expression
[i
]);
360 // g_print("switch:%c -->subtree:%p\n",expression[i],subtree);
361 switch(expression
[i
]) {
366 t1
= (LttvFilter
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
367 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
368 t2
= lttv_filter_tree_new();
369 t2
->node
= LTTV_LOGICAL_AND
;
370 if(subtree
!= NULL
) {
371 t2
->left
= LTTV_TREE_NODE
;
372 t2
->l_child
.t
= subtree
;
374 t1
->right
= LTTV_TREE_NODE
;
377 a_simple_expression
->value
= a_field_component
->str
;
378 a_field_component
= g_string_new("");
379 t2
->left
= LTTV_TREE_LEAF
;
380 t2
->l_child
.leaf
= a_simple_expression
;
381 a_simple_expression
= g_new(LttvSimpleExpression
,1);
382 t1
->right
= LTTV_TREE_NODE
;
388 t1
= (LttvFilter
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
389 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
390 t2
= lttv_filter_tree_new();
391 t2
->node
= LTTV_LOGICAL_OR
;
392 if(subtree
!= NULL
) {
393 t2
->left
= LTTV_TREE_NODE
;
394 t2
->l_child
.t
= subtree
;
396 t1
->right
= LTTV_TREE_NODE
;
399 a_simple_expression
->value
= a_field_component
->str
;
400 a_field_component
= g_string_new("");
401 t2
->left
= LTTV_TREE_LEAF
;
402 t2
->l_child
.leaf
= a_simple_expression
;
403 a_simple_expression
= g_new(LttvSimpleExpression
,1);
404 t1
->right
= LTTV_TREE_NODE
;
409 t1
= (LttvFilter
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
410 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
411 t2
= lttv_filter_tree_new();
412 t2
->node
= LTTV_LOGICAL_XOR
;
413 if(subtree
!= NULL
) {
414 t2
->left
= LTTV_TREE_NODE
;
415 t2
->l_child
.t
= subtree
;
417 t1
->right
= LTTV_TREE_NODE
;
420 a_simple_expression
->value
= a_field_component
->str
;
421 a_field_component
= g_string_new("");
422 t2
->left
= LTTV_TREE_LEAF
;
423 t2
->l_child
.leaf
= a_simple_expression
;
424 a_simple_expression
= g_new(LttvSimpleExpression
,1);
425 t1
->right
= LTTV_TREE_NODE
;
429 case '!': /* not, or not equal (math op) */
430 if(expression
[i
+1] == '=') { /* != */
431 a_simple_expression
->op
= LTTV_FIELD_NE
;
433 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
434 a_field_component
= g_string_new("");
436 // g_print("%s\n",a_field_component);
437 // a_field_component = g_string_new("");
438 t1
= (LttvFilter
*)g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
439 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
440 t2
= lttv_filter_tree_new();
441 t2
->node
= LTTV_LOGICAL_NOT
;
442 t1
->right
= LTTV_TREE_NODE
;
446 case '(': /* start of parenthesis */
449 p_nesting
++; /* incrementing parenthesis nesting value */
450 t1
= lttv_filter_tree_new();
451 g_ptr_array_add( tree_stack
,(gpointer
) t1
);
453 case ')': /* end of parenthesis */
456 p_nesting
--; /* decrementing parenthesis nesting value */
457 if(p_nesting
<0 || tree_stack
->len
<2) {
458 g_warning("Wrong filtering options, the string\n\"%s\"\n\
459 is not valid due to parenthesis incorrect use",expression
);
463 g_assert(tree_stack
->len
>0);
464 if(subtree
!= NULL
) {
465 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
466 while(t1
->right
!= LTTV_TREE_IDLE
&& t1
->right
!= LTTV_TREE_LEAF
) {
467 g_assert(t1
!=NULL
&& t1
->r_child
.t
!= NULL
);
470 t1
->right
= LTTV_TREE_NODE
;
471 t1
->r_child
.t
= subtree
;
472 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
473 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
475 a_simple_expression
->value
= a_field_component
->str
;
476 a_field_component
= g_string_new("");
477 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
478 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
479 t1
->right
= LTTV_TREE_LEAF
;
480 t1
->r_child
.leaf
= a_simple_expression
;
481 a_simple_expression
= g_new(LttvSimpleExpression
,1);
482 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
483 g_assert(subtree
!= NULL
);
484 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
489 * mathematic operators
491 case '<': /* lower, lower or equal */
492 if(expression
[i
+1] == '=') { /* <= */
494 a_simple_expression
->op
= LTTV_FIELD_LE
;
495 } else a_simple_expression
->op
= LTTV_FIELD_LT
;
496 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
497 a_field_component
= g_string_new("");
499 case '>': /* higher, higher or equal */
500 if(expression
[i
+1] == '=') { /* >= */
502 a_simple_expression
->op
= LTTV_FIELD_GE
;
503 } else a_simple_expression
->op
= LTTV_FIELD_GT
;
504 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
505 a_field_component
= g_string_new("");
507 case '=': /* equal */
508 a_simple_expression
->op
= LTTV_FIELD_EQ
;
509 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
510 a_field_component
= g_string_new("");
513 * Field concatening caracter
516 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
517 a_field_component
= g_string_new("");
519 default: /* concatening current string */
520 g_string_append_c(a_field_component
,expression
[i
]);
524 g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree
,tree
,t1
,t2
);
525 g_print("stack size: %i\n",tree_stack
->len
);
528 * Preliminary check to see
529 * if tree was constructed correctly
532 g_warning("Wrong filtering options, the string\n\"%s\"\n\
533 is not valid due to parenthesis incorrect use",expression
);
537 if(tree_stack
->len
!= 1) /* only root tree should remain */
540 /* processing last element of expression */
541 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
542 while(t1
->right
!= LTTV_TREE_IDLE
) t1
= t1
->r_child
.t
;
543 if(subtree
!= NULL
) { /* add the subtree */
544 t1
->right
= LTTV_TREE_NODE
;
545 t1
->r_child
.t
= subtree
;
547 } else { /* add a leaf */
548 a_simple_expression
->value
= a_field_component
->str
;
549 a_field_component
= g_string_new("");
550 t1
->right
= LTTV_TREE_LEAF
;
551 t1
->r_child
.leaf
= a_simple_expression
;
553 * FIXME: is it really necessary to reallocate
554 * LttvSimpleExpression at this point ??
556 a_simple_expression
= g_new(LttvSimpleExpression
,1);
559 g_assert(tree
!= NULL
);
560 g_assert(subtree
== NULL
);
562 lttv_filter_tracefile(tree
,NULL
);
569 lttv_filter_destroy(LttvFilter
* filter
) {
574 * Assign a new tree for the current expression
576 * @return pointer of LttvFilterTree
578 LttvFilterTree
* lttv_filter_tree_new() {
579 LttvFilterTree
* tree
;
581 tree
= g_new(LttvFilter
,1);
582 tree
->node
= 0; //g_new(lttv_expression,1);
583 // tree->node->type = LTTV_UNDEFINED_EXPRESSION;
584 tree
->left
= LTTV_TREE_IDLE
;
585 tree
->right
= LTTV_TREE_IDLE
;
591 * Destroys the tree and his sub-trees
592 * @param tree Tree which must be destroyed
594 void lttv_filter_tree_destroy(LttvFilterTree
* tree
) {
596 if(tree
== NULL
) return;
598 if(tree
->left
== LTTV_TREE_LEAF
) g_free(tree
->l_child
.leaf
);
599 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
601 if(tree
->right
== LTTV_TREE_LEAF
) g_free(tree
->r_child
.leaf
);
602 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
610 * Apply the filter to a specific trace
611 * @param filter the current filter applied
612 * @param tracefile the trace to apply the filter to
613 * @return success/failure of operation
616 lttv_filter_tracefile(LttvFilter
*filter
, LttTracefile
*tracefile
) {
619 * Each tree is parsed in inorder.
620 * This way, it's possible to apply the left filter of the
621 * tree, then decide whether or not the right branch should
622 * be parsed depending on the linking logical operator
624 * As for the filtering structure, since we are trying
625 * to remove elements from the trace, it might be better
626 * managing an array of all items to be removed ..
629 g_print("node:%p lchild:%p rchild:%p\n",filter
,filter
->l_child
.t
,filter
->r_child
.t
);
630 g_print("node type%i\n",filter
->node
);
631 if(filter
->left
== LTTV_TREE_NODE
) lttv_filter_tracefile(filter
->l_child
.t
,NULL
);
632 else if(filter
->left
== LTTV_TREE_LEAF
) {
633 g_assert(filter
->l_child
.leaf
->value
!= NULL
);
634 g_print("%p: left is qqch %i %s\n",filter
,filter
->l_child
.leaf
->op
,filter
->l_child
.leaf
->value
);
636 if(filter
->right
== LTTV_TREE_NODE
) lttv_filter_tracefile(filter
->r_child
.t
,NULL
);
637 else if(filter
->right
== LTTV_TREE_LEAF
) {
638 g_assert(filter
->r_child
.leaf
->value
!= NULL
);
639 g_print("%p: right is qqch %i %s\n",filter
,filter
->r_child
.leaf
->op
,filter
->r_child
.leaf
->value
);
644 char *f_name, *e_name;
654 GString *fe_name = g_string_new("");
656 nb = ltt_trace_eventtype_number(tcs->parent.t);
657 g_print("NB:%i\n",nb);
658 for(i = 0 ; i < nb ; i++) {
659 et = ltt_trace_eventtype_get(tcs->parent.t, i);
660 e_name = ltt_eventtype_name(et);
661 f_name = ltt_facility_name(ltt_eventtype_facility(et));
662 g_string_printf(fe_name, "%s.%s", f_name, e_name);
663 g_print("facility:%s and event:%s\n",f_name,e_name);
669 lttv_filter_tracestate(LttvFilter
*filter
, LttvTraceState
*tracestate
) {
674 * Apply the filter to a specific event
675 * @param filter the current filter applied
676 * @param event the event to apply the filter to
677 * @return success/failure of operation
680 lttv_filter_event(LttvFilter
*filter
, LttEvent
*event
) {
685 * Initializes the filter module and specific values
687 static void module_init()
691 * Quarks initialization
692 * for hardcoded filtering options
694 * TODO: traceset has no yet been defined
698 LTTV_FILTER_EVENT
= g_quark_from_string("event");
699 LTTV_FILTER_TRACE
= g_quark_from_string("trace");
700 LTTV_FILTER_TRACESET
= g_quark_from_string("traceset");
701 LTTV_FILTER_STATE
= g_quark_from_string("state");
702 LTTV_FILTER_TRACEFILE
= g_quark_from_string("tracefile");
704 /* event.name, tracefile.name, trace.name */
705 LTTV_FILTER_NAME
= g_quark_from_string("name");
707 /* event sub fields */
708 LTTV_FILTER_CATEGORY
= g_quark_from_string("category");
709 LTTV_FILTER_TIME
= g_quark_from_string("time");
710 LTTV_FILTER_TSC
= g_quark_from_string("tsc");
712 /* state sub fields */
713 LTTV_FILTER_PID
= g_quark_from_string("pid");
714 LTTV_FILTER_PPID
= g_quark_from_string("ppid");
715 LTTV_FILTER_C_TIME
= g_quark_from_string("creation_time");
716 LTTV_FILTER_I_TIME
= g_quark_from_string("insertion_time");
717 LTTV_FILTER_P_NAME
= g_quark_from_string("process_name");
718 LTTV_FILTER_EX_MODE
= g_quark_from_string("execution_mode");
719 LTTV_FILTER_EX_SUBMODE
= g_quark_from_string("execution_submode");
720 LTTV_FILTER_P_STATUS
= g_quark_from_string("process_status");
721 LTTV_FILTER_CPU
= g_quark_from_string("cpu");
726 * Destroys the filter module and specific values
728 static void module_destroy()
733 LTTV_MODULE("filter", "Filters traceset and events", \
734 "Filters traceset and events specifically to user input", \
735 module_init
, module_destroy
)
This page took 0.043256 seconds and 5 git commands to generate.