Skip to content

Commit 4c86bc5

Browse files
committed
tracing: Add :mod: command to enabled module events
Add a :mod: command to enable only events from a given module from the set_events file. echo '*:mod:<module>' > set_events Or echo ':mod:<module>' > set_events Will enable all events for that module. Specific events can also be enabled via: echo '<event>:mod:<module>' > set_events Or echo '<system>:<event>:mod:<module>' > set_events Or echo '*:<event>:mod:<module>' > set_events The ":mod:" keyword is consistent with the function tracing filter to enable functions from a given module. Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Link: https://lore.kernel.org/20250116143533.214496360@goodmis.org Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 80c3e28 commit 4c86bc5

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

Documentation/trace/events.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ command::
5555

5656
# echo 'irq:*' > /sys/kernel/tracing/set_event
5757

58+
The set_event file may also be used to enable events associated to only
59+
a specific module::
60+
61+
# echo ':mod:<module>' > /sys/kernel/tracing/set_event
62+
63+
Will enable all events in the module ``<module>``.
64+
65+
The text before ``:mod:`` will be parsed to specify specific events that the
66+
module creates::
67+
68+
# echo '<match>:mod:<module>' > /sys/kernel/tracing/set_event
69+
70+
The above will enable any system or event that ``<match>`` matches. If
71+
``<match>`` is ``"*"`` then it will match all events.
72+
73+
To enable only a specific event within a system::
74+
75+
# echo '<system>:<event>:mod:<module>' > /sys/kernel/tracing/set_event
76+
77+
If ``<event>`` is ``"*"`` then it will match all events within the system
78+
for a given module.
79+
5880
2.2 Via the 'enable' toggle
5981
---------------------------
6082

kernel/trace/trace.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,6 +5514,8 @@ static const char readme_msg[] =
55145514
"\t efield: For event probes ('e' types), the field is on of the fields\n"
55155515
"\t of the <attached-group>/<attached-event>.\n"
55165516
#endif
5517+
" set_event\t\t- Enables events by name written into it\n"
5518+
"\t\t\t Can enable module events via: :mod:<module>\n"
55175519
" events/\t\t- Directory containing all trace event subsystems:\n"
55185520
" enable\t\t- Write 0/1 to enable/disable tracing of all events\n"
55195521
" events/<system>/\t- Directory containing all trace events for <system>:\n"

kernel/trace/trace_events.c

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,17 +1153,36 @@ static void remove_event_file_dir(struct trace_event_file *file)
11531153
*/
11541154
static int
11551155
__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
1156-
const char *sub, const char *event, int set)
1156+
const char *sub, const char *event, int set,
1157+
const char *mod)
11571158
{
11581159
struct trace_event_file *file;
11591160
struct trace_event_call *call;
1161+
char *module __free(kfree) = NULL;
11601162
const char *name;
11611163
int ret = -EINVAL;
11621164
int eret = 0;
11631165

1166+
if (mod) {
1167+
char *p;
1168+
1169+
module = kstrdup(mod, GFP_KERNEL);
1170+
if (!module)
1171+
return -ENOMEM;
1172+
1173+
/* Replace all '-' with '_' as that's what modules do */
1174+
for (p = strchr(module, '-'); p; p = strchr(p + 1, '-'))
1175+
*p = '_';
1176+
}
1177+
11641178
list_for_each_entry(file, &tr->events, list) {
11651179

11661180
call = file->event_call;
1181+
1182+
/* If a module is specified, skip events that are not that module */
1183+
if (module && (!call->module || strcmp(module_name(call->module), module)))
1184+
continue;
1185+
11671186
name = trace_event_name(call);
11681187

11691188
if (!name || !call->class || !call->class->reg)
@@ -1200,24 +1219,34 @@ __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
12001219
}
12011220

12021221
static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1203-
const char *sub, const char *event, int set)
1222+
const char *sub, const char *event, int set,
1223+
const char *mod)
12041224
{
12051225
int ret;
12061226

12071227
mutex_lock(&event_mutex);
1208-
ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
1228+
ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set, mod);
12091229
mutex_unlock(&event_mutex);
12101230

12111231
return ret;
12121232
}
12131233

12141234
int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
12151235
{
1216-
char *event = NULL, *sub = NULL, *match;
1236+
char *event = NULL, *sub = NULL, *match, *mod;
12171237
int ret;
12181238

12191239
if (!tr)
12201240
return -ENOENT;
1241+
1242+
/* Modules events can be appened with :mod:<module> */
1243+
mod = strstr(buf, ":mod:");
1244+
if (mod) {
1245+
*mod = '\0';
1246+
/* move to the module name */
1247+
mod += 5;
1248+
}
1249+
12211250
/*
12221251
* The buf format can be <subsystem>:<event-name>
12231252
* *:<event-name> means any event by that name.
@@ -1240,9 +1269,13 @@ int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
12401269
sub = NULL;
12411270
if (!strlen(event) || strcmp(event, "*") == 0)
12421271
event = NULL;
1272+
} else if (mod) {
1273+
/* Allow wildcard for no length or star */
1274+
if (!strlen(match) || strcmp(match, "*") == 0)
1275+
match = NULL;
12431276
}
12441277

1245-
ret = __ftrace_set_clr_event(tr, match, sub, event, set);
1278+
ret = __ftrace_set_clr_event(tr, match, sub, event, set, mod);
12461279

12471280
/* Put back the colon to allow this to be called again */
12481281
if (buf)
@@ -1270,7 +1303,7 @@ int trace_set_clr_event(const char *system, const char *event, int set)
12701303
if (!tr)
12711304
return -ENODEV;
12721305

1273-
return __ftrace_set_clr_event(tr, NULL, system, event, set);
1306+
return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
12741307
}
12751308
EXPORT_SYMBOL_GPL(trace_set_clr_event);
12761309

@@ -1296,7 +1329,7 @@ int trace_array_set_clr_event(struct trace_array *tr, const char *system,
12961329
return -ENOENT;
12971330

12981331
set = (enable == true) ? 1 : 0;
1299-
return __ftrace_set_clr_event(tr, NULL, system, event, set);
1332+
return __ftrace_set_clr_event(tr, NULL, system, event, set, NULL);
13001333
}
13011334
EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
13021335

@@ -1646,7 +1679,7 @@ system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
16461679
if (system)
16471680
name = system->name;
16481681

1649-
ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1682+
ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val, NULL);
16501683
if (ret)
16511684
goto out;
16521685

@@ -4094,7 +4127,7 @@ int event_trace_del_tracer(struct trace_array *tr)
40944127
__ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
40954128

40964129
/* Disable any running events */
4097-
__ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
4130+
__ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0, NULL);
40984131

40994132
/* Make sure no more events are being executed */
41004133
tracepoint_synchronize_unregister();
@@ -4378,7 +4411,7 @@ static __init void event_trace_self_tests(void)
43784411

43794412
pr_info("Testing event system %s: ", system->name);
43804413

4381-
ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
4414+
ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1, NULL);
43824415
if (WARN_ON_ONCE(ret)) {
43834416
pr_warn("error enabling system %s\n",
43844417
system->name);
@@ -4387,7 +4420,7 @@ static __init void event_trace_self_tests(void)
43874420

43884421
event_test_stuff();
43894422

4390-
ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
4423+
ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0, NULL);
43914424
if (WARN_ON_ONCE(ret)) {
43924425
pr_warn("error disabling system %s\n",
43934426
system->name);
@@ -4402,7 +4435,7 @@ static __init void event_trace_self_tests(void)
44024435
pr_info("Running tests on all trace events:\n");
44034436
pr_info("Testing all events: ");
44044437

4405-
ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
4438+
ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1, NULL);
44064439
if (WARN_ON_ONCE(ret)) {
44074440
pr_warn("error enabling all events\n");
44084441
return;
@@ -4411,7 +4444,7 @@ static __init void event_trace_self_tests(void)
44114444
event_test_stuff();
44124445

44134446
/* reset sysname */
4414-
ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
4447+
ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0, NULL);
44154448
if (WARN_ON_ONCE(ret)) {
44164449
pr_warn("error disabling all events\n");
44174450
return;

0 commit comments

Comments
 (0)