1 // SPDX-License-Identifier: GPL-2.0
3 * trace_events_filter - generic event filtering
5 * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com>
8 #include <linux/module.h>
9 #include <linux/ctype.h>
10 #include <linux/mutex.h>
11 #include <linux/perf_event.h>
12 #include <linux/slab.h>
15 #include "trace_output.h"
17 #define DEFAULT_SYS_FILTER_MESSAGE \
18 "### global filter ###\n" \
19 "# Use this to set filters for multiple events.\n" \
20 "# Only events with the given fields will be affected.\n" \
21 "# If no events are modified, an error message will be displayed here"
23 /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */
38 enum filter_op_ids
{ OPS
};
43 static const char * ops
[] = { OPS
};
46 * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND
47 * pred_funcs_##type below must match the order of them above.
49 #define PRED_FUNC_START OP_LE
50 #define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
53 C(NONE, "No error"), \
54 C(INVALID_OP, "Invalid operator"), \
55 C(TOO_MANY_OPEN, "Too many '('"), \
56 C(TOO_MANY_CLOSE, "Too few '('"), \
57 C(MISSING_QUOTE, "Missing matching quote"), \
58 C(OPERAND_TOO_LONG, "Operand too long"), \
59 C(EXPECT_STRING, "Expecting string field"), \
60 C(EXPECT_DIGIT, "Expecting numeric field"), \
61 C(ILLEGAL_FIELD_OP, "Illegal operation for field type"), \
62 C(FIELD_NOT_FOUND, "Field not found"), \
63 C(ILLEGAL_INTVAL, "Illegal integer value"), \
64 C(BAD_SUBSYS_FILTER, "Couldn't find or set field in one of a subsystem's events"), \
65 C(TOO_MANY_PREDS, "Too many terms in predicate expression"), \
66 C(INVALID_FILTER, "Meaningless filter expression"), \
67 C(IP_FIELD_ONLY, "Only 'ip' field is supported for function trace"), \
68 C(INVALID_VALUE, "Invalid value (did you forget quotes)?"), \
69 C(NO_FILTER, "No filter found"),
72 #define C(a, b) FILT_ERR_##a
79 static char *err_text
[] = { ERRORS
};
81 /* Called after a '!' character but "!=" and "!~" are not "not"s */
82 static bool is_not(const char *str
)
93 * prog_entry - a singe entry in the filter program
94 * @target: Index to jump to on a branch (actually one minus the index)
95 * @when_to_branch: The value of the result of the predicate to do a branch
96 * @pred: The predicate to execute.
101 struct filter_pred
*pred
;
105 * update_preds- assign a program entry a label target
106 * @prog: The program array
107 * @N: The index of the current entry in @prog
108 * @when_to_branch: What to assign a program entry for its branch condition
110 * The program entry at @N has a target that points to the index of a program
111 * entry that can have its target and when_to_branch fields updated.
112 * Update the current program entry denoted by index @N target field to be
113 * that of the updated entry. This will denote the entry to update if
114 * we are processing an "||" after an "&&"
116 static void update_preds(struct prog_entry
*prog
, int N
, int invert
)
122 prog
[t
].when_to_branch
= invert
;
127 struct filter_parse_error
{
132 static void parse_error(struct filter_parse_error
*pe
, int err
, int pos
)
135 pe
->lasterr_pos
= pos
;
138 typedef int (*parse_pred_fn
)(const char *str
, void *data
, int pos
,
139 struct filter_parse_error
*pe
,
140 struct filter_pred
**pred
);
149 * Without going into a formal proof, this explains the method that is used in
150 * parsing the logical expressions.
152 * For example, if we have: "a && !(!b || (c && g)) || d || e && !f"
153 * The first pass will convert it into the following program:
155 * n1: r=a; l1: if (!r) goto l4;
156 * n2: r=b; l2: if (!r) goto l4;
157 * n3: r=c; r=!r; l3: if (r) goto l4;
158 * n4: r=g; r=!r; l4: if (r) goto l5;
159 * n5: r=d; l5: if (r) goto T
160 * n6: r=e; l6: if (!r) goto l7;
161 * n7: r=f; r=!r; l7: if (!r) goto F
165 * To do this, we use a data structure to represent each of the above
166 * predicate and conditions that has:
168 * predicate, when_to_branch, invert, target
170 * The "predicate" will hold the function to determine the result "r".
171 * The "when_to_branch" denotes what "r" should be if a branch is to be taken
172 * "&&" would contain "!r" or (0) and "||" would contain "r" or (1).
173 * The "invert" holds whether the value should be reversed before testing.
174 * The "target" contains the label "l#" to jump to.
176 * A stack is created to hold values when parentheses are used.
178 * To simplify the logic, the labels will start at 0 and not 1.
180 * The possible invert values are 1 and 0. The number of "!"s that are in scope
181 * before the predicate determines the invert value, if the number is odd then
182 * the invert value is 1 and 0 otherwise. This means the invert value only
183 * needs to be toggled when a new "!" is introduced compared to what is stored
184 * on the stack, where parentheses were used.
186 * The top of the stack and "invert" are initialized to zero.
190 * #1 A loop through all the tokens is done:
192 * #2 If the token is an "(", the stack is push, and the current stack value
193 * gets the current invert value, and the loop continues to the next token.
194 * The top of the stack saves the "invert" value to keep track of what
195 * the current inversion is. As "!(a && !b || c)" would require all
196 * predicates being affected separately by the "!" before the parentheses.
197 * And that would end up being equivalent to "(!a || b) && !c"
199 * #3 If the token is an "!", the current "invert" value gets inverted, and
200 * the loop continues. Note, if the next token is a predicate, then
201 * this "invert" value is only valid for the current program entry,
202 * and does not affect other predicates later on.
204 * The only other acceptable token is the predicate string.
206 * #4 A new entry into the program is added saving: the predicate and the
207 * current value of "invert". The target is currently assigned to the
208 * previous program index (this will not be its final value).
210 * #5 We now enter another loop and look at the next token. The only valid
211 * tokens are ")", "&&", "||" or end of the input string "\0".
213 * #6 The invert variable is reset to the current value saved on the top of
216 * #7 The top of the stack holds not only the current invert value, but also
217 * if a "&&" or "||" needs to be processed. Note, the "&&" takes higher
218 * precedence than "||". That is "a && b || c && d" is equivalent to
219 * "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs
220 * to be processed. This is the case if an "&&" was the last token. If it was
221 * then we call update_preds(). This takes the program, the current index in
222 * the program, and the current value of "invert". More will be described
223 * below about this function.
225 * #8 If the next token is "&&" then we set a flag in the top of the stack
226 * that denotes that "&&" needs to be processed, break out of this loop
227 * and continue with the outer loop.
229 * #9 Otherwise, if a "||" needs to be processed then update_preds() is called.
230 * This is called with the program, the current index in the program, but
231 * this time with an inverted value of "invert" (that is !invert). This is
232 * because the value taken will become the "when_to_branch" value of the
234 * Note, this is called when the next token is not an "&&". As stated before,
235 * "&&" takes higher precedence, and "||" should not be processed yet if the
236 * next logical operation is "&&".
238 * #10 If the next token is "||" then we set a flag in the top of the stack
239 * that denotes that "||" needs to be processed, break out of this loop
240 * and continue with the outer loop.
242 * #11 If this is the end of the input string "\0" then we break out of both
245 * #12 Otherwise, the next token is ")", where we pop the stack and continue
248 * Now to discuss the update_pred() function, as that is key to the setting up
249 * of the program. Remember the "target" of the program is initialized to the
250 * previous index and not the "l" label. The target holds the index into the
251 * program that gets affected by the operand. Thus if we have something like
252 * "a || b && c", when we process "a" the target will be "-1" (undefined).
253 * When we process "b", its target is "0", which is the index of "a", as that's
254 * the predicate that is affected by "||". But because the next token after "b"
255 * is "&&" we don't call update_preds(). Instead continue to "c". As the
256 * next token after "c" is not "&&" but the end of input, we first process the
257 * "&&" by calling update_preds() for the "&&" then we process the "||" by
258 * callin updates_preds() with the values for processing "||".
260 * What does that mean? What update_preds() does is to first save the "target"
261 * of the program entry indexed by the current program entry's "target"
262 * (remember the "target" is initialized to previous program entry), and then
263 * sets that "target" to the current index which represents the label "l#".
264 * That entry's "when_to_branch" is set to the value passed in (the "invert"
265 * or "!invert"). Then it sets the current program entry's target to the saved
266 * "target" value (the old value of the program that had its "target" updated
269 * Looking back at "a || b && c", we have the following steps:
270 * "a" - prog[0] = { "a", X, -1 } // pred, when_to_branch, target
271 * "||" - flag that we need to process "||"; continue outer loop
272 * "b" - prog[1] = { "b", X, 0 }
273 * "&&" - flag that we need to process "&&"; continue outer loop
274 * (Notice we did not process "||")
275 * "c" - prog[2] = { "c", X, 1 }
276 * update_preds(prog, 2, 0); // invert = 0 as we are processing "&&"
277 * t = prog[2].target; // t = 1
278 * s = prog[t].target; // s = 0
279 * prog[t].target = 2; // Set target to "l2"
280 * prog[t].when_to_branch = 0;
281 * prog[2].target = s;
282 * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||"
283 * t = prog[2].target; // t = 0
284 * s = prog[t].target; // s = -1
285 * prog[t].target = 2; // Set target to "l2"
286 * prog[t].when_to_branch = 1;
287 * prog[2].target = s;
289 * #13 Which brings us to the final step of the first pass, which is to set
290 * the last program entry's when_to_branch and target, which will be
291 * when_to_branch = 0; target = N; ( the label after the program entry after
292 * the last program entry processed above).
294 * If we denote "TRUE" to be the entry after the last program entry processed,
295 * and "FALSE" the program entry after that, we are now done with the first
298 * Making the above "a || b && c" have a progam of:
299 * prog[0] = { "a", 1, 2 }
300 * prog[1] = { "b", 0, 2 }
301 * prog[2] = { "c", 0, 3 }
303 * Which translates into:
304 * n0: r = a; l0: if (r) goto l2;
305 * n1: r = b; l1: if (!r) goto l2;
306 * n2: r = c; l2: if (!r) goto l3; // Which is the same as "goto F;"
307 * T: return TRUE; l3:
310 * Although, after the first pass, the program is correct, it is
311 * inefficient. The simple sample of "a || b && c" could be easily been
313 * n0: r = a; if (r) goto T
314 * n1: r = b; if (!r) goto F
315 * n2: r = c; if (!r) goto F
319 * The First Pass is over the input string. The next too passes are over
320 * the program itself.
324 * Which brings us to the second pass. If a jump to a label has the
325 * same condition as that label, it can instead jump to its target.
326 * The original example of "a && !(!b || (c && g)) || d || e && !f"
327 * where the first pass gives us:
329 * n1: r=a; l1: if (!r) goto l4;
330 * n2: r=b; l2: if (!r) goto l4;
331 * n3: r=c; r=!r; l3: if (r) goto l4;
332 * n4: r=g; r=!r; l4: if (r) goto l5;
333 * n5: r=d; l5: if (r) goto T
334 * n6: r=e; l6: if (!r) goto l7;
335 * n7: r=f; r=!r; l7: if (!r) goto F:
339 * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;".
340 * And "l5: if (r) goto T", we could optimize this by converting l3 and l4
341 * to go directly to T. To accomplish this, we start from the last
342 * entry in the program and work our way back. If the target of the entry
343 * has the same "when_to_branch" then we could use that entry's target.
344 * Doing this, the above would end up as:
346 * n1: r=a; l1: if (!r) goto l4;
347 * n2: r=b; l2: if (!r) goto l4;
348 * n3: r=c; r=!r; l3: if (r) goto T;
349 * n4: r=g; r=!r; l4: if (r) goto T;
350 * n5: r=d; l5: if (r) goto T;
351 * n6: r=e; l6: if (!r) goto F;
352 * n7: r=f; r=!r; l7: if (!r) goto F;
356 * In that same pass, if the "when_to_branch" doesn't match, we can simply
357 * go to the program entry after the label. That is, "l2: if (!r) goto l4;"
358 * where "l4: if (r) goto T;", then we can convert l2 to be:
359 * "l2: if (!r) goto n5;".
361 * This will have the second pass give us:
362 * n1: r=a; l1: if (!r) goto n5;
363 * n2: r=b; l2: if (!r) goto n5;
364 * n3: r=c; r=!r; l3: if (r) goto T;
365 * n4: r=g; r=!r; l4: if (r) goto T;
366 * n5: r=d; l5: if (r) goto T
367 * n6: r=e; l6: if (!r) goto F;
368 * n7: r=f; r=!r; l7: if (!r) goto F
372 * Notice, all the "l#" labels are no longer used, and they can now
377 * For the third pass we deal with the inverts. As they simply just
378 * make the "when_to_branch" get inverted, a simple loop over the
379 * program to that does: "when_to_branch ^= invert;" will do the
380 * job, leaving us with:
381 * n1: r=a; if (!r) goto n5;
382 * n2: r=b; if (!r) goto n5;
383 * n3: r=c: if (!r) goto T;
384 * n4: r=g; if (!r) goto T;
385 * n5: r=d; if (r) goto T
386 * n6: r=e; if (!r) goto F;
387 * n7: r=f; if (r) goto F
391 * As "r = a; if (!r) goto n5;" is obviously the same as
392 * "if (!a) goto n5;" without doing anything we can interperate the
394 * n1: if (!a) goto n5;
395 * n2: if (!b) goto n5;
396 * n3: if (!c) goto T;
397 * n4: if (!g) goto T;
399 * n6: if (!e) goto F;
404 * Since the inverts are discarded at the end, there's no reason to store
405 * them in the program array (and waste memory). A separate array to hold
406 * the inverts is used and freed at the end.
408 static struct prog_entry
*
409 predicate_parse(const char *str
, int nr_parens
, int nr_preds
,
410 parse_pred_fn parse_pred
, void *data
,
411 struct filter_parse_error
*pe
)
413 struct prog_entry
*prog_stack
;
414 struct prog_entry
*prog
;
415 const char *ptr
= str
;
416 char *inverts
= NULL
;
425 nr_preds
+= 2; /* For TRUE and FALSE */
427 op_stack
= kmalloc_array(nr_parens
, sizeof(*op_stack
), GFP_KERNEL
);
429 return ERR_PTR(-ENOMEM
);
430 prog_stack
= kmalloc_array(nr_preds
, sizeof(*prog_stack
), GFP_KERNEL
);
432 parse_error(pe
, -ENOMEM
, 0);
435 inverts
= kmalloc_array(nr_preds
, sizeof(*inverts
), GFP_KERNEL
);
437 parse_error(pe
, -ENOMEM
, 0);
446 while (*ptr
) { /* #1 */
447 const char *next
= ptr
++;
454 if (top
- op_stack
> nr_parens
)
455 return ERR_PTR(-EINVAL
);
466 parse_error(pe
, FILT_ERR_TOO_MANY_PREDS
, next
- str
);
470 inverts
[N
] = invert
; /* #4 */
471 prog
[N
].target
= N
-1;
473 len
= parse_pred(next
, data
, ptr
- str
, pe
, &prog
[N
].pred
);
494 /* accepting only "&&" or "||" */
495 if (next
[1] == next
[0]) {
501 parse_error(pe
, FILT_ERR_TOO_MANY_PREDS
,
506 invert
= *top
& INVERT
;
508 if (*top
& PROCESS_AND
) { /* #7 */
509 update_preds(prog
, N
- 1, invert
);
510 *top
&= ~PROCESS_AND
;
512 if (*next
== '&') { /* #8 */
516 if (*top
& PROCESS_OR
) { /* #9 */
517 update_preds(prog
, N
- 1, !invert
);
520 if (*next
== '|') { /* #10 */
524 if (!*next
) /* #11 */
527 if (top
== op_stack
) {
530 parse_error(pe
, FILT_ERR_TOO_MANY_CLOSE
, ptr
- str
);
537 if (top
!= op_stack
) {
539 parse_error(pe
, FILT_ERR_TOO_MANY_OPEN
, ptr
- str
);
546 parse_error(pe
, FILT_ERR_NO_FILTER
, ptr
- str
);
550 prog
[N
].pred
= NULL
; /* #13 */
551 prog
[N
].target
= 1; /* TRUE */
552 prog
[N
+1].pred
= NULL
;
553 prog
[N
+1].target
= 0; /* FALSE */
554 prog
[N
-1].target
= N
;
555 prog
[N
-1].when_to_branch
= false;
558 for (i
= N
-1 ; i
--; ) {
559 int target
= prog
[i
].target
;
560 if (prog
[i
].when_to_branch
== prog
[target
].when_to_branch
)
561 prog
[i
].target
= prog
[target
].target
;
565 for (i
= 0; i
< N
; i
++) {
566 invert
= inverts
[i
] ^ prog
[i
].when_to_branch
;
567 prog
[i
].when_to_branch
= invert
;
568 /* Make sure the program always moves forward */
569 if (WARN_ON(prog
[i
].target
<= i
)) {
585 #define DEFINE_COMPARISON_PRED(type) \
586 static int filter_pred_LT_##type(struct filter_pred *pred, void *event) \
588 type *addr = (type *)(event + pred->offset); \
589 type val = (type)pred->val; \
590 return *addr < val; \
592 static int filter_pred_LE_##type(struct filter_pred *pred, void *event) \
594 type *addr = (type *)(event + pred->offset); \
595 type val = (type)pred->val; \
596 return *addr <= val; \
598 static int filter_pred_GT_##type(struct filter_pred *pred, void *event) \
600 type *addr = (type *)(event + pred->offset); \
601 type val = (type)pred->val; \
602 return *addr > val; \
604 static int filter_pred_GE_##type(struct filter_pred *pred, void *event) \
606 type *addr = (type *)(event + pred->offset); \
607 type val = (type)pred->val; \
608 return *addr >= val; \
610 static int filter_pred_BAND_##type(struct filter_pred *pred, void *event) \
612 type *addr = (type *)(event + pred->offset); \
613 type val = (type)pred->val; \
614 return !!(*addr & val); \
616 static const filter_pred_fn_t pred_funcs_##type[] = { \
617 filter_pred_LE_##type, \
618 filter_pred_LT_##type, \
619 filter_pred_GE_##type, \
620 filter_pred_GT_##type, \
621 filter_pred_BAND_##type, \
624 #define DEFINE_EQUALITY_PRED(size) \
625 static int filter_pred_##size(struct filter_pred *pred, void *event) \
627 u##size *addr = (u##size *)(event + pred->offset); \
628 u##size val = (u##size)pred->val; \
631 match = (val == *addr) ^ pred->not; \
636 DEFINE_COMPARISON_PRED(s64
);
637 DEFINE_COMPARISON_PRED(u64
);
638 DEFINE_COMPARISON_PRED(s32
);
639 DEFINE_COMPARISON_PRED(u32
);
640 DEFINE_COMPARISON_PRED(s16
);
641 DEFINE_COMPARISON_PRED(u16
);
642 DEFINE_COMPARISON_PRED(s8
);
643 DEFINE_COMPARISON_PRED(u8
);
645 DEFINE_EQUALITY_PRED(64);
646 DEFINE_EQUALITY_PRED(32);
647 DEFINE_EQUALITY_PRED(16);
648 DEFINE_EQUALITY_PRED(8);
650 /* Filter predicate for fixed sized arrays of characters */
651 static int filter_pred_string(struct filter_pred
*pred
, void *event
)
653 char *addr
= (char *)(event
+ pred
->offset
);
656 cmp
= pred
->regex
.match(addr
, &pred
->regex
, pred
->regex
.field_len
);
658 match
= cmp
^ pred
->not;
663 /* Filter predicate for char * pointers */
664 static int filter_pred_pchar(struct filter_pred
*pred
, void *event
)
666 char **addr
= (char **)(event
+ pred
->offset
);
668 int len
= strlen(*addr
) + 1; /* including tailing '\0' */
670 cmp
= pred
->regex
.match(*addr
, &pred
->regex
, len
);
672 match
= cmp
^ pred
->not;
678 * Filter predicate for dynamic sized arrays of characters.
679 * These are implemented through a list of strings at the end
681 * Also each of these strings have a field in the entry which
682 * contains its offset from the beginning of the entry.
683 * We have then first to get this field, dereference it
684 * and add it to the address of the entry, and at last we have
685 * the address of the string.
687 static int filter_pred_strloc(struct filter_pred
*pred
, void *event
)
689 u32 str_item
= *(u32
*)(event
+ pred
->offset
);
690 int str_loc
= str_item
& 0xffff;
691 int str_len
= str_item
>> 16;
692 char *addr
= (char *)(event
+ str_loc
);
695 cmp
= pred
->regex
.match(addr
, &pred
->regex
, str_len
);
697 match
= cmp
^ pred
->not;
702 /* Filter predicate for CPUs. */
703 static int filter_pred_cpu(struct filter_pred
*pred
, void *event
)
707 cpu
= raw_smp_processor_id();
728 /* Filter predicate for COMM. */
729 static int filter_pred_comm(struct filter_pred
*pred
, void *event
)
733 cmp
= pred
->regex
.match(current
->comm
, &pred
->regex
,
735 return cmp
^ pred
->not;
738 static int filter_pred_none(struct filter_pred
*pred
, void *event
)
744 * regex_match_foo - Basic regex callbacks
746 * @str: the string to be searched
747 * @r: the regex structure containing the pattern string
748 * @len: the length of the string to be searched (including '\0')
751 * - @str might not be NULL-terminated if it's of type DYN_STRING
752 * or STATIC_STRING, unless @len is zero.
755 static int regex_match_full(char *str
, struct regex
*r
, int len
)
757 /* len of zero means str is dynamic and ends with '\0' */
759 return strcmp(str
, r
->pattern
) == 0;
761 return strncmp(str
, r
->pattern
, len
) == 0;
764 static int regex_match_front(char *str
, struct regex
*r
, int len
)
766 if (len
&& len
< r
->len
)
769 return strncmp(str
, r
->pattern
, r
->len
) == 0;
772 static int regex_match_middle(char *str
, struct regex
*r
, int len
)
775 return strstr(str
, r
->pattern
) != NULL
;
777 return strnstr(str
, r
->pattern
, len
) != NULL
;
780 static int regex_match_end(char *str
, struct regex
*r
, int len
)
782 int strlen
= len
- 1;
784 if (strlen
>= r
->len
&&
785 memcmp(str
+ strlen
- r
->len
, r
->pattern
, r
->len
) == 0)
790 static int regex_match_glob(char *str
, struct regex
*r
, int len __maybe_unused
)
792 if (glob_match(r
->pattern
, str
))
798 * filter_parse_regex - parse a basic regex
799 * @buff: the raw regex
800 * @len: length of the regex
801 * @search: will point to the beginning of the string to compare
802 * @not: tell whether the match will have to be inverted
804 * This passes in a buffer containing a regex and this function will
805 * set search to point to the search part of the buffer and
806 * return the type of search it is (see enum above).
807 * This does modify buff.
810 * search returns the pointer to use for comparison.
811 * not returns 1 if buff started with a '!'
814 enum regex_type
filter_parse_regex(char *buff
, int len
, char **search
, int *not)
816 int type
= MATCH_FULL
;
819 if (buff
[0] == '!') {
828 if (isdigit(buff
[0]))
831 for (i
= 0; i
< len
; i
++) {
832 if (buff
[i
] == '*') {
834 type
= MATCH_END_ONLY
;
835 } else if (i
== len
- 1) {
836 if (type
== MATCH_END_ONLY
)
837 type
= MATCH_MIDDLE_ONLY
;
839 type
= MATCH_FRONT_ONLY
;
842 } else { /* pattern continues, use full glob */
845 } else if (strchr("[?\\", buff
[i
])) {
855 static void filter_build_regex(struct filter_pred
*pred
)
857 struct regex
*r
= &pred
->regex
;
859 enum regex_type type
= MATCH_FULL
;
861 if (pred
->op
== OP_GLOB
) {
862 type
= filter_parse_regex(r
->pattern
, r
->len
, &search
, &pred
->not);
863 r
->len
= strlen(search
);
864 memmove(r
->pattern
, search
, r
->len
+1);
868 /* MATCH_INDEX should not happen, but if it does, match full */
871 r
->match
= regex_match_full
;
873 case MATCH_FRONT_ONLY
:
874 r
->match
= regex_match_front
;
876 case MATCH_MIDDLE_ONLY
:
877 r
->match
= regex_match_middle
;
880 r
->match
= regex_match_end
;
883 r
->match
= regex_match_glob
;
888 /* return 1 if event matches, 0 otherwise (discard) */
889 int filter_match_preds(struct event_filter
*filter
, void *rec
)
891 struct prog_entry
*prog
;
894 /* no filter is considered a match */
898 /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */
899 prog
= rcu_dereference_raw(filter
->prog
);
903 for (i
= 0; prog
[i
].pred
; i
++) {
904 struct filter_pred
*pred
= prog
[i
].pred
;
905 int match
= pred
->fn(pred
, rec
);
906 if (match
== prog
[i
].when_to_branch
)
909 return prog
[i
].target
;
911 EXPORT_SYMBOL_GPL(filter_match_preds
);
913 static void remove_filter_string(struct event_filter
*filter
)
918 kfree(filter
->filter_string
);
919 filter
->filter_string
= NULL
;
922 static void append_filter_err(struct filter_parse_error
*pe
,
923 struct event_filter
*filter
)
926 int pos
= pe
->lasterr_pos
;
930 if (WARN_ON(!filter
->filter_string
))
933 s
= kmalloc(sizeof(*s
), GFP_KERNEL
);
938 len
= strlen(filter
->filter_string
);
942 /* indexing is off by one */
946 trace_seq_puts(s
, filter
->filter_string
);
947 if (pe
->lasterr
> 0) {
948 trace_seq_printf(s
, "\n%*s", pos
, "^");
949 trace_seq_printf(s
, "\nparse_error: %s\n", err_text
[pe
->lasterr
]);
951 trace_seq_printf(s
, "\nError: (%d)\n", pe
->lasterr
);
953 trace_seq_putc(s
, 0);
954 buf
= kmemdup_nul(s
->buffer
, s
->seq
.len
, GFP_KERNEL
);
956 kfree(filter
->filter_string
);
957 filter
->filter_string
= buf
;
962 static inline struct event_filter
*event_filter(struct trace_event_file
*file
)
967 /* caller must hold event_mutex */
968 void print_event_filter(struct trace_event_file
*file
, struct trace_seq
*s
)
970 struct event_filter
*filter
= event_filter(file
);
972 if (filter
&& filter
->filter_string
)
973 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
975 trace_seq_puts(s
, "none\n");
978 void print_subsystem_event_filter(struct event_subsystem
*system
,
981 struct event_filter
*filter
;
983 mutex_lock(&event_mutex
);
984 filter
= system
->filter
;
985 if (filter
&& filter
->filter_string
)
986 trace_seq_printf(s
, "%s\n", filter
->filter_string
);
988 trace_seq_puts(s
, DEFAULT_SYS_FILTER_MESSAGE
"\n");
989 mutex_unlock(&event_mutex
);
992 static void free_prog(struct event_filter
*filter
)
994 struct prog_entry
*prog
;
997 prog
= rcu_access_pointer(filter
->prog
);
1001 for (i
= 0; prog
[i
].pred
; i
++)
1002 kfree(prog
[i
].pred
);
1006 static void filter_disable(struct trace_event_file
*file
)
1008 unsigned long old_flags
= file
->flags
;
1010 file
->flags
&= ~EVENT_FILE_FL_FILTERED
;
1012 if (old_flags
!= file
->flags
)
1013 trace_buffered_event_disable();
1016 static void __free_filter(struct event_filter
*filter
)
1022 kfree(filter
->filter_string
);
1026 void free_event_filter(struct event_filter
*filter
)
1028 __free_filter(filter
);
1031 static inline void __remove_filter(struct trace_event_file
*file
)
1033 filter_disable(file
);
1034 remove_filter_string(file
->filter
);
1037 static void filter_free_subsystem_preds(struct trace_subsystem_dir
*dir
,
1038 struct trace_array
*tr
)
1040 struct trace_event_file
*file
;
1042 list_for_each_entry(file
, &tr
->events
, list
) {
1043 if (file
->system
!= dir
)
1045 __remove_filter(file
);
1049 static inline void __free_subsystem_filter(struct trace_event_file
*file
)
1051 __free_filter(file
->filter
);
1052 file
->filter
= NULL
;
1055 static void filter_free_subsystem_filters(struct trace_subsystem_dir
*dir
,
1056 struct trace_array
*tr
)
1058 struct trace_event_file
*file
;
1060 list_for_each_entry(file
, &tr
->events
, list
) {
1061 if (file
->system
!= dir
)
1063 __free_subsystem_filter(file
);
1067 int filter_assign_type(const char *type
)
1069 if (strstr(type
, "__data_loc") && strstr(type
, "char"))
1070 return FILTER_DYN_STRING
;
1072 if (strchr(type
, '[') && strstr(type
, "char"))
1073 return FILTER_STATIC_STRING
;
1075 return FILTER_OTHER
;
1078 static filter_pred_fn_t
select_comparison_fn(enum filter_op_ids op
,
1079 int field_size
, int field_is_signed
)
1081 filter_pred_fn_t fn
= NULL
;
1082 int pred_func_index
= -1;
1089 if (WARN_ON_ONCE(op
< PRED_FUNC_START
))
1091 pred_func_index
= op
- PRED_FUNC_START
;
1092 if (WARN_ON_ONCE(pred_func_index
> PRED_FUNC_MAX
))
1096 switch (field_size
) {
1098 if (pred_func_index
< 0)
1099 fn
= filter_pred_64
;
1100 else if (field_is_signed
)
1101 fn
= pred_funcs_s64
[pred_func_index
];
1103 fn
= pred_funcs_u64
[pred_func_index
];
1106 if (pred_func_index
< 0)
1107 fn
= filter_pred_32
;
1108 else if (field_is_signed
)
1109 fn
= pred_funcs_s32
[pred_func_index
];
1111 fn
= pred_funcs_u32
[pred_func_index
];
1114 if (pred_func_index
< 0)
1115 fn
= filter_pred_16
;
1116 else if (field_is_signed
)
1117 fn
= pred_funcs_s16
[pred_func_index
];
1119 fn
= pred_funcs_u16
[pred_func_index
];
1122 if (pred_func_index
< 0)
1124 else if (field_is_signed
)
1125 fn
= pred_funcs_s8
[pred_func_index
];
1127 fn
= pred_funcs_u8
[pred_func_index
];
1134 /* Called when a predicate is encountered by predicate_parse() */
1135 static int parse_pred(const char *str
, void *data
,
1136 int pos
, struct filter_parse_error
*pe
,
1137 struct filter_pred
**pred_ptr
)
1139 struct trace_event_call
*call
= data
;
1140 struct ftrace_event_field
*field
;
1141 struct filter_pred
*pred
= NULL
;
1142 char num_buf
[24]; /* Big enough to hold an address */
1152 /* First find the field to associate to */
1153 while (isspace(str
[i
]))
1157 while (isalnum(str
[i
]) || str
[i
] == '_')
1165 field_name
= kmemdup_nul(str
+ s
, len
, GFP_KERNEL
);
1169 /* Make sure that the field exists */
1171 field
= trace_find_event_field(call
, field_name
);
1174 parse_error(pe
, FILT_ERR_FIELD_NOT_FOUND
, pos
+ i
);
1178 while (isspace(str
[i
]))
1181 /* Make sure this op is supported */
1182 for (op
= 0; ops
[op
]; op
++) {
1183 /* This is why '<=' must come before '<' in ops[] */
1184 if (strncmp(str
+ i
, ops
[op
], strlen(ops
[op
])) == 0)
1189 parse_error(pe
, FILT_ERR_INVALID_OP
, pos
+ i
);
1193 i
+= strlen(ops
[op
]);
1195 while (isspace(str
[i
]))
1200 pred
= kzalloc(sizeof(*pred
), GFP_KERNEL
);
1204 pred
->field
= field
;
1205 pred
->offset
= field
->offset
;
1208 if (ftrace_event_is_function(call
)) {
1210 * Perf does things different with function events.
1211 * It only allows an "ip" field, and expects a string.
1212 * But the string does not need to be surrounded by quotes.
1213 * If it is a string, the assigned function as a nop,
1214 * (perf doesn't use it) and grab everything.
1216 if (strcmp(field
->name
, "ip") != 0) {
1217 parse_error(pe
, FILT_ERR_IP_FIELD_ONLY
, pos
+ i
);
1220 pred
->fn
= filter_pred_none
;
1223 * Quotes are not required, but if they exist then we need
1224 * to read them till we hit a matching one.
1226 if (str
[i
] == '\'' || str
[i
] == '"')
1231 for (i
++; str
[i
]; i
++) {
1232 if (q
&& str
[i
] == q
)
1234 if (!q
&& (str
[i
] == ')' || str
[i
] == '&' ||
1242 if (len
>= MAX_FILTER_STR_VAL
) {
1243 parse_error(pe
, FILT_ERR_OPERAND_TOO_LONG
, pos
+ i
);
1247 pred
->regex
.len
= len
;
1248 strncpy(pred
->regex
.pattern
, str
+ s
, len
);
1249 pred
->regex
.pattern
[len
] = 0;
1251 /* This is either a string, or an integer */
1252 } else if (str
[i
] == '\'' || str
[i
] == '"') {
1255 /* Make sure the op is OK for strings */
1264 parse_error(pe
, FILT_ERR_ILLEGAL_FIELD_OP
, pos
+ i
);
1268 /* Make sure the field is OK for strings */
1269 if (!is_string_field(field
)) {
1270 parse_error(pe
, FILT_ERR_EXPECT_DIGIT
, pos
+ i
);
1274 for (i
++; str
[i
]; i
++) {
1279 parse_error(pe
, FILT_ERR_MISSING_QUOTE
, pos
+ i
);
1286 if (len
>= MAX_FILTER_STR_VAL
) {
1287 parse_error(pe
, FILT_ERR_OPERAND_TOO_LONG
, pos
+ i
);
1291 pred
->regex
.len
= len
;
1292 strncpy(pred
->regex
.pattern
, str
+ s
, len
);
1293 pred
->regex
.pattern
[len
] = 0;
1295 filter_build_regex(pred
);
1297 if (field
->filter_type
== FILTER_COMM
) {
1298 pred
->fn
= filter_pred_comm
;
1300 } else if (field
->filter_type
== FILTER_STATIC_STRING
) {
1301 pred
->fn
= filter_pred_string
;
1302 pred
->regex
.field_len
= field
->size
;
1304 } else if (field
->filter_type
== FILTER_DYN_STRING
)
1305 pred
->fn
= filter_pred_strloc
;
1307 pred
->fn
= filter_pred_pchar
;
1308 /* go past the last quote */
1311 } else if (isdigit(str
[i
]) || str
[i
] == '-') {
1313 /* Make sure the field is not a string */
1314 if (is_string_field(field
)) {
1315 parse_error(pe
, FILT_ERR_EXPECT_STRING
, pos
+ i
);
1319 if (op
== OP_GLOB
) {
1320 parse_error(pe
, FILT_ERR_ILLEGAL_FIELD_OP
, pos
+ i
);
1327 /* We allow 0xDEADBEEF */
1328 while (isalnum(str
[i
]))
1332 /* 0xfeedfacedeadbeef is 18 chars max */
1333 if (len
>= sizeof(num_buf
)) {
1334 parse_error(pe
, FILT_ERR_OPERAND_TOO_LONG
, pos
+ i
);
1338 strncpy(num_buf
, str
+ s
, len
);
1341 /* Make sure it is a value */
1342 if (field
->is_signed
)
1343 ret
= kstrtoll(num_buf
, 0, &val
);
1345 ret
= kstrtoull(num_buf
, 0, &val
);
1347 parse_error(pe
, FILT_ERR_ILLEGAL_INTVAL
, pos
+ s
);
1353 if (field
->filter_type
== FILTER_CPU
)
1354 pred
->fn
= filter_pred_cpu
;
1356 pred
->fn
= select_comparison_fn(pred
->op
, field
->size
,
1358 if (pred
->op
== OP_NE
)
1363 parse_error(pe
, FILT_ERR_INVALID_VALUE
, pos
+ i
);
1376 TOO_MANY_CLOSE
= -1,
1382 * Read the filter string once to calculate the number of predicates
1383 * as well as how deep the parentheses go.
1386 * 0 - everything is fine (err is undefined)
1389 * -3 - No matching quote
1391 static int calc_stack(const char *str
, int *parens
, int *preds
, int *err
)
1393 bool is_pred
= false;
1395 int open
= 1; /* Count the expression as "(E)" */
1403 for (i
= 0; str
[i
]; i
++) {
1404 if (isspace(str
[i
]))
1407 if (str
[i
] == quote
)
1420 if (str
[i
+1] != str
[i
])
1427 if (open
> max_open
)
1434 return TOO_MANY_CLOSE
;
1447 return MISSING_QUOTE
;
1453 /* find the bad open */
1456 if (str
[i
] == quote
)
1462 if (level
== open
) {
1464 return TOO_MANY_OPEN
;
1477 /* First character is the '(' with missing ')' */
1479 return TOO_MANY_OPEN
;
1482 /* Set the size of the required stacks */
1488 static int process_preds(struct trace_event_call
*call
,
1489 const char *filter_string
,
1490 struct event_filter
*filter
,
1491 struct filter_parse_error
*pe
)
1493 struct prog_entry
*prog
;
1499 ret
= calc_stack(filter_string
, &nr_parens
, &nr_preds
, &index
);
1503 parse_error(pe
, FILT_ERR_MISSING_QUOTE
, index
);
1506 parse_error(pe
, FILT_ERR_TOO_MANY_OPEN
, index
);
1509 parse_error(pe
, FILT_ERR_TOO_MANY_CLOSE
, index
);
1517 prog
= predicate_parse(filter_string
, nr_parens
, nr_preds
,
1518 parse_pred
, call
, pe
);
1520 return PTR_ERR(prog
);
1522 rcu_assign_pointer(filter
->prog
, prog
);
1526 static inline void event_set_filtered_flag(struct trace_event_file
*file
)
1528 unsigned long old_flags
= file
->flags
;
1530 file
->flags
|= EVENT_FILE_FL_FILTERED
;
1532 if (old_flags
!= file
->flags
)
1533 trace_buffered_event_enable();
1536 static inline void event_set_filter(struct trace_event_file
*file
,
1537 struct event_filter
*filter
)
1539 rcu_assign_pointer(file
->filter
, filter
);
1542 static inline void event_clear_filter(struct trace_event_file
*file
)
1544 RCU_INIT_POINTER(file
->filter
, NULL
);
1548 event_set_no_set_filter_flag(struct trace_event_file
*file
)
1550 file
->flags
|= EVENT_FILE_FL_NO_SET_FILTER
;
1554 event_clear_no_set_filter_flag(struct trace_event_file
*file
)
1556 file
->flags
&= ~EVENT_FILE_FL_NO_SET_FILTER
;
1560 event_no_set_filter_flag(struct trace_event_file
*file
)
1562 if (file
->flags
& EVENT_FILE_FL_NO_SET_FILTER
)
1568 struct filter_list
{
1569 struct list_head list
;
1570 struct event_filter
*filter
;
1573 static int process_system_preds(struct trace_subsystem_dir
*dir
,
1574 struct trace_array
*tr
,
1575 struct filter_parse_error
*pe
,
1576 char *filter_string
)
1578 struct trace_event_file
*file
;
1579 struct filter_list
*filter_item
;
1580 struct event_filter
*filter
= NULL
;
1581 struct filter_list
*tmp
;
1582 LIST_HEAD(filter_list
);
1586 list_for_each_entry(file
, &tr
->events
, list
) {
1588 if (file
->system
!= dir
)
1591 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
1595 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
1596 if (!filter
->filter_string
)
1599 err
= process_preds(file
->event_call
, filter_string
, filter
, pe
);
1601 filter_disable(file
);
1602 parse_error(pe
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1603 append_filter_err(pe
, filter
);
1605 event_set_filtered_flag(file
);
1608 filter_item
= kzalloc(sizeof(*filter_item
), GFP_KERNEL
);
1612 list_add_tail(&filter_item
->list
, &filter_list
);
1614 * Regardless of if this returned an error, we still
1615 * replace the filter for the call.
1617 filter_item
->filter
= event_filter(file
);
1618 event_set_filter(file
, filter
);
1628 * The calls can still be using the old filters.
1629 * Do a synchronize_rcu() and to ensure all calls are
1630 * done with them before we free them.
1632 tracepoint_synchronize_unregister();
1633 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1634 __free_filter(filter_item
->filter
);
1635 list_del(&filter_item
->list
);
1640 /* No call succeeded */
1641 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1642 list_del(&filter_item
->list
);
1645 parse_error(pe
, FILT_ERR_BAD_SUBSYS_FILTER
, 0);
1649 /* If any call succeeded, we still need to sync */
1651 tracepoint_synchronize_unregister();
1652 list_for_each_entry_safe(filter_item
, tmp
, &filter_list
, list
) {
1653 __free_filter(filter_item
->filter
);
1654 list_del(&filter_item
->list
);
1660 static int create_filter_start(char *filter_string
, bool set_str
,
1661 struct filter_parse_error
**pse
,
1662 struct event_filter
**filterp
)
1664 struct event_filter
*filter
;
1665 struct filter_parse_error
*pe
= NULL
;
1668 if (WARN_ON_ONCE(*pse
|| *filterp
))
1671 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
1672 if (filter
&& set_str
) {
1673 filter
->filter_string
= kstrdup(filter_string
, GFP_KERNEL
);
1674 if (!filter
->filter_string
)
1678 pe
= kzalloc(sizeof(*pe
), GFP_KERNEL
);
1680 if (!filter
|| !pe
|| err
) {
1682 __free_filter(filter
);
1686 /* we're committed to creating a new filter */
1693 static void create_filter_finish(struct filter_parse_error
*pe
)
1699 * create_filter - create a filter for a trace_event_call
1700 * @call: trace_event_call to create a filter for
1701 * @filter_str: filter string
1702 * @set_str: remember @filter_str and enable detailed error in filter
1703 * @filterp: out param for created filter (always updated on return)
1704 * Must be a pointer that references a NULL pointer.
1706 * Creates a filter for @call with @filter_str. If @set_str is %true,
1707 * @filter_str is copied and recorded in the new filter.
1709 * On success, returns 0 and *@filterp points to the new filter. On
1710 * failure, returns -errno and *@filterp may point to %NULL or to a new
1711 * filter. In the latter case, the returned filter contains error
1712 * information if @set_str is %true and the caller is responsible for
1715 static int create_filter(struct trace_event_call
*call
,
1716 char *filter_string
, bool set_str
,
1717 struct event_filter
**filterp
)
1719 struct filter_parse_error
*pe
= NULL
;
1722 /* filterp must point to NULL */
1723 if (WARN_ON(*filterp
))
1726 err
= create_filter_start(filter_string
, set_str
, &pe
, filterp
);
1730 err
= process_preds(call
, filter_string
, *filterp
, pe
);
1732 append_filter_err(pe
, *filterp
);
1733 create_filter_finish(pe
);
1738 int create_event_filter(struct trace_event_call
*call
,
1739 char *filter_str
, bool set_str
,
1740 struct event_filter
**filterp
)
1742 return create_filter(call
, filter_str
, set_str
, filterp
);
1746 * create_system_filter - create a filter for an event_subsystem
1747 * @system: event_subsystem to create a filter for
1748 * @filter_str: filter string
1749 * @filterp: out param for created filter (always updated on return)
1751 * Identical to create_filter() except that it creates a subsystem filter
1752 * and always remembers @filter_str.
1754 static int create_system_filter(struct trace_subsystem_dir
*dir
,
1755 struct trace_array
*tr
,
1756 char *filter_str
, struct event_filter
**filterp
)
1758 struct filter_parse_error
*pe
= NULL
;
1761 err
= create_filter_start(filter_str
, true, &pe
, filterp
);
1763 err
= process_system_preds(dir
, tr
, pe
, filter_str
);
1765 /* System filters just show a default message */
1766 kfree((*filterp
)->filter_string
);
1767 (*filterp
)->filter_string
= NULL
;
1769 append_filter_err(pe
, *filterp
);
1772 create_filter_finish(pe
);
1777 /* caller must hold event_mutex */
1778 int apply_event_filter(struct trace_event_file
*file
, char *filter_string
)
1780 struct trace_event_call
*call
= file
->event_call
;
1781 struct event_filter
*filter
= NULL
;
1784 if (!strcmp(strstrip(filter_string
), "0")) {
1785 filter_disable(file
);
1786 filter
= event_filter(file
);
1791 event_clear_filter(file
);
1793 /* Make sure the filter is not being used */
1794 tracepoint_synchronize_unregister();
1795 __free_filter(filter
);
1800 err
= create_filter(call
, filter_string
, true, &filter
);
1803 * Always swap the call filter with the new filter
1804 * even if there was an error. If there was an error
1805 * in the filter, we disable the filter and show the error
1809 struct event_filter
*tmp
;
1811 tmp
= event_filter(file
);
1813 event_set_filtered_flag(file
);
1815 filter_disable(file
);
1817 event_set_filter(file
, filter
);
1820 /* Make sure the call is done with the filter */
1821 tracepoint_synchronize_unregister();
1829 int apply_subsystem_event_filter(struct trace_subsystem_dir
*dir
,
1830 char *filter_string
)
1832 struct event_subsystem
*system
= dir
->subsystem
;
1833 struct trace_array
*tr
= dir
->tr
;
1834 struct event_filter
*filter
= NULL
;
1837 mutex_lock(&event_mutex
);
1839 /* Make sure the system still has events */
1840 if (!dir
->nr_events
) {
1845 if (!strcmp(strstrip(filter_string
), "0")) {
1846 filter_free_subsystem_preds(dir
, tr
);
1847 remove_filter_string(system
->filter
);
1848 filter
= system
->filter
;
1849 system
->filter
= NULL
;
1850 /* Ensure all filters are no longer used */
1851 tracepoint_synchronize_unregister();
1852 filter_free_subsystem_filters(dir
, tr
);
1853 __free_filter(filter
);
1857 err
= create_system_filter(dir
, tr
, filter_string
, &filter
);
1860 * No event actually uses the system filter
1861 * we can free it without synchronize_rcu().
1863 __free_filter(system
->filter
);
1864 system
->filter
= filter
;
1867 mutex_unlock(&event_mutex
);
1872 #ifdef CONFIG_PERF_EVENTS
1874 void ftrace_profile_free_filter(struct perf_event
*event
)
1876 struct event_filter
*filter
= event
->filter
;
1878 event
->filter
= NULL
;
1879 __free_filter(filter
);
1882 struct function_filter_data
{
1883 struct ftrace_ops
*ops
;
1888 #ifdef CONFIG_FUNCTION_TRACER
1890 ftrace_function_filter_re(char *buf
, int len
, int *count
)
1894 str
= kstrndup(buf
, len
, GFP_KERNEL
);
1899 * The argv_split function takes white space
1900 * as a separator, so convert ',' into spaces.
1902 strreplace(str
, ',', ' ');
1904 re
= argv_split(GFP_KERNEL
, str
, count
);
1909 static int ftrace_function_set_regexp(struct ftrace_ops
*ops
, int filter
,
1910 int reset
, char *re
, int len
)
1915 ret
= ftrace_set_filter(ops
, re
, len
, reset
);
1917 ret
= ftrace_set_notrace(ops
, re
, len
, reset
);
1922 static int __ftrace_function_set_filter(int filter
, char *buf
, int len
,
1923 struct function_filter_data
*data
)
1925 int i
, re_cnt
, ret
= -EINVAL
;
1929 reset
= filter
? &data
->first_filter
: &data
->first_notrace
;
1932 * The 'ip' field could have multiple filters set, separated
1933 * either by space or comma. We first cut the filter and apply
1934 * all pieces separatelly.
1936 re
= ftrace_function_filter_re(buf
, len
, &re_cnt
);
1940 for (i
= 0; i
< re_cnt
; i
++) {
1941 ret
= ftrace_function_set_regexp(data
->ops
, filter
, *reset
,
1942 re
[i
], strlen(re
[i
]));
1954 static int ftrace_function_check_pred(struct filter_pred
*pred
)
1956 struct ftrace_event_field
*field
= pred
->field
;
1959 * Check the predicate for function trace, verify:
1960 * - only '==' and '!=' is used
1961 * - the 'ip' field is used
1963 if ((pred
->op
!= OP_EQ
) && (pred
->op
!= OP_NE
))
1966 if (strcmp(field
->name
, "ip"))
1972 static int ftrace_function_set_filter_pred(struct filter_pred
*pred
,
1973 struct function_filter_data
*data
)
1977 /* Checking the node is valid for function trace. */
1978 ret
= ftrace_function_check_pred(pred
);
1982 return __ftrace_function_set_filter(pred
->op
== OP_EQ
,
1983 pred
->regex
.pattern
,
1988 static bool is_or(struct prog_entry
*prog
, int i
)
1993 * Only "||" is allowed for function events, thus,
1994 * all true branches should jump to true, and any
1995 * false branch should jump to false.
1997 target
= prog
[i
].target
+ 1;
1998 /* True and false have NULL preds (all prog entries should jump to one */
1999 if (prog
[target
].pred
)
2002 /* prog[target].target is 1 for TRUE, 0 for FALSE */
2003 return prog
[i
].when_to_branch
== prog
[target
].target
;
2006 static int ftrace_function_set_filter(struct perf_event
*event
,
2007 struct event_filter
*filter
)
2009 struct prog_entry
*prog
= rcu_dereference_protected(filter
->prog
,
2010 lockdep_is_held(&event_mutex
));
2011 struct function_filter_data data
= {
2014 .ops
= &event
->ftrace_ops
,
2018 for (i
= 0; prog
[i
].pred
; i
++) {
2019 struct filter_pred
*pred
= prog
[i
].pred
;
2021 if (!is_or(prog
, i
))
2024 if (ftrace_function_set_filter_pred(pred
, &data
) < 0)
2030 static int ftrace_function_set_filter(struct perf_event
*event
,
2031 struct event_filter
*filter
)
2035 #endif /* CONFIG_FUNCTION_TRACER */
2037 int ftrace_profile_set_filter(struct perf_event
*event
, int event_id
,
2041 struct event_filter
*filter
= NULL
;
2042 struct trace_event_call
*call
;
2044 mutex_lock(&event_mutex
);
2046 call
= event
->tp_event
;
2056 err
= create_filter(call
, filter_str
, false, &filter
);
2060 if (ftrace_event_is_function(call
))
2061 err
= ftrace_function_set_filter(event
, filter
);
2063 event
->filter
= filter
;
2066 if (err
|| ftrace_event_is_function(call
))
2067 __free_filter(filter
);
2070 mutex_unlock(&event_mutex
);
2075 #endif /* CONFIG_PERF_EVENTS */
2077 #ifdef CONFIG_FTRACE_STARTUP_TEST
2079 #include <linux/types.h>
2080 #include <linux/tracepoint.h>
2082 #define CREATE_TRACE_POINTS
2083 #include "trace_events_filter_test.h"
2085 #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \
2088 .rec = { .a = va, .b = vb, .c = vc, .d = vd, \
2089 .e = ve, .f = vf, .g = vg, .h = vh }, \
2091 .not_visited = nvisit, \
2096 static struct test_filter_data_t
{
2098 struct trace_event_raw_ftrace_test_filter rec
;
2101 } test_filter_data
[] = {
2102 #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \
2103 "e == 1 && f == 1 && g == 1 && h == 1"
2104 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, ""),
2105 DATA_REC(NO
, 0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"),
2106 DATA_REC(NO
, 1, 1, 1, 1, 1, 1, 1, 0, ""),
2108 #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \
2109 "e == 1 || f == 1 || g == 1 || h == 1"
2110 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2111 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2112 DATA_REC(YES
, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"),
2114 #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \
2115 "(e == 1 || f == 1) && (g == 1 || h == 1)"
2116 DATA_REC(NO
, 0, 0, 1, 1, 1, 1, 1, 1, "dfh"),
2117 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2118 DATA_REC(YES
, 1, 0, 1, 0, 0, 1, 0, 1, "bd"),
2119 DATA_REC(NO
, 1, 0, 1, 0, 0, 1, 0, 0, "bd"),
2121 #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \
2122 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2123 DATA_REC(YES
, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"),
2124 DATA_REC(YES
, 0, 0, 0, 0, 0, 0, 1, 1, ""),
2125 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2127 #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \
2128 "(e == 1 && f == 1) || (g == 1 && h == 1)"
2129 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 0, "gh"),
2130 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 1, ""),
2131 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, ""),
2133 #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \
2134 "(e == 1 || f == 1)) && (g == 1 || h == 1)"
2135 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"),
2136 DATA_REC(NO
, 0, 0, 0, 0, 0, 0, 0, 0, ""),
2137 DATA_REC(YES
, 1, 1, 1, 1, 1, 0, 1, 1, "h"),
2139 #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \
2140 "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))"
2141 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"),
2142 DATA_REC(NO
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2143 DATA_REC(NO
, 1, 0, 1, 0, 1, 0, 1, 0, ""),
2145 #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \
2146 "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))"
2147 DATA_REC(YES
, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"),
2148 DATA_REC(YES
, 0, 1, 0, 1, 0, 1, 0, 1, ""),
2149 DATA_REC(YES
, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"),
2157 #define DATA_CNT ARRAY_SIZE(test_filter_data)
2159 static int test_pred_visited
;
2161 static int test_pred_visited_fn(struct filter_pred
*pred
, void *event
)
2163 struct ftrace_event_field
*field
= pred
->field
;
2165 test_pred_visited
= 1;
2166 printk(KERN_INFO
"\npred visited %s\n", field
->name
);
2170 static void update_pred_fn(struct event_filter
*filter
, char *fields
)
2172 struct prog_entry
*prog
= rcu_dereference_protected(filter
->prog
,
2173 lockdep_is_held(&event_mutex
));
2176 for (i
= 0; prog
[i
].pred
; i
++) {
2177 struct filter_pred
*pred
= prog
[i
].pred
;
2178 struct ftrace_event_field
*field
= pred
->field
;
2180 WARN_ON_ONCE(!pred
->fn
);
2183 WARN_ONCE(1, "all leafs should have field defined %d", i
);
2187 if (!strchr(fields
, *field
->name
))
2190 pred
->fn
= test_pred_visited_fn
;
2194 static __init
int ftrace_test_event_filter(void)
2198 printk(KERN_INFO
"Testing ftrace filter: ");
2200 for (i
= 0; i
< DATA_CNT
; i
++) {
2201 struct event_filter
*filter
= NULL
;
2202 struct test_filter_data_t
*d
= &test_filter_data
[i
];
2205 err
= create_filter(&event_ftrace_test_filter
, d
->filter
,
2209 "Failed to get filter for '%s', err %d\n",
2211 __free_filter(filter
);
2215 /* Needed to dereference filter->prog */
2216 mutex_lock(&event_mutex
);
2218 * The preemption disabling is not really needed for self
2219 * tests, but the rcu dereference will complain without it.
2222 if (*d
->not_visited
)
2223 update_pred_fn(filter
, d
->not_visited
);
2225 test_pred_visited
= 0;
2226 err
= filter_match_preds(filter
, &d
->rec
);
2229 mutex_unlock(&event_mutex
);
2231 __free_filter(filter
);
2233 if (test_pred_visited
) {
2235 "Failed, unwanted pred visited for filter %s\n",
2240 if (err
!= d
->match
) {
2242 "Failed to match filter '%s', expected %d\n",
2243 d
->filter
, d
->match
);
2249 printk(KERN_CONT
"OK\n");
2254 late_initcall(ftrace_test_event_filter
);
2256 #endif /* CONFIG_FTRACE_STARTUP_TEST */