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