blob: 648930823b571083c1a95000937962e565162eb2 [file] [log] [blame]
Steven Rostedt (VMware)bcea3f92018-08-16 11:23:53 -04001// SPDX-License-Identifier: GPL-2.0
Steven Rostedtb77e38a2009-02-24 10:21:36 -05002/*
3 * event tracer
4 *
5 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 *
Steven Rostedt981d0812009-03-02 13:53:59 -05007 * - Added format output of fields of the trace point.
8 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
9 *
Steven Rostedtb77e38a2009-02-24 10:21:36 -050010 */
11
Fabian Frederick3448bac2014-06-07 13:43:08 +020012#define pr_fmt(fmt) fmt
13
Steven Rostedte6187002009-04-15 13:36:40 -040014#include <linux/workqueue.h>
15#include <linux/spinlock.h>
16#include <linux/kthread.h>
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -050017#include <linux/tracefs.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050018#include <linux/uaccess.h>
19#include <linux/module.h>
20#include <linux/ctype.h>
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -040021#include <linux/sort.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Steven Rostedte6187002009-04-15 13:36:40 -040023#include <linux/delay.h>
Steven Rostedtb77e38a2009-02-24 10:21:36 -050024
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -040025#include <trace/events/sched.h>
26
Li Zefan020e5f82009-07-01 10:47:05 +080027#include <asm/setup.h>
28
Steven Rostedt91729ef92009-03-02 15:03:01 -050029#include "trace_output.h"
Steven Rostedtb77e38a2009-02-24 10:21:36 -050030
Steven Rostedt4e5292e2009-09-12 19:26:21 -040031#undef TRACE_SYSTEM
Steven Rostedtb628b3e2009-02-27 23:32:58 -050032#define TRACE_SYSTEM "TRACE_SYSTEM"
33
Li Zefan20c89282009-05-06 10:33:45 +080034DEFINE_MUTEX(event_mutex);
Steven Rostedt11a241a2009-03-02 11:49:04 -050035
Steven Rostedta59fd602009-04-10 13:52:20 -040036LIST_HEAD(ftrace_events);
Daniel Wagner9f616682015-08-10 14:35:46 +020037static LIST_HEAD(ftrace_generic_fields);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080038static LIST_HEAD(ftrace_common_fields);
Steven Rostedta59fd602009-04-10 13:52:20 -040039
Steven Rostedtd1a29142013-02-27 20:23:57 -050040#define GFP_TRACE (GFP_KERNEL | __GFP_ZERO)
41
42static struct kmem_cache *field_cachep;
43static struct kmem_cache *file_cachep;
44
Steven Rostedt6e94a782013-06-27 10:58:31 -040045static inline int system_refcount(struct event_subsystem *system)
46{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020047 return system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040048}
49
50static int system_refcount_inc(struct event_subsystem *system)
51{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020052 return system->ref_count++;
Steven Rostedt6e94a782013-06-27 10:58:31 -040053}
54
55static int system_refcount_dec(struct event_subsystem *system)
56{
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +020057 return --system->ref_count;
Steven Rostedt6e94a782013-06-27 10:58:31 -040058}
59
Steven Rostedtae63b31e2012-05-03 23:09:03 -040060/* Double loops, do not use break, only goto's work */
61#define do_for_each_event_file(tr, file) \
62 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
63 list_for_each_entry(file, &tr->events, list)
64
65#define do_for_each_event_file_safe(tr, file) \
66 list_for_each_entry(tr, &ftrace_trace_arrays, list) { \
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -040067 struct trace_event_file *___n; \
Steven Rostedtae63b31e2012-05-03 23:09:03 -040068 list_for_each_entry_safe(file, ___n, &tr->events, list)
69
70#define while_for_each_event_file() \
71 }
72
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080073static struct ftrace_event_field *
74__find_event_field(struct list_head *head, char *name)
75{
76 struct ftrace_event_field *field;
77
78 list_for_each_entry(field, head, link) {
79 if (!strcmp(field->name, name))
80 return field;
81 }
82
83 return NULL;
84}
85
86struct ftrace_event_field *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -040087trace_find_event_field(struct trace_event_call *call, char *name)
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +080088{
89 struct ftrace_event_field *field;
90 struct list_head *head;
91
Steven Rostedt (Red Hat)e57cbaf2016-03-03 17:18:20 -050092 head = trace_get_fields(call);
93 field = __find_event_field(head, name);
94 if (field)
95 return field;
96
Daniel Wagner9f616682015-08-10 14:35:46 +020097 field = __find_event_field(&ftrace_generic_fields, name);
98 if (field)
99 return field;
100
Steven Rostedt (Red Hat)e57cbaf2016-03-03 17:18:20 -0500101 return __find_event_field(&ftrace_common_fields, name);
zhangwei(Jovi)b3a8c6f2013-03-11 15:13:42 +0800102}
103
Li Zefan8728fe52010-05-24 16:22:49 +0800104static int __trace_define_field(struct list_head *head, const char *type,
105 const char *name, int offset, int size,
106 int is_signed, int filter_type)
Tom Zanussicf027f62009-03-22 03:30:39 -0500107{
108 struct ftrace_event_field *field;
109
Steven Rostedtd1a29142013-02-27 20:23:57 -0500110 field = kmem_cache_alloc(field_cachep, GFP_TRACE);
Tom Zanussicf027f62009-03-22 03:30:39 -0500111 if (!field)
Namhyung Kimaaf6ac02013-06-07 15:07:48 +0900112 return -ENOMEM;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100113
Steven Rostedt92edca02013-02-27 20:41:37 -0500114 field->name = name;
115 field->type = type;
Ingo Molnarfe9f57f22009-03-22 18:41:59 +0100116
Li Zefan43b51ea2009-08-07 10:33:22 +0800117 if (filter_type == FILTER_OTHER)
118 field->filter_type = filter_assign_type(type);
119 else
120 field->filter_type = filter_type;
121
Tom Zanussicf027f62009-03-22 03:30:39 -0500122 field->offset = offset;
123 field->size = size;
Tom Zanussia118e4d2009-04-28 03:04:53 -0500124 field->is_signed = is_signed;
Li Zefanaa38e9f2009-08-07 10:33:02 +0800125
Steven Rostedt2e33af02010-04-22 10:35:55 -0400126 list_add(&field->link, head);
Tom Zanussicf027f62009-03-22 03:30:39 -0500127
128 return 0;
Tom Zanussicf027f62009-03-22 03:30:39 -0500129}
Li Zefan8728fe52010-05-24 16:22:49 +0800130
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400131int trace_define_field(struct trace_event_call *call, const char *type,
Li Zefan8728fe52010-05-24 16:22:49 +0800132 const char *name, int offset, int size, int is_signed,
133 int filter_type)
134{
135 struct list_head *head;
136
137 if (WARN_ON(!call->class))
138 return 0;
139
140 head = trace_get_fields(call);
141 return __trace_define_field(head, type, name, offset, size,
142 is_signed, filter_type);
143}
Steven Rostedt17c873e2009-04-10 18:12:50 -0400144EXPORT_SYMBOL_GPL(trace_define_field);
Tom Zanussicf027f62009-03-22 03:30:39 -0500145
Daniel Wagner9f616682015-08-10 14:35:46 +0200146#define __generic_field(type, item, filter_type) \
147 ret = __trace_define_field(&ftrace_generic_fields, #type, \
148 #item, 0, 0, is_signed_type(type), \
149 filter_type); \
150 if (ret) \
151 return ret;
152
Li Zefane647d6b2009-08-19 15:54:32 +0800153#define __common_field(type, item) \
Li Zefan8728fe52010-05-24 16:22:49 +0800154 ret = __trace_define_field(&ftrace_common_fields, #type, \
155 "common_" #item, \
156 offsetof(typeof(ent), item), \
157 sizeof(ent.item), \
158 is_signed_type(type), FILTER_OTHER); \
Li Zefane647d6b2009-08-19 15:54:32 +0800159 if (ret) \
160 return ret;
161
Daniel Wagner9f616682015-08-10 14:35:46 +0200162static int trace_define_generic_fields(void)
163{
164 int ret;
165
Steven Rostedt (Red Hat)e57cbaf2016-03-03 17:18:20 -0500166 __generic_field(int, CPU, FILTER_CPU);
167 __generic_field(int, cpu, FILTER_CPU);
168 __generic_field(char *, COMM, FILTER_COMM);
169 __generic_field(char *, comm, FILTER_COMM);
Daniel Wagner9f616682015-08-10 14:35:46 +0200170
171 return ret;
172}
173
Li Zefan8728fe52010-05-24 16:22:49 +0800174static int trace_define_common_fields(void)
Li Zefane647d6b2009-08-19 15:54:32 +0800175{
176 int ret;
177 struct trace_entry ent;
178
179 __common_field(unsigned short, type);
180 __common_field(unsigned char, flags);
181 __common_field(unsigned char, preempt_count);
182 __common_field(int, pid);
Li Zefane647d6b2009-08-19 15:54:32 +0800183
184 return ret;
185}
186
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400187static void trace_destroy_fields(struct trace_event_call *call)
Li Zefan2df75e42009-05-06 10:33:04 +0800188{
189 struct ftrace_event_field *field, *next;
Steven Rostedt2e33af02010-04-22 10:35:55 -0400190 struct list_head *head;
Li Zefan2df75e42009-05-06 10:33:04 +0800191
Steven Rostedt2e33af02010-04-22 10:35:55 -0400192 head = trace_get_fields(call);
193 list_for_each_entry_safe(field, next, head, link) {
Li Zefan2df75e42009-05-06 10:33:04 +0800194 list_del(&field->link);
Steven Rostedtd1a29142013-02-27 20:23:57 -0500195 kmem_cache_free(field_cachep, field);
Li Zefan2df75e42009-05-06 10:33:04 +0800196 }
197}
198
Alexei Starovoitov32bbe002016-04-06 18:43:28 -0700199/*
200 * run-time version of trace_event_get_offsets_<call>() that returns the last
201 * accessible offset of trace fields excluding __dynamic_array bytes
202 */
203int trace_event_get_offsets(struct trace_event_call *call)
204{
205 struct ftrace_event_field *tail;
206 struct list_head *head;
207
208 head = trace_get_fields(call);
209 /*
210 * head->next points to the last field with the largest offset,
211 * since it was added last by trace_define_field()
212 */
213 tail = list_first_entry(head, struct ftrace_event_field, link);
214 return tail->offset + tail->size;
215}
216
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400217int trace_event_raw_init(struct trace_event_call *call)
Li Zefan87d9b4e2009-12-08 11:14:20 +0800218{
219 int id;
220
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400221 id = register_trace_event(&call->event);
Li Zefan87d9b4e2009-12-08 11:14:20 +0800222 if (!id)
223 return -ENODEV;
Li Zefan87d9b4e2009-12-08 11:14:20 +0800224
225 return 0;
226}
227EXPORT_SYMBOL_GPL(trace_event_raw_init);
228
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400229bool trace_event_ignore_this_pid(struct trace_event_file *trace_file)
230{
231 struct trace_array *tr = trace_file->tr;
232 struct trace_array_cpu *data;
233 struct trace_pid_list *pid_list;
234
Joel Fernandes (Google)da25a672018-08-02 19:34:07 -0700235 pid_list = rcu_dereference_raw(tr->filtered_pids);
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400236 if (!pid_list)
237 return false;
238
239 data = this_cpu_ptr(tr->trace_buffer.data);
240
241 return data->ignore_pid;
242}
243EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid);
244
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400245void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
246 struct trace_event_file *trace_file,
247 unsigned long len)
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400248{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400249 struct trace_event_call *event_call = trace_file->event_call;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400250
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400251 if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
252 trace_event_ignore_this_pid(trace_file))
253 return NULL;
254
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400255 local_save_flags(fbuffer->flags);
256 fbuffer->pc = preempt_count();
Steven Rostedt (Red Hat)e9478412016-06-17 17:40:58 -0400257 /*
258 * If CONFIG_PREEMPT is enabled, then the tracepoint itself disables
259 * preemption (adding one to the preempt_count). Since we are
260 * interested in the preempt_count at the time the tracepoint was
261 * hit, we need to subtract one to offset the increment.
262 */
263 if (IS_ENABLED(CONFIG_PREEMPT))
264 fbuffer->pc--;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400265 fbuffer->trace_file = trace_file;
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400266
267 fbuffer->event =
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400268 trace_event_buffer_lock_reserve(&fbuffer->buffer, trace_file,
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400269 event_call->event.type, len,
270 fbuffer->flags, fbuffer->pc);
271 if (!fbuffer->event)
272 return NULL;
273
274 fbuffer->entry = ring_buffer_event_data(fbuffer->event);
275 return fbuffer->entry;
276}
Steven Rostedt (Red Hat)3f795dc2015-05-05 13:18:46 -0400277EXPORT_SYMBOL_GPL(trace_event_buffer_reserve);
Steven Rostedt3fd40d12012-08-09 22:42:57 -0400278
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400279int trace_event_reg(struct trace_event_call *call,
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400280 enum trace_reg type, void *data)
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400281{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400282 struct trace_event_file *file = data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400283
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400284 WARN_ON(!(call->flags & TRACE_EVENT_FL_TRACEPOINT));
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400285 switch (type) {
286 case TRACE_REG_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400287 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400288 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400289 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400290 case TRACE_REG_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400291 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400292 call->class->probe,
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400293 file);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400294 return 0;
295
296#ifdef CONFIG_PERF_EVENTS
297 case TRACE_REG_PERF_REGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400298 return tracepoint_probe_register(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400299 call->class->perf_probe,
300 call);
301 case TRACE_REG_PERF_UNREGISTER:
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400302 tracepoint_probe_unregister(call->tp,
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400303 call->class->perf_probe,
304 call);
305 return 0;
Jiri Olsaceec0b62012-02-15 15:51:49 +0100306 case TRACE_REG_PERF_OPEN:
307 case TRACE_REG_PERF_CLOSE:
Jiri Olsa489c75c2012-02-15 15:51:50 +0100308 case TRACE_REG_PERF_ADD:
309 case TRACE_REG_PERF_DEL:
Jiri Olsaceec0b62012-02-15 15:51:49 +0100310 return 0;
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400311#endif
312 }
313 return 0;
314}
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -0400315EXPORT_SYMBOL_GPL(trace_event_reg);
Steven Rostedta1d0ce82010-06-08 11:22:06 -0400316
Li Zefane870e9a2010-07-02 11:07:32 +0800317void trace_event_enable_cmd_record(bool enable)
318{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400319 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400320 struct trace_array *tr;
Li Zefane870e9a2010-07-02 11:07:32 +0800321
322 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400323 do_for_each_event_file(tr, file) {
324
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400325 if (!(file->flags & EVENT_FILE_FL_ENABLED))
Li Zefane870e9a2010-07-02 11:07:32 +0800326 continue;
327
328 if (enable) {
329 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400330 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800331 } else {
332 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400333 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800334 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400335 } while_for_each_event_file();
Li Zefane870e9a2010-07-02 11:07:32 +0800336 mutex_unlock(&event_mutex);
337}
338
Joel Fernandesd914ba32017-06-26 19:01:55 -0700339void trace_event_enable_tgid_record(bool enable)
340{
341 struct trace_event_file *file;
342 struct trace_array *tr;
343
344 mutex_lock(&event_mutex);
345 do_for_each_event_file(tr, file) {
346 if (!(file->flags & EVENT_FILE_FL_ENABLED))
347 continue;
348
349 if (enable) {
350 tracing_start_tgid_record();
351 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
352 } else {
353 tracing_stop_tgid_record();
354 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT,
355 &file->flags);
356 }
357 } while_for_each_event_file();
358 mutex_unlock(&event_mutex);
359}
360
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400361static int __ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400362 int enable, int soft_disable)
Steven Rostedtfd994982009-02-28 02:41:25 -0500363{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400364 struct trace_event_call *call = file->event_call;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400365 struct trace_array *tr = file->tr;
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400366 unsigned long file_flags = file->flags;
Li Zefan3b8e4272009-12-08 11:14:52 +0800367 int ret = 0;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400368 int disable;
Li Zefan3b8e4272009-12-08 11:14:52 +0800369
Steven Rostedtfd994982009-02-28 02:41:25 -0500370 switch (enable) {
371 case 0:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400372 /*
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900373 * When soft_disable is set and enable is cleared, the sm_ref
374 * reference counter is decremented. If it reaches 0, we want
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400375 * to clear the SOFT_DISABLED flag but leave the event in the
376 * state that it was. That is, if the event was enabled and
377 * SOFT_DISABLED isn't set, then do nothing. But if SOFT_DISABLED
378 * is set we do not want the event to be enabled before we
379 * clear the bit.
380 *
381 * When soft_disable is not set but the SOFT_MODE flag is,
382 * we do nothing. Do not disable the tracepoint, otherwise
383 * "soft enable"s (clearing the SOFT_DISABLED bit) wont work.
384 */
385 if (soft_disable) {
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900386 if (atomic_dec_return(&file->sm_ref) > 0)
387 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400388 disable = file->flags & EVENT_FILE_FL_SOFT_DISABLED;
389 clear_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400390 } else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400391 disable = !(file->flags & EVENT_FILE_FL_SOFT_MODE);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400392
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400393 if (disable && (file->flags & EVENT_FILE_FL_ENABLED)) {
394 clear_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
395 if (file->flags & EVENT_FILE_FL_RECORDED_CMD) {
Li Zefane870e9a2010-07-02 11:07:32 +0800396 tracing_stop_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400397 clear_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800398 }
Joel Fernandesd914ba32017-06-26 19:01:55 -0700399
400 if (file->flags & EVENT_FILE_FL_RECORDED_TGID) {
401 tracing_stop_tgid_record();
Chunyu Hu7685ab62017-09-05 13:36:46 +0800402 clear_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
Joel Fernandesd914ba32017-06-26 19:01:55 -0700403 }
404
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400405 call->class->reg(call, TRACE_REG_UNREGISTER, file);
Steven Rostedtfd994982009-02-28 02:41:25 -0500406 }
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500407 /* If in SOFT_MODE, just set the SOFT_DISABLE_BIT, else clear it */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400408 if (file->flags & EVENT_FILE_FL_SOFT_MODE)
409 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Tom Zanussi3baa5e42013-06-29 00:08:07 -0500410 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400411 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500412 break;
413 case 1:
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400414 /*
415 * When soft_disable is set and enable is set, we want to
416 * register the tracepoint for the event, but leave the event
417 * as is. That means, if the event was already enabled, we do
418 * nothing (but set SOFT_MODE). If the event is disabled, we
419 * set SOFT_DISABLED before enabling the event tracepoint, so
420 * it still seems to be disabled.
421 */
422 if (!soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400423 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900424 else {
425 if (atomic_inc_return(&file->sm_ref) > 1)
426 break;
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400427 set_bit(EVENT_FILE_FL_SOFT_MODE_BIT, &file->flags);
Masami Hiramatsu1cf4c072013-05-09 14:44:29 +0900428 }
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400429
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400430 if (!(file->flags & EVENT_FILE_FL_ENABLED)) {
Joel Fernandesd914ba32017-06-26 19:01:55 -0700431 bool cmd = false, tgid = false;
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400432
433 /* Keep the event disabled, when going to SOFT_MODE. */
434 if (soft_disable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400435 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400436
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400437 if (tr->trace_flags & TRACE_ITER_RECORD_CMD) {
Joel Fernandesd914ba32017-06-26 19:01:55 -0700438 cmd = true;
Li Zefane870e9a2010-07-02 11:07:32 +0800439 tracing_start_cmdline_record();
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400440 set_bit(EVENT_FILE_FL_RECORDED_CMD_BIT, &file->flags);
Li Zefane870e9a2010-07-02 11:07:32 +0800441 }
Joel Fernandesd914ba32017-06-26 19:01:55 -0700442
443 if (tr->trace_flags & TRACE_ITER_RECORD_TGID) {
444 tgid = true;
445 tracing_start_tgid_record();
446 set_bit(EVENT_FILE_FL_RECORDED_TGID_BIT, &file->flags);
447 }
448
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400449 ret = call->class->reg(call, TRACE_REG_REGISTER, file);
Li Zefan3b8e4272009-12-08 11:14:52 +0800450 if (ret) {
Joel Fernandesd914ba32017-06-26 19:01:55 -0700451 if (cmd)
452 tracing_stop_cmdline_record();
453 if (tgid)
454 tracing_stop_tgid_record();
Li Zefan3b8e4272009-12-08 11:14:52 +0800455 pr_info("event trace: Could not enable event "
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400456 "%s\n", trace_event_name(call));
Li Zefan3b8e4272009-12-08 11:14:52 +0800457 break;
458 }
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400459 set_bit(EVENT_FILE_FL_ENABLED_BIT, &file->flags);
Steven Rostedt (Red Hat)575380d2013-03-04 23:05:12 -0500460
461 /* WAS_ENABLED gets set but never cleared. */
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -0400462 set_bit(EVENT_FILE_FL_WAS_ENABLED_BIT, &file->flags);
Steven Rostedtfd994982009-02-28 02:41:25 -0500463 }
Steven Rostedtfd994982009-02-28 02:41:25 -0500464 break;
465 }
Li Zefan3b8e4272009-12-08 11:14:52 +0800466
Steven Rostedt (Red Hat)0fc1b092016-05-03 17:15:43 -0400467 /* Enable or disable use of trace_buffered_event */
468 if ((file_flags & EVENT_FILE_FL_SOFT_DISABLED) !=
469 (file->flags & EVENT_FILE_FL_SOFT_DISABLED)) {
470 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED)
471 trace_buffered_event_enable();
472 else
473 trace_buffered_event_disable();
474 }
475
Li Zefan3b8e4272009-12-08 11:14:52 +0800476 return ret;
Steven Rostedtfd994982009-02-28 02:41:25 -0500477}
478
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400479int trace_event_enable_disable(struct trace_event_file *file,
Tom Zanussi85f2b082013-10-24 08:59:24 -0500480 int enable, int soft_disable)
481{
482 return __ftrace_event_enable_disable(file, enable, soft_disable);
483}
484
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400485static int ftrace_event_enable_disable(struct trace_event_file *file,
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -0400486 int enable)
487{
488 return __ftrace_event_enable_disable(file, enable, 0);
489}
490
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400491static void ftrace_clear_events(struct trace_array *tr)
Zhaolei0e907c92009-05-25 18:13:59 +0800492{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400493 struct trace_event_file *file;
Zhaolei0e907c92009-05-25 18:13:59 +0800494
495 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400496 list_for_each_entry(file, &tr->events, list) {
497 ftrace_event_enable_disable(file, 0);
Zhaolei0e907c92009-05-25 18:13:59 +0800498 }
499 mutex_unlock(&event_mutex);
500}
501
Steven Rostedtc37775d2016-04-13 16:59:18 -0400502static void
503event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
504{
505 struct trace_pid_list *pid_list;
506 struct trace_array *tr = data;
507
Joel Fernandes (Google)da25a672018-08-02 19:34:07 -0700508 pid_list = rcu_dereference_raw(tr->filtered_pids);
Steven Rostedt4e267db2016-04-14 07:38:13 -0400509 trace_filter_add_remove_task(pid_list, NULL, task);
Steven Rostedtc37775d2016-04-13 16:59:18 -0400510}
511
512static void
513event_filter_pid_sched_process_fork(void *data,
514 struct task_struct *self,
515 struct task_struct *task)
516{
517 struct trace_pid_list *pid_list;
518 struct trace_array *tr = data;
519
520 pid_list = rcu_dereference_sched(tr->filtered_pids);
Steven Rostedt4e267db2016-04-14 07:38:13 -0400521 trace_filter_add_remove_task(pid_list, self, task);
Steven Rostedtc37775d2016-04-13 16:59:18 -0400522}
523
524void trace_event_follow_fork(struct trace_array *tr, bool enable)
525{
526 if (enable) {
527 register_trace_prio_sched_process_fork(event_filter_pid_sched_process_fork,
528 tr, INT_MIN);
529 register_trace_prio_sched_process_exit(event_filter_pid_sched_process_exit,
530 tr, INT_MAX);
531 } else {
532 unregister_trace_sched_process_fork(event_filter_pid_sched_process_fork,
533 tr);
534 unregister_trace_sched_process_exit(event_filter_pid_sched_process_exit,
535 tr);
536 }
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400537}
538
539static void
Linus Torvalds22402cd2015-11-06 13:30:20 -0800540event_filter_pid_sched_switch_probe_pre(void *data, bool preempt,
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400541 struct task_struct *prev, struct task_struct *next)
542{
543 struct trace_array *tr = data;
544 struct trace_pid_list *pid_list;
545
546 pid_list = rcu_dereference_sched(tr->filtered_pids);
547
548 this_cpu_write(tr->trace_buffer.data->ignore_pid,
Steven Rostedt4e267db2016-04-14 07:38:13 -0400549 trace_ignore_this_task(pid_list, prev) &&
550 trace_ignore_this_task(pid_list, next));
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400551}
552
553static void
Linus Torvalds22402cd2015-11-06 13:30:20 -0800554event_filter_pid_sched_switch_probe_post(void *data, bool preempt,
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400555 struct task_struct *prev, struct task_struct *next)
556{
557 struct trace_array *tr = data;
558 struct trace_pid_list *pid_list;
559
560 pid_list = rcu_dereference_sched(tr->filtered_pids);
561
562 this_cpu_write(tr->trace_buffer.data->ignore_pid,
Steven Rostedt4e267db2016-04-14 07:38:13 -0400563 trace_ignore_this_task(pid_list, next));
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400564}
565
566static void
567event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task)
568{
569 struct trace_array *tr = data;
570 struct trace_pid_list *pid_list;
571
572 /* Nothing to do if we are already tracing */
573 if (!this_cpu_read(tr->trace_buffer.data->ignore_pid))
574 return;
575
576 pid_list = rcu_dereference_sched(tr->filtered_pids);
577
578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
Steven Rostedt4e267db2016-04-14 07:38:13 -0400579 trace_ignore_this_task(pid_list, task));
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400580}
581
582static void
583event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task)
584{
585 struct trace_array *tr = data;
586 struct trace_pid_list *pid_list;
587
588 /* Nothing to do if we are not tracing */
589 if (this_cpu_read(tr->trace_buffer.data->ignore_pid))
590 return;
591
592 pid_list = rcu_dereference_sched(tr->filtered_pids);
593
594 /* Set tracing if current is enabled */
595 this_cpu_write(tr->trace_buffer.data->ignore_pid,
Steven Rostedt4e267db2016-04-14 07:38:13 -0400596 trace_ignore_this_task(pid_list, current));
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400597}
598
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400599static void __ftrace_clear_event_pids(struct trace_array *tr)
600{
601 struct trace_pid_list *pid_list;
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400602 struct trace_event_file *file;
603 int cpu;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400604
605 pid_list = rcu_dereference_protected(tr->filtered_pids,
606 lockdep_is_held(&event_mutex));
607 if (!pid_list)
608 return;
609
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400610 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr);
611 unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr);
612
613 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr);
614 unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr);
615
Steven Rostedt (Red Hat)0f72e372015-12-01 16:08:05 -0500616 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre, tr);
617 unregister_trace_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post, tr);
618
619 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_pre, tr);
620 unregister_trace_sched_waking(event_filter_pid_sched_wakeup_probe_post, tr);
621
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -0400622 list_for_each_entry(file, &tr->events, list) {
623 clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
624 }
625
626 for_each_possible_cpu(cpu)
627 per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false;
628
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400629 rcu_assign_pointer(tr->filtered_pids, NULL);
630
631 /* Wait till all users are no longer using pid filtering */
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -0400632 tracepoint_synchronize_unregister();
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400633
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -0400634 trace_free_pid_list(pid_list);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400635}
636
637static void ftrace_clear_event_pids(struct trace_array *tr)
638{
639 mutex_lock(&event_mutex);
640 __ftrace_clear_event_pids(tr);
641 mutex_unlock(&event_mutex);
642}
643
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400644static void __put_system(struct event_subsystem *system)
645{
646 struct event_filter *filter = system->filter;
647
Steven Rostedt6e94a782013-06-27 10:58:31 -0400648 WARN_ON_ONCE(system_refcount(system) == 0);
649 if (system_refcount_dec(system))
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400650 return;
651
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400652 list_del(&system->list);
653
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400654 if (filter) {
655 kfree(filter->filter_string);
656 kfree(filter);
657 }
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +0200658 kfree_const(system->name);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400659 kfree(system);
660}
661
662static void __get_system(struct event_subsystem *system)
663{
Steven Rostedt6e94a782013-06-27 10:58:31 -0400664 WARN_ON_ONCE(system_refcount(system) == 0);
665 system_refcount_inc(system);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400666}
667
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400668static void __get_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400669{
670 WARN_ON_ONCE(dir->ref_count == 0);
671 dir->ref_count++;
672 __get_system(dir->subsystem);
673}
674
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400675static void __put_system_dir(struct trace_subsystem_dir *dir)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400676{
677 WARN_ON_ONCE(dir->ref_count == 0);
678 /* If the subsystem is about to be freed, the dir must be too */
Steven Rostedt6e94a782013-06-27 10:58:31 -0400679 WARN_ON_ONCE(system_refcount(dir->subsystem) == 1 && dir->ref_count != 1);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400680
681 __put_system(dir->subsystem);
682 if (!--dir->ref_count)
683 kfree(dir);
684}
685
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400686static void put_system(struct trace_subsystem_dir *dir)
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400687{
688 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400689 __put_system_dir(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -0400690 mutex_unlock(&event_mutex);
691}
692
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -0400693static void remove_subsystem(struct trace_subsystem_dir *dir)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200694{
695 if (!dir)
696 return;
697
698 if (!--dir->nr_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500699 tracefs_remove_recursive(dir->entry);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200700 list_del(&dir->list);
701 __put_system_dir(dir);
702 }
703}
704
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400705static void remove_event_file_dir(struct trace_event_file *file)
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200706{
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200707 struct dentry *dir = file->dir;
708 struct dentry *child;
709
710 if (dir) {
711 spin_lock(&dir->d_lock); /* probably unneeded */
Al Viro946e51f2014-10-26 19:19:16 -0400712 list_for_each_entry(child, &dir->d_subdirs, d_child) {
David Howells7682c912015-03-17 22:26:16 +0000713 if (d_really_is_positive(child)) /* probably unneeded */
714 d_inode(child)->i_private = NULL;
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200715 }
716 spin_unlock(&dir->d_lock);
717
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -0500718 tracefs_remove_recursive(dir);
Oleg Nesterovbf682c32013-07-28 20:35:27 +0200719 }
720
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200721 list_del(&file->list);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200722 remove_subsystem(file->system);
Oleg Nesterov2448e342014-07-11 21:06:38 +0200723 free_event_filter(file->filter);
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +0200724 kmem_cache_free(file_cachep, file);
725}
726
Li Zefan8f31bfe2009-05-08 10:31:42 +0800727/*
728 * __ftrace_set_clr_event(NULL, NULL, NULL, set) will set/unset all events.
729 */
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400730static int
731__ftrace_set_clr_event_nolock(struct trace_array *tr, const char *match,
732 const char *sub, const char *event, int set)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500733{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400734 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400735 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400736 const char *name;
Steven Rostedt29f93942009-05-08 16:06:47 -0400737 int ret = -EINVAL;
Steven Rostedt (Red Hat)989a0a32016-11-28 13:54:57 -0500738 int eret = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500739
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400740 list_for_each_entry(file, &tr->events, list) {
741
742 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400743 name = trace_event_name(call);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500744
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400745 if (!name || !call->class || !call->class->reg)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500746 continue;
Steven Rostedt1473e442009-02-24 14:15:08 -0500747
Steven Rostedt9b637762012-05-10 15:55:43 -0400748 if (call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
749 continue;
750
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500751 if (match &&
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400752 strcmp(match, name) != 0 &&
Steven Rostedt8f082012010-04-20 10:47:33 -0400753 strcmp(match, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500754 continue;
755
Steven Rostedt8f082012010-04-20 10:47:33 -0400756 if (sub && strcmp(sub, call->class->system) != 0)
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500757 continue;
758
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -0400759 if (event && strcmp(event, name) != 0)
Steven Rostedt1473e442009-02-24 14:15:08 -0500760 continue;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500761
Steven Rostedt (Red Hat)989a0a32016-11-28 13:54:57 -0500762 ret = ftrace_event_enable_disable(file, set);
Steven Rostedtfd994982009-02-28 02:41:25 -0500763
Steven Rostedt (Red Hat)989a0a32016-11-28 13:54:57 -0500764 /*
765 * Save the first error and return that. Some events
766 * may still have been enabled, but let the user
767 * know that something went wrong.
768 */
769 if (ret && !eret)
770 eret = ret;
771
772 ret = eret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500773 }
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -0400774
775 return ret;
776}
777
778static int __ftrace_set_clr_event(struct trace_array *tr, const char *match,
779 const char *sub, const char *event, int set)
780{
781 int ret;
782
783 mutex_lock(&event_mutex);
784 ret = __ftrace_set_clr_event_nolock(tr, match, sub, event, set);
Steven Rostedt11a241a2009-03-02 11:49:04 -0500785 mutex_unlock(&event_mutex);
786
Steven Rostedtb628b3e2009-02-27 23:32:58 -0500787 return ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500788}
789
Denis Efremov595a4382019-07-04 20:21:10 +0300790int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set)
Li Zefan8f31bfe2009-05-08 10:31:42 +0800791{
792 char *event = NULL, *sub = NULL, *match;
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900793 int ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800794
795 /*
796 * The buf format can be <subsystem>:<event-name>
797 * *:<event-name> means any event by that name.
798 * :<event-name> is the same.
799 *
800 * <subsystem>:* means all events in that subsystem
801 * <subsystem>: means the same.
802 *
803 * <name> (no ':') means all events in a subsystem with
804 * the name <name> or any event that matches <name>
805 */
806
807 match = strsep(&buf, ":");
808 if (buf) {
809 sub = match;
810 event = buf;
811 match = NULL;
812
813 if (!strlen(sub) || strcmp(sub, "*") == 0)
814 sub = NULL;
815 if (!strlen(event) || strcmp(event, "*") == 0)
816 event = NULL;
817 }
818
Joonsoo Kim84fce9d2015-04-16 13:44:44 +0900819 ret = __ftrace_set_clr_event(tr, match, sub, event, set);
820
821 /* Put back the colon to allow this to be called again */
822 if (buf)
823 *(buf - 1) = ':';
824
825 return ret;
Li Zefan8f31bfe2009-05-08 10:31:42 +0800826}
Divya Indif45d1222019-03-20 11:28:51 -0700827EXPORT_SYMBOL_GPL(ftrace_set_clr_event);
Li Zefan8f31bfe2009-05-08 10:31:42 +0800828
Steven Rostedt4671c792009-05-08 16:27:41 -0400829/**
830 * trace_set_clr_event - enable or disable an event
831 * @system: system name to match (NULL for any system)
832 * @event: event name to match (NULL for all events, within system)
833 * @set: 1 to enable, 0 to disable
834 *
835 * This is a way for other parts of the kernel to enable or disable
836 * event recording.
837 *
838 * Returns 0 on success, -EINVAL if the parameters do not match any
839 * registered events.
840 */
841int trace_set_clr_event(const char *system, const char *event, int set)
842{
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400843 struct trace_array *tr = top_trace_array();
844
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +0900845 if (!tr)
846 return -ENODEV;
847
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400848 return __ftrace_set_clr_event(tr, NULL, system, event, set);
Steven Rostedt4671c792009-05-08 16:27:41 -0400849}
Yuanhan Liu56355b82010-11-08 14:05:12 +0800850EXPORT_SYMBOL_GPL(trace_set_clr_event);
Steven Rostedt4671c792009-05-08 16:27:41 -0400851
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500852/* 128 should be much more than enough */
853#define EVENT_BUF_SIZE 127
854
855static ssize_t
856ftrace_event_write(struct file *file, const char __user *ubuf,
857 size_t cnt, loff_t *ppos)
858{
jolsa@redhat.com48966362009-09-11 17:29:28 +0200859 struct trace_parser parser;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400860 struct seq_file *m = file->private_data;
861 struct trace_array *tr = m->private;
Li Zefan4ba79782009-09-22 13:52:20 +0800862 ssize_t read, ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500863
Li Zefan4ba79782009-09-22 13:52:20 +0800864 if (!cnt)
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500865 return 0;
866
Steven Rostedt1852fcc2009-03-11 14:33:00 -0400867 ret = tracing_update_buffers();
868 if (ret < 0)
869 return ret;
870
jolsa@redhat.com48966362009-09-11 17:29:28 +0200871 if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500872 return -ENOMEM;
873
jolsa@redhat.com48966362009-09-11 17:29:28 +0200874 read = trace_get_user(&parser, ubuf, cnt, ppos);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500875
Li Zefan4ba79782009-09-22 13:52:20 +0800876 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com48966362009-09-11 17:29:28 +0200877 int set = 1;
878
879 if (*parser.buffer == '!')
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500880 set = 0;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500881
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400882 ret = ftrace_set_clr_event(tr, parser.buffer + !set, set);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500883 if (ret)
jolsa@redhat.com48966362009-09-11 17:29:28 +0200884 goto out_put;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500885 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500886
887 ret = read;
888
jolsa@redhat.com48966362009-09-11 17:29:28 +0200889 out_put:
890 trace_parser_put(&parser);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500891
892 return ret;
893}
894
895static void *
896t_next(struct seq_file *m, void *v, loff_t *pos)
897{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400898 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400899 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400900 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500901
902 (*pos)++;
903
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400904 list_for_each_entry_continue(file, &tr->events, list) {
905 call = file->event_call;
Steven Rostedt40e26812009-03-10 11:32:40 -0400906 /*
907 * The ftrace subsystem is for showing formats only.
908 * They can not be enabled or disabled via the event files.
909 */
Steven Rostedt (Red Hat)d0454372016-02-24 09:04:24 -0500910 if (call->class && call->class->reg &&
911 !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400912 return file;
Steven Rostedt40e26812009-03-10 11:32:40 -0400913 }
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500914
Li Zefan30bd39c2009-09-18 14:07:05 +0800915 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500916}
917
918static void *t_start(struct seq_file *m, loff_t *pos)
919{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400920 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400921 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800922 loff_t l;
923
Li Zefan20c89282009-05-06 10:33:45 +0800924 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800925
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400926 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800927 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400928 file = t_next(m, file, &l);
929 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800930 break;
931 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400932 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500933}
934
935static void *
936s_next(struct seq_file *m, void *v, loff_t *pos)
937{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400938 struct trace_event_file *file = v;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400939 struct trace_array *tr = m->private;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500940
941 (*pos)++;
942
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400943 list_for_each_entry_continue(file, &tr->events, list) {
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -0400944 if (file->flags & EVENT_FILE_FL_ENABLED)
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400945 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500946 }
947
Li Zefan30bd39c2009-09-18 14:07:05 +0800948 return NULL;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500949}
950
951static void *s_start(struct seq_file *m, loff_t *pos)
952{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400953 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400954 struct trace_array *tr = m->private;
Li Zefane1c7e2a2009-06-24 09:52:29 +0800955 loff_t l;
956
Li Zefan20c89282009-05-06 10:33:45 +0800957 mutex_lock(&event_mutex);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800958
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400959 file = list_entry(&tr->events, struct trace_event_file, list);
Li Zefane1c7e2a2009-06-24 09:52:29 +0800960 for (l = 0; l <= *pos; ) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400961 file = s_next(m, file, &l);
962 if (!file)
Li Zefane1c7e2a2009-06-24 09:52:29 +0800963 break;
964 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -0400965 return file;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500966}
967
968static int t_show(struct seq_file *m, void *v)
969{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -0400970 struct trace_event_file *file = v;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400971 struct trace_event_call *call = file->event_call;
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500972
Steven Rostedt8f082012010-04-20 10:47:33 -0400973 if (strcmp(call->class->system, TRACE_SYSTEM) != 0)
974 seq_printf(m, "%s:", call->class->system);
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -0400975 seq_printf(m, "%s\n", trace_event_name(call));
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500976
977 return 0;
978}
979
980static void t_stop(struct seq_file *m, void *p)
981{
Li Zefan20c89282009-05-06 10:33:45 +0800982 mutex_unlock(&event_mutex);
Steven Rostedtb77e38a2009-02-24 10:21:36 -0500983}
984
Steven Rostedtf4d34a82016-04-13 16:27:49 -0400985static void *
986p_next(struct seq_file *m, void *v, loff_t *pos)
987{
988 struct trace_array *tr = m->private;
989 struct trace_pid_list *pid_list = rcu_dereference_sched(tr->filtered_pids);
Steven Rostedtf4d34a82016-04-13 16:27:49 -0400990
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -0400991 return trace_pid_next(pid_list, v, pos);
Steven Rostedtf4d34a82016-04-13 16:27:49 -0400992}
993
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400994static void *p_start(struct seq_file *m, loff_t *pos)
Steven Rostedt (Red Hat)fb662282015-10-26 03:45:22 -0400995 __acquires(RCU)
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -0400996{
997 struct trace_pid_list *pid_list;
998 struct trace_array *tr = m->private;
999
1000 /*
1001 * Grab the mutex, to keep calls to p_next() having the same
1002 * tr->filtered_pids as p_start() has.
1003 * If we just passed the tr->filtered_pids around, then RCU would
1004 * have been enough, but doing that makes things more complex.
1005 */
1006 mutex_lock(&event_mutex);
1007 rcu_read_lock_sched();
1008
1009 pid_list = rcu_dereference_sched(tr->filtered_pids);
1010
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001011 if (!pid_list)
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001012 return NULL;
1013
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -04001014 return trace_pid_start(pid_list, pos);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001015}
1016
1017static void p_stop(struct seq_file *m, void *p)
Steven Rostedt (Red Hat)fb662282015-10-26 03:45:22 -04001018 __releases(RCU)
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001019{
1020 rcu_read_unlock_sched();
1021 mutex_unlock(&event_mutex);
1022}
1023
Steven Rostedt1473e442009-02-24 14:15:08 -05001024static ssize_t
1025event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1026 loff_t *ppos)
1027{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001028 struct trace_event_file *file;
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001029 unsigned long flags;
Tom Zanussia4390592013-06-29 00:08:04 -05001030 char buf[4] = "0";
Steven Rostedt1473e442009-02-24 14:15:08 -05001031
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001032 mutex_lock(&event_mutex);
1033 file = event_file_data(filp);
1034 if (likely(file))
1035 flags = file->flags;
1036 mutex_unlock(&event_mutex);
1037
1038 if (!file)
1039 return -ENODEV;
1040
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001041 if (flags & EVENT_FILE_FL_ENABLED &&
1042 !(flags & EVENT_FILE_FL_SOFT_DISABLED))
Tom Zanussia4390592013-06-29 00:08:04 -05001043 strcpy(buf, "1");
1044
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001045 if (flags & EVENT_FILE_FL_SOFT_DISABLED ||
1046 flags & EVENT_FILE_FL_SOFT_MODE)
Tom Zanussia4390592013-06-29 00:08:04 -05001047 strcat(buf, "*");
1048
1049 strcat(buf, "\n");
Steven Rostedt1473e442009-02-24 14:15:08 -05001050
Steven Rostedt (Red Hat)417944c2013-03-12 13:26:18 -04001051 return simple_read_from_buffer(ubuf, cnt, ppos, buf, strlen(buf));
Steven Rostedt1473e442009-02-24 14:15:08 -05001052}
1053
1054static ssize_t
1055event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1056 loff_t *ppos)
1057{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001058 struct trace_event_file *file;
Steven Rostedt1473e442009-02-24 14:15:08 -05001059 unsigned long val;
1060 int ret;
1061
Peter Huewe22fe9b52011-06-07 21:58:27 +02001062 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1063 if (ret)
Steven Rostedt1473e442009-02-24 14:15:08 -05001064 return ret;
1065
Steven Rostedt1852fcc2009-03-11 14:33:00 -04001066 ret = tracing_update_buffers();
1067 if (ret < 0)
1068 return ret;
1069
Steven Rostedt1473e442009-02-24 14:15:08 -05001070 switch (val) {
1071 case 0:
Steven Rostedt1473e442009-02-24 14:15:08 -05001072 case 1:
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001073 ret = -ENODEV;
Steven Rostedt11a241a2009-03-02 11:49:04 -05001074 mutex_lock(&event_mutex);
Oleg Nesterovbc6f6b02013-07-26 19:25:36 +02001075 file = event_file_data(filp);
1076 if (likely(file))
1077 ret = ftrace_event_enable_disable(file, val);
Steven Rostedt11a241a2009-03-02 11:49:04 -05001078 mutex_unlock(&event_mutex);
Steven Rostedt1473e442009-02-24 14:15:08 -05001079 break;
1080
1081 default:
1082 return -EINVAL;
1083 }
1084
1085 *ppos += cnt;
1086
Li Zefan3b8e4272009-12-08 11:14:52 +08001087 return ret ? ret : cnt;
Steven Rostedt1473e442009-02-24 14:15:08 -05001088}
1089
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001090static ssize_t
1091system_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
1092 loff_t *ppos)
1093{
Li Zefanc142b152009-05-08 10:32:05 +08001094 const char set_to_char[4] = { '?', '0', '1', 'X' };
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001095 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001096 struct event_subsystem *system = dir->subsystem;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001097 struct trace_event_call *call;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001098 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001099 struct trace_array *tr = dir->tr;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001100 char buf[2];
Li Zefanc142b152009-05-08 10:32:05 +08001101 int set = 0;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001102 int ret;
1103
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001104 mutex_lock(&event_mutex);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001105 list_for_each_entry(file, &tr->events, list) {
1106 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001107 if (!trace_event_name(call) || !call->class || !call->class->reg)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001108 continue;
1109
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001110 if (system && strcmp(call->class->system, system->name) != 0)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001111 continue;
1112
1113 /*
1114 * We need to find out if all the events are set
1115 * or if all events or cleared, or if we have
1116 * a mixture.
1117 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04001118 set |= (1 << !!(file->flags & EVENT_FILE_FL_ENABLED));
Li Zefanc142b152009-05-08 10:32:05 +08001119
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001120 /*
1121 * If we have a mixture, no need to look further.
1122 */
Li Zefanc142b152009-05-08 10:32:05 +08001123 if (set == 3)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001124 break;
1125 }
1126 mutex_unlock(&event_mutex);
1127
Li Zefanc142b152009-05-08 10:32:05 +08001128 buf[0] = set_to_char[set];
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001129 buf[1] = '\n';
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001130
1131 ret = simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
1132
1133 return ret;
1134}
1135
1136static ssize_t
1137system_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
1138 loff_t *ppos)
1139{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001140 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001141 struct event_subsystem *system = dir->subsystem;
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001142 const char *name = NULL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001143 unsigned long val;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001144 ssize_t ret;
1145
Peter Huewe22fe9b52011-06-07 21:58:27 +02001146 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1147 if (ret)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001148 return ret;
1149
1150 ret = tracing_update_buffers();
1151 if (ret < 0)
1152 return ret;
1153
Li Zefan8f31bfe2009-05-08 10:31:42 +08001154 if (val != 0 && val != 1)
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001155 return -EINVAL;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001156
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001157 /*
1158 * Opening of "enable" adds a ref count to system,
1159 * so the name is safe to use.
1160 */
1161 if (system)
1162 name = system->name;
1163
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001164 ret = __ftrace_set_clr_event(dir->tr, NULL, name, NULL, val);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001165 if (ret)
Li Zefan8f31bfe2009-05-08 10:31:42 +08001166 goto out;
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001167
1168 ret = cnt;
1169
Li Zefan8f31bfe2009-05-08 10:31:42 +08001170out:
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001171 *ppos += cnt;
1172
1173 return ret;
1174}
1175
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001176enum {
1177 FORMAT_HEADER = 1,
Li Zefan86397dc2010-08-17 13:53:06 +08001178 FORMAT_FIELD_SEPERATOR = 2,
1179 FORMAT_PRINTFMT = 3,
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001180};
1181
1182static void *f_next(struct seq_file *m, void *v, loff_t *pos)
Steven Rostedt981d0812009-03-02 13:53:59 -05001183{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001184 struct trace_event_call *call = event_file_data(m->private);
Li Zefan86397dc2010-08-17 13:53:06 +08001185 struct list_head *common_head = &ftrace_common_fields;
1186 struct list_head *head = trace_get_fields(call);
Oleg Nesterov7710b632013-07-18 20:47:10 +02001187 struct list_head *node = v;
Steven Rostedt981d0812009-03-02 13:53:59 -05001188
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001189 (*pos)++;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001190
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001191 switch ((unsigned long)v) {
1192 case FORMAT_HEADER:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001193 node = common_head;
1194 break;
Li Zefan86397dc2010-08-17 13:53:06 +08001195
1196 case FORMAT_FIELD_SEPERATOR:
Oleg Nesterov7710b632013-07-18 20:47:10 +02001197 node = head;
1198 break;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001199
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001200 case FORMAT_PRINTFMT:
1201 /* all done */
1202 return NULL;
Lai Jiangshan5a65e952009-12-15 15:39:53 +08001203 }
1204
Oleg Nesterov7710b632013-07-18 20:47:10 +02001205 node = node->prev;
1206 if (node == common_head)
Li Zefan86397dc2010-08-17 13:53:06 +08001207 return (void *)FORMAT_FIELD_SEPERATOR;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001208 else if (node == head)
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001209 return (void *)FORMAT_PRINTFMT;
Oleg Nesterov7710b632013-07-18 20:47:10 +02001210 else
1211 return node;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001212}
1213
1214static int f_show(struct seq_file *m, void *v)
1215{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001216 struct trace_event_call *call = event_file_data(m->private);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001217 struct ftrace_event_field *field;
1218 const char *array_descriptor;
1219
1220 switch ((unsigned long)v) {
1221 case FORMAT_HEADER:
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001222 seq_printf(m, "name: %s\n", trace_event_name(call));
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001223 seq_printf(m, "ID: %d\n", call->event.type);
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001224 seq_puts(m, "format:\n");
Li Zefan8728fe52010-05-24 16:22:49 +08001225 return 0;
1226
Li Zefan86397dc2010-08-17 13:53:06 +08001227 case FORMAT_FIELD_SEPERATOR:
1228 seq_putc(m, '\n');
1229 return 0;
1230
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001231 case FORMAT_PRINTFMT:
1232 seq_printf(m, "\nprint fmt: %s\n",
1233 call->print_fmt);
1234 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001235 }
1236
Oleg Nesterov7710b632013-07-18 20:47:10 +02001237 field = list_entry(v, struct ftrace_event_field, link);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001238 /*
1239 * Smartly shows the array type(except dynamic array).
1240 * Normal:
1241 * field:TYPE VAR
1242 * If TYPE := TYPE[LEN], it is shown:
1243 * field:TYPE VAR[LEN]
1244 */
1245 array_descriptor = strchr(field->type, '[');
1246
Steven Rostedt (VMware)b6b27352018-12-20 13:20:07 -05001247 if (str_has_prefix(field->type, "__data_loc"))
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001248 array_descriptor = NULL;
1249
1250 if (!array_descriptor)
1251 seq_printf(m, "\tfield:%s %s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1252 field->type, field->name, field->offset,
1253 field->size, !!field->is_signed);
1254 else
1255 seq_printf(m, "\tfield:%.*s %s%s;\toffset:%u;\tsize:%u;\tsigned:%d;\n",
1256 (int)(array_descriptor - field->type),
1257 field->type, field->name,
1258 array_descriptor, field->offset,
1259 field->size, !!field->is_signed);
1260
1261 return 0;
1262}
1263
Oleg Nesterov7710b632013-07-18 20:47:10 +02001264static void *f_start(struct seq_file *m, loff_t *pos)
1265{
1266 void *p = (void *)FORMAT_HEADER;
1267 loff_t l = 0;
1268
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001269 /* ->stop() is called even if ->start() fails */
1270 mutex_lock(&event_mutex);
1271 if (!event_file_data(m->private))
1272 return ERR_PTR(-ENODEV);
1273
Oleg Nesterov7710b632013-07-18 20:47:10 +02001274 while (l < *pos && p)
1275 p = f_next(m, p, &l);
1276
1277 return p;
1278}
1279
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001280static void f_stop(struct seq_file *m, void *p)
1281{
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001282 mutex_unlock(&event_mutex);
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001283}
1284
1285static const struct seq_operations trace_format_seq_ops = {
1286 .start = f_start,
1287 .next = f_next,
1288 .stop = f_stop,
1289 .show = f_show,
1290};
1291
1292static int trace_format_open(struct inode *inode, struct file *file)
1293{
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001294 struct seq_file *m;
1295 int ret;
1296
1297 ret = seq_open(file, &trace_format_seq_ops);
1298 if (ret < 0)
1299 return ret;
1300
1301 m = file->private_data;
Oleg Nesterovc5a44a12013-07-26 19:25:43 +02001302 m->private = file;
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001303
1304 return 0;
Steven Rostedt981d0812009-03-02 13:53:59 -05001305}
1306
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001307static ssize_t
1308event_id_read(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1309{
Oleg Nesterov1a111262013-07-26 19:25:32 +02001310 int id = (long)event_file_data(filp);
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001311 char buf[32];
1312 int len;
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001313
Oleg Nesterov1a111262013-07-26 19:25:32 +02001314 if (unlikely(!id))
1315 return -ENODEV;
1316
1317 len = sprintf(buf, "%d\n", id);
1318
Oleg Nesterovcd458ba2013-07-18 20:47:12 +02001319 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001320}
1321
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001322static ssize_t
1323event_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1324 loff_t *ppos)
1325{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001326 struct trace_event_file *file;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001327 struct trace_seq *s;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001328 int r = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001329
1330 if (*ppos)
1331 return 0;
1332
1333 s = kmalloc(sizeof(*s), GFP_KERNEL);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001334
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001335 if (!s)
1336 return -ENOMEM;
1337
1338 trace_seq_init(s);
1339
Oleg Nesterove2912b02013-07-26 19:25:40 +02001340 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001341 file = event_file_data(filp);
1342 if (file)
1343 print_event_filter(file, s);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001344 mutex_unlock(&event_mutex);
1345
Tom Zanussif306cc82013-10-24 08:34:17 -05001346 if (file)
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001347 r = simple_read_from_buffer(ubuf, cnt, ppos,
1348 s->buffer, trace_seq_used(s));
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001349
1350 kfree(s);
1351
1352 return r;
1353}
1354
1355static ssize_t
1356event_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1357 loff_t *ppos)
1358{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001359 struct trace_event_file *file;
Tom Zanussi8b372562009-04-28 03:04:59 -05001360 char *buf;
Oleg Nesterove2912b02013-07-26 19:25:40 +02001361 int err = -ENODEV;
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001362
Tom Zanussi8b372562009-04-28 03:04:59 -05001363 if (cnt >= PAGE_SIZE)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001364 return -EINVAL;
1365
Al Viro70f6cbb2015-12-24 00:13:10 -05001366 buf = memdup_user_nul(ubuf, cnt);
1367 if (IS_ERR(buf))
1368 return PTR_ERR(buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001369
Oleg Nesterove2912b02013-07-26 19:25:40 +02001370 mutex_lock(&event_mutex);
Tom Zanussif306cc82013-10-24 08:34:17 -05001371 file = event_file_data(filp);
1372 if (file)
1373 err = apply_event_filter(file, buf);
Oleg Nesterove2912b02013-07-26 19:25:40 +02001374 mutex_unlock(&event_mutex);
1375
Al Viro70f6cbb2015-12-24 00:13:10 -05001376 kfree(buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001377 if (err < 0)
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001378 return err;
Tom Zanussi0a19e532009-04-13 03:17:50 -05001379
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001380 *ppos += cnt;
1381
1382 return cnt;
1383}
1384
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001385static LIST_HEAD(event_subsystems);
1386
1387static int subsystem_open(struct inode *inode, struct file *filp)
1388{
1389 struct event_subsystem *system = NULL;
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001390 struct trace_subsystem_dir *dir = NULL; /* Initialize for gcc */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001391 struct trace_array *tr;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001392 int ret;
1393
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001394 if (tracing_is_disabled())
1395 return -ENODEV;
1396
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001397 /* Make sure the system still exists */
1398 mutex_lock(&event_mutex);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04001399 mutex_lock(&trace_types_lock);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001400 list_for_each_entry(tr, &ftrace_trace_arrays, list) {
1401 list_for_each_entry(dir, &tr->systems, list) {
1402 if (dir == inode->i_private) {
1403 /* Don't open systems with no events */
1404 if (dir->nr_events) {
1405 __get_system_dir(dir);
1406 system = dir->subsystem;
1407 }
1408 goto exit_loop;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001409 }
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001410 }
1411 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001412 exit_loop:
Alexander Z Lama8227412013-07-01 19:37:54 -07001413 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04001414 mutex_unlock(&event_mutex);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001415
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001416 if (!system)
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001417 return -ENODEV;
1418
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001419 /* Some versions of gcc think dir can be uninitialized here */
1420 WARN_ON(!dir);
1421
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001422 /* Still need to increment the ref count of the system */
1423 if (trace_array_get(tr) < 0) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001424 put_system(dir);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001425 return -ENODEV;
1426 }
1427
1428 ret = tracing_open_generic(inode, filp);
1429 if (ret < 0) {
1430 trace_array_put(tr);
1431 put_system(dir);
1432 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001433
1434 return ret;
1435}
1436
1437static int system_tr_open(struct inode *inode, struct file *filp)
1438{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001439 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001440 struct trace_array *tr = inode->i_private;
1441 int ret;
1442
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001443 if (tracing_is_disabled())
1444 return -ENODEV;
1445
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001446 if (trace_array_get(tr) < 0)
1447 return -ENODEV;
1448
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001449 /* Make a temporary dir that has no system but points to tr */
1450 dir = kzalloc(sizeof(*dir), GFP_KERNEL);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001451 if (!dir) {
1452 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001453 return -ENOMEM;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001454 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001455
1456 dir->tr = tr;
1457
1458 ret = tracing_open_generic(inode, filp);
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001459 if (ret < 0) {
1460 trace_array_put(tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001461 kfree(dir);
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001462 return ret;
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001463 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001464
1465 filp->private_data = dir;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001466
Geyslan G. Bemd6d35232013-11-06 16:02:51 -03001467 return 0;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001468}
1469
1470static int subsystem_release(struct inode *inode, struct file *file)
1471{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001472 struct trace_subsystem_dir *dir = file->private_data;
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001473
Steven Rostedt (Red Hat)8e2e2fa2013-07-02 15:30:53 -04001474 trace_array_put(dir->tr);
1475
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001476 /*
1477 * If dir->subsystem is NULL, then this is a temporary
1478 * descriptor that was made for a trace_array to enable
1479 * all subsystems.
1480 */
1481 if (dir->subsystem)
1482 put_system(dir);
1483 else
1484 kfree(dir);
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001485
1486 return 0;
1487}
1488
Tom Zanussicfb180f2009-03-22 03:31:17 -05001489static ssize_t
1490subsystem_filter_read(struct file *filp, char __user *ubuf, size_t cnt,
1491 loff_t *ppos)
1492{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001493 struct trace_subsystem_dir *dir = filp->private_data;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001494 struct event_subsystem *system = dir->subsystem;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001495 struct trace_seq *s;
1496 int r;
1497
1498 if (*ppos)
1499 return 0;
1500
1501 s = kmalloc(sizeof(*s), GFP_KERNEL);
1502 if (!s)
1503 return -ENOMEM;
1504
1505 trace_seq_init(s);
1506
Tom Zanussi8b372562009-04-28 03:04:59 -05001507 print_subsystem_event_filter(system, s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001508 r = simple_read_from_buffer(ubuf, cnt, ppos,
1509 s->buffer, trace_seq_used(s));
Tom Zanussicfb180f2009-03-22 03:31:17 -05001510
1511 kfree(s);
1512
1513 return r;
1514}
1515
1516static ssize_t
1517subsystem_filter_write(struct file *filp, const char __user *ubuf, size_t cnt,
1518 loff_t *ppos)
1519{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001520 struct trace_subsystem_dir *dir = filp->private_data;
Tom Zanussi8b372562009-04-28 03:04:59 -05001521 char *buf;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001522 int err;
1523
Tom Zanussi8b372562009-04-28 03:04:59 -05001524 if (cnt >= PAGE_SIZE)
Tom Zanussicfb180f2009-03-22 03:31:17 -05001525 return -EINVAL;
1526
Al Viro70f6cbb2015-12-24 00:13:10 -05001527 buf = memdup_user_nul(ubuf, cnt);
1528 if (IS_ERR(buf))
1529 return PTR_ERR(buf);
Tom Zanussicfb180f2009-03-22 03:31:17 -05001530
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001531 err = apply_subsystem_event_filter(dir, buf);
Al Viro70f6cbb2015-12-24 00:13:10 -05001532 kfree(buf);
Tom Zanussi8b372562009-04-28 03:04:59 -05001533 if (err < 0)
Li Zefan44e9c8b2009-04-11 15:55:28 +08001534 return err;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001535
1536 *ppos += cnt;
1537
1538 return cnt;
1539}
1540
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001541static ssize_t
1542show_header(struct file *filp, char __user *ubuf, size_t cnt, loff_t *ppos)
1543{
1544 int (*func)(struct trace_seq *s) = filp->private_data;
1545 struct trace_seq *s;
1546 int r;
1547
1548 if (*ppos)
1549 return 0;
1550
1551 s = kmalloc(sizeof(*s), GFP_KERNEL);
1552 if (!s)
1553 return -ENOMEM;
1554
1555 trace_seq_init(s);
1556
1557 func(s);
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001558 r = simple_read_from_buffer(ubuf, cnt, ppos,
1559 s->buffer, trace_seq_used(s));
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001560
1561 kfree(s);
1562
1563 return r;
1564}
1565
Steven Rostedt (Red Hat)8ca532a2015-10-21 15:27:36 -04001566static void ignore_task_cpu(void *data)
1567{
1568 struct trace_array *tr = data;
1569 struct trace_pid_list *pid_list;
1570
1571 /*
1572 * This function is called by on_each_cpu() while the
1573 * event_mutex is held.
1574 */
1575 pid_list = rcu_dereference_protected(tr->filtered_pids,
1576 mutex_is_locked(&event_mutex));
1577
1578 this_cpu_write(tr->trace_buffer.data->ignore_pid,
Steven Rostedt4e267db2016-04-14 07:38:13 -04001579 trace_ignore_this_task(pid_list, current));
Steven Rostedt (Red Hat)8ca532a2015-10-21 15:27:36 -04001580}
1581
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001582static ssize_t
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001583ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001584 size_t cnt, loff_t *ppos)
1585{
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001586 struct seq_file *m = filp->private_data;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001587 struct trace_array *tr = m->private;
1588 struct trace_pid_list *filtered_pids = NULL;
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001589 struct trace_pid_list *pid_list;
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001590 struct trace_event_file *file;
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -04001591 ssize_t ret;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001592
1593 if (!cnt)
1594 return 0;
1595
1596 ret = tracing_update_buffers();
1597 if (ret < 0)
1598 return ret;
1599
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001600 mutex_lock(&event_mutex);
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -04001601
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001602 filtered_pids = rcu_dereference_protected(tr->filtered_pids,
1603 lockdep_is_held(&event_mutex));
1604
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -04001605 ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1606 if (ret < 0)
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001607 goto out;
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001608
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001609 rcu_assign_pointer(tr->filtered_pids, pid_list);
1610
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001611 list_for_each_entry(file, &tr->events, list) {
1612 set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags);
1613 }
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001614
1615 if (filtered_pids) {
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -04001616 tracepoint_synchronize_unregister();
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -04001617 trace_free_pid_list(filtered_pids);
1618 } else if (pid_list) {
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001619 /*
1620 * Register a probe that is called before all other probes
1621 * to set ignore_pid if next or prev do not match.
1622 * Register a probe this is called after all other probes
1623 * to only keep ignore_pid set if next pid matches.
1624 */
1625 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre,
1626 tr, INT_MAX);
1627 register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post,
1628 tr, 0);
1629
1630 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre,
1631 tr, INT_MAX);
1632 register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post,
1633 tr, 0);
Steven Rostedt (Red Hat)0f72e372015-12-01 16:08:05 -05001634
1635 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_pre,
1636 tr, INT_MAX);
1637 register_trace_prio_sched_wakeup_new(event_filter_pid_sched_wakeup_probe_post,
1638 tr, 0);
1639
1640 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_pre,
1641 tr, INT_MAX);
1642 register_trace_prio_sched_waking(event_filter_pid_sched_wakeup_probe_post,
1643 tr, 0);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001644 }
1645
Steven Rostedt (Red Hat)799fd442015-11-02 13:08:26 -05001646 /*
1647 * Ignoring of pids is done at task switch. But we have to
1648 * check for those tasks that are currently running.
1649 * Always do this in case a pid was appended or removed.
1650 */
1651 on_each_cpu(ignore_task_cpu, tr, 1);
1652
Steven Rostedtf4d34a82016-04-13 16:27:49 -04001653 out:
Steven Rostedt (Red Hat)3fdaf802015-09-25 12:58:44 -04001654 mutex_unlock(&event_mutex);
1655
Steven Rostedt (Red Hat)76c813e2016-04-21 11:35:30 -04001656 if (ret > 0)
1657 *ppos += ret;
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001658
1659 return ret;
1660}
1661
Steven Rostedt15075ca2012-05-03 14:57:28 -04001662static int ftrace_event_avail_open(struct inode *inode, struct file *file);
1663static int ftrace_event_set_open(struct inode *inode, struct file *file);
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001664static int ftrace_event_set_pid_open(struct inode *inode, struct file *file);
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001665static int ftrace_event_release(struct inode *inode, struct file *file);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001666
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001667static const struct seq_operations show_event_seq_ops = {
1668 .start = t_start,
1669 .next = t_next,
1670 .show = t_show,
1671 .stop = t_stop,
1672};
1673
1674static const struct seq_operations show_set_event_seq_ops = {
1675 .start = s_start,
1676 .next = s_next,
1677 .show = t_show,
1678 .stop = t_stop,
1679};
1680
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001681static const struct seq_operations show_set_pid_seq_ops = {
1682 .start = p_start,
1683 .next = p_next,
Steven Rostedt (Red Hat)5cc89762016-04-20 15:19:54 -04001684 .show = trace_pid_show,
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001685 .stop = p_stop,
1686};
1687
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001688static const struct file_operations ftrace_avail_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001689 .open = ftrace_event_avail_open,
Steven Rostedt2314c4a2009-03-10 12:04:02 -04001690 .read = seq_read,
1691 .llseek = seq_lseek,
1692 .release = seq_release,
1693};
1694
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001695static const struct file_operations ftrace_set_event_fops = {
Steven Rostedt15075ca2012-05-03 14:57:28 -04001696 .open = ftrace_event_set_open,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001697 .read = seq_read,
1698 .write = ftrace_event_write,
1699 .llseek = seq_lseek,
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001700 .release = ftrace_event_release,
Steven Rostedtb77e38a2009-02-24 10:21:36 -05001701};
1702
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001703static const struct file_operations ftrace_set_event_pid_fops = {
1704 .open = ftrace_event_set_pid_open,
1705 .read = seq_read,
1706 .write = ftrace_event_pid_write,
1707 .llseek = seq_lseek,
1708 .release = ftrace_event_release,
1709};
1710
Steven Rostedt1473e442009-02-24 14:15:08 -05001711static const struct file_operations ftrace_enable_fops = {
Oleg Nesterovbf682c32013-07-28 20:35:27 +02001712 .open = tracing_open_generic,
Steven Rostedt1473e442009-02-24 14:15:08 -05001713 .read = event_enable_read,
1714 .write = event_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001715 .llseek = default_llseek,
Steven Rostedt1473e442009-02-24 14:15:08 -05001716};
1717
Steven Rostedt981d0812009-03-02 13:53:59 -05001718static const struct file_operations ftrace_event_format_fops = {
Steven Rostedt2a37a3d2010-06-03 15:21:34 -04001719 .open = trace_format_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .release = seq_release,
Steven Rostedt981d0812009-03-02 13:53:59 -05001723};
1724
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001725static const struct file_operations ftrace_event_id_fops = {
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001726 .read = event_id_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001727 .llseek = default_llseek,
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001728};
1729
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001730static const struct file_operations ftrace_event_filter_fops = {
1731 .open = tracing_open_generic,
1732 .read = event_filter_read,
1733 .write = event_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001734 .llseek = default_llseek,
Tom Zanussi7ce7e422009-03-22 03:31:04 -05001735};
1736
Tom Zanussicfb180f2009-03-22 03:31:17 -05001737static const struct file_operations ftrace_subsystem_filter_fops = {
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001738 .open = subsystem_open,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001739 .read = subsystem_filter_read,
1740 .write = subsystem_filter_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001741 .llseek = default_llseek,
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001742 .release = subsystem_release,
Tom Zanussicfb180f2009-03-22 03:31:17 -05001743};
1744
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001745static const struct file_operations ftrace_system_enable_fops = {
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001746 .open = subsystem_open,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001747 .read = system_enable_read,
1748 .write = system_enable_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001749 .llseek = default_llseek,
Steven Rostedt40ee4df2011-07-05 14:32:51 -04001750 .release = subsystem_release,
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001751};
1752
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001753static const struct file_operations ftrace_tr_enable_fops = {
1754 .open = system_tr_open,
1755 .read = system_enable_read,
1756 .write = system_enable_write,
1757 .llseek = default_llseek,
1758 .release = subsystem_release,
1759};
1760
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001761static const struct file_operations ftrace_show_header_fops = {
1762 .open = tracing_open_generic,
1763 .read = show_header,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001764 .llseek = default_llseek,
Steven Rostedtd1b182a2009-04-15 16:53:47 -04001765};
1766
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001767static int
1768ftrace_event_open(struct inode *inode, struct file *file,
1769 const struct seq_operations *seq_ops)
Steven Rostedt1473e442009-02-24 14:15:08 -05001770{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001771 struct seq_file *m;
1772 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001773
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001774 ret = seq_open(file, seq_ops);
1775 if (ret < 0)
1776 return ret;
1777 m = file->private_data;
1778 /* copy tr over to seq ops */
1779 m->private = inode->i_private;
Steven Rostedt1473e442009-02-24 14:15:08 -05001780
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001781 return ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001782}
1783
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001784static int ftrace_event_release(struct inode *inode, struct file *file)
1785{
1786 struct trace_array *tr = inode->i_private;
1787
1788 trace_array_put(tr);
1789
1790 return seq_release(inode, file);
1791}
1792
Steven Rostedt15075ca2012-05-03 14:57:28 -04001793static int
1794ftrace_event_avail_open(struct inode *inode, struct file *file)
1795{
1796 const struct seq_operations *seq_ops = &show_event_seq_ops;
1797
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001798 return ftrace_event_open(inode, file, seq_ops);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001799}
1800
1801static int
1802ftrace_event_set_open(struct inode *inode, struct file *file)
1803{
1804 const struct seq_operations *seq_ops = &show_set_event_seq_ops;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001805 struct trace_array *tr = inode->i_private;
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001806 int ret;
1807
1808 if (trace_array_get(tr) < 0)
1809 return -ENODEV;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001810
1811 if ((file->f_mode & FMODE_WRITE) &&
1812 (file->f_flags & O_TRUNC))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001813 ftrace_clear_events(tr);
Steven Rostedt15075ca2012-05-03 14:57:28 -04001814
Alexander Z Lamf77d09a2013-07-18 11:18:44 -07001815 ret = ftrace_event_open(inode, file, seq_ops);
1816 if (ret < 0)
1817 trace_array_put(tr);
1818 return ret;
Steven Rostedt15075ca2012-05-03 14:57:28 -04001819}
1820
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04001821static int
1822ftrace_event_set_pid_open(struct inode *inode, struct file *file)
1823{
1824 const struct seq_operations *seq_ops = &show_set_pid_seq_ops;
1825 struct trace_array *tr = inode->i_private;
1826 int ret;
1827
1828 if (trace_array_get(tr) < 0)
1829 return -ENODEV;
1830
1831 if ((file->f_mode & FMODE_WRITE) &&
1832 (file->f_flags & O_TRUNC))
1833 ftrace_clear_event_pids(tr);
1834
1835 ret = ftrace_event_open(inode, file, seq_ops);
1836 if (ret < 0)
1837 trace_array_put(tr);
1838 return ret;
1839}
1840
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001841static struct event_subsystem *
1842create_new_subsystem(const char *name)
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001843{
1844 struct event_subsystem *system;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001845
1846 /* need to create new entry */
1847 system = kmalloc(sizeof(*system), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001848 if (!system)
1849 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001850
Steven Rostedte9dbfae2011-07-05 11:36:06 -04001851 system->ref_count = 1;
Steven Rostedt6e94a782013-06-27 10:58:31 -04001852
1853 /* Only allocate if dynamic (kprobes and modules) */
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001854 system->name = kstrdup_const(name, GFP_KERNEL);
1855 if (!system->name)
1856 goto out_free;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001857
Tom Zanussi30e673b2009-04-28 03:04:47 -05001858 system->filter = NULL;
Tom Zanussicfb180f2009-03-22 03:31:17 -05001859
Tom Zanussi8b372562009-04-28 03:04:59 -05001860 system->filter = kzalloc(sizeof(struct event_filter), GFP_KERNEL);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001861 if (!system->filter)
1862 goto out_free;
1863
1864 list_add(&system->list, &event_subsystems);
1865
1866 return system;
1867
1868 out_free:
Rasmus Villemoes79ac6ef2015-09-09 23:24:01 +02001869 kfree_const(system->name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001870 kfree(system);
1871 return NULL;
1872}
1873
1874static struct dentry *
1875event_subsystem_dir(struct trace_array *tr, const char *name,
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001876 struct trace_event_file *file, struct dentry *parent)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001877{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04001878 struct trace_subsystem_dir *dir;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001879 struct event_subsystem *system;
1880 struct dentry *entry;
1881
1882 /* First see if we did not already create this dir */
1883 list_for_each_entry(dir, &tr->systems, list) {
1884 system = dir->subsystem;
1885 if (strcmp(system->name, name) == 0) {
1886 dir->nr_events++;
1887 file->system = dir;
1888 return dir->entry;
1889 }
Tom Zanussi8b372562009-04-28 03:04:59 -05001890 }
1891
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001892 /* Now see if the system itself exists. */
1893 list_for_each_entry(system, &event_subsystems, list) {
1894 if (strcmp(system->name, name) == 0)
1895 break;
1896 }
1897 /* Reset system variable when not found */
1898 if (&system->list == &event_subsystems)
1899 system = NULL;
1900
1901 dir = kmalloc(sizeof(*dir), GFP_KERNEL);
1902 if (!dir)
1903 goto out_fail;
1904
1905 if (!system) {
1906 system = create_new_subsystem(name);
1907 if (!system)
1908 goto out_free;
1909 } else
1910 __get_system(system);
1911
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001912 dir->entry = tracefs_create_dir(name, parent);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001913 if (!dir->entry) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001914 pr_warn("Failed to create system directory %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001915 __put_system(system);
1916 goto out_free;
1917 }
1918
1919 dir->tr = tr;
1920 dir->ref_count = 1;
1921 dir->nr_events = 1;
1922 dir->subsystem = system;
1923 file->system = dir;
1924
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001925 entry = tracefs_create_file("filter", 0644, dir->entry, dir,
Tom Zanussie1112b42009-03-31 00:48:49 -05001926 &ftrace_subsystem_filter_fops);
Tom Zanussi8b372562009-04-28 03:04:59 -05001927 if (!entry) {
1928 kfree(system->filter);
1929 system->filter = NULL;
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001930 pr_warn("Could not create tracefs '%s/filter' entry\n", name);
Tom Zanussi8b372562009-04-28 03:04:59 -05001931 }
Tom Zanussie1112b42009-03-31 00:48:49 -05001932
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001933 trace_create_file("enable", 0644, dir->entry, dir,
Frederic Weisbeckerf3f3f002009-09-24 15:27:41 +02001934 &ftrace_system_enable_fops);
Steven Rostedt8ae79a12009-05-06 22:52:15 -04001935
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001936 list_add(&dir->list, &tr->systems);
1937
1938 return dir->entry;
1939
1940 out_free:
1941 kfree(dir);
1942 out_fail:
1943 /* Only print this message if failed on memory allocation */
1944 if (!dir || !system)
Fabian Frederick3448bac2014-06-07 13:43:08 +02001945 pr_warn("No memory to create event subsystem %s\n", name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001946 return NULL;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001947}
1948
Steven Rostedt1473e442009-02-24 14:15:08 -05001949static int
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04001950event_create_dir(struct dentry *parent, struct trace_event_file *file)
Steven Rostedt1473e442009-02-24 14:15:08 -05001951{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04001952 struct trace_event_call *call = file->event_call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001953 struct trace_array *tr = file->tr;
Steven Rostedt2e33af02010-04-22 10:35:55 -04001954 struct list_head *head;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001955 struct dentry *d_events;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04001956 const char *name;
Steven Rostedtfd994982009-02-28 02:41:25 -05001957 int ret;
Steven Rostedt1473e442009-02-24 14:15:08 -05001958
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001959 /*
1960 * If the trace point header did not define TRACE_SYSTEM
1961 * then the system would be called "TRACE_SYSTEM".
1962 */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001963 if (strcmp(call->class->system, TRACE_SYSTEM) != 0) {
1964 d_events = event_subsystem_dir(tr, call->class->system, file, parent);
1965 if (!d_events)
1966 return -ENOMEM;
1967 } else
1968 d_events = parent;
Steven Rostedt6ecc2d12009-02-27 21:33:02 -05001969
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04001970 name = trace_event_name(call);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001971 file->dir = tracefs_create_dir(name, d_events);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001972 if (!file->dir) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001973 pr_warn("Could not create tracefs '%s' directory\n", name);
Steven Rostedt1473e442009-02-24 14:15:08 -05001974 return -1;
1975 }
1976
Steven Rostedt9b637762012-05-10 15:55:43 -04001977 if (call->class->reg && !(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE))
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001978 trace_create_file("enable", 0644, file->dir, file,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001979 &ftrace_enable_fops);
Steven Rostedt1473e442009-02-24 14:15:08 -05001980
Steven Rostedt22392912010-04-21 12:27:06 -04001981#ifdef CONFIG_PERF_EVENTS
Steven Rostedta1d0ce82010-06-08 11:22:06 -04001982 if (call->event.type && call->class->reg)
Oleg Nesterov1a111262013-07-26 19:25:32 +02001983 trace_create_file("id", 0444, file->dir,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02001984 (void *)(long)call->event.type,
1985 &ftrace_event_id_fops);
Steven Rostedt22392912010-04-21 12:27:06 -04001986#endif
Peter Zijlstra23725ae2009-03-19 20:26:13 +01001987
Li Zefanc9d932c2010-05-24 16:24:28 +08001988 /*
1989 * Other events may have the same class. Only update
1990 * the fields if they are not already defined.
1991 */
1992 head = trace_get_fields(call);
1993 if (list_empty(head)) {
1994 ret = call->class->define_fields(call);
1995 if (ret < 0) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02001996 pr_warn("Could not initialize trace point events/%s\n",
1997 name);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04001998 return -1;
Tom Zanussicf027f62009-03-22 03:30:39 -05001999 }
2000 }
2001
Chunyu Hu854145e2016-05-03 19:34:34 +08002002 /*
2003 * Only event directories that can be enabled should have
Steven Rostedt (VMware)5d948c82018-05-09 09:48:41 -04002004 * triggers or filters.
Chunyu Hu854145e2016-05-03 19:34:34 +08002005 */
Steven Rostedt (VMware)5d948c82018-05-09 09:48:41 -04002006 if (!(call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)) {
2007 trace_create_file("filter", 0644, file->dir, file,
2008 &ftrace_event_filter_fops);
2009
Chunyu Hu854145e2016-05-03 19:34:34 +08002010 trace_create_file("trigger", 0644, file->dir, file,
2011 &event_trigger_fops);
Steven Rostedt (VMware)5d948c82018-05-09 09:48:41 -04002012 }
Tom Zanussi85f2b082013-10-24 08:59:24 -05002013
Tom Zanussi7ef224d2016-03-03 12:54:42 -06002014#ifdef CONFIG_HIST_TRIGGERS
2015 trace_create_file("hist", 0444, file->dir, file,
2016 &event_hist_fops);
2017#endif
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002018 trace_create_file("format", 0444, file->dir, call,
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002019 &ftrace_event_format_fops);
Steven Rostedtfd994982009-02-28 02:41:25 -05002020
Steven Rostedt1473e442009-02-24 14:15:08 -05002021 return 0;
2022}
2023
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002024static void remove_event_from_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002025{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002026 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002027 struct trace_array *tr;
2028
2029 do_for_each_event_file_safe(tr, file) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002030 if (file->event_call != call)
2031 continue;
2032
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002033 remove_event_file_dir(file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002034 /*
2035 * The do_for_each_event_file_safe() is
2036 * a double loop. After finding the call for this
2037 * trace_array, we use break to jump to the next
2038 * trace_array.
2039 */
2040 break;
2041 } while_for_each_event_file();
2042}
2043
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002044static void event_remove(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03002045{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002046 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002047 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002048
2049 do_for_each_event_file(tr, file) {
2050 if (file->event_call != call)
2051 continue;
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04002052
2053 if (file->flags & EVENT_FILE_FL_WAS_ENABLED)
2054 tr->clear_trace = true;
2055
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002056 ftrace_event_enable_disable(file, 0);
2057 /*
2058 * The do_for_each_event_file() is
2059 * a double loop. After finding the call for this
2060 * trace_array, we use break to jump to the next
2061 * trace_array.
2062 */
2063 break;
2064 } while_for_each_event_file();
2065
Ezequiel Garcia87819152012-09-12 11:47:57 -03002066 if (call->event.funcs)
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04002067 __unregister_trace_event(&call->event);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002068 remove_event_from_tracers(call);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002069 list_del(&call->list);
2070}
2071
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002072static int event_init(struct trace_event_call *call)
Ezequiel Garcia87819152012-09-12 11:47:57 -03002073{
2074 int ret = 0;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002075 const char *name;
Ezequiel Garcia87819152012-09-12 11:47:57 -03002076
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002077 name = trace_event_name(call);
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002078 if (WARN_ON(!name))
Ezequiel Garcia87819152012-09-12 11:47:57 -03002079 return -EINVAL;
2080
2081 if (call->class->raw_init) {
2082 ret = call->class->raw_init(call);
2083 if (ret < 0 && ret != -ENOSYS)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002084 pr_warn("Could not initialize trace events/%s\n", name);
Ezequiel Garcia87819152012-09-12 11:47:57 -03002085 }
2086
2087 return ret;
2088}
2089
Li Zefan67ead0a2010-05-24 16:25:13 +08002090static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002091__register_event(struct trace_event_call *call, struct module *mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002092{
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002093 int ret;
Steven Rostedt6d723732009-04-10 14:53:50 -04002094
Ezequiel Garcia87819152012-09-12 11:47:57 -03002095 ret = event_init(call);
2096 if (ret < 0)
2097 return ret;
Steven Rostedt701970b2009-04-24 23:11:22 -04002098
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002099 list_add(&call->list, &ftrace_events);
Li Zefan67ead0a2010-05-24 16:25:13 +08002100 call->mod = mod;
Masami Hiramatsu88f70d72009-09-25 11:20:54 -07002101
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002102 return 0;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002103}
2104
Jeremy Linton67ec0d82017-05-31 16:56:49 -05002105static char *eval_replace(char *ptr, struct trace_eval_map *map, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002106{
2107 int rlen;
2108 int elen;
2109
Jeremy Linton67ec0d82017-05-31 16:56:49 -05002110 /* Find the length of the eval value as a string */
Jeremy Linton00f4b652017-05-31 16:56:43 -05002111 elen = snprintf(ptr, 0, "%ld", map->eval_value);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002112 /* Make sure there's enough room to replace the string with the value */
2113 if (len < elen)
2114 return NULL;
2115
Jeremy Linton00f4b652017-05-31 16:56:43 -05002116 snprintf(ptr, elen + 1, "%ld", map->eval_value);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002117
2118 /* Get the rest of the string of ptr */
2119 rlen = strlen(ptr + len);
2120 memmove(ptr + elen, ptr + len, rlen);
2121 /* Make sure we end the new string */
2122 ptr[elen + rlen] = 0;
2123
2124 return ptr + elen;
2125}
2126
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002127static void update_event_printk(struct trace_event_call *call,
Jeremy Linton00f4b652017-05-31 16:56:43 -05002128 struct trace_eval_map *map)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002129{
2130 char *ptr;
2131 int quote = 0;
Jeremy Linton00f4b652017-05-31 16:56:43 -05002132 int len = strlen(map->eval_string);
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002133
2134 for (ptr = call->print_fmt; *ptr; ptr++) {
2135 if (*ptr == '\\') {
2136 ptr++;
2137 /* paranoid */
2138 if (!*ptr)
2139 break;
2140 continue;
2141 }
2142 if (*ptr == '"') {
2143 quote ^= 1;
2144 continue;
2145 }
2146 if (quote)
2147 continue;
2148 if (isdigit(*ptr)) {
2149 /* skip numbers */
2150 do {
2151 ptr++;
2152 /* Check for alpha chars like ULL */
2153 } while (isalnum(*ptr));
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002154 if (!*ptr)
2155 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002156 /*
2157 * A number must have some kind of delimiter after
2158 * it, and we can ignore that too.
2159 */
2160 continue;
2161 }
2162 if (isalpha(*ptr) || *ptr == '_') {
Jeremy Linton00f4b652017-05-31 16:56:43 -05002163 if (strncmp(map->eval_string, ptr, len) == 0 &&
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002164 !isalnum(ptr[len]) && ptr[len] != '_') {
Jeremy Linton67ec0d82017-05-31 16:56:49 -05002165 ptr = eval_replace(ptr, map, len);
2166 /* enum/sizeof string smaller than value */
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002167 if (WARN_ON_ONCE(!ptr))
2168 return;
2169 /*
Jeremy Linton67ec0d82017-05-31 16:56:49 -05002170 * No need to decrement here, as eval_replace()
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002171 * returns the pointer to the character passed
Jeremy Linton67ec0d82017-05-31 16:56:49 -05002172 * the eval, and two evals can not be placed
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002173 * back to back without something in between.
2174 * We can skip that something in between.
2175 */
2176 continue;
2177 }
2178 skip_more:
2179 do {
2180 ptr++;
2181 } while (isalnum(*ptr) || *ptr == '_');
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002182 if (!*ptr)
2183 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002184 /*
2185 * If what comes after this variable is a '.' or
2186 * '->' then we can continue to ignore that string.
2187 */
2188 if (*ptr == '.' || (ptr[0] == '-' && ptr[1] == '>')) {
2189 ptr += *ptr == '.' ? 1 : 2;
Steven Rostedt (Red Hat)31938992015-04-17 10:27:57 -04002190 if (!*ptr)
2191 break;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002192 goto skip_more;
2193 }
2194 /*
2195 * Once again, we can skip the delimiter that came
2196 * after the string.
2197 */
2198 continue;
2199 }
2200 }
2201}
2202
Jeremy Lintonf57a4142017-05-31 16:56:48 -05002203void trace_event_eval_update(struct trace_eval_map **map, int len)
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002204{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002205 struct trace_event_call *call, *p;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002206 const char *last_system = NULL;
Steven Rostedt (VMware)1ebe1ea2018-01-18 15:53:10 -05002207 bool first = false;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002208 int last_i;
2209 int i;
2210
2211 down_write(&trace_event_sem);
2212 list_for_each_entry_safe(call, p, &ftrace_events, list) {
2213 /* events are usually grouped together with systems */
2214 if (!last_system || call->class->system != last_system) {
Steven Rostedt (VMware)1ebe1ea2018-01-18 15:53:10 -05002215 first = true;
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002216 last_i = 0;
2217 last_system = call->class->system;
2218 }
2219
Steven Rostedt (VMware)1ebe1ea2018-01-18 15:53:10 -05002220 /*
2221 * Since calls are grouped by systems, the likelyhood that the
2222 * next call in the iteration belongs to the same system as the
2223 * previous call is high. As an optimization, we skip seaching
2224 * for a map[] that matches the call's system if the last call
2225 * was from the same system. That's what last_i is for. If the
2226 * call has the same system as the previous call, then last_i
2227 * will be the index of the first map[] that has a matching
2228 * system.
2229 */
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002230 for (i = last_i; i < len; i++) {
2231 if (call->class->system == map[i]->system) {
2232 /* Save the first system if need be */
Steven Rostedt (VMware)1ebe1ea2018-01-18 15:53:10 -05002233 if (first) {
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002234 last_i = i;
Steven Rostedt (VMware)1ebe1ea2018-01-18 15:53:10 -05002235 first = false;
2236 }
Steven Rostedt (Red Hat)0c564a52015-03-24 17:58:09 -04002237 update_event_printk(call, map[i]);
2238 }
2239 }
2240 }
2241 up_write(&trace_event_sem);
2242}
2243
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002244static struct trace_event_file *
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002245trace_create_new_event(struct trace_event_call *call,
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002246 struct trace_array *tr)
2247{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002248 struct trace_event_file *file;
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002249
2250 file = kmem_cache_alloc(file_cachep, GFP_TRACE);
2251 if (!file)
2252 return NULL;
2253
2254 file->event_call = call;
2255 file->tr = tr;
2256 atomic_set(&file->sm_ref, 0);
Tom Zanussi85f2b082013-10-24 08:59:24 -05002257 atomic_set(&file->tm_ref, 0);
2258 INIT_LIST_HEAD(&file->triggers);
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002259 list_add(&file->list, &tr->events);
2260
2261 return file;
2262}
2263
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002264/* Add an event to a trace directory */
2265static int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002266__trace_add_new_event(struct trace_event_call *call, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002267{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002268 struct trace_event_file *file;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002269
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002270 file = trace_create_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002271 if (!file)
2272 return -ENOMEM;
2273
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002274 return event_create_dir(tr->event_dir, file);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002275}
2276
Steven Rostedt77248222013-02-27 16:28:06 -05002277/*
2278 * Just create a decriptor for early init. A descriptor is required
2279 * for enabling events at boot. We want to enable events before
2280 * the filesystem is initialized.
2281 */
2282static __init int
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002283__trace_early_add_new_event(struct trace_event_call *call,
Steven Rostedt77248222013-02-27 16:28:06 -05002284 struct trace_array *tr)
2285{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002286 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002287
Steven Rostedt (Red Hat)da511bf2013-05-09 15:00:07 -04002288 file = trace_create_new_event(call, tr);
Steven Rostedt77248222013-02-27 16:28:06 -05002289 if (!file)
2290 return -ENOMEM;
2291
Steven Rostedt77248222013-02-27 16:28:06 -05002292 return 0;
2293}
2294
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002295struct ftrace_module_file_ops;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002296static void __add_event_to_tracers(struct trace_event_call *call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002297
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05002298/* Add an additional event_call dynamically */
2299int trace_add_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002300{
2301 int ret;
Masami Hiramatsufc800a12018-11-05 18:00:43 +09002302 lockdep_assert_held(&event_mutex);
2303
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002304 mutex_lock(&trace_types_lock);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002305
2306 ret = __register_event(call, NULL);
2307 if (ret >= 0)
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002308 __add_event_to_tracers(call);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002309
Alexander Z Lama8227412013-07-01 19:37:54 -07002310 mutex_unlock(&trace_types_lock);
Masami Hiramatsufc800a12018-11-05 18:00:43 +09002311 return ret;
2312}
2313
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002314/*
Alexander Z Lama8227412013-07-01 19:37:54 -07002315 * Must be called under locking of trace_types_lock, event_mutex and
2316 * trace_event_sem.
Masami Hiramatsu4fead8e2009-09-14 16:49:12 -04002317 */
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002318static void __trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002319{
Ezequiel Garcia87819152012-09-12 11:47:57 -03002320 event_remove(call);
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002321 trace_destroy_fields(call);
Oleg Nesterov57375742014-07-15 20:48:16 +02002322 free_event_filter(call->filter);
2323 call->filter = NULL;
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002324}
2325
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002326static int probe_remove_event_call(struct trace_event_call *call)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002327{
Oleg Nesterov2816c552013-07-29 19:50:33 +02002328 struct trace_array *tr;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002329 struct trace_event_file *file;
Oleg Nesterov2816c552013-07-29 19:50:33 +02002330
2331#ifdef CONFIG_PERF_EVENTS
2332 if (call->perf_refcount)
2333 return -EBUSY;
2334#endif
2335 do_for_each_event_file(tr, file) {
2336 if (file->event_call != call)
2337 continue;
2338 /*
2339 * We can't rely on ftrace_event_enable_disable(enable => 0)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002340 * we are going to do, EVENT_FILE_FL_SOFT_MODE can suppress
Oleg Nesterov2816c552013-07-29 19:50:33 +02002341 * TRACE_REG_UNREGISTER.
2342 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002343 if (file->flags & EVENT_FILE_FL_ENABLED)
Oleg Nesterov2816c552013-07-29 19:50:33 +02002344 return -EBUSY;
Steven Rostedt (Red Hat)2ba64032013-07-31 13:16:22 -04002345 /*
2346 * The do_for_each_event_file_safe() is
2347 * a double loop. After finding the call for this
2348 * trace_array, we use break to jump to the next
2349 * trace_array.
2350 */
Oleg Nesterov2816c552013-07-29 19:50:33 +02002351 break;
2352 } while_for_each_event_file();
2353
2354 __trace_remove_event_call(call);
2355
2356 return 0;
2357}
2358
Steven Rostedt (VMware)7e1413e2018-12-04 13:35:45 -05002359/* Remove an event_call */
2360int trace_remove_event_call(struct trace_event_call *call)
Masami Hiramatsufc800a12018-11-05 18:00:43 +09002361{
2362 int ret;
2363
2364 lockdep_assert_held(&event_mutex);
2365
2366 mutex_lock(&trace_types_lock);
2367 down_write(&trace_event_sem);
2368 ret = probe_remove_event_call(call);
2369 up_write(&trace_event_sem);
2370 mutex_unlock(&trace_types_lock);
2371
2372 return ret;
2373}
2374
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002375#define for_each_event(event, start, end) \
2376 for (event = start; \
2377 (unsigned long)event < (unsigned long)end; \
2378 event++)
2379
2380#ifdef CONFIG_MODULES
2381
Steven Rostedt6d723732009-04-10 14:53:50 -04002382static void trace_module_add_events(struct module *mod)
2383{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002384 struct trace_event_call **call, **start, **end;
Steven Rostedt6d723732009-04-10 14:53:50 -04002385
Steven Rostedt (Red Hat)45ab2812014-02-26 13:37:38 -05002386 if (!mod->num_trace_events)
2387 return;
2388
2389 /* Don't add infrastructure for mods without tracepoints */
2390 if (trace_module_has_bad_taint(mod)) {
2391 pr_err("%s: module has bad taint, not creating trace events\n",
2392 mod->name);
2393 return;
2394 }
2395
Steven Rostedt6d723732009-04-10 14:53:50 -04002396 start = mod->trace_events;
2397 end = mod->trace_events + mod->num_trace_events;
2398
Steven Rostedt6d723732009-04-10 14:53:50 -04002399 for_each_event(call, start, end) {
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002400 __register_event(*call, mod);
Oleg Nesterov779c5e32013-07-31 19:31:32 +02002401 __add_event_to_tracers(*call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002402 }
2403}
2404
2405static void trace_module_remove_events(struct module *mod)
2406{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002407 struct trace_event_call *call, *p;
Steven Rostedt6d723732009-04-10 14:53:50 -04002408
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002409 down_write(&trace_event_sem);
Steven Rostedt6d723732009-04-10 14:53:50 -04002410 list_for_each_entry_safe(call, p, &ftrace_events, list) {
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04002411 if (call->mod == mod)
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -04002412 __trace_remove_event_call(call);
Steven Rostedt6d723732009-04-10 14:53:50 -04002413 }
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002414 up_write(&trace_event_sem);
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002415
2416 /*
2417 * It is safest to reset the ring buffer if the module being unloaded
Steven Rostedt (Red Hat)873c6422013-03-04 23:26:06 -05002418 * registered any events that were used. The only worry is if
2419 * a new module gets loaded, and takes on the same id as the events
2420 * of this module. When printing out the buffer, traced events left
2421 * over from this module may be passed to the new module events and
2422 * unexpected results may occur.
Steven Rostedt9456f0f2009-05-06 21:54:09 -04002423 */
Steven Rostedt (VMware)065e63f2017-08-31 17:03:47 -04002424 tracing_reset_all_online_cpus();
Steven Rostedt6d723732009-04-10 14:53:50 -04002425}
2426
Steven Rostedt61f919a2009-04-14 18:22:32 -04002427static int trace_module_notify(struct notifier_block *self,
2428 unsigned long val, void *data)
Steven Rostedt6d723732009-04-10 14:53:50 -04002429{
2430 struct module *mod = data;
2431
2432 mutex_lock(&event_mutex);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002433 mutex_lock(&trace_types_lock);
Steven Rostedt6d723732009-04-10 14:53:50 -04002434 switch (val) {
2435 case MODULE_STATE_COMING:
2436 trace_module_add_events(mod);
2437 break;
2438 case MODULE_STATE_GOING:
2439 trace_module_remove_events(mod);
2440 break;
2441 }
Alexander Z Lama8227412013-07-01 19:37:54 -07002442 mutex_unlock(&trace_types_lock);
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002443 mutex_unlock(&event_mutex);
Steven Rostedt6d723732009-04-10 14:53:50 -04002444
2445 return 0;
2446}
Steven Rostedt (Red Hat)315326c2013-03-02 17:37:14 -05002447
Oleg Nesterov836d4812013-07-31 19:31:37 +02002448static struct notifier_block trace_module_nb = {
2449 .notifier_call = trace_module_notify,
Steven Rostedt (Red Hat)3673b8e2015-03-25 15:44:21 -04002450 .priority = 1, /* higher than trace.c module notify */
Oleg Nesterov836d4812013-07-31 19:31:37 +02002451};
Steven Rostedt61f919a2009-04-14 18:22:32 -04002452#endif /* CONFIG_MODULES */
Steven Rostedt6d723732009-04-10 14:53:50 -04002453
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002454/* Create a new event directory structure for a trace directory. */
2455static void
2456__trace_add_event_dirs(struct trace_array *tr)
2457{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002458 struct trace_event_call *call;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002459 int ret;
2460
2461 list_for_each_entry(call, &ftrace_events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002462 ret = __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002463 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002464 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002465 trace_event_name(call));
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002466 }
2467}
2468
Steven Rostedt (VMware)3c965292018-05-08 15:06:38 -04002469/* Returns any file that matches the system and event */
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002470struct trace_event_file *
Steven Rostedt (VMware)3c965292018-05-08 15:06:38 -04002471__find_event_file(struct trace_array *tr, const char *system, const char *event)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002472{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002473 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002474 struct trace_event_call *call;
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002475 const char *name;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002476
2477 list_for_each_entry(file, &tr->events, list) {
2478
2479 call = file->event_call;
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002480 name = trace_event_name(call);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002481
Steven Rostedt (VMware)3c965292018-05-08 15:06:38 -04002482 if (!name || !call->class)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002483 continue;
2484
Mathieu Desnoyersde7b2972014-04-08 17:26:21 -04002485 if (strcmp(event, name) == 0 &&
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002486 strcmp(system, call->class->system) == 0)
2487 return file;
2488 }
2489 return NULL;
2490}
2491
Steven Rostedt (VMware)3c965292018-05-08 15:06:38 -04002492/* Returns valid trace event files that match system and event */
2493struct trace_event_file *
2494find_event_file(struct trace_array *tr, const char *system, const char *event)
2495{
2496 struct trace_event_file *file;
2497
2498 file = __find_event_file(tr, system, event);
2499 if (!file || !file->event_call->class->reg ||
2500 file->event_call->flags & TRACE_EVENT_FL_IGNORE_ENABLE)
2501 return NULL;
2502
2503 return file;
2504}
2505
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002506#ifdef CONFIG_DYNAMIC_FTRACE
2507
2508/* Avoid typos */
2509#define ENABLE_EVENT_STR "enable_event"
2510#define DISABLE_EVENT_STR "disable_event"
2511
2512struct event_probe_data {
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002513 struct trace_event_file *file;
Steven Rostedt (Red Hat)2875a082013-12-20 23:23:05 -05002514 unsigned long count;
2515 int ref;
2516 bool enable;
2517};
2518
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002519static void update_event_probe(struct event_probe_data *data)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002520{
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002521 if (data->enable)
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002522 clear_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002523 else
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04002524 set_bit(EVENT_FILE_FL_SOFT_DISABLED_BIT, &data->file->flags);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002525}
2526
2527static void
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002528event_enable_probe(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04002529 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002530 void *data)
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002531{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002532 struct ftrace_func_mapper *mapper = data;
2533 struct event_probe_data *edata;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002534 void **pdata;
2535
2536 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2537 if (!pdata || !*pdata)
2538 return;
2539
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002540 edata = *pdata;
2541 update_event_probe(edata);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002542}
2543
2544static void
Steven Rostedt (VMware)bca6c8d2017-04-03 18:18:47 -04002545event_enable_count_probe(unsigned long ip, unsigned long parent_ip,
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04002546 struct trace_array *tr, struct ftrace_probe_ops *ops,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002547 void *data)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002548{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002549 struct ftrace_func_mapper *mapper = data;
2550 struct event_probe_data *edata;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002551 void **pdata;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002552
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002553 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2554 if (!pdata || !*pdata)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002555 return;
2556
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002557 edata = *pdata;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002558
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002559 if (!edata->count)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002560 return;
2561
2562 /* Skip if the event is in a state we want to switch to */
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002563 if (edata->enable == !(edata->file->flags & EVENT_FILE_FL_SOFT_DISABLED))
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002564 return;
2565
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002566 if (edata->count != -1)
2567 (edata->count)--;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002568
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002569 update_event_probe(edata);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002570}
2571
2572static int
2573event_enable_print(struct seq_file *m, unsigned long ip,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002574 struct ftrace_probe_ops *ops, void *data)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002575{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002576 struct ftrace_func_mapper *mapper = data;
2577 struct event_probe_data *edata;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002578 void **pdata;
2579
2580 pdata = ftrace_func_mapper_find_ip(mapper, ip);
2581
2582 if (WARN_ON_ONCE(!pdata || !*pdata))
2583 return 0;
2584
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002585 edata = *pdata;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002586
2587 seq_printf(m, "%ps:", (void *)ip);
2588
2589 seq_printf(m, "%s:%s:%s",
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002590 edata->enable ? ENABLE_EVENT_STR : DISABLE_EVENT_STR,
2591 edata->file->event_call->class->system,
2592 trace_event_name(edata->file->event_call));
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002593
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002594 if (edata->count == -1)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01002595 seq_puts(m, ":unlimited\n");
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002596 else
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002597 seq_printf(m, ":count=%ld\n", edata->count);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002598
2599 return 0;
2600}
2601
2602static int
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04002603event_enable_init(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002604 unsigned long ip, void *init_data, void **data)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002605{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002606 struct ftrace_func_mapper *mapper = *data;
2607 struct event_probe_data *edata = init_data;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002608 int ret;
2609
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002610 if (!mapper) {
2611 mapper = allocate_ftrace_func_mapper();
2612 if (!mapper)
2613 return -ENODEV;
2614 *data = mapper;
2615 }
2616
2617 ret = ftrace_func_mapper_add_ip(mapper, ip, edata);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002618 if (ret < 0)
2619 return ret;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002620
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002621 edata->ref++;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002622
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002623 return 0;
2624}
2625
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002626static int free_probe_data(void *data)
2627{
2628 struct event_probe_data *edata = data;
2629
2630 edata->ref--;
2631 if (!edata->ref) {
2632 /* Remove the SOFT_MODE flag */
2633 __ftrace_event_enable_disable(edata->file, 0, 1);
2634 module_put(edata->file->event_call->mod);
2635 kfree(edata);
2636 }
2637 return 0;
2638}
2639
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002640static void
Steven Rostedt (VMware)b5f081b2017-04-10 22:30:05 -04002641event_enable_free(struct ftrace_probe_ops *ops, struct trace_array *tr,
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002642 unsigned long ip, void *data)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002643{
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002644 struct ftrace_func_mapper *mapper = data;
2645 struct event_probe_data *edata;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002646
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002647 if (!ip) {
2648 if (!mapper)
2649 return;
2650 free_ftrace_func_mapper(mapper, free_probe_data);
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002651 return;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002652 }
Steven Rostedt (VMware)6e444312017-04-19 22:39:44 -04002653
2654 edata = ftrace_func_mapper_remove_ip(mapper, ip);
2655
2656 if (WARN_ON_ONCE(!edata))
2657 return;
2658
2659 if (WARN_ON_ONCE(edata->ref <= 0))
2660 return;
2661
2662 free_probe_data(edata);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002663}
2664
2665static struct ftrace_probe_ops event_enable_probe_ops = {
2666 .func = event_enable_probe,
2667 .print = event_enable_print,
2668 .init = event_enable_init,
2669 .free = event_enable_free,
2670};
2671
2672static struct ftrace_probe_ops event_enable_count_probe_ops = {
2673 .func = event_enable_count_probe,
2674 .print = event_enable_print,
2675 .init = event_enable_init,
2676 .free = event_enable_free,
2677};
2678
2679static struct ftrace_probe_ops event_disable_probe_ops = {
2680 .func = event_enable_probe,
2681 .print = event_enable_print,
2682 .init = event_enable_init,
2683 .free = event_enable_free,
2684};
2685
2686static struct ftrace_probe_ops event_disable_count_probe_ops = {
2687 .func = event_enable_count_probe,
2688 .print = event_enable_print,
2689 .init = event_enable_init,
2690 .free = event_enable_free,
2691};
2692
2693static int
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04002694event_enable_func(struct trace_array *tr, struct ftrace_hash *hash,
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002695 char *glob, char *cmd, char *param, int enabled)
2696{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002697 struct trace_event_file *file;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002698 struct ftrace_probe_ops *ops;
2699 struct event_probe_data *data;
2700 const char *system;
2701 const char *event;
2702 char *number;
2703 bool enable;
2704 int ret;
2705
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09002706 if (!tr)
2707 return -ENODEV;
2708
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002709 /* hash funcs only work with set_ftrace_filter */
Harsh Prateek Bora8092e802013-05-24 12:52:17 +05302710 if (!enabled || !param)
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002711 return -EINVAL;
2712
2713 system = strsep(&param, ":");
2714 if (!param)
2715 return -EINVAL;
2716
2717 event = strsep(&param, ":");
2718
2719 mutex_lock(&event_mutex);
2720
2721 ret = -EINVAL;
2722 file = find_event_file(tr, system, event);
2723 if (!file)
2724 goto out;
2725
2726 enable = strcmp(cmd, ENABLE_EVENT_STR) == 0;
2727
2728 if (enable)
2729 ops = param ? &event_enable_count_probe_ops : &event_enable_probe_ops;
2730 else
2731 ops = param ? &event_disable_count_probe_ops : &event_disable_probe_ops;
2732
2733 if (glob[0] == '!') {
Steven Rostedt (VMware)7b60f3d2017-04-18 14:50:39 -04002734 ret = unregister_ftrace_function_probe_func(glob+1, tr, ops);
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002735 goto out;
2736 }
2737
2738 ret = -ENOMEM;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002739
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002740 data = kzalloc(sizeof(*data), GFP_KERNEL);
2741 if (!data)
2742 goto out;
2743
2744 data->enable = enable;
2745 data->count = -1;
2746 data->file = file;
2747
2748 if (!param)
2749 goto out_reg;
2750
2751 number = strsep(&param, ":");
2752
2753 ret = -EINVAL;
2754 if (!strlen(number))
2755 goto out_free;
2756
2757 /*
2758 * We use the callback data field (which is a pointer)
2759 * as our counter.
2760 */
2761 ret = kstrtoul(number, 0, &data->count);
2762 if (ret)
2763 goto out_free;
2764
2765 out_reg:
2766 /* Don't let event modules unload while probe registered */
2767 ret = try_module_get(file->event_call->mod);
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002768 if (!ret) {
2769 ret = -EBUSY;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002770 goto out_free;
Masami Hiramatsu6ed01062013-05-16 20:48:49 +09002771 }
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002772
2773 ret = __ftrace_event_enable_disable(file, 1, 1);
2774 if (ret < 0)
2775 goto out_put;
Steven Rostedt (VMware)41794f12017-04-03 20:58:35 -04002776
Steven Rostedt (VMware)04ec7bb2017-04-05 13:12:55 -04002777 ret = register_ftrace_function_probe(glob, tr, ops, data);
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002778 /*
2779 * The above returns on success the # of functions enabled,
2780 * but if it didn't find any functions it returns zero.
2781 * Consider no functions a failure too.
2782 */
Masami Hiramatsua5b85bd2013-05-09 14:44:14 +09002783 if (!ret) {
2784 ret = -ENOENT;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002785 goto out_disable;
Steven Rostedt (Red Hat)ff305de2013-05-09 11:30:26 -04002786 } else if (ret < 0)
2787 goto out_disable;
2788 /* Just return zero, not the number of enabled functions */
2789 ret = 0;
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04002790 out:
2791 mutex_unlock(&event_mutex);
2792 return ret;
2793
2794 out_disable:
2795 __ftrace_event_enable_disable(file, 0, 1);
2796 out_put:
2797 module_put(file->event_call->mod);
2798 out_free:
2799 kfree(data);
2800 goto out;
2801}
2802
2803static struct ftrace_func_command event_enable_cmd = {
2804 .name = ENABLE_EVENT_STR,
2805 .func = event_enable_func,
2806};
2807
2808static struct ftrace_func_command event_disable_cmd = {
2809 .name = DISABLE_EVENT_STR,
2810 .func = event_enable_func,
2811};
2812
2813static __init int register_event_cmds(void)
2814{
2815 int ret;
2816
2817 ret = register_ftrace_command(&event_enable_cmd);
2818 if (WARN_ON(ret < 0))
2819 return ret;
2820 ret = register_ftrace_command(&event_disable_cmd);
2821 if (WARN_ON(ret < 0))
2822 unregister_ftrace_command(&event_enable_cmd);
2823 return ret;
2824}
2825#else
2826static inline int register_event_cmds(void) { return 0; }
2827#endif /* CONFIG_DYNAMIC_FTRACE */
2828
Steven Rostedt77248222013-02-27 16:28:06 -05002829/*
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002830 * The top level array has already had its trace_event_file
Steven Rostedt77248222013-02-27 16:28:06 -05002831 * descriptors created in order to allow for early events to
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002832 * be recorded. This function is called after the tracefs has been
Steven Rostedt77248222013-02-27 16:28:06 -05002833 * initialized, and we now have to create the files associated
2834 * to the events.
2835 */
2836static __init void
2837__trace_early_add_event_dirs(struct trace_array *tr)
2838{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002839 struct trace_event_file *file;
Steven Rostedt77248222013-02-27 16:28:06 -05002840 int ret;
2841
2842
2843 list_for_each_entry(file, &tr->events, list) {
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002844 ret = event_create_dir(tr->event_dir, file);
Steven Rostedt77248222013-02-27 16:28:06 -05002845 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002846 pr_warn("Could not create directory for event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002847 trace_event_name(file->event_call));
Steven Rostedt77248222013-02-27 16:28:06 -05002848 }
2849}
2850
2851/*
2852 * For early boot up, the top trace array requires to have
2853 * a list of events that can be enabled. This must be done before
2854 * the filesystem is set up in order to allow events to be traced
2855 * early.
2856 */
2857static __init void
2858__trace_early_add_events(struct trace_array *tr)
2859{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002860 struct trace_event_call *call;
Steven Rostedt77248222013-02-27 16:28:06 -05002861 int ret;
2862
2863 list_for_each_entry(call, &ftrace_events, list) {
2864 /* Early boot up should not have any modules loaded */
2865 if (WARN_ON_ONCE(call->mod))
2866 continue;
2867
2868 ret = __trace_early_add_new_event(call, tr);
2869 if (ret < 0)
Fabian Frederick3448bac2014-06-07 13:43:08 +02002870 pr_warn("Could not create early event %s\n",
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04002871 trace_event_name(call));
Steven Rostedt77248222013-02-27 16:28:06 -05002872 }
2873}
2874
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002875/* Remove the event directory structure for a trace directory. */
2876static void
2877__trace_remove_event_dirs(struct trace_array *tr)
2878{
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04002879 struct trace_event_file *file, *next;
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002880
Oleg Nesterovf6a84bd2013-07-26 19:25:47 +02002881 list_for_each_entry_safe(file, next, &tr->events, list)
2882 remove_event_file_dir(file);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04002883}
2884
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002885static void __add_event_to_tracers(struct trace_event_call *call)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002886{
2887 struct trace_array *tr;
2888
Oleg Nesterov620a30e2013-07-31 19:31:35 +02002889 list_for_each_entry(tr, &ftrace_trace_arrays, list)
2890 __trace_add_new_event(call, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002891}
2892
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04002893extern struct trace_event_call *__start_ftrace_events[];
2894extern struct trace_event_call *__stop_ftrace_events[];
Steven Rostedta59fd602009-04-10 13:52:20 -04002895
Li Zefan020e5f82009-07-01 10:47:05 +08002896static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
2897
2898static __init int setup_trace_event(char *str)
2899{
2900 strlcpy(bootup_event_buf, str, COMMAND_LINE_SIZE);
Steven Rostedt (Red Hat)55034cd2013-03-07 22:48:09 -05002901 ring_buffer_expanded = true;
2902 tracing_selftest_disabled = true;
Li Zefan020e5f82009-07-01 10:47:05 +08002903
2904 return 1;
2905}
2906__setup("trace_event=", setup_trace_event);
2907
Steven Rostedt77248222013-02-27 16:28:06 -05002908/* Expects to have event_mutex held when called */
2909static int
2910create_event_toplevel_files(struct dentry *parent, struct trace_array *tr)
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002911{
2912 struct dentry *d_events;
2913 struct dentry *entry;
2914
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002915 entry = tracefs_create_file("set_event", 0644, parent,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002916 tr, &ftrace_set_event_fops);
2917 if (!entry) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002918 pr_warn("Could not create tracefs 'set_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002919 return -ENOMEM;
2920 }
2921
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002922 d_events = tracefs_create_dir("events", parent);
Steven Rostedt277ba042012-08-03 16:10:49 -04002923 if (!d_events) {
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05002924 pr_warn("Could not create tracefs 'events' directory\n");
Steven Rostedt277ba042012-08-03 16:10:49 -04002925 return -ENOMEM;
2926 }
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002927
Steven Rostedt (Red Hat)7d436402016-11-22 18:32:03 -05002928 entry = trace_create_file("enable", 0644, d_events,
2929 tr, &ftrace_tr_enable_fops);
2930 if (!entry) {
2931 pr_warn("Could not create tracefs 'enable' entry\n");
2932 return -ENOMEM;
2933 }
2934
2935 /* There are not as crucial, just warn if they are not created */
2936
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04002937 entry = tracefs_create_file("set_event_pid", 0644, parent,
2938 tr, &ftrace_set_event_pid_fops);
Steven Rostedt (Red Hat)7d436402016-11-22 18:32:03 -05002939 if (!entry)
2940 pr_warn("Could not create tracefs 'set_event_pid' entry\n");
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04002941
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002942 /* ring buffer internal formats */
Steven Rostedt (Red Hat)7d436402016-11-22 18:32:03 -05002943 entry = trace_create_file("header_page", 0444, d_events,
2944 ring_buffer_print_page_header,
2945 &ftrace_show_header_fops);
2946 if (!entry)
2947 pr_warn("Could not create tracefs 'header_page' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002948
Steven Rostedt (Red Hat)7d436402016-11-22 18:32:03 -05002949 entry = trace_create_file("header_event", 0444, d_events,
2950 ring_buffer_print_entry_header,
2951 &ftrace_show_header_fops);
2952 if (!entry)
2953 pr_warn("Could not create tracefs 'header_event' entry\n");
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002954
2955 tr->event_dir = d_events;
Steven Rostedt77248222013-02-27 16:28:06 -05002956
2957 return 0;
2958}
2959
2960/**
2961 * event_trace_add_tracer - add a instance of a trace_array to events
2962 * @parent: The parent dentry to place the files/directories for events in
2963 * @tr: The trace array associated with these events
2964 *
2965 * When a new instance is created, it needs to set up its events
2966 * directory, as well as other files associated with events. It also
2967 * creates the event hierachry in the @parent/events directory.
2968 *
2969 * Returns 0 on success.
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002970 *
2971 * Must be called with event_mutex held.
Steven Rostedt77248222013-02-27 16:28:06 -05002972 */
2973int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr)
2974{
2975 int ret;
2976
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002977 lockdep_assert_held(&event_mutex);
Steven Rostedt77248222013-02-27 16:28:06 -05002978
2979 ret = create_event_toplevel_files(parent, tr);
2980 if (ret)
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002981 goto out;
Steven Rostedt77248222013-02-27 16:28:06 -05002982
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002983 down_write(&trace_event_sem);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04002984 __trace_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08002985 up_write(&trace_event_sem);
Steven Rostedt277ba042012-08-03 16:10:49 -04002986
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04002987 out:
Steven Rostedt77248222013-02-27 16:28:06 -05002988 return ret;
2989}
2990
2991/*
2992 * The top trace array already had its file descriptors created.
2993 * Now the files themselves need to be created.
2994 */
2995static __init int
2996early_event_add_tracer(struct dentry *parent, struct trace_array *tr)
2997{
2998 int ret;
2999
3000 mutex_lock(&event_mutex);
3001
3002 ret = create_event_toplevel_files(parent, tr);
3003 if (ret)
3004 goto out_unlock;
3005
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003006 down_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05003007 __trace_early_add_event_dirs(tr);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003008 up_write(&trace_event_sem);
Steven Rostedt77248222013-02-27 16:28:06 -05003009
3010 out_unlock:
3011 mutex_unlock(&event_mutex);
3012
3013 return ret;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003014}
3015
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04003016/* Must be called with event_mutex held */
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003017int event_trace_del_tracer(struct trace_array *tr)
3018{
Steven Rostedt (VMware)12ecef02017-09-21 16:22:49 -04003019 lockdep_assert_held(&event_mutex);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003020
Tom Zanussi85f2b082013-10-24 08:59:24 -05003021 /* Disable any event triggers and associated soft-disabled events */
3022 clear_event_triggers(tr);
3023
Steven Rostedt (Red Hat)49090102015-09-24 11:33:26 -04003024 /* Clear the pid list */
3025 __ftrace_clear_event_pids(tr);
3026
Steven Rostedt (Red Hat)2a6c24a2013-07-02 14:48:23 -04003027 /* Disable any running events */
3028 __ftrace_set_clr_event_nolock(tr, NULL, NULL, NULL, 0);
3029
Steven Rostedt (VMware)e0a568d2018-08-09 15:31:48 -04003030 /* Make sure no more events are being executed */
3031 tracepoint_synchronize_unregister();
Steven Rostedt3ccb0122013-12-03 12:41:20 -05003032
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003033 down_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003034 __trace_remove_event_dirs(tr);
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003035 tracefs_remove_recursive(tr->event_dir);
zhangwei(Jovi)52f6ad62013-03-11 15:14:03 +08003036 up_write(&trace_event_sem);
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003037
3038 tr->event_dir = NULL;
3039
Steven Rostedt0c8916c2012-08-07 16:14:16 -04003040 return 0;
3041}
3042
Steven Rostedtd1a29142013-02-27 20:23:57 -05003043static __init int event_trace_memsetup(void)
3044{
3045 field_cachep = KMEM_CACHE(ftrace_event_field, SLAB_PANIC);
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04003046 file_cachep = KMEM_CACHE(trace_event_file, SLAB_PANIC);
Steven Rostedtd1a29142013-02-27 20:23:57 -05003047 return 0;
3048}
3049
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003050static __init void
3051early_enable_events(struct trace_array *tr, bool disable_first)
3052{
3053 char *buf = bootup_event_buf;
3054 char *token;
3055 int ret;
3056
3057 while (true) {
3058 token = strsep(&buf, ",");
3059
3060 if (!token)
3061 break;
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003062
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05003063 if (*token) {
3064 /* Restarting syscalls requires that we stop them first */
3065 if (disable_first)
3066 ftrace_set_clr_event(tr, token, 0);
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003067
Steven Rostedt (Red Hat)43ed3842015-11-03 22:15:14 -05003068 ret = ftrace_set_clr_event(tr, token, 1);
3069 if (ret)
3070 pr_warn("Failed to enable trace event: %s\n", token);
3071 }
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003072
3073 /* Put back the comma to allow this to be called again */
3074 if (buf)
3075 *(buf - 1) = ',';
3076 }
3077}
3078
Ezequiel Garcia87819152012-09-12 11:47:57 -03003079static __init int event_trace_enable(void)
3080{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003081 struct trace_array *tr = top_trace_array();
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003082 struct trace_event_call **iter, *call;
Ezequiel Garcia87819152012-09-12 11:47:57 -03003083 int ret;
3084
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003085 if (!tr)
3086 return -ENODEV;
3087
Ezequiel Garcia87819152012-09-12 11:47:57 -03003088 for_each_event(iter, __start_ftrace_events, __stop_ftrace_events) {
3089
3090 call = *iter;
3091 ret = event_init(call);
3092 if (!ret)
3093 list_add(&call->list, &ftrace_events);
3094 }
3095
Steven Rostedt77248222013-02-27 16:28:06 -05003096 /*
3097 * We need the top trace array to have a working set of trace
3098 * points at early init, before the debug files and directories
3099 * are created. Create the file entries now, and attach them
3100 * to the actual file dentries later.
3101 */
3102 __trace_early_add_events(tr);
3103
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003104 early_enable_events(tr, false);
Steven Rostedt81698832012-10-11 10:15:05 -04003105
3106 trace_printk_start_comm();
3107
Steven Rostedt (Red Hat)3cd715d2013-03-12 19:35:13 -04003108 register_event_cmds();
3109
Tom Zanussi85f2b082013-10-24 08:59:24 -05003110 register_trigger_cmds();
3111
Ezequiel Garcia87819152012-09-12 11:47:57 -03003112 return 0;
3113}
3114
Steven Rostedt (Red Hat)ce1039b2015-01-14 12:53:45 -05003115/*
3116 * event_trace_enable() is called from trace_event_init() first to
3117 * initialize events and perhaps start any events that are on the
3118 * command line. Unfortunately, there are some events that will not
3119 * start this early, like the system call tracepoints that need
3120 * to set the TIF_SYSCALL_TRACEPOINT flag of pid 1. But event_trace_enable()
3121 * is called before pid 1 starts, and this flag is never set, making
3122 * the syscall tracepoint never get reached, but the event is enabled
3123 * regardless (and not doing anything).
3124 */
3125static __init int event_trace_enable_again(void)
3126{
3127 struct trace_array *tr;
3128
3129 tr = top_trace_array();
3130 if (!tr)
3131 return -ENODEV;
3132
3133 early_enable_events(tr, true);
3134
3135 return 0;
3136}
3137
3138early_initcall(event_trace_enable_again);
3139
Steven Rostedt (VMware)58b92542018-05-08 15:09:27 -04003140__init int event_trace_init(void)
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003141{
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003142 struct trace_array *tr;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003143 struct dentry *d_tracer;
3144 struct dentry *entry;
Steven Rostedt6d723732009-04-10 14:53:50 -04003145 int ret;
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003146
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003147 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003148 if (!tr)
3149 return -ENODEV;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003150
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003151 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05003152 if (IS_ERR(d_tracer))
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003153 return 0;
3154
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003155 entry = tracefs_create_file("available_events", 0444, d_tracer,
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003156 tr, &ftrace_avail_fops);
Steven Rostedt2314c4a2009-03-10 12:04:02 -04003157 if (!entry)
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05003158 pr_warn("Could not create tracefs 'available_events' entry\n");
Steven Rostedt2314c4a2009-03-10 12:04:02 -04003159
Daniel Wagner9f616682015-08-10 14:35:46 +02003160 if (trace_define_generic_fields())
3161 pr_warn("tracing: Failed to allocated generic fields");
3162
Li Zefan8728fe52010-05-24 16:22:49 +08003163 if (trace_define_common_fields())
Fabian Frederick3448bac2014-06-07 13:43:08 +02003164 pr_warn("tracing: Failed to allocate common fields");
Li Zefan8728fe52010-05-24 16:22:49 +08003165
Steven Rostedt77248222013-02-27 16:28:06 -05003166 ret = early_event_add_tracer(d_tracer, tr);
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003167 if (ret)
3168 return ret;
Li Zefan020e5f82009-07-01 10:47:05 +08003169
Oleg Nesterov836d4812013-07-31 19:31:37 +02003170#ifdef CONFIG_MODULES
Steven Rostedt6d723732009-04-10 14:53:50 -04003171 ret = register_module_notifier(&trace_module_nb);
Ming Lei55379372009-05-18 23:04:46 +08003172 if (ret)
Fabian Frederick3448bac2014-06-07 13:43:08 +02003173 pr_warn("Failed to register trace events module notifier\n");
Oleg Nesterov836d4812013-07-31 19:31:37 +02003174#endif
Steven Rostedtb77e38a2009-02-24 10:21:36 -05003175 return 0;
3176}
Steven Rostedt (Red Hat)5f893b22014-12-12 20:05:10 -05003177
3178void __init trace_event_init(void)
3179{
3180 event_trace_memsetup();
3181 init_ftrace_syscalls();
3182 event_trace_enable();
3183}
3184
Steven Rostedt (VMware)b3015fe2019-05-23 19:40:17 -04003185#ifdef CONFIG_EVENT_TRACE_STARTUP_TEST
Steven Rostedte6187002009-04-15 13:36:40 -04003186
3187static DEFINE_SPINLOCK(test_spinlock);
3188static DEFINE_SPINLOCK(test_spinlock_irq);
3189static DEFINE_MUTEX(test_mutex);
3190
3191static __init void test_work(struct work_struct *dummy)
3192{
3193 spin_lock(&test_spinlock);
3194 spin_lock_irq(&test_spinlock_irq);
3195 udelay(1);
3196 spin_unlock_irq(&test_spinlock_irq);
3197 spin_unlock(&test_spinlock);
3198
3199 mutex_lock(&test_mutex);
3200 msleep(1);
3201 mutex_unlock(&test_mutex);
3202}
3203
3204static __init int event_test_thread(void *unused)
3205{
3206 void *test_malloc;
3207
3208 test_malloc = kmalloc(1234, GFP_KERNEL);
3209 if (!test_malloc)
3210 pr_info("failed to kmalloc\n");
3211
3212 schedule_on_each_cpu(test_work);
3213
3214 kfree(test_malloc);
3215
3216 set_current_state(TASK_INTERRUPTIBLE);
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003217 while (!kthread_should_stop()) {
Steven Rostedte6187002009-04-15 13:36:40 -04003218 schedule();
Peter Zijlstrafe0e01c2014-10-08 18:51:10 +02003219 set_current_state(TASK_INTERRUPTIBLE);
3220 }
3221 __set_current_state(TASK_RUNNING);
Steven Rostedte6187002009-04-15 13:36:40 -04003222
3223 return 0;
3224}
3225
3226/*
3227 * Do various things that may trigger events.
3228 */
3229static __init void event_test_stuff(void)
3230{
3231 struct task_struct *test_thread;
3232
3233 test_thread = kthread_run(event_test_thread, NULL, "test-events");
3234 msleep(1);
3235 kthread_stop(test_thread);
3236}
3237
3238/*
3239 * For every trace event defined, we will test each trace point separately,
3240 * and then by groups, and finally all trace points.
3241 */
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003242static __init void event_trace_self_tests(void)
Steven Rostedte6187002009-04-15 13:36:40 -04003243{
Steven Rostedt (Red Hat)7967b3e2015-05-13 14:59:40 -04003244 struct trace_subsystem_dir *dir;
Steven Rostedt (Red Hat)7f1d2f82015-05-05 10:09:53 -04003245 struct trace_event_file *file;
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -04003246 struct trace_event_call *call;
Steven Rostedte6187002009-04-15 13:36:40 -04003247 struct event_subsystem *system;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003248 struct trace_array *tr;
Steven Rostedte6187002009-04-15 13:36:40 -04003249 int ret;
3250
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003251 tr = top_trace_array();
Yoshihiro YUNOMAEdc81e5e2014-06-06 07:35:17 +09003252 if (!tr)
3253 return;
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003254
Steven Rostedte6187002009-04-15 13:36:40 -04003255 pr_info("Running tests on trace events:\n");
3256
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003257 list_for_each_entry(file, &tr->events, list) {
3258
3259 call = file->event_call;
Steven Rostedte6187002009-04-15 13:36:40 -04003260
Steven Rostedt22392912010-04-21 12:27:06 -04003261 /* Only test those that have a probe */
3262 if (!call->class || !call->class->probe)
Steven Rostedte6187002009-04-15 13:36:40 -04003263 continue;
3264
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003265/*
3266 * Testing syscall events here is pretty useless, but
3267 * we still do it if configured. But this is time consuming.
3268 * What we really need is a user thread to perform the
3269 * syscalls as we test.
3270 */
3271#ifndef CONFIG_EVENT_TRACE_TEST_SYSCALLS
Steven Rostedt8f082012010-04-20 10:47:33 -04003272 if (call->class->system &&
3273 strcmp(call->class->system, "syscalls") == 0)
Steven Rostedt1f5a6b42009-09-14 11:58:24 -04003274 continue;
3275#endif
3276
Steven Rostedt (Red Hat)687fcc42015-05-13 14:20:14 -04003277 pr_info("Testing event %s: ", trace_event_name(call));
Steven Rostedte6187002009-04-15 13:36:40 -04003278
3279 /*
3280 * If an event is already enabled, someone is using
3281 * it and the self test should not be on.
3282 */
Steven Rostedt (Red Hat)5d6ad962015-05-13 15:12:33 -04003283 if (file->flags & EVENT_FILE_FL_ENABLED) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003284 pr_warn("Enabled event during self test!\n");
Steven Rostedte6187002009-04-15 13:36:40 -04003285 WARN_ON_ONCE(1);
3286 continue;
3287 }
3288
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003289 ftrace_event_enable_disable(file, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003290 event_test_stuff();
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003291 ftrace_event_enable_disable(file, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003292
3293 pr_cont("OK\n");
3294 }
3295
3296 /* Now test at the sub system level */
3297
3298 pr_info("Running tests on trace event systems:\n");
3299
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003300 list_for_each_entry(dir, &tr->systems, list) {
3301
3302 system = dir->subsystem;
Steven Rostedte6187002009-04-15 13:36:40 -04003303
3304 /* the ftrace system is special, skip it */
3305 if (strcmp(system->name, "ftrace") == 0)
3306 continue;
3307
3308 pr_info("Testing event system %s: ", system->name);
3309
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003310 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003311 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003312 pr_warn("error enabling system %s\n",
3313 system->name);
Steven Rostedte6187002009-04-15 13:36:40 -04003314 continue;
3315 }
3316
3317 event_test_stuff();
3318
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003319 ret = __ftrace_set_clr_event(tr, NULL, system->name, NULL, 0);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003320 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003321 pr_warn("error disabling system %s\n",
3322 system->name);
Yuanhan Liu76bab1b2012-08-27 15:13:45 +08003323 continue;
3324 }
Steven Rostedte6187002009-04-15 13:36:40 -04003325
3326 pr_cont("OK\n");
3327 }
3328
3329 /* Test with all events enabled */
3330
3331 pr_info("Running tests on all trace events:\n");
3332 pr_info("Testing all events: ");
3333
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003334 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 1);
Steven Rostedte6187002009-04-15 13:36:40 -04003335 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003336 pr_warn("error enabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003337 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003338 }
3339
3340 event_test_stuff();
3341
3342 /* reset sysname */
Steven Rostedtae63b31e2012-05-03 23:09:03 -04003343 ret = __ftrace_set_clr_event(tr, NULL, NULL, NULL, 0);
Steven Rostedte6187002009-04-15 13:36:40 -04003344 if (WARN_ON_ONCE(ret)) {
Fabian Frederick3448bac2014-06-07 13:43:08 +02003345 pr_warn("error disabling all events\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003346 return;
Steven Rostedte6187002009-04-15 13:36:40 -04003347 }
3348
3349 pr_cont("OK\n");
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003350}
3351
3352#ifdef CONFIG_FUNCTION_TRACER
3353
Tejun Heo245b2e72009-06-24 15:13:48 +09003354static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003355
Steven Rostedt (Red Hat)9b9db272016-04-29 18:10:21 -04003356static struct trace_event_file event_trace_file __initdata;
Steven Rostedt (Red Hat)b7f0c952015-09-25 17:38:44 -04003357
3358static void __init
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003359function_test_events_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003360 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003361{
3362 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -04003363 struct ring_buffer *buffer;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003364 struct ftrace_entry *entry;
3365 unsigned long flags;
3366 long disabled;
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003367 int cpu;
3368 int pc;
3369
3370 pc = preempt_count();
Steven Rostedt5168ae52010-06-03 09:36:50 -04003371 preempt_disable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003372 cpu = raw_smp_processor_id();
Tejun Heo245b2e72009-06-24 15:13:48 +09003373 disabled = atomic_inc_return(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003374
3375 if (disabled != 1)
3376 goto out;
3377
3378 local_save_flags(flags);
3379
Steven Rostedt (Red Hat)9b9db272016-04-29 18:10:21 -04003380 event = trace_event_buffer_lock_reserve(&buffer, &event_trace_file,
3381 TRACE_FN, sizeof(*entry),
3382 flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003383 if (!event)
3384 goto out;
3385 entry = ring_buffer_event_data(event);
3386 entry->ip = ip;
3387 entry->parent_ip = parent_ip;
3388
Steven Rostedt (Red Hat)9b9db272016-04-29 18:10:21 -04003389 event_trigger_unlock_commit(&event_trace_file, buffer, event,
3390 entry, flags, pc);
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003391 out:
Tejun Heo245b2e72009-06-24 15:13:48 +09003392 atomic_dec(&per_cpu(ftrace_test_event_disable, cpu));
Steven Rostedt5168ae52010-06-03 09:36:50 -04003393 preempt_enable_notrace();
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003394}
3395
3396static struct ftrace_ops trace_ops __initdata =
3397{
3398 .func = function_test_events_call,
Steven Rostedt47409742012-07-20 11:04:44 -04003399 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003400};
3401
3402static __init void event_trace_self_test_with_function(void)
3403{
Steven Rostedt17bb6152011-05-23 15:27:46 -04003404 int ret;
Steven Rostedt (Red Hat)9b9db272016-04-29 18:10:21 -04003405
3406 event_trace_file.tr = top_trace_array();
3407 if (WARN_ON(!event_trace_file.tr))
Steven Rostedt (Red Hat)2d34f482015-09-30 11:45:22 -04003408 return;
Steven Rostedt (Red Hat)9b9db272016-04-29 18:10:21 -04003409
Steven Rostedt17bb6152011-05-23 15:27:46 -04003410 ret = register_ftrace_function(&trace_ops);
3411 if (WARN_ON(ret < 0)) {
3412 pr_info("Failed to enable function tracer for event tests\n");
3413 return;
3414 }
Steven Rostedt9ea21c12009-04-16 12:15:44 -04003415 pr_info("Running tests again, along with the function tracer\n");
3416 event_trace_self_tests();
3417 unregister_ftrace_function(&trace_ops);
3418}
3419#else
3420static __init void event_trace_self_test_with_function(void)
3421{
3422}
3423#endif
3424
3425static __init int event_trace_self_tests_init(void)
3426{
Li Zefan020e5f82009-07-01 10:47:05 +08003427 if (!tracing_selftest_disabled) {
3428 event_trace_self_tests();
3429 event_trace_self_test_with_function();
3430 }
Steven Rostedte6187002009-04-15 13:36:40 -04003431
3432 return 0;
3433}
3434
Steven Rostedt28d20e22009-04-20 12:12:44 -04003435late_initcall(event_trace_self_tests_init);
Steven Rostedte6187002009-04-15 13:36:40 -04003436
3437#endif