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, entry->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, entry->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, entry->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 key = event->type & (EVENT_HASHSIZE - 1);
440 hlist_add_head_rcu(&event->node, &event_hash[key]);
444 mutex_unlock(&trace_event_mutex);
450 * unregister_ftrace_event - remove a no longer used event
451 * @event: the event to remove
453 int unregister_ftrace_event(struct trace_event *event)
455 mutex_lock(&trace_event_mutex);
456 hlist_del(&event->node);
457 mutex_unlock(&trace_event_mutex);
466 enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags)
468 return TRACE_TYPE_HANDLED;
472 static enum print_line_t trace_fn_latency(struct trace_iterator *iter,
475 struct ftrace_entry *field;
476 struct trace_seq *s = &iter->seq;
478 trace_assign_type(field, iter->ent);
480 if (!seq_print_ip_sym(s, field->ip, flags))
482 if (!trace_seq_puts(s, " ("))
484 if (!seq_print_ip_sym(s, field->parent_ip, flags))
486 if (!trace_seq_puts(s, ")\n"))
489 return TRACE_TYPE_HANDLED;
492 return TRACE_TYPE_PARTIAL_LINE;
495 static enum print_line_t trace_fn_trace(struct trace_iterator *iter, int flags)
497 struct ftrace_entry *field;
498 struct trace_seq *s = &iter->seq;
500 trace_assign_type(field, iter->ent);
502 if (!seq_print_ip_sym(s, field->ip, flags))
505 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
506 if (!trace_seq_printf(s, " <-"))
508 if (!seq_print_ip_sym(s,
513 if (!trace_seq_printf(s, "\n"))
516 return TRACE_TYPE_HANDLED;
519 return TRACE_TYPE_PARTIAL_LINE;
522 static enum print_line_t trace_fn_raw(struct trace_iterator *iter, int flags)
524 struct ftrace_entry *field;
526 trace_assign_type(field, iter->ent);
528 if (!trace_seq_printf(&iter->seq, "%lx %lx\n",
531 return TRACE_TYPE_PARTIAL_LINE;
533 return TRACE_TYPE_HANDLED;
536 static enum print_line_t trace_fn_hex(struct trace_iterator *iter, int flags)
538 struct ftrace_entry *field;
539 struct trace_seq *s = &iter->seq;
541 trace_assign_type(field, iter->ent);
543 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
544 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
546 return TRACE_TYPE_HANDLED;
549 static enum print_line_t trace_fn_bin(struct trace_iterator *iter, int flags)
551 struct ftrace_entry *field;
552 struct trace_seq *s = &iter->seq;
554 trace_assign_type(field, iter->ent);
556 SEQ_PUT_FIELD_RET(s, field->ip);
557 SEQ_PUT_FIELD_RET(s, field->parent_ip);
559 return TRACE_TYPE_HANDLED;
562 static struct trace_event trace_fn_event = {
564 .trace = trace_fn_trace,
565 .latency_trace = trace_fn_latency,
568 .binary = trace_fn_bin,
571 /* TRACE_CTX an TRACE_WAKE */
572 static enum print_line_t trace_ctxwake_print(struct trace_iterator *iter,
575 struct ctx_switch_entry *field;
579 trace_assign_type(field, iter->ent);
581 T = task_state_char(field->next_state);
582 S = task_state_char(field->prev_state);
583 comm = trace_find_cmdline(field->next_pid);
584 if (!trace_seq_printf(&iter->seq,
585 " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
593 return TRACE_TYPE_PARTIAL_LINE;
595 return TRACE_TYPE_HANDLED;
598 static enum print_line_t trace_ctx_print(struct trace_iterator *iter, int flags)
600 return trace_ctxwake_print(iter, "==>");
603 static enum print_line_t trace_wake_print(struct trace_iterator *iter,
606 return trace_ctxwake_print(iter, " +");
609 static int trace_ctxwake_raw(struct trace_iterator *iter, char S)
611 struct ctx_switch_entry *field;
614 trace_assign_type(field, iter->ent);
617 task_state_char(field->prev_state);
618 T = task_state_char(field->next_state);
619 if (!trace_seq_printf(&iter->seq, "%d %d %c %d %d %d %c\n",
627 return TRACE_TYPE_PARTIAL_LINE;
629 return TRACE_TYPE_HANDLED;
632 static enum print_line_t trace_ctx_raw(struct trace_iterator *iter, int flags)
634 return trace_ctxwake_raw(iter, 0);
637 static enum print_line_t trace_wake_raw(struct trace_iterator *iter, int flags)
639 return trace_ctxwake_raw(iter, '+');
643 static int trace_ctxwake_hex(struct trace_iterator *iter, char S)
645 struct ctx_switch_entry *field;
646 struct trace_seq *s = &iter->seq;
649 trace_assign_type(field, iter->ent);
652 task_state_char(field->prev_state);
653 T = task_state_char(field->next_state);
655 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
656 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
657 SEQ_PUT_HEX_FIELD_RET(s, S);
658 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
659 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
660 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
661 SEQ_PUT_HEX_FIELD_RET(s, T);
663 return TRACE_TYPE_HANDLED;
666 static enum print_line_t trace_ctx_hex(struct trace_iterator *iter, int flags)
668 return trace_ctxwake_hex(iter, 0);
671 static enum print_line_t trace_wake_hex(struct trace_iterator *iter, int flags)
673 return trace_ctxwake_hex(iter, '+');
676 static enum print_line_t trace_ctxwake_bin(struct trace_iterator *iter,
679 struct ctx_switch_entry *field;
680 struct trace_seq *s = &iter->seq;
682 trace_assign_type(field, iter->ent);
684 SEQ_PUT_FIELD_RET(s, field->prev_pid);
685 SEQ_PUT_FIELD_RET(s, field->prev_prio);
686 SEQ_PUT_FIELD_RET(s, field->prev_state);
687 SEQ_PUT_FIELD_RET(s, field->next_pid);
688 SEQ_PUT_FIELD_RET(s, field->next_prio);
689 SEQ_PUT_FIELD_RET(s, field->next_state);
691 return TRACE_TYPE_HANDLED;
694 static struct trace_event trace_ctx_event = {
696 .trace = trace_ctx_print,
697 .latency_trace = trace_ctx_print,
698 .raw = trace_ctx_raw,
699 .hex = trace_ctx_hex,
700 .binary = trace_ctxwake_bin,
703 static struct trace_event trace_wake_event = {
705 .trace = trace_wake_print,
706 .latency_trace = trace_wake_print,
707 .raw = trace_wake_raw,
708 .hex = trace_wake_hex,
709 .binary = trace_ctxwake_bin,
713 static enum print_line_t trace_special_print(struct trace_iterator *iter,
716 struct special_entry *field;
718 trace_assign_type(field, iter->ent);
720 if (!trace_seq_printf(&iter->seq, "# %ld %ld %ld\n",
724 return TRACE_TYPE_PARTIAL_LINE;
726 return TRACE_TYPE_HANDLED;
729 static enum print_line_t trace_special_hex(struct trace_iterator *iter,
732 struct special_entry *field;
733 struct trace_seq *s = &iter->seq;
735 trace_assign_type(field, iter->ent);
737 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
738 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
739 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
741 return TRACE_TYPE_HANDLED;
744 static enum print_line_t trace_special_bin(struct trace_iterator *iter,
747 struct special_entry *field;
748 struct trace_seq *s = &iter->seq;
750 trace_assign_type(field, iter->ent);
752 SEQ_PUT_FIELD_RET(s, field->arg1);
753 SEQ_PUT_FIELD_RET(s, field->arg2);
754 SEQ_PUT_FIELD_RET(s, field->arg3);
756 return TRACE_TYPE_HANDLED;
759 static struct trace_event trace_special_event = {
760 .type = TRACE_SPECIAL,
761 .trace = trace_special_print,
762 .latency_trace = trace_special_print,
763 .raw = trace_special_print,
764 .hex = trace_special_hex,
765 .binary = trace_special_bin,
770 static enum print_line_t trace_stack_print(struct trace_iterator *iter,
773 struct stack_entry *field;
774 struct trace_seq *s = &iter->seq;
777 trace_assign_type(field, iter->ent);
779 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
781 if (!trace_seq_puts(s, " <= "))
784 if (!seq_print_ip_sym(s, field->caller[i], flags))
787 if (!trace_seq_puts(s, "\n"))
791 return TRACE_TYPE_HANDLED;
794 return TRACE_TYPE_PARTIAL_LINE;
797 static struct trace_event trace_stack_event = {
799 .trace = trace_stack_print,
800 .latency_trace = trace_stack_print,
801 .raw = trace_special_print,
802 .hex = trace_special_hex,
803 .binary = trace_special_bin,
806 /* TRACE_USER_STACK */
807 static enum print_line_t trace_user_stack_print(struct trace_iterator *iter,
810 struct userstack_entry *field;
811 struct trace_seq *s = &iter->seq;
813 trace_assign_type(field, iter->ent);
815 if (!seq_print_userip_objs(field, s, flags))
818 if (!trace_seq_putc(s, '\n'))
821 return TRACE_TYPE_HANDLED;
824 return TRACE_TYPE_PARTIAL_LINE;
827 static struct trace_event trace_user_stack_event = {
828 .type = TRACE_USER_STACK,
829 .trace = trace_user_stack_print,
830 .latency_trace = trace_user_stack_print,
831 .raw = trace_special_print,
832 .hex = trace_special_hex,
833 .binary = trace_special_bin,
837 static enum print_line_t trace_print_print(struct trace_iterator *iter,
840 struct print_entry *field;
841 struct trace_seq *s = &iter->seq;
843 trace_assign_type(field, iter->ent);
845 if (!seq_print_ip_sym(s, field->ip, flags))
848 if (!trace_seq_printf(s, ": %s", field->buf))
851 return TRACE_TYPE_HANDLED;
854 return TRACE_TYPE_PARTIAL_LINE;
857 static enum print_line_t trace_print_raw(struct trace_iterator *iter, int flags)
859 struct print_entry *field;
861 trace_assign_type(field, iter->ent);
863 if (!trace_seq_printf(&iter->seq, "# %lx %s", field->ip, field->buf))
866 return TRACE_TYPE_HANDLED;
869 return TRACE_TYPE_PARTIAL_LINE;
872 static struct trace_event trace_print_event = {
874 .trace = trace_print_print,
875 .latency_trace = trace_print_print,
876 .raw = trace_print_raw,
877 .hex = trace_nop_print,
878 .binary = trace_nop_print,
881 static struct trace_event *events[] __initdata = {
885 &trace_special_event,
887 &trace_user_stack_event,
892 __init static int init_events(void)
894 struct trace_event *event;
897 for (i = 0; events[i]; i++) {
900 ret = register_ftrace_event(event);
902 printk(KERN_WARNING "event %d failed to register\n",
910 device_initcall(init_events);