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
);
289 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
291 static int task_state_char(unsigned long state
)
293 int bit
= state
? __ffs(state
) + 1 : 0;
295 return bit
< sizeof(state_to_char
) - 1 ? state_to_char
[bit
] : '?';
299 * ftrace_find_event - find a registered event
300 * @type: the type of event to look for
302 * Returns an event of type @type otherwise NULL
304 struct trace_event
*ftrace_find_event(int type
)
306 struct trace_event
*event
;
307 struct hlist_node
*n
;
310 key
= type
& (EVENT_HASHSIZE
- 1);
312 hlist_for_each_entry_rcu(event
, n
, &event_hash
[key
], node
) {
313 if (event
->type
== type
)
321 * register_ftrace_event - register output for an event type
322 * @event: the event type to register
324 * Event types are stored in a hash and this hash is used to
325 * find a way to print an event. If the @event->type is set
326 * then it will use that type, otherwise it will assign a
329 * If you assign your own type, please make sure it is added
330 * to the trace_type enum in trace.h, to avoid collisions
331 * with the dynamic types.
333 * Returns the event type number or zero on error.
335 int register_ftrace_event(struct trace_event
*event
)
340 mutex_lock(&trace_event_mutex
);
343 event
->type
= next_event_type
++;
344 else if (event
->type
> __TRACE_LAST_TYPE
) {
345 printk(KERN_WARNING
"Need to add type to trace.h\n");
349 if (ftrace_find_event(event
->type
))
352 key
= event
->type
& (EVENT_HASHSIZE
- 1);
354 hlist_add_head_rcu(&event
->node
, &event_hash
[key
]);
358 mutex_unlock(&trace_event_mutex
);
364 * unregister_ftrace_event - remove a no longer used event
365 * @event: the event to remove
367 int unregister_ftrace_event(struct trace_event
*event
)
369 mutex_lock(&trace_event_mutex
);
370 hlist_del(&event
->node
);
371 mutex_unlock(&trace_event_mutex
);
381 trace_nop_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
388 trace_fn_latency(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
390 struct ftrace_entry
*field
;
392 trace_assign_type(field
, entry
);
394 if (!seq_print_ip_sym(s
, field
->ip
, flags
))
396 if (!trace_seq_puts(s
, " ("))
398 if (!seq_print_ip_sym(s
, field
->parent_ip
, flags
))
400 if (!trace_seq_puts(s
, ")\n"))
406 return TRACE_TYPE_PARTIAL_LINE
;
410 trace_fn_trace(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
412 struct ftrace_entry
*field
;
414 trace_assign_type(field
, entry
);
416 if (!seq_print_ip_sym(s
, field
->ip
, flags
))
419 if ((flags
& TRACE_ITER_PRINT_PARENT
) && field
->parent_ip
) {
420 if (!trace_seq_printf(s
, " <-"))
422 if (!seq_print_ip_sym(s
,
427 if (!trace_seq_printf(s
, "\n"))
433 return TRACE_TYPE_PARTIAL_LINE
;
437 trace_fn_raw(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
439 struct ftrace_entry
*field
;
441 trace_assign_type(field
, entry
);
443 if (trace_seq_printf(s
, "%x %x\n",
446 return TRACE_TYPE_PARTIAL_LINE
;
452 trace_fn_hex(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
454 struct ftrace_entry
*field
;
456 trace_assign_type(field
, entry
);
458 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
459 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
465 trace_fn_bin(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
467 struct ftrace_entry
*field
;
469 trace_assign_type(field
, entry
);
471 SEQ_PUT_FIELD_RET(s
, field
->ip
);
472 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
477 static struct trace_event trace_fn_event
= {
479 .trace
= trace_fn_trace
,
480 .latency_trace
= trace_fn_latency
,
483 .binary
= trace_fn_bin
,
486 /* TRACE_CTX an TRACE_WAKE */
488 trace_ctxwake_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
,
491 struct ctx_switch_entry
*field
;
495 trace_assign_type(field
, entry
);
497 T
= task_state_char(field
->next_state
);
498 S
= task_state_char(field
->prev_state
);
499 comm
= trace_find_cmdline(field
->next_pid
);
500 if (trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
508 return TRACE_TYPE_PARTIAL_LINE
;
514 trace_ctx_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
516 return trace_ctxwake_print(s
, entry
, flags
, "==>");
520 trace_wake_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
522 return trace_ctxwake_print(s
, entry
, flags
, " +");
526 trace_ctxwake_raw(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
,
529 struct ctx_switch_entry
*field
;
532 trace_assign_type(field
, entry
);
535 task_state_char(field
->prev_state
);
536 T
= task_state_char(field
->next_state
);
537 if (trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
545 return TRACE_TYPE_PARTIAL_LINE
;
551 trace_ctx_raw(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
553 return trace_ctxwake_raw(s
, entry
, flags
, 0);
557 trace_wake_raw(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
559 return trace_ctxwake_raw(s
, entry
, flags
, '+');
564 trace_ctxwake_hex(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
,
567 struct ctx_switch_entry
*field
;
570 trace_assign_type(field
, entry
);
573 task_state_char(field
->prev_state
);
574 T
= task_state_char(field
->next_state
);
576 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
577 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
578 SEQ_PUT_HEX_FIELD_RET(s
, S
);
579 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
580 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
581 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
582 SEQ_PUT_HEX_FIELD_RET(s
, T
);
588 trace_ctx_hex(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
590 return trace_ctxwake_hex(s
, entry
, flags
, 0);
594 trace_wake_hex(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
596 return trace_ctxwake_hex(s
, entry
, flags
, '+');
600 trace_ctxwake_bin(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
602 struct ctx_switch_entry
*field
;
604 trace_assign_type(field
, entry
);
606 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
607 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
608 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
609 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
610 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
611 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
616 static struct trace_event trace_ctx_event
= {
618 .trace
= trace_ctx_print
,
619 .latency_trace
= trace_ctx_print
,
620 .raw
= trace_ctx_raw
,
621 .hex
= trace_ctx_hex
,
622 .binary
= trace_ctxwake_bin
,
625 static struct trace_event trace_wake_event
= {
627 .trace
= trace_wake_print
,
628 .latency_trace
= trace_wake_print
,
629 .raw
= trace_wake_raw
,
630 .hex
= trace_wake_hex
,
631 .binary
= trace_ctxwake_bin
,
636 trace_special_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
638 struct special_entry
*field
;
640 trace_assign_type(field
, entry
);
642 if (trace_seq_printf(s
, "# %ld %ld %ld\n",
646 return TRACE_TYPE_PARTIAL_LINE
;
652 trace_special_hex(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
654 struct special_entry
*field
;
656 trace_assign_type(field
, entry
);
658 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
659 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
660 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
666 trace_special_bin(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
668 struct special_entry
*field
;
670 trace_assign_type(field
, entry
);
672 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
673 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
674 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
679 static struct trace_event trace_special_event
= {
680 .type
= TRACE_SPECIAL
,
681 .trace
= trace_special_print
,
682 .latency_trace
= trace_special_print
,
683 .raw
= trace_special_print
,
684 .hex
= trace_special_hex
,
685 .binary
= trace_special_bin
,
691 trace_stack_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
693 struct stack_entry
*field
;
696 trace_assign_type(field
, entry
);
698 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
700 if (trace_seq_puts(s
, " <= "))
703 if (seq_print_ip_sym(s
, field
->caller
[i
], flags
))
706 if (trace_seq_puts(s
, "\n"))
713 return TRACE_TYPE_PARTIAL_LINE
;
716 static struct trace_event trace_stack_event
= {
718 .trace
= trace_stack_print
,
719 .latency_trace
= trace_stack_print
,
720 .raw
= trace_special_print
,
721 .hex
= trace_special_hex
,
722 .binary
= trace_special_bin
,
725 /* TRACE_USER_STACK */
727 trace_user_stack_print(struct trace_seq
*s
, struct trace_entry
*entry
,
730 struct userstack_entry
*field
;
732 trace_assign_type(field
, entry
);
734 if (seq_print_userip_objs(field
, s
, flags
))
737 if (trace_seq_putc(s
, '\n'))
743 return TRACE_TYPE_PARTIAL_LINE
;
746 static struct trace_event trace_user_stack_event
= {
747 .type
= TRACE_USER_STACK
,
748 .trace
= trace_user_stack_print
,
749 .latency_trace
= trace_user_stack_print
,
750 .raw
= trace_special_print
,
751 .hex
= trace_special_hex
,
752 .binary
= trace_special_bin
,
757 trace_print_print(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
759 struct print_entry
*field
;
761 trace_assign_type(field
, entry
);
763 if (seq_print_ip_sym(s
, field
->ip
, flags
))
766 if (trace_seq_printf(s
, ": %s", field
->buf
))
772 return TRACE_TYPE_PARTIAL_LINE
;
776 trace_print_raw(struct trace_seq
*s
, struct trace_entry
*entry
, int flags
)
778 struct print_entry
*field
;
780 trace_assign_type(field
, entry
);
782 if (seq_print_ip_sym(s
, field
->ip
, flags
))
785 if (trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
))
791 return TRACE_TYPE_PARTIAL_LINE
;
794 static struct trace_event trace_print_event
= {
796 .trace
= trace_print_print
,
797 .latency_trace
= trace_print_print
,
798 .raw
= trace_print_raw
,
799 .hex
= trace_nop_print
,
800 .binary
= trace_nop_print
,
803 static struct trace_event
*events
[] __initdata
= {
807 &trace_special_event
,
809 &trace_user_stack_event
,
814 __init
static int init_events(void)
816 struct trace_event
*event
;
819 for (i
= 0; events
[i
]; i
++) {
822 ret
= register_ftrace_event(event
);
824 printk(KERN_WARNING
"event %d failed to register\n",
832 device_initcall(init_events
);