]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/trace_events_trigger.c
UBUNTU: link-to-tracker: update tracking bug
[mirror_ubuntu-bionic-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>
b2d09103 25#include <linux/rculist.h>
85f2b082
TZ
26
27#include "trace.h"
28
29static LIST_HEAD(trigger_commands);
30static DEFINE_MUTEX(trigger_cmd_mutex);
31
ab4bf008 32void trigger_data_free(struct event_trigger_data *data)
2a2df321 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
7f1d2f82 43 * @file: The trace_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 65enum event_trigger_type
7f1d2f82 66event_triggers_call(struct trace_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 75 list_for_each_entry_rcu(data, &file->triggers, list) {
104f2810
TZ
76 if (data->paused)
77 continue;
bac5fb97 78 if (!rec) {
c4a59230 79 data->ops->func(data, rec);
bac5fb97
TZ
80 continue;
81 }
561a4fe8 82 filter = rcu_dereference_sched(data->filter);
d8a30f20 83 if (filter && !filter_match_preds(filter, rec))
bac5fb97 84 continue;
353206f5 85 if (event_command_post_trigger(data->cmd_ops)) {
bac5fb97
TZ
86 tt |= data->cmd_ops->trigger_type;
87 continue;
88 }
c4a59230 89 data->ops->func(data, rec);
bac5fb97
TZ
90 }
91 return tt;
85f2b082
TZ
92}
93EXPORT_SYMBOL_GPL(event_triggers_call);
94
bac5fb97
TZ
95/**
96 * event_triggers_post_call - Call 'post_triggers' for a trace event
7f1d2f82 97 * @file: The trace_event_file associated with the event
bac5fb97 98 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
c4a59230 99 * @rec: The trace entry for the event
bac5fb97
TZ
100 *
101 * For each trigger associated with an event, invoke the trigger
102 * function registered with the associated trigger command, if the
103 * corresponding bit is set in the tt enum passed into this function.
104 * See @event_triggers_call for details on how those bits are set.
105 *
106 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
107 */
108void
7f1d2f82 109event_triggers_post_call(struct trace_event_file *file,
c4a59230
TZ
110 enum event_trigger_type tt,
111 void *rec)
bac5fb97
TZ
112{
113 struct event_trigger_data *data;
114
115 list_for_each_entry_rcu(data, &file->triggers, list) {
104f2810
TZ
116 if (data->paused)
117 continue;
bac5fb97 118 if (data->cmd_ops->trigger_type & tt)
c4a59230 119 data->ops->func(data, rec);
bac5fb97
TZ
120 }
121}
122EXPORT_SYMBOL_GPL(event_triggers_post_call);
123
dd97b954
SRRH
124#define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
125
85f2b082
TZ
126static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
127{
7f1d2f82 128 struct trace_event_file *event_file = event_file_data(m->private);
85f2b082 129
dd97b954
SRRH
130 if (t == SHOW_AVAILABLE_TRIGGERS)
131 return NULL;
132
85f2b082
TZ
133 return seq_list_next(t, &event_file->triggers, pos);
134}
135
136static void *trigger_start(struct seq_file *m, loff_t *pos)
137{
7f1d2f82 138 struct trace_event_file *event_file;
85f2b082
TZ
139
140 /* ->stop() is called even if ->start() fails */
141 mutex_lock(&event_mutex);
142 event_file = event_file_data(m->private);
143 if (unlikely(!event_file))
144 return ERR_PTR(-ENODEV);
145
dd97b954
SRRH
146 if (list_empty(&event_file->triggers))
147 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
148
85f2b082
TZ
149 return seq_list_start(&event_file->triggers, *pos);
150}
151
152static void trigger_stop(struct seq_file *m, void *t)
153{
154 mutex_unlock(&event_mutex);
155}
156
157static int trigger_show(struct seq_file *m, void *v)
158{
159 struct event_trigger_data *data;
dd97b954
SRRH
160 struct event_command *p;
161
162 if (v == SHOW_AVAILABLE_TRIGGERS) {
163 seq_puts(m, "# Available triggers:\n");
164 seq_putc(m, '#');
165 mutex_lock(&trigger_cmd_mutex);
166 list_for_each_entry_reverse(p, &trigger_commands, list)
167 seq_printf(m, " %s", p->name);
168 seq_putc(m, '\n');
169 mutex_unlock(&trigger_cmd_mutex);
170 return 0;
171 }
85f2b082
TZ
172
173 data = list_entry(v, struct event_trigger_data, list);
174 data->ops->print(m, data->ops, data);
175
176 return 0;
177}
178
179static const struct seq_operations event_triggers_seq_ops = {
180 .start = trigger_start,
181 .next = trigger_next,
182 .stop = trigger_stop,
183 .show = trigger_show,
184};
185
186static int event_trigger_regex_open(struct inode *inode, struct file *file)
187{
188 int ret = 0;
189
190 mutex_lock(&event_mutex);
191
192 if (unlikely(!event_file_data(file))) {
193 mutex_unlock(&event_mutex);
194 return -ENODEV;
195 }
196
a88e1cfb
TZ
197 if ((file->f_mode & FMODE_WRITE) &&
198 (file->f_flags & O_TRUNC)) {
199 struct trace_event_file *event_file;
200 struct event_command *p;
201
202 event_file = event_file_data(file);
203
204 list_for_each_entry(p, &trigger_commands, list) {
205 if (p->unreg_all)
206 p->unreg_all(event_file);
207 }
208 }
209
85f2b082
TZ
210 if (file->f_mode & FMODE_READ) {
211 ret = seq_open(file, &event_triggers_seq_ops);
212 if (!ret) {
213 struct seq_file *m = file->private_data;
214 m->private = file;
215 }
216 }
217
218 mutex_unlock(&event_mutex);
219
220 return ret;
221}
222
7f1d2f82 223static int trigger_process_regex(struct trace_event_file *file, char *buff)
85f2b082
TZ
224{
225 char *command, *next = buff;
226 struct event_command *p;
227 int ret = -EINVAL;
228
229 command = strsep(&next, ": \t");
230 command = (command[0] != '!') ? command : command + 1;
231
232 mutex_lock(&trigger_cmd_mutex);
233 list_for_each_entry(p, &trigger_commands, list) {
234 if (strcmp(p->name, command) == 0) {
235 ret = p->func(p, file, buff, command, next);
236 goto out_unlock;
237 }
238 }
239 out_unlock:
240 mutex_unlock(&trigger_cmd_mutex);
241
242 return ret;
243}
244
245static ssize_t event_trigger_regex_write(struct file *file,
246 const char __user *ubuf,
247 size_t cnt, loff_t *ppos)
248{
7f1d2f82 249 struct trace_event_file *event_file;
85f2b082
TZ
250 ssize_t ret;
251 char *buf;
252
253 if (!cnt)
254 return 0;
255
256 if (cnt >= PAGE_SIZE)
257 return -EINVAL;
258
70f6cbb6
AV
259 buf = memdup_user_nul(ubuf, cnt);
260 if (IS_ERR(buf))
261 return PTR_ERR(buf);
85f2b082 262
85f2b082
TZ
263 strim(buf);
264
265 mutex_lock(&event_mutex);
266 event_file = event_file_data(file);
267 if (unlikely(!event_file)) {
268 mutex_unlock(&event_mutex);
70f6cbb6 269 kfree(buf);
85f2b082
TZ
270 return -ENODEV;
271 }
272 ret = trigger_process_regex(event_file, buf);
273 mutex_unlock(&event_mutex);
274
70f6cbb6 275 kfree(buf);
85f2b082
TZ
276 if (ret < 0)
277 goto out;
278
279 *ppos += cnt;
280 ret = cnt;
281 out:
282 return ret;
283}
284
285static int event_trigger_regex_release(struct inode *inode, struct file *file)
286{
287 mutex_lock(&event_mutex);
288
289 if (file->f_mode & FMODE_READ)
290 seq_release(inode, file);
291
292 mutex_unlock(&event_mutex);
293
294 return 0;
295}
296
297static ssize_t
298event_trigger_write(struct file *filp, const char __user *ubuf,
299 size_t cnt, loff_t *ppos)
300{
301 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
302}
303
304static int
305event_trigger_open(struct inode *inode, struct file *filp)
306{
307 return event_trigger_regex_open(inode, filp);
308}
309
310static int
311event_trigger_release(struct inode *inode, struct file *file)
312{
313 return event_trigger_regex_release(inode, file);
314}
315
316const struct file_operations event_trigger_fops = {
317 .open = event_trigger_open,
318 .read = seq_read,
319 .write = event_trigger_write,
098c879e 320 .llseek = tracing_lseek,
85f2b082
TZ
321 .release = event_trigger_release,
322};
323
2a2df321
TZ
324/*
325 * Currently we only register event commands from __init, so mark this
326 * __init too.
327 */
ab4bf008 328__init int register_event_command(struct event_command *cmd)
2a2df321
TZ
329{
330 struct event_command *p;
331 int ret = 0;
332
333 mutex_lock(&trigger_cmd_mutex);
334 list_for_each_entry(p, &trigger_commands, list) {
335 if (strcmp(cmd->name, p->name) == 0) {
336 ret = -EBUSY;
337 goto out_unlock;
338 }
339 }
340 list_add(&cmd->list, &trigger_commands);
341 out_unlock:
342 mutex_unlock(&trigger_cmd_mutex);
343
344 return ret;
345}
346
347/*
348 * Currently we only unregister event commands from __init, so mark
349 * this __init too.
350 */
d0bad49b 351__init int unregister_event_command(struct event_command *cmd)
2a2df321
TZ
352{
353 struct event_command *p, *n;
354 int ret = -ENODEV;
355
356 mutex_lock(&trigger_cmd_mutex);
357 list_for_each_entry_safe(p, n, &trigger_commands, list) {
358 if (strcmp(cmd->name, p->name) == 0) {
359 ret = 0;
360 list_del_init(&p->list);
361 goto out_unlock;
362 }
363 }
364 out_unlock:
365 mutex_unlock(&trigger_cmd_mutex);
366
367 return ret;
368}
369
370/**
371 * event_trigger_print - Generic event_trigger_ops @print implementation
372 * @name: The name of the event trigger
373 * @m: The seq_file being printed to
374 * @data: Trigger-specific data
375 * @filter_str: filter_str to print, if present
376 *
377 * Common implementation for event triggers to print themselves.
378 *
379 * Usually wrapped by a function that simply sets the @name of the
380 * trigger command and then invokes this.
381 *
382 * Return: 0 on success, errno otherwise
383 */
384static int
385event_trigger_print(const char *name, struct seq_file *m,
386 void *data, char *filter_str)
387{
388 long count = (long)data;
389
fa6f0cc7 390 seq_puts(m, name);
2a2df321
TZ
391
392 if (count == -1)
393 seq_puts(m, ":unlimited");
394 else
395 seq_printf(m, ":count=%ld", count);
396
397 if (filter_str)
398 seq_printf(m, " if %s\n", filter_str);
399 else
1177e436 400 seq_putc(m, '\n');
2a2df321
TZ
401
402 return 0;
403}
404
405/**
406 * event_trigger_init - Generic event_trigger_ops @init implementation
407 * @ops: The trigger ops associated with the trigger
408 * @data: Trigger-specific data
409 *
410 * Common implementation of event trigger initialization.
411 *
412 * Usually used directly as the @init method in event trigger
413 * implementations.
414 *
415 * Return: 0 on success, errno otherwise
416 */
ab4bf008
TZ
417int event_trigger_init(struct event_trigger_ops *ops,
418 struct event_trigger_data *data)
2a2df321
TZ
419{
420 data->ref++;
421 return 0;
422}
423
424/**
425 * event_trigger_free - Generic event_trigger_ops @free implementation
426 * @ops: The trigger ops associated with the trigger
427 * @data: Trigger-specific data
428 *
429 * Common implementation of event trigger de-initialization.
430 *
431 * Usually used directly as the @free method in event trigger
432 * implementations.
433 */
434static void
435event_trigger_free(struct event_trigger_ops *ops,
436 struct event_trigger_data *data)
437{
438 if (WARN_ON_ONCE(data->ref <= 0))
439 return;
440
441 data->ref--;
442 if (!data->ref)
443 trigger_data_free(data);
444}
445
ab4bf008
TZ
446int trace_event_trigger_enable_disable(struct trace_event_file *file,
447 int trigger_enable)
85f2b082
TZ
448{
449 int ret = 0;
450
451 if (trigger_enable) {
452 if (atomic_inc_return(&file->tm_ref) > 1)
453 return ret;
5d6ad960 454 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
85f2b082
TZ
455 ret = trace_event_enable_disable(file, 1, 1);
456 } else {
457 if (atomic_dec_return(&file->tm_ref) > 0)
458 return ret;
5d6ad960 459 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
85f2b082
TZ
460 ret = trace_event_enable_disable(file, 0, 1);
461 }
462
463 return ret;
464}
465
466/**
467 * clear_event_triggers - Clear all triggers associated with a trace array
468 * @tr: The trace array to clear
469 *
470 * For each trigger, the triggering event has its tm_ref decremented
471 * via trace_event_trigger_enable_disable(), and any associated event
472 * (in the case of enable/disable_event triggers) will have its sm_ref
473 * decremented via free()->trace_event_enable_disable(). That
474 * combination effectively reverses the soft-mode/trigger state added
475 * by trigger registration.
476 *
477 * Must be called with event_mutex held.
478 */
479void
480clear_event_triggers(struct trace_array *tr)
481{
7f1d2f82 482 struct trace_event_file *file;
85f2b082
TZ
483
484 list_for_each_entry(file, &tr->events, list) {
7a89d3be
SRV
485 struct event_trigger_data *data, *n;
486 list_for_each_entry_safe(data, n, &file->triggers, list) {
85f2b082 487 trace_event_trigger_enable_disable(file, 0);
7a89d3be 488 list_del_rcu(&data->list);
85f2b082
TZ
489 if (data->ops->free)
490 data->ops->free(data->ops, data);
491 }
492 }
493}
494
bac5fb97
TZ
495/**
496 * update_cond_flag - Set or reset the TRIGGER_COND bit
7f1d2f82 497 * @file: The trace_event_file associated with the event
bac5fb97
TZ
498 *
499 * If an event has triggers and any of those triggers has a filter or
500 * a post_trigger, trigger invocation needs to be deferred until after
501 * the current event has logged its data, and the event should have
502 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
503 * cleared.
504 */
ab4bf008 505void update_cond_flag(struct trace_event_file *file)
bac5fb97
TZ
506{
507 struct event_trigger_data *data;
508 bool set_cond = false;
509
510 list_for_each_entry_rcu(data, &file->triggers, list) {
353206f5
SRRH
511 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
512 event_command_needs_rec(data->cmd_ops)) {
bac5fb97
TZ
513 set_cond = true;
514 break;
515 }
516 }
517
518 if (set_cond)
5d6ad960 519 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
bac5fb97 520 else
5d6ad960 521 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
bac5fb97
TZ
522}
523
2a2df321
TZ
524/**
525 * register_trigger - Generic event_command @reg implementation
526 * @glob: The raw string used to register the trigger
527 * @ops: The trigger ops associated with the trigger
528 * @data: Trigger-specific data to associate with the trigger
7f1d2f82 529 * @file: The trace_event_file associated with the event
2a2df321
TZ
530 *
531 * Common implementation for event trigger registration.
532 *
533 * Usually used directly as the @reg method in event command
534 * implementations.
535 *
536 * Return: 0 on success, errno otherwise
537 */
538static int register_trigger(char *glob, struct event_trigger_ops *ops,
539 struct event_trigger_data *data,
7f1d2f82 540 struct trace_event_file *file)
2a2df321
TZ
541{
542 struct event_trigger_data *test;
543 int ret = 0;
544
545 list_for_each_entry_rcu(test, &file->triggers, list) {
546 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
547 ret = -EEXIST;
548 goto out;
549 }
550 }
551
552 if (data->ops->init) {
553 ret = data->ops->init(data->ops, data);
554 if (ret < 0)
555 goto out;
556 }
557
558 list_add_rcu(&data->list, &file->triggers);
559 ret++;
560
4e4a4d75 561 update_cond_flag(file);
2a2df321
TZ
562 if (trace_event_trigger_enable_disable(file, 1) < 0) {
563 list_del_rcu(&data->list);
4e4a4d75 564 update_cond_flag(file);
2a2df321
TZ
565 ret--;
566 }
567out:
568 return ret;
569}
570
571/**
572 * unregister_trigger - Generic event_command @unreg implementation
573 * @glob: The raw string used to register the trigger
574 * @ops: The trigger ops associated with the trigger
575 * @test: Trigger-specific data used to find the trigger to remove
7f1d2f82 576 * @file: The trace_event_file associated with the event
2a2df321
TZ
577 *
578 * Common implementation for event trigger unregistration.
579 *
580 * Usually used directly as the @unreg method in event command
581 * implementations.
582 */
ab4bf008
TZ
583void unregister_trigger(char *glob, struct event_trigger_ops *ops,
584 struct event_trigger_data *test,
585 struct trace_event_file *file)
2a2df321
TZ
586{
587 struct event_trigger_data *data;
588 bool unregistered = false;
589
590 list_for_each_entry_rcu(data, &file->triggers, list) {
591 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
592 unregistered = true;
593 list_del_rcu(&data->list);
594 trace_event_trigger_enable_disable(file, 0);
4e4a4d75 595 update_cond_flag(file);
2a2df321
TZ
596 break;
597 }
598 }
599
600 if (unregistered && data->ops->free)
601 data->ops->free(data->ops, data);
602}
603
604/**
605 * event_trigger_callback - Generic event_command @func implementation
606 * @cmd_ops: The command ops, used for trigger registration
7f1d2f82 607 * @file: The trace_event_file associated with the event
2a2df321
TZ
608 * @glob: The raw string used to register the trigger
609 * @cmd: The cmd portion of the string used to register the trigger
610 * @param: The params portion of the string used to register the trigger
611 *
612 * Common implementation for event command parsing and trigger
613 * instantiation.
614 *
615 * Usually used directly as the @func method in event command
616 * implementations.
617 *
618 * Return: 0 on success, errno otherwise
619 */
620static int
621event_trigger_callback(struct event_command *cmd_ops,
7f1d2f82 622 struct trace_event_file *file,
2a2df321
TZ
623 char *glob, char *cmd, char *param)
624{
625 struct event_trigger_data *trigger_data;
626 struct event_trigger_ops *trigger_ops;
627 char *trigger = NULL;
628 char *number;
629 int ret;
630
631 /* separate the trigger from the filter (t:n [if filter]) */
632 if (param && isdigit(param[0]))
633 trigger = strsep(&param, " \t");
634
635 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
636
637 ret = -ENOMEM;
638 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
639 if (!trigger_data)
640 goto out;
641
642 trigger_data->count = -1;
643 trigger_data->ops = trigger_ops;
644 trigger_data->cmd_ops = cmd_ops;
9ccd9a81 645 trigger_data->private_data = file;
2a2df321 646 INIT_LIST_HEAD(&trigger_data->list);
db1388b4 647 INIT_LIST_HEAD(&trigger_data->named_list);
2a2df321
TZ
648
649 if (glob[0] == '!') {
650 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
651 kfree(trigger_data);
652 ret = 0;
653 goto out;
654 }
655
656 if (trigger) {
657 number = strsep(&trigger, ":");
658
659 ret = -EINVAL;
660 if (!strlen(number))
661 goto out_free;
662
663 /*
664 * We use the callback data field (which is a pointer)
665 * as our counter.
666 */
667 ret = kstrtoul(number, 0, &trigger_data->count);
668 if (ret)
669 goto out_free;
670 }
671
672 if (!param) /* if param is non-empty, it's supposed to be a filter */
673 goto out_reg;
674
675 if (!cmd_ops->set_filter)
676 goto out_reg;
677
678 ret = cmd_ops->set_filter(param, trigger_data, file);
679 if (ret < 0)
680 goto out_free;
681
682 out_reg:
683 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
684 /*
685 * The above returns on success the # of functions enabled,
686 * but if it didn't find any functions it returns zero.
687 * Consider no functions a failure too.
688 */
689 if (!ret) {
690 ret = -ENOENT;
691 goto out_free;
692 } else if (ret < 0)
693 goto out_free;
694 ret = 0;
695 out:
696 return ret;
697
698 out_free:
bac5fb97
TZ
699 if (cmd_ops->set_filter)
700 cmd_ops->set_filter(NULL, trigger_data, NULL);
2a2df321
TZ
701 kfree(trigger_data);
702 goto out;
703}
704
bac5fb97
TZ
705/**
706 * set_trigger_filter - Generic event_command @set_filter implementation
707 * @filter_str: The filter string for the trigger, NULL to remove filter
708 * @trigger_data: Trigger-specific data
7f1d2f82 709 * @file: The trace_event_file associated with the event
bac5fb97
TZ
710 *
711 * Common implementation for event command filter parsing and filter
712 * instantiation.
713 *
714 * Usually used directly as the @set_filter method in event command
715 * implementations.
716 *
717 * Also used to remove a filter (if filter_str = NULL).
718 *
719 * Return: 0 on success, errno otherwise
720 */
ab4bf008
TZ
721int set_trigger_filter(char *filter_str,
722 struct event_trigger_data *trigger_data,
723 struct trace_event_file *file)
bac5fb97
TZ
724{
725 struct event_trigger_data *data = trigger_data;
726 struct event_filter *filter = NULL, *tmp;
727 int ret = -EINVAL;
728 char *s;
729
730 if (!filter_str) /* clear the current filter */
731 goto assign;
732
733 s = strsep(&filter_str, " \t");
734
735 if (!strlen(s) || strcmp(s, "if") != 0)
736 goto out;
737
738 if (!filter_str)
739 goto out;
740
741 /* The filter is for the 'trigger' event, not the triggered event */
742 ret = create_event_filter(file->event_call, filter_str, false, &filter);
743 if (ret)
744 goto out;
745 assign:
d8a30f20 746 tmp = rcu_access_pointer(data->filter);
bac5fb97
TZ
747
748 rcu_assign_pointer(data->filter, filter);
749
750 if (tmp) {
751 /* Make sure the call is done with the filter */
752 synchronize_sched();
753 free_event_filter(tmp);
754 }
755
756 kfree(data->filter_str);
757 data->filter_str = NULL;
758
759 if (filter_str) {
760 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
761 if (!data->filter_str) {
d8a30f20 762 free_event_filter(rcu_access_pointer(data->filter));
bac5fb97
TZ
763 data->filter = NULL;
764 ret = -ENOMEM;
765 }
766 }
767 out:
768 return ret;
769}
770
db1388b4
TZ
771static LIST_HEAD(named_triggers);
772
773/**
774 * find_named_trigger - Find the common named trigger associated with @name
775 * @name: The name of the set of named triggers to find the common data for
776 *
777 * Named triggers are sets of triggers that share a common set of
778 * trigger data. The first named trigger registered with a given name
779 * owns the common trigger data that the others subsequently
780 * registered with the same name will reference. This function
781 * returns the common trigger data associated with that first
782 * registered instance.
783 *
784 * Return: the common trigger data for the given named trigger on
785 * success, NULL otherwise.
786 */
787struct event_trigger_data *find_named_trigger(const char *name)
788{
789 struct event_trigger_data *data;
790
791 if (!name)
792 return NULL;
793
794 list_for_each_entry(data, &named_triggers, named_list) {
795 if (data->named_data)
796 continue;
797 if (strcmp(data->name, name) == 0)
798 return data;
799 }
800
801 return NULL;
802}
803
804/**
805 * is_named_trigger - determine if a given trigger is a named trigger
806 * @test: The trigger data to test
807 *
808 * Return: true if 'test' is a named trigger, false otherwise.
809 */
810bool is_named_trigger(struct event_trigger_data *test)
811{
812 struct event_trigger_data *data;
813
814 list_for_each_entry(data, &named_triggers, named_list) {
815 if (test == data)
816 return true;
817 }
818
819 return false;
820}
821
822/**
823 * save_named_trigger - save the trigger in the named trigger list
824 * @name: The name of the named trigger set
825 * @data: The trigger data to save
826 *
827 * Return: 0 if successful, negative error otherwise.
828 */
829int save_named_trigger(const char *name, struct event_trigger_data *data)
830{
831 data->name = kstrdup(name, GFP_KERNEL);
832 if (!data->name)
833 return -ENOMEM;
834
835 list_add(&data->named_list, &named_triggers);
836
837 return 0;
838}
839
840/**
841 * del_named_trigger - delete a trigger from the named trigger list
842 * @data: The trigger data to delete
843 */
844void del_named_trigger(struct event_trigger_data *data)
845{
846 kfree(data->name);
847 data->name = NULL;
848
849 list_del(&data->named_list);
850}
851
852static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
853{
854 struct event_trigger_data *test;
855
856 list_for_each_entry(test, &named_triggers, named_list) {
857 if (strcmp(test->name, data->name) == 0) {
858 if (pause) {
859 test->paused_tmp = test->paused;
860 test->paused = true;
861 } else {
862 test->paused = test->paused_tmp;
863 }
864 }
865 }
866}
867
868/**
869 * pause_named_trigger - Pause all named triggers with the same name
870 * @data: The trigger data of a named trigger to pause
871 *
872 * Pauses a named trigger along with all other triggers having the
873 * same name. Because named triggers share a common set of data,
874 * pausing only one is meaningless, so pausing one named trigger needs
875 * to pause all triggers with the same name.
876 */
877void pause_named_trigger(struct event_trigger_data *data)
878{
879 __pause_named_trigger(data, true);
880}
881
882/**
883 * unpause_named_trigger - Un-pause all named triggers with the same name
884 * @data: The trigger data of a named trigger to unpause
885 *
886 * Un-pauses a named trigger along with all other triggers having the
887 * same name. Because named triggers share a common set of data,
888 * unpausing only one is meaningless, so unpausing one named trigger
889 * needs to unpause all triggers with the same name.
890 */
891void unpause_named_trigger(struct event_trigger_data *data)
892{
893 __pause_named_trigger(data, false);
894}
895
896/**
897 * set_named_trigger_data - Associate common named trigger data
898 * @data: The trigger data of a named trigger to unpause
899 *
900 * Named triggers are sets of triggers that share a common set of
901 * trigger data. The first named trigger registered with a given name
902 * owns the common trigger data that the others subsequently
903 * registered with the same name will reference. This function
904 * associates the common trigger data from the first trigger with the
905 * given trigger.
906 */
907void set_named_trigger_data(struct event_trigger_data *data,
908 struct event_trigger_data *named_data)
909{
910 data->named_data = named_data;
911}
912
2a2df321 913static void
c4a59230 914traceon_trigger(struct event_trigger_data *data, void *rec)
2a2df321
TZ
915{
916 if (tracing_is_on())
917 return;
918
919 tracing_on();
920}
921
922static void
c4a59230 923traceon_count_trigger(struct event_trigger_data *data, void *rec)
2a2df321 924{
e8dc6371
SRRH
925 if (tracing_is_on())
926 return;
927
2a2df321
TZ
928 if (!data->count)
929 return;
930
931 if (data->count != -1)
932 (data->count)--;
933
e8dc6371 934 tracing_on();
2a2df321
TZ
935}
936
937static void
c4a59230 938traceoff_trigger(struct event_trigger_data *data, void *rec)
2a2df321
TZ
939{
940 if (!tracing_is_on())
941 return;
942
943 tracing_off();
944}
945
946static void
c4a59230 947traceoff_count_trigger(struct event_trigger_data *data, void *rec)
2a2df321 948{
e8dc6371
SRRH
949 if (!tracing_is_on())
950 return;
951
2a2df321
TZ
952 if (!data->count)
953 return;
954
955 if (data->count != -1)
956 (data->count)--;
957
e8dc6371 958 tracing_off();
2a2df321
TZ
959}
960
961static int
962traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
963 struct event_trigger_data *data)
964{
965 return event_trigger_print("traceon", m, (void *)data->count,
966 data->filter_str);
967}
968
969static int
970traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
971 struct event_trigger_data *data)
972{
973 return event_trigger_print("traceoff", m, (void *)data->count,
974 data->filter_str);
975}
976
977static struct event_trigger_ops traceon_trigger_ops = {
978 .func = traceon_trigger,
979 .print = traceon_trigger_print,
980 .init = event_trigger_init,
981 .free = event_trigger_free,
982};
983
984static struct event_trigger_ops traceon_count_trigger_ops = {
985 .func = traceon_count_trigger,
986 .print = traceon_trigger_print,
987 .init = event_trigger_init,
988 .free = event_trigger_free,
989};
990
991static struct event_trigger_ops traceoff_trigger_ops = {
992 .func = traceoff_trigger,
993 .print = traceoff_trigger_print,
994 .init = event_trigger_init,
995 .free = event_trigger_free,
996};
997
998static struct event_trigger_ops traceoff_count_trigger_ops = {
999 .func = traceoff_count_trigger,
1000 .print = traceoff_trigger_print,
1001 .init = event_trigger_init,
1002 .free = event_trigger_free,
1003};
1004
1005static struct event_trigger_ops *
1006onoff_get_trigger_ops(char *cmd, char *param)
1007{
1008 struct event_trigger_ops *ops;
1009
1010 /* we register both traceon and traceoff to this callback */
1011 if (strcmp(cmd, "traceon") == 0)
1012 ops = param ? &traceon_count_trigger_ops :
1013 &traceon_trigger_ops;
1014 else
1015 ops = param ? &traceoff_count_trigger_ops :
1016 &traceoff_trigger_ops;
1017
1018 return ops;
1019}
1020
1021static struct event_command trigger_traceon_cmd = {
1022 .name = "traceon",
1023 .trigger_type = ETT_TRACE_ONOFF,
1024 .func = event_trigger_callback,
1025 .reg = register_trigger,
1026 .unreg = unregister_trigger,
1027 .get_trigger_ops = onoff_get_trigger_ops,
bac5fb97 1028 .set_filter = set_trigger_filter,
2a2df321
TZ
1029};
1030
1031static struct event_command trigger_traceoff_cmd = {
1032 .name = "traceoff",
1033 .trigger_type = ETT_TRACE_ONOFF,
a0d0c621 1034 .flags = EVENT_CMD_FL_POST_TRIGGER,
2a2df321
TZ
1035 .func = event_trigger_callback,
1036 .reg = register_trigger,
1037 .unreg = unregister_trigger,
1038 .get_trigger_ops = onoff_get_trigger_ops,
bac5fb97 1039 .set_filter = set_trigger_filter,
2a2df321
TZ
1040};
1041
93e31ffb
TZ
1042#ifdef CONFIG_TRACER_SNAPSHOT
1043static void
c4a59230 1044snapshot_trigger(struct event_trigger_data *data, void *rec)
93e31ffb 1045{
9ccd9a81
SRV
1046 struct trace_event_file *file = data->private_data;
1047
1048 if (file)
1049 tracing_snapshot_instance(file->tr);
1050 else
1051 tracing_snapshot();
93e31ffb
TZ
1052}
1053
1054static void
c4a59230 1055snapshot_count_trigger(struct event_trigger_data *data, void *rec)
93e31ffb
TZ
1056{
1057 if (!data->count)
1058 return;
1059
1060 if (data->count != -1)
1061 (data->count)--;
1062
c4a59230 1063 snapshot_trigger(data, rec);
93e31ffb
TZ
1064}
1065
1066static int
1067register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1068 struct event_trigger_data *data,
7f1d2f82 1069 struct trace_event_file *file)
93e31ffb
TZ
1070{
1071 int ret = register_trigger(glob, ops, data, file);
1072
9ccd9a81 1073 if (ret > 0 && tracing_alloc_snapshot_instance(file->tr) != 0) {
93e31ffb
TZ
1074 unregister_trigger(glob, ops, data, file);
1075 ret = 0;
1076 }
1077
1078 return ret;
1079}
1080
1081static int
1082snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1083 struct event_trigger_data *data)
1084{
1085 return event_trigger_print("snapshot", m, (void *)data->count,
1086 data->filter_str);
1087}
1088
1089static struct event_trigger_ops snapshot_trigger_ops = {
1090 .func = snapshot_trigger,
1091 .print = snapshot_trigger_print,
1092 .init = event_trigger_init,
1093 .free = event_trigger_free,
1094};
1095
1096static struct event_trigger_ops snapshot_count_trigger_ops = {
1097 .func = snapshot_count_trigger,
1098 .print = snapshot_trigger_print,
1099 .init = event_trigger_init,
1100 .free = event_trigger_free,
1101};
1102
1103static struct event_trigger_ops *
1104snapshot_get_trigger_ops(char *cmd, char *param)
1105{
1106 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1107}
1108
1109static struct event_command trigger_snapshot_cmd = {
1110 .name = "snapshot",
1111 .trigger_type = ETT_SNAPSHOT,
1112 .func = event_trigger_callback,
1113 .reg = register_snapshot_trigger,
1114 .unreg = unregister_trigger,
1115 .get_trigger_ops = snapshot_get_trigger_ops,
bac5fb97 1116 .set_filter = set_trigger_filter,
93e31ffb
TZ
1117};
1118
1119static __init int register_trigger_snapshot_cmd(void)
1120{
1121 int ret;
1122
1123 ret = register_event_command(&trigger_snapshot_cmd);
1124 WARN_ON(ret < 0);
1125
1126 return ret;
1127}
1128#else
1129static __init int register_trigger_snapshot_cmd(void) { return 0; }
1130#endif /* CONFIG_TRACER_SNAPSHOT */
1131
f21ecbb3 1132#ifdef CONFIG_STACKTRACE
2ee5b92a
SRV
1133#ifdef CONFIG_UNWINDER_ORC
1134/* Skip 2:
1135 * event_triggers_post_call()
1136 * trace_event_raw_event_xxx()
1137 */
1138# define STACK_SKIP 2
1139#else
f21ecbb3 1140/*
2ee5b92a 1141 * Skip 4:
f21ecbb3
TZ
1142 * stacktrace_trigger()
1143 * event_triggers_post_call()
2ee5b92a 1144 * trace_event_buffer_commit()
a7237765 1145 * trace_event_raw_event_xxx()
f21ecbb3 1146 */
2ee5b92a
SRV
1147#define STACK_SKIP 4
1148#endif
f21ecbb3
TZ
1149
1150static void
c4a59230 1151stacktrace_trigger(struct event_trigger_data *data, void *rec)
f21ecbb3
TZ
1152{
1153 trace_dump_stack(STACK_SKIP);
1154}
1155
1156static void
c4a59230 1157stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
f21ecbb3
TZ
1158{
1159 if (!data->count)
1160 return;
1161
1162 if (data->count != -1)
1163 (data->count)--;
1164
c4a59230 1165 stacktrace_trigger(data, rec);
f21ecbb3
TZ
1166}
1167
1168static int
1169stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1170 struct event_trigger_data *data)
1171{
1172 return event_trigger_print("stacktrace", m, (void *)data->count,
1173 data->filter_str);
1174}
1175
1176static struct event_trigger_ops stacktrace_trigger_ops = {
1177 .func = stacktrace_trigger,
1178 .print = stacktrace_trigger_print,
1179 .init = event_trigger_init,
1180 .free = event_trigger_free,
1181};
1182
1183static struct event_trigger_ops stacktrace_count_trigger_ops = {
1184 .func = stacktrace_count_trigger,
1185 .print = stacktrace_trigger_print,
1186 .init = event_trigger_init,
1187 .free = event_trigger_free,
1188};
1189
1190static struct event_trigger_ops *
1191stacktrace_get_trigger_ops(char *cmd, char *param)
1192{
1193 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1194}
1195
1196static struct event_command trigger_stacktrace_cmd = {
1197 .name = "stacktrace",
1198 .trigger_type = ETT_STACKTRACE,
353206f5 1199 .flags = EVENT_CMD_FL_POST_TRIGGER,
f21ecbb3
TZ
1200 .func = event_trigger_callback,
1201 .reg = register_trigger,
1202 .unreg = unregister_trigger,
1203 .get_trigger_ops = stacktrace_get_trigger_ops,
bac5fb97 1204 .set_filter = set_trigger_filter,
f21ecbb3
TZ
1205};
1206
1207static __init int register_trigger_stacktrace_cmd(void)
1208{
1209 int ret;
1210
1211 ret = register_event_command(&trigger_stacktrace_cmd);
1212 WARN_ON(ret < 0);
1213
1214 return ret;
1215}
1216#else
1217static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1218#endif /* CONFIG_STACKTRACE */
1219
2a2df321
TZ
1220static __init void unregister_trigger_traceon_traceoff_cmds(void)
1221{
1222 unregister_event_command(&trigger_traceon_cmd);
1223 unregister_event_command(&trigger_traceoff_cmd);
1224}
1225
7862ad18 1226static void
c4a59230 1227event_enable_trigger(struct event_trigger_data *data, void *rec)
7862ad18
TZ
1228{
1229 struct enable_trigger_data *enable_data = data->private_data;
1230
1231 if (enable_data->enable)
5d6ad960 1232 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
7862ad18 1233 else
5d6ad960 1234 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
7862ad18
TZ
1235}
1236
1237static void
c4a59230 1238event_enable_count_trigger(struct event_trigger_data *data, void *rec)
7862ad18
TZ
1239{
1240 struct enable_trigger_data *enable_data = data->private_data;
1241
1242 if (!data->count)
1243 return;
1244
1245 /* Skip if the event is in a state we want to switch to */
5d6ad960 1246 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
7862ad18
TZ
1247 return;
1248
1249 if (data->count != -1)
1250 (data->count)--;
1251
c4a59230 1252 event_enable_trigger(data, rec);
7862ad18
TZ
1253}
1254
d0bad49b
TZ
1255int event_enable_trigger_print(struct seq_file *m,
1256 struct event_trigger_ops *ops,
1257 struct event_trigger_data *data)
7862ad18
TZ
1258{
1259 struct enable_trigger_data *enable_data = data->private_data;
1260
1261 seq_printf(m, "%s:%s:%s",
d0bad49b
TZ
1262 enable_data->hist ?
1263 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1264 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
7862ad18 1265 enable_data->file->event_call->class->system,
687fcc4a 1266 trace_event_name(enable_data->file->event_call));
7862ad18
TZ
1267
1268 if (data->count == -1)
1269 seq_puts(m, ":unlimited");
1270 else
1271 seq_printf(m, ":count=%ld", data->count);
1272
1273 if (data->filter_str)
1274 seq_printf(m, " if %s\n", data->filter_str);
1275 else
1177e436 1276 seq_putc(m, '\n');
7862ad18
TZ
1277
1278 return 0;
1279}
1280
d0bad49b
TZ
1281void event_enable_trigger_free(struct event_trigger_ops *ops,
1282 struct event_trigger_data *data)
7862ad18
TZ
1283{
1284 struct enable_trigger_data *enable_data = data->private_data;
1285
1286 if (WARN_ON_ONCE(data->ref <= 0))
1287 return;
1288
1289 data->ref--;
1290 if (!data->ref) {
1291 /* Remove the SOFT_MODE flag */
1292 trace_event_enable_disable(enable_data->file, 0, 1);
1293 module_put(enable_data->file->event_call->mod);
1294 trigger_data_free(data);
1295 kfree(enable_data);
1296 }
1297}
1298
1299static struct event_trigger_ops event_enable_trigger_ops = {
1300 .func = event_enable_trigger,
1301 .print = event_enable_trigger_print,
1302 .init = event_trigger_init,
1303 .free = event_enable_trigger_free,
1304};
1305
1306static struct event_trigger_ops event_enable_count_trigger_ops = {
1307 .func = event_enable_count_trigger,
1308 .print = event_enable_trigger_print,
1309 .init = event_trigger_init,
1310 .free = event_enable_trigger_free,
1311};
1312
1313static struct event_trigger_ops event_disable_trigger_ops = {
1314 .func = event_enable_trigger,
1315 .print = event_enable_trigger_print,
1316 .init = event_trigger_init,
1317 .free = event_enable_trigger_free,
1318};
1319
1320static struct event_trigger_ops event_disable_count_trigger_ops = {
1321 .func = event_enable_count_trigger,
1322 .print = event_enable_trigger_print,
1323 .init = event_trigger_init,
1324 .free = event_enable_trigger_free,
1325};
1326
d0bad49b
TZ
1327int event_enable_trigger_func(struct event_command *cmd_ops,
1328 struct trace_event_file *file,
1329 char *glob, char *cmd, char *param)
7862ad18 1330{
7f1d2f82 1331 struct trace_event_file *event_enable_file;
7862ad18
TZ
1332 struct enable_trigger_data *enable_data;
1333 struct event_trigger_data *trigger_data;
1334 struct event_trigger_ops *trigger_ops;
1335 struct trace_array *tr = file->tr;
1336 const char *system;
1337 const char *event;
d0bad49b 1338 bool hist = false;
7862ad18
TZ
1339 char *trigger;
1340 char *number;
1341 bool enable;
1342 int ret;
1343
1344 if (!param)
1345 return -EINVAL;
1346
1347 /* separate the trigger from the filter (s:e:n [if filter]) */
1348 trigger = strsep(&param, " \t");
1349 if (!trigger)
1350 return -EINVAL;
1351
1352 system = strsep(&trigger, ":");
1353 if (!trigger)
1354 return -EINVAL;
1355
1356 event = strsep(&trigger, ":");
1357
1358 ret = -EINVAL;
1359 event_enable_file = find_event_file(tr, system, event);
1360 if (!event_enable_file)
1361 goto out;
1362
d0bad49b
TZ
1363#ifdef CONFIG_HIST_TRIGGERS
1364 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1365 (strcmp(cmd, DISABLE_HIST_STR) == 0));
7862ad18 1366
d0bad49b
TZ
1367 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1368 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1369#else
1370 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1371#endif
7862ad18
TZ
1372 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1373
1374 ret = -ENOMEM;
1375 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1376 if (!trigger_data)
1377 goto out;
1378
1379 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1380 if (!enable_data) {
1381 kfree(trigger_data);
1382 goto out;
1383 }
1384
1385 trigger_data->count = -1;
1386 trigger_data->ops = trigger_ops;
1387 trigger_data->cmd_ops = cmd_ops;
1388 INIT_LIST_HEAD(&trigger_data->list);
1389 RCU_INIT_POINTER(trigger_data->filter, NULL);
1390
d0bad49b 1391 enable_data->hist = hist;
7862ad18
TZ
1392 enable_data->enable = enable;
1393 enable_data->file = event_enable_file;
1394 trigger_data->private_data = enable_data;
1395
1396 if (glob[0] == '!') {
1397 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1398 kfree(trigger_data);
1399 kfree(enable_data);
1400 ret = 0;
1401 goto out;
1402 }
1403
1404 if (trigger) {
1405 number = strsep(&trigger, ":");
1406
1407 ret = -EINVAL;
1408 if (!strlen(number))
1409 goto out_free;
1410
1411 /*
1412 * We use the callback data field (which is a pointer)
1413 * as our counter.
1414 */
1415 ret = kstrtoul(number, 0, &trigger_data->count);
1416 if (ret)
1417 goto out_free;
1418 }
1419
1420 if (!param) /* if param is non-empty, it's supposed to be a filter */
1421 goto out_reg;
1422
1423 if (!cmd_ops->set_filter)
1424 goto out_reg;
1425
1426 ret = cmd_ops->set_filter(param, trigger_data, file);
1427 if (ret < 0)
1428 goto out_free;
1429
1430 out_reg:
1431 /* Don't let event modules unload while probe registered */
1432 ret = try_module_get(event_enable_file->event_call->mod);
1433 if (!ret) {
1434 ret = -EBUSY;
1435 goto out_free;
1436 }
1437
1438 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1439 if (ret < 0)
1440 goto out_put;
1441 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1442 /*
1443 * The above returns on success the # of functions enabled,
1444 * but if it didn't find any functions it returns zero.
1445 * Consider no functions a failure too.
1446 */
1447 if (!ret) {
1448 ret = -ENOENT;
1449 goto out_disable;
1450 } else if (ret < 0)
1451 goto out_disable;
1452 /* Just return zero, not the number of enabled functions */
1453 ret = 0;
1454 out:
1455 return ret;
1456
1457 out_disable:
1458 trace_event_enable_disable(event_enable_file, 0, 1);
1459 out_put:
1460 module_put(event_enable_file->event_call->mod);
1461 out_free:
bac5fb97
TZ
1462 if (cmd_ops->set_filter)
1463 cmd_ops->set_filter(NULL, trigger_data, NULL);
7862ad18
TZ
1464 kfree(trigger_data);
1465 kfree(enable_data);
1466 goto out;
1467}
1468
d0bad49b
TZ
1469int event_enable_register_trigger(char *glob,
1470 struct event_trigger_ops *ops,
1471 struct event_trigger_data *data,
1472 struct trace_event_file *file)
7862ad18
TZ
1473{
1474 struct enable_trigger_data *enable_data = data->private_data;
1475 struct enable_trigger_data *test_enable_data;
1476 struct event_trigger_data *test;
1477 int ret = 0;
1478
1479 list_for_each_entry_rcu(test, &file->triggers, list) {
1480 test_enable_data = test->private_data;
1481 if (test_enable_data &&
d0bad49b
TZ
1482 (test->cmd_ops->trigger_type ==
1483 data->cmd_ops->trigger_type) &&
7862ad18
TZ
1484 (test_enable_data->file == enable_data->file)) {
1485 ret = -EEXIST;
1486 goto out;
1487 }
1488 }
1489
1490 if (data->ops->init) {
1491 ret = data->ops->init(data->ops, data);
1492 if (ret < 0)
1493 goto out;
1494 }
1495
1496 list_add_rcu(&data->list, &file->triggers);
1497 ret++;
1498
4e4a4d75 1499 update_cond_flag(file);
7862ad18
TZ
1500 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1501 list_del_rcu(&data->list);
4e4a4d75 1502 update_cond_flag(file);
7862ad18
TZ
1503 ret--;
1504 }
1505out:
1506 return ret;
1507}
1508
d0bad49b
TZ
1509void event_enable_unregister_trigger(char *glob,
1510 struct event_trigger_ops *ops,
1511 struct event_trigger_data *test,
1512 struct trace_event_file *file)
7862ad18
TZ
1513{
1514 struct enable_trigger_data *test_enable_data = test->private_data;
1515 struct enable_trigger_data *enable_data;
1516 struct event_trigger_data *data;
1517 bool unregistered = false;
1518
1519 list_for_each_entry_rcu(data, &file->triggers, list) {
1520 enable_data = data->private_data;
1521 if (enable_data &&
d0bad49b
TZ
1522 (data->cmd_ops->trigger_type ==
1523 test->cmd_ops->trigger_type) &&
7862ad18
TZ
1524 (enable_data->file == test_enable_data->file)) {
1525 unregistered = true;
1526 list_del_rcu(&data->list);
1527 trace_event_trigger_enable_disable(file, 0);
4e4a4d75 1528 update_cond_flag(file);
7862ad18
TZ
1529 break;
1530 }
1531 }
1532
1533 if (unregistered && data->ops->free)
1534 data->ops->free(data->ops, data);
1535}
1536
1537static struct event_trigger_ops *
1538event_enable_get_trigger_ops(char *cmd, char *param)
1539{
1540 struct event_trigger_ops *ops;
1541 bool enable;
1542
d0bad49b
TZ
1543#ifdef CONFIG_HIST_TRIGGERS
1544 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1545 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1546#else
7862ad18 1547 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
d0bad49b 1548#endif
7862ad18
TZ
1549 if (enable)
1550 ops = param ? &event_enable_count_trigger_ops :
1551 &event_enable_trigger_ops;
1552 else
1553 ops = param ? &event_disable_count_trigger_ops :
1554 &event_disable_trigger_ops;
1555
1556 return ops;
1557}
1558
1559static struct event_command trigger_enable_cmd = {
1560 .name = ENABLE_EVENT_STR,
1561 .trigger_type = ETT_EVENT_ENABLE,
1562 .func = event_enable_trigger_func,
1563 .reg = event_enable_register_trigger,
1564 .unreg = event_enable_unregister_trigger,
1565 .get_trigger_ops = event_enable_get_trigger_ops,
bac5fb97 1566 .set_filter = set_trigger_filter,
7862ad18
TZ
1567};
1568
1569static struct event_command trigger_disable_cmd = {
1570 .name = DISABLE_EVENT_STR,
1571 .trigger_type = ETT_EVENT_ENABLE,
1572 .func = event_enable_trigger_func,
1573 .reg = event_enable_register_trigger,
1574 .unreg = event_enable_unregister_trigger,
1575 .get_trigger_ops = event_enable_get_trigger_ops,
bac5fb97 1576 .set_filter = set_trigger_filter,
7862ad18
TZ
1577};
1578
1579static __init void unregister_trigger_enable_disable_cmds(void)
1580{
1581 unregister_event_command(&trigger_enable_cmd);
1582 unregister_event_command(&trigger_disable_cmd);
1583}
1584
1585static __init int register_trigger_enable_disable_cmds(void)
1586{
1587 int ret;
1588
1589 ret = register_event_command(&trigger_enable_cmd);
1590 if (WARN_ON(ret < 0))
1591 return ret;
1592 ret = register_event_command(&trigger_disable_cmd);
1593 if (WARN_ON(ret < 0))
1594 unregister_trigger_enable_disable_cmds();
1595
1596 return ret;
1597}
1598
2a2df321
TZ
1599static __init int register_trigger_traceon_traceoff_cmds(void)
1600{
1601 int ret;
1602
1603 ret = register_event_command(&trigger_traceon_cmd);
1604 if (WARN_ON(ret < 0))
1605 return ret;
1606 ret = register_event_command(&trigger_traceoff_cmd);
1607 if (WARN_ON(ret < 0))
1608 unregister_trigger_traceon_traceoff_cmds();
1609
1610 return ret;
1611}
1612
85f2b082
TZ
1613__init int register_trigger_cmds(void)
1614{
2a2df321 1615 register_trigger_traceon_traceoff_cmds();
93e31ffb 1616 register_trigger_snapshot_cmd();
f21ecbb3 1617 register_trigger_stacktrace_cmd();
7862ad18 1618 register_trigger_enable_disable_cmds();
d0bad49b 1619 register_trigger_hist_enable_disable_cmds();
7ef224d1 1620 register_trigger_hist_cmd();
2a2df321 1621
85f2b082
TZ
1622 return 0;
1623}