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