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