1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_filter - generic event filtering
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
8 #include <linux/module.h>
9 #include <linux/ctype.h>
10 #include <linux/mutex.h>
11 #include <linux/perf_event.h>
12 #include <linux/slab.h>
15 #include "trace_output.h"
17 #define DEFAULT_SYS_FILTER_MESSAGE \
18 "### global filter ###\n" \
19 "# Use this to set filters for multiple events.\n" \
20 "# Only events with the given fields will be affected.\n" \
21 "# If no events are modified, an error message will be displayed here"
23 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
38 enum filter_op_ids { OPS };
43 static const char * ops[] = { OPS };
46 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
47 * pred_funcs_##type below must match the order of them above.
49 #define PRED_FUNC_START OP_LE
50 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
53 C(NONE, "No error"), \
54 C(INVALID_OP, "Invalid operator"), \
55 C(TOO_MANY_OPEN, "Too many '('"), \
56 C(TOO_MANY_CLOSE, "Too few '('"), \
57 C(MISSING_QUOTE, "Missing matching quote"), \
58 C(OPERAND_TOO_LONG, "Operand too long"), \
59 C(EXPECT_STRING, "Expecting string field"), \
60 C(EXPECT_DIGIT, "Expecting numeric field"), \
61 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
62 C(FIELD_NOT_FOUND, "Field not found"), \
63 C(ILLEGAL_INTVAL, "Illegal integer value"), \
64 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
65 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
66 C(INVALID_FILTER, "Meaningless filter expression"), \
67 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
68 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
70 C(NO_FILTER, "No filter found")
73 #define C(a, b) FILT_ERR_##a
80 static const char *err_text[] = { ERRORS };
82 /* Called after a '!' character but "!=" and "!~" are not "not"s */
83 static bool is_not(const char *str)
94 * prog_entry - a singe entry in the filter program
95 * @target: Index to jump to on a branch (actually one minus the index)
96 * @when_to_branch: The value of the result of the predicate to do a branch
97 * @pred: The predicate to execute.
102 struct filter_pred *pred;
106 * update_preds- assign a program entry a label target
107 * @prog: The program array
108 * @N: The index of the current entry in @prog
109 * @when_to_branch: What to assign a program entry for its branch condition
111 * The program entry at @N has a target that points to the index of a program
112 * entry that can have its target and when_to_branch fields updated.
113 * Update the current program entry denoted by index @N target field to be
114 * that of the updated entry. This will denote the entry to update if
115 * we are processing an "||" after an "&&"
117 static void update_preds(struct prog_entry *prog, int N, int invert)
123 prog[t].when_to_branch = invert;
128 struct filter_parse_error {
133 static void parse_error(struct filter_parse_error *pe, int err, int pos)
136 pe->lasterr_pos = pos;
139 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
140 struct filter_parse_error *pe,
141 struct filter_pred **pred);
150 * Without going into a formal proof, this explains the method that is used in
151 * parsing the logical expressions.
153 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
154 * The first pass will convert it into the following program:
156 * n1: r=a; l1: if (!r) goto l4;
157 * n2: r=b; l2: if (!r) goto l4;
158 * n3: r=c; r=!r; l3: if (r) goto l4;
159 * n4: r=g; r=!r; l4: if (r) goto l5;
160 * n5: r=d; l5: if (r) goto T
161 * n6: r=e; l6: if (!r) goto l7;
162 * n7: r=f; r=!r; l7: if (!r) goto F
166 * To do this, we use a data structure to represent each of the above
167 * predicate and conditions that has:
169 * predicate, when_to_branch, invert, target
171 * The "predicate" will hold the function to determine the result "r".
172 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
173 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
174 * The "invert" holds whether the value should be reversed before testing.
175 * The "target" contains the label "l#" to jump to.
177 * A stack is created to hold values when parentheses are used.
179 * To simplify the logic, the labels will start at 0 and not 1.
181 * The possible invert values are 1 and 0. The number of "!"s that are in scope
182 * before the predicate determines the invert value, if the number is odd then
183 * the invert value is 1 and 0 otherwise. This means the invert value only
184 * needs to be toggled when a new "!" is introduced compared to what is stored
185 * on the stack, where parentheses were used.
187 * The top of the stack and "invert" are initialized to zero.
191 * #1 A loop through all the tokens is done:
193 * #2 If the token is an "(", the stack is push, and the current stack value
194 * gets the current invert value, and the loop continues to the next token.
195 * The top of the stack saves the "invert" value to keep track of what
196 * the current inversion is. As "!(a && !b || c)" would require all
197 * predicates being affected separately by the "!" before the parentheses.
198 * And that would end up being equivalent to "(!a || b) && !c"
200 * #3 If the token is an "!", the current "invert" value gets inverted, and
201 * the loop continues. Note, if the next token is a predicate, then
202 * this "invert" value is only valid for the current program entry,
203 * and does not affect other predicates later on.
205 * The only other acceptable token is the predicate string.
207 * #4 A new entry into the program is added saving: the predicate and the
208 * current value of "invert". The target is currently assigned to the
209 * previous program index (this will not be its final value).
211 * #5 We now enter another loop and look at the next token. The only valid
212 * tokens are ")", "&&", "||" or end of the input string "\0".
214 * #6 The invert variable is reset to the current value saved on the top of
217 * #7 The top of the stack holds not only the current invert value, but also
218 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
219 * precedence than "||". That is "a && b || c && d" is equivalent to
220 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
221 * to be processed. This is the case if an "&&" was the last token. If it was
222 * then we call update_preds(). This takes the program, the current index in
223 * the program, and the current value of "invert". More will be described
224 * below about this function.
226 * #8 If the next token is "&&" then we set a flag in the top of the stack
227 * that denotes that "&&" needs to be processed, break out of this loop
228 * and continue with the outer loop.
230 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
231 * This is called with the program, the current index in the program, but
232 * this time with an inverted value of "invert" (that is !invert). This is
233 * because the value taken will become the "when_to_branch" value of the
235 * Note, this is called when the next token is not an "&&". As stated before,
236 * "&&" takes higher precedence, and "||" should not be processed yet if the
237 * next logical operation is "&&".
239 * #10 If the next token is "||" then we set a flag in the top of the stack
240 * that denotes that "||" needs to be processed, break out of this loop
241 * and continue with the outer loop.
243 * #11 If this is the end of the input string "\0" then we break out of both
246 * #12 Otherwise, the next token is ")", where we pop the stack and continue
249 * Now to discuss the update_pred() function, as that is key to the setting up
250 * of the program. Remember the "target" of the program is initialized to the
251 * previous index and not the "l" label. The target holds the index into the
252 * program that gets affected by the operand. Thus if we have something like
253 * "a || b && c", when we process "a" the target will be "-1" (undefined).
254 * When we process "b", its target is "0", which is the index of "a", as that's
255 * the predicate that is affected by "||". But because the next token after "b"
256 * is "&&" we don't call update_preds(). Instead continue to "c". As the
257 * next token after "c" is not "&&" but the end of input, we first process the
258 * "&&" by calling update_preds() for the "&&" then we process the "||" by
259 * callin updates_preds() with the values for processing "||".
261 * What does that mean? What update_preds() does is to first save the "target"
262 * of the program entry indexed by the current program entry's "target"
263 * (remember the "target" is initialized to previous program entry), and then
264 * sets that "target" to the current index which represents the label "l#".
265 * That entry's "when_to_branch" is set to the value passed in (the "invert"
266 * or "!invert"). Then it sets the current program entry's target to the saved
267 * "target" value (the old value of the program that had its "target" updated
270 * Looking back at "a || b && c", we have the following steps:
271 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
272 * "||" - flag that we need to process "||"; continue outer loop
273 * "b" - prog[1] = { "b", X, 0 }
274 * "&&" - flag that we need to process "&&"; continue outer loop
275 * (Notice we did not process "||")
276 * "c" - prog[2] = { "c", X, 1 }
277 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
278 * t = prog[2].target; // t = 1
279 * s = prog[t].target; // s = 0
280 * prog[t].target = 2; // Set target to "l2"
281 * prog[t].when_to_branch = 0;
282 * prog[2].target = s;
283 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
284 * t = prog[2].target; // t = 0
285 * s = prog[t].target; // s = -1
286 * prog[t].target = 2; // Set target to "l2"
287 * prog[t].when_to_branch = 1;
288 * prog[2].target = s;
290 * #13 Which brings us to the final step of the first pass, which is to set
291 * the last program entry's when_to_branch and target, which will be
292 * when_to_branch = 0; target = N; ( the label after the program entry after
293 * the last program entry processed above).
295 * If we denote "TRUE" to be the entry after the last program entry processed,
296 * and "FALSE" the program entry after that, we are now done with the first
299 * Making the above "a || b && c" have a progam of:
300 * prog[0] = { "a", 1, 2 }
301 * prog[1] = { "b", 0, 2 }
302 * prog[2] = { "c", 0, 3 }
304 * Which translates into:
305 * n0: r = a; l0: if (r) goto l2;
306 * n1: r = b; l1: if (!r) goto l2;
307 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
308 * T: return TRUE; l3:
311 * Although, after the first pass, the program is correct, it is
312 * inefficient. The simple sample of "a || b && c" could be easily been
314 * n0: r = a; if (r) goto T
315 * n1: r = b; if (!r) goto F
316 * n2: r = c; if (!r) goto F
320 * The First Pass is over the input string. The next too passes are over
321 * the program itself.
325 * Which brings us to the second pass. If a jump to a label has the
326 * same condition as that label, it can instead jump to its target.
327 * The original example of "a && !(!b || (c && g)) || d || e && !f"
328 * where the first pass gives us:
330 * n1: r=a; l1: if (!r) goto l4;
331 * n2: r=b; l2: if (!r) goto l4;
332 * n3: r=c; r=!r; l3: if (r) goto l4;
333 * n4: r=g; r=!r; l4: if (r) goto l5;
334 * n5: r=d; l5: if (r) goto T
335 * n6: r=e; l6: if (!r) goto l7;
336 * n7: r=f; r=!r; l7: if (!r) goto F:
340 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
341 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
342 * to go directly to T. To accomplish this, we start from the last
343 * entry in the program and work our way back. If the target of the entry
344 * has the same "when_to_branch" then we could use that entry's target.
345 * Doing this, the above would end up as:
347 * n1: r=a; l1: if (!r) goto l4;
348 * n2: r=b; l2: if (!r) goto l4;
349 * n3: r=c; r=!r; l3: if (r) goto T;
350 * n4: r=g; r=!r; l4: if (r) goto T;
351 * n5: r=d; l5: if (r) goto T;
352 * n6: r=e; l6: if (!r) goto F;
353 * n7: r=f; r=!r; l7: if (!r) goto F;
357 * In that same pass, if the "when_to_branch" doesn't match, we can simply
358 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
359 * where "l4: if (r) goto T;", then we can convert l2 to be:
360 * "l2: if (!r) goto n5;".
362 * This will have the second pass give us:
363 * n1: r=a; l1: if (!r) goto n5;
364 * n2: r=b; l2: if (!r) goto n5;
365 * n3: r=c; r=!r; l3: if (r) goto T;
366 * n4: r=g; r=!r; l4: if (r) goto T;
367 * n5: r=d; l5: if (r) goto T
368 * n6: r=e; l6: if (!r) goto F;
369 * n7: r=f; r=!r; l7: if (!r) goto F
373 * Notice, all the "l#" labels are no longer used, and they can now
378 * For the third pass we deal with the inverts. As they simply just
379 * make the "when_to_branch" get inverted, a simple loop over the
380 * program to that does: "when_to_branch ^= invert;" will do the
381 * job, leaving us with:
382 * n1: r=a; if (!r) goto n5;
383 * n2: r=b; if (!r) goto n5;
384 * n3: r=c: if (!r) goto T;
385 * n4: r=g; if (!r) goto T;
386 * n5: r=d; if (r) goto T
387 * n6: r=e; if (!r) goto F;
388 * n7: r=f; if (r) goto F
392 * As "r = a; if (!r) goto n5;" is obviously the same as
393 * "if (!a) goto n5;" without doing anything we can interperate the
395 * n1: if (!a) goto n5;
396 * n2: if (!b) goto n5;
397 * n3: if (!c) goto T;
398 * n4: if (!g) goto T;
400 * n6: if (!e) goto F;
405 * Since the inverts are discarded at the end, there's no reason to store
406 * them in the program array (and waste memory). A separate array to hold
407 * the inverts is used and freed at the end.
409 static struct prog_entry *
410 predicate_parse(const char *str, int nr_parens, int nr_preds,
411 parse_pred_fn parse_pred, void *data,
412 struct filter_parse_error *pe)
414 struct prog_entry *prog_stack;
415 struct prog_entry *prog;
416 const char *ptr = str;
417 char *inverts = NULL;
426 nr_preds += 2; /* For TRUE and FALSE */
428 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
430 return ERR_PTR(-ENOMEM);
431 prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
433 parse_error(pe, -ENOMEM, 0);
436 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
438 parse_error(pe, -ENOMEM, 0);
447 while (*ptr) { /* #1 */
448 const char *next = ptr++;
455 if (top - op_stack > nr_parens)
456 return ERR_PTR(-EINVAL);
467 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
471 inverts[N] = invert; /* #4 */
472 prog[N].target = N-1;
474 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
495 /* accepting only "&&" or "||" */
496 if (next[1] == next[0]) {
502 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
507 invert = *top & INVERT;
509 if (*top & PROCESS_AND) { /* #7 */
510 update_preds(prog, N - 1, invert);
511 *top &= ~PROCESS_AND;
513 if (*next == '&') { /* #8 */
517 if (*top & PROCESS_OR) { /* #9 */
518 update_preds(prog, N - 1, !invert);
521 if (*next == '|') { /* #10 */
525 if (!*next) /* #11 */
528 if (top == op_stack) {
531 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
538 if (top != op_stack) {
540 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
547 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
551 prog[N].pred = NULL; /* #13 */
552 prog[N].target = 1; /* TRUE */
553 prog[N+1].pred = NULL;
554 prog[N+1].target = 0; /* FALSE */
555 prog[N-1].target = N;
556 prog[N-1].when_to_branch = false;
559 for (i = N-1 ; i--; ) {
560 int target = prog[i].target;
561 if (prog[i].when_to_branch == prog[target].when_to_branch)
562 prog[i].target = prog[target].target;
566 for (i = 0; i < N; i++) {
567 invert = inverts[i] ^ prog[i].when_to_branch;
568 prog[i].when_to_branch = invert;
569 /* Make sure the program always moves forward */
570 if (WARN_ON(prog[i].target <= i)) {
583 for (i = 0; prog_stack[i].pred; i++)
584 kfree(prog_stack[i].pred);
590 #define DEFINE_COMPARISON_PRED(type) \
591 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
593 type *addr = (type *)(event + pred->offset); \
594 type val = (type)pred->val; \
595 return *addr < val; \
597 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
599 type *addr = (type *)(event + pred->offset); \
600 type val = (type)pred->val; \
601 return *addr <= val; \
603 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
605 type *addr = (type *)(event + pred->offset); \
606 type val = (type)pred->val; \
607 return *addr > val; \
609 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
611 type *addr = (type *)(event + pred->offset); \
612 type val = (type)pred->val; \
613 return *addr >= val; \
615 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
617 type *addr = (type *)(event + pred->offset); \
618 type val = (type)pred->val; \
619 return !!(*addr & val); \
621 static const filter_pred_fn_t pred_funcs_##type[] = { \
622 filter_pred_LE_##type, \
623 filter_pred_LT_##type, \
624 filter_pred_GE_##type, \
625 filter_pred_GT_##type, \
626 filter_pred_BAND_##type, \
629 #define DEFINE_EQUALITY_PRED(size) \
630 static int filter_pred_##size(struct filter_pred *pred, void *event) \
632 u##size *addr = (u##size *)(event + pred->offset); \
633 u##size val = (u##size)pred->val; \
636 match = (val == *addr) ^ pred->not; \
641 DEFINE_COMPARISON_PRED(s64);
642 DEFINE_COMPARISON_PRED(u64);
643 DEFINE_COMPARISON_PRED(s32);
644 DEFINE_COMPARISON_PRED(u32);
645 DEFINE_COMPARISON_PRED(s16);
646 DEFINE_COMPARISON_PRED(u16);
647 DEFINE_COMPARISON_PRED(s8);
648 DEFINE_COMPARISON_PRED(u8);
650 DEFINE_EQUALITY_PRED(64);
651 DEFINE_EQUALITY_PRED(32);
652 DEFINE_EQUALITY_PRED(16);
653 DEFINE_EQUALITY_PRED(8);
655 /* Filter predicate for fixed sized arrays of characters */
656 static int filter_pred_string(struct filter_pred *pred, void *event)
658 char *addr = (char *)(event + pred->offset);
661 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
663 match = cmp ^ pred->not;
668 /* Filter predicate for char * pointers */
669 static int filter_pred_pchar(struct filter_pred *pred, void *event)
671 char **addr = (char **)(event + pred->offset);
673 int len = strlen(*addr) + 1; /* including tailing '\0' */
675 cmp = pred->regex.match(*addr, &pred->regex, len);
677 match = cmp ^ pred->not;
683 * Filter predicate for dynamic sized arrays of characters.
684 * These are implemented through a list of strings at the end
686 * Also each of these strings have a field in the entry which
687 * contains its offset from the beginning of the entry.
688 * We have then first to get this field, dereference it
689 * and add it to the address of the entry, and at last we have
690 * the address of the string.
692 static int filter_pred_strloc(struct filter_pred *pred, void *event)
694 u32 str_item = *(u32 *)(event + pred->offset);
695 int str_loc = str_item & 0xffff;
696 int str_len = str_item >> 16;
697 char *addr = (char *)(event + str_loc);
700 cmp = pred->regex.match(addr, &pred->regex, str_len);
702 match = cmp ^ pred->not;
707 /* Filter predicate for CPUs. */
708 static int filter_pred_cpu(struct filter_pred *pred, void *event)
712 cpu = raw_smp_processor_id();
733 /* Filter predicate for COMM. */
734 static int filter_pred_comm(struct filter_pred *pred, void *event)
738 cmp = pred->regex.match(current->comm, &pred->regex,
740 return cmp ^ pred->not;
743 static int filter_pred_none(struct filter_pred *pred, void *event)
749 * regex_match_foo - Basic regex callbacks
751 * @str: the string to be searched
752 * @r: the regex structure containing the pattern string
753 * @len: the length of the string to be searched (including '\0')
756 * - @str might not be NULL-terminated if it's of type DYN_STRING
757 * or STATIC_STRING, unless @len is zero.
760 static int regex_match_full(char *str, struct regex *r, int len)
762 /* len of zero means str is dynamic and ends with '\0' */
764 return strcmp(str, r->pattern) == 0;
766 return strncmp(str, r->pattern, len) == 0;
769 static int regex_match_front(char *str, struct regex *r, int len)
771 if (len && len < r->len)
774 return strncmp(str, r->pattern, r->len) == 0;
777 static int regex_match_middle(char *str, struct regex *r, int len)
780 return strstr(str, r->pattern) != NULL;
782 return strnstr(str, r->pattern, len) != NULL;
785 static int regex_match_end(char *str, struct regex *r, int len)
787 int strlen = len - 1;
789 if (strlen >= r->len &&
790 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
795 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
797 if (glob_match(r->pattern, str))
803 * filter_parse_regex - parse a basic regex
804 * @buff: the raw regex
805 * @len: length of the regex
806 * @search: will point to the beginning of the string to compare
807 * @not: tell whether the match will have to be inverted
809 * This passes in a buffer containing a regex and this function will
810 * set search to point to the search part of the buffer and
811 * return the type of search it is (see enum above).
812 * This does modify buff.
815 * search returns the pointer to use for comparison.
816 * not returns 1 if buff started with a '!'
819 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
821 int type = MATCH_FULL;
824 if (buff[0] == '!') {
833 if (isdigit(buff[0]))
836 for (i = 0; i < len; i++) {
837 if (buff[i] == '*') {
839 type = MATCH_END_ONLY;
840 } else if (i == len - 1) {
841 if (type == MATCH_END_ONLY)
842 type = MATCH_MIDDLE_ONLY;
844 type = MATCH_FRONT_ONLY;
847 } else { /* pattern continues, use full glob */
850 } else if (strchr("[?\\", buff[i])) {
860 static void filter_build_regex(struct filter_pred *pred)
862 struct regex *r = &pred->regex;
864 enum regex_type type = MATCH_FULL;
866 if (pred->op == OP_GLOB) {
867 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
868 r->len = strlen(search);
869 memmove(r->pattern, search, r->len+1);
873 /* MATCH_INDEX should not happen, but if it does, match full */
876 r->match = regex_match_full;
878 case MATCH_FRONT_ONLY:
879 r->match = regex_match_front;
881 case MATCH_MIDDLE_ONLY:
882 r->match = regex_match_middle;
885 r->match = regex_match_end;
888 r->match = regex_match_glob;
893 /* return 1 if event matches, 0 otherwise (discard) */
894 int filter_match_preds(struct event_filter *filter, void *rec)
896 struct prog_entry *prog;
899 /* no filter is considered a match */
903 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
904 prog = rcu_dereference_raw(filter->prog);
908 for (i = 0; prog[i].pred; i++) {
909 struct filter_pred *pred = prog[i].pred;
910 int match = pred->fn(pred, rec);
911 if (match == prog[i].when_to_branch)
914 return prog[i].target;
916 EXPORT_SYMBOL_GPL(filter_match_preds);
918 static void remove_filter_string(struct event_filter *filter)
923 kfree(filter->filter_string);
924 filter->filter_string = NULL;
927 static void append_filter_err(struct trace_array *tr,
928 struct filter_parse_error *pe,
929 struct event_filter *filter)
932 int pos = pe->lasterr_pos;
936 if (WARN_ON(!filter->filter_string))
939 s = kmalloc(sizeof(*s), GFP_KERNEL);
944 len = strlen(filter->filter_string);
948 /* indexing is off by one */
952 trace_seq_puts(s, filter->filter_string);
953 if (pe->lasterr > 0) {
954 trace_seq_printf(s, "\n%*s", pos, "^");
955 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
956 tracing_log_err(tr, "event filter parse error",
957 filter->filter_string, err_text,
958 pe->lasterr, pe->lasterr_pos);
960 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
961 tracing_log_err(tr, "event filter parse error",
962 filter->filter_string, err_text,
965 trace_seq_putc(s, 0);
966 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
968 kfree(filter->filter_string);
969 filter->filter_string = buf;
974 static inline struct event_filter *event_filter(struct trace_event_file *file)
979 /* caller must hold event_mutex */
980 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
982 struct event_filter *filter = event_filter(file);
984 if (filter && filter->filter_string)
985 trace_seq_printf(s, "%s\n", filter->filter_string);
987 trace_seq_puts(s, "none\n");
990 void print_subsystem_event_filter(struct event_subsystem *system,
993 struct event_filter *filter;
995 mutex_lock(&event_mutex);
996 filter = system->filter;
997 if (filter && filter->filter_string)
998 trace_seq_printf(s, "%s\n", filter->filter_string);
1000 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1001 mutex_unlock(&event_mutex);
1004 static void free_prog(struct event_filter *filter)
1006 struct prog_entry *prog;
1009 prog = rcu_access_pointer(filter->prog);
1013 for (i = 0; prog[i].pred; i++)
1014 kfree(prog[i].pred);
1018 static void filter_disable(struct trace_event_file *file)
1020 unsigned long old_flags = file->flags;
1022 file->flags &= ~EVENT_FILE_FL_FILTERED;
1024 if (old_flags != file->flags)
1025 trace_buffered_event_disable();
1028 static void __free_filter(struct event_filter *filter)
1034 kfree(filter->filter_string);
1038 void free_event_filter(struct event_filter *filter)
1040 __free_filter(filter);
1043 static inline void __remove_filter(struct trace_event_file *file)
1045 filter_disable(file);
1046 remove_filter_string(file->filter);
1049 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1050 struct trace_array *tr)
1052 struct trace_event_file *file;
1054 list_for_each_entry(file, &tr->events, list) {
1055 if (file->system != dir)
1057 __remove_filter(file);
1061 static inline void __free_subsystem_filter(struct trace_event_file *file)
1063 __free_filter(file->filter);
1064 file->filter = NULL;
1067 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1068 struct trace_array *tr)
1070 struct trace_event_file *file;
1072 list_for_each_entry(file, &tr->events, list) {
1073 if (file->system != dir)
1075 __free_subsystem_filter(file);
1079 int filter_assign_type(const char *type)
1081 if (strstr(type, "__data_loc") && strstr(type, "char"))
1082 return FILTER_DYN_STRING;
1084 if (strchr(type, '[') && strstr(type, "char"))
1085 return FILTER_STATIC_STRING;
1087 return FILTER_OTHER;
1090 static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1091 int field_size, int field_is_signed)
1093 filter_pred_fn_t fn = NULL;
1094 int pred_func_index = -1;
1101 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1103 pred_func_index = op - PRED_FUNC_START;
1104 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1108 switch (field_size) {
1110 if (pred_func_index < 0)
1111 fn = filter_pred_64;
1112 else if (field_is_signed)
1113 fn = pred_funcs_s64[pred_func_index];
1115 fn = pred_funcs_u64[pred_func_index];
1118 if (pred_func_index < 0)
1119 fn = filter_pred_32;
1120 else if (field_is_signed)
1121 fn = pred_funcs_s32[pred_func_index];
1123 fn = pred_funcs_u32[pred_func_index];
1126 if (pred_func_index < 0)
1127 fn = filter_pred_16;
1128 else if (field_is_signed)
1129 fn = pred_funcs_s16[pred_func_index];
1131 fn = pred_funcs_u16[pred_func_index];
1134 if (pred_func_index < 0)
1136 else if (field_is_signed)
1137 fn = pred_funcs_s8[pred_func_index];
1139 fn = pred_funcs_u8[pred_func_index];
1146 /* Called when a predicate is encountered by predicate_parse() */
1147 static int parse_pred(const char *str, void *data,
1148 int pos, struct filter_parse_error *pe,
1149 struct filter_pred **pred_ptr)
1151 struct trace_event_call *call = data;
1152 struct ftrace_event_field *field;
1153 struct filter_pred *pred = NULL;
1154 char num_buf[24]; /* Big enough to hold an address */
1164 /* First find the field to associate to */
1165 while (isspace(str[i]))
1169 while (isalnum(str[i]) || str[i] == '_')
1177 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1181 /* Make sure that the field exists */
1183 field = trace_find_event_field(call, field_name);
1186 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1190 while (isspace(str[i]))
1193 /* Make sure this op is supported */
1194 for (op = 0; ops[op]; op++) {
1195 /* This is why '<=' must come before '<' in ops[] */
1196 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1201 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1205 i += strlen(ops[op]);
1207 while (isspace(str[i]))
1212 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1216 pred->field = field;
1217 pred->offset = field->offset;
1220 if (ftrace_event_is_function(call)) {
1222 * Perf does things different with function events.
1223 * It only allows an "ip" field, and expects a string.
1224 * But the string does not need to be surrounded by quotes.
1225 * If it is a string, the assigned function as a nop,
1226 * (perf doesn't use it) and grab everything.
1228 if (strcmp(field->name, "ip") != 0) {
1229 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1232 pred->fn = filter_pred_none;
1235 * Quotes are not required, but if they exist then we need
1236 * to read them till we hit a matching one.
1238 if (str[i] == '\'' || str[i] == '"')
1243 for (i++; str[i]; i++) {
1244 if (q && str[i] == q)
1246 if (!q && (str[i] == ')' || str[i] == '&' ||
1254 if (len >= MAX_FILTER_STR_VAL) {
1255 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1259 pred->regex.len = len;
1260 strncpy(pred->regex.pattern, str + s, len);
1261 pred->regex.pattern[len] = 0;
1263 /* This is either a string, or an integer */
1264 } else if (str[i] == '\'' || str[i] == '"') {
1267 /* Make sure the op is OK for strings */
1276 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1280 /* Make sure the field is OK for strings */
1281 if (!is_string_field(field)) {
1282 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1286 for (i++; str[i]; i++) {
1291 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1298 if (len >= MAX_FILTER_STR_VAL) {
1299 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1303 pred->regex.len = len;
1304 strncpy(pred->regex.pattern, str + s, len);
1305 pred->regex.pattern[len] = 0;
1307 filter_build_regex(pred);
1309 if (field->filter_type == FILTER_COMM) {
1310 pred->fn = filter_pred_comm;
1312 } else if (field->filter_type == FILTER_STATIC_STRING) {
1313 pred->fn = filter_pred_string;
1314 pred->regex.field_len = field->size;
1316 } else if (field->filter_type == FILTER_DYN_STRING)
1317 pred->fn = filter_pred_strloc;
1319 pred->fn = filter_pred_pchar;
1320 /* go past the last quote */
1323 } else if (isdigit(str[i]) || str[i] == '-') {
1325 /* Make sure the field is not a string */
1326 if (is_string_field(field)) {
1327 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1331 if (op == OP_GLOB) {
1332 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1339 /* We allow 0xDEADBEEF */
1340 while (isalnum(str[i]))
1344 /* 0xfeedfacedeadbeef is 18 chars max */
1345 if (len >= sizeof(num_buf)) {
1346 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1350 strncpy(num_buf, str + s, len);
1353 /* Make sure it is a value */
1354 if (field->is_signed)
1355 ret = kstrtoll(num_buf, 0, &val);
1357 ret = kstrtoull(num_buf, 0, &val);
1359 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1365 if (field->filter_type == FILTER_CPU)
1366 pred->fn = filter_pred_cpu;
1368 pred->fn = select_comparison_fn(pred->op, field->size,
1370 if (pred->op == OP_NE)
1375 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1388 TOO_MANY_CLOSE = -1,
1394 * Read the filter string once to calculate the number of predicates
1395 * as well as how deep the parentheses go.
1398 * 0 - everything is fine (err is undefined)
1401 * -3 - No matching quote
1403 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1405 bool is_pred = false;
1407 int open = 1; /* Count the expression as "(E)" */
1415 for (i = 0; str[i]; i++) {
1416 if (isspace(str[i]))
1419 if (str[i] == quote)
1432 if (str[i+1] != str[i])
1439 if (open > max_open)
1446 return TOO_MANY_CLOSE;
1459 return MISSING_QUOTE;
1465 /* find the bad open */
1468 if (str[i] == quote)
1474 if (level == open) {
1476 return TOO_MANY_OPEN;
1489 /* First character is the '(' with missing ')' */
1491 return TOO_MANY_OPEN;
1494 /* Set the size of the required stacks */
1500 static int process_preds(struct trace_event_call *call,
1501 const char *filter_string,
1502 struct event_filter *filter,
1503 struct filter_parse_error *pe)
1505 struct prog_entry *prog;
1511 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1515 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1518 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1521 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
1529 prog = predicate_parse(filter_string, nr_parens, nr_preds,
1530 parse_pred, call, pe);
1532 return PTR_ERR(prog);
1534 rcu_assign_pointer(filter->prog, prog);
1538 static inline void event_set_filtered_flag(struct trace_event_file *file)
1540 unsigned long old_flags = file->flags;
1542 file->flags |= EVENT_FILE_FL_FILTERED;
1544 if (old_flags != file->flags)
1545 trace_buffered_event_enable();
1548 static inline void event_set_filter(struct trace_event_file *file,
1549 struct event_filter *filter)
1551 rcu_assign_pointer(file->filter, filter);
1554 static inline void event_clear_filter(struct trace_event_file *file)
1556 RCU_INIT_POINTER(file->filter, NULL);
1560 event_set_no_set_filter_flag(struct trace_event_file *file)
1562 file->flags |= EVENT_FILE_FL_NO_SET_FILTER;
1566 event_clear_no_set_filter_flag(struct trace_event_file *file)
1568 file->flags &= ~EVENT_FILE_FL_NO_SET_FILTER;
1572 event_no_set_filter_flag(struct trace_event_file *file)
1574 if (file->flags & EVENT_FILE_FL_NO_SET_FILTER)
1580 struct filter_list {
1581 struct list_head list;
1582 struct event_filter *filter;
1585 static int process_system_preds(struct trace_subsystem_dir *dir,
1586 struct trace_array *tr,
1587 struct filter_parse_error *pe,
1588 char *filter_string)
1590 struct trace_event_file *file;
1591 struct filter_list *filter_item;
1592 struct event_filter *filter = NULL;
1593 struct filter_list *tmp;
1594 LIST_HEAD(filter_list);
1598 list_for_each_entry(file, &tr->events, list) {
1600 if (file->system != dir)
1603 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1607 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1608 if (!filter->filter_string)
1611 err = process_preds(file->event_call, filter_string, filter, pe);
1613 filter_disable(file);
1614 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1615 append_filter_err(tr, pe, filter);
1617 event_set_filtered_flag(file);
1620 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1624 list_add_tail(&filter_item->list, &filter_list);
1626 * Regardless of if this returned an error, we still
1627 * replace the filter for the call.
1629 filter_item->filter = event_filter(file);
1630 event_set_filter(file, filter);
1640 * The calls can still be using the old filters.
1641 * Do a synchronize_rcu() and to ensure all calls are
1642 * done with them before we free them.
1644 tracepoint_synchronize_unregister();
1645 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1646 __free_filter(filter_item->filter);
1647 list_del(&filter_item->list);
1652 /* No call succeeded */
1653 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1654 list_del(&filter_item->list);
1657 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1661 /* If any call succeeded, we still need to sync */
1663 tracepoint_synchronize_unregister();
1664 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1665 __free_filter(filter_item->filter);
1666 list_del(&filter_item->list);
1672 static int create_filter_start(char *filter_string, bool set_str,
1673 struct filter_parse_error **pse,
1674 struct event_filter **filterp)
1676 struct event_filter *filter;
1677 struct filter_parse_error *pe = NULL;
1680 if (WARN_ON_ONCE(*pse || *filterp))
1683 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1684 if (filter && set_str) {
1685 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1686 if (!filter->filter_string)
1690 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1692 if (!filter || !pe || err) {
1694 __free_filter(filter);
1698 /* we're committed to creating a new filter */
1705 static void create_filter_finish(struct filter_parse_error *pe)
1711 * create_filter - create a filter for a trace_event_call
1712 * @call: trace_event_call to create a filter for
1713 * @filter_str: filter string
1714 * @set_str: remember @filter_str and enable detailed error in filter
1715 * @filterp: out param for created filter (always updated on return)
1716 * Must be a pointer that references a NULL pointer.
1718 * Creates a filter for @call with @filter_str. If @set_str is %true,
1719 * @filter_str is copied and recorded in the new filter.
1721 * On success, returns 0 and *@filterp points to the new filter. On
1722 * failure, returns -errno and *@filterp may point to %NULL or to a new
1723 * filter. In the latter case, the returned filter contains error
1724 * information if @set_str is %true and the caller is responsible for
1727 static int create_filter(struct trace_array *tr,
1728 struct trace_event_call *call,
1729 char *filter_string, bool set_str,
1730 struct event_filter **filterp)
1732 struct filter_parse_error *pe = NULL;
1735 /* filterp must point to NULL */
1736 if (WARN_ON(*filterp))
1739 err = create_filter_start(filter_string, set_str, &pe, filterp);
1743 err = process_preds(call, filter_string, *filterp, pe);
1745 append_filter_err(tr, pe, *filterp);
1746 create_filter_finish(pe);
1751 int create_event_filter(struct trace_array *tr,
1752 struct trace_event_call *call,
1753 char *filter_str, bool set_str,
1754 struct event_filter **filterp)
1756 return create_filter(tr, call, filter_str, set_str, filterp);
1760 * create_system_filter - create a filter for an event_subsystem
1761 * @system: event_subsystem to create a filter for
1762 * @filter_str: filter string
1763 * @filterp: out param for created filter (always updated on return)
1765 * Identical to create_filter() except that it creates a subsystem filter
1766 * and always remembers @filter_str.
1768 static int create_system_filter(struct trace_subsystem_dir *dir,
1769 struct trace_array *tr,
1770 char *filter_str, struct event_filter **filterp)
1772 struct filter_parse_error *pe = NULL;
1775 err = create_filter_start(filter_str, true, &pe, filterp);
1777 err = process_system_preds(dir, tr, pe, filter_str);
1779 /* System filters just show a default message */
1780 kfree((*filterp)->filter_string);
1781 (*filterp)->filter_string = NULL;
1783 append_filter_err(tr, pe, *filterp);
1786 create_filter_finish(pe);
1791 /* caller must hold event_mutex */
1792 int apply_event_filter(struct trace_event_file *file, char *filter_string)
1794 struct trace_event_call *call = file->event_call;
1795 struct event_filter *filter = NULL;
1798 if (!strcmp(strstrip(filter_string), "0")) {
1799 filter_disable(file);
1800 filter = event_filter(file);
1805 event_clear_filter(file);
1807 /* Make sure the filter is not being used */
1808 tracepoint_synchronize_unregister();
1809 __free_filter(filter);
1814 err = create_filter(file->tr, call, filter_string, true, &filter);
1817 * Always swap the call filter with the new filter
1818 * even if there was an error. If there was an error
1819 * in the filter, we disable the filter and show the error
1823 struct event_filter *tmp;
1825 tmp = event_filter(file);
1827 event_set_filtered_flag(file);
1829 filter_disable(file);
1831 event_set_filter(file, filter);
1834 /* Make sure the call is done with the filter */
1835 tracepoint_synchronize_unregister();
1843 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1844 char *filter_string)
1846 struct event_subsystem *system = dir->subsystem;
1847 struct trace_array *tr = dir->tr;
1848 struct event_filter *filter = NULL;
1851 mutex_lock(&event_mutex);
1853 /* Make sure the system still has events */
1854 if (!dir->nr_events) {
1859 if (!strcmp(strstrip(filter_string), "0")) {
1860 filter_free_subsystem_preds(dir, tr);
1861 remove_filter_string(system->filter);
1862 filter = system->filter;
1863 system->filter = NULL;
1864 /* Ensure all filters are no longer used */
1865 tracepoint_synchronize_unregister();
1866 filter_free_subsystem_filters(dir, tr);
1867 __free_filter(filter);
1871 err = create_system_filter(dir, tr, filter_string, &filter);
1874 * No event actually uses the system filter
1875 * we can free it without synchronize_rcu().
1877 __free_filter(system->filter);
1878 system->filter = filter;
1881 mutex_unlock(&event_mutex);
1886 #ifdef CONFIG_PERF_EVENTS
1888 void ftrace_profile_free_filter(struct perf_event *event)
1890 struct event_filter *filter = event->filter;
1892 event->filter = NULL;
1893 __free_filter(filter);
1896 struct function_filter_data {
1897 struct ftrace_ops *ops;
1902 #ifdef CONFIG_FUNCTION_TRACER
1904 ftrace_function_filter_re(char *buf, int len, int *count)
1908 str = kstrndup(buf, len, GFP_KERNEL);
1913 * The argv_split function takes white space
1914 * as a separator, so convert ',' into spaces.
1916 strreplace(str, ',', ' ');
1918 re = argv_split(GFP_KERNEL, str, count);
1923 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
1924 int reset, char *re, int len)
1929 ret = ftrace_set_filter(ops, re, len, reset);
1931 ret = ftrace_set_notrace(ops, re, len, reset);
1936 static int __ftrace_function_set_filter(int filter, char *buf, int len,
1937 struct function_filter_data *data)
1939 int i, re_cnt, ret = -EINVAL;
1943 reset = filter ? &data->first_filter : &data->first_notrace;
1946 * The 'ip' field could have multiple filters set, separated
1947 * either by space or comma. We first cut the filter and apply
1948 * all pieces separatelly.
1950 re = ftrace_function_filter_re(buf, len, &re_cnt);
1954 for (i = 0; i < re_cnt; i++) {
1955 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
1956 re[i], strlen(re[i]));
1968 static int ftrace_function_check_pred(struct filter_pred *pred)
1970 struct ftrace_event_field *field = pred->field;
1973 * Check the predicate for function trace, verify:
1974 * - only '==' and '!=' is used
1975 * - the 'ip' field is used
1977 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
1980 if (strcmp(field->name, "ip"))
1986 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
1987 struct function_filter_data *data)
1991 /* Checking the node is valid for function trace. */
1992 ret = ftrace_function_check_pred(pred);
1996 return __ftrace_function_set_filter(pred->op == OP_EQ,
1997 pred->regex.pattern,
2002 static bool is_or(struct prog_entry *prog, int i)
2007 * Only "||" is allowed for function events, thus,
2008 * all true branches should jump to true, and any
2009 * false branch should jump to false.
2011 target = prog[i].target + 1;
2012 /* True and false have NULL preds (all prog entries should jump to one */
2013 if (prog[target].pred)
2016 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2017 return prog[i].when_to_branch == prog[target].target;
2020 static int ftrace_function_set_filter(struct perf_event *event,
2021 struct event_filter *filter)
2023 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2024 lockdep_is_held(&event_mutex));
2025 struct function_filter_data data = {
2028 .ops = &event->ftrace_ops,
2032 for (i = 0; prog[i].pred; i++) {
2033 struct filter_pred *pred = prog[i].pred;
2035 if (!is_or(prog, i))
2038 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2044 static int ftrace_function_set_filter(struct perf_event *event,
2045 struct event_filter *filter)
2049 #endif /* CONFIG_FUNCTION_TRACER */
2051 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2055 struct event_filter *filter = NULL;
2056 struct trace_event_call *call;
2058 mutex_lock(&event_mutex);
2060 call = event->tp_event;
2070 err = create_filter(NULL, call, filter_str, false, &filter);
2074 if (ftrace_event_is_function(call))
2075 err = ftrace_function_set_filter(event, filter);
2077 event->filter = filter;
2080 if (err || ftrace_event_is_function(call))
2081 __free_filter(filter);
2084 mutex_unlock(&event_mutex);
2089 #endif /* CONFIG_PERF_EVENTS */
2091 #ifdef CONFIG_FTRACE_STARTUP_TEST
2093 #include <linux/types.h>
2094 #include <linux/tracepoint.h>
2096 #define CREATE_TRACE_POINTS
2097 #include "trace_events_filter_test.h"
2099 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2102 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2103 .e = ve, .f = vf, .g = vg, .h = vh }, \
2105 .not_visited = nvisit, \
2110 static struct test_filter_data_t {
2112 struct trace_event_raw_ftrace_test_filter rec;
2115 } test_filter_data[] = {
2116 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2117 "e == 1 && f == 1 && g == 1 && h == 1"
2118 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2119 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2120 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2122 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2123 "e == 1 || f == 1 || g == 1 || h == 1"
2124 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2125 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2126 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2128 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2129 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2130 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2131 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2132 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2133 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2135 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2136 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2137 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2138 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2139 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2141 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2142 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2143 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2144 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2145 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2147 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2148 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2149 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2150 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2151 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2153 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2154 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2155 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2156 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2157 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2159 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2160 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2161 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2162 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2163 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2171 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2173 static int test_pred_visited;
2175 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2177 struct ftrace_event_field *field = pred->field;
2179 test_pred_visited = 1;
2180 printk(KERN_INFO "\npred visited %s\n", field->name);
2184 static void update_pred_fn(struct event_filter *filter, char *fields)
2186 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2187 lockdep_is_held(&event_mutex));
2190 for (i = 0; prog[i].pred; i++) {
2191 struct filter_pred *pred = prog[i].pred;
2192 struct ftrace_event_field *field = pred->field;
2194 WARN_ON_ONCE(!pred->fn);
2197 WARN_ONCE(1, "all leafs should have field defined %d", i);
2201 if (!strchr(fields, *field->name))
2204 pred->fn = test_pred_visited_fn;
2208 static __init int ftrace_test_event_filter(void)
2212 printk(KERN_INFO "Testing ftrace filter: ");
2214 for (i = 0; i < DATA_CNT; i++) {
2215 struct event_filter *filter = NULL;
2216 struct test_filter_data_t *d = &test_filter_data[i];
2219 err = create_filter(NULL, &event_ftrace_test_filter,
2220 d->filter, false, &filter);
2223 "Failed to get filter for '%s', err %d\n",
2225 __free_filter(filter);
2229 /* Needed to dereference filter->prog */
2230 mutex_lock(&event_mutex);
2232 * The preemption disabling is not really needed for self
2233 * tests, but the rcu dereference will complain without it.
2236 if (*d->not_visited)
2237 update_pred_fn(filter, d->not_visited);
2239 test_pred_visited = 0;
2240 err = filter_match_preds(filter, &d->rec);
2243 mutex_unlock(&event_mutex);
2245 __free_filter(filter);
2247 if (test_pred_visited) {
2249 "Failed, unwanted pred visited for filter %s\n",
2254 if (err != d->match) {
2256 "Failed to match filter '%s', expected %d\n",
2257 d->filter, d->match);
2263 printk(KERN_CONT "OK\n");
2268 late_initcall(ftrace_test_event_filter);
2270 #endif /* CONFIG_FTRACE_STARTUP_TEST */