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