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>
34 #include <linux/splice.h>
36 #include <linux/stacktrace.h>
37 #include <linux/ring_buffer.h>
38 #include <linux/irqflags.h>
41 #include "trace_output.h"
43 #define TRACE_BUFFER_FLAGS (RB_FL_OVERWRITE)
45 unsigned long __read_mostly tracing_max_latency
;
46 unsigned long __read_mostly tracing_thresh
;
49 * We need to change this state when a selftest is running.
50 * A selftest will lurk into the ring-buffer to count the
51 * entries inserted during the selftest although some concurrent
52 * insertions into the ring-buffer such as ftrace_printk could occurred
53 * at the same time, giving false positive or negative results.
55 static bool __read_mostly tracing_selftest_running
;
58 * If a tracer is running, we do not want to run SELFTEST.
60 static bool __read_mostly tracing_selftest_disabled
;
62 /* For tracers that don't implement custom flags */
63 static struct tracer_opt dummy_tracer_opt
[] = {
67 static struct tracer_flags dummy_tracer_flags
= {
69 .opts
= dummy_tracer_opt
72 static int dummy_set_flag(u32 old_flags
, u32 bit
, int set
)
78 * Kill all tracing for good (never come back).
79 * It is initialized to 1 but will turn to zero if the initialization
80 * of the tracer is successful. But that is the only place that sets
83 int tracing_disabled
= 1;
85 static DEFINE_PER_CPU(local_t
, ftrace_cpu_disabled
);
87 static inline void ftrace_disable_cpu(void)
90 local_inc(&__get_cpu_var(ftrace_cpu_disabled
));
93 static inline void ftrace_enable_cpu(void)
95 local_dec(&__get_cpu_var(ftrace_cpu_disabled
));
99 static cpumask_var_t __read_mostly tracing_buffer_mask
;
101 #define for_each_tracing_cpu(cpu) \
102 for_each_cpu(cpu, tracing_buffer_mask)
105 * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
107 * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
108 * is set, then ftrace_dump is called. This will output the contents
109 * of the ftrace buffers to the console. This is very useful for
110 * capturing traces that lead to crashes and outputing it to a
113 * It is default off, but you can enable it with either specifying
114 * "ftrace_dump_on_oops" in the kernel command line, or setting
115 * /proc/sys/kernel/ftrace_dump_on_oops to true.
117 int ftrace_dump_on_oops
;
119 static int tracing_set_tracer(const char *buf
);
121 #define BOOTUP_TRACER_SIZE 100
122 static char bootup_tracer_buf
[BOOTUP_TRACER_SIZE
] __initdata
;
123 static char *default_bootup_tracer
;
125 static int __init
set_ftrace(char *str
)
127 strncpy(bootup_tracer_buf
, str
, BOOTUP_TRACER_SIZE
);
128 default_bootup_tracer
= bootup_tracer_buf
;
131 __setup("ftrace=", set_ftrace
);
133 static int __init
set_ftrace_dump_on_oops(char *str
)
135 ftrace_dump_on_oops
= 1;
138 __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops
);
141 ns2usecs(cycle_t nsec
)
148 cycle_t
ftrace_now(int cpu
)
150 u64 ts
= ring_buffer_time_stamp(cpu
);
151 ring_buffer_normalize_time_stamp(cpu
, &ts
);
156 * The global_trace is the descriptor that holds the tracing
157 * buffers for the live tracing. For each CPU, it contains
158 * a link list of pages that will store trace entries. The
159 * page descriptor of the pages in the memory is used to hold
160 * the link list by linking the lru item in the page descriptor
161 * to each of the pages in the buffer per CPU.
163 * For each active CPU there is a data field that holds the
164 * pages for the buffer for that CPU. Each CPU has the same number
165 * of pages allocated for its buffer.
167 static struct trace_array global_trace
;
169 static DEFINE_PER_CPU(struct trace_array_cpu
, global_trace_cpu
);
172 * The max_tr is used to snapshot the global_trace when a maximum
173 * latency is reached. Some tracers will use this to store a maximum
174 * trace while it continues examining live traces.
176 * The buffers for the max_tr are set up the same as the global_trace.
177 * When a snapshot is taken, the link list of the max_tr is swapped
178 * with the link list of the global_trace and the buffers are reset for
179 * the global_trace so the tracing can continue.
181 static struct trace_array max_tr
;
183 static DEFINE_PER_CPU(struct trace_array_cpu
, max_data
);
185 /* tracer_enabled is used to toggle activation of a tracer */
186 static int tracer_enabled
= 1;
189 * tracing_is_enabled - return tracer_enabled status
191 * This function is used by other tracers to know the status
192 * of the tracer_enabled flag. Tracers may use this function
193 * to know if it should enable their features when starting
194 * up. See irqsoff tracer for an example (start_irqsoff_tracer).
196 int tracing_is_enabled(void)
198 return tracer_enabled
;
202 * trace_buf_size is the size in bytes that is allocated
203 * for a buffer. Note, the number of bytes is always rounded
206 * This number is purposely set to a low number of 16384.
207 * If the dump on oops happens, it will be much appreciated
208 * to not have to wait for all that output. Anyway this can be
209 * boot time and run time configurable.
211 #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
213 static unsigned long trace_buf_size
= TRACE_BUF_SIZE_DEFAULT
;
215 /* trace_types holds a link list of available tracers. */
216 static struct tracer
*trace_types __read_mostly
;
218 /* current_trace points to the tracer that is currently active */
219 static struct tracer
*current_trace __read_mostly
;
222 * max_tracer_type_len is used to simplify the allocating of
223 * buffers to read userspace tracer names. We keep track of
224 * the longest tracer name registered.
226 static int max_tracer_type_len
;
229 * trace_types_lock is used to protect the trace_types list.
230 * This lock is also used to keep user access serialized.
231 * Accesses from userspace will grab this lock while userspace
232 * activities happen inside the kernel.
234 static DEFINE_MUTEX(trace_types_lock
);
236 /* trace_wait is a waitqueue for tasks blocked on trace_poll */
237 static DECLARE_WAIT_QUEUE_HEAD(trace_wait
);
239 /* trace_flags holds trace_options default values */
240 unsigned long trace_flags
= TRACE_ITER_PRINT_PARENT
| TRACE_ITER_PRINTK
|
241 TRACE_ITER_ANNOTATE
| TRACE_ITER_CONTEXT_INFO
;
244 * trace_wake_up - wake up tasks waiting for trace input
246 * Simply wakes up any task that is blocked on the trace_wait
247 * queue. These is used with trace_poll for tasks polling the trace.
249 void trace_wake_up(void)
252 * The runqueue_is_locked() can fail, but this is the best we
255 if (!(trace_flags
& TRACE_ITER_BLOCK
) && !runqueue_is_locked())
256 wake_up(&trace_wait
);
259 static int __init
set_buf_size(char *str
)
261 unsigned long buf_size
;
266 ret
= strict_strtoul(str
, 0, &buf_size
);
267 /* nr_entries can not be zero */
268 if (ret
< 0 || buf_size
== 0)
270 trace_buf_size
= buf_size
;
273 __setup("trace_buf_size=", set_buf_size
);
275 unsigned long nsecs_to_usecs(unsigned long nsecs
)
280 /* These must match the bit postions in trace_iterator_flags */
281 static const char *trace_options
[] = {
304 * ftrace_max_lock is used to protect the swapping of buffers
305 * when taking a max snapshot. The buffers themselves are
306 * protected by per_cpu spinlocks. But the action of the swap
307 * needs its own lock.
309 * This is defined as a raw_spinlock_t in order to help
310 * with performance when lockdep debugging is enabled.
312 static raw_spinlock_t ftrace_max_lock
=
313 (raw_spinlock_t
)__RAW_SPIN_LOCK_UNLOCKED
;
316 * Copy the new maximum trace into the separate maximum-trace
317 * structure. (this way the maximum trace is permanently saved,
318 * for later retrieval via /debugfs/tracing/latency_trace)
321 __update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
323 struct trace_array_cpu
*data
= tr
->data
[cpu
];
326 max_tr
.time_start
= data
->preempt_timestamp
;
328 data
= max_tr
.data
[cpu
];
329 data
->saved_latency
= tracing_max_latency
;
331 memcpy(data
->comm
, tsk
->comm
, TASK_COMM_LEN
);
332 data
->pid
= tsk
->pid
;
333 data
->uid
= task_uid(tsk
);
334 data
->nice
= tsk
->static_prio
- 20 - MAX_RT_PRIO
;
335 data
->policy
= tsk
->policy
;
336 data
->rt_priority
= tsk
->rt_priority
;
338 /* record this tasks comm */
339 tracing_record_cmdline(current
);
343 trace_seq_reset(struct trace_seq
*s
)
349 ssize_t
trace_seq_to_user(struct trace_seq
*s
, char __user
*ubuf
, size_t cnt
)
354 if (s
->len
<= s
->readpos
)
357 len
= s
->len
- s
->readpos
;
360 ret
= copy_to_user(ubuf
, s
->buffer
+ s
->readpos
, cnt
);
368 ssize_t
trace_seq_to_buffer(struct trace_seq
*s
, void *buf
, size_t cnt
)
373 if (s
->len
<= s
->readpos
)
376 len
= s
->len
- s
->readpos
;
379 ret
= memcpy(buf
, s
->buffer
+ s
->readpos
, cnt
);
388 trace_print_seq(struct seq_file
*m
, struct trace_seq
*s
)
390 int len
= s
->len
>= PAGE_SIZE
? PAGE_SIZE
- 1 : s
->len
;
393 seq_puts(m
, s
->buffer
);
399 * update_max_tr - snapshot all trace buffers from global_trace to max_tr
401 * @tsk: the task with the latency
402 * @cpu: The cpu that initiated the trace.
404 * Flip the buffers between the @tr and the max_tr and record information
405 * about which task was the cause of this latency.
408 update_max_tr(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
410 struct ring_buffer
*buf
= tr
->buffer
;
412 WARN_ON_ONCE(!irqs_disabled());
413 __raw_spin_lock(&ftrace_max_lock
);
415 tr
->buffer
= max_tr
.buffer
;
418 ftrace_disable_cpu();
419 ring_buffer_reset(tr
->buffer
);
422 __update_max_tr(tr
, tsk
, cpu
);
423 __raw_spin_unlock(&ftrace_max_lock
);
427 * update_max_tr_single - only copy one trace over, and reset the rest
429 * @tsk - task with the latency
430 * @cpu - the cpu of the buffer to copy.
432 * Flip the trace of a single CPU buffer between the @tr and the max_tr.
435 update_max_tr_single(struct trace_array
*tr
, struct task_struct
*tsk
, int cpu
)
439 WARN_ON_ONCE(!irqs_disabled());
440 __raw_spin_lock(&ftrace_max_lock
);
442 ftrace_disable_cpu();
444 ring_buffer_reset(max_tr
.buffer
);
445 ret
= ring_buffer_swap_cpu(max_tr
.buffer
, tr
->buffer
, cpu
);
449 WARN_ON_ONCE(ret
&& ret
!= -EAGAIN
);
451 __update_max_tr(tr
, tsk
, cpu
);
452 __raw_spin_unlock(&ftrace_max_lock
);
456 * register_tracer - register a tracer with the ftrace system.
457 * @type - the plugin for the tracer
459 * Register a new plugin tracer.
461 int register_tracer(struct tracer
*type
)
468 pr_info("Tracer must have a name\n");
473 * When this gets called we hold the BKL which means that
474 * preemption is disabled. Various trace selftests however
475 * need to disable and enable preemption for successful tests.
476 * So we drop the BKL here and grab it after the tests again.
479 mutex_lock(&trace_types_lock
);
481 tracing_selftest_running
= true;
483 for (t
= trace_types
; t
; t
= t
->next
) {
484 if (strcmp(type
->name
, t
->name
) == 0) {
486 pr_info("Trace %s already registered\n",
494 type
->set_flag
= &dummy_set_flag
;
496 type
->flags
= &dummy_tracer_flags
;
498 if (!type
->flags
->opts
)
499 type
->flags
->opts
= dummy_tracer_opt
;
501 #ifdef CONFIG_FTRACE_STARTUP_TEST
502 if (type
->selftest
&& !tracing_selftest_disabled
) {
503 struct tracer
*saved_tracer
= current_trace
;
504 struct trace_array
*tr
= &global_trace
;
508 * Run a selftest on this tracer.
509 * Here we reset the trace buffer, and set the current
510 * tracer to be this tracer. The tracer can then run some
511 * internal tracing to verify that everything is in order.
512 * If we fail, we do not register this tracer.
514 for_each_tracing_cpu(i
)
515 tracing_reset(tr
, i
);
517 current_trace
= type
;
518 /* the test is responsible for initializing and enabling */
519 pr_info("Testing tracer %s: ", type
->name
);
520 ret
= type
->selftest(type
, tr
);
521 /* the test is responsible for resetting too */
522 current_trace
= saved_tracer
;
524 printk(KERN_CONT
"FAILED!\n");
527 /* Only reset on passing, to avoid touching corrupted buffers */
528 for_each_tracing_cpu(i
)
529 tracing_reset(tr
, i
);
531 printk(KERN_CONT
"PASSED\n");
535 type
->next
= trace_types
;
537 len
= strlen(type
->name
);
538 if (len
> max_tracer_type_len
)
539 max_tracer_type_len
= len
;
542 tracing_selftest_running
= false;
543 mutex_unlock(&trace_types_lock
);
545 if (ret
|| !default_bootup_tracer
)
548 if (strncmp(default_bootup_tracer
, type
->name
, BOOTUP_TRACER_SIZE
))
551 printk(KERN_INFO
"Starting tracer '%s'\n", type
->name
);
552 /* Do we want this tracer to start on bootup? */
553 tracing_set_tracer(type
->name
);
554 default_bootup_tracer
= NULL
;
555 /* disable other selftests, since this will break it. */
556 tracing_selftest_disabled
= 1;
557 #ifdef CONFIG_FTRACE_STARTUP_TEST
558 printk(KERN_INFO
"Disabling FTRACE selftests due to running tracer '%s'\n",
567 void unregister_tracer(struct tracer
*type
)
572 mutex_lock(&trace_types_lock
);
573 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
577 pr_info("Trace %s not registered\n", type
->name
);
583 if (type
== current_trace
&& tracer_enabled
) {
586 if (current_trace
->stop
)
587 current_trace
->stop(&global_trace
);
588 current_trace
= &nop_trace
;
591 if (strlen(type
->name
) != max_tracer_type_len
)
594 max_tracer_type_len
= 0;
595 for (t
= &trace_types
; *t
; t
= &(*t
)->next
) {
596 len
= strlen((*t
)->name
);
597 if (len
> max_tracer_type_len
)
598 max_tracer_type_len
= len
;
601 mutex_unlock(&trace_types_lock
);
604 void tracing_reset(struct trace_array
*tr
, int cpu
)
606 ftrace_disable_cpu();
607 ring_buffer_reset_cpu(tr
->buffer
, cpu
);
611 void tracing_reset_online_cpus(struct trace_array
*tr
)
615 tr
->time_start
= ftrace_now(tr
->cpu
);
617 for_each_online_cpu(cpu
)
618 tracing_reset(tr
, cpu
);
621 #define SAVED_CMDLINES 128
622 static unsigned map_pid_to_cmdline
[PID_MAX_DEFAULT
+1];
623 static unsigned map_cmdline_to_pid
[SAVED_CMDLINES
];
624 static char saved_cmdlines
[SAVED_CMDLINES
][TASK_COMM_LEN
];
625 static int cmdline_idx
;
626 static DEFINE_SPINLOCK(trace_cmdline_lock
);
628 /* temporary disable recording */
629 atomic_t trace_record_cmdline_disabled __read_mostly
;
631 static void trace_init_cmdlines(void)
633 memset(&map_pid_to_cmdline
, -1, sizeof(map_pid_to_cmdline
));
634 memset(&map_cmdline_to_pid
, -1, sizeof(map_cmdline_to_pid
));
638 static int trace_stop_count
;
639 static DEFINE_SPINLOCK(tracing_start_lock
);
642 * ftrace_off_permanent - disable all ftrace code permanently
644 * This should only be called when a serious anomally has
645 * been detected. This will turn off the function tracing,
646 * ring buffers, and other tracing utilites. It takes no
647 * locks and can be called from any context.
649 void ftrace_off_permanent(void)
651 tracing_disabled
= 1;
653 tracing_off_permanent();
657 * tracing_start - quick start of the tracer
659 * If tracing is enabled but was stopped by tracing_stop,
660 * this will start the tracer back up.
662 void tracing_start(void)
664 struct ring_buffer
*buffer
;
667 if (tracing_disabled
)
670 spin_lock_irqsave(&tracing_start_lock
, flags
);
671 if (--trace_stop_count
) {
672 if (trace_stop_count
< 0) {
673 /* Someone screwed up their debugging */
675 trace_stop_count
= 0;
681 buffer
= global_trace
.buffer
;
683 ring_buffer_record_enable(buffer
);
685 buffer
= max_tr
.buffer
;
687 ring_buffer_record_enable(buffer
);
691 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
695 * tracing_stop - quick stop of the tracer
697 * Light weight way to stop tracing. Use in conjunction with
700 void tracing_stop(void)
702 struct ring_buffer
*buffer
;
706 spin_lock_irqsave(&tracing_start_lock
, flags
);
707 if (trace_stop_count
++)
710 buffer
= global_trace
.buffer
;
712 ring_buffer_record_disable(buffer
);
714 buffer
= max_tr
.buffer
;
716 ring_buffer_record_disable(buffer
);
719 spin_unlock_irqrestore(&tracing_start_lock
, flags
);
722 void trace_stop_cmdline_recording(void);
724 static void trace_save_cmdline(struct task_struct
*tsk
)
729 if (!tsk
->pid
|| unlikely(tsk
->pid
> PID_MAX_DEFAULT
))
733 * It's not the end of the world if we don't get
734 * the lock, but we also don't want to spin
735 * nor do we want to disable interrupts,
736 * so if we miss here, then better luck next time.
738 if (!spin_trylock(&trace_cmdline_lock
))
741 idx
= map_pid_to_cmdline
[tsk
->pid
];
742 if (idx
>= SAVED_CMDLINES
) {
743 idx
= (cmdline_idx
+ 1) % SAVED_CMDLINES
;
745 map
= map_cmdline_to_pid
[idx
];
746 if (map
<= PID_MAX_DEFAULT
)
747 map_pid_to_cmdline
[map
] = (unsigned)-1;
749 map_pid_to_cmdline
[tsk
->pid
] = idx
;
754 memcpy(&saved_cmdlines
[idx
], tsk
->comm
, TASK_COMM_LEN
);
756 spin_unlock(&trace_cmdline_lock
);
759 char *trace_find_cmdline(int pid
)
761 char *cmdline
= "<...>";
767 if (pid
> PID_MAX_DEFAULT
)
770 map
= map_pid_to_cmdline
[pid
];
771 if (map
>= SAVED_CMDLINES
)
774 cmdline
= saved_cmdlines
[map
];
780 void tracing_record_cmdline(struct task_struct
*tsk
)
782 if (atomic_read(&trace_record_cmdline_disabled
))
785 trace_save_cmdline(tsk
);
789 tracing_generic_entry_update(struct trace_entry
*entry
, unsigned long flags
,
792 struct task_struct
*tsk
= current
;
794 entry
->preempt_count
= pc
& 0xff;
795 entry
->pid
= (tsk
) ? tsk
->pid
: 0;
796 entry
->tgid
= (tsk
) ? tsk
->tgid
: 0;
798 #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
799 (irqs_disabled_flags(flags
) ? TRACE_FLAG_IRQS_OFF
: 0) |
801 TRACE_FLAG_IRQS_NOSUPPORT
|
803 ((pc
& HARDIRQ_MASK
) ? TRACE_FLAG_HARDIRQ
: 0) |
804 ((pc
& SOFTIRQ_MASK
) ? TRACE_FLAG_SOFTIRQ
: 0) |
805 (need_resched() ? TRACE_FLAG_NEED_RESCHED
: 0);
808 struct ring_buffer_event
*trace_buffer_lock_reserve(struct trace_array
*tr
,
811 unsigned long flags
, int pc
)
813 struct ring_buffer_event
*event
;
815 event
= ring_buffer_lock_reserve(tr
->buffer
, len
);
817 struct trace_entry
*ent
= ring_buffer_event_data(event
);
819 tracing_generic_entry_update(ent
, flags
, pc
);
825 static void ftrace_trace_stack(struct trace_array
*tr
,
826 unsigned long flags
, int skip
, int pc
);
827 static void ftrace_trace_userstack(struct trace_array
*tr
,
828 unsigned long flags
, int pc
);
830 void trace_buffer_unlock_commit(struct trace_array
*tr
,
831 struct ring_buffer_event
*event
,
832 unsigned long flags
, int pc
)
834 ring_buffer_unlock_commit(tr
->buffer
, event
);
836 ftrace_trace_stack(tr
, flags
, 6, pc
);
837 ftrace_trace_userstack(tr
, flags
, pc
);
842 trace_function(struct trace_array
*tr
,
843 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
846 struct ring_buffer_event
*event
;
847 struct ftrace_entry
*entry
;
849 /* If we are reading the ring buffer, don't trace */
850 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
853 event
= trace_buffer_lock_reserve(tr
, TRACE_FN
, sizeof(*entry
),
857 entry
= ring_buffer_event_data(event
);
859 entry
->parent_ip
= parent_ip
;
860 ring_buffer_unlock_commit(tr
->buffer
, event
);
863 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
864 static void __trace_graph_entry(struct trace_array
*tr
,
865 struct ftrace_graph_ent
*trace
,
869 struct ring_buffer_event
*event
;
870 struct ftrace_graph_ent_entry
*entry
;
872 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
875 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_ENT
,
876 sizeof(*entry
), flags
, pc
);
879 entry
= ring_buffer_event_data(event
);
880 entry
->graph_ent
= *trace
;
881 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
884 static void __trace_graph_return(struct trace_array
*tr
,
885 struct ftrace_graph_ret
*trace
,
889 struct ring_buffer_event
*event
;
890 struct ftrace_graph_ret_entry
*entry
;
892 if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled
))))
895 event
= trace_buffer_lock_reserve(&global_trace
, TRACE_GRAPH_RET
,
896 sizeof(*entry
), flags
, pc
);
899 entry
= ring_buffer_event_data(event
);
901 ring_buffer_unlock_commit(global_trace
.buffer
, event
);
906 ftrace(struct trace_array
*tr
, struct trace_array_cpu
*data
,
907 unsigned long ip
, unsigned long parent_ip
, unsigned long flags
,
910 if (likely(!atomic_read(&data
->disabled
)))
911 trace_function(tr
, ip
, parent_ip
, flags
, pc
);
914 static void __ftrace_trace_stack(struct trace_array
*tr
,
918 #ifdef CONFIG_STACKTRACE
919 struct ring_buffer_event
*event
;
920 struct stack_entry
*entry
;
921 struct stack_trace trace
;
923 event
= trace_buffer_lock_reserve(tr
, TRACE_STACK
,
924 sizeof(*entry
), flags
, pc
);
927 entry
= ring_buffer_event_data(event
);
928 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
930 trace
.nr_entries
= 0;
931 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
933 trace
.entries
= entry
->caller
;
935 save_stack_trace(&trace
);
936 ring_buffer_unlock_commit(tr
->buffer
, event
);
940 static void ftrace_trace_stack(struct trace_array
*tr
,
944 if (!(trace_flags
& TRACE_ITER_STACKTRACE
))
947 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
950 void __trace_stack(struct trace_array
*tr
,
954 __ftrace_trace_stack(tr
, flags
, skip
, pc
);
957 static void ftrace_trace_userstack(struct trace_array
*tr
,
958 unsigned long flags
, int pc
)
960 #ifdef CONFIG_STACKTRACE
961 struct ring_buffer_event
*event
;
962 struct userstack_entry
*entry
;
963 struct stack_trace trace
;
965 if (!(trace_flags
& TRACE_ITER_USERSTACKTRACE
))
968 event
= trace_buffer_lock_reserve(tr
, TRACE_USER_STACK
,
969 sizeof(*entry
), flags
, pc
);
972 entry
= ring_buffer_event_data(event
);
974 memset(&entry
->caller
, 0, sizeof(entry
->caller
));
976 trace
.nr_entries
= 0;
977 trace
.max_entries
= FTRACE_STACK_ENTRIES
;
979 trace
.entries
= entry
->caller
;
981 save_stack_trace_user(&trace
);
982 ring_buffer_unlock_commit(tr
->buffer
, event
);
986 void __trace_userstack(struct trace_array
*tr
, unsigned long flags
)
988 ftrace_trace_userstack(tr
, flags
, preempt_count());
992 ftrace_trace_special(void *__tr
,
993 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
,
996 struct ring_buffer_event
*event
;
997 struct trace_array
*tr
= __tr
;
998 struct special_entry
*entry
;
1000 event
= trace_buffer_lock_reserve(tr
, TRACE_SPECIAL
,
1001 sizeof(*entry
), 0, pc
);
1004 entry
= ring_buffer_event_data(event
);
1008 trace_buffer_unlock_commit(tr
, event
, 0, pc
);
1012 __trace_special(void *__tr
, void *__data
,
1013 unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1015 ftrace_trace_special(__tr
, arg1
, arg2
, arg3
, preempt_count());
1019 tracing_sched_switch_trace(struct trace_array
*tr
,
1020 struct task_struct
*prev
,
1021 struct task_struct
*next
,
1022 unsigned long flags
, int pc
)
1024 struct ring_buffer_event
*event
;
1025 struct ctx_switch_entry
*entry
;
1027 event
= trace_buffer_lock_reserve(tr
, TRACE_CTX
,
1028 sizeof(*entry
), flags
, pc
);
1031 entry
= ring_buffer_event_data(event
);
1032 entry
->prev_pid
= prev
->pid
;
1033 entry
->prev_prio
= prev
->prio
;
1034 entry
->prev_state
= prev
->state
;
1035 entry
->next_pid
= next
->pid
;
1036 entry
->next_prio
= next
->prio
;
1037 entry
->next_state
= next
->state
;
1038 entry
->next_cpu
= task_cpu(next
);
1039 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1043 tracing_sched_wakeup_trace(struct trace_array
*tr
,
1044 struct task_struct
*wakee
,
1045 struct task_struct
*curr
,
1046 unsigned long flags
, int pc
)
1048 struct ring_buffer_event
*event
;
1049 struct ctx_switch_entry
*entry
;
1051 event
= trace_buffer_lock_reserve(tr
, TRACE_WAKE
,
1052 sizeof(*entry
), flags
, pc
);
1055 entry
= ring_buffer_event_data(event
);
1056 entry
->prev_pid
= curr
->pid
;
1057 entry
->prev_prio
= curr
->prio
;
1058 entry
->prev_state
= curr
->state
;
1059 entry
->next_pid
= wakee
->pid
;
1060 entry
->next_prio
= wakee
->prio
;
1061 entry
->next_state
= wakee
->state
;
1062 entry
->next_cpu
= task_cpu(wakee
);
1063 trace_buffer_unlock_commit(tr
, event
, flags
, pc
);
1067 ftrace_special(unsigned long arg1
, unsigned long arg2
, unsigned long arg3
)
1069 struct trace_array
*tr
= &global_trace
;
1070 struct trace_array_cpu
*data
;
1071 unsigned long flags
;
1075 if (tracing_disabled
)
1078 pc
= preempt_count();
1079 local_irq_save(flags
);
1080 cpu
= raw_smp_processor_id();
1081 data
= tr
->data
[cpu
];
1083 if (likely(atomic_inc_return(&data
->disabled
) == 1))
1084 ftrace_trace_special(tr
, arg1
, arg2
, arg3
, pc
);
1086 atomic_dec(&data
->disabled
);
1087 local_irq_restore(flags
);
1090 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1091 int trace_graph_entry(struct ftrace_graph_ent
*trace
)
1093 struct trace_array
*tr
= &global_trace
;
1094 struct trace_array_cpu
*data
;
1095 unsigned long flags
;
1100 if (!ftrace_trace_task(current
))
1103 if (!ftrace_graph_addr(trace
->func
))
1106 local_irq_save(flags
);
1107 cpu
= raw_smp_processor_id();
1108 data
= tr
->data
[cpu
];
1109 disabled
= atomic_inc_return(&data
->disabled
);
1110 if (likely(disabled
== 1)) {
1111 pc
= preempt_count();
1112 __trace_graph_entry(tr
, trace
, flags
, pc
);
1114 /* Only do the atomic if it is not already set */
1115 if (!test_tsk_trace_graph(current
))
1116 set_tsk_trace_graph(current
);
1117 atomic_dec(&data
->disabled
);
1118 local_irq_restore(flags
);
1123 void trace_graph_return(struct ftrace_graph_ret
*trace
)
1125 struct trace_array
*tr
= &global_trace
;
1126 struct trace_array_cpu
*data
;
1127 unsigned long flags
;
1132 local_irq_save(flags
);
1133 cpu
= raw_smp_processor_id();
1134 data
= tr
->data
[cpu
];
1135 disabled
= atomic_inc_return(&data
->disabled
);
1136 if (likely(disabled
== 1)) {
1137 pc
= preempt_count();
1138 __trace_graph_return(tr
, trace
, flags
, pc
);
1141 clear_tsk_trace_graph(current
);
1142 atomic_dec(&data
->disabled
);
1143 local_irq_restore(flags
);
1145 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1147 enum trace_file_type
{
1148 TRACE_FILE_LAT_FMT
= 1,
1149 TRACE_FILE_ANNOTATE
= 2,
1152 static void trace_iterator_increment(struct trace_iterator
*iter
)
1154 /* Don't allow ftrace to trace into the ring buffers */
1155 ftrace_disable_cpu();
1158 if (iter
->buffer_iter
[iter
->cpu
])
1159 ring_buffer_read(iter
->buffer_iter
[iter
->cpu
], NULL
);
1161 ftrace_enable_cpu();
1164 static struct trace_entry
*
1165 peek_next_entry(struct trace_iterator
*iter
, int cpu
, u64
*ts
)
1167 struct ring_buffer_event
*event
;
1168 struct ring_buffer_iter
*buf_iter
= iter
->buffer_iter
[cpu
];
1170 /* Don't allow ftrace to trace into the ring buffers */
1171 ftrace_disable_cpu();
1174 event
= ring_buffer_iter_peek(buf_iter
, ts
);
1176 event
= ring_buffer_peek(iter
->tr
->buffer
, cpu
, ts
);
1178 ftrace_enable_cpu();
1180 return event
? ring_buffer_event_data(event
) : NULL
;
1183 static struct trace_entry
*
1184 __find_next_entry(struct trace_iterator
*iter
, int *ent_cpu
, u64
*ent_ts
)
1186 struct ring_buffer
*buffer
= iter
->tr
->buffer
;
1187 struct trace_entry
*ent
, *next
= NULL
;
1188 u64 next_ts
= 0, ts
;
1192 for_each_tracing_cpu(cpu
) {
1194 if (ring_buffer_empty_cpu(buffer
, cpu
))
1197 ent
= peek_next_entry(iter
, cpu
, &ts
);
1200 * Pick the entry with the smallest timestamp:
1202 if (ent
&& (!next
|| ts
< next_ts
)) {
1210 *ent_cpu
= next_cpu
;
1218 /* Find the next real entry, without updating the iterator itself */
1219 struct trace_entry
*trace_find_next_entry(struct trace_iterator
*iter
,
1220 int *ent_cpu
, u64
*ent_ts
)
1222 return __find_next_entry(iter
, ent_cpu
, ent_ts
);
1225 /* Find the next real entry, and increment the iterator to the next entry */
1226 static void *find_next_entry_inc(struct trace_iterator
*iter
)
1228 iter
->ent
= __find_next_entry(iter
, &iter
->cpu
, &iter
->ts
);
1231 trace_iterator_increment(iter
);
1233 return iter
->ent
? iter
: NULL
;
1236 static void trace_consume(struct trace_iterator
*iter
)
1238 /* Don't allow ftrace to trace into the ring buffers */
1239 ftrace_disable_cpu();
1240 ring_buffer_consume(iter
->tr
->buffer
, iter
->cpu
, &iter
->ts
);
1241 ftrace_enable_cpu();
1244 static void *s_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1246 struct trace_iterator
*iter
= m
->private;
1252 /* can't go backwards */
1257 ent
= find_next_entry_inc(iter
);
1261 while (ent
&& iter
->idx
< i
)
1262 ent
= find_next_entry_inc(iter
);
1269 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
1271 struct trace_iterator
*iter
= m
->private;
1276 mutex_lock(&trace_types_lock
);
1278 if (!current_trace
|| current_trace
!= iter
->trace
) {
1279 mutex_unlock(&trace_types_lock
);
1283 atomic_inc(&trace_record_cmdline_disabled
);
1285 if (*pos
!= iter
->pos
) {
1290 ftrace_disable_cpu();
1292 for_each_tracing_cpu(cpu
) {
1293 ring_buffer_iter_reset(iter
->buffer_iter
[cpu
]);
1296 ftrace_enable_cpu();
1298 for (p
= iter
; p
&& l
< *pos
; p
= s_next(m
, p
, &l
))
1303 p
= s_next(m
, p
, &l
);
1309 static void s_stop(struct seq_file
*m
, void *p
)
1311 atomic_dec(&trace_record_cmdline_disabled
);
1312 mutex_unlock(&trace_types_lock
);
1315 static void print_lat_help_header(struct seq_file
*m
)
1317 seq_puts(m
, "# _------=> CPU# \n");
1318 seq_puts(m
, "# / _-----=> irqs-off \n");
1319 seq_puts(m
, "# | / _----=> need-resched \n");
1320 seq_puts(m
, "# || / _---=> hardirq/softirq \n");
1321 seq_puts(m
, "# ||| / _--=> preempt-depth \n");
1322 seq_puts(m
, "# |||| / \n");
1323 seq_puts(m
, "# ||||| delay \n");
1324 seq_puts(m
, "# cmd pid ||||| time | caller \n");
1325 seq_puts(m
, "# \\ / ||||| \\ | / \n");
1328 static void print_func_help_header(struct seq_file
*m
)
1330 seq_puts(m
, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
1331 seq_puts(m
, "# | | | | |\n");
1336 print_trace_header(struct seq_file
*m
, struct trace_iterator
*iter
)
1338 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1339 struct trace_array
*tr
= iter
->tr
;
1340 struct trace_array_cpu
*data
= tr
->data
[tr
->cpu
];
1341 struct tracer
*type
= current_trace
;
1342 unsigned long total
;
1343 unsigned long entries
;
1344 const char *name
= "preemption";
1349 entries
= ring_buffer_entries(iter
->tr
->buffer
);
1351 ring_buffer_overruns(iter
->tr
->buffer
);
1353 seq_printf(m
, "%s latency trace v1.1.5 on %s\n",
1355 seq_puts(m
, "-----------------------------------"
1356 "---------------------------------\n");
1357 seq_printf(m
, " latency: %lu us, #%lu/%lu, CPU#%d |"
1358 " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
1359 nsecs_to_usecs(data
->saved_latency
),
1363 #if defined(CONFIG_PREEMPT_NONE)
1365 #elif defined(CONFIG_PREEMPT_VOLUNTARY)
1367 #elif defined(CONFIG_PREEMPT)
1372 /* These are reserved for later use */
1375 seq_printf(m
, " #P:%d)\n", num_online_cpus());
1379 seq_puts(m
, " -----------------\n");
1380 seq_printf(m
, " | task: %.16s-%d "
1381 "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
1382 data
->comm
, data
->pid
, data
->uid
, data
->nice
,
1383 data
->policy
, data
->rt_priority
);
1384 seq_puts(m
, " -----------------\n");
1386 if (data
->critical_start
) {
1387 seq_puts(m
, " => started at: ");
1388 seq_print_ip_sym(&iter
->seq
, data
->critical_start
, sym_flags
);
1389 trace_print_seq(m
, &iter
->seq
);
1390 seq_puts(m
, "\n => ended at: ");
1391 seq_print_ip_sym(&iter
->seq
, data
->critical_end
, sym_flags
);
1392 trace_print_seq(m
, &iter
->seq
);
1399 static void test_cpu_buff_start(struct trace_iterator
*iter
)
1401 struct trace_seq
*s
= &iter
->seq
;
1403 if (!(trace_flags
& TRACE_ITER_ANNOTATE
))
1406 if (!(iter
->iter_flags
& TRACE_FILE_ANNOTATE
))
1409 if (cpumask_test_cpu(iter
->cpu
, iter
->started
))
1412 cpumask_set_cpu(iter
->cpu
, iter
->started
);
1413 trace_seq_printf(s
, "##### CPU %u buffer started ####\n", iter
->cpu
);
1416 static enum print_line_t
print_lat_fmt(struct trace_iterator
*iter
)
1418 struct trace_seq
*s
= &iter
->seq
;
1419 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1420 struct trace_event
*event
;
1421 struct trace_entry
*entry
= iter
->ent
;
1423 test_cpu_buff_start(iter
);
1425 event
= ftrace_find_event(entry
->type
);
1427 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1428 if (!trace_print_lat_context(iter
))
1433 return event
->latency_trace(iter
, sym_flags
);
1435 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1438 return TRACE_TYPE_HANDLED
;
1440 return TRACE_TYPE_PARTIAL_LINE
;
1443 static enum print_line_t
print_trace_fmt(struct trace_iterator
*iter
)
1445 struct trace_seq
*s
= &iter
->seq
;
1446 unsigned long sym_flags
= (trace_flags
& TRACE_ITER_SYM_MASK
);
1447 struct trace_entry
*entry
;
1448 struct trace_event
*event
;
1452 test_cpu_buff_start(iter
);
1454 event
= ftrace_find_event(entry
->type
);
1456 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1457 if (!trace_print_context(iter
))
1462 return event
->trace(iter
, sym_flags
);
1464 if (!trace_seq_printf(s
, "Unknown type %d\n", entry
->type
))
1467 return TRACE_TYPE_HANDLED
;
1469 return TRACE_TYPE_PARTIAL_LINE
;
1472 static enum print_line_t
print_raw_fmt(struct trace_iterator
*iter
)
1474 struct trace_seq
*s
= &iter
->seq
;
1475 struct trace_entry
*entry
;
1476 struct trace_event
*event
;
1480 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1481 if (!trace_seq_printf(s
, "%d %d %llu ",
1482 entry
->pid
, iter
->cpu
, iter
->ts
))
1486 event
= ftrace_find_event(entry
->type
);
1488 return event
->raw(iter
, 0);
1490 if (!trace_seq_printf(s
, "%d ?\n", entry
->type
))
1493 return TRACE_TYPE_HANDLED
;
1495 return TRACE_TYPE_PARTIAL_LINE
;
1498 static enum print_line_t
print_hex_fmt(struct trace_iterator
*iter
)
1500 struct trace_seq
*s
= &iter
->seq
;
1501 unsigned char newline
= '\n';
1502 struct trace_entry
*entry
;
1503 struct trace_event
*event
;
1507 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1508 SEQ_PUT_HEX_FIELD_RET(s
, entry
->pid
);
1509 SEQ_PUT_HEX_FIELD_RET(s
, iter
->cpu
);
1510 SEQ_PUT_HEX_FIELD_RET(s
, iter
->ts
);
1513 event
= ftrace_find_event(entry
->type
);
1515 enum print_line_t ret
= event
->hex(iter
, 0);
1516 if (ret
!= TRACE_TYPE_HANDLED
)
1520 SEQ_PUT_FIELD_RET(s
, newline
);
1522 return TRACE_TYPE_HANDLED
;
1525 static enum print_line_t
print_printk_msg_only(struct trace_iterator
*iter
)
1527 struct trace_seq
*s
= &iter
->seq
;
1528 struct trace_entry
*entry
= iter
->ent
;
1529 struct print_entry
*field
;
1532 trace_assign_type(field
, entry
);
1534 ret
= trace_seq_printf(s
, "%s", field
->buf
);
1536 return TRACE_TYPE_PARTIAL_LINE
;
1538 return TRACE_TYPE_HANDLED
;
1541 static enum print_line_t
print_bin_fmt(struct trace_iterator
*iter
)
1543 struct trace_seq
*s
= &iter
->seq
;
1544 struct trace_entry
*entry
;
1545 struct trace_event
*event
;
1549 if (trace_flags
& TRACE_ITER_CONTEXT_INFO
) {
1550 SEQ_PUT_FIELD_RET(s
, entry
->pid
);
1551 SEQ_PUT_FIELD_RET(s
, iter
->cpu
);
1552 SEQ_PUT_FIELD_RET(s
, iter
->ts
);
1555 event
= ftrace_find_event(entry
->type
);
1556 return event
? event
->binary(iter
, 0) : TRACE_TYPE_HANDLED
;
1559 static int trace_empty(struct trace_iterator
*iter
)
1563 for_each_tracing_cpu(cpu
) {
1564 if (iter
->buffer_iter
[cpu
]) {
1565 if (!ring_buffer_iter_empty(iter
->buffer_iter
[cpu
]))
1568 if (!ring_buffer_empty_cpu(iter
->tr
->buffer
, cpu
))
1576 static enum print_line_t
print_trace_line(struct trace_iterator
*iter
)
1578 enum print_line_t ret
;
1580 if (iter
->trace
&& iter
->trace
->print_line
) {
1581 ret
= iter
->trace
->print_line(iter
);
1582 if (ret
!= TRACE_TYPE_UNHANDLED
)
1586 if (iter
->ent
->type
== TRACE_PRINT
&&
1587 trace_flags
& TRACE_ITER_PRINTK
&&
1588 trace_flags
& TRACE_ITER_PRINTK_MSGONLY
)
1589 return print_printk_msg_only(iter
);
1591 if (trace_flags
& TRACE_ITER_BIN
)
1592 return print_bin_fmt(iter
);
1594 if (trace_flags
& TRACE_ITER_HEX
)
1595 return print_hex_fmt(iter
);
1597 if (trace_flags
& TRACE_ITER_RAW
)
1598 return print_raw_fmt(iter
);
1600 if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
)
1601 return print_lat_fmt(iter
);
1603 return print_trace_fmt(iter
);
1606 static int s_show(struct seq_file
*m
, void *v
)
1608 struct trace_iterator
*iter
= v
;
1610 if (iter
->ent
== NULL
) {
1612 seq_printf(m
, "# tracer: %s\n", iter
->trace
->name
);
1615 if (iter
->trace
&& iter
->trace
->print_header
)
1616 iter
->trace
->print_header(m
);
1617 else if (iter
->iter_flags
& TRACE_FILE_LAT_FMT
) {
1618 /* print nothing if the buffers are empty */
1619 if (trace_empty(iter
))
1621 print_trace_header(m
, iter
);
1622 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1623 print_lat_help_header(m
);
1625 if (!(trace_flags
& TRACE_ITER_VERBOSE
))
1626 print_func_help_header(m
);
1629 print_trace_line(iter
);
1630 trace_print_seq(m
, &iter
->seq
);
1636 static struct seq_operations tracer_seq_ops
= {
1643 static struct trace_iterator
*
1644 __tracing_open(struct inode
*inode
, struct file
*file
, int *ret
)
1646 struct trace_iterator
*iter
;
1650 if (tracing_disabled
) {
1655 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
1661 mutex_lock(&trace_types_lock
);
1662 if (current_trace
&& current_trace
->print_max
)
1665 iter
->tr
= inode
->i_private
;
1666 iter
->trace
= current_trace
;
1669 /* Notify the tracer early; before we stop tracing. */
1670 if (iter
->trace
&& iter
->trace
->open
)
1671 iter
->trace
->open(iter
);
1673 /* Annotate start of buffers if we had overruns */
1674 if (ring_buffer_overruns(iter
->tr
->buffer
))
1675 iter
->iter_flags
|= TRACE_FILE_ANNOTATE
;
1678 for_each_tracing_cpu(cpu
) {
1680 iter
->buffer_iter
[cpu
] =
1681 ring_buffer_read_start(iter
->tr
->buffer
, cpu
);
1683 if (!iter
->buffer_iter
[cpu
])
1687 /* TODO stop tracer */
1688 *ret
= seq_open(file
, &tracer_seq_ops
);
1692 m
= file
->private_data
;
1695 /* stop the trace while dumping */
1698 mutex_unlock(&trace_types_lock
);
1704 for_each_tracing_cpu(cpu
) {
1705 if (iter
->buffer_iter
[cpu
])
1706 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1708 mutex_unlock(&trace_types_lock
);
1711 return ERR_PTR(-ENOMEM
);
1714 int tracing_open_generic(struct inode
*inode
, struct file
*filp
)
1716 if (tracing_disabled
)
1719 filp
->private_data
= inode
->i_private
;
1723 int tracing_release(struct inode
*inode
, struct file
*file
)
1725 struct seq_file
*m
= (struct seq_file
*)file
->private_data
;
1726 struct trace_iterator
*iter
= m
->private;
1729 mutex_lock(&trace_types_lock
);
1730 for_each_tracing_cpu(cpu
) {
1731 if (iter
->buffer_iter
[cpu
])
1732 ring_buffer_read_finish(iter
->buffer_iter
[cpu
]);
1735 if (iter
->trace
&& iter
->trace
->close
)
1736 iter
->trace
->close(iter
);
1738 /* reenable tracing if it was previously enabled */
1740 mutex_unlock(&trace_types_lock
);
1742 seq_release(inode
, file
);
1747 static int tracing_open(struct inode
*inode
, struct file
*file
)
1751 __tracing_open(inode
, file
, &ret
);
1756 static int tracing_lt_open(struct inode
*inode
, struct file
*file
)
1758 struct trace_iterator
*iter
;
1761 iter
= __tracing_open(inode
, file
, &ret
);
1764 iter
->iter_flags
|= TRACE_FILE_LAT_FMT
;
1771 t_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1773 struct tracer
*t
= m
->private;
1785 static void *t_start(struct seq_file
*m
, loff_t
*pos
)
1787 struct tracer
*t
= m
->private;
1790 mutex_lock(&trace_types_lock
);
1791 for (; t
&& l
< *pos
; t
= t_next(m
, t
, &l
))
1797 static void t_stop(struct seq_file
*m
, void *p
)
1799 mutex_unlock(&trace_types_lock
);
1802 static int t_show(struct seq_file
*m
, void *v
)
1804 struct tracer
*t
= v
;
1809 seq_printf(m
, "%s", t
->name
);
1818 static struct seq_operations show_traces_seq_ops
= {
1825 static int show_traces_open(struct inode
*inode
, struct file
*file
)
1829 if (tracing_disabled
)
1832 ret
= seq_open(file
, &show_traces_seq_ops
);
1834 struct seq_file
*m
= file
->private_data
;
1835 m
->private = trace_types
;
1841 static struct file_operations tracing_fops
= {
1842 .open
= tracing_open
,
1844 .llseek
= seq_lseek
,
1845 .release
= tracing_release
,
1848 static struct file_operations tracing_lt_fops
= {
1849 .open
= tracing_lt_open
,
1851 .llseek
= seq_lseek
,
1852 .release
= tracing_release
,
1855 static struct file_operations show_traces_fops
= {
1856 .open
= show_traces_open
,
1858 .release
= seq_release
,
1862 * Only trace on a CPU if the bitmask is set:
1864 static cpumask_var_t tracing_cpumask
;
1867 * The tracer itself will not take this lock, but still we want
1868 * to provide a consistent cpumask to user-space:
1870 static DEFINE_MUTEX(tracing_cpumask_update_lock
);
1873 * Temporary storage for the character representation of the
1874 * CPU bitmask (and one more byte for the newline):
1876 static char mask_str
[NR_CPUS
+ 1];
1879 tracing_cpumask_read(struct file
*filp
, char __user
*ubuf
,
1880 size_t count
, loff_t
*ppos
)
1884 mutex_lock(&tracing_cpumask_update_lock
);
1886 len
= cpumask_scnprintf(mask_str
, count
, tracing_cpumask
);
1887 if (count
- len
< 2) {
1891 len
+= sprintf(mask_str
+ len
, "\n");
1892 count
= simple_read_from_buffer(ubuf
, count
, ppos
, mask_str
, NR_CPUS
+1);
1895 mutex_unlock(&tracing_cpumask_update_lock
);
1901 tracing_cpumask_write(struct file
*filp
, const char __user
*ubuf
,
1902 size_t count
, loff_t
*ppos
)
1905 cpumask_var_t tracing_cpumask_new
;
1907 if (!alloc_cpumask_var(&tracing_cpumask_new
, GFP_KERNEL
))
1910 mutex_lock(&tracing_cpumask_update_lock
);
1911 err
= cpumask_parse_user(ubuf
, count
, tracing_cpumask_new
);
1915 local_irq_disable();
1916 __raw_spin_lock(&ftrace_max_lock
);
1917 for_each_tracing_cpu(cpu
) {
1919 * Increase/decrease the disabled counter if we are
1920 * about to flip a bit in the cpumask:
1922 if (cpumask_test_cpu(cpu
, tracing_cpumask
) &&
1923 !cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
1924 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
1926 if (!cpumask_test_cpu(cpu
, tracing_cpumask
) &&
1927 cpumask_test_cpu(cpu
, tracing_cpumask_new
)) {
1928 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
1931 __raw_spin_unlock(&ftrace_max_lock
);
1934 cpumask_copy(tracing_cpumask
, tracing_cpumask_new
);
1936 mutex_unlock(&tracing_cpumask_update_lock
);
1937 free_cpumask_var(tracing_cpumask_new
);
1942 mutex_unlock(&tracing_cpumask_update_lock
);
1943 free_cpumask_var(tracing_cpumask
);
1948 static struct file_operations tracing_cpumask_fops
= {
1949 .open
= tracing_open_generic
,
1950 .read
= tracing_cpumask_read
,
1951 .write
= tracing_cpumask_write
,
1955 tracing_trace_options_read(struct file
*filp
, char __user
*ubuf
,
1956 size_t cnt
, loff_t
*ppos
)
1962 u32 tracer_flags
= current_trace
->flags
->val
;
1963 struct tracer_opt
*trace_opts
= current_trace
->flags
->opts
;
1966 /* calulate max size */
1967 for (i
= 0; trace_options
[i
]; i
++) {
1968 len
+= strlen(trace_options
[i
]);
1969 len
+= 3; /* "no" and space */
1973 * Increase the size with names of options specific
1974 * of the current tracer.
1976 for (i
= 0; trace_opts
[i
].name
; i
++) {
1977 len
+= strlen(trace_opts
[i
].name
);
1978 len
+= 3; /* "no" and space */
1981 /* +2 for \n and \0 */
1982 buf
= kmalloc(len
+ 2, GFP_KERNEL
);
1986 for (i
= 0; trace_options
[i
]; i
++) {
1987 if (trace_flags
& (1 << i
))
1988 r
+= sprintf(buf
+ r
, "%s ", trace_options
[i
]);
1990 r
+= sprintf(buf
+ r
, "no%s ", trace_options
[i
]);
1993 for (i
= 0; trace_opts
[i
].name
; i
++) {
1994 if (tracer_flags
& trace_opts
[i
].bit
)
1995 r
+= sprintf(buf
+ r
, "%s ",
1996 trace_opts
[i
].name
);
1998 r
+= sprintf(buf
+ r
, "no%s ",
1999 trace_opts
[i
].name
);
2002 r
+= sprintf(buf
+ r
, "\n");
2003 WARN_ON(r
>= len
+ 2);
2005 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2012 /* Try to assign a tracer specific option */
2013 static int set_tracer_option(struct tracer
*trace
, char *cmp
, int neg
)
2015 struct tracer_flags
*trace_flags
= trace
->flags
;
2016 struct tracer_opt
*opts
= NULL
;
2020 for (i
= 0; trace_flags
->opts
[i
].name
; i
++) {
2021 opts
= &trace_flags
->opts
[i
];
2022 len
= strlen(opts
->name
);
2024 if (strncmp(cmp
, opts
->name
, len
) == 0) {
2025 ret
= trace
->set_flag(trace_flags
->val
,
2031 if (!trace_flags
->opts
[i
].name
)
2034 /* Refused to handle */
2039 trace_flags
->val
&= ~opts
->bit
;
2041 trace_flags
->val
|= opts
->bit
;
2047 tracing_trace_options_write(struct file
*filp
, const char __user
*ubuf
,
2048 size_t cnt
, loff_t
*ppos
)
2056 if (cnt
>= sizeof(buf
))
2059 if (copy_from_user(&buf
, ubuf
, cnt
))
2064 if (strncmp(buf
, "no", 2) == 0) {
2069 for (i
= 0; trace_options
[i
]; i
++) {
2070 int len
= strlen(trace_options
[i
]);
2072 if (strncmp(cmp
, trace_options
[i
], len
) == 0) {
2074 trace_flags
&= ~(1 << i
);
2076 trace_flags
|= (1 << i
);
2081 /* If no option could be set, test the specific tracer options */
2082 if (!trace_options
[i
]) {
2083 ret
= set_tracer_option(current_trace
, cmp
, neg
);
2093 static struct file_operations tracing_iter_fops
= {
2094 .open
= tracing_open_generic
,
2095 .read
= tracing_trace_options_read
,
2096 .write
= tracing_trace_options_write
,
2099 static const char readme_msg
[] =
2100 "tracing mini-HOWTO:\n\n"
2102 "# mount -t debugfs nodev /debug\n\n"
2103 "# cat /debug/tracing/available_tracers\n"
2104 "wakeup preemptirqsoff preemptoff irqsoff ftrace sched_switch none\n\n"
2105 "# cat /debug/tracing/current_tracer\n"
2107 "# echo sched_switch > /debug/tracing/current_tracer\n"
2108 "# cat /debug/tracing/current_tracer\n"
2110 "# cat /debug/tracing/trace_options\n"
2111 "noprint-parent nosym-offset nosym-addr noverbose\n"
2112 "# echo print-parent > /debug/tracing/trace_options\n"
2113 "# echo 1 > /debug/tracing/tracing_enabled\n"
2114 "# cat /debug/tracing/trace > /tmp/trace.txt\n"
2115 "echo 0 > /debug/tracing/tracing_enabled\n"
2119 tracing_readme_read(struct file
*filp
, char __user
*ubuf
,
2120 size_t cnt
, loff_t
*ppos
)
2122 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2123 readme_msg
, strlen(readme_msg
));
2126 static struct file_operations tracing_readme_fops
= {
2127 .open
= tracing_open_generic
,
2128 .read
= tracing_readme_read
,
2132 tracing_ctrl_read(struct file
*filp
, char __user
*ubuf
,
2133 size_t cnt
, loff_t
*ppos
)
2138 r
= sprintf(buf
, "%u\n", tracer_enabled
);
2139 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2143 tracing_ctrl_write(struct file
*filp
, const char __user
*ubuf
,
2144 size_t cnt
, loff_t
*ppos
)
2146 struct trace_array
*tr
= filp
->private_data
;
2151 if (cnt
>= sizeof(buf
))
2154 if (copy_from_user(&buf
, ubuf
, cnt
))
2159 ret
= strict_strtoul(buf
, 10, &val
);
2165 mutex_lock(&trace_types_lock
);
2166 if (tracer_enabled
^ val
) {
2169 if (current_trace
->start
)
2170 current_trace
->start(tr
);
2175 if (current_trace
->stop
)
2176 current_trace
->stop(tr
);
2179 mutex_unlock(&trace_types_lock
);
2187 tracing_set_trace_read(struct file
*filp
, char __user
*ubuf
,
2188 size_t cnt
, loff_t
*ppos
)
2190 char buf
[max_tracer_type_len
+2];
2193 mutex_lock(&trace_types_lock
);
2195 r
= sprintf(buf
, "%s\n", current_trace
->name
);
2197 r
= sprintf(buf
, "\n");
2198 mutex_unlock(&trace_types_lock
);
2200 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2203 int tracer_init(struct tracer
*t
, struct trace_array
*tr
)
2205 tracing_reset_online_cpus(tr
);
2209 static int tracing_set_tracer(const char *buf
)
2211 struct trace_array
*tr
= &global_trace
;
2215 mutex_lock(&trace_types_lock
);
2216 for (t
= trace_types
; t
; t
= t
->next
) {
2217 if (strcmp(t
->name
, buf
) == 0)
2224 if (t
== current_trace
)
2227 trace_branch_disable();
2228 if (current_trace
&& current_trace
->reset
)
2229 current_trace
->reset(tr
);
2233 ret
= tracer_init(t
, tr
);
2238 trace_branch_enable(tr
);
2240 mutex_unlock(&trace_types_lock
);
2246 tracing_set_trace_write(struct file
*filp
, const char __user
*ubuf
,
2247 size_t cnt
, loff_t
*ppos
)
2249 char buf
[max_tracer_type_len
+1];
2256 if (cnt
> max_tracer_type_len
)
2257 cnt
= max_tracer_type_len
;
2259 if (copy_from_user(&buf
, ubuf
, cnt
))
2264 /* strip ending whitespace. */
2265 for (i
= cnt
- 1; i
> 0 && isspace(buf
[i
]); i
--)
2268 err
= tracing_set_tracer(buf
);
2278 tracing_max_lat_read(struct file
*filp
, char __user
*ubuf
,
2279 size_t cnt
, loff_t
*ppos
)
2281 unsigned long *ptr
= filp
->private_data
;
2285 r
= snprintf(buf
, sizeof(buf
), "%ld\n",
2286 *ptr
== (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr
));
2287 if (r
> sizeof(buf
))
2289 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2293 tracing_max_lat_write(struct file
*filp
, const char __user
*ubuf
,
2294 size_t cnt
, loff_t
*ppos
)
2296 long *ptr
= filp
->private_data
;
2301 if (cnt
>= sizeof(buf
))
2304 if (copy_from_user(&buf
, ubuf
, cnt
))
2309 ret
= strict_strtoul(buf
, 10, &val
);
2318 static atomic_t tracing_reader
;
2320 static int tracing_open_pipe(struct inode
*inode
, struct file
*filp
)
2322 struct trace_iterator
*iter
;
2324 if (tracing_disabled
)
2327 /* We only allow for reader of the pipe */
2328 if (atomic_inc_return(&tracing_reader
) != 1) {
2329 atomic_dec(&tracing_reader
);
2333 /* create a buffer to store the information to pass to userspace */
2334 iter
= kzalloc(sizeof(*iter
), GFP_KERNEL
);
2338 if (!alloc_cpumask_var(&iter
->started
, GFP_KERNEL
)) {
2343 mutex_lock(&trace_types_lock
);
2345 /* trace pipe does not show start of buffer */
2346 cpumask_setall(iter
->started
);
2348 iter
->tr
= &global_trace
;
2349 iter
->trace
= current_trace
;
2350 filp
->private_data
= iter
;
2352 if (iter
->trace
->pipe_open
)
2353 iter
->trace
->pipe_open(iter
);
2354 mutex_unlock(&trace_types_lock
);
2359 static int tracing_release_pipe(struct inode
*inode
, struct file
*file
)
2361 struct trace_iterator
*iter
= file
->private_data
;
2363 free_cpumask_var(iter
->started
);
2365 atomic_dec(&tracing_reader
);
2371 tracing_poll_pipe(struct file
*filp
, poll_table
*poll_table
)
2373 struct trace_iterator
*iter
= filp
->private_data
;
2375 if (trace_flags
& TRACE_ITER_BLOCK
) {
2377 * Always select as readable when in blocking mode
2379 return POLLIN
| POLLRDNORM
;
2381 if (!trace_empty(iter
))
2382 return POLLIN
| POLLRDNORM
;
2383 poll_wait(filp
, &trace_wait
, poll_table
);
2384 if (!trace_empty(iter
))
2385 return POLLIN
| POLLRDNORM
;
2395 tracing_read_pipe(struct file
*filp
, char __user
*ubuf
,
2396 size_t cnt
, loff_t
*ppos
)
2398 struct trace_iterator
*iter
= filp
->private_data
;
2401 /* return any leftover data */
2402 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2406 trace_seq_reset(&iter
->seq
);
2408 mutex_lock(&trace_types_lock
);
2409 if (iter
->trace
->read
) {
2410 sret
= iter
->trace
->read(iter
, filp
, ubuf
, cnt
, ppos
);
2417 while (trace_empty(iter
)) {
2419 if ((filp
->f_flags
& O_NONBLOCK
)) {
2425 * This is a make-shift waitqueue. The reason we don't use
2426 * an actual wait queue is because:
2427 * 1) we only ever have one waiter
2428 * 2) the tracing, traces all functions, we don't want
2429 * the overhead of calling wake_up and friends
2430 * (and tracing them too)
2431 * Anyway, this is really very primitive wakeup.
2433 set_current_state(TASK_INTERRUPTIBLE
);
2434 iter
->tr
->waiter
= current
;
2436 mutex_unlock(&trace_types_lock
);
2438 /* sleep for 100 msecs, and try again. */
2439 schedule_timeout(HZ
/10);
2441 mutex_lock(&trace_types_lock
);
2443 iter
->tr
->waiter
= NULL
;
2445 if (signal_pending(current
)) {
2450 if (iter
->trace
!= current_trace
)
2454 * We block until we read something and tracing is disabled.
2455 * We still block if tracing is disabled, but we have never
2456 * read anything. This allows a user to cat this file, and
2457 * then enable tracing. But after we have read something,
2458 * we give an EOF when tracing is again disabled.
2460 * iter->pos will be 0 if we haven't read anything.
2462 if (!tracer_enabled
&& iter
->pos
)
2468 /* stop when tracing is finished */
2469 if (trace_empty(iter
))
2472 if (cnt
>= PAGE_SIZE
)
2473 cnt
= PAGE_SIZE
- 1;
2475 /* reset all but tr, trace, and overruns */
2476 memset(&iter
->seq
, 0,
2477 sizeof(struct trace_iterator
) -
2478 offsetof(struct trace_iterator
, seq
));
2481 while (find_next_entry_inc(iter
) != NULL
) {
2482 enum print_line_t ret
;
2483 int len
= iter
->seq
.len
;
2485 ret
= print_trace_line(iter
);
2486 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2487 /* don't print partial lines */
2488 iter
->seq
.len
= len
;
2491 if (ret
!= TRACE_TYPE_NO_CONSUME
)
2492 trace_consume(iter
);
2494 if (iter
->seq
.len
>= cnt
)
2498 /* Now copy what we have to the user */
2499 sret
= trace_seq_to_user(&iter
->seq
, ubuf
, cnt
);
2500 if (iter
->seq
.readpos
>= iter
->seq
.len
)
2501 trace_seq_reset(&iter
->seq
);
2504 * If there was nothing to send to user, inspite of consuming trace
2505 * entries, go back to wait for more entries.
2511 mutex_unlock(&trace_types_lock
);
2516 static void tracing_pipe_buf_release(struct pipe_inode_info
*pipe
,
2517 struct pipe_buffer
*buf
)
2519 __free_page(buf
->page
);
2522 static void tracing_spd_release_pipe(struct splice_pipe_desc
*spd
,
2525 __free_page(spd
->pages
[idx
]);
2528 static struct pipe_buf_operations tracing_pipe_buf_ops
= {
2530 .map
= generic_pipe_buf_map
,
2531 .unmap
= generic_pipe_buf_unmap
,
2532 .confirm
= generic_pipe_buf_confirm
,
2533 .release
= tracing_pipe_buf_release
,
2534 .steal
= generic_pipe_buf_steal
,
2535 .get
= generic_pipe_buf_get
,
2538 static ssize_t
tracing_splice_read_pipe(struct file
*filp
,
2540 struct pipe_inode_info
*pipe
,
2544 struct page
*pages
[PIPE_BUFFERS
];
2545 struct partial_page partial
[PIPE_BUFFERS
];
2546 struct trace_iterator
*iter
= filp
->private_data
;
2547 struct splice_pipe_desc spd
= {
2550 .nr_pages
= 0, /* This gets updated below. */
2552 .ops
= &tracing_pipe_buf_ops
,
2553 .spd_release
= tracing_spd_release_pipe
,
2559 mutex_lock(&trace_types_lock
);
2561 if (iter
->trace
->splice_read
) {
2562 ret
= iter
->trace
->splice_read(iter
, filp
,
2563 ppos
, pipe
, len
, flags
);
2568 ret
= tracing_wait_pipe(filp
);
2572 if (!iter
->ent
&& !find_next_entry_inc(iter
)) {
2577 /* Fill as many pages as possible. */
2578 for (i
= 0, rem
= len
; i
< PIPE_BUFFERS
&& rem
; i
++) {
2579 pages
[i
] = alloc_page(GFP_KERNEL
);
2581 /* Seq buffer is page-sized, exactly what we need. */
2583 count
= iter
->seq
.len
;
2584 ret
= print_trace_line(iter
);
2585 count
= iter
->seq
.len
- count
;
2588 iter
->seq
.len
-= count
;
2591 if (ret
== TRACE_TYPE_PARTIAL_LINE
) {
2592 iter
->seq
.len
-= count
;
2596 trace_consume(iter
);
2598 if (!find_next_entry_inc(iter
)) {
2605 /* Copy the data into the page, so we can start over. */
2606 ret
= trace_seq_to_buffer(&iter
->seq
,
2607 page_address(pages
[i
]),
2610 __free_page(pages
[i
]);
2613 partial
[i
].offset
= 0;
2614 partial
[i
].len
= iter
->seq
.len
;
2616 trace_seq_reset(&iter
->seq
);
2619 mutex_unlock(&trace_types_lock
);
2623 return splice_to_pipe(pipe
, &spd
);
2626 mutex_unlock(&trace_types_lock
);
2632 tracing_entries_read(struct file
*filp
, char __user
*ubuf
,
2633 size_t cnt
, loff_t
*ppos
)
2635 struct trace_array
*tr
= filp
->private_data
;
2639 r
= sprintf(buf
, "%lu\n", tr
->entries
>> 10);
2640 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2644 tracing_entries_write(struct file
*filp
, const char __user
*ubuf
,
2645 size_t cnt
, loff_t
*ppos
)
2651 if (cnt
>= sizeof(buf
))
2654 if (copy_from_user(&buf
, ubuf
, cnt
))
2659 ret
= strict_strtoul(buf
, 10, &val
);
2663 /* must have at least 1 entry */
2667 mutex_lock(&trace_types_lock
);
2671 /* disable all cpu buffers */
2672 for_each_tracing_cpu(cpu
) {
2673 if (global_trace
.data
[cpu
])
2674 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
2675 if (max_tr
.data
[cpu
])
2676 atomic_inc(&max_tr
.data
[cpu
]->disabled
);
2679 /* value is in KB */
2682 if (val
!= global_trace
.entries
) {
2683 ret
= ring_buffer_resize(global_trace
.buffer
, val
);
2689 ret
= ring_buffer_resize(max_tr
.buffer
, val
);
2693 r
= ring_buffer_resize(global_trace
.buffer
,
2694 global_trace
.entries
);
2696 /* AARGH! We are left with different
2697 * size max buffer!!!! */
2699 tracing_disabled
= 1;
2704 global_trace
.entries
= val
;
2709 /* If check pages failed, return ENOMEM */
2710 if (tracing_disabled
)
2713 for_each_tracing_cpu(cpu
) {
2714 if (global_trace
.data
[cpu
])
2715 atomic_dec(&global_trace
.data
[cpu
]->disabled
);
2716 if (max_tr
.data
[cpu
])
2717 atomic_dec(&max_tr
.data
[cpu
]->disabled
);
2721 max_tr
.entries
= global_trace
.entries
;
2722 mutex_unlock(&trace_types_lock
);
2727 static int mark_printk(const char *fmt
, ...)
2731 va_start(args
, fmt
);
2732 ret
= trace_vprintk(0, -1, fmt
, args
);
2738 tracing_mark_write(struct file
*filp
, const char __user
*ubuf
,
2739 size_t cnt
, loff_t
*fpos
)
2744 if (tracing_disabled
)
2747 if (cnt
> TRACE_BUF_SIZE
)
2748 cnt
= TRACE_BUF_SIZE
;
2750 buf
= kmalloc(cnt
+ 1, GFP_KERNEL
);
2754 if (copy_from_user(buf
, ubuf
, cnt
)) {
2759 /* Cut from the first nil or newline. */
2761 end
= strchr(buf
, '\n');
2765 cnt
= mark_printk("%s\n", buf
);
2772 static struct file_operations tracing_max_lat_fops
= {
2773 .open
= tracing_open_generic
,
2774 .read
= tracing_max_lat_read
,
2775 .write
= tracing_max_lat_write
,
2778 static struct file_operations tracing_ctrl_fops
= {
2779 .open
= tracing_open_generic
,
2780 .read
= tracing_ctrl_read
,
2781 .write
= tracing_ctrl_write
,
2784 static struct file_operations set_tracer_fops
= {
2785 .open
= tracing_open_generic
,
2786 .read
= tracing_set_trace_read
,
2787 .write
= tracing_set_trace_write
,
2790 static struct file_operations tracing_pipe_fops
= {
2791 .open
= tracing_open_pipe
,
2792 .poll
= tracing_poll_pipe
,
2793 .read
= tracing_read_pipe
,
2794 .splice_read
= tracing_splice_read_pipe
,
2795 .release
= tracing_release_pipe
,
2798 static struct file_operations tracing_entries_fops
= {
2799 .open
= tracing_open_generic
,
2800 .read
= tracing_entries_read
,
2801 .write
= tracing_entries_write
,
2804 static struct file_operations tracing_mark_fops
= {
2805 .open
= tracing_open_generic
,
2806 .write
= tracing_mark_write
,
2809 #ifdef CONFIG_DYNAMIC_FTRACE
2811 int __weak
ftrace_arch_read_dyn_info(char *buf
, int size
)
2817 tracing_read_dyn_info(struct file
*filp
, char __user
*ubuf
,
2818 size_t cnt
, loff_t
*ppos
)
2820 static char ftrace_dyn_info_buffer
[1024];
2821 static DEFINE_MUTEX(dyn_info_mutex
);
2822 unsigned long *p
= filp
->private_data
;
2823 char *buf
= ftrace_dyn_info_buffer
;
2824 int size
= ARRAY_SIZE(ftrace_dyn_info_buffer
);
2827 mutex_lock(&dyn_info_mutex
);
2828 r
= sprintf(buf
, "%ld ", *p
);
2830 r
+= ftrace_arch_read_dyn_info(buf
+r
, (size
-1)-r
);
2833 r
= simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
2835 mutex_unlock(&dyn_info_mutex
);
2840 static struct file_operations tracing_dyn_info_fops
= {
2841 .open
= tracing_open_generic
,
2842 .read
= tracing_read_dyn_info
,
2846 static struct dentry
*d_tracer
;
2848 struct dentry
*tracing_init_dentry(void)
2855 d_tracer
= debugfs_create_dir("tracing", NULL
);
2857 if (!d_tracer
&& !once
) {
2859 pr_warning("Could not create debugfs directory 'tracing'\n");
2866 #ifdef CONFIG_FTRACE_SELFTEST
2867 /* Let selftest have access to static functions in this file */
2868 #include "trace_selftest.c"
2871 static __init
int tracer_init_debugfs(void)
2873 struct dentry
*d_tracer
;
2874 struct dentry
*entry
;
2876 d_tracer
= tracing_init_dentry();
2878 entry
= debugfs_create_file("tracing_enabled", 0644, d_tracer
,
2879 &global_trace
, &tracing_ctrl_fops
);
2881 pr_warning("Could not create debugfs 'tracing_enabled' entry\n");
2883 entry
= debugfs_create_file("trace_options", 0644, d_tracer
,
2884 NULL
, &tracing_iter_fops
);
2886 pr_warning("Could not create debugfs 'trace_options' entry\n");
2888 entry
= debugfs_create_file("tracing_cpumask", 0644, d_tracer
,
2889 NULL
, &tracing_cpumask_fops
);
2891 pr_warning("Could not create debugfs 'tracing_cpumask' entry\n");
2893 entry
= debugfs_create_file("latency_trace", 0444, d_tracer
,
2894 &global_trace
, &tracing_lt_fops
);
2896 pr_warning("Could not create debugfs 'latency_trace' entry\n");
2898 entry
= debugfs_create_file("trace", 0444, d_tracer
,
2899 &global_trace
, &tracing_fops
);
2901 pr_warning("Could not create debugfs 'trace' entry\n");
2903 entry
= debugfs_create_file("available_tracers", 0444, d_tracer
,
2904 &global_trace
, &show_traces_fops
);
2906 pr_warning("Could not create debugfs 'available_tracers' entry\n");
2908 entry
= debugfs_create_file("current_tracer", 0444, d_tracer
,
2909 &global_trace
, &set_tracer_fops
);
2911 pr_warning("Could not create debugfs 'current_tracer' entry\n");
2913 entry
= debugfs_create_file("tracing_max_latency", 0644, d_tracer
,
2914 &tracing_max_latency
,
2915 &tracing_max_lat_fops
);
2917 pr_warning("Could not create debugfs "
2918 "'tracing_max_latency' entry\n");
2920 entry
= debugfs_create_file("tracing_thresh", 0644, d_tracer
,
2921 &tracing_thresh
, &tracing_max_lat_fops
);
2923 pr_warning("Could not create debugfs "
2924 "'tracing_thresh' entry\n");
2925 entry
= debugfs_create_file("README", 0644, d_tracer
,
2926 NULL
, &tracing_readme_fops
);
2928 pr_warning("Could not create debugfs 'README' entry\n");
2930 entry
= debugfs_create_file("trace_pipe", 0644, d_tracer
,
2931 NULL
, &tracing_pipe_fops
);
2933 pr_warning("Could not create debugfs "
2934 "'trace_pipe' entry\n");
2936 entry
= debugfs_create_file("buffer_size_kb", 0644, d_tracer
,
2937 &global_trace
, &tracing_entries_fops
);
2939 pr_warning("Could not create debugfs "
2940 "'buffer_size_kb' entry\n");
2942 entry
= debugfs_create_file("trace_marker", 0220, d_tracer
,
2943 NULL
, &tracing_mark_fops
);
2945 pr_warning("Could not create debugfs "
2946 "'trace_marker' entry\n");
2948 #ifdef CONFIG_DYNAMIC_FTRACE
2949 entry
= debugfs_create_file("dyn_ftrace_total_info", 0444, d_tracer
,
2950 &ftrace_update_tot_cnt
,
2951 &tracing_dyn_info_fops
);
2953 pr_warning("Could not create debugfs "
2954 "'dyn_ftrace_total_info' entry\n");
2956 #ifdef CONFIG_SYSPROF_TRACER
2957 init_tracer_sysprof_debugfs(d_tracer
);
2962 int trace_vprintk(unsigned long ip
, int depth
, const char *fmt
, va_list args
)
2964 static DEFINE_SPINLOCK(trace_buf_lock
);
2965 static char trace_buf
[TRACE_BUF_SIZE
];
2967 struct ring_buffer_event
*event
;
2968 struct trace_array
*tr
= &global_trace
;
2969 struct trace_array_cpu
*data
;
2970 int cpu
, len
= 0, size
, pc
;
2971 struct print_entry
*entry
;
2972 unsigned long irq_flags
;
2974 if (tracing_disabled
|| tracing_selftest_running
)
2977 pc
= preempt_count();
2978 preempt_disable_notrace();
2979 cpu
= raw_smp_processor_id();
2980 data
= tr
->data
[cpu
];
2982 if (unlikely(atomic_read(&data
->disabled
)))
2985 pause_graph_tracing();
2986 spin_lock_irqsave(&trace_buf_lock
, irq_flags
);
2987 len
= vsnprintf(trace_buf
, TRACE_BUF_SIZE
, fmt
, args
);
2989 len
= min(len
, TRACE_BUF_SIZE
-1);
2992 size
= sizeof(*entry
) + len
+ 1;
2993 event
= trace_buffer_lock_reserve(tr
, TRACE_PRINT
, size
, irq_flags
, pc
);
2996 entry
= ring_buffer_event_data(event
);
2998 entry
->depth
= depth
;
3000 memcpy(&entry
->buf
, trace_buf
, len
);
3001 entry
->buf
[len
] = 0;
3002 ring_buffer_unlock_commit(tr
->buffer
, event
);
3005 spin_unlock_irqrestore(&trace_buf_lock
, irq_flags
);
3006 unpause_graph_tracing();
3008 preempt_enable_notrace();
3012 EXPORT_SYMBOL_GPL(trace_vprintk
);
3014 int __ftrace_printk(unsigned long ip
, const char *fmt
, ...)
3019 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3023 ret
= trace_vprintk(ip
, task_curr_ret_stack(current
), fmt
, ap
);
3027 EXPORT_SYMBOL_GPL(__ftrace_printk
);
3029 int __ftrace_vprintk(unsigned long ip
, const char *fmt
, va_list ap
)
3031 if (!(trace_flags
& TRACE_ITER_PRINTK
))
3034 return trace_vprintk(ip
, task_curr_ret_stack(current
), fmt
, ap
);
3036 EXPORT_SYMBOL_GPL(__ftrace_vprintk
);
3038 static int trace_panic_handler(struct notifier_block
*this,
3039 unsigned long event
, void *unused
)
3041 if (ftrace_dump_on_oops
)
3046 static struct notifier_block trace_panic_notifier
= {
3047 .notifier_call
= trace_panic_handler
,
3049 .priority
= 150 /* priority: INT_MAX >= x >= 0 */
3052 static int trace_die_handler(struct notifier_block
*self
,
3058 if (ftrace_dump_on_oops
)
3067 static struct notifier_block trace_die_notifier
= {
3068 .notifier_call
= trace_die_handler
,
3073 * printk is set to max of 1024, we really don't need it that big.
3074 * Nothing should be printing 1000 characters anyway.
3076 #define TRACE_MAX_PRINT 1000
3079 * Define here KERN_TRACE so that we have one place to modify
3080 * it if we decide to change what log level the ftrace dump
3083 #define KERN_TRACE KERN_EMERG
3086 trace_printk_seq(struct trace_seq
*s
)
3088 /* Probably should print a warning here. */
3092 /* should be zero ended, but we are paranoid. */
3093 s
->buffer
[s
->len
] = 0;
3095 printk(KERN_TRACE
"%s", s
->buffer
);
3100 void ftrace_dump(void)
3102 static DEFINE_SPINLOCK(ftrace_dump_lock
);
3103 /* use static because iter can be a bit big for the stack */
3104 static struct trace_iterator iter
;
3105 static int dump_ran
;
3106 unsigned long flags
;
3110 spin_lock_irqsave(&ftrace_dump_lock
, flags
);
3116 /* No turning back! */
3120 for_each_tracing_cpu(cpu
) {
3121 atomic_inc(&global_trace
.data
[cpu
]->disabled
);
3124 /* don't look at user memory in panic mode */
3125 trace_flags
&= ~TRACE_ITER_SYM_USEROBJ
;
3127 printk(KERN_TRACE
"Dumping ftrace buffer:\n");
3129 iter
.tr
= &global_trace
;
3130 iter
.trace
= current_trace
;
3133 * We need to stop all tracing on all CPUS to read the
3134 * the next buffer. This is a bit expensive, but is
3135 * not done often. We fill all what we can read,
3136 * and then release the locks again.
3139 while (!trace_empty(&iter
)) {
3142 printk(KERN_TRACE
"---------------------------------\n");
3146 /* reset all but tr, trace, and overruns */
3147 memset(&iter
.seq
, 0,
3148 sizeof(struct trace_iterator
) -
3149 offsetof(struct trace_iterator
, seq
));
3150 iter
.iter_flags
|= TRACE_FILE_LAT_FMT
;
3153 if (find_next_entry_inc(&iter
) != NULL
) {
3154 print_trace_line(&iter
);
3155 trace_consume(&iter
);
3158 trace_printk_seq(&iter
.seq
);
3162 printk(KERN_TRACE
" (ftrace buffer empty)\n");
3164 printk(KERN_TRACE
"---------------------------------\n");
3167 spin_unlock_irqrestore(&ftrace_dump_lock
, flags
);
3170 __init
static int tracer_alloc_buffers(void)
3172 struct trace_array_cpu
*data
;
3176 if (!alloc_cpumask_var(&tracing_buffer_mask
, GFP_KERNEL
))
3179 if (!alloc_cpumask_var(&tracing_cpumask
, GFP_KERNEL
))
3180 goto out_free_buffer_mask
;
3182 cpumask_copy(tracing_buffer_mask
, cpu_possible_mask
);
3183 cpumask_copy(tracing_cpumask
, cpu_all_mask
);
3185 /* TODO: make the number of buffers hot pluggable with CPUS */
3186 global_trace
.buffer
= ring_buffer_alloc(trace_buf_size
,
3187 TRACE_BUFFER_FLAGS
);
3188 if (!global_trace
.buffer
) {
3189 printk(KERN_ERR
"tracer: failed to allocate ring buffer!\n");
3191 goto out_free_cpumask
;
3193 global_trace
.entries
= ring_buffer_size(global_trace
.buffer
);
3196 #ifdef CONFIG_TRACER_MAX_TRACE
3197 max_tr
.buffer
= ring_buffer_alloc(trace_buf_size
,
3198 TRACE_BUFFER_FLAGS
);
3199 if (!max_tr
.buffer
) {
3200 printk(KERN_ERR
"tracer: failed to allocate max ring buffer!\n");
3202 ring_buffer_free(global_trace
.buffer
);
3203 goto out_free_cpumask
;
3205 max_tr
.entries
= ring_buffer_size(max_tr
.buffer
);
3206 WARN_ON(max_tr
.entries
!= global_trace
.entries
);
3209 /* Allocate the first page for all buffers */
3210 for_each_tracing_cpu(i
) {
3211 data
= global_trace
.data
[i
] = &per_cpu(global_trace_cpu
, i
);
3212 max_tr
.data
[i
] = &per_cpu(max_data
, i
);
3215 trace_init_cmdlines();
3217 register_tracer(&nop_trace
);
3218 current_trace
= &nop_trace
;
3219 #ifdef CONFIG_BOOT_TRACER
3220 register_tracer(&boot_tracer
);
3222 /* All seems OK, enable tracing */
3223 tracing_disabled
= 0;
3225 atomic_notifier_chain_register(&panic_notifier_list
,
3226 &trace_panic_notifier
);
3228 register_die_notifier(&trace_die_notifier
);
3232 free_cpumask_var(tracing_cpumask
);
3233 out_free_buffer_mask
:
3234 free_cpumask_var(tracing_buffer_mask
);
3239 __init
static int clear_boot_tracer(void)
3242 * The default tracer at boot buffer is an init section.
3243 * This function is called in lateinit. If we did not
3244 * find the boot tracer, then clear it out, to prevent
3245 * later registration from accessing the buffer that is
3246 * about to be freed.
3248 if (!default_bootup_tracer
)
3251 printk(KERN_INFO
"ftrace bootup tracer '%s' not registered.\n",
3252 default_bootup_tracer
);
3253 default_bootup_tracer
= NULL
;
3258 early_initcall(tracer_alloc_buffers
);
3259 fs_initcall(tracer_init_debugfs
);
3260 late_initcall(clear_boot_tracer
);