blob: d00fee705f9c57defef5af41dadc63514d405cbc [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Tom Zanussi85f2b082013-10-24 08:59:24 -05002/*
3 * trace_events_trigger - trace event triggers
4 *
Tom Zanussi85f2b082013-10-24 08:59:24 -05005 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
6 */
7
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -04008#include <linux/security.h>
Tom Zanussi85f2b082013-10-24 08:59:24 -05009#include <linux/module.h>
10#include <linux/ctype.h>
11#include <linux/mutex.h>
12#include <linux/slab.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010013#include <linux/rculist.h>
Tom Zanussi85f2b082013-10-24 08:59:24 -050014
15#include "trace.h"
16
17static LIST_HEAD(trigger_commands);
18static DEFINE_MUTEX(trigger_cmd_mutex);
19
Tom Zanussiab4bf002015-12-10 12:50:44 -060020void trigger_data_free(struct event_trigger_data *data)
Tom Zanussi2a2df322013-10-24 08:59:25 -050021{
Tom Zanussibac5fb92013-10-24 08:59:29 -050022 if (data->cmd_ops->set_filter)
23 data->cmd_ops->set_filter(NULL, data, NULL);
24
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -040025 /* make sure current triggers exit before free */
26 tracepoint_synchronize_unregister();
27
Tom Zanussi2a2df322013-10-24 08:59:25 -050028 kfree(data);
29}
30
Tom Zanussi85f2b082013-10-24 08:59:24 -050031/**
32 * event_triggers_call - Call triggers associated with a trace event
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040033 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050034 * @rec: The trace entry for the event, NULL for unconditional invocation
Tom Zanussi85f2b082013-10-24 08:59:24 -050035 *
36 * For each trigger associated with an event, invoke the trigger
Tom Zanussibac5fb92013-10-24 08:59:29 -050037 * function registered with the associated trigger command. If rec is
38 * non-NULL, it means that the trigger requires further processing and
39 * shouldn't be unconditionally invoked. If rec is non-NULL and the
40 * trigger has a filter associated with it, rec will checked against
41 * the filter and if the record matches the trigger will be invoked.
42 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
43 * in any case until the current event is written, the trigger
44 * function isn't invoked but the bit associated with the deferred
45 * trigger is set in the return value.
46 *
47 * Returns an enum event_trigger_type value containing a set bit for
48 * any trigger that should be deferred, ETT_NONE if nothing to defer.
Tom Zanussi85f2b082013-10-24 08:59:24 -050049 *
50 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
51 *
52 * Return: an enum event_trigger_type value containing a set bit for
53 * any trigger that should be deferred, ETT_NONE if nothing to defer.
54 */
Tom Zanussibac5fb92013-10-24 08:59:29 -050055enum event_trigger_type
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -040056event_triggers_call(struct trace_event_file *file,
57 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -060058 struct ring_buffer_event *event)
Tom Zanussibac5fb92013-10-24 08:59:29 -050059{
60 struct event_trigger_data *data;
61 enum event_trigger_type tt = ETT_NONE;
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -050062 struct event_filter *filter;
Tom Zanussibac5fb92013-10-24 08:59:29 -050063
64 if (list_empty(&file->triggers))
65 return tt;
66
67 list_for_each_entry_rcu(data, &file->triggers, list) {
Tom Zanussi104f2812015-12-10 12:50:47 -060068 if (data->paused)
69 continue;
Tom Zanussibac5fb92013-10-24 08:59:29 -050070 if (!rec) {
Tom Zanussifb339e52022-01-10 08:04:12 -060071 data->ops->trigger(data, buffer, rec, event);
Tom Zanussibac5fb92013-10-24 08:59:29 -050072 continue;
73 }
Steven Rostedt (Red Hat)561a4fe2014-05-02 13:30:04 -040074 filter = rcu_dereference_sched(data->filter);
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -050075 if (filter && !filter_match_preds(filter, rec))
Tom Zanussibac5fb92013-10-24 08:59:29 -050076 continue;
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -050077 if (event_command_post_trigger(data->cmd_ops)) {
Tom Zanussibac5fb92013-10-24 08:59:29 -050078 tt |= data->cmd_ops->trigger_type;
79 continue;
80 }
Tom Zanussifb339e52022-01-10 08:04:12 -060081 data->ops->trigger(data, buffer, rec, event);
Tom Zanussibac5fb92013-10-24 08:59:29 -050082 }
83 return tt;
84}
85EXPORT_SYMBOL_GPL(event_triggers_call);
86
87/**
88 * event_triggers_post_call - Call 'post_triggers' for a trace event
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040089 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050090 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
91 *
92 * For each trigger associated with an event, invoke the trigger
93 * function registered with the associated trigger command, if the
94 * corresponding bit is set in the tt enum passed into this function.
95 * See @event_triggers_call for details on how those bits are set.
96 *
97 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
98 */
99void
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400100event_triggers_post_call(struct trace_event_file *file,
Steven Rostedt (VMware)c94e45b2018-05-07 16:02:14 -0400101 enum event_trigger_type tt)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500102{
103 struct event_trigger_data *data;
104
Tom Zanussibac5fb92013-10-24 08:59:29 -0500105 list_for_each_entry_rcu(data, &file->triggers, list) {
Tom Zanussi104f2812015-12-10 12:50:47 -0600106 if (data->paused)
107 continue;
Tom Zanussibac5fb92013-10-24 08:59:29 -0500108 if (data->cmd_ops->trigger_type & tt)
Tom Zanussifb339e52022-01-10 08:04:12 -0600109 data->ops->trigger(data, NULL, NULL, NULL);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500110 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500111}
Tom Zanussibac5fb92013-10-24 08:59:29 -0500112EXPORT_SYMBOL_GPL(event_triggers_post_call);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500113
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500114#define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
115
Tom Zanussi85f2b082013-10-24 08:59:24 -0500116static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
117{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400118 struct trace_event_file *event_file = event_file_data(m->private);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500119
Vasily Averin6722b232020-01-24 10:03:06 +0300120 if (t == SHOW_AVAILABLE_TRIGGERS) {
121 (*pos)++;
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500122 return NULL;
Vasily Averin6722b232020-01-24 10:03:06 +0300123 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500124 return seq_list_next(t, &event_file->triggers, pos);
125}
126
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400127static bool check_user_trigger(struct trace_event_file *file)
128{
129 struct event_trigger_data *data;
130
131 list_for_each_entry_rcu(data, &file->triggers, list) {
132 if (data->flags & EVENT_TRIGGER_FL_PROBE)
133 continue;
134 return true;
135 }
136 return false;
137}
138
Tom Zanussi85f2b082013-10-24 08:59:24 -0500139static void *trigger_start(struct seq_file *m, loff_t *pos)
140{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400141 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500142
143 /* ->stop() is called even if ->start() fails */
144 mutex_lock(&event_mutex);
145 event_file = event_file_data(m->private);
146 if (unlikely(!event_file))
147 return ERR_PTR(-ENODEV);
148
Tzvetomir Stoyanov (VMware)7491e2c2021-08-19 11:26:06 -0400149 if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500150 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
151
Tom Zanussi85f2b082013-10-24 08:59:24 -0500152 return seq_list_start(&event_file->triggers, *pos);
153}
154
155static void trigger_stop(struct seq_file *m, void *t)
156{
157 mutex_unlock(&event_mutex);
158}
159
160static int trigger_show(struct seq_file *m, void *v)
161{
162 struct event_trigger_data *data;
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500163 struct event_command *p;
164
165 if (v == SHOW_AVAILABLE_TRIGGERS) {
166 seq_puts(m, "# Available triggers:\n");
167 seq_putc(m, '#');
168 mutex_lock(&trigger_cmd_mutex);
169 list_for_each_entry_reverse(p, &trigger_commands, list)
170 seq_printf(m, " %s", p->name);
171 seq_putc(m, '\n');
172 mutex_unlock(&trigger_cmd_mutex);
173 return 0;
174 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500175
176 data = list_entry(v, struct event_trigger_data, list);
177 data->ops->print(m, data->ops, data);
178
179 return 0;
180}
181
182static const struct seq_operations event_triggers_seq_ops = {
183 .start = trigger_start,
184 .next = trigger_next,
185 .stop = trigger_stop,
186 .show = trigger_show,
187};
188
189static int event_trigger_regex_open(struct inode *inode, struct file *file)
190{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -0400191 int ret;
192
193 ret = security_locked_down(LOCKDOWN_TRACEFS);
194 if (ret)
195 return ret;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500196
197 mutex_lock(&event_mutex);
198
199 if (unlikely(!event_file_data(file))) {
200 mutex_unlock(&event_mutex);
201 return -ENODEV;
202 }
203
Tom Zanussia88e1cf2015-12-10 12:50:49 -0600204 if ((file->f_mode & FMODE_WRITE) &&
205 (file->f_flags & O_TRUNC)) {
206 struct trace_event_file *event_file;
207 struct event_command *p;
208
209 event_file = event_file_data(file);
210
211 list_for_each_entry(p, &trigger_commands, list) {
212 if (p->unreg_all)
213 p->unreg_all(event_file);
214 }
215 }
216
Tom Zanussi85f2b082013-10-24 08:59:24 -0500217 if (file->f_mode & FMODE_READ) {
218 ret = seq_open(file, &event_triggers_seq_ops);
219 if (!ret) {
220 struct seq_file *m = file->private_data;
221 m->private = file;
222 }
223 }
224
225 mutex_unlock(&event_mutex);
226
227 return ret;
228}
229
Masami Hiramatsu81a59552020-01-11 01:06:29 +0900230int trigger_process_regex(struct trace_event_file *file, char *buff)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500231{
Masami Hiramatsu6784bea2020-06-20 12:46:03 +0900232 char *command, *next;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500233 struct event_command *p;
234 int ret = -EINVAL;
235
Masami Hiramatsu6784bea2020-06-20 12:46:03 +0900236 next = buff = skip_spaces(buff);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500237 command = strsep(&next, ": \t");
Masami Hiramatsu6784bea2020-06-20 12:46:03 +0900238 if (next) {
239 next = skip_spaces(next);
240 if (!*next)
241 next = NULL;
242 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500243 command = (command[0] != '!') ? command : command + 1;
244
245 mutex_lock(&trigger_cmd_mutex);
246 list_for_each_entry(p, &trigger_commands, list) {
247 if (strcmp(p->name, command) == 0) {
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -0600248 ret = p->parse(p, file, buff, command, next);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500249 goto out_unlock;
250 }
251 }
252 out_unlock:
253 mutex_unlock(&trigger_cmd_mutex);
254
255 return ret;
256}
257
258static ssize_t event_trigger_regex_write(struct file *file,
259 const char __user *ubuf,
260 size_t cnt, loff_t *ppos)
261{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400262 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500263 ssize_t ret;
264 char *buf;
265
266 if (!cnt)
267 return 0;
268
269 if (cnt >= PAGE_SIZE)
270 return -EINVAL;
271
Al Viro70f6cbb2015-12-24 00:13:10 -0500272 buf = memdup_user_nul(ubuf, cnt);
273 if (IS_ERR(buf))
274 return PTR_ERR(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500275
Tom Zanussi85f2b082013-10-24 08:59:24 -0500276 strim(buf);
277
278 mutex_lock(&event_mutex);
279 event_file = event_file_data(file);
280 if (unlikely(!event_file)) {
281 mutex_unlock(&event_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -0500282 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500283 return -ENODEV;
284 }
285 ret = trigger_process_regex(event_file, buf);
286 mutex_unlock(&event_mutex);
287
Al Viro70f6cbb2015-12-24 00:13:10 -0500288 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500289 if (ret < 0)
290 goto out;
291
292 *ppos += cnt;
293 ret = cnt;
294 out:
295 return ret;
296}
297
298static int event_trigger_regex_release(struct inode *inode, struct file *file)
299{
300 mutex_lock(&event_mutex);
301
302 if (file->f_mode & FMODE_READ)
303 seq_release(inode, file);
304
305 mutex_unlock(&event_mutex);
306
307 return 0;
308}
309
310static ssize_t
311event_trigger_write(struct file *filp, const char __user *ubuf,
312 size_t cnt, loff_t *ppos)
313{
314 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
315}
316
317static int
318event_trigger_open(struct inode *inode, struct file *filp)
319{
Steven Rostedt (VMware)17911ff2019-10-11 17:22:50 -0400320 /* Checks for tracefs lockdown */
Tom Zanussi85f2b082013-10-24 08:59:24 -0500321 return event_trigger_regex_open(inode, filp);
322}
323
324static int
325event_trigger_release(struct inode *inode, struct file *file)
326{
327 return event_trigger_regex_release(inode, file);
328}
329
330const struct file_operations event_trigger_fops = {
331 .open = event_trigger_open,
332 .read = seq_read,
333 .write = event_trigger_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -0500334 .llseek = tracing_lseek,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500335 .release = event_trigger_release,
336};
337
Tom Zanussi2a2df322013-10-24 08:59:25 -0500338/*
339 * Currently we only register event commands from __init, so mark this
340 * __init too.
341 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600342__init int register_event_command(struct event_command *cmd)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500343{
344 struct event_command *p;
345 int ret = 0;
346
347 mutex_lock(&trigger_cmd_mutex);
348 list_for_each_entry(p, &trigger_commands, list) {
349 if (strcmp(cmd->name, p->name) == 0) {
350 ret = -EBUSY;
351 goto out_unlock;
352 }
353 }
354 list_add(&cmd->list, &trigger_commands);
355 out_unlock:
356 mutex_unlock(&trigger_cmd_mutex);
357
358 return ret;
359}
360
361/*
362 * Currently we only unregister event commands from __init, so mark
363 * this __init too.
364 */
Tom Zanussid0bad492016-03-03 12:54:55 -0600365__init int unregister_event_command(struct event_command *cmd)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500366{
367 struct event_command *p, *n;
368 int ret = -ENODEV;
369
370 mutex_lock(&trigger_cmd_mutex);
371 list_for_each_entry_safe(p, n, &trigger_commands, list) {
372 if (strcmp(cmd->name, p->name) == 0) {
373 ret = 0;
374 list_del_init(&p->list);
375 goto out_unlock;
376 }
377 }
378 out_unlock:
379 mutex_unlock(&trigger_cmd_mutex);
380
381 return ret;
382}
383
384/**
385 * event_trigger_print - Generic event_trigger_ops @print implementation
386 * @name: The name of the event trigger
387 * @m: The seq_file being printed to
388 * @data: Trigger-specific data
389 * @filter_str: filter_str to print, if present
390 *
391 * Common implementation for event triggers to print themselves.
392 *
393 * Usually wrapped by a function that simply sets the @name of the
394 * trigger command and then invokes this.
395 *
396 * Return: 0 on success, errno otherwise
397 */
398static int
399event_trigger_print(const char *name, struct seq_file *m,
400 void *data, char *filter_str)
401{
402 long count = (long)data;
403
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100404 seq_puts(m, name);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500405
406 if (count == -1)
407 seq_puts(m, ":unlimited");
408 else
409 seq_printf(m, ":count=%ld", count);
410
411 if (filter_str)
412 seq_printf(m, " if %s\n", filter_str);
413 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +0100414 seq_putc(m, '\n');
Tom Zanussi2a2df322013-10-24 08:59:25 -0500415
416 return 0;
417}
418
419/**
420 * event_trigger_init - Generic event_trigger_ops @init implementation
421 * @ops: The trigger ops associated with the trigger
422 * @data: Trigger-specific data
423 *
424 * Common implementation of event trigger initialization.
425 *
426 * Usually used directly as the @init method in event trigger
427 * implementations.
428 *
429 * Return: 0 on success, errno otherwise
430 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600431int event_trigger_init(struct event_trigger_ops *ops,
432 struct event_trigger_data *data)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500433{
434 data->ref++;
435 return 0;
436}
437
438/**
439 * event_trigger_free - Generic event_trigger_ops @free implementation
440 * @ops: The trigger ops associated with the trigger
441 * @data: Trigger-specific data
442 *
443 * Common implementation of event trigger de-initialization.
444 *
445 * Usually used directly as the @free method in event trigger
446 * implementations.
447 */
448static void
449event_trigger_free(struct event_trigger_ops *ops,
450 struct event_trigger_data *data)
451{
452 if (WARN_ON_ONCE(data->ref <= 0))
453 return;
454
455 data->ref--;
456 if (!data->ref)
457 trigger_data_free(data);
458}
459
Tom Zanussiab4bf002015-12-10 12:50:44 -0600460int trace_event_trigger_enable_disable(struct trace_event_file *file,
461 int trigger_enable)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500462{
463 int ret = 0;
464
465 if (trigger_enable) {
466 if (atomic_inc_return(&file->tm_ref) > 1)
467 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400468 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500469 ret = trace_event_enable_disable(file, 1, 1);
470 } else {
471 if (atomic_dec_return(&file->tm_ref) > 0)
472 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400473 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500474 ret = trace_event_enable_disable(file, 0, 1);
475 }
476
477 return ret;
478}
479
480/**
481 * clear_event_triggers - Clear all triggers associated with a trace array
482 * @tr: The trace array to clear
483 *
484 * For each trigger, the triggering event has its tm_ref decremented
485 * via trace_event_trigger_enable_disable(), and any associated event
486 * (in the case of enable/disable_event triggers) will have its sm_ref
487 * decremented via free()->trace_event_enable_disable(). That
488 * combination effectively reverses the soft-mode/trigger state added
489 * by trigger registration.
490 *
491 * Must be called with event_mutex held.
492 */
493void
494clear_event_triggers(struct trace_array *tr)
495{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400496 struct trace_event_file *file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500497
498 list_for_each_entry(file, &tr->events, list) {
Steven Rostedt (VMware)86b389f2018-05-27 20:54:44 -0400499 struct event_trigger_data *data, *n;
500 list_for_each_entry_safe(data, n, &file->triggers, list) {
Tom Zanussi85f2b082013-10-24 08:59:24 -0500501 trace_event_trigger_enable_disable(file, 0);
Steven Rostedt (VMware)86b389f2018-05-27 20:54:44 -0400502 list_del_rcu(&data->list);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500503 if (data->ops->free)
504 data->ops->free(data->ops, data);
505 }
506 }
507}
508
Tom Zanussi2a2df322013-10-24 08:59:25 -0500509/**
Tom Zanussibac5fb92013-10-24 08:59:29 -0500510 * update_cond_flag - Set or reset the TRIGGER_COND bit
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400511 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -0500512 *
513 * If an event has triggers and any of those triggers has a filter or
514 * a post_trigger, trigger invocation needs to be deferred until after
515 * the current event has logged its data, and the event should have
516 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
517 * cleared.
518 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600519void update_cond_flag(struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -0500520{
521 struct event_trigger_data *data;
522 bool set_cond = false;
523
Masami Hiramatsu3b42a4c2019-12-20 11:31:43 +0900524 lockdep_assert_held(&event_mutex);
525
526 list_for_each_entry(data, &file->triggers, list) {
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -0500527 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
528 event_command_needs_rec(data->cmd_ops)) {
Tom Zanussibac5fb92013-10-24 08:59:29 -0500529 set_cond = true;
530 break;
531 }
532 }
533
534 if (set_cond)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400535 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500536 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400537 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500538}
539
540/**
Tom Zanussi2a2df322013-10-24 08:59:25 -0500541 * register_trigger - Generic event_command @reg implementation
542 * @glob: The raw string used to register the trigger
Tom Zanussi2a2df322013-10-24 08:59:25 -0500543 * @data: Trigger-specific data to associate with the trigger
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400544 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500545 *
546 * Common implementation for event trigger registration.
547 *
548 * Usually used directly as the @reg method in event command
549 * implementations.
550 *
551 * Return: 0 on success, errno otherwise
552 */
Tom Zanussi2378a2d2022-01-10 08:04:13 -0600553static int register_trigger(char *glob,
Tom Zanussi2a2df322013-10-24 08:59:25 -0500554 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400555 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500556{
557 struct event_trigger_data *test;
558 int ret = 0;
559
Masami Hiramatsu3b42a4c2019-12-20 11:31:43 +0900560 lockdep_assert_held(&event_mutex);
561
562 list_for_each_entry(test, &file->triggers, list) {
Tom Zanussi2a2df322013-10-24 08:59:25 -0500563 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
564 ret = -EEXIST;
565 goto out;
566 }
567 }
568
569 if (data->ops->init) {
570 ret = data->ops->init(data->ops, data);
571 if (ret < 0)
572 goto out;
573 }
574
575 list_add_rcu(&data->list, &file->triggers);
576 ret++;
577
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600578 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500579 if (trace_event_trigger_enable_disable(file, 1) < 0) {
580 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600581 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500582 ret--;
583 }
584out:
585 return ret;
586}
587
588/**
589 * unregister_trigger - Generic event_command @unreg implementation
590 * @glob: The raw string used to register the trigger
Tom Zanussi2a2df322013-10-24 08:59:25 -0500591 * @test: Trigger-specific data used to find the trigger to remove
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400592 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500593 *
594 * Common implementation for event trigger unregistration.
595 *
596 * Usually used directly as the @unreg method in event command
597 * implementations.
598 */
Tom Zanussi2378a2d2022-01-10 08:04:13 -0600599static void unregister_trigger(char *glob,
Steven Rostedt (VMware)f6b74252018-07-24 18:55:58 -0400600 struct event_trigger_data *test,
601 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500602{
603 struct event_trigger_data *data;
604 bool unregistered = false;
605
Masami Hiramatsu3b42a4c2019-12-20 11:31:43 +0900606 lockdep_assert_held(&event_mutex);
607
608 list_for_each_entry(data, &file->triggers, list) {
Tom Zanussi2a2df322013-10-24 08:59:25 -0500609 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
610 unregistered = true;
611 list_del_rcu(&data->list);
612 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600613 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500614 break;
615 }
616 }
617
618 if (unregistered && data->ops->free)
619 data->ops->free(data->ops, data);
620}
621
Tom Zanussi86599db2022-01-10 08:04:14 -0600622/*
623 * Event trigger parsing helper functions.
624 *
625 * These functions help make it easier to write an event trigger
626 * parsing function i.e. the struct event_command.parse() callback
627 * function responsible for parsing and registering a trigger command
628 * written to the 'trigger' file.
629 *
630 * A trigger command (or just 'trigger' for short) takes the form:
631 * [trigger] [if filter]
632 *
633 * The struct event_command.parse() callback (and other struct
634 * event_command functions) refer to several components of a trigger
635 * command. Those same components are referenced by the event trigger
636 * parsing helper functions defined below. These components are:
637 *
638 * cmd - the trigger command name
639 * glob - the trigger command name optionally prefaced with '!'
640 * param_and_filter - text following cmd and ':'
641 * param - text following cmd and ':' and stripped of filter
642 * filter - the optional filter text following (and including) 'if'
643 *
644 * To illustrate the use of these componenents, here are some concrete
645 * examples. For the following triggers:
646 *
647 * echo 'traceon:5 if pid == 0' > trigger
648 * - 'traceon' is both cmd and glob
649 * - '5 if pid == 0' is the param_and_filter
650 * - '5' is the param
651 * - 'if pid == 0' is the filter
652 *
653 * echo 'enable_event:sys:event:n' > trigger
654 * - 'enable_event' is both cmd and glob
655 * - 'sys:event:n' is the param_and_filter
656 * - 'sys:event:n' is the param
657 * - there is no filter
658 *
659 * echo 'hist:keys=pid if prio > 50' > trigger
660 * - 'hist' is both cmd and glob
661 * - 'keys=pid if prio > 50' is the param_and_filter
662 * - 'keys=pid' is the param
663 * - 'if prio > 50' is the filter
664 *
665 * echo '!enable_event:sys:event:n' > trigger
666 * - 'enable_event' the cmd
667 * - '!enable_event' is the glob
668 * - 'sys:event:n' is the param_and_filter
669 * - 'sys:event:n' is the param
670 * - there is no filter
671 *
672 * echo 'traceoff' > trigger
673 * - 'traceoff' is both cmd and glob
674 * - there is no param_and_filter
675 * - there is no param
676 * - there is no filter
677 *
678 * There are a few different categories of event trigger covered by
679 * these helpers:
680 *
681 * - triggers that don't require a parameter e.g. traceon
682 * - triggers that do require a parameter e.g. enable_event and hist
683 * - triggers that though they may not require a param may support an
684 * optional 'n' param (n = number of times the trigger should fire)
685 * e.g.: traceon:5 or enable_event:sys:event:n
686 * - triggers that do not support an 'n' param e.g. hist
687 *
688 * These functions can be used or ignored as necessary - it all
689 * depends on the complexity of the trigger, and the granularity of
690 * the functions supported reflects the fact that some implementations
691 * may need to customize certain aspects of their implementations and
692 * won't need certain functions. For instance, the hist trigger
693 * implementation doesn't use event_trigger_separate_filter() because
694 * it has special requirements for handling the filter.
695 */
696
697/**
698 * event_trigger_check_remove - check whether an event trigger specifies remove
699 * @glob: The trigger command string, with optional remove(!) operator
700 *
701 * The event trigger callback implementations pass in 'glob' as a
702 * parameter. This is the command name either with or without a
703 * remove(!) operator. This function simply parses the glob and
704 * determines whether the command corresponds to a trigger removal or
705 * a trigger addition.
706 *
707 * Return: true if this is a remove command, false otherwise
708 */
709bool event_trigger_check_remove(const char *glob)
710{
711 return (glob && glob[0] == '!') ? true : false;
712}
713
714/**
715 * event_trigger_empty_param - check whether the param is empty
716 * @param: The trigger param string
717 *
718 * The event trigger callback implementations pass in 'param' as a
719 * parameter. This corresponds to the string following the command
720 * name minus the command name. This function can be called by a
721 * callback implementation for any command that requires a param; a
722 * callback that doesn't require a param can ignore it.
723 *
724 * Return: true if this is an empty param, false otherwise
725 */
726bool event_trigger_empty_param(const char *param)
727{
728 return !param;
729}
730
731/**
732 * event_trigger_separate_filter - separate an event trigger from a filter
733 * @param: The param string containing trigger and possibly filter
734 * @trigger: outparam, will be filled with a pointer to the trigger
735 * @filter: outparam, will be filled with a pointer to the filter
736 * @param_required: Specifies whether or not the param string is required
737 *
738 * Given a param string of the form '[trigger] [if filter]', this
739 * function separates the filter from the trigger and returns the
740 * trigger in *trigger and the filter in *filter. Either the *trigger
741 * or the *filter may be set to NULL by this function - if not set to
742 * NULL, they will contain strings corresponding to the trigger and
743 * filter.
744 *
745 * There are two cases that need to be handled with respect to the
746 * passed-in param: either the param is required, or it is not
747 * required. If @param_required is set, and there's no param, it will
748 * return -EINVAL. If @param_required is not set and there's a param
749 * that starts with a number, that corresponds to the case of a
750 * trigger with :n (n = number of times the trigger should fire) and
751 * the parsing continues normally; otherwise the function just returns
752 * and assumes param just contains a filter and there's nothing else
753 * to do.
754 *
755 * Return: 0 on success, errno otherwise
756 */
757int event_trigger_separate_filter(char *param_and_filter, char **param,
758 char **filter, bool param_required)
759{
760 int ret = 0;
761
762 *param = *filter = NULL;
763
764 if (!param_and_filter) {
765 if (param_required)
766 ret = -EINVAL;
767 goto out;
768 }
769
770 /*
771 * Here we check for an optional param. The only legal
772 * optional param is :n, and if that's the case, continue
773 * below. Otherwise we assume what's left is a filter and
774 * return it as the filter string for the caller to deal with.
775 */
776 if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
777 *filter = param_and_filter;
778 goto out;
779 }
780
781 /*
782 * Separate the param from the filter (param [if filter]).
783 * Here we have either an optional :n param or a required
784 * param and an optional filter.
785 */
786 *param = strsep(&param_and_filter, " \t");
787
788 /*
789 * Here we have a filter, though it may be empty.
790 */
791 if (param_and_filter) {
792 *filter = skip_spaces(param_and_filter);
793 if (!**filter)
794 *filter = NULL;
795 }
796out:
797 return ret;
798}
799
800/**
801 * event_trigger_alloc - allocate and init event_trigger_data for a trigger
802 * @cmd_ops: The event_command operations for the trigger
803 * @cmd: The cmd string
804 * @param: The param string
805 * @private_data: User data to associate with the event trigger
806 *
807 * Allocate an event_trigger_data instance and initialize it. The
808 * @cmd_ops are used along with the @cmd and @param to get the
809 * trigger_ops to assign to the event_trigger_data. @private_data can
810 * also be passed in and associated with the event_trigger_data.
811 *
812 * Use event_trigger_free() to free an event_trigger_data object.
813 *
814 * Return: The trigger_data object success, NULL otherwise
815 */
816struct event_trigger_data *event_trigger_alloc(struct event_command *cmd_ops,
817 char *cmd,
818 char *param,
819 void *private_data)
820{
821 struct event_trigger_data *trigger_data;
822 struct event_trigger_ops *trigger_ops;
823
824 trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
825
826 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
827 if (!trigger_data)
828 return NULL;
829
830 trigger_data->count = -1;
831 trigger_data->ops = trigger_ops;
832 trigger_data->cmd_ops = cmd_ops;
833 trigger_data->private_data = private_data;
834
835 INIT_LIST_HEAD(&trigger_data->list);
836 INIT_LIST_HEAD(&trigger_data->named_list);
837 RCU_INIT_POINTER(trigger_data->filter, NULL);
838
839 return trigger_data;
840}
841
842/**
843 * event_trigger_parse_num - parse and return the number param for a trigger
844 * @param: The param string
845 * @trigger_data: The trigger_data for the trigger
846 *
847 * Parse the :n (n = number of times the trigger should fire) param
848 * and set the count variable in the trigger_data to the parsed count.
849 *
850 * Return: 0 on success, errno otherwise
851 */
852int event_trigger_parse_num(char *param,
853 struct event_trigger_data *trigger_data)
854{
855 char *number;
856 int ret = 0;
857
858 if (param) {
859 number = strsep(&param, ":");
860
861 if (!strlen(number))
862 return -EINVAL;
863
864 /*
865 * We use the callback data field (which is a pointer)
866 * as our counter.
867 */
868 ret = kstrtoul(number, 0, &trigger_data->count);
869 }
870
871 return ret;
872}
873
874/**
875 * event_trigger_set_filter - set an event trigger's filter
876 * @cmd_ops: The event_command operations for the trigger
877 * @file: The event file for the trigger's event
878 * @param: The string containing the filter
879 * @trigger_data: The trigger_data for the trigger
880 *
881 * Set the filter for the trigger. If the filter is NULL, just return
882 * without error.
883 *
884 * Return: 0 on success, errno otherwise
885 */
886int event_trigger_set_filter(struct event_command *cmd_ops,
887 struct trace_event_file *file,
888 char *param,
889 struct event_trigger_data *trigger_data)
890{
891 if (param && cmd_ops->set_filter)
892 return cmd_ops->set_filter(param, trigger_data, file);
893
894 return 0;
895}
896
897/**
898 * event_trigger_reset_filter - reset an event trigger's filter
899 * @cmd_ops: The event_command operations for the trigger
900 * @trigger_data: The trigger_data for the trigger
901 *
902 * Reset the filter for the trigger to no filter.
903 */
904void event_trigger_reset_filter(struct event_command *cmd_ops,
905 struct event_trigger_data *trigger_data)
906{
907 if (cmd_ops->set_filter)
908 cmd_ops->set_filter(NULL, trigger_data, NULL);
909}
910
911/**
912 * event_trigger_register - register an event trigger
913 * @cmd_ops: The event_command operations for the trigger
914 * @file: The event file for the trigger's event
915 * @glob: The trigger command string, with optional remove(!) operator
916 * @cmd: The cmd string
917 * @param: The param string
918 * @trigger_data: The trigger_data for the trigger
919 * @n_registered: optional outparam, the number of triggers registered
920 *
921 * Register an event trigger. The @cmd_ops are used to call the
922 * cmd_ops->reg() function which actually does the registration. The
923 * cmd_ops->reg() function returns the number of triggers registered,
924 * which is assigned to n_registered, if n_registered is non-NULL.
925 *
926 * Return: 0 on success, errno otherwise
927 */
928int event_trigger_register(struct event_command *cmd_ops,
929 struct trace_event_file *file,
930 char *glob,
931 char *cmd,
932 char *param,
933 struct event_trigger_data *trigger_data,
934 int *n_registered)
935{
936 int ret;
937
938 if (n_registered)
939 *n_registered = 0;
940
941 ret = cmd_ops->reg(glob, trigger_data, file);
942 /*
943 * The above returns on success the # of functions enabled,
944 * but if it didn't find any functions it returns zero.
945 * Consider no functions a failure too.
946 */
947 if (!ret) {
948 cmd_ops->unreg(glob, trigger_data, file);
949 ret = -ENOENT;
950 } else if (ret > 0) {
951 if (n_registered)
952 *n_registered = ret;
953 /* Just return zero, not the number of enabled functions */
954 ret = 0;
955 }
956
957 return ret;
958}
959
960/*
961 * End event trigger parsing helper functions.
962 */
963
Tom Zanussi2a2df322013-10-24 08:59:25 -0500964/**
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -0600965 * event_trigger_parse - Generic event_command @parse implementation
Tom Zanussi2a2df322013-10-24 08:59:25 -0500966 * @cmd_ops: The command ops, used for trigger registration
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400967 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500968 * @glob: The raw string used to register the trigger
969 * @cmd: The cmd portion of the string used to register the trigger
970 * @param: The params portion of the string used to register the trigger
971 *
972 * Common implementation for event command parsing and trigger
973 * instantiation.
974 *
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -0600975 * Usually used directly as the @parse method in event command
Tom Zanussi2a2df322013-10-24 08:59:25 -0500976 * implementations.
977 *
978 * Return: 0 on success, errno otherwise
979 */
980static int
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -0600981event_trigger_parse(struct event_command *cmd_ops,
982 struct trace_event_file *file,
983 char *glob, char *cmd, char *param)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500984{
985 struct event_trigger_data *trigger_data;
986 struct event_trigger_ops *trigger_ops;
987 char *trigger = NULL;
988 char *number;
989 int ret;
990
991 /* separate the trigger from the filter (t:n [if filter]) */
Masami Hiramatsu6784bea2020-06-20 12:46:03 +0900992 if (param && isdigit(param[0])) {
Tom Zanussi2a2df322013-10-24 08:59:25 -0500993 trigger = strsep(&param, " \t");
Masami Hiramatsu6784bea2020-06-20 12:46:03 +0900994 if (param) {
995 param = skip_spaces(param);
996 if (!*param)
997 param = NULL;
998 }
999 }
Tom Zanussi2a2df322013-10-24 08:59:25 -05001000
1001 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1002
1003 ret = -ENOMEM;
1004 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1005 if (!trigger_data)
1006 goto out;
1007
1008 trigger_data->count = -1;
1009 trigger_data->ops = trigger_ops;
1010 trigger_data->cmd_ops = cmd_ops;
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001011 trigger_data->private_data = file;
Tom Zanussi2a2df322013-10-24 08:59:25 -05001012 INIT_LIST_HEAD(&trigger_data->list);
Tom Zanussidb1388b2016-03-03 12:54:58 -06001013 INIT_LIST_HEAD(&trigger_data->named_list);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001014
1015 if (glob[0] == '!') {
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001016 cmd_ops->unreg(glob+1, trigger_data, file);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001017 kfree(trigger_data);
1018 ret = 0;
1019 goto out;
1020 }
1021
1022 if (trigger) {
1023 number = strsep(&trigger, ":");
1024
1025 ret = -EINVAL;
1026 if (!strlen(number))
1027 goto out_free;
1028
1029 /*
1030 * We use the callback data field (which is a pointer)
1031 * as our counter.
1032 */
1033 ret = kstrtoul(number, 0, &trigger_data->count);
1034 if (ret)
1035 goto out_free;
1036 }
1037
1038 if (!param) /* if param is non-empty, it's supposed to be a filter */
1039 goto out_reg;
1040
1041 if (!cmd_ops->set_filter)
1042 goto out_reg;
1043
1044 ret = cmd_ops->set_filter(param, trigger_data, file);
1045 if (ret < 0)
1046 goto out_free;
1047
1048 out_reg:
Steven Rostedt (VMware)1863c382018-07-24 19:13:31 -04001049 /* Up the trigger_data count to make sure reg doesn't free it on failure */
1050 event_trigger_init(trigger_ops, trigger_data);
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001051 ret = cmd_ops->reg(glob, trigger_data, file);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001052 /*
1053 * The above returns on success the # of functions enabled,
1054 * but if it didn't find any functions it returns zero.
1055 * Consider no functions a failure too.
1056 */
1057 if (!ret) {
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001058 cmd_ops->unreg(glob, trigger_data, file);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001059 ret = -ENOENT;
Steven Rostedt (VMware)1863c382018-07-24 19:13:31 -04001060 } else if (ret > 0)
1061 ret = 0;
1062
1063 /* Down the counter of trigger_data or free it if not used anymore */
1064 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001065 out:
1066 return ret;
1067
1068 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -05001069 if (cmd_ops->set_filter)
1070 cmd_ops->set_filter(NULL, trigger_data, NULL);
Tom Zanussi2a2df322013-10-24 08:59:25 -05001071 kfree(trigger_data);
1072 goto out;
1073}
1074
Tom Zanussibac5fb92013-10-24 08:59:29 -05001075/**
1076 * set_trigger_filter - Generic event_command @set_filter implementation
1077 * @filter_str: The filter string for the trigger, NULL to remove filter
1078 * @trigger_data: Trigger-specific data
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001079 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -05001080 *
1081 * Common implementation for event command filter parsing and filter
1082 * instantiation.
1083 *
1084 * Usually used directly as the @set_filter method in event command
1085 * implementations.
1086 *
1087 * Also used to remove a filter (if filter_str = NULL).
1088 *
1089 * Return: 0 on success, errno otherwise
1090 */
Tom Zanussiab4bf002015-12-10 12:50:44 -06001091int set_trigger_filter(char *filter_str,
1092 struct event_trigger_data *trigger_data,
1093 struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -05001094{
1095 struct event_trigger_data *data = trigger_data;
1096 struct event_filter *filter = NULL, *tmp;
1097 int ret = -EINVAL;
1098 char *s;
1099
1100 if (!filter_str) /* clear the current filter */
1101 goto assign;
1102
1103 s = strsep(&filter_str, " \t");
1104
1105 if (!strlen(s) || strcmp(s, "if") != 0)
1106 goto out;
1107
1108 if (!filter_str)
1109 goto out;
1110
1111 /* The filter is for the 'trigger' event, not the triggered event */
Steven Rostedt (VMware)1e144d72019-04-01 16:07:48 -04001112 ret = create_event_filter(file->tr, file->event_call,
1113 filter_str, false, &filter);
Steven Rostedt (VMware)3cec6382018-12-09 21:17:30 -05001114 /*
1115 * If create_event_filter() fails, filter still needs to be freed.
1116 * Which the calling code will do with data->filter.
1117 */
Tom Zanussibac5fb92013-10-24 08:59:29 -05001118 assign:
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -05001119 tmp = rcu_access_pointer(data->filter);
Tom Zanussibac5fb92013-10-24 08:59:29 -05001120
1121 rcu_assign_pointer(data->filter, filter);
1122
1123 if (tmp) {
1124 /* Make sure the call is done with the filter */
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -04001125 tracepoint_synchronize_unregister();
Tom Zanussibac5fb92013-10-24 08:59:29 -05001126 free_event_filter(tmp);
1127 }
1128
1129 kfree(data->filter_str);
1130 data->filter_str = NULL;
1131
1132 if (filter_str) {
1133 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
1134 if (!data->filter_str) {
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -05001135 free_event_filter(rcu_access_pointer(data->filter));
Tom Zanussibac5fb92013-10-24 08:59:29 -05001136 data->filter = NULL;
1137 ret = -ENOMEM;
1138 }
1139 }
1140 out:
1141 return ret;
1142}
1143
Tom Zanussidb1388b2016-03-03 12:54:58 -06001144static LIST_HEAD(named_triggers);
1145
1146/**
1147 * find_named_trigger - Find the common named trigger associated with @name
1148 * @name: The name of the set of named triggers to find the common data for
1149 *
1150 * Named triggers are sets of triggers that share a common set of
1151 * trigger data. The first named trigger registered with a given name
1152 * owns the common trigger data that the others subsequently
1153 * registered with the same name will reference. This function
1154 * returns the common trigger data associated with that first
1155 * registered instance.
1156 *
1157 * Return: the common trigger data for the given named trigger on
1158 * success, NULL otherwise.
1159 */
1160struct event_trigger_data *find_named_trigger(const char *name)
1161{
1162 struct event_trigger_data *data;
1163
1164 if (!name)
1165 return NULL;
1166
1167 list_for_each_entry(data, &named_triggers, named_list) {
1168 if (data->named_data)
1169 continue;
1170 if (strcmp(data->name, name) == 0)
1171 return data;
1172 }
1173
1174 return NULL;
1175}
1176
1177/**
1178 * is_named_trigger - determine if a given trigger is a named trigger
1179 * @test: The trigger data to test
1180 *
1181 * Return: true if 'test' is a named trigger, false otherwise.
1182 */
1183bool is_named_trigger(struct event_trigger_data *test)
1184{
1185 struct event_trigger_data *data;
1186
1187 list_for_each_entry(data, &named_triggers, named_list) {
1188 if (test == data)
1189 return true;
1190 }
1191
1192 return false;
1193}
1194
1195/**
1196 * save_named_trigger - save the trigger in the named trigger list
1197 * @name: The name of the named trigger set
1198 * @data: The trigger data to save
1199 *
1200 * Return: 0 if successful, negative error otherwise.
1201 */
1202int save_named_trigger(const char *name, struct event_trigger_data *data)
1203{
1204 data->name = kstrdup(name, GFP_KERNEL);
1205 if (!data->name)
1206 return -ENOMEM;
1207
1208 list_add(&data->named_list, &named_triggers);
1209
1210 return 0;
1211}
1212
1213/**
1214 * del_named_trigger - delete a trigger from the named trigger list
1215 * @data: The trigger data to delete
1216 */
1217void del_named_trigger(struct event_trigger_data *data)
1218{
1219 kfree(data->name);
1220 data->name = NULL;
1221
1222 list_del(&data->named_list);
1223}
1224
1225static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
1226{
1227 struct event_trigger_data *test;
1228
1229 list_for_each_entry(test, &named_triggers, named_list) {
1230 if (strcmp(test->name, data->name) == 0) {
1231 if (pause) {
1232 test->paused_tmp = test->paused;
1233 test->paused = true;
1234 } else {
1235 test->paused = test->paused_tmp;
1236 }
1237 }
1238 }
1239}
1240
1241/**
1242 * pause_named_trigger - Pause all named triggers with the same name
1243 * @data: The trigger data of a named trigger to pause
1244 *
1245 * Pauses a named trigger along with all other triggers having the
1246 * same name. Because named triggers share a common set of data,
1247 * pausing only one is meaningless, so pausing one named trigger needs
1248 * to pause all triggers with the same name.
1249 */
1250void pause_named_trigger(struct event_trigger_data *data)
1251{
1252 __pause_named_trigger(data, true);
1253}
1254
1255/**
1256 * unpause_named_trigger - Un-pause all named triggers with the same name
1257 * @data: The trigger data of a named trigger to unpause
1258 *
1259 * Un-pauses a named trigger along with all other triggers having the
1260 * same name. Because named triggers share a common set of data,
1261 * unpausing only one is meaningless, so unpausing one named trigger
1262 * needs to unpause all triggers with the same name.
1263 */
1264void unpause_named_trigger(struct event_trigger_data *data)
1265{
1266 __pause_named_trigger(data, false);
1267}
1268
1269/**
1270 * set_named_trigger_data - Associate common named trigger data
Qiujun Huang099dcc12021-05-15 10:57:35 +00001271 * @data: The trigger data to associate
1272 * @named_data: The common named trigger to be associated
Tom Zanussidb1388b2016-03-03 12:54:58 -06001273 *
1274 * Named triggers are sets of triggers that share a common set of
1275 * trigger data. The first named trigger registered with a given name
1276 * owns the common trigger data that the others subsequently
1277 * registered with the same name will reference. This function
1278 * associates the common trigger data from the first trigger with the
1279 * given trigger.
1280 */
1281void set_named_trigger_data(struct event_trigger_data *data,
1282 struct event_trigger_data *named_data)
1283{
1284 data->named_data = named_data;
1285}
1286
Tom Zanussi067fe032018-01-15 20:51:56 -06001287struct event_trigger_data *
1288get_named_trigger_data(struct event_trigger_data *data)
1289{
1290 return data->named_data;
1291}
1292
Tom Zanussi2a2df322013-10-24 08:59:25 -05001293static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001294traceon_trigger(struct event_trigger_data *data,
1295 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001296 struct ring_buffer_event *event)
Tom Zanussi2a2df322013-10-24 08:59:25 -05001297{
1298 if (tracing_is_on())
1299 return;
1300
1301 tracing_on();
1302}
1303
1304static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001305traceon_count_trigger(struct event_trigger_data *data,
1306 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001307 struct ring_buffer_event *event)
Tom Zanussi2a2df322013-10-24 08:59:25 -05001308{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -05001309 if (tracing_is_on())
1310 return;
1311
Tom Zanussi2a2df322013-10-24 08:59:25 -05001312 if (!data->count)
1313 return;
1314
1315 if (data->count != -1)
1316 (data->count)--;
1317
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -05001318 tracing_on();
Tom Zanussi2a2df322013-10-24 08:59:25 -05001319}
1320
1321static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001322traceoff_trigger(struct event_trigger_data *data,
1323 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001324 struct ring_buffer_event *event)
Tom Zanussi2a2df322013-10-24 08:59:25 -05001325{
1326 if (!tracing_is_on())
1327 return;
1328
1329 tracing_off();
1330}
1331
1332static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001333traceoff_count_trigger(struct event_trigger_data *data,
1334 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001335 struct ring_buffer_event *event)
Tom Zanussi2a2df322013-10-24 08:59:25 -05001336{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -05001337 if (!tracing_is_on())
1338 return;
1339
Tom Zanussi2a2df322013-10-24 08:59:25 -05001340 if (!data->count)
1341 return;
1342
1343 if (data->count != -1)
1344 (data->count)--;
1345
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -05001346 tracing_off();
Tom Zanussi2a2df322013-10-24 08:59:25 -05001347}
1348
1349static int
1350traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1351 struct event_trigger_data *data)
1352{
1353 return event_trigger_print("traceon", m, (void *)data->count,
1354 data->filter_str);
1355}
1356
1357static int
1358traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1359 struct event_trigger_data *data)
1360{
1361 return event_trigger_print("traceoff", m, (void *)data->count,
1362 data->filter_str);
1363}
1364
1365static struct event_trigger_ops traceon_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001366 .trigger = traceon_trigger,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001367 .print = traceon_trigger_print,
1368 .init = event_trigger_init,
1369 .free = event_trigger_free,
1370};
1371
1372static struct event_trigger_ops traceon_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001373 .trigger = traceon_count_trigger,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001374 .print = traceon_trigger_print,
1375 .init = event_trigger_init,
1376 .free = event_trigger_free,
1377};
1378
1379static struct event_trigger_ops traceoff_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001380 .trigger = traceoff_trigger,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001381 .print = traceoff_trigger_print,
1382 .init = event_trigger_init,
1383 .free = event_trigger_free,
1384};
1385
1386static struct event_trigger_ops traceoff_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001387 .trigger = traceoff_count_trigger,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001388 .print = traceoff_trigger_print,
1389 .init = event_trigger_init,
1390 .free = event_trigger_free,
1391};
1392
1393static struct event_trigger_ops *
1394onoff_get_trigger_ops(char *cmd, char *param)
1395{
1396 struct event_trigger_ops *ops;
1397
1398 /* we register both traceon and traceoff to this callback */
1399 if (strcmp(cmd, "traceon") == 0)
1400 ops = param ? &traceon_count_trigger_ops :
1401 &traceon_trigger_ops;
1402 else
1403 ops = param ? &traceoff_count_trigger_ops :
1404 &traceoff_trigger_ops;
1405
1406 return ops;
1407}
1408
1409static struct event_command trigger_traceon_cmd = {
1410 .name = "traceon",
1411 .trigger_type = ETT_TRACE_ONOFF,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001412 .parse = event_trigger_parse,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001413 .reg = register_trigger,
1414 .unreg = unregister_trigger,
1415 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001416 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001417};
1418
1419static struct event_command trigger_traceoff_cmd = {
1420 .name = "traceoff",
1421 .trigger_type = ETT_TRACE_ONOFF,
Masami Hiramatsua0d0c622016-09-09 01:05:45 +09001422 .flags = EVENT_CMD_FL_POST_TRIGGER,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001423 .parse = event_trigger_parse,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001424 .reg = register_trigger,
1425 .unreg = unregister_trigger,
1426 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001427 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001428};
1429
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001430#ifdef CONFIG_TRACER_SNAPSHOT
1431static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001432snapshot_trigger(struct event_trigger_data *data,
1433 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001434 struct ring_buffer_event *event)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001435{
Steven Rostedt (VMware)2824f502018-05-28 10:56:36 -04001436 struct trace_event_file *file = data->private_data;
1437
1438 if (file)
1439 tracing_snapshot_instance(file->tr);
1440 else
1441 tracing_snapshot();
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001442}
1443
1444static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001445snapshot_count_trigger(struct event_trigger_data *data,
1446 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001447 struct ring_buffer_event *event)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001448{
1449 if (!data->count)
1450 return;
1451
1452 if (data->count != -1)
1453 (data->count)--;
1454
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001455 snapshot_trigger(data, buffer, rec, event);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001456}
1457
1458static int
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001459register_snapshot_trigger(char *glob,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001460 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001461 struct trace_event_file *file)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001462{
Xiao Yang0bbe7f72020-04-14 09:51:45 +08001463 if (tracing_alloc_snapshot_instance(file->tr) != 0)
1464 return 0;
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001465
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001466 return register_trigger(glob, data, file);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001467}
1468
1469static int
1470snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1471 struct event_trigger_data *data)
1472{
1473 return event_trigger_print("snapshot", m, (void *)data->count,
1474 data->filter_str);
1475}
1476
1477static struct event_trigger_ops snapshot_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001478 .trigger = snapshot_trigger,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001479 .print = snapshot_trigger_print,
1480 .init = event_trigger_init,
1481 .free = event_trigger_free,
1482};
1483
1484static struct event_trigger_ops snapshot_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001485 .trigger = snapshot_count_trigger,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001486 .print = snapshot_trigger_print,
1487 .init = event_trigger_init,
1488 .free = event_trigger_free,
1489};
1490
1491static struct event_trigger_ops *
1492snapshot_get_trigger_ops(char *cmd, char *param)
1493{
1494 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1495}
1496
1497static struct event_command trigger_snapshot_cmd = {
1498 .name = "snapshot",
1499 .trigger_type = ETT_SNAPSHOT,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001500 .parse = event_trigger_parse,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001501 .reg = register_snapshot_trigger,
1502 .unreg = unregister_trigger,
1503 .get_trigger_ops = snapshot_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001504 .set_filter = set_trigger_filter,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001505};
1506
1507static __init int register_trigger_snapshot_cmd(void)
1508{
1509 int ret;
1510
1511 ret = register_event_command(&trigger_snapshot_cmd);
1512 WARN_ON(ret < 0);
1513
1514 return ret;
1515}
1516#else
1517static __init int register_trigger_snapshot_cmd(void) { return 0; }
1518#endif /* CONFIG_TRACER_SNAPSHOT */
1519
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001520#ifdef CONFIG_STACKTRACE
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05001521#ifdef CONFIG_UNWINDER_ORC
1522/* Skip 2:
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001523 * event_triggers_post_call()
Steven Rostedt (Red Hat)a7237762015-05-13 15:27:47 -04001524 * trace_event_raw_event_xxx()
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001525 */
Steven Rostedt (VMware)2ee5b922018-01-23 13:25:04 -05001526# define STACK_SKIP 2
1527#else
1528/*
1529 * Skip 4:
1530 * stacktrace_trigger()
1531 * event_triggers_post_call()
1532 * trace_event_buffer_commit()
1533 * trace_event_raw_event_xxx()
1534 */
1535#define STACK_SKIP 4
1536#endif
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001537
1538static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001539stacktrace_trigger(struct event_trigger_data *data,
1540 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001541 struct ring_buffer_event *event)
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001542{
1543 trace_dump_stack(STACK_SKIP);
1544}
1545
1546static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001547stacktrace_count_trigger(struct event_trigger_data *data,
1548 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001549 struct ring_buffer_event *event)
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001550{
1551 if (!data->count)
1552 return;
1553
1554 if (data->count != -1)
1555 (data->count)--;
1556
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001557 stacktrace_trigger(data, buffer, rec, event);
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001558}
1559
1560static int
1561stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1562 struct event_trigger_data *data)
1563{
1564 return event_trigger_print("stacktrace", m, (void *)data->count,
1565 data->filter_str);
1566}
1567
1568static struct event_trigger_ops stacktrace_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001569 .trigger = stacktrace_trigger,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001570 .print = stacktrace_trigger_print,
1571 .init = event_trigger_init,
1572 .free = event_trigger_free,
1573};
1574
1575static struct event_trigger_ops stacktrace_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001576 .trigger = stacktrace_count_trigger,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001577 .print = stacktrace_trigger_print,
1578 .init = event_trigger_init,
1579 .free = event_trigger_free,
1580};
1581
1582static struct event_trigger_ops *
1583stacktrace_get_trigger_ops(char *cmd, char *param)
1584{
1585 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1586}
1587
1588static struct event_command trigger_stacktrace_cmd = {
1589 .name = "stacktrace",
1590 .trigger_type = ETT_STACKTRACE,
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -05001591 .flags = EVENT_CMD_FL_POST_TRIGGER,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001592 .parse = event_trigger_parse,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001593 .reg = register_trigger,
1594 .unreg = unregister_trigger,
1595 .get_trigger_ops = stacktrace_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001596 .set_filter = set_trigger_filter,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001597};
1598
1599static __init int register_trigger_stacktrace_cmd(void)
1600{
1601 int ret;
1602
1603 ret = register_event_command(&trigger_stacktrace_cmd);
1604 WARN_ON(ret < 0);
1605
1606 return ret;
1607}
1608#else
1609static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1610#endif /* CONFIG_STACKTRACE */
1611
Tom Zanussi2a2df322013-10-24 08:59:25 -05001612static __init void unregister_trigger_traceon_traceoff_cmds(void)
1613{
1614 unregister_event_command(&trigger_traceon_cmd);
1615 unregister_event_command(&trigger_traceoff_cmd);
1616}
1617
Tom Zanussi7862ad12013-10-24 08:59:28 -05001618static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001619event_enable_trigger(struct event_trigger_data *data,
1620 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001621 struct ring_buffer_event *event)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001622{
1623 struct enable_trigger_data *enable_data = data->private_data;
1624
1625 if (enable_data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001626 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001627 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001628 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001629}
1630
1631static void
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001632event_enable_count_trigger(struct event_trigger_data *data,
1633 struct trace_buffer *buffer, void *rec,
Tom Zanussi1ac4f512018-01-15 20:51:42 -06001634 struct ring_buffer_event *event)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001635{
1636 struct enable_trigger_data *enable_data = data->private_data;
1637
1638 if (!data->count)
1639 return;
1640
1641 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001642 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussi7862ad12013-10-24 08:59:28 -05001643 return;
1644
1645 if (data->count != -1)
1646 (data->count)--;
1647
Steven Rostedt (VMware)b47e3302021-03-16 12:41:03 -04001648 event_enable_trigger(data, buffer, rec, event);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001649}
1650
Tom Zanussid0bad492016-03-03 12:54:55 -06001651int event_enable_trigger_print(struct seq_file *m,
1652 struct event_trigger_ops *ops,
1653 struct event_trigger_data *data)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001654{
1655 struct enable_trigger_data *enable_data = data->private_data;
1656
1657 seq_printf(m, "%s:%s:%s",
Tom Zanussid0bad492016-03-03 12:54:55 -06001658 enable_data->hist ?
1659 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1660 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
Tom Zanussi7862ad12013-10-24 08:59:28 -05001661 enable_data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001662 trace_event_name(enable_data->file->event_call));
Tom Zanussi7862ad12013-10-24 08:59:28 -05001663
1664 if (data->count == -1)
1665 seq_puts(m, ":unlimited");
1666 else
1667 seq_printf(m, ":count=%ld", data->count);
1668
1669 if (data->filter_str)
1670 seq_printf(m, " if %s\n", data->filter_str);
1671 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001672 seq_putc(m, '\n');
Tom Zanussi7862ad12013-10-24 08:59:28 -05001673
1674 return 0;
1675}
1676
Tom Zanussid0bad492016-03-03 12:54:55 -06001677void event_enable_trigger_free(struct event_trigger_ops *ops,
1678 struct event_trigger_data *data)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001679{
1680 struct enable_trigger_data *enable_data = data->private_data;
1681
1682 if (WARN_ON_ONCE(data->ref <= 0))
1683 return;
1684
1685 data->ref--;
1686 if (!data->ref) {
1687 /* Remove the SOFT_MODE flag */
1688 trace_event_enable_disable(enable_data->file, 0, 1);
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -04001689 trace_event_put_ref(enable_data->file->event_call);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001690 trigger_data_free(data);
1691 kfree(enable_data);
1692 }
1693}
1694
1695static struct event_trigger_ops event_enable_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001696 .trigger = event_enable_trigger,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001697 .print = event_enable_trigger_print,
1698 .init = event_trigger_init,
1699 .free = event_enable_trigger_free,
1700};
1701
1702static struct event_trigger_ops event_enable_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001703 .trigger = event_enable_count_trigger,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001704 .print = event_enable_trigger_print,
1705 .init = event_trigger_init,
1706 .free = event_enable_trigger_free,
1707};
1708
1709static struct event_trigger_ops event_disable_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001710 .trigger = event_enable_trigger,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001711 .print = event_enable_trigger_print,
1712 .init = event_trigger_init,
1713 .free = event_enable_trigger_free,
1714};
1715
1716static struct event_trigger_ops event_disable_count_trigger_ops = {
Tom Zanussifb339e52022-01-10 08:04:12 -06001717 .trigger = event_enable_count_trigger,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001718 .print = event_enable_trigger_print,
1719 .init = event_trigger_init,
1720 .free = event_enable_trigger_free,
1721};
1722
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001723int event_enable_trigger_parse(struct event_command *cmd_ops,
1724 struct trace_event_file *file,
1725 char *glob, char *cmd, char *param)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001726{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001727 struct trace_event_file *event_enable_file;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001728 struct enable_trigger_data *enable_data;
1729 struct event_trigger_data *trigger_data;
1730 struct event_trigger_ops *trigger_ops;
1731 struct trace_array *tr = file->tr;
1732 const char *system;
1733 const char *event;
Tom Zanussid0bad492016-03-03 12:54:55 -06001734 bool hist = false;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001735 char *trigger;
1736 char *number;
1737 bool enable;
1738 int ret;
1739
1740 if (!param)
1741 return -EINVAL;
1742
1743 /* separate the trigger from the filter (s:e:n [if filter]) */
1744 trigger = strsep(&param, " \t");
1745 if (!trigger)
1746 return -EINVAL;
Masami Hiramatsu6784bea2020-06-20 12:46:03 +09001747 if (param) {
1748 param = skip_spaces(param);
1749 if (!*param)
1750 param = NULL;
1751 }
Tom Zanussi7862ad12013-10-24 08:59:28 -05001752
1753 system = strsep(&trigger, ":");
1754 if (!trigger)
1755 return -EINVAL;
1756
1757 event = strsep(&trigger, ":");
1758
1759 ret = -EINVAL;
1760 event_enable_file = find_event_file(tr, system, event);
1761 if (!event_enable_file)
1762 goto out;
1763
Tom Zanussid0bad492016-03-03 12:54:55 -06001764#ifdef CONFIG_HIST_TRIGGERS
1765 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1766 (strcmp(cmd, DISABLE_HIST_STR) == 0));
Tom Zanussi7862ad12013-10-24 08:59:28 -05001767
Tom Zanussid0bad492016-03-03 12:54:55 -06001768 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1769 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1770#else
1771 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1772#endif
Tom Zanussi7862ad12013-10-24 08:59:28 -05001773 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1774
1775 ret = -ENOMEM;
1776 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1777 if (!trigger_data)
1778 goto out;
1779
1780 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1781 if (!enable_data) {
1782 kfree(trigger_data);
1783 goto out;
1784 }
1785
1786 trigger_data->count = -1;
1787 trigger_data->ops = trigger_ops;
1788 trigger_data->cmd_ops = cmd_ops;
1789 INIT_LIST_HEAD(&trigger_data->list);
1790 RCU_INIT_POINTER(trigger_data->filter, NULL);
1791
Tom Zanussid0bad492016-03-03 12:54:55 -06001792 enable_data->hist = hist;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001793 enable_data->enable = enable;
1794 enable_data->file = event_enable_file;
1795 trigger_data->private_data = enable_data;
1796
1797 if (glob[0] == '!') {
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001798 cmd_ops->unreg(glob+1, trigger_data, file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001799 kfree(trigger_data);
1800 kfree(enable_data);
1801 ret = 0;
1802 goto out;
1803 }
1804
Steven Rostedt (VMware)15cc7862018-07-25 16:02:06 -04001805 /* Up the trigger_data count to make sure nothing frees it on failure */
1806 event_trigger_init(trigger_ops, trigger_data);
1807
Tom Zanussi7862ad12013-10-24 08:59:28 -05001808 if (trigger) {
1809 number = strsep(&trigger, ":");
1810
1811 ret = -EINVAL;
1812 if (!strlen(number))
1813 goto out_free;
1814
1815 /*
1816 * We use the callback data field (which is a pointer)
1817 * as our counter.
1818 */
1819 ret = kstrtoul(number, 0, &trigger_data->count);
1820 if (ret)
1821 goto out_free;
1822 }
1823
1824 if (!param) /* if param is non-empty, it's supposed to be a filter */
1825 goto out_reg;
1826
1827 if (!cmd_ops->set_filter)
1828 goto out_reg;
1829
1830 ret = cmd_ops->set_filter(param, trigger_data, file);
1831 if (ret < 0)
1832 goto out_free;
1833
1834 out_reg:
1835 /* Don't let event modules unload while probe registered */
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -04001836 ret = trace_event_try_get_ref(event_enable_file->event_call);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001837 if (!ret) {
1838 ret = -EBUSY;
1839 goto out_free;
1840 }
1841
1842 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1843 if (ret < 0)
1844 goto out_put;
Tom Zanussi2378a2d2022-01-10 08:04:13 -06001845 ret = cmd_ops->reg(glob, trigger_data, file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001846 /*
1847 * The above returns on success the # of functions enabled,
1848 * but if it didn't find any functions it returns zero.
1849 * Consider no functions a failure too.
1850 */
1851 if (!ret) {
1852 ret = -ENOENT;
1853 goto out_disable;
1854 } else if (ret < 0)
1855 goto out_disable;
1856 /* Just return zero, not the number of enabled functions */
1857 ret = 0;
Steven Rostedt (VMware)15cc7862018-07-25 16:02:06 -04001858 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001859 out:
1860 return ret;
1861
1862 out_disable:
1863 trace_event_enable_disable(event_enable_file, 0, 1);
1864 out_put:
Steven Rostedt (VMware)1d185382021-08-16 23:42:57 -04001865 trace_event_put_ref(event_enable_file->event_call);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001866 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -05001867 if (cmd_ops->set_filter)
1868 cmd_ops->set_filter(NULL, trigger_data, NULL);
Steven Rostedt (VMware)15cc7862018-07-25 16:02:06 -04001869 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001870 kfree(enable_data);
1871 goto out;
1872}
1873
Tom Zanussid0bad492016-03-03 12:54:55 -06001874int event_enable_register_trigger(char *glob,
Tom Zanussid0bad492016-03-03 12:54:55 -06001875 struct event_trigger_data *data,
1876 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001877{
1878 struct enable_trigger_data *enable_data = data->private_data;
1879 struct enable_trigger_data *test_enable_data;
1880 struct event_trigger_data *test;
1881 int ret = 0;
1882
Masami Hiramatsu3b42a4c2019-12-20 11:31:43 +09001883 lockdep_assert_held(&event_mutex);
1884
1885 list_for_each_entry(test, &file->triggers, list) {
Tom Zanussi7862ad12013-10-24 08:59:28 -05001886 test_enable_data = test->private_data;
1887 if (test_enable_data &&
Tom Zanussid0bad492016-03-03 12:54:55 -06001888 (test->cmd_ops->trigger_type ==
1889 data->cmd_ops->trigger_type) &&
Tom Zanussi7862ad12013-10-24 08:59:28 -05001890 (test_enable_data->file == enable_data->file)) {
1891 ret = -EEXIST;
1892 goto out;
1893 }
1894 }
1895
1896 if (data->ops->init) {
1897 ret = data->ops->init(data->ops, data);
1898 if (ret < 0)
1899 goto out;
1900 }
1901
1902 list_add_rcu(&data->list, &file->triggers);
1903 ret++;
1904
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001905 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001906 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1907 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001908 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001909 ret--;
1910 }
1911out:
1912 return ret;
1913}
1914
Tom Zanussid0bad492016-03-03 12:54:55 -06001915void event_enable_unregister_trigger(char *glob,
Tom Zanussid0bad492016-03-03 12:54:55 -06001916 struct event_trigger_data *test,
1917 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001918{
1919 struct enable_trigger_data *test_enable_data = test->private_data;
1920 struct enable_trigger_data *enable_data;
1921 struct event_trigger_data *data;
1922 bool unregistered = false;
1923
Masami Hiramatsu3b42a4c2019-12-20 11:31:43 +09001924 lockdep_assert_held(&event_mutex);
1925
1926 list_for_each_entry(data, &file->triggers, list) {
Tom Zanussi7862ad12013-10-24 08:59:28 -05001927 enable_data = data->private_data;
1928 if (enable_data &&
Tom Zanussid0bad492016-03-03 12:54:55 -06001929 (data->cmd_ops->trigger_type ==
1930 test->cmd_ops->trigger_type) &&
Tom Zanussi7862ad12013-10-24 08:59:28 -05001931 (enable_data->file == test_enable_data->file)) {
1932 unregistered = true;
1933 list_del_rcu(&data->list);
1934 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001935 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001936 break;
1937 }
1938 }
1939
1940 if (unregistered && data->ops->free)
1941 data->ops->free(data->ops, data);
1942}
1943
1944static struct event_trigger_ops *
1945event_enable_get_trigger_ops(char *cmd, char *param)
1946{
1947 struct event_trigger_ops *ops;
1948 bool enable;
1949
Tom Zanussid0bad492016-03-03 12:54:55 -06001950#ifdef CONFIG_HIST_TRIGGERS
1951 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1952 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1953#else
Tom Zanussi7862ad12013-10-24 08:59:28 -05001954 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
Tom Zanussid0bad492016-03-03 12:54:55 -06001955#endif
Tom Zanussi7862ad12013-10-24 08:59:28 -05001956 if (enable)
1957 ops = param ? &event_enable_count_trigger_ops :
1958 &event_enable_trigger_ops;
1959 else
1960 ops = param ? &event_disable_count_trigger_ops :
1961 &event_disable_trigger_ops;
1962
1963 return ops;
1964}
1965
1966static struct event_command trigger_enable_cmd = {
1967 .name = ENABLE_EVENT_STR,
1968 .trigger_type = ETT_EVENT_ENABLE,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001969 .parse = event_enable_trigger_parse,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001970 .reg = event_enable_register_trigger,
1971 .unreg = event_enable_unregister_trigger,
1972 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001973 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001974};
1975
1976static struct event_command trigger_disable_cmd = {
1977 .name = DISABLE_EVENT_STR,
1978 .trigger_type = ETT_EVENT_ENABLE,
Tom Zanussi9ec5a7d2022-01-10 08:04:11 -06001979 .parse = event_enable_trigger_parse,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001980 .reg = event_enable_register_trigger,
1981 .unreg = event_enable_unregister_trigger,
1982 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001983 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001984};
1985
1986static __init void unregister_trigger_enable_disable_cmds(void)
1987{
1988 unregister_event_command(&trigger_enable_cmd);
1989 unregister_event_command(&trigger_disable_cmd);
1990}
1991
1992static __init int register_trigger_enable_disable_cmds(void)
1993{
1994 int ret;
1995
1996 ret = register_event_command(&trigger_enable_cmd);
1997 if (WARN_ON(ret < 0))
1998 return ret;
1999 ret = register_event_command(&trigger_disable_cmd);
2000 if (WARN_ON(ret < 0))
2001 unregister_trigger_enable_disable_cmds();
2002
2003 return ret;
2004}
2005
Tom Zanussi2a2df322013-10-24 08:59:25 -05002006static __init int register_trigger_traceon_traceoff_cmds(void)
2007{
2008 int ret;
2009
2010 ret = register_event_command(&trigger_traceon_cmd);
2011 if (WARN_ON(ret < 0))
2012 return ret;
2013 ret = register_event_command(&trigger_traceoff_cmd);
2014 if (WARN_ON(ret < 0))
2015 unregister_trigger_traceon_traceoff_cmds();
2016
2017 return ret;
2018}
2019
Tom Zanussi85f2b082013-10-24 08:59:24 -05002020__init int register_trigger_cmds(void)
2021{
Tom Zanussi2a2df322013-10-24 08:59:25 -05002022 register_trigger_traceon_traceoff_cmds();
Tom Zanussi93e31ff2013-10-24 08:59:26 -05002023 register_trigger_snapshot_cmd();
Tom Zanussif21ecbb2013-10-24 08:59:27 -05002024 register_trigger_stacktrace_cmd();
Tom Zanussi7862ad12013-10-24 08:59:28 -05002025 register_trigger_enable_disable_cmds();
Tom Zanussid0bad492016-03-03 12:54:55 -06002026 register_trigger_hist_enable_disable_cmds();
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002027 register_trigger_hist_cmd();
Tom Zanussi2a2df322013-10-24 08:59:25 -05002028
Tom Zanussi85f2b082013-10-24 08:59:24 -05002029 return 0;
2030}