| 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, LttvFilter* subtree, LttvLogicalOp op) { |
| 95 | |
| 96 | LttvFilter* t1 = NULL; |
| 97 | LttvFilter* t2 = NULL; |
| 98 | |
| 99 | t1 = (LttvFilter*)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) { |
| 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 | if(g_quark_try_string(f->str) == LTTV_FILTER_EVENT) { |
| 136 | f=g_ptr_array_index(fp,1); |
| 137 | if(g_quark_try_string(f->str) == LTTV_FILTER_NAME) {} |
| 138 | else if(g_quark_try_string(f->str) == LTTV_FILTER_CATEGORY) {} |
| 139 | else if(g_quark_try_string(f->str) == LTTV_FILTER_TIME) { |
| 140 | // offset = &((LttEvent*)NULL)->event_time); |
| 141 | } |
| 142 | else if(g_quark_try_string(f->str) == LTTV_FILTER_TSC) { |
| 143 | // offset = &((LttEvent*)NULL)->event_cycle_count); |
| 144 | } |
| 145 | else { /* core.xml specified options */ |
| 146 | |
| 147 | } |
| 148 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACEFILE) { |
| 149 | f=g_ptr_array_index(fp,1); |
| 150 | if(g_quark_try_string(f->str) == LTTV_FILTER_NAME) {} |
| 151 | else return FALSE; |
| 152 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACE) { |
| 153 | f=g_ptr_array_index(fp,1); |
| 154 | if(g_quark_try_string(f->str) == LTTV_FILTER_NAME) {} |
| 155 | else return FALSE; |
| 156 | |
| 157 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_STATE) { |
| 158 | f=g_ptr_array_index(fp,1); |
| 159 | if(g_quark_try_string(f->str) == LTTV_FILTER_PID) {} |
| 160 | else if(g_quark_try_string(f->str) == LTTV_FILTER_PPID) {} |
| 161 | else if(g_quark_try_string(f->str) == LTTV_FILTER_C_TIME) {} |
| 162 | else if(g_quark_try_string(f->str) == LTTV_FILTER_I_TIME) {} |
| 163 | else if(g_quark_try_string(f->str) == LTTV_FILTER_P_NAME) {} |
| 164 | else if(g_quark_try_string(f->str) == LTTV_FILTER_EX_MODE) {} |
| 165 | else if(g_quark_try_string(f->str) == LTTV_FILTER_EX_SUBMODE) {} |
| 166 | else if(g_quark_try_string(f->str) == LTTV_FILTER_P_STATUS) {} |
| 167 | else if(g_quark_try_string(f->str) == LTTV_FILTER_CPU) {} |
| 168 | else return FALSE; |
| 169 | |
| 170 | } else { |
| 171 | g_warning("Unrecognized field in filter string"); |
| 172 | return FALSE; |
| 173 | } |
| 174 | return TRUE; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add an filtering option to the current tree |
| 179 | * @param expression Current expression to parse |
| 180 | * @return success/failure of operation |
| 181 | */ |
| 182 | gboolean |
| 183 | parse_simple_expression(GString* expression) { |
| 184 | |
| 185 | unsigned i; |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Applies the 'equal' operator to the |
| 194 | * specified structure and value |
| 195 | * @return success/failure of operation |
| 196 | */ |
| 197 | gboolean lttv_apply_op_eq() { |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Applies the 'not equal' operator to the |
| 203 | * specified structure and value |
| 204 | * @return success/failure of operation |
| 205 | */ |
| 206 | gboolean lttv_apply_op_ne() { |
| 207 | |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Applies the 'lower than' operator to the |
| 212 | * specified structure and value |
| 213 | * @return success/failure of operation |
| 214 | */ |
| 215 | gboolean lttv_apply_op_lt() { |
| 216 | |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Applies the 'lower or equal' operator to the |
| 221 | * specified structure and value |
| 222 | * @return success/failure of operation |
| 223 | */ |
| 224 | gboolean lttv_apply_op_le() { |
| 225 | |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Applies the 'greater than' operator to the |
| 230 | * specified structure and value |
| 231 | * @return success/failure of operation |
| 232 | */ |
| 233 | gboolean lttv_apply_op_gt() { |
| 234 | |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Applies the 'greater or equal' operator to the |
| 239 | * specified structure and value |
| 240 | * @return success/failure of operation |
| 241 | */ |
| 242 | gboolean lttv_apply_op_ge() { |
| 243 | |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | /** |
| 249 | * Makes a copy of the current filter tree |
| 250 | * @param tree pointer to the current tree |
| 251 | * @return new copy of the filter tree |
| 252 | */ |
| 253 | LttvFilterTree* |
| 254 | lttv_filter_tree_clone(LttvFilterTree* tree) { |
| 255 | |
| 256 | |
| 257 | |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Makes a copy of the current filter |
| 262 | * @param filter pointer to the current filter |
| 263 | * @return new copy of the filter |
| 264 | */ |
| 265 | LttvFilter* |
| 266 | lttv_filter_clone(LttvFilter* filter) { |
| 267 | |
| 268 | |
| 269 | LttvFilter* newfilter = g_new(LttvFilter,1); |
| 270 | |
| 271 | // newfilter->expression = g_new(char,1) |
| 272 | strcpy(newfilter->expression,filter->expression); |
| 273 | |
| 274 | newfilter->head = lttv_filter_tree_clone(filter->head); |
| 275 | |
| 276 | return newfilter; |
| 277 | |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Creates a new lttv_filter |
| 283 | * @param expression filtering options string |
| 284 | * @param t pointer to the current LttvTrace |
| 285 | * @return the current lttv_filter or NULL if error |
| 286 | */ |
| 287 | LttvFilter* |
| 288 | lttv_filter_new(char *expression, LttvTraceState *tcs) { |
| 289 | |
| 290 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
| 291 | |
| 292 | unsigned |
| 293 | i, |
| 294 | p_nesting=0, /* parenthesis nesting value */ |
| 295 | b=0; /* current breakpoint in expression string */ |
| 296 | |
| 297 | /* trees */ |
| 298 | LttvFilter |
| 299 | *tree = lttv_filter_tree_new(), /* main tree */ |
| 300 | *subtree = NULL, /* buffer for subtrees */ |
| 301 | *t1, /* buffer #1 */ |
| 302 | *t2; /* buffer #2 */ |
| 303 | |
| 304 | /* |
| 305 | * Tree Stack |
| 306 | * each element of the list |
| 307 | * is a sub tree created |
| 308 | * by the use of parenthesis in the |
| 309 | * global expression. The final tree |
| 310 | * will be the one left at the root of |
| 311 | * the list |
| 312 | */ |
| 313 | GPtrArray *tree_stack = g_ptr_array_new(); |
| 314 | g_ptr_array_add( tree_stack,(gpointer) tree ); |
| 315 | |
| 316 | /* temporary values */ |
| 317 | GString *a_field_component = g_string_new(""); |
| 318 | GPtrArray *a_field_path = NULL; |
| 319 | |
| 320 | LttvSimpleExpression* a_simple_expression = g_new(LttvSimpleExpression,1); |
| 321 | |
| 322 | /* |
| 323 | * Parse entire expression and construct |
| 324 | * the binary tree. There are two steps |
| 325 | * in browsing that string |
| 326 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
| 327 | * 2. finding simple expressions |
| 328 | * - field path ( separated by dots ) |
| 329 | * - op ( >, <, =, >=, <=, !=) |
| 330 | * - value ( integer, string ... ) |
| 331 | * To spare computing time, the whole |
| 332 | * string is parsed in this loop for a |
| 333 | * O(n) complexity order. |
| 334 | * |
| 335 | * When encountering logical op &,|,^ |
| 336 | * 1. parse the last value if any |
| 337 | * 2. create a new tree |
| 338 | * 3. add the expression (simple exp, or exp (subtree)) to the tree |
| 339 | * 4. concatenate this tree with the current tree on top of the stack |
| 340 | * When encountering math ops >,>=,<,<=,=,!= |
| 341 | * 1. add to op to the simple expression |
| 342 | * 2. concatenate last field component to field path |
| 343 | * When encountering concatening ops . |
| 344 | * 1. concatenate last field component to field path |
| 345 | * When encountering opening parenthesis (,{,[ |
| 346 | * 1. create a new subtree on top of tree stack |
| 347 | * When encountering closing parenthesis ),},] |
| 348 | * 1. add the expression on right child of the current tree |
| 349 | * 2. the subtree is completed, allocate a new subtree |
| 350 | * 3. pop the tree value from the tree stack |
| 351 | */ |
| 352 | |
| 353 | a_field_path = g_ptr_array_new(); |
| 354 | g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */ |
| 355 | |
| 356 | |
| 357 | for(i=0;i<strlen(expression);i++) { |
| 358 | // g_print("%s\n",a_field_component->str); |
| 359 | g_print("%c ",expression[i]); |
| 360 | // g_print("switch:%c -->subtree:%p\n",expression[i],subtree); |
| 361 | switch(expression[i]) { |
| 362 | /* |
| 363 | * logical operators |
| 364 | */ |
| 365 | case '&': /* and */ |
| 366 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 367 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 368 | t2 = lttv_filter_tree_new(); |
| 369 | t2->node = LTTV_LOGICAL_AND; |
| 370 | if(subtree != NULL) { |
| 371 | t2->left = LTTV_TREE_NODE; |
| 372 | t2->l_child.t = subtree; |
| 373 | subtree = NULL; |
| 374 | t1->right = LTTV_TREE_NODE; |
| 375 | t1->r_child.t = t2; |
| 376 | } else { |
| 377 | a_simple_expression->value = a_field_component->str; |
| 378 | a_field_component = g_string_new(""); |
| 379 | t2->left = LTTV_TREE_LEAF; |
| 380 | t2->l_child.leaf = a_simple_expression; |
| 381 | a_simple_expression = g_new(LttvSimpleExpression,1); |
| 382 | t1->right = LTTV_TREE_NODE; |
| 383 | t1->r_child.t = t2; |
| 384 | } |
| 385 | |
| 386 | break; |
| 387 | case '|': /* or */ |
| 388 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 389 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 390 | t2 = lttv_filter_tree_new(); |
| 391 | t2->node = LTTV_LOGICAL_OR; |
| 392 | if(subtree != NULL) { |
| 393 | t2->left = LTTV_TREE_NODE; |
| 394 | t2->l_child.t = subtree; |
| 395 | subtree = NULL; |
| 396 | t1->right = LTTV_TREE_NODE; |
| 397 | t1->r_child.t = t2; |
| 398 | } else { |
| 399 | a_simple_expression->value = a_field_component->str; |
| 400 | a_field_component = g_string_new(""); |
| 401 | t2->left = LTTV_TREE_LEAF; |
| 402 | t2->l_child.leaf = a_simple_expression; |
| 403 | a_simple_expression = g_new(LttvSimpleExpression,1); |
| 404 | t1->right = LTTV_TREE_NODE; |
| 405 | t1->r_child.t = t2; |
| 406 | } |
| 407 | break; |
| 408 | case '^': /* xor */ |
| 409 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 410 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 411 | t2 = lttv_filter_tree_new(); |
| 412 | t2->node = LTTV_LOGICAL_XOR; |
| 413 | if(subtree != NULL) { |
| 414 | t2->left = LTTV_TREE_NODE; |
| 415 | t2->l_child.t = subtree; |
| 416 | subtree = NULL; |
| 417 | t1->right = LTTV_TREE_NODE; |
| 418 | t1->r_child.t = t2; |
| 419 | } else { |
| 420 | a_simple_expression->value = a_field_component->str; |
| 421 | a_field_component = g_string_new(""); |
| 422 | t2->left = LTTV_TREE_LEAF; |
| 423 | t2->l_child.leaf = a_simple_expression; |
| 424 | a_simple_expression = g_new(LttvSimpleExpression,1); |
| 425 | t1->right = LTTV_TREE_NODE; |
| 426 | t1->r_child.t = t2; |
| 427 | } |
| 428 | break; |
| 429 | case '!': /* not, or not equal (math op) */ |
| 430 | if(expression[i+1] == '=') { /* != */ |
| 431 | a_simple_expression->op = LTTV_FIELD_NE; |
| 432 | i++; |
| 433 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 434 | a_field_component = g_string_new(""); |
| 435 | } else { /* ! */ |
| 436 | // g_print("%s\n",a_field_component); |
| 437 | // a_field_component = g_string_new(""); |
| 438 | t1 = (LttvFilter*)g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 439 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 440 | t2 = lttv_filter_tree_new(); |
| 441 | t2->node = LTTV_LOGICAL_NOT; |
| 442 | t1->right = LTTV_TREE_NODE; |
| 443 | t1->r_child.t = t2; |
| 444 | } |
| 445 | break; |
| 446 | case '(': /* start of parenthesis */ |
| 447 | case '[': |
| 448 | case '{': |
| 449 | p_nesting++; /* incrementing parenthesis nesting value */ |
| 450 | t1 = lttv_filter_tree_new(); |
| 451 | g_ptr_array_add( tree_stack,(gpointer) t1 ); |
| 452 | break; |
| 453 | case ')': /* end of parenthesis */ |
| 454 | case ']': |
| 455 | case '}': |
| 456 | p_nesting--; /* decrementing parenthesis nesting value */ |
| 457 | if(p_nesting<0 || tree_stack->len<2) { |
| 458 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 459 | is not valid due to parenthesis incorrect use",expression); |
| 460 | return NULL; |
| 461 | } |
| 462 | |
| 463 | g_assert(tree_stack->len>0); |
| 464 | if(subtree != NULL) { |
| 465 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 466 | while(t1->right != LTTV_TREE_IDLE && t1->right != LTTV_TREE_LEAF) { |
| 467 | g_assert(t1!=NULL && t1->r_child.t != NULL); |
| 468 | t1 = t1->r_child.t; |
| 469 | } |
| 470 | t1->right = LTTV_TREE_NODE; |
| 471 | t1->r_child.t = subtree; |
| 472 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 473 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
| 474 | } else { |
| 475 | a_simple_expression->value = a_field_component->str; |
| 476 | a_field_component = g_string_new(""); |
| 477 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 478 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 479 | t1->right = LTTV_TREE_LEAF; |
| 480 | t1->r_child.leaf = a_simple_expression; |
| 481 | a_simple_expression = g_new(LttvSimpleExpression,1); |
| 482 | subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 483 | g_assert(subtree != NULL); |
| 484 | g_ptr_array_remove_index(tree_stack,tree_stack->len-1); |
| 485 | } |
| 486 | break; |
| 487 | |
| 488 | /* |
| 489 | * mathematic operators |
| 490 | */ |
| 491 | case '<': /* lower, lower or equal */ |
| 492 | if(expression[i+1] == '=') { /* <= */ |
| 493 | i++; |
| 494 | a_simple_expression->op = LTTV_FIELD_LE; |
| 495 | } else a_simple_expression->op = LTTV_FIELD_LT; |
| 496 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 497 | a_field_component = g_string_new(""); |
| 498 | break; |
| 499 | case '>': /* higher, higher or equal */ |
| 500 | if(expression[i+1] == '=') { /* >= */ |
| 501 | i++; |
| 502 | a_simple_expression->op = LTTV_FIELD_GE; |
| 503 | } else a_simple_expression->op = LTTV_FIELD_GT; |
| 504 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 505 | a_field_component = g_string_new(""); |
| 506 | break; |
| 507 | case '=': /* equal */ |
| 508 | a_simple_expression->op = LTTV_FIELD_EQ; |
| 509 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 510 | a_field_component = g_string_new(""); |
| 511 | break; |
| 512 | /* |
| 513 | * Field concatening caracter |
| 514 | */ |
| 515 | case '.': /* dot */ |
| 516 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 517 | a_field_component = g_string_new(""); |
| 518 | break; |
| 519 | default: /* concatening current string */ |
| 520 | g_string_append_c(a_field_component,expression[i]); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | g_print("subtree:%p, tree:%p, t1:%p, t2:%p\n",subtree,tree,t1,t2); |
| 525 | g_print("stack size: %i\n",tree_stack->len); |
| 526 | |
| 527 | /* |
| 528 | * Preliminary check to see |
| 529 | * if tree was constructed correctly |
| 530 | */ |
| 531 | if( p_nesting>0 ) { |
| 532 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 533 | is not valid due to parenthesis incorrect use",expression); |
| 534 | return NULL; |
| 535 | } |
| 536 | |
| 537 | if(tree_stack->len != 1) /* only root tree should remain */ |
| 538 | return NULL; |
| 539 | |
| 540 | /* processing last element of expression */ |
| 541 | t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); |
| 542 | while(t1->right != LTTV_TREE_IDLE) t1 = t1->r_child.t; |
| 543 | if(subtree != NULL) { /* add the subtree */ |
| 544 | t1->right = LTTV_TREE_NODE; |
| 545 | t1->r_child.t = subtree; |
| 546 | subtree = NULL; |
| 547 | } else { /* add a leaf */ |
| 548 | a_simple_expression->value = a_field_component->str; |
| 549 | a_field_component = g_string_new(""); |
| 550 | t1->right = LTTV_TREE_LEAF; |
| 551 | t1->r_child.leaf = a_simple_expression; |
| 552 | /* |
| 553 | * FIXME: is it really necessary to reallocate |
| 554 | * LttvSimpleExpression at this point ?? |
| 555 | */ |
| 556 | a_simple_expression = g_new(LttvSimpleExpression,1); |
| 557 | } |
| 558 | |
| 559 | g_assert(tree != NULL); |
| 560 | g_assert(subtree == NULL); |
| 561 | |
| 562 | lttv_filter_tracefile(tree,NULL); |
| 563 | |
| 564 | return tree; |
| 565 | |
| 566 | } |
| 567 | |
| 568 | void |
| 569 | lttv_filter_destroy(LttvFilter* filter) { |
| 570 | |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Assign a new tree for the current expression |
| 575 | * or sub expression |
| 576 | * @return pointer of LttvFilterTree |
| 577 | */ |
| 578 | LttvFilterTree* lttv_filter_tree_new() { |
| 579 | LttvFilterTree* tree; |
| 580 | |
| 581 | tree = g_new(LttvFilter,1); |
| 582 | tree->node = 0; //g_new(lttv_expression,1); |
| 583 | // tree->node->type = LTTV_UNDEFINED_EXPRESSION; |
| 584 | tree->left = LTTV_TREE_IDLE; |
| 585 | tree->right = LTTV_TREE_IDLE; |
| 586 | |
| 587 | return tree; |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Destroys the tree and his sub-trees |
| 592 | * @param tree Tree which must be destroyed |
| 593 | */ |
| 594 | void lttv_filter_tree_destroy(LttvFilterTree* tree) { |
| 595 | |
| 596 | if(tree == NULL) return; |
| 597 | |
| 598 | if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf); |
| 599 | else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t); |
| 600 | |
| 601 | if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf); |
| 602 | else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t); |
| 603 | |
| 604 | g_free(tree->node); |
| 605 | g_free(tree); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | /** |
| 610 | * Apply the filter to a specific trace |
| 611 | * @param filter the current filter applied |
| 612 | * @param tracefile the trace to apply the filter to |
| 613 | * @return success/failure of operation |
| 614 | */ |
| 615 | gboolean |
| 616 | lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile) { |
| 617 | |
| 618 | /* |
| 619 | * Each tree is parsed in inorder. |
| 620 | * This way, it's possible to apply the left filter of the |
| 621 | * tree, then decide whether or not the right branch should |
| 622 | * be parsed depending on the linking logical operator |
| 623 | * |
| 624 | * As for the filtering structure, since we are trying |
| 625 | * to remove elements from the trace, it might be better |
| 626 | * managing an array of all items to be removed .. |
| 627 | */ |
| 628 | |
| 629 | g_print("node:%p lchild:%p rchild:%p\n",filter,filter->l_child.t,filter->r_child.t); |
| 630 | g_print("node type%i\n",filter->node); |
| 631 | if(filter->left == LTTV_TREE_NODE) lttv_filter_tracefile(filter->l_child.t,NULL); |
| 632 | else if(filter->left == LTTV_TREE_LEAF) { |
| 633 | g_assert(filter->l_child.leaf->value != NULL); |
| 634 | g_print("%p: left is qqch %i %s\n",filter,filter->l_child.leaf->op,filter->l_child.leaf->value); |
| 635 | } |
| 636 | if(filter->right == LTTV_TREE_NODE) lttv_filter_tracefile(filter->r_child.t,NULL); |
| 637 | else if(filter->right == LTTV_TREE_LEAF) { |
| 638 | g_assert(filter->r_child.leaf->value != NULL); |
| 639 | g_print("%p: right is qqch %i %s\n",filter,filter->r_child.leaf->op,filter->r_child.leaf->value); |
| 640 | } |
| 641 | |
| 642 | /* test */ |
| 643 | /* int i, nb; |
| 644 | char *f_name, *e_name; |
| 645 | |
| 646 | char* field = "cpu"; |
| 647 | |
| 648 | LttvTraceHook h; |
| 649 | |
| 650 | LttEventType *et; |
| 651 | |
| 652 | LttType *t; |
| 653 | |
| 654 | GString *fe_name = g_string_new(""); |
| 655 | |
| 656 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
| 657 | g_print("NB:%i\n",nb); |
| 658 | for(i = 0 ; i < nb ; i++) { |
| 659 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
| 660 | e_name = ltt_eventtype_name(et); |
| 661 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
| 662 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
| 663 | g_print("facility:%s and event:%s\n",f_name,e_name); |
| 664 | } |
| 665 | */ |
| 666 | } |
| 667 | |
| 668 | gboolean |
| 669 | lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate) { |
| 670 | |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Apply the filter to a specific event |
| 675 | * @param filter the current filter applied |
| 676 | * @param event the event to apply the filter to |
| 677 | * @return success/failure of operation |
| 678 | */ |
| 679 | gboolean |
| 680 | lttv_filter_event(LttvFilter *filter, LttEvent *event) { |
| 681 | |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Initializes the filter module and specific values |
| 686 | */ |
| 687 | static void module_init() |
| 688 | { |
| 689 | |
| 690 | /* |
| 691 | * Quarks initialization |
| 692 | * for hardcoded filtering options |
| 693 | * |
| 694 | * TODO: traceset has no yet been defined |
| 695 | */ |
| 696 | |
| 697 | /* top fields */ |
| 698 | LTTV_FILTER_EVENT = g_quark_from_string("event"); |
| 699 | LTTV_FILTER_TRACE = g_quark_from_string("trace"); |
| 700 | LTTV_FILTER_TRACESET = g_quark_from_string("traceset"); |
| 701 | LTTV_FILTER_STATE = g_quark_from_string("state"); |
| 702 | LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile"); |
| 703 | |
| 704 | /* event.name, tracefile.name, trace.name */ |
| 705 | LTTV_FILTER_NAME = g_quark_from_string("name"); |
| 706 | |
| 707 | /* event sub fields */ |
| 708 | LTTV_FILTER_CATEGORY = g_quark_from_string("category"); |
| 709 | LTTV_FILTER_TIME = g_quark_from_string("time"); |
| 710 | LTTV_FILTER_TSC = g_quark_from_string("tsc"); |
| 711 | |
| 712 | /* state sub fields */ |
| 713 | LTTV_FILTER_PID = g_quark_from_string("pid"); |
| 714 | LTTV_FILTER_PPID = g_quark_from_string("ppid"); |
| 715 | LTTV_FILTER_C_TIME = g_quark_from_string("creation_time"); |
| 716 | LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time"); |
| 717 | LTTV_FILTER_P_NAME = g_quark_from_string("process_name"); |
| 718 | LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode"); |
| 719 | LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode"); |
| 720 | LTTV_FILTER_P_STATUS = g_quark_from_string("process_status"); |
| 721 | LTTV_FILTER_CPU = g_quark_from_string("cpu"); |
| 722 | |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * Destroys the filter module and specific values |
| 727 | */ |
| 728 | static void module_destroy() |
| 729 | { |
| 730 | } |
| 731 | |
| 732 | |
| 733 | LTTV_MODULE("filter", "Filters traceset and events", \ |
| 734 | "Filters traceset and events specifically to user input", \ |
| 735 | module_init, module_destroy) |
| 736 | |
| 737 | |
| 738 | |