]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/trace/trace_events.c
Merge branch 'tracing/syscalls' into tracing/core
[mirror_ubuntu-zesty-kernel.git] / kernel / trace / trace_events.c
CommitLineData
b77e38aa
SR
1/*
2 * event tracer
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
981d081e
SR
6 * - Added format output of fields of the trace point.
7 * This was based off of work by Tom Zanussi <tzanussi@gmail.com>.
8 *
b77e38aa
SR
9 */
10
11#include <linux/debugfs.h>
12#include <linux/uaccess.h>
13#include <linux/module.h>
14#include <linux/ctype.h>
15
91729ef9 16#include "trace_output.h"
b77e38aa 17
b628b3e6
SR
18#define TRACE_SYSTEM "TRACE_SYSTEM"
19
11a241a3
SR
20static DEFINE_MUTEX(event_mutex);
21
1473e441
SR
22#define events_for_each(event) \
23 for (event = __start_ftrace_events; \
24 (unsigned long)event < (unsigned long)__stop_ftrace_events; \
25 event++)
26
b77e38aa
SR
27static void ftrace_clear_events(void)
28{
29 struct ftrace_event_call *call = (void *)__start_ftrace_events;
30
31
32 while ((unsigned long)call < (unsigned long)__stop_ftrace_events) {
33
34 if (call->enabled) {
35 call->enabled = 0;
36 call->unregfunc();
37 }
38 call++;
39 }
40}
41
fd994989
SR
42static void ftrace_event_enable_disable(struct ftrace_event_call *call,
43 int enable)
44{
45
46 switch (enable) {
47 case 0:
48 if (call->enabled) {
49 call->enabled = 0;
50 call->unregfunc();
51 }
fd994989
SR
52 break;
53 case 1:
da4d0302 54 if (!call->enabled) {
fd994989
SR
55 call->enabled = 1;
56 call->regfunc();
57 }
fd994989
SR
58 break;
59 }
60}
61
b77e38aa
SR
62static int ftrace_set_clr_event(char *buf, int set)
63{
1473e441 64 struct ftrace_event_call *call = __start_ftrace_events;
b628b3e6
SR
65 char *event = NULL, *sub = NULL, *match;
66 int ret = -EINVAL;
67
68 /*
69 * The buf format can be <subsystem>:<event-name>
70 * *:<event-name> means any event by that name.
71 * :<event-name> is the same.
72 *
73 * <subsystem>:* means all events in that subsystem
74 * <subsystem>: means the same.
75 *
76 * <name> (no ':') means all events in a subsystem with
77 * the name <name> or any event that matches <name>
78 */
79
80 match = strsep(&buf, ":");
81 if (buf) {
82 sub = match;
83 event = buf;
84 match = NULL;
85
86 if (!strlen(sub) || strcmp(sub, "*") == 0)
87 sub = NULL;
88 if (!strlen(event) || strcmp(event, "*") == 0)
89 event = NULL;
90 }
b77e38aa 91
11a241a3 92 mutex_lock(&event_mutex);
1473e441 93 events_for_each(call) {
b77e38aa 94
40e26815 95 if (!call->name || !call->regfunc)
1473e441
SR
96 continue;
97
b628b3e6
SR
98 if (match &&
99 strcmp(match, call->name) != 0 &&
100 strcmp(match, call->system) != 0)
101 continue;
102
103 if (sub && strcmp(sub, call->system) != 0)
104 continue;
105
106 if (event && strcmp(event, call->name) != 0)
b77e38aa 107 continue;
b77e38aa 108
fd994989
SR
109 ftrace_event_enable_disable(call, set);
110
b628b3e6 111 ret = 0;
b77e38aa 112 }
11a241a3
SR
113 mutex_unlock(&event_mutex);
114
b628b3e6 115 return ret;
b77e38aa
SR
116}
117
118/* 128 should be much more than enough */
119#define EVENT_BUF_SIZE 127
120
121static ssize_t
122ftrace_event_write(struct file *file, const char __user *ubuf,
123 size_t cnt, loff_t *ppos)
124{
125 size_t read = 0;
126 int i, set = 1;
127 ssize_t ret;
128 char *buf;
129 char ch;
130
131 if (!cnt || cnt < 0)
132 return 0;
133
1852fcce
SR
134 ret = tracing_update_buffers();
135 if (ret < 0)
136 return ret;
137
b77e38aa
SR
138 ret = get_user(ch, ubuf++);
139 if (ret)
140 return ret;
141 read++;
142 cnt--;
143
144 /* skip white space */
145 while (cnt && isspace(ch)) {
146 ret = get_user(ch, ubuf++);
147 if (ret)
148 return ret;
149 read++;
150 cnt--;
151 }
152
153 /* Only white space found? */
154 if (isspace(ch)) {
155 file->f_pos += read;
156 ret = read;
157 return ret;
158 }
159
160 buf = kmalloc(EVENT_BUF_SIZE+1, GFP_KERNEL);
161 if (!buf)
162 return -ENOMEM;
163
164 if (cnt > EVENT_BUF_SIZE)
165 cnt = EVENT_BUF_SIZE;
166
167 i = 0;
168 while (cnt && !isspace(ch)) {
169 if (!i && ch == '!')
170 set = 0;
171 else
172 buf[i++] = ch;
173
174 ret = get_user(ch, ubuf++);
175 if (ret)
176 goto out_free;
177 read++;
178 cnt--;
179 }
180 buf[i] = 0;
181
182 file->f_pos += read;
183
184 ret = ftrace_set_clr_event(buf, set);
185 if (ret)
186 goto out_free;
187
188 ret = read;
189
190 out_free:
191 kfree(buf);
192
193 return ret;
194}
195
196static void *
197t_next(struct seq_file *m, void *v, loff_t *pos)
198{
199 struct ftrace_event_call *call = m->private;
200 struct ftrace_event_call *next = call;
201
202 (*pos)++;
203
40e26815
SR
204 for (;;) {
205 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
206 return NULL;
207
208 /*
209 * The ftrace subsystem is for showing formats only.
210 * They can not be enabled or disabled via the event files.
211 */
212 if (call->regfunc)
213 break;
214
215 call++;
216 next = call;
217 }
b77e38aa
SR
218
219 m->private = ++next;
220
221 return call;
222}
223
224static void *t_start(struct seq_file *m, loff_t *pos)
225{
226 return t_next(m, NULL, pos);
227}
228
229static void *
230s_next(struct seq_file *m, void *v, loff_t *pos)
231{
232 struct ftrace_event_call *call = m->private;
233 struct ftrace_event_call *next;
234
235 (*pos)++;
236
237 retry:
238 if ((unsigned long)call >= (unsigned long)__stop_ftrace_events)
239 return NULL;
240
241 if (!call->enabled) {
242 call++;
243 goto retry;
244 }
245
246 next = call;
247 m->private = ++next;
248
249 return call;
250}
251
252static void *s_start(struct seq_file *m, loff_t *pos)
253{
254 return s_next(m, NULL, pos);
255}
256
257static int t_show(struct seq_file *m, void *v)
258{
259 struct ftrace_event_call *call = v;
260
b628b3e6
SR
261 if (strcmp(call->system, TRACE_SYSTEM) != 0)
262 seq_printf(m, "%s:", call->system);
b77e38aa
SR
263 seq_printf(m, "%s\n", call->name);
264
265 return 0;
266}
267
268static void t_stop(struct seq_file *m, void *p)
269{
270}
271
272static int
273ftrace_event_seq_open(struct inode *inode, struct file *file)
274{
275 int ret;
276 const struct seq_operations *seq_ops;
277
278 if ((file->f_mode & FMODE_WRITE) &&
279 !(file->f_flags & O_APPEND))
280 ftrace_clear_events();
281
282 seq_ops = inode->i_private;
283 ret = seq_open(file, seq_ops);
284 if (!ret) {
285 struct seq_file *m = file->private_data;
286
287 m->private = __start_ftrace_events;
288 }
289 return ret;
290}
291
1473e441
SR
292static ssize_t
293event_enable_read(struct file *filp, char __user *ubuf, size_t cnt,
294 loff_t *ppos)
295{
296 struct ftrace_event_call *call = filp->private_data;
297 char *buf;
298
da4d0302 299 if (call->enabled)
1473e441
SR
300 buf = "1\n";
301 else
302 buf = "0\n";
303
304 return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
305}
306
307static ssize_t
308event_enable_write(struct file *filp, const char __user *ubuf, size_t cnt,
309 loff_t *ppos)
310{
311 struct ftrace_event_call *call = filp->private_data;
312 char buf[64];
313 unsigned long val;
314 int ret;
315
316 if (cnt >= sizeof(buf))
317 return -EINVAL;
318
319 if (copy_from_user(&buf, ubuf, cnt))
320 return -EFAULT;
321
322 buf[cnt] = 0;
323
324 ret = strict_strtoul(buf, 10, &val);
325 if (ret < 0)
326 return ret;
327
1852fcce
SR
328 ret = tracing_update_buffers();
329 if (ret < 0)
330 return ret;
331
1473e441
SR
332 switch (val) {
333 case 0:
1473e441 334 case 1:
11a241a3 335 mutex_lock(&event_mutex);
fd994989 336 ftrace_event_enable_disable(call, val);
11a241a3 337 mutex_unlock(&event_mutex);
1473e441
SR
338 break;
339
340 default:
341 return -EINVAL;
342 }
343
344 *ppos += cnt;
345
346 return cnt;
347}
348
91729ef9 349#undef FIELD
156b5f17 350#define FIELD(type, name) \
ce8eb2bf 351 #type, #name, offsetof(typeof(field), name), sizeof(field.name)
91729ef9
SR
352
353static int trace_write_header(struct trace_seq *s)
354{
355 struct trace_entry field;
356
357 /* struct trace_entry */
358 return trace_seq_printf(s,
ce8eb2bf
SR
359 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
360 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
361 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
362 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
363 "\tfield:%s %s;\toffset:%zu;\tsize:%zu;\n"
91729ef9
SR
364 "\n",
365 FIELD(unsigned char, type),
366 FIELD(unsigned char, flags),
367 FIELD(unsigned char, preempt_count),
368 FIELD(int, pid),
369 FIELD(int, tgid));
370}
da4d0302 371
981d081e
SR
372static ssize_t
373event_format_read(struct file *filp, char __user *ubuf, size_t cnt,
374 loff_t *ppos)
375{
376 struct ftrace_event_call *call = filp->private_data;
377 struct trace_seq *s;
378 char *buf;
379 int r;
380
381 s = kmalloc(sizeof(*s), GFP_KERNEL);
382 if (!s)
383 return -ENOMEM;
384
385 trace_seq_init(s);
386
387 if (*ppos)
388 return 0;
389
c5e4e192
SR
390 /* If any of the first writes fail, so will the show_format. */
391
392 trace_seq_printf(s, "name: %s\n", call->name);
393 trace_seq_printf(s, "ID: %d\n", call->id);
394 trace_seq_printf(s, "format:\n");
91729ef9
SR
395 trace_write_header(s);
396
981d081e
SR
397 r = call->show_format(s);
398 if (!r) {
399 /*
400 * ug! The format output is bigger than a PAGE!!
401 */
402 buf = "FORMAT TOO BIG\n";
403 r = simple_read_from_buffer(ubuf, cnt, ppos,
404 buf, strlen(buf));
405 goto out;
406 }
407
408 r = simple_read_from_buffer(ubuf, cnt, ppos,
409 s->buffer, s->len);
410 out:
411 kfree(s);
412 return r;
413}
414
b77e38aa
SR
415static const struct seq_operations show_event_seq_ops = {
416 .start = t_start,
417 .next = t_next,
418 .show = t_show,
419 .stop = t_stop,
420};
421
422static const struct seq_operations show_set_event_seq_ops = {
423 .start = s_start,
424 .next = s_next,
425 .show = t_show,
426 .stop = t_stop,
427};
428
2314c4ae
SR
429static const struct file_operations ftrace_avail_fops = {
430 .open = ftrace_event_seq_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = seq_release,
434};
435
b77e38aa
SR
436static const struct file_operations ftrace_set_event_fops = {
437 .open = ftrace_event_seq_open,
438 .read = seq_read,
439 .write = ftrace_event_write,
440 .llseek = seq_lseek,
441 .release = seq_release,
442};
443
1473e441
SR
444static const struct file_operations ftrace_enable_fops = {
445 .open = tracing_open_generic,
446 .read = event_enable_read,
447 .write = event_enable_write,
448};
449
981d081e
SR
450static const struct file_operations ftrace_event_format_fops = {
451 .open = tracing_open_generic,
452 .read = event_format_read,
453};
454
1473e441
SR
455static struct dentry *event_trace_events_dir(void)
456{
457 static struct dentry *d_tracer;
458 static struct dentry *d_events;
459
460 if (d_events)
461 return d_events;
462
463 d_tracer = tracing_init_dentry();
464 if (!d_tracer)
465 return NULL;
466
467 d_events = debugfs_create_dir("events", d_tracer);
468 if (!d_events)
469 pr_warning("Could not create debugfs "
470 "'events' directory\n");
471
472 return d_events;
473}
474
6ecc2d1c
SR
475struct event_subsystem {
476 struct list_head list;
477 const char *name;
478 struct dentry *entry;
479};
480
481static LIST_HEAD(event_subsystems);
482
483static struct dentry *
484event_subsystem_dir(const char *name, struct dentry *d_events)
485{
486 struct event_subsystem *system;
487
488 /* First see if we did not already create this dir */
489 list_for_each_entry(system, &event_subsystems, list) {
490 if (strcmp(system->name, name) == 0)
491 return system->entry;
492 }
493
494 /* need to create new entry */
495 system = kmalloc(sizeof(*system), GFP_KERNEL);
496 if (!system) {
497 pr_warning("No memory to create event subsystem %s\n",
498 name);
499 return d_events;
500 }
501
502 system->entry = debugfs_create_dir(name, d_events);
503 if (!system->entry) {
504 pr_warning("Could not create event subsystem %s\n",
505 name);
506 kfree(system);
507 return d_events;
508 }
509
510 system->name = name;
511 list_add(&system->list, &event_subsystems);
512
513 return system->entry;
514}
515
1473e441
SR
516static int
517event_create_dir(struct ftrace_event_call *call, struct dentry *d_events)
518{
519 struct dentry *entry;
fd994989 520 int ret;
1473e441 521
6ecc2d1c
SR
522 /*
523 * If the trace point header did not define TRACE_SYSTEM
524 * then the system would be called "TRACE_SYSTEM".
525 */
526 if (strcmp(call->system, "TRACE_SYSTEM") != 0)
527 d_events = event_subsystem_dir(call->system, d_events);
528
fd994989
SR
529 if (call->raw_init) {
530 ret = call->raw_init();
531 if (ret < 0) {
532 pr_warning("Could not initialize trace point"
533 " events/%s\n", call->name);
534 return ret;
535 }
536 }
537
1473e441
SR
538 call->dir = debugfs_create_dir(call->name, d_events);
539 if (!call->dir) {
540 pr_warning("Could not create debugfs "
541 "'%s' directory\n", call->name);
542 return -1;
543 }
544
770cb243
SR
545 if (call->regfunc) {
546 entry = debugfs_create_file("enable", 0644, call->dir, call,
547 &ftrace_enable_fops);
548 if (!entry)
549 pr_warning("Could not create debugfs "
550 "'%s/enable' entry\n", call->name);
551 }
1473e441 552
981d081e
SR
553 /* A trace may not want to export its format */
554 if (!call->show_format)
555 return 0;
556
557 entry = debugfs_create_file("format", 0444, call->dir, call,
558 &ftrace_event_format_fops);
559 if (!entry)
560 pr_warning("Could not create debugfs "
561 "'%s/format' entry\n", call->name);
fd994989 562
1473e441
SR
563 return 0;
564}
565
b77e38aa
SR
566static __init int event_trace_init(void)
567{
1473e441 568 struct ftrace_event_call *call = __start_ftrace_events;
b77e38aa
SR
569 struct dentry *d_tracer;
570 struct dentry *entry;
1473e441 571 struct dentry *d_events;
b77e38aa
SR
572
573 d_tracer = tracing_init_dentry();
574 if (!d_tracer)
575 return 0;
576
2314c4ae
SR
577 entry = debugfs_create_file("available_events", 0444, d_tracer,
578 (void *)&show_event_seq_ops,
579 &ftrace_avail_fops);
580 if (!entry)
581 pr_warning("Could not create debugfs "
582 "'available_events' entry\n");
583
b77e38aa
SR
584 entry = debugfs_create_file("set_event", 0644, d_tracer,
585 (void *)&show_set_event_seq_ops,
586 &ftrace_set_event_fops);
587 if (!entry)
588 pr_warning("Could not create debugfs "
589 "'set_event' entry\n");
590
1473e441
SR
591 d_events = event_trace_events_dir();
592 if (!d_events)
593 return 0;
594
595 events_for_each(call) {
596 /* The linker may leave blanks */
597 if (!call->name)
598 continue;
599 event_create_dir(call, d_events);
600 }
601
b77e38aa
SR
602 return 0;
603}
604fs_initcall(event_trace_init);