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->raw == NULL)
441 event->raw = trace_nop_print;
442 if (event->hex == NULL)
443 event->hex = trace_nop_print;
444 if (event->binary == NULL)
445 event->binary = trace_nop_print;
447 key = event->type & (EVENT_HASHSIZE - 1);
449 hlist_add_head_rcu(&event->node, &event_hash[key]);
453 mutex_unlock(&trace_event_mutex);
459 * unregister_ftrace_event - remove a no longer used event
460 * @event: the event to remove
462 int unregister_ftrace_event(struct trace_event *event)
464 mutex_lock(&trace_event_mutex);
465 hlist_del(&event->node);
466 mutex_unlock(&trace_event_mutex);
475 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
477 return TRACE_TYPE_HANDLED;
481 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
483 struct ftrace_entry *field;
484 struct trace_seq *s = &iter->seq;
486 trace_assign_type(field, iter->ent);
488 if (!seq_print_ip_sym(s, field->ip, flags))
491 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
492 if (!trace_seq_printf(s, " <-"))
494 if (!seq_print_ip_sym(s,
499 if (!trace_seq_printf(s, "\n"))
502 return TRACE_TYPE_HANDLED;
505 return TRACE_TYPE_PARTIAL_LINE;
508 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
510 struct ftrace_entry *field;
512 trace_assign_type(field, iter->ent);
514 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
517 return TRACE_TYPE_PARTIAL_LINE;
519 return TRACE_TYPE_HANDLED;
522 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
524 struct ftrace_entry *field;
525 struct trace_seq *s = &iter->seq;
527 trace_assign_type(field, iter->ent);
529 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
530 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
532 return TRACE_TYPE_HANDLED;
535 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
537 struct ftrace_entry *field;
538 struct trace_seq *s = &iter->seq;
540 trace_assign_type(field, iter->ent);
542 SEQ_PUT_FIELD_RET(s, field->ip);
543 SEQ_PUT_FIELD_RET(s, field->parent_ip);
545 return TRACE_TYPE_HANDLED;
548 static struct trace_event trace_fn_event = {
550 .trace = trace_fn_trace,
553 .binary = trace_fn_bin,
556 /* TRACE_CTX an TRACE_WAKE */
557 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
560 struct ctx_switch_entry *field;
564 trace_assign_type(field, iter->ent);
566 T = task_state_char(field->next_state);
567 S = task_state_char(field->prev_state);
568 comm = trace_find_cmdline(field->next_pid);
569 if (!trace_seq_printf(&iter->seq,
570 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
578 return TRACE_TYPE_PARTIAL_LINE;
580 return TRACE_TYPE_HANDLED;
583 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
585 return trace_ctxwake_print(iter, "==>");
588 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
591 return trace_ctxwake_print(iter, " +");
594 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
596 struct ctx_switch_entry *field;
599 trace_assign_type(field, iter->ent);
602 task_state_char(field->prev_state);
603 T = task_state_char(field->next_state);
604 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
612 return TRACE_TYPE_PARTIAL_LINE;
614 return TRACE_TYPE_HANDLED;
617 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
619 return trace_ctxwake_raw(iter, 0);
622 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
624 return trace_ctxwake_raw(iter, '+');
628 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
630 struct ctx_switch_entry *field;
631 struct trace_seq *s = &iter->seq;
634 trace_assign_type(field, iter->ent);
637 task_state_char(field->prev_state);
638 T = task_state_char(field->next_state);
640 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
641 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
642 SEQ_PUT_HEX_FIELD_RET(s, S);
643 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
644 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
645 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
646 SEQ_PUT_HEX_FIELD_RET(s, T);
648 return TRACE_TYPE_HANDLED;
651 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
653 return trace_ctxwake_hex(iter, 0);
656 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
658 return trace_ctxwake_hex(iter, '+');
661 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
664 struct ctx_switch_entry *field;
665 struct trace_seq *s = &iter->seq;
667 trace_assign_type(field, iter->ent);
669 SEQ_PUT_FIELD_RET(s, field->prev_pid);
670 SEQ_PUT_FIELD_RET(s, field->prev_prio);
671 SEQ_PUT_FIELD_RET(s, field->prev_state);
672 SEQ_PUT_FIELD_RET(s, field->next_pid);
673 SEQ_PUT_FIELD_RET(s, field->next_prio);
674 SEQ_PUT_FIELD_RET(s, field->next_state);
676 return TRACE_TYPE_HANDLED;
679 static struct trace_event trace_ctx_event = {
681 .trace = trace_ctx_print,
682 .raw = trace_ctx_raw,
683 .hex = trace_ctx_hex,
684 .binary = trace_ctxwake_bin,
687 static struct trace_event trace_wake_event = {
689 .trace = trace_wake_print,
690 .raw = trace_wake_raw,
691 .hex = trace_wake_hex,
692 .binary = trace_ctxwake_bin,
696 static enum print_line_t trace_special_print(struct trace_iterator *iter,
699 struct special_entry *field;
701 trace_assign_type(field, iter->ent);
703 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
707 return TRACE_TYPE_PARTIAL_LINE;
709 return TRACE_TYPE_HANDLED;
712 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
715 struct special_entry *field;
716 struct trace_seq *s = &iter->seq;
718 trace_assign_type(field, iter->ent);
720 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
721 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
722 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
724 return TRACE_TYPE_HANDLED;
727 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
730 struct special_entry *field;
731 struct trace_seq *s = &iter->seq;
733 trace_assign_type(field, iter->ent);
735 SEQ_PUT_FIELD_RET(s, field->arg1);
736 SEQ_PUT_FIELD_RET(s, field->arg2);
737 SEQ_PUT_FIELD_RET(s, field->arg3);
739 return TRACE_TYPE_HANDLED;
742 static struct trace_event trace_special_event = {
743 .type = TRACE_SPECIAL,
744 .trace = trace_special_print,
745 .raw = trace_special_print,
746 .hex = trace_special_hex,
747 .binary = trace_special_bin,
752 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
755 struct stack_entry *field;
756 struct trace_seq *s = &iter->seq;
759 trace_assign_type(field, iter->ent);
761 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
763 if (!trace_seq_puts(s, " <= "))
766 if (!seq_print_ip_sym(s, field->caller[i], flags))
769 if (!trace_seq_puts(s, "\n"))
773 return TRACE_TYPE_HANDLED;
776 return TRACE_TYPE_PARTIAL_LINE;
779 static struct trace_event trace_stack_event = {
781 .trace = trace_stack_print,
782 .raw = trace_special_print,
783 .hex = trace_special_hex,
784 .binary = trace_special_bin,
787 /* TRACE_USER_STACK */
788 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
791 struct userstack_entry *field;
792 struct trace_seq *s = &iter->seq;
794 trace_assign_type(field, iter->ent);
796 if (!seq_print_userip_objs(field, s, flags))
799 if (!trace_seq_putc(s, '\n'))
802 return TRACE_TYPE_HANDLED;
805 return TRACE_TYPE_PARTIAL_LINE;
808 static struct trace_event trace_user_stack_event = {
809 .type = TRACE_USER_STACK,
810 .trace = trace_user_stack_print,
811 .raw = trace_special_print,
812 .hex = trace_special_hex,
813 .binary = trace_special_bin,
817 static enum print_line_t trace_print_print(struct trace_iterator *iter,
820 struct print_entry *field;
821 struct trace_seq *s = &iter->seq;
823 trace_assign_type(field, iter->ent);
825 if (!seq_print_ip_sym(s, field->ip, flags))
828 if (!trace_seq_printf(s, ": %s", field->buf))
831 return TRACE_TYPE_HANDLED;
834 return TRACE_TYPE_PARTIAL_LINE;
837 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
839 struct print_entry *field;
841 trace_assign_type(field, iter->ent);
843 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
846 return TRACE_TYPE_HANDLED;
849 return TRACE_TYPE_PARTIAL_LINE;
852 static struct trace_event trace_print_event = {
854 .trace = trace_print_print,
855 .raw = trace_print_raw,
858 static struct trace_event *events[] __initdata = {
862 &trace_special_event,
864 &trace_user_stack_event,
869 __init static int init_events(void)
871 struct trace_event *event;
874 for (i = 0; events[i]; i++) {
877 ret = register_ftrace_event(event);
879 printk(KERN_WARNING "event %d failed to register\n",
887 device_initcall(init_events);