0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #define pr_fmt(fmt) fmt
0013
0014 #include <linux/workqueue.h>
0015 #include <linux/security.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/kthread.h>
0018 #include <linux/tracefs.h>
0019 #include <linux/uaccess.h>
0020 #include <linux/module.h>
0021 #include <linux/ctype.h>
0022 #include <linux/sort.h>
0023 #include <linux/slab.h>
0024 #include <linux/delay.h>
0025
0026 #include <trace/events/sched.h>
0027 #include <trace/syscall.h>
0028
0029 #include <asm/setup.h>
0030
0031 #include "trace_output.h"
0032
0033 #undef TRACE_SYSTEM
0034 #define TRACE_SYSTEM "TRACE_SYSTEM"
0035
0036 DEFINE_MUTEX(event_mutex);
0037
0038 LIST_HEAD(ftrace_events);
0039 static LIST_HEAD(ftrace_generic_fields);
0040 static LIST_HEAD(ftrace_common_fields);
0041 static bool eventdir_initialized;
0042
0043 static LIST_HEAD(module_strings);
0044
0045 struct module_string {
0046 struct list_head next;
0047 struct module *module;
0048 char *str;
0049 };
0050
0051 #define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
0052
0053 static struct kmem_cache *field_cachep;
0054 static struct kmem_cache *file_cachep;
0055
0056 static inline int system_refcount(struct event_subsystem *system)
0057 {
0058 return system->ref_count;
0059 }
0060
0061 static int system_refcount_inc(struct event_subsystem *system)
0062 {
0063 return system->ref_count++;
0064 }
0065
0066 static int system_refcount_dec(struct event_subsystem *system)
0067 {
0068 return --system->ref_count;
0069 }
0070
0071
0072 #define do_for_each_event_file(tr, file) \
0073 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
0074 list_for_each_entry(file, &tr->events, list)
0075
0076 #define do_for_each_event_file_safe(tr, file) \
0077 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
0078 struct trace_event_file *___n; \
0079 list_for_each_entry_safe(file, ___n, &tr->events, list)
0080
0081 #define while_for_each_event_file() \
0082 }
0083
0084 static struct ftrace_event_field *
0085 __find_event_field(struct list_head *head, char *name)
0086 {
0087 struct ftrace_event_field *field;
0088
0089 list_for_each_entry(field, head, link) {
0090 if (!strcmp(field->name, name))
0091 return field;
0092 }
0093
0094 return NULL;
0095 }
0096
0097 struct ftrace_event_field *
0098 trace_find_event_field(struct trace_event_call *call, char *name)
0099 {
0100 struct ftrace_event_field *field;
0101 struct list_head *head;
0102
0103 head = trace_get_fields(call);
0104 field = __find_event_field(head, name);
0105 if (field)
0106 return field;
0107
0108 field = __find_event_field(&ftrace_generic_fields, name);
0109 if (field)
0110 return field;
0111
0112 return __find_event_field(&ftrace_common_fields, name);
0113 }
0114
0115 static int __trace_define_field(struct list_head *head, const char *type,
0116 const char *name, int offset, int size,
0117 int is_signed, int filter_type)
0118 {
0119 struct ftrace_event_field *field;
0120
0121 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
0122 if (!field)
0123 return -ENOMEM;
0124
0125 field->name = name;
0126 field->type = type;
0127
0128 if (filter_type == FILTER_OTHER)
0129 field->filter_type = filter_assign_type(type);
0130 else
0131 field->filter_type = filter_type;
0132
0133 field->offset = offset;
0134 field->size = size;
0135 field->is_signed = is_signed;
0136
0137 list_add(&field->link, head);
0138
0139 return 0;
0140 }
0141
0142 int trace_define_field(struct trace_event_call *call, const char *type,
0143 const char *name, int offset, int size, int is_signed,
0144 int filter_type)
0145 {
0146 struct list_head *head;
0147
0148 if (WARN_ON(!call->class))
0149 return 0;
0150
0151 head = trace_get_fields(call);
0152 return __trace_define_field(head, type, name, offset, size,
0153 is_signed, filter_type);
0154 }
0155 EXPORT_SYMBOL_GPL(trace_define_field);
0156
0157 #define __generic_field(type, item, filter_type) \
0158 ret = __trace_define_field(&ftrace_generic_fields, #type, \
0159 #item, 0, 0, is_signed_type(type), \
0160 filter_type); \
0161 if (ret) \
0162 return ret;
0163
0164 #define __common_field(type, item) \
0165 ret = __trace_define_field(&ftrace_common_fields, #type, \
0166 "common_" #item, \
0167 offsetof(typeof(ent), item), \
0168 sizeof(ent.item), \
0169 is_signed_type(type), FILTER_OTHER); \
0170 if (ret) \
0171 return ret;
0172
0173 static int trace_define_generic_fields(void)
0174 {
0175 int ret;
0176
0177 __generic_field(int, CPU, FILTER_CPU);
0178 __generic_field(int, cpu, FILTER_CPU);
0179 __generic_field(int, common_cpu, FILTER_CPU);
0180 __generic_field(char *, COMM, FILTER_COMM);
0181 __generic_field(char *, comm, FILTER_COMM);
0182
0183 return ret;
0184 }
0185
0186 static int trace_define_common_fields(void)
0187 {
0188 int ret;
0189 struct trace_entry ent;
0190
0191 __common_field(unsigned short, type);
0192 __common_field(unsigned char, flags);
0193
0194 __common_field(unsigned char, preempt_count);
0195 __common_field(int, pid);
0196
0197 return ret;
0198 }
0199
0200 static void trace_destroy_fields(struct trace_event_call *call)
0201 {
0202 struct ftrace_event_field *field, *next;
0203 struct list_head *head;
0204
0205 head = trace_get_fields(call);
0206 list_for_each_entry_safe(field, next, head, link) {
0207 list_del(&field->link);
0208 kmem_cache_free(field_cachep, field);
0209 }
0210 }
0211
0212
0213
0214
0215
0216 int trace_event_get_offsets(struct trace_event_call *call)
0217 {
0218 struct ftrace_event_field *tail;
0219 struct list_head *head;
0220
0221 head = trace_get_fields(call);
0222
0223
0224
0225
0226 tail = list_first_entry(head, struct ftrace_event_field, link);
0227 return tail->offset + tail->size;
0228 }
0229
0230
0231
0232
0233
0234 static bool test_field(const char *fmt, struct trace_event_call *call)
0235 {
0236 struct trace_event_fields *field = call->class->fields_array;
0237 const char *array_descriptor;
0238 const char *p = fmt;
0239 int len;
0240
0241 if (!(len = str_has_prefix(fmt, "REC->")))
0242 return false;
0243 fmt += len;
0244 for (p = fmt; *p; p++) {
0245 if (!isalnum(*p) && *p != '_')
0246 break;
0247 }
0248 len = p - fmt;
0249
0250 for (; field->type; field++) {
0251 if (strncmp(field->name, fmt, len) ||
0252 field->name[len])
0253 continue;
0254 array_descriptor = strchr(field->type, '[');
0255
0256 return array_descriptor != NULL;
0257 }
0258 return false;
0259 }
0260
0261
0262
0263
0264
0265
0266
0267 static void test_event_printk(struct trace_event_call *call)
0268 {
0269 u64 dereference_flags = 0;
0270 bool first = true;
0271 const char *fmt, *c, *r, *a;
0272 int parens = 0;
0273 char in_quote = 0;
0274 int start_arg = 0;
0275 int arg = 0;
0276 int i;
0277
0278 fmt = call->print_fmt;
0279
0280 if (!fmt)
0281 return;
0282
0283 for (i = 0; fmt[i]; i++) {
0284 switch (fmt[i]) {
0285 case '\\':
0286 i++;
0287 if (!fmt[i])
0288 return;
0289 continue;
0290 case '"':
0291 case '\'':
0292
0293
0294
0295
0296
0297
0298
0299 if (first) {
0300 if (fmt[i] == '\'')
0301 continue;
0302 if (in_quote) {
0303 arg = 0;
0304 first = false;
0305
0306
0307
0308
0309 if (!dereference_flags)
0310 return;
0311 }
0312 }
0313 if (in_quote) {
0314 if (in_quote == fmt[i])
0315 in_quote = 0;
0316 } else {
0317 in_quote = fmt[i];
0318 }
0319 continue;
0320 case '%':
0321 if (!first || !in_quote)
0322 continue;
0323 i++;
0324 if (!fmt[i])
0325 return;
0326 switch (fmt[i]) {
0327 case '%':
0328 continue;
0329 case 'p':
0330
0331 switch (fmt[i + 1]) {
0332 case 'B': case 'R': case 'r':
0333 case 'b': case 'M': case 'm':
0334 case 'I': case 'i': case 'E':
0335 case 'U': case 'V': case 'N':
0336 case 'a': case 'd': case 'D':
0337 case 'g': case 't': case 'C':
0338 case 'O': case 'f':
0339 if (WARN_ONCE(arg == 63,
0340 "Too many args for event: %s",
0341 trace_event_name(call)))
0342 return;
0343 dereference_flags |= 1ULL << arg;
0344 }
0345 break;
0346 default:
0347 {
0348 bool star = false;
0349 int j;
0350
0351
0352 for (j = 0; fmt[i + j]; j++) {
0353 if (isdigit(fmt[i + j]) ||
0354 fmt[i + j] == '.')
0355 continue;
0356 if (fmt[i + j] == '*') {
0357 star = true;
0358 continue;
0359 }
0360 if ((fmt[i + j] == 's') && star)
0361 arg++;
0362 break;
0363 }
0364 break;
0365 }
0366
0367 }
0368 arg++;
0369 continue;
0370 case '(':
0371 if (in_quote)
0372 continue;
0373 parens++;
0374 continue;
0375 case ')':
0376 if (in_quote)
0377 continue;
0378 parens--;
0379 if (WARN_ONCE(parens < 0,
0380 "Paren mismatch for event: %s\narg='%s'\n%*s",
0381 trace_event_name(call),
0382 fmt + start_arg,
0383 (i - start_arg) + 5, "^"))
0384 return;
0385 continue;
0386 case ',':
0387 if (in_quote || parens)
0388 continue;
0389 i++;
0390 while (isspace(fmt[i]))
0391 i++;
0392 start_arg = i;
0393 if (!(dereference_flags & (1ULL << arg)))
0394 goto next_arg;
0395
0396
0397 c = strchr(fmt + i, ',');
0398 r = strstr(fmt + i, "REC->");
0399 if (r && (!c || r < c)) {
0400
0401
0402
0403
0404
0405
0406
0407
0408 a = strchr(fmt + i, '&');
0409 if ((a && (a < r)) || test_field(r, call))
0410 dereference_flags &= ~(1ULL << arg);
0411 } else if ((r = strstr(fmt + i, "__get_dynamic_array(")) &&
0412 (!c || r < c)) {
0413 dereference_flags &= ~(1ULL << arg);
0414 } else if ((r = strstr(fmt + i, "__get_sockaddr(")) &&
0415 (!c || r < c)) {
0416 dereference_flags &= ~(1ULL << arg);
0417 }
0418
0419 next_arg:
0420 i--;
0421 arg++;
0422 }
0423 }
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433 if (WARN_ON_ONCE(dereference_flags)) {
0434 arg = 1;
0435 while (!(dereference_flags & 1)) {
0436 dereference_flags >>= 1;
0437 arg++;
0438 }
0439 pr_warn("event %s has unsafe dereference of argument %d\n",
0440 trace_event_name(call), arg);
0441 pr_warn("print_fmt: %s\n", fmt);
0442 }
0443 }
0444
0445 int trace_event_raw_init(struct trace_event_call *call)
0446 {
0447 int id;
0448
0449 id = register_trace_event(&call->event);
0450 if (!id)
0451 return -ENODEV;
0452
0453 test_event_printk(call);
0454
0455 return 0;
0456 }
0457 EXPORT_SYMBOL_GPL(trace_event_raw_init);
0458
0459 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
0460 {
0461 struct trace_array *tr = trace_file->tr;
0462 struct trace_array_cpu *data;
0463 struct trace_pid_list *no_pid_list;
0464 struct trace_pid_list *pid_list;
0465
0466 pid_list = rcu_dereference_raw(tr->filtered_pids);
0467 no_pid_list = rcu_dereference_raw(tr->filtered_no_pids);
0468
0469 if (!pid_list && !no_pid_list)
0470 return false;
0471
0472 data = this_cpu_ptr(tr->array_buffer.data);
0473
0474 return data->ignore_pid;
0475 }
0476 EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
0477
0478 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
0479 struct trace_event_file *trace_file,
0480 unsigned long len)
0481 {
0482 struct trace_event_call *event_call = trace_file->event_call;
0483
0484 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
0485 trace_event_ignore_this_pid(trace_file))
0486 return NULL;
0487
0488
0489
0490
0491
0492
0493
0494 fbuffer->trace_ctx = tracing_gen_ctx_dec();
0495 fbuffer->trace_file = trace_file;
0496
0497 fbuffer->event =
0498 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
0499 event_call->event.type, len,
0500 fbuffer->trace_ctx);
0501 if (!fbuffer->event)
0502 return NULL;
0503
0504 fbuffer->regs = NULL;
0505 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
0506 return fbuffer->entry;
0507 }
0508 EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
0509
0510 int trace_event_reg(struct trace_event_call *call,
0511 enum trace_reg type, void *data)
0512 {
0513 struct trace_event_file *file = data;
0514
0515 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
0516 switch (type) {
0517 case TRACE_REG_REGISTER:
0518 return tracepoint_probe_register(call->tp,
0519 call->class->probe,
0520 file);
0521 case TRACE_REG_UNREGISTER:
0522 tracepoint_probe_unregister(call->tp,
0523 call->class->probe,
0524 file);
0525 return 0;
0526
0527 #ifdef CONFIG_PERF_EVENTS
0528 case TRACE_REG_PERF_REGISTER:
0529 return tracepoint_probe_register(call->tp,
0530 call->class->perf_probe,
0531 call);
0532 case TRACE_REG_PERF_UNREGISTER:
0533 tracepoint_probe_unregister(call->tp,
0534 call->class->perf_probe,
0535 call);
0536 return 0;
0537 case TRACE_REG_PERF_OPEN:
0538 case TRACE_REG_PERF_CLOSE:
0539 case TRACE_REG_PERF_ADD:
0540 case TRACE_REG_PERF_DEL:
0541 return 0;
0542 #endif
0543 }
0544 return 0;
0545 }
0546 EXPORT_SYMBOL_GPL(trace_event_reg);
0547
0548 void trace_event_enable_cmd_record(bool enable)
0549 {
0550 struct trace_event_file *file;
0551 struct trace_array *tr;
0552
0553 lockdep_assert_held(&event_mutex);
0554
0555 do_for_each_event_file(tr, file) {
0556
0557 if (!(file->flags & EVENT_FILE_FL_ENABLED))
0558 continue;
0559
0560 if (enable) {
0561 tracing_start_cmdline_record();
0562 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
0563 } else {
0564 tracing_stop_cmdline_record();
0565 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
0566 }
0567 } while_for_each_event_file();
0568 }
0569
0570 void trace_event_enable_tgid_record(bool enable)
0571 {
0572 struct trace_event_file *file;
0573 struct trace_array *tr;
0574
0575 lockdep_assert_held(&event_mutex);
0576
0577 do_for_each_event_file(tr, file) {
0578 if (!(file->flags & EVENT_FILE_FL_ENABLED))
0579 continue;
0580
0581 if (enable) {
0582 tracing_start_tgid_record();
0583 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
0584 } else {
0585 tracing_stop_tgid_record();
0586 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
0587 &file->flags);
0588 }
0589 } while_for_each_event_file();
0590 }
0591
0592 static int __ftrace_event_enable_disable(struct trace_event_file *file,
0593 int enable, int soft_disable)
0594 {
0595 struct trace_event_call *call = file->event_call;
0596 struct trace_array *tr = file->tr;
0597 unsigned long file_flags = file->flags;
0598 int ret = 0;
0599 int disable;
0600
0601 switch (enable) {
0602 case 0:
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616 if (soft_disable) {
0617 if (atomic_dec_return(&file->sm_ref) > 0)
0618 break;
0619 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
0620 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
0621 } else
0622 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
0623
0624 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
0625 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
0626 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
0627 tracing_stop_cmdline_record();
0628 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
0629 }
0630
0631 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
0632 tracing_stop_tgid_record();
0633 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
0634 }
0635
0636 call->class->reg(call, TRACE_REG_UNREGISTER, file);
0637 }
0638
0639 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
0640 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
0641 else
0642 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
0643 break;
0644 case 1:
0645
0646
0647
0648
0649
0650
0651
0652
0653 if (!soft_disable)
0654 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
0655 else {
0656 if (atomic_inc_return(&file->sm_ref) > 1)
0657 break;
0658 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
0659 }
0660
0661 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
0662 bool cmd = false, tgid = false;
0663
0664
0665 if (soft_disable)
0666 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
0667
0668 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
0669 cmd = true;
0670 tracing_start_cmdline_record();
0671 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
0672 }
0673
0674 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
0675 tgid = true;
0676 tracing_start_tgid_record();
0677 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
0678 }
0679
0680 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
0681 if (ret) {
0682 if (cmd)
0683 tracing_stop_cmdline_record();
0684 if (tgid)
0685 tracing_stop_tgid_record();
0686 pr_info("event trace: Could not enable event "
0687 "%s\n", trace_event_name(call));
0688 break;
0689 }
0690 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
0691
0692
0693 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
0694 }
0695 break;
0696 }
0697
0698
0699 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
0700 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
0701 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
0702 trace_buffered_event_enable();
0703 else
0704 trace_buffered_event_disable();
0705 }
0706
0707 return ret;
0708 }
0709
0710 int trace_event_enable_disable(struct trace_event_file *file,
0711 int enable, int soft_disable)
0712 {
0713 return __ftrace_event_enable_disable(file, enable, soft_disable);
0714 }
0715
0716 static int ftrace_event_enable_disable(struct trace_event_file *file,
0717 int enable)
0718 {
0719 return __ftrace_event_enable_disable(file, enable, 0);
0720 }
0721
0722 static void ftrace_clear_events(struct trace_array *tr)
0723 {
0724 struct trace_event_file *file;
0725
0726 mutex_lock(&event_mutex);
0727 list_for_each_entry(file, &tr->events, list) {
0728 ftrace_event_enable_disable(file, 0);
0729 }
0730 mutex_unlock(&event_mutex);
0731 }
0732
0733 static void
0734 event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
0735 {
0736 struct trace_pid_list *pid_list;
0737 struct trace_array *tr = data;
0738
0739 pid_list = rcu_dereference_raw(tr->filtered_pids);
0740 trace_filter_add_remove_task(pid_list, NULL, task);
0741
0742 pid_list = rcu_dereference_raw(tr->filtered_no_pids);
0743 trace_filter_add_remove_task(pid_list, NULL, task);
0744 }
0745
0746 static void
0747 event_filter_pid_sched_process_fork(void *data,
0748 struct task_struct *self,
0749 struct task_struct *task)
0750 {
0751 struct trace_pid_list *pid_list;
0752 struct trace_array *tr = data;
0753
0754 pid_list = rcu_dereference_sched(tr->filtered_pids);
0755 trace_filter_add_remove_task(pid_list, self, task);
0756
0757 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
0758 trace_filter_add_remove_task(pid_list, self, task);
0759 }
0760
0761 void trace_event_follow_fork(struct trace_array *tr, bool enable)
0762 {
0763 if (enable) {
0764 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
0765 tr, INT_MIN);
0766 register_trace_prio_sched_process_free(event_filter_pid_sched_process_exit,
0767 tr, INT_MAX);
0768 } else {
0769 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
0770 tr);
0771 unregister_trace_sched_process_free(event_filter_pid_sched_process_exit,
0772 tr);
0773 }
0774 }
0775
0776 static void
0777 event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
0778 struct task_struct *prev,
0779 struct task_struct *next,
0780 unsigned int prev_state)
0781 {
0782 struct trace_array *tr = data;
0783 struct trace_pid_list *no_pid_list;
0784 struct trace_pid_list *pid_list;
0785 bool ret;
0786
0787 pid_list = rcu_dereference_sched(tr->filtered_pids);
0788 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
0789
0790
0791
0792
0793
0794 ret = trace_ignore_this_task(NULL, no_pid_list, prev) &&
0795 trace_ignore_this_task(NULL, no_pid_list, next);
0796
0797 this_cpu_write(tr->array_buffer.data->ignore_pid, ret ||
0798 (trace_ignore_this_task(pid_list, NULL, prev) &&
0799 trace_ignore_this_task(pid_list, NULL, next)));
0800 }
0801
0802 static void
0803 event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
0804 struct task_struct *prev,
0805 struct task_struct *next,
0806 unsigned int prev_state)
0807 {
0808 struct trace_array *tr = data;
0809 struct trace_pid_list *no_pid_list;
0810 struct trace_pid_list *pid_list;
0811
0812 pid_list = rcu_dereference_sched(tr->filtered_pids);
0813 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
0814
0815 this_cpu_write(tr->array_buffer.data->ignore_pid,
0816 trace_ignore_this_task(pid_list, no_pid_list, next));
0817 }
0818
0819 static void
0820 event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
0821 {
0822 struct trace_array *tr = data;
0823 struct trace_pid_list *no_pid_list;
0824 struct trace_pid_list *pid_list;
0825
0826
0827 if (!this_cpu_read(tr->array_buffer.data->ignore_pid))
0828 return;
0829
0830 pid_list = rcu_dereference_sched(tr->filtered_pids);
0831 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
0832
0833 this_cpu_write(tr->array_buffer.data->ignore_pid,
0834 trace_ignore_this_task(pid_list, no_pid_list, task));
0835 }
0836
0837 static void
0838 event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
0839 {
0840 struct trace_array *tr = data;
0841 struct trace_pid_list *no_pid_list;
0842 struct trace_pid_list *pid_list;
0843
0844
0845 if (this_cpu_read(tr->array_buffer.data->ignore_pid))
0846 return;
0847
0848 pid_list = rcu_dereference_sched(tr->filtered_pids);
0849 no_pid_list = rcu_dereference_sched(tr->filtered_no_pids);
0850
0851
0852 this_cpu_write(tr->array_buffer.data->ignore_pid,
0853 trace_ignore_this_task(pid_list, no_pid_list, current));
0854 }
0855
0856 static void unregister_pid_events(struct trace_array *tr)
0857 {
0858 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
0859 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
0860
0861 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
0862 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
0863
0864 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
0865 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
0866
0867 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
0868 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
0869 }
0870
0871 static void __ftrace_clear_event_pids(struct trace_array *tr, int type)
0872 {
0873 struct trace_pid_list *pid_list;
0874 struct trace_pid_list *no_pid_list;
0875 struct trace_event_file *file;
0876 int cpu;
0877
0878 pid_list = rcu_dereference_protected(tr->filtered_pids,
0879 lockdep_is_held(&event_mutex));
0880 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
0881 lockdep_is_held(&event_mutex));
0882
0883
0884 if (!pid_type_enabled(type, pid_list, no_pid_list))
0885 return;
0886
0887 if (!still_need_pid_events(type, pid_list, no_pid_list)) {
0888 unregister_pid_events(tr);
0889
0890 list_for_each_entry(file, &tr->events, list) {
0891 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
0892 }
0893
0894 for_each_possible_cpu(cpu)
0895 per_cpu_ptr(tr->array_buffer.data, cpu)->ignore_pid = false;
0896 }
0897
0898 if (type & TRACE_PIDS)
0899 rcu_assign_pointer(tr->filtered_pids, NULL);
0900
0901 if (type & TRACE_NO_PIDS)
0902 rcu_assign_pointer(tr->filtered_no_pids, NULL);
0903
0904
0905 tracepoint_synchronize_unregister();
0906
0907 if ((type & TRACE_PIDS) && pid_list)
0908 trace_pid_list_free(pid_list);
0909
0910 if ((type & TRACE_NO_PIDS) && no_pid_list)
0911 trace_pid_list_free(no_pid_list);
0912 }
0913
0914 static void ftrace_clear_event_pids(struct trace_array *tr, int type)
0915 {
0916 mutex_lock(&event_mutex);
0917 __ftrace_clear_event_pids(tr, type);
0918 mutex_unlock(&event_mutex);
0919 }
0920
0921 static void __put_system(struct event_subsystem *system)
0922 {
0923 struct event_filter *filter = system->filter;
0924
0925 WARN_ON_ONCE(system_refcount(system) == 0);
0926 if (system_refcount_dec(system))
0927 return;
0928
0929 list_del(&system->list);
0930
0931 if (filter) {
0932 kfree(filter->filter_string);
0933 kfree(filter);
0934 }
0935 kfree_const(system->name);
0936 kfree(system);
0937 }
0938
0939 static void __get_system(struct event_subsystem *system)
0940 {
0941 WARN_ON_ONCE(system_refcount(system) == 0);
0942 system_refcount_inc(system);
0943 }
0944
0945 static void __get_system_dir(struct trace_subsystem_dir *dir)
0946 {
0947 WARN_ON_ONCE(dir->ref_count == 0);
0948 dir->ref_count++;
0949 __get_system(dir->subsystem);
0950 }
0951
0952 static void __put_system_dir(struct trace_subsystem_dir *dir)
0953 {
0954 WARN_ON_ONCE(dir->ref_count == 0);
0955
0956 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
0957
0958 __put_system(dir->subsystem);
0959 if (!--dir->ref_count)
0960 kfree(dir);
0961 }
0962
0963 static void put_system(struct trace_subsystem_dir *dir)
0964 {
0965 mutex_lock(&event_mutex);
0966 __put_system_dir(dir);
0967 mutex_unlock(&event_mutex);
0968 }
0969
0970 static void remove_subsystem(struct trace_subsystem_dir *dir)
0971 {
0972 if (!dir)
0973 return;
0974
0975 if (!--dir->nr_events) {
0976 tracefs_remove(dir->entry);
0977 list_del(&dir->list);
0978 __put_system_dir(dir);
0979 }
0980 }
0981
0982 static void remove_event_file_dir(struct trace_event_file *file)
0983 {
0984 struct dentry *dir = file->dir;
0985 struct dentry *child;
0986
0987 if (dir) {
0988 spin_lock(&dir->d_lock);
0989 list_for_each_entry(child, &dir->d_subdirs, d_child) {
0990 if (d_really_is_positive(child))
0991 d_inode(child)->i_private = NULL;
0992 }
0993 spin_unlock(&dir->d_lock);
0994
0995 tracefs_remove(dir);
0996 }
0997
0998 list_del(&file->list);
0999 remove_subsystem(file->system);
1000 free_event_filter(file->filter);
1001 kmem_cache_free(file_cachep, file);
1002 }
1003
1004
1005
1006
1007 static int
1008 __ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
1009 const char *sub, const char *event, int set)
1010 {
1011 struct trace_event_file *file;
1012 struct trace_event_call *call;
1013 const char *name;
1014 int ret = -EINVAL;
1015 int eret = 0;
1016
1017 list_for_each_entry(file, &tr->events, list) {
1018
1019 call = file->event_call;
1020 name = trace_event_name(call);
1021
1022 if (!name || !call->class || !call->class->reg)
1023 continue;
1024
1025 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
1026 continue;
1027
1028 if (match &&
1029 strcmp(match, name) != 0 &&
1030 strcmp(match, call->class->system) != 0)
1031 continue;
1032
1033 if (sub && strcmp(sub, call->class->system) != 0)
1034 continue;
1035
1036 if (event && strcmp(event, name) != 0)
1037 continue;
1038
1039 ret = ftrace_event_enable_disable(file, set);
1040
1041
1042
1043
1044
1045
1046 if (ret && !eret)
1047 eret = ret;
1048
1049 ret = eret;
1050 }
1051
1052 return ret;
1053 }
1054
1055 static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
1056 const char *sub, const char *event, int set)
1057 {
1058 int ret;
1059
1060 mutex_lock(&event_mutex);
1061 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
1062 mutex_unlock(&event_mutex);
1063
1064 return ret;
1065 }
1066
1067 int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
1068 {
1069 char *event = NULL, *sub = NULL, *match;
1070 int ret;
1071
1072 if (!tr)
1073 return -ENOENT;
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086 match = strsep(&buf, ":");
1087 if (buf) {
1088 sub = match;
1089 event = buf;
1090 match = NULL;
1091
1092 if (!strlen(sub) || strcmp(sub, "*") == 0)
1093 sub = NULL;
1094 if (!strlen(event) || strcmp(event, "*") == 0)
1095 event = NULL;
1096 }
1097
1098 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
1099
1100
1101 if (buf)
1102 *(buf - 1) = ':';
1103
1104 return ret;
1105 }
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 int trace_set_clr_event(const char *system, const char *event, int set)
1120 {
1121 struct trace_array *tr = top_trace_array();
1122
1123 if (!tr)
1124 return -ENODEV;
1125
1126 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1127 }
1128 EXPORT_SYMBOL_GPL(trace_set_clr_event);
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143 int trace_array_set_clr_event(struct trace_array *tr, const char *system,
1144 const char *event, bool enable)
1145 {
1146 int set;
1147
1148 if (!tr)
1149 return -ENOENT;
1150
1151 set = (enable == true) ? 1 : 0;
1152 return __ftrace_set_clr_event(tr, NULL, system, event, set);
1153 }
1154 EXPORT_SYMBOL_GPL(trace_array_set_clr_event);
1155
1156
1157 #define EVENT_BUF_SIZE 127
1158
1159 static ssize_t
1160 ftrace_event_write(struct file *file, const char __user *ubuf,
1161 size_t cnt, loff_t *ppos)
1162 {
1163 struct trace_parser parser;
1164 struct seq_file *m = file->private_data;
1165 struct trace_array *tr = m->private;
1166 ssize_t read, ret;
1167
1168 if (!cnt)
1169 return 0;
1170
1171 ret = tracing_update_buffers();
1172 if (ret < 0)
1173 return ret;
1174
1175 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1176 return -ENOMEM;
1177
1178 read = trace_get_user(&parser, ubuf, cnt, ppos);
1179
1180 if (read >= 0 && trace_parser_loaded((&parser))) {
1181 int set = 1;
1182
1183 if (*parser.buffer == '!')
1184 set = 0;
1185
1186 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
1187 if (ret)
1188 goto out_put;
1189 }
1190
1191 ret = read;
1192
1193 out_put:
1194 trace_parser_put(&parser);
1195
1196 return ret;
1197 }
1198
1199 static void *
1200 t_next(struct seq_file *m, void *v, loff_t *pos)
1201 {
1202 struct trace_event_file *file = v;
1203 struct trace_event_call *call;
1204 struct trace_array *tr = m->private;
1205
1206 (*pos)++;
1207
1208 list_for_each_entry_continue(file, &tr->events, list) {
1209 call = file->event_call;
1210
1211
1212
1213
1214 if (call->class && call->class->reg &&
1215 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
1216 return file;
1217 }
1218
1219 return NULL;
1220 }
1221
1222 static void *t_start(struct seq_file *m, loff_t *pos)
1223 {
1224 struct trace_event_file *file;
1225 struct trace_array *tr = m->private;
1226 loff_t l;
1227
1228 mutex_lock(&event_mutex);
1229
1230 file = list_entry(&tr->events, struct trace_event_file, list);
1231 for (l = 0; l <= *pos; ) {
1232 file = t_next(m, file, &l);
1233 if (!file)
1234 break;
1235 }
1236 return file;
1237 }
1238
1239 static void *
1240 s_next(struct seq_file *m, void *v, loff_t *pos)
1241 {
1242 struct trace_event_file *file = v;
1243 struct trace_array *tr = m->private;
1244
1245 (*pos)++;
1246
1247 list_for_each_entry_continue(file, &tr->events, list) {
1248 if (file->flags & EVENT_FILE_FL_ENABLED)
1249 return file;
1250 }
1251
1252 return NULL;
1253 }
1254
1255 static void *s_start(struct seq_file *m, loff_t *pos)
1256 {
1257 struct trace_event_file *file;
1258 struct trace_array *tr = m->private;
1259 loff_t l;
1260
1261 mutex_lock(&event_mutex);
1262
1263 file = list_entry(&tr->events, struct trace_event_file, list);
1264 for (l = 0; l <= *pos; ) {
1265 file = s_next(m, file, &l);
1266 if (!file)
1267 break;
1268 }
1269 return file;
1270 }
1271
1272 static int t_show(struct seq_file *m, void *v)
1273 {
1274 struct trace_event_file *file = v;
1275 struct trace_event_call *call = file->event_call;
1276
1277 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
1278 seq_printf(m, "%s:", call->class->system);
1279 seq_printf(m, "%s\n", trace_event_name(call));
1280
1281 return 0;
1282 }
1283
1284 static void t_stop(struct seq_file *m, void *p)
1285 {
1286 mutex_unlock(&event_mutex);
1287 }
1288
1289 static void *
1290 __next(struct seq_file *m, void *v, loff_t *pos, int type)
1291 {
1292 struct trace_array *tr = m->private;
1293 struct trace_pid_list *pid_list;
1294
1295 if (type == TRACE_PIDS)
1296 pid_list = rcu_dereference_sched(tr->filtered_pids);
1297 else
1298 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1299
1300 return trace_pid_next(pid_list, v, pos);
1301 }
1302
1303 static void *
1304 p_next(struct seq_file *m, void *v, loff_t *pos)
1305 {
1306 return __next(m, v, pos, TRACE_PIDS);
1307 }
1308
1309 static void *
1310 np_next(struct seq_file *m, void *v, loff_t *pos)
1311 {
1312 return __next(m, v, pos, TRACE_NO_PIDS);
1313 }
1314
1315 static void *__start(struct seq_file *m, loff_t *pos, int type)
1316 __acquires(RCU)
1317 {
1318 struct trace_pid_list *pid_list;
1319 struct trace_array *tr = m->private;
1320
1321
1322
1323
1324
1325
1326
1327 mutex_lock(&event_mutex);
1328 rcu_read_lock_sched();
1329
1330 if (type == TRACE_PIDS)
1331 pid_list = rcu_dereference_sched(tr->filtered_pids);
1332 else
1333 pid_list = rcu_dereference_sched(tr->filtered_no_pids);
1334
1335 if (!pid_list)
1336 return NULL;
1337
1338 return trace_pid_start(pid_list, pos);
1339 }
1340
1341 static void *p_start(struct seq_file *m, loff_t *pos)
1342 __acquires(RCU)
1343 {
1344 return __start(m, pos, TRACE_PIDS);
1345 }
1346
1347 static void *np_start(struct seq_file *m, loff_t *pos)
1348 __acquires(RCU)
1349 {
1350 return __start(m, pos, TRACE_NO_PIDS);
1351 }
1352
1353 static void p_stop(struct seq_file *m, void *p)
1354 __releases(RCU)
1355 {
1356 rcu_read_unlock_sched();
1357 mutex_unlock(&event_mutex);
1358 }
1359
1360 static ssize_t
1361 event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1362 loff_t *ppos)
1363 {
1364 struct trace_event_file *file;
1365 unsigned long flags;
1366 char buf[4] = "0";
1367
1368 mutex_lock(&event_mutex);
1369 file = event_file_data(filp);
1370 if (likely(file))
1371 flags = file->flags;
1372 mutex_unlock(&event_mutex);
1373
1374 if (!file)
1375 return -ENODEV;
1376
1377 if (flags & EVENT_FILE_FL_ENABLED &&
1378 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
1379 strcpy(buf, "1");
1380
1381 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1382 flags & EVENT_FILE_FL_SOFT_MODE)
1383 strcat(buf, "*");
1384
1385 strcat(buf, "\n");
1386
1387 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
1388 }
1389
1390 static ssize_t
1391 event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1392 loff_t *ppos)
1393 {
1394 struct trace_event_file *file;
1395 unsigned long val;
1396 int ret;
1397
1398 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1399 if (ret)
1400 return ret;
1401
1402 ret = tracing_update_buffers();
1403 if (ret < 0)
1404 return ret;
1405
1406 switch (val) {
1407 case 0:
1408 case 1:
1409 ret = -ENODEV;
1410 mutex_lock(&event_mutex);
1411 file = event_file_data(filp);
1412 if (likely(file))
1413 ret = ftrace_event_enable_disable(file, val);
1414 mutex_unlock(&event_mutex);
1415 break;
1416
1417 default:
1418 return -EINVAL;
1419 }
1420
1421 *ppos += cnt;
1422
1423 return ret ? ret : cnt;
1424 }
1425
1426 static ssize_t
1427 system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1428 loff_t *ppos)
1429 {
1430 const char set_to_char[4] = { '?', '0', '1', 'X' };
1431 struct trace_subsystem_dir *dir = filp->private_data;
1432 struct event_subsystem *system = dir->subsystem;
1433 struct trace_event_call *call;
1434 struct trace_event_file *file;
1435 struct trace_array *tr = dir->tr;
1436 char buf[2];
1437 int set = 0;
1438 int ret;
1439
1440 mutex_lock(&event_mutex);
1441 list_for_each_entry(file, &tr->events, list) {
1442 call = file->event_call;
1443 if ((call->flags & TRACE_EVENT_FL_IGNORE_ENABLE) ||
1444 !trace_event_name(call) || !call->class || !call->class->reg)
1445 continue;
1446
1447 if (system && strcmp(call->class->system, system->name) != 0)
1448 continue;
1449
1450
1451
1452
1453
1454
1455 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
1456
1457
1458
1459
1460 if (set == 3)
1461 break;
1462 }
1463 mutex_unlock(&event_mutex);
1464
1465 buf[0] = set_to_char[set];
1466 buf[1] = '\n';
1467
1468 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1469
1470 return ret;
1471 }
1472
1473 static ssize_t
1474 system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1475 loff_t *ppos)
1476 {
1477 struct trace_subsystem_dir *dir = filp->private_data;
1478 struct event_subsystem *system = dir->subsystem;
1479 const char *name = NULL;
1480 unsigned long val;
1481 ssize_t ret;
1482
1483 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1484 if (ret)
1485 return ret;
1486
1487 ret = tracing_update_buffers();
1488 if (ret < 0)
1489 return ret;
1490
1491 if (val != 0 && val != 1)
1492 return -EINVAL;
1493
1494
1495
1496
1497
1498 if (system)
1499 name = system->name;
1500
1501 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
1502 if (ret)
1503 goto out;
1504
1505 ret = cnt;
1506
1507 out:
1508 *ppos += cnt;
1509
1510 return ret;
1511 }
1512
1513 enum {
1514 FORMAT_HEADER = 1,
1515 FORMAT_FIELD_SEPERATOR = 2,
1516 FORMAT_PRINTFMT = 3,
1517 };
1518
1519 static void *f_next(struct seq_file *m, void *v, loff_t *pos)
1520 {
1521 struct trace_event_call *call = event_file_data(m->private);
1522 struct list_head *common_head = &ftrace_common_fields;
1523 struct list_head *head = trace_get_fields(call);
1524 struct list_head *node = v;
1525
1526 (*pos)++;
1527
1528 switch ((unsigned long)v) {
1529 case FORMAT_HEADER:
1530 node = common_head;
1531 break;
1532
1533 case FORMAT_FIELD_SEPERATOR:
1534 node = head;
1535 break;
1536
1537 case FORMAT_PRINTFMT:
1538
1539 return NULL;
1540 }
1541
1542 node = node->prev;
1543 if (node == common_head)
1544 return (void *)FORMAT_FIELD_SEPERATOR;
1545 else if (node == head)
1546 return (void *)FORMAT_PRINTFMT;
1547 else
1548 return node;
1549 }
1550
1551 static int f_show(struct seq_file *m, void *v)
1552 {
1553 struct trace_event_call *call = event_file_data(m->private);
1554 struct ftrace_event_field *field;
1555 const char *array_descriptor;
1556
1557 switch ((unsigned long)v) {
1558 case FORMAT_HEADER:
1559 seq_printf(m, "name: %s\n", trace_event_name(call));
1560 seq_printf(m, "ID: %d\n", call->event.type);
1561 seq_puts(m, "format:\n");
1562 return 0;
1563
1564 case FORMAT_FIELD_SEPERATOR:
1565 seq_putc(m, '\n');
1566 return 0;
1567
1568 case FORMAT_PRINTFMT:
1569 seq_printf(m, "\nprint fmt: %s\n",
1570 call->print_fmt);
1571 return 0;
1572 }
1573
1574 field = list_entry(v, struct ftrace_event_field, link);
1575
1576
1577
1578
1579
1580
1581
1582 array_descriptor = strchr(field->type, '[');
1583
1584 if (str_has_prefix(field->type, "__data_loc"))
1585 array_descriptor = NULL;
1586
1587 if (!array_descriptor)
1588 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1589 field->type, field->name, field->offset,
1590 field->size, !!field->is_signed);
1591 else
1592 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1593 (int)(array_descriptor - field->type),
1594 field->type, field->name,
1595 array_descriptor, field->offset,
1596 field->size, !!field->is_signed);
1597
1598 return 0;
1599 }
1600
1601 static void *f_start(struct seq_file *m, loff_t *pos)
1602 {
1603 void *p = (void *)FORMAT_HEADER;
1604 loff_t l = 0;
1605
1606
1607 mutex_lock(&event_mutex);
1608 if (!event_file_data(m->private))
1609 return ERR_PTR(-ENODEV);
1610
1611 while (l < *pos && p)
1612 p = f_next(m, p, &l);
1613
1614 return p;
1615 }
1616
1617 static void f_stop(struct seq_file *m, void *p)
1618 {
1619 mutex_unlock(&event_mutex);
1620 }
1621
1622 static const struct seq_operations trace_format_seq_ops = {
1623 .start = f_start,
1624 .next = f_next,
1625 .stop = f_stop,
1626 .show = f_show,
1627 };
1628
1629 static int trace_format_open(struct inode *inode, struct file *file)
1630 {
1631 struct seq_file *m;
1632 int ret;
1633
1634
1635
1636 ret = seq_open(file, &trace_format_seq_ops);
1637 if (ret < 0)
1638 return ret;
1639
1640 m = file->private_data;
1641 m->private = file;
1642
1643 return 0;
1644 }
1645
1646 static ssize_t
1647 event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1648 {
1649 int id = (long)event_file_data(filp);
1650 char buf[32];
1651 int len;
1652
1653 if (unlikely(!id))
1654 return -ENODEV;
1655
1656 len = sprintf(buf, "%d\n", id);
1657
1658 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1659 }
1660
1661 static ssize_t
1662 event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1663 loff_t *ppos)
1664 {
1665 struct trace_event_file *file;
1666 struct trace_seq *s;
1667 int r = -ENODEV;
1668
1669 if (*ppos)
1670 return 0;
1671
1672 s = kmalloc(sizeof(*s), GFP_KERNEL);
1673
1674 if (!s)
1675 return -ENOMEM;
1676
1677 trace_seq_init(s);
1678
1679 mutex_lock(&event_mutex);
1680 file = event_file_data(filp);
1681 if (file)
1682 print_event_filter(file, s);
1683 mutex_unlock(&event_mutex);
1684
1685 if (file)
1686 r = simple_read_from_buffer(ubuf, cnt, ppos,
1687 s->buffer, trace_seq_used(s));
1688
1689 kfree(s);
1690
1691 return r;
1692 }
1693
1694 static ssize_t
1695 event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1696 loff_t *ppos)
1697 {
1698 struct trace_event_file *file;
1699 char *buf;
1700 int err = -ENODEV;
1701
1702 if (cnt >= PAGE_SIZE)
1703 return -EINVAL;
1704
1705 buf = memdup_user_nul(ubuf, cnt);
1706 if (IS_ERR(buf))
1707 return PTR_ERR(buf);
1708
1709 mutex_lock(&event_mutex);
1710 file = event_file_data(filp);
1711 if (file)
1712 err = apply_event_filter(file, buf);
1713 mutex_unlock(&event_mutex);
1714
1715 kfree(buf);
1716 if (err < 0)
1717 return err;
1718
1719 *ppos += cnt;
1720
1721 return cnt;
1722 }
1723
1724 static LIST_HEAD(event_subsystems);
1725
1726 static int subsystem_open(struct inode *inode, struct file *filp)
1727 {
1728 struct trace_subsystem_dir *dir = NULL, *iter_dir;
1729 struct trace_array *tr = NULL, *iter_tr;
1730 struct event_subsystem *system = NULL;
1731 int ret;
1732
1733 if (tracing_is_disabled())
1734 return -ENODEV;
1735
1736
1737 mutex_lock(&event_mutex);
1738 mutex_lock(&trace_types_lock);
1739 list_for_each_entry(iter_tr, &ftrace_trace_arrays, list) {
1740 list_for_each_entry(iter_dir, &iter_tr->systems, list) {
1741 if (iter_dir == inode->i_private) {
1742
1743 tr = iter_tr;
1744 dir = iter_dir;
1745 if (dir->nr_events) {
1746 __get_system_dir(dir);
1747 system = dir->subsystem;
1748 }
1749 goto exit_loop;
1750 }
1751 }
1752 }
1753 exit_loop:
1754 mutex_unlock(&trace_types_lock);
1755 mutex_unlock(&event_mutex);
1756
1757 if (!system)
1758 return -ENODEV;
1759
1760
1761 if (trace_array_get(tr) < 0) {
1762 put_system(dir);
1763 return -ENODEV;
1764 }
1765
1766 ret = tracing_open_generic(inode, filp);
1767 if (ret < 0) {
1768 trace_array_put(tr);
1769 put_system(dir);
1770 }
1771
1772 return ret;
1773 }
1774
1775 static int system_tr_open(struct inode *inode, struct file *filp)
1776 {
1777 struct trace_subsystem_dir *dir;
1778 struct trace_array *tr = inode->i_private;
1779 int ret;
1780
1781
1782 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
1783 if (!dir)
1784 return -ENOMEM;
1785
1786 ret = tracing_open_generic_tr(inode, filp);
1787 if (ret < 0) {
1788 kfree(dir);
1789 return ret;
1790 }
1791 dir->tr = tr;
1792 filp->private_data = dir;
1793
1794 return 0;
1795 }
1796
1797 static int subsystem_release(struct inode *inode, struct file *file)
1798 {
1799 struct trace_subsystem_dir *dir = file->private_data;
1800
1801 trace_array_put(dir->tr);
1802
1803
1804
1805
1806
1807
1808 if (dir->subsystem)
1809 put_system(dir);
1810 else
1811 kfree(dir);
1812
1813 return 0;
1814 }
1815
1816 static ssize_t
1817 subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1818 loff_t *ppos)
1819 {
1820 struct trace_subsystem_dir *dir = filp->private_data;
1821 struct event_subsystem *system = dir->subsystem;
1822 struct trace_seq *s;
1823 int r;
1824
1825 if (*ppos)
1826 return 0;
1827
1828 s = kmalloc(sizeof(*s), GFP_KERNEL);
1829 if (!s)
1830 return -ENOMEM;
1831
1832 trace_seq_init(s);
1833
1834 print_subsystem_event_filter(system, s);
1835 r = simple_read_from_buffer(ubuf, cnt, ppos,
1836 s->buffer, trace_seq_used(s));
1837
1838 kfree(s);
1839
1840 return r;
1841 }
1842
1843 static ssize_t
1844 subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1845 loff_t *ppos)
1846 {
1847 struct trace_subsystem_dir *dir = filp->private_data;
1848 char *buf;
1849 int err;
1850
1851 if (cnt >= PAGE_SIZE)
1852 return -EINVAL;
1853
1854 buf = memdup_user_nul(ubuf, cnt);
1855 if (IS_ERR(buf))
1856 return PTR_ERR(buf);
1857
1858 err = apply_subsystem_event_filter(dir, buf);
1859 kfree(buf);
1860 if (err < 0)
1861 return err;
1862
1863 *ppos += cnt;
1864
1865 return cnt;
1866 }
1867
1868 static ssize_t
1869 show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1870 {
1871 int (*func)(struct trace_seq *s) = filp->private_data;
1872 struct trace_seq *s;
1873 int r;
1874
1875 if (*ppos)
1876 return 0;
1877
1878 s = kmalloc(sizeof(*s), GFP_KERNEL);
1879 if (!s)
1880 return -ENOMEM;
1881
1882 trace_seq_init(s);
1883
1884 func(s);
1885 r = simple_read_from_buffer(ubuf, cnt, ppos,
1886 s->buffer, trace_seq_used(s));
1887
1888 kfree(s);
1889
1890 return r;
1891 }
1892
1893 static void ignore_task_cpu(void *data)
1894 {
1895 struct trace_array *tr = data;
1896 struct trace_pid_list *pid_list;
1897 struct trace_pid_list *no_pid_list;
1898
1899
1900
1901
1902
1903 pid_list = rcu_dereference_protected(tr->filtered_pids,
1904 mutex_is_locked(&event_mutex));
1905 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
1906 mutex_is_locked(&event_mutex));
1907
1908 this_cpu_write(tr->array_buffer.data->ignore_pid,
1909 trace_ignore_this_task(pid_list, no_pid_list, current));
1910 }
1911
1912 static void register_pid_events(struct trace_array *tr)
1913 {
1914
1915
1916
1917
1918
1919
1920 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1921 tr, INT_MAX);
1922 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1923 tr, 0);
1924
1925 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1926 tr, INT_MAX);
1927 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1928 tr, 0);
1929
1930 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1931 tr, INT_MAX);
1932 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1933 tr, 0);
1934
1935 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1936 tr, INT_MAX);
1937 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1938 tr, 0);
1939 }
1940
1941 static ssize_t
1942 event_pid_write(struct file *filp, const char __user *ubuf,
1943 size_t cnt, loff_t *ppos, int type)
1944 {
1945 struct seq_file *m = filp->private_data;
1946 struct trace_array *tr = m->private;
1947 struct trace_pid_list *filtered_pids = NULL;
1948 struct trace_pid_list *other_pids = NULL;
1949 struct trace_pid_list *pid_list;
1950 struct trace_event_file *file;
1951 ssize_t ret;
1952
1953 if (!cnt)
1954 return 0;
1955
1956 ret = tracing_update_buffers();
1957 if (ret < 0)
1958 return ret;
1959
1960 mutex_lock(&event_mutex);
1961
1962 if (type == TRACE_PIDS) {
1963 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1964 lockdep_is_held(&event_mutex));
1965 other_pids = rcu_dereference_protected(tr->filtered_no_pids,
1966 lockdep_is_held(&event_mutex));
1967 } else {
1968 filtered_pids = rcu_dereference_protected(tr->filtered_no_pids,
1969 lockdep_is_held(&event_mutex));
1970 other_pids = rcu_dereference_protected(tr->filtered_pids,
1971 lockdep_is_held(&event_mutex));
1972 }
1973
1974 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1975 if (ret < 0)
1976 goto out;
1977
1978 if (type == TRACE_PIDS)
1979 rcu_assign_pointer(tr->filtered_pids, pid_list);
1980 else
1981 rcu_assign_pointer(tr->filtered_no_pids, pid_list);
1982
1983 list_for_each_entry(file, &tr->events, list) {
1984 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1985 }
1986
1987 if (filtered_pids) {
1988 tracepoint_synchronize_unregister();
1989 trace_pid_list_free(filtered_pids);
1990 } else if (pid_list && !other_pids) {
1991 register_pid_events(tr);
1992 }
1993
1994
1995
1996
1997
1998
1999 on_each_cpu(ignore_task_cpu, tr, 1);
2000
2001 out:
2002 mutex_unlock(&event_mutex);
2003
2004 if (ret > 0)
2005 *ppos += ret;
2006
2007 return ret;
2008 }
2009
2010 static ssize_t
2011 ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
2012 size_t cnt, loff_t *ppos)
2013 {
2014 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_PIDS);
2015 }
2016
2017 static ssize_t
2018 ftrace_event_npid_write(struct file *filp, const char __user *ubuf,
2019 size_t cnt, loff_t *ppos)
2020 {
2021 return event_pid_write(filp, ubuf, cnt, ppos, TRACE_NO_PIDS);
2022 }
2023
2024 static int ftrace_event_avail_open(struct inode *inode, struct file *file);
2025 static int ftrace_event_set_open(struct inode *inode, struct file *file);
2026 static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
2027 static int ftrace_event_set_npid_open(struct inode *inode, struct file *file);
2028 static int ftrace_event_release(struct inode *inode, struct file *file);
2029
2030 static const struct seq_operations show_event_seq_ops = {
2031 .start = t_start,
2032 .next = t_next,
2033 .show = t_show,
2034 .stop = t_stop,
2035 };
2036
2037 static const struct seq_operations show_set_event_seq_ops = {
2038 .start = s_start,
2039 .next = s_next,
2040 .show = t_show,
2041 .stop = t_stop,
2042 };
2043
2044 static const struct seq_operations show_set_pid_seq_ops = {
2045 .start = p_start,
2046 .next = p_next,
2047 .show = trace_pid_show,
2048 .stop = p_stop,
2049 };
2050
2051 static const struct seq_operations show_set_no_pid_seq_ops = {
2052 .start = np_start,
2053 .next = np_next,
2054 .show = trace_pid_show,
2055 .stop = p_stop,
2056 };
2057
2058 static const struct file_operations ftrace_avail_fops = {
2059 .open = ftrace_event_avail_open,
2060 .read = seq_read,
2061 .llseek = seq_lseek,
2062 .release = seq_release,
2063 };
2064
2065 static const struct file_operations ftrace_set_event_fops = {
2066 .open = ftrace_event_set_open,
2067 .read = seq_read,
2068 .write = ftrace_event_write,
2069 .llseek = seq_lseek,
2070 .release = ftrace_event_release,
2071 };
2072
2073 static const struct file_operations ftrace_set_event_pid_fops = {
2074 .open = ftrace_event_set_pid_open,
2075 .read = seq_read,
2076 .write = ftrace_event_pid_write,
2077 .llseek = seq_lseek,
2078 .release = ftrace_event_release,
2079 };
2080
2081 static const struct file_operations ftrace_set_event_notrace_pid_fops = {
2082 .open = ftrace_event_set_npid_open,
2083 .read = seq_read,
2084 .write = ftrace_event_npid_write,
2085 .llseek = seq_lseek,
2086 .release = ftrace_event_release,
2087 };
2088
2089 static const struct file_operations ftrace_enable_fops = {
2090 .open = tracing_open_generic,
2091 .read = event_enable_read,
2092 .write = event_enable_write,
2093 .llseek = default_llseek,
2094 };
2095
2096 static const struct file_operations ftrace_event_format_fops = {
2097 .open = trace_format_open,
2098 .read = seq_read,
2099 .llseek = seq_lseek,
2100 .release = seq_release,
2101 };
2102
2103 static const struct file_operations ftrace_event_id_fops = {
2104 .read = event_id_read,
2105 .llseek = default_llseek,
2106 };
2107
2108 static const struct file_operations ftrace_event_filter_fops = {
2109 .open = tracing_open_generic,
2110 .read = event_filter_read,
2111 .write = event_filter_write,
2112 .llseek = default_llseek,
2113 };
2114
2115 static const struct file_operations ftrace_subsystem_filter_fops = {
2116 .open = subsystem_open,
2117 .read = subsystem_filter_read,
2118 .write = subsystem_filter_write,
2119 .llseek = default_llseek,
2120 .release = subsystem_release,
2121 };
2122
2123 static const struct file_operations ftrace_system_enable_fops = {
2124 .open = subsystem_open,
2125 .read = system_enable_read,
2126 .write = system_enable_write,
2127 .llseek = default_llseek,
2128 .release = subsystem_release,
2129 };
2130
2131 static const struct file_operations ftrace_tr_enable_fops = {
2132 .open = system_tr_open,
2133 .read = system_enable_read,
2134 .write = system_enable_write,
2135 .llseek = default_llseek,
2136 .release = subsystem_release,
2137 };
2138
2139 static const struct file_operations ftrace_show_header_fops = {
2140 .open = tracing_open_generic,
2141 .read = show_header,
2142 .llseek = default_llseek,
2143 };
2144
2145 static int
2146 ftrace_event_open(struct inode *inode, struct file *file,
2147 const struct seq_operations *seq_ops)
2148 {
2149 struct seq_file *m;
2150 int ret;
2151
2152 ret = security_locked_down(LOCKDOWN_TRACEFS);
2153 if (ret)
2154 return ret;
2155
2156 ret = seq_open(file, seq_ops);
2157 if (ret < 0)
2158 return ret;
2159 m = file->private_data;
2160
2161 m->private = inode->i_private;
2162
2163 return ret;
2164 }
2165
2166 static int ftrace_event_release(struct inode *inode, struct file *file)
2167 {
2168 struct trace_array *tr = inode->i_private;
2169
2170 trace_array_put(tr);
2171
2172 return seq_release(inode, file);
2173 }
2174
2175 static int
2176 ftrace_event_avail_open(struct inode *inode, struct file *file)
2177 {
2178 const struct seq_operations *seq_ops = &show_event_seq_ops;
2179
2180
2181 return ftrace_event_open(inode, file, seq_ops);
2182 }
2183
2184 static int
2185 ftrace_event_set_open(struct inode *inode, struct file *file)
2186 {
2187 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
2188 struct trace_array *tr = inode->i_private;
2189 int ret;
2190
2191 ret = tracing_check_open_get_tr(tr);
2192 if (ret)
2193 return ret;
2194
2195 if ((file->f_mode & FMODE_WRITE) &&
2196 (file->f_flags & O_TRUNC))
2197 ftrace_clear_events(tr);
2198
2199 ret = ftrace_event_open(inode, file, seq_ops);
2200 if (ret < 0)
2201 trace_array_put(tr);
2202 return ret;
2203 }
2204
2205 static int
2206 ftrace_event_set_pid_open(struct inode *inode, struct file *file)
2207 {
2208 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
2209 struct trace_array *tr = inode->i_private;
2210 int ret;
2211
2212 ret = tracing_check_open_get_tr(tr);
2213 if (ret)
2214 return ret;
2215
2216 if ((file->f_mode & FMODE_WRITE) &&
2217 (file->f_flags & O_TRUNC))
2218 ftrace_clear_event_pids(tr, TRACE_PIDS);
2219
2220 ret = ftrace_event_open(inode, file, seq_ops);
2221 if (ret < 0)
2222 trace_array_put(tr);
2223 return ret;
2224 }
2225
2226 static int
2227 ftrace_event_set_npid_open(struct inode *inode, struct file *file)
2228 {
2229 const struct seq_operations *seq_ops = &show_set_no_pid_seq_ops;
2230 struct trace_array *tr = inode->i_private;
2231 int ret;
2232
2233 ret = tracing_check_open_get_tr(tr);
2234 if (ret)
2235 return ret;
2236
2237 if ((file->f_mode & FMODE_WRITE) &&
2238 (file->f_flags & O_TRUNC))
2239 ftrace_clear_event_pids(tr, TRACE_NO_PIDS);
2240
2241 ret = ftrace_event_open(inode, file, seq_ops);
2242 if (ret < 0)
2243 trace_array_put(tr);
2244 return ret;
2245 }
2246
2247 static struct event_subsystem *
2248 create_new_subsystem(const char *name)
2249 {
2250 struct event_subsystem *system;
2251
2252
2253 system = kmalloc(sizeof(*system), GFP_KERNEL);
2254 if (!system)
2255 return NULL;
2256
2257 system->ref_count = 1;
2258
2259
2260 system->name = kstrdup_const(name, GFP_KERNEL);
2261 if (!system->name)
2262 goto out_free;
2263
2264 system->filter = NULL;
2265
2266 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
2267 if (!system->filter)
2268 goto out_free;
2269
2270 list_add(&system->list, &event_subsystems);
2271
2272 return system;
2273
2274 out_free:
2275 kfree_const(system->name);
2276 kfree(system);
2277 return NULL;
2278 }
2279
2280 static struct dentry *
2281 event_subsystem_dir(struct trace_array *tr, const char *name,
2282 struct trace_event_file *file, struct dentry *parent)
2283 {
2284 struct event_subsystem *system, *iter;
2285 struct trace_subsystem_dir *dir;
2286 struct dentry *entry;
2287
2288
2289 list_for_each_entry(dir, &tr->systems, list) {
2290 system = dir->subsystem;
2291 if (strcmp(system->name, name) == 0) {
2292 dir->nr_events++;
2293 file->system = dir;
2294 return dir->entry;
2295 }
2296 }
2297
2298
2299 system = NULL;
2300 list_for_each_entry(iter, &event_subsystems, list) {
2301 if (strcmp(iter->name, name) == 0) {
2302 system = iter;
2303 break;
2304 }
2305 }
2306
2307 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
2308 if (!dir)
2309 goto out_fail;
2310
2311 if (!system) {
2312 system = create_new_subsystem(name);
2313 if (!system)
2314 goto out_free;
2315 } else
2316 __get_system(system);
2317
2318 dir->entry = tracefs_create_dir(name, parent);
2319 if (!dir->entry) {
2320 pr_warn("Failed to create system directory %s\n", name);
2321 __put_system(system);
2322 goto out_free;
2323 }
2324
2325 dir->tr = tr;
2326 dir->ref_count = 1;
2327 dir->nr_events = 1;
2328 dir->subsystem = system;
2329 file->system = dir;
2330
2331
2332 if (strcmp(name, "ftrace") != 0) {
2333
2334 entry = tracefs_create_file("filter", TRACE_MODE_WRITE,
2335 dir->entry, dir,
2336 &ftrace_subsystem_filter_fops);
2337 if (!entry) {
2338 kfree(system->filter);
2339 system->filter = NULL;
2340 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
2341 }
2342
2343 trace_create_file("enable", TRACE_MODE_WRITE, dir->entry, dir,
2344 &ftrace_system_enable_fops);
2345 }
2346
2347 list_add(&dir->list, &tr->systems);
2348
2349 return dir->entry;
2350
2351 out_free:
2352 kfree(dir);
2353 out_fail:
2354
2355 if (!dir || !system)
2356 pr_warn("No memory to create event subsystem %s\n", name);
2357 return NULL;
2358 }
2359
2360 static int
2361 event_define_fields(struct trace_event_call *call)
2362 {
2363 struct list_head *head;
2364 int ret = 0;
2365
2366
2367
2368
2369
2370 head = trace_get_fields(call);
2371 if (list_empty(head)) {
2372 struct trace_event_fields *field = call->class->fields_array;
2373 unsigned int offset = sizeof(struct trace_entry);
2374
2375 for (; field->type; field++) {
2376 if (field->type == TRACE_FUNCTION_TYPE) {
2377 field->define_fields(call);
2378 break;
2379 }
2380
2381 offset = ALIGN(offset, field->align);
2382 ret = trace_define_field(call, field->type, field->name,
2383 offset, field->size,
2384 field->is_signed, field->filter_type);
2385 if (WARN_ON_ONCE(ret)) {
2386 pr_err("error code is %d\n", ret);
2387 break;
2388 }
2389
2390 offset += field->size;
2391 }
2392 }
2393
2394 return ret;
2395 }
2396
2397 static int
2398 event_create_dir(struct dentry *parent, struct trace_event_file *file)
2399 {
2400 struct trace_event_call *call = file->event_call;
2401 struct trace_array *tr = file->tr;
2402 struct dentry *d_events;
2403 const char *name;
2404 int ret;
2405
2406
2407
2408
2409
2410 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
2411 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
2412 if (!d_events)
2413 return -ENOMEM;
2414 } else
2415 d_events = parent;
2416
2417 name = trace_event_name(call);
2418 file->dir = tracefs_create_dir(name, d_events);
2419 if (!file->dir) {
2420 pr_warn("Could not create tracefs '%s' directory\n", name);
2421 return -1;
2422 }
2423
2424 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
2425 trace_create_file("enable", TRACE_MODE_WRITE, file->dir, file,
2426 &ftrace_enable_fops);
2427
2428 #ifdef CONFIG_PERF_EVENTS
2429 if (call->event.type && call->class->reg)
2430 trace_create_file("id", TRACE_MODE_READ, file->dir,
2431 (void *)(long)call->event.type,
2432 &ftrace_event_id_fops);
2433 #endif
2434
2435 ret = event_define_fields(call);
2436 if (ret < 0) {
2437 pr_warn("Could not initialize trace point events/%s\n", name);
2438 return ret;
2439 }
2440
2441
2442
2443
2444
2445 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2446 trace_create_file("filter", TRACE_MODE_WRITE, file->dir,
2447 file, &ftrace_event_filter_fops);
2448
2449 trace_create_file("trigger", TRACE_MODE_WRITE, file->dir,
2450 file, &event_trigger_fops);
2451 }
2452
2453 #ifdef CONFIG_HIST_TRIGGERS
2454 trace_create_file("hist", TRACE_MODE_READ, file->dir, file,
2455 &event_hist_fops);
2456 #endif
2457 #ifdef CONFIG_HIST_TRIGGERS_DEBUG
2458 trace_create_file("hist_debug", TRACE_MODE_READ, file->dir, file,
2459 &event_hist_debug_fops);
2460 #endif
2461 trace_create_file("format", TRACE_MODE_READ, file->dir, call,
2462 &ftrace_event_format_fops);
2463
2464 #ifdef CONFIG_TRACE_EVENT_INJECT
2465 if (call->event.type && call->class->reg)
2466 trace_create_file("inject", 0200, file->dir, file,
2467 &event_inject_fops);
2468 #endif
2469
2470 return 0;
2471 }
2472
2473 static void remove_event_from_tracers(struct trace_event_call *call)
2474 {
2475 struct trace_event_file *file;
2476 struct trace_array *tr;
2477
2478 do_for_each_event_file_safe(tr, file) {
2479 if (file->event_call != call)
2480 continue;
2481
2482 remove_event_file_dir(file);
2483
2484
2485
2486
2487
2488
2489 break;
2490 } while_for_each_event_file();
2491 }
2492
2493 static void event_remove(struct trace_event_call *call)
2494 {
2495 struct trace_array *tr;
2496 struct trace_event_file *file;
2497
2498 do_for_each_event_file(tr, file) {
2499 if (file->event_call != call)
2500 continue;
2501
2502 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2503 tr->clear_trace = true;
2504
2505 ftrace_event_enable_disable(file, 0);
2506
2507
2508
2509
2510
2511
2512 break;
2513 } while_for_each_event_file();
2514
2515 if (call->event.funcs)
2516 __unregister_trace_event(&call->event);
2517 remove_event_from_tracers(call);
2518 list_del(&call->list);
2519 }
2520
2521 static int event_init(struct trace_event_call *call)
2522 {
2523 int ret = 0;
2524 const char *name;
2525
2526 name = trace_event_name(call);
2527 if (WARN_ON(!name))
2528 return -EINVAL;
2529
2530 if (call->class->raw_init) {
2531 ret = call->class->raw_init(call);
2532 if (ret < 0 && ret != -ENOSYS)
2533 pr_warn("Could not initialize trace events/%s\n", name);
2534 }
2535
2536 return ret;
2537 }
2538
2539 static int
2540 __register_event(struct trace_event_call *call, struct module *mod)
2541 {
2542 int ret;
2543
2544 ret = event_init(call);
2545 if (ret < 0)
2546 return ret;
2547
2548 list_add(&call->list, &ftrace_events);
2549 if (call->flags & TRACE_EVENT_FL_DYNAMIC)
2550 atomic_set(&call->refcnt, 0);
2551 else
2552 call->module = mod;
2553
2554 return 0;
2555 }
2556
2557 static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
2558 {
2559 int rlen;
2560 int elen;
2561
2562
2563 elen = snprintf(ptr, 0, "%ld", map->eval_value);
2564
2565 if (len < elen)
2566 return NULL;
2567
2568 snprintf(ptr, elen + 1, "%ld", map->eval_value);
2569
2570
2571 rlen = strlen(ptr + len);
2572 memmove(ptr + elen, ptr + len, rlen);
2573
2574 ptr[elen + rlen] = 0;
2575
2576 return ptr + elen;
2577 }
2578
2579 static void update_event_printk(struct trace_event_call *call,
2580 struct trace_eval_map *map)
2581 {
2582 char *ptr;
2583 int quote = 0;
2584 int len = strlen(map->eval_string);
2585
2586 for (ptr = call->print_fmt; *ptr; ptr++) {
2587 if (*ptr == '\\') {
2588 ptr++;
2589
2590 if (!*ptr)
2591 break;
2592 continue;
2593 }
2594 if (*ptr == '"') {
2595 quote ^= 1;
2596 continue;
2597 }
2598 if (quote)
2599 continue;
2600 if (isdigit(*ptr)) {
2601
2602 do {
2603 ptr++;
2604
2605 } while (isalnum(*ptr));
2606 if (!*ptr)
2607 break;
2608
2609
2610
2611
2612 continue;
2613 }
2614 if (isalpha(*ptr) || *ptr == '_') {
2615 if (strncmp(map->eval_string, ptr, len) == 0 &&
2616 !isalnum(ptr[len]) && ptr[len] != '_') {
2617 ptr = eval_replace(ptr, map, len);
2618
2619 if (WARN_ON_ONCE(!ptr))
2620 return;
2621
2622
2623
2624
2625
2626
2627
2628 continue;
2629 }
2630 skip_more:
2631 do {
2632 ptr++;
2633 } while (isalnum(*ptr) || *ptr == '_');
2634 if (!*ptr)
2635 break;
2636
2637
2638
2639
2640 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2641 ptr += *ptr == '.' ? 1 : 2;
2642 if (!*ptr)
2643 break;
2644 goto skip_more;
2645 }
2646
2647
2648
2649
2650 continue;
2651 }
2652 }
2653 }
2654
2655 static void add_str_to_module(struct module *module, char *str)
2656 {
2657 struct module_string *modstr;
2658
2659 modstr = kmalloc(sizeof(*modstr), GFP_KERNEL);
2660
2661
2662
2663
2664
2665
2666
2667 if (WARN_ON_ONCE(!modstr))
2668 return;
2669
2670 modstr->module = module;
2671 modstr->str = str;
2672
2673 list_add(&modstr->next, &module_strings);
2674 }
2675
2676 static void update_event_fields(struct trace_event_call *call,
2677 struct trace_eval_map *map)
2678 {
2679 struct ftrace_event_field *field;
2680 struct list_head *head;
2681 char *ptr;
2682 char *str;
2683 int len = strlen(map->eval_string);
2684
2685
2686 if (WARN_ON_ONCE(call->flags & TRACE_EVENT_FL_DYNAMIC))
2687 return;
2688
2689 head = trace_get_fields(call);
2690 list_for_each_entry(field, head, link) {
2691 ptr = strchr(field->type, '[');
2692 if (!ptr)
2693 continue;
2694 ptr++;
2695
2696 if (!isalpha(*ptr) && *ptr != '_')
2697 continue;
2698
2699 if (strncmp(map->eval_string, ptr, len) != 0)
2700 continue;
2701
2702 str = kstrdup(field->type, GFP_KERNEL);
2703 if (WARN_ON_ONCE(!str))
2704 return;
2705 ptr = str + (ptr - field->type);
2706 ptr = eval_replace(ptr, map, len);
2707
2708 if (WARN_ON_ONCE(!ptr)) {
2709 kfree(str);
2710 continue;
2711 }
2712
2713
2714
2715
2716
2717
2718 if (call->module)
2719 add_str_to_module(call->module, str);
2720
2721 field->type = str;
2722 }
2723 }
2724
2725 void trace_event_eval_update(struct trace_eval_map **map, int len)
2726 {
2727 struct trace_event_call *call, *p;
2728 const char *last_system = NULL;
2729 bool first = false;
2730 int last_i;
2731 int i;
2732
2733 down_write(&trace_event_sem);
2734 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2735
2736 if (!last_system || call->class->system != last_system) {
2737 first = true;
2738 last_i = 0;
2739 last_system = call->class->system;
2740 }
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752 for (i = last_i; i < len; i++) {
2753 if (call->class->system == map[i]->system) {
2754
2755 if (first) {
2756 last_i = i;
2757 first = false;
2758 }
2759 update_event_printk(call, map[i]);
2760 update_event_fields(call, map[i]);
2761 }
2762 }
2763 }
2764 up_write(&trace_event_sem);
2765 }
2766
2767 static struct trace_event_file *
2768 trace_create_new_event(struct trace_event_call *call,
2769 struct trace_array *tr)
2770 {
2771 struct trace_pid_list *no_pid_list;
2772 struct trace_pid_list *pid_list;
2773 struct trace_event_file *file;
2774 unsigned int first;
2775
2776 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2777 if (!file)
2778 return NULL;
2779
2780 pid_list = rcu_dereference_protected(tr->filtered_pids,
2781 lockdep_is_held(&event_mutex));
2782 no_pid_list = rcu_dereference_protected(tr->filtered_no_pids,
2783 lockdep_is_held(&event_mutex));
2784
2785 if (!trace_pid_list_first(pid_list, &first) ||
2786 !trace_pid_list_first(no_pid_list, &first))
2787 file->flags |= EVENT_FILE_FL_PID_FILTER;
2788
2789 file->event_call = call;
2790 file->tr = tr;
2791 atomic_set(&file->sm_ref, 0);
2792 atomic_set(&file->tm_ref, 0);
2793 INIT_LIST_HEAD(&file->triggers);
2794 list_add(&file->list, &tr->events);
2795
2796 return file;
2797 }
2798
2799
2800 static int
2801 __trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
2802 {
2803 struct trace_event_file *file;
2804
2805 file = trace_create_new_event(call, tr);
2806 if (!file)
2807 return -ENOMEM;
2808
2809 if (eventdir_initialized)
2810 return event_create_dir(tr->event_dir, file);
2811 else
2812 return event_define_fields(call);
2813 }
2814
2815
2816
2817
2818
2819
2820 static int
2821 __trace_early_add_new_event(struct trace_event_call *call,
2822 struct trace_array *tr)
2823 {
2824 struct trace_event_file *file;
2825
2826 file = trace_create_new_event(call, tr);
2827 if (!file)
2828 return -ENOMEM;
2829
2830 return event_define_fields(call);
2831 }
2832
2833 struct ftrace_module_file_ops;
2834 static void __add_event_to_tracers(struct trace_event_call *call);
2835
2836
2837 int trace_add_event_call(struct trace_event_call *call)
2838 {
2839 int ret;
2840 lockdep_assert_held(&event_mutex);
2841
2842 mutex_lock(&trace_types_lock);
2843
2844 ret = __register_event(call, NULL);
2845 if (ret >= 0)
2846 __add_event_to_tracers(call);
2847
2848 mutex_unlock(&trace_types_lock);
2849 return ret;
2850 }
2851 EXPORT_SYMBOL_GPL(trace_add_event_call);
2852
2853
2854
2855
2856
2857 static void __trace_remove_event_call(struct trace_event_call *call)
2858 {
2859 event_remove(call);
2860 trace_destroy_fields(call);
2861 free_event_filter(call->filter);
2862 call->filter = NULL;
2863 }
2864
2865 static int probe_remove_event_call(struct trace_event_call *call)
2866 {
2867 struct trace_array *tr;
2868 struct trace_event_file *file;
2869
2870 #ifdef CONFIG_PERF_EVENTS
2871 if (call->perf_refcount)
2872 return -EBUSY;
2873 #endif
2874 do_for_each_event_file(tr, file) {
2875 if (file->event_call != call)
2876 continue;
2877
2878
2879
2880
2881
2882 if (file->flags & EVENT_FILE_FL_ENABLED)
2883 return -EBUSY;
2884
2885
2886
2887
2888
2889
2890 break;
2891 } while_for_each_event_file();
2892
2893 __trace_remove_event_call(call);
2894
2895 return 0;
2896 }
2897
2898
2899 int trace_remove_event_call(struct trace_event_call *call)
2900 {
2901 int ret;
2902
2903 lockdep_assert_held(&event_mutex);
2904
2905 mutex_lock(&trace_types_lock);
2906 down_write(&trace_event_sem);
2907 ret = probe_remove_event_call(call);
2908 up_write(&trace_event_sem);
2909 mutex_unlock(&trace_types_lock);
2910
2911 return ret;
2912 }
2913 EXPORT_SYMBOL_GPL(trace_remove_event_call);
2914
2915 #define for_each_event(event, start, end) \
2916 for (event = start; \
2917 (unsigned long)event < (unsigned long)end; \
2918 event++)
2919
2920 #ifdef CONFIG_MODULES
2921
2922 static void trace_module_add_events(struct module *mod)
2923 {
2924 struct trace_event_call **call, **start, **end;
2925
2926 if (!mod->num_trace_events)
2927 return;
2928
2929
2930 if (trace_module_has_bad_taint(mod)) {
2931 pr_err("%s: module has bad taint, not creating trace events\n",
2932 mod->name);
2933 return;
2934 }
2935
2936 start = mod->trace_events;
2937 end = mod->trace_events + mod->num_trace_events;
2938
2939 for_each_event(call, start, end) {
2940 __register_event(*call, mod);
2941 __add_event_to_tracers(*call);
2942 }
2943 }
2944
2945 static void trace_module_remove_events(struct module *mod)
2946 {
2947 struct trace_event_call *call, *p;
2948 struct module_string *modstr, *m;
2949
2950 down_write(&trace_event_sem);
2951 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2952 if ((call->flags & TRACE_EVENT_FL_DYNAMIC) || !call->module)
2953 continue;
2954 if (call->module == mod)
2955 __trace_remove_event_call(call);
2956 }
2957
2958 list_for_each_entry_safe(modstr, m, &module_strings, next) {
2959 if (modstr->module != mod)
2960 continue;
2961 list_del(&modstr->next);
2962 kfree(modstr->str);
2963 kfree(modstr);
2964 }
2965 up_write(&trace_event_sem);
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975 tracing_reset_all_online_cpus();
2976 }
2977
2978 static int trace_module_notify(struct notifier_block *self,
2979 unsigned long val, void *data)
2980 {
2981 struct module *mod = data;
2982
2983 mutex_lock(&event_mutex);
2984 mutex_lock(&trace_types_lock);
2985 switch (val) {
2986 case MODULE_STATE_COMING:
2987 trace_module_add_events(mod);
2988 break;
2989 case MODULE_STATE_GOING:
2990 trace_module_remove_events(mod);
2991 break;
2992 }
2993 mutex_unlock(&trace_types_lock);
2994 mutex_unlock(&event_mutex);
2995
2996 return NOTIFY_OK;
2997 }
2998
2999 static struct notifier_block trace_module_nb = {
3000 .notifier_call = trace_module_notify,
3001 .priority = 1,
3002 };
3003 #endif
3004
3005
3006 static void
3007 __trace_add_event_dirs(struct trace_array *tr)
3008 {
3009 struct trace_event_call *call;
3010 int ret;
3011
3012 list_for_each_entry(call, &ftrace_events, list) {
3013 ret = __trace_add_new_event(call, tr);
3014 if (ret < 0)
3015 pr_warn("Could not create directory for event %s\n",
3016 trace_event_name(call));
3017 }
3018 }
3019
3020
3021 struct trace_event_file *
3022 __find_event_file(struct trace_array *tr, const char *system, const char *event)
3023 {
3024 struct trace_event_file *file;
3025 struct trace_event_call *call;
3026 const char *name;
3027
3028 list_for_each_entry(file, &tr->events, list) {
3029
3030 call = file->event_call;
3031 name = trace_event_name(call);
3032
3033 if (!name || !call->class)
3034 continue;
3035
3036 if (strcmp(event, name) == 0 &&
3037 strcmp(system, call->class->system) == 0)
3038 return file;
3039 }
3040 return NULL;
3041 }
3042
3043
3044 struct trace_event_file *
3045 find_event_file(struct trace_array *tr, const char *system, const char *event)
3046 {
3047 struct trace_event_file *file;
3048
3049 file = __find_event_file(tr, system, event);
3050 if (!file || !file->event_call->class->reg ||
3051 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
3052 return NULL;
3053
3054 return file;
3055 }
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077 struct trace_event_file *trace_get_event_file(const char *instance,
3078 const char *system,
3079 const char *event)
3080 {
3081 struct trace_array *tr = top_trace_array();
3082 struct trace_event_file *file = NULL;
3083 int ret = -EINVAL;
3084
3085 if (instance) {
3086 tr = trace_array_find_get(instance);
3087 if (!tr)
3088 return ERR_PTR(-ENOENT);
3089 } else {
3090 ret = trace_array_get(tr);
3091 if (ret)
3092 return ERR_PTR(ret);
3093 }
3094
3095 mutex_lock(&event_mutex);
3096
3097 file = find_event_file(tr, system, event);
3098 if (!file) {
3099 trace_array_put(tr);
3100 ret = -EINVAL;
3101 goto out;
3102 }
3103
3104
3105 ret = trace_event_try_get_ref(file->event_call);
3106 if (!ret) {
3107 trace_array_put(tr);
3108 ret = -EBUSY;
3109 goto out;
3110 }
3111
3112 ret = 0;
3113 out:
3114 mutex_unlock(&event_mutex);
3115
3116 if (ret)
3117 file = ERR_PTR(ret);
3118
3119 return file;
3120 }
3121 EXPORT_SYMBOL_GPL(trace_get_event_file);
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132 void trace_put_event_file(struct trace_event_file *file)
3133 {
3134 mutex_lock(&event_mutex);
3135 trace_event_put_ref(file->event_call);
3136 mutex_unlock(&event_mutex);
3137
3138 trace_array_put(file->tr);
3139 }
3140 EXPORT_SYMBOL_GPL(trace_put_event_file);
3141
3142 #ifdef CONFIG_DYNAMIC_FTRACE
3143
3144
3145 #define ENABLE_EVENT_STR "enable_event"
3146 #define DISABLE_EVENT_STR "disable_event"
3147
3148 struct event_probe_data {
3149 struct trace_event_file *file;
3150 unsigned long count;
3151 int ref;
3152 bool enable;
3153 };
3154
3155 static void update_event_probe(struct event_probe_data *data)
3156 {
3157 if (data->enable)
3158 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3159 else
3160 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
3161 }
3162
3163 static void
3164 event_enable_probe(unsigned long ip, unsigned long parent_ip,
3165 struct trace_array *tr, struct ftrace_probe_ops *ops,
3166 void *data)
3167 {
3168 struct ftrace_func_mapper *mapper = data;
3169 struct event_probe_data *edata;
3170 void **pdata;
3171
3172 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3173 if (!pdata || !*pdata)
3174 return;
3175
3176 edata = *pdata;
3177 update_event_probe(edata);
3178 }
3179
3180 static void
3181 event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
3182 struct trace_array *tr, struct ftrace_probe_ops *ops,
3183 void *data)
3184 {
3185 struct ftrace_func_mapper *mapper = data;
3186 struct event_probe_data *edata;
3187 void **pdata;
3188
3189 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3190 if (!pdata || !*pdata)
3191 return;
3192
3193 edata = *pdata;
3194
3195 if (!edata->count)
3196 return;
3197
3198
3199 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
3200 return;
3201
3202 if (edata->count != -1)
3203 (edata->count)--;
3204
3205 update_event_probe(edata);
3206 }
3207
3208 static int
3209 event_enable_print(struct seq_file *m, unsigned long ip,
3210 struct ftrace_probe_ops *ops, void *data)
3211 {
3212 struct ftrace_func_mapper *mapper = data;
3213 struct event_probe_data *edata;
3214 void **pdata;
3215
3216 pdata = ftrace_func_mapper_find_ip(mapper, ip);
3217
3218 if (WARN_ON_ONCE(!pdata || !*pdata))
3219 return 0;
3220
3221 edata = *pdata;
3222
3223 seq_printf(m, "%ps:", (void *)ip);
3224
3225 seq_printf(m, "%s:%s:%s",
3226 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
3227 edata->file->event_call->class->system,
3228 trace_event_name(edata->file->event_call));
3229
3230 if (edata->count == -1)
3231 seq_puts(m, ":unlimited\n");
3232 else
3233 seq_printf(m, ":count=%ld\n", edata->count);
3234
3235 return 0;
3236 }
3237
3238 static int
3239 event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
3240 unsigned long ip, void *init_data, void **data)
3241 {
3242 struct ftrace_func_mapper *mapper = *data;
3243 struct event_probe_data *edata = init_data;
3244 int ret;
3245
3246 if (!mapper) {
3247 mapper = allocate_ftrace_func_mapper();
3248 if (!mapper)
3249 return -ENODEV;
3250 *data = mapper;
3251 }
3252
3253 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
3254 if (ret < 0)
3255 return ret;
3256
3257 edata->ref++;
3258
3259 return 0;
3260 }
3261
3262 static int free_probe_data(void *data)
3263 {
3264 struct event_probe_data *edata = data;
3265
3266 edata->ref--;
3267 if (!edata->ref) {
3268
3269 __ftrace_event_enable_disable(edata->file, 0, 1);
3270 trace_event_put_ref(edata->file->event_call);
3271 kfree(edata);
3272 }
3273 return 0;
3274 }
3275
3276 static void
3277 event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
3278 unsigned long ip, void *data)
3279 {
3280 struct ftrace_func_mapper *mapper = data;
3281 struct event_probe_data *edata;
3282
3283 if (!ip) {
3284 if (!mapper)
3285 return;
3286 free_ftrace_func_mapper(mapper, free_probe_data);
3287 return;
3288 }
3289
3290 edata = ftrace_func_mapper_remove_ip(mapper, ip);
3291
3292 if (WARN_ON_ONCE(!edata))
3293 return;
3294
3295 if (WARN_ON_ONCE(edata->ref <= 0))
3296 return;
3297
3298 free_probe_data(edata);
3299 }
3300
3301 static struct ftrace_probe_ops event_enable_probe_ops = {
3302 .func = event_enable_probe,
3303 .print = event_enable_print,
3304 .init = event_enable_init,
3305 .free = event_enable_free,
3306 };
3307
3308 static struct ftrace_probe_ops event_enable_count_probe_ops = {
3309 .func = event_enable_count_probe,
3310 .print = event_enable_print,
3311 .init = event_enable_init,
3312 .free = event_enable_free,
3313 };
3314
3315 static struct ftrace_probe_ops event_disable_probe_ops = {
3316 .func = event_enable_probe,
3317 .print = event_enable_print,
3318 .init = event_enable_init,
3319 .free = event_enable_free,
3320 };
3321
3322 static struct ftrace_probe_ops event_disable_count_probe_ops = {
3323 .func = event_enable_count_probe,
3324 .print = event_enable_print,
3325 .init = event_enable_init,
3326 .free = event_enable_free,
3327 };
3328
3329 static int
3330 event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
3331 char *glob, char *cmd, char *param, int enabled)
3332 {
3333 struct trace_event_file *file;
3334 struct ftrace_probe_ops *ops;
3335 struct event_probe_data *data;
3336 const char *system;
3337 const char *event;
3338 char *number;
3339 bool enable;
3340 int ret;
3341
3342 if (!tr)
3343 return -ENODEV;
3344
3345
3346 if (!enabled || !param)
3347 return -EINVAL;
3348
3349 system = strsep(¶m, ":");
3350 if (!param)
3351 return -EINVAL;
3352
3353 event = strsep(¶m, ":");
3354
3355 mutex_lock(&event_mutex);
3356
3357 ret = -EINVAL;
3358 file = find_event_file(tr, system, event);
3359 if (!file)
3360 goto out;
3361
3362 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
3363
3364 if (enable)
3365 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
3366 else
3367 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
3368
3369 if (glob[0] == '!') {
3370 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
3371 goto out;
3372 }
3373
3374 ret = -ENOMEM;
3375
3376 data = kzalloc(sizeof(*data), GFP_KERNEL);
3377 if (!data)
3378 goto out;
3379
3380 data->enable = enable;
3381 data->count = -1;
3382 data->file = file;
3383
3384 if (!param)
3385 goto out_reg;
3386
3387 number = strsep(¶m, ":");
3388
3389 ret = -EINVAL;
3390 if (!strlen(number))
3391 goto out_free;
3392
3393
3394
3395
3396
3397 ret = kstrtoul(number, 0, &data->count);
3398 if (ret)
3399 goto out_free;
3400
3401 out_reg:
3402
3403 ret = trace_event_try_get_ref(file->event_call);
3404 if (!ret) {
3405 ret = -EBUSY;
3406 goto out_free;
3407 }
3408
3409 ret = __ftrace_event_enable_disable(file, 1, 1);
3410 if (ret < 0)
3411 goto out_put;
3412
3413 ret = register_ftrace_function_probe(glob, tr, ops, data);
3414
3415
3416
3417
3418
3419 if (!ret) {
3420 ret = -ENOENT;
3421 goto out_disable;
3422 } else if (ret < 0)
3423 goto out_disable;
3424
3425 ret = 0;
3426 out:
3427 mutex_unlock(&event_mutex);
3428 return ret;
3429
3430 out_disable:
3431 __ftrace_event_enable_disable(file, 0, 1);
3432 out_put:
3433 trace_event_put_ref(file->event_call);
3434 out_free:
3435 kfree(data);
3436 goto out;
3437 }
3438
3439 static struct ftrace_func_command event_enable_cmd = {
3440 .name = ENABLE_EVENT_STR,
3441 .func = event_enable_func,
3442 };
3443
3444 static struct ftrace_func_command event_disable_cmd = {
3445 .name = DISABLE_EVENT_STR,
3446 .func = event_enable_func,
3447 };
3448
3449 static __init int register_event_cmds(void)
3450 {
3451 int ret;
3452
3453 ret = register_ftrace_command(&event_enable_cmd);
3454 if (WARN_ON(ret < 0))
3455 return ret;
3456 ret = register_ftrace_command(&event_disable_cmd);
3457 if (WARN_ON(ret < 0))
3458 unregister_ftrace_command(&event_enable_cmd);
3459 return ret;
3460 }
3461 #else
3462 static inline int register_event_cmds(void) { return 0; }
3463 #endif
3464
3465
3466
3467
3468
3469
3470
3471
3472 static void __trace_early_add_event_dirs(struct trace_array *tr)
3473 {
3474 struct trace_event_file *file;
3475 int ret;
3476
3477
3478 list_for_each_entry(file, &tr->events, list) {
3479 ret = event_create_dir(tr->event_dir, file);
3480 if (ret < 0)
3481 pr_warn("Could not create directory for event %s\n",
3482 trace_event_name(file->event_call));
3483 }
3484 }
3485
3486
3487
3488
3489
3490
3491
3492 void __trace_early_add_events(struct trace_array *tr)
3493 {
3494 struct trace_event_call *call;
3495 int ret;
3496
3497 list_for_each_entry(call, &ftrace_events, list) {
3498
3499 if (!(call->flags & TRACE_EVENT_FL_DYNAMIC) &&
3500 WARN_ON_ONCE(call->module))
3501 continue;
3502
3503 ret = __trace_early_add_new_event(call, tr);
3504 if (ret < 0)
3505 pr_warn("Could not create early event %s\n",
3506 trace_event_name(call));
3507 }
3508 }
3509
3510
3511 static void
3512 __trace_remove_event_dirs(struct trace_array *tr)
3513 {
3514 struct trace_event_file *file, *next;
3515
3516 list_for_each_entry_safe(file, next, &tr->events, list)
3517 remove_event_file_dir(file);
3518 }
3519
3520 static void __add_event_to_tracers(struct trace_event_call *call)
3521 {
3522 struct trace_array *tr;
3523
3524 list_for_each_entry(tr, &ftrace_trace_arrays, list)
3525 __trace_add_new_event(call, tr);
3526 }
3527
3528 extern struct trace_event_call *__start_ftrace_events[];
3529 extern struct trace_event_call *__stop_ftrace_events[];
3530
3531 static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
3532
3533 static __init int setup_trace_event(char *str)
3534 {
3535 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
3536 ring_buffer_expanded = true;
3537 disable_tracing_selftest("running event tracing");
3538
3539 return 1;
3540 }
3541 __setup("trace_event=", setup_trace_event);
3542
3543
3544 static int
3545 create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
3546 {
3547 struct dentry *d_events;
3548 struct dentry *entry;
3549
3550 entry = trace_create_file("set_event", TRACE_MODE_WRITE, parent,
3551 tr, &ftrace_set_event_fops);
3552 if (!entry)
3553 return -ENOMEM;
3554
3555 d_events = tracefs_create_dir("events", parent);
3556 if (!d_events) {
3557 pr_warn("Could not create tracefs 'events' directory\n");
3558 return -ENOMEM;
3559 }
3560
3561 entry = trace_create_file("enable", TRACE_MODE_WRITE, d_events,
3562 tr, &ftrace_tr_enable_fops);
3563 if (!entry)
3564 return -ENOMEM;
3565
3566
3567
3568 trace_create_file("set_event_pid", TRACE_MODE_WRITE, parent,
3569 tr, &ftrace_set_event_pid_fops);
3570
3571 trace_create_file("set_event_notrace_pid",
3572 TRACE_MODE_WRITE, parent, tr,
3573 &ftrace_set_event_notrace_pid_fops);
3574
3575
3576 trace_create_file("header_page", TRACE_MODE_READ, d_events,
3577 ring_buffer_print_page_header,
3578 &ftrace_show_header_fops);
3579
3580 trace_create_file("header_event", TRACE_MODE_READ, d_events,
3581 ring_buffer_print_entry_header,
3582 &ftrace_show_header_fops);
3583
3584 tr->event_dir = d_events;
3585
3586 return 0;
3587 }
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602 int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
3603 {
3604 int ret;
3605
3606 lockdep_assert_held(&event_mutex);
3607
3608 ret = create_event_toplevel_files(parent, tr);
3609 if (ret)
3610 goto out;
3611
3612 down_write(&trace_event_sem);
3613
3614 if (unlikely(!list_empty(&tr->events)))
3615 __trace_early_add_event_dirs(tr);
3616 else
3617 __trace_add_event_dirs(tr);
3618 up_write(&trace_event_sem);
3619
3620 out:
3621 return ret;
3622 }
3623
3624
3625
3626
3627
3628 static __init int
3629 early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
3630 {
3631 int ret;
3632
3633 mutex_lock(&event_mutex);
3634
3635 ret = create_event_toplevel_files(parent, tr);
3636 if (ret)
3637 goto out_unlock;
3638
3639 down_write(&trace_event_sem);
3640 __trace_early_add_event_dirs(tr);
3641 up_write(&trace_event_sem);
3642
3643 out_unlock:
3644 mutex_unlock(&event_mutex);
3645
3646 return ret;
3647 }
3648
3649
3650 int event_trace_del_tracer(struct trace_array *tr)
3651 {
3652 lockdep_assert_held(&event_mutex);
3653
3654
3655 clear_event_triggers(tr);
3656
3657
3658 __ftrace_clear_event_pids(tr, TRACE_PIDS | TRACE_NO_PIDS);
3659
3660
3661 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3662
3663
3664 tracepoint_synchronize_unregister();
3665
3666 down_write(&trace_event_sem);
3667 __trace_remove_event_dirs(tr);
3668 tracefs_remove(tr->event_dir);
3669 up_write(&trace_event_sem);
3670
3671 tr->event_dir = NULL;
3672
3673 return 0;
3674 }
3675
3676 static __init int event_trace_memsetup(void)
3677 {
3678 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
3679 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
3680 return 0;
3681 }
3682
3683 static __init void
3684 early_enable_events(struct trace_array *tr, bool disable_first)
3685 {
3686 char *buf = bootup_event_buf;
3687 char *token;
3688 int ret;
3689
3690 while (true) {
3691 token = strsep(&buf, ",");
3692
3693 if (!token)
3694 break;
3695
3696 if (*token) {
3697
3698 if (disable_first)
3699 ftrace_set_clr_event(tr, token, 0);
3700
3701 ret = ftrace_set_clr_event(tr, token, 1);
3702 if (ret)
3703 pr_warn("Failed to enable trace event: %s\n", token);
3704 }
3705
3706
3707 if (buf)
3708 *(buf - 1) = ',';
3709 }
3710 }
3711
3712 static __init int event_trace_enable(void)
3713 {
3714 struct trace_array *tr = top_trace_array();
3715 struct trace_event_call **iter, *call;
3716 int ret;
3717
3718 if (!tr)
3719 return -ENODEV;
3720
3721 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3722
3723 call = *iter;
3724 ret = event_init(call);
3725 if (!ret)
3726 list_add(&call->list, &ftrace_events);
3727 }
3728
3729
3730
3731
3732
3733
3734
3735 __trace_early_add_events(tr);
3736
3737 early_enable_events(tr, false);
3738
3739 trace_printk_start_comm();
3740
3741 register_event_cmds();
3742
3743 register_trigger_cmds();
3744
3745 return 0;
3746 }
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758 static __init int event_trace_enable_again(void)
3759 {
3760 struct trace_array *tr;
3761
3762 tr = top_trace_array();
3763 if (!tr)
3764 return -ENODEV;
3765
3766 early_enable_events(tr, true);
3767
3768 return 0;
3769 }
3770
3771 early_initcall(event_trace_enable_again);
3772
3773
3774 static __init int event_trace_init_fields(void)
3775 {
3776 if (trace_define_generic_fields())
3777 pr_warn("tracing: Failed to allocated generic fields");
3778
3779 if (trace_define_common_fields())
3780 pr_warn("tracing: Failed to allocate common fields");
3781
3782 return 0;
3783 }
3784
3785 __init int event_trace_init(void)
3786 {
3787 struct trace_array *tr;
3788 int ret;
3789
3790 tr = top_trace_array();
3791 if (!tr)
3792 return -ENODEV;
3793
3794 trace_create_file("available_events", TRACE_MODE_READ,
3795 NULL, tr, &ftrace_avail_fops);
3796
3797 ret = early_event_add_tracer(NULL, tr);
3798 if (ret)
3799 return ret;
3800
3801 #ifdef CONFIG_MODULES
3802 ret = register_module_notifier(&trace_module_nb);
3803 if (ret)
3804 pr_warn("Failed to register trace events module notifier\n");
3805 #endif
3806
3807 eventdir_initialized = true;
3808
3809 return 0;
3810 }
3811
3812 void __init trace_event_init(void)
3813 {
3814 event_trace_memsetup();
3815 init_ftrace_syscalls();
3816 event_trace_enable();
3817 event_trace_init_fields();
3818 }
3819
3820 #ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
3821
3822 static DEFINE_SPINLOCK(test_spinlock);
3823 static DEFINE_SPINLOCK(test_spinlock_irq);
3824 static DEFINE_MUTEX(test_mutex);
3825
3826 static __init void test_work(struct work_struct *dummy)
3827 {
3828 spin_lock(&test_spinlock);
3829 spin_lock_irq(&test_spinlock_irq);
3830 udelay(1);
3831 spin_unlock_irq(&test_spinlock_irq);
3832 spin_unlock(&test_spinlock);
3833
3834 mutex_lock(&test_mutex);
3835 msleep(1);
3836 mutex_unlock(&test_mutex);
3837 }
3838
3839 static __init int event_test_thread(void *unused)
3840 {
3841 void *test_malloc;
3842
3843 test_malloc = kmalloc(1234, GFP_KERNEL);
3844 if (!test_malloc)
3845 pr_info("failed to kmalloc\n");
3846
3847 schedule_on_each_cpu(test_work);
3848
3849 kfree(test_malloc);
3850
3851 set_current_state(TASK_INTERRUPTIBLE);
3852 while (!kthread_should_stop()) {
3853 schedule();
3854 set_current_state(TASK_INTERRUPTIBLE);
3855 }
3856 __set_current_state(TASK_RUNNING);
3857
3858 return 0;
3859 }
3860
3861
3862
3863
3864 static __init void event_test_stuff(void)
3865 {
3866 struct task_struct *test_thread;
3867
3868 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3869 msleep(1);
3870 kthread_stop(test_thread);
3871 }
3872
3873
3874
3875
3876
3877 static __init void event_trace_self_tests(void)
3878 {
3879 struct trace_subsystem_dir *dir;
3880 struct trace_event_file *file;
3881 struct trace_event_call *call;
3882 struct event_subsystem *system;
3883 struct trace_array *tr;
3884 int ret;
3885
3886 tr = top_trace_array();
3887 if (!tr)
3888 return;
3889
3890 pr_info("Running tests on trace events:\n");
3891
3892 list_for_each_entry(file, &tr->events, list) {
3893
3894 call = file->event_call;
3895
3896
3897 if (!call->class || !call->class->probe)
3898 continue;
3899
3900
3901
3902
3903
3904
3905
3906 #ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
3907 if (call->class->system &&
3908 strcmp(call->class->system, "syscalls") == 0)
3909 continue;
3910 #endif
3911
3912 pr_info("Testing event %s: ", trace_event_name(call));
3913
3914
3915
3916
3917
3918 if (file->flags & EVENT_FILE_FL_ENABLED) {
3919 pr_warn("Enabled event during self test!\n");
3920 WARN_ON_ONCE(1);
3921 continue;
3922 }
3923
3924 ftrace_event_enable_disable(file, 1);
3925 event_test_stuff();
3926 ftrace_event_enable_disable(file, 0);
3927
3928 pr_cont("OK\n");
3929 }
3930
3931
3932
3933 pr_info("Running tests on trace event systems:\n");
3934
3935 list_for_each_entry(dir, &tr->systems, list) {
3936
3937 system = dir->subsystem;
3938
3939
3940 if (strcmp(system->name, "ftrace") == 0)
3941 continue;
3942
3943 pr_info("Testing event system %s: ", system->name);
3944
3945 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
3946 if (WARN_ON_ONCE(ret)) {
3947 pr_warn("error enabling system %s\n",
3948 system->name);
3949 continue;
3950 }
3951
3952 event_test_stuff();
3953
3954 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
3955 if (WARN_ON_ONCE(ret)) {
3956 pr_warn("error disabling system %s\n",
3957 system->name);
3958 continue;
3959 }
3960
3961 pr_cont("OK\n");
3962 }
3963
3964
3965
3966 pr_info("Running tests on all trace events:\n");
3967 pr_info("Testing all events: ");
3968
3969 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
3970 if (WARN_ON_ONCE(ret)) {
3971 pr_warn("error enabling all events\n");
3972 return;
3973 }
3974
3975 event_test_stuff();
3976
3977
3978 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
3979 if (WARN_ON_ONCE(ret)) {
3980 pr_warn("error disabling all events\n");
3981 return;
3982 }
3983
3984 pr_cont("OK\n");
3985 }
3986
3987 #ifdef CONFIG_FUNCTION_TRACER
3988
3989 static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
3990
3991 static struct trace_event_file event_trace_file __initdata;
3992
3993 static void __init
3994 function_test_events_call(unsigned long ip, unsigned long parent_ip,
3995 struct ftrace_ops *op, struct ftrace_regs *regs)
3996 {
3997 struct trace_buffer *buffer;
3998 struct ring_buffer_event *event;
3999 struct ftrace_entry *entry;
4000 unsigned int trace_ctx;
4001 long disabled;
4002 int cpu;
4003
4004 trace_ctx = tracing_gen_ctx();
4005 preempt_disable_notrace();
4006 cpu = raw_smp_processor_id();
4007 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
4008
4009 if (disabled != 1)
4010 goto out;
4011
4012 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
4013 TRACE_FN, sizeof(*entry),
4014 trace_ctx);
4015 if (!event)
4016 goto out;
4017 entry = ring_buffer_event_data(event);
4018 entry->ip = ip;
4019 entry->parent_ip = parent_ip;
4020
4021 event_trigger_unlock_commit(&event_trace_file, buffer, event,
4022 entry, trace_ctx);
4023 out:
4024 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
4025 preempt_enable_notrace();
4026 }
4027
4028 static struct ftrace_ops trace_ops __initdata =
4029 {
4030 .func = function_test_events_call,
4031 };
4032
4033 static __init void event_trace_self_test_with_function(void)
4034 {
4035 int ret;
4036
4037 event_trace_file.tr = top_trace_array();
4038 if (WARN_ON(!event_trace_file.tr))
4039 return;
4040
4041 ret = register_ftrace_function(&trace_ops);
4042 if (WARN_ON(ret < 0)) {
4043 pr_info("Failed to enable function tracer for event tests\n");
4044 return;
4045 }
4046 pr_info("Running tests again, along with the function tracer\n");
4047 event_trace_self_tests();
4048 unregister_ftrace_function(&trace_ops);
4049 }
4050 #else
4051 static __init void event_trace_self_test_with_function(void)
4052 {
4053 }
4054 #endif
4055
4056 static __init int event_trace_self_tests_init(void)
4057 {
4058 if (!tracing_selftest_disabled) {
4059 event_trace_self_tests();
4060 event_trace_self_test_with_function();
4061 }
4062
4063 return 0;
4064 }
4065
4066 late_initcall(event_trace_self_tests_init);
4067
4068 #endif