2 * trace_events_filter - generic event filtering
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.
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.
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.
18 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
21 #include <linux/module.h>
22 #include <linux/ctype.h>
23 #include <linux/mutex.h>
24 #include <linux/perf_event.h>
25 #include <linux/slab.h>
28 #include "trace_output.h"
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"
36 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
51 enum filter_op_ids { OPS };
56 static const char * ops[] = { OPS };
59 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
60 * pred_funcs_##type below must match the order of them above.
62 #define PRED_FUNC_START OP_LE
63 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
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)?"), \
82 C(NO_FILTER, "No filter found"),
85 #define C(a, b) FILT_ERR_##a
92 static char *err_text[] = { ERRORS };
94 /* Called after a '!' character but "!=" and "!~" are not "not"s */
95 static bool is_not(const char *str)
106 * prog_entry - a singe entry in the filter program
107 * @target: Index to jump to on a branch (actually one minus the index)
108 * @when_to_branch: The value of the result of the predicate to do a branch
109 * @pred: The predicate to execute.
114 struct filter_pred *pred;
118 * update_preds- assign a program entry a label target
119 * @prog: The program array
120 * @N: The index of the current entry in @prog
121 * @when_to_branch: What to assign a program entry for its branch condition
123 * The program entry at @N has a target that points to the index of a program
124 * entry that can have its target and when_to_branch fields updated.
125 * Update the current program entry denoted by index @N target field to be
126 * that of the updated entry. This will denote the entry to update if
127 * we are processing an "||" after an "&&"
129 static void update_preds(struct prog_entry *prog, int N, int invert)
135 prog[t].when_to_branch = invert;
140 struct filter_parse_error {
145 static void parse_error(struct filter_parse_error *pe, int err, int pos)
148 pe->lasterr_pos = pos;
151 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
152 struct filter_parse_error *pe,
153 struct filter_pred **pred);
162 * Without going into a formal proof, this explains the method that is used in
163 * parsing the logical expressions.
165 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
166 * The first pass will convert it into the following program:
168 * n1: r=a; l1: if (!r) goto l4;
169 * n2: r=b; l2: if (!r) goto l4;
170 * n3: r=c; r=!r; l3: if (r) goto l4;
171 * n4: r=g; r=!r; l4: if (r) goto l5;
172 * n5: r=d; l5: if (r) goto T
173 * n6: r=e; l6: if (!r) goto l7;
174 * n7: r=f; r=!r; l7: if (!r) goto F
178 * To do this, we use a data structure to represent each of the above
179 * predicate and conditions that has:
181 * predicate, when_to_branch, invert, target
183 * The "predicate" will hold the function to determine the result "r".
184 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
185 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
186 * The "invert" holds whether the value should be reversed before testing.
187 * The "target" contains the label "l#" to jump to.
189 * A stack is created to hold values when parentheses are used.
191 * To simplify the logic, the labels will start at 0 and not 1.
193 * The possible invert values are 1 and 0. The number of "!"s that are in scope
194 * before the predicate determines the invert value, if the number is odd then
195 * the invert value is 1 and 0 otherwise. This means the invert value only
196 * needs to be toggled when a new "!" is introduced compared to what is stored
197 * on the stack, where parentheses were used.
199 * The top of the stack and "invert" are initialized to zero.
203 * #1 A loop through all the tokens is done:
205 * #2 If the token is an "(", the stack is push, and the current stack value
206 * gets the current invert value, and the loop continues to the next token.
207 * The top of the stack saves the "invert" value to keep track of what
208 * the current inversion is. As "!(a && !b || c)" would require all
209 * predicates being affected separately by the "!" before the parentheses.
210 * And that would end up being equivalent to "(!a || b) && !c"
212 * #3 If the token is an "!", the current "invert" value gets inverted, and
213 * the loop continues. Note, if the next token is a predicate, then
214 * this "invert" value is only valid for the current program entry,
215 * and does not affect other predicates later on.
217 * The only other acceptable token is the predicate string.
219 * #4 A new entry into the program is added saving: the predicate and the
220 * current value of "invert". The target is currently assigned to the
221 * previous program index (this will not be its final value).
223 * #5 We now enter another loop and look at the next token. The only valid
224 * tokens are ")", "&&", "||" or end of the input string "\0".
226 * #6 The invert variable is reset to the current value saved on the top of
229 * #7 The top of the stack holds not only the current invert value, but also
230 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
231 * precedence than "||". That is "a && b || c && d" is equivalent to
232 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
233 * to be processed. This is the case if an "&&" was the last token. If it was
234 * then we call update_preds(). This takes the program, the current index in
235 * the program, and the current value of "invert". More will be described
236 * below about this function.
238 * #8 If the next token is "&&" then we set a flag in the top of the stack
239 * that denotes that "&&" needs to be processed, break out of this loop
240 * and continue with the outer loop.
242 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
243 * This is called with the program, the current index in the program, but
244 * this time with an inverted value of "invert" (that is !invert). This is
245 * because the value taken will become the "when_to_branch" value of the
247 * Note, this is called when the next token is not an "&&". As stated before,
248 * "&&" takes higher precedence, and "||" should not be processed yet if the
249 * next logical operation is "&&".
251 * #10 If the next token is "||" then we set a flag in the top of the stack
252 * that denotes that "||" needs to be processed, break out of this loop
253 * and continue with the outer loop.
255 * #11 If this is the end of the input string "\0" then we break out of both
258 * #12 Otherwise, the next token is ")", where we pop the stack and continue
261 * Now to discuss the update_pred() function, as that is key to the setting up
262 * of the program. Remember the "target" of the program is initialized to the
263 * previous index and not the "l" label. The target holds the index into the
264 * program that gets affected by the operand. Thus if we have something like
265 * "a || b && c", when we process "a" the target will be "-1" (undefined).
266 * When we process "b", its target is "0", which is the index of "a", as that's
267 * the predicate that is affected by "||". But because the next token after "b"
268 * is "&&" we don't call update_preds(). Instead continue to "c". As the
269 * next token after "c" is not "&&" but the end of input, we first process the
270 * "&&" by calling update_preds() for the "&&" then we process the "||" by
271 * callin updates_preds() with the values for processing "||".
273 * What does that mean? What update_preds() does is to first save the "target"
274 * of the program entry indexed by the current program entry's "target"
275 * (remember the "target" is initialized to previous program entry), and then
276 * sets that "target" to the current index which represents the label "l#".
277 * That entry's "when_to_branch" is set to the value passed in (the "invert"
278 * or "!invert"). Then it sets the current program entry's target to the saved
279 * "target" value (the old value of the program that had its "target" updated
282 * Looking back at "a || b && c", we have the following steps:
283 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
284 * "||" - flag that we need to process "||"; continue outer loop
285 * "b" - prog[1] = { "b", X, 0 }
286 * "&&" - flag that we need to process "&&"; continue outer loop
287 * (Notice we did not process "||")
288 * "c" - prog[2] = { "c", X, 1 }
289 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
290 * t = prog[2].target; // t = 1
291 * s = prog[t].target; // s = 0
292 * prog[t].target = 2; // Set target to "l2"
293 * prog[t].when_to_branch = 0;
294 * prog[2].target = s;
295 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
296 * t = prog[2].target; // t = 0
297 * s = prog[t].target; // s = -1
298 * prog[t].target = 2; // Set target to "l2"
299 * prog[t].when_to_branch = 1;
300 * prog[2].target = s;
302 * #13 Which brings us to the final step of the first pass, which is to set
303 * the last program entry's when_to_branch and target, which will be
304 * when_to_branch = 0; target = N; ( the label after the program entry after
305 * the last program entry processed above).
307 * If we denote "TRUE" to be the entry after the last program entry processed,
308 * and "FALSE" the program entry after that, we are now done with the first
311 * Making the above "a || b && c" have a progam of:
312 * prog[0] = { "a", 1, 2 }
313 * prog[1] = { "b", 0, 2 }
314 * prog[2] = { "c", 0, 3 }
316 * Which translates into:
317 * n0: r = a; l0: if (r) goto l2;
318 * n1: r = b; l1: if (!r) goto l2;
319 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
320 * T: return TRUE; l3:
323 * Although, after the first pass, the program is correct, it is
324 * inefficient. The simple sample of "a || b && c" could be easily been
326 * n0: r = a; if (r) goto T
327 * n1: r = b; if (!r) goto F
328 * n2: r = c; if (!r) goto F
332 * The First Pass is over the input string. The next too passes are over
333 * the program itself.
337 * Which brings us to the second pass. If a jump to a label has the
338 * same condition as that label, it can instead jump to its target.
339 * The original example of "a && !(!b || (c && g)) || d || e && !f"
340 * where the first pass gives us:
342 * n1: r=a; l1: if (!r) goto l4;
343 * n2: r=b; l2: if (!r) goto l4;
344 * n3: r=c; r=!r; l3: if (r) goto l4;
345 * n4: r=g; r=!r; l4: if (r) goto l5;
346 * n5: r=d; l5: if (r) goto T
347 * n6: r=e; l6: if (!r) goto l7;
348 * n7: r=f; r=!r; l7: if (!r) goto F:
352 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
353 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
354 * to go directly to T. To accomplish this, we start from the last
355 * entry in the program and work our way back. If the target of the entry
356 * has the same "when_to_branch" then we could use that entry's target.
357 * Doing this, the above would end up as:
359 * n1: r=a; l1: if (!r) goto l4;
360 * n2: r=b; l2: if (!r) goto l4;
361 * n3: r=c; r=!r; l3: if (r) goto T;
362 * n4: r=g; r=!r; l4: if (r) goto T;
363 * n5: r=d; l5: if (r) goto T;
364 * n6: r=e; l6: if (!r) goto F;
365 * n7: r=f; r=!r; l7: if (!r) goto F;
369 * In that same pass, if the "when_to_branch" doesn't match, we can simply
370 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
371 * where "l4: if (r) goto T;", then we can convert l2 to be:
372 * "l2: if (!r) goto n5;".
374 * This will have the second pass give us:
375 * n1: r=a; l1: if (!r) goto n5;
376 * n2: r=b; l2: if (!r) goto n5;
377 * n3: r=c; r=!r; l3: if (r) goto T;
378 * n4: r=g; r=!r; l4: if (r) goto T;
379 * n5: r=d; l5: if (r) goto T
380 * n6: r=e; l6: if (!r) goto F;
381 * n7: r=f; r=!r; l7: if (!r) goto F
385 * Notice, all the "l#" labels are no longer used, and they can now
390 * For the third pass we deal with the inverts. As they simply just
391 * make the "when_to_branch" get inverted, a simple loop over the
392 * program to that does: "when_to_branch ^= invert;" will do the
393 * job, leaving us with:
394 * n1: r=a; if (!r) goto n5;
395 * n2: r=b; if (!r) goto n5;
396 * n3: r=c: if (!r) goto T;
397 * n4: r=g; if (!r) goto T;
398 * n5: r=d; if (r) goto T
399 * n6: r=e; if (!r) goto F;
400 * n7: r=f; if (r) goto F
404 * As "r = a; if (!r) goto n5;" is obviously the same as
405 * "if (!a) goto n5;" without doing anything we can interperate the
407 * n1: if (!a) goto n5;
408 * n2: if (!b) goto n5;
409 * n3: if (!c) goto T;
410 * n4: if (!g) goto T;
412 * n6: if (!e) goto F;
417 * Since the inverts are discarded at the end, there's no reason to store
418 * them in the program array (and waste memory). A separate array to hold
419 * the inverts is used and freed at the end.
421 static struct prog_entry *
422 predicate_parse(const char *str, int nr_parens, int nr_preds,
423 parse_pred_fn parse_pred, void *data,
424 struct filter_parse_error *pe)
426 struct prog_entry *prog_stack;
427 struct prog_entry *prog;
428 const char *ptr = str;
429 char *inverts = NULL;
438 nr_preds += 2; /* For TRUE and FALSE */
440 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
442 return ERR_PTR(-ENOMEM);
443 prog_stack = kmalloc_array(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
445 parse_error(pe, -ENOMEM, 0);
448 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
450 parse_error(pe, -ENOMEM, 0);
459 while (*ptr) { /* #1 */
460 const char *next = ptr++;
467 if (top - op_stack > nr_parens)
468 return ERR_PTR(-EINVAL);
479 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
483 inverts[N] = invert; /* #4 */
484 prog[N].target = N-1;
486 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
507 if (next[1] == next[0]) {
512 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
517 invert = *top & INVERT;
519 if (*top & PROCESS_AND) { /* #7 */
520 update_preds(prog, N - 1, invert);
521 *top &= ~PROCESS_AND;
523 if (*next == '&') { /* #8 */
527 if (*top & PROCESS_OR) { /* #9 */
528 update_preds(prog, N - 1, !invert);
531 if (*next == '|') { /* #10 */
535 if (!*next) /* #11 */
538 if (top == op_stack) {
541 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
548 if (top != op_stack) {
550 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
557 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
561 prog[N].pred = NULL; /* #13 */
562 prog[N].target = 1; /* TRUE */
563 prog[N+1].pred = NULL;
564 prog[N+1].target = 0; /* FALSE */
565 prog[N-1].target = N;
566 prog[N-1].when_to_branch = false;
569 for (i = N-1 ; i--; ) {
570 int target = prog[i].target;
571 if (prog[i].when_to_branch == prog[target].when_to_branch)
572 prog[i].target = prog[target].target;
576 for (i = 0; i < N; i++) {
577 invert = inverts[i] ^ prog[i].when_to_branch;
578 prog[i].when_to_branch = invert;
579 /* Make sure the program always moves forward */
580 if (WARN_ON(prog[i].target <= i)) {
594 #define DEFINE_COMPARISON_PRED(type) \
595 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
597 type *addr = (type *)(event + pred->offset); \
598 type val = (type)pred->val; \
599 return *addr < val; \
601 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
603 type *addr = (type *)(event + pred->offset); \
604 type val = (type)pred->val; \
605 return *addr <= val; \
607 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
609 type *addr = (type *)(event + pred->offset); \
610 type val = (type)pred->val; \
611 return *addr > val; \
613 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
615 type *addr = (type *)(event + pred->offset); \
616 type val = (type)pred->val; \
617 return *addr >= val; \
619 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
621 type *addr = (type *)(event + pred->offset); \
622 type val = (type)pred->val; \
623 return !!(*addr & val); \
625 static const filter_pred_fn_t pred_funcs_##type[] = { \
626 filter_pred_LE_##type, \
627 filter_pred_LT_##type, \
628 filter_pred_GE_##type, \
629 filter_pred_GT_##type, \
630 filter_pred_BAND_##type, \
633 #define DEFINE_EQUALITY_PRED(size) \
634 static int filter_pred_##size(struct filter_pred *pred, void *event) \
636 u##size *addr = (u##size *)(event + pred->offset); \
637 u##size val = (u##size)pred->val; \
640 match = (val == *addr) ^ pred->not; \
645 DEFINE_COMPARISON_PRED(s64);
646 DEFINE_COMPARISON_PRED(u64);
647 DEFINE_COMPARISON_PRED(s32);
648 DEFINE_COMPARISON_PRED(u32);
649 DEFINE_COMPARISON_PRED(s16);
650 DEFINE_COMPARISON_PRED(u16);
651 DEFINE_COMPARISON_PRED(s8);
652 DEFINE_COMPARISON_PRED(u8);
654 DEFINE_EQUALITY_PRED(64);
655 DEFINE_EQUALITY_PRED(32);
656 DEFINE_EQUALITY_PRED(16);
657 DEFINE_EQUALITY_PRED(8);
659 /* Filter predicate for fixed sized arrays of characters */
660 static int filter_pred_string(struct filter_pred *pred, void *event)
662 char *addr = (char *)(event + pred->offset);
665 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
667 match = cmp ^ pred->not;
672 /* Filter predicate for char * pointers */
673 static int filter_pred_pchar(struct filter_pred *pred, void *event)
675 char **addr = (char **)(event + pred->offset);
677 int len = strlen(*addr) + 1; /* including tailing '\0' */
679 cmp = pred->regex.match(*addr, &pred->regex, len);
681 match = cmp ^ pred->not;
687 * Filter predicate for dynamic sized arrays of characters.
688 * These are implemented through a list of strings at the end
690 * Also each of these strings have a field in the entry which
691 * contains its offset from the beginning of the entry.
692 * We have then first to get this field, dereference it
693 * and add it to the address of the entry, and at last we have
694 * the address of the string.
696 static int filter_pred_strloc(struct filter_pred *pred, void *event)
698 u32 str_item = *(u32 *)(event + pred->offset);
699 int str_loc = str_item & 0xffff;
700 int str_len = str_item >> 16;
701 char *addr = (char *)(event + str_loc);
704 cmp = pred->regex.match(addr, &pred->regex, str_len);
706 match = cmp ^ pred->not;
711 /* Filter predicate for CPUs. */
712 static int filter_pred_cpu(struct filter_pred *pred, void *event)
716 cpu = raw_smp_processor_id();
737 /* Filter predicate for COMM. */
738 static int filter_pred_comm(struct filter_pred *pred, void *event)
742 cmp = pred->regex.match(current->comm, &pred->regex,
744 return cmp ^ pred->not;
747 static int filter_pred_none(struct filter_pred *pred, void *event)
753 * regex_match_foo - Basic regex callbacks
755 * @str: the string to be searched
756 * @r: the regex structure containing the pattern string
757 * @len: the length of the string to be searched (including '\0')
760 * - @str might not be NULL-terminated if it's of type DYN_STRING
761 * or STATIC_STRING, unless @len is zero.
764 static int regex_match_full(char *str, struct regex *r, int len)
766 /* len of zero means str is dynamic and ends with '\0' */
768 return strcmp(str, r->pattern) == 0;
770 return strncmp(str, r->pattern, len) == 0;
773 static int regex_match_front(char *str, struct regex *r, int len)
775 if (len && len < r->len)
778 return strncmp(str, r->pattern, r->len) == 0;
781 static int regex_match_middle(char *str, struct regex *r, int len)
784 return strstr(str, r->pattern) != NULL;
786 return strnstr(str, r->pattern, len) != NULL;
789 static int regex_match_end(char *str, struct regex *r, int len)
791 int strlen = len - 1;
793 if (strlen >= r->len &&
794 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
799 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
801 if (glob_match(r->pattern, str))
807 * filter_parse_regex - parse a basic regex
808 * @buff: the raw regex
809 * @len: length of the regex
810 * @search: will point to the beginning of the string to compare
811 * @not: tell whether the match will have to be inverted
813 * This passes in a buffer containing a regex and this function will
814 * set search to point to the search part of the buffer and
815 * return the type of search it is (see enum above).
816 * This does modify buff.
819 * search returns the pointer to use for comparison.
820 * not returns 1 if buff started with a '!'
823 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
825 int type = MATCH_FULL;
828 if (buff[0] == '!') {
837 for (i = 0; i < len; i++) {
838 if (buff[i] == '*') {
840 type = MATCH_END_ONLY;
841 } else if (i == len - 1) {
842 if (type == MATCH_END_ONLY)
843 type = MATCH_MIDDLE_ONLY;
845 type = MATCH_FRONT_ONLY;
848 } else { /* pattern continues, use full glob */
851 } else if (strchr("[?\\", buff[i])) {
861 static void filter_build_regex(struct filter_pred *pred)
863 struct regex *r = &pred->regex;
865 enum regex_type type = MATCH_FULL;
867 if (pred->op == OP_GLOB) {
868 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
869 r->len = strlen(search);
870 memmove(r->pattern, search, r->len+1);
875 r->match = regex_match_full;
877 case MATCH_FRONT_ONLY:
878 r->match = regex_match_front;
880 case MATCH_MIDDLE_ONLY:
881 r->match = regex_match_middle;
884 r->match = regex_match_end;
887 r->match = regex_match_glob;
892 /* return 1 if event matches, 0 otherwise (discard) */
893 int filter_match_preds(struct event_filter *filter, void *rec)
895 struct prog_entry *prog;
898 /* no filter is considered a match */
902 prog = rcu_dereference_sched(filter->prog);
906 for (i = 0; prog[i].pred; i++) {
907 struct filter_pred *pred = prog[i].pred;
908 int match = pred->fn(pred, rec);
909 if (match == prog[i].when_to_branch)
912 return prog[i].target;
914 EXPORT_SYMBOL_GPL(filter_match_preds);
916 static void remove_filter_string(struct event_filter *filter)
921 kfree(filter->filter_string);
922 filter->filter_string = NULL;
925 static void append_filter_err(struct filter_parse_error *pe,
926 struct event_filter *filter)
929 int pos = pe->lasterr_pos;
933 if (WARN_ON(!filter->filter_string))
936 s = kmalloc(sizeof(*s), GFP_KERNEL);
941 len = strlen(filter->filter_string);
945 /* indexing is off by one */
949 trace_seq_puts(s, filter->filter_string);
950 if (pe->lasterr > 0) {
951 trace_seq_printf(s, "\n%*s", pos, "^");
952 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
954 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
956 trace_seq_putc(s, 0);
957 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
959 kfree(filter->filter_string);
960 filter->filter_string = buf;
965 static inline struct event_filter *event_filter(struct trace_event_file *file)
970 /* caller must hold event_mutex */
971 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
973 struct event_filter *filter = event_filter(file);
975 if (filter && filter->filter_string)
976 trace_seq_printf(s, "%s\n", filter->filter_string);
978 trace_seq_puts(s, "none\n");
981 void print_subsystem_event_filter(struct event_subsystem *system,
984 struct event_filter *filter;
986 mutex_lock(&event_mutex);
987 filter = system->filter;
988 if (filter && filter->filter_string)
989 trace_seq_printf(s, "%s\n", filter->filter_string);
991 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
992 mutex_unlock(&event_mutex);
995 static void free_prog(struct event_filter *filter)
997 struct prog_entry *prog;
1000 prog = rcu_access_pointer(filter->prog);
1004 for (i = 0; prog[i].pred; i++)
1005 kfree(prog[i].pred);
1009 static void filter_disable(struct trace_event_file *file)
1011 unsigned long old_flags = file->flags;
1013 file->flags &= ~EVENT_FILE_FL_FILTERED;
1015 if (old_flags != file->flags)
1016 trace_buffered_event_disable();
1019 static void __free_filter(struct event_filter *filter)
1025 kfree(filter->filter_string);
1029 void free_event_filter(struct event_filter *filter)
1031 __free_filter(filter);
1034 static inline void __remove_filter(struct trace_event_file *file)
1036 filter_disable(file);
1037 remove_filter_string(file->filter);
1040 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1041 struct trace_array *tr)
1043 struct trace_event_file *file;
1045 list_for_each_entry(file, &tr->events, list) {
1046 if (file->system != dir)
1048 __remove_filter(file);
1052 static inline void __free_subsystem_filter(struct trace_event_file *file)
1054 __free_filter(file->filter);
1055 file->filter = NULL;
1058 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1059 struct trace_array *tr)
1061 struct trace_event_file *file;
1063 list_for_each_entry(file, &tr->events, list) {
1064 if (file->system != dir)
1066 __free_subsystem_filter(file);
1070 int filter_assign_type(const char *type)
1072 if (strstr(type, "__data_loc") && strstr(type, "char"))
1073 return FILTER_DYN_STRING;
1075 if (strchr(type, '[') && strstr(type, "char"))
1076 return FILTER_STATIC_STRING;
1078 return FILTER_OTHER;
1081 static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1082 int field_size, int field_is_signed)
1084 filter_pred_fn_t fn = NULL;
1085 int pred_func_index = -1;
1092 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1094 pred_func_index = op - PRED_FUNC_START;
1095 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1099 switch (field_size) {
1101 if (pred_func_index < 0)
1102 fn = filter_pred_64;
1103 else if (field_is_signed)
1104 fn = pred_funcs_s64[pred_func_index];
1106 fn = pred_funcs_u64[pred_func_index];
1109 if (pred_func_index < 0)
1110 fn = filter_pred_32;
1111 else if (field_is_signed)
1112 fn = pred_funcs_s32[pred_func_index];
1114 fn = pred_funcs_u32[pred_func_index];
1117 if (pred_func_index < 0)
1118 fn = filter_pred_16;
1119 else if (field_is_signed)
1120 fn = pred_funcs_s16[pred_func_index];
1122 fn = pred_funcs_u16[pred_func_index];
1125 if (pred_func_index < 0)
1127 else if (field_is_signed)
1128 fn = pred_funcs_s8[pred_func_index];
1130 fn = pred_funcs_u8[pred_func_index];
1137 /* Called when a predicate is encountered by predicate_parse() */
1138 static int parse_pred(const char *str, void *data,
1139 int pos, struct filter_parse_error *pe,
1140 struct filter_pred **pred_ptr)
1142 struct trace_event_call *call = data;
1143 struct ftrace_event_field *field;
1144 struct filter_pred *pred = NULL;
1145 char num_buf[24]; /* Big enough to hold an address */
1155 /* First find the field to associate to */
1156 while (isspace(str[i]))
1160 while (isalnum(str[i]) || str[i] == '_')
1168 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1172 /* Make sure that the field exists */
1174 field = trace_find_event_field(call, field_name);
1177 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1181 while (isspace(str[i]))
1184 /* Make sure this op is supported */
1185 for (op = 0; ops[op]; op++) {
1186 /* This is why '<=' must come before '<' in ops[] */
1187 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1192 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1196 i += strlen(ops[op]);
1198 while (isspace(str[i]))
1203 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1207 pred->field = field;
1208 pred->offset = field->offset;
1211 if (ftrace_event_is_function(call)) {
1213 * Perf does things different with function events.
1214 * It only allows an "ip" field, and expects a string.
1215 * But the string does not need to be surrounded by quotes.
1216 * If it is a string, the assigned function as a nop,
1217 * (perf doesn't use it) and grab everything.
1219 if (strcmp(field->name, "ip") != 0) {
1220 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1223 pred->fn = filter_pred_none;
1226 * Quotes are not required, but if they exist then we need
1227 * to read them till we hit a matching one.
1229 if (str[i] == '\'' || str[i] == '"')
1234 for (i++; str[i]; i++) {
1235 if (q && str[i] == q)
1237 if (!q && (str[i] == ')' || str[i] == '&' ||
1245 if (len >= MAX_FILTER_STR_VAL) {
1246 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1250 pred->regex.len = len;
1251 strncpy(pred->regex.pattern, str + s, len);
1252 pred->regex.pattern[len] = 0;
1254 /* This is either a string, or an integer */
1255 } else if (str[i] == '\'' || str[i] == '"') {
1258 /* Make sure the op is OK for strings */
1267 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1271 /* Make sure the field is OK for strings */
1272 if (!is_string_field(field)) {
1273 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1277 for (i++; str[i]; i++) {
1282 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1289 if (len >= MAX_FILTER_STR_VAL) {
1290 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1294 pred->regex.len = len;
1295 strncpy(pred->regex.pattern, str + s, len);
1296 pred->regex.pattern[len] = 0;
1298 filter_build_regex(pred);
1300 if (field->filter_type == FILTER_COMM) {
1301 pred->fn = filter_pred_comm;
1303 } else if (field->filter_type == FILTER_STATIC_STRING) {
1304 pred->fn = filter_pred_string;
1305 pred->regex.field_len = field->size;
1307 } else if (field->filter_type == FILTER_DYN_STRING)
1308 pred->fn = filter_pred_strloc;
1310 pred->fn = filter_pred_pchar;
1311 /* go past the last quote */
1314 } else if (isdigit(str[i])) {
1316 /* Make sure the field is not a string */
1317 if (is_string_field(field)) {
1318 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1322 if (op == OP_GLOB) {
1323 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1327 /* We allow 0xDEADBEEF */
1328 while (isalnum(str[i]))
1332 /* 0xfeedfacedeadbeef is 18 chars max */
1333 if (len >= sizeof(num_buf)) {
1334 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1338 strncpy(num_buf, str + s, len);
1341 /* Make sure it is a value */
1342 if (field->is_signed)
1343 ret = kstrtoll(num_buf, 0, &val);
1345 ret = kstrtoull(num_buf, 0, &val);
1347 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1353 if (field->filter_type == FILTER_CPU)
1354 pred->fn = filter_pred_cpu;
1356 pred->fn = select_comparison_fn(pred->op, field->size,
1358 if (pred->op == OP_NE)
1363 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1376 TOO_MANY_CLOSE = -1,
1382 * Read the filter string once to calculate the number of predicates
1383 * as well as how deep the parentheses go.
1386 * 0 - everything is fine (err is undefined)
1389 * -3 - No matching quote
1391 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1393 bool is_pred = false;
1395 int open = 1; /* Count the expression as "(E)" */
1403 for (i = 0; str[i]; i++) {
1404 if (isspace(str[i]))
1407 if (str[i] == quote)
1420 if (str[i+1] != str[i])
1427 if (open > max_open)
1434 return TOO_MANY_CLOSE;
1447 return MISSING_QUOTE;
1453 /* find the bad open */
1456 if (str[i] == quote)
1462 if (level == open) {
1464 return TOO_MANY_OPEN;
1477 /* First character is the '(' with missing ')' */
1479 return TOO_MANY_OPEN;
1482 /* Set the size of the required stacks */
1488 static int process_preds(struct trace_event_call *call,
1489 const char *filter_string,
1490 struct event_filter *filter,
1491 struct filter_parse_error *pe)
1493 struct prog_entry *prog;
1499 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1503 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1506 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1509 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
1517 prog = predicate_parse(filter_string, nr_parens, nr_preds,
1518 parse_pred, call, pe);
1520 return PTR_ERR(prog);
1522 rcu_assign_pointer(filter->prog, prog);
1526 static inline void event_set_filtered_flag(struct trace_event_file *file)
1528 unsigned long old_flags = file->flags;
1530 file->flags |= EVENT_FILE_FL_FILTERED;
1532 if (old_flags != file->flags)
1533 trace_buffered_event_enable();
1536 static inline void event_set_filter(struct trace_event_file *file,
1537 struct event_filter *filter)
1539 rcu_assign_pointer(file->filter, filter);
1542 static inline void event_clear_filter(struct trace_event_file *file)
1544 RCU_INIT_POINTER(file->filter, NULL);
1548 event_set_no_set_filter_flag(struct trace_event_file *file)
1550 file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
1554 event_clear_no_set_filter_flag(struct trace_event_file *file)
1556 file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
1560 event_no_set_filter_flag(struct trace_event_file *file)
1562 if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
1568 struct filter_list {
1569 struct list_head list;
1570 struct event_filter *filter;
1573 static int process_system_preds(struct trace_subsystem_dir *dir,
1574 struct trace_array *tr,
1575 struct filter_parse_error *pe,
1576 char *filter_string)
1578 struct trace_event_file *file;
1579 struct filter_list *filter_item;
1580 struct event_filter *filter = NULL;
1581 struct filter_list *tmp;
1582 LIST_HEAD(filter_list);
1586 list_for_each_entry(file, &tr->events, list) {
1588 if (file->system != dir)
1591 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1595 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1596 if (!filter->filter_string)
1599 err = process_preds(file->event_call, filter_string, filter, pe);
1601 filter_disable(file);
1602 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1603 append_filter_err(pe, filter);
1605 event_set_filtered_flag(file);
1608 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1612 list_add_tail(&filter_item->list, &filter_list);
1614 * Regardless of if this returned an error, we still
1615 * replace the filter for the call.
1617 filter_item->filter = event_filter(file);
1618 event_set_filter(file, filter);
1628 * The calls can still be using the old filters.
1629 * Do a synchronize_sched() to ensure all calls are
1630 * done with them before we free them.
1632 synchronize_sched();
1633 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1634 __free_filter(filter_item->filter);
1635 list_del(&filter_item->list);
1640 /* No call succeeded */
1641 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1642 list_del(&filter_item->list);
1645 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1649 /* If any call succeeded, we still need to sync */
1651 synchronize_sched();
1652 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1653 __free_filter(filter_item->filter);
1654 list_del(&filter_item->list);
1660 static int create_filter_start(char *filter_string, bool set_str,
1661 struct filter_parse_error **pse,
1662 struct event_filter **filterp)
1664 struct event_filter *filter;
1665 struct filter_parse_error *pe = NULL;
1668 if (WARN_ON_ONCE(*pse || *filterp))
1671 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1672 if (filter && set_str) {
1673 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1674 if (!filter->filter_string)
1678 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1680 if (!filter || !pe || err) {
1682 __free_filter(filter);
1686 /* we're committed to creating a new filter */
1693 static void create_filter_finish(struct filter_parse_error *pe)
1699 * create_filter - create a filter for a trace_event_call
1700 * @call: trace_event_call to create a filter for
1701 * @filter_str: filter string
1702 * @set_str: remember @filter_str and enable detailed error in filter
1703 * @filterp: out param for created filter (always updated on return)
1704 * Must be a pointer that references a NULL pointer.
1706 * Creates a filter for @call with @filter_str. If @set_str is %true,
1707 * @filter_str is copied and recorded in the new filter.
1709 * On success, returns 0 and *@filterp points to the new filter. On
1710 * failure, returns -errno and *@filterp may point to %NULL or to a new
1711 * filter. In the latter case, the returned filter contains error
1712 * information if @set_str is %true and the caller is responsible for
1715 static int create_filter(struct trace_event_call *call,
1716 char *filter_string, bool set_str,
1717 struct event_filter **filterp)
1719 struct filter_parse_error *pe = NULL;
1722 /* filterp must point to NULL */
1723 if (WARN_ON(*filterp))
1726 err = create_filter_start(filter_string, set_str, &pe, filterp);
1730 err = process_preds(call, filter_string, *filterp, pe);
1732 append_filter_err(pe, *filterp);
1737 int create_event_filter(struct trace_event_call *call,
1738 char *filter_str, bool set_str,
1739 struct event_filter **filterp)
1741 return create_filter(call, filter_str, set_str, filterp);
1745 * create_system_filter - create a filter for an event_subsystem
1746 * @system: event_subsystem to create a filter for
1747 * @filter_str: filter string
1748 * @filterp: out param for created filter (always updated on return)
1750 * Identical to create_filter() except that it creates a subsystem filter
1751 * and always remembers @filter_str.
1753 static int create_system_filter(struct trace_subsystem_dir *dir,
1754 struct trace_array *tr,
1755 char *filter_str, struct event_filter **filterp)
1757 struct filter_parse_error *pe = NULL;
1760 err = create_filter_start(filter_str, true, &pe, filterp);
1762 err = process_system_preds(dir, tr, pe, filter_str);
1764 /* System filters just show a default message */
1765 kfree((*filterp)->filter_string);
1766 (*filterp)->filter_string = NULL;
1768 append_filter_err(pe, *filterp);
1771 create_filter_finish(pe);
1776 /* caller must hold event_mutex */
1777 int apply_event_filter(struct trace_event_file *file, char *filter_string)
1779 struct trace_event_call *call = file->event_call;
1780 struct event_filter *filter = NULL;
1783 if (!strcmp(strstrip(filter_string), "0")) {
1784 filter_disable(file);
1785 filter = event_filter(file);
1790 event_clear_filter(file);
1792 /* Make sure the filter is not being used */
1793 synchronize_sched();
1794 __free_filter(filter);
1799 err = create_filter(call, filter_string, true, &filter);
1802 * Always swap the call filter with the new filter
1803 * even if there was an error. If there was an error
1804 * in the filter, we disable the filter and show the error
1808 struct event_filter *tmp;
1810 tmp = event_filter(file);
1812 event_set_filtered_flag(file);
1814 filter_disable(file);
1816 event_set_filter(file, filter);
1819 /* Make sure the call is done with the filter */
1820 synchronize_sched();
1828 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1829 char *filter_string)
1831 struct event_subsystem *system = dir->subsystem;
1832 struct trace_array *tr = dir->tr;
1833 struct event_filter *filter = NULL;
1836 mutex_lock(&event_mutex);
1838 /* Make sure the system still has events */
1839 if (!dir->nr_events) {
1844 if (!strcmp(strstrip(filter_string), "0")) {
1845 filter_free_subsystem_preds(dir, tr);
1846 remove_filter_string(system->filter);
1847 filter = system->filter;
1848 system->filter = NULL;
1849 /* Ensure all filters are no longer used */
1850 synchronize_sched();
1851 filter_free_subsystem_filters(dir, tr);
1852 __free_filter(filter);
1856 err = create_system_filter(dir, tr, filter_string, &filter);
1859 * No event actually uses the system filter
1860 * we can free it without synchronize_sched().
1862 __free_filter(system->filter);
1863 system->filter = filter;
1866 mutex_unlock(&event_mutex);
1871 #ifdef CONFIG_PERF_EVENTS
1873 void ftrace_profile_free_filter(struct perf_event *event)
1875 struct event_filter *filter = event->filter;
1877 event->filter = NULL;
1878 __free_filter(filter);
1881 struct function_filter_data {
1882 struct ftrace_ops *ops;
1887 #ifdef CONFIG_FUNCTION_TRACER
1889 ftrace_function_filter_re(char *buf, int len, int *count)
1893 str = kstrndup(buf, len, GFP_KERNEL);
1898 * The argv_split function takes white space
1899 * as a separator, so convert ',' into spaces.
1901 strreplace(str, ',', ' ');
1903 re = argv_split(GFP_KERNEL, str, count);
1908 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
1909 int reset, char *re, int len)
1914 ret = ftrace_set_filter(ops, re, len, reset);
1916 ret = ftrace_set_notrace(ops, re, len, reset);
1921 static int __ftrace_function_set_filter(int filter, char *buf, int len,
1922 struct function_filter_data *data)
1924 int i, re_cnt, ret = -EINVAL;
1928 reset = filter ? &data->first_filter : &data->first_notrace;
1931 * The 'ip' field could have multiple filters set, separated
1932 * either by space or comma. We first cut the filter and apply
1933 * all pieces separatelly.
1935 re = ftrace_function_filter_re(buf, len, &re_cnt);
1939 for (i = 0; i < re_cnt; i++) {
1940 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
1941 re[i], strlen(re[i]));
1953 static int ftrace_function_check_pred(struct filter_pred *pred)
1955 struct ftrace_event_field *field = pred->field;
1958 * Check the predicate for function trace, verify:
1959 * - only '==' and '!=' is used
1960 * - the 'ip' field is used
1962 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
1965 if (strcmp(field->name, "ip"))
1971 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
1972 struct function_filter_data *data)
1976 /* Checking the node is valid for function trace. */
1977 ret = ftrace_function_check_pred(pred);
1981 return __ftrace_function_set_filter(pred->op == OP_EQ,
1982 pred->regex.pattern,
1987 static bool is_or(struct prog_entry *prog, int i)
1992 * Only "||" is allowed for function events, thus,
1993 * all true branches should jump to true, and any
1994 * false branch should jump to false.
1996 target = prog[i].target + 1;
1997 /* True and false have NULL preds (all prog entries should jump to one */
1998 if (prog[target].pred)
2001 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2002 return prog[i].when_to_branch == prog[target].target;
2005 static int ftrace_function_set_filter(struct perf_event *event,
2006 struct event_filter *filter)
2008 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2009 lockdep_is_held(&event_mutex));
2010 struct function_filter_data data = {
2013 .ops = &event->ftrace_ops,
2017 for (i = 0; prog[i].pred; i++) {
2018 struct filter_pred *pred = prog[i].pred;
2020 if (!is_or(prog, i))
2023 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2029 static int ftrace_function_set_filter(struct perf_event *event,
2030 struct event_filter *filter)
2034 #endif /* CONFIG_FUNCTION_TRACER */
2036 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2040 struct event_filter *filter = NULL;
2041 struct trace_event_call *call;
2043 mutex_lock(&event_mutex);
2045 call = event->tp_event;
2055 err = create_filter(call, filter_str, false, &filter);
2059 if (ftrace_event_is_function(call))
2060 err = ftrace_function_set_filter(event, filter);
2062 event->filter = filter;
2065 if (err || ftrace_event_is_function(call))
2066 __free_filter(filter);
2069 mutex_unlock(&event_mutex);
2074 #endif /* CONFIG_PERF_EVENTS */
2076 #ifdef CONFIG_FTRACE_STARTUP_TEST
2078 #include <linux/types.h>
2079 #include <linux/tracepoint.h>
2081 #define CREATE_TRACE_POINTS
2082 #include "trace_events_filter_test.h"
2084 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2087 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2088 .e = ve, .f = vf, .g = vg, .h = vh }, \
2090 .not_visited = nvisit, \
2095 static struct test_filter_data_t {
2097 struct trace_event_raw_ftrace_test_filter rec;
2100 } test_filter_data[] = {
2101 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2102 "e == 1 && f == 1 && g == 1 && h == 1"
2103 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2104 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2105 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2107 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2108 "e == 1 || f == 1 || g == 1 || h == 1"
2109 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2110 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2111 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2113 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2114 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2115 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2116 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2117 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2118 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2120 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2121 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2122 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2123 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2124 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2126 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2127 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2128 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2129 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2130 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2132 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2133 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2134 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2135 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2136 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2138 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2139 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2140 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2141 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2142 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2144 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2145 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2146 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2147 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2148 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2156 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2158 static int test_pred_visited;
2160 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2162 struct ftrace_event_field *field = pred->field;
2164 test_pred_visited = 1;
2165 printk(KERN_INFO "\npred visited %s\n", field->name);
2169 static void update_pred_fn(struct event_filter *filter, char *fields)
2171 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2172 lockdep_is_held(&event_mutex));
2175 for (i = 0; prog[i].pred; i++) {
2176 struct filter_pred *pred = prog[i].pred;
2177 struct ftrace_event_field *field = pred->field;
2179 WARN_ON_ONCE(!pred->fn);
2182 WARN_ONCE(1, "all leafs should have field defined %d", i);
2186 if (!strchr(fields, *field->name))
2189 pred->fn = test_pred_visited_fn;
2193 static __init int ftrace_test_event_filter(void)
2197 printk(KERN_INFO "Testing ftrace filter: ");
2199 for (i = 0; i < DATA_CNT; i++) {
2200 struct event_filter *filter = NULL;
2201 struct test_filter_data_t *d = &test_filter_data[i];
2204 err = create_filter(&event_ftrace_test_filter, d->filter,
2208 "Failed to get filter for '%s', err %d\n",
2210 __free_filter(filter);
2214 /* Needed to dereference filter->prog */
2215 mutex_lock(&event_mutex);
2217 * The preemption disabling is not really needed for self
2218 * tests, but the rcu dereference will complain without it.
2221 if (*d->not_visited)
2222 update_pred_fn(filter, d->not_visited);
2224 test_pred_visited = 0;
2225 err = filter_match_preds(filter, &d->rec);
2228 mutex_unlock(&event_mutex);
2230 __free_filter(filter);
2232 if (test_pred_visited) {
2234 "Failed, unwanted pred visited for filter %s\n",
2239 if (err != d->match) {
2241 "Failed to match filter '%s', expected %d\n",
2242 d->filter, d->match);
2248 printk(KERN_CONT "OK\n");
2253 late_initcall(ftrace_test_event_filter);
2255 #endif /* CONFIG_FTRACE_STARTUP_TEST */