9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
0769c82f |
2 | * Copyright (C) 2003-2005 Michel Dagenais |
9c312311 |
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 | |
31452f49 |
19 | /* |
48f6f3c2 |
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 |
a4c292d4 |
23 | larger or equal to a specified value. |
24 | */ |
48f6f3c2 |
25 | |
0769c82f |
26 | /* |
27 | * YET TO BE ANSWERED |
f4e9dd16 |
28 | * - the exists an other lttv_filter which conflicts with this one |
0769c82f |
29 | */ |
30 | |
31452f49 |
31 | #include <lttv/filter.h> |
32 | |
31452f49 |
33 | /* |
a4c292d4 |
34 | read_token |
48f6f3c2 |
35 | |
a4c292d4 |
36 | read_expression |
37 | ( read expr ) |
38 | simple expr [ op expr ] |
48f6f3c2 |
39 | |
a4c292d4 |
40 | read_simple_expression |
41 | read_field_path [ rel value ] |
48f6f3c2 |
42 | |
a4c292d4 |
43 | read_field_path |
44 | read_field_component [. field path] |
48f6f3c2 |
45 | |
a4c292d4 |
46 | read_field_component |
47 | name [ \[ value \] ] |
48f6f3c2 |
48 | |
a4c292d4 |
49 | data struct: |
50 | and/or(left/right) |
51 | not(child) |
52 | op(left/right) |
53 | path(component...) -> field |
31452f49 |
54 | */ |
55 | |
1a7fa682 |
56 | GQuark |
57 | LTTV_FILTER_TRACE, |
58 | LTTV_FILTER_TRACESET, |
59 | LTTV_FILTER_TRACEFILE, |
60 | LTTV_FILTER_STATE, |
91ad3f0a |
61 | LTTV_FILTER_EVENT, |
62 | LTTV_FILTER_NAME, |
63 | LTTV_FILTER_CATEGORY, |
64 | LTTV_FILTER_TIME, |
65 | LTTV_FILTER_TSC, |
66 | LTTV_FILTER_PID, |
67 | LTTV_FILTER_PPID, |
68 | LTTV_FILTER_C_TIME, |
69 | LTTV_FILTER_I_TIME, |
70 | LTTV_FILTER_P_NAME, |
71 | LTTV_FILTER_EX_MODE, |
72 | LTTV_FILTER_EX_SUBMODE, |
73 | LTTV_FILTER_P_STATUS, |
74 | LTTV_FILTER_CPU; |
75 | |
f4e9dd16 |
76 | /** |
77 | * Assign a new tree for the current expression |
78 | * or sub expression |
79 | * @return pointer of lttv_filter_tree |
80 | */ |
81 | lttv_filter_tree* lttv_filter_tree_new() { |
82 | lttv_filter_tree* tree; |
83 | |
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; |
89 | |
90 | return tree; |
91 | } |
92 | |
93 | /** |
94 | * Destroys the tree and his sub-trees |
95 | * @param tree Tree which must be destroyed |
96 | */ |
97 | void lttv_filter_tree_destroy(lttv_filter_tree* tree) { |
98 | |
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); |
101 | |
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); |
104 | |
105 | g_free(tree->node); |
106 | g_free(tree); |
107 | } |
1a7fa682 |
108 | |
0769c82f |
109 | /** |
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 |
115 | */ |
116 | gboolean |
f4e9dd16 |
117 | parse_field_path(GPtrArray* fp) { |
0769c82f |
118 | |
f4e9dd16 |
119 | GString* f = NULL; |
120 | g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data; |
0769c82f |
121 | |
91ad3f0a |
122 | if(g_quark_try_string(f->str) == LTTV_FILTER_EVENT) { |
123 | // parse_subfield(fp, LTTV_FILTER_EVENT); |
124 | |
125 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACEFILE) { |
126 | |
127 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACE) { |
128 | |
129 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_STATE) { |
130 | |
131 | } else { |
132 | g_warning("Unrecognized field in filter string"); |
133 | return FALSE; |
0769c82f |
134 | } |
91ad3f0a |
135 | return TRUE; |
0769c82f |
136 | } |
137 | |
31452f49 |
138 | /** |
84a333d6 |
139 | * Add an filtering option to the current tree |
140 | * @param expression Current expression to parse |
141 | * @return success/failure of operation |
142 | */ |
143 | gboolean |
144 | parse_simple_expression(GString* expression) { |
145 | |
146 | unsigned i; |
147 | |
a4c292d4 |
148 | |
0769c82f |
149 | |
a4c292d4 |
150 | |
84a333d6 |
151 | } |
152 | |
153 | /** |
154 | * Creates a new lttv_filter |
31452f49 |
155 | * @param expression filtering options string |
156 | * @param t pointer to the current LttvTrace |
84a333d6 |
157 | * @return the current lttv_filter or NULL if error |
31452f49 |
158 | */ |
f4e9dd16 |
159 | lttv_filter_tree* |
0769c82f |
160 | lttv_filter_new(char *expression, LttvTraceState *tcs) { |
a4c292d4 |
161 | |
0769c82f |
162 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
a4c292d4 |
163 | |
a4c292d4 |
164 | unsigned |
165 | i, |
91ad3f0a |
166 | p_nesting=0, /* parenthesis nesting value */ |
a4c292d4 |
167 | b=0; /* current breakpoint in expression string */ |
31452f49 |
168 | |
f4e9dd16 |
169 | /* |
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 |
176 | * the list |
177 | */ |
18d1226f |
178 | lttv_filter_tree* tree = lttv_filter_tree_new(); |
f4e9dd16 |
179 | lttv_filter_tree* subtree = NULL; |
18d1226f |
180 | // lttv_filter_tree* current_tree = NULL; |
181 | GPtrArray *tree_stack = g_ptr_array_new(); |
182 | g_ptr_array_add( tree_stack,(gpointer) tree ); |
f4e9dd16 |
183 | |
a4c292d4 |
184 | /* temporary values */ |
0769c82f |
185 | GString *a_field_component = g_string_new(""); |
f4e9dd16 |
186 | GPtrArray *a_field_path = NULL; |
187 | |
a4c292d4 |
188 | lttv_simple_expression a_simple_expression; |
0769c82f |
189 | |
a4c292d4 |
190 | /* |
191 | * Parse entire expression and construct |
192 | * the binary tree. There are two steps |
193 | * in browsing that string |
f4e9dd16 |
194 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
a4c292d4 |
195 | * 2. finding simple expressions |
0769c82f |
196 | * - field path ( separated by dots ) |
a4c292d4 |
197 | * - op ( >, <, =, >=, <=, !=) |
0769c82f |
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. |
a4c292d4 |
202 | */ |
f4e9dd16 |
203 | |
18d1226f |
204 | /* |
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 |
221 | */ |
222 | |
f4e9dd16 |
223 | a_field_path = g_ptr_array_new(); |
224 | g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */ |
225 | |
18d1226f |
226 | lttv_filter_tree *t1, *t2; |
227 | |
a4c292d4 |
228 | for(i=0;i<strlen(expression);i++) { |
18d1226f |
229 | // g_print("%s\n",a_field_component->str); |
230 | g_print("%c\n",expression[i]); |
a4c292d4 |
231 | switch(expression[i]) { |
232 | /* |
233 | * logical operators |
234 | */ |
235 | case '&': /* and */ |
18d1226f |
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; |
f4e9dd16 |
241 | if(subtree != NULL) { |
18d1226f |
242 | t2->left = LTTV_TREE_NODE; |
243 | t2->l_child.t = subtree; |
f4e9dd16 |
244 | subtree = NULL; |
18d1226f |
245 | t1->right = LTTV_TREE_NODE; |
246 | t1->l_child.t = t2; |
f4e9dd16 |
247 | } else { |
18d1226f |
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; |
253 | t1->l_child.t = t2; |
f4e9dd16 |
254 | } |
255 | |
256 | break; |
a4c292d4 |
257 | case '|': /* or */ |
f4e9dd16 |
258 | break; |
a4c292d4 |
259 | case '^': /* xor */ |
a4c292d4 |
260 | break; |
261 | case '!': /* not, or not equal (math op) */ |
262 | if(expression[i+1] == '=') { /* != */ |
263 | a_simple_expression.op = LTTV_FIELD_NE; |
264 | i++; |
265 | } else { /* ! */ |
0769c82f |
266 | g_print("%s\n",a_field_component); |
1a7fa682 |
267 | a_field_component = g_string_new(""); |
a4c292d4 |
268 | } |
269 | break; |
270 | case '(': /* start of parenthesis */ |
91ad3f0a |
271 | case '[': |
272 | case '{': |
273 | p_nesting++; /* incrementing parenthesis nesting value */ |
f4e9dd16 |
274 | lttv_filter_tree* subtree = lttv_filter_tree_new(); |
18d1226f |
275 | g_ptr_array_add( tree_stack,(gpointer) subtree ); |
a4c292d4 |
276 | break; |
277 | case ')': /* end of parenthesis */ |
91ad3f0a |
278 | case ']': |
279 | case '}': |
280 | p_nesting--; /* decrementing parenthesis nesting value */ |
18d1226f |
281 | if(p_nesting<0 || tree_stack->len<2) { |
f4e9dd16 |
282 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
283 | is not valid due to parenthesis incorrect use",expression); |
284 | return NULL; |
285 | } |
18d1226f |
286 | |
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); |
293 | t1 = t1->r_child.t; |
294 | } |
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); |
299 | } else { |
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); |
308 | } |
f4e9dd16 |
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); |
319 | break; |
320 | */ |
18d1226f |
321 | // subtree = g_ptr_array_index(tree_stack,tree_stack->len); |
322 | // g_ptr_array_remove_index(tree_stack,tree_stack->len); |
a4c292d4 |
323 | break; |
324 | |
325 | /* |
326 | * mathematic operators |
327 | */ |
328 | case '<': /* lower, lower or equal */ |
329 | if(expression[i+1] == '=') { /* <= */ |
330 | i++; |
f4e9dd16 |
331 | a_simple_expression.op = LTTV_FIELD_LE; |
a4c292d4 |
332 | } else a_simple_expression.op = LTTV_FIELD_LT; |
f4e9dd16 |
333 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
334 | a_field_component = g_string_new(""); |
a4c292d4 |
335 | break; |
336 | case '>': /* higher, higher or equal */ |
337 | if(expression[i+1] == '=') { /* >= */ |
338 | i++; |
f4e9dd16 |
339 | a_simple_expression.op = LTTV_FIELD_GE; |
a4c292d4 |
340 | } else a_simple_expression.op = LTTV_FIELD_GT; |
f4e9dd16 |
341 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
342 | a_field_component = g_string_new(""); |
a4c292d4 |
343 | break; |
344 | case '=': /* equal */ |
345 | a_simple_expression.op = LTTV_FIELD_EQ; |
f4e9dd16 |
346 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
347 | a_field_component = g_string_new(""); |
a4c292d4 |
348 | break; |
0769c82f |
349 | /* |
350 | * Field concatening caracter |
351 | */ |
352 | case '.': /* dot */ |
f4e9dd16 |
353 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
0769c82f |
354 | a_field_component = g_string_new(""); |
355 | break; |
a4c292d4 |
356 | default: /* concatening current string */ |
1a7fa682 |
357 | g_string_append_c(a_field_component,expression[i]); |
a4c292d4 |
358 | } |
359 | } |
360 | |
361 | |
362 | |
91ad3f0a |
363 | if( p_nesting>0 ) { |
a4c292d4 |
364 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
365 | is not valid due to parenthesis incorrect use",expression); |
366 | return NULL; |
367 | } |
31452f49 |
368 | } |
369 | |
84a333d6 |
370 | /** |
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 |
375 | */ |
31452f49 |
376 | gboolean |
91ad3f0a |
377 | lttv_filter_tracefile(lttv_filter_t *filter, LttTracefile *tracefile) { |
0769c82f |
378 | |
379 | |
380 | |
381 | /* test */ |
382 | /* int i, nb; |
383 | char *f_name, *e_name; |
31452f49 |
384 | |
0769c82f |
385 | char* field = "cpu"; |
386 | |
387 | LttvTraceHook h; |
388 | |
389 | LttEventType *et; |
390 | |
391 | LttType *t; |
392 | |
393 | GString *fe_name = g_string_new(""); |
394 | |
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); |
403 | } |
404 | */ |
31452f49 |
405 | } |
406 | |
1a7fa682 |
407 | gboolean |
91ad3f0a |
408 | lttv_filter_tracestate(lttv_filter_t *filter, LttvTraceState *tracestate) { |
341aa948 |
409 | |
410 | } |
1a7fa682 |
411 | |
84a333d6 |
412 | /** |
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 |
417 | */ |
31452f49 |
418 | gboolean |
91ad3f0a |
419 | lttv_filter_event(lttv_filter_t *filter, LttEvent *event) { |
31452f49 |
420 | |
421 | } |
1a7fa682 |
422 | |
91ad3f0a |
423 | /** |
424 | * Initializes the filter module and specific values |
425 | */ |
1a7fa682 |
426 | static void module_init() |
427 | { |
91ad3f0a |
428 | |
429 | /* |
430 | * Quarks initialization |
431 | * for hardcoded filtering options |
432 | * |
433 | * TODO: traceset has no yet been defined |
434 | */ |
435 | |
436 | /* top fields */ |
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"); |
1a7fa682 |
442 | |
91ad3f0a |
443 | /* event.name, tracefile.name, trace.name */ |
444 | LTTV_FILTER_NAME = g_quark_from_string("name"); |
445 | |
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"); |
450 | |
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"); |
461 | |
1a7fa682 |
462 | } |
463 | |
91ad3f0a |
464 | /** |
465 | * Destroys the filter module and specific values |
466 | */ |
1a7fa682 |
467 | static void module_destroy() |
468 | { |
469 | } |
470 | |
471 | |
91ad3f0a |
472 | LTTV_MODULE("filter", "Filters traceset and events", \ |
473 | "Filters traceset and events specifically to user input", \ |
1a7fa682 |
474 | module_init, module_destroy) |
475 | |
476 | |
477 | |