X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=ltt%2Fbranches%2Fpoly%2Flttv%2Flttv%2Ffilter.c;h=06c8370c8f8ce960bfcc7f18f1bcc05f947f9993;hb=826f1ab264718e5e4c6b101557cd205422d3d20b;hp=12f14e23791ef6eb0f5a24aa6fe2d8f956a14371;hpb=be66ef347d153a74e69c0b6d2ede3327a7bafdd3;p=lttv.git diff --git a/ltt/branches/poly/lttv/lttv/filter.c b/ltt/branches/poly/lttv/lttv/filter.c index 12f14e23..06c8370c 100644 --- a/ltt/branches/poly/lttv/lttv/filter.c +++ b/ltt/branches/poly/lttv/lttv/filter.c @@ -61,16 +61,22 @@ * \todo * - refine switch of expression in multiple uses functions * - remove the idle expressions in the tree - * - replace string for enum value for corresponding value */ -#define TEST +#ifdef HAVE_CONFIG_H +#include +#endif + +//#define TEST #ifdef TEST #include #include #endif #include +#include +#include +#include /** * @fn LttvSimpleExpression* lttv_simple_expression_new() @@ -106,7 +112,7 @@ lttv_simple_expression_assign_field(GPtrArray* fp, LttvSimpleExpression* se) { GString* f = NULL; if(fp->len < 2) return FALSE; - g_assert(f=g_ptr_array_remove_index(fp,0)); + g_assert((f=g_ptr_array_remove_index(fp,0))); /* * Parse through the specified @@ -373,7 +379,6 @@ lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value) { gboolean is_double = FALSE; LttTime t = ltt_time_zero; GString* v; - GQuark quark; switch(se->field) { /* @@ -418,14 +423,14 @@ lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value) { /* cannot specify number with more than one '.' */ if(is_double) return FALSE; else is_double = TRUE; - t.tv_sec = atoi(v); + t.tv_sec = atoi(v->str); g_string_free(v,TRUE); v = g_string_new(""); } else g_string_append_c(v,value[i]); } /* number can be integer or double */ - if(is_double) t.tv_nsec = atoi(v); - else t.tv_sec = atoi(v); + if(is_double) t.tv_nsec = atoi(v->str); + else t.tv_sec = atoi(v->str); g_string_free(v,TRUE); @@ -589,7 +594,7 @@ gboolean lttv_apply_op_eq_string(const gpointer v1, LttvFieldValue v2) { */ gboolean lttv_apply_op_eq_quark(const gpointer v1, LttvFieldValue v2) { GQuark* r = (GQuark*) v1; - g_print("v1:%i v2:%i\n",*r,v2.v_uint32); +// g_print("v1:%i v2:%i\n",*r,v2.v_uint32); return (*r == v2.v_uint32); } @@ -1043,7 +1048,9 @@ lttv_filter_tree_clone(const LttvFilterTree* tree) { */ LttvFilter* lttv_filter_clone(const LttvFilter* filter) { - + + if(!filter) return NULL; + LttvFilter* newfilter = g_new(LttvFilter,1); strcpy(newfilter->expression,filter->expression); @@ -1089,14 +1096,16 @@ lttv_filter_update(LttvFilter* filter) { int i, - p_nesting=0; /* parenthesis nesting value */ - + p_nesting=0, /* parenthesis nesting value */ + not=0; + /* trees */ LttvFilterTree *tree = lttv_filter_tree_new(), /* main tree */ *subtree = NULL, /* buffer for subtrees */ *t1, /* buffer #1 */ - *t2; /* buffer #2 */ + *t2, /* buffer #2 */ + *t3; /* buffer #3 */ /* * the filter @@ -1164,35 +1173,44 @@ lttv_filter_update(LttvFilter* filter) { for(i=0;iexpression);i++) { // debug - //g_print("%c ",filter->expression[i]); +// g_print("%c\n ",filter->expression[i]); switch(filter->expression[i]) { /* * logical operators */ case '&': /* and */ - + + /* get current tree in tree stack */ t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); + + /* get current node at absolute right */ while(t1->right != LTTV_TREE_IDLE) { g_assert(t1->right == LTTV_TREE_NODE); t1 = t1->r_child.t; } t2 = lttv_filter_tree_new(); t2->node = LTTV_LOGICAL_AND; + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t2; + if(not) { /* add not operator to tree */ + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t2->left = LTTV_TREE_NODE; + t2->l_child.t = t3; + t2 = t3; + not = 0; + } if(subtree != NULL) { /* append subtree to current tree */ t2->left = LTTV_TREE_NODE; t2->l_child.t = subtree; subtree = NULL; - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; } else { /* append a simple expression */ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); a_field_component = g_string_new(""); t2->left = LTTV_TREE_LEAF; t2->l_child.leaf = a_simple_expression; a_simple_expression = lttv_simple_expression_new(); - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; } break; @@ -1205,20 +1223,26 @@ lttv_filter_update(LttvFilter* filter) { } t2 = lttv_filter_tree_new(); t2->node = LTTV_LOGICAL_OR; - if(subtree != NULL) { /* append subtree to current tree */ + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t2; + if(not) { // add not operator to tree + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t2->left = LTTV_TREE_NODE; + t2->l_child.t = t3; + t2 = t3; + not = 0; + } + if(subtree != NULL) { /* append subtree to current tree */ t2->left = LTTV_TREE_NODE; t2->l_child.t = subtree; subtree = NULL; - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; - } else { /* append a simple expression */ + } else { /* append a simple expression */ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); a_field_component = g_string_new(""); t2->left = LTTV_TREE_LEAF; t2->l_child.leaf = a_simple_expression; a_simple_expression = lttv_simple_expression_new(); - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; } break; @@ -1231,20 +1255,26 @@ lttv_filter_update(LttvFilter* filter) { } t2 = lttv_filter_tree_new(); t2->node = LTTV_LOGICAL_XOR; + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t2; + if(not) { // add not operator to tree + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t2->left = LTTV_TREE_NODE; + t2->l_child.t = t3; + t2 = t3; + not = 0; + } if(subtree != NULL) { /* append subtree to current tree */ t2->left = LTTV_TREE_NODE; t2->l_child.t = subtree; subtree = NULL; - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; } else { /* append a simple expression */ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); a_field_component = g_string_new(""); t2->left = LTTV_TREE_LEAF; t2->l_child.leaf = a_simple_expression; a_simple_expression = lttv_simple_expression_new(); - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; } break; @@ -1257,15 +1287,7 @@ lttv_filter_update(LttvFilter* filter) { lttv_simple_expression_assign_operator(a_simple_expression,LTTV_FIELD_NE); i++; } else { /* ! */ - t1 = (LttvFilterTree*)g_ptr_array_index(tree_stack,tree_stack->len-1); - while(t1->right != LTTV_TREE_IDLE) { - g_assert(t1->right == LTTV_TREE_NODE); - t1 = t1->r_child.t; - } - t2 = lttv_filter_tree_new(); - t2->node = LTTV_LOGICAL_NOT; - t1->right = LTTV_TREE_NODE; - t1->r_child.t = t2; + not=1; } break; @@ -1275,6 +1297,13 @@ lttv_filter_update(LttvFilter* filter) { p_nesting++; /* incrementing parenthesis nesting value */ t1 = lttv_filter_tree_new(); + if(not) { /* add not operator to tree */ + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t3; + not = 0; + } g_ptr_array_add( tree_stack,(gpointer) t1 ); break; @@ -1291,13 +1320,21 @@ lttv_filter_update(LttvFilter* filter) { /* there must at least be the root tree left in the array */ g_assert(tree_stack->len>0); - + + t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); + while(t1->right != LTTV_TREE_IDLE) { + t1 = t1->r_child.t; + } + if(not) { // add not operator to tree + g_print("ici"); + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t3; + t1 = t3; + not = 0; + } if(subtree != NULL) { /* append subtree to current tree */ - t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); - while(t1->right != LTTV_TREE_IDLE) { - g_assert(t1->right == LTTV_TREE_NODE); - t1 = t1->r_child.t; - } t1->right = LTTV_TREE_NODE; t1->r_child.t = subtree; subtree = g_ptr_array_index(tree_stack,tree_stack->len-1); @@ -1305,12 +1342,6 @@ lttv_filter_update(LttvFilter* filter) { } else { /* assign subtree as current tree */ lttv_simple_expression_assign_value(a_simple_expression,g_string_free(a_field_component,FALSE)); a_field_component = g_string_new(""); - t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); - while(t1->right != LTTV_TREE_IDLE) { - g_assert(t1->right == LTTV_TREE_NODE); - g_assert(t1->r_child.t != NULL); - t1 = t1->r_child.t; - } t1->right = LTTV_TREE_LEAF; t1->r_child.leaf = a_simple_expression; a_simple_expression = lttv_simple_expression_new(); @@ -1388,12 +1419,22 @@ lttv_filter_update(LttvFilter* filter) { if(tree_stack->len != 1) /* only root tree should remain */ return FALSE; - /* processing last element of expression */ + /* + * processing last element of expression + */ t1 = g_ptr_array_index(tree_stack,tree_stack->len-1); while(t1->right != LTTV_TREE_IDLE) { g_assert(t1->right == LTTV_TREE_NODE); t1 = t1->r_child.t; } + if(not) { // add not operator to tree + t3 = lttv_filter_tree_new(); + t3->node = LTTV_LOGICAL_NOT; + t1->right = LTTV_TREE_NODE; + t1->r_child.t = t3; + t1 = t3; + not = 0; + } if(subtree != NULL) { /* add the subtree */ t1->right = LTTV_TREE_NODE; t1->r_child.t = subtree; @@ -1436,7 +1477,7 @@ lttv_filter_update(LttvFilter* filter) { /* debug */ g_print("+++++++++++++++ BEGIN PRINT ++++++++++++++++\n"); - lttv_print_tree(filter->head) ; + lttv_print_tree(filter->head,0) ; g_print("+++++++++++++++ END PRINT ++++++++++++++++++\n"); /* success */ @@ -1453,8 +1494,12 @@ lttv_filter_update(LttvFilter* filter) { void lttv_filter_destroy(LttvFilter* filter) { - g_free(filter->expression); - lttv_filter_tree_destroy(filter->head); + if(!filter) return; + + if(filter->expression) + g_free(filter->expression); + if(filter->head) + lttv_filter_tree_destroy(filter->head); g_free(filter); } @@ -1494,6 +1539,7 @@ lttv_filter_append_expression(LttvFilter* filter, const char *expression) { if(expression == NULL) return FALSE; if(filter == NULL) return FALSE; + if(expression[0] == '\0') return FALSE; /* Empty expression */ GString* s = g_string_new(""); if(filter->expression != NULL) { @@ -1633,7 +1679,8 @@ lttv_filter_tree_parse( else if(t->right == LTTV_TREE_LEAF) { rresult = lttv_filter_tree_parse_branch(t->r_child.leaf,event,tracefile,trace,state,context); } - + + /* * Apply and return the * logical link between the @@ -1642,7 +1689,8 @@ lttv_filter_tree_parse( switch(t->node) { case LTTV_LOGICAL_OR: return (lresult | rresult); case LTTV_LOGICAL_AND: return (lresult & rresult); - case LTTV_LOGICAL_NOT: return (!rresult); + case LTTV_LOGICAL_NOT: + return (t->left==LTTV_TREE_LEAF)?!lresult:((t->right==LTTV_TREE_LEAF)?!rresult:TRUE); case LTTV_LOGICAL_XOR: return (lresult ^ rresult); case 0: return (rresult); default: @@ -1681,16 +1729,14 @@ lttv_filter_tree_parse_branch( case LTTV_FILTER_TRACE_NAME: if(trace == NULL) return TRUE; else { - char* trace_name = ltt_trace_name(trace); - GQuark quark = g_quark_from_string(trace_name); + GQuark quark = ltt_trace_name(trace); return se->op((gpointer)&quark,v); } break; case LTTV_FILTER_TRACEFILE_NAME: if(tracefile == NULL) return TRUE; else { - char* tracefile_name = ltt_tracefile_name(tracefile); - GQuark quark = g_quark_from_string(tracefile_name); + GQuark quark = ltt_tracefile_name(tracefile); return se->op((gpointer)&quark,v); } break; @@ -1749,8 +1795,7 @@ lttv_filter_tree_parse_branch( else { LttEventType* et; et = ltt_event_eventtype(event); - char* event_name = ltt_eventtype_name(et); - GQuark quark = g_quark_from_string(event_name); + GQuark quark = ltt_eventtype_name(et); return se->op((gpointer)&quark,v); } break; @@ -1804,21 +1849,28 @@ lttv_filter_tree_parse_branch( * @param t the pointer to the current LttvFilterTree */ void -lttv_print_tree(const LttvFilterTree* t) { +lttv_print_tree(const LttvFilterTree* t, const int count) { - g_print("node:%p lchild:%p rchild:%p\n",t, //t->l_child.t,t->r_child.t); + g_print("node:%p lchild:%p rchild:%p depth:%i\n",t, //t->l_child.t,t->r_child.t); (t->left==LTTV_TREE_NODE)?t->l_child.t:NULL, - (t->right==LTTV_TREE_NODE)?t->r_child.t:NULL); - g_print("node type: %i / [left] %i / [right] %i\n",t->node,t->left,t->right); - if(t->left == LTTV_TREE_NODE) lttv_print_tree(t->l_child.t); - else if(t->left == LTTV_TREE_LEAF) { - g_print("%p: left is %i %p value\n",t,t->l_child.leaf->field,t->l_child.leaf->op); + (t->right==LTTV_TREE_NODE)?t->r_child.t:NULL, + count); + g_print("logic operator: %s\n",(t->node&1)?"OR":((t->node&2)?"AND":((t->node&4)?"NOT":((t->node&8)?"XOR":"IDLE")))); + g_print("|-> left branch %p is a %s\n",t->l_child.t,(t->left==LTTV_TREE_NODE)?"NODE":((t->left==LTTV_TREE_LEAF)?"LEAF":"IDLE")); + if(t->left == LTTV_TREE_LEAF) { + g_print("| |-> field type number: %i\n",t->l_child.leaf->field); + g_print("| |-> offset is: %i\n",t->l_child.leaf->offset); + g_print("| |-> operator function is: %p\n",t->l_child.leaf->op); } - if(t->right == LTTV_TREE_NODE) lttv_print_tree(t->r_child.t); - else if(t->right == LTTV_TREE_LEAF) { - g_print("%p: right is %i %p value\n",t,t->r_child.leaf->field,t->r_child.leaf->op); + g_print("|-> right branch %p is a %s\n",t->r_child.t,(t->right==LTTV_TREE_NODE)?"NODE":((t->right==LTTV_TREE_LEAF)?"LEAF":"IDLE")); + if(t->right == LTTV_TREE_LEAF) { + g_print("| |-> field type number: %i\n",t->r_child.leaf->field); + g_print("| |-> offset is: %i\n",t->r_child.leaf->offset); + g_print("| |-> operator function is: %p\n",t->r_child.leaf->op); } - + + if(t->left == LTTV_TREE_NODE) lttv_print_tree(t->l_child.t,count+1); + if(t->right == LTTV_TREE_NODE) lttv_print_tree(t->r_child.t,count+1); } /**