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