fix a kmap leak in virtio_console
[linux/fpc-iii.git] / kernel / trace / trace_output.c
blobed32284fbe32b4cd95b67c54e04a276eedd5b3c2
1 /*
2 * trace_output.c
4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
6 */
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/ftrace.h>
12 #include "trace_output.h"
14 /* must be a power of 2 */
15 #define EVENT_HASHSIZE 128
17 DECLARE_RWSEM(trace_event_sem);
19 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
21 static int next_event_type = __TRACE_LAST_TYPE + 1;
23 int trace_print_seq(struct seq_file *m, struct trace_seq *s)
25 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
26 int ret;
28 ret = seq_write(m, s->buffer, len);
31 * Only reset this buffer if we successfully wrote to the
32 * seq_file buffer.
34 if (!ret)
35 trace_seq_init(s);
37 return ret;
40 enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter)
42 struct trace_seq *s = &iter->seq;
43 struct trace_entry *entry = iter->ent;
44 struct bputs_entry *field;
45 int ret;
47 trace_assign_type(field, entry);
49 ret = trace_seq_puts(s, field->str);
50 if (!ret)
51 return TRACE_TYPE_PARTIAL_LINE;
53 return TRACE_TYPE_HANDLED;
56 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
58 struct trace_seq *s = &iter->seq;
59 struct trace_entry *entry = iter->ent;
60 struct bprint_entry *field;
61 int ret;
63 trace_assign_type(field, entry);
65 ret = trace_seq_bprintf(s, field->fmt, field->buf);
66 if (!ret)
67 return TRACE_TYPE_PARTIAL_LINE;
69 return TRACE_TYPE_HANDLED;
72 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
74 struct trace_seq *s = &iter->seq;
75 struct trace_entry *entry = iter->ent;
76 struct print_entry *field;
77 int ret;
79 trace_assign_type(field, entry);
81 ret = trace_seq_puts(s, field->buf);
82 if (!ret)
83 return TRACE_TYPE_PARTIAL_LINE;
85 return TRACE_TYPE_HANDLED;
88 /**
89 * trace_seq_printf - sequence printing of trace information
90 * @s: trace sequence descriptor
91 * @fmt: printf format string
93 * It returns 0 if the trace oversizes the buffer's free
94 * space, 1 otherwise.
96 * The tracer may use either sequence operations or its own
97 * copy to user routines. To simplify formating of a trace
98 * trace_seq_printf is used to store strings into a special
99 * buffer (@s). Then the output may be either used by
100 * the sequencer or pulled into another buffer.
103 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
105 int len = (PAGE_SIZE - 1) - s->len;
106 va_list ap;
107 int ret;
109 if (s->full || !len)
110 return 0;
112 va_start(ap, fmt);
113 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
114 va_end(ap);
116 /* If we can't write it all, don't bother writing anything */
117 if (ret >= len) {
118 s->full = 1;
119 return 0;
122 s->len += ret;
124 return 1;
126 EXPORT_SYMBOL_GPL(trace_seq_printf);
129 * trace_seq_vprintf - sequence printing of trace information
130 * @s: trace sequence descriptor
131 * @fmt: printf format string
133 * The tracer may use either sequence operations or its own
134 * copy to user routines. To simplify formating of a trace
135 * trace_seq_printf is used to store strings into a special
136 * buffer (@s). Then the output may be either used by
137 * the sequencer or pulled into another buffer.
140 trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
142 int len = (PAGE_SIZE - 1) - s->len;
143 int ret;
145 if (s->full || !len)
146 return 0;
148 ret = vsnprintf(s->buffer + s->len, len, fmt, args);
150 /* If we can't write it all, don't bother writing anything */
151 if (ret >= len) {
152 s->full = 1;
153 return 0;
156 s->len += ret;
158 return len;
160 EXPORT_SYMBOL_GPL(trace_seq_vprintf);
162 int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
164 int len = (PAGE_SIZE - 1) - s->len;
165 int ret;
167 if (s->full || !len)
168 return 0;
170 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
172 /* If we can't write it all, don't bother writing anything */
173 if (ret >= len) {
174 s->full = 1;
175 return 0;
178 s->len += ret;
180 return len;
184 * trace_seq_puts - trace sequence printing of simple string
185 * @s: trace sequence descriptor
186 * @str: simple string to record
188 * The tracer may use either the sequence operations or its own
189 * copy to user routines. This function records a simple string
190 * into a special buffer (@s) for later retrieval by a sequencer
191 * or other mechanism.
193 int trace_seq_puts(struct trace_seq *s, const char *str)
195 int len = strlen(str);
197 if (s->full)
198 return 0;
200 if (len > ((PAGE_SIZE - 1) - s->len)) {
201 s->full = 1;
202 return 0;
205 memcpy(s->buffer + s->len, str, len);
206 s->len += len;
208 return len;
211 int trace_seq_putc(struct trace_seq *s, unsigned char c)
213 if (s->full)
214 return 0;
216 if (s->len >= (PAGE_SIZE - 1)) {
217 s->full = 1;
218 return 0;
221 s->buffer[s->len++] = c;
223 return 1;
225 EXPORT_SYMBOL(trace_seq_putc);
227 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
229 if (s->full)
230 return 0;
232 if (len > ((PAGE_SIZE - 1) - s->len)) {
233 s->full = 1;
234 return 0;
237 memcpy(s->buffer + s->len, mem, len);
238 s->len += len;
240 return len;
243 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
245 unsigned char hex[HEX_CHARS];
246 const unsigned char *data = mem;
247 int i, j;
249 if (s->full)
250 return 0;
252 #ifdef __BIG_ENDIAN
253 for (i = 0, j = 0; i < len; i++) {
254 #else
255 for (i = len-1, j = 0; i >= 0; i--) {
256 #endif
257 hex[j++] = hex_asc_hi(data[i]);
258 hex[j++] = hex_asc_lo(data[i]);
260 hex[j++] = ' ';
262 return trace_seq_putmem(s, hex, j);
265 void *trace_seq_reserve(struct trace_seq *s, size_t len)
267 void *ret;
269 if (s->full)
270 return NULL;
272 if (len > ((PAGE_SIZE - 1) - s->len)) {
273 s->full = 1;
274 return NULL;
277 ret = s->buffer + s->len;
278 s->len += len;
280 return ret;
283 int trace_seq_path(struct trace_seq *s, const struct path *path)
285 unsigned char *p;
287 if (s->full)
288 return 0;
290 if (s->len >= (PAGE_SIZE - 1)) {
291 s->full = 1;
292 return 0;
295 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
296 if (!IS_ERR(p)) {
297 p = mangle_path(s->buffer + s->len, p, "\n");
298 if (p) {
299 s->len = p - s->buffer;
300 return 1;
302 } else {
303 s->buffer[s->len++] = '?';
304 return 1;
307 s->full = 1;
308 return 0;
311 const char *
312 ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
313 unsigned long flags,
314 const struct trace_print_flags *flag_array)
316 unsigned long mask;
317 const char *str;
318 const char *ret = p->buffer + p->len;
319 int i, first = 1;
321 for (i = 0; flag_array[i].name && flags; i++) {
323 mask = flag_array[i].mask;
324 if ((flags & mask) != mask)
325 continue;
327 str = flag_array[i].name;
328 flags &= ~mask;
329 if (!first && delim)
330 trace_seq_puts(p, delim);
331 else
332 first = 0;
333 trace_seq_puts(p, str);
336 /* check for left over flags */
337 if (flags) {
338 if (!first && delim)
339 trace_seq_puts(p, delim);
340 trace_seq_printf(p, "0x%lx", flags);
343 trace_seq_putc(p, 0);
345 return ret;
347 EXPORT_SYMBOL(ftrace_print_flags_seq);
349 const char *
350 ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
351 const struct trace_print_flags *symbol_array)
353 int i;
354 const char *ret = p->buffer + p->len;
356 for (i = 0; symbol_array[i].name; i++) {
358 if (val != symbol_array[i].mask)
359 continue;
361 trace_seq_puts(p, symbol_array[i].name);
362 break;
365 if (ret == (const char *)(p->buffer + p->len))
366 trace_seq_printf(p, "0x%lx", val);
368 trace_seq_putc(p, 0);
370 return ret;
372 EXPORT_SYMBOL(ftrace_print_symbols_seq);
374 #if BITS_PER_LONG == 32
375 const char *
376 ftrace_print_symbols_seq_u64(struct trace_seq *p, unsigned long long val,
377 const struct trace_print_flags_u64 *symbol_array)
379 int i;
380 const char *ret = p->buffer + p->len;
382 for (i = 0; symbol_array[i].name; i++) {
384 if (val != symbol_array[i].mask)
385 continue;
387 trace_seq_puts(p, symbol_array[i].name);
388 break;
391 if (ret == (const char *)(p->buffer + p->len))
392 trace_seq_printf(p, "0x%llx", val);
394 trace_seq_putc(p, 0);
396 return ret;
398 EXPORT_SYMBOL(ftrace_print_symbols_seq_u64);
399 #endif
401 const char *
402 ftrace_print_hex_seq(struct trace_seq *p, const unsigned char *buf, int buf_len)
404 int i;
405 const char *ret = p->buffer + p->len;
407 for (i = 0; i < buf_len; i++)
408 trace_seq_printf(p, "%s%2.2x", i == 0 ? "" : " ", buf[i]);
410 trace_seq_putc(p, 0);
412 return ret;
414 EXPORT_SYMBOL(ftrace_print_hex_seq);
416 int ftrace_raw_output_prep(struct trace_iterator *iter,
417 struct trace_event *trace_event)
419 struct ftrace_event_call *event;
420 struct trace_seq *s = &iter->seq;
421 struct trace_seq *p = &iter->tmp_seq;
422 struct trace_entry *entry;
423 int ret;
425 event = container_of(trace_event, struct ftrace_event_call, event);
426 entry = iter->ent;
428 if (entry->type != event->event.type) {
429 WARN_ON_ONCE(1);
430 return TRACE_TYPE_UNHANDLED;
433 trace_seq_init(p);
434 ret = trace_seq_printf(s, "%s: ", event->name);
435 if (!ret)
436 return TRACE_TYPE_PARTIAL_LINE;
438 return 0;
440 EXPORT_SYMBOL(ftrace_raw_output_prep);
442 #ifdef CONFIG_KRETPROBES
443 static inline const char *kretprobed(const char *name)
445 static const char tramp_name[] = "kretprobe_trampoline";
446 int size = sizeof(tramp_name);
448 if (strncmp(tramp_name, name, size) == 0)
449 return "[unknown/kretprobe'd]";
450 return name;
452 #else
453 static inline const char *kretprobed(const char *name)
455 return name;
457 #endif /* CONFIG_KRETPROBES */
459 static int
460 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
462 #ifdef CONFIG_KALLSYMS
463 char str[KSYM_SYMBOL_LEN];
464 const char *name;
466 kallsyms_lookup(address, NULL, NULL, NULL, str);
468 name = kretprobed(str);
470 return trace_seq_printf(s, fmt, name);
471 #endif
472 return 1;
475 static int
476 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
477 unsigned long address)
479 #ifdef CONFIG_KALLSYMS
480 char str[KSYM_SYMBOL_LEN];
481 const char *name;
483 sprint_symbol(str, address);
484 name = kretprobed(str);
486 return trace_seq_printf(s, fmt, name);
487 #endif
488 return 1;
491 #ifndef CONFIG_64BIT
492 # define IP_FMT "%08lx"
493 #else
494 # define IP_FMT "%016lx"
495 #endif
497 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
498 unsigned long ip, unsigned long sym_flags)
500 struct file *file = NULL;
501 unsigned long vmstart = 0;
502 int ret = 1;
504 if (s->full)
505 return 0;
507 if (mm) {
508 const struct vm_area_struct *vma;
510 down_read(&mm->mmap_sem);
511 vma = find_vma(mm, ip);
512 if (vma) {
513 file = vma->vm_file;
514 vmstart = vma->vm_start;
516 if (file) {
517 ret = trace_seq_path(s, &file->f_path);
518 if (ret)
519 ret = trace_seq_printf(s, "[+0x%lx]",
520 ip - vmstart);
522 up_read(&mm->mmap_sem);
524 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
525 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
526 return ret;
530 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
531 unsigned long sym_flags)
533 struct mm_struct *mm = NULL;
534 int ret = 1;
535 unsigned int i;
537 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
538 struct task_struct *task;
540 * we do the lookup on the thread group leader,
541 * since individual threads might have already quit!
543 rcu_read_lock();
544 task = find_task_by_vpid(entry->tgid);
545 if (task)
546 mm = get_task_mm(task);
547 rcu_read_unlock();
550 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
551 unsigned long ip = entry->caller[i];
553 if (ip == ULONG_MAX || !ret)
554 break;
555 if (ret)
556 ret = trace_seq_puts(s, " => ");
557 if (!ip) {
558 if (ret)
559 ret = trace_seq_puts(s, "??");
560 if (ret)
561 ret = trace_seq_putc(s, '\n');
562 continue;
564 if (!ret)
565 break;
566 if (ret)
567 ret = seq_print_user_ip(s, mm, ip, sym_flags);
568 ret = trace_seq_putc(s, '\n');
571 if (mm)
572 mmput(mm);
573 return ret;
577 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
579 int ret;
581 if (!ip)
582 return trace_seq_putc(s, '0');
584 if (sym_flags & TRACE_ITER_SYM_OFFSET)
585 ret = seq_print_sym_offset(s, "%s", ip);
586 else
587 ret = seq_print_sym_short(s, "%s", ip);
589 if (!ret)
590 return 0;
592 if (sym_flags & TRACE_ITER_SYM_ADDR)
593 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
594 return ret;
598 * trace_print_lat_fmt - print the irq, preempt and lockdep fields
599 * @s: trace seq struct to write to
600 * @entry: The trace entry field from the ring buffer
602 * Prints the generic fields of irqs off, in hard or softirq, preempt
603 * count.
605 int trace_print_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
607 char hardsoft_irq;
608 char need_resched;
609 char irqs_off;
610 int hardirq;
611 int softirq;
612 int ret;
614 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
615 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
617 irqs_off =
618 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
619 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' :
620 '.';
622 switch (entry->flags & (TRACE_FLAG_NEED_RESCHED |
623 TRACE_FLAG_PREEMPT_RESCHED)) {
624 case TRACE_FLAG_NEED_RESCHED | TRACE_FLAG_PREEMPT_RESCHED:
625 need_resched = 'N';
626 break;
627 case TRACE_FLAG_NEED_RESCHED:
628 need_resched = 'n';
629 break;
630 case TRACE_FLAG_PREEMPT_RESCHED:
631 need_resched = 'p';
632 break;
633 default:
634 need_resched = '.';
635 break;
638 hardsoft_irq =
639 (hardirq && softirq) ? 'H' :
640 hardirq ? 'h' :
641 softirq ? 's' :
642 '.';
644 if (!trace_seq_printf(s, "%c%c%c",
645 irqs_off, need_resched, hardsoft_irq))
646 return 0;
648 if (entry->preempt_count)
649 ret = trace_seq_printf(s, "%x", entry->preempt_count);
650 else
651 ret = trace_seq_putc(s, '.');
653 return ret;
656 static int
657 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
659 char comm[TASK_COMM_LEN];
661 trace_find_cmdline(entry->pid, comm);
663 if (!trace_seq_printf(s, "%8.8s-%-5d %3d",
664 comm, entry->pid, cpu))
665 return 0;
667 return trace_print_lat_fmt(s, entry);
670 static unsigned long preempt_mark_thresh_us = 100;
672 static int
673 lat_print_timestamp(struct trace_iterator *iter, u64 next_ts)
675 unsigned long verbose = trace_flags & TRACE_ITER_VERBOSE;
676 unsigned long in_ns = iter->iter_flags & TRACE_FILE_TIME_IN_NS;
677 unsigned long long abs_ts = iter->ts - iter->trace_buffer->time_start;
678 unsigned long long rel_ts = next_ts - iter->ts;
679 struct trace_seq *s = &iter->seq;
681 if (in_ns) {
682 abs_ts = ns2usecs(abs_ts);
683 rel_ts = ns2usecs(rel_ts);
686 if (verbose && in_ns) {
687 unsigned long abs_usec = do_div(abs_ts, USEC_PER_MSEC);
688 unsigned long abs_msec = (unsigned long)abs_ts;
689 unsigned long rel_usec = do_div(rel_ts, USEC_PER_MSEC);
690 unsigned long rel_msec = (unsigned long)rel_ts;
692 return trace_seq_printf(
693 s, "[%08llx] %ld.%03ldms (+%ld.%03ldms): ",
694 ns2usecs(iter->ts),
695 abs_msec, abs_usec,
696 rel_msec, rel_usec);
697 } else if (verbose && !in_ns) {
698 return trace_seq_printf(
699 s, "[%016llx] %lld (+%lld): ",
700 iter->ts, abs_ts, rel_ts);
701 } else if (!verbose && in_ns) {
702 return trace_seq_printf(
703 s, " %4lldus%c: ",
704 abs_ts,
705 rel_ts > preempt_mark_thresh_us ? '!' :
706 rel_ts > 1 ? '+' : ' ');
707 } else { /* !verbose && !in_ns */
708 return trace_seq_printf(s, " %4lld: ", abs_ts);
712 int trace_print_context(struct trace_iterator *iter)
714 struct trace_seq *s = &iter->seq;
715 struct trace_entry *entry = iter->ent;
716 unsigned long long t;
717 unsigned long secs, usec_rem;
718 char comm[TASK_COMM_LEN];
719 int ret;
721 trace_find_cmdline(entry->pid, comm);
723 ret = trace_seq_printf(s, "%16s-%-5d [%03d] ",
724 comm, entry->pid, iter->cpu);
725 if (!ret)
726 return 0;
728 if (trace_flags & TRACE_ITER_IRQ_INFO) {
729 ret = trace_print_lat_fmt(s, entry);
730 if (!ret)
731 return 0;
734 if (iter->iter_flags & TRACE_FILE_TIME_IN_NS) {
735 t = ns2usecs(iter->ts);
736 usec_rem = do_div(t, USEC_PER_SEC);
737 secs = (unsigned long)t;
738 return trace_seq_printf(s, " %5lu.%06lu: ", secs, usec_rem);
739 } else
740 return trace_seq_printf(s, " %12llu: ", iter->ts);
743 int trace_print_lat_context(struct trace_iterator *iter)
745 u64 next_ts;
746 int ret;
747 /* trace_find_next_entry will reset ent_size */
748 int ent_size = iter->ent_size;
749 struct trace_seq *s = &iter->seq;
750 struct trace_entry *entry = iter->ent,
751 *next_entry = trace_find_next_entry(iter, NULL,
752 &next_ts);
753 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
755 /* Restore the original ent_size */
756 iter->ent_size = ent_size;
758 if (!next_entry)
759 next_ts = iter->ts;
761 if (verbose) {
762 char comm[TASK_COMM_LEN];
764 trace_find_cmdline(entry->pid, comm);
766 ret = trace_seq_printf(
767 s, "%16s %5d %3d %d %08x %08lx ",
768 comm, entry->pid, iter->cpu, entry->flags,
769 entry->preempt_count, iter->idx);
770 } else {
771 ret = lat_print_generic(s, entry, iter->cpu);
774 if (ret)
775 ret = lat_print_timestamp(iter, next_ts);
777 return ret;
780 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
782 static int task_state_char(unsigned long state)
784 int bit = state ? __ffs(state) + 1 : 0;
786 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
790 * ftrace_find_event - find a registered event
791 * @type: the type of event to look for
793 * Returns an event of type @type otherwise NULL
794 * Called with trace_event_read_lock() held.
796 struct trace_event *ftrace_find_event(int type)
798 struct trace_event *event;
799 unsigned key;
801 key = type & (EVENT_HASHSIZE - 1);
803 hlist_for_each_entry(event, &event_hash[key], node) {
804 if (event->type == type)
805 return event;
808 return NULL;
811 static LIST_HEAD(ftrace_event_list);
813 static int trace_search_list(struct list_head **list)
815 struct trace_event *e;
816 int last = __TRACE_LAST_TYPE;
818 if (list_empty(&ftrace_event_list)) {
819 *list = &ftrace_event_list;
820 return last + 1;
824 * We used up all possible max events,
825 * lets see if somebody freed one.
827 list_for_each_entry(e, &ftrace_event_list, list) {
828 if (e->type != last + 1)
829 break;
830 last++;
833 /* Did we used up all 65 thousand events??? */
834 if ((last + 1) > FTRACE_MAX_EVENT)
835 return 0;
837 *list = &e->list;
838 return last + 1;
841 void trace_event_read_lock(void)
843 down_read(&trace_event_sem);
846 void trace_event_read_unlock(void)
848 up_read(&trace_event_sem);
852 * register_ftrace_event - register output for an event type
853 * @event: the event type to register
855 * Event types are stored in a hash and this hash is used to
856 * find a way to print an event. If the @event->type is set
857 * then it will use that type, otherwise it will assign a
858 * type to use.
860 * If you assign your own type, please make sure it is added
861 * to the trace_type enum in trace.h, to avoid collisions
862 * with the dynamic types.
864 * Returns the event type number or zero on error.
866 int register_ftrace_event(struct trace_event *event)
868 unsigned key;
869 int ret = 0;
871 down_write(&trace_event_sem);
873 if (WARN_ON(!event))
874 goto out;
876 if (WARN_ON(!event->funcs))
877 goto out;
879 INIT_LIST_HEAD(&event->list);
881 if (!event->type) {
882 struct list_head *list = NULL;
884 if (next_event_type > FTRACE_MAX_EVENT) {
886 event->type = trace_search_list(&list);
887 if (!event->type)
888 goto out;
890 } else {
892 event->type = next_event_type++;
893 list = &ftrace_event_list;
896 if (WARN_ON(ftrace_find_event(event->type)))
897 goto out;
899 list_add_tail(&event->list, list);
901 } else if (event->type > __TRACE_LAST_TYPE) {
902 printk(KERN_WARNING "Need to add type to trace.h\n");
903 WARN_ON(1);
904 goto out;
905 } else {
906 /* Is this event already used */
907 if (ftrace_find_event(event->type))
908 goto out;
911 if (event->funcs->trace == NULL)
912 event->funcs->trace = trace_nop_print;
913 if (event->funcs->raw == NULL)
914 event->funcs->raw = trace_nop_print;
915 if (event->funcs->hex == NULL)
916 event->funcs->hex = trace_nop_print;
917 if (event->funcs->binary == NULL)
918 event->funcs->binary = trace_nop_print;
920 key = event->type & (EVENT_HASHSIZE - 1);
922 hlist_add_head(&event->node, &event_hash[key]);
924 ret = event->type;
925 out:
926 up_write(&trace_event_sem);
928 return ret;
930 EXPORT_SYMBOL_GPL(register_ftrace_event);
933 * Used by module code with the trace_event_sem held for write.
935 int __unregister_ftrace_event(struct trace_event *event)
937 hlist_del(&event->node);
938 list_del(&event->list);
939 return 0;
943 * unregister_ftrace_event - remove a no longer used event
944 * @event: the event to remove
946 int unregister_ftrace_event(struct trace_event *event)
948 down_write(&trace_event_sem);
949 __unregister_ftrace_event(event);
950 up_write(&trace_event_sem);
952 return 0;
954 EXPORT_SYMBOL_GPL(unregister_ftrace_event);
957 * Standard events
960 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags,
961 struct trace_event *event)
963 if (!trace_seq_printf(&iter->seq, "type: %d\n", iter->ent->type))
964 return TRACE_TYPE_PARTIAL_LINE;
966 return TRACE_TYPE_HANDLED;
969 /* TRACE_FN */
970 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags,
971 struct trace_event *event)
973 struct ftrace_entry *field;
974 struct trace_seq *s = &iter->seq;
976 trace_assign_type(field, iter->ent);
978 if (!seq_print_ip_sym(s, field->ip, flags))
979 goto partial;
981 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
982 if (!trace_seq_puts(s, " <-"))
983 goto partial;
984 if (!seq_print_ip_sym(s,
985 field->parent_ip,
986 flags))
987 goto partial;
989 if (!trace_seq_putc(s, '\n'))
990 goto partial;
992 return TRACE_TYPE_HANDLED;
994 partial:
995 return TRACE_TYPE_PARTIAL_LINE;
998 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags,
999 struct trace_event *event)
1001 struct ftrace_entry *field;
1003 trace_assign_type(field, iter->ent);
1005 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
1006 field->ip,
1007 field->parent_ip))
1008 return TRACE_TYPE_PARTIAL_LINE;
1010 return TRACE_TYPE_HANDLED;
1013 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags,
1014 struct trace_event *event)
1016 struct ftrace_entry *field;
1017 struct trace_seq *s = &iter->seq;
1019 trace_assign_type(field, iter->ent);
1021 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
1022 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
1024 return TRACE_TYPE_HANDLED;
1027 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags,
1028 struct trace_event *event)
1030 struct ftrace_entry *field;
1031 struct trace_seq *s = &iter->seq;
1033 trace_assign_type(field, iter->ent);
1035 SEQ_PUT_FIELD_RET(s, field->ip);
1036 SEQ_PUT_FIELD_RET(s, field->parent_ip);
1038 return TRACE_TYPE_HANDLED;
1041 static struct trace_event_functions trace_fn_funcs = {
1042 .trace = trace_fn_trace,
1043 .raw = trace_fn_raw,
1044 .hex = trace_fn_hex,
1045 .binary = trace_fn_bin,
1048 static struct trace_event trace_fn_event = {
1049 .type = TRACE_FN,
1050 .funcs = &trace_fn_funcs,
1053 /* TRACE_CTX an TRACE_WAKE */
1054 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
1055 char *delim)
1057 struct ctx_switch_entry *field;
1058 char comm[TASK_COMM_LEN];
1059 int S, T;
1062 trace_assign_type(field, iter->ent);
1064 T = task_state_char(field->next_state);
1065 S = task_state_char(field->prev_state);
1066 trace_find_cmdline(field->next_pid, comm);
1067 if (!trace_seq_printf(&iter->seq,
1068 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1069 field->prev_pid,
1070 field->prev_prio,
1071 S, delim,
1072 field->next_cpu,
1073 field->next_pid,
1074 field->next_prio,
1075 T, comm))
1076 return TRACE_TYPE_PARTIAL_LINE;
1078 return TRACE_TYPE_HANDLED;
1081 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags,
1082 struct trace_event *event)
1084 return trace_ctxwake_print(iter, "==>");
1087 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
1088 int flags, struct trace_event *event)
1090 return trace_ctxwake_print(iter, " +");
1093 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
1095 struct ctx_switch_entry *field;
1096 int T;
1098 trace_assign_type(field, iter->ent);
1100 if (!S)
1101 S = task_state_char(field->prev_state);
1102 T = task_state_char(field->next_state);
1103 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
1104 field->prev_pid,
1105 field->prev_prio,
1107 field->next_cpu,
1108 field->next_pid,
1109 field->next_prio,
1111 return TRACE_TYPE_PARTIAL_LINE;
1113 return TRACE_TYPE_HANDLED;
1116 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags,
1117 struct trace_event *event)
1119 return trace_ctxwake_raw(iter, 0);
1122 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags,
1123 struct trace_event *event)
1125 return trace_ctxwake_raw(iter, '+');
1129 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
1131 struct ctx_switch_entry *field;
1132 struct trace_seq *s = &iter->seq;
1133 int T;
1135 trace_assign_type(field, iter->ent);
1137 if (!S)
1138 S = task_state_char(field->prev_state);
1139 T = task_state_char(field->next_state);
1141 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
1142 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
1143 SEQ_PUT_HEX_FIELD_RET(s, S);
1144 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
1145 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
1146 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
1147 SEQ_PUT_HEX_FIELD_RET(s, T);
1149 return TRACE_TYPE_HANDLED;
1152 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags,
1153 struct trace_event *event)
1155 return trace_ctxwake_hex(iter, 0);
1158 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags,
1159 struct trace_event *event)
1161 return trace_ctxwake_hex(iter, '+');
1164 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
1165 int flags, struct trace_event *event)
1167 struct ctx_switch_entry *field;
1168 struct trace_seq *s = &iter->seq;
1170 trace_assign_type(field, iter->ent);
1172 SEQ_PUT_FIELD_RET(s, field->prev_pid);
1173 SEQ_PUT_FIELD_RET(s, field->prev_prio);
1174 SEQ_PUT_FIELD_RET(s, field->prev_state);
1175 SEQ_PUT_FIELD_RET(s, field->next_pid);
1176 SEQ_PUT_FIELD_RET(s, field->next_prio);
1177 SEQ_PUT_FIELD_RET(s, field->next_state);
1179 return TRACE_TYPE_HANDLED;
1182 static struct trace_event_functions trace_ctx_funcs = {
1183 .trace = trace_ctx_print,
1184 .raw = trace_ctx_raw,
1185 .hex = trace_ctx_hex,
1186 .binary = trace_ctxwake_bin,
1189 static struct trace_event trace_ctx_event = {
1190 .type = TRACE_CTX,
1191 .funcs = &trace_ctx_funcs,
1194 static struct trace_event_functions trace_wake_funcs = {
1195 .trace = trace_wake_print,
1196 .raw = trace_wake_raw,
1197 .hex = trace_wake_hex,
1198 .binary = trace_ctxwake_bin,
1201 static struct trace_event trace_wake_event = {
1202 .type = TRACE_WAKE,
1203 .funcs = &trace_wake_funcs,
1206 /* TRACE_STACK */
1208 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
1209 int flags, struct trace_event *event)
1211 struct stack_entry *field;
1212 struct trace_seq *s = &iter->seq;
1213 unsigned long *p;
1214 unsigned long *end;
1216 trace_assign_type(field, iter->ent);
1217 end = (unsigned long *)((long)iter->ent + iter->ent_size);
1219 if (!trace_seq_puts(s, "<stack trace>\n"))
1220 goto partial;
1222 for (p = field->caller; p && *p != ULONG_MAX && p < end; p++) {
1223 if (!trace_seq_puts(s, " => "))
1224 goto partial;
1226 if (!seq_print_ip_sym(s, *p, flags))
1227 goto partial;
1228 if (!trace_seq_putc(s, '\n'))
1229 goto partial;
1232 return TRACE_TYPE_HANDLED;
1234 partial:
1235 return TRACE_TYPE_PARTIAL_LINE;
1238 static struct trace_event_functions trace_stack_funcs = {
1239 .trace = trace_stack_print,
1242 static struct trace_event trace_stack_event = {
1243 .type = TRACE_STACK,
1244 .funcs = &trace_stack_funcs,
1247 /* TRACE_USER_STACK */
1248 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
1249 int flags, struct trace_event *event)
1251 struct userstack_entry *field;
1252 struct trace_seq *s = &iter->seq;
1254 trace_assign_type(field, iter->ent);
1256 if (!trace_seq_puts(s, "<user stack trace>\n"))
1257 goto partial;
1259 if (!seq_print_userip_objs(field, s, flags))
1260 goto partial;
1262 return TRACE_TYPE_HANDLED;
1264 partial:
1265 return TRACE_TYPE_PARTIAL_LINE;
1268 static struct trace_event_functions trace_user_stack_funcs = {
1269 .trace = trace_user_stack_print,
1272 static struct trace_event trace_user_stack_event = {
1273 .type = TRACE_USER_STACK,
1274 .funcs = &trace_user_stack_funcs,
1277 /* TRACE_BPUTS */
1278 static enum print_line_t
1279 trace_bputs_print(struct trace_iterator *iter, int flags,
1280 struct trace_event *event)
1282 struct trace_entry *entry = iter->ent;
1283 struct trace_seq *s = &iter->seq;
1284 struct bputs_entry *field;
1286 trace_assign_type(field, entry);
1288 if (!seq_print_ip_sym(s, field->ip, flags))
1289 goto partial;
1291 if (!trace_seq_puts(s, ": "))
1292 goto partial;
1294 if (!trace_seq_puts(s, field->str))
1295 goto partial;
1297 return TRACE_TYPE_HANDLED;
1299 partial:
1300 return TRACE_TYPE_PARTIAL_LINE;
1304 static enum print_line_t
1305 trace_bputs_raw(struct trace_iterator *iter, int flags,
1306 struct trace_event *event)
1308 struct bputs_entry *field;
1309 struct trace_seq *s = &iter->seq;
1311 trace_assign_type(field, iter->ent);
1313 if (!trace_seq_printf(s, ": %lx : ", field->ip))
1314 goto partial;
1316 if (!trace_seq_puts(s, field->str))
1317 goto partial;
1319 return TRACE_TYPE_HANDLED;
1321 partial:
1322 return TRACE_TYPE_PARTIAL_LINE;
1325 static struct trace_event_functions trace_bputs_funcs = {
1326 .trace = trace_bputs_print,
1327 .raw = trace_bputs_raw,
1330 static struct trace_event trace_bputs_event = {
1331 .type = TRACE_BPUTS,
1332 .funcs = &trace_bputs_funcs,
1335 /* TRACE_BPRINT */
1336 static enum print_line_t
1337 trace_bprint_print(struct trace_iterator *iter, int flags,
1338 struct trace_event *event)
1340 struct trace_entry *entry = iter->ent;
1341 struct trace_seq *s = &iter->seq;
1342 struct bprint_entry *field;
1344 trace_assign_type(field, entry);
1346 if (!seq_print_ip_sym(s, field->ip, flags))
1347 goto partial;
1349 if (!trace_seq_puts(s, ": "))
1350 goto partial;
1352 if (!trace_seq_bprintf(s, field->fmt, field->buf))
1353 goto partial;
1355 return TRACE_TYPE_HANDLED;
1357 partial:
1358 return TRACE_TYPE_PARTIAL_LINE;
1362 static enum print_line_t
1363 trace_bprint_raw(struct trace_iterator *iter, int flags,
1364 struct trace_event *event)
1366 struct bprint_entry *field;
1367 struct trace_seq *s = &iter->seq;
1369 trace_assign_type(field, iter->ent);
1371 if (!trace_seq_printf(s, ": %lx : ", field->ip))
1372 goto partial;
1374 if (!trace_seq_bprintf(s, field->fmt, field->buf))
1375 goto partial;
1377 return TRACE_TYPE_HANDLED;
1379 partial:
1380 return TRACE_TYPE_PARTIAL_LINE;
1383 static struct trace_event_functions trace_bprint_funcs = {
1384 .trace = trace_bprint_print,
1385 .raw = trace_bprint_raw,
1388 static struct trace_event trace_bprint_event = {
1389 .type = TRACE_BPRINT,
1390 .funcs = &trace_bprint_funcs,
1393 /* TRACE_PRINT */
1394 static enum print_line_t trace_print_print(struct trace_iterator *iter,
1395 int flags, struct trace_event *event)
1397 struct print_entry *field;
1398 struct trace_seq *s = &iter->seq;
1400 trace_assign_type(field, iter->ent);
1402 if (!seq_print_ip_sym(s, field->ip, flags))
1403 goto partial;
1405 if (!trace_seq_printf(s, ": %s", field->buf))
1406 goto partial;
1408 return TRACE_TYPE_HANDLED;
1410 partial:
1411 return TRACE_TYPE_PARTIAL_LINE;
1414 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags,
1415 struct trace_event *event)
1417 struct print_entry *field;
1419 trace_assign_type(field, iter->ent);
1421 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
1422 goto partial;
1424 return TRACE_TYPE_HANDLED;
1426 partial:
1427 return TRACE_TYPE_PARTIAL_LINE;
1430 static struct trace_event_functions trace_print_funcs = {
1431 .trace = trace_print_print,
1432 .raw = trace_print_raw,
1435 static struct trace_event trace_print_event = {
1436 .type = TRACE_PRINT,
1437 .funcs = &trace_print_funcs,
1441 static struct trace_event *events[] __initdata = {
1442 &trace_fn_event,
1443 &trace_ctx_event,
1444 &trace_wake_event,
1445 &trace_stack_event,
1446 &trace_user_stack_event,
1447 &trace_bputs_event,
1448 &trace_bprint_event,
1449 &trace_print_event,
1450 NULL
1453 __init static int init_events(void)
1455 struct trace_event *event;
1456 int i, ret;
1458 for (i = 0; events[i]; i++) {
1459 event = events[i];
1461 ret = register_ftrace_event(event);
1462 if (!ret) {
1463 printk(KERN_WARNING "event %d failed to register\n",
1464 event->type);
1465 WARN_ON_ONCE(1);
1469 return 0;
1471 early_initcall(init_events);