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/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 };
61 FILTER_PRED_FN_STRING,
62 FILTER_PRED_FN_STRLOC,
63 FILTER_PRED_FN_STRRELLOC,
64 FILTER_PRED_FN_PCHAR_USER,
68 FILTER_PRED_TEST_VISITED,
72 enum filter_pred_fn fn_num;
76 struct ftrace_event_field *field;
83 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
84 * pred_funcs_##type below must match the order of them above.
86 #define PRED_FUNC_START OP_LE
87 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
90 C(NONE, "No error"), \
91 C(INVALID_OP, "Invalid operator"), \
92 C(TOO_MANY_OPEN, "Too many '('"), \
93 C(TOO_MANY_CLOSE, "Too few '('"), \
94 C(MISSING_QUOTE, "Missing matching quote"), \
95 C(OPERAND_TOO_LONG, "Operand too long"), \
96 C(EXPECT_STRING, "Expecting string field"), \
97 C(EXPECT_DIGIT, "Expecting numeric field"), \
98 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
99 C(FIELD_NOT_FOUND, "Field not found"), \
100 C(ILLEGAL_INTVAL, "Illegal integer value"), \
101 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
102 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
103 C(INVALID_FILTER, "Meaningless filter expression"), \
104 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
105 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
107 C(NO_FILTER, "No filter found")
110 #define C(a, b) FILT_ERR_##a
117 static const char *err_text[] = { ERRORS };
119 /* Called after a '!' character but "!=" and "!~" are not "not"s */
120 static bool is_not(const char *str)
131 * prog_entry - a singe entry in the filter program
132 * @target: Index to jump to on a branch (actually one minus the index)
133 * @when_to_branch: The value of the result of the predicate to do a branch
134 * @pred: The predicate to execute.
139 struct filter_pred *pred;
143 * update_preds- assign a program entry a label target
144 * @prog: The program array
145 * @N: The index of the current entry in @prog
146 * @when_to_branch: What to assign a program entry for its branch condition
148 * The program entry at @N has a target that points to the index of a program
149 * entry that can have its target and when_to_branch fields updated.
150 * Update the current program entry denoted by index @N target field to be
151 * that of the updated entry. This will denote the entry to update if
152 * we are processing an "||" after an "&&"
154 static void update_preds(struct prog_entry *prog, int N, int invert)
160 prog[t].when_to_branch = invert;
165 struct filter_parse_error {
170 static void parse_error(struct filter_parse_error *pe, int err, int pos)
173 pe->lasterr_pos = pos;
176 typedef int (*parse_pred_fn)(const char *str, void *data, int pos,
177 struct filter_parse_error *pe,
178 struct filter_pred **pred);
187 * Without going into a formal proof, this explains the method that is used in
188 * parsing the logical expressions.
190 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
191 * The first pass will convert it into the following program:
193 * n1: r=a; l1: if (!r) goto l4;
194 * n2: r=b; l2: if (!r) goto l4;
195 * n3: r=c; r=!r; l3: if (r) goto l4;
196 * n4: r=g; r=!r; l4: if (r) goto l5;
197 * n5: r=d; l5: if (r) goto T
198 * n6: r=e; l6: if (!r) goto l7;
199 * n7: r=f; r=!r; l7: if (!r) goto F
203 * To do this, we use a data structure to represent each of the above
204 * predicate and conditions that has:
206 * predicate, when_to_branch, invert, target
208 * The "predicate" will hold the function to determine the result "r".
209 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
210 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
211 * The "invert" holds whether the value should be reversed before testing.
212 * The "target" contains the label "l#" to jump to.
214 * A stack is created to hold values when parentheses are used.
216 * To simplify the logic, the labels will start at 0 and not 1.
218 * The possible invert values are 1 and 0. The number of "!"s that are in scope
219 * before the predicate determines the invert value, if the number is odd then
220 * the invert value is 1 and 0 otherwise. This means the invert value only
221 * needs to be toggled when a new "!" is introduced compared to what is stored
222 * on the stack, where parentheses were used.
224 * The top of the stack and "invert" are initialized to zero.
228 * #1 A loop through all the tokens is done:
230 * #2 If the token is an "(", the stack is push, and the current stack value
231 * gets the current invert value, and the loop continues to the next token.
232 * The top of the stack saves the "invert" value to keep track of what
233 * the current inversion is. As "!(a && !b || c)" would require all
234 * predicates being affected separately by the "!" before the parentheses.
235 * And that would end up being equivalent to "(!a || b) && !c"
237 * #3 If the token is an "!", the current "invert" value gets inverted, and
238 * the loop continues. Note, if the next token is a predicate, then
239 * this "invert" value is only valid for the current program entry,
240 * and does not affect other predicates later on.
242 * The only other acceptable token is the predicate string.
244 * #4 A new entry into the program is added saving: the predicate and the
245 * current value of "invert". The target is currently assigned to the
246 * previous program index (this will not be its final value).
248 * #5 We now enter another loop and look at the next token. The only valid
249 * tokens are ")", "&&", "||" or end of the input string "\0".
251 * #6 The invert variable is reset to the current value saved on the top of
254 * #7 The top of the stack holds not only the current invert value, but also
255 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
256 * precedence than "||". That is "a && b || c && d" is equivalent to
257 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
258 * to be processed. This is the case if an "&&" was the last token. If it was
259 * then we call update_preds(). This takes the program, the current index in
260 * the program, and the current value of "invert". More will be described
261 * below about this function.
263 * #8 If the next token is "&&" then we set a flag in the top of the stack
264 * that denotes that "&&" needs to be processed, break out of this loop
265 * and continue with the outer loop.
267 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
268 * This is called with the program, the current index in the program, but
269 * this time with an inverted value of "invert" (that is !invert). This is
270 * because the value taken will become the "when_to_branch" value of the
272 * Note, this is called when the next token is not an "&&". As stated before,
273 * "&&" takes higher precedence, and "||" should not be processed yet if the
274 * next logical operation is "&&".
276 * #10 If the next token is "||" then we set a flag in the top of the stack
277 * that denotes that "||" needs to be processed, break out of this loop
278 * and continue with the outer loop.
280 * #11 If this is the end of the input string "\0" then we break out of both
283 * #12 Otherwise, the next token is ")", where we pop the stack and continue
286 * Now to discuss the update_pred() function, as that is key to the setting up
287 * of the program. Remember the "target" of the program is initialized to the
288 * previous index and not the "l" label. The target holds the index into the
289 * program that gets affected by the operand. Thus if we have something like
290 * "a || b && c", when we process "a" the target will be "-1" (undefined).
291 * When we process "b", its target is "0", which is the index of "a", as that's
292 * the predicate that is affected by "||". But because the next token after "b"
293 * is "&&" we don't call update_preds(). Instead continue to "c". As the
294 * next token after "c" is not "&&" but the end of input, we first process the
295 * "&&" by calling update_preds() for the "&&" then we process the "||" by
296 * calling updates_preds() with the values for processing "||".
298 * What does that mean? What update_preds() does is to first save the "target"
299 * of the program entry indexed by the current program entry's "target"
300 * (remember the "target" is initialized to previous program entry), and then
301 * sets that "target" to the current index which represents the label "l#".
302 * That entry's "when_to_branch" is set to the value passed in (the "invert"
303 * or "!invert"). Then it sets the current program entry's target to the saved
304 * "target" value (the old value of the program that had its "target" updated
307 * Looking back at "a || b && c", we have the following steps:
308 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
309 * "||" - flag that we need to process "||"; continue outer loop
310 * "b" - prog[1] = { "b", X, 0 }
311 * "&&" - flag that we need to process "&&"; continue outer loop
312 * (Notice we did not process "||")
313 * "c" - prog[2] = { "c", X, 1 }
314 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
315 * t = prog[2].target; // t = 1
316 * s = prog[t].target; // s = 0
317 * prog[t].target = 2; // Set target to "l2"
318 * prog[t].when_to_branch = 0;
319 * prog[2].target = s;
320 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
321 * t = prog[2].target; // t = 0
322 * s = prog[t].target; // s = -1
323 * prog[t].target = 2; // Set target to "l2"
324 * prog[t].when_to_branch = 1;
325 * prog[2].target = s;
327 * #13 Which brings us to the final step of the first pass, which is to set
328 * the last program entry's when_to_branch and target, which will be
329 * when_to_branch = 0; target = N; ( the label after the program entry after
330 * the last program entry processed above).
332 * If we denote "TRUE" to be the entry after the last program entry processed,
333 * and "FALSE" the program entry after that, we are now done with the first
336 * Making the above "a || b && c" have a program of:
337 * prog[0] = { "a", 1, 2 }
338 * prog[1] = { "b", 0, 2 }
339 * prog[2] = { "c", 0, 3 }
341 * Which translates into:
342 * n0: r = a; l0: if (r) goto l2;
343 * n1: r = b; l1: if (!r) goto l2;
344 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
345 * T: return TRUE; l3:
348 * Although, after the first pass, the program is correct, it is
349 * inefficient. The simple sample of "a || b && c" could be easily been
351 * n0: r = a; if (r) goto T
352 * n1: r = b; if (!r) goto F
353 * n2: r = c; if (!r) goto F
357 * The First Pass is over the input string. The next too passes are over
358 * the program itself.
362 * Which brings us to the second pass. If a jump to a label has the
363 * same condition as that label, it can instead jump to its target.
364 * The original example of "a && !(!b || (c && g)) || d || e && !f"
365 * where the first pass gives us:
367 * n1: r=a; l1: if (!r) goto l4;
368 * n2: r=b; l2: if (!r) goto l4;
369 * n3: r=c; r=!r; l3: if (r) goto l4;
370 * n4: r=g; r=!r; l4: if (r) goto l5;
371 * n5: r=d; l5: if (r) goto T
372 * n6: r=e; l6: if (!r) goto l7;
373 * n7: r=f; r=!r; l7: if (!r) goto F:
377 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
378 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
379 * to go directly to T. To accomplish this, we start from the last
380 * entry in the program and work our way back. If the target of the entry
381 * has the same "when_to_branch" then we could use that entry's target.
382 * Doing this, the above would end up as:
384 * n1: r=a; l1: if (!r) goto l4;
385 * n2: r=b; l2: if (!r) goto l4;
386 * n3: r=c; r=!r; l3: if (r) goto T;
387 * n4: r=g; r=!r; l4: if (r) goto T;
388 * n5: r=d; l5: if (r) goto T;
389 * n6: r=e; l6: if (!r) goto F;
390 * n7: r=f; r=!r; l7: if (!r) goto F;
394 * In that same pass, if the "when_to_branch" doesn't match, we can simply
395 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
396 * where "l4: if (r) goto T;", then we can convert l2 to be:
397 * "l2: if (!r) goto n5;".
399 * This will have the second pass give us:
400 * n1: r=a; l1: if (!r) goto n5;
401 * n2: r=b; l2: if (!r) goto n5;
402 * n3: r=c; r=!r; l3: if (r) goto T;
403 * n4: r=g; r=!r; l4: if (r) goto T;
404 * n5: r=d; l5: if (r) goto T
405 * n6: r=e; l6: if (!r) goto F;
406 * n7: r=f; r=!r; l7: if (!r) goto F
410 * Notice, all the "l#" labels are no longer used, and they can now
415 * For the third pass we deal with the inverts. As they simply just
416 * make the "when_to_branch" get inverted, a simple loop over the
417 * program to that does: "when_to_branch ^= invert;" will do the
418 * job, leaving us with:
419 * n1: r=a; if (!r) goto n5;
420 * n2: r=b; if (!r) goto n5;
421 * n3: r=c: if (!r) goto T;
422 * n4: r=g; if (!r) goto T;
423 * n5: r=d; if (r) goto T
424 * n6: r=e; if (!r) goto F;
425 * n7: r=f; if (r) goto F
429 * As "r = a; if (!r) goto n5;" is obviously the same as
430 * "if (!a) goto n5;" without doing anything we can interpret the
432 * n1: if (!a) goto n5;
433 * n2: if (!b) goto n5;
434 * n3: if (!c) goto T;
435 * n4: if (!g) goto T;
437 * n6: if (!e) goto F;
442 * Since the inverts are discarded at the end, there's no reason to store
443 * them in the program array (and waste memory). A separate array to hold
444 * the inverts is used and freed at the end.
446 static struct prog_entry *
447 predicate_parse(const char *str, int nr_parens, int nr_preds,
448 parse_pred_fn parse_pred, void *data,
449 struct filter_parse_error *pe)
451 struct prog_entry *prog_stack;
452 struct prog_entry *prog;
453 const char *ptr = str;
454 char *inverts = NULL;
463 nr_preds += 2; /* For TRUE and FALSE */
465 op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL);
467 return ERR_PTR(-ENOMEM);
468 prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL);
470 parse_error(pe, -ENOMEM, 0);
473 inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL);
475 parse_error(pe, -ENOMEM, 0);
484 while (*ptr) { /* #1 */
485 const char *next = ptr++;
492 if (top - op_stack > nr_parens) {
506 parse_error(pe, FILT_ERR_TOO_MANY_PREDS, next - str);
510 inverts[N] = invert; /* #4 */
511 prog[N].target = N-1;
513 len = parse_pred(next, data, ptr - str, pe, &prog[N].pred);
534 /* accepting only "&&" or "||" */
535 if (next[1] == next[0]) {
541 parse_error(pe, FILT_ERR_TOO_MANY_PREDS,
546 invert = *top & INVERT;
548 if (*top & PROCESS_AND) { /* #7 */
549 update_preds(prog, N - 1, invert);
550 *top &= ~PROCESS_AND;
552 if (*next == '&') { /* #8 */
556 if (*top & PROCESS_OR) { /* #9 */
557 update_preds(prog, N - 1, !invert);
560 if (*next == '|') { /* #10 */
564 if (!*next) /* #11 */
567 if (top == op_stack) {
570 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, ptr - str);
577 if (top != op_stack) {
579 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, ptr - str);
586 parse_error(pe, FILT_ERR_NO_FILTER, ptr - str);
590 prog[N].pred = NULL; /* #13 */
591 prog[N].target = 1; /* TRUE */
592 prog[N+1].pred = NULL;
593 prog[N+1].target = 0; /* FALSE */
594 prog[N-1].target = N;
595 prog[N-1].when_to_branch = false;
598 for (i = N-1 ; i--; ) {
599 int target = prog[i].target;
600 if (prog[i].when_to_branch == prog[target].when_to_branch)
601 prog[i].target = prog[target].target;
605 for (i = 0; i < N; i++) {
606 invert = inverts[i] ^ prog[i].when_to_branch;
607 prog[i].when_to_branch = invert;
608 /* Make sure the program always moves forward */
609 if (WARN_ON(prog[i].target <= i)) {
622 for (i = 0; prog_stack[i].pred; i++)
623 kfree(prog_stack[i].pred);
629 enum pred_cmp_types {
638 #define DEFINE_COMPARISON_PRED(type) \
639 static int filter_pred_##type(struct filter_pred *pred, void *event) \
641 switch (pred->op) { \
643 type *addr = (type *)(event + pred->offset); \
644 type val = (type)pred->val; \
645 return *addr < val; \
648 type *addr = (type *)(event + pred->offset); \
649 type val = (type)pred->val; \
650 return *addr <= val; \
653 type *addr = (type *)(event + pred->offset); \
654 type val = (type)pred->val; \
655 return *addr > val; \
658 type *addr = (type *)(event + pred->offset); \
659 type val = (type)pred->val; \
660 return *addr >= val; \
663 type *addr = (type *)(event + pred->offset); \
664 type val = (type)pred->val; \
665 return !!(*addr & val); \
672 #define DEFINE_EQUALITY_PRED(size) \
673 static int filter_pred_##size(struct filter_pred *pred, void *event) \
675 u##size *addr = (u##size *)(event + pred->offset); \
676 u##size val = (u##size)pred->val; \
679 match = (val == *addr) ^ pred->not; \
684 DEFINE_COMPARISON_PRED(s64);
685 DEFINE_COMPARISON_PRED(u64);
686 DEFINE_COMPARISON_PRED(s32);
687 DEFINE_COMPARISON_PRED(u32);
688 DEFINE_COMPARISON_PRED(s16);
689 DEFINE_COMPARISON_PRED(u16);
690 DEFINE_COMPARISON_PRED(s8);
691 DEFINE_COMPARISON_PRED(u8);
693 DEFINE_EQUALITY_PRED(64);
694 DEFINE_EQUALITY_PRED(32);
695 DEFINE_EQUALITY_PRED(16);
696 DEFINE_EQUALITY_PRED(8);
698 /* user space strings temp buffer */
699 #define USTRING_BUF_SIZE 1024
701 struct ustring_buffer {
702 char buffer[USTRING_BUF_SIZE];
705 static __percpu struct ustring_buffer *ustring_per_cpu;
707 static __always_inline char *test_string(char *str)
709 struct ustring_buffer *ubuf;
712 if (!ustring_per_cpu)
715 ubuf = this_cpu_ptr(ustring_per_cpu);
718 /* For safety, do not trust the string pointer */
719 if (!strncpy_from_kernel_nofault(kstr, str, USTRING_BUF_SIZE))
724 static __always_inline char *test_ustring(char *str)
726 struct ustring_buffer *ubuf;
730 if (!ustring_per_cpu)
733 ubuf = this_cpu_ptr(ustring_per_cpu);
736 /* user space address? */
737 ustr = (char __user *)str;
738 if (!strncpy_from_user_nofault(kstr, ustr, USTRING_BUF_SIZE))
744 /* Filter predicate for fixed sized arrays of characters */
745 static int filter_pred_string(struct filter_pred *pred, void *event)
747 char *addr = (char *)(event + pred->offset);
750 cmp = pred->regex.match(addr, &pred->regex, pred->regex.field_len);
752 match = cmp ^ pred->not;
757 static __always_inline int filter_pchar(struct filter_pred *pred, char *str)
762 len = strlen(str) + 1; /* including tailing '\0' */
763 cmp = pred->regex.match(str, &pred->regex, len);
765 match = cmp ^ pred->not;
769 /* Filter predicate for char * pointers */
770 static int filter_pred_pchar(struct filter_pred *pred, void *event)
772 char **addr = (char **)(event + pred->offset);
775 str = test_string(*addr);
779 return filter_pchar(pred, str);
782 /* Filter predicate for char * pointers in user space*/
783 static int filter_pred_pchar_user(struct filter_pred *pred, void *event)
785 char **addr = (char **)(event + pred->offset);
788 str = test_ustring(*addr);
792 return filter_pchar(pred, str);
796 * Filter predicate for dynamic sized arrays of characters.
797 * These are implemented through a list of strings at the end
799 * Also each of these strings have a field in the entry which
800 * contains its offset from the beginning of the entry.
801 * We have then first to get this field, dereference it
802 * and add it to the address of the entry, and at last we have
803 * the address of the string.
805 static int filter_pred_strloc(struct filter_pred *pred, void *event)
807 u32 str_item = *(u32 *)(event + pred->offset);
808 int str_loc = str_item & 0xffff;
809 int str_len = str_item >> 16;
810 char *addr = (char *)(event + str_loc);
813 cmp = pred->regex.match(addr, &pred->regex, str_len);
815 match = cmp ^ pred->not;
821 * Filter predicate for relative dynamic sized arrays of characters.
822 * These are implemented through a list of strings at the end
823 * of the entry as same as dynamic string.
824 * The difference is that the relative one records the location offset
825 * from the field itself, not the event entry.
827 static int filter_pred_strrelloc(struct filter_pred *pred, void *event)
829 u32 *item = (u32 *)(event + pred->offset);
830 u32 str_item = *item;
831 int str_loc = str_item & 0xffff;
832 int str_len = str_item >> 16;
833 char *addr = (char *)(&item[1]) + str_loc;
836 cmp = pred->regex.match(addr, &pred->regex, str_len);
838 match = cmp ^ pred->not;
843 /* Filter predicate for CPUs. */
844 static int filter_pred_cpu(struct filter_pred *pred, void *event)
848 cpu = raw_smp_processor_id();
869 /* Filter predicate for COMM. */
870 static int filter_pred_comm(struct filter_pred *pred, void *event)
874 cmp = pred->regex.match(current->comm, &pred->regex,
876 return cmp ^ pred->not;
880 * regex_match_foo - Basic regex callbacks
882 * @str: the string to be searched
883 * @r: the regex structure containing the pattern string
884 * @len: the length of the string to be searched (including '\0')
887 * - @str might not be NULL-terminated if it's of type DYN_STRING
888 * RDYN_STRING, or STATIC_STRING, unless @len is zero.
891 static int regex_match_full(char *str, struct regex *r, int len)
893 /* len of zero means str is dynamic and ends with '\0' */
895 return strcmp(str, r->pattern) == 0;
897 return strncmp(str, r->pattern, len) == 0;
900 static int regex_match_front(char *str, struct regex *r, int len)
902 if (len && len < r->len)
905 return strncmp(str, r->pattern, r->len) == 0;
908 static int regex_match_middle(char *str, struct regex *r, int len)
911 return strstr(str, r->pattern) != NULL;
913 return strnstr(str, r->pattern, len) != NULL;
916 static int regex_match_end(char *str, struct regex *r, int len)
918 int strlen = len - 1;
920 if (strlen >= r->len &&
921 memcmp(str + strlen - r->len, r->pattern, r->len) == 0)
926 static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused)
928 if (glob_match(r->pattern, str))
934 * filter_parse_regex - parse a basic regex
935 * @buff: the raw regex
936 * @len: length of the regex
937 * @search: will point to the beginning of the string to compare
938 * @not: tell whether the match will have to be inverted
940 * This passes in a buffer containing a regex and this function will
941 * set search to point to the search part of the buffer and
942 * return the type of search it is (see enum above).
943 * This does modify buff.
946 * search returns the pointer to use for comparison.
947 * not returns 1 if buff started with a '!'
950 enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not)
952 int type = MATCH_FULL;
955 if (buff[0] == '!') {
964 if (isdigit(buff[0]))
967 for (i = 0; i < len; i++) {
968 if (buff[i] == '*') {
970 type = MATCH_END_ONLY;
971 } else if (i == len - 1) {
972 if (type == MATCH_END_ONLY)
973 type = MATCH_MIDDLE_ONLY;
975 type = MATCH_FRONT_ONLY;
978 } else { /* pattern continues, use full glob */
981 } else if (strchr("[?\\", buff[i])) {
991 static void filter_build_regex(struct filter_pred *pred)
993 struct regex *r = &pred->regex;
995 enum regex_type type = MATCH_FULL;
997 if (pred->op == OP_GLOB) {
998 type = filter_parse_regex(r->pattern, r->len, &search, &pred->not);
999 r->len = strlen(search);
1000 memmove(r->pattern, search, r->len+1);
1004 /* MATCH_INDEX should not happen, but if it does, match full */
1007 r->match = regex_match_full;
1009 case MATCH_FRONT_ONLY:
1010 r->match = regex_match_front;
1012 case MATCH_MIDDLE_ONLY:
1013 r->match = regex_match_middle;
1015 case MATCH_END_ONLY:
1016 r->match = regex_match_end;
1019 r->match = regex_match_glob;
1025 #ifdef CONFIG_FTRACE_STARTUP_TEST
1026 static int test_pred_visited_fn(struct filter_pred *pred, void *event);
1028 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
1035 static int filter_pred_fn_call(struct filter_pred *pred, void *event);
1037 /* return 1 if event matches, 0 otherwise (discard) */
1038 int filter_match_preds(struct event_filter *filter, void *rec)
1040 struct prog_entry *prog;
1043 /* no filter is considered a match */
1047 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
1048 prog = rcu_dereference_raw(filter->prog);
1052 for (i = 0; prog[i].pred; i++) {
1053 struct filter_pred *pred = prog[i].pred;
1054 int match = filter_pred_fn_call(pred, rec);
1055 if (match == prog[i].when_to_branch)
1058 return prog[i].target;
1060 EXPORT_SYMBOL_GPL(filter_match_preds);
1062 static void remove_filter_string(struct event_filter *filter)
1067 kfree(filter->filter_string);
1068 filter->filter_string = NULL;
1071 static void append_filter_err(struct trace_array *tr,
1072 struct filter_parse_error *pe,
1073 struct event_filter *filter)
1075 struct trace_seq *s;
1076 int pos = pe->lasterr_pos;
1080 if (WARN_ON(!filter->filter_string))
1083 s = kmalloc(sizeof(*s), GFP_KERNEL);
1088 len = strlen(filter->filter_string);
1092 /* indexing is off by one */
1096 trace_seq_puts(s, filter->filter_string);
1097 if (pe->lasterr > 0) {
1098 trace_seq_printf(s, "\n%*s", pos, "^");
1099 trace_seq_printf(s, "\nparse_error: %s\n", err_text[pe->lasterr]);
1100 tracing_log_err(tr, "event filter parse error",
1101 filter->filter_string, err_text,
1102 pe->lasterr, pe->lasterr_pos);
1104 trace_seq_printf(s, "\nError: (%d)\n", pe->lasterr);
1105 tracing_log_err(tr, "event filter parse error",
1106 filter->filter_string, err_text,
1109 trace_seq_putc(s, 0);
1110 buf = kmemdup_nul(s->buffer, s->seq.len, GFP_KERNEL);
1112 kfree(filter->filter_string);
1113 filter->filter_string = buf;
1118 static inline struct event_filter *event_filter(struct trace_event_file *file)
1120 return file->filter;
1123 /* caller must hold event_mutex */
1124 void print_event_filter(struct trace_event_file *file, struct trace_seq *s)
1126 struct event_filter *filter = event_filter(file);
1128 if (filter && filter->filter_string)
1129 trace_seq_printf(s, "%s\n", filter->filter_string);
1131 trace_seq_puts(s, "none\n");
1134 void print_subsystem_event_filter(struct event_subsystem *system,
1135 struct trace_seq *s)
1137 struct event_filter *filter;
1139 mutex_lock(&event_mutex);
1140 filter = system->filter;
1141 if (filter && filter->filter_string)
1142 trace_seq_printf(s, "%s\n", filter->filter_string);
1144 trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n");
1145 mutex_unlock(&event_mutex);
1148 static void free_prog(struct event_filter *filter)
1150 struct prog_entry *prog;
1153 prog = rcu_access_pointer(filter->prog);
1157 for (i = 0; prog[i].pred; i++)
1158 kfree(prog[i].pred);
1162 static void filter_disable(struct trace_event_file *file)
1164 unsigned long old_flags = file->flags;
1166 file->flags &= ~EVENT_FILE_FL_FILTERED;
1168 if (old_flags != file->flags)
1169 trace_buffered_event_disable();
1172 static void __free_filter(struct event_filter *filter)
1178 kfree(filter->filter_string);
1182 void free_event_filter(struct event_filter *filter)
1184 __free_filter(filter);
1187 static inline void __remove_filter(struct trace_event_file *file)
1189 filter_disable(file);
1190 remove_filter_string(file->filter);
1193 static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir,
1194 struct trace_array *tr)
1196 struct trace_event_file *file;
1198 list_for_each_entry(file, &tr->events, list) {
1199 if (file->system != dir)
1201 __remove_filter(file);
1205 static inline void __free_subsystem_filter(struct trace_event_file *file)
1207 __free_filter(file->filter);
1208 file->filter = NULL;
1211 static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir,
1212 struct trace_array *tr)
1214 struct trace_event_file *file;
1216 list_for_each_entry(file, &tr->events, list) {
1217 if (file->system != dir)
1219 __free_subsystem_filter(file);
1223 int filter_assign_type(const char *type)
1225 if (strstr(type, "__data_loc") && strstr(type, "char"))
1226 return FILTER_DYN_STRING;
1228 if (strstr(type, "__rel_loc") && strstr(type, "char"))
1229 return FILTER_RDYN_STRING;
1231 if (strchr(type, '[') && strstr(type, "char"))
1232 return FILTER_STATIC_STRING;
1234 if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0)
1235 return FILTER_PTR_STRING;
1237 return FILTER_OTHER;
1240 static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op,
1241 int field_size, int field_is_signed)
1243 enum filter_pred_fn fn = FILTER_PRED_FN_NOP;
1244 int pred_func_index = -1;
1251 if (WARN_ON_ONCE(op < PRED_FUNC_START))
1253 pred_func_index = op - PRED_FUNC_START;
1254 if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
1258 switch (field_size) {
1260 if (pred_func_index < 0)
1261 fn = FILTER_PRED_FN_64;
1262 else if (field_is_signed)
1263 fn = FILTER_PRED_FN_S64;
1265 fn = FILTER_PRED_FN_U64;
1268 if (pred_func_index < 0)
1269 fn = FILTER_PRED_FN_32;
1270 else if (field_is_signed)
1271 fn = FILTER_PRED_FN_S32;
1273 fn = FILTER_PRED_FN_U32;
1276 if (pred_func_index < 0)
1277 fn = FILTER_PRED_FN_16;
1278 else if (field_is_signed)
1279 fn = FILTER_PRED_FN_S16;
1281 fn = FILTER_PRED_FN_U16;
1284 if (pred_func_index < 0)
1285 fn = FILTER_PRED_FN_8;
1286 else if (field_is_signed)
1287 fn = FILTER_PRED_FN_S8;
1289 fn = FILTER_PRED_FN_U8;
1297 static int filter_pred_fn_call(struct filter_pred *pred, void *event)
1299 switch (pred->fn_num) {
1300 case FILTER_PRED_FN_64:
1301 return filter_pred_64(pred, event);
1302 case FILTER_PRED_FN_S64:
1303 return filter_pred_s64(pred, event);
1304 case FILTER_PRED_FN_U64:
1305 return filter_pred_u64(pred, event);
1306 case FILTER_PRED_FN_32:
1307 return filter_pred_32(pred, event);
1308 case FILTER_PRED_FN_S32:
1309 return filter_pred_s32(pred, event);
1310 case FILTER_PRED_FN_U32:
1311 return filter_pred_u32(pred, event);
1312 case FILTER_PRED_FN_16:
1313 return filter_pred_16(pred, event);
1314 case FILTER_PRED_FN_S16:
1315 return filter_pred_s16(pred, event);
1316 case FILTER_PRED_FN_U16:
1317 return filter_pred_u16(pred, event);
1318 case FILTER_PRED_FN_8:
1319 return filter_pred_8(pred, event);
1320 case FILTER_PRED_FN_S8:
1321 return filter_pred_s8(pred, event);
1322 case FILTER_PRED_FN_U8:
1323 return filter_pred_u8(pred, event);
1324 case FILTER_PRED_FN_COMM:
1325 return filter_pred_comm(pred, event);
1326 case FILTER_PRED_FN_STRING:
1327 return filter_pred_string(pred, event);
1328 case FILTER_PRED_FN_STRLOC:
1329 return filter_pred_strloc(pred, event);
1330 case FILTER_PRED_FN_STRRELLOC:
1331 return filter_pred_strrelloc(pred, event);
1332 case FILTER_PRED_FN_PCHAR_USER:
1333 return filter_pred_pchar_user(pred, event);
1334 case FILTER_PRED_FN_PCHAR:
1335 return filter_pred_pchar(pred, event);
1336 case FILTER_PRED_FN_CPU:
1337 return filter_pred_cpu(pred, event);
1338 case FILTER_PRED_TEST_VISITED:
1339 return test_pred_visited_fn(pred, event);
1345 /* Called when a predicate is encountered by predicate_parse() */
1346 static int parse_pred(const char *str, void *data,
1347 int pos, struct filter_parse_error *pe,
1348 struct filter_pred **pred_ptr)
1350 struct trace_event_call *call = data;
1351 struct ftrace_event_field *field;
1352 struct filter_pred *pred = NULL;
1353 char num_buf[24]; /* Big enough to hold an address */
1355 bool ustring = false;
1364 /* First find the field to associate to */
1365 while (isspace(str[i]))
1369 while (isalnum(str[i]) || str[i] == '_')
1377 field_name = kmemdup_nul(str + s, len, GFP_KERNEL);
1381 /* Make sure that the field exists */
1383 field = trace_find_event_field(call, field_name);
1386 parse_error(pe, FILT_ERR_FIELD_NOT_FOUND, pos + i);
1390 /* See if the field is a user space string */
1391 if ((len = str_has_prefix(str + i, ".ustring"))) {
1396 while (isspace(str[i]))
1399 /* Make sure this op is supported */
1400 for (op = 0; ops[op]; op++) {
1401 /* This is why '<=' must come before '<' in ops[] */
1402 if (strncmp(str + i, ops[op], strlen(ops[op])) == 0)
1407 parse_error(pe, FILT_ERR_INVALID_OP, pos + i);
1411 i += strlen(ops[op]);
1413 while (isspace(str[i]))
1418 pred = kzalloc(sizeof(*pred), GFP_KERNEL);
1422 pred->field = field;
1423 pred->offset = field->offset;
1426 if (ftrace_event_is_function(call)) {
1428 * Perf does things different with function events.
1429 * It only allows an "ip" field, and expects a string.
1430 * But the string does not need to be surrounded by quotes.
1431 * If it is a string, the assigned function as a nop,
1432 * (perf doesn't use it) and grab everything.
1434 if (strcmp(field->name, "ip") != 0) {
1435 parse_error(pe, FILT_ERR_IP_FIELD_ONLY, pos + i);
1438 pred->fn_num = FILTER_PRED_FN_NOP;
1441 * Quotes are not required, but if they exist then we need
1442 * to read them till we hit a matching one.
1444 if (str[i] == '\'' || str[i] == '"')
1449 for (i++; str[i]; i++) {
1450 if (q && str[i] == q)
1452 if (!q && (str[i] == ')' || str[i] == '&' ||
1460 if (len >= MAX_FILTER_STR_VAL) {
1461 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1465 pred->regex.len = len;
1466 strncpy(pred->regex.pattern, str + s, len);
1467 pred->regex.pattern[len] = 0;
1469 /* This is either a string, or an integer */
1470 } else if (str[i] == '\'' || str[i] == '"') {
1473 /* Make sure the op is OK for strings */
1482 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1486 /* Make sure the field is OK for strings */
1487 if (!is_string_field(field)) {
1488 parse_error(pe, FILT_ERR_EXPECT_DIGIT, pos + i);
1492 for (i++; str[i]; i++) {
1497 parse_error(pe, FILT_ERR_MISSING_QUOTE, pos + i);
1504 if (len >= MAX_FILTER_STR_VAL) {
1505 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1509 pred->regex.len = len;
1510 strncpy(pred->regex.pattern, str + s, len);
1511 pred->regex.pattern[len] = 0;
1513 filter_build_regex(pred);
1515 if (field->filter_type == FILTER_COMM) {
1516 pred->fn_num = FILTER_PRED_FN_COMM;
1518 } else if (field->filter_type == FILTER_STATIC_STRING) {
1519 pred->fn_num = FILTER_PRED_FN_STRING;
1520 pred->regex.field_len = field->size;
1522 } else if (field->filter_type == FILTER_DYN_STRING) {
1523 pred->fn_num = FILTER_PRED_FN_STRLOC;
1524 } else if (field->filter_type == FILTER_RDYN_STRING)
1525 pred->fn_num = FILTER_PRED_FN_STRRELLOC;
1528 if (!ustring_per_cpu) {
1529 /* Once allocated, keep it around for good */
1530 ustring_per_cpu = alloc_percpu(struct ustring_buffer);
1531 if (!ustring_per_cpu)
1536 pred->fn_num = FILTER_PRED_FN_PCHAR_USER;
1538 pred->fn_num = FILTER_PRED_FN_PCHAR;
1540 /* go past the last quote */
1543 } else if (isdigit(str[i]) || str[i] == '-') {
1545 /* Make sure the field is not a string */
1546 if (is_string_field(field)) {
1547 parse_error(pe, FILT_ERR_EXPECT_STRING, pos + i);
1551 if (op == OP_GLOB) {
1552 parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
1559 /* We allow 0xDEADBEEF */
1560 while (isalnum(str[i]))
1564 /* 0xfeedfacedeadbeef is 18 chars max */
1565 if (len >= sizeof(num_buf)) {
1566 parse_error(pe, FILT_ERR_OPERAND_TOO_LONG, pos + i);
1570 strncpy(num_buf, str + s, len);
1573 /* Make sure it is a value */
1574 if (field->is_signed)
1575 ret = kstrtoll(num_buf, 0, &val);
1577 ret = kstrtoull(num_buf, 0, &val);
1579 parse_error(pe, FILT_ERR_ILLEGAL_INTVAL, pos + s);
1585 if (field->filter_type == FILTER_CPU)
1586 pred->fn_num = FILTER_PRED_FN_CPU;
1588 pred->fn_num = select_comparison_fn(pred->op, field->size,
1590 if (pred->op == OP_NE)
1595 parse_error(pe, FILT_ERR_INVALID_VALUE, pos + i);
1611 TOO_MANY_CLOSE = -1,
1617 * Read the filter string once to calculate the number of predicates
1618 * as well as how deep the parentheses go.
1621 * 0 - everything is fine (err is undefined)
1624 * -3 - No matching quote
1626 static int calc_stack(const char *str, int *parens, int *preds, int *err)
1628 bool is_pred = false;
1630 int open = 1; /* Count the expression as "(E)" */
1638 for (i = 0; str[i]; i++) {
1639 if (isspace(str[i]))
1642 if (str[i] == quote)
1655 if (str[i+1] != str[i])
1662 if (open > max_open)
1669 return TOO_MANY_CLOSE;
1682 return MISSING_QUOTE;
1688 /* find the bad open */
1691 if (str[i] == quote)
1697 if (level == open) {
1699 return TOO_MANY_OPEN;
1712 /* First character is the '(' with missing ')' */
1714 return TOO_MANY_OPEN;
1717 /* Set the size of the required stacks */
1723 static int process_preds(struct trace_event_call *call,
1724 const char *filter_string,
1725 struct event_filter *filter,
1726 struct filter_parse_error *pe)
1728 struct prog_entry *prog;
1734 ret = calc_stack(filter_string, &nr_parens, &nr_preds, &index);
1738 parse_error(pe, FILT_ERR_MISSING_QUOTE, index);
1741 parse_error(pe, FILT_ERR_TOO_MANY_OPEN, index);
1744 parse_error(pe, FILT_ERR_TOO_MANY_CLOSE, index);
1752 prog = predicate_parse(filter_string, nr_parens, nr_preds,
1753 parse_pred, call, pe);
1755 return PTR_ERR(prog);
1757 rcu_assign_pointer(filter->prog, prog);
1761 static inline void event_set_filtered_flag(struct trace_event_file *file)
1763 unsigned long old_flags = file->flags;
1765 file->flags |= EVENT_FILE_FL_FILTERED;
1767 if (old_flags != file->flags)
1768 trace_buffered_event_enable();
1771 static inline void event_set_filter(struct trace_event_file *file,
1772 struct event_filter *filter)
1774 rcu_assign_pointer(file->filter, filter);
1777 static inline void event_clear_filter(struct trace_event_file *file)
1779 RCU_INIT_POINTER(file->filter, NULL);
1782 struct filter_list {
1783 struct list_head list;
1784 struct event_filter *filter;
1787 static int process_system_preds(struct trace_subsystem_dir *dir,
1788 struct trace_array *tr,
1789 struct filter_parse_error *pe,
1790 char *filter_string)
1792 struct trace_event_file *file;
1793 struct filter_list *filter_item;
1794 struct event_filter *filter = NULL;
1795 struct filter_list *tmp;
1796 LIST_HEAD(filter_list);
1800 list_for_each_entry(file, &tr->events, list) {
1802 if (file->system != dir)
1805 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1809 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1810 if (!filter->filter_string)
1813 err = process_preds(file->event_call, filter_string, filter, pe);
1815 filter_disable(file);
1816 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1817 append_filter_err(tr, pe, filter);
1819 event_set_filtered_flag(file);
1822 filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL);
1826 list_add_tail(&filter_item->list, &filter_list);
1828 * Regardless of if this returned an error, we still
1829 * replace the filter for the call.
1831 filter_item->filter = event_filter(file);
1832 event_set_filter(file, filter);
1842 * The calls can still be using the old filters.
1843 * Do a synchronize_rcu() and to ensure all calls are
1844 * done with them before we free them.
1846 tracepoint_synchronize_unregister();
1847 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1848 __free_filter(filter_item->filter);
1849 list_del(&filter_item->list);
1854 /* No call succeeded */
1855 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1856 list_del(&filter_item->list);
1859 parse_error(pe, FILT_ERR_BAD_SUBSYS_FILTER, 0);
1862 __free_filter(filter);
1863 /* If any call succeeded, we still need to sync */
1865 tracepoint_synchronize_unregister();
1866 list_for_each_entry_safe(filter_item, tmp, &filter_list, list) {
1867 __free_filter(filter_item->filter);
1868 list_del(&filter_item->list);
1874 static int create_filter_start(char *filter_string, bool set_str,
1875 struct filter_parse_error **pse,
1876 struct event_filter **filterp)
1878 struct event_filter *filter;
1879 struct filter_parse_error *pe = NULL;
1882 if (WARN_ON_ONCE(*pse || *filterp))
1885 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
1886 if (filter && set_str) {
1887 filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
1888 if (!filter->filter_string)
1892 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1894 if (!filter || !pe || err) {
1896 __free_filter(filter);
1900 /* we're committed to creating a new filter */
1907 static void create_filter_finish(struct filter_parse_error *pe)
1913 * create_filter - create a filter for a trace_event_call
1914 * @tr: the trace array associated with these events
1915 * @call: trace_event_call to create a filter for
1916 * @filter_string: filter string
1917 * @set_str: remember @filter_str and enable detailed error in filter
1918 * @filterp: out param for created filter (always updated on return)
1919 * Must be a pointer that references a NULL pointer.
1921 * Creates a filter for @call with @filter_str. If @set_str is %true,
1922 * @filter_str is copied and recorded in the new filter.
1924 * On success, returns 0 and *@filterp points to the new filter. On
1925 * failure, returns -errno and *@filterp may point to %NULL or to a new
1926 * filter. In the latter case, the returned filter contains error
1927 * information if @set_str is %true and the caller is responsible for
1930 static int create_filter(struct trace_array *tr,
1931 struct trace_event_call *call,
1932 char *filter_string, bool set_str,
1933 struct event_filter **filterp)
1935 struct filter_parse_error *pe = NULL;
1938 /* filterp must point to NULL */
1939 if (WARN_ON(*filterp))
1942 err = create_filter_start(filter_string, set_str, &pe, filterp);
1946 err = process_preds(call, filter_string, *filterp, pe);
1948 append_filter_err(tr, pe, *filterp);
1949 create_filter_finish(pe);
1954 int create_event_filter(struct trace_array *tr,
1955 struct trace_event_call *call,
1956 char *filter_str, bool set_str,
1957 struct event_filter **filterp)
1959 return create_filter(tr, call, filter_str, set_str, filterp);
1963 * create_system_filter - create a filter for an event subsystem
1964 * @dir: the descriptor for the subsystem directory
1965 * @filter_str: filter string
1966 * @filterp: out param for created filter (always updated on return)
1968 * Identical to create_filter() except that it creates a subsystem filter
1969 * and always remembers @filter_str.
1971 static int create_system_filter(struct trace_subsystem_dir *dir,
1972 char *filter_str, struct event_filter **filterp)
1974 struct filter_parse_error *pe = NULL;
1977 err = create_filter_start(filter_str, true, &pe, filterp);
1979 err = process_system_preds(dir, dir->tr, pe, filter_str);
1981 /* System filters just show a default message */
1982 kfree((*filterp)->filter_string);
1983 (*filterp)->filter_string = NULL;
1985 append_filter_err(dir->tr, pe, *filterp);
1988 create_filter_finish(pe);
1993 /* caller must hold event_mutex */
1994 int apply_event_filter(struct trace_event_file *file, char *filter_string)
1996 struct trace_event_call *call = file->event_call;
1997 struct event_filter *filter = NULL;
2000 if (!strcmp(strstrip(filter_string), "0")) {
2001 filter_disable(file);
2002 filter = event_filter(file);
2007 event_clear_filter(file);
2009 /* Make sure the filter is not being used */
2010 tracepoint_synchronize_unregister();
2011 __free_filter(filter);
2016 err = create_filter(file->tr, call, filter_string, true, &filter);
2019 * Always swap the call filter with the new filter
2020 * even if there was an error. If there was an error
2021 * in the filter, we disable the filter and show the error
2025 struct event_filter *tmp;
2027 tmp = event_filter(file);
2029 event_set_filtered_flag(file);
2031 filter_disable(file);
2033 event_set_filter(file, filter);
2036 /* Make sure the call is done with the filter */
2037 tracepoint_synchronize_unregister();
2045 int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
2046 char *filter_string)
2048 struct event_subsystem *system = dir->subsystem;
2049 struct trace_array *tr = dir->tr;
2050 struct event_filter *filter = NULL;
2053 mutex_lock(&event_mutex);
2055 /* Make sure the system still has events */
2056 if (!dir->nr_events) {
2061 if (!strcmp(strstrip(filter_string), "0")) {
2062 filter_free_subsystem_preds(dir, tr);
2063 remove_filter_string(system->filter);
2064 filter = system->filter;
2065 system->filter = NULL;
2066 /* Ensure all filters are no longer used */
2067 tracepoint_synchronize_unregister();
2068 filter_free_subsystem_filters(dir, tr);
2069 __free_filter(filter);
2073 err = create_system_filter(dir, filter_string, &filter);
2076 * No event actually uses the system filter
2077 * we can free it without synchronize_rcu().
2079 __free_filter(system->filter);
2080 system->filter = filter;
2083 mutex_unlock(&event_mutex);
2088 #ifdef CONFIG_PERF_EVENTS
2090 void ftrace_profile_free_filter(struct perf_event *event)
2092 struct event_filter *filter = event->filter;
2094 event->filter = NULL;
2095 __free_filter(filter);
2098 struct function_filter_data {
2099 struct ftrace_ops *ops;
2104 #ifdef CONFIG_FUNCTION_TRACER
2106 ftrace_function_filter_re(char *buf, int len, int *count)
2110 str = kstrndup(buf, len, GFP_KERNEL);
2115 * The argv_split function takes white space
2116 * as a separator, so convert ',' into spaces.
2118 strreplace(str, ',', ' ');
2120 re = argv_split(GFP_KERNEL, str, count);
2125 static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
2126 int reset, char *re, int len)
2131 ret = ftrace_set_filter(ops, re, len, reset);
2133 ret = ftrace_set_notrace(ops, re, len, reset);
2138 static int __ftrace_function_set_filter(int filter, char *buf, int len,
2139 struct function_filter_data *data)
2141 int i, re_cnt, ret = -EINVAL;
2145 reset = filter ? &data->first_filter : &data->first_notrace;
2148 * The 'ip' field could have multiple filters set, separated
2149 * either by space or comma. We first cut the filter and apply
2150 * all pieces separately.
2152 re = ftrace_function_filter_re(buf, len, &re_cnt);
2156 for (i = 0; i < re_cnt; i++) {
2157 ret = ftrace_function_set_regexp(data->ops, filter, *reset,
2158 re[i], strlen(re[i]));
2170 static int ftrace_function_check_pred(struct filter_pred *pred)
2172 struct ftrace_event_field *field = pred->field;
2175 * Check the predicate for function trace, verify:
2176 * - only '==' and '!=' is used
2177 * - the 'ip' field is used
2179 if ((pred->op != OP_EQ) && (pred->op != OP_NE))
2182 if (strcmp(field->name, "ip"))
2188 static int ftrace_function_set_filter_pred(struct filter_pred *pred,
2189 struct function_filter_data *data)
2193 /* Checking the node is valid for function trace. */
2194 ret = ftrace_function_check_pred(pred);
2198 return __ftrace_function_set_filter(pred->op == OP_EQ,
2199 pred->regex.pattern,
2204 static bool is_or(struct prog_entry *prog, int i)
2209 * Only "||" is allowed for function events, thus,
2210 * all true branches should jump to true, and any
2211 * false branch should jump to false.
2213 target = prog[i].target + 1;
2214 /* True and false have NULL preds (all prog entries should jump to one */
2215 if (prog[target].pred)
2218 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2219 return prog[i].when_to_branch == prog[target].target;
2222 static int ftrace_function_set_filter(struct perf_event *event,
2223 struct event_filter *filter)
2225 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2226 lockdep_is_held(&event_mutex));
2227 struct function_filter_data data = {
2230 .ops = &event->ftrace_ops,
2234 for (i = 0; prog[i].pred; i++) {
2235 struct filter_pred *pred = prog[i].pred;
2237 if (!is_or(prog, i))
2240 if (ftrace_function_set_filter_pred(pred, &data) < 0)
2246 static int ftrace_function_set_filter(struct perf_event *event,
2247 struct event_filter *filter)
2251 #endif /* CONFIG_FUNCTION_TRACER */
2253 int ftrace_profile_set_filter(struct perf_event *event, int event_id,
2257 struct event_filter *filter = NULL;
2258 struct trace_event_call *call;
2260 mutex_lock(&event_mutex);
2262 call = event->tp_event;
2272 err = create_filter(NULL, call, filter_str, false, &filter);
2276 if (ftrace_event_is_function(call))
2277 err = ftrace_function_set_filter(event, filter);
2279 event->filter = filter;
2282 if (err || ftrace_event_is_function(call))
2283 __free_filter(filter);
2286 mutex_unlock(&event_mutex);
2291 #endif /* CONFIG_PERF_EVENTS */
2293 #ifdef CONFIG_FTRACE_STARTUP_TEST
2295 #include <linux/types.h>
2296 #include <linux/tracepoint.h>
2298 #define CREATE_TRACE_POINTS
2299 #include "trace_events_filter_test.h"
2301 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2304 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2305 .e = ve, .f = vf, .g = vg, .h = vh }, \
2307 .not_visited = nvisit, \
2312 static struct test_filter_data_t {
2314 struct trace_event_raw_ftrace_test_filter rec;
2317 } test_filter_data[] = {
2318 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2319 "e == 1 && f == 1 && g == 1 && h == 1"
2320 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2321 DATA_REC(NO, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2322 DATA_REC(NO, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2324 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2325 "e == 1 || f == 1 || g == 1 || h == 1"
2326 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2327 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2328 DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2330 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2331 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2332 DATA_REC(NO, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2333 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2334 DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2335 DATA_REC(NO, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2337 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2338 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2339 DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2340 DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2341 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2343 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2344 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2345 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2346 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2347 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2349 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2350 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2351 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2352 DATA_REC(NO, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2353 DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2355 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2356 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2357 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2358 DATA_REC(NO, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2359 DATA_REC(NO, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2361 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2362 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2363 DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2364 DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2365 DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2373 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2375 static int test_pred_visited;
2377 static int test_pred_visited_fn(struct filter_pred *pred, void *event)
2379 struct ftrace_event_field *field = pred->field;
2381 test_pred_visited = 1;
2382 printk(KERN_INFO "\npred visited %s\n", field->name);
2386 static void update_pred_fn(struct event_filter *filter, char *fields)
2388 struct prog_entry *prog = rcu_dereference_protected(filter->prog,
2389 lockdep_is_held(&event_mutex));
2392 for (i = 0; prog[i].pred; i++) {
2393 struct filter_pred *pred = prog[i].pred;
2394 struct ftrace_event_field *field = pred->field;
2396 WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP);
2399 WARN_ONCE(1, "all leafs should have field defined %d", i);
2403 if (!strchr(fields, *field->name))
2406 pred->fn_num = FILTER_PRED_TEST_VISITED;
2410 static __init int ftrace_test_event_filter(void)
2414 printk(KERN_INFO "Testing ftrace filter: ");
2416 for (i = 0; i < DATA_CNT; i++) {
2417 struct event_filter *filter = NULL;
2418 struct test_filter_data_t *d = &test_filter_data[i];
2421 err = create_filter(NULL, &event_ftrace_test_filter,
2422 d->filter, false, &filter);
2425 "Failed to get filter for '%s', err %d\n",
2427 __free_filter(filter);
2431 /* Needed to dereference filter->prog */
2432 mutex_lock(&event_mutex);
2434 * The preemption disabling is not really needed for self
2435 * tests, but the rcu dereference will complain without it.
2438 if (*d->not_visited)
2439 update_pred_fn(filter, d->not_visited);
2441 test_pred_visited = 0;
2442 err = filter_match_preds(filter, &d->rec);
2445 mutex_unlock(&event_mutex);
2447 __free_filter(filter);
2449 if (test_pred_visited) {
2451 "Failed, unwanted pred visited for filter %s\n",
2456 if (err != d->match) {
2458 "Failed to match filter '%s', expected %d\n",
2459 d->filter, d->match);
2465 printk(KERN_CONT "OK\n");
2470 late_initcall(ftrace_test_event_filter);
2472 #endif /* CONFIG_FTRACE_STARTUP_TEST */