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