This source file includes following definitions.
- trigger_data_free
- event_triggers_call
- event_triggers_post_call
- trigger_next
- trigger_start
- trigger_stop
- trigger_show
- event_trigger_regex_open
- trigger_process_regex
- event_trigger_regex_write
- event_trigger_regex_release
- event_trigger_write
- event_trigger_open
- event_trigger_release
- register_event_command
- unregister_event_command
- event_trigger_print
- event_trigger_init
- event_trigger_free
- trace_event_trigger_enable_disable
- clear_event_triggers
- update_cond_flag
- register_trigger
- unregister_trigger
- event_trigger_callback
- set_trigger_filter
- find_named_trigger
- is_named_trigger
- save_named_trigger
- del_named_trigger
- __pause_named_trigger
- pause_named_trigger
- unpause_named_trigger
- set_named_trigger_data
- get_named_trigger_data
- traceon_trigger
- traceon_count_trigger
- traceoff_trigger
- traceoff_count_trigger
- traceon_trigger_print
- traceoff_trigger_print
- onoff_get_trigger_ops
- snapshot_trigger
- snapshot_count_trigger
- register_snapshot_trigger
- snapshot_trigger_print
- snapshot_get_trigger_ops
- register_trigger_snapshot_cmd
- register_trigger_snapshot_cmd
- stacktrace_trigger
- stacktrace_count_trigger
- stacktrace_trigger_print
- stacktrace_get_trigger_ops
- register_trigger_stacktrace_cmd
- register_trigger_stacktrace_cmd
- unregister_trigger_traceon_traceoff_cmds
- event_enable_trigger
- event_enable_count_trigger
- event_enable_trigger_print
- event_enable_trigger_free
- event_enable_trigger_func
- event_enable_register_trigger
- event_enable_unregister_trigger
- event_enable_get_trigger_ops
- unregister_trigger_enable_disable_cmds
- register_trigger_enable_disable_cmds
- register_trigger_traceon_traceoff_cmds
- register_trigger_cmds
1
2
3
4
5
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
26 tracepoint_synchronize_unregister();
27
28 kfree(data);
29 }
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 enum event_trigger_type
56 event_triggers_call(struct trace_event_file *file, void *rec,
57 struct ring_buffer_event *event)
58 {
59 struct event_trigger_data *data;
60 enum event_trigger_type tt = ETT_NONE;
61 struct event_filter *filter;
62
63 if (list_empty(&file->triggers))
64 return tt;
65
66 list_for_each_entry_rcu(data, &file->triggers, list) {
67 if (data->paused)
68 continue;
69 if (!rec) {
70 data->ops->func(data, rec, event);
71 continue;
72 }
73 filter = rcu_dereference_sched(data->filter);
74 if (filter && !filter_match_preds(filter, rec))
75 continue;
76 if (event_command_post_trigger(data->cmd_ops)) {
77 tt |= data->cmd_ops->trigger_type;
78 continue;
79 }
80 data->ops->func(data, rec, event);
81 }
82 return tt;
83 }
84 EXPORT_SYMBOL_GPL(event_triggers_call);
85
86
87
88
89
90
91
92
93
94
95
96
97
98 void
99 event_triggers_post_call(struct trace_event_file *file,
100 enum event_trigger_type tt)
101 {
102 struct event_trigger_data *data;
103
104 list_for_each_entry_rcu(data, &file->triggers, list) {
105 if (data->paused)
106 continue;
107 if (data->cmd_ops->trigger_type & tt)
108 data->ops->func(data, NULL, NULL);
109 }
110 }
111 EXPORT_SYMBOL_GPL(event_triggers_post_call);
112
113 #define SHOW_AVAILABLE_TRIGGERS (void *)(1UL)
114
115 static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
116 {
117 struct trace_event_file *event_file = event_file_data(m->private);
118
119 if (t == SHOW_AVAILABLE_TRIGGERS) {
120 (*pos)++;
121 return NULL;
122 }
123 return seq_list_next(t, &event_file->triggers, pos);
124 }
125
126 static void *trigger_start(struct seq_file *m, loff_t *pos)
127 {
128 struct trace_event_file *event_file;
129
130
131 mutex_lock(&event_mutex);
132 event_file = event_file_data(m->private);
133 if (unlikely(!event_file))
134 return ERR_PTR(-ENODEV);
135
136 if (list_empty(&event_file->triggers))
137 return *pos == 0 ? SHOW_AVAILABLE_TRIGGERS : NULL;
138
139 return seq_list_start(&event_file->triggers, *pos);
140 }
141
142 static void trigger_stop(struct seq_file *m, void *t)
143 {
144 mutex_unlock(&event_mutex);
145 }
146
147 static int trigger_show(struct seq_file *m, void *v)
148 {
149 struct event_trigger_data *data;
150 struct event_command *p;
151
152 if (v == SHOW_AVAILABLE_TRIGGERS) {
153 seq_puts(m, "# Available triggers:\n");
154 seq_putc(m, '#');
155 mutex_lock(&trigger_cmd_mutex);
156 list_for_each_entry_reverse(p, &trigger_commands, list)
157 seq_printf(m, " %s", p->name);
158 seq_putc(m, '\n');
159 mutex_unlock(&trigger_cmd_mutex);
160 return 0;
161 }
162
163 data = list_entry(v, struct event_trigger_data, list);
164 data->ops->print(m, data->ops, data);
165
166 return 0;
167 }
168
169 static const struct seq_operations event_triggers_seq_ops = {
170 .start = trigger_start,
171 .next = trigger_next,
172 .stop = trigger_stop,
173 .show = trigger_show,
174 };
175
176 static int event_trigger_regex_open(struct inode *inode, struct file *file)
177 {
178 int ret;
179
180 ret = security_locked_down(LOCKDOWN_TRACEFS);
181 if (ret)
182 return ret;
183
184 mutex_lock(&event_mutex);
185
186 if (unlikely(!event_file_data(file))) {
187 mutex_unlock(&event_mutex);
188 return -ENODEV;
189 }
190
191 if ((file->f_mode & FMODE_WRITE) &&
192 (file->f_flags & O_TRUNC)) {
193 struct trace_event_file *event_file;
194 struct event_command *p;
195
196 event_file = event_file_data(file);
197
198 list_for_each_entry(p, &trigger_commands, list) {
199 if (p->unreg_all)
200 p->unreg_all(event_file);
201 }
202 }
203
204 if (file->f_mode & FMODE_READ) {
205 ret = seq_open(file, &event_triggers_seq_ops);
206 if (!ret) {
207 struct seq_file *m = file->private_data;
208 m->private = file;
209 }
210 }
211
212 mutex_unlock(&event_mutex);
213
214 return ret;
215 }
216
217 static int trigger_process_regex(struct trace_event_file *file, char *buff)
218 {
219 char *command, *next = buff;
220 struct event_command *p;
221 int ret = -EINVAL;
222
223 command = strsep(&next, ": \t");
224 command = (command[0] != '!') ? command : command + 1;
225
226 mutex_lock(&trigger_cmd_mutex);
227 list_for_each_entry(p, &trigger_commands, list) {
228 if (strcmp(p->name, command) == 0) {
229 ret = p->func(p, file, buff, command, next);
230 goto out_unlock;
231 }
232 }
233 out_unlock:
234 mutex_unlock(&trigger_cmd_mutex);
235
236 return ret;
237 }
238
239 static ssize_t event_trigger_regex_write(struct file *file,
240 const char __user *ubuf,
241 size_t cnt, loff_t *ppos)
242 {
243 struct trace_event_file *event_file;
244 ssize_t ret;
245 char *buf;
246
247 if (!cnt)
248 return 0;
249
250 if (cnt >= PAGE_SIZE)
251 return -EINVAL;
252
253 buf = memdup_user_nul(ubuf, cnt);
254 if (IS_ERR(buf))
255 return PTR_ERR(buf);
256
257 strim(buf);
258
259 mutex_lock(&event_mutex);
260 event_file = event_file_data(file);
261 if (unlikely(!event_file)) {
262 mutex_unlock(&event_mutex);
263 kfree(buf);
264 return -ENODEV;
265 }
266 ret = trigger_process_regex(event_file, buf);
267 mutex_unlock(&event_mutex);
268
269 kfree(buf);
270 if (ret < 0)
271 goto out;
272
273 *ppos += cnt;
274 ret = cnt;
275 out:
276 return ret;
277 }
278
279 static int event_trigger_regex_release(struct inode *inode, struct file *file)
280 {
281 mutex_lock(&event_mutex);
282
283 if (file->f_mode & FMODE_READ)
284 seq_release(inode, file);
285
286 mutex_unlock(&event_mutex);
287
288 return 0;
289 }
290
291 static ssize_t
292 event_trigger_write(struct file *filp, const char __user *ubuf,
293 size_t cnt, loff_t *ppos)
294 {
295 return event_trigger_regex_write(filp, ubuf, cnt, ppos);
296 }
297
298 static int
299 event_trigger_open(struct inode *inode, struct file *filp)
300 {
301
302 return event_trigger_regex_open(inode, filp);
303 }
304
305 static int
306 event_trigger_release(struct inode *inode, struct file *file)
307 {
308 return event_trigger_regex_release(inode, file);
309 }
310
311 const struct file_operations event_trigger_fops = {
312 .open = event_trigger_open,
313 .read = seq_read,
314 .write = event_trigger_write,
315 .llseek = tracing_lseek,
316 .release = event_trigger_release,
317 };
318
319
320
321
322
323 __init int register_event_command(struct event_command *cmd)
324 {
325 struct event_command *p;
326 int ret = 0;
327
328 mutex_lock(&trigger_cmd_mutex);
329 list_for_each_entry(p, &trigger_commands, list) {
330 if (strcmp(cmd->name, p->name) == 0) {
331 ret = -EBUSY;
332 goto out_unlock;
333 }
334 }
335 list_add(&cmd->list, &trigger_commands);
336 out_unlock:
337 mutex_unlock(&trigger_cmd_mutex);
338
339 return ret;
340 }
341
342
343
344
345
346 __init int unregister_event_command(struct event_command *cmd)
347 {
348 struct event_command *p, *n;
349 int ret = -ENODEV;
350
351 mutex_lock(&trigger_cmd_mutex);
352 list_for_each_entry_safe(p, n, &trigger_commands, list) {
353 if (strcmp(cmd->name, p->name) == 0) {
354 ret = 0;
355 list_del_init(&p->list);
356 goto out_unlock;
357 }
358 }
359 out_unlock:
360 mutex_unlock(&trigger_cmd_mutex);
361
362 return ret;
363 }
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 static int
380 event_trigger_print(const char *name, struct seq_file *m,
381 void *data, char *filter_str)
382 {
383 long count = (long)data;
384
385 seq_puts(m, name);
386
387 if (count == -1)
388 seq_puts(m, ":unlimited");
389 else
390 seq_printf(m, ":count=%ld", count);
391
392 if (filter_str)
393 seq_printf(m, " if %s\n", filter_str);
394 else
395 seq_putc(m, '\n');
396
397 return 0;
398 }
399
400
401
402
403
404
405
406
407
408
409
410
411
412 int event_trigger_init(struct event_trigger_ops *ops,
413 struct event_trigger_data *data)
414 {
415 data->ref++;
416 return 0;
417 }
418
419
420
421
422
423
424
425
426
427
428
429 static void
430 event_trigger_free(struct event_trigger_ops *ops,
431 struct event_trigger_data *data)
432 {
433 if (WARN_ON_ONCE(data->ref <= 0))
434 return;
435
436 data->ref--;
437 if (!data->ref)
438 trigger_data_free(data);
439 }
440
441 int trace_event_trigger_enable_disable(struct trace_event_file *file,
442 int trigger_enable)
443 {
444 int ret = 0;
445
446 if (trigger_enable) {
447 if (atomic_inc_return(&file->tm_ref) > 1)
448 return ret;
449 set_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
450 ret = trace_event_enable_disable(file, 1, 1);
451 } else {
452 if (atomic_dec_return(&file->tm_ref) > 0)
453 return ret;
454 clear_bit(EVENT_FILE_FL_TRIGGER_MODE_BIT, &file->flags);
455 ret = trace_event_enable_disable(file, 0, 1);
456 }
457
458 return ret;
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474 void
475 clear_event_triggers(struct trace_array *tr)
476 {
477 struct trace_event_file *file;
478
479 list_for_each_entry(file, &tr->events, list) {
480 struct event_trigger_data *data, *n;
481 list_for_each_entry_safe(data, n, &file->triggers, list) {
482 trace_event_trigger_enable_disable(file, 0);
483 list_del_rcu(&data->list);
484 if (data->ops->free)
485 data->ops->free(data->ops, data);
486 }
487 }
488 }
489
490
491
492
493
494
495
496
497
498
499
500 void update_cond_flag(struct trace_event_file *file)
501 {
502 struct event_trigger_data *data;
503 bool set_cond = false;
504
505 lockdep_assert_held(&event_mutex);
506
507 list_for_each_entry(data, &file->triggers, list) {
508 if (data->filter || event_command_post_trigger(data->cmd_ops) ||
509 event_command_needs_rec(data->cmd_ops)) {
510 set_cond = true;
511 break;
512 }
513 }
514
515 if (set_cond)
516 set_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
517 else
518 clear_bit(EVENT_FILE_FL_TRIGGER_COND_BIT, &file->flags);
519 }
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535 static int register_trigger(char *glob, struct event_trigger_ops *ops,
536 struct event_trigger_data *data,
537 struct trace_event_file *file)
538 {
539 struct event_trigger_data *test;
540 int ret = 0;
541
542 lockdep_assert_held(&event_mutex);
543
544 list_for_each_entry(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
560 update_cond_flag(file);
561 if (trace_event_trigger_enable_disable(file, 1) < 0) {
562 list_del_rcu(&data->list);
563 update_cond_flag(file);
564 ret--;
565 }
566 out:
567 return ret;
568 }
569
570
571
572
573
574
575
576
577
578
579
580
581
582 static void unregister_trigger(char *glob, struct event_trigger_ops *ops,
583 struct event_trigger_data *test,
584 struct trace_event_file *file)
585 {
586 struct event_trigger_data *data;
587 bool unregistered = false;
588
589 lockdep_assert_held(&event_mutex);
590
591 list_for_each_entry(data, &file->triggers, list) {
592 if (data->cmd_ops->trigger_type == test->cmd_ops->trigger_type) {
593 unregistered = true;
594 list_del_rcu(&data->list);
595 trace_event_trigger_enable_disable(file, 0);
596 update_cond_flag(file);
597 break;
598 }
599 }
600
601 if (unregistered && data->ops->free)
602 data->ops->free(data->ops, data);
603 }
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621 static int
622 event_trigger_callback(struct event_command *cmd_ops,
623 struct trace_event_file *file,
624 char *glob, char *cmd, char *param)
625 {
626 struct event_trigger_data *trigger_data;
627 struct event_trigger_ops *trigger_ops;
628 char *trigger = NULL;
629 char *number;
630 int ret;
631
632
633 if (param && isdigit(param[0]))
634 trigger = strsep(¶m, " \t");
635
636 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
637
638 ret = -ENOMEM;
639 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
640 if (!trigger_data)
641 goto out;
642
643 trigger_data->count = -1;
644 trigger_data->ops = trigger_ops;
645 trigger_data->cmd_ops = cmd_ops;
646 trigger_data->private_data = file;
647 INIT_LIST_HEAD(&trigger_data->list);
648 INIT_LIST_HEAD(&trigger_data->named_list);
649
650 if (glob[0] == '!') {
651 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
652 kfree(trigger_data);
653 ret = 0;
654 goto out;
655 }
656
657 if (trigger) {
658 number = strsep(&trigger, ":");
659
660 ret = -EINVAL;
661 if (!strlen(number))
662 goto out_free;
663
664
665
666
667
668 ret = kstrtoul(number, 0, &trigger_data->count);
669 if (ret)
670 goto out_free;
671 }
672
673 if (!param)
674 goto out_reg;
675
676 if (!cmd_ops->set_filter)
677 goto out_reg;
678
679 ret = cmd_ops->set_filter(param, trigger_data, file);
680 if (ret < 0)
681 goto out_free;
682
683 out_reg:
684
685 event_trigger_init(trigger_ops, trigger_data);
686 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
687
688
689
690
691
692 if (!ret) {
693 cmd_ops->unreg(glob, trigger_ops, trigger_data, file);
694 ret = -ENOENT;
695 } else if (ret > 0)
696 ret = 0;
697
698
699 event_trigger_free(trigger_ops, trigger_data);
700 out:
701 return ret;
702
703 out_free:
704 if (cmd_ops->set_filter)
705 cmd_ops->set_filter(NULL, trigger_data, NULL);
706 kfree(trigger_data);
707 goto out;
708 }
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726 int set_trigger_filter(char *filter_str,
727 struct event_trigger_data *trigger_data,
728 struct trace_event_file *file)
729 {
730 struct event_trigger_data *data = trigger_data;
731 struct event_filter *filter = NULL, *tmp;
732 int ret = -EINVAL;
733 char *s;
734
735 if (!filter_str)
736 goto assign;
737
738 s = strsep(&filter_str, " \t");
739
740 if (!strlen(s) || strcmp(s, "if") != 0)
741 goto out;
742
743 if (!filter_str)
744 goto out;
745
746
747 ret = create_event_filter(file->tr, file->event_call,
748 filter_str, false, &filter);
749
750
751
752
753 assign:
754 tmp = rcu_access_pointer(data->filter);
755
756 rcu_assign_pointer(data->filter, filter);
757
758 if (tmp) {
759
760 tracepoint_synchronize_unregister();
761 free_event_filter(tmp);
762 }
763
764 kfree(data->filter_str);
765 data->filter_str = NULL;
766
767 if (filter_str) {
768 data->filter_str = kstrdup(filter_str, GFP_KERNEL);
769 if (!data->filter_str) {
770 free_event_filter(rcu_access_pointer(data->filter));
771 data->filter = NULL;
772 ret = -ENOMEM;
773 }
774 }
775 out:
776 return ret;
777 }
778
779 static LIST_HEAD(named_triggers);
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795 struct event_trigger_data *find_named_trigger(const char *name)
796 {
797 struct event_trigger_data *data;
798
799 if (!name)
800 return NULL;
801
802 list_for_each_entry(data, &named_triggers, named_list) {
803 if (data->named_data)
804 continue;
805 if (strcmp(data->name, name) == 0)
806 return data;
807 }
808
809 return NULL;
810 }
811
812
813
814
815
816
817
818 bool is_named_trigger(struct event_trigger_data *test)
819 {
820 struct event_trigger_data *data;
821
822 list_for_each_entry(data, &named_triggers, named_list) {
823 if (test == data)
824 return true;
825 }
826
827 return false;
828 }
829
830
831
832
833
834
835
836
837 int save_named_trigger(const char *name, struct event_trigger_data *data)
838 {
839 data->name = kstrdup(name, GFP_KERNEL);
840 if (!data->name)
841 return -ENOMEM;
842
843 list_add(&data->named_list, &named_triggers);
844
845 return 0;
846 }
847
848
849
850
851
852 void del_named_trigger(struct event_trigger_data *data)
853 {
854 kfree(data->name);
855 data->name = NULL;
856
857 list_del(&data->named_list);
858 }
859
860 static void __pause_named_trigger(struct event_trigger_data *data, bool pause)
861 {
862 struct event_trigger_data *test;
863
864 list_for_each_entry(test, &named_triggers, named_list) {
865 if (strcmp(test->name, data->name) == 0) {
866 if (pause) {
867 test->paused_tmp = test->paused;
868 test->paused = true;
869 } else {
870 test->paused = test->paused_tmp;
871 }
872 }
873 }
874 }
875
876
877
878
879
880
881
882
883
884
885 void pause_named_trigger(struct event_trigger_data *data)
886 {
887 __pause_named_trigger(data, true);
888 }
889
890
891
892
893
894
895
896
897
898
899 void unpause_named_trigger(struct event_trigger_data *data)
900 {
901 __pause_named_trigger(data, false);
902 }
903
904
905
906
907
908
909
910
911
912
913
914
915 void set_named_trigger_data(struct event_trigger_data *data,
916 struct event_trigger_data *named_data)
917 {
918 data->named_data = named_data;
919 }
920
921 struct event_trigger_data *
922 get_named_trigger_data(struct event_trigger_data *data)
923 {
924 return data->named_data;
925 }
926
927 static void
928 traceon_trigger(struct event_trigger_data *data, void *rec,
929 struct ring_buffer_event *event)
930 {
931 if (tracing_is_on())
932 return;
933
934 tracing_on();
935 }
936
937 static void
938 traceon_count_trigger(struct event_trigger_data *data, void *rec,
939 struct ring_buffer_event *event)
940 {
941 if (tracing_is_on())
942 return;
943
944 if (!data->count)
945 return;
946
947 if (data->count != -1)
948 (data->count)--;
949
950 tracing_on();
951 }
952
953 static void
954 traceoff_trigger(struct event_trigger_data *data, void *rec,
955 struct ring_buffer_event *event)
956 {
957 if (!tracing_is_on())
958 return;
959
960 tracing_off();
961 }
962
963 static void
964 traceoff_count_trigger(struct event_trigger_data *data, void *rec,
965 struct ring_buffer_event *event)
966 {
967 if (!tracing_is_on())
968 return;
969
970 if (!data->count)
971 return;
972
973 if (data->count != -1)
974 (data->count)--;
975
976 tracing_off();
977 }
978
979 static int
980 traceon_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
981 struct event_trigger_data *data)
982 {
983 return event_trigger_print("traceon", m, (void *)data->count,
984 data->filter_str);
985 }
986
987 static int
988 traceoff_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
989 struct event_trigger_data *data)
990 {
991 return event_trigger_print("traceoff", m, (void *)data->count,
992 data->filter_str);
993 }
994
995 static struct event_trigger_ops traceon_trigger_ops = {
996 .func = traceon_trigger,
997 .print = traceon_trigger_print,
998 .init = event_trigger_init,
999 .free = event_trigger_free,
1000 };
1001
1002 static struct event_trigger_ops traceon_count_trigger_ops = {
1003 .func = traceon_count_trigger,
1004 .print = traceon_trigger_print,
1005 .init = event_trigger_init,
1006 .free = event_trigger_free,
1007 };
1008
1009 static struct event_trigger_ops traceoff_trigger_ops = {
1010 .func = traceoff_trigger,
1011 .print = traceoff_trigger_print,
1012 .init = event_trigger_init,
1013 .free = event_trigger_free,
1014 };
1015
1016 static struct event_trigger_ops traceoff_count_trigger_ops = {
1017 .func = traceoff_count_trigger,
1018 .print = traceoff_trigger_print,
1019 .init = event_trigger_init,
1020 .free = event_trigger_free,
1021 };
1022
1023 static struct event_trigger_ops *
1024 onoff_get_trigger_ops(char *cmd, char *param)
1025 {
1026 struct event_trigger_ops *ops;
1027
1028
1029 if (strcmp(cmd, "traceon") == 0)
1030 ops = param ? &traceon_count_trigger_ops :
1031 &traceon_trigger_ops;
1032 else
1033 ops = param ? &traceoff_count_trigger_ops :
1034 &traceoff_trigger_ops;
1035
1036 return ops;
1037 }
1038
1039 static struct event_command trigger_traceon_cmd = {
1040 .name = "traceon",
1041 .trigger_type = ETT_TRACE_ONOFF,
1042 .func = event_trigger_callback,
1043 .reg = register_trigger,
1044 .unreg = unregister_trigger,
1045 .get_trigger_ops = onoff_get_trigger_ops,
1046 .set_filter = set_trigger_filter,
1047 };
1048
1049 static struct event_command trigger_traceoff_cmd = {
1050 .name = "traceoff",
1051 .trigger_type = ETT_TRACE_ONOFF,
1052 .flags = EVENT_CMD_FL_POST_TRIGGER,
1053 .func = event_trigger_callback,
1054 .reg = register_trigger,
1055 .unreg = unregister_trigger,
1056 .get_trigger_ops = onoff_get_trigger_ops,
1057 .set_filter = set_trigger_filter,
1058 };
1059
1060 #ifdef CONFIG_TRACER_SNAPSHOT
1061 static void
1062 snapshot_trigger(struct event_trigger_data *data, void *rec,
1063 struct ring_buffer_event *event)
1064 {
1065 struct trace_event_file *file = data->private_data;
1066
1067 if (file)
1068 tracing_snapshot_instance(file->tr);
1069 else
1070 tracing_snapshot();
1071 }
1072
1073 static void
1074 snapshot_count_trigger(struct event_trigger_data *data, void *rec,
1075 struct ring_buffer_event *event)
1076 {
1077 if (!data->count)
1078 return;
1079
1080 if (data->count != -1)
1081 (data->count)--;
1082
1083 snapshot_trigger(data, rec, event);
1084 }
1085
1086 static int
1087 register_snapshot_trigger(char *glob, struct event_trigger_ops *ops,
1088 struct event_trigger_data *data,
1089 struct trace_event_file *file)
1090 {
1091 if (tracing_alloc_snapshot_instance(file->tr) != 0)
1092 return 0;
1093
1094 return register_trigger(glob, ops, data, file);
1095 }
1096
1097 static int
1098 snapshot_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1099 struct event_trigger_data *data)
1100 {
1101 return event_trigger_print("snapshot", m, (void *)data->count,
1102 data->filter_str);
1103 }
1104
1105 static struct event_trigger_ops snapshot_trigger_ops = {
1106 .func = snapshot_trigger,
1107 .print = snapshot_trigger_print,
1108 .init = event_trigger_init,
1109 .free = event_trigger_free,
1110 };
1111
1112 static struct event_trigger_ops snapshot_count_trigger_ops = {
1113 .func = snapshot_count_trigger,
1114 .print = snapshot_trigger_print,
1115 .init = event_trigger_init,
1116 .free = event_trigger_free,
1117 };
1118
1119 static struct event_trigger_ops *
1120 snapshot_get_trigger_ops(char *cmd, char *param)
1121 {
1122 return param ? &snapshot_count_trigger_ops : &snapshot_trigger_ops;
1123 }
1124
1125 static struct event_command trigger_snapshot_cmd = {
1126 .name = "snapshot",
1127 .trigger_type = ETT_SNAPSHOT,
1128 .func = event_trigger_callback,
1129 .reg = register_snapshot_trigger,
1130 .unreg = unregister_trigger,
1131 .get_trigger_ops = snapshot_get_trigger_ops,
1132 .set_filter = set_trigger_filter,
1133 };
1134
1135 static __init int register_trigger_snapshot_cmd(void)
1136 {
1137 int ret;
1138
1139 ret = register_event_command(&trigger_snapshot_cmd);
1140 WARN_ON(ret < 0);
1141
1142 return ret;
1143 }
1144 #else
1145 static __init int register_trigger_snapshot_cmd(void) { return 0; }
1146 #endif
1147
1148 #ifdef CONFIG_STACKTRACE
1149 #ifdef CONFIG_UNWINDER_ORC
1150
1151
1152
1153
1154 # define STACK_SKIP 2
1155 #else
1156
1157
1158
1159
1160
1161
1162
1163 #define STACK_SKIP 4
1164 #endif
1165
1166 static void
1167 stacktrace_trigger(struct event_trigger_data *data, void *rec,
1168 struct ring_buffer_event *event)
1169 {
1170 trace_dump_stack(STACK_SKIP);
1171 }
1172
1173 static void
1174 stacktrace_count_trigger(struct event_trigger_data *data, void *rec,
1175 struct ring_buffer_event *event)
1176 {
1177 if (!data->count)
1178 return;
1179
1180 if (data->count != -1)
1181 (data->count)--;
1182
1183 stacktrace_trigger(data, rec, event);
1184 }
1185
1186 static int
1187 stacktrace_trigger_print(struct seq_file *m, struct event_trigger_ops *ops,
1188 struct event_trigger_data *data)
1189 {
1190 return event_trigger_print("stacktrace", m, (void *)data->count,
1191 data->filter_str);
1192 }
1193
1194 static struct event_trigger_ops stacktrace_trigger_ops = {
1195 .func = stacktrace_trigger,
1196 .print = stacktrace_trigger_print,
1197 .init = event_trigger_init,
1198 .free = event_trigger_free,
1199 };
1200
1201 static struct event_trigger_ops stacktrace_count_trigger_ops = {
1202 .func = stacktrace_count_trigger,
1203 .print = stacktrace_trigger_print,
1204 .init = event_trigger_init,
1205 .free = event_trigger_free,
1206 };
1207
1208 static struct event_trigger_ops *
1209 stacktrace_get_trigger_ops(char *cmd, char *param)
1210 {
1211 return param ? &stacktrace_count_trigger_ops : &stacktrace_trigger_ops;
1212 }
1213
1214 static struct event_command trigger_stacktrace_cmd = {
1215 .name = "stacktrace",
1216 .trigger_type = ETT_STACKTRACE,
1217 .flags = EVENT_CMD_FL_POST_TRIGGER,
1218 .func = event_trigger_callback,
1219 .reg = register_trigger,
1220 .unreg = unregister_trigger,
1221 .get_trigger_ops = stacktrace_get_trigger_ops,
1222 .set_filter = set_trigger_filter,
1223 };
1224
1225 static __init int register_trigger_stacktrace_cmd(void)
1226 {
1227 int ret;
1228
1229 ret = register_event_command(&trigger_stacktrace_cmd);
1230 WARN_ON(ret < 0);
1231
1232 return ret;
1233 }
1234 #else
1235 static __init int register_trigger_stacktrace_cmd(void) { return 0; }
1236 #endif
1237
1238 static __init void unregister_trigger_traceon_traceoff_cmds(void)
1239 {
1240 unregister_event_command(&trigger_traceon_cmd);
1241 unregister_event_command(&trigger_traceoff_cmd);
1242 }
1243
1244 static void
1245 event_enable_trigger(struct event_trigger_data *data, void *rec,
1246 struct ring_buffer_event *event)
1247 {
1248 struct enable_trigger_data *enable_data = data->private_data;
1249
1250 if (enable_data->enable)
1251 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1252 else
1253 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &enable_data->file->flags);
1254 }
1255
1256 static void
1257 event_enable_count_trigger(struct event_trigger_data *data, void *rec,
1258 struct ring_buffer_event *event)
1259 {
1260 struct enable_trigger_data *enable_data = data->private_data;
1261
1262 if (!data->count)
1263 return;
1264
1265
1266 if (enable_data->enable == !(enable_data->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
1267 return;
1268
1269 if (data->count != -1)
1270 (data->count)--;
1271
1272 event_enable_trigger(data, rec, event);
1273 }
1274
1275 int event_enable_trigger_print(struct seq_file *m,
1276 struct event_trigger_ops *ops,
1277 struct event_trigger_data *data)
1278 {
1279 struct enable_trigger_data *enable_data = data->private_data;
1280
1281 seq_printf(m, "%s:%s:%s",
1282 enable_data->hist ?
1283 (enable_data->enable ? ENABLE_HIST_STR : DISABLE_HIST_STR) :
1284 (enable_data->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR),
1285 enable_data->file->event_call->class->system,
1286 trace_event_name(enable_data->file->event_call));
1287
1288 if (data->count == -1)
1289 seq_puts(m, ":unlimited");
1290 else
1291 seq_printf(m, ":count=%ld", data->count);
1292
1293 if (data->filter_str)
1294 seq_printf(m, " if %s\n", data->filter_str);
1295 else
1296 seq_putc(m, '\n');
1297
1298 return 0;
1299 }
1300
1301 void event_enable_trigger_free(struct event_trigger_ops *ops,
1302 struct event_trigger_data *data)
1303 {
1304 struct enable_trigger_data *enable_data = data->private_data;
1305
1306 if (WARN_ON_ONCE(data->ref <= 0))
1307 return;
1308
1309 data->ref--;
1310 if (!data->ref) {
1311
1312 trace_event_enable_disable(enable_data->file, 0, 1);
1313 module_put(enable_data->file->event_call->mod);
1314 trigger_data_free(data);
1315 kfree(enable_data);
1316 }
1317 }
1318
1319 static struct event_trigger_ops event_enable_trigger_ops = {
1320 .func = event_enable_trigger,
1321 .print = event_enable_trigger_print,
1322 .init = event_trigger_init,
1323 .free = event_enable_trigger_free,
1324 };
1325
1326 static struct event_trigger_ops event_enable_count_trigger_ops = {
1327 .func = event_enable_count_trigger,
1328 .print = event_enable_trigger_print,
1329 .init = event_trigger_init,
1330 .free = event_enable_trigger_free,
1331 };
1332
1333 static struct event_trigger_ops event_disable_trigger_ops = {
1334 .func = event_enable_trigger,
1335 .print = event_enable_trigger_print,
1336 .init = event_trigger_init,
1337 .free = event_enable_trigger_free,
1338 };
1339
1340 static struct event_trigger_ops event_disable_count_trigger_ops = {
1341 .func = event_enable_count_trigger,
1342 .print = event_enable_trigger_print,
1343 .init = event_trigger_init,
1344 .free = event_enable_trigger_free,
1345 };
1346
1347 int event_enable_trigger_func(struct event_command *cmd_ops,
1348 struct trace_event_file *file,
1349 char *glob, char *cmd, char *param)
1350 {
1351 struct trace_event_file *event_enable_file;
1352 struct enable_trigger_data *enable_data;
1353 struct event_trigger_data *trigger_data;
1354 struct event_trigger_ops *trigger_ops;
1355 struct trace_array *tr = file->tr;
1356 const char *system;
1357 const char *event;
1358 bool hist = false;
1359 char *trigger;
1360 char *number;
1361 bool enable;
1362 int ret;
1363
1364 if (!param)
1365 return -EINVAL;
1366
1367
1368 trigger = strsep(¶m, " \t");
1369 if (!trigger)
1370 return -EINVAL;
1371
1372 system = strsep(&trigger, ":");
1373 if (!trigger)
1374 return -EINVAL;
1375
1376 event = strsep(&trigger, ":");
1377
1378 ret = -EINVAL;
1379 event_enable_file = find_event_file(tr, system, event);
1380 if (!event_enable_file)
1381 goto out;
1382
1383 #ifdef CONFIG_HIST_TRIGGERS
1384 hist = ((strcmp(cmd, ENABLE_HIST_STR) == 0) ||
1385 (strcmp(cmd, DISABLE_HIST_STR) == 0));
1386
1387 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1388 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1389 #else
1390 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1391 #endif
1392 trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger);
1393
1394 ret = -ENOMEM;
1395 trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL);
1396 if (!trigger_data)
1397 goto out;
1398
1399 enable_data = kzalloc(sizeof(*enable_data), GFP_KERNEL);
1400 if (!enable_data) {
1401 kfree(trigger_data);
1402 goto out;
1403 }
1404
1405 trigger_data->count = -1;
1406 trigger_data->ops = trigger_ops;
1407 trigger_data->cmd_ops = cmd_ops;
1408 INIT_LIST_HEAD(&trigger_data->list);
1409 RCU_INIT_POINTER(trigger_data->filter, NULL);
1410
1411 enable_data->hist = hist;
1412 enable_data->enable = enable;
1413 enable_data->file = event_enable_file;
1414 trigger_data->private_data = enable_data;
1415
1416 if (glob[0] == '!') {
1417 cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file);
1418 kfree(trigger_data);
1419 kfree(enable_data);
1420 ret = 0;
1421 goto out;
1422 }
1423
1424
1425 event_trigger_init(trigger_ops, trigger_data);
1426
1427 if (trigger) {
1428 number = strsep(&trigger, ":");
1429
1430 ret = -EINVAL;
1431 if (!strlen(number))
1432 goto out_free;
1433
1434
1435
1436
1437
1438 ret = kstrtoul(number, 0, &trigger_data->count);
1439 if (ret)
1440 goto out_free;
1441 }
1442
1443 if (!param)
1444 goto out_reg;
1445
1446 if (!cmd_ops->set_filter)
1447 goto out_reg;
1448
1449 ret = cmd_ops->set_filter(param, trigger_data, file);
1450 if (ret < 0)
1451 goto out_free;
1452
1453 out_reg:
1454
1455 ret = try_module_get(event_enable_file->event_call->mod);
1456 if (!ret) {
1457 ret = -EBUSY;
1458 goto out_free;
1459 }
1460
1461 ret = trace_event_enable_disable(event_enable_file, 1, 1);
1462 if (ret < 0)
1463 goto out_put;
1464 ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
1465
1466
1467
1468
1469
1470 if (!ret) {
1471 ret = -ENOENT;
1472 goto out_disable;
1473 } else if (ret < 0)
1474 goto out_disable;
1475
1476 ret = 0;
1477 event_trigger_free(trigger_ops, trigger_data);
1478 out:
1479 return ret;
1480
1481 out_disable:
1482 trace_event_enable_disable(event_enable_file, 0, 1);
1483 out_put:
1484 module_put(event_enable_file->event_call->mod);
1485 out_free:
1486 if (cmd_ops->set_filter)
1487 cmd_ops->set_filter(NULL, trigger_data, NULL);
1488 event_trigger_free(trigger_ops, trigger_data);
1489 kfree(enable_data);
1490 goto out;
1491 }
1492
1493 int event_enable_register_trigger(char *glob,
1494 struct event_trigger_ops *ops,
1495 struct event_trigger_data *data,
1496 struct trace_event_file *file)
1497 {
1498 struct enable_trigger_data *enable_data = data->private_data;
1499 struct enable_trigger_data *test_enable_data;
1500 struct event_trigger_data *test;
1501 int ret = 0;
1502
1503 lockdep_assert_held(&event_mutex);
1504
1505 list_for_each_entry(test, &file->triggers, list) {
1506 test_enable_data = test->private_data;
1507 if (test_enable_data &&
1508 (test->cmd_ops->trigger_type ==
1509 data->cmd_ops->trigger_type) &&
1510 (test_enable_data->file == enable_data->file)) {
1511 ret = -EEXIST;
1512 goto out;
1513 }
1514 }
1515
1516 if (data->ops->init) {
1517 ret = data->ops->init(data->ops, data);
1518 if (ret < 0)
1519 goto out;
1520 }
1521
1522 list_add_rcu(&data->list, &file->triggers);
1523 ret++;
1524
1525 update_cond_flag(file);
1526 if (trace_event_trigger_enable_disable(file, 1) < 0) {
1527 list_del_rcu(&data->list);
1528 update_cond_flag(file);
1529 ret--;
1530 }
1531 out:
1532 return ret;
1533 }
1534
1535 void event_enable_unregister_trigger(char *glob,
1536 struct event_trigger_ops *ops,
1537 struct event_trigger_data *test,
1538 struct trace_event_file *file)
1539 {
1540 struct enable_trigger_data *test_enable_data = test->private_data;
1541 struct enable_trigger_data *enable_data;
1542 struct event_trigger_data *data;
1543 bool unregistered = false;
1544
1545 lockdep_assert_held(&event_mutex);
1546
1547 list_for_each_entry(data, &file->triggers, list) {
1548 enable_data = data->private_data;
1549 if (enable_data &&
1550 (data->cmd_ops->trigger_type ==
1551 test->cmd_ops->trigger_type) &&
1552 (enable_data->file == test_enable_data->file)) {
1553 unregistered = true;
1554 list_del_rcu(&data->list);
1555 trace_event_trigger_enable_disable(file, 0);
1556 update_cond_flag(file);
1557 break;
1558 }
1559 }
1560
1561 if (unregistered && data->ops->free)
1562 data->ops->free(data->ops, data);
1563 }
1564
1565 static struct event_trigger_ops *
1566 event_enable_get_trigger_ops(char *cmd, char *param)
1567 {
1568 struct event_trigger_ops *ops;
1569 bool enable;
1570
1571 #ifdef CONFIG_HIST_TRIGGERS
1572 enable = ((strcmp(cmd, ENABLE_EVENT_STR) == 0) ||
1573 (strcmp(cmd, ENABLE_HIST_STR) == 0));
1574 #else
1575 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
1576 #endif
1577 if (enable)
1578 ops = param ? &event_enable_count_trigger_ops :
1579 &event_enable_trigger_ops;
1580 else
1581 ops = param ? &event_disable_count_trigger_ops :
1582 &event_disable_trigger_ops;
1583
1584 return ops;
1585 }
1586
1587 static struct event_command trigger_enable_cmd = {
1588 .name = ENABLE_EVENT_STR,
1589 .trigger_type = ETT_EVENT_ENABLE,
1590 .func = event_enable_trigger_func,
1591 .reg = event_enable_register_trigger,
1592 .unreg = event_enable_unregister_trigger,
1593 .get_trigger_ops = event_enable_get_trigger_ops,
1594 .set_filter = set_trigger_filter,
1595 };
1596
1597 static struct event_command trigger_disable_cmd = {
1598 .name = DISABLE_EVENT_STR,
1599 .trigger_type = ETT_EVENT_ENABLE,
1600 .func = event_enable_trigger_func,
1601 .reg = event_enable_register_trigger,
1602 .unreg = event_enable_unregister_trigger,
1603 .get_trigger_ops = event_enable_get_trigger_ops,
1604 .set_filter = set_trigger_filter,
1605 };
1606
1607 static __init void unregister_trigger_enable_disable_cmds(void)
1608 {
1609 unregister_event_command(&trigger_enable_cmd);
1610 unregister_event_command(&trigger_disable_cmd);
1611 }
1612
1613 static __init int register_trigger_enable_disable_cmds(void)
1614 {
1615 int ret;
1616
1617 ret = register_event_command(&trigger_enable_cmd);
1618 if (WARN_ON(ret < 0))
1619 return ret;
1620 ret = register_event_command(&trigger_disable_cmd);
1621 if (WARN_ON(ret < 0))
1622 unregister_trigger_enable_disable_cmds();
1623
1624 return ret;
1625 }
1626
1627 static __init int register_trigger_traceon_traceoff_cmds(void)
1628 {
1629 int ret;
1630
1631 ret = register_event_command(&trigger_traceon_cmd);
1632 if (WARN_ON(ret < 0))
1633 return ret;
1634 ret = register_event_command(&trigger_traceoff_cmd);
1635 if (WARN_ON(ret < 0))
1636 unregister_trigger_traceon_traceoff_cmds();
1637
1638 return ret;
1639 }
1640
1641 __init int register_trigger_cmds(void)
1642 {
1643 register_trigger_traceon_traceoff_cmds();
1644 register_trigger_snapshot_cmd();
1645 register_trigger_stacktrace_cmd();
1646 register_trigger_enable_disable_cmds();
1647 register_trigger_hist_enable_disable_cmds();
1648 register_trigger_hist_cmd();
1649
1650 return 0;
1651 }