tracing: Parse filter from boot event string

Make it possible to specify a trace event filter from the
kernel command line.

Example:

	trace_event=syscalls:sys_enter_clock_nanosleep:filter:which_clock==0

Link: https://patch.msgid.link/20260908-tracing-cli-event-filter-v2-7-05396a3fb663@linutronix.de
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
This commit is contained in:
Thomas Weißschuh 2026-09-08 08:22:21 +02:00 committed by Steven Rostedt
parent d30778dee4
commit 084100a1b5
7 changed files with 58 additions and 16 deletions

View File

@ -7733,8 +7733,13 @@ Kernel parameters
trace_event=:mod:<module>
The value before :mod: will only enable specific events
that are part of the module. See the above mentioned
document for more information.
that are part of the module.
To apply a filter, use the :filter: keyword:
trace_event=...:filter:<filter>
See the above mentioned document for more information.
trace_instance=[instance-info]
[FTRACE] Create a ring buffer instance early in boot up.

View File

@ -118,6 +118,11 @@ In order to facilitate early boot debugging, use boot option::
event-list is a comma separated list of events. See section 2.1 for event
format.
The boot option also supports the ``:filter:`` keyword to apply a filter.
The ``:filter:`` keyword needs to be at the end of the event specification::
<system>:<event>:mod:<module>:filter:<filter>
3. Defining an event-enabled tracepoint
=======================================

View File

@ -883,7 +883,7 @@ extern int trace_add_event_call(struct trace_event_call *call);
extern int trace_remove_event_call(struct trace_event_call *call);
extern int trace_event_get_offsets(struct trace_event_call *call);
int ftrace_set_clr_event(struct trace_array *tr, const char *buf, int set);
int ftrace_set_clr_event(struct trace_array *tr, const char *buf, int set, bool handle_filter);
int trace_set_clr_event(const char *system, const char *event, int set);
int trace_array_set_clr_event(struct trace_array *tr, const char *system,
const char *event, bool enable);

View File

@ -2511,5 +2511,6 @@ static inline int rv_init_interface(void)
#endif /* _LINUX_KERNEL_TRACE_H */
#if IS_ENABLED(CONFIG_KUNIT)
void ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, char **mod);
void ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, char **mod,
bool handle_filter, char **filter);
#endif

View File

@ -94,7 +94,7 @@ trace_boot_enable_events(struct trace_array *tr, struct xbc_node *node)
continue;
}
if (ftrace_set_clr_event(tr, buf, 1) < 0)
if (ftrace_set_clr_event(tr, buf, 1, false) < 0)
pr_err("Failed to enable event: %s\n", p);
}
}

View File

@ -1458,10 +1458,21 @@ static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
}
VISIBLE_IF_KUNIT void
ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, char **mod)
ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, char **mod,
bool handle_filter, char **filter)
{
*event = NULL;
*sub = NULL;
*filter = NULL;
/* event filters can be appended with :filter:<filter> */
if (handle_filter) {
*filter = strstr(buf, ":filter:");
if (*filter) {
**filter = '\0';
*filter += 8;
}
}
/* Modules events can be appended with :mod:<module> */
*mod = strstr(buf, ":mod:");
@ -1501,9 +1512,9 @@ ftrace_parse_event_string(char *buf, char **match, char **sub, char **event, cha
}
EXPORT_SYMBOL_IF_KUNIT(ftrace_parse_event_string);
int ftrace_set_clr_event(struct trace_array *tr, const char *arg_buf, int set)
int ftrace_set_clr_event(struct trace_array *tr, const char *arg_buf, int set, bool handle_filter)
{
char *event, *sub, *match, *mod;
char *event, *sub, *match, *mod, *filter;
if (!tr)
return -ENOENT;
@ -1512,9 +1523,9 @@ int ftrace_set_clr_event(struct trace_array *tr, const char *arg_buf, int set)
if (!dupped_buf)
return -ENOMEM;
ftrace_parse_event_string(dupped_buf, &match, &sub, &event, &mod);
ftrace_parse_event_string(dupped_buf, &match, &sub, &event, &mod, handle_filter, &filter);
return __ftrace_set_clr_event(tr, match, sub, event, set, mod, NULL);
return __ftrace_set_clr_event(tr, match, sub, event, set, mod, filter);
}
/**
@ -1596,7 +1607,7 @@ ftrace_event_write(struct file *file, const char __user *ubuf,
if (*parser.buffer == '!')
set = 0;
ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
ret = ftrace_set_clr_event(tr, parser.buffer + !set, set, false);
if (ret)
goto out_put;
}
@ -4865,10 +4876,10 @@ static __init void __early_set_events(struct trace_array *tr, char *buf, bool en
while ((token = strsep(&buf, ","))) {
if (*token) {
if (enable) {
if (ftrace_set_clr_event(tr, token, 1))
if (ftrace_set_clr_event(tr, token, 1, true))
pr_warn("Failed to enable trace event: %s\n", token);
} else {
ftrace_set_clr_event(tr, token, 0);
ftrace_set_clr_event(tr, token, 0, true);
}
}

View File

@ -8,7 +8,7 @@
struct parse_event_string_test_case {
const char *input;
const char *match, *sub, *event, *mod;
const char *match, *sub, *event, *mod, *filter;
};
static const struct parse_event_string_test_case parse_event_string_test_cases[] = {
@ -37,6 +37,12 @@ static const struct parse_event_string_test_case parse_event_string_test_cases[]
.sub = "sub",
.event = "event",
},
{
"sub:event:filter:filter",
.sub = "sub",
.event = "event",
.filter = "filter",
},
{
"match",
.match = "match",
@ -51,6 +57,19 @@ static const struct parse_event_string_test_case parse_event_string_test_cases[]
.event = "event",
.mod = "module",
},
{
"sub:event:mod:module:filter:filter",
.sub = "sub",
.event = "event",
.mod = "module",
.filter = "filter",
},
{
"sub:event:mod:module:filter:unexpected-filter",
.sub = "sub",
.event = "event",
.mod = "module:filter:unexpected-filter",
},
};
static void
@ -72,18 +91,19 @@ KUNIT_ARRAY_PARAM(parse_event_string, parse_event_string_test_cases, parse_event
static void parse_event_string(struct kunit *test)
{
const struct parse_event_string_test_case *params = test->param_value;
char *input, *match, *sub, *event, *mod;
char *input, *match, *sub, *event, *mod, *filter;
input = kunit_kstrdup(test, params->input, GFP_KERNEL);
if (!input)
kunit_skip(test, "ENOMEM");
ftrace_parse_event_string(input, &match, &sub, &event, &mod);
ftrace_parse_event_string(input, &match, &sub, &event, &mod, params->filter, &filter);
EXPECT_NULL_OR_STR_EQ(test, match, params->match);
EXPECT_NULL_OR_STR_EQ(test, sub, params->sub);
EXPECT_NULL_OR_STR_EQ(test, event, params->event);
EXPECT_NULL_OR_STR_EQ(test, mod, params->mod);
EXPECT_NULL_OR_STR_EQ(test, filter, params->filter);
}
static struct kunit_case trace_events_test_cases[] = {