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