]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - kernel/trace/trace_events_trigger.c
tracing: Remove double-underscore naming in syscall trigger invocations
[mirror_ubuntu-jammy-kernel.git] / kernel / trace / trace_events_trigger.c
CommitLineData
85f2b082
TZ
1/*
2 * trace_events_trigger - trace event triggers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
19 */
20
21#include <linux/module.h>
22#include <linux/ctype.h>
23#include <linux/mutex.h>
24#include <linux/slab.h>
25
26#include "trace.h"
27
28static LIST_HEAD(trigger_commands);
29static DEFINE_MUTEX(trigger_cmd_mutex);
30
2a2df321
TZ
31static void
32trigger_data_free(struct event_trigger_data *data)
33{
bac5fb97
TZ
34 if (data->cmd_ops->set_filter)
35 data->cmd_ops->set_filter(NULL, data, NULL);
36
2a2df321
TZ
37 synchronize_sched(); /* make sure current triggers exit before free */
38 kfree(data);
39}
40
85f2b082
TZ
41/**
42 * event_triggers_call - Call triggers associated with a trace event
43 * @file: The ftrace_event_file associated with the event
bac5fb97 44 * @rec: The trace entry for the event, NULL for unconditional invocation
85f2b082
TZ
45 *
46 * For each trigger associated with an event, invoke the trigger
bac5fb97
TZ
47 * function registered with the associated trigger command. If rec is
48 * non-NULL, it means that the trigger requires further processing and
49 * shouldn't be unconditionally invoked. If rec is non-NULL and the
50 * trigger has a filter associated with it, rec will checked against
51 * the filter and if the record matches the trigger will be invoked.
52 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
53 * in any case until the current event is written, the trigger
54 * function isn't invoked but the bit associated with the deferred
55 * trigger is set in the return value.
56 *
57 * Returns an enum event_trigger_type value containing a set bit for
58 * any trigger that should be deferred, ETT_NONE if nothing to defer.
85f2b082
TZ
59 *
60 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
61 *
62 * Return: an enum event_trigger_type value containing a set bit for
63 * any trigger that should be deferred, ETT_NONE if nothing to defer.
64 */
bac5fb97
TZ
65enum event_trigger_type
66event_triggers_call(struct ftrace_event_file *file, void *rec)
85f2b082
TZ
67{
68 struct event_trigger_data *data;
bac5fb97 69 enum event_trigger_type tt = ETT_NONE;
d8a30f20 70 struct event_filter *filter;
85f2b082
TZ
71
72 if (list_empty(&file->triggers))
bac5fb97 73 return tt;
85f2b082 74
bac5fb97
TZ
75 list_for_each_entry_rcu(data, &file->triggers, list) {
76 if (!rec) {
77 data->ops->func(data);
78 continue;
79 }
d8a30f20
SRRH
80 filter = rcu_dereference(data->filter);
81 if (filter && !filter_match_preds(filter, rec))
bac5fb97
TZ
82 continue;
83 if (data->cmd_ops->post_trigger) {
84 tt |= data->cmd_ops->trigger_type;
85 continue;
86 }
85f2b082 87 data->ops->func(data);
bac5fb97
TZ
88 }
89 return tt;
85f2b082
TZ
90}
91EXPORT_SYMBOL_GPL(event_triggers_call);
92
bac5fb97
TZ
93/**
94 * event_triggers_post_call - Call 'post_triggers' for a trace event
95 * @file: The ftrace_event_file associated with the event
96 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
97 *
98 * For each trigger associated with an event, invoke the trigger
99 * function registered with the associated trigger command, if the
100 * corresponding bit is set in the tt enum passed into this function.
101 * See @event_triggers_call for details on how those bits are set.
102 *
103 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
104 */
105void
106event_triggers_post_call(struct ftrace_event_file *file,
107 enum event_trigger_type tt)
108{
109 struct event_trigger_data *data;
110
111 list_for_each_entry_rcu(data, &file->triggers, list) {
112 if (data->cmd_ops->trigger_type & tt)
113 data->ops->func(data);
114 }
115}
116EXPORT_SYMBOL_GPL(event_triggers_post_call);
117
85f2b082
TZ
118static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
119{
120 struct ftrace_event_file *event_file = event_file_data(m->private);
121
122 return seq_list_next(t, &event_file->triggers, pos);
123}
124
125static void *trigger_start(struct seq_file *m, loff_t *pos)
126{
127 struct ftrace_event_file *event_file;
128
129 /* ->stop() is called even if ->start() fails */
130 mutex_lock(&event_mutex);
131 event_file = event_file_data(m->private);
132 if (unlikely(!event_file))
133 return ERR_PTR(-ENODEV);
134
135 return seq_list_start(&event_file->triggers, *pos);
136}
137
138static void trigger_stop(struct seq_file *m, void *t)
139{
140 mutex_unlock(&event_mutex);
141}
142
143static int trigger_show(struct seq_file *m, void *v)
144{
145 struct event_trigger_data *data;
146
147 data = list_entry(v, struct event_trigger_data, list);
148 data->ops->print(m, data->ops, data);
149
150 return 0;
151}
152
153static const struct seq_operations event_triggers_seq_ops = {
154 .start = trigger_start,
155 .next = trigger_next,
156 .stop = trigger_stop,
157 .show = trigger_show,
158};
159
160static int event_trigger_regex_open(struct inode *inode, struct file *file)
161{
162 int ret = 0;
163
164 mutex_lock(&event_mutex);
165
166 if (unlikely(!event_file_data(file))) {
167 mutex_unlock(&event_mutex);
168 return -ENODEV;
169 }
170
171 if (file->f_mode & FMODE_READ) {
172 ret = seq_open(file, &event_triggers_seq_ops);
173 if (!ret) {
174 struct seq_file *m = file->private_data;
175 m->private = file;
176 }
177 }
178
179 mutex_unlock(&event_mutex);
180
181 return ret;
182}
183
184static int trigger_process_regex(struct ftrace_event_file *file, char *buff)
185{
186 char *command, *next = buff;
187 struct event_command *p;
188 int ret = -EINVAL;
189
190 command = strsep(&next, ": \t");
191 command = (command[0] != '!') ? command : command + 1;
192
193 mutex_lock(&trigger_cmd_mutex);
194 list_for_each_entry(p, &trigger_commands, list) {
195 if (strcmp(p->name, command) == 0) {
196 ret = p->func(p, file, buff, command, next);
197 goto out_unlock;
198 }
199 }
200 out_unlock:
201 mutex_unlock(&trigger_cmd_mutex);
202
203 return ret;
204}
205
206static ssize_t event_trigger_regex_write(struct file *file,
207 const char __user *ubuf,
208 size_t cnt, loff_t *ppos)
209{
210 struct ftrace_event_file *event_file;
211 ssize_t ret;
212 char *buf;
213
214 if (!cnt)
215 return 0;
216
217 if (cnt >= PAGE_SIZE)
218 return -EINVAL;
219
220 buf = (char *)__get_free_page(GFP_TEMPORARY);
221 if (!buf)
222 return -ENOMEM;
223
224 if (copy_from_user(buf, ubuf, cnt)) {
225 free_page((unsigned long)buf);
226 return -EFAULT;
227 }
228 buf[cnt] = '\0';
229 strim(buf);
230
231 mutex_lock(&event_mutex);
232 event_file = event_file_data(file);
233 if (unlikely(!event_file)) {
234 mutex_unlock(&event_mutex);
235 free_page((unsigned long)buf);
236 return -ENODEV;
237 }
238 ret = trigger_process_regex(event_file, buf);
239 mutex_unlock(&event_mutex);
240
241 free_page((unsigned long)buf);
242 if (ret < 0)
243 goto out;
244
245 *ppos += cnt;
246 ret = cnt;
247 out:
248 return ret;
249}
250
251static int event_trigger_regex_release(struct inode *inode, struct file *file)
252{
253 mutex_lock(&event_mutex);
254
255 if (file->f_mode & FMODE_READ)
256 seq_release(inode, file);
257
258 mutex_unlock(&event_mutex);
259
260 return 0;
261}
262
263static ssize_t
264event_trigger_write(struct file *filp, const char __user *ubuf,
265 size_t cnt, loff_t *ppos)
266{
267 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
268}
269
270static int
271event_trigger_open(struct inode *inode, struct file *filp)
272{
273 return event_trigger_regex_open(inode, filp);
274}
275
276static int
277event_trigger_release(struct inode *inode, struct file *file)
278{
279 return event_trigger_regex_release(inode, file);
280}
281
282const struct file_operations event_trigger_fops = {
283 .open = event_trigger_open,
284 .read = seq_read,
285 .write = event_trigger_write,
098c879e 286 .llseek = tracing_lseek,
85f2b082
TZ
287 .release = event_trigger_release,
288};
289
2a2df321
TZ
290/*
291 * Currently we only register event commands from __init, so mark this
292 * __init too.
293 */
294static __init int register_event_command(struct event_command *cmd)
295{
296 struct event_command *p;
297 int ret = 0;
298
299 mutex_lock(&trigger_cmd_mutex);
300 list_for_each_entry(p, &trigger_commands, list) {
301 if (strcmp(cmd->name, p->name) == 0) {
302 ret = -EBUSY;
303 goto out_unlock;
304 }
305 }
306 list_add(&cmd->list, &trigger_commands);
307 out_unlock:
308 mutex_unlock(&trigger_cmd_mutex);
309
310 return ret;
311}
312
313/*
314 * Currently we only unregister event commands from __init, so mark
315 * this __init too.
316 */
317static __init int unregister_event_command(struct event_command *cmd)
318{
319 struct event_command *p, *n;
320 int ret = -ENODEV;
321
322 mutex_lock(&trigger_cmd_mutex);
323 list_for_each_entry_safe(p, n, &trigger_commands, list) {
324 if (strcmp(cmd->name, p->name) == 0) {
325 ret = 0;
326 list_del_init(&p->list);
327 goto out_unlock;
328 }
329 }
330 out_unlock:
331 mutex_unlock(&trigger_cmd_mutex);
332
333 return ret;
334}
335
336/**
337 * event_trigger_print - Generic event_trigger_ops @print implementation
338 * @name: The name of the event trigger
339 * @m: The seq_file being printed to
340 * @data: Trigger-specific data
341 * @filter_str: filter_str to print, if present
342 *
343 * Common implementation for event triggers to print themselves.
344 *
345 * Usually wrapped by a function that simply sets the @name of the
346 * trigger command and then invokes this.
347 *
348 * Return: 0 on success, errno otherwise
349 */
350static int
351event_trigger_print(const char *name, struct seq_file *m,
352 void *data, char *filter_str)
353{
354 long count = (long)data;
355
356 seq_printf(m, "%s", name);
357
358 if (count == -1)
359 seq_puts(m, ":unlimited");
360 else
361 seq_printf(m, ":count=%ld", count);
362
363 if (filter_str)
364 seq_printf(m, " if %s\n", filter_str);
365 else
366 seq_puts(m, "\n");
367
368 return 0;
369}
370
371/**
372 * event_trigger_init - Generic event_trigger_ops @init implementation
373 * @ops: The trigger ops associated with the trigger
374 * @data: Trigger-specific data
375 *
376 * Common implementation of event trigger initialization.
377 *
378 * Usually used directly as the @init method in event trigger
379 * implementations.
380 *
381 * Return: 0 on success, errno otherwise
382 */
383static int
384event_trigger_init(struct event_trigger_ops *ops,
385 struct event_trigger_data *data)
386{
387 data->ref++;
388 return 0;
389}
390
391/**
392 * event_trigger_free - Generic event_trigger_ops @free implementation
393 * @ops: The trigger ops associated with the trigger
394 * @data: Trigger-specific data
395 *
396 * Common implementation of event trigger de-initialization.
397 *
398 * Usually used directly as the @free method in event trigger
399 * implementations.
400 */
401static void
402event_trigger_free(struct event_trigger_ops *ops,
403 struct event_trigger_data *data)
404{
405 if (WARN_ON_ONCE(data->ref <= 0))
406 return;
407
408 data->ref--;
409 if (!data->ref)
410 trigger_data_free(data);
411}
412
85f2b082
TZ
413static int trace_event_trigger_enable_disable(struct ftrace_event_file *file,
414 int trigger_enable)
415{
416 int ret = 0;
417
418 if (trigger_enable) {
419 if (atomic_inc_return(&file->tm_ref) > 1)
420 return ret;
421 set_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags);
422 ret = trace_event_enable_disable(file, 1, 1);
423 } else {
424 if (atomic_dec_return(&file->tm_ref) > 0)
425 return ret;
426 clear_bit(FTRACE_EVENT_FL_TRIGGER_MODE_BIT, &file->flags);
427 ret = trace_event_enable_disable(file, 0, 1);
428 }
429
430 return ret;
431}
432
433/**
434 * clear_event_triggers - Clear all triggers associated with a trace array
435 * @tr: The trace array to clear
436 *
437 * For each trigger, the triggering event has its tm_ref decremented
438 * via trace_event_trigger_enable_disable(), and any associated event
439 * (in the case of enable/disable_event triggers) will have its sm_ref
440 * decremented via free()->trace_event_enable_disable(). That
441 * combination effectively reverses the soft-mode/trigger state added
442 * by trigger registration.
443 *
444 * Must be called with event_mutex held.
445 */
446void
447clear_event_triggers(struct trace_array *tr)
448{
449 struct ftrace_event_file *file;
450
451 list_for_each_entry(file, &tr->events, list) {
452 struct event_trigger_data *data;
453 list_for_each_entry_rcu(data, &file->triggers, list) {
454 trace_event_trigger_enable_disable(file, 0);
455 if (data->ops->free)
456 data->ops->free(data->ops, data);
457 }
458 }
459}
460
bac5fb97
TZ
461/**
462 * update_cond_flag - Set or reset the TRIGGER_COND bit
463 * @file: The ftrace_event_file associated with the event
464 *
465 * If an event has triggers and any of those triggers has a filter or
466 * a post_trigger, trigger invocation needs to be deferred until after
467 * the current event has logged its data, and the event should have
468 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
469 * cleared.
470 */
471static void update_cond_flag(struct ftrace_event_file *file)
472{
473 struct event_trigger_data *data;
474 bool set_cond = false;
475
476 list_for_each_entry_rcu(data, &file->triggers, list) {
477 if (data->filter || data->cmd_ops->post_trigger) {
478 set_cond = true;
479 break;
480 }
481 }
482
483 if (set_cond)
484 set_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT, &file->flags);
485 else
486 clear_bit(FTRACE_EVENT_FL_TRIGGER_COND_BIT, &file->flags);
487}
488
2a2df321
TZ
489/**
490 * register_trigger - Generic event_command @reg implementation
491 * @glob: The raw string used to register the trigger
492 * @ops: The trigger ops associated with the trigger
493 * @data: Trigger-specific data to associate with the trigger
494 * @file: The ftrace_event_file associated with the event
495 *
496 * Common implementation for event trigger registration.
497 *
498 * Usually used directly as the @reg method in event command
499 * implementations.
500 *
501 * Return: 0 on success, errno otherwise
502 */
503static int register_trigger(char *glob, struct event_trigger_ops *ops,
504 struct event_trigger_data *data,
505 struct ftrace_event_file *file)
506{
507 struct event_trigger_data *test;
508 int ret = 0;
509
510 list_for_each_entry_rcu(test, &file->triggers, list) {
511 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
512 ret = -EEXIST;
513 goto out;
514 }
515 }
516
517 if (data->ops->init) {
518 ret = data->ops->init(data->ops, data);
519 if (ret < 0)
520 goto out;
521 }
522
523 list_add_rcu(&data->list, &file->triggers);
524 ret++;
525
526 if (trace_event_trigger_enable_disable(file, 1) < 0) {
527 list_del_rcu(&data->list);
528 ret--;
529 }
bac5fb97 530 update_cond_flag(file);
2a2df321
TZ
531out:
532 return ret;
533}
534
535/**
536 * unregister_trigger - Generic event_command @unreg implementation
537 * @glob: The raw string used to register the trigger
538 * @ops: The trigger ops associated with the trigger
539 * @test: Trigger-specific data used to find the trigger to remove
540 * @file: The ftrace_event_file associated with the event
541 *
542 * Common implementation for event trigger unregistration.
543 *
544 * Usually used directly as the @unreg method in event command
545 * implementations.
546 */
547static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
548 struct event_trigger_data *test,
549 struct ftrace_event_file *file)
550{
551 struct event_trigger_data *data;
552 bool unregistered = false;
553
554 list_for_each_entry_rcu(data, &file->triggers, list) {
555 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
556 unregistered = true;
557 list_del_rcu(&data->list);
bac5fb97 558 update_cond_flag(file);
2a2df321
TZ
559 trace_event_trigger_enable_disable(file, 0);
560 break;
561 }
562 }
563
564 if (unregistered && data->ops->free)
565 data->ops->free(data->ops, data);
566}
567
568/**
569 * event_trigger_callback - Generic event_command @func implementation
570 * @cmd_ops: The command ops, used for trigger registration
571 * @file: The ftrace_event_file associated with the event
572 * @glob: The raw string used to register the trigger
573 * @cmd: The cmd portion of the string used to register the trigger
574 * @param: The params portion of the string used to register the trigger
575 *
576 * Common implementation for event command parsing and trigger
577 * instantiation.
578 *
579 * Usually used directly as the @func method in event command
580 * implementations.
581 *
582 * Return: 0 on success, errno otherwise
583 */
584static int
585event_trigger_callback(struct event_command *cmd_ops,
586 struct ftrace_event_file *file,
587 char *glob, char *cmd, char *param)
588{
589 struct event_trigger_data *trigger_data;
590 struct event_trigger_ops *trigger_ops;
591 char *trigger = NULL;
592 char *number;
593 int ret;
594
595 /* separate the trigger from the filter (t:n [if filter]) */
596 if (param && isdigit(param[0]))
597 trigger = strsep(&param, " \t");
598
599 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
600
601 ret = -ENOMEM;
602 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
603 if (!trigger_data)
604 goto out;
605
606 trigger_data->count = -1;
607 trigger_data->ops = trigger_ops;
608 trigger_data->cmd_ops = cmd_ops;
609 INIT_LIST_HEAD(&trigger_data->list);
610
611 if (glob[0] == '!') {
612 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
613 kfree(trigger_data);
614 ret = 0;
615 goto out;
616 }
617
618 if (trigger) {
619 number = strsep(&trigger, ":");
620
621 ret = -EINVAL;
622 if (!strlen(number))
623 goto out_free;
624
625 /*
626 * We use the callback data field (which is a pointer)
627 * as our counter.
628 */
629 ret = kstrtoul(number, 0, &trigger_data->count);
630 if (ret)
631 goto out_free;
632 }
633
634 if (!param) /* if param is non-empty, it's supposed to be a filter */
635 goto out_reg;
636
637 if (!cmd_ops->set_filter)
638 goto out_reg;
639
640 ret = cmd_ops->set_filter(param, trigger_data, file);
641 if (ret < 0)
642 goto out_free;
643
644 out_reg:
645 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
646 /*
647 * The above returns on success the # of functions enabled,
648 * but if it didn't find any functions it returns zero.
649 * Consider no functions a failure too.
650 */
651 if (!ret) {
652 ret = -ENOENT;
653 goto out_free;
654 } else if (ret < 0)
655 goto out_free;
656 ret = 0;
657 out:
658 return ret;
659
660 out_free:
bac5fb97
TZ
661 if (cmd_ops->set_filter)
662 cmd_ops->set_filter(NULL, trigger_data, NULL);
2a2df321
TZ
663 kfree(trigger_data);
664 goto out;
665}
666
bac5fb97
TZ
667/**
668 * set_trigger_filter - Generic event_command @set_filter implementation
669 * @filter_str: The filter string for the trigger, NULL to remove filter
670 * @trigger_data: Trigger-specific data
671 * @file: The ftrace_event_file associated with the event
672 *
673 * Common implementation for event command filter parsing and filter
674 * instantiation.
675 *
676 * Usually used directly as the @set_filter method in event command
677 * implementations.
678 *
679 * Also used to remove a filter (if filter_str = NULL).
680 *
681 * Return: 0 on success, errno otherwise
682 */
683static int set_trigger_filter(char *filter_str,
684 struct event_trigger_data *trigger_data,
685 struct ftrace_event_file *file)
686{
687 struct event_trigger_data *data = trigger_data;
688 struct event_filter *filter = NULL, *tmp;
689 int ret = -EINVAL;
690 char *s;
691
692 if (!filter_str) /* clear the current filter */
693 goto assign;
694
695 s = strsep(&filter_str, " \t");
696
697 if (!strlen(s) || strcmp(s, "if") != 0)
698 goto out;
699
700 if (!filter_str)
701 goto out;
702
703 /* The filter is for the 'trigger' event, not the triggered event */
704 ret = create_event_filter(file->event_call, filter_str, false, &filter);
705 if (ret)
706 goto out;
707 assign:
d8a30f20 708 tmp = rcu_access_pointer(data->filter);
bac5fb97
TZ
709
710 rcu_assign_pointer(data->filter, filter);
711
712 if (tmp) {
713 /* Make sure the call is done with the filter */
714 synchronize_sched();
715 free_event_filter(tmp);
716 }
717
718 kfree(data->filter_str);
719 data->filter_str = NULL;
720
721 if (filter_str) {
722 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
723 if (!data->filter_str) {
d8a30f20 724 free_event_filter(rcu_access_pointer(data->filter));
bac5fb97
TZ
725 data->filter = NULL;
726 ret = -ENOMEM;
727 }
728 }
729 out:
730 return ret;
731}
732
2a2df321
TZ
733static void
734traceon_trigger(struct event_trigger_data *data)
735{
736 if (tracing_is_on())
737 return;
738
739 tracing_on();
740}
741
742static void
743traceon_count_trigger(struct event_trigger_data *data)
744{
745 if (!data->count)
746 return;
747
748 if (data->count != -1)
749 (data->count)--;
750
751 traceon_trigger(data);
752}
753
754static void
755traceoff_trigger(struct event_trigger_data *data)
756{
757 if (!tracing_is_on())
758 return;
759
760 tracing_off();
761}
762
763static void
764traceoff_count_trigger(struct event_trigger_data *data)
765{
766 if (!data->count)
767 return;
768
769 if (data->count != -1)
770 (data->count)--;
771
772 traceoff_trigger(data);
773}
774
775static int
776traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
777 struct event_trigger_data *data)
778{
779 return event_trigger_print("traceon", m, (void *)data->count,
780 data->filter_str);
781}
782
783static int
784traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
785 struct event_trigger_data *data)
786{
787 return event_trigger_print("traceoff", m, (void *)data->count,
788 data->filter_str);
789}
790
791static struct event_trigger_ops traceon_trigger_ops = {
792 .func = traceon_trigger,
793 .print = traceon_trigger_print,
794 .init = event_trigger_init,
795 .free = event_trigger_free,
796};
797
798static struct event_trigger_ops traceon_count_trigger_ops = {
799 .func = traceon_count_trigger,
800 .print = traceon_trigger_print,
801 .init = event_trigger_init,
802 .free = event_trigger_free,
803};
804
805static struct event_trigger_ops traceoff_trigger_ops = {
806 .func = traceoff_trigger,
807 .print = traceoff_trigger_print,
808 .init = event_trigger_init,
809 .free = event_trigger_free,
810};
811
812static struct event_trigger_ops traceoff_count_trigger_ops = {
813 .func = traceoff_count_trigger,
814 .print = traceoff_trigger_print,
815 .init = event_trigger_init,
816 .free = event_trigger_free,
817};
818
819static struct event_trigger_ops *
820onoff_get_trigger_ops(char *cmd, char *param)
821{
822 struct event_trigger_ops *ops;
823
824 /* we register both traceon and traceoff to this callback */
825 if (strcmp(cmd, "traceon") == 0)
826 ops = param ? &traceon_count_trigger_ops :
827 &traceon_trigger_ops;
828 else
829 ops = param ? &traceoff_count_trigger_ops :
830 &traceoff_trigger_ops;
831
832 return ops;
833}
834
835static struct event_command trigger_traceon_cmd = {
836 .name = "traceon",
837 .trigger_type = ETT_TRACE_ONOFF,
838 .func = event_trigger_callback,
839 .reg = register_trigger,
840 .unreg = unregister_trigger,
841 .get_trigger_ops = onoff_get_trigger_ops,
bac5fb97 842 .set_filter = set_trigger_filter,
2a2df321
TZ
843};
844
845static struct event_command trigger_traceoff_cmd = {
846 .name = "traceoff",
847 .trigger_type = ETT_TRACE_ONOFF,
848 .func = event_trigger_callback,
849 .reg = register_trigger,
850 .unreg = unregister_trigger,
851 .get_trigger_ops = onoff_get_trigger_ops,
bac5fb97 852 .set_filter = set_trigger_filter,
2a2df321
TZ
853};
854
93e31ffb
TZ
855#ifdef CONFIG_TRACER_SNAPSHOT
856static void
857snapshot_trigger(struct event_trigger_data *data)
858{
859 tracing_snapshot();
860}
861
862static void
863snapshot_count_trigger(struct event_trigger_data *data)
864{
865 if (!data->count)
866 return;
867
868 if (data->count != -1)
869 (data->count)--;
870
871 snapshot_trigger(data);
872}
873
874static int
875register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
876 struct event_trigger_data *data,
877 struct ftrace_event_file *file)
878{
879 int ret = register_trigger(glob, ops, data, file);
880
881 if (ret > 0 && tracing_alloc_snapshot() != 0) {
882 unregister_trigger(glob, ops, data, file);
883 ret = 0;
884 }
885
886 return ret;
887}
888
889static int
890snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
891 struct event_trigger_data *data)
892{
893 return event_trigger_print("snapshot", m, (void *)data->count,
894 data->filter_str);
895}
896
897static struct event_trigger_ops snapshot_trigger_ops = {
898 .func = snapshot_trigger,
899 .print = snapshot_trigger_print,
900 .init = event_trigger_init,
901 .free = event_trigger_free,
902};
903
904static struct event_trigger_ops snapshot_count_trigger_ops = {
905 .func = snapshot_count_trigger,
906 .print = snapshot_trigger_print,
907 .init = event_trigger_init,
908 .free = event_trigger_free,
909};
910
911static struct event_trigger_ops *
912snapshot_get_trigger_ops(char *cmd, char *param)
913{
914 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
915}
916
917static struct event_command trigger_snapshot_cmd = {
918 .name = "snapshot",
919 .trigger_type = ETT_SNAPSHOT,
920 .func = event_trigger_callback,
921 .reg = register_snapshot_trigger,
922 .unreg = unregister_trigger,
923 .get_trigger_ops = snapshot_get_trigger_ops,
bac5fb97 924 .set_filter = set_trigger_filter,
93e31ffb
TZ
925};
926
927static __init int register_trigger_snapshot_cmd(void)
928{
929 int ret;
930
931 ret = register_event_command(&trigger_snapshot_cmd);
932 WARN_ON(ret < 0);
933
934 return ret;
935}
936#else
937static __init int register_trigger_snapshot_cmd(void) { return 0; }
938#endif /* CONFIG_TRACER_SNAPSHOT */
939
f21ecbb3
TZ
940#ifdef CONFIG_STACKTRACE
941/*
942 * Skip 3:
943 * stacktrace_trigger()
944 * event_triggers_post_call()
945 * ftrace_raw_event_xxx()
946 */
947#define STACK_SKIP 3
948
949static void
950stacktrace_trigger(struct event_trigger_data *data)
951{
952 trace_dump_stack(STACK_SKIP);
953}
954
955static void
956stacktrace_count_trigger(struct event_trigger_data *data)
957{
958 if (!data->count)
959 return;
960
961 if (data->count != -1)
962 (data->count)--;
963
964 stacktrace_trigger(data);
965}
966
967static int
968stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
969 struct event_trigger_data *data)
970{
971 return event_trigger_print("stacktrace", m, (void *)data->count,
972 data->filter_str);
973}
974
975static struct event_trigger_ops stacktrace_trigger_ops = {
976 .func = stacktrace_trigger,
977 .print = stacktrace_trigger_print,
978 .init = event_trigger_init,
979 .free = event_trigger_free,
980};
981
982static struct event_trigger_ops stacktrace_count_trigger_ops = {
983 .func = stacktrace_count_trigger,
984 .print = stacktrace_trigger_print,
985 .init = event_trigger_init,
986 .free = event_trigger_free,
987};
988
989static struct event_trigger_ops *
990stacktrace_get_trigger_ops(char *cmd, char *param)
991{
992 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
993}
994
995static struct event_command trigger_stacktrace_cmd = {
996 .name = "stacktrace",
997 .trigger_type = ETT_STACKTRACE,
998 .post_trigger = true,
999 .func = event_trigger_callback,
1000 .reg = register_trigger,
1001 .unreg = unregister_trigger,
1002 .get_trigger_ops = stacktrace_get_trigger_ops,
bac5fb97 1003 .set_filter = set_trigger_filter,
f21ecbb3
TZ
1004};
1005
1006static __init int register_trigger_stacktrace_cmd(void)
1007{
1008 int ret;
1009
1010 ret = register_event_command(&trigger_stacktrace_cmd);
1011 WARN_ON(ret < 0);
1012
1013 return ret;
1014}
1015#else
1016static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1017#endif /* CONFIG_STACKTRACE */
1018
2a2df321
TZ
1019static __init void unregister_trigger_traceon_traceoff_cmds(void)
1020{
1021 unregister_event_command(&trigger_traceon_cmd);
1022 unregister_event_command(&trigger_traceoff_cmd);
1023}
1024
7862ad18
TZ
1025/* Avoid typos */
1026#define ENABLE_EVENT_STR "enable_event"
1027#define DISABLE_EVENT_STR "disable_event"
1028
1029struct enable_trigger_data {
1030 struct ftrace_event_file *file;
1031 bool enable;
1032};
1033
1034static void
1035event_enable_trigger(struct event_trigger_data *data)
1036{
1037 struct enable_trigger_data *enable_data = data->private_data;
1038
1039 if (enable_data->enable)
1040 clear_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1041 else
1042 set_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1043}
1044
1045static void
1046event_enable_count_trigger(struct event_trigger_data *data)
1047{
1048 struct enable_trigger_data *enable_data = data->private_data;
1049
1050 if (!data->count)
1051 return;
1052
1053 /* Skip if the event is in a state we want to switch to */
1054 if (enable_data->enable == !(enable_data->file->flags & FTRACE_EVENT_FL_SOFT_DISABLED))
1055 return;
1056
1057 if (data->count != -1)
1058 (data->count)--;
1059
1060 event_enable_trigger(data);
1061}
1062
1063static int
1064event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1065 struct event_trigger_data *data)
1066{
1067 struct enable_trigger_data *enable_data = data->private_data;
1068
1069 seq_printf(m, "%s:%s:%s",
1070 enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1071 enable_data->file->event_call->class->system,
1072 enable_data->file->event_call->name);
1073
1074 if (data->count == -1)
1075 seq_puts(m, ":unlimited");
1076 else
1077 seq_printf(m, ":count=%ld", data->count);
1078
1079 if (data->filter_str)
1080 seq_printf(m, " if %s\n", data->filter_str);
1081 else
1082 seq_puts(m, "\n");
1083
1084 return 0;
1085}
1086
1087static void
1088event_enable_trigger_free(struct event_trigger_ops *ops,
1089 struct event_trigger_data *data)
1090{
1091 struct enable_trigger_data *enable_data = data->private_data;
1092
1093 if (WARN_ON_ONCE(data->ref <= 0))
1094 return;
1095
1096 data->ref--;
1097 if (!data->ref) {
1098 /* Remove the SOFT_MODE flag */
1099 trace_event_enable_disable(enable_data->file, 0, 1);
1100 module_put(enable_data->file->event_call->mod);
1101 trigger_data_free(data);
1102 kfree(enable_data);
1103 }
1104}
1105
1106static struct event_trigger_ops event_enable_trigger_ops = {
1107 .func = event_enable_trigger,
1108 .print = event_enable_trigger_print,
1109 .init = event_trigger_init,
1110 .free = event_enable_trigger_free,
1111};
1112
1113static struct event_trigger_ops event_enable_count_trigger_ops = {
1114 .func = event_enable_count_trigger,
1115 .print = event_enable_trigger_print,
1116 .init = event_trigger_init,
1117 .free = event_enable_trigger_free,
1118};
1119
1120static struct event_trigger_ops event_disable_trigger_ops = {
1121 .func = event_enable_trigger,
1122 .print = event_enable_trigger_print,
1123 .init = event_trigger_init,
1124 .free = event_enable_trigger_free,
1125};
1126
1127static struct event_trigger_ops event_disable_count_trigger_ops = {
1128 .func = event_enable_count_trigger,
1129 .print = event_enable_trigger_print,
1130 .init = event_trigger_init,
1131 .free = event_enable_trigger_free,
1132};
1133
1134static int
1135event_enable_trigger_func(struct event_command *cmd_ops,
1136 struct ftrace_event_file *file,
1137 char *glob, char *cmd, char *param)
1138{
1139 struct ftrace_event_file *event_enable_file;
1140 struct enable_trigger_data *enable_data;
1141 struct event_trigger_data *trigger_data;
1142 struct event_trigger_ops *trigger_ops;
1143 struct trace_array *tr = file->tr;
1144 const char *system;
1145 const char *event;
1146 char *trigger;
1147 char *number;
1148 bool enable;
1149 int ret;
1150
1151 if (!param)
1152 return -EINVAL;
1153
1154 /* separate the trigger from the filter (s:e:n [if filter]) */
1155 trigger = strsep(&param, " \t");
1156 if (!trigger)
1157 return -EINVAL;
1158
1159 system = strsep(&trigger, ":");
1160 if (!trigger)
1161 return -EINVAL;
1162
1163 event = strsep(&trigger, ":");
1164
1165 ret = -EINVAL;
1166 event_enable_file = find_event_file(tr, system, event);
1167 if (!event_enable_file)
1168 goto out;
1169
1170 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1171
1172 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1173
1174 ret = -ENOMEM;
1175 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1176 if (!trigger_data)
1177 goto out;
1178
1179 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1180 if (!enable_data) {
1181 kfree(trigger_data);
1182 goto out;
1183 }
1184
1185 trigger_data->count = -1;
1186 trigger_data->ops = trigger_ops;
1187 trigger_data->cmd_ops = cmd_ops;
1188 INIT_LIST_HEAD(&trigger_data->list);
1189 RCU_INIT_POINTER(trigger_data->filter, NULL);
1190
1191 enable_data->enable = enable;
1192 enable_data->file = event_enable_file;
1193 trigger_data->private_data = enable_data;
1194
1195 if (glob[0] == '!') {
1196 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1197 kfree(trigger_data);
1198 kfree(enable_data);
1199 ret = 0;
1200 goto out;
1201 }
1202
1203 if (trigger) {
1204 number = strsep(&trigger, ":");
1205
1206 ret = -EINVAL;
1207 if (!strlen(number))
1208 goto out_free;
1209
1210 /*
1211 * We use the callback data field (which is a pointer)
1212 * as our counter.
1213 */
1214 ret = kstrtoul(number, 0, &trigger_data->count);
1215 if (ret)
1216 goto out_free;
1217 }
1218
1219 if (!param) /* if param is non-empty, it's supposed to be a filter */
1220 goto out_reg;
1221
1222 if (!cmd_ops->set_filter)
1223 goto out_reg;
1224
1225 ret = cmd_ops->set_filter(param, trigger_data, file);
1226 if (ret < 0)
1227 goto out_free;
1228
1229 out_reg:
1230 /* Don't let event modules unload while probe registered */
1231 ret = try_module_get(event_enable_file->event_call->mod);
1232 if (!ret) {
1233 ret = -EBUSY;
1234 goto out_free;
1235 }
1236
1237 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1238 if (ret < 0)
1239 goto out_put;
1240 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1241 /*
1242 * The above returns on success the # of functions enabled,
1243 * but if it didn't find any functions it returns zero.
1244 * Consider no functions a failure too.
1245 */
1246 if (!ret) {
1247 ret = -ENOENT;
1248 goto out_disable;
1249 } else if (ret < 0)
1250 goto out_disable;
1251 /* Just return zero, not the number of enabled functions */
1252 ret = 0;
1253 out:
1254 return ret;
1255
1256 out_disable:
1257 trace_event_enable_disable(event_enable_file, 0, 1);
1258 out_put:
1259 module_put(event_enable_file->event_call->mod);
1260 out_free:
bac5fb97
TZ
1261 if (cmd_ops->set_filter)
1262 cmd_ops->set_filter(NULL, trigger_data, NULL);
7862ad18
TZ
1263 kfree(trigger_data);
1264 kfree(enable_data);
1265 goto out;
1266}
1267
1268static int event_enable_register_trigger(char *glob,
1269 struct event_trigger_ops *ops,
1270 struct event_trigger_data *data,
1271 struct ftrace_event_file *file)
1272{
1273 struct enable_trigger_data *enable_data = data->private_data;
1274 struct enable_trigger_data *test_enable_data;
1275 struct event_trigger_data *test;
1276 int ret = 0;
1277
1278 list_for_each_entry_rcu(test, &file->triggers, list) {
1279 test_enable_data = test->private_data;
1280 if (test_enable_data &&
1281 (test_enable_data->file == enable_data->file)) {
1282 ret = -EEXIST;
1283 goto out;
1284 }
1285 }
1286
1287 if (data->ops->init) {
1288 ret = data->ops->init(data->ops, data);
1289 if (ret < 0)
1290 goto out;
1291 }
1292
1293 list_add_rcu(&data->list, &file->triggers);
1294 ret++;
1295
1296 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1297 list_del_rcu(&data->list);
1298 ret--;
1299 }
bac5fb97 1300 update_cond_flag(file);
7862ad18
TZ
1301out:
1302 return ret;
1303}
1304
1305static void event_enable_unregister_trigger(char *glob,
1306 struct event_trigger_ops *ops,
1307 struct event_trigger_data *test,
1308 struct ftrace_event_file *file)
1309{
1310 struct enable_trigger_data *test_enable_data = test->private_data;
1311 struct enable_trigger_data *enable_data;
1312 struct event_trigger_data *data;
1313 bool unregistered = false;
1314
1315 list_for_each_entry_rcu(data, &file->triggers, list) {
1316 enable_data = data->private_data;
1317 if (enable_data &&
1318 (enable_data->file == test_enable_data->file)) {
1319 unregistered = true;
1320 list_del_rcu(&data->list);
bac5fb97 1321 update_cond_flag(file);
7862ad18
TZ
1322 trace_event_trigger_enable_disable(file, 0);
1323 break;
1324 }
1325 }
1326
1327 if (unregistered && data->ops->free)
1328 data->ops->free(data->ops, data);
1329}
1330
1331static struct event_trigger_ops *
1332event_enable_get_trigger_ops(char *cmd, char *param)
1333{
1334 struct event_trigger_ops *ops;
1335 bool enable;
1336
1337 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1338
1339 if (enable)
1340 ops = param ? &event_enable_count_trigger_ops :
1341 &event_enable_trigger_ops;
1342 else
1343 ops = param ? &event_disable_count_trigger_ops :
1344 &event_disable_trigger_ops;
1345
1346 return ops;
1347}
1348
1349static struct event_command trigger_enable_cmd = {
1350 .name = ENABLE_EVENT_STR,
1351 .trigger_type = ETT_EVENT_ENABLE,
1352 .func = event_enable_trigger_func,
1353 .reg = event_enable_register_trigger,
1354 .unreg = event_enable_unregister_trigger,
1355 .get_trigger_ops = event_enable_get_trigger_ops,
bac5fb97 1356 .set_filter = set_trigger_filter,
7862ad18
TZ
1357};
1358
1359static struct event_command trigger_disable_cmd = {
1360 .name = DISABLE_EVENT_STR,
1361 .trigger_type = ETT_EVENT_ENABLE,
1362 .func = event_enable_trigger_func,
1363 .reg = event_enable_register_trigger,
1364 .unreg = event_enable_unregister_trigger,
1365 .get_trigger_ops = event_enable_get_trigger_ops,
bac5fb97 1366 .set_filter = set_trigger_filter,
7862ad18
TZ
1367};
1368
1369static __init void unregister_trigger_enable_disable_cmds(void)
1370{
1371 unregister_event_command(&trigger_enable_cmd);
1372 unregister_event_command(&trigger_disable_cmd);
1373}
1374
1375static __init int register_trigger_enable_disable_cmds(void)
1376{
1377 int ret;
1378
1379 ret = register_event_command(&trigger_enable_cmd);
1380 if (WARN_ON(ret < 0))
1381 return ret;
1382 ret = register_event_command(&trigger_disable_cmd);
1383 if (WARN_ON(ret < 0))
1384 unregister_trigger_enable_disable_cmds();
1385
1386 return ret;
1387}
1388
2a2df321
TZ
1389static __init int register_trigger_traceon_traceoff_cmds(void)
1390{
1391 int ret;
1392
1393 ret = register_event_command(&trigger_traceon_cmd);
1394 if (WARN_ON(ret < 0))
1395 return ret;
1396 ret = register_event_command(&trigger_traceoff_cmd);
1397 if (WARN_ON(ret < 0))
1398 unregister_trigger_traceon_traceoff_cmds();
1399
1400 return ret;
1401}
1402
85f2b082
TZ
1403__init int register_trigger_cmds(void)
1404{
2a2df321 1405 register_trigger_traceon_traceoff_cmds();
93e31ffb 1406 register_trigger_snapshot_cmd();
f21ecbb3 1407 register_trigger_stacktrace_cmd();
7862ad18 1408 register_trigger_enable_disable_cmds();
2a2df321 1409
85f2b082
TZ
1410 return 0;
1411}