]>
Commit | Line | Data |
---|---|---|
7ce7e424 TZ |
1 | /* |
2 | * trace_events_filter - generic event filtering | |
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 as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
17 | * | |
18 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | |
19 | */ | |
20 | ||
7ce7e424 TZ |
21 | #include <linux/module.h> |
22 | #include <linux/ctype.h> | |
ac1adc55 | 23 | #include <linux/mutex.h> |
6fb2915d | 24 | #include <linux/perf_event.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
7ce7e424 TZ |
26 | |
27 | #include "trace.h" | |
4bda2d51 | 28 | #include "trace_output.h" |
7ce7e424 | 29 | |
49aa2951 SR |
30 | #define DEFAULT_SYS_FILTER_MESSAGE \ |
31 | "### global filter ###\n" \ | |
32 | "# Use this to set filters for multiple events.\n" \ | |
33 | "# Only events with the given fields will be affected.\n" \ | |
34 | "# If no events are modified, an error message will be displayed here" | |
35 | ||
80765597 | 36 | /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */ |
e9baef0d | 37 | #define OPS \ |
80765597 SRV |
38 | C( OP_GLOB, "~" ), \ |
39 | C( OP_NE, "!=" ), \ | |
40 | C( OP_EQ, "==" ), \ | |
41 | C( OP_LE, "<=" ), \ | |
42 | C( OP_LT, "<" ), \ | |
43 | C( OP_GE, ">=" ), \ | |
44 | C( OP_GT, ">" ), \ | |
45 | C( OP_BAND, "&" ), \ | |
46 | C( OP_MAX, NULL ) | |
e9baef0d SRV |
47 | |
48 | #undef C | |
80765597 | 49 | #define C(a, b) a |
e9baef0d SRV |
50 | |
51 | enum filter_op_ids { OPS }; | |
8b372562 | 52 | |
e9baef0d | 53 | #undef C |
80765597 | 54 | #define C(a, b) b |
8b372562 | 55 | |
80765597 | 56 | static const char * ops[] = { OPS }; |
8b372562 | 57 | |
478325f1 | 58 | /* |
80765597 | 59 | * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND |
478325f1 SRV |
60 | * pred_funcs_##type below must match the order of them above. |
61 | */ | |
80765597 | 62 | #define PRED_FUNC_START OP_LE |
478325f1 SRV |
63 | #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START) |
64 | ||
e9baef0d | 65 | #define ERRORS \ |
80765597 SRV |
66 | C(NONE, "No error"), \ |
67 | C(INVALID_OP, "Invalid operator"), \ | |
68 | C(TOO_MANY_OPEN, "Too many '('"), \ | |
69 | C(TOO_MANY_CLOSE, "Too few '('"), \ | |
70 | C(MISSING_QUOTE, "Missing matching quote"), \ | |
71 | C(OPERAND_TOO_LONG, "Operand too long"), \ | |
72 | C(EXPECT_STRING, "Expecting string field"), \ | |
73 | C(EXPECT_DIGIT, "Expecting numeric field"), \ | |
74 | C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \ | |
75 | C(FIELD_NOT_FOUND, "Field not found"), \ | |
76 | C(ILLEGAL_INTVAL, "Illegal integer value"), \ | |
77 | C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \ | |
78 | C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \ | |
79 | C(INVALID_FILTER, "Meaningless filter expression"), \ | |
80 | C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \ | |
81 | C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), | |
e9baef0d SRV |
82 | |
83 | #undef C | |
84 | #define C(a, b) FILT_ERR_##a | |
85 | ||
86 | enum { ERRORS }; | |
87 | ||
88 | #undef C | |
89 | #define C(a, b) b | |
90 | ||
91 | static char *err_text[] = { ERRORS }; | |
8b372562 | 92 | |
80765597 SRV |
93 | /* Called after a '!' character but "!=" and "!~" are not "not"s */ |
94 | static bool is_not(const char *str) | |
95 | { | |
96 | switch (str[1]) { | |
97 | case '=': | |
98 | case '~': | |
99 | return false; | |
100 | } | |
101 | return true; | |
102 | } | |
8b372562 | 103 | |
80765597 SRV |
104 | /** |
105 | * prog_entry - a singe entry in the filter program | |
106 | * @target: Index to jump to on a branch (actually one minus the index) | |
107 | * @when_to_branch: The value of the result of the predicate to do a branch | |
108 | * @pred: The predicate to execute. | |
109 | */ | |
110 | struct prog_entry { | |
111 | int target; | |
112 | int when_to_branch; | |
113 | struct filter_pred *pred; | |
8b372562 TZ |
114 | }; |
115 | ||
80765597 SRV |
116 | /** |
117 | * update_preds- assign a program entry a label target | |
118 | * @prog: The program array | |
119 | * @N: The index of the current entry in @prog | |
120 | * @when_to_branch: What to assign a program entry for its branch condition | |
121 | * | |
122 | * The program entry at @N has a target that points to the index of a program | |
123 | * entry that can have its target and when_to_branch fields updated. | |
124 | * Update the current program entry denoted by index @N target field to be | |
125 | * that of the updated entry. This will denote the entry to update if | |
126 | * we are processing an "||" after an "&&" | |
127 | */ | |
128 | static void update_preds(struct prog_entry *prog, int N, int invert) | |
129 | { | |
130 | int t, s; | |
131 | ||
132 | t = prog[N].target; | |
133 | s = prog[t].target; | |
134 | prog[t].when_to_branch = invert; | |
135 | prog[t].target = N; | |
136 | prog[N].target = s; | |
137 | } | |
138 | ||
139 | struct filter_parse_error { | |
8b372562 TZ |
140 | int lasterr; |
141 | int lasterr_pos; | |
8b372562 TZ |
142 | }; |
143 | ||
80765597 SRV |
144 | static void parse_error(struct filter_parse_error *pe, int err, int pos) |
145 | { | |
146 | pe->lasterr = err; | |
147 | pe->lasterr_pos = pos; | |
148 | } | |
149 | ||
150 | typedef int (*parse_pred_fn)(const char *str, void *data, int pos, | |
151 | struct filter_parse_error *pe, | |
152 | struct filter_pred **pred); | |
153 | ||
154 | enum { | |
155 | INVERT = 1, | |
156 | PROCESS_AND = 2, | |
157 | PROCESS_OR = 4, | |
61e9dea2 SR |
158 | }; |
159 | ||
80765597 SRV |
160 | /* |
161 | * Without going into a formal proof, this explains the method that is used in | |
162 | * parsing the logical expressions. | |
163 | * | |
164 | * For example, if we have: "a && !(!b || (c && g)) || d || e && !f" | |
165 | * The first pass will convert it into the following program: | |
166 | * | |
167 | * n1: r=a; l1: if (!r) goto l4; | |
168 | * n2: r=b; l2: if (!r) goto l4; | |
169 | * n3: r=c; r=!r; l3: if (r) goto l4; | |
170 | * n4: r=g; r=!r; l4: if (r) goto l5; | |
171 | * n5: r=d; l5: if (r) goto T | |
172 | * n6: r=e; l6: if (!r) goto l7; | |
173 | * n7: r=f; r=!r; l7: if (!r) goto F | |
174 | * T: return TRUE | |
175 | * F: return FALSE | |
176 | * | |
177 | * To do this, we use a data structure to represent each of the above | |
178 | * predicate and conditions that has: | |
179 | * | |
180 | * predicate, when_to_branch, invert, target | |
181 | * | |
182 | * The "predicate" will hold the function to determine the result "r". | |
183 | * The "when_to_branch" denotes what "r" should be if a branch is to be taken | |
184 | * "&&" would contain "!r" or (0) and "||" would contain "r" or (1). | |
185 | * The "invert" holds whether the value should be reversed before testing. | |
186 | * The "target" contains the label "l#" to jump to. | |
187 | * | |
188 | * A stack is created to hold values when parentheses are used. | |
189 | * | |
190 | * To simplify the logic, the labels will start at 0 and not 1. | |
191 | * | |
192 | * The possible invert values are 1 and 0. The number of "!"s that are in scope | |
193 | * before the predicate determines the invert value, if the number is odd then | |
194 | * the invert value is 1 and 0 otherwise. This means the invert value only | |
195 | * needs to be toggled when a new "!" is introduced compared to what is stored | |
196 | * on the stack, where parentheses were used. | |
197 | * | |
198 | * The top of the stack and "invert" are initialized to zero. | |
199 | * | |
200 | * ** FIRST PASS ** | |
201 | * | |
202 | * #1 A loop through all the tokens is done: | |
203 | * | |
204 | * #2 If the token is an "(", the stack is push, and the current stack value | |
205 | * gets the current invert value, and the loop continues to the next token. | |
206 | * The top of the stack saves the "invert" value to keep track of what | |
207 | * the current inversion is. As "!(a && !b || c)" would require all | |
208 | * predicates being affected separately by the "!" before the parentheses. | |
209 | * And that would end up being equivalent to "(!a || b) && !c" | |
210 | * | |
211 | * #3 If the token is an "!", the current "invert" value gets inverted, and | |
212 | * the loop continues. Note, if the next token is a predicate, then | |
213 | * this "invert" value is only valid for the current program entry, | |
214 | * and does not affect other predicates later on. | |
215 | * | |
216 | * The only other acceptable token is the predicate string. | |
217 | * | |
218 | * #4 A new entry into the program is added saving: the predicate and the | |
219 | * current value of "invert". The target is currently assigned to the | |
220 | * previous program index (this will not be its final value). | |
221 | * | |
222 | * #5 We now enter another loop and look at the next token. The only valid | |
223 | * tokens are ")", "&&", "||" or end of the input string "\0". | |
224 | * | |
225 | * #6 The invert variable is reset to the current value saved on the top of | |
226 | * the stack. | |
227 | * | |
228 | * #7 The top of the stack holds not only the current invert value, but also | |
229 | * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher | |
230 | * precedence than "||". That is "a && b || c && d" is equivalent to | |
231 | * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs | |
232 | * to be processed. This is the case if an "&&" was the last token. If it was | |
233 | * then we call update_preds(). This takes the program, the current index in | |
234 | * the program, and the current value of "invert". More will be described | |
235 | * below about this function. | |
236 | * | |
237 | * #8 If the next token is "&&" then we set a flag in the top of the stack | |
238 | * that denotes that "&&" needs to be processed, break out of this loop | |
239 | * and continue with the outer loop. | |
240 | * | |
241 | * #9 Otherwise, if a "||" needs to be processed then update_preds() is called. | |
242 | * This is called with the program, the current index in the program, but | |
243 | * this time with an inverted value of "invert" (that is !invert). This is | |
244 | * because the value taken will become the "when_to_branch" value of the | |
245 | * program. | |
246 | * Note, this is called when the next token is not an "&&". As stated before, | |
247 | * "&&" takes higher precedence, and "||" should not be processed yet if the | |
248 | * next logical operation is "&&". | |
249 | * | |
250 | * #10 If the next token is "||" then we set a flag in the top of the stack | |
251 | * that denotes that "||" needs to be processed, break out of this loop | |
252 | * and continue with the outer loop. | |
253 | * | |
254 | * #11 If this is the end of the input string "\0" then we break out of both | |
255 | * loops. | |
256 | * | |
257 | * #12 Otherwise, the next token is ")", where we pop the stack and continue | |
258 | * this inner loop. | |
259 | * | |
260 | * Now to discuss the update_pred() function, as that is key to the setting up | |
261 | * of the program. Remember the "target" of the program is initialized to the | |
262 | * previous index and not the "l" label. The target holds the index into the | |
263 | * program that gets affected by the operand. Thus if we have something like | |
264 | * "a || b && c", when we process "a" the target will be "-1" (undefined). | |
265 | * When we process "b", its target is "0", which is the index of "a", as that's | |
266 | * the predicate that is affected by "||". But because the next token after "b" | |
267 | * is "&&" we don't call update_preds(). Instead continue to "c". As the | |
268 | * next token after "c" is not "&&" but the end of input, we first process the | |
269 | * "&&" by calling update_preds() for the "&&" then we process the "||" by | |
270 | * callin updates_preds() with the values for processing "||". | |
271 | * | |
272 | * What does that mean? What update_preds() does is to first save the "target" | |
273 | * of the program entry indexed by the current program entry's "target" | |
274 | * (remember the "target" is initialized to previous program entry), and then | |
275 | * sets that "target" to the current index which represents the label "l#". | |
276 | * That entry's "when_to_branch" is set to the value passed in (the "invert" | |
277 | * or "!invert"). Then it sets the current program entry's target to the saved | |
278 | * "target" value (the old value of the program that had its "target" updated | |
279 | * to the label). | |
280 | * | |
281 | * Looking back at "a || b && c", we have the following steps: | |
282 | * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target | |
283 | * "||" - flag that we need to process "||"; continue outer loop | |
284 | * "b" - prog[1] = { "b", X, 0 } | |
285 | * "&&" - flag that we need to process "&&"; continue outer loop | |
286 | * (Notice we did not process "||") | |
287 | * "c" - prog[2] = { "c", X, 1 } | |
288 | * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&" | |
289 | * t = prog[2].target; // t = 1 | |
290 | * s = prog[t].target; // s = 0 | |
291 | * prog[t].target = 2; // Set target to "l2" | |
292 | * prog[t].when_to_branch = 0; | |
293 | * prog[2].target = s; | |
294 | * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||" | |
295 | * t = prog[2].target; // t = 0 | |
296 | * s = prog[t].target; // s = -1 | |
297 | * prog[t].target = 2; // Set target to "l2" | |
298 | * prog[t].when_to_branch = 1; | |
299 | * prog[2].target = s; | |
300 | * | |
301 | * #13 Which brings us to the final step of the first pass, which is to set | |
302 | * the last program entry's when_to_branch and target, which will be | |
303 | * when_to_branch = 0; target = N; ( the label after the program entry after | |
304 | * the last program entry processed above). | |
305 | * | |
306 | * If we denote "TRUE" to be the entry after the last program entry processed, | |
307 | * and "FALSE" the program entry after that, we are now done with the first | |
308 | * pass. | |
309 | * | |
310 | * Making the above "a || b && c" have a progam of: | |
311 | * prog[0] = { "a", 1, 2 } | |
312 | * prog[1] = { "b", 0, 2 } | |
313 | * prog[2] = { "c", 0, 3 } | |
314 | * | |
315 | * Which translates into: | |
316 | * n0: r = a; l0: if (r) goto l2; | |
317 | * n1: r = b; l1: if (!r) goto l2; | |
318 | * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;" | |
319 | * T: return TRUE; l3: | |
320 | * F: return FALSE | |
321 | * | |
322 | * Although, after the first pass, the program is correct, it is | |
323 | * inefficient. The simple sample of "a || b && c" could be easily been | |
324 | * converted into: | |
325 | * n0: r = a; if (r) goto T | |
326 | * n1: r = b; if (!r) goto F | |
327 | * n2: r = c; if (!r) goto F | |
328 | * T: return TRUE; | |
329 | * F: return FALSE; | |
330 | * | |
331 | * The First Pass is over the input string. The next too passes are over | |
332 | * the program itself. | |
333 | * | |
334 | * ** SECOND PASS ** | |
335 | * | |
336 | * Which brings us to the second pass. If a jump to a label has the | |
337 | * same condition as that label, it can instead jump to its target. | |
338 | * The original example of "a && !(!b || (c && g)) || d || e && !f" | |
339 | * where the first pass gives us: | |
340 | * | |
341 | * n1: r=a; l1: if (!r) goto l4; | |
342 | * n2: r=b; l2: if (!r) goto l4; | |
343 | * n3: r=c; r=!r; l3: if (r) goto l4; | |
344 | * n4: r=g; r=!r; l4: if (r) goto l5; | |
345 | * n5: r=d; l5: if (r) goto T | |
346 | * n6: r=e; l6: if (!r) goto l7; | |
347 | * n7: r=f; r=!r; l7: if (!r) goto F: | |
348 | * T: return TRUE; | |
349 | * F: return FALSE | |
350 | * | |
351 | * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;". | |
352 | * And "l5: if (r) goto T", we could optimize this by converting l3 and l4 | |
353 | * to go directly to T. To accomplish this, we start from the last | |
354 | * entry in the program and work our way back. If the target of the entry | |
355 | * has the same "when_to_branch" then we could use that entry's target. | |
356 | * Doing this, the above would end up as: | |
357 | * | |
358 | * n1: r=a; l1: if (!r) goto l4; | |
359 | * n2: r=b; l2: if (!r) goto l4; | |
360 | * n3: r=c; r=!r; l3: if (r) goto T; | |
361 | * n4: r=g; r=!r; l4: if (r) goto T; | |
362 | * n5: r=d; l5: if (r) goto T; | |
363 | * n6: r=e; l6: if (!r) goto F; | |
364 | * n7: r=f; r=!r; l7: if (!r) goto F; | |
365 | * T: return TRUE | |
366 | * F: return FALSE | |
367 | * | |
368 | * In that same pass, if the "when_to_branch" doesn't match, we can simply | |
369 | * go to the program entry after the label. That is, "l2: if (!r) goto l4;" | |
370 | * where "l4: if (r) goto T;", then we can convert l2 to be: | |
371 | * "l2: if (!r) goto n5;". | |
372 | * | |
373 | * This will have the second pass give us: | |
374 | * n1: r=a; l1: if (!r) goto n5; | |
375 | * n2: r=b; l2: if (!r) goto n5; | |
376 | * n3: r=c; r=!r; l3: if (r) goto T; | |
377 | * n4: r=g; r=!r; l4: if (r) goto T; | |
378 | * n5: r=d; l5: if (r) goto T | |
379 | * n6: r=e; l6: if (!r) goto F; | |
380 | * n7: r=f; r=!r; l7: if (!r) goto F | |
381 | * T: return TRUE | |
382 | * F: return FALSE | |
383 | * | |
384 | * Notice, all the "l#" labels are no longer used, and they can now | |
385 | * be discarded. | |
386 | * | |
387 | * ** THIRD PASS ** | |
388 | * | |
389 | * For the third pass we deal with the inverts. As they simply just | |
390 | * make the "when_to_branch" get inverted, a simple loop over the | |
391 | * program to that does: "when_to_branch ^= invert;" will do the | |
392 | * job, leaving us with: | |
393 | * n1: r=a; if (!r) goto n5; | |
394 | * n2: r=b; if (!r) goto n5; | |
395 | * n3: r=c: if (!r) goto T; | |
396 | * n4: r=g; if (!r) goto T; | |
397 | * n5: r=d; if (r) goto T | |
398 | * n6: r=e; if (!r) goto F; | |
399 | * n7: r=f; if (r) goto F | |
400 | * T: return TRUE | |
401 | * F: return FALSE | |
402 | * | |
403 | * As "r = a; if (!r) goto n5;" is obviously the same as | |
404 | * "if (!a) goto n5;" without doing anything we can interperate the | |
405 | * program as: | |
406 | * n1: if (!a) goto n5; | |
407 | * n2: if (!b) goto n5; | |
408 | * n3: if (!c) goto T; | |
409 | * n4: if (!g) goto T; | |
410 | * n5: if (d) goto T | |
411 | * n6: if (!e) goto F; | |
412 | * n7: if (f) goto F | |
413 | * T: return TRUE | |
414 | * F: return FALSE | |
415 | * | |
416 | * Since the inverts are discarded at the end, there's no reason to store | |
417 | * them in the program array (and waste memory). A separate array to hold | |
418 | * the inverts is used and freed at the end. | |
419 | */ | |
420 | static struct prog_entry * | |
421 | predicate_parse(const char *str, int nr_parens, int nr_preds, | |
422 | parse_pred_fn parse_pred, void *data, | |
423 | struct filter_parse_error *pe) | |
424 | { | |
425 | struct prog_entry *prog_stack; | |
426 | struct prog_entry *prog; | |
427 | const char *ptr = str; | |
428 | char *inverts = NULL; | |
429 | int *op_stack; | |
430 | int *top; | |
431 | int invert = 0; | |
432 | int ret = -ENOMEM; | |
433 | int len; | |
434 | int N = 0; | |
435 | int i; | |
436 | ||
437 | nr_preds += 2; /* For TRUE and FALSE */ | |
438 | ||
6da2ec56 | 439 | op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL); |
80765597 SRV |
440 | if (!op_stack) |
441 | return ERR_PTR(-ENOMEM); | |
6da2ec56 | 442 | prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL); |
80765597 SRV |
443 | if (!prog_stack) { |
444 | parse_error(pe, -ENOMEM, 0); | |
445 | goto out_free; | |
446 | } | |
6da2ec56 | 447 | inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL); |
80765597 SRV |
448 | if (!inverts) { |
449 | parse_error(pe, -ENOMEM, 0); | |
450 | goto out_free; | |
451 | } | |
452 | ||
453 | top = op_stack; | |
454 | prog = prog_stack; | |
455 | *top = 0; | |
456 | ||
457 | /* First pass */ | |
458 | while (*ptr) { /* #1 */ | |
459 | const char *next = ptr++; | |
460 | ||
461 | if (isspace(*next)) | |
462 | continue; | |
463 | ||
464 | switch (*next) { | |
465 | case '(': /* #2 */ | |
466 | if (top - op_stack > nr_parens) | |
467 | return ERR_PTR(-EINVAL); | |
468 | *(++top) = invert; | |
469 | continue; | |
470 | case '!': /* #3 */ | |
471 | if (!is_not(next)) | |
472 | break; | |
473 | invert = !invert; | |
474 | continue; | |
475 | } | |
476 | ||
477 | if (N >= nr_preds) { | |
478 | parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str); | |
479 | goto out_free; | |
480 | } | |
481 | ||
482 | inverts[N] = invert; /* #4 */ | |
483 | prog[N].target = N-1; | |
484 | ||
485 | len = parse_pred(next, data, ptr - str, pe, &prog[N].pred); | |
486 | if (len < 0) { | |
487 | ret = len; | |
488 | goto out_free; | |
489 | } | |
490 | ptr = next + len; | |
491 | ||
492 | N++; | |
493 | ||
494 | ret = -1; | |
495 | while (1) { /* #5 */ | |
496 | next = ptr++; | |
497 | if (isspace(*next)) | |
498 | continue; | |
499 | ||
500 | switch (*next) { | |
501 | case ')': | |
502 | case '\0': | |
503 | break; | |
504 | case '&': | |
505 | case '|': | |
506 | if (next[1] == next[0]) { | |
507 | ptr++; | |
508 | break; | |
509 | } | |
510 | default: | |
511 | parse_error(pe, FILT_ERR_TOO_MANY_PREDS, | |
512 | next - str); | |
513 | goto out_free; | |
514 | } | |
515 | ||
516 | invert = *top & INVERT; | |
517 | ||
518 | if (*top & PROCESS_AND) { /* #7 */ | |
519 | update_preds(prog, N - 1, invert); | |
520 | *top &= ~PROCESS_AND; | |
521 | } | |
522 | if (*next == '&') { /* #8 */ | |
523 | *top |= PROCESS_AND; | |
524 | break; | |
525 | } | |
526 | if (*top & PROCESS_OR) { /* #9 */ | |
527 | update_preds(prog, N - 1, !invert); | |
528 | *top &= ~PROCESS_OR; | |
529 | } | |
530 | if (*next == '|') { /* #10 */ | |
531 | *top |= PROCESS_OR; | |
532 | break; | |
533 | } | |
534 | if (!*next) /* #11 */ | |
535 | goto out; | |
536 | ||
537 | if (top == op_stack) { | |
538 | ret = -1; | |
539 | /* Too few '(' */ | |
540 | parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str); | |
541 | goto out_free; | |
542 | } | |
543 | top--; /* #12 */ | |
544 | } | |
545 | } | |
546 | out: | |
547 | if (top != op_stack) { | |
548 | /* Too many '(' */ | |
549 | parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str); | |
550 | goto out_free; | |
551 | } | |
552 | ||
553 | prog[N].pred = NULL; /* #13 */ | |
554 | prog[N].target = 1; /* TRUE */ | |
555 | prog[N+1].pred = NULL; | |
556 | prog[N+1].target = 0; /* FALSE */ | |
557 | prog[N-1].target = N; | |
558 | prog[N-1].when_to_branch = false; | |
559 | ||
560 | /* Second Pass */ | |
561 | for (i = N-1 ; i--; ) { | |
562 | int target = prog[i].target; | |
563 | if (prog[i].when_to_branch == prog[target].when_to_branch) | |
564 | prog[i].target = prog[target].target; | |
565 | } | |
566 | ||
567 | /* Third Pass */ | |
568 | for (i = 0; i < N; i++) { | |
569 | invert = inverts[i] ^ prog[i].when_to_branch; | |
570 | prog[i].when_to_branch = invert; | |
571 | /* Make sure the program always moves forward */ | |
572 | if (WARN_ON(prog[i].target <= i)) { | |
573 | ret = -EINVAL; | |
574 | goto out_free; | |
575 | } | |
576 | } | |
577 | ||
578 | return prog; | |
579 | out_free: | |
580 | kfree(op_stack); | |
581 | kfree(prog_stack); | |
582 | kfree(inverts); | |
583 | return ERR_PTR(ret); | |
584 | } | |
585 | ||
197e2eab | 586 | #define DEFINE_COMPARISON_PRED(type) \ |
fdf5b679 | 587 | static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \ |
197e2eab LZ |
588 | { \ |
589 | type *addr = (type *)(event + pred->offset); \ | |
590 | type val = (type)pred->val; \ | |
80765597 | 591 | return *addr < val; \ |
fdf5b679 SRRH |
592 | } \ |
593 | static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \ | |
594 | { \ | |
595 | type *addr = (type *)(event + pred->offset); \ | |
596 | type val = (type)pred->val; \ | |
80765597 | 597 | return *addr <= val; \ |
fdf5b679 SRRH |
598 | } \ |
599 | static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \ | |
600 | { \ | |
601 | type *addr = (type *)(event + pred->offset); \ | |
602 | type val = (type)pred->val; \ | |
80765597 | 603 | return *addr > val; \ |
fdf5b679 SRRH |
604 | } \ |
605 | static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \ | |
606 | { \ | |
607 | type *addr = (type *)(event + pred->offset); \ | |
608 | type val = (type)pred->val; \ | |
80765597 | 609 | return *addr >= val; \ |
fdf5b679 SRRH |
610 | } \ |
611 | static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \ | |
612 | { \ | |
613 | type *addr = (type *)(event + pred->offset); \ | |
614 | type val = (type)pred->val; \ | |
80765597 | 615 | return !!(*addr & val); \ |
fdf5b679 SRRH |
616 | } \ |
617 | static const filter_pred_fn_t pred_funcs_##type[] = { \ | |
fdf5b679 | 618 | filter_pred_LE_##type, \ |
80765597 | 619 | filter_pred_LT_##type, \ |
fdf5b679 | 620 | filter_pred_GE_##type, \ |
80765597 | 621 | filter_pred_GT_##type, \ |
fdf5b679 SRRH |
622 | filter_pred_BAND_##type, \ |
623 | }; | |
624 | ||
197e2eab | 625 | #define DEFINE_EQUALITY_PRED(size) \ |
58d9a597 | 626 | static int filter_pred_##size(struct filter_pred *pred, void *event) \ |
197e2eab LZ |
627 | { \ |
628 | u##size *addr = (u##size *)(event + pred->offset); \ | |
629 | u##size val = (u##size)pred->val; \ | |
630 | int match; \ | |
631 | \ | |
632 | match = (val == *addr) ^ pred->not; \ | |
633 | \ | |
634 | return match; \ | |
635 | } | |
636 | ||
8b372562 TZ |
637 | DEFINE_COMPARISON_PRED(s64); |
638 | DEFINE_COMPARISON_PRED(u64); | |
639 | DEFINE_COMPARISON_PRED(s32); | |
640 | DEFINE_COMPARISON_PRED(u32); | |
641 | DEFINE_COMPARISON_PRED(s16); | |
642 | DEFINE_COMPARISON_PRED(u16); | |
643 | DEFINE_COMPARISON_PRED(s8); | |
644 | DEFINE_COMPARISON_PRED(u8); | |
645 | ||
646 | DEFINE_EQUALITY_PRED(64); | |
647 | DEFINE_EQUALITY_PRED(32); | |
648 | DEFINE_EQUALITY_PRED(16); | |
649 | DEFINE_EQUALITY_PRED(8); | |
650 | ||
e8808c10 | 651 | /* Filter predicate for fixed sized arrays of characters */ |
58d9a597 | 652 | static int filter_pred_string(struct filter_pred *pred, void *event) |
7ce7e424 TZ |
653 | { |
654 | char *addr = (char *)(event + pred->offset); | |
655 | int cmp, match; | |
656 | ||
1889d209 | 657 | cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len); |
7ce7e424 | 658 | |
1889d209 | 659 | match = cmp ^ pred->not; |
7ce7e424 TZ |
660 | |
661 | return match; | |
662 | } | |
663 | ||
87a342f5 | 664 | /* Filter predicate for char * pointers */ |
58d9a597 | 665 | static int filter_pred_pchar(struct filter_pred *pred, void *event) |
87a342f5 LZ |
666 | { |
667 | char **addr = (char **)(event + pred->offset); | |
668 | int cmp, match; | |
16da27a8 | 669 | int len = strlen(*addr) + 1; /* including tailing '\0' */ |
87a342f5 | 670 | |
16da27a8 | 671 | cmp = pred->regex.match(*addr, &pred->regex, len); |
87a342f5 | 672 | |
1889d209 | 673 | match = cmp ^ pred->not; |
87a342f5 LZ |
674 | |
675 | return match; | |
676 | } | |
677 | ||
e8808c10 FW |
678 | /* |
679 | * Filter predicate for dynamic sized arrays of characters. | |
680 | * These are implemented through a list of strings at the end | |
681 | * of the entry. | |
682 | * Also each of these strings have a field in the entry which | |
683 | * contains its offset from the beginning of the entry. | |
684 | * We have then first to get this field, dereference it | |
685 | * and add it to the address of the entry, and at last we have | |
686 | * the address of the string. | |
687 | */ | |
58d9a597 | 688 | static int filter_pred_strloc(struct filter_pred *pred, void *event) |
e8808c10 | 689 | { |
7d536cb3 LZ |
690 | u32 str_item = *(u32 *)(event + pred->offset); |
691 | int str_loc = str_item & 0xffff; | |
692 | int str_len = str_item >> 16; | |
e8808c10 FW |
693 | char *addr = (char *)(event + str_loc); |
694 | int cmp, match; | |
695 | ||
1889d209 | 696 | cmp = pred->regex.match(addr, &pred->regex, str_len); |
e8808c10 | 697 | |
1889d209 | 698 | match = cmp ^ pred->not; |
e8808c10 FW |
699 | |
700 | return match; | |
701 | } | |
702 | ||
9f616680 DW |
703 | /* Filter predicate for CPUs. */ |
704 | static int filter_pred_cpu(struct filter_pred *pred, void *event) | |
705 | { | |
706 | int cpu, cmp; | |
9f616680 DW |
707 | |
708 | cpu = raw_smp_processor_id(); | |
709 | cmp = pred->val; | |
710 | ||
711 | switch (pred->op) { | |
712 | case OP_EQ: | |
80765597 SRV |
713 | return cpu == cmp; |
714 | case OP_NE: | |
715 | return cpu != cmp; | |
9f616680 | 716 | case OP_LT: |
80765597 | 717 | return cpu < cmp; |
9f616680 | 718 | case OP_LE: |
80765597 | 719 | return cpu <= cmp; |
9f616680 | 720 | case OP_GT: |
80765597 | 721 | return cpu > cmp; |
9f616680 | 722 | case OP_GE: |
80765597 | 723 | return cpu >= cmp; |
9f616680 | 724 | default: |
80765597 | 725 | return 0; |
9f616680 | 726 | } |
9f616680 DW |
727 | } |
728 | ||
729 | /* Filter predicate for COMM. */ | |
730 | static int filter_pred_comm(struct filter_pred *pred, void *event) | |
731 | { | |
80765597 | 732 | int cmp; |
9f616680 DW |
733 | |
734 | cmp = pred->regex.match(current->comm, &pred->regex, | |
80765597 SRV |
735 | TASK_COMM_LEN); |
736 | return cmp ^ pred->not; | |
9f616680 DW |
737 | } |
738 | ||
58d9a597 | 739 | static int filter_pred_none(struct filter_pred *pred, void *event) |
0a19e53c TZ |
740 | { |
741 | return 0; | |
742 | } | |
743 | ||
d1303dd1 LZ |
744 | /* |
745 | * regex_match_foo - Basic regex callbacks | |
746 | * | |
747 | * @str: the string to be searched | |
748 | * @r: the regex structure containing the pattern string | |
749 | * @len: the length of the string to be searched (including '\0') | |
750 | * | |
751 | * Note: | |
752 | * - @str might not be NULL-terminated if it's of type DYN_STRING | |
10f20e9f | 753 | * or STATIC_STRING, unless @len is zero. |
d1303dd1 LZ |
754 | */ |
755 | ||
1889d209 FW |
756 | static int regex_match_full(char *str, struct regex *r, int len) |
757 | { | |
10f20e9f SRV |
758 | /* len of zero means str is dynamic and ends with '\0' */ |
759 | if (!len) | |
760 | return strcmp(str, r->pattern) == 0; | |
761 | ||
762 | return strncmp(str, r->pattern, len) == 0; | |
1889d209 FW |
763 | } |
764 | ||
765 | static int regex_match_front(char *str, struct regex *r, int len) | |
766 | { | |
10f20e9f | 767 | if (len && len < r->len) |
dc432c3d SRV |
768 | return 0; |
769 | ||
10f20e9f | 770 | return strncmp(str, r->pattern, r->len) == 0; |
1889d209 FW |
771 | } |
772 | ||
773 | static int regex_match_middle(char *str, struct regex *r, int len) | |
774 | { | |
10f20e9f SRV |
775 | if (!len) |
776 | return strstr(str, r->pattern) != NULL; | |
777 | ||
778 | return strnstr(str, r->pattern, len) != NULL; | |
1889d209 FW |
779 | } |
780 | ||
781 | static int regex_match_end(char *str, struct regex *r, int len) | |
782 | { | |
a3291c14 | 783 | int strlen = len - 1; |
1889d209 | 784 | |
a3291c14 LZ |
785 | if (strlen >= r->len && |
786 | memcmp(str + strlen - r->len, r->pattern, r->len) == 0) | |
1889d209 FW |
787 | return 1; |
788 | return 0; | |
789 | } | |
790 | ||
60f1d5e3 MH |
791 | static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused) |
792 | { | |
793 | if (glob_match(r->pattern, str)) | |
794 | return 1; | |
795 | return 0; | |
796 | } | |
80765597 | 797 | |
3f6fe06d FW |
798 | /** |
799 | * filter_parse_regex - parse a basic regex | |
800 | * @buff: the raw regex | |
801 | * @len: length of the regex | |
802 | * @search: will point to the beginning of the string to compare | |
803 | * @not: tell whether the match will have to be inverted | |
804 | * | |
805 | * This passes in a buffer containing a regex and this function will | |
1889d209 FW |
806 | * set search to point to the search part of the buffer and |
807 | * return the type of search it is (see enum above). | |
808 | * This does modify buff. | |
809 | * | |
810 | * Returns enum type. | |
811 | * search returns the pointer to use for comparison. | |
812 | * not returns 1 if buff started with a '!' | |
813 | * 0 otherwise. | |
814 | */ | |
3f6fe06d | 815 | enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) |
1889d209 FW |
816 | { |
817 | int type = MATCH_FULL; | |
818 | int i; | |
819 | ||
820 | if (buff[0] == '!') { | |
821 | *not = 1; | |
822 | buff++; | |
823 | len--; | |
824 | } else | |
825 | *not = 0; | |
826 | ||
827 | *search = buff; | |
828 | ||
829 | for (i = 0; i < len; i++) { | |
830 | if (buff[i] == '*') { | |
831 | if (!i) { | |
1889d209 | 832 | type = MATCH_END_ONLY; |
60f1d5e3 | 833 | } else if (i == len - 1) { |
1889d209 FW |
834 | if (type == MATCH_END_ONLY) |
835 | type = MATCH_MIDDLE_ONLY; | |
836 | else | |
837 | type = MATCH_FRONT_ONLY; | |
838 | buff[i] = 0; | |
839 | break; | |
60f1d5e3 | 840 | } else { /* pattern continues, use full glob */ |
07234021 | 841 | return MATCH_GLOB; |
1889d209 | 842 | } |
60f1d5e3 | 843 | } else if (strchr("[?\\", buff[i])) { |
07234021 | 844 | return MATCH_GLOB; |
1889d209 FW |
845 | } |
846 | } | |
07234021 SRV |
847 | if (buff[0] == '*') |
848 | *search = buff + 1; | |
1889d209 FW |
849 | |
850 | return type; | |
851 | } | |
852 | ||
b0f1a59a | 853 | static void filter_build_regex(struct filter_pred *pred) |
1889d209 FW |
854 | { |
855 | struct regex *r = &pred->regex; | |
b0f1a59a LZ |
856 | char *search; |
857 | enum regex_type type = MATCH_FULL; | |
b0f1a59a LZ |
858 | |
859 | if (pred->op == OP_GLOB) { | |
80765597 | 860 | type = filter_parse_regex(r->pattern, r->len, &search, &pred->not); |
b0f1a59a LZ |
861 | r->len = strlen(search); |
862 | memmove(r->pattern, search, r->len+1); | |
863 | } | |
1889d209 FW |
864 | |
865 | switch (type) { | |
866 | case MATCH_FULL: | |
867 | r->match = regex_match_full; | |
868 | break; | |
869 | case MATCH_FRONT_ONLY: | |
870 | r->match = regex_match_front; | |
871 | break; | |
872 | case MATCH_MIDDLE_ONLY: | |
873 | r->match = regex_match_middle; | |
874 | break; | |
875 | case MATCH_END_ONLY: | |
876 | r->match = regex_match_end; | |
877 | break; | |
60f1d5e3 MH |
878 | case MATCH_GLOB: |
879 | r->match = regex_match_glob; | |
880 | break; | |
1889d209 | 881 | } |
f30120fc JO |
882 | } |
883 | ||
7ce7e424 | 884 | /* return 1 if event matches, 0 otherwise (discard) */ |
6fb2915d | 885 | int filter_match_preds(struct event_filter *filter, void *rec) |
7ce7e424 | 886 | { |
80765597 SRV |
887 | struct prog_entry *prog; |
888 | int i; | |
7ce7e424 | 889 | |
6d54057d | 890 | /* no filter is considered a match */ |
75b8e982 SR |
891 | if (!filter) |
892 | return 1; | |
893 | ||
80765597 SRV |
894 | prog = rcu_dereference_sched(filter->prog); |
895 | if (!prog) | |
6d54057d SR |
896 | return 1; |
897 | ||
80765597 SRV |
898 | for (i = 0; prog[i].pred; i++) { |
899 | struct filter_pred *pred = prog[i].pred; | |
900 | int match = pred->fn(pred, rec); | |
901 | if (match == prog[i].when_to_branch) | |
902 | i = prog[i].target; | |
903 | } | |
904 | return prog[i].target; | |
7ce7e424 | 905 | } |
17c873ec | 906 | EXPORT_SYMBOL_GPL(filter_match_preds); |
7ce7e424 | 907 | |
8b372562 TZ |
908 | static void remove_filter_string(struct event_filter *filter) |
909 | { | |
75b8e982 SR |
910 | if (!filter) |
911 | return; | |
912 | ||
8b372562 TZ |
913 | kfree(filter->filter_string); |
914 | filter->filter_string = NULL; | |
915 | } | |
916 | ||
80765597 | 917 | static void append_filter_err(struct filter_parse_error *pe, |
8b372562 TZ |
918 | struct event_filter *filter) |
919 | { | |
559d4212 | 920 | struct trace_seq *s; |
80765597 | 921 | int pos = pe->lasterr_pos; |
559d4212 SRV |
922 | char *buf; |
923 | int len; | |
8b372562 | 924 | |
559d4212 | 925 | if (WARN_ON(!filter->filter_string)) |
4bda2d51 | 926 | return; |
7ce7e424 | 927 | |
559d4212 SRV |
928 | s = kmalloc(sizeof(*s), GFP_KERNEL); |
929 | if (!s) | |
930 | return; | |
931 | trace_seq_init(s); | |
932 | ||
933 | len = strlen(filter->filter_string); | |
934 | if (pos > len) | |
80765597 SRV |
935 | pos = len; |
936 | ||
937 | /* indexing is off by one */ | |
938 | if (pos) | |
939 | pos++; | |
559d4212 SRV |
940 | |
941 | trace_seq_puts(s, filter->filter_string); | |
80765597 SRV |
942 | if (pe->lasterr > 0) { |
943 | trace_seq_printf(s, "\n%*s", pos, "^"); | |
944 | trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]); | |
945 | } else { | |
946 | trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr); | |
947 | } | |
559d4212 SRV |
948 | trace_seq_putc(s, 0); |
949 | buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL); | |
950 | if (buf) { | |
951 | kfree(filter->filter_string); | |
952 | filter->filter_string = buf; | |
953 | } | |
954 | kfree(s); | |
7ce7e424 TZ |
955 | } |
956 | ||
7f1d2f82 | 957 | static inline struct event_filter *event_filter(struct trace_event_file *file) |
f306cc82 | 958 | { |
dcb0b557 | 959 | return file->filter; |
f306cc82 TZ |
960 | } |
961 | ||
e2912b09 | 962 | /* caller must hold event_mutex */ |
7f1d2f82 | 963 | void print_event_filter(struct trace_event_file *file, struct trace_seq *s) |
ac1adc55 | 964 | { |
f306cc82 | 965 | struct event_filter *filter = event_filter(file); |
8b372562 | 966 | |
8e254c1d | 967 | if (filter && filter->filter_string) |
8b372562 TZ |
968 | trace_seq_printf(s, "%s\n", filter->filter_string); |
969 | else | |
146c3442 | 970 | trace_seq_puts(s, "none\n"); |
ac1adc55 TZ |
971 | } |
972 | ||
8b372562 | 973 | void print_subsystem_event_filter(struct event_subsystem *system, |
ac1adc55 TZ |
974 | struct trace_seq *s) |
975 | { | |
75b8e982 | 976 | struct event_filter *filter; |
8b372562 | 977 | |
00e95830 | 978 | mutex_lock(&event_mutex); |
75b8e982 | 979 | filter = system->filter; |
8e254c1d | 980 | if (filter && filter->filter_string) |
8b372562 TZ |
981 | trace_seq_printf(s, "%s\n", filter->filter_string); |
982 | else | |
146c3442 | 983 | trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n"); |
00e95830 | 984 | mutex_unlock(&event_mutex); |
ac1adc55 TZ |
985 | } |
986 | ||
80765597 | 987 | static void free_prog(struct event_filter *filter) |
c9c53ca0 | 988 | { |
80765597 | 989 | struct prog_entry *prog; |
60705c89 SRRH |
990 | int i; |
991 | ||
80765597 SRV |
992 | prog = rcu_access_pointer(filter->prog); |
993 | if (!prog) | |
994 | return; | |
995 | ||
996 | for (i = 0; prog[i].pred; i++) | |
997 | kfree(prog[i].pred); | |
998 | kfree(prog); | |
c9c53ca0 SR |
999 | } |
1000 | ||
7f1d2f82 | 1001 | static void filter_disable(struct trace_event_file *file) |
f306cc82 | 1002 | { |
0fc1b09f SRRH |
1003 | unsigned long old_flags = file->flags; |
1004 | ||
dcb0b557 | 1005 | file->flags &= ~EVENT_FILE_FL_FILTERED; |
0fc1b09f SRRH |
1006 | |
1007 | if (old_flags != file->flags) | |
1008 | trace_buffered_event_disable(); | |
f306cc82 TZ |
1009 | } |
1010 | ||
c9c53ca0 | 1011 | static void __free_filter(struct event_filter *filter) |
2df75e41 | 1012 | { |
8e254c1d LZ |
1013 | if (!filter) |
1014 | return; | |
1015 | ||
80765597 | 1016 | free_prog(filter); |
57be8887 | 1017 | kfree(filter->filter_string); |
2df75e41 | 1018 | kfree(filter); |
6fb2915d LZ |
1019 | } |
1020 | ||
bac5fb97 TZ |
1021 | void free_event_filter(struct event_filter *filter) |
1022 | { | |
1023 | __free_filter(filter); | |
1024 | } | |
1025 | ||
7f1d2f82 | 1026 | static inline void __remove_filter(struct trace_event_file *file) |
8e254c1d | 1027 | { |
f306cc82 | 1028 | filter_disable(file); |
dcb0b557 | 1029 | remove_filter_string(file->filter); |
f306cc82 TZ |
1030 | } |
1031 | ||
7967b3e0 | 1032 | static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir, |
f306cc82 TZ |
1033 | struct trace_array *tr) |
1034 | { | |
7f1d2f82 | 1035 | struct trace_event_file *file; |
8e254c1d | 1036 | |
f306cc82 | 1037 | list_for_each_entry(file, &tr->events, list) { |
bb9ef1cb | 1038 | if (file->system != dir) |
8e254c1d | 1039 | continue; |
f306cc82 | 1040 | __remove_filter(file); |
8e254c1d | 1041 | } |
8e254c1d | 1042 | } |
7ce7e424 | 1043 | |
7f1d2f82 | 1044 | static inline void __free_subsystem_filter(struct trace_event_file *file) |
cfb180f3 | 1045 | { |
dcb0b557 SRRH |
1046 | __free_filter(file->filter); |
1047 | file->filter = NULL; | |
f306cc82 TZ |
1048 | } |
1049 | ||
7967b3e0 | 1050 | static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir, |
f306cc82 TZ |
1051 | struct trace_array *tr) |
1052 | { | |
7f1d2f82 | 1053 | struct trace_event_file *file; |
cfb180f3 | 1054 | |
f306cc82 | 1055 | list_for_each_entry(file, &tr->events, list) { |
80765597 | 1056 | if (file->system != dir) |
7ce7e424 | 1057 | continue; |
80765597 | 1058 | __free_subsystem_filter(file); |
8b372562 | 1059 | } |
80765597 | 1060 | } |
8b372562 | 1061 | |
80765597 SRV |
1062 | int filter_assign_type(const char *type) |
1063 | { | |
1064 | if (strstr(type, "__data_loc") && strstr(type, "char")) | |
1065 | return FILTER_DYN_STRING; | |
8b372562 | 1066 | |
80765597 SRV |
1067 | if (strchr(type, '[') && strstr(type, "char")) |
1068 | return FILTER_STATIC_STRING; | |
8b372562 | 1069 | |
80765597 | 1070 | return FILTER_OTHER; |
8b372562 TZ |
1071 | } |
1072 | ||
80765597 SRV |
1073 | static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op, |
1074 | int field_size, int field_is_signed) | |
8b372562 | 1075 | { |
80765597 SRV |
1076 | filter_pred_fn_t fn = NULL; |
1077 | int pred_func_index = -1; | |
81570d9c | 1078 | |
80765597 SRV |
1079 | switch (op) { |
1080 | case OP_EQ: | |
1081 | case OP_NE: | |
1082 | break; | |
1083 | default: | |
1084 | if (WARN_ON_ONCE(op < PRED_FUNC_START)) | |
1085 | return NULL; | |
1086 | pred_func_index = op - PRED_FUNC_START; | |
1087 | if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX)) | |
1088 | return NULL; | |
8b372562 TZ |
1089 | } |
1090 | ||
80765597 SRV |
1091 | switch (field_size) { |
1092 | case 8: | |
1093 | if (pred_func_index < 0) | |
1094 | fn = filter_pred_64; | |
1095 | else if (field_is_signed) | |
1096 | fn = pred_funcs_s64[pred_func_index]; | |
1097 | else | |
1098 | fn = pred_funcs_u64[pred_func_index]; | |
1099 | break; | |
1100 | case 4: | |
1101 | if (pred_func_index < 0) | |
1102 | fn = filter_pred_32; | |
1103 | else if (field_is_signed) | |
1104 | fn = pred_funcs_s32[pred_func_index]; | |
1105 | else | |
1106 | fn = pred_funcs_u32[pred_func_index]; | |
1107 | break; | |
1108 | case 2: | |
1109 | if (pred_func_index < 0) | |
1110 | fn = filter_pred_16; | |
1111 | else if (field_is_signed) | |
1112 | fn = pred_funcs_s16[pred_func_index]; | |
1113 | else | |
1114 | fn = pred_funcs_u16[pred_func_index]; | |
1115 | break; | |
1116 | case 1: | |
1117 | if (pred_func_index < 0) | |
1118 | fn = filter_pred_8; | |
1119 | else if (field_is_signed) | |
1120 | fn = pred_funcs_s8[pred_func_index]; | |
1121 | else | |
1122 | fn = pred_funcs_u8[pred_func_index]; | |
1123 | break; | |
61aaef55 | 1124 | } |
8b372562 | 1125 | |
80765597 | 1126 | return fn; |
8b372562 TZ |
1127 | } |
1128 | ||
80765597 SRV |
1129 | /* Called when a predicate is encountered by predicate_parse() */ |
1130 | static int parse_pred(const char *str, void *data, | |
1131 | int pos, struct filter_parse_error *pe, | |
1132 | struct filter_pred **pred_ptr) | |
8b372562 | 1133 | { |
80765597 SRV |
1134 | struct trace_event_call *call = data; |
1135 | struct ftrace_event_field *field; | |
1136 | struct filter_pred *pred = NULL; | |
1137 | char num_buf[24]; /* Big enough to hold an address */ | |
1138 | char *field_name; | |
1139 | char q; | |
1140 | u64 val; | |
1141 | int len; | |
1142 | int ret; | |
1143 | int op; | |
1144 | int s; | |
1145 | int i = 0; | |
8b372562 | 1146 | |
80765597 SRV |
1147 | /* First find the field to associate to */ |
1148 | while (isspace(str[i])) | |
1149 | i++; | |
1150 | s = i; | |
8b372562 | 1151 | |
80765597 SRV |
1152 | while (isalnum(str[i]) || str[i] == '_') |
1153 | i++; | |
1154 | ||
1155 | len = i - s; | |
1156 | ||
1157 | if (!len) | |
1158 | return -1; | |
1159 | ||
1160 | field_name = kmemdup_nul(str + s, len, GFP_KERNEL); | |
1161 | if (!field_name) | |
1162 | return -ENOMEM; | |
1163 | ||
1164 | /* Make sure that the field exists */ | |
7ce7e424 | 1165 | |
80765597 SRV |
1166 | field = trace_find_event_field(call, field_name); |
1167 | kfree(field_name); | |
1168 | if (!field) { | |
1169 | parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i); | |
bcabd91c LZ |
1170 | return -EINVAL; |
1171 | } | |
1172 | ||
80765597 SRV |
1173 | while (isspace(str[i])) |
1174 | i++; | |
f66578a7 | 1175 | |
80765597 SRV |
1176 | /* Make sure this op is supported */ |
1177 | for (op = 0; ops[op]; op++) { | |
1178 | /* This is why '<=' must come before '<' in ops[] */ | |
1179 | if (strncmp(str + i, ops[op], strlen(ops[op])) == 0) | |
1180 | break; | |
1181 | } | |
c9c53ca0 | 1182 | |
80765597 SRV |
1183 | if (!ops[op]) { |
1184 | parse_error(pe, FILT_ERR_INVALID_OP, pos + i); | |
1185 | goto err_free; | |
c9c53ca0 SR |
1186 | } |
1187 | ||
80765597 | 1188 | i += strlen(ops[op]); |
c9c53ca0 | 1189 | |
80765597 SRV |
1190 | while (isspace(str[i])) |
1191 | i++; | |
f03f5979 | 1192 | |
80765597 | 1193 | s = i; |
f03f5979 | 1194 | |
80765597 SRV |
1195 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); |
1196 | if (!pred) | |
1197 | return -ENOMEM; | |
f03f5979 | 1198 | |
80765597 SRV |
1199 | pred->field = field; |
1200 | pred->offset = field->offset; | |
1201 | pred->op = op; | |
1202 | ||
1203 | if (ftrace_event_is_function(call)) { | |
f03f5979 | 1204 | /* |
80765597 SRV |
1205 | * Perf does things different with function events. |
1206 | * It only allows an "ip" field, and expects a string. | |
1207 | * But the string does not need to be surrounded by quotes. | |
1208 | * If it is a string, the assigned function as a nop, | |
1209 | * (perf doesn't use it) and grab everything. | |
f03f5979 | 1210 | */ |
80765597 SRV |
1211 | if (strcmp(field->name, "ip") != 0) { |
1212 | parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i); | |
1213 | goto err_free; | |
1214 | } | |
1215 | pred->fn = filter_pred_none; | |
1216 | ||
1217 | /* | |
1218 | * Quotes are not required, but if they exist then we need | |
1219 | * to read them till we hit a matching one. | |
1220 | */ | |
1221 | if (str[i] == '\'' || str[i] == '"') | |
1222 | q = str[i]; | |
1223 | else | |
1224 | q = 0; | |
1225 | ||
1226 | for (i++; str[i]; i++) { | |
1227 | if (q && str[i] == q) | |
1228 | break; | |
1229 | if (!q && (str[i] == ')' || str[i] == '&' || | |
1230 | str[i] == '|')) | |
1231 | break; | |
1232 | } | |
1233 | /* Skip quotes */ | |
1234 | if (q) | |
1235 | s++; | |
1236 | len = i - s; | |
1237 | if (len >= MAX_FILTER_STR_VAL) { | |
1238 | parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); | |
1239 | goto err_free; | |
1240 | } | |
ec126cac | 1241 | |
80765597 SRV |
1242 | pred->regex.len = len; |
1243 | strncpy(pred->regex.pattern, str + s, len); | |
1244 | pred->regex.pattern[len] = 0; | |
1245 | ||
1246 | /* This is either a string, or an integer */ | |
1247 | } else if (str[i] == '\'' || str[i] == '"') { | |
1248 | char q = str[i]; | |
1249 | ||
1250 | /* Make sure the op is OK for strings */ | |
1251 | switch (op) { | |
1252 | case OP_NE: | |
1253 | pred->not = 1; | |
1254 | /* Fall through */ | |
1255 | case OP_GLOB: | |
1256 | case OP_EQ: | |
1257 | break; | |
1258 | default: | |
1259 | parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); | |
1260 | goto err_free; | |
1261 | } | |
ec126cac | 1262 | |
80765597 SRV |
1263 | /* Make sure the field is OK for strings */ |
1264 | if (!is_string_field(field)) { | |
1265 | parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i); | |
1266 | goto err_free; | |
1267 | } | |
43cd4145 | 1268 | |
80765597 SRV |
1269 | for (i++; str[i]; i++) { |
1270 | if (str[i] == q) | |
1271 | break; | |
1272 | } | |
1273 | if (!str[i]) { | |
1274 | parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i); | |
1275 | goto err_free; | |
1276 | } | |
43cd4145 | 1277 | |
80765597 SRV |
1278 | /* Skip quotes */ |
1279 | s++; | |
1280 | len = i - s; | |
1281 | if (len >= MAX_FILTER_STR_VAL) { | |
1282 | parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); | |
1283 | goto err_free; | |
1284 | } | |
c00b060f | 1285 | |
80765597 SRV |
1286 | pred->regex.len = len; |
1287 | strncpy(pred->regex.pattern, str + s, len); | |
1288 | pred->regex.pattern[len] = 0; | |
43cd4145 | 1289 | |
80765597 | 1290 | filter_build_regex(pred); |
43cd4145 | 1291 | |
80765597 SRV |
1292 | if (field->filter_type == FILTER_COMM) { |
1293 | pred->fn = filter_pred_comm; | |
96bc293a | 1294 | |
80765597 SRV |
1295 | } else if (field->filter_type == FILTER_STATIC_STRING) { |
1296 | pred->fn = filter_pred_string; | |
1297 | pred->regex.field_len = field->size; | |
96bc293a | 1298 | |
80765597 SRV |
1299 | } else if (field->filter_type == FILTER_DYN_STRING) |
1300 | pred->fn = filter_pred_strloc; | |
1301 | else | |
1302 | pred->fn = filter_pred_pchar; | |
1303 | /* go past the last quote */ | |
1304 | i++; | |
96bc293a | 1305 | |
80765597 | 1306 | } else if (isdigit(str[i])) { |
96bc293a | 1307 | |
80765597 SRV |
1308 | /* Make sure the field is not a string */ |
1309 | if (is_string_field(field)) { | |
1310 | parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i); | |
1311 | goto err_free; | |
1312 | } | |
96bc293a | 1313 | |
80765597 SRV |
1314 | if (op == OP_GLOB) { |
1315 | parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i); | |
1316 | goto err_free; | |
1317 | } | |
43cd4145 | 1318 | |
80765597 SRV |
1319 | /* We allow 0xDEADBEEF */ |
1320 | while (isalnum(str[i])) | |
1321 | i++; | |
43cd4145 | 1322 | |
80765597 SRV |
1323 | len = i - s; |
1324 | /* 0xfeedfacedeadbeef is 18 chars max */ | |
1325 | if (len >= sizeof(num_buf)) { | |
1326 | parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i); | |
1327 | goto err_free; | |
1328 | } | |
43cd4145 | 1329 | |
80765597 SRV |
1330 | strncpy(num_buf, str + s, len); |
1331 | num_buf[len] = 0; | |
43cd4145 | 1332 | |
80765597 SRV |
1333 | /* Make sure it is a value */ |
1334 | if (field->is_signed) | |
1335 | ret = kstrtoll(num_buf, 0, &val); | |
1336 | else | |
1337 | ret = kstrtoull(num_buf, 0, &val); | |
1338 | if (ret) { | |
1339 | parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s); | |
1340 | goto err_free; | |
1341 | } | |
43cd4145 | 1342 | |
80765597 | 1343 | pred->val = val; |
43cd4145 | 1344 | |
80765597 SRV |
1345 | if (field->filter_type == FILTER_CPU) |
1346 | pred->fn = filter_pred_cpu; | |
1347 | else { | |
1348 | pred->fn = select_comparison_fn(pred->op, field->size, | |
1349 | field->is_signed); | |
1350 | if (pred->op == OP_NE) | |
1351 | pred->not = 1; | |
1352 | } | |
1b797fe5 | 1353 | |
80765597 SRV |
1354 | } else { |
1355 | parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i); | |
1356 | goto err_free; | |
1357 | } | |
1b797fe5 | 1358 | |
80765597 SRV |
1359 | *pred_ptr = pred; |
1360 | return i; | |
1b797fe5 | 1361 | |
80765597 SRV |
1362 | err_free: |
1363 | kfree(pred); | |
1364 | return -EINVAL; | |
1b797fe5 JO |
1365 | } |
1366 | ||
80765597 SRV |
1367 | enum { |
1368 | TOO_MANY_CLOSE = -1, | |
1369 | TOO_MANY_OPEN = -2, | |
1370 | MISSING_QUOTE = -3, | |
1371 | }; | |
1372 | ||
43cd4145 | 1373 | /* |
80765597 SRV |
1374 | * Read the filter string once to calculate the number of predicates |
1375 | * as well as how deep the parentheses go. | |
1376 | * | |
1377 | * Returns: | |
1378 | * 0 - everything is fine (err is undefined) | |
1379 | * -1 - too many ')' | |
1380 | * -2 - too many '(' | |
1381 | * -3 - No matching quote | |
43cd4145 | 1382 | */ |
80765597 SRV |
1383 | static int calc_stack(const char *str, int *parens, int *preds, int *err) |
1384 | { | |
1385 | bool is_pred = false; | |
1386 | int nr_preds = 0; | |
1387 | int open = 1; /* Count the expression as "(E)" */ | |
1388 | int last_quote = 0; | |
1389 | int max_open = 1; | |
1390 | int quote = 0; | |
1391 | int i; | |
8b372562 | 1392 | |
80765597 | 1393 | *err = 0; |
c9c53ca0 | 1394 | |
80765597 SRV |
1395 | for (i = 0; str[i]; i++) { |
1396 | if (isspace(str[i])) | |
1397 | continue; | |
1398 | if (quote) { | |
1399 | if (str[i] == quote) | |
1400 | quote = 0; | |
8b372562 TZ |
1401 | continue; |
1402 | } | |
1403 | ||
80765597 SRV |
1404 | switch (str[i]) { |
1405 | case '\'': | |
1406 | case '"': | |
1407 | quote = str[i]; | |
1408 | last_quote = i; | |
1409 | break; | |
1410 | case '|': | |
1411 | case '&': | |
1412 | if (str[i+1] != str[i]) | |
1413 | break; | |
1414 | is_pred = false; | |
1415 | continue; | |
1416 | case '(': | |
1417 | is_pred = false; | |
1418 | open++; | |
1419 | if (open > max_open) | |
1420 | max_open = open; | |
1421 | continue; | |
1422 | case ')': | |
1423 | is_pred = false; | |
1424 | if (open == 1) { | |
1425 | *err = i; | |
1426 | return TOO_MANY_CLOSE; | |
e12c09cf | 1427 | } |
80765597 | 1428 | open--; |
e12c09cf SRRH |
1429 | continue; |
1430 | } | |
80765597 SRV |
1431 | if (!is_pred) { |
1432 | nr_preds++; | |
1433 | is_pred = true; | |
1f9963cb | 1434 | } |
80765597 | 1435 | } |
1f9963cb | 1436 | |
80765597 SRV |
1437 | if (quote) { |
1438 | *err = last_quote; | |
1439 | return MISSING_QUOTE; | |
1440 | } | |
61aaef55 | 1441 | |
80765597 SRV |
1442 | if (open != 1) { |
1443 | int level = open; | |
8b372562 | 1444 | |
80765597 SRV |
1445 | /* find the bad open */ |
1446 | for (i--; i; i--) { | |
1447 | if (quote) { | |
1448 | if (str[i] == quote) | |
1449 | quote = 0; | |
1450 | continue; | |
1451 | } | |
1452 | switch (str[i]) { | |
1453 | case '(': | |
1454 | if (level == open) { | |
1455 | *err = i; | |
1456 | return TOO_MANY_OPEN; | |
1457 | } | |
1458 | level--; | |
1459 | break; | |
1460 | case ')': | |
1461 | level++; | |
1462 | break; | |
1463 | case '\'': | |
1464 | case '"': | |
1465 | quote = str[i]; | |
1466 | break; | |
1467 | } | |
1468 | } | |
1469 | /* First character is the '(' with missing ')' */ | |
1470 | *err = 0; | |
1471 | return TOO_MANY_OPEN; | |
8b372562 | 1472 | } |
7ce7e424 | 1473 | |
80765597 SRV |
1474 | /* Set the size of the required stacks */ |
1475 | *parens = max_open; | |
1476 | *preds = nr_preds; | |
1477 | return 0; | |
1478 | } | |
1479 | ||
1480 | static int process_preds(struct trace_event_call *call, | |
1481 | const char *filter_string, | |
1482 | struct event_filter *filter, | |
1483 | struct filter_parse_error *pe) | |
1484 | { | |
1485 | struct prog_entry *prog; | |
1486 | int nr_parens; | |
1487 | int nr_preds; | |
1488 | int index; | |
1489 | int ret; | |
1490 | ||
1491 | ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index); | |
1492 | if (ret < 0) { | |
1493 | switch (ret) { | |
1494 | case MISSING_QUOTE: | |
1495 | parse_error(pe, FILT_ERR_MISSING_QUOTE, index); | |
1496 | break; | |
1497 | case TOO_MANY_OPEN: | |
1498 | parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index); | |
1499 | break; | |
1500 | default: | |
1501 | parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index); | |
61e9dea2 | 1502 | } |
80765597 | 1503 | return ret; |
61e9dea2 SR |
1504 | } |
1505 | ||
ba16293d RB |
1506 | if (!nr_preds) |
1507 | return -EINVAL; | |
1508 | ||
1509 | prog = predicate_parse(filter_string, nr_parens, nr_preds, | |
80765597 | 1510 | parse_pred, call, pe); |
ba16293d RB |
1511 | if (IS_ERR(prog)) |
1512 | return PTR_ERR(prog); | |
1513 | ||
80765597 SRV |
1514 | rcu_assign_pointer(filter->prog, prog); |
1515 | return 0; | |
7ce7e424 TZ |
1516 | } |
1517 | ||
7f1d2f82 | 1518 | static inline void event_set_filtered_flag(struct trace_event_file *file) |
f306cc82 | 1519 | { |
0fc1b09f SRRH |
1520 | unsigned long old_flags = file->flags; |
1521 | ||
dcb0b557 | 1522 | file->flags |= EVENT_FILE_FL_FILTERED; |
0fc1b09f SRRH |
1523 | |
1524 | if (old_flags != file->flags) | |
1525 | trace_buffered_event_enable(); | |
f306cc82 TZ |
1526 | } |
1527 | ||
7f1d2f82 | 1528 | static inline void event_set_filter(struct trace_event_file *file, |
f306cc82 TZ |
1529 | struct event_filter *filter) |
1530 | { | |
dcb0b557 | 1531 | rcu_assign_pointer(file->filter, filter); |
f306cc82 TZ |
1532 | } |
1533 | ||
7f1d2f82 | 1534 | static inline void event_clear_filter(struct trace_event_file *file) |
f306cc82 | 1535 | { |
dcb0b557 | 1536 | RCU_INIT_POINTER(file->filter, NULL); |
f306cc82 TZ |
1537 | } |
1538 | ||
1539 | static inline void | |
7f1d2f82 | 1540 | event_set_no_set_filter_flag(struct trace_event_file *file) |
f306cc82 | 1541 | { |
dcb0b557 | 1542 | file->flags |= EVENT_FILE_FL_NO_SET_FILTER; |
f306cc82 TZ |
1543 | } |
1544 | ||
1545 | static inline void | |
7f1d2f82 | 1546 | event_clear_no_set_filter_flag(struct trace_event_file *file) |
f306cc82 | 1547 | { |
dcb0b557 | 1548 | file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER; |
f306cc82 TZ |
1549 | } |
1550 | ||
1551 | static inline bool | |
7f1d2f82 | 1552 | event_no_set_filter_flag(struct trace_event_file *file) |
f306cc82 | 1553 | { |
5d6ad960 | 1554 | if (file->flags & EVENT_FILE_FL_NO_SET_FILTER) |
f306cc82 TZ |
1555 | return true; |
1556 | ||
f306cc82 TZ |
1557 | return false; |
1558 | } | |
1559 | ||
75b8e982 SR |
1560 | struct filter_list { |
1561 | struct list_head list; | |
1562 | struct event_filter *filter; | |
1563 | }; | |
1564 | ||
80765597 | 1565 | static int process_system_preds(struct trace_subsystem_dir *dir, |
f306cc82 | 1566 | struct trace_array *tr, |
80765597 | 1567 | struct filter_parse_error *pe, |
fce29d15 LZ |
1568 | char *filter_string) |
1569 | { | |
7f1d2f82 | 1570 | struct trace_event_file *file; |
75b8e982 | 1571 | struct filter_list *filter_item; |
404a3add | 1572 | struct event_filter *filter = NULL; |
75b8e982 SR |
1573 | struct filter_list *tmp; |
1574 | LIST_HEAD(filter_list); | |
fce29d15 | 1575 | bool fail = true; |
a66abe7f | 1576 | int err; |
fce29d15 | 1577 | |
f306cc82 | 1578 | list_for_each_entry(file, &tr->events, list) { |
0fc3ca9a | 1579 | |
bb9ef1cb | 1580 | if (file->system != dir) |
0fc3ca9a SR |
1581 | continue; |
1582 | ||
404a3add SRV |
1583 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); |
1584 | if (!filter) | |
75b8e982 | 1585 | goto fail_mem; |
0fc3ca9a | 1586 | |
567f6989 SRV |
1587 | filter->filter_string = kstrdup(filter_string, GFP_KERNEL); |
1588 | if (!filter->filter_string) | |
75b8e982 | 1589 | goto fail_mem; |
fce29d15 | 1590 | |
80765597 | 1591 | err = process_preds(file->event_call, filter_string, filter, pe); |
75b8e982 | 1592 | if (err) { |
f306cc82 | 1593 | filter_disable(file); |
80765597 SRV |
1594 | parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0); |
1595 | append_filter_err(pe, filter); | |
75b8e982 | 1596 | } else |
f306cc82 | 1597 | event_set_filtered_flag(file); |
404a3add SRV |
1598 | |
1599 | ||
1600 | filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL); | |
1601 | if (!filter_item) | |
1602 | goto fail_mem; | |
1603 | ||
1604 | list_add_tail(&filter_item->list, &filter_list); | |
75b8e982 SR |
1605 | /* |
1606 | * Regardless of if this returned an error, we still | |
1607 | * replace the filter for the call. | |
1608 | */ | |
404a3add SRV |
1609 | filter_item->filter = event_filter(file); |
1610 | event_set_filter(file, filter); | |
1611 | filter = NULL; | |
75b8e982 | 1612 | |
fce29d15 LZ |
1613 | fail = false; |
1614 | } | |
1615 | ||
0fc3ca9a SR |
1616 | if (fail) |
1617 | goto fail; | |
1618 | ||
75b8e982 SR |
1619 | /* |
1620 | * The calls can still be using the old filters. | |
1621 | * Do a synchronize_sched() to ensure all calls are | |
1622 | * done with them before we free them. | |
1623 | */ | |
1624 | synchronize_sched(); | |
1625 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { | |
1626 | __free_filter(filter_item->filter); | |
1627 | list_del(&filter_item->list); | |
1628 | kfree(filter_item); | |
1629 | } | |
fce29d15 | 1630 | return 0; |
0fc3ca9a | 1631 | fail: |
75b8e982 SR |
1632 | /* No call succeeded */ |
1633 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { | |
1634 | list_del(&filter_item->list); | |
1635 | kfree(filter_item); | |
1636 | } | |
80765597 | 1637 | parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0); |
0fc3ca9a | 1638 | return -EINVAL; |
75b8e982 | 1639 | fail_mem: |
404a3add | 1640 | kfree(filter); |
75b8e982 SR |
1641 | /* If any call succeeded, we still need to sync */ |
1642 | if (!fail) | |
1643 | synchronize_sched(); | |
1644 | list_for_each_entry_safe(filter_item, tmp, &filter_list, list) { | |
1645 | __free_filter(filter_item->filter); | |
1646 | list_del(&filter_item->list); | |
1647 | kfree(filter_item); | |
1648 | } | |
1649 | return -ENOMEM; | |
fce29d15 LZ |
1650 | } |
1651 | ||
567f6989 | 1652 | static int create_filter_start(char *filter_string, bool set_str, |
80765597 | 1653 | struct filter_parse_error **pse, |
38b78eb8 TH |
1654 | struct event_filter **filterp) |
1655 | { | |
1656 | struct event_filter *filter; | |
80765597 | 1657 | struct filter_parse_error *pe = NULL; |
38b78eb8 TH |
1658 | int err = 0; |
1659 | ||
80765597 SRV |
1660 | if (WARN_ON_ONCE(*pse || *filterp)) |
1661 | return -EINVAL; | |
38b78eb8 | 1662 | |
c7399708 | 1663 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); |
567f6989 SRV |
1664 | if (filter && set_str) { |
1665 | filter->filter_string = kstrdup(filter_string, GFP_KERNEL); | |
1666 | if (!filter->filter_string) | |
1667 | err = -ENOMEM; | |
1668 | } | |
38b78eb8 | 1669 | |
80765597 | 1670 | pe = kzalloc(sizeof(*pe), GFP_KERNEL); |
38b78eb8 | 1671 | |
80765597 SRV |
1672 | if (!filter || !pe || err) { |
1673 | kfree(pe); | |
38b78eb8 TH |
1674 | __free_filter(filter); |
1675 | return -ENOMEM; | |
1676 | } | |
1677 | ||
1678 | /* we're committed to creating a new filter */ | |
1679 | *filterp = filter; | |
80765597 | 1680 | *pse = pe; |
38b78eb8 | 1681 | |
80765597 | 1682 | return 0; |
38b78eb8 TH |
1683 | } |
1684 | ||
80765597 | 1685 | static void create_filter_finish(struct filter_parse_error *pe) |
38b78eb8 | 1686 | { |
80765597 | 1687 | kfree(pe); |
38b78eb8 TH |
1688 | } |
1689 | ||
1690 | /** | |
2425bcb9 SRRH |
1691 | * create_filter - create a filter for a trace_event_call |
1692 | * @call: trace_event_call to create a filter for | |
38b78eb8 TH |
1693 | * @filter_str: filter string |
1694 | * @set_str: remember @filter_str and enable detailed error in filter | |
1695 | * @filterp: out param for created filter (always updated on return) | |
1696 | * | |
1697 | * Creates a filter for @call with @filter_str. If @set_str is %true, | |
1698 | * @filter_str is copied and recorded in the new filter. | |
1699 | * | |
1700 | * On success, returns 0 and *@filterp points to the new filter. On | |
1701 | * failure, returns -errno and *@filterp may point to %NULL or to a new | |
1702 | * filter. In the latter case, the returned filter contains error | |
1703 | * information if @set_str is %true and the caller is responsible for | |
1704 | * freeing it. | |
1705 | */ | |
2425bcb9 | 1706 | static int create_filter(struct trace_event_call *call, |
80765597 | 1707 | char *filter_string, bool set_str, |
38b78eb8 TH |
1708 | struct event_filter **filterp) |
1709 | { | |
80765597 | 1710 | struct filter_parse_error *pe = NULL; |
38b78eb8 TH |
1711 | int err; |
1712 | ||
0b3dec05 | 1713 | err = create_filter_start(filter_string, set_str, &pe, filterp); |
80765597 SRV |
1714 | if (err) |
1715 | return err; | |
1716 | ||
0b3dec05 | 1717 | err = process_preds(call, filter_string, *filterp, pe); |
80765597 | 1718 | if (err && set_str) |
0b3dec05 | 1719 | append_filter_err(pe, *filterp); |
38b78eb8 | 1720 | |
38b78eb8 TH |
1721 | return err; |
1722 | } | |
1723 | ||
2425bcb9 | 1724 | int create_event_filter(struct trace_event_call *call, |
bac5fb97 TZ |
1725 | char *filter_str, bool set_str, |
1726 | struct event_filter **filterp) | |
1727 | { | |
1728 | return create_filter(call, filter_str, set_str, filterp); | |
1729 | } | |
1730 | ||
38b78eb8 TH |
1731 | /** |
1732 | * create_system_filter - create a filter for an event_subsystem | |
1733 | * @system: event_subsystem to create a filter for | |
1734 | * @filter_str: filter string | |
1735 | * @filterp: out param for created filter (always updated on return) | |
1736 | * | |
1737 | * Identical to create_filter() except that it creates a subsystem filter | |
1738 | * and always remembers @filter_str. | |
1739 | */ | |
7967b3e0 | 1740 | static int create_system_filter(struct trace_subsystem_dir *dir, |
f306cc82 | 1741 | struct trace_array *tr, |
38b78eb8 TH |
1742 | char *filter_str, struct event_filter **filterp) |
1743 | { | |
80765597 | 1744 | struct filter_parse_error *pe = NULL; |
38b78eb8 TH |
1745 | int err; |
1746 | ||
0b3dec05 | 1747 | err = create_filter_start(filter_str, true, &pe, filterp); |
38b78eb8 | 1748 | if (!err) { |
80765597 | 1749 | err = process_system_preds(dir, tr, pe, filter_str); |
38b78eb8 TH |
1750 | if (!err) { |
1751 | /* System filters just show a default message */ | |
0b3dec05 SRV |
1752 | kfree((*filterp)->filter_string); |
1753 | (*filterp)->filter_string = NULL; | |
38b78eb8 | 1754 | } else { |
0b3dec05 | 1755 | append_filter_err(pe, *filterp); |
38b78eb8 TH |
1756 | } |
1757 | } | |
80765597 | 1758 | create_filter_finish(pe); |
38b78eb8 | 1759 | |
38b78eb8 TH |
1760 | return err; |
1761 | } | |
1762 | ||
e2912b09 | 1763 | /* caller must hold event_mutex */ |
7f1d2f82 | 1764 | int apply_event_filter(struct trace_event_file *file, char *filter_string) |
8b372562 | 1765 | { |
2425bcb9 | 1766 | struct trace_event_call *call = file->event_call; |
0b3dec05 | 1767 | struct event_filter *filter = NULL; |
e2912b09 | 1768 | int err; |
8b372562 TZ |
1769 | |
1770 | if (!strcmp(strstrip(filter_string), "0")) { | |
f306cc82 TZ |
1771 | filter_disable(file); |
1772 | filter = event_filter(file); | |
1773 | ||
75b8e982 | 1774 | if (!filter) |
e2912b09 | 1775 | return 0; |
f306cc82 TZ |
1776 | |
1777 | event_clear_filter(file); | |
1778 | ||
f76690af SR |
1779 | /* Make sure the filter is not being used */ |
1780 | synchronize_sched(); | |
75b8e982 | 1781 | __free_filter(filter); |
f306cc82 | 1782 | |
e2912b09 | 1783 | return 0; |
8b372562 TZ |
1784 | } |
1785 | ||
38b78eb8 | 1786 | err = create_filter(call, filter_string, true, &filter); |
8b372562 | 1787 | |
75b8e982 SR |
1788 | /* |
1789 | * Always swap the call filter with the new filter | |
1790 | * even if there was an error. If there was an error | |
1791 | * in the filter, we disable the filter and show the error | |
1792 | * string | |
1793 | */ | |
38b78eb8 | 1794 | if (filter) { |
f306cc82 | 1795 | struct event_filter *tmp; |
38b78eb8 | 1796 | |
f306cc82 | 1797 | tmp = event_filter(file); |
38b78eb8 | 1798 | if (!err) |
f306cc82 | 1799 | event_set_filtered_flag(file); |
38b78eb8 | 1800 | else |
f306cc82 | 1801 | filter_disable(file); |
38b78eb8 | 1802 | |
f306cc82 | 1803 | event_set_filter(file, filter); |
38b78eb8 TH |
1804 | |
1805 | if (tmp) { | |
1806 | /* Make sure the call is done with the filter */ | |
1807 | synchronize_sched(); | |
1808 | __free_filter(tmp); | |
1809 | } | |
75b8e982 | 1810 | } |
8b372562 TZ |
1811 | |
1812 | return err; | |
1813 | } | |
1814 | ||
7967b3e0 | 1815 | int apply_subsystem_event_filter(struct trace_subsystem_dir *dir, |
8b372562 TZ |
1816 | char *filter_string) |
1817 | { | |
ae63b31e | 1818 | struct event_subsystem *system = dir->subsystem; |
f306cc82 | 1819 | struct trace_array *tr = dir->tr; |
0b3dec05 | 1820 | struct event_filter *filter = NULL; |
75b8e982 | 1821 | int err = 0; |
8b372562 | 1822 | |
00e95830 | 1823 | mutex_lock(&event_mutex); |
8b372562 | 1824 | |
e9dbfae5 | 1825 | /* Make sure the system still has events */ |
ae63b31e | 1826 | if (!dir->nr_events) { |
e9dbfae5 SR |
1827 | err = -ENODEV; |
1828 | goto out_unlock; | |
1829 | } | |
1830 | ||
8b372562 | 1831 | if (!strcmp(strstrip(filter_string), "0")) { |
bb9ef1cb | 1832 | filter_free_subsystem_preds(dir, tr); |
8b372562 | 1833 | remove_filter_string(system->filter); |
75b8e982 SR |
1834 | filter = system->filter; |
1835 | system->filter = NULL; | |
1836 | /* Ensure all filters are no longer used */ | |
1837 | synchronize_sched(); | |
bb9ef1cb | 1838 | filter_free_subsystem_filters(dir, tr); |
75b8e982 | 1839 | __free_filter(filter); |
a66abe7f | 1840 | goto out_unlock; |
8b372562 TZ |
1841 | } |
1842 | ||
bb9ef1cb | 1843 | err = create_system_filter(dir, tr, filter_string, &filter); |
38b78eb8 TH |
1844 | if (filter) { |
1845 | /* | |
1846 | * No event actually uses the system filter | |
1847 | * we can free it without synchronize_sched(). | |
1848 | */ | |
1849 | __free_filter(system->filter); | |
1850 | system->filter = filter; | |
1851 | } | |
8cd995b6 | 1852 | out_unlock: |
00e95830 | 1853 | mutex_unlock(&event_mutex); |
8b372562 TZ |
1854 | |
1855 | return err; | |
1856 | } | |
7ce7e424 | 1857 | |
07b139c8 | 1858 | #ifdef CONFIG_PERF_EVENTS |
6fb2915d LZ |
1859 | |
1860 | void ftrace_profile_free_filter(struct perf_event *event) | |
1861 | { | |
1862 | struct event_filter *filter = event->filter; | |
1863 | ||
1864 | event->filter = NULL; | |
c9c53ca0 | 1865 | __free_filter(filter); |
6fb2915d LZ |
1866 | } |
1867 | ||
5500fa51 JO |
1868 | struct function_filter_data { |
1869 | struct ftrace_ops *ops; | |
1870 | int first_filter; | |
1871 | int first_notrace; | |
1872 | }; | |
1873 | ||
1874 | #ifdef CONFIG_FUNCTION_TRACER | |
1875 | static char ** | |
1876 | ftrace_function_filter_re(char *buf, int len, int *count) | |
1877 | { | |
1bb56471 | 1878 | char *str, **re; |
5500fa51 JO |
1879 | |
1880 | str = kstrndup(buf, len, GFP_KERNEL); | |
1881 | if (!str) | |
1882 | return NULL; | |
1883 | ||
1884 | /* | |
1885 | * The argv_split function takes white space | |
1886 | * as a separator, so convert ',' into spaces. | |
1887 | */ | |
1bb56471 | 1888 | strreplace(str, ',', ' '); |
5500fa51 JO |
1889 | |
1890 | re = argv_split(GFP_KERNEL, str, count); | |
1891 | kfree(str); | |
1892 | return re; | |
1893 | } | |
1894 | ||
1895 | static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter, | |
1896 | int reset, char *re, int len) | |
1897 | { | |
1898 | int ret; | |
1899 | ||
1900 | if (filter) | |
1901 | ret = ftrace_set_filter(ops, re, len, reset); | |
1902 | else | |
1903 | ret = ftrace_set_notrace(ops, re, len, reset); | |
1904 | ||
1905 | return ret; | |
1906 | } | |
1907 | ||
1908 | static int __ftrace_function_set_filter(int filter, char *buf, int len, | |
1909 | struct function_filter_data *data) | |
1910 | { | |
92d8d4a8 | 1911 | int i, re_cnt, ret = -EINVAL; |
5500fa51 JO |
1912 | int *reset; |
1913 | char **re; | |
1914 | ||
1915 | reset = filter ? &data->first_filter : &data->first_notrace; | |
1916 | ||
1917 | /* | |
1918 | * The 'ip' field could have multiple filters set, separated | |
1919 | * either by space or comma. We first cut the filter and apply | |
1920 | * all pieces separatelly. | |
1921 | */ | |
1922 | re = ftrace_function_filter_re(buf, len, &re_cnt); | |
1923 | if (!re) | |
1924 | return -EINVAL; | |
1925 | ||
1926 | for (i = 0; i < re_cnt; i++) { | |
1927 | ret = ftrace_function_set_regexp(data->ops, filter, *reset, | |
1928 | re[i], strlen(re[i])); | |
1929 | if (ret) | |
1930 | break; | |
1931 | ||
1932 | if (*reset) | |
1933 | *reset = 0; | |
1934 | } | |
1935 | ||
1936 | argv_free(re); | |
1937 | return ret; | |
1938 | } | |
1939 | ||
80765597 | 1940 | static int ftrace_function_check_pred(struct filter_pred *pred) |
5500fa51 JO |
1941 | { |
1942 | struct ftrace_event_field *field = pred->field; | |
1943 | ||
80765597 SRV |
1944 | /* |
1945 | * Check the predicate for function trace, verify: | |
1946 | * - only '==' and '!=' is used | |
1947 | * - the 'ip' field is used | |
1948 | */ | |
1949 | if ((pred->op != OP_EQ) && (pred->op != OP_NE)) | |
1950 | return -EINVAL; | |
5500fa51 | 1951 | |
80765597 SRV |
1952 | if (strcmp(field->name, "ip")) |
1953 | return -EINVAL; | |
5500fa51 JO |
1954 | |
1955 | return 0; | |
1956 | } | |
1957 | ||
80765597 SRV |
1958 | static int ftrace_function_set_filter_pred(struct filter_pred *pred, |
1959 | struct function_filter_data *data) | |
5500fa51 | 1960 | { |
80765597 SRV |
1961 | int ret; |
1962 | ||
5500fa51 | 1963 | /* Checking the node is valid for function trace. */ |
80765597 SRV |
1964 | ret = ftrace_function_check_pred(pred); |
1965 | if (ret) | |
1966 | return ret; | |
1967 | ||
1968 | return __ftrace_function_set_filter(pred->op == OP_EQ, | |
1969 | pred->regex.pattern, | |
1970 | pred->regex.len, | |
1971 | data); | |
1972 | } | |
1973 | ||
1974 | static bool is_or(struct prog_entry *prog, int i) | |
1975 | { | |
1976 | int target; | |
5500fa51 | 1977 | |
80765597 SRV |
1978 | /* |
1979 | * Only "||" is allowed for function events, thus, | |
1980 | * all true branches should jump to true, and any | |
1981 | * false branch should jump to false. | |
1982 | */ | |
1983 | target = prog[i].target + 1; | |
1984 | /* True and false have NULL preds (all prog entries should jump to one */ | |
1985 | if (prog[target].pred) | |
1986 | return false; | |
1987 | ||
1988 | /* prog[target].target is 1 for TRUE, 0 for FALSE */ | |
1989 | return prog[i].when_to_branch == prog[target].target; | |
5500fa51 JO |
1990 | } |
1991 | ||
1992 | static int ftrace_function_set_filter(struct perf_event *event, | |
1993 | struct event_filter *filter) | |
1994 | { | |
1f3b0faa SRV |
1995 | struct prog_entry *prog = rcu_dereference_protected(filter->prog, |
1996 | lockdep_is_held(&event_mutex)); | |
5500fa51 JO |
1997 | struct function_filter_data data = { |
1998 | .first_filter = 1, | |
1999 | .first_notrace = 1, | |
2000 | .ops = &event->ftrace_ops, | |
2001 | }; | |
80765597 SRV |
2002 | int i; |
2003 | ||
2004 | for (i = 0; prog[i].pred; i++) { | |
2005 | struct filter_pred *pred = prog[i].pred; | |
2006 | ||
2007 | if (!is_or(prog, i)) | |
2008 | return -EINVAL; | |
5500fa51 | 2009 | |
80765597 SRV |
2010 | if (ftrace_function_set_filter_pred(pred, &data) < 0) |
2011 | return -EINVAL; | |
2012 | } | |
2013 | return 0; | |
5500fa51 JO |
2014 | } |
2015 | #else | |
2016 | static int ftrace_function_set_filter(struct perf_event *event, | |
2017 | struct event_filter *filter) | |
2018 | { | |
2019 | return -ENODEV; | |
2020 | } | |
2021 | #endif /* CONFIG_FUNCTION_TRACER */ | |
2022 | ||
6fb2915d LZ |
2023 | int ftrace_profile_set_filter(struct perf_event *event, int event_id, |
2024 | char *filter_str) | |
2025 | { | |
2026 | int err; | |
0b3dec05 | 2027 | struct event_filter *filter = NULL; |
2425bcb9 | 2028 | struct trace_event_call *call; |
6fb2915d LZ |
2029 | |
2030 | mutex_lock(&event_mutex); | |
2031 | ||
3f78f935 | 2032 | call = event->tp_event; |
a66abe7f IM |
2033 | |
2034 | err = -EINVAL; | |
3f78f935 | 2035 | if (!call) |
a66abe7f | 2036 | goto out_unlock; |
6fb2915d | 2037 | |
a66abe7f | 2038 | err = -EEXIST; |
6fb2915d | 2039 | if (event->filter) |
a66abe7f | 2040 | goto out_unlock; |
6fb2915d | 2041 | |
38b78eb8 | 2042 | err = create_filter(call, filter_str, false, &filter); |
5500fa51 JO |
2043 | if (err) |
2044 | goto free_filter; | |
2045 | ||
2046 | if (ftrace_event_is_function(call)) | |
2047 | err = ftrace_function_set_filter(event, filter); | |
38b78eb8 | 2048 | else |
5500fa51 JO |
2049 | event->filter = filter; |
2050 | ||
2051 | free_filter: | |
2052 | if (err || ftrace_event_is_function(call)) | |
c9c53ca0 | 2053 | __free_filter(filter); |
6fb2915d | 2054 | |
a66abe7f | 2055 | out_unlock: |
6fb2915d LZ |
2056 | mutex_unlock(&event_mutex); |
2057 | ||
2058 | return err; | |
2059 | } | |
2060 | ||
07b139c8 | 2061 | #endif /* CONFIG_PERF_EVENTS */ |
6fb2915d | 2062 | |
1d0e78e3 JO |
2063 | #ifdef CONFIG_FTRACE_STARTUP_TEST |
2064 | ||
2065 | #include <linux/types.h> | |
2066 | #include <linux/tracepoint.h> | |
2067 | ||
2068 | #define CREATE_TRACE_POINTS | |
2069 | #include "trace_events_filter_test.h" | |
2070 | ||
1d0e78e3 JO |
2071 | #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ |
2072 | { \ | |
2073 | .filter = FILTER, \ | |
2074 | .rec = { .a = va, .b = vb, .c = vc, .d = vd, \ | |
2075 | .e = ve, .f = vf, .g = vg, .h = vh }, \ | |
2076 | .match = m, \ | |
2077 | .not_visited = nvisit, \ | |
2078 | } | |
2079 | #define YES 1 | |
2080 | #define NO 0 | |
2081 | ||
2082 | static struct test_filter_data_t { | |
2083 | char *filter; | |
a7237765 | 2084 | struct trace_event_raw_ftrace_test_filter rec; |
1d0e78e3 JO |
2085 | int match; |
2086 | char *not_visited; | |
2087 | } test_filter_data[] = { | |
2088 | #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \ | |
2089 | "e == 1 && f == 1 && g == 1 && h == 1" | |
2090 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""), | |
2091 | DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"), | |
2092 | DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""), | |
2093 | #undef FILTER | |
2094 | #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \ | |
2095 | "e == 1 || f == 1 || g == 1 || h == 1" | |
2096 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), | |
2097 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""), | |
2098 | DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"), | |
2099 | #undef FILTER | |
2100 | #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \ | |
2101 | "(e == 1 || f == 1) && (g == 1 || h == 1)" | |
2102 | DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"), | |
2103 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), | |
2104 | DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"), | |
2105 | DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"), | |
2106 | #undef FILTER | |
2107 | #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \ | |
2108 | "(e == 1 && f == 1) || (g == 1 && h == 1)" | |
2109 | DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"), | |
2110 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""), | |
2111 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), | |
2112 | #undef FILTER | |
2113 | #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \ | |
2114 | "(e == 1 && f == 1) || (g == 1 && h == 1)" | |
2115 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"), | |
2116 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""), | |
2117 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""), | |
2118 | #undef FILTER | |
2119 | #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \ | |
2120 | "(e == 1 || f == 1)) && (g == 1 || h == 1)" | |
2121 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"), | |
2122 | DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""), | |
2123 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"), | |
2124 | #undef FILTER | |
2125 | #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \ | |
2126 | "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))" | |
2127 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"), | |
2128 | DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""), | |
2129 | DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""), | |
2130 | #undef FILTER | |
2131 | #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \ | |
2132 | "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))" | |
2133 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"), | |
2134 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), | |
2135 | DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"), | |
2136 | }; | |
2137 | ||
2138 | #undef DATA_REC | |
2139 | #undef FILTER | |
2140 | #undef YES | |
2141 | #undef NO | |
2142 | ||
0a4d0564 | 2143 | #define DATA_CNT ARRAY_SIZE(test_filter_data) |
1d0e78e3 JO |
2144 | |
2145 | static int test_pred_visited; | |
2146 | ||
2147 | static int test_pred_visited_fn(struct filter_pred *pred, void *event) | |
2148 | { | |
2149 | struct ftrace_event_field *field = pred->field; | |
2150 | ||
2151 | test_pred_visited = 1; | |
2152 | printk(KERN_INFO "\npred visited %s\n", field->name); | |
2153 | return 1; | |
2154 | } | |
2155 | ||
80765597 | 2156 | static void update_pred_fn(struct event_filter *filter, char *fields) |
1d0e78e3 | 2157 | { |
8ec8405f SRV |
2158 | struct prog_entry *prog = rcu_dereference_protected(filter->prog, |
2159 | lockdep_is_held(&event_mutex)); | |
80765597 | 2160 | int i; |
1d0e78e3 | 2161 | |
80765597 SRV |
2162 | for (i = 0; prog[i].pred; i++) { |
2163 | struct filter_pred *pred = prog[i].pred; | |
1d0e78e3 JO |
2164 | struct ftrace_event_field *field = pred->field; |
2165 | ||
80765597 SRV |
2166 | WARN_ON_ONCE(!pred->fn); |
2167 | ||
1d0e78e3 | 2168 | if (!field) { |
80765597 SRV |
2169 | WARN_ONCE(1, "all leafs should have field defined %d", i); |
2170 | continue; | |
1d0e78e3 | 2171 | } |
80765597 | 2172 | |
1d0e78e3 | 2173 | if (!strchr(fields, *field->name)) |
80765597 | 2174 | continue; |
1d0e78e3 | 2175 | |
1d0e78e3 JO |
2176 | pred->fn = test_pred_visited_fn; |
2177 | } | |
1d0e78e3 JO |
2178 | } |
2179 | ||
2180 | static __init int ftrace_test_event_filter(void) | |
2181 | { | |
2182 | int i; | |
2183 | ||
2184 | printk(KERN_INFO "Testing ftrace filter: "); | |
2185 | ||
2186 | for (i = 0; i < DATA_CNT; i++) { | |
2187 | struct event_filter *filter = NULL; | |
2188 | struct test_filter_data_t *d = &test_filter_data[i]; | |
2189 | int err; | |
2190 | ||
38b78eb8 TH |
2191 | err = create_filter(&event_ftrace_test_filter, d->filter, |
2192 | false, &filter); | |
1d0e78e3 JO |
2193 | if (err) { |
2194 | printk(KERN_INFO | |
2195 | "Failed to get filter for '%s', err %d\n", | |
2196 | d->filter, err); | |
38b78eb8 | 2197 | __free_filter(filter); |
1d0e78e3 JO |
2198 | break; |
2199 | } | |
2200 | ||
8ec8405f SRV |
2201 | /* Needed to dereference filter->prog */ |
2202 | mutex_lock(&event_mutex); | |
86b6ef21 SR |
2203 | /* |
2204 | * The preemption disabling is not really needed for self | |
2205 | * tests, but the rcu dereference will complain without it. | |
2206 | */ | |
2207 | preempt_disable(); | |
1d0e78e3 | 2208 | if (*d->not_visited) |
80765597 | 2209 | update_pred_fn(filter, d->not_visited); |
1d0e78e3 JO |
2210 | |
2211 | test_pred_visited = 0; | |
2212 | err = filter_match_preds(filter, &d->rec); | |
86b6ef21 | 2213 | preempt_enable(); |
1d0e78e3 | 2214 | |
8ec8405f SRV |
2215 | mutex_unlock(&event_mutex); |
2216 | ||
1d0e78e3 JO |
2217 | __free_filter(filter); |
2218 | ||
2219 | if (test_pred_visited) { | |
2220 | printk(KERN_INFO | |
2221 | "Failed, unwanted pred visited for filter %s\n", | |
2222 | d->filter); | |
2223 | break; | |
2224 | } | |
2225 | ||
2226 | if (err != d->match) { | |
2227 | printk(KERN_INFO | |
2228 | "Failed to match filter '%s', expected %d\n", | |
2229 | d->filter, d->match); | |
2230 | break; | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | if (i == DATA_CNT) | |
2235 | printk(KERN_CONT "OK\n"); | |
2236 | ||
2237 | return 0; | |
2238 | } | |
2239 | ||
2240 | late_initcall(ftrace_test_event_filter); | |
2241 | ||
2242 | #endif /* CONFIG_FTRACE_STARTUP_TEST */ |