- redefined tree for the core filter
[lttv.git] / ltt / branches / poly / lttv / lttv / filter.c
CommitLineData
9c312311 1/* This file is part of the Linux Trace Toolkit viewer
0769c82f 2 * Copyright (C) 2003-2005 Michel Dagenais
9c312311 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
31452f49 19/*
48f6f3c2 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
a4c292d4 23 larger or equal to a specified value.
24*/
48f6f3c2 25
0769c82f 26/*
27 * YET TO BE ANSWERED
f4e9dd16 28 * - the exists an other lttv_filter which conflicts with this one
0769c82f 29 */
30
31452f49 31#include <lttv/filter.h>
32
31452f49 33/*
a4c292d4 34 read_token
48f6f3c2 35
a4c292d4 36 read_expression
37 ( read expr )
38 simple expr [ op expr ]
48f6f3c2 39
a4c292d4 40 read_simple_expression
41 read_field_path [ rel value ]
48f6f3c2 42
a4c292d4 43 read_field_path
44 read_field_component [. field path]
48f6f3c2 45
a4c292d4 46 read_field_component
47 name [ \[ value \] ]
48f6f3c2 48
a4c292d4 49 data struct:
50 and/or(left/right)
51 not(child)
52 op(left/right)
53 path(component...) -> field
31452f49 54*/
55
1a7fa682 56GQuark
57 LTTV_FILTER_TRACE,
58 LTTV_FILTER_TRACESET,
59 LTTV_FILTER_TRACEFILE,
60 LTTV_FILTER_STATE,
91ad3f0a 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
f4e9dd16 76/**
77 * Assign a new tree for the current expression
78 * or sub expression
79 * @return pointer of lttv_filter_tree
80 */
81lttv_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 */
97void 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}
1a7fa682 108
0769c82f 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 */
116gboolean
f4e9dd16 117parse_field_path(GPtrArray* fp) {
0769c82f 118
f4e9dd16 119 GString* f = NULL;
120 g_assert(f=g_ptr_array_index(fp,0)); //list_first(fp)->data;
0769c82f 121
91ad3f0a 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;
0769c82f 134 }
91ad3f0a 135 return TRUE;
0769c82f 136}
137
31452f49 138/**
84a333d6 139 * Add an filtering option to the current tree
140 * @param expression Current expression to parse
141 * @return success/failure of operation
142 */
143gboolean
144parse_simple_expression(GString* expression) {
145
146 unsigned i;
147
a4c292d4 148
0769c82f 149
a4c292d4 150
84a333d6 151}
152
153/**
154 * Creates a new lttv_filter
31452f49 155 * @param expression filtering options string
156 * @param t pointer to the current LttvTrace
84a333d6 157 * @return the current lttv_filter or NULL if error
31452f49 158 */
f4e9dd16 159lttv_filter_tree*
0769c82f 160lttv_filter_new(char *expression, LttvTraceState *tcs) {
a4c292d4 161
0769c82f 162 g_print("filter::lttv_filter_new()\n"); /* debug */
a4c292d4 163
a4c292d4 164 unsigned
165 i,
91ad3f0a 166 p_nesting=0, /* parenthesis nesting value */
a4c292d4 167 b=0; /* current breakpoint in expression string */
31452f49 168
f4e9dd16 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
a4c292d4 184 /* temporary values */
0769c82f 185 GString *a_field_component = g_string_new("");
f4e9dd16 186 GPtrArray *a_field_path = NULL;
187
a4c292d4 188 lttv_simple_expression a_simple_expression;
0769c82f 189
a4c292d4 190 /*
191 * Parse entire expression and construct
192 * the binary tree. There are two steps
193 * in browsing that string
f4e9dd16 194 * 1. finding boolean ops " &,|,^,! " and parenthesis " {,(,[,],),} "
a4c292d4 195 * 2. finding simple expressions
0769c82f 196 * - field path ( separated by dots )
a4c292d4 197 * - op ( >, <, =, >=, <=, !=)
0769c82f 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.
a4c292d4 202 */
f4e9dd16 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
a4c292d4 207 for(i=0;i<strlen(expression);i++) {
0769c82f 208 g_print("%s\n",a_field_component->str);
a4c292d4 209 switch(expression[i]) {
210 /*
211 * logical operators
212 */
213 case '&': /* and */
f4e9dd16 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;
a4c292d4 230 case '|': /* or */
f4e9dd16 231 break;
a4c292d4 232 case '^': /* xor */
a4c292d4 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 { /* ! */
0769c82f 239 g_print("%s\n",a_field_component);
1a7fa682 240 a_field_component = g_string_new("");
a4c292d4 241 }
242 break;
243 case '(': /* start of parenthesis */
91ad3f0a 244 case '[':
245 case '{':
246 p_nesting++; /* incrementing parenthesis nesting value */
f4e9dd16 247 lttv_filter_tree* subtree = lttv_filter_tree_new();
248 g_ptr_array_add( tree_list,(gpointer) subtree );
a4c292d4 249 break;
250 case ')': /* end of parenthesis */
91ad3f0a 251 case ']':
252 case '}':
253 p_nesting--; /* decrementing parenthesis nesting value */
f4e9dd16 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);
a4c292d4 275 break;
276
277 /*
278 * mathematic operators
279 */
280 case '<': /* lower, lower or equal */
281 if(expression[i+1] == '=') { /* <= */
282 i++;
f4e9dd16 283 a_simple_expression.op = LTTV_FIELD_LE;
a4c292d4 284 } else a_simple_expression.op = LTTV_FIELD_LT;
f4e9dd16 285 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
286 a_field_component = g_string_new("");
a4c292d4 287 break;
288 case '>': /* higher, higher or equal */
289 if(expression[i+1] == '=') { /* >= */
290 i++;
f4e9dd16 291 a_simple_expression.op = LTTV_FIELD_GE;
a4c292d4 292 } else a_simple_expression.op = LTTV_FIELD_GT;
f4e9dd16 293 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
294 a_field_component = g_string_new("");
a4c292d4 295 break;
296 case '=': /* equal */
297 a_simple_expression.op = LTTV_FIELD_EQ;
f4e9dd16 298 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
299 a_field_component = g_string_new("");
a4c292d4 300 break;
0769c82f 301 /*
302 * Field concatening caracter
303 */
304 case '.': /* dot */
f4e9dd16 305 g_ptr_array_add( a_field_path,(gpointer) a_field_component );
0769c82f 306 a_field_component = g_string_new("");
307 break;
a4c292d4 308 default: /* concatening current string */
1a7fa682 309 g_string_append_c(a_field_component,expression[i]);
a4c292d4 310 }
311 }
312
313
314
91ad3f0a 315 if( p_nesting>0 ) {
a4c292d4 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 }
31452f49 320}
321
84a333d6 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 */
31452f49 328gboolean
91ad3f0a 329lttv_filter_tracefile(lttv_filter_t *filter, LttTracefile *tracefile) {
0769c82f 330
331
332
333 /* test */
334/* int i, nb;
335 char *f_name, *e_name;
31452f49 336
0769c82f 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 */
31452f49 357}
358
1a7fa682 359gboolean
91ad3f0a 360lttv_filter_tracestate(lttv_filter_t *filter, LttvTraceState *tracestate) {
341aa948 361
362}
1a7fa682 363
84a333d6 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 */
31452f49 370gboolean
91ad3f0a 371lttv_filter_event(lttv_filter_t *filter, LttEvent *event) {
31452f49 372
373}
1a7fa682 374
91ad3f0a 375/**
376 * Initializes the filter module and specific values
377 */
1a7fa682 378static void module_init()
379{
91ad3f0a 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");
1a7fa682 394
91ad3f0a 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
1a7fa682 414}
415
91ad3f0a 416/**
417 * Destroys the filter module and specific values
418 */
1a7fa682 419static void module_destroy()
420{
421}
422
423
91ad3f0a 424LTTV_MODULE("filter", "Filters traceset and events", \
425 "Filters traceset and events specifically to user input", \
1a7fa682 426 module_init, module_destroy)
427
428
429
This page took 0.050084 seconds and 4 git commands to generate.