9c312311 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
852f16bb |
2 | * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa |
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 | |
7e7af7f2 |
19 | /*! \file lttv/lttv/filter.c |
20 | * \brief Defines the core filter of application |
21 | * |
22 | * consist in AND, OR and NOT nested expressions, forming a tree with |
23 | * simple relations as leaves. The simple relations test if a field |
24 | * in an event is equal, not equal, smaller, smaller or equal, larger, or |
25 | * larger or equal to a specified value. |
26 | * |
27 | * Fields specified in a simple expression can take following |
28 | * values |
29 | * |
da2e1bfb |
30 | * \verbatim |
31 | * LttvTracefileContext{} |
7e7af7f2 |
32 | * |->event\ |
6471e71f |
33 | * | |->name (String, converted to GQuark) (channel.event) |
34 | * | |->subname (String, converted to GQuark) |
7e7af7f2 |
35 | * | |->category (String, not yet implemented) |
36 | * | |->time (LttTime) |
bbd9d557 |
37 | * | |->tsc (LttCycleCount --> uint64) |
33bdc8dd |
38 | * | |->target_pid (target PID of the event) |
7e7af7f2 |
39 | * | |->fields |
6471e71f |
40 | * | |->"channel name" |
c56a714e |
41 | * | |->"event name" |
42 | * | |->"field name" |
43 | * | |->"sub-field name" |
44 | * | |->... |
45 | * | |->"leaf-field name" (field type) |
6471e71f |
46 | * |->channel (or tracefile) |
7e7af7f2 |
47 | * | |->name (String, converted to GQuark) |
48 | * |->trace |
49 | * | |->name (String, converted to GQuark) |
50 | * |->state |
2ec11087 |
51 | * |->pid (guint) |
52 | * |->ppid (guint) |
7e7af7f2 |
53 | * |->creation_time (LttTime) |
54 | * |->insertion_time (LttTime) |
55 | * |->process_name (String, converted to GQuark) |
7b5f6cf1 |
56 | * |->thread_brand (String, converted to GQuark) |
7e7af7f2 |
57 | * |->execution_mode (LttvExecutionMode) |
58 | * |->execution_submode (LttvExecutionSubmode) |
59 | * |->process_status (LttvProcessStatus) |
6e0d58d6 |
60 | * |->cpu (guint) |
da2e1bfb |
61 | * \endverbatim |
150f0d33 |
62 | */ |
63 | |
64 | /* |
bbd9d557 |
65 | * \todo |
150f0d33 |
66 | * - refine switch of expression in multiple uses functions |
be66ef34 |
67 | * - remove the idle expressions in the tree |
150f0d33 |
68 | */ |
69 | |
4e4d11b3 |
70 | #ifdef HAVE_CONFIG_H |
71 | #include <config.h> |
72 | #endif |
73 | |
72911c5d |
74 | //#define TEST |
be66ef34 |
75 | #ifdef TEST |
76 | #include <time.h> |
77 | #include <sys/time.h> |
78 | #endif |
79 | |
87834cd1 |
80 | #include <lttv/lttv.h> |
150f0d33 |
81 | #include <lttv/filter.h> |
327a4314 |
82 | #include <ltt/trace.h> |
83 | #include <stdlib.h> |
84 | #include <string.h> |
80f9611a |
85 | |
86 | /** |
56e29124 |
87 | * @fn LttvSimpleExpression* lttv_simple_expression_new() |
88 | * |
80f9611a |
89 | * Constructor for LttvSimpleExpression |
90 | * @return pointer to new LttvSimpleExpression |
91 | */ |
92 | LttvSimpleExpression* |
93 | lttv_simple_expression_new() { |
94 | |
95 | LttvSimpleExpression* se = g_new(LttvSimpleExpression,1); |
96 | |
97 | se->field = LTTV_FILTER_UNDEFINED; |
98 | se->op = NULL; |
99 | se->offset = 0; |
80f9611a |
100 | |
101 | return se; |
102 | } |
103 | |
9354c7da |
104 | /* |
105 | * Keeps the array order. |
106 | */ |
107 | static inline gpointer ltt_g_ptr_array_remove_index_slow(GPtrArray *fp, |
108 | int index) |
109 | { |
110 | gpointer ptr; |
111 | int i; |
112 | |
113 | if (fp->len == 0) |
114 | return NULL; |
115 | |
116 | ptr = g_ptr_array_index(fp, index); |
117 | for (i = index; i < fp->len - 1; i++) { |
118 | g_ptr_array_index(fp, i) = g_ptr_array_index(fp, i + 1); |
119 | } |
120 | g_ptr_array_remove_index(fp, fp->len - 1); |
121 | return ptr; |
122 | } |
123 | |
0769c82f |
124 | /** |
7e7af7f2 |
125 | * @fn gboolean lttv_simple_expression_assign_field(GPtrArray*,LttvSimpleExpression*) |
56e29124 |
126 | * |
0769c82f |
127 | * Parse through filtering field hierarchy as specified |
128 | * by user. This function compares each value to |
129 | * predetermined quarks |
130 | * @param fp The field path list |
bb87caa7 |
131 | * @param se current simple expression |
0769c82f |
132 | * @return success/failure of operation |
133 | */ |
134 | gboolean |
9ab5ebd7 |
135 | lttv_simple_expression_assign_field(GPtrArray* fp, LttvSimpleExpression* se) { |
0769c82f |
136 | |
f4e9dd16 |
137 | GString* f = NULL; |
6668c31b |
138 | |
2b99ec10 |
139 | if(fp->len < 2) return FALSE; |
9354c7da |
140 | g_assert((f=ltt_g_ptr_array_remove_index_slow(fp,0))); |
141 | |
142 | |
47aa6e58 |
143 | /* |
144 | * Parse through the specified |
145 | * hardcoded fields. |
146 | * |
147 | * Take note however that the |
148 | * 'event' subfields might change |
149 | * depending on values specified |
150 | * in core.xml file. Hence, if |
151 | * none of the subfields in the |
152 | * array match the hardcoded |
153 | * subfields, it will be considered |
154 | * as a dynamic field |
155 | */ |
6668c31b |
156 | |
80f9611a |
157 | if(!g_strcasecmp(f->str,"trace") ) { |
47aa6e58 |
158 | /* |
159 | * Possible values: |
160 | * trace.name |
161 | */ |
73050a5f |
162 | g_string_free(f,TRUE); |
9354c7da |
163 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
80f9611a |
164 | if(!g_strcasecmp(f->str,"name")) { |
389ba50e |
165 | se->field = LTTV_FILTER_TRACE_NAME; |
166 | } |
80f9611a |
167 | } else if(!g_strcasecmp(f->str,"traceset") ) { |
47aa6e58 |
168 | /* |
169 | * FIXME: not yet implemented ! |
170 | */ |
6471e71f |
171 | } else if(!g_strcasecmp(f->str,"tracefile") |
172 | || !g_strcasecmp(f->str,"channel") ) { |
47aa6e58 |
173 | /* |
174 | * Possible values: |
175 | * tracefile.name |
6471e71f |
176 | * channel.name |
47aa6e58 |
177 | */ |
73050a5f |
178 | g_string_free(f,TRUE); |
9354c7da |
179 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
80f9611a |
180 | if(!g_strcasecmp(f->str,"name")) { |
389ba50e |
181 | se->field = LTTV_FILTER_TRACEFILE_NAME; |
182 | } |
80f9611a |
183 | } else if(!g_strcasecmp(f->str,"state") ) { |
47aa6e58 |
184 | /* |
185 | * Possible values: |
186 | * state.pid |
187 | * state.ppid |
188 | * state.creation_time |
189 | * state.insertion_time |
190 | * state.process_name |
7b5f6cf1 |
191 | * state.thread_brand |
47aa6e58 |
192 | * state.execution_mode |
193 | * state.execution_submode |
194 | * state.process_status |
195 | * state.cpu |
196 | */ |
73050a5f |
197 | g_string_free(f,TRUE); |
9354c7da |
198 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
80f9611a |
199 | if(!g_strcasecmp(f->str,"pid") ) { |
389ba50e |
200 | se->field = LTTV_FILTER_STATE_PID; |
201 | } |
80f9611a |
202 | else if(!g_strcasecmp(f->str,"ppid") ) { |
389ba50e |
203 | se->field = LTTV_FILTER_STATE_PPID; |
204 | } |
80f9611a |
205 | else if(!g_strcasecmp(f->str,"creation_time") ) { |
389ba50e |
206 | se->field = LTTV_FILTER_STATE_CT; |
207 | } |
80f9611a |
208 | else if(!g_strcasecmp(f->str,"insertion_time") ) { |
389ba50e |
209 | se->field = LTTV_FILTER_STATE_IT; |
210 | } |
80f9611a |
211 | else if(!g_strcasecmp(f->str,"process_name") ) { |
389ba50e |
212 | se->field = LTTV_FILTER_STATE_P_NAME; |
213 | } |
7b5f6cf1 |
214 | else if(!g_strcasecmp(f->str,"thread_brand") ) { |
215 | se->field = LTTV_FILTER_STATE_T_BRAND; |
216 | } |
80f9611a |
217 | else if(!g_strcasecmp(f->str,"execution_mode") ) { |
389ba50e |
218 | se->field = LTTV_FILTER_STATE_EX_MODE; |
219 | } |
80f9611a |
220 | else if(!g_strcasecmp(f->str,"execution_submode") ) { |
389ba50e |
221 | se->field = LTTV_FILTER_STATE_EX_SUBMODE; |
222 | } |
80f9611a |
223 | else if(!g_strcasecmp(f->str,"process_status") ) { |
389ba50e |
224 | se->field = LTTV_FILTER_STATE_P_STATUS; |
225 | } |
80f9611a |
226 | else if(!g_strcasecmp(f->str,"cpu") ) { |
389ba50e |
227 | se->field = LTTV_FILTER_STATE_CPU; |
228 | } |
80f9611a |
229 | } else if(!g_strcasecmp(f->str,"event") ) { |
389ba50e |
230 | /* |
231 | * Possible values: |
232 | * event.name |
6471e71f |
233 | * event.channel |
389ba50e |
234 | * event.category |
235 | * event.time |
236 | * event.tsc |
33bdc8dd |
237 | * event.target_pid |
c56a714e |
238 | * event.field |
389ba50e |
239 | */ |
73050a5f |
240 | g_string_free(f,TRUE); |
9354c7da |
241 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
6668c31b |
242 | |
80f9611a |
243 | if(!g_strcasecmp(f->str,"name") ) { |
389ba50e |
244 | se->field = LTTV_FILTER_EVENT_NAME; |
245 | } |
6471e71f |
246 | else if(!g_strcasecmp(f->str,"subname") ) { |
247 | se->field = LTTV_FILTER_EVENT_SUBNAME; |
248 | } |
80f9611a |
249 | else if(!g_strcasecmp(f->str,"category") ) { |
389ba50e |
250 | /* |
251 | * FIXME: Category not yet functional in lttv |
252 | */ |
253 | se->field = LTTV_FILTER_EVENT_CATEGORY; |
254 | } |
80f9611a |
255 | else if(!g_strcasecmp(f->str,"time") ) { |
389ba50e |
256 | se->field = LTTV_FILTER_EVENT_TIME; |
2b99ec10 |
257 | } |
80f9611a |
258 | else if(!g_strcasecmp(f->str,"tsc") ) { |
389ba50e |
259 | se->field = LTTV_FILTER_EVENT_TSC; |
2b99ec10 |
260 | } |
33bdc8dd |
261 | else if(!g_strcasecmp(f->str,"target_pid") ) { |
262 | se->field = LTTV_FILTER_EVENT_TARGET_PID; |
263 | } |
c56a714e |
264 | else if(!g_strcasecmp(f->str,"field") ) { |
389ba50e |
265 | se->field = LTTV_FILTER_EVENT_FIELD; |
c8f08e26 |
266 | g_string_free(f,TRUE); |
9354c7da |
267 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
c56a714e |
268 | |
269 | } else { |
9354c7da |
270 | //g_string_free(f,TRUE); |
271 | //f=ltt_g_ptr_array_remove_index_slow(fp,0); |
c56a714e |
272 | g_warning("Unknown event filter subtype %s", f->str); |
2b99ec10 |
273 | } |
91ad3f0a |
274 | } else { |
fb04197e |
275 | g_string_free(f,TRUE); |
9354c7da |
276 | f=ltt_g_ptr_array_remove_index_slow(fp,0); |
fb04197e |
277 | |
91ad3f0a |
278 | g_warning("Unrecognized field in filter string"); |
0769c82f |
279 | } |
47aa6e58 |
280 | |
56e29124 |
281 | /* free memory for last string */ |
73050a5f |
282 | g_string_free(f,TRUE); |
56e29124 |
283 | |
284 | /* array should be empty */ |
73050a5f |
285 | g_assert(fp->len == 0); |
56e29124 |
286 | |
56e29124 |
287 | if(se->field == LTTV_FILTER_UNDEFINED) { |
288 | g_warning("The specified field was not recognized !"); |
289 | return FALSE; |
290 | } |
91ad3f0a |
291 | return TRUE; |
0769c82f |
292 | } |
293 | |
bb87caa7 |
294 | /** |
56e29124 |
295 | * @fn gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression*,LttvExpressionOp) |
296 | * |
bb87caa7 |
297 | * Sets the function pointer for the current |
298 | * Simple Expression |
299 | * @param se current simple expression |
7e7af7f2 |
300 | * @param op current operator |
bb87caa7 |
301 | * @return success/failure of operation |
302 | */ |
be66ef34 |
303 | gboolean |
304 | lttv_simple_expression_assign_operator(LttvSimpleExpression* se, LttvExpressionOp op) { |
aa4600f3 |
305 | |
bb87caa7 |
306 | switch(se->field) { |
56e29124 |
307 | /* |
308 | * string |
309 | */ |
bb87caa7 |
310 | case LTTV_FILTER_TRACE_NAME: |
311 | case LTTV_FILTER_TRACEFILE_NAME: |
312 | case LTTV_FILTER_STATE_P_NAME: |
7b5f6cf1 |
313 | case LTTV_FILTER_STATE_T_BRAND: |
6471e71f |
314 | case LTTV_FILTER_EVENT_SUBNAME: |
be66ef34 |
315 | case LTTV_FILTER_STATE_EX_MODE: |
316 | case LTTV_FILTER_STATE_EX_SUBMODE: |
317 | case LTTV_FILTER_STATE_P_STATUS: |
bb87caa7 |
318 | switch(op) { |
319 | case LTTV_FIELD_EQ: |
c6832b57 |
320 | se->op = lttv_apply_op_eq_quark; |
bb87caa7 |
321 | break; |
322 | case LTTV_FIELD_NE: |
c6832b57 |
323 | se->op = lttv_apply_op_ne_quark; |
bb87caa7 |
324 | break; |
325 | default: |
73050a5f |
326 | g_warning("Error encountered in operator assignment = or != expected"); |
bb87caa7 |
327 | return FALSE; |
328 | } |
329 | break; |
6471e71f |
330 | /* |
331 | * two strings. |
332 | */ |
333 | case LTTV_FILTER_EVENT_NAME: |
334 | switch(op) { |
335 | case LTTV_FIELD_EQ: |
336 | se->op = lttv_apply_op_eq_quarks; |
337 | break; |
338 | case LTTV_FIELD_NE: |
339 | se->op = lttv_apply_op_ne_quarks; |
340 | break; |
341 | default: |
342 | g_warning("Error encountered in operator assignment = or != expected"); |
343 | return FALSE; |
344 | } |
56e29124 |
345 | /* |
346 | * integer |
347 | */ |
bbd9d557 |
348 | case LTTV_FILTER_EVENT_TSC: |
bb87caa7 |
349 | switch(op) { |
350 | case LTTV_FIELD_EQ: |
351 | se->op = lttv_apply_op_eq_uint64; |
352 | break; |
353 | case LTTV_FIELD_NE: |
354 | se->op = lttv_apply_op_ne_uint64; |
355 | break; |
356 | case LTTV_FIELD_LT: |
357 | se->op = lttv_apply_op_lt_uint64; |
358 | break; |
359 | case LTTV_FIELD_LE: |
360 | se->op = lttv_apply_op_le_uint64; |
361 | break; |
362 | case LTTV_FIELD_GT: |
363 | se->op = lttv_apply_op_gt_uint64; |
364 | break; |
365 | case LTTV_FIELD_GE: |
366 | se->op = lttv_apply_op_ge_uint64; |
367 | break; |
368 | default: |
369 | g_warning("Error encountered in operator assignment"); |
370 | return FALSE; |
371 | } |
372 | break; |
6e0d58d6 |
373 | /* |
2ec11087 |
374 | * unsigned integers |
6e0d58d6 |
375 | */ |
376 | case LTTV_FILTER_STATE_CPU: |
377 | case LTTV_FILTER_STATE_PID: |
378 | case LTTV_FILTER_STATE_PPID: |
33bdc8dd |
379 | case LTTV_FILTER_EVENT_TARGET_PID: |
6e0d58d6 |
380 | switch(op) { |
381 | case LTTV_FIELD_EQ: |
2ec11087 |
382 | se->op = lttv_apply_op_eq_uint; |
6e0d58d6 |
383 | break; |
384 | case LTTV_FIELD_NE: |
2ec11087 |
385 | se->op = lttv_apply_op_ne_uint; |
6e0d58d6 |
386 | break; |
387 | case LTTV_FIELD_LT: |
2ec11087 |
388 | se->op = lttv_apply_op_lt_uint; |
6e0d58d6 |
389 | break; |
390 | case LTTV_FIELD_LE: |
2ec11087 |
391 | se->op = lttv_apply_op_le_uint; |
6e0d58d6 |
392 | break; |
393 | case LTTV_FIELD_GT: |
2ec11087 |
394 | se->op = lttv_apply_op_gt_uint; |
6e0d58d6 |
395 | break; |
396 | case LTTV_FIELD_GE: |
2ec11087 |
397 | se->op = lttv_apply_op_ge_uint; |
6e0d58d6 |
398 | break; |
399 | default: |
400 | g_warning("Error encountered in operator assignment"); |
401 | return FALSE; |
402 | } |
403 | break; |
404 | |
f017db44 |
405 | /* |
406 | * Enums |
be66ef34 |
407 | * Entered as string, converted to enum |
408 | * |
f017db44 |
409 | * can only be compared with 'equal' or 'not equal' operators |
410 | * |
411 | * unsigned int of 16 bits are used here since enums |
be66ef34 |
412 | * should not go over 2^16-1 values |
f017db44 |
413 | */ |
be66ef34 |
414 | // case /*NOTHING*/: |
415 | // switch(op) { |
416 | // case LTTV_FIELD_EQ: |
417 | // se->op = lttv_apply_op_eq_uint16; |
418 | // break; |
419 | // case LTTV_FIELD_NE: |
420 | // se->op = lttv_apply_op_ne_uint16; |
421 | // break; |
422 | // default: |
423 | // g_warning("Error encountered in operator assignment = or != expected"); |
424 | // return FALSE; |
425 | // } |
426 | // break; |
56e29124 |
427 | /* |
7145a073 |
428 | * Ltttime |
56e29124 |
429 | */ |
bb87caa7 |
430 | case LTTV_FILTER_STATE_CT: |
431 | case LTTV_FILTER_STATE_IT: |
432 | case LTTV_FILTER_EVENT_TIME: |
bb87caa7 |
433 | switch(op) { |
434 | case LTTV_FIELD_EQ: |
7145a073 |
435 | se->op = lttv_apply_op_eq_ltttime; |
bb87caa7 |
436 | break; |
437 | case LTTV_FIELD_NE: |
7145a073 |
438 | se->op = lttv_apply_op_ne_ltttime; |
bb87caa7 |
439 | break; |
440 | case LTTV_FIELD_LT: |
7145a073 |
441 | se->op = lttv_apply_op_lt_ltttime; |
bb87caa7 |
442 | break; |
443 | case LTTV_FIELD_LE: |
7145a073 |
444 | se->op = lttv_apply_op_le_ltttime; |
bb87caa7 |
445 | break; |
446 | case LTTV_FIELD_GT: |
7145a073 |
447 | se->op = lttv_apply_op_gt_ltttime; |
bb87caa7 |
448 | break; |
449 | case LTTV_FIELD_GE: |
7145a073 |
450 | se->op = lttv_apply_op_ge_ltttime; |
bb87caa7 |
451 | break; |
452 | default: |
453 | g_warning("Error encountered in operator assignment"); |
454 | return FALSE; |
455 | } |
456 | break; |
457 | default: |
9ab5ebd7 |
458 | g_warning("Error encountered in operator assignation ! Field type:%i",se->field); |
bb87caa7 |
459 | return FALSE; |
460 | } |
aa4600f3 |
461 | |
462 | return TRUE; |
bb87caa7 |
463 | |
464 | } |
465 | |
9ab5ebd7 |
466 | /** |
7e7af7f2 |
467 | * @fn gboolean lttv_simple_expression_assign_value(LttvSimpleExpression*,char*) |
9ab5ebd7 |
468 | * |
469 | * Assign the value field to the current LttvSimpleExpression |
470 | * @param se pointer to the current LttvSimpleExpression |
471 | * @param value string value for simple expression |
472 | */ |
be66ef34 |
473 | gboolean |
474 | lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value) { |
9ab5ebd7 |
475 | |
46c40a93 |
476 | unsigned i; |
477 | gboolean is_double = FALSE; |
c6832b57 |
478 | LttTime t = ltt_time_zero; |
479 | GString* v; |
571ef1ed |
480 | guint string_len; |
9ab5ebd7 |
481 | |
482 | switch(se->field) { |
483 | /* |
be66ef34 |
484 | * Strings |
485 | * entered as strings, converted to Quarks |
9ab5ebd7 |
486 | */ |
487 | case LTTV_FILTER_TRACE_NAME: |
488 | case LTTV_FILTER_TRACEFILE_NAME: |
489 | case LTTV_FILTER_STATE_P_NAME: |
7b5f6cf1 |
490 | case LTTV_FILTER_STATE_T_BRAND: |
6471e71f |
491 | case LTTV_FILTER_EVENT_SUBNAME: |
be66ef34 |
492 | case LTTV_FILTER_STATE_EX_MODE: |
493 | case LTTV_FILTER_STATE_EX_SUBMODE: |
494 | case LTTV_FILTER_STATE_P_STATUS: |
495 | // se->value.v_string = value; |
c56a714e |
496 | se->value.v_quark = g_quark_from_string(value); |
c6832b57 |
497 | g_free(value); |
9ab5ebd7 |
498 | break; |
6471e71f |
499 | /* |
500 | * Two strings. |
501 | */ |
502 | case LTTV_FILTER_EVENT_NAME: |
503 | { |
504 | /* channel.event */ |
505 | char *end = strchr(value, '.'); |
506 | if (end) { |
507 | *end = '\0'; |
508 | end++; |
509 | se->value.v_quarks.q[0] = g_quark_from_string(value); |
510 | se->value.v_quarks.q[1] = g_quark_from_string(end); |
511 | } else { |
512 | se->value.v_quarks.q[0] = (GQuark)0; |
513 | se->value.v_quarks.q[1] = g_quark_from_string(value); |
514 | } |
515 | g_free(value); |
516 | } |
517 | break; |
9ab5ebd7 |
518 | /* |
be66ef34 |
519 | * integer -- supposed to be uint64 |
9ab5ebd7 |
520 | */ |
bbd9d557 |
521 | case LTTV_FILTER_EVENT_TSC: |
9ab5ebd7 |
522 | se->value.v_uint64 = atoi(value); |
523 | g_free(value); |
524 | break; |
6e0d58d6 |
525 | /* |
2ec11087 |
526 | * unsigned integers |
6e0d58d6 |
527 | */ |
528 | case LTTV_FILTER_STATE_PID: |
529 | case LTTV_FILTER_STATE_PPID: |
530 | case LTTV_FILTER_STATE_CPU: |
33bdc8dd |
531 | case LTTV_FILTER_EVENT_TARGET_PID: |
2ec11087 |
532 | se->value.v_uint = atoi(value); |
6e0d58d6 |
533 | g_free(value); |
534 | break; |
9ab5ebd7 |
535 | /* |
7145a073 |
536 | * LttTime |
9ab5ebd7 |
537 | */ |
538 | case LTTV_FILTER_STATE_CT: |
539 | case LTTV_FILTER_STATE_IT: |
540 | case LTTV_FILTER_EVENT_TIME: |
7145a073 |
541 | //se->value.v_double = atof(value); |
46c40a93 |
542 | /* |
543 | * parsing logic could be optimised, |
544 | * but as for now, simpler this way |
545 | */ |
546 | v = g_string_new(""); |
571ef1ed |
547 | string_len = strlen(value); |
548 | for(i=0;i<string_len;i++) { |
46c40a93 |
549 | if(value[i] == '.') { |
550 | /* cannot specify number with more than one '.' */ |
551 | if(is_double) return FALSE; |
552 | else is_double = TRUE; |
826f1ab2 |
553 | t.tv_sec = atoi(v->str); |
46c40a93 |
554 | g_string_free(v,TRUE); |
555 | v = g_string_new(""); |
4ec0c904 |
556 | } else v = g_string_append_c(v,value[i]); |
46c40a93 |
557 | } |
558 | /* number can be integer or double */ |
826f1ab2 |
559 | if(is_double) t.tv_nsec = atoi(v->str); |
571ef1ed |
560 | else { |
561 | t.tv_sec = atoi(v->str); |
562 | t.tv_nsec = 0; |
563 | } |
c6832b57 |
564 | |
46c40a93 |
565 | g_string_free(v,TRUE); |
566 | |
567 | se->value.v_ltttime = t; |
9ab5ebd7 |
568 | g_free(value); |
569 | break; |
570 | default: |
571 | g_warning("Error encountered in value assignation ! Field type = %i",se->field); |
c6832b57 |
572 | g_free(value); |
9ab5ebd7 |
573 | return FALSE; |
574 | } |
575 | |
576 | return TRUE; |
577 | |
578 | } |
579 | |
31452f49 |
580 | /** |
56e29124 |
581 | * @fn void lttv_simple_expression_destroy(LttvSimpleExpression*) |
582 | * |
9ab5ebd7 |
583 | * Disallocate memory for the current |
56e29124 |
584 | * simple expression |
585 | * @param se pointer to the current LttvSimpleExpression |
586 | */ |
587 | void |
588 | lttv_simple_expression_destroy(LttvSimpleExpression* se) { |
589 | |
9ab5ebd7 |
590 | // g_free(se->value); |
786c5c3b |
591 | // switch(se->field) { |
592 | // case LTTV_FILTER_TRACE_NAME: |
593 | // case LTTV_FILTER_TRACEFILE_NAME: |
594 | // case LTTV_FILTER_STATE_P_NAME: |
595 | // case LTTV_FILTER_EVENT_NAME: |
596 | // g_free(se->value.v_string); |
597 | // break; |
598 | // } |
56e29124 |
599 | g_free(se); |
600 | |
601 | } |
602 | |
603 | /** |
604 | * @fn gint lttv_struct_type(gint) |
605 | * |
80f9611a |
606 | * Finds the structure type depending |
607 | * on the fields in parameters |
608 | * @params ft Field of the current structure |
609 | * @return LttvStructType enum or -1 for error |
84a333d6 |
610 | */ |
80f9611a |
611 | gint |
612 | lttv_struct_type(gint ft) { |
5f185a2b |
613 | |
80f9611a |
614 | switch(ft) { |
615 | case LTTV_FILTER_TRACE_NAME: |
616 | return LTTV_FILTER_TRACE; |
617 | break; |
618 | case LTTV_FILTER_TRACEFILE_NAME: |
619 | return LTTV_FILTER_TRACEFILE; |
620 | break; |
621 | case LTTV_FILTER_STATE_PID: |
622 | case LTTV_FILTER_STATE_PPID: |
623 | case LTTV_FILTER_STATE_CT: |
624 | case LTTV_FILTER_STATE_IT: |
625 | case LTTV_FILTER_STATE_P_NAME: |
6471e71f |
626 | case LTTV_FILTER_STATE_T_BRAND: |
80f9611a |
627 | case LTTV_FILTER_STATE_EX_MODE: |
628 | case LTTV_FILTER_STATE_EX_SUBMODE: |
629 | case LTTV_FILTER_STATE_P_STATUS: |
630 | case LTTV_FILTER_STATE_CPU: |
631 | return LTTV_FILTER_STATE; |
632 | break; |
633 | case LTTV_FILTER_EVENT_NAME: |
6471e71f |
634 | case LTTV_FILTER_EVENT_SUBNAME: |
80f9611a |
635 | case LTTV_FILTER_EVENT_CATEGORY: |
636 | case LTTV_FILTER_EVENT_TIME: |
637 | case LTTV_FILTER_EVENT_TSC: |
33bdc8dd |
638 | case LTTV_FILTER_EVENT_TARGET_PID: |
80f9611a |
639 | case LTTV_FILTER_EVENT_FIELD: |
640 | return LTTV_FILTER_EVENT; |
641 | break; |
642 | default: |
643 | return -1; |
644 | } |
84a333d6 |
645 | } |
646 | |
2ec11087 |
647 | /** |
648 | * @fn gboolean lttv_apply_op_eq_uint(gpointer,LttvFieldValue) |
649 | * |
650 | * Applies the 'equal' operator to the |
651 | * specified structure and value |
652 | * @param v1 left member of comparison |
653 | * @param v2 right member of comparison |
654 | * @return success/failure of operation |
655 | */ |
656 | gboolean lttv_apply_op_eq_uint(const gpointer v1, LttvFieldValue v2) { |
657 | |
658 | guint* r = (guint*) v1; |
659 | return (*r == v2.v_uint); |
660 | |
661 | } |
662 | |
150f0d33 |
663 | /** |
7e7af7f2 |
664 | * @fn gboolean lttv_apply_op_eq_uint64(gpointer,LttvFieldValue) |
56e29124 |
665 | * |
150f0d33 |
666 | * Applies the 'equal' operator to the |
47aa6e58 |
667 | * specified structure and value |
668 | * @param v1 left member of comparison |
669 | * @param v2 right member of comparison |
150f0d33 |
670 | * @return success/failure of operation |
671 | */ |
4d9ff942 |
672 | gboolean lttv_apply_op_eq_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
673 | |
674 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
675 | return (*r == v2.v_uint64); |
83aa92fc |
676 | |
677 | } |
150f0d33 |
678 | |
5b729fcf |
679 | /** |
7e7af7f2 |
680 | * @fn gboolean lttv_apply_op_eq_uint32(gpointer,LttvFieldValue) |
56e29124 |
681 | * |
5b729fcf |
682 | * Applies the 'equal' operator to the |
47aa6e58 |
683 | * specified structure and value |
684 | * @param v1 left member of comparison |
685 | * @param v2 right member of comparison |
5b729fcf |
686 | * @return success/failure of operation |
687 | */ |
4d9ff942 |
688 | gboolean lttv_apply_op_eq_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
689 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
690 | return (*r == v2.v_uint32); |
83aa92fc |
691 | } |
5b729fcf |
692 | |
693 | /** |
7e7af7f2 |
694 | * @fn gboolean lttv_apply_op_eq_uint16(gpointer,LttvFieldValue) |
56e29124 |
695 | * |
5b729fcf |
696 | * Applies the 'equal' operator to the |
47aa6e58 |
697 | * specified structure and value |
698 | * @param v1 left member of comparison |
699 | * @param v2 right member of comparison |
5b729fcf |
700 | * @return success/failure of operation |
701 | */ |
4d9ff942 |
702 | gboolean lttv_apply_op_eq_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
703 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
704 | return (*r == v2.v_uint16); |
83aa92fc |
705 | } |
5b729fcf |
706 | |
707 | /** |
7e7af7f2 |
708 | * @fn gboolean lttv_apply_op_eq_double(gpointer,LttvFieldValue) |
56e29124 |
709 | * |
5b729fcf |
710 | * Applies the 'equal' operator to the |
47aa6e58 |
711 | * specified structure and value |
712 | * @param v1 left member of comparison |
713 | * @param v2 right member of comparison |
5b729fcf |
714 | * @return success/failure of operation |
715 | */ |
4d9ff942 |
716 | gboolean lttv_apply_op_eq_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
717 | double* r = (double*) v1; |
9ab5ebd7 |
718 | return (*r == v2.v_double); |
83aa92fc |
719 | } |
5b729fcf |
720 | |
721 | /** |
7e7af7f2 |
722 | * @fn gboolean lttv_apply_op_eq_string(gpointer,LttvFieldValue) |
56e29124 |
723 | * |
5b729fcf |
724 | * Applies the 'equal' operator to the |
47aa6e58 |
725 | * specified structure and value |
726 | * @param v1 left member of comparison |
727 | * @param v2 right member of comparison |
5b729fcf |
728 | * @return success/failure of operation |
729 | */ |
4d9ff942 |
730 | gboolean lttv_apply_op_eq_string(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
731 | char* r = (char*) v1; |
9ab5ebd7 |
732 | return (!g_strcasecmp(r,v2.v_string)); |
83aa92fc |
733 | } |
150f0d33 |
734 | |
c6832b57 |
735 | /** |
7e7af7f2 |
736 | * @fn gboolean lttv_apply_op_eq_quark(gpointer,LttvFieldValue) |
c6832b57 |
737 | * |
738 | * Applies the 'equal' operator to the |
739 | * specified structure and value |
740 | * @param v1 left member of comparison |
741 | * @param v2 right member of comparison |
742 | * @return success/failure of operation |
743 | */ |
744 | gboolean lttv_apply_op_eq_quark(const gpointer v1, LttvFieldValue v2) { |
745 | GQuark* r = (GQuark*) v1; |
c56a714e |
746 | return (*r == v2.v_quark); |
c6832b57 |
747 | } |
748 | |
6471e71f |
749 | /** |
750 | * @fn gboolean lttv_apply_op_eq_quarks(gpointer,LttvFieldValue) |
751 | * |
752 | * Applies the 'equal' operator to the |
753 | * specified structure and value |
754 | * @param v1 left member of comparison |
755 | * @param v2 right member of comparison |
756 | * @return success/failure of operation |
757 | */ |
758 | gboolean lttv_apply_op_eq_quarks(const gpointer v1, LttvFieldValue v2) { |
759 | GQuark *r1 = (GQuark *) v1; |
760 | GQuark *r2 = r1 + 1; |
761 | if (likely(*r1 != (GQuark)0) && *r1 != v2.v_quarks.q[0]) |
762 | return 0; |
763 | if (*r2 != v2.v_quarks.q[1]) |
764 | return 0; |
765 | return 1; |
766 | } |
767 | |
7145a073 |
768 | /** |
7e7af7f2 |
769 | * @fn gboolean lttv_apply_op_eq_ltttime(gpointer,LttvFieldValue) |
7145a073 |
770 | * |
771 | * Applies the 'equal' operator to the |
772 | * specified structure and value |
773 | * @param v1 left member of comparison |
774 | * @param v2 right member of comparison |
775 | * @return success/failure of operation |
776 | */ |
4d9ff942 |
777 | gboolean lttv_apply_op_eq_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
778 | LttTime* r = (LttTime*) v1; |
c6832b57 |
779 | return ltt_time_compare(*r, v2.v_ltttime)==0?1:0; |
7145a073 |
780 | } |
781 | |
2ec11087 |
782 | /** |
783 | * @fn gboolean lttv_apply_op_ne_uint(gpointer,LttvFieldValue) |
784 | * |
785 | * Applies the 'not equal' operator to the |
786 | * specified structure and value |
787 | * @param v1 left member of comparison |
788 | * @param v2 right member of comparison |
789 | * @return success/failure of operation |
790 | */ |
791 | gboolean lttv_apply_op_ne_uint(const gpointer v1, LttvFieldValue v2) { |
792 | guint* r = (guint*) v1; |
793 | return (*r != v2.v_uint); |
794 | } |
7145a073 |
795 | |
150f0d33 |
796 | /** |
7e7af7f2 |
797 | * @fn gboolean lttv_apply_op_ne_uint64(gpointer,LttvFieldValue) |
56e29124 |
798 | * |
150f0d33 |
799 | * Applies the 'not equal' operator to the |
47aa6e58 |
800 | * specified structure and value |
801 | * @param v1 left member of comparison |
802 | * @param v2 right member of comparison |
150f0d33 |
803 | * @return success/failure of operation |
804 | */ |
4d9ff942 |
805 | gboolean lttv_apply_op_ne_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
806 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
807 | return (*r != v2.v_uint64); |
83aa92fc |
808 | } |
150f0d33 |
809 | |
5b729fcf |
810 | /** |
7e7af7f2 |
811 | * @fn gboolean lttv_apply_op_ne_uint32(gpointer,LttvFieldValue) |
56e29124 |
812 | * |
5b729fcf |
813 | * Applies the 'not equal' operator to the |
47aa6e58 |
814 | * specified structure and value |
815 | * @param v1 left member of comparison |
816 | * @param v2 right member of comparison |
5b729fcf |
817 | * @return success/failure of operation |
818 | */ |
4d9ff942 |
819 | gboolean lttv_apply_op_ne_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
820 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
821 | return (*r != v2.v_uint32); |
83aa92fc |
822 | } |
5b729fcf |
823 | |
824 | /** |
7e7af7f2 |
825 | * @fn gboolean lttv_apply_op_ne_uint16(gpointer,LttvFieldValue) |
56e29124 |
826 | * |
5b729fcf |
827 | * Applies the 'not equal' operator to the |
47aa6e58 |
828 | * specified structure and value |
829 | * @param v1 left member of comparison |
830 | * @param v2 right member of comparison |
5b729fcf |
831 | * @return success/failure of operation |
832 | */ |
4d9ff942 |
833 | gboolean lttv_apply_op_ne_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
834 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
835 | return (*r != v2.v_uint16); |
83aa92fc |
836 | } |
5b729fcf |
837 | |
838 | /** |
7e7af7f2 |
839 | * @fn gboolean lttv_apply_op_ne_double(gpointer,LttvFieldValue) |
56e29124 |
840 | * |
5b729fcf |
841 | * Applies the 'not equal' operator to the |
47aa6e58 |
842 | * specified structure and value |
843 | * @param v1 left member of comparison |
844 | * @param v2 right member of comparison |
5b729fcf |
845 | * @return success/failure of operation |
846 | */ |
4d9ff942 |
847 | gboolean lttv_apply_op_ne_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
848 | double* r = (double*) v1; |
9ab5ebd7 |
849 | return (*r != v2.v_double); |
83aa92fc |
850 | } |
5b729fcf |
851 | |
852 | /** |
7e7af7f2 |
853 | * @fn gboolean lttv_apply_op_ne_string(gpointer,LttvFieldValue) |
56e29124 |
854 | * |
5b729fcf |
855 | * Applies the 'not equal' operator to the |
47aa6e58 |
856 | * specified structure and value |
857 | * @param v1 left member of comparison |
858 | * @param v2 right member of comparison |
5b729fcf |
859 | * @return success/failure of operation |
860 | */ |
4d9ff942 |
861 | gboolean lttv_apply_op_ne_string(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
862 | char* r = (char*) v1; |
9ab5ebd7 |
863 | return (g_strcasecmp(r,v2.v_string)); |
83aa92fc |
864 | } |
150f0d33 |
865 | |
c6832b57 |
866 | /** |
7e7af7f2 |
867 | * @fn gboolean lttv_apply_op_ne_quark(gpointer,LttvFieldValue) |
c6832b57 |
868 | * |
869 | * Applies the 'not equal' operator to the |
870 | * specified structure and value |
871 | * @param v1 left member of comparison |
872 | * @param v2 right member of comparison |
873 | * @return success/failure of operation |
874 | */ |
875 | gboolean lttv_apply_op_ne_quark(const gpointer v1, LttvFieldValue v2) { |
876 | GQuark* r = (GQuark*) v1; |
c56a714e |
877 | return (*r != v2.v_quark); |
c6832b57 |
878 | } |
879 | |
6471e71f |
880 | /** |
881 | * @fn gboolean lttv_apply_op_ne_quarks(gpointer,LttvFieldValue) |
882 | * |
883 | * Applies the 'equal' operator to the |
884 | * specified structure and value |
885 | * @param v1 left member of comparison |
886 | * @param v2 right member of comparison |
887 | * @return success/failure of operation |
888 | */ |
889 | gboolean lttv_apply_op_ne_quarks(const gpointer v1, LttvFieldValue v2) { |
890 | GQuark *r1 = (GQuark *) v1; |
891 | GQuark *r2 = r1 + 1; |
892 | if ((*r1 == (GQuark)0 || *r1 == v2.v_quarks.q[0]) && *r2 == v2.v_quarks.q[1]) |
893 | return 0; |
894 | else |
895 | return 1; |
896 | } |
c6832b57 |
897 | |
7145a073 |
898 | /** |
7e7af7f2 |
899 | * @fn gboolean lttv_apply_op_ne_ltttime(gpointer,LttvFieldValue) |
7145a073 |
900 | * |
901 | * Applies the 'not equal' operator to the |
902 | * specified structure and value |
903 | * @param v1 left member of comparison |
904 | * @param v2 right member of comparison |
905 | * @return success/failure of operation |
906 | */ |
4d9ff942 |
907 | gboolean lttv_apply_op_ne_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
908 | LttTime* r = (LttTime*) v1; |
46c40a93 |
909 | return ltt_time_compare(*r, v2.v_ltttime)!=0?1:0; |
7145a073 |
910 | } |
911 | |
2ec11087 |
912 | /** |
913 | * @fn gboolean lttv_apply_op_lt_uint(gpointer,LttvFieldValue) |
914 | * |
915 | * Applies the 'lower than' operator to the |
916 | * specified structure and value |
917 | * @param v1 left member of comparison |
918 | * @param v2 right member of comparison |
919 | * @return success/failure of operation |
920 | */ |
921 | gboolean lttv_apply_op_lt_uint(const gpointer v1, LttvFieldValue v2) { |
922 | guint* r = (guint*) v1; |
923 | return (*r < v2.v_uint); |
924 | } |
7145a073 |
925 | |
150f0d33 |
926 | /** |
7e7af7f2 |
927 | * @fn gboolean lttv_apply_op_lt_uint64(gpointer,LttvFieldValue) |
56e29124 |
928 | * |
150f0d33 |
929 | * Applies the 'lower than' operator to the |
47aa6e58 |
930 | * specified structure and value |
931 | * @param v1 left member of comparison |
932 | * @param v2 right member of comparison |
150f0d33 |
933 | * @return success/failure of operation |
934 | */ |
4d9ff942 |
935 | gboolean lttv_apply_op_lt_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
936 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
937 | return (*r < v2.v_uint64); |
83aa92fc |
938 | } |
150f0d33 |
939 | |
5b729fcf |
940 | /** |
7e7af7f2 |
941 | * @fn gboolean lttv_apply_op_lt_uint32(gpointer,LttvFieldValue) |
56e29124 |
942 | * |
5b729fcf |
943 | * Applies the 'lower than' operator to the |
47aa6e58 |
944 | * specified structure and value |
945 | * @param v1 left member of comparison |
946 | * @param v2 right member of comparison |
5b729fcf |
947 | * @return success/failure of operation |
948 | */ |
4d9ff942 |
949 | gboolean lttv_apply_op_lt_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
950 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
951 | return (*r < v2.v_uint32); |
83aa92fc |
952 | } |
5b729fcf |
953 | |
954 | /** |
7e7af7f2 |
955 | * @fn gboolean lttv_apply_op_lt_uint16(gpointer,LttvFieldValue) |
56e29124 |
956 | * |
5b729fcf |
957 | * Applies the 'lower than' operator to the |
47aa6e58 |
958 | * specified structure and value |
959 | * @param v1 left member of comparison |
960 | * @param v2 right member of comparison |
5b729fcf |
961 | * @return success/failure of operation |
962 | */ |
4d9ff942 |
963 | gboolean lttv_apply_op_lt_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
964 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
965 | return (*r < v2.v_uint16); |
83aa92fc |
966 | } |
5b729fcf |
967 | |
968 | /** |
7e7af7f2 |
969 | * @fn gboolean lttv_apply_op_lt_double(gpointer,LttvFieldValue) |
56e29124 |
970 | * |
5b729fcf |
971 | * Applies the 'lower than' operator to the |
47aa6e58 |
972 | * specified structure and value |
973 | * @param v1 left member of comparison |
974 | * @param v2 right member of comparison |
5b729fcf |
975 | * @return success/failure of operation |
976 | */ |
4d9ff942 |
977 | gboolean lttv_apply_op_lt_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
978 | double* r = (double*) v1; |
9ab5ebd7 |
979 | return (*r < v2.v_double); |
83aa92fc |
980 | } |
5b729fcf |
981 | |
7145a073 |
982 | /** |
7e7af7f2 |
983 | * @fn gboolean lttv_apply_op_lt_ltttime(gpointer,LttvFieldValue) |
7145a073 |
984 | * |
985 | * Applies the 'lower than' operator to the |
986 | * specified structure and value |
987 | * @param v1 left member of comparison |
988 | * @param v2 right member of comparison |
989 | * @return success/failure of operation |
990 | */ |
4d9ff942 |
991 | gboolean lttv_apply_op_lt_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
992 | LttTime* r = (LttTime*) v1; |
c6832b57 |
993 | // return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec < v2.v_ltttime.tv_nsec))); |
994 | return ltt_time_compare(*r, v2.v_ltttime)==-1?1:0; |
7145a073 |
995 | } |
996 | |
2ec11087 |
997 | /** |
998 | * @fn gboolean lttv_apply_op_le_uint(gpointer,LttvFieldValue) |
999 | * |
1000 | * Applies the 'lower or equal' operator to the |
1001 | * specified structure and value |
1002 | * @param v1 left member of comparison |
1003 | * @param v2 right member of comparison |
1004 | * @return success/failure of operation |
1005 | */ |
1006 | gboolean lttv_apply_op_le_uint(const gpointer v1, LttvFieldValue v2) { |
1007 | guint* r = (guint*) v1; |
1008 | return (*r <= v2.v_uint); |
1009 | } |
7145a073 |
1010 | |
5b729fcf |
1011 | /** |
7e7af7f2 |
1012 | * @fn gboolean lttv_apply_op_le_uint64(gpointer,LttvFieldValue) |
56e29124 |
1013 | * |
1014 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
1015 | * specified structure and value |
1016 | * @param v1 left member of comparison |
1017 | * @param v2 right member of comparison |
5b729fcf |
1018 | * @return success/failure of operation |
1019 | */ |
4d9ff942 |
1020 | gboolean lttv_apply_op_le_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1021 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
1022 | return (*r <= v2.v_uint64); |
83aa92fc |
1023 | } |
150f0d33 |
1024 | |
1025 | /** |
7e7af7f2 |
1026 | * @fn gboolean lttv_apply_op_le_uint32(gpointer,LttvFieldValue) |
56e29124 |
1027 | * |
150f0d33 |
1028 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
1029 | * specified structure and value |
1030 | * @param v1 left member of comparison |
1031 | * @param v2 right member of comparison |
150f0d33 |
1032 | * @return success/failure of operation |
1033 | */ |
4d9ff942 |
1034 | gboolean lttv_apply_op_le_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1035 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
1036 | return (*r <= v2.v_uint32); |
83aa92fc |
1037 | } |
150f0d33 |
1038 | |
5b729fcf |
1039 | /** |
7e7af7f2 |
1040 | * @fn gboolean lttv_apply_op_le_uint16(gpointer,LttvFieldValue) |
56e29124 |
1041 | * |
5b729fcf |
1042 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
1043 | * specified structure and value |
1044 | * @param v1 left member of comparison |
1045 | * @param v2 right member of comparison |
5b729fcf |
1046 | * @return success/failure of operation |
1047 | */ |
4d9ff942 |
1048 | gboolean lttv_apply_op_le_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1049 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
1050 | return (*r <= v2.v_uint16); |
83aa92fc |
1051 | } |
5b729fcf |
1052 | |
1053 | /** |
7e7af7f2 |
1054 | * @fn gboolean lttv_apply_op_le_double(gpointer,LttvFieldValue) |
56e29124 |
1055 | * |
5b729fcf |
1056 | * Applies the 'lower or equal' operator to the |
47aa6e58 |
1057 | * specified structure and value |
1058 | * @param v1 left member of comparison |
1059 | * @param v2 right member of comparison |
5b729fcf |
1060 | * @return success/failure of operation |
1061 | */ |
4d9ff942 |
1062 | gboolean lttv_apply_op_le_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1063 | double* r = (double*) v1; |
9ab5ebd7 |
1064 | return (*r <= v2.v_double); |
83aa92fc |
1065 | } |
5b729fcf |
1066 | |
7145a073 |
1067 | /** |
7e7af7f2 |
1068 | * @fn gboolean lttv_apply_op_le_ltttime(gpointer,LttvFieldValue) |
7145a073 |
1069 | * |
1070 | * Applies the 'lower or equal' operator to the |
1071 | * specified structure and value |
1072 | * @param v1 left member of comparison |
1073 | * @param v2 right member of comparison |
1074 | * @return success/failure of operation |
1075 | */ |
4d9ff942 |
1076 | gboolean lttv_apply_op_le_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
1077 | LttTime* r = (LttTime*) v1; |
c6832b57 |
1078 | // return ((r->tv_sec < v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec <= v2.v_ltttime.tv_nsec))); |
1079 | return ltt_time_compare(*r, v2.v_ltttime)<1?1:0; |
7145a073 |
1080 | } |
1081 | |
1082 | |
2ec11087 |
1083 | /** |
1084 | * @fn gboolean lttv_apply_op_gt_uint(gpointer,LttvFieldValue) |
1085 | * |
1086 | * Applies the 'greater than' operator to the |
1087 | * specified structure and value |
1088 | * @param v1 left member of comparison |
1089 | * @param v2 right member of comparison |
1090 | * @return success/failure of operation |
1091 | */ |
1092 | gboolean lttv_apply_op_gt_uint(const gpointer v1, LttvFieldValue v2) { |
1093 | guint* r = (guint*) v1; |
1094 | return (*r > v2.v_uint); |
1095 | } |
1096 | |
5b729fcf |
1097 | /** |
7e7af7f2 |
1098 | * @fn gboolean lttv_apply_op_gt_uint64(gpointer,LttvFieldValue) |
56e29124 |
1099 | * |
83aa92fc |
1100 | * Applies the 'greater than' operator to the |
47aa6e58 |
1101 | * specified structure and value |
1102 | * @param v1 left member of comparison |
1103 | * @param v2 right member of comparison |
5b729fcf |
1104 | * @return success/failure of operation |
1105 | */ |
4d9ff942 |
1106 | gboolean lttv_apply_op_gt_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1107 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
1108 | return (*r > v2.v_uint64); |
83aa92fc |
1109 | } |
150f0d33 |
1110 | |
1111 | /** |
7e7af7f2 |
1112 | * @fn gboolean lttv_apply_op_gt_uint32(gpointer,LttvFieldValue) |
56e29124 |
1113 | * |
150f0d33 |
1114 | * Applies the 'greater than' operator to the |
47aa6e58 |
1115 | * specified structure and value |
1116 | * @param v1 left member of comparison |
1117 | * @param v2 right member of comparison |
150f0d33 |
1118 | * @return success/failure of operation |
1119 | */ |
4d9ff942 |
1120 | gboolean lttv_apply_op_gt_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1121 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
1122 | return (*r > v2.v_uint32); |
83aa92fc |
1123 | } |
150f0d33 |
1124 | |
5b729fcf |
1125 | /** |
7e7af7f2 |
1126 | * @fn gboolean lttv_apply_op_gt_uint16(gpointer,LttvFieldValue) |
56e29124 |
1127 | * |
5b729fcf |
1128 | * Applies the 'greater than' operator to the |
47aa6e58 |
1129 | * specified structure and value |
1130 | * @param v1 left member of comparison |
1131 | * @param v2 right member of comparison |
5b729fcf |
1132 | * @return success/failure of operation |
1133 | */ |
4d9ff942 |
1134 | gboolean lttv_apply_op_gt_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1135 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
1136 | return (*r > v2.v_uint16); |
83aa92fc |
1137 | } |
5b729fcf |
1138 | |
1139 | /** |
7e7af7f2 |
1140 | * @fn gboolean lttv_apply_op_gt_double(gpointer,LttvFieldValue) |
56e29124 |
1141 | * |
5b729fcf |
1142 | * Applies the 'greater than' operator to the |
47aa6e58 |
1143 | * specified structure and value |
1144 | * @param v1 left member of comparison |
1145 | * @param v2 right member of comparison |
5b729fcf |
1146 | * @return success/failure of operation |
1147 | */ |
4d9ff942 |
1148 | gboolean lttv_apply_op_gt_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1149 | double* r = (double*) v1; |
9ab5ebd7 |
1150 | return (*r > v2.v_double); |
83aa92fc |
1151 | } |
5b729fcf |
1152 | |
7145a073 |
1153 | /** |
7e7af7f2 |
1154 | * @fn gboolean lttv_apply_op_gt_ltttime(gpointer,LttvFieldValue) |
7145a073 |
1155 | * |
1156 | * Applies the 'greater than' operator to the |
1157 | * specified structure and value |
1158 | * @param v1 left member of comparison |
1159 | * @param v2 right member of comparison |
1160 | * @return success/failure of operation |
1161 | */ |
4d9ff942 |
1162 | gboolean lttv_apply_op_gt_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
1163 | LttTime* r = (LttTime*) v1; |
c6832b57 |
1164 | // return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec > v2.v_ltttime.tv_nsec))); |
1165 | return ltt_time_compare(*r, v2.v_ltttime)==1?1:0; |
7145a073 |
1166 | } |
1167 | |
2ec11087 |
1168 | /** |
1169 | * @fn gboolean lttv_apply_op_ge_uint(gpointer,LttvFieldValue) |
1170 | * |
1171 | * Applies the 'greater or equal' operator to the |
1172 | * specified structure and value |
1173 | * @param v1 left member of comparison |
1174 | * @param v2 right member of comparison |
1175 | * @return success/failure of operation |
1176 | */ |
1177 | gboolean lttv_apply_op_ge_uint(const gpointer v1, LttvFieldValue v2) { |
1178 | guint* r = (guint*) v1; |
1179 | return (*r >= v2.v_uint); |
1180 | } |
7145a073 |
1181 | |
5b729fcf |
1182 | /** |
7e7af7f2 |
1183 | * @fn gboolean lttv_apply_op_ge_uint64(gpointer,LttvFieldValue) |
56e29124 |
1184 | * |
83aa92fc |
1185 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
1186 | * specified structure and value |
1187 | * @param v1 left member of comparison |
1188 | * @param v2 right member of comparison |
5b729fcf |
1189 | * @return success/failure of operation |
1190 | */ |
4d9ff942 |
1191 | gboolean lttv_apply_op_ge_uint64(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1192 | guint64* r = (guint64*) v1; |
9ab5ebd7 |
1193 | return (*r >= v2.v_uint64); |
83aa92fc |
1194 | } |
150f0d33 |
1195 | |
1196 | /** |
7e7af7f2 |
1197 | * @fn gboolean lttv_apply_op_ge_uint32(gpointer,LttvFieldValue) |
56e29124 |
1198 | * |
150f0d33 |
1199 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
1200 | * specified structure and value |
1201 | * @param v1 left member of comparison |
1202 | * @param v2 right member of comparison |
150f0d33 |
1203 | * @return success/failure of operation |
1204 | */ |
4d9ff942 |
1205 | gboolean lttv_apply_op_ge_uint32(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1206 | guint32* r = (guint32*) v1; |
9ab5ebd7 |
1207 | return (*r >= v2.v_uint32); |
83aa92fc |
1208 | } |
150f0d33 |
1209 | |
5b729fcf |
1210 | /** |
7e7af7f2 |
1211 | * @fn gboolean lttv_apply_op_ge_uint16(gpointer,LttvFieldValue) |
56e29124 |
1212 | * |
5b729fcf |
1213 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
1214 | * specified structure and value |
1215 | * @param v1 left member of comparison |
1216 | * @param v2 right member of comparison |
5b729fcf |
1217 | * @return success/failure of operation |
1218 | */ |
4d9ff942 |
1219 | gboolean lttv_apply_op_ge_uint16(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1220 | guint16* r = (guint16*) v1; |
9ab5ebd7 |
1221 | return (*r >= v2.v_uint16); |
83aa92fc |
1222 | } |
150f0d33 |
1223 | |
5b729fcf |
1224 | /** |
7e7af7f2 |
1225 | * @fn gboolean lttv_apply_op_ge_double(gpointer,LttvFieldValue) |
56e29124 |
1226 | * |
5b729fcf |
1227 | * Applies the 'greater or equal' operator to the |
47aa6e58 |
1228 | * specified structure and value |
1229 | * @param v1 left member of comparison |
1230 | * @param v2 right member of comparison |
5b729fcf |
1231 | * @return success/failure of operation |
1232 | */ |
4d9ff942 |
1233 | gboolean lttv_apply_op_ge_double(const gpointer v1, LttvFieldValue v2) { |
83aa92fc |
1234 | double* r = (double*) v1; |
9ab5ebd7 |
1235 | return (*r >= v2.v_double); |
83aa92fc |
1236 | } |
150f0d33 |
1237 | |
7145a073 |
1238 | /** |
7e7af7f2 |
1239 | * @fn gboolean lttv_apply_op_ge_ltttime(gpointer,LttvFieldValue) |
7145a073 |
1240 | * |
1241 | * Applies the 'greater or equal' operator to the |
1242 | * specified structure and value |
1243 | * @param v1 left member of comparison |
1244 | * @param v2 right member of comparison |
1245 | * @return success/failure of operation |
1246 | */ |
4d9ff942 |
1247 | gboolean lttv_apply_op_ge_ltttime(const gpointer v1, LttvFieldValue v2) { |
7145a073 |
1248 | LttTime* r = (LttTime*) v1; |
c6832b57 |
1249 | // return ((r->tv_sec > v2.v_ltttime.tv_sec) || ((r->tv_sec == v2.v_ltttime.tv_sec) && (r->tv_nsec >= v2.v_ltttime.tv_nsec))); |
1250 | return ltt_time_compare(*r, v2.v_ltttime)>-1?1:0; |
7145a073 |
1251 | } |
1252 | |
1253 | |
150f0d33 |
1254 | |
1255 | /** |
56e29124 |
1256 | * Makes a copy of the current filter tree |
1257 | * @param tree pointer to the current tree |
1258 | * @return new copy of the filter tree |
150f0d33 |
1259 | */ |
1260 | LttvFilterTree* |
4d9ff942 |
1261 | lttv_filter_tree_clone(const LttvFilterTree* tree) { |
150f0d33 |
1262 | |
8c89f5a8 |
1263 | LttvFilterTree* newtree = lttv_filter_tree_new(); |
150f0d33 |
1264 | |
8c89f5a8 |
1265 | newtree->node = tree->node; |
1266 | |
1267 | newtree->left = tree->left; |
1268 | if(newtree->left == LTTV_TREE_NODE) { |
1269 | newtree->l_child.t = lttv_filter_tree_clone(tree->l_child.t); |
1270 | } else if(newtree->left == LTTV_TREE_LEAF) { |
1271 | newtree->l_child.leaf = lttv_simple_expression_new(); |
1272 | newtree->l_child.leaf->field = tree->l_child.leaf->field; |
1273 | newtree->l_child.leaf->offset = tree->l_child.leaf->offset; |
1274 | newtree->l_child.leaf->op = tree->l_child.leaf->op; |
9ab5ebd7 |
1275 | /* FIXME: special case for string copy ! */ |
1276 | newtree->l_child.leaf->value = tree->l_child.leaf->value; |
8c89f5a8 |
1277 | } |
1278 | |
1279 | newtree->right = tree->right; |
1280 | if(newtree->right == LTTV_TREE_NODE) { |
1281 | newtree->r_child.t = lttv_filter_tree_clone(tree->r_child.t); |
1282 | } else if(newtree->right == LTTV_TREE_LEAF) { |
1283 | newtree->r_child.leaf = lttv_simple_expression_new(); |
1284 | newtree->r_child.leaf->field = tree->r_child.leaf->field; |
1285 | newtree->r_child.leaf->offset = tree->r_child.leaf->offset; |
1286 | newtree->r_child.leaf->op = tree->r_child.leaf->op; |
9ab5ebd7 |
1287 | newtree->r_child.leaf->value = tree->r_child.leaf->value; |
8c89f5a8 |
1288 | } |
1289 | |
1290 | return newtree; |
1291 | |
150f0d33 |
1292 | } |
1293 | |
1294 | /** |
56e29124 |
1295 | * Makes a copy of the current filter |
1296 | * @param filter pointer to the current filter |
1297 | * @return new copy of the filter |
150f0d33 |
1298 | */ |
1299 | LttvFilter* |
4d9ff942 |
1300 | lttv_filter_clone(const LttvFilter* filter) { |
ebcead4a |
1301 | |
571ef1ed |
1302 | if(!filter) return NULL; |
ebcead4a |
1303 | |
150f0d33 |
1304 | LttvFilter* newfilter = g_new(LttvFilter,1); |
1305 | |
150f0d33 |
1306 | strcpy(newfilter->expression,filter->expression); |
1307 | |
1308 | newfilter->head = lttv_filter_tree_clone(filter->head); |
1309 | |
1310 | return newfilter; |
1311 | |
1312 | } |
1313 | |
1314 | |
84a333d6 |
1315 | /** |
56e29124 |
1316 | * @fn LttvFilter* lttv_filter_new() |
1317 | * |
571ef1ed |
1318 | * Creates a new LttvFilter |
1319 | * @return the current LttvFilter or NULL if error |
31452f49 |
1320 | */ |
2ea36caf |
1321 | LttvFilter* |
5f185a2b |
1322 | lttv_filter_new() { |
a4c292d4 |
1323 | |
5f185a2b |
1324 | LttvFilter* filter = g_new(LttvFilter,1); |
1325 | filter->expression = NULL; |
1326 | filter->head = NULL; |
7e7af7f2 |
1327 | |
1328 | return filter; |
5f185a2b |
1329 | |
1330 | } |
a4c292d4 |
1331 | |
8c89f5a8 |
1332 | /** |
56e29124 |
1333 | * @fn gboolean lttv_filter_update(LttvFilter*) |
1334 | * |
8c89f5a8 |
1335 | * Updates the current LttvFilter by building |
1336 | * its tree based upon the expression string |
1337 | * @param filter pointer to the current LttvFilter |
1338 | * @return Failure/Success of operation |
1339 | */ |
5f185a2b |
1340 | gboolean |
1341 | lttv_filter_update(LttvFilter* filter) { |
1342 | |
571ef1ed |
1343 | // g_print("filter::lttv_filter_new()\n"); /* debug */ |
5f185a2b |
1344 | |
1345 | if(filter->expression == NULL) return FALSE; |
1346 | |
571ef1ed |
1347 | int |
a4c292d4 |
1348 | i, |
571ef1ed |
1349 | p_nesting=0, /* parenthesis nesting value */ |
72911c5d |
1350 | not=0; |
571ef1ed |
1351 | guint expression_len; |
72911c5d |
1352 | |
1601b365 |
1353 | /* trees */ |
5b729fcf |
1354 | LttvFilterTree |
1601b365 |
1355 | *tree = lttv_filter_tree_new(), /* main tree */ |
1356 | *subtree = NULL, /* buffer for subtrees */ |
1357 | *t1, /* buffer #1 */ |
72911c5d |
1358 | *t2, /* buffer #2 */ |
1359 | *t3; /* buffer #3 */ |
1601b365 |
1360 | |
5f185a2b |
1361 | /* |
1362 | * the filter |
1363 | * If the tree already exists, |
1364 | * destroy it and build a new one |
1365 | */ |
1366 | if(filter->head != NULL) lttv_filter_tree_destroy(filter->head); |
f3020899 |
1367 | filter->head = NULL; /* will be assigned at the end */ |
5f185a2b |
1368 | |
1601b365 |
1369 | /* |
1370 | * Tree Stack |
f4e9dd16 |
1371 | * each element of the list |
1372 | * is a sub tree created |
1373 | * by the use of parenthesis in the |
1374 | * global expression. The final tree |
1601b365 |
1375 | * will be the one left at the root of |
f4e9dd16 |
1376 | * the list |
1377 | */ |
18d1226f |
1378 | GPtrArray *tree_stack = g_ptr_array_new(); |
1379 | g_ptr_array_add( tree_stack,(gpointer) tree ); |
f4e9dd16 |
1380 | |
a4c292d4 |
1381 | /* temporary values */ |
0769c82f |
1382 | GString *a_field_component = g_string_new(""); |
ef2b07c1 |
1383 | GString *a_string_spaces = g_string_new(""); |
56e29124 |
1384 | GPtrArray *a_field_path = g_ptr_array_new(); |
1385 | |
1386 | /* simple expression buffer */ |
389ba50e |
1387 | LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new(); |
d564d2ad |
1388 | |
1389 | gint nest_quotes = 0; |
0769c82f |
1390 | |
a4c292d4 |
1391 | /* |
571ef1ed |
1392 | * Parse entire expression and construct |
1393 | * the binary tree. There are two steps |
1394 | * in browsing that string |
1395 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
1396 | * 2. finding simple expressions |
1397 | * - field path ( separated by dots ) |
1398 | * - op ( >, <, =, >=, <=, !=) |
1399 | * - value ( integer, string ... ) |
1400 | * To spare computing time, the whole |
1401 | * string is parsed in this loop for a |
1402 | * O(n) complexity order. |
1601b365 |
1403 | * |
18d1226f |
1404 | * When encountering logical op &,|,^ |
1405 | * 1. parse the last value if any |
1406 | * 2. create a new tree |
1407 | * 3. add the expression (simple exp, or exp (subtree)) to the tree |
1408 | * 4. concatenate this tree with the current tree on top of the stack |
1409 | * When encountering math ops >,>=,<,<=,=,!= |
1410 | * 1. add to op to the simple expression |
1411 | * 2. concatenate last field component to field path |
1412 | * When encountering concatening ops . |
1413 | * 1. concatenate last field component to field path |
1414 | * When encountering opening parenthesis (,{,[ |
1415 | * 1. create a new subtree on top of tree stack |
1416 | * When encountering closing parenthesis ),},] |
1417 | * 1. add the expression on right child of the current tree |
1418 | * 2. the subtree is completed, allocate a new subtree |
1419 | * 3. pop the tree value from the tree stack |
1420 | */ |
be66ef34 |
1421 | |
1422 | #ifdef TEST |
1423 | struct timeval starttime; |
1424 | struct timeval endtime; |
1425 | gettimeofday(&starttime, NULL); |
1426 | #endif |
18d1226f |
1427 | |
571ef1ed |
1428 | expression_len = strlen(filter->expression); |
1429 | for(i=0;i<expression_len;i++) { |
5f185a2b |
1430 | // debug |
72911c5d |
1431 | // g_print("%c\n ",filter->expression[i]); |
d564d2ad |
1432 | if(nest_quotes) { |
1433 | switch(filter->expression[i]) { |
571ef1ed |
1434 | case '\\' : |
1435 | if(filter->expression[i+1] == '\"') { |
1436 | i++; |
1437 | } |
1438 | break; |
2db17921 |
1439 | case '\"': |
571ef1ed |
1440 | nest_quotes = 0; |
2db17921 |
1441 | i++; |
571ef1ed |
1442 | break; |
d564d2ad |
1443 | } |
1444 | if(a_string_spaces->len != 0) { |
1445 | a_field_component = g_string_append( |
1446 | a_field_component, a_string_spaces->str); |
2db17921 |
1447 | a_string_spaces = g_string_set_size(a_string_spaces, 0); |
d564d2ad |
1448 | } |
1449 | a_field_component = g_string_append_c(a_field_component, |
1450 | filter->expression[i]); |
1451 | continue; |
1452 | } |
1453 | |
5f185a2b |
1454 | switch(filter->expression[i]) { |
a4c292d4 |
1455 | /* |
1456 | * logical operators |
1457 | */ |
1458 | case '&': /* and */ |
72911c5d |
1459 | |
1460 | /* get current tree in tree stack */ |
5b729fcf |
1461 | t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
72911c5d |
1462 | |
1463 | /* get current node at absolute right */ |
9ab5ebd7 |
1464 | while(t1->right != LTTV_TREE_IDLE) { |
1465 | g_assert(t1->right == LTTV_TREE_NODE); |
1466 | t1 = t1->r_child.t; |
1467 | } |
18d1226f |
1468 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
1469 | t2->node = LTTV_LOGICAL_AND; |
72911c5d |
1470 | t1->right = LTTV_TREE_NODE; |
1471 | t1->r_child.t = t2; |
1472 | if(not) { /* add not operator to tree */ |
1473 | t3 = lttv_filter_tree_new(); |
1474 | t3->node = LTTV_LOGICAL_NOT; |
1475 | t2->left = LTTV_TREE_NODE; |
1476 | t2->l_child.t = t3; |
72911c5d |
1477 | t2 = t3; |
1478 | not = 0; |
72911c5d |
1479 | } |
bb87caa7 |
1480 | if(subtree != NULL) { /* append subtree to current tree */ |
18d1226f |
1481 | t2->left = LTTV_TREE_NODE; |
1482 | t2->l_child.t = subtree; |
f4e9dd16 |
1483 | subtree = NULL; |
bb87caa7 |
1484 | } else { /* append a simple expression */ |
9ab5ebd7 |
1485 | lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); |
18d1226f |
1486 | a_field_component = g_string_new(""); |
571ef1ed |
1487 | g_string_free(a_string_spaces, TRUE); |
1488 | a_string_spaces = g_string_new(""); |
18d1226f |
1489 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
1490 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
1491 | a_simple_expression = lttv_simple_expression_new(); |
f4e9dd16 |
1492 | } |
f4e9dd16 |
1493 | break; |
aa4600f3 |
1494 | |
a4c292d4 |
1495 | case '|': /* or */ |
aa4600f3 |
1496 | |
e00d6a24 |
1497 | t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
9ab5ebd7 |
1498 | while(t1->right != LTTV_TREE_IDLE) { |
1499 | g_assert(t1->right == LTTV_TREE_NODE); |
1500 | t1 = t1->r_child.t; |
1501 | } |
1601b365 |
1502 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
1503 | t2->node = LTTV_LOGICAL_OR; |
72911c5d |
1504 | t1->right = LTTV_TREE_NODE; |
1505 | t1->r_child.t = t2; |
1506 | if(not) { // add not operator to tree |
1507 | t3 = lttv_filter_tree_new(); |
1508 | t3->node = LTTV_LOGICAL_NOT; |
1509 | t2->left = LTTV_TREE_NODE; |
1510 | t2->l_child.t = t3; |
1511 | t2 = t3; |
1512 | not = 0; |
1513 | } |
1514 | if(subtree != NULL) { /* append subtree to current tree */ |
1601b365 |
1515 | t2->left = LTTV_TREE_NODE; |
1516 | t2->l_child.t = subtree; |
1517 | subtree = NULL; |
72911c5d |
1518 | } else { /* append a simple expression */ |
9ab5ebd7 |
1519 | lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); |
1601b365 |
1520 | a_field_component = g_string_new(""); |
571ef1ed |
1521 | g_string_free(a_string_spaces, TRUE); |
1522 | a_string_spaces = g_string_new(""); |
1601b365 |
1523 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
1524 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
1525 | a_simple_expression = lttv_simple_expression_new(); |
1601b365 |
1526 | } |
f4e9dd16 |
1527 | break; |
aa4600f3 |
1528 | |
a4c292d4 |
1529 | case '^': /* xor */ |
aa4600f3 |
1530 | |
e00d6a24 |
1531 | t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
9ab5ebd7 |
1532 | while(t1->right != LTTV_TREE_IDLE) { |
1533 | g_assert(t1->right == LTTV_TREE_NODE); |
1534 | t1 = t1->r_child.t; |
1535 | } |
1601b365 |
1536 | t2 = lttv_filter_tree_new(); |
0cdc2470 |
1537 | t2->node = LTTV_LOGICAL_XOR; |
72911c5d |
1538 | t1->right = LTTV_TREE_NODE; |
1539 | t1->r_child.t = t2; |
1540 | if(not) { // add not operator to tree |
1541 | t3 = lttv_filter_tree_new(); |
1542 | t3->node = LTTV_LOGICAL_NOT; |
1543 | t2->left = LTTV_TREE_NODE; |
1544 | t2->l_child.t = t3; |
1545 | t2 = t3; |
1546 | not = 0; |
1547 | } |
bb87caa7 |
1548 | if(subtree != NULL) { /* append subtree to current tree */ |
1601b365 |
1549 | t2->left = LTTV_TREE_NODE; |
1550 | t2->l_child.t = subtree; |
1551 | subtree = NULL; |
bb87caa7 |
1552 | } else { /* append a simple expression */ |
9ab5ebd7 |
1553 | lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); |
1601b365 |
1554 | a_field_component = g_string_new(""); |
571ef1ed |
1555 | g_string_free(a_string_spaces, TRUE); |
1556 | a_string_spaces = g_string_new(""); |
1601b365 |
1557 | t2->left = LTTV_TREE_LEAF; |
0cdc2470 |
1558 | t2->l_child.leaf = a_simple_expression; |
389ba50e |
1559 | a_simple_expression = lttv_simple_expression_new(); |
1601b365 |
1560 | } |
a4c292d4 |
1561 | break; |
aa4600f3 |
1562 | |
a4c292d4 |
1563 | case '!': /* not, or not equal (math op) */ |
aa4600f3 |
1564 | |
5f185a2b |
1565 | if(filter->expression[i+1] == '=') { /* != */ |
0cdc2470 |
1566 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
9ab5ebd7 |
1567 | lttv_simple_expression_assign_field(a_field_path,a_simple_expression); |
0cdc2470 |
1568 | a_field_component = g_string_new(""); |
571ef1ed |
1569 | g_string_free(a_string_spaces, TRUE); |
1570 | a_string_spaces = g_string_new(""); |
56e29124 |
1571 | lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_NE); |
aa4600f3 |
1572 | i++; |
a4c292d4 |
1573 | } else { /* ! */ |
72911c5d |
1574 | not=1; |
a4c292d4 |
1575 | } |
1576 | break; |
aa4600f3 |
1577 | |
a4c292d4 |
1578 | case '(': /* start of parenthesis */ |
91ad3f0a |
1579 | case '[': |
1580 | case '{': |
aa4600f3 |
1581 | |
91ad3f0a |
1582 | p_nesting++; /* incrementing parenthesis nesting value */ |
1601b365 |
1583 | t1 = lttv_filter_tree_new(); |
72911c5d |
1584 | if(not) { /* add not operator to tree */ |
1585 | t3 = lttv_filter_tree_new(); |
1586 | t3->node = LTTV_LOGICAL_NOT; |
1587 | t1->right = LTTV_TREE_NODE; |
1588 | t1->r_child.t = t3; |
1589 | not = 0; |
1590 | } |
1601b365 |
1591 | g_ptr_array_add( tree_stack,(gpointer) t1 ); |
a4c292d4 |
1592 | break; |
aa4600f3 |
1593 | |
a4c292d4 |
1594 | case ')': /* end of parenthesis */ |
91ad3f0a |
1595 | case ']': |
1596 | case '}': |
aa4600f3 |
1597 | |
91ad3f0a |
1598 | p_nesting--; /* decrementing parenthesis nesting value */ |
18d1226f |
1599 | if(p_nesting<0 || tree_stack->len<2) { |
f4e9dd16 |
1600 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
571ef1ed |
1601 | is not valid due to parenthesis incorrect use",filter->expression); |
5f185a2b |
1602 | return FALSE; |
f4e9dd16 |
1603 | } |
56e29124 |
1604 | |
1605 | /* there must at least be the root tree left in the array */ |
18d1226f |
1606 | g_assert(tree_stack->len>0); |
72911c5d |
1607 | |
1608 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
1609 | while(t1->right != LTTV_TREE_IDLE) { |
1610 | t1 = t1->r_child.t; |
1611 | } |
1612 | if(not) { // add not operator to tree |
1613 | g_print("ici"); |
1614 | t3 = lttv_filter_tree_new(); |
1615 | t3->node = LTTV_LOGICAL_NOT; |
1616 | t1->right = LTTV_TREE_NODE; |
1617 | t1->r_child.t = t3; |
1618 | t1 = t3; |
1619 | not = 0; |
1620 | } |
bb87caa7 |
1621 | if(subtree != NULL) { /* append subtree to current tree */ |
18d1226f |
1622 | t1->right = LTTV_TREE_NODE; |
1623 | t1->r_child.t = subtree; |
1624 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
1625 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
bb87caa7 |
1626 | } else { /* assign subtree as current tree */ |
9ab5ebd7 |
1627 | lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); |
18d1226f |
1628 | a_field_component = g_string_new(""); |
571ef1ed |
1629 | g_string_free(a_string_spaces, TRUE); |
1630 | a_string_spaces = g_string_new(""); |
18d1226f |
1631 | t1->right = LTTV_TREE_LEAF; |
0cdc2470 |
1632 | t1->r_child.leaf = a_simple_expression; |
389ba50e |
1633 | a_simple_expression = lttv_simple_expression_new(); |
9ab5ebd7 |
1634 | subtree = g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
18d1226f |
1635 | } |
a4c292d4 |
1636 | break; |
1637 | |
571ef1ed |
1638 | /* |
1639 | * mathematic operators |
a4c292d4 |
1640 | */ |
1641 | case '<': /* lower, lower or equal */ |
aa4600f3 |
1642 | |
1643 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
9ab5ebd7 |
1644 | lttv_simple_expression_assign_field(a_field_path,a_simple_expression); |
aa4600f3 |
1645 | a_field_component = g_string_new(""); |
571ef1ed |
1646 | g_string_free(a_string_spaces, TRUE); |
1647 | a_string_spaces = g_string_new(""); |
5f185a2b |
1648 | if(filter->expression[i+1] == '=') { /* <= */ |
a4c292d4 |
1649 | i++; |
56e29124 |
1650 | lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LE); |
1651 | } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_LT); |
aa4600f3 |
1652 | break; |
1653 | |
1654 | case '>': /* higher, higher or equal */ |
1655 | |
1656 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
9ab5ebd7 |
1657 | lttv_simple_expression_assign_field(a_field_path,a_simple_expression); |
f4e9dd16 |
1658 | a_field_component = g_string_new(""); |
571ef1ed |
1659 | g_string_free(a_string_spaces, TRUE); |
1660 | a_string_spaces = g_string_new(""); |
5f185a2b |
1661 | if(filter->expression[i+1] == '=') { /* >= */ |
a4c292d4 |
1662 | i++; |
56e29124 |
1663 | lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GE); |
1664 | } else lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_GT); |
aa4600f3 |
1665 | break; |
1666 | |
a4c292d4 |
1667 | case '=': /* equal */ |
aa4600f3 |
1668 | |
f4e9dd16 |
1669 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
9ab5ebd7 |
1670 | lttv_simple_expression_assign_field(a_field_path,a_simple_expression); |
f4e9dd16 |
1671 | a_field_component = g_string_new(""); |
571ef1ed |
1672 | g_string_free(a_string_spaces, TRUE); |
1673 | a_string_spaces = g_string_new(""); |
56e29124 |
1674 | lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_EQ); |
a4c292d4 |
1675 | break; |
aa4600f3 |
1676 | |
0769c82f |
1677 | /* |
1678 | * Field concatening caracter |
1679 | */ |
1680 | case '.': /* dot */ |
aa4600f3 |
1681 | |
bb87caa7 |
1682 | /* |
1683 | * divide field expression into elements |
1684 | * in a_field_path array. |
4d9ff942 |
1685 | * |
1686 | * A dot can also be present in double values |
bb87caa7 |
1687 | */ |
8ff6243c |
1688 | if(a_simple_expression->field == LTTV_FILTER_UNDEFINED) { |
bb87caa7 |
1689 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
1690 | a_field_component = g_string_new(""); |
571ef1ed |
1691 | g_string_free(a_string_spaces, TRUE); |
1692 | a_string_spaces = g_string_new(""); |
2db17921 |
1693 | } else { |
1694 | /* Operator found, we are in the value field */ |
1695 | g_string_append_c(a_field_component, filter->expression[i]); |
8ff6243c |
1696 | } |
1697 | break; |
ef2b07c1 |
1698 | case ' ': /* keep spaces that are within a field component */ |
2db17921 |
1699 | if(a_field_component->len == 0) break; /* ignore */ |
571ef1ed |
1700 | else |
1701 | a_string_spaces = g_string_append_c(a_string_spaces, |
1702 | filter->expression[i]); |
ef2b07c1 |
1703 | |
4d9ff942 |
1704 | case '\n': /* ignore */ |
0769c82f |
1705 | break; |
d564d2ad |
1706 | case '\"': |
1707 | nest_quotes?(nest_quotes=0):(nest_quotes=1); |
1708 | break; |
a4c292d4 |
1709 | default: /* concatening current string */ |
d564d2ad |
1710 | if(a_string_spaces->len != 0) { |
2db17921 |
1711 | a_field_component = g_string_append( |
4ec0c904 |
1712 | a_field_component, a_string_spaces->str); |
2db17921 |
1713 | a_string_spaces = g_string_set_size(a_string_spaces, 0); |
d564d2ad |
1714 | } |
1715 | a_field_component = g_string_append_c(a_field_component, |
1716 | filter->expression[i]); |
a4c292d4 |
1717 | } |
1718 | } |
1601b365 |
1719 | |
0cdc2470 |
1720 | /* |
1721 | * Preliminary check to see |
1722 | * if tree was constructed correctly |
1723 | */ |
1724 | if( p_nesting>0 ) { |
1725 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
571ef1ed |
1726 | is not valid due to parenthesis incorrect use",filter->expression); |
5f185a2b |
1727 | return FALSE; |
0cdc2470 |
1728 | } |
1729 | |
1730 | if(tree_stack->len != 1) /* only root tree should remain */ |
5f185a2b |
1731 | return FALSE; |
1601b365 |
1732 | |
72911c5d |
1733 | /* |
1734 | * processing last element of expression |
1735 | */ |
410c83da |
1736 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
9ab5ebd7 |
1737 | while(t1->right != LTTV_TREE_IDLE) { |
1738 | g_assert(t1->right == LTTV_TREE_NODE); |
1739 | t1 = t1->r_child.t; |
1740 | } |
72911c5d |
1741 | if(not) { // add not operator to tree |
1742 | t3 = lttv_filter_tree_new(); |
1743 | t3->node = LTTV_LOGICAL_NOT; |
1744 | t1->right = LTTV_TREE_NODE; |
1745 | t1->r_child.t = t3; |
1746 | t1 = t3; |
1747 | not = 0; |
1748 | } |
410c83da |
1749 | if(subtree != NULL) { /* add the subtree */ |
1750 | t1->right = LTTV_TREE_NODE; |
0cdc2470 |
1751 | t1->r_child.t = subtree; |
410c83da |
1752 | subtree = NULL; |
1753 | } else { /* add a leaf */ |
9ab5ebd7 |
1754 | lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); |
56e29124 |
1755 | a_field_component = NULL; |
571ef1ed |
1756 | g_string_free(a_string_spaces, TRUE); |
1757 | a_string_spaces = NULL; |
410c83da |
1758 | t1->right = LTTV_TREE_LEAF; |
0cdc2470 |
1759 | t1->r_child.leaf = a_simple_expression; |
56e29124 |
1760 | a_simple_expression = NULL; |
410c83da |
1761 | } |
1762 | |
56e29124 |
1763 | |
73050a5f |
1764 | /* free the pointer array */ |
1765 | g_assert(a_field_path->len == 0); |
1766 | g_ptr_array_free(a_field_path,TRUE); |
56e29124 |
1767 | |
1768 | /* free the tree stack -- but keep the root tree */ |
9354c7da |
1769 | filter->head = ltt_g_ptr_array_remove_index_slow(tree_stack,0); |
f3020899 |
1770 | g_ptr_array_free(tree_stack,TRUE); |
1771 | |
56e29124 |
1772 | /* free the field buffer if allocated */ |
1773 | if(a_field_component != NULL) g_string_free(a_field_component,TRUE); |
571ef1ed |
1774 | if(a_string_spaces != NULL) g_string_free(a_string_spaces, TRUE); |
ef2b07c1 |
1775 | |
56e29124 |
1776 | /* free the simple expression buffer if allocated */ |
1777 | if(a_simple_expression != NULL) lttv_simple_expression_destroy(a_simple_expression); |
73050a5f |
1778 | |
f3020899 |
1779 | g_assert(filter->head != NULL); /* tree should exist */ |
56e29124 |
1780 | g_assert(subtree == NULL); /* remaining subtree should be included in main tree */ |
be66ef34 |
1781 | |
1782 | #ifdef TEST |
1783 | gettimeofday(&endtime, NULL); |
1784 | |
1785 | /* Calcul du temps de l'algorithme */ |
1786 | double time1 = starttime.tv_sec + (starttime.tv_usec/1000000.0); |
1787 | double time2 = endtime.tv_sec + (endtime.tv_usec/1000000.0); |
1788 | // g_print("Tree build took %.10f ms for strlen of %i\n",(time2-time1)*1000,strlen(filter->expression)); |
1789 | g_print("%.10f %i\n",(time2-time1)*1000,strlen(filter->expression)); |
1790 | #endif |
a4c292d4 |
1791 | |
56e29124 |
1792 | /* debug */ |
c2a4581e |
1793 | g_debug("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n"); |
72911c5d |
1794 | lttv_print_tree(filter->head,0) ; |
c2a4581e |
1795 | g_debug("+++++++++++++++ END PRINT ++++++++++++++++++\n"); |
56e29124 |
1796 | |
1797 | /* success */ |
5f185a2b |
1798 | return TRUE; |
80f9611a |
1799 | |
31452f49 |
1800 | } |
1801 | |
8c89f5a8 |
1802 | /** |
56e29124 |
1803 | * @fn void lttv_filter_destroy(LttvFilter*) |
1804 | * |
8c89f5a8 |
1805 | * Destroy the current LttvFilter |
1806 | * @param filter pointer to the current LttvFilter |
1807 | */ |
1da1525d |
1808 | void |
2ea36caf |
1809 | lttv_filter_destroy(LttvFilter* filter) { |
5f185a2b |
1810 | |
571ef1ed |
1811 | if(!filter) return; |
ebcead4a |
1812 | |
571ef1ed |
1813 | if(filter->expression) |
1814 | g_free(filter->expression); |
1815 | if(filter->head) |
1816 | lttv_filter_tree_destroy(filter->head); |
5f185a2b |
1817 | g_free(filter); |
1818 | |
1da1525d |
1819 | } |
1820 | |
150f0d33 |
1821 | /** |
8ff6243c |
1822 | * @fn LttvFilterTree* lttv_filter_tree_new() |
56e29124 |
1823 | * |
150f0d33 |
1824 | * Assign a new tree for the current expression |
1825 | * or sub expression |
1826 | * @return pointer of LttvFilterTree |
1827 | */ |
5f185a2b |
1828 | LttvFilterTree* |
1829 | lttv_filter_tree_new() { |
150f0d33 |
1830 | LttvFilterTree* tree; |
1831 | |
e00d6a24 |
1832 | tree = g_new(LttvFilterTree,1); |
150f0d33 |
1833 | tree->node = 0; //g_new(lttv_expression,1); |
150f0d33 |
1834 | tree->left = LTTV_TREE_IDLE; |
1835 | tree->right = LTTV_TREE_IDLE; |
f3020899 |
1836 | tree->r_child.t = NULL; |
1837 | tree->l_child.t = NULL; |
1838 | |
150f0d33 |
1839 | return tree; |
1840 | } |
1841 | |
80f9611a |
1842 | /** |
56e29124 |
1843 | * @fn void lttv_filter_append_expression(LttvFilter*,char*) |
1844 | * |
80f9611a |
1845 | * Append a new expression to the expression |
1846 | * defined in the current filter |
1847 | * @param filter pointer to the current LttvFilter |
1848 | * @param expression string that must be appended |
56e29124 |
1849 | * @return Success/Failure of operation |
80f9611a |
1850 | */ |
be66ef34 |
1851 | gboolean |
1852 | lttv_filter_append_expression(LttvFilter* filter, const char *expression) { |
80f9611a |
1853 | |
56e29124 |
1854 | if(expression == NULL) return FALSE; |
da2e1bfb |
1855 | if(filter == NULL) return FALSE; |
0bc23ba9 |
1856 | if(expression[0] == '\0') return FALSE; /* Empty expression */ |
80f9611a |
1857 | |
da2e1bfb |
1858 | GString* s = g_string_new(""); |
1859 | if(filter->expression != NULL) { |
4ec0c904 |
1860 | s = g_string_append(s,filter->expression); |
1861 | s = g_string_append_c(s,'&'); |
da2e1bfb |
1862 | } |
4ec0c904 |
1863 | s = g_string_append(s,expression); |
786c5c3b |
1864 | |
1865 | g_free(filter->expression); |
da2e1bfb |
1866 | filter->expression = g_string_free(s,FALSE); |
1867 | |
1868 | /* TRUE if construction of tree proceeded without errors */ |
56e29124 |
1869 | return lttv_filter_update(filter); |
80f9611a |
1870 | |
1871 | } |
1872 | |
1873 | /** |
56e29124 |
1874 | * @fn void lttv_filter_clear_expression(LttvFilter*) |
1875 | * |
80f9611a |
1876 | * Clear the filter expression from the |
1877 | * current filter and sets its pointer to NULL |
1878 | * @param filter pointer to the current LttvFilter |
1879 | */ |
be66ef34 |
1880 | void |
1881 | lttv_filter_clear_expression(LttvFilter* filter) { |
80f9611a |
1882 | |
1883 | if(filter->expression != NULL) { |
1884 | g_free(filter->expression); |
1885 | filter->expression = NULL; |
1886 | } |
1887 | |
1888 | } |
1889 | |
150f0d33 |
1890 | /** |
56e29124 |
1891 | * @fn void lttv_filter_tree_destroy(LttvFilterTree*) |
1892 | * |
150f0d33 |
1893 | * Destroys the tree and his sub-trees |
1894 | * @param tree Tree which must be destroyed |
1895 | */ |
5f185a2b |
1896 | void |
1897 | lttv_filter_tree_destroy(LttvFilterTree* tree) { |
56e29124 |
1898 | |
150f0d33 |
1899 | if(tree == NULL) return; |
1900 | |
56e29124 |
1901 | if(tree->left == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->l_child.leaf); |
150f0d33 |
1902 | else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t); |
1903 | |
56e29124 |
1904 | if(tree->right == LTTV_TREE_LEAF) lttv_simple_expression_destroy(tree->r_child.leaf); |
150f0d33 |
1905 | else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t); |
1906 | |
e00d6a24 |
1907 | // g_free(tree->node); |
150f0d33 |
1908 | g_free(tree); |
1909 | } |
1910 | |
84a333d6 |
1911 | /** |
80f9611a |
1912 | * Global parsing function for the current |
1913 | * LttvFilterTree |
7e7af7f2 |
1914 | * @param t pointer to the current LttvFilterTree |
80f9611a |
1915 | * @param event current LttEvent, NULL if not used |
1916 | * @param tracefile current LttTracefile, NULL if not used |
1917 | * @param trace current LttTrace, NULL if not used |
1918 | * @param state current LttvProcessState, NULL if not used |
4d9ff942 |
1919 | * @param context current LttvTracefileContext, NULL if not used |
1920 | * @return response of filter |
84a333d6 |
1921 | */ |
31452f49 |
1922 | gboolean |
80f9611a |
1923 | lttv_filter_tree_parse( |
4d9ff942 |
1924 | const LttvFilterTree* t, |
1925 | const LttEvent* event, |
1926 | const LttTracefile* tracefile, |
1927 | const LttTrace* trace, |
b6ef18af |
1928 | const LttvTracefileContext* context, |
1929 | const LttvProcessState* state, |
1930 | const LttvTraceContext* tc |
80f9611a |
1931 | /*,...*/) |
1932 | { |
0769c82f |
1933 | |
80f9611a |
1934 | /* |
1601b365 |
1935 | * Each tree is parsed in inorder. |
1936 | * This way, it's possible to apply the left filter of the |
1937 | * tree, then decide whether or not the right branch should |
1938 | * be parsed depending on the linking logical operator |
1939 | * |
80f9611a |
1940 | * Each node consists in a |
1941 | * 1. logical operator |
1942 | * 2. left child ( node or simple expression ) |
1943 | * 3. right child ( node or simple expression ) |
1944 | * |
1945 | * When the child is a simple expression, we must |
1946 | * before all determine if the expression refers to |
1947 | * a structure which is whithin observation ( not NULL ). |
1948 | * -If so, the expression is evaluated. |
1949 | * -If not, the result is set to TRUE since this particular |
1950 | * operation does not interfere with the lttv structure |
1951 | * |
1952 | * The result of each simple expression will directly |
1953 | * affect the next branch. This way, depending on |
1954 | * the linking logical operator, the parser will decide |
1955 | * to explore or not the next branch. |
1956 | * 1. AND OPERATOR |
1957 | * -If result of left branch is 0 / FALSE |
1958 | * then don't explore right branch and return 0; |
1959 | * -If result of left branch is 1 / TRUE then explore |
1960 | * 2. OR OPERATOR |
1961 | * -If result of left branch is 1 / TRUE |
1962 | * then don't explore right branch and return 1; |
1963 | * -If result of left branch is 0 / FALSE then explore |
1964 | * 3. XOR OPERATOR |
56e29124 |
1965 | * -Result of left branch will not affect exploration of |
80f9611a |
1966 | * right branch |
1601b365 |
1967 | */ |
73050a5f |
1968 | |
80f9611a |
1969 | gboolean lresult = FALSE, rresult = FALSE; |
33e44b82 |
1970 | |
43ed82b5 |
1971 | LttvTraceState *ts = NULL; |
ae3d0f50 |
1972 | LttvTracefileState *tfs = (LttvTracefileState*)context; |
b6ef18af |
1973 | if(tc) |
1974 | ts = (LttvTraceState*)tc; |
1975 | else if(context) |
1976 | ts = (LttvTraceState*)context->t_context; |
1977 | |
1978 | if(tfs) { |
1979 | guint cpu = tfs->cpu; |
1980 | if(ts) |
1981 | state = ts->running_process[cpu]; |
1982 | } |
0769c82f |
1983 | |
80f9611a |
1984 | /* |
1985 | * Parse left branch |
1986 | */ |
4d9ff942 |
1987 | if(t->left == LTTV_TREE_NODE) { |
b6ef18af |
1988 | lresult = lttv_filter_tree_parse(t->l_child.t,event,tracefile,trace,context,NULL,NULL); |
4d9ff942 |
1989 | } |
5b729fcf |
1990 | else if(t->left == LTTV_TREE_LEAF) { |
4d9ff942 |
1991 | lresult = lttv_filter_tree_parse_branch(t->l_child.leaf,event,tracefile,trace,state,context); |
0cdc2470 |
1992 | } |
4d9ff942 |
1993 | |
80f9611a |
1994 | /* |
1995 | * Parse linking operator |
1996 | * make a cutoff if possible |
1997 | */ |
1998 | if((t->node & LTTV_LOGICAL_OR) && lresult == TRUE) return TRUE; |
1999 | if((t->node & LTTV_LOGICAL_AND) && lresult == FALSE) return FALSE; |
2000 | |
2001 | /* |
2002 | * Parse right branch |
2003 | */ |
4d9ff942 |
2004 | if(t->right == LTTV_TREE_NODE) { |
b6ef18af |
2005 | rresult = lttv_filter_tree_parse(t->r_child.t,event,tracefile,trace,context,NULL,NULL); |
4d9ff942 |
2006 | } |
5b729fcf |
2007 | else if(t->right == LTTV_TREE_LEAF) { |
4d9ff942 |
2008 | rresult = lttv_filter_tree_parse_branch(t->r_child.leaf,event,tracefile,trace,state,context); |
2009 | } |
2bdb97c5 |
2010 | |
2bdb97c5 |
2011 | |
4d9ff942 |
2012 | /* |
2013 | * Apply and return the |
2014 | * logical link between the |
2015 | * two operation |
2016 | */ |
2017 | switch(t->node) { |
2018 | case LTTV_LOGICAL_OR: return (lresult | rresult); |
2019 | case LTTV_LOGICAL_AND: return (lresult & rresult); |
72911c5d |
2020 | case LTTV_LOGICAL_NOT: |
2021 | return (t->left==LTTV_TREE_LEAF)?!lresult:((t->right==LTTV_TREE_LEAF)?!rresult:TRUE); |
4d9ff942 |
2022 | case LTTV_LOGICAL_XOR: return (lresult ^ rresult); |
2023 | case 0: return (rresult); |
2024 | default: |
2025 | /* |
2026 | * This case should never be |
2027 | * parsed, if so, this subtree |
2028 | * is cancelled ! |
2029 | */ |
2030 | return TRUE; |
2031 | } |
2032 | |
2033 | } |
2034 | |
2035 | /** |
4d9ff942 |
2036 | * This function parses a particular branch of the tree |
7e7af7f2 |
2037 | * @param se pointer to the current LttvSimpleExpression |
4d9ff942 |
2038 | * @param event current LttEvent, NULL if not used |
2039 | * @param tracefile current LttTracefile, NULL if not used |
2040 | * @param trace current LttTrace, NULL if not used |
2041 | * @param state current LttvProcessState, NULL if not used |
2042 | * @param context current LttvTracefileContext, NULL if not used |
2043 | * @return response of filter |
2044 | */ |
be66ef34 |
2045 | gboolean |
2046 | lttv_filter_tree_parse_branch( |
4d9ff942 |
2047 | const LttvSimpleExpression* se, |
2048 | const LttEvent* event, |
2049 | const LttTracefile* tracefile, |
2050 | const LttTrace* trace, |
2051 | const LttvProcessState* state, |
2052 | const LttvTracefileContext* context) { |
2053 | |
9ab5ebd7 |
2054 | LttvFieldValue v; |
4d9ff942 |
2055 | v = se->value; |
2056 | switch(se->field) { |
80f9611a |
2057 | case LTTV_FILTER_TRACE_NAME: |
4d9ff942 |
2058 | if(trace == NULL) return TRUE; |
c6832b57 |
2059 | else { |
eed2ef37 |
2060 | GQuark quark = ltt_trace_name(trace); |
c6832b57 |
2061 | return se->op((gpointer)&quark,v); |
2062 | } |
80f9611a |
2063 | break; |
2064 | case LTTV_FILTER_TRACEFILE_NAME: |
4d9ff942 |
2065 | if(tracefile == NULL) return TRUE; |
c6832b57 |
2066 | else { |
eed2ef37 |
2067 | GQuark quark = ltt_tracefile_name(tracefile); |
c6832b57 |
2068 | return se->op((gpointer)&quark,v); |
2069 | } |
80f9611a |
2070 | break; |
2071 | case LTTV_FILTER_STATE_PID: |
4d9ff942 |
2072 | if(state == NULL) return TRUE; |
2073 | else return se->op((gpointer)&state->pid,v); |
80f9611a |
2074 | break; |
2075 | case LTTV_FILTER_STATE_PPID: |
4d9ff942 |
2076 | if(state == NULL) return TRUE; |
2077 | else return se->op((gpointer)&state->ppid,v); |
80f9611a |
2078 | break; |
2079 | case LTTV_FILTER_STATE_CT: |
4d9ff942 |
2080 | if(state == NULL) return TRUE; |
80f9611a |
2081 | else { |
4d9ff942 |
2082 | return se->op((gpointer)&state->creation_time,v); |
80f9611a |
2083 | } |
2084 | break; |
2085 | case LTTV_FILTER_STATE_IT: |
4d9ff942 |
2086 | if(state == NULL) return TRUE; |
80f9611a |
2087 | else { |
4d9ff942 |
2088 | return se->op((gpointer)&state->insertion_time,v); |
80f9611a |
2089 | } |
2090 | break; |
2091 | case LTTV_FILTER_STATE_P_NAME: |
4d9ff942 |
2092 | if(state == NULL) return TRUE; |
46c40a93 |
2093 | else { |
bbd9d557 |
2094 | GQuark quark = state->name; |
c6832b57 |
2095 | return se->op((gpointer)&quark,v); |
46c40a93 |
2096 | } |
80f9611a |
2097 | break; |
d4dd4885 |
2098 | case LTTV_FILTER_STATE_T_BRAND: |
7b5f6cf1 |
2099 | if(state == NULL) return TRUE; |
2100 | else { |
2101 | GQuark quark = state->brand; |
2102 | return se->op((gpointer)&quark,v); |
2103 | } |
2104 | break; |
80f9611a |
2105 | case LTTV_FILTER_STATE_EX_MODE: |
4d9ff942 |
2106 | if(state == NULL) return TRUE; |
2107 | else return se->op((gpointer)&state->state->t,v); |
80f9611a |
2108 | break; |
2109 | case LTTV_FILTER_STATE_EX_SUBMODE: |
4d9ff942 |
2110 | if(state == NULL) return TRUE; |
2111 | else return se->op((gpointer)&state->state->n,v); |
80f9611a |
2112 | break; |
2113 | case LTTV_FILTER_STATE_P_STATUS: |
4d9ff942 |
2114 | if(state == NULL) return TRUE; |
2115 | else return se->op((gpointer)&state->state->s,v); |
80f9611a |
2116 | break; |
2117 | case LTTV_FILTER_STATE_CPU: |
e3162168 |
2118 | if(state == NULL) return TRUE; |
46c40a93 |
2119 | else { |
e3162168 |
2120 | return se->op((gpointer)&state->cpu,v); |
46c40a93 |
2121 | } |
80f9611a |
2122 | break; |
2123 | case LTTV_FILTER_EVENT_NAME: |
6471e71f |
2124 | if(event == NULL) return TRUE; |
2125 | else { |
2126 | struct marker_info *info; |
2127 | GQuark qtuple[2]; |
2128 | LttTracefile *tf = context->tf; |
2129 | qtuple[0] = ltt_tracefile_name(tracefile); |
2130 | info = marker_get_info_from_id(tf->mdata, event->event_id); |
2131 | g_assert(info != NULL); |
2132 | qtuple[1] = info->name; |
2133 | return se->op((gpointer)qtuple,v); |
2134 | } |
2135 | break; |
2136 | case LTTV_FILTER_EVENT_SUBNAME: |
4d9ff942 |
2137 | if(event == NULL) return TRUE; |
2138 | else { |
0ad10e7f |
2139 | struct marker_info *info; |
750eb11a |
2140 | LttTracefile *tf = context->tf; |
2141 | info = marker_get_info_from_id(tf->mdata, event->event_id); |
0ad10e7f |
2142 | g_assert(info != NULL); |
2143 | GQuark quark = info->name; |
ffd088ef |
2144 | return se->op((gpointer)&quark,v); |
2145 | } |
2146 | break; |
80f9611a |
2147 | case LTTV_FILTER_EVENT_CATEGORY: |
2148 | /* |
c6832b57 |
2149 | * TODO: Not yet implemented |
80f9611a |
2150 | */ |
4d9ff942 |
2151 | return TRUE; |
80f9611a |
2152 | break; |
2153 | case LTTV_FILTER_EVENT_TIME: |
4d9ff942 |
2154 | if(event == NULL) return TRUE; |
46c40a93 |
2155 | else { |
2156 | LttTime time = ltt_event_time(event); |
4d9ff942 |
2157 | return se->op((gpointer)&time,v); |
46c40a93 |
2158 | } |
80f9611a |
2159 | break; |
2160 | case LTTV_FILTER_EVENT_TSC: |
bbd9d557 |
2161 | if(event == NULL) return TRUE; |
2162 | else { |
2163 | LttCycleCount count = ltt_event_cycle_count(event); |
2164 | return se->op((gpointer)&count,v); |
2165 | } |
80f9611a |
2166 | break; |
33bdc8dd |
2167 | case LTTV_FILTER_EVENT_TARGET_PID: |
2168 | if(context == NULL) return TRUE; |
2169 | else { |
2170 | guint target_pid = |
2171 | lttv_state_get_target_pid((LttvTracefileState*)context); |
2172 | return se->op((gpointer)&target_pid,v); |
2173 | } |
2174 | break; |
80f9611a |
2175 | case LTTV_FILTER_EVENT_FIELD: |
2176 | /* |
2177 | * TODO: Use the offset to |
2178 | * find the dynamic field |
2179 | * in the event struct |
2180 | */ |
4d9ff942 |
2181 | return TRUE; |
80f9611a |
2182 | default: |
2183 | /* |
2184 | * This case should never be |
4d9ff942 |
2185 | * parsed, if so, the whole |
2186 | * filtering is cancelled |
80f9611a |
2187 | */ |
2188 | g_warning("Error while parsing the filter tree"); |
2189 | return TRUE; |
2190 | } |
4d9ff942 |
2191 | |
2192 | /* should never get here */ |
2193 | return TRUE; |
2194 | |
80f9611a |
2195 | } |
0769c82f |
2196 | |
4d9ff942 |
2197 | |
2198 | |
80f9611a |
2199 | /** |
7e7af7f2 |
2200 | * Debug function. Prints tree memory allocation. |
56e29124 |
2201 | * @param t the pointer to the current LttvFilterTree |
80f9611a |
2202 | */ |
2203 | void |
72911c5d |
2204 | lttv_print_tree(const LttvFilterTree* t, const int count) { |
0769c82f |
2205 | |
c2a4581e |
2206 | g_debug("node:%p lchild:%p rchild:%p depth:%i\n",t, //t->l_child.t,t->r_child.t); |
80f9611a |
2207 | (t->left==LTTV_TREE_NODE)?t->l_child.t:NULL, |
72911c5d |
2208 | (t->right==LTTV_TREE_NODE)?t->r_child.t:NULL, |
2209 | count); |
c2a4581e |
2210 | g_debug("logic operator: %s\n",(t->node&1)?"OR":((t->node&2)?"AND":((t->node&4)?"NOT":((t->node&8)?"XOR":"IDLE")))); |
2211 | g_debug("|-> left branch %p is a %s\n",t->l_child.t,(t->left==LTTV_TREE_NODE)?"NODE":((t->left==LTTV_TREE_LEAF)?"LEAF":"IDLE")); |
72911c5d |
2212 | if(t->left == LTTV_TREE_LEAF) { |
c2a4581e |
2213 | g_debug("| |-> field type number: %i\n",t->l_child.leaf->field); |
2214 | g_debug("| |-> offset is: %i\n",t->l_child.leaf->offset); |
2215 | g_debug("| |-> operator function is: %p\n",t->l_child.leaf->op); |
0769c82f |
2216 | } |
c2a4581e |
2217 | g_debug("|-> right branch %p is a %s\n",t->r_child.t,(t->right==LTTV_TREE_NODE)?"NODE":((t->right==LTTV_TREE_LEAF)?"LEAF":"IDLE")); |
72911c5d |
2218 | if(t->right == LTTV_TREE_LEAF) { |
c2a4581e |
2219 | g_debug("| |-> field type number: %i\n",t->r_child.leaf->field); |
2220 | g_debug("| |-> offset is: %i\n",t->r_child.leaf->offset); |
2221 | g_debug("| |-> operator function is: %p\n",t->r_child.leaf->op); |
80f9611a |
2222 | } |
72911c5d |
2223 | |
2224 | if(t->left == LTTV_TREE_NODE) lttv_print_tree(t->l_child.t,count+1); |
2225 | if(t->right == LTTV_TREE_NODE) lttv_print_tree(t->r_child.t,count+1); |
80f9611a |
2226 | } |
2227 | |
91ad3f0a |
2228 | /** |
56e29124 |
2229 | * @fn static void module_init() |
2230 | * |
91ad3f0a |
2231 | * Initializes the filter module and specific values |
2232 | */ |
1a7fa682 |
2233 | static void module_init() |
2234 | { |
91ad3f0a |
2235 | |
1a7fa682 |
2236 | } |
2237 | |
91ad3f0a |
2238 | /** |
7e7af7f2 |
2239 | * Destroys the filter module and specific values |
91ad3f0a |
2240 | */ |
1a7fa682 |
2241 | static void module_destroy() |
2242 | { |
56e29124 |
2243 | |
1a7fa682 |
2244 | } |
2245 | |
2246 | |
91ad3f0a |
2247 | LTTV_MODULE("filter", "Filters traceset and events", \ |
2248 | "Filters traceset and events specifically to user input", \ |
1a7fa682 |
2249 | module_init, module_destroy) |
2250 | |
2251 | |
2252 | |