blob: 8819944bbcbfb77b02d9ac163b7433b3043f8822 [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) {
Tom Zanussi104f2812015-12-10 12:50:47 -060075 if (data->paused)
76 continue;
Tom Zanussibac5fb92013-10-24 08:59:29 -050077 if (!rec) {
Tom Zanussic4a59232015-12-10 12:50:45 -060078 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -050079 continue;
80 }
Steven Rostedt (Red Hat)561a4fe2014-05-02 13:30:04 -040081 filter = rcu_dereference_sched(data->filter);
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -050082 if (filter && !filter_match_preds(filter, rec))
Tom Zanussibac5fb92013-10-24 08:59:29 -050083 continue;
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -050084 if (event_command_post_trigger(data->cmd_ops)) {
Tom Zanussibac5fb92013-10-24 08:59:29 -050085 tt |= data->cmd_ops->trigger_type;
86 continue;
87 }
Tom Zanussic4a59232015-12-10 12:50:45 -060088 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -050089 }
90 return tt;
91}
92EXPORT_SYMBOL_GPL(event_triggers_call);
93
94/**
95 * event_triggers_post_call - Call 'post_triggers' for a trace event
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040096 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050097 * @tt: enum event_trigger_type containing a set bit for each trigger to invoke
Tom Zanussic4a59232015-12-10 12:50:45 -060098 * @rec: The trace entry for the event
Tom Zanussibac5fb92013-10-24 08:59:29 -050099 *
100 * For each trigger associated with an event, invoke the trigger
101 * function registered with the associated trigger command, if the
102 * corresponding bit is set in the tt enum passed into this function.
103 * See @event_triggers_call for details on how those bits are set.
104 *
105 * Called from tracepoint handlers (with rcu_read_lock_sched() held).
106 */
107void
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400108event_triggers_post_call(struct trace_event_file *file,
Tom Zanussic4a59232015-12-10 12:50:45 -0600109 enum event_trigger_type tt,
110 void *rec)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500111{
112 struct event_trigger_data *data;
113
Tom Zanussibac5fb92013-10-24 08:59:29 -0500114 list_for_each_entry_rcu(data, &file->triggers, list) {
Tom Zanussi104f2812015-12-10 12:50:47 -0600115 if (data->paused)
116 continue;
Tom Zanussibac5fb92013-10-24 08:59:29 -0500117 if (data->cmd_ops->trigger_type & tt)
Tom Zanussic4a59232015-12-10 12:50:45 -0600118 data->ops->func(data, rec);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500119 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500120}
Tom Zanussibac5fb92013-10-24 08:59:29 -0500121EXPORT_SYMBOL_GPL(event_triggers_post_call);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500122
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500123#define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
124
Tom Zanussi85f2b082013-10-24 08:59:24 -0500125static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
126{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400127 struct trace_event_file *event_file = event_file_data(m->private);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500128
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500129 if (t == SHOW_AVAILABLE_TRIGGERS)
130 return NULL;
131
Tom Zanussi85f2b082013-10-24 08:59:24 -0500132 return seq_list_next(t, &event_file->triggers, pos);
133}
134
135static void *trigger_start(struct seq_file *m, loff_t *pos)
136{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400137 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500138
139 /* ->stop() is called even if ->start() fails */
140 mutex_lock(&event_mutex);
141 event_file = event_file_data(m->private);
142 if (unlikely(!event_file))
143 return ERR_PTR(-ENODEV);
144
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500145 if (list_empty(&event_file->triggers))
146 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
147
Tom Zanussi85f2b082013-10-24 08:59:24 -0500148 return seq_list_start(&event_file->triggers, *pos);
149}
150
151static void trigger_stop(struct seq_file *m, void *t)
152{
153 mutex_unlock(&event_mutex);
154}
155
156static int trigger_show(struct seq_file *m, void *v)
157{
158 struct event_trigger_data *data;
Steven Rostedt (Red Hat)dd97b952014-01-07 10:31:04 -0500159 struct event_command *p;
160
161 if (v == SHOW_AVAILABLE_TRIGGERS) {
162 seq_puts(m, "# Available triggers:\n");
163 seq_putc(m, '#');
164 mutex_lock(&trigger_cmd_mutex);
165 list_for_each_entry_reverse(p, &trigger_commands, list)
166 seq_printf(m, " %s", p->name);
167 seq_putc(m, '\n');
168 mutex_unlock(&trigger_cmd_mutex);
169 return 0;
170 }
Tom Zanussi85f2b082013-10-24 08:59:24 -0500171
172 data = list_entry(v, struct event_trigger_data, list);
173 data->ops->print(m, data->ops, data);
174
175 return 0;
176}
177
178static const struct seq_operations event_triggers_seq_ops = {
179 .start = trigger_start,
180 .next = trigger_next,
181 .stop = trigger_stop,
182 .show = trigger_show,
183};
184
185static int event_trigger_regex_open(struct inode *inode, struct file *file)
186{
187 int ret = 0;
188
189 mutex_lock(&event_mutex);
190
191 if (unlikely(!event_file_data(file))) {
192 mutex_unlock(&event_mutex);
193 return -ENODEV;
194 }
195
Tom Zanussia88e1cf2015-12-10 12:50:49 -0600196 if ((file->f_mode & FMODE_WRITE) &&
197 (file->f_flags & O_TRUNC)) {
198 struct trace_event_file *event_file;
199 struct event_command *p;
200
201 event_file = event_file_data(file);
202
203 list_for_each_entry(p, &trigger_commands, list) {
204 if (p->unreg_all)
205 p->unreg_all(event_file);
206 }
207 }
208
Tom Zanussi85f2b082013-10-24 08:59:24 -0500209 if (file->f_mode & FMODE_READ) {
210 ret = seq_open(file, &event_triggers_seq_ops);
211 if (!ret) {
212 struct seq_file *m = file->private_data;
213 m->private = file;
214 }
215 }
216
217 mutex_unlock(&event_mutex);
218
219 return ret;
220}
221
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400222static int trigger_process_regex(struct trace_event_file *file, char *buff)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500223{
224 char *command, *next = buff;
225 struct event_command *p;
226 int ret = -EINVAL;
227
228 command = strsep(&next, ": \t");
229 command = (command[0] != '!') ? command : command + 1;
230
231 mutex_lock(&trigger_cmd_mutex);
232 list_for_each_entry(p, &trigger_commands, list) {
233 if (strcmp(p->name, command) == 0) {
234 ret = p->func(p, file, buff, command, next);
235 goto out_unlock;
236 }
237 }
238 out_unlock:
239 mutex_unlock(&trigger_cmd_mutex);
240
241 return ret;
242}
243
244static ssize_t event_trigger_regex_write(struct file *file,
245 const char __user *ubuf,
246 size_t cnt, loff_t *ppos)
247{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400248 struct trace_event_file *event_file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500249 ssize_t ret;
250 char *buf;
251
252 if (!cnt)
253 return 0;
254
255 if (cnt >= PAGE_SIZE)
256 return -EINVAL;
257
Al Viro70f6cbb2015-12-24 00:13:10 -0500258 buf = memdup_user_nul(ubuf, cnt);
259 if (IS_ERR(buf))
260 return PTR_ERR(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500261
Tom Zanussi85f2b082013-10-24 08:59:24 -0500262 strim(buf);
263
264 mutex_lock(&event_mutex);
265 event_file = event_file_data(file);
266 if (unlikely(!event_file)) {
267 mutex_unlock(&event_mutex);
Al Viro70f6cbb2015-12-24 00:13:10 -0500268 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500269 return -ENODEV;
270 }
271 ret = trigger_process_regex(event_file, buf);
272 mutex_unlock(&event_mutex);
273
Al Viro70f6cbb2015-12-24 00:13:10 -0500274 kfree(buf);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500275 if (ret < 0)
276 goto out;
277
278 *ppos += cnt;
279 ret = cnt;
280 out:
281 return ret;
282}
283
284static int event_trigger_regex_release(struct inode *inode, struct file *file)
285{
286 mutex_lock(&event_mutex);
287
288 if (file->f_mode & FMODE_READ)
289 seq_release(inode, file);
290
291 mutex_unlock(&event_mutex);
292
293 return 0;
294}
295
296static ssize_t
297event_trigger_write(struct file *filp, const char __user *ubuf,
298 size_t cnt, loff_t *ppos)
299{
300 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
301}
302
303static int
304event_trigger_open(struct inode *inode, struct file *filp)
305{
306 return event_trigger_regex_open(inode, filp);
307}
308
309static int
310event_trigger_release(struct inode *inode, struct file *file)
311{
312 return event_trigger_regex_release(inode, file);
313}
314
315const struct file_operations event_trigger_fops = {
316 .open = event_trigger_open,
317 .read = seq_read,
318 .write = event_trigger_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -0500319 .llseek = tracing_lseek,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500320 .release = event_trigger_release,
321};
322
Tom Zanussi2a2df322013-10-24 08:59:25 -0500323/*
324 * Currently we only register event commands from __init, so mark this
325 * __init too.
326 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600327__init int register_event_command(struct event_command *cmd)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500328{
329 struct event_command *p;
330 int ret = 0;
331
332 mutex_lock(&trigger_cmd_mutex);
333 list_for_each_entry(p, &trigger_commands, list) {
334 if (strcmp(cmd->name, p->name) == 0) {
335 ret = -EBUSY;
336 goto out_unlock;
337 }
338 }
339 list_add(&cmd->list, &trigger_commands);
340 out_unlock:
341 mutex_unlock(&trigger_cmd_mutex);
342
343 return ret;
344}
345
346/*
347 * Currently we only unregister event commands from __init, so mark
348 * this __init too.
349 */
Tom Zanussid0bad492016-03-03 12:54:55 -0600350__init int unregister_event_command(struct event_command *cmd)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500351{
352 struct event_command *p, *n;
353 int ret = -ENODEV;
354
355 mutex_lock(&trigger_cmd_mutex);
356 list_for_each_entry_safe(p, n, &trigger_commands, list) {
357 if (strcmp(cmd->name, p->name) == 0) {
358 ret = 0;
359 list_del_init(&p->list);
360 goto out_unlock;
361 }
362 }
363 out_unlock:
364 mutex_unlock(&trigger_cmd_mutex);
365
366 return ret;
367}
368
369/**
370 * event_trigger_print - Generic event_trigger_ops @print implementation
371 * @name: The name of the event trigger
372 * @m: The seq_file being printed to
373 * @data: Trigger-specific data
374 * @filter_str: filter_str to print, if present
375 *
376 * Common implementation for event triggers to print themselves.
377 *
378 * Usually wrapped by a function that simply sets the @name of the
379 * trigger command and then invokes this.
380 *
381 * Return: 0 on success, errno otherwise
382 */
383static int
384event_trigger_print(const char *name, struct seq_file *m,
385 void *data, char *filter_str)
386{
387 long count = (long)data;
388
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +0100389 seq_puts(m, name);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500390
391 if (count == -1)
392 seq_puts(m, ":unlimited");
393 else
394 seq_printf(m, ":count=%ld", count);
395
396 if (filter_str)
397 seq_printf(m, " if %s\n", filter_str);
398 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +0100399 seq_putc(m, '\n');
Tom Zanussi2a2df322013-10-24 08:59:25 -0500400
401 return 0;
402}
403
404/**
405 * event_trigger_init - Generic event_trigger_ops @init implementation
406 * @ops: The trigger ops associated with the trigger
407 * @data: Trigger-specific data
408 *
409 * Common implementation of event trigger initialization.
410 *
411 * Usually used directly as the @init method in event trigger
412 * implementations.
413 *
414 * Return: 0 on success, errno otherwise
415 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600416int event_trigger_init(struct event_trigger_ops *ops,
417 struct event_trigger_data *data)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500418{
419 data->ref++;
420 return 0;
421}
422
423/**
424 * event_trigger_free - Generic event_trigger_ops @free implementation
425 * @ops: The trigger ops associated with the trigger
426 * @data: Trigger-specific data
427 *
428 * Common implementation of event trigger de-initialization.
429 *
430 * Usually used directly as the @free method in event trigger
431 * implementations.
432 */
433static void
434event_trigger_free(struct event_trigger_ops *ops,
435 struct event_trigger_data *data)
436{
437 if (WARN_ON_ONCE(data->ref <= 0))
438 return;
439
440 data->ref--;
441 if (!data->ref)
442 trigger_data_free(data);
443}
444
Tom Zanussiab4bf002015-12-10 12:50:44 -0600445int trace_event_trigger_enable_disable(struct trace_event_file *file,
446 int trigger_enable)
Tom Zanussi85f2b082013-10-24 08:59:24 -0500447{
448 int ret = 0;
449
450 if (trigger_enable) {
451 if (atomic_inc_return(&file->tm_ref) > 1)
452 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400453 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500454 ret = trace_event_enable_disable(file, 1, 1);
455 } else {
456 if (atomic_dec_return(&file->tm_ref) > 0)
457 return ret;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400458 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500459 ret = trace_event_enable_disable(file, 0, 1);
460 }
461
462 return ret;
463}
464
465/**
466 * clear_event_triggers - Clear all triggers associated with a trace array
467 * @tr: The trace array to clear
468 *
469 * For each trigger, the triggering event has its tm_ref decremented
470 * via trace_event_trigger_enable_disable(), and any associated event
471 * (in the case of enable/disable_event triggers) will have its sm_ref
472 * decremented via free()->trace_event_enable_disable(). That
473 * combination effectively reverses the soft-mode/trigger state added
474 * by trigger registration.
475 *
476 * Must be called with event_mutex held.
477 */
478void
479clear_event_triggers(struct trace_array *tr)
480{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400481 struct trace_event_file *file;
Tom Zanussi85f2b082013-10-24 08:59:24 -0500482
483 list_for_each_entry(file, &tr->events, list) {
Steven Rostedt (VMware)dfc80dc2018-05-27 20:54:44 -0400484 struct event_trigger_data *data, *n;
485 list_for_each_entry_safe(data, n, &file->triggers, list) {
Tom Zanussi85f2b082013-10-24 08:59:24 -0500486 trace_event_trigger_enable_disable(file, 0);
Steven Rostedt (VMware)dfc80dc2018-05-27 20:54:44 -0400487 list_del_rcu(&data->list);
Tom Zanussi85f2b082013-10-24 08:59:24 -0500488 if (data->ops->free)
489 data->ops->free(data->ops, data);
490 }
491 }
492}
493
Tom Zanussi2a2df322013-10-24 08:59:25 -0500494/**
Tom Zanussibac5fb92013-10-24 08:59:29 -0500495 * update_cond_flag - Set or reset the TRIGGER_COND bit
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400496 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -0500497 *
498 * If an event has triggers and any of those triggers has a filter or
499 * a post_trigger, trigger invocation needs to be deferred until after
500 * the current event has logged its data, and the event should have
501 * its TRIGGER_COND bit set, otherwise the TRIGGER_COND bit should be
502 * cleared.
503 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600504void update_cond_flag(struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -0500505{
506 struct event_trigger_data *data;
507 bool set_cond = false;
508
509 list_for_each_entry_rcu(data, &file->triggers, list) {
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -0500510 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
511 event_command_needs_rec(data->cmd_ops)) {
Tom Zanussibac5fb92013-10-24 08:59:29 -0500512 set_cond = true;
513 break;
514 }
515 }
516
517 if (set_cond)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400518 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500519 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400520 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500521}
522
523/**
Tom Zanussi2a2df322013-10-24 08:59:25 -0500524 * register_trigger - Generic event_command @reg implementation
525 * @glob: The raw string used to register the trigger
526 * @ops: The trigger ops associated with the trigger
527 * @data: Trigger-specific data to associate with the trigger
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400528 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500529 *
530 * Common implementation for event trigger registration.
531 *
532 * Usually used directly as the @reg method in event command
533 * implementations.
534 *
535 * Return: 0 on success, errno otherwise
536 */
537static int register_trigger(char *glob, struct event_trigger_ops *ops,
538 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400539 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500540{
541 struct event_trigger_data *test;
542 int ret = 0;
543
544 list_for_each_entry_rcu(test, &file->triggers, list) {
545 if (test->cmd_ops->trigger_type == data->cmd_ops->trigger_type) {
546 ret = -EEXIST;
547 goto out;
548 }
549 }
550
551 if (data->ops->init) {
552 ret = data->ops->init(data->ops, data);
553 if (ret < 0)
554 goto out;
555 }
556
557 list_add_rcu(&data->list, &file->triggers);
558 ret++;
559
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600560 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500561 if (trace_event_trigger_enable_disable(file, 1) < 0) {
562 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600563 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500564 ret--;
565 }
566out:
567 return ret;
568}
569
570/**
571 * unregister_trigger - Generic event_command @unreg implementation
572 * @glob: The raw string used to register the trigger
573 * @ops: The trigger ops associated with the trigger
574 * @test: Trigger-specific data used to find the trigger to remove
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400575 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500576 *
577 * Common implementation for event trigger unregistration.
578 *
579 * Usually used directly as the @unreg method in event command
580 * implementations.
581 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600582void unregister_trigger(char *glob, struct event_trigger_ops *ops,
583 struct event_trigger_data *test,
584 struct trace_event_file *file)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500585{
586 struct event_trigger_data *data;
587 bool unregistered = false;
588
589 list_for_each_entry_rcu(data, &file->triggers, list) {
590 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
591 unregistered = true;
592 list_del_rcu(&data->list);
593 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -0600594 update_cond_flag(file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500595 break;
596 }
597 }
598
599 if (unregistered && data->ops->free)
600 data->ops->free(data->ops, data);
601}
602
603/**
604 * event_trigger_callback - Generic event_command @func implementation
605 * @cmd_ops: The command ops, used for trigger registration
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400606 * @file: The trace_event_file associated with the event
Tom Zanussi2a2df322013-10-24 08:59:25 -0500607 * @glob: The raw string used to register the trigger
608 * @cmd: The cmd portion of the string used to register the trigger
609 * @param: The params portion of the string used to register the trigger
610 *
611 * Common implementation for event command parsing and trigger
612 * instantiation.
613 *
614 * Usually used directly as the @func method in event command
615 * implementations.
616 *
617 * Return: 0 on success, errno otherwise
618 */
619static int
620event_trigger_callback(struct event_command *cmd_ops,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400621 struct trace_event_file *file,
Tom Zanussi2a2df322013-10-24 08:59:25 -0500622 char *glob, char *cmd, char *param)
623{
624 struct event_trigger_data *trigger_data;
625 struct event_trigger_ops *trigger_ops;
626 char *trigger = NULL;
627 char *number;
628 int ret;
629
630 /* separate the trigger from the filter (t:n [if filter]) */
631 if (param && isdigit(param[0]))
632 trigger = strsep(&param, " \t");
633
634 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
635
636 ret = -ENOMEM;
637 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
638 if (!trigger_data)
639 goto out;
640
641 trigger_data->count = -1;
642 trigger_data->ops = trigger_ops;
643 trigger_data->cmd_ops = cmd_ops;
644 INIT_LIST_HEAD(&trigger_data->list);
Tom Zanussidb1388b2016-03-03 12:54:58 -0600645 INIT_LIST_HEAD(&trigger_data->named_list);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500646
647 if (glob[0] == '!') {
648 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
649 kfree(trigger_data);
650 ret = 0;
651 goto out;
652 }
653
654 if (trigger) {
655 number = strsep(&trigger, ":");
656
657 ret = -EINVAL;
658 if (!strlen(number))
659 goto out_free;
660
661 /*
662 * We use the callback data field (which is a pointer)
663 * as our counter.
664 */
665 ret = kstrtoul(number, 0, &trigger_data->count);
666 if (ret)
667 goto out_free;
668 }
669
670 if (!param) /* if param is non-empty, it's supposed to be a filter */
671 goto out_reg;
672
673 if (!cmd_ops->set_filter)
674 goto out_reg;
675
676 ret = cmd_ops->set_filter(param, trigger_data, file);
677 if (ret < 0)
678 goto out_free;
679
680 out_reg:
Steven Rostedt (VMware)2a0ce1f2018-07-24 19:13:31 -0400681 /* Up the trigger_data count to make sure reg doesn't free it on failure */
682 event_trigger_init(trigger_ops, trigger_data);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500683 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
684 /*
685 * The above returns on success the # of functions enabled,
686 * but if it didn't find any functions it returns zero.
687 * Consider no functions a failure too.
688 */
689 if (!ret) {
Steven Rostedt (VMware)2a0ce1f2018-07-24 19:13:31 -0400690 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500691 ret = -ENOENT;
Steven Rostedt (VMware)2a0ce1f2018-07-24 19:13:31 -0400692 } else if (ret > 0)
693 ret = 0;
694
695 /* Down the counter of trigger_data or free it if not used anymore */
696 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500697 out:
698 return ret;
699
700 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -0500701 if (cmd_ops->set_filter)
702 cmd_ops->set_filter(NULL, trigger_data, NULL);
Tom Zanussi2a2df322013-10-24 08:59:25 -0500703 kfree(trigger_data);
704 goto out;
705}
706
Tom Zanussibac5fb92013-10-24 08:59:29 -0500707/**
708 * set_trigger_filter - Generic event_command @set_filter implementation
709 * @filter_str: The filter string for the trigger, NULL to remove filter
710 * @trigger_data: Trigger-specific data
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400711 * @file: The trace_event_file associated with the event
Tom Zanussibac5fb92013-10-24 08:59:29 -0500712 *
713 * Common implementation for event command filter parsing and filter
714 * instantiation.
715 *
716 * Usually used directly as the @set_filter method in event command
717 * implementations.
718 *
719 * Also used to remove a filter (if filter_str = NULL).
720 *
721 * Return: 0 on success, errno otherwise
722 */
Tom Zanussiab4bf002015-12-10 12:50:44 -0600723int set_trigger_filter(char *filter_str,
724 struct event_trigger_data *trigger_data,
725 struct trace_event_file *file)
Tom Zanussibac5fb92013-10-24 08:59:29 -0500726{
727 struct event_trigger_data *data = trigger_data;
728 struct event_filter *filter = NULL, *tmp;
729 int ret = -EINVAL;
730 char *s;
731
732 if (!filter_str) /* clear the current filter */
733 goto assign;
734
735 s = strsep(&filter_str, " \t");
736
737 if (!strlen(s) || strcmp(s, "if") != 0)
738 goto out;
739
740 if (!filter_str)
741 goto out;
742
743 /* The filter is for the 'trigger' event, not the triggered event */
744 ret = create_event_filter(file->event_call, filter_str, false, &filter);
745 if (ret)
746 goto out;
747 assign:
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -0500748 tmp = rcu_access_pointer(data->filter);
Tom Zanussibac5fb92013-10-24 08:59:29 -0500749
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) {
Steven Rostedt (Red Hat)d8a30f22013-12-21 21:55:17 -0500764 free_event_filter(rcu_access_pointer(data->filter));
Tom Zanussibac5fb92013-10-24 08:59:29 -0500765 data->filter = NULL;
766 ret = -ENOMEM;
767 }
768 }
769 out:
770 return ret;
771}
772
Tom Zanussidb1388b2016-03-03 12:54:58 -0600773static LIST_HEAD(named_triggers);
774
775/**
776 * find_named_trigger - Find the common named trigger associated with @name
777 * @name: The name of the set of named triggers to find the common data for
778 *
779 * Named triggers are sets of triggers that share a common set of
780 * trigger data. The first named trigger registered with a given name
781 * owns the common trigger data that the others subsequently
782 * registered with the same name will reference. This function
783 * returns the common trigger data associated with that first
784 * registered instance.
785 *
786 * Return: the common trigger data for the given named trigger on
787 * success, NULL otherwise.
788 */
789struct event_trigger_data *find_named_trigger(const char *name)
790{
791 struct event_trigger_data *data;
792
793 if (!name)
794 return NULL;
795
796 list_for_each_entry(data, &named_triggers, named_list) {
797 if (data->named_data)
798 continue;
799 if (strcmp(data->name, name) == 0)
800 return data;
801 }
802
803 return NULL;
804}
805
806/**
807 * is_named_trigger - determine if a given trigger is a named trigger
808 * @test: The trigger data to test
809 *
810 * Return: true if 'test' is a named trigger, false otherwise.
811 */
812bool is_named_trigger(struct event_trigger_data *test)
813{
814 struct event_trigger_data *data;
815
816 list_for_each_entry(data, &named_triggers, named_list) {
817 if (test == data)
818 return true;
819 }
820
821 return false;
822}
823
824/**
825 * save_named_trigger - save the trigger in the named trigger list
826 * @name: The name of the named trigger set
827 * @data: The trigger data to save
828 *
829 * Return: 0 if successful, negative error otherwise.
830 */
831int save_named_trigger(const char *name, struct event_trigger_data *data)
832{
833 data->name = kstrdup(name, GFP_KERNEL);
834 if (!data->name)
835 return -ENOMEM;
836
837 list_add(&data->named_list, &named_triggers);
838
839 return 0;
840}
841
842/**
843 * del_named_trigger - delete a trigger from the named trigger list
844 * @data: The trigger data to delete
845 */
846void del_named_trigger(struct event_trigger_data *data)
847{
848 kfree(data->name);
849 data->name = NULL;
850
851 list_del(&data->named_list);
852}
853
854static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
855{
856 struct event_trigger_data *test;
857
858 list_for_each_entry(test, &named_triggers, named_list) {
859 if (strcmp(test->name, data->name) == 0) {
860 if (pause) {
861 test->paused_tmp = test->paused;
862 test->paused = true;
863 } else {
864 test->paused = test->paused_tmp;
865 }
866 }
867 }
868}
869
870/**
871 * pause_named_trigger - Pause all named triggers with the same name
872 * @data: The trigger data of a named trigger to pause
873 *
874 * Pauses a named trigger along with all other triggers having the
875 * same name. Because named triggers share a common set of data,
876 * pausing only one is meaningless, so pausing one named trigger needs
877 * to pause all triggers with the same name.
878 */
879void pause_named_trigger(struct event_trigger_data *data)
880{
881 __pause_named_trigger(data, true);
882}
883
884/**
885 * unpause_named_trigger - Un-pause all named triggers with the same name
886 * @data: The trigger data of a named trigger to unpause
887 *
888 * Un-pauses a named trigger along with all other triggers having the
889 * same name. Because named triggers share a common set of data,
890 * unpausing only one is meaningless, so unpausing one named trigger
891 * needs to unpause all triggers with the same name.
892 */
893void unpause_named_trigger(struct event_trigger_data *data)
894{
895 __pause_named_trigger(data, false);
896}
897
898/**
899 * set_named_trigger_data - Associate common named trigger data
900 * @data: The trigger data of a named trigger to unpause
901 *
902 * Named triggers are sets of triggers that share a common set of
903 * trigger data. The first named trigger registered with a given name
904 * owns the common trigger data that the others subsequently
905 * registered with the same name will reference. This function
906 * associates the common trigger data from the first trigger with the
907 * given trigger.
908 */
909void set_named_trigger_data(struct event_trigger_data *data,
910 struct event_trigger_data *named_data)
911{
912 data->named_data = named_data;
913}
914
Tom Zanussi2a2df322013-10-24 08:59:25 -0500915static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600916traceon_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500917{
918 if (tracing_is_on())
919 return;
920
921 tracing_on();
922}
923
924static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600925traceon_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500926{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500927 if (tracing_is_on())
928 return;
929
Tom Zanussi2a2df322013-10-24 08:59:25 -0500930 if (!data->count)
931 return;
932
933 if (data->count != -1)
934 (data->count)--;
935
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500936 tracing_on();
Tom Zanussi2a2df322013-10-24 08:59:25 -0500937}
938
939static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600940traceoff_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500941{
942 if (!tracing_is_on())
943 return;
944
945 tracing_off();
946}
947
948static void
Tom Zanussic4a59232015-12-10 12:50:45 -0600949traceoff_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi2a2df322013-10-24 08:59:25 -0500950{
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500951 if (!tracing_is_on())
952 return;
953
Tom Zanussi2a2df322013-10-24 08:59:25 -0500954 if (!data->count)
955 return;
956
957 if (data->count != -1)
958 (data->count)--;
959
Steven Rostedt (Red Hat)e8dc6372014-01-06 22:25:50 -0500960 tracing_off();
Tom Zanussi2a2df322013-10-24 08:59:25 -0500961}
962
963static int
964traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
965 struct event_trigger_data *data)
966{
967 return event_trigger_print("traceon", m, (void *)data->count,
968 data->filter_str);
969}
970
971static int
972traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
973 struct event_trigger_data *data)
974{
975 return event_trigger_print("traceoff", m, (void *)data->count,
976 data->filter_str);
977}
978
979static struct event_trigger_ops traceon_trigger_ops = {
980 .func = traceon_trigger,
981 .print = traceon_trigger_print,
982 .init = event_trigger_init,
983 .free = event_trigger_free,
984};
985
986static struct event_trigger_ops traceon_count_trigger_ops = {
987 .func = traceon_count_trigger,
988 .print = traceon_trigger_print,
989 .init = event_trigger_init,
990 .free = event_trigger_free,
991};
992
993static struct event_trigger_ops traceoff_trigger_ops = {
994 .func = traceoff_trigger,
995 .print = traceoff_trigger_print,
996 .init = event_trigger_init,
997 .free = event_trigger_free,
998};
999
1000static struct event_trigger_ops traceoff_count_trigger_ops = {
1001 .func = traceoff_count_trigger,
1002 .print = traceoff_trigger_print,
1003 .init = event_trigger_init,
1004 .free = event_trigger_free,
1005};
1006
1007static struct event_trigger_ops *
1008onoff_get_trigger_ops(char *cmd, char *param)
1009{
1010 struct event_trigger_ops *ops;
1011
1012 /* we register both traceon and traceoff to this callback */
1013 if (strcmp(cmd, "traceon") == 0)
1014 ops = param ? &traceon_count_trigger_ops :
1015 &traceon_trigger_ops;
1016 else
1017 ops = param ? &traceoff_count_trigger_ops :
1018 &traceoff_trigger_ops;
1019
1020 return ops;
1021}
1022
1023static struct event_command trigger_traceon_cmd = {
1024 .name = "traceon",
1025 .trigger_type = ETT_TRACE_ONOFF,
1026 .func = event_trigger_callback,
1027 .reg = register_trigger,
1028 .unreg = unregister_trigger,
1029 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001030 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001031};
1032
1033static struct event_command trigger_traceoff_cmd = {
1034 .name = "traceoff",
1035 .trigger_type = ETT_TRACE_ONOFF,
Masami Hiramatsua0d0c622016-09-09 01:05:45 +09001036 .flags = EVENT_CMD_FL_POST_TRIGGER,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001037 .func = event_trigger_callback,
1038 .reg = register_trigger,
1039 .unreg = unregister_trigger,
1040 .get_trigger_ops = onoff_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001041 .set_filter = set_trigger_filter,
Tom Zanussi2a2df322013-10-24 08:59:25 -05001042};
1043
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001044#ifdef CONFIG_TRACER_SNAPSHOT
1045static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001046snapshot_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001047{
1048 tracing_snapshot();
1049}
1050
1051static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001052snapshot_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001053{
1054 if (!data->count)
1055 return;
1056
1057 if (data->count != -1)
1058 (data->count)--;
1059
Tom Zanussic4a59232015-12-10 12:50:45 -06001060 snapshot_trigger(data, rec);
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001061}
1062
1063static int
1064register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1065 struct event_trigger_data *data,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001066 struct trace_event_file *file)
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001067{
1068 int ret = register_trigger(glob, ops, data, file);
1069
1070 if (ret > 0 && tracing_alloc_snapshot() != 0) {
1071 unregister_trigger(glob, ops, data, file);
1072 ret = 0;
1073 }
1074
1075 return ret;
1076}
1077
1078static int
1079snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1080 struct event_trigger_data *data)
1081{
1082 return event_trigger_print("snapshot", m, (void *)data->count,
1083 data->filter_str);
1084}
1085
1086static struct event_trigger_ops snapshot_trigger_ops = {
1087 .func = snapshot_trigger,
1088 .print = snapshot_trigger_print,
1089 .init = event_trigger_init,
1090 .free = event_trigger_free,
1091};
1092
1093static struct event_trigger_ops snapshot_count_trigger_ops = {
1094 .func = snapshot_count_trigger,
1095 .print = snapshot_trigger_print,
1096 .init = event_trigger_init,
1097 .free = event_trigger_free,
1098};
1099
1100static struct event_trigger_ops *
1101snapshot_get_trigger_ops(char *cmd, char *param)
1102{
1103 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1104}
1105
1106static struct event_command trigger_snapshot_cmd = {
1107 .name = "snapshot",
1108 .trigger_type = ETT_SNAPSHOT,
1109 .func = event_trigger_callback,
1110 .reg = register_snapshot_trigger,
1111 .unreg = unregister_trigger,
1112 .get_trigger_ops = snapshot_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001113 .set_filter = set_trigger_filter,
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001114};
1115
1116static __init int register_trigger_snapshot_cmd(void)
1117{
1118 int ret;
1119
1120 ret = register_event_command(&trigger_snapshot_cmd);
1121 WARN_ON(ret < 0);
1122
1123 return ret;
1124}
1125#else
1126static __init int register_trigger_snapshot_cmd(void) { return 0; }
1127#endif /* CONFIG_TRACER_SNAPSHOT */
1128
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001129#ifdef CONFIG_STACKTRACE
1130/*
1131 * Skip 3:
1132 * stacktrace_trigger()
1133 * event_triggers_post_call()
Steven Rostedt (Red Hat)a7237762015-05-13 15:27:47 -04001134 * trace_event_raw_event_xxx()
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001135 */
1136#define STACK_SKIP 3
1137
1138static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001139stacktrace_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001140{
1141 trace_dump_stack(STACK_SKIP);
1142}
1143
1144static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001145stacktrace_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001146{
1147 if (!data->count)
1148 return;
1149
1150 if (data->count != -1)
1151 (data->count)--;
1152
Tom Zanussic4a59232015-12-10 12:50:45 -06001153 stacktrace_trigger(data, rec);
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001154}
1155
1156static int
1157stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1158 struct event_trigger_data *data)
1159{
1160 return event_trigger_print("stacktrace", m, (void *)data->count,
1161 data->filter_str);
1162}
1163
1164static struct event_trigger_ops stacktrace_trigger_ops = {
1165 .func = stacktrace_trigger,
1166 .print = stacktrace_trigger_print,
1167 .init = event_trigger_init,
1168 .free = event_trigger_free,
1169};
1170
1171static struct event_trigger_ops stacktrace_count_trigger_ops = {
1172 .func = stacktrace_count_trigger,
1173 .print = stacktrace_trigger_print,
1174 .init = event_trigger_init,
1175 .free = event_trigger_free,
1176};
1177
1178static struct event_trigger_ops *
1179stacktrace_get_trigger_ops(char *cmd, char *param)
1180{
1181 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1182}
1183
1184static struct event_command trigger_stacktrace_cmd = {
1185 .name = "stacktrace",
1186 .trigger_type = ETT_STACKTRACE,
Steven Rostedt (Red Hat)353206f2016-02-22 15:55:09 -05001187 .flags = EVENT_CMD_FL_POST_TRIGGER,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001188 .func = event_trigger_callback,
1189 .reg = register_trigger,
1190 .unreg = unregister_trigger,
1191 .get_trigger_ops = stacktrace_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001192 .set_filter = set_trigger_filter,
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001193};
1194
1195static __init int register_trigger_stacktrace_cmd(void)
1196{
1197 int ret;
1198
1199 ret = register_event_command(&trigger_stacktrace_cmd);
1200 WARN_ON(ret < 0);
1201
1202 return ret;
1203}
1204#else
1205static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1206#endif /* CONFIG_STACKTRACE */
1207
Tom Zanussi2a2df322013-10-24 08:59:25 -05001208static __init void unregister_trigger_traceon_traceoff_cmds(void)
1209{
1210 unregister_event_command(&trigger_traceon_cmd);
1211 unregister_event_command(&trigger_traceoff_cmd);
1212}
1213
Tom Zanussi7862ad12013-10-24 08:59:28 -05001214static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001215event_enable_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001216{
1217 struct enable_trigger_data *enable_data = data->private_data;
1218
1219 if (enable_data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001220 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001221 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001222 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001223}
1224
1225static void
Tom Zanussic4a59232015-12-10 12:50:45 -06001226event_enable_count_trigger(struct event_trigger_data *data, void *rec)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001227{
1228 struct enable_trigger_data *enable_data = data->private_data;
1229
1230 if (!data->count)
1231 return;
1232
1233 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001234 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussi7862ad12013-10-24 08:59:28 -05001235 return;
1236
1237 if (data->count != -1)
1238 (data->count)--;
1239
Tom Zanussic4a59232015-12-10 12:50:45 -06001240 event_enable_trigger(data, rec);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001241}
1242
Tom Zanussid0bad492016-03-03 12:54:55 -06001243int event_enable_trigger_print(struct seq_file *m,
1244 struct event_trigger_ops *ops,
1245 struct event_trigger_data *data)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001246{
1247 struct enable_trigger_data *enable_data = data->private_data;
1248
1249 seq_printf(m, "%s:%s:%s",
Tom Zanussid0bad492016-03-03 12:54:55 -06001250 enable_data->hist ?
1251 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1252 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
Tom Zanussi7862ad12013-10-24 08:59:28 -05001253 enable_data->file->event_call->class->system,
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001254 trace_event_name(enable_data->file->event_call));
Tom Zanussi7862ad12013-10-24 08:59:28 -05001255
1256 if (data->count == -1)
1257 seq_puts(m, ":unlimited");
1258 else
1259 seq_printf(m, ":count=%ld", data->count);
1260
1261 if (data->filter_str)
1262 seq_printf(m, " if %s\n", data->filter_str);
1263 else
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001264 seq_putc(m, '\n');
Tom Zanussi7862ad12013-10-24 08:59:28 -05001265
1266 return 0;
1267}
1268
Tom Zanussid0bad492016-03-03 12:54:55 -06001269void event_enable_trigger_free(struct event_trigger_ops *ops,
1270 struct event_trigger_data *data)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001271{
1272 struct enable_trigger_data *enable_data = data->private_data;
1273
1274 if (WARN_ON_ONCE(data->ref <= 0))
1275 return;
1276
1277 data->ref--;
1278 if (!data->ref) {
1279 /* Remove the SOFT_MODE flag */
1280 trace_event_enable_disable(enable_data->file, 0, 1);
1281 module_put(enable_data->file->event_call->mod);
1282 trigger_data_free(data);
1283 kfree(enable_data);
1284 }
1285}
1286
1287static struct event_trigger_ops event_enable_trigger_ops = {
1288 .func = event_enable_trigger,
1289 .print = event_enable_trigger_print,
1290 .init = event_trigger_init,
1291 .free = event_enable_trigger_free,
1292};
1293
1294static struct event_trigger_ops event_enable_count_trigger_ops = {
1295 .func = event_enable_count_trigger,
1296 .print = event_enable_trigger_print,
1297 .init = event_trigger_init,
1298 .free = event_enable_trigger_free,
1299};
1300
1301static struct event_trigger_ops event_disable_trigger_ops = {
1302 .func = event_enable_trigger,
1303 .print = event_enable_trigger_print,
1304 .init = event_trigger_init,
1305 .free = event_enable_trigger_free,
1306};
1307
1308static struct event_trigger_ops event_disable_count_trigger_ops = {
1309 .func = event_enable_count_trigger,
1310 .print = event_enable_trigger_print,
1311 .init = event_trigger_init,
1312 .free = event_enable_trigger_free,
1313};
1314
Tom Zanussid0bad492016-03-03 12:54:55 -06001315int event_enable_trigger_func(struct event_command *cmd_ops,
1316 struct trace_event_file *file,
1317 char *glob, char *cmd, char *param)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001318{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001319 struct trace_event_file *event_enable_file;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001320 struct enable_trigger_data *enable_data;
1321 struct event_trigger_data *trigger_data;
1322 struct event_trigger_ops *trigger_ops;
1323 struct trace_array *tr = file->tr;
1324 const char *system;
1325 const char *event;
Tom Zanussid0bad492016-03-03 12:54:55 -06001326 bool hist = false;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001327 char *trigger;
1328 char *number;
1329 bool enable;
1330 int ret;
1331
1332 if (!param)
1333 return -EINVAL;
1334
1335 /* separate the trigger from the filter (s:e:n [if filter]) */
1336 trigger = strsep(&param, " \t");
1337 if (!trigger)
1338 return -EINVAL;
1339
1340 system = strsep(&trigger, ":");
1341 if (!trigger)
1342 return -EINVAL;
1343
1344 event = strsep(&trigger, ":");
1345
1346 ret = -EINVAL;
1347 event_enable_file = find_event_file(tr, system, event);
1348 if (!event_enable_file)
1349 goto out;
1350
Tom Zanussid0bad492016-03-03 12:54:55 -06001351#ifdef CONFIG_HIST_TRIGGERS
1352 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1353 (strcmp(cmd, DISABLE_HIST_STR) == 0));
Tom Zanussi7862ad12013-10-24 08:59:28 -05001354
Tom Zanussid0bad492016-03-03 12:54:55 -06001355 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1356 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1357#else
1358 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1359#endif
Tom Zanussi7862ad12013-10-24 08:59:28 -05001360 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1361
1362 ret = -ENOMEM;
1363 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1364 if (!trigger_data)
1365 goto out;
1366
1367 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1368 if (!enable_data) {
1369 kfree(trigger_data);
1370 goto out;
1371 }
1372
1373 trigger_data->count = -1;
1374 trigger_data->ops = trigger_ops;
1375 trigger_data->cmd_ops = cmd_ops;
1376 INIT_LIST_HEAD(&trigger_data->list);
1377 RCU_INIT_POINTER(trigger_data->filter, NULL);
1378
Tom Zanussid0bad492016-03-03 12:54:55 -06001379 enable_data->hist = hist;
Tom Zanussi7862ad12013-10-24 08:59:28 -05001380 enable_data->enable = enable;
1381 enable_data->file = event_enable_file;
1382 trigger_data->private_data = enable_data;
1383
1384 if (glob[0] == '!') {
1385 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1386 kfree(trigger_data);
1387 kfree(enable_data);
1388 ret = 0;
1389 goto out;
1390 }
1391
Steven Rostedt (VMware)a9737bb2018-07-25 16:02:06 -04001392 /* Up the trigger_data count to make sure nothing frees it on failure */
1393 event_trigger_init(trigger_ops, trigger_data);
1394
Tom Zanussi7862ad12013-10-24 08:59:28 -05001395 if (trigger) {
1396 number = strsep(&trigger, ":");
1397
1398 ret = -EINVAL;
1399 if (!strlen(number))
1400 goto out_free;
1401
1402 /*
1403 * We use the callback data field (which is a pointer)
1404 * as our counter.
1405 */
1406 ret = kstrtoul(number, 0, &trigger_data->count);
1407 if (ret)
1408 goto out_free;
1409 }
1410
1411 if (!param) /* if param is non-empty, it's supposed to be a filter */
1412 goto out_reg;
1413
1414 if (!cmd_ops->set_filter)
1415 goto out_reg;
1416
1417 ret = cmd_ops->set_filter(param, trigger_data, file);
1418 if (ret < 0)
1419 goto out_free;
1420
1421 out_reg:
1422 /* Don't let event modules unload while probe registered */
1423 ret = try_module_get(event_enable_file->event_call->mod);
1424 if (!ret) {
1425 ret = -EBUSY;
1426 goto out_free;
1427 }
1428
1429 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1430 if (ret < 0)
1431 goto out_put;
1432 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1433 /*
1434 * The above returns on success the # of functions enabled,
1435 * but if it didn't find any functions it returns zero.
1436 * Consider no functions a failure too.
1437 */
1438 if (!ret) {
1439 ret = -ENOENT;
1440 goto out_disable;
1441 } else if (ret < 0)
1442 goto out_disable;
1443 /* Just return zero, not the number of enabled functions */
1444 ret = 0;
Steven Rostedt (VMware)a9737bb2018-07-25 16:02:06 -04001445 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001446 out:
1447 return ret;
1448
1449 out_disable:
1450 trace_event_enable_disable(event_enable_file, 0, 1);
1451 out_put:
1452 module_put(event_enable_file->event_call->mod);
1453 out_free:
Tom Zanussibac5fb92013-10-24 08:59:29 -05001454 if (cmd_ops->set_filter)
1455 cmd_ops->set_filter(NULL, trigger_data, NULL);
Steven Rostedt (VMware)a9737bb2018-07-25 16:02:06 -04001456 event_trigger_free(trigger_ops, trigger_data);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001457 kfree(enable_data);
1458 goto out;
1459}
1460
Tom Zanussid0bad492016-03-03 12:54:55 -06001461int event_enable_register_trigger(char *glob,
1462 struct event_trigger_ops *ops,
1463 struct event_trigger_data *data,
1464 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001465{
1466 struct enable_trigger_data *enable_data = data->private_data;
1467 struct enable_trigger_data *test_enable_data;
1468 struct event_trigger_data *test;
1469 int ret = 0;
1470
1471 list_for_each_entry_rcu(test, &file->triggers, list) {
1472 test_enable_data = test->private_data;
1473 if (test_enable_data &&
Tom Zanussid0bad492016-03-03 12:54:55 -06001474 (test->cmd_ops->trigger_type ==
1475 data->cmd_ops->trigger_type) &&
Tom Zanussi7862ad12013-10-24 08:59:28 -05001476 (test_enable_data->file == enable_data->file)) {
1477 ret = -EEXIST;
1478 goto out;
1479 }
1480 }
1481
1482 if (data->ops->init) {
1483 ret = data->ops->init(data->ops, data);
1484 if (ret < 0)
1485 goto out;
1486 }
1487
1488 list_add_rcu(&data->list, &file->triggers);
1489 ret++;
1490
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001491 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001492 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1493 list_del_rcu(&data->list);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001494 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001495 ret--;
1496 }
1497out:
1498 return ret;
1499}
1500
Tom Zanussid0bad492016-03-03 12:54:55 -06001501void event_enable_unregister_trigger(char *glob,
1502 struct event_trigger_ops *ops,
1503 struct event_trigger_data *test,
1504 struct trace_event_file *file)
Tom Zanussi7862ad12013-10-24 08:59:28 -05001505{
1506 struct enable_trigger_data *test_enable_data = test->private_data;
1507 struct enable_trigger_data *enable_data;
1508 struct event_trigger_data *data;
1509 bool unregistered = false;
1510
1511 list_for_each_entry_rcu(data, &file->triggers, list) {
1512 enable_data = data->private_data;
1513 if (enable_data &&
Tom Zanussid0bad492016-03-03 12:54:55 -06001514 (data->cmd_ops->trigger_type ==
1515 test->cmd_ops->trigger_type) &&
Tom Zanussi7862ad12013-10-24 08:59:28 -05001516 (enable_data->file == test_enable_data->file)) {
1517 unregistered = true;
1518 list_del_rcu(&data->list);
1519 trace_event_trigger_enable_disable(file, 0);
Tom Zanussi4e4a4d72015-11-23 13:51:16 -06001520 update_cond_flag(file);
Tom Zanussi7862ad12013-10-24 08:59:28 -05001521 break;
1522 }
1523 }
1524
1525 if (unregistered && data->ops->free)
1526 data->ops->free(data->ops, data);
1527}
1528
1529static struct event_trigger_ops *
1530event_enable_get_trigger_ops(char *cmd, char *param)
1531{
1532 struct event_trigger_ops *ops;
1533 bool enable;
1534
Tom Zanussid0bad492016-03-03 12:54:55 -06001535#ifdef CONFIG_HIST_TRIGGERS
1536 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1537 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1538#else
Tom Zanussi7862ad12013-10-24 08:59:28 -05001539 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
Tom Zanussid0bad492016-03-03 12:54:55 -06001540#endif
Tom Zanussi7862ad12013-10-24 08:59:28 -05001541 if (enable)
1542 ops = param ? &event_enable_count_trigger_ops :
1543 &event_enable_trigger_ops;
1544 else
1545 ops = param ? &event_disable_count_trigger_ops :
1546 &event_disable_trigger_ops;
1547
1548 return ops;
1549}
1550
1551static struct event_command trigger_enable_cmd = {
1552 .name = ENABLE_EVENT_STR,
1553 .trigger_type = ETT_EVENT_ENABLE,
1554 .func = event_enable_trigger_func,
1555 .reg = event_enable_register_trigger,
1556 .unreg = event_enable_unregister_trigger,
1557 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001558 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001559};
1560
1561static struct event_command trigger_disable_cmd = {
1562 .name = DISABLE_EVENT_STR,
1563 .trigger_type = ETT_EVENT_ENABLE,
1564 .func = event_enable_trigger_func,
1565 .reg = event_enable_register_trigger,
1566 .unreg = event_enable_unregister_trigger,
1567 .get_trigger_ops = event_enable_get_trigger_ops,
Tom Zanussibac5fb92013-10-24 08:59:29 -05001568 .set_filter = set_trigger_filter,
Tom Zanussi7862ad12013-10-24 08:59:28 -05001569};
1570
1571static __init void unregister_trigger_enable_disable_cmds(void)
1572{
1573 unregister_event_command(&trigger_enable_cmd);
1574 unregister_event_command(&trigger_disable_cmd);
1575}
1576
1577static __init int register_trigger_enable_disable_cmds(void)
1578{
1579 int ret;
1580
1581 ret = register_event_command(&trigger_enable_cmd);
1582 if (WARN_ON(ret < 0))
1583 return ret;
1584 ret = register_event_command(&trigger_disable_cmd);
1585 if (WARN_ON(ret < 0))
1586 unregister_trigger_enable_disable_cmds();
1587
1588 return ret;
1589}
1590
Tom Zanussi2a2df322013-10-24 08:59:25 -05001591static __init int register_trigger_traceon_traceoff_cmds(void)
1592{
1593 int ret;
1594
1595 ret = register_event_command(&trigger_traceon_cmd);
1596 if (WARN_ON(ret < 0))
1597 return ret;
1598 ret = register_event_command(&trigger_traceoff_cmd);
1599 if (WARN_ON(ret < 0))
1600 unregister_trigger_traceon_traceoff_cmds();
1601
1602 return ret;
1603}
1604
Tom Zanussi85f2b082013-10-24 08:59:24 -05001605__init int register_trigger_cmds(void)
1606{
Tom Zanussi2a2df322013-10-24 08:59:25 -05001607 register_trigger_traceon_traceoff_cmds();
Tom Zanussi93e31ff2013-10-24 08:59:26 -05001608 register_trigger_snapshot_cmd();
Tom Zanussif21ecbb2013-10-24 08:59:27 -05001609 register_trigger_stacktrace_cmd();
Tom Zanussi7862ad12013-10-24 08:59:28 -05001610 register_trigger_enable_disable_cmds();
Tom Zanussid0bad492016-03-03 12:54:55 -06001611 register_trigger_hist_enable_disable_cmds();
Tom Zanussi7ef224d2016-03-03 12:54:42 -06001612 register_trigger_hist_cmd();
Tom Zanussi2a2df322013-10-24 08:59:25 -05001613
Tom Zanussi85f2b082013-10-24 08:59:24 -05001614 return 0;
1615}