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