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 | /* |
a4c292d4 |
20 | read_token |
48f6f3c2 |
21 | |
a4c292d4 |
22 | read_expression |
23 | ( read expr ) |
24 | simple expr [ op expr ] |
48f6f3c2 |
25 | |
a4c292d4 |
26 | read_simple_expression |
27 | read_field_path [ rel value ] |
48f6f3c2 |
28 | |
a4c292d4 |
29 | read_field_path |
30 | read_field_component [. field path] |
48f6f3c2 |
31 | |
a4c292d4 |
32 | read_field_component |
33 | name [ \[ value \] ] |
48f6f3c2 |
34 | |
a4c292d4 |
35 | data struct: |
36 | and/or(left/right) |
37 | not(child) |
38 | op(left/right) |
39 | path(component...) -> field |
150f0d33 |
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. |
31452f49 |
45 | */ |
46 | |
150f0d33 |
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 |
389ba50e |
57 | * * clear the field_path array after use |
150f0d33 |
58 | */ |
59 | |
60 | #include <lttv/filter.h> |
61 | |
62 | /* |
1a7fa682 |
63 | GQuark |
64 | LTTV_FILTER_TRACE, |
65 | LTTV_FILTER_TRACESET, |
66 | LTTV_FILTER_TRACEFILE, |
67 | LTTV_FILTER_STATE, |
91ad3f0a |
68 | LTTV_FILTER_EVENT, |
69 | LTTV_FILTER_NAME, |
70 | LTTV_FILTER_CATEGORY, |
71 | LTTV_FILTER_TIME, |
72 | LTTV_FILTER_TSC, |
73 | LTTV_FILTER_PID, |
74 | LTTV_FILTER_PPID, |
75 | LTTV_FILTER_C_TIME, |
76 | LTTV_FILTER_I_TIME, |
77 | LTTV_FILTER_P_NAME, |
78 | LTTV_FILTER_EX_MODE, |
79 | LTTV_FILTER_EX_SUBMODE, |
80 | LTTV_FILTER_P_STATUS, |
81 | LTTV_FILTER_CPU; |
150f0d33 |
82 | */ |
0cdc2470 |
83 | |
389ba50e |
84 | /** |
85 | * Constructor for LttvSimpleExpression |
86 | * @return pointer to new LttvSimpleExpression |
87 | */ |
2ea36caf |
88 | LttvSimpleExpression* |
0cdc2470 |
89 | lttv_simple_expression_new() { |
90 | |
389ba50e |
91 | LttvSimpleExpression* se = g_new(LttvSimpleExpression,1); |
92 | |
93 | se->field = LTTV_FILTER_UNDEFINED; |
bb87caa7 |
94 | se->op = NULL; |
389ba50e |
95 | se->offset = 0; |
96 | se->value = NULL; |
97 | |
98 | return se; |
0cdc2470 |
99 | } |
f4e9dd16 |
100 | /** |
150f0d33 |
101 | * add a node to the current tree |
bb87caa7 |
102 | * FIXME: Might be used to lower coding in lttv_filter_new switch expression |
150f0d33 |
103 | * @param stack the tree stack |
104 | * @param subtree the subtree if available (pointer or NULL) |
105 | * @param op the logical operator that will form the node |
f4e9dd16 |
106 | */ |
0cdc2470 |
107 | void |
5b729fcf |
108 | lttv_filter_tree_add_node(GPtrArray* stack, LttvFilterTree* subtree, LttvLogicalOp op) { |
0cdc2470 |
109 | |
5b729fcf |
110 | LttvFilterTree* t1 = NULL; |
111 | LttvFilterTree* t2 = NULL; |
0cdc2470 |
112 | |
5b729fcf |
113 | t1 = (LttvFilterTree*)g_ptr_array_index(stack,stack->len-1); |
0cdc2470 |
114 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
115 | t2 = lttv_filter_tree_new(); |
116 | t2->node = op; |
117 | if(subtree != NULL) { |
118 | t2->left = LTTV_TREE_NODE; |
119 | t2->l_child.t = subtree; |
120 | subtree = NULL; |
121 | t1->right = LTTV_TREE_NODE; |
122 | t1->r_child.t = t2; |
123 | } else { |
124 | // a_simple_expression->value = a_field_component->str; |
125 | // a_field_component = g_string_new(""); |
126 | t2->left = LTTV_TREE_LEAF; |
127 | // t2->l_child.leaf = a_simple_expression; |
128 | // a_simple_expression = g_new(lttv_simple_expression,1); |
129 | t1->right = LTTV_TREE_NODE; |
130 | t1->r_child.t = t2; |
131 | } |
132 | |
133 | } |
134 | |
0769c82f |
135 | /** |
136 | * Parse through filtering field hierarchy as specified |
137 | * by user. This function compares each value to |
138 | * predetermined quarks |
139 | * @param fp The field path list |
bb87caa7 |
140 | * @param se current simple expression |
0769c82f |
141 | * @return success/failure of operation |
142 | */ |
143 | gboolean |
47aa6e58 |
144 | parse_field_path(GPtrArray* fp, LttvSimpleExpression* se) { |
0769c82f |
145 | |
f4e9dd16 |
146 | GString* f = NULL; |
2b99ec10 |
147 | if(fp->len < 2) return FALSE; |
f4e9dd16 |
148 | g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data; |
47aa6e58 |
149 | |
150 | /* |
151 | * Parse through the specified |
152 | * hardcoded fields. |
153 | * |
154 | * Take note however that the |
155 | * 'event' subfields might change |
156 | * depending on values specified |
157 | * in core.xml file. Hence, if |
158 | * none of the subfields in the |
159 | * array match the hardcoded |
160 | * subfields, it will be considered |
161 | * as a dynamic field |
162 | */ |
163 | if(g_strcasecmp(f->str,"trace") ) { |
164 | /* |
165 | * Possible values: |
166 | * trace.name |
167 | */ |
168 | f=g_ptr_array_index(fp,1); |
389ba50e |
169 | if(g_strcasecmp(f->str,"name")) { |
170 | se->field = LTTV_FILTER_TRACE_NAME; |
171 | } |
47aa6e58 |
172 | else return FALSE; |
173 | } else if(g_strcasecmp(f->str,"traceset") ) { |
174 | /* |
175 | * FIXME: not yet implemented ! |
176 | */ |
177 | } else if(g_strcasecmp(f->str,"tracefile") ) { |
178 | /* |
179 | * Possible values: |
180 | * tracefile.name |
181 | */ |
182 | f=g_ptr_array_index(fp,1); |
389ba50e |
183 | if(g_strcasecmp(f->str,"name")) { |
184 | se->field = LTTV_FILTER_TRACEFILE_NAME; |
185 | } |
47aa6e58 |
186 | else return FALSE; |
187 | } else if(g_strcasecmp(f->str,"state") ) { |
188 | /* |
189 | * Possible values: |
190 | * state.pid |
191 | * state.ppid |
192 | * state.creation_time |
193 | * state.insertion_time |
194 | * state.process_name |
195 | * state.execution_mode |
196 | * state.execution_submode |
197 | * state.process_status |
198 | * state.cpu |
199 | */ |
200 | f=g_ptr_array_index(fp,1); |
389ba50e |
201 | if(g_strcasecmp(f->str,"pid") ) { |
202 | se->field = LTTV_FILTER_STATE_PID; |
203 | } |
204 | else if(g_strcasecmp(f->str,"ppid") ) { |
205 | se->field = LTTV_FILTER_STATE_PPID; |
206 | } |
207 | else if(g_strcasecmp(f->str,"creation_time") ) { |
208 | se->field = LTTV_FILTER_STATE_CT; |
209 | } |
210 | else if(g_strcasecmp(f->str,"insertion_time") ) { |
211 | se->field = LTTV_FILTER_STATE_IT; |
212 | } |
213 | else if(g_strcasecmp(f->str,"process_name") ) { |
214 | se->field = LTTV_FILTER_STATE_P_NAME; |
215 | } |
216 | else if(g_strcasecmp(f->str,"execution_mode") ) { |
217 | se->field = LTTV_FILTER_STATE_EX_MODE; |
218 | } |
219 | else if(g_strcasecmp(f->str,"execution_submode") ) { |
220 | se->field = LTTV_FILTER_STATE_EX_SUBMODE; |
221 | } |
222 | else if(g_strcasecmp(f->str,"process_status") ) { |
223 | se->field = LTTV_FILTER_STATE_P_STATUS; |
224 | } |
225 | else if(g_strcasecmp(f->str,"cpu") ) { |
226 | se->field = LTTV_FILTER_STATE_CPU; |
227 | } |
47aa6e58 |
228 | else return FALSE; |
229 | } else if(g_strcasecmp(f->str,"event") ) { |
389ba50e |
230 | /* |
231 | * Possible values: |
232 | * event.name |
233 | * event.category |
234 | * event.time |
235 | * event.tsc |
236 | */ |
2b99ec10 |
237 | f=g_ptr_array_index(fp,1); |
389ba50e |
238 | if(g_strcasecmp(f->str,"name") ) { |
239 | se->field = LTTV_FILTER_EVENT_NAME; |
240 | } |
241 | else if(g_strcasecmp(f->str,"category") ) { |
242 | /* |
243 | * FIXME: Category not yet functional in lttv |
244 | */ |
245 | se->field = LTTV_FILTER_EVENT_CATEGORY; |
246 | } |
47aa6e58 |
247 | else if(g_strcasecmp(f->str,"time") ) { |
389ba50e |
248 | se->field = LTTV_FILTER_EVENT_TIME; |
2b99ec10 |
249 | // offset = &((LttEvent*)NULL)->event_time); |
250 | } |
47aa6e58 |
251 | else if(g_strcasecmp(f->str,"tsc") ) { |
389ba50e |
252 | se->field = LTTV_FILTER_EVENT_TSC; |
2b99ec10 |
253 | // offset = &((LttEvent*)NULL)->event_cycle_count); |
254 | } |
255 | else { /* core.xml specified options */ |
389ba50e |
256 | se->field = LTTV_FILTER_EVENT_FIELD; |
257 | //se->offset = (...); |
2b99ec10 |
258 | } |
91ad3f0a |
259 | } else { |
260 | g_warning("Unrecognized field in filter string"); |
261 | return FALSE; |
0769c82f |
262 | } |
47aa6e58 |
263 | |
bb87caa7 |
264 | /* free the pointer array */ |
265 | g_ptr_array_free(fp,FALSE); |
266 | |
91ad3f0a |
267 | return TRUE; |
0769c82f |
268 | } |
269 | |
bb87caa7 |
270 | /** |
271 | * Sets the function pointer for the current |
272 | * Simple Expression |
273 | * @param se current simple expression |
274 | * @return success/failure of operation |
275 | */ |
276 | gboolean assign_operator(LttvSimpleExpression* se, LttvExpressionOp op) { |
aa4600f3 |
277 | |
278 | g_print("se->field = %i\n",se->field); |
279 | g_print("se->offset = %i\n",se->offset); |
280 | g_print("se->op = %p\n",se->op); |
281 | g_print("se->value = %s\n",se->value); |
282 | |
bb87caa7 |
283 | switch(se->field) { |
284 | /* char */ |
285 | case LTTV_FILTER_TRACE_NAME: |
286 | case LTTV_FILTER_TRACEFILE_NAME: |
287 | case LTTV_FILTER_STATE_P_NAME: |
288 | case LTTV_FILTER_EVENT_NAME: |
289 | switch(op) { |
290 | case LTTV_FIELD_EQ: |
291 | se->op = lttv_apply_op_eq_string; |
292 | break; |
293 | case LTTV_FIELD_NE: |
294 | se->op = lttv_apply_op_eq_string; |
295 | break; |
296 | default: |
297 | g_warning("Error encountered in operator assignment"); |
298 | return FALSE; |
299 | } |
300 | break; |
301 | case LTTV_FILTER_STATE_PID: |
302 | case LTTV_FILTER_STATE_PPID: |
303 | case LTTV_FILTER_STATE_EX_MODE: |
304 | case LTTV_FILTER_STATE_EX_SUBMODE: |
305 | case LTTV_FILTER_STATE_P_STATUS: |
306 | switch(op) { |
307 | case LTTV_FIELD_EQ: |
308 | se->op = lttv_apply_op_eq_uint64; |
309 | break; |
310 | case LTTV_FIELD_NE: |
311 | se->op = lttv_apply_op_ne_uint64; |
312 | break; |
313 | case LTTV_FIELD_LT: |
314 | se->op = lttv_apply_op_lt_uint64; |
315 | break; |
316 | case LTTV_FIELD_LE: |
317 | se->op = lttv_apply_op_le_uint64; |
318 | break; |
319 | case LTTV_FIELD_GT: |
320 | se->op = lttv_apply_op_gt_uint64; |
321 | break; |
322 | case LTTV_FIELD_GE: |
323 | se->op = lttv_apply_op_ge_uint64; |
324 | break; |
325 | default: |
326 | g_warning("Error encountered in operator assignment"); |
327 | return FALSE; |
328 | } |
329 | break; |
330 | case LTTV_FILTER_STATE_CT: |
331 | case LTTV_FILTER_STATE_IT: |
332 | case LTTV_FILTER_EVENT_TIME: |
333 | case LTTV_FILTER_EVENT_TSC: |
334 | switch(op) { |
335 | case LTTV_FIELD_EQ: |
336 | se->op = lttv_apply_op_eq_double; |
337 | break; |
338 | case LTTV_FIELD_NE: |
339 | se->op = lttv_apply_op_ne_double; |
340 | break; |
341 | case LTTV_FIELD_LT: |
342 | se->op = lttv_apply_op_lt_double; |
343 | break; |
344 | case LTTV_FIELD_LE: |
345 | se->op = lttv_apply_op_le_double; |
346 | break; |
347 | case LTTV_FIELD_GT: |
348 | se->op = lttv_apply_op_gt_double; |
349 | break; |
350 | case LTTV_FIELD_GE: |
351 | se->op = lttv_apply_op_ge_double; |
352 | break; |
353 | default: |
354 | g_warning("Error encountered in operator assignment"); |
355 | return FALSE; |
356 | } |
357 | break; |
358 | default: |
359 | g_warning("Error encountered in operator assignment"); |
360 | return FALSE; |
361 | } |
aa4600f3 |
362 | |
363 | return TRUE; |
364 | |
bb87caa7 |
365 | |
366 | } |
367 | |
31452f49 |
368 | /** |
84a333d6 |
369 | * Add an filtering option to the current tree |
370 | * @param expression Current expression to parse |
371 | * @return success/failure of operation |
372 | */ |
373 | gboolean |
374 | parse_simple_expression(GString* expression) { |
375 | |
376 | unsigned i; |
377 | |
a4c292d4 |
378 | |
0769c82f |
379 | |
a4c292d4 |
380 | |
5f185a2b |
381 | } |
382 | |
383 | /** |
384 | * Append a new expression to the expression |
385 | * defined in the current filter |
386 | * @param filter pointer to the current LttvFilter |
387 | * @param expression string that must be appended |
388 | */ |
389 | void lttv_filter_append_expression(LttvFilter* filter, char *expression) { |
390 | |
391 | if(expression == NULL) return; |
392 | if(filter == NULL) { |
393 | filter = lttv_filter_new(); |
394 | filter->expression = expression; |
395 | } else if(filter->expression == NULL) { |
396 | filter->expression = expression; |
397 | } else { |
398 | filter->expression = g_strconcat(filter->expression,"&",expression); |
399 | } |
400 | |
401 | lttv_filter_update(filter); |
402 | |
403 | } |
404 | |
8c89f5a8 |
405 | /** |
406 | * Clear the filter expression from the |
407 | * current filter and sets its pointer to NULL |
408 | * @param filter pointer to the current LttvFilter |
409 | */ |
5f185a2b |
410 | void lttv_filter_clear_expression(LttvFilter* filter) { |
411 | |
412 | if(filter->expression != NULL) { |
413 | g_free(filter->expression); |
414 | filter->expression = NULL; |
415 | } |
416 | |
84a333d6 |
417 | } |
418 | |
150f0d33 |
419 | /** |
420 | * Applies the 'equal' operator to the |
47aa6e58 |
421 | * specified structure and value |
422 | * @param v1 left member of comparison |
423 | * @param v2 right member of comparison |
150f0d33 |
424 | * @return success/failure of operation |
425 | */ |
83aa92fc |
426 | gboolean lttv_apply_op_eq_uint64(gpointer v1, char* v2) { |
427 | |
428 | guint64* r = (guint64*) v1; |
429 | guint64 l = atoi(v2); |
430 | return (*r == l); |
431 | |
432 | } |
150f0d33 |
433 | |
5b729fcf |
434 | /** |
435 | * Applies the 'equal' operator to the |
47aa6e58 |
436 | * specified structure and value |
437 | * @param v1 left member of comparison |
438 | * @param v2 right member of comparison |
5b729fcf |
439 | * @return success/failure of operation |
440 | */ |
83aa92fc |
441 | gboolean lttv_apply_op_eq_uint32(gpointer v1, char* v2) { |
442 | guint32* r = (guint32*) v1; |
443 | guint32 l = atoi(v2); |
444 | return (*r == l); |
445 | } |
5b729fcf |
446 | |
447 | /** |
448 | * Applies the 'equal' operator to the |
47aa6e58 |
449 | * specified structure and value |
450 | * @param v1 left member of comparison |
451 | * @param v2 right member of comparison |
5b729fcf |
452 | * @return success/failure of operation |
453 | */ |
83aa92fc |
454 | gboolean lttv_apply_op_eq_uint16(gpointer v1, char* v2) { |
455 | guint16* r = (guint16*) v1; |
456 | guint16 l = atoi(v2); |
457 | return (*r == l); |
458 | } |
5b729fcf |
459 | |
460 | /** |
461 | * Applies the 'equal' operator to the |
47aa6e58 |
462 | * specified structure and value |
463 | * @param v1 left member of comparison |
464 | * @param v2 right member of comparison |
5b729fcf |
465 | * @return success/failure of operation |
466 | */ |
83aa92fc |
467 | gboolean lttv_apply_op_eq_double(gpointer v1, char* v2) { |
468 | double* r = (double*) v1; |
469 | double l = atof(v2); |
470 | return (*r == l); |
471 | } |
5b729fcf |
472 | |
473 | /** |
474 | * Applies the 'equal' operator to the |
47aa6e58 |
475 | * specified structure and value |
476 | * @param v1 left member of comparison |
477 | * @param v2 right member of comparison |
5b729fcf |
478 | * @return success/failure of operation |
479 | */ |
83aa92fc |
480 | gboolean lttv_apply_op_eq_string(gpointer v1, char* v2) { |
481 | char* r = (char*) v1; |
482 | return (g_strcasecmp(r,v2)); |
483 | } |
150f0d33 |
484 | |
485 | /** |
486 | * Applies the 'not equal' operator to the |
47aa6e58 |
487 | * specified structure and value |
488 | * @param v1 left member of comparison |
489 | * @param v2 right member of comparison |
150f0d33 |
490 | * @return success/failure of operation |
491 | */ |
83aa92fc |
492 | gboolean lttv_apply_op_ne_uint64(gpointer v1, char* v2) { |
493 | guint64* r = (guint64*) v1; |
494 | guint64 l = atoi(v2); |
495 | return (*r != l); |
496 | } |
150f0d33 |
497 | |
5b729fcf |
498 | /** |
499 | * Applies the 'not equal' operator to the |
47aa6e58 |
500 | * specified structure and value |
501 | * @param v1 left member of comparison |
502 | * @param v2 right member of comparison |
5b729fcf |
503 | * @return success/failure of operation |
504 | */ |
83aa92fc |
505 | gboolean lttv_apply_op_ne_uint32(gpointer v1, char* v2) { |
506 | guint32* r = (guint32*) v1; |
507 | guint32 l = atoi(v2); |
508 | return (*r != l); |
509 | } |
5b729fcf |
510 | |
511 | /** |
512 | * Applies the 'not equal' operator to the |
47aa6e58 |
513 | * specified structure and value |
514 | * @param v1 left member of comparison |
515 | * @param v2 right member of comparison |
5b729fcf |
516 | * @return success/failure of operation |
517 | */ |
83aa92fc |
518 | gboolean lttv_apply_op_ne_uint16(gpointer v1, char* v2) { |
519 | guint16* r = (guint16*) v1; |
520 | guint16 l = atoi(v2); |
521 | return (*r != l); |
522 | } |
5b729fcf |
523 | |
524 | /** |
525 | * Applies the 'not equal' operator to the |
47aa6e58 |
526 | * specified structure and value |
527 | * @param v1 left member of comparison |
528 | * @param v2 right member of comparison |
5b729fcf |
529 | * @return success/failure of operation |
530 | */ |
83aa92fc |
531 | gboolean lttv_apply_op_ne_double(gpointer v1, char* v2) { |
532 | double* r = (double*) v1; |
533 | double l = atof(v2); |
534 | return (*r != l); |
535 | } |
5b729fcf |
536 | |
537 | /** |
538 | * Applies the 'not equal' operator to the |
47aa6e58 |
539 | * specified structure and value |
540 | * @param v1 left member of comparison |
541 | * @param v2 right member of comparison |
5b729fcf |
542 | * @return success/failure of operation |
543 | */ |
83aa92fc |
544 | gboolean lttv_apply_op_ne_string(gpointer v1, char* v2) { |
545 | char* r = (char*) v1; |
546 | return (!g_strcasecmp(r,v2)); |
547 | } |
150f0d33 |
548 | |
549 | /** |
550 | * Applies the 'lower than' operator to the |
47aa6e58 |
551 | * specified structure and value |
552 | * @param v1 left member of comparison |
553 | * @param v2 right member of comparison |
150f0d33 |
554 | * @return success/failure of operation |
555 | */ |
83aa92fc |
556 | gboolean lttv_apply_op_lt_uint64(gpointer v1, char* v2) { |
557 | guint64* r = (guint64*) v1; |
558 | guint64 l = atoi(v2); |
559 | return (*r < l); |
560 | } |
150f0d33 |
561 | |
5b729fcf |
562 | /** |
563 | * Applies the 'lower than' operator to the |
47aa6e58 |
564 | * specified structure and value |
565 | * @param v1 left member of comparison |
566 | * @param v2 right member of comparison |
5b729fcf |
567 | * @return success/failure of operation |
568 | */ |
83aa92fc |
569 | gboolean lttv_apply_op_lt_uint32(gpointer v1, char* v2) { |
570 | guint32* r = (guint32*) v1; |
571 | guint32 l = atoi(v2); |
572 | return (*r < l); |
573 | } |
5b729fcf |
574 | |
575 | /** |
576 | * Applies the 'lower than' operator to the |
47aa6e58 |
577 | * specified structure and value |
578 | * @param v1 left member of comparison |
579 | * @param v2 right member of comparison |
5b729fcf |
580 | * @return success/failure of operation |
581 | */ |
83aa92fc |
582 | gboolean lttv_apply_op_lt_uint16(gpointer v1, char* v2) { |
583 | guint16* r = (guint16*) v1; |
584 | guint16 l = atoi(v2); |
585 | return (*r < l); |
586 | } |
5b729fcf |
587 | |
588 | /** |
589 | * Applies the 'lower than' operator to the |
47aa6e58 |
590 | * specified structure and value |
591 | * @param v1 left member of comparison |
592 | * @param v2 right member of comparison |
5b729fcf |
593 | * @return success/failure of operation |
594 | */ |
83aa92fc |
595 | gboolean lttv_apply_op_lt_double(gpointer v1, char* v2) { |
596 | double* r = (double*) v1; |
597 | double l = atof(v2); |
598 | return (*r < l); |
599 | } |
5b729fcf |
600 | |
601 | /** |
602 | * Applies the 'lower than' operator to the |
47aa6e58 |
603 | * specified structure and value |
604 | * @param v1 left member of comparison |
605 | * @param v2 right member of comparison |
5b729fcf |
606 | * @return success/failure of operation |
607 | */ |
83aa92fc |
608 | gboolean lttv_apply_op_le_uint64(gpointer v1, char* v2) { |
609 | guint64* r = (guint64*) v1; |
610 | guint64 l = atoi(v2); |
611 | return (*r <= l); |
612 | } |
150f0d33 |
613 | |
614 | /** |
615 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
616 | * specified structure and value |
617 | * @param v1 left member of comparison |
618 | * @param v2 right member of comparison |
150f0d33 |
619 | * @return success/failure of operation |
620 | */ |
83aa92fc |
621 | gboolean lttv_apply_op_le_uint32(gpointer v1, char* v2) { |
622 | guint32* r = (guint32*) v1; |
623 | guint32 l = atoi(v2); |
624 | return (*r <= l); |
625 | } |
150f0d33 |
626 | |
5b729fcf |
627 | /** |
628 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
629 | * specified structure and value |
630 | * @param v1 left member of comparison |
631 | * @param v2 right member of comparison |
5b729fcf |
632 | * @return success/failure of operation |
633 | */ |
83aa92fc |
634 | gboolean lttv_apply_op_le_uint16(gpointer v1, char* v2) { |
635 | guint16* r = (guint16*) v1; |
636 | guint16 l = atoi(v2); |
637 | return (*r <= l); |
638 | } |
5b729fcf |
639 | |
640 | /** |
641 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
642 | * specified structure and value |
643 | * @param v1 left member of comparison |
644 | * @param v2 right member of comparison |
5b729fcf |
645 | * @return success/failure of operation |
646 | */ |
83aa92fc |
647 | gboolean lttv_apply_op_le_double(gpointer v1, char* v2) { |
648 | double* r = (double*) v1; |
649 | double l = atof(v2); |
650 | return (*r <= l); |
651 | } |
5b729fcf |
652 | |
653 | /** |
83aa92fc |
654 | * Applies the 'greater than' operator to the |
47aa6e58 |
655 | * specified structure and value |
656 | * @param v1 left member of comparison |
657 | * @param v2 right member of comparison |
5b729fcf |
658 | * @return success/failure of operation |
659 | */ |
83aa92fc |
660 | gboolean lttv_apply_op_gt_uint64(gpointer v1, char* v2) { |
661 | guint64* r = (guint64*) v1; |
662 | guint64 l = atoi(v2); |
663 | return (*r > l); |
664 | } |
150f0d33 |
665 | |
666 | /** |
667 | * Applies the 'greater than' operator to the |
47aa6e58 |
668 | * specified structure and value |
669 | * @param v1 left member of comparison |
670 | * @param v2 right member of comparison |
150f0d33 |
671 | * @return success/failure of operation |
672 | */ |
83aa92fc |
673 | gboolean lttv_apply_op_gt_uint32(gpointer v1, char* v2) { |
674 | guint32* r = (guint32*) v1; |
675 | guint32 l = atoi(v2); |
676 | return (*r > l); |
677 | } |
150f0d33 |
678 | |
5b729fcf |
679 | /** |
680 | * Applies the 'greater than' operator to the |
47aa6e58 |
681 | * specified structure and value |
682 | * @param v1 left member of comparison |
683 | * @param v2 right member of comparison |
5b729fcf |
684 | * @return success/failure of operation |
685 | */ |
83aa92fc |
686 | gboolean lttv_apply_op_gt_uint16(gpointer v1, char* v2) { |
687 | guint16* r = (guint16*) v1; |
688 | guint16 l = atoi(v2); |
689 | return (*r > l); |
690 | } |
5b729fcf |
691 | |
692 | /** |
693 | * Applies the 'greater than' operator to the |
47aa6e58 |
694 | * specified structure and value |
695 | * @param v1 left member of comparison |
696 | * @param v2 right member of comparison |
5b729fcf |
697 | * @return success/failure of operation |
698 | */ |
83aa92fc |
699 | gboolean lttv_apply_op_gt_double(gpointer v1, char* v2) { |
700 | double* r = (double*) v1; |
701 | double l = atof(v2); |
702 | return (*r > l); |
703 | } |
5b729fcf |
704 | |
705 | /** |
83aa92fc |
706 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
707 | * specified structure and value |
708 | * @param v1 left member of comparison |
709 | * @param v2 right member of comparison |
5b729fcf |
710 | * @return success/failure of operation |
711 | */ |
83aa92fc |
712 | gboolean lttv_apply_op_ge_uint64(gpointer v1, char* v2) { |
713 | guint64* r = (guint64*) v1; |
714 | guint64 l = atoi(v2); |
715 | return (*r >= l); |
716 | } |
150f0d33 |
717 | |
718 | /** |
719 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
720 | * specified structure and value |
721 | * @param v1 left member of comparison |
722 | * @param v2 right member of comparison |
150f0d33 |
723 | * @return success/failure of operation |
724 | */ |
83aa92fc |
725 | gboolean lttv_apply_op_ge_uint32(gpointer v1, char* v2) { |
726 | guint32* r = (guint32*) v1; |
727 | guint32 l = atoi(v2); |
728 | return (*r >= l); |
729 | } |
150f0d33 |
730 | |
5b729fcf |
731 | /** |
732 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
733 | * specified structure and value |
734 | * @param v1 left member of comparison |
735 | * @param v2 right member of comparison |
5b729fcf |
736 | * @return success/failure of operation |
737 | */ |
83aa92fc |
738 | gboolean lttv_apply_op_ge_uint16(gpointer v1, char* v2) { |
739 | guint16* r = (guint16*) v1; |
740 | guint16 l = atoi(v2); |
741 | return (*r >= l); |
742 | } |
150f0d33 |
743 | |
5b729fcf |
744 | /** |
745 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
746 | * specified structure and value |
747 | * @param v1 left member of comparison |
748 | * @param v2 right member of comparison |
5b729fcf |
749 | * @return success/failure of operation |
750 | */ |
83aa92fc |
751 | gboolean lttv_apply_op_ge_double(gpointer v1, char* v2) { |
752 | double* r = (double*) v1; |
753 | double l = atof(v2); |
754 | return (*r >= l); |
755 | } |
150f0d33 |
756 | |
757 | |
758 | /** |
759 | * Makes a copy of the current filter tree |
760 | * @param tree pointer to the current tree |
761 | * @return new copy of the filter tree |
762 | */ |
763 | LttvFilterTree* |
764 | lttv_filter_tree_clone(LttvFilterTree* tree) { |
765 | |
8c89f5a8 |
766 | LttvFilterTree* newtree = lttv_filter_tree_new(); |
150f0d33 |
767 | |
8c89f5a8 |
768 | newtree->node = tree->node; |
769 | |
770 | newtree->left = tree->left; |
771 | if(newtree->left == LTTV_TREE_NODE) { |
772 | newtree->l_child.t = lttv_filter_tree_clone(tree->l_child.t); |
773 | } else if(newtree->left == LTTV_TREE_LEAF) { |
774 | newtree->l_child.leaf = lttv_simple_expression_new(); |
775 | newtree->l_child.leaf->field = tree->l_child.leaf->field; |
776 | newtree->l_child.leaf->offset = tree->l_child.leaf->offset; |
777 | newtree->l_child.leaf->op = tree->l_child.leaf->op; |
778 | newtree->l_child.leaf->value = g_strconcat(tree->l_child.leaf->value); |
779 | } |
780 | |
781 | newtree->right = tree->right; |
782 | if(newtree->right == LTTV_TREE_NODE) { |
783 | newtree->r_child.t = lttv_filter_tree_clone(tree->r_child.t); |
784 | } else if(newtree->right == LTTV_TREE_LEAF) { |
785 | newtree->r_child.leaf = lttv_simple_expression_new(); |
786 | newtree->r_child.leaf->field = tree->r_child.leaf->field; |
787 | newtree->r_child.leaf->offset = tree->r_child.leaf->offset; |
788 | newtree->r_child.leaf->op = tree->r_child.leaf->op; |
789 | newtree->r_child.leaf->value = g_strconcat(tree->r_child.leaf->value); |
790 | } |
791 | |
792 | return newtree; |
793 | |
150f0d33 |
794 | } |
795 | |
796 | /** |
797 | * Makes a copy of the current filter |
798 | * @param filter pointer to the current filter |
799 | * @return new copy of the filter |
800 | */ |
801 | LttvFilter* |
802 | lttv_filter_clone(LttvFilter* filter) { |
803 | |
804 | |
805 | LttvFilter* newfilter = g_new(LttvFilter,1); |
806 | |
807 | // newfilter->expression = g_new(char,1) |
808 | strcpy(newfilter->expression,filter->expression); |
809 | |
810 | newfilter->head = lttv_filter_tree_clone(filter->head); |
811 | |
812 | return newfilter; |
813 | |
814 | } |
815 | |
816 | |
84a333d6 |
817 | /** |
818 | * Creates a new lttv_filter |
31452f49 |
819 | * @param expression filtering options string |
820 | * @param t pointer to the current LttvTrace |
84a333d6 |
821 | * @return the current lttv_filter or NULL if error |
31452f49 |
822 | */ |
2ea36caf |
823 | LttvFilter* |
5f185a2b |
824 | lttv_filter_new() { |
a4c292d4 |
825 | |
5f185a2b |
826 | LttvFilter* filter = g_new(LttvFilter,1); |
827 | filter->expression = NULL; |
828 | filter->head = NULL; |
829 | |
830 | } |
a4c292d4 |
831 | |
8c89f5a8 |
832 | /** |
833 | * Updates the current LttvFilter by building |
834 | * its tree based upon the expression string |
835 | * @param filter pointer to the current LttvFilter |
836 | * @return Failure/Success of operation |
837 | */ |
5f185a2b |
838 | gboolean |
839 | lttv_filter_update(LttvFilter* filter) { |
840 | |
841 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
842 | |
843 | if(filter->expression == NULL) return FALSE; |
844 | |
a4c292d4 |
845 | unsigned |
846 | i, |
91ad3f0a |
847 | p_nesting=0, /* parenthesis nesting value */ |
a4c292d4 |
848 | b=0; /* current breakpoint in expression string */ |
1601b365 |
849 | |
850 | /* trees */ |
5b729fcf |
851 | LttvFilterTree |
1601b365 |
852 | *tree = lttv_filter_tree_new(), /* main tree */ |
853 | *subtree = NULL, /* buffer for subtrees */ |
854 | *t1, /* buffer #1 */ |
855 | *t2; /* buffer #2 */ |
856 | |
5f185a2b |
857 | /* |
858 | * the filter |
859 | * If the tree already exists, |
860 | * destroy it and build a new one |
861 | */ |
862 | if(filter->head != NULL) lttv_filter_tree_destroy(filter->head); |
863 | filter->head = tree; |
864 | |
1601b365 |
865 | /* |
866 | * Tree Stack |
f4e9dd16 |
867 | * each element of the list |
868 | * is a sub tree created |
869 | * by the use of parenthesis in the |
870 | * global expression. The final tree |
1601b365 |
871 | * will be the one left at the root of |
f4e9dd16 |
872 | * the list |
873 | */ |
18d1226f |
874 | GPtrArray *tree_stack = g_ptr_array_new(); |
875 | g_ptr_array_add( tree_stack,(gpointer) tree ); |
f4e9dd16 |
876 | |
a4c292d4 |
877 | /* temporary values */ |
0769c82f |
878 | GString *a_field_component = g_string_new(""); |
f4e9dd16 |
879 | GPtrArray *a_field_path = NULL; |
880 | |
389ba50e |
881 | LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new(); |
0769c82f |
882 | |
a4c292d4 |
883 | /* |
884 | * Parse entire expression and construct |
885 | * the binary tree. There are two steps |
886 | * in browsing that string |
f4e9dd16 |
887 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
a4c292d4 |
888 | * 2. finding simple expressions |
0769c82f |
889 | * - field path ( separated by dots ) |
a4c292d4 |
890 | * - op ( >, <, =, >=, <=, !=) |
0769c82f |
891 | * - value ( integer, string ... ) |
892 | * To spare computing time, the whole |
893 | * string is parsed in this loop for a |
894 | * O(n) complexity order. |
1601b365 |
895 | * |
18d1226f |
896 | * When encountering logical op &,|,^ |
897 | * 1. parse the last value if any |
898 | * 2. create a new tree |
899 | * 3. add the expression (simple exp, or exp (subtree)) to the tree |
900 | * 4. concatenate this tree with the current tree on top of the stack |
901 | * When encountering math ops >,>=,<,<=,=,!= |
902 | * 1. add to op to the simple expression |
903 | * 2. concatenate last field component to field path |
904 | * When encountering concatening ops . |
905 | * 1. concatenate last field component to field path |
906 | * When encountering opening parenthesis (,{,[ |
907 | * 1. create a new subtree on top of tree stack |
908 | * When encountering closing parenthesis ),},] |
909 | * 1. add the expression on right child of the current tree |
910 | * 2. the subtree is completed, allocate a new subtree |
911 | * 3. pop the tree value from the tree stack |
912 | */ |
913 | |
f4e9dd16 |
914 | a_field_path = g_ptr_array_new(); |
915 | g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */ |
916 | |
18d1226f |
917 | |
5f185a2b |
918 | for(i=0;i<strlen(filter->expression);i++) { |
919 | // debug |
920 | g_print("%c ",filter->expression[i]); |
921 | switch(filter->expression[i]) { |
a4c292d4 |
922 | /* |
923 | * logical operators |
924 | */ |
925 | case '&': /* and */ |
aa4600f3 |
926 | |
5b729fcf |
927 | t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
928 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
18d1226f |
929 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
930 | t2->node = LTTV_LOGICAL_AND; |
bb87caa7 |
931 | if(subtree != NULL) { /* append subtree to current tree */ |
18d1226f |
932 | t2->left = LTTV_TREE_NODE; |
933 | t2->l_child.t = subtree; |
f4e9dd16 |
934 | subtree = NULL; |
18d1226f |
935 | t1->right = LTTV_TREE_NODE; |
410c83da |
936 | t1->r_child.t = t2; |
bb87caa7 |
937 | } else { /* append a simple expression */ |
0cdc2470 |
938 | a_simple_expression->value = a_field_component->str; |
18d1226f |
939 | a_field_component = g_string_new(""); |
940 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
941 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
942 | a_simple_expression = lttv_simple_expression_new(); |
18d1226f |
943 | t1->right = LTTV_TREE_NODE; |
410c83da |
944 | t1->r_child.t = t2; |
f4e9dd16 |
945 | } |
f4e9dd16 |
946 | break; |
aa4600f3 |
947 | |
a4c292d4 |
948 | case '|': /* or */ |
aa4600f3 |
949 | |
2ea36caf |
950 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
951 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
1601b365 |
952 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
953 | t2->node = LTTV_LOGICAL_OR; |
bb87caa7 |
954 | if(subtree != NULL) { /* append subtree to current tree */ |
1601b365 |
955 | t2->left = LTTV_TREE_NODE; |
956 | t2->l_child.t = subtree; |
957 | subtree = NULL; |
958 | t1->right = LTTV_TREE_NODE; |
959 | t1->r_child.t = t2; |
bb87caa7 |
960 | } else { /* append a simple expression */ |
0cdc2470 |
961 | a_simple_expression->value = a_field_component->str; |
1601b365 |
962 | a_field_component = g_string_new(""); |
963 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
964 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
965 | a_simple_expression = lttv_simple_expression_new(); |
1601b365 |
966 | t1->right = LTTV_TREE_NODE; |
967 | t1->r_child.t = t2; |
968 | } |
f4e9dd16 |
969 | break; |
aa4600f3 |
970 | |
a4c292d4 |
971 | case '^': /* xor */ |
aa4600f3 |
972 | |
2ea36caf |
973 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
974 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
1601b365 |
975 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
976 | t2->node = LTTV_LOGICAL_XOR; |
bb87caa7 |
977 | if(subtree != NULL) { /* append subtree to current tree */ |
1601b365 |
978 | t2->left = LTTV_TREE_NODE; |
979 | t2->l_child.t = subtree; |
980 | subtree = NULL; |
981 | t1->right = LTTV_TREE_NODE; |
982 | t1->r_child.t = t2; |
bb87caa7 |
983 | } else { /* append a simple expression */ |
0cdc2470 |
984 | a_simple_expression->value = a_field_component->str; |
1601b365 |
985 | a_field_component = g_string_new(""); |
986 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
987 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
988 | a_simple_expression = lttv_simple_expression_new(); |
1601b365 |
989 | t1->right = LTTV_TREE_NODE; |
990 | t1->r_child.t = t2; |
991 | } |
a4c292d4 |
992 | break; |
aa4600f3 |
993 | |
a4c292d4 |
994 | case '!': /* not, or not equal (math op) */ |
aa4600f3 |
995 | |
5f185a2b |
996 | if(filter->expression[i+1] == '=') { /* != */ |
0cdc2470 |
997 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
389ba50e |
998 | parse_field_path(a_field_path,a_simple_expression); |
0cdc2470 |
999 | a_field_component = g_string_new(""); |
aa4600f3 |
1000 | assign_operator(a_simple_expression,LTTV_FIELD_NE); |
1001 | i++; |
a4c292d4 |
1002 | } else { /* ! */ |
1601b365 |
1003 | // g_print("%s\n",a_field_component); |
1004 | // a_field_component = g_string_new(""); |
2ea36caf |
1005 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
1006 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
1601b365 |
1007 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
1008 | t2->node = LTTV_LOGICAL_NOT; |
1601b365 |
1009 | t1->right = LTTV_TREE_NODE; |
1010 | t1->r_child.t = t2; |
a4c292d4 |
1011 | } |
1012 | break; |
aa4600f3 |
1013 | |
a4c292d4 |
1014 | case '(': /* start of parenthesis */ |
91ad3f0a |
1015 | case '[': |
1016 | case '{': |
aa4600f3 |
1017 | |
91ad3f0a |
1018 | p_nesting++; /* incrementing parenthesis nesting value */ |
1601b365 |
1019 | t1 = lttv_filter_tree_new(); |
1020 | g_ptr_array_add( tree_stack,(gpointer) t1 ); |
a4c292d4 |
1021 | break; |
aa4600f3 |
1022 | |
a4c292d4 |
1023 | case ')': /* end of parenthesis */ |
91ad3f0a |
1024 | case ']': |
1025 | case '}': |
aa4600f3 |
1026 | |
91ad3f0a |
1027 | p_nesting--; /* decrementing parenthesis nesting value */ |
18d1226f |
1028 | if(p_nesting<0 || tree_stack->len<2) { |
f4e9dd16 |
1029 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
5f185a2b |
1030 | is not valid due to parenthesis incorrect use",filter->expression); |
1031 | return FALSE; |
f4e9dd16 |
1032 | } |
18d1226f |
1033 | |
1034 | g_assert(tree_stack->len>0); |
bb87caa7 |
1035 | if(subtree != NULL) { /* append subtree to current tree */ |
18d1226f |
1036 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
1037 | while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) { |
18d1226f |
1038 | g_assert(t1!=NULL && t1->r_child.t != NULL); |
1039 | t1 = t1->r_child.t; |
1040 | } |
1041 | t1->right = LTTV_TREE_NODE; |
1042 | t1->r_child.t = subtree; |
1043 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
1044 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
bb87caa7 |
1045 | } else { /* assign subtree as current tree */ |
0cdc2470 |
1046 | a_simple_expression->value = a_field_component->str; |
18d1226f |
1047 | a_field_component = g_string_new(""); |
1048 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
1049 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
18d1226f |
1050 | t1->right = LTTV_TREE_LEAF; |
0cdc2470 |
1051 | t1->r_child.leaf = a_simple_expression; |
389ba50e |
1052 | a_simple_expression = lttv_simple_expression_new(); |
18d1226f |
1053 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
1601b365 |
1054 | g_assert(subtree != NULL); |
18d1226f |
1055 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
1056 | } |
a4c292d4 |
1057 | break; |
1058 | |
1059 | /* |
1060 | * mathematic operators |
1061 | */ |
1062 | case '<': /* lower, lower or equal */ |
aa4600f3 |
1063 | |
1064 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
1065 | parse_field_path(a_field_path,a_simple_expression); |
1066 | a_field_component = g_string_new(""); |
5f185a2b |
1067 | if(filter->expression[i+1] == '=') { /* <= */ |
a4c292d4 |
1068 | i++; |
bb87caa7 |
1069 | assign_operator(a_simple_expression,LTTV_FIELD_LE); |
1070 | } else assign_operator(a_simple_expression,LTTV_FIELD_LT); |
aa4600f3 |
1071 | break; |
1072 | |
1073 | case '>': /* higher, higher or equal */ |
1074 | |
1075 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
389ba50e |
1076 | parse_field_path(a_field_path,a_simple_expression); |
f4e9dd16 |
1077 | a_field_component = g_string_new(""); |
5f185a2b |
1078 | if(filter->expression[i+1] == '=') { /* >= */ |
a4c292d4 |
1079 | i++; |
bb87caa7 |
1080 | assign_operator(a_simple_expression,LTTV_FIELD_GE); |
1081 | } else assign_operator(a_simple_expression,LTTV_FIELD_GT); |
aa4600f3 |
1082 | break; |
1083 | |
a4c292d4 |
1084 | case '=': /* equal */ |
aa4600f3 |
1085 | |
f4e9dd16 |
1086 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
389ba50e |
1087 | parse_field_path(a_field_path,a_simple_expression); |
f4e9dd16 |
1088 | a_field_component = g_string_new(""); |
aa4600f3 |
1089 | assign_operator(a_simple_expression,LTTV_FIELD_EQ); |
a4c292d4 |
1090 | break; |
aa4600f3 |
1091 | |
0769c82f |
1092 | /* |
1093 | * Field concatening caracter |
1094 | */ |
1095 | case '.': /* dot */ |
aa4600f3 |
1096 | |
bb87caa7 |
1097 | /* |
1098 | * divide field expression into elements |
1099 | * in a_field_path array. |
1100 | */ |
1101 | if(a_simple_expression->op != NULL) { |
1102 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
1103 | a_field_component = g_string_new(""); |
1104 | } |
0769c82f |
1105 | break; |
aa4600f3 |
1106 | |
a4c292d4 |
1107 | default: /* concatening current string */ |
5f185a2b |
1108 | g_string_append_c(a_field_component,filter->expression[i]); |
a4c292d4 |
1109 | } |
1110 | } |
1601b365 |
1111 | |
1112 | g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2); |
0cdc2470 |
1113 | g_print("stack size: %i\n",tree_stack->len); |
1114 | |
1115 | /* |
1116 | * Preliminary check to see |
1117 | * if tree was constructed correctly |
1118 | */ |
1119 | if( p_nesting>0 ) { |
1120 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
5f185a2b |
1121 | is not valid due to parenthesis incorrect use",filter->expression); |
1122 | return FALSE; |
0cdc2470 |
1123 | } |
1124 | |
1125 | if(tree_stack->len != 1) /* only root tree should remain */ |
5f185a2b |
1126 | return FALSE; |
1601b365 |
1127 | |
410c83da |
1128 | /* processing last element of expression */ |
410c83da |
1129 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
2a734d8e |
1130 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
410c83da |
1131 | if(subtree != NULL) { /* add the subtree */ |
1132 | t1->right = LTTV_TREE_NODE; |
0cdc2470 |
1133 | t1->r_child.t = subtree; |
410c83da |
1134 | subtree = NULL; |
1135 | } else { /* add a leaf */ |
0cdc2470 |
1136 | a_simple_expression->value = a_field_component->str; |
410c83da |
1137 | a_field_component = g_string_new(""); |
1138 | t1->right = LTTV_TREE_LEAF; |
0cdc2470 |
1139 | t1->r_child.leaf = a_simple_expression; |
2ea36caf |
1140 | /* |
1141 | * FIXME: is it really necessary to reallocate |
1142 | * LttvSimpleExpression at this point ?? |
1143 | */ |
389ba50e |
1144 | a_simple_expression = lttv_simple_expression_new(); |
410c83da |
1145 | } |
1146 | |
1147 | g_assert(tree != NULL); |
1148 | g_assert(subtree == NULL); |
a4c292d4 |
1149 | |
aa4600f3 |
1150 | lttv_filter_tracefile(filter, NULL) ; |
1151 | |
5f185a2b |
1152 | return TRUE; |
1153 | // return filter; |
410c83da |
1154 | |
31452f49 |
1155 | } |
1156 | |
8c89f5a8 |
1157 | /** |
1158 | * Destroy the current LttvFilter |
1159 | * @param filter pointer to the current LttvFilter |
1160 | */ |
1da1525d |
1161 | void |
2ea36caf |
1162 | lttv_filter_destroy(LttvFilter* filter) { |
5f185a2b |
1163 | |
1164 | g_free(filter->expression); |
1165 | lttv_filter_tree_destroy(filter->head); |
1166 | g_free(filter); |
1167 | |
1da1525d |
1168 | } |
1169 | |
150f0d33 |
1170 | /** |
1171 | * Assign a new tree for the current expression |
1172 | * or sub expression |
1173 | * @return pointer of LttvFilterTree |
1174 | */ |
5f185a2b |
1175 | LttvFilterTree* |
1176 | lttv_filter_tree_new() { |
150f0d33 |
1177 | LttvFilterTree* tree; |
1178 | |
1179 | tree = g_new(LttvFilter,1); |
1180 | tree->node = 0; //g_new(lttv_expression,1); |
150f0d33 |
1181 | tree->left = LTTV_TREE_IDLE; |
1182 | tree->right = LTTV_TREE_IDLE; |
1183 | |
1184 | return tree; |
1185 | } |
1186 | |
1187 | /** |
1188 | * Destroys the tree and his sub-trees |
1189 | * @param tree Tree which must be destroyed |
1190 | */ |
5f185a2b |
1191 | void |
1192 | lttv_filter_tree_destroy(LttvFilterTree* tree) { |
150f0d33 |
1193 | |
1194 | if(tree == NULL) return; |
1195 | |
1196 | if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf); |
1197 | else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t); |
1198 | |
1199 | if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf); |
1200 | else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t); |
1201 | |
1202 | g_free(tree->node); |
1203 | g_free(tree); |
1204 | } |
1205 | |
1206 | |
84a333d6 |
1207 | /** |
1208 | * Apply the filter to a specific trace |
1209 | * @param filter the current filter applied |
1210 | * @param tracefile the trace to apply the filter to |
1211 | * @return success/failure of operation |
1212 | */ |
31452f49 |
1213 | gboolean |
2ea36caf |
1214 | lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) { |
0769c82f |
1215 | |
5b729fcf |
1216 | LttvFilterTree* t = filter->head; |
1217 | |
1601b365 |
1218 | /* |
1219 | * Each tree is parsed in inorder. |
1220 | * This way, it's possible to apply the left filter of the |
1221 | * tree, then decide whether or not the right branch should |
1222 | * be parsed depending on the linking logical operator |
1223 | * |
1224 | * As for the filtering structure, since we are trying |
1225 | * to remove elements from the trace, it might be better |
1226 | * managing an array of all items to be removed .. |
1227 | */ |
0769c82f |
1228 | |
5f185a2b |
1229 | ///////////////////////////////////////////////////////////////////////////// |
1230 | // TEST // |
1231 | ///////////////////////////////////////////////////////////////////////////// |
5b729fcf |
1232 | g_print("node:%p lchild:%p rchild:%p\n",t,t->l_child.t,t->r_child.t); |
1233 | g_print("node type%i\n",t->node); |
1234 | if(t->left == LTTV_TREE_NODE) lttv_filter_tracefile(t->l_child.t,NULL); |
1235 | else if(t->left == LTTV_TREE_LEAF) { |
1236 | g_assert(t->l_child.leaf->value != NULL); |
1237 | g_print("%p: left is qqch %i %s\n",t,t->l_child.leaf->op,t->l_child.leaf->value); |
0cdc2470 |
1238 | } |
5b729fcf |
1239 | if(t->right == LTTV_TREE_NODE) lttv_filter_tracefile(t->r_child.t,NULL); |
1240 | else if(t->right == LTTV_TREE_LEAF) { |
1241 | g_assert(t->r_child.leaf->value != NULL); |
1242 | g_print("%p: right is qqch %i %s\n",t,t->r_child.leaf->op,t->r_child.leaf->value); |
0cdc2470 |
1243 | } |
5f185a2b |
1244 | ///////////////////////////////////////////////////////////////////////////// |
1245 | |
1246 | |
1247 | |
0769c82f |
1248 | |
1249 | /* test */ |
1250 | /* int i, nb; |
1251 | char *f_name, *e_name; |
31452f49 |
1252 | |
0769c82f |
1253 | char* field = "cpu"; |
1254 | |
1255 | LttvTraceHook h; |
1256 | |
1257 | LttEventType *et; |
1258 | |
1259 | LttType *t; |
1260 | |
1261 | GString *fe_name = g_string_new(""); |
1262 | |
1263 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
1264 | g_print("NB:%i\n",nb); |
1265 | for(i = 0 ; i < nb ; i++) { |
1266 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
1267 | e_name = ltt_eventtype_name(et); |
1268 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
1269 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
1270 | g_print("facility:%s and event:%s\n",f_name,e_name); |
1271 | } |
1272 | */ |
31452f49 |
1273 | } |
1274 | |
1a7fa682 |
1275 | gboolean |
2ea36caf |
1276 | lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) { |
341aa948 |
1277 | |
1278 | } |
1a7fa682 |
1279 | |
84a333d6 |
1280 | /** |
1281 | * Apply the filter to a specific event |
1282 | * @param filter the current filter applied |
1283 | * @param event the event to apply the filter to |
1284 | * @return success/failure of operation |
1285 | */ |
31452f49 |
1286 | gboolean |
2ea36caf |
1287 | lttv_filter_event(LttvFilter *filter, LttEvent *event) { |
31452f49 |
1288 | |
1289 | } |
1a7fa682 |
1290 | |
91ad3f0a |
1291 | /** |
1292 | * Initializes the filter module and specific values |
1293 | */ |
1a7fa682 |
1294 | static void module_init() |
1295 | { |
91ad3f0a |
1296 | |
1297 | /* |
1298 | * Quarks initialization |
1299 | * for hardcoded filtering options |
1300 | * |
1301 | * TODO: traceset has no yet been defined |
1302 | */ |
1303 | |
1304 | /* top fields */ |
5b729fcf |
1305 | // LTTV_FILTER_EVENT = g_quark_from_string("event"); |
1306 | // LTTV_FILTER_TRACE = g_quark_from_string("trace"); |
1307 | // LTTV_FILTER_TRACESET = g_quark_from_string("traceset"); |
1308 | // LTTV_FILTER_STATE = g_quark_from_string("state"); |
1309 | // LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile"); |
1a7fa682 |
1310 | |
91ad3f0a |
1311 | /* event.name, tracefile.name, trace.name */ |
5b729fcf |
1312 | // LTTV_FILTER_NAME = g_quark_from_string("name"); |
91ad3f0a |
1313 | |
1314 | /* event sub fields */ |
5b729fcf |
1315 | // LTTV_FILTER_CATEGORY = g_quark_from_string("category"); |
1316 | // LTTV_FILTER_TIME = g_quark_from_string("time"); |
1317 | // LTTV_FILTER_TSC = g_quark_from_string("tsc"); |
91ad3f0a |
1318 | |
1319 | /* state sub fields */ |
5b729fcf |
1320 | // LTTV_FILTER_PID = g_quark_from_string("pid"); |
1321 | // LTTV_FILTER_PPID = g_quark_from_string("ppid"); |
1322 | // LTTV_FILTER_C_TIME = g_quark_from_string("creation_time"); |
1323 | // LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time"); |
1324 | // LTTV_FILTER_P_NAME = g_quark_from_string("process_name"); |
1325 | // LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode"); |
1326 | // LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode"); |
1327 | // LTTV_FILTER_P_STATUS = g_quark_from_string("process_status"); |
1328 | // LTTV_FILTER_CPU = g_quark_from_string("cpu"); |
91ad3f0a |
1329 | |
1a7fa682 |
1330 | } |
1331 | |
91ad3f0a |
1332 | /** |
1333 | * Destroys the filter module and specific values |
1334 | */ |
1a7fa682 |
1335 | static void module_destroy() |
1336 | { |
1337 | } |
1338 | |
1339 | |
91ad3f0a |
1340 | LTTV_MODULE("filter", "Filters traceset and events", \ |
1341 | "Filters traceset and events specifically to user input", \ |
1a7fa682 |
1342 | module_init, module_destroy) |
1343 | |
1344 | |
1345 | |