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