2 * ring buffer based function tracer
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
7 * Originally taken from the RT patch by:
8 * Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code from the latency_tracer, that is:
11 * Copyright (C) 2004-2006 Ingo Molnar
12 * Copyright (C) 2004 William Lee Irwin III
14 #include <linux/utsrelease.h>
15 #include <linux/kallsyms.h>
16 #include <linux/seq_file.h>
17 #include <linux/notifier.h>
18 #include <linux/debugfs.h>
19 #include <linux/pagemap.h>
20 #include <linux/hardirq.h>
21 #include <linux/linkage.h>
22 #include <linux/uaccess.h>
23 #include <linux/ftrace.h>
24 #include <linux/module.h>
25 #include <linux/percpu.h>
26 #include <linux/kdebug.h>
27 #include <linux/ctype.h>
28 #include <linux/init.h>
29 #include <linux/poll.h>
30 #include <linux/gfp.h>
32 #include <linux/kprobes.h>
33 #include <linux/writeback.h>
35 #include <linux/stacktrace.h>
36 #include <linux/ring_buffer.h>
40 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
42 unsigned long __read_mostly tracing_max_latency
= (cycle_t
)ULONG_MAX
;
43 unsigned long __read_mostly tracing_thresh
;
45 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
47 static inline void ftrace_disable_cpu(void)
50 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
53 static inline void ftrace_enable_cpu(void)
55 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
59 static cpumask_t __read_mostly tracing_buffer_mask
;
61 #define for_each_tracing_cpu(cpu) \
62 for_each_cpu_mask(cpu, tracing_buffer_mask)
64 static int tracing_disabled
= 1;
67 ns2usecs(cycle_t nsec
)
74 cycle_t
ftrace_now(int cpu
)
76 u64 ts
= ring_buffer_time_stamp(cpu
);
77 ring_buffer_normalize_time_stamp(cpu
, &ts
);
82 * The global_trace is the descriptor that holds the tracing
83 * buffers for the live tracing. For each CPU, it contains
84 * a link list of pages that will store trace entries. The
85 * page descriptor of the pages in the memory is used to hold
86 * the link list by linking the lru item in the page descriptor
87 * to each of the pages in the buffer per CPU.
89 * For each active CPU there is a data field that holds the
90 * pages for the buffer for that CPU. Each CPU has the same number
91 * of pages allocated for its buffer.
93 static struct trace_array global_trace
;
95 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
98 * The max_tr is used to snapshot the global_trace when a maximum
99 * latency is reached. Some tracers will use this to store a maximum
100 * trace while it continues examining live traces.
102 * The buffers for the max_tr are set up the same as the global_trace.
103 * When a snapshot is taken, the link list of the max_tr is swapped
104 * with the link list of the global_trace and the buffers are reset for
105 * the global_trace so the tracing can continue.
107 static struct trace_array max_tr
;
109 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
111 /* tracer_enabled is used to toggle activation of a tracer */
112 static int tracer_enabled
= 1;
114 /* function tracing enabled */
115 int ftrace_function_enabled
;
118 * trace_buf_size is the size in bytes that is allocated
119 * for a buffer. Note, the number of bytes is always rounded
122 * This number is purposely set to a low number of 16384.
123 * If the dump on oops happens, it will be much appreciated
124 * to not have to wait for all that output. Anyway this can be
125 * boot time and run time configurable.
127 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
129 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
131 /* trace_types holds a link list of available tracers. */
132 static struct tracer
*trace_types __read_mostly
;
134 /* current_trace points to the tracer that is currently active */
135 static struct tracer
*current_trace __read_mostly
;
138 * max_tracer_type_len is used to simplify the allocating of
139 * buffers to read userspace tracer names. We keep track of
140 * the longest tracer name registered.
142 static int max_tracer_type_len
;
145 * trace_types_lock is used to protect the trace_types list.
146 * This lock is also used to keep user access serialized.
147 * Accesses from userspace will grab this lock while userspace
148 * activities happen inside the kernel.
150 static DEFINE_MUTEX(trace_types_lock
);
152 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
153 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
155 /* trace_flags holds iter_ctrl options */
156 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
;
159 * trace_wake_up - wake up tasks waiting for trace input
161 * Simply wakes up any task that is blocked on the trace_wait
162 * queue. These is used with trace_poll for tasks polling the trace.
164 void trace_wake_up(void)
167 * The runqueue_is_locked() can fail, but this is the best we
170 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
171 wake_up(&trace_wait
);
174 static int __init
set_buf_size(char *str
)
176 unsigned long buf_size
;
181 ret
= strict_strtoul(str
, 0, &buf_size
);
182 /* nr_entries can not be zero */
183 if (ret
< 0 || buf_size
== 0)
185 trace_buf_size
= buf_size
;
188 __setup("trace_buf_size=", set_buf_size
);
190 unsigned long nsecs_to_usecs(unsigned long nsecs
)
196 * TRACE_ITER_SYM_MASK masks the options in trace_flags that
197 * control the output of kernel symbols.
199 #define TRACE_ITER_SYM_MASK \
200 (TRACE_ITER_PRINT_PARENT|TRACE_ITER_SYM_OFFSET|TRACE_ITER_SYM_ADDR)
202 /* These must match the bit postions in trace_iterator_flags */
203 static const char *trace_options
[] = {
219 * ftrace_max_lock is used to protect the swapping of buffers
220 * when taking a max snapshot. The buffers themselves are
221 * protected by per_cpu spinlocks. But the action of the swap
222 * needs its own lock.
224 * This is defined as a raw_spinlock_t in order to help
225 * with performance when lockdep debugging is enabled.
227 static raw_spinlock_t ftrace_max_lock
=
228 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
231 * Copy the new maximum trace into the separate maximum-trace
232 * structure. (this way the maximum trace is permanently saved,
233 * for later retrieval via /debugfs/tracing/latency_trace)
236 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
238 struct trace_array_cpu
*data
= tr
->data
[cpu
];
241 max_tr
.time_start
= data
->preempt_timestamp
;
243 data
= max_tr
.data
[cpu
];
244 data
->saved_latency
= tracing_max_latency
;
246 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
247 data
->pid
= tsk
->pid
;
248 data
->uid
= tsk
->uid
;
249 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
250 data
->policy
= tsk
->policy
;
251 data
->rt_priority
= tsk
->rt_priority
;
253 /* record this tasks comm */
254 tracing_record_cmdline(current
);
258 * trace_seq_printf - sequence printing of trace information
259 * @s: trace sequence descriptor
260 * @fmt: printf format string
262 * The tracer may use either sequence operations or its own
263 * copy to user routines. To simplify formating of a trace
264 * trace_seq_printf is used to store strings into a special
265 * buffer (@s). Then the output may be either used by
266 * the sequencer or pulled into another buffer.
269 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
271 int len
= (PAGE_SIZE
- 1) - s
->len
;
279 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
282 /* If we can't write it all, don't bother writing anything */
292 * trace_seq_puts - trace sequence printing of simple string
293 * @s: trace sequence descriptor
294 * @str: simple string to record
296 * The tracer may use either the sequence operations or its own
297 * copy to user routines. This function records a simple string
298 * into a special buffer (@s) for later retrieval by a sequencer
299 * or other mechanism.
302 trace_seq_puts(struct trace_seq
*s
, const char *str
)
304 int len
= strlen(str
);
306 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
309 memcpy(s
->buffer
+ s
->len
, str
, len
);
316 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
318 if (s
->len
>= (PAGE_SIZE
- 1))
321 s
->buffer
[s
->len
++] = c
;
327 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
329 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
332 memcpy(s
->buffer
+ s
->len
, mem
, len
);
339 static const char hex2asc
[] = "0123456789abcdef";
342 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
344 unsigned char hex
[HEX_CHARS
];
345 unsigned char *data
= mem
;
349 BUG_ON(len
>= HEX_CHARS
);
352 for (i
= 0, j
= 0; i
< len
; i
++) {
354 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
358 hex
[j
++] = hex2asc
[byte
& 0x0f];
359 hex
[j
++] = hex2asc
[byte
>> 4];
363 return trace_seq_putmem(s
, hex
, j
);
367 trace_seq_reset(struct trace_seq
*s
)
373 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
378 if (s
->len
<= s
->readpos
)
381 len
= s
->len
- s
->readpos
;
384 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
393 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
395 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
398 seq_puts(m
, s
->buffer
);
404 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
406 * @tsk: the task with the latency
407 * @cpu: The cpu that initiated the trace.
409 * Flip the buffers between the @tr and the max_tr and record information
410 * about which task was the cause of this latency.
413 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
415 struct ring_buffer
*buf
= tr
->buffer
;
417 WARN_ON_ONCE(!irqs_disabled());
418 __raw_spin_lock(&ftrace_max_lock
);
420 tr
->buffer
= max_tr
.buffer
;
423 ftrace_disable_cpu();
424 ring_buffer_reset(tr
->buffer
);
427 __update_max_tr(tr
, tsk
, cpu
);
428 __raw_spin_unlock(&ftrace_max_lock
);
432 * update_max_tr_single - only copy one trace over, and reset the rest
434 * @tsk - task with the latency
435 * @cpu - the cpu of the buffer to copy.
437 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
440 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
444 WARN_ON_ONCE(!irqs_disabled());
445 __raw_spin_lock(&ftrace_max_lock
);
447 ftrace_disable_cpu();
449 ring_buffer_reset(max_tr
.buffer
);
450 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
456 __update_max_tr(tr
, tsk
, cpu
);
457 __raw_spin_unlock(&ftrace_max_lock
);
461 * register_tracer - register a tracer with the ftrace system.
462 * @type - the plugin for the tracer
464 * Register a new plugin tracer.
466 int register_tracer(struct tracer
*type
)
473 pr_info("Tracer must have a name\n");
477 mutex_lock(&trace_types_lock
);
478 for (t
= trace_types
; t
; t
= t
->next
) {
479 if (strcmp(type
->name
, t
->name
) == 0) {
481 pr_info("Trace %s already registered\n",
488 #ifdef CONFIG_FTRACE_STARTUP_TEST
489 if (type
->selftest
) {
490 struct tracer
*saved_tracer
= current_trace
;
491 struct trace_array
*tr
= &global_trace
;
492 int saved_ctrl
= tr
->ctrl
;
495 * Run a selftest on this tracer.
496 * Here we reset the trace buffer, and set the current
497 * tracer to be this tracer. The tracer can then run some
498 * internal tracing to verify that everything is in order.
499 * If we fail, we do not register this tracer.
501 for_each_tracing_cpu(i
) {
502 tracing_reset(tr
, i
);
504 current_trace
= type
;
506 /* the test is responsible for initializing and enabling */
507 pr_info("Testing tracer %s: ", type
->name
);
508 ret
= type
->selftest(type
, tr
);
509 /* the test is responsible for resetting too */
510 current_trace
= saved_tracer
;
511 tr
->ctrl
= saved_ctrl
;
513 printk(KERN_CONT
"FAILED!\n");
516 /* Only reset on passing, to avoid touching corrupted buffers */
517 for_each_tracing_cpu(i
) {
518 tracing_reset(tr
, i
);
520 printk(KERN_CONT
"PASSED\n");
524 type
->next
= trace_types
;
526 len
= strlen(type
->name
);
527 if (len
> max_tracer_type_len
)
528 max_tracer_type_len
= len
;
531 mutex_unlock(&trace_types_lock
);
536 void unregister_tracer(struct tracer
*type
)
541 mutex_lock(&trace_types_lock
);
542 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
546 pr_info("Trace %s not registered\n", type
->name
);
551 if (strlen(type
->name
) != max_tracer_type_len
)
554 max_tracer_type_len
= 0;
555 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
556 len
= strlen((*t
)->name
);
557 if (len
> max_tracer_type_len
)
558 max_tracer_type_len
= len
;
561 mutex_unlock(&trace_types_lock
);
564 void tracing_reset(struct trace_array
*tr
, int cpu
)
566 ftrace_disable_cpu();
567 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
571 #define SAVED_CMDLINES 128
572 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
573 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
574 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
575 static int cmdline_idx
;
576 static DEFINE_SPINLOCK(trace_cmdline_lock
);
578 /* temporary disable recording */
579 atomic_t trace_record_cmdline_disabled __read_mostly
;
581 static void trace_init_cmdlines(void)
583 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
584 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
588 void trace_stop_cmdline_recording(void);
590 static void trace_save_cmdline(struct task_struct
*tsk
)
595 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
599 * It's not the end of the world if we don't get
600 * the lock, but we also don't want to spin
601 * nor do we want to disable interrupts,
602 * so if we miss here, then better luck next time.
604 if (!spin_trylock(&trace_cmdline_lock
))
607 idx
= map_pid_to_cmdline
[tsk
->pid
];
608 if (idx
>= SAVED_CMDLINES
) {
609 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
611 map
= map_cmdline_to_pid
[idx
];
612 if (map
<= PID_MAX_DEFAULT
)
613 map_pid_to_cmdline
[map
] = (unsigned)-1;
615 map_pid_to_cmdline
[tsk
->pid
] = idx
;
620 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
622 spin_unlock(&trace_cmdline_lock
);
625 static char *trace_find_cmdline(int pid
)
627 char *cmdline
= "<...>";
633 if (pid
> PID_MAX_DEFAULT
)
636 map
= map_pid_to_cmdline
[pid
];
637 if (map
>= SAVED_CMDLINES
)
640 cmdline
= saved_cmdlines
[map
];
646 void tracing_record_cmdline(struct task_struct
*tsk
)
648 if (atomic_read(&trace_record_cmdline_disabled
))
651 trace_save_cmdline(tsk
);
655 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
658 struct task_struct
*tsk
= current
;
660 entry
->preempt_count
= pc
& 0xff;
661 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
663 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
664 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
665 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
666 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
670 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
671 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
674 struct ring_buffer_event
*event
;
675 struct ftrace_entry
*entry
;
676 unsigned long irq_flags
;
678 /* If we are reading the ring buffer, don't trace */
679 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
682 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
686 entry
= ring_buffer_event_data(event
);
687 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
688 entry
->ent
.type
= TRACE_FN
;
690 entry
->parent_ip
= parent_ip
;
691 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
695 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
696 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
699 if (likely(!atomic_read(&data
->disabled
)))
700 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
703 static void ftrace_trace_stack(struct trace_array
*tr
,
704 struct trace_array_cpu
*data
,
708 struct ring_buffer_event
*event
;
709 struct stack_entry
*entry
;
710 struct stack_trace trace
;
711 unsigned long irq_flags
;
713 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
716 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
720 entry
= ring_buffer_event_data(event
);
721 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
722 entry
->ent
.type
= TRACE_STACK
;
724 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
726 trace
.nr_entries
= 0;
727 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
729 trace
.entries
= entry
->caller
;
731 save_stack_trace(&trace
);
732 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
735 void __trace_stack(struct trace_array
*tr
,
736 struct trace_array_cpu
*data
,
740 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
744 ftrace_trace_special(void *__tr
, void *__data
,
745 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
748 struct ring_buffer_event
*event
;
749 struct trace_array_cpu
*data
= __data
;
750 struct trace_array
*tr
= __tr
;
751 struct special_entry
*entry
;
752 unsigned long irq_flags
;
754 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
758 entry
= ring_buffer_event_data(event
);
759 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
760 entry
->ent
.type
= TRACE_SPECIAL
;
764 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
765 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
771 __trace_special(void *__tr
, void *__data
,
772 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
774 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
778 tracing_sched_switch_trace(struct trace_array
*tr
,
779 struct trace_array_cpu
*data
,
780 struct task_struct
*prev
,
781 struct task_struct
*next
,
782 unsigned long flags
, int pc
)
784 struct ring_buffer_event
*event
;
785 struct ctx_switch_entry
*entry
;
786 unsigned long irq_flags
;
788 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
792 entry
= ring_buffer_event_data(event
);
793 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
794 entry
->ent
.type
= TRACE_CTX
;
795 entry
->prev_pid
= prev
->pid
;
796 entry
->prev_prio
= prev
->prio
;
797 entry
->prev_state
= prev
->state
;
798 entry
->next_pid
= next
->pid
;
799 entry
->next_prio
= next
->prio
;
800 entry
->next_state
= next
->state
;
801 entry
->next_cpu
= task_cpu(next
);
802 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
803 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
807 tracing_sched_wakeup_trace(struct trace_array
*tr
,
808 struct trace_array_cpu
*data
,
809 struct task_struct
*wakee
,
810 struct task_struct
*curr
,
811 unsigned long flags
, int pc
)
813 struct ring_buffer_event
*event
;
814 struct ctx_switch_entry
*entry
;
815 unsigned long irq_flags
;
817 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
821 entry
= ring_buffer_event_data(event
);
822 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
823 entry
->ent
.type
= TRACE_WAKE
;
824 entry
->prev_pid
= curr
->pid
;
825 entry
->prev_prio
= curr
->prio
;
826 entry
->prev_state
= curr
->state
;
827 entry
->next_pid
= wakee
->pid
;
828 entry
->next_prio
= wakee
->prio
;
829 entry
->next_state
= wakee
->state
;
830 entry
->next_cpu
= task_cpu(wakee
);
831 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
832 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
838 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
840 struct trace_array
*tr
= &global_trace
;
841 struct trace_array_cpu
*data
;
845 if (tracing_disabled
|| !tr
->ctrl
)
848 pc
= preempt_count();
849 preempt_disable_notrace();
850 cpu
= raw_smp_processor_id();
851 data
= tr
->data
[cpu
];
853 if (likely(!atomic_read(&data
->disabled
)))
854 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
856 preempt_enable_notrace();
861 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
863 struct trace_array
*tr
= &global_trace
;
864 struct trace_array_cpu
*data
;
870 if (unlikely(!ftrace_function_enabled
))
876 pc
= preempt_count();
877 resched
= need_resched();
878 preempt_disable_notrace();
879 local_save_flags(flags
);
880 cpu
= raw_smp_processor_id();
881 data
= tr
->data
[cpu
];
882 disabled
= atomic_inc_return(&data
->disabled
);
884 if (likely(disabled
== 1))
885 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
887 atomic_dec(&data
->disabled
);
889 preempt_enable_no_resched_notrace();
891 preempt_enable_notrace();
894 static struct ftrace_ops trace_ops __read_mostly
=
896 .func
= function_trace_call
,
899 void tracing_start_function_trace(void)
901 ftrace_function_enabled
= 0;
902 register_ftrace_function(&trace_ops
);
904 ftrace_function_enabled
= 1;
907 void tracing_stop_function_trace(void)
909 ftrace_function_enabled
= 0;
910 unregister_ftrace_function(&trace_ops
);
914 enum trace_file_type
{
915 TRACE_FILE_LAT_FMT
= 1,
918 static void trace_iterator_increment(struct trace_iterator
*iter
, int cpu
)
920 /* Don't allow ftrace to trace into the ring buffers */
921 ftrace_disable_cpu();
924 if (iter
->buffer_iter
[iter
->cpu
])
925 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
930 static struct trace_entry
*
931 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
933 struct ring_buffer_event
*event
;
934 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
936 /* Don't allow ftrace to trace into the ring buffers */
937 ftrace_disable_cpu();
940 event
= ring_buffer_iter_peek(buf_iter
, ts
);
942 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
946 return event
? ring_buffer_event_data(event
) : NULL
;
949 static struct trace_entry
*
950 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
952 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
953 struct trace_entry
*ent
, *next
= NULL
;
958 for_each_tracing_cpu(cpu
) {
960 if (ring_buffer_empty_cpu(buffer
, cpu
))
963 ent
= peek_next_entry(iter
, cpu
, &ts
);
966 * Pick the entry with the smallest timestamp:
968 if (ent
&& (!next
|| ts
< next_ts
)) {
984 /* Find the next real entry, without updating the iterator itself */
985 static struct trace_entry
*
986 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
988 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
991 /* Find the next real entry, and increment the iterator to the next entry */
992 static void *find_next_entry_inc(struct trace_iterator
*iter
)
994 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
997 trace_iterator_increment(iter
, iter
->cpu
);
999 return iter
->ent
? iter
: NULL
;
1002 static void trace_consume(struct trace_iterator
*iter
)
1004 /* Don't allow ftrace to trace into the ring buffers */
1005 ftrace_disable_cpu();
1006 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1007 ftrace_enable_cpu();
1010 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1012 struct trace_iterator
*iter
= m
->private;
1018 /* can't go backwards */
1023 ent
= find_next_entry_inc(iter
);
1027 while (ent
&& iter
->idx
< i
)
1028 ent
= find_next_entry_inc(iter
);
1035 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1037 struct trace_iterator
*iter
= m
->private;
1042 mutex_lock(&trace_types_lock
);
1044 if (!current_trace
|| current_trace
!= iter
->trace
) {
1045 mutex_unlock(&trace_types_lock
);
1049 atomic_inc(&trace_record_cmdline_disabled
);
1051 /* let the tracer grab locks here if needed */
1052 if (current_trace
->start
)
1053 current_trace
->start(iter
);
1055 if (*pos
!= iter
->pos
) {
1060 ftrace_disable_cpu();
1062 for_each_tracing_cpu(cpu
) {
1063 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1066 ftrace_enable_cpu();
1068 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1073 p
= s_next(m
, p
, &l
);
1079 static void s_stop(struct seq_file
*m
, void *p
)
1081 struct trace_iterator
*iter
= m
->private;
1083 atomic_dec(&trace_record_cmdline_disabled
);
1085 /* let the tracer release locks here if needed */
1086 if (current_trace
&& current_trace
== iter
->trace
&& iter
->trace
->stop
)
1087 iter
->trace
->stop(iter
);
1089 mutex_unlock(&trace_types_lock
);
1092 #define KRETPROBE_MSG "[unknown/kretprobe'd]"
1094 #ifdef CONFIG_KRETPROBES
1095 static inline int kretprobed(unsigned long addr
)
1097 return addr
== (unsigned long)kretprobe_trampoline
;
1100 static inline int kretprobed(unsigned long addr
)
1104 #endif /* CONFIG_KRETPROBES */
1107 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1109 #ifdef CONFIG_KALLSYMS
1110 char str
[KSYM_SYMBOL_LEN
];
1112 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1114 return trace_seq_printf(s
, fmt
, str
);
1120 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1121 unsigned long address
)
1123 #ifdef CONFIG_KALLSYMS
1124 char str
[KSYM_SYMBOL_LEN
];
1126 sprint_symbol(str
, address
);
1127 return trace_seq_printf(s
, fmt
, str
);
1132 #ifndef CONFIG_64BIT
1133 # define IP_FMT "%08lx"
1135 # define IP_FMT "%016lx"
1139 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1144 return trace_seq_printf(s
, "0");
1146 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1147 ret
= seq_print_sym_offset(s
, "%s", ip
);
1149 ret
= seq_print_sym_short(s
, "%s", ip
);
1154 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1155 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1159 static void print_lat_help_header(struct seq_file
*m
)
1161 seq_puts(m
, "# _------=> CPU# \n");
1162 seq_puts(m
, "# / _-----=> irqs-off \n");
1163 seq_puts(m
, "# | / _----=> need-resched \n");
1164 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1165 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1166 seq_puts(m
, "# |||| / \n");
1167 seq_puts(m
, "# ||||| delay \n");
1168 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1169 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1172 static void print_func_help_header(struct seq_file
*m
)
1174 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1175 seq_puts(m
, "# | | | | |\n");
1180 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1182 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1183 struct trace_array
*tr
= iter
->tr
;
1184 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1185 struct tracer
*type
= current_trace
;
1186 unsigned long total
;
1187 unsigned long entries
;
1188 const char *name
= "preemption";
1193 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1195 ring_buffer_overruns(iter
->tr
->buffer
);
1197 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1199 seq_puts(m
, "-----------------------------------"
1200 "---------------------------------\n");
1201 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1202 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1203 nsecs_to_usecs(data
->saved_latency
),
1207 #if defined(CONFIG_PREEMPT_NONE)
1209 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1211 #elif defined(CONFIG_PREEMPT)
1216 /* These are reserved for later use */
1219 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1223 seq_puts(m
, " -----------------\n");
1224 seq_printf(m
, " | task: %.16s-%d "
1225 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1226 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1227 data
->policy
, data
->rt_priority
);
1228 seq_puts(m
, " -----------------\n");
1230 if (data
->critical_start
) {
1231 seq_puts(m
, " => started at: ");
1232 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1233 trace_print_seq(m
, &iter
->seq
);
1234 seq_puts(m
, "\n => ended at: ");
1235 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1236 trace_print_seq(m
, &iter
->seq
);
1244 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1246 int hardirq
, softirq
;
1249 comm
= trace_find_cmdline(entry
->pid
);
1251 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1252 trace_seq_printf(s
, "%3d", cpu
);
1253 trace_seq_printf(s
, "%c%c",
1254 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' : '.',
1255 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1257 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1258 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1259 if (hardirq
&& softirq
) {
1260 trace_seq_putc(s
, 'H');
1263 trace_seq_putc(s
, 'h');
1266 trace_seq_putc(s
, 's');
1268 trace_seq_putc(s
, '.');
1272 if (entry
->preempt_count
)
1273 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1275 trace_seq_puts(s
, ".");
1278 unsigned long preempt_mark_thresh
= 100;
1281 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1282 unsigned long rel_usecs
)
1284 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1285 if (rel_usecs
> preempt_mark_thresh
)
1286 trace_seq_puts(s
, "!: ");
1287 else if (rel_usecs
> 1)
1288 trace_seq_puts(s
, "+: ");
1290 trace_seq_puts(s
, " : ");
1293 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1296 * The message is supposed to contain an ending newline.
1297 * If the printing stops prematurely, try to add a newline of our own.
1299 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1301 struct trace_entry
*ent
;
1302 struct trace_field_cont
*cont
;
1305 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1306 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1307 trace_seq_putc(s
, '\n');
1312 cont
= (struct trace_field_cont
*)ent
;
1314 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1316 ftrace_disable_cpu();
1318 if (iter
->buffer_iter
[iter
->cpu
])
1319 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1321 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1323 ftrace_enable_cpu();
1325 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1326 } while (ent
&& ent
->type
== TRACE_CONT
);
1329 trace_seq_putc(s
, '\n');
1332 static enum print_line_t
1333 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1335 struct trace_seq
*s
= &iter
->seq
;
1336 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1337 struct trace_entry
*next_entry
;
1338 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1339 struct trace_entry
*entry
= iter
->ent
;
1340 unsigned long abs_usecs
;
1341 unsigned long rel_usecs
;
1348 if (entry
->type
== TRACE_CONT
)
1349 return TRACE_TYPE_HANDLED
;
1351 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1354 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1355 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1358 comm
= trace_find_cmdline(entry
->pid
);
1359 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1360 " %ld.%03ldms (+%ld.%03ldms): ",
1362 entry
->pid
, cpu
, entry
->flags
,
1363 entry
->preempt_count
, trace_idx
,
1366 abs_usecs
% 1000, rel_usecs
/1000,
1369 lat_print_generic(s
, entry
, cpu
);
1370 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1372 switch (entry
->type
) {
1374 struct ftrace_entry
*field
;
1376 trace_assign_type(field
, entry
);
1378 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1379 trace_seq_puts(s
, " (");
1380 if (kretprobed(field
->parent_ip
))
1381 trace_seq_puts(s
, KRETPROBE_MSG
);
1383 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1384 trace_seq_puts(s
, ")\n");
1389 struct ctx_switch_entry
*field
;
1391 trace_assign_type(field
, entry
);
1393 T
= field
->next_state
< sizeof(state_to_char
) ?
1394 state_to_char
[field
->next_state
] : 'X';
1396 state
= field
->prev_state
?
1397 __ffs(field
->prev_state
) + 1 : 0;
1398 S
= state
< sizeof(state_to_char
) - 1 ? state_to_char
[state
] : 'X';
1399 comm
= trace_find_cmdline(field
->next_pid
);
1400 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1403 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1410 case TRACE_SPECIAL
: {
1411 struct special_entry
*field
;
1413 trace_assign_type(field
, entry
);
1415 trace_seq_printf(s
, "# %ld %ld %ld\n",
1422 struct stack_entry
*field
;
1424 trace_assign_type(field
, entry
);
1426 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1428 trace_seq_puts(s
, " <= ");
1429 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1431 trace_seq_puts(s
, "\n");
1435 struct print_entry
*field
;
1437 trace_assign_type(field
, entry
);
1439 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1440 trace_seq_printf(s
, ": %s", field
->buf
);
1441 if (entry
->flags
& TRACE_FLAG_CONT
)
1442 trace_seq_print_cont(s
, iter
);
1446 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1448 return TRACE_TYPE_HANDLED
;
1451 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1453 struct trace_seq
*s
= &iter
->seq
;
1454 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1455 struct trace_entry
*entry
;
1456 unsigned long usec_rem
;
1457 unsigned long long t
;
1466 if (entry
->type
== TRACE_CONT
)
1467 return TRACE_TYPE_HANDLED
;
1469 comm
= trace_find_cmdline(iter
->ent
->pid
);
1471 t
= ns2usecs(iter
->ts
);
1472 usec_rem
= do_div(t
, 1000000ULL);
1473 secs
= (unsigned long)t
;
1475 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1477 return TRACE_TYPE_PARTIAL_LINE
;
1478 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1480 return TRACE_TYPE_PARTIAL_LINE
;
1481 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1483 return TRACE_TYPE_PARTIAL_LINE
;
1485 switch (entry
->type
) {
1487 struct ftrace_entry
*field
;
1489 trace_assign_type(field
, entry
);
1491 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1493 return TRACE_TYPE_PARTIAL_LINE
;
1494 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
1496 ret
= trace_seq_printf(s
, " <-");
1498 return TRACE_TYPE_PARTIAL_LINE
;
1499 if (kretprobed(field
->parent_ip
))
1500 ret
= trace_seq_puts(s
, KRETPROBE_MSG
);
1502 ret
= seq_print_ip_sym(s
,
1506 return TRACE_TYPE_PARTIAL_LINE
;
1508 ret
= trace_seq_printf(s
, "\n");
1510 return TRACE_TYPE_PARTIAL_LINE
;
1515 struct ctx_switch_entry
*field
;
1517 trace_assign_type(field
, entry
);
1519 S
= field
->prev_state
< sizeof(state_to_char
) ?
1520 state_to_char
[field
->prev_state
] : 'X';
1521 T
= field
->next_state
< sizeof(state_to_char
) ?
1522 state_to_char
[field
->next_state
] : 'X';
1523 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
1527 entry
->type
== TRACE_CTX
? "==>" : " +",
1533 return TRACE_TYPE_PARTIAL_LINE
;
1536 case TRACE_SPECIAL
: {
1537 struct special_entry
*field
;
1539 trace_assign_type(field
, entry
);
1541 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1546 return TRACE_TYPE_PARTIAL_LINE
;
1550 struct stack_entry
*field
;
1552 trace_assign_type(field
, entry
);
1554 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1556 ret
= trace_seq_puts(s
, " <= ");
1558 return TRACE_TYPE_PARTIAL_LINE
;
1560 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
1563 return TRACE_TYPE_PARTIAL_LINE
;
1565 ret
= trace_seq_puts(s
, "\n");
1567 return TRACE_TYPE_PARTIAL_LINE
;
1571 struct print_entry
*field
;
1573 trace_assign_type(field
, entry
);
1575 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1576 trace_seq_printf(s
, ": %s", field
->buf
);
1577 if (entry
->flags
& TRACE_FLAG_CONT
)
1578 trace_seq_print_cont(s
, iter
);
1582 return TRACE_TYPE_HANDLED
;
1585 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1587 struct trace_seq
*s
= &iter
->seq
;
1588 struct trace_entry
*entry
;
1594 if (entry
->type
== TRACE_CONT
)
1595 return TRACE_TYPE_HANDLED
;
1597 ret
= trace_seq_printf(s
, "%d %d %llu ",
1598 entry
->pid
, iter
->cpu
, iter
->ts
);
1600 return TRACE_TYPE_PARTIAL_LINE
;
1602 switch (entry
->type
) {
1604 struct ftrace_entry
*field
;
1606 trace_assign_type(field
, entry
);
1608 ret
= trace_seq_printf(s
, "%x %x\n",
1612 return TRACE_TYPE_PARTIAL_LINE
;
1617 struct ctx_switch_entry
*field
;
1619 trace_assign_type(field
, entry
);
1621 S
= field
->prev_state
< sizeof(state_to_char
) ?
1622 state_to_char
[field
->prev_state
] : 'X';
1623 T
= field
->next_state
< sizeof(state_to_char
) ?
1624 state_to_char
[field
->next_state
] : 'X';
1625 if (entry
->type
== TRACE_WAKE
)
1627 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
1636 return TRACE_TYPE_PARTIAL_LINE
;
1641 struct special_entry
*field
;
1643 trace_assign_type(field
, entry
);
1645 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
1650 return TRACE_TYPE_PARTIAL_LINE
;
1654 struct print_entry
*field
;
1656 trace_assign_type(field
, entry
);
1658 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
1659 if (entry
->flags
& TRACE_FLAG_CONT
)
1660 trace_seq_print_cont(s
, iter
);
1664 return TRACE_TYPE_HANDLED
;
1667 #define SEQ_PUT_FIELD_RET(s, x) \
1669 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
1673 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
1675 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
1679 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1681 struct trace_seq
*s
= &iter
->seq
;
1682 unsigned char newline
= '\n';
1683 struct trace_entry
*entry
;
1688 if (entry
->type
== TRACE_CONT
)
1689 return TRACE_TYPE_HANDLED
;
1691 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1692 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1693 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1695 switch (entry
->type
) {
1697 struct ftrace_entry
*field
;
1699 trace_assign_type(field
, entry
);
1701 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
1702 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
1707 struct ctx_switch_entry
*field
;
1709 trace_assign_type(field
, entry
);
1711 S
= field
->prev_state
< sizeof(state_to_char
) ?
1712 state_to_char
[field
->prev_state
] : 'X';
1713 T
= field
->next_state
< sizeof(state_to_char
) ?
1714 state_to_char
[field
->next_state
] : 'X';
1715 if (entry
->type
== TRACE_WAKE
)
1717 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
1718 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
1719 SEQ_PUT_HEX_FIELD_RET(s
, S
);
1720 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
1721 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
1722 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
1723 SEQ_PUT_HEX_FIELD_RET(s
, T
);
1728 struct special_entry
*field
;
1730 trace_assign_type(field
, entry
);
1732 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
1733 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
1734 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
1738 SEQ_PUT_FIELD_RET(s
, newline
);
1740 return TRACE_TYPE_HANDLED
;
1743 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1745 struct trace_seq
*s
= &iter
->seq
;
1746 struct trace_entry
*entry
;
1750 if (entry
->type
== TRACE_CONT
)
1751 return TRACE_TYPE_HANDLED
;
1753 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1754 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1755 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1757 switch (entry
->type
) {
1759 struct ftrace_entry
*field
;
1761 trace_assign_type(field
, entry
);
1763 SEQ_PUT_FIELD_RET(s
, field
->ip
);
1764 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
1768 struct ctx_switch_entry
*field
;
1770 trace_assign_type(field
, entry
);
1772 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
1773 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
1774 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
1775 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
1776 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
1777 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
1782 struct special_entry
*field
;
1784 trace_assign_type(field
, entry
);
1786 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
1787 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
1788 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
1795 static int trace_empty(struct trace_iterator
*iter
)
1799 for_each_tracing_cpu(cpu
) {
1800 if (iter
->buffer_iter
[cpu
]) {
1801 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1804 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1812 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1814 enum print_line_t ret
;
1816 if (iter
->trace
&& iter
->trace
->print_line
) {
1817 ret
= iter
->trace
->print_line(iter
);
1818 if (ret
!= TRACE_TYPE_UNHANDLED
)
1822 if (trace_flags
& TRACE_ITER_BIN
)
1823 return print_bin_fmt(iter
);
1825 if (trace_flags
& TRACE_ITER_HEX
)
1826 return print_hex_fmt(iter
);
1828 if (trace_flags
& TRACE_ITER_RAW
)
1829 return print_raw_fmt(iter
);
1831 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1832 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
1834 return print_trace_fmt(iter
);
1837 static int s_show(struct seq_file
*m
, void *v
)
1839 struct trace_iterator
*iter
= v
;
1841 if (iter
->ent
== NULL
) {
1843 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1846 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1847 /* print nothing if the buffers are empty */
1848 if (trace_empty(iter
))
1850 print_trace_header(m
, iter
);
1851 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1852 print_lat_help_header(m
);
1854 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1855 print_func_help_header(m
);
1858 print_trace_line(iter
);
1859 trace_print_seq(m
, &iter
->seq
);
1865 static struct seq_operations tracer_seq_ops
= {
1872 static struct trace_iterator
*
1873 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1875 struct trace_iterator
*iter
;
1879 if (tracing_disabled
) {
1884 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1890 mutex_lock(&trace_types_lock
);
1891 if (current_trace
&& current_trace
->print_max
)
1894 iter
->tr
= inode
->i_private
;
1895 iter
->trace
= current_trace
;
1898 for_each_tracing_cpu(cpu
) {
1900 iter
->buffer_iter
[cpu
] =
1901 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1903 if (!iter
->buffer_iter
[cpu
])
1907 /* TODO stop tracer */
1908 *ret
= seq_open(file
, &tracer_seq_ops
);
1912 m
= file
->private_data
;
1915 /* stop the trace while dumping */
1916 if (iter
->tr
->ctrl
) {
1918 ftrace_function_enabled
= 0;
1921 if (iter
->trace
&& iter
->trace
->open
)
1922 iter
->trace
->open(iter
);
1924 mutex_unlock(&trace_types_lock
);
1930 for_each_tracing_cpu(cpu
) {
1931 if (iter
->buffer_iter
[cpu
])
1932 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1934 mutex_unlock(&trace_types_lock
);
1936 return ERR_PTR(-ENOMEM
);
1939 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1941 if (tracing_disabled
)
1944 filp
->private_data
= inode
->i_private
;
1948 int tracing_release(struct inode
*inode
, struct file
*file
)
1950 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1951 struct trace_iterator
*iter
= m
->private;
1954 mutex_lock(&trace_types_lock
);
1955 for_each_tracing_cpu(cpu
) {
1956 if (iter
->buffer_iter
[cpu
])
1957 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1960 if (iter
->trace
&& iter
->trace
->close
)
1961 iter
->trace
->close(iter
);
1963 /* reenable tracing if it was previously enabled */
1964 if (iter
->tr
->ctrl
) {
1967 * It is safe to enable function tracing even if it
1970 ftrace_function_enabled
= 1;
1972 mutex_unlock(&trace_types_lock
);
1974 seq_release(inode
, file
);
1979 static int tracing_open(struct inode
*inode
, struct file
*file
)
1983 __tracing_open(inode
, file
, &ret
);
1988 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1990 struct trace_iterator
*iter
;
1993 iter
= __tracing_open(inode
, file
, &ret
);
1996 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2003 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2005 struct tracer
*t
= m
->private;
2017 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2019 struct tracer
*t
= m
->private;
2022 mutex_lock(&trace_types_lock
);
2023 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2029 static void t_stop(struct seq_file
*m
, void *p
)
2031 mutex_unlock(&trace_types_lock
);
2034 static int t_show(struct seq_file
*m
, void *v
)
2036 struct tracer
*t
= v
;
2041 seq_printf(m
, "%s", t
->name
);
2050 static struct seq_operations show_traces_seq_ops
= {
2057 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2061 if (tracing_disabled
)
2064 ret
= seq_open(file
, &show_traces_seq_ops
);
2066 struct seq_file
*m
= file
->private_data
;
2067 m
->private = trace_types
;
2073 static struct file_operations tracing_fops
= {
2074 .open
= tracing_open
,
2076 .llseek
= seq_lseek
,
2077 .release
= tracing_release
,
2080 static struct file_operations tracing_lt_fops
= {
2081 .open
= tracing_lt_open
,
2083 .llseek
= seq_lseek
,
2084 .release
= tracing_release
,
2087 static struct file_operations show_traces_fops
= {
2088 .open
= show_traces_open
,
2090 .release
= seq_release
,
2094 * Only trace on a CPU if the bitmask is set:
2096 static cpumask_t tracing_cpumask
= CPU_MASK_ALL
;
2099 * When tracing/tracing_cpu_mask is modified then this holds
2100 * the new bitmask we are about to install:
2102 static cpumask_t tracing_cpumask_new
;
2105 * The tracer itself will not take this lock, but still we want
2106 * to provide a consistent cpumask to user-space:
2108 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2111 * Temporary storage for the character representation of the
2112 * CPU bitmask (and one more byte for the newline):
2114 static char mask_str
[NR_CPUS
+ 1];
2117 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2118 size_t count
, loff_t
*ppos
)
2122 mutex_lock(&tracing_cpumask_update_lock
);
2124 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2125 if (count
- len
< 2) {
2129 len
+= sprintf(mask_str
+ len
, "\n");
2130 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2133 mutex_unlock(&tracing_cpumask_update_lock
);
2139 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2140 size_t count
, loff_t
*ppos
)
2144 mutex_lock(&tracing_cpumask_update_lock
);
2145 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2149 raw_local_irq_disable();
2150 __raw_spin_lock(&ftrace_max_lock
);
2151 for_each_tracing_cpu(cpu
) {
2153 * Increase/decrease the disabled counter if we are
2154 * about to flip a bit in the cpumask:
2156 if (cpu_isset(cpu
, tracing_cpumask
) &&
2157 !cpu_isset(cpu
, tracing_cpumask_new
)) {
2158 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2160 if (!cpu_isset(cpu
, tracing_cpumask
) &&
2161 cpu_isset(cpu
, tracing_cpumask_new
)) {
2162 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2165 __raw_spin_unlock(&ftrace_max_lock
);
2166 raw_local_irq_enable();
2168 tracing_cpumask
= tracing_cpumask_new
;
2170 mutex_unlock(&tracing_cpumask_update_lock
);
2175 mutex_unlock(&tracing_cpumask_update_lock
);
2180 static struct file_operations tracing_cpumask_fops
= {
2181 .open
= tracing_open_generic
,
2182 .read
= tracing_cpumask_read
,
2183 .write
= tracing_cpumask_write
,
2187 tracing_iter_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2188 size_t cnt
, loff_t
*ppos
)
2195 /* calulate max size */
2196 for (i
= 0; trace_options
[i
]; i
++) {
2197 len
+= strlen(trace_options
[i
]);
2198 len
+= 3; /* "no" and space */
2201 /* +2 for \n and \0 */
2202 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2206 for (i
= 0; trace_options
[i
]; i
++) {
2207 if (trace_flags
& (1 << i
))
2208 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2210 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2213 r
+= sprintf(buf
+ r
, "\n");
2214 WARN_ON(r
>= len
+ 2);
2216 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2224 tracing_iter_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2225 size_t cnt
, loff_t
*ppos
)
2232 if (cnt
>= sizeof(buf
))
2235 if (copy_from_user(&buf
, ubuf
, cnt
))
2240 if (strncmp(buf
, "no", 2) == 0) {
2245 for (i
= 0; trace_options
[i
]; i
++) {
2246 int len
= strlen(trace_options
[i
]);
2248 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2250 trace_flags
&= ~(1 << i
);
2252 trace_flags
|= (1 << i
);
2257 * If no option could be set, return an error:
2259 if (!trace_options
[i
])
2267 static struct file_operations tracing_iter_fops
= {
2268 .open
= tracing_open_generic
,
2269 .read
= tracing_iter_ctrl_read
,
2270 .write
= tracing_iter_ctrl_write
,
2273 static const char readme_msg
[] =
2274 "tracing mini-HOWTO:\n\n"
2276 "# mount -t debugfs nodev /debug\n\n"
2277 "# cat /debug/tracing/available_tracers\n"
2278 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2279 "# cat /debug/tracing/current_tracer\n"
2281 "# echo sched_switch > /debug/tracing/current_tracer\n"
2282 "# cat /debug/tracing/current_tracer\n"
2284 "# cat /debug/tracing/iter_ctrl\n"
2285 "noprint-parent nosym-offset nosym-addr noverbose\n"
2286 "# echo print-parent > /debug/tracing/iter_ctrl\n"
2287 "# echo 1 > /debug/tracing/tracing_enabled\n"
2288 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2289 "echo 0 > /debug/tracing/tracing_enabled\n"
2293 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2294 size_t cnt
, loff_t
*ppos
)
2296 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2297 readme_msg
, strlen(readme_msg
));
2300 static struct file_operations tracing_readme_fops
= {
2301 .open
= tracing_open_generic
,
2302 .read
= tracing_readme_read
,
2306 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2307 size_t cnt
, loff_t
*ppos
)
2309 struct trace_array
*tr
= filp
->private_data
;
2313 r
= sprintf(buf
, "%ld\n", tr
->ctrl
);
2314 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2318 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2319 size_t cnt
, loff_t
*ppos
)
2321 struct trace_array
*tr
= filp
->private_data
;
2326 if (cnt
>= sizeof(buf
))
2329 if (copy_from_user(&buf
, ubuf
, cnt
))
2334 ret
= strict_strtoul(buf
, 10, &val
);
2340 mutex_lock(&trace_types_lock
);
2341 if (tr
->ctrl
^ val
) {
2349 if (current_trace
&& current_trace
->ctrl_update
)
2350 current_trace
->ctrl_update(tr
);
2352 mutex_unlock(&trace_types_lock
);
2360 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2361 size_t cnt
, loff_t
*ppos
)
2363 char buf
[max_tracer_type_len
+2];
2366 mutex_lock(&trace_types_lock
);
2368 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2370 r
= sprintf(buf
, "\n");
2371 mutex_unlock(&trace_types_lock
);
2373 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2377 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2378 size_t cnt
, loff_t
*ppos
)
2380 struct trace_array
*tr
= &global_trace
;
2382 char buf
[max_tracer_type_len
+1];
2385 if (cnt
> max_tracer_type_len
)
2386 cnt
= max_tracer_type_len
;
2388 if (copy_from_user(&buf
, ubuf
, cnt
))
2393 /* strip ending whitespace. */
2394 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2397 mutex_lock(&trace_types_lock
);
2398 for (t
= trace_types
; t
; t
= t
->next
) {
2399 if (strcmp(t
->name
, buf
) == 0)
2402 if (!t
|| t
== current_trace
)
2405 if (current_trace
&& current_trace
->reset
)
2406 current_trace
->reset(tr
);
2413 mutex_unlock(&trace_types_lock
);
2421 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2422 size_t cnt
, loff_t
*ppos
)
2424 unsigned long *ptr
= filp
->private_data
;
2428 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2429 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2430 if (r
> sizeof(buf
))
2432 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2436 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2437 size_t cnt
, loff_t
*ppos
)
2439 long *ptr
= filp
->private_data
;
2444 if (cnt
>= sizeof(buf
))
2447 if (copy_from_user(&buf
, ubuf
, cnt
))
2452 ret
= strict_strtoul(buf
, 10, &val
);
2461 static atomic_t tracing_reader
;
2463 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2465 struct trace_iterator
*iter
;
2467 if (tracing_disabled
)
2470 /* We only allow for reader of the pipe */
2471 if (atomic_inc_return(&tracing_reader
) != 1) {
2472 atomic_dec(&tracing_reader
);
2476 /* create a buffer to store the information to pass to userspace */
2477 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2481 mutex_lock(&trace_types_lock
);
2482 iter
->tr
= &global_trace
;
2483 iter
->trace
= current_trace
;
2484 filp
->private_data
= iter
;
2486 if (iter
->trace
->pipe_open
)
2487 iter
->trace
->pipe_open(iter
);
2488 mutex_unlock(&trace_types_lock
);
2493 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2495 struct trace_iterator
*iter
= file
->private_data
;
2498 atomic_dec(&tracing_reader
);
2504 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2506 struct trace_iterator
*iter
= filp
->private_data
;
2508 if (trace_flags
& TRACE_ITER_BLOCK
) {
2510 * Always select as readable when in blocking mode
2512 return POLLIN
| POLLRDNORM
;
2514 if (!trace_empty(iter
))
2515 return POLLIN
| POLLRDNORM
;
2516 poll_wait(filp
, &trace_wait
, poll_table
);
2517 if (!trace_empty(iter
))
2518 return POLLIN
| POLLRDNORM
;
2528 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2529 size_t cnt
, loff_t
*ppos
)
2531 struct trace_iterator
*iter
= filp
->private_data
;
2534 /* return any leftover data */
2535 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2539 trace_seq_reset(&iter
->seq
);
2541 mutex_lock(&trace_types_lock
);
2542 if (iter
->trace
->read
) {
2543 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2550 while (trace_empty(iter
)) {
2552 if ((filp
->f_flags
& O_NONBLOCK
)) {
2558 * This is a make-shift waitqueue. The reason we don't use
2559 * an actual wait queue is because:
2560 * 1) we only ever have one waiter
2561 * 2) the tracing, traces all functions, we don't want
2562 * the overhead of calling wake_up and friends
2563 * (and tracing them too)
2564 * Anyway, this is really very primitive wakeup.
2566 set_current_state(TASK_INTERRUPTIBLE
);
2567 iter
->tr
->waiter
= current
;
2569 mutex_unlock(&trace_types_lock
);
2571 /* sleep for 100 msecs, and try again. */
2572 schedule_timeout(HZ
/10);
2574 mutex_lock(&trace_types_lock
);
2576 iter
->tr
->waiter
= NULL
;
2578 if (signal_pending(current
)) {
2583 if (iter
->trace
!= current_trace
)
2587 * We block until we read something and tracing is disabled.
2588 * We still block if tracing is disabled, but we have never
2589 * read anything. This allows a user to cat this file, and
2590 * then enable tracing. But after we have read something,
2591 * we give an EOF when tracing is again disabled.
2593 * iter->pos will be 0 if we haven't read anything.
2595 if (!tracer_enabled
&& iter
->pos
)
2601 /* stop when tracing is finished */
2602 if (trace_empty(iter
))
2605 if (cnt
>= PAGE_SIZE
)
2606 cnt
= PAGE_SIZE
- 1;
2608 /* reset all but tr, trace, and overruns */
2609 memset(&iter
->seq
, 0,
2610 sizeof(struct trace_iterator
) -
2611 offsetof(struct trace_iterator
, seq
));
2614 while (find_next_entry_inc(iter
) != NULL
) {
2615 enum print_line_t ret
;
2616 int len
= iter
->seq
.len
;
2618 ret
= print_trace_line(iter
);
2619 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2620 /* don't print partial lines */
2621 iter
->seq
.len
= len
;
2625 trace_consume(iter
);
2627 if (iter
->seq
.len
>= cnt
)
2631 /* Now copy what we have to the user */
2632 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2633 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2634 trace_seq_reset(&iter
->seq
);
2637 * If there was nothing to send to user, inspite of consuming trace
2638 * entries, go back to wait for more entries.
2644 mutex_unlock(&trace_types_lock
);
2650 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2651 size_t cnt
, loff_t
*ppos
)
2653 struct trace_array
*tr
= filp
->private_data
;
2657 r
= sprintf(buf
, "%lu\n", tr
->entries
);
2658 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2662 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2663 size_t cnt
, loff_t
*ppos
)
2668 struct trace_array
*tr
= filp
->private_data
;
2670 if (cnt
>= sizeof(buf
))
2673 if (copy_from_user(&buf
, ubuf
, cnt
))
2678 ret
= strict_strtoul(buf
, 10, &val
);
2682 /* must have at least 1 entry */
2686 mutex_lock(&trace_types_lock
);
2690 pr_info("ftrace: please disable tracing"
2691 " before modifying buffer size\n");
2695 if (val
!= global_trace
.entries
) {
2696 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2702 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2706 r
= ring_buffer_resize(global_trace
.buffer
,
2707 global_trace
.entries
);
2709 /* AARGH! We are left with different
2710 * size max buffer!!!! */
2712 tracing_disabled
= 1;
2717 global_trace
.entries
= val
;
2722 /* If check pages failed, return ENOMEM */
2723 if (tracing_disabled
)
2726 max_tr
.entries
= global_trace
.entries
;
2727 mutex_unlock(&trace_types_lock
);
2732 static int mark_printk(const char *fmt
, ...)
2736 va_start(args
, fmt
);
2737 ret
= trace_vprintk(0, fmt
, args
);
2743 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2744 size_t cnt
, loff_t
*fpos
)
2748 struct trace_array
*tr
= &global_trace
;
2750 if (!tr
->ctrl
|| tracing_disabled
)
2753 if (cnt
> TRACE_BUF_SIZE
)
2754 cnt
= TRACE_BUF_SIZE
;
2756 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2760 if (copy_from_user(buf
, ubuf
, cnt
)) {
2765 /* Cut from the first nil or newline. */
2767 end
= strchr(buf
, '\n');
2771 cnt
= mark_printk("%s\n", buf
);
2778 static struct file_operations tracing_max_lat_fops
= {
2779 .open
= tracing_open_generic
,
2780 .read
= tracing_max_lat_read
,
2781 .write
= tracing_max_lat_write
,
2784 static struct file_operations tracing_ctrl_fops
= {
2785 .open
= tracing_open_generic
,
2786 .read
= tracing_ctrl_read
,
2787 .write
= tracing_ctrl_write
,
2790 static struct file_operations set_tracer_fops
= {
2791 .open
= tracing_open_generic
,
2792 .read
= tracing_set_trace_read
,
2793 .write
= tracing_set_trace_write
,
2796 static struct file_operations tracing_pipe_fops
= {
2797 .open
= tracing_open_pipe
,
2798 .poll
= tracing_poll_pipe
,
2799 .read
= tracing_read_pipe
,
2800 .release
= tracing_release_pipe
,
2803 static struct file_operations tracing_entries_fops
= {
2804 .open
= tracing_open_generic
,
2805 .read
= tracing_entries_read
,
2806 .write
= tracing_entries_write
,
2809 static struct file_operations tracing_mark_fops
= {
2810 .open
= tracing_open_generic
,
2811 .write
= tracing_mark_write
,
2814 #ifdef CONFIG_DYNAMIC_FTRACE
2817 tracing_read_long(struct file
*filp
, char __user
*ubuf
,
2818 size_t cnt
, loff_t
*ppos
)
2820 unsigned long *p
= filp
->private_data
;
2824 r
= sprintf(buf
, "%ld\n", *p
);
2826 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2829 static struct file_operations tracing_read_long_fops
= {
2830 .open
= tracing_open_generic
,
2831 .read
= tracing_read_long
,
2835 static struct dentry
*d_tracer
;
2837 struct dentry
*tracing_init_dentry(void)
2844 d_tracer
= debugfs_create_dir("tracing", NULL
);
2846 if (!d_tracer
&& !once
) {
2848 pr_warning("Could not create debugfs directory 'tracing'\n");
2855 #ifdef CONFIG_FTRACE_SELFTEST
2856 /* Let selftest have access to static functions in this file */
2857 #include "trace_selftest.c"
2860 static __init
int tracer_init_debugfs(void)
2862 struct dentry
*d_tracer
;
2863 struct dentry
*entry
;
2865 d_tracer
= tracing_init_dentry();
2867 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2868 &global_trace
, &tracing_ctrl_fops
);
2870 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2872 entry
= debugfs_create_file("iter_ctrl", 0644, d_tracer
,
2873 NULL
, &tracing_iter_fops
);
2875 pr_warning("Could not create debugfs 'iter_ctrl' entry\n");
2877 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2878 NULL
, &tracing_cpumask_fops
);
2880 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2882 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2883 &global_trace
, &tracing_lt_fops
);
2885 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2887 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2888 &global_trace
, &tracing_fops
);
2890 pr_warning("Could not create debugfs 'trace' entry\n");
2892 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2893 &global_trace
, &show_traces_fops
);
2895 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2897 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2898 &global_trace
, &set_tracer_fops
);
2900 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2902 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2903 &tracing_max_latency
,
2904 &tracing_max_lat_fops
);
2906 pr_warning("Could not create debugfs "
2907 "'tracing_max_latency' entry\n");
2909 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2910 &tracing_thresh
, &tracing_max_lat_fops
);
2912 pr_warning("Could not create debugfs "
2913 "'tracing_thresh' entry\n");
2914 entry
= debugfs_create_file("README", 0644, d_tracer
,
2915 NULL
, &tracing_readme_fops
);
2917 pr_warning("Could not create debugfs 'README' entry\n");
2919 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2920 NULL
, &tracing_pipe_fops
);
2922 pr_warning("Could not create debugfs "
2923 "'trace_pipe' entry\n");
2925 entry
= debugfs_create_file("trace_entries", 0644, d_tracer
,
2926 &global_trace
, &tracing_entries_fops
);
2928 pr_warning("Could not create debugfs "
2929 "'trace_entries' entry\n");
2931 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
2932 NULL
, &tracing_mark_fops
);
2934 pr_warning("Could not create debugfs "
2935 "'trace_marker' entry\n");
2937 #ifdef CONFIG_DYNAMIC_FTRACE
2938 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2939 &ftrace_update_tot_cnt
,
2940 &tracing_read_long_fops
);
2942 pr_warning("Could not create debugfs "
2943 "'dyn_ftrace_total_info' entry\n");
2945 #ifdef CONFIG_SYSPROF_TRACER
2946 init_tracer_sysprof_debugfs(d_tracer
);
2951 int trace_vprintk(unsigned long ip
, const char *fmt
, va_list args
)
2953 static DEFINE_SPINLOCK(trace_buf_lock
);
2954 static char trace_buf
[TRACE_BUF_SIZE
];
2956 struct ring_buffer_event
*event
;
2957 struct trace_array
*tr
= &global_trace
;
2958 struct trace_array_cpu
*data
;
2959 struct print_entry
*entry
;
2960 unsigned long flags
, irq_flags
;
2961 int cpu
, len
= 0, size
, pc
;
2963 if (!tr
->ctrl
|| tracing_disabled
)
2966 pc
= preempt_count();
2967 preempt_disable_notrace();
2968 cpu
= raw_smp_processor_id();
2969 data
= tr
->data
[cpu
];
2971 if (unlikely(atomic_read(&data
->disabled
)))
2974 spin_lock_irqsave(&trace_buf_lock
, flags
);
2975 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
2977 len
= min(len
, TRACE_BUF_SIZE
-1);
2980 size
= sizeof(*entry
) + len
+ 1;
2981 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
2984 entry
= ring_buffer_event_data(event
);
2985 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
2986 entry
->ent
.type
= TRACE_PRINT
;
2989 memcpy(&entry
->buf
, trace_buf
, len
);
2990 entry
->buf
[len
] = 0;
2991 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
2994 spin_unlock_irqrestore(&trace_buf_lock
, flags
);
2997 preempt_enable_notrace();
3001 EXPORT_SYMBOL_GPL(trace_vprintk
);
3003 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3008 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3012 ret
= trace_vprintk(ip
, fmt
, ap
);
3016 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3018 static int trace_panic_handler(struct notifier_block
*this,
3019 unsigned long event
, void *unused
)
3025 static struct notifier_block trace_panic_notifier
= {
3026 .notifier_call
= trace_panic_handler
,
3028 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3031 static int trace_die_handler(struct notifier_block
*self
,
3045 static struct notifier_block trace_die_notifier
= {
3046 .notifier_call
= trace_die_handler
,
3051 * printk is set to max of 1024, we really don't need it that big.
3052 * Nothing should be printing 1000 characters anyway.
3054 #define TRACE_MAX_PRINT 1000
3057 * Define here KERN_TRACE so that we have one place to modify
3058 * it if we decide to change what log level the ftrace dump
3061 #define KERN_TRACE KERN_INFO
3064 trace_printk_seq(struct trace_seq
*s
)
3066 /* Probably should print a warning here. */
3070 /* should be zero ended, but we are paranoid. */
3071 s
->buffer
[s
->len
] = 0;
3073 printk(KERN_TRACE
"%s", s
->buffer
);
3079 void ftrace_dump(void)
3081 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3082 /* use static because iter can be a bit big for the stack */
3083 static struct trace_iterator iter
;
3084 static cpumask_t mask
;
3085 static int dump_ran
;
3086 unsigned long flags
;
3090 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3096 /* No turning back! */
3097 ftrace_kill_atomic();
3099 for_each_tracing_cpu(cpu
) {
3100 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3103 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3105 iter
.tr
= &global_trace
;
3106 iter
.trace
= current_trace
;
3109 * We need to stop all tracing on all CPUS to read the
3110 * the next buffer. This is a bit expensive, but is
3111 * not done often. We fill all what we can read,
3112 * and then release the locks again.
3117 while (!trace_empty(&iter
)) {
3120 printk(KERN_TRACE
"---------------------------------\n");
3124 /* reset all but tr, trace, and overruns */
3125 memset(&iter
.seq
, 0,
3126 sizeof(struct trace_iterator
) -
3127 offsetof(struct trace_iterator
, seq
));
3128 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3131 if (find_next_entry_inc(&iter
) != NULL
) {
3132 print_trace_line(&iter
);
3133 trace_consume(&iter
);
3136 trace_printk_seq(&iter
.seq
);
3140 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3142 printk(KERN_TRACE
"---------------------------------\n");
3145 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3148 __init
static int tracer_alloc_buffers(void)
3150 struct trace_array_cpu
*data
;
3153 /* TODO: make the number of buffers hot pluggable with CPUS */
3154 tracing_buffer_mask
= cpu_possible_map
;
3156 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3157 TRACE_BUFFER_FLAGS
);
3158 if (!global_trace
.buffer
) {
3159 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3163 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3165 #ifdef CONFIG_TRACER_MAX_TRACE
3166 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3167 TRACE_BUFFER_FLAGS
);
3168 if (!max_tr
.buffer
) {
3169 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3171 ring_buffer_free(global_trace
.buffer
);
3174 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3175 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3178 /* Allocate the first page for all buffers */
3179 for_each_tracing_cpu(i
) {
3180 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3181 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3184 trace_init_cmdlines();
3186 register_tracer(&nop_trace
);
3187 #ifdef CONFIG_BOOT_TRACER
3188 register_tracer(&boot_tracer
);
3189 current_trace
= &boot_tracer
;
3190 current_trace
->init(&global_trace
);
3192 current_trace
= &nop_trace
;
3195 /* All seems OK, enable tracing */
3196 global_trace
.ctrl
= tracer_enabled
;
3197 tracing_disabled
= 0;
3199 atomic_notifier_chain_register(&panic_notifier_list
,
3200 &trace_panic_notifier
);
3202 register_die_notifier(&trace_die_notifier
);
3206 early_initcall(tracer_alloc_buffers
);
3207 fs_initcall(tracer_init_debugfs
);