]> Pileus Git - ~andy/linux/blob - kernel/trace/trace_output.c
7c4a0ca650b565004818d5ad08fc969711725eee
[~andy/linux] / 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
213 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
214 {
215         if (s->full)
216                 return 0;
217
218         if (len > ((PAGE_SIZE - 1) - s->len)) {
219                 s->full = 1;
220                 return 0;
221         }
222
223         memcpy(s->buffer + s->len, mem, len);
224         s->len += len;
225
226         return len;
227 }
228
229 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
230 {
231         unsigned char hex[HEX_CHARS];
232         const unsigned char *data = mem;
233         int i, j;
234
235         if (s->full)
236                 return 0;
237
238 #ifdef __BIG_ENDIAN
239         for (i = 0, j = 0; i < len; i++) {
240 #else
241         for (i = len-1, j = 0; i >= 0; i--) {
242 #endif
243                 hex[j++] = hex_asc_hi(data[i]);
244                 hex[j++] = hex_asc_lo(data[i]);
245         }
246         hex[j++] = ' ';
247
248         return trace_seq_putmem(s, hex, j);
249 }
250
251 void *trace_seq_reserve(struct trace_seq *s, size_t len)
252 {
253         void *ret;
254
255         if (s->full)
256                 return 0;
257
258         if (len > ((PAGE_SIZE - 1) - s->len)) {
259                 s->full = 1;
260                 return NULL;
261         }
262
263         ret = s->buffer + s->len;
264         s->len += len;
265
266         return ret;
267 }
268
269 int trace_seq_path(struct trace_seq *s, struct path *path)
270 {
271         unsigned char *p;
272
273         if (s->full)
274                 return 0;
275
276         if (s->len >= (PAGE_SIZE - 1)) {
277                 s->full = 1;
278                 return 0;
279         }
280
281         p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
282         if (!IS_ERR(p)) {
283                 p = mangle_path(s->buffer + s->len, p, "\n");
284                 if (p) {
285                         s->len = p - s->buffer;
286                         return 1;
287                 }
288         } else {
289                 s->buffer[s->len++] = '?';
290                 return 1;
291         }
292
293         s->full = 1;
294         return 0;
295 }
296
297 const char *
298 ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
299                        unsigned long flags,
300                        const struct trace_print_flags *flag_array)
301 {
302         unsigned long mask;
303         const char *str;
304         const char *ret = p->buffer + p->len;
305         int i;
306
307         for (i = 0;  flag_array[i].name && flags; i++) {
308
309                 mask = flag_array[i].mask;
310                 if ((flags & mask) != mask)
311                         continue;
312
313                 str = flag_array[i].name;
314                 flags &= ~mask;
315                 if (p->len && delim)
316                         trace_seq_puts(p, delim);
317                 trace_seq_puts(p, str);
318         }
319
320         /* check for left over flags */
321         if (flags) {
322                 if (p->len && delim)
323                         trace_seq_puts(p, delim);
324                 trace_seq_printf(p, "0x%lx", flags);
325         }
326
327         trace_seq_putc(p, 0);
328
329         return ret;
330 }
331 EXPORT_SYMBOL(ftrace_print_flags_seq);
332
333 const char *
334 ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
335                          const struct trace_print_flags *symbol_array)
336 {
337         int i;
338         const char *ret = p->buffer + p->len;
339
340         for (i = 0;  symbol_array[i].name; i++) {
341
342                 if (val != symbol_array[i].mask)
343                         continue;
344
345                 trace_seq_puts(p, symbol_array[i].name);
346                 break;
347         }
348
349         if (!p->len)
350                 trace_seq_printf(p, "0x%lx", val);
351                 
352         trace_seq_putc(p, 0);
353
354         return ret;
355 }
356 EXPORT_SYMBOL(ftrace_print_symbols_seq);
357
358 const char *
359 ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
360 {
361         int i;
362         const char *ret = p->buffer + p->len;
363
364         for (i = 0; i < buf_len; i++)
365                 trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);
366
367         trace_seq_putc(p, 0);
368
369         return ret;
370 }
371 EXPORT_SYMBOL(ftrace_print_hex_seq);
372
373 #ifdef CONFIG_KRETPROBES
374 static inline const char *kretprobed(const char *name)
375 {
376         static const char tramp_name[] = "kretprobe_trampoline";
377         int size = sizeof(tramp_name);
378
379         if (strncmp(tramp_name, name, size) == 0)
380                 return "[unknown/kretprobe'd]";
381         return name;
382 }
383 #else
384 static inline const char *kretprobed(const char *name)
385 {
386         return name;
387 }
388 #endif /* CONFIG_KRETPROBES */
389
390 static int
391 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
392 {
393 #ifdef CONFIG_KALLSYMS
394         char str[KSYM_SYMBOL_LEN];
395         const char *name;
396
397         kallsyms_lookup(address, NULL, NULL, NULL, str);
398
399         name = kretprobed(str);
400
401         return trace_seq_printf(s, fmt, name);
402 #endif
403         return 1;
404 }
405
406 static int
407 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
408                      unsigned long address)
409 {
410 #ifdef CONFIG_KALLSYMS
411         char str[KSYM_SYMBOL_LEN];
412         const char *name;
413
414         sprint_symbol(str, address);
415         name = kretprobed(str);
416
417         return trace_seq_printf(s, fmt, name);
418 #endif
419         return 1;
420 }
421
422 #ifndef CONFIG_64BIT
423 # define IP_FMT "%08lx"
424 #else
425 # define IP_FMT "%016lx"
426 #endif
427
428 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
429                       unsigned long ip, unsigned long sym_flags)
430 {
431         struct file *file = NULL;
432         unsigned long vmstart = 0;
433         int ret = 1;
434
435         if (s->full)
436                 return 0;
437
438         if (mm) {
439                 const struct vm_area_struct *vma;
440
441                 down_read(&mm->mmap_sem);
442                 vma = find_vma(mm, ip);
443                 if (vma) {
444                         file = vma->vm_file;
445                         vmstart = vma->vm_start;
446                 }
447                 if (file) {
448                         ret = trace_seq_path(s, &file->f_path);
449                         if (ret)
450                                 ret = trace_seq_printf(s, "[+0x%lx]",
451                                                        ip - vmstart);
452                 }
453                 up_read(&mm->mmap_sem);
454         }
455         if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
456                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
457         return ret;
458 }
459
460 int
461 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
462                       unsigned long sym_flags)
463 {
464         struct mm_struct *mm = NULL;
465         int ret = 1;
466         unsigned int i;
467
468         if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
469                 struct task_struct *task;
470                 /*
471                  * we do the lookup on the thread group leader,
472                  * since individual threads might have already quit!
473                  */
474                 rcu_read_lock();
475                 task = find_task_by_vpid(entry->tgid);
476                 if (task)
477                         mm = get_task_mm(task);
478                 rcu_read_unlock();
479         }
480
481         for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
482                 unsigned long ip = entry->caller[i];
483
484                 if (ip == ULONG_MAX || !ret)
485                         break;
486                 if (ret)
487                         ret = trace_seq_puts(s, " => ");
488                 if (!ip) {
489                         if (ret)
490                                 ret = trace_seq_puts(s, "??");
491                         if (ret)
492                                 ret = trace_seq_puts(s, "\n");
493                         continue;
494                 }
495                 if (!ret)
496                         break;
497                 if (ret)
498                         ret = seq_print_user_ip(s, mm, ip, sym_flags);
499                 ret = trace_seq_puts(s, "\n");
500         }
501
502         if (mm)
503                 mmput(mm);
504         return ret;
505 }
506
507 int
508 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
509 {
510         int ret;
511
512         if (!ip)
513                 return trace_seq_printf(s, "0");
514
515         if (sym_flags & TRACE_ITER_SYM_OFFSET)
516                 ret = seq_print_sym_offset(s, "%s", ip);
517         else
518                 ret = seq_print_sym_short(s, "%s", ip);
519
520         if (!ret)
521                 return 0;
522
523         if (sym_flags & TRACE_ITER_SYM_ADDR)
524                 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
525         return ret;
526 }
527
528 /**
529  * trace_print_lat_fmt - print the irq, preempt and lockdep fields
530  * @s: trace seq struct to write to
531  * @entry: The trace entry field from the ring buffer
532  *
533  * Prints the generic fields of irqs off, in hard or softirq, preempt
534  * count and lock depth.
535  */
536 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
537 {
538         int hardirq, softirq;
539         int ret;
540
541         hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
542         softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
543
544         if (!trace_seq_printf(s, "%c%c%c",
545                               (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
546                                 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
547                                   'X' : '.',
548                               (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
549                                 'N' : '.',
550                               (hardirq && softirq) ? 'H' :
551                                 hardirq ? 'h' : softirq ? 's' : '.'))
552                 return 0;
553
554         if (entry->preempt_count)
555                 ret = trace_seq_printf(s, "%x", entry->preempt_count);
556         else
557                 ret = trace_seq_putc(s, '.');
558
559         if (!ret)
560                 return 0;
561
562         if (entry->lock_depth < 0)
563                 return trace_seq_putc(s, '.');
564
565         return trace_seq_printf(s, "%d", entry->lock_depth);
566 }
567
568 static int
569 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
570 {
571         char comm[TASK_COMM_LEN];
572
573         trace_find_cmdline(entry->pid, comm);
574
575         if (!trace_seq_printf(s, "%8.8s-%-5d %3d",
576                               comm, entry->pid, cpu))
577                 return 0;
578
579         return trace_print_lat_fmt(s, entry);
580 }
581
582 static unsigned long preempt_mark_thresh = 100;
583
584 static int
585 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
586                     unsigned long rel_usecs)
587 {
588         return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
589                                 rel_usecs > preempt_mark_thresh ? '!' :
590                                   rel_usecs > 1 ? '+' : ' ');
591 }
592
593 int trace_print_context(struct trace_iterator *iter)
594 {
595         struct trace_seq *s = &iter->seq;
596         struct trace_entry *entry = iter->ent;
597         unsigned long long t = ns2usecs(iter->ts);
598         unsigned long usec_rem = do_div(t, USEC_PER_SEC);
599         unsigned long secs = (unsigned long)t;
600         char comm[TASK_COMM_LEN];
601
602         trace_find_cmdline(entry->pid, comm);
603
604         return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
605                                 comm, entry->pid, iter->cpu, secs, usec_rem);
606 }
607
608 int trace_print_lat_context(struct trace_iterator *iter)
609 {
610         u64 next_ts;
611         int ret;
612         struct trace_seq *s = &iter->seq;
613         struct trace_entry *entry = iter->ent,
614                            *next_entry = trace_find_next_entry(iter, NULL,
615                                                                &next_ts);
616         unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
617         unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
618         unsigned long rel_usecs;
619
620         if (!next_entry)
621                 next_ts = iter->ts;
622         rel_usecs = ns2usecs(next_ts - iter->ts);
623
624         if (verbose) {
625                 char comm[TASK_COMM_LEN];
626
627                 trace_find_cmdline(entry->pid, comm);
628
629                 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08llx]"
630                                        " %ld.%03ldms (+%ld.%03ldms): ", comm,
631                                        entry->pid, iter->cpu, entry->flags,
632                                        entry->preempt_count, iter->idx,
633                                        ns2usecs(iter->ts),
634                                        abs_usecs / USEC_PER_MSEC,
635                                        abs_usecs % USEC_PER_MSEC,
636                                        rel_usecs / USEC_PER_MSEC,
637                                        rel_usecs % USEC_PER_MSEC);
638         } else {
639                 ret = lat_print_generic(s, entry, iter->cpu);
640                 if (ret)
641                         ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
642         }
643
644         return ret;
645 }
646
647 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
648
649 static int task_state_char(unsigned long state)
650 {
651         int bit = state ? __ffs(state) + 1 : 0;
652
653         return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
654 }
655
656 /**
657  * ftrace_find_event - find a registered event
658  * @type: the type of event to look for
659  *
660  * Returns an event of type @type otherwise NULL
661  * Called with trace_event_read_lock() held.
662  */
663 struct trace_event *ftrace_find_event(int type)
664 {
665         struct trace_event *event;
666         struct hlist_node *n;
667         unsigned key;
668
669         key = type & (EVENT_HASHSIZE - 1);
670
671         hlist_for_each_entry(event, n, &event_hash[key], node) {
672                 if (event->type == type)
673                         return event;
674         }
675
676         return NULL;
677 }
678
679 static LIST_HEAD(ftrace_event_list);
680
681 static int trace_search_list(struct list_head **list)
682 {
683         struct trace_event *e;
684         int last = __TRACE_LAST_TYPE;
685
686         if (list_empty(&ftrace_event_list)) {
687                 *list = &ftrace_event_list;
688                 return last + 1;
689         }
690
691         /*
692          * We used up all possible max events,
693          * lets see if somebody freed one.
694          */
695         list_for_each_entry(e, &ftrace_event_list, list) {
696                 if (e->type != last + 1)
697                         break;
698                 last++;
699         }
700
701         /* Did we used up all 65 thousand events??? */
702         if ((last + 1) > FTRACE_MAX_EVENT)
703                 return 0;
704
705         *list = &e->list;
706         return last + 1;
707 }
708
709 void trace_event_read_lock(void)
710 {
711         down_read(&trace_event_mutex);
712 }
713
714 void trace_event_read_unlock(void)
715 {
716         up_read(&trace_event_mutex);
717 }
718
719 /**
720  * register_ftrace_event - register output for an event type
721  * @event: the event type to register
722  *
723  * Event types are stored in a hash and this hash is used to
724  * find a way to print an event. If the @event->type is set
725  * then it will use that type, otherwise it will assign a
726  * type to use.
727  *
728  * If you assign your own type, please make sure it is added
729  * to the trace_type enum in trace.h, to avoid collisions
730  * with the dynamic types.
731  *
732  * Returns the event type number or zero on error.
733  */
734 int register_ftrace_event(struct trace_event *event)
735 {
736         unsigned key;
737         int ret = 0;
738
739         down_write(&trace_event_mutex);
740
741         if (WARN_ON(!event))
742                 goto out;
743
744         INIT_LIST_HEAD(&event->list);
745
746         if (!event->type) {
747                 struct list_head *list = NULL;
748
749                 if (next_event_type > FTRACE_MAX_EVENT) {
750
751                         event->type = trace_search_list(&list);
752                         if (!event->type)
753                                 goto out;
754
755                 } else {
756                         
757                         event->type = next_event_type++;
758                         list = &ftrace_event_list;
759                 }
760
761                 if (WARN_ON(ftrace_find_event(event->type)))
762                         goto out;
763
764                 list_add_tail(&event->list, list);
765
766         } else if (event->type > __TRACE_LAST_TYPE) {
767                 printk(KERN_WARNING "Need to add type to trace.h\n");
768                 WARN_ON(1);
769                 goto out;
770         } else {
771                 /* Is this event already used */
772                 if (ftrace_find_event(event->type))
773                         goto out;
774         }
775
776         if (event->trace == NULL)
777                 event->trace = trace_nop_print;
778         if (event->raw == NULL)
779                 event->raw = trace_nop_print;
780         if (event->hex == NULL)
781                 event->hex = trace_nop_print;
782         if (event->binary == NULL)
783                 event->binary = trace_nop_print;
784
785         key = event->type & (EVENT_HASHSIZE - 1);
786
787         hlist_add_head(&event->node, &event_hash[key]);
788
789         ret = event->type;
790  out:
791         up_write(&trace_event_mutex);
792
793         return ret;
794 }
795 EXPORT_SYMBOL_GPL(register_ftrace_event);
796
797 /*
798  * Used by module code with the trace_event_mutex held for write.
799  */
800 int __unregister_ftrace_event(struct trace_event *event)
801 {
802         hlist_del(&event->node);
803         list_del(&event->list);
804         return 0;
805 }
806
807 /**
808  * unregister_ftrace_event - remove a no longer used event
809  * @event: the event to remove
810  */
811 int unregister_ftrace_event(struct trace_event *event)
812 {
813         down_write(&trace_event_mutex);
814         __unregister_ftrace_event(event);
815         up_write(&trace_event_mutex);
816
817         return 0;
818 }
819 EXPORT_SYMBOL_GPL(unregister_ftrace_event);
820
821 /*
822  * Standard events
823  */
824
825 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
826 {
827         return TRACE_TYPE_HANDLED;
828 }
829
830 /* TRACE_FN */
831 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
832 {
833         struct ftrace_entry *field;
834         struct trace_seq *s = &iter->seq;
835
836         trace_assign_type(field, iter->ent);
837
838         if (!seq_print_ip_sym(s, field->ip, flags))
839                 goto partial;
840
841         if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
842                 if (!trace_seq_printf(s, " <-"))
843                         goto partial;
844                 if (!seq_print_ip_sym(s,
845                                       field->parent_ip,
846                                       flags))
847                         goto partial;
848         }
849         if (!trace_seq_printf(s, "\n"))
850                 goto partial;
851
852         return TRACE_TYPE_HANDLED;
853
854  partial:
855         return TRACE_TYPE_PARTIAL_LINE;
856 }
857
858 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
859 {
860         struct ftrace_entry *field;
861
862         trace_assign_type(field, iter->ent);
863
864         if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
865                               field->ip,
866                               field->parent_ip))
867                 return TRACE_TYPE_PARTIAL_LINE;
868
869         return TRACE_TYPE_HANDLED;
870 }
871
872 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
873 {
874         struct ftrace_entry *field;
875         struct trace_seq *s = &iter->seq;
876
877         trace_assign_type(field, iter->ent);
878
879         SEQ_PUT_HEX_FIELD_RET(s, field->ip);
880         SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
881
882         return TRACE_TYPE_HANDLED;
883 }
884
885 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
886 {
887         struct ftrace_entry *field;
888         struct trace_seq *s = &iter->seq;
889
890         trace_assign_type(field, iter->ent);
891
892         SEQ_PUT_FIELD_RET(s, field->ip);
893         SEQ_PUT_FIELD_RET(s, field->parent_ip);
894
895         return TRACE_TYPE_HANDLED;
896 }
897
898 static struct trace_event trace_fn_event = {
899         .type           = TRACE_FN,
900         .trace          = trace_fn_trace,
901         .raw            = trace_fn_raw,
902         .hex            = trace_fn_hex,
903         .binary         = trace_fn_bin,
904 };
905
906 /* TRACE_CTX an TRACE_WAKE */
907 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
908                                              char *delim)
909 {
910         struct ctx_switch_entry *field;
911         char comm[TASK_COMM_LEN];
912         int S, T;
913
914
915         trace_assign_type(field, iter->ent);
916
917         T = task_state_char(field->next_state);
918         S = task_state_char(field->prev_state);
919         trace_find_cmdline(field->next_pid, comm);
920         if (!trace_seq_printf(&iter->seq,
921                               " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
922                               field->prev_pid,
923                               field->prev_prio,
924                               S, delim,
925                               field->next_cpu,
926                               field->next_pid,
927                               field->next_prio,
928                               T, comm))
929                 return TRACE_TYPE_PARTIAL_LINE;
930
931         return TRACE_TYPE_HANDLED;
932 }
933
934 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
935 {
936         return trace_ctxwake_print(iter, "==>");
937 }
938
939 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
940                                           int flags)
941 {
942         return trace_ctxwake_print(iter, "  +");
943 }
944
945 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
946 {
947         struct ctx_switch_entry *field;
948         int T;
949
950         trace_assign_type(field, iter->ent);
951
952         if (!S)
953                 S = task_state_char(field->prev_state);
954         T = task_state_char(field->next_state);
955         if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
956                               field->prev_pid,
957                               field->prev_prio,
958                               S,
959                               field->next_cpu,
960                               field->next_pid,
961                               field->next_prio,
962                               T))
963                 return TRACE_TYPE_PARTIAL_LINE;
964
965         return TRACE_TYPE_HANDLED;
966 }
967
968 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
969 {
970         return trace_ctxwake_raw(iter, 0);
971 }
972
973 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
974 {
975         return trace_ctxwake_raw(iter, '+');
976 }
977
978
979 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
980 {
981         struct ctx_switch_entry *field;
982         struct trace_seq *s = &iter->seq;
983         int T;
984
985         trace_assign_type(field, iter->ent);
986
987         if (!S)
988                 S = task_state_char(field->prev_state);
989         T = task_state_char(field->next_state);
990
991         SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
992         SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
993         SEQ_PUT_HEX_FIELD_RET(s, S);
994         SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
995         SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
996         SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
997         SEQ_PUT_HEX_FIELD_RET(s, T);
998
999         return TRACE_TYPE_HANDLED;
1000 }
1001
1002 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
1003 {
1004         return trace_ctxwake_hex(iter, 0);
1005 }
1006
1007 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
1008 {
1009         return trace_ctxwake_hex(iter, '+');
1010 }
1011
1012 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1013                                            int flags)
1014 {
1015         struct ctx_switch_entry *field;
1016         struct trace_seq *s = &iter->seq;
1017
1018         trace_assign_type(field, iter->ent);
1019
1020         SEQ_PUT_FIELD_RET(s, field->prev_pid);
1021         SEQ_PUT_FIELD_RET(s, field->prev_prio);
1022         SEQ_PUT_FIELD_RET(s, field->prev_state);
1023         SEQ_PUT_FIELD_RET(s, field->next_pid);
1024         SEQ_PUT_FIELD_RET(s, field->next_prio);
1025         SEQ_PUT_FIELD_RET(s, field->next_state);
1026
1027         return TRACE_TYPE_HANDLED;
1028 }
1029
1030 static struct trace_event trace_ctx_event = {
1031         .type           = TRACE_CTX,
1032         .trace          = trace_ctx_print,
1033         .raw            = trace_ctx_raw,
1034         .hex            = trace_ctx_hex,
1035         .binary         = trace_ctxwake_bin,
1036 };
1037
1038 static struct trace_event trace_wake_event = {
1039         .type           = TRACE_WAKE,
1040         .trace          = trace_wake_print,
1041         .raw            = trace_wake_raw,
1042         .hex            = trace_wake_hex,
1043         .binary         = trace_ctxwake_bin,
1044 };
1045
1046 /* TRACE_SPECIAL */
1047 static enum print_line_t trace_special_print(struct trace_iterator *iter,
1048                                              int flags)
1049 {
1050         struct special_entry *field;
1051
1052         trace_assign_type(field, iter->ent);
1053
1054         if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
1055                               field->arg1,
1056                               field->arg2,
1057                               field->arg3))
1058                 return TRACE_TYPE_PARTIAL_LINE;
1059
1060         return TRACE_TYPE_HANDLED;
1061 }
1062
1063 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
1064                                            int flags)
1065 {
1066         struct special_entry *field;
1067         struct trace_seq *s = &iter->seq;
1068
1069         trace_assign_type(field, iter->ent);
1070
1071         SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
1072         SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
1073         SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
1074
1075         return TRACE_TYPE_HANDLED;
1076 }
1077
1078 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
1079                                            int flags)
1080 {
1081         struct special_entry *field;
1082         struct trace_seq *s = &iter->seq;
1083
1084         trace_assign_type(field, iter->ent);
1085
1086         SEQ_PUT_FIELD_RET(s, field->arg1);
1087         SEQ_PUT_FIELD_RET(s, field->arg2);
1088         SEQ_PUT_FIELD_RET(s, field->arg3);
1089
1090         return TRACE_TYPE_HANDLED;
1091 }
1092
1093 static struct trace_event trace_special_event = {
1094         .type           = TRACE_SPECIAL,
1095         .trace          = trace_special_print,
1096         .raw            = trace_special_print,
1097         .hex            = trace_special_hex,
1098         .binary         = trace_special_bin,
1099 };
1100
1101 /* TRACE_STACK */
1102
1103 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1104                                            int flags)
1105 {
1106         struct stack_entry *field;
1107         struct trace_seq *s = &iter->seq;
1108         int i;
1109
1110         trace_assign_type(field, iter->ent);
1111
1112         if (!trace_seq_puts(s, "<stack trace>\n"))
1113                 goto partial;
1114         for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
1115                 if (!field->caller[i] || (field->caller[i] == ULONG_MAX))
1116                         break;
1117                 if (!trace_seq_puts(s, " => "))
1118                         goto partial;
1119
1120                 if (!seq_print_ip_sym(s, field->caller[i], flags))
1121                         goto partial;
1122                 if (!trace_seq_puts(s, "\n"))
1123                         goto partial;
1124         }
1125
1126         return TRACE_TYPE_HANDLED;
1127
1128  partial:
1129         return TRACE_TYPE_PARTIAL_LINE;
1130 }
1131
1132 static struct trace_event trace_stack_event = {
1133         .type           = TRACE_STACK,
1134         .trace          = trace_stack_print,
1135         .raw            = trace_special_print,
1136         .hex            = trace_special_hex,
1137         .binary         = trace_special_bin,
1138 };
1139
1140 /* TRACE_USER_STACK */
1141 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1142                                                 int flags)
1143 {
1144         struct userstack_entry *field;
1145         struct trace_seq *s = &iter->seq;
1146
1147         trace_assign_type(field, iter->ent);
1148
1149         if (!trace_seq_puts(s, "<user stack trace>\n"))
1150                 goto partial;
1151
1152         if (!seq_print_userip_objs(field, s, flags))
1153                 goto partial;
1154
1155         return TRACE_TYPE_HANDLED;
1156
1157  partial:
1158         return TRACE_TYPE_PARTIAL_LINE;
1159 }
1160
1161 static struct trace_event trace_user_stack_event = {
1162         .type           = TRACE_USER_STACK,
1163         .trace          = trace_user_stack_print,
1164         .raw            = trace_special_print,
1165         .hex            = trace_special_hex,
1166         .binary         = trace_special_bin,
1167 };
1168
1169 /* TRACE_BPRINT */
1170 static enum print_line_t
1171 trace_bprint_print(struct trace_iterator *iter, int flags)
1172 {
1173         struct trace_entry *entry = iter->ent;
1174         struct trace_seq *s = &iter->seq;
1175         struct bprint_entry *field;
1176
1177         trace_assign_type(field, entry);
1178
1179         if (!seq_print_ip_sym(s, field->ip, flags))
1180                 goto partial;
1181
1182         if (!trace_seq_puts(s, ": "))
1183                 goto partial;
1184
1185         if (!trace_seq_bprintf(s, field->fmt, field->buf))
1186                 goto partial;
1187
1188         return TRACE_TYPE_HANDLED;
1189
1190  partial:
1191         return TRACE_TYPE_PARTIAL_LINE;
1192 }
1193
1194
1195 static enum print_line_t
1196 trace_bprint_raw(struct trace_iterator *iter, int flags)
1197 {
1198         struct bprint_entry *field;
1199         struct trace_seq *s = &iter->seq;
1200
1201         trace_assign_type(field, iter->ent);
1202
1203         if (!trace_seq_printf(s, ": %lx : ", field->ip))
1204                 goto partial;
1205
1206         if (!trace_seq_bprintf(s, field->fmt, field->buf))
1207                 goto partial;
1208
1209         return TRACE_TYPE_HANDLED;
1210
1211  partial:
1212         return TRACE_TYPE_PARTIAL_LINE;
1213 }
1214
1215
1216 static struct trace_event trace_bprint_event = {
1217         .type           = TRACE_BPRINT,
1218         .trace          = trace_bprint_print,
1219         .raw            = trace_bprint_raw,
1220 };
1221
1222 /* TRACE_PRINT */
1223 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1224                                            int flags)
1225 {
1226         struct print_entry *field;
1227         struct trace_seq *s = &iter->seq;
1228
1229         trace_assign_type(field, iter->ent);
1230
1231         if (!seq_print_ip_sym(s, field->ip, flags))
1232                 goto partial;
1233
1234         if (!trace_seq_printf(s, ": %s", field->buf))
1235                 goto partial;
1236
1237         return TRACE_TYPE_HANDLED;
1238
1239  partial:
1240         return TRACE_TYPE_PARTIAL_LINE;
1241 }
1242
1243 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
1244 {
1245         struct print_entry *field;
1246
1247         trace_assign_type(field, iter->ent);
1248
1249         if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
1250                 goto partial;
1251
1252         return TRACE_TYPE_HANDLED;
1253
1254  partial:
1255         return TRACE_TYPE_PARTIAL_LINE;
1256 }
1257
1258 static struct trace_event trace_print_event = {
1259         .type           = TRACE_PRINT,
1260         .trace          = trace_print_print,
1261         .raw            = trace_print_raw,
1262 };
1263
1264
1265 static struct trace_event *events[] __initdata = {
1266         &trace_fn_event,
1267         &trace_ctx_event,
1268         &trace_wake_event,
1269         &trace_special_event,
1270         &trace_stack_event,
1271         &trace_user_stack_event,
1272         &trace_bprint_event,
1273         &trace_print_event,
1274         NULL
1275 };
1276
1277 __init static int init_events(void)
1278 {
1279         struct trace_event *event;
1280         int i, ret;
1281
1282         for (i = 0; events[i]; i++) {
1283                 event = events[i];
1284
1285                 ret = register_ftrace_event(event);
1286                 if (!ret) {
1287                         printk(KERN_WARNING "event %d failed to register\n",
1288                                event->type);
1289                         WARN_ON_ONCE(1);
1290                 }
1291         }
1292
1293         return 0;
1294 }
1295 device_initcall(init_events);