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, 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, void *mem, size_t len)
153 unsigned char hex[HEX_CHARS];
154 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 int trace_seq_path(struct trace_seq *s, struct path *path)
174 if (s->len >= (PAGE_SIZE - 1))
176 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
178 p = mangle_path(s->buffer + s->len, p, "\n");
180 s->len = p - s->buffer;
184 s->buffer[s->len++] = '?';
191 #ifdef CONFIG_KRETPROBES
192 static inline const char *kretprobed(const char *name)
194 static const char tramp_name[] = "kretprobe_trampoline";
195 int size = sizeof(tramp_name);
197 if (strncmp(tramp_name, name, size) == 0)
198 return "[unknown/kretprobe'd]";
202 static inline const char *kretprobed(const char *name)
206 #endif /* CONFIG_KRETPROBES */
209 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
211 #ifdef CONFIG_KALLSYMS
212 char str[KSYM_SYMBOL_LEN];
215 kallsyms_lookup(address, NULL, NULL, NULL, str);
217 name = kretprobed(str);
219 return trace_seq_printf(s, fmt, name);
225 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
226 unsigned long address)
228 #ifdef CONFIG_KALLSYMS
229 char str[KSYM_SYMBOL_LEN];
232 sprint_symbol(str, address);
233 name = kretprobed(str);
235 return trace_seq_printf(s, fmt, name);
241 # define IP_FMT "%08lx"
243 # define IP_FMT "%016lx"
246 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
247 unsigned long ip, unsigned long sym_flags)
249 struct file *file = NULL;
250 unsigned long vmstart = 0;
254 const struct vm_area_struct *vma;
256 down_read(&mm->mmap_sem);
257 vma = find_vma(mm, ip);
260 vmstart = vma->vm_start;
263 ret = trace_seq_path(s, &file->f_path);
265 ret = trace_seq_printf(s, "[+0x%lx]",
268 up_read(&mm->mmap_sem);
270 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
271 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
276 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
277 unsigned long sym_flags)
279 struct mm_struct *mm = NULL;
283 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
284 struct task_struct *task;
286 * we do the lookup on the thread group leader,
287 * since individual threads might have already quit!
290 task = find_task_by_vpid(entry->ent.tgid);
292 mm = get_task_mm(task);
296 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
297 unsigned long ip = entry->caller[i];
299 if (ip == ULONG_MAX || !ret)
302 ret = trace_seq_puts(s, " <- ");
305 ret = trace_seq_puts(s, "??");
311 ret = seq_print_user_ip(s, mm, ip, sym_flags);
320 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
325 return trace_seq_printf(s, "0");
327 if (sym_flags & TRACE_ITER_SYM_OFFSET)
328 ret = seq_print_sym_offset(s, "%s", ip);
330 ret = seq_print_sym_short(s, "%s", ip);
335 if (sym_flags & TRACE_ITER_SYM_ADDR)
336 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
341 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
343 int hardirq, softirq;
344 char comm[TASK_COMM_LEN];
346 trace_find_cmdline(entry->pid, comm);
347 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
348 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
350 if (!trace_seq_printf(s, "%8.8s-%-5d %3d%c%c%c",
351 comm, entry->pid, cpu,
352 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
353 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
355 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
357 (hardirq && softirq) ? 'H' :
358 hardirq ? 'h' : softirq ? 's' : '.'))
361 if (entry->preempt_count)
362 return trace_seq_printf(s, "%x", entry->preempt_count);
363 return trace_seq_puts(s, ".");
366 static unsigned long preempt_mark_thresh = 100;
369 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
370 unsigned long rel_usecs)
372 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
373 rel_usecs > preempt_mark_thresh ? '!' :
374 rel_usecs > 1 ? '+' : ' ');
377 int trace_print_context(struct trace_iterator *iter)
379 struct trace_seq *s = &iter->seq;
380 struct trace_entry *entry = iter->ent;
381 unsigned long long t = ns2usecs(iter->ts);
382 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
383 unsigned long secs = (unsigned long)t;
384 char comm[TASK_COMM_LEN];
386 trace_find_cmdline(entry->pid, comm);
388 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
389 comm, entry->pid, iter->cpu, secs, usec_rem);
392 int trace_print_lat_context(struct trace_iterator *iter)
396 struct trace_seq *s = &iter->seq;
397 struct trace_entry *entry = iter->ent,
398 *next_entry = trace_find_next_entry(iter, NULL,
400 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
401 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
402 unsigned long rel_usecs;
406 rel_usecs = ns2usecs(next_ts - iter->ts);
409 char comm[TASK_COMM_LEN];
411 trace_find_cmdline(entry->pid, comm);
413 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08lx]"
414 " %ld.%03ldms (+%ld.%03ldms): ", comm,
415 entry->pid, iter->cpu, entry->flags,
416 entry->preempt_count, iter->idx,
418 abs_usecs / USEC_PER_MSEC,
419 abs_usecs % USEC_PER_MSEC,
420 rel_usecs / USEC_PER_MSEC,
421 rel_usecs % USEC_PER_MSEC);
423 ret = lat_print_generic(s, entry, iter->cpu);
425 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
431 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
433 static int task_state_char(unsigned long state)
435 int bit = state ? __ffs(state) + 1 : 0;
437 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
441 * ftrace_find_event - find a registered event
442 * @type: the type of event to look for
444 * Returns an event of type @type otherwise NULL
446 struct trace_event *ftrace_find_event(int type)
448 struct trace_event *event;
449 struct hlist_node *n;
452 key = type & (EVENT_HASHSIZE - 1);
454 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
455 if (event->type == type)
463 * register_ftrace_event - register output for an event type
464 * @event: the event type to register
466 * Event types are stored in a hash and this hash is used to
467 * find a way to print an event. If the @event->type is set
468 * then it will use that type, otherwise it will assign a
471 * If you assign your own type, please make sure it is added
472 * to the trace_type enum in trace.h, to avoid collisions
473 * with the dynamic types.
475 * Returns the event type number or zero on error.
477 int register_ftrace_event(struct trace_event *event)
482 mutex_lock(&trace_event_mutex);
485 ret = next_event_type++;
490 event->type = next_event_type++;
491 else if (event->type > __TRACE_LAST_TYPE) {
492 printk(KERN_WARNING "Need to add type to trace.h\n");
496 if (ftrace_find_event(event->type))
499 if (event->trace == NULL)
500 event->trace = trace_nop_print;
501 if (event->raw == NULL)
502 event->raw = trace_nop_print;
503 if (event->hex == NULL)
504 event->hex = trace_nop_print;
505 if (event->binary == NULL)
506 event->binary = trace_nop_print;
508 key = event->type & (EVENT_HASHSIZE - 1);
510 hlist_add_head_rcu(&event->node, &event_hash[key]);
514 mutex_unlock(&trace_event_mutex);
520 * unregister_ftrace_event - remove a no longer used event
521 * @event: the event to remove
523 int unregister_ftrace_event(struct trace_event *event)
525 mutex_lock(&trace_event_mutex);
526 hlist_del(&event->node);
527 mutex_unlock(&trace_event_mutex);
536 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
538 return TRACE_TYPE_HANDLED;
542 static enum print_line_t trace_fn_trace(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 if (!seq_print_ip_sym(s, field->ip, flags))
552 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
553 if (!trace_seq_printf(s, " <-"))
555 if (!seq_print_ip_sym(s,
560 if (!trace_seq_printf(s, "\n"))
563 return TRACE_TYPE_HANDLED;
566 return TRACE_TYPE_PARTIAL_LINE;
569 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
571 struct ftrace_entry *field;
573 trace_assign_type(field, iter->ent);
575 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
578 return TRACE_TYPE_PARTIAL_LINE;
580 return TRACE_TYPE_HANDLED;
583 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
585 struct ftrace_entry *field;
586 struct trace_seq *s = &iter->seq;
588 trace_assign_type(field, iter->ent);
590 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
591 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
593 return TRACE_TYPE_HANDLED;
596 static enum print_line_t trace_fn_bin(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_FIELD_RET(s, field->ip);
604 SEQ_PUT_FIELD_RET(s, field->parent_ip);
606 return TRACE_TYPE_HANDLED;
609 static struct trace_event trace_fn_event = {
611 .trace = trace_fn_trace,
614 .binary = trace_fn_bin,
617 /* TRACE_CTX an TRACE_WAKE */
618 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
621 struct ctx_switch_entry *field;
622 char comm[TASK_COMM_LEN];
626 trace_assign_type(field, iter->ent);
628 T = task_state_char(field->next_state);
629 S = task_state_char(field->prev_state);
630 trace_find_cmdline(field->next_pid, comm);
631 if (!trace_seq_printf(&iter->seq,
632 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
640 return TRACE_TYPE_PARTIAL_LINE;
642 return TRACE_TYPE_HANDLED;
645 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
647 return trace_ctxwake_print(iter, "==>");
650 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
653 return trace_ctxwake_print(iter, " +");
656 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
658 struct ctx_switch_entry *field;
661 trace_assign_type(field, iter->ent);
664 task_state_char(field->prev_state);
665 T = task_state_char(field->next_state);
666 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
674 return TRACE_TYPE_PARTIAL_LINE;
676 return TRACE_TYPE_HANDLED;
679 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
681 return trace_ctxwake_raw(iter, 0);
684 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
686 return trace_ctxwake_raw(iter, '+');
690 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
692 struct ctx_switch_entry *field;
693 struct trace_seq *s = &iter->seq;
696 trace_assign_type(field, iter->ent);
699 task_state_char(field->prev_state);
700 T = task_state_char(field->next_state);
702 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
703 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
704 SEQ_PUT_HEX_FIELD_RET(s, S);
705 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
706 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
707 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
708 SEQ_PUT_HEX_FIELD_RET(s, T);
710 return TRACE_TYPE_HANDLED;
713 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
715 return trace_ctxwake_hex(iter, 0);
718 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
720 return trace_ctxwake_hex(iter, '+');
723 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
726 struct ctx_switch_entry *field;
727 struct trace_seq *s = &iter->seq;
729 trace_assign_type(field, iter->ent);
731 SEQ_PUT_FIELD_RET(s, field->prev_pid);
732 SEQ_PUT_FIELD_RET(s, field->prev_prio);
733 SEQ_PUT_FIELD_RET(s, field->prev_state);
734 SEQ_PUT_FIELD_RET(s, field->next_pid);
735 SEQ_PUT_FIELD_RET(s, field->next_prio);
736 SEQ_PUT_FIELD_RET(s, field->next_state);
738 return TRACE_TYPE_HANDLED;
741 static struct trace_event trace_ctx_event = {
743 .trace = trace_ctx_print,
744 .raw = trace_ctx_raw,
745 .hex = trace_ctx_hex,
746 .binary = trace_ctxwake_bin,
749 static struct trace_event trace_wake_event = {
751 .trace = trace_wake_print,
752 .raw = trace_wake_raw,
753 .hex = trace_wake_hex,
754 .binary = trace_ctxwake_bin,
758 static enum print_line_t trace_special_print(struct trace_iterator *iter,
761 struct special_entry *field;
763 trace_assign_type(field, iter->ent);
765 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
769 return TRACE_TYPE_PARTIAL_LINE;
771 return TRACE_TYPE_HANDLED;
774 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
777 struct special_entry *field;
778 struct trace_seq *s = &iter->seq;
780 trace_assign_type(field, iter->ent);
782 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
783 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
784 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
786 return TRACE_TYPE_HANDLED;
789 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
792 struct special_entry *field;
793 struct trace_seq *s = &iter->seq;
795 trace_assign_type(field, iter->ent);
797 SEQ_PUT_FIELD_RET(s, field->arg1);
798 SEQ_PUT_FIELD_RET(s, field->arg2);
799 SEQ_PUT_FIELD_RET(s, field->arg3);
801 return TRACE_TYPE_HANDLED;
804 static struct trace_event trace_special_event = {
805 .type = TRACE_SPECIAL,
806 .trace = trace_special_print,
807 .raw = trace_special_print,
808 .hex = trace_special_hex,
809 .binary = trace_special_bin,
814 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
817 struct stack_entry *field;
818 struct trace_seq *s = &iter->seq;
821 trace_assign_type(field, iter->ent);
823 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
825 if (!trace_seq_puts(s, " <= "))
828 if (!seq_print_ip_sym(s, field->caller[i], flags))
831 if (!trace_seq_puts(s, "\n"))
835 return TRACE_TYPE_HANDLED;
838 return TRACE_TYPE_PARTIAL_LINE;
841 static struct trace_event trace_stack_event = {
843 .trace = trace_stack_print,
844 .raw = trace_special_print,
845 .hex = trace_special_hex,
846 .binary = trace_special_bin,
849 /* TRACE_USER_STACK */
850 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
853 struct userstack_entry *field;
854 struct trace_seq *s = &iter->seq;
856 trace_assign_type(field, iter->ent);
858 if (!seq_print_userip_objs(field, s, flags))
861 if (!trace_seq_putc(s, '\n'))
864 return TRACE_TYPE_HANDLED;
867 return TRACE_TYPE_PARTIAL_LINE;
870 static struct trace_event trace_user_stack_event = {
871 .type = TRACE_USER_STACK,
872 .trace = trace_user_stack_print,
873 .raw = trace_special_print,
874 .hex = trace_special_hex,
875 .binary = trace_special_bin,
879 static enum print_line_t
880 trace_bprint_print(struct trace_iterator *iter, int flags)
882 struct trace_entry *entry = iter->ent;
883 struct trace_seq *s = &iter->seq;
884 struct bprint_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;
904 static enum print_line_t
905 trace_bprint_raw(struct trace_iterator *iter, int flags)
907 struct bprint_entry *field;
908 struct trace_seq *s = &iter->seq;
910 trace_assign_type(field, iter->ent);
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;
925 static struct trace_event trace_bprint_event = {
926 .type = TRACE_BPRINT,
927 .trace = trace_bprint_print,
928 .raw = trace_bprint_raw,
932 static enum print_line_t trace_print_print(struct trace_iterator *iter,
935 struct print_entry *field;
936 struct trace_seq *s = &iter->seq;
938 trace_assign_type(field, iter->ent);
940 if (!seq_print_ip_sym(s, field->ip, flags))
943 if (!trace_seq_printf(s, ": %s", field->buf))
946 return TRACE_TYPE_HANDLED;
949 return TRACE_TYPE_PARTIAL_LINE;
952 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
954 struct print_entry *field;
956 trace_assign_type(field, iter->ent);
958 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
961 return TRACE_TYPE_HANDLED;
964 return TRACE_TYPE_PARTIAL_LINE;
967 static struct trace_event trace_print_event = {
969 .trace = trace_print_print,
970 .raw = trace_print_raw,
974 static struct trace_event *events[] __initdata = {
978 &trace_special_event,
980 &trace_user_stack_event,
986 __init static int init_events(void)
988 struct trace_event *event;
991 for (i = 0; events[i]; i++) {
994 ret = register_ftrace_event(event);
996 printk(KERN_WARNING "event %d failed to register\n",
1004 device_initcall(init_events);