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);
297 trace_seq_printf(s, "%8.8s-%-5d ", comm, entry->pid);
298 trace_seq_printf(s, "%3d", cpu);
299 trace_seq_printf(s, "%c%c",
300 (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
301 (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ? 'X' : '.',
302 ((entry->flags & TRACE_FLAG_NEED_RESCHED) ? 'N' : '.'));
304 hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
305 softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
306 if (hardirq && softirq) {
307 trace_seq_putc(s, 'H');
310 trace_seq_putc(s, 'h');
313 trace_seq_putc(s, 's');
315 trace_seq_putc(s, '.');
319 if (entry->preempt_count)
320 trace_seq_printf(s, "%x", entry->preempt_count);
322 trace_seq_puts(s, ".");
325 static unsigned long preempt_mark_thresh = 100;
328 lat_print_timestamp(struct trace_seq *s, u64 abs_usecs,
329 unsigned long rel_usecs)
331 trace_seq_printf(s, " %4lldus", abs_usecs);
332 if (rel_usecs > preempt_mark_thresh)
333 trace_seq_puts(s, "!: ");
334 else if (rel_usecs > 1)
335 trace_seq_puts(s, "+: ");
337 trace_seq_puts(s, " : ");
340 int trace_print_context(struct trace_iterator *iter)
342 struct trace_seq *s = &iter->seq;
343 struct trace_entry *entry = iter->ent;
344 char *comm = trace_find_cmdline(entry->pid);
345 unsigned long long t = ns2usecs(iter->ts);
346 unsigned long usec_rem = do_div(t, USEC_PER_SEC);
347 unsigned long secs = (unsigned long)t;
349 if (!trace_seq_printf(s, "%16s-%-5d ", comm, entry->pid))
351 if (!trace_seq_printf(s, "[%03d] ", entry->cpu))
353 if (!trace_seq_printf(s, "%5lu.%06lu: ", secs, usec_rem))
359 return TRACE_TYPE_PARTIAL_LINE;
362 int trace_print_lat_context(struct trace_iterator *iter)
365 struct trace_seq *s = &iter->seq;
366 struct trace_entry *entry = iter->ent,
367 *next_entry = trace_find_next_entry(iter, NULL,
369 unsigned long verbose = (trace_flags & TRACE_ITER_VERBOSE);
370 unsigned long abs_usecs = ns2usecs(iter->ts - iter->tr->time_start);
371 unsigned long rel_usecs;
375 rel_usecs = ns2usecs(next_ts - iter->ts);
378 char *comm = trace_find_cmdline(entry->pid);
379 trace_seq_printf(s, "%16s %5d %3d %d %08x %08lx [%08lx]"
380 " %ld.%03ldms (+%ld.%03ldms): ",
382 entry->pid, entry->cpu, entry->flags,
383 entry->preempt_count, iter->idx,
386 abs_usecs % 1000, rel_usecs/1000,
389 lat_print_generic(s, entry, entry->cpu);
390 lat_print_timestamp(s, abs_usecs, rel_usecs);
396 static const char state_to_char[] = TASK_STATE_TO_CHAR_STR;
398 static int task_state_char(unsigned long state)
400 int bit = state ? __ffs(state) + 1 : 0;
402 return bit < sizeof(state_to_char) - 1 ? state_to_char[bit] : '?';
406 * ftrace_find_event - find a registered event
407 * @type: the type of event to look for
409 * Returns an event of type @type otherwise NULL
411 struct trace_event *ftrace_find_event(int type)
413 struct trace_event *event;
414 struct hlist_node *n;
417 key = type & (EVENT_HASHSIZE - 1);
419 hlist_for_each_entry_rcu(event, n, &event_hash[key], node) {
420 if (event->type == type)
428 * register_ftrace_event - register output for an event type
429 * @event: the event type to register
431 * Event types are stored in a hash and this hash is used to
432 * find a way to print an event. If the @event->type is set
433 * then it will use that type, otherwise it will assign a
436 * If you assign your own type, please make sure it is added
437 * to the trace_type enum in trace.h, to avoid collisions
438 * with the dynamic types.
440 * Returns the event type number or zero on error.
442 int register_ftrace_event(struct trace_event *event)
447 mutex_lock(&trace_event_mutex);
450 event->type = next_event_type++;
451 else if (event->type > __TRACE_LAST_TYPE) {
452 printk(KERN_WARNING "Need to add type to trace.h\n");
456 if (ftrace_find_event(event->type))
459 key = event->type & (EVENT_HASHSIZE - 1);
461 hlist_add_head_rcu(&event->node, &event_hash[key]);
465 mutex_unlock(&trace_event_mutex);
471 * unregister_ftrace_event - remove a no longer used event
472 * @event: the event to remove
474 int unregister_ftrace_event(struct trace_event *event)
476 mutex_lock(&trace_event_mutex);
477 hlist_del(&event->node);
478 mutex_unlock(&trace_event_mutex);
488 trace_nop_print(struct trace_seq *s, struct trace_entry *entry, int flags)
495 trace_fn_latency(struct trace_seq *s, struct trace_entry *entry, int flags)
497 struct ftrace_entry *field;
499 trace_assign_type(field, entry);
501 if (!seq_print_ip_sym(s, field->ip, flags))
503 if (!trace_seq_puts(s, " ("))
505 if (!seq_print_ip_sym(s, field->parent_ip, flags))
507 if (!trace_seq_puts(s, ")\n"))
513 return TRACE_TYPE_PARTIAL_LINE;
517 trace_fn_trace(struct trace_seq *s, struct trace_entry *entry, int flags)
519 struct ftrace_entry *field;
521 trace_assign_type(field, entry);
523 if (!seq_print_ip_sym(s, field->ip, flags))
526 if ((flags & TRACE_ITER_PRINT_PARENT) && field->parent_ip) {
527 if (!trace_seq_printf(s, " <-"))
529 if (!seq_print_ip_sym(s,
534 if (!trace_seq_printf(s, "\n"))
540 return TRACE_TYPE_PARTIAL_LINE;
544 trace_fn_raw(struct trace_seq *s, struct trace_entry *entry, int flags)
546 struct ftrace_entry *field;
548 trace_assign_type(field, entry);
550 if (!trace_seq_printf(s, "%lx %lx\n",
553 return TRACE_TYPE_PARTIAL_LINE;
559 trace_fn_hex(struct trace_seq *s, struct trace_entry *entry, int flags)
561 struct ftrace_entry *field;
563 trace_assign_type(field, entry);
565 SEQ_PUT_HEX_FIELD_RET(s, field->ip);
566 SEQ_PUT_HEX_FIELD_RET(s, field->parent_ip);
572 trace_fn_bin(struct trace_seq *s, struct trace_entry *entry, int flags)
574 struct ftrace_entry *field;
576 trace_assign_type(field, entry);
578 SEQ_PUT_FIELD_RET(s, field->ip);
579 SEQ_PUT_FIELD_RET(s, field->parent_ip);
584 static struct trace_event trace_fn_event = {
586 .trace = trace_fn_trace,
587 .latency_trace = trace_fn_latency,
590 .binary = trace_fn_bin,
593 /* TRACE_CTX an TRACE_WAKE */
595 trace_ctxwake_print(struct trace_seq *s, struct trace_entry *entry, int flags,
598 struct ctx_switch_entry *field;
602 trace_assign_type(field, entry);
604 T = task_state_char(field->next_state);
605 S = task_state_char(field->prev_state);
606 comm = trace_find_cmdline(field->next_pid);
607 if (!trace_seq_printf(s, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
615 return TRACE_TYPE_PARTIAL_LINE;
621 trace_ctx_print(struct trace_seq *s, struct trace_entry *entry, int flags)
623 return trace_ctxwake_print(s, entry, flags, "==>");
627 trace_wake_print(struct trace_seq *s, struct trace_entry *entry, int flags)
629 return trace_ctxwake_print(s, entry, flags, " +");
633 trace_ctxwake_raw(struct trace_seq *s, struct trace_entry *entry, int flags,
636 struct ctx_switch_entry *field;
639 trace_assign_type(field, entry);
642 task_state_char(field->prev_state);
643 T = task_state_char(field->next_state);
644 if (!trace_seq_printf(s, "%d %d %c %d %d %d %c\n",
652 return TRACE_TYPE_PARTIAL_LINE;
658 trace_ctx_raw(struct trace_seq *s, struct trace_entry *entry, int flags)
660 return trace_ctxwake_raw(s, entry, flags, 0);
664 trace_wake_raw(struct trace_seq *s, struct trace_entry *entry, int flags)
666 return trace_ctxwake_raw(s, entry, flags, '+');
671 trace_ctxwake_hex(struct trace_seq *s, struct trace_entry *entry, int flags,
674 struct ctx_switch_entry *field;
677 trace_assign_type(field, entry);
680 task_state_char(field->prev_state);
681 T = task_state_char(field->next_state);
683 SEQ_PUT_HEX_FIELD_RET(s, field->prev_pid);
684 SEQ_PUT_HEX_FIELD_RET(s, field->prev_prio);
685 SEQ_PUT_HEX_FIELD_RET(s, S);
686 SEQ_PUT_HEX_FIELD_RET(s, field->next_cpu);
687 SEQ_PUT_HEX_FIELD_RET(s, field->next_pid);
688 SEQ_PUT_HEX_FIELD_RET(s, field->next_prio);
689 SEQ_PUT_HEX_FIELD_RET(s, T);
695 trace_ctx_hex(struct trace_seq *s, struct trace_entry *entry, int flags)
697 return trace_ctxwake_hex(s, entry, flags, 0);
701 trace_wake_hex(struct trace_seq *s, struct trace_entry *entry, int flags)
703 return trace_ctxwake_hex(s, entry, flags, '+');
707 trace_ctxwake_bin(struct trace_seq *s, struct trace_entry *entry, int flags)
709 struct ctx_switch_entry *field;
711 trace_assign_type(field, entry);
713 SEQ_PUT_FIELD_RET(s, field->prev_pid);
714 SEQ_PUT_FIELD_RET(s, field->prev_prio);
715 SEQ_PUT_FIELD_RET(s, field->prev_state);
716 SEQ_PUT_FIELD_RET(s, field->next_pid);
717 SEQ_PUT_FIELD_RET(s, field->next_prio);
718 SEQ_PUT_FIELD_RET(s, field->next_state);
723 static struct trace_event trace_ctx_event = {
725 .trace = trace_ctx_print,
726 .latency_trace = trace_ctx_print,
727 .raw = trace_ctx_raw,
728 .hex = trace_ctx_hex,
729 .binary = trace_ctxwake_bin,
732 static struct trace_event trace_wake_event = {
734 .trace = trace_wake_print,
735 .latency_trace = trace_wake_print,
736 .raw = trace_wake_raw,
737 .hex = trace_wake_hex,
738 .binary = trace_ctxwake_bin,
743 trace_special_print(struct trace_seq *s, struct trace_entry *entry, int flags)
745 struct special_entry *field;
747 trace_assign_type(field, entry);
749 if (!trace_seq_printf(s, "# %ld %ld %ld\n",
753 return TRACE_TYPE_PARTIAL_LINE;
759 trace_special_hex(struct trace_seq *s, struct trace_entry *entry, int flags)
761 struct special_entry *field;
763 trace_assign_type(field, entry);
765 SEQ_PUT_HEX_FIELD_RET(s, field->arg1);
766 SEQ_PUT_HEX_FIELD_RET(s, field->arg2);
767 SEQ_PUT_HEX_FIELD_RET(s, field->arg3);
773 trace_special_bin(struct trace_seq *s, struct trace_entry *entry, int flags)
775 struct special_entry *field;
777 trace_assign_type(field, entry);
779 SEQ_PUT_FIELD_RET(s, field->arg1);
780 SEQ_PUT_FIELD_RET(s, field->arg2);
781 SEQ_PUT_FIELD_RET(s, field->arg3);
786 static struct trace_event trace_special_event = {
787 .type = TRACE_SPECIAL,
788 .trace = trace_special_print,
789 .latency_trace = trace_special_print,
790 .raw = trace_special_print,
791 .hex = trace_special_hex,
792 .binary = trace_special_bin,
798 trace_stack_print(struct trace_seq *s, struct trace_entry *entry, int flags)
800 struct stack_entry *field;
803 trace_assign_type(field, entry);
805 for (i = 0; i < FTRACE_STACK_ENTRIES; i++) {
807 if (!trace_seq_puts(s, " <= "))
810 if (!seq_print_ip_sym(s, field->caller[i], flags))
813 if (!trace_seq_puts(s, "\n"))
820 return TRACE_TYPE_PARTIAL_LINE;
823 static struct trace_event trace_stack_event = {
825 .trace = trace_stack_print,
826 .latency_trace = trace_stack_print,
827 .raw = trace_special_print,
828 .hex = trace_special_hex,
829 .binary = trace_special_bin,
832 /* TRACE_USER_STACK */
834 trace_user_stack_print(struct trace_seq *s, struct trace_entry *entry,
837 struct userstack_entry *field;
839 trace_assign_type(field, entry);
841 if (!seq_print_userip_objs(field, s, flags))
844 if (!trace_seq_putc(s, '\n'))
850 return TRACE_TYPE_PARTIAL_LINE;
853 static struct trace_event trace_user_stack_event = {
854 .type = TRACE_USER_STACK,
855 .trace = trace_user_stack_print,
856 .latency_trace = trace_user_stack_print,
857 .raw = trace_special_print,
858 .hex = trace_special_hex,
859 .binary = trace_special_bin,
864 trace_print_print(struct trace_seq *s, struct trace_entry *entry, int flags)
866 struct print_entry *field;
868 trace_assign_type(field, entry);
870 if (!seq_print_ip_sym(s, field->ip, flags))
873 if (!trace_seq_printf(s, ": %s", field->buf))
879 return TRACE_TYPE_PARTIAL_LINE;
883 trace_print_raw(struct trace_seq *s, struct trace_entry *entry, int flags)
885 struct print_entry *field;
887 trace_assign_type(field, entry);
889 if (!trace_seq_printf(s, "# %lx %s", field->ip, field->buf))
895 return TRACE_TYPE_PARTIAL_LINE;
898 static struct trace_event trace_print_event = {
900 .trace = trace_print_print,
901 .latency_trace = trace_print_print,
902 .raw = trace_print_raw,
903 .hex = trace_nop_print,
904 .binary = trace_nop_print,
907 static struct trace_event *events[] __initdata = {
911 &trace_special_event,
913 &trace_user_stack_event,
918 __init static int init_events(void)
920 struct trace_event *event;
923 for (i = 0; events[i]; i++) {
926 ret = register_ftrace_event(event);
928 printk(KERN_WARNING "event %d failed to register\n",
936 device_initcall(init_events);