blob: 0a62887c63c086495b166348f028e0f717264ad5 [file] [log] [blame]
Tom Zanussi85f2b082013-10-24 08:59:24 -05001/*
2 * trace_events_trigger - trace event triggers
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * Copyright (C) 2013 Tom Zanussi <tom.zanussi@linux.intel.com>
19 */
20
21#include <linux/module.h>
22#include <linux/ctype.h>
23#include <linux/mutex.h>
24#include <linux/slab.h>
25
26#include "trace.h"
27
28static LIST_HEAD(trigger_commands);
29static DEFINE_MUTEX(trigger_cmd_mutex);
30
Tom Zanussiab4bf002015-12-10 12:50:44 -060031void trigger_data_free(struct event_trigger_data *data)
Tom Zanussi2a2df322013-10-24 08:59:25 -050032{
Tom Zanussibac5fb92013-10-24 08:59:29 -050033 if (data->cmd_ops->set_filter)
34 data->cmd_ops->set_filter(NULL, data, NULL);
35
Tom Zanussi2a2df322013-10-24 08:59:25 -050036 synchronize_sched(); /* make sure current triggers exit before free */
37 kfree(data);
38}
39
Tom Zanussi85f2b082013-10-24 08:59:24 -050040/**
41 * event_triggers_call - Call triggers associated with a trace event
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040042 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050043 * @rec: The trace entry for the event, NULL for unconditional invocation
Tom Zanussi85f2b082013-10-24 08:59:24 -050044 *
45 * For each trigger associated with an event, invoke the trigger
Tom Zanussibac5fb92013-10-24 08:59:29 -050046 * function registered with the associated trigger command. If rec is
47 * non-NULL, it means that the trigger requires further processing and
48 * shouldn't be unconditionally invoked. If rec is non-NULL and the
49 * trigger has a filter associated with it, rec will checked against
50 * the filter and if the record matches the trigger will be invoked.
51 * If the trigger is a 'post_trigger', meaning it shouldn't be invoked
52 * in any case until the current event is written, the trigger
53 * function isn't invoked but the bit associated with the deferred
54 * trigger is set in the return value.
55 *
56 * Returns an enum event_trigger_type value containing a set bit for
57 * any trigger that should be deferred, ETT_NONE if nothing to defer.
Tom Zanussi85f2b082013-10-24 08:59:24 -050058 *
59 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
60 *
61 * Return: an enum event_trigger_type value containing a set bit for
62 * any trigger that should be deferred, ETT_NONE if nothing to defer.
63 */
Tom Zanussibac5fb92013-10-24 08:59:29 -050064enum event_trigger_type
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040065event_triggers_call(struct trace_event_file *file, void *rec)
Tom Zanussibac5fb92013-10-24 08:59:29 -050066{
67 struct event_trigger_data *data;
68 enum event_trigger_type tt = ETT_NONE;
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -050069 struct event_filter *filter;
Tom Zanussibac5fb92013-10-24 08:59:29 -050070
71 if (list_empty(&file->triggers))
72 return tt;
73
74 list_for_each_entry_rcu(data, &file->triggers, list) {
75 if (!rec) {
Tom Zanussic4a59232015-12-10 12:50:45 -060076 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -050077 continue;
78 }
Steven Rostedt (Red Hat)561a4fe2014-05-02 13:30:04 -040079 filter = rcu_dereference_sched(data->filter);
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -050080 if (filter && !filter_match_preds(filter, rec))
Tom Zanussibac5fb92013-10-24 08:59:29 -050081 continue;
82 if (data->cmd_ops->post_trigger) {
83 tt |= data->cmd_ops->trigger_type;
84 continue;
85 }
Tom Zanussic4a59232015-12-10 12:50:45 -060086 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -050087 }
88 return tt;
89}
90EXPORT_SYMBOL_GPL(event_triggers_call);
91
92/**
93 * event_triggers_post_call - Call 'post_triggers' for a trace event
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040094 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050095 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
Tom Zanussic4a59232015-12-10 12:50:45 -060096 * @rec: The trace entry for the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050097 *
98 * For each trigger associated with an event, invoke the trigger
99 * function registered with the associated trigger command, if the
100 * corresponding bit is set in the tt enum passed into this function.
101 * See @event_triggers_call for details on how those bits are set.
102 *
103 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
104 */
105void
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400106event_triggers_post_call(struct trace_event_file *file,
Tom Zanussic4a59232015-12-10 12:50:45 -0600107 enum event_trigger_type tt,
108 void *rec)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500109{
110 struct event_trigger_data *data;
111
Tom Zanussibac5fb92013-10-24 08:59:29 -0500112 list_for_each_entry_rcu(data, &file->triggers, list) {
113 if (data->cmd_ops->trigger_type & tt)
Tom Zanussic4a59232015-12-10 12:50:45 -0600114 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500115 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500116}
Tom Zanussibac5fb92013-10-24 08:59:29 -0500117EXPORT_SYMBOL_GPL(event_triggers_post_call);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500118
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500119#define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
120
Tom Zanussi85f2b082013-10-24 08:59:24 -0500121static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
122{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400123 struct trace_event_file *event_file = event_file_data(m->private);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500124
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500125 if (t == SHOW_AVAILABLE_TRIGGERS)
126 return NULL;
127
Tom Zanussi85f2b082013-10-24 08:59:24 -0500128 return seq_list_next(t, &event_file->triggers, pos);
129}
130
131static void *trigger_start(struct seq_file *m, loff_t *pos)
132{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400133 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500134
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
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500141 if (list_empty(&event_file->triggers))
142 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
143
Tom Zanussi85f2b082013-10-24 08:59:24 -0500144 return seq_list_start(&event_file->triggers, *pos);
145}
146
147static void trigger_stop(struct seq_file *m, void *t)
148{
149 mutex_unlock(&event_mutex);
150}
151
152static int trigger_show(struct seq_file *m, void *v)
153{
154 struct event_trigger_data *data;
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500155 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 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500167
168 data = list_entry(v, struct event_trigger_data, list);
169 data->ops->print(m, data->ops, data);
170
171 return 0;
172}
173
174static 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
181static 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
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400205static int trigger_process_regex(struct trace_event_file *file, char *buff)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500206{
207 char *command, *next = buff;
208 struct event_command *p;
209 int ret = -EINVAL;
210
211 command = strsep(&next, ": \t");
212 command = (command[0] != '!') ? command : command + 1;
213
214 mutex_lock(&trigger_cmd_mutex);
215 list_for_each_entry(p, &trigger_commands, list) {
216 if (strcmp(p->name, command) == 0) {
217 ret = p->func(p, file, buff, command, next);
218 goto out_unlock;
219 }
220 }
221 out_unlock:
222 mutex_unlock(&trigger_cmd_mutex);
223
224 return ret;
225}
226
227static ssize_t event_trigger_regex_write(struct file *file,
228 const char __user *ubuf,
229 size_t cnt, loff_t *ppos)
230{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400231 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500232 ssize_t ret;
233 char *buf;
234
235 if (!cnt)
236 return 0;
237
238 if (cnt >= PAGE_SIZE)
239 return -EINVAL;
240
Al Viro70f6cbb2015-12-24 00:13:10 -0500241 buf = memdup_user_nul(ubuf, cnt);
242 if (IS_ERR(buf))
243 return PTR_ERR(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500244
Tom Zanussi85f2b082013-10-24 08:59:24 -0500245 strim(buf);
246
247 mutex_lock(&event_mutex);
248 event_file = event_file_data(file);
249 if (unlikely(!event_file)) {
250 mutex_unlock(&event_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -0500251 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500252 return -ENODEV;
253 }
254 ret = trigger_process_regex(event_file, buf);
255 mutex_unlock(&event_mutex);
256
Al Viro70f6cbb2015-12-24 00:13:10 -0500257 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500258 if (ret < 0)
259 goto out;
260
261 *ppos += cnt;
262 ret = cnt;
263 out:
264 return ret;
265}
266
267static int event_trigger_regex_release(struct inode *inode, struct file *file)
268{
269 mutex_lock(&event_mutex);
270
271 if (file->f_mode & FMODE_READ)
272 seq_release(inode, file);
273
274 mutex_unlock(&event_mutex);
275
276 return 0;
277}
278
279static ssize_t
280event_trigger_write(struct file *filp, const char __user *ubuf,
281 size_t cnt, loff_t *ppos)
282{
283 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
284}
285
286static int
287event_trigger_open(struct inode *inode, struct file *filp)
288{
289 return event_trigger_regex_open(inode, filp);
290}
291
292static int
293event_trigger_release(struct inode *inode, struct file *file)
294{
295 return event_trigger_regex_release(inode, file);
296}
297
298const struct file_operations event_trigger_fops = {
299 .open = event_trigger_open,
300 .read = seq_read,
301 .write = event_trigger_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -0500302 .llseek = tracing_lseek,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500303 .release = event_trigger_release,
304};
305
Tom Zanussi2a2df322013-10-24 08:59:25 -0500306/*
307 * Currently we only register event commands from __init, so mark this
308 * __init too.
309 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600310__init int register_event_command(struct event_command *cmd)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500311{
312 struct event_command *p;
313 int ret = 0;
314
315 mutex_lock(&trigger_cmd_mutex);
316 list_for_each_entry(p, &trigger_commands, list) {
317 if (strcmp(cmd->name, p->name) == 0) {
318 ret = -EBUSY;
319 goto out_unlock;
320 }
321 }
322 list_add(&cmd->list, &trigger_commands);
323 out_unlock:
324 mutex_unlock(&trigger_cmd_mutex);
325
326 return ret;
327}
328
329/*
330 * Currently we only unregister event commands from __init, so mark
331 * this __init too.
332 */
333static __init int unregister_event_command(struct event_command *cmd)
334{
335 struct event_command *p, *n;
336 int ret = -ENODEV;
337
338 mutex_lock(&trigger_cmd_mutex);
339 list_for_each_entry_safe(p, n, &trigger_commands, list) {
340 if (strcmp(cmd->name, p->name) == 0) {
341 ret = 0;
342 list_del_init(&p->list);
343 goto out_unlock;
344 }
345 }
346 out_unlock:
347 mutex_unlock(&trigger_cmd_mutex);
348
349 return ret;
350}
351
352/**
353 * event_trigger_print - Generic event_trigger_ops @print implementation
354 * @name: The name of the event trigger
355 * @m: The seq_file being printed to
356 * @data: Trigger-specific data
357 * @filter_str: filter_str to print, if present
358 *
359 * Common implementation for event triggers to print themselves.
360 *
361 * Usually wrapped by a function that simply sets the @name of the
362 * trigger command and then invokes this.
363 *
364 * Return: 0 on success, errno otherwise
365 */
366static int
367event_trigger_print(const char *name, struct seq_file *m,
368 void *data, char *filter_str)
369{
370 long count = (long)data;
371
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100372 seq_puts(m, name);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500373
374 if (count == -1)
375 seq_puts(m, ":unlimited");
376 else
377 seq_printf(m, ":count=%ld", count);
378
379 if (filter_str)
380 seq_printf(m, " if %s\n", filter_str);
381 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +0100382 seq_putc(m, '\n');
Tom Zanussi2a2df322013-10-24 08:59:25 -0500383
384 return 0;
385}
386
387/**
388 * event_trigger_init - Generic event_trigger_ops @init implementation
389 * @ops: The trigger ops associated with the trigger
390 * @data: Trigger-specific data
391 *
392 * Common implementation of event trigger initialization.
393 *
394 * Usually used directly as the @init method in event trigger
395 * implementations.
396 *
397 * Return: 0 on success, errno otherwise
398 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600399int event_trigger_init(struct event_trigger_ops *ops,
400 struct event_trigger_data *data)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500401{
402 data->ref++;
403 return 0;
404}
405
406/**
407 * event_trigger_free - Generic event_trigger_ops @free implementation
408 * @ops: The trigger ops associated with the trigger
409 * @data: Trigger-specific data
410 *
411 * Common implementation of event trigger de-initialization.
412 *
413 * Usually used directly as the @free method in event trigger
414 * implementations.
415 */
416static void
417event_trigger_free(struct event_trigger_ops *ops,
418 struct event_trigger_data *data)
419{
420 if (WARN_ON_ONCE(data->ref <= 0))
421 return;
422
423 data->ref--;
424 if (!data->ref)
425 trigger_data_free(data);
426}
427
Tom Zanussiab4bf002015-12-10 12:50:44 -0600428int trace_event_trigger_enable_disable(struct trace_event_file *file,
429 int trigger_enable)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500430{
431 int ret = 0;
432
433 if (trigger_enable) {
434 if (atomic_inc_return(&file->tm_ref) > 1)
435 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400436 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500437 ret = trace_event_enable_disable(file, 1, 1);
438 } else {
439 if (atomic_dec_return(&file->tm_ref) > 0)
440 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400441 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500442 ret = trace_event_enable_disable(file, 0, 1);
443 }
444
445 return ret;
446}
447
448/**
449 * clear_event_triggers - Clear all triggers associated with a trace array
450 * @tr: The trace array to clear
451 *
452 * For each trigger, the triggering event has its tm_ref decremented
453 * via trace_event_trigger_enable_disable(), and any associated event
454 * (in the case of enable/disable_event triggers) will have its sm_ref
455 * decremented via free()->trace_event_enable_disable(). That
456 * combination effectively reverses the soft-mode/trigger state added
457 * by trigger registration.
458 *
459 * Must be called with event_mutex held.
460 */
461void
462clear_event_triggers(struct trace_array *tr)
463{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400464 struct trace_event_file *file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500465
466 list_for_each_entry(file, &tr->events, list) {
467 struct event_trigger_data *data;
468 list_for_each_entry_rcu(data, &file->triggers, list) {
469 trace_event_trigger_enable_disable(file, 0);
470 if (data->ops->free)
471 data->ops->free(data->ops, data);
472 }
473 }
474}
475
Tom Zanussi2a2df322013-10-24 08:59:25 -0500476/**
Tom Zanussibac5fb92013-10-24 08:59:29 -0500477 * update_cond_flag - Set or reset the TRIGGER_COND bit
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400478 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -0500479 *
480 * If an event has triggers and any of those triggers has a filter or
481 * a post_trigger, trigger invocation needs to be deferred until after
482 * the current event has logged its data, and the event should have
483 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
484 * cleared.
485 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600486void update_cond_flag(struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -0500487{
488 struct event_trigger_data *data;
489 bool set_cond = false;
490
491 list_for_each_entry_rcu(data, &file->triggers, list) {
492 if (data->filter || data->cmd_ops->post_trigger) {
493 set_cond = true;
494 break;
495 }
496 }
497
498 if (set_cond)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400499 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500500 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400501 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500502}
503
504/**
Tom Zanussi2a2df322013-10-24 08:59:25 -0500505 * register_trigger - Generic event_command @reg implementation
506 * @glob: The raw string used to register the trigger
507 * @ops: The trigger ops associated with the trigger
508 * @data: Trigger-specific data to associate with the trigger
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400509 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500510 *
511 * Common implementation for event trigger registration.
512 *
513 * Usually used directly as the @reg method in event command
514 * implementations.
515 *
516 * Return: 0 on success, errno otherwise
517 */
518static int register_trigger(char *glob, struct event_trigger_ops *ops,
519 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400520 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500521{
522 struct event_trigger_data *test;
523 int ret = 0;
524
525 list_for_each_entry_rcu(test, &file->triggers, list) {
526 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
527 ret = -EEXIST;
528 goto out;
529 }
530 }
531
532 if (data->ops->init) {
533 ret = data->ops->init(data->ops, data);
534 if (ret < 0)
535 goto out;
536 }
537
538 list_add_rcu(&data->list, &file->triggers);
539 ret++;
540
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600541 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500542 if (trace_event_trigger_enable_disable(file, 1) < 0) {
543 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600544 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500545 ret--;
546 }
547out:
548 return ret;
549}
550
551/**
552 * unregister_trigger - Generic event_command @unreg implementation
553 * @glob: The raw string used to register the trigger
554 * @ops: The trigger ops associated with the trigger
555 * @test: Trigger-specific data used to find the trigger to remove
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400556 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500557 *
558 * Common implementation for event trigger unregistration.
559 *
560 * Usually used directly as the @unreg method in event command
561 * implementations.
562 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600563void unregister_trigger(char *glob, struct event_trigger_ops *ops,
564 struct event_trigger_data *test,
565 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500566{
567 struct event_trigger_data *data;
568 bool unregistered = false;
569
570 list_for_each_entry_rcu(data, &file->triggers, list) {
571 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
572 unregistered = true;
573 list_del_rcu(&data->list);
574 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600575 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500576 break;
577 }
578 }
579
580 if (unregistered && data->ops->free)
581 data->ops->free(data->ops, data);
582}
583
584/**
585 * event_trigger_callback - Generic event_command @func implementation
586 * @cmd_ops: The command ops, used for trigger registration
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400587 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500588 * @glob: The raw string used to register the trigger
589 * @cmd: The cmd portion of the string used to register the trigger
590 * @param: The params portion of the string used to register the trigger
591 *
592 * Common implementation for event command parsing and trigger
593 * instantiation.
594 *
595 * Usually used directly as the @func method in event command
596 * implementations.
597 *
598 * Return: 0 on success, errno otherwise
599 */
600static int
601event_trigger_callback(struct event_command *cmd_ops,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400602 struct trace_event_file *file,
Tom Zanussi2a2df322013-10-24 08:59:25 -0500603 char *glob, char *cmd, char *param)
604{
605 struct event_trigger_data *trigger_data;
606 struct event_trigger_ops *trigger_ops;
607 char *trigger = NULL;
608 char *number;
609 int ret;
610
611 /* separate the trigger from the filter (t:n [if filter]) */
612 if (param && isdigit(param[0]))
613 trigger = strsep(&param, " \t");
614
615 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
616
617 ret = -ENOMEM;
618 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
619 if (!trigger_data)
620 goto out;
621
622 trigger_data->count = -1;
623 trigger_data->ops = trigger_ops;
624 trigger_data->cmd_ops = cmd_ops;
625 INIT_LIST_HEAD(&trigger_data->list);
626
627 if (glob[0] == '!') {
628 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
629 kfree(trigger_data);
630 ret = 0;
631 goto out;
632 }
633
634 if (trigger) {
635 number = strsep(&trigger, ":");
636
637 ret = -EINVAL;
638 if (!strlen(number))
639 goto out_free;
640
641 /*
642 * We use the callback data field (which is a pointer)
643 * as our counter.
644 */
645 ret = kstrtoul(number, 0, &trigger_data->count);
646 if (ret)
647 goto out_free;
648 }
649
650 if (!param) /* if param is non-empty, it's supposed to be a filter */
651 goto out_reg;
652
653 if (!cmd_ops->set_filter)
654 goto out_reg;
655
656 ret = cmd_ops->set_filter(param, trigger_data, file);
657 if (ret < 0)
658 goto out_free;
659
660 out_reg:
661 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
662 /*
663 * The above returns on success the # of functions enabled,
664 * but if it didn't find any functions it returns zero.
665 * Consider no functions a failure too.
666 */
667 if (!ret) {
668 ret = -ENOENT;
669 goto out_free;
670 } else if (ret < 0)
671 goto out_free;
672 ret = 0;
673 out:
674 return ret;
675
676 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -0500677 if (cmd_ops->set_filter)
678 cmd_ops->set_filter(NULL, trigger_data, NULL);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500679 kfree(trigger_data);
680 goto out;
681}
682
Tom Zanussibac5fb92013-10-24 08:59:29 -0500683/**
684 * set_trigger_filter - Generic event_command @set_filter implementation
685 * @filter_str: The filter string for the trigger, NULL to remove filter
686 * @trigger_data: Trigger-specific data
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400687 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -0500688 *
689 * Common implementation for event command filter parsing and filter
690 * instantiation.
691 *
692 * Usually used directly as the @set_filter method in event command
693 * implementations.
694 *
695 * Also used to remove a filter (if filter_str = NULL).
696 *
697 * Return: 0 on success, errno otherwise
698 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600699int set_trigger_filter(char *filter_str,
700 struct event_trigger_data *trigger_data,
701 struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -0500702{
703 struct event_trigger_data *data = trigger_data;
704 struct event_filter *filter = NULL, *tmp;
705 int ret = -EINVAL;
706 char *s;
707
708 if (!filter_str) /* clear the current filter */
709 goto assign;
710
711 s = strsep(&filter_str, " \t");
712
713 if (!strlen(s) || strcmp(s, "if") != 0)
714 goto out;
715
716 if (!filter_str)
717 goto out;
718
719 /* The filter is for the 'trigger' event, not the triggered event */
720 ret = create_event_filter(file->event_call, filter_str, false, &filter);
721 if (ret)
722 goto out;
723 assign:
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -0500724 tmp = rcu_access_pointer(data->filter);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500725
726 rcu_assign_pointer(data->filter, filter);
727
728 if (tmp) {
729 /* Make sure the call is done with the filter */
730 synchronize_sched();
731 free_event_filter(tmp);
732 }
733
734 kfree(data->filter_str);
735 data->filter_str = NULL;
736
737 if (filter_str) {
738 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
739 if (!data->filter_str) {
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -0500740 free_event_filter(rcu_access_pointer(data->filter));
Tom Zanussibac5fb92013-10-24 08:59:29 -0500741 data->filter = NULL;
742 ret = -ENOMEM;
743 }
744 }
745 out:
746 return ret;
747}
748
Tom Zanussi2a2df322013-10-24 08:59:25 -0500749static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600750traceon_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500751{
752 if (tracing_is_on())
753 return;
754
755 tracing_on();
756}
757
758static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600759traceon_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500760{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500761 if (tracing_is_on())
762 return;
763
Tom Zanussi2a2df322013-10-24 08:59:25 -0500764 if (!data->count)
765 return;
766
767 if (data->count != -1)
768 (data->count)--;
769
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500770 tracing_on();
Tom Zanussi2a2df322013-10-24 08:59:25 -0500771}
772
773static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600774traceoff_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500775{
776 if (!tracing_is_on())
777 return;
778
779 tracing_off();
780}
781
782static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600783traceoff_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500784{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500785 if (!tracing_is_on())
786 return;
787
Tom Zanussi2a2df322013-10-24 08:59:25 -0500788 if (!data->count)
789 return;
790
791 if (data->count != -1)
792 (data->count)--;
793
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500794 tracing_off();
Tom Zanussi2a2df322013-10-24 08:59:25 -0500795}
796
797static int
798traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
799 struct event_trigger_data *data)
800{
801 return event_trigger_print("traceon", m, (void *)data->count,
802 data->filter_str);
803}
804
805static int
806traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
807 struct event_trigger_data *data)
808{
809 return event_trigger_print("traceoff", m, (void *)data->count,
810 data->filter_str);
811}
812
813static struct event_trigger_ops traceon_trigger_ops = {
814 .func = traceon_trigger,
815 .print = traceon_trigger_print,
816 .init = event_trigger_init,
817 .free = event_trigger_free,
818};
819
820static struct event_trigger_ops traceon_count_trigger_ops = {
821 .func = traceon_count_trigger,
822 .print = traceon_trigger_print,
823 .init = event_trigger_init,
824 .free = event_trigger_free,
825};
826
827static struct event_trigger_ops traceoff_trigger_ops = {
828 .func = traceoff_trigger,
829 .print = traceoff_trigger_print,
830 .init = event_trigger_init,
831 .free = event_trigger_free,
832};
833
834static struct event_trigger_ops traceoff_count_trigger_ops = {
835 .func = traceoff_count_trigger,
836 .print = traceoff_trigger_print,
837 .init = event_trigger_init,
838 .free = event_trigger_free,
839};
840
841static struct event_trigger_ops *
842onoff_get_trigger_ops(char *cmd, char *param)
843{
844 struct event_trigger_ops *ops;
845
846 /* we register both traceon and traceoff to this callback */
847 if (strcmp(cmd, "traceon") == 0)
848 ops = param ? &traceon_count_trigger_ops :
849 &traceon_trigger_ops;
850 else
851 ops = param ? &traceoff_count_trigger_ops :
852 &traceoff_trigger_ops;
853
854 return ops;
855}
856
857static struct event_command trigger_traceon_cmd = {
858 .name = "traceon",
859 .trigger_type = ETT_TRACE_ONOFF,
860 .func = event_trigger_callback,
861 .reg = register_trigger,
862 .unreg = unregister_trigger,
863 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -0500864 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -0500865};
866
867static struct event_command trigger_traceoff_cmd = {
868 .name = "traceoff",
869 .trigger_type = ETT_TRACE_ONOFF,
870 .func = event_trigger_callback,
871 .reg = register_trigger,
872 .unreg = unregister_trigger,
873 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -0500874 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -0500875};
876
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500877#ifdef CONFIG_TRACER_SNAPSHOT
878static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600879snapshot_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500880{
881 tracing_snapshot();
882}
883
884static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600885snapshot_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500886{
887 if (!data->count)
888 return;
889
890 if (data->count != -1)
891 (data->count)--;
892
Tom Zanussic4a59232015-12-10 12:50:45 -0600893 snapshot_trigger(data, rec);
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500894}
895
896static int
897register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
898 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400899 struct trace_event_file *file)
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500900{
901 int ret = register_trigger(glob, ops, data, file);
902
903 if (ret > 0 && tracing_alloc_snapshot() != 0) {
904 unregister_trigger(glob, ops, data, file);
905 ret = 0;
906 }
907
908 return ret;
909}
910
911static int
912snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
913 struct event_trigger_data *data)
914{
915 return event_trigger_print("snapshot", m, (void *)data->count,
916 data->filter_str);
917}
918
919static struct event_trigger_ops snapshot_trigger_ops = {
920 .func = snapshot_trigger,
921 .print = snapshot_trigger_print,
922 .init = event_trigger_init,
923 .free = event_trigger_free,
924};
925
926static struct event_trigger_ops snapshot_count_trigger_ops = {
927 .func = snapshot_count_trigger,
928 .print = snapshot_trigger_print,
929 .init = event_trigger_init,
930 .free = event_trigger_free,
931};
932
933static struct event_trigger_ops *
934snapshot_get_trigger_ops(char *cmd, char *param)
935{
936 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
937}
938
939static struct event_command trigger_snapshot_cmd = {
940 .name = "snapshot",
941 .trigger_type = ETT_SNAPSHOT,
942 .func = event_trigger_callback,
943 .reg = register_snapshot_trigger,
944 .unreg = unregister_trigger,
945 .get_trigger_ops = snapshot_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -0500946 .set_filter = set_trigger_filter,
Tom Zanussi93e31ff2013-10-24 08:59:26 -0500947};
948
949static __init int register_trigger_snapshot_cmd(void)
950{
951 int ret;
952
953 ret = register_event_command(&trigger_snapshot_cmd);
954 WARN_ON(ret < 0);
955
956 return ret;
957}
958#else
959static __init int register_trigger_snapshot_cmd(void) { return 0; }
960#endif /* CONFIG_TRACER_SNAPSHOT */
961
Tom Zanussif21ecbb2013-10-24 08:59:27 -0500962#ifdef CONFIG_STACKTRACE
963/*
964 * Skip 3:
965 * stacktrace_trigger()
966 * event_triggers_post_call()
Steven Rostedt (Red Hat)a7237762015-05-13 15:27:47 -0400967 * trace_event_raw_event_xxx()
Tom Zanussif21ecbb2013-10-24 08:59:27 -0500968 */
969#define STACK_SKIP 3
970
971static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600972stacktrace_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussif21ecbb2013-10-24 08:59:27 -0500973{
974 trace_dump_stack(STACK_SKIP);
975}
976
977static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600978stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussif21ecbb2013-10-24 08:59:27 -0500979{
980 if (!data->count)
981 return;
982
983 if (data->count != -1)
984 (data->count)--;
985
Tom Zanussic4a59232015-12-10 12:50:45 -0600986 stacktrace_trigger(data, rec);
Tom Zanussif21ecbb2013-10-24 08:59:27 -0500987}
988
989static int
990stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
991 struct event_trigger_data *data)
992{
993 return event_trigger_print("stacktrace", m, (void *)data->count,
994 data->filter_str);
995}
996
997static struct event_trigger_ops stacktrace_trigger_ops = {
998 .func = stacktrace_trigger,
999 .print = stacktrace_trigger_print,
1000 .init = event_trigger_init,
1001 .free = event_trigger_free,
1002};
1003
1004static struct event_trigger_ops stacktrace_count_trigger_ops = {
1005 .func = stacktrace_count_trigger,
1006 .print = stacktrace_trigger_print,
1007 .init = event_trigger_init,
1008 .free = event_trigger_free,
1009};
1010
1011static struct event_trigger_ops *
1012stacktrace_get_trigger_ops(char *cmd, char *param)
1013{
1014 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1015}
1016
1017static struct event_command trigger_stacktrace_cmd = {
1018 .name = "stacktrace",
1019 .trigger_type = ETT_STACKTRACE,
1020 .post_trigger = true,
1021 .func = event_trigger_callback,
1022 .reg = register_trigger,
1023 .unreg = unregister_trigger,
1024 .get_trigger_ops = stacktrace_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001025 .set_filter = set_trigger_filter,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001026};
1027
1028static __init int register_trigger_stacktrace_cmd(void)
1029{
1030 int ret;
1031
1032 ret = register_event_command(&trigger_stacktrace_cmd);
1033 WARN_ON(ret < 0);
1034
1035 return ret;
1036}
1037#else
1038static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1039#endif /* CONFIG_STACKTRACE */
1040
Tom Zanussi2a2df322013-10-24 08:59:25 -05001041static __init void unregister_trigger_traceon_traceoff_cmds(void)
1042{
1043 unregister_event_command(&trigger_traceon_cmd);
1044 unregister_event_command(&trigger_traceoff_cmd);
1045}
1046
Tom Zanussi7862ad12013-10-24 08:59:28 -05001047/* Avoid typos */
1048#define ENABLE_EVENT_STR "enable_event"
1049#define DISABLE_EVENT_STR "disable_event"
1050
1051struct enable_trigger_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001052 struct trace_event_file *file;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001053 bool enable;
1054};
1055
1056static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001057event_enable_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001058{
1059 struct enable_trigger_data *enable_data = data->private_data;
1060
1061 if (enable_data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001062 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001063 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001064 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001065}
1066
1067static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001068event_enable_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001069{
1070 struct enable_trigger_data *enable_data = data->private_data;
1071
1072 if (!data->count)
1073 return;
1074
1075 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001076 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussi7862ad12013-10-24 08:59:28 -05001077 return;
1078
1079 if (data->count != -1)
1080 (data->count)--;
1081
Tom Zanussic4a59232015-12-10 12:50:45 -06001082 event_enable_trigger(data, rec);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001083}
1084
1085static int
1086event_enable_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1087 struct event_trigger_data *data)
1088{
1089 struct enable_trigger_data *enable_data = data->private_data;
1090
1091 seq_printf(m, "%s:%s:%s",
1092 enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
1093 enable_data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001094 trace_event_name(enable_data->file->event_call));
Tom Zanussi7862ad12013-10-24 08:59:28 -05001095
1096 if (data->count == -1)
1097 seq_puts(m, ":unlimited");
1098 else
1099 seq_printf(m, ":count=%ld", data->count);
1100
1101 if (data->filter_str)
1102 seq_printf(m, " if %s\n", data->filter_str);
1103 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001104 seq_putc(m, '\n');
Tom Zanussi7862ad12013-10-24 08:59:28 -05001105
1106 return 0;
1107}
1108
1109static void
1110event_enable_trigger_free(struct event_trigger_ops *ops,
1111 struct event_trigger_data *data)
1112{
1113 struct enable_trigger_data *enable_data = data->private_data;
1114
1115 if (WARN_ON_ONCE(data->ref <= 0))
1116 return;
1117
1118 data->ref--;
1119 if (!data->ref) {
1120 /* Remove the SOFT_MODE flag */
1121 trace_event_enable_disable(enable_data->file, 0, 1);
1122 module_put(enable_data->file->event_call->mod);
1123 trigger_data_free(data);
1124 kfree(enable_data);
1125 }
1126}
1127
1128static struct event_trigger_ops event_enable_trigger_ops = {
1129 .func = event_enable_trigger,
1130 .print = event_enable_trigger_print,
1131 .init = event_trigger_init,
1132 .free = event_enable_trigger_free,
1133};
1134
1135static struct event_trigger_ops event_enable_count_trigger_ops = {
1136 .func = event_enable_count_trigger,
1137 .print = event_enable_trigger_print,
1138 .init = event_trigger_init,
1139 .free = event_enable_trigger_free,
1140};
1141
1142static struct event_trigger_ops event_disable_trigger_ops = {
1143 .func = event_enable_trigger,
1144 .print = event_enable_trigger_print,
1145 .init = event_trigger_init,
1146 .free = event_enable_trigger_free,
1147};
1148
1149static struct event_trigger_ops event_disable_count_trigger_ops = {
1150 .func = event_enable_count_trigger,
1151 .print = event_enable_trigger_print,
1152 .init = event_trigger_init,
1153 .free = event_enable_trigger_free,
1154};
1155
1156static int
1157event_enable_trigger_func(struct event_command *cmd_ops,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001158 struct trace_event_file *file,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001159 char *glob, char *cmd, char *param)
1160{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001161 struct trace_event_file *event_enable_file;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001162 struct enable_trigger_data *enable_data;
1163 struct event_trigger_data *trigger_data;
1164 struct event_trigger_ops *trigger_ops;
1165 struct trace_array *tr = file->tr;
1166 const char *system;
1167 const char *event;
1168 char *trigger;
1169 char *number;
1170 bool enable;
1171 int ret;
1172
1173 if (!param)
1174 return -EINVAL;
1175
1176 /* separate the trigger from the filter (s:e:n [if filter]) */
1177 trigger = strsep(&param, " \t");
1178 if (!trigger)
1179 return -EINVAL;
1180
1181 system = strsep(&trigger, ":");
1182 if (!trigger)
1183 return -EINVAL;
1184
1185 event = strsep(&trigger, ":");
1186
1187 ret = -EINVAL;
1188 event_enable_file = find_event_file(tr, system, event);
1189 if (!event_enable_file)
1190 goto out;
1191
1192 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1193
1194 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1195
1196 ret = -ENOMEM;
1197 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1198 if (!trigger_data)
1199 goto out;
1200
1201 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1202 if (!enable_data) {
1203 kfree(trigger_data);
1204 goto out;
1205 }
1206
1207 trigger_data->count = -1;
1208 trigger_data->ops = trigger_ops;
1209 trigger_data->cmd_ops = cmd_ops;
1210 INIT_LIST_HEAD(&trigger_data->list);
1211 RCU_INIT_POINTER(trigger_data->filter, NULL);
1212
1213 enable_data->enable = enable;
1214 enable_data->file = event_enable_file;
1215 trigger_data->private_data = enable_data;
1216
1217 if (glob[0] == '!') {
1218 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1219 kfree(trigger_data);
1220 kfree(enable_data);
1221 ret = 0;
1222 goto out;
1223 }
1224
1225 if (trigger) {
1226 number = strsep(&trigger, ":");
1227
1228 ret = -EINVAL;
1229 if (!strlen(number))
1230 goto out_free;
1231
1232 /*
1233 * We use the callback data field (which is a pointer)
1234 * as our counter.
1235 */
1236 ret = kstrtoul(number, 0, &trigger_data->count);
1237 if (ret)
1238 goto out_free;
1239 }
1240
1241 if (!param) /* if param is non-empty, it's supposed to be a filter */
1242 goto out_reg;
1243
1244 if (!cmd_ops->set_filter)
1245 goto out_reg;
1246
1247 ret = cmd_ops->set_filter(param, trigger_data, file);
1248 if (ret < 0)
1249 goto out_free;
1250
1251 out_reg:
1252 /* Don't let event modules unload while probe registered */
1253 ret = try_module_get(event_enable_file->event_call->mod);
1254 if (!ret) {
1255 ret = -EBUSY;
1256 goto out_free;
1257 }
1258
1259 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1260 if (ret < 0)
1261 goto out_put;
1262 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1263 /*
1264 * The above returns on success the # of functions enabled,
1265 * but if it didn't find any functions it returns zero.
1266 * Consider no functions a failure too.
1267 */
1268 if (!ret) {
1269 ret = -ENOENT;
1270 goto out_disable;
1271 } else if (ret < 0)
1272 goto out_disable;
1273 /* Just return zero, not the number of enabled functions */
1274 ret = 0;
1275 out:
1276 return ret;
1277
1278 out_disable:
1279 trace_event_enable_disable(event_enable_file, 0, 1);
1280 out_put:
1281 module_put(event_enable_file->event_call->mod);
1282 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -05001283 if (cmd_ops->set_filter)
1284 cmd_ops->set_filter(NULL, trigger_data, NULL);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001285 kfree(trigger_data);
1286 kfree(enable_data);
1287 goto out;
1288}
1289
1290static int event_enable_register_trigger(char *glob,
1291 struct event_trigger_ops *ops,
1292 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001293 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001294{
1295 struct enable_trigger_data *enable_data = data->private_data;
1296 struct enable_trigger_data *test_enable_data;
1297 struct event_trigger_data *test;
1298 int ret = 0;
1299
1300 list_for_each_entry_rcu(test, &file->triggers, list) {
1301 test_enable_data = test->private_data;
1302 if (test_enable_data &&
1303 (test_enable_data->file == enable_data->file)) {
1304 ret = -EEXIST;
1305 goto out;
1306 }
1307 }
1308
1309 if (data->ops->init) {
1310 ret = data->ops->init(data->ops, data);
1311 if (ret < 0)
1312 goto out;
1313 }
1314
1315 list_add_rcu(&data->list, &file->triggers);
1316 ret++;
1317
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001318 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001319 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1320 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001321 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001322 ret--;
1323 }
1324out:
1325 return ret;
1326}
1327
1328static void event_enable_unregister_trigger(char *glob,
1329 struct event_trigger_ops *ops,
1330 struct event_trigger_data *test,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001331 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001332{
1333 struct enable_trigger_data *test_enable_data = test->private_data;
1334 struct enable_trigger_data *enable_data;
1335 struct event_trigger_data *data;
1336 bool unregistered = false;
1337
1338 list_for_each_entry_rcu(data, &file->triggers, list) {
1339 enable_data = data->private_data;
1340 if (enable_data &&
1341 (enable_data->file == test_enable_data->file)) {
1342 unregistered = true;
1343 list_del_rcu(&data->list);
1344 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001345 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001346 break;
1347 }
1348 }
1349
1350 if (unregistered && data->ops->free)
1351 data->ops->free(data->ops, data);
1352}
1353
1354static struct event_trigger_ops *
1355event_enable_get_trigger_ops(char *cmd, char *param)
1356{
1357 struct event_trigger_ops *ops;
1358 bool enable;
1359
1360 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1361
1362 if (enable)
1363 ops = param ? &event_enable_count_trigger_ops :
1364 &event_enable_trigger_ops;
1365 else
1366 ops = param ? &event_disable_count_trigger_ops :
1367 &event_disable_trigger_ops;
1368
1369 return ops;
1370}
1371
1372static struct event_command trigger_enable_cmd = {
1373 .name = ENABLE_EVENT_STR,
1374 .trigger_type = ETT_EVENT_ENABLE,
1375 .func = event_enable_trigger_func,
1376 .reg = event_enable_register_trigger,
1377 .unreg = event_enable_unregister_trigger,
1378 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001379 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001380};
1381
1382static struct event_command trigger_disable_cmd = {
1383 .name = DISABLE_EVENT_STR,
1384 .trigger_type = ETT_EVENT_ENABLE,
1385 .func = event_enable_trigger_func,
1386 .reg = event_enable_register_trigger,
1387 .unreg = event_enable_unregister_trigger,
1388 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001389 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001390};
1391
1392static __init void unregister_trigger_enable_disable_cmds(void)
1393{
1394 unregister_event_command(&trigger_enable_cmd);
1395 unregister_event_command(&trigger_disable_cmd);
1396}
1397
1398static __init int register_trigger_enable_disable_cmds(void)
1399{
1400 int ret;
1401
1402 ret = register_event_command(&trigger_enable_cmd);
1403 if (WARN_ON(ret < 0))
1404 return ret;
1405 ret = register_event_command(&trigger_disable_cmd);
1406 if (WARN_ON(ret < 0))
1407 unregister_trigger_enable_disable_cmds();
1408
1409 return ret;
1410}
1411
Tom Zanussi2a2df322013-10-24 08:59:25 -05001412static __init int register_trigger_traceon_traceoff_cmds(void)
1413{
1414 int ret;
1415
1416 ret = register_event_command(&trigger_traceon_cmd);
1417 if (WARN_ON(ret < 0))
1418 return ret;
1419 ret = register_event_command(&trigger_traceoff_cmd);
1420 if (WARN_ON(ret < 0))
1421 unregister_trigger_traceon_traceoff_cmds();
1422
1423 return ret;
1424}
1425
Tom Zanussi85f2b082013-10-24 08:59:24 -05001426__init int register_trigger_cmds(void)
1427{
Tom Zanussi2a2df322013-10-24 08:59:25 -05001428 register_trigger_traceon_traceoff_cmds();
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001429 register_trigger_snapshot_cmd();
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001430 register_trigger_stacktrace_cmd();
Tom Zanussi7862ad12013-10-24 08:59:28 -05001431 register_trigger_enable_disable_cmds();
Tom Zanussi2a2df322013-10-24 08:59:25 -05001432
Tom Zanussi85f2b082013-10-24 08:59:24 -05001433 return 0;
1434}