tracing: Remove special traces
[linux-2.6.git] / kernel / trace / trace_output.c
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
17 DECLARE_RWSEM(trace_event_mutex);
18
19 DEFINE_PER_CPU(struct trace_seq, ftrace_event_seq);
20 EXPORT_PER_CPU_SYMBOL(ftrace_event_seq);
21
22 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
23
24 static int next_event_type = __TRACE_LAST_TYPE + 1;
25
26 int trace_print_seq(struct seq_file *m, struct trace_seq *s)
27 {
28         int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
29         int ret;
30
31         ret = seq_write(m, s->buffer, len);
32
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);
39
40         return ret;
41 }
42
43 enum 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
59 enum 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
75 /**
76  * trace_seq_printf - sequence printing of trace information
77  * @s: trace sequence descriptor
78  * @fmt: printf format string
79  *
80  * It returns 0 if the trace oversizes the buffer's free
81  * space, 1 otherwise.
82  *
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  */
89 int
90 trace_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
96         if (s->full || !len)
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 */
104         if (ret >= len) {
105                 s->full = 1;
106                 return 0;
107         }
108
109         s->len += ret;
110
111         return 1;
112 }
113 EXPORT_SYMBOL_GPL(trace_seq_printf);
114
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  */
126 int
127 trace_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
132         if (s->full || !len)
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 */
138         if (ret >= len) {
139                 s->full = 1;
140                 return 0;
141         }
142
143         s->len += ret;
144
145         return len;
146 }
147 EXPORT_SYMBOL_GPL(trace_seq_vprintf);
148
149 int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
150 {
151         int len = (PAGE_SIZE - 1) - s->len;
152         int ret;
153
154         if (s->full || !len)
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 */
160         if (ret >= len) {
161                 s->full = 1;
162                 return 0;
163         }
164
165         s->len += ret;
166
167         return len;
168 }
169
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  */
180 int trace_seq_puts(struct trace_seq *s, const char *str)
181 {
182         int len = strlen(str);
183
184         if (s->full)
185                 return 0;
186
187         if (len > ((PAGE_SIZE - 1) - s->len)) {
188                 s->full = 1;
189                 return 0;
190         }
191
192         memcpy(s->buffer + s->len, str, len);
193         s->len += len;
194
195         return len;
196 }
197
198 int trace_seq_putc(struct trace_seq *s, unsigned char c)
199 {
200         if (s->full)
201                 return 0;
202
203         if (s->len >= (PAGE_SIZE - 1)) {
204                 s->full = 1;
205                 return 0;
206         }
207
208         s->buffer[s->len++] = c;
209
210         return 1;
211 }
212 EXPORT_SYMBOL(trace_seq_putc);
213
214 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
215 {
216         if (s->full)
217                 return 0;
218
219         if (len > ((PAGE_SIZE - 1) - s->len)) {
220                 s->full = 1;
221                 return 0;
222         }
223
224         memcpy(s->buffer + s->len, mem, len);
225         s->len += len;
226
227         return len;
228 }
229
230 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
231 {
232         unsigned char hex[HEX_CHARS];
233         const unsigned char *data = mem;
234         int i, j;
235
236         if (s->full)
237                 return 0;
238
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
252 void *trace_seq_reserve(struct trace_seq *s, size_t len)
253 {
254         void *ret;
255
256         if (s->full)
257                 return NULL;
258
259         if (len > ((PAGE_SIZE - 1) - s->len)) {
260                 s->full = 1;
261                 return NULL;
262         }
263
264         ret = s->buffer + s->len;
265         s->len += len;
266
267         return ret;
268 }
269
270 int trace_seq_path(struct trace_seq *s, struct path *path)
271 {
272         unsigned char *p;
273
274         if (s->full)
275                 return 0;
276
277         if (s->len >= (PAGE_SIZE - 1)) {
278                 s->full = 1;
279                 return 0;
280         }
281
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
294         s->full = 1;
295         return 0;
296 }
297
298 const char *
299 ftrace_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;
305         const char *ret = p->buffer + p->len;
306         int i;
307
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
330         return ret;
331 }
332 EXPORT_SYMBOL(ftrace_print_flags_seq);
333
334 const char *
335 ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
336                          const struct trace_print_flags *symbol_array)
337 {
338         int i;
339         const char *ret = p->buffer + p->len;
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
355         return ret;
356 }
357 EXPORT_SYMBOL(ftrace_print_symbols_seq);
358
359 const char *
360 ftrace_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 }
372 EXPORT_SYMBOL(ftrace_print_hex_seq);
373
374 #ifdef CONFIG_KRETPROBES
375 static 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
385 static inline const char *kretprobed(const char *name)
386 {
387         return name;
388 }
389 #endif /* CONFIG_KRETPROBES */
390
391 static int
392 seq_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
407 static int
408 seq_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
429 int 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
436         if (s->full)
437                 return 0;
438
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
461 int
462 seq_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();
476                 task = find_task_by_vpid(entry->tgid);
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;
487                 if (ret)
488                         ret = trace_seq_puts(s, " => ");
489                 if (!ip) {
490                         if (ret)
491                                 ret = trace_seq_puts(s, "??");
492                         if (ret)
493                                 ret = trace_seq_puts(s, "\n");
494                         continue;
495                 }
496                 if (!ret)
497                         break;
498                 if (ret)
499                         ret = seq_print_user_ip(s, mm, ip, sym_flags);
500                 ret = trace_seq_puts(s, "\n");
501         }
502
503         if (mm)
504                 mmput(mm);
505         return ret;
506 }
507
508 int
509 seq_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
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  */
537 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
538 {
539         int hardirq, softirq;
540         int ret;
541
542         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
543         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
544
545         if (!trace_seq_printf(s, "%c%c%c",
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;
554
555         if (entry->preempt_count)
556                 ret = trace_seq_printf(s, "%x", entry->preempt_count);
557         else
558                 ret = trace_seq_putc(s, '.');
559
560         if (!ret)
561                 return 0;
562
563         if (entry->lock_depth < 0)
564                 return trace_seq_putc(s, '.');
565
566         return trace_seq_printf(s, "%d", entry->lock_depth);
567 }
568
569 static int
570 lat_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
583 static unsigned long preempt_mark_thresh = 100;
584
585 static int
586 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
587                     unsigned long rel_usecs)
588 {
589         return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
590                                 rel_usecs > preempt_mark_thresh ? '!' :
591                                   rel_usecs > 1 ? '+' : ' ');
592 }
593
594 int trace_print_context(struct trace_iterator *iter)
595 {
596         struct trace_seq *s = &iter->seq;
597         struct trace_entry *entry = iter->ent;
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;
601         char comm[TASK_COMM_LEN];
602
603         trace_find_cmdline(entry->pid, comm);
604
605         return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
606                                 comm, entry->pid, iter->cpu, secs, usec_rem);
607 }
608
609 int trace_print_lat_context(struct trace_iterator *iter)
610 {
611         u64 next_ts;
612         int ret;
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) {
626                 char comm[TASK_COMM_LEN];
627
628                 trace_find_cmdline(entry->pid, comm);
629
630                 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08llx]"
631                                        " %ld.%03ldms (+%ld.%03ldms): ", comm,
632                                        entry->pid, iter->cpu, entry->flags,
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);
639         } else {
640                 ret = lat_print_generic(s, entry, iter->cpu);
641                 if (ret)
642                         ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
643         }
644
645         return ret;
646 }
647
648 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
649
650 static 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
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
662  * Called with trace_event_read_lock() held.
663  */
664 struct 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
672         hlist_for_each_entry(event, n, &event_hash[key], node) {
673                 if (event->type == type)
674                         return event;
675         }
676
677         return NULL;
678 }
679
680 static LIST_HEAD(ftrace_event_list);
681
682 static 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
710 void trace_event_read_lock(void)
711 {
712         down_read(&trace_event_mutex);
713 }
714
715 void trace_event_read_unlock(void)
716 {
717         up_read(&trace_event_mutex);
718 }
719
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  */
735 int register_ftrace_event(struct trace_event *event)
736 {
737         unsigned key;
738         int ret = 0;
739
740         down_write(&trace_event_mutex);
741
742         if (WARN_ON(!event))
743                 goto out;
744
745         if (WARN_ON(!event->funcs))
746                 goto out;
747
748         INIT_LIST_HEAD(&event->list);
749
750         if (!event->type) {
751                 struct list_head *list = NULL;
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) {
771                 printk(KERN_WARNING "Need to add type to trace.h\n");
772                 WARN_ON(1);
773                 goto out;
774         } else {
775                 /* Is this event already used */
776                 if (ftrace_find_event(event->type))
777                         goto out;
778         }
779
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;
788
789         key = event->type & (EVENT_HASHSIZE - 1);
790
791         hlist_add_head(&event->node, &event_hash[key]);
792
793         ret = event->type;
794  out:
795         up_write(&trace_event_mutex);
796
797         return ret;
798 }
799 EXPORT_SYMBOL_GPL(register_ftrace_event);
800
801 /*
802  * Used by module code with the trace_event_mutex held for write.
803  */
804 int __unregister_ftrace_event(struct trace_event *event)
805 {
806         hlist_del(&event->node);
807         list_del(&event->list);
808         return 0;
809 }
810
811 /**
812  * unregister_ftrace_event - remove a no longer used event
813  * @event: the event to remove
814  */
815 int unregister_ftrace_event(struct trace_event *event)
816 {
817         down_write(&trace_event_mutex);
818         __unregister_ftrace_event(event);
819         up_write(&trace_event_mutex);
820
821         return 0;
822 }
823 EXPORT_SYMBOL_GPL(unregister_ftrace_event);
824
825 /*
826  * Standard events
827  */
828
829 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
830                                   struct trace_event *event)
831 {
832         return TRACE_TYPE_HANDLED;
833 }
834
835 /* TRACE_FN */
836 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
837                                         struct trace_event *event)
838 {
839         struct ftrace_entry *field;
840         struct trace_seq *s = &iter->seq;
841
842         trace_assign_type(field, iter->ent);
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
858         return TRACE_TYPE_HANDLED;
859
860  partial:
861         return TRACE_TYPE_PARTIAL_LINE;
862 }
863
864 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
865                                       struct trace_event *event)
866 {
867         struct ftrace_entry *field;
868
869         trace_assign_type(field, iter->ent);
870
871         if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
872                               field->ip,
873                               field->parent_ip))
874                 return TRACE_TYPE_PARTIAL_LINE;
875
876         return TRACE_TYPE_HANDLED;
877 }
878
879 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
880                                       struct trace_event *event)
881 {
882         struct ftrace_entry *field;
883         struct trace_seq *s = &iter->seq;
884
885         trace_assign_type(field, iter->ent);
886
887         SEQ_PUT_HEX_FIELD_RET(s, field->ip);
888         SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
889
890         return TRACE_TYPE_HANDLED;
891 }
892
893 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
894                                       struct trace_event *event)
895 {
896         struct ftrace_entry *field;
897         struct trace_seq *s = &iter->seq;
898
899         trace_assign_type(field, iter->ent);
900
901         SEQ_PUT_FIELD_RET(s, field->ip);
902         SEQ_PUT_FIELD_RET(s, field->parent_ip);
903
904         return TRACE_TYPE_HANDLED;
905 }
906
907 static struct trace_event_functions trace_fn_funcs = {
908         .trace          = trace_fn_trace,
909         .raw            = trace_fn_raw,
910         .hex            = trace_fn_hex,
911         .binary         = trace_fn_bin,
912 };
913
914 static struct trace_event trace_fn_event = {
915         .type           = TRACE_FN,
916         .funcs          = &trace_fn_funcs,
917 };
918
919 /* TRACE_CTX an TRACE_WAKE */
920 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
921                                              char *delim)
922 {
923         struct ctx_switch_entry *field;
924         char comm[TASK_COMM_LEN];
925         int S, T;
926
927
928         trace_assign_type(field, iter->ent);
929
930         T = task_state_char(field->next_state);
931         S = task_state_char(field->prev_state);
932         trace_find_cmdline(field->next_pid, comm);
933         if (!trace_seq_printf(&iter->seq,
934                               " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
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))
942                 return TRACE_TYPE_PARTIAL_LINE;
943
944         return TRACE_TYPE_HANDLED;
945 }
946
947 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
948                                          struct trace_event *event)
949 {
950         return trace_ctxwake_print(iter, "==>");
951 }
952
953 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
954                                           int flags, struct trace_event *event)
955 {
956         return trace_ctxwake_print(iter, "  +");
957 }
958
959 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
960 {
961         struct ctx_switch_entry *field;
962         int T;
963
964         trace_assign_type(field, iter->ent);
965
966         if (!S)
967                 S = task_state_char(field->prev_state);
968         T = task_state_char(field->next_state);
969         if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
970                               field->prev_pid,
971                               field->prev_prio,
972                               S,
973                               field->next_cpu,
974                               field->next_pid,
975                               field->next_prio,
976                               T))
977                 return TRACE_TYPE_PARTIAL_LINE;
978
979         return TRACE_TYPE_HANDLED;
980 }
981
982 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
983                                        struct trace_event *event)
984 {
985         return trace_ctxwake_raw(iter, 0);
986 }
987
988 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
989                                         struct trace_event *event)
990 {
991         return trace_ctxwake_raw(iter, '+');
992 }
993
994
995 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
996 {
997         struct ctx_switch_entry *field;
998         struct trace_seq *s = &iter->seq;
999         int T;
1000
1001         trace_assign_type(field, iter->ent);
1002
1003         if (!S)
1004                 S = task_state_char(field->prev_state);
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
1015         return TRACE_TYPE_HANDLED;
1016 }
1017
1018 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1019                                        struct trace_event *event)
1020 {
1021         return trace_ctxwake_hex(iter, 0);
1022 }
1023
1024 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1025                                         struct trace_event *event)
1026 {
1027         return trace_ctxwake_hex(iter, '+');
1028 }
1029
1030 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1031                                            int flags, struct trace_event *event)
1032 {
1033         struct ctx_switch_entry *field;
1034         struct trace_seq *s = &iter->seq;
1035
1036         trace_assign_type(field, iter->ent);
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
1045         return TRACE_TYPE_HANDLED;
1046 }
1047
1048 static struct trace_event_functions trace_ctx_funcs = {
1049         .trace          = trace_ctx_print,
1050         .raw            = trace_ctx_raw,
1051         .hex            = trace_ctx_hex,
1052         .binary         = trace_ctxwake_bin,
1053 };
1054
1055 static struct trace_event trace_ctx_event = {
1056         .type           = TRACE_CTX,
1057         .funcs          = &trace_ctx_funcs,
1058 };
1059
1060 static struct trace_event_functions trace_wake_funcs = {
1061         .trace          = trace_wake_print,
1062         .raw            = trace_wake_raw,
1063         .hex            = trace_wake_hex,
1064         .binary         = trace_ctxwake_bin,
1065 };
1066
1067 static struct trace_event trace_wake_event = {
1068         .type           = TRACE_WAKE,
1069         .funcs          = &trace_wake_funcs,
1070 };
1071
1072 /* TRACE_STACK */
1073
1074 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1075                                            int flags, struct trace_event *event)
1076 {
1077         struct stack_entry *field;
1078         struct trace_seq *s = &iter->seq;
1079         int i;
1080
1081         trace_assign_type(field, iter->ent);
1082
1083         if (!trace_seq_puts(s, "<stack trace>\n"))
1084                 goto partial;
1085         for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1086                 if (!field->caller[i] || (field->caller[i] == ULONG_MAX))
1087                         break;
1088                 if (!trace_seq_puts(s, " => "))
1089                         goto partial;
1090
1091                 if (!seq_print_ip_sym(s, field->caller[i], flags))
1092                         goto partial;
1093                 if (!trace_seq_puts(s, "\n"))
1094                         goto partial;
1095         }
1096
1097         return TRACE_TYPE_HANDLED;
1098
1099  partial:
1100         return TRACE_TYPE_PARTIAL_LINE;
1101 }
1102
1103 static struct trace_event_functions trace_stack_funcs = {
1104         .trace          = trace_stack_print,
1105 };
1106
1107 static struct trace_event trace_stack_event = {
1108         .type           = TRACE_STACK,
1109         .funcs          = &trace_stack_funcs,
1110 };
1111
1112 /* TRACE_USER_STACK */
1113 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1114                                                 int flags, struct trace_event *event)
1115 {
1116         struct userstack_entry *field;
1117         struct trace_seq *s = &iter->seq;
1118
1119         trace_assign_type(field, iter->ent);
1120
1121         if (!trace_seq_puts(s, "<user stack trace>\n"))
1122                 goto partial;
1123
1124         if (!seq_print_userip_objs(field, s, flags))
1125                 goto partial;
1126
1127         return TRACE_TYPE_HANDLED;
1128
1129  partial:
1130         return TRACE_TYPE_PARTIAL_LINE;
1131 }
1132
1133 static struct trace_event_functions trace_user_stack_funcs = {
1134         .trace          = trace_user_stack_print,
1135 };
1136
1137 static struct trace_event trace_user_stack_event = {
1138         .type           = TRACE_USER_STACK,
1139         .funcs          = &trace_user_stack_funcs,
1140 };
1141
1142 /* TRACE_BPRINT */
1143 static enum print_line_t
1144 trace_bprint_print(struct trace_iterator *iter, int flags,
1145                    struct trace_event *event)
1146 {
1147         struct trace_entry *entry = iter->ent;
1148         struct trace_seq *s = &iter->seq;
1149         struct bprint_entry *field;
1150
1151         trace_assign_type(field, entry);
1152
1153         if (!seq_print_ip_sym(s, field->ip, flags))
1154                 goto partial;
1155
1156         if (!trace_seq_puts(s, ": "))
1157                 goto partial;
1158
1159         if (!trace_seq_bprintf(s, field->fmt, field->buf))
1160                 goto partial;
1161
1162         return TRACE_TYPE_HANDLED;
1163
1164  partial:
1165         return TRACE_TYPE_PARTIAL_LINE;
1166 }
1167
1168
1169 static enum print_line_t
1170 trace_bprint_raw(struct trace_iterator *iter, int flags,
1171                  struct trace_event *event)
1172 {
1173         struct bprint_entry *field;
1174         struct trace_seq *s = &iter->seq;
1175
1176         trace_assign_type(field, iter->ent);
1177
1178         if (!trace_seq_printf(s, ": %lx : ", field->ip))
1179                 goto partial;
1180
1181         if (!trace_seq_bprintf(s, field->fmt, field->buf))
1182                 goto partial;
1183
1184         return TRACE_TYPE_HANDLED;
1185
1186  partial:
1187         return TRACE_TYPE_PARTIAL_LINE;
1188 }
1189
1190 static struct trace_event_functions trace_bprint_funcs = {
1191         .trace          = trace_bprint_print,
1192         .raw            = trace_bprint_raw,
1193 };
1194
1195 static struct trace_event trace_bprint_event = {
1196         .type           = TRACE_BPRINT,
1197         .funcs          = &trace_bprint_funcs,
1198 };
1199
1200 /* TRACE_PRINT */
1201 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1202                                            int flags, struct trace_event *event)
1203 {
1204         struct print_entry *field;
1205         struct trace_seq *s = &iter->seq;
1206
1207         trace_assign_type(field, iter->ent);
1208
1209         if (!seq_print_ip_sym(s, field->ip, flags))
1210                 goto partial;
1211
1212         if (!trace_seq_printf(s, ": %s", field->buf))
1213                 goto partial;
1214
1215         return TRACE_TYPE_HANDLED;
1216
1217  partial:
1218         return TRACE_TYPE_PARTIAL_LINE;
1219 }
1220
1221 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1222                                          struct trace_event *event)
1223 {
1224         struct print_entry *field;
1225
1226         trace_assign_type(field, iter->ent);
1227
1228         if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
1229                 goto partial;
1230
1231         return TRACE_TYPE_HANDLED;
1232
1233  partial:
1234         return TRACE_TYPE_PARTIAL_LINE;
1235 }
1236
1237 static struct trace_event_functions trace_print_funcs = {
1238         .trace          = trace_print_print,
1239         .raw            = trace_print_raw,
1240 };
1241
1242 static struct trace_event trace_print_event = {
1243         .type           = TRACE_PRINT,
1244         .funcs          = &trace_print_funcs,
1245 };
1246
1247
1248 static struct trace_event *events[] __initdata = {
1249         &trace_fn_event,
1250         &trace_ctx_event,
1251         &trace_wake_event,
1252         &trace_stack_event,
1253         &trace_user_stack_event,
1254         &trace_bprint_event,
1255         &trace_print_event,
1256         NULL
1257 };
1258
1259 __init static int init_events(void)
1260 {
1261         struct trace_event *event;
1262         int i, ret;
1263
1264         for (i = 0; events[i]; i++) {
1265                 event = events[i];
1266
1267                 ret = register_ftrace_event(event);
1268                 if (!ret) {
1269                         printk(KERN_WARNING "event %d failed to register\n",
1270                                event->type);
1271                         WARN_ON_ONCE(1);
1272                 }
1273         }
1274
1275         return 0;
1276 }
1277 device_initcall(init_events);