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