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_puts - trace sequence printing of simple string
58 * @s: trace sequence descriptor
59 * @str: simple string to record
61 * The tracer may use either the sequence operations or its own
62 * copy to user routines. This function records a simple string
63 * into a special buffer (@s) for later retrieval by a sequencer
66 int trace_seq_puts(struct trace_seq *s, const char *str)
68 int len = strlen(str);
70 if (len > ((PAGE_SIZE - 1) - s->len))
73 memcpy(s->buffer + s->len, str, len);
79 int trace_seq_putc(struct trace_seq *s, unsigned char c)
81 if (s->len >= (PAGE_SIZE - 1))
84 s->buffer[s->len++] = c;
89 int trace_seq_putmem(struct trace_seq *s, void *mem, size_t len)
91 if (len > ((PAGE_SIZE - 1) - s->len))
94 memcpy(s->buffer + s->len, mem, len);
100 int trace_seq_putmem_hex(struct trace_seq *s, void *mem, size_t len)
102 unsigned char hex[HEX_CHARS];
103 unsigned char *data = mem;
107 for (i = 0, j = 0; i < len; i++) {
109 for (i = len-1, j = 0; i >= 0; i--) {
111 hex[j++] = hex_asc_hi(data[i]);
112 hex[j++] = hex_asc_lo(data[i]);
116 return trace_seq_putmem(s, hex, j);
119 int trace_seq_path(struct trace_seq *s, struct path *path)
123 if (s->len >= (PAGE_SIZE - 1))
125 p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
127 p = mangle_path(s->buffer + s->len, p, "\n");
129 s->len = p - s->buffer;
133 s->buffer[s->len++] = '?';
140 #ifdef CONFIG_KRETPROBES
141 static inline const char *kretprobed(const char *name)
143 static const char tramp_name[] = "kretprobe_trampoline";
144 int size = sizeof(tramp_name);
146 if (strncmp(tramp_name, name, size) == 0)
147 return "[unknown/kretprobe'd]";
151 static inline const char *kretprobed(const char *name)
155 #endif /* CONFIG_KRETPROBES */
158 seq_print_sym_short(struct trace_seq *s, const char *fmt, unsigned long address)
160 #ifdef CONFIG_KALLSYMS
161 char str[KSYM_SYMBOL_LEN];
164 kallsyms_lookup(address, NULL, NULL, NULL, str);
166 name = kretprobed(str);
168 return trace_seq_printf(s, fmt, name);
174 seq_print_sym_offset(struct trace_seq *s, const char *fmt,
175 unsigned long address)
177 #ifdef CONFIG_KALLSYMS
178 char str[KSYM_SYMBOL_LEN];
181 sprint_symbol(str, address);
182 name = kretprobed(str);
184 return trace_seq_printf(s, fmt, name);
190 # define IP_FMT "%08lx"
192 # define IP_FMT "%016lx"
195 int seq_print_user_ip(struct trace_seq *s, struct mm_struct *mm,
196 unsigned long ip, unsigned long sym_flags)
198 struct file *file = NULL;
199 unsigned long vmstart = 0;
203 const struct vm_area_struct *vma;
205 down_read(&mm->mmap_sem);
206 vma = find_vma(mm, ip);
209 vmstart = vma->vm_start;
212 ret = trace_seq_path(s, &file->f_path);
214 ret = trace_seq_printf(s, "[+0x%lx]",
217 up_read(&mm->mmap_sem);
219 if (ret && ((sym_flags & TRACE_ITER_SYM_ADDR) || !file))
220 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
225 seq_print_userip_objs(const struct userstack_entry *entry, struct trace_seq *s,
226 unsigned long sym_flags)
228 struct mm_struct *mm = NULL;
232 if (trace_flags & TRACE_ITER_SYM_USEROBJ) {
233 struct task_struct *task;
235 * we do the lookup on the thread group leader,
236 * since individual threads might have already quit!
239 task = find_task_by_vpid(entry->ent.tgid);
241 mm = get_task_mm(task);
245 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
246 unsigned long ip = entry->caller[i];
248 if (ip == ULONG_MAX || !ret)
251 ret = trace_seq_puts(s, " <- ");
254 ret = trace_seq_puts(s, "??");
260 ret = seq_print_user_ip(s, mm, ip, sym_flags);
269 seq_print_ip_sym(struct trace_seq *s, unsigned long ip, unsigned long sym_flags)
274 return trace_seq_printf(s, "0");
276 if (sym_flags & TRACE_ITER_SYM_OFFSET)
277 ret = seq_print_sym_offset(s, "%s", ip);
279 ret = seq_print_sym_short(s, "%s", ip);
284 if (sym_flags & TRACE_ITER_SYM_ADDR)
285 ret = trace_seq_printf(s, " <" IP_FMT ">", ip);
290 lat_print_generic(struct trace_seq *s, struct trace_entry *entry, int cpu)
292 int hardirq, softirq;
295 comm = trace_find_cmdline(entry->pid);
296 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
297 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
299 if (!trace_seq_printf(s, "%8.8s-%-5d %3d%c%c%c",
300 comm, entry->pid, cpu,
301 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
302 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
304 (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
306 (hardirq && softirq) ? 'H' :
307 hardirq ? 'h' : softirq ? 's' : '.'))
310 if (entry->preempt_count)
311 return trace_seq_printf(s, "%x", entry->preempt_count);
312 return trace_seq_puts(s, ".");
315 static unsigned long preempt_mark_thresh = 100;
318 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
319 unsigned long rel_usecs)
321 return trace_seq_printf(s, " %4lldus%c: ", abs_usecs,
322 rel_usecs > preempt_mark_thresh ? '!' :
323 rel_usecs > 1 ? '+' : ' ');
326 int trace_print_context(struct trace_iterator *iter)
328 struct trace_seq *s = &iter->seq;
329 struct trace_entry *entry = iter->ent;
330 char *comm = trace_find_cmdline(entry->pid);
331 unsigned long long t = ns2usecs(iter->ts);
332 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
333 unsigned long secs = (unsigned long)t;
335 return trace_seq_printf(s, "%16s-%-5d [%03d] %5lu.%06lu: ",
336 comm, entry->pid, iter->cpu, secs, usec_rem);
339 int trace_print_lat_context(struct trace_iterator *iter)
343 struct trace_seq *s = &iter->seq;
344 struct trace_entry *entry = iter->ent,
345 *next_entry = trace_find_next_entry(iter, NULL,
347 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
348 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
349 unsigned long rel_usecs;
353 rel_usecs = ns2usecs(next_ts - iter->ts);
356 char *comm = trace_find_cmdline(entry->pid);
357 ret = trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08lx]"
358 " %ld.%03ldms (+%ld.%03ldms): ", comm,
359 entry->pid, iter->cpu, entry->flags,
360 entry->preempt_count, iter->idx,
362 abs_usecs / USEC_PER_MSEC,
363 abs_usecs % USEC_PER_MSEC,
364 rel_usecs / USEC_PER_MSEC,
365 rel_usecs % USEC_PER_MSEC);
367 ret = lat_print_generic(s, entry, iter->cpu);
369 ret = lat_print_timestamp(s, abs_usecs, rel_usecs);
375 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
377 static int task_state_char(unsigned long state)
379 int bit = state ? __ffs(state) + 1 : 0;
381 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
385 * ftrace_find_event - find a registered event
386 * @type: the type of event to look for
388 * Returns an event of type @type otherwise NULL
390 struct trace_event *ftrace_find_event(int type)
392 struct trace_event *event;
393 struct hlist_node *n;
396 key = type & (EVENT_HASHSIZE - 1);
398 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
399 if (event->type == type)
407 * register_ftrace_event - register output for an event type
408 * @event: the event type to register
410 * Event types are stored in a hash and this hash is used to
411 * find a way to print an event. If the @event->type is set
412 * then it will use that type, otherwise it will assign a
415 * If you assign your own type, please make sure it is added
416 * to the trace_type enum in trace.h, to avoid collisions
417 * with the dynamic types.
419 * Returns the event type number or zero on error.
421 int register_ftrace_event(struct trace_event *event)
426 mutex_lock(&trace_event_mutex);
429 event->type = next_event_type++;
430 else if (event->type > __TRACE_LAST_TYPE) {
431 printk(KERN_WARNING "Need to add type to trace.h\n");
435 if (ftrace_find_event(event->type))
438 if (event->trace == NULL)
439 event->trace = trace_nop_print;
440 if (event->latency_trace == NULL)
441 event->latency_trace = trace_nop_print;
442 if (event->raw == NULL)
443 event->raw = trace_nop_print;
444 if (event->hex == NULL)
445 event->hex = trace_nop_print;
446 if (event->binary == NULL)
447 event->binary = trace_nop_print;
449 key = event->type & (EVENT_HASHSIZE - 1);
451 hlist_add_head_rcu(&event->node, &event_hash[key]);
455 mutex_unlock(&trace_event_mutex);
461 * unregister_ftrace_event - remove a no longer used event
462 * @event: the event to remove
464 int unregister_ftrace_event(struct trace_event *event)
466 mutex_lock(&trace_event_mutex);
467 hlist_del(&event->node);
468 mutex_unlock(&trace_event_mutex);
477 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
479 return TRACE_TYPE_HANDLED;
483 static enum print_line_t trace_fn_latency(struct trace_iterator *iter,
486 struct ftrace_entry *field;
487 struct trace_seq *s = &iter->seq;
489 trace_assign_type(field, iter->ent);
491 if (!seq_print_ip_sym(s, field->ip, flags))
493 if (!trace_seq_puts(s, " ("))
495 if (!seq_print_ip_sym(s, field->parent_ip, flags))
497 if (!trace_seq_puts(s, ")\n"))
500 return TRACE_TYPE_HANDLED;
503 return TRACE_TYPE_PARTIAL_LINE;
506 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
508 struct ftrace_entry *field;
509 struct trace_seq *s = &iter->seq;
511 trace_assign_type(field, iter->ent);
513 if (!seq_print_ip_sym(s, field->ip, flags))
516 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
517 if (!trace_seq_printf(s, " <-"))
519 if (!seq_print_ip_sym(s,
524 if (!trace_seq_printf(s, "\n"))
527 return TRACE_TYPE_HANDLED;
530 return TRACE_TYPE_PARTIAL_LINE;
533 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
535 struct ftrace_entry *field;
537 trace_assign_type(field, iter->ent);
539 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
542 return TRACE_TYPE_PARTIAL_LINE;
544 return TRACE_TYPE_HANDLED;
547 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
549 struct ftrace_entry *field;
550 struct trace_seq *s = &iter->seq;
552 trace_assign_type(field, iter->ent);
554 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
555 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
557 return TRACE_TYPE_HANDLED;
560 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
562 struct ftrace_entry *field;
563 struct trace_seq *s = &iter->seq;
565 trace_assign_type(field, iter->ent);
567 SEQ_PUT_FIELD_RET(s, field->ip);
568 SEQ_PUT_FIELD_RET(s, field->parent_ip);
570 return TRACE_TYPE_HANDLED;
573 static struct trace_event trace_fn_event = {
575 .trace = trace_fn_trace,
576 .latency_trace = trace_fn_latency,
579 .binary = trace_fn_bin,
582 /* TRACE_CTX an TRACE_WAKE */
583 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
586 struct ctx_switch_entry *field;
590 trace_assign_type(field, iter->ent);
592 T = task_state_char(field->next_state);
593 S = task_state_char(field->prev_state);
594 comm = trace_find_cmdline(field->next_pid);
595 if (!trace_seq_printf(&iter->seq,
596 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
604 return TRACE_TYPE_PARTIAL_LINE;
606 return TRACE_TYPE_HANDLED;
609 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
611 return trace_ctxwake_print(iter, "==>");
614 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
617 return trace_ctxwake_print(iter, " +");
620 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
622 struct ctx_switch_entry *field;
625 trace_assign_type(field, iter->ent);
628 task_state_char(field->prev_state);
629 T = task_state_char(field->next_state);
630 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
638 return TRACE_TYPE_PARTIAL_LINE;
640 return TRACE_TYPE_HANDLED;
643 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
645 return trace_ctxwake_raw(iter, 0);
648 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
650 return trace_ctxwake_raw(iter, '+');
654 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
656 struct ctx_switch_entry *field;
657 struct trace_seq *s = &iter->seq;
660 trace_assign_type(field, iter->ent);
663 task_state_char(field->prev_state);
664 T = task_state_char(field->next_state);
666 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
667 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
668 SEQ_PUT_HEX_FIELD_RET(s, S);
669 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
670 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
671 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
672 SEQ_PUT_HEX_FIELD_RET(s, T);
674 return TRACE_TYPE_HANDLED;
677 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
679 return trace_ctxwake_hex(iter, 0);
682 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
684 return trace_ctxwake_hex(iter, '+');
687 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
690 struct ctx_switch_entry *field;
691 struct trace_seq *s = &iter->seq;
693 trace_assign_type(field, iter->ent);
695 SEQ_PUT_FIELD_RET(s, field->prev_pid);
696 SEQ_PUT_FIELD_RET(s, field->prev_prio);
697 SEQ_PUT_FIELD_RET(s, field->prev_state);
698 SEQ_PUT_FIELD_RET(s, field->next_pid);
699 SEQ_PUT_FIELD_RET(s, field->next_prio);
700 SEQ_PUT_FIELD_RET(s, field->next_state);
702 return TRACE_TYPE_HANDLED;
705 static struct trace_event trace_ctx_event = {
707 .trace = trace_ctx_print,
708 .latency_trace = trace_ctx_print,
709 .raw = trace_ctx_raw,
710 .hex = trace_ctx_hex,
711 .binary = trace_ctxwake_bin,
714 static struct trace_event trace_wake_event = {
716 .trace = trace_wake_print,
717 .latency_trace = trace_wake_print,
718 .raw = trace_wake_raw,
719 .hex = trace_wake_hex,
720 .binary = trace_ctxwake_bin,
724 static enum print_line_t trace_special_print(struct trace_iterator *iter,
727 struct special_entry *field;
729 trace_assign_type(field, iter->ent);
731 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
735 return TRACE_TYPE_PARTIAL_LINE;
737 return TRACE_TYPE_HANDLED;
740 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
743 struct special_entry *field;
744 struct trace_seq *s = &iter->seq;
746 trace_assign_type(field, iter->ent);
748 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
749 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
750 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
752 return TRACE_TYPE_HANDLED;
755 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
758 struct special_entry *field;
759 struct trace_seq *s = &iter->seq;
761 trace_assign_type(field, iter->ent);
763 SEQ_PUT_FIELD_RET(s, field->arg1);
764 SEQ_PUT_FIELD_RET(s, field->arg2);
765 SEQ_PUT_FIELD_RET(s, field->arg3);
767 return TRACE_TYPE_HANDLED;
770 static struct trace_event trace_special_event = {
771 .type = TRACE_SPECIAL,
772 .trace = trace_special_print,
773 .latency_trace = trace_special_print,
774 .raw = trace_special_print,
775 .hex = trace_special_hex,
776 .binary = trace_special_bin,
781 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
784 struct stack_entry *field;
785 struct trace_seq *s = &iter->seq;
788 trace_assign_type(field, iter->ent);
790 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
792 if (!trace_seq_puts(s, " <= "))
795 if (!seq_print_ip_sym(s, field->caller[i], flags))
798 if (!trace_seq_puts(s, "\n"))
802 return TRACE_TYPE_HANDLED;
805 return TRACE_TYPE_PARTIAL_LINE;
808 static struct trace_event trace_stack_event = {
810 .trace = trace_stack_print,
811 .latency_trace = trace_stack_print,
812 .raw = trace_special_print,
813 .hex = trace_special_hex,
814 .binary = trace_special_bin,
817 /* TRACE_USER_STACK */
818 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
821 struct userstack_entry *field;
822 struct trace_seq *s = &iter->seq;
824 trace_assign_type(field, iter->ent);
826 if (!seq_print_userip_objs(field, s, flags))
829 if (!trace_seq_putc(s, '\n'))
832 return TRACE_TYPE_HANDLED;
835 return TRACE_TYPE_PARTIAL_LINE;
838 static struct trace_event trace_user_stack_event = {
839 .type = TRACE_USER_STACK,
840 .trace = trace_user_stack_print,
841 .latency_trace = trace_user_stack_print,
842 .raw = trace_special_print,
843 .hex = trace_special_hex,
844 .binary = trace_special_bin,
848 static enum print_line_t trace_print_print(struct trace_iterator *iter,
851 struct print_entry *field;
852 struct trace_seq *s = &iter->seq;
854 trace_assign_type(field, iter->ent);
856 if (!seq_print_ip_sym(s, field->ip, flags))
859 if (!trace_seq_printf(s, ": %s", field->buf))
862 return TRACE_TYPE_HANDLED;
865 return TRACE_TYPE_PARTIAL_LINE;
868 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
870 struct print_entry *field;
872 trace_assign_type(field, iter->ent);
874 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
877 return TRACE_TYPE_HANDLED;
880 return TRACE_TYPE_PARTIAL_LINE;
883 static struct trace_event trace_print_event = {
885 .trace = trace_print_print,
886 .latency_trace = trace_print_print,
887 .raw = trace_print_raw,
890 static struct trace_event *events[] __initdata = {
894 &trace_special_event,
896 &trace_user_stack_event,
901 __init static int init_events(void)
903 struct trace_event *event;
906 for (i = 0; events[i]; i++) {
909 ret = register_ftrace_event(event);
911 printk(KERN_WARNING "event %d failed to register\n",
919 device_initcall(init_events);