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,
20 consist in AND, OR and NOT nested expressions, forming a tree with
21 simple relations as leaves. The simple relations test is a field
22 in an event is equal, not equal, smaller, smaller or equal, larger, or
23 larger or equal to a specified value.
28 * - the exists an other lttv_filter which conflicts with this one
31 #include <lttv/filter.h>
38 simple expr [ op expr ]
40 read_simple_expression
41 read_field_path [ rel value ]
44 read_field_component [. field path]
53 path(component...) -> field
59 LTTV_FILTER_TRACEFILE
,
72 LTTV_FILTER_EX_SUBMODE
,
77 * Assign a new tree for the current expression
79 * @return pointer of lttv_filter_tree
81 lttv_filter_tree
* lttv_filter_tree_new() {
82 lttv_filter_tree
* tree
;
84 tree
= g_new(lttv_filter_tree
,1);
85 tree
->node
= g_new(lttv_expression
,1);
86 tree
->node
->type
= LTTV_UNDEFINED_EXPRESSION
;
87 tree
->left
= LTTV_TREE_UNDEFINED
;
88 tree
->right
= LTTV_TREE_UNDEFINED
;
94 * Destroys the tree and his sub-trees
95 * @param tree Tree which must be destroyed
97 void lttv_filter_tree_destroy(lttv_filter_tree
* tree
) {
99 if(tree
->left
== LTTV_TREE_LEAF
) g_free(tree
->l_child
.leaf
);
100 else if(tree
->left
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->l_child
.t
);
102 if(tree
->right
== LTTV_TREE_LEAF
) g_free(tree
->r_child
.leaf
);
103 else if(tree
->right
== LTTV_TREE_NODE
) lttv_filter_tree_destroy(tree
->r_child
.t
);
110 * Parse through filtering field hierarchy as specified
111 * by user. This function compares each value to
112 * predetermined quarks
113 * @param fp The field path list
114 * @return success/failure of operation
117 parse_field_path(GPtrArray
* fp
) {
120 g_assert(f
=g_ptr_array_index(fp
,0)); //list_first(fp)->data;
122 if(g_quark_try_string(f
->str
) == LTTV_FILTER_EVENT
) {
123 // parse_subfield(fp, LTTV_FILTER_EVENT);
125 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACEFILE
) {
127 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACE
) {
129 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_STATE
) {
132 g_warning("Unrecognized field in filter string");
139 * Add an filtering option to the current tree
140 * @param expression Current expression to parse
141 * @return success/failure of operation
144 parse_simple_expression(GString
* expression
) {
154 * Creates a new lttv_filter
155 * @param expression filtering options string
156 * @param t pointer to the current LttvTrace
157 * @return the current lttv_filter or NULL if error
160 lttv_filter_new(char *expression
, LttvTraceState
*tcs
) {
162 g_print("filter::lttv_filter_new()\n"); /* debug */
166 p_nesting
=0, /* parenthesis nesting value */
167 b
=0; /* current breakpoint in expression string */
170 * Main tree & Tree concatening list
171 * each element of the list
172 * is a sub tree created
173 * by the use of parenthesis in the
174 * global expression. The final tree
175 * will be the one created at the root of
178 lttv_filter_tree
* tree
= lttv_filter_tree_new();
179 lttv_filter_tree
* subtree
= NULL
;
180 // lttv_filter_tree* current_tree = NULL;
181 GPtrArray
*tree_stack
= g_ptr_array_new();
182 g_ptr_array_add( tree_stack
,(gpointer
) tree
);
184 /* temporary values */
185 GString
*a_field_component
= g_string_new("");
186 GPtrArray
*a_field_path
= NULL
;
188 lttv_simple_expression a_simple_expression
;
191 * Parse entire expression and construct
192 * the binary tree. There are two steps
193 * in browsing that string
194 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
195 * 2. finding simple expressions
196 * - field path ( separated by dots )
197 * - op ( >, <, =, >=, <=, !=)
198 * - value ( integer, string ... )
199 * To spare computing time, the whole
200 * string is parsed in this loop for a
201 * O(n) complexity order.
205 * When encountering logical op &,|,^
206 * 1. parse the last value if any
207 * 2. create a new tree
208 * 3. add the expression (simple exp, or exp (subtree)) to the tree
209 * 4. concatenate this tree with the current tree on top of the stack
210 * When encountering math ops >,>=,<,<=,=,!=
211 * 1. add to op to the simple expression
212 * 2. concatenate last field component to field path
213 * When encountering concatening ops .
214 * 1. concatenate last field component to field path
215 * When encountering opening parenthesis (,{,[
216 * 1. create a new subtree on top of tree stack
217 * When encountering closing parenthesis ),},]
218 * 1. add the expression on right child of the current tree
219 * 2. the subtree is completed, allocate a new subtree
220 * 3. pop the tree value from the tree stack
223 a_field_path
= g_ptr_array_new();
224 g_ptr_array_set_size(a_field_path
,2); /* by default, recording 2 field expressions */
226 lttv_filter_tree
*t1
, *t2
;
228 for(i
=0;i
<strlen(expression
);i
++) {
229 // g_print("%s\n",a_field_component->str);
230 g_print("%c\n",expression
[i
]);
231 switch(expression
[i
]) {
236 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
237 while(t1
->right
!= LTTV_TREE_UNDEFINED
) t1
= t1
->r_child
.t
;
238 t2
= lttv_filter_tree_new();
239 t2
->node
->type
= LTTV_EXPRESSION_OP
;
240 t2
->node
->e
.op
= LTTV_LOGICAL_AND
;
241 if(subtree
!= NULL
) {
242 t2
->left
= LTTV_TREE_NODE
;
243 t2
->l_child
.t
= subtree
;
245 t1
->right
= LTTV_TREE_NODE
;
248 a_simple_expression
.value
= a_field_component
->str
;
249 a_field_component
= g_string_new("");
250 t2
->left
= LTTV_TREE_LEAF
;
251 t2
->l_child
.leaf
= g_new(lttv_simple_expression
,1);
252 t1
->right
= LTTV_TREE_NODE
;
261 case '!': /* not, or not equal (math op) */
262 if(expression
[i
+1] == '=') { /* != */
263 a_simple_expression
.op
= LTTV_FIELD_NE
;
266 g_print("%s\n",a_field_component
);
267 a_field_component
= g_string_new("");
270 case '(': /* start of parenthesis */
273 p_nesting
++; /* incrementing parenthesis nesting value */
274 lttv_filter_tree
* subtree
= lttv_filter_tree_new();
275 g_ptr_array_add( tree_stack
,(gpointer
) subtree
);
277 case ')': /* end of parenthesis */
280 p_nesting
--; /* decrementing parenthesis nesting value */
281 if(p_nesting
<0 || tree_stack
->len
<2) {
282 g_warning("Wrong filtering options, the string\n\"%s\"\n\
283 is not valid due to parenthesis incorrect use",expression
);
287 g_assert(tree_stack
->len
>0);
288 if(subtree
!= NULL
) {
289 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
290 /* FIXME ==> SEG FAULT */
291 while(t1
->right
!= LTTV_TREE_UNDEFINED
&& t1
->right
!= LTTV_TREE_LEAF
) {
292 g_assert(t1
!=NULL
&& t1
->r_child
.t
!= NULL
);
295 t1
->right
= LTTV_TREE_NODE
;
296 t1
->r_child
.t
= subtree
;
297 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
298 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
300 a_simple_expression
.value
= a_field_component
->str
;
301 a_field_component
= g_string_new("");
302 t1
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
303 while(t1
->right
!= LTTV_TREE_UNDEFINED
) t1
= t1
->r_child
.t
;
304 t1
->right
= LTTV_TREE_LEAF
;
305 t1
->r_child
.leaf
= g_new(lttv_simple_expression
,1);
306 subtree
= g_ptr_array_index(tree_stack
,tree_stack
->len
-1);
307 g_ptr_array_remove_index(tree_stack
,tree_stack
->len
-1);
309 /* lttv_filter_tree *sub1 = g_ptr_array_index(tree_list,tree_list->len-1);
310 lttv_filter_tree *sub2 = g_ptr_array_index(tree_list,tree_list->len);
311 if(sub1->left == LTTV_TREE_UNDEFINED){
312 sub1->l_child.t = sub2;
313 sub1->left = LTTV_TREE_NODE;
314 } else if(sub1->right == LTTV_TREE_UNDEFINED){
315 sub1->r_child.t = sub2;
316 sub1->right = LTTV_TREE_NODE;
317 } else g_error("error during tree assignation");
318 g_ptr_array_remove_index(tree_list,tree_list->len);
321 // subtree = g_ptr_array_index(tree_stack,tree_stack->len);
322 // g_ptr_array_remove_index(tree_stack,tree_stack->len);
326 * mathematic operators
328 case '<': /* lower, lower or equal */
329 if(expression
[i
+1] == '=') { /* <= */
331 a_simple_expression
.op
= LTTV_FIELD_LE
;
332 } else a_simple_expression
.op
= LTTV_FIELD_LT
;
333 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
334 a_field_component
= g_string_new("");
336 case '>': /* higher, higher or equal */
337 if(expression
[i
+1] == '=') { /* >= */
339 a_simple_expression
.op
= LTTV_FIELD_GE
;
340 } else a_simple_expression
.op
= LTTV_FIELD_GT
;
341 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
342 a_field_component
= g_string_new("");
344 case '=': /* equal */
345 a_simple_expression
.op
= LTTV_FIELD_EQ
;
346 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
347 a_field_component
= g_string_new("");
350 * Field concatening caracter
353 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
354 a_field_component
= g_string_new("");
356 default: /* concatening current string */
357 g_string_append_c(a_field_component
,expression
[i
]);
364 g_warning("Wrong filtering options, the string\n\"%s\"\n\
365 is not valid due to parenthesis incorrect use",expression
);
371 * Apply the filter to a specific trace
372 * @param filter the current filter applied
373 * @param tracefile the trace to apply the filter to
374 * @return success/failure of operation
377 lttv_filter_tracefile(lttv_filter_t
*filter
, LttTracefile
*tracefile
) {
383 char *f_name, *e_name;
393 GString *fe_name = g_string_new("");
395 nb = ltt_trace_eventtype_number(tcs->parent.t);
396 g_print("NB:%i\n",nb);
397 for(i = 0 ; i < nb ; i++) {
398 et = ltt_trace_eventtype_get(tcs->parent.t, i);
399 e_name = ltt_eventtype_name(et);
400 f_name = ltt_facility_name(ltt_eventtype_facility(et));
401 g_string_printf(fe_name, "%s.%s", f_name, e_name);
402 g_print("facility:%s and event:%s\n",f_name,e_name);
408 lttv_filter_tracestate(lttv_filter_t
*filter
, LttvTraceState
*tracestate
) {
413 * Apply the filter to a specific event
414 * @param filter the current filter applied
415 * @param event the event to apply the filter to
416 * @return success/failure of operation
419 lttv_filter_event(lttv_filter_t
*filter
, LttEvent
*event
) {
424 * Initializes the filter module and specific values
426 static void module_init()
430 * Quarks initialization
431 * for hardcoded filtering options
433 * TODO: traceset has no yet been defined
437 LTTV_FILTER_EVENT
= g_quark_from_string("event");
438 LTTV_FILTER_TRACE
= g_quark_from_string("trace");
439 LTTV_FILTER_TRACESET
= g_quark_from_string("traceset");
440 LTTV_FILTER_STATE
= g_quark_from_string("state");
441 LTTV_FILTER_TRACEFILE
= g_quark_from_string("tracefile");
443 /* event.name, tracefile.name, trace.name */
444 LTTV_FILTER_NAME
= g_quark_from_string("name");
446 /* event sub fields */
447 LTTV_FILTER_CATEGORY
= g_quark_from_string("category");
448 LTTV_FILTER_TIME
= g_quark_from_string("time");
449 LTTV_FILTER_TSC
= g_quark_from_string("tsc");
451 /* state sub fields */
452 LTTV_FILTER_PID
= g_quark_from_string("pid");
453 LTTV_FILTER_PPID
= g_quark_from_string("ppid");
454 LTTV_FILTER_C_TIME
= g_quark_from_string("creation_time");
455 LTTV_FILTER_I_TIME
= g_quark_from_string("insertion_time");
456 LTTV_FILTER_P_NAME
= g_quark_from_string("process_name");
457 LTTV_FILTER_EX_MODE
= g_quark_from_string("execution_mode");
458 LTTV_FILTER_EX_SUBMODE
= g_quark_from_string("execution_submode");
459 LTTV_FILTER_P_STATUS
= g_quark_from_string("process_status");
460 LTTV_FILTER_CPU
= g_quark_from_string("cpu");
465 * Destroys the filter module and specific values
467 static void module_destroy()
472 LTTV_MODULE("filter", "Filters traceset and events", \
473 "Filters traceset and events specifically to user input", \
474 module_init
, module_destroy
)