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