4 * Copyright (C) 2008 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
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 static DEFINE_MUTEX(trace_event_mutex);
18 static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly;
20 static int next_event_type = __TRACE_LAST_TYPE + 1;
23 * trace_seq_printf - sequence printing of trace information
24 * @s: trace sequence descriptor
25 * @fmt: printf format string
27 * The tracer may use either sequence operations or its own
28 * copy to user routines. To simplify formating of a trace
29 * trace_seq_printf is used to store strings into a special
30 * buffer (@s). Then the output may be either used by
31 * the sequencer or pulled into another buffer.
34 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
36 int len = (PAGE_SIZE - 1) - s->len;
44 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
47 /* If we can't write it all, don't bother writing anything */
57 trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
59 int len = (PAGE_SIZE - 1) - s->len;
65 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
67 /* If we can't write it all, don't bother writing anything */
77 * trace_seq_puts - trace sequence printing of simple string
78 * @s: trace sequence descriptor
79 * @str: simple string to record
81 * The tracer may use either the sequence operations or its own
82 * copy to user routines. This function records a simple string
83 * into a special buffer (@s) for later retrieval by a sequencer
86 int trace_seq_puts(struct trace_seq *s, const char *str)
88 int len = strlen(str);
90 if (len > ((PAGE_SIZE - 1) - s->len))
93 memcpy(s->buffer + s->len, str, len);
99 int trace_seq_putc(struct trace_seq *s, unsigned char c)
101 if (s->len >= (PAGE_SIZE - 1))
104 s->buffer[s->len++] = c;
109 int trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
111 if (len > ((PAGE_SIZE - 1) - s->len))
114 memcpy(s->buffer + s->len, mem, len);
120 int trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
122 unsigned char hex[HEX_CHARS];
123 unsigned char *data = mem;
127 for (i = 0, j = 0; i < len; i++) {
129 for (i = len-1, j = 0; i >= 0; i--) {
131 hex[j++] = hex_asc_hi(data[i]);
132 hex[j++] = hex_asc_lo(data[i]);
136 return trace_seq_putmem(s, hex, j);
139 int trace_seq_path(struct trace_seq *s, struct path *path)
143 if (s->len >= (PAGE_SIZE - 1))
145 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
147 p = mangle_path(s->buffer + s->len, p, "\n");
149 s->len = p - s->buffer;
153 s->buffer[s->len++] = '?';
160 #ifdef CONFIG_KRETPROBES
161 static inline const char *kretprobed(const char *name)
163 static const char tramp_name[] = "kretprobe_trampoline";
164 int size = sizeof(tramp_name);
166 if (strncmp(tramp_name, name, size) == 0)
167 return "[unknown/kretprobe'd]";
171 static inline const char *kretprobed(const char *name)
175 #endif /* CONFIG_KRETPROBES */
178 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
180 #ifdef CONFIG_KALLSYMS
181 char str[KSYM_SYMBOL_LEN];
184 kallsyms_lookup(address, NULL, NULL, NULL, str);
186 name = kretprobed(str);
188 return trace_seq_printf(s, fmt, name);
194 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
195 unsigned long address)
197 #ifdef CONFIG_KALLSYMS
198 char str[KSYM_SYMBOL_LEN];
201 sprint_symbol(str, address);
202 name = kretprobed(str);
204 return trace_seq_printf(s, fmt, name);
210 # define IP_FMT "%08lx"
212 # define IP_FMT "%016lx"
215 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
216 unsigned long ip, unsigned long sym_flags)
218 struct file *file = NULL;
219 unsigned long vmstart = 0;
223 const struct vm_area_struct *vma;
225 down_read(&mm->mmap_sem);
226 vma = find_vma(mm, ip);
229 vmstart = vma->vm_start;
232 ret = trace_seq_path(s, &file->f_path);
234 ret = trace_seq_printf(s, "[+0x%lx]",
237 up_read(&mm->mmap_sem);
239 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
240 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
245 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
246 unsigned long sym_flags)
248 struct mm_struct *mm = NULL;
252 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
253 struct task_struct *task;
255 * we do the lookup on the thread group leader,
256 * since individual threads might have already quit!
259 task = find_task_by_vpid(entry->ent.tgid);
261 mm = get_task_mm(task);
265 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
266 unsigned long ip = entry->caller[i];
268 if (ip == ULONG_MAX || !ret)
271 ret = trace_seq_puts(s, " <- ");
274 ret = trace_seq_puts(s, "??");
280 ret = seq_print_user_ip(s, mm, ip, sym_flags);
289 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
294 return trace_seq_printf(s, "0");
296 if (sym_flags & TRACE_ITER_SYM_OFFSET)
297 ret = seq_print_sym_offset(s, "%s", ip);
299 ret = seq_print_sym_short(s, "%s", ip);
304 if (sym_flags & TRACE_ITER_SYM_ADDR)
305 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
310 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
312 int hardirq, softirq;
315 comm = trace_find_cmdline(entry->pid);
316 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
317 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
319 if (!trace_seq_printf(s, "%8.8s-%-5d %3d%c%c%c",
320 comm, entry->pid, cpu,
321 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
322 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
324 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
326 (hardirq && softirq) ? 'H' :
327 hardirq ? 'h' : softirq ? 's' : '.'))
330 if (entry->preempt_count)
331 return trace_seq_printf(s, "%x", entry->preempt_count);
332 return trace_seq_puts(s, ".");
335 static unsigned long preempt_mark_thresh = 100;
338 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
339 unsigned long rel_usecs)
341 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
342 rel_usecs > preempt_mark_thresh ? '!' :
343 rel_usecs > 1 ? '+' : ' ');
346 int trace_print_context(struct trace_iterator *iter)
348 struct trace_seq *s = &iter->seq;
349 struct trace_entry *entry = iter->ent;
350 char *comm = trace_find_cmdline(entry->pid);
351 unsigned long long t = ns2usecs(iter->ts);
352 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
353 unsigned long secs = (unsigned long)t;
355 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
356 comm, entry->pid, iter->cpu, secs, usec_rem);
359 int trace_print_lat_context(struct trace_iterator *iter)
363 struct trace_seq *s = &iter->seq;
364 struct trace_entry *entry = iter->ent,
365 *next_entry = trace_find_next_entry(iter, NULL,
367 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
368 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
369 unsigned long rel_usecs;
373 rel_usecs = ns2usecs(next_ts - iter->ts);
376 char *comm = trace_find_cmdline(entry->pid);
377 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08lx]"
378 " %ld.%03ldms (+%ld.%03ldms): ", comm,
379 entry->pid, iter->cpu, entry->flags,
380 entry->preempt_count, iter->idx,
382 abs_usecs / USEC_PER_MSEC,
383 abs_usecs % USEC_PER_MSEC,
384 rel_usecs / USEC_PER_MSEC,
385 rel_usecs % USEC_PER_MSEC);
387 ret = lat_print_generic(s, entry, iter->cpu);
389 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
395 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
397 static int task_state_char(unsigned long state)
399 int bit = state ? __ffs(state) + 1 : 0;
401 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
405 * ftrace_find_event - find a registered event
406 * @type: the type of event to look for
408 * Returns an event of type @type otherwise NULL
410 struct trace_event *ftrace_find_event(int type)
412 struct trace_event *event;
413 struct hlist_node *n;
416 key = type & (EVENT_HASHSIZE - 1);
418 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
419 if (event->type == type)
427 * register_ftrace_event - register output for an event type
428 * @event: the event type to register
430 * Event types are stored in a hash and this hash is used to
431 * find a way to print an event. If the @event->type is set
432 * then it will use that type, otherwise it will assign a
435 * If you assign your own type, please make sure it is added
436 * to the trace_type enum in trace.h, to avoid collisions
437 * with the dynamic types.
439 * Returns the event type number or zero on error.
441 int register_ftrace_event(struct trace_event *event)
446 mutex_lock(&trace_event_mutex);
449 event->type = next_event_type++;
450 else if (event->type > __TRACE_LAST_TYPE) {
451 printk(KERN_WARNING "Need to add type to trace.h\n");
455 if (ftrace_find_event(event->type))
458 if (event->trace == NULL)
459 event->trace = trace_nop_print;
460 if (event->raw == NULL)
461 event->raw = trace_nop_print;
462 if (event->hex == NULL)
463 event->hex = trace_nop_print;
464 if (event->binary == NULL)
465 event->binary = trace_nop_print;
467 key = event->type & (EVENT_HASHSIZE - 1);
469 hlist_add_head_rcu(&event->node, &event_hash[key]);
473 mutex_unlock(&trace_event_mutex);
479 * unregister_ftrace_event - remove a no longer used event
480 * @event: the event to remove
482 int unregister_ftrace_event(struct trace_event *event)
484 mutex_lock(&trace_event_mutex);
485 hlist_del(&event->node);
486 mutex_unlock(&trace_event_mutex);
495 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
497 return TRACE_TYPE_HANDLED;
501 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
503 struct ftrace_entry *field;
504 struct trace_seq *s = &iter->seq;
506 trace_assign_type(field, iter->ent);
508 if (!seq_print_ip_sym(s, field->ip, flags))
511 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
512 if (!trace_seq_printf(s, " <-"))
514 if (!seq_print_ip_sym(s,
519 if (!trace_seq_printf(s, "\n"))
522 return TRACE_TYPE_HANDLED;
525 return TRACE_TYPE_PARTIAL_LINE;
528 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
530 struct ftrace_entry *field;
532 trace_assign_type(field, iter->ent);
534 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
537 return TRACE_TYPE_PARTIAL_LINE;
539 return TRACE_TYPE_HANDLED;
542 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
544 struct ftrace_entry *field;
545 struct trace_seq *s = &iter->seq;
547 trace_assign_type(field, iter->ent);
549 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
550 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
552 return TRACE_TYPE_HANDLED;
555 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
557 struct ftrace_entry *field;
558 struct trace_seq *s = &iter->seq;
560 trace_assign_type(field, iter->ent);
562 SEQ_PUT_FIELD_RET(s, field->ip);
563 SEQ_PUT_FIELD_RET(s, field->parent_ip);
565 return TRACE_TYPE_HANDLED;
568 static struct trace_event trace_fn_event = {
570 .trace = trace_fn_trace,
573 .binary = trace_fn_bin,
576 /* TRACE_CTX an TRACE_WAKE */
577 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
580 struct ctx_switch_entry *field;
584 trace_assign_type(field, iter->ent);
586 T = task_state_char(field->next_state);
587 S = task_state_char(field->prev_state);
588 comm = trace_find_cmdline(field->next_pid);
589 if (!trace_seq_printf(&iter->seq,
590 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
598 return TRACE_TYPE_PARTIAL_LINE;
600 return TRACE_TYPE_HANDLED;
603 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
605 return trace_ctxwake_print(iter, "==>");
608 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
611 return trace_ctxwake_print(iter, " +");
614 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
616 struct ctx_switch_entry *field;
619 trace_assign_type(field, iter->ent);
622 task_state_char(field->prev_state);
623 T = task_state_char(field->next_state);
624 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
632 return TRACE_TYPE_PARTIAL_LINE;
634 return TRACE_TYPE_HANDLED;
637 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
639 return trace_ctxwake_raw(iter, 0);
642 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
644 return trace_ctxwake_raw(iter, '+');
648 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
650 struct ctx_switch_entry *field;
651 struct trace_seq *s = &iter->seq;
654 trace_assign_type(field, iter->ent);
657 task_state_char(field->prev_state);
658 T = task_state_char(field->next_state);
660 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
661 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
662 SEQ_PUT_HEX_FIELD_RET(s, S);
663 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
664 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
665 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
666 SEQ_PUT_HEX_FIELD_RET(s, T);
668 return TRACE_TYPE_HANDLED;
671 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
673 return trace_ctxwake_hex(iter, 0);
676 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
678 return trace_ctxwake_hex(iter, '+');
681 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
684 struct ctx_switch_entry *field;
685 struct trace_seq *s = &iter->seq;
687 trace_assign_type(field, iter->ent);
689 SEQ_PUT_FIELD_RET(s, field->prev_pid);
690 SEQ_PUT_FIELD_RET(s, field->prev_prio);
691 SEQ_PUT_FIELD_RET(s, field->prev_state);
692 SEQ_PUT_FIELD_RET(s, field->next_pid);
693 SEQ_PUT_FIELD_RET(s, field->next_prio);
694 SEQ_PUT_FIELD_RET(s, field->next_state);
696 return TRACE_TYPE_HANDLED;
699 static struct trace_event trace_ctx_event = {
701 .trace = trace_ctx_print,
702 .raw = trace_ctx_raw,
703 .hex = trace_ctx_hex,
704 .binary = trace_ctxwake_bin,
707 static struct trace_event trace_wake_event = {
709 .trace = trace_wake_print,
710 .raw = trace_wake_raw,
711 .hex = trace_wake_hex,
712 .binary = trace_ctxwake_bin,
716 static enum print_line_t trace_special_print(struct trace_iterator *iter,
719 struct special_entry *field;
721 trace_assign_type(field, iter->ent);
723 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
727 return TRACE_TYPE_PARTIAL_LINE;
729 return TRACE_TYPE_HANDLED;
732 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
735 struct special_entry *field;
736 struct trace_seq *s = &iter->seq;
738 trace_assign_type(field, iter->ent);
740 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
741 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
742 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
744 return TRACE_TYPE_HANDLED;
747 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
750 struct special_entry *field;
751 struct trace_seq *s = &iter->seq;
753 trace_assign_type(field, iter->ent);
755 SEQ_PUT_FIELD_RET(s, field->arg1);
756 SEQ_PUT_FIELD_RET(s, field->arg2);
757 SEQ_PUT_FIELD_RET(s, field->arg3);
759 return TRACE_TYPE_HANDLED;
762 static struct trace_event trace_special_event = {
763 .type = TRACE_SPECIAL,
764 .trace = trace_special_print,
765 .raw = trace_special_print,
766 .hex = trace_special_hex,
767 .binary = trace_special_bin,
772 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
775 struct stack_entry *field;
776 struct trace_seq *s = &iter->seq;
779 trace_assign_type(field, iter->ent);
781 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
783 if (!trace_seq_puts(s, " <= "))
786 if (!seq_print_ip_sym(s, field->caller[i], flags))
789 if (!trace_seq_puts(s, "\n"))
793 return TRACE_TYPE_HANDLED;
796 return TRACE_TYPE_PARTIAL_LINE;
799 static struct trace_event trace_stack_event = {
801 .trace = trace_stack_print,
802 .raw = trace_special_print,
803 .hex = trace_special_hex,
804 .binary = trace_special_bin,
807 /* TRACE_USER_STACK */
808 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
811 struct userstack_entry *field;
812 struct trace_seq *s = &iter->seq;
814 trace_assign_type(field, iter->ent);
816 if (!seq_print_userip_objs(field, s, flags))
819 if (!trace_seq_putc(s, '\n'))
822 return TRACE_TYPE_HANDLED;
825 return TRACE_TYPE_PARTIAL_LINE;
828 static struct trace_event trace_user_stack_event = {
829 .type = TRACE_USER_STACK,
830 .trace = trace_user_stack_print,
831 .raw = trace_special_print,
832 .hex = trace_special_hex,
833 .binary = trace_special_bin,
837 static enum print_line_t trace_print_print(struct trace_iterator *iter,
840 struct print_entry *field;
841 struct trace_seq *s = &iter->seq;
843 trace_assign_type(field, iter->ent);
845 if (!seq_print_ip_sym(s, field->ip, flags))
848 if (!trace_seq_printf(s, ": %s", field->buf))
851 return TRACE_TYPE_HANDLED;
854 return TRACE_TYPE_PARTIAL_LINE;
857 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
859 struct print_entry *field;
861 trace_assign_type(field, iter->ent);
863 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
866 return TRACE_TYPE_HANDLED;
869 return TRACE_TYPE_PARTIAL_LINE;
872 static struct trace_event trace_print_event = {
874 .trace = trace_print_print,
875 .raw = trace_print_raw,
879 static enum print_line_t
880 trace_bprintk_print(struct trace_iterator *iter, int flags)
882 struct trace_entry *entry = iter->ent;
883 struct trace_seq *s = &iter->seq;
884 struct bprintk_entry *field;
886 trace_assign_type(field, entry);
888 if (!seq_print_ip_sym(s, field->ip, flags))
891 if (!trace_seq_puts(s, ": "))
894 if (!trace_seq_bprintf(s, field->fmt, field->buf))
897 return TRACE_TYPE_HANDLED;
900 return TRACE_TYPE_PARTIAL_LINE;
903 static enum print_line_t
904 trace_bprintk_raw(struct trace_iterator *iter, int flags)
906 struct trace_entry *entry = iter->ent;
907 struct trace_seq *s = &iter->seq;
908 struct bprintk_entry *field;
910 trace_assign_type(field, entry);
912 if (!trace_seq_printf(s, ": %lx : ", field->ip))
915 if (!trace_seq_bprintf(s, field->fmt, field->buf))
918 return TRACE_TYPE_HANDLED;
921 return TRACE_TYPE_PARTIAL_LINE;
924 static struct trace_event trace_bprintk_event = {
925 .type = TRACE_BPRINTK,
926 .trace = trace_bprintk_print,
927 .raw = trace_bprintk_raw,
928 .hex = trace_nop_print,
929 .binary = trace_nop_print,
932 static struct trace_event *events[] __initdata = {
936 &trace_special_event,
938 &trace_user_stack_event,
940 &trace_bprintk_event,
944 __init static int init_events(void)
946 struct trace_event *event;
949 for (i = 0; events[i]; i++) {
952 ret = register_ftrace_event(event);
954 printk(KERN_WARNING "event %d failed to register\n",
962 device_initcall(init_events);