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>
37 #include <linux/irqflags.h>
41 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
43 unsigned long __read_mostly tracing_max_latency
;
44 unsigned long __read_mostly tracing_thresh
;
47 * We need to change this state when a selftest is running.
48 * A selftest will lurk into the ring-buffer to count the
49 * entries inserted during the selftest although some concurrent
50 * insertions into the ring-buffer such as ftrace_printk could occurred
51 * at the same time, giving false positive or negative results.
53 static bool __read_mostly tracing_selftest_running
;
55 /* For tracers that don't implement custom flags */
56 static struct tracer_opt dummy_tracer_opt
[] = {
60 static struct tracer_flags dummy_tracer_flags
= {
62 .opts
= dummy_tracer_opt
65 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
71 * Kill all tracing for good (never come back).
72 * It is initialized to 1 but will turn to zero if the initialization
73 * of the tracer is successful. But that is the only place that sets
76 int tracing_disabled
= 1;
78 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
80 static inline void ftrace_disable_cpu(void)
83 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
86 static inline void ftrace_enable_cpu(void)
88 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
92 static cpumask_var_t __read_mostly tracing_buffer_mask
;
94 #define for_each_tracing_cpu(cpu) \
95 for_each_cpu(cpu, tracing_buffer_mask)
98 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
100 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
101 * is set, then ftrace_dump is called. This will output the contents
102 * of the ftrace buffers to the console. This is very useful for
103 * capturing traces that lead to crashes and outputing it to a
106 * It is default off, but you can enable it with either specifying
107 * "ftrace_dump_on_oops" in the kernel command line, or setting
108 * /proc/sys/kernel/ftrace_dump_on_oops to true.
110 int ftrace_dump_on_oops
;
112 static int tracing_set_tracer(char *buf
);
114 static int __init
set_ftrace(char *str
)
116 tracing_set_tracer(str
);
119 __setup("ftrace", set_ftrace
);
121 static int __init
set_ftrace_dump_on_oops(char *str
)
123 ftrace_dump_on_oops
= 1;
126 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
129 ns2usecs(cycle_t nsec
)
136 cycle_t
ftrace_now(int cpu
)
138 u64 ts
= ring_buffer_time_stamp(cpu
);
139 ring_buffer_normalize_time_stamp(cpu
, &ts
);
144 * The global_trace is the descriptor that holds the tracing
145 * buffers for the live tracing. For each CPU, it contains
146 * a link list of pages that will store trace entries. The
147 * page descriptor of the pages in the memory is used to hold
148 * the link list by linking the lru item in the page descriptor
149 * to each of the pages in the buffer per CPU.
151 * For each active CPU there is a data field that holds the
152 * pages for the buffer for that CPU. Each CPU has the same number
153 * of pages allocated for its buffer.
155 static struct trace_array global_trace
;
157 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
160 * The max_tr is used to snapshot the global_trace when a maximum
161 * latency is reached. Some tracers will use this to store a maximum
162 * trace while it continues examining live traces.
164 * The buffers for the max_tr are set up the same as the global_trace.
165 * When a snapshot is taken, the link list of the max_tr is swapped
166 * with the link list of the global_trace and the buffers are reset for
167 * the global_trace so the tracing can continue.
169 static struct trace_array max_tr
;
171 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
173 /* tracer_enabled is used to toggle activation of a tracer */
174 static int tracer_enabled
= 1;
177 * tracing_is_enabled - return tracer_enabled status
179 * This function is used by other tracers to know the status
180 * of the tracer_enabled flag. Tracers may use this function
181 * to know if it should enable their features when starting
182 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
184 int tracing_is_enabled(void)
186 return tracer_enabled
;
189 /* function tracing enabled */
190 int ftrace_function_enabled
;
193 * trace_buf_size is the size in bytes that is allocated
194 * for a buffer. Note, the number of bytes is always rounded
197 * This number is purposely set to a low number of 16384.
198 * If the dump on oops happens, it will be much appreciated
199 * to not have to wait for all that output. Anyway this can be
200 * boot time and run time configurable.
202 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
204 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
206 /* trace_types holds a link list of available tracers. */
207 static struct tracer
*trace_types __read_mostly
;
209 /* current_trace points to the tracer that is currently active */
210 static struct tracer
*current_trace __read_mostly
;
213 * max_tracer_type_len is used to simplify the allocating of
214 * buffers to read userspace tracer names. We keep track of
215 * the longest tracer name registered.
217 static int max_tracer_type_len
;
220 * trace_types_lock is used to protect the trace_types list.
221 * This lock is also used to keep user access serialized.
222 * Accesses from userspace will grab this lock while userspace
223 * activities happen inside the kernel.
225 static DEFINE_MUTEX(trace_types_lock
);
227 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
228 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
230 /* trace_flags holds trace_options default values */
231 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
235 * trace_wake_up - wake up tasks waiting for trace input
237 * Simply wakes up any task that is blocked on the trace_wait
238 * queue. These is used with trace_poll for tasks polling the trace.
240 void trace_wake_up(void)
243 * The runqueue_is_locked() can fail, but this is the best we
246 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
247 wake_up(&trace_wait
);
250 static int __init
set_buf_size(char *str
)
252 unsigned long buf_size
;
257 ret
= strict_strtoul(str
, 0, &buf_size
);
258 /* nr_entries can not be zero */
259 if (ret
< 0 || buf_size
== 0)
261 trace_buf_size
= buf_size
;
264 __setup("trace_buf_size=", set_buf_size
);
266 unsigned long nsecs_to_usecs(unsigned long nsecs
)
271 /* These must match the bit postions in trace_iterator_flags */
272 static const char *trace_options
[] = {
294 * ftrace_max_lock is used to protect the swapping of buffers
295 * when taking a max snapshot. The buffers themselves are
296 * protected by per_cpu spinlocks. But the action of the swap
297 * needs its own lock.
299 * This is defined as a raw_spinlock_t in order to help
300 * with performance when lockdep debugging is enabled.
302 static raw_spinlock_t ftrace_max_lock
=
303 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
306 * Copy the new maximum trace into the separate maximum-trace
307 * structure. (this way the maximum trace is permanently saved,
308 * for later retrieval via /debugfs/tracing/latency_trace)
311 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
313 struct trace_array_cpu
*data
= tr
->data
[cpu
];
316 max_tr
.time_start
= data
->preempt_timestamp
;
318 data
= max_tr
.data
[cpu
];
319 data
->saved_latency
= tracing_max_latency
;
321 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
322 data
->pid
= tsk
->pid
;
323 data
->uid
= task_uid(tsk
);
324 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
325 data
->policy
= tsk
->policy
;
326 data
->rt_priority
= tsk
->rt_priority
;
328 /* record this tasks comm */
329 tracing_record_cmdline(current
);
333 * trace_seq_printf - sequence printing of trace information
334 * @s: trace sequence descriptor
335 * @fmt: printf format string
337 * The tracer may use either sequence operations or its own
338 * copy to user routines. To simplify formating of a trace
339 * trace_seq_printf is used to store strings into a special
340 * buffer (@s). Then the output may be either used by
341 * the sequencer or pulled into another buffer.
344 trace_seq_printf(struct trace_seq
*s
, const char *fmt
, ...)
346 int len
= (PAGE_SIZE
- 1) - s
->len
;
354 ret
= vsnprintf(s
->buffer
+ s
->len
, len
, fmt
, ap
);
357 /* If we can't write it all, don't bother writing anything */
367 * trace_seq_puts - trace sequence printing of simple string
368 * @s: trace sequence descriptor
369 * @str: simple string to record
371 * The tracer may use either the sequence operations or its own
372 * copy to user routines. This function records a simple string
373 * into a special buffer (@s) for later retrieval by a sequencer
374 * or other mechanism.
377 trace_seq_puts(struct trace_seq
*s
, const char *str
)
379 int len
= strlen(str
);
381 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
384 memcpy(s
->buffer
+ s
->len
, str
, len
);
391 trace_seq_putc(struct trace_seq
*s
, unsigned char c
)
393 if (s
->len
>= (PAGE_SIZE
- 1))
396 s
->buffer
[s
->len
++] = c
;
402 trace_seq_putmem(struct trace_seq
*s
, void *mem
, size_t len
)
404 if (len
> ((PAGE_SIZE
- 1) - s
->len
))
407 memcpy(s
->buffer
+ s
->len
, mem
, len
);
413 #define MAX_MEMHEX_BYTES 8
414 #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
417 trace_seq_putmem_hex(struct trace_seq
*s
, void *mem
, size_t len
)
419 unsigned char hex
[HEX_CHARS
];
420 unsigned char *data
= mem
;
424 for (i
= 0, j
= 0; i
< len
; i
++) {
426 for (i
= len
-1, j
= 0; i
>= 0; i
--) {
428 hex
[j
++] = hex_asc_hi(data
[i
]);
429 hex
[j
++] = hex_asc_lo(data
[i
]);
433 return trace_seq_putmem(s
, hex
, j
);
437 trace_seq_path(struct trace_seq
*s
, struct path
*path
)
441 if (s
->len
>= (PAGE_SIZE
- 1))
443 p
= d_path(path
, s
->buffer
+ s
->len
, PAGE_SIZE
- s
->len
);
445 p
= mangle_path(s
->buffer
+ s
->len
, p
, "\n");
447 s
->len
= p
- s
->buffer
;
451 s
->buffer
[s
->len
++] = '?';
459 trace_seq_reset(struct trace_seq
*s
)
465 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
470 if (s
->len
<= s
->readpos
)
473 len
= s
->len
- s
->readpos
;
476 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
485 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
487 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
490 seq_puts(m
, s
->buffer
);
496 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
498 * @tsk: the task with the latency
499 * @cpu: The cpu that initiated the trace.
501 * Flip the buffers between the @tr and the max_tr and record information
502 * about which task was the cause of this latency.
505 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
507 struct ring_buffer
*buf
= tr
->buffer
;
509 WARN_ON_ONCE(!irqs_disabled());
510 __raw_spin_lock(&ftrace_max_lock
);
512 tr
->buffer
= max_tr
.buffer
;
515 ftrace_disable_cpu();
516 ring_buffer_reset(tr
->buffer
);
519 __update_max_tr(tr
, tsk
, cpu
);
520 __raw_spin_unlock(&ftrace_max_lock
);
524 * update_max_tr_single - only copy one trace over, and reset the rest
526 * @tsk - task with the latency
527 * @cpu - the cpu of the buffer to copy.
529 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
532 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
536 WARN_ON_ONCE(!irqs_disabled());
537 __raw_spin_lock(&ftrace_max_lock
);
539 ftrace_disable_cpu();
541 ring_buffer_reset(max_tr
.buffer
);
542 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
548 __update_max_tr(tr
, tsk
, cpu
);
549 __raw_spin_unlock(&ftrace_max_lock
);
553 * register_tracer - register a tracer with the ftrace system.
554 * @type - the plugin for the tracer
556 * Register a new plugin tracer.
558 int register_tracer(struct tracer
*type
)
565 pr_info("Tracer must have a name\n");
570 * When this gets called we hold the BKL which means that
571 * preemption is disabled. Various trace selftests however
572 * need to disable and enable preemption for successful tests.
573 * So we drop the BKL here and grab it after the tests again.
576 mutex_lock(&trace_types_lock
);
578 tracing_selftest_running
= true;
580 for (t
= trace_types
; t
; t
= t
->next
) {
581 if (strcmp(type
->name
, t
->name
) == 0) {
583 pr_info("Trace %s already registered\n",
591 type
->set_flag
= &dummy_set_flag
;
593 type
->flags
= &dummy_tracer_flags
;
595 if (!type
->flags
->opts
)
596 type
->flags
->opts
= dummy_tracer_opt
;
598 #ifdef CONFIG_FTRACE_STARTUP_TEST
599 if (type
->selftest
) {
600 struct tracer
*saved_tracer
= current_trace
;
601 struct trace_array
*tr
= &global_trace
;
605 * Run a selftest on this tracer.
606 * Here we reset the trace buffer, and set the current
607 * tracer to be this tracer. The tracer can then run some
608 * internal tracing to verify that everything is in order.
609 * If we fail, we do not register this tracer.
611 for_each_tracing_cpu(i
)
612 tracing_reset(tr
, i
);
614 current_trace
= type
;
615 /* the test is responsible for initializing and enabling */
616 pr_info("Testing tracer %s: ", type
->name
);
617 ret
= type
->selftest(type
, tr
);
618 /* the test is responsible for resetting too */
619 current_trace
= saved_tracer
;
621 printk(KERN_CONT
"FAILED!\n");
624 /* Only reset on passing, to avoid touching corrupted buffers */
625 for_each_tracing_cpu(i
)
626 tracing_reset(tr
, i
);
628 printk(KERN_CONT
"PASSED\n");
632 type
->next
= trace_types
;
634 len
= strlen(type
->name
);
635 if (len
> max_tracer_type_len
)
636 max_tracer_type_len
= len
;
639 tracing_selftest_running
= false;
640 mutex_unlock(&trace_types_lock
);
646 void unregister_tracer(struct tracer
*type
)
651 mutex_lock(&trace_types_lock
);
652 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
656 pr_info("Trace %s not registered\n", type
->name
);
661 if (strlen(type
->name
) != max_tracer_type_len
)
664 max_tracer_type_len
= 0;
665 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
666 len
= strlen((*t
)->name
);
667 if (len
> max_tracer_type_len
)
668 max_tracer_type_len
= len
;
671 mutex_unlock(&trace_types_lock
);
674 void tracing_reset(struct trace_array
*tr
, int cpu
)
676 ftrace_disable_cpu();
677 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
681 void tracing_reset_online_cpus(struct trace_array
*tr
)
685 tr
->time_start
= ftrace_now(tr
->cpu
);
687 for_each_online_cpu(cpu
)
688 tracing_reset(tr
, cpu
);
691 #define SAVED_CMDLINES 128
692 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
693 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
694 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
695 static int cmdline_idx
;
696 static DEFINE_SPINLOCK(trace_cmdline_lock
);
698 /* temporary disable recording */
699 atomic_t trace_record_cmdline_disabled __read_mostly
;
701 static void trace_init_cmdlines(void)
703 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
704 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
708 static int trace_stop_count
;
709 static DEFINE_SPINLOCK(tracing_start_lock
);
712 * ftrace_off_permanent - disable all ftrace code permanently
714 * This should only be called when a serious anomally has
715 * been detected. This will turn off the function tracing,
716 * ring buffers, and other tracing utilites. It takes no
717 * locks and can be called from any context.
719 void ftrace_off_permanent(void)
721 tracing_disabled
= 1;
723 tracing_off_permanent();
727 * tracing_start - quick start of the tracer
729 * If tracing is enabled but was stopped by tracing_stop,
730 * this will start the tracer back up.
732 void tracing_start(void)
734 struct ring_buffer
*buffer
;
737 if (tracing_disabled
)
740 spin_lock_irqsave(&tracing_start_lock
, flags
);
741 if (--trace_stop_count
)
744 if (trace_stop_count
< 0) {
745 /* Someone screwed up their debugging */
747 trace_stop_count
= 0;
752 buffer
= global_trace
.buffer
;
754 ring_buffer_record_enable(buffer
);
756 buffer
= max_tr
.buffer
;
758 ring_buffer_record_enable(buffer
);
762 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
766 * tracing_stop - quick stop of the tracer
768 * Light weight way to stop tracing. Use in conjunction with
771 void tracing_stop(void)
773 struct ring_buffer
*buffer
;
777 spin_lock_irqsave(&tracing_start_lock
, flags
);
778 if (trace_stop_count
++)
781 buffer
= global_trace
.buffer
;
783 ring_buffer_record_disable(buffer
);
785 buffer
= max_tr
.buffer
;
787 ring_buffer_record_disable(buffer
);
790 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
793 void trace_stop_cmdline_recording(void);
795 static void trace_save_cmdline(struct task_struct
*tsk
)
800 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
804 * It's not the end of the world if we don't get
805 * the lock, but we also don't want to spin
806 * nor do we want to disable interrupts,
807 * so if we miss here, then better luck next time.
809 if (!spin_trylock(&trace_cmdline_lock
))
812 idx
= map_pid_to_cmdline
[tsk
->pid
];
813 if (idx
>= SAVED_CMDLINES
) {
814 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
816 map
= map_cmdline_to_pid
[idx
];
817 if (map
<= PID_MAX_DEFAULT
)
818 map_pid_to_cmdline
[map
] = (unsigned)-1;
820 map_pid_to_cmdline
[tsk
->pid
] = idx
;
825 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
827 spin_unlock(&trace_cmdline_lock
);
830 char *trace_find_cmdline(int pid
)
832 char *cmdline
= "<...>";
838 if (pid
> PID_MAX_DEFAULT
)
841 map
= map_pid_to_cmdline
[pid
];
842 if (map
>= SAVED_CMDLINES
)
845 cmdline
= saved_cmdlines
[map
];
851 void tracing_record_cmdline(struct task_struct
*tsk
)
853 if (atomic_read(&trace_record_cmdline_disabled
))
856 trace_save_cmdline(tsk
);
860 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
863 struct task_struct
*tsk
= current
;
865 entry
->preempt_count
= pc
& 0xff;
866 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
867 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
869 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
870 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
872 TRACE_FLAG_IRQS_NOSUPPORT
|
874 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
875 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
876 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
880 trace_function(struct trace_array
*tr
, struct trace_array_cpu
*data
,
881 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
884 struct ring_buffer_event
*event
;
885 struct ftrace_entry
*entry
;
886 unsigned long irq_flags
;
888 /* If we are reading the ring buffer, don't trace */
889 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
892 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
896 entry
= ring_buffer_event_data(event
);
897 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
898 entry
->ent
.type
= TRACE_FN
;
900 entry
->parent_ip
= parent_ip
;
901 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
904 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
905 static void __trace_graph_entry(struct trace_array
*tr
,
906 struct trace_array_cpu
*data
,
907 struct ftrace_graph_ent
*trace
,
911 struct ring_buffer_event
*event
;
912 struct ftrace_graph_ent_entry
*entry
;
913 unsigned long irq_flags
;
915 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
918 event
= ring_buffer_lock_reserve(global_trace
.buffer
, sizeof(*entry
),
922 entry
= ring_buffer_event_data(event
);
923 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
924 entry
->ent
.type
= TRACE_GRAPH_ENT
;
925 entry
->graph_ent
= *trace
;
926 ring_buffer_unlock_commit(global_trace
.buffer
, event
, irq_flags
);
929 static void __trace_graph_return(struct trace_array
*tr
,
930 struct trace_array_cpu
*data
,
931 struct ftrace_graph_ret
*trace
,
935 struct ring_buffer_event
*event
;
936 struct ftrace_graph_ret_entry
*entry
;
937 unsigned long irq_flags
;
939 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
942 event
= ring_buffer_lock_reserve(global_trace
.buffer
, sizeof(*entry
),
946 entry
= ring_buffer_event_data(event
);
947 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
948 entry
->ent
.type
= TRACE_GRAPH_RET
;
950 ring_buffer_unlock_commit(global_trace
.buffer
, event
, irq_flags
);
955 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
956 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
959 if (likely(!atomic_read(&data
->disabled
)))
960 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
963 static void ftrace_trace_stack(struct trace_array
*tr
,
964 struct trace_array_cpu
*data
,
968 #ifdef CONFIG_STACKTRACE
969 struct ring_buffer_event
*event
;
970 struct stack_entry
*entry
;
971 struct stack_trace trace
;
972 unsigned long irq_flags
;
974 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
977 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
981 entry
= ring_buffer_event_data(event
);
982 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
983 entry
->ent
.type
= TRACE_STACK
;
985 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
987 trace
.nr_entries
= 0;
988 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
990 trace
.entries
= entry
->caller
;
992 save_stack_trace(&trace
);
993 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
997 void __trace_stack(struct trace_array
*tr
,
998 struct trace_array_cpu
*data
,
1002 ftrace_trace_stack(tr
, data
, flags
, skip
, preempt_count());
1005 static void ftrace_trace_userstack(struct trace_array
*tr
,
1006 struct trace_array_cpu
*data
,
1007 unsigned long flags
, int pc
)
1009 #ifdef CONFIG_STACKTRACE
1010 struct ring_buffer_event
*event
;
1011 struct userstack_entry
*entry
;
1012 struct stack_trace trace
;
1013 unsigned long irq_flags
;
1015 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
1018 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1022 entry
= ring_buffer_event_data(event
);
1023 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1024 entry
->ent
.type
= TRACE_USER_STACK
;
1026 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
1028 trace
.nr_entries
= 0;
1029 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
1031 trace
.entries
= entry
->caller
;
1033 save_stack_trace_user(&trace
);
1034 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1038 void __trace_userstack(struct trace_array
*tr
,
1039 struct trace_array_cpu
*data
,
1040 unsigned long flags
)
1042 ftrace_trace_userstack(tr
, data
, flags
, preempt_count());
1046 ftrace_trace_special(void *__tr
, void *__data
,
1047 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
1050 struct ring_buffer_event
*event
;
1051 struct trace_array_cpu
*data
= __data
;
1052 struct trace_array
*tr
= __tr
;
1053 struct special_entry
*entry
;
1054 unsigned long irq_flags
;
1056 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1060 entry
= ring_buffer_event_data(event
);
1061 tracing_generic_entry_update(&entry
->ent
, 0, pc
);
1062 entry
->ent
.type
= TRACE_SPECIAL
;
1066 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1067 ftrace_trace_stack(tr
, data
, irq_flags
, 4, pc
);
1068 ftrace_trace_userstack(tr
, data
, irq_flags
, pc
);
1074 __trace_special(void *__tr
, void *__data
,
1075 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1077 ftrace_trace_special(__tr
, __data
, arg1
, arg2
, arg3
, preempt_count());
1081 tracing_sched_switch_trace(struct trace_array
*tr
,
1082 struct trace_array_cpu
*data
,
1083 struct task_struct
*prev
,
1084 struct task_struct
*next
,
1085 unsigned long flags
, int pc
)
1087 struct ring_buffer_event
*event
;
1088 struct ctx_switch_entry
*entry
;
1089 unsigned long irq_flags
;
1091 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1095 entry
= ring_buffer_event_data(event
);
1096 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1097 entry
->ent
.type
= TRACE_CTX
;
1098 entry
->prev_pid
= prev
->pid
;
1099 entry
->prev_prio
= prev
->prio
;
1100 entry
->prev_state
= prev
->state
;
1101 entry
->next_pid
= next
->pid
;
1102 entry
->next_prio
= next
->prio
;
1103 entry
->next_state
= next
->state
;
1104 entry
->next_cpu
= task_cpu(next
);
1105 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1106 ftrace_trace_stack(tr
, data
, flags
, 5, pc
);
1107 ftrace_trace_userstack(tr
, data
, flags
, pc
);
1111 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1112 struct trace_array_cpu
*data
,
1113 struct task_struct
*wakee
,
1114 struct task_struct
*curr
,
1115 unsigned long flags
, int pc
)
1117 struct ring_buffer_event
*event
;
1118 struct ctx_switch_entry
*entry
;
1119 unsigned long irq_flags
;
1121 event
= ring_buffer_lock_reserve(tr
->buffer
, sizeof(*entry
),
1125 entry
= ring_buffer_event_data(event
);
1126 tracing_generic_entry_update(&entry
->ent
, flags
, pc
);
1127 entry
->ent
.type
= TRACE_WAKE
;
1128 entry
->prev_pid
= curr
->pid
;
1129 entry
->prev_prio
= curr
->prio
;
1130 entry
->prev_state
= curr
->state
;
1131 entry
->next_pid
= wakee
->pid
;
1132 entry
->next_prio
= wakee
->prio
;
1133 entry
->next_state
= wakee
->state
;
1134 entry
->next_cpu
= task_cpu(wakee
);
1135 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
1136 ftrace_trace_stack(tr
, data
, flags
, 6, pc
);
1137 ftrace_trace_userstack(tr
, data
, flags
, pc
);
1143 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1145 struct trace_array
*tr
= &global_trace
;
1146 struct trace_array_cpu
*data
;
1147 unsigned long flags
;
1151 if (tracing_disabled
)
1154 pc
= preempt_count();
1155 local_irq_save(flags
);
1156 cpu
= raw_smp_processor_id();
1157 data
= tr
->data
[cpu
];
1159 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1160 ftrace_trace_special(tr
, data
, arg1
, arg2
, arg3
, pc
);
1162 atomic_dec(&data
->disabled
);
1163 local_irq_restore(flags
);
1166 #ifdef CONFIG_FUNCTION_TRACER
1168 function_trace_call_preempt_only(unsigned long ip
, unsigned long parent_ip
)
1170 struct trace_array
*tr
= &global_trace
;
1171 struct trace_array_cpu
*data
;
1172 unsigned long flags
;
1177 if (unlikely(!ftrace_function_enabled
))
1180 pc
= preempt_count();
1181 resched
= ftrace_preempt_disable();
1182 local_save_flags(flags
);
1183 cpu
= raw_smp_processor_id();
1184 data
= tr
->data
[cpu
];
1185 disabled
= atomic_inc_return(&data
->disabled
);
1187 if (likely(disabled
== 1))
1188 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1190 atomic_dec(&data
->disabled
);
1191 ftrace_preempt_enable(resched
);
1195 function_trace_call(unsigned long ip
, unsigned long parent_ip
)
1197 struct trace_array
*tr
= &global_trace
;
1198 struct trace_array_cpu
*data
;
1199 unsigned long flags
;
1204 if (unlikely(!ftrace_function_enabled
))
1208 * Need to use raw, since this must be called before the
1209 * recursive protection is performed.
1211 local_irq_save(flags
);
1212 cpu
= raw_smp_processor_id();
1213 data
= tr
->data
[cpu
];
1214 disabled
= atomic_inc_return(&data
->disabled
);
1216 if (likely(disabled
== 1)) {
1217 pc
= preempt_count();
1218 trace_function(tr
, data
, ip
, parent_ip
, flags
, pc
);
1221 atomic_dec(&data
->disabled
);
1222 local_irq_restore(flags
);
1225 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1226 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1228 struct trace_array
*tr
= &global_trace
;
1229 struct trace_array_cpu
*data
;
1230 unsigned long flags
;
1235 if (!ftrace_trace_task(current
))
1238 if (!ftrace_graph_addr(trace
->func
))
1241 local_irq_save(flags
);
1242 cpu
= raw_smp_processor_id();
1243 data
= tr
->data
[cpu
];
1244 disabled
= atomic_inc_return(&data
->disabled
);
1245 if (likely(disabled
== 1)) {
1246 pc
= preempt_count();
1247 __trace_graph_entry(tr
, data
, trace
, flags
, pc
);
1249 /* Only do the atomic if it is not already set */
1250 if (!test_tsk_trace_graph(current
))
1251 set_tsk_trace_graph(current
);
1252 atomic_dec(&data
->disabled
);
1253 local_irq_restore(flags
);
1258 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1260 struct trace_array
*tr
= &global_trace
;
1261 struct trace_array_cpu
*data
;
1262 unsigned long flags
;
1267 local_irq_save(flags
);
1268 cpu
= raw_smp_processor_id();
1269 data
= tr
->data
[cpu
];
1270 disabled
= atomic_inc_return(&data
->disabled
);
1271 if (likely(disabled
== 1)) {
1272 pc
= preempt_count();
1273 __trace_graph_return(tr
, data
, trace
, flags
, pc
);
1276 clear_tsk_trace_graph(current
);
1277 atomic_dec(&data
->disabled
);
1278 local_irq_restore(flags
);
1280 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1282 static struct ftrace_ops trace_ops __read_mostly
=
1284 .func
= function_trace_call
,
1287 void tracing_start_function_trace(void)
1289 ftrace_function_enabled
= 0;
1291 if (trace_flags
& TRACE_ITER_PREEMPTONLY
)
1292 trace_ops
.func
= function_trace_call_preempt_only
;
1294 trace_ops
.func
= function_trace_call
;
1296 register_ftrace_function(&trace_ops
);
1297 ftrace_function_enabled
= 1;
1300 void tracing_stop_function_trace(void)
1302 ftrace_function_enabled
= 0;
1303 unregister_ftrace_function(&trace_ops
);
1307 enum trace_file_type
{
1308 TRACE_FILE_LAT_FMT
= 1,
1309 TRACE_FILE_ANNOTATE
= 2,
1312 static void trace_iterator_increment(struct trace_iterator
*iter
)
1314 /* Don't allow ftrace to trace into the ring buffers */
1315 ftrace_disable_cpu();
1318 if (iter
->buffer_iter
[iter
->cpu
])
1319 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1321 ftrace_enable_cpu();
1324 static struct trace_entry
*
1325 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1327 struct ring_buffer_event
*event
;
1328 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1330 /* Don't allow ftrace to trace into the ring buffers */
1331 ftrace_disable_cpu();
1334 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1336 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1338 ftrace_enable_cpu();
1340 return event
? ring_buffer_event_data(event
) : NULL
;
1343 static struct trace_entry
*
1344 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1346 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1347 struct trace_entry
*ent
, *next
= NULL
;
1348 u64 next_ts
= 0, ts
;
1352 for_each_tracing_cpu(cpu
) {
1354 if (ring_buffer_empty_cpu(buffer
, cpu
))
1357 ent
= peek_next_entry(iter
, cpu
, &ts
);
1360 * Pick the entry with the smallest timestamp:
1362 if (ent
&& (!next
|| ts
< next_ts
)) {
1370 *ent_cpu
= next_cpu
;
1378 /* Find the next real entry, without updating the iterator itself */
1379 static struct trace_entry
*
1380 find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1382 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1385 /* Find the next real entry, and increment the iterator to the next entry */
1386 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1388 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1391 trace_iterator_increment(iter
);
1393 return iter
->ent
? iter
: NULL
;
1396 static void trace_consume(struct trace_iterator
*iter
)
1398 /* Don't allow ftrace to trace into the ring buffers */
1399 ftrace_disable_cpu();
1400 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1401 ftrace_enable_cpu();
1404 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1406 struct trace_iterator
*iter
= m
->private;
1412 /* can't go backwards */
1417 ent
= find_next_entry_inc(iter
);
1421 while (ent
&& iter
->idx
< i
)
1422 ent
= find_next_entry_inc(iter
);
1429 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1431 struct trace_iterator
*iter
= m
->private;
1436 mutex_lock(&trace_types_lock
);
1438 if (!current_trace
|| current_trace
!= iter
->trace
) {
1439 mutex_unlock(&trace_types_lock
);
1443 atomic_inc(&trace_record_cmdline_disabled
);
1445 if (*pos
!= iter
->pos
) {
1450 ftrace_disable_cpu();
1452 for_each_tracing_cpu(cpu
) {
1453 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1456 ftrace_enable_cpu();
1458 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1463 p
= s_next(m
, p
, &l
);
1469 static void s_stop(struct seq_file
*m
, void *p
)
1471 atomic_dec(&trace_record_cmdline_disabled
);
1472 mutex_unlock(&trace_types_lock
);
1475 #ifdef CONFIG_KRETPROBES
1476 static inline const char *kretprobed(const char *name
)
1478 static const char tramp_name
[] = "kretprobe_trampoline";
1479 int size
= sizeof(tramp_name
);
1481 if (strncmp(tramp_name
, name
, size
) == 0)
1482 return "[unknown/kretprobe'd]";
1486 static inline const char *kretprobed(const char *name
)
1490 #endif /* CONFIG_KRETPROBES */
1493 seq_print_sym_short(struct trace_seq
*s
, const char *fmt
, unsigned long address
)
1495 #ifdef CONFIG_KALLSYMS
1496 char str
[KSYM_SYMBOL_LEN
];
1499 kallsyms_lookup(address
, NULL
, NULL
, NULL
, str
);
1501 name
= kretprobed(str
);
1503 return trace_seq_printf(s
, fmt
, name
);
1509 seq_print_sym_offset(struct trace_seq
*s
, const char *fmt
,
1510 unsigned long address
)
1512 #ifdef CONFIG_KALLSYMS
1513 char str
[KSYM_SYMBOL_LEN
];
1516 sprint_symbol(str
, address
);
1517 name
= kretprobed(str
);
1519 return trace_seq_printf(s
, fmt
, name
);
1524 #ifndef CONFIG_64BIT
1525 # define IP_FMT "%08lx"
1527 # define IP_FMT "%016lx"
1531 seq_print_ip_sym(struct trace_seq
*s
, unsigned long ip
, unsigned long sym_flags
)
1536 return trace_seq_printf(s
, "0");
1538 if (sym_flags
& TRACE_ITER_SYM_OFFSET
)
1539 ret
= seq_print_sym_offset(s
, "%s", ip
);
1541 ret
= seq_print_sym_short(s
, "%s", ip
);
1546 if (sym_flags
& TRACE_ITER_SYM_ADDR
)
1547 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1551 static inline int seq_print_user_ip(struct trace_seq
*s
, struct mm_struct
*mm
,
1552 unsigned long ip
, unsigned long sym_flags
)
1554 struct file
*file
= NULL
;
1555 unsigned long vmstart
= 0;
1559 const struct vm_area_struct
*vma
;
1561 down_read(&mm
->mmap_sem
);
1562 vma
= find_vma(mm
, ip
);
1564 file
= vma
->vm_file
;
1565 vmstart
= vma
->vm_start
;
1568 ret
= trace_seq_path(s
, &file
->f_path
);
1570 ret
= trace_seq_printf(s
, "[+0x%lx]", ip
- vmstart
);
1572 up_read(&mm
->mmap_sem
);
1574 if (ret
&& ((sym_flags
& TRACE_ITER_SYM_ADDR
) || !file
))
1575 ret
= trace_seq_printf(s
, " <" IP_FMT
">", ip
);
1580 seq_print_userip_objs(const struct userstack_entry
*entry
, struct trace_seq
*s
,
1581 unsigned long sym_flags
)
1583 struct mm_struct
*mm
= NULL
;
1587 if (trace_flags
& TRACE_ITER_SYM_USEROBJ
) {
1588 struct task_struct
*task
;
1590 * we do the lookup on the thread group leader,
1591 * since individual threads might have already quit!
1594 task
= find_task_by_vpid(entry
->ent
.tgid
);
1596 mm
= get_task_mm(task
);
1600 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1601 unsigned long ip
= entry
->caller
[i
];
1603 if (ip
== ULONG_MAX
|| !ret
)
1606 ret
= trace_seq_puts(s
, " <- ");
1609 ret
= trace_seq_puts(s
, "??");
1615 ret
= seq_print_user_ip(s
, mm
, ip
, sym_flags
);
1623 static void print_lat_help_header(struct seq_file
*m
)
1625 seq_puts(m
, "# _------=> CPU# \n");
1626 seq_puts(m
, "# / _-----=> irqs-off \n");
1627 seq_puts(m
, "# | / _----=> need-resched \n");
1628 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1629 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1630 seq_puts(m
, "# |||| / \n");
1631 seq_puts(m
, "# ||||| delay \n");
1632 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1633 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1636 static void print_func_help_header(struct seq_file
*m
)
1638 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1639 seq_puts(m
, "# | | | | |\n");
1644 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1646 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1647 struct trace_array
*tr
= iter
->tr
;
1648 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1649 struct tracer
*type
= current_trace
;
1650 unsigned long total
;
1651 unsigned long entries
;
1652 const char *name
= "preemption";
1657 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1659 ring_buffer_overruns(iter
->tr
->buffer
);
1661 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1663 seq_puts(m
, "-----------------------------------"
1664 "---------------------------------\n");
1665 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1666 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1667 nsecs_to_usecs(data
->saved_latency
),
1671 #if defined(CONFIG_PREEMPT_NONE)
1673 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1675 #elif defined(CONFIG_PREEMPT)
1680 /* These are reserved for later use */
1683 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1687 seq_puts(m
, " -----------------\n");
1688 seq_printf(m
, " | task: %.16s-%d "
1689 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1690 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1691 data
->policy
, data
->rt_priority
);
1692 seq_puts(m
, " -----------------\n");
1694 if (data
->critical_start
) {
1695 seq_puts(m
, " => started at: ");
1696 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1697 trace_print_seq(m
, &iter
->seq
);
1698 seq_puts(m
, "\n => ended at: ");
1699 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1700 trace_print_seq(m
, &iter
->seq
);
1708 lat_print_generic(struct trace_seq
*s
, struct trace_entry
*entry
, int cpu
)
1710 int hardirq
, softirq
;
1713 comm
= trace_find_cmdline(entry
->pid
);
1715 trace_seq_printf(s
, "%8.8s-%-5d ", comm
, entry
->pid
);
1716 trace_seq_printf(s
, "%3d", cpu
);
1717 trace_seq_printf(s
, "%c%c",
1718 (entry
->flags
& TRACE_FLAG_IRQS_OFF
) ? 'd' :
1719 (entry
->flags
& TRACE_FLAG_IRQS_NOSUPPORT
) ? 'X' : '.',
1720 ((entry
->flags
& TRACE_FLAG_NEED_RESCHED
) ? 'N' : '.'));
1722 hardirq
= entry
->flags
& TRACE_FLAG_HARDIRQ
;
1723 softirq
= entry
->flags
& TRACE_FLAG_SOFTIRQ
;
1724 if (hardirq
&& softirq
) {
1725 trace_seq_putc(s
, 'H');
1728 trace_seq_putc(s
, 'h');
1731 trace_seq_putc(s
, 's');
1733 trace_seq_putc(s
, '.');
1737 if (entry
->preempt_count
)
1738 trace_seq_printf(s
, "%x", entry
->preempt_count
);
1740 trace_seq_puts(s
, ".");
1743 unsigned long preempt_mark_thresh
= 100;
1746 lat_print_timestamp(struct trace_seq
*s
, u64 abs_usecs
,
1747 unsigned long rel_usecs
)
1749 trace_seq_printf(s
, " %4lldus", abs_usecs
);
1750 if (rel_usecs
> preempt_mark_thresh
)
1751 trace_seq_puts(s
, "!: ");
1752 else if (rel_usecs
> 1)
1753 trace_seq_puts(s
, "+: ");
1755 trace_seq_puts(s
, " : ");
1758 static const char state_to_char
[] = TASK_STATE_TO_CHAR_STR
;
1760 static int task_state_char(unsigned long state
)
1762 int bit
= state
? __ffs(state
) + 1 : 0;
1764 return bit
< sizeof(state_to_char
) - 1 ? state_to_char
[bit
] : '?';
1768 * The message is supposed to contain an ending newline.
1769 * If the printing stops prematurely, try to add a newline of our own.
1771 void trace_seq_print_cont(struct trace_seq
*s
, struct trace_iterator
*iter
)
1773 struct trace_entry
*ent
;
1774 struct trace_field_cont
*cont
;
1777 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1778 if (!ent
|| ent
->type
!= TRACE_CONT
) {
1779 trace_seq_putc(s
, '\n');
1784 cont
= (struct trace_field_cont
*)ent
;
1786 ok
= (trace_seq_printf(s
, "%s", cont
->buf
) > 0);
1788 ftrace_disable_cpu();
1790 if (iter
->buffer_iter
[iter
->cpu
])
1791 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1793 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, NULL
);
1795 ftrace_enable_cpu();
1797 ent
= peek_next_entry(iter
, iter
->cpu
, NULL
);
1798 } while (ent
&& ent
->type
== TRACE_CONT
);
1801 trace_seq_putc(s
, '\n');
1804 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1806 struct trace_seq
*s
= &iter
->seq
;
1808 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1811 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1814 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1817 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1818 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1821 static enum print_line_t
1822 print_lat_fmt(struct trace_iterator
*iter
, unsigned int trace_idx
, int cpu
)
1824 struct trace_seq
*s
= &iter
->seq
;
1825 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1826 struct trace_entry
*next_entry
;
1827 unsigned long verbose
= (trace_flags
& TRACE_ITER_VERBOSE
);
1828 struct trace_entry
*entry
= iter
->ent
;
1829 unsigned long abs_usecs
;
1830 unsigned long rel_usecs
;
1836 if (entry
->type
== TRACE_CONT
)
1837 return TRACE_TYPE_HANDLED
;
1839 test_cpu_buff_start(iter
);
1841 next_entry
= find_next_entry(iter
, NULL
, &next_ts
);
1844 rel_usecs
= ns2usecs(next_ts
- iter
->ts
);
1845 abs_usecs
= ns2usecs(iter
->ts
- iter
->tr
->time_start
);
1848 comm
= trace_find_cmdline(entry
->pid
);
1849 trace_seq_printf(s
, "%16s %5d %3d %d %08x %08x [%08lx]"
1850 " %ld.%03ldms (+%ld.%03ldms): ",
1852 entry
->pid
, cpu
, entry
->flags
,
1853 entry
->preempt_count
, trace_idx
,
1856 abs_usecs
% 1000, rel_usecs
/1000,
1859 lat_print_generic(s
, entry
, cpu
);
1860 lat_print_timestamp(s
, abs_usecs
, rel_usecs
);
1862 switch (entry
->type
) {
1864 struct ftrace_entry
*field
;
1866 trace_assign_type(field
, entry
);
1868 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1869 trace_seq_puts(s
, " (");
1870 seq_print_ip_sym(s
, field
->parent_ip
, sym_flags
);
1871 trace_seq_puts(s
, ")\n");
1876 struct ctx_switch_entry
*field
;
1878 trace_assign_type(field
, entry
);
1880 T
= task_state_char(field
->next_state
);
1881 S
= task_state_char(field
->prev_state
);
1882 comm
= trace_find_cmdline(field
->next_pid
);
1883 trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c %s\n",
1886 S
, entry
->type
== TRACE_CTX
? "==>" : " +",
1893 case TRACE_SPECIAL
: {
1894 struct special_entry
*field
;
1896 trace_assign_type(field
, entry
);
1898 trace_seq_printf(s
, "# %ld %ld %ld\n",
1905 struct stack_entry
*field
;
1907 trace_assign_type(field
, entry
);
1909 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
1911 trace_seq_puts(s
, " <= ");
1912 seq_print_ip_sym(s
, field
->caller
[i
], sym_flags
);
1914 trace_seq_puts(s
, "\n");
1918 struct print_entry
*field
;
1920 trace_assign_type(field
, entry
);
1922 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1923 trace_seq_printf(s
, ": %s", field
->buf
);
1924 if (entry
->flags
& TRACE_FLAG_CONT
)
1925 trace_seq_print_cont(s
, iter
);
1928 case TRACE_BRANCH
: {
1929 struct trace_branch
*field
;
1931 trace_assign_type(field
, entry
);
1933 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
1934 field
->correct
? " ok " : " MISS ",
1940 case TRACE_USER_STACK
: {
1941 struct userstack_entry
*field
;
1943 trace_assign_type(field
, entry
);
1945 seq_print_userip_objs(field
, s
, sym_flags
);
1946 trace_seq_putc(s
, '\n');
1950 trace_seq_printf(s
, "Unknown type %d\n", entry
->type
);
1952 return TRACE_TYPE_HANDLED
;
1955 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1957 struct trace_seq
*s
= &iter
->seq
;
1958 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1959 struct trace_entry
*entry
;
1960 unsigned long usec_rem
;
1961 unsigned long long t
;
1970 if (entry
->type
== TRACE_CONT
)
1971 return TRACE_TYPE_HANDLED
;
1973 test_cpu_buff_start(iter
);
1975 comm
= trace_find_cmdline(iter
->ent
->pid
);
1977 t
= ns2usecs(iter
->ts
);
1978 usec_rem
= do_div(t
, 1000000ULL);
1979 secs
= (unsigned long)t
;
1981 ret
= trace_seq_printf(s
, "%16s-%-5d ", comm
, entry
->pid
);
1983 return TRACE_TYPE_PARTIAL_LINE
;
1984 ret
= trace_seq_printf(s
, "[%03d] ", iter
->cpu
);
1986 return TRACE_TYPE_PARTIAL_LINE
;
1987 ret
= trace_seq_printf(s
, "%5lu.%06lu: ", secs
, usec_rem
);
1989 return TRACE_TYPE_PARTIAL_LINE
;
1991 switch (entry
->type
) {
1993 struct ftrace_entry
*field
;
1995 trace_assign_type(field
, entry
);
1997 ret
= seq_print_ip_sym(s
, field
->ip
, sym_flags
);
1999 return TRACE_TYPE_PARTIAL_LINE
;
2000 if ((sym_flags
& TRACE_ITER_PRINT_PARENT
) &&
2002 ret
= trace_seq_printf(s
, " <-");
2004 return TRACE_TYPE_PARTIAL_LINE
;
2005 ret
= seq_print_ip_sym(s
,
2009 return TRACE_TYPE_PARTIAL_LINE
;
2011 ret
= trace_seq_printf(s
, "\n");
2013 return TRACE_TYPE_PARTIAL_LINE
;
2018 struct ctx_switch_entry
*field
;
2020 trace_assign_type(field
, entry
);
2022 T
= task_state_char(field
->next_state
);
2023 S
= task_state_char(field
->prev_state
);
2024 ret
= trace_seq_printf(s
, " %5d:%3d:%c %s [%03d] %5d:%3d:%c\n",
2028 entry
->type
== TRACE_CTX
? "==>" : " +",
2034 return TRACE_TYPE_PARTIAL_LINE
;
2037 case TRACE_SPECIAL
: {
2038 struct special_entry
*field
;
2040 trace_assign_type(field
, entry
);
2042 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
2047 return TRACE_TYPE_PARTIAL_LINE
;
2051 struct stack_entry
*field
;
2053 trace_assign_type(field
, entry
);
2055 for (i
= 0; i
< FTRACE_STACK_ENTRIES
; i
++) {
2057 ret
= trace_seq_puts(s
, " <= ");
2059 return TRACE_TYPE_PARTIAL_LINE
;
2061 ret
= seq_print_ip_sym(s
, field
->caller
[i
],
2064 return TRACE_TYPE_PARTIAL_LINE
;
2066 ret
= trace_seq_puts(s
, "\n");
2068 return TRACE_TYPE_PARTIAL_LINE
;
2072 struct print_entry
*field
;
2074 trace_assign_type(field
, entry
);
2076 seq_print_ip_sym(s
, field
->ip
, sym_flags
);
2077 trace_seq_printf(s
, ": %s", field
->buf
);
2078 if (entry
->flags
& TRACE_FLAG_CONT
)
2079 trace_seq_print_cont(s
, iter
);
2082 case TRACE_GRAPH_RET
: {
2083 return print_graph_function(iter
);
2085 case TRACE_GRAPH_ENT
: {
2086 return print_graph_function(iter
);
2088 case TRACE_BRANCH
: {
2089 struct trace_branch
*field
;
2091 trace_assign_type(field
, entry
);
2093 trace_seq_printf(s
, "[%s] %s:%s:%d\n",
2094 field
->correct
? " ok " : " MISS ",
2100 case TRACE_USER_STACK
: {
2101 struct userstack_entry
*field
;
2103 trace_assign_type(field
, entry
);
2105 ret
= seq_print_userip_objs(field
, s
, sym_flags
);
2107 return TRACE_TYPE_PARTIAL_LINE
;
2108 ret
= trace_seq_putc(s
, '\n');
2110 return TRACE_TYPE_PARTIAL_LINE
;
2114 return TRACE_TYPE_HANDLED
;
2117 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
2119 struct trace_seq
*s
= &iter
->seq
;
2120 struct trace_entry
*entry
;
2126 if (entry
->type
== TRACE_CONT
)
2127 return TRACE_TYPE_HANDLED
;
2129 ret
= trace_seq_printf(s
, "%d %d %llu ",
2130 entry
->pid
, iter
->cpu
, iter
->ts
);
2132 return TRACE_TYPE_PARTIAL_LINE
;
2134 switch (entry
->type
) {
2136 struct ftrace_entry
*field
;
2138 trace_assign_type(field
, entry
);
2140 ret
= trace_seq_printf(s
, "%x %x\n",
2144 return TRACE_TYPE_PARTIAL_LINE
;
2149 struct ctx_switch_entry
*field
;
2151 trace_assign_type(field
, entry
);
2153 T
= task_state_char(field
->next_state
);
2154 S
= entry
->type
== TRACE_WAKE
? '+' :
2155 task_state_char(field
->prev_state
);
2156 ret
= trace_seq_printf(s
, "%d %d %c %d %d %d %c\n",
2165 return TRACE_TYPE_PARTIAL_LINE
;
2169 case TRACE_USER_STACK
:
2171 struct special_entry
*field
;
2173 trace_assign_type(field
, entry
);
2175 ret
= trace_seq_printf(s
, "# %ld %ld %ld\n",
2180 return TRACE_TYPE_PARTIAL_LINE
;
2184 struct print_entry
*field
;
2186 trace_assign_type(field
, entry
);
2188 trace_seq_printf(s
, "# %lx %s", field
->ip
, field
->buf
);
2189 if (entry
->flags
& TRACE_FLAG_CONT
)
2190 trace_seq_print_cont(s
, iter
);
2194 return TRACE_TYPE_HANDLED
;
2197 #define SEQ_PUT_FIELD_RET(s, x) \
2199 if (!trace_seq_putmem(s, &(x), sizeof(x))) \
2203 #define SEQ_PUT_HEX_FIELD_RET(s, x) \
2205 BUILD_BUG_ON(sizeof(x) > MAX_MEMHEX_BYTES); \
2206 if (!trace_seq_putmem_hex(s, &(x), sizeof(x))) \
2210 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
2212 struct trace_seq
*s
= &iter
->seq
;
2213 unsigned char newline
= '\n';
2214 struct trace_entry
*entry
;
2219 if (entry
->type
== TRACE_CONT
)
2220 return TRACE_TYPE_HANDLED
;
2222 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
2223 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
2224 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
2226 switch (entry
->type
) {
2228 struct ftrace_entry
*field
;
2230 trace_assign_type(field
, entry
);
2232 SEQ_PUT_HEX_FIELD_RET(s
, field
->ip
);
2233 SEQ_PUT_HEX_FIELD_RET(s
, field
->parent_ip
);
2238 struct ctx_switch_entry
*field
;
2240 trace_assign_type(field
, entry
);
2242 T
= task_state_char(field
->next_state
);
2243 S
= entry
->type
== TRACE_WAKE
? '+' :
2244 task_state_char(field
->prev_state
);
2245 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_pid
);
2246 SEQ_PUT_HEX_FIELD_RET(s
, field
->prev_prio
);
2247 SEQ_PUT_HEX_FIELD_RET(s
, S
);
2248 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_cpu
);
2249 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_pid
);
2250 SEQ_PUT_HEX_FIELD_RET(s
, field
->next_prio
);
2251 SEQ_PUT_HEX_FIELD_RET(s
, T
);
2255 case TRACE_USER_STACK
:
2257 struct special_entry
*field
;
2259 trace_assign_type(field
, entry
);
2261 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg1
);
2262 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg2
);
2263 SEQ_PUT_HEX_FIELD_RET(s
, field
->arg3
);
2267 SEQ_PUT_FIELD_RET(s
, newline
);
2269 return TRACE_TYPE_HANDLED
;
2272 static enum print_line_t
print_printk_msg_only(struct trace_iterator
*iter
)
2274 struct trace_seq
*s
= &iter
->seq
;
2275 struct trace_entry
*entry
= iter
->ent
;
2276 struct print_entry
*field
;
2279 trace_assign_type(field
, entry
);
2281 ret
= trace_seq_printf(s
, field
->buf
);
2283 return TRACE_TYPE_PARTIAL_LINE
;
2285 if (entry
->flags
& TRACE_FLAG_CONT
)
2286 trace_seq_print_cont(s
, iter
);
2288 return TRACE_TYPE_HANDLED
;
2291 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
2293 struct trace_seq
*s
= &iter
->seq
;
2294 struct trace_entry
*entry
;
2298 if (entry
->type
== TRACE_CONT
)
2299 return TRACE_TYPE_HANDLED
;
2301 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
2302 SEQ_PUT_FIELD_RET(s
, entry
->cpu
);
2303 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
2305 switch (entry
->type
) {
2307 struct ftrace_entry
*field
;
2309 trace_assign_type(field
, entry
);
2311 SEQ_PUT_FIELD_RET(s
, field
->ip
);
2312 SEQ_PUT_FIELD_RET(s
, field
->parent_ip
);
2316 struct ctx_switch_entry
*field
;
2318 trace_assign_type(field
, entry
);
2320 SEQ_PUT_FIELD_RET(s
, field
->prev_pid
);
2321 SEQ_PUT_FIELD_RET(s
, field
->prev_prio
);
2322 SEQ_PUT_FIELD_RET(s
, field
->prev_state
);
2323 SEQ_PUT_FIELD_RET(s
, field
->next_pid
);
2324 SEQ_PUT_FIELD_RET(s
, field
->next_prio
);
2325 SEQ_PUT_FIELD_RET(s
, field
->next_state
);
2329 case TRACE_USER_STACK
:
2331 struct special_entry
*field
;
2333 trace_assign_type(field
, entry
);
2335 SEQ_PUT_FIELD_RET(s
, field
->arg1
);
2336 SEQ_PUT_FIELD_RET(s
, field
->arg2
);
2337 SEQ_PUT_FIELD_RET(s
, field
->arg3
);
2344 static int trace_empty(struct trace_iterator
*iter
)
2348 for_each_tracing_cpu(cpu
) {
2349 if (iter
->buffer_iter
[cpu
]) {
2350 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
2353 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
2361 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
2363 enum print_line_t ret
;
2365 if (iter
->trace
&& iter
->trace
->print_line
) {
2366 ret
= iter
->trace
->print_line(iter
);
2367 if (ret
!= TRACE_TYPE_UNHANDLED
)
2371 if (iter
->ent
->type
== TRACE_PRINT
&&
2372 trace_flags
& TRACE_ITER_PRINTK
&&
2373 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
2374 return print_printk_msg_only(iter
);
2376 if (trace_flags
& TRACE_ITER_BIN
)
2377 return print_bin_fmt(iter
);
2379 if (trace_flags
& TRACE_ITER_HEX
)
2380 return print_hex_fmt(iter
);
2382 if (trace_flags
& TRACE_ITER_RAW
)
2383 return print_raw_fmt(iter
);
2385 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
2386 return print_lat_fmt(iter
, iter
->idx
, iter
->cpu
);
2388 return print_trace_fmt(iter
);
2391 static int s_show(struct seq_file
*m
, void *v
)
2393 struct trace_iterator
*iter
= v
;
2395 if (iter
->ent
== NULL
) {
2397 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
2400 if (iter
->trace
&& iter
->trace
->print_header
)
2401 iter
->trace
->print_header(m
);
2402 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
2403 /* print nothing if the buffers are empty */
2404 if (trace_empty(iter
))
2406 print_trace_header(m
, iter
);
2407 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2408 print_lat_help_header(m
);
2410 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
2411 print_func_help_header(m
);
2414 print_trace_line(iter
);
2415 trace_print_seq(m
, &iter
->seq
);
2421 static struct seq_operations tracer_seq_ops
= {
2428 static struct trace_iterator
*
2429 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
2431 struct trace_iterator
*iter
;
2435 if (tracing_disabled
) {
2440 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2446 mutex_lock(&trace_types_lock
);
2447 if (current_trace
&& current_trace
->print_max
)
2450 iter
->tr
= inode
->i_private
;
2451 iter
->trace
= current_trace
;
2454 /* Notify the tracer early; before we stop tracing. */
2455 if (iter
->trace
&& iter
->trace
->open
)
2456 iter
->trace
->open(iter
);
2458 /* Annotate start of buffers if we had overruns */
2459 if (ring_buffer_overruns(iter
->tr
->buffer
))
2460 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
2463 for_each_tracing_cpu(cpu
) {
2465 iter
->buffer_iter
[cpu
] =
2466 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
2468 if (!iter
->buffer_iter
[cpu
])
2472 /* TODO stop tracer */
2473 *ret
= seq_open(file
, &tracer_seq_ops
);
2477 m
= file
->private_data
;
2480 /* stop the trace while dumping */
2483 mutex_unlock(&trace_types_lock
);
2489 for_each_tracing_cpu(cpu
) {
2490 if (iter
->buffer_iter
[cpu
])
2491 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2493 mutex_unlock(&trace_types_lock
);
2496 return ERR_PTR(-ENOMEM
);
2499 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
2501 if (tracing_disabled
)
2504 filp
->private_data
= inode
->i_private
;
2508 int tracing_release(struct inode
*inode
, struct file
*file
)
2510 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
2511 struct trace_iterator
*iter
= m
->private;
2514 mutex_lock(&trace_types_lock
);
2515 for_each_tracing_cpu(cpu
) {
2516 if (iter
->buffer_iter
[cpu
])
2517 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
2520 if (iter
->trace
&& iter
->trace
->close
)
2521 iter
->trace
->close(iter
);
2523 /* reenable tracing if it was previously enabled */
2525 mutex_unlock(&trace_types_lock
);
2527 seq_release(inode
, file
);
2532 static int tracing_open(struct inode
*inode
, struct file
*file
)
2536 __tracing_open(inode
, file
, &ret
);
2541 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
2543 struct trace_iterator
*iter
;
2546 iter
= __tracing_open(inode
, file
, &ret
);
2549 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
2556 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2558 struct tracer
*t
= m
->private;
2570 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
2572 struct tracer
*t
= m
->private;
2575 mutex_lock(&trace_types_lock
);
2576 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
2582 static void t_stop(struct seq_file
*m
, void *p
)
2584 mutex_unlock(&trace_types_lock
);
2587 static int t_show(struct seq_file
*m
, void *v
)
2589 struct tracer
*t
= v
;
2594 seq_printf(m
, "%s", t
->name
);
2603 static struct seq_operations show_traces_seq_ops
= {
2610 static int show_traces_open(struct inode
*inode
, struct file
*file
)
2614 if (tracing_disabled
)
2617 ret
= seq_open(file
, &show_traces_seq_ops
);
2619 struct seq_file
*m
= file
->private_data
;
2620 m
->private = trace_types
;
2626 static struct file_operations tracing_fops
= {
2627 .open
= tracing_open
,
2629 .llseek
= seq_lseek
,
2630 .release
= tracing_release
,
2633 static struct file_operations tracing_lt_fops
= {
2634 .open
= tracing_lt_open
,
2636 .llseek
= seq_lseek
,
2637 .release
= tracing_release
,
2640 static struct file_operations show_traces_fops
= {
2641 .open
= show_traces_open
,
2643 .release
= seq_release
,
2647 * Only trace on a CPU if the bitmask is set:
2649 static cpumask_var_t tracing_cpumask
;
2652 * The tracer itself will not take this lock, but still we want
2653 * to provide a consistent cpumask to user-space:
2655 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
2658 * Temporary storage for the character representation of the
2659 * CPU bitmask (and one more byte for the newline):
2661 static char mask_str
[NR_CPUS
+ 1];
2664 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
2665 size_t count
, loff_t
*ppos
)
2669 mutex_lock(&tracing_cpumask_update_lock
);
2671 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
2672 if (count
- len
< 2) {
2676 len
+= sprintf(mask_str
+ len
, "\n");
2677 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
2680 mutex_unlock(&tracing_cpumask_update_lock
);
2686 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
2687 size_t count
, loff_t
*ppos
)
2690 cpumask_var_t tracing_cpumask_new
;
2692 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
2695 mutex_lock(&tracing_cpumask_update_lock
);
2696 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
2700 local_irq_disable();
2701 __raw_spin_lock(&ftrace_max_lock
);
2702 for_each_tracing_cpu(cpu
) {
2704 * Increase/decrease the disabled counter if we are
2705 * about to flip a bit in the cpumask:
2707 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2708 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2709 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2711 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
2712 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
2713 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2716 __raw_spin_unlock(&ftrace_max_lock
);
2719 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
2721 mutex_unlock(&tracing_cpumask_update_lock
);
2722 free_cpumask_var(tracing_cpumask_new
);
2727 mutex_unlock(&tracing_cpumask_update_lock
);
2728 free_cpumask_var(tracing_cpumask
);
2733 static struct file_operations tracing_cpumask_fops
= {
2734 .open
= tracing_open_generic
,
2735 .read
= tracing_cpumask_read
,
2736 .write
= tracing_cpumask_write
,
2740 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
2741 size_t cnt
, loff_t
*ppos
)
2747 u32 tracer_flags
= current_trace
->flags
->val
;
2748 struct tracer_opt
*trace_opts
= current_trace
->flags
->opts
;
2751 /* calulate max size */
2752 for (i
= 0; trace_options
[i
]; i
++) {
2753 len
+= strlen(trace_options
[i
]);
2754 len
+= 3; /* "no" and space */
2758 * Increase the size with names of options specific
2759 * of the current tracer.
2761 for (i
= 0; trace_opts
[i
].name
; i
++) {
2762 len
+= strlen(trace_opts
[i
].name
);
2763 len
+= 3; /* "no" and space */
2766 /* +2 for \n and \0 */
2767 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
2771 for (i
= 0; trace_options
[i
]; i
++) {
2772 if (trace_flags
& (1 << i
))
2773 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
2775 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
2778 for (i
= 0; trace_opts
[i
].name
; i
++) {
2779 if (tracer_flags
& trace_opts
[i
].bit
)
2780 r
+= sprintf(buf
+ r
, "%s ",
2781 trace_opts
[i
].name
);
2783 r
+= sprintf(buf
+ r
, "no%s ",
2784 trace_opts
[i
].name
);
2787 r
+= sprintf(buf
+ r
, "\n");
2788 WARN_ON(r
>= len
+ 2);
2790 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2797 /* Try to assign a tracer specific option */
2798 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2800 struct tracer_flags
*trace_flags
= trace
->flags
;
2801 struct tracer_opt
*opts
= NULL
;
2805 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2806 opts
= &trace_flags
->opts
[i
];
2807 len
= strlen(opts
->name
);
2809 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2810 ret
= trace
->set_flag(trace_flags
->val
,
2816 if (!trace_flags
->opts
[i
].name
)
2819 /* Refused to handle */
2824 trace_flags
->val
&= ~opts
->bit
;
2826 trace_flags
->val
|= opts
->bit
;
2832 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2833 size_t cnt
, loff_t
*ppos
)
2841 if (cnt
>= sizeof(buf
))
2844 if (copy_from_user(&buf
, ubuf
, cnt
))
2849 if (strncmp(buf
, "no", 2) == 0) {
2854 for (i
= 0; trace_options
[i
]; i
++) {
2855 int len
= strlen(trace_options
[i
]);
2857 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2859 trace_flags
&= ~(1 << i
);
2861 trace_flags
|= (1 << i
);
2866 /* If no option could be set, test the specific tracer options */
2867 if (!trace_options
[i
]) {
2868 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2878 static struct file_operations tracing_iter_fops
= {
2879 .open
= tracing_open_generic
,
2880 .read
= tracing_trace_options_read
,
2881 .write
= tracing_trace_options_write
,
2884 static const char readme_msg
[] =
2885 "tracing mini-HOWTO:\n\n"
2887 "# mount -t debugfs nodev /debug\n\n"
2888 "# cat /debug/tracing/available_tracers\n"
2889 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2890 "# cat /debug/tracing/current_tracer\n"
2892 "# echo sched_switch > /debug/tracing/current_tracer\n"
2893 "# cat /debug/tracing/current_tracer\n"
2895 "# cat /debug/tracing/trace_options\n"
2896 "noprint-parent nosym-offset nosym-addr noverbose\n"
2897 "# echo print-parent > /debug/tracing/trace_options\n"
2898 "# echo 1 > /debug/tracing/tracing_enabled\n"
2899 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2900 "echo 0 > /debug/tracing/tracing_enabled\n"
2904 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2905 size_t cnt
, loff_t
*ppos
)
2907 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2908 readme_msg
, strlen(readme_msg
));
2911 static struct file_operations tracing_readme_fops
= {
2912 .open
= tracing_open_generic
,
2913 .read
= tracing_readme_read
,
2917 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2918 size_t cnt
, loff_t
*ppos
)
2923 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2924 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2928 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2929 size_t cnt
, loff_t
*ppos
)
2931 struct trace_array
*tr
= filp
->private_data
;
2936 if (cnt
>= sizeof(buf
))
2939 if (copy_from_user(&buf
, ubuf
, cnt
))
2944 ret
= strict_strtoul(buf
, 10, &val
);
2950 mutex_lock(&trace_types_lock
);
2951 if (tracer_enabled
^ val
) {
2954 if (current_trace
->start
)
2955 current_trace
->start(tr
);
2960 if (current_trace
->stop
)
2961 current_trace
->stop(tr
);
2964 mutex_unlock(&trace_types_lock
);
2972 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2973 size_t cnt
, loff_t
*ppos
)
2975 char buf
[max_tracer_type_len
+2];
2978 mutex_lock(&trace_types_lock
);
2980 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2982 r
= sprintf(buf
, "\n");
2983 mutex_unlock(&trace_types_lock
);
2985 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2988 static int tracing_set_tracer(char *buf
)
2990 struct trace_array
*tr
= &global_trace
;
2994 mutex_lock(&trace_types_lock
);
2995 for (t
= trace_types
; t
; t
= t
->next
) {
2996 if (strcmp(t
->name
, buf
) == 0)
3003 if (t
== current_trace
)
3006 trace_branch_disable();
3007 if (current_trace
&& current_trace
->reset
)
3008 current_trace
->reset(tr
);
3017 trace_branch_enable(tr
);
3019 mutex_unlock(&trace_types_lock
);
3025 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
3026 size_t cnt
, loff_t
*ppos
)
3028 char buf
[max_tracer_type_len
+1];
3035 if (cnt
> max_tracer_type_len
)
3036 cnt
= max_tracer_type_len
;
3038 if (copy_from_user(&buf
, ubuf
, cnt
))
3043 /* strip ending whitespace. */
3044 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
3047 err
= tracing_set_tracer(buf
);
3057 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
3058 size_t cnt
, loff_t
*ppos
)
3060 unsigned long *ptr
= filp
->private_data
;
3064 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
3065 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
3066 if (r
> sizeof(buf
))
3068 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3072 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
3073 size_t cnt
, loff_t
*ppos
)
3075 long *ptr
= filp
->private_data
;
3080 if (cnt
>= sizeof(buf
))
3083 if (copy_from_user(&buf
, ubuf
, cnt
))
3088 ret
= strict_strtoul(buf
, 10, &val
);
3097 static atomic_t tracing_reader
;
3099 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
3101 struct trace_iterator
*iter
;
3103 if (tracing_disabled
)
3106 /* We only allow for reader of the pipe */
3107 if (atomic_inc_return(&tracing_reader
) != 1) {
3108 atomic_dec(&tracing_reader
);
3112 /* create a buffer to store the information to pass to userspace */
3113 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
3117 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
3122 mutex_lock(&trace_types_lock
);
3124 /* trace pipe does not show start of buffer */
3125 cpumask_setall(iter
->started
);
3127 iter
->tr
= &global_trace
;
3128 iter
->trace
= current_trace
;
3129 filp
->private_data
= iter
;
3131 if (iter
->trace
->pipe_open
)
3132 iter
->trace
->pipe_open(iter
);
3133 mutex_unlock(&trace_types_lock
);
3138 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
3140 struct trace_iterator
*iter
= file
->private_data
;
3142 free_cpumask_var(iter
->started
);
3144 atomic_dec(&tracing_reader
);
3150 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
3152 struct trace_iterator
*iter
= filp
->private_data
;
3154 if (trace_flags
& TRACE_ITER_BLOCK
) {
3156 * Always select as readable when in blocking mode
3158 return POLLIN
| POLLRDNORM
;
3160 if (!trace_empty(iter
))
3161 return POLLIN
| POLLRDNORM
;
3162 poll_wait(filp
, &trace_wait
, poll_table
);
3163 if (!trace_empty(iter
))
3164 return POLLIN
| POLLRDNORM
;
3174 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
3175 size_t cnt
, loff_t
*ppos
)
3177 struct trace_iterator
*iter
= filp
->private_data
;
3180 /* return any leftover data */
3181 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3185 trace_seq_reset(&iter
->seq
);
3187 mutex_lock(&trace_types_lock
);
3188 if (iter
->trace
->read
) {
3189 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
3196 while (trace_empty(iter
)) {
3198 if ((filp
->f_flags
& O_NONBLOCK
)) {
3204 * This is a make-shift waitqueue. The reason we don't use
3205 * an actual wait queue is because:
3206 * 1) we only ever have one waiter
3207 * 2) the tracing, traces all functions, we don't want
3208 * the overhead of calling wake_up and friends
3209 * (and tracing them too)
3210 * Anyway, this is really very primitive wakeup.
3212 set_current_state(TASK_INTERRUPTIBLE
);
3213 iter
->tr
->waiter
= current
;
3215 mutex_unlock(&trace_types_lock
);
3217 /* sleep for 100 msecs, and try again. */
3218 schedule_timeout(HZ
/10);
3220 mutex_lock(&trace_types_lock
);
3222 iter
->tr
->waiter
= NULL
;
3224 if (signal_pending(current
)) {
3229 if (iter
->trace
!= current_trace
)
3233 * We block until we read something and tracing is disabled.
3234 * We still block if tracing is disabled, but we have never
3235 * read anything. This allows a user to cat this file, and
3236 * then enable tracing. But after we have read something,
3237 * we give an EOF when tracing is again disabled.
3239 * iter->pos will be 0 if we haven't read anything.
3241 if (!tracer_enabled
&& iter
->pos
)
3247 /* stop when tracing is finished */
3248 if (trace_empty(iter
))
3251 if (cnt
>= PAGE_SIZE
)
3252 cnt
= PAGE_SIZE
- 1;
3254 /* reset all but tr, trace, and overruns */
3255 memset(&iter
->seq
, 0,
3256 sizeof(struct trace_iterator
) -
3257 offsetof(struct trace_iterator
, seq
));
3260 while (find_next_entry_inc(iter
) != NULL
) {
3261 enum print_line_t ret
;
3262 int len
= iter
->seq
.len
;
3264 ret
= print_trace_line(iter
);
3265 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
3266 /* don't print partial lines */
3267 iter
->seq
.len
= len
;
3271 trace_consume(iter
);
3273 if (iter
->seq
.len
>= cnt
)
3277 /* Now copy what we have to the user */
3278 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
3279 if (iter
->seq
.readpos
>= iter
->seq
.len
)
3280 trace_seq_reset(&iter
->seq
);
3283 * If there was nothing to send to user, inspite of consuming trace
3284 * entries, go back to wait for more entries.
3290 mutex_unlock(&trace_types_lock
);
3296 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
3297 size_t cnt
, loff_t
*ppos
)
3299 struct trace_array
*tr
= filp
->private_data
;
3303 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
3304 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3308 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
3309 size_t cnt
, loff_t
*ppos
)
3315 if (cnt
>= sizeof(buf
))
3318 if (copy_from_user(&buf
, ubuf
, cnt
))
3323 ret
= strict_strtoul(buf
, 10, &val
);
3327 /* must have at least 1 entry */
3331 mutex_lock(&trace_types_lock
);
3335 /* disable all cpu buffers */
3336 for_each_tracing_cpu(cpu
) {
3337 if (global_trace
.data
[cpu
])
3338 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3339 if (max_tr
.data
[cpu
])
3340 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
3343 /* value is in KB */
3346 if (val
!= global_trace
.entries
) {
3347 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
3353 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
3357 r
= ring_buffer_resize(global_trace
.buffer
,
3358 global_trace
.entries
);
3360 /* AARGH! We are left with different
3361 * size max buffer!!!! */
3363 tracing_disabled
= 1;
3368 global_trace
.entries
= val
;
3373 /* If check pages failed, return ENOMEM */
3374 if (tracing_disabled
)
3377 for_each_tracing_cpu(cpu
) {
3378 if (global_trace
.data
[cpu
])
3379 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
3380 if (max_tr
.data
[cpu
])
3381 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
3385 max_tr
.entries
= global_trace
.entries
;
3386 mutex_unlock(&trace_types_lock
);
3391 static int mark_printk(const char *fmt
, ...)
3395 va_start(args
, fmt
);
3396 ret
= trace_vprintk(0, -1, fmt
, args
);
3402 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
3403 size_t cnt
, loff_t
*fpos
)
3408 if (tracing_disabled
)
3411 if (cnt
> TRACE_BUF_SIZE
)
3412 cnt
= TRACE_BUF_SIZE
;
3414 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
3418 if (copy_from_user(buf
, ubuf
, cnt
)) {
3423 /* Cut from the first nil or newline. */
3425 end
= strchr(buf
, '\n');
3429 cnt
= mark_printk("%s\n", buf
);
3436 static struct file_operations tracing_max_lat_fops
= {
3437 .open
= tracing_open_generic
,
3438 .read
= tracing_max_lat_read
,
3439 .write
= tracing_max_lat_write
,
3442 static struct file_operations tracing_ctrl_fops
= {
3443 .open
= tracing_open_generic
,
3444 .read
= tracing_ctrl_read
,
3445 .write
= tracing_ctrl_write
,
3448 static struct file_operations set_tracer_fops
= {
3449 .open
= tracing_open_generic
,
3450 .read
= tracing_set_trace_read
,
3451 .write
= tracing_set_trace_write
,
3454 static struct file_operations tracing_pipe_fops
= {
3455 .open
= tracing_open_pipe
,
3456 .poll
= tracing_poll_pipe
,
3457 .read
= tracing_read_pipe
,
3458 .release
= tracing_release_pipe
,
3461 static struct file_operations tracing_entries_fops
= {
3462 .open
= tracing_open_generic
,
3463 .read
= tracing_entries_read
,
3464 .write
= tracing_entries_write
,
3467 static struct file_operations tracing_mark_fops
= {
3468 .open
= tracing_open_generic
,
3469 .write
= tracing_mark_write
,
3472 #ifdef CONFIG_DYNAMIC_FTRACE
3474 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
3480 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
3481 size_t cnt
, loff_t
*ppos
)
3483 static char ftrace_dyn_info_buffer
[1024];
3484 static DEFINE_MUTEX(dyn_info_mutex
);
3485 unsigned long *p
= filp
->private_data
;
3486 char *buf
= ftrace_dyn_info_buffer
;
3487 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
3490 mutex_lock(&dyn_info_mutex
);
3491 r
= sprintf(buf
, "%ld ", *p
);
3493 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
3496 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
3498 mutex_unlock(&dyn_info_mutex
);
3503 static struct file_operations tracing_dyn_info_fops
= {
3504 .open
= tracing_open_generic
,
3505 .read
= tracing_read_dyn_info
,
3509 static struct dentry
*d_tracer
;
3511 struct dentry
*tracing_init_dentry(void)
3518 d_tracer
= debugfs_create_dir("tracing", NULL
);
3520 if (!d_tracer
&& !once
) {
3522 pr_warning("Could not create debugfs directory 'tracing'\n");
3529 #ifdef CONFIG_FTRACE_SELFTEST
3530 /* Let selftest have access to static functions in this file */
3531 #include "trace_selftest.c"
3534 static __init
int tracer_init_debugfs(void)
3536 struct dentry
*d_tracer
;
3537 struct dentry
*entry
;
3539 d_tracer
= tracing_init_dentry();
3541 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
3542 &global_trace
, &tracing_ctrl_fops
);
3544 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
3546 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
3547 NULL
, &tracing_iter_fops
);
3549 pr_warning("Could not create debugfs 'trace_options' entry\n");
3551 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
3552 NULL
, &tracing_cpumask_fops
);
3554 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
3556 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
3557 &global_trace
, &tracing_lt_fops
);
3559 pr_warning("Could not create debugfs 'latency_trace' entry\n");
3561 entry
= debugfs_create_file("trace", 0444, d_tracer
,
3562 &global_trace
, &tracing_fops
);
3564 pr_warning("Could not create debugfs 'trace' entry\n");
3566 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
3567 &global_trace
, &show_traces_fops
);
3569 pr_warning("Could not create debugfs 'available_tracers' entry\n");
3571 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
3572 &global_trace
, &set_tracer_fops
);
3574 pr_warning("Could not create debugfs 'current_tracer' entry\n");
3576 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
3577 &tracing_max_latency
,
3578 &tracing_max_lat_fops
);
3580 pr_warning("Could not create debugfs "
3581 "'tracing_max_latency' entry\n");
3583 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
3584 &tracing_thresh
, &tracing_max_lat_fops
);
3586 pr_warning("Could not create debugfs "
3587 "'tracing_thresh' entry\n");
3588 entry
= debugfs_create_file("README", 0644, d_tracer
,
3589 NULL
, &tracing_readme_fops
);
3591 pr_warning("Could not create debugfs 'README' entry\n");
3593 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
3594 NULL
, &tracing_pipe_fops
);
3596 pr_warning("Could not create debugfs "
3597 "'trace_pipe' entry\n");
3599 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
3600 &global_trace
, &tracing_entries_fops
);
3602 pr_warning("Could not create debugfs "
3603 "'buffer_size_kb' entry\n");
3605 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
3606 NULL
, &tracing_mark_fops
);
3608 pr_warning("Could not create debugfs "
3609 "'trace_marker' entry\n");
3611 #ifdef CONFIG_DYNAMIC_FTRACE
3612 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
3613 &ftrace_update_tot_cnt
,
3614 &tracing_dyn_info_fops
);
3616 pr_warning("Could not create debugfs "
3617 "'dyn_ftrace_total_info' entry\n");
3619 #ifdef CONFIG_SYSPROF_TRACER
3620 init_tracer_sysprof_debugfs(d_tracer
);
3625 int trace_vprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
3627 static DEFINE_SPINLOCK(trace_buf_lock
);
3628 static char trace_buf
[TRACE_BUF_SIZE
];
3630 struct ring_buffer_event
*event
;
3631 struct trace_array
*tr
= &global_trace
;
3632 struct trace_array_cpu
*data
;
3633 int cpu
, len
= 0, size
, pc
;
3634 struct print_entry
*entry
;
3635 unsigned long irq_flags
;
3637 if (tracing_disabled
|| tracing_selftest_running
)
3640 pc
= preempt_count();
3641 preempt_disable_notrace();
3642 cpu
= raw_smp_processor_id();
3643 data
= tr
->data
[cpu
];
3645 if (unlikely(atomic_read(&data
->disabled
)))
3648 pause_graph_tracing();
3649 spin_lock_irqsave(&trace_buf_lock
, irq_flags
);
3650 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
3652 len
= min(len
, TRACE_BUF_SIZE
-1);
3655 size
= sizeof(*entry
) + len
+ 1;
3656 event
= ring_buffer_lock_reserve(tr
->buffer
, size
, &irq_flags
);
3659 entry
= ring_buffer_event_data(event
);
3660 tracing_generic_entry_update(&entry
->ent
, irq_flags
, pc
);
3661 entry
->ent
.type
= TRACE_PRINT
;
3663 entry
->depth
= depth
;
3665 memcpy(&entry
->buf
, trace_buf
, len
);
3666 entry
->buf
[len
] = 0;
3667 ring_buffer_unlock_commit(tr
->buffer
, event
, irq_flags
);
3670 spin_unlock_irqrestore(&trace_buf_lock
, irq_flags
);
3671 unpause_graph_tracing();
3673 preempt_enable_notrace();
3677 EXPORT_SYMBOL_GPL(trace_vprintk
);
3679 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3684 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3688 ret
= trace_vprintk(ip
, task_curr_ret_stack(current
), fmt
, ap
);
3692 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3694 static int trace_panic_handler(struct notifier_block
*this,
3695 unsigned long event
, void *unused
)
3697 if (ftrace_dump_on_oops
)
3702 static struct notifier_block trace_panic_notifier
= {
3703 .notifier_call
= trace_panic_handler
,
3705 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3708 static int trace_die_handler(struct notifier_block
*self
,
3714 if (ftrace_dump_on_oops
)
3723 static struct notifier_block trace_die_notifier
= {
3724 .notifier_call
= trace_die_handler
,
3729 * printk is set to max of 1024, we really don't need it that big.
3730 * Nothing should be printing 1000 characters anyway.
3732 #define TRACE_MAX_PRINT 1000
3735 * Define here KERN_TRACE so that we have one place to modify
3736 * it if we decide to change what log level the ftrace dump
3739 #define KERN_TRACE KERN_EMERG
3742 trace_printk_seq(struct trace_seq
*s
)
3744 /* Probably should print a warning here. */
3748 /* should be zero ended, but we are paranoid. */
3749 s
->buffer
[s
->len
] = 0;
3751 printk(KERN_TRACE
"%s", s
->buffer
);
3756 void ftrace_dump(void)
3758 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3759 /* use static because iter can be a bit big for the stack */
3760 static struct trace_iterator iter
;
3761 static int dump_ran
;
3762 unsigned long flags
;
3766 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3772 /* No turning back! */
3776 for_each_tracing_cpu(cpu
) {
3777 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3780 /* don't look at user memory in panic mode */
3781 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
3783 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3785 iter
.tr
= &global_trace
;
3786 iter
.trace
= current_trace
;
3789 * We need to stop all tracing on all CPUS to read the
3790 * the next buffer. This is a bit expensive, but is
3791 * not done often. We fill all what we can read,
3792 * and then release the locks again.
3795 while (!trace_empty(&iter
)) {
3798 printk(KERN_TRACE
"---------------------------------\n");
3802 /* reset all but tr, trace, and overruns */
3803 memset(&iter
.seq
, 0,
3804 sizeof(struct trace_iterator
) -
3805 offsetof(struct trace_iterator
, seq
));
3806 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3809 if (find_next_entry_inc(&iter
) != NULL
) {
3810 print_trace_line(&iter
);
3811 trace_consume(&iter
);
3814 trace_printk_seq(&iter
.seq
);
3818 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3820 printk(KERN_TRACE
"---------------------------------\n");
3823 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3826 __init
static int tracer_alloc_buffers(void)
3828 struct trace_array_cpu
*data
;
3832 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
3835 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
3836 goto out_free_buffer_mask
;
3838 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
3839 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
3841 /* TODO: make the number of buffers hot pluggable with CPUS */
3842 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3843 TRACE_BUFFER_FLAGS
);
3844 if (!global_trace
.buffer
) {
3845 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3847 goto out_free_cpumask
;
3849 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3852 #ifdef CONFIG_TRACER_MAX_TRACE
3853 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3854 TRACE_BUFFER_FLAGS
);
3855 if (!max_tr
.buffer
) {
3856 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3858 ring_buffer_free(global_trace
.buffer
);
3859 goto out_free_cpumask
;
3861 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3862 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3865 /* Allocate the first page for all buffers */
3866 for_each_tracing_cpu(i
) {
3867 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3868 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3871 trace_init_cmdlines();
3873 register_tracer(&nop_trace
);
3874 #ifdef CONFIG_BOOT_TRACER
3875 register_tracer(&boot_tracer
);
3876 current_trace
= &boot_tracer
;
3877 current_trace
->init(&global_trace
);
3879 current_trace
= &nop_trace
;
3882 /* All seems OK, enable tracing */
3883 tracing_disabled
= 0;
3885 atomic_notifier_chain_register(&panic_notifier_list
,
3886 &trace_panic_notifier
);
3888 register_die_notifier(&trace_die_notifier
);
3892 free_cpumask_var(tracing_cpumask
);
3893 out_free_buffer_mask
:
3894 free_cpumask_var(tracing_buffer_mask
);
3898 early_initcall(tracer_alloc_buffers
);
3899 fs_initcall(tracer_init_debugfs
);