]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/trace/trace_output.c
tg3: Add phy-related preprocessor constants
[net-next-2.6.git] / kernel / trace / trace_output.c
CommitLineData
f0868d1e
SR
1/*
2 * trace_output.c
3 *
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
5 *
6 */
7
8#include <linux/module.h>
9#include <linux/mutex.h>
10#include <linux/ftrace.h>
11
12#include "trace_output.h"
13
14/* must be a power of 2 */
15#define EVENT_HASHSIZE 128
16
110bf2b7 17DECLARE_RWSEM(trace_event_mutex);
be74b73a
SR
18
19DEFINE_PER_CPU(struct trace_seq, ftrace_event_seq);
ec081ddc 20EXPORT_PER_CPU_SYMBOL(ftrace_event_seq);
be74b73a 21
f0868d1e
SR
22static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
23
24static int next_event_type = __TRACE_LAST_TYPE + 1;
25
a63ce5b3 26int trace_print_seq(struct seq_file *m, struct trace_seq *s)
0706f1c4
SR
27{
28 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
a63ce5b3
SR
29 int ret;
30
31 ret = seq_write(m, s->buffer, len);
0706f1c4 32
a63ce5b3
SR
33 /*
34 * Only reset this buffer if we successfully wrote to the
35 * seq_file buffer.
36 */
37 if (!ret)
38 trace_seq_init(s);
0706f1c4 39
a63ce5b3 40 return ret;
0706f1c4
SR
41}
42
5ef841f6
SR
43enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
44{
45 struct trace_seq *s = &iter->seq;
46 struct trace_entry *entry = iter->ent;
47 struct bprint_entry *field;
48 int ret;
49
50 trace_assign_type(field, entry);
51
52 ret = trace_seq_bprintf(s, field->fmt, field->buf);
53 if (!ret)
54 return TRACE_TYPE_PARTIAL_LINE;
55
56 return TRACE_TYPE_HANDLED;
57}
58
59enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
60{
61 struct trace_seq *s = &iter->seq;
62 struct trace_entry *entry = iter->ent;
63 struct print_entry *field;
64 int ret;
65
66 trace_assign_type(field, entry);
67
68 ret = trace_seq_printf(s, "%s", field->buf);
69 if (!ret)
70 return TRACE_TYPE_PARTIAL_LINE;
71
72 return TRACE_TYPE_HANDLED;
73}
74
f0868d1e
SR
75/**
76 * trace_seq_printf - sequence printing of trace information
77 * @s: trace sequence descriptor
78 * @fmt: printf format string
79 *
3e69533b
JO
80 * It returns 0 if the trace oversizes the buffer's free
81 * space, 1 otherwise.
82 *
f0868d1e
SR
83 * The tracer may use either sequence operations or its own
84 * copy to user routines. To simplify formating of a trace
85 * trace_seq_printf is used to store strings into a special
86 * buffer (@s). Then the output may be either used by
87 * the sequencer or pulled into another buffer.
88 */
89int
90trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
91{
92 int len = (PAGE_SIZE - 1) - s->len;
93 va_list ap;
94 int ret;
95
d184b31c 96 if (s->full || !len)
f0868d1e
SR
97 return 0;
98
99 va_start(ap, fmt);
100 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
101 va_end(ap);
102
103 /* If we can't write it all, don't bother writing anything */
d184b31c
JB
104 if (ret >= len) {
105 s->full = 1;
f0868d1e 106 return 0;
d184b31c 107 }
f0868d1e
SR
108
109 s->len += ret;
110
3e69533b 111 return 1;
f0868d1e 112}
17c873ec 113EXPORT_SYMBOL_GPL(trace_seq_printf);
f0868d1e 114
725c624a
SR
115/**
116 * trace_seq_vprintf - sequence printing of trace information
117 * @s: trace sequence descriptor
118 * @fmt: printf format string
119 *
120 * The tracer may use either sequence operations or its own
121 * copy to user routines. To simplify formating of a trace
122 * trace_seq_printf is used to store strings into a special
123 * buffer (@s). Then the output may be either used by
124 * the sequencer or pulled into another buffer.
125 */
126int
127trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
128{
129 int len = (PAGE_SIZE - 1) - s->len;
130 int ret;
131
d184b31c 132 if (s->full || !len)
725c624a
SR
133 return 0;
134
135 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
136
137 /* If we can't write it all, don't bother writing anything */
d184b31c
JB
138 if (ret >= len) {
139 s->full = 1;
725c624a 140 return 0;
d184b31c 141 }
725c624a
SR
142
143 s->len += ret;
144
145 return len;
146}
147EXPORT_SYMBOL_GPL(trace_seq_vprintf);
148
769b0441 149int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
1427cdf0
LJ
150{
151 int len = (PAGE_SIZE - 1) - s->len;
152 int ret;
153
d184b31c 154 if (s->full || !len)
1427cdf0
LJ
155 return 0;
156
157 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
158
159 /* If we can't write it all, don't bother writing anything */
d184b31c
JB
160 if (ret >= len) {
161 s->full = 1;
1427cdf0 162 return 0;
d184b31c 163 }
1427cdf0
LJ
164
165 s->len += ret;
166
167 return len;
168}
169
f0868d1e
SR
170/**
171 * trace_seq_puts - trace sequence printing of simple string
172 * @s: trace sequence descriptor
173 * @str: simple string to record
174 *
175 * The tracer may use either the sequence operations or its own
176 * copy to user routines. This function records a simple string
177 * into a special buffer (@s) for later retrieval by a sequencer
178 * or other mechanism.
179 */
180int trace_seq_puts(struct trace_seq *s, const char *str)
181{
182 int len = strlen(str);
183
d184b31c 184 if (s->full)
f0868d1e
SR
185 return 0;
186
d184b31c
JB
187 if (len > ((PAGE_SIZE - 1) - s->len)) {
188 s->full = 1;
189 return 0;
190 }
191
f0868d1e
SR
192 memcpy(s->buffer + s->len, str, len);
193 s->len += len;
194
195 return len;
196}
197
198int trace_seq_putc(struct trace_seq *s, unsigned char c)
199{
d184b31c 200 if (s->full)
f0868d1e
SR
201 return 0;
202
d184b31c
JB
203 if (s->len >= (PAGE_SIZE - 1)) {
204 s->full = 1;
205 return 0;
206 }
207
f0868d1e
SR
208 s->buffer[s->len++] = c;
209
210 return 1;
211}
bf816235 212EXPORT_SYMBOL(trace_seq_putc);
f0868d1e 213
b14b70a6 214int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
f0868d1e 215{
d184b31c 216 if (s->full)
f0868d1e
SR
217 return 0;
218
d184b31c
JB
219 if (len > ((PAGE_SIZE - 1) - s->len)) {
220 s->full = 1;
221 return 0;
222 }
223
f0868d1e
SR
224 memcpy(s->buffer + s->len, mem, len);
225 s->len += len;
226
227 return len;
228}
229
b14b70a6 230int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
f0868d1e
SR
231{
232 unsigned char hex[HEX_CHARS];
b14b70a6 233 const unsigned char *data = mem;
f0868d1e
SR
234 int i, j;
235
d184b31c
JB
236 if (s->full)
237 return 0;
238
f0868d1e
SR
239#ifdef __BIG_ENDIAN
240 for (i = 0, j = 0; i < len; i++) {
241#else
242 for (i = len-1, j = 0; i >= 0; i--) {
243#endif
244 hex[j++] = hex_asc_hi(data[i]);
245 hex[j++] = hex_asc_lo(data[i]);
246 }
247 hex[j++] = ' ';
248
249 return trace_seq_putmem(s, hex, j);
250}
251
bdd6df6a
EGM
252void *trace_seq_reserve(struct trace_seq *s, size_t len)
253{
254 void *ret;
255
d184b31c 256 if (s->full)
668eb65f 257 return NULL;
d184b31c
JB
258
259 if (len > ((PAGE_SIZE - 1) - s->len)) {
260 s->full = 1;
bdd6df6a 261 return NULL;
d184b31c 262 }
bdd6df6a
EGM
263
264 ret = s->buffer + s->len;
265 s->len += len;
266
267 return ret;
268}
269
f0868d1e
SR
270int trace_seq_path(struct trace_seq *s, struct path *path)
271{
272 unsigned char *p;
273
d184b31c 274 if (s->full)
f0868d1e 275 return 0;
d184b31c
JB
276
277 if (s->len >= (PAGE_SIZE - 1)) {
278 s->full = 1;
279 return 0;
280 }
281
f0868d1e
SR
282 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
283 if (!IS_ERR(p)) {
284 p = mangle_path(s->buffer + s->len, p, "\n");
285 if (p) {
286 s->len = p - s->buffer;
287 return 1;
288 }
289 } else {
290 s->buffer[s->len++] = '?';
291 return 1;
292 }
293
d184b31c 294 s->full = 1;
f0868d1e
SR
295 return 0;
296}
297
be74b73a
SR
298const char *
299ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
300 unsigned long flags,
301 const struct trace_print_flags *flag_array)
302{
303 unsigned long mask;
304 const char *str;
56d8bd3f 305 const char *ret = p->buffer + p->len;
be74b73a
SR
306 int i;
307
be74b73a
SR
308 for (i = 0; flag_array[i].name && flags; i++) {
309
310 mask = flag_array[i].mask;
311 if ((flags & mask) != mask)
312 continue;
313
314 str = flag_array[i].name;
315 flags &= ~mask;
316 if (p->len && delim)
317 trace_seq_puts(p, delim);
318 trace_seq_puts(p, str);
319 }
320
321 /* check for left over flags */
322 if (flags) {
323 if (p->len && delim)
324 trace_seq_puts(p, delim);
325 trace_seq_printf(p, "0x%lx", flags);
326 }
327
328 trace_seq_putc(p, 0);
329
56d8bd3f 330 return ret;
be74b73a 331}
ec081ddc 332EXPORT_SYMBOL(ftrace_print_flags_seq);
be74b73a 333
0f4fc29d
SR
334const char *
335ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
336 const struct trace_print_flags *symbol_array)
337{
338 int i;
56d8bd3f 339 const char *ret = p->buffer + p->len;
0f4fc29d
SR
340
341 for (i = 0; symbol_array[i].name; i++) {
342
343 if (val != symbol_array[i].mask)
344 continue;
345
346 trace_seq_puts(p, symbol_array[i].name);
347 break;
348 }
349
350 if (!p->len)
351 trace_seq_printf(p, "0x%lx", val);
352
353 trace_seq_putc(p, 0);
354
56d8bd3f 355 return ret;
0f4fc29d 356}
ec081ddc 357EXPORT_SYMBOL(ftrace_print_symbols_seq);
0f4fc29d 358
5a2e3995
KT
359const char *
360ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
361{
362 int i;
363 const char *ret = p->buffer + p->len;
364
365 for (i = 0; i < buf_len; i++)
366 trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);
367
368 trace_seq_putc(p, 0);
369
370 return ret;
371}
372EXPORT_SYMBOL(ftrace_print_hex_seq);
373
f0868d1e
SR
374#ifdef CONFIG_KRETPROBES
375static inline const char *kretprobed(const char *name)
376{
377 static const char tramp_name[] = "kretprobe_trampoline";
378 int size = sizeof(tramp_name);
379
380 if (strncmp(tramp_name, name, size) == 0)
381 return "[unknown/kretprobe'd]";
382 return name;
383}
384#else
385static inline const char *kretprobed(const char *name)
386{
387 return name;
388}
389#endif /* CONFIG_KRETPROBES */
390
391static int
392seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
393{
394#ifdef CONFIG_KALLSYMS
395 char str[KSYM_SYMBOL_LEN];
396 const char *name;
397
398 kallsyms_lookup(address, NULL, NULL, NULL, str);
399
400 name = kretprobed(str);
401
402 return trace_seq_printf(s, fmt, name);
403#endif
404 return 1;
405}
406
407static int
408seq_print_sym_offset(struct trace_seq *s, const char *fmt,
409 unsigned long address)
410{
411#ifdef CONFIG_KALLSYMS
412 char str[KSYM_SYMBOL_LEN];
413 const char *name;
414
415 sprint_symbol(str, address);
416 name = kretprobed(str);
417
418 return trace_seq_printf(s, fmt, name);
419#endif
420 return 1;
421}
422
423#ifndef CONFIG_64BIT
424# define IP_FMT "%08lx"
425#else
426# define IP_FMT "%016lx"
427#endif
428
429int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
430 unsigned long ip, unsigned long sym_flags)
431{
432 struct file *file = NULL;
433 unsigned long vmstart = 0;
434 int ret = 1;
435
d184b31c
JB
436 if (s->full)
437 return 0;
438
f0868d1e
SR
439 if (mm) {
440 const struct vm_area_struct *vma;
441
442 down_read(&mm->mmap_sem);
443 vma = find_vma(mm, ip);
444 if (vma) {
445 file = vma->vm_file;
446 vmstart = vma->vm_start;
447 }
448 if (file) {
449 ret = trace_seq_path(s, &file->f_path);
450 if (ret)
451 ret = trace_seq_printf(s, "[+0x%lx]",
452 ip - vmstart);
453 }
454 up_read(&mm->mmap_sem);
455 }
456 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
457 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
458 return ret;
459}
460
461int
462seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
463 unsigned long sym_flags)
464{
465 struct mm_struct *mm = NULL;
466 int ret = 1;
467 unsigned int i;
468
469 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
470 struct task_struct *task;
471 /*
472 * we do the lookup on the thread group leader,
473 * since individual threads might have already quit!
474 */
475 rcu_read_lock();
48659d31 476 task = find_task_by_vpid(entry->tgid);
f0868d1e
SR
477 if (task)
478 mm = get_task_mm(task);
479 rcu_read_unlock();
480 }
481
482 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
483 unsigned long ip = entry->caller[i];
484
485 if (ip == ULONG_MAX || !ret)
486 break;
048dc50c 487 if (ret)
488 ret = trace_seq_puts(s, " => ");
f0868d1e
SR
489 if (!ip) {
490 if (ret)
491 ret = trace_seq_puts(s, "??");
048dc50c 492 if (ret)
493 ret = trace_seq_puts(s, "\n");
f0868d1e
SR
494 continue;
495 }
496 if (!ret)
497 break;
498 if (ret)
499 ret = seq_print_user_ip(s, mm, ip, sym_flags);
048dc50c 500 ret = trace_seq_puts(s, "\n");
f0868d1e
SR
501 }
502
503 if (mm)
504 mmput(mm);
505 return ret;
506}
507
508int
509seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
510{
511 int ret;
512
513 if (!ip)
514 return trace_seq_printf(s, "0");
515
516 if (sym_flags & TRACE_ITER_SYM_OFFSET)
517 ret = seq_print_sym_offset(s, "%s", ip);
518 else
519 ret = seq_print_sym_short(s, "%s", ip);
520
521 if (!ret)
522 return 0;
523
524 if (sym_flags & TRACE_ITER_SYM_ADDR)
525 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
526 return ret;
527}
528
f81c972d
SR
529/**
530 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
531 * @s: trace seq struct to write to
532 * @entry: The trace entry field from the ring buffer
533 *
534 * Prints the generic fields of irqs off, in hard or softirq, preempt
535 * count and lock depth.
536 */
537int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
c4a8e8be
FW
538{
539 int hardirq, softirq;
637e7e86 540 int ret;
c4a8e8be 541
c4a8e8be
FW
542 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
543 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
d9793bd8 544
f81c972d 545 if (!trace_seq_printf(s, "%c%c%c",
d9793bd8
ACM
546 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
547 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
548 'X' : '.',
549 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
550 'N' : '.',
551 (hardirq && softirq) ? 'H' :
552 hardirq ? 'h' : softirq ? 's' : '.'))
553 return 0;
c4a8e8be 554
829b876d
SR
555 if (entry->preempt_count)
556 ret = trace_seq_printf(s, "%x", entry->preempt_count);
637e7e86 557 else
829b876d
SR
558 ret = trace_seq_putc(s, '.');
559
637e7e86
SR
560 if (!ret)
561 return 0;
562
829b876d
SR
563 if (entry->lock_depth < 0)
564 return trace_seq_putc(s, '.');
565
566 return trace_seq_printf(s, "%d", entry->lock_depth);
c4a8e8be
FW
567}
568
f81c972d
SR
569static int
570lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
571{
572 char comm[TASK_COMM_LEN];
573
574 trace_find_cmdline(entry->pid, comm);
575
576 if (!trace_seq_printf(s, "%8.8s-%-5d %3d",
577 comm, entry->pid, cpu))
578 return 0;
579
580 return trace_print_lat_fmt(s, entry);
581}
582
c4a8e8be
FW
583static unsigned long preempt_mark_thresh = 100;
584
d9793bd8 585static int
c4a8e8be
FW
586lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
587 unsigned long rel_usecs)
588{
d9793bd8
ACM
589 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
590 rel_usecs > preempt_mark_thresh ? '!' :
591 rel_usecs > 1 ? '+' : ' ');
c4a8e8be
FW
592}
593
594int trace_print_context(struct trace_iterator *iter)
595{
596 struct trace_seq *s = &iter->seq;
597 struct trace_entry *entry = iter->ent;
c4a8e8be
FW
598 unsigned long long t = ns2usecs(iter->ts);
599 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
600 unsigned long secs = (unsigned long)t;
4ca53085
SR
601 char comm[TASK_COMM_LEN];
602
603 trace_find_cmdline(entry->pid, comm);
c4a8e8be 604
d9793bd8 605 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
1830b52d 606 comm, entry->pid, iter->cpu, secs, usec_rem);
c4a8e8be
FW
607}
608
609int trace_print_lat_context(struct trace_iterator *iter)
610{
611 u64 next_ts;
d9793bd8 612 int ret;
c4a8e8be
FW
613 struct trace_seq *s = &iter->seq;
614 struct trace_entry *entry = iter->ent,
615 *next_entry = trace_find_next_entry(iter, NULL,
616 &next_ts);
617 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
618 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
619 unsigned long rel_usecs;
620
621 if (!next_entry)
622 next_ts = iter->ts;
623 rel_usecs = ns2usecs(next_ts - iter->ts);
624
625 if (verbose) {
4ca53085
SR
626 char comm[TASK_COMM_LEN];
627
628 trace_find_cmdline(entry->pid, comm);
629
cf8e3474 630 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08llx]"
d9793bd8 631 " %ld.%03ldms (+%ld.%03ldms): ", comm,
1830b52d 632 entry->pid, iter->cpu, entry->flags,
d9793bd8
ACM
633 entry->preempt_count, iter->idx,
634 ns2usecs(iter->ts),
635 abs_usecs / USEC_PER_MSEC,
636 abs_usecs % USEC_PER_MSEC,
637 rel_usecs / USEC_PER_MSEC,
638 rel_usecs % USEC_PER_MSEC);
c4a8e8be 639 } else {
1830b52d 640 ret = lat_print_generic(s, entry, iter->cpu);
d9793bd8
ACM
641 if (ret)
642 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
c4a8e8be
FW
643 }
644
d9793bd8 645 return ret;
c4a8e8be
FW
646}
647
f633cef0
SR
648static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
649
650static int task_state_char(unsigned long state)
651{
652 int bit = state ? __ffs(state) + 1 : 0;
653
654 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
655}
656
f0868d1e
SR
657/**
658 * ftrace_find_event - find a registered event
659 * @type: the type of event to look for
660 *
661 * Returns an event of type @type otherwise NULL
4f535968 662 * Called with trace_event_read_lock() held.
f0868d1e
SR
663 */
664struct trace_event *ftrace_find_event(int type)
665{
666 struct trace_event *event;
667 struct hlist_node *n;
668 unsigned key;
669
670 key = type & (EVENT_HASHSIZE - 1);
671
4f535968 672 hlist_for_each_entry(event, n, &event_hash[key], node) {
f0868d1e
SR
673 if (event->type == type)
674 return event;
675 }
676
677 return NULL;
678}
679
060fa5c8
SR
680static LIST_HEAD(ftrace_event_list);
681
682static int trace_search_list(struct list_head **list)
683{
684 struct trace_event *e;
685 int last = __TRACE_LAST_TYPE;
686
687 if (list_empty(&ftrace_event_list)) {
688 *list = &ftrace_event_list;
689 return last + 1;
690 }
691
692 /*
693 * We used up all possible max events,
694 * lets see if somebody freed one.
695 */
696 list_for_each_entry(e, &ftrace_event_list, list) {
697 if (e->type != last + 1)
698 break;
699 last++;
700 }
701
702 /* Did we used up all 65 thousand events??? */
703 if ((last + 1) > FTRACE_MAX_EVENT)
704 return 0;
705
706 *list = &e->list;
707 return last + 1;
708}
709
4f535968
LJ
710void trace_event_read_lock(void)
711{
712 down_read(&trace_event_mutex);
713}
714
715void trace_event_read_unlock(void)
716{
717 up_read(&trace_event_mutex);
718}
719
f0868d1e
SR
720/**
721 * register_ftrace_event - register output for an event type
722 * @event: the event type to register
723 *
724 * Event types are stored in a hash and this hash is used to
725 * find a way to print an event. If the @event->type is set
726 * then it will use that type, otherwise it will assign a
727 * type to use.
728 *
729 * If you assign your own type, please make sure it is added
730 * to the trace_type enum in trace.h, to avoid collisions
731 * with the dynamic types.
732 *
733 * Returns the event type number or zero on error.
734 */
735int register_ftrace_event(struct trace_event *event)
736{
737 unsigned key;
738 int ret = 0;
739
4f535968 740 down_write(&trace_event_mutex);
f0868d1e 741
060fa5c8 742 if (WARN_ON(!event))
28bea271 743 goto out;
28bea271 744
a9a57763
SR
745 if (WARN_ON(!event->funcs))
746 goto out;
747
060fa5c8
SR
748 INIT_LIST_HEAD(&event->list);
749
750 if (!event->type) {
48dd0fed 751 struct list_head *list = NULL;
060fa5c8
SR
752
753 if (next_event_type > FTRACE_MAX_EVENT) {
754
755 event->type = trace_search_list(&list);
756 if (!event->type)
757 goto out;
758
759 } else {
760
761 event->type = next_event_type++;
762 list = &ftrace_event_list;
763 }
764
765 if (WARN_ON(ftrace_find_event(event->type)))
766 goto out;
767
768 list_add_tail(&event->list, list);
769
770 } else if (event->type > __TRACE_LAST_TYPE) {
f0868d1e
SR
771 printk(KERN_WARNING "Need to add type to trace.h\n");
772 WARN_ON(1);
f0868d1e 773 goto out;
060fa5c8
SR
774 } else {
775 /* Is this event already used */
776 if (ftrace_find_event(event->type))
777 goto out;
778 }
f0868d1e 779
a9a57763
SR
780 if (event->funcs->trace == NULL)
781 event->funcs->trace = trace_nop_print;
782 if (event->funcs->raw == NULL)
783 event->funcs->raw = trace_nop_print;
784 if (event->funcs->hex == NULL)
785 event->funcs->hex = trace_nop_print;
786 if (event->funcs->binary == NULL)
787 event->funcs->binary = trace_nop_print;
268ccda0 788
f0868d1e
SR
789 key = event->type & (EVENT_HASHSIZE - 1);
790
4f535968 791 hlist_add_head(&event->node, &event_hash[key]);
f0868d1e
SR
792
793 ret = event->type;
794 out:
4f535968 795 up_write(&trace_event_mutex);
f0868d1e
SR
796
797 return ret;
798}
17c873ec 799EXPORT_SYMBOL_GPL(register_ftrace_event);
f0868d1e 800
110bf2b7
SR
801/*
802 * Used by module code with the trace_event_mutex held for write.
803 */
804int __unregister_ftrace_event(struct trace_event *event)
805{
806 hlist_del(&event->node);
807 list_del(&event->list);
808 return 0;
809}
810
f0868d1e
SR
811/**
812 * unregister_ftrace_event - remove a no longer used event
813 * @event: the event to remove
814 */
815int unregister_ftrace_event(struct trace_event *event)
816{
4f535968 817 down_write(&trace_event_mutex);
110bf2b7 818 __unregister_ftrace_event(event);
4f535968 819 up_write(&trace_event_mutex);
f0868d1e
SR
820
821 return 0;
822}
17c873ec 823EXPORT_SYMBOL_GPL(unregister_ftrace_event);
f633cef0
SR
824
825/*
826 * Standard events
827 */
828
a9a57763
SR
829enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
830 struct trace_event *event)
f633cef0 831{
d9793bd8 832 return TRACE_TYPE_HANDLED;
f633cef0
SR
833}
834
835/* TRACE_FN */
a9a57763
SR
836static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
837 struct trace_event *event)
f633cef0
SR
838{
839 struct ftrace_entry *field;
2c9b238e 840 struct trace_seq *s = &iter->seq;
f633cef0 841
2c9b238e 842 trace_assign_type(field, iter->ent);
f633cef0
SR
843
844 if (!seq_print_ip_sym(s, field->ip, flags))
845 goto partial;
846
847 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
848 if (!trace_seq_printf(s, " <-"))
849 goto partial;
850 if (!seq_print_ip_sym(s,
851 field->parent_ip,
852 flags))
853 goto partial;
854 }
855 if (!trace_seq_printf(s, "\n"))
856 goto partial;
857
d9793bd8 858 return TRACE_TYPE_HANDLED;
f633cef0
SR
859
860 partial:
861 return TRACE_TYPE_PARTIAL_LINE;
862}
863
a9a57763
SR
864static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
865 struct trace_event *event)
f633cef0
SR
866{
867 struct ftrace_entry *field;
868
2c9b238e 869 trace_assign_type(field, iter->ent);
f633cef0 870
2c9b238e 871 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
6c1a99af
LJ
872 field->ip,
873 field->parent_ip))
f633cef0
SR
874 return TRACE_TYPE_PARTIAL_LINE;
875
d9793bd8 876 return TRACE_TYPE_HANDLED;
f633cef0
SR
877}
878
a9a57763
SR
879static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
880 struct trace_event *event)
f633cef0
SR
881{
882 struct ftrace_entry *field;
2c9b238e 883 struct trace_seq *s = &iter->seq;
f633cef0 884
2c9b238e 885 trace_assign_type(field, iter->ent);
f633cef0
SR
886
887 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
888 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
889
d9793bd8 890 return TRACE_TYPE_HANDLED;
f633cef0
SR
891}
892
a9a57763
SR
893static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
894 struct trace_event *event)
f633cef0
SR
895{
896 struct ftrace_entry *field;
2c9b238e 897 struct trace_seq *s = &iter->seq;
f633cef0 898
2c9b238e 899 trace_assign_type(field, iter->ent);
f633cef0
SR
900
901 SEQ_PUT_FIELD_RET(s, field->ip);
902 SEQ_PUT_FIELD_RET(s, field->parent_ip);
903
d9793bd8 904 return TRACE_TYPE_HANDLED;
f633cef0
SR
905}
906
a9a57763 907static struct trace_event_functions trace_fn_funcs = {
f633cef0 908 .trace = trace_fn_trace,
f633cef0
SR
909 .raw = trace_fn_raw,
910 .hex = trace_fn_hex,
911 .binary = trace_fn_bin,
912};
913
a9a57763
SR
914static struct trace_event trace_fn_event = {
915 .type = TRACE_FN,
916 .funcs = &trace_fn_funcs,
917};
918
f633cef0 919/* TRACE_CTX an TRACE_WAKE */
ae7462b4
ACM
920static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
921 char *delim)
f633cef0
SR
922{
923 struct ctx_switch_entry *field;
4ca53085 924 char comm[TASK_COMM_LEN];
f633cef0
SR
925 int S, T;
926
4ca53085 927
2c9b238e 928 trace_assign_type(field, iter->ent);
f633cef0
SR
929
930 T = task_state_char(field->next_state);
931 S = task_state_char(field->prev_state);
4ca53085 932 trace_find_cmdline(field->next_pid, comm);
2c9b238e
ACM
933 if (!trace_seq_printf(&iter->seq,
934 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
6c1a99af
LJ
935 field->prev_pid,
936 field->prev_prio,
937 S, delim,
938 field->next_cpu,
939 field->next_pid,
940 field->next_prio,
941 T, comm))
f633cef0
SR
942 return TRACE_TYPE_PARTIAL_LINE;
943
d9793bd8 944 return TRACE_TYPE_HANDLED;
f633cef0
SR
945}
946
a9a57763
SR
947static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
948 struct trace_event *event)
f633cef0 949{
2c9b238e 950 return trace_ctxwake_print(iter, "==>");
f633cef0
SR
951}
952
ae7462b4 953static enum print_line_t trace_wake_print(struct trace_iterator *iter,
a9a57763 954 int flags, struct trace_event *event)
f633cef0 955{
2c9b238e 956 return trace_ctxwake_print(iter, " +");
f633cef0
SR
957}
958
2c9b238e 959static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
f633cef0
SR
960{
961 struct ctx_switch_entry *field;
962 int T;
963
2c9b238e 964 trace_assign_type(field, iter->ent);
f633cef0
SR
965
966 if (!S)
b0f56f1a 967 S = task_state_char(field->prev_state);
f633cef0 968 T = task_state_char(field->next_state);
2c9b238e 969 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
6c1a99af
LJ
970 field->prev_pid,
971 field->prev_prio,
972 S,
973 field->next_cpu,
974 field->next_pid,
975 field->next_prio,
976 T))
f633cef0
SR
977 return TRACE_TYPE_PARTIAL_LINE;
978
d9793bd8 979 return TRACE_TYPE_HANDLED;
f633cef0
SR
980}
981
a9a57763
SR
982static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
983 struct trace_event *event)
f633cef0 984{
2c9b238e 985 return trace_ctxwake_raw(iter, 0);
f633cef0
SR
986}
987
a9a57763
SR
988static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
989 struct trace_event *event)
f633cef0 990{
2c9b238e 991 return trace_ctxwake_raw(iter, '+');
f633cef0
SR
992}
993
994
2c9b238e 995static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
f633cef0
SR
996{
997 struct ctx_switch_entry *field;
2c9b238e 998 struct trace_seq *s = &iter->seq;
f633cef0
SR
999 int T;
1000
2c9b238e 1001 trace_assign_type(field, iter->ent);
f633cef0
SR
1002
1003 if (!S)
b0f56f1a 1004 S = task_state_char(field->prev_state);
f633cef0
SR
1005 T = task_state_char(field->next_state);
1006
1007 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1008 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1009 SEQ_PUT_HEX_FIELD_RET(s, S);
1010 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1011 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1012 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1013 SEQ_PUT_HEX_FIELD_RET(s, T);
1014
d9793bd8 1015 return TRACE_TYPE_HANDLED;
f633cef0
SR
1016}
1017
a9a57763
SR
1018static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1019 struct trace_event *event)
f633cef0 1020{
2c9b238e 1021 return trace_ctxwake_hex(iter, 0);
f633cef0
SR
1022}
1023
a9a57763
SR
1024static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1025 struct trace_event *event)
f633cef0 1026{
2c9b238e 1027 return trace_ctxwake_hex(iter, '+');
f633cef0
SR
1028}
1029
ae7462b4 1030static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
a9a57763 1031 int flags, struct trace_event *event)
f633cef0
SR
1032{
1033 struct ctx_switch_entry *field;
2c9b238e 1034 struct trace_seq *s = &iter->seq;
f633cef0 1035
2c9b238e 1036 trace_assign_type(field, iter->ent);
f633cef0
SR
1037
1038 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1039 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1040 SEQ_PUT_FIELD_RET(s, field->prev_state);
1041 SEQ_PUT_FIELD_RET(s, field->next_pid);
1042 SEQ_PUT_FIELD_RET(s, field->next_prio);
1043 SEQ_PUT_FIELD_RET(s, field->next_state);
1044
d9793bd8 1045 return TRACE_TYPE_HANDLED;
f633cef0
SR
1046}
1047
a9a57763 1048static struct trace_event_functions trace_ctx_funcs = {
f633cef0 1049 .trace = trace_ctx_print,
f633cef0
SR
1050 .raw = trace_ctx_raw,
1051 .hex = trace_ctx_hex,
1052 .binary = trace_ctxwake_bin,
1053};
1054
a9a57763
SR
1055static struct trace_event trace_ctx_event = {
1056 .type = TRACE_CTX,
1057 .funcs = &trace_ctx_funcs,
1058};
1059
1060static struct trace_event_functions trace_wake_funcs = {
f633cef0 1061 .trace = trace_wake_print,
f633cef0
SR
1062 .raw = trace_wake_raw,
1063 .hex = trace_wake_hex,
1064 .binary = trace_ctxwake_bin,
1065};
1066
a9a57763
SR
1067static struct trace_event trace_wake_event = {
1068 .type = TRACE_WAKE,
1069 .funcs = &trace_wake_funcs,
1070};
1071
f633cef0 1072/* TRACE_SPECIAL */
ae7462b4 1073static enum print_line_t trace_special_print(struct trace_iterator *iter,
a9a57763 1074 int flags, struct trace_event *event)
f633cef0
SR
1075{
1076 struct special_entry *field;
1077
2c9b238e 1078 trace_assign_type(field, iter->ent);
f633cef0 1079
2c9b238e 1080 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
6c1a99af
LJ
1081 field->arg1,
1082 field->arg2,
1083 field->arg3))
f633cef0
SR
1084 return TRACE_TYPE_PARTIAL_LINE;
1085
d9793bd8 1086 return TRACE_TYPE_HANDLED;
f633cef0
SR
1087}
1088
ae7462b4 1089static enum print_line_t trace_special_hex(struct trace_iterator *iter,
a9a57763 1090 int flags, struct trace_event *event)
f633cef0
SR
1091{
1092 struct special_entry *field;
2c9b238e 1093 struct trace_seq *s = &iter->seq;
f633cef0 1094
2c9b238e 1095 trace_assign_type(field, iter->ent);
f633cef0
SR
1096
1097 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1098 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1099 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1100
d9793bd8 1101 return TRACE_TYPE_HANDLED;
f633cef0
SR
1102}
1103
ae7462b4 1104static enum print_line_t trace_special_bin(struct trace_iterator *iter,
a9a57763 1105 int flags, struct trace_event *event)
f633cef0
SR
1106{
1107 struct special_entry *field;
2c9b238e 1108 struct trace_seq *s = &iter->seq;
f633cef0 1109
2c9b238e 1110 trace_assign_type(field, iter->ent);
f633cef0
SR
1111
1112 SEQ_PUT_FIELD_RET(s, field->arg1);
1113 SEQ_PUT_FIELD_RET(s, field->arg2);
1114 SEQ_PUT_FIELD_RET(s, field->arg3);
1115
d9793bd8 1116 return TRACE_TYPE_HANDLED;
f633cef0
SR
1117}
1118
a9a57763 1119static struct trace_event_functions trace_special_funcs = {
f633cef0 1120 .trace = trace_special_print,
f633cef0
SR
1121 .raw = trace_special_print,
1122 .hex = trace_special_hex,
1123 .binary = trace_special_bin,
1124};
1125
a9a57763
SR
1126static struct trace_event trace_special_event = {
1127 .type = TRACE_SPECIAL,
1128 .funcs = &trace_special_funcs,
1129};
1130
f633cef0
SR
1131/* TRACE_STACK */
1132
ae7462b4 1133static enum print_line_t trace_stack_print(struct trace_iterator *iter,
a9a57763 1134 int flags, struct trace_event *event)
f633cef0
SR
1135{
1136 struct stack_entry *field;
2c9b238e 1137 struct trace_seq *s = &iter->seq;
f633cef0
SR
1138 int i;
1139
2c9b238e 1140 trace_assign_type(field, iter->ent);
f633cef0 1141
563af16c 1142 if (!trace_seq_puts(s, "<stack trace>\n"))
f11b3f4e 1143 goto partial;
f633cef0 1144 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
f11b3f4e 1145 if (!field->caller[i] || (field->caller[i] == ULONG_MAX))
1ec7c484 1146 break;
f11b3f4e 1147 if (!trace_seq_puts(s, " => "))
1148 goto partial;
f633cef0 1149
f11b3f4e 1150 if (!seq_print_ip_sym(s, field->caller[i], flags))
1151 goto partial;
6c1a99af 1152 if (!trace_seq_puts(s, "\n"))
f633cef0
SR
1153 goto partial;
1154 }
1155
d9793bd8 1156 return TRACE_TYPE_HANDLED;
f633cef0
SR
1157
1158 partial:
1159 return TRACE_TYPE_PARTIAL_LINE;
1160}
1161
a9a57763 1162static struct trace_event_functions trace_stack_funcs = {
f633cef0 1163 .trace = trace_stack_print,
f633cef0
SR
1164 .raw = trace_special_print,
1165 .hex = trace_special_hex,
1166 .binary = trace_special_bin,
1167};
1168
a9a57763
SR
1169static struct trace_event trace_stack_event = {
1170 .type = TRACE_STACK,
1171 .funcs = &trace_stack_funcs,
1172};
1173
f633cef0 1174/* TRACE_USER_STACK */
ae7462b4 1175static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
a9a57763 1176 int flags, struct trace_event *event)
f633cef0
SR
1177{
1178 struct userstack_entry *field;
2c9b238e 1179 struct trace_seq *s = &iter->seq;
f633cef0 1180
2c9b238e 1181 trace_assign_type(field, iter->ent);
f633cef0 1182
563af16c 1183 if (!trace_seq_puts(s, "<user stack trace>\n"))
f633cef0
SR
1184 goto partial;
1185
048dc50c 1186 if (!seq_print_userip_objs(field, s, flags))
f633cef0
SR
1187 goto partial;
1188
d9793bd8 1189 return TRACE_TYPE_HANDLED;
f633cef0
SR
1190
1191 partial:
1192 return TRACE_TYPE_PARTIAL_LINE;
1193}
1194
a9a57763 1195static struct trace_event_functions trace_user_stack_funcs = {
f633cef0 1196 .trace = trace_user_stack_print,
f633cef0
SR
1197 .raw = trace_special_print,
1198 .hex = trace_special_hex,
1199 .binary = trace_special_bin,
1200};
1201
a9a57763
SR
1202static struct trace_event trace_user_stack_event = {
1203 .type = TRACE_USER_STACK,
1204 .funcs = &trace_user_stack_funcs,
1205};
1206
48ead020 1207/* TRACE_BPRINT */
1427cdf0 1208static enum print_line_t
a9a57763
SR
1209trace_bprint_print(struct trace_iterator *iter, int flags,
1210 struct trace_event *event)
1427cdf0
LJ
1211{
1212 struct trace_entry *entry = iter->ent;
1213 struct trace_seq *s = &iter->seq;
48ead020 1214 struct bprint_entry *field;
1427cdf0
LJ
1215
1216 trace_assign_type(field, entry);
1217
1218 if (!seq_print_ip_sym(s, field->ip, flags))
1219 goto partial;
1220
1221 if (!trace_seq_puts(s, ": "))
1222 goto partial;
1223
1224 if (!trace_seq_bprintf(s, field->fmt, field->buf))
1225 goto partial;
1226
1227 return TRACE_TYPE_HANDLED;
1228
1229 partial:
1230 return TRACE_TYPE_PARTIAL_LINE;
1231}
1232
769b0441 1233
48ead020 1234static enum print_line_t
a9a57763
SR
1235trace_bprint_raw(struct trace_iterator *iter, int flags,
1236 struct trace_event *event)
1427cdf0 1237{
48ead020 1238 struct bprint_entry *field;
1427cdf0 1239 struct trace_seq *s = &iter->seq;
1427cdf0 1240
769b0441 1241 trace_assign_type(field, iter->ent);
1427cdf0
LJ
1242
1243 if (!trace_seq_printf(s, ": %lx : ", field->ip))
1244 goto partial;
1245
1246 if (!trace_seq_bprintf(s, field->fmt, field->buf))
1247 goto partial;
1248
1249 return TRACE_TYPE_HANDLED;
1250
1251 partial:
1252 return TRACE_TYPE_PARTIAL_LINE;
1253}
1254
a9a57763
SR
1255static struct trace_event_functions trace_bprint_funcs = {
1256 .trace = trace_bprint_print,
1257 .raw = trace_bprint_raw,
1258};
769b0441 1259
48ead020
FW
1260static struct trace_event trace_bprint_event = {
1261 .type = TRACE_BPRINT,
a9a57763 1262 .funcs = &trace_bprint_funcs,
48ead020
FW
1263};
1264
1265/* TRACE_PRINT */
1266static enum print_line_t trace_print_print(struct trace_iterator *iter,
a9a57763 1267 int flags, struct trace_event *event)
48ead020
FW
1268{
1269 struct print_entry *field;
1270 struct trace_seq *s = &iter->seq;
1271
1272 trace_assign_type(field, iter->ent);
1273
1274 if (!seq_print_ip_sym(s, field->ip, flags))
1275 goto partial;
1276
1277 if (!trace_seq_printf(s, ": %s", field->buf))
1278 goto partial;
1279
1280 return TRACE_TYPE_HANDLED;
1281
1282 partial:
1283 return TRACE_TYPE_PARTIAL_LINE;
1284}
1285
a9a57763
SR
1286static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1287 struct trace_event *event)
48ead020
FW
1288{
1289 struct print_entry *field;
1290
1291 trace_assign_type(field, iter->ent);
1292
1293 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
1294 goto partial;
1295
1296 return TRACE_TYPE_HANDLED;
1297
1298 partial:
1299 return TRACE_TYPE_PARTIAL_LINE;
1300}
1301
a9a57763 1302static struct trace_event_functions trace_print_funcs = {
769b0441
FW
1303 .trace = trace_print_print,
1304 .raw = trace_print_raw,
1427cdf0
LJ
1305};
1306
a9a57763
SR
1307static struct trace_event trace_print_event = {
1308 .type = TRACE_PRINT,
1309 .funcs = &trace_print_funcs,
1310};
1311
48ead020 1312
f633cef0
SR
1313static struct trace_event *events[] __initdata = {
1314 &trace_fn_event,
1315 &trace_ctx_event,
1316 &trace_wake_event,
1317 &trace_special_event,
1318 &trace_stack_event,
1319 &trace_user_stack_event,
48ead020 1320 &trace_bprint_event,
f633cef0
SR
1321 &trace_print_event,
1322 NULL
1323};
1324
1325__init static int init_events(void)
1326{
1327 struct trace_event *event;
1328 int i, ret;
1329
1330 for (i = 0; events[i]; i++) {
1331 event = events[i];
1332
1333 ret = register_ftrace_event(event);
1334 if (!ret) {
1335 printk(KERN_WARNING "event %d failed to register\n",
1336 event->type);
1337 WARN_ON_ONCE(1);
1338 }
1339 }
1340
1341 return 0;
1342}
1343device_initcall(init_events);