1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * trace_events_trigger - trace event triggers
4 *
5 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
6 */
7
8 #include <linux/security.h>
9 #include <linux/module.h>
10 #include <linux/ctype.h>
11 #include <linux/mutex.h>
12 #include <linux/slab.h>
13 #include <linux/rculist.h>
14
15 #include "trace.h"
16
17 static LIST_HEAD(trigger_commands);
18 static DEFINE_MUTEX(trigger_cmd_mutex);
19
trigger_data_free(struct event_trigger_data * data)20 void trigger_data_free(struct event_trigger_data *data)
21 {
22 if (data->cmd_ops->set_filter)
23 data->cmd_ops->set_filter(NULL, data, NULL);
24
25 /* make sure current triggers exit before free */
26 tracepoint_synchronize_unregister();
27
28 kfree(data);
29 }
30
31 /**
32 * event_triggers_call - Call triggers associated with a trace event
33 * @file: The trace_event_file associated with the event
34 * @buffer: The ring buffer that the event is being written to
35 * @rec: The trace entry for the event, NULL for unconditional invocation
36 * @event: The event meta data in the ring buffer
37 *
38 * For each trigger associated with an event, invoke the trigger
39 * function registered with the associated trigger command. If rec is
40 * non-NULL, it means that the trigger requires further processing and
41 * shouldn't be unconditionally invoked. If rec is non-NULL and the
42 * trigger has a filter associated with it, rec will checked against
43 * the filter and if the record matches the trigger will be invoked.
44 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
45 * in any case until the current event is written, the trigger
46 * function isn't invoked but the bit associated with the deferred
47 * trigger is set in the return value.
48 *
49 * Returns an enum event_trigger_type value containing a set bit for
50 * any trigger that should be deferred, ETT_NONE if nothing to defer.
51 *
52 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
53 *
54 * Return: an enum event_trigger_type value containing a set bit for
55 * any trigger that should be deferred, ETT_NONE if nothing to defer.
56 */
57 enum event_trigger_type
event_triggers_call(struct trace_event_file * file,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)58 event_triggers_call(struct trace_event_file *file,
59 struct trace_buffer *buffer, void *rec,
60 struct ring_buffer_event *event)
61 {
62 struct event_trigger_data *data;
63 enum event_trigger_type tt = ETT_NONE;
64 struct event_filter *filter;
65
66 if (list_empty(&file->triggers))
67 return tt;
68
69 list_for_each_entry_rcu(data, &file->triggers, list) {
70 if (data->paused)
71 continue;
72 if (!rec) {
73 data->ops->trigger(data, buffer, rec, event);
74 continue;
75 }
76 filter = rcu_dereference_sched(data->filter);
77 if (filter && !filter_match_preds(filter, rec))
78 continue;
79 if (event_command_post_trigger(data->cmd_ops)) {
80 tt |= data->cmd_ops->trigger_type;
81 continue;
82 }
83 data->ops->trigger(data, buffer, rec, event);
84 }
85 return tt;
86 }
87 EXPORT_SYMBOL_GPL(event_triggers_call);
88
__trace_trigger_soft_disabled(struct trace_event_file * file)89 bool __trace_trigger_soft_disabled(struct trace_event_file *file)
90 {
91 unsigned long eflags = file->flags;
92
93 if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
94 event_triggers_call(file, NULL, NULL, NULL);
95 if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
96 return true;
97 if (eflags & EVENT_FILE_FL_PID_FILTER)
98 return trace_event_ignore_this_pid(file);
99 return false;
100 }
101 EXPORT_SYMBOL_GPL(__trace_trigger_soft_disabled);
102
103 /**
104 * event_triggers_post_call - Call 'post_triggers' for a trace event
105 * @file: The trace_event_file associated with the event
106 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
107 *
108 * For each trigger associated with an event, invoke the trigger
109 * function registered with the associated trigger command, if the
110 * corresponding bit is set in the tt enum passed into this function.
111 * See @event_triggers_call for details on how those bits are set.
112 *
113 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
114 */
115 void
event_triggers_post_call(struct trace_event_file * file,enum event_trigger_type tt)116 event_triggers_post_call(struct trace_event_file *file,
117 enum event_trigger_type tt)
118 {
119 struct event_trigger_data *data;
120
121 list_for_each_entry_rcu(data, &file->triggers, list) {
122 if (data->paused)
123 continue;
124 if (data->cmd_ops->trigger_type & tt)
125 data->ops->trigger(data, NULL, NULL, NULL);
126 }
127 }
128 EXPORT_SYMBOL_GPL(event_triggers_post_call);
129
130 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
131
trigger_next(struct seq_file * m,void * t,loff_t * pos)132 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
133 {
134 struct trace_event_file *event_file = event_file_data(m->private);
135
136 if (t == SHOW_AVAILABLE_TRIGGERS) {
137 (*pos)++;
138 return NULL;
139 }
140 return seq_list_next(t, &event_file->triggers, pos);
141 }
142
check_user_trigger(struct trace_event_file * file)143 static bool check_user_trigger(struct trace_event_file *file)
144 {
145 struct event_trigger_data *data;
146
147 list_for_each_entry_rcu(data, &file->triggers, list,
148 lockdep_is_held(&event_mutex)) {
149 if (data->flags & EVENT_TRIGGER_FL_PROBE)
150 continue;
151 return true;
152 }
153 return false;
154 }
155
trigger_start(struct seq_file * m,loff_t * pos)156 static void *trigger_start(struct seq_file *m, loff_t *pos)
157 {
158 struct trace_event_file *event_file;
159
160 /* ->stop() is called even if ->start() fails */
161 mutex_lock(&event_mutex);
162 event_file = event_file_file(m->private);
163 if (unlikely(!event_file))
164 return ERR_PTR(-ENODEV);
165
166 if (list_empty(&event_file->triggers) || !check_user_trigger(event_file))
167 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
168
169 return seq_list_start(&event_file->triggers, *pos);
170 }
171
trigger_stop(struct seq_file * m,void * t)172 static void trigger_stop(struct seq_file *m, void *t)
173 {
174 mutex_unlock(&event_mutex);
175 }
176
trigger_show(struct seq_file * m,void * v)177 static int trigger_show(struct seq_file *m, void *v)
178 {
179 struct event_trigger_data *data;
180 struct event_command *p;
181
182 if (v == SHOW_AVAILABLE_TRIGGERS) {
183 seq_puts(m, "# Available triggers:\n");
184 seq_putc(m, '#');
185 mutex_lock(&trigger_cmd_mutex);
186 list_for_each_entry_reverse(p, &trigger_commands, list)
187 seq_printf(m, " %s", p->name);
188 seq_putc(m, '\n');
189 mutex_unlock(&trigger_cmd_mutex);
190 return 0;
191 }
192
193 data = list_entry(v, struct event_trigger_data, list);
194 data->ops->print(m, data);
195
196 return 0;
197 }
198
199 static const struct seq_operations event_triggers_seq_ops = {
200 .start = trigger_start,
201 .next = trigger_next,
202 .stop = trigger_stop,
203 .show = trigger_show,
204 };
205
event_trigger_regex_open(struct inode * inode,struct file * file)206 static int event_trigger_regex_open(struct inode *inode, struct file *file)
207 {
208 int ret;
209
210 ret = security_locked_down(LOCKDOWN_TRACEFS);
211 if (ret)
212 return ret;
213
214 guard(mutex)(&event_mutex);
215
216 if (unlikely(!event_file_file(file)))
217 return -ENODEV;
218
219 if ((file->f_mode & FMODE_WRITE) &&
220 (file->f_flags & O_TRUNC)) {
221 struct trace_event_file *event_file;
222 struct event_command *p;
223
224 event_file = event_file_data(file);
225
226 list_for_each_entry(p, &trigger_commands, list) {
227 if (p->unreg_all)
228 p->unreg_all(event_file);
229 }
230 }
231
232 if (file->f_mode & FMODE_READ) {
233 ret = seq_open(file, &event_triggers_seq_ops);
234 if (!ret) {
235 struct seq_file *m = file->private_data;
236 m->private = file;
237 }
238 }
239
240 return ret;
241 }
242
trigger_process_regex(struct trace_event_file * file,char * buff)243 int trigger_process_regex(struct trace_event_file *file, char *buff)
244 {
245 char *command, *next;
246 struct event_command *p;
247
248 next = buff = skip_spaces(buff);
249 command = strsep(&next, ": \t");
250 if (next) {
251 next = skip_spaces(next);
252 if (!*next)
253 next = NULL;
254 }
255 command = (command[0] != '!') ? command : command + 1;
256
257 guard(mutex)(&trigger_cmd_mutex);
258
259 list_for_each_entry(p, &trigger_commands, list) {
260 if (strcmp(p->name, command) == 0)
261 return p->parse(p, file, buff, command, next);
262 }
263
264 return -EINVAL;
265 }
266
event_trigger_regex_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)267 static ssize_t event_trigger_regex_write(struct file *file,
268 const char __user *ubuf,
269 size_t cnt, loff_t *ppos)
270 {
271 struct trace_event_file *event_file;
272 ssize_t ret;
273 char *buf __free(kfree) = NULL;
274
275 if (!cnt)
276 return 0;
277
278 if (cnt >= PAGE_SIZE)
279 return -EINVAL;
280
281 buf = memdup_user_nul(ubuf, cnt);
282 if (IS_ERR(buf))
283 return PTR_ERR(buf);
284
285 strim(buf);
286
287 guard(mutex)(&event_mutex);
288
289 event_file = event_file_file(file);
290 if (unlikely(!event_file))
291 return -ENODEV;
292
293 ret = trigger_process_regex(event_file, buf);
294 if (ret < 0)
295 return ret;
296
297 *ppos += cnt;
298 return cnt;
299 }
300
event_trigger_regex_release(struct inode * inode,struct file * file)301 static int event_trigger_regex_release(struct inode *inode, struct file *file)
302 {
303 mutex_lock(&event_mutex);
304
305 if (file->f_mode & FMODE_READ)
306 seq_release(inode, file);
307
308 mutex_unlock(&event_mutex);
309
310 return 0;
311 }
312
313 static ssize_t
event_trigger_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)314 event_trigger_write(struct file *filp, const char __user *ubuf,
315 size_t cnt, loff_t *ppos)
316 {
317 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
318 }
319
320 static int
event_trigger_open(struct inode * inode,struct file * filp)321 event_trigger_open(struct inode *inode, struct file *filp)
322 {
323 /* Checks for tracefs lockdown */
324 return event_trigger_regex_open(inode, filp);
325 }
326
327 static int
event_trigger_release(struct inode * inode,struct file * file)328 event_trigger_release(struct inode *inode, struct file *file)
329 {
330 return event_trigger_regex_release(inode, file);
331 }
332
333 const struct file_operations event_trigger_fops = {
334 .open = event_trigger_open,
335 .read = seq_read,
336 .write = event_trigger_write,
337 .llseek = tracing_lseek,
338 .release = event_trigger_release,
339 };
340
341 /*
342 * Currently we only register event commands from __init, so mark this
343 * __init too.
344 */
register_event_command(struct event_command * cmd)345 __init int register_event_command(struct event_command *cmd)
346 {
347 struct event_command *p;
348
349 guard(mutex)(&trigger_cmd_mutex);
350
351 list_for_each_entry(p, &trigger_commands, list) {
352 if (strcmp(cmd->name, p->name) == 0)
353 return -EBUSY;
354 }
355 list_add(&cmd->list, &trigger_commands);
356
357 return 0;
358 }
359
360 /*
361 * Currently we only unregister event commands from __init, so mark
362 * this __init too.
363 */
unregister_event_command(struct event_command * cmd)364 __init int unregister_event_command(struct event_command *cmd)
365 {
366 struct event_command *p, *n;
367
368 guard(mutex)(&trigger_cmd_mutex);
369
370 list_for_each_entry_safe(p, n, &trigger_commands, list) {
371 if (strcmp(cmd->name, p->name) == 0) {
372 list_del_init(&p->list);
373 return 0;
374 }
375 }
376
377 return -ENODEV;
378 }
379
380 /**
381 * event_trigger_print - Generic event_trigger_ops @print implementation
382 * @name: The name of the event trigger
383 * @m: The seq_file being printed to
384 * @data: Trigger-specific data
385 * @filter_str: filter_str to print, if present
386 *
387 * Common implementation for event triggers to print themselves.
388 *
389 * Usually wrapped by a function that simply sets the @name of the
390 * trigger command and then invokes this.
391 *
392 * Return: 0 on success, errno otherwise
393 */
394 static int
event_trigger_print(const char * name,struct seq_file * m,void * data,char * filter_str)395 event_trigger_print(const char *name, struct seq_file *m,
396 void *data, char *filter_str)
397 {
398 long count = (long)data;
399
400 seq_puts(m, name);
401
402 if (count == -1)
403 seq_puts(m, ":unlimited");
404 else
405 seq_printf(m, ":count=%ld", count);
406
407 if (filter_str)
408 seq_printf(m, " if %s\n", filter_str);
409 else
410 seq_putc(m, '\n');
411
412 return 0;
413 }
414
415 /**
416 * event_trigger_init - Generic event_trigger_ops @init implementation
417 * @data: Trigger-specific data
418 *
419 * Common implementation of event trigger initialization.
420 *
421 * Usually used directly as the @init method in event trigger
422 * implementations.
423 *
424 * Return: 0 on success, errno otherwise
425 */
event_trigger_init(struct event_trigger_data * data)426 int event_trigger_init(struct event_trigger_data *data)
427 {
428 data->ref++;
429 return 0;
430 }
431
432 /**
433 * event_trigger_free - Generic event_trigger_ops @free implementation
434 * @data: Trigger-specific data
435 *
436 * Common implementation of event trigger de-initialization.
437 *
438 * Usually used directly as the @free method in event trigger
439 * implementations.
440 */
441 static void
event_trigger_free(struct event_trigger_data * data)442 event_trigger_free(struct event_trigger_data *data)
443 {
444 if (WARN_ON_ONCE(data->ref <= 0))
445 return;
446
447 data->ref--;
448 if (!data->ref)
449 trigger_data_free(data);
450 }
451
trace_event_trigger_enable_disable(struct trace_event_file * file,int trigger_enable)452 int trace_event_trigger_enable_disable(struct trace_event_file *file,
453 int trigger_enable)
454 {
455 int ret = 0;
456
457 if (trigger_enable) {
458 if (atomic_inc_return(&file->tm_ref) > 1)
459 return ret;
460 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
461 ret = trace_event_enable_disable(file, 1, 1);
462 } else {
463 if (atomic_dec_return(&file->tm_ref) > 0)
464 return ret;
465 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
466 ret = trace_event_enable_disable(file, 0, 1);
467 }
468
469 return ret;
470 }
471
472 /**
473 * clear_event_triggers - Clear all triggers associated with a trace array
474 * @tr: The trace array to clear
475 *
476 * For each trigger, the triggering event has its tm_ref decremented
477 * via trace_event_trigger_enable_disable(), and any associated event
478 * (in the case of enable/disable_event triggers) will have its sm_ref
479 * decremented via free()->trace_event_enable_disable(). That
480 * combination effectively reverses the soft-mode/trigger state added
481 * by trigger registration.
482 *
483 * Must be called with event_mutex held.
484 */
485 void
clear_event_triggers(struct trace_array * tr)486 clear_event_triggers(struct trace_array *tr)
487 {
488 struct trace_event_file *file;
489
490 list_for_each_entry(file, &tr->events, list) {
491 struct event_trigger_data *data, *n;
492 list_for_each_entry_safe(data, n, &file->triggers, list) {
493 trace_event_trigger_enable_disable(file, 0);
494 list_del_rcu(&data->list);
495 if (data->ops->free)
496 data->ops->free(data);
497 }
498 }
499 }
500
501 /**
502 * update_cond_flag - Set or reset the TRIGGER_COND bit
503 * @file: The trace_event_file associated with the event
504 *
505 * If an event has triggers and any of those triggers has a filter or
506 * a post_trigger, trigger invocation needs to be deferred until after
507 * the current event has logged its data, and the event should have
508 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
509 * cleared.
510 */
update_cond_flag(struct trace_event_file * file)511 void update_cond_flag(struct trace_event_file *file)
512 {
513 struct event_trigger_data *data;
514 bool set_cond = false;
515
516 lockdep_assert_held(&event_mutex);
517
518 list_for_each_entry(data, &file->triggers, list) {
519 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
520 event_command_needs_rec(data->cmd_ops)) {
521 set_cond = true;
522 break;
523 }
524 }
525
526 if (set_cond)
527 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
528 else
529 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
530 }
531
532 /**
533 * register_trigger - Generic event_command @reg implementation
534 * @glob: The raw string used to register the trigger
535 * @data: Trigger-specific data to associate with the trigger
536 * @file: The trace_event_file associated with the event
537 *
538 * Common implementation for event trigger registration.
539 *
540 * Usually used directly as the @reg method in event command
541 * implementations.
542 *
543 * Return: 0 on success, errno otherwise
544 */
register_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)545 static int register_trigger(char *glob,
546 struct event_trigger_data *data,
547 struct trace_event_file *file)
548 {
549 struct event_trigger_data *test;
550 int ret = 0;
551
552 lockdep_assert_held(&event_mutex);
553
554 list_for_each_entry(test, &file->triggers, list) {
555 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
556 ret = -EEXIST;
557 goto out;
558 }
559 }
560
561 if (data->ops->init) {
562 ret = data->ops->init(data);
563 if (ret < 0)
564 goto out;
565 }
566
567 list_add_rcu(&data->list, &file->triggers);
568
569 update_cond_flag(file);
570 ret = trace_event_trigger_enable_disable(file, 1);
571 if (ret < 0) {
572 list_del_rcu(&data->list);
573 update_cond_flag(file);
574 }
575 out:
576 return ret;
577 }
578
579 /*
580 * True if the trigger was found and unregistered, else false.
581 */
try_unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)582 static bool try_unregister_trigger(char *glob,
583 struct event_trigger_data *test,
584 struct trace_event_file *file)
585 {
586 struct event_trigger_data *data = NULL, *iter;
587
588 lockdep_assert_held(&event_mutex);
589
590 list_for_each_entry(iter, &file->triggers, list) {
591 if (iter->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
592 data = iter;
593 list_del_rcu(&data->list);
594 trace_event_trigger_enable_disable(file, 0);
595 update_cond_flag(file);
596 break;
597 }
598 }
599
600 if (data) {
601 if (data->ops->free)
602 data->ops->free(data);
603
604 return true;
605 }
606
607 return false;
608 }
609
610 /**
611 * unregister_trigger - Generic event_command @unreg implementation
612 * @glob: The raw string used to register the trigger
613 * @test: Trigger-specific data used to find the trigger to remove
614 * @file: The trace_event_file associated with the event
615 *
616 * Common implementation for event trigger unregistration.
617 *
618 * Usually used directly as the @unreg method in event command
619 * implementations.
620 */
unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)621 static void unregister_trigger(char *glob,
622 struct event_trigger_data *test,
623 struct trace_event_file *file)
624 {
625 try_unregister_trigger(glob, test, file);
626 }
627
628 /*
629 * Event trigger parsing helper functions.
630 *
631 * These functions help make it easier to write an event trigger
632 * parsing function i.e. the struct event_command.parse() callback
633 * function responsible for parsing and registering a trigger command
634 * written to the 'trigger' file.
635 *
636 * A trigger command (or just 'trigger' for short) takes the form:
637 * [trigger] [if filter]
638 *
639 * The struct event_command.parse() callback (and other struct
640 * event_command functions) refer to several components of a trigger
641 * command. Those same components are referenced by the event trigger
642 * parsing helper functions defined below. These components are:
643 *
644 * cmd - the trigger command name
645 * glob - the trigger command name optionally prefaced with '!'
646 * param_and_filter - text following cmd and ':'
647 * param - text following cmd and ':' and stripped of filter
648 * filter - the optional filter text following (and including) 'if'
649 *
650 * To illustrate the use of these componenents, here are some concrete
651 * examples. For the following triggers:
652 *
653 * echo 'traceon:5 if pid == 0' > trigger
654 * - 'traceon' is both cmd and glob
655 * - '5 if pid == 0' is the param_and_filter
656 * - '5' is the param
657 * - 'if pid == 0' is the filter
658 *
659 * echo 'enable_event:sys:event:n' > trigger
660 * - 'enable_event' is both cmd and glob
661 * - 'sys:event:n' is the param_and_filter
662 * - 'sys:event:n' is the param
663 * - there is no filter
664 *
665 * echo 'hist:keys=pid if prio > 50' > trigger
666 * - 'hist' is both cmd and glob
667 * - 'keys=pid if prio > 50' is the param_and_filter
668 * - 'keys=pid' is the param
669 * - 'if prio > 50' is the filter
670 *
671 * echo '!enable_event:sys:event:n' > trigger
672 * - 'enable_event' the cmd
673 * - '!enable_event' is the glob
674 * - 'sys:event:n' is the param_and_filter
675 * - 'sys:event:n' is the param
676 * - there is no filter
677 *
678 * echo 'traceoff' > trigger
679 * - 'traceoff' is both cmd and glob
680 * - there is no param_and_filter
681 * - there is no param
682 * - there is no filter
683 *
684 * There are a few different categories of event trigger covered by
685 * these helpers:
686 *
687 * - triggers that don't require a parameter e.g. traceon
688 * - triggers that do require a parameter e.g. enable_event and hist
689 * - triggers that though they may not require a param may support an
690 * optional 'n' param (n = number of times the trigger should fire)
691 * e.g.: traceon:5 or enable_event:sys:event:n
692 * - triggers that do not support an 'n' param e.g. hist
693 *
694 * These functions can be used or ignored as necessary - it all
695 * depends on the complexity of the trigger, and the granularity of
696 * the functions supported reflects the fact that some implementations
697 * may need to customize certain aspects of their implementations and
698 * won't need certain functions. For instance, the hist trigger
699 * implementation doesn't use event_trigger_separate_filter() because
700 * it has special requirements for handling the filter.
701 */
702
703 /**
704 * event_trigger_check_remove - check whether an event trigger specifies remove
705 * @glob: The trigger command string, with optional remove(!) operator
706 *
707 * The event trigger callback implementations pass in 'glob' as a
708 * parameter. This is the command name either with or without a
709 * remove(!) operator. This function simply parses the glob and
710 * determines whether the command corresponds to a trigger removal or
711 * a trigger addition.
712 *
713 * Return: true if this is a remove command, false otherwise
714 */
event_trigger_check_remove(const char * glob)715 bool event_trigger_check_remove(const char *glob)
716 {
717 return (glob && glob[0] == '!') ? true : false;
718 }
719
720 /**
721 * event_trigger_empty_param - check whether the param is empty
722 * @param: The trigger param string
723 *
724 * The event trigger callback implementations pass in 'param' as a
725 * parameter. This corresponds to the string following the command
726 * name minus the command name. This function can be called by a
727 * callback implementation for any command that requires a param; a
728 * callback that doesn't require a param can ignore it.
729 *
730 * Return: true if this is an empty param, false otherwise
731 */
event_trigger_empty_param(const char * param)732 bool event_trigger_empty_param(const char *param)
733 {
734 return !param;
735 }
736
737 /**
738 * event_trigger_separate_filter - separate an event trigger from a filter
739 * @param_and_filter: String containing trigger and possibly filter
740 * @param: outparam, will be filled with a pointer to the trigger
741 * @filter: outparam, will be filled with a pointer to the filter
742 * @param_required: Specifies whether or not the param string is required
743 *
744 * Given a param string of the form '[trigger] [if filter]', this
745 * function separates the filter from the trigger and returns the
746 * trigger in @param and the filter in @filter. Either the @param
747 * or the @filter may be set to NULL by this function - if not set to
748 * NULL, they will contain strings corresponding to the trigger and
749 * filter.
750 *
751 * There are two cases that need to be handled with respect to the
752 * passed-in param: either the param is required, or it is not
753 * required. If @param_required is set, and there's no param, it will
754 * return -EINVAL. If @param_required is not set and there's a param
755 * that starts with a number, that corresponds to the case of a
756 * trigger with :n (n = number of times the trigger should fire) and
757 * the parsing continues normally; otherwise the function just returns
758 * and assumes param just contains a filter and there's nothing else
759 * to do.
760 *
761 * Return: 0 on success, errno otherwise
762 */
event_trigger_separate_filter(char * param_and_filter,char ** param,char ** filter,bool param_required)763 int event_trigger_separate_filter(char *param_and_filter, char **param,
764 char **filter, bool param_required)
765 {
766 int ret = 0;
767
768 *param = *filter = NULL;
769
770 if (!param_and_filter) {
771 if (param_required)
772 ret = -EINVAL;
773 goto out;
774 }
775
776 /*
777 * Here we check for an optional param. The only legal
778 * optional param is :n, and if that's the case, continue
779 * below. Otherwise we assume what's left is a filter and
780 * return it as the filter string for the caller to deal with.
781 */
782 if (!param_required && param_and_filter && !isdigit(param_and_filter[0])) {
783 *filter = param_and_filter;
784 goto out;
785 }
786
787 /*
788 * Separate the param from the filter (param [if filter]).
789 * Here we have either an optional :n param or a required
790 * param and an optional filter.
791 */
792 *param = strsep(¶m_and_filter, " \t");
793
794 /*
795 * Here we have a filter, though it may be empty.
796 */
797 if (param_and_filter) {
798 *filter = skip_spaces(param_and_filter);
799 if (!**filter)
800 *filter = NULL;
801 }
802 out:
803 return ret;
804 }
805
806 /**
807 * event_trigger_alloc - allocate and init event_trigger_data for a trigger
808 * @cmd_ops: The event_command operations for the trigger
809 * @cmd: The cmd string
810 * @param: The param string
811 * @private_data: User data to associate with the event trigger
812 *
813 * Allocate an event_trigger_data instance and initialize it. The
814 * @cmd_ops are used along with the @cmd and @param to get the
815 * trigger_ops to assign to the event_trigger_data. @private_data can
816 * also be passed in and associated with the event_trigger_data.
817 *
818 * Use event_trigger_free() to free an event_trigger_data object.
819 *
820 * Return: The trigger_data object success, NULL otherwise
821 */
event_trigger_alloc(struct event_command * cmd_ops,char * cmd,char * param,void * private_data)822 struct event_trigger_data *event_trigger_alloc(struct event_command *cmd_ops,
823 char *cmd,
824 char *param,
825 void *private_data)
826 {
827 struct event_trigger_data *trigger_data;
828 struct event_trigger_ops *trigger_ops;
829
830 trigger_ops = cmd_ops->get_trigger_ops(cmd, param);
831
832 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
833 if (!trigger_data)
834 return NULL;
835
836 trigger_data->count = -1;
837 trigger_data->ops = trigger_ops;
838 trigger_data->cmd_ops = cmd_ops;
839 trigger_data->private_data = private_data;
840
841 INIT_LIST_HEAD(&trigger_data->list);
842 INIT_LIST_HEAD(&trigger_data->named_list);
843 RCU_INIT_POINTER(trigger_data->filter, NULL);
844
845 return trigger_data;
846 }
847
848 /**
849 * event_trigger_parse_num - parse and return the number param for a trigger
850 * @param: The param string
851 * @trigger_data: The trigger_data for the trigger
852 *
853 * Parse the :n (n = number of times the trigger should fire) param
854 * and set the count variable in the trigger_data to the parsed count.
855 *
856 * Return: 0 on success, errno otherwise
857 */
event_trigger_parse_num(char * param,struct event_trigger_data * trigger_data)858 int event_trigger_parse_num(char *param,
859 struct event_trigger_data *trigger_data)
860 {
861 char *number;
862 int ret = 0;
863
864 if (param) {
865 number = strsep(¶m, ":");
866
867 if (!strlen(number))
868 return -EINVAL;
869
870 /*
871 * We use the callback data field (which is a pointer)
872 * as our counter.
873 */
874 ret = kstrtoul(number, 0, &trigger_data->count);
875 }
876
877 return ret;
878 }
879
880 /**
881 * event_trigger_set_filter - set an event trigger's filter
882 * @cmd_ops: The event_command operations for the trigger
883 * @file: The event file for the trigger's event
884 * @param: The string containing the filter
885 * @trigger_data: The trigger_data for the trigger
886 *
887 * Set the filter for the trigger. If the filter is NULL, just return
888 * without error.
889 *
890 * Return: 0 on success, errno otherwise
891 */
event_trigger_set_filter(struct event_command * cmd_ops,struct trace_event_file * file,char * param,struct event_trigger_data * trigger_data)892 int event_trigger_set_filter(struct event_command *cmd_ops,
893 struct trace_event_file *file,
894 char *param,
895 struct event_trigger_data *trigger_data)
896 {
897 if (param && cmd_ops->set_filter)
898 return cmd_ops->set_filter(param, trigger_data, file);
899
900 return 0;
901 }
902
903 /**
904 * event_trigger_reset_filter - reset an event trigger's filter
905 * @cmd_ops: The event_command operations for the trigger
906 * @trigger_data: The trigger_data for the trigger
907 *
908 * Reset the filter for the trigger to no filter.
909 */
event_trigger_reset_filter(struct event_command * cmd_ops,struct event_trigger_data * trigger_data)910 void event_trigger_reset_filter(struct event_command *cmd_ops,
911 struct event_trigger_data *trigger_data)
912 {
913 if (cmd_ops->set_filter)
914 cmd_ops->set_filter(NULL, trigger_data, NULL);
915 }
916
917 /**
918 * event_trigger_register - register an event trigger
919 * @cmd_ops: The event_command operations for the trigger
920 * @file: The event file for the trigger's event
921 * @glob: The trigger command string, with optional remove(!) operator
922 * @trigger_data: The trigger_data for the trigger
923 *
924 * Register an event trigger. The @cmd_ops are used to call the
925 * cmd_ops->reg() function which actually does the registration.
926 *
927 * Return: 0 on success, errno otherwise
928 */
event_trigger_register(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,struct event_trigger_data * trigger_data)929 int event_trigger_register(struct event_command *cmd_ops,
930 struct trace_event_file *file,
931 char *glob,
932 struct event_trigger_data *trigger_data)
933 {
934 return cmd_ops->reg(glob, trigger_data, file);
935 }
936
937 /**
938 * event_trigger_unregister - unregister an event trigger
939 * @cmd_ops: The event_command operations for the trigger
940 * @file: The event file for the trigger's event
941 * @glob: The trigger command string, with optional remove(!) operator
942 * @trigger_data: The trigger_data for the trigger
943 *
944 * Unregister an event trigger. The @cmd_ops are used to call the
945 * cmd_ops->unreg() function which actually does the unregistration.
946 */
event_trigger_unregister(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,struct event_trigger_data * trigger_data)947 void event_trigger_unregister(struct event_command *cmd_ops,
948 struct trace_event_file *file,
949 char *glob,
950 struct event_trigger_data *trigger_data)
951 {
952 cmd_ops->unreg(glob, trigger_data, file);
953 }
954
955 /*
956 * End event trigger parsing helper functions.
957 */
958
959 /**
960 * event_trigger_parse - Generic event_command @parse implementation
961 * @cmd_ops: The command ops, used for trigger registration
962 * @file: The trace_event_file associated with the event
963 * @glob: The raw string used to register the trigger
964 * @cmd: The cmd portion of the string used to register the trigger
965 * @param_and_filter: The param and filter portion of the string used to register the trigger
966 *
967 * Common implementation for event command parsing and trigger
968 * instantiation.
969 *
970 * Usually used directly as the @parse method in event command
971 * implementations.
972 *
973 * Return: 0 on success, errno otherwise
974 */
975 static int
event_trigger_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)976 event_trigger_parse(struct event_command *cmd_ops,
977 struct trace_event_file *file,
978 char *glob, char *cmd, char *param_and_filter)
979 {
980 struct event_trigger_data *trigger_data;
981 char *param, *filter;
982 bool remove;
983 int ret;
984
985 remove = event_trigger_check_remove(glob);
986
987 ret = event_trigger_separate_filter(param_and_filter, ¶m, &filter, false);
988 if (ret)
989 return ret;
990
991 ret = -ENOMEM;
992 trigger_data = event_trigger_alloc(cmd_ops, cmd, param, file);
993 if (!trigger_data)
994 goto out;
995
996 if (remove) {
997 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
998 kfree(trigger_data);
999 ret = 0;
1000 goto out;
1001 }
1002
1003 ret = event_trigger_parse_num(param, trigger_data);
1004 if (ret)
1005 goto out_free;
1006
1007 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
1008 if (ret < 0)
1009 goto out_free;
1010
1011 /* Up the trigger_data count to make sure reg doesn't free it on failure */
1012 event_trigger_init(trigger_data);
1013
1014 ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1015 if (ret)
1016 goto out_free;
1017
1018 /* Down the counter of trigger_data or free it if not used anymore */
1019 event_trigger_free(trigger_data);
1020 out:
1021 return ret;
1022
1023 out_free:
1024 event_trigger_reset_filter(cmd_ops, trigger_data);
1025 kfree(trigger_data);
1026 goto out;
1027 }
1028
1029 /**
1030 * set_trigger_filter - Generic event_command @set_filter implementation
1031 * @filter_str: The filter string for the trigger, NULL to remove filter
1032 * @trigger_data: Trigger-specific data
1033 * @file: The trace_event_file associated with the event
1034 *
1035 * Common implementation for event command filter parsing and filter
1036 * instantiation.
1037 *
1038 * Usually used directly as the @set_filter method in event command
1039 * implementations.
1040 *
1041 * Also used to remove a filter (if filter_str = NULL).
1042 *
1043 * Return: 0 on success, errno otherwise
1044 */
set_trigger_filter(char * filter_str,struct event_trigger_data * trigger_data,struct trace_event_file * file)1045 int set_trigger_filter(char *filter_str,
1046 struct event_trigger_data *trigger_data,
1047 struct trace_event_file *file)
1048 {
1049 struct event_trigger_data *data = trigger_data;
1050 struct event_filter *filter = NULL, *tmp;
1051 int ret = -EINVAL;
1052 char *s;
1053
1054 if (!filter_str) /* clear the current filter */
1055 goto assign;
1056
1057 s = strsep(&filter_str, " \t");
1058
1059 if (!strlen(s) || strcmp(s, "if") != 0)
1060 goto out;
1061
1062 if (!filter_str)
1063 goto out;
1064
1065 /* The filter is for the 'trigger' event, not the triggered event */
1066 ret = create_event_filter(file->tr, file->event_call,
1067 filter_str, true, &filter);
1068
1069 /* Only enabled set_str for error handling */
1070 if (filter) {
1071 kfree(filter->filter_string);
1072 filter->filter_string = NULL;
1073 }
1074
1075 /*
1076 * If create_event_filter() fails, filter still needs to be freed.
1077 * Which the calling code will do with data->filter.
1078 */
1079 assign:
1080 tmp = rcu_access_pointer(data->filter);
1081
1082 rcu_assign_pointer(data->filter, filter);
1083
1084 if (tmp) {
1085 /*
1086 * Make sure the call is done with the filter.
1087 * It is possible that a filter could fail at boot up,
1088 * and then this path will be called. Avoid the synchronization
1089 * in that case.
1090 */
1091 if (system_state != SYSTEM_BOOTING)
1092 tracepoint_synchronize_unregister();
1093 free_event_filter(tmp);
1094 }
1095
1096 kfree(data->filter_str);
1097 data->filter_str = NULL;
1098
1099 if (filter_str) {
1100 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
1101 if (!data->filter_str) {
1102 free_event_filter(rcu_access_pointer(data->filter));
1103 data->filter = NULL;
1104 ret = -ENOMEM;
1105 }
1106 }
1107 out:
1108 return ret;
1109 }
1110
1111 static LIST_HEAD(named_triggers);
1112
1113 /**
1114 * find_named_trigger - Find the common named trigger associated with @name
1115 * @name: The name of the set of named triggers to find the common data for
1116 *
1117 * Named triggers are sets of triggers that share a common set of
1118 * trigger data. The first named trigger registered with a given name
1119 * owns the common trigger data that the others subsequently
1120 * registered with the same name will reference. This function
1121 * returns the common trigger data associated with that first
1122 * registered instance.
1123 *
1124 * Return: the common trigger data for the given named trigger on
1125 * success, NULL otherwise.
1126 */
find_named_trigger(const char * name)1127 struct event_trigger_data *find_named_trigger(const char *name)
1128 {
1129 struct event_trigger_data *data;
1130
1131 if (!name)
1132 return NULL;
1133
1134 list_for_each_entry(data, &named_triggers, named_list) {
1135 if (data->named_data)
1136 continue;
1137 if (strcmp(data->name, name) == 0)
1138 return data;
1139 }
1140
1141 return NULL;
1142 }
1143
1144 /**
1145 * is_named_trigger - determine if a given trigger is a named trigger
1146 * @test: The trigger data to test
1147 *
1148 * Return: true if 'test' is a named trigger, false otherwise.
1149 */
is_named_trigger(struct event_trigger_data * test)1150 bool is_named_trigger(struct event_trigger_data *test)
1151 {
1152 struct event_trigger_data *data;
1153
1154 list_for_each_entry(data, &named_triggers, named_list) {
1155 if (test == data)
1156 return true;
1157 }
1158
1159 return false;
1160 }
1161
1162 /**
1163 * save_named_trigger - save the trigger in the named trigger list
1164 * @name: The name of the named trigger set
1165 * @data: The trigger data to save
1166 *
1167 * Return: 0 if successful, negative error otherwise.
1168 */
save_named_trigger(const char * name,struct event_trigger_data * data)1169 int save_named_trigger(const char *name, struct event_trigger_data *data)
1170 {
1171 data->name = kstrdup(name, GFP_KERNEL);
1172 if (!data->name)
1173 return -ENOMEM;
1174
1175 list_add(&data->named_list, &named_triggers);
1176
1177 return 0;
1178 }
1179
1180 /**
1181 * del_named_trigger - delete a trigger from the named trigger list
1182 * @data: The trigger data to delete
1183 */
del_named_trigger(struct event_trigger_data * data)1184 void del_named_trigger(struct event_trigger_data *data)
1185 {
1186 kfree(data->name);
1187 data->name = NULL;
1188
1189 list_del(&data->named_list);
1190 }
1191
__pause_named_trigger(struct event_trigger_data * data,bool pause)1192 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
1193 {
1194 struct event_trigger_data *test;
1195
1196 list_for_each_entry(test, &named_triggers, named_list) {
1197 if (strcmp(test->name, data->name) == 0) {
1198 if (pause) {
1199 test->paused_tmp = test->paused;
1200 test->paused = true;
1201 } else {
1202 test->paused = test->paused_tmp;
1203 }
1204 }
1205 }
1206 }
1207
1208 /**
1209 * pause_named_trigger - Pause all named triggers with the same name
1210 * @data: The trigger data of a named trigger to pause
1211 *
1212 * Pauses a named trigger along with all other triggers having the
1213 * same name. Because named triggers share a common set of data,
1214 * pausing only one is meaningless, so pausing one named trigger needs
1215 * to pause all triggers with the same name.
1216 */
pause_named_trigger(struct event_trigger_data * data)1217 void pause_named_trigger(struct event_trigger_data *data)
1218 {
1219 __pause_named_trigger(data, true);
1220 }
1221
1222 /**
1223 * unpause_named_trigger - Un-pause all named triggers with the same name
1224 * @data: The trigger data of a named trigger to unpause
1225 *
1226 * Un-pauses a named trigger along with all other triggers having the
1227 * same name. Because named triggers share a common set of data,
1228 * unpausing only one is meaningless, so unpausing one named trigger
1229 * needs to unpause all triggers with the same name.
1230 */
unpause_named_trigger(struct event_trigger_data * data)1231 void unpause_named_trigger(struct event_trigger_data *data)
1232 {
1233 __pause_named_trigger(data, false);
1234 }
1235
1236 /**
1237 * set_named_trigger_data - Associate common named trigger data
1238 * @data: The trigger data to associate
1239 * @named_data: The common named trigger to be associated
1240 *
1241 * Named triggers are sets of triggers that share a common set of
1242 * trigger data. The first named trigger registered with a given name
1243 * owns the common trigger data that the others subsequently
1244 * registered with the same name will reference. This function
1245 * associates the common trigger data from the first trigger with the
1246 * given trigger.
1247 */
set_named_trigger_data(struct event_trigger_data * data,struct event_trigger_data * named_data)1248 void set_named_trigger_data(struct event_trigger_data *data,
1249 struct event_trigger_data *named_data)
1250 {
1251 data->named_data = named_data;
1252 }
1253
1254 struct event_trigger_data *
get_named_trigger_data(struct event_trigger_data * data)1255 get_named_trigger_data(struct event_trigger_data *data)
1256 {
1257 return data->named_data;
1258 }
1259
1260 static void
traceon_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1261 traceon_trigger(struct event_trigger_data *data,
1262 struct trace_buffer *buffer, void *rec,
1263 struct ring_buffer_event *event)
1264 {
1265 struct trace_event_file *file = data->private_data;
1266
1267 if (file) {
1268 if (tracer_tracing_is_on(file->tr))
1269 return;
1270
1271 tracer_tracing_on(file->tr);
1272 return;
1273 }
1274
1275 if (tracing_is_on())
1276 return;
1277
1278 tracing_on();
1279 }
1280
1281 static void
traceon_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1282 traceon_count_trigger(struct event_trigger_data *data,
1283 struct trace_buffer *buffer, void *rec,
1284 struct ring_buffer_event *event)
1285 {
1286 struct trace_event_file *file = data->private_data;
1287
1288 if (file) {
1289 if (tracer_tracing_is_on(file->tr))
1290 return;
1291 } else {
1292 if (tracing_is_on())
1293 return;
1294 }
1295
1296 if (!data->count)
1297 return;
1298
1299 if (data->count != -1)
1300 (data->count)--;
1301
1302 if (file)
1303 tracer_tracing_on(file->tr);
1304 else
1305 tracing_on();
1306 }
1307
1308 static void
traceoff_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1309 traceoff_trigger(struct event_trigger_data *data,
1310 struct trace_buffer *buffer, void *rec,
1311 struct ring_buffer_event *event)
1312 {
1313 struct trace_event_file *file = data->private_data;
1314
1315 if (file) {
1316 if (!tracer_tracing_is_on(file->tr))
1317 return;
1318
1319 tracer_tracing_off(file->tr);
1320 return;
1321 }
1322
1323 if (!tracing_is_on())
1324 return;
1325
1326 tracing_off();
1327 }
1328
1329 static void
traceoff_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1330 traceoff_count_trigger(struct event_trigger_data *data,
1331 struct trace_buffer *buffer, void *rec,
1332 struct ring_buffer_event *event)
1333 {
1334 struct trace_event_file *file = data->private_data;
1335
1336 if (file) {
1337 if (!tracer_tracing_is_on(file->tr))
1338 return;
1339 } else {
1340 if (!tracing_is_on())
1341 return;
1342 }
1343
1344 if (!data->count)
1345 return;
1346
1347 if (data->count != -1)
1348 (data->count)--;
1349
1350 if (file)
1351 tracer_tracing_off(file->tr);
1352 else
1353 tracing_off();
1354 }
1355
1356 static int
traceon_trigger_print(struct seq_file * m,struct event_trigger_data * data)1357 traceon_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1358 {
1359 return event_trigger_print("traceon", m, (void *)data->count,
1360 data->filter_str);
1361 }
1362
1363 static int
traceoff_trigger_print(struct seq_file * m,struct event_trigger_data * data)1364 traceoff_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1365 {
1366 return event_trigger_print("traceoff", m, (void *)data->count,
1367 data->filter_str);
1368 }
1369
1370 static struct event_trigger_ops traceon_trigger_ops = {
1371 .trigger = traceon_trigger,
1372 .print = traceon_trigger_print,
1373 .init = event_trigger_init,
1374 .free = event_trigger_free,
1375 };
1376
1377 static struct event_trigger_ops traceon_count_trigger_ops = {
1378 .trigger = traceon_count_trigger,
1379 .print = traceon_trigger_print,
1380 .init = event_trigger_init,
1381 .free = event_trigger_free,
1382 };
1383
1384 static struct event_trigger_ops traceoff_trigger_ops = {
1385 .trigger = traceoff_trigger,
1386 .print = traceoff_trigger_print,
1387 .init = event_trigger_init,
1388 .free = event_trigger_free,
1389 };
1390
1391 static struct event_trigger_ops traceoff_count_trigger_ops = {
1392 .trigger = traceoff_count_trigger,
1393 .print = traceoff_trigger_print,
1394 .init = event_trigger_init,
1395 .free = event_trigger_free,
1396 };
1397
1398 static struct event_trigger_ops *
onoff_get_trigger_ops(char * cmd,char * param)1399 onoff_get_trigger_ops(char *cmd, char *param)
1400 {
1401 struct event_trigger_ops *ops;
1402
1403 /* we register both traceon and traceoff to this callback */
1404 if (strcmp(cmd, "traceon") == 0)
1405 ops = param ? &traceon_count_trigger_ops :
1406 &traceon_trigger_ops;
1407 else
1408 ops = param ? &traceoff_count_trigger_ops :
1409 &traceoff_trigger_ops;
1410
1411 return ops;
1412 }
1413
1414 static struct event_command trigger_traceon_cmd = {
1415 .name = "traceon",
1416 .trigger_type = ETT_TRACE_ONOFF,
1417 .parse = event_trigger_parse,
1418 .reg = register_trigger,
1419 .unreg = unregister_trigger,
1420 .get_trigger_ops = onoff_get_trigger_ops,
1421 .set_filter = set_trigger_filter,
1422 };
1423
1424 static struct event_command trigger_traceoff_cmd = {
1425 .name = "traceoff",
1426 .trigger_type = ETT_TRACE_ONOFF,
1427 .flags = EVENT_CMD_FL_POST_TRIGGER,
1428 .parse = event_trigger_parse,
1429 .reg = register_trigger,
1430 .unreg = unregister_trigger,
1431 .get_trigger_ops = onoff_get_trigger_ops,
1432 .set_filter = set_trigger_filter,
1433 };
1434
1435 #ifdef CONFIG_TRACER_SNAPSHOT
1436 static void
snapshot_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1437 snapshot_trigger(struct event_trigger_data *data,
1438 struct trace_buffer *buffer, void *rec,
1439 struct ring_buffer_event *event)
1440 {
1441 struct trace_event_file *file = data->private_data;
1442
1443 if (file)
1444 tracing_snapshot_instance(file->tr);
1445 else
1446 tracing_snapshot();
1447 }
1448
1449 static void
snapshot_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1450 snapshot_count_trigger(struct event_trigger_data *data,
1451 struct trace_buffer *buffer, void *rec,
1452 struct ring_buffer_event *event)
1453 {
1454 if (!data->count)
1455 return;
1456
1457 if (data->count != -1)
1458 (data->count)--;
1459
1460 snapshot_trigger(data, buffer, rec, event);
1461 }
1462
1463 static int
register_snapshot_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)1464 register_snapshot_trigger(char *glob,
1465 struct event_trigger_data *data,
1466 struct trace_event_file *file)
1467 {
1468 int ret = tracing_arm_snapshot(file->tr);
1469
1470 if (ret < 0)
1471 return ret;
1472
1473 ret = register_trigger(glob, data, file);
1474 if (ret < 0)
1475 tracing_disarm_snapshot(file->tr);
1476 return ret;
1477 }
1478
unregister_snapshot_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)1479 static void unregister_snapshot_trigger(char *glob,
1480 struct event_trigger_data *data,
1481 struct trace_event_file *file)
1482 {
1483 if (try_unregister_trigger(glob, data, file))
1484 tracing_disarm_snapshot(file->tr);
1485 }
1486
1487 static int
snapshot_trigger_print(struct seq_file * m,struct event_trigger_data * data)1488 snapshot_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1489 {
1490 return event_trigger_print("snapshot", m, (void *)data->count,
1491 data->filter_str);
1492 }
1493
1494 static struct event_trigger_ops snapshot_trigger_ops = {
1495 .trigger = snapshot_trigger,
1496 .print = snapshot_trigger_print,
1497 .init = event_trigger_init,
1498 .free = event_trigger_free,
1499 };
1500
1501 static struct event_trigger_ops snapshot_count_trigger_ops = {
1502 .trigger = snapshot_count_trigger,
1503 .print = snapshot_trigger_print,
1504 .init = event_trigger_init,
1505 .free = event_trigger_free,
1506 };
1507
1508 static struct event_trigger_ops *
snapshot_get_trigger_ops(char * cmd,char * param)1509 snapshot_get_trigger_ops(char *cmd, char *param)
1510 {
1511 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1512 }
1513
1514 static struct event_command trigger_snapshot_cmd = {
1515 .name = "snapshot",
1516 .trigger_type = ETT_SNAPSHOT,
1517 .parse = event_trigger_parse,
1518 .reg = register_snapshot_trigger,
1519 .unreg = unregister_snapshot_trigger,
1520 .get_trigger_ops = snapshot_get_trigger_ops,
1521 .set_filter = set_trigger_filter,
1522 };
1523
register_trigger_snapshot_cmd(void)1524 static __init int register_trigger_snapshot_cmd(void)
1525 {
1526 int ret;
1527
1528 ret = register_event_command(&trigger_snapshot_cmd);
1529 WARN_ON(ret < 0);
1530
1531 return ret;
1532 }
1533 #else
register_trigger_snapshot_cmd(void)1534 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1535 #endif /* CONFIG_TRACER_SNAPSHOT */
1536
1537 #ifdef CONFIG_STACKTRACE
1538 #ifdef CONFIG_UNWINDER_ORC
1539 /* Skip 2:
1540 * event_triggers_post_call()
1541 * trace_event_raw_event_xxx()
1542 */
1543 # define STACK_SKIP 2
1544 #else
1545 /*
1546 * Skip 4:
1547 * stacktrace_trigger()
1548 * event_triggers_post_call()
1549 * trace_event_buffer_commit()
1550 * trace_event_raw_event_xxx()
1551 */
1552 #define STACK_SKIP 4
1553 #endif
1554
1555 static void
stacktrace_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1556 stacktrace_trigger(struct event_trigger_data *data,
1557 struct trace_buffer *buffer, void *rec,
1558 struct ring_buffer_event *event)
1559 {
1560 struct trace_event_file *file = data->private_data;
1561
1562 if (file)
1563 __trace_stack(file->tr, tracing_gen_ctx(), STACK_SKIP);
1564 else
1565 trace_dump_stack(STACK_SKIP);
1566 }
1567
1568 static void
stacktrace_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1569 stacktrace_count_trigger(struct event_trigger_data *data,
1570 struct trace_buffer *buffer, void *rec,
1571 struct ring_buffer_event *event)
1572 {
1573 if (!data->count)
1574 return;
1575
1576 if (data->count != -1)
1577 (data->count)--;
1578
1579 stacktrace_trigger(data, buffer, rec, event);
1580 }
1581
1582 static int
stacktrace_trigger_print(struct seq_file * m,struct event_trigger_data * data)1583 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_data *data)
1584 {
1585 return event_trigger_print("stacktrace", m, (void *)data->count,
1586 data->filter_str);
1587 }
1588
1589 static struct event_trigger_ops stacktrace_trigger_ops = {
1590 .trigger = stacktrace_trigger,
1591 .print = stacktrace_trigger_print,
1592 .init = event_trigger_init,
1593 .free = event_trigger_free,
1594 };
1595
1596 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1597 .trigger = stacktrace_count_trigger,
1598 .print = stacktrace_trigger_print,
1599 .init = event_trigger_init,
1600 .free = event_trigger_free,
1601 };
1602
1603 static struct event_trigger_ops *
stacktrace_get_trigger_ops(char * cmd,char * param)1604 stacktrace_get_trigger_ops(char *cmd, char *param)
1605 {
1606 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1607 }
1608
1609 static struct event_command trigger_stacktrace_cmd = {
1610 .name = "stacktrace",
1611 .trigger_type = ETT_STACKTRACE,
1612 .flags = EVENT_CMD_FL_POST_TRIGGER,
1613 .parse = event_trigger_parse,
1614 .reg = register_trigger,
1615 .unreg = unregister_trigger,
1616 .get_trigger_ops = stacktrace_get_trigger_ops,
1617 .set_filter = set_trigger_filter,
1618 };
1619
register_trigger_stacktrace_cmd(void)1620 static __init int register_trigger_stacktrace_cmd(void)
1621 {
1622 int ret;
1623
1624 ret = register_event_command(&trigger_stacktrace_cmd);
1625 WARN_ON(ret < 0);
1626
1627 return ret;
1628 }
1629 #else
register_trigger_stacktrace_cmd(void)1630 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1631 #endif /* CONFIG_STACKTRACE */
1632
unregister_trigger_traceon_traceoff_cmds(void)1633 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1634 {
1635 unregister_event_command(&trigger_traceon_cmd);
1636 unregister_event_command(&trigger_traceoff_cmd);
1637 }
1638
1639 static void
event_enable_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1640 event_enable_trigger(struct event_trigger_data *data,
1641 struct trace_buffer *buffer, void *rec,
1642 struct ring_buffer_event *event)
1643 {
1644 struct enable_trigger_data *enable_data = data->private_data;
1645
1646 if (enable_data->enable)
1647 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1648 else
1649 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1650 }
1651
1652 static void
event_enable_count_trigger(struct event_trigger_data * data,struct trace_buffer * buffer,void * rec,struct ring_buffer_event * event)1653 event_enable_count_trigger(struct event_trigger_data *data,
1654 struct trace_buffer *buffer, void *rec,
1655 struct ring_buffer_event *event)
1656 {
1657 struct enable_trigger_data *enable_data = data->private_data;
1658
1659 if (!data->count)
1660 return;
1661
1662 /* Skip if the event is in a state we want to switch to */
1663 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1664 return;
1665
1666 if (data->count != -1)
1667 (data->count)--;
1668
1669 event_enable_trigger(data, buffer, rec, event);
1670 }
1671
event_enable_trigger_print(struct seq_file * m,struct event_trigger_data * data)1672 int event_enable_trigger_print(struct seq_file *m,
1673 struct event_trigger_data *data)
1674 {
1675 struct enable_trigger_data *enable_data = data->private_data;
1676
1677 seq_printf(m, "%s:%s:%s",
1678 enable_data->hist ?
1679 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1680 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1681 enable_data->file->event_call->class->system,
1682 trace_event_name(enable_data->file->event_call));
1683
1684 if (data->count == -1)
1685 seq_puts(m, ":unlimited");
1686 else
1687 seq_printf(m, ":count=%ld", data->count);
1688
1689 if (data->filter_str)
1690 seq_printf(m, " if %s\n", data->filter_str);
1691 else
1692 seq_putc(m, '\n');
1693
1694 return 0;
1695 }
1696
event_enable_trigger_free(struct event_trigger_data * data)1697 void event_enable_trigger_free(struct event_trigger_data *data)
1698 {
1699 struct enable_trigger_data *enable_data = data->private_data;
1700
1701 if (WARN_ON_ONCE(data->ref <= 0))
1702 return;
1703
1704 data->ref--;
1705 if (!data->ref) {
1706 /* Remove the SOFT_MODE flag */
1707 trace_event_enable_disable(enable_data->file, 0, 1);
1708 trace_event_put_ref(enable_data->file->event_call);
1709 trigger_data_free(data);
1710 kfree(enable_data);
1711 }
1712 }
1713
1714 static struct event_trigger_ops event_enable_trigger_ops = {
1715 .trigger = event_enable_trigger,
1716 .print = event_enable_trigger_print,
1717 .init = event_trigger_init,
1718 .free = event_enable_trigger_free,
1719 };
1720
1721 static struct event_trigger_ops event_enable_count_trigger_ops = {
1722 .trigger = event_enable_count_trigger,
1723 .print = event_enable_trigger_print,
1724 .init = event_trigger_init,
1725 .free = event_enable_trigger_free,
1726 };
1727
1728 static struct event_trigger_ops event_disable_trigger_ops = {
1729 .trigger = event_enable_trigger,
1730 .print = event_enable_trigger_print,
1731 .init = event_trigger_init,
1732 .free = event_enable_trigger_free,
1733 };
1734
1735 static struct event_trigger_ops event_disable_count_trigger_ops = {
1736 .trigger = event_enable_count_trigger,
1737 .print = event_enable_trigger_print,
1738 .init = event_trigger_init,
1739 .free = event_enable_trigger_free,
1740 };
1741
event_enable_trigger_parse(struct event_command * cmd_ops,struct trace_event_file * file,char * glob,char * cmd,char * param_and_filter)1742 int event_enable_trigger_parse(struct event_command *cmd_ops,
1743 struct trace_event_file *file,
1744 char *glob, char *cmd, char *param_and_filter)
1745 {
1746 struct trace_event_file *event_enable_file;
1747 struct enable_trigger_data *enable_data;
1748 struct event_trigger_data *trigger_data;
1749 struct trace_array *tr = file->tr;
1750 char *param, *filter;
1751 bool enable, remove;
1752 const char *system;
1753 const char *event;
1754 bool hist = false;
1755 int ret;
1756
1757 remove = event_trigger_check_remove(glob);
1758
1759 if (event_trigger_empty_param(param_and_filter))
1760 return -EINVAL;
1761
1762 ret = event_trigger_separate_filter(param_and_filter, ¶m, &filter, true);
1763 if (ret)
1764 return ret;
1765
1766 system = strsep(¶m, ":");
1767 if (!param)
1768 return -EINVAL;
1769
1770 event = strsep(¶m, ":");
1771
1772 ret = -EINVAL;
1773 event_enable_file = find_event_file(tr, system, event);
1774 if (!event_enable_file)
1775 goto out;
1776
1777 #ifdef CONFIG_HIST_TRIGGERS
1778 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1779 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1780
1781 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1782 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1783 #else
1784 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1785 #endif
1786 ret = -ENOMEM;
1787
1788 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1789 if (!enable_data)
1790 goto out;
1791
1792 enable_data->hist = hist;
1793 enable_data->enable = enable;
1794 enable_data->file = event_enable_file;
1795
1796 trigger_data = event_trigger_alloc(cmd_ops, cmd, param, enable_data);
1797 if (!trigger_data) {
1798 kfree(enable_data);
1799 goto out;
1800 }
1801
1802 if (remove) {
1803 event_trigger_unregister(cmd_ops, file, glob+1, trigger_data);
1804 kfree(trigger_data);
1805 kfree(enable_data);
1806 ret = 0;
1807 goto out;
1808 }
1809
1810 /* Up the trigger_data count to make sure nothing frees it on failure */
1811 event_trigger_init(trigger_data);
1812
1813 ret = event_trigger_parse_num(param, trigger_data);
1814 if (ret)
1815 goto out_free;
1816
1817 ret = event_trigger_set_filter(cmd_ops, file, filter, trigger_data);
1818 if (ret < 0)
1819 goto out_free;
1820
1821 /* Don't let event modules unload while probe registered */
1822 ret = trace_event_try_get_ref(event_enable_file->event_call);
1823 if (!ret) {
1824 ret = -EBUSY;
1825 goto out_free;
1826 }
1827
1828 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1829 if (ret < 0)
1830 goto out_put;
1831
1832 ret = event_trigger_register(cmd_ops, file, glob, trigger_data);
1833 if (ret)
1834 goto out_disable;
1835
1836 event_trigger_free(trigger_data);
1837 out:
1838 return ret;
1839 out_disable:
1840 trace_event_enable_disable(event_enable_file, 0, 1);
1841 out_put:
1842 trace_event_put_ref(event_enable_file->event_call);
1843 out_free:
1844 event_trigger_reset_filter(cmd_ops, trigger_data);
1845 event_trigger_free(trigger_data);
1846 kfree(enable_data);
1847
1848 goto out;
1849 }
1850
event_enable_register_trigger(char * glob,struct event_trigger_data * data,struct trace_event_file * file)1851 int event_enable_register_trigger(char *glob,
1852 struct event_trigger_data *data,
1853 struct trace_event_file *file)
1854 {
1855 struct enable_trigger_data *enable_data = data->private_data;
1856 struct enable_trigger_data *test_enable_data;
1857 struct event_trigger_data *test;
1858 int ret = 0;
1859
1860 lockdep_assert_held(&event_mutex);
1861
1862 list_for_each_entry(test, &file->triggers, list) {
1863 test_enable_data = test->private_data;
1864 if (test_enable_data &&
1865 (test->cmd_ops->trigger_type ==
1866 data->cmd_ops->trigger_type) &&
1867 (test_enable_data->file == enable_data->file)) {
1868 ret = -EEXIST;
1869 goto out;
1870 }
1871 }
1872
1873 if (data->ops->init) {
1874 ret = data->ops->init(data);
1875 if (ret < 0)
1876 goto out;
1877 }
1878
1879 list_add_rcu(&data->list, &file->triggers);
1880
1881 update_cond_flag(file);
1882 ret = trace_event_trigger_enable_disable(file, 1);
1883 if (ret < 0) {
1884 list_del_rcu(&data->list);
1885 update_cond_flag(file);
1886 }
1887 out:
1888 return ret;
1889 }
1890
event_enable_unregister_trigger(char * glob,struct event_trigger_data * test,struct trace_event_file * file)1891 void event_enable_unregister_trigger(char *glob,
1892 struct event_trigger_data *test,
1893 struct trace_event_file *file)
1894 {
1895 struct enable_trigger_data *test_enable_data = test->private_data;
1896 struct event_trigger_data *data = NULL, *iter;
1897 struct enable_trigger_data *enable_data;
1898
1899 lockdep_assert_held(&event_mutex);
1900
1901 list_for_each_entry(iter, &file->triggers, list) {
1902 enable_data = iter->private_data;
1903 if (enable_data &&
1904 (iter->cmd_ops->trigger_type ==
1905 test->cmd_ops->trigger_type) &&
1906 (enable_data->file == test_enable_data->file)) {
1907 data = iter;
1908 list_del_rcu(&data->list);
1909 trace_event_trigger_enable_disable(file, 0);
1910 update_cond_flag(file);
1911 break;
1912 }
1913 }
1914
1915 if (data && data->ops->free)
1916 data->ops->free(data);
1917 }
1918
1919 static struct event_trigger_ops *
event_enable_get_trigger_ops(char * cmd,char * param)1920 event_enable_get_trigger_ops(char *cmd, char *param)
1921 {
1922 struct event_trigger_ops *ops;
1923 bool enable;
1924
1925 #ifdef CONFIG_HIST_TRIGGERS
1926 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1927 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1928 #else
1929 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1930 #endif
1931 if (enable)
1932 ops = param ? &event_enable_count_trigger_ops :
1933 &event_enable_trigger_ops;
1934 else
1935 ops = param ? &event_disable_count_trigger_ops :
1936 &event_disable_trigger_ops;
1937
1938 return ops;
1939 }
1940
1941 static struct event_command trigger_enable_cmd = {
1942 .name = ENABLE_EVENT_STR,
1943 .trigger_type = ETT_EVENT_ENABLE,
1944 .parse = event_enable_trigger_parse,
1945 .reg = event_enable_register_trigger,
1946 .unreg = event_enable_unregister_trigger,
1947 .get_trigger_ops = event_enable_get_trigger_ops,
1948 .set_filter = set_trigger_filter,
1949 };
1950
1951 static struct event_command trigger_disable_cmd = {
1952 .name = DISABLE_EVENT_STR,
1953 .trigger_type = ETT_EVENT_ENABLE,
1954 .parse = event_enable_trigger_parse,
1955 .reg = event_enable_register_trigger,
1956 .unreg = event_enable_unregister_trigger,
1957 .get_trigger_ops = event_enable_get_trigger_ops,
1958 .set_filter = set_trigger_filter,
1959 };
1960
unregister_trigger_enable_disable_cmds(void)1961 static __init void unregister_trigger_enable_disable_cmds(void)
1962 {
1963 unregister_event_command(&trigger_enable_cmd);
1964 unregister_event_command(&trigger_disable_cmd);
1965 }
1966
register_trigger_enable_disable_cmds(void)1967 static __init int register_trigger_enable_disable_cmds(void)
1968 {
1969 int ret;
1970
1971 ret = register_event_command(&trigger_enable_cmd);
1972 if (WARN_ON(ret < 0))
1973 return ret;
1974 ret = register_event_command(&trigger_disable_cmd);
1975 if (WARN_ON(ret < 0))
1976 unregister_trigger_enable_disable_cmds();
1977
1978 return ret;
1979 }
1980
register_trigger_traceon_traceoff_cmds(void)1981 static __init int register_trigger_traceon_traceoff_cmds(void)
1982 {
1983 int ret;
1984
1985 ret = register_event_command(&trigger_traceon_cmd);
1986 if (WARN_ON(ret < 0))
1987 return ret;
1988 ret = register_event_command(&trigger_traceoff_cmd);
1989 if (WARN_ON(ret < 0))
1990 unregister_trigger_traceon_traceoff_cmds();
1991
1992 return ret;
1993 }
1994
register_trigger_cmds(void)1995 __init int register_trigger_cmds(void)
1996 {
1997 register_trigger_traceon_traceoff_cmds();
1998 register_trigger_snapshot_cmd();
1999 register_trigger_stacktrace_cmd();
2000 register_trigger_enable_disable_cmds();
2001 register_trigger_hist_enable_disable_cmds();
2002 register_trigger_hist_cmd();
2003
2004 return 0;
2005 }
2006