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