| 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 | consist in AND, OR and NOT nested expressions, forming a tree with |
| 21 | simple relations as leaves. The simple relations test is a field |
| 22 | in an event is equal, not equal, smaller, smaller or equal, larger, or |
| 23 | larger or equal to a specified value. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * YET TO BE ANSWERED |
| 28 | * - the exists an other lttv_filter which conflicts with this one |
| 29 | */ |
| 30 | |
| 31 | #include <lttv/filter.h> |
| 32 | |
| 33 | /* |
| 34 | read_token |
| 35 | |
| 36 | read_expression |
| 37 | ( read expr ) |
| 38 | simple expr [ op expr ] |
| 39 | |
| 40 | read_simple_expression |
| 41 | read_field_path [ rel value ] |
| 42 | |
| 43 | read_field_path |
| 44 | read_field_component [. field path] |
| 45 | |
| 46 | read_field_component |
| 47 | name [ \[ value \] ] |
| 48 | |
| 49 | data struct: |
| 50 | and/or(left/right) |
| 51 | not(child) |
| 52 | op(left/right) |
| 53 | path(component...) -> field |
| 54 | */ |
| 55 | |
| 56 | GQuark |
| 57 | LTTV_FILTER_TRACE, |
| 58 | LTTV_FILTER_TRACESET, |
| 59 | LTTV_FILTER_TRACEFILE, |
| 60 | LTTV_FILTER_STATE, |
| 61 | LTTV_FILTER_EVENT, |
| 62 | LTTV_FILTER_NAME, |
| 63 | LTTV_FILTER_CATEGORY, |
| 64 | LTTV_FILTER_TIME, |
| 65 | LTTV_FILTER_TSC, |
| 66 | LTTV_FILTER_PID, |
| 67 | LTTV_FILTER_PPID, |
| 68 | LTTV_FILTER_C_TIME, |
| 69 | LTTV_FILTER_I_TIME, |
| 70 | LTTV_FILTER_P_NAME, |
| 71 | LTTV_FILTER_EX_MODE, |
| 72 | LTTV_FILTER_EX_SUBMODE, |
| 73 | LTTV_FILTER_P_STATUS, |
| 74 | LTTV_FILTER_CPU; |
| 75 | |
| 76 | /** |
| 77 | * Assign a new tree for the current expression |
| 78 | * or sub expression |
| 79 | * @return pointer of lttv_filter_tree |
| 80 | */ |
| 81 | lttv_filter_tree* lttv_filter_tree_new() { |
| 82 | lttv_filter_tree* tree; |
| 83 | |
| 84 | tree = g_new(lttv_filter_tree,1); |
| 85 | tree->node = g_new(lttv_expression,1); |
| 86 | tree->node->type = LTTV_UNDEFINED_EXPRESSION; |
| 87 | tree->left = LTTV_TREE_UNDEFINED; |
| 88 | tree->right = LTTV_TREE_UNDEFINED; |
| 89 | |
| 90 | return tree; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Destroys the tree and his sub-trees |
| 95 | * @param tree Tree which must be destroyed |
| 96 | */ |
| 97 | void lttv_filter_tree_destroy(lttv_filter_tree* tree) { |
| 98 | |
| 99 | if(tree->left == LTTV_TREE_LEAF) g_free(tree->l_child.leaf); |
| 100 | else if(tree->left == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->l_child.t); |
| 101 | |
| 102 | if(tree->right == LTTV_TREE_LEAF) g_free(tree->r_child.leaf); |
| 103 | else if(tree->right == LTTV_TREE_NODE) lttv_filter_tree_destroy(tree->r_child.t); |
| 104 | |
| 105 | g_free(tree->node); |
| 106 | g_free(tree); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Parse through filtering field hierarchy as specified |
| 111 | * by user. This function compares each value to |
| 112 | * predetermined quarks |
| 113 | * @param fp The field path list |
| 114 | * @return success/failure of operation |
| 115 | */ |
| 116 | gboolean |
| 117 | parse_field_path(GPtrArray* fp) { |
| 118 | |
| 119 | GString* f = NULL; |
| 120 | g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data; |
| 121 | |
| 122 | if(g_quark_try_string(f->str) == LTTV_FILTER_EVENT) { |
| 123 | // parse_subfield(fp, LTTV_FILTER_EVENT); |
| 124 | |
| 125 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACEFILE) { |
| 126 | |
| 127 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_TRACE) { |
| 128 | |
| 129 | } else if(g_quark_try_string(f->str) == LTTV_FILTER_STATE) { |
| 130 | |
| 131 | } else { |
| 132 | g_warning("Unrecognized field in filter string"); |
| 133 | return FALSE; |
| 134 | } |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Add an filtering option to the current tree |
| 140 | * @param expression Current expression to parse |
| 141 | * @return success/failure of operation |
| 142 | */ |
| 143 | gboolean |
| 144 | parse_simple_expression(GString* expression) { |
| 145 | |
| 146 | unsigned i; |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Creates a new lttv_filter |
| 155 | * @param expression filtering options string |
| 156 | * @param t pointer to the current LttvTrace |
| 157 | * @return the current lttv_filter or NULL if error |
| 158 | */ |
| 159 | lttv_filter_tree* |
| 160 | lttv_filter_new(char *expression, LttvTraceState *tcs) { |
| 161 | |
| 162 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
| 163 | |
| 164 | unsigned |
| 165 | i, |
| 166 | p_nesting=0, /* parenthesis nesting value */ |
| 167 | b=0; /* current breakpoint in expression string */ |
| 168 | |
| 169 | /* |
| 170 | * Main tree & Tree concatening list |
| 171 | * each element of the list |
| 172 | * is a sub tree created |
| 173 | * by the use of parenthesis in the |
| 174 | * global expression. The final tree |
| 175 | * will be the one created at the root of |
| 176 | * the list |
| 177 | */ |
| 178 | lttv_filter_tree* tree = NULL; |
| 179 | lttv_filter_tree* subtree = NULL; |
| 180 | lttv_filter_tree* current_tree = NULL; |
| 181 | GPtrArray *tree_list = g_ptr_array_new(); |
| 182 | g_ptr_array_add( tree_list,(gpointer) tree ); |
| 183 | |
| 184 | /* temporary values */ |
| 185 | GString *a_field_component = g_string_new(""); |
| 186 | GPtrArray *a_field_path = NULL; |
| 187 | |
| 188 | lttv_simple_expression a_simple_expression; |
| 189 | |
| 190 | /* |
| 191 | * Parse entire expression and construct |
| 192 | * the binary tree. There are two steps |
| 193 | * in browsing that string |
| 194 | * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} " |
| 195 | * 2. finding simple expressions |
| 196 | * - field path ( separated by dots ) |
| 197 | * - op ( >, <, =, >=, <=, !=) |
| 198 | * - value ( integer, string ... ) |
| 199 | * To spare computing time, the whole |
| 200 | * string is parsed in this loop for a |
| 201 | * O(n) complexity order. |
| 202 | */ |
| 203 | |
| 204 | a_field_path = g_ptr_array_new(); |
| 205 | g_ptr_array_set_size(a_field_path,2); /* by default, recording 2 field expressions */ |
| 206 | |
| 207 | for(i=0;i<strlen(expression);i++) { |
| 208 | g_print("%s\n",a_field_component->str); |
| 209 | switch(expression[i]) { |
| 210 | /* |
| 211 | * logical operators |
| 212 | */ |
| 213 | case '&': /* and */ |
| 214 | a_simple_expression.value = a_field_component->str; |
| 215 | a_field_component = g_string_new(""); |
| 216 | lttv_filter_tree* t; |
| 217 | t = lttv_filter_tree_new(); |
| 218 | t->node->type = LTTV_EXPRESSION_OP; |
| 219 | t->node->e.op = LTTV_LOGICAL_AND; |
| 220 | if(subtree != NULL) { |
| 221 | t->left = LTTV_TREE_NODE; |
| 222 | t->l_child.t = subtree; |
| 223 | subtree = NULL; |
| 224 | } else { |
| 225 | t->left = LTTV_TREE_LEAF; |
| 226 | t->l_child.leaf = g_new(lttv_simple_expression,1); |
| 227 | } |
| 228 | |
| 229 | break; |
| 230 | case '|': /* or */ |
| 231 | break; |
| 232 | case '^': /* xor */ |
| 233 | break; |
| 234 | case '!': /* not, or not equal (math op) */ |
| 235 | if(expression[i+1] == '=') { /* != */ |
| 236 | a_simple_expression.op = LTTV_FIELD_NE; |
| 237 | i++; |
| 238 | } else { /* ! */ |
| 239 | g_print("%s\n",a_field_component); |
| 240 | a_field_component = g_string_new(""); |
| 241 | } |
| 242 | break; |
| 243 | case '(': /* start of parenthesis */ |
| 244 | case '[': |
| 245 | case '{': |
| 246 | p_nesting++; /* incrementing parenthesis nesting value */ |
| 247 | lttv_filter_tree* subtree = lttv_filter_tree_new(); |
| 248 | g_ptr_array_add( tree_list,(gpointer) subtree ); |
| 249 | break; |
| 250 | case ')': /* end of parenthesis */ |
| 251 | case ']': |
| 252 | case '}': |
| 253 | p_nesting--; /* decrementing parenthesis nesting value */ |
| 254 | a_simple_expression.value = a_field_component->str; |
| 255 | a_field_component = g_string_new(""); |
| 256 | if(p_nesting<0 || tree_list->len<2) { |
| 257 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 258 | is not valid due to parenthesis incorrect use",expression); |
| 259 | return NULL; |
| 260 | } |
| 261 | /* lttv_filter_tree *sub1 = g_ptr_array_index(tree_list,tree_list->len-1); |
| 262 | lttv_filter_tree *sub2 = g_ptr_array_index(tree_list,tree_list->len); |
| 263 | if(sub1->left == LTTV_TREE_UNDEFINED){ |
| 264 | sub1->l_child.t = sub2; |
| 265 | sub1->left = LTTV_TREE_NODE; |
| 266 | } else if(sub1->right == LTTV_TREE_UNDEFINED){ |
| 267 | sub1->r_child.t = sub2; |
| 268 | sub1->right = LTTV_TREE_NODE; |
| 269 | } else g_error("error during tree assignation"); |
| 270 | g_ptr_array_remove_index(tree_list,tree_list->len); |
| 271 | break; |
| 272 | */ |
| 273 | subtree = g_ptr_array_index(tree_list,tree_list->len); |
| 274 | g_ptr_array_remove_index(tree_list,tree_list->len); |
| 275 | break; |
| 276 | |
| 277 | /* |
| 278 | * mathematic operators |
| 279 | */ |
| 280 | case '<': /* lower, lower or equal */ |
| 281 | if(expression[i+1] == '=') { /* <= */ |
| 282 | i++; |
| 283 | a_simple_expression.op = LTTV_FIELD_LE; |
| 284 | } else a_simple_expression.op = LTTV_FIELD_LT; |
| 285 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 286 | a_field_component = g_string_new(""); |
| 287 | break; |
| 288 | case '>': /* higher, higher or equal */ |
| 289 | if(expression[i+1] == '=') { /* >= */ |
| 290 | i++; |
| 291 | a_simple_expression.op = LTTV_FIELD_GE; |
| 292 | } else a_simple_expression.op = LTTV_FIELD_GT; |
| 293 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 294 | a_field_component = g_string_new(""); |
| 295 | break; |
| 296 | case '=': /* equal */ |
| 297 | a_simple_expression.op = LTTV_FIELD_EQ; |
| 298 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 299 | a_field_component = g_string_new(""); |
| 300 | break; |
| 301 | /* |
| 302 | * Field concatening caracter |
| 303 | */ |
| 304 | case '.': /* dot */ |
| 305 | g_ptr_array_add( a_field_path,(gpointer) a_field_component ); |
| 306 | a_field_component = g_string_new(""); |
| 307 | break; |
| 308 | default: /* concatening current string */ |
| 309 | g_string_append_c(a_field_component,expression[i]); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | if( p_nesting>0 ) { |
| 316 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 317 | is not valid due to parenthesis incorrect use",expression); |
| 318 | return NULL; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Apply the filter to a specific trace |
| 324 | * @param filter the current filter applied |
| 325 | * @param tracefile the trace to apply the filter to |
| 326 | * @return success/failure of operation |
| 327 | */ |
| 328 | gboolean |
| 329 | lttv_filter_tracefile(lttv_filter_t *filter, LttTracefile *tracefile) { |
| 330 | |
| 331 | |
| 332 | |
| 333 | /* test */ |
| 334 | /* int i, nb; |
| 335 | char *f_name, *e_name; |
| 336 | |
| 337 | char* field = "cpu"; |
| 338 | |
| 339 | LttvTraceHook h; |
| 340 | |
| 341 | LttEventType *et; |
| 342 | |
| 343 | LttType *t; |
| 344 | |
| 345 | GString *fe_name = g_string_new(""); |
| 346 | |
| 347 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
| 348 | g_print("NB:%i\n",nb); |
| 349 | for(i = 0 ; i < nb ; i++) { |
| 350 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
| 351 | e_name = ltt_eventtype_name(et); |
| 352 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
| 353 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
| 354 | g_print("facility:%s and event:%s\n",f_name,e_name); |
| 355 | } |
| 356 | */ |
| 357 | } |
| 358 | |
| 359 | gboolean |
| 360 | lttv_filter_tracestate(lttv_filter_t *filter, LttvTraceState *tracestate) { |
| 361 | |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Apply the filter to a specific event |
| 366 | * @param filter the current filter applied |
| 367 | * @param event the event to apply the filter to |
| 368 | * @return success/failure of operation |
| 369 | */ |
| 370 | gboolean |
| 371 | lttv_filter_event(lttv_filter_t *filter, LttEvent *event) { |
| 372 | |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Initializes the filter module and specific values |
| 377 | */ |
| 378 | static void module_init() |
| 379 | { |
| 380 | |
| 381 | /* |
| 382 | * Quarks initialization |
| 383 | * for hardcoded filtering options |
| 384 | * |
| 385 | * TODO: traceset has no yet been defined |
| 386 | */ |
| 387 | |
| 388 | /* top fields */ |
| 389 | LTTV_FILTER_EVENT = g_quark_from_string("event"); |
| 390 | LTTV_FILTER_TRACE = g_quark_from_string("trace"); |
| 391 | LTTV_FILTER_TRACESET = g_quark_from_string("traceset"); |
| 392 | LTTV_FILTER_STATE = g_quark_from_string("state"); |
| 393 | LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile"); |
| 394 | |
| 395 | /* event.name, tracefile.name, trace.name */ |
| 396 | LTTV_FILTER_NAME = g_quark_from_string("name"); |
| 397 | |
| 398 | /* event sub fields */ |
| 399 | LTTV_FILTER_CATEGORY = g_quark_from_string("category"); |
| 400 | LTTV_FILTER_TIME = g_quark_from_string("time"); |
| 401 | LTTV_FILTER_TSC = g_quark_from_string("tsc"); |
| 402 | |
| 403 | /* state sub fields */ |
| 404 | LTTV_FILTER_PID = g_quark_from_string("pid"); |
| 405 | LTTV_FILTER_PPID = g_quark_from_string("ppid"); |
| 406 | LTTV_FILTER_C_TIME = g_quark_from_string("creation_time"); |
| 407 | LTTV_FILTER_I_TIME = g_quark_from_string("insertion_time"); |
| 408 | LTTV_FILTER_P_NAME = g_quark_from_string("process_name"); |
| 409 | LTTV_FILTER_EX_MODE = g_quark_from_string("execution_mode"); |
| 410 | LTTV_FILTER_EX_SUBMODE = g_quark_from_string("execution_submode"); |
| 411 | LTTV_FILTER_P_STATUS = g_quark_from_string("process_status"); |
| 412 | LTTV_FILTER_CPU = g_quark_from_string("cpu"); |
| 413 | |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Destroys the filter module and specific values |
| 418 | */ |
| 419 | static void module_destroy() |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | |
| 424 | LTTV_MODULE("filter", "Filters traceset and events", \ |
| 425 | "Filters traceset and events specifically to user input", \ |
| 426 | module_init, module_destroy) |
| 427 | |
| 428 | |
| 429 | |