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;
22 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
24 struct trace_seq *s = &iter->seq;
25 struct trace_entry *entry = iter->ent;
26 struct bprint_entry *field;
29 trace_assign_type(field, entry);
31 ret = trace_seq_bprintf(s, field->fmt, field->buf);
33 return TRACE_TYPE_PARTIAL_LINE;
35 return TRACE_TYPE_HANDLED;
38 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
40 struct trace_seq *s = &iter->seq;
41 struct trace_entry *entry = iter->ent;
42 struct print_entry *field;
45 trace_assign_type(field, entry);
47 ret = trace_seq_printf(s, "%s", field->buf);
49 return TRACE_TYPE_PARTIAL_LINE;
51 return TRACE_TYPE_HANDLED;
55 * trace_seq_printf - sequence printing of trace information
56 * @s: trace sequence descriptor
57 * @fmt: printf format string
59 * The tracer may use either sequence operations or its own
60 * copy to user routines. To simplify formating of a trace
61 * trace_seq_printf is used to store strings into a special
62 * buffer (@s). Then the output may be either used by
63 * the sequencer or pulled into another buffer.
66 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
68 int len = (PAGE_SIZE - 1) - s->len;
76 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
79 /* If we can't write it all, don't bother writing anything */
88 int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
90 int len = (PAGE_SIZE - 1) - s->len;
96 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
98 /* If we can't write it all, don't bother writing anything */
108 * trace_seq_puts - trace sequence printing of simple string
109 * @s: trace sequence descriptor
110 * @str: simple string to record
112 * The tracer may use either the sequence operations or its own
113 * copy to user routines. This function records a simple string
114 * into a special buffer (@s) for later retrieval by a sequencer
115 * or other mechanism.
117 int trace_seq_puts(struct trace_seq *s, const char *str)
119 int len = strlen(str);
121 if (len > ((PAGE_SIZE - 1) - s->len))
124 memcpy(s->buffer + s->len, str, len);
130 int trace_seq_putc(struct trace_seq *s, unsigned char c)
132 if (s->len >= (PAGE_SIZE - 1))
135 s->buffer[s->len++] = c;
140 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
142 if (len > ((PAGE_SIZE - 1) - s->len))
145 memcpy(s->buffer + s->len, mem, len);
151 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
153 unsigned char hex[HEX_CHARS];
154 const unsigned char *data = mem;
158 for (i = 0, j = 0; i < len; i++) {
160 for (i = len-1, j = 0; i >= 0; i--) {
162 hex[j++] = hex_asc_hi(data[i]);
163 hex[j++] = hex_asc_lo(data[i]);
167 return trace_seq_putmem(s, hex, j);
170 void *trace_seq_reserve(struct trace_seq *s, size_t len)
174 if (len > ((PAGE_SIZE - 1) - s->len))
177 ret = s->buffer + s->len;
183 int trace_seq_path(struct trace_seq *s, struct path *path)
187 if (s->len >= (PAGE_SIZE - 1))
189 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
191 p = mangle_path(s->buffer + s->len, p, "\n");
193 s->len = p - s->buffer;
197 s->buffer[s->len++] = '?';
204 #ifdef CONFIG_KRETPROBES
205 static inline const char *kretprobed(const char *name)
207 static const char tramp_name[] = "kretprobe_trampoline";
208 int size = sizeof(tramp_name);
210 if (strncmp(tramp_name, name, size) == 0)
211 return "[unknown/kretprobe'd]";
215 static inline const char *kretprobed(const char *name)
219 #endif /* CONFIG_KRETPROBES */
222 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
224 #ifdef CONFIG_KALLSYMS
225 char str[KSYM_SYMBOL_LEN];
228 kallsyms_lookup(address, NULL, NULL, NULL, str);
230 name = kretprobed(str);
232 return trace_seq_printf(s, fmt, name);
238 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
239 unsigned long address)
241 #ifdef CONFIG_KALLSYMS
242 char str[KSYM_SYMBOL_LEN];
245 sprint_symbol(str, address);
246 name = kretprobed(str);
248 return trace_seq_printf(s, fmt, name);
254 # define IP_FMT "%08lx"
256 # define IP_FMT "%016lx"
259 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
260 unsigned long ip, unsigned long sym_flags)
262 struct file *file = NULL;
263 unsigned long vmstart = 0;
267 const struct vm_area_struct *vma;
269 down_read(&mm->mmap_sem);
270 vma = find_vma(mm, ip);
273 vmstart = vma->vm_start;
276 ret = trace_seq_path(s, &file->f_path);
278 ret = trace_seq_printf(s, "[+0x%lx]",
281 up_read(&mm->mmap_sem);
283 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
284 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
289 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
290 unsigned long sym_flags)
292 struct mm_struct *mm = NULL;
296 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
297 struct task_struct *task;
299 * we do the lookup on the thread group leader,
300 * since individual threads might have already quit!
303 task = find_task_by_vpid(entry->ent.tgid);
305 mm = get_task_mm(task);
309 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
310 unsigned long ip = entry->caller[i];
312 if (ip == ULONG_MAX || !ret)
315 ret = trace_seq_puts(s, " <- ");
318 ret = trace_seq_puts(s, "??");
324 ret = seq_print_user_ip(s, mm, ip, sym_flags);
333 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
338 return trace_seq_printf(s, "0");
340 if (sym_flags & TRACE_ITER_SYM_OFFSET)
341 ret = seq_print_sym_offset(s, "%s", ip);
343 ret = seq_print_sym_short(s, "%s", ip);
348 if (sym_flags & TRACE_ITER_SYM_ADDR)
349 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
354 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
356 int hardirq, softirq;
357 char comm[TASK_COMM_LEN];
359 trace_find_cmdline(entry->pid, comm);
360 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
361 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
363 if (!trace_seq_printf(s, "%8.8s-%-5d %3d%c%c%c",
364 comm, entry->pid, cpu,
365 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
366 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
368 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
370 (hardirq && softirq) ? 'H' :
371 hardirq ? 'h' : softirq ? 's' : '.'))
374 if (entry->preempt_count)
375 return trace_seq_printf(s, "%x", entry->preempt_count);
376 return trace_seq_puts(s, ".");
379 static unsigned long preempt_mark_thresh = 100;
382 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
383 unsigned long rel_usecs)
385 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
386 rel_usecs > preempt_mark_thresh ? '!' :
387 rel_usecs > 1 ? '+' : ' ');
390 int trace_print_context(struct trace_iterator *iter)
392 struct trace_seq *s = &iter->seq;
393 struct trace_entry *entry = iter->ent;
394 unsigned long long t = ns2usecs(iter->ts);
395 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
396 unsigned long secs = (unsigned long)t;
397 char comm[TASK_COMM_LEN];
399 trace_find_cmdline(entry->pid, comm);
401 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
402 comm, entry->pid, iter->cpu, secs, usec_rem);
405 int trace_print_lat_context(struct trace_iterator *iter)
409 struct trace_seq *s = &iter->seq;
410 struct trace_entry *entry = iter->ent,
411 *next_entry = trace_find_next_entry(iter, NULL,
413 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
414 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
415 unsigned long rel_usecs;
419 rel_usecs = ns2usecs(next_ts - iter->ts);
422 char comm[TASK_COMM_LEN];
424 trace_find_cmdline(entry->pid, comm);
426 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08lx]"
427 " %ld.%03ldms (+%ld.%03ldms): ", comm,
428 entry->pid, iter->cpu, entry->flags,
429 entry->preempt_count, iter->idx,
431 abs_usecs / USEC_PER_MSEC,
432 abs_usecs % USEC_PER_MSEC,
433 rel_usecs / USEC_PER_MSEC,
434 rel_usecs % USEC_PER_MSEC);
436 ret = lat_print_generic(s, entry, iter->cpu);
438 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
444 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
446 static int task_state_char(unsigned long state)
448 int bit = state ? __ffs(state) + 1 : 0;
450 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
454 * ftrace_find_event - find a registered event
455 * @type: the type of event to look for
457 * Returns an event of type @type otherwise NULL
459 struct trace_event *ftrace_find_event(int type)
461 struct trace_event *event;
462 struct hlist_node *n;
465 key = type & (EVENT_HASHSIZE - 1);
467 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
468 if (event->type == type)
476 * register_ftrace_event - register output for an event type
477 * @event: the event type to register
479 * Event types are stored in a hash and this hash is used to
480 * find a way to print an event. If the @event->type is set
481 * then it will use that type, otherwise it will assign a
484 * If you assign your own type, please make sure it is added
485 * to the trace_type enum in trace.h, to avoid collisions
486 * with the dynamic types.
488 * Returns the event type number or zero on error.
490 int register_ftrace_event(struct trace_event *event)
495 mutex_lock(&trace_event_mutex);
498 ret = next_event_type++;
503 event->type = next_event_type++;
504 else if (event->type > __TRACE_LAST_TYPE) {
505 printk(KERN_WARNING "Need to add type to trace.h\n");
509 if (ftrace_find_event(event->type))
512 if (event->trace == NULL)
513 event->trace = trace_nop_print;
514 if (event->raw == NULL)
515 event->raw = trace_nop_print;
516 if (event->hex == NULL)
517 event->hex = trace_nop_print;
518 if (event->binary == NULL)
519 event->binary = trace_nop_print;
521 key = event->type & (EVENT_HASHSIZE - 1);
523 hlist_add_head_rcu(&event->node, &event_hash[key]);
527 mutex_unlock(&trace_event_mutex);
533 * unregister_ftrace_event - remove a no longer used event
534 * @event: the event to remove
536 int unregister_ftrace_event(struct trace_event *event)
538 mutex_lock(&trace_event_mutex);
539 hlist_del(&event->node);
540 mutex_unlock(&trace_event_mutex);
549 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
551 return TRACE_TYPE_HANDLED;
555 static enum print_line_t trace_fn_trace(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 if (!seq_print_ip_sym(s, field->ip, flags))
565 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
566 if (!trace_seq_printf(s, " <-"))
568 if (!seq_print_ip_sym(s,
573 if (!trace_seq_printf(s, "\n"))
576 return TRACE_TYPE_HANDLED;
579 return TRACE_TYPE_PARTIAL_LINE;
582 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
584 struct ftrace_entry *field;
586 trace_assign_type(field, iter->ent);
588 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
591 return TRACE_TYPE_PARTIAL_LINE;
593 return TRACE_TYPE_HANDLED;
596 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
598 struct ftrace_entry *field;
599 struct trace_seq *s = &iter->seq;
601 trace_assign_type(field, iter->ent);
603 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
604 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
606 return TRACE_TYPE_HANDLED;
609 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
611 struct ftrace_entry *field;
612 struct trace_seq *s = &iter->seq;
614 trace_assign_type(field, iter->ent);
616 SEQ_PUT_FIELD_RET(s, field->ip);
617 SEQ_PUT_FIELD_RET(s, field->parent_ip);
619 return TRACE_TYPE_HANDLED;
622 static struct trace_event trace_fn_event = {
624 .trace = trace_fn_trace,
627 .binary = trace_fn_bin,
630 /* TRACE_CTX an TRACE_WAKE */
631 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
634 struct ctx_switch_entry *field;
635 char comm[TASK_COMM_LEN];
639 trace_assign_type(field, iter->ent);
641 T = task_state_char(field->next_state);
642 S = task_state_char(field->prev_state);
643 trace_find_cmdline(field->next_pid, comm);
644 if (!trace_seq_printf(&iter->seq,
645 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
653 return TRACE_TYPE_PARTIAL_LINE;
655 return TRACE_TYPE_HANDLED;
658 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
660 return trace_ctxwake_print(iter, "==>");
663 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
666 return trace_ctxwake_print(iter, " +");
669 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
671 struct ctx_switch_entry *field;
674 trace_assign_type(field, iter->ent);
677 task_state_char(field->prev_state);
678 T = task_state_char(field->next_state);
679 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
687 return TRACE_TYPE_PARTIAL_LINE;
689 return TRACE_TYPE_HANDLED;
692 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
694 return trace_ctxwake_raw(iter, 0);
697 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
699 return trace_ctxwake_raw(iter, '+');
703 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
705 struct ctx_switch_entry *field;
706 struct trace_seq *s = &iter->seq;
709 trace_assign_type(field, iter->ent);
712 task_state_char(field->prev_state);
713 T = task_state_char(field->next_state);
715 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
716 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
717 SEQ_PUT_HEX_FIELD_RET(s, S);
718 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
719 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
720 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
721 SEQ_PUT_HEX_FIELD_RET(s, T);
723 return TRACE_TYPE_HANDLED;
726 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
728 return trace_ctxwake_hex(iter, 0);
731 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
733 return trace_ctxwake_hex(iter, '+');
736 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
739 struct ctx_switch_entry *field;
740 struct trace_seq *s = &iter->seq;
742 trace_assign_type(field, iter->ent);
744 SEQ_PUT_FIELD_RET(s, field->prev_pid);
745 SEQ_PUT_FIELD_RET(s, field->prev_prio);
746 SEQ_PUT_FIELD_RET(s, field->prev_state);
747 SEQ_PUT_FIELD_RET(s, field->next_pid);
748 SEQ_PUT_FIELD_RET(s, field->next_prio);
749 SEQ_PUT_FIELD_RET(s, field->next_state);
751 return TRACE_TYPE_HANDLED;
754 static struct trace_event trace_ctx_event = {
756 .trace = trace_ctx_print,
757 .raw = trace_ctx_raw,
758 .hex = trace_ctx_hex,
759 .binary = trace_ctxwake_bin,
762 static struct trace_event trace_wake_event = {
764 .trace = trace_wake_print,
765 .raw = trace_wake_raw,
766 .hex = trace_wake_hex,
767 .binary = trace_ctxwake_bin,
771 static enum print_line_t trace_special_print(struct trace_iterator *iter,
774 struct special_entry *field;
776 trace_assign_type(field, iter->ent);
778 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
782 return TRACE_TYPE_PARTIAL_LINE;
784 return TRACE_TYPE_HANDLED;
787 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
790 struct special_entry *field;
791 struct trace_seq *s = &iter->seq;
793 trace_assign_type(field, iter->ent);
795 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
796 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
797 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
799 return TRACE_TYPE_HANDLED;
802 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
805 struct special_entry *field;
806 struct trace_seq *s = &iter->seq;
808 trace_assign_type(field, iter->ent);
810 SEQ_PUT_FIELD_RET(s, field->arg1);
811 SEQ_PUT_FIELD_RET(s, field->arg2);
812 SEQ_PUT_FIELD_RET(s, field->arg3);
814 return TRACE_TYPE_HANDLED;
817 static struct trace_event trace_special_event = {
818 .type = TRACE_SPECIAL,
819 .trace = trace_special_print,
820 .raw = trace_special_print,
821 .hex = trace_special_hex,
822 .binary = trace_special_bin,
827 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
830 struct stack_entry *field;
831 struct trace_seq *s = &iter->seq;
834 trace_assign_type(field, iter->ent);
836 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
838 if (!trace_seq_puts(s, " <= "))
841 if (!seq_print_ip_sym(s, field->caller[i], flags))
844 if (!trace_seq_puts(s, "\n"))
848 return TRACE_TYPE_HANDLED;
851 return TRACE_TYPE_PARTIAL_LINE;
854 static struct trace_event trace_stack_event = {
856 .trace = trace_stack_print,
857 .raw = trace_special_print,
858 .hex = trace_special_hex,
859 .binary = trace_special_bin,
862 /* TRACE_USER_STACK */
863 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
866 struct userstack_entry *field;
867 struct trace_seq *s = &iter->seq;
869 trace_assign_type(field, iter->ent);
871 if (!seq_print_userip_objs(field, s, flags))
874 if (!trace_seq_putc(s, '\n'))
877 return TRACE_TYPE_HANDLED;
880 return TRACE_TYPE_PARTIAL_LINE;
883 static struct trace_event trace_user_stack_event = {
884 .type = TRACE_USER_STACK,
885 .trace = trace_user_stack_print,
886 .raw = trace_special_print,
887 .hex = trace_special_hex,
888 .binary = trace_special_bin,
892 static enum print_line_t
893 trace_bprint_print(struct trace_iterator *iter, int flags)
895 struct trace_entry *entry = iter->ent;
896 struct trace_seq *s = &iter->seq;
897 struct bprint_entry *field;
899 trace_assign_type(field, entry);
901 if (!seq_print_ip_sym(s, field->ip, flags))
904 if (!trace_seq_puts(s, ": "))
907 if (!trace_seq_bprintf(s, field->fmt, field->buf))
910 return TRACE_TYPE_HANDLED;
913 return TRACE_TYPE_PARTIAL_LINE;
917 static enum print_line_t
918 trace_bprint_raw(struct trace_iterator *iter, int flags)
920 struct bprint_entry *field;
921 struct trace_seq *s = &iter->seq;
923 trace_assign_type(field, iter->ent);
925 if (!trace_seq_printf(s, ": %lx : ", field->ip))
928 if (!trace_seq_bprintf(s, field->fmt, field->buf))
931 return TRACE_TYPE_HANDLED;
934 return TRACE_TYPE_PARTIAL_LINE;
938 static struct trace_event trace_bprint_event = {
939 .type = TRACE_BPRINT,
940 .trace = trace_bprint_print,
941 .raw = trace_bprint_raw,
945 static enum print_line_t trace_print_print(struct trace_iterator *iter,
948 struct print_entry *field;
949 struct trace_seq *s = &iter->seq;
951 trace_assign_type(field, iter->ent);
953 if (!seq_print_ip_sym(s, field->ip, flags))
956 if (!trace_seq_printf(s, ": %s", field->buf))
959 return TRACE_TYPE_HANDLED;
962 return TRACE_TYPE_PARTIAL_LINE;
965 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
967 struct print_entry *field;
969 trace_assign_type(field, iter->ent);
971 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
974 return TRACE_TYPE_HANDLED;
977 return TRACE_TYPE_PARTIAL_LINE;
980 static struct trace_event trace_print_event = {
982 .trace = trace_print_print,
983 .raw = trace_print_raw,
987 static struct trace_event *events[] __initdata = {
991 &trace_special_event,
993 &trace_user_stack_event,
999 __init static int init_events(void)
1001 struct trace_event *event;
1004 for (i = 0; events[i]; i++) {
1007 ret = register_ftrace_event(event);
1009 printk(KERN_WARNING "event %d failed to register\n",
1017 device_initcall(init_events);