| 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 | * - should all the structures and field types be associated with GQuarks |
| 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 | |
| 63 | /** |
| 64 | * Parse through filtering field hierarchy as specified |
| 65 | * by user. This function compares each value to |
| 66 | * predetermined quarks |
| 67 | * @param fp The field path list |
| 68 | * @return success/failure of operation |
| 69 | */ |
| 70 | gboolean |
| 71 | parse_field_path(GList* fp) { |
| 72 | |
| 73 | GString* f = g_list_first(fp)->data; |
| 74 | |
| 75 | switch(g_quark_try_string(f->str)) { |
| 76 | // case LTTV_FILTER_TRACE: |
| 77 | // |
| 78 | // break; |
| 79 | // case LTTV_FILTER_TRACEFILE: |
| 80 | // |
| 81 | // break; |
| 82 | // case LTTV_FILTER_TRACESET: |
| 83 | // |
| 84 | // break; |
| 85 | // case LTTV_FILTER_STATE: |
| 86 | // |
| 87 | // break; |
| 88 | // case LTTV_FILTER_EVENT: |
| 89 | // |
| 90 | // break; |
| 91 | default: /* Quark value unrecognized or equal to 0 */ |
| 92 | g_warning("Unrecognized field in filter string"); |
| 93 | return FALSE; |
| 94 | } |
| 95 | return TRUE; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Add an filtering option to the current tree |
| 100 | * @param expression Current expression to parse |
| 101 | * @return success/failure of operation |
| 102 | */ |
| 103 | gboolean |
| 104 | parse_simple_expression(GString* expression) { |
| 105 | |
| 106 | unsigned i; |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Creates a new lttv_filter |
| 115 | * @param expression filtering options string |
| 116 | * @param t pointer to the current LttvTrace |
| 117 | * @return the current lttv_filter or NULL if error |
| 118 | */ |
| 119 | lttv_filter* |
| 120 | lttv_filter_new(char *expression, LttvTraceState *tcs) { |
| 121 | |
| 122 | g_print("filter::lttv_filter_new()\n"); /* debug */ |
| 123 | |
| 124 | unsigned |
| 125 | i, |
| 126 | p=0, /* parenthesis nesting value */ |
| 127 | b=0; /* current breakpoint in expression string */ |
| 128 | |
| 129 | |
| 130 | gpointer tree = NULL; |
| 131 | |
| 132 | /* temporary values */ |
| 133 | GString *a_field_component = g_string_new(""); |
| 134 | GList *a_field_path = NULL; |
| 135 | lttv_simple_expression a_simple_expression; |
| 136 | |
| 137 | /* |
| 138 | * 1. parse expression |
| 139 | * 2. construct binary tree |
| 140 | * 3. return corresponding filter |
| 141 | */ |
| 142 | |
| 143 | /* |
| 144 | * Binary tree memory allocation |
| 145 | * - based upon a preliminary block size |
| 146 | */ |
| 147 | gulong size = (strlen(expression)/AVERAGE_EXPRESSION_LENGTH)*MAX_FACTOR; |
| 148 | tree = g_malloc(size*sizeof(lttv_filter_tree)); |
| 149 | |
| 150 | /* |
| 151 | * Parse entire expression and construct |
| 152 | * the binary tree. There are two steps |
| 153 | * in browsing that string |
| 154 | * 1. finding boolean ops ( &,|,^,! ) and parenthesis |
| 155 | * 2. finding simple expressions |
| 156 | * - field path ( separated by dots ) |
| 157 | * - op ( >, <, =, >=, <=, !=) |
| 158 | * - value ( integer, string ... ) |
| 159 | * To spare computing time, the whole |
| 160 | * string is parsed in this loop for a |
| 161 | * O(n) complexity order. |
| 162 | */ |
| 163 | for(i=0;i<strlen(expression);i++) { |
| 164 | g_print("%s\n",a_field_component->str); |
| 165 | switch(expression[i]) { |
| 166 | /* |
| 167 | * logical operators |
| 168 | */ |
| 169 | case '&': /* and */ |
| 170 | case '|': /* or */ |
| 171 | case '^': /* xor */ |
| 172 | g_list_append( a_field_path, a_field_component ); |
| 173 | a_field_component = g_string_new(""); |
| 174 | break; |
| 175 | case '!': /* not, or not equal (math op) */ |
| 176 | if(expression[i+1] == '=') { /* != */ |
| 177 | a_simple_expression.op = LTTV_FIELD_NE; |
| 178 | i++; |
| 179 | } else { /* ! */ |
| 180 | g_print("%s\n",a_field_component); |
| 181 | a_field_component = g_string_new(""); |
| 182 | } |
| 183 | break; |
| 184 | case '(': /* start of parenthesis */ |
| 185 | p++; /* incrementing parenthesis nesting value */ |
| 186 | break; |
| 187 | case ')': /* end of parenthesis */ |
| 188 | p--; /* decrementing parenthesis nesting value */ |
| 189 | break; |
| 190 | |
| 191 | /* |
| 192 | * mathematic operators |
| 193 | */ |
| 194 | case '<': /* lower, lower or equal */ |
| 195 | if(expression[i+1] == '=') { /* <= */ |
| 196 | i++; |
| 197 | a_simple_expression.op = LTTV_FIELD_LE; |
| 198 | } else a_simple_expression.op = LTTV_FIELD_LT; |
| 199 | break; |
| 200 | case '>': /* higher, higher or equal */ |
| 201 | if(expression[i+1] == '=') { /* >= */ |
| 202 | i++; |
| 203 | a_simple_expression.op = LTTV_FIELD_GE; |
| 204 | } else a_simple_expression.op = LTTV_FIELD_GT; |
| 205 | break; |
| 206 | case '=': /* equal */ |
| 207 | a_simple_expression.op = LTTV_FIELD_EQ; |
| 208 | break; |
| 209 | /* |
| 210 | * Field concatening caracter |
| 211 | */ |
| 212 | case '.': /* dot */ |
| 213 | g_list_append( a_field_path, a_field_component ); |
| 214 | a_field_component = g_string_new(""); |
| 215 | break; |
| 216 | default: /* concatening current string */ |
| 217 | g_string_append_c(a_field_component,expression[i]); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | |
| 222 | |
| 223 | if( p>0 ) { |
| 224 | g_warning("Wrong filtering options, the string\n\"%s\"\n\ |
| 225 | is not valid due to parenthesis incorrect use",expression); |
| 226 | return NULL; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Apply the filter to a specific trace |
| 232 | * @param filter the current filter applied |
| 233 | * @param tracefile the trace to apply the filter to |
| 234 | * @return success/failure of operation |
| 235 | */ |
| 236 | gboolean |
| 237 | lttv_filter_tracefile(lttv_filter *filter, LttTracefile *tracefile) { |
| 238 | |
| 239 | |
| 240 | |
| 241 | /* test */ |
| 242 | /* int i, nb; |
| 243 | char *f_name, *e_name; |
| 244 | |
| 245 | char* field = "cpu"; |
| 246 | |
| 247 | LttvTraceHook h; |
| 248 | |
| 249 | LttEventType *et; |
| 250 | |
| 251 | LttType *t; |
| 252 | |
| 253 | GString *fe_name = g_string_new(""); |
| 254 | |
| 255 | nb = ltt_trace_eventtype_number(tcs->parent.t); |
| 256 | g_print("NB:%i\n",nb); |
| 257 | for(i = 0 ; i < nb ; i++) { |
| 258 | et = ltt_trace_eventtype_get(tcs->parent.t, i); |
| 259 | e_name = ltt_eventtype_name(et); |
| 260 | f_name = ltt_facility_name(ltt_eventtype_facility(et)); |
| 261 | g_string_printf(fe_name, "%s.%s", f_name, e_name); |
| 262 | g_print("facility:%s and event:%s\n",f_name,e_name); |
| 263 | } |
| 264 | */ |
| 265 | } |
| 266 | |
| 267 | gboolean |
| 268 | lttv_filter_tracestate(lttv_filter *filter, LttvTraceState *tracestate) { |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Apply the filter to a specific event |
| 274 | * @param filter the current filter applied |
| 275 | * @param event the event to apply the filter to |
| 276 | * @return success/failure of operation |
| 277 | */ |
| 278 | gboolean |
| 279 | lttv_filter_event(lttv_filter *filter, LttEvent *event) { |
| 280 | |
| 281 | } |
| 282 | |
| 283 | static void module_init() |
| 284 | { |
| 285 | LTTV_FILTER_EVENT = g_quark_from_string("event"); |
| 286 | LTTV_FILTER_TRACE = g_quark_from_string("trace"); |
| 287 | LTTV_FILTER_TRACESET = g_quark_from_string("traceset"); |
| 288 | LTTV_FILTER_STATE = g_quark_from_string("state"); |
| 289 | LTTV_FILTER_TRACEFILE = g_quark_from_string("tracefile"); |
| 290 | |
| 291 | } |
| 292 | |
| 293 | static void module_destroy() |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | |
| 298 | //LTTV_MODULE("filter", \ |
| 299 | "", \ |
| 300 | "", \ |
| 301 | module_init, module_destroy) |
| 302 | |
| 303 | |
| 304 | |