| 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 | * Constructor for LttvSimpleExpression |
| 86 | * @return pointer to new LttvSimpleExpression |
| 87 | */ |
| 88 | LttvSimpleExpression* |
| 89 | lttv_simple_expression_new() { |
| 90 | |
| 91 | LttvSimpleExpression* se = g_new(LttvSimpleExpression,1); |
| 92 | |
| 93 | se->field = LTTV_FILTER_UNDEFINED; |
| 94 | se->offset = 0; |
| 95 | se->value = NULL; |
| 96 | |
| 97 | return se; |
| 98 | } |
| 99 | /** |
| 100 | * add a node to the current tree |
| 101 | * @param stack the tree stack |
| 102 | * @param subtree the subtree if available (pointer or NULL) |
| 103 | * @param op the logical operator that will form the node |
| 104 | */ |
| 105 | void |
| 106 | lttv_filter_tree_add_node(GPtrArray* stack, LttvFilterTree* subtree, LttvLogicalOp op) { |
| 107 | |
| 108 | LttvFilterTree* t1 = NULL; |
| 109 | LttvFilterTree* t2 = NULL; |
| 110 | |
| 111 | t1 = (LttvFilterTree*)g_ptr_array_index(stack,stack->len-1); |
| 112 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 113 | t2 = lttv_filter_tree_new(); |
| 114 | t2->node = op; |
| 115 | if(subtree != NULL) { |
| 116 | t2->left = LTTV_TREE_NODE; |
| 117 | t2->l_child.t = subtree; |
| 118 | subtree = NULL; |
| 119 | t1->right = LTTV_TREE_NODE; |
| 120 | t1->r_child.t = t2; |
| 121 | } else { |
| 122 | // a_simple_expression->value = a_field_component->str; |
| 123 | // a_field_component = g_string_new(""); |
| 124 | t2->left = LTTV_TREE_LEAF; |
| 125 | // t2->l_child.leaf = a_simple_expression; |
| 126 | // a_simple_expression = g_new(lttv_simple_expression,1); |
| 127 | t1->right = LTTV_TREE_NODE; |
| 128 | t1->r_child.t = t2; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Parse through filtering field hierarchy as specified |
| 135 | * by user. This function compares each value to |
| 136 | * predetermined quarks |
| 137 | * @param fp The field path list |
| 138 | * @return success/failure of operation |
| 139 | */ |
| 140 | gboolean |
| 141 | parse_field_path(GPtrArray* fp, LttvSimpleExpression* se) { |
| 142 | |
| 143 | GString* f = NULL; |
| 144 | if(fp->len < 2) return FALSE; |
| 145 | g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data; |
| 146 | |
| 147 | /* |
| 148 | * Parse through the specified |
| 149 | * hardcoded fields. |
| 150 | * |
| 151 | * Take note however that the |
| 152 | * 'event' subfields might change |
| 153 | * depending on values specified |
| 154 | * in core.xml file. Hence, if |
| 155 | * none of the subfields in the |
| 156 | * array match the hardcoded |
| 157 | * subfields, it will be considered |
| 158 | * as a dynamic field |
| 159 | */ |
| 160 | if(g_strcasecmp(f->str,"trace") ) { |
| 161 | /* |
| 162 | * Possible values: |
| 163 | * trace.name |
| 164 | */ |
| 165 | f=g_ptr_array_index(fp,1); |
| 166 | if(g_strcasecmp(f->str,"name")) { |
| 167 | se->field = LTTV_FILTER_TRACE_NAME; |
| 168 | } |
| 169 | else return FALSE; |
| 170 | } else if(g_strcasecmp(f->str,"traceset") ) { |
| 171 | /* |
| 172 | * FIXME: not yet implemented ! |
| 173 | */ |
| 174 | } else if(g_strcasecmp(f->str,"tracefile") ) { |
| 175 | /* |
| 176 | * Possible values: |
| 177 | * tracefile.name |
| 178 | */ |
| 179 | f=g_ptr_array_index(fp,1); |
| 180 | if(g_strcasecmp(f->str,"name")) { |
| 181 | se->field = LTTV_FILTER_TRACEFILE_NAME; |
| 182 | } |
| 183 | else return FALSE; |
| 184 | } else if(g_strcasecmp(f->str,"state") ) { |
| 185 | /* |
| 186 | * Possible values: |
| 187 | * state.pid |
| 188 | * state.ppid |
| 189 | * state.creation_time |
| 190 | * state.insertion_time |
| 191 | * state.process_name |
| 192 | * state.execution_mode |
| 193 | * state.execution_submode |
| 194 | * state.process_status |
| 195 | * state.cpu |
| 196 | */ |
| 197 | f=g_ptr_array_index(fp,1); |
| 198 | if(g_strcasecmp(f->str,"pid") ) { |
| 199 | se->field = LTTV_FILTER_STATE_PID; |
| 200 | } |
| 201 | else if(g_strcasecmp(f->str,"ppid") ) { |
| 202 | se->field = LTTV_FILTER_STATE_PPID; |
| 203 | } |
| 204 | else if(g_strcasecmp(f->str,"creation_time") ) { |
| 205 | se->field = LTTV_FILTER_STATE_CT; |
| 206 | } |
| 207 | else if(g_strcasecmp(f->str,"insertion_time") ) { |
| 208 | se->field = LTTV_FILTER_STATE_IT; |
| 209 | } |
| 210 | else if(g_strcasecmp(f->str,"process_name") ) { |
| 211 | se->field = LTTV_FILTER_STATE_P_NAME; |
| 212 | } |
| 213 | else if(g_strcasecmp(f->str,"execution_mode") ) { |
| 214 | se->field = LTTV_FILTER_STATE_EX_MODE; |
| 215 | } |
| 216 | else if(g_strcasecmp(f->str,"execution_submode") ) { |
| 217 | se->field = LTTV_FILTER_STATE_EX_SUBMODE; |
| 218 | } |
| 219 | else if(g_strcasecmp(f->str,"process_status") ) { |
| 220 | se->field = LTTV_FILTER_STATE_P_STATUS; |
| 221 | } |
| 222 | else if(g_strcasecmp(f->str,"cpu") ) { |
| 223 | se->field = LTTV_FILTER_STATE_CPU; |
| 224 | } |
| 225 | else return FALSE; |
| 226 | } else if(g_strcasecmp(f->str,"event") ) { |
| 227 | /* |
| 228 | * Possible values: |
| 229 | * event.name |
| 230 | * event.category |
| 231 | * event.time |
| 232 | * event.tsc |
| 233 | */ |
| 234 | f=g_ptr_array_index(fp,1); |
| 235 | if(g_strcasecmp(f->str,"name") ) { |
| 236 | se->field = LTTV_FILTER_EVENT_NAME; |
| 237 | } |
| 238 | else if(g_strcasecmp(f->str,"category") ) { |
| 239 | /* |
| 240 | * FIXME: Category not yet functional in lttv |
| 241 | */ |
| 242 | se->field = LTTV_FILTER_EVENT_CATEGORY; |
| 243 | } |
| 244 | else if(g_strcasecmp(f->str,"time") ) { |
| 245 | se->field = LTTV_FILTER_EVENT_TIME; |
| 246 | // offset = &((LttEvent*)NULL)->event_time); |
| 247 | } |
| 248 | else if(g_strcasecmp(f->str,"tsc") ) { |
| 249 | se->field = LTTV_FILTER_EVENT_TSC; |
| 250 | // offset = &((LttEvent*)NULL)->event_cycle_count); |
| 251 | } |
| 252 | else { /* core.xml specified options */ |
| 253 | se->field = LTTV_FILTER_EVENT_FIELD; |
| 254 | //se->offset = (...); |
| 255 | } |
| 256 | } else { |
| 257 | g_warning("Unrecognized field in filter string"); |
| 258 | return FALSE; |
| 259 | } |
| 260 | |
| 261 | return TRUE; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Add an filtering option to the current tree |
| 266 | * @param expression Current expression to parse |
| 267 | * @return success/failure of operation |
| 268 | */ |
| 269 | gboolean |
| 270 | parse_simple_expression(GString* expression) { |
| 271 | |
| 272 | unsigned i; |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Applies the 'equal' operator to the |
| 281 | * specified structure and value |
| 282 | * @param v1 left member of comparison |
| 283 | * @param v2 right member of comparison |
| 284 | * @return success/failure of operation |
| 285 | */ |
| 286 | gboolean lttv_apply_op_eq_uint64(guint64 v1, guint64 v2) {} |
| 287 | |
| 288 | /** |
| 289 | * Applies the 'equal' operator to the |
| 290 | * specified structure and value |
| 291 | * @param v1 left member of comparison |
| 292 | * @param v2 right member of comparison |
| 293 | * @return success/failure of operation |
| 294 | */ |
| 295 | gboolean lttv_apply_op_eq_uint32(guint32 v1, guint32 v2) {} |
| 296 | |
| 297 | /** |
| 298 | * Applies the 'equal' operator to the |
| 299 | * specified structure and value |
| 300 | * @param v1 left member of comparison |
| 301 | * @param v2 right member of comparison |
| 302 | * @return success/failure of operation |
| 303 | */ |
| 304 | gboolean lttv_apply_op_eq_uint16(guint16 v1, guint16 v2) {} |
| 305 | |
| 306 | /** |
| 307 | * Applies the 'equal' operator to the |
| 308 | * specified structure and value |
| 309 | * @param v1 left member of comparison |
| 310 | * @param v2 right member of comparison |
| 311 | * @return success/failure of operation |
| 312 | */ |
| 313 | gboolean lttv_apply_op_eq_double(double v1, double v2) {} |
| 314 | |
| 315 | /** |
| 316 | * Applies the 'equal' operator to the |
| 317 | * specified structure and value |
| 318 | * @param v1 left member of comparison |
| 319 | * @param v2 right member of comparison |
| 320 | * @return success/failure of operation |
| 321 | */ |
| 322 | gboolean lttv_apply_op_eq_string(char* v1, char* v2) {} |
| 323 | |
| 324 | /** |
| 325 | * Applies the 'not equal' operator to the |
| 326 | * specified structure and value |
| 327 | * @param v1 left member of comparison |
| 328 | * @param v2 right member of comparison |
| 329 | * @return success/failure of operation |
| 330 | */ |
| 331 | gboolean lttv_apply_op_ne_uint64(guint64 v1, guint64 v2) {} |
| 332 | |
| 333 | /** |
| 334 | * Applies the 'not equal' operator to the |
| 335 | * specified structure and value |
| 336 | * @param v1 left member of comparison |
| 337 | * @param v2 right member of comparison |
| 338 | * @return success/failure of operation |
| 339 | */ |
| 340 | gboolean lttv_apply_op_ne_uint32(guint32 v1, guint32 v2) {} |
| 341 | |
| 342 | /** |
| 343 | * Applies the 'not equal' operator to the |
| 344 | * specified structure and value |
| 345 | * @param v1 left member of comparison |
| 346 | * @param v2 right member of comparison |
| 347 | * @return success/failure of operation |
| 348 | */ |
| 349 | gboolean lttv_apply_op_ne_uint16(guint16 v1, guint16 v2) {} |
| 350 | |
| 351 | /** |
| 352 | * Applies the 'not equal' operator to the |
| 353 | * specified structure and value |
| 354 | * @param v1 left member of comparison |
| 355 | * @param v2 right member of comparison |
| 356 | * @return success/failure of operation |
| 357 | */ |
| 358 | gboolean lttv_apply_op_ne_double(double v1, double v2) {} |
| 359 | |
| 360 | /** |
| 361 | * Applies the 'not equal' operator to the |
| 362 | * specified structure and value |
| 363 | * @param v1 left member of comparison |
| 364 | * @param v2 right member of comparison |
| 365 | * @return success/failure of operation |
| 366 | */ |
| 367 | gboolean lttv_apply_op_ne_string(char* v1, char* v2) {} |
| 368 | |
| 369 | /** |
| 370 | * Applies the 'lower than' operator to the |
| 371 | * specified structure and value |
| 372 | * @param v1 left member of comparison |
| 373 | * @param v2 right member of comparison |
| 374 | * @return success/failure of operation |
| 375 | */ |
| 376 | gboolean lttv_apply_op_lt_uint64(guint64 v1, guint64 v2) {} |
| 377 | |
| 378 | /** |
| 379 | * Applies the 'lower than' operator to the |
| 380 | * specified structure and value |
| 381 | * @param v1 left member of comparison |
| 382 | * @param v2 right member of comparison |
| 383 | * @return success/failure of operation |
| 384 | */ |
| 385 | gboolean lttv_apply_op_lt_uint32(guint32 v1, guint32 v2) {} |
| 386 | |
| 387 | /** |
| 388 | * Applies the 'lower than' operator to the |
| 389 | * specified structure and value |
| 390 | * @param v1 left member of comparison |
| 391 | * @param v2 right member of comparison |
| 392 | * @return success/failure of operation |
| 393 | */ |
| 394 | gboolean lttv_apply_op_lt_uint16(guint16 v1, guint16 v2) {} |
| 395 | |
| 396 | /** |
| 397 | * Applies the 'lower than' operator to the |
| 398 | * specified structure and value |
| 399 | * @param v1 left member of comparison |
| 400 | * @param v2 right member of comparison |
| 401 | * @return success/failure of operation |
| 402 | */ |
| 403 | gboolean lttv_apply_op_lt_double(double v1, double v2) {} |
| 404 | |
| 405 | /** |
| 406 | * Applies the 'lower than' operator to the |
| 407 | * specified structure and value |
| 408 | * @param v1 left member of comparison |
| 409 | * @param v2 right member of comparison |
| 410 | * @return success/failure of operation |
| 411 | */ |
| 412 | gboolean lttv_apply_op_le_uint64(guint64 v1, guint64 v2) {} |
| 413 | |
| 414 | /** |
| 415 | * Applies the 'lower or equal' operator to the |
| 416 | * specified structure and value |
| 417 | * @param v1 left member of comparison |
| 418 | * @param v2 right member of comparison |
| 419 | * @return success/failure of operation |
| 420 | */ |
| 421 | gboolean lttv_apply_op_le_uint32(guint32 v1, guint32 v2) {} |
| 422 | |
| 423 | /** |
| 424 | * Applies the 'lower or equal' operator to the |
| 425 | * specified structure and value |
| 426 | * @param v1 left member of comparison |
| 427 | * @param v2 right member of comparison |
| 428 | * @return success/failure of operation |
| 429 | */ |
| 430 | gboolean lttv_apply_op_le_uint16(guint16 v1, guint16 v2) {} |
| 431 | |
| 432 | /** |
| 433 | * Applies the 'lower or equal' operator to the |
| 434 | * specified structure and value |
| 435 | * @param v1 left member of comparison |
| 436 | * @param v2 right member of comparison |
| 437 | * @return success/failure of operation |
| 438 | */ |
| 439 | gboolean lttv_apply_op_le_double(double v1, double v2) {} |
| 440 | |
| 441 | /** |
| 442 | * Applies the 'lower or equal' operator to the |
| 443 | * specified structure and value |
| 444 | * @param v1 left member of comparison |
| 445 | * @param v2 right member of comparison |
| 446 | * @return success/failure of operation |
| 447 | */ |
| 448 | gboolean lttv_apply_op_gt_uint64(guint64 v1, guint64 v2) {} |
| 449 | |
| 450 | /** |
| 451 | * Applies the 'greater than' operator to the |
| 452 | * specified structure and value |
| 453 | * @param v1 left member of comparison |
| 454 | * @param v2 right member of comparison |
| 455 | * @return success/failure of operation |
| 456 | */ |
| 457 | gboolean lttv_apply_op_gt_uint32(guint32 v1, guint32 v2) {} |
| 458 | |
| 459 | /** |
| 460 | * Applies the 'greater than' operator to the |
| 461 | * specified structure and value |
| 462 | * @param v1 left member of comparison |
| 463 | * @param v2 right member of comparison |
| 464 | * @return success/failure of operation |
| 465 | */ |
| 466 | gboolean lttv_apply_op_gt_uint16(guint16 v1, guint16 v2) {} |
| 467 | |
| 468 | /** |
| 469 | * Applies the 'greater than' operator to the |
| 470 | * specified structure and value |
| 471 | * @param v1 left member of comparison |
| 472 | * @param v2 right member of comparison |
| 473 | * @return success/failure of operation |
| 474 | */ |
| 475 | gboolean lttv_apply_op_gt_double(double v1, double v2) {} |
| 476 | |
| 477 | /** |
| 478 | * Applies the 'greater than' operator to the |
| 479 | * specified structure and value |
| 480 | * @param v1 left member of comparison |
| 481 | * @param v2 right member of comparison |
| 482 | * @return success/failure of operation |
| 483 | */ |
| 484 | gboolean lttv_apply_op_ge_uint64(guint64 v1, guint64 v2) {} |
| 485 | |
| 486 | /** |
| 487 | * Applies the 'greater or equal' operator to the |
| 488 | * specified structure and value |
| 489 | * @param v1 left member of comparison |
| 490 | * @param v2 right member of comparison |
| 491 | * @return success/failure of operation |
| 492 | */ |
| 493 | gboolean lttv_apply_op_ge_uint32(guint32 v1, guint32 v2) {} |
| 494 | |
| 495 | /** |
| 496 | * Applies the 'greater or equal' operator to the |
| 497 | * specified structure and value |
| 498 | * @param v1 left member of comparison |
| 499 | * @param v2 right member of comparison |
| 500 | * @return success/failure of operation |
| 501 | */ |
| 502 | gboolean lttv_apply_op_ge_uint16(guint16 v1, guint16 v2) {} |
| 503 | |
| 504 | /** |
| 505 | * Applies the 'greater or equal' operator to the |
| 506 | * specified structure and value |
| 507 | * @param v1 left member of comparison |
| 508 | * @param v2 right member of comparison |
| 509 | * @return success/failure of operation |
| 510 | */ |
| 511 | gboolean lttv_apply_op_ge_double(double v1, double v2) {} |
| 512 | |
| 513 | |
| 514 | /** |
| 515 | * Makes a copy of the current filter tree |
| 516 | * @param tree pointer to the current tree |
| 517 | * @return new copy of the filter tree |
| 518 | */ |
| 519 | LttvFilterTree* |
| 520 | lttv_filter_tree_clone(LttvFilterTree* tree) { |
| 521 | |
| 522 | |
| 523 | |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Makes a copy of the current filter |
| 528 | * @param filter pointer to the current filter |
| 529 | * @return new copy of the filter |
| 530 | */ |
| 531 | LttvFilter* |
| 532 | lttv_filter_clone(LttvFilter* filter) { |
| 533 | |
| 534 | |
| 535 | LttvFilter* newfilter = g_new(LttvFilter,1); |
| 536 | |
| 537 | // newfilter->expression = g_new(char,1) |
| 538 | strcpy(newfilter->expression,filter->expression); |
| 539 | |
| 540 | newfilter->head = lttv_filter_tree_clone(filter->head); |
| 541 | |
| 542 | return newfilter; |
| 543 | |
| 544 | } |
| 545 | |
| 546 | |
| 547 | /** |
| 548 | * Creates a new lttv_filter |
| 549 | * @param expression filtering options string |
| 550 | * @param t pointer to the current LttvTrace |
| 551 | * @return the current lttv_filter or NULL if error |
| 552 | */ |
| 553 | LttvFilter* |
| 554 | lttv_filter_new(char *expression, LttvTraceState *tcs) { |
| 555 | |
| 556 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
| 557 | |
| 558 | unsigned |
| 559 | i, |
| 560 | p_nesting=0, /* parenthesis nesting value */ |
| 561 | b=0; /* current breakpoint in expression string */ |
| 562 | |
| 563 | /* trees */ |
| 564 | LttvFilterTree |
| 565 | *tree = lttv_filter_tree_new(), /* main tree */ |
| 566 | *subtree = NULL, /* buffer for subtrees */ |
| 567 | *t1, /* buffer #1 */ |
| 568 | *t2; /* buffer #2 */ |
| 569 | |
| 570 | /* |
| 571 | * Tree Stack |
| 572 | * each element of the list |
| 573 | * is a sub tree created |
| 574 | * by the use of parenthesis in the |
| 575 | * global expression. The final tree |
| 576 | * will be the one left at the root of |
| 577 | * the list |
| 578 | */ |
| 579 | GPtrArray *tree_stack = g_ptr_array_new(); |
| 580 | g_ptr_array_add( tree_stack,(gpointer) tree ); |
| 581 | |
| 582 | /* temporary values */ |
| 583 | GString *a_field_component = g_string_new(""); |
| 584 | GPtrArray *a_field_path = NULL; |
| 585 | |
| 586 | LttvSimpleExpression* a_simple_expression = lttv_simple_expression_new(); |
| 587 | |
| 588 | /* |
| 589 | * Parse entire expression and construct |
| 590 | * the binary tree. There are two steps |
| 591 | * in browsing that string |
| 592 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
| 593 | * 2. finding simple expressions |
| 594 | * - field path ( separated by dots ) |
| 595 | * - op ( >, <, =, >=, <=, !=) |
| 596 | * - value ( integer, string ... ) |
| 597 | * To spare computing time, the whole |
| 598 | * string is parsed in this loop for a |
| 599 | * O(n) complexity order. |
| 600 | * |
| 601 | * When encountering logical op &,|,^ |
| 602 | * 1. parse the last value if any |
| 603 | * 2. create a new tree |
| 604 | * 3. add the expression (simple exp, or exp (subtree)) to the tree |
| 605 | * 4. concatenate this tree with the current tree on top of the stack |
| 606 | * When encountering math ops >,>=,<,<=,=,!= |
| 607 | * 1. add to op to the simple expression |
| 608 | * 2. concatenate last field component to field path |
| 609 | * When encountering concatening ops . |
| 610 | * 1. concatenate last field component to field path |
| 611 | * When encountering opening parenthesis (,{,[ |
| 612 | * 1. create a new subtree on top of tree stack |
| 613 | * When encountering closing parenthesis ),},] |
| 614 | * 1. add the expression on right child of the current tree |
| 615 | * 2. the subtree is completed, allocate a new subtree |
| 616 | * 3. pop the tree value from the tree stack |
| 617 | */ |
| 618 | |
| 619 | a_field_path = g_ptr_array_new(); |
| 620 | g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */ |
| 621 | |
| 622 | |
| 623 | for(i=0;i<strlen(expression);i++) { |
| 624 | // g_print("%s\n",a_field_component->str); |
| 625 | g_print("%c ",expression[i]); |
| 626 | // g_print("switch:%c -->subtree:%p\n",expression[i],subtree); |
| 627 | switch(expression[i]) { |
| 628 | /* |
| 629 | * logical operators |
| 630 | */ |
| 631 | case '&': /* and */ |
| 632 | t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 633 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 634 | t2 = lttv_filter_tree_new(); |
| 635 | t2->node = LTTV_LOGICAL_AND; |
| 636 | if(subtree != NULL) { |
| 637 | t2->left = LTTV_TREE_NODE; |
| 638 | t2->l_child.t = subtree; |
| 639 | subtree = NULL; |
| 640 | t1->right = LTTV_TREE_NODE; |
| 641 | t1->r_child.t = t2; |
| 642 | } else { |
| 643 | a_simple_expression->value = a_field_component->str; |
| 644 | a_field_component = g_string_new(""); |
| 645 | t2->left = LTTV_TREE_LEAF; |
| 646 | t2->l_child.leaf = a_simple_expression; |
| 647 | a_simple_expression = lttv_simple_expression_new(); |
| 648 | t1->right = LTTV_TREE_NODE; |
| 649 | t1->r_child.t = t2; |
| 650 | } |
| 651 | |
| 652 | break; |
| 653 | case '|': /* or */ |
| 654 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 655 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 656 | t2 = lttv_filter_tree_new(); |
| 657 | t2->node = LTTV_LOGICAL_OR; |
| 658 | if(subtree != NULL) { |
| 659 | t2->left = LTTV_TREE_NODE; |
| 660 | t2->l_child.t = subtree; |
| 661 | subtree = NULL; |
| 662 | t1->right = LTTV_TREE_NODE; |
| 663 | t1->r_child.t = t2; |
| 664 | } else { |
| 665 | a_simple_expression->value = a_field_component->str; |
| 666 | a_field_component = g_string_new(""); |
| 667 | t2->left = LTTV_TREE_LEAF; |
| 668 | t2->l_child.leaf = a_simple_expression; |
| 669 | a_simple_expression = lttv_simple_expression_new(); |
| 670 | t1->right = LTTV_TREE_NODE; |
| 671 | t1->r_child.t = t2; |
| 672 | } |
| 673 | break; |
| 674 | case '^': /* xor */ |
| 675 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 676 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 677 | t2 = lttv_filter_tree_new(); |
| 678 | t2->node = LTTV_LOGICAL_XOR; |
| 679 | if(subtree != NULL) { |
| 680 | t2->left = LTTV_TREE_NODE; |
| 681 | t2->l_child.t = subtree; |
| 682 | subtree = NULL; |
| 683 | t1->right = LTTV_TREE_NODE; |
| 684 | t1->r_child.t = t2; |
| 685 | } else { |
| 686 | a_simple_expression->value = a_field_component->str; |
| 687 | a_field_component = g_string_new(""); |
| 688 | t2->left = LTTV_TREE_LEAF; |
| 689 | t2->l_child.leaf = a_simple_expression; |
| 690 | a_simple_expression = lttv_simple_expression_new(); |
| 691 | t1->right = LTTV_TREE_NODE; |
| 692 | t1->r_child.t = t2; |
| 693 | } |
| 694 | break; |
| 695 | case '!': /* not, or not equal (math op) */ |
| 696 | if(expression[i+1] == '=') { /* != */ |
| 697 | a_simple_expression->op = LTTV_FIELD_NE; |
| 698 | i++; |
| 699 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 700 | parse_field_path(a_field_path,a_simple_expression); |
| 701 | a_field_component = g_string_new(""); |
| 702 | } else { /* ! */ |
| 703 | // g_print("%s\n",a_field_component); |
| 704 | // a_field_component = g_string_new(""); |
| 705 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 706 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 707 | t2 = lttv_filter_tree_new(); |
| 708 | t2->node = LTTV_LOGICAL_NOT; |
| 709 | t1->right = LTTV_TREE_NODE; |
| 710 | t1->r_child.t = t2; |
| 711 | } |
| 712 | break; |
| 713 | case '(': /* start of parenthesis */ |
| 714 | case '[': |
| 715 | case '{': |
| 716 | p_nesting++; /* incrementing parenthesis nesting value */ |
| 717 | t1 = lttv_filter_tree_new(); |
| 718 | g_ptr_array_add( tree_stack,(gpointer) t1 ); |
| 719 | break; |
| 720 | case ')': /* end of parenthesis */ |
| 721 | case ']': |
| 722 | case '}': |
| 723 | p_nesting--; /* decrementing parenthesis nesting value */ |
| 724 | if(p_nesting<0 || tree_stack->len<2) { |
| 725 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 726 | is not valid due to parenthesis incorrect use",expression); |
| 727 | return NULL; |
| 728 | } |
| 729 | |
| 730 | g_assert(tree_stack->len>0); |
| 731 | if(subtree != NULL) { |
| 732 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 733 | while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) { |
| 734 | g_assert(t1!=NULL && t1->r_child.t != NULL); |
| 735 | t1 = t1->r_child.t; |
| 736 | } |
| 737 | t1->right = LTTV_TREE_NODE; |
| 738 | t1->r_child.t = subtree; |
| 739 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 740 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
| 741 | } else { |
| 742 | a_simple_expression->value = a_field_component->str; |
| 743 | a_field_component = g_string_new(""); |
| 744 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 745 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 746 | t1->right = LTTV_TREE_LEAF; |
| 747 | t1->r_child.leaf = a_simple_expression; |
| 748 | a_simple_expression = lttv_simple_expression_new(); |
| 749 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 750 | g_assert(subtree != NULL); |
| 751 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
| 752 | } |
| 753 | break; |
| 754 | |
| 755 | /* |
| 756 | * mathematic operators |
| 757 | */ |
| 758 | case '<': /* lower, lower or equal */ |
| 759 | if(expression[i+1] == '=') { /* <= */ |
| 760 | i++; |
| 761 | a_simple_expression->op = LTTV_FIELD_LE; |
| 762 | } else a_simple_expression->op = LTTV_FIELD_LT; |
| 763 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 764 | parse_field_path(a_field_path,a_simple_expression); |
| 765 | a_field_component = g_string_new(""); |
| 766 | break; |
| 767 | case '>': /* higher, higher or equal */ |
| 768 | if(expression[i+1] == '=') { /* >= */ |
| 769 | i++; |
| 770 | a_simple_expression->op = LTTV_FIELD_GE; |
| 771 | } else a_simple_expression->op = LTTV_FIELD_GT; |
| 772 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 773 | parse_field_path(a_field_path,a_simple_expression); |
| 774 | a_field_component = g_string_new(""); |
| 775 | break; |
| 776 | case '=': /* equal */ |
| 777 | a_simple_expression->op = LTTV_FIELD_EQ; |
| 778 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 779 | parse_field_path(a_field_path,a_simple_expression); |
| 780 | a_field_component = g_string_new(""); |
| 781 | break; |
| 782 | /* |
| 783 | * Field concatening caracter |
| 784 | */ |
| 785 | case '.': /* dot */ |
| 786 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 787 | a_field_component = g_string_new(""); |
| 788 | break; |
| 789 | default: /* concatening current string */ |
| 790 | g_string_append_c(a_field_component,expression[i]); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2); |
| 795 | g_print("stack size: %i\n",tree_stack->len); |
| 796 | |
| 797 | /* |
| 798 | * Preliminary check to see |
| 799 | * if tree was constructed correctly |
| 800 | */ |
| 801 | if( p_nesting>0 ) { |
| 802 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 803 | is not valid due to parenthesis incorrect use",expression); |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | if(tree_stack->len != 1) /* only root tree should remain */ |
| 808 | return NULL; |
| 809 | |
| 810 | /* processing last element of expression */ |
| 811 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 812 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 813 | if(subtree != NULL) { /* add the subtree */ |
| 814 | t1->right = LTTV_TREE_NODE; |
| 815 | t1->r_child.t = subtree; |
| 816 | subtree = NULL; |
| 817 | } else { /* add a leaf */ |
| 818 | a_simple_expression->value = a_field_component->str; |
| 819 | a_field_component = g_string_new(""); |
| 820 | t1->right = LTTV_TREE_LEAF; |
| 821 | t1->r_child.leaf = a_simple_expression; |
| 822 | /* |
| 823 | * FIXME: is it really necessary to reallocate |
| 824 | * LttvSimpleExpression at this point ?? |
| 825 | */ |
| 826 | a_simple_expression = lttv_simple_expression_new(); |
| 827 | } |
| 828 | |
| 829 | g_assert(tree != NULL); |
| 830 | g_assert(subtree == NULL); |
| 831 | |
| 832 | lttv_filter_tracefile(tree,NULL); |
| 833 | |
| 834 | return tree; |
| 835 | |
| 836 | } |
| 837 | |
| 838 | void |
| 839 | lttv_filter_destroy(LttvFilter* filter) { |
| 840 | |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Assign a new tree for the current expression |
| 845 | * or sub expression |
| 846 | * @return pointer of LttvFilterTree |
| 847 | */ |
| 848 | LttvFilterTree* lttv_filter_tree_new() { |
| 849 | LttvFilterTree* tree; |
| 850 | |
| 851 | tree = g_new(LttvFilter,1); |
| 852 | tree->node = 0; //g_new(lttv_expression,1); |
| 853 | // tree->node->type = LTTV_UNDEFINED_EXPRESSION; |
| 854 | tree->left = LTTV_TREE_IDLE; |
| 855 | tree->right = LTTV_TREE_IDLE; |
| 856 | |
| 857 | return tree; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Destroys the tree and his sub-trees |
| 862 | * @param tree Tree which must be destroyed |
| 863 | */ |
| 864 | void lttv_filter_tree_destroy(LttvFilterTree* tree) { |
| 865 | |
| 866 | if(tree == NULL) return; |
| 867 | |
| 868 | if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf); |
| 869 | else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t); |
| 870 | |
| 871 | if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf); |
| 872 | else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t); |
| 873 | |
| 874 | g_free(tree->node); |
| 875 | g_free(tree); |
| 876 | } |
| 877 | |
| 878 | |
| 879 | /** |
| 880 | * Apply the filter to a specific trace |
| 881 | * @param filter the current filter applied |
| 882 | * @param tracefile the trace to apply the filter to |
| 883 | * @return success/failure of operation |
| 884 | */ |
| 885 | gboolean |
| 886 | lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) { |
| 887 | |
| 888 | LttvFilterTree* t = filter->head; |
| 889 | |
| 890 | /* |
| 891 | * Each tree is parsed in inorder. |
| 892 | * This way, it's possible to apply the left filter of the |
| 893 | * tree, then decide whether or not the right branch should |
| 894 | * be parsed depending on the linking logical operator |
| 895 | * |
| 896 | * As for the filtering structure, since we are trying |
| 897 | * to remove elements from the trace, it might be better |
| 898 | * managing an array of all items to be removed .. |
| 899 | */ |
| 900 | |
| 901 | g_print("node:%p lchild:%p rchild:%p\n",t,t->l_child.t,t->r_child.t); |
| 902 | g_print("node type%i\n",t->node); |
| 903 | if(t->left == LTTV_TREE_NODE) lttv_filter_tracefile(t->l_child.t,NULL); |
| 904 | else if(t->left == LTTV_TREE_LEAF) { |
| 905 | g_assert(t->l_child.leaf->value != NULL); |
| 906 | g_print("%p: left is qqch %i %s\n",t,t->l_child.leaf->op,t->l_child.leaf->value); |
| 907 | } |
| 908 | if(t->right == LTTV_TREE_NODE) lttv_filter_tracefile(t->r_child.t,NULL); |
| 909 | else if(t->right == LTTV_TREE_LEAF) { |
| 910 | g_assert(t->r_child.leaf->value != NULL); |
| 911 | g_print("%p: right is qqch %i %s\n",t,t->r_child.leaf->op,t->r_child.leaf->value); |
| 912 | } |
| 913 | |
| 914 | /* test */ |
| 915 | /* int i, nb; |
| 916 | char *f_name, *e_name; |
| 917 | |
| 918 | char* field = "cpu"; |
| 919 | |
| 920 | LttvTraceHook h; |
| 921 | |
| 922 | LttEventType *et; |
| 923 | |
| 924 | LttType *t; |
| 925 | |
| 926 | GString *fe_name = g_string_new(""); |
| 927 | |
| 928 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
| 929 | g_print("NB:%i\n",nb); |
| 930 | for(i = 0 ; i < nb ; i++) { |
| 931 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
| 932 | e_name = ltt_eventtype_name(et); |
| 933 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
| 934 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
| 935 | g_print("facility:%s and event:%s\n",f_name,e_name); |
| 936 | } |
| 937 | */ |
| 938 | } |
| 939 | |
| 940 | gboolean |
| 941 | lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) { |
| 942 | |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * Apply the filter to a specific event |
| 947 | * @param filter the current filter applied |
| 948 | * @param event the event to apply the filter to |
| 949 | * @return success/failure of operation |
| 950 | */ |
| 951 | gboolean |
| 952 | lttv_filter_event(LttvFilter *filter, LttEvent *event) { |
| 953 | |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Initializes the filter module and specific values |
| 958 | */ |
| 959 | static void module_init() |
| 960 | { |
| 961 | |
| 962 | /* |
| 963 | * Quarks initialization |
| 964 | * for hardcoded filtering options |
| 965 | * |
| 966 | * TODO: traceset has no yet been defined |
| 967 | */ |
| 968 | |
| 969 | /* top fields */ |
| 970 | // LTTV_FILTER_EVENT = g_quark_from_string("event"); |
| 971 | // LTTV_FILTER_TRACE = g_quark_from_string("trace"); |
| 972 | // LTTV_FILTER_TRACESET = g_quark_from_string("traceset"); |
| 973 | // LTTV_FILTER_STATE = g_quark_from_string("state"); |
| 974 | // LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile"); |
| 975 | |
| 976 | /* event.name, tracefile.name, trace.name */ |
| 977 | // LTTV_FILTER_NAME = g_quark_from_string("name"); |
| 978 | |
| 979 | /* event sub fields */ |
| 980 | // LTTV_FILTER_CATEGORY = g_quark_from_string("category"); |
| 981 | // LTTV_FILTER_TIME = g_quark_from_string("time"); |
| 982 | // LTTV_FILTER_TSC = g_quark_from_string("tsc"); |
| 983 | |
| 984 | /* state sub fields */ |
| 985 | // LTTV_FILTER_PID = g_quark_from_string("pid"); |
| 986 | // LTTV_FILTER_PPID = g_quark_from_string("ppid"); |
| 987 | // LTTV_FILTER_C_TIME = g_quark_from_string("creation_time"); |
| 988 | // LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time"); |
| 989 | // LTTV_FILTER_P_NAME = g_quark_from_string("process_name"); |
| 990 | // LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode"); |
| 991 | // LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode"); |
| 992 | // LTTV_FILTER_P_STATUS = g_quark_from_string("process_status"); |
| 993 | // LTTV_FILTER_CPU = g_quark_from_string("cpu"); |
| 994 | |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * Destroys the filter module and specific values |
| 999 | */ |
| 1000 | static void module_destroy() |
| 1001 | { |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | LTTV_MODULE("filter", "Filters traceset and events", \ |
| 1006 | "Filters traceset and events specifically to user input", \ |
| 1007 | module_init, module_destroy) |
| 1008 | |
| 1009 | |
| 1010 | |