1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_filter - generic event filtering
8 #include <linux/uaccess.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/perf_event.h>
13 #include <linux/slab.h>
16 #include "trace_output.h"
18 #define DEFAULT_SYS_FILTER_MESSAGE \
19 "### global filter ###\n" \
20 "# Use this to set filters for multiple events.\n" \
21 "# Only events with the given fields will be affected.\n" \
22 "# If no events are modified, an error message will be displayed here"
24 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
39 enum filter_op_ids { OPS };
44 static const char * ops[] = { OPS };
47 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
48 * pred_funcs_##type below must match the order of them above.
50 #define PRED_FUNC_START OP_LE
51 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
54 C(NONE, "No error"), \
55 C(INVALID_OP, "Invalid operator"), \
56 C(TOO_MANY_OPEN, "Too many '('"), \
57 C(TOO_MANY_CLOSE, "Too few '('"), \
58 C(MISSING_QUOTE, "Missing matching quote"), \
59 C(OPERAND_TOO_LONG, "Operand too long"), \
60 C(EXPECT_STRING, "Expecting string field"), \
61 C(EXPECT_DIGIT, "Expecting numeric field"), \
62 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
63 C(FIELD_NOT_FOUND, "Field not found"), \
64 C(ILLEGAL_INTVAL, "Illegal integer value"), \
65 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
66 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
67 C(INVALID_FILTER, "Meaningless filter expression"), \
68 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
69 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
71 C(NO_FILTER, "No filter found")
74 #define C(a, b) FILT_ERR_##a
81 static const char *err_text[] = { ERRORS };
83 /* Called after a '!' character but "!=" and "!~" are not "not"s */
84 static bool is_not(const char *str)
95 * prog_entry - a singe entry in the filter program
96 * @target: Index to jump to on a branch (actually one minus the index)
97 * @when_to_branch: The value of the result of the predicate to do a branch
98 * @pred: The predicate to execute.
103 struct filter_pred *pred;
107 * update_preds- assign a program entry a label target
108 * @prog: The program array
109 * @N: The index of the current entry in @prog
110 * @when_to_branch: What to assign a program entry for its branch condition
112 * The program entry at @N has a target that points to the index of a program
113 * entry that can have its target and when_to_branch fields updated.
114 * Update the current program entry denoted by index @N target field to be
115 * that of the updated entry. This will denote the entry to update if
116 * we are processing an "||" after an "&&"
118 static void update_preds(struct prog_entry *prog, int N, int invert)
124 prog[t].when_to_branch = invert;
129 struct filter_parse_error {
134 static void parse_error(struct filter_parse_error *pe, int err, int pos)
137 pe->lasterr_pos = pos;
140 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
141 struct filter_parse_error *pe,
142 struct filter_pred **pred);
151 * Without going into a formal proof, this explains the method that is used in
152 * parsing the logical expressions.
154 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
155 * The first pass will convert it into the following program:
157 * n1: r=a; l1: if (!r) goto l4;
158 * n2: r=b; l2: if (!r) goto l4;
159 * n3: r=c; r=!r; l3: if (r) goto l4;
160 * n4: r=g; r=!r; l4: if (r) goto l5;
161 * n5: r=d; l5: if (r) goto T
162 * n6: r=e; l6: if (!r) goto l7;
163 * n7: r=f; r=!r; l7: if (!r) goto F
167 * To do this, we use a data structure to represent each of the above
168 * predicate and conditions that has:
170 * predicate, when_to_branch, invert, target
172 * The "predicate" will hold the function to determine the result "r".
173 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
174 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
175 * The "invert" holds whether the value should be reversed before testing.
176 * The "target" contains the label "l#" to jump to.
178 * A stack is created to hold values when parentheses are used.
180 * To simplify the logic, the labels will start at 0 and not 1.
182 * The possible invert values are 1 and 0. The number of "!"s that are in scope
183 * before the predicate determines the invert value, if the number is odd then
184 * the invert value is 1 and 0 otherwise. This means the invert value only
185 * needs to be toggled when a new "!" is introduced compared to what is stored
186 * on the stack, where parentheses were used.
188 * The top of the stack and "invert" are initialized to zero.
192 * #1 A loop through all the tokens is done:
194 * #2 If the token is an "(", the stack is push, and the current stack value
195 * gets the current invert value, and the loop continues to the next token.
196 * The top of the stack saves the "invert" value to keep track of what
197 * the current inversion is. As "!(a && !b || c)" would require all
198 * predicates being affected separately by the "!" before the parentheses.
199 * And that would end up being equivalent to "(!a || b) && !c"
201 * #3 If the token is an "!", the current "invert" value gets inverted, and
202 * the loop continues. Note, if the next token is a predicate, then
203 * this "invert" value is only valid for the current program entry,
204 * and does not affect other predicates later on.
206 * The only other acceptable token is the predicate string.
208 * #4 A new entry into the program is added saving: the predicate and the
209 * current value of "invert". The target is currently assigned to the
210 * previous program index (this will not be its final value).
212 * #5 We now enter another loop and look at the next token. The only valid
213 * tokens are ")", "&&", "||" or end of the input string "\0".
215 * #6 The invert variable is reset to the current value saved on the top of
218 * #7 The top of the stack holds not only the current invert value, but also
219 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
220 * precedence than "||". That is "a && b || c && d" is equivalent to
221 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
222 * to be processed. This is the case if an "&&" was the last token. If it was
223 * then we call update_preds(). This takes the program, the current index in
224 * the program, and the current value of "invert". More will be described
225 * below about this function.
227 * #8 If the next token is "&&" then we set a flag in the top of the stack
228 * that denotes that "&&" needs to be processed, break out of this loop
229 * and continue with the outer loop.
231 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
232 * This is called with the program, the current index in the program, but
233 * this time with an inverted value of "invert" (that is !invert). This is
234 * because the value taken will become the "when_to_branch" value of the
236 * Note, this is called when the next token is not an "&&". As stated before,
237 * "&&" takes higher precedence, and "||" should not be processed yet if the
238 * next logical operation is "&&".
240 * #10 If the next token is "||" then we set a flag in the top of the stack
241 * that denotes that "||" needs to be processed, break out of this loop
242 * and continue with the outer loop.
244 * #11 If this is the end of the input string "\0" then we break out of both
247 * #12 Otherwise, the next token is ")", where we pop the stack and continue
250 * Now to discuss the update_pred() function, as that is key to the setting up
251 * of the program. Remember the "target" of the program is initialized to the
252 * previous index and not the "l" label. The target holds the index into the
253 * program that gets affected by the operand. Thus if we have something like
254 * "a || b && c", when we process "a" the target will be "-1" (undefined).
255 * When we process "b", its target is "0", which is the index of "a", as that's
256 * the predicate that is affected by "||". But because the next token after "b"
257 * is "&&" we don't call update_preds(). Instead continue to "c". As the
258 * next token after "c" is not "&&" but the end of input, we first process the
259 * "&&" by calling update_preds() for the "&&" then we process the "||" by
260 * calling updates_preds() with the values for processing "||".
262 * What does that mean? What update_preds() does is to first save the "target"
263 * of the program entry indexed by the current program entry's "target"
264 * (remember the "target" is initialized to previous program entry), and then
265 * sets that "target" to the current index which represents the label "l#".
266 * That entry's "when_to_branch" is set to the value passed in (the "invert"
267 * or "!invert"). Then it sets the current program entry's target to the saved
268 * "target" value (the old value of the program that had its "target" updated
271 * Looking back at "a || b && c", we have the following steps:
272 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
273 * "||" - flag that we need to process "||"; continue outer loop
274 * "b" - prog[1] = { "b", X, 0 }
275 * "&&" - flag that we need to process "&&"; continue outer loop
276 * (Notice we did not process "||")
277 * "c" - prog[2] = { "c", X, 1 }
278 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
279 * t = prog[2].target; // t = 1
280 * s = prog[t].target; // s = 0
281 * prog[t].target = 2; // Set target to "l2"
282 * prog[t].when_to_branch = 0;
283 * prog[2].target = s;
284 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
285 * t = prog[2].target; // t = 0
286 * s = prog[t].target; // s = -1
287 * prog[t].target = 2; // Set target to "l2"
288 * prog[t].when_to_branch = 1;
289 * prog[2].target = s;
291 * #13 Which brings us to the final step of the first pass, which is to set
292 * the last program entry's when_to_branch and target, which will be
293 * when_to_branch = 0; target = N; ( the label after the program entry after
294 * the last program entry processed above).
296 * If we denote "TRUE" to be the entry after the last program entry processed,
297 * and "FALSE" the program entry after that, we are now done with the first
300 * Making the above "a || b && c" have a program of:
301 * prog[0] = { "a", 1, 2 }
302 * prog[1] = { "b", 0, 2 }
303 * prog[2] = { "c", 0, 3 }
305 * Which translates into:
306 * n0: r = a; l0: if (r) goto l2;
307 * n1: r = b; l1: if (!r) goto l2;
308 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
309 * T: return TRUE; l3:
312 * Although, after the first pass, the program is correct, it is
313 * inefficient. The simple sample of "a || b && c" could be easily been
315 * n0: r = a; if (r) goto T
316 * n1: r = b; if (!r) goto F
317 * n2: r = c; if (!r) goto F
321 * The First Pass is over the input string. The next too passes are over
322 * the program itself.
326 * Which brings us to the second pass. If a jump to a label has the
327 * same condition as that label, it can instead jump to its target.
328 * The original example of "a && !(!b || (c && g)) || d || e && !f"
329 * where the first pass gives us:
331 * n1: r=a; l1: if (!r) goto l4;
332 * n2: r=b; l2: if (!r) goto l4;
333 * n3: r=c; r=!r; l3: if (r) goto l4;
334 * n4: r=g; r=!r; l4: if (r) goto l5;
335 * n5: r=d; l5: if (r) goto T
336 * n6: r=e; l6: if (!r) goto l7;
337 * n7: r=f; r=!r; l7: if (!r) goto F:
341 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
342 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
343 * to go directly to T. To accomplish this, we start from the last
344 * entry in the program and work our way back. If the target of the entry
345 * has the same "when_to_branch" then we could use that entry's target.
346 * Doing this, the above would end up as:
348 * n1: r=a; l1: if (!r) goto l4;
349 * n2: r=b; l2: if (!r) goto l4;
350 * n3: r=c; r=!r; l3: if (r) goto T;
351 * n4: r=g; r=!r; l4: if (r) goto T;
352 * n5: r=d; l5: if (r) goto T;
353 * n6: r=e; l6: if (!r) goto F;
354 * n7: r=f; r=!r; l7: if (!r) goto F;
358 * In that same pass, if the "when_to_branch" doesn't match, we can simply
359 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
360 * where "l4: if (r) goto T;", then we can convert l2 to be:
361 * "l2: if (!r) goto n5;".
363 * This will have the second pass give us:
364 * n1: r=a; l1: if (!r) goto n5;
365 * n2: r=b; l2: if (!r) goto n5;
366 * n3: r=c; r=!r; l3: if (r) goto T;
367 * n4: r=g; r=!r; l4: if (r) goto T;
368 * n5: r=d; l5: if (r) goto T
369 * n6: r=e; l6: if (!r) goto F;
370 * n7: r=f; r=!r; l7: if (!r) goto F
374 * Notice, all the "l#" labels are no longer used, and they can now
379 * For the third pass we deal with the inverts. As they simply just
380 * make the "when_to_branch" get inverted, a simple loop over the
381 * program to that does: "when_to_branch ^= invert;" will do the
382 * job, leaving us with:
383 * n1: r=a; if (!r) goto n5;
384 * n2: r=b; if (!r) goto n5;
385 * n3: r=c: if (!r) goto T;
386 * n4: r=g; if (!r) goto T;
387 * n5: r=d; if (r) goto T
388 * n6: r=e; if (!r) goto F;
389 * n7: r=f; if (r) goto F
393 * As "r = a; if (!r) goto n5;" is obviously the same as
394 * "if (!a) goto n5;" without doing anything we can interpret the
396 * n1: if (!a) goto n5;
397 * n2: if (!b) goto n5;
398 * n3: if (!c) goto T;
399 * n4: if (!g) goto T;
401 * n6: if (!e) goto F;
406 * Since the inverts are discarded at the end, there's no reason to store
407 * them in the program array (and waste memory). A separate array to hold
408 * the inverts is used and freed at the end.
410 static struct prog_entry *
411 predicate_parse(const char *str, int nr_parens, int nr_preds,
412 parse_pred_fn parse_pred, void *data,
413 struct filter_parse_error *pe)
415 struct prog_entry *prog_stack;
416 struct prog_entry *prog;
417 const char *ptr = str;
418 char *inverts = NULL;
427 nr_preds += 2; /* For TRUE and FALSE */
429 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
431 return ERR_PTR(-ENOMEM);
432 prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
434 parse_error(pe, -ENOMEM, 0);
437 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
439 parse_error(pe, -ENOMEM, 0);
448 while (*ptr) { /* #1 */
449 const char *next = ptr++;
456 if (top - op_stack > nr_parens) {
470 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
474 inverts[N] = invert; /* #4 */
475 prog[N].target = N-1;
477 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
498 /* accepting only "&&" or "||" */
499 if (next[1] == next[0]) {
505 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
510 invert = *top & INVERT;
512 if (*top & PROCESS_AND) { /* #7 */
513 update_preds(prog, N - 1, invert);
514 *top &= ~PROCESS_AND;
516 if (*next == '&') { /* #8 */
520 if (*top & PROCESS_OR) { /* #9 */
521 update_preds(prog, N - 1, !invert);
524 if (*next == '|') { /* #10 */
528 if (!*next) /* #11 */
531 if (top == op_stack) {
534 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
541 if (top != op_stack) {
543 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
550 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
554 prog[N].pred = NULL; /* #13 */
555 prog[N].target = 1; /* TRUE */
556 prog[N+1].pred = NULL;
557 prog[N+1].target = 0; /* FALSE */
558 prog[N-1].target = N;
559 prog[N-1].when_to_branch = false;
562 for (i = N-1 ; i--; ) {
563 int target = prog[i].target;
564 if (prog[i].when_to_branch == prog[target].when_to_branch)
565 prog[i].target = prog[target].target;
569 for (i = 0; i < N; i++) {
570 invert = inverts[i] ^ prog[i].when_to_branch;
571 prog[i].when_to_branch = invert;
572 /* Make sure the program always moves forward */
573 if (WARN_ON(prog[i].target <= i)) {
586 for (i = 0; prog_stack[i].pred; i++)
587 kfree(prog_stack[i].pred);
593 #define DEFINE_COMPARISON_PRED(type) \
594 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
596 type *addr = (type *)(event + pred->offset); \
597 type val = (type)pred->val; \
598 return *addr < val; \
600 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
602 type *addr = (type *)(event + pred->offset); \
603 type val = (type)pred->val; \
604 return *addr <= val; \
606 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
608 type *addr = (type *)(event + pred->offset); \
609 type val = (type)pred->val; \
610 return *addr > val; \
612 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
614 type *addr = (type *)(event + pred->offset); \
615 type val = (type)pred->val; \
616 return *addr >= val; \
618 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
620 type *addr = (type *)(event + pred->offset); \
621 type val = (type)pred->val; \
622 return !!(*addr & val); \
624 static const filter_pred_fn_t pred_funcs_##type[] = { \
625 filter_pred_LE_##type, \
626 filter_pred_LT_##type, \
627 filter_pred_GE_##type, \
628 filter_pred_GT_##type, \
629 filter_pred_BAND_##type, \
632 #define DEFINE_EQUALITY_PRED(size) \
633 static int filter_pred_##size(struct filter_pred *pred, void *event) \
635 u##size *addr = (u##size *)(event + pred->offset); \
636 u##size val = (u##size)pred->val; \
639 match = (val == *addr) ^ pred->not; \
644 DEFINE_COMPARISON_PRED(s64);
645 DEFINE_COMPARISON_PRED(u64);
646 DEFINE_COMPARISON_PRED(s32);
647 DEFINE_COMPARISON_PRED(u32);
648 DEFINE_COMPARISON_PRED(s16);
649 DEFINE_COMPARISON_PRED(u16);
650 DEFINE_COMPARISON_PRED(s8);
651 DEFINE_COMPARISON_PRED(u8);
653 DEFINE_EQUALITY_PRED(64);
654 DEFINE_EQUALITY_PRED(32);
655 DEFINE_EQUALITY_PRED(16);
656 DEFINE_EQUALITY_PRED(8);
658 /* user space strings temp buffer */
659 #define USTRING_BUF_SIZE 1024
661 struct ustring_buffer {
662 char buffer[USTRING_BUF_SIZE];
665 static __percpu struct ustring_buffer *ustring_per_cpu;
667 static __always_inline char *test_string(char *str)
669 struct ustring_buffer *ubuf;
672 if (!ustring_per_cpu)
675 ubuf = this_cpu_ptr(ustring_per_cpu);
678 /* For safety, do not trust the string pointer */
679 if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
684 static __always_inline char *test_ustring(char *str)
686 struct ustring_buffer *ubuf;
690 if (!ustring_per_cpu)
693 ubuf = this_cpu_ptr(ustring_per_cpu);
696 /* user space address? */
697 ustr = (char __user *)str;
698 if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
704 /* Filter predicate for fixed sized arrays of characters */
705 static int filter_pred_string(struct filter_pred *pred, void *event)
707 char *addr = (char *)(event + pred->offset);
710 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
712 match = cmp ^ pred->not;
717 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
722 len = strlen(str) + 1; /* including tailing '\0' */
723 cmp = pred->regex.match(str, &pred->regex, len);
725 match = cmp ^ pred->not;
729 /* Filter predicate for char * pointers */
730 static int filter_pred_pchar(struct filter_pred *pred, void *event)
732 char **addr = (char **)(event + pred->offset);
735 str = test_string(*addr);
739 return filter_pchar(pred, str);
742 /* Filter predicate for char * pointers in user space*/
743 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
745 char **addr = (char **)(event + pred->offset);
748 str = test_ustring(*addr);
752 return filter_pchar(pred, str);
756 * Filter predicate for dynamic sized arrays of characters.
757 * These are implemented through a list of strings at the end
759 * Also each of these strings have a field in the entry which
760 * contains its offset from the beginning of the entry.
761 * We have then first to get this field, dereference it
762 * and add it to the address of the entry, and at last we have
763 * the address of the string.
765 static int filter_pred_strloc(struct filter_pred *pred, void *event)
767 u32 str_item = *(u32 *)(event + pred->offset);
768 int str_loc = str_item & 0xffff;
769 int str_len = str_item >> 16;
770 char *addr = (char *)(event + str_loc);
773 cmp = pred->regex.match(addr, &pred->regex, str_len);
775 match = cmp ^ pred->not;
781 * Filter predicate for relative dynamic sized arrays of characters.
782 * These are implemented through a list of strings at the end
783 * of the entry as same as dynamic string.
784 * The difference is that the relative one records the location offset
785 * from the field itself, not the event entry.
787 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
789 u32 *item = (u32 *)(event + pred->offset);
790 u32 str_item = *item;
791 int str_loc = str_item & 0xffff;
792 int str_len = str_item >> 16;
793 char *addr = (char *)(&item[1]) + str_loc;
796 cmp = pred->regex.match(addr, &pred->regex, str_len);
798 match = cmp ^ pred->not;
803 /* Filter predicate for CPUs. */
804 static int filter_pred_cpu(struct filter_pred *pred, void *event)
808 cpu = raw_smp_processor_id();
829 /* Filter predicate for COMM. */
830 static int filter_pred_comm(struct filter_pred *pred, void *event)
834 cmp = pred->regex.match(current->comm, &pred->regex,
836 return cmp ^ pred->not;
839 static int filter_pred_none(struct filter_pred *pred, void *event)
845 * regex_match_foo - Basic regex callbacks
847 * @str: the string to be searched
848 * @r: the regex structure containing the pattern string
849 * @len: the length of the string to be searched (including '\0')
852 * - @str might not be NULL-terminated if it's of type DYN_STRING
853 * RDYN_STRING, or STATIC_STRING, unless @len is zero.
856 static int regex_match_full(char *str, struct regex *r, int len)
858 /* len of zero means str is dynamic and ends with '\0' */
860 return strcmp(str, r->pattern) == 0;
862 return strncmp(str, r->pattern, len) == 0;
865 static int regex_match_front(char *str, struct regex *r, int len)
867 if (len && len < r->len)
870 return strncmp(str, r->pattern, r->len) == 0;
873 static int regex_match_middle(char *str, struct regex *r, int len)
876 return strstr(str, r->pattern) != NULL;
878 return strnstr(str, r->pattern, len) != NULL;
881 static int regex_match_end(char *str, struct regex *r, int len)
883 int strlen = len - 1;
885 if (strlen >= r->len &&
886 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
891 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
893 if (glob_match(r->pattern, str))
899 * filter_parse_regex - parse a basic regex
900 * @buff: the raw regex
901 * @len: length of the regex
902 * @search: will point to the beginning of the string to compare
903 * @not: tell whether the match will have to be inverted
905 * This passes in a buffer containing a regex and this function will
906 * set search to point to the search part of the buffer and
907 * return the type of search it is (see enum above).
908 * This does modify buff.
911 * search returns the pointer to use for comparison.
912 * not returns 1 if buff started with a '!'
915 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
917 int type = MATCH_FULL;
920 if (buff[0] == '!') {
929 if (isdigit(buff[0]))
932 for (i = 0; i < len; i++) {
933 if (buff[i] == '*') {
935 type = MATCH_END_ONLY;
936 } else if (i == len - 1) {
937 if (type == MATCH_END_ONLY)
938 type = MATCH_MIDDLE_ONLY;
940 type = MATCH_FRONT_ONLY;
943 } else { /* pattern continues, use full glob */
946 } else if (strchr("[?\\", buff[i])) {
956 static void filter_build_regex(struct filter_pred *pred)
958 struct regex *r = &pred->regex;
960 enum regex_type type = MATCH_FULL;
962 if (pred->op == OP_GLOB) {
963 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
964 r->len = strlen(search);
965 memmove(r->pattern, search, r->len+1);
969 /* MATCH_INDEX should not happen, but if it does, match full */
972 r->match = regex_match_full;
974 case MATCH_FRONT_ONLY:
975 r->match = regex_match_front;
977 case MATCH_MIDDLE_ONLY:
978 r->match = regex_match_middle;
981 r->match = regex_match_end;
984 r->match = regex_match_glob;
989 /* return 1 if event matches, 0 otherwise (discard) */
990 int filter_match_preds(struct event_filter *filter, void *rec)
992 struct prog_entry *prog;
995 /* no filter is considered a match */
999 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1000 prog = rcu_dereference_raw(filter->prog);
1004 for (i = 0; prog[i].pred; i++) {
1005 struct filter_pred *pred = prog[i].pred;
1006 int match = pred->fn(pred, rec);
1007 if (match == prog[i].when_to_branch)
1010 return prog[i].target;
1012 EXPORT_SYMBOL_GPL(filter_match_preds);
1014 static void remove_filter_string(struct event_filter *filter)
1019 kfree(filter->filter_string);
1020 filter->filter_string = NULL;
1023 static void append_filter_err(struct trace_array *tr,
1024 struct filter_parse_error *pe,
1025 struct event_filter *filter)
1027 struct trace_seq *s;
1028 int pos = pe->lasterr_pos;
1032 if (WARN_ON(!filter->filter_string))
1035 s = kmalloc(sizeof(*s), GFP_KERNEL);
1040 len = strlen(filter->filter_string);
1044 /* indexing is off by one */
1048 trace_seq_puts(s, filter->filter_string);
1049 if (pe->lasterr > 0) {
1050 trace_seq_printf(s, "\n%*s", pos, "^");
1051 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1052 tracing_log_err(tr, "event filter parse error",
1053 filter->filter_string, err_text,
1054 pe->lasterr, pe->lasterr_pos);
1056 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1057 tracing_log_err(tr, "event filter parse error",
1058 filter->filter_string, err_text,
1061 trace_seq_putc(s, 0);
1062 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1064 kfree(filter->filter_string);
1065 filter->filter_string = buf;
1070 static inline struct event_filter *event_filter(struct trace_event_file *file)
1072 return file->filter;
1075 /* caller must hold event_mutex */
1076 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1078 struct event_filter *filter = event_filter(file);
1080 if (filter && filter->filter_string)
1081 trace_seq_printf(s, "%s\n", filter->filter_string);
1083 trace_seq_puts(s, "none\n");
1086 void print_subsystem_event_filter(struct event_subsystem *system,
1087 struct trace_seq *s)
1089 struct event_filter *filter;
1091 mutex_lock(&event_mutex);
1092 filter = system->filter;
1093 if (filter && filter->filter_string)
1094 trace_seq_printf(s, "%s\n", filter->filter_string);
1096 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1097 mutex_unlock(&event_mutex);
1100 static void free_prog(struct event_filter *filter)
1102 struct prog_entry *prog;
1105 prog = rcu_access_pointer(filter->prog);
1109 for (i = 0; prog[i].pred; i++)
1110 kfree(prog[i].pred);
1114 static void filter_disable(struct trace_event_file *file)
1116 unsigned long old_flags = file->flags;
1118 file->flags &= ~EVENT_FILE_FL_FILTERED;
1120 if (old_flags != file->flags)
1121 trace_buffered_event_disable();
1124 static void __free_filter(struct event_filter *filter)
1130 kfree(filter->filter_string);
1134 void free_event_filter(struct event_filter *filter)
1136 __free_filter(filter);
1139 static inline void __remove_filter(struct trace_event_file *file)
1141 filter_disable(file);
1142 remove_filter_string(file->filter);
1145 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1146 struct trace_array *tr)
1148 struct trace_event_file *file;
1150 list_for_each_entry(file, &tr->events, list) {
1151 if (file->system != dir)
1153 __remove_filter(file);
1157 static inline void __free_subsystem_filter(struct trace_event_file *file)
1159 __free_filter(file->filter);
1160 file->filter = NULL;
1163 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1164 struct trace_array *tr)
1166 struct trace_event_file *file;
1168 list_for_each_entry(file, &tr->events, list) {
1169 if (file->system != dir)
1171 __free_subsystem_filter(file);
1175 int filter_assign_type(const char *type)
1177 if (strstr(type, "__data_loc") && strstr(type, "char"))
1178 return FILTER_DYN_STRING;
1180 if (strstr(type, "__rel_loc") && strstr(type, "char"))
1181 return FILTER_RDYN_STRING;
1183 if (strchr(type, '[') && strstr(type, "char"))
1184 return FILTER_STATIC_STRING;
1186 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1187 return FILTER_PTR_STRING;
1189 return FILTER_OTHER;
1192 static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
1193 int field_size, int field_is_signed)
1195 filter_pred_fn_t fn = NULL;
1196 int pred_func_index = -1;
1203 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1205 pred_func_index = op - PRED_FUNC_START;
1206 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1210 switch (field_size) {
1212 if (pred_func_index < 0)
1213 fn = filter_pred_64;
1214 else if (field_is_signed)
1215 fn = pred_funcs_s64[pred_func_index];
1217 fn = pred_funcs_u64[pred_func_index];
1220 if (pred_func_index < 0)
1221 fn = filter_pred_32;
1222 else if (field_is_signed)
1223 fn = pred_funcs_s32[pred_func_index];
1225 fn = pred_funcs_u32[pred_func_index];
1228 if (pred_func_index < 0)
1229 fn = filter_pred_16;
1230 else if (field_is_signed)
1231 fn = pred_funcs_s16[pred_func_index];
1233 fn = pred_funcs_u16[pred_func_index];
1236 if (pred_func_index < 0)
1238 else if (field_is_signed)
1239 fn = pred_funcs_s8[pred_func_index];
1241 fn = pred_funcs_u8[pred_func_index];
1248 /* Called when a predicate is encountered by predicate_parse() */
1249 static int parse_pred(const char *str, void *data,
1250 int pos, struct filter_parse_error *pe,
1251 struct filter_pred **pred_ptr)
1253 struct trace_event_call *call = data;
1254 struct ftrace_event_field *field;
1255 struct filter_pred *pred = NULL;
1256 char num_buf[24]; /* Big enough to hold an address */
1258 bool ustring = false;
1267 /* First find the field to associate to */
1268 while (isspace(str[i]))
1272 while (isalnum(str[i]) || str[i] == '_')
1280 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1284 /* Make sure that the field exists */
1286 field = trace_find_event_field(call, field_name);
1289 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1293 /* See if the field is a user space string */
1294 if ((len = str_has_prefix(str + i, ".ustring"))) {
1299 while (isspace(str[i]))
1302 /* Make sure this op is supported */
1303 for (op = 0; ops[op]; op++) {
1304 /* This is why '<=' must come before '<' in ops[] */
1305 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1310 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1314 i += strlen(ops[op]);
1316 while (isspace(str[i]))
1321 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1325 pred->field = field;
1326 pred->offset = field->offset;
1329 if (ftrace_event_is_function(call)) {
1331 * Perf does things different with function events.
1332 * It only allows an "ip" field, and expects a string.
1333 * But the string does not need to be surrounded by quotes.
1334 * If it is a string, the assigned function as a nop,
1335 * (perf doesn't use it) and grab everything.
1337 if (strcmp(field->name, "ip") != 0) {
1338 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1341 pred->fn = filter_pred_none;
1344 * Quotes are not required, but if they exist then we need
1345 * to read them till we hit a matching one.
1347 if (str[i] == '\'' || str[i] == '"')
1352 for (i++; str[i]; i++) {
1353 if (q && str[i] == q)
1355 if (!q && (str[i] == ')' || str[i] == '&' ||
1363 if (len >= MAX_FILTER_STR_VAL) {
1364 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1368 pred->regex.len = len;
1369 strncpy(pred->regex.pattern, str + s, len);
1370 pred->regex.pattern[len] = 0;
1372 /* This is either a string, or an integer */
1373 } else if (str[i] == '\'' || str[i] == '"') {
1376 /* Make sure the op is OK for strings */
1385 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1389 /* Make sure the field is OK for strings */
1390 if (!is_string_field(field)) {
1391 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1395 for (i++; str[i]; i++) {
1400 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1407 if (len >= MAX_FILTER_STR_VAL) {
1408 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1412 pred->regex.len = len;
1413 strncpy(pred->regex.pattern, str + s, len);
1414 pred->regex.pattern[len] = 0;
1416 filter_build_regex(pred);
1418 if (field->filter_type == FILTER_COMM) {
1419 pred->fn = filter_pred_comm;
1421 } else if (field->filter_type == FILTER_STATIC_STRING) {
1422 pred->fn = filter_pred_string;
1423 pred->regex.field_len = field->size;
1425 } else if (field->filter_type == FILTER_DYN_STRING) {
1426 pred->fn = filter_pred_strloc;
1427 } else if (field->filter_type == FILTER_RDYN_STRING)
1428 pred->fn = filter_pred_strrelloc;
1431 if (!ustring_per_cpu) {
1432 /* Once allocated, keep it around for good */
1433 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
1434 if (!ustring_per_cpu)
1439 pred->fn = filter_pred_pchar_user;
1441 pred->fn = filter_pred_pchar;
1443 /* go past the last quote */
1446 } else if (isdigit(str[i]) || str[i] == '-') {
1448 /* Make sure the field is not a string */
1449 if (is_string_field(field)) {
1450 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1454 if (op == OP_GLOB) {
1455 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1462 /* We allow 0xDEADBEEF */
1463 while (isalnum(str[i]))
1467 /* 0xfeedfacedeadbeef is 18 chars max */
1468 if (len >= sizeof(num_buf)) {
1469 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1473 strncpy(num_buf, str + s, len);
1476 /* Make sure it is a value */
1477 if (field->is_signed)
1478 ret = kstrtoll(num_buf, 0, &val);
1480 ret = kstrtoull(num_buf, 0, &val);
1482 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1488 if (field->filter_type == FILTER_CPU)
1489 pred->fn = filter_pred_cpu;
1491 pred->fn = select_comparison_fn(pred->op, field->size,
1493 if (pred->op == OP_NE)
1498 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1514 TOO_MANY_CLOSE = -1,
1520 * Read the filter string once to calculate the number of predicates
1521 * as well as how deep the parentheses go.
1524 * 0 - everything is fine (err is undefined)
1527 * -3 - No matching quote
1529 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1531 bool is_pred = false;
1533 int open = 1; /* Count the expression as "(E)" */
1541 for (i = 0; str[i]; i++) {
1542 if (isspace(str[i]))
1545 if (str[i] == quote)
1558 if (str[i+1] != str[i])
1565 if (open > max_open)
1572 return TOO_MANY_CLOSE;
1585 return MISSING_QUOTE;
1591 /* find the bad open */
1594 if (str[i] == quote)
1600 if (level == open) {
1602 return TOO_MANY_OPEN;
1615 /* First character is the '(' with missing ')' */
1617 return TOO_MANY_OPEN;
1620 /* Set the size of the required stacks */
1626 static int process_preds(struct trace_event_call *call,
1627 const char *filter_string,
1628 struct event_filter *filter,
1629 struct filter_parse_error *pe)
1631 struct prog_entry *prog;
1637 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1641 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1644 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1647 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
1655 prog = predicate_parse(filter_string, nr_parens, nr_preds,
1656 parse_pred, call, pe);
1658 return PTR_ERR(prog);
1660 rcu_assign_pointer(filter->prog, prog);
1664 static inline void event_set_filtered_flag(struct trace_event_file *file)
1666 unsigned long old_flags = file->flags;
1668 file->flags |= EVENT_FILE_FL_FILTERED;
1670 if (old_flags != file->flags)
1671 trace_buffered_event_enable();
1674 static inline void event_set_filter(struct trace_event_file *file,
1675 struct event_filter *filter)
1677 rcu_assign_pointer(file->filter, filter);
1680 static inline void event_clear_filter(struct trace_event_file *file)
1682 RCU_INIT_POINTER(file->filter, NULL);
1685 struct filter_list {
1686 struct list_head list;
1687 struct event_filter *filter;
1690 static int process_system_preds(struct trace_subsystem_dir *dir,
1691 struct trace_array *tr,
1692 struct filter_parse_error *pe,
1693 char *filter_string)
1695 struct trace_event_file *file;
1696 struct filter_list *filter_item;
1697 struct event_filter *filter = NULL;
1698 struct filter_list *tmp;
1699 LIST_HEAD(filter_list);
1703 list_for_each_entry(file, &tr->events, list) {
1705 if (file->system != dir)
1708 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1712 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1713 if (!filter->filter_string)
1716 err = process_preds(file->event_call, filter_string, filter, pe);
1718 filter_disable(file);
1719 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1720 append_filter_err(tr, pe, filter);
1722 event_set_filtered_flag(file);
1725 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1729 list_add_tail(&filter_item->list, &filter_list);
1731 * Regardless of if this returned an error, we still
1732 * replace the filter for the call.
1734 filter_item->filter = event_filter(file);
1735 event_set_filter(file, filter);
1745 * The calls can still be using the old filters.
1746 * Do a synchronize_rcu() and to ensure all calls are
1747 * done with them before we free them.
1749 tracepoint_synchronize_unregister();
1750 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1751 __free_filter(filter_item->filter);
1752 list_del(&filter_item->list);
1757 /* No call succeeded */
1758 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1759 list_del(&filter_item->list);
1762 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1765 __free_filter(filter);
1766 /* If any call succeeded, we still need to sync */
1768 tracepoint_synchronize_unregister();
1769 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1770 __free_filter(filter_item->filter);
1771 list_del(&filter_item->list);
1777 static int create_filter_start(char *filter_string, bool set_str,
1778 struct filter_parse_error **pse,
1779 struct event_filter **filterp)
1781 struct event_filter *filter;
1782 struct filter_parse_error *pe = NULL;
1785 if (WARN_ON_ONCE(*pse || *filterp))
1788 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1789 if (filter && set_str) {
1790 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1791 if (!filter->filter_string)
1795 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1797 if (!filter || !pe || err) {
1799 __free_filter(filter);
1803 /* we're committed to creating a new filter */
1810 static void create_filter_finish(struct filter_parse_error *pe)
1816 * create_filter - create a filter for a trace_event_call
1817 * @tr: the trace array associated with these events
1818 * @call: trace_event_call to create a filter for
1819 * @filter_str: filter string
1820 * @set_str: remember @filter_str and enable detailed error in filter
1821 * @filterp: out param for created filter (always updated on return)
1822 * Must be a pointer that references a NULL pointer.
1824 * Creates a filter for @call with @filter_str. If @set_str is %true,
1825 * @filter_str is copied and recorded in the new filter.
1827 * On success, returns 0 and *@filterp points to the new filter. On
1828 * failure, returns -errno and *@filterp may point to %NULL or to a new
1829 * filter. In the latter case, the returned filter contains error
1830 * information if @set_str is %true and the caller is responsible for
1833 static int create_filter(struct trace_array *tr,
1834 struct trace_event_call *call,
1835 char *filter_string, bool set_str,
1836 struct event_filter **filterp)
1838 struct filter_parse_error *pe = NULL;
1841 /* filterp must point to NULL */
1842 if (WARN_ON(*filterp))
1845 err = create_filter_start(filter_string, set_str, &pe, filterp);
1849 err = process_preds(call, filter_string, *filterp, pe);
1851 append_filter_err(tr, pe, *filterp);
1852 create_filter_finish(pe);
1857 int create_event_filter(struct trace_array *tr,
1858 struct trace_event_call *call,
1859 char *filter_str, bool set_str,
1860 struct event_filter **filterp)
1862 return create_filter(tr, call, filter_str, set_str, filterp);
1866 * create_system_filter - create a filter for an event subsystem
1867 * @dir: the descriptor for the subsystem directory
1868 * @filter_str: filter string
1869 * @filterp: out param for created filter (always updated on return)
1871 * Identical to create_filter() except that it creates a subsystem filter
1872 * and always remembers @filter_str.
1874 static int create_system_filter(struct trace_subsystem_dir *dir,
1875 char *filter_str, struct event_filter **filterp)
1877 struct filter_parse_error *pe = NULL;
1880 err = create_filter_start(filter_str, true, &pe, filterp);
1882 err = process_system_preds(dir, dir->tr, pe, filter_str);
1884 /* System filters just show a default message */
1885 kfree((*filterp)->filter_string);
1886 (*filterp)->filter_string = NULL;
1888 append_filter_err(dir->tr, pe, *filterp);
1891 create_filter_finish(pe);
1896 /* caller must hold event_mutex */
1897 int apply_event_filter(struct trace_event_file *file, char *filter_string)
1899 struct trace_event_call *call = file->event_call;
1900 struct event_filter *filter = NULL;
1903 if (!strcmp(strstrip(filter_string), "0")) {
1904 filter_disable(file);
1905 filter = event_filter(file);
1910 event_clear_filter(file);
1912 /* Make sure the filter is not being used */
1913 tracepoint_synchronize_unregister();
1914 __free_filter(filter);
1919 err = create_filter(file->tr, call, filter_string, true, &filter);
1922 * Always swap the call filter with the new filter
1923 * even if there was an error. If there was an error
1924 * in the filter, we disable the filter and show the error
1928 struct event_filter *tmp;
1930 tmp = event_filter(file);
1932 event_set_filtered_flag(file);
1934 filter_disable(file);
1936 event_set_filter(file, filter);
1939 /* Make sure the call is done with the filter */
1940 tracepoint_synchronize_unregister();
1948 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
1949 char *filter_string)
1951 struct event_subsystem *system = dir->subsystem;
1952 struct trace_array *tr = dir->tr;
1953 struct event_filter *filter = NULL;
1956 mutex_lock(&event_mutex);
1958 /* Make sure the system still has events */
1959 if (!dir->nr_events) {
1964 if (!strcmp(strstrip(filter_string), "0")) {
1965 filter_free_subsystem_preds(dir, tr);
1966 remove_filter_string(system->filter);
1967 filter = system->filter;
1968 system->filter = NULL;
1969 /* Ensure all filters are no longer used */
1970 tracepoint_synchronize_unregister();
1971 filter_free_subsystem_filters(dir, tr);
1972 __free_filter(filter);
1976 err = create_system_filter(dir, filter_string, &filter);
1979 * No event actually uses the system filter
1980 * we can free it without synchronize_rcu().
1982 __free_filter(system->filter);
1983 system->filter = filter;
1986 mutex_unlock(&event_mutex);
1991 #ifdef CONFIG_PERF_EVENTS
1993 void ftrace_profile_free_filter(struct perf_event *event)
1995 struct event_filter *filter = event->filter;
1997 event->filter = NULL;
1998 __free_filter(filter);
2001 struct function_filter_data {
2002 struct ftrace_ops *ops;
2007 #ifdef CONFIG_FUNCTION_TRACER
2009 ftrace_function_filter_re(char *buf, int len, int *count)
2013 str = kstrndup(buf, len, GFP_KERNEL);
2018 * The argv_split function takes white space
2019 * as a separator, so convert ',' into spaces.
2021 strreplace(str, ',', ' ');
2023 re = argv_split(GFP_KERNEL, str, count);
2028 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2029 int reset, char *re, int len)
2034 ret = ftrace_set_filter(ops, re, len, reset);
2036 ret = ftrace_set_notrace(ops, re, len, reset);
2041 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2042 struct function_filter_data *data)
2044 int i, re_cnt, ret = -EINVAL;
2048 reset = filter ? &data->first_filter : &data->first_notrace;
2051 * The 'ip' field could have multiple filters set, separated
2052 * either by space or comma. We first cut the filter and apply
2053 * all pieces separately.
2055 re = ftrace_function_filter_re(buf, len, &re_cnt);
2059 for (i = 0; i < re_cnt; i++) {
2060 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2061 re[i], strlen(re[i]));
2073 static int ftrace_function_check_pred(struct filter_pred *pred)
2075 struct ftrace_event_field *field = pred->field;
2078 * Check the predicate for function trace, verify:
2079 * - only '==' and '!=' is used
2080 * - the 'ip' field is used
2082 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2085 if (strcmp(field->name, "ip"))
2091 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2092 struct function_filter_data *data)
2096 /* Checking the node is valid for function trace. */
2097 ret = ftrace_function_check_pred(pred);
2101 return __ftrace_function_set_filter(pred->op == OP_EQ,
2102 pred->regex.pattern,
2107 static bool is_or(struct prog_entry *prog, int i)
2112 * Only "||" is allowed for function events, thus,
2113 * all true branches should jump to true, and any
2114 * false branch should jump to false.
2116 target = prog[i].target + 1;
2117 /* True and false have NULL preds (all prog entries should jump to one */
2118 if (prog[target].pred)
2121 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2122 return prog[i].when_to_branch == prog[target].target;
2125 static int ftrace_function_set_filter(struct perf_event *event,
2126 struct event_filter *filter)
2128 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2129 lockdep_is_held(&event_mutex));
2130 struct function_filter_data data = {
2133 .ops = &event->ftrace_ops,
2137 for (i = 0; prog[i].pred; i++) {
2138 struct filter_pred *pred = prog[i].pred;
2140 if (!is_or(prog, i))
2143 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2149 static int ftrace_function_set_filter(struct perf_event *event,
2150 struct event_filter *filter)
2154 #endif /* CONFIG_FUNCTION_TRACER */
2156 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2160 struct event_filter *filter = NULL;
2161 struct trace_event_call *call;
2163 mutex_lock(&event_mutex);
2165 call = event->tp_event;
2175 err = create_filter(NULL, call, filter_str, false, &filter);
2179 if (ftrace_event_is_function(call))
2180 err = ftrace_function_set_filter(event, filter);
2182 event->filter = filter;
2185 if (err || ftrace_event_is_function(call))
2186 __free_filter(filter);
2189 mutex_unlock(&event_mutex);
2194 #endif /* CONFIG_PERF_EVENTS */
2196 #ifdef CONFIG_FTRACE_STARTUP_TEST
2198 #include <linux/types.h>
2199 #include <linux/tracepoint.h>
2201 #define CREATE_TRACE_POINTS
2202 #include "trace_events_filter_test.h"
2204 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2207 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2208 .e = ve, .f = vf, .g = vg, .h = vh }, \
2210 .not_visited = nvisit, \
2215 static struct test_filter_data_t {
2217 struct trace_event_raw_ftrace_test_filter rec;
2220 } test_filter_data[] = {
2221 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2222 "e == 1 && f == 1 && g == 1 && h == 1"
2223 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2224 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2225 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2227 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2228 "e == 1 || f == 1 || g == 1 || h == 1"
2229 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2230 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2231 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2233 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2234 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2235 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2236 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2237 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2238 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2240 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2241 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2242 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2243 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2244 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2246 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2247 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2248 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2249 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2250 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2252 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2253 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2254 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2255 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2256 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2258 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2259 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2260 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2261 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2262 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2264 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2265 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2266 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2267 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2268 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2276 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2278 static int test_pred_visited;
2280 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2282 struct ftrace_event_field *field = pred->field;
2284 test_pred_visited = 1;
2285 printk(KERN_INFO "\npred visited %s\n", field->name);
2289 static void update_pred_fn(struct event_filter *filter, char *fields)
2291 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2292 lockdep_is_held(&event_mutex));
2295 for (i = 0; prog[i].pred; i++) {
2296 struct filter_pred *pred = prog[i].pred;
2297 struct ftrace_event_field *field = pred->field;
2299 WARN_ON_ONCE(!pred->fn);
2302 WARN_ONCE(1, "all leafs should have field defined %d", i);
2306 if (!strchr(fields, *field->name))
2309 pred->fn = test_pred_visited_fn;
2313 static __init int ftrace_test_event_filter(void)
2317 printk(KERN_INFO "Testing ftrace filter: ");
2319 for (i = 0; i < DATA_CNT; i++) {
2320 struct event_filter *filter = NULL;
2321 struct test_filter_data_t *d = &test_filter_data[i];
2324 err = create_filter(NULL, &event_ftrace_test_filter,
2325 d->filter, false, &filter);
2328 "Failed to get filter for '%s', err %d\n",
2330 __free_filter(filter);
2334 /* Needed to dereference filter->prog */
2335 mutex_lock(&event_mutex);
2337 * The preemption disabling is not really needed for self
2338 * tests, but the rcu dereference will complain without it.
2341 if (*d->not_visited)
2342 update_pred_fn(filter, d->not_visited);
2344 test_pred_visited = 0;
2345 err = filter_match_preds(filter, &d->rec);
2348 mutex_unlock(&event_mutex);
2350 __free_filter(filter);
2352 if (test_pred_visited) {
2354 "Failed, unwanted pred visited for filter %s\n",
2359 if (err != d->match) {
2361 "Failed to match filter '%s', expected %d\n",
2362 d->filter, d->match);
2368 printk(KERN_CONT "OK\n");
2373 late_initcall(ftrace_test_event_filter);
2375 #endif /* CONFIG_FTRACE_STARTUP_TEST */