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