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 void trace_print_seq(struct seq_file *m, struct trace_seq *s)
24 int len = s->len >= PAGE_SIZE ? PAGE_SIZE - 1 : s->len;
27 seq_puts(m, s->buffer);
32 enum print_line_t trace_print_bprintk_msg_only(struct trace_iterator *iter)
34 struct trace_seq *s = &iter->seq;
35 struct trace_entry *entry = iter->ent;
36 struct bprint_entry *field;
39 trace_assign_type(field, entry);
41 ret = trace_seq_bprintf(s, field->fmt, field->buf);
43 return TRACE_TYPE_PARTIAL_LINE;
45 return TRACE_TYPE_HANDLED;
48 enum print_line_t trace_print_printk_msg_only(struct trace_iterator *iter)
50 struct trace_seq *s = &iter->seq;
51 struct trace_entry *entry = iter->ent;
52 struct print_entry *field;
55 trace_assign_type(field, entry);
57 ret = trace_seq_printf(s, "%s", field->buf);
59 return TRACE_TYPE_PARTIAL_LINE;
61 return TRACE_TYPE_HANDLED;
65 * trace_seq_printf - sequence printing of trace information
66 * @s: trace sequence descriptor
67 * @fmt: printf format string
69 * The tracer may use either sequence operations or its own
70 * copy to user routines. To simplify formating of a trace
71 * trace_seq_printf is used to store strings into a special
72 * buffer (@s). Then the output may be either used by
73 * the sequencer or pulled into another buffer.
76 trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
78 int len = (PAGE_SIZE - 1) - s->len;
86 ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
89 /* If we can't write it all, don't bother writing anything */
98 int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
100 int len = (PAGE_SIZE - 1) - s->len;
106 ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
108 /* If we can't write it all, don't bother writing anything */
118 * trace_seq_puts - trace sequence printing of simple string
119 * @s: trace sequence descriptor
120 * @str: simple string to record
122 * The tracer may use either the sequence operations or its own
123 * copy to user routines. This function records a simple string
124 * into a special buffer (@s) for later retrieval by a sequencer
125 * or other mechanism.
127 int trace_seq_puts(struct trace_seq *s, const char *str)
129 int len = strlen(str);
131 if (len > ((PAGE_SIZE - 1) - s->len))
134 memcpy(s->buffer + s->len, str, len);
140 int trace_seq_putc(struct trace_seq *s, unsigned char c)
142 if (s->len >= (PAGE_SIZE - 1))
145 s->buffer[s->len++] = c;
150 int trace_seq_putmem(struct trace_seq *s, const void *mem, size_t len)
152 if (len > ((PAGE_SIZE - 1) - s->len))
155 memcpy(s->buffer + s->len, mem, len);
161 int trace_seq_putmem_hex(struct trace_seq *s, const void *mem, size_t len)
163 unsigned char hex[HEX_CHARS];
164 const unsigned char *data = mem;
168 for (i = 0, j = 0; i < len; i++) {
170 for (i = len-1, j = 0; i >= 0; i--) {
172 hex[j++] = hex_asc_hi(data[i]);
173 hex[j++] = hex_asc_lo(data[i]);
177 return trace_seq_putmem(s, hex, j);
180 void *trace_seq_reserve(struct trace_seq *s, size_t len)
184 if (len > ((PAGE_SIZE - 1) - s->len))
187 ret = s->buffer + s->len;
193 int trace_seq_path(struct trace_seq *s, struct path *path)
197 if (s->len >= (PAGE_SIZE - 1))
199 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
201 p = mangle_path(s->buffer + s->len, p, "\n");
203 s->len = p - s->buffer;
207 s->buffer[s->len++] = '?';
214 #ifdef CONFIG_KRETPROBES
215 static inline const char *kretprobed(const char *name)
217 static const char tramp_name[] = "kretprobe_trampoline";
218 int size = sizeof(tramp_name);
220 if (strncmp(tramp_name, name, size) == 0)
221 return "[unknown/kretprobe'd]";
225 static inline const char *kretprobed(const char *name)
229 #endif /* CONFIG_KRETPROBES */
232 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
234 #ifdef CONFIG_KALLSYMS
235 char str[KSYM_SYMBOL_LEN];
238 kallsyms_lookup(address, NULL, NULL, NULL, str);
240 name = kretprobed(str);
242 return trace_seq_printf(s, fmt, name);
248 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
249 unsigned long address)
251 #ifdef CONFIG_KALLSYMS
252 char str[KSYM_SYMBOL_LEN];
255 sprint_symbol(str, address);
256 name = kretprobed(str);
258 return trace_seq_printf(s, fmt, name);
264 # define IP_FMT "%08lx"
266 # define IP_FMT "%016lx"
269 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
270 unsigned long ip, unsigned long sym_flags)
272 struct file *file = NULL;
273 unsigned long vmstart = 0;
277 const struct vm_area_struct *vma;
279 down_read(&mm->mmap_sem);
280 vma = find_vma(mm, ip);
283 vmstart = vma->vm_start;
286 ret = trace_seq_path(s, &file->f_path);
288 ret = trace_seq_printf(s, "[+0x%lx]",
291 up_read(&mm->mmap_sem);
293 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
294 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
299 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
300 unsigned long sym_flags)
302 struct mm_struct *mm = NULL;
306 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
307 struct task_struct *task;
309 * we do the lookup on the thread group leader,
310 * since individual threads might have already quit!
313 task = find_task_by_vpid(entry->ent.tgid);
315 mm = get_task_mm(task);
319 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
320 unsigned long ip = entry->caller[i];
322 if (ip == ULONG_MAX || !ret)
325 ret = trace_seq_puts(s, " <- ");
328 ret = trace_seq_puts(s, "??");
334 ret = seq_print_user_ip(s, mm, ip, sym_flags);
343 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
348 return trace_seq_printf(s, "0");
350 if (sym_flags & TRACE_ITER_SYM_OFFSET)
351 ret = seq_print_sym_offset(s, "%s", ip);
353 ret = seq_print_sym_short(s, "%s", ip);
358 if (sym_flags & TRACE_ITER_SYM_ADDR)
359 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
364 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
366 int hardirq, softirq;
367 char comm[TASK_COMM_LEN];
369 trace_find_cmdline(entry->pid, comm);
370 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
371 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
373 if (!trace_seq_printf(s, "%8.8s-%-5d %3d%c%c%c",
374 comm, entry->pid, cpu,
375 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
376 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
378 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
380 (hardirq && softirq) ? 'H' :
381 hardirq ? 'h' : softirq ? 's' : '.'))
384 if (entry->preempt_count)
385 return trace_seq_printf(s, "%x", entry->preempt_count);
386 return trace_seq_puts(s, ".");
389 static unsigned long preempt_mark_thresh = 100;
392 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
393 unsigned long rel_usecs)
395 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
396 rel_usecs > preempt_mark_thresh ? '!' :
397 rel_usecs > 1 ? '+' : ' ');
400 int trace_print_context(struct trace_iterator *iter)
402 struct trace_seq *s = &iter->seq;
403 struct trace_entry *entry = iter->ent;
404 unsigned long long t = ns2usecs(iter->ts);
405 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
406 unsigned long secs = (unsigned long)t;
407 char comm[TASK_COMM_LEN];
409 trace_find_cmdline(entry->pid, comm);
411 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
412 comm, entry->pid, iter->cpu, secs, usec_rem);
415 int trace_print_lat_context(struct trace_iterator *iter)
419 struct trace_seq *s = &iter->seq;
420 struct trace_entry *entry = iter->ent,
421 *next_entry = trace_find_next_entry(iter, NULL,
423 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
424 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
425 unsigned long rel_usecs;
429 rel_usecs = ns2usecs(next_ts - iter->ts);
432 char comm[TASK_COMM_LEN];
434 trace_find_cmdline(entry->pid, comm);
436 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08llx]"
437 " %ld.%03ldms (+%ld.%03ldms): ", comm,
438 entry->pid, iter->cpu, entry->flags,
439 entry->preempt_count, iter->idx,
441 abs_usecs / USEC_PER_MSEC,
442 abs_usecs % USEC_PER_MSEC,
443 rel_usecs / USEC_PER_MSEC,
444 rel_usecs % USEC_PER_MSEC);
446 ret = lat_print_generic(s, entry, iter->cpu);
448 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
454 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
456 static int task_state_char(unsigned long state)
458 int bit = state ? __ffs(state) + 1 : 0;
460 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
464 * ftrace_find_event - find a registered event
465 * @type: the type of event to look for
467 * Returns an event of type @type otherwise NULL
469 struct trace_event *ftrace_find_event(int type)
471 struct trace_event *event;
472 struct hlist_node *n;
475 key = type & (EVENT_HASHSIZE - 1);
477 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
478 if (event->type == type)
486 * register_ftrace_event - register output for an event type
487 * @event: the event type to register
489 * Event types are stored in a hash and this hash is used to
490 * find a way to print an event. If the @event->type is set
491 * then it will use that type, otherwise it will assign a
494 * If you assign your own type, please make sure it is added
495 * to the trace_type enum in trace.h, to avoid collisions
496 * with the dynamic types.
498 * Returns the event type number or zero on error.
500 int register_ftrace_event(struct trace_event *event)
505 mutex_lock(&trace_event_mutex);
508 ret = next_event_type++;
513 event->type = next_event_type++;
514 else if (event->type > __TRACE_LAST_TYPE) {
515 printk(KERN_WARNING "Need to add type to trace.h\n");
519 if (ftrace_find_event(event->type))
522 if (event->trace == NULL)
523 event->trace = trace_nop_print;
524 if (event->raw == NULL)
525 event->raw = trace_nop_print;
526 if (event->hex == NULL)
527 event->hex = trace_nop_print;
528 if (event->binary == NULL)
529 event->binary = trace_nop_print;
531 key = event->type & (EVENT_HASHSIZE - 1);
533 hlist_add_head_rcu(&event->node, &event_hash[key]);
537 mutex_unlock(&trace_event_mutex);
543 * unregister_ftrace_event - remove a no longer used event
544 * @event: the event to remove
546 int unregister_ftrace_event(struct trace_event *event)
548 mutex_lock(&trace_event_mutex);
549 hlist_del(&event->node);
550 mutex_unlock(&trace_event_mutex);
559 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
561 return TRACE_TYPE_HANDLED;
565 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
567 struct ftrace_entry *field;
568 struct trace_seq *s = &iter->seq;
570 trace_assign_type(field, iter->ent);
572 if (!seq_print_ip_sym(s, field->ip, flags))
575 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
576 if (!trace_seq_printf(s, " <-"))
578 if (!seq_print_ip_sym(s,
583 if (!trace_seq_printf(s, "\n"))
586 return TRACE_TYPE_HANDLED;
589 return TRACE_TYPE_PARTIAL_LINE;
592 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
594 struct ftrace_entry *field;
596 trace_assign_type(field, iter->ent);
598 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
601 return TRACE_TYPE_PARTIAL_LINE;
603 return TRACE_TYPE_HANDLED;
606 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
608 struct ftrace_entry *field;
609 struct trace_seq *s = &iter->seq;
611 trace_assign_type(field, iter->ent);
613 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
614 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
616 return TRACE_TYPE_HANDLED;
619 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
621 struct ftrace_entry *field;
622 struct trace_seq *s = &iter->seq;
624 trace_assign_type(field, iter->ent);
626 SEQ_PUT_FIELD_RET(s, field->ip);
627 SEQ_PUT_FIELD_RET(s, field->parent_ip);
629 return TRACE_TYPE_HANDLED;
632 static struct trace_event trace_fn_event = {
634 .trace = trace_fn_trace,
637 .binary = trace_fn_bin,
640 /* TRACE_CTX an TRACE_WAKE */
641 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
644 struct ctx_switch_entry *field;
645 char comm[TASK_COMM_LEN];
649 trace_assign_type(field, iter->ent);
651 T = task_state_char(field->next_state);
652 S = task_state_char(field->prev_state);
653 trace_find_cmdline(field->next_pid, comm);
654 if (!trace_seq_printf(&iter->seq,
655 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
663 return TRACE_TYPE_PARTIAL_LINE;
665 return TRACE_TYPE_HANDLED;
668 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
670 return trace_ctxwake_print(iter, "==>");
673 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
676 return trace_ctxwake_print(iter, " +");
679 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
681 struct ctx_switch_entry *field;
684 trace_assign_type(field, iter->ent);
687 task_state_char(field->prev_state);
688 T = task_state_char(field->next_state);
689 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
697 return TRACE_TYPE_PARTIAL_LINE;
699 return TRACE_TYPE_HANDLED;
702 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
704 return trace_ctxwake_raw(iter, 0);
707 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
709 return trace_ctxwake_raw(iter, '+');
713 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
715 struct ctx_switch_entry *field;
716 struct trace_seq *s = &iter->seq;
719 trace_assign_type(field, iter->ent);
722 task_state_char(field->prev_state);
723 T = task_state_char(field->next_state);
725 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
726 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
727 SEQ_PUT_HEX_FIELD_RET(s, S);
728 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
729 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
730 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
731 SEQ_PUT_HEX_FIELD_RET(s, T);
733 return TRACE_TYPE_HANDLED;
736 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
738 return trace_ctxwake_hex(iter, 0);
741 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
743 return trace_ctxwake_hex(iter, '+');
746 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
749 struct ctx_switch_entry *field;
750 struct trace_seq *s = &iter->seq;
752 trace_assign_type(field, iter->ent);
754 SEQ_PUT_FIELD_RET(s, field->prev_pid);
755 SEQ_PUT_FIELD_RET(s, field->prev_prio);
756 SEQ_PUT_FIELD_RET(s, field->prev_state);
757 SEQ_PUT_FIELD_RET(s, field->next_pid);
758 SEQ_PUT_FIELD_RET(s, field->next_prio);
759 SEQ_PUT_FIELD_RET(s, field->next_state);
761 return TRACE_TYPE_HANDLED;
764 static struct trace_event trace_ctx_event = {
766 .trace = trace_ctx_print,
767 .raw = trace_ctx_raw,
768 .hex = trace_ctx_hex,
769 .binary = trace_ctxwake_bin,
772 static struct trace_event trace_wake_event = {
774 .trace = trace_wake_print,
775 .raw = trace_wake_raw,
776 .hex = trace_wake_hex,
777 .binary = trace_ctxwake_bin,
781 static enum print_line_t trace_special_print(struct trace_iterator *iter,
784 struct special_entry *field;
786 trace_assign_type(field, iter->ent);
788 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
792 return TRACE_TYPE_PARTIAL_LINE;
794 return TRACE_TYPE_HANDLED;
797 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
800 struct special_entry *field;
801 struct trace_seq *s = &iter->seq;
803 trace_assign_type(field, iter->ent);
805 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
806 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
807 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
809 return TRACE_TYPE_HANDLED;
812 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
815 struct special_entry *field;
816 struct trace_seq *s = &iter->seq;
818 trace_assign_type(field, iter->ent);
820 SEQ_PUT_FIELD_RET(s, field->arg1);
821 SEQ_PUT_FIELD_RET(s, field->arg2);
822 SEQ_PUT_FIELD_RET(s, field->arg3);
824 return TRACE_TYPE_HANDLED;
827 static struct trace_event trace_special_event = {
828 .type = TRACE_SPECIAL,
829 .trace = trace_special_print,
830 .raw = trace_special_print,
831 .hex = trace_special_hex,
832 .binary = trace_special_bin,
837 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
840 struct stack_entry *field;
841 struct trace_seq *s = &iter->seq;
844 trace_assign_type(field, iter->ent);
846 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
848 if (!trace_seq_puts(s, " <= "))
851 if (!seq_print_ip_sym(s, field->caller[i], flags))
854 if (!trace_seq_puts(s, "\n"))
858 return TRACE_TYPE_HANDLED;
861 return TRACE_TYPE_PARTIAL_LINE;
864 static struct trace_event trace_stack_event = {
866 .trace = trace_stack_print,
867 .raw = trace_special_print,
868 .hex = trace_special_hex,
869 .binary = trace_special_bin,
872 /* TRACE_USER_STACK */
873 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
876 struct userstack_entry *field;
877 struct trace_seq *s = &iter->seq;
879 trace_assign_type(field, iter->ent);
881 if (!seq_print_userip_objs(field, s, flags))
884 if (!trace_seq_putc(s, '\n'))
887 return TRACE_TYPE_HANDLED;
890 return TRACE_TYPE_PARTIAL_LINE;
893 static struct trace_event trace_user_stack_event = {
894 .type = TRACE_USER_STACK,
895 .trace = trace_user_stack_print,
896 .raw = trace_special_print,
897 .hex = trace_special_hex,
898 .binary = trace_special_bin,
902 static enum print_line_t
903 trace_bprint_print(struct trace_iterator *iter, int flags)
905 struct trace_entry *entry = iter->ent;
906 struct trace_seq *s = &iter->seq;
907 struct bprint_entry *field;
909 trace_assign_type(field, entry);
911 if (!seq_print_ip_sym(s, field->ip, flags))
914 if (!trace_seq_puts(s, ": "))
917 if (!trace_seq_bprintf(s, field->fmt, field->buf))
920 return TRACE_TYPE_HANDLED;
923 return TRACE_TYPE_PARTIAL_LINE;
927 static enum print_line_t
928 trace_bprint_raw(struct trace_iterator *iter, int flags)
930 struct bprint_entry *field;
931 struct trace_seq *s = &iter->seq;
933 trace_assign_type(field, iter->ent);
935 if (!trace_seq_printf(s, ": %lx : ", field->ip))
938 if (!trace_seq_bprintf(s, field->fmt, field->buf))
941 return TRACE_TYPE_HANDLED;
944 return TRACE_TYPE_PARTIAL_LINE;
948 static struct trace_event trace_bprint_event = {
949 .type = TRACE_BPRINT,
950 .trace = trace_bprint_print,
951 .raw = trace_bprint_raw,
955 static enum print_line_t trace_print_print(struct trace_iterator *iter,
958 struct print_entry *field;
959 struct trace_seq *s = &iter->seq;
961 trace_assign_type(field, iter->ent);
963 if (!seq_print_ip_sym(s, field->ip, flags))
966 if (!trace_seq_printf(s, ": %s", field->buf))
969 return TRACE_TYPE_HANDLED;
972 return TRACE_TYPE_PARTIAL_LINE;
975 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
977 struct print_entry *field;
979 trace_assign_type(field, iter->ent);
981 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
984 return TRACE_TYPE_HANDLED;
987 return TRACE_TYPE_PARTIAL_LINE;
990 static struct trace_event trace_print_event = {
992 .trace = trace_print_print,
993 .raw = trace_print_raw,
997 static struct trace_event *events[] __initdata = {
1001 &trace_special_event,
1003 &trace_user_stack_event,
1004 &trace_bprint_event,
1009 __init static int init_events(void)
1011 struct trace_event *event;
1014 for (i = 0; events[i]; i++) {
1017 ret = register_ftrace_event(event);
1019 printk(KERN_WARNING "event %d failed to register\n",
1027 device_initcall(init_events);