1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2005 Michel Dagenais
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;
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.
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,
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.
28 * - the exists an other lttv_filter which conflicts with this one
31 #include <lttv/filter.h>
38 simple expr [ op expr ]
40 read_simple_expression
41 read_field_path [ rel value ]
44 read_field_component [. field path]
53 path(component...) -> field
59 LTTV_FILTER_TRACEFILE
,
72 LTTV_FILTER_EX_SUBMODE
,
77 * Assign a new tree for the current expression
79 * @return pointer of lttv_filter_tree
81 lttv_filter_tree
* lttv_filter_tree_new() {
82 lttv_filter_tree
* tree
;
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
;
94 * Destroys the tree and his sub-trees
95 * @param tree Tree which must be destroyed
97 void lttv_filter_tree_destroy(lttv_filter_tree
* tree
) {
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
);
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
);
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
117 parse_field_path(GPtrArray
* fp
) {
120 g_assert(f
=g_ptr_array_index(fp
,0)); //list_first(fp)->data;
122 if(g_quark_try_string(f
->str
) == LTTV_FILTER_EVENT
) {
123 // parse_subfield(fp, LTTV_FILTER_EVENT);
125 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACEFILE
) {
127 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_TRACE
) {
129 } else if(g_quark_try_string(f
->str
) == LTTV_FILTER_STATE
) {
132 g_warning("Unrecognized field in filter string");
139 * Add an filtering option to the current tree
140 * @param expression Current expression to parse
141 * @return success/failure of operation
144 parse_simple_expression(GString
* expression
) {
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
160 lttv_filter_new(char *expression
, LttvTraceState
*tcs
) {
162 g_print("filter::lttv_filter_new()\n"); /* debug */
166 p_nesting
=0, /* parenthesis nesting value */
167 b
=0; /* current breakpoint in expression string */
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
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
);
184 /* temporary values */
185 GString
*a_field_component
= g_string_new("");
186 GPtrArray
*a_field_path
= NULL
;
188 lttv_simple_expression a_simple_expression
;
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.
204 a_field_path
= g_ptr_array_new();
205 g_ptr_array_set_size(a_field_path
,2); /* by default, recording 2 field expressions */
207 for(i
=0;i
<strlen(expression
);i
++) {
208 g_print("%s\n",a_field_component
->str
);
209 switch(expression
[i
]) {
214 a_simple_expression
.value
= a_field_component
->str
;
215 a_field_component
= g_string_new("");
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
;
225 t
->left
= LTTV_TREE_LEAF
;
226 t
->l_child
.leaf
= g_new(lttv_simple_expression
,1);
234 case '!': /* not, or not equal (math op) */
235 if(expression
[i
+1] == '=') { /* != */
236 a_simple_expression
.op
= LTTV_FIELD_NE
;
239 g_print("%s\n",a_field_component
);
240 a_field_component
= g_string_new("");
243 case '(': /* start of parenthesis */
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
);
250 case ')': /* end of parenthesis */
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
);
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);
273 subtree
= g_ptr_array_index(tree_list
,tree_list
->len
);
274 g_ptr_array_remove_index(tree_list
,tree_list
->len
);
278 * mathematic operators
280 case '<': /* lower, lower or equal */
281 if(expression
[i
+1] == '=') { /* <= */
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("");
288 case '>': /* higher, higher or equal */
289 if(expression
[i
+1] == '=') { /* >= */
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("");
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("");
302 * Field concatening caracter
305 g_ptr_array_add( a_field_path
,(gpointer
) a_field_component
);
306 a_field_component
= g_string_new("");
308 default: /* concatening current string */
309 g_string_append_c(a_field_component
,expression
[i
]);
316 g_warning("Wrong filtering options, the string\n\"%s\"\n\
317 is not valid due to parenthesis incorrect use",expression
);
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
329 lttv_filter_tracefile(lttv_filter_t
*filter
, LttTracefile
*tracefile
) {
335 char *f_name, *e_name;
345 GString *fe_name = g_string_new("");
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);
360 lttv_filter_tracestate(lttv_filter_t
*filter
, LttvTraceState
*tracestate
) {
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
371 lttv_filter_event(lttv_filter_t
*filter
, LttEvent
*event
) {
376 * Initializes the filter module and specific values
378 static void module_init()
382 * Quarks initialization
383 * for hardcoded filtering options
385 * TODO: traceset has no yet been defined
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");
395 /* event.name, tracefile.name, trace.name */
396 LTTV_FILTER_NAME
= g_quark_from_string("name");
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");
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");
417 * Destroys the filter module and specific values
419 static void module_destroy()
424 LTTV_MODULE("filter", "Filters traceset and events", \
425 "Filters traceset and events specifically to user input", \
426 module_init
, module_destroy
)